Skip to content

Commit fcc22ac

Browse files
pkitszelPeter Zijlstra
authored andcommitted
cleanup: Adjust scoped_guard() macros to avoid potential warning
Change scoped_guard() and scoped_cond_guard() macros to make reasoning about them easier for static analysis tools (smatch, compiler diagnostics), especially to enable them to tell if the given usage of scoped_guard() is with a conditional lock class (interruptible-locks, try-locks) or not (like simple mutex_lock()). Add compile-time error if scoped_cond_guard() is used for non-conditional lock class. Beyond easier tooling and a little shrink reported by bloat-o-meter this patch enables developer to write code like: int foo(struct my_drv *adapter) { scoped_guard(spinlock, &adapter->some_spinlock) return adapter->spinlock_protected_var; } Current scoped_guard() implementation does not support that, due to compiler complaining: error: control reaches end of non-void function [-Werror=return-type] Technical stuff about the change: scoped_guard() macro uses common idiom of using "for" statement to declare a scoped variable. Unfortunately, current logic is too hard for compiler diagnostics to be sure that there is exactly one loop step; fix that. To make any loop so trivial that there is no above warning, it must not depend on any non-const variable to tell if there are more steps. There is no obvious solution for that in C, but one could use the compound statement expression with "goto" jumping past the "loop", effectively leaving only the subscope part of the loop semantics. More impl details: one more level of macro indirection is now needed to avoid duplicating label names; I didn't spot any other place that is using the "for (...; goto label) if (0) label: break;" idiom, so it's not packed for reuse beyond scoped_guard() family, what makes actual macros code cleaner. There was also a need to introduce const true/false variable per lock class, it is used to aid compiler diagnostics reasoning about "exactly 1 step" loops (note that converting that to function would undo the whole benefit). Big thanks to Andy Shevchenko for help on this patch, both internal and public, ranging from whitespace/formatting, through commit message clarifications, general improvements, ending with presenting alternative approaches - all despite not even liking the idea. Big thanks to Dmitry Torokhov for the idea of compile-time check for scoped_cond_guard() (to use it only with conditional locsk), and general improvements for the patch. Big thanks to David Lechner for idea to cover also scoped_cond_guard(). Signed-off-by: Przemek Kitszel <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Dmitry Torokhov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 0d75e0c commit fcc22ac

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

include/linux/cleanup.h

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -285,14 +285,20 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
285285
* similar to scoped_guard(), except it does fail when the lock
286286
* acquire fails.
287287
*
288+
* Only for conditional locks.
288289
*/
289290

291+
#define __DEFINE_CLASS_IS_CONDITIONAL(_name, _is_cond) \
292+
static __maybe_unused const bool class_##_name##_is_conditional = _is_cond
293+
290294
#define DEFINE_GUARD(_name, _type, _lock, _unlock) \
295+
__DEFINE_CLASS_IS_CONDITIONAL(_name, false); \
291296
DEFINE_CLASS(_name, _type, if (_T) { _unlock; }, ({ _lock; _T; }), _type _T); \
292297
static inline void * class_##_name##_lock_ptr(class_##_name##_t *_T) \
293298
{ return (void *)(__force unsigned long)*_T; }
294299

295300
#define DEFINE_GUARD_COND(_name, _ext, _condlock) \
301+
__DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \
296302
EXTEND_CLASS(_name, _ext, \
297303
({ void *_t = _T; if (_T && !(_condlock)) _t = NULL; _t; }), \
298304
class_##_name##_t _T) \
@@ -303,17 +309,40 @@ static inline class_##_name##_t class_##_name##ext##_constructor(_init_args) \
303309
CLASS(_name, __UNIQUE_ID(guard))
304310

305311
#define __guard_ptr(_name) class_##_name##_lock_ptr
312+
#define __is_cond_ptr(_name) class_##_name##_is_conditional
306313

307-
#define scoped_guard(_name, args...) \
308-
for (CLASS(_name, scope)(args), \
309-
*done = NULL; __guard_ptr(_name)(&scope) && !done; done = (void *)1)
310-
311-
#define scoped_cond_guard(_name, _fail, args...) \
312-
for (CLASS(_name, scope)(args), \
313-
*done = NULL; !done; done = (void *)1) \
314-
if (!__guard_ptr(_name)(&scope)) _fail; \
315-
else
316-
314+
/*
315+
* Helper macro for scoped_guard().
316+
*
317+
* Note that the "!__is_cond_ptr(_name)" part of the condition ensures that
318+
* compiler would be sure that for the unconditional locks the body of the
319+
* loop (caller-provided code glued to the else clause) could not be skipped.
320+
* It is needed because the other part - "__guard_ptr(_name)(&scope)" - is too
321+
* hard to deduce (even if could be proven true for unconditional locks).
322+
*/
323+
#define __scoped_guard(_name, _label, args...) \
324+
for (CLASS(_name, scope)(args); \
325+
__guard_ptr(_name)(&scope) || !__is_cond_ptr(_name); \
326+
({ goto _label; })) \
327+
if (0) { \
328+
_label: \
329+
break; \
330+
} else
331+
332+
#define scoped_guard(_name, args...) \
333+
__scoped_guard(_name, __UNIQUE_ID(label), args)
334+
335+
#define __scoped_cond_guard(_name, _fail, _label, args...) \
336+
for (CLASS(_name, scope)(args); true; ({ goto _label; })) \
337+
if (!__guard_ptr(_name)(&scope)) { \
338+
BUILD_BUG_ON(!__is_cond_ptr(_name)); \
339+
_fail; \
340+
_label: \
341+
break; \
342+
} else
343+
344+
#define scoped_cond_guard(_name, _fail, args...) \
345+
__scoped_cond_guard(_name, _fail, __UNIQUE_ID(label), args)
317346
/*
318347
* Additional helper macros for generating lock guards with types, either for
319348
* locks that don't have a native type (eg. RCU, preempt) or those that need a
@@ -369,14 +398,17 @@ static inline class_##_name##_t class_##_name##_constructor(void) \
369398
}
370399

371400
#define DEFINE_LOCK_GUARD_1(_name, _type, _lock, _unlock, ...) \
401+
__DEFINE_CLASS_IS_CONDITIONAL(_name, false); \
372402
__DEFINE_UNLOCK_GUARD(_name, _type, _unlock, __VA_ARGS__) \
373403
__DEFINE_LOCK_GUARD_1(_name, _type, _lock)
374404

375405
#define DEFINE_LOCK_GUARD_0(_name, _lock, _unlock, ...) \
406+
__DEFINE_CLASS_IS_CONDITIONAL(_name, false); \
376407
__DEFINE_UNLOCK_GUARD(_name, void, _unlock, __VA_ARGS__) \
377408
__DEFINE_LOCK_GUARD_0(_name, _lock)
378409

379410
#define DEFINE_LOCK_GUARD_1_COND(_name, _ext, _condlock) \
411+
__DEFINE_CLASS_IS_CONDITIONAL(_name##_ext, true); \
380412
EXTEND_CLASS(_name, _ext, \
381413
({ class_##_name##_t _t = { .lock = l }, *_T = &_t;\
382414
if (_T->lock && !(_condlock)) _T->lock = NULL; \

0 commit comments

Comments
 (0)