linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Like Xu <like.xu.linux@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>
Cc: Alexander Antonov <alexander.antonov@linux.intel.com>,
	Jiri Olsa <jolsa@redhat.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Stephane Eranian <eranian@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] perf iostat: Fix Segmentation fault from NULL 'struct perf_counts_values *'
Date: Mon, 27 Sep 2021 16:11:15 +0800	[thread overview]
Message-ID: <20210927081115.39568-2-likexu@tencent.com> (raw)
In-Reply-To: <20210927081115.39568-1-likexu@tencent.com>

From: Like Xu <likexu@tencent.com>

If the perf-iostat user specifies two or more iio_root_ports and
also specifies the cpu(s) by -C which is not *connected to all*
the above iio ports, the iostat_print_metric() will run into trouble:

For example:

 $ perf iostat list
 S0-uncore_iio_0<0000:16>
 S1-uncore_iio_0<0000:97> # <--- CPU 1 is located in the socket S0

 $ perf iostat 0000:16,0000:97 -C 1 -- ls
 port 	Inbound Read(MB)	Inbound Write(MB)	Outbound Read(MB)	Outbound
 Write(MB) ../perf-iostat: line 12: 104418 Segmentation fault
 (core dumped) perf stat --iostat$DELIMITER$*

The core-dump stack says, in the above corner case, the returned
(struct perf_counts_values *) count will be NULL, and the caller
iostat_print_metric() apparently doesn't not handle this case.

 433	struct perf_counts_values *count = perf_counts(evsel->counts, die, 0);
 434
 435	if (count->run && count->ena) {
 (gdb) p count
 $1 = (struct perf_counts_values *) 0x0

The deeper reason is that there are actually no statistics from the user
specified pair "iostat 0000:X, -C (disconnected) Y ", but let's fix it with
minimum cost by adding a NULL check in the user space.

Signed-off-by: Like Xu <likexu@tencent.com>
---
 tools/perf/arch/x86/util/iostat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/arch/x86/util/iostat.c b/tools/perf/arch/x86/util/iostat.c
index eeafe97b8105..792cd75ade33 100644
--- a/tools/perf/arch/x86/util/iostat.c
+++ b/tools/perf/arch/x86/util/iostat.c
@@ -432,7 +432,7 @@ void iostat_print_metric(struct perf_stat_config *config, struct evsel *evsel,
 	u8 die = ((struct iio_root_port *)evsel->priv)->die;
 	struct perf_counts_values *count = perf_counts(evsel->counts, die, 0);
 
-	if (count->run && count->ena) {
+	if (count && count->run && count->ena) {
 		if (evsel->prev_raw_counts && !out->force_header) {
 			struct perf_counts_values *prev_count =
 				perf_counts(evsel->prev_raw_counts, die, 0);
-- 
2.32.0


  reply	other threads:[~2021-09-27  8:13 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-27  8:11 [PATCH 1/2] perf iostat: Use system-wide mode if the target cpu_list is unspecified Like Xu
2021-09-27  8:11 ` Like Xu [this message]
2021-09-27 12:42   ` [PATCH 2/2] perf iostat: Fix Segmentation fault from NULL 'struct perf_counts_values *' Arnaldo Carvalho de Melo
2021-09-27 12:40 ` [PATCH 1/2] perf iostat: Use system-wide mode if the target cpu_list is unspecified Arnaldo Carvalho de Melo
2021-09-28  9:06 ` Alexander Antonov

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=20210927081115.39568-2-likexu@tencent.com \
    --to=like.xu.linux@gmail.com \
    --cc=acme@kernel.org \
    --cc=alexander.antonov@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=eranian@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@redhat.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).