Re: [PATCH v2 3/4] sched: add sched_numa_find_nth_cpu()

Valentin Schneider
2022-11-15T17:26:29Z

On 12/11/22 11:09, Yury Norov wrote:
> The function finds Nth set CPU in a given cpumask starting from a given
> node.
>
> Leveraging the fact that each hop in sched_domains_numa_masks includes the
> same or greater number of CPUs than the previous one, we can use binary
> search on hops instead of linear walk, which makes the overall complexity
> of O(log n) in terms of number of cpumask_weight() calls.
>

So one thing regarding the bsearch and NUMA levels; until not so long ago
we couldn't even support 3 hops [1], and this only got detected when such
machines started showing up.

Your bsearch here operates on NUMA levels, which represent hops, and so far
we know of systems that have up to 4 levels. I'd be surprised (and also
appalled) if we even doubled that in the next decade, so with that in mind,
a linear walk might not be so horrible.

[1]: https://lore.kernel.org/all/20210224030944.15232-1-song.bao.hua@hisilicon.com/

> Signed-off-by: Yury Norov <yury.norov@gmail.com>
> ---
> +int sched_numa_find_nth_cpu(const struct cpumask *cpus, int cpu, int node)
> +{
> + struct __cmp_key k = { cpus, NULL, node, cpu, 0 };
> + int hop, ret = nr_cpu_ids;
> +
> + rcu_read_lock();
> + k.masks = rcu_dereference(sched_domains_numa_masks);
> + if (!k.masks)
> + goto unlock;
> +
> + hop = (struct cpumask ***)
> + bsearch(&k, k.masks, sched_domains_numa_levels, sizeof(k.masks[0]), cmp) - k.masks;
> +
> + ret = hop ?
> + cpumask_nth_and_andnot(cpu - k.w, cpus, k.masks[hop][node], k.masks[hop-1][node]) :
> + cpumask_nth_and(cpu - k.w, cpus, k.masks[0][node]);
^^^
wouldn't this always be 0 here?

> +unlock:
> + rcu_read_unlock();
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(sched_numa_find_nth_cpu);
> #endif /* CONFIG_NUMA */
>
> static int __sdt_alloc(const struct cpumask *cpu_map)
> --
> 2.34.1