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