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 <[email protected]>

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/[email protected]?part=2

Reply via email to