kernel.h: Split out COUNT_ARGS() and CONCATENATE() to args.h
PATCH v2 1/1 kernel.h: Split out COUNT_ARGS() and CONCATENATE() to args.h
kernel.h is being used as a dump for all kinds of stuff for a long time. The COUNT_ARGS() and CONCATENATE() macros may be used in some places without need of the full kernel.h dependency train with it.
Here is the attempt on cleaning it up by splitting out these macros().
While at it, include new header where it's being used and drop custom implementation of these macros and document how it works.
---
v2: converted existing users at the same time, documented (Andrew, Rasmus)
arch/x86/include/asm/rmwcc.h | 11 +++-------- include/kunit/test.h | 1 + include/linux/args.h | 28 ++++++++++++++++++++++++++++ include/linux/arm-smccc.h | 27 ++++++++++----------------- include/linux/genl_magic_struct.h | 8 +++----- include/linux/kernel.h | 7 ------- include/linux/pci.h | 2 +- include/trace/bpf_probe.h | 2 ++ 8 files changed, 48 insertions(+), 38 deletions(-) create mode 100644 include/linux/args.h
arch/x86/include/asm/rmwcc.h +3 -8
@@ -2,12 +2,7 @@#ifndef _ASM_X86_RMWcc#define _ASM_X86_RMWcc -/* This counts to 12. Any more, it will return 13th argument. */ -#define __RMWcc_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n -#define RMWcc_ARGS(X...) __RMWcc_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) - -#define __RMWcc_CONCAT (a, b) a ## b -#define RMWcc_CONCAT (a, b) __RMWcc_CONCAT(a, b) +#include <linux/args.h> #define __CLOBBERS_MEM(clb...) "memory", ## clb
@@ -48,7 +43,7 @@ cc_label: c = true; \#define GEN_UNARY_RMWcc_3 (op, var, cc) \ GEN_UNARY_RMWcc_4(op, var, cc, "%[var]") -#define GEN_UNARY_RMWcc(X...) RMWcc_CONCAT(GEN_UNARY_RMWcc_, RMWcc_ARGS(X))(X) +#define GEN_UNARY_RMWcc(X...) CONCATENATE(GEN_UNARY_RMWcc_, COUNT_ARGS(X))(X)#define GEN_BINARY_RMWcc_6 (op, var, cc, vcon, _val, arg0) \ __GEN_RMWcc(op " %[val], " arg0, var, cc, \
@@ -57,7 +52,7 @@ cc_label: c = true; \#define GEN_BINARY_RMWcc_5 (op, var, cc, vcon, val) \ GEN_BINARY_RMWcc_6(op, var, cc, vcon, val, "%[var]") -#define GEN_BINARY_RMWcc(X...) RMWcc_CONCAT(GEN_BINARY_RMWcc_, RMWcc_ARGS(X))(X) +#define GEN_BINARY_RMWcc(X...) CONCATENATE(GEN_BINARY_RMWcc_, COUNT_ARGS(X))(X) #define GEN_UNARY_SUFFIXED_RMWcc(op, suffix, var, cc, clobbers...) \ __GEN_RMWcc(op " %[var]\n\t" suffix, var, cc, \
include/kunit/test.h +1 -0
@@ -12,6 +12,7 @@#include <kunit/assert.h> #include <kunit/try-catch.h> +#include <linux/args.h> #include <linux/compiler.h> #include <linux/container_of.h> #include <linux/err.h>
include/linux/args.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_ARGS_H +#define _LINUX_ARGS_H + +/* +* How do these macros work? +* +* In __COUNT_ARGS() _0 to _12 are just placeholders from the start +* in order to make sure _n is positioned over the correct number +* from 12 to 0 (depending on X, which is a variadic argument list). +* They serve no purpose other than occupying a position. Since each +* macro parameter must have a distinct identifier, those identifiers +* are as good as any. +* +* In COUNT_ARGS() we use actual integers, so __COUNT_ARGS() returns +* that as _n. +*/ + +/* This counts to 12. Any more, it will return 13th argument. */ +#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n +#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +/* Concatenate two parameters, but allow them to be expanded beforehand. */ +#define __CONCAT (a, b) a ## b +#define CONCATENATE (a, b) __CONCAT(a, b) + +#endif /* _LINUX_ARGS_H */
include/linux/arm-smccc.h +10 -17
@@ -5,6 +5,7 @@#ifndef __LINUX_ARM_SMCCC_H#define __LINUX_ARM_SMCCC_H +#include <linux/args.h> #include <linux/init.h> #include <uapi/linux/const.h>
@@ -413,11 +414,6 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,#endif -#define ___count_args (_0, _1, _2, _3, _4, _5, _6, _7, _8, x, ...) x - -#define __count_args (...) \ - ___count_args(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0) -#define __constraint_read_0 "r" (arg0)#define __constraint_read_1 __constraint_read_0, "r" (arg1)#define __constraint_read_2 __constraint_read_1, "r" (arg2)
@@ -475,14 +471,6 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,__declare_arg_6 (a0, a1, a2, a3, a4, a5, a6, res); \ register typeof(a7) arg7 asm("r7") = __a7 -#define ___declare_args (count, ...) __declare_arg_ ## count(__VA_ARGS__) -#define __declare_args (count, ...) ___declare_args(count, __VA_ARGS__) - -#define ___constraints (count) \ - : __constraint_read_ ## count \ - : smccc_sve_clobbers "memory" -#define __constraints (count) ___constraints(count) -/* * We have an output list that is not necessarily used, and GCC feels * entitled to optimise the whole sequence away. "volatile" is what
@@ -494,11 +482,13 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1, registerunsigned long r1 asm("r1" ); \ registerunsigned long r2 asm("r2" ); \ registerunsigned long r3 asm("r3" ); \ -__declare_args (__count_args (__VA_ARGS__), __VA_ARGS__); \ +CONCATENATE (__declare_arg_,COUNT_ARGS (__VA_ARGS__)); \ asm volatile(SMCCC_SVE_CHECK \ inst "\n" : \ "=r" (r0), "=r" (r1), "=r" (r2), "=r" (r3) \ - __constraints(__count_args(__VA_ARGS__))); \ + : CONCATENATE(__constraint_read_, \ + COUNT_ARGS(__VA_ARGS__)) \ + : smccc_sve_clobbers "memory"); \if (___res) \ *___res = (typeof(*___res)){r0, r1, r2, r3}; \ }while (0 )
@@ -542,8 +532,11 @@ asmlinkage void __arm_smccc_hvc(unsigned long a0, unsigned long a1,*/ #define __fail_smccc_1_1 (...) \ do { \ - __declare_args(__count_args(__VA_ARGS__), __VA_ARGS__); \ - asm ("" : __constraints(__count_args(__VA_ARGS__))); \ + CONCATENATE(__declare_arg_, COUNT_ARGS(__VA_ARGS__)); \ + asm ("" : \ + : CONCATENATE(__constraint_read_, \ + COUNT_ARGS(__VA_ARGS__)) \ + : smccc_sve_clobbers "memory"); \ if (___res) \ ___res->a0 = SMCCC_RET_NOT_SUPPORTED; \ } while (0)
include/linux/genl_magic_struct.h +3 -5
@@ -14,14 +14,12 @@# error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"#endif +#include <linux/args.h> #include <linux/genetlink.h> #include <linux/types.h> -#define CONCAT__ (a,b) a ## b -#define CONCAT_ (a,b) CONCAT__(a,b) - -extern int CONCAT_ (GENL_MAGIC_FAMILY ,_genl_register )(void ); -extern void CONCAT_ (GENL_MAGIC_FAMILY ,_genl_unregister )(void ); +extern int CONCATENATE (GENL_MAGIC_FAMILY ,_genl_register )(void ); +extern void CONCATENATE (GENL_MAGIC_FAMILY ,_genl_unregister )(void );/* * Extension of genl attribute validation policies {{{2
include/linux/kernel.h +0 -7
@@ -419,13 +419,6 @@ ftrace_vprintk(const char *fmt, va_list ap)static inline void ftrace_dump (enum ftrace_dump_mode oops_dump_mode) { }#endif /* CONFIG_TRACING */ -/* This counts to 12. Any more, it will return 13th argument. */ -#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n -#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) - -#define __CONCAT (a, b) a ## b -#define CONCATENATE (a, b) __CONCAT(a, b) -/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ #ifdef CONFIG_FTRACE_MCOUNT_RECORD # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
include/linux/pci.h +1 -1
@@ -23,7 +23,7 @@#ifndef LINUX_PCI_H #define LINUX_PCI_H - +#include <linux/args.h> #include <linux/mod_devicetable.h> #include <linux/types.h>
include/trace/bpf_probe.h +2 -0
@@ -12,6 +12,8 @@#undef __perf_task#define __perf_task (t) (t) +#include <linux/args.h> +/* cast any integer, pointer, or small struct to u64 */ #define UINTTYPE (size) \ __typeof__(__builtin_choose_expr(size == 1, (u8)1, \
Signature · 1 line
2.40.0.1.gaa8946217a0b
Quoted 11 lines · On Fri, 14 Jul 2023 17:22:37 +0300 Andy Shevchenko…
kernel.his being used as a dump for all kinds of stuff for a long time. TheCOUNT_ARGS()andCONCATENATE()macros may be used in some places without need of the fullkernel.hdependency train with it.Here is the attempt on cleaning it up by splitting out these
macros().While at it, include new header where it's being used and drop custom implementation of these macros and document how it works.
Signed-off-by Andy Shevchenko <[email protected]>---
-- Steve
Quoted 8 lines · On Fri, 14 Jul 2023 17:22:37 +0300 Andy Shevchenko…
kernel.his being used as a dump for all kinds of stuff for a long time. TheCOUNT_ARGS()andCONCATENATE()macros may be used in some places without need of the fullkernel.hdependency train with it.Here is the attempt on cleaning it up by splitting out these
macros().While at it, include new header where it's being used and drop custom implementation of these macros and document how it works.
This blows up my x86_64 allmodconfig build, starting with
In file included from drivers/block/drbd/drbd_nl.c:78:./include/linux/genl_magic_func.h:26:26: error: 'CONCAT_' declared as function returning an array
26 | static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = {
| ^~~~~~~./include/linux/genl_magic_func.h:26:15: error: parameter names (without types) in function declaration [-Werror]
26 | static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = {
| ^~~~~~~~~~
./include/linux/genl_magic_func.h:26:15: error: function 'CONCAT_' is initialized like a variable
./include/linux/drbd_genl.h:88:13: error: array index in non-array initializer
88 | GENL_struct(DRBD_NLA_CFG_REPLY, 1, drbd_cfg_reply,
| ^~~~~~~~~~~~~~~~~~
./include/linux/genl_magic_func.h:24:10: note: in definition of macro 'GENL_struct'
24 | [tag_name] = { .type = NLA_NESTED },
| ^~~~~~~~...
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Fri, 14 Jul 2023 17:22:37 +0300 Andy Shevchenko…
kernel.his being used as a dump for all kinds of stuff for a long time. TheCOUNT_ARGS()andCONCATENATE()macros may be used in some places without need of the fullkernel.hdependency train with it.
Quoted 6 lines
Quoted 4 lines
Here is the attempt on cleaning it up by splitting out these
macros().While at it, include new header where it's being used and drop custom implementation of these macros and document how it works.
This blows up my
x86_64allmodconfig build, starting with
Oh, don't know how I missed to grep for that. Thank you, now I'm reproducing this on my side. I think I will split this to a few patches in v3, so we can see better if anything happens.
Signature · 2 lines
With Best Regards, Andy Shevchenko