blob: bb71f2871dac7a061a182d785ca7206d072b15be [file] [log] [blame]
Peter Zijlstraf405df52016-11-14 18:06:19 +01001#ifndef _LINUX_REFCOUNT_H
2#define _LINUX_REFCOUNT_H
3
Peter Zijlstraf405df52016-11-14 18:06:19 +01004#include <linux/atomic.h>
Peter Zijlstraf405df52016-11-14 18:06:19 +01005#include <linux/mutex.h>
6#include <linux/spinlock.h>
Elena Reshetova318b1dedc2017-02-23 15:09:34 +02007#include <linux/kernel.h>
Peter Zijlstraf405df52016-11-14 18:06:19 +01008
David Windsorbd174162017-03-10 10:34:12 -05009/**
10 * refcount_t - variant of atomic_t specialized for reference counts
11 * @refs: atomic_t counter field
12 *
13 * The counter saturates at UINT_MAX and will not move once
14 * there. This avoids wrapping the counter and causing 'spurious'
15 * use-after-free bugs.
16 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010017typedef struct refcount_struct {
18 atomic_t refs;
19} refcount_t;
20
21#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
22
David Windsorbd174162017-03-10 10:34:12 -050023/**
24 * refcount_set - set a refcount's value
25 * @r: the refcount
26 * @n: value to which the refcount will be set
27 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010028static inline void refcount_set(refcount_t *r, unsigned int n)
29{
30 atomic_set(&r->refs, n);
31}
32
David Windsorbd174162017-03-10 10:34:12 -050033/**
34 * refcount_read - get a refcount's value
35 * @r: the refcount
36 *
37 * Return: the refcount's value
38 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010039static inline unsigned int refcount_read(const refcount_t *r)
40{
41 return atomic_read(&r->refs);
42}
43
Kees Cookfd25d19f2017-06-21 13:00:26 -070044#ifdef CONFIG_REFCOUNT_FULL
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010045extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r);
46extern void refcount_add(unsigned int i, refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010047
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010048extern __must_check bool refcount_inc_not_zero(refcount_t *r);
49extern void refcount_inc(refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010050
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010051extern __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r);
52extern void refcount_sub(unsigned int i, refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010053
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010054extern __must_check bool refcount_dec_and_test(refcount_t *r);
55extern void refcount_dec(refcount_t *r);
Kees Cookfd25d19f2017-06-21 13:00:26 -070056#else
57static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r)
58{
59 return atomic_add_unless(&r->refs, i, 0);
60}
61
62static inline void refcount_add(unsigned int i, refcount_t *r)
63{
64 atomic_add(i, &r->refs);
65}
66
67static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
68{
69 return atomic_add_unless(&r->refs, 1, 0);
70}
71
72static inline void refcount_inc(refcount_t *r)
73{
74 atomic_inc(&r->refs);
75}
76
77static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r)
78{
79 return atomic_sub_and_test(i, &r->refs);
80}
81
82static inline void refcount_sub(unsigned int i, refcount_t *r)
83{
84 atomic_sub(i, &r->refs);
85}
86
87static inline __must_check bool refcount_dec_and_test(refcount_t *r)
88{
89 return atomic_dec_and_test(&r->refs);
90}
91
92static inline void refcount_dec(refcount_t *r)
93{
94 atomic_dec(&r->refs);
95}
96#endif /* CONFIG_REFCOUNT_FULL */
Peter Zijlstraf405df52016-11-14 18:06:19 +010097
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010098extern __must_check bool refcount_dec_if_one(refcount_t *r);
99extern __must_check bool refcount_dec_not_one(refcount_t *r);
100extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
101extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
Peter Zijlstraf405df52016-11-14 18:06:19 +0100102
103#endif /* _LINUX_REFCOUNT_H */