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

From: Andrea Righi

Date: Wed Apr 08 2026 - 13:40:56 EST


Hi Koba,

On Thu, Apr 09, 2026 at 01:23:12AM +0800, KobaK wrote:
> From: Koba Ko <kobak@xxxxxxxxxx>
>
> 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")
> Signed-off-by: Koba Ko <kobak@xxxxxxxxxx>
> ---
> kernel/sched/ext.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> index b757b853b42bb..0648088a76f09 100644
> --- a/kernel/sched/ext.c
> +++ 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;
> }
>
> --
> 2.43.0
>

Acked-by: Andrea Righi <arighi@xxxxxxxxxx>

Thanks,
-Andrea