linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] tracing: Allocate mask_str buffer dynamically
@ 2017-10-25 16:20 changbin.du
  2017-10-26 11:15 ` Steven Rostedt
  2017-10-31 16:19 ` Steven Rostedt
  0 siblings, 2 replies; 4+ messages in thread
From: changbin.du @ 2017-10-25 16:20 UTC (permalink / raw)
  To: rostedt, mingo; +Cc: 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.

The exact buffer size should be:
  DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;

Example output:
  ff,ffffffff

Signed-off-by: Changbin Du <changbin.du@intel.com>

---
v2:
  - remove 'static' declaration.
  - fix buffer size.
---
 kernel/trace/trace.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 752e5da..6b70648 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -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;
 }
-- 
2.7.4

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

* Re: [PATCH v2] tracing: Allocate mask_str buffer dynamically
  2017-10-25 16:20 [PATCH v2] tracing: Allocate mask_str buffer dynamically changbin.du
@ 2017-10-26 11:15 ` Steven Rostedt
  2017-10-31 16:19 ` Steven Rostedt
  1 sibling, 0 replies; 4+ messages in thread
From: Steven Rostedt @ 2017-10-26 11:15 UTC (permalink / raw)
  To: changbin.du; +Cc: mingo, linux-kernel

On Thu, 26 Oct 2017 00:20:28 +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.
> 
> The exact buffer size should be:
>   DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;
> 
> Example output:
>   ff,ffffffff
> 
> Signed-off-by: Changbin Du <changbin.du@intel.com>
> 

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

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

* Re: [PATCH v2] tracing: Allocate mask_str buffer dynamically
  2017-10-25 16:20 [PATCH v2] tracing: Allocate mask_str buffer dynamically changbin.du
  2017-10-26 11:15 ` Steven Rostedt
@ 2017-10-31 16:19 ` Steven Rostedt
  2017-11-01  2:56   ` Du, Changbin
  1 sibling, 1 reply; 4+ messages in thread
From: Steven Rostedt @ 2017-10-31 16:19 UTC (permalink / raw)
  To: changbin.du; +Cc: mingo, linux-kernel

On Thu, 26 Oct 2017 00:20:28 +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.
> 
> The exact buffer size should be:
>   DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;
> 
> Example output:
>   ff,ffffffff
> 
> Signed-off-by: Changbin Du <changbin.du@intel.com>
> 
> ---
> v2:
>   - remove 'static' declaration.
>   - fix buffer size.
> ---
>  kernel/trace/trace.c | 18 ++++++++++--------
>  1 file changed, 10 insertions(+), 8 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 752e5da..6b70648 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -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.

>  
> -/*
> - * 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);

This patch can remove the mutex as well, since there's no sharing of
the mask anymore.

-- Steve

>  
> -	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;
>  }

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

* Re: [PATCH v2] tracing: Allocate mask_str buffer dynamically
  2017-10-31 16:19 ` Steven Rostedt
@ 2017-11-01  2:56   ` Du, Changbin
  0 siblings, 0 replies; 4+ messages in thread
From: Du, Changbin @ 2017-11-01  2:56 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: changbin.du, mingo, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2776 bytes --]

Hi Rostedt,
On Tue, Oct 31, 2017 at 12:19:58PM -0400, Steven Rostedt wrote:
> On Thu, 26 Oct 2017 00:20:28 +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.
> > 
> > The exact buffer size should be:
> >   DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2;
> > 
> > Example output:
> >   ff,ffffffff
> > 
> > Signed-off-by: Changbin Du <changbin.du@intel.com>
> > 
> > ---
> > v2:
> >   - remove 'static' declaration.
> >   - fix buffer size.
> > ---
> >  kernel/trace/trace.c | 18 ++++++++++--------
> >  1 file changed, 10 insertions(+), 8 deletions(-)
> > 
> > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> > index 752e5da..6b70648 100644
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> > @@ -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.
> 
> >  
> > -/*
> > - * 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);
> 
> This patch can remove the mutex as well, since there's no sharing of
> the mask anymore.
> 
> -- Steve
>
ok, let me remove it in v3.

> >  
> > -	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;
> >  }
> 

-- 
Thanks,
Changbin Du

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

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

end of thread, other threads:[~2017-11-01  3:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-25 16:20 [PATCH v2] tracing: Allocate mask_str buffer dynamically changbin.du
2017-10-26 11:15 ` Steven Rostedt
2017-10-31 16:19 ` Steven Rostedt
2017-11-01  2:56   ` Du, Changbin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).