Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] drm/ttm: broken interleave node rotation on order decrease in
  __ttm_pool_alloc()
- [Medium] drm/ttm: unconstrained order-0 fallback and reclaim in
  ttm_pool_alloc_interleave()
- [Medium] drm/ttm: cross-node pool take in
  ttm_pool_type_take_interleave() before alloc
--

commit 99e6c53830405c25271c091f8f19cad519333639
Author: Himal Prasad Ghimiray <[email protected]>

drm/ttm: Add TTM_NUMA_POLICY_INTERLEAVE

This commit adds TTM_NUMA_POLICY_INTERLEAVE to rotate TTM page allocations
and pool lookups across the NUMA nodes in ttm_operation_ctx.numa_info.nodes.

> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index 8976de3a552d1..210d443185644 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
> @@ -373,6 +373,38 @@ static struct page *ttm_pool_type_take(struct 
> ttm_pool_type *pt, int nid)
>       return p;
>  }
>  
> +/* Return the node in @nodes nearest to @from, lowest node id on ties */
> +static int ttm_pool_nearest_node(int from, const nodemask_t *nodes)
> +{
> +     int n, nid = first_node(*nodes);
> +
> +     for_each_node_mask(n, *nodes) {
> +             if (node_distance(from, n) < node_distance(from, nid))
> +                     nid = n;
> +     }
> +
> +     return nid;
> +}
> +
> +/* Take from @nid first, then from the other nodes nearest to @nid first */
> +static struct page *ttm_pool_type_take_interleave(struct ttm_pool_type *pt,
> +                                               int nid, const nodemask_t 
> *nodes)
> +{
> +     nodemask_t todo = *nodes;
> +     struct page *p;
> +
> +     p = ttm_pool_type_take(pt, nid);
> +     node_clear(nid, todo);
> +     while (!p && !nodes_empty(todo)) {
> +             int n = ttm_pool_nearest_node(nid, &todo);
> +
> +             node_clear(n, todo);
> +             p = ttm_pool_type_take(pt, n);
> +     }
> +
> +     return p;
> +}

[Severity: Medium]
Does ttm_pool_type_take_interleave() take cached pool pages from other nodes
in @nodes before trying a fresh page allocation on @nid?

When @nid's pool for a write-combined or uncached order is empty,
__ttm_pool_alloc() drains pooled pages from sibling policy nodes even when
@nid has free system memory.  Because ttm_pool_free() returns freed pages to
the pool of page_to_nid(p), @nid's pool remains empty and interleaved WC/UC
allocations collapse onto whichever node already had cached pool pages.

[ ... ]

> @@ -830,6 +873,31 @@ static struct page *ttm_pool_alloc_bind(struct ttm_pool 
> *pool, gfp_t gfp_flags,
>       return p;
>  }
>  
> +/*
> + * Allocate pages of size 1 << order for TTM_NUMA_POLICY_INTERLEAVE. Try the
> + * interleaved node @nid first, then the other nodes in @nodes nearest to 
> @nid
> + * first, as the per-node zonelist fallback of core mm does, before the 
> caller
> + * lowers the order.
> + */
> +static struct page *ttm_pool_alloc_interleave(struct ttm_pool *pool,
> +                                           gfp_t gfp_flags, unsigned int 
> order,
> +                                           int nid, const nodemask_t *nodes)
> +{
> +     nodemask_t todo = *nodes;
> +     struct page *p;
> +
> +     p = ttm_pool_alloc_page(pool, gfp_flags, order, nid);
> +     node_clear(nid, todo);
> +     while (!p && !nodes_empty(todo)) {
> +             int n = ttm_pool_nearest_node(nid, &todo);
> +
> +             node_clear(n, todo);
> +             p = ttm_pool_alloc_page(pool, gfp_flags, order, n);
> +     }
> +
> +     return p;
> +}

[Severity: Medium]
Does ttm_pool_alloc_interleave() fall back outside @nodes on order-0
allocations and stall in reclaim before trying other policy nodes?

ttm_pool_alloc_page() only sets __GFP_THISNODE when order > 0.  At order 0,
the first ttm_pool_alloc_page() call walks @nid's unfiltered fallback
zonelist, which can allocate from a node outside @nodes before the remaining
nodes in @todo are tried, and if all zones are exhausted the while loop
repeats system-wide direct reclaim once for each remaining node in @todo.
In addition, unlike ttm_pool_alloc_bind(), ttm_pool_alloc_interleave() does
not try a non-reclaim pass across @nodes first, so a depleted @nid enters
direct reclaim and compaction even when another node in @nodes has free
pages of that order.

[ ... ]

> @@ -893,11 +961,24 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, 
> struct ttm_tt *tt,
>            order = ttm_pool_alloc_find_order(order, alloc)) {
>               struct ttm_pool_type *pt;
>  
> +             if (policy == TTM_NUMA_POLICY_INTERLEAVE) {
> +                     unsigned int slot = tt->num_pages - 
> alloc->remaining_pages;
> +
> +                     alloc_nid = 
> ttm_pool_policy_nth_node(ctx->numa_info.nodes,
> +                                                          slot >> order);
> +                     take_nid = alloc_nid;
> +             }

[Severity: High]
Does computing the node index as slot >> order fail to rotate across
ctx->numa_info.nodes whenever the allocation order decreases?

In __ttm_pool_alloc(), order is monotonically non-increasing, and slot is the
sum of 1 << prev_order for all prior chunks.  Whenever a chunk has a smaller
order than the chunks before it, every prior chunk contributed an even
multiple of 1 << order pages to slot, so slot >> order is always even.  When
nodes_weight(*ctx->numa_info.nodes) is even, such as a 2-node mask,
(slot >> order) % nodes_weight(...) is always 0 on every decreased order.
For example, a 3 MiB buffer (orders 9 and 8) or any buffer smaller than
8 MiB whose size is not a multiple of 4 MiB places all of its chunks on the
first policy node instead of interleaving them.  Counting allocated chunks
with a separate counter that increments once per allocated page would rotate
across nodes regardless of order changes.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=3

Reply via email to