Below the pool limit, TTM can retain returned pages after a workload
exits. Use delayed work to drain the pool through the existing per-node
shrink path after five seconds without new page returns.

Update page accounting before publishing pages to the pool so
concurrent reclaim sees consistent counts. Cancel the worker before
tearing down the pool. Set ttm.page_pool_idle_ms to zero to disable idle
reclaim.

In QEMU, 768 MiB remained pooled after 60 seconds without the patch and
had returned to the allocator by the eight-second sample with it.

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

diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index 1bf37023f..6af8a9c89 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -36,7 +36,9 @@
 #include <linux/dma-mapping.h>
 #include <linux/debugfs.h>
 #include <linux/highmem.h>
+#include <linux/jiffies.h>
 #include <linux/sched/mm.h>
+#include <linux/workqueue.h>
 
 #ifdef CONFIG_X86
 #include <asm/set_memory.h>
@@ -134,8 +136,17 @@ static unsigned long page_pool_size;
 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool per 
NUMA node");
 module_param(page_pool_size, ulong, 0644);
 
+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 pool_node_limit[MAX_NUMNODES];
 static atomic_long_t allocated_pages[MAX_NUMNODES];
+static struct delayed_work pool_idle_work;
+static DEFINE_SPINLOCK(pool_idle_lock);
+static unsigned long pool_idle_delay;
+static unsigned long pool_last_return;
 
 static struct ttm_pool_type global_write_combined[NR_PAGE_ORDERS];
 static struct ttm_pool_type global_uncached[NR_PAGE_ORDERS];
@@ -322,6 +333,25 @@ static void ttm_pool_unmap(struct ttm_pool *pool, 
dma_addr_t dma_addr,
                       DMA_BIDIRECTIONAL);
 }
 
+/* Arm idle reclaim once and let the worker check for more recent returns. */
+static void ttm_pool_note_return(void)
+{
+       unsigned long now;
+
+       if (!pool_idle_delay)
+               return;
+
+       now = jiffies;
+       if (READ_ONCE(pool_last_return) != now) {
+               spin_lock(&pool_idle_lock);
+               WRITE_ONCE(pool_last_return, jiffies);
+               spin_unlock(&pool_idle_lock);
+       }
+       if (!delayed_work_pending(&pool_idle_work))
+               queue_delayed_work(system_dfl_long_wq, &pool_idle_work,
+                                  pool_idle_delay);
+}
+
 /* Give pages into a specific pool_type */
 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
 {
@@ -335,14 +365,17 @@ static void ttm_pool_type_give(struct ttm_pool_type *pt, 
struct page *p)
                        clear_page(page_address(p + i));
        }
 
+       /* Account for the pages before a concurrent shrinker can take them. */
+       atomic_long_add(num_pages, &allocated_pages[nid]);
+       mod_lruvec_page_state(p, NR_GPU_ACTIVE, -num_pages);
+       mod_lruvec_page_state(p, NR_GPU_RECLAIM, num_pages);
+
        INIT_LIST_HEAD(&p->lru);
        rcu_read_lock();
        list_lru_add(&pt->pages, &p->lru, nid, NULL);
        rcu_read_unlock();
 
-       atomic_long_add(num_pages, &allocated_pages[nid]);
-       mod_lruvec_page_state(p, NR_GPU_ACTIVE, -num_pages);
-       mod_lruvec_page_state(p, NR_GPU_RECLAIM, num_pages);
+       ttm_pool_note_return();
 }
 
 static enum lru_status take_one_from_lru(struct list_head *item,
@@ -473,6 +506,30 @@ static unsigned int ttm_pool_shrink(int nid, unsigned long 
num_to_free)
        return num_pages;
 }
 
+/* Return unused pool pages after the last return has aged out. */
+static void ttm_pool_shrink_idle(struct work_struct *work)
+{
+       const unsigned long batch = 1UL << (MAX_PAGE_ORDER / 2);
+       int nid;
+
+       for_each_node_state(nid, N_MEMORY) {
+               while (atomic_long_read(&allocated_pages[nid]) > 0) {
+                       unsigned long now = jiffies;
+                       unsigned long expires = READ_ONCE(pool_last_return) +
+                                               pool_idle_delay;
+
+                       if (time_before(now, expires)) {
+                               queue_delayed_work(system_dfl_long_wq, 
&pool_idle_work,
+                                                  expires - now);
+                               return;
+                       }
+
+                       ttm_pool_shrink(nid, batch);
+                       cond_resched();
+               }
+       }
+}
+
 /* Return the allocation order based for a page */
 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
 {
@@ -1492,8 +1549,11 @@ static inline u64 ttm_get_node_memory_size(int nid)
 int ttm_pool_mgr_init(unsigned long num_pages)
 {
        unsigned int i;
-
        int nid;
+
+       pool_idle_delay = msecs_to_jiffies(page_pool_idle_ms);
+       INIT_DELAYED_WORK(&pool_idle_work, ttm_pool_shrink_idle);
+
        for_each_node(nid) {
                if (!page_pool_size) {
                        u64 node_size = ttm_get_node_memory_size(nid);
@@ -1551,6 +1611,8 @@ void ttm_pool_mgr_fini(void)
 {
        unsigned int i;
 
+       disable_delayed_work_sync(&pool_idle_work);
+
        for (i = 0; i < NR_PAGE_ORDERS; ++i) {
                ttm_pool_type_fini(&global_write_combined[i]);
                ttm_pool_type_fini(&global_uncached[i]);
-- 
2.55.0

Reply via email to