Re: [PATCH] mm/memory_hotplug: fix kfree() of bootmem memory

From: Miaohe Lin
Date: Mon Feb 07 2022 - 20:59:25 EST


Hi:
On 2022/2/7 22:33, David Hildenbrand wrote:
> On 07.02.22 14:56, Miaohe Lin wrote:
>> We can't use kfree() to release the resource as it might come from bootmem.
>> Use release_mem_region() instead.
>
> How can this happen? release_mem_region() is called either from
> __add_memory() or from add_memory_driver_managed(), where we allocated
> the region via register_memory_resource(). Both functions shouldn't ever
> be called before the buddy is up an running.
>
> Do you have a backtrace of an actual instance of this issue? Or was this
> identified as possibly broken by code inspection?
>

This is identified as possibly broken by code inspection. IIUC, alloc_resource
is always used to allocate the resource. It has the below logic:

if (bootmem_resource_free) {
res = bootmem_resource_free;
bootmem_resource_free = res->sibling;
}

where bootmem_resource_free is used to reusing the resource entries allocated by boot
mem after the system is up:

/*
* For memory hotplug, there is no way to free resource entries allocated
* by boot mem after the system is up. So for reusing the resource entry
* we need to remember the resource.
*/
static struct resource *bootmem_resource_free;

So I think register_memory_resource() can reuse the resource allocated by bootmem.
Or am I miss anything?

Thanks.