mm/sparse.c: improve the error handling for sparse_add_one_section()

Improve the error handling for mm/sparse.c::sparse_add_one_section(). And I
see no reason to check 'usemap' until holding the 'pgdat_resize_lock'.

[geoffrey.levand@am.sony.com: sparse_index_init() returns -EEXIST]
Cc: Christoph Lameter <clameter@sgi.com>
Acked-by: Dave Hansen <haveblue@us.ibm.com>
Cc: Rik van Riel <riel@redhat.com>
Acked-by: Yasunori Goto <y-goto@jp.fujitsu.com>
Cc: Andy Whitcroft <apw@shadowen.org>
Signed-off-by: WANG Cong <xiyou.wangcong@gmail.com>
Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

WANG Cong and committed by
Linus Torvalds
bbd06825 af0cd5a7

+12 -6
+12 -6
mm/sparse.c
··· 391 391 * no locking for this, because it does its own 392 392 * plus, it does a kmalloc 393 393 */ 394 - sparse_index_init(section_nr, pgdat->node_id); 394 + ret = sparse_index_init(section_nr, pgdat->node_id); 395 + if (ret < 0 && ret != -EEXIST) 396 + return ret; 395 397 memmap = kmalloc_section_memmap(section_nr, pgdat->node_id, nr_pages); 398 + if (!memmap) 399 + return -ENOMEM; 396 400 usemap = __kmalloc_section_usemap(); 401 + if (!usemap) { 402 + __kfree_section_memmap(memmap, nr_pages); 403 + return -ENOMEM; 404 + } 397 405 398 406 pgdat_resize_lock(pgdat, &flags); 399 407 ··· 411 403 goto out; 412 404 } 413 405 414 - if (!usemap) { 415 - ret = -ENOMEM; 416 - goto out; 417 - } 418 406 ms->section_mem_map |= SECTION_MARKED_PRESENT; 419 407 420 408 ret = sparse_init_one_section(ms, section_nr, memmap, usemap); 421 409 422 410 out: 423 411 pgdat_resize_unlock(pgdat, &flags); 424 - if (ret <= 0) 412 + if (ret <= 0) { 413 + kfree(usemap); 425 414 __kfree_section_memmap(memmap, nr_pages); 415 + } 426 416 return ret; 427 417 } 428 418 #endif