Skip to contentSkip to conversation

TTM pool shrinker improvements

PATCH In review Tvrtko Ursulin 4 participants 7 messages Jun 3, 2025 – Jun 4, 2025

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

v2 cover TTM pool shrinker improvements

Just two small patches to make the TTM pool shrinker respect the shrink target configured by the core, and to also signal the core it can cheaply free more than the default 128 pages.

Motivation was that I was running some VK-CTS tests and noticed shrinker callback was getting called quite a lot so thought why not make it a tiny bit more respectful and efficient.

v2:
* Simplify loop logic and improve commit message.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>

Tvrtko Ursulin (2):
drm/ttm: Respect the shrinker core free target
drm/ttm: Increase pool shrinker batch target

 drivers/gpu/drm/ttm/ttm_pool.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.0
#1 lore raw

PATCH v2 1/2 drm/ttm: Respect the shrinker core free target

Currently the TTM shrinker aborts shrinking as soon as it frees pages from any of the page order pools and by doing so it can fail to respect the freeing target which was configured by the shrinker core.

We use the wording "can fail" because the number of freed pages will depend on the presence of pages in the pools and the order of the pools on the LRU list. For example if there are no free pages in the high order pools the shrinker core may require multiple passes over the TTM shrinker before it will free the default target of 128 pages (assuming there are free pages in the low order pools). This inefficiency can be compounded by the pool LRU where multiple further calls into the TTM shrinker are required to end up looking at the pool with pages.

Improve this by never freeing less than the shrinker core has requested.

At the same time we start reporting the number of scanned pages (freed in this case), which prevents the core shrinker from giving up on the TTM shrinker too soon and moving on.

v2:
* Simplify loop logic. (Christian)
* Improve commit message.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +5 -3
index c2ea865be657..c060c90b89c0 100644
@@ -1132,7 +1132,6 @@ void ttm_pool_fini(struct ttm_pool *pool)
 }
 EXPORT_SYMBOL(ttm_pool_fini);
 
-/* As long as pages are available make sure to release at least one */
 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
 					    struct shrink_control *sc)
 {
@@ -1140,9 +1139,12 @@ static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
 
 	do
 		num_freed += ttm_pool_shrink();
-	while (!num_freed && atomic_long_read(&allocated_pages));
+	while (num_freed < sc->nr_to_scan &&
+	       atomic_long_read(&allocated_pages));
 
-	return num_freed;
+	sc->nr_scanned = num_freed;
+
+	return num_freed ?: SHRINK_STOP;
 }
 
 /* Return the number of pages available or SHRINK_EMPTY if we have none */
Signature · 1 line
2.48.0
#2 lore raw

PATCH v2 2/2 drm/ttm: Increase pool shrinker batch target

In reply to Tvrtko Ursulin

The default core shrink target of 128 pages (SHRINK_BATCH) is quite low relative to how cheap TTM pool shrinking is, and how the free pages are distributed in page order pools.

We can make the target a bit more aggressive by making it roughly the average number of pages across all pools, freeing more of the cached pages every time shrinker core invokes our callback.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_pool.c +8 -1
index c060c90b89c0..e671812789ea 100644
@@ -1265,10 +1265,16 @@ int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
 }
 EXPORT_SYMBOL(ttm_pool_debugfs);
 
+/* Free average pool number of pages.  */
+#define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)
+
 /* Test the shrinker functions and dump the result */
 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
 {
-	struct shrink_control sc = { .gfp_mask = GFP_NOFS };
+	struct shrink_control sc = {
+		.gfp_mask = GFP_NOFS,
+		.nr_to_scan = TTM_SHRINKER_BATCH,
+	};
 
 	fs_reclaim_acquire(GFP_KERNEL);
 	seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
@@ -1326,6 +1332,7 @@ int ttm_pool_mgr_init(unsigned long num_pages)
 
 	mm_shrinker->count_objects = ttm_pool_shrinker_count;
 	mm_shrinker->scan_objects = ttm_pool_shrinker_scan;
+	mm_shrinker->batch = TTM_SHRINKER_BATCH;
 	mm_shrinker->seeks = 1;
 
 	shrinker_register(mm_shrinker);
Signature · 1 line
2.48.0
#3 lore raw

Re: [PATCH v2 2/2] drm/ttm: Increase pool shrinker batch target

Quoted 20 lines · Tvrtko Ursulin

The default core shrink target of 128 pages (SHRINK_BATCH) is quite low relative to how cheap TTM pool shrinking is, and how the free pages are distributed in page order pools.

We can make the target a bit more aggressive by making it roughly the average number of pages across all pools, freeing more of the cached pages every time shrinker core invokes our callback.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
index c060c90b89c0..e671812789ea 100644

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m) · line 1269

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_pool.c +2 -0
@@ -1265,10 +1265,16 @@ int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
 }
 EXPORT_SYMBOL(ttm_pool_debugfs);
 
+/* Free average pool number of pages.  */
+#define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)

To be honest this feels random. But I can't come up with a better idea either and it still looks better than the default as far as I can see.

So feel free to add Reviewed-by: Christian König <[email protected]> to the series.

Should I push it to drm-misc-next or do you now have commit rights?

Regards,
Christian.

