All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Eduardo P. Gomez" <eduardoprocopiogomez@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Subject: Re: [RFC PATCH 1/2] drm/ttm: reclaim pooled pages after a period of inactivity
Date: Wed, 25 Nov 2026 09:04:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-232971@kernel.org> (raw)
In-Reply-To: <20261124222812.511867-2-eduardoprocopiogomez@gmail.com>

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

  reply	other threads:[~2026-11-25  9:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-25  9:04   ` sashiko-bot [this message]
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
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
2026-11-25 19:36       ` Eduardo P. Gomez
2026-11-26 10:35         ` Thomas Hellström
2026-11-29  3:46           ` Eduardo P. Gomez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-232971@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=eduardoprocopiogomez@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.