dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH v3] drm/ttm: reclaim excess pooled pages with batched write-back
@ 2026-11-25  2:46 Eduardo P. Gomez
  2026-11-25  3:00 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Eduardo P. Gomez @ 2026-11-25  2:46 UTC (permalink / raw)
  To: dri-devel
  Cc: Natalie Vock, Arun Pravin, Matthew Auld, Matthew Brost,
	Thomas Hellström, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter, Eduardo P. Gomez

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 <eduardoprocopiogomez@gmail.com>
---
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/20261125015613.127297-1-eduardoprocopiogomez@gmail.com/


 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [RFC PATCH v3] drm/ttm: reclaim excess pooled pages with batched write-back
  2026-11-25  2:46 [RFC PATCH v3] drm/ttm: reclaim excess pooled pages with batched write-back Eduardo P. Gomez
@ 2026-11-25  3:00 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25  3:00 UTC (permalink / raw)
  To: Eduardo P. Gomez, dri-devel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/ttm: unbounded busy-spin in ttm_pool_free() and
  ttm_pool_shrinker_scan()
- [Medium] drm/ttm: unscaled batch in ttm_pool_shrink_idle() biases
  high-order reclaim
--

commit a0eba1b6041ea9cb1a00634b848d2c76f8f265d4
Author: Eduardo P. Gomez <eduardoprocopiogomez@gmail.com>

drm/ttm: reclaim excess pooled pages with batched write-back

This commit adds a delayed work item to reclaim TTM pooled pages above a
per-node reserve and batches write-back caching conversions for order-0
WC and UC pages on x86.

> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 1bf37023fed66..3c14577593eb2 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c

[ ... ]

> @@ -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]);
> +		}

[Severity: Medium]
Should num_to_free in ttm_pool_shrink() also be scaled down by pt->order, or
should batch be expressed in base pages?

ttm_pool_shrink_idle() passes a fixed batch of 32 LRU entries to
ttm_pool_shrink(), which scales page_budget by pt->order but leaves
num_to_free unscaled unless it exceeds page_budget.  Each rotation across
shrinker_list therefore drains at most 32 base pages (128 KiB) from an
order-0 pool before moving it to the tail of all 44+ pool types, but up to
32 compound allocations (64 MiB at order-9 or 128 MiB at order-10) from a
high-order pool.  This disproportionately evicts high-order pages from the
128 MiB idle reserve and forces 43 empty list_lru walks and lock cycles for
every 32 pages freed from a single active order-0 pool.

[ ... ]

> @@ -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]));

[Severity: Medium]
Can ttm_pool_free() and ttm_pool_shrinker_scan() busy-spin when the
remaining counted pages on a node are in flight?

This patch moves atomic_long_add(&allocated_pages[nid]) before
list_lru_add() in ttm_pool_type_give(), and in ttm_pool_free_wb_batch()
keeps pages isolated from pt->pages across set_pages_array_wb() (which takes
cpa_lock and runs cross-CPU TLB shootdowns) before decrementing
allocated_pages[nid].

CPU0 (pool_idle_work)             CPU1
ttm_pool_shrink_idle()
  ttm_pool_shrink()
    list_lru_walk_node()
    /* pt->pages now empty */
    ttm_pool_free_wb_batch()
      set_pages_array_wb()
                                  ttm_pool_free()
                                    while (allocated_pages[nid] > limit)
                                      ttm_pool_shrink() -> 0
                                      /* spins on shrinker_lock */
      atomic_long_dec(...)

Unlike ttm_pool_shrink_idle(), neither ttm_pool_free() nor
ttm_pool_shrinker_scan() bounds consecutive empty scans with
empty < nr_types or calls cond_resched().  While in-flight pages keep
allocated_pages[nid] above limit (or above 0 in the shrinker), a concurrent
caller spins through shrinker_list acquiring and releasing pool_shrink_rwsem
and shrinker_lock, which can cause severe lock contention or soft-lockups if
the thread holding the in-flight batch is preempted.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125024630.257843-1-eduardoprocopiogomez@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25  3:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  2:46 [RFC PATCH v3] drm/ttm: reclaim excess pooled pages with batched write-back Eduardo P. Gomez
2026-11-25  3:00 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox