Message ID | bbbab32e3e104bdc2238724a6a4a85e539f49ddd.1698512661.git.christophe.jaillet@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1317535vqb; Sat, 28 Oct 2023 10:05:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEdke6EM+Vg00QuD+U6he0y4zRssuiBMpxPBkvzdZNrEyT6krSCqzEEMBoliXjJjIQGDlih X-Received: by 2002:a05:6a00:1a04:b0:68f:c215:a825 with SMTP id g4-20020a056a001a0400b0068fc215a825mr5408622pfv.12.1698512713823; Sat, 28 Oct 2023 10:05:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698512713; cv=none; d=google.com; s=arc-20160816; b=Kh9qor17t+gK2nTPa2sAE1lFhf4vs91+ZbS73P/7o0NqQpYN0n/L8fV4TvmNLCzi/6 7Y4Mjc5YHGtjTWqHVZdfusPZJfZxhK30GTclei7+cHmULY4zqIvtHKjAw5qGykkTeqlJ Tc+xwgJbtWo/3CiHV76p00wBbOVrsylk5FMucCkZzRoxnrHQdaCqfZqQiTAfV17SF+my XrkKOBjtj2Gd5pffAfeSfQK/BnP6O+UDya2yxbfHXuxgMObKF7/dzZyZc3hQ3HP2OCmf dLmNTYJvS8jEWiFXmScSdz46tfQYCdmu6ES+FbALsXtBbKDBo2TxYCdeI0P11n+6PgC1 RJlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=u3XpE1EDC+9oMRTYO51KW7vHfl6tXKZ9dZgi4Ew6grU=; fh=AR/Qp4o9SmokTbFyeO+0r0G6A1u9G0b1cf8RunrmoCg=; b=cOSgQ2metXk4VG7A7nI0URwVFsfuZevg+g9hW/ksJNe9AlWMUMJhTAXZz+OU7vxPEf Bj5hAEkht6Jb/CCtJM7HRI5tOVm18nqYTreOHOq1yIwbJBld9f7iwEwKvcsZbnsOcGRn CTiPxYH56MoQMFRLeXHUzOsEpaF2a0sXn7eEmKgKrCelXoBr0dHI0bUKh6Fh96uAoKs6 6N/4pv6BQcrTsXu4E8JBsptGpeU8dQ2ZZEmao55LH+r3uiGt+dq/awfjxoTrd88KkKmR nV8vMV0cgFsmixK6j06Z5CwCVWpS3z60XngSmNXpsyuC3q6xyrB5QVwbeLy1DqoW3+CC rBUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=FFFaV4Gt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id s83-20020a632c56000000b005b8eacb29c1si2577752pgs.437.2023.10.28.10.05.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 10:05:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=FFFaV4Gt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id EFCCB8051177; Sat, 28 Oct 2023 10:05:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229479AbjJ1RE6 (ORCPT <rfc822;peter110.wang@gmail.com> + 28 others); Sat, 28 Oct 2023 13:04:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229446AbjJ1RE4 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 28 Oct 2023 13:04:56 -0400 Received: from smtp.smtpout.orange.fr (smtp-20.smtpout.orange.fr [80.12.242.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2832DE for <linux-kernel@vger.kernel.org>; Sat, 28 Oct 2023 10:04:53 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id wmjwqs5Qa8ZI8wmjxqgmOJ; Sat, 28 Oct 2023 19:04:51 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1698512691; bh=u3XpE1EDC+9oMRTYO51KW7vHfl6tXKZ9dZgi4Ew6grU=; h=From:To:Cc:Subject:Date; b=FFFaV4Gty5EI+wmhvij8pSq4owQ4Xx9sdgwF4xBqKES36Ps0obVUkc1iiRDUl/QGj sGZxd6YF/cuLKS/bs+TDwAIHE7+W6luyIIxjK6pyXFCido6AvaqDjTK+WOFtmPso96 gx1/8xhj5yheGPzZkPggN7b/uds8sMfo+D5LXKDOWnmA7n7laLQCmYQj6BAwIprPZi qW8pIBXHCTavVb7sROXP82UcJP1zcCbnjMKOhoYutKQCMkW3Kx0fEaMgdv1JPtzcy6 hRkjfo40TWJc6fMbM4yHziAvhHNEBknVMsCtecdshmPDUSRttNInYe7uQ0fYlecPHo AspLu+cDjMoGw== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sat, 28 Oct 2023 19:04:51 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: Davidlohr Bueso <dave@stgolabs.net>, "Paul E. McKenney" <paulmck@kernel.org>, Josh Triplett <josh@joshtriplett.org>, Frederic Weisbecker <frederic@kernel.org>, Neeraj Upadhyay <quic_neeraju@quicinc.com>, Joel Fernandes <joel@joelfernandes.org>, Boqun Feng <boqun.feng@gmail.com>, Steven Rostedt <rostedt@goodmis.org>, Mathieu Desnoyers <mathieu.desnoyers@efficios.com>, Lai Jiangshan <jiangshanlai@gmail.com>, Zqiang <qiang.zhang1211@gmail.com> Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET <christophe.jaillet@wanadoo.fr>, rcu@vger.kernel.org Subject: [PATCH] refscale: Optimize process_durations() Date: Sat, 28 Oct 2023 19:04:44 +0200 Message-Id: <bbbab32e3e104bdc2238724a6a4a85e539f49ddd.1698512661.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Sat, 28 Oct 2023 10:05:11 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781019667460784900 X-GMAIL-MSGID: 1781019667460784900 |
Series |
refscale: Optimize process_durations()
|
|
Commit Message
Christophe JAILLET
Oct. 28, 2023, 5:04 p.m. UTC
process_durations() is not a hot path, but there is no good reason to
iterate over and over the data already in 'buf'.
Using a seq_buf saves some useless strcat() and the need of a temp buffer.
Data is written directly at the correct place.
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
kernel/rcu/refscale.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
Comments
On Sat, 28 Oct 2023 19:04:44 +0200 Christophe JAILLET <christophe.jaillet@wanadoo.fr> wrote: > process_durations() is not a hot path, but there is no good reason to > iterate over and over the data already in 'buf'. > > Using a seq_buf saves some useless strcat() and the need of a temp buffer. > Data is written directly at the correct place. > Agreed. > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > kernel/rcu/refscale.c | 20 +++++++++++++------- > 1 file changed, 13 insertions(+), 7 deletions(-) > > diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c > index 2c2648a3ad30..861485d865ec 100644 > --- a/kernel/rcu/refscale.c > +++ b/kernel/rcu/refscale.c > @@ -28,6 +28,7 @@ > #include <linux/rcupdate_trace.h> > #include <linux/reboot.h> > #include <linux/sched.h> > +#include <linux/seq_buf.h> > #include <linux/spinlock.h> > #include <linux/smp.h> > #include <linux/stat.h> > @@ -890,31 +891,36 @@ static u64 process_durations(int n) > { > int i; > struct reader_task *rt; > - char buf1[64]; > + struct seq_buf s; > char *buf; > u64 sum = 0; > > buf = kmalloc(800 + 64, GFP_KERNEL); > if (!buf) > return 0; > - buf[0] = 0; > + > + seq_buf_init(&s, buf, 800 + 64); > + > sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", > exp_idx); > > for (i = 0; i < n && !torture_must_stop(); i++) { > rt = &(reader_tasks[i]); > - sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); > > if (i % 5 == 0) > - strcat(buf, "\n"); > - if (strlen(buf) >= 800) { > + seq_buf_putc(&s, '\n'); I was confused here thinking it was originally adding a '\n' to buf1 on i % 5, but it's adding it to buf! Yeah, using seq_buf is much less confusing and then less error prone. Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org> -- Steve > + > + if (seq_buf_used(&s) >= 800) { > + seq_buf_terminate(&s); > pr_alert("%s", buf); > - buf[0] = 0; > + seq_buf_clear(&s); > } > - strcat(buf, buf1); > + > + seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns); > > sum += rt->last_duration_ns; > } > + seq_buf_terminate(&s); > pr_alert("%s\n", buf); > > kfree(buf);
On Sat, 28 Oct 2023, Christophe JAILLET wrote: >process_durations() is not a hot path, but there is no good reason to >iterate over and over the data already in 'buf'. > >Using a seq_buf saves some useless strcat() and the need of a temp buffer. >Data is written directly at the correct place. Makes sense. Reviewed-by: Davidlohr Bueso <dave@stgolabs.net>
On Mon, Oct 30, 2023 at 09:55:16AM -0700, Davidlohr Bueso wrote: > On Sat, 28 Oct 2023, Christophe JAILLET wrote: > > > process_durations() is not a hot path, but there is no good reason to > > iterate over and over the data already in 'buf'. > > > > Using a seq_buf saves some useless strcat() and the need of a temp buffer. > > Data is written directly at the correct place. > > Makes sense. > > Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> Queued and pushed, thank you all! Thanx, Paul
On Tue, Oct 31, 2023 at 11:21:14AM -0700, Paul E. McKenney wrote: > On Mon, Oct 30, 2023 at 09:55:16AM -0700, Davidlohr Bueso wrote: > > On Sat, 28 Oct 2023, Christophe JAILLET wrote: > > > > > process_durations() is not a hot path, but there is no good reason to > > > iterate over and over the data already in 'buf'. > > > > > > Using a seq_buf saves some useless strcat() and the need of a temp buffer. > > > Data is written directly at the correct place. > > > > Makes sense. > > > > Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> > > Queued and pushed, thank you all! But an allmodconfig build complains about seq_buf_putc() being undefined, that is, not exported. I suspect that other seq_buf_*() functions in this patch might also be complained about. I am dropping this for the moment. Please make it pass an allmodconfig build so that I can pull it in again. Please see below for the commit. Thanx, Paul ------------------------------------------------------------------------ commit a1ef9b4cff53c509f412c354c715449d7f2e159b Author: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Date: Sat Oct 28 19:04:44 2023 +0200 refscale: Optimize process_durations() The process_durations() function is not on a hot path, but there is still no good reason to iterate over and over the data already in 'buf', but this is exactly what the current use of strlen() and strcat() do. Using a seq_buf saves some useless strcat() and the need of a temp buffer. Data is written directly at the correct place. Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Reviewed-by: Steven Rostedt (Google) <rostedt@goodmis.org> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c index 2c2648a3ad30..861485d865ec 100644 --- a/kernel/rcu/refscale.c +++ b/kernel/rcu/refscale.c @@ -28,6 +28,7 @@ #include <linux/rcupdate_trace.h> #include <linux/reboot.h> #include <linux/sched.h> +#include <linux/seq_buf.h> #include <linux/spinlock.h> #include <linux/smp.h> #include <linux/stat.h> @@ -890,31 +891,36 @@ static u64 process_durations(int n) { int i; struct reader_task *rt; - char buf1[64]; + struct seq_buf s; char *buf; u64 sum = 0; buf = kmalloc(800 + 64, GFP_KERNEL); if (!buf) return 0; - buf[0] = 0; + + seq_buf_init(&s, buf, 800 + 64); + sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", exp_idx); for (i = 0; i < n && !torture_must_stop(); i++) { rt = &(reader_tasks[i]); - sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); if (i % 5 == 0) - strcat(buf, "\n"); - if (strlen(buf) >= 800) { + seq_buf_putc(&s, '\n'); + + if (seq_buf_used(&s) >= 800) { + seq_buf_terminate(&s); pr_alert("%s", buf); - buf[0] = 0; + seq_buf_clear(&s); } - strcat(buf, buf1); + + seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns); sum += rt->last_duration_ns; } + seq_buf_terminate(&s); pr_alert("%s\n", buf); kfree(buf);
Le 31/10/2023 à 23:47, Paul E. McKenney a écrit : > On Tue, Oct 31, 2023 at 11:21:14AM -0700, Paul E. McKenney wrote: >> On Mon, Oct 30, 2023 at 09:55:16AM -0700, Davidlohr Bueso wrote: >>> On Sat, 28 Oct 2023, Christophe JAILLET wrote: >>> >>>> process_durations() is not a hot path, but there is no good reason to >>>> iterate over and over the data already in 'buf'. >>>> >>>> Using a seq_buf saves some useless strcat() and the need of a temp buffer. >>>> Data is written directly at the correct place. >>> >>> Makes sense. >>> >>> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> >> >> Queued and pushed, thank you all! > > But an allmodconfig build complains about seq_buf_putc() being undefined, > that is, not exported. I suspect that other seq_buf_*() functions in > this patch might also be complained about. > > I am dropping this for the moment. Please make it pass an allmodconfig > build so that I can pull it in again. Please see below for the commit. > > Thanx, Paul > Ouch! seq_buf_init(), seq_buf_terminate(), seq_buf_clear() are inlined functions in a .h file, so shouldn't be a problem. seq_buf_printf() is exported, but seq_buf_putc() is not! Really odd to me. Kees Cook (added in cc) suggests to use this API (see [1]) to avoid some potential issues and ease the management of NULL terminated strings in buffers. (#LinuxHardening). I'll propose to add the missing EXPORT_SYMBOL_GPL. CJ [1]: https://lore.kernel.org/all/202310241629.0A4206316F@keescook/
On Wed, Nov 01, 2023 at 08:41:39AM +0100, Christophe JAILLET wrote: > Le 31/10/2023 à 23:47, Paul E. McKenney a écrit : > > On Tue, Oct 31, 2023 at 11:21:14AM -0700, Paul E. McKenney wrote: > > > On Mon, Oct 30, 2023 at 09:55:16AM -0700, Davidlohr Bueso wrote: > > > > On Sat, 28 Oct 2023, Christophe JAILLET wrote: > > > > > > > > > process_durations() is not a hot path, but there is no good reason to > > > > > iterate over and over the data already in 'buf'. > > > > > > > > > > Using a seq_buf saves some useless strcat() and the need of a temp buffer. > > > > > Data is written directly at the correct place. > > > > > > > > Makes sense. > > > > > > > > Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> > > > > > > Queued and pushed, thank you all! > > > > But an allmodconfig build complains about seq_buf_putc() being undefined, > > that is, not exported. I suspect that other seq_buf_*() functions in > > this patch might also be complained about. > > > > I am dropping this for the moment. Please make it pass an allmodconfig > > build so that I can pull it in again. Please see below for the commit. > > Ouch! Believe me, I know that feeling! ;-) > seq_buf_init(), seq_buf_terminate(), seq_buf_clear() are inlined functions > in a .h file, so shouldn't be a problem. > > seq_buf_printf() is exported, but seq_buf_putc() is not! > Really odd to me. > > Kees Cook (added in cc) suggests to use this API (see [1]) to avoid some > potential issues and ease the management of NULL terminated strings in > buffers. (#LinuxHardening). > > I'll propose to add the missing EXPORT_SYMBOL_GPL. Very good, looking forward to seeing the result. Thanx, Paul > CJ > > [1]: https://lore.kernel.org/all/202310241629.0A4206316F@keescook/ >
diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c index 2c2648a3ad30..861485d865ec 100644 --- a/kernel/rcu/refscale.c +++ b/kernel/rcu/refscale.c @@ -28,6 +28,7 @@ #include <linux/rcupdate_trace.h> #include <linux/reboot.h> #include <linux/sched.h> +#include <linux/seq_buf.h> #include <linux/spinlock.h> #include <linux/smp.h> #include <linux/stat.h> @@ -890,31 +891,36 @@ static u64 process_durations(int n) { int i; struct reader_task *rt; - char buf1[64]; + struct seq_buf s; char *buf; u64 sum = 0; buf = kmalloc(800 + 64, GFP_KERNEL); if (!buf) return 0; - buf[0] = 0; + + seq_buf_init(&s, buf, 800 + 64); + sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", exp_idx); for (i = 0; i < n && !torture_must_stop(); i++) { rt = &(reader_tasks[i]); - sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); if (i % 5 == 0) - strcat(buf, "\n"); - if (strlen(buf) >= 800) { + seq_buf_putc(&s, '\n'); + + if (seq_buf_used(&s) >= 800) { + seq_buf_terminate(&s); pr_alert("%s", buf); - buf[0] = 0; + seq_buf_clear(&s); } - strcat(buf, buf1); + + seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns); sum += rt->last_duration_ns; } + seq_buf_terminate(&s); pr_alert("%s\n", buf); kfree(buf);