Skip to content

Commit ae07569

Browse files
captain5050namhyung
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]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 1190410 commit ae07569

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-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: 22 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;
@@ -200,24 +201,39 @@ int thread__set_namespaces(struct thread *thread, u64 timestamp,
200201
return ret;
201202
}
202203

203-
struct comm *thread__comm(struct thread *thread)
204+
static struct comm *__thread__comm(struct thread *thread)
205+
SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
204206
{
205207
if (list_empty(thread__comm_list(thread)))
206208
return NULL;
207209

208210
return list_first_entry(thread__comm_list(thread), struct comm, list);
209211
}
210212

213+
struct comm *thread__comm(struct thread *thread)
214+
{
215+
struct comm *res = NULL;
216+
217+
down_read(thread__comm_lock(thread));
218+
res = __thread__comm(thread);
219+
up_read(thread__comm_lock(thread));
220+
return res;
221+
}
222+
211223
struct comm *thread__exec_comm(struct thread *thread)
212224
{
213225
struct comm *comm, *last = NULL, *second_last = NULL;
214226

227+
down_read(thread__comm_lock(thread));
215228
list_for_each_entry(comm, thread__comm_list(thread), list) {
216-
if (comm->exec)
229+
if (comm->exec) {
230+
up_read(thread__comm_lock(thread));
217231
return comm;
232+
}
218233
second_last = last;
219234
last = comm;
220235
}
236+
up_read(thread__comm_lock(thread));
221237

222238
/*
223239
* 'last' with no start time might be the parent's comm of a synthesized
@@ -233,8 +249,9 @@ struct comm *thread__exec_comm(struct thread *thread)
233249

234250
static int ____thread__set_comm(struct thread *thread, const char *str,
235251
u64 timestamp, bool exec)
252+
EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread))
236253
{
237-
struct comm *new, *curr = thread__comm(thread);
254+
struct comm *new, *curr = __thread__comm(thread);
238255

239256
/* Override the default :tid entry */
240257
if (!thread__comm_set(thread)) {
@@ -285,8 +302,9 @@ int thread__set_comm_from_proc(struct thread *thread)
285302
}
286303

287304
static const char *__thread__comm_str(struct thread *thread)
305+
SHARED_LOCKS_REQUIRED(thread__comm_lock(thread))
288306
{
289-
const struct comm *comm = thread__comm(thread);
307+
const struct comm *comm = __thread__comm(thread);
290308

291309
if (!comm)
292310
return NULL;

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)