lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <689a750ad41b2_50ce10064@dwillia2-xfh.jf.intel.com.notmuch>
Date: Mon, 11 Aug 2025 15:56:10 -0700
From: <dan.j.williams@...el.com>
To: Andy Shevchenko <andriy.shevchenko@...el.com>, 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>, "Peter
 Zijlstra (Intel)" <peterz@...radead.org>, Linus Torvalds
	<torvalds@...ux-foundation.org>, David Lechner <dlechner@...libre.com>,
	Jonathan Cameron <jonathan.cameron@...wei.com>
Subject: Re: [PATCH] cleanup: Fix unused guard error function with
 DEFINE_CLASS_IS_COND_GUARD

Andy Shevchenko 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").
> > 
> > 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.
> 
> Hmm... It seems fixes the timer case, but left others still fail:
> 
> drivers/pwm/core.c:54:1: error: unused function 'class_pwmchip_lock_err' [-Werror,-Wunused-function]
>    54 | DEFINE_GUARD(pwmchip, struct pwm_chip *, pwmchip_lock(_T), pwmchip_unlock(_T))
>       | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> include/linux/cleanup.h:380:2: note: expanded from macro 'DEFINE_GUARD'
>   380 |         DEFINE_CLASS_IS_GUARD(_name)
>       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> include/linux/cleanup.h:372:2: note: expanded from macro 'DEFINE_CLASS_IS_GUARD'
>   372 |         __DEFINE_GUARD_LOCK_PTR(_name, _T)
>       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> include/linux/cleanup.h:358:20: note: expanded from macro '__DEFINE_GUARD_LOCK_PTR'
>   358 |         static inline int class_##_name##_lock_err(class_##_name##_t *_T)   \
>       |                           ^~~~~~~~~~~~~~~~~~~~~~~~
> <scratch space>:81:1: note: expanded from here
>    81 | class_pwmchip_lock_err
>       | ^~~~~~~~~~~~~~~~~~~~~~
> 1 error generated.

Ok, so in these cases where the guard is unconditional, the _lock_err()
method can be safely ignored. Here is a quick patch (below), only gcc
compile tested, that tries to quiet the warning for unconditional
guards, but keep the warning for conditional guards.

If you have some time to try it out, great, otherwise I will circle back
with a clang W=1 compile test before submitting the formal patch:

-- 8< --
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index d8e7d1e5561b..4f5b719d0117 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -347,26 +347,30 @@ static __maybe_unused const bool class_##_name##_is_conditional = _is_cond
 			_ptr = NULL;                                        \
 		}                                                           \
 		return _ptr;                                                \
-	}                                                                   \
-	static inline int class_##_name##_lock_err(class_##_name##_t *_T)   \
-	{                                                                   \
-		long _rc = (__force unsigned long)*(_exp);                  \
-		if (!_rc) {                                                 \
-			_rc = -EBUSY;                                       \
-		}                                                           \
-		if (!IS_ERR_VALUE(_rc)) {                                   \
-			_rc = 0;                                            \
-		}                                                           \
-		return _rc;                                                 \
+	}
+
+#define __DEFINE_GUARD_LOCK_ERR(_name, _exp, _attr)                      \
+	static _attr int class_##_name##_lock_err(class_##_name##_t *_T) \
+	{                                                                \
+		long _rc = (__force unsigned long)*(_exp);               \
+		if (!_rc) {                                              \
+			_rc = -EBUSY;                                    \
+		}                                                        \
+		if (!IS_ERR_VALUE(_rc)) {                                \
+			_rc = 0;                                         \
+		}                                                        \
+		return _rc;                                              \
 	}
 
 #define DEFINE_CLASS_IS_GUARD(_name) \
 	__DEFINE_CLASS_IS_CONDITIONAL(_name, false); \
-	__DEFINE_GUARD_LOCK_PTR(_name, _T)
+	__DEFINE_GUARD_LOCK_PTR(_name, _T) \
+	__DEFINE_GUARD_LOCK_ERR(_name, _T, __maybe_unused)
 
 #define DEFINE_CLASS_IS_COND_GUARD(_name) \
 	__DEFINE_CLASS_IS_CONDITIONAL(_name, true); \
-	__DEFINE_GUARD_LOCK_PTR(_name, _T)
+	__DEFINE_GUARD_LOCK_PTR(_name, _T) \
+	__DEFINE_GUARD_LOCK_ERR(_name, _T, inline)
 
 #define DEFINE_GUARD(_name, _type, _lock, _unlock) \
 	DEFINE_CLASS(_name, _type, if (!__GUARD_IS_ERR(_T)) { _unlock; }, ({ _lock; _T; }), _type _T); \
@@ -465,7 +469,8 @@ static inline void class_##_name##_destructor(class_##_name##_t *_T)	\
 	if (!__GUARD_IS_ERR(_T->lock)) { _unlock; }			\
 }									\
 									\
-__DEFINE_GUARD_LOCK_PTR(_name, &_T->lock)
+__DEFINE_GUARD_LOCK_PTR(_name, &_T->lock) \
+__DEFINE_GUARD_LOCK_ERR(_name, &_T->lock, __maybe_unused)
 
 #define __DEFINE_LOCK_GUARD_1(_name, _type, _lock)			\
 static inline class_##_name##_t class_##_name##_constructor(_type *l)	\

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