From: "David Hildenbrand (Arm)" <david@kernel.org>
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>,
Ying Huang <huang.ying.caritas@gmail.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
Barry Song <baohua@kernel.org>,
Ryan Roberts <ryan.roberts@arm.com>,
Chuanhua Han <hanchuanhua@oppo.com>,
Kiryl Shutsemau <kas@kernel.org>, Kyle Zeng <kylebot@openai.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] mm: fix address wraparound when scanning large folio PTEs
Date: Wed, 14 Oct 2026 10:12:06 +0200 [thread overview]
Message-ID: <9add8400-66c1-47e9-b5af-7a4081ebf3c4@kernel.org> (raw)
In-Reply-To: <as4kvHe9IFZH5qrC@gremlin>
>> Reported-by: Kyle Zeng <kylebot@openai.com>
>> Fixes: d2136d749d76 ("mm: support multi-size THP numa balancing")
>> Fixes: 508758960b8d ("mm: swap: entirely map large folios found in swapcache")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>> ---
>> mm/memory.c | 43 +++++++++++++++++++++++++++++--------------
>> 1 file changed, 29 insertions(+), 14 deletions(-)
>>
>> diff --git a/mm/memory.c b/mm/memory.c
>> index 1f5d5f7d39cd2..a3823010aa981 100644
>> --- a/mm/memory.c
>> +++ b/mm/memory.c
>> @@ -4933,6 +4933,27 @@ static void check_swap_exclusive(struct folio *folio, swp_entry_t entry,
>> } while (--nr_pages);
>> }
>>
>> +/*
>> + * Get the address range in which a folio might be mapped, assuming its pages
>> + * are mapped consecutively and the folio page at the given idx is mapped at
>> + * the given address.
>> + */
>
> I know it's a static function but kdoc here might be nice given the number of
> params, including 2 output parameters.
I had that, but decided "not worth it for an internal helper".
Is anything here really that confusing to understand in mm/memory.c context? I
highly doubt it.
>
> Also then can specify exclusive end, etc.
>
>> +static void folio_pte_scan_range(const struct folio *folio,
>> + const struct vm_area_struct *vma, unsigned long addr,
>> + unsigned long idx, unsigned long *start, unsigned long *end)
>
> I don't love the name - to me it implies to me that you're scanning the range.
>
> Maybe clamp_folio_pte_range()? I guess though clamp implies it limits an input
> range.
Ugly.
>
> Maybe get_folio_pte_range()?
I can just call it "folio_pte_range()". Slightly similar (but different) to
"folio_pte_batch()"
>
> Also I think it might be useful to have it return the number of pages contained
> within the range?
>
> Then this below:
>
> + folio_pte_scan_range(folio, vma, address, idx, &folio_start,
> + &folio_end);
> + if (unlikely((folio_end - folio_start) >> PAGE_SHIFT != nr))
>
> Could be:
>
> + if (unlikely(get_folio_pte_range(folio, vma, address, idx,
> + &folio_start, &folio_end)) != nr)
>
> OTOH, that's pretty ugly... :))
Not a fan of these semantics.
>
>> +{
>> + /* Stay within the page table and VMA boundaries. */
>> + const unsigned long pmd_start = addr & PMD_MASK;
>> + const unsigned long pmd_end = pmd_start + PMD_SIZE;
>
> OK so get the range of the PTE page table.
>
>> + const unsigned long lower = max(pmd_start, vma->vm_start);
>
> Then clamp to VMA start...
>
>> + const unsigned long upper = min(pmd_end - 1, vma->vm_end - 1) + 1;
>
> This is trickier :)
>
> I guess you're doing this in case either bound is right at wraparound
> somehow?
>
> But then couldn't the +1 push you over the edge?
>
> Oh wait no, because then vma->vm_end would have to equal 0, which would
> fail the other-place enforced invariant that vma->vm_start < vma->vm_end.
>
> So it's pmd_end that could overflow, hence the -1, +1 dance.
>
Yes. It's fairly common, though, to handle clamping of exclusive ends that way.
> Yeah I kind of hate that, that's a lot of 'just have to know' right there.
>
> Actually we have pmd_addr_end() already, could we use that?
>
> #define pmd_addr_end(addr, end) \
> ({ unsigned long __boundary = ((addr) + PMD_SIZE) & PMD_MASK; \
> (__boundary - 1 < (end) - 1)? __boundary: (end); \
> })
As replied elsewhere, I had that, but decided to open-code as pmd_addr_end() has
very weird semantics with folded page tables, where we just don't clamp at all.
I'll sleep over it, but to me using pmd_addr_end() is not particularly better.
>
> That embeds the trick in the helper and it expresses what you're getting.
>
> Then it could be:
>
> const unsigned long pmd_start = addr & PMD_MASK;
> const unsigned long lower = max(pmd_start, vma->vm_start);
> const unsigned long upper = pmd_addr_end(addr, vma->vm_end);
>
>> + const unsigned long offset = idx << PAGE_SHIFT;
>> +
>> + /* Avoid any address wraparounds. */
>> + *start = addr - min(addr - lower, offset);
>
> This is also tricky. This is clamping to max(lower, addr - offset) right,
> but being very careful about underflow?
>
> I don't love the addr - (addr - lower) = lower here, and it feels like it
> embeds a comparison there too. It's all very implicit.
>
> Actually maybe just naming addr - lower gets us there:
>
> const unsigned long max_offset = addr - lower;
> ...
> *start = addr - min(max_offset, offset);
>
> There's check_sub_overflow() or you could use a ternary also but both feel
> like they add more complexity than is useful.
>
>> + *end = addr + min(upper - addr, folio_size(folio) - offset);
>
> Same deal here.
>
> Maybe something like:
>
> const unsigned long max_remaining_bytes = upper - addr;
> ...
> *end = addr + min(max_remaining_bytes, folio_size(folio) - offset);
>
Sorry, to push back, but are we overthinking this helper here?
I much rather get this fixed quickly then fine tuning some details in a helper
with clear semantics that anybody can understand when looking at it for 1 minute?
--
Cheers,
David
next prev parent reply other threads:[~2026-10-14 8:12 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-13 12:30 [PATCH] mm: fix address wraparound when scanning large folio PTEs David Hildenbrand (Arm)
2026-10-13 12:57 ` Kiryl Shutsemau
2026-10-13 13:17 ` Barry Song
2026-10-14 8:03 ` David Hildenbrand (Arm)
2026-10-14 8:04 ` David Hildenbrand (Arm)
2026-10-13 14:11 ` Lorenzo Stoakes (ARM)
2026-10-14 8:12 ` David Hildenbrand (Arm) [this message]
2026-10-14 13:27 ` Lorenzo Stoakes (ARM)
2026-10-14 13:45 ` David Hildenbrand (Arm)
2026-10-14 13:49 ` David Hildenbrand (Arm)
2026-10-14 14:57 ` Lorenzo Stoakes (ARM)
2026-10-14 14:56 ` Lorenzo Stoakes (ARM)
2026-10-14 6:02 ` Baolin Wang
2026-10-14 8:02 ` David Hildenbrand (Arm)
2026-10-14 16:30 ` Lance Yang
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=9add8400-66c1-47e9-b5af-7a4081ebf3c4@kernel.org \
--to=david@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=hanchuanhua@oppo.com \
--cc=huang.ying.caritas@gmail.com \
--cc=kas@kernel.org \
--cc=kylebot@openai.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=stable@vger.kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
/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