All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-18 13:12 ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:12 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Anshuman Khandual, Oscar Salvador, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel

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/1600332402-30123-1-git-send-email-anshuman.khandual@arm.com/

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.11-rc4 and has been tested on arm64. But only
build tested on s390.

Changes in V3

- Updated the commit message in [PATCH 1/3]
- Replaced 1 with 'true' and 0 with 'false' in memhp_range_allowed()
- Updated memhp_range.end as VMEM_MAX_PHYS - 1 and updated vmem_add_mapping() on s390
- Changed memhp_range_allowed() behaviour in __add_pages()
- Updated __add_pages() to return E2BIG when memhp_range_allowed() fails for non-linear mapping based requests

Changes in V2:

https://lore.kernel.org/linux-mm/1608218912-28932-1-git-send-email-anshuman.khandual@arm.com/

- Changed s390 version per Heiko and updated the commit message
- Called memhp_range_allowed() only for arch_add_memory() in pagemap_range()
- Exported the symbol memhp_get_pluggable_range() 

Changes in V1:

https://lore.kernel.org/linux-mm/1607400978-31595-1-git-send-email-anshuman.khandual@arm.com/

- Fixed build problems with (MEMORY_HOTPLUG & !MEMORY_HOTREMOVE)
- Added missing prototype for arch_get_mappable_range()
- Added VM_BUG_ON() check for memhp_range_allowed() in arch_add_memory() per David

Changes in RFC V2:

https://lore.kernel.org/linux-mm/1606706992-26656-1-git-send-email-anshuman.khandual@arm.com/

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/1606098529-7907-1-git-send-email-anshuman.khandual@arm.com/

Cc: Oscar Salvador <osalvador@suse.de>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-s390@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org

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

David Hildenbrand (1):
  virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug

 arch/arm64/mm/mmu.c            | 15 +++----
 arch/s390/mm/init.c            |  1 +
 arch/s390/mm/vmem.c            | 15 ++++++-
 drivers/virtio/virtio_mem.c    | 40 +++++++++++------
 include/linux/memory_hotplug.h | 10 +++++
 mm/memory_hotplug.c            | 79 ++++++++++++++++++++++++++--------
 mm/memremap.c                  |  6 +++
 7 files changed, 125 insertions(+), 41 deletions(-)

-- 
2.20.1


^ permalink raw reply	[flat|nested] 57+ messages in thread

* [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-18 13:12 ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:12 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, Anshuman Khandual,
	linux-kernel, Will Deacon, Ard Biesheuvel, linux-arm-kernel,
	Oscar Salvador

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/1600332402-30123-1-git-send-email-anshuman.khandual@arm.com/

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.11-rc4 and has been tested on arm64. But only
build tested on s390.

Changes in V3

- Updated the commit message in [PATCH 1/3]
- Replaced 1 with 'true' and 0 with 'false' in memhp_range_allowed()
- Updated memhp_range.end as VMEM_MAX_PHYS - 1 and updated vmem_add_mapping() on s390
- Changed memhp_range_allowed() behaviour in __add_pages()
- Updated __add_pages() to return E2BIG when memhp_range_allowed() fails for non-linear mapping based requests

Changes in V2:

https://lore.kernel.org/linux-mm/1608218912-28932-1-git-send-email-anshuman.khandual@arm.com/

- Changed s390 version per Heiko and updated the commit message
- Called memhp_range_allowed() only for arch_add_memory() in pagemap_range()
- Exported the symbol memhp_get_pluggable_range() 

Changes in V1:

https://lore.kernel.org/linux-mm/1607400978-31595-1-git-send-email-anshuman.khandual@arm.com/

- Fixed build problems with (MEMORY_HOTPLUG & !MEMORY_HOTREMOVE)
- Added missing prototype for arch_get_mappable_range()
- Added VM_BUG_ON() check for memhp_range_allowed() in arch_add_memory() per David

Changes in RFC V2:

https://lore.kernel.org/linux-mm/1606706992-26656-1-git-send-email-anshuman.khandual@arm.com/

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/1606098529-7907-1-git-send-email-anshuman.khandual@arm.com/

Cc: Oscar Salvador <osalvador@suse.de>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-s390@vger.kernel.org
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org

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

David Hildenbrand (1):
  virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug

 arch/arm64/mm/mmu.c            | 15 +++----
 arch/s390/mm/init.c            |  1 +
 arch/s390/mm/vmem.c            | 15 ++++++-
 drivers/virtio/virtio_mem.c    | 40 +++++++++++------
 include/linux/memory_hotplug.h | 10 +++++
 mm/memory_hotplug.c            | 79 ++++++++++++++++++++++++++--------
 mm/memremap.c                  |  6 +++
 7 files changed, 125 insertions(+), 41 deletions(-)

-- 
2.20.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-18 13:12 ` Anshuman Khandual
@ 2021-01-18 13:12   ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:12 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Anshuman Khandual, Oscar Salvador, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel

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() but instead adds a VM_BUG_ON() check which would
ensure that the range has been validated with memhp_range_allowed() earlier
in the call chain. Besides memhp_get_pluggable_range() also can be used by
potential memory hotplug callers to avail the allowed physical range which
would go through on a given platform.

This does not really add any new range check in generic memory hotplug but
instead compensates for lost checks in arch_add_memory() where applicable
and check_hotplug_memory_addressable(), with unified memhp_range_allowed().

Cc: David Hildenbrand <david@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
Suggested-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 include/linux/memory_hotplug.h | 10 +++++
 mm/memory_hotplug.c            | 79 ++++++++++++++++++++++++++--------
 mm/memremap.c                  |  6 +++
 3 files changed, 76 insertions(+), 19 deletions(-)

diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h
index 15acce5ab106..439b013f818a 100644
--- a/include/linux/memory_hotplug.h
+++ b/include/linux/memory_hotplug.h
@@ -70,6 +70,9 @@ 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);
+struct range memhp_get_pluggable_range(bool need_mapping);
+
 /*
  * Extended parameters for memory hotplug:
  * altmap: alternative allocator for memmap array (optional)
@@ -281,6 +284,13 @@ static inline bool movable_node_is_enabled(void)
 }
 #endif /* ! CONFIG_MEMORY_HOTPLUG */
 
+/*
+ * Keep this declaration outside CONFIG_MEMORY_HOTPLUG as some
+ * platforms might override and use arch_get_mappable_range()
+ * for internal non memory hotplug purposes.
+ */
+struct range arch_get_mappable_range(void);
+
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 /*
  * pgdat resizing functions
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index f9d57b9be8c7..f62664e77ff9 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -107,6 +107,9 @@ static struct resource *register_memory_resource(u64 start, u64 size,
 	if (strcmp(resource_name, "System RAM"))
 		flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED;
 
+	if (!memhp_range_allowed(start, size, true))
+		return ERR_PTR(-E2BIG);
+
 	/*
 	 * Make sure value parsed from 'mem=' only restricts memory adding
 	 * while booting, so that memory hotplug won't be impacted. Please
@@ -284,22 +287,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
 	return 0;
 }
 
-static int check_hotplug_memory_addressable(unsigned long pfn,
-					    unsigned long nr_pages)
-{
-	const u64 max_addr = PFN_PHYS(pfn + nr_pages) - 1;
-
-	if (max_addr >> MAX_PHYSMEM_BITS) {
-		const u64 max_allowed = (1ull << (MAX_PHYSMEM_BITS + 1)) - 1;
-		WARN(1,
-		     "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n",
-		     (u64)PFN_PHYS(pfn), max_addr, max_allowed);
-		return -E2BIG;
-	}
-
-	return 0;
-}
-
 /*
  * Reasonably generic function for adding memory.  It is
  * expected that archs that support memory hotplug will
@@ -317,9 +304,8 @@ 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;
+	if(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false))
+		return -E2BIG;
 
 	if (altmap) {
 		/*
@@ -1180,6 +1166,61 @@ int add_memory_driver_managed(int nid, u64 start, u64 size,
 }
 EXPORT_SYMBOL_GPL(add_memory_driver_managed);
 
+/*
+ * Platforms should define arch_get_mappable_range() that provides
+ * maximum possible addressable physical memory range for which the
+ * linear mapping could be created. The platform returned address
+ * range must adhere to these following semantics.
+ *
+ * - range.start <= range.end
+ * - Range includes both end points [range.start..range.end]
+ *
+ * There is also a fallback definition provided here, allowing the
+ * entire possible physical address range in case any platform does
+ * not define arch_get_mappable_range().
+ */
+struct range __weak arch_get_mappable_range(void)
+{
+	struct range memhp_range = {
+		.start = 0UL,
+		.end = -1ULL,
+	};
+	return memhp_range;
+}
+
+struct range memhp_get_pluggable_range(bool need_mapping)
+{
+	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
+	struct range memhp_range;
+
+	if (need_mapping) {
+		memhp_range = arch_get_mappable_range();
+		if (memhp_range.start > max_phys) {
+			memhp_range.start = 0;
+			memhp_range.end = 0;
+		}
+		memhp_range.end = min_t(u64, memhp_range.end, max_phys);
+	} else {
+		memhp_range.start = 0;
+		memhp_range.end = max_phys;
+	}
+	return memhp_range;
+}
+EXPORT_SYMBOL_GPL(memhp_get_pluggable_range);
+
+bool memhp_range_allowed(u64 start, u64 size, bool need_mapping)
+{
+	struct range memhp_range = memhp_get_pluggable_range(need_mapping);
+	u64 end = start + size;
+
+	if (start < end && start >= memhp_range.start && (end - 1) <= memhp_range.end)
+		return true;
+
+	pr_warn("Hotplug memory [%#llx-%#llx] exceeds maximum addressable range [%#llx-%#llx]\n",
+		start, end, memhp_range.start, memhp_range.end);
+	return false;
+}
+
 #ifdef CONFIG_MEMORY_HOTREMOVE
 /*
  * Confirm all pages in a range [start, end) belong to the same zone (skipping
diff --git a/mm/memremap.c b/mm/memremap.c
index 16b2fb482da1..e15b13736f6a 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -253,6 +253,12 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 			goto err_kasan;
 		}
 
+		if (!memhp_range_allowed(range->start, range_len(range), true)) {
+			error = -ERANGE;
+			mem_hotplug_done();
+			goto err_add_memory;
+		}
+
 		error = arch_add_memory(nid, range->start, range_len(range),
 					params);
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-18 13:12   ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:12 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, Anshuman Khandual,
	linux-kernel, Will Deacon, Ard Biesheuvel, linux-arm-kernel,
	Oscar Salvador

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() but instead adds a VM_BUG_ON() check which would
ensure that the range has been validated with memhp_range_allowed() earlier
in the call chain. Besides memhp_get_pluggable_range() also can be used by
potential memory hotplug callers to avail the allowed physical range which
would go through on a given platform.

This does not really add any new range check in generic memory hotplug but
instead compensates for lost checks in arch_add_memory() where applicable
and check_hotplug_memory_addressable(), with unified memhp_range_allowed().

Cc: David Hildenbrand <david@redhat.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org
Suggested-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 include/linux/memory_hotplug.h | 10 +++++
 mm/memory_hotplug.c            | 79 ++++++++++++++++++++++++++--------
 mm/memremap.c                  |  6 +++
 3 files changed, 76 insertions(+), 19 deletions(-)

diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h
index 15acce5ab106..439b013f818a 100644
--- a/include/linux/memory_hotplug.h
+++ b/include/linux/memory_hotplug.h
@@ -70,6 +70,9 @@ 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);
+struct range memhp_get_pluggable_range(bool need_mapping);
+
 /*
  * Extended parameters for memory hotplug:
  * altmap: alternative allocator for memmap array (optional)
@@ -281,6 +284,13 @@ static inline bool movable_node_is_enabled(void)
 }
 #endif /* ! CONFIG_MEMORY_HOTPLUG */
 
+/*
+ * Keep this declaration outside CONFIG_MEMORY_HOTPLUG as some
+ * platforms might override and use arch_get_mappable_range()
+ * for internal non memory hotplug purposes.
+ */
+struct range arch_get_mappable_range(void);
+
 #if defined(CONFIG_MEMORY_HOTPLUG) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT)
 /*
  * pgdat resizing functions
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index f9d57b9be8c7..f62664e77ff9 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -107,6 +107,9 @@ static struct resource *register_memory_resource(u64 start, u64 size,
 	if (strcmp(resource_name, "System RAM"))
 		flags |= IORESOURCE_SYSRAM_DRIVER_MANAGED;
 
+	if (!memhp_range_allowed(start, size, true))
+		return ERR_PTR(-E2BIG);
+
 	/*
 	 * Make sure value parsed from 'mem=' only restricts memory adding
 	 * while booting, so that memory hotplug won't be impacted. Please
@@ -284,22 +287,6 @@ static int check_pfn_span(unsigned long pfn, unsigned long nr_pages,
 	return 0;
 }
 
-static int check_hotplug_memory_addressable(unsigned long pfn,
-					    unsigned long nr_pages)
-{
-	const u64 max_addr = PFN_PHYS(pfn + nr_pages) - 1;
-
-	if (max_addr >> MAX_PHYSMEM_BITS) {
-		const u64 max_allowed = (1ull << (MAX_PHYSMEM_BITS + 1)) - 1;
-		WARN(1,
-		     "Hotplugged memory exceeds maximum addressable address, range=%#llx-%#llx, maximum=%#llx\n",
-		     (u64)PFN_PHYS(pfn), max_addr, max_allowed);
-		return -E2BIG;
-	}
-
-	return 0;
-}
-
 /*
  * Reasonably generic function for adding memory.  It is
  * expected that archs that support memory hotplug will
@@ -317,9 +304,8 @@ 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;
+	if(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false))
+		return -E2BIG;
 
 	if (altmap) {
 		/*
@@ -1180,6 +1166,61 @@ int add_memory_driver_managed(int nid, u64 start, u64 size,
 }
 EXPORT_SYMBOL_GPL(add_memory_driver_managed);
 
+/*
+ * Platforms should define arch_get_mappable_range() that provides
+ * maximum possible addressable physical memory range for which the
+ * linear mapping could be created. The platform returned address
+ * range must adhere to these following semantics.
+ *
+ * - range.start <= range.end
+ * - Range includes both end points [range.start..range.end]
+ *
+ * There is also a fallback definition provided here, allowing the
+ * entire possible physical address range in case any platform does
+ * not define arch_get_mappable_range().
+ */
+struct range __weak arch_get_mappable_range(void)
+{
+	struct range memhp_range = {
+		.start = 0UL,
+		.end = -1ULL,
+	};
+	return memhp_range;
+}
+
+struct range memhp_get_pluggable_range(bool need_mapping)
+{
+	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
+	struct range memhp_range;
+
+	if (need_mapping) {
+		memhp_range = arch_get_mappable_range();
+		if (memhp_range.start > max_phys) {
+			memhp_range.start = 0;
+			memhp_range.end = 0;
+		}
+		memhp_range.end = min_t(u64, memhp_range.end, max_phys);
+	} else {
+		memhp_range.start = 0;
+		memhp_range.end = max_phys;
+	}
+	return memhp_range;
+}
+EXPORT_SYMBOL_GPL(memhp_get_pluggable_range);
+
+bool memhp_range_allowed(u64 start, u64 size, bool need_mapping)
+{
+	struct range memhp_range = memhp_get_pluggable_range(need_mapping);
+	u64 end = start + size;
+
+	if (start < end && start >= memhp_range.start && (end - 1) <= memhp_range.end)
+		return true;
+
+	pr_warn("Hotplug memory [%#llx-%#llx] exceeds maximum addressable range [%#llx-%#llx]\n",
+		start, end, memhp_range.start, memhp_range.end);
+	return false;
+}
+
 #ifdef CONFIG_MEMORY_HOTREMOVE
 /*
  * Confirm all pages in a range [start, end) belong to the same zone (skipping
diff --git a/mm/memremap.c b/mm/memremap.c
index 16b2fb482da1..e15b13736f6a 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -253,6 +253,12 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 			goto err_kasan;
 		}
 
+		if (!memhp_range_allowed(range->start, range_len(range), true)) {
+			error = -ERANGE;
+			mem_hotplug_done();
+			goto err_add_memory;
+		}
+
 		error = arch_add_memory(nid, range->start, range_len(range),
 					params);
 	}
-- 
2.20.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH V3 2/3] arm64/mm: Define arch_get_mappable_range()
  2021-01-18 13:12 ` Anshuman Khandual
@ 2021-01-18 13:13   ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:13 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Anshuman Khandual, Oscar Salvador, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel

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. It
also adds a VM_BUG_ON() check that would ensure that memhp_range_allowed()
has already been called.

Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 arch/arm64/mm/mmu.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index ae0c3d023824..f2e1770c9f29 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1442,16 +1442,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,
@@ -1459,11 +1462,7 @@ 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;
-	}
-
+	VM_BUG_ON(!memhp_range_allowed(start, size, true));
 	if (rodata_full || debug_pagealloc_enabled())
 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH V3 2/3] arm64/mm: Define arch_get_mappable_range()
@ 2021-01-18 13:13   ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:13 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, Anshuman Khandual,
	linux-kernel, Will Deacon, Ard Biesheuvel, linux-arm-kernel,
	Oscar Salvador

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. It
also adds a VM_BUG_ON() check that would ensure that memhp_range_allowed()
has already been called.

Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 arch/arm64/mm/mmu.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
index ae0c3d023824..f2e1770c9f29 100644
--- a/arch/arm64/mm/mmu.c
+++ b/arch/arm64/mm/mmu.c
@@ -1442,16 +1442,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,
@@ -1459,11 +1462,7 @@ 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;
-	}
-
+	VM_BUG_ON(!memhp_range_allowed(start, size, true));
 	if (rodata_full || debug_pagealloc_enabled())
 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
 
-- 
2.20.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
  2021-01-18 13:12 ` Anshuman Khandual
@ 2021-01-18 13:13   ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:13 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Anshuman Khandual, Oscar Salvador, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel

This overrides arch_get_mappabble_range() on s390 platform which will be
used with recently added generic framework. It modifies the existing range
check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
been called on the hotplug path.

Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: linux-s390@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 arch/s390/mm/init.c |  1 +
 arch/s390/mm/vmem.c | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
index 73a163065b95..97017a4bcc90 100644
--- a/arch/s390/mm/init.c
+++ b/arch/s390/mm/init.c
@@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
 	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
 		return -EINVAL;
 
+	VM_BUG_ON(!memhp_range_allowed(start, size, true));
 	rc = vmem_add_mapping(start, size);
 	if (rc)
 		return rc;
diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
index 01f3a5f58e64..afc39ff1cc8d 100644
--- a/arch/s390/mm/vmem.c
+++ b/arch/s390/mm/vmem.c
@@ -4,6 +4,7 @@
  *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  */
 
+#include <linux/memory_hotplug.h>
 #include <linux/memblock.h>
 #include <linux/pfn.h>
 #include <linux/mm.h>
@@ -532,11 +533,23 @@ 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 - 1;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
+	struct range range;
 	int ret;
 
-	if (start + size > VMEM_MAX_PHYS ||
+	range = arch_get_mappable_range();
+	if (start < range.start ||
+	    start + size > range.end + 1 ||
 	    start + size < start)
 		return -ERANGE;
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
@ 2021-01-18 13:13   ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:13 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, Anshuman Khandual,
	linux-kernel, Will Deacon, Ard Biesheuvel, linux-arm-kernel,
	Oscar Salvador

This overrides arch_get_mappabble_range() on s390 platform which will be
used with recently added generic framework. It modifies the existing range
check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
been called on the hotplug path.

Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: linux-s390@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 arch/s390/mm/init.c |  1 +
 arch/s390/mm/vmem.c | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
index 73a163065b95..97017a4bcc90 100644
--- a/arch/s390/mm/init.c
+++ b/arch/s390/mm/init.c
@@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
 	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
 		return -EINVAL;
 
+	VM_BUG_ON(!memhp_range_allowed(start, size, true));
 	rc = vmem_add_mapping(start, size);
 	if (rc)
 		return rc;
diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
index 01f3a5f58e64..afc39ff1cc8d 100644
--- a/arch/s390/mm/vmem.c
+++ b/arch/s390/mm/vmem.c
@@ -4,6 +4,7 @@
  *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  */
 
+#include <linux/memory_hotplug.h>
 #include <linux/memblock.h>
 #include <linux/pfn.h>
 #include <linux/mm.h>
@@ -532,11 +533,23 @@ 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 - 1;
+	return memhp_range;
+}
+
 int vmem_add_mapping(unsigned long start, unsigned long size)
 {
+	struct range range;
 	int ret;
 
-	if (start + size > VMEM_MAX_PHYS ||
+	range = arch_get_mappable_range();
+	if (start < range.start ||
+	    start + size > range.end + 1 ||
 	    start + size < start)
 		return -ERANGE;
 
-- 
2.20.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-18 13:12 ` Anshuman Khandual
@ 2021-01-18 13:13   ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:13 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Anshuman Khandual, Oscar Salvador, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel, Michael S. Tsirkin, Jason Wang, Pankaj Gupta,
	Michal Hocko, Wei Yang, teawater, Pankaj Gupta, Jonathan Cameron

From: David Hildenbrand <david@redhat.com>

Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
are more restrictions of which memory we can actually hotplug, especially
om arm64 or s390x once we support them: we might receive something like
-E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
operation.

So, check right when initializing the device which memory we can add,
warning the user. Try only adding actually pluggable ranges: in the worst
case, no memory provided by our device is pluggable.

In the usual case, we expect all device memory to be pluggable, and in
corner cases only some memory at the end of the device-managed memory
region to not be pluggable.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: catalin.marinas@arm.com
Cc: teawater <teawaterz@linux.alibaba.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: hca@linux.ibm.com
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Michal Hocko <mhocko@kernel.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 drivers/virtio/virtio_mem.c | 40 +++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 9fc9ec4a25f5..1fe40b2d7b6d 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -2222,7 +2222,7 @@ static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
  */
 static void virtio_mem_refresh_config(struct virtio_mem *vm)
 {
-	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
+	const struct range pluggable_range = memhp_get_pluggable_range(true);
 	uint64_t new_plugged_size, usable_region_size, end_addr;
 
 	/* the plugged_size is just a reflection of what _we_ did previously */
@@ -2234,15 +2234,25 @@ static void virtio_mem_refresh_config(struct virtio_mem *vm)
 	/* calculate the last usable memory block id */
 	virtio_cread_le(vm->vdev, struct virtio_mem_config,
 			usable_region_size, &usable_region_size);
-	end_addr = vm->addr + usable_region_size;
-	end_addr = min(end_addr, phys_limit);
+	end_addr = min(vm->addr + usable_region_size - 1,
+		       pluggable_range.end);
 
-	if (vm->in_sbm)
-		vm->sbm.last_usable_mb_id =
-					 virtio_mem_phys_to_mb_id(end_addr) - 1;
-	else
-		vm->bbm.last_usable_bb_id =
-				     virtio_mem_phys_to_bb_id(vm, end_addr) - 1;
+	if (vm->in_sbm) {
+		vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr);
+		if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes()))
+			vm->sbm.last_usable_mb_id--;
+	} else {
+		vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm,
+								     end_addr);
+		if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size))
+			vm->bbm.last_usable_bb_id--;
+	}
+	/*
+	 * If we cannot plug any of our device memory (e.g., nothing in the
+	 * usable region is addressable), the last usable memory block id will
+	 * be smaller than the first usable memory block id. We'll stop
+	 * attempting to add memory with -ENOSPC from our main loop.
+	 */
 
 	/* see if there is a request to change the size */
 	virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
@@ -2364,6 +2374,7 @@ static int virtio_mem_init_vq(struct virtio_mem *vm)
 
 static int virtio_mem_init(struct virtio_mem *vm)
 {
+	const struct range pluggable_range = memhp_get_pluggable_range(true);
 	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
 	uint64_t sb_size, addr;
 	uint16_t node_id;
@@ -2405,9 +2416,10 @@ static int virtio_mem_init(struct virtio_mem *vm)
 	if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
 		dev_warn(&vm->vdev->dev,
 			 "The alignment of the physical end address can make some memory unusable.\n");
-	if (vm->addr + vm->region_size > phys_limit)
+	if (vm->addr < pluggable_range.start ||
+	    vm->addr + vm->region_size - 1 > pluggable_range.end)
 		dev_warn(&vm->vdev->dev,
-			 "Some memory is not addressable. This can make some memory unusable.\n");
+			 "Some device memory is not addressable/pluggable. This can make some memory unusable.\n");
 
 	/*
 	 * We want subblocks to span at least MAX_ORDER_NR_PAGES and
@@ -2429,7 +2441,8 @@ static int virtio_mem_init(struct virtio_mem *vm)
 				     vm->sbm.sb_size;
 
 		/* Round up to the next full memory block */
-		addr = vm->addr + memory_block_size_bytes() - 1;
+		addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
+		       memory_block_size_bytes() - 1;
 		vm->sbm.first_mb_id = virtio_mem_phys_to_mb_id(addr);
 		vm->sbm.next_mb_id = vm->sbm.first_mb_id;
 	} else {
@@ -2450,7 +2463,8 @@ static int virtio_mem_init(struct virtio_mem *vm)
 		}
 
 		/* Round up to the next aligned big block */
-		addr = vm->addr + vm->bbm.bb_size - 1;
+		addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
+		       vm->bbm.bb_size - 1;
 		vm->bbm.first_bb_id = virtio_mem_phys_to_bb_id(vm, addr);
 		vm->bbm.next_bb_id = vm->bbm.first_bb_id;
 	}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 57+ messages in thread

* [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
@ 2021-01-18 13:13   ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:13 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Wei Yang, Vasily Gorbik,
	Anshuman Khandual, Jason Wang, Michael S. Tsirkin, linux-kernel,
	Michal Hocko, Pankaj Gupta, teawater, Jonathan Cameron,
	Pankaj Gupta, Will Deacon, Ard Biesheuvel, linux-arm-kernel,
	Oscar Salvador

From: David Hildenbrand <david@redhat.com>

Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
are more restrictions of which memory we can actually hotplug, especially
om arm64 or s390x once we support them: we might receive something like
-E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
operation.

So, check right when initializing the device which memory we can add,
warning the user. Try only adding actually pluggable ranges: in the worst
case, no memory provided by our device is pluggable.

In the usual case, we expect all device memory to be pluggable, and in
corner cases only some memory at the end of the device-managed memory
region to not be pluggable.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: catalin.marinas@arm.com
Cc: teawater <teawaterz@linux.alibaba.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: hca@linux.ibm.com
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Michal Hocko <mhocko@kernel.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
---
 drivers/virtio/virtio_mem.c | 40 +++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 9fc9ec4a25f5..1fe40b2d7b6d 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -2222,7 +2222,7 @@ static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
  */
 static void virtio_mem_refresh_config(struct virtio_mem *vm)
 {
-	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
+	const struct range pluggable_range = memhp_get_pluggable_range(true);
 	uint64_t new_plugged_size, usable_region_size, end_addr;
 
 	/* the plugged_size is just a reflection of what _we_ did previously */
@@ -2234,15 +2234,25 @@ static void virtio_mem_refresh_config(struct virtio_mem *vm)
 	/* calculate the last usable memory block id */
 	virtio_cread_le(vm->vdev, struct virtio_mem_config,
 			usable_region_size, &usable_region_size);
-	end_addr = vm->addr + usable_region_size;
-	end_addr = min(end_addr, phys_limit);
+	end_addr = min(vm->addr + usable_region_size - 1,
+		       pluggable_range.end);
 
-	if (vm->in_sbm)
-		vm->sbm.last_usable_mb_id =
-					 virtio_mem_phys_to_mb_id(end_addr) - 1;
-	else
-		vm->bbm.last_usable_bb_id =
-				     virtio_mem_phys_to_bb_id(vm, end_addr) - 1;
+	if (vm->in_sbm) {
+		vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr);
+		if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes()))
+			vm->sbm.last_usable_mb_id--;
+	} else {
+		vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm,
+								     end_addr);
+		if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size))
+			vm->bbm.last_usable_bb_id--;
+	}
+	/*
+	 * If we cannot plug any of our device memory (e.g., nothing in the
+	 * usable region is addressable), the last usable memory block id will
+	 * be smaller than the first usable memory block id. We'll stop
+	 * attempting to add memory with -ENOSPC from our main loop.
+	 */
 
 	/* see if there is a request to change the size */
 	virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
@@ -2364,6 +2374,7 @@ static int virtio_mem_init_vq(struct virtio_mem *vm)
 
 static int virtio_mem_init(struct virtio_mem *vm)
 {
+	const struct range pluggable_range = memhp_get_pluggable_range(true);
 	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
 	uint64_t sb_size, addr;
 	uint16_t node_id;
@@ -2405,9 +2416,10 @@ static int virtio_mem_init(struct virtio_mem *vm)
 	if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
 		dev_warn(&vm->vdev->dev,
 			 "The alignment of the physical end address can make some memory unusable.\n");
-	if (vm->addr + vm->region_size > phys_limit)
+	if (vm->addr < pluggable_range.start ||
+	    vm->addr + vm->region_size - 1 > pluggable_range.end)
 		dev_warn(&vm->vdev->dev,
-			 "Some memory is not addressable. This can make some memory unusable.\n");
+			 "Some device memory is not addressable/pluggable. This can make some memory unusable.\n");
 
 	/*
 	 * We want subblocks to span at least MAX_ORDER_NR_PAGES and
@@ -2429,7 +2441,8 @@ static int virtio_mem_init(struct virtio_mem *vm)
 				     vm->sbm.sb_size;
 
 		/* Round up to the next full memory block */
-		addr = vm->addr + memory_block_size_bytes() - 1;
+		addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
+		       memory_block_size_bytes() - 1;
 		vm->sbm.first_mb_id = virtio_mem_phys_to_mb_id(addr);
 		vm->sbm.next_mb_id = vm->sbm.first_mb_id;
 	} else {
@@ -2450,7 +2463,8 @@ static int virtio_mem_init(struct virtio_mem *vm)
 		}
 
 		/* Round up to the next aligned big block */
-		addr = vm->addr + vm->bbm.bb_size - 1;
+		addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
+		       vm->bbm.bb_size - 1;
 		vm->bbm.first_bb_id = virtio_mem_phys_to_bb_id(vm, addr);
 		vm->bbm.next_bb_id = vm->bbm.first_bb_id;
 	}
