Re: [PATCH] sched/ext: fix cpumask leak in scx_init() error path

From: Cheng-Yang Chou

Date: Wed Apr 08 2026 - 15:18:58 EST


Hi Andrea, Koba,

On Wed, Apr 08, 2026 at 07:34:19PM +0200, Andrea Righi wrote:
> > +++ b/kernel/sched/ext.c
> > @@ -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);
>
> 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.
>
> > 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 */
}
--
Thanks,
Cheng-Yang