Re: [PATCH v10] mm: fix ASSERT_EXCLUSIVE_BITS by passing memdesc_flags_t by pointer

From: David Hildenbrand (Arm)

Date: Wed Jul 08 2026 - 04:11:20 EST


On 7/8/26 04:15, Hui Zhu wrote:
> From: Hui Zhu <zhuhui@xxxxxxxxxx>
>
> KCSAN reports a data race between page_to_nid()/folio_pgdat() reading
> page->flags and folio_trylock()/folio_lock() concurrently doing
> test_and_set_bit_lock(PG_locked, ...) on the same word, e.g.:
>
> BUG: KCSAN: data-race in __lruvec_stat_mod_folio / shmem_get_folio_gfp
>
> The race is benign: nid/zone bits are set once at page init and never
> overlap with PG_locked. However, ASSERT_EXCLUSIVE_BITS() inside
> memdesc_nid/zonenum() was checking a by-value copy of the flags word,
> not the live page->flags, so it failed to annotate the real access.
>
> Change memdesc_nid(), memdesc_zonenum(), memdesc_section(), and
> memdesc_is_zone_device() to take a const memdesc_flags_t * and update
> all callers to pass &page->flags / &folio->flags, so
> ASSERT_EXCLUSIVE_BITS() operates on the actual shared word.
>
> Guard the ASSERT_EXCLUSIVE_BITS() call in memdesc_zonenum() under
> ZONES_WIDTH != 0 to avoid a zero-mask check on configs where the zone
> field is absent. memdesc_section() needs no such guard, since
> SECTIONS_WIDTH is never 0 wherever SECTION_IN_PAGE_FLAGS is defined.
> Under CONFIG_NUMA=n, memdesc_nid() itself is stubbed to "return 0"
> instead of reading page->flags, since NODES_MASK is 0 and the check
> can never fire; page_to_nid()/folio_nid() now just call memdesc_nid()
> unconditionally and rely on that stub, instead of duplicating the
> CONFIG_NUMA split at each call site.
>
> Co-developed-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> Signed-off-by: Hui Zhu <zhuhui@xxxxxxxxxx>
> ---
> Changelog:
> v10:
> According to the comments of David, drop the redundant CONFIG_NUMA split
> in page_to_nid()/folio_nid() and remove the SECTIONS_WIDTH != 0 guard
> around ASSERT_EXCLUSIVE_BITS() in memdesc_section().
> v9:
> Add the SECTIONS_WIDTH check to memdesc_section.
> v8:
> According to the comments of Andrew, include kcsan-checks.h in mm.h.
> Incorporate David's patch that switch memdesc_nid(), memdesc_zonenum(),
> memdesc_section() and memdesc_is_zone_device() to take a const
> memdesc_flags_t * instead of using a per-accessor macro/call-site hack.
> Update all callers accordingly and extend the same exclusive-bits check
> to memdesc_section() and memdesc_is_zone_device(), guarded by
> SECTIONS_WIDTH != 0 / reusing ZONES_WIDTH != 0 to avoid zero-mask checks
> on configs without the corresponding field.
> v7:
> According to the comments of Sashiko, restrict the memdesc_nid() macro
> to CONFIG_NUMA, keeping a plain "return 0" static inline stub otherwise,
> and re-add a local page pointer in page_to_nid() to avoid evaluating
> PF_POISONED_CHECK(page) twice.
> v6:
> According to the comments of David, turn memdesc_nid() from a static
> inline function into a macro so ASSERT_EXCLUSIVE_BITS() can check the
> caller's page->flags/folio->flags directly.
> v5:
> According to the comments of Sashiko, guard the ASSERT_EXCLUSIVE_BITS()
> calls with #ifndef NODE_NOT_IN_PAGE_FLAGS (for nid) and #if
> ZONES_WIDTH != 0 (for zonenum).
> According to the comments of David, avoid calling
> PF_POISONED_CHECK(page) twice in page_to_nid().
> According to the warning of lkp, switch the CONFIG_NUMA=n
> page_to_nid()/folio_nid() stubs from macros to static inline functions.
> v4:
> According to the comments of Andrew and Sashiko, set
> page_to_nid()/folio_nid() as static inline stubs returning 0
> under CONFIG_NUMA=n.
> v3:
> According to the comments of Andrew and Sashiko, move
> ASSERT_EXCLUSIVE_BITS out of memdesc_nid()/memdesc_zonenum()
> into the page/folio call sites.
> v2:
> According to the comments of David, remove useless comments and use
> ASSERT_EXCLUSIVE_BITS() in memdesc_nid() instead of data_race() in
> page_to_nid().
>
> include/asm-generic/memory_model.h | 2 +-
> include/linux/mm.h | 28 ++++++++++++++++++++--------
> include/linux/mm_inline.h | 4 ++--
> include/linux/mmzone.h | 26 ++++++++++++++------------
> mm/page_alloc.c | 6 +++---
> mm/slab.h | 2 +-
> mm/sparse.c | 2 +-
> 7 files changed, 42 insertions(+), 28 deletions(-)
>
> diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h
> index fd74de50b054..c6b4eafaf4cb 100644
> --- a/include/asm-generic/memory_model.h
> +++ b/include/asm-generic/memory_model.h
> @@ -53,7 +53,7 @@ static inline int pfn_valid(unsigned long pfn)
> */
> #define __page_to_pfn(pg) \
> ({ const struct page *__pg = (pg); \
> - int __sec = memdesc_section(__pg->flags); \
> + int __sec = memdesc_section(&__pg->flags); \
> (unsigned long)(__pg - __section_mem_map_addr(__nr_to_section(__sec))); \
> })
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 2101e5205fc0..01683d50b3cd 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -37,6 +37,7 @@
> #include <linux/bitmap.h>
> #include <linux/bitops.h>
> #include <linux/iommu-debug-pagealloc.h>
> +#include <linux/kcsan-checks.h>
>
> struct mempolicy;
> struct anon_vma;
> @@ -2286,22 +2287,32 @@ static inline int page_zone_id(struct page *page)
> }
>
> #ifdef NODE_NOT_IN_PAGE_FLAGS
> -int memdesc_nid(memdesc_flags_t mdf);
> +int memdesc_nid(const memdesc_flags_t *mdf);
> #else
> -static inline int memdesc_nid(memdesc_flags_t mdf)
> +#ifdef CONFIG_NUMA
> +static inline int memdesc_nid(const memdesc_flags_t *mdf)
> {
> - return (mdf.f >> NODES_PGSHIFT) & NODES_MASK;
> + ASSERT_EXCLUSIVE_BITS(mdf->f, NODES_MASK << NODES_PGSHIFT);
> + return (mdf->f >> NODES_PGSHIFT) & NODES_MASK;
> }
> +#else
> +static inline int memdesc_nid(const memdesc_flags_t *mdf)
> +{
> + return 0;
> +}
> +#endif
> #endif
>
> static inline int page_to_nid(const struct page *page)
> {
> - return memdesc_nid(PF_POISONED_CHECK(page)->flags);
> + const struct page *p = PF_POISONED_CHECK(page);
> +
> + return memdesc_nid(&p->flags);
> }

Nit: Why not keep it as

return memdesc_nid(&(PF_POISONED_CHECK(page)->flags));

Apart from that

Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

--
Cheers,

David