Skip to content

Commit fc800a1

Browse files
mhiramatrostedt
authored andcommitted
tracing: Lock event_mutex before synth_event_mutex
synthetic event is using synth_event_mutex for protecting synth_event_list, and event_trigger_write() path acquires locks as below order. event_trigger_write(event_mutex) ->trigger_process_regex(trigger_cmd_mutex) ->event_hist_trigger_func(synth_event_mutex) On the other hand, synthetic event creation and deletion paths call trace_add_event_call() and trace_remove_event_call() which acquires event_mutex. In that case, if we keep the synth_event_mutex locked while registering/unregistering synthetic events, its dependency will be inversed. To avoid this issue, current synthetic event is using a 2 phase process to create/delete events. For example, it searches existing events under synth_event_mutex to check for event-name conflicts, and unlocks synth_event_mutex, then registers a new event under event_mutex locked. Finally, it locks synth_event_mutex and tries to add the new event to the list. But it can introduce complexity and a chance for name conflicts. To solve this simpler, this introduces trace_add_event_call_nolock() and trace_remove_event_call_nolock() which don't acquire event_mutex inside. synthetic event can lock event_mutex before synth_event_mutex to solve the lock dependency issue simpler. Link: http://lkml.kernel.org/r/154140844377.17322.13781091165954002713.stgit@devbox Reviewed-by: Tom Zanussi <tom.zanussi@linux.intel.com> Tested-by: Tom Zanussi <tom.zanussi@linux.intel.com> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 547cd9e commit fc800a1

File tree

3 files changed

+40
-20
lines changed

3 files changed

+40
-20
lines changed

include/linux/trace_events.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ extern int trace_event_raw_init(struct trace_event_call *call);
529529
extern int trace_define_field(struct trace_event_call *call, const char *type,
530530
const char *name, int offset, int size,
531531
int is_signed, int filter_type);
532+
extern int trace_add_event_call_nolock(struct trace_event_call *call);
533+
extern int trace_remove_event_call_nolock(struct trace_event_call *call);
532534
extern int trace_add_event_call(struct trace_event_call *call);
533535
extern int trace_remove_event_call(struct trace_event_call *call);
534536
extern int trace_event_get_offsets(struct trace_event_call *call);

kernel/trace/trace_events.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,18 +2305,28 @@ __trace_early_add_new_event(struct trace_event_call *call,
23052305
struct ftrace_module_file_ops;
23062306
static void __add_event_to_tracers(struct trace_event_call *call);
23072307

2308-
/* Add an additional event_call dynamically */
2309-
int trace_add_event_call(struct trace_event_call *call)
2308+
int trace_add_event_call_nolock(struct trace_event_call *call)
23102309
{
23112310
int ret;
2312-
mutex_lock(&event_mutex);
2311+
lockdep_assert_held(&event_mutex);
2312+
23132313
mutex_lock(&trace_types_lock);
23142314

23152315
ret = __register_event(call, NULL);
23162316
if (ret >= 0)
23172317
__add_event_to_tracers(call);
23182318

23192319
mutex_unlock(&trace_types_lock);
2320+
return ret;
2321+
}
2322+
2323+
/* Add an additional event_call dynamically */
2324+
int trace_add_event_call(struct trace_event_call *call)
2325+
{
2326+
int ret;
2327+
2328+
mutex_lock(&event_mutex);
2329+
ret = trace_add_event_call_nolock(call);
23202330
mutex_unlock(&event_mutex);
23212331
return ret;
23222332
}
@@ -2366,17 +2376,29 @@ static int probe_remove_event_call(struct trace_event_call *call)
23662376
return 0;
23672377
}
23682378

