Pages returned to the TTM pool can remain cached below its hard
limit when there is no memory pressure. On a 32 GiB Intel Xe
desktop, this left several GiB cached after the GPU workload exited.

Queue reclaim when a return leaves a node above its reserve. Use a
fixed five-second delay by default so further returns cannot
postpone pending reclaim. Retain a 128 MiB target per NUMA node,
clamped to the node limit. The memory-pressure shrinker can still
reclaim below that target.

Limit each delayed pass by the initial excess and consecutive scans
without progress. Requeue if excess remains. In ttm_pool_free(),
use a single counter snapshot for the hard-limit comparison and
subtraction so concurrent reclaim cannot underflow the request.

On x86, reclaim can restore write-back caching and flush remote TLBs
once for every order-0 WC or UC page. Restore write-back for up to 32
eligible pages per call before freeing them. Use the individual path
if the array conversion fails or a page is ineligible.

In a two-vCPU QEMU guest, reclaiming 128 MiB of 4 KiB WC pages took
1,024 write-back calls and 2,048 CPA TLB callbacks, down from 32,768
calls and 65,536 callbacks without batching. A separate run drained
a 768 MiB burst while 32 MiB allocations and returns continued.

Signed-off-by: Eduardo P. Gomez <[email protected]>
---
Changes since v2:
- Batch write-back conversion for order-0 WC/UC pages on x86.
- Verify the reduction in CPA TLB callbacks in a two-vCPU QEMU guest.

v2: 
https://lore.kernel.org/r/[email protected]/


 drivers/gpu/drm/ttm/ttm_pool.c | 157 ++++++++++++++++++++++++++++++---
 1 file changed, 144 insertions(+), 13 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index 1bf37023f..3c1457759 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -36,7 +36,10 @@
 #include <linux/dma-mapping.h>
 #include <linux/debugfs.h>
 #include <linux/highmem.h>
+#include <linux/jiffies.h>
 #include <linux/sched/mm.h>
+#include <linux/sizes.h>
+#include <linux/workqueue.h>
 
 #ifdef CONFIG_X86
 #include <asm/set_memory.h>
@@ -134,8 +137,20 @@ 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 reclaim delay 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 delayed 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;
+static unsigned long pool_idle_delay;
 
 static struct ttm_pool_type global_write_combined[NR_PAGE_ORDERS];
 static struct ttm_pool_type global_uncached[NR_PAGE_ORDERS];
@@ -243,9 +258,6 @@ static void ttm_pool_free_page(struct ttm_pool *pool, enum 
ttm_caching caching,
        void *vaddr;
 
 #ifdef CONFIG_X86
-       /* We don't care that set_pages_wb is inefficient here. This is only
-        * used when we have to shrink and CPU overhead is irrelevant then.
-        */
        if (caching != ttm_cached && !PageHighMem(p))
                set_pages_wb(p, 1 << order);
 #endif
@@ -322,6 +334,21 @@ static void ttm_pool_unmap(struct ttm_pool *pool, 
dma_addr_t dma_addr,
                       DMA_BIDIRECTIONAL);
 }
 
