printk: add macros to simplify handling struct va_format
Subsystems (5)
v2 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 few patches to show the usage and the potential.
Provided helper deals with variadic args to va_format conversion/forwarding. Similar solution could be proposed also for variadic args to va_list conversion. Common code:
va_list args;
va_start(args, fmt);
ret = func(..., fmt, args);
va_end(args);
return ret;
Could be replaced with:
ret = va_list_call(fmt, func, ..., fmt);
This is less rewarding (less lines to remove) but still looks promising. Also
separate helper should be created for functions without return value,
unless somebody knows trick how to do it in one macro:
va_list_void_call(fmt, func, ..., fmt);Any comments?
---
Changes in v2:
- added two patches simplifying handling of variadic args in drivers/core and mm in slightly less obvious cases, this way both mm and drivers/core do not use anymore hand rolled variadic -> va_format conversions.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Andrzej Hajda (5):
printk: add macros to simplify handling struct va_format drivers/core: use va_format_call helper drivers/core: simplify variadic args handling
mm: use va_format_call helper
mm: simplify variadic args handling
drivers/base/core.c | 82 ++++++++------------------------------------------ include/linux/printk.h | 30 ++++++++++++++++++ mm/page_alloc.c | 14 +++------ mm/slub.c | 33 +++----------------- 4 files changed, 52 insertions(+), 107 deletions(-)
---
base-commit: 37a098cc87aafdf4de147851cbf350bb66c51b45
change-id: 20251126-va_format_call-41053ef60fd7
Best regards,
Signature · 1 line
Andrzej Hajda <[email protected]>
PATCH v2 1/5 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 v2 2/5 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 v2 3/5 drivers/core: simplify variadic args handling
In reply to Andrzej HajdaChanging argument type from va_list to struct va_format * allows to simplify variadic argument handling with va_format_call helper.
--- drivers/base/core.c | 50 +++++++------------------------------------------- 1 file changed, 7 insertions(+), 43 deletions(-)
drivers/base/core.c +7 -43
@@ -4965,30 +4965,12 @@ define_dev_printk_level(_dev_info, KERN_INFO);#endif static void __dev_probe_failed (const struct device *dev,int err,bool fatal, -const char *fmt,va_list vargsp) +const char *fmt,struct va_format *vaf) { -struct va_format vaf; -va_list vargs; - -/* -* On x86_64 and possibly on other architectures, va_list is actually a -* size-1 array containing a structure. As a result, function parameter -* vargsp decays from T[1] to T*, and &vargsp has type T** rather than -* T(*)[1], which is expected by its assignment to vaf.va below. -* -* One standard way to solve this mess is by creating a copy in a local -* variable of type va_list and then using a pointer to that local copy -* instead, which is the approach employed here. -*/ -va_copy (vargs, vargsp); - - vaf.fmt = fmt; - vaf.va = &vargs; -switch (err) {case -EPROBE_DEFER : -device_set_deferred_probe_reason (dev, &vaf); -dev_dbg (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +device_set_deferred_probe_reason (dev, vaf); +dev_dbg (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);break ;case -ENOMEM :
@@ -4998,13 +4980,11 @@ static void __dev_probe_failed(const struct device *dev, int err, bool fatal,default :/* Log fatal final failures as errors, otherwise produce warnings */ if (fatal) -dev_err (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +dev_err (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);else -dev_warn (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +dev_warn (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);break ; } - -va_end (vargs); }/**
@@ -5042,15 +5022,7 @@ static void __dev_probe_failed(const struct device *dev, int err, bool fatal,*/ int dev_err_probe (const struct device *dev,int err,const char *fmt, ...) { -va_list vargs; - -va_start (vargs, fmt); - -/* Use dev_err() for logging when err doesn't equal -EPROBE_DEFER */ -__dev_probe_failed (dev, err, true, fmt, vargs); - -va_end (vargs); - +va_format_call (fmt, __dev_probe_failed, dev, err, true, fmt, va_format_arg);return err; }EXPORT_SYMBOL_GPL (dev_err_probe);
@@ -5090,15 +5062,7 @@ EXPORT_SYMBOL_GPL(dev_err_probe);*/ int dev_warn_probe (const struct device *dev,int err,const char *fmt, ...) { -va_list vargs; - -va_start (vargs, fmt); - -/* Use dev_warn() for logging when err doesn't equal -EPROBE_DEFER */ -__dev_probe_failed (dev, err, false, fmt, vargs); - -va_end (vargs); - +va_format_call (fmt, __dev_probe_failed, dev, err, false, fmt, va_format_arg);return err; }EXPORT_SYMBOL_GPL (dev_warn_probe);
Signature · 1 line
2.43.0
Petr Mladek
maintainerRe: [PATCH v2 3/5] drivers/core: simplify variadic args handling
I am adding Andy and Rasmus into Cc who are active vsprintf-related code reviewers...
You might see the entire patchset at
https://lore.kernel.org/all/[email protected]/
Quoted 21 lines · Andrzej Hajda
Changing argument type from
va_listto structva_format* allows to simplify variadic argument handling withva_format_callhelper.drivers/base/core.c +1
-8@@ -4965,30 +4965,12 @@ define_dev_printk_level(_dev_info, KERN_INFO);#endif static void __dev_probe_failed (const struct device *dev,int err,bool fatal, -const char *fmt,va_list vargsp) +const char *fmt,struct va_format *vaf) { -struct va_format vaf; -va_list vargs; - -/* -* On x86_64 and possibly on other architectures, va_list is actually a -* size-1 array containing a structure. As a result, function parameter -* vargsp decays from T[1] to T*, and &vargsp has type T** rather than
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4986
Quoted 10 lines
drivers/base/core.c +0
-10-* T(*)[1], which is expected by its assignment to vaf.va below. -* -* One standard way to solve this mess is by creating a copy in a local -* variable of type va_list and then using a pointer to that local copy -* instead, which is the approach employed here. -*/ -va_copy (vargs, vargsp); - - vaf.fmt = fmt; - vaf.va = &vargs;
I am always a bit lost when using this API. Why is it safe to remove the va_copy() here, please?
The va_format_call() uses va_start()/va_end() which is replacing these calls in dev_err_probe() and dev_warn_probe().
It is possible that the original code was actually wrong because it uses the same copy (&vaf) everywhere, see below.
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4990
Quoted 3 lines
drivers/base/core.c +0
-1switch (err) {case -EPROBE_DEFER : -device_set_deferred_probe_reason (dev, &vaf);
This function processes the arguments via:
+ device_set_deferred_probe_reason()
+ kasprintf()
+ va_start()/va_end()
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4991
Quoted 1 line
drivers/base/core.c +0
-1-dev_dbg (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf);
This function uses the already processed copy of the arguments. IMHO, it might print a garbage because of this. IMHO, it should use the original va_list() or might need its own copy.
Note that this call does not modify the va_list because it uses "%pV" and vsprintf() creates its own copy in this case, see va_format() in lib/vsprintf.c.
Quoted 6 lines
drivers/base/core.c +2
-0+device_set_deferred_probe_reason (dev, vaf); +dev_dbg (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);break ;case -ENOMEM :
drivers/base/core.c static void __dev_probe_failed(const struct device *dev, int err, bool fatal, · line 4985
Quoted 8 lines
drivers/base/core.c +2
-2@@ -4998,13 +4980,11 @@ static void __dev_probe_failed(const struct device *dev, int err, bool fatal,default :/* Log fatal final failures as errors, otherwise produce warnings */ if (fatal) -dev_err (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +dev_err (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);else -dev_warn (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +dev_warn (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);
This should be fine because of using "%pV".
Quoted 31 lines
drivers/base/core.c +1
-14break ; } - -va_end (vargs); }/** @@ -5042,15 +5022,7 @@ static void __dev_probe_failed(const struct device *dev, int err, bool fatal,*/ int dev_err_probe (const struct device *dev,int err,const char *fmt, ...) { -va_list vargs; - -va_start (vargs, fmt); - -/* Use dev_err() for logging when err doesn't equal -EPROBE_DEFER */ -__dev_probe_failed (dev, err, true, fmt, vargs); - -va_end (vargs); - +va_format_call (fmt, __dev_probe_failed, dev, err, true, fmt, va_format_arg);return err; }EXPORT_SYMBOL_GPL (dev_err_probe);@@ -5090,15 +5062,7 @@ EXPORT_SYMBOL_GPL(dev_err_probe);*/ int dev_warn_probe (const struct device *dev,int err,const char *fmt, ...) { -va_list vargs; - -va_start (vargs, fmt);
drivers/base/core.c EXPORT_SYMBOL_GPL(dev_err_probe); · line 5068
Quoted 10 lines
drivers/base/core.c +1
-6- -/* Use dev_warn() for logging when err doesn't equal -EPROBE_DEFER */ -__dev_probe_failed (dev, err, false, fmt, vargs); - -va_end (vargs); - +va_format_call (fmt, __dev_probe_failed, dev, err, false, fmt, va_format_arg);return err; }EXPORT_SYMBOL_GPL (dev_warn_probe);
Best Regards,
Petr
Re: [PATCH v2 3/5] drivers/core: simplify variadic args handling
W dniu 02.12.2025 o 16:51, Petr Mladek pisze:
Quoted 32 lines
I am adding Andy and Rasmus into Cc who are active vsprintf-related code reviewers...
You might see the entire patchset at
https://lore.kernel.org/all/[email protected]/Quoted 25 lines · Andrzej Hajda
Changing argument type from
va_listto structva_format* allows to simplify variadic argument handling withva_format_callhelper.drivers/base/core.c +1
-12@@ -4965,30 +4965,12 @@ define_dev_printk_level(_dev_info, KERN_INFO);#endif static void __dev_probe_failed (const struct device *dev,int err,bool fatal, -const char *fmt,va_list vargsp) +const char *fmt,struct va_format *vaf) { -struct va_format vaf; -va_list vargs; - -/* -* On x86_64 and possibly on other architectures, va_list is actually a -* size-1 array containing a structure. As a result, function parameter -* vargsp decays from T[1] to T*, and &vargsp has type T** rather than -* T(*)[1], which is expected by its assignment to vaf.va below. -* -* One standard way to solve this mess is by creating a copy in a local -* variable of type va_list and then using a pointer to that local copy
Quoted 8 lines
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4986
Quoted 6 lines
drivers/base/core.c +0
-6-* instead, which is the approach employed here. -*/ -va_copy (vargs, vargsp); - - vaf.fmt = fmt; - vaf.va = &vargs;I am always a bit lost when using this API. Why is it safe to remove the
va_copy()here, please?
Not very familiar with this workaround, just my thoughts about it.
It is just va_list is compiler's private implementation, which can be anything.
And if it happens to be T[1], it's type decays to T* if it is type of argument of the function.
So vargsp is in fact of type T*, and &vargs is of type T** and it does not point to va_list anymore.
So in short passing va_list to a function, which takes a pointer to the arg is problematic.
va_format_call DOES NOT pass va_list to a function, so it seems to be safe.
Quoted 6 lines
The
va_format_call()uses va_start()/va_end() which is replacing these calls indev_err_probe()anddev_warn_probe().It is possible that the original code was actually wrong because it uses the same copy (&vaf) everywhere, see below.
Quoted 8 lines
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4990
Quoted 3 lines
drivers/base/core.c +0
-1switch (err) {case -EPROBE_DEFER : -device_set_deferred_probe_reason (dev, &vaf);This function processes the arguments via:
+
device_set_deferred_probe_reason()
+kasprintf()
+ va_start()/va_end()
This va_start/va_end is for var_args of kasprintf, not for &vaf, I hope parsing %pV uses va_copy.
Regards
Andrzej
Quoted 69 lines
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4991
Quoted 2 lines
drivers/base/core.c +0
-1-dev_dbg (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf);This function uses the already processed copy of the arguments. IMHO, it might print a garbage because of this. IMHO, it should use the original
va_list()or might need its own copy.Note that this call does not modify the
va_listbecause it uses "%pV" andvsprintf()creates its own copy in this case, seeva_format()inlib/vsprintf.c.Quoted 6 lines
drivers/base/core.c +2
-0+device_set_deferred_probe_reason (dev, vaf); +dev_dbg (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);break ;case -ENOMEM :drivers/base/core.c static void __dev_probe_failed(const struct device *dev, int err, bool fatal, · line 4985
Quoted 8 lines
drivers/base/core.c +2
-2@@ -4998,13 +4980,11 @@ static void __dev_probe_failed(const struct device *dev, int err, bool fatal,default :/* Log fatal final failures as errors, otherwise produce warnings */ if (fatal) -dev_err (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +dev_err (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);else -dev_warn (dev,"error %pe: %pV" ,ERR_PTR (err), &vaf); +dev_warn (dev,"error %pe: %pV" ,ERR_PTR (err), vaf);This should be fine because of using "%pV".
Quoted 31 lines
drivers/base/core.c +1
-14break ; } - -va_end (vargs); }/** @@ -5042,15 +5022,7 @@ static void __dev_probe_failed(const struct device *dev, int err, bool fatal,*/ int dev_err_probe (const struct device *dev,int err,const char *fmt, ...) { -va_list vargs; - -va_start (vargs, fmt); - -/* Use dev_err() for logging when err doesn't equal -EPROBE_DEFER */ -__dev_probe_failed (dev, err, true, fmt, vargs); - -va_end (vargs); - +va_format_call (fmt, __dev_probe_failed, dev, err, true, fmt, va_format_arg);return err; }EXPORT_SYMBOL_GPL (dev_err_probe);@@ -5090,15 +5062,7 @@ EXPORT_SYMBOL_GPL(dev_err_probe);*/ int dev_warn_probe (const struct device *dev,int err,const char *fmt, ...) { -va_list vargs; - -va_start (vargs, fmt);drivers/base/core.c EXPORT_SYMBOL_GPL(dev_err_probe); · line 5068
Quoted 10 lines
drivers/base/core.c +1
-6- -/* Use dev_warn() for logging when err doesn't equal -EPROBE_DEFER */ -__dev_probe_failed (dev, err, false, fmt, vargs); - -va_end (vargs); - +va_format_call (fmt, __dev_probe_failed, dev, err, false, fmt, va_format_arg);return err; }EXPORT_SYMBOL_GPL (dev_warn_probe);Best Regards,
Petr
Re: [PATCH v2 3/5] drivers/core: simplify variadic args handling
Quoted 6 lines · Andrzej
W dniu 02.12.2025 o 16:51, Petr Mladek pisze:
Quoted 5 lines
I am adding Andy and Rasmus into Cc who are active vsprintf-related code reviewers...
You might see the entire patchset at
https://lore.kernel.org/all/[email protected]/
TBH, I don't like the result. There are two problems with readability:
1) macro well hides the actual low-level call, hard to parse from its parameters;
2) sometimes it has va_format_call(fmt, ..., fmt, ...) which is confusing.
Implementation is also doubtful (to me) as GCC extension. Can't it rather return an error code and use something like do { } while (0) inside? OTOH, may be this is not feasible in a clean way...
And what is the motivation? Just make less LoCs? I would really like to see at least vmlinux sizes, the reports that GCC _and_ clang are both happy with the compilation as of make W=1 of this on both 32- and 64-bit cases.
Does it solve any issue? Does it bring any consistency or standardisation here?
Quoted 1 line
Quoted 1 line · Andrzej Hajda
...
Quoted 26 lines
Quoted 9 lines
Quoted 9 lines
drivers/base/core.c +0
-9-/* -* On x86_64 and possibly on other architectures, va_list is actually a -* size-1 array containing a structure. As a result, function parameter -* vargsp decays from T[1] to T*, and &vargsp has type T** rather than -* T(*)[1], which is expected by its assignment to vaf.va below. -* -* One standard way to solve this mess is by creating a copy in a local -* variable of type va_list and then using a pointer to that local copy -* instead, which is the approach employed here. Quoted 8 lines
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4986
Quoted 5 lines
drivers/base/core.c +0
-5-*/ -va_copy (vargs, vargsp); - - vaf.fmt = fmt; - vaf.va = &vargs;I am always a bit lost when using this API. Why is it safe to remove the
va_copy()here, please?Not very familiar with this workaround, just my thoughts about it.
It is just
va_listis compiler's private implementation, which can be anything.And if it happens to be T[1], it's type decays to T* if it is type of argument of the function.
Quoted 7 lines
So vargsp is in fact of type T*, and &vargs is of type T** and it does not point to
va_listanymore.So in short passing
va_listto a function, which takes a pointer to the arg is problematic.
va_format_callDOES NOT passva_listto a function, so it seems to be safe.
I'm sorry, I can't be helpful here, as I am not well familiar with va_*() stuff. The idea is interesting, nevertheless, but see above.
Quoted 11 lines
Quoted 11 lines
The
va_format_call()uses va_start()/va_end() which is replacing these calls indev_err_probe()anddev_warn_probe().It is possible that the original code was actually wrong because it uses the same copy (&vaf) everywhere, see below.
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4990
Quoted 3 lines
switch (err) { case -EPROBE_DEFER:drivers/base/core.c +0
-1-device_set_deferred_probe_reason (dev, &vaf);This function processes the arguments via:
Quoted 6 lines
Quoted 3 lines
+
device_set_deferred_probe_reason()
+kasprintf()
+ va_start()/va_end()This va_start/va_end is for
var_argsof kasprintf, not for &vaf, I hope parsing %pV usesva_copy.
Yes, it does call va_copy().
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 3/5] drivers/core: simplify variadic args handling
W dniu 03.12.2025 o 15:47, Andy Shevchenko pisze:
Quoted 11 lines · Andrzej
Quoted 6 lines
W dniu 02.12.2025 o 16:51, Petr Mladek pisze:
Quoted 5 lines
I am adding Andy and Rasmus into Cc who are active vsprintf-related code reviewers...
You might see the entire patchset at
https://lore.kernel.org/all/[email protected]/TBH, I don't like the result. There are two problems with readability:
1) macro well hides the actual low-level call, hard to parse from its parameters;
I hope 'va_format_call' is a strong suggestion that sth is to be called.
Quoted 2 lines
2) sometimes it has
va_format_call(fmt, ..., fmt, ...)which is confusing.
Many functions sometimes takes the same argument twice.
1st argument is to indicate beginning of the '...' in caller function args list, quite natural.
2nd argument is function name, and the rest are it's arguments, quite natural.
Since C23 standard, we could omit the 1st argument [1], but this is potential future.
[1]: https://en.cppreference.com/w/c/variadic/va_start
Quoted 4 lines
Implementation is also doubtful (to me) as GCC extension. Can't it rather return an error code and use something like do { } while (0) inside? OTOH, may be this is not feasible in a clean way...
???, expression statement "({ ...})" is present even in macro samples in kernel coding-style [2].
[2] https://www.kernel.org/doc/html/latest/process/coding-style.html
Quoted 2 lines
And what is the motivation? Just make less LoCs?
Also, but first:
- encapsulate common repeatable pattern into one macro, with clear purpose - forward variable args to va_format aware function.
- simplify handling variable arguments: it is easier to write single line instead of ten, usually interleaved by other declarations and code.
Is it so hard to see it? Have you seen other patches - quite good examples.
Quoted 1 line
I would really like to see at least vmlinux sizes,
What for? This not about binary size optimisation.
Quoted 2 lines
the reports that GCC _and_ clang are both happy with
the compilation as ofmake W=1of this on both 32- and 64-bit cases.
I do not expect any problems here, but will check :)
Quoted 2 lines
Does it solve any issue? Does it bring any consistency or standardisation here?
No. Yes.
Regards
Andrzej
Quoted 57 lines
Quoted 2 lines
Quoted 2 lines · Andrzej Hajda
...
Quoted 24 lines
Quoted 8 lines
Quoted 8 lines
drivers/base/core.c +0
-8-/* -* On x86_64 and possibly on other architectures, va_list is actually a -* size-1 array containing a structure. As a result, function parameter -* vargsp decays from T[1] to T*, and &vargsp has type T** rather than -* T(*)[1], which is expected by its assignment to vaf.va below. -* -* One standard way to solve this mess is by creating a copy in a local -* variable of type va_list and then using a pointer to that local copy Quoted 8 lines
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4986
Quoted 6 lines
drivers/base/core.c +0
-6-* instead, which is the approach employed here. -*/ -va_copy (vargs, vargsp); - - vaf.fmt = fmt; - vaf.va = &vargs;I am always a bit lost when using this API. Why is it safe to remove the
va_copy()here, please?Not very familiar with this workaround, just my thoughts about it.
It is just
va_listis compiler's private implementation, which can be anything.And if it happens to be T[1], it's type decays to T* if it is type of argument of the function.
Quoted 7 lines
So vargsp is in fact of type T*, and &vargs is of type T** and it does not point to
va_listanymore.So in short passing
va_listto a function, which takes a pointer to the arg is problematic.
va_format_callDOES NOT passva_listto a function, so it seems to be safe.I'm sorry, I can't be helpful here, as I am not well familiar with va_*() stuff. The idea is interesting, nevertheless, but see above.
Quoted 11 lines
Quoted 11 lines
The
va_format_call()uses va_start()/va_end() which is replacing these calls indev_err_probe()anddev_warn_probe().It is possible that the original code was actually wrong because it uses the same copy (&vaf) everywhere, see below.
drivers/base/core.c define_dev_printk_level(_dev_info, KERN_INFO); · line 4990
Quoted 3 lines
switch (err) { case -EPROBE_DEFER:drivers/base/core.c +0
-1-device_set_deferred_probe_reason (dev, &vaf);This function processes the arguments via:
Quoted 5 lines
Quoted 3 lines
+
device_set_deferred_probe_reason()
+kasprintf()
+ va_start()/va_end()This va_start/va_end is for
var_argsof kasprintf, not for &vaf, I hope parsing %pV usesva_copy.Yes, it does call
va_copy().
PATCH v2 4/5 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
PATCH v2 5/5 mm: simplify variadic args handling
In reply to Andrzej HajdaChanging argument type from va_list to struct va_format * allows to simplify variadic argument handling with va_format_call helper.
--- mm/slub.c | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-)
mm/slub.c +4 -19
@@ -1135,27 +1135,16 @@ void skip_orig_size_check(struct kmem_cache *s, const void *object)set_orig_size (s, (void *)object, s->object_size); } -static void __slab_bug (struct kmem_cache *s,const char *fmt,va_list argsp) +static void __slab_bug (struct kmem_cache *s,const char *fmt,struct va_format *vaf) { -struct va_format vaf; -va_list args; - -va_copy (args, argsp); - vaf.fmt = fmt; - vaf.va = &args;pr_err ("=============================================================================\n" ); -pr_err ("BUG %s (%s): %pV\n" , s ? s->name :"<unknown>" ,print_tainted (), &vaf); +pr_err ("BUG %s (%s): %pV\n" , s ? s->name :"<unknown>" ,print_tainted (), vaf);pr_err ("-----------------------------------------------------------------------------\n\n" ); -va_end (args); }static void slab_bug (struct kmem_cache *s,const char *fmt, ...) { -va_list args; - -va_start (args, fmt); -__slab_bug (s, fmt, args); -va_end (args); +va_format_call (fmt, __slab_bug, s, fmt, va_format_arg); }__printf (2 ,3 )
@@ -1252,14 +1241,10 @@ static void __slab_err(struct slab *slab) static __printf(3, 4) void slab_err(struct kmem_cache *s, struct slab *slab,const char *fmt, ...) { - va_list args; - if (slab_add_kunit_errors()) return; -va_start (args, fmt); -__slab_bug (s, fmt, args); -va_end (args); +va_format_call (fmt, __slab_bug, s, fmt, va_format_arg);__slab_err (slab); }
Signature · 1 line
2.43.0