Soft-dirty and uffd-wp live in the PMD, not in the swap entry, so every path that manipulates them for a PMD migration entry has to do the same for a PMD swap entry. Otherwise clearing soft-dirty skips a swapped-out THP, UFFDIO_WRITEPROTECT does not arm it, and mremap does not mark the destination. change_huge_pmd() needs nothing beyond dropping its thp_migration_supported() gate: a PMD swap entry encodes no write permission, so it falls into the default arm of change_non_present_huge_pmd() and picks up only the uffd-wp update. Signed-off-by: Usama Arif --- fs/proc/task_mmu.c | 4 ++-- mm/huge_memory.c | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 97c3fd7acf15d..83cf39006e5f8 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1655,7 +1655,7 @@ static void clear_soft_dirty_pmd(struct vm_area_struct *vma, pmd = pmd_clear_soft_dirty(pmd); set_pmd_at(vma->vm_mm, addr, pmdp, pmd); - } else if (pmd_is_migration_entry(pmd)) { + } else if (pmd_is_migration_entry(pmd) || pmd_is_swap_entry(pmd)) { pmd = pmd_swp_clear_soft_dirty(pmd); set_pmd_at(vma->vm_mm, addr, pmdp, pmd); } @@ -2481,7 +2481,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma, old = pmdp_invalidate_ad(vma, addr, pmdp); pmd = pmd_mkuffd(old); set_pmd_at(vma->vm_mm, addr, pmdp, pmd); - } else if (pmd_is_migration_entry(pmd)) { + } else if (pmd_is_migration_entry(pmd) || pmd_is_swap_entry(pmd)) { pmd = pmd_swp_mkuffd(pmd); set_pmd_at(vma->vm_mm, addr, pmdp, pmd); } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 80d18ca972ecf..56eb87fa98376 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2637,7 +2637,8 @@ static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl, static pmd_t move_soft_dirty_pmd(pmd_t pmd) { if (pgtable_supports_soft_dirty()) { - if (unlikely(pmd_is_migration_entry(pmd))) + if (unlikely(pmd_is_migration_entry(pmd) || + pmd_is_swap_entry(pmd))) pmd = pmd_swp_mksoft_dirty(pmd); else if (pmd_present(pmd)) pmd = pmd_mksoft_dirty(pmd); @@ -2729,6 +2730,12 @@ static void change_non_present_huge_pmd(struct mm_struct *mm, pmd_t newpmd; VM_WARN_ON(!pmd_is_valid_softleaf(*pmd)); + + /* + * Note that a PMD swap entry falls into the default branch below: it + * does not encode write permission in the entry type, so only the + * uffd_wp flag update at the end applies to it. + */ if (softleaf_is_migration_write(entry)) { const struct folio *folio = softleaf_to_folio(entry); @@ -2789,7 +2796,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, if (!ptl) return 0; - if (thp_migration_supported() && pmd_is_valid_softleaf(*pmd)) { + if (pmd_is_valid_softleaf(*pmd)) { change_non_present_huge_pmd(mm, addr, pmd, uffd_prot, uffd_prot_resolve); goto unlock; -- 2.53.0-Meta