Re: [PATCH] sched/topology: Free NUMA masks on topology allocation failure

Tim Chen <[email protected]> Mon, 03 Aug 2026 18:15:08 -0700
Newsgroups org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On Fri, 2026-07-31 at 16:14 +0800, Fengyu Wang wrote:
> sched_init_numa() publishes sched_domains_numa_masks before it
> allocates the topology array.  When that allocation fails, the early
> return leaves the masks published while sched_domains_numa_levels is
> still zero: nothing dereferences them, but nothing can free them
> either, and the topology they were built for is never installed.
> Unpublish and free them instead.
>=20
> Fixes: cb83b629bae0 ("sched/numa: Rewrite the CONFIG_NUMA sched domain su=
pport")
> Signed-off-by: Fengyu Wang <[email protected]>
> ---
> Tested by hardcoding tl to NULL right after the kzalloc() to force the
> failure path; the masks are released and the machine boots normally.
>=20
>  kernel/sched/topology.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>=20
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index 622e2e01974c..208fdc52f52d 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -2403,8 +2403,17 @@ void sched_init_numa(int offline_node)
> =20
>  	tl =3D kzalloc((i + nr_levels + 1) *
>  			sizeof(struct sched_domain_topology_level), GFP_KERNEL);
> -	if (!tl)
> +	if (!tl) {
> +		rcu_assign_pointer(sched_domains_numa_masks, NULL);
> +		synchronize_rcu();
> +		for (i =3D 0; i < nr_levels; i++) {
> +			for_each_node(j)
> +				kfree(masks[i][j]);
> +			kfree(masks[i]);
> +		}
> +		kfree(masks);
>  		return;
> +	}

The code is cleaner without the synchronize_rcu() and set to null dance if =
we do
rcu_assign_pointer(sched_domains_numa_masks, masks);
after the tl check.

Thanks.

Tim

> =20
>  	/*
>  	 * Copy the default topology bits..