Draining the idle pool makes the next burst pay the cold allocation
cost again. Retain a configurable 128 MiB reserve per NUMA node, capped
by the node's existing pool limit. Reclaim under memory pressure can
still use the reserve. A zero reserve restores the full idle drain.

In QEMU, idle reclaim reduced the pool from 768 MiB to 128 MiB, leaving
pages that the next allocation could reuse after the timeout.

Signed-off-by: Eduardo P. Gomez <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index 6af8a9c89..3aad264fa 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -38,6 +38,7 @@
 #include <linux/highmem.h>
 #include <linux/jiffies.h>
 #include <linux/sched/mm.h>
+#include <linux/sizes.h>
 #include <linux/workqueue.h>
 
 #ifdef CONFIG_X86
@@ -141,6 +142,11 @@ static unsigned int page_pool_idle_ms = 5000;
 MODULE_PARM_DESC(page_pool_idle_ms, "TTM page pool idle timeout in ms (0 
disables it)");
 module_param(page_pool_idle_ms, uint, 0444);
 
+static unsigned long page_pool_idle_reserve = SZ_128M / PAGE_SIZE;
+
+MODULE_PARM_DESC(page_pool_idle_reserve, "Target pages to retain per NUMA node 
after idle reclaim");
+module_param(page_pool_idle_reserve, ulong, 0444);
+
 static unsigned long pool_node_limit[MAX_NUMNODES];
 static atomic_long_t allocated_pages[MAX_NUMNODES];
 static struct delayed_work pool_idle_work;
@@ -485,7 +491,8 @@ static struct ttm_pool_type *ttm_pool_select_type(struct 
ttm_pool *pool,
 }
 
 /* Free pages using the per-node shrinker list */
-static unsigned int ttm_pool_shrink(int nid, unsigned long num_to_free)
+static unsigned int ttm_pool_shrink(int nid, unsigned long num_to_free,
+                                   unsigned long page_budget)
 {
        LIST_HEAD(dispose);
        struct ttm_pool_type *pt;
@@ -497,6 +504,10 @@ static unsigned int ttm_pool_shrink(int nid, unsigned long 
num_to_free)
        list_move_tail(&pt->shrinker_list, &shrinker_list);
        spin_unlock(&shrinker_lock);
 
+       /* A list entry represents a whole compound allocation. */
+       if (page_budget)
+               num_to_free = min(num_to_free, ((page_budget - 1) >> pt->order) 
+ 1);
+
        num_pages = list_lru_walk_node(&pt->pages, nid, 
pool_move_to_dispose_list, &dispose, &num_to_free);
        num_pages *= 1 << pt->order;
 
@@ -513,7 +524,12 @@ static void ttm_pool_shrink_idle(struct work_struct *work)
        int nid;
 
        for_each_node_state(nid, N_MEMORY) {
-               while (atomic_long_read(&allocated_pages[nid]) > 0) {
+               unsigned long reserve = min(page_pool_idle_reserve,
+                                           pool_node_limit[nid]);
+               long pages;
+
+               while ((pages = atomic_long_read(&allocated_pages[nid])) > 0 &&
+                      pages > reserve) {
                        unsigned long now = jiffies;
                        unsigned long expires = READ_ONCE(pool_last_return) +
                                                pool_idle_delay;
@@ -524,7 +540,7 @@ static void ttm_pool_shrink_idle(struct work_struct *work)
                                return;
                        }
 
-                       ttm_pool_shrink(nid, batch);
+                       ttm_pool_shrink(nid, batch, pages - reserve);
                        cond_resched();
                }
        }
@@ -1040,7 +1056,7 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt 
*tt)
 
        while (atomic_long_read(&allocated_pages[nid]) > pool_node_limit[nid]) {
                unsigned long diff = atomic_long_read(&allocated_pages[nid]) - 
pool_node_limit[nid];
-               ttm_pool_shrink(nid, diff);
+               ttm_pool_shrink(nid, diff, 0);
        }
 }
 EXPORT_SYMBOL(ttm_pool_free);
@@ -1366,7 +1382,7 @@ static unsigned long ttm_pool_shrinker_scan(struct 
shrinker *shrink,
        unsigned long num_freed = 0;
 
        do
-               num_freed += ttm_pool_shrink(sc->nid, sc->nr_to_scan);
+               num_freed += ttm_pool_shrink(sc->nid, sc->nr_to_scan, 0);
        while (num_freed < sc->nr_to_scan &&
               atomic_long_read(&allocated_pages[sc->nid]));
 
-- 
2.55.0

Reply via email to