Skip to contentSkip to conversation

drm/ttm: Make pool shrinker more responsive

PATCH In review Tvrtko Ursulin 2 participants 7 messages May 15, 2025 – May 16, 2025

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

PATCH 1/1 drm/ttm: Make pool shrinker more responsive

Currently the TTM pool shrinker ensures it frees at least something every time it is invoked, but it also lies to the core a bit on how hard it tried.

For example core will ask it to free SHRINK_BATCH pages but the shrinker can, due how it walks the LRU list of pools, free just a single page and still leave the core thinking it expended the full SHRINK_BATCH effort.

Apart from being inefficient in terms of number of calls to the TTM pool shrinker required, another consquence of this is that the core can abandon the shrinking attempt too early due thinking that the whole set of freeable pages has been scanned.

We fix this last part by correctly reporting how many out of potentially freeable pages have been scanned.

We also do the freeing in an aggressive manner, considering the scan target as a free target, to ensure walks over pools with unfreeable pages do not cause no-op calls into our callback.

And finally we customise the shrinker batch size based on the median pool order and the total number of pools, with the aim of searching more possible pools on an average invocation.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 39 +++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 15 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +24 -15
index c2ea865be657..a76fe5f95781 100644
@@ -368,7 +368,7 @@ static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
 }
 
 /* Free pages using the global shrinker list */
-static unsigned int ttm_pool_shrink(void)
+static unsigned int ttm_pool_shrink(unsigned long *nr_scanned)
 {
 	struct ttm_pool_type *pt;
 	unsigned int num_pages;
@@ -380,16 +380,15 @@ static unsigned int ttm_pool_shrink(void)
 	list_move_tail(&pt->shrinker_list, &shrinker_list);
 	spin_unlock(&shrinker_lock);
 
+	num_pages = 1 << pt->order;
+	(*nr_scanned) += num_pages;
+
 	p = ttm_pool_type_take(pt);
-	if (p) {
+	if (p)
 		ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
-		num_pages = 1 << pt->order;
-	} else {
-		num_pages = 0;
-	}
 	up_read(&pool_shrink_rwsem);
 
-	return num_pages;
+	return p ? num_pages : 0;
 }
 
 /* Return the allocation order based for a page */
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  */
 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
 {
+	unsigned long nr_scanned = 0;
+
 	ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
 
 	while (atomic_long_read(&allocated_pages) > page_pool_size)
-		ttm_pool_shrink();
+		ttm_pool_shrink(&nr_scanned);
 }
 EXPORT_SYMBOL(ttm_pool_free);
 
@@ -1132,17 +1133,21 @@ 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));
+	sc->nr_scanned = 0;
+	to_scan = min_t(unsigned long,
+			sc->nr_to_scan, atomic_long_read(&allocated_pages));
+	while (freed < to_scan) {
+		freed += ttm_pool_shrink(&sc->nr_scanned);
+		to_scan = min_t(unsigned long,
+				to_scan, atomic_long_read(&allocated_pages));
+	}
 
-	return num_freed;
+	return sc->nr_scanned ? freed : SHRINK_STOP;
 }
 
 /* Return the number of pages available or SHRINK_EMPTY if we have none */
@@ -1266,7 +1271,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),
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;
 	mm_shrinker->seeks = 1;
 
 	shrinker_register(mm_shrinker);
Signature · 1 line
2.48.0
#1 lore raw
Quoted 54 lines · Tvrtko Ursulin

Currently the TTM pool shrinker ensures it frees at least something every time it is invoked, but it also lies to the core a bit on how hard it tried.

For example core will ask it to free SHRINK_BATCH pages but the shrinker can, due how it walks the LRU list of pools, free just a single page and still leave the core thinking it expended the full SHRINK_BATCH effort.

Apart from being inefficient in terms of number of calls to the TTM pool shrinker required, another consquence of this is that the core can abandon the shrinking attempt too early due thinking that the whole set of freeable pages has been scanned.

We fix this last part by correctly reporting how many out of potentially freeable pages have been scanned.

We also do the freeing in an aggressive manner, considering the scan target as a free target, to ensure walks over pools with unfreeable pages do not cause no-op calls into our callback.

And finally we customise the shrinker batch size based on the median pool order and the total number of pools, with the aim of searching more possible pools on an average invocation.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 39 +++++++++++++++++++++-------------
 1 file changed, 24 insertions(+), 15 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +5 -2
index c2ea865be657..a76fe5f95781 100644
@@ -368,7 +368,7 @@ static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
 }
 
 /* Free pages using the global shrinker list */
-static unsigned int ttm_pool_shrink(void)
+static unsigned int ttm_pool_shrink(unsigned long *nr_scanned)
 {
 	struct ttm_pool_type *pt;
 	unsigned int num_pages;
@@ -380,16 +380,15 @@ static unsigned int ttm_pool_shrink(void)
 	list_move_tail(&pt->shrinker_list, &shrinker_list);
 	spin_unlock(&shrinker_lock);
 
+	num_pages = 1 << pt->order;
+	(*nr_scanned) += num_pages;
+
 	p = ttm_pool_type_take(pt);
-	if (p) {
+	if (p)

drivers/gpu/drm/ttm/ttm_pool.c static unsigned int ttm_pool_shrink(void) · line 392

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -5
 		ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
-		num_pages = 1 << pt->order;
-	} else {
-		num_pages = 0;
-	}
 	up_read(&pool_shrink_rwsem);
 
-	return num_pages;
+	return p ? num_pages : 0;
 }

That change here doesn't make any sense.

Scanning a pool type for pages and not finding anything doesn't mean we have scanned for freeable pages and then not freed them.

Quoted 31 lines
drivers/gpu/drm/ttm/ttm_pool.c +5 -6
 /* Return the allocation order based for a page */
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
  */
 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
 {
+	unsigned long nr_scanned = 0;
+
 	ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
 
 	while (atomic_long_read(&allocated_pages) > page_pool_size)
-		ttm_pool_shrink();
+		ttm_pool_shrink(&nr_scanned);
 }
 EXPORT_SYMBOL(ttm_pool_free);
 
@@ -1132,17 +1133,21 @@ 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));
+	sc->nr_scanned = 0;

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

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +8 -1
+	to_scan = min_t(unsigned long,
+			sc->nr_to_scan, atomic_long_read(&allocated_pages));
+	while (freed < to_scan) {
+		freed += ttm_pool_shrink(&sc->nr_scanned);
+		to_scan = min_t(unsigned long,
+				to_scan, atomic_long_read(&allocated_pages));
+	}
 
-	return num_freed;
+	return sc->nr_scanned ? freed : SHRINK_STOP;

That again doesn't make sense. That we only find pool types which don't have pages doesn't mean we have scanned them.

As far as I can see the existing code was correct after all.

Quoted 16 lines
drivers/gpu/drm/ttm/ttm_pool.c +4 -1
 }
 
 /* Return the number of pages available or SHRINK_EMPTY if we have none */
@@ -1266,7 +1271,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),

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_mgr_init(unsigned long num_pages) · line 1335

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -0
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;

Since we install only one global shrinker for all pool types, which might contain everything from 1 page till 512 pages, this seems to not make sense at all either.

What exactly are you trying to do here?

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
 	mm_shrinker->seeks = 1;
 
 	shrinker_register(mm_shrinker);
#2 lore raw
Quoted 62 lines · Christian König
Quoted 61 lines · Tvrtko Ursulin

Currently the TTM pool shrinker ensures it frees at least something every time it is invoked, but it also lies to the core a bit on how hard it tried.

For example core will ask it to free SHRINK_BATCH pages but the shrinker can, due how it walks the LRU list of pools, free just a single page and still leave the core thinking it expended the full SHRINK_BATCH effort.

Apart from being inefficient in terms of number of calls to the TTM pool shrinker required, another consquence of this is that the core can abandon the shrinking attempt too early due thinking that the whole set of freeable pages has been scanned.

We fix this last part by correctly reporting how many out of potentially freeable pages have been scanned.

We also do the freeing in an aggressive manner, considering the scan target as a free target, to ensure walks over pools with unfreeable pages do not cause no-op calls into our callback.

And finally we customise the shrinker batch size based on the median pool order and the total number of pools, with the aim of searching more possible pools on an average invocation.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
  drivers/gpu/drm/ttm/ttm_pool.c | 39 +++++++++++++++++++++-------------
  1 file changed, 24 insertions(+), 15 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +5 -6
index c2ea865be657..a76fe5f95781 100644
@@ -368,7 +368,7 @@ static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
  }
 
  /* Free pages using the global shrinker list */
-static unsigned int ttm_pool_shrink(void)
+static unsigned int ttm_pool_shrink(unsigned long *nr_scanned)
  {
  	struct ttm_pool_type *pt;
  	unsigned int num_pages;
@@ -380,16 +380,15 @@ static unsigned int ttm_pool_shrink(void)
  	list_move_tail(&pt->shrinker_list, &shrinker_list);
  	spin_unlock(&shrinker_lock);
 
+	num_pages = 1 << pt->order;
+	(*nr_scanned) += num_pages;
+
  	p = ttm_pool_type_take(pt);
-	if (p) {
+	if (p)
  		ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
-		num_pages = 1 << pt->order;
-	} else {
-		num_pages = 0;
-	}
  	up_read(&pool_shrink_rwsem);
 
Quoted 7 lines

drivers/gpu/drm/ttm/ttm_pool.c static unsigned int ttm_pool_shrink(void) · line 392

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -1
-	return num_pages;
+	return p ? num_pages : 0;
  }

That change here doesn't make any sense.

Scanning a pool type for pages and not finding anything doesn't mean we have scanned for freeable pages and then not freed them.

I couldn't quite understand this sentence but let me try and explain.

I have empirically observed when looking at a different bug that the TTM shrinking does not work as efficiently as it could, and as efficiently as the shrinker core expects it to work.

Currently shrinker asks TTM to shrink SHRINK_BATCH pages.

Lets say ttm_pool_shrinks happens to try order 0 pool first so frees one page. It returns 1 to the shrinker core.

But because TTM shrinker does not currently update shrinkerctl->nr_scanned, shrinker core assumes TTM looked at full SHRINK_BATCH pages with every call, and adds and decrements that value to the counters it uses to determine when to stop trying the TTM shrinker and move to the next one.

Therefore the exit condition can trigger too early in relation to the number of shrinkable pages in TTM pools. Or it can require too many calls to ttm_pool_shrinker_scan. Depending on the distribution of freeable pages per pools.

This patch:

1. Fixes the TTM shrinker to correctly report shrinkctl->nr_scanned.

This way the break condition in shrinker core works correctly.

2. Makes TTM shrinker actually scan as much as the core requested from it (respecting shrinkerctl->nr_to_scan).

This avoids only scanning say one page when shrinker core asked to scan 128. This reduces the number of calls into the TTM shrinker from the core to free same amount of pages.

3. Tunes the batch size away from the default 128 (SHRINK_BATCH) into a value based on the median size of the TTM pools.

This ensures that on average TTM shrinker tries to actually free _something_ more often than it does now. Otherwise it can happen to try a few pools (depending LRU order) and give up, while the freeable pages are actually in the pools not yet looked at. By tuning the batch size it ensures that on average more pools are looked at.

Now mind you I looked at this two months ago, which is when I first sent this patch out, but haven't went through this today again, it still makes sense to me. So I am curious why you think it totally does not. It looks obvious to me it is not respecting the contract with the core so I would be surprised if I was missing some obvious gotcha.

Regards,

Tvrtko

Quoted 79 lines
Quoted 32 lines
drivers/gpu/drm/ttm/ttm_pool.c +5 -6
  /* Return the allocation order based for a page */
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
   */
  void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
  {
+	unsigned long nr_scanned = 0;
+
  	ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
 
  	while (atomic_long_read(&allocated_pages) > page_pool_size)
-		ttm_pool_shrink();
+		ttm_pool_shrink(&nr_scanned);
  }
  EXPORT_SYMBOL(ttm_pool_free);
 
@@ -1132,17 +1133,21 @@ 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));
+	sc->nr_scanned = 0;

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

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +8 -1
+	to_scan = min_t(unsigned long,
+			sc->nr_to_scan, atomic_long_read(&allocated_pages));
+	while (freed < to_scan) {
+		freed += ttm_pool_shrink(&sc->nr_scanned);
+		to_scan = min_t(unsigned long,
+				to_scan, atomic_long_read(&allocated_pages));
+	}
 
-	return num_freed;
+	return sc->nr_scanned ? freed : SHRINK_STOP;

That again doesn't make sense. That we only find pool types which don't have pages doesn't mean we have scanned them.

As far as I can see the existing code was correct after all.

Quoted 16 lines
drivers/gpu/drm/ttm/ttm_pool.c +4 -1
  }
 
  /* Return the number of pages available or SHRINK_EMPTY if we have none */
@@ -1266,7 +1271,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),

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_mgr_init(unsigned long num_pages) · line 1335

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -0
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;

Since we install only one global shrinker for all pool types, which might contain everything from 1 page till 512 pages, this seems to not make sense at all either.

What exactly are you trying to do here?

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
  	mm_shrinker->seeks = 1;
 
  	shrinker_register(mm_shrinker);
#3 lore raw
Quoted 67 lines · Tvrtko Ursulin
Quoted 65 lines · Christian König
Quoted 51 lines · Tvrtko Ursulin

Currently the TTM pool shrinker ensures it frees at least something every time it is invoked, but it also lies to the core a bit on how hard it tried.

For example core will ask it to free SHRINK_BATCH pages but the shrinker can, due how it walks the LRU list of pools, free just a single page and still leave the core thinking it expended the full SHRINK_BATCH effort.

Apart from being inefficient in terms of number of calls to the TTM pool shrinker required, another consquence of this is that the core can abandon the shrinking attempt too early due thinking that the whole set of freeable pages has been scanned.

We fix this last part by correctly reporting how many out of potentially freeable pages have been scanned.

We also do the freeing in an aggressive manner, considering the scan target as a free target, to ensure walks over pools with unfreeable pages do not cause no-op calls into our callback.

And finally we customise the shrinker batch size based on the median pool order and the total number of pools, with the aim of searching more possible pools on an average invocation.

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

---
  drivers/gpu/drm/ttm/ttm_pool.c | 39 +++++++++++++++++++++-------------

  1 file changed, 24 insertions(+), 15 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +3 -2
index c2ea865be657..a76fe5f95781 100644
@@ -368,7 +368,7 @@ static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
   }
     /* Free pages using the global shrinker list */
-static unsigned int ttm_pool_shrink(void)
+static unsigned int ttm_pool_shrink(unsigned long *nr_scanned)
   {
       struct ttm_pool_type *pt;
       unsigned int num_pages;
@@ -380,16 +380,15 @@ static unsigned int ttm_pool_shrink(void)
       list_move_tail(&pt->shrinker_list, &shrinker_list);
       spin_unlock(&shrinker_lock);
   +    num_pages = 1 << pt->order;
+    (*nr_scanned) += num_pages;
+
       p = ttm_pool_type_take(pt);
-    if (p) {

drivers/gpu/drm/ttm/ttm_pool.c static unsigned int ttm_pool_shrink(void) · line 391

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +2 -4
+    if (p)
           ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
-        num_pages = 1 << pt->order;
-    } else {
-        num_pages = 0;
-    }
       up_read(&pool_shrink_rwsem);
   -    return num_pages;
+    return p ? num_pages : 0;
   }

That change here doesn't make any sense.

Quoted 5 lines
Quoted 1 line

Scanning a pool type for pages and not finding anything doesn't mean we have scanned for freeable pages and then not freed them.

I couldn't quite understand this sentence but let me try and explain.

I have empirically observed when looking at a different bug that the TTM shrinking does not work as efficiently as it could, and as efficiently as the shrinker core expects it to work.

Yeah, that could be quite likely.

Quoted 3 lines

Currently shrinker asks TTM to shrink SHRINK_BATCH pages.

Lets say ttm_pool_shrinks happens to try order 0 pool first so frees one page. It returns 1 to the shrinker core.

That's intentional. We have gone back and forth over if we should just scan one pool or multiple.

Quoted 1 line

But because TTM shrinker does not currently update shrinkerctl->nr_scanned, shrinker core assumes TTM looked at full SHRINK_BATCH pages with every call, and adds and decrements that value to the counters it uses to determine when to stop trying the TTM shrinker and move to the next one.

Yeah that was intentional as well.

Quoted 1 line

Therefore the exit condition can trigger too early in relation to the number of shrinkable pages in TTM pools. Or it can require too many calls to ttm_pool_shrinker_scan.

Oh, that is indeed problematic.

Quoted 1 line

Depending on the distribution of freeable pages per pools.

No, that should be completely unrelated.

Quoted 3 lines

This patch:

1. Fixes the TTM shrinker to correctly report shrinkctl->nr_scanned.

No it doesn't. The nr_scanned for the TTM pool is not related to the number of pool types scanned.

Quoted 5 lines

This way the break condition in shrinker core works correctly.

2. Makes TTM shrinker actually scan as much as the core requested from it (respecting shrinkerctl->nr_to_scan).

This avoids only scanning say one page when shrinker core asked to scan 128. This reduces the number of calls into the TTM shrinker from the core to free same amount of pages.

That should be easy, you just need to adjust the while loop in ttm_pool_shrinker_scan().

Quoted 4 lines

3. Tunes the batch size away from the default 128 (SHRINK_BATCH) into a value based on the median size of the TTM pools.

This ensures that on average TTM shrinker tries to actually free _something_ more often than it does now. Otherwise it can happen to try a few pools (depending LRU order) and give up, while the freeable pages are actually in the pools not yet looked at. By tuning the batch size it ensures that on average more pools are looked at.

That doesn't make sense. The number of pools looked at is completely irrelevant for the shrinker.

We should just look at all pools round robin until the number of objects/pages the shrinker wants to free has been freed.

Quoted 2 lines

Now mind you I looked at this two months ago, which is when I first sent this patch out, but haven't went through this today again, it still makes sense to me. So I am curious why you think it totally does not. It looks obvious to me it is not respecting the contract with the core so I would be surprised if I was missing some obvious gotcha.

It sounds like you are assuming that the pool type structure are the objects to scan and we abort when we have scanned a certain amount. But that isn't true, we abort when we can't find any page to free any more.

Regards,
Christian.

Quoted 75 lines

Regards,

Tvrtko

Quoted 69 lines
Quoted 28 lines
drivers/gpu/drm/ttm/ttm_pool.c +3 -1
     /* Return the allocation order based for a page */
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
    */
   void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
   {
+    unsigned long nr_scanned = 0;
+
       ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
         while (atomic_long_read(&allocated_pages) > page_pool_size)
-        ttm_pool_shrink();
+        ttm_pool_shrink(&nr_scanned);
   }
   EXPORT_SYMBOL(ttm_pool_free);
   @@ -1132,17 +1133,21 @@ 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)); +    sc->nr_scanned = 0;
+    to_scan = min_t(unsigned long,

Quoted 8 lines

+            sc->nr_to_scan, atomic_long_read(&allocated_pages)); +    while (freed < to_scan) {
+        freed += ttm_pool_shrink(&sc->nr_scanned);
+        to_scan = min_t(unsigned long,
+                to_scan, atomic_long_read(&allocated_pages)); +    }
  -    return num_freed;
+    return sc->nr_scanned ? freed : SHRINK_STOP;

That again doesn't make sense. That we only find pool types which don't have pages doesn't mean we have scanned them.

As far as I can see the existing code was correct after all.

Quoted 14 lines

  }
    /* Return the number of pages available or SHRINK_EMPTY if we have none */

drivers/gpu/drm/ttm/ttm_pool.c +4 -1
@@ -1266,7 +1271,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),

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_mgr_init(unsigned long num_pages) · line 1334

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -0
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;

Since we install only one global shrinker for all pool types, which might contain everything from 1 page till 512 pages, this seems to not make sense at all either.

What exactly are you trying to do here?

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
       mm_shrinker->seeks = 1;
         shrinker_register(mm_shrinker);
#4 lore raw
Quoted 78 lines · Christian König
Quoted 67 lines · Tvrtko Ursulin
Quoted 65 lines · Christian König
Quoted 51 lines · Tvrtko Ursulin

Currently the TTM pool shrinker ensures it frees at least something every time it is invoked, but it also lies to the core a bit on how hard it tried.

For example core will ask it to free SHRINK_BATCH pages but the shrinker can, due how it walks the LRU list of pools, free just a single page and still leave the core thinking it expended the full SHRINK_BATCH effort.

Apart from being inefficient in terms of number of calls to the TTM pool shrinker required, another consquence of this is that the core can abandon the shrinking attempt too early due thinking that the whole set of freeable pages has been scanned.

We fix this last part by correctly reporting how many out of potentially freeable pages have been scanned.

We also do the freeing in an aggressive manner, considering the scan target as a free target, to ensure walks over pools with unfreeable pages do not cause no-op calls into our callback.

And finally we customise the shrinker batch size based on the median pool order and the total number of pools, with the aim of searching more possible pools on an average invocation.

Cc Christian König <[email protected]>
Cc Thomas Hellström <[email protected]>
---
   drivers/gpu/drm/ttm/ttm_pool.c | 39 +++++++++++++++++++++-------------
   1 file changed, 24 insertions(+), 15 deletions(-)
drivers/gpu/drm/ttm/ttm_pool.c +3 -2
index c2ea865be657..a76fe5f95781 100644
@@ -368,7 +368,7 @@ static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
   }
     /* Free pages using the global shrinker list */
-static unsigned int ttm_pool_shrink(void)
+static unsigned int ttm_pool_shrink(unsigned long *nr_scanned)
   {
       struct ttm_pool_type *pt;
       unsigned int num_pages;
@@ -380,16 +380,15 @@ static unsigned int ttm_pool_shrink(void)
       list_move_tail(&pt->shrinker_list, &shrinker_list);
       spin_unlock(&shrinker_lock);
   +    num_pages = 1 << pt->order;
+    (*nr_scanned) += num_pages;
+
       p = ttm_pool_type_take(pt);
-    if (p) {

drivers/gpu/drm/ttm/ttm_pool.c static unsigned int ttm_pool_shrink(void) · line 391

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +2 -4
+    if (p)
           ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
-        num_pages = 1 << pt->order;
-    } else {
-        num_pages = 0;
-    }
       up_read(&pool_shrink_rwsem);
   -    return num_pages;
+    return p ? num_pages : 0;
   }

