Skip to contentSkip to conversation

sched/topology: Check return value of zalloc_cpumask_var()

PATCH In review Daniil Dulov 2 participants 2 messages Mar 7, 2024 – Mar 14, 2024 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched/topology: Check return value of zalloc_cpumask_var()

zalloc_cpumask_var() returns false if mask was not allocated. So, let's check if cpu masks are allocated successfully.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 8d5dc5126bb2 ("sched/topology: Small cleanup")
---
 kernel/sched/topology.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/sched/topology.c +4 -3
index 10d1391e7416..2df98ffdfe16 100644
@@ -2600,9 +2600,10 @@ int __init sched_init_domains(const struct cpumask *cpu_map)
 {
 	int err;
 
-	zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
-	zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
-	zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
+	if (!zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL) ||
+	    !zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL) ||
+	    !zalloc_cpumask_var(&fallback_doms, GFP_KERNEL))
+		return -ENOMEM;
 
 	arch_update_cpu_topology();
 	asym_cpu_capacity_scan();
Signature · 1 line
2.25.1
#1 lore raw
Quoted 16 lines · Daniil Dulov

zalloc_cpumask_var() returns false if mask was not allocated. So, let's check if cpu masks are allocated successfully.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 8d5dc5126bb2 ("sched/topology: Small cleanup")
---
 kernel/sched/topology.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/sched/topology.c +0 -0
index 10d1391e7416..2df98ffdfe16 100644

kernel/sched/topology.c int __init sched_init_domains(const struct cpumask *cpu_map) · line 2606

Quoted 10 lines
kernel/sched/topology.c +4 -3
@@ -2600,9 +2600,10 @@ int __init sched_init_domains(const struct cpumask *cpu_map)
 {
      int err;
 
-	zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL);
-	zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL);
-	zalloc_cpumask_var(&fallback_doms, GFP_KERNEL);
+	if (!zalloc_cpumask_var(&sched_domains_tmpmask, GFP_KERNEL) ||
+	    !zalloc_cpumask_var(&sched_domains_tmpmask2, GFP_KERNEL) ||
+	    !zalloc_cpumask_var(&fallback_doms, GFP_KERNEL))
+		return -ENOMEM;

Despite returning an int, sched_init_domains()' return value is ignored by sched_init_smp()

The int return was added in 2006 by:
51888ca25a03 ("[PATCH] sched_domain: handle kmalloc failure")
but it looks like sched_init_smp() never checked it.

Now, allocation failures this early in init really isn't good and more things are bound to explode, but I suppose you could add a return value check in sched_init_smp() and WARN on anything non-zero. Without sched_domain's, we'll still have the boot CPU doing useful work.

Quoted 5 lines
kernel/sched/topology.c +0 -1
      arch_update_cpu_topology();
      asym_cpu_capacity_scan();
--

2.25.1