Re: [PATCH v4 16/35] kref: Add context-analysis annotations

Peter Zijlstra <[email protected]> Thu, 11 Dec 2025 13:26:36 +0100
Newsgroups org.kernel.vger.linux-sparse,dev.linux.lists.llvm,org.kernel.vger.linux-crypto,org.kernel.vger.linux-doc,org.kernel.vger.linux-kbuild,org.kernel.vger.linux-kernel,org.kernel.vger.linux-security-module,org.kernel.vger.linux-wireless,org.kernel.vger.rcu,org.kvack.linux-mm
Message-ID <[email protected]>
On Thu, Nov 20, 2025 at 04:09:41PM +0100, Marco Elver wrote:
> Mark functions that conditionally acquire the passed lock.
> 
> Signed-off-by: Marco Elver <[email protected]>
> ---
>  include/linux/kref.h | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/linux/kref.h b/include/linux/kref.h
> index 88e82ab1367c..9bc6abe57572 100644
> --- a/include/linux/kref.h
> +++ b/include/linux/kref.h
> @@ -81,6 +81,7 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
>  static inline int kref_put_mutex(struct kref *kref,
>  				 void (*release)(struct kref *kref),
>  				 struct mutex *mutex)
> +	__cond_acquires(true, mutex)
>  {
>  	if (refcount_dec_and_mutex_lock(&kref->refcount, mutex)) {
>  		release(kref);
> @@ -102,6 +103,7 @@ static inline int kref_put_mutex(struct kref *kref,
>  static inline int kref_put_lock(struct kref *kref,
>  				void (*release)(struct kref *kref),
>  				spinlock_t *lock)
> +	__cond_acquires(true, lock)
>  {
>  	if (refcount_dec_and_lock(&kref->refcount, lock)) {
>  		release(kref);
> -- 
> 2.52.0.rc1.455.g30608eb744-goog
> 

Note that both use the underlying refcount_dec_and_*lock() functions.
Its a bit sad that annotation those isn't sufficient. These are inline
functions after all, the compiler should be able to see through all that.