diff options
author | Peter Zijlstra <peterz@infradead.org> | 2016-11-14 12:03:11 -0500 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2017-01-18 04:03:29 -0500 |
commit | 0a13cd1a05e7a549259d4f803d2ec2efda07ed7c (patch) | |
tree | 8d8799d8a46d827a689e9939d472654b307e16c8 | |
parent | 23b19ec3377924eb8f303880102e056e9214ba72 (diff) |
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>
-rw-r--r-- | include/linux/kref.h | 22 | ||||
-rw-r--r-- | net/sunrpc/svcauth.c | 15 |
2 files changed, 26 insertions, 11 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 @@ | |||
19 | #include <linux/atomic.h> | 19 | #include <linux/atomic.h> |
20 | #include <linux/kernel.h> | 20 | #include <linux/kernel.h> |
21 | #include <linux/mutex.h> | 21 | #include <linux/mutex.h> |
22 | #include <linux/spinlock.h> | ||
22 | 23 | ||
23 | struct kref { | 24 | struct kref { |
24 | atomic_t refcount; | 25 | atomic_t refcount; |
@@ -86,12 +87,21 @@ static inline int kref_put_mutex(struct kref *kref, | |||
86 | struct mutex *lock) | 87 | struct mutex *lock) |
87 | { | 88 | { |
88 | WARN_ON(release == NULL); | 89 | WARN_ON(release == NULL); |
89 | if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) { | 90 | |
90 | mutex_lock(lock); | 91 | if (atomic_dec_and_mutex_lock(&kref->refcount, lock)) { |
91 | if (unlikely(!atomic_dec_and_test(&kref->refcount))) { | 92 | release(kref); |
92 | mutex_unlock(lock); | 93 | return 1; |
93 | return 0; | 94 | } |
94 | } | 95 | return 0; |
96 | } | ||
97 | |||
98 | static inline int kref_put_lock(struct kref *kref, | ||
99 | void (*release)(struct kref *kref), | ||
100 | spinlock_t *lock) | ||
101 | { | ||
102 | WARN_ON(release == NULL); | ||
103 | |||
104 | if (atomic_dec_and_lock(&kref->refcount, lock)) { | ||
95 | release(kref); | 105 | release(kref); |
96 | return 1; | 106 | return 1; |
97 | } | 107 | } |
diff --git a/net/sunrpc/svcauth.c b/net/sunrpc/svcauth.c index e112da8005b5..bb8db3cb8032 100644 --- a/net/sunrpc/svcauth.c +++ b/net/sunrpc/svcauth.c | |||
@@ -126,13 +126,18 @@ EXPORT_SYMBOL_GPL(svc_auth_unregister); | |||
126 | static struct hlist_head auth_domain_table[DN_HASHMAX]; | 126 | static struct hlist_head auth_domain_table[DN_HASHMAX]; |
127 | static DEFINE_SPINLOCK(auth_domain_lock); | 127 | static DEFINE_SPINLOCK(auth_domain_lock); |
128 | 128 | ||
129 | static void auth_domain_release(struct kref *kref) | ||
130 | { | ||
131 | struct auth_domain *dom = container_of(kref, struct auth_domain, ref); | ||
132 | |||
133 | hlist_del(&dom->hash); | ||
134 | dom->flavour->domain_release(dom); | ||
135 | spin_unlock(&auth_domain_lock); | ||
136 | } | ||
137 | |||
129 | void auth_domain_put(struct auth_domain *dom) | 138 | void auth_domain_put(struct auth_domain *dom) |
130 | { | 139 | { |
131 | if (atomic_dec_and_lock(&dom->ref.refcount, &auth_domain_lock)) { | 140 | kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock); |
132 | hlist_del(&dom->hash); | ||
133 | dom->flavour->domain_release(dom); | ||
134 | spin_unlock(&auth_domain_lock); | ||
135 | } | ||
136 | } | 141 | } |
137 | EXPORT_SYMBOL_GPL(auth_domain_put); | 142 | EXPORT_SYMBOL_GPL(auth_domain_put); |
138 | 143 | ||