From: Keith Mannthey Introduce and implement valid_add_memory_range to MEMORY_HOTPLUG. The RESERVE path needs to be careful about check to make sure it only onlines correct memory. The SPASRMEM path is using resource code to do this so it gets a no-op check. This frame work makes it was to add specific checks for valid add memory ranges as the need arises. Signed-off-by: Keith Mannthey Cc: KAMEZAWA Hiroyuki Cc: Andi Kleen Signed-off-by: Andrew Morton --- arch/x86_64/mm/srat.c | 6 ++++++ include/linux/memory_hotplug.h | 2 +- mm/memory_hotplug.c | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff -puN arch/x86_64/mm/srat.c~hot-add-mem-x86_64-valid-add-range-check arch/x86_64/mm/srat.c --- a/arch/x86_64/mm/srat.c~hot-add-mem-x86_64-valid-add-range-check +++ a/arch/x86_64/mm/srat.c @@ -205,6 +205,12 @@ static inline int save_add_info(void) { return hotadd_percent > 0; } + +inline int valid_add_memory_range(int nid, u64 start, u64 size) { + if (nodes_add[nid].start <= start && nodes_add[nid].end >= (start+size)) + return 1; + return 0; +} #else int update_end_of_memory(unsigned long end) {return 0;} static int hotadd_enough_memory(struct bootnode *nd) {return 1;} diff -puN include/linux/memory_hotplug.h~hot-add-mem-x86_64-valid-add-range-check include/linux/memory_hotplug.h --- a/include/linux/memory_hotplug.h~hot-add-mem-x86_64-valid-add-range-check +++ a/include/linux/memory_hotplug.h @@ -159,7 +159,7 @@ static inline int mhp_notimplemented(con dump_stack(); return -ENOSYS; } - +extern int valid_add_memory_range(int nid, u64 start, u64 size); #endif /* ! CONFIG_MEMORY_HOTPLUG */ static inline int __remove_pages(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages) diff -puN mm/memory_hotplug.c~hot-add-mem-x86_64-valid-add-range-check mm/memory_hotplug.c --- a/mm/memory_hotplug.c~hot-add-mem-x86_64-valid-add-range-check +++ a/mm/memory_hotplug.c @@ -190,6 +190,11 @@ int online_pages(unsigned long pfn, unsi vm_total_pages = nr_free_pagecache_pages(); return 0; } + +inline int valid_add_memory_range(int nid, u64 start, u64 size) { + return 1; +} + #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */ static pg_data_t *hotadd_new_pgdat(int nid, u64 start) @@ -258,6 +263,9 @@ int add_memory(int nid, u64 start, u64 s struct resource *res; int ret; + if (!valid_add_memory_range(nid,start,size)) + return -EINVAL; + res = register_memory_resource(start, size); if (!res) return -EEXIST; _