Skip to contentSkip to conversation

x86/mm: Tear down the direct map when memory hot add fails

PATCH In review Chris Down 6 participants 6 messages Nov 8 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Chris Down

author
#0 lore raw

PATCH 1/1 x86/mm: Tear down the direct map when memory hot add fails

arch_add_memory() sets up the direct map before calling add_pages(). If add_pages() fails, the range stays mapped and any page tables allocated for it are leaked. A later hot add can reuse the stale mapping.

Remove the direct mapping on failure.

Fixes 45e0b78b0532 ("[PATCH] hot-add-mem x86_64: use CONFIG_MEMORY_HOTPLUG_RESERVE")
---
 arch/x86/mm/init_64.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c +9 -1
index 8b6d86fb5a42..846cb34b38dd 100644
@@ -989,15 +989,23 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
 	return ret;
 }
 
+static void __meminit kernel_physical_mapping_remove(unsigned long start,
+						     unsigned long end);
+
 int arch_add_memory(int nid, u64 start, u64 size,
 		    struct mhp_params *params)
 {
 	unsigned long start_pfn = start >> PAGE_SHIFT;
 	unsigned long nr_pages = size >> PAGE_SHIFT;
+	int ret;
 
 	init_memory_mapping(start, start + size, params->pgprot);
 
-	return add_pages(nid, start_pfn, nr_pages, params);
+	ret = add_pages(nid, start_pfn, nr_pages, params);
+	if (ret)
+		kernel_physical_mapping_remove(start, start + size);
+
+	return ret;
 }
 
 static void __meminit free_pagetable(struct page *page)

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Chris Down

arch_add_memory() sets up the direct map before calling add_pages(). If add_pages() fails, the range stays mapped and any page tables allocated for it are leaked. A later hot add can reuse the stale mapping.

Remove the direct mapping on failure.

Fixes 45e0b78b0532 ("[PATCH] hot-add-mem x86_64: use CONFIG_MEMORY_HOTPLUG_RESERVE")

---

Looks good to me,

Quoted 34 lines
 arch/x86/mm/init_64.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c +9 -1
index 8b6d86fb5a42..846cb34b38dd 100644
@@ -989,15 +989,23 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
 	return ret;
 }
 
+static void __meminit kernel_physical_mapping_remove(unsigned long start,
+						     unsigned long end);
+
 int arch_add_memory(int nid, u64 start, u64 size,
 		    struct mhp_params *params)
 {
 	unsigned long start_pfn = start >> PAGE_SHIFT;
 	unsigned long nr_pages = size >> PAGE_SHIFT;
+	int ret;
 
 	init_memory_mapping(start, start + size, params->pgprot);
 
-	return add_pages(nid, start_pfn, nr_pages, params);
+	ret = add_pages(nid, start_pfn, nr_pages, params);
+	if (ret)
+		kernel_physical_mapping_remove(start, start + size);
+
+	return ret;
 }
 
 static void __meminit free_pagetable(struct page *page)

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

Signature · 2 lines
Cheers,
Harry / Hyeonggon
Acked-by on v1 1/1
#2 lore raw
In reply to Chris Down
Quoted 9 lines · Chris Down

arch_add_memory() sets up the direct map before calling add_pages(). If add_pages() fails, the range stays mapped and any page tables allocated for it are leaked. A later hot add can reuse the stale mapping.

Quoted 4 lines

Remove the direct mapping on failure.

Fixes 45e0b78b0532 ("[PATCH] hot-add-mem x86_64: use CONFIG_MEMORY_HOTPLUG_RESERVE")

Thanks.

Acked-by on v1 1/1
#3 lore raw
In reply to Chris Down
Quoted 9 lines · Chris Down

arch_add_memory() sets up the direct map before calling add_pages(). If add_pages() fails, the range stays mapped and any page tables allocated for it are leaked. A later hot add can reuse the stale mapping.

Remove the direct mapping on failure.

Fixes 45e0b78b0532 ("[PATCH] hot-add-mem x86_64: use CONFIG_MEMORY_HOTPLUG_RESERVE")
Signature · 2 lines
Oscar Salvador
SUSE Labs
Acked-by on v1 1/1
#4 lore raw
In reply to Chris Down
Quoted 43 lines · Chris Down

arch_add_memory() sets up the direct map before calling add_pages(). If add_pages() fails, the range stays mapped and any page tables allocated for it are leaked. A later hot add can reuse the stale mapping.

Remove the direct mapping on failure.

Fixes 45e0b78b0532 ("[PATCH] hot-add-mem x86_64: use CONFIG_MEMORY_HOTPLUG_RESERVE")
---
 arch/x86/mm/init_64.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c +9 -1
index 8b6d86fb5a42..846cb34b38dd 100644
@@ -989,15 +989,23 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
 	return ret;
 }
 
+static void __meminit kernel_physical_mapping_remove(unsigned long start,
+						     unsigned long end);
+
 int arch_add_memory(int nid, u64 start, u64 size,
 		    struct mhp_params *params)
 {
 	unsigned long start_pfn = start >> PAGE_SHIFT;
 	unsigned long nr_pages = size >> PAGE_SHIFT;
+	int ret;
 
 	init_memory_mapping(start, start + size, params->pgprot);
 
-	return add_pages(nid, start_pfn, nr_pages, params);
+	ret = add_pages(nid, start_pfn, nr_pages, params);
+	if (ret)
+		kernel_physical_mapping_remove(start, start + size);
+
+	return ret;
 }
 
 static void __meminit free_pagetable(struct page *page)
Quoted 1 line

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

I thought we fixed that already, but I remembered

https://lore.kernel.org/all/[email protected]/

Signature · 3 lines
Cheers,

David
Reviewed-by
#5 lore raw
Quoted 42 lines · David Hildenbrand (Arm)
Quoted 41 lines · Chris Down

arch_add_memory() sets up the direct map before calling add_pages(). If add_pages() fails, the range stays mapped and any page tables allocated for it are leaked. A later hot add can reuse the stale mapping.

Remove the direct mapping on failure.

Fixes 45e0b78b0532 ("[PATCH] hot-add-mem x86_64: use CONFIG_MEMORY_HOTPLUG_RESERVE")
---
  arch/x86/mm/init_64.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
arch/x86/mm/init_64.c +9 -1
index 8b6d86fb5a42..846cb34b38dd 100644
@@ -989,15 +989,23 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
  	return ret;
  }
 
+static void __meminit kernel_physical_mapping_remove(unsigned long start,
+						     unsigned long end);
+
  int arch_add_memory(int nid, u64 start, u64 size,
  		    struct mhp_params *params)
  {
  	unsigned long start_pfn = start >> PAGE_SHIFT;
  	unsigned long nr_pages = size >> PAGE_SHIFT;
+	int ret;
 
  	init_memory_mapping(start, start + size, params->pgprot);
 
-	return add_pages(nid, start_pfn, nr_pages, params);
+	ret = add_pages(nid, start_pfn, nr_pages, params);
+	if (ret)
+		kernel_physical_mapping_remove(start, start + size);
+
+	return ret;
  }
 
Quoted 7 lines
Quoted 3 lines
arch/x86/mm/init_64.c +0 -0
  static void __meminit free_pagetable(struct page *page)

base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd

I thought we fixed that already, but I remembered

https://lore.kernel.org/all/[email protected]/

Ouch ... I missed the direct map cleanup in that fix ...