mm/huge_memory: fix huge_zero_pfn race
Subsystems (3)
v2 cover mm/huge_memory: fix huge_zero_pfn race
There is a subtle race in the reference-counted huge_zero_folio implementation.
The fast path atomic logic fails to account for the fact that the shrinker (which drops the final huge_zero_refcount pin) can overwrite huge_zero_pfn with the ~0UL sentinel value in shrink_huge_zero_folio_scan() after a racing get_huge_zero_folio() installed a valid value there.
This results in huge_zero_folio being correctly set but huge_zero_pfn being set incorrectly and thus is_huge_zero_pfn() and consequently is_huge_zero_pmd() will misidentify the huge zero folio as being an ordinary THP folio.
This can result in the huge zero folio being split and otherwise treated incorrectly.
The solution to this is very subtle as there is an atomic fast path, and thus ordering in weakly ordered architectures has to be treated very carefully.
The first commit fixes the issue by introducing a spinlock around huge_zero_[pfn, folio, refcount] write, with careful consideration paid to load/store ordering in the fast path. It is placed first and kept as small as possible so that it can be backported on its own.
The second commit is a pure cleanup which reworks the CONFIG_PERSISTENT_HUGE_ZERO_FOLIO logic to better separate the persistent logic from the dynamically allocated one.
Andrew - I know you don't like a mix of fix/cleanup, but thought it'd be easier to keep the 2 together as there's a dependency.
---
v2:
- Reordered the series so the fix now comes first with the CONFIG_PERSISTENT_HUGE_ZERO_FOLIO separation following it as a pure cleanup as per David.
- Fixed up wording as per David.
- Cleaned up Cc's since dependency of fix on cleanup no longer exists.
v1:
https://patch.msgid.link/[email protected]
To: Andrew Morton <[email protected]>
To: David Hildenbrand <[email protected]>
To: Zi Yan <[email protected]>
To: Baolin Wang <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Nico Pache <[email protected]>
To: Ryan Roberts <[email protected]>
To: Dev Jain <[email protected]>
To: Barry Song <[email protected]>
To: Lance Yang <[email protected]>
To: Usama Arif <[email protected]>
To: Pankaj Raghav <[email protected]>
To: Hannes Reinecke <[email protected]>
To: Hugh Dickins <[email protected]>
To: Yang Shi <[email protected]>
To: Kiryl Shutsemau <[email protected]>
---
Lorenzo Stoakes (ARM) (2):
mm/huge_memory: fix huge_zero_pfn race
mm/huge_memory: separate out CONFIG_PERSISTENT_HUGE_ZERO_FOLIO logic
mm/huge_memory.c | 189 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 116 insertions(+), 73 deletions(-)
---
base-commit: 5db85e34ec49beee590676d55f13e8e2b14c742f
change-id: 20260728-fix-refcounted-huge-zero-21cb07b4fa3e
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
PATCH v2 1/2 mm/huge_memory: fix huge_zero_pfn race
If !CONFIG_PERSISTENT_HUGE_ZERO_FOLIO, the huge_zero_folio is refcounted by huge_zero_refcount and returned by mm_get_huge_zero_folio().
When the caller is done with the huge zero page, its reference count is decremented. Only a shrinker can set the reference count to zero.
A race can unfortunately occur between a shrinker decrementing the reference count to zero and a concurrent page fault.
This is because shrink_huge_zero_folio_scan() might, if very unlucky, be preempted between setting huge_zero_refcount to zero and writing an invalid value.
During this time get_huge_zero_folio() could write to huge_zero_pfn before shrink_huge_zero_folio_scan() resumes.
In this event the huge zero folio will be persistently misidentified causing the THP code path to be entered inappropriately for the huge zero folio:
CPU 0 CPU 1
=======================================|=================================
shrink_huge_zero_folio_scan() |
atomic_cmpxchg() sets refcount to 0 |
xchg() sets huge_zero_folio to NULL | get_huge_zero_folio()
| | atomic_inc_not_zero() -> zero
preempted for a long time | Allocate new huge zero folio
| | Write valid huge_zero_folio
v | Write valid huge_zero_pfn Overwrite huge_zero_pfn with ~0UL <--- Invalid overwrite!
This results in is_huge_zero_pfn() and is_huge_zero_pmd() incorrectly returning false for a huge zero page which could result in issues like the huge zero folio being incorrectly split.
Note that the issue is with huge_zero_pfn not huge_zero_folio, as get_huge_zero_folio() uses cmpxchg() gated on huge_zero_folio being NULL with a retry loop and shrink_huge_zero_folio_scan() uses xchg() to set huge_zero_folio.
Fix the issue by introducing a spinlock, huge_zero_lock, to prevent concurrent write of huge_zero_folio, huge_zero_pfn and huge_zero_refcount.
There needs to be significant care taken here to ensure correctness:
The fast path in get_huge_zero_folio() uses atomic_inc_not_zero(), which is outside of the critical section, and means huge zero allocation is gated on zero huge_zero_refcount.
The fast path doesn't use huge_zero_lock, so the critical section is irrelevant to it.
So invariants are required - huge_zero_refcount MUST:
* Only be set in the huge_zero_lock critical section to ensure serialisation of huge_zero_pfn, huge_zero_folio and huge_zero_refcount writes.
* Be set non-zero only AFTER huge_zero_[pfn, folio] are set to valid values so installation of the huge zero folio on read page fault ensures concurrent is_huge_zero_*() calls correctly identify the huge zero folio.
* Be set zero only BEFORE huge_zero_[pfn, folio] are set to NULL and ~0UL respectively, and atomically.
Establish these by:
* Only setting huge_zero_refcount to zero or an absolute value in the huge_zero_lock critical section in get_huge_zero_folio() and shrink_huge_zero_folio_scan(), and always updating atomically there and elsewhere.
* Using atomic_set_release(&huge_zero_refcount) in get_huge_zero_folio() after huge_zero_[pfn, folio] are set. This is paired with atomic_inc_not_zero() to ensure atomic_inc_not_zero() only observes a non-zero value if huge_zero_[pfn, folio] are set.
* Using atomic_cmpxchg() in shrink_huge_zero_folio_scan() (as before) to ensure that it is set zero only when equal to 1 and set atomically.
* atomic_cmpxchg() being fully ordered ensures this is done prior to huge_zero_[folio, pfn] being set to NULL and ~0UL respectively.
Eliminate the retry loop in get_huge_zero_folio() as the atomic_cmpxchg() in shrink_huge_zero_folio_scan() is now performed under the lock, and replace with an equally locked atomic_inc() to set the reference count should the caller be raced on huge zero folio installation.
folio_put() naturally implies a full memory barrier so its ordering is maintained correctly.
The huge zero folio also cannot be released except when the shrinker does so as it is non-LRU and non-rmappable.
Note that only the huge zero shrinker (via shrink_huge_zero_folio_scan()) can actually set huge_zero_refcount to zero, which is the count of mm's which have at least one huge zero folio installed plus one shrinker pin.
Additionally convert a BUG_ON() to a VM_WARN_ON_ONCE().
--- mm/huge_memory.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-)
mm/huge_memory.c +29 -14
@@ -41,6 +41,7 @@#include <linux/pgalloc.h> #include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> +#include <linux/cleanup.h> #include <asm/tlb.h> #include "internal.h"
@@ -78,6 +79,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,static bool split_underused_thp = true;static atomic_t huge_zero_refcount; +static DEFINE_SPINLOCK (huge_zero_lock);struct folio *huge_zero_folio__read_mostly ;unsigned long huge_zero_pfn__read_mostly = ~0UL ;unsigned long huge_anon_orders_always__read_mostly ;
@@ -224,7 +226,8 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,static bool get_huge_zero_folio (void ) {struct folio *zero_folio; -retry: + +/* Paired with atomic_set_release(). */ if (likely (atomic_inc_not_zero (&huge_zero_refcount)))return true;
@@ -237,17 +240,22 @@ static bool get_huge_zero_folio(void) }/* Ensure zero folio won't have large_rmappable flag set. */ folio_clear_large_rmappable (zero_folio); -preempt_disable (); -if (cmpxchg (&huge_zero_folio,NULL , zero_folio)) { -preempt_enable (); + +/* Paired with critical section in shrink_huge_zero_folio_scan(). */ +spin_lock (&huge_zero_lock); +if (huge_zero_folio) { +/* Somebody else already installed it. */ +atomic_inc (&huge_zero_refcount); +spin_unlock (&huge_zero_lock);folio_put (zero_folio); - goto retry; +return true; } +WRITE_ONCE (huge_zero_folio, zero_folio);WRITE_ONCE (huge_zero_pfn,folio_pfn (zero_folio)); +/* Paired with atomic_inc_not_zero(). +1 for shrinker pin. */ +atomic_set_release (&huge_zero_refcount,2 ); +spin_unlock (&huge_zero_lock); -/* We take additional reference here. It will be put back by shrinker */ -atomic_set (&huge_zero_refcount,2 ); -preempt_enable ();count_vm_event (THP_ZERO_PAGE_ALLOC );return true; }
@@ -297,15 +305,22 @@ static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink,static unsigned long shrink_huge_zero_folio_scan (struct shrinker *shrink,struct shrink_control *sc) { -if (atomic_cmpxchg (&huge_zero_refcount,1 ,0 ) ==1 ) { -struct folio *zero_folio =xchg (&huge_zero_folio,NULL ); -BUG_ON (zero_folio ==NULL ); +struct folio *zero_folio; + +/* Paired with critical section in get_huge_zero_folio(). */ + scoped_guard(spinlock, &huge_zero_lock) { +/* Paired with atomic_inc_not_zero() in get_huge_zero_folio(). */ +if (atomic_cmpxchg (&huge_zero_refcount,1 ,0 ) !=1 ) +return 0 ; + + zero_folio = huge_zero_folio; +VM_WARN_ON_ONCE (!zero_folio); +WRITE_ONCE (huge_zero_folio,NULL );WRITE_ONCE (huge_zero_pfn, ~0UL ); -folio_put (zero_folio); -return HPAGE_PMD_NR ; } -return 0 ; +folio_put (zero_folio); +return HPAGE_PMD_NR ; }static struct shrinker *huge_zero_folio_shrinker;
Signature · 1 line
2.55.0
PATCH v2 2/2 mm/huge_memory: separate out CONFIG_PERSISTENT_HUGE_ZERO_FOLIO logic
In reply to Lorenzo Stoakes (ARM)Rather than mixing the refcounted and non-refcounted CONFIG_PERSISTENT_HUGE_ZERO_FOLIO logic, separate the two out cleanly so it is clear what happens when this configuration option is set and what happens when it is not.
Introduce HUGE_ZERO_UNSET_PFN to abstract the ~0UL assignment, only introduce the refcount, lock and shrinker if !CONFIG_PERSISTENT_HUGE_ZERO_FOLIO, abstract initialisation and teardown, abstract the huge zero folio allocation from refcounting.
Also change a BUG_ON() to WARN_ON_ONCE() while we're at it.
No functional change intended.
--- mm/huge_memory.c | 160 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 94 insertions(+), 66 deletions(-)
mm/huge_memory.c +94 -66
@@ -78,10 +78,15 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, struct shrink_control *sc);static bool split_underused_thp = true; +#define HUGE_ZERO_UNSET_PFN (~0UL) +struct folio *huge_zero_folio__read_mostly ; +unsigned long huge_zero_pfn__read_mostly =HUGE_ZERO_UNSET_PFN ; +#ifndef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO static atomic_t huge_zero_refcount;static DEFINE_SPINLOCK (huge_zero_lock); -struct folio *huge_zero_folio__read_mostly ; -unsigned long huge_zero_pfn__read_mostly = ~0UL ; +static struct shrinker *huge_zero_folio_shrinker; +#endif +unsigned long huge_anon_orders_always__read_mostly ;unsigned long huge_anon_orders_madvise__read_mostly ;unsigned long huge_anon_orders_inherit__read_mostly ;
@@ -223,23 +228,58 @@ unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma,return orders; } -static bool get_huge_zero_folio (void ) +static struct folio *alloc_huge_zero_folio (void ) {struct folio *zero_folio; -/* Paired with atomic_set_release(). */ -if (likely (atomic_inc_not_zero (&huge_zero_refcount))) -return true; - zero_folio =folio_alloc ((GFP_TRANSHUGE | __GFP_ZERO | __GFP_ZEROTAGS) & ~__GFP_MOVABLE,HPAGE_PMD_ORDER );if (!zero_folio) {count_vm_event (THP_ZERO_PAGE_ALLOC_FAILED ); -return false; +return NULL ; + } +folio_clear_large_rmappable (zero_folio);/* Explicitly not rmappable. */ +return zero_folio; +} + +#ifdef CONFIG_PERSISTENT_HUGE_ZERO_FOLIO +static int __init huge_zero_init (void ) +{ + huge_zero_folio =alloc_huge_zero_folio (); +if (!huge_zero_folio) { +pr_warn ("Allocating persistent huge zero folio failed\n" ); + }else { + huge_zero_pfn =folio_pfn (huge_zero_folio); +count_vm_event (THP_ZERO_PAGE_ALLOC ); } -/* Ensure zero folio won't have large_rmappable flag set. */ -folio_clear_large_rmappable (zero_folio); +return 0 ; +} + +static void __init huge_zero_shrinker_exit (void ) +{ +} + +struct folio *mm_get_huge_zero_folio (struct mm_struct *mm) +{ +return huge_zero_folio; +} + +void mm_put_huge_zero_folio (struct mm_struct *mm) +{ +} +#else +static bool get_huge_zero_folio (void ) +{ +struct folio *zero_folio; + +/* Paired with atomic_set_release(). */ +if (likely (atomic_inc_not_zero (&huge_zero_refcount))) +return true; + + zero_folio =alloc_huge_zero_folio (); +if (unlikely (!zero_folio)) +return false;/* Paired with critical section in shrink_huge_zero_folio_scan(). */ spin_lock (&huge_zero_lock);
@@ -266,33 +306,7 @@ static void put_huge_zero_folio(void)* Counter should never go to zero here. Only shrinker can put * last reference. */ -BUG_ON (atomic_dec_and_test (&huge_zero_refcount)); -} - -struct folio *mm_get_huge_zero_folio (struct mm_struct *mm) -{ -if (IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO )) -return huge_zero_folio; - -if (mm_flags_test (MMF_HUGE_ZERO_FOLIO , mm)) -return READ_ONCE (huge_zero_folio); - -if (!get_huge_zero_folio ()) -return NULL ; - -if (mm_flags_test_and_set (MMF_HUGE_ZERO_FOLIO , mm)) -put_huge_zero_folio (); - -return READ_ONCE (huge_zero_folio); -} - -void mm_put_huge_zero_folio (struct mm_struct *mm) -{ -if (IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO )) -return ; - -if (mm_flags_test (MMF_HUGE_ZERO_FOLIO , mm)) -put_huge_zero_folio (); +WARN_ON_ONCE (atomic_dec_and_test (&huge_zero_refcount)); } static unsigned long shrink_huge_zero_folio_count(struct shrinker *shrink,
@@ -316,14 +330,53 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, zero_folio = huge_zero_folio;VM_WARN_ON_ONCE (!zero_folio);WRITE_ONCE (huge_zero_folio,NULL ); -WRITE_ONCE (huge_zero_pfn, ~0UL ); + WRITE_ONCE(huge_zero_pfn, HUGE_ZERO_UNSET_PFN); }folio_put (zero_folio);return HPAGE_PMD_NR ; } -static struct shrinker *huge_zero_folio_shrinker; +static int __init huge_zero_init (void ) +{ + huge_zero_folio_shrinker =shrinker_alloc (0 ,"thp-zero" ); +if (!huge_zero_folio_shrinker) { +shrinker_free (deferred_split_shrinker); +list_lru_destroy (&deferred_split_lru); +return -ENOMEM ; + } + + huge_zero_folio_shrinker->count_objects = shrink_huge_zero_folio_count; + huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan; +shrinker_register (huge_zero_folio_shrinker); +return 0 ; +} + +static void __init huge_zero_shrinker_exit (void ) +{ +shrinker_free (huge_zero_folio_shrinker); +} + +struct folio *mm_get_huge_zero_folio (struct mm_struct *mm) +{ +if (mm_flags_test (MMF_HUGE_ZERO_FOLIO , mm)) +return READ_ONCE (huge_zero_folio); + +if (!get_huge_zero_folio ()) +return NULL ; + +if (mm_flags_test_and_set (MMF_HUGE_ZERO_FOLIO , mm)) +put_huge_zero_folio (); + +return READ_ONCE (huge_zero_folio); +} + +void mm_put_huge_zero_folio (struct mm_struct *mm) +{ +if (mm_flags_test (MMF_HUGE_ZERO_FOLIO , mm)) +put_huge_zero_folio (); +} +#endif /* CONFIG_PERSISTENT_HUGE_ZERO_FOLIO */ #ifdef CONFIG_SYSFS static ssize_t enabled_show(struct kobject *kobj,
@@ -987,39 +1040,14 @@ static int __init thp_shrinker_init(void) deferred_split_shrinker->scan_objects = deferred_split_scan;shrinker_register (deferred_split_shrinker); -if (IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO )) { -/* -* Bump the reference of the huge_zero_folio and do not -* initialize the shrinker. -* -* huge_zero_folio will always be NULL on failure. We assume -* that get_huge_zero_folio() will most likely not fail as -* thp_shrinker_init() is invoked early on during boot. -*/ -if (!get_huge_zero_folio ()) -pr_warn ("Allocating persistent huge zero folio failed\n" ); -return 0 ; - } - - huge_zero_folio_shrinker =shrinker_alloc (0 ,"thp-zero" ); -if (!huge_zero_folio_shrinker) { -shrinker_free (deferred_split_shrinker); -list_lru_destroy (&deferred_split_lru); -return -ENOMEM ; - } - - huge_zero_folio_shrinker->count_objects = shrink_huge_zero_folio_count; - huge_zero_folio_shrinker->scan_objects = shrink_huge_zero_folio_scan; -shrinker_register (huge_zero_folio_shrinker); - -return 0 ; +return huge_zero_init (); }static void __init thp_shrinker_exit (void ) { -shrinker_free (huge_zero_folio_shrinker);shrinker_free (deferred_split_shrinker);list_lru_destroy (&deferred_split_lru); +huge_zero_shrinker_exit (); }static int __init hugepage_init (void )
Signature · 1 line
2.55.0
Quoted 100 lines · Lorenzo Stoakes (ARM)
If !CONFIG_PERSISTENT_HUGE_ZERO_FOLIO, the
huge_zero_foliois refcounted byhuge_zero_refcountand returned bymm_get_huge_zero_folio().When the caller is done with the huge zero page, its reference count is decremented. Only a shrinker can set the reference count to zero.
A race can unfortunately occur between a shrinker decrementing the reference count to zero and a concurrent page fault.
This is because
shrink_huge_zero_folio_scan()might, if very unlucky, be preempted between settinghuge_zero_refcountto zero and writing an invalid value.During this time
get_huge_zero_folio()could write tohuge_zero_pfnbeforeshrink_huge_zero_folio_scan()resumes.In this event the huge zero folio will be persistently misidentified causing the THP code path to be entered inappropriately for the huge zero folio:
CPU 0 CPU 1
=======================================|================================= shrink_huge_zero_folio_scan() | atomic_cmpxchg() sets refcount to 0 | xchg() sets huge_zero_folio to NULL | get_huge_zero_folio() | | atomic_inc_not_zero() -> zero preempted for a long time | Allocate new huge zero folio | | Write valid huge_zero_folio v | Write valid huge_zero_pfnOverwrite
huge_zero_pfnwith ~0UL <--- Invalid overwrite!This results in
is_huge_zero_pfn()andis_huge_zero_pmd()incorrectly returning false for a huge zero page which could result in issues like the huge zero folio being incorrectly split.Note that the issue is with
huge_zero_pfnnothuge_zero_folio, asget_huge_zero_folio()usescmpxchg()gated onhuge_zero_foliobeing NULL with a retry loop andshrink_huge_zero_folio_scan()usesxchg()to sethuge_zero_folio.Fix the issue by introducing a spinlock,
huge_zero_lock, to prevent concurrent write ofhuge_zero_folio,huge_zero_pfnandhuge_zero_refcount.There needs to be significant care taken here to ensure correctness:
The fast path in
get_huge_zero_folio()usesatomic_inc_not_zero(), which is outside of the critical section, and means huge zero allocation is gated on zerohuge_zero_refcount.The fast path doesn't use
huge_zero_lock, so the critical section is irrelevant to it.So invariants are required -
huge_zero_refcountMUST:* Only be set in the
huge_zero_lockcritical section to ensure serialisation ofhuge_zero_pfn,huge_zero_folioandhuge_zero_refcountwrites.* Be set non-zero only AFTER huge_zero_[pfn, folio] are set to valid values so installation of the huge zero folio on read page fault ensures concurrent is_huge_zero_*() calls correctly identify the huge zero folio.
* Be set zero only BEFORE huge_zero_[pfn, folio] are set to NULL and ~0UL respectively, and atomically.
Establish these by:
* Only setting
huge_zero_refcountto zero or an absolute value in thehuge_zero_lockcritical section inget_huge_zero_folio()andshrink_huge_zero_folio_scan(), and always updating atomically there and elsewhere.* Using
atomic_set_release(&huge_zero_refcount)inget_huge_zero_folio()after huge_zero_[pfn, folio] are set. This is paired withatomic_inc_not_zero()to ensureatomic_inc_not_zero()only observes a non-zero value if huge_zero_[pfn, folio] are set.* Using
atomic_cmpxchg()inshrink_huge_zero_folio_scan()(as before) to ensure that it is set zero only when equal to 1 and set atomically.*
atomic_cmpxchg()being fully ordered ensures this is done prior to huge_zero_[folio, pfn] being set to NULL and ~0UL respectively.Eliminate the retry loop in
get_huge_zero_folio()as theatomic_cmpxchg()inshrink_huge_zero_folio_scan()is now performed under the lock, and replace with an equally lockedatomic_inc()to set the reference count should the caller be raced on huge zero folio installation.
folio_put()naturally implies a full memory barrier so its ordering is maintained correctly.The huge zero folio also cannot be released except when the shrinker does so as it is non-LRU and non-rmappable.
Note that only the huge zero shrinker (via
shrink_huge_zero_folio_scan()) can actually sethuge_zero_refcountto zero, which is the count of mm's which have at least one huge zero folio installed plus one shrinker pin.Additionally convert a
BUG_ON()to aVM_WARN_ON_ONCE().
Quoted 7 lines
Suggested-by David Hildenbrand (Arm) <[email protected]>Reported-by Hengbin Zhang <[email protected]>Fixes 3b77e8c8cde5 ("mm/thp: make is_huge_zero_pmd() safe and quicker")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
Thanks!
Signature · 3 lines
Cheers, David
Andrew Morton
maintainerQuoted 20 lines · On Thu, 30 Jul 2026 11:55:46 +0100 "Lorenzo Stoakes (ARM)" <[email protected]>…
There is a subtle race in the reference-counted
huge_zero_folioimplementation.The fast path atomic logic fails to account for the fact that the shrinker (which drops the final
huge_zero_refcountpin) can overwritehuge_zero_pfnwith the ~0UL sentinel value inshrink_huge_zero_folio_scan()after a racingget_huge_zero_folio()installed a valid value there.This results in
huge_zero_foliobeing correctly set buthuge_zero_pfnbeing set incorrectly and thusis_huge_zero_pfn()and consequentlyis_huge_zero_pmd()will misidentify the huge zero folio as being an ordinary THP folio.This can result in the huge zero folio being split and otherwise treated incorrectly.
The solution to this is very subtle as there is an atomic fast path, and thus ordering in weakly ordered architectures has to be treated very carefully.
Quoted 8 lines
The first commit fixes the issue by introducing a spinlock around huge_zero_[pfn, folio, refcount] write, with careful consideration paid to load/store ordering in the fast path. It is placed first and kept as small as possible so that it can be backported on its own.
The second commit is a pure cleanup which reworks the
CONFIG_PERSISTENT_HUGE_ZERO_FOLIOlogic to better separate the persistent logic from the dynamically allocated one.
Thanks, updated.
And moved to mm-hotfixes. But is it appropriate to fast-track a tricky-looking fix to a four year old bug?
Quoted 2 lines
Andrew - I know you don't like a mix of fix/cleanup, but thought it'd be easier to keep the 2 together as there's a dependency.
Sure.
If we were to upstream just the fix then really we should defer merging the cleanup into any tree until a later time. Because the cleanup might accidentally fix a flaw in the fix. Sending just half the series upstream results in an untested code combination. Unlikely, but it's happened a small number of times and it's a risk. Keeping both patches together eliminates that risk.
Quoted 7 lines
---
v2:
- Reordered the series so the fix now comes first with theCONFIG_PERSISTENT_HUGE_ZERO_FOLIOseparation following it as a pure cleanup as per David.
- Fixed up wording as per David.
- Cleaned up Cc's since dependency of fix on cleanup no longer exists.
Here's how v2 altered mm.git. One changed line, unchangelogged!
mm/huge_memory.c renamed from mm/huge_memory.c~b +1 -1
@@ -328,7 +328,7 @@ static unsigned long shrink_huge_zero_foreturn 0 ; zero_folio = huge_zero_folio; -VM_WARN_ON_ONCE (!huge_zero_folio); +VM_WARN_ON_ONCE (!zero_folio);WRITE_ONCE (huge_zero_folio,NULL );WRITE_ONCE (huge_zero_pfn,HUGE_ZERO_UNSET_PFN ); }
_
Sashiko said some waffle because it still doesn't understand that __init-time allocations are considered immortal.
A cleanup opportunity is to get into this code and formalize that convention. And the convention that debugfs return errors are to be ignored.
David Hildenbrand (Arm)
maintainerRe: [PATCH mm-hotfixes v2 2/2] mm/huge_memory: separate out CONFIG_PERSISTENT_HUGE_ZERO_FOLIO logic
In reply to Lorenzo Stoakes (ARM)Quoted 5 lines · Lorenzo Stoakes (ARM)
Rather than mixing the refcounted and non-refcounted
CONFIG_PERSISTENT_HUGE_ZERO_FOLIOlogic, separate the two out cleanly so it is clear what happens when this configuration option is set and what happens when it is not.
Quoted 8 lines
Introduce
HUGE_ZERO_UNSET_PFNto abstract the ~0UL assignment, only introduce the refcount, lock and shrinker if !CONFIG_PERSISTENT_HUGE_ZERO_FOLIO, abstract initialisation and teardown, abstract the huge zero folio allocation from refcounting.Also change a
BUG_ON()toWARN_ON_ONCE()while we're at it.No functional change intended.
Ugh. Why is this patch already upsteam with
It's absolutely not a fix. It's a cleanup that should have been reviewed properly.
I really wanted to review this.
Making David grumpy in the morning, good job everyone.
Signature · 3 lines
Cheers, David
Re: [PATCH mm-hotfixes v2 2/2] mm/huge_memory: separate out CONFIG_PERSISTENT_HUGE_ZERO_FOLIO logic
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 10 lines · Lorenzo Stoakes (ARM)
Rather than mixing the refcounted and non-refcounted
CONFIG_PERSISTENT_HUGE_ZERO_FOLIOlogic, separate the two out cleanly so it is clear what happens when this configuration option is set and what happens when it is not.Introduce
HUGE_ZERO_UNSET_PFNto abstract the ~0UL assignment, only introduce the refcount, lock and shrinker if !CONFIG_PERSISTENT_HUGE_ZERO_FOLIO, abstract initialisation and teardown, abstract the huge zero folio allocation from refcounting.
Quoted 8 lines
Quoted 3 lines
Also change a
BUG_ON()toWARN_ON_ONCE()while we're at it.No functional change intended.
Ugh. Why is this patch already upsteam with
Fixes 3b77e8c8cde5 ("mm/thp: make is_huge_zero_pmd() safe and quicker")Cc <[email protected]>
Yeah these tags weren't part of it + it was intended as a separate thing but I guess it got lost in translation.
I should probably have just sent it entirely separately after the fix landed so my bad on that part.
Quoted 6 lines
It's absolutely not a fix. It's a cleanup that should have been reviewed properly.
I really wanted to review this.
Making David grumpy in the morning, good job everyone.
Sorry about that :) At least it's not schlop? ;))
Quoted 5 lines
--
Cheers,David
--
Cheers, Lorenzo