From: Wen Jiang <jiangwenxiaomi@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Uladzislau Rezki <urezki@gmail.com>,
linux-mm@kvack.org
Cc: Barry Song <baohua@kernel.org>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Anshuman Khandual <anshuman.khandual@arm.com>,
David Hildenbrand <david@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Will Deacon <will@kernel.org>, Leo Yan <leo.yan@arm.com>,
Xueyuan Chen <xueyuan.chen21@gmail.com>,
Mike Rapoport <rppt@kernel.org>,
Pasha Tatashin <pasha.tatashin@soleen.com>,
Pratyush Yadav <pratyush@kernel.org>,
Alexander Graf <graf@amazon.com>,
kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, Wen Jiang <jiangwen6@xiaomi.com>
Subject: [RFC PATCH 3/4] mm/vmalloc: try smaller block sizes before base pages
Date: Wed, 2 Dec 2026 17:12:11 +0800 [thread overview]
Message-ID: <20261202091212.363755-4-jiangwenxiaomi@gmail.com> (raw)
In-Reply-To: <20261202091212.363755-1-jiangwenxiaomi@gmail.com>
From: Wen Jiang <jiangwen6@xiaomi.com>
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.
Suggested-by: Barry Song <baohua@kernel.org>
Assisted-by: LLM
Signed-off-by: Wen Jiang <jiangwen6@xiaomi.com>
---
mm/vmalloc.c | 50 +++++++++++++++++++++++++++++++++-----------------
1 file changed, 33 insertions(+), 17 deletions(-)
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 595b07a6cc3d1..5edc6bd20969b 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -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.
--
2.34.1
next prev parent reply other threads:[~2026-12-02 9:13 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 9:12 [RFC PATCH 0/4] mm/vmalloc: Reduce memory usage and speed up huge vmalloc Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 1/4] mm/vmalloc: extract vmalloc_area_map_range() Wen Jiang
2026-12-02 9:12 ` [RFC PATCH 2/4] mm/vmalloc: add VM_HAS_HUGE_VMAP Wen Jiang
2026-12-02 9:12 ` Wen Jiang [this message]
2026-12-02 9:12 ` [RFC PATCH 4/4] mm/vmalloc: page align huge vmalloc sizes Wen Jiang
2026-12-02 12:09 ` [RFC PATCH 0/4] mm/vmalloc: Reduce memory usage and speed up huge vmalloc Dev Jain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202091212.363755-4-jiangwenxiaomi@gmail.com \
--to=jiangwenxiaomi@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=anshuman.khandual@arm.com \
--cc=baohua@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=graf@amazon.com \
--cc=jiangwen6@xiaomi.com \
--cc=kexec@lists.infradead.org \
--cc=leo.yan@arm.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pasha.tatashin@soleen.com \
--cc=pratyush@kernel.org \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=urezki@gmail.com \
--cc=will@kernel.org \
--cc=xueyuan.chen21@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®