1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_GUARDS_H 3 #define __LINUX_GUARDS_H 4 5 #include <linux/compiler.h> 6 7 /* 8 * DEFINE_FREE(name, type, free): 9 * simple helper macro that defines the required wrapper for a __free() 10 * based cleanup function. @free is an expression using '_T' to access 11 * the variable. 12 * 13 * __free(name): 14 * variable attribute to add a scoped based cleanup to the variable. 15 * 16 * no_free_ptr(var): 17 * like a non-atomic xchg(var, NULL), such that the cleanup function will 18 * be inhibited -- provided it sanely deals with a NULL value. 19 * 20 * return_ptr(p): 21 * returns p while inhibiting the __free(). 22 * 23 * Ex. 24 * 25 * DEFINE_FREE(kfree, void *, if (_T) kfree(_T)) 26 * 27 * struct obj *p __free(kfree) = kmalloc(...); 28 * if (!p) 29 * return NULL; 30 * 31 * if (!init_obj(p)) 32 * return NULL; 33 * 34 * return_ptr(p); 35 */ 36 37 #define DEFINE_FREE(_name, _type, _free) \ 38 static inline void __free_##_name(void *p) { _type _T = *(_type *)p; _free; } 39 40 #define __free(_name) __cleanup(__free_##_name) 41 42 #define no_free_ptr(p) \ 43 ({ __auto_type __ptr = (p); (p) = NULL; __ptr; }) 44 45 #define return_ptr(p) return no_free_ptr(p) 46 47 48 /* 49 * DEFINE_CLASS(name, type, exit, init, init_args...): 50 * helper to define the destructor and constructor for a type. 51 * @exit is an expression using '_T' -- similar to FREE above. 52 * @init is an expression in @init_args resulting in @type 53 * 54 * EXTEND_CLASS(name, ext, init, init_args...): 55 * extends class @name to @name@ext with the new constructor 56 * 57 * CLASS(name, var)(args...): 58 * declare the variable @var as an instance of the named class 59 * 60 * Ex. 61 * 62 * DEFINE_CLASS(fdget, struct fd, fdput(_T), fdget(fd), int fd) 63 * 64 * CLASS(fdget, f)(fd); 65 * if (!f.file) 66 * return -EBADF; 67 * 68 * // use 'f' without concern 69 */ 70 71 #define DEFINE_CLASS(_name, _type, _exit, _init, _init_args...) \ 72 typedef _type class_##_name##_t; \ 73 static inline void class_##_name##_destructor(_type *p) \ 74 { _type _T = *p; _exit; } \ 75 static inline _type class_##_name##_constructor(_init_args) \ 76 { _type t = _init; return t; } 77 78 #define EXTEND_CLASS(_name, ext, _init, _init_args...) \ 79 typedef class_##_name##_t class_##_name##ext##_t; \ 80 static inline void class_##_name##ext##_destructor(class_##_name##_t *p)\ 81 { class_##_name##_destructor(p); } \ 82 static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \ 83 { class_##_name##_t t = _init; return t; } 84 85 #define CLASS(_name, var) \ 86 class_##_name##_t var __cleanup(class_##_name##_destructor) = \ 87 class_##_name##_constructor 88 89 90 /* 91 * DEFINE_GUARD(name, type, lock, unlock): 92 * trivial wrapper around DEFINE_CLASS() above specifically 93 * for locks. 94 * 95 * DEFINE_GUARD_COND(name, ext, condlock) 96 * wrapper around EXTEND_CLASS above to add conditional lock 97 * variants to a base class, eg. mutex_trylock() or 98 * mutex_lock_interruptible(). 99 * 100 * guard(name): 101 * an anonymous instance of the (guard) class, not recommended for 102 * conditional locks. 103 * 104 * scoped_guard (name, args...) { }: 105 * similar to CLASS(name, scope)(args), except the variable (with the 106 * explicit name 'scope') is declard in a for-loop such that its scope is 107 * bound to the next (compound) statement. 108 * 109 * for conditional locks the loop body is skipped when the lock is not 110 * acquired. 111 * 112 * scoped_cond_guard (name, fail, args...) { }: 113 * similar to scoped_guard(), except it does fail when the lock 114 * acquire fails. 115 * 116 * Only for conditional locks. 117 */ 118 119 #define __DEFINE_CLASS_IS_CONDITIONAL(_name, _is_cond) \ 120 static __maybe_unused const bool class_##_name##_is_conditional = _is_cond 121 122 #define DEFINE_GUARD(_name, _type, _lock, _unlock) \ 123 __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \ 124 DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \ 125 static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \ 126 { return (void *)(__force unsigned long)*_T; } 127 128 #define DEFINE_GUARD_COND(_name, _ext, _condlock) \ 129 __DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \ 130 EXTEND_CLASS(_name, _ext, \ 131 ({ void *_t = _T; if (_T && !(_condlock)) _t = NULL; _t; }), \ 132 class_##_name##_t _T) \ 133 static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \ 134 { return class_##_name##_lock_ptr(_T); } 135 136 #define guard(_name) \ 137 CLASS(_name, __UNIQUE_ID(guard)) 138 139 #define __guard_ptr(_name) class_##_name##_lock_ptr 140 #define __is_cond_ptr(_name) class_##_name##_is_conditional 141 142 /* 143 * Helper macro for scoped_guard(). 144 * 145 * Note that the "!__is_cond_ptr(_name)" part of the condition ensures that 146 * compiler would be sure that for the unconditional locks the body of the 147 * loop (caller-provided code glued to the else clause) could not be skipped. 148 * It is needed because the other part - "__guard_ptr(_name)(&scope)" - is too 149 * hard to deduce (even if could be proven true for unconditional locks). 150 */ 151 #define __scoped_guard(_name, _label, args...) \ 152 for (CLASS(_name, scope)(args); \ 153 __guard_ptr(_name)(&scope) || !__is_cond_ptr(_name); \ 154 ({ goto _label; })) \ 155 if (0) { \ 156 _label: \ 157 break; \ 158 } else 159 160 #define scoped_guard(_name, args...) \ 161 __scoped_guard(_name, __UNIQUE_ID(label), args) 162 163 #define __scoped_cond_guard(_name, _fail, _label, args...) \ 164 for (CLASS(_name, scope)(args); true; ({ goto _label; })) \ 165 if (!__guard_ptr(_name)(&scope)) { \ 166 BUILD_BUG_ON(!__is_cond_ptr(_name)); \ 167 _fail; \ 168 _label: \ 169 break; \ 170 } else 171 172 #define scoped_cond_guard(_name, _fail, args...) \ 173 __scoped_cond_guard(_name, _fail, __UNIQUE_ID(label), args) 174 /* 175 * Additional helper macros for generating lock guards with types, either for 176 * locks that don't have a native type (eg. RCU, preempt) or those that need a 177 * 'fat' pointer (eg. spin_lock_irqsave). 178 * 179 * DEFINE_LOCK_GUARD_0(name, lock, unlock, ...) 180 * DEFINE_LOCK_GUARD_1(name, type, lock, unlock, ...) 181 * DEFINE_LOCK_GUARD_1_COND(name, ext, condlock) 182 * 183 * will result in the following type: 184 * 185 * typedef struct { 186 * type *lock; // 'type := void' for the _0 variant 187 * __VA_ARGS__; 188 * } class_##name##_t; 189 * 190 * As above, both _lock and _unlock are statements, except this time '_T' will 191 * be a pointer to the above struct. 192 */ 193 194 #define __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, ...) \ 195 typedef struct { \ 196 _type *lock; \ 197 __VA_ARGS__; \ 198 } class_##_name##_t; \ 199 \ 200 static inline void class_##_name##_destructor(class_##_name##_t *_T) \ 201 { \ 202 if (_T->lock) { _unlock; } \ 203 } \ 204 \ 205 static inline void *class_##_name##_lock_ptr(class_##_name##_t *_T) \ 206 { \ 207 return (void *)(__force unsigned long)_T->lock; \ 208 } 209 210 211 #define __DEFINE_LOCK_GUARD_1(_name, _type, _lock) \ 212 static inline class_##_name##_t class_##_name##_constructor(_type *l) \ 213 { \ 214 class_##_name##_t _t = { .lock = l }, *_T = &_t; \ 215 _lock; \ 216 return _t; \ 217 } 218 219 #define __DEFINE_LOCK_GUARD_0(_name, _lock) \ 220 static inline class_##_name##_t class_##_name##_constructor(void) \ 221 { \ 222 class_##_name##_t _t = { .lock = (void*)1 }, \ 223 *_T __maybe_unused = &_t; \ 224 _lock; \ 225 return _t; \ 226 } 227 228 #define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...) \ 229 __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \ 230 __DEFINE_UNLOCK_GUARD(_name, _type, _unlock, __VA_ARGS__) \ 231 __DEFINE_LOCK_GUARD_1(_name, _type, _lock) 232 233 #define DEFINE_LOCK_GUARD_0(_name, _lock, _unlock, ...) \ 234 __DEFINE_CLASS_IS_CONDITIONAL(_name, false); \ 235 __DEFINE_UNLOCK_GUARD(_name, void, _unlock, __VA_ARGS__) \ 236 __DEFINE_LOCK_GUARD_0(_name, _lock) 237 238 #define DEFINE_LOCK_GUARD_1_COND(_name, _ext, _condlock) \ 239 __DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \ 240 EXTEND_CLASS(_name, _ext, \ 241 ({ class_##_name##_t _t = { .lock = l }, *_T = &_t;\ 242 if (_T->lock && !(_condlock)) _T->lock = NULL; \ 243 _t; }), \ 244 typeof_member(class_##_name##_t, lock) l) \ 245 static inline void * class_##_name##_ext##_lock_ptr(class_##_name##_t *_T) \ 246 { return class_##_name##_lock_ptr(_T); } 247 248 249 #endif /* __LINUX_GUARDS_H */ 250