[PATCH 3/3] kref: introduce __kref_put_lock macro

From: Alexander Aring
Date: Mon Nov 06 2023 - 14:12:50 EST


This patch introduce the __kref_put_lock macro to easily write a
kref_put_lock functionality based on refcount_dec_and_lock functions.
Existing per lock type specific kref_put_lock functionality are updated
to use the new __kref_put_lock macro.

Co-developed: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Signed-off-by: Alexander Aring <aahringo@xxxxxxxxxx>
---
include/linux/kref.h | 37 +++++++++++++++++++++++++++----------
1 file changed, 27 insertions(+), 10 deletions(-)

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

+/**
+ * __kref_dec_and_lock - macro to create code to holding a lock and call the
+ * release callback if being able to decremnt refcount
+ * to 0
+ * @kref: the kref
+ * @_release: callback for the release function
+ * @_ref_dec_and_lock: ref_and_lock lock specific function call code
+ *
+ * The result will be directly returned as a right operand operation. Uusally
+ * the caller use it directly after a return statement.
+ */
+#define __kref_put_lock(_kref, _release, _ref_dec_and_lock) \
+({ \
+ int _ret = 0; \
+ \
+ if (_ref_dec_and_lock) { \
+ _release(kref); \
+ _ret = 1; \
+ } \
+ \
+ _ret; \
+})
+
static inline int kref_put_mutex(struct kref *kref,
void (*release)(struct kref *kref),
struct mutex *lock)
{
- if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) {
- release(kref);
- return 1;
- }
- return 0;
+ return __kref_put_lock(kref, release,
+ refcount_dec_and_mutex_lock(&kref->refcount, 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_lock(kref, release,
+ refcount_dec_and_lock(&kref->refcount, lock));
}

/**
--
2.39.3