Skip to contentSkip to conversation

refcount: introduce __refcount_dec_and_lock macro

PATCH In review Alexander Aring 1 participant 3 messages Nov 13, 2023 – Feb 1, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 refcount: introduce __refcount_dec_and_lock macro

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 (Intel) <[email protected]>

---
changes since v2:
- drop kdoc move to header, it need to be at the implementation
- add Acked-by from Peter Zijlstra
- fix Peter Zijlstra in Co-developed

 include/linux/refcount.h | 26 ++++++++++++++++++++++++++
 lib/refcount.c           | 36 ++++++------------------------------
 2 files changed, 32 insertions(+), 30 deletions(-)
include/linux/refcount.h +26 -0
index a62fcca97486..0db03df681fe 100644
@@ -361,6 +361,32 @@ 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;						\
+})
+
 extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock) __cond_acquires(lock);
 extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock) __cond_acquires(lock);
 extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r,
lib/refcount.c +6 -30
index a207a8f22b3c..2afe279cfb5a 100644
@@ -112,16 +112,8 @@ 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);
 
@@ -143,16 +135,8 @@ 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);
 
@@ -172,15 +156,7 @@ 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);
Signature · 1 line
2.39.3
#1 lore raw

PATCH v2 2/2 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 <[email protected]>

---
 include/linux/kref.h | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)
include/linux/kref.h +27 -10
index d32e21a2538c..d8e26ac1d54f 100644
@@ -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));
 }
 
 /**
Signature · 1 line
2.39.3
#2 lore raw
In reply to Alexander Aring

Hi,

Quoted 10 lines · Alexander Aring

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.

ping? :)

Any chance to bring this upstream?

- Alex