Message ID | 20230730222134.54547-1-jim.cromie@gmail.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp1725920vqg; Sun, 30 Jul 2023 17:37:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlFnpXV/IQ/ShdMhDHW/ETXGGrxnADS20xnoUbTIL1RWodWaGLrYbtwZ30u9mw/qGGd03X0R X-Received: by 2002:a05:6a20:12d6:b0:133:6816:c889 with SMTP id v22-20020a056a2012d600b001336816c889mr11161840pzg.40.1690763875684; Sun, 30 Jul 2023 17:37:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690763875; cv=none; d=google.com; s=arc-20160816; b=RNSZuiRrYyLrzxfJeX7SVvDe3v1YbHkTvjtyrp/Yp7fp33U4XbuAVyAsND4DONo54u AL8NVUelxUYQye6UC1JvyaFdGwapIUaqiXr0Rlv6kul65xz0iTGxxISyGiNaQ14s0mSA Kg7D+j/mSijnMm+sNzdOCJNNsHfyYs3POVlPhFZ1zZTAYHt/Nv9jOPFGzbuSZUXgyetN P470RdKzh3Sxt9FYqRP1G6GszPereFn1fDzUwAPL5NJzU0TloDzr+ZlJKpV/e8V0Z4HY JvJ8WiCDaqFppSLaPNv9OC8mjkQ7wDl2BSag6GnYDvmrV8+my6B3qjF35lMWCldEQSHj nBJQ== 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=v6sXvL+4NhCJx1KHyGUnBGwLgagdAPRwzE5mdbZT8XY=; fh=rNWHzhIkBkiOVhM7lvSXnXhCnKSiUUgsGv3xSiLgtvg=; b=jlEjVkmXq6558a/WbQ3HUtlJSJ+dgoJdFqzDI+ec2mQtp+rJn8jkmIAfg4u3+QMima GFZcuJkasImltjf9lWh/Lk5kOQc8nOMlKGHddQWO1V0YnURJ2bGSZrBn7JWfouyskcA8 SKCvNVWmGsLFIvgYedhNHhiay/fC8VYCTIE3NZA2AaX262IBnPhjqFUmvKp/HE3Hmug+ EsjUO89EmBHiM9rSEFtQImH6e8HhNTmuWx7kkB6tgiaCXtRqqx7L1JJ48xyi0l6ELRjz k+qv8xUrjv9putSJUp3mqXBDv9gPiyQfbr4+UrCvoYspierUPxEl7z5Zso4C6LUBMIKz Udlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Tkd4q14q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bq26-20020a056a000e1a00b006871a05bb91si3266448pfb.367.2023.07.30.17.37.38; Sun, 30 Jul 2023 17:37:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Tkd4q14q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229731AbjG3WVn (ORCPT <rfc822;dengxinlin2429@gmail.com> + 99 others); Sun, 30 Jul 2023 18:21:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjG3WVl (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 30 Jul 2023 18:21:41 -0400 Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 986D910F2 for <linux-kernel@vger.kernel.org>; Sun, 30 Jul 2023 15:21:40 -0700 (PDT) Received: by mail-io1-xd29.google.com with SMTP id ca18e2360f4ac-7907d03b0aeso90640339f.3 for <linux-kernel@vger.kernel.org>; Sun, 30 Jul 2023 15:21:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690755700; x=1691360500; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=v6sXvL+4NhCJx1KHyGUnBGwLgagdAPRwzE5mdbZT8XY=; b=Tkd4q14qRESRdFYnJY9kblnFhjZ/tWbOiMW3zdK4uKrWZol5LSkDkmdbbEa6kxHPxD MUN5lmdzRxhOJ01lgiiWU7zWaQB4LnVAiA7sWLGB5ahhwEKgr1FtDmy70fRNBgz2Jd9F g3NjYcISXfsAH4yLnrskPQ5cLONz+n5+1Q55EpXt4mGpqGnxS1NJRr6eAG90bAn6GyTZ XxjM6AKeUTSeRiLzA439n3Z8+uCxPzN381ES7M0T1oo3zTLdoX/6kpGapcnIiH2HFHN/ QsKkYe2IpUpwWLKQaX825z7WOe04JtnWzOZ3aCS7tm7doU7OX2RFRUY/68XRGNpz331u vMPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690755700; x=1691360500; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=v6sXvL+4NhCJx1KHyGUnBGwLgagdAPRwzE5mdbZT8XY=; b=HlXn2Dhh4VbU0sYqt1h/TnwTY6LCiOENpryc1MZyy1UmnKFJBi75iQQoNQdu+7kjiF zCtygRsYZFJEXpydhpGQtnGxqTEhwYiwXV3Z1t8Pl8xrDsRRX2HQ2MYJ4Hiq+1NUI/Ik bU5PEBPoN83hy94ExcFQWaHWxrUHyX4LpPFgQ1qJKkIBn+aD2HY068UZucFlh2ouVt53 6519H0G0VcHat955mctR1J+f51mWO89ktCSCGRenyucJtOUFVTvqCEj4dLFD59MKTRNB kcjwyhh1vgxLxMvB5fYggWel2VpHS5e/FnFCzI4bB9h6v/rt/+QETT3OjYVdvqX3tcBE FPYg== X-Gm-Message-State: ABy/qLa1aaFPsV7i6kgy/XSE6gQbvd1Zu8VXXHKlXyeL2n2whjlyE1ic i5ix/zinPAbff0h5R27EzLY= X-Received: by 2002:a5e:9810:0:b0:783:49ec:8115 with SMTP id s16-20020a5e9810000000b0078349ec8115mr6891087ioj.5.1690755699833; Sun, 30 Jul 2023 15:21:39 -0700 (PDT) Received: from frodo.. (c-73-78-62-130.hsd1.co.comcast.net. [73.78.62.130]) by smtp.googlemail.com with ESMTPSA id y11-20020a6bd80b000000b00786582eb9fbsm2801056iob.15.2023.07.30.15.21.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jul 2023 15:21:39 -0700 (PDT) From: Jim Cromie <jim.cromie@gmail.com> To: rostedt@goodmis.org, linux-kernel@vger.kernel.org Cc: Jim Cromie <jim.cromie@gmail.com>, Vincent Whitchurch <vincent.whitchurch@axis.com>, daniel@ffwll.ch, pmladek@suse.com, sergey.senozhatsky@gmail.com, john.ogness@linutronix.de, Simon Ser <contact@emersion.fr>, Sean Paul <seanpaul@chromium.org> Subject: [RFC PATCH 1/1] tracefs: add TP_printk_no_nl - RFC Date: Sun, 30 Jul 2023 16:21:34 -0600 Message-ID: <20230730222134.54547-1-jim.cromie@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772894421558971293 X-GMAIL-MSGID: 1772894421558971293 |
Series |
[RFC,1/1] tracefs: add TP_printk_no_nl - RFC
|
|
Commit Message
Jim Cromie
July 30, 2023, 10:21 p.m. UTC
This variant of TP_printk() does *not* add the trailing newline.
https://lore.kernel.org/lkml/20200825153338.17061-1-vincent.whitchurch@axis.com/
taught dyndbg to send pr_debug() msgs to tracefs, via -x/T flag.
It "reused" the include/trace/events/printk.h console event,
which does the following:
TP_fast_assign(
/*
* Each trace entry is printed in a new line.
* If the msg finishes with '\n', cut it off
* to avoid blank lines in the trace.
*/
if ((len > 0) && (text[len-1] == '\n'))
len -= 1;
memcpy(__get_str(msg), text, len);
__get_str(msg)[len] = 0;
),
That trim work could be avoided, *iff* all pr_debug() callers are
known to have no '\n' to strip. While thats not true for *all*
callsites, it is 99+% true for DRM.debug callsites, and can be made
true for some subsets of prdbg/dyndbg callsites.
WANTED: macros to validate that a literal format-str has or doesn't
have a trailing newline, or to provide or trim trailing newline(s?).
Should be usable in TP_printk* defns, for use in new event defns.
Cc: <rostedt@goodmis.org>
Cc: Vincent Whitchurch <vincent.whitchurch@axis.com>
Cc: <daniel@ffwll.ch>
Cc: <pmladek@suse.com>
Cc: <sergey.senozhatsky@gmail.com>
Cc: <john.ogness@linutronix.de>
Cc: Simon Ser <contact@emersion.fr>
Cc: Sean Paul <seanpaul@chromium.org>
Signed-off-by: Jim Cromie <jim.cromie@gmail.com>
---
include/trace/stages/stage3_trace_output.h | 3 +++
include/trace/stages/stage7_class_define.h | 3 +++
2 files changed, 6 insertions(+)
Comments
On 2023-07-30, Jim Cromie <jim.cromie@gmail.com> wrote: > This variant of TP_printk() does *not* add the trailing newline. > > https://lore.kernel.org/lkml/20200825153338.17061-1-vincent.whitchurch@axis.com/ > > taught dyndbg to send pr_debug() msgs to tracefs, via -x/T flag. > > It "reused" the include/trace/events/printk.h console event, > which does the following: > > TP_fast_assign( > /* > * Each trace entry is printed in a new line. > * If the msg finishes with '\n', cut it off > * to avoid blank lines in the trace. > */ > if ((len > 0) && (text[len-1] == '\n')) > len -= 1; > > memcpy(__get_str(msg), text, len); > __get_str(msg)[len] = 0; > ), > > That trim work could be avoided, *iff* all pr_debug() callers are > known to have no '\n' to strip. While thats not true for *all* > callsites, it is 99+% true for DRM.debug callsites, and can be made > true for some subsets of prdbg/dyndbg callsites. Note that the trailing '\n' in printk messages has a purpose. It finalizes commitment of the message to the ringbuffer so that the message cannot be extended by any LOG_CONT printk's that may come along (from any context). If it is not intended that a message is extended, then that message should include a trailing '\n'. Grepping through the kernel, I am surprised how many messages are missing the trailing newline when it is obvious they are not intended to be extended. I consider these bugs. John Ogness
On Mon 2023-07-31 10:31:03, John Ogness wrote: > On 2023-07-30, Jim Cromie <jim.cromie@gmail.com> wrote: > > This variant of TP_printk() does *not* add the trailing newline. > > > > https://lore.kernel.org/lkml/20200825153338.17061-1-vincent.whitchurch@axis.com/ > > > > taught dyndbg to send pr_debug() msgs to tracefs, via -x/T flag. > > > > It "reused" the include/trace/events/printk.h console event, > > which does the following: > > > > TP_fast_assign( > > /* > > * Each trace entry is printed in a new line. > > * If the msg finishes with '\n', cut it off > > * to avoid blank lines in the trace. > > */ > > if ((len > 0) && (text[len-1] == '\n')) > > len -= 1; > > > > memcpy(__get_str(msg), text, len); > > __get_str(msg)[len] = 0; > > ), > > > > That trim work could be avoided, *iff* all pr_debug() callers are > > known to have no '\n' to strip. While thats not true for *all* > > callsites, it is 99+% true for DRM.debug callsites, and can be made > > true for some subsets of prdbg/dyndbg callsites. > > Note that the trailing '\n' in printk messages has a purpose. It > finalizes commitment of the message to the ringbuffer so that the > message cannot be extended by any LOG_CONT printk's that may come along > (from any context). Also the message is printed on consoles only when finalized. As a result, the last non-finalized message might not be visible on consoles for hours or days. > If it is not intended that a message is extended, then that message > should include a trailing '\n'. > > Grepping through the kernel, I am surprised how many messages are > missing the trailing newline when it is obvious they are not intended to > be extended. I consider these bugs. I consider this bug as well. My understanding is that this patch affects only messages printed to the ftrace buffer so that it does not affect printk() directly. But still. It creates a habit that is bad for printk(). Also relies on the fact that people are aware of this macro and use it. IMHO, it will not work in practice. Or do I miss something? Best Regards, Petr
On Mon, 31 Jul 2023 12:21:29 +0200 Petr Mladek <pmladek@suse.com> wrote: > > Grepping through the kernel, I am surprised how many messages are > > missing the trailing newline when it is obvious they are not intended to > > be extended. I consider these bugs. > > I consider this bug as well. > > My understanding is that this patch affects only messages printed to > the ftrace buffer so that it does not affect printk() directly. > > But still. It creates a habit that is bad for printk(). Also relies > on the fact that people are aware of this macro and use it. IMHO, > it will not work in practice. Or do I miss something? I believe that the problem Jim is addressing is that there's printk() messages that also go to a tracepoint() function. The tracepoint function will add its own '\n' to the message, and now we have two '\n's and this causes extra spaces in the output of the ftrace ring buffer. Perhaps what we should do is to make sure that the output doesn't add more newlines than just one. Something like this: (totally untested) diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index db575094c498..a1b73ffa1552 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -333,6 +333,8 @@ int trace_output_raw(struct trace_iterator *iter, char *name, trace_seq_printf(s, "%s: ", name); trace_seq_vprintf(s, trace_event_format(iter, fmt), ap); + trace_seq_trim_newlines(s); + trace_seq_putc(s, '\n'); return trace_handle_return(s); } diff --git a/kernel/trace/trace_seq.c b/kernel/trace/trace_seq.c index bac06ee3b98b..d4e2049809e3 100644 --- a/kernel/trace/trace_seq.c +++ b/kernel/trace/trace_seq.c @@ -221,6 +221,22 @@ void trace_seq_puts(struct trace_seq *s, const char *str) } EXPORT_SYMBOL_GPL(trace_seq_puts); +/** + * trace_seq_trim_newlines - remove ending newlines + * @s: trace sequence descriptor + * + * Remove ending newlines from the buffer. + */ +void trace_seq_trim_newlines(struct trace_seq *s) +{ + if (unlikely(!s->seq.size || !s->seq.len)) + return; + + while (s->seq.len && s->buffer[s->seq.len - 1] == '\n') { + s->seq.len--; + } +} + /** * trace_seq_putc - trace sequence printing of simple character * @s: trace sequence descriptor A similar thing would need to go into libtraceevent. -- Steve
On Mon, Jul 31, 2023 at 9:21 AM Steven Rostedt <rostedt@goodmis.org> wrote: > > On Mon, 31 Jul 2023 12:21:29 +0200 > Petr Mladek <pmladek@suse.com> wrote: > > > > Grepping through the kernel, I am surprised how many messages are > > > missing the trailing newline when it is obvious they are not intended to > > > be extended. I consider these bugs. > > > > I consider this bug as well. > > agreed. I blame pr_cont() at least in part. Note - I have submitted drm_dbg: add trailing newlines where missing https://patchwork.freedesktop.org/series/121583/ it appears to have failed - on what look like unrelated failures - pipe etc > > My understanding is that this patch affects only messages printed to > > the ftrace buffer so that it does not affect printk() directly. > > > > But still. It creates a habit that is bad for printk(). Also relies > > on the fact that people are aware of this macro and use it. IMHO, > > it will not work in practice. Or do I miss something? something like (opposite of) this, used inside pr_debug, would force all callers to add \n. #include <assert.h> #define FAIL_IF_TRAILING_NEWLINE(str) \ _Static_assert((str[sizeof(str) - 2] != '\n'), "String literal has a trailing newline") obviously, Inverted, gentler versions are perhaps better. > > I believe that the problem Jim is addressing is that there's printk() > messages that also go to a tracepoint() function. The tracepoint function > will add its own '\n' to the message, and now we have two '\n's and this > causes extra spaces in the output of the ftrace ring buffer. Yes. only quibble - there arent yet, there could be. Vincents patch didnt go in, Im trying to get it ready for another submit. I uncovered this bit on the way, figured I could turn it into a question, and maybe a little conceptual buy-in. This is a 0 or 1 "\n" problem, quietly accepting 25 in a row could paper over another problem. maybe complain-once after 2-3 ? TP_printk_no_nl doesnt solve all problems - it could work for adding DRM.debug trace-events, cuz the callsites have few of the bad-habits noted above, *very* few drm_dbg()s without trailing \n. - presuming theyre fixable. fixing the larger body of pr_debug()s is quite a nuisance, and if dyndbg -T flag sends them, you'd get the larger varying population of \n's So some real-time stripping /adding is needed for some populations of pr_debugs the no_nl macro would at least allow event-definers to assert that theyre sending their own. > > Perhaps what we should do is to make sure that the output doesn't add more > newlines than just one. > > Something like this: (totally untested) > > diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c > index db575094c498..a1b73ffa1552 100644 > --- a/kernel/trace/trace_output.c > +++ b/kernel/trace/trace_output.c > @@ -333,6 +333,8 @@ int trace_output_raw(struct trace_iterator *iter, char *name, > > trace_seq_printf(s, "%s: ", name); > trace_seq_vprintf(s, trace_event_format(iter, fmt), ap); > + trace_seq_trim_newlines(s); > + trace_seq_putc(s, '\n'); > > return trace_handle_return(s); > } > diff --git a/kernel/trace/trace_seq.c b/kernel/trace/trace_seq.c > index bac06ee3b98b..d4e2049809e3 100644 > --- a/kernel/trace/trace_seq.c > +++ b/kernel/trace/trace_seq.c > @@ -221,6 +221,22 @@ void trace_seq_puts(struct trace_seq *s, const char *str) > } > EXPORT_SYMBOL_GPL(trace_seq_puts); > > +/** > + * trace_seq_trim_newlines - remove ending newlines > + * @s: trace sequence descriptor > + * > + * Remove ending newlines from the buffer. > + */ > +void trace_seq_trim_newlines(struct trace_seq *s) > +{ > + if (unlikely(!s->seq.size || !s->seq.len)) > + return; > + > + while (s->seq.len && s->buffer[s->seq.len - 1] == '\n') { > + s->seq.len--; > + } > +} > + > /** > * trace_seq_putc - trace sequence printing of simple character > * @s: trace sequence descriptor > > > > A similar thing would need to go into libtraceevent. > > -- Steve
diff --git a/include/trace/stages/stage3_trace_output.h b/include/trace/stages/stage3_trace_output.h index c1fb1355d309..5f5c1374fa10 100644 --- a/include/trace/stages/stage3_trace_output.h +++ b/include/trace/stages/stage3_trace_output.h @@ -8,6 +8,9 @@ #undef TP_printk #define TP_printk(fmt, args...) fmt "\n", args +#undef TP_printk_no_nl +#define TP_printk_no_nl(fmt, args...) fmt, args + #undef __get_dynamic_array #define __get_dynamic_array(field) \ ((void *)__entry + (__entry->__data_loc_##field & 0xffff)) diff --git a/include/trace/stages/stage7_class_define.h b/include/trace/stages/stage7_class_define.h index bcb960d16fc0..8247e4478f19 100644 --- a/include/trace/stages/stage7_class_define.h +++ b/include/trace/stages/stage7_class_define.h @@ -37,3 +37,6 @@ #undef TP_printk #define TP_printk(fmt, args...) "\"" fmt "\", " __stringify(args) + +#undef TP_printk_no_nl +#define TP_printk_no_nl(fmt, args...) "\"" fmt "\", " __stringify(args)