From: Alexander Aring <aahringo@redhat.com>
To: peterz@infradead.org
Cc: will@kernel.org, gfs2@lists.linux.dev, aahringo@redhat.com,
boqun.feng@gmail.com, mark.rutland@arm.com,
linux-kernel@vger.kernel.org
Subject: [PATCHv2 2/2] kref: introduce __kref_put_lock macro
Date: Mon, 13 Nov 2023 15:48:09 -0500 [thread overview]
Message-ID: <20231113204809.4052009-2-aahringo@redhat.com> (raw)
In-Reply-To: <20231113204809.4052009-1-aahringo@redhat.com>
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@infradead.org>
Signed-off-by: Alexander Aring <aahringo@redhat.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
next prev parent reply other threads:[~2023-11-13 20:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-13 20:48 [PATCHv2 1/2] refcount: introduce __refcount_dec_and_lock macro Alexander Aring
2023-11-13 20:48 ` Alexander Aring [this message]
2024-02-01 14:59 ` Alexander Aring
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231113204809.4052009-2-aahringo@redhat.com \
--to=aahringo@redhat.com \
--cc=boqun.feng@gmail.com \
--cc=gfs2@lists.linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=peterz@infradead.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox