Skip to contentSkip to conversation

powerpc/mm: Cleanup memory block size probing

PATCH Superseded Aneesh Kumar K.V 4 participants 9 messages Jun 9, 2023 – Jun 20, 2023

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 powerpc/mm: Cleanup memory block size probing

Parse the device tree in early init to find the memory block size to be used by the kernel. Consolidate the memory block size device tree parsing to one helper and use that on both powernv and pseries. We still want to use machine-specific callback because on all machine types other than powernv and pseries we continue to return MIN_MEMORY_BLOCK_SIZE.

pseries_memory_block_size used to look for the second memory block (memory@x) to determine the memory_block_size value. This patch changed that to look at all memory blocks and make sure we can map them all correctly using the computed memory block size value.

---
Changes from v1:
* Add a helper to parse device tree for memory block size
* Drop pseries and powernv functions used.

 arch/powerpc/include/asm/book3s/64/mmu.h      |  5 +-
 arch/powerpc/mm/book3s64/radix_pgtable.c      | 65 +------------
 arch/powerpc/mm/init_64.c                     | 95 +++++++++++++++++++
 arch/powerpc/platforms/powernv/setup.c        | 10 +-
 .../platforms/pseries/hotplug-memory.c        | 60 +-----------
 arch/powerpc/platforms/pseries/pseries.h      |  2 -
 arch/powerpc/platforms/pseries/setup.c        |  7 ++
 7 files changed, 109 insertions(+), 135 deletions(-)
arch/powerpc/include/asm/book3s/64/mmu.h +1 -4
index 570a4960cf17..28033fd5403c 100644
@@ -71,10 +71,7 @@ extern unsigned int mmu_pid_bits;
 /* Base PID to allocate from */
 extern unsigned int mmu_base_pid;
 
-/*
- * memory block size used with radix translation.
- */
-extern unsigned long __ro_after_init radix_mem_block_size;
+extern unsigned long __ro_after_init memory_block_size;
 
 #define PRTB_SIZE_SHIFT	(mmu_pid_bits + 4)
 #define PRTB_ENTRIES	(1ul << mmu_pid_bits)
arch/powerpc/mm/book3s64/radix_pgtable.c +1 -64

89 lines not on this page · raw message

arch/powerpc/mm/init_64.c +95 -0

113 lines not on this page · raw message

arch/powerpc/platforms/powernv/setup.c +1 -9
index 5e9c6b55809f..4dbb47ddbdcc 100644
@@ -482,15 +482,7 @@ static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
 #ifdef CONFIG_MEMORY_HOTPLUG
 static unsigned long pnv_memory_block_size(void)
 {
-	/*
-	 * We map the kernel linear region with 1GB large pages on radix. For
-	 * memory hot unplug to work our memory block size must be at least
-	 * this size.
-	 */
-	if (radix_enabled())
-		return radix_mem_block_size;
-	else
-		return 256UL * 1024 * 1024;
+	return memory_block_size;
 }
 #endif
 
arch/powerpc/platforms/pseries/hotplug-memory.c +4 -56

93 lines not on this page · raw message

arch/powerpc/platforms/pseries/pseries.h +0 -2
index f8bce40ebd0c..02c012976b19 100644
@@ -90,8 +90,6 @@ extern struct pci_controller_ops pseries_pci_controller_ops;
 int pseries_msi_allocate_domains(struct pci_controller *phb);
 void pseries_msi_free_domains(struct pci_controller *phb);
 
-unsigned long pseries_memory_block_size(void);
-
 extern int CMO_PrPSP;
 extern int CMO_SecPSP;
 extern unsigned long CMO_PageSize;
arch/powerpc/platforms/pseries/setup.c +7 -0
index e2a57cfa6c83..6683debe283e 100644
@@ -1116,6 +1116,13 @@ static int pSeries_pci_probe_mode(struct pci_bus *bus)
 	return PCI_PROBE_NORMAL;
 }
 
