[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20231106191138.3179599-3-aahringo@redhat.com>
Date: Mon, 6 Nov 2023 14:11:38 -0500
From: Alexander Aring <aahringo@...hat.com>
To: peterz@...radead.org
Cc: will@...nel.org, gfs2@...ts.linux.dev, aahringo@...hat.com,
boqun.feng@...il.com, mark.rutland@....com,
linux-kernel@...r.kernel.org
Subject: [PATCH 3/3] kref: introduce __kref_put_lock macro
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@...radead.org>
Signed-off-by: Alexander Aring <aahringo@...hat.com>
---
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
Powered by blists - more mailing lists