Skip to content

Commit 4eaf3f6

Browse files
haicheng-litorvalds
authored andcommitted
mem-hotplug: fix potential race while building zonelist for new populated zone
Add global mutex zonelists_mutex to fix the possible race: CPU0 CPU1 CPU2 (1) zone->present_pages += online_pages; (2) build_all_zonelists(); (3) alloc_page(); (4) free_page(); (5) build_all_zonelists(); (6) __build_all_zonelists(); (7) zone->pageset = alloc_percpu(); In step (3,4), zone->pageset still points to boot_pageset, so bad things may happen if 2+ nodes are in this state. Even if only 1 node is accessing the boot_pageset, (3) may still consume too much memory to fail the memory allocations in step (7). Besides, atomic operation ensures alloc_percpu() in step (7) will never fail since there is a new fresh memory block added in step(6). [haicheng.li@linux.intel.com: hold zonelists_mutex when build_all_zonelists] Signed-off-by: Haicheng Li <haicheng.li@linux.intel.com> Signed-off-by: Wu Fengguang <fengguang.wu@intel.com> Reviewed-by: Andi Kleen <andi.kleen@intel.com> Cc: Christoph Lameter <cl@linux-foundation.org> Cc: Mel Gorman <mel@csn.ul.ie> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1f52250 commit 4eaf3f6

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

‎include/linux/mmzone.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ typedef struct pglist_data {
650650

651651
#include <linux/memory_hotplug.h>
652652

653+
extern struct mutex zonelists_mutex;
653654
void get_zone_counts(unsigned long *active, unsigned long *inactive,
654655
unsigned long *free);
655656
void build_all_zonelists(void *data);

‎kernel/cpu.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,11 @@ int __cpuinit cpu_up(unsigned int cpu)
357357
return -ENOMEM;
358358
}
359359

360-
if (pgdat->node_zonelists->_zonerefs->zone == NULL)
360+
if (pgdat->node_zonelists->_zonerefs->zone == NULL) {
361+
mutex_lock(&zonelists_mutex);
361362
build_all_zonelists(NULL);
363+
mutex_unlock(&zonelists_mutex);
364+
}
362365
#endif
363366

364367
cpu_maps_update_begin();

‎mm/memory_hotplug.c‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,6 @@ int online_pages(unsigned long pfn, unsigned long nr_pages)
389389
int nid;
390390
int ret;
391391
struct memory_notify arg;
392-
/*
393-
* mutex to protect zone->pageset when it's still shared
394-
* in onlined_pages()
395-
*/
396-
static DEFINE_MUTEX(zone_pageset_mutex);
397392

398393
arg.start_pfn = pfn;
399394
arg.nr_pages = nr_pages;
@@ -420,14 +415,14 @@ int online_pages(unsigned long pfn, unsigned long nr_pages)
420415
* This means the page allocator ignores this zone.
421416
* So, zonelist must be updated after online.
422417
*/
423-
mutex_lock(&zone_pageset_mutex);
418+
mutex_lock(&zonelists_mutex);
424419
if (!populated_zone(zone))
425420
need_zonelists_rebuild = 1;
426421

427422
ret = walk_system_ram_range(pfn, nr_pages, &onlined_pages,
428423
online_pages_range);
429424
if (ret) {
430-
mutex_unlock(&zone_pageset_mutex);
425+
mutex_unlock(&zonelists_mutex);
431426
printk(KERN_DEBUG "online_pages %lx at %lx failed\n",
432427
nr_pages, pfn);
433428
memory_notify(MEM_CANCEL_ONLINE, &arg);
@@ -441,7 +436,7 @@ int online_pages(unsigned long pfn, unsigned long nr_pages)
441436
else
442437
zone_pcp_update(zone);
443438

444-
mutex_unlock(&zone_pageset_mutex);
439+
mutex_unlock(&zonelists_mutex);
445440
setup_per_zone_wmarks();
446441
calculate_zone_inactive_ratio(zone);
447442
if (onlined_pages) {

‎mm/page_alloc.c‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2571,8 +2571,11 @@ int numa_zonelist_order_handler(ctl_table *table, int write,
25712571
strncpy((char*)table->data, saved_string,
25722572
NUMA_ZONELIST_ORDER_LEN);
25732573
user_zonelist_order = oldval;
2574-
} else if (oldval != user_zonelist_order)
2574+
} else if (oldval != user_zonelist_order) {
2575+
mutex_lock(&zonelists_mutex);
25752576
build_all_zonelists(NULL);
2577+
mutex_unlock(&zonelists_mutex);
2578+
}
25762579
}
25772580
out:
25782581
mutex_unlock(&zl_order_mutex);
@@ -2924,6 +2927,12 @@ static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
29242927
static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
29252928
static void setup_zone_pageset(struct zone *zone);
29262929

2930+
/*
2931+
* Global mutex to protect against size modification of zonelists
2932+
* as well as to serialize pageset setup for the new populated zone.
2933+
*/
2934+
DEFINE_MUTEX(zonelists_mutex);
2935+
29272936
/* return values int ....just for stop_machine() */
29282937
static __init_refok int __build_all_zonelists(void *data)
29292938
{
@@ -2967,6 +2976,10 @@ static __init_refok int __build_all_zonelists(void *data)
29672976
return 0;
29682977
}
29692978

2979+
/*
2980+
* Called with zonelists_mutex held always
2981+
* unless system_state == SYSTEM_BOOTING.
2982+
*/
29702983
void build_all_zonelists(void *data)
29712984
{
29722985
set_zonelist_order();

0 commit comments

Comments
 (0)