+#ifdef CONFIG_MEMORY_HOTPLUG
+static unsigned long pseries_memory_block_size(void)
+{
+	return memory_block_size;
+}
+#endif
+
 struct pci_controller_ops pseries_pci_controller_ops = {
 	.probe_mode		= pSeries_pci_probe_mode,
 #ifdef CONFIG_SPAPR_TCE_IOMMU
Signature · 1 line
2.40.1
#1 lore raw

PATCH v2 2/2 powerpc/mm: Add memory_block_size as a kernel parameter

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

Restrict memory_block_size value to a power of 2 value similar to LMB size. The memory block size should also be more than the section size.

---
 .../admin-guide/kernel-parameters.txt         |  3 +++
 arch/powerpc/kernel/setup_64.c                | 23 +++++++++++++++++++
 arch/powerpc/mm/init_64.c                     | 17 ++++++++++----
 3 files changed, 38 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
index 9e5bab29685f..833b8c5b4b4c 100644
@@ -3190,6 +3190,9 @@
 			Note that even when enabled, there are a few cases where
 			the feature is not effective.
 
+	memory_block_size=size [PPC]
+			 Use this parameter to configure the memory block size value.
+
 	memtest=	[KNL,X86,ARM,M68K,PPC,RISCV] Enable memtest
 			Format: <integer>
 			default : 0 <disable>
arch/powerpc/kernel/setup_64.c +23 -0
index 246201d0d879..cbdb924462c7 100644
@@ -892,6 +892,29 @@ unsigned long memory_block_size_bytes(void)
 
 	return MIN_MEMORY_BLOCK_SIZE;
 }
+
+/*
+ * Restrict to a power of 2 value for memblock which is larger than
+ * section size
+ */
+static int __init parse_mem_block_size(char *ptr)
+{
+	unsigned int order;
+	unsigned long size = memparse(ptr, NULL);
+
+	order = fls64(size);
+	if (!order)
+		return 0;
+
+	order--;
+	if (order < SECTION_SIZE_BITS)
+		return 0;
+
+	memory_block_size = 1UL << order;
+
+	return 0;
+}
+early_param("memory_block_size", parse_mem_block_size);
 #endif
 
 #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
arch/powerpc/mm/init_64.c +12 -5
index 97a9163f1280..5e6dde593ea3 100644
@@ -549,13 +549,20 @@ static int __init probe_memory_block_size(unsigned long node, const char *uname,
 	return 0;
 }
 
