Skip to contentSkip to conversation

Add bounds check for Hotplugged memory

PATCH Superseded Alastair D'Silva 2 participants 4 messages Sep 16, 2019

Subsystems (2)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add bounds check for Hotplugged memory

From: Alastair D'Silva <[email protected]>

This series adds bounds checks for hotplugged memory, ensuring that it is within the physically addressable range (for platforms that define MAX_(POSSIBLE_)PHYSMEM_BITS.

This allows for early failure, rather than attempting to access bogus section numbers.

Changelog:
V2:
- Don't use MAX_POSSIBLE_PHYSMEM_BITS as it's wider that what may be available

Alastair D'Silva (2):
memory_hotplug: Add a bounds check to check_hotplug_memory_range()
mm: Add a bounds check in devm_memremap_pages()

 include/linux/memory_hotplug.h |  1 +
 mm/memory_hotplug.c            | 13 ++++++++++++-
 mm/memremap.c                  |  8 ++++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/2 memory_hotplug: Add a bounds check to check_hotplug_memory_range()

From: Alastair D'Silva <[email protected]>

On PowerPC, the address ranges allocated to OpenCAPI LPC memory are allocated from firmware. These address ranges may be higher than what older kernels permit, as we increased the maximum permissable address in commit 4ffe713b7587 ("powerpc/mm: Increase the max addressable memory to 2PB"). It is possible that the addressable range may change again in the future.

In this scenario, we end up with a bogus section returned from __section_nr (see the discussion on the thread "mm: Trigger bug on if a section is not found in __section_nr").

Adding a check here means that we fail early and have an opportunity to handle the error gracefully, rather than rumbling on and potentially accessing an incorrect section.

Further discussion is also on the thread ("powerpc: Perform a bounds check in arch_add_memory").

---
 include/linux/memory_hotplug.h |  1 +
 mm/memory_hotplug.c            | 13 ++++++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
include/linux/memory_hotplug.h +1 -0
index f46ea71b4ffd..bc477e98a310 100644
@@ -110,6 +110,7 @@ extern void __online_page_increment_counters(struct page *page);
 extern void __online_page_free(struct page *page);
 
 extern int try_online_node(int nid);
+int check_hotplug_memory_addressable(u64 start, u64 size);
 
 extern int arch_add_memory(int nid, u64 start, u64 size,
 			struct mhp_restrictions *restrictions);
mm/memory_hotplug.c +12 -1
index c73f09913165..02cb9a74f561 100644
@@ -1030,6 +1030,17 @@ int try_online_node(int nid)
 	return ret;
 }
 
+int check_hotplug_memory_addressable(u64 start, u64 size)
+{
+#ifdef MAX_PHYSMEM_BITS
+	if ((start + size - 1) >> MAX_PHYSMEM_BITS)
+		return -E2BIG;
+#endif
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(check_hotplug_memory_addressable);
+
 static int check_hotplug_memory_range(u64 start, u64 size)
 {
 	/* memory range must be block size aligned */
@@ -1040,7 +1051,7 @@ static int check_hotplug_memory_range(u64 start, u64 size)
 		return -EINVAL;
 	}
 
-	return 0;
+	return check_hotplug_memory_addressable(start, size);
 }
 
 static int online_memory_block(struct memory_block *mem, void *arg)
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 2/2 mm: Add a bounds check in devm_memremap_pages()

In reply to Alastair D'Silva

From: Alastair D'Silva <[email protected]>

The call to check_hotplug_memory_addressable() validates that the memory is fully addressable.

Without this call, it is possible that we may remap pages that is not physically addressable, resulting in bogus section numbers being returned from __section_nr().

---
 mm/memremap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/memremap.c +8 -0
index 86432650f829..fd00993caa3e 100644
@@ -269,6 +269,13 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
 
 	mem_hotplug_begin();
 
+	error = check_hotplug_memory_addressable(res->start,
+						 resource_size(res));
+	if (error) {
+		mem_hotplug_done();
+		goto err_checkrange;
+	}
+
 	/*
 	 * For device private memory we call add_pages() as we only need to
 	 * allocate and initialize struct page for the device memory. More-
@@ -324,6 +331,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
 
  err_add_memory:
 	kasan_remove_zero_shadow(__va(res->start), resource_size(res));
+ err_checkrange:
  err_kasan:
 	untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
  err_pfn_remap:
Signature · 1 line
2.21.0
#3 lore raw

Re: [PATCH v2 2/2] mm: Add a bounds check in devm_memremap_pages()

Quoted 19 lines · Alastair D'Silva

From: Alastair D'Silva <[email protected]>

The call to check_hotplug_memory_addressable() validates that the memory is fully addressable.

Without this call, it is possible that we may remap pages that is not physically addressable, resulting in bogus section numbers being returned from __section_nr().

---
 mm/memremap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/memremap.c +0 -0
index 86432650f829..fd00993caa3e 100644

mm/memremap.c void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) · line 277

Quoted 9 lines
mm/memremap.c +6 -0
@@ -269,6 +269,13 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
 
 	mem_hotplug_begin();
 
+	error = check_hotplug_memory_addressable(res->start,
+						 resource_size(res));
+	if (error) {
+		mem_hotplug_done();
+		goto err_checkrange;
+	}

As I said in reply to v1, please move this out of the memory hotplug lock. These are static checks.

Quoted 13 lines
mm/memremap.c +2 -0
+
 	/*
 	 * For device private memory we call add_pages() as we only need to
 	 * allocate and initialize struct page for the device memory. More-
@@ -324,6 +331,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
 
  err_add_memory:
 	kasan_remove_zero_shadow(__va(res->start), resource_size(res));
+ err_checkrange:
  err_kasan:
 	untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
  err_pfn_remap:
Signature · 3 lines
Thanks,

David / dhildenb