From: Hao Ge <hao.ge@linux.dev>
To: Suren Baghdasaryan <surenb@google.com>
Cc: Uladzislau Rezki <urezki@gmail.com>, Dev Jain <dev.jain@arm.com>,
Andrew Morton <akpm@linux-foundation.org>,
Madhavan Srinivasan <maddy@linux.ibm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Nicholas Piggin <npiggin@gmail.com>,
"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
Shrikanth Hegde <sshegde@linux.ibm.com>,
Alexander Potapenko <glider@google.com>,
Marco Elver <elver@google.com>,
Dmitry Vyukov <dvyukov@google.com>,
Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
Christoph Lameter <cl@gentwo.org>,
linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
kasan-dev@googlegroups.com, linux-mm@kvack.org
Subject: Re: [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() mapped on failure
Date: Mon, 23 Nov 2026 18:50:28 +0800 [thread overview]
Message-ID: <7fad5338-7e9e-4fcf-b369-20f80639a9e4@linux.dev> (raw)
In-Reply-To: <CAJuCfpFvA5oqop2tovy33fKydx7n8Sm=X-hE0sJxpcR1gg5hMA@mail.gmail.com>
Hi Suren
On 2026/11/22 11:58, Suren Baghdasaryan wrote:
> On Sun, Nov 15, 2026 at 11:14 PM Hao Ge <hao.ge@linux.dev> wrote:
>>
>> __vmap_pages_range_noflush() maps a struct page array and may fail
>> half way. The ptes installed so far stay behind, so a retry of the
>> same range, the __GFP_NOFAIL loop in __vmalloc_area_node() or the
>> module tags populate, trips the WARN_ON(!pte_none()) in
>> vmap_pages_pte_range() and fails with -EBUSY.
>>
>> vmap_pages_range_noflush_walk() now reports how many pages it
>> installed through its running index into the pages array, and
>> __vmap_pages_range_noflush() undoes [addr, addr + mapped_pages *
>> PAGE_SIZE) on failure. vmap_pages_pte_range() fails with -EBUSY
>> instead of installing over a pte that is already set, so the ptes
>> below that point are all ours, while a pte above it can only have
>> been put there by somebody else, for example when the caller's range
>> overlaps an existing mapping. Going further would clear ptes that
>> are not ours.
>>
>> This path can run with a non-blocking gfp mask, the undo passes
>> gfpflags_allow_blocking() as can_resched.
>>
>> Suggested-by: Suren Baghdasaryan <surenb@google.com>
>> Suggested-by: Uladzislau Rezki <urezki@gmail.com>
>> Signed-off-by: Hao Ge <hao.ge@linux.dev>
>> ---
>> mm/kmsan/hooks.c | 4 ++--
>> mm/kmsan/shadow.c | 4 ++--
>> mm/vmalloc.c | 36 +++++++++++++++++++++++++++++-------
>> mm/vmalloc.h | 3 ++-
>> 4 files changed, 35 insertions(+), 12 deletions(-)
>>
>> diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c
>> index 69008b085658..d830a4e649f1 100644
>> --- a/mm/kmsan/hooks.c
>> +++ b/mm/kmsan/hooks.c
>> @@ -208,7 +208,7 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,
>> mapped = __vmap_pages_range_noflush(
>> vmalloc_shadow(start + off),
>> vmalloc_shadow(start + off + PAGE_SIZE), prot, &shadow,
>> - PAGE_SHIFT);
>> + PAGE_SHIFT, gfp_mask);
>> if (mapped) {
>> err = mapped;
>> goto ret;
>> @@ -218,7 +218,7 @@ int kmsan_ioremap_page_range(unsigned long start, unsigned long end,
>> mapped = __vmap_pages_range_noflush(
>> vmalloc_origin(start + off),
>> vmalloc_origin(start + off + PAGE_SIZE), prot, &origin,
>> - PAGE_SHIFT);
>> + PAGE_SHIFT, gfp_mask);
>> if (mapped) {
>> err = mapped;
>> goto ret;
>> diff --git a/mm/kmsan/shadow.c b/mm/kmsan/shadow.c
>> index 212449db34a1..452760a333e4 100644
>> --- a/mm/kmsan/shadow.c
>> +++ b/mm/kmsan/shadow.c
>> @@ -247,7 +247,7 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
>> origin_end = vmalloc_meta((void *)end, KMSAN_META_ORIGIN);
>> kmsan_enter_runtime();
>> mapped = __vmap_pages_range_noflush(shadow_start, shadow_end, prot,
>> - s_pages, page_shift);
>> + s_pages, page_shift, gfp_mask);
>> kmsan_leave_runtime();
>> if (mapped) {
>> err = mapped;
>> @@ -255,7 +255,7 @@ int kmsan_vmap_pages_range_noflush(unsigned long start, unsigned long end,
>> }
>> kmsan_enter_runtime();
>> mapped = __vmap_pages_range_noflush(origin_start, origin_end, prot,
>> - o_pages, page_shift);
>> + o_pages, page_shift, gfp_mask);
>> kmsan_leave_runtime();
>> if (mapped) {
>> /* Undo the shadow mapping set up above. */
>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
>> index 5c1eacc2a0ac..4f3c320a4849 100644
>> --- a/mm/vmalloc.c
>> +++ b/mm/vmalloc.c
>> @@ -725,7 +725,8 @@ static int vmap_pages_p4d_range(pgd_t *pgd, unsigned long addr,
>> * may have contiguous chunks, as hinted by @shift.
>> */
>> static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
>> - pgprot_t prot, struct page **pages, unsigned int shift)
>> + pgprot_t prot, struct page **pages, unsigned int shift,
>> + unsigned int *mapped_pages)
>> {
>> unsigned long start = addr;
>> pgd_t *pgd;
>> @@ -748,6 +749,7 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
>> if (mask & ARCH_PAGE_TABLE_SYNC_MASK)
>> arch_sync_kernel_mappings(start, end);
>>
>> + *mapped_pages = nr;
>> return err;
>> }
>>
>> @@ -758,18 +760,36 @@ static int vmap_pages_range_noflush_walk(unsigned long addr, unsigned long end,
>> * The caller is responsible for calling flush_cache_vmap() after this
>> * function returns successfully and before the addresses are accessed.
>> *
>> + * On failure the PTEs installed by this call are torn down before
>> + * returning, so a retry sees a clean range; PTEs that were already
>> + * there when the walk failed are left alone. When @gfp_mask cannot
>> + * block, the teardown skips cond_resched(), it may run in atomic
>> + * context.
>> + *
>> * This is an internal function only. Do not use outside mm/.
>> */
>> int __vmap_pages_range_noflush(unsigned long addr, unsigned long end,
>> - pgprot_t prot, struct page **pages, unsigned int page_shift)
>> + pgprot_t prot, struct page **pages, unsigned int page_shift,
>> + gfp_t gfp_mask)
>> {
>> + int err;
>> + unsigned int mapped_pages = 0;
>> +
>> if (WARN_ON_ONCE(page_shift < PAGE_SHIFT))
>> return -EINVAL;
>>
>> if (!IS_ENABLED(CONFIG_HAVE_ARCH_HUGE_VMALLOC))
>> page_shift = PAGE_SHIFT;
>>
>> - return vmap_pages_range_noflush_walk(addr, end, prot, pages, page_shift);
>> + err = vmap_pages_range_noflush_walk(addr, end, prot, pages, page_shift,
>> + &mapped_pages);
>> +
>> + /* Undo the PTEs installed before the failure. */
>> + if (err && mapped_pages)
>> + __vunmap_range_noflush(addr, addr + (mapped_pages * PAGE_SIZE),
>> + gfpflags_allow_blocking(gfp_mask));
>> +
>> + return err;
>> }
>>
>> int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
>> @@ -781,7 +801,8 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,
>>
>> if (ret)
>> return ret;
>> - return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift);
>> + return __vmap_pages_range_noflush(addr, end, prot, pages, page_shift,
>> + gfp_mask);
>> }
>>
>> static int __vmap_pages_range(unsigned long addr, unsigned long end,
>> @@ -3715,7 +3736,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
>> pgprot_t prot, struct page **pages)
>> {
>> const unsigned int nr_pages = (end - addr) >> PAGE_SHIFT;
>> - unsigned int prev_shift = 0, batch_idx = 0;
>> + unsigned int prev_shift = 0, batch_idx = 0, nr_mapped;
>> unsigned long batch_start = addr, batch_end = addr;
>> int err;
>>
>> @@ -3734,7 +3755,8 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
>>
>> if (shift != prev_shift) {
>> err = vmap_pages_range_noflush_walk(batch_start, batch_end,
>> - prot, pages + batch_idx, prev_shift);
>> + prot, pages + batch_idx, prev_shift,
>> + &nr_mapped);
>> if (err)
>> goto out;
>> prev_shift = shift;
>> @@ -3756,7 +3778,7 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
>> /* Remaining */
>> if (batch_start < end)
>> err = vmap_pages_range_noflush_walk(batch_start, end, prot,
>> - pages + batch_idx, prev_shift);
>> + pages + batch_idx, prev_shift, &nr_mapped);
>>
>> out:
>> if (!err)
>
> So, you are recording nr_mapped but not undoing
> vmap_pages_range_batched() partial mappings yet. You do that in patch
> #9. It's weird to fix one place but leave the other one until later. I
> would suggest merging patches 7 and 9 to have a complete fix.
Yes, that looks correct when revisiting these patches.
Thanks
Best Regards
Hao
>
>> diff --git a/mm/vmalloc.h b/mm/vmalloc.h
>> index 4ec114a75f04..9ea28845f713 100644
>> --- a/mm/vmalloc.h
>> +++ b/mm/vmalloc.h
>> @@ -40,7 +40,8 @@ void clear_vm_uninitialized_flag(struct vm_struct *vm);
>>
>> int __must_check __vmap_pages_range_noflush(unsigned long addr,
>> unsigned long end, pgprot_t prot,
>> - struct page **pages, unsigned int page_shift);
>> + struct page **pages, unsigned int page_shift,
>> + gfp_t gfp_mask);
>>
>> void vunmap_range_noflush(unsigned long start, unsigned long end);
>>
>> --
>> 2.25.1
>>
next prev parent reply other threads:[~2026-11-23 10:50 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 7:13 [PATCH v5 00/11] mm/vmalloc: fix up vmap error handling Hao Ge
2026-11-16 7:13 ` [PATCH v5 01/11] mm/vmalloc: skip cond_resched() in the vmap unmap paths when atomic Hao Ge
2026-11-18 19:00 ` Uladzislau Rezki
2026-11-19 5:02 ` Dev Jain
2026-11-20 21:07 ` Suren Baghdasaryan
2026-11-23 6:17 ` Hao Ge
2026-11-16 7:13 ` [PATCH v5 02/11] mm/kmsan: undo the shadow mapping when the origin mapping fails Hao Ge
2026-11-20 21:14 ` Suren Baghdasaryan
2026-11-20 21:49 ` Suren Baghdasaryan
2026-11-23 8:12 ` Hao Ge
2026-11-23 10:15 ` Hao Ge
2026-11-16 7:13 ` [PATCH v5 03/11] mm/vmalloc: reject an unaligned range in vmap_page_range() Hao Ge
2026-11-17 5:53 ` Dev Jain
2026-11-18 19:10 ` Uladzislau Rezki
2026-11-20 22:06 ` Suren Baghdasaryan
2026-11-16 7:13 ` [PATCH v5 04/11] mm/vmalloc: reject an unaligned range in __vunmap_range_noflush() Hao Ge
2026-11-17 6:05 ` Dev Jain
2026-11-18 19:16 ` Uladzislau Rezki
2026-11-20 22:13 ` Suren Baghdasaryan
2026-11-23 8:24 ` Hao Ge
2026-11-16 7:13 ` [PATCH v5 05/11] mm/vmalloc: only flush the cache after the vmap succeeded Hao Ge
2026-11-18 19:31 ` Uladzislau Rezki
2026-11-20 22:24 ` Suren Baghdasaryan
2026-11-16 7:13 ` [PATCH v5 06/11] mm/vmalloc: undo what vmap_page_range() mapped on failure Hao Ge
2026-11-18 19:50 ` Uladzislau Rezki
2026-11-23 9:39 ` Hao Ge
2026-11-20 22:29 ` Suren Baghdasaryan
2026-11-16 7:13 ` [PATCH v5 07/11] mm/vmalloc: undo what __vmap_pages_range_noflush() " Hao Ge
2026-11-22 3:58 ` Suren Baghdasaryan
2026-11-23 10:50 ` Hao Ge [this message]
2026-11-16 7:13 ` [PATCH v5 08/11] mm/vmalloc: drop the KMSAN metadata in vmap_pages_range_noflush() " Hao Ge
2026-11-22 4:04 ` Suren Baghdasaryan
2026-11-16 7:13 ` [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped " Hao Ge
2026-11-22 4:13 ` Suren Baghdasaryan
2026-11-23 11:32 ` Hao Ge
2026-11-16 7:13 ` [PATCH v5 10/11] powerpc: drop redundant unmaps of failed vmap mappings Hao Ge
2026-11-16 7:13 ` [PATCH v5 11/11] mm/percpu: stop unmapping the CPU that failed to map Hao Ge
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=7fad5338-7e9e-4fcf-b369-20f80639a9e4@linux.dev \
--to=hao.ge@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=chleroy@kernel.org \
--cc=cl@gentwo.org \
--cc=dennis@kernel.org \
--cc=dev.jain@arm.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=mpe@ellerman.id.au \
--cc=npiggin@gmail.com \
--cc=ritesh.list@gmail.com \
--cc=sshegde@linux.ibm.com \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--cc=urezki@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