All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Eduardo P. Gomez" <eduardoprocopiogomez@gmail.com>,
	dri-devel@lists.freedesktop.org
Subject: Re: [RFC PATCH v3] drm/ttm: reclaim excess pooled pages with batched write-back
Date: Wed, 25 Nov 2026 03:00:58 +0000	[thread overview]
Message-ID: <sashiko-outbox-232432@kernel.org> (raw)
In-Reply-To: <20261125024630.257843-1-eduardoprocopiogomez@gmail.com>

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

      reply	other threads:[~2026-11-25  3:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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-232432@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.