Skip to content

Commit 8f454c9

Browse files
captain5050acmel
authored andcommitted
perf thread: Ensure comm_lock held for comm_list
Add thread safety annotations for comm_list and add locking for two instances where the list is accessed without the lock held (in contradiction to ____thread__set_comm()). Signed-off-by: Ian Rogers <[email protected]> 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: 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]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 6fe0644 commit 8f454c9

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

tools/perf/util/comm.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static struct comm_strs {
2424
static void comm_strs__remove_if_last(struct comm_str *cs);
2525

2626
static void comm_strs__init(void)
27+
NO_THREAD_SAFETY_ANALYSIS /* Inherently single threaded due to pthread_once. */
2728
{
2829
init_rwsem(&_comm_strs.lock);
2930
_comm_strs.capacity = 16;
@@ -119,6 +120,7 @@ static void comm_strs__remove_if_last(struct comm_str *cs)
119120
}
120121

121122
static struct comm_str *__comm_strs__find(struct comm_strs *comm_strs, const char *str)
123+
SHARED_LOCKS_REQUIRED(comm_strs->lock)
122124
{
123125
struct comm_str **result;
124126

tools/perf/util/thread.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ int thread__init_maps(struct thread *thread, struct machine *machine)
4141
}
4242

4343
struct thread *thread__new(pid_t pid, pid_t tid)
44+
NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single threaded. */
4445
{
4546
RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
4647
struct thread *thread;
@@ -202,22 +203,29 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
202203

203204
struct comm *thread__comm(struct thread *thread)
204205
{
205-
if (list_empty(thread__comm_list(thread)))
206-
return NULL;
206+
struct comm *res = NULL;
207207

208-
return list_first_entry(thread__comm_list(thread), struct comm, list);
208+
down_read(thread__comm_lock(thread));
209+
if (!list_empty(thread__comm_list(thread)))
210+
res = list_first_entry(thread__comm_list(thread), struct comm, list);
211+
up_read(thread__comm_lock(thread));
212+
return res;
209213
}
210214

211215
struct comm *thread__exec_comm(struct thread *thread)
212216
{
213217
struct comm *comm, *last = NULL, *second_last = NULL;
214218

219+
down_read(thread__comm_lock(thread));
215220
list_for_each_entry(comm, thread__comm_list(thread), list) {
216-
if (comm->exec)
221+
if (comm->exec) {
222+
up_read(thread__comm_lock(thread));
217223
return comm;
224+
}
218225
second_last = last;
219226
last = comm;
220227
}
228+
up_read(thread__comm_lock(thread));
221229

222230
/*
223231
* 'last' with no start time might be the parent's comm of a synthesized
@@ -233,6 +241,7 @@ struct comm *thread__exec_comm(struct thread *thread)
233241

234242
static int ____thread__set_comm(struct thread *thread, const char *str,
235243
u64 timestamp, bool exec)
244+
EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
236245
{
237246
struct comm *new, *curr = thread__comm(thread);
238247

tools/perf/util/thread.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,15 @@ static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread
236236
return &RC_CHK_ACCESS(thread)->namespaces_lock;
237237
}
238238

239-
static inline struct list_head *thread__comm_list(struct thread *thread)
239+
static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
240240
{
241-
return &RC_CHK_ACCESS(thread)->comm_list;
241+
return &RC_CHK_ACCESS(thread)->comm_lock;
242242
}
243243

244-
static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
244+
static inline struct list_head *thread__comm_list(struct thread *thread)
245+
SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
245246
{
246-
return &RC_CHK_ACCESS(thread)->comm_lock;
247+
return &RC_CHK_ACCESS(thread)->comm_list;
247248
}
248249

249250
static inline u64 thread__db_id(const struct thread *thread)

0 commit comments

Comments
 (0)