Skip to contentSkip to conversation

sched/ext: fix cpumask leak in scx_init() error path

PATCH In review KobaK 4 participants 4 messages Apr 8 – Apr 9

Subsystems (2)
lore
Share & tools
lore

KobaK

author
#0 lore raw

PATCH 1/1 sched/ext: fix cpumask leak in scx_init() error path

From: Koba Ko <[email protected]>

In scx_init(), two cpumask allocations are combined with || short-circuit evaluation. If the first alloc_cpumask_var (scx_bypass_lb_donee_cpumask) succeeds but the second (scx_bypass_lb_resched_cpumask) fails, the first cpumask is leaked.

Split the allocations into separate checks with per-allocation error messages and free the first cpumask when the second allocation fails.

Fixes 95d1df610cdc7 ("sched_ext: Implement load balancer for bypass mode")
---
 kernel/sched/ext.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/sched/ext.c +8 -3
index b757b853b42bb..0648088a76f09 100644
@@ -9662,9 +9662,14 @@ static int __init scx_init(void)
 		return ret;
 	}
 
-	if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL) ||
-	    !alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
-		pr_err("sched_ext: Failed to allocate cpumasks\n");
+	if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL)) {
+		pr_err("sched_ext: Failed to allocate donee cpumask\n");
+		return -ENOMEM;
+	}
+
+	if (!alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
+		pr_err("sched_ext: Failed to allocate resched cpumask\n");
+		free_cpumask_var(scx_bypass_lb_donee_cpumask);
 		return -ENOMEM;
 	}
 
Signature · 1 line
2.43.0
reviewer Acked-by on v1 1/1
#1 lore raw

Hi Koba,

Quoted 25 lines · KobaK

From: Koba Ko <[email protected]>

In scx_init(), two cpumask allocations are combined with || short-circuit evaluation. If the first alloc_cpumask_var (scx_bypass_lb_donee_cpumask) succeeds but the second (scx_bypass_lb_resched_cpumask) fails, the first cpumask is leaked.

Split the allocations into separate checks with per-allocation error messages and free the first cpumask when the second allocation fails.

Fixes 95d1df610cdc7 ("sched_ext: Implement load balancer for bypass mode")
---
 kernel/sched/ext.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/sched/ext.c +0 -1
index b757b853b42bb..0648088a76f09 100644
@@ -9662,9 +9662,14 @@ static int __init scx_init(void)
 		return ret;
 	}
 
-	if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL) ||

kernel/sched/ext.c static int __init scx_init(void) · line 9672

Quoted 10 lines
kernel/sched/ext.c +8 -2
-	    !alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
-		pr_err("sched_ext: Failed to allocate cpumasks\n");
+	if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL)) {
+		pr_err("sched_ext: Failed to allocate donee cpumask\n");
+		return -ENOMEM;
+	}
+
+	if (!alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
+		pr_err("sched_ext: Failed to allocate resched cpumask\n");
+		free_cpumask_var(scx_bypass_lb_donee_cpumask);

We should probably do a proper teardown of the other stuff as well at some point (sysfs group, kset, PM notifier, etc.), but this looks good to me.

Quoted 6 lines
kernel/sched/ext.c +0 -0
 		return -ENOMEM;
 	}
 
Signature · 1 line
2.43.0

Thanks,
-Andrea

#2 lore raw

Hi Andrea, Koba,

Quoted 5 lines · Andrea Righi
Quoted 5 lines

+++ b/kernel/sched/ext.c

kernel/sched/ext.c +0 -0
@@ -9662,9 +9662,14 @@ static int __init scx_init(void)
 		return ret;
 	}
 
Quoted 17 lines

kernel/sched/ext.c static int __init scx_init(void) · line 9672

Quoted 11 lines
kernel/sched/ext.c +8 -3
-	if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL) ||
-	    !alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
-		pr_err("sched_ext: Failed to allocate cpumasks\n");
+	if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL)) {
+		pr_err("sched_ext: Failed to allocate donee cpumask\n");
+		return -ENOMEM;
+	}
+
+	if (!alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
+		pr_err("sched_ext: Failed to allocate resched cpumask\n");
+		free_cpumask_var(scx_bypass_lb_donee_cpumask);

We should probably do a proper teardown of the other stuff as well at some point (sysfs group, kset, PM notifier, etc.), but this looks good to me.

Quoted 2 lines
kernel/sched/ext.c +0 -0
 		return -ENOMEM;
 	}

Should kset_unregister() also be called in these error paths?

scx_kset is allocated earlier in scx_init() and none of the failure paths after that call kset_unregister()

	scx_kset = kset_create_and_add("sched_ext", &scx_uevent_ops, kernel_kobj);
	...
	ret = sysfs_create_group(&scx_kset->kobj, &scx_global_attr_group);
	if (ret < 0) {
		pr_err("sched_ext: Failed to add global attributes\n");
+		kset_unregister(scx_kset);
		return ret;	/* scx_kset leaked */
	}

    if (!alloc_cpumask_var(&scx_bypass_lb_donee_cpumask, GFP_KERNEL)) {
		pr_err("sched_ext: Failed to allocate donee cpumask\n");
+		kset_unregister(scx_kset);
		return -ENOMEM;	/* scx_kset leaked */
	}

	if (!alloc_cpumask_var(&scx_bypass_lb_resched_cpumask, GFP_KERNEL)) {
		pr_err("sched_ext: Failed to allocate resched cpumask\n");
		free_cpumask_var(scx_bypass_lb_donee_cpumask);
+		kset_unregister(scx_kset);
		return -ENOMEM;	/* scx_kset leaked */
	}
Signature · 2 lines
Thanks,
Cheng-Yang
reviewer
#3 lore raw
In reply to KobaK
Quoted 6 lines · KobaK

From: Koba Ko <[email protected]>

In scx_init(), two cpumask allocations are combined with || short-circuit evaluation. If the first alloc_cpumask_var (scx_bypass_lb_donee_cpumask) succeeds but the second (scx_bypass_lb_resched_cpumask) fails, the first cpumask is leaked.

This is an initcall. It runs once and failure is a kernel bug triggering a WARN. It won't fail and error handling is meaningless here. Once you fail one of these, the system is not in a good state as later code paths assume that these succeeded during boot. Not freeing is the least of our problems.

Thanks.

Signature · 1 line
tejun