Skip to contentSkip to conversation

implement "memmap on memory" feature on s390

PATCH Superseded Sumanth Korikkar 2 participants 13 messages Nov 27, 2023 – Nov 28, 2023

Subsystems (6)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover implement "memmap on memory" feature on s390

Hi All,

The patch series implements "memmap on memory" feature on s390.

Patch 1 introduces MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE memory notifiers to prepare the transition of memory to and from a physically accessible state. New mhp_flag MHP_OFFLINE_INACCESSIBLE is introduced to ensure altmap cannot be written when addidng memory - before it is set online. This enhancement is crucial for implementing the "memmap on memory" feature for s390 in a subsequent patch.

Patches 2 allocates vmemmap pages from self-contained memory range for s390. It allocates memory map (struct pages array) from the hotplugged memory range, rather than using system memory by passing altmap to vmemmap functions.

Patch 3 removes unhandled memory notifier types on s390.

Patch 4 implements MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE memory notifiers on s390. MEM_PREPARE_ONLINE memory notifier makes memory block physical accessible via sclp assign command. The notifier ensures self-contained memory maps are accessible and hence enabling the "memmap on memory" on s390. MEM_FINISH_OFFLINE memory notifier shifts the memory block to an inaccessible state via sclp unassign command

Patch 5 finally enables MHP_MEMMAP_ON_MEMORY on s390

Note:
These patches are rebased on top of three fixes:
mm: use vmem_altmap code without CONFIG_ZONE_DEVICE mm/memory_hotplug: fix error handling in add_memory_resource() mm/memory_hotplug: add missing mem_hotplug_lock

v3:
* added comments to MHP_OFFLINE_ACCESSIBLE as suggested by David.
* Squashed three commits related to new memory notifier.

v2:
* Fixes are integrated and hence removed from this patch series Suggestions from David:
* Add new flag MHP_OFFLINE_INACCESSIBLE to avoid accessing memory during memory hotplug addition phase.
* Avoid page_init_poison() on memmap during mhp addition phase, when MHP_OFFLINE_INACCESSIBLE mhp_flag is passed in add_memory().
* Do not skip add_pages() in arch_add_memory(). Similarly, remove similar hacks in arch_remove_memory().
* Use MHP_PREPARE_ONLINE/MHP_FINISH_OFFLINE naming convention for new memory notifiers.
* Rearrange removal of unused s390 memory notifier.
* Necessary commit messages changes.

Thank you

Sumanth Korikkar (5):
mm/memory_hotplug: introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE
notifiers
s390/mm: allocate vmemmap pages from self-contained memory range s390/sclp: remove unhandled memory notifier type s390/mm: implement MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers
s390: enable MHP_MEMMAP_ON_MEMORY

 arch/s390/Kconfig              |  1 +
 arch/s390/mm/init.c            |  3 --
 arch/s390/mm/vmem.c            | 62 +++++++++++++++++++---------------
 drivers/base/memory.c          | 21 ++++++++++--
 drivers/s390/char/sclp_cmd.c   | 31 ++++++++++++-----
 include/linux/memory.h         |  2 ++
 include/linux/memory_hotplug.h | 18 +++++++++-
 include/linux/memremap.h       |  1 +
 mm/memory_hotplug.c            | 30 ++++++++++++++--
 mm/sparse.c                    |  3 +-
 10 files changed, 127 insertions(+), 45 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH v3 1/5 mm/memory_hotplug: introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

Introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE memory notifiers to prepare the transition of memory to and from a physically accessible state. This enhancement is crucial for implementing the "memmap on memory" feature for s390 in a subsequent patch.

Platforms such as x86 can support physical memory hotplug via ACPI. When there is physical memory hotplug, ACPI event leads to the memory addition with the following callchain:
acpi_memory_device_add()
-> acpi_memory_enable_device()

     -> __add_memory()

After this, the hotplugged memory is physically accessible, and altmap support prepared, before the "memmap on memory" initialization in memory_block_online() is called.

On s390, memory hotplug works in a different way. The available hotplug memory has to be defined upfront in the hypervisor, but it is made physically accessible only when the user sets it online via sysfs, currently in the MEM_GOING_ONLINE notifier. This is too late and "memmap on memory" initialization is performed before calling MEM_GOING_ONLINE notifier.

During the memory hotplug addition phase, altmap support is prepared and during the memory onlining phase s390 requires memory to be physically accessible and then subsequently initiate the "memmap on memory" initialization process.

The memory provider will handle new MEM_PREPARE_ONLINE / MEM_FINISH_OFFLINE notifications and make the memory accessible.

The mhp_flag MHP_OFFLINE_INACCESSIBLE is introduced and is relevant when used along with MHP_MEMMAP_ON_MEMORY, because the altmap cannot be written (e.g., poisoned) when adding memory -- before it is set online. This allows for adding memory with an altmap that is not currently made available by a hypervisor. When onlining that memory, the hypervisor can be instructed to make that memory accessible via the new notifiers and the onlining phase will not require any memory allocations, which is helpful in low-memory situations.

All architectures ignore unknown memory notifiers. Therefore, the introduction of these new notifiers does not result in any functional modifications across architectures.

