* [RFC PATCH 1/2] drm/ttm: reclaim pooled pages after a period of inactivity
2026-11-24 22:28 [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention Eduardo P. Gomez
@ 2026-11-24 22:28 ` Eduardo P. Gomez
2026-11-25 9:04 ` sashiko-bot
2026-11-24 22:28 ` [RFC PATCH 2/2] drm/ttm: retain a reserve during idle pool reclaim Eduardo P. Gomez
2026-11-25 10:14 ` [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention Thomas Hellström
2 siblings, 1 reply; 11+ messages in thread
From: Eduardo P. Gomez @ 2026-11-24 22:28 UTC (permalink / raw)
To: nat, arunpravin.paneerselvam
Cc: dri-devel, matthew.auld, matthew.brost, thomas.hellstrom,
Eduardo P. Gomez
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 <eduardoprocopiogomez@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [RFC PATCH 1/2] drm/ttm: reclaim pooled pages after a period of inactivity
2026-11-24 22:28 ` [RFC PATCH 1/2] drm/ttm: reclaim pooled pages after a period of inactivity Eduardo P. Gomez
@ 2026-11-25 9:04 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-25 9:04 UTC (permalink / raw)
To: Eduardo P. Gomez; +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/ttm: race in ttm_pool_type_give() and spin in
ttm_pool_shrink_idle()
- [High] drm/ttm: unbatched set_pages_wb() IPI storm in
ttm_pool_shrink_idle()
--
commit be4ff4c980b2546ac4ff032bfa520f015175f024
Author: Eduardo P. Gomez <eduardoprocopiogomez@gmail.com>
drm/ttm: reclaim pooled pages after a period of inactivity
This commit adds a delayed work item to drain pooled TTM pages through
ttm_pool_shrink() after five seconds without page returns, and moves page
accounting in ttm_pool_type_give() ahead of list_lru_add().
> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 1bf37023fed66..6af8a9c894b4e 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
[ ... ]
> @@ -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();
> }
[Severity: High]
Can ttm_pool_shrink_idle() immediately reclaim a page that is being
returned here, or spin when allocated_pages[nid] is ahead of the LRU?
Because allocated_pages[nid] and pt->pages are updated before
ttm_pool_note_return() refreshes pool_last_return, an already-running idle
worker sees the new page while pool_last_return still holds the old
expired timestamp:
CPU0 CPU1 (kworker)
ttm_pool_type_give() ttm_pool_shrink_idle()
atomic_long_add(...,
&allocated_pages[nid])
list_lru_add(&pt->pages, ...)
atomic_long_read(&allocated_pages[nid])
expires = READ_ONCE(pool_last_return)
+ pool_idle_delay
time_before(now, expires) == false
ttm_pool_shrink(nid, batch)
<-- reclaims just-returned page
ttm_pool_note_return()
WRITE_ONCE(pool_last_return, jiffies)
In addition, ttm_pool_shrink_idle() loops for as long as
atomic_long_read(&allocated_pages[nid]) > 0 without checking whether
ttm_pool_shrink() freed any pages. allocated_pages[nid] is positive while
no pages remain on any pt->pages LRU both between atomic_long_add() and
list_lru_add() above (with preemption enabled) and while concurrent
ttm_pool_dispose_list() calls are freeing isolated pages before
decrementing allocated_pages[nid]. During those windows,
ttm_pool_shrink() returns 0 on every call and ttm_pool_shrink_idle() spins
acquiring pool_shrink_rwsem and shrinker_lock; if direct reclaim preempts
ttm_pool_type_give() between atomic_long_add() and list_lru_add(),
ttm_pool_shrinker_scan() can also spin without cond_resched().
Updating pool_last_return before publishing the page and breaking out of
the shrink loop when a full pass over shrinker_list frees no pages would
avoid both issues.
[ ... ]
> @@ -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();
> + }
> + }
> +}
[Severity: High]
Does draining the pool through ttm_pool_shrink() trigger an IPI storm of
unbatched set_pages_wb() calls on x86?
ttm_pool_shrink() frees pages one entry at a time via
ttm_pool_dispose_list() -> ttm_pool_free_page():
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
...
}
Unlike ttm_pool_backup(), which batches page-attribute changes with
set_pages_array_wb(), each set_pages_wb() call here issues a synchronous
cross-CPU TLB shootdown IPI to every online CPU for a single page block.
Because ttm_pool_shrink() also inspects and rotates only one ttm_pool_type
per call across the global and per-device entries on shrinker_list,
draining a single populated pool type walks dozens of empty LRUs under
pool_shrink_rwsem and shrinker_lock for every 32-entry batch. Later
commit "drm/ttm: retain a reserve during idle pool reclaim" in this series
keeps a 128 MiB per-node reserve, but any pooled WC/UC pages above that
threshold still hit this path five seconds after the last return.
Also, pool_last_return is only updated in ttm_pool_type_give() and not in
ttm_pool_type_take(), so ttm_pool_shrink_idle() will drain the pool even
while a workload is actively taking pages via ttm_pool_alloc().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124222812.511867-2-eduardoprocopiogomez@gmail.com?part=1
^ permalink raw reply [flat|nested] 11+ messages in thread
* [RFC PATCH 2/2] drm/ttm: retain a reserve during idle pool reclaim
2026-11-24 22:28 [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention Eduardo P. Gomez
2026-11-24 22:28 ` [RFC PATCH 1/2] drm/ttm: reclaim pooled pages after a period of inactivity Eduardo P. Gomez
@ 2026-11-24 22:28 ` Eduardo P. Gomez
2026-11-25 9:00 ` sashiko-bot
2026-11-25 10:14 ` [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention Thomas Hellström
2 siblings, 1 reply; 11+ messages in thread
From: Eduardo P. Gomez @ 2026-11-24 22:28 UTC (permalink / raw)
To: nat, arunpravin.paneerselvam
Cc: dri-devel, matthew.auld, matthew.brost, thomas.hellstrom,
Eduardo P. Gomez
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 <eduardoprocopiogomez@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 11+ messages in thread* Re: [RFC PATCH 2/2] drm/ttm: retain a reserve during idle pool reclaim
2026-11-24 22:28 ` [RFC PATCH 2/2] drm/ttm: retain a reserve during idle pool reclaim Eduardo P. Gomez
@ 2026-11-25 9:00 ` sashiko-bot
0 siblings, 0 replies; 11+ messages in thread
From: sashiko-bot @ 2026-11-25 9:00 UTC (permalink / raw)
To: Eduardo P. Gomez; +Cc: dri-devel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] drm/ttm: unit mismatch between num_to_free and page_budget in
ttm_pool_shrink()
- [Medium] drm/ttm: reserve underrun and spin in ttm_pool_shrink_idle()
on in-flight dispose
--
commit 4e83ddb6cbf7cc0eb4ace5ed5228122c85cb44a0
Author: Eduardo P. Gomez <eduardoprocopiogomez@gmail.com>
drm/ttm: retain a reserve during idle pool reclaim
This commit adds a configurable per-NUMA-node page reserve that is
retained during idle TTM page pool reclaim.
> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 6af8a9c894b4e..3aad264fa11f8 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
[ ... ]
> @@ -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;
[Severity: Medium]
Should num_to_free also be converted from base pages to pt->order compound
list entries?
page_budget is shifted by pt->order, but num_to_free is passed directly to
list_lru_walk_node() as a count of compound entries. In
ttm_pool_shrink_idle(), batch is 32, so when pt->order is 10 and
pages - reserve is large, a single ttm_pool_shrink() call can isolate and
free up to 32 order-10 allocations (128 MiB) under pool_shrink_rwsem
before re-checking pool_last_return or calling cond_resched(). Meanwhile,
ttm_pool_free() and ttm_pool_shrinker_scan() pass base-page counts (diff
and sc->nr_to_scan) as num_to_free with page_budget = 0, over-shrinking
high-order pools by a factor of 1 << pt->order.
Converting num_to_free from base pages to compound entries inside
ttm_pool_shrink() and passing min(batch, pages - reserve) from
ttm_pool_shrink_idle() would keep all callers in base-page units without
needing a separate page_budget parameter.
> @@ -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();
> }
> }
[Severity: Medium]
Can ttm_pool_shrink_idle() drain the pool below reserve and busy-spin when
another thread is concurrently disposing pages?
list_lru_walk_node() isolates a batch of entries from pt->pages onto a
local dispose list, while ttm_pool_dispose_list() decrements
allocated_pages[nid] one entry at a time across ttm_pool_free_page()
calls:
CPU0 (ttm_pool_shrinker_scan) CPU1 (ttm_pool_shrink_idle)
ttm_pool_shrink()
list_lru_walk_node()
/* isolates pages to dispose */
pages = atomic_long_read(&allocated_pages[nid])
/* still includes CPU0's isolated pages */
ttm_pool_shrink(nid, batch, pages - reserve)
/* drains pt->pages below reserve */
ttm_pool_dispose_list()
atomic_long_sub(...)
ttm_pool_free_page(...)
While CPU0 is freeing its dispose list (which on x86 issues per-entry
set_pages_wb() TLB shootdowns for WC/UC pages), allocated_pages[nid] stays
elevated above the number of pages still on pt->pages. CPU1 computes
pages - reserve from that stale total and drains remaining pooled pages
below reserve. If all pt->pages on the node become empty while CPU0's
in-flight dispose list still holds more than reserve pages, CPU1 loops
with ttm_pool_shrink() returning 0, repeatedly rotating shrinker_list
under pool_shrink_rwsem and shrinker_lock until CPU0 decrements
allocated_pages[nid] to or below reserve.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124222812.511867-2-eduardoprocopiogomez@gmail.com?part=2
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention
2026-11-24 22:28 [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention Eduardo P. Gomez
2026-11-24 22:28 ` [RFC PATCH 1/2] drm/ttm: reclaim pooled pages after a period of inactivity Eduardo P. Gomez
2026-11-24 22:28 ` [RFC PATCH 2/2] drm/ttm: retain a reserve during idle pool reclaim Eduardo P. Gomez
@ 2026-11-25 10:14 ` Thomas Hellström
2026-11-25 13:27 ` Eduardo P. Gomez
2 siblings, 1 reply; 11+ messages in thread
From: Thomas Hellström @ 2026-11-25 10:14 UTC (permalink / raw)
To: Eduardo P. Gomez, nat, arunpravin.paneerselvam
Cc: dri-devel, matthew.auld, matthew.brost
Hi Eduardo,
On Tue, 2026-11-24 at 19:28 -0300, Eduardo P. Gomez wrote:
> I started looking into this after my Intel Xe desktop with 32 GiB of
> RAM
> would eventually freeze following repeated graphics allocations (A
> test
> program allocated around 12 GiB without freeing up). In a
> separate test, a standalone kernel module exercised the TTM page pool
> directly. On unpatched 7.4-rc4 in a single-node QEMU guest, 768 MiB
> remained in the pool 60s after the module returned its pages.
This series fails to explain why this is necessary? That is, why
doesn't the TTM pool shrinker reclaim the pages on memory pressure,
which is the way this is intended to work?
Thanks,
Thomas
>
> Patch 1 adds a 5s timeout after the last page return. It can
> stand on its own and drains the idle pool. In QEMU, the pool was
> empty
> at the eight-second sample. The trade-off is that the next burst may
> have to allocate and prepare those pages again.
>
> Patch 2 builds on the timeout and keeps a 128 MiB reserve per NUMA
> node
> to reduce that trade-off. With both patches, the same 768 MiB test
> left
> 128 MiB in the pool at the 8s and 60s samples. The
> shrinker can still reclaim the reserve under memory pressure.
>
> In a separate QEMU benchmark with a 750ms timeout, the median time to
> allocate 128 MiB after expiry was 185ms after a full drain and 0.45ms
> with the reserve. A 768 MiB request took 1.13s after a full drain and
> 0.95s with the reserve. The unpatched kernel kept the entire pool and
> reused those 768 MiB in about 2ms. These tests measure idle retention
> and allocation latency in QEMU.
>
> The test files can be downloaded at
> https://codeberg.org/EduApps-CDG/linux-ttm-pool.
> It includes 2 C files (one for test and another for benchmark), my
> logs,
> some build/test helping bash scripts and the compiled binary I'm
> using
> on my machine right now.
>
> Feedback and change requests are welcome.
>
> Eduardo P. Gomez (2):
> drm/ttm: reclaim pooled pages after a period of inactivity
> drm/ttm: retain a reserve during idle pool reclaim
>
> drivers/gpu/drm/ttm/ttm_pool.c | 92 +++++++++++++++++++++++++++++++-
> --
> 1 file changed, 85 insertions(+), 7 deletions(-)
>
>
> base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention
2026-11-25 10:14 ` [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention Thomas Hellström
@ 2026-11-25 13:27 ` Eduardo P. Gomez
2026-11-25 15:47 ` Thomas Hellström
0 siblings, 1 reply; 11+ messages in thread
From: Eduardo P. Gomez @ 2026-11-25 13:27 UTC (permalink / raw)
To: Thomas Hellström
Cc: Natalie Vock, Arun Pravin, dri-devel, Matthew Auld, Matthew Brost
[-- Attachment #1: Type: text/plain, Size: 1138 bytes --]
(sending a copy to the mail list)
Hi Thomas,
Under memory pressure, the existing shrinker indeed worked in my unpatched
7.4-rc4 QEMU test. It reclaimed all 768 MiB, and the competing allocation
completed.
And yes, I'm actually facing out of memory freezes on my 32 GiB desktop. My
early investigations showed GPUReclaim was using most of my RAM (the test
repo is the result of my last test before creating the patch). The tests
shows GPUReclaim stayed at 12.25 GiB of 32 GiB for at least 60 seconds
after the reproducer exited. In a separate 8 GiB desktop run with v3, it
dropped to about 128 MiB by the five-second sample.
Once the allocation burst is over, what benefit do we get from keeping the
full pool until memory pressure, compared with trimming the excess after a
short window for reuse? My concern is the cost of reclaiming a pool that
large when another workload already needs the memory.
If you’d like to try the standalone reproducer at 8 GiB, I’d be interested
to see how long a competing allocation takes on the unpatched kernel once
it needs memory back from the pool.
Thanks,
Eduardo
[-- Attachment #2: Type: text/html, Size: 1447 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention
2026-11-25 13:27 ` Eduardo P. Gomez
@ 2026-11-25 15:47 ` Thomas Hellström
2026-11-25 19:36 ` Eduardo P. Gomez
0 siblings, 1 reply; 11+ messages in thread
From: Thomas Hellström @ 2026-11-25 15:47 UTC (permalink / raw)
To: Eduardo P. Gomez
Cc: Natalie Vock, Arun Pravin, dri-devel, Matthew Auld, Matthew Brost
On Wed, 2026-11-25 at 10:27 -0300, Eduardo P. Gomez wrote:
> (sending a copy to the mail list)
>
> Hi Thomas,
>
> Under memory pressure, the existing shrinker indeed worked in my
> unpatched
> 7.4-rc4 QEMU test. It reclaimed all 768 MiB, and the competing
> allocation
> completed.
>
> And yes, I'm actually facing out of memory freezes on my 32 GiB
> desktop. My
> early investigations showed GPUReclaim was using most of my RAM (the
> test
> repo is the result of my last test before creating the patch). The
> tests
> shows GPUReclaim stayed at 12.25 GiB of 32 GiB for at least 60
> seconds
> after the reproducer exited. In a separate 8 GiB desktop run with v3,
> it
> dropped to about 128 MiB by the five-second sample.
>
> Once the allocation burst is over, what benefit do we get from
> keeping the
> full pool until memory pressure, compared with trimming the excess
> after a
> short window for reuse? My concern is the cost of reclaiming a pool
> that
> large when another workload already needs the memory.
The benefit is that since it is costly to transition memory from WC to
WB, we do it only on demand. The kernel can't guess whether the next
application to be used requires a lot of GPU memory or not. And we
don't need to add complexity to go beyond the core reclaim mechanisms.
But the allocation latency didn't seem to be the trigger for this work,
rather unexpected OOMS. Did you actually see any instance of remaining
GPUReclaim memory when OOM was hit?
Thanks,
Thomas
>
> If you’d like to try the standalone reproducer at 8 GiB, I’d be
> interested
> to see how long a competing allocation takes on the unpatched kernel
> once
> it needs memory back from the pool.
>
> Thanks,
> Eduardo
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention
2026-11-25 15:47 ` Thomas Hellström
@ 2026-11-25 19:36 ` Eduardo P. Gomez
2026-11-26 10:35 ` Thomas Hellström
0 siblings, 1 reply; 11+ messages in thread
From: Eduardo P. Gomez @ 2026-11-25 19:36 UTC (permalink / raw)
To: Thomas Hellström
Cc: Natalie Vock, Arun Pravin, dri-devel, Matthew Auld, Matthew Brost
[-- Attachment #1: Type: text/plain, Size: 612 bytes --]
Hi Thomas,
> Did you actually see any instance of remaining GPUReclaim memory when OOM
was hit?
Yes. Twice actually. During a userspace memory-pressure test on my 32 GiB
desktop running kernel 7.3.5 without my TTM patch, the kernel reported two
global OOMs and killed kde-ram (the test I mentioned before) both times:
2026-11-23 21:43:51 -0300. gpu_reclaim:10045728kB global_oom
2026-11-23 21:44:08 -0300 gpu_reclaim:10045736kB global_oom
About 9.58 GiB was still reported as reclaimable GPU memory at the time of
each OOM.
The full kernel log for the first event is here:
https://paste.rs/ajafn
[-- Attachment #2: Type: text/html, Size: 998 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread* Re: [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention
2026-11-25 19:36 ` Eduardo P. Gomez
@ 2026-11-26 10:35 ` Thomas Hellström
2026-11-29 3:46 ` Eduardo P. Gomez
0 siblings, 1 reply; 11+ messages in thread
From: Thomas Hellström @ 2026-11-26 10:35 UTC (permalink / raw)
To: Eduardo P. Gomez
Cc: Natalie Vock, Arun Pravin, dri-devel, Matthew Auld, Matthew Brost
On Wed, 2026-11-25 at 16:36 -0300, Eduardo P. Gomez wrote:
> Hi Thomas,
>
> > Did you actually see any instance of remaining GPUReclaim memory
> > when OOM
> was hit?
>
> Yes. Twice actually. During a userspace memory-pressure test on my 32
> GiB
> desktop running kernel 7.3.5 without my TTM patch, the kernel
> reported two
> global OOMs and killed kde-ram (the test I mentioned before) both
> times:
>
> 2026-11-23 21:43:51 -0300. gpu_reclaim:10045728kB global_oom
> 2026-11-23 21:44:08 -0300 gpu_reclaim:10045736kB global_oom
>
> About 9.58 GiB was still reported as reclaimable GPU memory at the
> time of
> each OOM.
>
> The full kernel log for the first event is here:
> https://paste.rs/ajafn
Judging from the log there is also a high number of inactive_file pages
available that are not reclaimed.
This IMO points to something odd happening with reclaim itself rather
than something specific to the ttm pool shrinker?
Thanks,
Thomas
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC PATCH 0/2] drm/ttm: fix excessive idle memory retention
2026-11-26 10:35 ` Thomas Hellström
@ 2026-11-29 3:46 ` Eduardo P. Gomez
0 siblings, 0 replies; 11+ messages in thread
From: Eduardo P. Gomez @ 2026-11-29 3:46 UTC (permalink / raw)
To: Thomas Hellström
Cc: Natalie Vock, Arun Pravin, dri-devel, Matthew Auld, Matthew Brost
[-- Attachment #1: Type: text/plain, Size: 1534 bytes --]
Hello again, Thomas! I'm still checking the cause of inactive_file
increase. I tried reading a big file on a VM and doesn't look like it's
keeping ram busy.
In other hand, working for hours in my PC surely did increase my
"Inactive(file)" reading from /proc/meminfo.
I'm still working on it. You might be right. This might be a generalized
issue with reclaim, not TTM.
On Thu, Nov 26, 2026, 7:35 AM Thomas Hellström <
thomas.hellstrom@linux.intel.com> wrote:
> On Wed, 2026-11-25 at 16:36 -0300, Eduardo P. Gomez wrote:
> > Hi Thomas,
> >
> > > Did you actually see any instance of remaining GPUReclaim memory
> > > when OOM
> > was hit?
> >
> > Yes. Twice actually. During a userspace memory-pressure test on my 32
> > GiB
> > desktop running kernel 7.3.5 without my TTM patch, the kernel
> > reported two
> > global OOMs and killed kde-ram (the test I mentioned before) both
> > times:
> >
> > 2026-11-23 21:43:51 -0300. gpu_reclaim:10045728kB global_oom
> > 2026-11-23 21:44:08 -0300 gpu_reclaim:10045736kB global_oom
> >
> > About 9.58 GiB was still reported as reclaimable GPU memory at the
> > time of
> > each OOM.
> >
> > The full kernel log for the first event is here:
> > https://paste.rs/ajafn
>
> Judging from the log there is also a high number of inactive_file pages
> available that are not reclaimed.
>
> This IMO points to something odd happening with reclaim itself rather
> than something specific to the ttm pool shrinker?
>
> Thanks,
> Thomas
>
[-- Attachment #2: Type: text/html, Size: 2203 bytes --]
^ permalink raw reply [flat|nested] 11+ messages in thread