Re: [PATCH v5 08/11] mm/vmalloc: drop the KMSAN metadata in vmap_pages_range_noflush() on failure

From: Suren Baghdasaryan

Date: Sat Nov 21 2026 - 23:05:15 EST


On Sun, Nov 15, 2026 at 11:14 PM Hao Ge <hao.ge@xxxxxxxxx> wrote:
>
> vmap_pages_range_noflush() sets up the KMSAN metadata for the whole
> range before it starts mapping. If the mapping fails, the page
> tables undo themselves now, but the metadata stays behind, and the
> next attempt to map the same range hits the leftover shadow PTEs.
>
> Tear the metadata down on the error path, it was fully set up before
> the mapping failed. The teardown follows the page tables and skips
> cond_resched() when the caller's gfp mask cannot block.

I think this patch should also be folded into patch 7. Leaving the
metadata after unmapping a failed partial mapping introduces an
inconsistency, which this patch fixes. So, in a sense you are fixing
an inconsistency that you created in the patch 7. Instead of
introducing an issue and then fixing it let's avoid introducing it in
the first place. That's what merging those patches will achieve.

>
> Signed-off-by: Hao Ge <hao.ge@xxxxxxxxx>
> ---
> mm/vmalloc.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
> index 4f3c320a4849..28e65073624a 100644
> --- a/mm/vmalloc.c
> +++ b/mm/vmalloc.c
> @@ -801,8 +801,18 @@ 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,
> - gfp_mask);
> +
> + ret = __vmap_pages_range_noflush(addr, end, prot, pages, page_shift,
> + gfp_mask);
> + /*
> + * The page tables undo themselves on failure. Tear down the
> + * metadata that was fully set up before the mapping failed.
> + */
> + if (ret)
> + kmsan_vunmap_range_noflush(addr, end,
> + gfpflags_allow_blocking(gfp_mask));
> +
> + return ret;
> }
>
> static int __vmap_pages_range(unsigned long addr, unsigned long end,
> --
> 2.25.1
>