2369-
/* Remove an event_call */
2370-
int trace_remove_event_call(struct trace_event_call *call)
2379+
/* no event_mutex version */
2380+
int trace_remove_event_call_nolock(struct trace_event_call *call)
23712381
{
23722382
int ret;
23732383

2374-
mutex_lock(&event_mutex);
2384+
lockdep_assert_held(&event_mutex);
2385+
23752386
mutex_lock(&trace_types_lock);
23762387
down_write(&trace_event_sem);
23772388
ret = probe_remove_event_call(call);
23782389
up_write(&trace_event_sem);
23792390
mutex_unlock(&trace_types_lock);
2391+
2392+
return ret;
2393+
}
2394+
2395+
/* Remove an event_call */
2396+
int trace_remove_event_call(struct trace_event_call *call)
2397+
{
2398+
int ret;
2399+
2400+
mutex_lock(&event_mutex);
2401+
ret = trace_remove_event_call_nolock(call);
23802402
mutex_unlock(&event_mutex);
23812403

23822404
return ret;

kernel/trace/trace_events_hist.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ static int register_synth_event(struct synth_event *event)
912912
call->data = event;
913913
call->tp = event->tp;
914914

915-
ret = trace_add_event_call(call);
915+
ret = trace_add_event_call_nolock(call);
916916
if (ret) {
917917
pr_warn("Failed to register synthetic event: %s\n",
918918
trace_event_name(call));
@@ -936,7 +936,7 @@ static int unregister_synth_event(struct synth_event *event)
936936
struct trace_event_call *call = &event->call;
937937
int ret;
938938

939-
ret = trace_remove_event_call(call);
939+
ret = trace_remove_event_call_nolock(call);
940940

941941
return ret;
942942
}
@@ -1013,12 +1013,10 @@ static void add_or_delete_synth_event(struct synth_event *event, int delete)
10131013
if (delete)
10141014
free_synth_event(event);
10151015
else {
1016-
mutex_lock(&synth_event_mutex);
10171016
if (!find_synth_event(event->name))
10181017
list_add(&event->list, &synth_event_list);
10191018
else
10201019
free_synth_event(event);
1021-
mutex_unlock(&synth_event_mutex);
10221020
}
10231021
}
10241022

@@ -1030,6 +1028,7 @@ static int create_synth_event(int argc, char **argv)
10301028
int i, consumed = 0, n_fields = 0, ret = 0;
10311029
char *name;
10321030

1031+
mutex_lock(&event_mutex);
10331032
mutex_lock(&synth_event_mutex);
10341033

10351034
/*
@@ -1102,8 +1101,6 @@ static int create_synth_event(int argc, char **argv)
11021101
goto err;
11031102
}
11041103
out:
1105-
mutex_unlock(&synth_event_mutex);
1106-
11071104
if (event) {
11081105
if (delete_event) {
11091106
ret = unregister_synth_event(event);
@@ -1113,10 +1110,13 @@ static int create_synth_event(int argc, char **argv)
11131110
add_or_delete_synth_event(event, ret);
11141111
}
11151112
}
1113+
mutex_unlock(&synth_event_mutex);
1114+
mutex_unlock(&event_mutex);
11161115

11171116
return ret;
11181117
err:
11191118
mutex_unlock(&synth_event_mutex);
1119+
mutex_unlock(&event_mutex);
11201120

11211121
for (i = 0; i < n_fields; i++)
11221122
free_synth_field(fields[i]);
@@ -1127,12 +1127,10 @@ static int create_synth_event(int argc, char **argv)
11271127

11281128
static int release_all_synth_events(void)
11291129
{
1130-
struct list_head release_events;
11311130
struct synth_event *event, *e;
11321131
int ret = 0;
11331132

1134-
INIT_LIST_HEAD(&release_events);
1135-
1133+
mutex_lock(&event_mutex);
11361134
mutex_lock(&synth_event_mutex);
11371135

11381136
list_for_each_entry(event, &synth_event_list, list) {
@@ -1142,16 +1140,14 @@ static int release_all_synth_events(void)
11421140
}
11431141
}
11441142

1145-
list_splice_init(&event->list, &release_events);
1146-
1147-
mutex_unlock(&synth_event_mutex);
1148-
1149-
list_for_each_entry_safe(event, e, &release_events, list) {
1143+
list_for_each_entry_safe(event, e, &synth_event_list, list) {
11501144
list_del(&event->list);
11511145

11521146
ret = unregister_synth_event(event);
11531147
add_or_delete_synth_event(event, !ret);
11541148
}
1149+
mutex_unlock(&synth_event_mutex);
1150+
mutex_unlock(&event_mutex);
11551151

11561152
return ret;
11571153
}

0 commit comments

Comments
 (0)