[PATCH] mm: use softleaf_migration_mkreadable() in change_protection()

From: Chris Down

Date: Tue Dec 01 2026 - 23:59:17 EST


change_protection() downgrades a writable migration entry to a readable
one in change_softleaf_pte(), change_non_present_huge_pmd() and
hugetlb_change_protection(). Each of them open codes the same choice:
the new entry is readable exclusive if the folio is anonymous, since a
writable anon migration entry implies AnonExclusive, and plain readable
otherwise.

PAGEMAP_SCAN's write-protect path needed the same downgrade, so
softleaf_migration_mkreadable() was added to leafops.h for it. Let's use
it in change_protection() too. Each site still handles the soft-dirty
and uffd bits itself as before. hugetlb_change_protection() only looked
up the folio for this choice, so its local goes away.

The other places that choose between readable exclusive and readable
migration entries, such as try_to_migrate_one() and
__split_huge_pmd_locked(), carry over the exclusive state of the page or
of the old entry rather than downgrade a writable one, so they are left
alone.

No functional change intended.

Suggested-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
mm/huge_memory.c | 7 +------
mm/hugetlb.c | 8 +-------
mm/mprotect.c | 7 +------
3 files changed, 3 insertions(+), 19 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 4fc888ddfb69..ce028838bdba 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2704,16 +2704,11 @@ static void change_non_present_huge_pmd(struct mm_struct *mm,

VM_WARN_ON(!pmd_is_valid_softleaf(*pmd));
if (softleaf_is_migration_write(entry)) {
- const struct folio *folio = softleaf_to_folio(entry);
-
/*
* A protection check is difficult so
* just be safe and disable write
*/
- if (folio_test_anon(folio))
- entry = make_readable_exclusive_migration_entry(swp_offset(entry));
- else
- entry = make_readable_migration_entry(swp_offset(entry));
+ entry = softleaf_migration_mkreadable(entry);
newpmd = softleaf_to_pmd(entry);
if (pmd_swp_soft_dirty(*pmd))
newpmd = pmd_swp_mksoft_dirty(newpmd);
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 367c93f851d5..f1e0ed41af09 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6831,16 +6831,10 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
if (unlikely(softleaf_is_hwpoison(entry))) {
/* Nothing to do. */
} else if (unlikely(softleaf_is_migration(entry))) {
- struct folio *folio = softleaf_to_folio(entry);
pte_t newpte = pte;

if (softleaf_is_migration_write(entry)) {
- if (folio_test_anon(folio))
- entry = make_readable_exclusive_migration_entry(
- swp_offset(entry));
- else
- entry = make_readable_migration_entry(
- swp_offset(entry));
+ entry = softleaf_migration_mkreadable(entry);
newpte = swp_entry_to_pte(entry);
pages++;
}
diff --git a/mm/mprotect.c b/mm/mprotect.c
index ef4e7188e24d..e1c4f8fca4e8 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -221,16 +221,11 @@ static long change_softleaf_pte(struct vm_area_struct *vma,
pte_t newpte;

if (softleaf_is_migration_write(entry)) {
- const struct folio *folio = softleaf_to_folio(entry);
-
/*
* A protection check is difficult so
* just be safe and disable write
*/
- if (folio_test_anon(folio))
- entry = make_readable_exclusive_migration_entry(swp_offset(entry));
- else
- entry = make_readable_migration_entry(swp_offset(entry));
+ entry = softleaf_migration_mkreadable(entry);
newpte = swp_entry_to_pte(entry);
if (pte_swp_soft_dirty(oldpte))
newpte = pte_swp_mksoft_dirty(newpte);

base-commit: 4bffe3da0065e60f45a0b96f7460e5e7f7d2b63a
--
2.56.1