[RFC 2/2] kref: introduce kref_put_lockptr() and use lockptr

From: Alexander Aring
Date: Fri Nov 03 2023 - 12:17:29 EST


This patch switches to make kref_put_lock() more locktype independend by
introducing kref_put_lockptr() and using refcount_dec_and_lockptr(). The
user can now pass a lockptr and do the specific locktype operation by
parameters. The current kref_put_mutex() and kref_put_lock() has been
adapted to use the new kref_put_lockptr() implementation for existing
users.

Signed-off-by: Alexander Aring <aahringo@xxxxxxxxxx>
---
include/linux/kref.h | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/include/linux/kref.h b/include/linux/kref.h
index d32e21a2538c..09bc79435dbb 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -68,26 +68,33 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
return 0;
}

-static inline int kref_put_mutex(struct kref *kref,
- void (*release)(struct kref *kref),
- struct mutex *lock)
+static inline int kref_put_lockptr(struct kref *kref,
+ void (*release)(struct kref *kref),
+ void (*lock)(void *lockptr),
+ void (*unlock)(void *lockptr),
+ void *lockptr)
{
- if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) {
+ if (refcount_dec_and_lockptr(&kref->refcount, lock, unlock, lockptr)) {
release(kref);
return 1;
}
return 0;
}

+static inline int kref_put_mutex(struct kref *kref,
+ void (*release)(struct kref *kref),
+ struct mutex *lock)
+{
+ return kref_put_lockptr(kref, release, lockptr_mutex_lock,
+ lockptr_mutex_unlock, lock);
+}
+
static inline int kref_put_lock(struct kref *kref,
void (*release)(struct kref *kref),
spinlock_t *lock)
{
- if (refcount_dec_and_lock(&kref->refcount, lock)) {
- release(kref);
- return 1;
- }
- return 0;
+ return kref_put_lockptr(kref, release, lockptr_spin_lock,
+ lockptr_spin_unlock, lock);
}

/**
--
2.39.3