[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <13b41a39-cdb1-4537-b4c8-940674c5875d@linux.dev>
Date: Fri, 17 Oct 2025 16:47:20 +0800
From: Lance Yang <lance.yang@...ux.dev>
To: David Hildenbrand <david@...hat.com>, Wei Yang
<richard.weiyang@...il.com>, Dev Jain <dev.jain@....com>
Cc: akpm@...ux-foundation.org, lorenzo.stoakes@...cle.com, ziy@...dia.com,
baolin.wang@...ux.alibaba.com, Liam.Howlett@...cle.com, npache@...hat.com,
ryan.roberts@....com, baohua@...nel.org, ioworker0@...il.com,
linux-kernel@...r.kernel.org, linux-mm@...ck.org
Subject: Re: [PATCH mm-new 1/1] mm/khugepaged: guard is_zero_pfn() calls with
pte_present()
On 2025/10/17 16:43, David Hildenbrand wrote:
> On 17.10.25 10:37, Lance Yang wrote:
>>
>>
>> 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@...ux.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@...cle.com>
>>>>>> Signed-off-by: Lance Yang <lance.yang@...ux.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.
>
> I lost track of what the result will be, so a v2 would be nice at least
> for me :)
Sure. V2 on the way ;)
Powered by blists - more mailing lists