Skip to contentSkip to conversation

mm/migrate: snapshot the source nid before the misplaced migration

PATCH Applied Yafang Shao 3 participants 3 messages Oct 27 – Oct 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/migrate: snapshot the source nid before the misplaced migration

A successful migration frees the source folio before the promotion counter update, so the folio_nid() read there operates on a freed folio — which may already have been reallocated. It happens to work today, as Zi explained:

: Strictly speaking, it can be seen as a benign UAF. It works, because : 1. folio->flags happens to be the same as page->flags,
: 2. folio/page nid is stored in the ->flags and never changed. :
: In memdesc world, after a folio is freed, I assume folio->flags would be : zeroed since struct folio is freed and calling folio_nid() on a freed : folio would give a bogus result (yeah, it can be mitigated by not : zeroing node field and each node has its own struct folio pool, but that : makes folio_nid() implementation dependent).

and Matthew explained further:

: once struct folio is separately allocated, it's a UAF. It could be : recycled to a different NID or maybe the slab containing the folio could : be freed (assuming we're not holding the RCU read lock at this point in : migration, and I don't see why we would be).

A separate concern is memory hot-removal. With memory-tiering mode enabled, successful migration can release the source before the promotion-accounting folio_nid() read. If the task is preempted there, an otherwise-free source block can be offlined and removed first. On the x86 path, removal can unmap the source's vmemmap, so the later read can fault. The snapshot avoids that read.

Fixes e39bb6be9f2b ("NUMA Balancing: add page promotion counter")
Cc Ying Huang <[email protected]>
Cc Matthew Wilcox <[email protected]>
---
 mm/migrate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

---

v1->v2: improve the commit log, thanks to Zi, Matthew and Andrew.

mm/migrate.c +2 -1
index 15b45832bcfa..91ba064e1bf4 100644
@@ -2757,6 +2757,7 @@ int migrate_misplaced_folio(struct folio *folio, int node)
 	pg_data_t *pgdat = NODE_DATA(node);
 	int nr_remaining;
 	unsigned int nr_succeeded;
+	int src_node = folio_nid(folio);
 	LIST_HEAD(migratepages);
 	struct mem_cgroup *memcg = get_mem_cgroup_from_folio(folio);
 	struct lruvec *lruvec = mem_cgroup_lruvec(memcg, pgdat);
@@ -2771,7 +2772,7 @@ int migrate_misplaced_folio(struct folio *folio, int node)
 		count_vm_numa_events(NUMA_PAGE_MIGRATE, nr_succeeded);
 		count_memcg_events(memcg, NUMA_PAGE_MIGRATE, nr_succeeded);
 		if ((sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING)
-		    && !node_is_toptier(folio_nid(folio))
+		    && !node_is_toptier(src_node)
 		    && node_is_toptier(node))
 			mod_lruvec_state(lruvec, PGPROMOTE_SUCCESS, nr_succeeded);
 	}
Signature · 1 line
2.52.0
reviewer Acked-by on v2 1/1
#1 lore raw
Quoted 35 lines · Yafang Shao

A successful migration frees the source folio before the promotion counter update, so the folio_nid() read there operates on a freed folio — which may already have been reallocated. It happens to work today, as Zi explained:

: Strictly speaking, it can be seen as a benign UAF. It works, because : 1. folio->flags happens to be the same as page->flags,
: 2. folio/page nid is stored in the ->flags and never changed. :
: In memdesc world, after a folio is freed, I assume folio->flags would be : zeroed since struct folio is freed and calling folio_nid() on a freed : folio would give a bogus result (yeah, it can be mitigated by not : zeroing node field and each node has its own struct folio pool, but that : makes folio_nid() implementation dependent).

and Matthew explained further:

: once struct folio is separately allocated, it's a UAF. It could be : recycled to a different NID or maybe the slab containing the folio could : be freed (assuming we're not holding the RCU read lock at this point in : migration, and I don't see why we would be).

A separate concern is memory hot-removal. With memory-tiering mode enabled, successful migration can release the source before the promotion-accounting folio_nid() read. If the task is preempted there, an otherwise-free source block can be offlined and removed first. On the x86 path, removal can unmap the source's vmemmap, so the later read can fault. The snapshot avoids that read.

Fixes e39bb6be9f2b ("NUMA Balancing: add page promotion counter")
Cc Ying Huang <[email protected]>
Cc Matthew Wilcox <[email protected]>
Quoted 8 lines
---
 mm/migrate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

---

v1->v2: improve the commit log, thanks to Zi, Matthew and Andrew.

LGTM. Thanks.

Best Regards,
Yan, Zi

maintainer Acked-by on v2 1/1
#2 lore raw
In reply to Yafang Shao
Quoted 48 lines · Yafang Shao

A successful migration frees the source folio before the promotion counter update, so the folio_nid() read there operates on a freed folio — which may already have been reallocated. It happens to work today, as Zi explained:

: Strictly speaking, it can be seen as a benign UAF. It works, because : 1. folio->flags happens to be the same as page->flags,
: 2. folio/page nid is stored in the ->flags and never changed. :
: In memdesc world, after a folio is freed, I assume folio->flags would be : zeroed since struct folio is freed and calling folio_nid() on a freed : folio would give a bogus result (yeah, it can be mitigated by not : zeroing node field and each node has its own struct folio pool, but that : makes folio_nid() implementation dependent).

and Matthew explained further:

: once struct folio is separately allocated, it's a UAF. It could be : recycled to a different NID or maybe the slab containing the folio could : be freed (assuming we're not holding the RCU read lock at this point in : migration, and I don't see why we would be).

A separate concern is memory hot-removal. With memory-tiering mode enabled, successful migration can release the source before the promotion-accounting folio_nid() read. If the task is preempted there, an otherwise-free source block can be offlined and removed first. On the x86 path, removal can unmap the source's vmemmap, so the later read can fault. The snapshot avoids that read.

Fixes e39bb6be9f2b ("NUMA Balancing: add page promotion counter")
Cc Ying Huang <[email protected]>
Cc Matthew Wilcox <[email protected]>
---
 mm/migrate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

---

v1->v2: improve the commit log, thanks to Zi, Matthew and Andrew.

mm/migrate.c +0 -0
index 15b45832bcfa..91ba064e1bf4 100644

mm/migrate.c int migrate_misplaced_folio(struct folio *folio, int node) · line 2760

Quoted 4 lines
mm/migrate.c +1 -0
@@ -2757,6 +2757,7 @@ int migrate_misplaced_folio(struct folio *folio, int node)
 	pg_data_t *pgdat = NODE_DATA(node);
 	int nr_remaining;
 	unsigned int nr_succeeded;
+	int src_node = folio_nid(folio);

can be const.

Signature · 3 lines
Cheers,

David