-/*
- * start with 1G memory block size. Early init will
- * fix this with correct value.
- */
-unsigned long memory_block_size __ro_after_init = 1UL << 30;
+unsigned long memory_block_size __ro_after_init;
 static void __init early_init_memory_block_size(void)
 {
+	/*
+	 * if it is set via early param just return.
+	 */
+	if (memory_block_size)
+		return;
+
+	/*
+	 * start with 1G memory block size. update_memory_block_size()
+	 * will derive the right value based on device tree details.
+	 */
+	memory_block_size = 1UL << 30;
 	/*
 	 * We need to do memory_block_size probe early so that
 	 * radix__early_init_mmu() can use this as limit for
Signature · 1 line
2.40.1
Reviewed-by on v2 1/2
#8 lore raw
In reply to Aneesh Kumar K.V
Quoted 10 lines · Aneesh Kumar K.V

Parse the device tree in early init to find the memory block size to be used by the kernel. Consolidate the memory block size device tree parsing to one helper and use that on both powernv and pseries. We still want to use machine-specific callback because on all machine types other than powernv and pseries we continue to return MIN_MEMORY_BLOCK_SIZE.

pseries_memory_block_size used to look for the second memory block (memory@x) to determine the memory_block_size value. This patch changed that to look at all memory blocks and make sure we can map them all correctly using the computed memory block size value.

Signature · 1 line
Reza Arbab
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] powerpc/mm: Add memory_block_size as a kernel parameter

In reply to Aneesh Kumar K.V
Quoted 3 lines · Aneesh Kumar K.V

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

Case in point; the memory block size determined at boot is 1GB, but we know that 15.75GB of device memory will be hotplugged during runtime.

Signature · 1 line
Reza Arbab
#3 lore raw

Re: [PATCH v2 2/2] powerpc/mm: Add memory_block_size as a kernel parameter

In reply to Aneesh Kumar K.V
Quoted 3 lines · Aneesh Kumar K.V

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

So, the unfortunate thing is that these devices would have worked out of the box before the memory block size was increased from 256 MiB to 1 GiB in these setups. Now, one has to fine-tune the memory block size. The only other arch that I know, which supports setting the memory block size, is x86 for special (large) UV systems -- and at least in the past 128 MiB vs. 2 GiB memory blocks made a performance difference during boot (maybe no longer today, who knows).

Obviously, less tunable and getting stuff simply working out of the box is preferable.

Two questions:

1) Isn't there a way to improve auto-detection to fallback to 256 MiB in these setups, to avoid specifying these parameters?

2) Is the 256 MiB -> 1 GiB memory block size switch really worth it? On x86-64, experiments (with direct map fragmentation) showed that the effective performance boost is pretty insignificant, so I wonder how big the 1 GiB direct map performance improvement is.

I guess the only real issue with 256 MiB memory blocks and 1 GiB direct mapping is memory unplug of boot memory: when unplugging a 256 MiB block, one would have to remap the 1 GiB range using 2 MiB ranges.

... I was wondering what would happen if you simply leave the direct mapping in this corner case in place instead of doing this remapping. IOW, remove the memory but keep the direct map pointing at the removed memory. Nobody should be touching it, or are there any cases where that could hurt?

Or is there any other reason why we really want 1 GiB memory blocks instead of to defaulting to 256 MiB the way it used to be?

Thanks!

Quoted 89 lines

Restrict memory_block_size value to a power of 2 value similar to LMB size. The memory block size should also be more than the section size.

---
  .../admin-guide/kernel-parameters.txt         |  3 +++
  arch/powerpc/kernel/setup_64.c                | 23 +++++++++++++++++++
  arch/powerpc/mm/init_64.c                     | 17 ++++++++++----
  3 files changed, 38 insertions(+), 5 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +3 -0
index 9e5bab29685f..833b8c5b4b4c 100644
@@ -3190,6 +3190,9 @@
  			Note that even when enabled, there are a few cases where
  			the feature is not effective.
 
+	memory_block_size=size [PPC]
+			 Use this parameter to configure the memory block size value.
+
  	memtest=	[KNL,X86,ARM,M68K,PPC,RISCV] Enable memtest
  			Format: <integer>
  			default : 0 <disable>
arch/powerpc/kernel/setup_64.c +23 -0
index 246201d0d879..cbdb924462c7 100644
@@ -892,6 +892,29 @@ unsigned long memory_block_size_bytes(void)
 
  	return MIN_MEMORY_BLOCK_SIZE;
  }
+
+/*
+ * Restrict to a power of 2 value for memblock which is larger than
+ * section size
+ */
+static int __init parse_mem_block_size(char *ptr)
+{
+	unsigned int order;
+	unsigned long size = memparse(ptr, NULL);
+
+	order = fls64(size);
+	if (!order)
+		return 0;
+
+	order--;
+	if (order < SECTION_SIZE_BITS)
+		return 0;
+
+	memory_block_size = 1UL << order;
+
+	return 0;
+}
+early_param("memory_block_size", parse_mem_block_size);
  #endif
 
  #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
arch/powerpc/mm/init_64.c +12 -5
index 97a9163f1280..5e6dde593ea3 100644
@@ -549,13 +549,20 @@ static int __init probe_memory_block_size(unsigned long node, const char *uname,
  	return 0;
  }
 
-/*
- * start with 1G memory block size. Early init will
- * fix this with correct value.
- */
-unsigned long memory_block_size __ro_after_init = 1UL << 30;
+unsigned long memory_block_size __ro_after_init;
  static void __init early_init_memory_block_size(void)
  {
+	/*
+	 * if it is set via early param just return.
+	 */
+	if (memory_block_size)
+		return;
+
+	/*
+	 * start with 1G memory block size. update_memory_block_size()
+	 * will derive the right value based on device tree details.
+	 */
+	memory_block_size = 1UL << 30;
  	/*
  	 * We need to do memory_block_size probe early so that
  	 * radix__early_init_mmu() can use this as limit for
Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw

Re: [PATCH v2 2/2] powerpc/mm: Add memory_block_size as a kernel parameter

Quoted 14 lines · David Hildenbrand
Quoted 3 lines · Aneesh Kumar K.V

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

So, the unfortunate thing is that these devices would have worked out of the box before the memory block size was increased from 256 MiB to 1 GiB in these setups. Now, one has to fine-tune the memory block size. The only other arch that I know, which supports setting the memory block size, is x86 for special (large) UV systems -- and at least in the past 128 MiB vs. 2 GiB memory blocks made a performance difference during boot (maybe no longer today, who knows).

Quoted 7 lines

Obviously, less tunable and getting stuff simply working out of the box is preferable.

Two questions:

1) Isn't there a way to improve auto-detection to fallback to 256 MiB in these setups, to avoid specifying these parameters?

The patch does try to detect as much as possible by looking at device tree nodes and aperture window size. But there are still cases where we find a memory aperture of size X GB and device driver hotplug X.YGB memory.

Quoted 5 lines

2) Is the 256 MiB -> 1 GiB memory block size switch really worth it? On x86-64, experiments (with direct map fragmentation) showed that the effective performance boost is pretty insignificant, so I wonder how big the 1 GiB direct map performance improvement is.

Tarun is running some tests to evaluate the impact. We used to use 1GiB mapping always. This was later switched to use memory block size to fix issues with memory unplug
commit af9d00e93a4f ("powerpc/mm/radix: Create separate mappings for hot-plugged memory") explains some details related to that change.

Quoted 14 lines

I guess the only real issue with 256 MiB memory blocks and 1 GiB direct mapping is memory unplug of boot memory: when unplugging a 256 MiB block, one would have to remap the 1 GiB range using 2 MiB ranges.

... I was wondering what would happen if you simply leave the direct mapping in this corner case in place instead of doing this remapping. IOW, remove the memory but keep the direct map pointing at the removed memory. Nobody should be touching it, or are there any cases where that could hurt?

Quoted 3 lines

Or is there any other reason why we really want 1 GiB memory blocks instead of to defaulting to 256 MiB the way it used to be?

The idea we are working towards is to keep the memory block size small but map the boot memory using 1G. An unplug request can split that 1G mapping later. We could look at the possibility of leaving that mapping without splitting. But not sure why we would want to do that if we can correctly split things. Right now there is no splitting support in powerpc.

-aneesh

#5 lore raw

Re: [PATCH v2 2/2] powerpc/mm: Add memory_block_size as a kernel parameter

Quoted 19 lines · Aneesh Kumar K.V
Quoted 17 lines · David Hildenbrand
Quoted 3 lines · Aneesh Kumar K.V

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

So, the unfortunate thing is that these devices would have worked out of the box before the memory block size was increased from 256 MiB to 1 GiB in these setups. Now, one has to fine-tune the memory block size. The only other arch that I know, which supports setting the memory block size, is x86 for special (large) UV systems -- and at least in the past 128 MiB vs. 2 GiB memory blocks made a performance difference during boot (maybe no longer today, who knows).

Obviously, less tunable and getting stuff simply working out of the box is preferable.

Quoted 9 lines
Quoted 4 lines

Two questions:

1) Isn't there a way to improve auto-detection to fallback to 256 MiB in these setups, to avoid specifying these parameters?

The patch does try to detect as much as possible by looking at device tree nodes and aperture window size. But there are still cases where we find a memory aperture of size X GB and device driver hotplug X.YGB memory.

Okay, and I assume we can't detect that case easily.

Which interface is that device driver using to hotplug memory? It's quite surprising I have to say ...

Quoted 7 lines
Quoted 5 lines

2) Is the 256 MiB -> 1 GiB memory block size switch really worth it? On x86-64, experiments (with direct map fragmentation) showed that the effective performance boost is pretty insignificant, so I wonder how big the 1 GiB direct map performance improvement is.

Quoted 6 lines

Tarun is running some tests to evaluate the impact. We used to use 1GiB mapping always. This was later switched to use memory block size to fix issues with memory unplug
commit af9d00e93a4f ("powerpc/mm/radix: Create separate mappings for hot-plugged memory") explains some details related to that change.

IIUC, that commit (conditionally) increased the memory block size to avoid the splitting, correct? By that, it broke the device driver use case.

Quoted 15 lines
Quoted 15 lines

I guess the only real issue with 256 MiB memory blocks and 1 GiB direct mapping is memory unplug of boot memory: when unplugging a 256 MiB block, one would have to remap the 1 GiB range using 2 MiB ranges.

... I was wondering what would happen if you simply leave the direct mapping in this corner case in place instead of doing this remapping. IOW, remove the memory but keep the direct map pointing at the removed memory. Nobody should be touching it, or are there any cases where that could hurt?

Quoted 5 lines
Quoted 3 lines

Or is there any other reason why we really want 1 GiB memory blocks instead of to defaulting to 256 MiB the way it used to be?

The idea we are working towards is to keep the memory block size small

That would be preferable, yes ...

Quoted 4 lines

but map the boot memory using 1G. An unplug request can split that 1G mapping later. We could look at the possibility of leaving that mapping without splitting. But not sure why we would want to do that if we can correctly split things. Right now there is no splitting support in powerpc.

If splitting over-complicates the matter (and well, it will even consume more memory), it might at least be worth looking into that. Yes, it's cleaner.

I think there is also the option to fail memory offlining (and therefore unplug) if we have a 1 GiB mapping and don't want to split. For hotplugged memory it would always work to unplug again. aarch64 blocks any boot memory from getting unplugged.

But I guess that might break existing use cases (unplug boot memory) on ppc64 that rely on ZONE_MOVABLE to have it working with guarantees, right? Could be optimized but not sure if that's the best approach.

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw

Re: [PATCH v2 2/2] powerpc/mm: Add memory_block_size as a kernel parameter

In reply to David Hildenbrand
Quoted 19 lines · David Hildenbrand
Quoted 3 lines · Aneesh Kumar K.V

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

So, the unfortunate thing is that these devices would have worked out of the box before the memory block size was increased from 256 MiB to 1 GiB in these setups. Now, one has to fine-tune the memory block size. The only other arch that I know, which supports setting the memory block size, is x86 for special (large) UV systems -- and at least in the past 128 MiB vs. 2 GiB memory blocks made a performance difference during boot (maybe no longer today, who knows).

Obviously, less tunable and getting stuff simply working out of the box is preferable.

Two questions:

Quoted 7 lines

1) Isn't there a way to improve auto-detection to fallback to 256 MiB in these setups, to avoid specifying these parameters?

2) Is the 256 MiB -> 1 GiB memory block size switch really worth it? On x86-64, experiments (with direct map fragmentation) showed that the effective performance boost is pretty insignificant, so I wonder how big the 1 GiB direct map performance improvement is.

The other issue is simply the number of sysfs entries.

With 64TB of memory and a 256MB block size you end up with ~250,000 directories in /sys/devices/system/memory.

cheers

#7 lore raw

Re: [PATCH v2 2/2] powerpc/mm: Add memory_block_size as a kernel parameter

Quoted 28 lines · Michael Ellerman
Quoted 19 lines · David Hildenbrand
Quoted 3 lines · Aneesh Kumar K.V

Certain devices can possess non-standard memory capacities, not constrained to multiples of 1GB. Provide a kernel parameter so that we can map the device memory completely on memory hotplug.

So, the unfortunate thing is that these devices would have worked out of the box before the memory block size was increased from 256 MiB to 1 GiB in these setups. Now, one has to fine-tune the memory block size. The only other arch that I know, which supports setting the memory block size, is x86 for special (large) UV systems -- and at least in the past 128 MiB vs. 2 GiB memory blocks made a performance difference during boot (maybe no longer today, who knows).

Obviously, less tunable and getting stuff simply working out of the box is preferable.

Two questions:

Quoted 7 lines

1) Isn't there a way to improve auto-detection to fallback to 256 MiB in these setups, to avoid specifying these parameters?

2) Is the 256 MiB -> 1 GiB memory block size switch really worth it? On x86-64, experiments (with direct map fragmentation) showed that the effective performance boost is pretty insignificant, so I wonder how big the 1 GiB direct map performance improvement is.

Quoted 4 lines

The other issue is simply the number of sysfs entries.

With 64TB of memory and a 256MB block size you end up with ~250,000 directories in /sys/devices/system/memory.

Yes, and so far on other archs we only optimize for that for on UV x86 systems (with a default of 2 GiB). And that was added before we started to speed up memory device lookups significantly using a radix tree IIRC.

It's worth noting that there was a discussion on:

(a) not creating these device sysfs entries (when configured on the cmdline); often, nobody really ends up using them to online/offline memory blocks. Then, the only primary users is lsmem.

(b) exposing logical devices (e.g., a DIMM) taht can only be offlined/removed as a whole, instead of their individual memblocks (when configured on the cmdline). But for PPC64 that won't help.

But (a) gets more tricky if device drivers (and things like dax/kmem) rely on user-space memory onlining/offlining.

Signature · 3 lines
Cheers,

David / dhildenb