diff mbox series

[4/4] tracing: Allow boot instances to have snapshot buffers

Message ID 20230111145842.847715402@goodmis.org (mailing list archive)
State Superseded
Headers show
Series tracing: Addition of tracing instances via kernel command line | expand

Commit Message

Steven Rostedt Jan. 11, 2023, 2:56 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Add to ftrace_boot_snapshot, "=<instance>" name, where the instance will
get a snapshot buffer, and will take a snapshot at the end of boot (which
will save the boot traces).

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 .../admin-guide/kernel-parameters.txt         |  9 +++
 kernel/trace/trace.c                          | 77 +++++++++++++++++--
 2 files changed, 79 insertions(+), 7 deletions(-)

Comments

kernel test robot Jan. 11, 2023, 8:03 p.m. UTC | #1
Hi Steven,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.2-rc3 next-20230111]
[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-Add-creation-of-instances-at-boot-command-line/20230111-230425
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230111145842.847715402%40goodmis.org
patch subject: [PATCH 4/4] tracing: Allow boot instances to have snapshot buffers
config: arc-defconfig
compiler: arc-elf-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/b4515aa1407184147348b74b42bf10af90b905e7
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Steven-Rostedt/tracing-Add-creation-of-instances-at-boot-command-line/20230111-230425
        git checkout b4515aa1407184147348b74b42bf10af90b905e7
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   kernel/trace/trace.c: In function 'enable_instances':
>> kernel/trace/trace.c:10244:25: error: implicit declaration of function 'do_allocate_snapshot'; did you mean 'boot_alloc_snapshot'? [-Werror=implicit-function-declaration]
   10244 |                         do_allocate_snapshot(tok);
         |                         ^~~~~~~~~~~~~~~~~~~~
         |                         boot_alloc_snapshot
   cc1: some warnings being treated as errors


vim +10244 kernel/trace/trace.c

 10227	
 10228	__init static void enable_instances(void)
 10229	{
 10230		struct trace_array *tr;
 10231		char *curr_str;
 10232		char *str;
 10233		char *tok;
 10234	
 10235		/* A tab is always appended */
 10236		boot_instance_info[boot_instance_index - 1] = '\0';
 10237		str = boot_instance_info;
 10238	
 10239		while ((curr_str = strsep(&str, "\t"))) {
 10240	
 10241			tok = strsep(&curr_str, ",");
 10242	
 10243			if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
 10244				do_allocate_snapshot(tok);
 10245	
 10246			tr = trace_array_get_by_name(tok);
 10247			if (!tr) {
 10248				pr_warn("Failed to create instance buffer %s\n", curr_str);
 10249				continue;
 10250			}
 10251			/* Allow user space to delete it */
 10252			trace_array_put(tr);
 10253	
 10254			while ((tok = strsep(&curr_str, ","))) {
 10255				early_enable_events(tr, tok, true);
 10256			}
 10257		}
 10258	}
 10259
kernel test robot Jan. 11, 2023, 9:34 p.m. UTC | #2
Hi Steven,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.2-rc3 next-20230111]
[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-Add-creation-of-instances-at-boot-command-line/20230111-230425
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230111145842.847715402%40goodmis.org
patch subject: [PATCH 4/4] tracing: Allow boot instances to have snapshot buffers
config: hexagon-randconfig-r012-20230110
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project 8d9828ef5aa9688500657d36cd2aefbe12bbd162)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/b4515aa1407184147348b74b42bf10af90b905e7
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Steven-Rostedt/tracing-Add-creation-of-instances-at-boot-command-line/20230111-230425
        git checkout b4515aa1407184147348b74b42bf10af90b905e7
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash kernel/trace/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   In file included from kernel/trace/trace.c:18:
   In file included from include/linux/writeback.h:13:
   In file included from include/linux/blk_types.h:10:
   In file included from include/linux/bvec.h:10:
   In file included from include/linux/highmem.h:12:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __raw_readb(PCI_IOBASE + addr);
                             ~~~~~~~~~~ ^
   include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
   #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
                                                     ^
   In file included from kernel/trace/trace.c:18:
   In file included from include/linux/writeback.h:13:
   In file included from include/linux/blk_types.h:10:
   In file included from include/linux/bvec.h:10:
   In file included from include/linux/highmem.h:12:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
                                                           ~~~~~~~~~~ ^
   include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
   #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
                                                     ^
   In file included from kernel/trace/trace.c:18:
   In file included from include/linux/writeback.h:13:
   In file included from include/linux/blk_types.h:10:
   In file included from include/linux/bvec.h:10:
   In file included from include/linux/highmem.h:12:
   In file included from include/linux/hardirq.h:11:
   In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
   In file included from include/asm-generic/hardirq.h:17:
   In file included from include/linux/irq.h:20:
   In file included from include/linux/io.h:13:
   In file included from arch/hexagon/include/asm/io.h:334:
   include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writeb(value, PCI_IOBASE + addr);
                               ~~~~~~~~~~ ^
   include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
   include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
           __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
                                                         ~~~~~~~~~~ ^
