Skip to contentSkip to conversation

drm_buddy regresion fix and minor improvements

PATCH In review Pierre-Eric Pelloux-Prayer 2 participants 5 messages Jul 2, 2025 – Jul 3, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover drm_buddy regresion fix and minor improvements

Hi,

The first patch fixes a performance issue caused by a change in drm_buddy ("drm/buddy: Implement tracking clear page feature"). It may be related to https://gitlab.freedesktop.org/drm/amd/-/issues/4260.

The other 2 patches are improvements based on my limited understanding of the code and that seem to work as expected.

Pierre-Eric Pelloux-Prayer (3):
drm/buddy: add a flag to disable trimming of non cleared blocks drm/buddy: use DRM_BUDDY_CLEAR_ALLOCATION as a hint, not a hard req drm/buddy: dont go over the higher orders multiple times

 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 10 ++-
 drivers/gpu/drm/drm_buddy.c                  | 61 ++++++++++------
 drivers/gpu/drm/tests/drm_buddy_test.c       | 75 +++-----------------
 include/drm/drm_buddy.h                      |  3 +-
 4 files changed, 57 insertions(+), 92 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 drm/buddy: add a flag to disable trimming of non cleared blocks

A vkcts test case is triggering a case where the drm buddy allocator wastes lots of memory and performs badly:

dEQP-VK.memory.allocation.basic.size_8KiB.reverse.count_4000

For each memory pool type, the test will allocate 4000 8kB objects, and then will release them. The alignment request is 256kB.

For each object, the allocator will select a 256kB block (to match the alignment), and then trim it to 8kB, adding lots of free entries to the free_lists of order 5 to 1.
On deallocation, none of these objects will be merged with their buddy because their "clear status" is different: only the block that was handed over to the driver might come back cleared. Also since the test don't allocate much memory, the allocator don't need to force the merge process so it will repeat the same logic for each run.

As a result, after the first run (which takes about 6sec), the freelists look like this:

chunk_size: 4KiB, total: 16368MiB, free: 15354MiB, clear_free: 397MiB [...]
order- 5 free: 1914 MiB, blocks: 15315

   order- 4 free:      957 MiB, blocks: 15325
   order- 3 free:      480 MiB, blocks: 15360
   order- 2 free:      239 MiB, blocks: 15347
   order- 1 free:      238 MiB, blocks: 30489

After the second run (19 sec):

chunk_size: 4KiB, total: 16368MiB, free: 15374MiB, clear_free: 537MiB [...]
order- 5 free: 3326 MiB, blocks: 26615
order- 4 free: 1663 MiB, blocks: 26619

   order- 3 free:      833 MiB, blocks: 26659
   order- 2 free:      416 MiB, blocks: 26643
   order- 1 free:      414 MiB, blocks: 53071

list_insert_sorted is part of the problem here since it iterates over the free_list to figure out where to insert the new blocks.

To fix this while keeping the clear tracking information, a new bit is exposed to drivers, allowing them to disable trimming for blocks that aren't "clear". This bit is used by amdgpu because it always returns cleared memory to drm_buddy.

With this bit set, the "merge buddies on deallocation logic" can work again, and the free_list are not growing indefinitely anymore.

So after a run we get:

chunk_size: 4KiB, total: 16368MiB, free: 15306MiB, clear_free: 1734MiB [...]

   order- 5 free:        2 MiB, blocks: 17
   order- 4 free:        2 MiB, blocks: 35
   order- 3 free:        1 MiB, blocks: 41

order- 2 free: 656 KiB, blocks: 41
order- 1 free: 256 KiB, blocks: 32

The runtime is better (2 sec) and stable across multiple runs, and we also see that the reported "clear_free" amount is larger than without the patch.

Fixes 96950929eb23 ("drm/buddy: Implement tracking clear page feature")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 ++++++++
 drivers/gpu/drm/drm_buddy.c                  | 1 +
 include/drm/drm_buddy.h                      | 1 +
 3 files changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +8 -0
index abdc52b0895a..dbbaa15a973e 100644
@@ -499,6 +499,14 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 
 	INIT_LIST_HEAD(&vres->blocks);
 
+	/* Trimming create smaller blocks that may never be given to the driver.
+	 * Such blocks won't be cleared until being seen by the driver, which might
+	 * never occur (for instance UMD might request large alignment) => in such
+	 * case, upon release of the block, the drm_buddy allocator won't merge them
+	 * back, because their clear status is different.
+	 */
+	vres->flags = DRM_BUDDY_TRIM_IF_CLEAR;
+
 	if (place->flags & TTM_PL_FLAG_TOPDOWN)
 		vres->flags |= DRM_BUDDY_TOPDOWN_ALLOCATION;
 