-- 
2.20.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-18 13:13   ` Anshuman Khandual
@ 2021-01-18 13:21     ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:21 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, Michal Hocko,
	Wei Yang, teawater, Pankaj Gupta, Jonathan Cameron



On 1/18/21 6:43 PM, Anshuman Khandual wrote:
> From: David Hildenbrand <david@redhat.com>
> 
> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
> are more restrictions of which memory we can actually hotplug, especially
> om arm64 or s390x once we support them: we might receive something like
> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
> operation.
> 
> So, check right when initializing the device which memory we can add,
> warning the user. Try only adding actually pluggable ranges: in the worst
> case, no memory provided by our device is pluggable.
> 
> In the usual case, we expect all device memory to be pluggable, and in
> corner cases only some memory at the end of the device-managed memory
> region to not be pluggable.
> 
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Jason Wang <jasowang@redhat.com>
> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
> Cc: Michal Hocko <mhocko@kernel.org>
> Cc: Oscar Salvador <osalvador@suse.de>
> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: catalin.marinas@arm.com
> Cc: teawater <teawaterz@linux.alibaba.com>
> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: hca@linux.ibm.com
> Cc: Vasily Gorbik <gor@linux.ibm.com>
> Cc: Will Deacon <will@kernel.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: Heiko Carstens <hca@linux.ibm.com>
> Cc: Michal Hocko <mhocko@kernel.org>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>

Hello David,

As your original patch was in the RFC state, I have just maintained
the same here as well. But once you test this patch along with the
new series, please do let me know if this needs to be converted to
a normal PATCH instead. Thank you.

- Anshuman

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
@ 2021-01-18 13:21     ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-18 13:21 UTC (permalink / raw)
  To: linux-mm, akpm, david, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Wei Yang, Vasily Gorbik,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, linux-kernel,
	Michal Hocko, Pankaj Gupta, teawater, Jonathan Cameron,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador



On 1/18/21 6:43 PM, Anshuman Khandual wrote:
> From: David Hildenbrand <david@redhat.com>
> 
> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
> are more restrictions of which memory we can actually hotplug, especially
> om arm64 or s390x once we support them: we might receive something like
> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
> operation.
> 
> So, check right when initializing the device which memory we can add,
> warning the user. Try only adding actually pluggable ranges: in the worst
> case, no memory provided by our device is pluggable.
> 
> In the usual case, we expect all device memory to be pluggable, and in
> corner cases only some memory at the end of the device-managed memory
> region to not be pluggable.
> 
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Jason Wang <jasowang@redhat.com>
> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
> Cc: Michal Hocko <mhocko@kernel.org>
> Cc: Oscar Salvador <osalvador@suse.de>
> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: catalin.marinas@arm.com
> Cc: teawater <teawaterz@linux.alibaba.com>
> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: hca@linux.ibm.com
> Cc: Vasily Gorbik <gor@linux.ibm.com>
> Cc: Will Deacon <will@kernel.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: Heiko Carstens <hca@linux.ibm.com>
> Cc: Michal Hocko <mhocko@kernel.org>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>

Hello David,

As your original patch was in the RFC state, I have just maintained
the same here as well. But once you test this patch along with the
new series, please do let me know if this needs to be converted to
a normal PATCH instead. Thank you.

- Anshuman

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-18 13:12   ` Anshuman Khandual
@ 2021-01-19 12:21     ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:21 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 18.01.21 14:12, Anshuman Khandual wrote:
> 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() but instead adds a VM_BUG_ON() check which would

In this patch, you keep the __add_pages() checks. But as discussed, we
could perform it in mm/memremap.c:pagemap_range() insted and convert it
to a VM_BUG_ON().

Apart from that looks good to me.

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-19 12:21     ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:21 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 18.01.21 14:12, Anshuman Khandual wrote:
> 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() but instead adds a VM_BUG_ON() check which would

In this patch, you keep the __add_pages() checks. But as discussed, we
could perform it in mm/memremap.c:pagemap_range() insted and convert it
to a VM_BUG_ON().

Apart from that looks good to me.

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 2/3] arm64/mm: Define arch_get_mappable_range()
  2021-01-18 13:13   ` Anshuman Khandual
@ 2021-01-19 12:24     ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:24 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 18.01.21 14:13, Anshuman Khandual wrote:
> 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. It
> also adds a VM_BUG_ON() check that would ensure that memhp_range_allowed()
> has already been called.
> 
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will@kernel.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: linux-arm-kernel@lists.infradead.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> ---
>  arch/arm64/mm/mmu.c | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index ae0c3d023824..f2e1770c9f29 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1442,16 +1442,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,
> @@ -1459,11 +1462,7 @@ 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;
> -	}
> -
> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>  	if (rodata_full || debug_pagealloc_enabled())
>  		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
>  
> 

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 2/3] arm64/mm: Define arch_get_mappable_range()
@ 2021-01-19 12:24     ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:24 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 18.01.21 14:13, Anshuman Khandual wrote:
> 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. It
> also adds a VM_BUG_ON() check that would ensure that memhp_range_allowed()
> has already been called.
> 
> Cc: Catalin Marinas <catalin.marinas@arm.com>
> Cc: Will Deacon <will@kernel.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: linux-arm-kernel@lists.infradead.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> ---
>  arch/arm64/mm/mmu.c | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
> index ae0c3d023824..f2e1770c9f29 100644
> --- a/arch/arm64/mm/mmu.c
> +++ b/arch/arm64/mm/mmu.c
> @@ -1442,16 +1442,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,
> @@ -1459,11 +1462,7 @@ 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;
> -	}
> -
> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>  	if (rodata_full || debug_pagealloc_enabled())
>  		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
>  
> 

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
  2021-01-18 13:13   ` Anshuman Khandual
@ 2021-01-19 12:26     ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:26 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 18.01.21 14:13, Anshuman Khandual wrote:
> This overrides arch_get_mappabble_range() on s390 platform which will be
> used with recently added generic framework. It modifies the existing range
> check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
> VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
> been called on the hotplug path.
> 
> Cc: Heiko Carstens <hca@linux.ibm.com>
> Cc: Vasily Gorbik <gor@linux.ibm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: linux-s390@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Acked-by: Heiko Carstens <hca@linux.ibm.com>
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> ---
>  arch/s390/mm/init.c |  1 +
>  arch/s390/mm/vmem.c | 15 ++++++++++++++-
>  2 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
> index 73a163065b95..97017a4bcc90 100644
> --- a/arch/s390/mm/init.c
> +++ b/arch/s390/mm/init.c
> @@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
>  	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
>  		return -EINVAL;
>  
> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>  	rc = vmem_add_mapping(start, size);
>  	if (rc)
>  		return rc;
> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
> index 01f3a5f58e64..afc39ff1cc8d 100644
> --- a/arch/s390/mm/vmem.c
> +++ b/arch/s390/mm/vmem.c
> @@ -4,6 +4,7 @@
>   *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
>   */
>  
> +#include <linux/memory_hotplug.h>
>  #include <linux/memblock.h>
>  #include <linux/pfn.h>
>  #include <linux/mm.h>
> @@ -532,11 +533,23 @@ 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;

You could do:

memhp_range = {
	.start = 0,
	.end =  VMEM_MAX_PHYS - 1,
};

Similar in the arm64 patch.

