Re: [PATCH v5 09/11] mm/vmalloc: undo what vmap_pages_range_batched() mapped on failure
From: Hao Ge
Date: Wed Nov 25 2026 - 04:31:40 EST
Hi Suren
On 2026/11/24 00:13, Suren Baghdasaryan wrote:
> On Mon, Nov 23, 2026 at 3:32 AM Hao Ge <hao.ge@xxxxxxxxx> wrote:
>>
>> Hi Suren
>>
>>
>> On 2026/11/22 12:13, Suren Baghdasaryan wrote:
>>> On Sun, Nov 15, 2026 at 11:14 PM Hao Ge <hao.ge@xxxxxxxxx> wrote:
>>>>
>>>> vmap_pages_range_batched() maps the range batch by batch. When a
>>>> batch fails, the batches before it are installed, and the failing
>>>> one may have installed some of its pages. mapped_end follows the
>>>> end of the last installed page, and the error path undoes
>>>> [addr, mapped_end), never past the point where the walk stopped.
>>>>
>>>> The KMSAN metadata is set up for the whole range up front, so it is
>>>> torn down as a whole when the mapping fails, like in
>>>> vmap_pages_range_noflush(). The only caller, vmap(), also calls
>>>> vunmap() on its error path; the double teardown only walks over PTEs
>>>> that are already cleared.
>>>
>>> As I mentioned before I think this patch should be merged with patch 7.
>>
>> Ack.
>>
>>>
>>>>
>>>> Signed-off-by: Hao Ge <hao.ge@xxxxxxxxx>
>>>> ---
>>>> mm/vmalloc.c | 22 ++++++++++++++++------
>>>> 1 file changed, 16 insertions(+), 6 deletions(-)
>>>>
>>>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
>>>> index 28e65073624a..aa80bc273ec0 100644
>>>> --- a/mm/vmalloc.c
>>>> +++ b/mm/vmalloc.c
>>>> @@ -3748,13 +3748,14 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
>>>> const unsigned int nr_pages = (end - addr) >> PAGE_SHIFT;
>>>> unsigned int prev_shift = 0, batch_idx = 0, nr_mapped;
>>>> unsigned long batch_start = addr, batch_end = addr;
>>>> + unsigned long mapped_end = addr;
>>>> int err;
>>>>
>>>> err = kmsan_vmap_pages_range_noflush(addr, end, prot, pages,
>>>> PAGE_SHIFT, GFP_KERNEL);
>>>>
>>>> if (err)
>>>> - goto out;
>>>> + return err;
>>>>
>>>> for (unsigned int i = 0; i < nr_pages; ) {
>>>> unsigned int shift = PAGE_SHIFT +
>>>> @@ -3764,9 +3765,10 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
>>>> prev_shift = shift;
>>>>
>>>> if (shift != prev_shift) {
>>>> - err = vmap_pages_range_noflush_walk(batch_start, batch_end,
>>>> - prot, pages + batch_idx, prev_shift,
>>>> - &nr_mapped);
>>>> + err = vmap_pages_range_noflush_walk(batch_start,
>>>> + batch_end, prot, pages + batch_idx,
>>>> + prev_shift, &nr_mapped);
>>>> + mapped_end = batch_start + (nr_mapped * PAGE_SIZE);
>>>> if (err)
>>>> goto out;
>>>> prev_shift = shift;
>>>> @@ -3786,13 +3788,21 @@ static int vmap_pages_range_batched(unsigned long addr, unsigned long end,
>>>> }
>>>>
>>>> /* Remaining */
>>>> - if (batch_start < end)
>>>> + if (batch_start < end) {
>>>> err = vmap_pages_range_noflush_walk(batch_start, end, prot,
>>>> pages + batch_idx, prev_shift, &nr_mapped);
>>>> + mapped_end = batch_start + (nr_mapped * PAGE_SIZE);
>>>> + }
>>>>
>>>> out:
>>>> - if (!err)
>>>> + if (err) {
>>>> + /* Undo the part the batches installed before they failed. */
>>>> + if (mapped_end > addr)
>>>> + __vunmap_range_noflush(addr, mapped_end, true);
>>>> + kmsan_vunmap_range_noflush(addr, end, true);
>>>
>>> So, you are unmapping (addr, mapped_end) range but removing KMSAN
>>> metadata for the entire (addr, end) range. Is that intended?
>>
>> Yes, that is intended. The metadata for the whole range is set up
>> before the mapping starts, kmsan_vmap_pages_range_noflush() at the top
>> of vmap_pages_range_batched() installs the shadow and origin pages for
>> the entire [addr, end) in one go. So on failure the metadata is fully
>> installed and has to go as a whole, while the page tables only reached
>> mapped_end and are undone up to there.
>>
>> I know this looks somewhat awkward. The asymmetry comes from the metadata
>> having to be set up before the mapping starts, I don't see a way to avoid it.
>> Maybe add a comment explaining this?
>
> Are there any side-effects like the one we found earlier with
> unmapping PTEs which we never installed?
I haven't found any so far. If I've missed a scenario, please
do let me know.
I remain unsure whether kmsan_vunmap_range_noflush() needs a
TLB flush - I already brought this up in the thread for
patch 2.
> I don't think a comment is needed as it's quite obvious.
>
OK.
Thanks again for your and Ulad'careful and patient review of this series.
I recognise this version is a bit messy - I had a lot going on at the time,
and I apologise for that.
I'll take that into account and keep improving.
Thanks
Best Regards
Hao
>>
>> WDYT?
>>
>> Thanks
>> Best Regards
>> Hao
>>
>>>
>>>> + } else {
>>>> flush_cache_vmap(addr, end);
>>>> + }
>>>> return err;
>>>> }
>>>>
>>>> --
>>>> 2.25.1
>>>>