mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-05-14 21:38:46 +02:00
473e470f16
The sunrpc change to use trace_printk() for debugging caused
a new warning for every instance of dprintk() in some configurations,
when -Wformat-security is enabled:
fs/nfs/getroot.c: In function 'nfs_get_root':
fs/nfs/getroot.c:90:17: error: format not a string literal and no format arguments [-Werror=format-security]
90 | nfs_errorf(fc, "NFS: Couldn't getattr on root");
I've been slowly chipping away at those warnings over time with the
intention of enabling them by default in the future. While I could not
figure out why this only happens for this one instance, I see that the
__trace_bprintk() function is always called with a local variable as
the format string, rather than a literal.
Move the __printf(2,3) annotation on this function from the declaration
to the caller. As this is can only be validated for literals, the
attribute on the declaration causes the warnings every time, but
removing it entirely introduces a new warning on the __ftrace_vbprintk()
definition.
The format strings still get checked because the underlying literal keeps
getting passed into __trace_printk() in the "else" branch, which is not
taken but still evaluated for compile-time warnings.
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Anna Schumaker <anna.schumaker@oracle.com>
Cc: Chuck Lever <chuck.lever@oracle.com>
Cc: Simon Horman <horms@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Yury Norov <ynorov@nvidia.com>
Cc: Randy Dunlap <rdunlap@infradead.org>
Link: https://patch.msgid.link/20260203164545.3174910-1-arnd@kernel.org
Fixes: ec7d8e68ef ("sunrpc: add a Kconfig option to redirect dfprintk() output to trace buffer")
Acked-by: Jeff Layton <jlayton@kernel.org>
Acked-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
204 lines
6.7 KiB
C
204 lines
6.7 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _LINUX_TRACE_PRINTK_H
|
|
#define _LINUX_TRACE_PRINTK_H
|
|
|
|
#include <linux/compiler_attributes.h>
|
|
#include <linux/instruction_pointer.h>
|
|
#include <linux/stddef.h>
|
|
#include <linux/stringify.h>
|
|
|
|
/*
|
|
* General tracing related utility functions - trace_printk(),
|
|
* tracing_on/tracing_off and tracing_start()/tracing_stop
|
|
*
|
|
* Use tracing_on/tracing_off when you want to quickly turn on or off
|
|
* tracing. It simply enables or disables the recording of the trace events.
|
|
* This also corresponds to the user space /sys/kernel/tracing/tracing_on
|
|
* file, which gives a means for the kernel and userspace to interact.
|
|
* Place a tracing_off() in the kernel where you want tracing to end.
|
|
* From user space, examine the trace, and then echo 1 > tracing_on
|
|
* to continue tracing.
|
|
*
|
|
* tracing_stop/tracing_start has slightly more overhead. It is used
|
|
* by things like suspend to ram where disabling the recording of the
|
|
* trace is not enough, but tracing must actually stop because things
|
|
* like calling smp_processor_id() may crash the system.
|
|
*
|
|
* Most likely, you want to use tracing_on/tracing_off.
|
|
*/
|
|
|
|
enum ftrace_dump_mode {
|
|
DUMP_NONE,
|
|
DUMP_ALL,
|
|
DUMP_ORIG,
|
|
DUMP_PARAM,
|
|
};
|
|
|
|
#ifdef CONFIG_TRACING
|
|
void tracing_on(void);
|
|
void tracing_off(void);
|
|
int tracing_is_on(void);
|
|
void tracing_snapshot(void);
|
|
void tracing_snapshot_alloc(void);
|
|
|
|
extern void tracing_start(void);
|
|
extern void tracing_stop(void);
|
|
|
|
static inline __printf(1, 2)
|
|
void ____trace_printk_check_format(const char *fmt, ...)
|
|
{
|
|
}
|
|
#define __trace_printk_check_format(fmt, args...) \
|
|
do { \
|
|
if (0) \
|
|
____trace_printk_check_format(fmt, ##args); \
|
|
} while (0)
|
|
|
|
/**
|
|
* trace_printk - printf formatting in the ftrace buffer
|
|
* @fmt: the printf format for printing
|
|
*
|
|
* Note: __trace_printk is an internal function for trace_printk() and
|
|
* the @ip is passed in via the trace_printk() macro.
|
|
*
|
|
* This function allows a kernel developer to debug fast path sections
|
|
* that printk is not appropriate for. By scattering in various
|
|
* printk like tracing in the code, a developer can quickly see
|
|
* where problems are occurring.
|
|
*
|
|
* This is intended as a debugging tool for the developer only.
|
|
* Please refrain from leaving trace_printks scattered around in
|
|
* your code. (Extra memory is used for special buffers that are
|
|
* allocated when trace_printk() is used.)
|
|
*
|
|
* A little optimization trick is done here. If there's only one
|
|
* argument, there's no need to scan the string for printf formats.
|
|
* The trace_puts() will suffice. But how can we take advantage of
|
|
* using trace_puts() when trace_printk() has only one argument?
|
|
* By stringifying the args and checking the size we can tell
|
|
* whether or not there are args. __stringify((__VA_ARGS__)) will
|
|
* turn into "()\0" with a size of 3 when there are no args, anything
|
|
* else will be bigger. All we need to do is define a string to this,
|
|
* and then take its size and compare to 3. If it's bigger, use
|
|
* do_trace_printk() otherwise, optimize it to trace_puts(). Then just
|
|
* let gcc optimize the rest.
|
|
*/
|
|
|
|
#define trace_printk(fmt, ...) \
|
|
do { \
|
|
char _______STR[] = __stringify((__VA_ARGS__)); \
|
|
if (sizeof(_______STR) > 3) \
|
|
do_trace_printk(fmt, ##__VA_ARGS__); \
|
|
else \
|
|
trace_puts(fmt); \
|
|
} while (0)
|
|
|
|
#define do_trace_printk(fmt, args...) \
|
|
do { \
|
|
static const char *trace_printk_fmt __used \
|
|
__section("__trace_printk_fmt") = \
|
|
__builtin_constant_p(fmt) ? fmt : NULL; \
|
|
\
|
|
__trace_printk_check_format(fmt, ##args); \
|
|
\
|
|
if (__builtin_constant_p(fmt)) \
|
|
__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
|
|
else \
|
|
__trace_printk(_THIS_IP_, fmt, ##args); \
|
|
} while (0)
|
|
|
|
int __trace_bprintk(unsigned long ip, const char *fmt, ...);
|
|
|
|
extern __printf(2, 3)
|
|
int __trace_printk(unsigned long ip, const char *fmt, ...);
|
|
|
|
/**
|
|
* trace_puts - write a string into the ftrace buffer
|
|
* @str: the string to record
|
|
*
|
|
* Note: __trace_bputs is an internal function for trace_puts and
|
|
* the @ip is passed in via the trace_puts macro.
|
|
*
|
|
* This is similar to trace_printk() but is made for those really fast
|
|
* paths that a developer wants the least amount of "Heisenbug" effects,
|
|
* where the processing of the print format is still too much.
|
|
*
|
|
* This function allows a kernel developer to debug fast path sections
|
|
* that printk is not appropriate for. By scattering in various
|
|
* printk like tracing in the code, a developer can quickly see
|
|
* where problems are occurring.
|
|
*
|
|
* This is intended as a debugging tool for the developer only.
|
|
* Please refrain from leaving trace_puts scattered around in
|
|
* your code. (Extra memory is used for special buffers that are
|
|
* allocated when trace_puts() is used.)
|
|
*
|
|
* Returns: 0 if nothing was written, positive # if string was.
|
|
* (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
|
|
*/
|
|
|
|
#define trace_puts(str) ({ \
|
|
static const char *trace_printk_fmt __used \
|
|
__section("__trace_printk_fmt") = \
|
|
__builtin_constant_p(str) ? str : NULL; \
|
|
\
|
|
if (__builtin_constant_p(str)) \
|
|
__trace_bputs(_THIS_IP_, trace_printk_fmt); \
|
|
else \
|
|
__trace_puts(_THIS_IP_, str); \
|
|
})
|
|
extern int __trace_bputs(unsigned long ip, const char *str);
|
|
extern int __trace_puts(unsigned long ip, const char *str);
|
|
|
|
extern void trace_dump_stack(int skip);
|
|
|
|
/*
|
|
* The double __builtin_constant_p is because gcc will give us an error
|
|
* if we try to allocate the static variable to fmt if it is not a
|
|
* constant. Even with the outer if statement.
|
|
*/
|
|
#define ftrace_vprintk(fmt, vargs) \
|
|
do { \
|
|
if (__builtin_constant_p(fmt)) { \
|
|
static const char *trace_printk_fmt __used \
|
|
__section("__trace_printk_fmt") = \
|
|
__builtin_constant_p(fmt) ? fmt : NULL; \
|
|
\
|
|
__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
|
|
} else \
|
|
__ftrace_vprintk(_THIS_IP_, fmt, vargs); \
|
|
} while (0)
|
|
|
|
extern __printf(2, 0) int
|
|
__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
|
|
|
|
extern __printf(2, 0) int
|
|
__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
|
|
|
|
extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
|
|
#else
|
|
static inline void tracing_start(void) { }
|
|
static inline void tracing_stop(void) { }
|
|
static inline void trace_dump_stack(int skip) { }
|
|
|
|
static inline void tracing_on(void) { }
|
|
static inline void tracing_off(void) { }
|
|
static inline int tracing_is_on(void) { return 0; }
|
|
static inline void tracing_snapshot(void) { }
|
|
static inline void tracing_snapshot_alloc(void) { }
|
|
|
|
static inline __printf(1, 2)
|
|
int trace_printk(const char *fmt, ...)
|
|
{
|
|
return 0;
|
|
}
|
|
static __printf(1, 0) inline int
|
|
ftrace_vprintk(const char *fmt, va_list ap)
|
|
{
|
|
return 0;
|
|
}
|
|
static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
|
|
#endif /* CONFIG_TRACING */
|
|
|
|
#endif
|