Skip to content

Commit 3a53acf

Browse files
KAGA-KOKOrostedt
authored andcommitted
tracing: Fix lock inversion in trace_event_enable_tgid_record()
Task T2 Task T3 trace_options_core_write() subsystem_open() mutex_lock(trace_types_lock) mutex_lock(event_mutex) set_tracer_flag() trace_event_enable_tgid_record() mutex_lock(trace_types_lock) mutex_lock(event_mutex) This gives a circular dependency deadlock between trace_types_lock and event_mutex. To fix this invert the usage of trace_types_lock and event_mutex in trace_options_core_write(). This keeps the sequence of lock usage consistent. Link: http://lkml.kernel.org/r/0101016eef175e38-8ca71caf-a4eb-480d-a1e6-6f0bbc015495-000000@us-west-2.amazonses.com Cc: [email protected] Fixes: d914ba3 ("tracing: Add support for recording tgid of tasks") Signed-off-by: Prateek Sood <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 106f41f commit 3a53acf

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

kernel/trace/trace.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4685,6 +4685,10 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
46854685

46864686
int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
46874687
{
4688+
if ((mask == TRACE_ITER_RECORD_TGID) ||
4689+
(mask == TRACE_ITER_RECORD_CMD))
4690+
lockdep_assert_held(&event_mutex);
4691+
46884692
/* do nothing if flag is already set */
46894693
if (!!(tr->trace_flags & mask) == !!enabled)
46904694
return 0;
@@ -4752,6 +4756,7 @@ static int trace_set_options(struct trace_array *tr, char *option)
47524756

47534757
cmp += len;
47544758

4759+
mutex_lock(&event_mutex);
47554760
mutex_lock(&trace_types_lock);
47564761

47574762
ret = match_string(trace_options, -1, cmp);
@@ -4762,6 +4767,7 @@ static int trace_set_options(struct trace_array *tr, char *option)
47624767
ret = set_tracer_flag(tr, 1 << ret, !neg);
47634768

47644769
mutex_unlock(&trace_types_lock);
4770+
mutex_unlock(&event_mutex);
47654771

47664772
/*
47674773
* If the first trailing whitespace is replaced with '\0' by strstrip,
@@ -8076,9 +8082,11 @@ trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
80768082
if (val != 0 && val != 1)
80778083
return -EINVAL;
80788084

8085+
mutex_lock(&event_mutex);
80798086
mutex_lock(&trace_types_lock);
80808087
ret = set_tracer_flag(tr, 1 << index, val);
80818088
mutex_unlock(&trace_types_lock);
8089+
mutex_unlock(&event_mutex);
80828090

80838091
if (ret < 0)
80848092
return ret;

kernel/trace/trace_events.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ void trace_event_enable_cmd_record(bool enable)
320320
struct trace_event_file *file;
321321
struct trace_array *tr;
322322

323-
mutex_lock(&event_mutex);
323+
lockdep_assert_held(&event_mutex);
324+
324325
do_for_each_event_file(tr, file) {
325326

326327
if (!(file->flags & EVENT_FILE_FL_ENABLED))
@@ -334,15 +335,15 @@ void trace_event_enable_cmd_record(bool enable)
334335
clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
335336
}
336337
} while_for_each_event_file();
337-
mutex_unlock(&event_mutex);
338338
}
339339

340340
void trace_event_enable_tgid_record(bool enable)
341341
{
342342
struct trace_event_file *file;
343343
struct trace_array *tr;
344344

345-
mutex_lock(&event_mutex);
345+
lockdep_assert_held(&event_mutex);
346+
346347
do_for_each_event_file(tr, file) {
347348
if (!(file->flags & EVENT_FILE_FL_ENABLED))
348349
continue;
@@ -356,7 +357,6 @@ void trace_event_enable_tgid_record(bool enable)
356357
&file->flags);
357358
}
358359
} while_for_each_event_file();
359-
mutex_unlock(&event_mutex);
360360
}
361361

362362
static int __ftrace_event_enable_disable(struct trace_event_file *file,

0 commit comments

Comments
 (0)