From: Xie Yuanbin <qq570070308@gmail.com>
To: rppt@kernel.org, david.laight.linux@gmail.com,
akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org,
Liam.Howlett@oracle.com, vbabka@kernel.org, surenb@google.com,
mhocko@suse.com, nathan@kernel.org,
nick.desaulniers+lkml@gmail.com, morbo@google.com,
justinstitt@google.com
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
llvm@lists.linux.dev, Xie Yuanbin <qq570070308@gmail.com>
Subject: [PATCH] mm: optimize the implementation of WARN_ON_ONCE_GFP()
Date: Mon, 9 Mar 2026 23:38:11 +0800 [thread overview]
Message-ID: <20260309153811.40958-1-qq570070308@gmail.com> (raw)
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
next reply other threads:[~2026-03-09 15:38 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-09 15:38 Xie Yuanbin [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260309153811.40958-1-qq570070308@gmail.com \
--to=qq570070308@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=david.laight.linux@gmail.com \
--cc=david@kernel.org \
--cc=justinstitt@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=llvm@lists.linux.dev \
--cc=mhocko@suse.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox