[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251220215608.434614-3-david.laight.linux@gmail.com>
Date: Sat, 20 Dec 2025 21:56:05 +0000
From: david.laight.linux@...il.com
To: Thomas Gleixner <tglx@...utronix.de>,
Linus Torvalds <torvalds@...ux-foundation.org>,
Peter Zijlstra <peterz@...radead.org>,
Mathieu Desnoyers <mathieu.desnoyers@...icios.com>,
Kees Cook <kees@...nel.org>,
linux-kernel@...r.kernel.org,
akpm@...ux-foundation.org,
Al Viro <viro@...iv.linux.org.uk>
Cc: David Laight <david.laight.linux@...il.com>
Subject: [PATCH 2/5] compiler.h: Add generic support for 'autoterminating nested for() loops'
From: David Laight <david.laight.linux@...il.com>
Autoterminating nested for() loops can be used inside #defines to
declare variables that are scoped to the statement that follows.
These are used by __scoped_user_access() but may have other uses
and the gory details are best separated from the use.
Using 'with (declaration)' and 'and_with (declaration)' seems to
read reasonably well and doesn't seem to collide with any existing
code.
As an example the scoped user access definition becomes:
with (typeof(uptr) _tmpptr = __scoped_user_access_begin(mode, uptr, size, elbl)) \
and_with (CLASS(user_##mode##_access, scope)(_tmpptr)) \
/* Force modified pointer usage within the scope */ \
and_with (const typeof(uptr) uptr = _tmpptr)
Signed-off-by: David Laight <david.laight.linux@...il.com>
---
include/linux/compiler.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/include/linux/compiler.h b/include/linux/compiler.h
index 04487c9bd751..da89a5efda5a 100644
--- a/include/linux/compiler.h
+++ b/include/linux/compiler.h
@@ -377,6 +377,32 @@ static inline void *offset_to_ptr(const int *off)
*/
#define prevent_tail_call_optimization() mb()
+/*
+ * Sometimes a #define needs to declare a variable that is scoped
+ * to the statement that follows without having mismatched {}.
+ * with (int x = expression) {
+ * statements
+ * }
+ * is the same as:
+ * {
+ * int x = expression;
+ * statements
+ * }
+ * but lets it all be hidden from the call site, eg:
+ * frobnicate(args) {
+ * statements
+ * }
+ * Only a single variable can be defined, and_with() allows extra ones
+ * without adding an additional outer loop.
+ *
+ * The controlled scope can be terminated using break, continue or goto.
+ */
+#define with(declaration) \
+ for (bool _with_done = false; !_with_done; _with_done = true) \
+ and_with (declaration)
+#define and_with(declaration) \
+ for (declaration; !_with_done; _with_done = true)
+
#include <asm/rwonce.h>
#endif /* __LINUX_COMPILER_H */
--
2.39.5
Powered by blists - more mailing lists