loongarch.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Bibo Mao <maobibo@loongson.cn>
To: Tianyang Zhang <zhangtianyang@loongson.cn>,
	chenhuacai@kernel.org, kernel@xen0n.name,
	akpm@linux-foundation.org, willy@infradead.org, david@redhat.com,
	linmag7@gmail.com, thuth@redhat.com, apopple@nvidia.com
Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org,
	Liupu Wang <wangliupu@loongson.cn>
Subject: Re: [PATCH] Loongarch:Make pte/pmd_modify can set _PAGE_MODIFIED
Date: Wed, 8 Apr 2026 16:10:58 +0800	[thread overview]
Message-ID: <db75dcb9-25ab-ddea-f00f-c4322c867a63@loongson.cn> (raw)
In-Reply-To: <721fdf6e-61c4-2e31-c584-04d8380f2952@loongson.cn>



On 2025/11/6 下午5:53, Tianyang Zhang wrote:
> 
> 在 2025/11/6 下午3:07, Bibo Mao 写道:
>>
>>
>> On 2025/11/4 下午3:30, Tianyang Zhang wrote:
>>> In the current pte_modify operation, _PAGE_DIRTY might be cleared. Since
>>> the hardware-page-walk does not have a predefined _PAGE_MODIFIED flag,
>>> this could lead to loss of valid data in certain scenarios.
>>>
>>> The new modification involves checking whether the original PTE has the
>>> _PAGE_DIRTY flag. If it exists, the _PAGE_MODIFIED bit is set, ensuring
>>> that the pte_dirty interface can return accurate information.
>>>
>>> Co-developed-by: Liupu Wang <wangliupu@loongson.cn>
>>> Signed-off-by: Liupu Wang <wangliupu@loongson.cn>
>>> Signed-off-by: Tianyang Zhang <zhangtianyang@loongson.cn>
>>> ---
>>>   arch/loongarch/include/asm/pgtable.h | 17 +++++++++++++----
>>>   1 file changed, 13 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/arch/loongarch/include/asm/pgtable.h 
>>> b/arch/loongarch/include/asm/pgtable.h
>>> index bd128696e96d..106abfa5183b 100644
>>> --- a/arch/loongarch/include/asm/pgtable.h
>>> +++ b/arch/loongarch/include/asm/pgtable.h
>>> @@ -424,8 +424,13 @@ static inline unsigned long 
>>> pte_accessible(struct mm_struct *mm, pte_t a)
>>>     static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
>>>   {
>>> -    return __pte((pte_val(pte) & _PAGE_CHG_MASK) |
>>> -             (pgprot_val(newprot) & ~_PAGE_CHG_MASK));
>>> +    unsigned long val = (pte_val(pte) & _PAGE_CHG_MASK) |
>>> +             (pgprot_val(newprot) & ~_PAGE_CHG_MASK);
>>> +
>>> +    if (pte_val(pte) & _PAGE_DIRTY)
>>> +        val |= _PAGE_MODIFIED;
>> Since ptep_get_and_clear() is not atomic operation on LoongArch like 
>> other architectures, considering this scenery with HW PTW enabled:
>>     CPU 0:                         CPU1
>>                              old_pte = ptep_modify_prot_start();
>>                                   old_pte = ptep_get(ptep);
>>
>>     write(buf);
>> *HW will set _PAGE_DIRTY bit*
>>                                   pte_clear(mm, address, ptep);
>> ^^^^^^^^^^ For CPU1, bit _PAGE_DIRTY is no set in old_pte, _PAGE_DIRTY 
>> will be lost also. ^^^^^^^^^^^
>>                              pte = pte_modify(old_pte,)
>>                              ptep_modify_prot_commit(.., pte)
> 
> There does appear to be an issue here. It seems we should use 
> `__HAVE_ARCH_PTEP_GET_AND_CLEAR` and implement ptep_get_and_clear via 
> `atomic_xchg`.
> 
> However, I believe this change should be submitted in a new patch.
What is the status about API ptep_get_and_clear() on LoongArch?

Regards
Bibo Mao

> 
> Thanks
> 
> Tianyang
> 
>>
>> Regards
>> Bibo Mao
>>> +
>>> +    return __pte(val);
>>>   }
>>>     extern void __update_tlb(struct vm_area_struct *vma,
>>> @@ -547,9 +552,13 @@ static inline struct page *pmd_page(pmd_t pmd)
>>>     static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
>>>   {
>>> -    pmd_val(pmd) = (pmd_val(pmd) & _HPAGE_CHG_MASK) |
>>> +    unsigned long val = (pmd_val(pmd) & _HPAGE_CHG_MASK) |
>>>                   (pgprot_val(newprot) & ~_HPAGE_CHG_MASK);
>>> -    return pmd;
>>> +
>>> +    if (pmd_val(pmd) & _PAGE_DIRTY)
>>> +        val |= _PAGE_MODIFIED;
>>> +
>>> +    return __pmd(val);
>>>   }
>>>     static inline pmd_t pmd_mkinvalid(pmd_t pmd)
>>>


      reply	other threads:[~2026-04-08  8:13 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-04  7:30 [PATCH] Loongarch:Make pte/pmd_modify can set _PAGE_MODIFIED Tianyang Zhang
2025-11-04  8:00 ` Huacai Chen
2025-11-05  0:56   ` Tianyang Zhang
2025-11-05  1:07     ` Huacai Chen
2025-11-05  1:18       ` Bibo Mao
2025-11-06  1:55         ` Tianyang Zhang
2025-11-06  2:10           ` Bibo Mao
2025-11-06  8:50             ` Tianyang Zhang
2025-11-06  9:02               ` Bibo Mao
2025-11-06  7:07 ` Bibo Mao
2025-11-06  9:53   ` Tianyang Zhang
2026-04-08  8:10     ` Bibo Mao [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=db75dcb9-25ab-ddea-f00f-c4322c867a63@loongson.cn \
    --to=maobibo@loongson.cn \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=chenhuacai@kernel.org \
    --cc=david@redhat.com \
    --cc=kernel@xen0n.name \
    --cc=linmag7@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@lists.linux.dev \
    --cc=thuth@redhat.com \
    --cc=wangliupu@loongson.cn \
    --cc=willy@infradead.org \
    --cc=zhangtianyang@loongson.cn \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).