[PATCH 51/60] mm: restrict explicit migration to N_MEMORY_MEMPOLICY
From: Gregory Price via B4 Relay
Date: Fri Nov 20 2026 - 13:33:04 EST
From: "Gregory Price (Meta)" <gourry@xxxxxxxxxx>
mbind(MPOL_MF_MOVE), migrate_pages() and move_pages() act only on folios
on common memory. N_MEMORY_MEMPOLICY nodes are the nodes memory policy
and explicit migration may target, so let explicit migration also act on
folios already on them.
Add folio_allows_mempolicy(), which requires the folio's node to permit
memory policy, and use it instead.
Memory on other private nodes is still left alone:
- move_pages() refuses to migrate it.
- mbind(MPOL_MF_MOVE) and migrate_pages() skip its folios.
Cpuset migration can now find pages on these nodes. Evacuate them only
from nodes the cpuset no longer allows. Private nodes are still never
destinations for this kind of migration.
Migration keeps its best-effort behavior and can affect shared backing
pages seen by other mappers.
Signed-off-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
---
kernel/cgroup/cpuset.c | 8 +++++++-
mm/internal.h | 7 +++++++
mm/mempolicy.c | 4 ++--
mm/migrate.c | 2 +-
4 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
index e20f9ccb8ab7..7d4b922a1914 100644
--- a/kernel/cgroup/cpuset.c
+++ b/kernel/cgroup/cpuset.c
@@ -2686,8 +2686,14 @@ static void cpuset_migrate_mm_workfn(struct work_struct *work)
{
struct cpuset_migrate_mm_work *mwork =
container_of(work, struct cpuset_migrate_mm_work, work);
+ nodemask_t retained;
- /* Private nodes are never migration destinations. */
+ /*
+ * Private nodes are never migration destinations. Evacuate private
+ * memory only from nodes the cpuset no longer allows.
+ */
+ nodes_and(retained, mwork->to, node_states[N_MEMORY_PRIVATE]);
+ nodes_andnot(mwork->from, mwork->from, retained);
nodes_andnot(mwork->to, mwork->to, node_states[N_MEMORY_PRIVATE]);
/* on a wq worker, no need to worry about %current's mems_allowed */
do_migrate_pages(mwork->mm, &mwork->from, &mwork->to, MPOL_MF_MOVE_ALL);
diff --git a/mm/internal.h b/mm/internal.h
index fa1ae221564e..4bac570bb29f 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -87,6 +87,13 @@ static inline bool folio_allows_reclaim(struct folio *folio)
node_state(folio_nid(folio), N_MEMORY_RECLAIM);
}
+/* May userspace NUMA operations act on this folio's memory? */
+static inline bool folio_allows_mempolicy(struct folio *folio)
+{
+ return !folio_is_zone_device(folio) &&
+ node_state(folio_nid(folio), N_MEMORY_MEMPOLICY);
+}
+
/* mm/vmscan.c */
unsigned long zone_reclaimable_pages(struct zone *zone);
unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 2bfe8b2b6287..9d2e56017ebe 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -705,7 +705,7 @@ static void queue_folios_pmd(pmd_t *pmd, unsigned long addr,
return;
}
folio = vm_normal_folio_pmd(walk->vma, addr, pmdval);
- if (!folio || !folio_is_common_memory(folio))
+ if (!folio || !folio_allows_mempolicy(folio))
return;
if (!queue_folio_required(folio, qp))
return;
@@ -763,7 +763,7 @@ static int queue_folios_pte_range(pmd_t *pmd, unsigned long addr,
continue;
}
folio = vm_normal_folio(vma, addr, ptent);
- if (!folio || !folio_is_common_memory(folio))
+ if (!folio || !folio_allows_mempolicy(folio))
continue;
if (folio_test_large(folio) && max_nr != 1)
nr = folio_pte_batch(folio, pte, ptent, max_nr);
diff --git a/mm/migrate.c b/mm/migrate.c
index ac583afa1bc5..6791834f14fe 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -2287,7 +2287,7 @@ static int __add_folio_for_migration(struct folio *folio, int node,
if (is_zero_folio(folio) || is_huge_zero_folio(folio))
return -EFAULT;
- if (!folio_is_common_memory(folio))
+ if (!folio_allows_mempolicy(folio))
return -ENOENT;
if (folio_nid(folio) == node)
--
2.56.1