Skip to content

Commit dfb6cd1

Browse files
committed
tracing: Fix very unlikely race of registering two stat tracers
Looking through old emails in my INBOX, I came across a patch from Luis Henriques that attempted to fix a race of two stat tracers registering the same stat trace (extremely unlikely, as this is done in the kernel, and probably doesn't even exist). The submitted patch wasn't quite right as it needed to deal with clean up a bit better (if two stat tracers were the same, it would have the same files). But to make the code cleaner, all we needed to do is to keep the all_stat_sessions_mutex held for most of the registering function. Link: http://lkml.kernel.org/r/1410299375-20068-1-git-send-email-luis.henriques@canonical.com Fixes: 002bb86 ("tracing/ftrace: separate events tracing and stats tracing engine") Reported-by: Luis Henriques <luis.henriques@canonical.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 659ded3 commit dfb6cd1

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

‎kernel/trace/trace_stat.c‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ static int init_stat_file(struct stat_session *session)
304304
int register_stat_tracer(struct tracer_stat *trace)
305305
{
306306
struct stat_session *session, *node;
307-
int ret;
307+
int ret = -EINVAL;
308308

309309
if (!trace)
310310
return -EINVAL;
@@ -315,17 +315,15 @@ int register_stat_tracer(struct tracer_stat *trace)
315315
/* Already registered? */
316316
mutex_lock(&all_stat_sessions_mutex);
317317
list_for_each_entry(node, &all_stat_sessions, session_list) {
318-
if (node->ts == trace) {
319-
mutex_unlock(&all_stat_sessions_mutex);
320-
return -EINVAL;
321-
}
318+
if (node->ts == trace)
319+
goto out;
322320
}
323-
mutex_unlock(&all_stat_sessions_mutex);
324321

322+
ret = -ENOMEM;
325323
/* Init the session */
326324
session = kzalloc(sizeof(*session), GFP_KERNEL);
327325
if (!session)
328-
return -ENOMEM;
326+
goto out;
329327

330328
session->ts = trace;
331329
INIT_LIST_HEAD(&session->session_list);
@@ -334,15 +332,16 @@ int register_stat_tracer(struct tracer_stat *trace)
334332
ret = init_stat_file(session);
335333
if (ret) {
336334
destroy_session(session);
337-
return ret;
335+
goto out;
338336
}
339337

338+
ret = 0;
340339
/* Register */
341-
mutex_lock(&all_stat_sessions_mutex);
342340
list_add_tail(&session->session_list, &all_stat_sessions);
341+
out:
343342
mutex_unlock(&all_stat_sessions_mutex);
344343

345-
return 0;
344+
return ret;
346345
}
347346

348347
void unregister_stat_tracer(struct tracer_stat *trace)

0 commit comments

Comments
 (0)