drivers/gpu/drm/drm_buddy.c +1 -0
index a1e652b7631d..555c72abce4c 100644
@@ -1092,6 +1092,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 
 	/* Trim the allocated block to the required size */
 	if (!(flags & DRM_BUDDY_TRIM_DISABLE) &&
+	    (!(flags & DRM_BUDDY_TRIM_IF_CLEAR) || drm_buddy_block_is_clear(block)) &&
 	    original_size != size) {
 		struct list_head *trim_list;
 		LIST_HEAD(temp);
include/drm/drm_buddy.h +1 -0
index 9689a7c5dd36..c338d03028c3 100644
@@ -28,6 +28,7 @@
 #define DRM_BUDDY_CLEAR_ALLOCATION		BIT(3)
 #define DRM_BUDDY_CLEARED			BIT(4)
 #define DRM_BUDDY_TRIM_DISABLE			BIT(5)
+#define DRM_BUDDY_TRIM_IF_CLEAR			BIT(6)
 
 struct drm_buddy_block {
 #define DRM_BUDDY_HEADER_OFFSET GENMASK_ULL(63, 12)
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH v1 1/3] drm/buddy: add a flag to disable trimming of non cleared blocks

Quoted 39 lines · Pierre-Eric Pelloux-Prayer

A vkcts test case is triggering a case where the drm buddy allocator wastes lots of memory and performs badly:

dEQP-VK.memory.allocation.basic.size_8KiB.reverse.count_4000

For each memory pool type, the test will allocate 4000 8kB objects, and then will release them. The alignment request is 256kB.

For each object, the allocator will select a 256kB block (to match the alignment), and then trim it to 8kB, adding lots of free entries to the free_lists of order 5 to 1.
On deallocation, none of these objects will be merged with their buddy because their "clear status" is different: only the block that was handed over to the driver might come back cleared. Also since the test don't allocate much memory, the allocator don't need to force the merge process so it will repeat the same logic for each run.

As a result, after the first run (which takes about 6sec), the freelists look like this:

   chunk_size: 4KiB, total: 16368MiB, free: 15354MiB, clear_free: 397MiB
   [...]
   order- 5 free:     1914 MiB, blocks: 15315
   order- 4 free:      957 MiB, blocks: 15325
   order- 3 free:      480 MiB, blocks: 15360
   order- 2 free:      239 MiB, blocks: 15347
   order- 1 free:      238 MiB, blocks: 30489

After the second run (19 sec):

   chunk_size: 4KiB, total: 16368MiB, free: 15374MiB, clear_free: 537MiB
   [...]
   order- 5 free:     3326 MiB, blocks: 26615
   order- 4 free:     1663 MiB, blocks: 26619
   order- 3 free:      833 MiB, blocks: 26659
   order- 2 free:      416 MiB, blocks: 26643
   order- 1 free:      414 MiB, blocks: 53071
Quoted 7 lines

list_insert_sorted is part of the problem here since it iterates over the free_list to figure out where to insert the new blocks.

To fix this while keeping the clear tracking information, a new bit is exposed to drivers, allowing them to disable trimming for blocks that aren't "clear". This bit is used by amdgpu because it always returns cleared memory to drm_buddy.

That's an extremely good catch, but I don't think this is the right solution.

If I understood it correctly we now give back 256k instead of 8k to the caller and that is a really big no-go.

Regards,
Christian.

Quoted 69 lines

With this bit set, the "merge buddies on deallocation logic" can work again, and the free_list are not growing indefinitely anymore.

So after a run we get:

   chunk_size: 4KiB, total: 16368MiB, free: 15306MiB, clear_free: 1734MiB
   [...]
   order- 5 free:        2 MiB, blocks: 17
   order- 4 free:        2 MiB, blocks: 35
   order- 3 free:        1 MiB, blocks: 41
   order- 2 free:      656 KiB, blocks: 41
   order- 1 free:      256 KiB, blocks: 32

The runtime is better (2 sec) and stable across multiple runs, and we also see that the reported "clear_free" amount is larger than without the patch.

Fixes 96950929eb23 ("drm/buddy: Implement tracking clear page feature")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 8 ++++++++
 drivers/gpu/drm/drm_buddy.c                  | 1 +
 include/drm/drm_buddy.h                      | 1 +
 3 files changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +8 -0
index abdc52b0895a..dbbaa15a973e 100644
@@ -499,6 +499,14 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 
 	INIT_LIST_HEAD(&vres->blocks);
 
+	/* Trimming create smaller blocks that may never be given to the driver.
+	 * Such blocks won't be cleared until being seen by the driver, which might
+	 * never occur (for instance UMD might request large alignment) => in such
+	 * case, upon release of the block, the drm_buddy allocator won't merge them
+	 * back, because their clear status is different.
+	 */
+	vres->flags = DRM_BUDDY_TRIM_IF_CLEAR;
+
 	if (place->flags & TTM_PL_FLAG_TOPDOWN)
 		vres->flags |= DRM_BUDDY_TOPDOWN_ALLOCATION;
 
drivers/gpu/drm/drm_buddy.c +1 -0
index a1e652b7631d..555c72abce4c 100644
@@ -1092,6 +1092,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 
 	/* Trim the allocated block to the required size */
 	if (!(flags & DRM_BUDDY_TRIM_DISABLE) &&
+	    (!(flags & DRM_BUDDY_TRIM_IF_CLEAR) || drm_buddy_block_is_clear(block)) &&
 	    original_size != size) {
 		struct list_head *trim_list;
 		LIST_HEAD(temp);
include/drm/drm_buddy.h +1 -0
index 9689a7c5dd36..c338d03028c3 100644
@@ -28,6 +28,7 @@
 #define DRM_BUDDY_CLEAR_ALLOCATION		BIT(3)
 #define DRM_BUDDY_CLEARED			BIT(4)
 #define DRM_BUDDY_TRIM_DISABLE			BIT(5)
+#define DRM_BUDDY_TRIM_IF_CLEAR			BIT(6)
 
 struct drm_buddy_block {
 #define DRM_BUDDY_HEADER_OFFSET GENMASK_ULL(63, 12)
#3 lore raw

PATCH 2/3 drm/buddy: use DRM_BUDDY_CLEAR_ALLOCATION as a hint, not a hard req

In reply to Pierre-Eric Pelloux-Prayer

The rationale for this change is that it's preferable to return non-cleared memory instead of splitting up higher-order blocks as this leads to more fragmented memory.

The driver will be able to clear the memory by itself if required and the clear tracking will avoid the need for useless clearing jobs.

This commit renames DRM_BUDDY_CLEAR_ALLOCATION as DRM_BUDDY_PREFER_CLEAR_ALLOCATION to make the intent clearer, and delete the tests that expected that passing this flag would return cleared memory.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c |  2 +-
 drivers/gpu/drm/drm_buddy.c                  | 43 ++++++-----
 drivers/gpu/drm/tests/drm_buddy_test.c       | 75 +++-----------------
 include/drm/drm_buddy.h                      |  2 +-
 4 files changed, 35 insertions(+), 87 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +1 -1
index dbbaa15a973e..24dd094eac84 100644
@@ -514,7 +514,7 @@ static int amdgpu_vram_mgr_new(struct ttm_resource_manager *man,
 		vres->flags |= DRM_BUDDY_CONTIGUOUS_ALLOCATION;
 
 	if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED)
-		vres->flags |= DRM_BUDDY_CLEAR_ALLOCATION;
+		vres->flags |= DRM_BUDDY_PREFER_CLEAR_ALLOCATION;
 
 	if (fpfn || lpfn != mgr->mm.size)
 		/* Allocate blocks in desired range */
drivers/gpu/drm/drm_buddy.c +25 -18

93 lines not on this page · raw message

drivers/gpu/drm/tests/drm_buddy_test.c +8 -67

103 lines not on this page · raw message

include/drm/drm_buddy.h +1 -1
index c338d03028c3..ed06be63a770 100644
@@ -25,7 +25,7 @@
 #define DRM_BUDDY_RANGE_ALLOCATION		BIT(0)
 #define DRM_BUDDY_TOPDOWN_ALLOCATION		BIT(1)
 #define DRM_BUDDY_CONTIGUOUS_ALLOCATION		BIT(2)
-#define DRM_BUDDY_CLEAR_ALLOCATION		BIT(3)
+#define DRM_BUDDY_PREFER_CLEAR_ALLOCATION	BIT(3)
 #define DRM_BUDDY_CLEARED			BIT(4)
 #define DRM_BUDDY_TRIM_DISABLE			BIT(5)
 #define DRM_BUDDY_TRIM_IF_CLEAR			BIT(6)
Signature · 1 line
2.43.0
#4 lore raw

PATCH 3/3 drm/buddy: dont go over the higher orders multiple times

In reply to Pierre-Eric Pelloux-Prayer

AFAICT the rationale for the loop is to:
1) try to allocate from the preferred order
2) if it fails, try higher orders (order + 1 -> max order)
3) if it fails, try smaller orders (order - 1 -> min order)

