Skip to contentSkip to conversation

TTM pool shrinker improvements

PATCH Superseded Tvrtko Ursulin 2 participants 4 messages Jun 2, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

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 | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.0
#1 lore raw

PATCH 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.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +17 -8
index c2ea865be657..a3247a82cadd 100644
@@ -1132,17 +1132,24 @@ 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)
 {
-	unsigned long num_freed = 0;
+	unsigned long to_scan, freed = 0;
 
-	do
-		num_freed += ttm_pool_shrink();
-	while (!num_freed && atomic_long_read(&allocated_pages));
+	to_scan = min_t(unsigned long,
+			sc->nr_to_scan,
+			atomic_long_read(&allocated_pages));
+	while (freed < to_scan) {
+		freed += ttm_pool_shrink();
+		to_scan = min_t(unsigned long,
+				to_scan,
+				atomic_long_read(&allocated_pages));
+	}
 
-	return num_freed;
+	sc->nr_scanned = freed;
+
+	return freed ?: SHRINK_STOP;
 }
 
 /* Return the number of pages available or SHRINK_EMPTY if we have none */
@@ -1266,8 +1273,10 @@ EXPORT_SYMBOL(ttm_pool_debugfs);
 /* 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 = 1,
+	};
 	fs_reclaim_acquire(GFP_KERNEL);
 	seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
 		   ttm_pool_shrinker_scan(mm_shrinker, &sc));
Signature · 1 line
2.48.0
#2 lore raw

Re: [PATCH 1/2] drm/ttm: Respect the shrinker core free target

Quoted 30 lines · Tvrtko Ursulin

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.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
index c2ea865be657..a3247a82cadd 100644
@@ -1132,17 +1132,24 @@ void ttm_pool_fini(struct ttm_pool *pool)
 }
 EXPORT_SYMBOL(ttm_pool_fini);
 

drivers/gpu/drm/ttm/ttm_pool.c void ttm_pool_fini(struct ttm_pool *pool) · line 1143

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -5
-/* 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)
 {
-	unsigned long num_freed = 0;
+	unsigned long to_scan, freed = 0;
 
-	do
-		num_freed += ttm_pool_shrink();
-	while (!num_freed && atomic_long_read(&allocated_pages));

This can be massively simplified by changing this here to (num_freed < sc->nr_to_scan && atomic_long_read(&allocated_pages)).

Regards,
Christian.

Quoted 30 lines
drivers/gpu/drm/ttm/ttm_pool.c +16 -3
+	to_scan = min_t(unsigned long,
+			sc->nr_to_scan,
+			atomic_long_read(&allocated_pages));
+	while (freed < to_scan) {
+		freed += ttm_pool_shrink();
+		to_scan = min_t(unsigned long,
+				to_scan,
+				atomic_long_read(&allocated_pages));
+	}
 
-	return num_freed;
+	sc->nr_scanned = freed;
+
+	return freed ?: SHRINK_STOP;
 }
 
 /* Return the number of pages available or SHRINK_EMPTY if we have none */
@@ -1266,8 +1273,10 @@ EXPORT_SYMBOL(ttm_pool_debugfs);
 /* 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 = 1,
+	};
 	fs_reclaim_acquire(GFP_KERNEL);
 	seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
 		   ttm_pool_shrinker_scan(mm_shrinker, &sc));
#3 lore raw

PATCH 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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_pool.c +6 -1
index a3247a82cadd..ca6bbf9d0996 100644
@@ -1270,13 +1270,17 @@ 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,
-		.nr_to_scan = 1,
+		.nr_to_scan = TTM_SHRINKER_BATCH,
 	};
+
 	fs_reclaim_acquire(GFP_KERNEL);
 	seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(mm_shrinker, &sc),
 		   ttm_pool_shrinker_scan(mm_shrinker, &sc));
@@ -1333,6 +1337,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