Skip to contentSkip to conversation

Revert "perf thread: Ensure comm_lock held for comm_list"

PATCH In review Arnaldo Carvalho de Melo 2 participants 3 messages May 28, 2025 perf events

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

PATCH 1/1 Revert "perf thread: Ensure comm_lock held for comm_list"

This reverts commit 8f454c95817d15ee529d58389612ea4b34f5ffb3.

'perf top' is freezing on exit sometimes, bisected to this one, revert.

Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Athira Rajeev <[email protected]>
Cc Bill Wendling <[email protected]>
Cc Chaitanya S Prakash <[email protected]>
Cc Fei Lang <[email protected]>
Cc Howard Chu <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc James Clark <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Kan Liang <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephen Brennan <[email protected]>
---
 tools/perf/util/comm.c   |  2 --
 tools/perf/util/thread.c | 17 ++++-------------
 tools/perf/util/thread.h |  9 ++++-----
 3 files changed, 8 insertions(+), 20 deletions(-)
tools/perf/util/comm.c +0 -2
index 9880247a2c3364cb..8aa456d7c2cd2d74 100644
@@ -24,7 +24,6 @@ static struct comm_strs {
 static void comm_strs__remove_if_last(struct comm_str *cs);
 
 static void comm_strs__init(void)
-	NO_THREAD_SAFETY_ANALYSIS /* Inherently single threaded due to pthread_once. */
 {
 	init_rwsem(&_comm_strs.lock);
 	_comm_strs.capacity = 16;
@@ -120,7 +119,6 @@ static void comm_strs__remove_if_last(struct comm_str *cs)
 }
 
 static struct comm_str *__comm_strs__find(struct comm_strs *comm_strs, const char *str)
-	SHARED_LOCKS_REQUIRED(comm_strs->lock)
 {
 	struct comm_str **result;
 
tools/perf/util/thread.c +4 -13
index c202b98b36c29215..415c0e5d1e751a47 100644
@@ -41,7 +41,6 @@ int thread__init_maps(struct thread *thread, struct machine *machine)
 }
 
 struct thread *thread__new(pid_t pid, pid_t tid)
-	NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single threaded. */
 {
 	RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
 	struct thread *thread;
@@ -203,29 +202,22 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
 
 struct comm *thread__comm(struct thread *thread)
 {
-	struct comm *res = NULL;
+	if (list_empty(thread__comm_list(thread)))
+		return NULL;
 
-	down_read(thread__comm_lock(thread));
-	if (!list_empty(thread__comm_list(thread)))
-		res = list_first_entry(thread__comm_list(thread), struct comm, list);
-	up_read(thread__comm_lock(thread));
-	return res;
+	return list_first_entry(thread__comm_list(thread), struct comm, list);
 }
 
 struct comm *thread__exec_comm(struct thread *thread)
 {
 	struct comm *comm, *last = NULL, *second_last = NULL;
 
-	down_read(thread__comm_lock(thread));
 	list_for_each_entry(comm, thread__comm_list(thread), list) {
-		if (comm->exec) {
-			up_read(thread__comm_lock(thread));
+		if (comm->exec)
 			return comm;
-		}
 		second_last = last;
 		last = comm;
 	}
-	up_read(thread__comm_lock(thread));
 
 	/*
 	 * 'last' with no start time might be the parent's comm of a synthesized
@@ -241,7 +233,6 @@ struct comm *thread__exec_comm(struct thread *thread)
 
 static int ____thread__set_comm(struct thread *thread, const char *str,
 				u64 timestamp, bool exec)
-	EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
 {
 	struct comm *new, *curr = thread__comm(thread);
 
tools/perf/util/thread.h +4 -5
index 56e08c8ae005e82b..cd574a896418ac94 100644
@@ -236,15 +236,14 @@ static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread
 	return &RC_CHK_ACCESS(thread)->namespaces_lock;
 }
 
-static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
+static inline struct list_head *thread__comm_list(struct thread *thread)
 {
-	return &RC_CHK_ACCESS(thread)->comm_lock;
+	return &RC_CHK_ACCESS(thread)->comm_list;
 }
 
-static inline struct list_head *thread__comm_list(struct thread *thread)
-	SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
+static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
 {
-	return &RC_CHK_ACCESS(thread)->comm_list;
+	return &RC_CHK_ACCESS(thread)->comm_lock;
 }
 
 static inline u64 thread__db_id(const struct thread *thread)
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

Hi Ian,

This one had hit perf-tools-next, so I'm reverting it till we
figure out, since I'm trying to finish processing patches real soon now for this window, to give it some time to soak in linux-next.

I noticed that sometimes when trying to exit 'perf top' it just
sat there, so doing a bisect I ended up on this one, had no time to properly investigate it.

- Arnaldo

Quoted 131 lines · Arnaldo Carvalho de Melo

This reverts commit 8f454c95817d15ee529d58389612ea4b34f5ffb3.

'perf top' is freezing on exit sometimes, bisected to this one, revert.

Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Athira Rajeev <[email protected]>
Cc Bill Wendling <[email protected]>
Cc Chaitanya S Prakash <[email protected]>
Cc Fei Lang <[email protected]>
Cc Howard Chu <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc James Clark <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Kan Liang <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephen Brennan <[email protected]>
---
 tools/perf/util/comm.c   |  2 --
 tools/perf/util/thread.c | 17 ++++-------------
 tools/perf/util/thread.h |  9 ++++-----
 3 files changed, 8 insertions(+), 20 deletions(-)
tools/perf/util/comm.c +0 -2
index 9880247a2c3364cb..8aa456d7c2cd2d74 100644
@@ -24,7 +24,6 @@ static struct comm_strs {
 static void comm_strs__remove_if_last(struct comm_str *cs);
 
 static void comm_strs__init(void)
-	NO_THREAD_SAFETY_ANALYSIS /* Inherently single threaded due to pthread_once. */
 {
 	init_rwsem(&_comm_strs.lock);
 	_comm_strs.capacity = 16;
@@ -120,7 +119,6 @@ static void comm_strs__remove_if_last(struct comm_str *cs)
 }
 
 static struct comm_str *__comm_strs__find(struct comm_strs *comm_strs, const char *str)
-	SHARED_LOCKS_REQUIRED(comm_strs->lock)
 {
 	struct comm_str **result;
 
tools/perf/util/thread.c +4 -13
index c202b98b36c29215..415c0e5d1e751a47 100644
@@ -41,7 +41,6 @@ int thread__init_maps(struct thread *thread, struct machine *machine)
 }
 
 struct thread *thread__new(pid_t pid, pid_t tid)
-	NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single threaded. */
 {
 	RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
 	struct thread *thread;
@@ -203,29 +202,22 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
 
 struct comm *thread__comm(struct thread *thread)
 {
-	struct comm *res = NULL;
+	if (list_empty(thread__comm_list(thread)))
+		return NULL;
 
-	down_read(thread__comm_lock(thread));
-	if (!list_empty(thread__comm_list(thread)))
-		res = list_first_entry(thread__comm_list(thread), struct comm, list);
-	up_read(thread__comm_lock(thread));
-	return res;
+	return list_first_entry(thread__comm_list(thread), struct comm, list);
 }
 
 struct comm *thread__exec_comm(struct thread *thread)
 {
 	struct comm *comm, *last = NULL, *second_last = NULL;
 
-	down_read(thread__comm_lock(thread));
 	list_for_each_entry(comm, thread__comm_list(thread), list) {
-		if (comm->exec) {
-			up_read(thread__comm_lock(thread));
+		if (comm->exec)
 			return comm;
-		}
 		second_last = last;
 		last = comm;
 	}
-	up_read(thread__comm_lock(thread));
 
 	/*
 	 * 'last' with no start time might be the parent's comm of a synthesized
@@ -241,7 +233,6 @@ struct comm *thread__exec_comm(struct thread *thread)
 
 static int ____thread__set_comm(struct thread *thread, const char *str,
 				u64 timestamp, bool exec)
-	EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
 {
 	struct comm *new, *curr = thread__comm(thread);
 
tools/perf/util/thread.h +4 -5
index 56e08c8ae005e82b..cd574a896418ac94 100644
@@ -236,15 +236,14 @@ static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread
 	return &RC_CHK_ACCESS(thread)->namespaces_lock;
 }
 
-static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
+static inline struct list_head *thread__comm_list(struct thread *thread)
 {
-	return &RC_CHK_ACCESS(thread)->comm_lock;
+	return &RC_CHK_ACCESS(thread)->comm_list;
 }
 
-static inline struct list_head *thread__comm_list(struct thread *thread)
-	SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
+static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
 {
-	return &RC_CHK_ACCESS(thread)->comm_list;
+	return &RC_CHK_ACCESS(thread)->comm_lock;
 }
 
 static inline u64 thread__db_id(const struct thread *thread)
Signature · 1 line
2.49.0
reviewer
#2 lore raw
Quoted 10 lines · Arnaldo Carvalho de Melo

Hi Ian,

This one had hit perf-tools-next, so I'm reverting it till we
figure out, since I'm trying to finish processing patches real soon now for this window, to give it some time to soak in linux-next.

I noticed that sometimes when trying to exit 'perf top' it just
sat there, so doing a bisect I ended up on this one, had no time to properly investigate it.

Hi Arnaldo,

I believe it is fixed by:
https://lore.kernel.org/lkml/[email protected]/
I was actually trying to repro a DEBUG=1 issue I'd seen in perf top with this assert failing:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/util/maps.c?h=perf-tools-next#n80

```
assert(RC_CHK_EQUAL(map__kmap(map)->kmaps, maps));
```

but no dice on that - I suspect it has something to do with modules or BPF or something.
Anyway, with the patch above perf top for me I wasn't able to make it crash even when being aggressive and running with both address and memory sanitizer.

Thanks,
Ian

Quoted 134 lines

- Arnaldo

Quoted 131 lines · Arnaldo Carvalho de Melo

This reverts commit 8f454c95817d15ee529d58389612ea4b34f5ffb3.

'perf top' is freezing on exit sometimes, bisected to this one, revert.

Cc Adrian Hunter <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Athira Rajeev <[email protected]>
Cc Bill Wendling <[email protected]>
Cc Chaitanya S Prakash <[email protected]>
Cc Fei Lang <[email protected]>
Cc Howard Chu <[email protected]>
Cc Ian Rogers <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc James Clark <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Justin Stitt <[email protected]>
Cc Kan Liang <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephen Brennan <[email protected]>
---
 tools/perf/util/comm.c   |  2 --
 tools/perf/util/thread.c | 17 ++++-------------
 tools/perf/util/thread.h |  9 ++++-----
 3 files changed, 8 insertions(+), 20 deletions(-)
tools/perf/util/comm.c +0 -2
index 9880247a2c3364cb..8aa456d7c2cd2d74 100644
@@ -24,7 +24,6 @@ static struct comm_strs {
 static void comm_strs__remove_if_last(struct comm_str *cs);
 
 static void comm_strs__init(void)
-     NO_THREAD_SAFETY_ANALYSIS /* Inherently single threaded due to pthread_once. */
 {
      init_rwsem(&_comm_strs.lock);
      _comm_strs.capacity = 16;
@@ -120,7 +119,6 @@ static void comm_strs__remove_if_last(struct comm_str *cs)
 }
 
 static struct comm_str *__comm_strs__find(struct comm_strs *comm_strs, const char *str)
-     SHARED_LOCKS_REQUIRED(comm_strs->lock)
 {
      struct comm_str **result;
 
tools/perf/util/thread.c +4 -13
index c202b98b36c29215..415c0e5d1e751a47 100644
@@ -41,7 +41,6 @@ int thread__init_maps(struct thread *thread, struct machine *machine)
 }
 
 struct thread *thread__new(pid_t pid, pid_t tid)
-     NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single threaded. */
 {
      RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
      struct thread *thread;
@@ -203,29 +202,22 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
 
 struct comm *thread__comm(struct thread *thread)
 {
-     struct comm *res = NULL;
+     if (list_empty(thread__comm_list(thread)))
+             return NULL;
 
-     down_read(thread__comm_lock(thread));
-     if (!list_empty(thread__comm_list(thread)))
-             res = list_first_entry(thread__comm_list(thread), struct comm, list);
-     up_read(thread__comm_lock(thread));
-     return res;
+     return list_first_entry(thread__comm_list(thread), struct comm, list);
 }
 
 struct comm *thread__exec_comm(struct thread *thread)
 {
      struct comm *comm, *last = NULL, *second_last = NULL;
 
-     down_read(thread__comm_lock(thread));
      list_for_each_entry(comm, thread__comm_list(thread), list) {
-             if (comm->exec) {
-                     up_read(thread__comm_lock(thread));
+             if (comm->exec)
                      return comm;
-             }
              second_last = last;
              last = comm;
      }
-     up_read(thread__comm_lock(thread));
 
      /*
       * 'last' with no start time might be the parent's comm of a synthesized
@@ -241,7 +233,6 @@ struct comm *thread__exec_comm(struct thread *thread)
 
 static int ____thread__set_comm(struct thread *thread, const char *str,
                              u64 timestamp, bool exec)
-     EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
 {
      struct comm *new, *curr = thread__comm(thread);
 
tools/perf/util/thread.h +4 -6
index 56e08c8ae005e82b..cd574a896418ac94 100644
@@ -236,15 +236,14 @@ static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread
      return &RC_CHK_ACCESS(thread)->namespaces_lock;
 }
 
-static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
+static inline struct list_head *thread__comm_list(struct thread *thread)
 {
-     return &RC_CHK_ACCESS(thread)->comm_lock;
+     return &RC_CHK_ACCESS(thread)->comm_list;
 }
 
-static inline struct list_head *thread__comm_list(struct thread *thread)
-     SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
+static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
 {
-     return &RC_CHK_ACCESS(thread)->comm_list;
+     return &RC_CHK_ACCESS(thread)->comm_lock;
 }
 
 static inline u64 thread__db_id(const struct thread *thread)
--

2.49.0