Received: by 10.223.164.202 with SMTP id h10csp177026wrb; Wed, 29 Nov 2017 19:37:39 -0800 (PST) X-Google-Smtp-Source: AGs4zMYfWIT3FziBzi5VQ7lyi/PPYRwUXOBTYz2XQNWbEjFWpoFZqbfD8VdrhaKUawh7vAeq43L9 X-Received: by 10.98.71.90 with SMTP id u87mr5181397pfa.75.1512013059662; Wed, 29 Nov 2017 19:37:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512013059; cv=none; d=google.com; s=arc-20160816; b=EXUOZYNz61TnVZT2gZQPMuxPezyYMy2/zVi+EF3VrlelhGnoaBo4nsKo3q7IJxAKFj JCL8lM3volLz2vvXi8NrveiLSDgAHiYVjmzMvNXVXW6+vjl9/+NqVjlK4NDWW5DA9VnY dQBCwS8FTkDlwJ4kOiw1ouO7ZlYPTZQY6UIEIEjnO8Rf7GWms33sUYdarC/Qcm6ij0Tq hldgvxnKCWIjIiTPO3o00mobbvm0udLmKCUhCNCW62aAoZDPTs47hRO6+7AotNQ8MhYd 3mmj/EGYukV5W3eqt/tXvMjG1yoiV4VW7A5I5PghVtlW4zSKd9+KV2yO9q/En7PdPMFz aXkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=H3J0TgEjvLbJD/1sNzrL7tbhFQqd1j5RL5VOJuPMAFs=; b=AyEpDaaCFEQOKpBxowqXkc7r7jTnAV9okFzqYwHuI0/YVR4pW2ZCneAHTZ5zFQrokX e0icuuJr195qi5wt1VDWmoxVMJrt7InvDOZsv2kb7WyClMln+yX9Z5v0zRIuqSs6jlC6 xJimRWnAtlnbhBXuBywA2pcEhljVd1I82fzD8xgqDEauEM3kQCBxP8TgifQX5U6Exyhu tvj2vNTCkfEDPiMUBTnXNpmqsGUmVOnUSIwmOdUNQ0mHJAxhDCChb2HIXoA9WlpDnjIz NvUxJzhCg/6VKqflDR+6+0K8IJfE7WNZ4KTVNdbSaPTvBTV/DUHOvjGdsAk/zi+XfAZz VI+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p6si2325171pgr.96.2017.11.29.19.37.24; Wed, 29 Nov 2017 19:37:39 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753403AbdK3Dgh (ORCPT + 99 others); Wed, 29 Nov 2017 22:36:37 -0500 Received: from mga07.intel.com ([134.134.136.100]:65149 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753109AbdK3Dgf (ORCPT ); Wed, 29 Nov 2017 22:36:35 -0500 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Nov 2017 19:36:34 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.45,339,1508828400"; d="scan'208";a="154576422" Received: from gvt-dell.bj.intel.com (HELO intel.com) ([10.238.154.59]) by orsmga004.jf.intel.com with SMTP; 29 Nov 2017 19:36:33 -0800 Date: Thu, 30 Nov 2017 11:29:00 +0800 From: "Du, Changbin" To: Steven Rostedt Cc: changbin.du@intel.com, mingo@redhat.com, linux-kernel@vger.kernel.org Subject: Re: [RESEND PATCH v3] tracing: Allocate mask_str buffer dynamically Message-ID: <20171130032900.nxagajetgcyzxmlo@intel.com> References: <1511930565-16531-1-git-send-email-changbin.du@intel.com> <20171129221209.2dd7681d@gandalf.local.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171129221209.2dd7681d@gandalf.local.home> User-Agent: NeoMutt/20171027-42-ad8712 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 29, 2017 at 10:12:09PM -0500, Steven Rostedt wrote: > On Wed, 29 Nov 2017 12:42:45 +0800 > changbin.du@intel.com wrote: > > > From: Changbin Du > > > > 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 > > Um, what if there's more than 64 CPUs, where I have booted several > boxes that have more. There's going to be more than 1 comma. > The commas are calculated by formula. (DIV_ROUND_UP(nr_cpu_ids, 4)) > > > > 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 > > Cc: Steven 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..6750d05 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); > > + /* Bitmap, ',' and two more bytes for the newline and '\0'. */ > > + len = DIV_ROUND_UP(nr_cpu_ids, 4) + nr_cpu_ids/32 + 2; > > This is broken. Instead do: > > len = snprintf(NULL, 0, "%*pb\n", > cpumask_pr_args(tr->tracing_cpumask)) + 1; > > mask_str = kmalloc(len, GFP_KERNEL); > [..] > len = snprintf(mask_str, len, "%*pb\n", > cpumask_pr_args(tr->tracing_cpumask)); > > -- Steve > hmm. I never know that snprintf has such usage. This is much better than calculating it by a formula. > > + 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; > -- Thanks, Changbin Du From 1585459079499409781@xxx Thu Nov 30 03:13:23 +0000 2017 X-GM-THRID: 1585374689803176997 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread