mm/vmalloc: Reduce memory usage and speed up huge vmalloc
Subsystems (4)
cover mm/vmalloc: Reduce memory usage and speed up huge vmalloc
From: Wen Jiang <[email protected]>
Huge vmalloc (VM_ALLOW_HUGE_VMAP, used by kvmalloc() and vmalloc_huge()) has two problems:
1. The size is rounded up to the mapping size. On arm64 a 2.1MB kvmalloc() backs 4MB, and an 8.1MB one backs 10MB. Besides the wasted memory, every extra page has to be zeroed on allocation (with init_on_alloc or __GFP_ZERO) and freed again by vfree(), so allocation and vfree() are also slower.
2. If a huge page allocation fails halfway, e.g. the first 2MB block succeeds but the second does not, everything obtained so far is freed and the whole area is allocated again with base pages. The time spent on the first block is wasted, and smaller block sizes such as arm64 CONT_PTE are never tried.
This series allocates and maps an area level by level, e.g. PMD -> CONT_PTE -> PAGE on arm64, keeping what was already obtained, and stops rounding the size up, so an area can mix block sizes:
2.1MB area on arm64 (not to scale):
baseline, 2MB blocks available: 4MB used, 2 TLB entries
+--------------------------+--------------------------+ | 2MB PMD | 2MB PMD, 1.9MB unused | +--------------------------+--------------------------+
baseline, 2MB blocks not available: 537 TLB entries
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| | | | | | | | | | | | | | | | | | | | | | | | | | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537 x 4KB PTEthis series: 2.1MB used, 11 TLB entries
+--------------------------+------+-+-+-+-+-+-+-+-+-+
| 2MB PMD | 64KB | | | | | | | | | |
+--------------------------+------+-+-+-+-+-+-+-+-+-+
CONT_PTE 9 x 4KB PTEPatch 1 extracts the mapping into vmalloc_area_map_range(), so that an area can be mapped in several runs.
Patch 2 adds an internal VM_HAS_HUGE_VMAP flag. A mixed area has a page_order of 0, so vrealloc() and is_vm_area_hugepages() use the flag to tell whether the area has any block mappings.
Patch 3 falls back level by level. Each level takes as many whole blocks as fit the remainder, and since the level only goes down, every run stays aligned for its shift. It builds on the vmalloc batched mapping series [1].
Patch 4 stops rounding the size up, except for VM_REQUIRE_HUGE_VMAP.
Results
=======
On RK3588 (Cortex-A76), pinned to one core with the performance governor. vmalloc_huge() and vfree() are timed separately; each number is the median of 2000 calls.
* Memory: a 2.1MB area uses 2.1MB instead of 4MB.
* vmalloc_huge() + vfree() is up to 1.9x faster, in proportion to the pages no longer allocated.
* With simulated fragmentation, where the second 2MB block cannot be obtained, vmalloc_huge() + vfree() is 1.4x-1.7x faster and the area keeps its PMD and CONT_PTE mappings.
1. Rounding up the size
Memory backing the area, with the mapping in 4K pages:
size baseline this series 2.1MB 4MB (PMD 1024) 2.1MB (PMD 512 + CONT 16 + PTE 9) 3MB 4MB (PMD 1024) 3MB (PMD 512 + CONT 256) 8.1MB 10MB (PMD 2560) 8.1MB (PMD 2048 + CONT 16 + PTE 9)
Time per call in us with init_on_alloc=1, lower is better. total is vmalloc_huge() plus vfree(), from the same run:
baseline this series
size alloc vfree total alloc vfree total speedup
2.1MB 143 43 186 76 23 99 1.88x 3MB 145 43 188 110 34 144 1.31x 8.1MB 376 107 483 304 88 392 1.23x 4MB 146 43 189 146 43 189 1.00x 8MB 295 86 381 297 86 383 0.99x
4MB and 8MB are mapped the same way on both kernels and take the same time, so splitting an area into several runs adds no measurable cost.
Time of vmalloc_huge() alone in us. zeroing is init_on_alloc=1 minus init_on_alloc=0:
init_on_alloc=1 init_on_alloc=0 zeroing
size baseline series baseline series baseline series
2.1MB 143 76 3.8 3.2 139 73 3MB 145 110 3.8 3.8 141 106 8.1MB 376 304 9.0 8.2 367 296 4MB 146 146 3.8 3.8 142 142 8MB 295 297 7.3 7.3 288 290
Without zeroing, vmalloc_huge() takes 3-9us on both kernels. Zeroing costs about 0.14us per 4K page on both, so the alloc gain comes from zeroing fewer pages. vfree() does not zero; it costs about 0.042us per 4K page on both kernels, so its gain also comes from freeing fewer pages.
2. Only the first 2MB block succeeds
A test hook lets each vmalloc_huge() call get only one 2MB block: the first order-9 allocation succeeds, and every later order-9 or larger allocation fails without calling the page allocator. The baseline gets the first 2MB block, frees it and allocates the whole area again with base pages. This series keeps the block and continues with CONT_PTE.
Mapping of the area, in 4K pages:
size baseline this series 4MB PTE 1024 PMD 512 + CONT 512 8MB PTE 2048 PMD 512 + CONT 1536
Time per call in us with init_on_alloc=1, lower is better:
baseline this series
size alloc vfree total alloc vfree total speedup
4MB 279 52 331 150 48 198 1.67x 8MB 468 102 570 309 97 406 1.40x
Without the hook, the same sizes take 189us and 381us in total (table above). The baseline fallback costs another 142us and 189us: the first block is allocated, zeroed and thrown away, and the whole area is allocated again from base pages. This series stays close to the no-failure time.
Testing on other machines and with real workloads would be very welcome.
lib/test_vmalloc.ko passes, and a vrealloc() shrink test shows that only areas without block mappings free their tail pages.
This series is based on mm-new.
[1] https://lore.kernel.org/all/[email protected]/
Wen Jiang (4):
mm/vmalloc: extract vmalloc_area_map_range() mm/vmalloc: add VM_HAS_HUGE_VMAP
mm/vmalloc: try smaller block sizes before base pages mm/vmalloc: page align huge vmalloc sizes
include/linux/vmalloc.h | 7 +- kernel/liveupdate/kexec_handover.c | 3 +- mm/vmalloc.c | 124 ++++++++++++++++++----------- 3 files changed, 85 insertions(+), 49 deletions(-)
Signature · 1 line
2.34.1
RFC PATCH 1/4 mm/vmalloc: extract vmalloc_area_map_range()
From: Wen Jiang <[email protected]>
Move the mapping of a vmalloc area's pages, including the gfp scope, the __GFP_NOFAIL retry loop and the failure warning, out of __vmalloc_area_node() into vmalloc_area_map_range(). It maps the pages [start, end) of the area, where start and end are indices into the page array, with the given shift.
The warning for a failed mapping reports the size of the mapped range instead of area->nr_pages. Both are the same at the only call site, which maps the whole area.
__vmalloc_area_node() still maps the whole area in one go. A follow-up change will map an area in several runs with different shifts.
No functional change.
--- mm/vmalloc.c | 64 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 26 deletions(-)
mm/vmalloc.c +38 -26
@@ -4005,6 +4005,42 @@ vm_area_alloc_pages(gfp_t gfp, int nid,return nr_allocated; } +static int vmalloc_area_map_range (struct vm_struct *area, +unsigned long start,unsigned long end,unsigned int shift, +pgprot_t prot,gfp_t gfp) +{ +const gfp_t nested_gfp = (gfp &GFP_RECLAIM_MASK ) | __GFP_ZERO; +unsigned long addr = (unsigned long )area->addr; +bool nofail = gfp & __GFP_NOFAIL; +unsigned int flags; +int ret; + +/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */ +if (!gfpflags_allow_blocking (gfp)) + nofail = false; + +/* +* page tables allocations ignore external gfp mask, enforce it +* by the scope API +*/ + flags =memalloc_apply_gfp_scope (gfp); +do { + ret =__vmap_pages_range (addr + (start <<PAGE_SHIFT ), + addr + (end <<PAGE_SHIFT ), prot, + area->pages + start, shift, nested_gfp); +if (nofail && (ret <0 )) +schedule_timeout_uninterruptible (1 ); + }while (nofail && (ret <0 )); +memalloc_restore_scope (flags); + +if (ret <0 ) +warn_alloc (gfp,NULL , +"vmalloc error: size %lu, failed to map pages" , + (end - start) <<PAGE_SHIFT ); + +return ret; +} +static LLIST_HEAD (pending_vm_area_cleanup);static void cleanup_vm_area_work (struct work_struct *work) {
@@ -4078,21 +4114,13 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, int node) {const gfp_t nested_gfp = (gfp_mask &GFP_RECLAIM_MASK ) | __GFP_ZERO; -bool nofail = gfp_mask & __GFP_NOFAIL; -unsigned long addr = (unsigned long )area->addr;unsigned long size =get_vm_area_size (area);unsigned long array_size;unsigned long nr_small_pages = size >>PAGE_SHIFT ;unsigned int page_order = page_shift -PAGE_SHIFT ; -unsigned int flags; -int ret; array_size = nr_small_pages *sizeof (struct page *); -/* __GFP_NOFAIL and "noblock" flags are mutually exclusive. */ -if (!gfpflags_allow_blocking (gfp_mask)) - nofail = false; -if (!(gfp_mask & (GFP_DMA |GFP_DMA32 ))) gfp_mask |= __GFP_HIGHMEM;
@@ -4145,25 +4173,9 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, goto fail; } -/* -* page tables allocations ignore external gfp mask, enforce it -* by the scope API -*/ - flags =memalloc_apply_gfp_scope (gfp_mask); -do { - ret =__vmap_pages_range (addr, addr + size, prot, area->pages, - page_shift, nested_gfp); -if (nofail && (ret <0 )) -schedule_timeout_uninterruptible (1 ); - }while (nofail && (ret <0 )); -memalloc_restore_scope (flags); - -if (ret <0 ) { -warn_alloc (gfp_mask,NULL , -"vmalloc error: size %lu, failed to map pages" , - area->nr_pages *PAGE_SIZE ); +if (vmalloc_area_map_range (area,0 , nr_small_pages, page_shift, prot, + gfp_mask)) goto fail; - }set_vm_area_page_order (area, page_order);return area->addr;
Signature · 1 line
2.34.1
RFC PATCH 2/4 mm/vmalloc: add VM_HAS_HUGE_VMAP
In reply to Wen JiangFrom: Wen Jiang <[email protected]>
vrealloc() frees the tail pages of a shrinking area unless its page_order is non-zero, as partial freeing would require splitting a huge mapping, and is_vm_area_hugepages() reports huge mappings from page_order as well. Both rely on a huge vmalloc area being mapped either entirely with blocks of 1 << page_order pages or entirely with base pages.
A follow-up change lets a huge vmalloc area fall back to smaller block sizes for part of the area. page_order then has to be the smallest order used, so that the page array can still be walked in steps of 1 << page_order. It can be 0 for an area that still contains PMD or contiguous PTE mappings.
Add an internal VM_HAS_HUGE_VMAP flag, set when at least part of an area is mapped with entries larger than PAGE_SIZE, and use it in vrealloc() and is_vm_area_hugepages() instead of page_order. Together with page_order it describes the layout:
!VM_HAS_HUGE_VMAP base pages only VM_HAS_HUGE_VMAP, page_order == 0 huge mappings plus base pages VM_HAS_HUGE_VMAP, page_order > 0 huge mappings only
For vrealloc() and the powerpc set_memory_*() guard, any huge mapping is reason enough to back off. alloc_large_system_hash() only uses the result for its boot message.
set_vm_area_page_order() sets the flag for a non-zero order, so for now the flag is set exactly when page_order is non-zero and there is no functional change.
Like page_order, the flag is only set by the vmalloc allocation path. Huge mappings created by vmap() or ioremap() are tracked by neither.
Let kho_preserve_vmalloc() accept the new flag, as it rejects any flag it does not know about.
--- include/linux/vmalloc.h | 7 ++++--- kernel/liveupdate/kexec_handover.c | 3 ++- mm/vmalloc.c | 9 ++++++--- 3 files changed, 12 insertions(+), 7 deletions(-)
include/linux/vmalloc.h +4 -3
@@ -40,6 +40,7 @@ struct iov_iter; /* in uio.h */#endif #define VM_SPARSE 0x00001000/* sparse vm_area. not all pages are present. */ #define VM_REQUIRE_HUGE_VMAP 0x00002000/* huge page mapping or nothing */ +#define VM_HAS_HUGE_VMAP 0x00004000/* part of the area has huge mappings */ /* bits [20..32] reserved for arch specific ioremap internals */
@@ -265,13 +266,13 @@ static inline bool is_vm_area_hugepages(const void *addr)* This may not 100% tell if the area is mapped with > PAGE_SIZE * page table entries, if for some reason the architecture indicates * larger sizes are available but decides not to use them, nothing -* prevents that. This only indicates the size of the physical page -* allocated in the vmalloc layer. +* prevents that. This only indicates that the vmalloc layer mapped +* at least part of the area with such entries. */ #ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC struct vm_struct *area =find_vm_area (addr); -return area && area->page_order >0 ; +return area && (area->flags &VM_HAS_HUGE_VMAP );#else return false;#endif
kernel/liveupdate/kexec_handover.c +2 -1
@@ -1288,7 +1288,8 @@ void kho_unpreserve_pages(struct page *page, unsigned long nr_pages)EXPORT_SYMBOL_GPL (kho_unpreserve_pages);/* vmalloc flags KHO supports */ -#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP) +#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP | \ + VM_HAS_HUGE_VMAP)/* KHO internal flags for vmalloc preservations */ #define KHO_VMALLOC_ALLOC 0x0001
mm/vmalloc.c +6 -3
@@ -3222,6 +3222,8 @@ static inline void set_vm_area_page_order(struct vm_struct *vm, unsigned int ord {#ifdef CONFIG_HAVE_ARCH_HUGE_VMALLOC vm->page_order = order; +if (order) + vm->flags |=VM_HAS_HUGE_VMAP ;#else BUG_ON (order !=0 );#endif
@@ -4613,7 +4615,7 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align/* * Free tail pages when shrink crosses a page boundary. * -* Skip huge page allocations (page_order > 0) as partial +* Skip areas with huge mappings (VM_HAS_HUGE_VMAP) as partial * freeing would require splitting. * * Skip VM_FLUSH_RESET_PERMS, as direct-map permissions must
@@ -4629,8 +4631,9 @@ void *vrealloc_node_align_noprof(const void *p, size_t size, unsigned long align* GFP_KERNEL, which could trigger a recursive deadlock * if we are under filesystem or I/O reclaim. */ -if (new_nr_pages < vm->nr_pages && !vm_area_page_order (vm) && - !(vm->flags & (VM_FLUSH_RESET_PERMS |VM_USERMAP )) && +if (new_nr_pages < vm->nr_pages && + !(vm->flags & (VM_HAS_HUGE_VMAP |VM_FLUSH_RESET_PERMS | +VM_USERMAP )) &&gfp_has_io_fs (flags)) {unsigned long addr = (unsigned long )kasan_reset_tag (p);unsigned long old_nr_pages = vm->nr_pages;
Signature · 1 line
2.34.1
RFC PATCH 3/4 mm/vmalloc: try smaller block sizes before base pages
In reply to Wen JiangFrom: Wen Jiang <[email protected]>
When a huge vmalloc allocation (VM_ALLOW_HUGE_VMAP) cannot get enough pages of the requested order, __vmalloc_node_range_noprof() frees everything it got, including the huge pages that did succeed, and starts over with base pages. Sizes in between, such as arm64's CONT_PTE, are never tried, so under fragmentation the area ends up fully mapped with PTEs even though much of it could have been covered by larger blocks.
Instead, allocate the area level by level, from the requested mapping size down through the smaller sizes the architecture can map, e.g. PMD -> CONT_PTE -> PAGE, keeping whatever was already obtained. Each level asks vm_area_alloc_pages() for as many whole blocks of its size as fit the remainder, and maps what it got with its own shift; the rest is left to the next level. The next level is picked from what is left or, if the level could not be filled, is the next smaller size. VM_REQUIRE_HUGE_VMAP does not fall back below the requested level.
Since the level only goes down, each level starts at an index that is a multiple of its block size, and the area is aligned to the requested mapping size, so every run is suitably aligned for its shift. An area that is fully backed at the requested level is mapped in one go, as before.
page_order is set to the smallest level that was used, so the page array can still be walked in steps of 1 << page_order. Since it is set after each run, VM_HAS_HUGE_VMAP gets set as soon as one run is mapped with a shift larger than PAGE_SHIFT.
__vmalloc_node_range_noprof() no longer retries the whole allocation with base pages when __vmalloc_area_node() fails. For a page allocation failure the fallback has already ended at base pages. Mapping with base pages needs more page table pages than with huge pages, so retrying after a mapping failure is unlikely to help either, and the page array would only shrink by the rounding up of the size, which the next change removes. __GFP_NOFAIL is still honoured by
the order-0 level, the page array allocation and the mapping. The retry is kept for the case where the vm area cannot be found with the huge alignment.
--- mm/vmalloc.c | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-)
mm/vmalloc.c +33 -17
@@ -4112,14 +4112,12 @@ memalloc_restore_scope(unsigned int flags) }static void *__vmalloc_area_node (struct vm_struct *area,gfp_t gfp_mask, -pgprot_t prot,unsigned int page_shift, -int node) +pgprot_t prot,unsigned int shift,int node) {const gfp_t nested_gfp = (gfp_mask &GFP_RECLAIM_MASK ) | __GFP_ZERO;unsigned long size =get_vm_area_size (area);unsigned long array_size;unsigned long nr_small_pages = size >>PAGE_SHIFT ; -unsigned int page_order = page_shift -PAGE_SHIFT ; array_size = nr_small_pages *sizeof (struct page *);
@@ -4146,12 +4144,34 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,* potentially dangerous (pre-mature OOM, disruptive reclaim * and compaction etc. * -* Please note, the __vmalloc_node_range_noprof() falls-back -* to order-0 pages if high-order attempt is unsuccessful. +* Please note, the allocation falls back to smaller orders and +* finally to order-0 pages if high-order attempt is unsuccessful. */ - area->nr_pages =vm_area_alloc_pages ( -vmalloc_gfp_adjust (gfp_mask, page_order), node, - page_order, nr_small_pages, area->pages); +while (area->nr_pages < nr_small_pages) { +unsigned int order = shift -PAGE_SHIFT ; +unsigned long left = nr_small_pages - area->nr_pages; +unsigned long nr; + + nr =vm_area_alloc_pages (vmalloc_gfp_adjust (gfp_mask, order), + node, order,round_down (left,1UL << order), + area->pages + area->nr_pages); +if (nr) { + area->nr_pages += nr; +if (vmalloc_area_map_range (area, area->nr_pages - nr, + area->nr_pages, shift, prot, gfp_mask)) + goto fail; +set_vm_area_page_order (area, order); + } + +/* No smaller size left, or huge pages are required. */ +if (shift ==PAGE_SHIFT || (area->flags &VM_REQUIRE_HUGE_VMAP )) +break ; + +if (left - nr <1UL << order) + shift =vm_shift (prot, (left - nr) <<PAGE_SHIFT ); +else /* this level failed, step down */ + shift =vm_shift (prot,1UL << (shift -1 )); + }/* * If not enough pages were obtained to accomplish an
@@ -4165,21 +4185,17 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask,* - a pending fatal signal * - insufficient huge page-order pages * -* Since we always retry allocations at order-0 in the huge page -* case a warning for either is spurious. +* Huge page-order allocations only fail here when huge pages +* are required, so a warning for either is spurious. */ -if (!fatal_signal_pending (current) && page_order ==0 ) +if (!fatal_signal_pending (current) && + !(area->flags &VM_REQUIRE_HUGE_VMAP ))warn_alloc (gfp_mask,NULL ,"vmalloc error: size %lu, failed to allocate pages" , nr_small_pages *PAGE_SIZE ); goto fail; } -if (vmalloc_area_map_range (area,0 , nr_small_pages, page_shift, prot, - gfp_mask)) - goto fail; - -set_vm_area_page_order (area, page_order);return area->addr; fail:
@@ -4335,7 +4351,7 @@ void *__vmalloc_node_range_noprof(unsigned long size, unsigned long align,/* Allocate physical pages and map them into vmalloc space. */ ret =__vmalloc_area_node (area, gfp_mask, prot, shift, node);if (!ret) - goto fail; +return NULL ;/* * Mark the pages as accessible, now that they are mapped.
Signature · 1 line
2.34.1
RFC PATCH 4/4 mm/vmalloc: page align huge vmalloc sizes
In reply to Wen JiangFrom: Wen Jiang <[email protected]>
__get_vm_area_node() rounds the size of a huge vmalloc area up to its mapping size, e.g. a 2.1MB kvmalloc() on arm64 reserves and backs 4MB, wasting almost 2MB of memory. The rounding was needed because each huge page allocation filled a whole block, so the page count had to be a multiple of the block size.
Now that the allocation falls back level by level and each level only asks for whole blocks that fit the remainder, the tail of an area can be backed by smaller blocks or base pages. Round the size up to PAGE_SIZE only. The alignment of the area is unchanged, as it is passed separately.
Keep the rounding for VM_REQUIRE_HUGE_VMAP, which must be backed by huge pages entirely.
--- mm/vmalloc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
mm/vmalloc.c +4 -1
@@ -3313,7 +3313,10 @@ struct vm_struct *__get_vm_area_node(unsigned long size,if (WARN_ON_ONCE (in_nmi () ||in_hardirq () ||softirq_count ()))return NULL ; - size =ALIGN (size,1ul << shift); +if (flags &VM_REQUIRE_HUGE_VMAP ) + size =ALIGN (size,1ul << shift); +else + size =PAGE_ALIGN (size);if (unlikely (!size))return NULL ;
Signature · 1 line
2.34.1
Quoted 47 lines · Wen Jiang
From: Wen Jiang <[email protected]>
Huge vmalloc (
VM_ALLOW_HUGE_VMAP, used bykvmalloc()andvmalloc_huge()) has two problems:1. The size is rounded up to the mapping size. On arm64 a 2.1MB
kvmalloc()backs 4MB, and an 8.1MB one backs 10MB. Besides the wasted memory, every extra page has to be zeroed on allocation (withinit_on_allocor__GFP_ZERO) and freed again byvfree(), so allocation andvfree()are also slower.2. If a huge page allocation fails halfway, e.g. the first 2MB block succeeds but the second does not, everything obtained so far is freed and the whole area is allocated again with base pages. The time spent on the first block is wasted, and smaller block sizes such as arm64
CONT_PTEare never tried.This series allocates and maps an area level by level, e.g. PMD ->
CONT_PTE-> PAGE on arm64, keeping what was already obtained, and stops rounding the size up, so an area can mix block sizes:2.1MB area on arm64 (not to scale):
baseline, 2MB blocks available: 4MB used, 2 TLB entries
+--------------------------+--------------------------+ | 2MB PMD | 2MB PMD, 1.9MB unused | +--------------------------+--------------------------+baseline, 2MB blocks not available: 537 TLB entries
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | | | | | | | | | | | | | | | | | | | | | | | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 537 x 4KB PTEthis series: 2.1MB used, 11 TLB entries
+--------------------------+------+-+-+-+-+-+-+-+-+-+ | 2MB PMD | 64KB | | | | | | | | | | +--------------------------+------+-+-+-+-+-+-+-+-+-+ CONT_PTE 9 x 4KB PTEPatch 1 extracts the mapping into
vmalloc_area_map_range(), so that an area can be mapped in several runs.Patch 2 adds an internal
VM_HAS_HUGE_VMAPflag. A mixed area has apage_orderof 0, sovrealloc()andis_vm_area_hugepages()use the flag to tell whether the area has any block mappings.
Quoted 6 lines
Patch 3 falls back level by level. Each level takes as many whole blocks as fit the remainder, and since the level only goes down, every run stays aligned for its shift. It builds on the vmalloc batched mapping series [1].
Patch 4 stops rounding the size up, except for
VM_REQUIRE_HUGE_VMAP.
Just for your note, I had tried this, but with a different motivation:
https://lore.kernel.org/all/[email protected]/
Now that you are doing this, and with some vmalloc-vfree optimizations done in the past months, we may actually revisit "vmalloc-huge by default".
Quoted 104 lines
Results
=======On RK3588 (Cortex-A76), pinned to one core with the performance governor.
vmalloc_huge()andvfree()are timed separately; each number is the median of 2000 calls.* Memory: a 2.1MB area uses 2.1MB instead of 4MB.
*vmalloc_huge()+vfree()is up to 1.9x faster, in proportion to the pages no longer allocated.
* With simulated fragmentation, where the second 2MB block cannot be obtained,vmalloc_huge()+vfree()is 1.4x-1.7x faster and the area keeps its PMD andCONT_PTEmappings.1. Rounding up the size
Memory backing the area, with the mapping in 4K pages:
size baseline this series 2.1MB 4MB (PMD 1024) 2.1MB (PMD 512 + CONT 16 + PTE 9) 3MB 4MB (PMD 1024) 3MB (PMD 512 + CONT 256) 8.1MB 10MB (PMD 2560) 8.1MB (PMD 2048 + CONT 16 + PTE 9)Time per call in us with init_on_alloc=1, lower is better. total is
vmalloc_huge()plusvfree(), from the same run:baseline this series
size alloc vfree total alloc vfree total speedup2.1MB 143 43 186 76 23 99 1.88x 3MB 145 43 188 110 34 144 1.31x 8.1MB 376 107 483 304 88 392 1.23x 4MB 146 43 189 146 43 189 1.00x 8MB 295 86 381 297 86 383 0.99x4MB and 8MB are mapped the same way on both kernels and take the same time, so splitting an area into several runs adds no measurable cost.
Time of
vmalloc_huge()alone in us. zeroing is init_on_alloc=1 minus init_on_alloc=0:init_on_alloc=1 init_on_alloc=0 zeroing
size baseline series baseline series baseline series2.1MB 143 76 3.8 3.2 139 73 3MB 145 110 3.8 3.8 141 106 8.1MB 376 304 9.0 8.2 367 296 4MB 146 146 3.8 3.8 142 142 8MB 295 297 7.3 7.3 288 290Without zeroing,
vmalloc_huge()takes 3-9us on both kernels. Zeroing costs about 0.14us per 4K page on both, so the alloc gain comes from zeroing fewer pages.vfree()does not zero; it costs about 0.042us per 4K page on both kernels, so its gain also comes from freeing fewer pages.2. Only the first 2MB block succeeds
A test hook lets each
vmalloc_huge()call get only one 2MB block: the first order-9 allocation succeeds, and every later order-9 or larger allocation fails without calling the page allocator. The baseline gets the first 2MB block, frees it and allocates the whole area again with base pages. This series keeps the block and continues withCONT_PTE.Mapping of the area, in 4K pages:
size baseline this series 4MB PTE 1024 PMD 512 + CONT 512 8MB PTE 2048 PMD 512 + CONT 1536Time per call in us with init_on_alloc=1, lower is better:
baseline this series
size alloc vfree total alloc vfree total speedup4MB 279 52 331 150 48 198 1.67x 8MB 468 102 570 309 97 406 1.40xWithout the hook, the same sizes take 189us and 381us in total (table above). The baseline fallback costs another 142us and 189us: the first block is allocated, zeroed and thrown away, and the whole area is allocated again from base pages. This series stays close to the no-failure time.
Testing on other machines and with real workloads would be very welcome.
lib/test_vmalloc.kopasses, and avrealloc()shrink test shows that only areas without block mappings free their tail pages.This series is based on mm-new.
[1] https://lore.kernel.org/all/[email protected]/
Wen Jiang (4):
mm/vmalloc: extractvmalloc_area_map_range()mm/vmalloc: addVM_HAS_HUGE_VMAP
mm/vmalloc: try smaller block sizes before base pages mm/vmalloc: page align huge vmalloc sizesinclude/linux/vmalloc.h | 7 +- kernel/liveupdate/kexec_handover.c | 3 +- mm/vmalloc.c | 124 ++++++++++++++++++----------- 3 files changed, 85 insertions(+), 49 deletions(-)