Skip to content

Commit 1d95fd9

Browse files
committed
ftrace: Switch ftrace.c code over to use guard()
There are a few functions in ftrace.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]> Link: https://lore.kernel.org/[email protected] Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 77e53cb commit 1d95fd9

File tree

1 file changed

+34
-63
lines changed

1 file changed

+34
-63
lines changed

kernel/trace/ftrace.c

Lines changed: 34 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -536,24 +536,21 @@ static int function_stat_show(struct seq_file *m, void *v)
536536
{
537537
struct ftrace_profile *rec = v;
538538
char str[KSYM_SYMBOL_LEN];
539-
int ret = 0;
540539
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
541540
static struct trace_seq s;
542541
unsigned long long avg;
543542
unsigned long long stddev;
544543
#endif
545-
mutex_lock(&ftrace_profile_lock);
544+
guard(mutex)(&ftrace_profile_lock);
546545

547546
/* we raced with function_profile_reset() */
548-
if (unlikely(rec->counter == 0)) {
549-
ret = -EBUSY;
550-
goto out;
551-
}
547+
if (unlikely(rec->counter == 0))
548+
return -EBUSY;
552549

553550
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
554551
avg = div64_ul(rec->time, rec->counter);
555552
if (tracing_thresh && (avg < tracing_thresh))
556-
goto out;
553+
return 0;
557554
#endif
558555

559556
kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
@@ -590,10 +587,8 @@ static int function_stat_show(struct seq_file *m, void *v)
590587
trace_print_seq(m, &s);
591588
#endif
592589
seq_putc(m, '\n');
593-
out:
594-
mutex_unlock(&ftrace_profile_lock);
595590

596-
return ret;
591+
return 0;
597592
}
598593

