vsprintf: Add __printf attribute to where it's required
Subsystems (4)
Andy Shevchenko
reviewerv2 cover vsprintf: Add __printf attribute to where it's required
This whole series started from a simple fix (see the last patch) to make GCC (Debian 14.2.0-17) happy when compiling with make W=1 (note, that CONFIG_WERROR=y and all warnings break the build!) down to a rabbit hole.
However starting from v2 the last patch doesn't require the first part, I prefer still to have them since the functions, while being _binary_ printf()-like, are still printf()-like. It also puts in align the tracing stuff with the rest and fixes the wrong parameter value.
These first 4 patches are organised in a strict order and can't be reshuffled, otherwise it will produce a warnings in between.
I believe the best route for the series is printk tree with immutable tag or branch for the others.
Alternatively the first 4 patches can be applied first as they are pretty much straightforward. They also can be squashed to one (as the same topic behind), but it all is up to the respective maintainers.
In v2:
- split out patch 5 (Rasmus)
- rewritten the approach for the va_format() fix (Rasmus)
- amended tracing patch (removed a blank line and a __printf() in C file)
Andy Shevchenko (6):
seq_buf: Mark binary printing functions with __printf() attribute
seq_file: Mark binary printing functions with __printf() attribute
tracing: Mark binary printing functions with __printf() attribute
vsnprintf: Mark binary printing functions with __printf() attribute
vsnprintf: Drop unused const char fmt * in va_format()
vsnprintf: Silence false positive GCC warning for va_format()
include/linux/seq_buf.h | 4 ++-- include/linux/seq_file.h | 1 + include/linux/string.h | 4 ++-- include/linux/trace.h | 4 ++-- include/linux/trace_seq.h | 8 ++++---- kernel/trace/trace.c | 11 +++-------- kernel/trace/trace.h | 16 +++++++++------- lib/vsprintf.c | 9 +++++++-- 8 files changed, 30 insertions(+), 27 deletions(-)
Signature · 1 line
2.47.2
Andy Shevchenko
reviewerPATCH v2 1/6 seq_buf: Mark binary printing functions with __printf() attribute
Binary printing functions are using printf() type of format, and compiler is not happy about them as is:
lib/seq_buf.c:162:17: error: function ‘seq_buf_bprintf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]
Fix the compilation errors by adding __printf() attribute.
--- include/linux/seq_buf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/seq_buf.h +2 -2
@@ -167,8 +167,8 @@ extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, const void *buf, size_t len, bool ascii);#ifdef CONFIG_BINARY_PRINTF -extern int -seq_buf_bprintf (struct seq_buf *s,const char *fmt,const u32 *binary); +__printf(2, 0) +int seq_buf_bprintf (struct seq_buf *s,const char *fmt,const u32 *binary);#endif void seq_buf_do_printk (struct seq_buf *s,const char *lvl);
Signature · 1 line
2.47.2
Andy Shevchenko
reviewerPATCH v2 2/6 seq_file: Mark binary printing functions with __printf() attribute
In reply to Andy ShevchenkoBinary printing functions are using printf() type of format, and compiler is not happy about them as is:
fs/seq_file.c:418:35: error: function ‘seq_bprintf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]
Fix the compilation errors by adding __printf() attribute.
--- include/linux/seq_file.h | 1 + 1 file changed, 1 insertion(+)
include/linux/seq_file.h +1 -0
@@ -181,6 +181,7 @@ int seq_open_private(struct file *, const struct seq_operations *, int);int seq_release_private (struct inode *,struct file *);#ifdef CONFIG_BINARY_PRINTF +__printf(2, 0)void seq_bprintf (struct seq_file *m,const char *f,const u32 *binary);#endif
Signature · 1 line
2.47.2
Andy Shevchenko
reviewerPATCH v2 3/6 tracing: Mark binary printing functions with __printf() attribute
In reply to Andy ShevchenkoBinary printing functions are using printf() type of format, and compiler is not happy about them as is:
kernel/trace/trace.c:3292:9: error: function ‘trace_vbprintk’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format] kernel/trace/trace_seq.c:182:9: error: function ‘trace_seq_bprintf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]
Fix the compilation errors by adding __printf() attribute.
While at it, move existing __printf() attributes from the implementations to the declarations. IT also fixes incorrect attribute parameters that are used for trace_array_printk().
--- include/linux/trace.h | 4 ++-- include/linux/trace_seq.h | 8 ++++---- kernel/trace/trace.c | 11 +++-------- kernel/trace/trace.h | 16 +++++++++------- 4 files changed, 18 insertions(+), 21 deletions(-)
include/linux/trace.h +2 -2
@@ -72,8 +72,8 @@ static inline int unregister_ftrace_export(struct trace_export *export)static inline void trace_printk_init_buffers (void ) { } -static inline int trace_array_printk (struct trace_array *tr,unsigned long ip, -const char *fmt, ...) +static inline __printf(3, 4) +int trace_array_printk(struct trace_array *tr, unsigned long ip, const char *fmt, ...) {return 0 ; }
include/linux/trace_seq.h +4 -4
@@ -88,8 +88,8 @@ extern __printf(2, 3)void trace_seq_printf (struct trace_seq *s,const char *fmt, ...); extern __printf(2, 0) void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args); -extern void -trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary); +extern __printf(2, 0) +void trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary); extern int trace_print_seq(struct seq_file *m, struct trace_seq *s); extern int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt);
@@ -113,8 +113,8 @@ static inline __printf(2, 3)void trace_seq_printf (struct trace_seq *s,const char *fmt, ...) { } -static inline void -trace_seq_bprintf (struct trace_seq *s,const char *fmt,const u32 *binary) +static inline __printf(2, 0) +void trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary) { }
kernel/trace/trace.c +3 -8
@@ -3322,10 +3322,9 @@ int trace_vbprintk(unsigned long ip, const char *fmt, va_list args) }EXPORT_SYMBOL_GPL (trace_vbprintk); -__printf(3, 0) -static int -__trace_array_vprintk (struct trace_buffer *buffer, -unsigned long ip,const char *fmt,va_list args) +static __printf(3, 0) +int __trace_array_vprintk(struct trace_buffer *buffer, + unsigned long ip, const char *fmt, va_list args) { struct ring_buffer_event *event;int len =0 , size;
@@ -3375,7 +3374,6 @@ __trace_array_vprintk(struct trace_buffer *buffer,return len; } -__printf(3, 0)int trace_array_vprintk (struct trace_array *tr,unsigned long ip,const char *fmt,va_list args) {
@@ -3405,7 +3403,6 @@ int trace_array_vprintk(struct trace_array *tr,* Note, trace_array_init_printk() must be called on @tr before this * can be used. */ -__printf(3, 0)int trace_array_printk (struct trace_array *tr,unsigned long ip,const char *fmt, ...) {
@@ -3450,7 +3447,6 @@ int trace_array_init_printk(struct trace_array *tr) }EXPORT_SYMBOL_GPL (trace_array_init_printk); -__printf(3, 4)int trace_array_printk_buf (struct trace_buffer *buffer,unsigned long ip,const char *fmt, ...) {
@@ -3466,7 +3462,6 @@ int trace_array_printk_buf(struct trace_buffer *buffer,return ret; } -__printf(2, 0)int trace_vprintk (unsigned long ip,const char *fmt,va_list args) {return trace_array_vprintk (printk_trace, ip, fmt, args);
kernel/trace/trace.h +9 -7
@@ -836,13 +836,15 @@ static inline void __init disable_tracing_selftest(const char *reason)extern void *head_page (struct trace_array_cpu *data);extern unsigned long long ns2usecs (u64 nsec); -extern int -trace_vbprintk (unsigned long ip,const char *fmt,va_list args); -extern int -trace_vprintk (unsigned long ip,const char *fmt,va_list args); -extern int -trace_array_vprintk (struct trace_array *tr, -unsigned long ip,const char *fmt,va_list args); + +__printf(2, 0) +int trace_vbprintk (unsigned long ip,const char *fmt,va_list args); +__printf(2, 0) +int trace_vprintk (unsigned long ip,const char *fmt,va_list args); +__printf(3, 0) +int trace_array_vprintk (struct trace_array *tr, +unsigned long ip,const char *fmt,va_list args); +__printf(3, 4)int trace_array_printk_buf (struct trace_buffer *buffer,unsigned long ip,const char *fmt, ...);void trace_printk_seq (struct trace_seq *s);
Signature · 1 line
2.47.2
Andy Shevchenko
reviewerPATCH v2 4/6 vsnprintf: Mark binary printing functions with __printf() attribute
In reply to Andy ShevchenkoBinary printf() functions are using printf() type of format, and compiler is not happy about them as is:
lib/vsprintf.c:3130:47: error: function ‘vbin_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format] lib/vsprintf.c:3298:33: error: function ‘bstr_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]
Fix the compilation errors by adding __printf() attribute.
--- include/linux/string.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/string.h +2 -2
@@ -336,8 +336,8 @@ int __sysfs_match_string(const char * const *array, size_t n, const char *s);#define sysfs_match_string (_a, _s) __sysfs_match_string(_a, ARRAY_SIZE(_a), _s)#ifdef CONFIG_BINARY_PRINTF -int vbin_printf (u32 *bin_buf,size_t size,const char *fmt,va_list args); -int bstr_printf (char *buf,size_t size,const char *fmt,const u32 *bin_buf); +__printf(3, 0) int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args); +__printf(3, 0) int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);#endif extern ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
Signature · 1 line
2.47.2
Steven Rostedt
maintainerRe: [PATCH v2 4/6] vsnprintf: Mark binary printing functions with __printf() attribute
Quoted 5 lines · On Fri, 21 Mar 2025 16:40:50 +0200 Andy Shevchenko…
Binary
printf()functions are usingprintf()type of format, and compiler is not happy about them as is:
lib/vsprintf.c:3130:47: error: function ‘vbin_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]lib/vsprintf.c:3298:33: error: function ‘bstr_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]
BTW, I find it disturbing that the compiler is set to "error" on a warning that "might be a candidate". What happens if it is not? We have to play games to quiet it.
Adding __printf() attributes to stubs seems to be a case of the compiler causing more problems than its worth :-/
I honestly hate this error on warning because it causes real pain when debugging. There's a lot of times I don't know if the value is long or long long, and when I get it wrong, my printk() causes the build to fail. It's especially annoying when both long and long long are the same size!
Fixing theses stupid errors takes a non trivial amount of time away from actual debugging.
-- Steve
Quoted 4 lines
Fix the compilation errors by adding
__printf()attribute.Signed-off-by Andy Shevchenko <[email protected]>
Andy Shevchenko
reviewerRe: [PATCH v2 4/6] vsnprintf: Mark binary printing functions with __printf() attribute
Quoted 13 lines · Steven Rostedt
Quoted 5 lines · On Fri, 21 Mar 2025 16:40:50 +0200 Andy Shevchenko…
Binary
printf()functions are usingprintf()type of format, and compiler is not happy about them as is:
lib/vsprintf.c:3130:47: error: function ‘vbin_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]lib/vsprintf.c:3298:33: error: function ‘bstr_printf’ might be a candidate for ‘gnu_printf’ format attribute [-Werror=suggest-attribute=format]BTW, I find it disturbing that the compiler is set to "error" on a warning that "might be a candidate". What happens if it is not? We have to play games to quiet it.
Quoted 5 lines
Adding
__printf()attributes to stubs seems to be a case of the compiler causing more problems than its worth :-/I honestly hate this error on warning because it causes real pain when debugging.
Tell it to Linus :-) since it was him who enabled that default. And since it's there and defconfigs are also part of the kernel I can't easy remove that, and TBH I even won't dare doing that.
Quoted 6 lines
There's a lot of times I don't know if the value is long or long long, and when I get it wrong, my
printk()causes the build to fail. It's especially annoying when both long and long long are the same size!Fixing theses stupid errors takes a non trivial amount of time away from actual debugging.
You (actually me) fix them once, currently CI's typically run with W=1, but with WERROR=n. Which means that the new code that is not fixed a priori, will induce the CI red report.
Quoted 3 lines
Quoted 3 lines
Fix the compilation errors by adding
__printf()attribute.Signed-off-by Andy Shevchenko <[email protected]>
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerPATCH v2 5/6 vsnprintf: Drop unused const char fmt * in va_format()
In reply to Andy Shevchenkova_format() doesn't use original formatting string, drop that argument as it's done elsewhere in similar cases.
--- lib/vsprintf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
lib/vsprintf.c +2 -2
@@ -1693,7 +1693,7 @@ char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, }static char *va_format (char *buf,char *end,struct va_format *va_fmt, -struct printf_spec spec,const char *fmt) +struct printf_spec spec) {va_list va;
@@ -2462,7 +2462,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,case 'U' :return uuid_string (buf, end, ptr, spec, fmt);case 'V' : -return va_format (buf, end, ptr, spec, fmt); +return va_format (buf, end, ptr, spec);case 'K' :return restricted_pointer (buf, end, ptr, spec);case 'N' :
Signature · 1 line
2.47.2
Andy Shevchenko
reviewerPATCH v2 6/6 vsnprintf: Silence false positive GCC warning for va_format()
In reply to Andy Shevchenkova_format() is using vsnprintf(), and GCC compiler (Debian 14.2.0-17) is not happy about this:
lib/vsprintf.c:1704:9: error: function ‘va_format’ might be a candidate for ‘gnu_print ’ format attribute [-Werror=suggest-attribute=format]
Fix the compilation errors (make W=1 when CONFIG_WERROR=y, which is default) by silencing the false positive GCC warning.
--- lib/vsprintf.c | 5 +++++ 1 file changed, 5 insertions(+)
lib/vsprintf.c +5 -0
@@ -1692,6 +1692,10 @@ char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,return buf; } +#pragma GCC diagnostic push +#ifndef __clang__ +#pragma GCC diagnostic ignored "-Wsuggest-attribute=format" +#endif static char *va_format (char *buf,char *end,struct va_format *va_fmt,struct printf_spec spec) {
@@ -1706,6 +1710,7 @@ static char *va_format(char *buf, char *end, struct va_format *va_fmt,return buf; } +#pragma GCC diagnostic popstatic noinline_for_stack char *uuid_string(char *buf, char *end, const u8 *addr,
Signature · 1 line
2.47.2
Quoted 13 lines · Andy Shevchenko
This whole series started from a simple fix (see the last patch) to make GCC (Debian 14.2.0-17) happy when compiling with
make W=1(note, thatCONFIG_WERROR=yand all warnings break the build!) down to a rabbit hole.However starting from v2 the last patch doesn't require the first part, I prefer still to have them since the functions, while being _binary_ printf()-like, are still printf()-like. It also puts in align the tracing stuff with the rest and fixes the wrong parameter value.
These first 4 patches are organised in a strict order and can't be reshuffled, otherwise it will produce a warnings in between.
Quoted 7 lines
I believe the best route for the series is printk tree with immutable tag or branch for the others.
Alternatively the first 4 patches can be applied first as they are pretty much straightforward. They also can be squashed to one (as the same topic behind), but it all is up to the respective maintainers.
The whole series looks good to me:
I am going to push it via the printk tree. I think about doing so as a second pull request by the end of this merge window.
Anyway, I am going to wait few more days for eventual feedback or push back.
Best Regards,
Petr
Petr Mladek
maintainerQuoted 24 lines · Petr Mladek
Quoted 13 lines · Andy Shevchenko
This whole series started from a simple fix (see the last patch) to make GCC (Debian 14.2.0-17) happy when compiling with
make W=1(note, thatCONFIG_WERROR=yand all warnings break the build!) down to a rabbit hole.However starting from v2 the last patch doesn't require the first part, I prefer still to have them since the functions, while being _binary_ printf()-like, are still printf()-like. It also puts in align the tracing stuff with the rest and fixes the wrong parameter value.
These first 4 patches are organised in a strict order and can't be reshuffled, otherwise it will produce a warnings in between.
Quoted 7 lines
I believe the best route for the series is printk tree with immutable tag or branch for the others.
Alternatively the first 4 patches can be applied first as they are pretty much straightforward. They also can be squashed to one (as the same topic behind), but it all is up to the respective maintainers.
The whole series looks good to me:
Quoted 7 lines
Reviewed-by Petr Mladek <[email protected]>I am going to push it via the printk tree. I think about doing so as a second pull request by the end of this merge window.
Anyway, I am going to wait few more days for eventual feedback or push back.
JFYI, I have pushed the patchset into printk/linux.git, branch for-6.15-printf-attribute.
I am going to send a pull request the following week if nothing happens in the meantime.
Best Regards,
Petr
Quoted 30 lines · Petr Mladek
Quoted 29 lines · Petr Mladek
Quoted 13 lines · Andy Shevchenko
This whole series started from a simple fix (see the last patch) to make GCC (Debian 14.2.0-17) happy when compiling with
make W=1(note, thatCONFIG_WERROR=yand all warnings break the build!) down to a rabbit hole.However starting from v2 the last patch doesn't require the first part, I prefer still to have them since the functions, while being _binary_ printf()-like, are still printf()-like. It also puts in align the tracing stuff with the rest and fixes the wrong parameter value.
These first 4 patches are organised in a strict order and can't be reshuffled, otherwise it will produce a warnings in between.
Quoted 7 lines
I believe the best route for the series is printk tree with immutable tag or branch for the others.
Alternatively the first 4 patches can be applied first as they are pretty much straightforward. They also can be squashed to one (as the same topic behind), but it all is up to the respective maintainers.
The whole series looks good to me:
Reviewed-by Petr Mladek <[email protected]>I am going to push it via the printk tree. I think about doing so as a second pull request by the end of this merge window.
Quoted 8 lines
Quoted 2 lines
Anyway, I am going to wait few more days for eventual feedback or push back.
JFYI, I have pushed the patchset into printk/linux.git, branch for-6.15-printf-attribute.
I am going to send a pull request the following week if nothing happens in the meantime.
Thank you!
It will make us much closer to the moment when we can enable WERROR=y in the CIs for make W=1 on x86 defconfigs (those that are in the kernel source tree).
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 34 lines · Andy Shevchenko
This whole series started from a simple fix (see the last patch) to make GCC (Debian 14.2.0-17) happy when compiling with
make W=1(note, thatCONFIG_WERROR=yand all warnings break the build!) down to a rabbit hole.However starting from v2 the last patch doesn't require the first part, I prefer still to have them since the functions, while being _binary_ printf()-like, are still printf()-like. It also puts in align the tracing stuff with the rest and fixes the wrong parameter value.
These first 4 patches are organised in a strict order and can't be reshuffled, otherwise it will produce a warnings in between.
I believe the best route for the series is printk tree with immutable tag or branch for the others.
Alternatively the first 4 patches can be applied first as they are pretty much straightforward. They also can be squashed to one (as the same topic behind), but it all is up to the respective maintainers.
In v2:
- split out patch 5 (Rasmus)
- rewritten the approach for theva_format()fix (Rasmus)
- amended tracing patch (removed a blank line and a__printf()in C file)Andy Shevchenko (6):
seq_buf: Mark binary printing functions with__printf()attribute
seq_file: Mark binary printing functions with__printf()attribute
tracing: Mark binary printing functions with__printf()attribute
vsnprintf: Mark binary printing functions with__printf()attribute
vsnprintf: Drop unused const char fmt * inva_format()
vsnprintf: Silence false positive GCC warning forva_format()
Quoted 9 lines
include/linux/seq_buf.h | 4 ++-- include/linux/seq_file.h | 1 + include/linux/string.h | 4 ++-- include/linux/trace.h | 4 ++-- include/linux/trace_seq.h | 8 ++++---- kernel/trace/trace.c | 11 +++-------- kernel/trace/trace.h | 16 +++++++++------- lib/vsprintf.c | 9 +++++++-- 8 files changed, 30 insertions(+), 27 deletions(-)
Cool; it'll be nice to get these marked up.
Signature · 1 line
Kees Cook