[PATCH 11/21] mm/migrate: check hugetlb PMD sharing in move_pages()
From: David Hildenbrand (Arm)
Date: Wed Nov 25 2026 - 17:36:28 EST
Without MPOL_MF_MOVE_ALL, which requires CAP_SYS_NICE, move_pages(2)
refuses to migrate folios that might be mapped by other processes.
However, folio_maybe_mapped_shared() does not check for page table
sharing. So move_pages(2) with MPOL_MF_MOVE can migrate hugetlb
folios that other processes map.
mbind(2) and migrate_pages(2) already handle PMD table sharing since
commit 73bdf65ea748 ("migrate: hugetlb: check for hugetlb shared PMD in
node migration"), but we missed move_pages(2).
Let's fix it by using hugetlb_folio_maybe_mapped_shared() for hugetlb
folios in move_pages(2). Such folios are now reported with -EACCES in
the status array.
Rework the code to have separate checks for hugetlb vs. !hugetlb.
This has low urgency.
Fixes: e632a938d914 ("mm: migrate: add hugepage migration code to move_pages()")
Cc: <stable@xxxxxxxxxxxxxxx>
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
mm/migrate.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/mm/migrate.c b/mm/migrate.c
index 0430197fdc04b..194d74152e8e3 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -2279,8 +2279,9 @@ static int do_move_pages_to_node(struct list_head *pagelist, int node)
return err;
}
-static int __add_folio_for_migration(struct folio *folio, int node,
- struct list_head *pagelist, bool migrate_all)
+static int __add_folio_for_migration(struct folio *folio,
+ struct folio_walk *fw, int node, struct list_head *pagelist,
+ bool migrate_all)
{
if (is_zero_folio(folio) || is_huge_zero_folio(folio))
return -EFAULT;
@@ -2291,13 +2292,19 @@ static int __add_folio_for_migration(struct folio *folio, int node,
if (folio_nid(folio) == node)
return 0;
- if (folio_maybe_mapped_shared(folio) && !migrate_all)
- return -EACCES;
-
if (folio_test_hugetlb(folio)) {
+ /* fw->ptep aliases fw->pmdp and fw->pudp. */
+ if (!migrate_all && hugetlb_folio_maybe_mapped_shared(folio, fw->ptep))
+ return -EACCES;
if (folio_isolate_hugetlb(folio, pagelist))
return 1;
- } else if (folio_isolate_lru(folio)) {
+ return -EBUSY;
+ }
+
+ if (!migrate_all && folio_maybe_mapped_shared(folio))
+ return -EACCES;
+
+ if (folio_isolate_lru(folio)) {
list_add_tail(&folio->lru, pagelist);
node_stat_mod_folio(folio,
NR_ISOLATED_ANON + folio_is_file_lru(folio),
@@ -2332,8 +2339,8 @@ static int add_folio_for_migration(struct mm_struct *mm, const void __user *p,
if (vma && vma_migratable(vma)) {
folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE);
if (folio) {
- err = __add_folio_for_migration(folio, node, pagelist,
- migrate_all);
+ err = __add_folio_for_migration(folio, &fw, node,
+ pagelist, migrate_all);
folio_walk_end(&fw, vma);
} else {
err = -ENOENT;
--
2.56.1