public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kernel/trace: Add TRACING_ALLOW_PRINTK config option
@ 2020-06-24  8:45 Nicolas Boichat
  2020-06-24 13:57 ` Jason Gunthorpe
                   ` (3 more replies)
  0 siblings, 4 replies; 21+ messages in thread
From: Nicolas Boichat @ 2020-06-24  8:45 UTC (permalink / raw)
  To: linux-kernel
  Cc: Steven Rostedt, Ingo Molnar, Andrew Morton, Kees Cook,
	Jason Gunthorpe, Daniel Vetter, Peter Zijlstra, Vinod Koul,
	Andy Shevchenko, Alexey Dobriyan, Tiezhu Yang, Nicolas Boichat,
	Thomas Gleixner, Guilherme G . Piccoli, Will Deacon, dianders,
	groeck

trace_printk is only meant as a debugging tool, and should never be
compiled into production code without source code changes, as
indicated by the warning that shows up on boot if any trace_printk
is called:
 **   NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE   **
 **                                                      **
 ** trace_printk() being used. Allocating extra memory.  **
 **                                                      **
 ** This means that this is a DEBUG kernel and it is     **
 ** unsafe for production use.                           **

If this option is set to n, the kernel will generate a build-time
error if trace_printk is used.

Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
---
 include/linux/kernel.h | 17 ++++++++++++++++-
 kernel/trace/Kconfig   |  9 +++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 196607aaf653082..b6addc6ba669e85 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -721,10 +721,15 @@ do {									\
 #define trace_printk(fmt, ...)				\
 do {							\
 	char _______STR[] = __stringify((__VA_ARGS__));	\
+							\
+	__static_assert(				\
+		IS_ENABLED(CONFIG_TRACING_ALLOW_PRINTK),\
+		"trace_printk called, please enable CONFIG_TRACING_ALLOW_PRINTK."); \
+							\
 	if (sizeof(_______STR) > 3)			\
 		do_trace_printk(fmt, ##__VA_ARGS__);	\
 	else						\
-		trace_puts(fmt);			\
+		do_trace_puts(fmt);			\
 } while (0)
 
 #define do_trace_printk(fmt, args...)					\
@@ -773,6 +778,13 @@ int __trace_printk(unsigned long ip, const char *fmt, ...);
  */
 
 #define trace_puts(str) ({						\
+	__static_assert(						\
+		IS_ENABLED(CONFIG_TRACING_ALLOW_PRINTK),		\
+		"trace_puts called, please enable CONFIG_TRACING_ALLOW_PRINTK."); \
+	do_trace_puts(str);						\
+})
+
+#define do_trace_puts(str) ({						\
 	static const char *trace_printk_fmt __used			\
 		__attribute__((section("__trace_printk_fmt"))) =	\
 		__builtin_constant_p(str) ? str : NULL;			\
@@ -794,6 +806,9 @@ extern void trace_dump_stack(int skip);
  */
 #define ftrace_vprintk(fmt, vargs)					\
 do {									\
+	__static_assert(						\
+		IS_ENABLED(CONFIG_TRACING_ALLOW_PRINTK),		\
+		"ftrace_vprintk called, please enable CONFIG_TRACING_ALLOW_PRINTK."); \
 	if (__builtin_constant_p(fmt)) {				\
 		static const char *trace_printk_fmt __used		\
 		  __attribute__((section("__trace_printk_fmt"))) =	\
diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig
index a4020c0b4508c99..89ffddebe1864aa 100644
--- a/kernel/trace/Kconfig
+++ b/kernel/trace/Kconfig
@@ -111,6 +111,15 @@ config GENERIC_TRACER
 	bool
 	select TRACING
 
+config TRACING_ALLOW_PRINTK
+	bool "Allow trace_printk"
+	default y if DEBUG_KERNEL
+	depends on TRACING
+	help
+	  trace_printk is only meant as a debugging tool. If this option is
+	  set to n, the kernel will generate a build-time error if trace_printk
+	  is used.
+
 #
 # Minimum requirements an architecture has to meet for us to
 # be able to offer generic tracing facilities:
-- 
2.27.0.111.gc72c7da667-goog


^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2020-06-30 12:39 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-24  8:45 [PATCH] kernel/trace: Add TRACING_ALLOW_PRINTK config option Nicolas Boichat
2020-06-24 13:57 ` Jason Gunthorpe
2020-06-25  1:13   ` Nicolas Boichat
2020-06-24 16:04 ` Steven Rostedt
2020-06-24 17:25   ` Alexei Starovoitov
2020-06-25  2:00     ` Nicolas Boichat
2020-06-25  3:59       ` Alexei Starovoitov
2020-06-26 22:14         ` Steven Rostedt
2020-06-28 17:27           ` Alexei Starovoitov
2020-06-28 18:46             ` Steven Rostedt
2020-06-28 19:00               ` Steven Rostedt
2020-06-28 19:21               ` Alexei Starovoitov
2020-06-28 19:43                 ` Steven Rostedt
2020-06-28 22:02                   ` Alexei Starovoitov
2020-06-28 22:28                     ` Steven Rostedt
2020-06-28 23:43                       ` Steven Rostedt
2020-06-30  5:16                         ` Alexei Starovoitov
2020-06-30 12:39                           ` Steven Rostedt
2020-06-25  1:32   ` Nicolas Boichat
2020-06-24 17:37 ` kernel test robot
2020-06-24 19:07 ` kernel test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox