Skip to content

Commit ebff7d8

Browse files
Yasuaki Ishimatsutorvalds
Yasuaki Ishimatsu
authored andcommitted
mem hotunplug: fix kfree() of bootmem memory
When hot removing memory presented at boot time, following messages are shown: kernel BUG at mm/slub.c:3409! invalid opcode: 0000 [hardkernel#1] SMP Modules linked in: ebtable_nat ebtables xt_CHECKSUM iptable_mangle bridge stp llc ipmi_devintf ipmi_msghandler sunrpc ipt_REJECT nf_conntrack_ipv4 nf_defrag_ipv4 iptable_filter ip_tables ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 xt_state nf_conntrack ip6table_filter ip6_tables binfmt_misc vfat fat dm_mirror dm_region_hash dm_log dm_mod vhost_net macvtap macvlan tun uinput iTCO_wdt iTCO_vendor_support coretemp kvm_intel kvm crc32c_intel ghash_clmulni_intel microcode pcspkr sg i2c_i801 lpc_ich mfd_core igb i2c_algo_bit i2c_core e1000e ptp pps_core tpm_infineon ioatdma dca sr_mod cdrom sd_mod crc_t10dif usb_storage megaraid_sas lpfc scsi_transport_fc scsi_tgt scsi_mod CPU 0 Pid: 5091, comm: kworker/0:2 Tainted: G W 3.9.0-rc6+ hardkernel#15 RIP: kfree+0x232/0x240 Process kworker/0:2 (pid: 5091, threadinfo ffff88084678c000, task ffff88083928ca80) Call Trace: __release_region+0xd4/0xe0 __remove_pages+0x52/0x110 arch_remove_memory+0x89/0xd0 remove_memory+0xc4/0x100 acpi_memory_device_remove+0x6d/0xb1 acpi_device_remove+0x89/0xab __device_release_driver+0x7c/0xf0 device_release_driver+0x2f/0x50 acpi_bus_device_detach+0x6c/0x70 acpi_ns_walk_namespace+0x11a/0x250 acpi_walk_namespace+0xee/0x137 acpi_bus_trim+0x33/0x7a acpi_bus_hot_remove_device+0xc4/0x1a1 acpi_os_execute_deferred+0x27/0x34 process_one_work+0x1f7/0x590 worker_thread+0x11a/0x370 kthread+0xee/0x100 ret_from_fork+0x7c/0xb0 RIP [<ffffffff811c41d2>] kfree+0x232/0x240 RSP <ffff88084678d968> The reason why the messages are shown is to release a resource structure, allocated by bootmem, by kfree(). So when we release a resource structure, we should check whether it is allocated by bootmem or not. But even if we know a resource structure is allocated by bootmem, we cannot release it since SLxB cannot treat it. So for reusing a resource structure, this patch remembers it by using bootmem_resource as follows: When releasing a resource structure by free_resource(), free_resource() checks whether the resource structure is allocated by bootmem or not. If it is allocated by bootmem, free_resource() adds it to bootmem_resource. If it is not allocated by bootmem, free_resource() release it by kfree(). And when getting a new resource structure by get_resource(), get_resource() checks whether bootmem_resource has released resource structures or not. If there is a released resource structure, get_resource() returns it. If there is not a releaed resource structure, get_resource() returns new resource structure allocated by kzalloc(). [akpm@linux-foundation.org: s/get_resource/alloc_resource/] Signed-off-by: Yasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com> Reviewed-by: Toshi Kani <toshi.kani@hp.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Ram Pai <linuxram@us.ibm.com> Cc: David Rientjes <rientjes@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9ca24e2 commit ebff7d8

File tree

1 file changed

+55
-13
lines changed

1 file changed

+55
-13
lines changed

Diff for: kernel/resource.c

+55-13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/seq_file.h>
2222
#include <linux/device.h>
2323
#include <linux/pfn.h>
24+
#include <linux/mm.h>
2425
#include <asm/io.h>
2526

2627

@@ -50,6 +51,14 @@ struct resource_constraint {
5051

5152
static DEFINE_RWLOCK(resource_lock);
5253

54+
/*
55+
* For memory hotplug, there is no way to free resource entries allocated
56+
* by boot mem after the system is up. So for reusing the resource entry
57+
* we need to remember the resource.
58+
*/
59+
static struct resource *bootmem_resource_free;
60+
static DEFINE_SPINLOCK(bootmem_resource_lock);
61+
5362
static void *r_next(struct seq_file *m, void *v, loff_t *pos)
5463
{
5564
struct resource *p = v;
@@ -151,6 +160,40 @@ __initcall(ioresources_init);
151160

152161
#endif /* CONFIG_PROC_FS */
153162

163+
static void free_resource(struct resource *res)
164+
{
165+
if (!res)
166+
return;
167+
168+
if (!PageSlab(virt_to_head_page(res))) {
169+
spin_lock(&bootmem_resource_lock);
170+
res->sibling = bootmem_resource_free;
171+
bootmem_resource_free = res;
172+
spin_unlock(&bootmem_resource_lock);
173+
} else {
174+
kfree(res);
175+
}
176+
}
177+
178+
static struct resource *alloc_resource(gfp_t flags)
179+
{
180+
struct resource *res = NULL;
181+
182+
spin_lock(&bootmem_resource_lock);
183+
if (bootmem_resource_free) {
184+
res = bootmem_resource_free;
185+
bootmem_resource_free = res->sibling;
186+
}
187+
spin_unlock(&bootmem_resource_lock);
188+
189+
if (res)
190+
memset(res, 0, sizeof(struct resource));
191+
else
192+
res = kzalloc(sizeof(struct resource), flags);
193+
194+
return res;
195+
}
196+
154197
/* Return the conflict entry if you can't request it */
155198
static struct resource * __request_resource(struct resource *root, struct resource *new)
156199
{
@@ -771,7 +814,7 @@ static void __init __reserve_region_with_split(struct resource *root,
771814
{
772815
struct resource *parent = root;
773816
struct resource *conflict;
774-
struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
817+
struct resource *res = alloc_resource(GFP_ATOMIC);
775818
struct resource *next_res = NULL;
776819

777820
if (!res)
@@ -796,7 +839,7 @@ static void __init __reserve_region_with_split(struct resource *root,
796839
/* conflict covered whole area */
797840
if (conflict->start <= res->start &&
798841
conflict->end >= res->end) {
799-
kfree(res);
842+
free_resource(res);
800843
WARN_ON(next_res);
801844
break;
802845
}
@@ -806,10 +849,9 @@ static void __init __reserve_region_with_split(struct resource *root,
806849
end = res->end;
807850
res->end = conflict->start - 1;
808851
if (conflict->end < end) {
809-
next_res = kzalloc(sizeof(*next_res),
810-
GFP_ATOMIC);
852+
next_res = alloc_resource(GFP_ATOMIC);
811853
if (!next_res) {
812-
kfree(res);
854+
free_resource(res);
813855
break;
814856
}
815857
next_res->name = name;
@@ -899,7 +941,7 @@ struct resource * __request_region(struct resource *parent,
899941
const char *name, int flags)
900942
{
901943
DECLARE_WAITQUEUE(wait, current);
902-
struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
944+
struct resource *res = alloc_resource(GFP_KERNEL);
903945

904946
if (!res)
905947
return NULL;
@@ -933,7 +975,7 @@ struct resource * __request_region(struct resource *parent,
933975
continue;
934976
}
935977
/* Uhhuh, that didn't work out.. */
936-
kfree(res);
978+
free_resource(res);
937979
res = NULL;
938980
break;
939981
}
@@ -967,7 +1009,7 @@ int __check_region(struct resource *parent, resource_size_t start,
9671009
return -EBUSY;
9681010

9691011
release_resource(res);
970-
kfree(res);
1012+
free_resource(res);
9711013
return 0;
9721014
}
9731015
EXPORT_SYMBOL(__check_region);
@@ -1007,7 +1049,7 @@ void __release_region(struct resource *parent, resource_size_t start,
10071049
write_unlock(&resource_lock);
10081050
if (res->flags & IORESOURCE_MUXED)
10091051
wake_up(&muxed_resource_wait);
1010-
kfree(res);
1052+
free_resource(res);
10111053
return;
10121054
}
10131055
p = &res->sibling;
@@ -1055,8 +1097,8 @@ int release_mem_region_adjustable(struct resource *parent,
10551097
if ((start < parent->start) || (end > parent->end))
10561098
return ret;
10571099

1058-
/* The kzalloc() result gets checked later */
1059-
new_res = kzalloc(sizeof(struct resource), GFP_KERNEL);
1100+
/* The alloc_resource() result gets checked later */
1101+
new_res = alloc_resource(GFP_KERNEL);
10601102

10611103
p = &parent->child;
10621104
write_lock(&resource_lock);
@@ -1083,7 +1125,7 @@ int release_mem_region_adjustable(struct resource *parent,
10831125
if (res->start == start && res->end == end) {
10841126
/* free the whole entry */
10851127
*p = res->sibling;
1086-
kfree(res);
1128+
free_resource(res);
10871129
ret = 0;
10881130
} else if (res->start == start && res->end != end) {
10891131
/* adjust the start */
@@ -1119,7 +1161,7 @@ int release_mem_region_adjustable(struct resource *parent,
11191161
}
11201162

11211163
write_unlock(&resource_lock);
1122-
kfree(new_res);
1164+
free_resource(new_res);
11231165
return ret;
11241166
}
11251167
#endif /* CONFIG_MEMORY_HOTREMOVE */

0 commit comments

Comments
 (0)