Hi,
On 27/05/2026 12:29, Arunpravin Paneer Selvam wrote:
The current buddy allocator maintains separate clear_tree[] and
dirty_tree[] rbtrees per order, preventing coalescing between cleared
and dirty buddies. Under mixed workloads, this creates a merge barrier:
adjacent buddies frequently end up split across trees, forcing reliance
on __force_merge() during allocation.
__force_merge() performs an O(N x max_order) scan under the VRAM manager
lock, leading to allocation stalls and failures for large contiguous
requests even when sufficient total free memory is available.
So is this contig with non power-of-two sizes?
Do we know if we could force_merge everything in one go or somehow be
more aggressive and do more than needed now, at the first sign of
contention here, instead of doing it piecemeal? Downside would be losing
more of the clear tracking, when this happens, but more re-merging.
Could we have another per-order list, of all blocks that we failed to
merge, when we did the free step? When doing the force merge step, we
maybe don't need to search blindly and can focus instead on the stuff
tracked in those lists? Maybe it doesn't need to be a list, but could be
another rb-tree?
We know the size of the total allocation, if we trigger force_merge,
could we try to merge enough in one go for the entire allocation,
instead of restarting the entire thing on the next iteration? Would that
help at all?
But I guess these are more for the stalling side, and won't help much
with the contig angle?
For the extent idea, is there any merit in maybe doing this for all
contig blobs, and not just cleared stuff? Or is the workload you are
seeing only benefit users that want cleared stuff? Wondering if this
would benefit all users that want contig? Like if we hypothetically kept
clear and dirty separate, like we do now, but with an improved
force_merge, and then have extent tracking for all contig blobs and
replace the try_harder stuff? When you do a contig alloc, the individual
clear/dirty is still all there within the range, so you can skip
re-clearing in some cases. I guess downside is overall more fuzzy contig
+ clear/free path, but I guess you would never get allocation failures,
when there is sufficient contig space?
Solution
Replace the dual-tree design with:
- A single free_tree[order] rbtree for dirty and mixed free blocks
(fully cleared free blocks float outside this tree)
- A lightweight out-of-band clear tracker (gpu_clear_tracker)
Fully cleared free blocks are tracked outside the buddy trees using an
augmented interval rbtree, enabling O(log E) lookup of the largest
cleared extents.
Buddy coalescing is now unconditional in __gpu_buddy_free(), regardless
of clear/dirty state. This removes the merge barrier and eliminates the
need for __force_merge().
Benefits
- Correct high-order allocations after mixed clear/dirty workloads
- Elimination of O(N x max_order) merge cost from the allocation path
- O(log E) cleared-extent lookup replacing O(N) scans
- Predictable allocation latency under fragmentation
- Reduced complexity with a single tree per order
Since there is no separate tracking for dirty stuff, is the non-cleared
alloc path a bit more "fuzzy" now, with it potentially stealing cleared
memory, or is it the same behaviour still?
For drivers that don't use free tracking, is there some benefit? Are
there any downsides there? I assume that clear tracker is always empty.