Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > linux.kernel > #1573086
| From | Peter Zijlstra <peterz@infradead.org> |
|---|---|
| Newsgroups | linux.kernel |
| Subject | [PATCH 2/5] kref: Implement using refcount_t |
| Date | 2017-02-03 14:40 +0100 |
| Message-ID | <t6MfM-29e-25@gated-at.bofh.it> (permalink) |
| References | <t6MfM-29e-5@gated-at.bofh.it> |
| Organization | linux.* mail to news gateway |
Use the refcount_t 'atomic' type to implement kref, this makes kref
more robust by bringing saturation semantics.
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
include/linux/kref.h | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)
--- a/include/linux/kref.h
+++ b/include/linux/kref.h
@@ -15,17 +15,14 @@
#ifndef _KREF_H_
#define _KREF_H_
-#include <linux/bug.h>
-#include <linux/atomic.h>
-#include <linux/kernel.h>
-#include <linux/mutex.h>
#include <linux/spinlock.h>
+#include <linux/refcount.h>
struct kref {
- atomic_t refcount;
+ refcount_t refcount;
};
-#define KREF_INIT(n) { .refcount = ATOMIC_INIT(n), }
+#define KREF_INIT(n) { .refcount = REFCOUNT_INIT(n), }
/**
* kref_init - initialize object.
@@ -33,12 +30,12 @@ struct kref {
*/
static inline void kref_init(struct kref *kref)
{
- atomic_set(&kref->refcount, 1);
+ refcount_set(&kref->refcount, 1);
}
-static inline int kref_read(const struct kref *kref)
+static inline unsigned int kref_read(const struct kref *kref)
{
- return atomic_read(&kref->refcount);
+ return refcount_read(&kref->refcount);
}
/**
@@ -47,11 +44,7 @@ static inline int kref_read(const struct
*/
static inline void kref_get(struct kref *kref)
{
- /* If refcount was 0 before incrementing then we have a race
- * condition when this kref is freeing by some other thread right now.
- * In this case one should use kref_get_unless_zero()
- */
- WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
+ refcount_inc(&kref->refcount);
}
/**
@@ -75,7 +68,7 @@ static inline int kref_put(struct kref *
{
WARN_ON(release == NULL);
- if (atomic_dec_and_test(&kref->refcount)) {
+ if (refcount_dec_and_test(&kref->refcount)) {
release(kref);
return 1;
}
@@ -88,7 +81,7 @@ static inline int kref_put_mutex(struct
{
WARN_ON(release == NULL);
- if (atomic_dec_and_mutex_lock(&kref->refcount, lock)) {
+ if (refcount_dec_and_mutex_lock(&kref->refcount, lock)) {
release(kref);
return 1;
}
@@ -101,7 +94,7 @@ static inline int kref_put_lock(struct k
{
WARN_ON(release == NULL);
- if (atomic_dec_and_lock(&kref->refcount, lock)) {
+ if (refcount_dec_and_lock(&kref->refcount, lock)) {
release(kref);
return 1;
}
@@ -126,6 +119,6 @@ static inline int kref_put_lock(struct k
*/
static inline int __must_check kref_get_unless_zero(struct kref *kref)
{
- return atomic_add_unless(&kref->refcount, 1, 0);
+ return refcount_inc_not_zero(&kref->refcount);
}
#endif /* _KREF_H_ */
Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[PATCH 0/5] refcount_t and various related bits Peter Zijlstra <peterz@infradead.org> - 2017-02-03 14:40 +0100
[PATCH 4/5] atomic: Introduce atomic_try_cmpxchg() Peter Zijlstra <peterz@infradead.org> - 2017-02-03 14:40 +0100
Re: [PATCH 4/5] atomic: Introduce atomic_try_cmpxchg() Boqun Feng <boqun.feng@gmail.com> - 2017-02-06 05:30 +0100
Re: [PATCH 4/5] atomic: Introduce atomic_try_cmpxchg() Boqun Feng <boqun.feng@gmail.com> - 2017-02-06 07:40 +0100
Re: [PATCH 4/5] atomic: Introduce atomic_try_cmpxchg() Peter Zijlstra <peterz@infradead.org> - 2017-02-06 09:20 +0100
[PATCH 2/5] kref: Implement using refcount_t Peter Zijlstra <peterz@infradead.org> - 2017-02-03 14:40 +0100
Re: [PATCH 2/5] kref: Implement using refcount_t Greg KH <gregkh@linuxfoundation.org> - 2017-02-06 14:10 +0100
[PATCH 5/5] refcount: Use atomic_try_cmpxchg() Peter Zijlstra <peterz@infradead.org> - 2017-02-03 14:40 +0100
[PATCH 1/5] refcount_t: A special purpose refcount type Peter Zijlstra <peterz@infradead.org> - 2017-02-03 14:40 +0100
Re: [PATCH 1/5] refcount_t: A special purpose refcount type Kees Cook <keescook@chromium.org> - 2017-02-03 19:10 +0100
Re: [PATCH 1/5] refcount_t: A special purpose refcount type Kees Cook <keescook@chromium.org> - 2017-02-04 00:40 +0100
[PATCH 3/5] x86: Implement __WARN using UD2 Peter Zijlstra <peterz@infradead.org> - 2017-02-03 14:40 +0100
csiph-web