Quoted 20 lines
drivers/gpu/drm/ttm/ttm_pool.c +6 -1
+
 /* Test the shrinker functions and dump the result */
 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
 {
-	struct shrink_control sc = { .gfp_mask = GFP_NOFS };
+	struct shrink_control sc = {
+		.gfp_mask = GFP_NOFS,
+		.nr_to_scan = TTM_SHRINKER_BATCH,
+	};
 
 	fs_reclaim_acquire(GFP_KERNEL);
 	seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
@@ -1326,6 +1332,7 @@ int ttm_pool_mgr_init(unsigned long num_pages)
 
 	mm_shrinker->count_objects = ttm_pool_shrinker_count;
 	mm_shrinker->scan_objects = ttm_pool_shrinker_scan;
+	mm_shrinker->batch = TTM_SHRINKER_BATCH;
 	mm_shrinker->seeks = 1;
 
 	shrinker_register(mm_shrinker);
#4 lore raw

Re: [PATCH v2 2/2] drm/ttm: Increase pool shrinker batch target

Quoted 24 lines · Christian König
Quoted 23 lines · Tvrtko Ursulin

The default core shrink target of 128 pages (SHRINK_BATCH) is quite low relative to how cheap TTM pool shrinking is, and how the free pages are distributed in page order pools.

We can make the target a bit more aggressive by making it roughly the average number of pages across all pools, freeing more of the cached pages every time shrinker core invokes our callback.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
  drivers/gpu/drm/ttm/ttm_pool.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
index c060c90b89c0..e671812789ea 100644
@@ -1265,10 +1265,16 @@ int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
  }
  EXPORT_SYMBOL(ttm_pool_debugfs);
 
Quoted 6 lines

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m) · line 1269

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_pool.c +2 -0
+/* Free average pool number of pages.  */
+#define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)

To be honest this feels random. But I can't come up with a better idea either and it still looks better than the default as far as I can see.

So feel free to add Reviewed-by: Christian König <[email protected]> to the series.

Thanks!

Quoted 1 line

Should I push it to drm-misc-next or do you now have commit rights?

I should be able to, think I've tested it already once.

Regards,

Tvrtko

Quoted 21 lines
Quoted 20 lines
drivers/gpu/drm/ttm/ttm_pool.c +6 -1
+
  /* Test the shrinker functions and dump the result */
  static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
  {
-	struct shrink_control sc = { .gfp_mask = GFP_NOFS };
+	struct shrink_control sc = {
+		.gfp_mask = GFP_NOFS,
+		.nr_to_scan = TTM_SHRINKER_BATCH,
+	};
 
  	fs_reclaim_acquire(GFP_KERNEL);
  	seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
@@ -1326,6 +1332,7 @@ int ttm_pool_mgr_init(unsigned long num_pages)
 
  	mm_shrinker->count_objects = ttm_pool_shrinker_count;
  	mm_shrinker->scan_objects = ttm_pool_shrinker_scan;
+	mm_shrinker->batch = TTM_SHRINKER_BATCH;
  	mm_shrinker->seeks = 1;
 
  	shrinker_register(mm_shrinker);
#5 lore raw

Re: [PATCH v2 2/2] drm/ttm: Increase pool shrinker batch target

Quoted 12 lines · Tvrtko Ursulin
Quoted 12 lines

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m) · line 1268

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_pool.c +2 -0
index c060c90b89c0..e671812789ea 100644
@@ -1265,10 +1265,16 @@ int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
 }
 EXPORT_SYMBOL(ttm_pool_debugfs);
+/* Free average pool number of pages.  */
+#define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)

To be honest this feels random. But I can't come up with a better idea either and it still looks better than the default as far as I can see.

Quoted 7 lines
Quoted 1 line

So feel free to add Reviewed-by: Christian König <[email protected]> to the series.

Thanks!

Quoted 1 line

Should I push it to drm-misc-next or do you now have commit rights?

I should be able to, think I've tested it already once.

this is defined inside an ifdef so now our kunit tests are failing with:

+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/ttm/tests/.kunitconfig ERROR:root:../drivers/gpu/drm/ttm/ttm_pool.c: In function ‘ttm_pool_mgr_init’:
../drivers/gpu/drm/ttm/ttm_pool.c:1335:30: error: ‘TTM_SHRINKER_BATCH’ undeclared (first use in this function)

 1335 |         mm_shrinker->batch = TTM_SHRINKER_BATCH;
      |                              ^~~~~~~~~~~~~~~~~~

Maybe move it to the top of the file or before the ifdef... ?

Lucas De Marchi

#6 lore raw

Re: [PATCH v2 2/2] drm/ttm: Increase pool shrinker batch target

Quoted 23 lines · Lucas De Marchi
Quoted 12 lines · Tvrtko Ursulin
Quoted 12 lines

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m) · line 1268

Quoted 9 lines
drivers/gpu/drm/ttm/ttm_pool.c +2 -0
index c060c90b89c0..e671812789ea 100644
@@ -1265,10 +1265,16 @@ int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
  }
  EXPORT_SYMBOL(ttm_pool_debugfs);
+/* Free average pool number of pages.  */
+#define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS)

To be honest this feels random. But I can't come up with a better idea either and it still looks better than the default as far as I can see.

Quoted 7 lines
Quoted 1 line

So feel free to add Reviewed-by: Christian König <[email protected]> to the series.

Thanks!

Quoted 1 line

Should I push it to drm-misc-next or do you now have commit rights?

I should be able to, think I've tested it already once.

this is defined inside an ifdef so now our kunit tests are failing with:

Quoted 7 lines

+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/ttm/tests/.kunitconfig ERROR:root:../drivers/gpu/drm/ttm/ttm_pool.c: In function ‘ttm_pool_mgr_init’:
../drivers/gpu/drm/ttm/ttm_pool.c:1335:30: error: ‘TTM_SHRINKER_BATCH’ undeclared (first use in this function) 1335 |         mm_shrinker->batch = TTM_SHRINKER_BATCH;
      |                              ^~~~~~~~~~~~~~~~~~

Maybe move it to the top of the file or before the ifdef... ?

Oh, good point. I totally missed that this defines is in between the debugfs functions.

Yeah probably better to move the define to the top of the file.

Regards,
Christian.

Quoted 2 lines

Lucas De Marchi