/* * Idea is to split the address space into uniform bias ranges, and then * in some random order allocate within each bias, using various * patterns within. This should detect if allocations leak out from a * given bias, for example.
*/
for (i = 0; i < count; i++) {
LIST_HEAD(tmp);
u32 size;
/* single big page */
KUNIT_ASSERT_FALSE_MSG(test,
drm_buddy_alloc_blocks(&mm, bias_start,
bias_end, bias_size, bias_size,
&tmp,
DRM_BUDDY_RANGE_ALLOCATION), "buddy_alloc i failed with bias(%x-%x), size=%u, ps=%u\n",
bias_start, bias_end, bias_size, bias_size);
drm_buddy_free_list(&mm, &tmp, 0);
/* single page with internal round_up */
KUNIT_ASSERT_FALSE_MSG(test,
drm_buddy_alloc_blocks(&mm, bias_start,
bias_end, ps, bias_size,
&tmp,
DRM_BUDDY_RANGE_ALLOCATION), "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
bias_start, bias_end, ps, bias_size);
drm_buddy_free_list(&mm, &tmp, 0);
/* random size within */
size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps); if (size)
KUNIT_ASSERT_FALSE_MSG(test,
drm_buddy_alloc_blocks(&mm, bias_start,
bias_end, size, ps,
&tmp,
DRM_BUDDY_RANGE_ALLOCATION), "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
bias_start, bias_end, size, ps);
bias_rem -= size; /* too big for current avail */
KUNIT_ASSERT_TRUE_MSG(test,
drm_buddy_alloc_blocks(&mm, bias_start,
bias_end, bias_rem + ps, ps,
&allocated,
DRM_BUDDY_RANGE_ALLOCATION), "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
bias_start, bias_end, bias_rem + ps, ps);
if (bias_rem) { /* random fill of the remainder */
size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
size = max(size, ps);
KUNIT_ASSERT_FALSE_MSG(test,
drm_buddy_alloc_blocks(&mm, bias_start,
bias_end, size, ps,
&allocated,
DRM_BUDDY_RANGE_ALLOCATION), "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
bias_start, bias_end, size, ps); /* * Intentionally allow some space to be left * unallocated, and ideally not always on the bias * boundaries.
*/
drm_buddy_free_list(&mm, &tmp, 0);
} else {
list_splice_tail(&tmp, &allocated);
}
}
/* * Something more free-form. Idea is to pick a random starting bias * range within the address space and then start filling it up. Also * randomly grow the bias range in both directions as we go along. This * should give us bias start/end which is not always uniform like above, * and in some cases will require the allocator to jump over already * allocated nodes in the middle of the address space.
*/
/* * Try to randomly grow the bias range in both directions, or * only one, or perhaps don't grow at all.
*/ do {
u32 old_bias_start = bias_start;
u32 old_bias_end = bias_end;
if (bias_start)
bias_start -= round_up(prandom_u32_state(&prng) % bias_start, ps); if (bias_end != mm_size)
bias_end += round_up(prandom_u32_state(&prng) % (mm_size - bias_end), ps);
/* * Allocate cleared blocks in the bias range when the DRM buddy's clear avail is * zero. This will validate the bias range allocation in scenarios like system boot * when no cleared blocks are available and exercise the fallback path too. The resulting * blocks should always be dirty.
*/
/* * Idea is to allocate and free some random portion of the address space, * returning those pages as non-dirty and randomly alternate between * requesting dirty and non-dirty pages (not going over the limit * we freed as non-dirty), putting that into two separate lists. * Loop over both lists at the end checking that the dirty list * is indeed all dirty pages and vice versa. Free it all again, * keeping the dirty/clear status.
*/
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
5 * ps, ps, &allocated,
DRM_BUDDY_TOPDOWN_ALLOCATION), "buddy_alloc hit an error size=%lu\n", 5 * ps);
drm_buddy_free_list(&mm, &allocated, DRM_BUDDY_CLEARED);
n_pages = 10; do { unsignedlong flags; struct list_head *list; int slot = i % 2;
if (slot == 0) {
list = &dirty;
flags = 0;
} else {
list = &clean;
flags = DRM_BUDDY_CLEAR_ALLOCATION;
}
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
ps, ps, list,
flags), "buddy_alloc hit an error size=%lu\n", ps);
} while (++i < n_pages);
/* * Trying to go over the clear limit for some allocation. * The allocation should never fail with reasonable page-size.
*/
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
10 * ps, ps, &clean,
DRM_BUDDY_CLEAR_ALLOCATION), "buddy_alloc hit an error size=%lu\n", 10 * ps);
/* * Create a new mm. Intentionally fragment the address space by creating * two alternating lists. Free both lists, one as dirty the other as clean. * Try to allocate double the previous size with matching min_page_size. The * allocation should never fail as it calls the force_merge. Also check that * the page is always dirty after force_merge. Free the page as dirty, then * repeat the whole thing, increment the order until we hit the max_order.
*/
i = 0;
n_pages = mm_size / ps; do { struct list_head *list; int slot = i % 2;
if (slot == 0)
list = &dirty; else
list = &clean;
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
ps, ps, list, 0), "buddy_alloc hit an error size=%lu\n", ps);
} while (++i < n_pages);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
size, size, &allocated,
DRM_BUDDY_CLEAR_ALLOCATION), "buddy_alloc hit an error size=%u\n", size);
total = 0;
list_for_each_entry(block, &allocated, link) { if (size != mm_size)
KUNIT_EXPECT_EQ(test, drm_buddy_block_is_clear(block), false);
total += drm_buddy_block_size(&mm, block);
}
KUNIT_EXPECT_EQ(test, total, size);
drm_buddy_free_list(&mm, &allocated, 0);
} while (++order <= max_order);
drm_buddy_fini(&mm);
/* * Create a new mm with a non power-of-two size. Allocate a random size from each * root, free as cleared and then call fini. This will ensure the multi-root * force merge during fini.
*/
mm_size = (SZ_4K << max_order) + (SZ_4K << (max_order - 2));
/* * Idea is to fragment the address space by alternating block * allocations between three different lists; one for left, middle and * right. We can then free a list to simulate fragmentation. In * particular we want to exercise the DRM_BUDDY_CONTIGUOUS_ALLOCATION, * including the try_harder path.
*/
i = 0;
n_pages = mm_size / ps; do { struct list_head *list; int slot = i % 3;
if (slot == 0)
list = &left; elseif (slot == 1)
list = &middle; else
list = &right;
KUNIT_ASSERT_FALSE_MSG(test,
drm_buddy_alloc_blocks(&mm, 0, mm_size,
ps, ps, list, 0), "buddy_alloc hit an error size=%lu\n",
ps);
} while (++i < n_pages);
drm_buddy_free_list(&mm, &right, 0);
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
3 * ps, ps, &allocated,
DRM_BUDDY_CONTIGUOUS_ALLOCATION), "buddy_alloc didn't error size=%lu\n", 3 * ps); /* * At this point we should have enough contiguous space for 2 blocks, * however they are never buddies (since we freed middle and right) so * will require the try_harder logic to find them.
*/
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
2 * ps, ps, &allocated,
DRM_BUDDY_CONTIGUOUS_ALLOCATION), "buddy_alloc hit an error size=%lu\n", 2 * ps);
/* * Create a pot-sized mm, then allocate one of each possible * order within. This should leave the mm with exactly one * page left. Free the largest block, then whittle down again. * Eventually we will have a fully 50% fragmented mm.
*/
for (top = max_order; top; top--) { /* Make room by freeing the largest allocated block */
block = list_first_entry_or_null(&blocks, typeof(*block), link); if (block) {
list_del(&block->link);
drm_buddy_free_block(&mm, block);
}
for (order = top; order--;) {
size = get_size(order, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start,
mm_size, size, size,
&tmp, flags), "buddy_alloc hit -ENOMEM with order=%d, top=%d\n",
order, top);
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
list_move_tail(&block->link, &blocks);
}
/* There should be one final page for this sub-allocation */
size = get_size(0, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc hit -ENOMEM for hole\n");
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
list_move_tail(&block->link, &holes);
size = get_size(top, mm.chunk_size);
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc unexpectedly succeeded at top-order %d/%d, it should be full!",
top, max_order);
}
drm_buddy_free_list(&mm, &holes, 0);
/* Nothing larger than blocks of chunk_size now available */ for (order = 1; order <= max_order; order++) {
size = get_size(order, mm.chunk_size);
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc unexpectedly succeeded at order %d, it should be full!",
order);
}
for (order = 0; order < max_order; order++) {
size = get_size(order, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc hit -ENOMEM with order=%d\n",
order);
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
list_move_tail(&block->link, &blocks);
}
/* And now the last remaining block available */
size = get_size(0, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc hit -ENOMEM on final alloc\n");
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
list_move_tail(&block->link, &blocks);
/* Should be completely full! */ for (order = max_order; order--;) {
size = get_size(order, mm.chunk_size);
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc unexpectedly succeeded, it should be full!");
}
/* As we free in increasing size, we make available larger blocks */
order = 1;
list_for_each_entry_safe(block, bn, &blocks, link) {
list_del(&block->link);
drm_buddy_free_block(&mm, block);
size = get_size(order, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc hit -ENOMEM with order=%d\n",
order);
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
/* To confirm, now the whole mm should be available */
size = get_size(max_order, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc (realloc) hit -ENOMEM with order=%d\n",
max_order);
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
for (order = 0; order <= max_order; order++) {
size = get_size(order, mm.chunk_size);
KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc hit -ENOMEM with order=%d\n",
order);
block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
list_move_tail(&block->link, &blocks);
}
/* Should be completely full! */
size = get_size(0, mm.chunk_size);
KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
size, size, &tmp, flags), "buddy_alloc unexpectedly succeeded, it should be full!");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.