> +
> +	memhp_range.start = 0;
> +	memhp_range.end =  VMEM_MAX_PHYS - 1;
> +	return memhp_range;
> +}
> +
>  int vmem_add_mapping(unsigned long start, unsigned long size)
>  {
> +	struct range range;
>  	int ret;
>  
> -	if (start + size > VMEM_MAX_PHYS ||
> +	range = arch_get_mappable_range();

You could do

struct range range = arch_get_mappable_range();

> +	if (start < range.start ||
> +	    start + size > range.end + 1 ||
>  	    start + size < start)
>  		return -ERANGE;
>  
> 


-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
@ 2021-01-19 12:26     ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:26 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 18.01.21 14:13, Anshuman Khandual wrote:
> This overrides arch_get_mappabble_range() on s390 platform which will be
> used with recently added generic framework. It modifies the existing range
> check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
> VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
> been called on the hotplug path.
> 
> Cc: Heiko Carstens <hca@linux.ibm.com>
> Cc: Vasily Gorbik <gor@linux.ibm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Cc: linux-s390@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Acked-by: Heiko Carstens <hca@linux.ibm.com>
> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> ---
>  arch/s390/mm/init.c |  1 +
>  arch/s390/mm/vmem.c | 15 ++++++++++++++-
>  2 files changed, 15 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
> index 73a163065b95..97017a4bcc90 100644
> --- a/arch/s390/mm/init.c
> +++ b/arch/s390/mm/init.c
> @@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
>  	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
>  		return -EINVAL;
>  
> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>  	rc = vmem_add_mapping(start, size);
>  	if (rc)
>  		return rc;
> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
> index 01f3a5f58e64..afc39ff1cc8d 100644
> --- a/arch/s390/mm/vmem.c
> +++ b/arch/s390/mm/vmem.c
> @@ -4,6 +4,7 @@
>   *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
>   */
>  
> +#include <linux/memory_hotplug.h>
>  #include <linux/memblock.h>
>  #include <linux/pfn.h>
>  #include <linux/mm.h>
> @@ -532,11 +533,23 @@ 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;

You could do:

memhp_range = {
	.start = 0,
	.end =  VMEM_MAX_PHYS - 1,
};

Similar in the arm64 patch.

> +
> +	memhp_range.start = 0;
> +	memhp_range.end =  VMEM_MAX_PHYS - 1;
> +	return memhp_range;
> +}
> +
>  int vmem_add_mapping(unsigned long start, unsigned long size)
>  {
> +	struct range range;
>  	int ret;
>  
> -	if (start + size > VMEM_MAX_PHYS ||
> +	range = arch_get_mappable_range();

You could do

struct range range = arch_get_mappable_range();

> +	if (start < range.start ||
> +	    start + size > range.end + 1 ||
>  	    start + size < start)
>  		return -ERANGE;
>  
> 


-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-18 13:21     ` Anshuman Khandual
@ 2021-01-19 12:27       ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:27 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, Michal Hocko,
	Wei Yang, teawater, Pankaj Gupta, Jonathan Cameron

On 18.01.21 14:21, Anshuman Khandual wrote:
> 
> 
> On 1/18/21 6:43 PM, Anshuman Khandual wrote:
>> From: David Hildenbrand <david@redhat.com>
>>
>> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
>> are more restrictions of which memory we can actually hotplug, especially
>> om arm64 or s390x once we support them: we might receive something like
>> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
>> operation.
>>
>> So, check right when initializing the device which memory we can add,
>> warning the user. Try only adding actually pluggable ranges: in the worst
>> case, no memory provided by our device is pluggable.
>>
>> In the usual case, we expect all device memory to be pluggable, and in
>> corner cases only some memory at the end of the device-managed memory
>> region to not be pluggable.
>>
>> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>> Cc: Jason Wang <jasowang@redhat.com>
>> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Cc: Oscar Salvador <osalvador@suse.de>
>> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: catalin.marinas@arm.com
>> Cc: teawater <teawaterz@linux.alibaba.com>
>> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
>> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
>> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> Cc: hca@linux.ibm.com
>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>> Cc: Will Deacon <will@kernel.org>
>> Cc: Ard Biesheuvel <ardb@kernel.org>
>> Cc: Mark Rutland <mark.rutland@arm.com>
>> Cc: Heiko Carstens <hca@linux.ibm.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> 
> Hello David,
> 
> As your original patch was in the RFC state, I have just maintained
> the same here as well. But once you test this patch along with the
> new series, please do let me know if this needs to be converted to
> a normal PATCH instead. Thank you.

I'll give it a churn on x86-64, where not that much should change. It
will be interesting to test with arm64 in such corner cases in the future.

Thanks

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
@ 2021-01-19 12:27       ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 12:27 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Wei Yang, Vasily Gorbik,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, linux-kernel,
	Michal Hocko, Pankaj Gupta, teawater, Jonathan Cameron,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 18.01.21 14:21, Anshuman Khandual wrote:
> 
> 
> On 1/18/21 6:43 PM, Anshuman Khandual wrote:
>> From: David Hildenbrand <david@redhat.com>
>>
>> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
>> are more restrictions of which memory we can actually hotplug, especially
>> om arm64 or s390x once we support them: we might receive something like
>> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
>> operation.
>>
>> So, check right when initializing the device which memory we can add,
>> warning the user. Try only adding actually pluggable ranges: in the worst
>> case, no memory provided by our device is pluggable.
>>
>> In the usual case, we expect all device memory to be pluggable, and in
>> corner cases only some memory at the end of the device-managed memory
>> region to not be pluggable.
>>
>> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>> Cc: Jason Wang <jasowang@redhat.com>
>> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Cc: Oscar Salvador <osalvador@suse.de>
>> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: catalin.marinas@arm.com
>> Cc: teawater <teawaterz@linux.alibaba.com>
>> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
>> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
>> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> Cc: hca@linux.ibm.com
>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>> Cc: Will Deacon <will@kernel.org>
>> Cc: Ard Biesheuvel <ardb@kernel.org>
>> Cc: Mark Rutland <mark.rutland@arm.com>
>> Cc: Heiko Carstens <hca@linux.ibm.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> 
> Hello David,
> 
> As your original patch was in the RFC state, I have just maintained
> the same here as well. But once you test this patch along with the
> new series, please do let me know if this needs to be converted to
> a normal PATCH instead. Thank you.

I'll give it a churn on x86-64, where not that much should change. It
will be interesting to test with arm64 in such corner cases in the future.

Thanks

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
  2021-01-18 13:12 ` Anshuman Khandual
@ 2021-01-19 13:33   ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 13:33 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 18.01.21 14:12, Anshuman Khandual wrote:
> 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/1600332402-30123-1-git-send-email-anshuman.khandual@arm.com/
> 
> 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.11-rc4 and has been tested on arm64. But only
> build tested on s390.
> 
> Changes in V3
> 
> - Updated the commit message in [PATCH 1/3]
> - Replaced 1 with 'true' and 0 with 'false' in memhp_range_allowed()
> - Updated memhp_range.end as VMEM_MAX_PHYS - 1 and updated vmem_add_mapping() on s390
> - Changed memhp_range_allowed() behaviour in __add_pages()
> - Updated __add_pages() to return E2BIG when memhp_range_allowed() fails for non-linear mapping based requests

Minor thing, we should make up our mind if we want to call stuff
internally "memhp_" or "mhp". I prefer the latter, because it is shorter.


-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-19 13:33   ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-19 13:33 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 18.01.21 14:12, Anshuman Khandual wrote:
> 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/1600332402-30123-1-git-send-email-anshuman.khandual@arm.com/
> 
> 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.11-rc4 and has been tested on arm64. But only
> build tested on s390.
> 
> Changes in V3
> 
> - Updated the commit message in [PATCH 1/3]
> - Replaced 1 with 'true' and 0 with 'false' in memhp_range_allowed()
> - Updated memhp_range.end as VMEM_MAX_PHYS - 1 and updated vmem_add_mapping() on s390
> - Changed memhp_range_allowed() behaviour in __add_pages()
> - Updated __add_pages() to return E2BIG when memhp_range_allowed() fails for non-linear mapping based requests

Minor thing, we should make up our mind if we want to call stuff
internally "memhp_" or "mhp". I prefer the latter, because it is shorter.


-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
  2021-01-19 13:33   ` David Hildenbrand
@ 2021-01-19 13:40     ` Oscar Salvador
  -1 siblings, 0 replies; 57+ messages in thread
From: Oscar Salvador @ 2021-01-19 13:40 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas,
	Vasily Gorbik, Will Deacon, Ard Biesheuvel, Mark Rutland,
	linux-arm-kernel, linux-s390, linux-kernel

On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
> Minor thing, we should make up our mind if we want to call stuff
> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.

I would rather use the latter as well. I used that in [1].
MEMHP_MERGE_RESOURCE should be renamed if we agree on that.

[1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/


-- 
Oscar Salvador
SUSE L3

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-19 13:40     ` Oscar Salvador
  0 siblings, 0 replies; 57+ messages in thread
From: Oscar Salvador @ 2021-01-19 13:40 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, Anshuman Khandual,
	catalin.marinas, hca, linux-kernel, linux-mm, akpm, Will Deacon,
	Ard Biesheuvel, linux-arm-kernel

On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
> Minor thing, we should make up our mind if we want to call stuff
> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.

I would rather use the latter as well. I used that in [1].
MEMHP_MERGE_RESOURCE should be renamed if we agree on that.

[1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/


-- 
Oscar Salvador
SUSE L3

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
  2021-01-19 12:26     ` David Hildenbrand
@ 2021-01-20  8:28       ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-20  8:28 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel



On 1/19/21 5:56 PM, David Hildenbrand wrote:
> On 18.01.21 14:13, Anshuman Khandual wrote:
>> This overrides arch_get_mappabble_range() on s390 platform which will be
>> used with recently added generic framework. It modifies the existing range
>> check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
>> VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
>> been called on the hotplug path.
>>
>> Cc: Heiko Carstens <hca@linux.ibm.com>
>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>> Cc: David Hildenbrand <david@redhat.com>
>> Cc: linux-s390@vger.kernel.org
>> Cc: linux-kernel@vger.kernel.org
>> Acked-by: Heiko Carstens <hca@linux.ibm.com>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
>> ---
>>  arch/s390/mm/init.c |  1 +
>>  arch/s390/mm/vmem.c | 15 ++++++++++++++-
>>  2 files changed, 15 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
>> index 73a163065b95..97017a4bcc90 100644
>> --- a/arch/s390/mm/init.c
>> +++ b/arch/s390/mm/init.c
>> @@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>  	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
>>  		return -EINVAL;
>>  
>> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>>  	rc = vmem_add_mapping(start, size);
>>  	if (rc)
>>  		return rc;
>> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
>> index 01f3a5f58e64..afc39ff1cc8d 100644
>> --- a/arch/s390/mm/vmem.c
>> +++ b/arch/s390/mm/vmem.c
>> @@ -4,6 +4,7 @@
>>   *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
>>   */
>>  
>> +#include <linux/memory_hotplug.h>
>>  #include <linux/memblock.h>
>>  #include <linux/pfn.h>
>>  #include <linux/mm.h>
>> @@ -532,11 +533,23 @@ 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;
> 
> You could do:
> 
> memhp_range = {
> 	.start = 0,
> 	.end =  VMEM_MAX_PHYS - 1,
> };
> 
> Similar in the arm64 patch.

There is a comment block just before this assignment on arm64. Also
it seems like code style preference and Heiko had originally agreed
on this particular patch. Could we just leave it unchanged please ?

> 
>> +
>> +	memhp_range.start = 0;
>> +	memhp_range.end =  VMEM_MAX_PHYS - 1;
>> +	return memhp_range;
>> +}
>> +
>>  int vmem_add_mapping(unsigned long start, unsigned long size)
>>  {
>> +	struct range range;
>>  	int ret;
>>  
>> -	if (start + size > VMEM_MAX_PHYS ||
>> +	range = arch_get_mappable_range();
> 
> You could do
> 
> struct range range = arch_get_mappable_range();

Sure, will change this though.

> 
>> +	if (start < range.start ||
>> +	    start + size > range.end + 1 ||
>>  	    start + size < start)
>>  		return -ERANGE;
>>  
>>
> 
> 

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
@ 2021-01-20  8:28       ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-20  8:28 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador



On 1/19/21 5:56 PM, David Hildenbrand wrote:
> On 18.01.21 14:13, Anshuman Khandual wrote:
>> This overrides arch_get_mappabble_range() on s390 platform which will be
>> used with recently added generic framework. It modifies the existing range
>> check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
>> VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
>> been called on the hotplug path.
>>
>> Cc: Heiko Carstens <hca@linux.ibm.com>
>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>> Cc: David Hildenbrand <david@redhat.com>
>> Cc: linux-s390@vger.kernel.org
>> Cc: linux-kernel@vger.kernel.org
>> Acked-by: Heiko Carstens <hca@linux.ibm.com>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
>> ---
>>  arch/s390/mm/init.c |  1 +
>>  arch/s390/mm/vmem.c | 15 ++++++++++++++-
>>  2 files changed, 15 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
>> index 73a163065b95..97017a4bcc90 100644
>> --- a/arch/s390/mm/init.c
>> +++ b/arch/s390/mm/init.c
>> @@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>  	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
>>  		return -EINVAL;
>>  
>> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>>  	rc = vmem_add_mapping(start, size);
>>  	if (rc)
>>  		return rc;
>> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
>> index 01f3a5f58e64..afc39ff1cc8d 100644
>> --- a/arch/s390/mm/vmem.c
>> +++ b/arch/s390/mm/vmem.c
>> @@ -4,6 +4,7 @@
>>   *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
>>   */
>>  
>> +#include <linux/memory_hotplug.h>
>>  #include <linux/memblock.h>
>>  #include <linux/pfn.h>
>>  #include <linux/mm.h>
>> @@ -532,11 +533,23 @@ 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;
> 
> You could do:
> 
> memhp_range = {
> 	.start = 0,
> 	.end =  VMEM_MAX_PHYS - 1,
> };
> 
> Similar in the arm64 patch.

There is a comment block just before this assignment on arm64. Also
it seems like code style preference and Heiko had originally agreed
on this particular patch. Could we just leave it unchanged please ?

> 
>> +
>> +	memhp_range.start = 0;
>> +	memhp_range.end =  VMEM_MAX_PHYS - 1;
>> +	return memhp_range;
>> +}
>> +
>>  int vmem_add_mapping(unsigned long start, unsigned long size)
>>  {
>> +	struct range range;
>>  	int ret;
>>  
>> -	if (start + size > VMEM_MAX_PHYS ||
>> +	range = arch_get_mappable_range();
> 
> You could do
> 
> struct range range = arch_get_mappable_range();

Sure, will change this though.

> 
>> +	if (start < range.start ||
>> +	    start + size > range.end + 1 ||
>>  	    start + size < start)
>>  		return -ERANGE;
>>  
>>
> 
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-19 12:21     ` David Hildenbrand
@ 2021-01-20  8:33       ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-20  8:33 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel



On 1/19/21 5:51 PM, David Hildenbrand wrote:
> On 18.01.21 14:12, Anshuman Khandual wrote:
>> 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() but instead adds a VM_BUG_ON() check which would
> 
> In this patch, you keep the __add_pages() checks. But as discussed, we
> could perform it in mm/memremap.c:pagemap_range() insted and convert it
> to a VM_BUG_ON().

Just to be sure, will the following change achieve what you are
suggesting here. pagemap_range() after this change, will again
be the same like the V1 series.

---
 mm/memory_hotplug.c |  3 +--
 mm/memremap.c       | 12 +++++-------
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 46faa914aa25..10d4ec8f349c 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -304,8 +304,7 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 	if (WARN_ON_ONCE(!params->pgprot.pgprot))
 		return -EINVAL;
 
-	if(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false))
-		return -E2BIG;
+	VM_BUG_ON(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false));
 
 	if (altmap) {
 		/*
diff --git a/mm/memremap.c b/mm/memremap.c
index e15b13736f6a..26c1825756cc 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -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 {
@@ -253,12 +257,6 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 			goto err_kasan;
 		}
 
-		if (!memhp_range_allowed(range->start, range_len(range), true)) {
-			error = -ERANGE;
-			mem_hotplug_done();
-			goto err_add_memory;
-		}
-
 		error = arch_add_memory(nid, range->start, range_len(range),
 					params);
 	}
-- 
2.20.1

^ permalink raw reply related	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-20  8:33       ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-20  8:33 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador



On 1/19/21 5:51 PM, David Hildenbrand wrote:
> On 18.01.21 14:12, Anshuman Khandual wrote:
>> 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() but instead adds a VM_BUG_ON() check which would
> 
> In this patch, you keep the __add_pages() checks. But as discussed, we
> could perform it in mm/memremap.c:pagemap_range() insted and convert it
> to a VM_BUG_ON().

Just to be sure, will the following change achieve what you are
suggesting here. pagemap_range() after this change, will again
be the same like the V1 series.

---
 mm/memory_hotplug.c |  3 +--
 mm/memremap.c       | 12 +++++-------
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 46faa914aa25..10d4ec8f349c 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -304,8 +304,7 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
 	if (WARN_ON_ONCE(!params->pgprot.pgprot))
 		return -EINVAL;
 
-	if(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false))
-		return -E2BIG;
+	VM_BUG_ON(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false));
 
 	if (altmap) {
 		/*
diff --git a/mm/memremap.c b/mm/memremap.c
index e15b13736f6a..26c1825756cc 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -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 {
@@ -253,12 +257,6 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
 			goto err_kasan;
 		}
 
-		if (!memhp_range_allowed(range->start, range_len(range), true)) {
-			error = -ERANGE;
-			mem_hotplug_done();
-			goto err_add_memory;
-		}
-
 		error = arch_add_memory(nid, range->start, range_len(range),
 					params);
 	}
-- 
2.20.1

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
  2021-01-19 13:40     ` Oscar Salvador
@ 2021-01-20  8:37       ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-20  8:37 UTC (permalink / raw)
  To: Oscar Salvador, David Hildenbrand
  Cc: linux-mm, akpm, hca, catalin.marinas, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel



On 1/19/21 7:10 PM, Oscar Salvador wrote:
> On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
>> Minor thing, we should make up our mind if we want to call stuff
>> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.
> 
> I would rather use the latter as well. I used that in [1].

Okay, will change all that is 'memhp' as 'mhp' instead.

> MEMHP_MERGE_RESOURCE should be renamed if we agree on that.
> 
> [1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/
> 
> 

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-20  8:37       ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-20  8:37 UTC (permalink / raw)
  To: Oscar Salvador, David Hildenbrand
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, catalin.marinas, hca,
	linux-kernel, linux-mm, akpm, Will Deacon, Ard Biesheuvel,
	linux-arm-kernel



On 1/19/21 7:10 PM, Oscar Salvador wrote:
> On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
>> Minor thing, we should make up our mind if we want to call stuff
>> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.
> 
> I would rather use the latter as well. I used that in [1].

Okay, will change all that is 'memhp' as 'mhp' instead.

> MEMHP_MERGE_RESOURCE should be renamed if we agree on that.
> 
> [1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/
> 
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
  2021-01-20  8:28       ` Anshuman Khandual
@ 2021-01-20 10:39         ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-20 10:39 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 20.01.21 09:28, Anshuman Khandual wrote:
> 
> 
> On 1/19/21 5:56 PM, David Hildenbrand wrote:
>> On 18.01.21 14:13, Anshuman Khandual wrote:
>>> This overrides arch_get_mappabble_range() on s390 platform which will be
>>> used with recently added generic framework. It modifies the existing range
>>> check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
>>> VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
>>> been called on the hotplug path.
>>>
>>> Cc: Heiko Carstens <hca@linux.ibm.com>
>>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>>> Cc: David Hildenbrand <david@redhat.com>
>>> Cc: linux-s390@vger.kernel.org
>>> Cc: linux-kernel@vger.kernel.org
>>> Acked-by: Heiko Carstens <hca@linux.ibm.com>
>>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
>>> ---
>>>  arch/s390/mm/init.c |  1 +
>>>  arch/s390/mm/vmem.c | 15 ++++++++++++++-
>>>  2 files changed, 15 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
>>> index 73a163065b95..97017a4bcc90 100644
>>> --- a/arch/s390/mm/init.c
>>> +++ b/arch/s390/mm/init.c
>>> @@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>>  	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
>>>  		return -EINVAL;
>>>  
>>> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>>>  	rc = vmem_add_mapping(start, size);
>>>  	if (rc)
>>>  		return rc;
>>> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
>>> index 01f3a5f58e64..afc39ff1cc8d 100644
>>> --- a/arch/s390/mm/vmem.c
>>> +++ b/arch/s390/mm/vmem.c
>>> @@ -4,6 +4,7 @@
>>>   *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
>>>   */
>>>  
>>> +#include <linux/memory_hotplug.h>
>>>  #include <linux/memblock.h>
>>>  #include <linux/pfn.h>
>>>  #include <linux/mm.h>
>>> @@ -532,11 +533,23 @@ 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;
>>
>> You could do:
>>
>> memhp_range = {
>> 	.start = 0,
>> 	.end =  VMEM_MAX_PHYS - 1,
>> };
>>
>> Similar in the arm64 patch.
> 
> There is a comment block just before this assignment on arm64. Also
> it seems like code style preference and Heiko had originally agreed
> on this particular patch. Could we just leave it unchanged please ?

That's not how review works. But as I said, "You could do".

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 3/3] s390/mm: Define arch_get_mappable_range()
@ 2021-01-20 10:39         ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-20 10:39 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 20.01.21 09:28, Anshuman Khandual wrote:
> 
> 
> On 1/19/21 5:56 PM, David Hildenbrand wrote:
>> On 18.01.21 14:13, Anshuman Khandual wrote:
>>> This overrides arch_get_mappabble_range() on s390 platform which will be
>>> used with recently added generic framework. It modifies the existing range
>>> check in vmem_add_mapping() using arch_get_mappable_range(). It also adds a
>>> VM_BUG_ON() check that would ensure that memhp_range_allowed() has already
>>> been called on the hotplug path.
>>>
>>> Cc: Heiko Carstens <hca@linux.ibm.com>
>>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>>> Cc: David Hildenbrand <david@redhat.com>
>>> Cc: linux-s390@vger.kernel.org
>>> Cc: linux-kernel@vger.kernel.org
>>> Acked-by: Heiko Carstens <hca@linux.ibm.com>
>>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
>>> ---
>>>  arch/s390/mm/init.c |  1 +
>>>  arch/s390/mm/vmem.c | 15 ++++++++++++++-
>>>  2 files changed, 15 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/arch/s390/mm/init.c b/arch/s390/mm/init.c
>>> index 73a163065b95..97017a4bcc90 100644
>>> --- a/arch/s390/mm/init.c
>>> +++ b/arch/s390/mm/init.c
>>> @@ -297,6 +297,7 @@ int arch_add_memory(int nid, u64 start, u64 size,
>>>  	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
>>>  		return -EINVAL;
>>>  
>>> +	VM_BUG_ON(!memhp_range_allowed(start, size, true));
>>>  	rc = vmem_add_mapping(start, size);
>>>  	if (rc)
>>>  		return rc;
>>> diff --git a/arch/s390/mm/vmem.c b/arch/s390/mm/vmem.c
>>> index 01f3a5f58e64..afc39ff1cc8d 100644
>>> --- a/arch/s390/mm/vmem.c
>>> +++ b/arch/s390/mm/vmem.c
>>> @@ -4,6 +4,7 @@
>>>   *    Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
>>>   */
>>>  
>>> +#include <linux/memory_hotplug.h>
>>>  #include <linux/memblock.h>
>>>  #include <linux/pfn.h>
>>>  #include <linux/mm.h>
>>> @@ -532,11 +533,23 @@ 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;
>>
>> You could do:
>>
>> memhp_range = {
>> 	.start = 0,
>> 	.end =  VMEM_MAX_PHYS - 1,
>> };
>>
>> Similar in the arm64 patch.
> 
> There is a comment block just before this assignment on arm64. Also
> it seems like code style preference and Heiko had originally agreed
> on this particular patch. Could we just leave it unchanged please ?

That's not how review works. But as I said, "You could do".

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-20  8:33       ` Anshuman Khandual
@ 2021-01-20 10:41         ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-20 10:41 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 20.01.21 09:33, Anshuman Khandual wrote:
> 
> 
> On 1/19/21 5:51 PM, David Hildenbrand wrote:
>> On 18.01.21 14:12, Anshuman Khandual wrote:
>>> 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() but instead adds a VM_BUG_ON() check which would
>>
>> In this patch, you keep the __add_pages() checks. But as discussed, we
>> could perform it in mm/memremap.c:pagemap_range() insted and convert it
>> to a VM_BUG_ON().
> 
> Just to be sure, will the following change achieve what you are
> suggesting here. pagemap_range() after this change, will again
> be the same like the V1 series.

Yeah, as we used to have in v1. Maybe other reviewers (@Oscar?) have a
different opinion.

If you decide to leave as-is, please fixup the patch description. Thanks!

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-20 10:41         ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-20 10:41 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 20.01.21 09:33, Anshuman Khandual wrote:
> 
> 
> On 1/19/21 5:51 PM, David Hildenbrand wrote:
>> On 18.01.21 14:12, Anshuman Khandual wrote:
>>> 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() but instead adds a VM_BUG_ON() check which would
>>
>> In this patch, you keep the __add_pages() checks. But as discussed, we
>> could perform it in mm/memremap.c:pagemap_range() insted and convert it
>> to a VM_BUG_ON().
> 
> Just to be sure, will the following change achieve what you are
> suggesting here. pagemap_range() after this change, will again
> be the same like the V1 series.

Yeah, as we used to have in v1. Maybe other reviewers (@Oscar?) have a
different opinion.

If you decide to leave as-is, please fixup the patch description. Thanks!

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-20 10:41         ` David Hildenbrand
@ 2021-01-20 11:58           ` Oscar Salvador
  -1 siblings, 0 replies; 57+ messages in thread
From: Oscar Salvador @ 2021-01-20 11:58 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas,
	Vasily Gorbik, Will Deacon, Ard Biesheuvel, Mark Rutland,
	linux-arm-kernel, linux-s390, linux-kernel

On Wed, Jan 20, 2021 at 11:41:53AM +0100, David Hildenbrand wrote:
> On 20.01.21 09:33, Anshuman Khandual wrote:
> > 
> > 
> > On 1/19/21 5:51 PM, David Hildenbrand wrote:
> >> On 18.01.21 14:12, Anshuman Khandual wrote:
> >>> 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() but instead adds a VM_BUG_ON() check which would
> >>
> >> In this patch, you keep the __add_pages() checks. But as discussed, we
> >> could perform it in mm/memremap.c:pagemap_range() insted and convert it
> >> to a VM_BUG_ON().
> > 
> > Just to be sure, will the following change achieve what you are
> > suggesting here. pagemap_range() after this change, will again
> > be the same like the V1 series.
> 
> Yeah, as we used to have in v1. Maybe other reviewers (@Oscar?) have a
> different opinion.

No, I think that placing the check in pagemap_range() out of the if-else
makes much more sense.
Actually, unless my memory  fails me that is what I suggested in v2.

I plan to have a look at the series later this week as I am fairly busy
atm.

Thanks


-- 
Oscar Salvador
SUSE L3

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-20 11:58           ` Oscar Salvador
  0 siblings, 0 replies; 57+ messages in thread
From: Oscar Salvador @ 2021-01-20 11:58 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, Anshuman Khandual,
	catalin.marinas, hca, linux-kernel, linux-mm, akpm, Will Deacon,
	Ard Biesheuvel, linux-arm-kernel

On Wed, Jan 20, 2021 at 11:41:53AM +0100, David Hildenbrand wrote:
> On 20.01.21 09:33, Anshuman Khandual wrote:
> > 
> > 
> > On 1/19/21 5:51 PM, David Hildenbrand wrote:
> >> On 18.01.21 14:12, Anshuman Khandual wrote:
> >>> 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() but instead adds a VM_BUG_ON() check which would
> >>
> >> In this patch, you keep the __add_pages() checks. But as discussed, we
> >> could perform it in mm/memremap.c:pagemap_range() insted and convert it
> >> to a VM_BUG_ON().
> > 
> > Just to be sure, will the following change achieve what you are
> > suggesting here. pagemap_range() after this change, will again
> > be the same like the V1 series.
> 
> Yeah, as we used to have in v1. Maybe other reviewers (@Oscar?) have a
> different opinion.

No, I think that placing the check in pagemap_range() out of the if-else
makes much more sense.
Actually, unless my memory  fails me that is what I suggested in v2.

I plan to have a look at the series later this week as I am fairly busy
atm.

Thanks


-- 
Oscar Salvador
SUSE L3

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-20  8:33       ` Anshuman Khandual
@ 2021-01-21  9:23         ` Oscar Salvador
  -1 siblings, 0 replies; 57+ messages in thread
From: Oscar Salvador @ 2021-01-21  9:23 UTC (permalink / raw)
  To: Anshuman Khandual
  Cc: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas,
	Vasily Gorbik, Will Deacon, Ard Biesheuvel, Mark Rutland,
	linux-arm-kernel, linux-s390, linux-kernel

On Wed, Jan 20, 2021 at 02:03:45PM +0530, Anshuman Khandual wrote:
> Just to be sure, will the following change achieve what you are
> suggesting here. pagemap_range() after this change, will again
> be the same like the V1 series.

With below diff on top it looks good to me:

Reviewed-by: Oscar Salvador <osalvador@suse.de>

The only nit I would have is whether the declaration of arch_get_mappable_range
should be in include/linux/memory_hotplug.h.
As you pointed out, arch_get_mappable_range() might be used by the platform
for other purposes, and since you are defining it out of CONFIG_MEMORY_HOTPLUG
anyway.
Would include/linu/memory.h be a better fit?

As I said, nothing to bikeshed about, just my thoughts.

> ---
>  mm/memory_hotplug.c |  3 +--
>  mm/memremap.c       | 12 +++++-------
>  2 files changed, 6 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 46faa914aa25..10d4ec8f349c 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -304,8 +304,7 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
>  	if (WARN_ON_ONCE(!params->pgprot.pgprot))
>  		return -EINVAL;
>  
> -	if(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false))
> -		return -E2BIG;
> +	VM_BUG_ON(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false));
>  
>  	if (altmap) {
>  		/*
> diff --git a/mm/memremap.c b/mm/memremap.c
> index e15b13736f6a..26c1825756cc 100644
> --- a/mm/memremap.c
> +++ b/mm/memremap.c
> @@ -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 {
> @@ -253,12 +257,6 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
>  			goto err_kasan;
>  		}
>  
> -		if (!memhp_range_allowed(range->start, range_len(range), true)) {
> -			error = -ERANGE;
> -			mem_hotplug_done();
> -			goto err_add_memory;
> -		}
> -
>  		error = arch_add_memory(nid, range->start, range_len(range),
>  					params);
>  	}
> -- 
> 2.20.1
> 

-- 
Oscar Salvador
SUSE L3

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-21  9:23         ` Oscar Salvador
  0 siblings, 0 replies; 57+ messages in thread
From: Oscar Salvador @ 2021-01-21  9:23 UTC (permalink / raw)
  To: Anshuman Khandual
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, David Hildenbrand,
	catalin.marinas, hca, linux-kernel, linux-mm, akpm, Will Deacon,
	Ard Biesheuvel, linux-arm-kernel

On Wed, Jan 20, 2021 at 02:03:45PM +0530, Anshuman Khandual wrote:
> Just to be sure, will the following change achieve what you are
> suggesting here. pagemap_range() after this change, will again
> be the same like the V1 series.

With below diff on top it looks good to me:

Reviewed-by: Oscar Salvador <osalvador@suse.de>

The only nit I would have is whether the declaration of arch_get_mappable_range
should be in include/linux/memory_hotplug.h.
As you pointed out, arch_get_mappable_range() might be used by the platform
for other purposes, and since you are defining it out of CONFIG_MEMORY_HOTPLUG
anyway.
Would include/linu/memory.h be a better fit?

As I said, nothing to bikeshed about, just my thoughts.

> ---
>  mm/memory_hotplug.c |  3 +--
>  mm/memremap.c       | 12 +++++-------
>  2 files changed, 6 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
> index 46faa914aa25..10d4ec8f349c 100644
> --- a/mm/memory_hotplug.c
> +++ b/mm/memory_hotplug.c
> @@ -304,8 +304,7 @@ int __ref __add_pages(int nid, unsigned long pfn, unsigned long nr_pages,
>  	if (WARN_ON_ONCE(!params->pgprot.pgprot))
>  		return -EINVAL;
>  
> -	if(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false))
> -		return -E2BIG;
> +	VM_BUG_ON(!memhp_range_allowed(PFN_PHYS(pfn), nr_pages * PAGE_SIZE, false));
>  
>  	if (altmap) {
>  		/*
> diff --git a/mm/memremap.c b/mm/memremap.c
> index e15b13736f6a..26c1825756cc 100644
> --- a/mm/memremap.c
> +++ b/mm/memremap.c
> @@ -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 {
> @@ -253,12 +257,6 @@ static int pagemap_range(struct dev_pagemap *pgmap, struct mhp_params *params,
>  			goto err_kasan;
>  		}
>  
> -		if (!memhp_range_allowed(range->start, range_len(range), true)) {
> -			error = -ERANGE;
> -			mem_hotplug_done();
> -			goto err_add_memory;
> -		}
> -
>  		error = arch_add_memory(nid, range->start, range_len(range),
>  					params);
>  	}
> -- 
> 2.20.1
> 

-- 
Oscar Salvador
SUSE L3

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-18 13:21     ` Anshuman Khandual
@ 2021-01-21  9:57       ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-21  9:57 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, Michal Hocko,
	Wei Yang, teawater, Pankaj Gupta, Jonathan Cameron

On 18.01.21 14:21, Anshuman Khandual wrote:
> 
> 
> On 1/18/21 6:43 PM, Anshuman Khandual wrote:
>> From: David Hildenbrand <david@redhat.com>
>>
>> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
>> are more restrictions of which memory we can actually hotplug, especially
>> om arm64 or s390x once we support them: we might receive something like
>> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
>> operation.
>>
>> So, check right when initializing the device which memory we can add,
>> warning the user. Try only adding actually pluggable ranges: in the worst
>> case, no memory provided by our device is pluggable.
>>
>> In the usual case, we expect all device memory to be pluggable, and in
>> corner cases only some memory at the end of the device-managed memory
>> region to not be pluggable.
>>
>> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>> Cc: Jason Wang <jasowang@redhat.com>
>> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Cc: Oscar Salvador <osalvador@suse.de>
>> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: catalin.marinas@arm.com
>> Cc: teawater <teawaterz@linux.alibaba.com>
>> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
>> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
>> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> Cc: hca@linux.ibm.com
>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>> Cc: Will Deacon <will@kernel.org>
>> Cc: Ard Biesheuvel <ardb@kernel.org>
>> Cc: Mark Rutland <mark.rutland@arm.com>
>> Cc: Heiko Carstens <hca@linux.ibm.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> 
> Hello David,
> 
> As your original patch was in the RFC state, I have just maintained
> the same here as well. But once you test this patch along with the
> new series, please do let me know if this needs to be converted to
> a normal PATCH instead. Thank you.

Yes, you can drop the RFC part. I assume you'll send another revision,
I'll do another test there, thanks!


-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
@ 2021-01-21  9:57       ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-21  9:57 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Wei Yang, Vasily Gorbik,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, linux-kernel,
	Michal Hocko, Pankaj Gupta, teawater, Jonathan Cameron,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 18.01.21 14:21, Anshuman Khandual wrote:
> 
> 
> On 1/18/21 6:43 PM, Anshuman Khandual wrote:
>> From: David Hildenbrand <david@redhat.com>
>>
>> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
>> are more restrictions of which memory we can actually hotplug, especially
>> om arm64 or s390x once we support them: we might receive something like
>> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
>> operation.
>>
>> So, check right when initializing the device which memory we can add,
>> warning the user. Try only adding actually pluggable ranges: in the worst
>> case, no memory provided by our device is pluggable.
>>
>> In the usual case, we expect all device memory to be pluggable, and in
>> corner cases only some memory at the end of the device-managed memory
>> region to not be pluggable.
>>
>> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>> Cc: Jason Wang <jasowang@redhat.com>
>> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Cc: Oscar Salvador <osalvador@suse.de>
>> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: catalin.marinas@arm.com
>> Cc: teawater <teawaterz@linux.alibaba.com>
>> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
>> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
>> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> Cc: hca@linux.ibm.com
>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>> Cc: Will Deacon <will@kernel.org>
>> Cc: Ard Biesheuvel <ardb@kernel.org>
>> Cc: Mark Rutland <mark.rutland@arm.com>
>> Cc: Heiko Carstens <hca@linux.ibm.com>
>> Cc: Michal Hocko <mhocko@kernel.org>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
> 
> Hello David,
> 
> As your original patch was in the RFC state, I have just maintained
> the same here as well. But once you test this patch along with the
> new series, please do let me know if this needs to be converted to
> a normal PATCH instead. Thank you.

Yes, you can drop the RFC part. I assume you'll send another revision,
I'll do another test there, thanks!


-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-21  9:57       ` David Hildenbrand
@ 2021-01-22  3:32         ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-22  3:32 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, Michal Hocko,
	Wei Yang, teawater, Pankaj Gupta, Jonathan Cameron



On 1/21/21 3:27 PM, David Hildenbrand wrote:
> On 18.01.21 14:21, Anshuman Khandual wrote:
>>
>>
>> On 1/18/21 6:43 PM, Anshuman Khandual wrote:
>>> From: David Hildenbrand <david@redhat.com>
>>>
>>> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
>>> are more restrictions of which memory we can actually hotplug, especially
>>> om arm64 or s390x once we support them: we might receive something like
>>> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
>>> operation.
>>>
>>> So, check right when initializing the device which memory we can add,
>>> warning the user. Try only adding actually pluggable ranges: in the worst
>>> case, no memory provided by our device is pluggable.
>>>
>>> In the usual case, we expect all device memory to be pluggable, and in
>>> corner cases only some memory at the end of the device-managed memory
>>> region to not be pluggable.
>>>
>>> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>>> Cc: Jason Wang <jasowang@redhat.com>
>>> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
>>> Cc: Michal Hocko <mhocko@kernel.org>
>>> Cc: Oscar Salvador <osalvador@suse.de>
>>> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
>>> Cc: Andrew Morton <akpm@linux-foundation.org>
>>> Cc: catalin.marinas@arm.com
>>> Cc: teawater <teawaterz@linux.alibaba.com>
>>> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
>>> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
>>> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>> Cc: hca@linux.ibm.com
>>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>>> Cc: Will Deacon <will@kernel.org>
>>> Cc: Ard Biesheuvel <ardb@kernel.org>
>>> Cc: Mark Rutland <mark.rutland@arm.com>
>>> Cc: Heiko Carstens <hca@linux.ibm.com>
>>> Cc: Michal Hocko <mhocko@kernel.org>
>>> Signed-off-by: David Hildenbrand <david@redhat.com>
>>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
>>
>> Hello David,
>>
>> As your original patch was in the RFC state, I have just maintained
>> the same here as well. But once you test this patch along with the
>> new series, please do let me know if this needs to be converted to
>> a normal PATCH instead. Thank you.
> 
> Yes, you can drop the RFC part. I assume you'll send another revision,
> I'll do another test there, thanks!

Sure, will drop the RFC in next version.

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
@ 2021-01-22  3:32         ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-22  3:32 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Wei Yang, Vasily Gorbik,
	Michael S. Tsirkin, Jason Wang, Pankaj Gupta, linux-kernel,
	Michal Hocko, Pankaj Gupta, teawater, Jonathan Cameron,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador



On 1/21/21 3:27 PM, David Hildenbrand wrote:
> On 18.01.21 14:21, Anshuman Khandual wrote:
>>
>>
>> On 1/18/21 6:43 PM, Anshuman Khandual wrote:
>>> From: David Hildenbrand <david@redhat.com>
>>>
>>> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
>>> are more restrictions of which memory we can actually hotplug, especially
>>> om arm64 or s390x once we support them: we might receive something like
>>> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
>>> operation.
>>>
>>> So, check right when initializing the device which memory we can add,
>>> warning the user. Try only adding actually pluggable ranges: in the worst
>>> case, no memory provided by our device is pluggable.
>>>
>>> In the usual case, we expect all device memory to be pluggable, and in
>>> corner cases only some memory at the end of the device-managed memory
>>> region to not be pluggable.
>>>
>>> Cc: "Michael S. Tsirkin" <mst@redhat.com>
>>> Cc: Jason Wang <jasowang@redhat.com>
>>> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
>>> Cc: Michal Hocko <mhocko@kernel.org>
>>> Cc: Oscar Salvador <osalvador@suse.de>
>>> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
>>> Cc: Andrew Morton <akpm@linux-foundation.org>
>>> Cc: catalin.marinas@arm.com
>>> Cc: teawater <teawaterz@linux.alibaba.com>
>>> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
>>> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
>>> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>> Cc: hca@linux.ibm.com
>>> Cc: Vasily Gorbik <gor@linux.ibm.com>
>>> Cc: Will Deacon <will@kernel.org>
>>> Cc: Ard Biesheuvel <ardb@kernel.org>
>>> Cc: Mark Rutland <mark.rutland@arm.com>
>>> Cc: Heiko Carstens <hca@linux.ibm.com>
>>> Cc: Michal Hocko <mhocko@kernel.org>
>>> Signed-off-by: David Hildenbrand <david@redhat.com>
>>> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com>
>>
>> Hello David,
>>
>> As your original patch was in the RFC state, I have just maintained
>> the same here as well. But once you test this patch along with the
>> new series, please do let me know if this needs to be converted to
>> a normal PATCH instead. Thank you.
> 
> Yes, you can drop the RFC part. I assume you'll send another revision,
> I'll do another test there, thanks!

Sure, will drop the RFC in next version.

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
  2021-01-20  8:37       ` Anshuman Khandual
@ 2021-01-22  6:04         ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-22  6:04 UTC (permalink / raw)
  To: Oscar Salvador, David Hildenbrand
  Cc: linux-mm, akpm, hca, catalin.marinas, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel


On 1/20/21 2:07 PM, Anshuman Khandual wrote:
> 
> 
> On 1/19/21 7:10 PM, Oscar Salvador wrote:
>> On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
>>> Minor thing, we should make up our mind if we want to call stuff
>>> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.
>>
>> I would rather use the latter as well. I used that in [1].
> 
> Okay, will change all that is 'memhp' as 'mhp' instead.
> 
>> MEMHP_MERGE_RESOURCE should be renamed if we agree on that.
>>
>> [1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/
>>

While replacing 'memhp' as 'mhp' in this series, noticed there are
some more 'memhp' scattered around the code from earlier. A mix of
both 'memhp' and 'mhp' might not be a good idea. Hence should we
just change these remaining 'memhp' as 'mhp' as well and possibly
also MEMHP_MERGE_RESOURCE as suggested earlier, in a subsequent
clean up patch ? Would there be a problem with memhp_default_state
being a command line parameter ?

From a tree wide search -

Documentation/admin-guide/kernel-parameters.txt:        memhp_default_state=online/offline
drivers/base/memory.c:int memhp_online_type_from_str(const char *str)
drivers/base/memory.c:  const int online_type = memhp_online_type_from_str(buf);
drivers/base/memory.c:                    online_type_to_str[memhp_default_online_type]);
drivers/base/memory.c:  const int online_type = memhp_online_type_from_str(buf);
drivers/base/memory.c:  memhp_default_online_type = online_type;
include/linux/memory_hotplug.h:extern int memhp_online_type_from_str(const char *str);
include/linux/memory_hotplug.h:extern int memhp_default_online_type;
mm/memory_hotplug.c:int memhp_default_online_type = MMOP_OFFLINE;
mm/memory_hotplug.c:int memhp_default_online_type = MMOP_ONLINE;
mm/memory_hotplug.c:static int __init setup_memhp_default_state(char *str)
mm/memory_hotplug.c:    const int online_type = memhp_online_type_from_str(str);
mm/memory_hotplug.c:            memhp_default_online_type = online_type;
mm/memory_hotplug.c:__setup("memhp_default_state=", setup_memhp_default_state);
mm/memory_hotplug.c:    mem->online_type = memhp_default_online_type;
mm/memory_hotplug.c:    if (memhp_default_online_type != MMOP_OFFLINE)

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-22  6:04         ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-22  6:04 UTC (permalink / raw)
  To: Oscar Salvador, David Hildenbrand
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, catalin.marinas, hca,
	linux-kernel, linux-mm, akpm, Will Deacon, Ard Biesheuvel,
	linux-arm-kernel


On 1/20/21 2:07 PM, Anshuman Khandual wrote:
> 
> 
> On 1/19/21 7:10 PM, Oscar Salvador wrote:
>> On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
>>> Minor thing, we should make up our mind if we want to call stuff
>>> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.
>>
>> I would rather use the latter as well. I used that in [1].
> 
> Okay, will change all that is 'memhp' as 'mhp' instead.
> 
>> MEMHP_MERGE_RESOURCE should be renamed if we agree on that.
>>
>> [1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/
>>

While replacing 'memhp' as 'mhp' in this series, noticed there are
some more 'memhp' scattered around the code from earlier. A mix of
both 'memhp' and 'mhp' might not be a good idea. Hence should we
just change these remaining 'memhp' as 'mhp' as well and possibly
also MEMHP_MERGE_RESOURCE as suggested earlier, in a subsequent
clean up patch ? Would there be a problem with memhp_default_state
being a command line parameter ?

From a tree wide search -

Documentation/admin-guide/kernel-parameters.txt:        memhp_default_state=online/offline
drivers/base/memory.c:int memhp_online_type_from_str(const char *str)
drivers/base/memory.c:  const int online_type = memhp_online_type_from_str(buf);
drivers/base/memory.c:                    online_type_to_str[memhp_default_online_type]);
drivers/base/memory.c:  const int online_type = memhp_online_type_from_str(buf);
drivers/base/memory.c:  memhp_default_online_type = online_type;
include/linux/memory_hotplug.h:extern int memhp_online_type_from_str(const char *str);
include/linux/memory_hotplug.h:extern int memhp_default_online_type;
mm/memory_hotplug.c:int memhp_default_online_type = MMOP_OFFLINE;
mm/memory_hotplug.c:int memhp_default_online_type = MMOP_ONLINE;
mm/memory_hotplug.c:static int __init setup_memhp_default_state(char *str)
mm/memory_hotplug.c:    const int online_type = memhp_online_type_from_str(str);
mm/memory_hotplug.c:            memhp_default_online_type = online_type;
mm/memory_hotplug.c:__setup("memhp_default_state=", setup_memhp_default_state);
mm/memory_hotplug.c:    mem->online_type = memhp_default_online_type;
mm/memory_hotplug.c:    if (memhp_default_online_type != MMOP_OFFLINE)

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
  2021-01-22  6:04         ` Anshuman Khandual
@ 2021-01-22  8:34           ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22  8:34 UTC (permalink / raw)
  To: Anshuman Khandual, Oscar Salvador
  Cc: linux-mm, akpm, hca, catalin.marinas, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland, linux-arm-kernel, linux-s390,
	linux-kernel

On 22.01.21 07:04, Anshuman Khandual wrote:
> 
> On 1/20/21 2:07 PM, Anshuman Khandual wrote:
>>
>>
>> On 1/19/21 7:10 PM, Oscar Salvador wrote:
>>> On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
>>>> Minor thing, we should make up our mind if we want to call stuff
>>>> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.
>>>
>>> I would rather use the latter as well. I used that in [1].
>>
>> Okay, will change all that is 'memhp' as 'mhp' instead.
>>
>>> MEMHP_MERGE_RESOURCE should be renamed if we agree on that.
>>>
>>> [1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/
>>>
> 
> While replacing 'memhp' as 'mhp' in this series, noticed there are
> some more 'memhp' scattered around the code from earlier. A mix of
> both 'memhp' and 'mhp' might not be a good idea. Hence should we
> just change these remaining 'memhp' as 'mhp' as well and possibly
> also MEMHP_MERGE_RESOURCE as suggested earlier, in a subsequent

As mentioned in another thread to Oscar, I already have a cleanup patch
for that one lying around, part of a bigger series. Might just send that
one out separately earlier.

> clean up patch ? Would there be a problem with memhp_default_state
> being a command line parameter ?

Yes, that one we should not change, to not break existing cmdlines
without good reason. We could change the
memhp_default_online_type/memhp_online_type_from_str/... thingies, though.

Feel free to send a patch, thanks!

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform
@ 2021-01-22  8:34           ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22  8:34 UTC (permalink / raw)
  To: Anshuman Khandual, Oscar Salvador
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, catalin.marinas, hca,
	linux-kernel, linux-mm, akpm, Will Deacon, Ard Biesheuvel,
	linux-arm-kernel

On 22.01.21 07:04, Anshuman Khandual wrote:
> 
> On 1/20/21 2:07 PM, Anshuman Khandual wrote:
>>
>>
>> On 1/19/21 7:10 PM, Oscar Salvador wrote:
>>> On Tue, Jan 19, 2021 at 02:33:03PM +0100, David Hildenbrand wrote:
>>>> Minor thing, we should make up our mind if we want to call stuff
>>>> internally "memhp_" or "mhp". I prefer the latter, because it is shorter.
>>>
>>> I would rather use the latter as well. I used that in [1].
>>
>> Okay, will change all that is 'memhp' as 'mhp' instead.
>>
>>> MEMHP_MERGE_RESOURCE should be renamed if we agree on that.
>>>
>>> [1] https://patchwork.kernel.org/project/linux-mm/cover/20201217130758.11565-1-osalvador@suse.de/
>>>
> 
> While replacing 'memhp' as 'mhp' in this series, noticed there are
> some more 'memhp' scattered around the code from earlier. A mix of
> both 'memhp' and 'mhp' might not be a good idea. Hence should we
> just change these remaining 'memhp' as 'mhp' as well and possibly
> also MEMHP_MERGE_RESOURCE as suggested earlier, in a subsequent

As mentioned in another thread to Oscar, I already have a cleanup patch
for that one lying around, part of a bigger series. Might just send that
one out separately earlier.

> clean up patch ? Would there be a problem with memhp_default_state
> being a command line parameter ?

Yes, that one we should not change, to not break existing cmdlines
without good reason. We could change the
memhp_default_online_type/memhp_online_type_from_str/... thingies, though.

Feel free to send a patch, thanks!

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-18 13:12   ` Anshuman Khandual
@ 2021-01-22  9:18     ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22  9:18 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel


> +/*
> + * Platforms should define arch_get_mappable_range() that provides
> + * maximum possible addressable physical memory range for which the
> + * linear mapping could be created. The platform returned address
> + * range must adhere to these following semantics.
> + *
> + * - range.start <= range.end
> + * - Range includes both end points [range.start..range.end]
> + *
> + * There is also a fallback definition provided here, allowing the
> + * entire possible physical address range in case any platform does
> + * not define arch_get_mappable_range().
> + */
> +struct range __weak arch_get_mappable_range(void)
> +{
> +	struct range memhp_range = {
> +		.start = 0UL,
> +		.end = -1ULL,
> +	};
> +	return memhp_range;
> +}
> +
> +struct range memhp_get_pluggable_range(bool need_mapping)
> +{
> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;

Sorry, thought about that line a bit more, and I think this is just
wrong (took me longer to realize as it should). The old code used this
calculation to print the limit only (in a wrong way), let's recap:

Assume MAX_PHYSMEM_BITS=32

	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;

Ehm, these are 33 bit.

OTOH, old code checked for

	if (max_addr >> MAX_PHYSMEM_BITS) {

Which makes sense, because

	0x1ffffffffull >> 32 = 1

results in "true", meaning it's to big, while

	0xffffffffull >> 32 = 0

correctly results in "false", meaning the address is fine.



So, this should just be

const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;

(similarly as calculated in virito-mem code, or in kernel/resource.c)


> +	struct range memhp_range;
> +
> +	if (need_mapping) {
> +		memhp_range = arch_get_mappable_range();
> +		if (memhp_range.start > max_phys) {
> +			memhp_range.start = 0;
> +			memhp_range.end = 0;
> +		}
> +		memhp_range.end = min_t(u64, memhp_range.end, max_phys);
> +	} else {
> +		memhp_range.start = 0;
> +		memhp_range.end = max_phys;
> +	}
> +	return memhp_range;
> +}
> +EXPORT_SYMBOL_GPL(memhp_get_pluggable_range);


-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-22  9:18     ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22  9:18 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador


> +/*
> + * Platforms should define arch_get_mappable_range() that provides
> + * maximum possible addressable physical memory range for which the
> + * linear mapping could be created. The platform returned address
> + * range must adhere to these following semantics.
> + *
> + * - range.start <= range.end
> + * - Range includes both end points [range.start..range.end]
> + *
> + * There is also a fallback definition provided here, allowing the
> + * entire possible physical address range in case any platform does
> + * not define arch_get_mappable_range().
> + */
> +struct range __weak arch_get_mappable_range(void)
> +{
> +	struct range memhp_range = {
> +		.start = 0UL,
> +		.end = -1ULL,
> +	};
> +	return memhp_range;
> +}
> +
> +struct range memhp_get_pluggable_range(bool need_mapping)
> +{
> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;

Sorry, thought about that line a bit more, and I think this is just
wrong (took me longer to realize as it should). The old code used this
calculation to print the limit only (in a wrong way), let's recap:

Assume MAX_PHYSMEM_BITS=32

	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;

Ehm, these are 33 bit.

OTOH, old code checked for

	if (max_addr >> MAX_PHYSMEM_BITS) {

Which makes sense, because

	0x1ffffffffull >> 32 = 1

results in "true", meaning it's to big, while

	0xffffffffull >> 32 = 0

correctly results in "false", meaning the address is fine.



So, this should just be

const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;

(similarly as calculated in virito-mem code, or in kernel/resource.c)


> +	struct range memhp_range;
> +
> +	if (need_mapping) {
> +		memhp_range = arch_get_mappable_range();
> +		if (memhp_range.start > max_phys) {
> +			memhp_range.start = 0;
> +			memhp_range.end = 0;
> +		}
> +		memhp_range.end = min_t(u64, memhp_range.end, max_phys);
> +	} else {
> +		memhp_range.start = 0;
> +		memhp_range.end = max_phys;
> +	}
> +	return memhp_range;
> +}
> +EXPORT_SYMBOL_GPL(memhp_get_pluggable_range);


-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-22  9:18     ` David Hildenbrand
@ 2021-01-22 10:41       ` Anshuman Khandual
  -1 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-22 10:41 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel


On 1/22/21 2:48 PM, David Hildenbrand wrote:
> 
>> +/*
>> + * Platforms should define arch_get_mappable_range() that provides
>> + * maximum possible addressable physical memory range for which the
>> + * linear mapping could be created. The platform returned address
>> + * range must adhere to these following semantics.
>> + *
>> + * - range.start <= range.end
>> + * - Range includes both end points [range.start..range.end]
>> + *
>> + * There is also a fallback definition provided here, allowing the
>> + * entire possible physical address range in case any platform does
>> + * not define arch_get_mappable_range().
>> + */
>> +struct range __weak arch_get_mappable_range(void)
>> +{
>> +	struct range memhp_range = {
>> +		.start = 0UL,
>> +		.end = -1ULL,
>> +	};
>> +	return memhp_range;
>> +}
>> +
>> +struct range memhp_get_pluggable_range(bool need_mapping)
>> +{
>> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
> 
> Sorry, thought about that line a bit more, and I think this is just
> wrong (took me longer to realize as it should). The old code used this
> calculation to print the limit only (in a wrong way), let's recap:
> 
> Assume MAX_PHYSMEM_BITS=32
> 
> 	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;
> 
> Ehm, these are 33 bit.
> 
> OTOH, old code checked for
> 
> 	if (max_addr >> MAX_PHYSMEM_BITS) {
> 
> Which makes sense, because
> 
> 	0x1ffffffffull >> 32 = 1
> 
> results in "true", meaning it's to big, while
> 
> 	0xffffffffull >> 32 = 0
> 
> correctly results in "false", meaning the address is fine.
> 
> 
> 
> So, this should just be
> 
> const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;
> 
> (similarly as calculated in virito-mem code, or in kernel/resource.c)

Should this be 1ULL << MAX_PHYSMEM_BITS - 1 instead ? Currently there are
three usage for this variable in the function.

- if (mhp_range.start > max_phys)
- mhp_range.end = min_t(u64, mhp_range.end, max_phys)
- mhp_range.end = max_phys

mhp_range.end being always inclusive on the higher end and could be maximum
(1ULL << MAX_PHYSMEM_BITS - 1) which is 0xFFFFFFFF instead of 0x100000000
when (1ULL << MAX_PHYSMEM_BITS) is followed for a 32 bit system. This seems
consistent with the default fallback (range.end = -1ULL) defined above.

> 
> 
>> +	struct range memhp_range;
>> +
>> +	if (need_mapping) {
>> +		memhp_range = arch_get_mappable_range();
>> +		if (memhp_range.start > max_phys) {
>> +			memhp_range.start = 0;
>> +			memhp_range.end = 0;
>> +		}
>> +		memhp_range.end = min_t(u64, memhp_range.end, max_phys);
>> +	} else {
>> +		memhp_range.start = 0;
>> +		memhp_range.end = max_phys;
>> +	}
>> +	return memhp_range;
>> +}
>> +EXPORT_SYMBOL_GPL(memhp_get_pluggable_range);
> 
> 

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-22 10:41       ` Anshuman Khandual
  0 siblings, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-22 10:41 UTC (permalink / raw)
  To: David Hildenbrand, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador


On 1/22/21 2:48 PM, David Hildenbrand wrote:
> 
>> +/*
>> + * Platforms should define arch_get_mappable_range() that provides
>> + * maximum possible addressable physical memory range for which the
>> + * linear mapping could be created. The platform returned address
>> + * range must adhere to these following semantics.
>> + *
>> + * - range.start <= range.end
>> + * - Range includes both end points [range.start..range.end]
>> + *
>> + * There is also a fallback definition provided here, allowing the
>> + * entire possible physical address range in case any platform does
>> + * not define arch_get_mappable_range().
>> + */
>> +struct range __weak arch_get_mappable_range(void)
>> +{
>> +	struct range memhp_range = {
>> +		.start = 0UL,
>> +		.end = -1ULL,
>> +	};
>> +	return memhp_range;
>> +}
>> +
>> +struct range memhp_get_pluggable_range(bool need_mapping)
>> +{
>> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
> 
> Sorry, thought about that line a bit more, and I think this is just
> wrong (took me longer to realize as it should). The old code used this
> calculation to print the limit only (in a wrong way), let's recap:
> 
> Assume MAX_PHYSMEM_BITS=32
> 
> 	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;
> 
> Ehm, these are 33 bit.
> 
> OTOH, old code checked for
> 
> 	if (max_addr >> MAX_PHYSMEM_BITS) {
> 
> Which makes sense, because
> 
> 	0x1ffffffffull >> 32 = 1
> 
> results in "true", meaning it's to big, while
> 
> 	0xffffffffull >> 32 = 0
> 
> correctly results in "false", meaning the address is fine.
> 
> 
> 
> So, this should just be
> 
> const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;
> 
> (similarly as calculated in virito-mem code, or in kernel/resource.c)

Should this be 1ULL << MAX_PHYSMEM_BITS - 1 instead ? Currently there are
three usage for this variable in the function.

- if (mhp_range.start > max_phys)
- mhp_range.end = min_t(u64, mhp_range.end, max_phys)
- mhp_range.end = max_phys

mhp_range.end being always inclusive on the higher end and could be maximum
(1ULL << MAX_PHYSMEM_BITS - 1) which is 0xFFFFFFFF instead of 0x100000000
when (1ULL << MAX_PHYSMEM_BITS) is followed for a 32 bit system. This seems
consistent with the default fallback (range.end = -1ULL) defined above.

> 
> 
>> +	struct range memhp_range;
>> +
>> +	if (need_mapping) {
>> +		memhp_range = arch_get_mappable_range();
>> +		if (memhp_range.start > max_phys) {
>> +			memhp_range.start = 0;
>> +			memhp_range.end = 0;
>> +		}
>> +		memhp_range.end = min_t(u64, memhp_range.end, max_phys);
>> +	} else {
>> +		memhp_range.start = 0;
>> +		memhp_range.end = max_phys;
>> +	}
>> +	return memhp_range;
>> +}
>> +EXPORT_SYMBOL_GPL(memhp_get_pluggable_range);
> 
> 

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-22 10:41       ` Anshuman Khandual
@ 2021-01-22 10:42         ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22 10:42 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 22.01.21 11:41, Anshuman Khandual wrote:
> 
> On 1/22/21 2:48 PM, David Hildenbrand wrote:
>>
>>> +/*
>>> + * Platforms should define arch_get_mappable_range() that provides
>>> + * maximum possible addressable physical memory range for which the
>>> + * linear mapping could be created. The platform returned address
>>> + * range must adhere to these following semantics.
>>> + *
>>> + * - range.start <= range.end
>>> + * - Range includes both end points [range.start..range.end]
>>> + *
>>> + * There is also a fallback definition provided here, allowing the
>>> + * entire possible physical address range in case any platform does
>>> + * not define arch_get_mappable_range().
>>> + */
>>> +struct range __weak arch_get_mappable_range(void)
>>> +{
>>> +	struct range memhp_range = {
>>> +		.start = 0UL,
>>> +		.end = -1ULL,
>>> +	};
>>> +	return memhp_range;
>>> +}
>>> +
>>> +struct range memhp_get_pluggable_range(bool need_mapping)
>>> +{
>>> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
>>
>> Sorry, thought about that line a bit more, and I think this is just
>> wrong (took me longer to realize as it should). The old code used this
>> calculation to print the limit only (in a wrong way), let's recap:
>>
>> Assume MAX_PHYSMEM_BITS=32
>>
>> 	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;
>>
>> Ehm, these are 33 bit.
>>
>> OTOH, old code checked for
>>
>> 	if (max_addr >> MAX_PHYSMEM_BITS) {
>>
>> Which makes sense, because
>>
>> 	0x1ffffffffull >> 32 = 1
>>
>> results in "true", meaning it's to big, while
>>
>> 	0xffffffffull >> 32 = 0
>>
>> correctly results in "false", meaning the address is fine.
>>
>>
>>
>> So, this should just be
>>
>> const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;
>>
>> (similarly as calculated in virito-mem code, or in kernel/resource.c)
> 
> Should this be 1ULL << MAX_PHYSMEM_BITS - 1 instead ? Currently there are

Yes, obviously, sorry, forgot the -1.

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-22 10:42         ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22 10:42 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 22.01.21 11:41, Anshuman Khandual wrote:
> 
> On 1/22/21 2:48 PM, David Hildenbrand wrote:
>>
>>> +/*
>>> + * Platforms should define arch_get_mappable_range() that provides
>>> + * maximum possible addressable physical memory range for which the
>>> + * linear mapping could be created. The platform returned address
>>> + * range must adhere to these following semantics.
>>> + *
>>> + * - range.start <= range.end
>>> + * - Range includes both end points [range.start..range.end]
>>> + *
>>> + * There is also a fallback definition provided here, allowing the
>>> + * entire possible physical address range in case any platform does
>>> + * not define arch_get_mappable_range().
>>> + */
>>> +struct range __weak arch_get_mappable_range(void)
>>> +{
>>> +	struct range memhp_range = {
>>> +		.start = 0UL,
>>> +		.end = -1ULL,
>>> +	};
>>> +	return memhp_range;
>>> +}
>>> +
>>> +struct range memhp_get_pluggable_range(bool need_mapping)
>>> +{
>>> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
>>
>> Sorry, thought about that line a bit more, and I think this is just
>> wrong (took me longer to realize as it should). The old code used this
>> calculation to print the limit only (in a wrong way), let's recap:
>>
>> Assume MAX_PHYSMEM_BITS=32
>>
>> 	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;
>>
>> Ehm, these are 33 bit.
>>
>> OTOH, old code checked for
>>
>> 	if (max_addr >> MAX_PHYSMEM_BITS) {
>>
>> Which makes sense, because
>>
>> 	0x1ffffffffull >> 32 = 1
>>
>> results in "true", meaning it's to big, while
>>
>> 	0xffffffffull >> 32 = 0
>>
>> correctly results in "false", meaning the address is fine.
>>
>>
>>
>> So, this should just be
>>
>> const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;
>>
>> (similarly as calculated in virito-mem code, or in kernel/resource.c)
> 
> Should this be 1ULL << MAX_PHYSMEM_BITS - 1 instead ? Currently there are

Yes, obviously, sorry, forgot the -1.

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
  2021-01-22 10:42         ` David Hildenbrand
@ 2021-01-22 10:43           ` David Hildenbrand
  -1 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22 10:43 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Oscar Salvador, Vasily Gorbik, Will Deacon, Ard Biesheuvel,
	Mark Rutland, linux-arm-kernel, linux-s390, linux-kernel

On 22.01.21 11:42, David Hildenbrand wrote:
> On 22.01.21 11:41, Anshuman Khandual wrote:
>>
>> On 1/22/21 2:48 PM, David Hildenbrand wrote:
>>>
>>>> +/*
>>>> + * Platforms should define arch_get_mappable_range() that provides
>>>> + * maximum possible addressable physical memory range for which the
>>>> + * linear mapping could be created. The platform returned address
>>>> + * range must adhere to these following semantics.
>>>> + *
>>>> + * - range.start <= range.end
>>>> + * - Range includes both end points [range.start..range.end]
>>>> + *
>>>> + * There is also a fallback definition provided here, allowing the
>>>> + * entire possible physical address range in case any platform does
>>>> + * not define arch_get_mappable_range().
>>>> + */
>>>> +struct range __weak arch_get_mappable_range(void)
>>>> +{
>>>> +	struct range memhp_range = {
>>>> +		.start = 0UL,
>>>> +		.end = -1ULL,
>>>> +	};
>>>> +	return memhp_range;
>>>> +}
>>>> +
>>>> +struct range memhp_get_pluggable_range(bool need_mapping)
>>>> +{
>>>> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
>>>
>>> Sorry, thought about that line a bit more, and I think this is just
>>> wrong (took me longer to realize as it should). The old code used this
>>> calculation to print the limit only (in a wrong way), let's recap:
>>>
>>> Assume MAX_PHYSMEM_BITS=32
>>>
>>> 	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;
>>>
>>> Ehm, these are 33 bit.
>>>
>>> OTOH, old code checked for
>>>
>>> 	if (max_addr >> MAX_PHYSMEM_BITS) {
>>>
>>> Which makes sense, because
>>>
>>> 	0x1ffffffffull >> 32 = 1
>>>
>>> results in "true", meaning it's to big, while
>>>
>>> 	0xffffffffull >> 32 = 0
>>>
>>> correctly results in "false", meaning the address is fine.
>>>
>>>
>>>
>>> So, this should just be
>>>
>>> const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;
>>>
>>> (similarly as calculated in virito-mem code, or in kernel/resource.c)
>>
>> Should this be 1ULL << MAX_PHYSMEM_BITS - 1 instead ? Currently there are
> 
> Yes, obviously, sorry, forgot the -1.
> 

const u64 max_phys = (1ULL << MAX_PHYSMEM_BITS) - 1;

to be precise.

-- 
Thanks,

David / dhildenb


^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added with platform
@ 2021-01-22 10:43           ` David Hildenbrand
  0 siblings, 0 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-22 10:43 UTC (permalink / raw)
  To: Anshuman Khandual, linux-mm, akpm, hca, catalin.marinas
  Cc: Mark Rutland, linux-s390, Vasily Gorbik, linux-kernel,
	Will Deacon, Ard Biesheuvel, linux-arm-kernel, Oscar Salvador

On 22.01.21 11:42, David Hildenbrand wrote:
> On 22.01.21 11:41, Anshuman Khandual wrote:
>>
>> On 1/22/21 2:48 PM, David Hildenbrand wrote:
>>>
>>>> +/*
>>>> + * Platforms should define arch_get_mappable_range() that provides
>>>> + * maximum possible addressable physical memory range for which the
>>>> + * linear mapping could be created. The platform returned address
>>>> + * range must adhere to these following semantics.
>>>> + *
>>>> + * - range.start <= range.end
>>>> + * - Range includes both end points [range.start..range.end]
>>>> + *
>>>> + * There is also a fallback definition provided here, allowing the
>>>> + * entire possible physical address range in case any platform does
>>>> + * not define arch_get_mappable_range().
>>>> + */
>>>> +struct range __weak arch_get_mappable_range(void)
>>>> +{
>>>> +	struct range memhp_range = {
>>>> +		.start = 0UL,
>>>> +		.end = -1ULL,
>>>> +	};
>>>> +	return memhp_range;
>>>> +}
>>>> +
>>>> +struct range memhp_get_pluggable_range(bool need_mapping)
>>>> +{
>>>> +	const u64 max_phys = (1ULL << (MAX_PHYSMEM_BITS + 1)) - 1;
>>>
>>> Sorry, thought about that line a bit more, and I think this is just
>>> wrong (took me longer to realize as it should). The old code used this
>>> calculation to print the limit only (in a wrong way), let's recap:
>>>
>>> Assume MAX_PHYSMEM_BITS=32
>>>
>>> 	max_phys = (1ULL << (32 + 1)) - 1 = 0x1ffffffffull;
>>>
>>> Ehm, these are 33 bit.
>>>
>>> OTOH, old code checked for
>>>
>>> 	if (max_addr >> MAX_PHYSMEM_BITS) {
>>>
>>> Which makes sense, because
>>>
>>> 	0x1ffffffffull >> 32 = 1
>>>
>>> results in "true", meaning it's to big, while
>>>
>>> 	0xffffffffull >> 32 = 0
>>>
>>> correctly results in "false", meaning the address is fine.
>>>
>>>
>>>
>>> So, this should just be
>>>
>>> const u64 max_phys = 1ULL << MAX_PHYSMEM_BITS;
>>>
>>> (similarly as calculated in virito-mem code, or in kernel/resource.c)
>>
>> Should this be 1ULL << MAX_PHYSMEM_BITS - 1 instead ? Currently there are
> 
> Yes, obviously, sorry, forgot the -1.
> 

const u64 max_phys = (1ULL << MAX_PHYSMEM_BITS) - 1;

to be precise.

-- 
Thanks,

David / dhildenb


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-11 12:41 ` [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug David Hildenbrand
  2021-01-11 19:49   ` kernel test robot
@ 2021-01-12  4:25   ` Anshuman Khandual
  1 sibling, 0 replies; 57+ messages in thread
From: Anshuman Khandual @ 2021-01-12  4:25 UTC (permalink / raw)
  To: David Hildenbrand, linux-kernel
  Cc: linux-mm, Michael S. Tsirkin, Jason Wang, Pankaj Gupta,
	Michal Hocko, Oscar Salvador, Wei Yang, Andrew Morton,
	catalin.marinas, teawater, Pankaj Gupta, Jonathan Cameron, hca,
	Vasily Gorbik, Will Deacon, Ard Biesheuvel, Mark Rutland



On 1/11/21 6:11 PM, David Hildenbrand wrote:
> Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
> are more restrictions of which memory we can actually hotplug, especially
> om arm64 or s390x once we support them: we might receive something like
> -E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
> operation.
> 
> So, check right when initializing the device which memory we can add,
> warning the user. Try only adding actually pluggable ranges: in the worst
> case, no memory provided by our device is pluggable.
> 
> In the usual case, we expect all device memory to be pluggable, and in
> corner cases only some memory at the end of the device-managed memory
> region to not be pluggable.
> 
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Jason Wang <jasowang@redhat.com>
> Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
> Cc: Michal Hocko <mhocko@kernel.org>
> Cc: Oscar Salvador <osalvador@suse.de>
> Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: catalin.marinas@arm.com
> Cc: teawater <teawaterz@linux.alibaba.com>
> Cc: Anshuman Khandual <anshuman.khandual@arm.com>
> Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
> Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Cc: hca@linux.ibm.com
> Cc: Vasily Gorbik <gor@linux.ibm.com>
> Cc: Will Deacon <will@kernel.org>
> Cc: Ard Biesheuvel <ardb@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: Heiko Carstens <hca@linux.ibm.com>
> Cc: Michal Hocko <mhocko@kernel.org>
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---
> 
> This is an example how virito-mem intends to use an interface like
> memhp_get_pluggable_range() once around. See:
> 
> "[PATCH V2 0/3] mm/hotplug: Pre-validate the address range with platform"
> https://lkml.kernel.org/r/1608218912-28932-1-git-send-email-anshuman.khandual@arm.com
> 
> @Anshuman, feel free to pick up and carry this patch. I'll retest the final
> result / new versions of you series.

Makes sense, will carry this patch in the series.

^ permalink raw reply	[flat|nested] 57+ messages in thread

* Re: [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2021-01-11 12:41 ` [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug David Hildenbrand
@ 2021-01-11 19:49   ` kernel test robot
  2021-01-12  4:25   ` Anshuman Khandual
  1 sibling, 0 replies; 57+ messages in thread
From: kernel test robot @ 2021-01-11 19:49 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 4655 bytes --]

Hi David,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on linus/master]
[also build test ERROR on v5.11-rc3 next-20210111]
[cannot apply to linux/master 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/David-Hildenbrand/virtio-mem-check-against-memhp_get_pluggable_range-which-memory-we-can-hotplug/20210111-205003
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 7c53f6b671f4aba70ff15e1b05148b10d58c2837
config: x86_64-rhel (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/a53d8904d125a1020b44f1bf11961f781da124f3
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review David-Hildenbrand/virtio-mem-check-against-memhp_get_pluggable_range-which-memory-we-can-hotplug/20210111-205003
        git checkout a53d8904d125a1020b44f1bf11961f781da124f3
        # save the attached .config to linux build tree
        make W=1 ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/virtio/virtio_mem.c: In function 'virtio_mem_refresh_config':
>> drivers/virtio/virtio_mem.c:2225:39: error: implicit declaration of function 'memhp_get_pluggable_range' [-Werror=implicit-function-declaration]
    2225 |  const struct range pluggable_range = memhp_get_pluggable_range(true);
         |                                       ^~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/virtio/virtio_mem.c:2225:39: error: invalid initializer
   drivers/virtio/virtio_mem.c: In function 'virtio_mem_init':
   drivers/virtio/virtio_mem.c:2377:39: error: invalid initializer
    2377 |  const struct range pluggable_range = memhp_get_pluggable_range(true);
         |                                       ^~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/virtio/virtio_mem.c:2378:17: warning: unused variable 'phys_limit' [-Wunused-variable]
    2378 |  const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
         |                 ^~~~~~~~~~
   cc1: some warnings being treated as errors


vim +/memhp_get_pluggable_range +2225 drivers/virtio/virtio_mem.c

  2219	
  2220	/*
  2221	 * Update all parts of the config that could have changed.
  2222	 */
  2223	static void virtio_mem_refresh_config(struct virtio_mem *vm)
  2224	{
> 2225		const struct range pluggable_range = memhp_get_pluggable_range(true);
  2226		uint64_t new_plugged_size, usable_region_size, end_addr;
  2227	
  2228		/* the plugged_size is just a reflection of what _we_ did previously */
  2229		virtio_cread_le(vm->vdev, struct virtio_mem_config, plugged_size,
  2230				&new_plugged_size);
  2231		if (WARN_ON_ONCE(new_plugged_size != vm->plugged_size))
  2232			vm->plugged_size = new_plugged_size;
  2233	
  2234		/* calculate the last usable memory block id */
  2235		virtio_cread_le(vm->vdev, struct virtio_mem_config,
  2236				usable_region_size, &usable_region_size);
  2237		end_addr = min(vm->addr + usable_region_size - 1,
  2238			       pluggable_range.end);
  2239	
  2240		if (vm->in_sbm) {
  2241			vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr);
  2242			if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes()))
  2243				vm->sbm.last_usable_mb_id--;
  2244		} else {
  2245			vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm,
  2246									     end_addr);
  2247			if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size))
  2248				vm->bbm.last_usable_bb_id--;
  2249		}
  2250		/*
  2251		 * If we cannot plug any of our device memory (e.g., nothing in the
  2252		 * usable region is addressable), the last usable memory block id will
  2253		 * be smaller than the first usable memory block id. We'll stop
  2254		 * attempting to add memory with -ENOSPC from our main loop.
  2255		 */
  2256	
  2257		/* see if there is a request to change the size */
  2258		virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
  2259				&vm->requested_size);
  2260	
  2261		dev_info(&vm->vdev->dev, "plugged size: 0x%llx", vm->plugged_size);
  2262		dev_info(&vm->vdev->dev, "requested size: 0x%llx", vm->requested_size);
  2263	}
  2264	

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

[-- Attachment #2: config.gz --]
[-- Type: application/gzip, Size: 45916 bytes --]

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug
  2020-12-17 15:28 [PATCH V2 0/3] mm/hotplug: " Anshuman Khandual
@ 2021-01-11 12:41 ` David Hildenbrand
  2021-01-11 19:49   ` kernel test robot
  2021-01-12  4:25   ` Anshuman Khandual
  0 siblings, 2 replies; 57+ messages in thread
From: David Hildenbrand @ 2021-01-11 12:41 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-mm, David Hildenbrand, Michael S. Tsirkin, Jason Wang,
	Pankaj Gupta, Michal Hocko, Oscar Salvador, Wei Yang,
	Andrew Morton, catalin.marinas, teawater, Anshuman Khandual,
	Pankaj Gupta, Jonathan Cameron, hca, Vasily Gorbik, Will Deacon,
	Ard Biesheuvel, Mark Rutland

Right now, we only check against MAX_PHYSMEM_BITS - but turns out there
are more restrictions of which memory we can actually hotplug, especially
om arm64 or s390x once we support them: we might receive something like
-E2BIG or -ERANGE from add_memory_driver_managed(), stopping device
operation.

So, check right when initializing the device which memory we can add,
warning the user. Try only adding actually pluggable ranges: in the worst
case, no memory provided by our device is pluggable.

In the usual case, we expect all device memory to be pluggable, and in
corner cases only some memory at the end of the device-managed memory
region to not be pluggable.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Wei Yang <richard.weiyang@linux.alibaba.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: catalin.marinas@arm.com
Cc: teawater <teawaterz@linux.alibaba.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: Pankaj Gupta <pankaj.gupta@cloud.ionos.com>
Cc: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Cc: hca@linux.ibm.com
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Michal Hocko <mhocko@kernel.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
---

This is an example how virito-mem intends to use an interface like
memhp_get_pluggable_range() once around. See:

"[PATCH V2 0/3] mm/hotplug: Pre-validate the address range with platform"
https://lkml.kernel.org/r/1608218912-28932-1-git-send-email-anshuman.khandual@arm.com

@Anshuman, feel free to pick up and carry this patch. I'll retest the final
result / new versions of you series.

---
 drivers/virtio/virtio_mem.c | 40 +++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 13 deletions(-)

diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 9fc9ec4a25f5..1fe40b2d7b6d 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -2222,7 +2222,7 @@ static int virtio_mem_unplug_pending_mb(struct virtio_mem *vm)
  */
 static void virtio_mem_refresh_config(struct virtio_mem *vm)
 {
-	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
+	const struct range pluggable_range = memhp_get_pluggable_range(true);
 	uint64_t new_plugged_size, usable_region_size, end_addr;
 
 	/* the plugged_size is just a reflection of what _we_ did previously */
@@ -2234,15 +2234,25 @@ static void virtio_mem_refresh_config(struct virtio_mem *vm)
 	/* calculate the last usable memory block id */
 	virtio_cread_le(vm->vdev, struct virtio_mem_config,
 			usable_region_size, &usable_region_size);
-	end_addr = vm->addr + usable_region_size;
-	end_addr = min(end_addr, phys_limit);
+	end_addr = min(vm->addr + usable_region_size - 1,
+		       pluggable_range.end);
 
-	if (vm->in_sbm)
-		vm->sbm.last_usable_mb_id =
-					 virtio_mem_phys_to_mb_id(end_addr) - 1;
-	else
-		vm->bbm.last_usable_bb_id =
-				     virtio_mem_phys_to_bb_id(vm, end_addr) - 1;
+	if (vm->in_sbm) {
+		vm->sbm.last_usable_mb_id = virtio_mem_phys_to_mb_id(end_addr);
+		if (!IS_ALIGNED(end_addr + 1, memory_block_size_bytes()))
+			vm->sbm.last_usable_mb_id--;
+	} else {
+		vm->bbm.last_usable_bb_id = virtio_mem_phys_to_bb_id(vm,
+								     end_addr);
+		if (!IS_ALIGNED(end_addr + 1, vm->bbm.bb_size))
+			vm->bbm.last_usable_bb_id--;
+	}
+	/*
+	 * If we cannot plug any of our device memory (e.g., nothing in the
+	 * usable region is addressable), the last usable memory block id will
+	 * be smaller than the first usable memory block id. We'll stop
+	 * attempting to add memory with -ENOSPC from our main loop.
+	 */
 
 	/* see if there is a request to change the size */
 	virtio_cread_le(vm->vdev, struct virtio_mem_config, requested_size,
@@ -2364,6 +2374,7 @@ static int virtio_mem_init_vq(struct virtio_mem *vm)
 
 static int virtio_mem_init(struct virtio_mem *vm)
 {
+	const struct range pluggable_range = memhp_get_pluggable_range(true);
 	const uint64_t phys_limit = 1UL << MAX_PHYSMEM_BITS;
 	uint64_t sb_size, addr;
 	uint16_t node_id;
@@ -2405,9 +2416,10 @@ static int virtio_mem_init(struct virtio_mem *vm)
 	if (!IS_ALIGNED(vm->addr + vm->region_size, memory_block_size_bytes()))
 		dev_warn(&vm->vdev->dev,
 			 "The alignment of the physical end address can make some memory unusable.\n");
-	if (vm->addr + vm->region_size > phys_limit)
+	if (vm->addr < pluggable_range.start ||
+	    vm->addr + vm->region_size - 1 > pluggable_range.end)
 		dev_warn(&vm->vdev->dev,
-			 "Some memory is not addressable. This can make some memory unusable.\n");
+			 "Some device memory is not addressable/pluggable. This can make some memory unusable.\n");
 
 	/*
 	 * We want subblocks to span at least MAX_ORDER_NR_PAGES and
@@ -2429,7 +2441,8 @@ static int virtio_mem_init(struct virtio_mem *vm)
 				     vm->sbm.sb_size;
 
 		/* Round up to the next full memory block */
-		addr = vm->addr + memory_block_size_bytes() - 1;
+		addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
+		       memory_block_size_bytes() - 1;
 		vm->sbm.first_mb_id = virtio_mem_phys_to_mb_id(addr);
 		vm->sbm.next_mb_id = vm->sbm.first_mb_id;
 	} else {
@@ -2450,7 +2463,8 @@ static int virtio_mem_init(struct virtio_mem *vm)
 		}
 
 		/* Round up to the next aligned big block */
-		addr = vm->addr + vm->bbm.bb_size - 1;
+		addr = max_t(uint64_t, vm->addr, pluggable_range.start) +
+		       vm->bbm.bb_size - 1;
 		vm->bbm.first_bb_id = virtio_mem_phys_to_bb_id(vm, addr);
 		vm->bbm.next_bb_id = vm->bbm.first_bb_id;
 	}
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 57+ messages in thread

end of thread, other threads:[~2021-01-22 10:48 UTC | newest]

Thread overview: 57+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-18 13:12 [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform Anshuman Khandual
2021-01-18 13:12 ` Anshuman Khandual
2021-01-18 13:12 ` [PATCH V3 1/3] mm/memory_hotplug: Prevalidate the address range being added " Anshuman Khandual
2021-01-18 13:12   ` Anshuman Khandual
2021-01-19 12:21   ` David Hildenbrand
2021-01-19 12:21     ` David Hildenbrand
2021-01-20  8:33     ` Anshuman Khandual
2021-01-20  8:33       ` Anshuman Khandual
2021-01-20 10:41       ` David Hildenbrand
2021-01-20 10:41         ` David Hildenbrand
2021-01-20 11:58         ` Oscar Salvador
2021-01-20 11:58           ` Oscar Salvador
2021-01-21  9:23       ` Oscar Salvador
2021-01-21  9:23         ` Oscar Salvador
2021-01-22  9:18   ` David Hildenbrand
2021-01-22  9:18     ` David Hildenbrand
2021-01-22 10:41     ` Anshuman Khandual
2021-01-22 10:41       ` Anshuman Khandual
2021-01-22 10:42       ` David Hildenbrand
2021-01-22 10:42         ` David Hildenbrand
2021-01-22 10:43         ` David Hildenbrand
2021-01-22 10:43           ` David Hildenbrand
2021-01-18 13:13 ` [PATCH V3 2/3] arm64/mm: Define arch_get_mappable_range() Anshuman Khandual
2021-01-18 13:13   ` Anshuman Khandual
2021-01-19 12:24   ` David Hildenbrand
2021-01-19 12:24     ` David Hildenbrand
2021-01-18 13:13 ` [PATCH V3 3/3] s390/mm: " Anshuman Khandual
2021-01-18 13:13   ` Anshuman Khandual
2021-01-19 12:26   ` David Hildenbrand
2021-01-19 12:26     ` David Hildenbrand
2021-01-20  8:28     ` Anshuman Khandual
2021-01-20  8:28       ` Anshuman Khandual
2021-01-20 10:39       ` David Hildenbrand
2021-01-20 10:39         ` David Hildenbrand
2021-01-18 13:13 ` [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug Anshuman Khandual
2021-01-18 13:13   ` Anshuman Khandual
2021-01-18 13:21   ` Anshuman Khandual
2021-01-18 13:21     ` Anshuman Khandual
2021-01-19 12:27     ` David Hildenbrand
2021-01-19 12:27       ` David Hildenbrand
2021-01-21  9:57     ` David Hildenbrand
2021-01-21  9:57       ` David Hildenbrand
2021-01-22  3:32       ` Anshuman Khandual
2021-01-22  3:32         ` Anshuman Khandual
2021-01-19 13:33 ` [PATCH V3 0/3] mm/memory_hotplug: Pre-validate the address range with platform David Hildenbrand
2021-01-19 13:33   ` David Hildenbrand
2021-01-19 13:40   ` Oscar Salvador
2021-01-19 13:40     ` Oscar Salvador
2021-01-20  8:37     ` Anshuman Khandual
2021-01-20  8:37       ` Anshuman Khandual
2021-01-22  6:04       ` Anshuman Khandual
2021-01-22  6:04         ` Anshuman Khandual
2021-01-22  8:34         ` David Hildenbrand
2021-01-22  8:34           ` David Hildenbrand
  -- strict thread matches above, loose matches on Subject: below --
2020-12-17 15:28 [PATCH V2 0/3] mm/hotplug: " Anshuman Khandual
2021-01-11 12:41 ` [PATCH RFC] virtio-mem: check against memhp_get_pluggable_range() which memory we can hotplug David Hildenbrand
2021-01-11 19:49   ` kernel test robot
2021-01-12  4:25   ` Anshuman Khandual

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.