Add support for memmap on memory feature on ppc64
Subsystems (9)
cover Add support for memmap on memory feature on ppc64
This patch series update memmap on memory feature to fall back to memmap allocation outside the memory block if the alignment rules are not met. This makes the feature more useful on architectures like ppc64 where alignment rules are different with 64K page size.
This patch series is dependent on dax vmemmap optimization series
posted here https://lore.kernel.org/linux-mm/[email protected]
Aneesh Kumar K.V (5):
mm/hotplug: Embed vmem_altmap details in memory block mm/hotplug: Allow architecture override for memmap on memory feature mm/hotplug: Simplify the handling of MHP_MEMMAP_ON_MEMORY flag mm/hotplug: Simplify ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE kconfig powerpc/book3s64/memhotplug: Enable memmap on memory for radix
arch/arm64/Kconfig | 4 +- arch/arm64/mm/mmu.c | 5 + arch/powerpc/Kconfig | 1 + arch/powerpc/mm/book3s64/radix_pgtable.c | 28 ++++++ .../platforms/pseries/hotplug-memory.c | 4 +- arch/x86/Kconfig | 4 +- arch/x86/mm/init_64.c | 6 ++ drivers/acpi/acpi_memhotplug.c | 3 +- drivers/base/memory.c | 28 ++++-- include/linux/memory.h | 25 +++-- include/linux/memory_hotplug.h | 17 +++- include/linux/memremap.h | 18 +--- mm/Kconfig | 3 + mm/memory_hotplug.c | 95 +++++++++---------- 14 files changed, 151 insertions(+), 90 deletions(-)
Signature · 1 line
2.41.0
RFC PATCH 1/5 mm/hotplug: Embed vmem_altmap details in memory block
With memmap on memory, some architecture needs more details w.r.t altmap such as base_pfn, end_pfn, etc to unmap vmemmap memory.
Embed vmem_altmap data structure to memory_bock and use that instead of nr_vmemmap_pages.
On memory unplug, if the kernel finds any memory block in the range to be using vmem_altmap, the kernel fails to unplug the memory if the request is not a single memory block unplug.
--- drivers/base/memory.c | 28 ++++++++++++++++++--------- include/linux/memory.h | 25 ++++++++++++++++++------ include/linux/memremap.h | 18 +---------------- mm/memory_hotplug.c | 42 +++++++++++++++++----------------------- 4 files changed, 57 insertions(+), 56 deletions(-)
drivers/base/memory.c +19 -9
@@ -106,6 +106,7 @@ static void memory_block_release(struct device *dev) {struct memory_block *mem =to_memory_block (dev); +WARN (mem->altmap.alloc,"Altmap not fully unmapped" );kfree (mem); }
@@ -183,7 +184,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 = mem->nr_vmemmap_pages; +unsigned long nr_vmemmap_pages =0 ;struct zone *zone;int ret;
@@ -200,6 +201,9 @@ static int memory_block_online(struct memory_block *mem)* stage helps to keep accounting easier to follow - e.g vmemmaps * belong to the same zone as the memory they backed. */ +if (mem->altmap.alloc) + nr_vmemmap_pages = mem->altmap.alloc + mem->altmap.reserve; +if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret)
@@ -230,7 +234,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 = mem->nr_vmemmap_pages; +unsigned long nr_vmemmap_pages =0 ;int ret;if (!mem->zone)
@@ -240,6 +244,9 @@ static int memory_block_offline(struct memory_block *mem)* Unaccount before offlining, such that unpopulated zone and kthreads * can properly be torn down in offline_pages(). */ +if (mem->altmap.alloc) + nr_vmemmap_pages = mem->altmap.alloc + mem->altmap.reserve; +if (nr_vmemmap_pages)adjust_present_page_count (pfn_to_page (start_pfn), mem->group, -nr_vmemmap_pages);
@@ -726,7 +733,7 @@ void memory_block_add_nid(struct memory_block *mem, int nid,#endif static int add_memory_block (unsigned long block_id,unsigned long state, -unsigned long nr_vmemmap_pages, +struct vmem_altmap *altmap,struct memory_group *group) {struct memory_block *mem;
@@ -744,7 +751,10 @@ static int add_memory_block(unsigned long block_id, unsigned long state, mem->start_section_nr = block_id * sections_per_block; mem->state = state; mem->nid =NUMA_NO_NODE ; - mem->nr_vmemmap_pages = nr_vmemmap_pages; +if (altmap) +memcpy (&mem->altmap, altmap,sizeof (*altmap)); +else + mem->altmap.alloc =0 ;INIT_LIST_HEAD (&mem->group_next);#ifndef CONFIG_NUMA
@@ -783,14 +793,14 @@ static int __init add_boot_memory_block(unsigned long base_section_nr)if (section_count ==0 )return 0 ;return add_memory_block (memory_block_id (base_section_nr), -MEM_ONLINE ,0 ,NULL ); +MEM_ONLINE ,NULL ,NULL ); }static int add_hotplug_memory_block (unsigned long block_id, -unsigned long nr_vmemmap_pages, +struct vmem_altmap *altmap,struct memory_group *group) { -return add_memory_block (block_id,MEM_OFFLINE , nr_vmemmap_pages, group); +return add_memory_block (block_id,MEM_OFFLINE , altmap, group); }static void remove_memory_block (struct memory_block *memory)
@@ -818,7 +828,7 @@ static void remove_memory_block(struct memory_block *memory)* Called under device_hotplug_lock. */ int create_memory_block_devices (unsigned long start,unsigned long size, -unsigned long vmemmap_pages, +struct vmem_altmap *altmap,struct memory_group *group) {const unsigned long start_block_id =pfn_to_block_id (PFN_DOWN (start));
@@ -832,7 +842,7 @@ int create_memory_block_devices(unsigned long start, unsigned long size,return -EINVAL ;for (block_id = start_block_id; block_id != end_block_id; block_id++) { - ret =add_hotplug_memory_block (block_id, vmemmap_pages, group); + ret =add_hotplug_memory_block (block_id, altmap, group);if (ret)break ; }
include/linux/memory.h +19 -6
@@ -64,6 +64,23 @@ struct memory_group { }; }; +/** +* struct vmem_altmap - pre-allocated storage for vmemmap_populate +* @base_pfn: base of the entire dev_pagemap mapping +* @reserve: pages mapped, but reserved for driver use (relative to @base) +* @free: free pages set aside in the mapping for memmap storage +* @align: pages reserved to meet allocation alignments +* @alloc: track pages consumed, private to vmemmap_populate() +*/ +struct vmem_altmap { +unsigned long base_pfn; +const unsigned long end_pfn; +const unsigned long reserve; +unsigned long free; +unsigned long align; +unsigned long alloc; +}; +struct memory_block {unsigned long start_section_nr;unsigned long state;/* serialized by the dev->lock */
@@ -77,11 +94,7 @@ struct memory_block {*/ struct zone *zone;struct device dev; -/* -* Number of vmemmap pages. These pages -* lay at the beginning of the memory block. -*/ -unsigned long nr_vmemmap_pages; +struct vmem_altmap altmap;struct memory_group *group;/* group (if any) for this block */ struct list_head group_next;/* next block inside memory group */ #if defined(CONFIG_MEMORY_FAILURE ) && defined(CONFIG_MEMORY_HOTPLUG )
@@ -147,7 +160,7 @@ static inline int hotplug_memory_notifier(notifier_fn_t fn, int pri)extern int register_memory_notifier (struct notifier_block *nb);extern void unregister_memory_notifier (struct notifier_block *nb);int create_memory_block_devices (unsigned long start,unsigned long size, -unsigned long vmemmap_pages, +struct vmem_altmap *altmap,struct memory_group *group);void remove_memory_block_devices (unsigned long start,unsigned long size);extern void memory_dev_init (void );
include/linux/memremap.h +1 -17
@@ -2,6 +2,7 @@#ifndef _LINUX_MEMREMAP_H_#define _LINUX_MEMREMAP_H_ +#include <linux/memory.h> #include <linux/mmzone.h> #include <linux/range.h> #include <linux/ioport.h>
@@ -10,23 +11,6 @@struct resource ;struct device ; -/** -* struct vmem_altmap - pre-allocated storage for vmemmap_populate -* @base_pfn: base of the entire dev_pagemap mapping -* @reserve: pages mapped, but reserved for driver use (relative to @base) -* @free: free pages set aside in the mapping for memmap storage -* @align: pages reserved to meet allocation alignments -* @alloc: track pages consumed, private to vmemmap_populate() -*/ -struct vmem_altmap { -unsigned long base_pfn; -const unsigned long end_pfn; -const unsigned long reserve; -unsigned long free; -unsigned long align; -unsigned long alloc; -}; -/* * Specialize ZONE_DEVICE memory into multiple types each has a different * usage.
mm/memory_hotplug.c +18 -24
70 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC PATCH 2/5 mm/hotplug: Allow architecture override for memmap on memory feature
In reply to Aneesh Kumar K.VSome architectures like ppc64 wants to enable this feature only with radix translation and their vemmap mappings have different alignment requirements. Add overrider for mhp_supports_memmap_on_memory() and also use altmap.reserve feature to adjust the pageblock alignment requirement.
The patch also fallback to allocation of memmap outside memblock if the alignment rules are not met for memmap on memory allocation. This allows to use the feature more widely allocating memmap as much as possible within the memory block getting added.
A follow patch to enable memmap on memory for ppc64 will use this.
--- arch/arm64/mm/mmu.c | 5 +++++ arch/x86/mm/init_64.c | 6 ++++++ include/linux/memory_hotplug.h | 3 ++- mm/memory_hotplug.c | 36 ++++++++++++++++++++++++---------- 4 files changed, 39 insertions(+), 11 deletions(-)
arch/arm64/mm/mmu.c +5 -0
@@ -1335,6 +1335,11 @@ void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)__remove_pgd_mapping (swapper_pg_dir,__phys_to_virt (start), size); } +bool mhp_supports_memmap_on_memory (unsigned long size) +{ +return __mhp_supports_memmap_on_memory (size); +} +/* * This memory hotplug notifier helps prevent boot memory from being * inadvertently removed as it blocks pfn range offlining process in
arch/x86/mm/init_64.c +6 -0
@@ -1264,6 +1264,12 @@ void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)__remove_pages (start_pfn, nr_pages, altmap); kernel_physical_mapping_remove(start, start + size); } + +bool mhp_supports_memmap_on_memory (unsigned long size) +{ +return __mhp_supports_memmap_on_memory (size); +} +#endif /* CONFIG_MEMORY_HOTPLUG */ static struct kcore_list kcore_vsyscall;
include/linux/memory_hotplug.h +2 -1
@@ -358,7 +358,8 @@ extern struct zone *zone_for_pfn_range(int online_type, int nid,extern int arch_create_linear_mapping (int nid,u64 start,u64 size,struct mhp_params *params);void arch_remove_linear_mapping (u64 start,u64 size); -extern bool mhp_supports_memmap_on_memory (unsigned long size); +bool mhp_supports_memmap_on_memory (unsigned long size); +bool __mhp_supports_memmap_on_memory (unsigned long size);#endif /* CONFIG_MEMORY_HOTPLUG */ #endif /* __LINUX_MEMORY_HOTPLUG_H */
mm/memory_hotplug.c +26 -10
@@ -1284,7 +1284,8 @@ static int online_memory_block(struct memory_block *mem, void *arg)return device_online (&mem->dev); } -bool mhp_supports_memmap_on_memory (unsigned long size) +/* Helper function for architecture to use. */ +bool __mhp_supports_memmap_on_memory (unsigned long size) {unsigned long nr_vmemmap_pages = size /PAGE_SIZE ;unsigned long vmemmap_size = nr_vmemmap_pages *sizeof (struct page );
@@ -1322,6 +1323,20 @@ bool mhp_supports_memmap_on_memory(unsigned long size)IS_ALIGNED (remaining_size, (pageblock_nr_pages <<PAGE_SHIFT )); } +bool __weak mhp_supports_memmap_on_memory (unsigned long size) +{ +return false; +} + +/* +* Architectures may want to override the altmap reserve details based +* on the alignment requirement for vmemmap mapping. +*/ +unsigned __weak long memory_block_align_base (struct resource *res) +{ +return 0 ; +} +/* * NOTE: The caller must call lock_device_hotplug() to serialize hotplug * and online/offline operations (triggered e.g. by sysfs).
@@ -1332,7 +1347,11 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) {struct mhp_params params = { .pgprot =pgprot_mhp (PAGE_KERNEL ) };enum memblock_flags memblock_flags =MEMBLOCK_NONE ; -struct vmem_altmap mhp_altmap = {}; +struct vmem_altmap mhp_altmap = { + .base_pfn =PHYS_PFN (res->start), + .end_pfn =PHYS_PFN (res->end), + .reserve =memory_block_align_base (res), + };struct memory_group *group =NULL ;u64 start, size;bool new_node = false;
@@ -1376,13 +1395,11 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)* Self hosted memmap array */ if (mhp_flags &MHP_MEMMAP_ON_MEMORY ) { -if (!mhp_supports_memmap_on_memory (size)) { - ret = -EINVAL ; - goto error; +if (mhp_supports_memmap_on_memory (size)) { + mhp_altmap.free =PHYS_PFN (size) - mhp_altmap.reserve; + params.altmap = &mhp_altmap; } - mhp_altmap.free =PHYS_PFN (size); - mhp_altmap.base_pfn =PHYS_PFN (start); - params.altmap = &mhp_altmap; +/* if not supported don't use altmap */ }/* call arch's memory hotadd */
@@ -1391,8 +1408,7 @@ int __ref add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags) goto error;/* create memory block devices after memory was added */ - ret =create_memory_block_devices (start, size, &mhp_altmap, - group); + ret =create_memory_block_devices (start, size, &mhp_altmap, group);if (ret) {arch_remove_memory (start, size,NULL ); goto error;
Signature · 1 line
2.41.0
RFC PATCH 3/5 mm/hotplug: Simplify the handling of MHP_MEMMAP_ON_MEMORY flag
In reply to Aneesh Kumar K.VInstead of checking for memmap on memory feature enablement within the functions checking for alignment, use the kernel parameter to control the memory hotplug flags. The generic kernel now enables memmap on memory feature if the hotplug flag request for the same.
The ACPI code now can pass the flag unconditionally because the kernel will fallback to not using the feature if the alignment rules are not met.
--- drivers/acpi/acpi_memhotplug.c | 3 +-- include/linux/memory_hotplug.h | 14 ++++++++++++++ mm/memory_hotplug.c | 19 ++++--------------- 3 files changed, 19 insertions(+), 17 deletions(-)
drivers/acpi/acpi_memhotplug.c +1 -2
@@ -211,8 +211,7 @@ static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)if (!info->length)continue ; -if (mhp_supports_memmap_on_memory (info->length)) - mhp_flags |=MHP_MEMMAP_ON_MEMORY ; + mhp_flags |=get_memmap_on_memory_flags (); result =__add_memory (mgid, info->start_addr, info->length, mhp_flags);
include/linux/memory_hotplug.h +14 -0
@@ -362,4 +362,18 @@ bool mhp_supports_memmap_on_memory(unsigned long size);bool __mhp_supports_memmap_on_memory (unsigned long size);#endif /* CONFIG_MEMORY_HOTPLUG */ +#ifdef CONFIG_MHP_MEMMAP_ON_MEMORY +extern bool memmap_on_memory; +static inline unsigned long get_memmap_on_memory_flags (void ) +{ +if (memmap_on_memory) +return MHP_MEMMAP_ON_MEMORY ; +return 0 ; +} +#else +static inline unsigned long get_memmap_on_memory_flags (void ) +{ +return 0 ; +} +#endif #endif /* __LINUX_MEMORY_HOTPLUG_H */
mm/memory_hotplug.c +4 -15
@@ -46,19 +46,9 @@/* * memory_hotplug.memmap_on_memory parameter */ -static bool memmap_on_memory__ro_after_init ; +bool memmap_on_memory__ro_after_init ;module_param (memmap_on_memory, bool,0444 );MODULE_PARM_DESC (memmap_on_memory,"Enable memmap on memory for memory hotplug" ); - -static inline bool mhp_memmap_on_memory (void ) -{ -return memmap_on_memory; -} -#else -static inline bool mhp_memmap_on_memory (void ) -{ -return false; -}#endif enum {
@@ -1317,10 +1307,9 @@ bool __mhp_supports_memmap_on_memory(unsigned long size)* altmap as an alternative source of memory, and we do not exactly * populate a single PMD. */ -return mhp_memmap_on_memory () && - size ==memory_block_size_bytes () && -IS_ALIGNED (vmemmap_size,PMD_SIZE ) && -IS_ALIGNED (remaining_size, (pageblock_nr_pages <<PAGE_SHIFT )); +return size ==memory_block_size_bytes () && +IS_ALIGNED (vmemmap_size,PMD_SIZE ) && +IS_ALIGNED (remaining_size, (pageblock_nr_pages <<PAGE_SHIFT )); }bool __weak mhp_supports_memmap_on_memory (unsigned long size)
Signature · 1 line
2.41.0
RFC PATCH 4/5 mm/hotplug: Simplify ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE kconfig
In reply to Aneesh Kumar K.VInstead of adding menu entry with all supported architectures, add mm/Kconfig variable and select the same from supported architectures.
No functional change in this patch.
--- arch/arm64/Kconfig | 4 +--- arch/x86/Kconfig | 4 +--- mm/Kconfig | 3 +++ 3 files changed, 5 insertions(+), 6 deletions(-)
arch/arm64/Kconfig +1 -3
@@ -78,6 +78,7 @@ config ARM64 select ARCH_INLINE_SPIN_UNLOCK_IRQ if !PREEMPTION select ARCH_INLINE_SPIN_UNLOCK_IRQRESTORE if !PREEMPTION select ARCH_KEEP_MEMBLOCK + select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE select ARCH_USE_CMPXCHG_LOCKREF select ARCH_USE_GNU_PROPERTY select ARCH_USE_MEMTEST
@@ -338,9 +339,6 @@ config GENERIC_CSUM config GENERIC_CALIBRATE_DELAY def_bool y -config ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE - def_bool y - config SMP def_bool y
arch/x86/Kconfig +1 -3
@@ -101,6 +101,7 @@ config X86 select ARCH_HAS_DEBUG_WX select ARCH_HAS_ZONE_DMA_SET if EXPERT select ARCH_HAVE_NMI_SAFE_CMPXCHG + select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE select ARCH_MIGHT_HAVE_ACPI_PDC if ACPI select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_MIGHT_HAVE_PC_SERIO
@@ -2656,9 +2657,6 @@ config ARCH_HAS_ADD_PAGES def_bool y depends on ARCH_ENABLE_MEMORY_HOTPLUG -config ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE - def_bool y - menu "Power management and ACPI options" config ARCH_HIBERNATION_HEADER
mm/Kconfig +3 -0
@@ -544,6 +544,9 @@ config MHP_MEMMAP_ON_MEMORY depends on MEMORY_HOTPLUG && SPARSEMEM_VMEMMAP depends on ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE +config ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE + bool + endif # MEMORY_HOTPLUG # Heavily threaded applications may benefit from splitting the mm-wide
Signature · 1 line
2.41.0
RFC PATCH 5/5 powerpc/book3s64/memhotplug: Enable memmap on memory for radix
In reply to Aneesh Kumar K.VRadix vmemmap mapping can map things correctly at the PMD level or PTE level based on different device boundary checks. We also use altmap.reserve feature to align things correctly at pageblock granularity. We can end up loosing some pages in memory with this. For ex: with 256MB memory block size, we require 4 pages to map vmemmap pages, In order to align things correctly we end up adding a reserve of 28 pages. ie, for every 4096 pages 28 pages get reserved.
--- arch/powerpc/Kconfig | 1 + arch/powerpc/mm/book3s64/radix_pgtable.c | 28 +++++++++++++++++++ .../platforms/pseries/hotplug-memory.c | 4 ++- 3 files changed, 32 insertions(+), 1 deletion(-)
arch/powerpc/Kconfig +1 -0
@@ -158,6 +158,7 @@ config PPC select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_HAVE_NMI_SAFE_CMPXCHG select ARCH_KEEP_MEMBLOCK + select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE if PPC_RADIX_MMU select ARCH_MIGHT_HAVE_PC_PARPORT select ARCH_MIGHT_HAVE_PC_SERIO select ARCH_OPTIONAL_KERNEL_RWX if ARCH_HAS_STRICT_KERNEL_RWX
arch/powerpc/mm/book3s64/radix_pgtable.c +28 -0
@@ -1692,3 +1692,31 @@ int pmd_free_pte_page(pmd_t *pmd, unsigned long addr)return 1 ; } + +/* +* mm/memory_hotplug.c:mhp_supports_memmap_on_memory goes into details +* some of the restrictions. We don't check for PMD_SIZE because our +* vmemmap allocation code can fallback correctly. The pageblock +* alignment requirement is met using altmap->reserve blocks. +*/ +bool mhp_supports_memmap_on_memory (unsigned long size) +{ +if (!radix_enabled ()) +return false; +/* +* The pageblock alignment requirement is met by using +* reserve blocks in altmap. +*/ +return size ==memory_block_size_bytes (); +} + +unsigned long memory_block_align_base (struct resource *res) +{ +unsigned long base_pfn =PHYS_PFN (res->start); +unsigned long align, size =resource_size (res); +unsigned long nr_vmemmap_pages = size /PAGE_SIZE ; +unsigned long vmemmap_size = (nr_vmemmap_pages *sizeof (struct page ))/PAGE_SIZE ; + + align =pageblock_align (base_pfn + vmemmap_size) - (base_pfn + vmemmap_size); +return align; +}
arch/powerpc/platforms/pseries/hotplug-memory.c +3 -1
@@ -617,6 +617,7 @@ static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)static int dlpar_add_lmb (struct drmem_lmb *lmb) { +mhp_t mhp_flags =MHP_NONE ;unsigned long block_sz;int nid, rc;
@@ -637,7 +638,8 @@ static int dlpar_add_lmb(struct drmem_lmb *lmb) nid = first_online_node;/* Add the memory */ - rc =__add_memory (nid, lmb->base_addr, block_sz,MHP_NONE ); + mhp_flags |=get_memmap_on_memory_flags (); + rc =__add_memory (nid, lmb->base_addr, block_sz, mhp_flags);if (rc) {invalidate_lmb_associativity_index (lmb);return rc;
Signature · 1 line
2.41.0