* [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation
@ 2026-10-21 12:18 Arunpravin Paneer Selvam
2026-10-21 12:18 ` [PATCH v6 2/2] gpu/buddy: add range-restricted contiguous allocation fallback Arunpravin Paneer Selvam
2026-10-21 12:49 ` [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Matthew Auld
0 siblings, 2 replies; 5+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-10-21 12:18 UTC (permalink / raw)
To: matthew.auld, dri-devel, intel-gfx, intel-xe, amd-gfx
Cc: christian.koenig, alexander.deucher, Anand.Raghavendra,
Arunpravin Paneer Selvam
From: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Range-restricted allocations (the visible-VRAM aperture) skip
dirty_steer_window() and reach __alloc_range_bias() with no clear/dirty
eligibility guard, so a dirty allocation freely splits clear extents (and
vice versa), interleaving the two classes until no large same-class run
survives and a 4K scanout pin fails with -ENOSPC. Use a two-pass range
search instead: block_incompatible() rejects opposite-class blocks in the
strict pass and relaxes only when the strict pass returns -ENOSPC, so a
request still succeeds when only opposite-class space remains. A KUnit
regression test covers the interleaving pattern and fails without the
guard.
v2: the strict pass can split a block and then reject both children, so
a split no longer implies a compatible block exists. Sashiko reported
the resulting leaked splits; fix as suggested by Matthew:
- Push the parent back on the DFS stack after each split and merge it
back with unsplit_block() if both children are still free.
- Make block_incompatible() judge free blocks only (split nodes keep a
stale clear bit) and take the requested order, so mixed blocks that
can hold a compatible block are descended instead of skipped.
- Extend the KUnit test with a coalesced mixed block and a check that
a rejected split is merged back.
Fixes: 1ad5e807f716 ("gpu/buddy: replace dual-tree/force_merge with decoupled dirty tracker")
Assisted-by: LLM
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Christian König <christian.koenig@amd.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
---
drivers/gpu/buddy.c | 112 ++++++++++++++++++----------
drivers/gpu/tests/gpu_buddy_test.c | 114 +++++++++++++++++++++++++++++
2 files changed, 189 insertions(+), 37 deletions(-)
diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index 503f7d5934e4..8eb5835d6944 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -1240,11 +1240,46 @@ static void __gpu_buddy_undo_splits(struct gpu_buddy *mm,
}
}
+static void unsplit_block(struct gpu_buddy *mm,
+ struct gpu_buddy_block *block)
+{
+ unsigned int child_order = gpu_buddy_block_order(block) - 1;
+
+ rbtree_remove(mm, block->left);
+ rbtree_remove(mm, block->right);
+ mm->free_scoreboard[child_order] -= 2;
+
+ gpu_block_free(mm, block->left);
+ gpu_block_free(mm, block->right);
+ block->left = NULL;
+ block->right = NULL;
+
+ __mark_free(mm, block, gpu_block_cached_state(block));
+}
+
+static bool block_incompatible(struct gpu_buddy_block *block,
+ unsigned int order,
+ unsigned long flags)
+{
+ bool needs_clear = flags & GPU_BUDDY_CLEAR_ALLOCATION;
+
+ if (!gpu_buddy_block_is_free(block))
+ return false;
+
+ if (gpu_buddy_block_order(block) == order)
+ return needs_clear ? !gpu_buddy_block_is_clear(block) :
+ block->has_clear;
+
+ return needs_clear ? !block->has_clear :
+ gpu_buddy_block_is_clear(block);
+}
+
static struct gpu_buddy_block *
__alloc_range_bias(struct gpu_buddy *mm,
u64 start, u64 end,
unsigned int order,
- unsigned long flags)
+ unsigned long flags,
+ bool fallback)
{
u64 req_size = mm->chunk_size << order;
struct gpu_buddy_block *block;
@@ -1258,9 +1293,7 @@ __alloc_range_bias(struct gpu_buddy *mm,
* This range-constrained search hands back the highest/right-most
* address that satisfies the request: the roots are seeded high-to-low
* and the right (higher-address) child is descended first, making
- * top-down the default placement here. A non-top-down clear request is
- * the only exception, where the descent is biased towards clear or
- * clear-containing subtrees to satisfy the clear preference.
+ * top-down the default placement here.
*/
for (i = mm->n_roots - 1; i >= 0; --i)
list_add_tail(&mm->roots[i]->tmp_link, &dfs);
@@ -1277,6 +1310,17 @@ __alloc_range_bias(struct gpu_buddy *mm,
list_del(&block->tmp_link);
+ /*
+ * We split this block earlier and found nothing compatible in
+ * either child; merge it back before moving on.
+ */
+ if (gpu_buddy_block_is_split(block) &&
+ gpu_buddy_block_is_free(block->left) &&
+ gpu_buddy_block_is_free(block->right)) {
+ unsplit_block(mm, block);
+ continue;
+ }
+
if (gpu_buddy_block_order(block) < order)
continue;
@@ -1298,6 +1342,9 @@ __alloc_range_bias(struct gpu_buddy *mm,
continue;
}
+ if (!fallback && block_incompatible(block, order, flags))
+ continue;
+
if (contains(start, end, block_start, block_end) &&
order == gpu_buddy_block_order(block)) {
/*
@@ -1313,40 +1360,13 @@ __alloc_range_bias(struct gpu_buddy *mm,
err = split_block(mm, block);
if (unlikely(err))
goto err_undo;
- }
-
- /*
- * Top-down is a strict address-placement policy, so when it is
- * requested we ignore clear steering and simply descend the
- * right (higher-address) child first. Only a non-top-down clear
- * request biases the descent towards clear/has_clear subtrees.
- */
- if ((flags & GPU_BUDDY_CLEAR_ALLOCATION) &&
- !(flags & GPU_BUDDY_TOPDOWN_ALLOCATION)) {
- struct gpu_buddy_block *prefer;
-
- if (gpu_buddy_block_is_clear(block->right))
- prefer = block->right;
- else if (gpu_buddy_block_is_clear(block->left))
- prefer = block->left;
- else if (block->right->has_clear)
- prefer = block->right;
- else if (block->left->has_clear)
- prefer = block->left;
- else
- prefer = block->right;
- if (prefer == block->right) {
- list_add(&block->left->tmp_link, &dfs);
- list_add(&block->right->tmp_link, &dfs);
- } else {
- list_add(&block->right->tmp_link, &dfs);
- list_add(&block->left->tmp_link, &dfs);
- }
- } else {
- list_add(&block->left->tmp_link, &dfs);
- list_add(&block->right->tmp_link, &dfs);
+ /* Revisit after the children so the split can be undone. */
+ list_add(&block->tmp_link, &dfs);
}
+
+ list_add(&block->left->tmp_link, &dfs);
+ list_add(&block->right->tmp_link, &dfs);
} while (1);
return ERR_PTR(-ENOSPC);
@@ -1361,6 +1381,24 @@ __alloc_range_bias(struct gpu_buddy *mm,
return ERR_PTR(err);
}
+static struct gpu_buddy_block *
+__gpu_buddy_alloc_range_bias(struct gpu_buddy *mm,
+ u64 start, u64 end,
+ unsigned int order,
+ unsigned long flags)
+{
+ struct gpu_buddy_block *block;
+ bool fallback = false;
+
+ block = __alloc_range_bias(mm, start, end, order,
+ flags, fallback);
+ if (IS_ERR(block))
+ return __alloc_range_bias(mm, start, end, order,
+ flags, !fallback);
+
+ return block;
+}
+
/* Return the highest-address free block of at least @order. */
static struct gpu_buddy_block *
get_maxblock(struct gpu_buddy *mm,
@@ -1924,7 +1962,7 @@ __gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
if (flags & GPU_BUDDY_RANGE_ALLOCATION) {
/* Allocate traversing within the range */
- block = __alloc_range_bias(mm, start, end, order, flags);
+ block = __gpu_buddy_alloc_range_bias(mm, start, end, order, flags);
if (!IS_ERR(block) || !steered)
return block;
diff --git a/drivers/gpu/tests/gpu_buddy_test.c b/drivers/gpu/tests/gpu_buddy_test.c
index b75d32ca6ca0..b78e3307958b 100644
--- a/drivers/gpu/tests/gpu_buddy_test.c
+++ b/drivers/gpu/tests/gpu_buddy_test.c
@@ -1597,6 +1597,119 @@ static int gpu_buddy_suite_init(struct kunit_suite *suite)
return 0;
}
+static void gpu_test_buddy_alloc_range_clear_guard(struct kunit *test)
+{
+ const unsigned long chunk = SZ_1M, mm_size = SZ_4M;
+ struct gpu_buddy_block *block;
+ LIST_HEAD(high_clear_a);
+ LIST_HEAD(high_clear_b);
+ LIST_HEAD(low_dirty);
+ struct gpu_buddy mm;
+ LIST_HEAD(barrier);
+ LIST_HEAD(result);
+
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, mm_size, chunk),
+ "buddy_init failed\n");
+
+ /*
+ * Lay out a 4 MiB aperture as three regions:
+ *
+ * [0, 1M) dirty free
+ * [1M, 2M) allocated (barrier, keeps the two halves un-mergeable)
+ * [2M, 4M) clear free (one naturally aligned 2 MiB extent)
+ *
+ * A dirty (non-clear) range allocation must take the low dirty block
+ * and leave the large clear extent intact. Without the guard the
+ * top-down search cuts the higher clear extent instead and returns a
+ * clear block -- the interleaving that fragments the aperture and
+ * ultimately fails a large same-class scanout allocation.
+ */
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 0, chunk, chunk, chunk,
+ &low_dirty, GPU_BUDDY_RANGE_ALLOCATION),
+ "alloc [0, 1M) failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, chunk, 2 * chunk, chunk, chunk,
+ &barrier, GPU_BUDDY_RANGE_ALLOCATION),
+ "alloc [1M, 2M) failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 2 * chunk, 3 * chunk, chunk, chunk,
+ &high_clear_a, GPU_BUDDY_RANGE_ALLOCATION),
+ "alloc [2M, 3M) failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 3 * chunk, 4 * chunk, chunk, chunk,
+ &high_clear_b, GPU_BUDDY_RANGE_ALLOCATION),
+ "alloc [3M, 4M) failed\n");
+
+ /* [2M, 4M) coalesces into one aligned clear extent; [0, 1M) stays dirty. */
+ gpu_buddy_free_list(&mm, &low_dirty, 0);
+ gpu_buddy_free_list(&mm, &high_clear_a, GPU_BUDDY_CLEARED);
+ gpu_buddy_free_list(&mm, &high_clear_b, GPU_BUDDY_CLEARED);
+
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 0, mm_size, chunk, chunk,
+ &result, GPU_BUDDY_RANGE_ALLOCATION),
+ "dirty range alloc failed\n");
+
+ block = list_first_entry(&result, struct gpu_buddy_block, link);
+ KUNIT_EXPECT_FALSE_MSG(test, gpu_buddy_block_is_clear(block),
+ "dirty range alloc cut the clear extent (offset=%llx)\n",
+ gpu_buddy_block_offset(block));
+ KUNIT_EXPECT_EQ(test, gpu_buddy_block_offset(block), 0ULL);
+
+ /*
+ * Coalesced GPU_BLOCK_MIXED subtree:
+ *
+ * [0, 1M) allocated (result)
+ * [1M, 2M) dirty free
+ * [2M, 3M) clear free } coalesced into [2M, 4M) GPU_BLOCK_MIXED
+ * [3M, 4M) dirty free }
+ *
+ * A dirty range alloc restricted to [1M, 3M) visits [2M, 4M) first,
+ * splits it, rejects the clear [2M, 3M) half and must unsplit
+ * [2M, 4M) before taking [1M, 2M). Otherwise the split leaks.
+ */
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 2 * chunk, 3 * chunk, chunk, chunk,
+ &high_clear_a, GPU_BUDDY_RANGE_ALLOCATION),
+ "alloc [2M, 3M) failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 3 * chunk, 4 * chunk, chunk, chunk,
+ &high_clear_b, GPU_BUDDY_RANGE_ALLOCATION),
+ "alloc [3M, 4M) failed\n");
+ gpu_buddy_free_list(&mm, &barrier, 0);
+ gpu_buddy_free_list(&mm, &high_clear_a, GPU_BUDDY_CLEARED);
+ gpu_buddy_free_list(&mm, &high_clear_b, 0);
+
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, chunk, 3 * chunk, chunk, chunk,
+ &low_dirty, GPU_BUDDY_RANGE_ALLOCATION),
+ "dirty range alloc [1M, 3M) failed\n");
+ block = list_first_entry(&low_dirty, struct gpu_buddy_block, link);
+ KUNIT_EXPECT_FALSE(test, gpu_buddy_block_is_clear(block));
+ KUNIT_EXPECT_EQ(test, gpu_buddy_block_offset(block), (u64)chunk);
+
+ /* The rejected split of [2M, 4M) must have been merged back. */
+ KUNIT_EXPECT_EQ(test, mm.free_scoreboard[0], 0ULL);
+ KUNIT_EXPECT_EQ(test, mm.free_scoreboard[1], 1ULL);
+
+ /* A clear alloc must descend into the mixed [2M, 4M) block. */
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 0, mm_size, chunk, chunk,
+ &high_clear_a,
+ GPU_BUDDY_RANGE_ALLOCATION |
+ GPU_BUDDY_CLEAR_ALLOCATION),
+ "clear range alloc failed\n");
+ block = list_first_entry(&high_clear_a, struct gpu_buddy_block, link);
+ KUNIT_EXPECT_TRUE(test, gpu_buddy_block_is_clear(block));
+ KUNIT_EXPECT_EQ(test, gpu_buddy_block_offset(block), (u64)(2 * chunk));
+
+ gpu_buddy_free_list(&mm, &high_clear_a, GPU_BUDDY_CLEARED);
+ gpu_buddy_free_list(&mm, &low_dirty, 0);
+ gpu_buddy_free_list(&mm, &result, 0);
+ gpu_buddy_fini(&mm);
+}
+
static struct kunit_case gpu_buddy_tests[] = {
KUNIT_CASE(gpu_test_buddy_alloc_limit),
KUNIT_CASE(gpu_test_buddy_alloc_optimistic),
@@ -1606,6 +1719,7 @@ static struct kunit_case gpu_buddy_tests[] = {
KUNIT_CASE(gpu_test_buddy_alloc_clear),
KUNIT_CASE(gpu_test_buddy_alloc_range),
KUNIT_CASE(gpu_test_buddy_alloc_range_bias),
+ KUNIT_CASE(gpu_test_buddy_alloc_range_clear_guard),
KUNIT_CASE_SLOW(gpu_test_buddy_fragmentation_performance),
KUNIT_CASE_SLOW(gpu_test_buddy_dirty_tracker_performance),
KUNIT_CASE(gpu_test_buddy_alloc_exceeds_max_order),
base-commit: b3de601e57d983e4a42e27d499516041c11c2bde
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v6 2/2] gpu/buddy: add range-restricted contiguous allocation fallback
2026-10-21 12:18 [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Arunpravin Paneer Selvam
@ 2026-10-21 12:18 ` Arunpravin Paneer Selvam
2026-10-21 12:46 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Matthew Auld
1 sibling, 1 reply; 5+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-10-21 12:18 UTC (permalink / raw)
To: matthew.auld, dri-devel, intel-gfx, intel-xe, amd-gfx
Cc: christian.koenig, alexander.deucher, Anand.Raghavendra,
Arunpravin Paneer Selvam
From: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
From: Arunpravin Paneer Selvam <arunpravin.paneerselvam@amd.com>
A range + contiguous allocation (e.g. a scanout FB confined to the
CPU-visible VRAM aperture) rounds its size up to a power of two and
requires a naturally aligned free block of that size; on a fragmented
aperture no such aligned block may exist even though enough contiguous
space is free, so the allocation fails with -ENOSPC.
The non-range contiguous path already recovers from this via
__alloc_contig_try_harder(), which stitches an exact-size span from
smaller adjacent blocks, but that fallback was unreachable once
GPU_BUDDY_RANGE_ALLOCATION was set. Extend __alloc_contig_try_harder()
with a [range_start, range_end) window and route the range + contiguous
case through it: each candidate placement is confined to the window and
aligned to min_block_size. Non-range callers pass [0, mm->size), where
the guards are no-ops, so the existing behaviour is unchanged. A KUnit
test covers the fragmentation pattern.
v6: replace the unbounded aligned-offset sweep with a bounded higher-order
free-tree walk, and clamp partial -ENOSPC fills past range_end
(reported by Sashiko).
v5: make the windowed try-harder search complete so it no longer returns
a spurious -ENOSPC while a valid placement exists:
- Descend through the lower orders instead of bailing out at the
requested order, so smaller free blocks are still considered.
- Clamp an out-of-range full fill back to range_end so the leftward
shift re-seats the span inside the window (reported by Sashiko).
- Add a final sweep of every min_block_size-aligned offset in the
window, catching a placement inside a larger coalesced block
(reported by Sashiko).
v4:
- As Matthew pointed out, the Fixes: tag did not fit this patch: it adds
try-harder range support, a new safety-net feature rather than the fix.
The root cause is __alloc_range_bias() losing its clear/dirty guard.
v3:
- Confine the try_harder scan to [range_start, range_end) to avoid
speculative out-of-window alloc/free.
v2:
- Drop the split-undo patch; the range-bias search always descends to
an exact-order block or fails the split (already handled), so the
extra undo was redundant. (Matthew)
Assisted-by: LLM
Cc: Matthew Auld <matthew.auld@intel.com>
Cc: Christian König <christian.koenig@amd.com>
Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
---
drivers/gpu/buddy.c | 166 ++++++++++++++++------
drivers/gpu/tests/gpu_buddy_test.c | 218 ++++++++++++++++++++++++++++-
2 files changed, 335 insertions(+), 49 deletions(-)
diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
index 8eb5835d6944..693176ee3daf 100644
--- a/drivers/gpu/buddy.c
+++ b/drivers/gpu/buddy.c
@@ -1723,43 +1723,32 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
blocks, total_allocated_on_err);
}
-static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
- u64 unaligned_offset,
- u64 size,
- u64 min_block_size,
- unsigned long flags,
- struct list_head *blocks)
-{
- u64 aligned_offset = round_down(unaligned_offset, min_block_size);
-
- return __gpu_buddy_alloc_range(mm, aligned_offset, size, flags,
- NULL, blocks);
-}
-
static int __alloc_contig_try_harder(struct gpu_buddy *mm,
+ u64 range_start, u64 range_end,
u64 size,
u64 min_block_size,
unsigned long flags,
struct list_head *blocks)
{
- u64 rhs_offset, lhs_offset, filled;
+ u64 rhs_offset, lhs_offset, filled, aligned;
struct gpu_buddy_block *block;
+ unsigned int min_order, order;
struct rb_root *root;
struct rb_node *iter;
unsigned long pages;
- unsigned int order;
u64 modify_size;
int err;
+ min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
+
modify_size = rounddown_pow_of_two(size);
pages = modify_size >> ilog2(mm->chunk_size);
order = fls(pages) - 1;
- if (order == 0)
- return -ENOSPC;
+retry_order:
root = &mm->free_tree[order];
if (RB_EMPTY_ROOT(root))
- return -ENOSPC;
+ goto next_order;
iter = rb_last(root);
while (iter) {
@@ -1767,25 +1756,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
rhs_offset = gpu_buddy_block_offset(block);
+ if (rhs_offset >= range_end) {
+ iter = rb_prev(iter);
+ continue;
+ }
+ if (rhs_offset < range_start)
+ break;
+
/* Allocate blocks traversing RHS */
err = __gpu_buddy_alloc_range(mm, rhs_offset, size,
flags, &filled, blocks);
if (err && err != -ENOSPC)
return err;
- if (!err && IS_ALIGNED(rhs_offset, min_block_size))
+
+ if (!err)
+ filled = size;
+
+ /* Clamp a full or partial fill that crosses range_end. */
+ if (rhs_offset + filled > range_end) {
+ gpu_buddy_free_list_internal(mm, blocks);
+ filled = range_end - rhs_offset;
+ err = -ENOSPC;
+ }
+
+ if (!err && IS_ALIGNED(rhs_offset, min_block_size) &&
+ rhs_offset >= range_start)
return 0;
+
if (!err) {
/* Allocate the unaligned RHS offset using round_down */
gpu_buddy_free_list_internal(mm, blocks);
- err = __alloc_contig_aligned_retry(mm, rhs_offset,
- size,
- min_block_size,
- flags, blocks);
- if (!err)
- return 0;
- if (err != -ENOSPC) {
- gpu_buddy_free_list_internal(mm, blocks);
- return err;
+
+ aligned = round_down(rhs_offset, min_block_size);
+ if (aligned >= range_start &&
+ aligned + size <= range_end) {
+ err = __gpu_buddy_alloc_range(mm, aligned, size,
+ flags, NULL, blocks);
+ if (!err)
+ return 0;
+ if (err != -ENOSPC) {
+ gpu_buddy_free_list_internal(mm, blocks);
+ return err;
+ }
}
goto next;
}
@@ -1797,21 +1809,73 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
/* Allocate the unaligned LHS offset using round_down */
gpu_buddy_free_list_internal(mm, blocks);
- err = __alloc_contig_aligned_retry(mm, lhs_offset,
- size,
- min_block_size,
- flags, blocks);
- if (!err)
- return 0;
- if (err != -ENOSPC) {
- gpu_buddy_free_list_internal(mm, blocks);
- return err;
+
+ aligned = round_down(lhs_offset, min_block_size);
+ if (aligned >= range_start && aligned + size <= range_end) {
+ err = __gpu_buddy_alloc_range(mm, aligned, size,
+ flags, NULL, blocks);
+ if (!err)
+ return 0;
+ if (err != -ENOSPC) {
+ gpu_buddy_free_list_internal(mm, blocks);
+ return err;
+ }
}
next:
gpu_buddy_free_list_internal(mm, blocks);
iter = rb_prev(iter);
}
+next_order:
+ if (order > min_order) {
+ order--;
+ goto retry_order;
+ }
+
+ /* A non-range request would already have used a larger free block. */
+ if (!(flags & GPU_BUDDY_RANGE_ALLOCATION))
+ return -ENOSPC;
+
+ /*
+ * Last resort: the span may sit inside a larger coalesced free block
+ * that the anchored search above never visits. Walk the higher-order
+ * free trees (bounded by the number of free blocks, not by the window
+ * size) and carve the highest aligned span that fits inside each block
+ * overlapping the window.
+ */
+ for (order = fls(modify_size >> ilog2(mm->chunk_size));
+ order <= mm->max_order; order++) {
+ for (iter = rb_last(&mm->free_tree[order]); iter;
+ iter = rb_prev(iter)) {
+ u64 low, high;
+
+ block = rbtree_get_free_block(iter);
+ low = max(gpu_buddy_block_offset(block), range_start);
+ high = min(gpu_buddy_block_offset(block) +
+ gpu_buddy_block_size(mm, block), range_end);
+
+ if (gpu_buddy_block_offset(block) >= range_end)
+ continue;
+ if (gpu_buddy_block_offset(block) +
+ gpu_buddy_block_size(mm, block) <= range_start)
+ break;
+ if (high < low + size)
+ continue;
+
+ aligned = round_down(high - size, min_block_size);
+ if (aligned < low)
+ continue;
+
+ err = __gpu_buddy_alloc_range(mm, aligned, size, flags,
+ NULL, blocks);
+ if (!err)
+ return 0;
+ gpu_buddy_free_list_internal(mm, blocks);
+ if (err != -ENOSPC)
+ return err;
+ }
+ }
+
return -ENOSPC;
}
@@ -2059,11 +2123,18 @@ int gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
if (order > mm->max_order || size > mm->size) {
- if ((flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) &&
- !(flags & GPU_BUDDY_RANGE_ALLOCATION))
- return __alloc_contig_try_harder(mm, original_size,
+ if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) {
+ u64 range_start, range_end;
+
+ range_start = (flags & GPU_BUDDY_RANGE_ALLOCATION) ? start : 0;
+ range_end = (flags & GPU_BUDDY_RANGE_ALLOCATION) ? end : mm->size;
+
+ return __alloc_contig_try_harder(mm, range_start,
+ range_end,
+ original_size,
original_min_size,
flags, blocks);
+ }
return -EINVAL;
}
@@ -2096,9 +2167,14 @@ int gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
* Try contiguous block allocation through
* try harder method.
*/
- if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION &&
- !(flags & GPU_BUDDY_RANGE_ALLOCATION)) {
- err = __alloc_contig_try_harder(mm,
+ if (flags & GPU_BUDDY_CONTIGUOUS_ALLOCATION) {
+ u64 range_start, range_end;
+
+ range_start = (flags & GPU_BUDDY_RANGE_ALLOCATION) ? start : 0;
+ range_end = (flags & GPU_BUDDY_RANGE_ALLOCATION) ? end : mm->size;
+
+ err = __alloc_contig_try_harder(mm, range_start,
+ range_end,
original_size,
original_min_size,
flags,
@@ -2106,9 +2182,9 @@ int gpu_buddy_alloc_blocks(struct gpu_buddy *mm,
if (!err)
return 0;
if (err != -ENOSPC)
- return err;
- goto err_free;
+ goto err_free;
}
+
err = -ENOSPC;
goto err_free;
} while (1);
diff --git a/drivers/gpu/tests/gpu_buddy_test.c b/drivers/gpu/tests/gpu_buddy_test.c
index b78e3307958b..46fdd1208c82 100644
--- a/drivers/gpu/tests/gpu_buddy_test.c
+++ b/drivers/gpu/tests/gpu_buddy_test.c
@@ -1251,6 +1251,147 @@ static void gpu_test_buddy_alloc_contiguous(struct kunit *test)
gpu_buddy_fini(&mm);
}
+/*
+ * Issue a range-restricted contiguous request and assert that it returns an
+ * exact-size, contiguous span confined to [range_start, range_end). Frees the
+ * allocation before returning; the caller owns mm init/fini and any pins.
+ */
+static void check_contig_range_alloc(struct kunit *test, struct gpu_buddy *mm,
+ u64 range_start, u64 range_end,
+ u64 size, u64 min, const char *shape)
+{
+ struct gpu_buddy_block *block, *prev;
+ LIST_HEAD(allocated);
+ u64 total;
+
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(mm, range_start, range_end,
+ size, min, &allocated,
+ GPU_BUDDY_CONTIGUOUS_ALLOCATION |
+ GPU_BUDDY_RANGE_ALLOCATION),
+ "%s: range-restricted contiguous alloc failed\n",
+ shape);
+
+ total = 0;
+ prev = NULL;
+ list_for_each_entry(block, &allocated, link) {
+ u64 offset = gpu_buddy_block_offset(block);
+ u64 bsize = gpu_buddy_block_size(mm, block);
+
+ KUNIT_EXPECT_TRUE_MSG(test, offset >= range_start &&
+ offset + bsize <= range_end,
+ "%s: block [%llx, %llx) outside range\n",
+ shape, offset, offset + bsize);
+ if (prev)
+ KUNIT_EXPECT_EQ_MSG(test,
+ gpu_buddy_block_offset(prev) +
+ gpu_buddy_block_size(mm, prev),
+ offset,
+ "%s: block at %llx not contiguous\n",
+ shape, offset);
+ prev = block;
+ total += bsize;
+ }
+ KUNIT_EXPECT_EQ_MSG(test, total, size, "%s: wrong total size\n", shape);
+
+ gpu_buddy_free_list(mm, &allocated, 0);
+}
+
+static void gpu_test_buddy_alloc_range_contiguous(struct kunit *test)
+{
+ const unsigned long ps = SZ_4K;
+ struct gpu_buddy mm;
+ LIST_HEAD(pin_lo);
+ LIST_HEAD(pin_hi);
+ LIST_HEAD(barrier);
+
+ /*
+ * Feature: confine a 12K contiguous request (rounded up to 16K) to
+ * [0, 32K), which splits into two naturally aligned 16K slots. Pin the
+ * first page of each ([0, 4K) and [16K, 20K)) so neither slot can
+ * satisfy the rounded-up 16K alloc, yet a contiguous but 16K-unaligned
+ * 12K hole remains at offset 4K. The fallback must stitch that hole
+ * instead of returning -ENOSPC.
+ */
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, 16 * ps, ps),
+ "buddy_init failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 0, ps, ps, ps,
+ &pin_lo, 0),
+ "failed to pin low page\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 4 * ps, 5 * ps, ps,
+ ps, &pin_hi, 0),
+ "failed to pin high page\n");
+ check_contig_range_alloc(test, &mm, 0, 8 * ps, 3 * ps, ps, "feature");
+ gpu_buddy_free_list(&mm, &pin_lo, 0);
+ gpu_buddy_free_list(&mm, &pin_hi, 0);
+ gpu_buddy_fini(&mm);
+
+ /*
+ * Higher-order: an empty arena coalesces all free
+ * space into one high-order block, so the anchored search finds no
+ * block at or below the derived order inside [4K, 20K). The allocator
+ * must still carve the 12K span out of the larger block.
+ */
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, 8 * ps, ps),
+ "buddy_init failed\n");
+ check_contig_range_alloc(test, &mm, ps, 5 * ps, 3 * ps, ps,
+ "higher_order");
+ gpu_buddy_fini(&mm);
+
+ /*
+ * Beyond range_end: pin [8K, 16K) and leave
+ * [16K, 32K) free, so the only fit inside [12K, 28K) is [16K, 28K) with
+ * a free page spilling past range_end. The tail coalesces above the
+ * derived order, so the request must still land on [16K, 28K).
+ */
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, 8 * ps, ps),
+ "buddy_init failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 2 * ps, 4 * ps,
+ 2 * ps, ps, &barrier, 0),
+ "failed to pin barrier\n");
+ check_contig_range_alloc(test, &mm, 3 * ps, 7 * ps, 3 * ps, ps,
+ "beyond_end");
+ gpu_buddy_free_list(&mm, &barrier, 0);
+ gpu_buddy_fini(&mm);
+
+ /*
+ * Left shift: pin [28K, 32K) and leave [0, 28K) free. For a 12K request
+ * in [4K, 20K) the anchored search fills rightward to [16K, 28K), a full
+ * fill ending past range_end. The overshoot must be clamped so the
+ * leftward shift finds the valid placement [8K, 20K).
+ */
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, 8 * ps, ps),
+ "buddy_init failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 7 * ps, 8 * ps,
+ ps, ps, &barrier, 0),
+ "failed to pin barrier\n");
+ check_contig_range_alloc(test, &mm, ps, 5 * ps, 3 * ps, ps,
+ "left_shift");
+ gpu_buddy_free_list(&mm, &barrier, 0);
+ gpu_buddy_fini(&mm);
+
+ /*
+ * Partial fill past range_end: pin [24K, 32K) and leave [0, 24K) free.
+ * For a 12K request in [4K, 20K) the anchored search at 16K fills only
+ * 8K before hitting the pin, still crossing range_end. A placement
+ * inside the window must be found regardless.
+ */
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, 8 * ps, ps),
+ "buddy_init failed\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 6 * ps, 8 * ps,
+ 2 * ps, ps, &barrier, 0),
+ "failed to pin barrier\n");
+ check_contig_range_alloc(test, &mm, ps, 5 * ps, 3 * ps, ps,
+ "partial_fill");
+ gpu_buddy_free_list(&mm, &barrier, 0);
+ gpu_buddy_fini(&mm);
+}
+
static void gpu_test_buddy_alloc_pathological(struct kunit *test)
{
u64 mm_size, size, start = 0;
@@ -1534,10 +1675,13 @@ static void gpu_test_buddy_alloc_exceeds_max_order(struct kunit *test)
GPU_BUDDY_RANGE_ALLOCATION);
KUNIT_EXPECT_EQ(test, err, -EINVAL);
- /* CONTIGUOUS + RANGE should return -EINVAL (no try_harder for RANGE) */
- err = gpu_buddy_alloc_blocks(&mm, 0, mm_size, size, SZ_4K, &blocks,
- GPU_BUDDY_CONTIGUOUS_ALLOCATION | GPU_BUDDY_RANGE_ALLOCATION);
- KUNIT_EXPECT_EQ(test, err, -EINVAL);
+ /* CONTIGUOUS + RANGE should succeed via the range-aware try_harder */
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_alloc_blocks(&mm, 0, mm_size, size,
+ SZ_4K, &blocks,
+ GPU_BUDDY_CONTIGUOUS_ALLOCATION |
+ GPU_BUDDY_RANGE_ALLOCATION),
+ "range contiguous alloc hit an error size=%llu\n", size);
+ gpu_buddy_free_list(&mm, &blocks, 0);
gpu_buddy_fini(&mm);
}
@@ -1586,6 +1730,70 @@ static void gpu_test_buddy_addr_to_block(struct kunit *test)
gpu_buddy_fini(&mm);
}
+static void gpu_test_buddy_alloc_contiguous_descent(struct kunit *test)
+{
+ const unsigned long ps = SZ_4K, mm_size = 16 * ps;
+ struct gpu_buddy_block *block, *prev;
+ LIST_HEAD(allocated);
+ struct gpu_buddy mm;
+ LIST_HEAD(pin_lo);
+ LIST_HEAD(pin_hi);
+ u64 total;
+
+ KUNIT_ASSERT_FALSE_MSG(test, gpu_buddy_init(&mm, mm_size, ps),
+ "buddy_init failed\n");
+
+ /*
+ * Pin [0, 16K) and [48K, 64K). The remaining [16K, 48K) is free as two
+ * adjacent 16K halves under different parents; being un-mergeable
+ * buddies they never coalesce, so free_tree[3] (32K) stays empty. A
+ * contiguous 32K request with min_block_size 4K therefore has no 32K
+ * anchor and must descend to order 2 and stitch [16K, 48K).
+ */
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 0, 4 * ps, 4 * ps,
+ ps, &pin_lo, 0),
+ "failed to pin low block\n");
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 12 * ps, 16 * ps,
+ 4 * ps, ps, &pin_hi, 0),
+ "failed to pin high block\n");
+
+ KUNIT_ASSERT_FALSE_MSG(test,
+ gpu_buddy_alloc_blocks(&mm, 0, mm_size, 8 * ps,
+ ps, &allocated,
+ GPU_BUDDY_CONTIGUOUS_ALLOCATION),
+ "contiguous alloc failed to descend\n");
+
+ /* The result must be exactly 8*ps, contiguous, within [16K, 48K). */
+ total = 0;
+ prev = NULL;
+ list_for_each_entry(block, &allocated, link) {
+ u64 offset = gpu_buddy_block_offset(block);
+ u64 bsize = gpu_buddy_block_size(&mm, block);
+
+ KUNIT_EXPECT_TRUE_MSG(test, offset >= 4 * ps &&
+ offset + bsize <= 12 * ps,
+ "block [%llx, %llx) outside stitched span\n",
+ offset, offset + bsize);
+ if (prev)
+ KUNIT_EXPECT_EQ_MSG(test,
+ gpu_buddy_block_offset(prev) +
+ gpu_buddy_block_size(&mm, prev),
+ offset,
+ "block at %llx not contiguous\n",
+ offset);
+ prev = block;
+ total += bsize;
+ }
+ KUNIT_EXPECT_EQ(test, total, 8 * ps);
+
+ gpu_buddy_free_list(&mm, &allocated, 0);
+ gpu_buddy_free_list(&mm, &pin_lo, 0);
+ gpu_buddy_free_list(&mm, &pin_hi, 0);
+ gpu_buddy_fini(&mm);
+}
+
static int gpu_buddy_suite_init(struct kunit_suite *suite)
{
while (!random_seed)
@@ -1716,6 +1924,8 @@ static struct kunit_case gpu_buddy_tests[] = {
KUNIT_CASE(gpu_test_buddy_alloc_pessimistic),
KUNIT_CASE(gpu_test_buddy_alloc_pathological),
KUNIT_CASE(gpu_test_buddy_alloc_contiguous),
+ KUNIT_CASE(gpu_test_buddy_alloc_range_contiguous),
+ KUNIT_CASE(gpu_test_buddy_alloc_contiguous_descent),
KUNIT_CASE(gpu_test_buddy_alloc_clear),
KUNIT_CASE(gpu_test_buddy_alloc_range),
KUNIT_CASE(gpu_test_buddy_alloc_range_bias),
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v6 2/2] gpu/buddy: add range-restricted contiguous allocation fallback
2026-10-21 12:18 ` [PATCH v6 2/2] gpu/buddy: add range-restricted contiguous allocation fallback Arunpravin Paneer Selvam
@ 2026-10-21 12:46 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-10-21 12:46 UTC (permalink / raw)
To: Arunpravin Paneer Selvam, dri-devel, intel-gfx, intel-xe
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] gpu/buddy: spurious -ENOSPC in __alloc_contig_try_harder() for range allocs
--
commit 14acba1e552d60bba9eee2e544118f0040222647
Author: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
gpu/buddy: add range-restricted contiguous allocation fallback
This commit extends __alloc_contig_try_harder() with a [range_start,
range_end) window and routes range-restricted contiguous allocations through
it when no single naturally aligned power-of-two block is available. It also
adds KUnit tests covering fragmented range-restricted contiguous allocations.
> diff --git a/drivers/gpu/buddy.c b/drivers/gpu/buddy.c
> index 8eb5835d6944c..693176ee3daf9 100644
> --- a/drivers/gpu/buddy.c
> +++ b/drivers/gpu/buddy.c
> @@ -1723,43 +1723,32 @@ static int __gpu_buddy_alloc_range(struct gpu_buddy *mm,
> blocks, total_allocated_on_err);
> }
>
> -static int __alloc_contig_aligned_retry(struct gpu_buddy *mm,
> - u64 unaligned_offset,
> - u64 size,
> - u64 min_block_size,
> - unsigned long flags,
> - struct list_head *blocks)
> -{
> - u64 aligned_offset = round_down(unaligned_offset, min_block_size);
> -
> - return __gpu_buddy_alloc_range(mm, aligned_offset, size, flags,
> - NULL, blocks);
> -}
> -
> static int __alloc_contig_try_harder(struct gpu_buddy *mm,
> + u64 range_start, u64 range_end,
> u64 size,
> u64 min_block_size,
> unsigned long flags,
> struct list_head *blocks)
> {
> - u64 rhs_offset, lhs_offset, filled;
> + u64 rhs_offset, lhs_offset, filled, aligned;
> struct gpu_buddy_block *block;
> + unsigned int min_order, order;
> struct rb_root *root;
> struct rb_node *iter;
> unsigned long pages;
> - unsigned int order;
> u64 modify_size;
> int err;
>
> + min_order = ilog2(min_block_size) - ilog2(mm->chunk_size);
> +
> modify_size = rounddown_pow_of_two(size);
> pages = modify_size >> ilog2(mm->chunk_size);
> order = fls(pages) - 1;
> - if (order == 0)
> - return -ENOSPC;
>
> +retry_order:
> root = &mm->free_tree[order];
> if (RB_EMPTY_ROOT(root))
> - return -ENOSPC;
> + goto next_order;
>
> iter = rb_last(root);
> while (iter) {
> @@ -1767,25 +1756,48 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>
> rhs_offset = gpu_buddy_block_offset(block);
>
> + if (rhs_offset >= range_end) {
> + iter = rb_prev(iter);
> + continue;
> + }
> + if (rhs_offset < range_start)
> + break;
> +
> /* Allocate blocks traversing RHS */
> err = __gpu_buddy_alloc_range(mm, rhs_offset, size,
> flags, &filled, blocks);
> if (err && err != -ENOSPC)
> return err;
> - if (!err && IS_ALIGNED(rhs_offset, min_block_size))
> +
> + if (!err)
> + filled = size;
> +
> + /* Clamp a full or partial fill that crosses range_end. */
> + if (rhs_offset + filled > range_end) {
> + gpu_buddy_free_list_internal(mm, blocks);
> + filled = range_end - rhs_offset;
> + err = -ENOSPC;
> + }
> +
> + if (!err && IS_ALIGNED(rhs_offset, min_block_size) &&
> + rhs_offset >= range_start)
> return 0;
> +
> if (!err) {
> /* Allocate the unaligned RHS offset using round_down */
> gpu_buddy_free_list_internal(mm, blocks);
> - err = __alloc_contig_aligned_retry(mm, rhs_offset,
> - size,
> - min_block_size,
> - flags, blocks);
> - if (!err)
> - return 0;
> - if (err != -ENOSPC) {
> - gpu_buddy_free_list_internal(mm, blocks);
> - return err;
> +
> + aligned = round_down(rhs_offset, min_block_size);
> + if (aligned >= range_start &&
> + aligned + size <= range_end) {
> + err = __gpu_buddy_alloc_range(mm, aligned, size,
> + flags, NULL, blocks);
> + if (!err)
> + return 0;
> + if (err != -ENOSPC) {
> + gpu_buddy_free_list_internal(mm, blocks);
> + return err;
> + }
> }
> goto next;
> }
> @@ -1797,21 +1809,73 @@ static int __alloc_contig_try_harder(struct gpu_buddy *mm,
>
> /* Allocate the unaligned LHS offset using round_down */
> gpu_buddy_free_list_internal(mm, blocks);
> - err = __alloc_contig_aligned_retry(mm, lhs_offset,
> - size,
> - min_block_size,
> - flags, blocks);
> - if (!err)
> - return 0;
> - if (err != -ENOSPC) {
> - gpu_buddy_free_list_internal(mm, blocks);
> - return err;
> +
> + aligned = round_down(lhs_offset, min_block_size);
> + if (aligned >= range_start && aligned + size <= range_end) {
> + err = __gpu_buddy_alloc_range(mm, aligned, size,
> + flags, NULL, blocks);
> + if (!err)
> + return 0;
> + if (err != -ENOSPC) {
> + gpu_buddy_free_list_internal(mm, blocks);
> + return err;
> + }
> }
> next:
> gpu_buddy_free_list_internal(mm, blocks);
> iter = rb_prev(iter);
> }
>
> +next_order:
> + if (order > min_order) {
> + order--;
> + goto retry_order;
> + }
> +
> + /* A non-range request would already have used a larger free block. */
> + if (!(flags & GPU_BUDDY_RANGE_ALLOCATION))
> + return -ENOSPC;
> +
> + /*
> + * Last resort: the span may sit inside a larger coalesced free block
> + * that the anchored search above never visits. Walk the higher-order
> + * free trees (bounded by the number of free blocks, not by the window
> + * size) and carve the highest aligned span that fits inside each block
> + * overlapping the window.
> + */
> + for (order = fls(modify_size >> ilog2(mm->chunk_size));
> + order <= mm->max_order; order++) {
> + for (iter = rb_last(&mm->free_tree[order]); iter;
> + iter = rb_prev(iter)) {
> + u64 low, high;
> +
> + block = rbtree_get_free_block(iter);
> + low = max(gpu_buddy_block_offset(block), range_start);
> + high = min(gpu_buddy_block_offset(block) +
> + gpu_buddy_block_size(mm, block), range_end);
> +
> + if (gpu_buddy_block_offset(block) >= range_end)
> + continue;
> + if (gpu_buddy_block_offset(block) +
> + gpu_buddy_block_size(mm, block) <= range_start)
> + break;
> + if (high < low + size)
> + continue;
> +
> + aligned = round_down(high - size, min_block_size);
> + if (aligned < low)
> + continue;
> +
> + err = __gpu_buddy_alloc_range(mm, aligned, size, flags,
> + NULL, blocks);
> + if (!err)
> + return 0;
> + gpu_buddy_free_list_internal(mm, blocks);
> + if (err != -ENOSPC)
> + return err;
> + }
> + }
> +
> return -ENOSPC;
> }
[Severity: Medium]
Can __alloc_contig_try_harder() in drivers/gpu/buddy.c still return a
spurious -ENOSPC when gpu_buddy_alloc_blocks() falls back to it for a
range-restricted or min_block_size-aligned contiguous allocation, if the
valid contiguous span straddles a higher-order block boundary, a block
starting below range_start, or a trailing block smaller than min_block_size?
In the first pass, the anchored search starts at
order = fls(pages) - 1, breaks out of the free_tree walk as soon as
rhs_offset < range_start:
if (rhs_offset < range_start)
break;
and stops descending at min_order:
if (order > min_order) {
order--;
goto retry_order;
}
In the second pass, the higher-order free_tree walk clips each individual
free block to [range_start, range_end) via [low, high) and requires the
entire request to fit inside that single block's clipped window:
if (high < low + size)
continue;
aligned = round_down(high - size, min_block_size);
if (aligned < low)
continue;
Because the second pass only tests placements contained within a single
higher-order block and never stitches across adjacent free blocks, it looks
like valid contiguous placements are missed in three cases:
1. A span straddling two higher-order blocks. For example, with chunk_size
of 4K, if [16K, 32K) is free at order 2 and [32K, 64K) is free at order 3, a
request for size = 12K, min_block_size = 4K in [24K, 40K) finds free_tree[1]
and free_tree[0] empty in the first pass. The second pass clips [16K, 32K)
to [24K, 32K) (8K < 12K) and [32K, 64K) to [32K, 40K) (8K < 12K), skipping
both and returning -ENOSPC even though the entire [24K, 40K) window is free.
2. A span straddling a block that starts below range_start and an adjacent
higher-order block. With the same free blocks [16K, 32K) at order 2 and
[32K, 64K) at order 3, a request for size = 20K, min_block_size = 4K in
[20K, 44K) starts at order 2, sees rhs_offset = 16K < range_start (20K), and
breaks without probing from range_start. The second pass clips [32K, 64K) to
[32K, 44K) (12K < 20K) and skips it, returning -ENOSPC when [20K, 44K) is
completely free.
3. A span straddling a higher-order block and a trailing free block with
order < min_order. Since min_block_size constrains the start alignment of
the contiguous span rather than the size of trailing blocks stitched into
it, if [0, 16K) is free at order 2 and [16K, 20K) is free at order 0, a
request for size = 12K, min_block_size = 8K (min_order = 1) in [8K, 24K)
stops descending at min_order = 1 without visiting [16K, 20K) at order 0,
and the second pass skips [0, 16K) because [8K, 16K) is only 8K (< 12K),
missing the valid 8K-aligned placement at [8K, 20K).
Should the anchored search instead start from mm->max_order down to order 0,
and clamp blocks that start below range_start (with rhs_offset + block_size >
range_start) to start probing at range_start?
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261021121813.1820345-1-arunpravin.paneerselvam@amd.com?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation
2026-10-21 12:18 [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Arunpravin Paneer Selvam
2026-10-21 12:18 ` [PATCH v6 2/2] gpu/buddy: add range-restricted contiguous allocation fallback Arunpravin Paneer Selvam
@ 2026-10-21 12:49 ` Matthew Auld
2026-10-21 13:45 ` Arunpravin Paneer Selvam
1 sibling, 1 reply; 5+ messages in thread
From: Matthew Auld @ 2026-10-21 12:49 UTC (permalink / raw)
To: Arunpravin Paneer Selvam, dri-devel, intel-gfx, intel-xe, amd-gfx
Cc: christian.koenig, alexander.deucher, Anand.Raghavendra
On 21/10/2026 13:18, Arunpravin Paneer Selvam wrote:
> From: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
>
> Range-restricted allocations (the visible-VRAM aperture) skip
> dirty_steer_window() and reach __alloc_range_bias() with no clear/dirty
> eligibility guard, so a dirty allocation freely splits clear extents (and
> vice versa), interleaving the two classes until no large same-class run
> survives and a 4K scanout pin fails with -ENOSPC. Use a two-pass range
> search instead: block_incompatible() rejects opposite-class blocks in the
> strict pass and relaxes only when the strict pass returns -ENOSPC, so a
> request still succeeds when only opposite-class space remains. A KUnit
> regression test covers the interleaving pattern and fails without the
> guard.
>
> v2: the strict pass can split a block and then reject both children, so
> a split no longer implies a compatible block exists. Sashiko reported
> the resulting leaked splits; fix as suggested by Matthew:
> - Push the parent back on the DFS stack after each split and merge it
> back with unsplit_block() if both children are still free.
> - Make block_incompatible() judge free blocks only (split nodes keep a
> stale clear bit) and take the requested order, so mixed blocks that
> can hold a compatible block are descended instead of skipped.
> - Extend the KUnit test with a coalesced mixed block and a check that
> a rejected split is merged back.
>
> Fixes: 1ad5e807f716 ("gpu/buddy: replace dual-tree/force_merge with decoupled dirty tracker")
> Assisted-by: LLM
> Cc: Matthew Auld <matthew.auld@intel.com>
> Cc: Christian König <christian.koenig@amd.com>
> Signed-off-by: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Are you just going to push this one by itself?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation
2026-10-21 12:49 ` [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Matthew Auld
@ 2026-10-21 13:45 ` Arunpravin Paneer Selvam
0 siblings, 0 replies; 5+ messages in thread
From: Arunpravin Paneer Selvam @ 2026-10-21 13:45 UTC (permalink / raw)
To: Matthew Auld, dri-devel, intel-gfx, intel-xe, amd-gfx
Cc: christian.koenig, alexander.deucher, Anand.Raghavendra
On 10/21/2026 6:19 PM, Matthew Auld wrote:
> On 21/10/2026 13:18, Arunpravin Paneer Selvam wrote:
>> From: Arunpravin Paneer Selvam <Arunpravin.PaneerSelvam@amd.com>
>>
>> Range-restricted allocations (the visible-VRAM aperture) skip
>> dirty_steer_window() and reach __alloc_range_bias() with no clear/dirty
>> eligibility guard, so a dirty allocation freely splits clear extents
>> (and
>> vice versa), interleaving the two classes until no large same-class run
>> survives and a 4K scanout pin fails with -ENOSPC. Use a two-pass range
>> search instead: block_incompatible() rejects opposite-class blocks in
>> the
>> strict pass and relaxes only when the strict pass returns -ENOSPC, so a
>> request still succeeds when only opposite-class space remains. A KUnit
>> regression test covers the interleaving pattern and fails without the
>> guard.
>>
>> v2: the strict pass can split a block and then reject both children, so
>> a split no longer implies a compatible block exists. Sashiko
>> reported
>> the resulting leaked splits; fix as suggested by Matthew:
>> - Push the parent back on the DFS stack after each split and merge it
>> back with unsplit_block() if both children are still free.
>> - Make block_incompatible() judge free blocks only (split nodes keep a
>> stale clear bit) and take the requested order, so mixed blocks that
>> can hold a compatible block are descended instead of skipped.
>> - Extend the KUnit test with a coalesced mixed block and a check that
>> a rejected split is merged back.
>>
>> Fixes: 1ad5e807f716 ("gpu/buddy: replace dual-tree/force_merge with
>> decoupled dirty tracker")
>> Assisted-by: LLM
>> Cc: Matthew Auld <matthew.auld@intel.com>
>> Cc: Christian König <christian.koenig@amd.com>
>> Signed-off-by: Arunpravin Paneer Selvam
>> <Arunpravin.PaneerSelvam@amd.com>
>
> Reviewed-by: Matthew Auld <matthew.auld@intel.com>
>
> Are you just going to push this one by itself?
Yes, I am planning to push this one separately. Patch 2 can follow later
after review.
Thanks,
Arun.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-10-21 13:46 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-21 12:18 [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Arunpravin Paneer Selvam
2026-10-21 12:18 ` [PATCH v6 2/2] gpu/buddy: add range-restricted contiguous allocation fallback Arunpravin Paneer Selvam
2026-10-21 12:46 ` sashiko-bot
2026-10-21 12:49 ` [PATCH v6 1/2] gpu/buddy: restore two-pass clear/dirty guard for range-biased allocation Matthew Auld
2026-10-21 13:45 ` Arunpravin Paneer Selvam
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox