Subject: [PATCH] tracing/timerlat: Hotplug support for the user-space interface

The osnoise/per_cpu/CPU$/timerlat_fd is create for each possible
CPU, but it might create confusion if the CPU is not online.

Create the file only for online CPUs, also follow hotplug by
creating and deleting as CPUs come and go.

Fixes: e88ed227f639 ("tracing/timerlat: Add user-space interface")
Signed-off-by: Daniel Bristot de Oliveira <[email protected]>
---
kernel/trace/trace_osnoise.c | 101 ++++++++++++++++++++++++++---------
1 file changed, 77 insertions(+), 24 deletions(-)

diff --git a/kernel/trace/trace_osnoise.c b/kernel/trace/trace_osnoise.c
index bd0d01d00fb9..1af01eec3e36 100644
--- a/kernel/trace/trace_osnoise.c
+++ b/kernel/trace/trace_osnoise.c
@@ -229,6 +229,19 @@ static inline struct osnoise_variables *this_cpu_osn_var(void)
}

#ifdef CONFIG_TIMERLAT_TRACER
+
+/*
+ * osnoise/per_cpu dir
+ */
+static struct dentry *osnoise_per_cpu_fd;
+
+struct osnoise_per_cpu_dir {
+ struct dentry *root;
+ struct dentry *timerlat_fd;
+};
+
+static DEFINE_PER_CPU(struct osnoise_per_cpu_dir, osnoise_per_cpu_dir);
+
/*
* Runtime information for the timer mode.
*/
@@ -2000,6 +2013,9 @@ static int start_kthread(unsigned int cpu)
char comm[24];

if (timerlat_enabled()) {
+ if (!test_bit(OSN_WORKLOAD, &osnoise_options))
+ return 0;
+
snprintf(comm, 24, "timerlat/%d", cpu);
main = timerlat_main;
} else {
@@ -2065,19 +2081,64 @@ static int start_per_cpu_kthreads(void)
return retval;
}

+#ifdef CONFIG_TIMERLAT_TRACER
+static const struct file_operations timerlat_fd_fops;
+static int timerlat_add_per_cpu_interface(long cpu)
+{
+ struct dentry *timerlat_fd, *cpu_dir_fd;
+ char cpu_str[30]; /* see trace.c: tracing_init_tracefs_percpu() */
+
+ if (!osnoise_per_cpu_fd)
+ return 0;
+
+ snprintf(cpu_str, 30, "cpu%ld", cpu);
+ cpu_dir_fd = tracefs_create_dir(cpu_str, osnoise_per_cpu_fd);
+
+ if (cpu_dir_fd) {
+ timerlat_fd = trace_create_file("timerlat_fd", TRACE_MODE_READ,
+ cpu_dir_fd, NULL, &timerlat_fd_fops);
+ WARN_ON_ONCE(!timerlat_fd);
+ per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root = cpu_dir_fd;
+ per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->timerlat_fd = timerlat_fd;
+
+ /* Record the CPU */
+ d_inode(timerlat_fd)->i_cdev = (void *)(cpu);
+
+ return 0;
+ }
+
+ return -ENOMEM;
+}
+
+static void timerlat_rm_per_cpu_interface(long cpu)
+{
+ struct dentry *cpu_dir = per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root;
+
+ if (cpu_dir) {
+ tracefs_remove(cpu_dir);
+ per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root = NULL;
+ per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->timerlat_fd = NULL;
+ }
+}
+#else
+static int timerlat_add_per_cpu_interface(long cpu) { return 0; };
+static void timerlat_rm_per_cpu_interface(long cpu) {};
+#endif
+
#ifdef CONFIG_HOTPLUG_CPU
static void osnoise_hotplug_workfn(struct work_struct *dummy)
{
unsigned int cpu = smp_processor_id();

mutex_lock(&trace_types_lock);
-
- if (!osnoise_has_registered_instances())
- goto out_unlock_trace;
-
mutex_lock(&interface_lock);
cpus_read_lock();

+ timerlat_add_per_cpu_interface(cpu);
+
+ if (!osnoise_has_registered_instances())
+ goto out_unlock;
+
if (!cpumask_test_cpu(cpu, &osnoise_cpumask))
goto out_unlock;

@@ -2086,7 +2147,6 @@ static void osnoise_hotplug_workfn(struct work_struct *dummy)
out_unlock:
cpus_read_unlock();
mutex_unlock(&interface_lock);
-out_unlock_trace:
mutex_unlock(&trace_types_lock);
}

@@ -2106,6 +2166,7 @@ static int osnoise_cpu_init(unsigned int cpu)
*/
static int osnoise_cpu_die(unsigned int cpu)
{
+ timerlat_rm_per_cpu_interface(cpu);
stop_kthread(cpu);
return 0;
}
@@ -2708,10 +2769,7 @@ static int init_timerlat_stack_tracefs(struct dentry *top_dir)

static int osnoise_create_cpu_timerlat_fd(struct dentry *top_dir)
{
- struct dentry *timerlat_fd;
- struct dentry *per_cpu;
- struct dentry *cpu_dir;
- char cpu_str[30]; /* see trace.c: tracing_init_tracefs_percpu() */
+ int retval;
long cpu;

/*
@@ -2720,29 +2778,24 @@ static int osnoise_create_cpu_timerlat_fd(struct dentry *top_dir)
* Because osnoise/timerlat have a single workload, having
* multiple files like these are wast of memory.
*/
- per_cpu = tracefs_create_dir("per_cpu", top_dir);
- if (!per_cpu)
+ osnoise_per_cpu_fd = tracefs_create_dir("per_cpu", top_dir);
+ if (!osnoise_per_cpu_fd)
return -ENOMEM;

- for_each_possible_cpu(cpu) {
- snprintf(cpu_str, 30, "cpu%ld", cpu);
- cpu_dir = tracefs_create_dir(cpu_str, per_cpu);
- if (!cpu_dir)
+ for_each_online_cpu(cpu) {
+ retval = timerlat_add_per_cpu_interface(cpu);
+ if (retval < 0)
goto out_clean;
-
- timerlat_fd = trace_create_file("timerlat_fd", TRACE_MODE_READ,
- cpu_dir, NULL, &timerlat_fd_fops);
- if (!timerlat_fd)
- goto out_clean;
-
- /* Record the CPU */
- d_inode(timerlat_fd)->i_cdev = (void *)(cpu);
}

return 0;

out_clean:
- tracefs_remove(per_cpu);
+ tracefs_remove(osnoise_per_cpu_fd);
+ /* tracefs_remove() recursively deletes all the other files */
+ osnoise_per_cpu_fd = NULL;
+ for_each_online_cpu(cpu)
+ timerlat_rm_per_cpu_interface(cpu);
return -ENOMEM;
}

--
2.38.1


2023-09-16 08:18:39

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] tracing/timerlat: Hotplug support for the user-space interface

Hi Daniel,

kernel test robot noticed the following build warnings:

[auto build test WARNING on linus/master]
[also build test WARNING on v6.6-rc1 next-20230915]
[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/Daniel-Bristot-de-Oliveira/tracing-timerlat-Hotplug-support-for-the-user-space-interface/20230915-230157
base: linus/master
patch link: https://lore.kernel.org/r/b619d9fd08a3bb47018cf40afa95783844a3c1fd.1694789910.git.bristot%40kernel.org
patch subject: [PATCH] tracing/timerlat: Hotplug support for the user-space interface
config: um-randconfig-002-20230916 (https://download.01.org/0day-ci/archive/20230916/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230916/[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_osnoise.c:2125:13: warning: 'timerlat_rm_per_cpu_interface' defined but not used [-Wunused-function]
2125 | static void timerlat_rm_per_cpu_interface(long cpu) {};
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> kernel/trace/trace_osnoise.c:2124:12: warning: 'timerlat_add_per_cpu_interface' defined but not used [-Wunused-function]
2124 | static int timerlat_add_per_cpu_interface(long cpu) { return 0; };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


vim +/timerlat_rm_per_cpu_interface +2125 kernel/trace/trace_osnoise.c

2112
2113 static void timerlat_rm_per_cpu_interface(long cpu)
2114 {
2115 struct dentry *cpu_dir = per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root;
2116
2117 if (cpu_dir) {
2118 tracefs_remove(cpu_dir);
2119 per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root = NULL;
2120 per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->timerlat_fd = NULL;
2121 }
2122 }
2123 #else
> 2124 static int timerlat_add_per_cpu_interface(long cpu) { return 0; };
> 2125 static void timerlat_rm_per_cpu_interface(long cpu) {};
2126 #endif
2127

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

Subject: Re: [PATCH] tracing/timerlat: Hotplug support for the user-space interface

On 9/16/23 02:21, kernel test robot wrote:
> Hi Daniel,
>
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on linus/master]
> [also build test WARNING on v6.6-rc1 next-20230915]
> [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/Daniel-Bristot-de-Oliveira/tracing-timerlat-Hotplug-support-for-the-user-space-interface/20230915-230157
> base: linus/master
> patch link: https://lore.kernel.org/r/b619d9fd08a3bb47018cf40afa95783844a3c1fd.1694789910.git.bristot%40kernel.org
> patch subject: [PATCH] tracing/timerlat: Hotplug support for the user-space interface
> config: um-randconfig-002-20230916 (https://download.01.org/0day-ci/archive/20230916/[email protected]/config)
>

^^ The config has no timerlat and no hotplug...

compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230916/[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_osnoise.c:2125:13: warning: 'timerlat_rm_per_cpu_interface' defined but not used [-Wunused-function]
> 2125 | static void timerlat_rm_per_cpu_interface(long cpu) {};
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> kernel/trace/trace_osnoise.c:2124:12: warning: 'timerlat_add_per_cpu_interface' defined but not used [-Wunused-function]
> 2124 | static int timerlat_add_per_cpu_interface(long cpu) { return 0; };
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

These functions are called when hotplug but no timerlat.... when no hotplug: defined but not used.

>
> vim +/timerlat_rm_per_cpu_interface +2125 kernel/trace/trace_osnoise.c
>
> 2112
> 2113 static void timerlat_rm_per_cpu_interface(long cpu)
> 2114 {
> 2115 struct dentry *cpu_dir = per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root;
> 2116
> 2117 if (cpu_dir) {
> 2118 tracefs_remove(cpu_dir);
> 2119 per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->root = NULL;
> 2120 per_cpu_ptr(&osnoise_per_cpu_dir, cpu)->timerlat_fd = NULL;
> 2121 }
> 2122 }
> 2123 #else
>> 2124 static int timerlat_add_per_cpu_interface(long cpu) { return 0; };
>> 2125 static void timerlat_rm_per_cpu_interface(long cpu) {};
> 2126 #endif
> 2127
>

Fixing it.

-- Daniel