mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yang Jihong <yangjihong1@huawei.com>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
	<namhyung@kernel.org>, <mark.rutland@arm.com>,
	<alexander.shishkin@linux.intel.com>, <jolsa@kernel.org>,
	<irogers@google.com>, <adrian.hunter@intel.com>,
	<linux-perf-users@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Cc: <yangjihong1@huawei.com>
Subject: [PATCH 0/3] perf record: Fix segfault with '--timestamp-filename'  option and pipe mode
Date: Fri, 19 Jan 2024 04:03:01 +0000	[thread overview]
Message-ID: <20240119040304.3708522-1-yangjihong1@huawei.com> (raw)

When perf record uses '--timestamp-filename' option and pipe mode,
will occur segfault:

  # perf record --timestamp-filename -o- true 1>/dev/null
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Dump -.2024011813361681 ]
  perf: Segmentation fault
  Segmentation fault (core dumped)

Debug the core file by using the gdb:

  # gdb perf core.3706841
  <SNIP>
  [New LWP 3706841]
  [Thread debugging using libthread_db enabled]
  Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
  Core was generated by `perf record --timestamp-filename -o- true'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145
  145             struct perf_stat_evsel *ps = evsel->stats;
  (gdb) bt
  #0  evsel__free_stat_priv (evsel=0x555ea56197af) at util/stat.c:145
  #1  evlist__free_stats (evlist=evlist@entry=0x555bf0dad1b0) at util/stat.c:215
  #2  0x0000555befd425e8 in evlist__delete (evlist=0x555bf0dad1b0) at util/evlist.c:175
  #3  0x0000555befd42785 in evlist__delete (evlist=<optimized out>) at util/evlist.c:181
  #4  0x0000555befc7f547 in cmd_record (argc=<optimized out>, argv=<optimized out>) at builtin-record.c:4252
  #5  0x0000555befd27700 in run_builtin (p=p@entry=0x555bf05acf88 <commands+264>, argc=argc@entry=4, argv=argv@entry=0x7ffc2696a920) at perf.c:349
  #6  0x0000555befc68751 in handle_internal_command (argv=0x7ffc2696a920, argc=4) at perf.c:402
  #7  run_argv (argv=<synthetic pointer>, argcp=<synthetic pointer>) at perf.c:446
  #8  main (argc=4, argv=0x7ffc2696a920) at perf.c:562

It is found that the memory of the evsel is modified.
The reason is that perf_data__switch() not initialized 'new_filename',
as a result, it uses the on-stack variable. It happens that the evsel
address is stored here. 'new_filename' is freed later when uses
'--timestamp-filename' option. Therefore, segfault occurs in the evsel_delete().


1. patch1 fixes this problem.
2. patch2 to optimize the process, check conflict between
   '--timestamp-filename' option and the pipe mode before recording to
   avoid switch perf data.
3. patch3 fixes the code style problem by the way.


Yang Jihong (3):
  perf record: Fix possible incorrect free in record__switch_output()
  perf record: Check conflict between '--timestamp-filename' option and
    pipe mode before recording
  perf data: Minor code style alignment cleanup

 tools/perf/builtin-record.c | 14 ++++++++++----
 tools/perf/util/data.c      | 10 ++++------
 tools/perf/util/data.h      |  6 +++---
 3 files changed, 17 insertions(+), 13 deletions(-)

-- 
2.34.1


             reply	other threads:[~2024-01-19  4:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-19  4:03 Yang Jihong [this message]
2024-01-19  4:03 ` [PATCH 1/3] perf record: Fix possible incorrect free in record__switch_output() Yang Jihong
2024-01-19  4:03 ` [PATCH 2/3] perf record: Check conflict between '--timestamp-filename' option and pipe mode before recording Yang Jihong
2024-01-19  4:03 ` [PATCH 3/3] perf data: Minor code style alignment cleanup Yang Jihong
2024-01-19 22:06 ` [PATCH 0/3] perf record: Fix segfault with '--timestamp-filename' option and pipe mode Namhyung Kim
2024-01-22 20:38 ` Namhyung Kim

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=20240119040304.3708522-1-yangjihong1@huawei.com \
    --to=yangjihong1@huawei.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --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

all inboxes | Powered by JetHome®