LoongArch: Set _PAGE_DIRTY only if _PAGE_MODIFIED is set in {pmd,pte}_mkwrite()

Message ID 20221121064826.2115193-1-chenhuacai@loongson.cn
State New
Headers
Series LoongArch: Set _PAGE_DIRTY only if _PAGE_MODIFIED is set in {pmd,pte}_mkwrite() |

Commit Message

Huacai Chen Nov. 21, 2022, 6:48 a.m. UTC
  Set _PAGE_DIRTY only if _PAGE_MODIFIED is set in {pmd,pte}_mkwrite().
Otherwise, _PAGE_DIRTY silences the TLB modify exception and make us
have no chance to mark a pmd/pte dirty (_PAGE_MODIFIED) for software.

Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
 arch/loongarch/include/asm/pgtable.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
  

Comments

Guo Ren Nov. 21, 2022, 10:03 a.m. UTC | #1
On Mon, Nov 21, 2022 at 2:51 PM Huacai Chen <chenhuacai@loongson.cn> wrote:
>
> Set _PAGE_DIRTY only if _PAGE_MODIFIED is set in {pmd,pte}_mkwrite().
> Otherwise, _PAGE_DIRTY silences the TLB modify exception and make us
> have no chance to mark a pmd/pte dirty (_PAGE_MODIFIED) for software.
Yes, the "VALID/DIRTY"  is a bad hardware pgtable-bit design. It
caused the software to waste more bits in PTE.  We need hardware
P/R/W/A/D definitions. It seems from MIPS in history, C-SKY also
follows that :P.

Reviewed-by: Guo Ren <guoren@kernel.org>

>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
> ---
>  arch/loongarch/include/asm/pgtable.h | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
> index debbe116f105..aa0e0e0d4ee5 100644
> --- a/arch/loongarch/include/asm/pgtable.h
> +++ b/arch/loongarch/include/asm/pgtable.h
> @@ -357,7 +357,9 @@ static inline pte_t pte_mkdirty(pte_t pte)
>
>  static inline pte_t pte_mkwrite(pte_t pte)
>  {
> -       pte_val(pte) |= (_PAGE_WRITE | _PAGE_DIRTY);
> +       pte_val(pte) |= _PAGE_WRITE;
> +       if (pte_val(pte) & _PAGE_MODIFIED)
> +               pte_val(pte) |= _PAGE_DIRTY;
>         return pte;
>  }
>
> @@ -457,7 +459,9 @@ static inline int pmd_write(pmd_t pmd)
>
>  static inline pmd_t pmd_mkwrite(pmd_t pmd)
>  {
> -       pmd_val(pmd) |= (_PAGE_WRITE | _PAGE_DIRTY);
> +       pmd_val(pmd) |= _PAGE_WRITE;
> +       if (pmd_val(pmd) & _PAGE_MODIFIED)
> +               pmd_val(pmd) |= _PAGE_DIRTY;
>         return pmd;
>  }
>
> --
> 2.31.1
>


--
Best Regards
 Guo Ren
  

Patch

diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index debbe116f105..aa0e0e0d4ee5 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -357,7 +357,9 @@  static inline pte_t pte_mkdirty(pte_t pte)
 
 static inline pte_t pte_mkwrite(pte_t pte)
 {
-	pte_val(pte) |= (_PAGE_WRITE | _PAGE_DIRTY);
+	pte_val(pte) |= _PAGE_WRITE;
+	if (pte_val(pte) & _PAGE_MODIFIED)
+		pte_val(pte) |= _PAGE_DIRTY;
 	return pte;
 }
 
@@ -457,7 +459,9 @@  static inline int pmd_write(pmd_t pmd)
 
 static inline pmd_t pmd_mkwrite(pmd_t pmd)
 {
-	pmd_val(pmd) |= (_PAGE_WRITE | _PAGE_DIRTY);
+	pmd_val(pmd) |= _PAGE_WRITE;
+	if (pmd_val(pmd) & _PAGE_MODIFIED)
+		pmd_val(pmd) |= _PAGE_DIRTY;
 	return pmd;
 }