* [RFC PATCH v2] drm/ttm: reclaim excess pooled pages after a fixed delay
@ 2026-11-25 1:56 Eduardo P. Gomez
2026-11-25 2:10 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Eduardo P. Gomez @ 2026-11-25 1:56 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 while the pool stays
below its hard limit and the memory-pressure shrinker is not invoked.
On a 32 GiB Intel Xe desktop, this left several GiB in the pool after
the GPU workloads that returned them had exited.
When a return leaves a NUMA node above its reserve, queue reclaim
after a fixed delay of five seconds by default. Further returns
leave the pending deadline unchanged so ongoing activity cannot
keep postponing reclaim. Retain a default target of 128 MiB per node,
clamped to the node limit. Reclaim under memory pressure can still
go below that target.
Bound each delayed pass by the initial excess and consecutive scans
without progress. Requeue if excess remains. In ttm_pool_free(),
use the same counter snapshot to check the node limit and calculate
the excess so concurrent reclaim cannot underflow the shrink request.
In QEMU, an initial 768 MiB pool fell to 128–160 MiB while a second
workload continued cycling 32 MiB allocations and settled at
128 MiB after those returns stopped.
Signed-off-by: Eduardo P. Gomez <eduardoprocopiogomez@gmail.com>
---
Changes since v1:
- Combine the earlier two-patch RFC into one patch.
- Start a fixed five-second delay once the pool is above its reserve.
- Bound scans without progress and calculate the hard-limit excess from
a single counter read.
AI assistance: OpenAI Codex helped analyze and implement the TTM
reclaim changes and run the QEMU reproducer.
drivers/gpu/drm/ttm/ttm_pool.c | 104 +++++++++++++++++++++++++++++----
1 file changed, 94 insertions(+), 10 deletions(-)
diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index 1bf37023f..09766496d 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];
@@ -322,6 +337,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 +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(nid);
}
static enum lru_status take_one_from_lru(struct list_head *item,
@@ -452,7 +485,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 +498,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 +511,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 +1056,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 +1388,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 +1571,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 +1633,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 v2] drm/ttm: reclaim excess pooled pages after a fixed delay
2026-11-25 1:56 [RFC PATCH v2] drm/ttm: reclaim excess pooled pages after a fixed delay Eduardo P. Gomez
@ 2026-11-25 2:10 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25 2:10 UTC (permalink / raw)
To: Eduardo P. Gomez, dri-devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] drm/ttm: unbatched set_pages_wb() IPI storm in
ttm_pool_shrink_idle()
--
commit e91f541d87136a66bf36718752a8777d95e748f4
Author: Eduardo P. Gomez <eduardoprocopiogomez@gmail.com>
drm/ttm: reclaim excess pooled pages after a fixed delay
This commit schedules delayed work to reclaim pooled TTM pages above a
per-node reserve after a fixed five-second window and uses a single
counter read in ttm_pool_free() when checking the node limit.
> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 1bf37023fed66..09766496d8810 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
[ ... ]
> @@ -473,6 +511,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]
Can draining the entire excess in one ttm_pool_shrink_idle() pass cause an
unbatched set_pages_wb() TLB shootdown storm on x86?
ttm_pool_shrink() -> ttm_pool_dispose_list() calls ttm_pool_free_page()
once per isolated entry, which restores write-back caching individually:
drivers/gpu/drm/ttm/ttm_pool.c:ttm_pool_free_page() {
...
#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
...
}
Each set_pages_wb() call takes cpa_lock and broadcasts a synchronous
cross-CPU TLB shootdown IPI, so reclaiming 1 GiB of order-0 write-combined
pages in one pass issues 262,144 individual set_pages_wb() calls while also
rotating through all empty shrinker_list entries after every 32-entry
batch. Because ttm_pool_note_return() uses a fixed five-second timer that
is not deferred by ongoing pool activity, an active GPU workload cycling
more than 128 MiB of WC/UC buffers will repeatedly pay this unbatched
set_pages_wb() shootdown storm and subsequent set_pages_array_wc() or
set_pages_array_uc() allocation shootdowns every five seconds.
Batching the caching transition across dispose list entries with
set_pages_array_wb() (as ttm_pool_backup() does) would avoid per-page CPA
shootdowns.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125015613.127297-1-eduardoprocopiogomez@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 2:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 1:56 [RFC PATCH v2] drm/ttm: reclaim excess pooled pages after a fixed delay Eduardo P. Gomez
2026-11-25 2:10 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox