[RFC PATCH 04/11] mm: Add memory hotplug handlers

From: Toshi Kani
Date: Wed Dec 12 2012 - 18:27:05 EST


Added memory hotplug handlers. mm_add_execute() onlines requested
memory ranges for hot-add and online operations, and mm_del_execute()
offlines them for hot-delete and offline operations. They are also
used for rollback as well.

mm_del_validate() fails a request if a requested memory range is
non-movable for delete. This check can be removed if we should
attempt to delete such range anyway (but can cause a rollback).

Signed-off-by: Toshi Kani <toshi.kani@xxxxxx>
---
mm/memory_hotplug.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 97 insertions(+)

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index e4eeaca..107a39d 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -29,6 +29,7 @@
#include <linux/suspend.h>
#include <linux/mm_inline.h>
#include <linux/firmware-map.h>
+#include <linux/hotplug.h>

#include <asm/tlbflush.h>

@@ -45,6 +46,9 @@ static void generic_online_page(struct page *page);

static online_page_callback_t online_page_callback = generic_online_page;

+static int mm_add_execute(struct hp_request *req, int rollback);
+static int mm_del_execute(struct hp_request *req, int rollback);
+
DEFINE_MUTEX(mem_hotplug_mutex);

void lock_memory_hotplug(void)
@@ -1055,3 +1059,96 @@ int remove_memory(u64 start, u64 size)
}
#endif /* CONFIG_MEMORY_HOTREMOVE */
EXPORT_SYMBOL_GPL(remove_memory);
+
+static int mm_add_execute(struct hp_request *req, int rollback)
+{
+ struct hp_device *hp_dev;
+ struct hp_memory *hp_mem;
+ int ret;
+
+ if (rollback)
+ return mm_del_execute(req, 0);
+
+ list_for_each_entry(hp_dev, &req->dev_list, list) {
+ if (hp_dev->class != HP_CLS_MEMORY)
+ continue;
+
+ hp_mem = &hp_dev->data.mem;
+
+ ret = add_memory(hp_mem->node,
+ hp_mem->start_addr, hp_mem->length);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int mm_del_validate(struct hp_request *req, int rollback)
+{
+ struct hp_device *hp_dev;
+ struct hp_memory *hp_mem;
+ unsigned long start_pfn, nr_pages;
+
+ if (rollback)
+ return 0;
+
+ list_for_each_entry(hp_dev, &req->dev_list, list) {
+ if (hp_dev->class != HP_CLS_MEMORY)
+ continue;
+
+ hp_mem = &hp_dev->data.mem;
+ start_pfn = hp_mem->start_addr >> PAGE_SHIFT;
+ nr_pages = PAGE_ALIGN(hp_mem->length) >> PAGE_SHIFT;
+
+ /*
+ * Check if this memory range is removable. This check can
+ * be removed if we should attempt to delete a non-movable
+ * range.
+ */
+ if (is_mem_section_removable(start_pfn, nr_pages)) {
+ pr_info("Memory [%#010llx-%#010llx] not removable\n",
+ hp_mem->start_addr,
+ hp_mem->start_addr + hp_mem->length-1);
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
+static int mm_del_execute(struct hp_request *req, int rollback)
+{
+ struct hp_device *hp_dev;
+ struct hp_memory *hp_mem;
+ int ret;
+
+ if (rollback)
+ return mm_add_execute(req, 0);
+
+ list_for_each_entry(hp_dev, &req->dev_list, list) {
+ if (hp_dev->class != HP_CLS_MEMORY)
+ continue;
+
+ hp_mem = &hp_dev->data.mem;
+
+ ret = remove_memory(hp_mem->start_addr, hp_mem->length);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+static int __init mm_hp_init(void)
+{
+ hp_register_handler(HP_ADD_EXECUTE, mm_add_execute,
+ HP_MEM_ADD_EXECUTE_ORDER);
+ hp_register_handler(HP_DEL_VALIDATE, mm_del_validate,
+ HP_MEM_DEL_VALIDATE_ORDER);
+ hp_register_handler(HP_DEL_EXECUTE, mm_del_execute,
+ HP_MEM_DEL_EXECUTE_ORDER);
+
+ return 0;
+}
+module_init(mm_hp_init);
--
1.7.11.7

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/