+/* Start one reclaim window for the shared pool when a node exceeds its 
reserve. */
+static void ttm_pool_note_return(int nid)
+{
+       unsigned long reserve = min(page_pool_idle_reserve, 
pool_node_limit[nid]);
+       long pages;
+
+       if (!pool_idle_delay || delayed_work_pending(&pool_idle_work))
+               return;
+
+       pages = atomic_long_read(&allocated_pages[nid]);
+       if (pages > 0 && pages > reserve)
+               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 +362,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(nid);
 }
 
 static enum lru_status take_one_from_lru(struct list_head *item,
@@ -398,9 +428,59 @@ static enum lru_status pool_move_to_dispose_list(struct 
list_head *item,
        return LRU_REMOVED;
 }
 
+#ifdef CONFIG_X86
+/* Restore one batch before returning its pages to the allocator. */
+static void ttm_pool_free_wb_batch(struct ttm_pool_type *pt,
+                                  struct page **pages, unsigned int nr)
+{
+       bool restored = !set_pages_array_wb(pages, nr);
+       unsigned int i;
+
+       for (i = 0; i < nr; i++) {
+               struct page *p = pages[i];
+
+               atomic_long_dec(&allocated_pages[page_to_nid(p)]);
+               if (restored)
+                       __free_pages_gpu_account(p, 0, true);
+               else
+                       ttm_pool_free_page(NULL, pt->caching, 0, p, true);
+       }
+}
+
+/* Batch normal order-0 pages using a bounded array without allocating memory. 
*/
+static void ttm_pool_dispose_wb(struct ttm_pool_type *pt,
+                              struct list_head *dispose)
+{
+       struct page *pages[32];
+       struct page *p, *next;
+       unsigned int nr = 0;
+
+       list_for_each_entry_safe(p, next, dispose, lru) {
+               if (PageHighMem(p))
+                       continue;
+
+               list_del_init(&p->lru);
+               pages[nr++] = p;
+               if (nr == ARRAY_SIZE(pages)) {
+                       ttm_pool_free_wb_batch(pt, pages, nr);
+                       nr = 0;
+               }
+       }
+
+       if (nr)
+               ttm_pool_free_wb_batch(pt, pages, nr);
+}
+#endif
+
 static void ttm_pool_dispose_list(struct ttm_pool_type *pt,
                                  struct list_head *dispose)
 {
+#ifdef CONFIG_X86
+       if (!pt->order && pt->caching != ttm_cached &&
+           (!pt->pool || !ttm_pool_uses_dma_alloc(pt->pool)))
+               ttm_pool_dispose_wb(pt, dispose);
+#endif
+
        while (!list_empty(dispose)) {
                struct page *p;
                p = list_first_entry(dispose, struct page, lru);
@@ -452,7 +532,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;
@@ -464,6 +545,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;
 
@@ -473,6 +558,46 @@ static unsigned int ttm_pool_shrink(int nid, unsigned long 
num_to_free)
        return num_pages;
 }
 
+/* Trim excess pooled pages when the fixed reclaim window expires. */
+static void ttm_pool_shrink_idle(struct work_struct *work)
+{
+       const unsigned long batch = 1UL << (MAX_PAGE_ORDER / 2);
+       size_t nr_types;
+       int nid;
+
+       spin_lock(&shrinker_lock);
+       nr_types = list_count_nodes(&shrinker_list);
+       spin_unlock(&shrinker_lock);
+
+       for_each_node_state(nid, N_MEMORY) {
+               unsigned long reserve = min(page_pool_idle_reserve,
+                                           pool_node_limit[nid]);
+               long pages = atomic_long_read(&allocated_pages[nid]);
+               unsigned long budget;
+               size_t empty = 0;
+
+               if (pages <= 0 || pages <= reserve)
+                       continue;
+
+               /* Bound freed pages and scans that make no progress. */
+               budget = pages - reserve;
+               while (budget && empty < nr_types && pages > 0 && pages > 
reserve) {
+                       unsigned long freed;
+
+                       freed = ttm_pool_shrink(nid, batch, min(budget, pages - 
reserve));
+                       budget -= min(budget, freed);
+                       empty = freed ? 0 : empty + 1;
+                       cond_resched();
+                       pages = atomic_long_read(&allocated_pages[nid]);
+               }
+
+               /* Concurrent returns or scans of empty lists can leave excess. 
*/
+               if (pages > 0 && pages > reserve)
+                       queue_delayed_work(system_dfl_long_wq, &pool_idle_work,
+                                          pool_idle_delay);
+       }
+}
+
 /* Return the allocation order based for a page */
 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
 {
@@ -978,13 +1103,14 @@ 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)
 {
        int nid = ttm_pool_nid(pool);
+       unsigned long limit = pool_node_limit[nid];
+       long pages;
 
        ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
 
-       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);
-       }
+       while ((pages = atomic_long_read(&allocated_pages[nid])) > 0 &&
+              pages > limit)
+               ttm_pool_shrink(nid, pages - limit, 0);
 }
 EXPORT_SYMBOL(ttm_pool_free);
 
@@ -1309,7 +1435,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]));
 
@@ -1492,8 +1618,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 +1680,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]);

base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
-- 
2.55.0

Reply via email to