Skip to contentSkip to conversation

mm/hugetlb_vmemmap: fix race with speculative PFN walkers

PATCH Applied Yu Zhao 4 participants 5 messages Jun 27, 2024 – Jul 2, 2024

Subsystems (3)
lore
Share & tools
lore

Yu Zhao

author
#0 lore raw

PATCH v2 1/1 mm/hugetlb_vmemmap: fix race with speculative PFN walkers

While investigating HVO for THPs [1], it turns out that speculative PFN walkers like compaction can race with vmemmap modifications, e.g.,

CPU 1 (vmemmap modifier) CPU 2 (speculative PFN walker)
------------------------------- ------------------------------ Allocates an LRU folio page1
Sees page1
Frees page1

Allocates a hugeTLB folio page2
(page1 being a tail of page2)

Updates vmemmap mapping page1
get_page_unless_zero(page1)

Even though page1->_refcount is zero after HVO, get_page_unless_zero() can still try to modify this read-only field, resulting in a crash.

An independent report [2] confirmed this race.

There are two discussed approaches to fix this race:
1. Make RO vmemmap RW so that get_page_unless_zero() can fail without triggering a PF.
2. Use RCU to make sure get_page_unless_zero() either sees zero page->_refcount through the old vmemmap or non-zero page->_refcount through the new one.

The second approach is preferred here because:
1. It can prevent illegal modifications to struct page[] that has been HVO'ed;
2. It can be generalized, in a way similar to ZERO_PAGE(), to fix similar races in other places, e.g., arch_remove_memory() on x86 [3], which frees vmemmap mapping offlined struct page[].

While adding synchronize_rcu(), the goal is to be surgical, rather than optimized. Specifically, calls to synchronize_rcu() on the error handling paths can be coalesced, but it is not done for the sake of
Simplicity: noticeably, this fix removes ~50% more lines than it adds.

According to the hugetlb_optimize_vmemmap section in Documentation/admin-guide/sysctl/vm.rst, enabling HVO makes allocating or freeing hugeTLB pages "~2x slower than before". Having synchronize_rcu() on top makes those operations even worse, and this also affects the user interface /proc/sys/vm/nr_overcommit_hugepages.

