[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <689ce0227a044_50ce100d7@dwillia2-xfh.jf.intel.com.notmuch>
Date: Wed, 13 Aug 2025 11:57:38 -0700
From: <dan.j.williams@...el.com>
To: Peter Zijlstra <peterz@...radead.org>, Dan Williams
<dan.j.williams@...el.com>
CC: <dave.jiang@...el.com>, <linux-kernel@...r.kernel.org>,
<linux-cxl@...r.kernel.org>, Nathan Chancellor <nathan@...nel.org>, "Linus
Torvalds" <torvalds@...ux-foundation.org>, David Lechner
<dlechner@...libre.com>, Jonathan Cameron <jonathan.cameron@...wei.com>,
"Andy Shevchenko" <andriy.shevchenko@...el.com>
Subject: Re: [PATCH] cleanup: Fix unused guard error function with
DEFINE_CLASS_IS_COND_GUARD
Peter Zijlstra wrote:
> On Mon, Aug 04, 2025 at 03:09:54PM -0700, Dan Williams wrote:
> > Andy reports that the "lock_timer" scheme in kernel/time/posix-timers.c,
> > with its custom usage of DEFINE_CLASS_IS_COND_GUARD(), results in:
> >
> > kernel/time/posix-timers.c:89:1: error: unused function 'class_lock_timer_lock_err' [-Werror,-Wunused-function]
> > 89 | DEFINE_CLASS_IS_COND_GUARD(lock_timer);
> >
> > ...with a clang W=1 build. Per Nathan, clang catches unused "static inline"
> > functions in C files since commit 6863f5643dd7 ("kbuild: allow Clang to
> > find unused static inline functions for W=1 build").
>
> I so loathe that warning :/
The only case I can think of where it might have some value, if you
squint, is in catching cases where someone defines a conditional guard,
but then somehow fails to use either scoped_cond_guard() or ACQUIRE().
I.e. guard(lock_timer)(timer_id), does not generate a compile time
warning.
However, if someone accidentally mixes guard() and ACQUIRE() in the same
C file, then the warning will not fire.
>
> > There are 2 ways to solve this, either mark the class_##_lock_err()
> > function as __maybe_unused, or make sure class_##_lock_err() *is* used /
> > gets called to disposition the lock status.
> >
> > At present __lock_timer() only indicates failure with a NULL __guard_ptr().
> > However, one could imagine that __lock_timer(), or some other custom
> > conditional locking primitive, wants to pass an ERR_PTR() to indicate the
> > reason for the lock acquisition failure.
> >
> > Update __scoped_cond_guard() to check for ERR_PTR() in addition to NULL
> > @scope values. This allows __lock_timer(), or another open coded
> > DEFINE_CLASS_IS_COND_GUARD() user, to switch to passing an ERR_PTR() in the
> > future. In the meantime, this just silences the warning.
> >
> > Cc: Nathan Chancellor <nathan@...nel.org>
> > Cc: "Peter Zijlstra (Intel)" <peterz@...radead.org>
> > Cc: Linus Torvalds <torvalds@...ux-foundation.org>
> > Cc: Dave Jiang <dave.jiang@...el.com>
> > Cc: David Lechner <dlechner@...libre.com>
> > Cc: Jonathan Cameron <jonathan.cameron@...wei.com>
> > Reported-by: Andy Shevchenko <andriy.shevchenko@...el.com>
> > Closes: http://lore.kernel.org/aIo18KZpmKuR4hVZ@black.igk.intel.com
> > Fixes: 857d18f23ab1 ("cleanup: Introduce ACQUIRE() and ACQUIRE_ERR() for conditional locks")
> > Signed-off-by: Dan Williams <dan.j.williams@...el.com>
> > ---
> > Dave, I am sending this to you to take upstream since the warning came
> > in through the CXL tree. If anyone else wants to take it just holler.
> >
> > include/linux/cleanup.h | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
> > index 4eb83dd71cfe..d8e7d1e5561b 100644
> > --- a/include/linux/cleanup.h
> > +++ b/include/linux/cleanup.h
> > @@ -423,7 +423,8 @@ _label: \
> >
> > #define __scoped_cond_guard(_name, _fail, _label, args...) \
> > for (CLASS(_name, scope)(args); true; ({ goto _label; })) \
> > - if (!__guard_ptr(_name)(&scope)) { \
> > + if (!__guard_ptr(_name)(&scope) || \
> > + __guard_err(_name)(&scope)) { \
> > BUILD_BUG_ON(!__is_cond_ptr(_name)); \
> > _fail; \
> > _label: \
>
> What does this do for code generation ?
size(1) identical:
text data bss dec hex filename
54648 20439 64 75151 1258f kernel/time/posix-timers.o
Within that same size it flips the test to a negative branch and reorders the code.
Before:
scoped_timer_get_or_fail(timer_id)
1246: 44 89 ff mov %r15d,%edi
1249: e8 d2 5a 00 00 call 6d20 <class_lock_timer_constructor>
124e: 49 89 c7 mov %rax,%r15
1251: 48 3d 01 f0 ff ff cmp $0xfffffffffffff001,%rax
1257: 0f 92 c0 setb %al
125a: 4d 85 ff test %r15,%r15
125d: 0f 95 c1 setne %cl
1260: 84 c8 test %cl,%al
1262: 0f 84 85 00 00 00 je 12ed <__ia32_sys_timer_gettime+0x15d>
...
12ed: 48 c7 c3 ea ff ff ff mov $0xffffffffffffffea,%rbx
if (likely((timr)))
12f4: 4d 85 ff test %r15,%r15
12f7: 74 0c je 1305 <__ia32_sys_timer_gettime+0x175>
After:
scoped_timer_get_or_fail(timer_id)
1246: 44 89 ff mov %r15d,%edi
1249: e8 d2 5a 00 00 call 6d20 <class_lock_timer_constructor>
124e: 49 89 c7 mov %rax,%r15
1251: 48 3d 01 f0 ff ff cmp $0xfffffffffffff001,%rax
1257: 0f 92 c0 setb %al
125a: 4d 85 ff test %r15,%r15
125d: 0f 95 c1 setne %cl
1260: 84 c8 test %cl,%al
1262: 75 21 jne 1285 <__ia32_sys_timer_gettime+0xf5>
1264: 48 c7 c3 ea ff ff ff mov $0xffffffffffffffea,%rbx
126b: 4d 85 ff test %r15,%r15
if (likely((timr)))
126e: 0f 84 94 00 00 00 je 1308 <__ia32_sys_timer_gettime+0x178>
Powered by blists - more mailing lists