Building the Linux kernel with Clang and LLVM
 help / color / mirror / Atom feed
From: Xie Yuanbin <qq570070308@gmail.com>
To: nathan@kernel.org, nick.desaulniers+lkml@gmail.com,
	morbo@google.com, justinstitt@google.com, qq570070308@gmail.com,
	masahiroy@kernel.org, jack@suse.cz, akpm@linux-foundation.org,
	maninder1.s@samsung.com
Cc: linux-kernel@vger.kernel.org, llvm@lists.linux.dev, will@kernel.org
Subject: [PATCH] Fix redundant judgment in WARN_ONCE with clang
Date: Sun,  9 Nov 2025 16:37:15 +0800	[thread overview]
Message-ID: <20251109083715.24495-1-qq570070308@gmail.com> (raw)

For c code:
```c
extern int xx;
void test(void)
{
	if (WARN_ONCE(xx, "x"))
		__asm__ volatile ("nop":::);
}
```

Clang will generate the following assembly code:
```assemble
test:
	movl	xx(%rip), %eax // Assume xx == 0 (likely case)
	testl	%eax, %eax // judge once
	je	.LBB0_3    // jump to .LBB0_3
	testb	$1, test.__already_done(%rip)
	je	.LBB0_2
.LBB0_3:
	testl	%eax, %eax // judge again
	je	.LBB0_5    // jump to .LBB0_5
.LBB0_4:
	nop
.LBB0_5:
	retq
	// omit
```

In the above code, `xx == 0` should be a likely case, but in this case,
xx has been judged twice.

Test info:
1. kernel source:
linux-next
commit 9c0826a5d9aa4d52206d ("Add linux-next specific files for 20251107")
2. compiler:
clang: Debian clang version 21.1.4 (8) with
Debian LLD 21.1.4 (compatible with GNU linkers)
3. config:
base on default x86_64_defconfig, and setting:
CONFIG_MITIGATION_RETHUNK=n
CONFIG_STACKPROTECTOR=n

Add unlikely to __ret_cond to help the compiler optimize correctly.

Signed-off-by: Xie Yuanbin <qq570070308@gmail.com>
---
 include/linux/once_lite.h | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/include/linux/once_lite.h b/include/linux/once_lite.h
index 27de7bc32a06..194c5ebbfbc4 100644
--- a/include/linux/once_lite.h
+++ b/include/linux/once_lite.h
@@ -10,17 +10,17 @@
 #define DO_ONCE_LITE(func, ...)						\
 	DO_ONCE_LITE_IF(true, func, ##__VA_ARGS__)
 
-#define __ONCE_LITE_IF(condition)					\
-	({								\
-		static bool __section(".data..once") __already_done;	\
-		bool __ret_cond = !!(condition);			\
-		bool __ret_once = false;				\
-									\
-		if (unlikely(__ret_cond && !__already_done)) {		\
-			__already_done = true;				\
-			__ret_once = true;				\
-		}							\
-		unlikely(__ret_once);					\
+#define __ONCE_LITE_IF(condition)						\
+	({									\
+		static bool __section(".data..once") __already_done;		\
+		bool __ret_cond = !!(condition);				\
+		bool __ret_once = false;					\
+										\
+		if (unlikely(__ret_cond) && unlikely(!__already_done)) {	\
+			__already_done = true;					\
+			__ret_once = true;					\
+		}								\
+		unlikely(__ret_once);						\
 	})
 
 #define DO_ONCE_LITE_IF(condition, func, ...)				\
-- 
2.51.0


             reply	other threads:[~2025-11-09  8:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-09  8:37 Xie Yuanbin [this message]
2025-11-09 17:03 ` [PATCH] Fix redundant judgment in WARN_ONCE with clang Andrew Morton
2025-11-10 14:40   ` Xie Yuanbin

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=20251109083715.24495-1-qq570070308@gmail.com \
    --to=qq570070308@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=jack@suse.cz \
    --cc=justinstitt@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=maninder1.s@samsung.com \
    --cc=masahiroy@kernel.org \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=nick.desaulniers+lkml@gmail.com \
    --cc=will@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