gfs2 filesystem and dlm development
 help / color / mirror / Atom feed
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: [PATCH 2/3] refcount: introduce __refcount_dec_and_lock macro
Date: Mon,  6 Nov 2023 14:11:37 -0500	[thread overview]
Message-ID: <20231106191138.3179599-2-aahringo@redhat.com> (raw)
In-Reply-To: <20231106191138.3179599-1-aahringo@redhat.com>

This patch adds the __refcount_dec_and_lock macro to generate code for a
lock specific refcount_dec_and_lock implementation. Existing
refcount_dec_and_lock implementation are updated to use the new
__refcount_dec_and_lock macro. In future other lock implementation can
added to use the refcount_dec_and_lock trick to only hold the lock when
the refcount is going to be zero. Per subsystem own lock implementation
can use the macro as well to provide such implementation for their own
locking type.

Co-developed: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Alexander Aring <aahringo@redhat.com>
---
 include/linux/refcount.h | 25 +++++++++++++++++++++++++
 lib/refcount.c           | 36 ++++++------------------------------
 2 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/include/linux/refcount.h b/include/linux/refcount.h
index 741cc6295f54..d5cf40d3f4bc 100644
--- a/include/linux/refcount.h
+++ b/include/linux/refcount.h
@@ -362,6 +362,31 @@ static inline void refcount_dec(refcount_t *r)
 extern __must_check bool refcount_dec_if_one(refcount_t *r);
 extern __must_check bool refcount_dec_not_one(refcount_t *r);
 
+/**
+ * __refcount_dec_and_lock - macro to create code to holding a lock if being
+ *                           able to decremnt refcount to 0
+ * @_ref: the refcount
+ * @_lock: lock function call code
+ * @_unlock: unlock 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 __refcount_dec_and_lock(_ref, _lock, _unlock)	\
+({							\
+	bool _ret = false;				\
+							\
+	if (!refcount_dec_not_one(_ref)) {		\
+		_lock;					\
+		if (!refcount_dec_and_test(_ref))	\
+			_unlock;			\
+		else					\
+			_ret = true;			\
+	}						\
+							\
+	_ret;						\
+})
+
 /**
  * refcount_dec_and_mutex_lock - return holding mutex if able to decrement
  *                               refcount to 0
diff --git a/lib/refcount.c b/lib/refcount.c
index c37edf66994f..5511498df708 100644
--- a/lib/refcount.c
+++ b/lib/refcount.c
@@ -96,46 +96,22 @@ EXPORT_SYMBOL(refcount_dec_not_one);
 
 bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock)
 {
-	if (refcount_dec_not_one(r))
-		return false;
-
-	mutex_lock(lock);
-	if (!refcount_dec_and_test(r)) {
-		mutex_unlock(lock);
-		return false;
-	}
-
-	return true;
+	return __refcount_dec_and_lock(r, mutex_lock(lock),
+				       mutex_unlock(lock));
 }
 EXPORT_SYMBOL(refcount_dec_and_mutex_lock);
 
 bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
 {
-	if (refcount_dec_not_one(r))
-		return false;
-
-	spin_lock(lock);
-	if (!refcount_dec_and_test(r)) {
-		spin_unlock(lock);
-		return false;
-	}
-
-	return true;
+	return __refcount_dec_and_lock(r, spin_lock(lock),
+				       spin_unlock(lock));
 }
 EXPORT_SYMBOL(refcount_dec_and_lock);
 
 bool refcount_dec_and_lock_irqsave(refcount_t *r, spinlock_t *lock,
 				   unsigned long *flags)
 {
-	if (refcount_dec_not_one(r))
-		return false;
-
-	spin_lock_irqsave(lock, *flags);
-	if (!refcount_dec_and_test(r)) {
-		spin_unlock_irqrestore(lock, *flags);
-		return false;
-	}
-
-	return true;
+	return __refcount_dec_and_lock(r, spin_lock_irqsave(lock, *flags),
+				       spin_unlock_irqrestore(lock, *flags));
 }
 EXPORT_SYMBOL(refcount_dec_and_lock_irqsave);
-- 
2.39.3


  reply	other threads:[~2023-11-06 19:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-06 19:11 [PATCH 1/3] refcount: move kdoc to header definition Alexander Aring
2023-11-06 19:11 ` Alexander Aring [this message]
2023-11-09  9:31   ` [PATCH 2/3] refcount: introduce __refcount_dec_and_lock macro Peter Zijlstra
2023-11-06 19:11 ` [PATCH 3/3] kref: introduce __kref_put_lock macro Alexander Aring
2023-11-09  7:01 ` [PATCH 1/3] refcount: move kdoc to header definition Christoph Hellwig
2023-11-09 14:15   ` 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=20231106191138.3179599-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