tracing: Move snapshot code out of trace.c and into trace_snapshot.c
Steven Rostedt
maintainerPATCH 1/1 tracing: Move snapshot code out of trace.c and into trace_snapshot.c
From: Steven Rostedt <[email protected]>
The trace.c file was a dumping ground for most tracing code. Start organizing it better by moving various functions out into their own files. Move all the snapshot code, including the max trace code into its own trace_snapshot.c file.
---
Changes since v1: https://patch.msgid.link/[email protected]
- Rebased on top of v7.0-rc2
- Fixed up a few config issues
include/linux/ftrace.h | 2 +- kernel/trace/Makefile | 1 + kernel/trace/trace.c | 1273 ++------------------------------- kernel/trace/trace.h | 105 ++- kernel/trace/trace_snapshot.c | 1067 +++++++++++++++++++++++++++ 5 files changed, 1230 insertions(+), 1218 deletions(-) create mode 100644 kernel/trace/trace_snapshot.c
include/linux/ftrace.h +1 -1
@@ -31,7 +31,7 @@#define ARCH_SUPPORTS_FTRACE_OPS 0#endif -#ifdef CONFIG_TRACING +#ifdef CONFIG_TRACER_SNAPSHOT extern void ftrace_boot_snapshot (void );#else static inline void ftrace_boot_snapshot (void ) { }
kernel/trace/Makefile +1 -0
@@ -69,6 +69,7 @@ obj-$(CONFIG_TRACING) += trace_seq.o obj-$(CONFIG_TRACING) += trace_stat.o obj-$(CONFIG_TRACING) += trace_printk.o obj-$(CONFIG_TRACING) += trace_pid.o +obj-$(CONFIG_TRACER_SNAPSHOT) += trace_snapshot.o obj-$(CONFIG_TRACING) += pid_list.o obj-$(CONFIG_TRACING_MAP) += tracing_map.o obj-$(CONFIG_PREEMPTIRQ_DELAY_TEST) += preemptirq_delay_test.o
kernel/trace/trace.c +64 -1209
1,518 lines not on this page · raw message
kernel/trace/trace.h +97 -8
169 lines not on this page · raw message
kernel/trace/trace_snapshot.c new file +1067 -0
1,067 lines not on this page · raw message
Signature · 1 line
2.51.0
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Steven,
kernel test robot noticed the following build errors:
[auto build test ERROR on v7.0-rc2]
[cannot apply to trace/for-next linus/master next-20260306] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Rostedt/tracing-Move-snapshot-code-out-of-trace-c-and-into-trace_snapshot-c/20260306-102714 base: v7.0-rc2 patch link: https://lore.kernel.org/r/20260305211810.3f48aa07%40robin patch subject: [PATCH] tracing: Move snapshot code out of trace.c and into trace_snapshot.c config: arc-randconfig-002-20260307 (https://download.01.org/0day-ci/archive/20260307/[email protected]/config) compiler: arc-linux-gcc (GCC) 14.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260307/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> kernel/trace/trace_snapshot.c:395:20: error: redefinition of 'latency_fsnotify'
395 | static inline void latency_fsnotify(struct trace_array *tr) { }
| ^~~~~~~~~~~~~~~~
In file included from kernel/trace/trace_snapshot.c:6:
kernel/trace/trace.h:852:20: note: previous definition of 'latency_fsnotify' with type 'void(struct trace_array *)'
852 | static inline void latency_fsnotify(struct trace_array *tr) { }
| ^~~~~~~~~~~~~~~~vim +/latency_fsnotify +395 kernel/trace/trace_snapshot.c
382
383 void latency_fsnotify(struct trace_array *tr)
384 {
385 if (!fsnotify_wq)
386 return;
387 /*
388 * We cannot call queue_work(&tr->fsnotify_work) from here because it's
389 * possible that we are called from __schedule() or do_idle(), which
390 * could cause a deadlock.
391 */
392 irq_work_queue(&tr->fsnotify_irqwork);
393 }
394 #else
> 395 static inline void latency_fsnotify(struct trace_array *tr) { } 396 #endif /* LATENCY_FS_NOTIFY */
397 static const struct file_operations tracing_max_lat_fops;
398
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Steven,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v7.0-rc2]
[cannot apply to trace/for-next linus/master next-20260305] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Rostedt/tracing-Move-snapshot-code-out-of-trace-c-and-into-trace_snapshot-c/20260306-102714 base: v7.0-rc2 patch link: https://lore.kernel.org/r/20260305211810.3f48aa07%40robin patch subject: [PATCH] tracing: Move snapshot code out of trace.c and into trace_snapshot.c config: sh-defconfig (https://download.01.org/0day-ci/archive/20260307/[email protected]/config) compiler: sh4-linux-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260307/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/trace/trace.c:820:5: warning: no previous prototype for 'tracing_alloc_snapshot' [-Wmissing-prototypes]
820 | int tracing_alloc_snapshot(void)
| ^~~~~~~~~~~~~~~~~~~~~~
vim +/tracing_alloc_snapshot +820 kernel/trace/trace.c
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 792)
93e31ffbf417a84 Tom Zanussi 2013-10-24 793 /**
93e31ffbf417a84 Tom Zanussi 2013-10-24 794 * tracing_alloc_snapshot - allocate snapshot buffer.
93e31ffbf417a84 Tom Zanussi 2013-10-24 795 *
93e31ffbf417a84 Tom Zanussi 2013-10-24 796 * This only allocates the snapshot buffer if it isn't already
93e31ffbf417a84 Tom Zanussi 2013-10-24 797 * allocated - it doesn't also take a snapshot.
93e31ffbf417a84 Tom Zanussi 2013-10-24 798 *
93e31ffbf417a84 Tom Zanussi 2013-10-24 799 * This is meant to be used in cases where the snapshot buffer needs
93e31ffbf417a84 Tom Zanussi 2013-10-24 800 * to be set up for events that can't sleep but need to be able to
93e31ffbf417a84 Tom Zanussi 2013-10-24 801 * trigger a snapshot.
93e31ffbf417a84 Tom Zanussi 2013-10-24 802 */
93e31ffbf417a84 Tom Zanussi 2013-10-24 803 int tracing_alloc_snapshot(void)
93e31ffbf417a84 Tom Zanussi 2013-10-24 804 {
93e31ffbf417a84 Tom Zanussi 2013-10-24 805 struct trace_array *tr = &global_trace;
93e31ffbf417a84 Tom Zanussi 2013-10-24 806 int ret;
93e31ffbf417a84 Tom Zanussi 2013-10-24 807
2824f5033248600 Steven Rostedt (VMware 2018-05-28 808) ret = tracing_alloc_snapshot_instance(tr);
93e31ffbf417a84 Tom Zanussi 2013-10-24 809 WARN_ON(ret < 0);
93e31ffbf417a84 Tom Zanussi 2013-10-24 810
93e31ffbf417a84 Tom Zanussi 2013-10-24 811 return ret;
93e31ffbf417a84 Tom Zanussi 2013-10-24 812 }
93e31ffbf417a84 Tom Zanussi 2013-10-24 813 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 814) #else
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 815) void tracing_snapshot(void)
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 816) {
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 817) WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 818) }
1b22e382ab40b0e Steven Rostedt (Red Hat 2013-03-09 819) EXPORT_SYMBOL_GPL(tracing_snapshot);
93e31ffbf417a84 Tom Zanussi 2013-10-24 @820 int tracing_alloc_snapshot(void)
93e31ffbf417a84 Tom Zanussi 2013-10-24 821 {
93e31ffbf417a84 Tom Zanussi 2013-10-24 822 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
93e31ffbf417a84 Tom Zanussi 2013-10-24 823 return -ENODEV;
93e31ffbf417a84 Tom Zanussi 2013-10-24 824 }
93e31ffbf417a84 Tom Zanussi 2013-10-24 825 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 826) void tracing_snapshot_alloc(void)
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 827) {
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 828) /* Give warning */
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 829) tracing_snapshot();
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 830) }
1b22e382ab40b0e Steven Rostedt (Red Hat 2013-03-09 831) EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 832) #endif /* CONFIG_TRACER_SNAPSHOT */
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 833) Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Steven,
kernel test robot noticed the following build warnings:
[auto build test WARNING on v7.0-rc2]
[cannot apply to trace/for-next linus/master next-20260306] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Steven-Rostedt/tracing-Move-snapshot-code-out-of-trace-c-and-into-trace_snapshot-c/20260306-102714 base: v7.0-rc2 patch link: https://lore.kernel.org/r/20260305211810.3f48aa07%40robin patch subject: [PATCH] tracing: Move snapshot code out of trace.c and into trace_snapshot.c config: sparc64-defconfig (https://download.01.org/0day-ci/archive/20260307/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260307/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> kernel/trace/trace.c:820:5: warning: no previous prototype for function 'tracing_alloc_snapshot' [-Wmissing-prototypes]
820 | int tracing_alloc_snapshot(void)
| ^
kernel/trace/trace.c:820:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
820 | int tracing_alloc_snapshot(void)
| ^
| static 1 warning generated.
vim +/tracing_alloc_snapshot +820 kernel/trace/trace.c
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 792)
93e31ffbf417a84 Tom Zanussi 2013-10-24 793 /**
93e31ffbf417a84 Tom Zanussi 2013-10-24 794 * tracing_alloc_snapshot - allocate snapshot buffer.
93e31ffbf417a84 Tom Zanussi 2013-10-24 795 *
93e31ffbf417a84 Tom Zanussi 2013-10-24 796 * This only allocates the snapshot buffer if it isn't already
93e31ffbf417a84 Tom Zanussi 2013-10-24 797 * allocated - it doesn't also take a snapshot.
93e31ffbf417a84 Tom Zanussi 2013-10-24 798 *
93e31ffbf417a84 Tom Zanussi 2013-10-24 799 * This is meant to be used in cases where the snapshot buffer needs
93e31ffbf417a84 Tom Zanussi 2013-10-24 800 * to be set up for events that can't sleep but need to be able to
93e31ffbf417a84 Tom Zanussi 2013-10-24 801 * trigger a snapshot.
93e31ffbf417a84 Tom Zanussi 2013-10-24 802 */
93e31ffbf417a84 Tom Zanussi 2013-10-24 803 int tracing_alloc_snapshot(void)
93e31ffbf417a84 Tom Zanussi 2013-10-24 804 {
93e31ffbf417a84 Tom Zanussi 2013-10-24 805 struct trace_array *tr = &global_trace;
93e31ffbf417a84 Tom Zanussi 2013-10-24 806 int ret;
93e31ffbf417a84 Tom Zanussi 2013-10-24 807
2824f5033248600 Steven Rostedt (VMware 2018-05-28 808) ret = tracing_alloc_snapshot_instance(tr);
93e31ffbf417a84 Tom Zanussi 2013-10-24 809 WARN_ON(ret < 0);
93e31ffbf417a84 Tom Zanussi 2013-10-24 810
93e31ffbf417a84 Tom Zanussi 2013-10-24 811 return ret;
93e31ffbf417a84 Tom Zanussi 2013-10-24 812 }
93e31ffbf417a84 Tom Zanussi 2013-10-24 813 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 814) #else
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 815) void tracing_snapshot(void)
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 816) {
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 817) WARN_ONCE(1, "Snapshot feature not enabled, but internal snapshot used");
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 818) }
1b22e382ab40b0e Steven Rostedt (Red Hat 2013-03-09 819) EXPORT_SYMBOL_GPL(tracing_snapshot);
93e31ffbf417a84 Tom Zanussi 2013-10-24 @820 int tracing_alloc_snapshot(void)
93e31ffbf417a84 Tom Zanussi 2013-10-24 821 {
93e31ffbf417a84 Tom Zanussi 2013-10-24 822 WARN_ONCE(1, "Snapshot feature not enabled, but snapshot allocation used");
93e31ffbf417a84 Tom Zanussi 2013-10-24 823 return -ENODEV;
93e31ffbf417a84 Tom Zanussi 2013-10-24 824 }
93e31ffbf417a84 Tom Zanussi 2013-10-24 825 EXPORT_SYMBOL_GPL(tracing_alloc_snapshot);
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 826) void tracing_snapshot_alloc(void)
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 827) {
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 828) /* Give warning */
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 829) tracing_snapshot();
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 830) }
1b22e382ab40b0e Steven Rostedt (Red Hat 2013-03-09 831) EXPORT_SYMBOL_GPL(tracing_snapshot_alloc);
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 832) #endif /* CONFIG_TRACER_SNAPSHOT */
ad909e21bbe69f1 Steven Rostedt (Red Hat 2013-03-06 833) Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 9 lines · kernel test robot
Quoted 1 line
Quoted 1 line
kernel/trace/trace.c:820:5: warning: no previous prototype for function 'tracing_alloc_snapshot' [-Wmissing-prototypes]820 | int
tracing_alloc_snapshot(void)
| ^
kernel/trace/trace.c:820:1: note: declare 'static' if the function
is not intended to be used outside of this translation unit820 | int tracing_alloc_snapshot(void) | ^ | static1 warning generated.
I saw the same thing and worked around it by removing the function. I then noticed that a bunch of code surrounding it is also unused and I removed that as well (see below). This version passes my randconfig build tests, but I suspect it is still wrong, since the code never had any callers and I don't understand why.
Arnd
include/linux/trace_printk.h +0 -4
@@ -38,8 +38,6 @@ enum ftrace_dump_mode {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 );
@@ -184,8 +182,6 @@ 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, ...)
kernel/trace/trace.c +0 -64
70 lines not on this page · raw message
kernel/trace/trace.h +0 -1
@@ -2306,7 +2306,6 @@ static inline int register_snapshot_cmd(void) { return 0; }# endif #else /* !CONFIG_TRACER_SNAPSHOT */ static inline int trace_allocate_snapshot (struct trace_array *tr,int size) {return 0 ; } -static inline int tracing_alloc_snapshot (void ) {return 0 ; }static inline void tracing_snapshot_instance (struct trace_array *tr) { }static inline int tracing_alloc_snapshot_instance (struct trace_array *tr) {
kernel/trace/trace_snapshot.c +0 -25
@@ -237,29 +237,6 @@ void tracing_disarm_snapshot(struct trace_array *tr)spin_unlock (&tr->snapshot_trigger_lock); } -/** -* tracing_snapshot_alloc - allocate and take a snapshot of the current buffer. -* -* This is similar to tracing_snapshot(), but it will allocate the -* snapshot buffer if it isn't already allocated. Use this only -* where it is safe to sleep, as the allocation may sleep. -* -* This causes a swap between the snapshot buffer and the current live -* tracing buffer. You can use this to take snapshots of the live -* trace when some condition is triggered, but continue to trace. -*/ -void tracing_snapshot_alloc (void ) -{ -int ret; - - ret =tracing_alloc_snapshot (); -if (ret <0 ) -return ; - -tracing_snapshot (); -} -EXPORT_SYMBOL_GPL (tracing_snapshot_alloc); -/** * tracing_snapshot_cond_enable - enable conditional snapshot for an instance * @tr: The tracing instance
@@ -391,8 +368,6 @@ void latency_fsnotify(struct trace_array *tr)*/ irq_work_queue(&tr->fsnotify_irqwork); } -#else -static inline void latency_fsnotify (struct trace_array *tr) { }#endif /* LATENCY_FS_NOTIFY */ static const struct file_operations tracing_max_lat_fops;
Steven Rostedt
maintainerQuoted 6 lines · On Mon, 30 Mar 2026 16:06:44 +0200 "Arnd Bergmann" <[email protected]> wrote:
I saw the same thing and worked around it by removing the function. I then noticed that a bunch of code surrounding it is also unused and I removed that as well (see below). This version passes my randconfig build tests, but I suspect it is still wrong, since the code never had any callers and I don't understand why.
Note, this code is in include/linux/tracing_printk.h, and is for debugging purposes (just like trace_printk() is). Hence, it shouldn't be removed.
The purpose is to call tracing_snapshot() when your code detects something isn't right (but it doesn't crash), and this will take a snapshot of the current trace that lead up to the anomaly.
If anything, I should add more to Documentation/trace/debugging.rst about it.
-- Steve
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Mon, 30 Mar 2026 16:06:44 +0200 "Arnd Bergmann" <[email protected]> wrote:
I saw the same thing and worked around it by removing the function. I then noticed that a bunch of code surrounding it is also unused and I removed that as well (see below). This version passes my randconfig build tests, but I suspect it is still wrong, since the code never had any callers and I don't understand why.
Quoted 6 lines
Note, this code is in
include/linux/tracing_printk.h, and is for debugging purposes (just liketrace_printk()is). Hence, it shouldn't be removed.The purpose is to call
tracing_snapshot()when your code detects something isn't right (but it doesn't crash), and this will take a snapshot of the current trace that lead up to the anomaly.
Right, I assumed it had to be something like that, just didn't immediately see what. I've sent a patch to just remove the duplicate inline function now.
Quoted 1 line
If anything, I should add more to
Documentation/trace/debugging.rstabout it.
Or maybe a samples module that serves to show how the interface gets used?
Arnd
Steven Rostedt
maintainerQuoted 3 lines · On Tue, 31 Mar 2026 12:53:38 +0200 "Arnd Bergmann" <[email protected]> wrote:
Right, I assumed it had to be something like that, just didn't immediately see what. I've sent a patch to just remove the duplicate inline function now.
Ah, I had already sent a patch to fix the duplicate because I saw the kernel test robot's report.
https://lore.kernel.org/all/[email protected]/
-- Steve