Skip to content

Commit 08b7673

Browse files
committed
tracing: Switch trace_stat.c code over to use guard()
There are a couple functions in trace_stat.c that have "goto out" or equivalent on error in order to release locks that were taken. This can be error prone or just simply make the code more complex. Switch every location that ends with unlocking a mutex on error over to using the guard(mutex)() infrastructure to let the compiler worry about releasing locks. This makes the code easier to read and understand. Cc: Masami Hiramatsu <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 6c05353 commit 08b7673

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

kernel/trace/trace_stat.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,19 @@ static int stat_seq_init(struct stat_session *session)
128128
int ret = 0;
129129
int i;
130130

131-
mutex_lock(&session->stat_mutex);
131+
guard(mutex)(&session->stat_mutex);
132132
__reset_stat_session(session);
133133

134134
if (!ts->stat_cmp)
135135
ts->stat_cmp = dummy_cmp;
136136

137137
stat = ts->stat_start(ts);
138138
if (!stat)
139-
goto exit;
139+
return 0;
140140

141141
ret = insert_stat(root, stat, ts->stat_cmp);
142142
if (ret)
143-
goto exit;
143+
return ret;
144144

145145
/*
146146
* Iterate over the tracer stat entries and store them in an rbtree.
@@ -157,13 +157,10 @@ static int stat_seq_init(struct stat_session *session)
157157
goto exit_free_rbtree;
158158
}
159159

160-
exit:
161-
mutex_unlock(&session->stat_mutex);
162160
return ret;
163161

164162
exit_free_rbtree:
165163
__reset_stat_session(session);
166-
mutex_unlock(&session->stat_mutex);
167164
return ret;
168165
}
169166

@@ -308,26 +305,26 @@ static int init_stat_file(struct stat_session *session)
308305
int register_stat_tracer(struct tracer_stat *trace)
309306
{
310307
struct stat_session *session, *node;
311-
int ret = -EINVAL;
308+
int ret;
312309

313310
if (!trace)
314311
return -EINVAL;
315312

316313
if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
317314
return -EINVAL;
318315

316+
guard(mutex)(&all_stat_sessions_mutex);
317+
319318
/* Already registered? */
320-
mutex_lock(&all_stat_sessions_mutex);
321319
list_for_each_entry(node, &all_stat_sessions, session_list) {
322320
if (node->ts == trace)
323-
goto out;
321+
return -EINVAL;
324322
}
325323

326-
ret = -ENOMEM;
327324
/* Init the session */
328325
session = kzalloc(sizeof(*session), GFP_KERNEL);
329326
if (!session)
330-
goto out;
327+
return -ENOMEM;
331328

332329
session->ts = trace;
333330
INIT_LIST_HEAD(&session->session_list);
@@ -336,16 +333,13 @@ int register_stat_tracer(struct tracer_stat *trace)
336333
ret = init_stat_file(session);
337334
if (ret) {
338335
destroy_session(session);
339-
goto out;
336+
return ret;
340337
}
341338

342-
ret = 0;
343339
/* Register */
344340
list_add_tail(&session->session_list, &all_stat_sessions);
345-
out:
346-
mutex_unlock(&all_stat_sessions_mutex);
347341

348-
return ret;
342+
return 0;
349343
}
350344

351345
void unregister_stat_tracer(struct tracer_stat *trace)

0 commit comments

Comments
 (0)