Add TTM_NUMA_POLICY_BIND to enum ttm_numa_policy. With this policy,
system pages are allocated only from the nodes in
ttm_operation_ctx.numa_info.nodes, tried nearest first from the local
node (ttm_pool_nid()) by node_distance(), with ties broken by lower
node id.

__GFP_THISNODE is added for each node so the allocator never falls back
to nodes outside the policy, including for order 0 allocations. Failures
on all policy nodes use the existing order fallback and error path.

Signed-off-by: Himal Prasad Ghimiray <[email protected]>
---
 drivers/gpu/drm/ttm/ttm_pool.c | 58 ++++++++++++++++++++++++++++++++--
 include/drm/ttm/ttm_bo.h       |  4 +++
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index e17bd5c0eab3..4a7775bfd0fb 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -780,6 +780,56 @@ static unsigned int ttm_pool_alloc_find_order(unsigned int 
highest,
        return min_t(unsigned int, highest, __fls(alloc->remaining_pages));
 }
 
+/*
+ * Try the nodes in @nodes once each, nearest first from the local node.
+ * __GFP_THISNODE prevents fallback to nodes outside the policy.
+ */
+static struct page *ttm_pool_alloc_bind_pass(struct ttm_pool *pool,
+                                            gfp_t gfp_flags, unsigned int 
order,
+                                            const nodemask_t *nodes)
+{
+       int local = ttm_pool_nid(pool);
+       nodemask_t todo = *nodes;
+       struct page *p = NULL;
+
+       while (!nodes_empty(todo)) {
+               int nid = first_node(todo), n;
+
+               for_each_node_mask(n, todo) {
+                       if (node_distance(local, n) < node_distance(local, nid))
+                               nid = n;
+               }
+               node_clear(nid, todo);
+
+               p = ttm_pool_alloc_page(pool, gfp_flags | __GFP_THISNODE, order,
+                                       nid);
+               if (p)
+                       break;
+       }
+
+       return p;
+}
+
+/*
+ * Allocate pages of size 1 << order for TTM_NUMA_POLICY_BIND. Nodes with free
+ * memory are tried without reclaim first, then with the caller's gfp flags.
+ */
+static struct page *ttm_pool_alloc_bind(struct ttm_pool *pool, gfp_t gfp_flags,
+                                       unsigned int order,
+                                       const nodemask_t *nodes)
+{
+       const unsigned int beneficial_order = ttm_pool_beneficial_order(pool);
+       gfp_t nowait = (gfp_flags & ~__GFP_RECLAIM) | __GFP_NOWARN;
+       struct page *p;
+
+       p = ttm_pool_alloc_bind_pass(pool, nowait, order, nodes);
+       /* Orders without reclaim would repeat the first pass */
+       if (!p && (!order || !beneficial_order || order == beneficial_order))
+               p = ttm_pool_alloc_bind_pass(pool, gfp_flags, order, nodes);
+
+       return p;
+}
+
 /* Validate the NUMA request in @ctx against @pool */
 static int ttm_pool_check_numa(struct ttm_pool *pool,
                               const struct ttm_operation_ctx *ctx)
@@ -858,8 +908,12 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct 
ttm_tt *tt,
                if (!p) {
                        page_caching = ttm_cached;
                        allow_pools = false;
-                       p = ttm_pool_alloc_page(pool, gfp_flags, order,
-                                               alloc_nid);
+                       if (policy == TTM_NUMA_POLICY_BIND)
+                               p = ttm_pool_alloc_bind(pool, gfp_flags, order,
+                                                       ctx->numa_info.nodes);
+                       else
+                               p = ttm_pool_alloc_page(pool, gfp_flags, order,
+                                                       alloc_nid);
                }
                /* If that fails, lower the order if possible and retry. */
                if (!p) {
diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h
index 5289c17cd449..e7605e7e97ec 100644
--- a/include/drm/ttm/ttm_bo.h
+++ b/include/drm/ttm/ttm_bo.h
@@ -58,10 +58,14 @@ struct ttm_tt;
  * @TTM_NUMA_POLICY_NONE: No policy. Use the pool default node.
  * @TTM_NUMA_POLICY_PREFERRED: Prefer the single node in
  *     ttm_operation_ctx.numa_info.nodes. Falls back to other nodes.
+ * @TTM_NUMA_POLICY_BIND: Allocate only from the nodes in
+ *     ttm_operation_ctx.numa_info.nodes, in ascending node order. Never falls
+ *     back to other nodes.
  */
 enum ttm_numa_policy {
        TTM_NUMA_POLICY_NONE = 0,
        TTM_NUMA_POLICY_PREFERRED,
+       TTM_NUMA_POLICY_BIND,
 };
 
 /**
-- 
2.43.0

Reply via email to