1 /* SPDX-License-Identifier: GPL-2.0-only */ 1 2 #ifndef _LINUX_RCUREF_H 3 #define _LINUX_RCUREF_H 4 5 #include <linux/atomic.h> 6 #include <linux/bug.h> 7 #include <linux/limits.h> 8 #include <linux/lockdep.h> 9 #include <linux/preempt.h> 10 #include <linux/rcupdate.h> 11 12 #define RCUREF_ONEREF 0x00000000U 13 #define RCUREF_MAXREF 0x7FFFFFFFU 14 #define RCUREF_SATURATED 0xA0000000U 15 #define RCUREF_RELEASED 0xC0000000U 16 #define RCUREF_DEAD 0xE0000000U 17 #define RCUREF_NOREF 0xFFFFFFFFU 18 19 /** 20 * rcuref_init - Initialize a rcuref reference 21 * @ref: Pointer to the reference count 22 * @cnt: The initial reference count ty 23 */ 24 static inline void rcuref_init(rcuref_t *ref, 25 { 26 atomic_set(&ref->refcnt, cnt - 1); 27 } 28 29 /** 30 * rcuref_read - Read the number of held refer 31 * @ref: Pointer to the reference count 32 * 33 * Return: The number of held references (0 .. 34 */ 35 static inline unsigned int rcuref_read(rcuref_ 36 { 37 unsigned int c = atomic_read(&ref->ref 38 39 /* Return 0 if within the DEAD zone. * 40 return c >= RCUREF_RELEASED ? 0 : c + 41 } 42 43 extern __must_check bool rcuref_get_slowpath(r 44 45 /** 46 * rcuref_get - Acquire one reference on a rcu 47 * @ref: Pointer to the reference count 48 * 49 * Similar to atomic_inc_not_zero() but satura 50 * 51 * Provides no memory ordering, it is assumed 52 * object memory to be stable (RCU, etc.). It 53 * and thereby orders future stores. See docum 54 * 55 * Return: 56 * False if the attempt to acquire a refe 57 * when the last reference has been put a 58 * 59 * True if a reference was successfully a 60 */ 61 static inline __must_check bool rcuref_get(rcu 62 { 63 /* 64 * Unconditionally increase the refere 65 * dead zones provide enough tolerance 66 */ 67 if (likely(!atomic_add_negative_relaxe 68 return true; 69 70 /* Handle the cases inside the saturat 71 return rcuref_get_slowpath(ref); 72 } 73 74 extern __must_check bool rcuref_put_slowpath(r 75 76 /* 77 * Internal helper. Do not invoke directly. 78 */ 79 static __always_inline __must_check bool __rcu 80 { 81 RCU_LOCKDEP_WARN(!rcu_read_lock_held() 82 "suspicious rcuref_pu 83 /* 84 * Unconditionally decrease the refere 85 * dead zones provide enough tolerance 86 */ 87 if (likely(!atomic_add_negative_releas 88 return false; 89 90 /* 91 * Handle the last reference drop and 92 * and dead zones. 93 */ 94 return rcuref_put_slowpath(ref); 95 } 96 97 /** 98 * rcuref_put_rcusafe -- Release one reference 99 * @ref: Pointer to the reference count 100 * 101 * Provides release memory ordering, such that 102 * before, and provides an acquire ordering on 103 * must come after. 104 * 105 * Can be invoked from contexts, which guarant 106 * happen which would free the object concurre 107 * the last reference and the slowpath races a 108 * put() pair. rcu_read_lock()'ed and atomic c 109 * 110 * Return: 111 * True if this was the last reference wi 112 * possible. This signals the caller that 113 * object which is protected by the refer 114 * 115 * False if there are still active refere 116 * with a concurrent get()/put() pair. Ca 117 * release the protected object. 118 */ 119 static inline __must_check bool rcuref_put_rcu 120 { 121 return __rcuref_put(ref); 122 } 123 124 /** 125 * rcuref_put -- Release one reference for a r 126 * @ref: Pointer to the reference count 127 * 128 * Can be invoked from any context. 129 * 130 * Provides release memory ordering, such that 131 * before, and provides an acquire ordering on 132 * must come after. 133 * 134 * Return: 135 * 136 * True if this was the last reference wi 137 * possible. This signals the caller that 138 * object, which is protected by the refe 139 * deconstruction. 140 * 141 * False if there are still active refere 142 * with a concurrent get()/put() pair. Ca 143 * deconstruct the protected object. 144 */ 145 static inline __must_check bool rcuref_put(rcu 146 { 147 bool released; 148 149 preempt_disable(); 150 released = __rcuref_put(ref); 151 preempt_enable(); 152 return released; 153 } 154 155 #endif 156
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.