The TTM pool already keeps its page caches, accounting and shrinker per NUMA node, but always allocates on the pool's default node. Add a numa_info member to struct ttm_operation_ctx with a policy and a nodemask, and honor it in __ttm_pool_alloc(), so both ttm_pool_alloc() and restore of backed-up content can place pages according to the policy.
Add enum ttm_numa_policy with TTM_NUMA_POLICY_PREFERRED, which allocates from the single node in the mask and may fall back to other nodes. TTM_NUMA_POLICY_NONE is zero, so zero-initialized users keep the existing behaviour. dma-alloc pools can't honor a policy and return -EOPNOTSUPP. An empty mask returns -EINVAL, and PREFERRED with a mask that is not exactly one node returns -EINVAL. v2: - Replace ttm_pool_alloc_node(), ttm_tt_restore_node() and ttm_pool_restore_and_alloc_node() with a node in struct ttm_operation_ctx, instead of adding a second NUMA aware path (Christian) - Keep NUMA_NO_NODE unresolved for alloc_pages_node() so memoryless nodes still fall back to numa_mem_id() (sashiko) - Fix commit message claiming the pool helpers already take a nid (sashiko) - Group the NUMA fields in a numa_info sub-struct - Replace the single node field with a policy and a nodemask, so other mempolicy modes can be added (Thomas) Signed-off-by: Himal Prasad Ghimiray <[email protected]> --- drivers/gpu/drm/ttm/ttm_pool.c | 56 +++++++++++++++++++++++++++++----- include/drm/ttm/ttm_bo.h | 30 ++++++++++++++++++ 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c index 1bf37023fed6..e17bd5c0eab3 100644 --- a/drivers/gpu/drm/ttm/ttm_pool.c +++ b/drivers/gpu/drm/ttm/ttm_pool.c @@ -158,9 +158,9 @@ static int ttm_pool_nid(struct ttm_pool *pool) return nid; } -/* Allocate pages of size 1 << order with the given gfp_flags */ +/* Allocate pages of size 1 << order with the given gfp_flags on node @nid */ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, - unsigned int order) + unsigned int order, int nid) { const unsigned int beneficial_order = ttm_pool_beneficial_order(pool); unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS; @@ -189,7 +189,7 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, } if (!ttm_pool_uses_dma_alloc(pool)) { - p = alloc_pages_node(pool->nid, gfp_flags, order); + p = alloc_pages_node(nid, gfp_flags, order); if (p) { p->private = order; mod_lruvec_page_state(p, NR_GPU_ACTIVE, 1 << order); @@ -780,11 +780,34 @@ static unsigned int ttm_pool_alloc_find_order(unsigned int highest, return min_t(unsigned int, highest, __fls(alloc->remaining_pages)); } +/* Validate the NUMA request in @ctx against @pool */ +static int ttm_pool_check_numa(struct ttm_pool *pool, + const struct ttm_operation_ctx *ctx) +{ + if (ctx->numa_info.policy == TTM_NUMA_POLICY_NONE) + return 0; + + if (ttm_pool_uses_dma_alloc(pool)) + return -EOPNOTSUPP; + + if (!ctx->numa_info.nodes || nodes_empty(*ctx->numa_info.nodes)) + return -EINVAL; + + if (ctx->numa_info.policy == TTM_NUMA_POLICY_PREFERRED && + nodes_weight(*ctx->numa_info.nodes) != 1) + return -EINVAL; + + return 0; +} + static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, const struct ttm_operation_ctx *ctx, struct ttm_pool_alloc_state *alloc, struct ttm_pool_tt_restore *restore) { + /* pool->nid may be NUMA_NO_NODE, letting alloc_pages_node() pick */ + int alloc_nid = pool->nid, take_nid = ttm_pool_nid(pool); + enum ttm_numa_policy policy = ctx->numa_info.policy; enum ttm_caching page_caching; gfp_t gfp_flags = GFP_USER; pgoff_t caching_divide; @@ -796,6 +819,12 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, WARN_ON(!alloc->remaining_pages || ttm_tt_is_populated(tt)); WARN_ON(alloc->dma_addr && !pool->dev); + if (policy != TTM_NUMA_POLICY_NONE) { + /* Start at the single node of the policy */ + alloc_nid = first_node(*ctx->numa_info.nodes); + take_nid = alloc_nid; + } + if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC) gfp_flags |= __GFP_ZERO; @@ -818,7 +847,7 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, p = NULL; pt = ttm_pool_select_type(pool, page_caching, order); if (pt && allow_pools) - p = ttm_pool_type_take(pt, ttm_pool_nid(pool)); + p = ttm_pool_type_take(pt, take_nid); /* * If that fails or previously failed, allocate from system. @@ -829,7 +858,8 @@ 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); + p = ttm_pool_alloc_page(pool, gfp_flags, order, + alloc_nid); } /* If that fails, lower the order if possible and retry. */ if (!p) { @@ -888,16 +918,22 @@ static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, * Fill the ttm_tt object with pages and also make sure to DMA map them when * necessary. * - * Returns: 0 on successe, negative error code otherwise. + * Returns: 0 on successe, -EOPNOTSUPP if @ctx requests a NUMA node on a + * dma-alloc pool, negative error code otherwise. */ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, struct ttm_operation_ctx *ctx) { struct ttm_pool_alloc_state alloc; + int ret; if (WARN_ON(ttm_tt_is_backed_up(tt))) return -EINVAL; + ret = ttm_pool_check_numa(pool, ctx); + if (ret) + return ret; + ttm_pool_alloc_state_init(tt, &alloc); return __ttm_pool_alloc(pool, tt, ctx, &alloc, NULL); @@ -915,7 +951,9 @@ EXPORT_SYMBOL(ttm_pool_alloc); * Fill the ttm_tt object with pages and also make sure to DMA map them when * necessary. Read in backed-up content. * - * Returns: 0 on successe, negative error code otherwise. + * Returns: 0 on success, -EOPNOTSUPP if @ctx requests a NUMA placement on a + * dma-alloc pool, -EINVAL on an empty NUMA policy, negative error code + * otherwise. */ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt, const struct ttm_operation_ctx *ctx) @@ -927,6 +965,10 @@ int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt, if (WARN_ON(!ttm_tt_is_backed_up(tt))) return -EINVAL; + ret = ttm_pool_check_numa(pool, ctx); + if (ret) + return ret; + if (!restore) { gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; diff --git a/include/drm/ttm/ttm_bo.h b/include/drm/ttm/ttm_bo.h index 32791c4db2a9..5289c17cd449 100644 --- a/include/drm/ttm/ttm_bo.h +++ b/include/drm/ttm/ttm_bo.h @@ -35,6 +35,7 @@ #include <linux/kref.h> #include <linux/list.h> +#include <linux/nodemask.h> #include "ttm_device.h" @@ -51,6 +52,18 @@ struct ttm_resource; struct ttm_resource_manager; struct ttm_tt; +/** + * enum ttm_numa_policy - NUMA placement policy for system pages + * + * @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. + */ +enum ttm_numa_policy { + TTM_NUMA_POLICY_NONE = 0, + TTM_NUMA_POLICY_PREFERRED, +}; + /** * enum ttm_bo_type * @@ -187,6 +200,23 @@ struct ttm_operation_ctx { * when multiple BOs share the same reservation object @resv. */ bool allow_res_evict; + /** + * @numa_info: NUMA placement of system pages. Not supported by + * dma-alloc pools. + */ + struct { + /** + * @numa_info.policy: Policy applied to the allocation, see + * &enum ttm_numa_policy. + */ + enum ttm_numa_policy policy; + /** + * @numa_info.nodes: Nodes the policy applies to. Must be set + * and non-empty when @policy is not TTM_NUMA_POLICY_NONE. Must + * contain exactly one node for TTM_NUMA_POLICY_PREFERRED. + */ + const nodemask_t *nodes; + } numa_info; /** * @resv: Reservation object to be used together with * @allow_res_evict. -- 2.43.0
