With CONFIG_PERSISTENT_HUGE_ZERO_FOLIO=n, mm_get_huge_zero_folio() returns READ_ONCE(huge_zero_folio) directly if MMF_HUGE_ZERO_FOLIO is already set in mm->flags. The flag test is a plain test_bit(), so the flag load and the huge_zero_folio load are ordered only by a control dependency, and a control dependency does not order a later load (Documentation/memory-barriers.txt: "A load-load control dependency requires a full read memory barrier"). The flag is set by mm_get_huge_zero_folio() on another thread of the same mm: get_huge_zero_folio() stores huge_zero_folio, and the fully ordered mm_flags_test_and_set() then sets MMF_HUGE_ZERO_FOLIO. A reader can see the flag set and still load the old value of huge_zero_folio, NULL, if the folio has just been allocated. do_huge_pmd_anonymous_page() then counts THP_FAULT_FALLBACK and returns VM_FAULT_FALLBACK: the fault falls back to a normal page; only statistics are affected. The window exists only if, on the reader's fault path, khugepaged_enter_vma() does not perform its fully ordered test-and-set of MMF_VM_HUGEPAGE on mm->flags after the writer has set MMF_HUGE_ZERO_FOLIO (for example because MMF_VM_HUGEPAGE is already set); such a test-and-set would order the two loads. Commit 33192a26cdde ("mm/huge_memory: fix huge_zero_pfn race") fixed the writer side of the refcount fast path, pairing atomic_set_release() of huge_zero_refcount with atomic_inc_not_zero(). This patch fixes the reader side of the per-mm flag fast path: add smp_rmb() between the flag test and the huge_zero_folio load, paired with the full barrier of mm_flags_test_and_set(). An acquire variant of mm_flags_test() would also work; this keeps the change local. The outcome is allowed by the Linux kernel memory model for a litmus test derived from the code, and by the AArch64 and PPC models for a litmus test of the pattern. With smp_rmb() between the flag test and the huge_zero_folio load, herd7 forbids it in the pattern tests (AArch64 and PPC models). The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Compile-tested with clang on arm64 and x86-64 with W=1, and checked that dmb ishld is emitted between the two loads in arm64 object code; not runtime-tested. Fixes: 6fcb52a56ff6 ("thp: reduce usage of huge zero page's atomic counter") Cc: Lorenzo Stoakes Signed-off-by: Zhenwen Liang --- mm/huge_memory.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 75e8641bf9ff..c31fc1c7d01e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -395,8 +395,11 @@ static void __init huge_zero_shrinker_exit(void) struct folio *mm_get_huge_zero_folio(struct mm_struct *mm) { - if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) + if (mm_flags_test(MMF_HUGE_ZERO_FOLIO, mm)) { + /* Paired with the full barrier of mm_flags_test_and_set(). */ + smp_rmb(); return READ_ONCE(huge_zero_folio); + } if (!get_huge_zero_folio()) return NULL; -- 2.56.1.839.g22a8290db9-goog