blob: e28cce21bad6cc1692b9624ccab74b93e448fd3e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Peter Zijlstraf405df52016-11-14 18:06:19 +01002#ifndef _LINUX_REFCOUNT_H
3#define _LINUX_REFCOUNT_H
4
Peter Zijlstraf405df52016-11-14 18:06:19 +01005#include <linux/atomic.h>
Alexey Dobriyan75a040f2018-04-01 01:00:36 +03006#include <linux/compiler.h>
7#include <linux/spinlock_types.h>
8
9struct mutex;
Peter Zijlstraf405df52016-11-14 18:06:19 +010010
David Windsorbd174162017-03-10 10:34:12 -050011/**
Elena Reshetovab6e859f2017-12-05 12:46:35 +020012 * struct refcount_t - variant of atomic_t specialized for reference counts
David Windsorbd174162017-03-10 10:34:12 -050013 * @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 Zijlstraf405df52016-11-14 18:06:19 +010019typedef struct refcount_struct {
20 atomic_t refs;
21} refcount_t;
22
23#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
24
David Windsorbd174162017-03-10 10:34:12 -050025/**
26 * refcount_set - set a refcount's value
27 * @r: the refcount
28 * @n: value to which the refcount will be set
29 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010030static inline void refcount_set(refcount_t *r, unsigned int n)
31{
32 atomic_set(&r->refs, n);
33}
34
David Windsorbd174162017-03-10 10:34:12 -050035/**
36 * refcount_read - get a refcount's value
37 * @r: the refcount
38 *
39 * Return: the refcount's value
40 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010041static inline unsigned int refcount_read(const refcount_t *r)
42{
43 return atomic_read(&r->refs);
44}
45
Mark Rutlandafed7bc2018-07-11 10:36:07 +010046extern __must_check bool refcount_add_not_zero_checked(unsigned int i, refcount_t *r);
47extern void refcount_add_checked(unsigned int i, refcount_t *r);
48
49extern __must_check bool refcount_inc_not_zero_checked(refcount_t *r);
50extern void refcount_inc_checked(refcount_t *r);
51
52extern __must_check bool refcount_sub_and_test_checked(unsigned int i, refcount_t *r);
53
54extern __must_check bool refcount_dec_and_test_checked(refcount_t *r);
55extern void refcount_dec_checked(refcount_t *r);
56
Kees Cookfd25d19f2017-06-21 13:00:26 -070057#ifdef CONFIG_REFCOUNT_FULL
Peter Zijlstraf405df52016-11-14 18:06:19 +010058
Mark Rutlandafed7bc2018-07-11 10:36:07 +010059#define refcount_add_not_zero refcount_add_not_zero_checked
60#define refcount_add refcount_add_checked
Peter Zijlstraf405df52016-11-14 18:06:19 +010061
Mark Rutlandafed7bc2018-07-11 10:36:07 +010062#define refcount_inc_not_zero refcount_inc_not_zero_checked
63#define refcount_inc refcount_inc_checked
Peter Zijlstraf405df52016-11-14 18:06:19 +010064
Mark Rutlandafed7bc2018-07-11 10:36:07 +010065#define refcount_sub_and_test refcount_sub_and_test_checked
66
67#define refcount_dec_and_test refcount_dec_and_test_checked
68#define refcount_dec refcount_dec_checked
69
Kees Cookfd25d19f2017-06-21 13:00:26 -070070#else
Kees Cook7a46ec02017-08-15 09:19:24 -070071# ifdef CONFIG_ARCH_HAS_REFCOUNT
72# include <asm/refcount.h>
73# else
Kees Cookfd25d19f2017-06-21 13:00:26 -070074static inline __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r)
75{
76 return atomic_add_unless(&r->refs, i, 0);
77}
78
79static inline void refcount_add(unsigned int i, refcount_t *r)
80{
81 atomic_add(i, &r->refs);
82}
83
84static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
85{
86 return atomic_add_unless(&r->refs, 1, 0);
87}
88
89static inline void refcount_inc(refcount_t *r)
90{
91 atomic_inc(&r->refs);
92}
93
94static inline __must_check bool refcount_sub_and_test(unsigned int i, refcount_t *r)
95{
96 return atomic_sub_and_test(i, &r->refs);
97}
98
Kees Cookfd25d19f2017-06-21 13:00:26 -070099static inline __must_check bool refcount_dec_and_test(refcount_t *r)
100{
101 return atomic_dec_and_test(&r->refs);
102}
103
104static inline void refcount_dec(refcount_t *r)
105{
106 atomic_dec(&r->refs);
107}
Kees Cook7a46ec02017-08-15 09:19:24 -0700108# endif /* !CONFIG_ARCH_HAS_REFCOUNT */
Kees Cookfd25d19f2017-06-21 13:00:26 -0700109#endif /* CONFIG_REFCOUNT_FULL */
Peter Zijlstraf405df52016-11-14 18:06:19 +0100110
Peter Zijlstra29dee3c2017-02-10 16:27:52 +0100111extern __must_check bool refcount_dec_if_one(refcount_t *r);
112extern __must_check bool refcount_dec_not_one(refcount_t *r);
113extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
114extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
Anna-Maria Gleixner7ea959c2018-06-12 18:16:21 +0200115extern __must_check bool refcount_dec_and_lock_irqsave(refcount_t *r,
116 spinlock_t *lock,
117 unsigned long *flags);
Peter Zijlstraf405df52016-11-14 18:06:19 +0100118#endif /* _LINUX_REFCOUNT_H */