Skip to content

Commit

Permalink
mem-hotplug: introduce {un}lock_memory_hotplug()
Browse files Browse the repository at this point in the history
Presently hwpoison is using lock_system_sleep() to prevent a race with
memory hotplug.  However lock_system_sleep() is a no-op if
CONFIG_HIBERNATION=n.  Therefore we need a new lock.

Signed-off-by: KOSAKI Motohiro <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Kamezawa Hiroyuki <[email protected]>
Suggested-by: Hugh Dickins <[email protected]>
Acked-by: Hugh Dickins <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
kosaki authored and torvalds committed Dec 2, 2010
1 parent 4fe65ca commit 20d6c96
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
6 changes: 6 additions & 0 deletions include/linux/memory_hotplug.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ extern void register_page_bootmem_info_node(struct pglist_data *pgdat);
extern void put_page_bootmem(struct page *page);
#endif

void lock_memory_hotplug(void);
void unlock_memory_hotplug(void);

#else /* ! CONFIG_MEMORY_HOTPLUG */
/*
* Stub functions for when hotplug is off
Expand Down Expand Up @@ -192,6 +195,9 @@ static inline void register_page_bootmem_info_node(struct pglist_data *pgdat)
{
}

static inline void lock_memory_hotplug(void) {}
static inline void unlock_memory_hotplug(void) {}

#endif /* ! CONFIG_MEMORY_HOTPLUG */

#ifdef CONFIG_MEMORY_HOTREMOVE
Expand Down
8 changes: 4 additions & 4 deletions mm/memory-failure.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <linux/slab.h>
#include <linux/swapops.h>
#include <linux/hugetlb.h>
#include <linux/memory_hotplug.h>
#include "internal.h"

int sysctl_memory_failure_early_kill __read_mostly = 0;
Expand Down Expand Up @@ -1230,11 +1231,10 @@ static int get_any_page(struct page *p, unsigned long pfn, int flags)
return 1;

/*
* The lock_system_sleep prevents a race with memory hotplug,
* because the isolation assumes there's only a single user.
* The lock_memory_hotplug prevents a race with memory hotplug.
* This is a big hammer, a better would be nicer.
*/
lock_system_sleep();
lock_memory_hotplug();

/*
* Isolate the page, so that it doesn't get reallocated if it
Expand Down Expand Up @@ -1264,7 +1264,7 @@ static int get_any_page(struct page *p, unsigned long pfn, int flags)
ret = 1;
}
unset_migratetype_isolate(p);
unlock_system_sleep();
unlock_memory_hotplug();
return ret;
}

Expand Down
31 changes: 24 additions & 7 deletions mm/memory_hotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@

#include "internal.h"

DEFINE_MUTEX(mem_hotplug_mutex);

void lock_memory_hotplug(void)
{
mutex_lock(&mem_hotplug_mutex);

/* for exclusive hibernation if CONFIG_HIBERNATION=y */
lock_system_sleep();
}

void unlock_memory_hotplug(void)
{
unlock_system_sleep();
mutex_unlock(&mem_hotplug_mutex);
}


/* add this memory to iomem resource */
static struct resource *register_memory_resource(u64 start, u64 size)
{
Expand Down Expand Up @@ -493,7 +510,7 @@ int mem_online_node(int nid)
pg_data_t *pgdat;
int ret;

lock_system_sleep();
lock_memory_hotplug();
pgdat = hotadd_new_pgdat(nid, 0);
if (pgdat) {
ret = -ENOMEM;
Expand All @@ -504,7 +521,7 @@ int mem_online_node(int nid)
BUG_ON(ret);

out:
unlock_system_sleep();
unlock_memory_hotplug();
return ret;
}

Expand All @@ -516,7 +533,7 @@ int __ref add_memory(int nid, u64 start, u64 size)
struct resource *res;
int ret;

lock_system_sleep();
lock_memory_hotplug();

res = register_memory_resource(start, size);
ret = -EEXIST;
Expand Down Expand Up @@ -563,7 +580,7 @@ int __ref add_memory(int nid, u64 start, u64 size)
release_memory_resource(res);

out:
unlock_system_sleep();
unlock_memory_hotplug();
return ret;
}
EXPORT_SYMBOL_GPL(add_memory);
Expand Down Expand Up @@ -791,7 +808,7 @@ static int offline_pages(unsigned long start_pfn,
if (!test_pages_in_a_zone(start_pfn, end_pfn))
return -EINVAL;

lock_system_sleep();
lock_memory_hotplug();

zone = page_zone(pfn_to_page(start_pfn));
node = zone_to_nid(zone);
Expand Down Expand Up @@ -880,7 +897,7 @@ static int offline_pages(unsigned long start_pfn,
writeback_set_ratelimit();

memory_notify(MEM_OFFLINE, &arg);
unlock_system_sleep();
unlock_memory_hotplug();
return 0;

failed_removal:
Expand All @@ -891,7 +908,7 @@ static int offline_pages(unsigned long start_pfn,
undo_isolate_page_range(start_pfn, end_pfn);

out:
unlock_system_sleep();
unlock_memory_hotplug();
return ret;
}

Expand Down

0 comments on commit 20d6c96

Please sign in to comment.