Commit e90bdb7f authored by Wen Congyang's avatar Wen Congyang Committed by Linus Torvalds

memory-hotplug: update memory block's state and notify userspace

remove_memory() will be called when hot removing a memory device.  But
even if offlining memory, we cannot notice it.  So the patch updates the
memory block's state and sends notification to userspace.

Additionally, the memory device may contain more than one memory block.
If the memory block has been offlined, __offline_pages() will fail.  So we
should try to offline one memory block at a time.

Thus remove_memory() also check each memory block's state.  So there is no
need to check the memory block's state before calling remove_memory().
Signed-off-by: default avatarWen Congyang <wency@cn.fujitsu.com>
Signed-off-by: default avatarYasuaki Ishimatsu <isimatu.yasuaki@jp.fujitsu.com>
Cc: David Rientjes <rientjes@google.com>
Cc: Jiang Liu <liuj97@gmail.com>
Cc: Len Brown <len.brown@intel.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Minchan Kim <minchan.kim@gmail.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent a16cee10
...@@ -275,13 +275,11 @@ memory_block_action(unsigned long phys_index, unsigned long action) ...@@ -275,13 +275,11 @@ memory_block_action(unsigned long phys_index, unsigned long action)
return ret; return ret;
} }
static int memory_block_change_state(struct memory_block *mem, static int __memory_block_change_state(struct memory_block *mem,
unsigned long to_state, unsigned long from_state_req) unsigned long to_state, unsigned long from_state_req)
{ {
int ret = 0; int ret = 0;
mutex_lock(&mem->state_mutex);
if (mem->state != from_state_req) { if (mem->state != from_state_req) {
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
...@@ -309,10 +307,20 @@ static int memory_block_change_state(struct memory_block *mem, ...@@ -309,10 +307,20 @@ static int memory_block_change_state(struct memory_block *mem,
break; break;
} }
out: out:
mutex_unlock(&mem->state_mutex);
return ret; return ret;
} }
static int memory_block_change_state(struct memory_block *mem,
unsigned long to_state, unsigned long from_state_req)
{
int ret;
mutex_lock(&mem->state_mutex);
ret = __memory_block_change_state(mem, to_state, from_state_req);
mutex_unlock(&mem->state_mutex);
return ret;
}
static ssize_t static ssize_t
store_mem_state(struct device *dev, store_mem_state(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count) struct device_attribute *attr, const char *buf, size_t count)
...@@ -652,6 +660,21 @@ int unregister_memory_section(struct mem_section *section) ...@@ -652,6 +660,21 @@ int unregister_memory_section(struct mem_section *section)
return remove_memory_block(0, section, 0); return remove_memory_block(0, section, 0);
} }
/*
* offline one memory block. If the memory block has been offlined, do nothing.
*/
int offline_memory_block(struct memory_block *mem)
{
int ret = 0;
mutex_lock(&mem->state_mutex);
if (mem->state != MEM_OFFLINE)
ret = __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
mutex_unlock(&mem->state_mutex);
return ret;
}
/* /*
* Initialize the sysfs support for memory devices... * Initialize the sysfs support for memory devices...
*/ */
......
...@@ -10,6 +10,7 @@ struct page; ...@@ -10,6 +10,7 @@ struct page;
struct zone; struct zone;
struct pglist_data; struct pglist_data;
struct mem_section; struct mem_section;
struct memory_block;
#ifdef CONFIG_MEMORY_HOTPLUG #ifdef CONFIG_MEMORY_HOTPLUG
...@@ -234,6 +235,7 @@ extern int mem_online_node(int nid); ...@@ -234,6 +235,7 @@ extern int mem_online_node(int nid);
extern int add_memory(int nid, u64 start, u64 size); extern int add_memory(int nid, u64 start, u64 size);
extern int arch_add_memory(int nid, u64 start, u64 size); extern int arch_add_memory(int nid, u64 start, u64 size);
extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages); extern int offline_pages(unsigned long start_pfn, unsigned long nr_pages);
extern int offline_memory_block(struct memory_block *mem);
extern int remove_memory(u64 start, u64 size); extern int remove_memory(u64 start, u64 size);
extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn, extern int sparse_add_one_section(struct zone *zone, unsigned long start_pfn,
int nr_pages); int nr_pages);
......
...@@ -1014,11 +1014,42 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages) ...@@ -1014,11 +1014,42 @@ int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
int remove_memory(u64 start, u64 size) int remove_memory(u64 start, u64 size)
{ {
struct memory_block *mem = NULL;
struct mem_section *section;
unsigned long start_pfn, end_pfn; unsigned long start_pfn, end_pfn;
unsigned long pfn, section_nr;
int ret;
start_pfn = PFN_DOWN(start); start_pfn = PFN_DOWN(start);
end_pfn = start_pfn + PFN_DOWN(size); end_pfn = start_pfn + PFN_DOWN(size);
return __offline_pages(start_pfn, end_pfn, 120 * HZ);
for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
section_nr = pfn_to_section_nr(pfn);
if (!present_section_nr(section_nr))
continue;
section = __nr_to_section(section_nr);
/* same memblock? */
if (mem)
if ((section_nr >= mem->start_section_nr) &&
(section_nr <= mem->end_section_nr))
continue;
mem = find_memory_block_hinted(section, mem);
if (!mem)
continue;
ret = offline_memory_block(mem);
if (ret) {
kobject_put(&mem->dev.kobj);
return ret;
}
}
if (mem)
kobject_put(&mem->dev.kobj);
return 0;
} }
#else #else
int offline_pages(unsigned long start_pfn, unsigned long nr_pages) int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment