XenevaOS
Loading...
Searching...
No Matches
cleanup.h
Go to the documentation of this file.
1#ifndef __LINUX_CLEANUP_H__
2#define __LINUX_CLEANUP_H__
3
4/*
5 * DCL <linux/cleanup.h> -- scope guards, in mainline's call syntax.
6 *
7 * Modern mainline tty/serial code does not say mutex_lock() ... mutex_unlock()
8 * a hundred lines apart. It says:
9 *
10 * guard(mutex)(&port->mutex) { ... }
11 * scoped_guard(spinlock_irq, &tty->ctrl.lock) { ... }
12 * scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->mutex) { ... }
13 *
14 * There are 120 such sites across serial_core.c, 8250_port.c, n_tty.c and
15 * tty_io.c, which makes this header load-bearing rather than ornamental:
16 * without it, none of them parse.
17 *
18 * Mainline builds them on __cleanup()/DEFINE_FREE()/IS_ERR() and a
19 * lock_ptr/lock_err protocol spread across <linux/compiler.h>, <linux/err.h>
20 * and <linux/args.h>. DCL keeps the *call syntax* -- which is what driver text
21 * depends on -- and implements it directly on the one piece of machinery both
22 * sides genuinely need, clang's __attribute__((cleanup)).
23 *
24 * The macros are deliberately spelled DCL_LOCK_GUARD*, not mainline's
25 * DEFINE_GUARD_*: adopting mainline's names while changing their argument
26 * lists would be a trap for whoever next ports serial_core.h. When that
27 * happens the few guard definitions in it get rewritten to these, and the
28 * call sites -- the part that matters -- stay byte-identical to upstream.
29 *
30 * The three forms:
31 *
32 * guard(name)(args) { ... }
33 * Acquire on the way in, release when the enclosing scope ends -- on
34 * fall-through, on `break`, on `return`, on `goto`, whichever comes
35 * first. That last part is the entire point: a `return` in the middle
36 * of a critical section still unlocks, which is exactly what a manual
37 * lock()/unlock() pair gets wrong once a function grows an error path.
38 *
39 * scoped_guard(name, args) { ... }
40 * The same, with the lifetime bound to the next compound statement.
41 *
42 * scoped_cond_guard(name, fail, args) { ... }
43 * For locks that can fail to be acquired. If acquisition failed the
44 * guard holds lock == NULL, so the body is skipped and `fail` runs
45 * instead -- and because nothing was ever locked, the destructor has
46 * nothing to release. `fail` is normally `return -ERESTARTSYS`.
47 *
48 * Guard *classes* (mutex, spinlock_irq, rwsem_read, uart_port_lock_irq, ...)
49 * live beside the primitives they wrap -- <linux/spinlock.h>, <linux/mutex.h>,
50 * <linux/rwsem.h>, later serial_core.h -- because a guard is only meaningful
51 * next to the lock it takes.
52 *
53 * DEFINE_FREE()/__free() were added once drm_bridge.h needed them: mainline
54 * spells the refcount drop of a chain-walked bridge as an automatic
55 * variable's destructor rather than an unmatched put at each return. Unlike
56 * the guard macros above, this one has no DCL-specific shape -- it is
57 * mainline's body verbatim -- so it lives here rather than beside a lock.
58 *
59 * DEFINE_CLASS()/ACQUIRE() are still not carried. The nine tty/serial files
60 * use none of them (only PCI_DEVICE_CLASS, an unrelated PCI macro, resembles
61 * them), so they are still not carried speculatively.
62 */
63
64/* clang scope-exit attribute; mainline spells this __cleanup too.
65 * Guarded because compiler_attributes.h:76 already defines it, and an
66 * unguarded second definition here produced a -Wmacro-redefined warning in
67 * every translation unit that reaches both -- which, now that <linux/file.h>
68 * includes this header, is most of them. The two definitions agree, so the
69 * guard costs nothing and keeps the noise count where it was. */
70#ifndef __cleanup
71#define __cleanup(fn) __attribute__((cleanup(fn)))
72#endif
73
74/*
75 * CLASS(name, var) -- the scoped-object declaration. Added after the other
76 * three because drm_syncobj.c:710 spells `CLASS(fd, f)(fd);`, and with
77 * nothing defined the line has neither a type nor an initialiser, so clang
78 * reports "use of undeclared identifier 'f'" at every later read of `f` --
79 * :710, :713 and :717, three sites for one missing macro.
80 *
81 * Note where the expansion stops. CLASS(fd, f) ends at the bare function
82 * designator `fd_constructor`, and the `(fd)` written at the *call site* is
83 * what supplies the argument. The macro cannot absorb it: the declaration
84 * and the call are written as two adjacent tokens by design, and that is
85 * also what lets the semicolon at :710 terminate an initialiser instead of
86 * leaving a declared-but-uninitialised variable.
87 *
88 * Only the fd class exists so far, and it lives in <linux/file.h> beside the
89 * struct it constructs and the two accessors that read it.
90 */
91#define CLASS(name, var) name##_t var __cleanup(name##_destructor) = name##_constructor
92
93/*
94 * DEFINE_FREE(name, type, free) / __free(name) -- the cleanup.h pair that
95 * mainline builds CLASS-free automatic storage on. Mainline's shape:
96 *
97 * DEFINE_FREE(drm_bridge_put, struct drm_bridge *,
98 * if (_T) drm_bridge_put(_T))
99 *
100 * declares __free_drm_bridge_put(void *p), and a declaration written
101 *
102 * struct drm_bridge *b __free(drm_bridge_put) = ...;
103 *
104 * runs that destructor when b goes out of scope. The destructor body is
105 * caller-written text that mentions a local `_T`, which the macro declares
106 * as the dereferenced pointee -- hence `_type _T = *(_type *)p` rather than
107 * a cast: for `_type = struct drm_bridge *` it evaluates to a
108 * `struct drm_bridge *` holding the pointer that was passed in, which is
109 * what makes `if (_T) drm_bridge_put(_T)` read correctly.
110 *
111 * The `if (_T)` is the caller's to write, not the macro's: mainline dropped
112 * the IS_ERR_OR_NULL() test that used to live here and moved the null check
113 * into each _free expression. Ported verbatim so a v7.2 caller that omits
114 * the test gets mainline's behaviour, including its null deref.
115 *
116 * __free() is the spelling the attribute is written with and is simply
117 * __cleanup() with the name spliced; __cleanup() above is already clang's
118 * scope-exit attribute, so nothing here needs new compiler support.
119 */
120#define DEFINE_FREE(_name, _type, _free) \
121 static __always_inline void __free_##_name(void *p) { _type _T = *(_type *)p; _free; }
122
123#define __free(_name) __cleanup(__free_##_name)
124
125#define no_free_ptr(p) \
126 ({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
127#define return_ptr(p) return no_free_ptr(p)
128
129#define DCL_CAT2(a, b) a##b
130#define DCL_CAT(a, b) DCL_CAT2(a, b)
131#define DCL_UID(base) DCL_CAT(base, __COUNTER__)
132
133/*
134 * DCL_LOCK_GUARD_TYPES(name, type, release) -- the half of a guard that both
135 * variants own: the struct carrying the held lock, NULL while it is not
136 * held, and the destructor that releases only a lock that is held.
137 *
138 * That NULL-means-not-held rule is the whole reason DCL_LOCK_GUARD_COND can
139 * report a failed acquire at all, so it is written once rather than twice --
140 * a destructor that disagreed with its own ctor would unlock a lock the
141 * caller never took, in code that runs on scope exit where nobody looks.
142 *
143 * Only the acquisition differs between the two macros, which is why each
144 * still spells its own: unconditional for DCL_LOCK_GUARD, an int expression
145 * that can fail for DCL_LOCK_GUARD_COND.
146 */
147#define DCL_LOCK_GUARD_TYPES(name, type, release) \
148 typedef struct { type* lock; } dclg_##name; \
149 static inline void dclg_##name##_release(dclg_##name* g) \
150 { \
151 type* _T = g->lock; \
152 if (_T) { release; } \
153 }
154
155/*
156 * DCL_LOCK_GUARD(name, type, acquire, release)
157 * For locks that cannot fail to be taken (spinlocks, the rwsem read side,
158 * task_lock): `acquire` runs unconditionally. `acquire` and `release` are
159 * statements written against a local `_T` of type `type *`.
160 *
161 * DCL_LOCK_GUARD_COND(name, type, acquire, release)
162 * Same shape, except `acquire` must be an *int expression* that is 0 on
163 * success (mutex_lock_interruptible's contract). A nonzero result leaves
164 * lock == NULL, which is how scoped_cond_guard() tells "failed" apart
165 * from "held" -- and how the destructor avoids unlocking a lock that was
166 * never taken.
167 */
168#define DCL_LOCK_GUARD(name, type, acquire, release) \
169 DCL_LOCK_GUARD_TYPES(name, type, release) \
170 static inline dclg_##name dclg_##name##_ctor(type* p) \
171 { \
172 dclg_##name g; \
173 type* _T = p; \
174 (void)_T; \
175 acquire; \
176 g.lock = p; \
177 return g; \
178 }
179
180#define DCL_LOCK_GUARD_COND(name, type, acquire, release) \
181 DCL_LOCK_GUARD_TYPES(name, type, release) \
182 static inline dclg_##name dclg_##name##_ctor(type* p) \
183 { \
184 dclg_##name g; \
185 type* _T = p; \
186 int _r = (acquire); \
187 (void)_T; \
188 g.lock = (_r == 0) ? p : 0; \
189 return g; \
190 }
191
192/*
193 * Mainline's spelling of the two forms that are actually instantiated.
194 *
195 * These exist because of Vendored/drivers/tty/serial/8250/8250.h, which says
196 * DEFINE_GUARD(serial8250_rpm, struct uart_8250_port *,
197 * serial8250_rpm_get(_T), serial8250_rpm_put(_T));
198 * and vendored files are never rewritten to suit DCL (Vendored/README.md
199 * rule 1) -- so when mainline text says DEFINE_GUARD, BaseHdr has to answer
200 * to that name. The DCL_* spellings stay for everything written by hand.
201 *
202 * The header above worried that adopting mainline's names "while changing
203 * their argument lists would be a trap". These do NOT change the list:
204 * DEFINE_GUARD(_name, _type, _lock, _unlock) has mainline's order, and the
205 * one real difference from DCL_LOCK_GUARD is that mainline passes a *pointer*
206 * type (`struct uart_port *`) and names the local `_T`, where DCL passes the
207 * pointee (`struct mutex`) and makes the local `type *_T`. Two shapes, each
208 * self-consistent, so neither spelling quietly means the other's.
209 *
210 * Not ported here, same rule as above: DEFINE_GUARD_COND and
211 * DEFINE_LOCK_GUARD_1_COND. serial_core.h's two _try definitions were
212 * removed with it -- nothing in 8250_port.c, serial_core.c or 8250.h takes a
213 * _try guard, and a conditional acquire with no caller is machinery nobody
214 * can test. The 3-argument forms are what to port if one shows up.
215 */
216
217/*
218 * DEFINE_GUARD(_name, _type, _lock, _unlock)
219 * _type is the POINTER type (mainline passes `struct uart_port *`), so
220 * the guard struct holds it directly and `_T` in the expressions is that
221 * pointer -- which is what makes `uart_port_unlock(_T)` read correctly.
222 */
223#define DEFINE_GUARD(_name, _type, _lock, _unlock) \
224 typedef struct { _type lock; } dclg_##_name; \
225 static inline void dclg_##_name##_release(dclg_##_name* g) \
226 { \
227 _type _T = g->lock; \
228 if (_T) { _unlock; } \
229 } \
230 static inline dclg_##_name dclg_##_name##_ctor(_type p) \
231 { \
232 dclg_##_name g; \
233 _type _T = p; \
234 (void)_T; \
235 _lock; \
236 g.lock = p; \
237 return g; \
238 }
239
240/*
241 * DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...)
242 * For guards whose lock routine carries state that outlives the call --
243 * uart_port_lock_irqsave() writes a saved-flags word that
244 * uart_port_unlock_irqrestore() reads back. That word has to live in the
245 * guard object, not on the ctor's stack, which is the entire reason this
246 * form takes a trailing declaration list (`unsigned long flags`).
247 *
248 * `_type` is the BARE type here (mainline passes `struct uart_port`), so
249 * the member is `_type *lock` and the expressions address into the guard:
250 * `_T->lock` and `_T->flags`. _T is therefore the guard, and it is
251 * written by the release function too -- both sides see the same struct
252 * because the ctor returns it by value and __attribute__((cleanup))
253 * passes the stored copy back.
254 */
255#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...) \
256 typedef struct { _type* lock; __VA_ARGS__; } dclg_##_name; \
257 typedef _type dcllt_##_name; \
258 static inline void dclg_##_name##_release(dclg_##_name* _T) \
259 { \
260 if (_T->lock) { _unlock; } \
261 } \
262 static inline dclg_##_name dclg_##_name##_ctor(_type* p) \
263 { \
264 dclg_##_name g = { .lock = p }; \
265 dclg_##_name* _T = &g; \
266 (void)_T; \
267 _lock; \
268 return g; \
269 }
270
271/*
272 * DECLARE_LOCK_GUARD_1_ATTRS(_name, _lock, _unlock) and
273 * WITH_LOCK_GUARD_1_ATTRS(_name, _T) -- the pairing mainline's
274 * cleanup.h:553/:557 spells, and the reason the typedef above exists:
275 * DECLARE_... has to re-state the constructor's prototype and mainline does
276 * that through `lock_##_name##_t`, which __DEFINE_UNLOCK_GUARD typedefs.
277 * DCL's DEFINE_LOCK_GUARD_1 did not keep such an alias, so `dcllt_##_name`
278 * is added beside it -- same idea, DCL spelling, and it has to agree with
279 * the definition's `_type*` or the re-declaration is a conflicting-types
280 * error at the call site rather than here.
281 *
282 * Only one call site exists today, seqlock.h:1363, declaring
283 * seqlock_init; nothing in the tree instantiates a guard from it, so what
284 * is needed is that the pair parses and matches. `_lock`/`_unlock` land
285 * where mainline puts them -- after the parameter list on the declaration,
286 * before the body on the definition -- and expand to nothing while
287 * WARN_CONTEXT_ANALYSIS is off, which is the arm this build takes
288 * (compiler-context-analysis.h:166).
289 */
290#define DECLARE_LOCK_GUARD_1_ATTRS(_name, _lock, _unlock) \
291 static inline dclg_##_name dclg_##_name##_ctor(dcllt_##_name *_T) _lock;\
292 static __always_inline void dclg_##_name##_unlock_ctx(dclg_##_name **_T)\
293 _unlock { }
294
295#define WITH_LOCK_GUARD_1_ATTRS(_name, _T) \
296 dclg_##_name##_ctor(_T), \
297 *DCL_UID(unlock) __cleanup(dclg_##_name##_unlock_ctx) = \
298 (void *)(unsigned long)(_T)
299
300/*
301 * DEFINE_LOCK_GUARD_0(_name, _lock, _unlock, ...)
302 * The no-object form: nothing to hold, so `lock` is not a pointer to a
303 * resource but the "this guard is held" flag -- mainline initialises it
304 * to (void*)1 in the constructor, which is what makes the release run
305 * unconditionally while a held lock would have gone NULL. That is the
306 * whole difference from DEFINE_LOCK_GUARD_1 above, and it is why this one
307 * takes no type: there is none.
308 *
309 * Three call sites wanted it and all three are simple acquire/release
310 * statements with no saved state: preempt.h:468/:469 (preempt_disable /
311 * preempt_enable, and the _notrace pair) and jump_label.h:337. DCL's
312 * guard variables are only ever written by guard()/scoped_guard(), and
313 * neither is spelled over a _0 guard yet -- what is needed today is that
314 * the definitions parse where mainline puts them, not that they fire.
315 *
316 * The `...` is mainline's and it is kept because clang accepts an empty
317 * __VA_ARGS__ in a struct body (verified), so a fourth argument -- an
318 * extra field beside `lock` -- costs nothing to leave room for.
319 */
320#define DEFINE_LOCK_GUARD_0(_name, _lock, _unlock, ...) \
321 typedef struct { void *lock; __VA_ARGS__; } dclg_##_name; \
322 static inline void dclg_##_name##_release(dclg_##_name* _T) \
323 { \
324 if (_T->lock) { _unlock; } \
325 } \
326 static inline dclg_##_name dclg_##_name##_ctor(void) \
327 { \
328 dclg_##_name _t = { .lock = (void*)1 }; \
329 dclg_##_name* _T = &_t; \
330 (void)_T; \
331 _lock; \
332 return _t; \
333 }
334
335/*
336 * guard(name)(args);
337 * An anonymous guard variable. The type name and the __COUNTER__ suffix
338 * keep two guards in one function from colliding, which is why the
339 * argument has to be pasted before it meets the counter (DCL_UID).
340 */
341#define guard(name) \
342 dclg_##name DCL_UID(dclg_##name) \
343 __cleanup(dclg_##name##_release) = dclg_##name##_ctor
344
345/*
346 * scoped_guard(name, args) { ... }
347 * The guard lives in the for-init, so its scope *is* the loop: leaving
348 * the body -- normally, by break, or by returning out of it -- runs the
349 * cleanup. `_dclg_done` starts NULL so the body runs exactly once; the
350 * increment arm trips it, which is what makes a `continue` end the loop
351 * rather than re-acquire the lock.
352 */
353#define scoped_guard(name, args...) \
354 for (dclg_##name _dclg __cleanup(dclg_##name##_release) \
355 = dclg_##name##_ctor(args), *_dclg_done = 0; \
356 !_dclg_done; _dclg_done = (void*)1)
357
358/*
359 * scoped_cond_guard(name, fail, args) { ... }
360 * Same loop, but the macro ends on an `if/else` so the caller's compound
361 * statement becomes the *else* branch: taken only when the lock was
362 * actually acquired. On failure `fail` runs inside the if -- usually a
363 * `return`, which fires the cleanup over a NULL lock and therefore
364 * releases nothing, exactly right.
365 */
366#define scoped_cond_guard(name, fail, args...) \
367 for (dclg_##name _dclg __cleanup(dclg_##name##_release) \
368 = dclg_##name##_ctor(args), *_dclg_done = 0; \
369 !_dclg_done; _dclg_done = (void*)1) \
370 if (_dclg.lock == 0) { fail; } else
371
372#endif /* __LINUX_CLEANUP_H__ */