public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tracing: Allocate mask_str buffer dynamically
@ 2017-10-25 11:24 changbin.du
  2017-10-25 14:08 ` Du, Changbin
  0 siblings, 1 reply; 2+ messages in thread
From: changbin.du @ 2017-10-25 11:24 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.

Signed-off-by: Changbin Du <changbin.du@intel.com>
---
 kernel/trace/trace.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 752e5da..d1b3f11 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -4184,19 +4184,18 @@ 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;
+	static char *mask_str;
 	int len;
 
+	mask_str = kmalloc(nr_cpu_ids + 1, GFP_KERNEL);
+	if (!mask_str)
+		return -ENOMEM;
+
 	mutex_lock(&tracing_cpumask_update_lock);
 
 	len = snprintf(mask_str, count, "%*pb\n",
@@ -4205,10 +4204,12 @@ tracing_cpumask_read(struct file *filp, char __user *ubuf,
 		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, nr_cpu_ids+1);
 
 out_err:
 	mutex_unlock(&tracing_cpumask_update_lock);
+	kfree(mask_str);
 
 	return count;
 }
-- 
2.7.4

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

* Re: [PATCH] tracing: Allocate mask_str buffer dynamically
  2017-10-25 11:24 [PATCH] tracing: Allocate mask_str buffer dynamically changbin.du
@ 2017-10-25 14:08 ` Du, Changbin
  0 siblings, 0 replies; 2+ messages in thread
From: Du, Changbin @ 2017-10-25 14:08 UTC (permalink / raw)
  To: changbin.du; +Cc: rostedt, mingo, linux-kernel

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

On Wed, Oct 25, 2017 at 07:24:36PM +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.
> 
> Signed-off-by: Changbin Du <changbin.du@intel.com>
> ---
>  kernel/trace/trace.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 752e5da..d1b3f11 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -4184,19 +4184,18 @@ 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;
> +	static char *mask_str;
ah, need remove 'static'.

>  	int len;
>  
> +	mask_str = kmalloc(nr_cpu_ids + 1, GFP_KERNEL);
> +	if (!mask_str)
> +		return -ENOMEM;
> +
>  	mutex_lock(&tracing_cpumask_update_lock);
>  
>  	len = snprintf(mask_str, count, "%*pb\n",
> @@ -4205,10 +4204,12 @@ tracing_cpumask_read(struct file *filp, char __user *ubuf,
>  		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, nr_cpu_ids+1);
>  
>  out_err:
>  	mutex_unlock(&tracing_cpumask_update_lock);
> +	kfree(mask_str);
>  
>  	return count;
>  }
> -- 
> 2.7.4
> 

-- 
Thanks,
Changbin Du

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

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

end of thread, other threads:[~2017-10-25 14:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-25 11:24 [PATCH] tracing: Allocate mask_str buffer dynamically changbin.du
2017-10-25 14:08 ` Du, Changbin

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