From: David Hildenbrand <david@redhat.com>
To: "Petr Vaněk" <arkamar@atlas.cz>
Cc: linux-kernel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Ryan Roberts <ryan.roberts@arm.com>,
linux-mm@kvack.org, stable@vger.kernel.org
Subject: Re: [PATCH 1/1] mm: Fix folio_pte_batch() overcount with zero PTEs
Date: Tue, 29 Apr 2025 20:56:03 +0200 [thread overview]
Message-ID: <1df577bb-eaba-4e34-9050-309ee1c7dc57@redhat.com> (raw)
In-Reply-To: <2025429183321-aBEbcQQY3WX6dsNI-arkamar@atlas.cz>
On 29.04.25 20:33, Petr Vaněk wrote:
> On Tue, Apr 29, 2025 at 05:45:53PM +0200, David Hildenbrand wrote:
>> On 29.04.25 16:52, David Hildenbrand wrote:
>>> On 29.04.25 16:45, Petr Vaněk wrote:
>>>> On Tue, Apr 29, 2025 at 04:29:30PM +0200, David Hildenbrand wrote:
>>>>> On 29.04.25 16:22, Petr Vaněk wrote:
>>>>>> folio_pte_batch() could overcount the number of contiguous PTEs when
>>>>>> pte_advance_pfn() returns a zero-valued PTE and the following PTE in
>>>>>> memory also happens to be zero. The loop doesn't break in such a case
>>>>>> because pte_same() returns true, and the batch size is advanced by one
>>>>>> more than it should be.
>>>>>>
>>>>>> To fix this, bail out early if a non-present PTE is encountered,
>>>>>> preventing the invalid comparison.
>>>>>>
>>>>>> This issue started to appear after commit 10ebac4f95e7 ("mm/memory:
>>>>>> optimize unmap/zap with PTE-mapped THP") and was discovered via git
>>>>>> bisect.
>>>>>>
>>>>>> Fixes: 10ebac4f95e7 ("mm/memory: optimize unmap/zap with PTE-mapped THP")
>>>>>> Cc: stable@vger.kernel.org
>>>>>> Signed-off-by: Petr Vaněk <arkamar@atlas.cz>
>>>>>> ---
>>>>>> mm/internal.h | 2 ++
>>>>>> 1 file changed, 2 insertions(+)
>>>>>>
>>>>>> diff --git a/mm/internal.h b/mm/internal.h
>>>>>> index e9695baa5922..c181fe2bac9d 100644
>>>>>> --- a/mm/internal.h
>>>>>> +++ b/mm/internal.h
>>>>>> @@ -279,6 +279,8 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr,
>>>>>> dirty = !!pte_dirty(pte);
>>>>>> pte = __pte_batch_clear_ignored(pte, flags);
>>>>>>
>>>>>> + if (!pte_present(pte))
>>>>>> + break;
>>>>>> if (!pte_same(pte, expected_pte))
>>>>>> break;
>>>>>
>>>>> How could pte_same() suddenly match on a present and non-present PTE.
>>>>
>>>> In the problematic case pte.pte == 0 and expected_pte.pte == 0 as well.
>>>> pte_same() returns a.pte == b.pte -> 0 == 0. Both are non-present PTEs.
>>>
>>> Observe that folio_pte_batch() was called *with a present pte*.
>>>
>>> do_zap_pte_range()
>>> if (pte_present(ptent))
>>> zap_present_ptes()
>>> folio_pte_batch()
>>>
>>> How can we end up with an expected_pte that is !present, if it is based
>>> on the provided pte that *is present* and we only used pte_advance_pfn()
>>> to advance the pfn?
>>
>> I've been staring at the code for too long and don't see the issue.
>>
>> We even have
>>
>> VM_WARN_ON_FOLIO(!pte_present(pte), folio);
>>
>> So the initial pteval we got is present.
>>
>> I don't see how
>>
>> nr = pte_batch_hint(start_ptep, pte);
>> expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags);
>>
>> would suddenly result in !pte_present(expected_pte).
>
> The issue is not happening in __pte_batch_clear_ignored but later in
> following line:
>
> expected_pte = pte_advance_pfn(expected_pte, nr);
>
> The issue seems to be in __pte function which converts PTE value to
> pte_t in pte_advance_pfn, because warnings disappears when I change the
> line to
>
> expected_pte = (pte_t){ .pte = pte_val(expected_pte) + (nr << PFN_PTE_SHIFT) };
>
> The kernel probably uses __pte function from
> arch/x86/include/asm/paravirt.h because it is configured with
> CONFIG_PARAVIRT=y:
>
> static inline pte_t __pte(pteval_t val)
> {
> return (pte_t) { PVOP_ALT_CALLEE1(pteval_t, mmu.make_pte, val,
> "mov %%rdi, %%rax", ALT_NOT_XEN) };
> }
>
> I guess it might cause this weird magic, but I need more time to
> understand what it does :)
What XEN does with basic primitives that convert between pteval and
pte_t is beyond horrible.
How come set_ptes() that uses pte_next_pfn()->pte_advance_pfn() does not
run into this?
Is it only a problem if we exceed a certain pfn?
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2025-04-29 18:56 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-29 14:22 [PATCH 0/1] mm: Fix regression after THP PTE optimization on Xen PV Dom0 Petr Vaněk
2025-04-29 14:22 ` [PATCH 1/1] mm: Fix folio_pte_batch() overcount with zero PTEs Petr Vaněk
2025-04-29 14:29 ` David Hildenbrand
2025-04-29 14:41 ` Ryan Roberts
2025-04-29 14:46 ` David Hildenbrand
2025-04-29 15:02 ` Ryan Roberts
2025-04-30 13:04 ` Petr Vaněk
2025-04-30 13:21 ` Ryan Roberts
2025-04-29 14:45 ` Petr Vaněk
2025-04-29 14:52 ` David Hildenbrand
2025-04-29 15:45 ` David Hildenbrand
2025-04-29 18:33 ` Petr Vaněk
2025-04-29 18:56 ` David Hildenbrand [this message]
2025-04-30 11:52 ` Petr Vaněk
2025-04-30 14:37 ` David Hildenbrand
2025-04-30 16:00 ` Petr Vaněk
2025-04-30 21:25 ` David Hildenbrand
2025-05-01 7:45 ` Petr Vaněk
2025-05-02 7:37 ` David Hildenbrand
2025-05-02 9:42 ` David Hildenbrand
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=1df577bb-eaba-4e34-9050-309ee1c7dc57@redhat.com \
--to=david@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=arkamar@atlas.cz \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryan.roberts@arm.com \
--cc=stable@vger.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