Skip to content

Commit bf2f34a

Browse files
committed
LoongArch: Set _PAGE_DIRTY only if _PAGE_WRITE is set in {pmd,pte}_mkdirty()
Now {pmd,pte}_mkdirty() set _PAGE_DIRTY bit unconditionally, this causes random segmentation fault after commit 0ccf7f1 ("mm/thp: carry over dirty bit when thp splits on pmd"). The reason is: when fork(), parent process use pmd_wrprotect() to clear huge page's _PAGE_WRITE and _PAGE_DIRTY (for COW); then pte_mkdirty() set _PAGE_DIRTY as well as _PAGE_MODIFIED while splitting dirty huge pages; once _PAGE_DIRTY is set, there will be no tlb modify exception so the COW machanism fails; and at last memory corruption occurred between parent and child processes. So, we should set _PAGE_DIRTY only when _PAGE_WRITE is set in {pmd,pte}_ mkdirty(). Cc: stable@vger.kernel.org Cc: Peter Xu <peterx@redhat.com> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent e428e96 commit bf2f34a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

arch/loongarch/include/asm/pgtable.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,9 @@ static inline pte_t pte_mkclean(pte_t pte)
349349

350350
static inline pte_t pte_mkdirty(pte_t pte)
351351
{
352-
pte_val(pte) |= (_PAGE_DIRTY | _PAGE_MODIFIED);
352+
pte_val(pte) |= _PAGE_MODIFIED;
353+
if (pte_val(pte) & _PAGE_WRITE)
354+
pte_val(pte) |= _PAGE_DIRTY;
353355
return pte;
354356
}
355357

@@ -478,7 +480,9 @@ static inline pmd_t pmd_mkclean(pmd_t pmd)
478480

479481
static inline pmd_t pmd_mkdirty(pmd_t pmd)
480482
{
481-
pmd_val(pmd) |= (_PAGE_DIRTY | _PAGE_MODIFIED);
483+
pmd_val(pmd) |= _PAGE_MODIFIED;
484+
if (pmd_val(pmd) & _PAGE_WRITE)
485+
pmd_val(pmd) |= _PAGE_DIRTY;
482486
return pmd;
483487
}
484488

0 commit comments

Comments
 (0)