Re: [PATCH] sched/isolation: Defer freeing of the bootmem housekeeping cpumasks

From: Mike Rapoport

Date: Mon Aug 03 2026 - 02:58:59 EST


On Sun, Aug 02, 2026 at 02:56:23PM +0300, Ionut Nechita wrote:
> housekeeping_setup() allocates the housekeeping cpumasks from memblock
> while parsing the command line, long before the page allocator exists.
> housekeeping_init() then reallocates them with kmalloc(), so that a later
> runtime update can free the old mask with kfree(), and releases the
> memblock allocations with memblock_free().
>
> That release is not safe where it currently sits. housekeeping_init() is
> called from start_kernel() after mm_core_init(), so slab_is_available()
> is already true and memblock_phys_free() takes the __free_reserved_area()
> path. But it is still called long before page_alloc_init_late(), so with
> CONFIG_DEFERRED_STRUCT_PAGE_INIT=y the deferred part of the memory map is
> not initialized yet, and __free_reserved_area() refuses to touch it:
>
> Record the bootmem masks instead and release them from a core_initcall,
> which runs after page_alloc_init_late() has initialized the deferred
> memory map. early_initcall() would still be too early: kernel_init_freeable()
> runs do_pre_smp_initcalls() before page_alloc_init_late().
>
> Masks that housekeeping_init() did not manage to replace, because
> kmalloc() failed, are never recorded and therefore stay live, preserving
> the existing error behaviour.
>
> Fixes: 27c3a5967f05 ("sched/isolation: Convert housekeeping cpumasks to rcu pointers")
> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=221804
> Link: https://lore.kernel.org/linux-mm/20260728134016.674388f101f141362598240f@xxxxxxxxxxxxxxxxxxxx/
> Suggested-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>
> Signed-off-by: Ionut Nechita <ionut.nechita@xxxxxxxxxxxxx>

Acked-by: Mike Rapoport (Microsoft) <rppt@xxxxxxxxxx>

Some nits below.

> ---
> kernel/sched/isolation.c | 24 +++++++++++++++++++++++-
> 1 file changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/sched/isolation.c b/kernel/sched/isolation.c
> index ef152d401fe2..5c36e33bac50 100644
> --- a/kernel/sched/isolation.c
> +++ b/kernel/sched/isolation.c
> @@ -28,6 +28,17 @@ struct housekeeping {
>
> static struct housekeeping housekeeping;
>
> +/*
> + * Bootmem cpumasks that housekeeping_init() has replaced with kmalloc()ed

Nit: s/has replaced/replaces

> + * copies. They can't be released right there: housekeeping_init() runs once
> + * the page allocator and slab are up, but before page_alloc_init_late() has
> + * initialized the deferred part of the memory map, and memblock_free() then
> + * reaches __free_reserved_area(), which refuses to touch a memory map that
> + * isn't fully initialized yet. Record them instead and release them from a
> + * core_initcall, which runs after page_alloc_init_late().

And I'd split it into 3 paragraphs :)

- the first sentence
- description why that memory can't be released in housekeeping_init()
- Record them ...

> + */
> +static struct cpumask *housekeeping_bootmem_masks[HK_TYPE_MAX] __initdata;
> +
> bool housekeeping_enabled(enum hk_type type)
> {
> return !!(READ_ONCE(housekeeping.flags) & BIT(type));
> @@ -189,10 +200,21 @@ void __init housekeeping_init(void)
> WARN_ON_ONCE(cpumask_empty(omask));
> cpumask_copy(nmask, omask);
> RCU_INIT_POINTER(housekeeping.cpumasks[type], nmask);
> - memblock_free(omask, cpumask_size());
> + housekeeping_bootmem_masks[type] = omask;
> }
> }
>
> +static int __init housekeeping_free_bootmem_masks(void)
> +{
> + enum hk_type type;
> +
> + for (type = 0; type < HK_TYPE_MAX; type++)
> + memblock_free(housekeeping_bootmem_masks[type], cpumask_size());
> +
> + return 0;
> +}
> +core_initcall(housekeeping_free_bootmem_masks);
> +
> static void __init housekeeping_setup_type(enum hk_type type,
> cpumask_var_t housekeeping_staging)
> {
>
> base-commit: 2d2338c93da79b3bfe4b6099a931d9468d539952
> --
> 2.55.0
>

--
Sincerely yours,
Mike.