public inbox for linux-mm@kvack.org
 help / color / mirror / Atom feed
* [PATCH] mm: optimize the implementation of WARN_ON_ONCE_GFP()
@ 2026-03-09 15:38 Xie Yuanbin
  2026-03-09 15:40 ` Matthew Wilcox
  0 siblings, 1 reply; 6+ messages in thread
From: Xie Yuanbin @ 2026-03-09 15:38 UTC (permalink / raw)
  To: rppt, david.laight.linux, akpm, david, ljs, Liam.Howlett, vbabka,
	surenb, mhocko, nathan, nick.desaulniers+lkml, morbo, justinstitt
  Cc: linux-mm, linux-kernel, llvm, Xie Yuanbin

As shown in the commit message of commit 242b872239f6a7deacbc
("include/linux/once_lite.h: fix judgment in WARN_ONCE with clang"),
the code "unlikely(a && b)" may generate poor assembly code if it is
actually "unlikely(a) && unlikely(b)" or "unlikely(a) && b".

WARN_ON_ONCE_GFP() may be used in the hot path code:
1. The argument cond shoud be unlikely.
2. When "1." is true, !(gfp & __GFP_NOWARN) is unlikely, otherwise, a
   WARN may be triggered, which is a very unlikely case.
3. When "1. && 2." is true, just like the implementation of WARN_ONCE(),
   !__warned can be unlikely.

Reorder __ret_warn_once judgement to first and split out the unlikely()
in WARN_ON_ONCE_GFP() to optimize performance.

Cc: Mike Rapoport <rppt@kernel.org>
Cc: David Laight <david.laight.linux@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Xie Yuanbin <qq570070308@gmail.com>
---
 mm/internal.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/internal.h b/mm/internal.h
index 6e1162e13289..49d2b7a270d3 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -93,7 +93,8 @@ struct pagetable_move_control {
 	static bool __section(".data..once") __warned;			\
 	int __ret_warn_once = !!(cond);					\
 									\
-	if (unlikely(!(gfp & __GFP_NOWARN) && __ret_warn_once && !__warned)) { \
+	if (unlikely(__ret_warn_once) &&				\
+	    unlikely(!(gfp & __GFP_NOWARN)) && unlikely(!__warned)) {	\
 		__warned = true;					\
 		WARN_ON(1);						\
 	}								\
-- 
2.53.0



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

end of thread, other threads:[~2026-03-27  5:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-09 15:38 [PATCH] mm: optimize the implementation of WARN_ON_ONCE_GFP() Xie Yuanbin
2026-03-09 15:40 ` Matthew Wilcox
2026-03-09 15:59   ` Xie Yuanbin
2026-03-10 10:55     ` Vlastimil Babka (SUSE)
2026-03-10 14:52       ` David Laight
2026-03-27  5:34         ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox