Re: [PATCH] sched/topology: Initialize sd_span after assignment to *sd

From: Nathan Chancellor

Date: Mon Mar 23 2026 - 18:43:20 EST


On Mon, Mar 23, 2026 at 10:36:27AM +0100, Peter Zijlstra wrote:
> Does this work?

Yes, that avoids the initial panic I reported.

Tested-by: Nathan Chancellor <nathan@xxxxxxxxxx>

> diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h
> index 51c29581f15e..defa86ed9b06 100644
> --- a/include/linux/sched/topology.h
> +++ b/include/linux/sched/topology.h
> @@ -153,7 +153,21 @@ struct sched_domain {
>
> static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
> {
> - return to_cpumask(sd->span);
> + /*
> + * Because C is an absolutely broken piece of shit, it is allowed for
> + * offsetof(*sd, span) < sizeof(*sd), this means that structure
> + * initialzation *sd = { ... }; which will clear every unmentioned
> + * member, can over-write the start of the flexible array member.
> + *
> + * Luckily, the way we allocate the flexible array is by:
> + *
> + * sizeof(*sd) + count * sizeof(*sd->span)
> + *
> + * this means that we have sufficient space for the whole flex array
> + * *outside* of sizeof(*sd). So use that, and avoid using sd->span.
> + */
> + unsigned long *bitmap = (void *)sd + sizeof(*sd);
> + return to_cpumask(bitmap);
> }
>
> extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],