Skip to content

Commit 0cbcc92

Browse files
MiaoheLintorvalds
authored andcommitted
kernel/resource: fix kfree() of bootmem memory again
Since commit ebff7d8 ("mem hotunplug: fix kfree() of bootmem memory"), we could get a resource allocated during boot via alloc_resource(). And it's required to release the resource using free_resource(). Howerver, many people use kfree directly which will result in kernel BUG. In order to fix this without fixing every call site, just leak a couple of bytes in such corner case. Link: https://lkml.kernel.org/r/[email protected] Fixes: ebff7d8 ("mem hotunplug: fix kfree() of bootmem memory") Signed-off-by: Miaohe Lin <[email protected]> Suggested-by: David Hildenbrand <[email protected]> Cc: Dan Williams <[email protected]> Cc: Alistair Popple <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b3d7fe8 commit 0cbcc92

File tree

1 file changed

+8
-33
lines changed

1 file changed

+8
-33
lines changed

kernel/resource.c

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ struct resource_constraint {
5656

5757
static DEFINE_RWLOCK(resource_lock);
5858

59-
/*
60-
* For memory hotplug, there is no way to free resource entries allocated
61-
* by boot mem after the system is up. So for reusing the resource entry
62-
* we need to remember the resource.
63-
*/
64-
static struct resource *bootmem_resource_free;
65-
static DEFINE_SPINLOCK(bootmem_resource_lock);
66-
6759
static struct resource *next_resource(struct resource *p)
6860
{
6961
if (p->child)
@@ -160,36 +152,19 @@ __initcall(ioresources_init);
160152

161153
static void free_resource(struct resource *res)
162154
{
163-
if (!res)
164-
return;
165-
166-
if (!PageSlab(virt_to_head_page(res))) {
167-
spin_lock(&bootmem_resource_lock);
168-
res->sibling = bootmem_resource_free;
169-
bootmem_resource_free = res;
170-
spin_unlock(&bootmem_resource_lock);
171-
} else {
155+
/**
156+
* If the resource was allocated using memblock early during boot
157+
* we'll leak it here: we can only return full pages back to the
158+
* buddy and trying to be smart and reusing them eventually in
159+
* alloc_resource() overcomplicates resource handling.
160+
*/
161+
if (res && PageSlab(virt_to_head_page(res)))
172162
kfree(res);
173-
}
174163
}
175164

176165
static struct resource *alloc_resource(gfp_t flags)
177166
{
178-
struct resource *res = NULL;
179-
180-
spin_lock(&bootmem_resource_lock);
181-
if (bootmem_resource_free) {
182-
res = bootmem_resource_free;
183-
bootmem_resource_free = res->sibling;
184-
}
185-
spin_unlock(&bootmem_resource_lock);
186-
187-
if (res)
188-
memset(res, 0, sizeof(struct resource));
189-
else
190-
res = kzalloc(sizeof(struct resource), flags);
191-
192-
return res;
167+
return kzalloc(sizeof(struct resource), flags);
193168
}
194169

195170
/* Return the conflict entry if you can't request it */

0 commit comments

Comments
 (0)