tracing: Allocate mask_str buffer dynamically
PATCH v2 1/1 tracing: Allocate mask_str buffer dynamically
From: Changbin Du <[email protected]>
The default NR_CPUS can be very large, but actual possible nr_cpu_ids usually is very small. For my x86 distribution, the NR_CPUS is 8192 and nr_cpu_ids is 4. About 2 pages are wasted.
Most machines don't have so many CPUs, so define a array with NR_CPUS just wastes memory. So let's allocate the buffer dynamically when need.
The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;
Example output:
ff,ffffffff
---
v2:
- remove 'static' declaration.
- fix buffer size.
--- kernel/trace/trace.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
kernel/trace/trace.c +10 -8
@@ -4184,31 +4184,33 @@ static const struct file_operations show_traces_fops = {*/ static DEFINE_MUTEX (tracing_cpumask_update_lock); -/* -* Temporary storage for the character representation of the -* CPU bitmask (and one more byte for the newline): -*/ -static char mask_str[NR_CPUS +1 ]; -static ssize_t tracing_cpumask_read (struct file *filp,char __user *ubuf,size_t count,loff_t *ppos) {struct trace_array *tr =file_inode (filp)->i_private; +char *mask_str;int len; +/* Bitmap, ',' and two more bytes for the newline and '\0'. */ + len =DIV_ROUND_UP (nr_cpu_ids,4 ) + nr_cpu_ids/32 +2 ; + mask_str =kmalloc (len,GFP_KERNEL ); +if (!mask_str) +return -ENOMEM ; +mutex_lock (&tracing_cpumask_update_lock); - len =snprintf (mask_str, count,"%*pb\n" , + len =snprintf (mask_str, len,"%*pb\n" ,cpumask_pr_args (tr->tracing_cpumask));if (len >= count) { count = -EINVAL ; goto out_err; } - count =simple_read_from_buffer (ubuf, count, ppos, mask_str,NR_CPUS +1 ); + count =simple_read_from_buffer (ubuf, count, ppos, mask_str, len); out_err:mutex_unlock (&tracing_cpumask_update_lock); +kfree (mask_str);return count; }
Signature · 1 line
2.7.4
Steven Rostedt
maintainerQuoted 9 lines · On Thu, 26 Oct 2017 00:20:28 +0800 [email protected] wrote:
From: Changbin Du <[email protected]>
The default
NR_CPUScan be very large, but actual possiblenr_cpu_idsusually is very small. For my x86 distribution, theNR_CPUSis 8192 andnr_cpu_idsis 4. About 2 pages are wasted.Most machines don't have so many CPUs, so define a array with
NR_CPUSjust wastes memory. So let's allocate the buffer dynamically when need.
Quoted 8 lines
The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4)+ nr_cpu_ids/32 + 2;Example output:
ff,ffffffffSigned-off-by Changbin Du <[email protected]>
Thanks for sending this. I'm currently traveling for conferences, and may miss looking at this when I get back. Please send me a friendly ping reply to this email next week, if you don't hear from me sooner.
-- Steve
Steven Rostedt
maintainerQuoted 30 lines · On Thu, 26 Oct 2017 00:20:28 +0800 [email protected] wrote:
From: Changbin Du <[email protected]>
The default
NR_CPUScan be very large, but actual possiblenr_cpu_idsusually is very small. For my x86 distribution, theNR_CPUSis 8192 andnr_cpu_idsis 4. About 2 pages are wasted.Most machines don't have so many CPUs, so define a array with
NR_CPUSjust wastes memory. So let's allocate the buffer dynamically when need.The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4)+ nr_cpu_ids/32 + 2;Example output:
ff,ffffffffSigned-off-by Changbin Du <[email protected]>---
v2:
- remove 'static' declaration.
- fix buffer size.--- kernel/trace/trace.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)kernel/trace/trace.c +0
-0
kernel/trace/trace.c static const struct file_operations show_traces_fops = { · line 4185
Quoted 2 lines
kernel/trace/trace.c +0
-0@@ -4184,31 +4184,33 @@ static const struct file_operations show_traces_fops = {*/ static DEFINE_MUTEX (tracing_cpumask_update_lock);
The above mutex was used to protect mask_str.
Quoted 12 lines
kernel/trace/trace.c +0
-6-/* -* Temporary storage for the character representation of the -* CPU bitmask (and one more byte for the newline): -*/ -static char mask_str[NR_CPUS +1 ]; -static ssize_t tracing_cpumask_read (struct file *filp,char __user *ubuf,size_t count,loff_t *ppos) {struct trace_array *tr =file_inode (filp)->i_private;
kernel/trace/trace.c static const struct file_operations show_traces_fops = { · line 4201
Quoted 10 lines
kernel/trace/trace.c +7
-0+char *mask_str;int len; +/* Bitmap, ',' and two more bytes for the newline and '\0'. */ + len =DIV_ROUND_UP (nr_cpu_ids,4 ) + nr_cpu_ids/32 +2 ; + mask_str =kmalloc (len,GFP_KERNEL ); +if (!mask_str) +return -ENOMEM ; +mutex_lock (&tracing_cpumask_update_lock);
This patch can remove the mutex as well, since there's no sharing of the mask anymore.
-- Steve
Quoted 17 lines
kernel/trace/trace.c +3
-2- len =snprintf (mask_str, count,"%*pb\n" , + len =snprintf (mask_str, len,"%*pb\n" ,cpumask_pr_args (tr->tracing_cpumask));if (len >= count) { count = -EINVAL ; goto out_err; } - count =simple_read_from_buffer (ubuf, count, ppos, mask_str,NR_CPUS +1 ); + count =simple_read_from_buffer (ubuf, count, ppos, mask_str, len); out_err:mutex_unlock (&tracing_cpumask_update_lock); +kfree (mask_str);return count; }
Hi Rostedt,
Quoted 64 lines · Steven Rostedt
Quoted 30 lines · On Thu, 26 Oct 2017 00:20:28 +0800 [email protected] wrote:
From: Changbin Du <[email protected]>
The default
NR_CPUScan be very large, but actual possiblenr_cpu_idsusually is very small. For my x86 distribution, theNR_CPUSis 8192 andnr_cpu_idsis 4. About 2 pages are wasted.Most machines don't have so many CPUs, so define a array with
NR_CPUSjust wastes memory. So let's allocate the buffer dynamically when need.The exact buffer size should be:
DIV_ROUND_UP(nr_cpu_ids, 4)+ nr_cpu_ids/32 + 2;Example output:
ff,ffffffffSigned-off-by Changbin Du <[email protected]>---
v2:
- remove 'static' declaration.
- fix buffer size.--- kernel/trace/trace.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)kernel/trace/trace.c +0
-0kernel/trace/trace.c static const struct file_operations show_traces_fops = { · line 4185
Quoted 2 lines
kernel/trace/trace.c +0
-0@@ -4184,31 +4184,33 @@ static const struct file_operations show_traces_fops = {*/ static DEFINE_MUTEX (tracing_cpumask_update_lock);The above mutex was used to protect
mask_str.Quoted 12 lines
kernel/trace/trace.c +0
-6-/* -* Temporary storage for the character representation of the -* CPU bitmask (and one more byte for the newline): -*/ -static char mask_str[NR_CPUS +1 ]; -static ssize_t tracing_cpumask_read (struct file *filp,char __user *ubuf,size_t count,loff_t *ppos) {struct trace_array *tr =file_inode (filp)->i_private;kernel/trace/trace.c static const struct file_operations show_traces_fops = { · line 4201
Quoted 10 lines
kernel/trace/trace.c +7
-0+char *mask_str;int len; +/* Bitmap, ',' and two more bytes for the newline and '\0'. */ + len =DIV_ROUND_UP (nr_cpu_ids,4 ) + nr_cpu_ids/32 +2 ; + mask_str =kmalloc (len,GFP_KERNEL ); +if (!mask_str) +return -ENOMEM ; +mutex_lock (&tracing_cpumask_update_lock);This patch can remove the mutex as well, since there's no sharing of the mask anymore.
Quoted 2 lines
-- Steve
ok, let me remove it in v3.
Quoted 18 lines
Quoted 17 lines
kernel/trace/trace.c +3
-2- len =snprintf (mask_str, count,"%*pb\n" , + len =snprintf (mask_str, len,"%*pb\n" ,cpumask_pr_args (tr->tracing_cpumask));if (len >= count) { count = -EINVAL ; goto out_err; } - count =simple_read_from_buffer (ubuf, count, ppos, mask_str,NR_CPUS +1 ); + count =simple_read_from_buffer (ubuf, count, ppos, mask_str, len); out_err:mutex_unlock (&tracing_cpumask_update_lock); +kfree (mask_str);return count; }
Signature · 2 lines
Thanks, Changbin Du