The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Fei Lang <langfei@huawei.com>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
	<namhyung@kernel.org>
Cc: <mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
	<jolsa@kernel.org>, <irogers@google.com>,
	<adrian.hunter@intel.com>, <kan.liang@linux.intel.com>,
	<james.clark@linaro.org>, <linux-kernel@vger.kernel.org>,
	<hewenliang4@huawei.com>, <liuchao173@huawei.com>,
	<laihangliang1@huawei.com>
Subject: [PATCH] perf comm str: Fix perf top coredump due to concurrent read and write
Date: Mon, 19 May 2025 19:48:36 +0800	[thread overview]
Message-ID: <20250519114836.611110-1-langfei@huawei.com> (raw)

(gdb) bt
    __strcmp_evex () at ../sysdeps/x86_64/multiarch/strcmp-evex.S:314
    sort.comm_collapse () at util/sort.c:202
    hist_entry__collapse at util/hist.c:1312
    hists__collapse_insert_entry at util/hist.c:1620
    hists__collapse_resort at util/hist.c:1704
    perf_top__resort_hists at builtin-top.c:303
    perf_top__print_sym_table at builtin-top.c:350
    display_thread at builtin-top.c:700

Link:https://bugzilla.kernel.org/show_bug.cgi?id=220096

Fixes: <3178f58b9894> ("perf comm str: Avoid sort during insert")
Signed-off-by: Fei Lang <langfei@huawei.com>
---
 tools/perf/util/comm.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c
index 8aa456d7c2cd..0438870d31d2 100644
--- a/tools/perf/util/comm.c
+++ b/tools/perf/util/comm.c
@@ -209,13 +209,16 @@ struct comm *comm__new(const char *str, u64 timestamp, bool exec)
 int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
 {
 	struct comm_str *new, *old = comm->comm_str;
+	struct comm_strs *comm_strs = comm_strs__get();
 
 	new = comm_strs__findnew(str);
 	if (!new)
 		return -ENOMEM;
 
+	down_write(&comm_strs->lock);
 	comm_str__put(old);
 	comm->comm_str = new;
+	up_write(&comm_strs->lock);
 	comm->start = timestamp;
 	if (exec)
 		comm->exec = true;
@@ -225,11 +228,22 @@ int comm__override(struct comm *comm, const char *str, u64 timestamp, bool exec)
 
 void comm__free(struct comm *comm)
 {
+	struct comm_strs *comm_strs = comm_strs__get();
+
+	down_write(&comm_strs->lock);
 	comm_str__put(comm->comm_str);
 	free(comm);
+	up_write(&comm_strs->lock);
 }
 
 const char *comm__str(const struct comm *comm)
 {
-	return comm_str__str(comm->comm_str);
+	struct comm_strs *comm_strs = comm_strs__get();
+	char *p;
+
+	down_read(&comm_strs->lock);
+	p = comm_str__str(comm->comm_str);
+	up_read(&comm_strs->lock);
+
+	return p;
 }
-- 
2.33.0


             reply	other threads:[~2025-05-19 11:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-19 11:48 Fei Lang [this message]
2025-05-19 22:48 ` [PATCH] perf comm str: Fix perf top coredump due to concurrent read and write Ian Rogers
2025-05-21 18:05   ` Arnaldo Carvalho de Melo

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=20250519114836.611110-1-langfei@huawei.com \
    --to=langfei@huawei.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=hewenliang4@huawei.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=laihangliang1@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuchao173@huawei.com \
    --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