mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4] tracing: Allocate mask_str buffer dynamically
@ 2017-11-30  3:39 changbin.du
  2017-12-12 11:15 ` Du, Changbin
  0 siblings, 1 reply; 4+ messages in thread
From: changbin.du @ 2017-11-30  3:39 UTC (permalink / raw)
  To: rostedt; +Cc: mingo, linux-kernel, Changbin Du

From: Changbin Du <changbin.du@intel.com>

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.

With this change, the mutext tracing_cpumask_update_lock also can be
removed now, which was used to protect mask_str.

Signed-off-by: Changbin Du <changbin.du@intel.com>
Cc: Steven Rostedt <rostedt@goodmis.org>

---
v4:
  - calculate the buffer size using snprintf. (Rostedt)
v3:
  - remove tracing_cpumask_update_lock which was used to protect mask_str. (Rostedt)
v2:
  - remove 'static' declaration.
  - fix buffer size.
---
 kernel/trace/trace.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 73e67b6..53d29c7 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -4178,37 +4178,30 @@ static const struct file_operations show_traces_fops = {
 	.llseek		= seq_lseek,
 };
 
-/*
- * The tracer itself will not take this lock, but still we want
- * to provide a consistent cpumask to user-space:
- */
-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;
 
-	mutex_lock(&tracing_cpumask_update_lock);
+	len = snprintf(NULL, 0, "%*pb\n",
+		       cpumask_pr_args(tr->tracing_cpumask)) + 1;
+	mask_str = kmalloc(len, GFP_KERNEL);
+	if (!mask_str)
+		return -ENOMEM;
 
-	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;
 }
@@ -4228,8 +4221,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	if (err)
 		goto err_unlock;
 
-	mutex_lock(&tracing_cpumask_update_lock);
-
 	local_irq_disable();
 	arch_spin_lock(&tr->max_lock);
 	for_each_tracing_cpu(cpu) {
@@ -4252,8 +4243,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
 	local_irq_enable();
 
 	cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
-
-	mutex_unlock(&tracing_cpumask_update_lock);
 	free_cpumask_var(tracing_cpumask_new);
 
 	return count;
-- 
2.7.4

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

* Re: [PATCH v4] tracing: Allocate mask_str buffer dynamically
  2017-11-30  3:39 [PATCH v4] tracing: Allocate mask_str buffer dynamically changbin.du
@ 2017-12-12 11:15 ` Du, Changbin
  2017-12-13 20:17   ` Steven Rostedt
  0 siblings, 1 reply; 4+ messages in thread
From: Du, Changbin @ 2017-12-12 11:15 UTC (permalink / raw)
  To: changbin.du; +Cc: rostedt, mingo, linux-kernel


Hi Rostedt, How about this version?

On Thu, Nov 30, 2017 at 11:39:43AM +0800, changbin.du@intel.com wrote:
> From: Changbin Du <changbin.du@intel.com>
> 
> 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.
> 
> With this change, the mutext tracing_cpumask_update_lock also can be
> removed now, which was used to protect mask_str.
> 
> Signed-off-by: Changbin Du <changbin.du@intel.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> 
> ---
> v4:
>   - calculate the buffer size using snprintf. (Rostedt)
> v3:
>   - remove tracing_cpumask_update_lock which was used to protect mask_str. (Rostedt)
> v2:
>   - remove 'static' declaration.
>   - fix buffer size.
> ---
>  kernel/trace/trace.c | 29 +++++++++--------------------
>  1 file changed, 9 insertions(+), 20 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 73e67b6..53d29c7 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -4178,37 +4178,30 @@ static const struct file_operations show_traces_fops = {
>  	.llseek		= seq_lseek,
>  };
>  
> -/*
> - * The tracer itself will not take this lock, but still we want
> - * to provide a consistent cpumask to user-space:
> - */
> -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;
>  
> -	mutex_lock(&tracing_cpumask_update_lock);
> +	len = snprintf(NULL, 0, "%*pb\n",
> +		       cpumask_pr_args(tr->tracing_cpumask)) + 1;
> +	mask_str = kmalloc(len, GFP_KERNEL);
> +	if (!mask_str)
> +		return -ENOMEM;
>  
> -	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;
>  }
> @@ -4228,8 +4221,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
>  	if (err)
>  		goto err_unlock;
>  
> -	mutex_lock(&tracing_cpumask_update_lock);
> -
>  	local_irq_disable();
>  	arch_spin_lock(&tr->max_lock);
>  	for_each_tracing_cpu(cpu) {
> @@ -4252,8 +4243,6 @@ tracing_cpumask_write(struct file *filp, const char __user *ubuf,
>  	local_irq_enable();
>  
>  	cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new);
> -
> -	mutex_unlock(&tracing_cpumask_update_lock);
>  	free_cpumask_var(tracing_cpumask_new);
>  
>  	return count;
> -- 
> 2.7.4
> 

-- 
Thanks,
Changbin Du

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

* Re: [PATCH v4] tracing: Allocate mask_str buffer dynamically
  2017-12-12 11:15 ` Du, Changbin
@ 2017-12-13 20:17   ` Steven Rostedt
  2017-12-14  2:31     ` Du, Changbin
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2017-12-13 20:17 UTC (permalink / raw)
  To: Du, Changbin; +Cc: mingo, linux-kernel

On Tue, 12 Dec 2017 19:15:53 +0800
"Du, Changbin" <changbin.du@intel.com> wrote:

> Hi Rostedt, How about this version?
> 

I've pulled it, but due to traveling, I haven't been able to push it to
Linus yet. I'm hoping to do that soon.

-- Steve

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

* Re: [PATCH v4] tracing: Allocate mask_str buffer dynamically
  2017-12-13 20:17   ` Steven Rostedt
@ 2017-12-14  2:31     ` Du, Changbin
  0 siblings, 0 replies; 4+ messages in thread
From: Du, Changbin @ 2017-12-14  2:31 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Du, Changbin, mingo, linux-kernel

On Wed, Dec 13, 2017 at 03:17:24PM -0500, Steven Rostedt wrote:
> On Tue, 12 Dec 2017 19:15:53 +0800
> "Du, Changbin" <changbin.du@intel.com> wrote:
> 
> > Hi Rostedt, How about this version?
> > 
> 
> I've pulled it, but due to traveling, I haven't been able to push it to
> Linus yet. I'm hoping to do that soon.
> 
> -- Steve

Thank you. And have a nice trip!

-- 
Thanks,
Changbin Du

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

end of thread, other threads:[~2017-12-14  2:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-30  3:39 [PATCH v4] tracing: Allocate mask_str buffer dynamically changbin.du
2017-12-12 11:15 ` Du, Changbin
2017-12-13 20:17   ` Steven Rostedt
2017-12-14  2:31     ` Du, Changbin

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

Powered by JetHome