Skip to contentSkip to conversation

sched/topology: convert NUMA distance masks allocation to kcalloc()

PATCH In review William Theesfeld 2 participants 2 messages Jun 5 – Jun 15 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/topology: convert NUMA distance masks allocation to kcalloc()

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.

No functional change.

---
 kernel/sched/topology.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched/topology.c +1 -1
index 5847b83d9..bb900ae22 100644
@@ -2083,7 +2083,7 @@ void sched_init_numa(int offline_node)
 	 * CPUs of nodes that are that many hops away from us.
 	 */
 	for (i = 0; i < nr_levels; i++) {
-		masks[i] = kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
+		masks[i] = kcalloc(nr_node_ids, sizeof(void *), GFP_KERNEL);
 		if (!masks[i])
 			return;
 
Signature · 1 line
2.54.0
#1 lore raw
Quoted 10 lines · William Theesfeld

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()