Fix redundant judgment in WARN_ONCE with clang
PATCH 1/1 Fix redundant judgment in WARN_ONCE with clang
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=nCONFIG_STACKPROTECTOR=n
Add unlikely to __ret_cond to help the compiler optimize correctly.
--- include/linux/once_lite.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
include/linux/once_lite.h +11 -11
@@ -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, ...) \
Signature · 1 line
2.51.0
Quoted 47 lines · On Sun, 9 Nov 2025 16:37:15 +0800 Xie Yuanbin <[email protected]> wrote:
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 == 0should be a likely case, but in this case, xx has been judged twice....
include/linux/once_lite.h +0
-8@@ -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; \
include/linux/once_lite.h line 19
Quoted 10 lines
include/linux/once_lite.h +7
-3- __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)) { \
Sure, !__already_done is unlikely here.
include/linux/once_lite.h line 23
Quoted 4 lines
include/linux/once_lite.h +4
-0+ __already_done = true; \ + __ret_once = true; \ + } \ +unlikely (__ret_once); \
It's a shame you messed with the whitespace. And I don't think it was necessary anyway. Here's what it would have looked like:
include/linux/once_lite.h renamed from include/linux/once_lite.h~fix-redundant-judgment-in-warn_once-with-clang +1 -1
@@ -16,7 +16,7 @@bool __ret_cond = !!(condition); \bool __ret_once = false; \ \ -if (unlikely (__ret_cond && !__already_done)) { \ +if (unlikely (__ret_cond) &&unlikely (!__already_done)) {\ __already_done = true; \ __ret_once = true; \ } \
_
Quoted 6 lines · Andrew Morton
It's a shame you messed with the whitespace. And I don't think it was necessary anyway. Here's what it would have looked like:
include/linux/once_lite.h line 22
Quoted 8 lines
include/linux/once_lite.h renamed from include/linux/once_lite.h~fix-redundant-judgment-in-warn_once-with-clang +1
-1@@ -16,7 +16,7 @@bool __ret_cond = !!(condition); \bool __ret_once = false; \ \ -if (unlikely (__ret_cond && !__already_done)) { \ +if (unlikely (__ret_cond) &&unlikely (!__already_done)) {\ __already_done = true; \ __ret_once = true; \ } \
Okay, thanks, I will send the v2 patch.
Xie Yuanbin