public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Lance Yang <lance.yang@linux.dev>
To: David Hildenbrand <david@redhat.com>,
	Wei Yang <richard.weiyang@gmail.com>, Dev Jain <dev.jain@arm.com>
Cc: akpm@linux-foundation.org, lorenzo.stoakes@oracle.com,
	ziy@nvidia.com, baolin.wang@linux.alibaba.com,
	Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com,
	baohua@kernel.org, ioworker0@gmail.com,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH mm-new 1/1] mm/khugepaged: guard is_zero_pfn() calls with pte_present()
Date: Fri, 17 Oct 2025 16:37:26 +0800	[thread overview]
Message-ID: <ab918a83-edb2-4a19-821d-a96de9e097eb@linux.dev> (raw)
In-Reply-To: <1674efca-6d4e-4247-8b1c-b6816360d8bb@redhat.com>



On 2025/10/17 16:11, David Hildenbrand wrote:
> On 17.10.25 03:27, Wei Yang wrote:
>> On Thu, Oct 16, 2025 at 11:47:06AM +0530, Dev Jain wrote:
>>>
>>> On 16/10/25 9:06 am, Lance Yang wrote:
>>>> From: Lance Yang <lance.yang@linux.dev>
>>>>
>>>> A non-present entry, like a swap PTE, contains completely different 
>>>> data
>>>> (swap type and offset). pte_pfn() doesn't know this, so if we feed it a
>>>> non-present entry, it will spit out a junk PFN.
>>>>
>>>> What if that junk PFN happens to match the zeropage's PFN by sheer
>>>> chance? While really unlikely, this would be really bad if it did.
>>>>
>>>> So, let's fix this potential bug by ensuring all calls to is_zero_pfn()
>>>> in khugepaged.c are properly guarded by a pte_present() check.
>>>>
>>>> Suggested-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
>>>> Signed-off-by: Lance Yang <lance.yang@linux.dev>
>>>> ---
>>>>    mm/khugepaged.c | 13 ++++++++-----
>>>>    1 file changed, 8 insertions(+), 5 deletions(-)
>>>>
>>>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
>>>> index d635d821f611..0341c3d13e9e 100644
>>>> --- a/mm/khugepaged.c
>>>> +++ b/mm/khugepaged.c
>>>> @@ -516,7 +516,7 @@ static void release_pte_pages(pte_t *pte, pte_t 
>>>> *_pte,
>>>>            pte_t pteval = ptep_get(_pte);
>>>>            unsigned long pfn;
>>>> -        if (pte_none(pteval))
>>>> +        if (!pte_present(pteval))
>>>>                continue;
>>>>            pfn = pte_pfn(pteval);
>>>>            if (is_zero_pfn(pfn))
>>>> @@ -690,9 +690,10 @@ static void 
>>>> __collapse_huge_page_copy_succeeded(pte_t *pte,
>>>>             address += nr_ptes * PAGE_SIZE) {
>>>>            nr_ptes = 1;
>>>>            pteval = ptep_get(_pte);
>>>> -        if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
>>>> +        if (pte_none(pteval) ||
>>>> +            (pte_present(pteval) && is_zero_pfn(pte_pfn(pteval)))) {
>>>>                add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
>>>> -            if (is_zero_pfn(pte_pfn(pteval))) {
>>>> +            if (!pte_none(pteval)) {
>>>
>>> Could save a level of indentation by saying
>>> if (pte_none(pteval))
>>>     continue;
>>>
>>
>> Vote for this :-)
> 
> I suspect there will be a v2, correct?

I was hoping a v2 wouldn't be necessary for this ;p

Of course, if we'd prefer a v2, I'm happy to send one out.

Cheers!

  reply	other threads:[~2025-10-17  8:37 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-16  3:36 [PATCH mm-new 1/1] mm/khugepaged: guard is_zero_pfn() calls with pte_present() Lance Yang
2025-10-16  5:34 ` Dev Jain
2025-10-16  5:59   ` Lance Yang
2025-10-16  6:15     ` Dev Jain
2025-10-16  6:17 ` Dev Jain
2025-10-16  6:26   ` Lance Yang
2025-10-17  1:27   ` Wei Yang
2025-10-17  8:11     ` David Hildenbrand
2025-10-17  8:37       ` Lance Yang [this message]
2025-10-17  8:43         ` David Hildenbrand
2025-10-17  8:47           ` Lance Yang
2025-10-17  9:35             ` Lorenzo Stoakes
2025-10-16  9:33 ` Wei Yang
2025-10-16 10:51   ` Lance Yang
2025-10-17  8:10 ` Baolin Wang

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=ab918a83-edb2-4a19-821d-a96de9e097eb@linux.dev \
    --to=lance.yang@linux.dev \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@redhat.com \
    --cc=dev.jain@arm.com \
    --cc=ioworker0@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=npache@redhat.com \
    --cc=richard.weiyang@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=ziy@nvidia.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