mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Dev Jain <dev.jain@arm.com>
To: Hao Ge <hao.ge@linux.dev>
Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org,
	kasan-dev@googlegroups.com, linux-mm@kvack.org,
	Sashiko <sashiko-bot@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>,
	Andrew Morton <akpm@linux-foundation.org>,
	Dennis Zhou <dennis@kernel.org>, Tejun Heo <tj@kernel.org>,
	Christoph Lameter <cl@gentwo.org>,
	Uladzislau Rezki <urezki@gmail.com>,
	Suren Baghdasaryan <surenb@google.com>,
	Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>
Subject: Re: [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range()
Date: Sat, 14 Nov 2026 12:22:11 +0530	[thread overview]
Message-ID: <91baeed5-c432-445b-a2db-7bc20c8921b0@arm.com> (raw)
In-Reply-To: <72d1b1d7-2fb6-402f-a557-0eaa767d6f86@linux.dev>



On 02/11/26 11:37 am, Hao Ge wrote:
> 
> 
> On 2026/10/30 22:20, Dev Jain wrote:
>>
>>
>> On 30/10/26 2:00 pm, Hao Ge wrote:
>>> Hi Dev
>>>
>>> Thanks for your review.
>>>
>>> On 2026/10/30 15:14, Dev Jain wrote:
>>>>
>>>>
>>>> On 26/10/26 1:33 pm, Hao Ge wrote:
>>>>> commit a739e6b557af ("mm: vmalloc: WARN_ON if mapping size is not
>>>>> PAGE_SIZE aligned") made vmap_pte_range() warn and bail out when the
>>>>> size is not a multiple of PAGE_SIZE. That loop steps in page
>>>>
>>>> Why are we checking the alignment so late into vmap_pte_range? Why not
>>>> simply do
>>>>
>>>> if (!IS_ALIGNED(addr, PAGE_SIZE) || !IS_ALIGNED(end, PAGE_SIZE))
>>>> 	return; (or return -EINVAL);
>>>>
>>>> at the start of vmap_range_noflush and vunmap_range_noflush?
>>>
>>> I think adding that check at the entry of vunmap_range_noflush()
>>> goes against its design intent. Its job is to tear down the mappings
>>> of the given VA range, so even when the caller passes a size that is
>>> not page-aligned, we still have to unmap whatever is mapped within
>>> that range.
>>>
>>> So the patch clamps end down to the last full page and keeps
>>> clearing. A buggy caller still gets its mapping torn down as far as
>>> it goes, and nothing is left behind. The check here is just for
>>> protection. 
>>>
>>> This is just my preliminary understanding; I may be mistaken.
>>
>> Hmm I get you, but this is very ugly. We are trying to tolerate
>> a blatant bug in the caller with not-so-nice code. But see below.
>>>
>>> Thanks
>>> Best Regards
>>> Hao
>>>
>>>
>>> But that's just my understanding, happy to be corrected.
>>>
>>>
>>>>
>>>>
>>>>> multiples and stops only at addr == end; with an unaligned size addr
>>>>> never lands on end, so the loop cannot terminate and walks off the
>>>>> end of the page table.
>>>>>
>>>>> vunmap_pte_range() has the same loop without any guard, so an
>>>>> unaligned range would clear PTEs past end without ever stopping,
>>>>> walking off the end of the page table.
>>>>>
>>>>> Add the check there too. The range is clamped down to the last full
>>>>> page rather than dropped, and the loop is skipped when nothing is
>>>>> left, so a buggy caller still gets its PTEs cleared as far as they
>>>>> go.
>>>>>
>>>>> Reported-by: Sashiko <sashiko-bot@kernel.org>
>>>>> Signed-off-by: Hao Ge <hao.ge@linux.dev>
>>>>> ---
>>>>>  mm/vmalloc.c | 5 +++++
>>>>>  1 file changed, 5 insertions(+)
>>>>>
>>>>> diff --git a/mm/vmalloc.c b/mm/vmalloc.c
>>>>> index 44e0eb32708f..7131f60d1a8e 100644
>>>>> --- a/mm/vmalloc.c
>>>>> +++ b/mm/vmalloc.c
>>>>> @@ -393,6 +393,11 @@ static void vunmap_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
>>>>>  	pte_t ptent;
>>>>>  	unsigned long size = PAGE_SIZE;
>>>>>  
>>>>> +	if (WARN_ON_ONCE(!PAGE_ALIGNED(end - addr)))
>>>>> +		end = addr + PAGE_ALIGN_DOWN(end - addr);
>>
>> For simplicity assume PAGE_SIZE is 4, start is 3, end is 10.
>> With your code we will get
>>
>> addr = 3, end = 3 + ALIGN_DOWN(10 - 3) = 3 + 4 = 7.
>> So the range becomes [3, 7) clearing only two ptes, the one covering
>> [0, 3], the second covering [4, 7]. I feel we should rather make
>> the range from [3, 10] to [0, 11], clearing three ptes.
>>
> 
> Thanks for the example. Looks like I nearly introduced a new bug while
> guarding against the old one, good thing you caught it.
> 
>> So, at the start of vunmap_range_noflush we could basically
>> WARN_ON on non-alignment, and then clamp the start down, and clamp
>> the end up to PAGE_SIZE.
> 
> I'm wondering whether we should simply BUG_ON for non-aligned ranges here.
> Because the unmap could silently clear mappings at adjacent addresses, which
> may not even belong to the caller, and this kind of problem is really hard to
> track down afterwards. Say [0, 3) is in use by someone else, with PAGE_SIZE=4
> an unmap starting at 3 still begins with the pte covering [0, 4), since the loop
> clears page by page, so that owner loses his mapping without doing anything wrong,
> and only sees the damage much later when he touches it again.
> 
> WDYT?
> 
>>
>> And with this also throw in a patch to bring the -EINVAL bailout
>> in vmap_pte_range to the top, in vmap_range_noflush and check
>> both start and end alignment.
> 
> For the vmap_range_noflush() check, fully agreed.  The current check
> is indeed too late, the range is only found to be bad after part of
> it has already been mapped. 
> 
> Looking forward to your valuable comments.

I was on vacation, I'll review the next version soon.


> 
> Thanks
> Best Regards
> Hao
>>
>>>>> +	if (end == addr)
>>>>> +		return;
>>>>> +
>>>>>  	pte = pte_offset_kernel(pmd, addr);
>>>>>  	lazy_mmu_mode_enable();
>>>>>  
>>>>
>>


  reply	other threads:[~2026-11-14  6:52 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-26  8:03 [PATCH v3 00/10] mm/vmalloc: fix up vmap error handling Hao Ge
2026-10-26  8:03 ` [PATCH v3 01/10] mm/kmsan: undo the shadow mapping when the origin mapping fails Hao Ge
2026-10-26  8:03 ` [PATCH v3 02/10] mm/kmsan: skip vunmap_range_noflush when KMSAN is disabled Hao Ge
2026-10-26  8:03 ` [PATCH v3 03/10] mm/vmalloc: clamp an unaligned range in vunmap_pte_range() Hao Ge
2026-10-30  7:14   ` Dev Jain
2026-10-30  8:30     ` Hao Ge
2026-10-30 14:20       ` Dev Jain
2026-11-02  6:07         ` Hao Ge
2026-11-14  6:52           ` Dev Jain [this message]
2026-10-26  8:03 ` [PATCH v3 04/10] mm/vmalloc: propagate errors in vmap_pages_p*d_range() Hao Ge
2026-10-30  7:29   ` Dev Jain
2026-10-30  9:18     ` Hao Ge
2026-11-01 16:00       ` Suren Baghdasaryan
2026-11-11 17:23         ` Uladzislau Rezki
2026-10-26  8:03 ` [PATCH v3 05/10] mm/vmalloc: reject an unaligned range in vmap_pages_pte_range() Hao Ge
2026-10-26  8:03 ` [PATCH v3 06/10] mm/vmalloc: reject empty and unaligned ranges in __vmap_pages_range_noflush() Hao Ge
2026-10-26  8:03 ` [PATCH v3 07/10] mm/vmalloc: only flush the cache after the vmap succeeded Hao Ge
2026-10-26  8:03 ` [PATCH v3 08/10] mm/vmalloc: undo partial mappings inside the mapping functions Hao Ge
2026-10-26  8:03 ` [PATCH v3 09/10] powerpc: drop redundant unmaps of failed vmap mappings Hao Ge
2026-10-26  8:03 ` [PATCH v3 10/10] 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=91baeed5-c432-445b-a2db-7bc20c8921b0@arm.com \
    --to=dev.jain@arm.com \
    --cc=akpm@linux-foundation.org \
    --cc=chleroy@kernel.org \
    --cc=cl@gentwo.org \
    --cc=dennis@kernel.org \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=hao.ge@linux.dev \
    --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=sashiko-bot@kernel.org \
    --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

all inboxes | Powered by JetHome®