Skip to contentSkip to conversation

perf comm str: Fix perf top coredump due to concurrent read and write

PATCH In review Fei Lang 3 participants 3 messages May 19, 2025 – May 21, 2025 perf events

lore
Share & tools
lore

Fei Lang

author
#0 lore raw

PATCH 1/1 perf comm str: Fix perf top coredump due to concurrent read and write

(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

Fixes <3178f58b9894> ("perf comm str: Avoid sort during insert")
---
 tools/perf/util/comm.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/perf/util/comm.c +15 -1
index 8aa456d7c2cd..0438870d31d2 100644
@@ -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;
 }
Signature · 1 line
2.33.0
reviewer
#1 lore raw
Quoted 24 lines · Fei Lang

(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

Fixes <3178f58b9894> ("perf comm str: Avoid sort during insert")
---
 tools/perf/util/comm.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/perf/util/comm.c +0 -0
index 8aa456d7c2cd..0438870d31d2 100644

tools/perf/util/comm.c struct comm *comm__new(const char *str, u64 timestamp, bool exec) · line 218

Quoted 10 lines
tools/perf/util/comm.c +2 -0
@@ -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_strs are a uniq-ified set of strs to avoid memory overhead from comm events. A comm_str is reference counted and immutable. Using the comm_str lock on the comm struct isn't something I agree with as we already have thread__comm_lock.

From the bug report $rdi is non-zero but comm_strs are immutable and reference counted, perhaps address sanitizer and reference count checking can point to the problem (add -fsanitize=address to your cflags). I put together some thread safety patches to see if the problem can be caught, but nothing that looks particularly likely:
https://lore.kernel.org/lkml/[email protected]/
I couldn't repro the problem locally.

Thanks,
Ian

Quoted 33 lines
tools/perf/util/comm.c +13 -2
        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

#2 lore raw
Quoted 41 lines · Ian Rogers
Quoted 24 lines · Fei Lang

(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

Fixes <3178f58b9894> ("perf comm str: Avoid sort during insert")
---
 tools/perf/util/comm.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tools/perf/util/comm.c +0 -0
index 8aa456d7c2cd..0438870d31d2 100644

tools/perf/util/comm.c struct comm *comm__new(const char *str, u64 timestamp, bool exec) · line 218

Quoted 10 lines
tools/perf/util/comm.c +2 -0
@@ -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_strs are a uniq-ified set of strs to avoid memory overhead from comm events. A comm_str is reference counted and immutable. Using the comm_str lock on the comm struct isn't something I agree with as we already have thread__comm_lock.

Quoted 7 lines
Quoted 1 line

From the bug report $rdi is non-zero but comm_strs are immutable and

reference counted, perhaps address sanitizer and reference count checking can point to the problem (add -fsanitize=address to your cflags). I put together some thread safety patches to see if the problem can be caught, but nothing that looks particularly likely:
https://lore.kernel.org/lkml/[email protected]/
I couldn't repro the problem locally.

I couldn't repro it here as well, and without your thread safety patches, that I have applied on my notebook and I'm merging with this workstation repo to push out.

- Arnaldo