From: Chen Yu <chen.yu@linux.dev>
To: Jianyong Wu <wujianyong@hygon.cn>
Cc: Ingo Molnar <mingo@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Juri Lelli <juri.lelli@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Chen Yu <yu.c.chen@intel.com>,
Tim Chen <tim.c.chen@linux.intel.com>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
Steven Rostedt <rostedt@goodmis.org>,
Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
Valentin Schneider <vschneid@redhat.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Shrikanth Hegde <sshegde@linux.ibm.com>,
Phil Auld <pauld@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
maddy@linux.ibm.com, Michael Ellerman <mpe@ellerman.id.au>,
ljs@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org,
jianyong.wu@outlook.com, zhongyuan@hygon.cn, huangsj@hygon.cn,
wangfengyu@hygon.cn, yingzhiwei@hygon.cn
Subject: Re: [RFC PATCH v3 01/17] sched/topology: Add llc_to_node() to translate LLC ID to NUMA node
Date: Sun, 29 Nov 2026 23:26:09 +0800 [thread overview]
Message-ID: <awxEEYOuwrX-3FQf@three-body> (raw)
In-Reply-To: <20261029022036.4002331-2-wujianyong@hygon.cn>
On Thu, Oct 29, 2026 at 10:20:19AM +0800, Jianyong Wu wrote:
>
> Building LLC affinity sequences requires knowing which NUMA node each
> LLC belongs to. Add a per-LLC-ID NUMA-node map derived from possible
> CPUs that currently hold an LLC ID, and expose it to the scheduler
> through llc_to_node(). Unassigned IDs return NUMA_NO_NODE.
>
> Signed-off-by: Jianyong Wu <wujianyong@hygon.cn>
> ---
> kernel/sched/sched.h | 1 +
> kernel/sched/topology.c | 93 +++++++++++++++++++++++++++++++++++++++--
> 2 files changed, 91 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
> index 56acf502ba26..4ee223416012 100644
> --- a/kernel/sched/sched.h
> +++ b/kernel/sched/sched.h
> @@ -4107,6 +4107,7 @@ static inline bool sched_cache_enabled(void)
> }
>
> extern void sched_cache_active_set(void);
> +extern int llc_to_node(int llc);
>
> #endif
>
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index 622e2e01974c..18171d2338fe 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -685,6 +685,24 @@ DEFINE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
> DEFINE_STATIC_KEY_FALSE(sched_asym_cpucapacity);
> DEFINE_STATIC_KEY_FALSE(sched_cluster_active);
>
> +#ifdef CONFIG_SCHED_CACHE
> +/*
> + * LLC id to NUMA node map. The number of entries lives inside the
> + * RCU-protected allocation so that a reader bound-checks against the
> + * length of the very map it dereferenced. max_lid is updated by
> + * __sched_domains_alloc_llc_id() well before rebuild_llc_node_map()
> + * installs the matching map, so it must not be used as the bound.
> + */
> +struct llc_node_map {
> + int nr_llc;
> + int node[];
> +};
> +
> +static struct llc_node_map __rcu *llc_to_node_map;
> +static void rebuild_llc_node_map(int size);
> +
> +#endif
> +
> static void update_top_cache_domain(int cpu)
> {
> struct sched_domain_shared *sds = NULL;
> @@ -856,6 +874,64 @@ DEFINE_STATIC_KEY_FALSE(sched_cache_active);
> /* user wants cache aware scheduling [0 or 1] */
> int sysctl_sched_cache_user = 1;
>
> +int llc_to_node(int llc)
> +{
> + struct llc_node_map *map;
> + int node = -1;
> +
> + rcu_read_lock();
> + map = rcu_dereference(llc_to_node_map);
> + if (map && llc >= 0 && llc < map->nr_llc)
> + node = map->node[llc];
> + rcu_read_unlock();
> +
> + return node;
> +}
> +
How about allocate llc_node = kmalloc_array(n, sizeof(*llc_node), GFP_KERNEL); near
sched_domains_llc_id_allocmask() in sched_init_domains()? In this way, we don't need
to reallocate the map array. We just update the llc_to_node_map[] values (either NUMA_NO_NODE
or the new llc_id) of the CPUs when the llc_id changes for that LLC domain, and we don't have
to update all the values in llc_to_node_map[]?
> +static void rebuild_llc_node_map(int size)
> +{
> + struct llc_node_map *new_map, *old_map;
> + int cpu, llc, node;
> +
> + lockdep_assert_held(&sched_domains_mutex);
> + if (size <= 0)
> + goto fail;
> +
> + new_map = kzalloc(struct_size(new_map, node, size), GFP_KERNEL);
> + if (!new_map)
> + goto fail;
> +
> + new_map->nr_llc = size;
> + for (llc = 0; llc < size; llc++)
> + new_map->node[llc] = NUMA_NO_NODE;
> +
> + /*
> + * for_each_possible_cpu() revisits the same LLC non-consecutively
> + * under SMT (each node's LLCs are walked once per thread), so
> + * dedup by populated node entries, not by comparing against the
> + * immediately preceding CPU's llc.
> + */
> + for_each_possible_cpu(cpu) {
> + llc = per_cpu(sd_llc_id, cpu);
> + if (llc < 0 || llc >= size || new_map->node[llc] >= 0)
new_map->node[llc] != NUMA_NO_NODE ?
> + continue;
> + node = cpu_to_node(cpu);
> + if (node >= 0 && node < nr_node_ids && node_possible(node))
I thought node != NUMA_NO_NODE should be enough, why check node < nr_node_ids and
node_possible(node)?
> + new_map->node[llc] = node;
> + }
> +
> +publish:
> + old_map = rcu_dereference_protected(llc_to_node_map,
> + lockdep_is_held(&sched_domains_mutex));
> + rcu_assign_pointer(llc_to_node_map, new_map);
> + kfree_rcu_mightsleep(old_map);
> + return;
> +
> +fail:
> + new_map = NULL;
> + goto publish;
If new_map allocation failed, can we still use the old llc_to_node_map?
> +}
> +
> /*
> * Get the effective LLC size in bytes that @cpu's bottom sched_domain
> * can use. A CPU within a cpuset partition can only use a proportion
> @@ -3040,7 +3116,8 @@ static int __sched_domains_alloc_llc_id(void)
> return lid;
> }
>
> -static void __sched_domains_free_llc_id(int cpu)
> +/* Return true only when the last CPU releases the LLC ID. */
> +static bool __sched_domains_free_llc_id(int cpu)
> {
> int i, lid, max;
>
> @@ -3048,14 +3125,14 @@ static void __sched_domains_free_llc_id(int cpu)
>
> lid = per_cpu(sd_llc_id, cpu);
> if (lid == -1 || lid >= nr_cpu_ids)
> - return;
> + return false;
>
> per_cpu(sd_llc_id, cpu) = -1;
>
> for_each_cpu(i, llc_mask(cpu)) {
> /* An online CPU owns the llc_id. */
> if (per_cpu(sd_llc_id, i) == lid)
> - return;
> + return false;
> }
>
> __cpumask_clear_cpu(lid, sched_domains_llc_id_allocmask);
> @@ -3064,12 +3141,18 @@ static void __sched_domains_free_llc_id(int cpu)
> /* shrink max lid to save memory */
> if (max < max_lid)
> max_lid = max;
> + return true;
> }
>
> void sched_domains_free_llc_id(int cpu)
> {
> sched_domains_mutex_lock();
> +#ifdef CONFIG_SCHED_CACHE
> + if (__sched_domains_free_llc_id(cpu))
> + rebuild_llc_node_map(max_lid + 1);
> +#else
May I know why llc_to_node depends on CONFIG_SCHED_CACHE? Since llc_id is already
independent of cache-aware scheduling, shouldn't llc_to_node be as well?
thanks,
Chenyu
> __sched_domains_free_llc_id(cpu);
> +#endif
> sched_domains_mutex_unlock();
> }
>
> @@ -3139,6 +3222,10 @@ build_sched_domains(const struct cpumask *cpu_map, struct sched_domain_attr *att
> }
> }
>
> +#ifdef CONFIG_SCHED_CACHE
> + /* The map depends on assigned LLC IDs, not on sd_llc allocations. */
> + rebuild_llc_node_map(max_lid + 1);
> +#endif
> if (WARN_ON(!topology_span_sane(cpu_map)))
> goto error;
>
> --
> 2.34.1
>
>
next prev parent reply other threads:[~2026-11-29 15:24 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 2:20 [RFC PATCH v3 00/17] sched: Scale cache-aware aggregation at LLC granularity Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 01/17] sched/topology: Add llc_to_node() to translate LLC ID to NUMA node Jianyong Wu
2026-11-29 15:26 ` Chen Yu [this message]
2026-10-29 2:20 ` [RFC PATCH v3 02/17] sched/topology: Build NUMA node affinity sequences Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 03/17] sched/topology: Add an iterator for cached NUMA node affinity rows Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 04/17] sched/topology: Build triangular affinity orders for LLCs in a node Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 05/17] sched/topology: Walk LLCs in NUMA node Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 06/17] sched/cache: Prioritize cache-preferred node selection over LLC selection Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 07/17] sched/topology: Add per-sd scratch for cache-aware load balancing Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 08/17] sched/cache: Introduce helpers for task migration decisions Jianyong Wu
2026-10-30 3:04 ` sashiko-bot
2026-10-29 2:20 ` [RFC PATCH v3 09/17] sched/cache: Introduce rq affinity gain calculation Jianyong Wu
2026-10-30 3:05 ` sashiko-bot
2026-10-29 2:20 ` [RFC PATCH v3 10/17] sched/cache: Pick the source rq/group with the highest affinity gain Jianyong Wu
2026-10-30 3:05 ` sashiko-bot
2026-10-29 2:20 ` [RFC PATCH v3 11/17] sched/cache: Drop prefer_sibling restriction for llc_balance Jianyong Wu
2026-10-29 2:20 ` [RFC PATCH v3 12/17] sched/cache: Apply multi-LLC aggregation policy to task migration Jianyong Wu
2026-10-30 3:05 ` sashiko-bot
2026-10-29 2:45 ` [RFC PATCH v3 13/17] sched/cache: Allow active balance to spread load from a full LLC Jianyong Wu
2026-10-30 3:05 ` sashiko-bot
2026-10-29 2:50 ` [RFC PATCH v3 14/17] sched/cache: Consider LLC load when selecting a single-task source rq Jianyong Wu
2026-10-29 2:56 ` [RFC PATCH v3 15/17] sched/cache: Include recently used preferred nodes in LLC scans Jianyong Wu
2026-10-30 3:05 ` sashiko-bot
2026-10-29 2:58 ` [RFC PATCH v3 16/17] sched/cache: Drop stale preferred LLC checks Jianyong Wu
2026-10-30 3:05 ` sashiko-bot
2026-10-29 2:59 ` [RFC PATCH v3 17/17] sched/debug: Show cache placement and preferred LLC in /proc/<pid>/sched Jianyong Wu
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=awxEEYOuwrX-3FQf@three-body \
--to=chen.yu@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=bsegall@google.com \
--cc=david@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=huangsj@hygon.cn \
--cc=jianyong.wu@outlook.com \
--cc=juri.lelli@redhat.com \
--cc=kprateek.nayak@amd.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=maddy@linux.ibm.com \
--cc=mgorman@suse.de \
--cc=mingo@redhat.com \
--cc=mpe@ellerman.id.au \
--cc=pauld@redhat.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=sshegde@linux.ibm.com \
--cc=tim.c.chen@linux.intel.com \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=wangfengyu@hygon.cn \
--cc=wujianyong@hygon.cn \
--cc=yingzhiwei@hygon.cn \
--cc=yu.c.chen@intel.com \
--cc=zhongyuan@hygon.cn \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox