Skip to contentSkip to conversation

mm/hotplug: Pre-validate the address range with platform

RFC Superseded Anshuman Khandual 4 participants 23 messages Nov 30, 2020 – Dec 8, 2020

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover mm/hotplug: Pre-validate the address range with platform

This series adds a mechanism allowing platforms to weigh in and prevalidate incoming address range before proceeding further with the memory hotplug. This helps prevent potential platform errors for the given address range, down the hotplug call chain, which inevitably fails the hotplug itself.

This mechanism was suggested by David Hildenbrand during another discussion with respect to a memory hotplug fix on arm64 platform.

https://lore.kernel.org/linux-arm-kernel/[email protected]/

This mechanism focuses on the addressibility aspect and not [sub] section alignment aspect. Hence check_hotplug_memory_range() and check_pfn_span() have been left unchanged. Wondering if all these can still be unified in an expanded memhp_range_allowed() check, that can be called from multiple memory hot add and remove paths.

This series applies on v5.10-rc6 and has been slightly tested on arm64. But looking for some early feedback here.

Changes in RFC V2:

Incorporated all review feedbacks from David.

- Added additional range check in __segment_load() on s390 which was lost
- Changed is_private init in pagemap_range()
- Moved the framework into mm/memory_hotplug.c
- Made arch_get_addressable_range() a __weak function
- Renamed arch_get_addressable_range() as arch_get_mappable_range()
- Callback arch_get_mappable_range() only handles range requiring linear mapping
- Merged multiple memhp_range_allowed() checks in register_memory_resource()
- Replaced WARN() with pr_warn() in memhp_range_allowed()
- Replaced error return code ERANGE with E2BIG

Changes in RFC V1:

https://lore.kernel.org/linux-mm/[email protected]/

Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Andrew Morton <[email protected]>

Anshuman Khandual (3):
mm/hotplug: Prevalidate the address range being added with platform arm64/mm: Define arch_get_mappable_range()
s390/mm: Define arch_get_mappable_range()

 arch/arm64/mm/mmu.c            | 14 +++----
 arch/s390/mm/extmem.c          |  5 +++
 arch/s390/mm/vmem.c            | 13 ++++--
 include/linux/memory_hotplug.h |  2 +
 mm/memory_hotplug.c            | 77 +++++++++++++++++++++++++---------
 mm/memremap.c                  |  6 ++-
 6 files changed, 84 insertions(+), 33 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

RFC v2 1/3 mm/hotplug: Prevalidate the address range being added with platform

This introduces memhp_range_allowed() which can be called in various memory hotplug paths to prevalidate the address range which is being added, with the platform. Then memhp_range_allowed() calls memhp_get_pluggable_range() which provides applicable address range depending on whether linear mapping is required or not. For ranges that require linear mapping, it calls a new arch callback arch_get_mappable_range() which the platform can override. So the new callback, in turn provides the platform an opportunity to configure acceptable memory hotplug address ranges in case there are constraints.

This mechanism will help prevent platform specific errors deep down during hotplug calls. This drops now redundant check_hotplug_memory_addressable() check in __add_pages().

---
 include/linux/memory_hotplug.h |  2 +
 mm/memory_hotplug.c            | 77 +++++++++++++++++++++++++---------
 mm/memremap.c                  |  6 ++-
 3 files changed, 64 insertions(+), 21 deletions(-)
include/linux/memory_hotplug.h +2 -0
index 551093b74596..047a711ab76a 100644
@@ -70,6 +70,8 @@ typedef int __bitwise mhp_t;
  */
 #define MEMHP_MERGE_RESOURCE	((__force mhp_t)BIT(0))
 
+bool memhp_range_allowed(u64 start, u64 size, bool need_mapping);
+
 /*
  * Extended parameters for memory hotplug:
  * altmap: alternative allocator for memmap array (optional)
mm/memory_hotplug.c +57 -20

99 lines not on this page · raw message

mm/memremap.c +5 -1
index 16b2fb482da1..26c1825756cc 100644
@@ -185,6 +185,7 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref)
 static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 		int range_id, int nid)
 {
+	const bool is_private = pgmap->type == MEMORY_DEVICE_PRIVATE;
 	struct range *range = &pgmap->ranges[range_id];
 	struct dev_pagemap *conflict_pgmap;
 	int error, is_ram;
@@ -230,6 +231,9 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 	if (error)
 		goto err_pfn_remap;
 
+	if (!memhp_range_allowed(range->start, range_len(range), !is_private))
+		goto err_pfn_remap;
+
 	mem_hotplug_begin();
 
 	/*
@@ -243,7 +247,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 	 * the CPU, we do want the linear mapping and thus use
 	 * arch_add_memory().
 	 */
-	if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
+	if (is_private) {
 		error = add_pages(nid, PHYS_PFN(range->start),
 				PHYS_PFN(range_len(range)), params);
 	} else {
Signature · 1 line
2.20.1
#2 lore raw

Re: [RFC V2 1/3] mm/hotplug: Prevalidate the address range being added with platform

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Anshuman,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on v5.10-rc6]
[also build test ERROR on next-20201201]
[cannot apply to mmotm/master s390/features arm64/for-next/core hnaz-linux-mm/master] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145
base:    b65054597872ce3aefbc6a666385eabdf9e288da
config: x86_64-randconfig-a005-20201201 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 2671fccf0381769276ca8246ec0499adcb9b0355)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/b2e5e21f5ab458f165eddf57099039bb40d6fecf
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145 git checkout b2e5e21f5ab458f165eddf57099039bb40d6fecf
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: memhp_range_allowed
   >>> referenced by memory_hotplug.c:110 (mm/memory_hotplug.c:110)
   >>>               memory_hotplug.o:(register_memory_resource) in archive mm/built-in.a

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#3 lore raw

Re: [RFC V2 1/3] mm/hotplug: Prevalidate the address range being added with platform

In reply to Anshuman Khandual
Quoted 13 lines · Anshuman Khandual

This introduces memhp_range_allowed() which can be called in various memory hotplug paths to prevalidate the address range which is being added, with the platform. Then memhp_range_allowed() calls memhp_get_pluggable_range() which provides applicable address range depending on whether linear mapping is required or not. For ranges that require linear mapping, it calls a new arch callback arch_get_mappable_range() which the platform can override. So the new callback, in turn provides the platform an opportunity to configure acceptable memory hotplug address ranges in case there are constraints.

This mechanism will help prevent platform specific errors deep down during hotplug calls. This drops now redundant check_hotplug_memory_addressable() check in __add_pages().

[...]

mm/memory_hotplug.c int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, · line 323

Quoted 11 lines

/*
* Reasonably generic function for adding memory. It is
* expected that archs that support memory hotplug will

mm/memory_hotplug.c +0 -4
@@ -317,10 +304,6 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 	if (WARN_ON_ONCE(!params->pgprot.pgprot))
 		return -EINVAL;
 
-	err = check_hotplug_memory_addressable(pfn, nr_pages);
-	if (err)
-		return err;
-

I was wondering if we should add a VM_BUG_ON(!memhp_range_allowed()) here to make it clearer that callers are expected to check that first. Maybe an other places as well (e.g., arch code where we remove the original checks)

[...]

Quoted 25 lines
mm/memory_hotplug.c +0 -0
 #endif /* CONFIG_MEMORY_HOTREMOVE */
