printk: add macros to simplify handling struct va_format
Subsystems (5)
cover printk: add macros to simplify handling struct va_format
This patchset contains patch abstracting out most common bits of va_format uses into small macro. The macro works as a forwarder of variadic args to va_format call.
Rough estimates based on dirty cocci script suggests about 180-230 users could be converted, allowing to remove about 1100-1300 lines of code. Of course, I do not plan to do it in mass. I just provide two patches to show the usage and potential.
---
Andrzej Hajda (3):
printk: add macros to simplify handling struct va_format drivers/core: use va_format_call helper
mm: use va_format_call helper
drivers/base/core.c | 32 ++++++-------------------------- include/linux/printk.h | 30 ++++++++++++++++++++++++++++++ mm/page_alloc.c | 14 ++++---------- mm/slub.c | 10 +--------- 4 files changed, 41 insertions(+), 45 deletions(-)
---
base-commit: 37a098cc87aafdf4de147851cbf350bb66c51b45
change-id: 20251126-va_format_call-41053ef60fd7
Best regards,
Signature · 1 line
Andrzej Hajda <[email protected]>
PATCH 1/3 printk: add macros to simplify handling struct va_format
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.
--- include/linux/printk.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
include/linux/printk.h +30 -0
@@ -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
Signature · 1 line
2.43.0
PATCH 2/3 drivers/core: use va_format_call helper
In reply to Andrzej HajdaIt simplifies the code and makes it more clear.
--- drivers/base/core.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-)
drivers/base/core.c +6 -26
@@ -4943,35 +4943,15 @@ static void __dev_printk(const char *level, const struct device *dev,void _dev_printk (const char *level,const struct device *dev,const char *fmt, ...) { -struct va_format vaf; -va_list args; - -va_start (args, fmt); - - vaf.fmt = fmt; - vaf.va = &args; - -__dev_printk (level, dev, &vaf); - -va_end (args); +va_format_call (fmt, __dev_printk, level, dev, va_format_arg); }EXPORT_SYMBOL (_dev_printk); -#define define_dev_printk_level (func, kern_level) \ -void func(const struct device *dev, const char *fmt, ...) \ -{ \ - struct va_format vaf; \ - va_list args; \ - \ - va_start(args, fmt); \ - \ - vaf.fmt = fmt; \ - vaf.va = &args; \ - \ - __dev_printk(kern_level, dev, &vaf); \ - \ - va_end(args); \ -} \ +#define define_dev_printk_level (func, kern_level) \ +void func(const struct device *dev, const char *fmt, ...) \ +{ \ + va_format_call(fmt, __dev_printk, kern_level, dev, va_format_arg); \ +} \ EXPORT_SYMBOL(func);define_dev_printk_level (_dev_emerg,KERN_EMERG );
Signature · 1 line
2.43.0
PATCH 3/3 mm: use va_format_call helper
In reply to Andrzej HajdaIt simplifies the code and makes it more clear.
--- mm/page_alloc.c | 14 ++++---------- mm/slub.c | 10 +--------- 2 files changed, 5 insertions(+), 19 deletions(-)
mm/page_alloc.c +4 -10
@@ -3940,22 +3940,16 @@ static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask)void warn_alloc (gfp_t gfp_mask,nodemask_t *nodemask,const char *fmt, ...) { -struct va_format vaf; -va_list args; static DEFINE_RATELIMIT_STATE(nopage_rs, 10*HZ, 1);if ((gfp_mask & __GFP_NOWARN) || !__ratelimit (&nopage_rs) || ((gfp_mask & __GFP_DMA) && !has_managed_dma ()))return ; - -va_start (args, fmt); - vaf.fmt = fmt; - vaf.va = &args; -pr_warn ("%s: %pV, mode:%#x(%pGg), nodemask=%*pbl" , - current->comm, &vaf, gfp_mask, &gfp_mask, -nodemask_pr_args (nodemask)); -va_end (args); +va_format_call (fmt, pr_warn, +"%s: %pV, mode:%#x(%pGg), nodemask=%*pbl" , + current->comm, va_format_arg, gfp_mask, &gfp_mask, +nodemask_pr_args (nodemask));cpuset_print_current_mems_allowed ();pr_cont ("\n" );
mm/slub.c +1 -9
@@ -1161,17 +1161,9 @@ static void slab_bug(struct kmem_cache *s, const char *fmt, ...) __printf(2, 3)static void slab_fix (struct kmem_cache *s,const char *fmt, ...) { -struct va_format vaf; -va_list args; -if (slab_add_kunit_errors ())return ; - -va_start (args, fmt); - vaf.fmt = fmt; - vaf.va = &args; -pr_err ("FIX %s: %pV\n" , s->name, &vaf); -va_end (args); +va_format_call (fmt, pr_err,"FIX %s: %pV\n" , s->name, va_format_arg); }static void print_trailer (struct kmem_cache *s,struct slab *slab,u8 *p)
Signature · 1 line
2.43.0