Skip to content

Commit aab58ab

Browse files
rostedtgregkh
authored andcommitted
tracing: Fix very unlikely race of registering two stat tracers
[ Upstream commit dfb6cd1 ] 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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 853aaba commit aab58ab

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
@@ -308,7 +308,7 @@ static int init_stat_file(struct stat_session *session)
308308
int register_stat_tracer(struct tracer_stat *trace)
309309
{
310310
struct stat_session *session, *node;
311-
int ret;
311+
int ret = -EINVAL;
312312

313313
if (!trace)
314314
return -EINVAL;
@@ -319,17 +319,15 @@ int register_stat_tracer(struct tracer_stat *trace)
319319
/* Already registered? */
320320
mutex_lock(&all_stat_sessions_mutex);
321321
list_for_each_entry(node, &all_stat_sessions, session_list) {
322-
if (node->ts == trace) {
323-
mutex_unlock(&all_stat_sessions_mutex);
324-
return -EINVAL;
325-
}
322+
if (node->ts == trace)
323+
goto out;
326324
}
327-
mutex_unlock(&all_stat_sessions_mutex);
328325

326+
ret = -ENOMEM;
329327
/* Init the session */
330328
session = kzalloc(sizeof(*session), GFP_KERNEL);
331329
if (!session)
332-
return -ENOMEM;
330+
goto out;
333331

334332
session->ts = trace;
335333
INIT_LIST_HEAD(&session->session_list);
@@ -338,15 +336,16 @@ int register_stat_tracer(struct tracer_stat *trace)
338336
ret = init_stat_file(session);
339337
if (ret) {
340338
destroy_session(session);
341-
return ret;
339+
goto out;
342340
}
343341

342+
ret = 0;
344343
/* Register */
345-
mutex_lock(&all_stat_sessions_mutex);
346344
list_add_tail(&session->session_list, &all_stat_sessions);
345+
out:
347346
mutex_unlock(&all_stat_sessions_mutex);
348347

349-
return 0;
348+
return ret;
350349
}
351350

352351
void unregister_stat_tracer(struct tracer_stat *trace)

0 commit comments

Comments
 (0)