summaryrefslogtreecommitdiffstats
path: root/include/linux/kref.h
diff options
context:
space:
mode:
authorPeter Zijlstra <peterz@infradead.org>2016-11-14 18:03:11 +0100
committerIngo Molnar <mingo@kernel.org>2017-01-18 10:03:29 +0100
commit0a13cd1a05e7a549259d4f803d2ec2efda07ed7c (patch)
tree8d8799d8a46d827a689e9939d472654b307e16c8 /include/linux/kref.h
parent23b19ec3377924eb8f303880102e056e9214ba72 (diff)
downloadlinux-0a13cd1a05e7a549259d4f803d2ec2efda07ed7c.tar.gz
linux-0a13cd1a05e7a549259d4f803d2ec2efda07ed7c.tar.bz2
linux-0a13cd1a05e7a549259d4f803d2ec2efda07ed7c.zip
locking/atomic, kref: Implement kref_put_lock()
Because home-rolling your own is _awesome_, stop doing it. Provide kref_put_lock(), just like kref_put_mutex() but for a spinlock. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'include/linux/kref.h')
-rw-r--r--include/linux/kref.h22
1 files changed, 16 insertions, 6 deletions
diff --git a/include/linux/kref.h b/include/linux/kref.h
index 31c49a64cdf9..9db9685fed84 100644
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -19,6 +19,7 @@
#include <linux/atomic.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
+#include <linux/spinlock.h>
struct kref {
atomic_t refcount;
@@ -86,12 +87,21 @@ static inline int kref_put_mutex(struct kref *kref,
struct mutex *lock)
{
WARN_ON(release == NULL);
- if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
- mutex_lock(lock);
- if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
- mutex_unlock(lock);
- return 0;
- }
+
+ if (atomic_dec_and_mutex_lock(&kref->refcount, lock)) {
+ release(kref);
+ return 1;
+ }
+ return 0;
+}
+
+static inline int kref_put_lock(struct kref *kref,
+ void (*release)(struct kref *kref),
+ spinlock_t *lock)
+{
+ WARN_ON(release == NULL);
+
+ if (atomic_dec_and_lock(&kref->refcount, lock)) {
release(kref);
return 1;
}