That change here doesn't make any sense.

Quoted 5 lines
Quoted 1 line

Scanning a pool type for pages and not finding anything doesn't mean we have scanned for freeable pages and then not freed them.

I couldn't quite understand this sentence but let me try and explain.

I have empirically observed when looking at a different bug that the TTM shrinking does not work as efficiently as it could, and as efficiently as the shrinker core expects it to work.

Yeah, that could be quite likely.

Quoted 2 lines

Currently shrinker asks TTM to shrink SHRINK_BATCH pages.

Quoted 7 lines
Quoted 1 line

Lets say ttm_pool_shrinks happens to try order 0 pool first so frees one page. It returns 1 to the shrinker core.

That's intentional. We have gone back and forth over if we should just scan one pool or multiple.

Quoted 1 line

But because TTM shrinker does not currently update shrinkerctl->nr_scanned, shrinker core assumes TTM looked at full SHRINK_BATCH pages with every call, and adds and decrements that value to the counters it uses to determine when to stop trying the TTM shrinker and move to the next one.

Yeah that was intentional as well.

What was the reason do you remember?

Quoted 6 lines
Quoted 1 line

Therefore the exit condition can trigger too early in relation to the number of shrinkable pages in TTM pools. Or it can require too many calls to ttm_pool_shrinker_scan.

Oh, that is indeed problematic.

Quoted 1 line

Depending on the distribution of freeable pages per pools.

Quoted 7 lines

No, that should be completely unrelated.

Quoted 3 lines

This patch:

1. Fixes the TTM shrinker to correctly report shrinkctl->nr_scanned.

No it doesn't. The nr_scanned for the TTM pool is not related to the number of pool types scanned.

Not sure what you mean here.

Ttm_pool_shrinker_count() reports in number of pages which shrinker core then uses to set nr_to_scan. But as ttm_pool_shrinker_scan() does not update nr_scanned the core shrinker logic is foiled. If that is by design I would like to understand why.

But as nr_scanned is currently not updated at all, it is obviously not related to pools, or anything really.

And in my patch it is still not related to the pools. I made it report the number of _pages_ scanned to align with ttm_pool_shrinker_count() and what the shrinker core expects.

Quoted 7 lines
Quoted 5 lines

This way the break condition in shrinker core works correctly.

2. Makes TTM shrinker actually scan as much as the core requested from it (respecting shrinkerctl->nr_to_scan).

This avoids only scanning say one page when shrinker core asked to scan 128. This reduces the number of calls into the TTM shrinker from the core to free same amount of pages.

That should be easy, you just need to adjust the while loop in ttm_pool_shrinker_scan().

Yes I think this part would be simple on its own.

Quoted 5 lines
Quoted 3 lines

3. Tunes the batch size away from the default 128 (SHRINK_BATCH) into a value based on the median size of the TTM pools.

This ensures that on average TTM shrinker tries to actually free _something_ more often than it does now. Otherwise it can happen to try a few pools (depending LRU order) and give up, while the freeable pages are actually in the pools not yet looked at. By tuning the batch size it ensures that on average more pools are looked at.

That doesn't make sense. The number of pools looked at is completely irrelevant for the shrinker.

Nr_to_scan the shrinker core asks for is default at most 128 pages.

This means TTM shrinker can look at the few small order pools, find nothing, and exit. Shrinker core may then call into it again to try again. Or maybe it can even move on the the next shrinker (away from TTM) if the distribution of free pages between pools is right.

The exact behaviour here will depend on whether TTM shrinker starts respecting nr_to_scan and reporting nr_scanned so it is a bit premature to discuss in detail until those two are settled.

Anyway, by tweaking the batch_size from 128 to 352 we can make the same effect in fewer iterations.

Quoted 1 line

We should just look at all pools round robin until the number of objects/pages the shrinker wants to free has been freed.

I thought that was what the patch does.

Although on a second look I think am missing an exit condition for the case where requested amount cannot be freed.

Quoted 3 lines
Quoted 1 line

Now mind you I looked at this two months ago, which is when I first sent this patch out, but haven't went through this today again, it still makes sense to me. So I am curious why you think it totally does not. It looks obvious to me it is not respecting the contract with the core so I would be surprised if I was missing some obvious gotcha.

It sounds like you are assuming that the pool type structure are the objects to scan and we abort when we have scanned a certain amount. But that isn't true, we abort when we can't find any page to free any more.

Maybe I am blind but AFAICT it currently exits as soon as it frees at least one page:

static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
struct shrink_control *sc)

{
	unsigned long num_freed = 0;

	do
		num_freed += ttm_pool_shrink();
	while (!num_freed && atomic_long_read(&allocated_pages));

	return num_freed;
}

Or you mean from the point of view of the core shrinker? Again, that one is complicated since it currently always decrements total_scan by SHRINK_BATCH. And total_scan is set to at most half of pool pages. So it can scan order0 find nothing -> total_scan -= 128, order1 find nothing -> total_scan -= 128, etc, and it soon accumulates a false positive large number of scanned pages and exits from TTM shrinker if freeable is low, but actually shrinkable pages are in the high order pool it did not even get to try.

Regards,

Tvrtko

