Return-Path: Received: from mail-sn1nam01on0099.outbound.protection.outlook.com ([104.47.32.99]:2346 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752062AbeARVFZ (ORCPT ); Thu, 18 Jan 2018 16:05:25 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Changbin Du , Steven Rostedt , Sasha Levin Subject: [added to the 4.1 stable tree] tracing: Allocate mask_str buffer dynamically Date: Thu, 18 Jan 2018 21:01:17 +0000 Message-ID: <20180118205908.3220-212-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Changbin Du This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 90e406f96f630c07d631a021fd4af10aac913e77 ] 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. Link: http://lkml.kernel.org/r/1512013183-19107-1-git-send-email-changbin.d= u@intel.com Fixes: 36dfe9252bd4c ("ftrace: make use of tracing_cpumask") Cc: stable@vger.kernel.org Signed-off-by: Changbin Du Signed-off-by: Steven Rostedt (VMware) Signed-off-by: Sasha Levin --- 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 ca1a306ea7e6..7a26798ffbf9 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -3376,37 +3376,30 @@ static const struct file_operations show_traces_fop= s =3D { .llseek =3D seq_lseek, }; =20 -/* - * 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 =3D file_inode(filp)->i_private; + char *mask_str; int len; =20 - mutex_lock(&tracing_cpumask_update_lock); + len =3D snprintf(NULL, 0, "%*pb\n", + cpumask_pr_args(tr->tracing_cpumask)) + 1; + mask_str =3D kmalloc(len, GFP_KERNEL); + if (!mask_str) + return -ENOMEM; =20 - len =3D snprintf(mask_str, count, "%*pb\n", + len =3D snprintf(mask_str, len, "%*pb\n", cpumask_pr_args(tr->tracing_cpumask)); if (len >=3D count) { count =3D -EINVAL; goto out_err; } - count =3D simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1)= ; + count =3D simple_read_from_buffer(ubuf, count, ppos, mask_str, len); =20 out_err: - mutex_unlock(&tracing_cpumask_update_lock); + kfree(mask_str); =20 return count; } @@ -3426,8 +3419,6 @@ tracing_cpumask_write(struct file *filp, const char _= _user *ubuf, if (err) goto err_unlock; =20 - mutex_lock(&tracing_cpumask_update_lock); - local_irq_disable(); arch_spin_lock(&tr->max_lock); for_each_tracing_cpu(cpu) { @@ -3450,8 +3441,6 @@ tracing_cpumask_write(struct file *filp, const char _= _user *ubuf, local_irq_enable(); =20 cpumask_copy(tr->tracing_cpumask, tracing_cpumask_new); - - mutex_unlock(&tracing_cpumask_update_lock); free_cpumask_var(tracing_cpumask_new); =20 return count; --=20 2.11.0