[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/[email protected]/
[3] https://lore.kernel.org/[email protected]/

---
 include/linux/page_ref.h |  8 +++++-
 mm/hugetlb.c             | 53 ++++++----------------------------------
 mm/hugetlb_vmemmap.c     | 16 ++++++++++++
 3 files changed, 30 insertions(+), 47 deletions(-)
include/linux/page_ref.h +7 -1
index 490d0ad6e56d..8c236c651d1d 100644
@@ -230,7 +230,13 @@ static inline int folio_ref_dec_return(struct folio *folio)
 
 static inline bool page_ref_add_unless(struct page *page, int nr, int u)
 {
-	bool ret = atomic_add_unless(&page->_refcount, nr, u);
+	bool ret = false;
+
+	rcu_read_lock();
+	/* avoid writing to the vmemmap area being remapped */
+	if (!page_is_fake_head(page) && page_ref_count(page) != u)
+		ret = atomic_add_unless(&page->_refcount, nr, u);
+	rcu_read_unlock();
 
 	if (page_ref_tracepoint_active(page_ref_mod_unless))
 		__page_ref_mod_unless(page, nr, ret);
mm/hugetlb.c +7 -46

111 lines not on this page · raw message

mm/hugetlb_vmemmap.c +16 -0
index fa00d61b6c5a..829112b0a914 100644
@@ -455,6 +455,8 @@ static int __hugetlb_vmemmap_restore_folio(const struct hstate *h,
 	unsigned long vmemmap_reuse;
 
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio);
+	VM_WARN_ON_ONCE_FOLIO(folio_ref_count(folio), folio);
+
 	if (!folio_test_hugetlb_vmemmap_optimized(folio))
 		return 0;
 
@@ -490,6 +492,9 @@ static int __hugetlb_vmemmap_restore_folio(const struct hstate *h,
  */
 int hugetlb_vmemmap_restore_folio(const struct hstate *h, struct folio *folio)
 {
+	/* avoid writes from page_ref_add_unless() while unfolding vmemmap */
+	synchronize_rcu();
+
 	return __hugetlb_vmemmap_restore_folio(h, folio, 0);
 }
 
@@ -514,6 +519,9 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h,
 	long restored = 0;
 	long ret = 0;
 
+	/* avoid writes from page_ref_add_unless() while unfolding vmemmap */
+	synchronize_rcu();
+
 	list_for_each_entry_safe(folio, t_folio, folio_list, lru) {
 		if (folio_test_hugetlb_vmemmap_optimized(folio)) {
 			ret = __hugetlb_vmemmap_restore_folio(h, folio,
@@ -559,6 +567,8 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h,
 	unsigned long vmemmap_reuse;
 
 	VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio);
+	VM_WARN_ON_ONCE_FOLIO(folio_ref_count(folio), folio);
+
 	if (!vmemmap_should_optimize_folio(h, folio))
 		return ret;
 
@@ -610,6 +620,9 @@ void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio)
 {
 	LIST_HEAD(vmemmap_pages);
 
+	/* avoid writes from page_ref_add_unless() while folding vmemmap */
+	synchronize_rcu();
+
 	__hugetlb_vmemmap_optimize_folio(h, folio, &vmemmap_pages, 0);
 	free_vmemmap_page_list(&vmemmap_pages);
 }
@@ -653,6 +666,9 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l
 
 	flush_tlb_all();
 
+	/* avoid writes from page_ref_add_unless() while folding vmemmap */
+	synchronize_rcu();
+
 	list_for_each_entry(folio, folio_list, lru) {
 		int ret;
 
Signature · 1 line
2.45.2.803.g4e1b14247a-goog
maintainer
#1 lore raw
Quoted 9 lines · On Thu, 27 Jun 2024 16:27:05 -0600 Yu Zhao <[email protected]> wrote:

While investigating HVO for THPs [1], it turns out that speculative PFN walkers like compaction can race with vmemmap modifications, e.g.,

CPU 1 (vmemmap modifier) CPU 2 (speculative PFN walker)
------------------------------- ------------------------------ Allocates an LRU folio page1
Sees page1
Frees page1

Quoted 8 lines

Allocates a hugeTLB folio page2
(page1 being a tail of page2)

Updates vmemmap mapping page1
get_page_unless_zero(page1)

Even though page1->_refcount is zero after HVO, get_page_unless_zero() can still try to modify this read-only field, resulting in a crash.

Ah. So we should backport this into earlier kernels, yes?

Are we able to identify a Fixes: for this? Looks difficult.

This seems quite hard to trigger. Do any particular userspace actions invoke the race?

author
#2 lore raw
Quoted 21 lines · Andrew Morton
Quoted 9 lines · On Thu, 27 Jun 2024 16:27:05 -0600 Yu Zhao <[email protected]> wrote:

While investigating HVO for THPs [1], it turns out that speculative PFN walkers like compaction can race with vmemmap modifications, e.g.,

CPU 1 (vmemmap modifier) CPU 2 (speculative PFN walker)
------------------------------- ------------------------------ Allocates an LRU folio page1
Sees page1
Frees page1

Quoted 8 lines

Allocates a hugeTLB folio page2
(page1 being a tail of page2)

Updates vmemmap mapping page1
get_page_unless_zero(page1)

Even though page1->_refcount is zero after HVO, get_page_unless_zero() can still try to modify this read-only field, resulting in a crash.

Quoted 6 lines

Ah. So we should backport this into earlier kernels, yes?

Are we able to identify a Fixes: for this? Looks difficult.

This seems quite hard to trigger. Do any particular userspace actions invoke the race?

Yes, *very* hard to trigger:
1. Most hugeTLB use cases I know of are static, i.e., reserved at boot time, because allocating at runtime is not reliable at all.
2. On top of that, someone has to be very unlucky to get tripped over above, because the race window is so small -- I wasn't able to trigger it with a stress testing that does nothing but that (with THPs though).

So I don't think it's worth cc'ing stable, unless Muchun recommends.

maintainer
#3 lore raw
Quoted 41 lines · Yu Zhao
Quoted 21 lines · Andrew Morton
Quoted 9 lines · On Thu, 27 Jun 2024 16:27:05 -0600 Yu Zhao <[email protected]> wrote:

While investigating HVO for THPs [1], it turns out that speculative PFN walkers like compaction can race with vmemmap modifications, e.g.,

CPU 1 (vmemmap modifier) CPU 2 (speculative PFN walker)
------------------------------- ------------------------------ Allocates an LRU folio page1
Sees page1
Frees page1

Quoted 8 lines

Allocates a hugeTLB folio page2
(page1 being a tail of page2)

Updates vmemmap mapping page1
get_page_unless_zero(page1)

Even though page1->_refcount is zero after HVO, get_page_unless_zero() can still try to modify this read-only field, resulting in a crash.

Quoted 6 lines

Ah. So we should backport this into earlier kernels, yes?

Are we able to identify a Fixes: for this? Looks difficult.

This seems quite hard to trigger. Do any particular userspace actions invoke the race?

Yes, *very* hard to trigger:
1. Most hugeTLB use cases I know of are static, i.e., reserved at boot time, because allocating at runtime is not reliable at all.
2. On top of that, someone has to be very unlucky to get tripped over above, because the race window is so small -- I wasn't able to trigger it with a stress testing that does nothing but that (with THPs though).

Quoted 1 line

So I don't think it's worth cc'ing stable, unless Muchun recommends.

I agree with Yu.

Thanks.

#4 lore raw
In reply to Yu Zhao
Quoted 63 lines · Yu Zhao

While investigating HVO for THPs [1], it turns out that speculative PFN walkers like compaction can race with vmemmap modifications, e.g.,

CPU 1 (vmemmap modifier) CPU 2 (speculative PFN walker)
------------------------------- ------------------------------ Allocates an LRU folio page1
Sees page1
Frees page1

Allocates a hugeTLB folio page2
(page1 being a tail of page2)

Updates vmemmap mapping page1
get_page_unless_zero(page1)

Even though page1->_refcount is zero after HVO, get_page_unless_zero() can still try to modify this read-only field, resulting in a crash.

An independent report [2] confirmed this race.

There are two discussed approaches to fix this race:
1. Make RO vmemmap RW so that get_page_unless_zero() can fail without triggering a PF.
2. Use RCU to make sure get_page_unless_zero() either sees zero page->_refcount through the old vmemmap or non-zero page->_refcount through the new one.

The second approach is preferred here because:
1. It can prevent illegal modifications to struct page[] that has been

    HVO'ed;

2. It can be generalized, in a way similar to ZERO_PAGE(), to fix similar races in other places, e.g., arch_remove_memory() on x86 [3], which frees vmemmap mapping offlined struct page[].

While adding synchronize_rcu(), the goal is to be surgical, rather than optimized. Specifically, calls to synchronize_rcu() on the error handling paths can be coalesced, but it is not done for the sake of
Simplicity: noticeably, this fix removes ~50% more lines than it adds.

According to the hugetlb_optimize_vmemmap section in Documentation/admin-guide/sysctl/vm.rst, enabling HVO makes allocating or freeing hugeTLB pages "~2x slower than before". Having synchronize_rcu() on top makes those operations even worse, and this also affects the user interface /proc/sys/vm/nr_overcommit_hugepages.

[1] https://lore.kernel.org/[email protected]/
[2] https://lore.kernel.org/[email protected]/
[3] https://lore.kernel.org/[email protected]/

---
  include/linux/page_ref.h |  8 +++++-
  mm/hugetlb.c             | 53 ++++++----------------------------------
  mm/hugetlb_vmemmap.c     | 16 ++++++++++++
  3 files changed, 30 insertions(+), 47 deletions(-)
include/linux/page_ref.h +0 -0
index 490d0ad6e56d..8c236c651d1d 100644
@@ -230,7 +230,13 @@ static inline int folio_ref_dec_return(struct folio *folio)
 

include/linux/page_ref.h static inline int folio_ref_dec_return(struct folio *folio) · line 239

Quoted 10 lines
include/linux/page_ref.h +7 -1
  static inline bool page_ref_add_unless(struct page *page, int nr, int u)
  {
-	bool ret = atomic_add_unless(&page->_refcount, nr, u);
+	bool ret = false;
+
+	rcu_read_lock();
+	/* avoid writing to the vmemmap area being remapped */
+	if (!page_is_fake_head(page) && page_ref_count(page) != u)
+		ret = atomic_add_unless(&page->_refcount, nr, u);
+	rcu_read_unlock();

The page_is_fake_head() thingy in page_ref.h is a bit suboptimal, currently it really only works on _refcount. But I get why it is required right now, hmmm.

(independent, all users of page_ref_add_unless seem to pass u==0, maybe we should clean that up at some point; hard to imagine other use cases for refcounts besides "unless 0").

Signature · 3 lines
Cheers,

David / dhildenb