[PATCH 1/3] printk: add macros to simplify handling struct va_format

Andrzej Hajda
2025-11-26T11:37:33Z

struct va_format is used to facilitate implementation of
variadic functions. Most of variadic users do not parse
arguments one by one, they just forward them to some
helper via va_format pointer. Introduced helpers simplifies
it more.

Signed-off-by: Andrzej Hajda <andrzej.hajda@intel.com>
---
include/linux/printk.h | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/include/linux/printk.h b/include/linux/printk.h
index 45c663124c9bd3b294031d839f1253f410313faa..6fd817ce29a66b33a804b660c2b770efa110ee7e 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -87,6 +87,36 @@ struct va_format {
va_list *va;
};

+/* Special macro used only by callers of va_format_call to position va_format argument */
+#define va_format_arg (&vaf)
+
+/**
+ * va_format_call() - forward variable arguments to va_format aware function
+ * @_fmt: format used by @_func function
+ * @_func: function which should be called
+ * @_args: arguments passed to @_func. Exactly one of them must be va_format_arg,
+ * to indicate position of *va_format type arg in arguments of @_func.
+ *
+ * Many variadic functions just forwards their variadic arguments to some helper
+ * function via va_format pointer. It involves few common steps encapsulated in
+ * this macro. The macro is accompanied by va_format_arg macro, described above.
+ * Sample implementation of common helper:
+ * void dev_err(struct device *dev, const char* fmt, ...)
+ * {
+ * va_format_call(fmt, pr_err, "%s %s: %pV", dev_driver_string(dev),
+ * dev_name(dev), va_format_arg);
+ * }
+ */
+#define va_format_call(_fmt, _func, _args...) \
+({ \
+ va_list ap; \
+ struct va_format vaf = { .fmt = _fmt, .va = &ap }; \
+ \
+ va_start(ap, _fmt); \
+ _func(_args); \
+ va_end(ap); \
+})
+
/*
* FW_BUG
* Add this to a message where you are sure the firmware is buggy or behaves

--
2.43.0