>> kernel/trace/trace.c:10244:4: error: call to undeclared function 'do_allocate_snapshot'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
                           do_allocate_snapshot(tok);
                           ^
   kernel/trace/trace.c:10244:4: note: did you mean 'boot_alloc_snapshot'?
   kernel/trace/trace.c:232:19: note: 'boot_alloc_snapshot' declared here
   static int __init boot_alloc_snapshot(char *str)
                     ^
   6 warnings and 1 error generated.


vim +/do_allocate_snapshot +10244 kernel/trace/trace.c

 10227	
 10228	__init static void enable_instances(void)
 10229	{
 10230		struct trace_array *tr;
 10231		char *curr_str;
 10232		char *str;
 10233		char *tok;
 10234	
 10235		/* A tab is always appended */
 10236		boot_instance_info[boot_instance_index - 1] = '\0';
 10237		str = boot_instance_info;
 10238	
 10239		while ((curr_str = strsep(&str, "\t"))) {
 10240	
 10241			tok = strsep(&curr_str, ",");
 10242	
 10243			if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
 10244				do_allocate_snapshot(tok);
 10245	
 10246			tr = trace_array_get_by_name(tok);
 10247			if (!tr) {
 10248				pr_warn("Failed to create instance buffer %s\n", curr_str);
 10249				continue;
 10250			}
 10251			/* Allow user space to delete it */
 10252			trace_array_put(tr);
 10253	
 10254			while ((tok = strsep(&curr_str, ","))) {
 10255				early_enable_events(tr, tok, true);
 10256			}
 10257		}
 10258	}
 10259
