Skip to contentSkip to conversation

refcount: introduce generic lockptr funcs

RFC In review Alexander Aring 1 participant 4 messages Nov 3, 2023

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

RFC 1/2 refcount: introduce generic lockptr funcs

This patch introduce lockptr refcount operations. Currently refcount has a lot of refcount_dec_and_lock() functionality for most common used locktype. Those functions look mostly all the same and is duplicated inside the refcount implementation. Instead of introducing a new whole refcount_dec_and_lock() functionality e.g. for rwlock_t and their _bh variants this patch will introduce lockptr. A lockptr is just a void * and refers to the actual locking instance that can even be an own locking type. Over the passed callbacks for lock and unlock operations the void *lockptr becomes to the real thing by casting it and do the locktype specific lock operation.

---
 include/linux/refcount.h | 15 +++++++
 lib/refcount.c           | 92 ++++++++++++++++++++++++++++------------
 2 files changed, 80 insertions(+), 27 deletions(-)
include/linux/refcount.h +15 -0
index a62fcca97486..7b1fb85212cc 100644
@@ -366,4 +366,19 @@ extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock)
 extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r,
 						       spinlock_t *lock,
 						       unsigned long *flags) __cond_acquires(lock);
+extern bool refcount_dec_and_lockptr(refcount_t *r, void (*lock)(void *lockptr),
+				     void (*unlock)(void *lockptr),  void *lockptr) __cond_acquires(lockptr);
+
+extern void lockptr_mutex_lock(void *lockptr) __acquires(lockptr);
+extern void lockptr_mutex_unlock(void *lockptr) __releases(lockptr);
+extern void lockptr_spin_lock(void *lockptr) __acquires(lockptr);
+extern void lockptr_spin_unlock(void *lockptr) __releases(lockptr);
+
+struct lockptr_irqsave_data {
+	void *lockptr;
+	unsigned long *flags;
+};
+extern void lockptr_irqsave(void *lockptr) __acquires(lockptr);
+extern void lockptr_irqsave(void *lockptr) __releases(lockptr);
+
 #endif /* _LINUX_REFCOUNT_H */
lib/refcount.c +65 -27

122 lines not on this page · raw message

Signature · 1 line
2.39.3
#1 lore raw

RFC 2/2 kref: introduce kref_put_lockptr() and use lockptr

This patch switches to make kref_put_lock() more locktype independend by introducing kref_put_lockptr() and using refcount_dec_and_lockptr(). The user can now pass a lockptr and do the specific locktype operation by parameters. The current kref_put_mutex() and kref_put_lock() has been adapted to use the new kref_put_lockptr() implementation for existing users.

---
 include/linux/kref.h | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
include/linux/kref.h +16 -9
index d32e21a2538c..09bc79435dbb 100644
@@ -68,26 +68,33 @@ static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref)
 	return 0;
 }
 
-static inline int kref_put_mutex(struct kref *kref,
-				 void (*release)(struct kref *kref),
-				 struct mutex *lock)
+static inline int kref_put_lockptr(struct kref *kref,
+				   void (*release)(struct kref *kref),
+				   void (*lock)(void *lockptr),
+				   void (*unlock)(void *lockptr),
+				   void *lockptr)
 {
-	if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) {
+	if (refcount_dec_and_lockptr(&kref->refcount, lock, unlock, lockptr)) {
 		release(kref);
 		return 1;
 	}
 	return 0;
 }
 
+static inline int kref_put_mutex(struct kref *kref,
+				 void (*release)(struct kref *kref),
+				 struct mutex *lock)
+{
+	return kref_put_lockptr(kref, release, lockptr_mutex_lock,
+				lockptr_mutex_unlock, 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_lockptr(kref, release, lockptr_spin_lock,
+				lockptr_spin_unlock, lock);
 }
 
 /**
Signature · 1 line
2.39.3
#2 lore raw
In reply to Alexander Aring

Hi,

Quoted 11 lines · Alexander Aring

This patch introduce lockptr refcount operations. Currently refcount has a lot of refcount_dec_and_lock() functionality for most common used locktype. Those functions look mostly all the same and is duplicated inside the refcount implementation. Instead of introducing a new whole refcount_dec_and_lock() functionality e.g. for rwlock_t and their _bh variants this patch will introduce lockptr. A lockptr is just a void * and refers to the actual locking instance that can even be an own locking type. Over the passed callbacks for lock and unlock operations the void *lockptr becomes to the real thing by casting it and do the locktype specific lock operation.

just an RFC to check if there is any interest to introduce something like this. I think the idea is clear. My current use case is to have rwlock_t and its bh lock operations using something like refcount_dec_and_write_lock_bh() and later kref_put_write_lock_bh(). I try to avoid copying some copy code again. I am open to any better design change. Or telling me to just duplicate code again for what I need it for. However this has the advantage that somebody can use their "own" locktype implementation out of kernel core code.

Thanks.

- Alex

#3 lore raw

Hi,

Quoted 16 lines · Alexander Aring

Hi,

Quoted 11 lines · Alexander Aring

This patch introduce lockptr refcount operations. Currently refcount has a lot of refcount_dec_and_lock() functionality for most common used locktype. Those functions look mostly all the same and is duplicated inside the refcount implementation. Instead of introducing a new whole refcount_dec_and_lock() functionality e.g. for rwlock_t and their _bh variants this patch will introduce lockptr. A lockptr is just a void * and refers to the actual locking instance that can even be an own locking type. Over the passed callbacks for lock and unlock operations the void *lockptr becomes to the real thing by casting it and do the locktype specific lock operation.

Quoted 8 lines

just an RFC to check if there is any interest to introduce something like this. I think the idea is clear. My current use case is to have rwlock_t and its bh lock operations using something like refcount_dec_and_write_lock_bh() and later kref_put_write_lock_bh(). I try to avoid copying some copy code again. I am open to any better design change. Or telling me to just duplicate code again for what I need it for. However this has the advantage that somebody can use their "own" locktype implementation out of kernel core code.

I will resubmit this RFC series because I forgot to cc the linux kernel mailing list that indeed makes sense here.

- Alex