Quoted 70 lines
Quoted 69 lines
Quoted 68 lines
Quoted 27 lines
drivers/gpu/drm/ttm/ttm_pool.c +3 -1
     /* Return the allocation order based for a page */
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
    */
   void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
   {
+    unsigned long nr_scanned = 0;
+
       ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
         while (atomic_long_read(&allocated_pages) > page_pool_size)
-        ttm_pool_shrink();
+        ttm_pool_shrink(&nr_scanned);
   }
   EXPORT_SYMBOL(ttm_pool_free);
   @@ -1132,17 +1133,21 @@ 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)); +    sc->nr_scanned = 0;
+    to_scan = min_t(unsigned long,

Quoted 8 lines

+            sc->nr_to_scan, atomic_long_read(&allocated_pages)); +    while (freed < to_scan) {
+        freed += ttm_pool_shrink(&sc->nr_scanned);
+        to_scan = min_t(unsigned long,
+                to_scan, atomic_long_read(&allocated_pages)); +    }
  -    return num_freed;
+    return sc->nr_scanned ? freed : SHRINK_STOP;

That again doesn't make sense. That we only find pool types which don't have pages doesn't mean we have scanned them.

As far as I can see the existing code was correct after all.

Quoted 14 lines

  }
    /* Return the number of pages available or SHRINK_EMPTY if we have none */

drivers/gpu/drm/ttm/ttm_pool.c +4 -1
@@ -1266,7 +1271,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),

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_mgr_init(unsigned long num_pages) · line 1334

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -0
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;

Since we install only one global shrinker for all pool types, which might contain everything from 1 page till 512 pages, this seems to not make sense at all either.

What exactly are you trying to do here?

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
       mm_shrinker->seeks = 1;
         shrinker_register(mm_shrinker);
#5 lore raw
Quoted 5 lines · Tvrtko Ursulin
Quoted 3 lines
Quoted 1 line

But because TTM shrinker does not currently update shrinkerctl->nr_scanned, shrinker core assumes TTM looked at full SHRINK_BATCH pages with every call, and adds and decrements that value to the counters it uses to determine when to stop trying the TTM shrinker and move to the next one.

Yeah that was intentional as well.

What was the reason do you remember?

Uff I think it hat something to do with fairness between the different pool types.

Quoted 15 lines
Quoted 7 lines
Quoted 2 lines

Therefore the exit condition can trigger too early in relation to the number of shrinkable pages in TTM pools. Or it can require too many calls to ttm_pool_shrinker_scan.

Oh, that is indeed problematic.

Quoted 1 line

Depending on the distribution of freeable pages per pools.

Quoted 7 lines

No, that should be completely unrelated.

Quoted 3 lines

This patch:

1. Fixes the TTM shrinker to correctly report shrinkctl->nr_scanned.

No it doesn't. The nr_scanned for the TTM pool is not related to the number of pool types scanned.

Quoted 7 lines

Not sure what you mean here.

Ttm_pool_shrinker_count() reports in number of pages which shrinker core then uses to set nr_to_scan. But as ttm_pool_shrinker_scan() does not update nr_scanned the core shrinker logic is foiled. If that is by design I would like to understand why.

But as nr_scanned is currently not updated at all, it is obviously not related to pools, or anything really.

And in my patch it is still not related to the pools. I made it report the number of _pages_ scanned to align with ttm_pool_shrinker_count() and what the shrinker core expects.

Exactly that's the point, you are not using the number of pages scanned, but rather the order of the pool type. And that is clearly incorrect.

Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

This way the break condition in shrinker core works correctly.

2. Makes TTM shrinker actually scan as much as the core requested from it (respecting shrinkerctl->nr_to_scan).

This avoids only scanning say one page when shrinker core asked to scan 128. This reduces the number of calls into the TTM shrinker from the core to free same amount of pages.

That should be easy, you just need to adjust the while loop in ttm_pool_shrinker_scan().

Yes I think this part would be simple on its own.

Quoted 2 lines
Quoted 2 lines

3. Tunes the batch size away from the default 128 (SHRINK_BATCH) into a value based on the median size of the TTM pools.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

This ensures that on average TTM shrinker tries to actually free _something_ more often than it does now. Otherwise it can happen to try a few pools (depending LRU order) and give up, while the freeable pages are actually in the pools not yet looked at. By tuning the batch size it ensures that on average more pools are looked at.

That doesn't make sense. The number of pools looked at is completely irrelevant for the shrinker.

Nr_to_scan the shrinker core asks for is default at most 128 pages.

This means TTM shrinker can look at the few small order pools, find nothing, and exit.

No, that is not what the code does. See here:

        do
                num_freed += ttm_pool_shrink();
        while (!num_freed && atomic_long_read(&allocated_pages));

We scan until we find at least one page to free or the global allocated pages counter says that we don't have any pages to free.

Looking at empty pools actually doesn't make much sense, so they are just skipped until we find the one which contains the pages.

Quoted 3 lines

Shrinker core may then call into it again to try again. Or maybe it can even move on the the next shrinker (away from TTM) if the distribution of free pages between pools is right.

The exact behaviour here will depend on whether TTM shrinker starts respecting nr_to_scan and reporting nr_scanned so it is a bit premature to discuss in detail until those two are settled.

Then it is probably a good idea to separate that.

Quoted 19 lines

Anyway, by tweaking the batch_size from 128 to 352 we can make the same effect in fewer iterations.

Quoted 1 line

We should just look at all pools round robin until the number of objects/pages the shrinker wants to free has been freed.

I thought that was what the patch does.

Although on a second look I think am missing an exit condition for the case where requested amount cannot be freed.

Quoted 3 lines
Quoted 1 line

Now mind you I looked at this two months ago, which is when I first sent this patch out, but haven't went through this today again, it still makes sense to me. So I am curious why you think it totally does not. It looks obvious to me it is not respecting the contract with the core so I would be surprised if I was missing some obvious gotcha.

It sounds like you are assuming that the pool type structure are the objects to scan and we abort when we have scanned a certain amount. But that isn't true, we abort when we can't find any page to free any more.

Maybe I am blind but AFAICT it currently exits as soon as it frees at least one page:

static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
{
    unsigned long num_freed = 0;

Quoted 8 lines

    do
        num_freed += ttm_pool_shrink();
    while (!num_freed && atomic_long_read(&allocated_pages));

    return num_freed;
}

Or you mean from the point of view of the core shrinker? Again, that one is complicated since it currently always decrements total_scan by SHRINK_BATCH. And total_scan is set to at most half of pool pages. So it can scan order0 find nothing -> total_scan -= 128, order1 find nothing -> total_scan -= 128, etc, and it soon accumulates a false positive large number of scanned pages and exits from TTM shrinker if freeable is low, but actually shrinkable pages are in the high order pool it did not even get to try.

That is not what happens. See the loop tries to free pages from all pools until it finds at least one.

After one page is freed the pool type is put at the end of the LRU, so with each new call to ttm_pool_shrink() we will keep going over different pool types.

See this here:

        pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
        list_move_tail(&pt->shrinker_list, &shrinker_list);

Regards,
Christian.

Quoted 76 lines

Regards,

Tvrtko

Quoted 70 lines
Quoted 69 lines
Quoted 68 lines
Quoted 27 lines

     /* Return the allocation order based for a page */

drivers/gpu/drm/ttm/ttm_pool.c +3 -1
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
     */
    void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
    {
+    unsigned long nr_scanned = 0;
+
        ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
          while (atomic_long_read(&allocated_pages) > page_pool_size)
-        ttm_pool_shrink();
+        ttm_pool_shrink(&nr_scanned);
    }
    EXPORT_SYMBOL(ttm_pool_free);
    @@ -1132,17 +1133,21 @@ 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)); +    sc->nr_scanned = 0;
+    to_scan = min_t(unsigned long,

Quoted 8 lines

+            sc->nr_to_scan, atomic_long_read(&allocated_pages)); +    while (freed < to_scan) {
+        freed += ttm_pool_shrink(&sc->nr_scanned);
+        to_scan = min_t(unsigned long,
+                to_scan, atomic_long_read(&allocated_pages)); +    }
   -    return num_freed;
+    return sc->nr_scanned ? freed : SHRINK_STOP;

That again doesn't make sense. That we only find pool types which don't have pages doesn't mean we have scanned them.

As far as I can see the existing code was correct after all.

Quoted 14 lines

   }
     /* Return the number of pages available or SHRINK_EMPTY if we have none */

drivers/gpu/drm/ttm/ttm_pool.c +4 -1
@@ -1266,7 +1271,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),

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_mgr_init(unsigned long num_pages) · line 1334

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -0
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;

Since we install only one global shrinker for all pool types, which might contain everything from 1 page till 512 pages, this seems to not make sense at all either.

What exactly are you trying to do here?

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
        mm_shrinker->seeks = 1;
          shrinker_register(mm_shrinker);
#6 lore raw
Quoted 26 lines · Christian König
Quoted 5 lines · Tvrtko Ursulin
Quoted 3 lines
Quoted 1 line

But because TTM shrinker does not currently update shrinkerctl->nr_scanned, shrinker core assumes TTM looked at full SHRINK_BATCH pages with every call, and adds and decrements that value to the counters it uses to determine when to stop trying the TTM shrinker and move to the next one.

Yeah that was intentional as well.

What was the reason do you remember?

Uff I think it hat something to do with fairness between the different pool types.

Quoted 17 lines
Quoted 7 lines
Quoted 2 lines

Therefore the exit condition can trigger too early in relation to the number of shrinkable pages in TTM pools. Or it can require too many calls to ttm_pool_shrinker_scan.

Oh, that is indeed problematic.

Quoted 1 line

Depending on the distribution of freeable pages per pools.

Quoted 7 lines

No, that should be completely unrelated.

Quoted 3 lines

This patch:

1. Fixes the TTM shrinker to correctly report shrinkctl->nr_scanned.

No it doesn't. The nr_scanned for the TTM pool is not related to the number of pool types scanned.

Not sure what you mean here.

Quoted 7 lines
Quoted 5 lines

Ttm_pool_shrinker_count() reports in number of pages which shrinker core then uses to set nr_to_scan. But as ttm_pool_shrinker_scan() does not update nr_scanned the core shrinker logic is foiled. If that is by design I would like to understand why.

But as nr_scanned is currently not updated at all, it is obviously not related to pools, or anything really.

And in my patch it is still not related to the pools. I made it report the number of _pages_ scanned to align with ttm_pool_shrinker_count() and what the shrinker core expects.

Exactly that's the point, you are not using the number of pages scanned, but rather the order of the pool type. And that is clearly incorrect.

Ugh, you are absolutely correct.

I don't know what happened here.. well I know to an extent. I was working on some other bug and saw some weird shrinker behaviour. Misunderstood how it worked, "fixed it", saw the trace points showing a lot less activity which made me think "yeah this is better" and moved on. Until I remembered this patch and sent it out.

I think there is a little bit that could be improved, but this version clearly goes into the trash.

Sorry for the trouble and thanks a lot for persisting with explanations!

Regards,

Tvrtko

Quoted 154 lines
Quoted 12 lines
Quoted 7 lines
Quoted 5 lines

This way the break condition in shrinker core works correctly.

2. Makes TTM shrinker actually scan as much as the core requested from it (respecting shrinkerctl->nr_to_scan).

