linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] huge_mm.h: is_huge_zero_folio(NULL) should return false
@ 2025-08-26 23:16 Max Kellermann
  2025-08-26 23:16 ` [PATCH 2/2] mm/swap: add BUG_ON(folio==NULL) to folios_put_refs() Max Kellermann
                   ` (2 more replies)
  0 siblings, 3 replies; 27+ messages in thread
From: Max Kellermann @ 2025-08-26 23:16 UTC (permalink / raw)
  To: akpm, david, lorenzo.stoakes, ziy, baolin.wang, Liam.Howlett,
	npache, ryan.roberts, dev.jain, baohua, shikemeng, kasong,
	nphamcs, bhe, chrisl, linux-mm, linux-kernel
  Cc: Max Kellermann

Calling is_huge_zero_folio(NULL) should not be legal - it makes no
sense, and a different (theoretical) implementation may dereference
the pointer.  But currently, lacking any explicit documentation, this
call is legal.

But if somebody really passes NULL, the function should not return
true - this isn't the huge zero folio after all!  However, if the
`huge_zero_folio` hasn't been allocated yet, it's NULL, and
is_huge_zero_folio(NULL) just happens to return true, which is a lie.

I believe this is a negligible corner case and I don't want to add any
overhead for this; but in debugging kernels, it may be helpful to add
this check, therefore I put it inside an `#ifdef CONFIG_DEBUG_VM`.

This weird side effect prevented me from reproducing a kernel crash
that occurred when the elements of a folio_batch were NULL - since
folios_put_refs() skips huge zero folios, this sometimes causes a
crash, but sometimes does not.  For debugging, it is better to reveal
such bugs reliably and not hide them behind random preconditions like
"has the huge zero folio already been created?"

Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
---
 include/linux/huge_mm.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index 7748489fde1b..e4c617c0b445 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -479,7 +479,12 @@ extern unsigned long huge_zero_pfn;
 
 static inline bool is_huge_zero_folio(const struct folio *folio)
 {
-	return READ_ONCE(huge_zero_folio) == folio;
+	const struct folio *hzf = READ_ONCE(huge_zero_folio);
+#ifdef CONFIG_DEBUG_VM
+	if (hzf == NULL)
+		return false;
+#endif
+	return hzf == folio;
 }
 
 static inline bool is_huge_zero_pfn(unsigned long pfn)
-- 
2.47.2



^ permalink raw reply related	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2025-08-29  1:25 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-26 23:16 [PATCH 1/2] huge_mm.h: is_huge_zero_folio(NULL) should return false Max Kellermann
2025-08-26 23:16 ` [PATCH 2/2] mm/swap: add BUG_ON(folio==NULL) to folios_put_refs() Max Kellermann
2025-08-27  1:42   ` Zi Yan
2025-08-27  2:12     ` Chris Li
2025-08-27  9:38   ` David Hildenbrand
2025-08-27  1:19 ` [PATCH 1/2] huge_mm.h: is_huge_zero_folio(NULL) should return false Zi Yan
2025-08-27  1:55 ` Andrew Morton
2025-08-27  4:39   ` Max Kellermann
2025-08-27  9:36     ` David Hildenbrand
2025-08-27 10:13       ` Max Kellermann
2025-08-27 11:56         ` David Hildenbrand
2025-08-27 13:06           ` Max Kellermann
2025-08-27 14:34             ` David Hildenbrand
2025-08-27 15:03               ` [PATCH v2] huge_mm.h: disallow is_huge_zero_folio(NULL) Max Kellermann
2025-08-27 15:16                 ` Lorenzo Stoakes
2025-08-27 15:38                   ` Max Kellermann
2025-08-27 20:01                   ` David Hildenbrand
2025-08-27 20:26                     ` David Hildenbrand
2025-08-27 20:44                       ` Lorenzo Stoakes
2025-08-28  8:48                     ` [PATCH v3] " Max Kellermann
2025-08-28  9:35                       ` Lorenzo Stoakes
2025-08-29  1:25                       ` Zi Yan
2025-08-27 23:53                 ` [PATCH v2] " Andrew Morton
2025-08-28  5:17                   ` Max Kellermann
2025-08-28  7:57                   ` David Hildenbrand
2025-08-27 15:05               ` [PATCH 1/2] huge_mm.h: is_huge_zero_folio(NULL) should return false Max Kellermann
2025-08-27  9:35   ` David Hildenbrand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).