]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/linux/refcount.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_REFCOUNT_H
3 #define _LINUX_REFCOUNT_H
5 #include <linux/atomic.h>
6 #include <linux/compiler.h>
7 #include <linux/spinlock_types.h>
12 * struct refcount_t - variant of atomic_t specialized for reference counts
13 * @refs: atomic_t counter field
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.
19 typedef struct refcount_struct
{
23 #define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
26 * refcount_set - set a refcount's value
28 * @n: value to which the refcount will be set
30 static inline void refcount_set(refcount_t
*r
, unsigned int n
)
32 atomic_set(&r
->refs
, n
);
36 * refcount_read - get a refcount's value
39 * Return: the refcount's value
41 static inline unsigned int refcount_read(const refcount_t
*r
)
43 return atomic_read(&r
->refs
);
46 extern __must_check
bool refcount_add_not_zero_checked(unsigned int i
, refcount_t
*r
);
47 extern void refcount_add_checked(unsigned int i
, refcount_t
*r
);
49 extern __must_check
bool refcount_inc_not_zero_checked(refcount_t
*r
);
50 extern void refcount_inc_checked(refcount_t
*r
);
52 extern __must_check
bool refcount_sub_and_test_checked(unsigned int i
, refcount_t
*r
);
54 extern __must_check
bool refcount_dec_and_test_checked(refcount_t
*r
);
55 extern void refcount_dec_checked(refcount_t
*r
);
57 #ifdef CONFIG_REFCOUNT_FULL
59 #define refcount_add_not_zero refcount_add_not_zero_checked
60 #define refcount_add refcount_add_checked
62 #define refcount_inc_not_zero refcount_inc_not_zero_checked
63 #define refcount_inc refcount_inc_checked
65 #define refcount_sub_and_test refcount_sub_and_test_checked
67 #define refcount_dec_and_test refcount_dec_and_test_checked
68 #define refcount_dec refcount_dec_checked
71 # ifdef CONFIG_ARCH_HAS_REFCOUNT
72 # include <asm/refcount.h>
74 static inline __must_check
bool refcount_add_not_zero(unsigned int i
, refcount_t
*r
)
76 return atomic_add_unless(&r
->refs
, i
, 0);
79 static inline void refcount_add(unsigned int i
, refcount_t
*r
)
81 atomic_add(i
, &r
->refs
);
84 static inline __must_check
bool refcount_inc_not_zero(refcount_t
*r
)
86 return atomic_add_unless(&r
->refs
, 1, 0);
89 static inline void refcount_inc(refcount_t
*r
)
94 static inline __must_check
bool refcount_sub_and_test(unsigned int i
, refcount_t
*r
)
96 return atomic_sub_and_test(i
, &r
->refs
);
99 static inline __must_check
bool refcount_dec_and_test(refcount_t
*r
)
101 return atomic_dec_and_test(&r
->refs
);
104 static inline void refcount_dec(refcount_t
*r
)
106 atomic_dec(&r
->refs
);
108 # endif /* !CONFIG_ARCH_HAS_REFCOUNT */
109 #endif /* CONFIG_REFCOUNT_FULL */
111 extern __must_check
bool refcount_dec_if_one(refcount_t
*r
);
112 extern __must_check
bool refcount_dec_not_one(refcount_t
*r
);
113 extern __must_check
bool refcount_dec_and_mutex_lock(refcount_t
*r
, struct mutex
*lock
);
114 extern __must_check
bool refcount_dec_and_lock(refcount_t
*r
, spinlock_t
*lock
);
115 extern __must_check
bool refcount_dec_and_lock_irqsave(refcount_t
*r
,
117 unsigned long *flags
);
118 #endif /* _LINUX_REFCOUNT_H */