linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Howard Chu <howardchu95@gmail.com>
Cc: Benjamin Peterson <benjamin@engflow.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	"Liang, Kan" <kan.liang@linux.intel.com>,
	"open list:PERFORMANCE EVENTS SUBSYSTEM"
	<linux-perf-users@vger.kernel.org>,
	"open list:PERFORMANCE EVENTS SUBSYSTEM"
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] perf trace: avoid garbage when not printing a trace event's arguments
Date: Fri, 1 Nov 2024 18:30:40 -0300	[thread overview]
Message-ID: <ZyVIgKcAuqZSYYB1@x1> (raw)
In-Reply-To: <CAH0uvohNO517GAD7XH-VvUvU+dPdeZBKDw0Teij50sAPSe9sCQ@mail.gmail.com>

On Fri, Nov 01, 2024 at 02:00:46PM -0700, Howard Chu wrote:
> Hello Benjamin,
> 
> Before your patch:
> 
> perf $ ./perf trace -e net:netif_rx_exit
>      0.000 irq/79-brcmf_p/1694977 net:netif_rx_exit(6n<)
>     28.153 irq/79-brcmf_p/1694977 net:netif_rx_exit(6n<)
>     36.429 irq/79-brcmf_p/1694977 net:netif_rx_exit(6n<)
>     36.461 irq/79-brcmf_p/1694977 net:netif_rx_exit(6n<)
> 
> After:
> 
> perf $ ./perf trace -e net:netif_rx_exit
>      0.000 irq/79-brcmf_p/1694977 net:netif_rx_exit()
>      7.352 irq/79-brcmf_p/1694977 net:netif_rx_exit()
>     30.232 irq/79-brcmf_p/1694977 net:netif_rx_exit()
>     37.529 irq/79-brcmf_p/1694977 net:netif_rx_exit()
> 
> It works beautifully, but I'm thinking can we simplify it by just doing:
> 
> +       char bf[2048] = { 0 };
>         size_t size = sizeof(bf);
> 
> That being said, trace__fprintf_tp_fields() should return void because
> currently its return value is not used by any other functions, plus
> this line:
> 
> return printed + fprintf(trace->output, "%s", bf);
> 
> in trace__fprintf_tp_fields() does not make much sense, since that
> will be 2 * printed. So I'm thinking maybe:
> 
> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> index 748b061f8678..8a628d1e80f3 100644
> --- a/tools/perf/builtin-trace.c
> +++ b/tools/perf/builtin-trace.c
> @@ -3021,10 +3021,10 @@ static void bpf_output__fprintf(struct trace *trace,
>         ++trace->nr_events_printed;
>  }
> 
> -static size_t trace__fprintf_tp_fields(struct trace *trace, struct
> evsel *evsel, struct perf_sample *sample,
> +static void trace__fprintf_tp_fields(struct trace *trace, struct
> evsel *evsel, struct perf_sample *sample,
>                                        struct thread *thread, void
> *augmented_args, int augmented_args_size)
>  {
> -       char bf[2048];
> +       char bf[2048] = { 0 };
>         size_t size = sizeof(bf);
>         struct tep_format_field *field = evsel->tp_format->format.fields;
>         struct syscall_arg_fmt *arg = __evsel__syscall_arg_fmt(evsel);
> @@ -3087,7 +3087,7 @@ static size_t trace__fprintf_tp_fields(struct
> trace *trace, struct evsel *evsel,
>                 printed += syscall_arg_fmt__scnprintf_val(arg, bf +
> printed, size - printed, &syscall_arg, val);
>         }
> 
> -       return printed + fprintf(trace->output, "%s", bf);
> +       fprintf(trace->output, "%s", bf);
>  }
> 
>  static int trace__event_handler(struct trace *trace, struct evsel *evsel,
> 
> 
> Then this will be one more diff line than yours.
> 
> But that's just my opinion, yours works just fine, thank you :).
> Arnaldo, what do you think?
> 
> Tested-by: Howard Chu <howardchu95@gmail.com>

I haven't tested it yet, just in my mind :-)

The patch looks ok and seems to fix a real problem, my only concern, a
pet peeve, was that it, in addition to fixing a real problem, did an
unrelated change, the "Remove the return value...", that part looks like
a distraction, something that shouldn't be there.

- Arnaldo
 
> Thanks,
> Howard
> 
> On Fri, Nov 1, 2024 at 10:27 AM Benjamin Peterson <benjamin@engflow.com> wrote:
> >
> > trace__fprintf_tp_fields may not print any tracepoint arguments. E.g., if the
> > argument values are all zero. Previously, this would result in a totally
> > uninitialized buffer being passed to fprintf, which could lead to garbage on the
> > console. Fix the problem by passing the number of initialized bytes fprintf.
> >
> > Remove the return value of trace__fprintf_tp_fields, since it was meaningless
> > and ignored.
> >
> > Fixes: f11b2803bb88 ("perf trace: Allow choosing how to augment the tracepoint arguments")
> > Signed-off-by: Benjamin Peterson <benjamin@engflow.com>
> > ---
> >  tools/perf/builtin-trace.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> > index d3f11b90d025..4e785ea29df6 100644
> > --- a/tools/perf/builtin-trace.c
> > +++ b/tools/perf/builtin-trace.c
> > @@ -3021,7 +3021,7 @@ static void bpf_output__fprintf(struct trace *trace,
> >         ++trace->nr_events_printed;
> >  }
> >
> > -static size_t trace__fprintf_tp_fields(struct trace *trace, struct evsel *evsel, struct perf_sample *sample,
> > +static void trace__fprintf_tp_fields(struct trace *trace, struct evsel *evsel, struct perf_sample *sample,
> >                                        struct thread *thread, void *augmented_args, int augmented_args_size)
> >  {
> >         char bf[2048];
> > @@ -3087,7 +3087,7 @@ static size_t trace__fprintf_tp_fields(struct trace *trace, struct evsel *evsel,
> >                 printed += syscall_arg_fmt__scnprintf_val(arg, bf + printed, size - printed, &syscall_arg, val);
> >         }
> >
> > -       return printed + fprintf(trace->output, "%s", bf);
> > +       fprintf(trace->output, "%.*s", (int)printed, bf);
> >  }
> >
> >  static int trace__event_handler(struct trace *trace, struct evsel *evsel,
> > --
> > 2.39.5
> >

  reply	other threads:[~2024-11-01 21:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-01  0:53 [PATCH] perf trace: avoid garbage when not printing an trace event's arguments Benjamin Peterson
2024-11-01  2:03 ` Howard Chu
2024-11-01 17:26   ` Benjamin Peterson
2024-11-01 17:27 ` [PATCH v2] perf trace: avoid garbage when not printing a " Benjamin Peterson
2024-11-01 21:00   ` Howard Chu
2024-11-01 21:30     ` Arnaldo Carvalho de Melo [this message]
2024-11-01 21:41       ` Benjamin Peterson
2024-11-02  0:38         ` Arnaldo Carvalho de Melo
2024-11-03 20:48           ` [PATCH v3] " Benjamin Peterson
2024-11-05 18:52             ` Arnaldo Carvalho de Melo
2024-11-06 17:03             ` Namhyung Kim
2024-11-03  4:54 ` [PATCH] perf trace: avoid garbage when not printing an " kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZyVIgKcAuqZSYYB1@x1 \
    --to=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=benjamin@engflow.com \
    --cc=howardchu95@gmail.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).