Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 2 | #ifndef _LINUX_REFCOUNT_H |
| 3 | #define _LINUX_REFCOUNT_H |
| 4 | |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 6 | #include <linux/mutex.h> |
| 7 | #include <linux/spinlock.h> |
Elena Reshetova | 318b1dedc | 2017-02-23 15:09:34 +0200 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 9 | |
David Windsor | bd17416 | 2017-03-10 10:34:12 -0500 | [diff] [blame] | 10 | /** |
Elena Reshetova | b6e859f | 2017-12-05 12:46:35 +0200 | [diff] [blame] | 11 | * struct refcount_t - variant of atomic_t specialized for reference counts |
David Windsor | bd17416 | 2017-03-10 10:34:12 -0500 | [diff] [blame] | 12 | * @refs: atomic_t counter field |
| 13 | * |
| 14 | * The counter saturates at UINT_MAX and will not move once |
| 15 | * there. This avoids wrapping the counter and causing 'spurious' |
| 16 | * use-after-free bugs. |
| 17 | */ |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 18 | typedef struct refcount_struct { |
| 19 | atomic_t refs; |
| 20 | } refcount_t; |
| 21 | |
| 22 | #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), } |
| 23 | |
David Windsor | bd17416 | 2017-03-10 10:34:12 -0500 | [diff] [blame] | 24 | /** |
| 25 | * refcount_set - set a refcount's value |
| 26 | * @r: the refcount |
| 27 | * @n: value to which the refcount will be set |
| 28 | */ |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 29 | static inline void refcount_set(refcount_t *r, unsigned int n) |
| 30 | { |
| 31 | atomic_set(&r->refs, n); |
| 32 | } |
| 33 | |
David Windsor | bd17416 | 2017-03-10 10:34:12 -0500 | [diff] [blame] | 34 | /** |
| 35 | * refcount_read - get a refcount's value |
| 36 | * @r: the refcount |
| 37 | * |
| 38 | * Return: the refcount's value |
| 39 | */ |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 40 | static inline unsigned int refcount_read(const refcount_t *r) |
| 41 | { |
| 42 | return atomic_read(&r->refs); |
| 43 | } |
| 44 | |
Kees Cook | fd25d19f | 2017-06-21 13:00:26 -0700 | [diff] [blame] | 45 | #ifdef CONFIG_REFCOUNT_FULL |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 46 | extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r); |
| 47 | extern void refcount_add(unsigned int i, refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 48 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 49 | extern __must_check bool refcount_inc_not_zero(refcount_t *r); |
| 50 | extern void refcount_inc(refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 51 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 52 | extern __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 53 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 54 | extern __must_check bool refcount_dec_and_test(refcount_t *r); |
| 55 | extern void refcount_dec(refcount_t *r); |
Kees Cook | fd25d19f | 2017-06-21 13:00:26 -0700 | [diff] [blame] | 56 | #else |
Kees Cook | 7a46ec0 | 2017-08-15 09:19:24 -0700 | [diff] [blame] | 57 | # ifdef CONFIG_ARCH_HAS_REFCOUNT |
| 58 | # include <asm/refcount.h> |
| 59 | # else |
Kees Cook | fd25d19f | 2017-06-21 13:00:26 -0700 | [diff] [blame] | 60 | static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r) |
| 61 | { |
| 62 | return atomic_add_unless(&r->refs, i, 0); |
| 63 | } |
| 64 | |
| 65 | static inline void refcount_add(unsigned int i, refcount_t *r) |
| 66 | { |
| 67 | atomic_add(i, &r->refs); |
| 68 | } |
| 69 | |
| 70 | static inline __must_check bool refcount_inc_not_zero(refcount_t *r) |
| 71 | { |
| 72 | return atomic_add_unless(&r->refs, 1, 0); |
| 73 | } |
| 74 | |
| 75 | static inline void refcount_inc(refcount_t *r) |
| 76 | { |
| 77 | atomic_inc(&r->refs); |
| 78 | } |
| 79 | |
| 80 | static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r) |
| 81 | { |
| 82 | return atomic_sub_and_test(i, &r->refs); |
| 83 | } |
| 84 | |
Kees Cook | fd25d19f | 2017-06-21 13:00:26 -0700 | [diff] [blame] | 85 | static inline __must_check bool refcount_dec_and_test(refcount_t *r) |
| 86 | { |
| 87 | return atomic_dec_and_test(&r->refs); |
| 88 | } |
| 89 | |
| 90 | static inline void refcount_dec(refcount_t *r) |
| 91 | { |
| 92 | atomic_dec(&r->refs); |
| 93 | } |
Kees Cook | 7a46ec0 | 2017-08-15 09:19:24 -0700 | [diff] [blame] | 94 | # endif /* !CONFIG_ARCH_HAS_REFCOUNT */ |
Kees Cook | fd25d19f | 2017-06-21 13:00:26 -0700 | [diff] [blame] | 95 | #endif /* CONFIG_REFCOUNT_FULL */ |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 96 | |
Peter Zijlstra | 29dee3c | 2017-02-10 16:27:52 +0100 | [diff] [blame] | 97 | extern __must_check bool refcount_dec_if_one(refcount_t *r); |
| 98 | extern __must_check bool refcount_dec_not_one(refcount_t *r); |
| 99 | extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock); |
| 100 | extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock); |
Peter Zijlstra | f405df5 | 2016-11-14 18:06:19 +0100 | [diff] [blame] | 101 | |
| 102 | #endif /* _LINUX_REFCOUNT_H */ |