[PATCH 1/3] lockref: add a __cond_lock annotation for lockref_put_or_lock

From: Christoph Hellwig

Date: Fri Nov 14 2025 - 00:53:01 EST


Add a cond_lock annotation for lockref_put_or_lock to make sparse
happy with using it. Note that for this the return value has to be
double-inverted as the return value convention of lockref_put_or_lock
is inverted compared to _trylock conventions expected by __cond_lock,
as lockref_put_or_lock returns true when it did not need to take the
lock.

Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
include/linux/lockref.h | 4 +++-
lib/lockref.c | 4 ++--
2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/linux/lockref.h b/include/linux/lockref.h
index 676721ee878d..7b4bb67db216 100644
--- a/include/linux/lockref.h
+++ b/include/linux/lockref.h
@@ -49,7 +49,9 @@ static inline void lockref_init(struct lockref *lockref)
void lockref_get(struct lockref *lockref);
int lockref_put_return(struct lockref *lockref);
bool lockref_get_not_zero(struct lockref *lockref);
-bool lockref_put_or_lock(struct lockref *lockref);
+bool _lockref_put_or_lock(struct lockref *lockref);
+#define lockref_put_or_lock(_lockref) \
+ (!__cond_lock((_lockref)->lock, !_lockref_put_or_lock(_lockref)))

void lockref_mark_dead(struct lockref *lockref);
bool lockref_get_not_dead(struct lockref *lockref);
diff --git a/lib/lockref.c b/lib/lockref.c
index 5d8e3ef3860e..667f0c30c867 100644
--- a/lib/lockref.c
+++ b/lib/lockref.c
@@ -105,7 +105,7 @@ EXPORT_SYMBOL(lockref_put_return);
* @lockref: pointer to lockref structure
* Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
*/
-bool lockref_put_or_lock(struct lockref *lockref)
+bool _lockref_put_or_lock(struct lockref *lockref)
{
CMPXCHG_LOOP(
new.count--;
@@ -122,7 +122,7 @@ bool lockref_put_or_lock(struct lockref *lockref)
spin_unlock(&lockref->lock);
return true;
}
-EXPORT_SYMBOL(lockref_put_or_lock);
+EXPORT_SYMBOL(_lockref_put_or_lock);

/**
* lockref_mark_dead - mark lockref dead
--
2.47.3