blob: 4193c41e383a897273605aac39f331b46512691a [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>
Peter Zijlstraf405df52016-11-14 18:06:19 +01006#include <linux/mutex.h>
7#include <linux/spinlock.h>
Elena Reshetova318b1dedc2017-02-23 15:09:34 +02008#include <linux/kernel.h>
Peter Zijlstraf405df52016-11-14 18:06:19 +01009
David Windsorbd174162017-03-10 10:34:12 -050010/**
Elena Reshetovab6e859f2017-12-05 12:46:35 +020011 * struct refcount_t - variant of atomic_t specialized for reference counts
David Windsorbd174162017-03-10 10:34:12 -050012 * @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 Zijlstraf405df52016-11-14 18:06:19 +010018typedef struct refcount_struct {
19 atomic_t refs;
20} refcount_t;
21
22#define REFCOUNT_INIT(n) { .refs = ATOMIC_INIT(n), }
23
David Windsorbd174162017-03-10 10:34:12 -050024/**
25 * refcount_set - set a refcount's value
26 * @r: the refcount
27 * @n: value to which the refcount will be set
28 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010029static inline void refcount_set(refcount_t *r, unsigned int n)
30{
31 atomic_set(&r->refs, n);
32}
33
David Windsorbd174162017-03-10 10:34:12 -050034/**
35 * refcount_read - get a refcount's value
36 * @r: the refcount
37 *
38 * Return: the refcount's value
39 */
Peter Zijlstraf405df52016-11-14 18:06:19 +010040static inline unsigned int refcount_read(const refcount_t *r)
41{
42 return atomic_read(&r->refs);
43}
44
Kees Cookfd25d19f2017-06-21 13:00:26 -070045#ifdef CONFIG_REFCOUNT_FULL
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010046extern __must_check bool refcount_add_not_zero(unsigned int i, refcount_t *r);
47extern void refcount_add(unsigned int i, refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010048
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010049extern __must_check bool refcount_inc_not_zero(refcount_t *r);
50extern void refcount_inc(refcount_t *r);
Peter Zijlstraf405df52016-11-14 18:06:19 +010051
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010052extern __must_check bool refcount_sub_and_test(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
Kees Cook7a46ec02017-08-15 09:19:24 -070057# ifdef CONFIG_ARCH_HAS_REFCOUNT
58# include <asm/refcount.h>
59# else
Kees Cookfd25d19f2017-06-21 13:00:26 -070060static 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
65static inline void refcount_add(unsigned int i, refcount_t *r)
66{
67 atomic_add(i, &r->refs);
68}
69
70static inline __must_check bool refcount_inc_not_zero(refcount_t *r)
71{
72 return atomic_add_unless(&r->refs, 1, 0);
73}
74
75static inline void refcount_inc(refcount_t *r)
76{
77 atomic_inc(&r->refs);
78}
79
80static 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 Cookfd25d19f2017-06-21 13:00:26 -070085static inline __must_check bool refcount_dec_and_test(refcount_t *r)
86{
87 return atomic_dec_and_test(&r->refs);
88}
89
90static inline void refcount_dec(refcount_t *r)
91{
92 atomic_dec(&r->refs);
93}
Kees Cook7a46ec02017-08-15 09:19:24 -070094# endif /* !CONFIG_ARCH_HAS_REFCOUNT */
Kees Cookfd25d19f2017-06-21 13:00:26 -070095#endif /* CONFIG_REFCOUNT_FULL */
Peter Zijlstraf405df52016-11-14 18:06:19 +010096
Peter Zijlstra29dee3c2017-02-10 16:27:52 +010097extern __must_check bool refcount_dec_if_one(refcount_t *r);
98extern __must_check bool refcount_dec_not_one(refcount_t *r);
99extern __must_check bool refcount_dec_and_mutex_lock(refcount_t *r, struct mutex *lock);
100extern __must_check bool refcount_dec_and_lock(refcount_t *r, spinlock_t *lock);
Peter Zijlstraf405df52016-11-14 18:06:19 +0100101
102#endif /* _LINUX_REFCOUNT_H */