Re: [PATCH] sched/topology: convert NUMA distance masks allocation to kcalloc()

From: Valentin Schneider

Date: Mon Jun 15 2026 - 11:25:04 EST


On 05/06/26 14:24, William Theesfeld wrote:
> sched_init_numa() builds the per-level NUMA distance masks via an
> open-coded kzalloc(nr_node_ids * sizeof(void *), ...). Switch to
> kcalloc(), which carries the same zero-on-allocation semantics and
> adds the standard size_mul overflow check on the multiplication.
>
> nr_node_ids is bounded by MAX_NUMNODES at compile time, so the
> overflow check folds away in practice; the conversion is for
> consistency with the kernel-wide pattern and to keep kernel/sched/
> free of the open-coded form.
>

What about other simlar kzalloc() use just in that function, and at large
in the remainder of topology.c?

Coccinelle tells me there's a handful; surely that script can be improved
to automagically patch the code and DTRT.

---
virtual report

@kzalloc@
expression E1, E2;
position p;
@@

* kzalloc@p(E1 * E2, ...)

@script:python@
p << kzalloc.p;
@@

msg = "WARNING: kzalloc() with multiplication should be kcalloc()"
coccilib.report.print_report(p[0], msg)
---
$ /usr/bin/spatch -D report --no-show-diff --very-quiet --cocci-file ./scripts/coccinelle/api/kzalloc_to_kcalloc.cocci --no-includes --include-headers -I ./arch/x86/include -I ./arch/x86/include/generated -I ./include -I ./include -I ./arch/x86/include/uapi -I ./arch/x86/include/generated/uapi -I ./include/uapi -I ./include/generated/uapi --include ./include/linux/compiler-version.h --include ./include/linux/kconfig.h --dir kernel/sched

5 files match
kernel/sched/topology.c:2363:9-16: WARNING: kzalloc() with multiplication should be kcalloc()
kernel/sched/topology.c:2372:13-20: WARNING: kzalloc() with multiplication should be kcalloc()
kernel/sched/topology.c:2404:6-13: WARNING: kzalloc() with multiplication should be kcalloc()