mm/memremap.c +4 -0
index 16b2fb482da1..26c1825756cc 100644
@@ -185,6 +185,7 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref)
 static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 		int range_id, int nid)
 {
+	const bool is_private = pgmap->type == MEMORY_DEVICE_PRIVATE;
 	struct range *range = &pgmap->ranges[range_id];
 	struct dev_pagemap *conflict_pgmap;
 	int error, is_ram;
@@ -230,6 +231,9 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 	if (error)
 		goto err_pfn_remap;
 
+	if (!memhp_range_allowed(range->start, range_len(range), !is_private))
+		goto err_pfn_remap;
+
 	mem_hotplug_begin();
 
 	/*

mm/memremap.c static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params, · line 253

Quoted 8 lines
mm/memremap.c +1 -1
@@ -243,7 +247,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 	 * the CPU, we do want the linear mapping and thus use
 	 * arch_add_memory().
 	 */
-	if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
+	if (is_private) {
 		error = add_pages(nid, PHYS_PFN(range->start),
 				PHYS_PFN(range_len(range)), params);
 	} else {

In general, LGTM.

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw

Re: [RFC V2 1/3] mm/hotplug: Prevalidate the address range being added with platform

Quoted 25 lines · David Hildenbrand
Quoted 13 lines · Anshuman Khandual

This introduces memhp_range_allowed() which can be called in various memory hotplug paths to prevalidate the address range which is being added, with the platform. Then memhp_range_allowed() calls memhp_get_pluggable_range() which provides applicable address range depending on whether linear mapping is required or not. For ranges that require linear mapping, it calls a new arch callback arch_get_mappable_range() which the platform can override. So the new callback, in turn provides the platform an opportunity to configure acceptable memory hotplug address ranges in case there are constraints.

This mechanism will help prevent platform specific errors deep down during hotplug calls. This drops now redundant check_hotplug_memory_addressable() check in __add_pages().

[...]

Quoted 7 lines

/*
* Reasonably generic function for adding memory. It is
* expected that archs that support memory hotplug will

mm/memory_hotplug.c +0 -0
@@ -317,10 +304,6 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 	if (WARN_ON_ONCE(!params->pgprot.pgprot))
 		return -EINVAL;
 
Quoted 9 lines

mm/memory_hotplug.c int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages, · line 323

Quoted 4 lines
mm/memory_hotplug.c +0 -4
-	err = check_hotplug_memory_addressable(pfn, nr_pages);
-	if (err)
-		return err;
-

I was wondering if we should add a VM_BUG_ON(!memhp_range_allowed()) here to make it clearer that callers are expected to check that first. Maybe an other places as well (e.g., arch code where we remove the original checks)

Makes sense, will add them.

Quoted 26 lines

[...]

Quoted 22 lines

#endif /* CONFIG_MEMORY_HOTREMOVE */

mm/memremap.c +4 -0
index 16b2fb482da1..26c1825756cc 100644
@@ -185,6 +185,7 @@ static void dev_pagemap_percpu_release(struct percpu_ref *ref)
 static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 		int range_id, int nid)
 {
+	const bool is_private = pgmap->type == MEMORY_DEVICE_PRIVATE;
 	struct range *range = &pgmap->ranges[range_id];
 	struct dev_pagemap *conflict_pgmap;
 	int error, is_ram;
@@ -230,6 +231,9 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 	if (error)
 		goto err_pfn_remap;
 
+	if (!memhp_range_allowed(range->start, range_len(range), !is_private))
+		goto err_pfn_remap;
+
 	mem_hotplug_begin();
 
Quoted 14 lines

mm/memremap.c static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params, · line 253

Quoted 11 lines
mm/memremap.c +1 -1
 	/*
@@ -243,7 +247,7 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 	 * the CPU, we do want the linear mapping and thus use
 	 * arch_add_memory().
 	 */
-	if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
+	if (is_private) {
 		error = add_pages(nid, PHYS_PFN(range->start),
 				PHYS_PFN(range_len(range)), params);
 	} else {

In general, LGTM.

Okay

#5 lore raw

RFC v2 2/3 arm64/mm: Define arch_get_mappable_range()

In reply to Anshuman Khandual

This overrides arch_get_mappable_range() on arm64 platform which will be used with recently added generic framework. It drops inside_linear_region() and subsequent check in arch_add_memory() which are no longer required.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 arch/arm64/mm/mmu.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
arch/arm64/mm/mmu.c +6 -8
index ca692a815731..49ec8f2838f2 100644
@@ -1444,16 +1444,19 @@ static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
 }
 
-static bool inside_linear_region(u64 start, u64 size)
+struct range arch_get_mappable_range(void)
 {
+	struct range memhp_range;
+
 	/*
 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
 	 * accommodating both its ends but excluding PAGE_END. Max physical
 	 * range which can be mapped inside this linear mapping range, must
 	 * also be derived from its end points.
 	 */
-	return start >= __pa(_PAGE_OFFSET(vabits_actual)) &&
-	       (start + size - 1) <= __pa(PAGE_END - 1);
+	memhp_range.start = __pa(_PAGE_OFFSET(vabits_actual));
+	memhp_range.end =  __pa(PAGE_END - 1);
+	return memhp_range;
 }
 
 int arch_add_memory(int nid, u64 start, u64 size,
@@ -1461,11 +1464,6 @@ int arch_add_memory(int nid, u64 start, u64 size,
 {
 	int ret, flags = 0;
 
-	if (!inside_linear_region(start, size)) {
-		pr_err("[%llx %llx] is outside linear mapping region\n", start, start + size);
-		return -EINVAL;
-	}
-
 	if (rodata_full || debug_pagealloc_enabled())
 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
 
Signature · 1 line
2.20.1
#6 lore raw

Re: [RFC V2 2/3] arm64/mm: Define arch_get_mappable_range()

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Anshuman,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on v5.10-rc6]
[cannot apply to mmotm/master s390/features arm64/for-next/core hnaz-linux-mm/master next-20201127] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145
base:    b65054597872ce3aefbc6a666385eabdf9e288da
config: arm64-allyesconfig (attached as .config)
compiler: aarch64-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/7d8145ca97fc2f90d8378399cf1823cfd90049be
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145 git checkout 7d8145ca97fc2f90d8378399cf1823cfd90049be
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/arm64/mm/mmu.c:1447:14: warning: no previous prototype for 'arch_get_mappable_range' [-Wmissing-prototypes]

1447 | struct range arch_get_mappable_range(void)
| ^~~~~~~~~~~~~~~~~~~~~~~

vim +/arch_get_mappable_range +1447 arch/arm64/mm/mmu.c

1446

> 1447	struct range arch_get_mappable_range(void)

1448 {
1449 struct range memhp_range;
1450
1451 /*
1452 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1453 * accommodating both its ends but excluding PAGE_END. Max physical
1454 * range which can be mapped inside this linear mapping range, must
1455 * also be derived from its end points.
1456 */
1457 memhp_range.start = __pa(_PAGE_OFFSET(vabits_actual));
1458 memhp_range.end = __pa(PAGE_END - 1);
1459 return memhp_range;
1460 }
1461

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

Re: [RFC V2 2/3] arm64/mm: Define arch_get_mappable_range()

In reply to Anshuman Khandual
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Anshuman,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on v5.10-rc6]
[cannot apply to mmotm/master s390/features arm64/for-next/core hnaz-linux-mm/master next-20201130] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145
base:    b65054597872ce3aefbc6a666385eabdf9e288da
config: arm64-randconfig-r002-20201130 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project dfcf1acf13226be0f599a7ab6b395b66dc9545d6)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/0day-ci/linux/commit/7d8145ca97fc2f90d8378399cf1823cfd90049be
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145 git checkout 7d8145ca97fc2f90d8378399cf1823cfd90049be
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm64

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> arch/arm64/mm/mmu.c:1447:14: warning: no previous prototype for function 'arch_get_mappable_range' [-Wmissing-prototypes]
   struct range arch_get_mappable_range(void)
                ^

arch/arm64/mm/mmu.c:1447:1: note: declare 'static' if the function is not intended to be used outside of this translation unit struct range arch_get_mappable_range(void)
^
static
1 warning generated.

vim +/arch_get_mappable_range +1447 arch/arm64/mm/mmu.c

1446

> 1447	struct range arch_get_mappable_range(void)

1448 {
1449 struct range memhp_range;
1450
1451 /*
1452 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
1453 * accommodating both its ends but excluding PAGE_END. Max physical
1454 * range which can be mapped inside this linear mapping range, must
1455 * also be derived from its end points.
1456 */
1457 memhp_range.start = __pa(_PAGE_OFFSET(vabits_actual));
1458 memhp_range.end = __pa(PAGE_END - 1);
1459 return memhp_range;
1460 }
1461

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#8 lore raw

Re: [RFC V2 2/3] arm64/mm: Define arch_get_mappable_range()

In reply to Anshuman Khandual
Quoted 44 lines · Anshuman Khandual

This overrides arch_get_mappable_range() on arm64 platform which will be used with recently added generic framework. It drops inside_linear_region() and subsequent check in arch_add_memory() which are no longer required.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 arch/arm64/mm/mmu.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
arch/arm64/mm/mmu.c +6 -3
index ca692a815731..49ec8f2838f2 100644
@@ -1444,16 +1444,19 @@ static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
 }
 
-static bool inside_linear_region(u64 start, u64 size)
+struct range arch_get_mappable_range(void)
 {
+	struct range memhp_range;
+
 	/*
 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
 	 * accommodating both its ends but excluding PAGE_END. Max physical
 	 * range which can be mapped inside this linear mapping range, must
 	 * also be derived from its end points.
 	 */
-	return start >= __pa(_PAGE_OFFSET(vabits_actual)) &&
-	       (start + size - 1) <= __pa(PAGE_END - 1);
+	memhp_range.start = __pa(_PAGE_OFFSET(vabits_actual));
+	memhp_range.end =  __pa(PAGE_END - 1);
+	return memhp_range;
 }
 
 int arch_add_memory(int nid, u64 start, u64 size,

arch/arm64/mm/mmu.c int arch_add_memory(int nid, u64 start, u64 size, · line 1467

Quoted 7 lines
arch/arm64/mm/mmu.c +0 -4
@@ -1461,11 +1464,6 @@ int arch_add_memory(int nid, u64 start, u64 size,
 {
 	int ret, flags = 0;
 
-	if (!inside_linear_region(start, size)) {
-		pr_err("[%llx %llx] is outside linear mapping region\n", start, start + size);
-		return -EINVAL;
-	}

As discussed, I think something like a VM_BUG_ON() here might makes sense, indicating that we require the caller to validate upfront. Same applies to the s390x variant.

Thanks!

Quoted 5 lines
arch/arm64/mm/mmu.c +0 -1
-
 	if (rodata_full || debug_pagealloc_enabled())
 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
 
Signature · 3 lines
Thanks,

David / dhildenb
#9 lore raw

Re: [RFC V2 2/3] arm64/mm: Define arch_get_mappable_range()

Quoted 48 lines · David Hildenbrand
Quoted 47 lines · Anshuman Khandual

This overrides arch_get_mappable_range() on arm64 platform which will be used with recently added generic framework. It drops inside_linear_region() and subsequent check in arch_add_memory() which are no longer required.

Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
Cc David Hildenbrand <[email protected]>
---
 arch/arm64/mm/mmu.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
arch/arm64/mm/mmu.c +6 -3
index ca692a815731..49ec8f2838f2 100644
@@ -1444,16 +1444,19 @@ static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
 }
 
-static bool inside_linear_region(u64 start, u64 size)
+struct range arch_get_mappable_range(void)
 {
+	struct range memhp_range;
+
 	/*
 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
 	 * accommodating both its ends but excluding PAGE_END. Max physical
 	 * range which can be mapped inside this linear mapping range, must
 	 * also be derived from its end points.
 	 */
-	return start >= __pa(_PAGE_OFFSET(vabits_actual)) &&
-	       (start + size - 1) <= __pa(PAGE_END - 1);
+	memhp_range.start = __pa(_PAGE_OFFSET(vabits_actual));
+	memhp_range.end =  __pa(PAGE_END - 1);
+	return memhp_range;
 }
 
 int arch_add_memory(int nid, u64 start, u64 size,
@@ -1461,11 +1464,6 @@ int arch_add_memory(int nid, u64 start, u64 size,
 {
 	int ret, flags = 0;
 
Quoted 7 lines

arch/arm64/mm/mmu.c int arch_add_memory(int nid, u64 start, u64 size, · line 1467

Quoted 4 lines
arch/arm64/mm/mmu.c +0 -4
-	if (!inside_linear_region(start, size)) {
-		pr_err("[%llx %llx] is outside linear mapping region\n", start, start + size);
-		return -EINVAL;
-	}

As discussed, I think something like a VM_BUG_ON() here might makes sense, indicating that we require the caller to validate upfront. Same applies to the s390x variant.

Sure, will do.

Quoted 3 lines

Thanks!

#10 lore raw

RFC v2 3/3 s390/mm: Define arch_get_mappable_range()

In reply to Anshuman Khandual

This overrides arch_get_mappabble_range() on s390 platform and drops now redundant similar check in vmem_add_mapping(). This compensates by adding a new check __segment_load() to preserve the existing functionality.

---
 arch/s390/mm/extmem.c |  5 +++++
 arch/s390/mm/vmem.c   | 13 +++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -4
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-
 	mutex_lock(&vmem_mutex);
 	ret = vmem_add_range(start, size);
 	if (ret)
Signature · 1 line
2.20.1
#11 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi Anshuman,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on v5.10-rc6]
[cannot apply to mmotm/master s390/features arm64/for-next/core hnaz-linux-mm/master next-20201127] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145
base:    b65054597872ce3aefbc6a666385eabdf9e288da
config: s390-randconfig-r024-20201130 (attached as .config)
compiler: s390-linux-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/07c7b752176c2d810fc3dc44f9b41593d995fbf2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Anshuman-Khandual/mm-hotplug-Pre-validate-the-address-range-with-platform/20201130-113145 git checkout 07c7b752176c2d810fc3dc44f9b41593d995fbf2
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=s390

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

arch/s390/mm/vmem.c:520:6: warning: no previous prototype for 'vmemmap_free' [-Wmissing-prototypes]
520 | void vmemmap_free(unsigned long start, unsigned long end,
| ^~~~~~~~~~~~

>> arch/s390/mm/vmem.c:535:14: warning: no previous prototype for 'arch_get_mappable_range' [-Wmissing-prototypes]

535 | struct range arch_get_mappable_range(void)
| ^~~~~~~~~~~~~~~~~~~~~~~

vim +/arch_get_mappable_range +535 arch/s390/mm/vmem.c

519

 > 520	void vmemmap_free(unsigned long start, unsigned long end,
   521			  struct vmem_altmap *altmap)
   522	{
   523		mutex_lock(&vmem_mutex);
   524		remove_pagetable(start, end, false);
   525		mutex_unlock(&vmem_mutex);
   526	}
   527	
   528	void vmem_remove_mapping(unsigned long start, unsigned long size)
   529	{
   530		mutex_lock(&vmem_mutex);
   531		vmem_remove_range(start, size);
   532		mutex_unlock(&vmem_mutex);
   533	}
   534	
 > 535	struct range arch_get_mappable_range(void)

536 {
537 struct range memhp_range;
538
539 memhp_range.start = 0;
540 memhp_range.end = VMEM_MAX_PHYS;
541 return memhp_range;
542 }
543

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#12 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

In reply to Anshuman Khandual
Quoted 46 lines · Anshuman Khandual

This overrides arch_get_mappabble_range() on s390 platform and drops now redundant similar check in vmem_add_mapping(). This compensates by adding a new check __segment_load() to preserve the existing functionality.

---
 arch/s390/mm/extmem.c |  5 +++++
 arch/s390/mm/vmem.c   | 13 +++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +7 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 10 lines
arch/s390/mm/vmem.c +2 -4
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

#13 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 53 lines · Heiko Carstens
Quoted 52 lines · Anshuman Khandual

This overrides arch_get_mappabble_range() on s390 platform and drops now redundant similar check in vmem_add_mapping(). This compensates by adding a new check __segment_load() to preserve the existing functionality.

---
 arch/s390/mm/extmem.c |  5 +++++
 arch/s390/mm/vmem.c   | 13 +++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
Quoted 9 lines

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 4 lines
arch/s390/mm/vmem.c +0 -4
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

The physical range check is being moved to the generic hotplug code via arch_get_mappable_range() instead, making the existing check in vmem_add_mapping() redundant. Dropping the check there necessitates adding back a similar check in __segment_load(). Otherwise there will be a loss of functionality in terms of range check.

May be we could just keep this existing check in vmem_add_mapping() as well in order avoid this movement but then it would be redundant check in every hotplug path.

So I guess the choice is to either have redundant range checks in all hotplug paths or future internal callers of vmem_add_mapping() take care of the range check.

maintainer
#14 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 53 lines · Anshuman Khandual
Quoted 37 lines
Quoted 37 lines
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
Quoted 9 lines

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 4 lines
arch/s390/mm/vmem.c +0 -4
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

The physical range check is being moved to the generic hotplug code via arch_get_mappable_range() instead, making the existing check in vmem_add_mapping() redundant. Dropping the check there necessitates adding back a similar check in __segment_load(). Otherwise there will be a loss of functionality in terms of range check.

Quoted 7 lines

May be we could just keep this existing check in vmem_add_mapping() as well in order avoid this movement but then it would be redundant check in every hotplug path.

So I guess the choice is to either have redundant range checks in all hotplug paths or future internal callers of vmem_add_mapping() take care of the range check.

The problem I have with this current approach from an architecture
perspective: we end up having two completely different methods which are doing the same and must be kept in sync. This might be obvious looking at this patch, but I'm sure this will go out-of-sync (aka broken) sooner or later.

Therefore I would really like to see a single method to do the range checking. Maybe you could add a callback into architecture code, so that such an architecture specific function could also be used elsewhere. Dunno.

#15 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 62 lines · Heiko Carstens
Quoted 53 lines · Anshuman Khandual
Quoted 37 lines
Quoted 37 lines
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
Quoted 9 lines

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 4 lines
arch/s390/mm/vmem.c +0 -4
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

The physical range check is being moved to the generic hotplug code via arch_get_mappable_range() instead, making the existing check in vmem_add_mapping() redundant. Dropping the check there necessitates adding back a similar check in __segment_load(). Otherwise there will be a loss of functionality in terms of range check.

Quoted 7 lines

May be we could just keep this existing check in vmem_add_mapping() as well in order avoid this movement but then it would be redundant check in every hotplug path.

So I guess the choice is to either have redundant range checks in all hotplug paths or future internal callers of vmem_add_mapping() take care of the range check.

Quoted 5 lines

The problem I have with this current approach from an architecture
perspective: we end up having two completely different methods which are doing the same and must be kept in sync. This might be obvious looking at this patch, but I'm sure this will go out-of-sync (aka broken) sooner or later.

Exactly, there should be one function only that was the whole idea of arch_get_mappable_range().

Quoted 6 lines

Therefore I would really like to see a single method to do the range checking. Maybe you could add a callback into architecture code, so that such an architecture specific function could also be used elsewhere. Dunno.

I think we can just switch to using "memhp_range_allowed()" here then after implementing arch_get_mappable_range().

Doesn't hurt to double check in vmem_add_mapping() - especially to keep extmem working without changes. At least for callers of memory hotplug it's then clear which values actually won't fail deep down in arch code.

Signature · 3 lines
Thanks,

David / dhildenb
#16 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 79 lines · David Hildenbrand
Quoted 62 lines · Heiko Carstens
Quoted 53 lines · Anshuman Khandual
Quoted 37 lines
Quoted 37 lines
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
Quoted 9 lines

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 4 lines
arch/s390/mm/vmem.c +0 -4
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

The physical range check is being moved to the generic hotplug code via arch_get_mappable_range() instead, making the existing check in vmem_add_mapping() redundant. Dropping the check there necessitates adding back a similar check in __segment_load(). Otherwise there will be a loss of functionality in terms of range check.

Quoted 7 lines

May be we could just keep this existing check in vmem_add_mapping() as well in order avoid this movement but then it would be redundant check in every hotplug path.

So I guess the choice is to either have redundant range checks in all hotplug paths or future internal callers of vmem_add_mapping() take care of the range check.

Quoted 5 lines

The problem I have with this current approach from an architecture
perspective: we end up having two completely different methods which are doing the same and must be kept in sync. This might be obvious looking at this patch, but I'm sure this will go out-of-sync (aka broken) sooner or later.

Exactly, there should be one function only that was the whole idea of arch_get_mappable_range().

Quoted 6 lines

Therefore I would really like to see a single method to do the range checking. Maybe you could add a callback into architecture code, so that such an architecture specific function could also be used elsewhere. Dunno.

Quoted 6 lines

I think we can just switch to using "memhp_range_allowed()" here then after implementing arch_get_mappable_range().

Doesn't hurt to double check in vmem_add_mapping() - especially to keep extmem working without changes. At least for callers of memory hotplug it's then clear which values actually won't fail deep down in arch code.

But there is a small problem here. memhp_range_allowed() is now defined and available with CONFIG_MEMORY_HOTPLUG where as vmem_add_mapping() and __segment_load() are generally available without any config dependency. So if CONFIG_MEMORY_HOTPLUG is not enabled there will be a build failure in vmem_add_mapping() for memhp_range_allowed() symbol.

We could just move VM_BUG_ON(!memhp_range_allowed(start, size, 1)) check from vmem_add_mapping() to arch_add_memory() like on arm64 platform. But then __segment_load() would need that additional new check to compensate as proposed earlier.

Also leaving vmem_add_mapping() and __segment_load() unchanged will cause the address range check to be called three times on the hotplug path i.e

1. register_memory_resource()
2. arch_add_memory()
3. vmem_add_mapping()

Moving memhp_range_allowed() check inside arch_add_memory() seems better and consistent with arm64. Also in the future, any platform which choose to override arch_get_mappable() will have this additional VM_BUG_ON() in their arch_add_memory().

#17 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 103 lines · Anshuman Khandual
Quoted 79 lines · David Hildenbrand
Quoted 62 lines · Heiko Carstens
Quoted 53 lines · Anshuman Khandual
Quoted 37 lines
Quoted 37 lines
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
Quoted 9 lines

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 4 lines
arch/s390/mm/vmem.c +0 -4
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

The physical range check is being moved to the generic hotplug code via arch_get_mappable_range() instead, making the existing check in vmem_add_mapping() redundant. Dropping the check there necessitates adding back a similar check in __segment_load(). Otherwise there will be a loss of functionality in terms of range check.

Quoted 7 lines

May be we could just keep this existing check in vmem_add_mapping() as well in order avoid this movement but then it would be redundant check in every hotplug path.

So I guess the choice is to either have redundant range checks in all hotplug paths or future internal callers of vmem_add_mapping() take care of the range check.

Quoted 5 lines

The problem I have with this current approach from an architecture
perspective: we end up having two completely different methods which are doing the same and must be kept in sync. This might be obvious looking at this patch, but I'm sure this will go out-of-sync (aka broken) sooner or later.

Exactly, there should be one function only that was the whole idea of arch_get_mappable_range().

Quoted 6 lines

Therefore I would really like to see a single method to do the range checking. Maybe you could add a callback into architecture code, so that such an architecture specific function could also be used elsewhere. Dunno.

Quoted 6 lines

I think we can just switch to using "memhp_range_allowed()" here then after implementing arch_get_mappable_range().

Doesn't hurt to double check in vmem_add_mapping() - especially to keep extmem working without changes. At least for callers of memory hotplug it's then clear which values actually won't fail deep down in arch code.

But there is a small problem here. memhp_range_allowed() is now defined and available with CONFIG_MEMORY_HOTPLUG where as vmem_add_mapping() and __segment_load() are generally available without any config dependency. So if CONFIG_MEMORY_HOTPLUG is not enabled there will be a build failure in vmem_add_mapping() for memhp_range_allowed() symbol.

We could just move VM_BUG_ON(!memhp_range_allowed(start, size, 1)) check from vmem_add_mapping() to arch_add_memory() like on arm64 platform. But then __segment_load() would need that additional new check to compensate as proposed earlier.

Also leaving vmem_add_mapping() and __segment_load() unchanged will cause the address range check to be called three times on the hotplug path i.e

Quoted 8 lines

1. register_memory_resource()
2. arch_add_memory()
3. vmem_add_mapping()

Moving memhp_range_allowed() check inside arch_add_memory() seems better and consistent with arm64. Also in the future, any platform which choose to override arch_get_mappable() will have this additional VM_BUG_ON() in their arch_add_memory().

Yeah, it might not make sense to add these checks all over the place. The important part is that

1. There is a check somewhere (and if it's deep down in arch code)
2. There is an obvious way for callers to find out what valid values are.

I guess it would be good enough to

a) Factor out getting arch ranges into arch_get_mappable_range()
b) Provide memhp_get_pluggable_range()

Both changes only make sense with an in-tree user. I'm planning on using this functionality in virtio-mem code. I can pickup your patches, drop the superfluous checks, and use it from virtio-mem code. Makese sense (BTW, looks like we'll see aarch64 support for virtio-mem soon)?

Signature · 3 lines
Thanks,

David / dhildenb
#18 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 116 lines · David Hildenbrand
Quoted 103 lines · Anshuman Khandual
Quoted 79 lines · David Hildenbrand
Quoted 62 lines · Heiko Carstens
Quoted 53 lines · Anshuman Khandual
Quoted 37 lines
Quoted 37 lines
arch/s390/mm/extmem.c +5 -0
index 5060956b8e7d..cc055a78f7b6 100644
@@ -337,6 +337,11 @@ __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long
 		goto out_free_resource;
 	}
 
+	if (seg->end + 1 > VMEM_MAX_PHYS || seg->end + 1 < seg->start_addr) {
+		rc = -ERANGE;
+		goto out_resource;
+	}
+
 	rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
 	if (rc)
 		goto out_resource;
arch/s390/mm/vmem.c +9 -0
index b239f2ba93b0..06dddcc0ce06 100644
@@ -532,14 +532,19 @@ void vmem_remove_mapping(unsigned long start, unsigned long size)
 	mutex_unlock(&vmem_mutex);
 }
 
+struct range arch_get_mappable_range(void)
+{
+	struct range memhp_range;
+
+	memhp_range.start = 0;
+	memhp_range.end =  VMEM_MAX_PHYS;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
 	int ret;
 
Quoted 9 lines

arch/s390/mm/vmem.c void vmem_remove_mapping(unsigned long start, unsigned long size) · line 542

Quoted 4 lines
arch/s390/mm/vmem.c +0 -4
-	if (start + size > VMEM_MAX_PHYS ||
-	    start + size < start)
-		return -ERANGE;
-

I really fail to see how this could be considered an improvement for s390. Especially I do not like that the (central) range check is now moved to the caller (__segment_load). Which would mean potential additional future callers would have to duplicate that code as well.

The physical range check is being moved to the generic hotplug code via arch_get_mappable_range() instead, making the existing check in vmem_add_mapping() redundant. Dropping the check there necessitates adding back a similar check in __segment_load(). Otherwise there will be a loss of functionality in terms of range check.

Quoted 7 lines

May be we could just keep this existing check in vmem_add_mapping() as well in order avoid this movement but then it would be redundant check in every hotplug path.

So I guess the choice is to either have redundant range checks in all hotplug paths or future internal callers of vmem_add_mapping() take care of the range check.

Quoted 5 lines

The problem I have with this current approach from an architecture
perspective: we end up having two completely different methods which are doing the same and must be kept in sync. This might be obvious looking at this patch, but I'm sure this will go out-of-sync (aka broken) sooner or later.

Exactly, there should be one function only that was the whole idea of arch_get_mappable_range().

Quoted 6 lines

Therefore I would really like to see a single method to do the range checking. Maybe you could add a callback into architecture code, so that such an architecture specific function could also be used elsewhere. Dunno.

Quoted 6 lines

I think we can just switch to using "memhp_range_allowed()" here then after implementing arch_get_mappable_range().

Doesn't hurt to double check in vmem_add_mapping() - especially to keep extmem working without changes. At least for callers of memory hotplug it's then clear which values actually won't fail deep down in arch code.

But there is a small problem here. memhp_range_allowed() is now defined and available with CONFIG_MEMORY_HOTPLUG where as vmem_add_mapping() and __segment_load() are generally available without any config dependency. So if CONFIG_MEMORY_HOTPLUG is not enabled there will be a build failure in vmem_add_mapping() for memhp_range_allowed() symbol.

We could just move VM_BUG_ON(!memhp_range_allowed(start, size, 1)) check from vmem_add_mapping() to arch_add_memory() like on arm64 platform. But then __segment_load() would need that additional new check to compensate as proposed earlier.

Also leaving vmem_add_mapping() and __segment_load() unchanged will cause the address range check to be called three times on the hotplug path i.e

Quoted 8 lines

1. register_memory_resource()
2. arch_add_memory()
3. vmem_add_mapping()

Moving memhp_range_allowed() check inside arch_add_memory() seems better and consistent with arm64. Also in the future, any platform which choose to override arch_get_mappable() will have this additional VM_BUG_ON() in their arch_add_memory().

Yeah, it might not make sense to add these checks all over the place. The important part is that

Quoted 8 lines

1. There is a check somewhere (and if it's deep down in arch code)
2. There is an obvious way for callers to find out what valid values are.

I guess it would be good enough to

a) Factor out getting arch ranges into arch_get_mappable_range()
b) Provide memhp_get_pluggable_range()

Have posted V1 earlier in the day which hopefully accommodates all previous suggestions but otherwise do let me know if anything else still needs to be improved upon.

https://lore.kernel.org/linux-mm/[email protected]/

Quoted 5 lines

Both changes only make sense with an in-tree user. I'm planning on using this functionality in virtio-mem code. I can pickup your patches, drop the superfluous checks, and use it from virtio-mem code. Makese sense (BTW, looks like we'll see aarch64 support for virtio-mem soon)?

I have not been following virtio-mem closely. But is there something pending on arm64 platform which prevents virtio-mem enablement ?

#19 lore raw

Re: [RFC V2 3/3] s390/mm: Define arch_get_mappable_range()

Quoted 8 lines
Quoted 5 lines

Both changes only make sense with an in-tree user. I'm planning on using this functionality in virtio-mem code. I can pickup your patches, drop the superfluous checks, and use it from virtio-mem code. Makese sense (BTW, looks like we'll see aarch64 support for virtio-mem soon)?

I have not been following virtio-mem closely. But is there something pending on arm64 platform which prevents virtio-mem enablement ?

Regarding enablement, I expect things to be working out of the box mostly. Jonathan is currently doing some testing and wants to send a simple unlock patch once done. [1]

Now, there are some things to improve in the future. virtio-mem adds/removes individual Linux memory blocks and logically plugs/unplugs MAX_ORDER - 1/pageblock_order pages inside Linux memory blocks.

1. memblock

On arm64 and powerpc, we create/delete memblocks when adding/removing memory, which is suboptimal (and the code is quite fragile as we don't handle errors ...). Hotplugged memory never has holes, so we can tweak relevant code to not check via the memblock api.

For example, pfn_valid() only has to check for memblock_is_map_memory() in case of !early_section() - otherwise it can just fallback to our generic pfn_valid() function.

2. MAX_ORDER - 1 / pageblock_order

With 64k base pages, virtio-mem can only logically plug/unplug in 512MB granularity, which is sub-optimal and inflexible. 4/2MB would be much better - however this would require always using 2MB THP on arm64 (IIRC via "cont" bits). Luckily, only some distributions use 64k base pages as default nowadays ... :)

3. Section size

virtio-mem benefits from small section sizes. Currently, we have 1G. With 4k base pages we could easily reduce it to something what x86 has (128 MB) - and I remember discussions regarding that already in other (IIRC NVDIMM / DIMM) context. Again, with 64k base pages we cannot go below 512 MB right now.

[1] https://lkml.kernel.org/r/[email protected]

Signature · 3 lines
Thanks,

David / dhildenb
#20 lore raw
In reply to Anshuman Khandual
Quoted 23 lines · Anshuman Khandual

This series adds a mechanism allowing platforms to weigh in and prevalidate incoming address range before proceeding further with the memory hotplug. This helps prevent potential platform errors for the given address range, down the hotplug call chain, which inevitably fails the hotplug itself.

This mechanism was suggested by David Hildenbrand during another discussion with respect to a memory hotplug fix on arm64 platform.

https://lore.kernel.org/linux-arm-kernel/[email protected]/

This mechanism focuses on the addressibility aspect and not [sub] section alignment aspect. Hence check_hotplug_memory_range() and check_pfn_span() have been left unchanged. Wondering if all these can still be unified in an expanded memhp_range_allowed() check, that can be called from multiple memory hot add and remove paths.

This series applies on v5.10-rc6 and has been slightly tested on arm64. But looking for some early feedback here.

Changes in RFC V2:

Incorporated all review feedbacks from David.

Quoted 9 lines

- Added additional range check in __segment_load() on s390 which was lost
- Changed is_private init in pagemap_range()
- Moved the framework into mm/memory_hotplug.c
- Made arch_get_addressable_range() a __weak function
- Renamed arch_get_addressable_range() as arch_get_mappable_range()
- Callback arch_get_mappable_range() only handles range requiring linear mapping
- Merged multiple memhp_range_allowed() checks in register_memory_resource()
- Replaced WARN() with pr_warn() in memhp_range_allowed()
- Replaced error return code ERANGE with E2BIG

There is one build failure with MEMORY_HOTPLUG=y and MEMORY_HOTREMOVE=n. There are warnings on arm64 and s390 platforms when built with W=1 due to lack of prototypes required with -Wmissing-prototypes. I have fixed all these problems for the next iteration when there is broad agreement on the overall approach.

maintainer
#21 lore raw
In reply to Anshuman Khandual
Quoted 37 lines · Anshuman Khandual

This series adds a mechanism allowing platforms to weigh in and prevalidate incoming address range before proceeding further with the memory hotplug. This helps prevent potential platform errors for the given address range, down the hotplug call chain, which inevitably fails the hotplug itself.

This mechanism was suggested by David Hildenbrand during another discussion with respect to a memory hotplug fix on arm64 platform.

https://lore.kernel.org/linux-arm-kernel/[email protected]/

This mechanism focuses on the addressibility aspect and not [sub] section alignment aspect. Hence check_hotplug_memory_range() and check_pfn_span() have been left unchanged. Wondering if all these can still be unified in an expanded memhp_range_allowed() check, that can be called from multiple memory hot add and remove paths.

This series applies on v5.10-rc6 and has been slightly tested on arm64. But looking for some early feedback here.

Changes in RFC V2:

Incorporated all review feedbacks from David.

- Added additional range check in __segment_load() on s390 which was lost
- Changed is_private init in pagemap_range()
- Moved the framework into mm/memory_hotplug.c
- Made arch_get_addressable_range() a __weak function
- Renamed arch_get_addressable_range() as arch_get_mappable_range()
- Callback arch_get_mappable_range() only handles range requiring linear mapping
- Merged multiple memhp_range_allowed() checks in register_memory_resource()
- Replaced WARN() with pr_warn() in memhp_range_allowed()
- Replaced error return code ERANGE with E2BIG

Changes in RFC V1:

https://lore.kernel.org/linux-mm/[email protected]/

Quoted 12 lines
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Andrew Morton <[email protected]>

Btw. please use git send-email's --cc-cover option to make sure that all patches of this series will be sent to all listed cc's. I really dislike to receive only the cover-letter and maybe on patch and then have to figure out where to find the rest.

Thanks :)

#22 lore raw
Quoted 51 lines · Heiko Carstens
Quoted 37 lines · Anshuman Khandual

This series adds a mechanism allowing platforms to weigh in and prevalidate incoming address range before proceeding further with the memory hotplug. This helps prevent potential platform errors for the given address range, down the hotplug call chain, which inevitably fails the hotplug itself.

This mechanism was suggested by David Hildenbrand during another discussion with respect to a memory hotplug fix on arm64 platform.

https://lore.kernel.org/linux-arm-kernel/[email protected]/

This mechanism focuses on the addressibility aspect and not [sub] section alignment aspect. Hence check_hotplug_memory_range() and check_pfn_span() have been left unchanged. Wondering if all these can still be unified in an expanded memhp_range_allowed() check, that can be called from multiple memory hot add and remove paths.

This series applies on v5.10-rc6 and has been slightly tested on arm64. But looking for some early feedback here.

Changes in RFC V2:

Incorporated all review feedbacks from David.

- Added additional range check in __segment_load() on s390 which was lost
- Changed is_private init in pagemap_range()
- Moved the framework into mm/memory_hotplug.c
- Made arch_get_addressable_range() a __weak function
- Renamed arch_get_addressable_range() as arch_get_mappable_range()
- Callback arch_get_mappable_range() only handles range requiring linear mapping
- Merged multiple memhp_range_allowed() checks in register_memory_resource()
- Replaced WARN() with pr_warn() in memhp_range_allowed()
- Replaced error return code ERANGE with E2BIG

Changes in RFC V1:

https://lore.kernel.org/linux-mm/[email protected]/

Quoted 12 lines
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Ard Biesheuvel <[email protected]>
Cc Mark Rutland <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Andrew Morton <[email protected]>
Quoted 4 lines

Btw. please use git send-email's --cc-cover option to make sure that all patches of this series will be sent to all listed cc's. I really dislike to receive only the cover-letter and maybe on patch and then have to figure out where to find the rest.

Okay, will ensure that.

Quoted 3 lines

Thanks :)