kernel test robot Jan. 11, 2023, 11:05 p.m. UTC | #3
Hi Steven,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master v6.2-rc3 next-20230111]
[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-Add-creation-of-instances-at-boot-command-line/20230111-230425
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230111145842.847715402%40goodmis.org
patch subject: [PATCH 4/4] tracing: Allow boot instances to have snapshot buffers
config: i386-randconfig-s001
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce:
        # apt-get install sparse
        # sparse version: v0.6.4-39-gce1a6720-dirty
        # https://github.com/intel-lab-lkp/linux/commit/b4515aa1407184147348b74b42bf10af90b905e7
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Steven-Rostedt/tracing-Add-creation-of-instances-at-boot-command-line/20230111-230425
        git checkout b4515aa1407184147348b74b42bf10af90b905e7
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 olddefconfig
        make W=1 C=1 CF='-fdiagnostic-prefix -D__CHECK_ENDIAN__' O=build_dir ARCH=i386 SHELL=/bin/bash kernel/trace/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

sparse warnings: (new ones prefixed by >>)
   kernel/trace/trace.c:5809:1: sparse: sparse: trying to concatenate 11905-character string (8191 bytes max)
   kernel/trace/trace.c:444:28: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct trace_export **list @@     got struct trace_export [noderef] __rcu ** @@
   kernel/trace/trace.c:444:28: sparse:     expected struct trace_export **list
   kernel/trace/trace.c:444:28: sparse:     got struct trace_export [noderef] __rcu **
   kernel/trace/trace.c:458:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct trace_export **list @@     got struct trace_export [noderef] __rcu ** @@
   kernel/trace/trace.c:458:33: sparse:     expected struct trace_export **list
   kernel/trace/trace.c:458:33: sparse:     got struct trace_export [noderef] __rcu **
   kernel/trace/trace.c:2934:38: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct event_filter *filter @@     got struct event_filter [noderef] __rcu *filter @@
   kernel/trace/trace.c:2934:38: sparse:     expected struct event_filter *filter
   kernel/trace/trace.c:2934:38: sparse:     got struct event_filter [noderef] __rcu *filter
>> kernel/trace/trace.c:10208:51: sparse: sparse: Using plain integer as NULL pointer
   kernel/trace/trace.c:398:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   kernel/trace/trace.c:398:9: sparse:    struct trace_export [noderef] __rcu *
   kernel/trace/trace.c:398:9: sparse:    struct trace_export *
   kernel/trace/trace.c:413:9: sparse: sparse: incompatible types in comparison expression (different address spaces):
   kernel/trace/trace.c:413:9: sparse:    struct trace_export [noderef] __rcu *
   kernel/trace/trace.c:413:9: sparse:    struct trace_export *

vim +10208 kernel/trace/trace.c

 10188	
 10189	#ifdef CONFIG_TRACER_MAX_TRACE
 10190	__init static bool tr_needs_alloc_snapshot(const char *name)
 10191	{
 10192		char *test;
 10193		int len = strlen(name);
 10194		bool ret;
 10195	
 10196		if (!boot_snapshot_index)
 10197			return false;
 10198	
 10199		if (strncmp(name, boot_snapshot_info, len) == 0 &&
 10200		    boot_snapshot_info[len] == '\t')
 10201			return true;
 10202	
 10203		test = kmalloc(strlen(name) + 3, GFP_KERNEL);
 10204		if (!test)
 10205			return false;
 10206	
 10207		sprintf(test, "\t%s\t", name);
 10208		ret = strstr(boot_snapshot_info, test) == 0;
 10209		kfree(test);
 10210		return ret;
 10211	}
 10212
kernel test robot Jan. 11, 2023, 11:45 p.m. UTC | #4
Hi Steven,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on linus/master v6.2-rc3 next-20230111]
[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-Add-creation-of-instances-at-boot-command-line/20230111-230425
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230111145842.847715402%40goodmis.org
patch subject: [PATCH 4/4] tracing: Allow boot instances to have snapshot buffers
config: i386-randconfig-a002
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/b4515aa1407184147348b74b42bf10af90b905e7
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Steven-Rostedt/tracing-Add-creation-of-instances-at-boot-command-line/20230111-230425
        git checkout b4515aa1407184147348b74b42bf10af90b905e7
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> kernel/trace/trace.c:10244:4: error: implicit declaration of function 'do_allocate_snapshot' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                           do_allocate_snapshot(tok);
                           ^
   kernel/trace/trace.c:10244:4: note: did you mean 'boot_alloc_snapshot'?
   kernel/trace/trace.c:232:19: note: 'boot_alloc_snapshot' declared here
   static int __init boot_alloc_snapshot(char *str)
                     ^
   1 error generated.


vim +/do_allocate_snapshot +10244 kernel/trace/trace.c

 10227	
 10228	__init static void enable_instances(void)
 10229	{
 10230		struct trace_array *tr;
 10231		char *curr_str;
 10232		char *str;
 10233		char *tok;
 10234	
 10235		/* A tab is always appended */
 10236		boot_instance_info[boot_instance_index - 1] = '\0';
 10237		str = boot_instance_info;
 10238	
 10239		while ((curr_str = strsep(&str, "\t"))) {
 10240	
 10241			tok = strsep(&curr_str, ",");
 10242	
 10243			if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
 10244				do_allocate_snapshot(tok);
 10245	
 10246			tr = trace_array_get_by_name(tok);
 10247			if (!tr) {
 10248				pr_warn("Failed to create instance buffer %s\n", curr_str);
 10249				continue;
 10250			}
 10251			/* Allow user space to delete it */
 10252			trace_array_put(tr);
 10253	
 10254			while ((tok = strsep(&curr_str, ","))) {
 10255				early_enable_events(tr, tok, true);
 10256			}
 10257		}
 10258	}
 10259
Ross Zwisler Jan. 14, 2023, 12:08 a.m. UTC | #5
On Wed, Jan 11, 2023 at 09:56:40AM -0500, Steven Rostedt wrote:
> From: "Steven Rostedt (Google)" <rostedt@goodmis.org>
> 
> Add to ftrace_boot_snapshot, "=<instance>" name, where the instance will
> get a snapshot buffer, and will take a snapshot at the end of boot (which
> will save the boot traces).
> 
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
> ---
>  .../admin-guide/kernel-parameters.txt         |  9 +++
>  kernel/trace/trace.c                          | 77 +++++++++++++++++--
>  2 files changed, 79 insertions(+), 7 deletions(-)
> 
> diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
> index 7a7f41652719..f4e87b17427f 100644
> --- a/Documentation/admin-guide/kernel-parameters.txt
> +++ b/Documentation/admin-guide/kernel-parameters.txt
> @@ -1532,6 +1532,15 @@
>  			boot up that is likely to be overridden by user space
>  			start up functionality.
>  
> +			Optionally, the snapshot can also be defined for a tracing
> +			instance that was created by the trace_instance= command
> +			line parameter.
> +
> +			trace_instance=foo,sched_switch ftrace_boot_snapshot=foo
> +
> +			The above will cause the "foo" tracing instance to trigger
> +			a snapshot at the end of boot up.
> +
>  	ftrace_dump_on_oops[=orig_cpu]
>  			[FTRACE] will dump the trace buffers on oops.
>  			If no parameter is passed, ftrace will dump
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 3cb9bbc0f076..d445789dc247 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -192,6 +192,9 @@ static bool snapshot_at_boot;
>  static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
>  static int boot_instance_index;
>  
> +static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;

For x86 machines at least COMMAND_LINE_SIZE is pretty big (2048), so between
boot_instance_info and boot_snapshot_info we are using an entire 4k of memory.
It seems unlikely that any user would need a string this long for these
options.  Should we trim this down to something smaller?

> +static int boot_snapshot_index;
> +
>  static int __init set_cmdline_ftrace(char *str)
>  {
>  	strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
> @@ -228,9 +231,22 @@ __setup("traceoff_on_warning", stop_trace_on_warning);
>  
>  static int __init boot_alloc_snapshot(char *str)
>  {
> -	allocate_snapshot = true;
> -	/* We also need the main ring buffer expanded */
> -	ring_buffer_expanded = true;
> +	char *slot = boot_snapshot_info + boot_snapshot_index;
> +	int left = COMMAND_LINE_SIZE - boot_snapshot_index;

sizeof(boot_snapshot_info) is a bit safer than COMMAND_LINE_SIZE so they don't
get out of sync, plus we may also want to shrink it a bit as mentioned above.

Just two nits, other than that you can add:

Reviewed-by: Ross Zwisler <zwisler@google.com>
Steven Rostedt Jan. 14, 2023, 4:04 a.m. UTC | #6
On Fri, 13 Jan 2023 17:08:09 -0700
Ross Zwisler <zwisler@google.com> wrote:

> > +++ b/kernel/trace/trace.c
> > @@ -192,6 +192,9 @@ static bool snapshot_at_boot;
> >  static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
> >  static int boot_instance_index;
> >  
> > +static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;  
> 
> For x86 machines at least COMMAND_LINE_SIZE is pretty big (2048), so between
> boot_instance_info and boot_snapshot_info we are using an entire 4k of memory.
> It seems unlikely that any user would need a string this long for these
> options.  Should we trim this down to something smaller?

They are both empty (BSS) and initdata. So they get allocated at boot up
and freed at the end of boot.

> 
> > +static int boot_snapshot_index;
> > +
> >  static int __init set_cmdline_ftrace(char *str)
> >  {
> >  	strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
> > @@ -228,9 +231,22 @@ __setup("traceoff_on_warning", stop_trace_on_warning);
> >  
> >  static int __init boot_alloc_snapshot(char *str)
> >  {
> > -	allocate_snapshot = true;
> > -	/* We also need the main ring buffer expanded */
> > -	ring_buffer_expanded = true;
> > +	char *slot = boot_snapshot_info + boot_snapshot_index;
> > +	int left = COMMAND_LINE_SIZE - boot_snapshot_index;  
> 
> sizeof(boot_snapshot_info) is a bit safer than COMMAND_LINE_SIZE so they don't
> get out of sync, plus we may also want to shrink it a bit as mentioned above.

Yeah, I'm willing to do this (as mentioned before).

> 
> Just two nits, other than that you can add:
> 
> Reviewed-by: Ross Zwisler <zwisler@google.com>

Thanks!

-- Steve
diff mbox series

Patch

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 7a7f41652719..f4e87b17427f 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1532,6 +1532,15 @@ 
 			boot up that is likely to be overridden by user space
 			start up functionality.
 
+			Optionally, the snapshot can also be defined for a tracing
+			instance that was created by the trace_instance= command
+			line parameter.
+
+			trace_instance=foo,sched_switch ftrace_boot_snapshot=foo
+
+			The above will cause the "foo" tracing instance to trigger
+			a snapshot at the end of boot up.
+
 	ftrace_dump_on_oops[=orig_cpu]
 			[FTRACE] will dump the trace buffers on oops.
 			If no parameter is passed, ftrace will dump
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 3cb9bbc0f076..d445789dc247 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -192,6 +192,9 @@  static bool snapshot_at_boot;
 static char boot_instance_info[COMMAND_LINE_SIZE] __initdata;
 static int boot_instance_index;
 
+static char boot_snapshot_info[COMMAND_LINE_SIZE] __initdata;
+static int boot_snapshot_index;
+
 static int __init set_cmdline_ftrace(char *str)
 {
 	strlcpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
@@ -228,9 +231,22 @@  __setup("traceoff_on_warning", stop_trace_on_warning);
 
 static int __init boot_alloc_snapshot(char *str)
 {
-	allocate_snapshot = true;
-	/* We also need the main ring buffer expanded */
-	ring_buffer_expanded = true;
+	char *slot = boot_snapshot_info + boot_snapshot_index;
+	int left = COMMAND_LINE_SIZE - boot_snapshot_index;
+	int ret;
+
+	if (str[0] == '=') {
+		str++;
+		if (strlen(str) >= left)
+			return -1;
+
+		ret = snprintf(slot, left, "%s\t", str);
+		boot_snapshot_index += ret;
+	} else {
+		allocate_snapshot = true;
+		/* We also need the main ring buffer expanded */
+		ring_buffer_expanded = true;
+	}
 	return 1;
 }
 __setup("alloc_snapshot", boot_alloc_snapshot);
@@ -9255,10 +9271,6 @@  static int allocate_trace_buffers(struct trace_array *tr, int size)
 	}
 	tr->allocated_snapshot = allocate_snapshot;
 
-	/*
-	 * Only the top level trace array gets its snapshot allocated
-	 * from the kernel command line.
-	 */
 	allocate_snapshot = false;
 #endif
 
@@ -10174,6 +10186,45 @@  ssize_t trace_parse_run_command(struct file *file, const char __user *buffer,
 	return ret;
 }
 
+#ifdef CONFIG_TRACER_MAX_TRACE
+__init static bool tr_needs_alloc_snapshot(const char *name)
+{
+	char *test;
+	int len = strlen(name);
+	bool ret;
+
+	if (!boot_snapshot_index)
+		return false;
+
+	if (strncmp(name, boot_snapshot_info, len) == 0 &&
+	    boot_snapshot_info[len] == '\t')
+		return true;
+
+	test = kmalloc(strlen(name) + 3, GFP_KERNEL);
+	if (!test)
+		return false;
+
+	sprintf(test, "\t%s\t", name);
+	ret = strstr(boot_snapshot_info, test) == 0;
+	kfree(test);
+	return ret;
+}
+
+__init static void do_allocate_snapshot(const char *name)
+{
+	if (!tr_needs_alloc_snapshot(name))
+		return;
+
+	/*
+	 * When allocate_snapshot is set, the next call to
+	 * allocate_trace_buffers() (called by trace_array_get_by_name())
+	 * will allocate the snapshot buffer. That will alse clear
+	 * this flag.
+	 */
+	allocate_snapshot = true;
+}
+#endif
+
 __init static void enable_instances(void)
 {
 	struct trace_array *tr;
@@ -10189,6 +10240,9 @@  __init static void enable_instances(void)
 
 		tok = strsep(&curr_str, ",");
 
+		if (IS_ENABLED(CONFIG_TRACER_MAX_TRACE))
+			do_allocate_snapshot(tok);
+
 		tr = trace_array_get_by_name(tok);
 		if (!tr) {
 			pr_warn("Failed to create instance buffer %s\n", curr_str);
@@ -10336,10 +10390,19 @@  __init static int tracer_alloc_buffers(void)
 
 void __init ftrace_boot_snapshot(void)
 {
+	struct trace_array *tr;
+
 	if (snapshot_at_boot) {
 		tracing_snapshot();
 		internal_trace_puts("** Boot snapshot taken **\n");
 	}
+
+	list_for_each_entry(tr, &ftrace_trace_arrays, list) {
+		if (tr == &global_trace)
+			continue;
+		trace_array_puts(tr, "** Boot snapshot taken **\n");
+		tracing_snapshot_instance(tr);
+	}
 }
 
 void __init early_trace_init(void)