---
 drivers/base/memory.c          | 21 +++++++++++++++++++--
 include/linux/memory.h         |  2 ++
 include/linux/memory_hotplug.h | 18 +++++++++++++++++-
 include/linux/memremap.h       |  1 +
 mm/memory_hotplug.c            | 30 ++++++++++++++++++++++++++++--
 mm/sparse.c                    |  3 ++-
 6 files changed, 69 insertions(+), 6 deletions(-)
drivers/base/memory.c +19 -2
index 8a13babd826c..5c6b2af75db4 100644
@@ -188,6 +188,7 @@ static int memory_block_online(struct memory_block *mem)
 	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
 	unsigned long nr_vmemmap_pages = 0;
+	struct memory_notify arg;
 	struct zone *zone;
 	int ret;
 
@@ -197,6 +198,14 @@ static int memory_block_online(struct memory_block *mem)
 	zone = zone_for_pfn_range(mem->online_type, mem->nid, mem->group,
 				  start_pfn, nr_pages);
 
+	arg.start_pfn = start_pfn;
+	arg.nr_pages = nr_pages;
+	mem_hotplug_begin();
+	ret = memory_notify(MEM_PREPARE_ONLINE, &arg);
+	ret = notifier_to_errno(ret);
+	if (ret)
+		goto out_notifier;
+
 	/*
 	 * Although vmemmap pages have a different lifecycle than the pages
 	 * they describe (they remain until the memory is unplugged), doing
@@ -207,9 +216,9 @@ static int memory_block_online(struct memory_block *mem)
 	if (mem->altmap)
 		nr_vmemmap_pages = mem->altmap->free;
 
-	mem_hotplug_begin();
 	if (nr_vmemmap_pages) {
-		ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages, zone);
+		ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages,
+						zone, mem->altmap->inaccessible);
 		if (ret)
 			goto out;
 	}
@@ -231,7 +240,11 @@ static int memory_block_online(struct memory_block *mem)
 					  nr_vmemmap_pages);
 
 	mem->zone = zone;
+	mem_hotplug_done();
+	return ret;
 out:
+	memory_notify(MEM_FINISH_OFFLINE, &arg);
+out_notifier:
 	mem_hotplug_done();
 	return ret;
 }
@@ -244,6 +257,7 @@ static int memory_block_offline(struct memory_block *mem)
 	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
 	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
 	unsigned long nr_vmemmap_pages = 0;
+	struct memory_notify arg;
 	int ret;
 
 	if (!mem->zone)
@@ -275,6 +289,9 @@ static int memory_block_offline(struct memory_block *mem)
 		mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
 
 	mem->zone = NULL;
+	arg.start_pfn = start_pfn;
+	arg.nr_pages = nr_pages;
+	memory_notify(MEM_FINISH_OFFLINE, &arg);
 out:
 	mem_hotplug_done();
 	return ret;
include/linux/memory.h +2 -0
index f53cfdaaaa41..de802994a8fa 100644
@@ -96,6 +96,8 @@ int set_memory_block_size_order(unsigned int order);
 #define	MEM_GOING_ONLINE	(1<<3)
 #define	MEM_CANCEL_ONLINE	(1<<4)
 #define	MEM_CANCEL_OFFLINE	(1<<5)
+#define	MEM_PREPARE_ONLINE	(1<<6)
+#define	MEM_FINISH_OFFLINE	(1<<7)
 
 struct memory_notify {
 	unsigned long start_pfn;
include/linux/memory_hotplug.h +17 -1
index 7d2076583494..ee00015575aa 100644
@@ -106,6 +106,22 @@ typedef int __bitwise mhp_t;
  * implies the node id (nid).
  */
 #define MHP_NID_IS_MGID		((__force mhp_t)BIT(2))
+/*
+ * The hotplugged memory is completely inaccessible while the memory is
+ * offline. The memory provider will handle MEM_PREPARE_ONLINE /
+ * MEM_FINISH_OFFLINE notifications and make the memory accessible.
+ *
+ * This flag is only relevant when used along with MHP_MEMMAP_ON_MEMORY,
+ * because the altmap cannot be written (e.g., poisoned) when adding
+ * memory -- before it is set online.
+ *
+ * This allows for adding memory with an altmap that is not currently
+ * made available by a hypervisor. When onlining that memory, the
+ * hypervisor can be instructed to make that memory available, and
+ * the onlining phase will not require any memory allocations, which is
+ * helpful in low-memory situations.
+ */
+#define MHP_OFFLINE_INACCESSIBLE	((__force mhp_t)BIT(3))
 
 /*
  * Extended parameters for memory hotplug:
@@ -154,7 +170,7 @@ extern void adjust_present_page_count(struct page *page,
 				      long nr_pages);
 /* VM interface that may be used by firmware interface */
 extern int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
-				     struct zone *zone);
+				     struct zone *zone, bool mhp_off_inaccessible);
 extern void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages);
 extern int online_pages(unsigned long pfn, unsigned long nr_pages,
 			struct zone *zone, struct memory_group *group);
include/linux/memremap.h +1 -0
index 744c830f4b13..9837f3e6fb95 100644
@@ -25,6 +25,7 @@ struct vmem_altmap {
 	unsigned long free;
 	unsigned long align;
 	unsigned long alloc;
+	bool inaccessible;
 };
 
 /*
mm/memory_hotplug.c +28 -2
index 7a5fc89a8652..ac7cfc09502d 100644
@@ -1083,8 +1083,25 @@ void adjust_present_page_count(struct page *page, struct memory_group *group,
 		group->present_kernel_pages += nr_pages;
 }
 
+static void page_init_poison_with_resched(unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long pfn, cur_nr_pages;
+
+	/* Poison struct pages because they are now uninitialized again. */
+	for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {
+		cond_resched();
+
+		/* Select all remaining pages up to the next section boundary */
+		cur_nr_pages =
+			min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);
+		page_init_poison(pfn_to_page(pfn),
+				 sizeof(struct page) * cur_nr_pages);
+	}
+}
+
 int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
-			      struct zone *zone)
+			      struct zone *zone, bool mhp_off_inaccessible)
 {
 	unsigned long end_pfn = pfn + nr_pages;
 	int ret, i;
@@ -1092,7 +1109,14 @@ int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
 	ret = kasan_add_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages));
 	if (ret)
 		return ret;
-
+	/*
+	 * Memory block is accessible at this stage and hence poison the struct
+	 * pages now.  If the memory block is accessible during memory hotplug
+	 * addition phase, then page poisining is already performed in
+	 * sparse_add_section().
+	 */
+	if (mhp_off_inaccessible)
+		page_init_poison_with_resched(pfn, nr_pages);
 	move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_UNMOVABLE);
 
 	for (i = 0; i < nr_pages; i++)
@@ -1439,6 +1463,8 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
 	if (mhp_flags & MHP_MEMMAP_ON_MEMORY) {
 		if (mhp_supports_memmap_on_memory(size)) {
 			mhp_altmap.free = memory_block_memmap_on_memory_pages();
+			if (mhp_flags & MHP_OFFLINE_INACCESSIBLE)
+				mhp_altmap.inaccessible = true;
 			params.altmap = kmalloc(sizeof(struct vmem_altmap), GFP_KERNEL);
 			if (!params.altmap) {
 				ret = -ENOMEM;
mm/sparse.c +2 -1
index 77d91e565045..3991c717b769 100644
@@ -907,7 +907,8 @@ int __meminit sparse_add_section(int nid, unsigned long start_pfn,
 	 * Poison uninitialized struct pages in order to catch invalid flags
 	 * combinations.
 	 */
-	page_init_poison(memmap, sizeof(struct page) * nr_pages);
+	if (!altmap || !altmap->inaccessible)
+		page_init_poison(memmap, sizeof(struct page) * nr_pages);
 
 	ms = __nr_to_section(section_nr);
 	set_section_nid(section_nr, nid);
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH v3 1/5] mm/memory_hotplug: introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

Quoted 48 lines · Sumanth Korikkar

Introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE memory notifiers to prepare the transition of memory to and from a physically accessible state. This enhancement is crucial for implementing the "memmap on memory" feature for s390 in a subsequent patch.

Platforms such as x86 can support physical memory hotplug via ACPI. When there is physical memory hotplug, ACPI event leads to the memory addition with the following callchain:
acpi_memory_device_add()
-> acpi_memory_enable_device()

      -> __add_memory()

After this, the hotplugged memory is physically accessible, and altmap support prepared, before the "memmap on memory" initialization in memory_block_online() is called.

On s390, memory hotplug works in a different way. The available hotplug memory has to be defined upfront in the hypervisor, but it is made physically accessible only when the user sets it online via sysfs, currently in the MEM_GOING_ONLINE notifier. This is too late and "memmap on memory" initialization is performed before calling MEM_GOING_ONLINE notifier.

During the memory hotplug addition phase, altmap support is prepared and during the memory onlining phase s390 requires memory to be physically accessible and then subsequently initiate the "memmap on memory" initialization process.

The memory provider will handle new MEM_PREPARE_ONLINE / MEM_FINISH_OFFLINE notifications and make the memory accessible.

The mhp_flag MHP_OFFLINE_INACCESSIBLE is introduced and is relevant when used along with MHP_MEMMAP_ON_MEMORY, because the altmap cannot be written (e.g., poisoned) when adding memory -- before it is set online. This allows for adding memory with an altmap that is not currently made available by a hypervisor. When onlining that memory, the hypervisor can be instructed to make that memory accessible via the new notifiers and the onlining phase will not require any memory allocations, which is helpful in low-memory situations.

All architectures ignore unknown memory notifiers. Therefore, the introduction of these new notifiers does not result in any functional modifications across architectures.

---

[...]

Quoted 37 lines
  };

/*

mm/memory_hotplug.c +18 -1
index 7a5fc89a8652..ac7cfc09502d 100644
@@ -1083,8 +1083,25 @@ void adjust_present_page_count(struct page *page, struct memory_group *group,
  		group->present_kernel_pages += nr_pages;
  }
 
+static void page_init_poison_with_resched(unsigned long start_pfn, unsigned long nr_pages)
+{
+	const unsigned long end_pfn = start_pfn + nr_pages;
+	unsigned long pfn, cur_nr_pages;
+
+	/* Poison struct pages because they are now uninitialized again. */
+	for (pfn = start_pfn; pfn < end_pfn; pfn += cur_nr_pages) {
+		cond_resched();
+
+		/* Select all remaining pages up to the next section boundary */
+		cur_nr_pages =
+			min(end_pfn - pfn, SECTION_ALIGN_UP(pfn + 1) - pfn);
+		page_init_poison(pfn_to_page(pfn),
+				 sizeof(struct page) * cur_nr_pages);
+	}
+}
+
  int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
-			      struct zone *zone)
+			      struct zone *zone, bool mhp_off_inaccessible)
  {
  	unsigned long end_pfn = pfn + nr_pages;
  	int ret, i;
@@ -1092,7 +1109,14 @@ int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
  	ret = kasan_add_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages));
  	if (ret)

mm/memory_hotplug.c int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages, · line 1119

Quoted 10 lines
mm/memory_hotplug.c +8 -1
  		return ret;
-
+	/*
+	 * Memory block is accessible at this stage and hence poison the struct
+	 * pages now.  If the memory block is accessible during memory hotplug
+	 * addition phase, then page poisining is already performed in
+	 * sparse_add_section().
+	 */
+	if (mhp_off_inaccessible)
+		page_init_poison_with_resched(pfn, nr_pages);

Can you elaborate why a simple page_init_poison() as for sparse_add_section() is insufficient?

Apart from that looks good.

Ideally, we'd be updating altmap->inaccessible as we online/offline memory. But then, we'd have to remember MHP_OFFLINE_INACCESSIBLE somehow differently.

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

Re: [PATCH v3 1/5] mm/memory_hotplug: introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

Quoted 49 lines · David Hildenbrand
Quoted 48 lines · Sumanth Korikkar

Introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE memory notifiers to prepare the transition of memory to and from a physically accessible state. This enhancement is crucial for implementing the "memmap on memory" feature for s390 in a subsequent patch.

Platforms such as x86 can support physical memory hotplug via ACPI. When there is physical memory hotplug, ACPI event leads to the memory addition with the following callchain:
acpi_memory_device_add()
-> acpi_memory_enable_device()

      -> __add_memory()

After this, the hotplugged memory is physically accessible, and altmap support prepared, before the "memmap on memory" initialization in memory_block_online() is called.

On s390, memory hotplug works in a different way. The available hotplug memory has to be defined upfront in the hypervisor, but it is made physically accessible only when the user sets it online via sysfs, currently in the MEM_GOING_ONLINE notifier. This is too late and "memmap on memory" initialization is performed before calling MEM_GOING_ONLINE notifier.

During the memory hotplug addition phase, altmap support is prepared and during the memory onlining phase s390 requires memory to be physically accessible and then subsequently initiate the "memmap on memory" initialization process.

The memory provider will handle new MEM_PREPARE_ONLINE / MEM_FINISH_OFFLINE notifications and make the memory accessible.

The mhp_flag MHP_OFFLINE_INACCESSIBLE is introduced and is relevant when used along with MHP_MEMMAP_ON_MEMORY, because the altmap cannot be written (e.g., poisoned) when adding memory -- before it is set online. This allows for adding memory with an altmap that is not currently made available by a hypervisor. When onlining that memory, the hypervisor can be instructed to make that memory accessible via the new notifiers and the onlining phase will not require any memory allocations, which is helpful in low-memory situations.

All architectures ignore unknown memory notifiers. Therefore, the introduction of these new notifiers does not result in any functional modifications across architectures.

---

...

Quoted 14 lines
Quoted 14 lines

int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,

mm/memory_hotplug.c +4 -2
-			      struct zone *zone)
+			      struct zone *zone, bool mhp_off_inaccessible)
  {
  	unsigned long end_pfn = pfn + nr_pages;
  	int ret, i;
@@ -1092,7 +1109,14 @@ int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages,
  	ret = kasan_add_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages));
  	if (ret)
  		return ret;
-
+	/*
+	 * Memory block is accessible at this stage and hence poison the struct
+	 * pages now.  If the memory block is accessible during memory hotplug
Quoted 9 lines

mm/memory_hotplug.c int mhp_init_memmap_on_memory(unsigned long pfn, unsigned long nr_pages, · line 1119

Quoted 5 lines
mm/memory_hotplug.c +5 -0
+	 * addition phase, then page poisining is already performed in
+	 * sparse_add_section().
+	 */
+	if (mhp_off_inaccessible)
+		page_init_poison_with_resched(pfn, nr_pages);

Can you elaborate why a simple page_init_poison() as for sparse_add_section() is insufficient?

Looks like cond_resched() is not needed. page_init_poison() could be performed similar to when adding new memory in sparse_add_section(). IIUC, As memory is onlined in memory block granuality, this cond_resched() wouldnt be needed then.

Thanks

#4 lore raw

Re: [PATCH v3 1/5] mm/memory_hotplug: introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

In reply to Sumanth Korikkar
Quoted 69 lines · Sumanth Korikkar

Introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE memory notifiers to prepare the transition of memory to and from a physically accessible state. This enhancement is crucial for implementing the "memmap on memory" feature for s390 in a subsequent patch.

Platforms such as x86 can support physical memory hotplug via ACPI. When there is physical memory hotplug, ACPI event leads to the memory addition with the following callchain:
acpi_memory_device_add()
-> acpi_memory_enable_device()

      -> __add_memory()

After this, the hotplugged memory is physically accessible, and altmap support prepared, before the "memmap on memory" initialization in memory_block_online() is called.

On s390, memory hotplug works in a different way. The available hotplug memory has to be defined upfront in the hypervisor, but it is made physically accessible only when the user sets it online via sysfs, currently in the MEM_GOING_ONLINE notifier. This is too late and "memmap on memory" initialization is performed before calling MEM_GOING_ONLINE notifier.

During the memory hotplug addition phase, altmap support is prepared and during the memory onlining phase s390 requires memory to be physically accessible and then subsequently initiate the "memmap on memory" initialization process.

The memory provider will handle new MEM_PREPARE_ONLINE / MEM_FINISH_OFFLINE notifications and make the memory accessible.

The mhp_flag MHP_OFFLINE_INACCESSIBLE is introduced and is relevant when used along with MHP_MEMMAP_ON_MEMORY, because the altmap cannot be written (e.g., poisoned) when adding memory -- before it is set online. This allows for adding memory with an altmap that is not currently made available by a hypervisor. When onlining that memory, the hypervisor can be instructed to make that memory accessible via the new notifiers and the onlining phase will not require any memory allocations, which is helpful in low-memory situations.

All architectures ignore unknown memory notifiers. Therefore, the introduction of these new notifiers does not result in any functional modifications across architectures.

---
  drivers/base/memory.c          | 21 +++++++++++++++++++--
  include/linux/memory.h         |  2 ++
  include/linux/memory_hotplug.h | 18 +++++++++++++++++-
  include/linux/memremap.h       |  1 +
  mm/memory_hotplug.c            | 30 ++++++++++++++++++++++++++++--
  mm/sparse.c                    |  3 ++-
  6 files changed, 69 insertions(+), 6 deletions(-)
drivers/base/memory.c +1 -0
index 8a13babd826c..5c6b2af75db4 100644
@@ -188,6 +188,7 @@ static int memory_block_online(struct memory_block *mem)
  	unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  	unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  	unsigned long nr_vmemmap_pages = 0;
+	struct memory_notify arg;
  	struct zone *zone;
  	int ret;
 

drivers/base/memory.c static int memory_block_online(struct memory_block *mem) · line 202

Quoted 5 lines
drivers/base/memory.c +2 -0
@@ -197,6 +198,14 @@ static int memory_block_online(struct memory_block *mem)
  	zone = zone_for_pfn_range(mem->online_type, mem->nid, mem->group,
  				  start_pfn, nr_pages);
 
+	arg.start_pfn = start_pfn;
+	arg.nr_pages = nr_pages;

Thinking about it, it's possibly cleanest to send the altmap range along here.

Later memory onlining notifiers will only be notified about the actual !altmap part.

arg.altmap_start_pfn = start_pfn;
arg.altmap_nr_pages = nr_vmemmap_pages;
arg.start_pfn = start_pfn + nr_vmemmap_pages;
arg.nr_pages = nr_pages - nr_vmemmap_pages;

Use that for the two new notifiers only and document it. Thoughts?

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw

PATCH v3 2/5 s390/mm: allocate vmemmap pages from self-contained memory range

In reply to Sumanth Korikkar

Allocate memory map (struct pages array) from the hotplugged memory range, rather than using system memory. The change addresses the issue where standby memory, when configured to be much larger than online memory, could potentially lead to ipl failure due to memory map allocation from online memory. For example, 16MB of memory map allocation is needed for a memory block size of 1GB and when standby memory is configured much larger than online memory, this could lead to ipl failure.

To address this issue, the solution involves introducing "memmap on memory" using the vmem_altmap structure on s390. Architectures that want to implement it should pass the altmap to the vmemmap_populate() function and its associated callchain. This enhancement is discussed in the commit 4b94ffdc4163 ("x86, mm: introduce vmem_altmap to augment vmemmap_populate()").

Provide "memmap on memory" support for s390 by passing the altmap in vmemmap_populate() and its callchain. The allocation path is described as follows:
* When altmap is NULL in vmemmap_populate(), memory map allocation occurs using the existing vmemmap_alloc_block_buf().
* When altmap is not NULL in vmemmap_populate(), memory map allocation still uses vmemmap_alloc_block_buf(), but this function internally calls altmap_alloc_block_buf().

For deallocation, the process is outlined as follows:
* When altmap is NULL in vmemmap_free(), memory map deallocation happens through free_pages().
* When altmap is not NULL in vmemmap_free(), memory map deallocation occurs via vmem_altmap_free().

While memory map allocation is primarily handled through the self-contained memory map range, there might still be a small amount of system memory allocation required for vmemmap pagetables. To mitigate this impact, this feature will be limited to machines with EDAT1 support.

---
 arch/s390/mm/init.c |  3 ---
 arch/s390/mm/vmem.c | 62 +++++++++++++++++++++++++--------------------
 2 files changed, 35 insertions(+), 30 deletions(-)
