[PATCH RFC 08/33] kref: Add thread-safety annotations in <linux/kref.h>

From: Bart Van Assche
Date: Thu Feb 06 2025 - 12:52:44 EST


Inform the Clang thread-safety analyzer that kref_put_mutex() callers
must not hold *mutex.

Note: annotating that the 'release' function pointer releases 'mutex' is
not possible because the mutex pointer is not an argument of the 'release'
function.

Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Bart Van Assche <bvanassche@xxxxxxx>
---
include/linux/kref.h | 3 +++
1 file changed, 3 insertions(+)

diff --git a/include/linux/kref.h b/include/linux/kref.h
index 88e82ab1367c..29c31b103557 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -15,6 +15,7 @@

#include <linux/spinlock.h>
#include <linux/refcount.h>
+#include <linux/thread_safety.h>

struct kref {
refcount_t refcount;
@@ -81,6 +82,8 @@ 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)
+ EXCLUDES(*mutex)
+ NO_THREAD_SAFETY_ANALYSIS
{
if (refcount_dec_and_mutex_lock(&kref->refcount, mutex)) {
release(kref);