Skip to content

Commit ebd1530

Browse files
hkamezawaLinus Torvalds
authored andcommitted
[PATCH] memory hotadd fixes: enhance collision check
This patch is for collision check enhancement for memory hot add. It's better to do resouce collision check before doing memory hot add, which will touch memory management structures. And add_section() should check section exists or not before calling sparse_add_one_section(). (sparse_add_one_section() will do another check anyway. but checking in memory_hotplug.c will be easy to understand.) Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: keith mannthey <kmannth@us.ibm.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 5d2870f commit ebd1530

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

‎mm/memory_hotplug.c‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ static int __add_section(struct zone *zone, unsigned long phys_start_pfn)
5252
int nr_pages = PAGES_PER_SECTION;
5353
int ret;
5454

55+
if (pfn_valid(phys_start_pfn))
56+
return -EEXIST;
57+
5558
ret = sparse_add_one_section(zone, phys_start_pfn, nr_pages);
5659

5760
if (ret < 0)
@@ -220,10 +223,9 @@ static void rollback_node_hotadd(int nid, pg_data_t *pgdat)
220223
}
221224

222225
/* add this memory to iomem resource */
223-
static int register_memory_resource(u64 start, u64 size)
226+
static struct resource *register_memory_resource(u64 start, u64 size)
224227
{
225228
struct resource *res;
226-
int ret = 0;
227229
res = kzalloc(sizeof(struct resource), GFP_KERNEL);
228230
BUG_ON(!res);
229231

@@ -235,9 +237,18 @@ static int register_memory_resource(u64 start, u64 size)
235237
printk("System RAM resource %llx - %llx cannot be added\n",
236238
(unsigned long long)res->start, (unsigned long long)res->end);
237239
kfree(res);
238-
ret = -EEXIST;
240+
res = NULL;
239241
}
240-
return ret;
242+
return res;
243+
}
244+
245+
static void release_memory_resource(struct resource *res)
246+
{
247+
if (!res)
248+
return;
249+
release_resource(res);
250+
kfree(res);
251+
return;
241252
}
242253

243254

@@ -246,8 +257,13 @@ int add_memory(int nid, u64 start, u64 size)
246257
{
247258
pg_data_t *pgdat = NULL;
248259
int new_pgdat = 0;
260+
struct resource *res;
249261
int ret;
250262

263+
res = register_memory_resource(start, size);
264+
if (!res)
265+
return -EEXIST;
266+
251267
if (!node_online(nid)) {
252268
pgdat = hotadd_new_pgdat(nid, start);
253269
if (!pgdat)
@@ -277,14 +293,13 @@ int add_memory(int nid, u64 start, u64 size)
277293
BUG_ON(ret);
278294
}
279295

280-
/* register this memory as resource */
281-
ret = register_memory_resource(start, size);
282-
283296
return ret;
284297
error:
285298
/* rollback pgdat allocation and others */
286299
if (new_pgdat)
287300
rollback_node_hotadd(nid, pgdat);
301+
if (res)
302+
release_memory_resource(res);
288303

289304
return ret;
290305
}

0 commit comments

Comments
 (0)