mm, memory_hotplug: allocate memmap from hotadded memory
Subsystems (13)
Oscar Salvador
maintainerv2 cover mm, memory_hotplug: allocate memmap from hotadded memory
Hi,
this is the v2 of the first RFC I sent back then in October [1]. In this new version I tried to reduce the complexity as much as possible, plus some clean ups.
[Testing]
I have tested it on "x86_64" (small/big memblocks) and on "powerpc". On both architectures hot-add/hot-remove online/offline operations worked as expected using vmemmap pages, I have not seen any issues so far. I wanted to try it out on Hyper-V/Xen, but I did not manage to. I plan to do so along this week (if time allows).
I would also like to test it on arm64, but I am not sure I can grab an arm64 box anytime soon.
[Coverletter]:
This is another step to make the memory hotplug more usable. The primary goal of this patchset is to reduce memory overhead of the hot added memory (at least for SPARSE_VMEMMAP memory model). The current way we use to populate memmap (struct page array) has two main drawbacks:
a) it consumes an additional memory until the hotadded memory itself is onlined and
b) memmap might end up on a different numa node which is especially true for movable_node configuration.
a) is problem especially for memory hotplug based memory "ballooning" solutions when the delay between physical memory hotplug and the onlining can lead to OOM and that led to introduction of hacks like auto onlining (see 31bc3858ea3e ("memory-hotplug: add automatic onlining policy for the newly added memory")).
b) can have performance drawbacks.
I have also seen hot-add operations failing on powerpc due to the fact that we try to use order-8 pages when populating the memmap array. Given 64KB base pagesize, that is 16MB.
If we run out of those, we just fail the operation and we cannot add more memory.
We could fallback to base pages as x86_64 does, but we can do better.
One way to mitigate all these issues is to simply allocate memmap array (which is the largest memory footprint of the physical memory hotplug) from the hotadded memory itself. VMEMMAP memory model allows us to map any pfn range so the memory doesn't need to be online to be usable for the array. See patch 3 for more details. In short I am reusing an existing vmem_altmap which wants to achieve the same thing for nvdim device memory.
There is also one potential drawback, though. If somebody uses memory hotplug for 1G (gigantic) hugetlb pages then this scheme will not work for them obviously because each memory block will contain reserved area. Large x86 machines will use 2G memblocks so at least one 1G page will be available but this is still not 2G...
I am not really sure somebody does that and how reliable that can work actually. Nevertheless, I _believe_ that onlining more memory into virtual machines is much more common usecase. Anyway if there ever is a strong demand for such a usecase we have basically 3 options a) enlarge memory blocks even more b) enhance altmap allocation strategy and reuse low memory sections to host memmaps of other sections on the same NUMA node c) have the memmap allocation strategy configurable to fallback to the current allocation.
[Overall design]:
Let us say we hot-add 2GB of memory on a x86_64 (memblock size = 128M). That is:
- 16 sections
- 524288 pages
- 8192 vmemmap pages (out of those 524288. We spend 512 pages for each section)
The range of pages is: 0xffffea0004000000 - 0xffffea0006000000 The vmemmap range is: 0xffffea0004000000 - 0xffffea0004080000
0xffffea0004000000 is the head vmemmap page (first page), while all the others are "tails".
We keep the following information in it:
- Head page:
- head->_refcount: number of sections
- head->private : number of vmemmap pages
- Tail page:
- tail->freelist : pointer to the head
This is done because it eases the work in cases where we have to compute the number of vmemmap pages to know how much do we have to skip etc, and to keep the right accounting to present_pages.
When we want to hot-remove the range, we need to be careful because the first pages of that range, are used for the memmap maping, so if we remove those first, we would blow up while accessing the others later on.
For that reason we keep the number of sections in head->_refcount, to know how much do we have to defer the free up.
Since in a hot-remove operation, sections are being removed sequentially, the approach taken here is that every time we hit free_section_memmap(), we decrease the refcount of the head.
When it reaches 0, we know that we hit the last section, so we call vmemmap_free() for the whole memory-range in backwards, so we make sure that the pages used for the mapping will be latest to be freed up.
The accounting is as follows:
Vmemmap pages are charged to spanned/present_paged, but not to manages_pages.
I yet have to check a couple of things like creating an accounting item like VMEMMAP_PAGES to show in /proc/meminfo to ease to spot the memory that went in there, testing Hyper-V/Xen to see how they react to the fact that we are using the beginning of the memory-range for our own purposes, and to check the thing about gigantic pages + hotplug.
I also have to check that there is no compilation/runtime errors when CONFIG_SPARSEMEM but !CONFIG_SPARSEMEM_VMEMMAP.
But before that, I would like to get people's feedback about the overall design, and ideas/suggestions.
[1] https://patchwork.kernel.org/cover/10685835/
Michal Hocko (3):
mm, memory_hotplug: cleanup memory offline path
mm, memory_hotplug: provide a more generic restrictions for memory
hotplug
mm, sparse: rename kmalloc_section_memmap, __kfree_section_memmap
Oscar Salvador (1):
mm, memory_hotplug: allocate memmap from the added memory range for
sparse-vmemmap
arch/arm64/mm/mmu.c | 10 ++- arch/ia64/mm/init.c | 5 +- arch/powerpc/mm/init_64.c | 7 ++ arch/powerpc/mm/mem.c | 6 +- arch/s390/mm/init.c | 12 ++- arch/sh/mm/init.c | 6 +- arch/x86/mm/init_32.c | 6 +- arch/x86/mm/init_64.c | 20 +++-- drivers/hv/hv_balloon.c | 1 + drivers/xen/balloon.c | 1 + include/linux/memory_hotplug.h | 42 ++++++++-- include/linux/memremap.h | 2 +- include/linux/page-flags.h | 23 +++++ kernel/memremap.c | 9 +- mm/compaction.c | 8 ++ mm/memory_hotplug.c | 186 +++++++++++++++++++++++++++++------------ mm/page_alloc.c | 47 ++++++++++- mm/page_isolation.c | 13 +++ mm/sparse.c | 124 +++++++++++++++++++++++++-- mm/util.c | 2 + 20 files changed, 431 insertions(+), 99 deletions(-)
Signature · 1 line
2.13.7
Oscar Salvador
maintainerRFC PATCH v2 1/4 mm, memory_hotplug: cleanup memory offline path
From: Michal Hocko <[email protected]>
check_pages_isolated_cb currently accounts the whole pfn range as being offlined if test_pages_isolated suceeds on the range. This is based on the assumption that all pages in the range are freed which is currently the case in most cases but it won't be with later changes. I haven't double checked but if the range contains invalid pfns we could theoretically over account and underflow zone's managed pages.
Move the offlined pages counting to offline_isolated_pages_cb and rely on __offline_isolated_pages to return the correct value. check_pages_isolated_cb will still do it's primary job and check the pfn range.
While we are at it remove check_pages_isolated and offline_isolated_pages and use directly walk_system_ram_range as do in online_pages.
--- include/linux/memory_hotplug.h | 2 +- mm/memory_hotplug.c | 45 +++++++++++------------------------------- mm/page_alloc.c | 11 +++++++++-- 3 files changed, 22 insertions(+), 36 deletions(-)
include/linux/memory_hotplug.h +1 -1
@@ -85,7 +85,7 @@ extern int add_one_highpage(struct page *page, int pfn, int bad_ppro);extern int online_pages (unsigned long ,unsigned long ,int );extern int test_pages_in_a_zone (unsigned long start_pfn,unsigned long end_pfn,unsigned long *valid_start,unsigned long *valid_end); -extern void __offline_isolated_pages (unsigned long ,unsigned long ); +extern unsigned long __offline_isolated_pages (unsigned long ,unsigned long );typedef int (*online_page_callback_t )(struct page *page,unsigned int order);
mm/memory_hotplug.c +12 -33
75 lines not on this page · raw message
mm/page_alloc.c +9 -2
@@ -8479,7 +8479,7 @@ void zone_pcp_reset(struct zone *zone)* All pages in the range must be in a single zone and isolated * before calling this. */ -void +unsigned long __offline_isolated_pages (unsigned long start_pfn,unsigned long end_pfn) {struct page *page;
@@ -8487,12 +8487,15 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)unsigned int order, i;unsigned long pfn;unsigned long flags; +unsigned long offlined_pages =0 ; +/* find the first valid pfn */ for (pfn = start_pfn; pfn < end_pfn; pfn++)if (pfn_valid (pfn))break ;if (pfn == end_pfn) -return ; +return offlined_pages; +offline_mem_sections (pfn, end_pfn); zone =page_zone (pfn_to_page (pfn));spin_lock_irqsave (&zone->lock, flags);
@@ -8510,12 +8513,14 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)if (unlikely (!PageBuddy (page) &&PageHWPoison (page))) { pfn++;SetPageReserved (page); + offlined_pages++;continue ; }BUG_ON (page_count (page));BUG_ON (!PageBuddy (page)); order =page_order (page); + offlined_pages +=1 << order;#ifdef CONFIG_DEBUG_VM pr_info ("remove from free list %lx %d %lx\n" , pfn,1 << order, end_pfn);
@@ -8528,6 +8533,8 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) pfn += (1 << order); }spin_unlock_irqrestore (&zone->lock, flags); + +return offlined_pages; }#endif
Signature · 1 line
2.13.7
Oscar Salvador
maintainerRFC PATCH v2 2/4 mm, memory_hotplug: provide a more generic restrictions for memory hotplug
In reply to Oscar SalvadorFrom: Michal Hocko <[email protected]>
arch_add_memory, __add_pages take a want_memblock which controls whether the newly added memory should get the sysfs memblock user API (e.g. ZONE_DEVICE users do not want/need this interface). Some callers even want to control where do we allocate the memmap from by configuring altmap.
Add a more generic hotplug context for arch_add_memory and __add_pages. struct mhp_restrictions contains flags which contains additional features to be enabled by the memory hotplug (MHP_MEMBLOCK_API currently) and altmap for alternative memmap allocator.
Please note that the complete altmap propagation down to vmemmap code is still not done in this patch. It will be done in the follow up to reduce the churn here.
This patch shouldn't introduce any functional change.
--- arch/arm64/mm/mmu.c | 5 ++--- arch/ia64/mm/init.c | 5 ++--- arch/powerpc/mm/mem.c | 6 +++--- arch/s390/mm/init.c | 6 +++--- arch/sh/mm/init.c | 6 +++--- arch/x86/mm/init_32.c | 6 +++--- arch/x86/mm/init_64.c | 10 +++++----- include/linux/memory_hotplug.h | 25 +++++++++++++++++++------ kernel/memremap.c | 9 ++++++--- mm/memory_hotplug.c | 10 ++++++---- 10 files changed, 52 insertions(+), 36 deletions(-)
arch/arm64/mm/mmu.c +2 -3
@@ -1049,8 +1049,7 @@ int p4d_free_pud_page(p4d_t *p4d, unsigned long addr) }#ifdef CONFIG_MEMORY_HOTPLUG -int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int arch_add_memory (int nid,u64 start,u64 size,struct mhp_restrictions *restrictions) {int flags =0 ;
@@ -1061,6 +1060,6 @@ int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap, size, PAGE_KERNEL, pgd_pgtable_alloc, flags);return __add_pages (nid, start >>PAGE_SHIFT , size >>PAGE_SHIFT , - altmap, want_memblock); + restrictions); }#endif
arch/ia64/mm/init.c +2 -3
@@ -644,14 +644,13 @@ mem_init (void) }#ifdef CONFIG_MEMORY_HOTPLUG -int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int arch_add_memory (int nid,u64 start,u64 size,struct mhp_restrictions *restrictions) {unsigned long start_pfn = start >>PAGE_SHIFT ;unsigned long nr_pages = size >>PAGE_SHIFT ;int ret; - ret =__add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); + ret =__add_pages (nid, start_pfn, nr_pages, restrictions);if (ret)printk ("%s: Problem encountered in __add_pages() as ret=%d\n" , __func__, ret);
arch/powerpc/mm/mem.c +3 -3
@@ -117,8 +117,8 @@ int __weak remove_section_mapping(unsigned long start, unsigned long end)return -ENODEV ; } -int __meminit arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int __meminit arch_add_memory (int nid,u64 start,u64 size, +struct mhp_restrictions *restrictions) {unsigned long start_pfn = start >>PAGE_SHIFT ;unsigned long nr_pages = size >>PAGE_SHIFT ;
@@ -135,7 +135,7 @@ int __meminit arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap * }flush_inval_dcache_range (start, start + size); -return __add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); +return __add_pages (nid, start_pfn, nr_pages, restrictions); }#ifdef CONFIG_MEMORY_HOTREMOVE
arch/s390/mm/init.c +3 -3
@@ -224,8 +224,8 @@ device_initcall(s390_cma_mem_init);#endif /* CONFIG_CMA */ -int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int arch_add_memory (int nid,u64 start,u64 size, +struct mhp_restrictions *restrictions) {unsigned long start_pfn =PFN_DOWN (start);unsigned long size_pages =PFN_DOWN (size);
@@ -235,7 +235,7 @@ int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,if (rc)return rc; - rc =__add_pages (nid, start_pfn, size_pages, altmap, want_memblock); + rc =__add_pages (nid, start_pfn, size_pages, restrictions);if (rc)vmem_remove_mapping (start, size);return rc;
arch/sh/mm/init.c +3 -3
@@ -410,15 +410,15 @@ void free_initrd_mem(unsigned long start, unsigned long end)#endif #ifdef CONFIG_MEMORY_HOTPLUG -int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int arch_add_memory (int nid,u64 start,u64 size, +struct mhp_restrictions *restrictions) {unsigned long start_pfn =PFN_DOWN (start);unsigned long nr_pages = size >>PAGE_SHIFT ;int ret;/* We only have ZONE_NORMAL, so this is easy.. */ - ret =__add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); + ret =__add_pages (nid, start_pfn, nr_pages, restrictions);if (unlikely (ret))printk ("%s: Failed, __add_pages() == %d\n" , __func__, ret);
arch/x86/mm/init_32.c +3 -3
@@ -850,13 +850,13 @@ void __init mem_init(void) }#ifdef CONFIG_MEMORY_HOTPLUG -int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int arch_add_memory (int nid,u64 start,u64 size, +struct mhp_restrictions *restrictions) {unsigned long start_pfn = start >>PAGE_SHIFT ;unsigned long nr_pages = size >>PAGE_SHIFT ; -return __add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); +return __add_pages (nid, start_pfn, nr_pages, restrictions); }#ifdef CONFIG_MEMORY_HOTREMOVE
arch/x86/mm/init_64.c +5 -5
@@ -777,11 +777,11 @@ static void update_end_of_memory_vars(u64 start, u64 size) }int add_pages (int nid,unsigned long start_pfn,unsigned long nr_pages, -struct vmem_altmap *altmap,bool want_memblock) +struct mhp_restrictions *restrictions) {int ret; - ret =__add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); + ret =__add_pages (nid, start_pfn, nr_pages, restrictions);WARN_ON_ONCE (ret);/* update max_pfn, max_low_pfn and high_memory */
@@ -791,15 +791,15 @@ int add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,return ret; } -int arch_add_memory (int nid,u64 start,u64 size,struct vmem_altmap *altmap, -bool want_memblock) +int arch_add_memory (int nid,u64 start,u64 size, +struct mhp_restrictions *restrictions) {unsigned long start_pfn = start >>PAGE_SHIFT ;unsigned long nr_pages = size >>PAGE_SHIFT ;init_memory_mapping (start, start + size); -return add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); +return add_pages (nid, start_pfn, nr_pages, restrictions); }#define PAGE_INUSE 0xFD
include/linux/memory_hotplug.h +19 -6
@@ -113,20 +113,33 @@ extern int __remove_pages(struct zone *zone, unsigned long start_pfn, unsigned long nr_pages, struct vmem_altmap *altmap);#endif /* CONFIG_MEMORY_HOTREMOVE */ +/* +* Do we want sysfs memblock files created. This will allow userspace to online +* and offline memory explicitly. Lack of this bit means that the caller has to +* call move_pfn_range_to_zone to finish the initialization. +*/ + +#define MHP_MEMBLOCK_API 1<<0 + +/* Restrictions for the memory hotplug */ +struct mhp_restrictions { +unsigned long flags;/* MHP_ flags */ +struct vmem_altmap *altmap;/* use this alternative allocator for memmaps */ +}; +/* reasonably generic interface to expand the physical pages */ extern int __add_pages (int nid,unsigned long start_pfn,unsigned long nr_pages, -struct vmem_altmap *altmap,bool want_memblock); +struct mhp_restrictions *restrictions);#ifndef CONFIG_ARCH_HAS_ADD_PAGES static inline int add_pages (int nid,unsigned long start_pfn, -unsigned long nr_pages,struct vmem_altmap *altmap, -bool want_memblock) +unsigned long nr_pages,struct mhp_restrictions *restrictions) { -return __add_pages (nid, start_pfn, nr_pages, altmap, want_memblock); +return __add_pages (nid, start_pfn, nr_pages, restrictions); }#else /* ARCH_HAS_ADD_PAGES */ int add_pages (int nid,unsigned long start_pfn,unsigned long nr_pages, -struct vmem_altmap *altmap,bool want_memblock); +struct mhp_restrictions *restrictions);#endif /* ARCH_HAS_ADD_PAGES */ #ifdef CONFIG_NUMA
@@ -328,7 +341,7 @@ extern int __add_memory(int nid, u64 start, u64 size);extern int add_memory (int nid,u64 start,u64 size);extern int add_memory_resource (int nid,struct resource *resource);extern int arch_add_memory (int nid,u64 start,u64 size, -struct vmem_altmap *altmap,bool want_memblock); +struct mhp_restrictions *restrictions);extern void move_pfn_range_to_zone (struct zone *zone,unsigned long start_pfn,unsigned long nr_pages,struct vmem_altmap *altmap);extern bool is_memblock_offlined (struct memory_block *mem);
kernel/memremap.c +6 -3
@@ -149,6 +149,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)struct resource *res = &pgmap->res;struct dev_pagemap *conflict_pgmap;pgprot_t pgprot =PAGE_KERNEL ; +struct mhp_restrictions restrictions = {};int error, nid, is_ram;if (!pgmap->ref || !pgmap->kill)
@@ -199,6 +200,9 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)if (error) goto err_pfn_remap; +/* We do not want any optional features only our own memmap */ + restrictions.altmap = altmap; +mem_hotplug_begin ();/*
@@ -214,7 +218,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)*/ if (pgmap->type ==MEMORY_DEVICE_PRIVATE ) { error =add_pages (nid, align_start >>PAGE_SHIFT , - align_size >>PAGE_SHIFT ,NULL , false); + align_size >>PAGE_SHIFT , &restrictions); }else { error =kasan_add_zero_shadow (__va (align_start), align_size);if (error) {
@@ -222,8 +226,7 @@ void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap) goto err_kasan; } - error =arch_add_memory (nid, align_start, align_size, altmap, - false); + error =arch_add_memory (nid, align_start, align_size, &restrictions); }if (!error) {
mm/memory_hotplug.c +6 -4
@@ -271,12 +271,12 @@ static int __meminit __add_section(int nid, unsigned long phys_start_pfn,* add the new pages. */ int __ref __add_pages (int nid,unsigned long phys_start_pfn, -unsigned long nr_pages,struct vmem_altmap *altmap, -bool want_memblock) +unsigned long nr_pages,struct mhp_restrictions *restrictions) {unsigned long i;int err =0 ;int start_sec, end_sec; +struct vmem_altmap *altmap = restrictions->altmap;/* during initialize mem_map, align hot-added range to section */ start_sec =pfn_to_section_nr (phys_start_pfn);
@@ -297,7 +297,7 @@ int __ref __add_pages(int nid, unsigned long phys_start_pfn,for (i = start_sec; i <= end_sec; i++) { err =__add_section (nid,section_nr_to_pfn (i), altmap, - want_memblock); + restrictions->flags &MHP_MEMBLOCK_API );/* * EEXIST is finally dealt with by ioresource collision
@@ -1108,6 +1108,7 @@ int __ref add_memory_resource(int nid, struct resource *res)u64 start, size;bool new_node = false;int ret; +struct mhp_restrictions restrictions = {}; start = res->start; size =resource_size (res);
@@ -1132,7 +1133,8 @@ int __ref add_memory_resource(int nid, struct resource *res) new_node = ret;/* call arch's memory hotadd */ - ret =arch_add_memory (nid, start, size,NULL , true); + restrictions.flags =MHP_MEMBLOCK_API ; + ret =arch_add_memory (nid, start, size, &restrictions);if (ret <0 ) goto error;
Signature · 1 line
2.13.7
Oscar Salvador
maintainerRFC PATCH v2 3/4 mm, memory_hotplug: allocate memmap from the added memory range for sparse-vmemmap
In reply to Oscar SalvadorPhysical memory hotadd has to allocate a memmap (struct page array) for the newly added memory section. Currently, alloc_pages_node() is used for those allocations.
This has some disadvantages:
a) an existing memory is consumed for that purpose
(~2MB per 128MB memory section on x86_64)
b) if the whole node is movable then we have off-node struct pages which has performance drawbacks.
a) has turned out to be a problem for memory hotplug based ballooning because the userspace might not react in time to online memory while the memory consumed during physical hotadd consumes enough memory to push system to OOM. 31bc3858ea3e ("memory-hotplug: add automatic onlining policy for the newly added memory") has been added to workaround that problem.
I have also seen hot-add operations failing on powerpc due to the fact that we try to use order-8 pages. If the base page size is 64KB, this gives us 16MB, and if we run out of those, we simply fail. One could arge that we can fall back to basepages as we do in x86_64.
But We can do much better when CONFIG_SPARSEMEM_VMEMMAP=y because vmemap page tables can map arbitrary memory. That means that we can simply use the beginning of each memory section and map struct pages there. struct pages which back the allocated space then just need to be treated carefully.
Add {_Set,_Clear}PageVmemmap helpers to distinguish those pages in pfn walkers. We do not have any spare page flag for this purpose so use the combination of PageReserved bit which already tells that the page should be ignored by the core mm code and store VMEMMAP_PAGE (which sets all bits but PAGE_MAPPING_FLAGS) into page->mapping.
On the memory hotplug front add a new MHP_MEMMAP_FROM_RANGE restriction flag. User is supposed to set the flag if the memmap should be allocated from the hotadded range. Please note that this is just a hint and architecture code can veto this if this cannot be supported. E.g. s390 cannot support this currently beause the physical memory range is made accessible only during memory online.
Implementation wise we reuse vmem_altmap infrastructure to override the default allocator used by __vmemap_populate. Once the memmap is allocated we need a way to mark altmap pfns used for the allocation. For this, we define a init() and a constructor() callback in mhp_restrictions structure.
Init() points now to init_altmap_memmap(), and constructor() to mark_vmemmap_pages().
init_altmap_memmap() takes care of checking the flags, and inits the vmemap_altmap structure with the required fields. mark_vmemmap_pages() takes care of marking the pages as Vmemmap, and inits some fields we need.
The current layout of the Vmemmap pages are:
- There is a head Vmemmap (first page), which has the following fields set:
* page->_refcount: number of sections that used this altmap
* page->private: total number of vmemmap pages
- The remaining vmemmap pages have:
* page->freelist: pointer to the head vmemmap page
This is done to easy the computation we need in some places.
So, let us say we hot-add 9GB on x86_64:
head->_refcount = 72 sectionshead->private = 36864 vmemmap pages
tail's->freelist = head
We keep a _refcount of the used sections to know how much do we have to defer the call to vmemmap_free().
The thing is that the first pages of the hot-added range are used to create the memmap mapping, so we cannot remove those first, otherwise we would blow up.
What we do is that since when we hot-remove a memory-range, sections are being removed sequentially, we wait until we hit the last section, and then we free the hole range to vmemmap_free backwards.
We know that it is the last section because in every pass we decrease head->_refcount, and when it reaches 0, we got our last section.
We also have to be careful about those pages during online and offline operations. They are simply skipped now so online will keep them reserved and so unusable for any other purpose and offline ignores them so they do not block the offline operation.
Please note that only the memory hotplug is currently using this allocation scheme. The boot time memmap allocation could use the same trick as well but this is not done yet.
--- arch/arm64/mm/mmu.c | 5 +- arch/powerpc/mm/init_64.c | 7 +++ arch/s390/mm/init.c | 6 ++ arch/x86/mm/init_64.c | 10 ++++ drivers/hv/hv_balloon.c | 1 + drivers/xen/balloon.c | 1 + include/linux/memory_hotplug.h | 23 +++++-- include/linux/memremap.h | 2 +- include/linux/page-flags.h | 23 +++++++ mm/compaction.c | 8 +++ mm/memory_hotplug.c | 133 ++++++++++++++++++++++++++++++++++++----- mm/page_alloc.c | 36 ++++++++++- mm/page_isolation.c | 13 ++++ mm/sparse.c | 108 +++++++++++++++++++++++++++++++++ mm/util.c | 2 + 15 files changed, 354 insertions(+), 24 deletions(-)
arch/arm64/mm/mmu.c +4 -1
@@ -749,7 +749,10 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,if (pmd_none (READ_ONCE (*pmdp))) {void *p =NULL ; - p =vmemmap_alloc_block_buf (PMD_SIZE , node); +if (altmap) + p =altmap_alloc_block_buf (PMD_SIZE , altmap); +else + p =vmemmap_alloc_block_buf (PMD_SIZE , node);if (!p)return -ENOMEM ;
arch/powerpc/mm/init_64.c +7 -0
@@ -296,6 +296,13 @@ void __ref vmemmap_free(unsigned long start, unsigned long end,if (base_pfn >= alt_start && base_pfn < alt_end) {vmem_altmap_free (altmap, nr_pages); + }else if (PageVmemmap (page)) { +/* +* runtime vmemmap pages are residing inside the memory +* section so they do not have to be freed anywhere. +*/ +while (PageVmemmap (page)) +__ClearPageVmemmap (page++); }else if (PageReserved (page)) {/* allocated from bootmem */ if (page_size <PAGE_SIZE ) {
arch/s390/mm/init.c +6 -0
@@ -231,6 +231,12 @@ int arch_add_memory(int nid, u64 start, u64 size,unsigned long size_pages =PFN_DOWN (size);int rc; +/* +* Physical memory is added only later during the memory online so we +* cannot use the added range at this stage unfortunately. +*/ + restrictions->flags &= ~MHP_MEMMAP_FROM_RANGE ; + rc =vmem_add_mapping (start, size);if (rc)return rc;
arch/x86/mm/init_64.c +10 -0
@@ -809,6 +809,16 @@ static void __meminit free_pagetable(struct page *page, int order)unsigned long magic;unsigned int nr_pages =1 << order; +/* +* runtime vmemmap pages are residing inside the memory section so +* they do not have to be freed anywhere. +*/ +if (PageVmemmap (page)) { +while (nr_pages--) +__ClearPageVmemmap (page++); +return ; + } +/* bootmem page has reserved flag */ if (PageReserved (page)) {__ClearPageReserved (page);
drivers/hv/hv_balloon.c +1 -0
@@ -1585,6 +1585,7 @@ static int balloon_probe(struct hv_device *dev,#ifdef CONFIG_MEMORY_HOTPLUG do_hot_add = hot_add; + hotplug_vmemmap_enabled = false;#else do_hot_add = false;#endif
drivers/xen/balloon.c +1 -0
@@ -715,6 +715,7 @@ static int __init balloon_init(void)set_online_page_callback (&xen_online_pages);register_memory_notifier (&xen_memory_nb);register_sysctl_table (xen_root); + hotplug_vmemmap_enabled = false;#endif #ifdef CONFIG_XEN_PV
include/linux/memory_hotplug.h +19 -4
@@ -118,13 +118,27 @@ extern int __remove_pages(struct zone *zone, unsigned long start_pfn,* and offline memory explicitly. Lack of this bit means that the caller has to * call move_pfn_range_to_zone to finish the initialization. */ -#define MHP_MEMBLOCK_API 1<<0 -/* Restrictions for the memory hotplug */ +/* +* Do we want memmap (struct page array) allocated from the hotadded range. +* Please note that only SPARSE_VMEMMAP implements this feature and some +* architectures might not support it even for that memory model (e.g. s390) +*/ +#define MHP_MEMMAP_FROM_RANGE 1<<1 + +/* Restrictions for the memory hotplug +* flags: MHP_ flags +* altmap: use this alternative allocator for memmaps +* init: callback to be called before we add this memory +* constructor: callback to be called once the more has been added +*/ struct mhp_restrictions { -unsigned long flags;/* MHP_ flags */ -struct vmem_altmap *altmap;/* use this alternative allocator for memmaps */ +unsigned long flags; +struct vmem_altmap *altmap; +void (*init)(unsigned long ,unsigned long ,struct vmem_altmap *, +struct mhp_restrictions *); +void (*constructor)(struct vmem_altmap *,struct mhp_restrictions *); };/* reasonably generic interface to expand the physical pages */
@@ -345,6 +359,7 @@ extern int arch_add_memory(int nid, u64 start, u64 size,extern void move_pfn_range_to_zone (struct zone *zone,unsigned long start_pfn,unsigned long nr_pages,struct vmem_altmap *altmap);extern bool is_memblock_offlined (struct memory_block *mem); +extern void mark_vmemmap_pages (struct vmem_altmap *self,struct mhp_restrictions *r);extern int sparse_add_one_section (int nid,unsigned long start_pfn,struct vmem_altmap *altmap); extern void sparse_remove_one_section(struct zone *zone, struct mem_section *ms,
include/linux/memremap.h +1 -1
@@ -16,7 +16,7 @@ struct device;* @alloc: track pages consumed, private to vmemmap_populate() */ struct vmem_altmap { -const unsigned long base_pfn; +unsigned long base_pfn;const unsigned long reserve;unsigned long free;unsigned long align;
include/linux/page-flags.h +23 -0
@@ -437,6 +437,29 @@ static __always_inline int __PageMovable(struct page *page)PAGE_MAPPING_MOVABLE ; } +#define VMEMMAP_PAGE ~PAGE_MAPPING_FLAGS +static __always_inline int PageVmemmap(struct page *page) +{ +return PageReserved (page) && (unsigned long )page->mapping ==VMEMMAP_PAGE ; +} + +static __always_inline void __ClearPageVmemmap(struct page *page) +{ +__ClearPageReserved (page); + page->mapping =NULL ; +} + +static __always_inline void __SetPageVmemmap(struct page *page) +{ +__SetPageReserved (page); + page->mapping = (void *)VMEMMAP_PAGE ; +} + +static __always_inline struct page *vmemmap_get_head(struct page *page) +{ +return (struct page *)page->freelist; +} +#ifdef CONFIG_KSM /* * A KSM page is one of those write-protected "shared pages" or "merged pages"
mm/compaction.c +8 -0
@@ -852,6 +852,14 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, page =pfn_to_page (low_pfn);/* +* Vmemmap pages are pages that are used for creating the memmap +* array mapping, and they reside in their hot-added memory range. +* Therefore, we cannot migrate them. +*/ +if (PageVmemmap (page)) + goto isolate_fail; + +/* * Check if the pageblock has already been marked skipped. * Only the aligned PFN is checked as the caller isolates * COMPACT_CLUSTER_MAX at a time so the second call must
mm/memory_hotplug.c +117 -16
226 lines not on this page · raw message
mm/page_alloc.c +34 -2
@@ -1257,14 +1257,19 @@ static void __meminit __init_struct_page_nolru(struct page *page, unsigned long zone, int nid, bool is_reserved) { -mm_zero_struct_page (page ); + if (!PageVmemmap(page)) { +/* +* Vmemmap pages need to preserve their state. +*/ +mm_zero_struct_page (page ); +init_page_count (page); + }/* * We can use a non-atomic operation for setting the * PG_reserved flag as we are still initializing the pages. */ set_page_links (page, zone, nid, pfn, is_reserved); -init_page_count (page);page_mapcount_reset (page);page_cpupid_reset_last (page);page_kasan_tag_reset (page);
@@ -8138,6 +8143,19 @@ bool has_unmovable_pages(struct zone *zone, struct page *page, int count, page =pfn_to_page (check); +/* +* Vmemmap pages are marked as reserved, so skip them here, +* otherwise the check below will drive us to a bad conclusion. +*/ +if (PageVmemmap (page)) { +struct page *head =vmemmap_get_head (page); +unsigned int skip_pages; + + skip_pages =page_private (head) - (page - head); + iter += skip_pages -1 ; +continue ; + } +if (PageReserved (page)) goto unmovable;
@@ -8506,6 +8524,20 @@ __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)continue ; } page =pfn_to_page (pfn); + +/* +* Vmemmap pages are self-hosted in the hot-added range, +* we do not need to free them, so skip them. +*/ +if (PageVmemmap (page)) { +struct page *head =vmemmap_get_head (page); +unsigned long skip_pages; + + skip_pages =page_private (head) - (page - head); + pfn += skip_pages; +continue ; + } +/* * The HWPoisoned page may be not in buddy system, and * page_count() is not 0.
mm/page_isolation.c +13 -0
@@ -155,6 +155,8 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages) page =pfn_to_online_page (pfn + i);if (!page)continue ; +if (PageVmemmap (page)) +continue ;return page; }return NULL ;
@@ -257,6 +259,17 @@ __test_page_isolated_in_pageblock(unsigned long pfn, unsigned long end_pfn,continue ; } page =pfn_to_page (pfn); +if (PageVmemmap (page)) { +/* +* Vmemmap pages are not isolated. Skip them. +*/ +struct page *head =vmemmap_get_head (page); +unsigned long skip_pages; + + skip_pages =page_private (head) - (page - head); + pfn += skip_pages; +continue ; + }if (PageBuddy (page))/* * If the page is on a free list, it has to be on
mm/sparse.c +108 -0
120 lines not on this page · raw message
mm/util.c +2 -0
@@ -517,6 +517,8 @@ struct address_space *page_mapping(struct page *page) mapping = page->mapping;if ((unsigned long )mapping &PAGE_MAPPING_ANON )return NULL ; +if ((unsigned long )mapping ==VMEMMAP_PAGE ) +return NULL ;return (void *)((unsigned long )mapping & ~PAGE_MAPPING_FLAGS ); }
Signature · 1 line
2.13.7
Oscar Salvador
maintainerRFC PATCH v2 4/4 mm, sparse: rename kmalloc_section_memmap, __kfree_section_memmap
In reply to Oscar SalvadorFrom: Michal Hocko <[email protected]>
The sufix "kmalloc" is misleading.
Rename it to alloc_section_memmap/free_section_memmap which better reflects the funcionality.
--- mm/sparse.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
mm/sparse.c +8 -8
@@ -676,13 +676,13 @@ static void free_deferred_vmemmap_range(unsigned long start, in_vmemmap_range = false; } -static inline struct page *kmalloc_section_memmap (unsigned long pnum,int nid, +static inline struct page *alloc_section_memmap (unsigned long pnum,int nid,struct vmem_altmap *altmap) {/* This will make the necessary allocations eventually. */ return sparse_mem_map_populate (pnum, nid, altmap); } -static void __kfree_section_memmap (struct page *memmap, +static void free_section_memmap (struct page *memmap,struct vmem_altmap *altmap) {unsigned long start = (unsigned long )memmap;
@@ -732,13 +732,13 @@ static struct page *__kmalloc_section_memmap(void)return ret; } -static inline struct page *kmalloc_section_memmap (unsigned long pnum,int nid, +static inline struct page *alloc_section_memmap (unsigned long pnum,int nid,struct vmem_altmap *altmap) {return __kmalloc_section_memmap (); } -static void __kfree_section_memmap (struct page *memmap, +static void free_section_memmap (struct page *memmap,struct vmem_altmap *altmap) {if (is_vmalloc_addr (memmap))
@@ -803,12 +803,12 @@ int __meminit sparse_add_one_section(int nid, unsigned long start_pfn,if (ret <0 && ret != -EEXIST )return ret; ret =0 ; - memmap =kmalloc_section_memmap (section_nr, nid, altmap); + memmap =alloc_section_memmap (section_nr, nid, altmap);if (!memmap)return -ENOMEM ; usemap =__kmalloc_section_usemap ();if (!usemap) { -__kfree_section_memmap (memmap, altmap); +free_section_memmap (memmap, altmap);return -ENOMEM ; }
@@ -830,7 +830,7 @@ int __meminit sparse_add_one_section(int nid, unsigned long start_pfn, out:if (ret <0 ) {kfree (usemap); -__kfree_section_memmap (memmap, altmap); +free_section_memmap (memmap, altmap); }return ret; }
@@ -881,7 +881,7 @@ static void free_section_usemap(struct page *memmap, unsigned long *usemap,if (PageSlab (usemap_page) ||PageCompound (usemap_page)) {kfree (usemap);if (memmap) -__kfree_section_memmap (memmap, altmap); +free_section_memmap (memmap, altmap);return ; }
Signature · 1 line
2.13.7
Quoted 43 lines · Oscar Salvador
Hi,
this is the v2 of the first RFC I sent back then in October [1]. In this new version I tried to reduce the complexity as much as possible, plus some clean ups.
[Testing]
I have tested it on "
x86_64" (small/big memblocks) and on "powerpc". On both architectures hot-add/hot-remove online/offline operations worked as expected using vmemmap pages, I have not seen any issues so far. I wanted to try it out on Hyper-V/Xen, but I did not manage to. I plan to do so along this week (if time allows).
I would also like to test it on arm64, but I am not sure I can grab an arm64 box anytime soon.[Coverletter]:
This is another step to make the memory hotplug more usable. The primary goal of this patchset is to reduce memory overhead of the hot added memory (at least for
SPARSE_VMEMMAPmemory model). The current way we use to populate memmap (struct page array) has two main drawbacks:a) it consumes an additional memory until the hotadded memory itself is onlined and
b) memmap might end up on a different numa node which is especially true formovable_nodeconfiguration.a) is problem especially for memory hotplug based memory "ballooning" solutions when the delay between physical memory hotplug and the onlining can lead to OOM and that led to introduction of hacks like auto onlining (see
31bc3858ea3e("memory-hotplug: add automatic onlining policy for the newly added memory")).b) can have performance drawbacks.
I have also seen hot-add operations failing on powerpc due to the fact that we try to use order-8 pages when populating the memmap array. Given 64KB base pagesize, that is 16MB.
If we run out of those, we just fail the operation and we cannot add more memory.
We could fallback to base pages asx86_64does, but we can do better.
Quoted 8 lines
One way to mitigate all these issues is to simply allocate memmap array (which is the largest memory footprint of the physical memory hotplug) from the hotadded memory itself. VMEMMAP memory model allows us to map any pfn range so the memory doesn't need to be online to be usable for the array. See patch 3 for more details. In short I am reusing an existing
vmem_altmapwhich wants to achieve the same thing for nvdim device memory.
I only had a quick glimpse. I would prefer if the caller of add_memory() can specify whether it would be ok to allocate vmmap from the range. This e.g. allows ACPI dimm code to allocate from the range, however other machanisms (XEN, hyper-v, virtio-mem) can allow it once they actually support it.
Also, while s390x standby memory cannot support allocating from the range, virtio-mem could easily support it on s390x.
Not sure how such an interface could look like, but I would really like to have control over that on the add_memory() interface, not per arch.
Signature · 3 lines
Thanks, David / dhildenb
Oscar Salvador
maintainerOn Fri, Jan 25, 2019 at 09:53:35AM +0100, David Hildenbrand wrote:
Hi David,
Quoted 5 lines
I only had a quick glimpse. I would prefer if the caller of
add_memory()can specify whether it would be ok to allocate vmmap from the range. This e.g. allows ACPI dimm code to allocate from the range, however other machanisms (XEN, hyper-v, virtio-mem) can allow it once they actually support it.
Well, I think this can be done, and it might make more sense, as we would get rid of some other flags to prevent allocating vmemmap besides mhp_restrictions.
Quoted 6 lines
Also, while s390x standby memory cannot support allocating from the range, virtio-mem could easily support it on s390x.
Not sure how such an interface could look like, but I would really like to have control over that on the
add_memory()interface, not per arch.
Let me try it out and will report back.
Btw, since you are a virt-guy, would it be do feasible for you to test the patchset on hyper-v, xen or your virtio-mem driver?
Thanks David!
Signature · 2 lines
Oscar Salvador SUSE L3
Quoted 9 lines · Oscar Salvador
On Fri, Jan 25, 2019 at 09:53:35AM +0100, David Hildenbrand wrote:
Hi David,Quoted 5 lines
I only had a quick glimpse. I would prefer if the caller of
add_memory()can specify whether it would be ok to allocate vmmap from the range. This e.g. allows ACPI dimm code to allocate from the range, however other machanisms (XEN, hyper-v, virtio-mem) can allow it once they actually support it.
Quoted 3 lines
Well, I think this can be done, and it might make more sense, as we would get rid of some other flags to prevent allocating vmemmap besides
mhp_restrictions.
Maybe we can also start passing a struct to add_memory() to describe such properties. This would avoid having to change all the layers over and over again. We would just have to establish some rules to avoid breaking stuff. E.g. the struct always has to be initialized to 0 so new features won't break any caller not wanting to make use of that.
E.g. memory block types (or if we come up with something better) would also have to add new parameters to add_memory() and friends.
Quoted 5 lines
Quoted 5 lines
Also, while s390x standby memory cannot support allocating from the range, virtio-mem could easily support it on s390x.
Quoted 7 lines
Quoted 2 lines
Not sure how such an interface could look like, but I would really like to have control over that on the
add_memory()interface, not per arch.Let me try it out and will report back.
Btw, since you are a virt-guy, would it be do feasible for you to test the patchset on hyper-v, xen or your virtio-mem driver?
I don't have a XEN or Hyper-V installation myself. cc-ing Vitaly, maybe he has time end resources to test on Hyper-V.
I'll be reworking my virtio-mem prototype soon and try with this patchset than! But this could take a little bit longer as I have tons of other stuff on my plate :) So don't worry about virtio-mem too much for now.
Quoted 3 lines
Thanks David!
Signature · 3 lines
Thanks, David / dhildenb
Oscar Salvador
maintainerQuoted 9 lines · Oscar Salvador
I yet have to check a couple of things like creating an accounting item like
VMEMMAP_PAGESto show in/proc/meminfoto ease to spot the memory that went in there, testing Hyper-V/Xen to see how they react to the fact that we are using the beginning of the memory-range for our own purposes, and to check the thing about gigantic pages + hotplug.
I also have to check that there is no compilation/runtime errors whenCONFIG_SPARSEMEMbut !CONFIG_SPARSEMEM_VMEMMAP.
But before that, I would like to get people's feedback about the overall design, and ideas/suggestions.
just a friendly reminder if some feedback is possible :-)
Signature · 2 lines
Oscar Salvador SUSE L3
Quoted 11 lines · Oscar Salvador
Quoted 9 lines · Oscar Salvador
I yet have to check a couple of things like creating an accounting item like
VMEMMAP_PAGESto show in/proc/meminfoto ease to spot the memory that went in there, testing Hyper-V/Xen to see how they react to the fact that we are using the beginning of the memory-range for our own purposes, and to check the thing about gigantic pages + hotplug.
I also have to check that there is no compilation/runtime errors whenCONFIG_SPARSEMEMbut !CONFIG_SPARSEMEM_VMEMMAP.
But before that, I would like to get people's feedback about the overall design, and ideas/suggestions.
Quoted 1 line
just a friendly reminder if some feedback is possible :-)
I will be off next week and will not get to this this week.
Signature · 2 lines
Michal Hocko SUSE Labs
Oscar Salvador
maintainerQuoted 12 lines · Michal Hocko
Quoted 11 lines · Oscar Salvador
Quoted 9 lines · Oscar Salvador
I yet have to check a couple of things like creating an accounting item like
VMEMMAP_PAGESto show in/proc/meminfoto ease to spot the memory that went in there, testing Hyper-V/Xen to see how they react to the fact that we are using the beginning of the memory-range for our own purposes, and to check the thing about gigantic pages + hotplug.
I also have to check that there is no compilation/runtime errors whenCONFIG_SPARSEMEMbut !CONFIG_SPARSEMEM_VMEMMAP.
But before that, I would like to get people's feedback about the overall design, and ideas/suggestions.
Quoted 3 lines
Quoted 1 line
just a friendly reminder if some feedback is possible :-)
I will be off next week and will not get to this this week.
Sure, it can wait.
In the meantime I will take the chance to clean up a couple of things.
Thanks
Signature · 2 lines
Oscar Salvador SUSE L3
Quoted 8 lines · On Tue, 22 Jan 2019 11:37:04 +0100 Oscar Salvador <[email protected]> wrote:
Hi,
this is the v2 of the first RFC I sent back then in October [1]. In this new version I tried to reduce the complexity as much as possible, plus some clean ups.
[Testing]
Quoted 7 lines
I have tested it on "
x86_64" (small/big memblocks) and on "powerpc". On both architectures hot-add/hot-remove online/offline operations worked as expected using vmemmap pages, I have not seen any issues so far. I wanted to try it out on Hyper-V/Xen, but I did not manage to. I plan to do so along this week (if time allows).
I would also like to test it on arm64, but I am not sure I can grab an arm64 box anytime soon.
Hi Oscar,
I ran tests on one of our arm64 machines. Particular machine doesn't actually have the mechanics for hotplug, so was all 'faked', but software wise it's all the same.
Upshot, seems to work as expected on arm64 as well.
Remove currently relies on some out of tree patches (and dirty hacks) due to the usual issue with how arm64 does pfn_valid. It's not even vaguely ready for upstream. I'll aim to post an informational set for anyone else testing in this area (it's more or less just a rebase of the patches from a few years ago).
+CC Shameer who has been testing the virtualization side for more details on that, and Robin who is driving forward memory hotplug in general on the arm64 side.
Thanks,
Jonathan
Quoted 140 lines
[Coverletter]:
This is another step to make the memory hotplug more usable. The primary goal of this patchset is to reduce memory overhead of the hot added memory (at least for
SPARSE_VMEMMAPmemory model). The current way we use to populate memmap (struct page array) has two main drawbacks:a) it consumes an additional memory until the hotadded memory itself is onlined and
b) memmap might end up on a different numa node which is especially true formovable_nodeconfiguration.a) is problem especially for memory hotplug based memory "ballooning" solutions when the delay between physical memory hotplug and the onlining can lead to OOM and that led to introduction of hacks like auto onlining (see
31bc3858ea3e("memory-hotplug: add automatic onlining policy for the newly added memory")).b) can have performance drawbacks.
I have also seen hot-add operations failing on powerpc due to the fact that we try to use order-8 pages when populating the memmap array. Given 64KB base pagesize, that is 16MB.
If we run out of those, we just fail the operation and we cannot add more memory.
We could fallback to base pages asx86_64does, but we can do better.One way to mitigate all these issues is to simply allocate memmap array (which is the largest memory footprint of the physical memory hotplug) from the hotadded memory itself. VMEMMAP memory model allows us to map any pfn range so the memory doesn't need to be online to be usable for the array. See patch 3 for more details. In short I am reusing an existing
vmem_altmapwhich wants to achieve the same thing for nvdim device memory.There is also one potential drawback, though. If somebody uses memory hotplug for 1G (gigantic) hugetlb pages then this scheme will not work for them obviously because each memory block will contain reserved area. Large x86 machines will use 2G memblocks so at least one 1G page will be available but this is still not 2G...
I am not really sure somebody does that and how reliable that can work actually. Nevertheless, I _believe_ that onlining more memory into virtual machines is much more common usecase. Anyway if there ever is a strong demand for such a usecase we have basically 3 options a) enlarge memory blocks even more b) enhance altmap allocation strategy and reuse low memory sections to host memmaps of other sections on the same NUMA node c) have the memmap allocation strategy configurable to fallback to the current allocation.
[Overall design]:
Let us say we hot-add 2GB of memory on a
x86_64(memblock size = 128M). That is:- 16 sections
- 524288 pages
- 8192 vmemmap pages (out of those 524288. We spend 512 pages for each section)The range of pages is:
0xffffea0004000000-0xffffea0006000000The vmemmap range is:0xffffea0004000000-0xffffea0004080000
0xffffea0004000000is the head vmemmap page (first page), while all the others are "tails".We keep the following information in it:
- Head page:
-head->_refcount: number of sections
-head->private: number of vmemmap pages
- Tail page:
-tail->freelist: pointer to the headThis is done because it eases the work in cases where we have to compute the number of vmemmap pages to know how much do we have to skip etc, and to keep the right accounting to
present_pages.When we want to hot-remove the range, we need to be careful because the first pages of that range, are used for the memmap maping, so if we remove those first, we would blow up while accessing the others later on.
For that reason we keep the number of sections inhead->_refcount, to know how much do we have to defer the free up.Since in a hot-remove operation, sections are being removed sequentially, the approach taken here is that every time we hit
free_section_memmap(), we decrease the refcount of the head.
When it reaches 0, we know that we hit the last section, so we callvmemmap_free()for the whole memory-range in backwards, so we make sure that the pages used for the mapping will be latest to be freed up.The accounting is as follows:
Vmemmap pages are charged to spanned/present_paged, but not to
manages_pages.I yet have to check a couple of things like creating an accounting item like
VMEMMAP_PAGESto show in/proc/meminfoto ease to spot the memory that went in there, testing Hyper-V/Xen to see how they react to the fact that we are using the beginning of the memory-range for our own purposes, and to check the thing about gigantic pages + hotplug.
I also have to check that there is no compilation/runtime errors whenCONFIG_SPARSEMEMbut !CONFIG_SPARSEMEM_VMEMMAP.
But before that, I would like to get people's feedback about the overall design, and ideas/suggestions.[1] https://patchwork.kernel.org/cover/10685835/
Michal Hocko (3):
mm,memory_hotplug: cleanup memory offline path
mm,memory_hotplug: provide a more generic restrictions for memory
hotplug
mm, sparse: renamekmalloc_section_memmap,__kfree_section_memmapOscar Salvador (1):
mm,memory_hotplug: allocate memmap from the added memory range for
sparse-vmemmaparch/arm64/mm/mmu.c | 10 ++- arch/ia64/mm/init.c | 5 +- arch/powerpc/mm/init_64.c | 7 ++ arch/powerpc/mm/mem.c | 6 +- arch/s390/mm/init.c | 12 ++- arch/sh/mm/init.c | 6 +- arch/x86/mm/init_32.c | 6 +- arch/x86/mm/init_64.c | 20 +++-- drivers/hv/hv_balloon.c | 1 + drivers/xen/balloon.c | 1 + include/linux/memory_hotplug.h | 42 ++++++++-- include/linux/memremap.h | 2 +- include/linux/page-flags.h | 23 +++++ kernel/memremap.c | 9 +- mm/compaction.c | 8 ++ mm/memory_hotplug.c | 186 +++++++++++++++++++++++++++++------------ mm/page_alloc.c | 47 ++++++++++- mm/page_isolation.c | 13 +++ mm/sparse.c | 124 +++++++++++++++++++++++++-- mm/util.c | 2 + 20 files changed, 431 insertions(+), 99 deletions(-)
Shameerali Kolothum Thodi
Tested-byQuoted 43 lines
-----Original Message-----
From: Jonathan Cameron
Sent: 12 February 2019 12:47
To: Oscar Salvador <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Shameerali Kolothum Thodi <[email protected]>; Linuxarm <[email protected]>; Robin Murphy <[email protected]>
Subject: Re: [RFC PATCH v2 0/4] mm,memory_hotplug: allocate memmap from hotadded memoryQuoted 8 lines · On Tue, 22 Jan 2019 11:37:04 +0100 Oscar Salvador <[email protected]> wrote:
Hi,
this is the v2 of the first RFC I sent back then in October [1]. In this new version I tried to reduce the complexity as much as possible, plus some clean ups.
[Testing]
Quoted 7 lines
I have tested it on "
x86_64" (small/big memblocks) and on "powerpc". On both architectures hot-add/hot-remove online/offline operations worked as expected using vmemmap pages, I have not seen any issues so far. I wanted to try it out on Hyper-V/Xen, but I did not manage to. I plan to do so along this week (if time allows).
I would also like to test it on arm64, but I am not sure I can grab an arm64 box anytime soon.Hi Oscar,
I ran tests on one of our arm64 machines. Particular machine doesn't actually have
the mechanics for hotplug, so was all 'faked', but software wise it's all the same.Upshot, seems to work as expected on arm64 as well.
Tested-by Jonathan Cameron <[email protected]>
Quoted 8 lines
Remove currently relies on some out of tree patches (and dirty hacks) due to the usual issue with how arm64 does
pfn_valid. It's not even vaguely ready for upstream. I'll aim to post an informational set for anyone else testing in this area (it's more or less just a rebase of the patches from a few years ago).+CC Shameer who has been testing the virtualization side for more details on that,
Right, I have sent out a RFC series[1] to enable mem hotplug for Qemu ARM virt platform. Using this Qemu, I ran few tests with your patches on a HiSilicon ARM64 platform. Looks like it is doing the job.
root@ubuntu:~# uname -a Linux ubuntu 5.0.0-rc1-mm1-00173-g22b0744 #5 SMP PREEMPT Tue Feb 5 10:32:26 GMT 2019 aarch64 aarch64 aarch64 GNU/Linux
root@ubuntu:~# numactl -H available: 2 nodes (0-1) node 0 cpus: 0 node 0 size: 981 MB node 0 free: 854 MB node 1 cpus: node 1 size: 0 MB node 1 free: 0 MB node distances: node 0 1 0: 10 20 1: 20 10 root@ubuntu:~# (qemu) (qemu) object_add memory-backend-ram,id=mem1,size=1G (qemu) device_add pc-dimm,id=dimm1,memdev=mem1,node=1 root@ubuntu:~# root@ubuntu:~# numactl -H available: 2 nodes (0-1) node 0 cpus: 0 node 0 size: 981 MB node 0 free: 853 MB node 1 cpus: node 1 size: 1008 MB node 1 free: 1008 MB node distances: node 0 1 0: 10 20 1: 20 10 root@ubuntu:~#
FWIW,
Thanks,
Shameer
[1] https://lists.gnu.org/archive/html/qemu-devel/2019-01/msg06966.html
and Robin who is driving forward memory hotplug in general on the arm64
Quoted 165 lines
side.
Thanks,
Jonathan
Quoted 6 lines
[Coverletter]:
This is another step to make the memory hotplug more usable. The primary goal of this patchset is to reduce memory overhead of the hot added memory (at least for
SPARSE_VMEMMAPmemory model). The current waywe use
Quoted 22 lines
to populate memmap (struct page array) has two main drawbacks:
a) it consumes an additional memory until the hotadded memory itself is onlined and
b) memmap might end up on a different numa node which is especially true formovable_nodeconfiguration.a) is problem especially for memory hotplug based memory "ballooning" solutions when the delay between physical memory hotplug and the onlining can lead to OOM and that led to introduction of hacks like auto onlining (see
31bc3858ea3e("memory-hotplug: add automatic onlining policy for the newly added memory")).b) can have performance drawbacks.
I have also seen hot-add operations failing on powerpc due to the fact that we try to use order-8 pages when populating the memmap array. Given 64KB base pagesize, that is 16MB.
If we run out of those, we just fail the operation and we cannot add more memory.
We could fallback to base pages asx86_64does, but we can do better.Quoted 3 lines
One way to mitigate all these issues is to simply allocate memmap array (which is the largest memory footprint of the physical memory hotplug) from the hotadded memory itself. VMEMMAP memory model allows us to
map
Quoted 20 lines
any pfn range so the memory doesn't need to be online to be usable for the array. See patch 3 for more details. In short I am reusing an existing
vmem_altmapwhich wants to achieve the same thing for nvdim device memory.There is also one potential drawback, though. If somebody uses memory hotplug for 1G (gigantic) hugetlb pages then this scheme will not work for them obviously because each memory block will contain reserved area. Large x86 machines will use 2G memblocks so at least one 1G page will be available but this is still not 2G...
I am not really sure somebody does that and how reliable that can work actually. Nevertheless, I _believe_ that onlining more memory into virtual machines is much more common usecase. Anyway if there ever is a strong demand for such a usecase we have basically 3 options a) enlarge memory blocks even more b) enhance altmap allocation strategy and reuse low memory sections to host memmaps of other sections on the same NUMA node c) have the memmap allocation strategy configurable to fallback to the current allocation.
Quoted 8 lines
[Overall design]:
Let us say we hot-add 2GB of memory on a
x86_64(memblock size = 128M). That is:- 16 sections
- 524288 pages
- 8192 vmemmap pages (out of those 524288. We spend 512 pages for eachsection)
Quoted 5 lines
The range of pages is:
0xffffea0004000000-0xffffea0006000000The vmemmap range is:0xffffea0004000000-0xffffea0004080000
0xffffea0004000000is the head vmemmap page (first page), while all theothers
Quoted 11 lines
are "tails".
We keep the following information in it:
- Head page:
-head->_refcount: number of sections
-head->private: number of vmemmap pages
- Tail page:
-tail->freelist: pointer to the headThis is done because it eases the work in cases where we have to compute
the
Quoted 1 line
number of vmemmap pages to know how much do we have to skip etc, and to
keep
Quoted 3 lines
the right accounting to
present_pages.When we want to hot-remove the range, we need to be careful because the
first
Quoted 1 line
pages of that range, are used for the memmap maping, so if we remove
those
Quoted 2 lines
first, we would blow up while accessing the others later on. For that reason we keep the number of sections in
head->_refcount, to knowhow
Quoted 4 lines
much do we have to defer the free up.
Since in a hot-remove operation, sections are being removed sequentially, the approach taken here is that every time we hit
free_section_memmap(), wedecrease
Quoted 3 lines
the refcount of the head.
When it reaches 0, we know that we hit the last section, so we callvmemmap_free()for the whole memory-range in backwards, so we makesure that
Quoted 5 lines
the pages used for the mapping will be latest to be freed up.
The accounting is as follows:
Vmemmap pages are charged to spanned/present_paged, but not to
manages_pages.Quoted 3 lines
I yet have to check a couple of things like creating an accounting item like
VMEMMAP_PAGESto show in/proc/meminfoto ease to spot thememory that
Quoted 2 lines
went in there, testing Hyper-V/Xen to see how they react to the fact that we are using the beginning of the memory-range for our own purposes, and
to
Quoted 7 lines
check the thing about gigantic pages + hotplug.
I also have to check that there is no compilation/runtime errors whenCONFIG_SPARSEMEMbut !CONFIG_SPARSEMEM_VMEMMAP.
But before that, I would like to get people's feedback about the overall design, and ideas/suggestions.Quoted 7 lines
[1] https://patchwork.kernel.org/cover/10685835/
Michal Hocko (3):
mm,memory_hotplug: cleanup memory offline path
mm,memory_hotplug: provide a more generic restrictions for memory
hotplug
mm, sparse: renamekmalloc_section_memmap,
__kfree_section_memmapQuoted 3 lines
Oscar Salvador (1):
mm,memory_hotplug: allocate memmap from the added memory rangefor
Quoted 18 lines
sparse-vmemmap
arch/arm64/mm/mmu.c | 10 ++- arch/ia64/mm/init.c | 5 +- arch/powerpc/mm/init_64.c | 7 ++ arch/powerpc/mm/mem.c | 6 +- arch/s390/mm/init.c | 12 ++- arch/sh/mm/init.c | 6 +- arch/x86/mm/init_32.c | 6 +- arch/x86/mm/init_64.c | 20 +++-- drivers/hv/hv_balloon.c | 1 + drivers/xen/balloon.c | 1 + include/linux/memory_hotplug.h | 42 ++++++++-- include/linux/memremap.h | 2 +- include/linux/page-flags.h | 23 +++++ kernel/memremap.c | 9 +- mm/compaction.c | 8 ++ mm/memory_hotplug.c | 186+++++++++++++++++++++++++++++------------
Quoted 3 lines
mm/page_alloc.c | 47 ++++++++++- mm/page_isolation.c | 13 +++ mm/sparse.c | 124+++++++++++++++++++++++++--
Quoted 3 lines
mm/util.c | 2 + 20 files changed, 431 insertions(+), 99 deletions(-)
Oscar Salvador
maintainerQuoted 9 lines · Shameerali Kolothum Thodi
Quoted 9 lines
Hi Oscar,
I ran tests on one of our arm64 machines. Particular machine doesn't actually have
the mechanics for hotplug, so was all 'faked', but software wise it's all the same.Upshot, seems to work as expected on arm64 as well.
Tested-by Jonathan Cameron <[email protected]>
Thanks Jonathan for having given it a spin, much appreciated! I was short of arm64 machines.
Quoted 4 lines
(qemu)
object_addmemory-backend-ram,id=mem1,size=1G (qemu)device_addpc-dimm,id=dimm1,memdev=mem1,node=1 root@ubuntu:~#root@ubuntu:~# numactl -H
...
Quoted 8 lines
node 1 cpus:
node 1 size: 1008 MB
node 1 free: 1008 MB
node distances:
node 0 10: 10 20 1: 20 10 root@ubuntu:~#
Ok, this is what I wanted to see.
When you hotplugged 1GB, 16MB out of 1024MB were spent for the memmap array, that is why you only see 1008MB there.
I am not sure what is the default section size for arm64, but assuming is 128MB, that would make sense as 1GB would mean 8 sections, and each section takes 2MB.
That means that at least the mechanism works.
Quoted 3 lines
FWIW,
Tested-by Shameer Kolothum <[email protected]>
thanks for having tested it ;-)!
Signature · 2 lines
Oscar Salvador SUSE L3
Quoted 32 lines · Oscar Salvador
Quoted 9 lines · Shameerali Kolothum Thodi
Quoted 9 lines
Hi Oscar,
I ran tests on one of our arm64 machines. Particular machine doesn't actually have
the mechanics for hotplug, so was all 'faked', but software wise it's all the same.Upshot, seems to work as expected on arm64 as well.
Tested-by Jonathan Cameron <[email protected]>Thanks Jonathan for having given it a spin, much appreciated! I was short of arm64 machines.
Quoted 4 lines
(qemu)
object_addmemory-backend-ram,id=mem1,size=1G (qemu)device_addpc-dimm,id=dimm1,memdev=mem1,node=1 root@ubuntu:~#root@ubuntu:~# numactl -H...
Quoted 8 lines
node 1 cpus:
node 1 size: 1008 MB
node 1 free: 1008 MB
node distances:
node 0 10: 10 20 1: 20 10 root@ubuntu:~#Ok, this is what I wanted to see.
When you hotplugged 1GB, 16MB out of 1024MB were spent for the memmap array, that is why you only see 1008MB there.
Quoted 5 lines
I am not sure what is the default section size for arm64, but assuming is 128MB, that would make sense as 1GB would mean 8 sections, and each section takes 2MB.
That means that at least the mechanism works.
Please make sure to test on a larger machine which has multi section memblocks. This is where I was hitting on bugs hard.
Signature · 2 lines
Michal Hocko SUSE Labs
Oscar Salvador
maintainerQuoted 2 lines · Michal Hocko
Please make sure to test on a larger machine which has multi section memblocks. This is where I was hitting on bugs hard.
I tested the patchset with large memblocks (2GB) on x86_64, and worked fine as well.
On powerpc I was only able to test it on normal memblocks, but I will check if I can boost the memory there to get large memblocks.
And about arm64, I will talk to Jonathan off-list to see if we can do the same.
Btw, in the meantime, we could get some parts reviewed perhaps.
Signature · 2 lines
Oscar Salvador SUSE L3