arch/s390/mm/init.c +0 -3
index 43e612bc2bcd..8d9a60ccb777 100644
@@ -281,9 +281,6 @@ int arch_add_memory(int nid, u64 start, u64 size,
 	unsigned long size_pages = PFN_DOWN(size);
 	int rc;
 
-	if (WARN_ON_ONCE(params->altmap))
-		return -EINVAL;
-
 	if (WARN_ON_ONCE(params->pgprot.pgprot != PAGE_KERNEL.pgprot))
 		return -EINVAL;
 
arch/s390/mm/vmem.c +35 -27

194 lines not on this page · raw message

Signature · 1 line
2.41.0
#6 lore raw

PATCH v3 3/5 s390/sclp: remove unhandled memory notifier type

In reply to Sumanth Korikkar

Remove memory notifier types which are unhandled by s390. Unhandled memory notifier types are covered by default case.

---
 drivers/s390/char/sclp_cmd.c | 3 ---
 1 file changed, 3 deletions(-)
drivers/s390/char/sclp_cmd.c +0 -3
index 11c428f4c7cf..355e63e44e95 100644
@@ -340,9 +340,6 @@ static int sclp_mem_notifier(struct notifier_block *nb,
 		if (contains_standby_increment(start, start + size))
 			rc = -EPERM;
 		break;
-	case MEM_ONLINE:
-	case MEM_CANCEL_OFFLINE:
-		break;
 	case MEM_GOING_ONLINE:
 		rc = sclp_mem_change_state(start, size, 1);
 		break;
Signature · 1 line
2.41.0
#8 lore raw

PATCH v3 4/5 s390/mm: implement MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

In reply to Sumanth Korikkar

MEM_PREPARE_ONLINE memory notifier makes memory block physical accessible via sclp assign command. The notifier ensures self-contained memory maps are accessible and hence enabling the "memmap on memory" on s390.

MEM_FINISH_OFFLINE memory notifier shifts the memory block to an inaccessible state via sclp unassign command.

Implementation considerations:
* When MHP_MEMMAP_ON_MEMORY is disabled, the system retains the old behavior. This means the memory map is allocated from default memory.
* If MACHINE_HAS_EDAT1 is unavailable, MHP_MEMMAP_ON_MEMORY is automatically disabled. This ensures that vmemmap pagetables do not consume additional memory from the default memory allocator.
* The MEM_GOING_ONLINE notifier has been modified to perform no operation, as MEM_PREPARE_ONLINE already executes the sclp assign command.
* The MEM_CANCEL_ONLINE/MEM_OFFLINE notifier now performs no operation, as MEM_FINISH_OFFLINE already executes the sclp unassign command.

---
 drivers/s390/char/sclp_cmd.c | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
drivers/s390/char/sclp_cmd.c +22 -6
index 355e63e44e95..30b829e4c052 100644
@@ -18,6 +18,7 @@
 #include <linux/mm.h>
 #include <linux/mmzone.h>
 #include <linux/memory.h>
+#include <linux/memory_hotplug.h>
 #include <linux/module.h>
 #include <asm/ctlreg.h>
 #include <asm/chpid.h>
@@ -26,6 +27,7 @@
 #include <asm/sclp.h>
 #include <asm/numa.h>
 #include <asm/facility.h>
+#include <asm/page-states.h>
 
 #include "sclp.h"
 
@@ -319,6 +321,7 @@ static bool contains_standby_increment(unsigned long start, unsigned long end)
 static int sclp_mem_notifier(struct notifier_block *nb,
 			     unsigned long action, void *data)
 {
+	struct memory_block *memory_block;
 	unsigned long start, size;
 	struct memory_notify *arg;
 	unsigned char id;
@@ -340,18 +343,29 @@ static int sclp_mem_notifier(struct notifier_block *nb,
 		if (contains_standby_increment(start, start + size))
 			rc = -EPERM;
 		break;
-	case MEM_GOING_ONLINE:
+	case MEM_PREPARE_ONLINE:
+		memory_block = find_memory_block(pfn_to_section_nr(arg->start_pfn));
+		if (!memory_block) {
+			rc = -EINVAL;
+			goto out;
+		}
 		rc = sclp_mem_change_state(start, size, 1);
+		if (rc || !memory_block->altmap)
+			goto out;
+		/*
+		 * Set CMMA state to nodat here, since the struct page memory
+		 * at the beginning of the memory block will not go through the
+		 * buddy allocator later.
+		 */
+		__arch_set_page_nodat((void *)__va(start), memory_block->altmap->free);
 		break;
-	case MEM_CANCEL_ONLINE:
-		sclp_mem_change_state(start, size, 0);
-		break;
-	case MEM_OFFLINE:
+	case MEM_FINISH_OFFLINE:
 		sclp_mem_change_state(start, size, 0);
 		break;
 	default:
 		break;
 	}
+out:
 	mutex_unlock(&sclp_mem_mutex);
 	return rc ? NOTIFY_BAD : NOTIFY_OK;
 }
@@ -397,7 +411,9 @@ static void __init add_memory_merged(u16 rn)
 	if (!size)
 		goto skip_add;
 	for (addr = start; addr < start + size; addr += block_size)
-		add_memory(0, addr, block_size, MHP_NONE);
+		add_memory(0, addr, block_size,
+			   MACHINE_HAS_EDAT1 ?
+			   MHP_MEMMAP_ON_MEMORY | MHP_OFFLINE_INACCESSIBLE : MHP_NONE);
 skip_add:
 	first_rn = rn;
 	num = 1;
Signature · 1 line
2.41.0
#9 lore raw

Re: [PATCH v3 4/5] s390/mm: implement MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

Quoted 64 lines · Sumanth Korikkar

MEM_PREPARE_ONLINE memory notifier makes memory block physical accessible via sclp assign command. The notifier ensures self-contained memory maps are accessible and hence enabling the "memmap on memory" on s390.

MEM_FINISH_OFFLINE memory notifier shifts the memory block to an inaccessible state via sclp unassign command.

Implementation considerations:
* When MHP_MEMMAP_ON_MEMORY is disabled, the system retains the old behavior. This means the memory map is allocated from default memory.
* If MACHINE_HAS_EDAT1 is unavailable, MHP_MEMMAP_ON_MEMORY is automatically disabled. This ensures that vmemmap pagetables do not consume additional memory from the default memory allocator.
* The MEM_GOING_ONLINE notifier has been modified to perform no operation, as MEM_PREPARE_ONLINE already executes the sclp assign command.
* The MEM_CANCEL_ONLINE/MEM_OFFLINE notifier now performs no operation, as MEM_FINISH_OFFLINE already executes the sclp unassign command.

---
  drivers/s390/char/sclp_cmd.c | 28 ++++++++++++++++++++++------
  1 file changed, 22 insertions(+), 6 deletions(-)
drivers/s390/char/sclp_cmd.c +8 -1
index 355e63e44e95..30b829e4c052 100644
@@ -18,6 +18,7 @@
  #include <linux/mm.h>
  #include <linux/mmzone.h>
  #include <linux/memory.h>
+#include <linux/memory_hotplug.h>
  #include <linux/module.h>
  #include <asm/ctlreg.h>
  #include <asm/chpid.h>
@@ -26,6 +27,7 @@
  #include <asm/sclp.h>
  #include <asm/numa.h>
  #include <asm/facility.h>
+#include <asm/page-states.h>
 
  #include "sclp.h"
 
@@ -319,6 +321,7 @@ static bool contains_standby_increment(unsigned long start, unsigned long end)
  static int sclp_mem_notifier(struct notifier_block *nb,
  			     unsigned long action, void *data)
  {
+	struct memory_block *memory_block;
  	unsigned long start, size;
  	struct memory_notify *arg;
  	unsigned char id;
@@ -340,18 +343,29 @@ static int sclp_mem_notifier(struct notifier_block *nb,
  		if (contains_standby_increment(start, start + size))
  			rc = -EPERM;
  		break;
-	case MEM_GOING_ONLINE:
+	case MEM_PREPARE_ONLINE:
+		memory_block = find_memory_block(pfn_to_section_nr(arg->start_pfn));
+		if (!memory_block) {
+			rc = -EINVAL;
+			goto out;

drivers/s390/char/sclp_cmd.c static int sclp_mem_notifier(struct notifier_block *nb, · line 360

Quoted 10 lines
drivers/s390/char/sclp_cmd.c +9 -0
+		}
  		rc = sclp_mem_change_state(start, size, 1);
+		if (rc || !memory_block->altmap)
+			goto out;
+		/*
+		 * Set CMMA state to nodat here, since the struct page memory
+		 * at the beginning of the memory block will not go through the
+		 * buddy allocator later.
+		 */
+		__arch_set_page_nodat((void *)__va(start), memory_block->altmap->free);

Looking up the memory block and grabbing the altmap from there is a bit unfortunate.

Why can't we do that when adding the altmap? Will the hypervisor scream at us?

... would we want to communicate any altmap start+size via the memory notifier instead?

Signature · 3 lines
Cheers,

David / dhildenb
#10 lore raw

Re: [PATCH v3 4/5] s390/mm: implement MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

Quoted 50 lines · David Hildenbrand
Quoted 36 lines
drivers/s390/char/sclp_cmd.c +8 -1
index 355e63e44e95..30b829e4c052 100644
@@ -18,6 +18,7 @@
  #include <linux/mm.h>
  #include <linux/mmzone.h>
  #include <linux/memory.h>
+#include <linux/memory_hotplug.h>
  #include <linux/module.h>
  #include <asm/ctlreg.h>
  #include <asm/chpid.h>
@@ -26,6 +27,7 @@
  #include <asm/sclp.h>
  #include <asm/numa.h>
  #include <asm/facility.h>
+#include <asm/page-states.h>
  #include "sclp.h"
@@ -319,6 +321,7 @@ static bool contains_standby_increment(unsigned long start, unsigned long end)
  static int sclp_mem_notifier(struct notifier_block *nb,
  			     unsigned long action, void *data)
  {
+	struct memory_block *memory_block;
  	unsigned long start, size;
  	struct memory_notify *arg;
  	unsigned char id;
@@ -340,18 +343,29 @@ static int sclp_mem_notifier(struct notifier_block *nb,
  		if (contains_standby_increment(start, start + size))
  			rc = -EPERM;
  		break;
-	case MEM_GOING_ONLINE:
+	case MEM_PREPARE_ONLINE:
+		memory_block = find_memory_block(pfn_to_section_nr(arg->start_pfn));
+		if (!memory_block) {
+			rc = -EINVAL;
+			goto out;

drivers/s390/char/sclp_cmd.c static int sclp_mem_notifier(struct notifier_block *nb, · line 360

Quoted 10 lines
drivers/s390/char/sclp_cmd.c +9 -0
+		}
  		rc = sclp_mem_change_state(start, size, 1);
+		if (rc || !memory_block->altmap)
+			goto out;
+		/*
+		 * Set CMMA state to nodat here, since the struct page memory
+		 * at the beginning of the memory block will not go through the
+		 * buddy allocator later.
+		 */
+		__arch_set_page_nodat((void *)__va(start), memory_block->altmap->free);

Looking up the memory block and grabbing the altmap from there is a bit unfortunate.

Quoted 3 lines

Why can't we do that when adding the altmap? Will the hypervisor scream at us?

calling __arch_set_page_nodat() before making memory block accessible will lead to crash. Hence, we think this is the only safe location to place it.

Quoted 2 lines

... would we want to communicate any altmap start+size via the memory notifier instead?

Passing start, size of memory range via memory notifier looks correct approach to me, as we try to make the specified range accessible.

If we want to pass altmap size (nr_vmemmap_pages), then we might need a new field in struct memory_notify, which would prevent access of memory_block->altmap->free in the notifier.

Do you want to take this approach instead?

If yes, Then I could add a new field nr_vmemmap_pages in struct memory_notify and place it in PATCH : introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers.

Thanks

#11 lore raw

Re: [PATCH v3 4/5] s390/mm: implement MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers

Quoted 68 lines · Sumanth Korikkar
Quoted 50 lines · David Hildenbrand
Quoted 36 lines
drivers/s390/char/sclp_cmd.c +8 -1
index 355e63e44e95..30b829e4c052 100644
@@ -18,6 +18,7 @@
   #include <linux/mm.h>
   #include <linux/mmzone.h>
   #include <linux/memory.h>
+#include <linux/memory_hotplug.h>
   #include <linux/module.h>
   #include <asm/ctlreg.h>
   #include <asm/chpid.h>
@@ -26,6 +27,7 @@
   #include <asm/sclp.h>
   #include <asm/numa.h>
   #include <asm/facility.h>
+#include <asm/page-states.h>
   #include "sclp.h"
@@ -319,6 +321,7 @@ static bool contains_standby_increment(unsigned long start, unsigned long end)
   static int sclp_mem_notifier(struct notifier_block *nb,
   			     unsigned long action, void *data)
   {
+	struct memory_block *memory_block;
   	unsigned long start, size;
   	struct memory_notify *arg;
   	unsigned char id;
@@ -340,18 +343,29 @@ static int sclp_mem_notifier(struct notifier_block *nb,
   		if (contains_standby_increment(start, start + size))
   			rc = -EPERM;
   		break;
-	case MEM_GOING_ONLINE:
+	case MEM_PREPARE_ONLINE:
+		memory_block = find_memory_block(pfn_to_section_nr(arg->start_pfn));
+		if (!memory_block) {
+			rc = -EINVAL;
+			goto out;

drivers/s390/char/sclp_cmd.c static int sclp_mem_notifier(struct notifier_block *nb, · line 360

Quoted 10 lines
drivers/s390/char/sclp_cmd.c +9 -0
+		}
   		rc = sclp_mem_change_state(start, size, 1);
+		if (rc || !memory_block->altmap)
+			goto out;
+		/*
+		 * Set CMMA state to nodat here, since the struct page memory
+		 * at the beginning of the memory block will not go through the
+		 * buddy allocator later.
+		 */
+		__arch_set_page_nodat((void *)__va(start), memory_block->altmap->free);

Looking up the memory block and grabbing the altmap from there is a bit unfortunate.

Quoted 3 lines

Why can't we do that when adding the altmap? Will the hypervisor scream at us?

calling __arch_set_page_nodat() before making memory block accessible will lead to crash. Hence, we think this is the only safe location to place it.

Quoted 2 lines

... would we want to communicate any altmap start+size via the memory notifier instead?

Passing start, size of memory range via memory notifier looks correct approach to me, as we try to make the specified range accessible.

If we want to pass altmap size (nr_vmemmap_pages), then we might need a new field in struct memory_notify, which would prevent access of memory_block->altmap->free in the notifier.

Quoted 5 lines

Do you want to take this approach instead?

If yes, Then I could add a new field nr_vmemmap_pages in struct memory_notify and place it in PATCH : introduce MEM_PREPARE_ONLINE/MEM_FINISH_OFFLINE notifiers.

Yes, see my other mail. That's probably cleanest!

Signature · 3 lines
Cheers,

David / dhildenb
#12 lore raw

PATCH v3 5/5 s390: enable MHP_MEMMAP_ON_MEMORY

In reply to Sumanth Korikkar

Enable MHP_MEMMAP_ON_MEMORY to support "memmap on memory". memory_hotplug.memmap_on_memory=true kernel parameter should be set in kernel boot option to enable the feature.

---
 arch/s390/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/s390/Kconfig +1 -0
index 3bec98d20283..4b9b0f947ddb 100644
@@ -113,6 +113,7 @@ config S390
 	select ARCH_INLINE_WRITE_UNLOCK_BH
 	select ARCH_INLINE_WRITE_UNLOCK_IRQ
 	select ARCH_INLINE_WRITE_UNLOCK_IRQRESTORE
+	select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE
 	select ARCH_STACKWALK
 	select ARCH_SUPPORTS_ATOMIC_RMW
 	select ARCH_SUPPORTS_DEBUG_PAGEALLOC
Signature · 1 line
2.41.0