This avoids only scanning say one page when shrinker core asked to scan 128. This reduces the number of calls into the TTM shrinker from the core to free same amount of pages.

That should be easy, you just need to adjust the while loop in ttm_pool_shrinker_scan().

Yes I think this part would be simple on its own.

Quoted 2 lines
Quoted 2 lines

3. Tunes the batch size away from the default 128 (SHRINK_BATCH) into a value based on the median size of the TTM pools.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

This ensures that on average TTM shrinker tries to actually free _something_ more often than it does now. Otherwise it can happen to try a few pools (depending LRU order) and give up, while the freeable pages are actually in the pools not yet looked at. By tuning the batch size it ensures that on average more pools are looked at.

That doesn't make sense. The number of pools looked at is completely irrelevant for the shrinker.

Nr_to_scan the shrinker core asks for is default at most 128 pages.

This means TTM shrinker can look at the few small order pools, find nothing, and exit.

No, that is not what the code does. See here:

         do
                 num_freed += ttm_pool_shrink();
         while (!num_freed && atomic_long_read(&allocated_pages));

We scan until we find at least one page to free or the global allocated pages counter says that we don't have any pages to free.

Looking at empty pools actually doesn't make much sense, so they are just skipped until we find the one which contains the pages.

Quoted 3 lines

Shrinker core may then call into it again to try again. Or maybe it can even move on the the next shrinker (away from TTM) if the distribution of free pages between pools is right.

The exact behaviour here will depend on whether TTM shrinker starts respecting nr_to_scan and reporting nr_scanned so it is a bit premature to discuss in detail until those two are settled.

Then it is probably a good idea to separate that.

Quoted 19 lines

Anyway, by tweaking the batch_size from 128 to 352 we can make the same effect in fewer iterations.

Quoted 1 line

We should just look at all pools round robin until the number of objects/pages the shrinker wants to free has been freed.

I thought that was what the patch does.

Although on a second look I think am missing an exit condition for the case where requested amount cannot be freed.

Quoted 3 lines
Quoted 1 line

Now mind you I looked at this two months ago, which is when I first sent this patch out, but haven't went through this today again, it still makes sense to me. So I am curious why you think it totally does not. It looks obvious to me it is not respecting the contract with the core so I would be surprised if I was missing some obvious gotcha.

It sounds like you are assuming that the pool type structure are the objects to scan and we abort when we have scanned a certain amount. But that isn't true, we abort when we can't find any page to free any more.

Maybe I am blind but AFAICT it currently exits as soon as it frees at least one page:

static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
                        struct shrink_control *sc)
{
    unsigned long num_freed = 0;

Quoted 8 lines

    do
        num_freed += ttm_pool_shrink();
    while (!num_freed && atomic_long_read(&allocated_pages));

    return num_freed;
}

Or you mean from the point of view of the core shrinker? Again, that one is complicated since it currently always decrements total_scan by SHRINK_BATCH. And total_scan is set to at most half of pool pages. So it can scan order0 find nothing -> total_scan -= 128, order1 find nothing -> total_scan -= 128, etc, and it soon accumulates a false positive large number of scanned pages and exits from TTM shrinker if freeable is low, but actually shrinkable pages are in the high order pool it did not even get to try.

That is not what happens. See the loop tries to free pages from all pools until it finds at least one.

After one page is freed the pool type is put at the end of the LRU, so with each new call to ttm_pool_shrink() we will keep going over different pool types.

See this here:

         pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
         list_move_tail(&pt->shrinker_list, &shrinker_list);

Regards,
Christian.

Quoted 76 lines

Regards,

Tvrtko

Quoted 70 lines
Quoted 69 lines
Quoted 68 lines
Quoted 27 lines

     /* Return the allocation order based for a page */

drivers/gpu/drm/ttm/ttm_pool.c +3 -1
@@ -881,10 +880,12 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
     */
    void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
    {
+    unsigned long nr_scanned = 0;
+
        ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
          while (atomic_long_read(&allocated_pages) > page_pool_size)
-        ttm_pool_shrink();
+        ttm_pool_shrink(&nr_scanned);
    }
    EXPORT_SYMBOL(ttm_pool_free);
    @@ -1132,17 +1133,21 @@ 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)); +    sc->nr_scanned = 0;
+    to_scan = min_t(unsigned long,

Quoted 8 lines

+            sc->nr_to_scan, atomic_long_read(&allocated_pages)); +    while (freed < to_scan) {
+        freed += ttm_pool_shrink(&sc->nr_scanned);
+        to_scan = min_t(unsigned long,
+                to_scan, atomic_long_read(&allocated_pages)); +    }
   -    return num_freed;
+    return sc->nr_scanned ? freed : SHRINK_STOP;

That again doesn't make sense. That we only find pool types which don't have pages doesn't mean we have scanned them.

As far as I can see the existing code was correct after all.

Quoted 14 lines

   }
     /* Return the number of pages available or SHRINK_EMPTY if we have none */

drivers/gpu/drm/ttm/ttm_pool.c +4 -1
@@ -1266,7 +1271,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),

drivers/gpu/drm/ttm/ttm_pool.c int ttm_pool_mgr_init(unsigned long num_pages) · line 1334

Quoted 3 lines
drivers/gpu/drm/ttm/ttm_pool.c +1 -0
@@ -1324,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 = (1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS;

Since we install only one global shrinker for all pool types, which might contain everything from 1 page till 512 pages, this seems to not make sense at all either.

What exactly are you trying to do here?

Regards,
Christian.

Quoted 2 lines
drivers/gpu/drm/ttm/ttm_pool.c +0 -0
        mm_shrinker->seeks = 1;
          shrinker_register(mm_shrinker);