599594
static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
@@ -944,20 +939,16 @@ ftrace_profile_write(struct file *filp, const char __user *ubuf,
944939

945940
val = !!val;
946941

947-
mutex_lock(&ftrace_profile_lock);
942+
guard(mutex)(&ftrace_profile_lock);
948943
if (ftrace_profile_enabled ^ val) {
949944
if (val) {
950945
ret = ftrace_profile_init();
951-
if (ret < 0) {
952-
cnt = ret;
953-
goto out;
954-
}
946+
if (ret < 0)
947+
return ret;
955948

956949
ret = register_ftrace_profiler();
957-
if (ret < 0) {
958-
cnt = ret;
959-
goto out;
960-
}
950+
if (ret < 0)
951+
return ret;
961952
ftrace_profile_enabled = 1;
962953
} else {
963954
ftrace_profile_enabled = 0;
@@ -968,8 +959,6 @@ ftrace_profile_write(struct file *filp, const char __user *ubuf,
968959
unregister_ftrace_profiler();
969960
}
970961
}
971-
out:
972-
mutex_unlock(&ftrace_profile_lock);
973962

974963
*ppos += cnt;
975964

@@ -5610,20 +5599,15 @@ static DEFINE_MUTEX(ftrace_cmd_mutex);
56105599
__init int register_ftrace_command(struct ftrace_func_command *cmd)
56115600
{
56125601
struct ftrace_func_command *p;
5613-
int ret = 0;
56145602

5615-
mutex_lock(&ftrace_cmd_mutex);
5603+
guard(mutex)(&ftrace_cmd_mutex);
56165604
list_for_each_entry(p, &ftrace_commands, list) {
5617-
if (strcmp(cmd->name, p->name) == 0) {
5618-
ret = -EBUSY;
5619-
goto out_unlock;
5620-
}
5605+
if (strcmp(cmd->name, p->name) == 0)
5606+
return -EBUSY;
56215607
}
56225608
list_add(&cmd->list, &ftrace_commands);
5623-
out_unlock:
5624-
mutex_unlock(&ftrace_cmd_mutex);
56255609

5626-
return ret;
5610+
return 0;
56275611
}
56285612

56295613
/*
@@ -5633,20 +5617,17 @@ __init int register_ftrace_command(struct ftrace_func_command *cmd)
56335617
__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
56345618
{
56355619
struct ftrace_func_command *p, *n;
5636-
int ret = -ENODEV;
56375620

5638-
mutex_lock(&ftrace_cmd_mutex);
5621+
guard(mutex)(&ftrace_cmd_mutex);
5622+
56395623
list_for_each_entry_safe(p, n, &ftrace_commands, list) {
56405624
if (strcmp(cmd->name, p->name) == 0) {
5641-
ret = 0;
56425625
list_del_init(&p->list);
5643-
goto out_unlock;
5626+
return 0;
56445627
}
56455628
}
5646-
out_unlock:
5647-
mutex_unlock(&ftrace_cmd_mutex);
56485629

5649-
return ret;
5630+
return -ENODEV;
56505631
}
56515632

56525633
static int ftrace_process_regex(struct ftrace_iterator *iter,
@@ -5656,7 +5637,7 @@ static int ftrace_process_regex(struct ftrace_iterator *iter,
56565637
struct trace_array *tr = iter->ops->private;
56575638
char *func, *command, *next = buff;
56585639
struct ftrace_func_command *p;
5659-
int ret = -EINVAL;
5640+
int ret;
56605641

56615642
func = strsep(&next, ":");
56625643

@@ -5673,17 +5654,14 @@ static int ftrace_process_regex(struct ftrace_iterator *iter,
56735654

56745655
command = strsep(&next, ":");
56755656

5676-
mutex_lock(&ftrace_cmd_mutex);
5657+
guard(mutex)(&ftrace_cmd_mutex);
5658+
56775659
list_for_each_entry(p, &ftrace_commands, list) {
5678-
if (strcmp(p->name, command) == 0) {
5679-
ret = p->func(tr, hash, func, command, next, enable);
5680-
goto out_unlock;
5681-
}
5660+
if (strcmp(p->name, command) == 0)
5661+
return p->func(tr, hash, func, command, next, enable);
56825662
}
5683-
out_unlock:
5684-
mutex_unlock(&ftrace_cmd_mutex);
56855663

5686-
return ret;
5664+
return -EINVAL;
56875665
}
56885666

56895667
static ssize_t
@@ -8280,7 +8258,7 @@ pid_write(struct file *filp, const char __user *ubuf,
82808258
if (!cnt)
82818259
return 0;
82828260

8283-
mutex_lock(&ftrace_lock);
8261+
guard(mutex)(&ftrace_lock);
82848262

82858263
switch (type) {
82868264
case TRACE_PIDS:
@@ -8296,14 +8274,13 @@ pid_write(struct file *filp, const char __user *ubuf,
82968274
lockdep_is_held(&ftrace_lock));
82978275
break;
82988276
default:
8299-
ret = -EINVAL;
83008277
WARN_ON_ONCE(1);
8301-
goto out;
8278+
return -EINVAL;
83028279
}
83038280

83048281
ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
83058282
if (ret < 0)
8306-
goto out;
8283+
return ret;
83078284

83088285
switch (type) {
83098286
case TRACE_PIDS:
@@ -8332,11 +8309,8 @@ pid_write(struct file *filp, const char __user *ubuf,
83328309

83338310
ftrace_update_pid_func();
83348311
ftrace_startup_all(0);
8335-
out:
8336-
mutex_unlock(&ftrace_lock);
83378312

8338-
if (ret > 0)
8339-
*ppos += ret;
8313+
*ppos += ret;
83408314

83418315
return ret;
83428316
}
@@ -8739,17 +8713,17 @@ static int
87398713
ftrace_enable_sysctl(const struct ctl_table *table, int write,
87408714
void *buffer, size_t *lenp, loff_t *ppos)
87418715
{
8742-
int ret = -ENODEV;
8716+
int ret;
87438717

8744-
mutex_lock(&ftrace_lock);
8718+
guard(mutex)(&ftrace_lock);
87458719

87468720
if (unlikely(ftrace_disabled))
8747-
goto out;
8721+
return -ENODEV;
87488722

87498723
ret = proc_dointvec(table, write, buffer, lenp, ppos);
87508724

87518725
if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
8752-
goto out;
8726+
return ret;
87538727

87548728
if (ftrace_enabled) {
87558729

@@ -8763,8 +8737,7 @@ ftrace_enable_sysctl(const struct ctl_table *table, int write,
87638737
} else {
87648738
if (is_permanent_ops_registered()) {
87658739
ftrace_enabled = true;
8766-
ret = -EBUSY;
8767-
goto out;
8740+
return -EBUSY;
87688741
}
87698742

87708743
/* stopping ftrace calls (just send to ftrace_stub) */
@@ -8774,9 +8747,7 @@ ftrace_enable_sysctl(const struct ctl_table *table, int write,
87748747
}
87758748

87768749
last_ftrace_enabled = !!ftrace_enabled;
8777-
out:
8778-
mutex_unlock(&ftrace_lock);
8779-
return ret;
8750+
return 0;
87808751
}
87818752

87828753
static struct ctl_table ftrace_sysctls[] = {

0 commit comments

Comments
 (0)