Steps 1 and 2 are covered by the loop going through [order, max_order]. Currently step 3 tries again [order, max_order] but with decreasing values of order.

This is wasteful, so change it to evaluate only order.

---
 drivers/gpu/drm/drm_buddy.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_buddy.c +12 -5
index fd31322b3d41..9d3723f2cff9 100644
@@ -590,13 +590,14 @@ __drm_buddy_alloc_range_bias(struct drm_buddy *mm,
 
 static struct drm_buddy_block *
 get_maxblock(struct drm_buddy *mm, unsigned int order,
+	     unsigned int max_order,
 	     unsigned long flags)
 {
 	struct drm_buddy_block *max_block = NULL, *block = NULL;
 	bool wants_clear;
 	unsigned int i;
 
-	for (i = order; i <= mm->max_order; ++i) {
+	for (i = order; i <= max_order; ++i) {
 		struct drm_buddy_block *tmp_block;
 
 		wants_clear = flags & DRM_BUDDY_PREFER_CLEAR_ALLOCATION;
@@ -635,6 +636,7 @@ get_maxblock(struct drm_buddy *mm, unsigned int order,
 static struct drm_buddy_block *
 alloc_from_freelist(struct drm_buddy *mm,
 		    unsigned int order,
+		    unsigned int max_order,
 		    unsigned long flags)
 {
 	struct drm_buddy_block *block = NULL;
@@ -643,12 +645,12 @@ alloc_from_freelist(struct drm_buddy *mm,
 	int err;
 
 	if (flags & DRM_BUDDY_TOPDOWN_ALLOCATION) {
-		block = get_maxblock(mm, order, flags);
+		block = get_maxblock(mm, order, max_order, flags);
 		if (block)
 			/* Store the obtained block order */
 			tmp = drm_buddy_block_order(block);
 	} else {
-		for (tmp = order; tmp <= mm->max_order; ++tmp) {
+		for (tmp = order; tmp <= max_order; ++tmp) {
 			struct drm_buddy_block *tmp_block;
 			wants_clear = flags & DRM_BUDDY_PREFER_CLEAR_ALLOCATION;
 
@@ -956,6 +958,7 @@ static struct drm_buddy_block *
 __drm_buddy_alloc_blocks(struct drm_buddy *mm,
 			 u64 start, u64 end,
 			 unsigned int order,
+			 unsigned int max_order,
 			 unsigned long flags)
 {
 	if (flags & DRM_BUDDY_RANGE_ALLOCATION)
@@ -964,7 +967,7 @@ __drm_buddy_alloc_blocks(struct drm_buddy *mm,
 						     order, flags);
 	else
 		/* Allocate from freelist */
-		return alloc_from_freelist(mm, order, flags);
+		return alloc_from_freelist(mm, order, max_order, flags);
 }
 
 /**
@@ -995,7 +998,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 {
 	struct drm_buddy_block *block = NULL;
 	u64 original_size, original_min_size;
-	unsigned int min_order, order;
+	unsigned int min_order, max_order, order;
 	LIST_HEAD(allocated);
 	unsigned long pages;
 	int err;
@@ -1044,6 +1047,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 
 	do {
 		order = min(order, (unsigned int)fls(pages) - 1);
+		max_order = mm->max_order;
 		BUG_ON(order > mm->max_order);
 		BUG_ON(order < min_order);
 
@@ -1051,6 +1055,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 			block = __drm_buddy_alloc_blocks(mm, start,
 							 end,
 							 order,
+							 max_order,
 							 flags);
 			if (!IS_ERR(block))
 				break;
@@ -1062,6 +1067,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 					block = __drm_buddy_alloc_blocks(mm, start,
 									 end,
 									 min_order,
+									 mm->max_order,
 									 flags);
 					if (!IS_ERR(block)) {
 						order = min_order;
@@ -1082,6 +1088,7 @@ int drm_buddy_alloc_blocks(struct drm_buddy *mm,
 				err = -ENOSPC;
 				goto err_free;
 			}
+			max_order = order;
 		} while (1);
 
 		mark_allocated(block);
Signature · 1 line
2.43.0