From: Wei Yang <richard.weiyang@gmail.com>
To: Dev Jain <dev.jain@arm.com>
Cc: Wei Yang <richard.weiyang@gmail.com>,
akpm@linux-foundation.org, david@kernel.org,
lorenzo.stoakes@oracle.com, riel@surriel.com,
Liam.Howlett@oracle.com, vbabka@kernel.org, harry.yoo@oracle.com,
jannh@google.com, baohua@kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, ryan.roberts@arm.com,
anshuman.khandual@arm.com, stable <stable@kernel.org>
Subject: Re: [PATCH v3] mm/rmap: fix incorrect pte restoration for lazyfree folios
Date: Tue, 3 Mar 2026 12:50:10 +0000 [thread overview]
Message-ID: <20260303125010.54j3bmu3joixezkk@master> (raw)
In-Reply-To: <257aa379-64cf-47af-b48d-8817f7bca257@arm.com>
On Tue, Mar 03, 2026 at 05:55:58PM +0530, Dev Jain wrote:
[...]
>>>
>>> Fixes: 354dffd29575 ("mm: support batched unmap for lazyfree large folios during reclamation")
>>> Cc: stable <stable@kernel.org>
>>> Signed-off-by: Dev Jain <dev.jain@arm.com>
>>> ---
>>> Patch applies on mm-unstable (9af4957ef127).
>>>
>>> v2->v3:
>>> - Don't special case for anon folios
>>>
>>> v1->v2:
>>> - Just respect the writable bit instead of hacking in a pte_wrprotect() in
>>> failure path
>>> - Also handle soft-dirty bit
>>>
>>> mm/rmap.c | 9 ++++++++-
>>> 1 file changed, 8 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/mm/rmap.c b/mm/rmap.c
>>> index bff8f222004e4..5a3e408e3f179 100644
>>> --- a/mm/rmap.c
>>> +++ b/mm/rmap.c
>>> @@ -1955,7 +1955,14 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio,
>>> if (userfaultfd_wp(vma))
>>> return 1;
>>>
>>> - return folio_pte_batch(folio, pvmw->pte, pte, max_nr);
>>> + /*
>>> + * If unmap fails, we need to restore the ptes. To avoid accidentally
>>> + * upgrading write permissions for ptes that were not originally
>>> + * writable, and to avoid losing the soft-dirty bit, use the
>>> + * appropriate FPB flags.
>>> + */
>>> + return folio_pte_batch_flags(folio, vma, pvmw->pte, &pte, max_nr,
>>> + FPB_RESPECT_WRITE | FPB_RESPECT_SOFT_DIRTY);
>>> }
>>>
>>
>> Hi, Dev
>>
>> When reading the code, I got one confusion. Current call flow is like below:
>>
>> try_to_unmap_one();
>> nr_pages = folio_unmap_pte_batch(folio, &pvmw, flags, pteval);
>> ..
>> pteval = get_and_clear_ptes(mm, address, pvmw.pte, nr_pages);
>> ..
>> set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
>>
>> We get pteval by folio_unmap_pte_batch() but it is set again by
>
>folio_unmap_pte_batch() gives the batch size, not pteval. pteval is
>given by get_and_clear_ptes() after accumulating a/d bits.
>
>> get_and_clear_ptes(), which maybe a different value. Then we use this pteval
>> to restore ptes.
>>
>> So even we fix folio_unmap_pte_batch(), how this impact on the final restored
>> value?
>
>By respecting writable bit, we ensure that the ptes in the batch do not
>have a mix of writable and non writable ptes.
>
Thanks for your explanation. I got it.
Then the fix LGTM.
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
>So, if pteval returned by get_and_clear_ptes() is writable, then it is
>guaranteed via folio_unmap_pte_batch() that the all pte values of
>these nr_pages consecutive ptes, are writable. And vice versa.
>
>>
>> Hope I don't miss something.
>>
>>> /*
>>> --
>>> 2.34.1
>>>
>>
--
Wei Yang
Help you, Help me
prev parent reply other threads:[~2026-03-03 12:50 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-03 6:15 Dev Jain
2026-03-03 8:50 ` David Hildenbrand (Arm)
2026-03-03 9:54 ` Lorenzo Stoakes
2026-03-03 10:22 ` Dev Jain
2026-03-03 9:57 ` Barry Song
2026-03-03 10:32 ` Dev Jain
2026-03-03 12:17 ` Wei Yang
2026-03-03 12:25 ` Dev Jain
2026-03-03 12:50 ` Wei Yang [this message]
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=20260303125010.54j3bmu3joixezkk@master \
--to=richard.weiyang@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=anshuman.khandual@arm.com \
--cc=baohua@kernel.org \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=harry.yoo@oracle.com \
--cc=jannh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=riel@surriel.com \
--cc=ryan.roberts@arm.com \
--cc=stable@kernel.org \
--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