[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20251109090317.4261cff25a497bcc8a358a7d@linux-foundation.org>
Date: Sun, 9 Nov 2025 09:03:17 -0800
From: Andrew Morton <akpm@...ux-foundation.org>
To: Xie Yuanbin <qq570070308@...il.com>
Cc: nathan@...nel.org, nick.desaulniers+lkml@...il.com, morbo@...gle.com,
justinstitt@...gle.com, masahiroy@...nel.org, jack@...e.cz,
maninder1.s@...sung.com, linux-kernel@...r.kernel.org,
llvm@...ts.linux.dev, will@...nel.org
Subject: Re: [PATCH] Fix redundant judgment in WARN_ONCE with clang
On Sun, 9 Nov 2025 16:37:15 +0800 Xie Yuanbin <qq570070308@...il.com> 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 == 0` should be a likely case, but in this case,
> xx has been judged twice.
>
> ...
>
> --- 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)) { \
Sure, !__already_done is unlikely here.
> + __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:
--- a/include/linux/once_lite.h~fix-redundant-judgment-in-warn_once-with-clang
+++ a/include/linux/once_lite.h
@@ -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; \
} \
_
Powered by blists - more mailing lists