Re: [PATCH 3/4] sched: fix sched_numa_find_nth_cpu() in CPU-less case
From: Yicong Yang
Date: Mon Aug 14 2023 - 04:45:03 EST
Hi Yury,
On 2023/8/11 0:24, Yury Norov wrote:
> When the node provided by user is CPU-less, corresponding record in
> sched_domains_numa_masks is not set. Trying to dereference it in the
> following code leads to kernel crash.
>
> To avoid it, start searching from the nearest node with CPUs.
>
> Fixes: cd7f55359c90 ("sched: add sched_numa_find_nth_cpu()")
> Reported-by: Yicong Yang <yangyicong@xxxxxxxxxxxxx>
> Closes: https://lore.kernel.org/lkml/CAAH8bW8C5humYnfpW3y5ypwx0E-09A3QxFE1JFzR66v+mO4XfA@xxxxxxxxxxxxxx/T/
> Reported-by: Guenter Roeck <linux@xxxxxxxxxxxx>
> Closes: https://lore.kernel.org/lkml/ZMHSNQfv39HN068m@yury-ThinkPad/T/#mf6431cb0b7f6f05193c41adeee444bc95bf2b1c4
> Signed-off-by: Yury Norov <yury.norov@xxxxxxxxx>
> ---
>
> This has been discovered and fixed by Yicong Yang:
>
> https://lore.kernel.org/lkml/CAAH8bW8C5humYnfpW3y5ypwx0E-09A3QxFE1JFzR66v+mO4XfA@xxxxxxxxxxxxxx/T/
>
> When discovering Guenter's failure report for sparc64, I found it's due to
> the same problem. And while fixing, I found an opportunity to generalize
> nearest NUMA node search and avoid code duplication.
>
> Yicong, if you like this approach, please feel free to add your co-developed-by
> or any appropriate tags.
>
Looks fine to me. One nit below.
Reviewed-by: Yicong Yang <yangyicong@xxxxxxxxxxxxx>
> kernel/sched/topology.c | 6 +++++-
> 1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index d3a3b2646ec4..66b387172b6f 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -2113,10 +2113,14 @@ static int hop_cmp(const void *a, const void *b)
> */
> int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node)
> {
> - struct __cmp_key k = { .cpus = cpus, .node = node, .cpu = cpu };
> + struct __cmp_key k = { .cpus = cpus, .cpu = cpu };
> struct cpumask ***hop_masks;
> int hop, ret = nr_cpu_ids;
>
> + /* CPU-less node entries are uninitialized in sched_domains_numa_masks */
> + node = numa_nearest_node(node, N_CPU);
> + k.node = node;
> +
We may also have problem if node == NUMA_NO_NODE, is it better to mention this
in the function comment or check it before we going on? Currently this function
is only used in cpumask_local_spread() and the caller has already checked it, but
considering this is an export function so somebody may use it directly.
I wondering whether we should put this block within the protection of rcu_read_lock()
for some issues like hotplug or not. Is it possible if @node become CPU-less subsequently?
> rcu_read_lock();
>
> k.masks = rcu_dereference(sched_domains_numa_masks);
>