Skip to content

Commit 59980d9

Browse files
committed
tracing: Switch trace_events.c code over to use guard()
There are several functions in trace_events.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. Some locations did some simple arithmetic after releasing the lock. As this causes no real overhead for holding a mutex while processing the file position (*ppos += cnt;) let the lock be held over this logic too. 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 4b8d63e commit 59980d9

File tree

1 file changed

+38
-65
lines changed

1 file changed

+38
-65
lines changed

kernel/trace/trace_events.c

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,19 +1546,18 @@ event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
15461546
if (ret)
15471547
return ret;
15481548

1549+
guard(mutex)(&event_mutex);
1550+
15491551
switch (val) {
15501552
case 0:
15511553
case 1:
1552-
mutex_lock(&event_mutex);
15531554
file = event_file_file(filp);
1554-
if (likely(file)) {
1555-
ret = tracing_update_buffers(file->tr);
1556-
if (ret >= 0)
1557-
ret = ftrace_event_enable_disable(file, val);
1558-
} else {
1559-
ret = -ENODEV;
1560-
}
1561-
mutex_unlock(&event_mutex);
1555+
if (!file)
1556+
return -ENODEV;
1557+
ret = tracing_update_buffers(file->tr);
1558+
if (ret < 0)
1559+
return ret;
1560+
ret = ftrace_event_enable_disable(file, val);
15621561
if (ret < 0)
15631562
return ret;
15641563
break;
@@ -2145,7 +2144,7 @@ event_pid_write(struct file *filp, const char __user *ubuf,
21452144
if (ret < 0)
21462145
return ret;
21472146

2148-
mutex_lock(&event_mutex);
2147+
guard(mutex)(&event_mutex);
21492148

21502149
if (type == TRACE_PIDS) {
21512150
filtered_pids = rcu_dereference_protected(tr->filtered_pids,
@@ -2161,7 +2160,7 @@ event_pid_write(struct file *filp, const char __user *ubuf,
21612160

21622161
ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
21632162
if (ret < 0)
2164-
goto out;
2163+
return ret;
21652164

21662165
if (type == TRACE_PIDS)
21672166
rcu_assign_pointer(tr->filtered_pids, pid_list);
@@ -2186,11 +2185,7 @@ event_pid_write(struct file *filp, const char __user *ubuf,
21862185
*/
21872186
on_each_cpu(ignore_task_cpu, tr, 1);
21882187

2189-
out:
2190-
mutex_unlock(&event_mutex);
2191-
2192-
if (ret > 0)
2193-
*ppos += ret;
2188+
*ppos += ret;
21942189

21952190
return ret;
21962191
}
@@ -3257,13 +3252,13 @@ int trace_add_event_call(struct trace_event_call *call)
32573252
int ret;
32583253
lockdep_assert_held(&event_mutex);
32593254

3260-
mutex_lock(&trace_types_lock);
3255+
guard(mutex)(&trace_types_lock);
32613256

32623257
ret = __register_event(call, NULL);
3263-
if (ret >= 0)
3264-
__add_event_to_tracers(call);
3258+
if (ret < 0)
3259+
return ret;
32653260

3266-
mutex_unlock(&trace_types_lock);
3261+
__add_event_to_tracers(call);
32673262
return ret;
32683263
}
32693264
EXPORT_SYMBOL_GPL(trace_add_event_call);
@@ -3517,30 +3512,21 @@ struct trace_event_file *trace_get_event_file(const char *instance,
35173512
return ERR_PTR(ret);
35183513
}
35193514

3520-
mutex_lock(&event_mutex);
3515+
guard(mutex)(&event_mutex);
35213516

35223517
file = find_event_file(tr, system, event);
35233518
if (!file) {
35243519
trace_array_put(tr);
3525-
ret = -EINVAL;
3526-
goto out;
3520+
return ERR_PTR(-EINVAL);
35273521
}
35283522

35293523
/* Don't let event modules unload while in use */
35303524
ret = trace_event_try_get_ref(file->event_call);
35313525
if (!ret) {
35323526
trace_array_put(tr);
3533-
ret = -EBUSY;
3534-
goto out;
3527+
return ERR_PTR(-EBUSY);
35353528
}
35363529

3537-
ret = 0;
3538-
out:
3539-
mutex_unlock(&event_mutex);
3540-
3541-
if (ret)
3542-
file = ERR_PTR(ret);
3543-
35443530
return file;
35453531
}
35463532
EXPORT_SYMBOL_GPL(trace_get_event_file);
@@ -3778,12 +3764,11 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
37783764

37793765
event = strsep(&param, ":");
37803766

3781-
mutex_lock(&event_mutex);
3767+
guard(mutex)(&event_mutex);
37823768

3783-
ret = -EINVAL;
37843769
file = find_event_file(tr, system, event);
37853770
if (!file)
3786-
goto out;
3771+
return -EINVAL;
37873772

37883773
enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
37893774

@@ -3792,40 +3777,34 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
37923777
else
37933778
ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
37943779

3795-
if (glob[0] == '!') {
3796-
ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3797-
goto out;
3798-
}
3799-
3800-
ret = -ENOMEM;
3780+
if (glob[0] == '!')
3781+
return unregister_ftrace_function_probe_func(glob+1, tr, ops);
38013782

38023783
if (param) {
38033784
number = strsep(&param, ":");
38043785

3805-
ret = -EINVAL;
38063786
if (!strlen(number))
3807-
goto out;
3787+
return -EINVAL;
38083788

38093789
/*
38103790
* We use the callback data field (which is a pointer)
38113791
* as our counter.
38123792
*/
38133793
ret = kstrtoul(number, 0, &count);
38143794
if (ret)
3815-
goto out;
3795+
return ret;
38163796
}
38173797

38183798
/* Don't let event modules unload while probe registered */
38193799
ret = trace_event_try_get_ref(file->event_call);
3820-
if (!ret) {
3821-
ret = -EBUSY;
3822-
goto out;
3823-
}
3800+
if (!ret)
3801+
return -EBUSY;
38243802

38253803
ret = __ftrace_event_enable_disable(file, 1, 1);
38263804
if (ret < 0)
38273805
goto out_put;
38283806

3807+
ret = -ENOMEM;
38293808
data = kzalloc(sizeof(*data), GFP_KERNEL);
38303809
if (!data)
38313810
goto out_put;
@@ -3840,23 +3819,20 @@ event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
38403819
* but if it didn't find any functions it returns zero.
38413820
* Consider no functions a failure too.
38423821
*/
3843-
if (!ret) {
3844-
ret = -ENOENT;
3845-
goto out_disable;
3846-
} else if (ret < 0)
3847-
goto out_disable;
3822+
38483823
/* Just return zero, not the number of enabled functions */
3849-
ret = 0;
3850-
out:
3851-
mutex_unlock(&event_mutex);
3852-
return ret;
3824+
if (ret > 0)
3825+
return 0;
38533826

3854-
out_disable:
38553827
kfree(data);
3828+
3829+
if (!ret)
3830+
ret = -ENOENT;
3831+
38563832
__ftrace_event_enable_disable(file, 0, 1);
38573833
out_put:
38583834
trace_event_put_ref(file->event_call);
3859-
goto out;
3835+
return ret;
38603836
}
38613837

38623838
static struct ftrace_func_command event_enable_cmd = {
@@ -4079,20 +4055,17 @@ early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
40794055
{
40804056
int ret;
40814057

4082-
mutex_lock(&event_mutex);
4058+
guard(mutex)(&event_mutex);
40834059

40844060
ret = create_event_toplevel_files(parent, tr);
40854061
if (ret)
4086-
goto out_unlock;
4062+
return ret;
40874063

40884064
down_write(&trace_event_sem);
40894065
__trace_early_add_event_dirs(tr);
40904066
up_write(&trace_event_sem);
40914067

4092-
out_unlock:
4093-
mutex_unlock(&event_mutex);
4094-
4095-
return ret;
4068+
return 0;
40964069
}
40974070

40984071
/* Must be called with event_mutex held */

0 commit comments

Comments
 (0)