Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <mhiramat@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Link: https://lore.kernel.org/20241223184941.718001540@goodmis.org
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

+36 -65
+36 -65
kernel/trace/ftrace.c
··· 536 536 { 537 537 struct ftrace_profile *rec = v; 538 538 char str[KSYM_SYMBOL_LEN]; 539 - int ret = 0; 540 539 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 541 540 static struct trace_seq s; 542 541 unsigned long long avg; 543 542 unsigned long long stddev; 544 543 #endif 545 - mutex_lock(&ftrace_profile_lock); 544 + guard(mutex)(&ftrace_profile_lock); 546 545 547 546 /* 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; 552 549 553 550 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 554 551 avg = div64_ul(rec->time, rec->counter); 555 552 if (tracing_thresh && (avg < tracing_thresh)) 556 - goto out; 553 + return 0; 557 554 #endif 558 555 559 556 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); ··· 587 590 trace_print_seq(m, &s); 588 591 #endif 589 592 seq_putc(m, '\n'); 590 - out: 591 - mutex_unlock(&ftrace_profile_lock); 592 593 593 - return ret; 594 + return 0; 594 595 } 595 596 596 597 static void ftrace_profile_reset(struct ftrace_profile_stat *stat) ··· 939 944 940 945 val = !!val; 941 946 942 - mutex_lock(&ftrace_profile_lock); 947 + guard(mutex)(&ftrace_profile_lock); 943 948 if (ftrace_profile_enabled ^ val) { 944 949 if (val) { 945 950 ret = ftrace_profile_init(); 946 - if (ret < 0) { 947 - cnt = ret; 948 - goto out; 949 - } 951 + if (ret < 0) 952 + return ret; 950 953 951 954 ret = register_ftrace_profiler(); 952 - if (ret < 0) { 953 - cnt = ret; 954 - goto out; 955 - } 955 + if (ret < 0) 956 + return ret; 956 957 ftrace_profile_enabled = 1; 957 958 } else { 958 959 ftrace_profile_enabled = 0; ··· 959 968 unregister_ftrace_profiler(); 960 969 } 961 970 } 962 - out: 963 - mutex_unlock(&ftrace_profile_lock); 964 971 965 972 *ppos += cnt; 966 973 ··· 5599 5610 __init int register_ftrace_command(struct ftrace_func_command *cmd) 5600 5611 { 5601 5612 struct ftrace_func_command *p; 5602 - int ret = 0; 5603 5613 5604 - mutex_lock(&ftrace_cmd_mutex); 5614 + guard(mutex)(&ftrace_cmd_mutex); 5605 5615 list_for_each_entry(p, &ftrace_commands, list) { 5606 - if (strcmp(cmd->name, p->name) == 0) { 5607 - ret = -EBUSY; 5608 - goto out_unlock; 5609 - } 5616 + if (strcmp(cmd->name, p->name) == 0) 5617 + return -EBUSY; 5610 5618 } 5611 5619 list_add(&cmd->list, &ftrace_commands); 5612 - out_unlock: 5613 - mutex_unlock(&ftrace_cmd_mutex); 5614 5620 5615 - return ret; 5621 + return 0; 5616 5622 } 5617 5623 5618 5624 /* ··· 5617 5633 __init int unregister_ftrace_command(struct ftrace_func_command *cmd) 5618 5634 { 5619 5635 struct ftrace_func_command *p, *n; 5620 - int ret = -ENODEV; 5621 5636 5622 - mutex_lock(&ftrace_cmd_mutex); 5637 + guard(mutex)(&ftrace_cmd_mutex); 5638 + 5623 5639 list_for_each_entry_safe(p, n, &ftrace_commands, list) { 5624 5640 if (strcmp(cmd->name, p->name) == 0) { 5625 - ret = 0; 5626 5641 list_del_init(&p->list); 5627 - goto out_unlock; 5642 + return 0; 5628 5643 } 5629 5644 } 5630 - out_unlock: 5631 - mutex_unlock(&ftrace_cmd_mutex); 5632 5645 5633 - return ret; 5646 + return -ENODEV; 5634 5647 } 5635 5648 5636 5649 static int ftrace_process_regex(struct ftrace_iterator *iter, ··· 5637 5656 struct trace_array *tr = iter->ops->private; 5638 5657 char *func, *command, *next = buff; 5639 5658 struct ftrace_func_command *p; 5640 - int ret = -EINVAL; 5659 + int ret; 5641 5660 5642 5661 func = strsep(&next, ":"); 5643 5662 ··· 5654 5673 5655 5674 command = strsep(&next, ":"); 5656 5675 5657 - mutex_lock(&ftrace_cmd_mutex); 5658 - list_for_each_entry(p, &ftrace_commands, list) { 5659 - if (strcmp(p->name, command) == 0) { 5660 - ret = p->func(tr, hash, func, command, next, enable); 5661 - goto out_unlock; 5662 - } 5663 - } 5664 - out_unlock: 5665 - mutex_unlock(&ftrace_cmd_mutex); 5676 + guard(mutex)(&ftrace_cmd_mutex); 5666 5677 5667 - return ret; 5678 + list_for_each_entry(p, &ftrace_commands, list) { 5679 + if (strcmp(p->name, command) == 0) 5680 + return p->func(tr, hash, func, command, next, enable); 5681 + } 5682 + 5683 + return -EINVAL; 5668 5684 } 5669 5685 5670 5686 static ssize_t ··· 8258 8280 if (!cnt) 8259 8281 return 0; 8260 8282 8261 - mutex_lock(&ftrace_lock); 8283 + guard(mutex)(&ftrace_lock); 8262 8284 8263 8285 switch (type) { 8264 8286 case TRACE_PIDS: ··· 8274 8296 lockdep_is_held(&ftrace_lock)); 8275 8297 break; 8276 8298 default: 8277 - ret = -EINVAL; 8278 8299 WARN_ON_ONCE(1); 8279 - goto out; 8300 + return -EINVAL; 8280 8301 } 8281 8302 8282 8303 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt); 8283 8304 if (ret < 0) 8284 - goto out; 8305 + return ret; 8285 8306 8286 8307 switch (type) { 8287 8308 case TRACE_PIDS: ··· 8309 8332 8310 8333 ftrace_update_pid_func(); 8311 8334 ftrace_startup_all(0); 8312 - out: 8313 - mutex_unlock(&ftrace_lock); 8314 8335 8315 - if (ret > 0) 8316 - *ppos += ret; 8336 + *ppos += ret; 8317 8337 8318 8338 return ret; 8319 8339 } ··· 8713 8739 ftrace_enable_sysctl(const struct ctl_table *table, int write, 8714 8740 void *buffer, size_t *lenp, loff_t *ppos) 8715 8741 { 8716 - int ret = -ENODEV; 8742 + int ret; 8717 8743 8718 - mutex_lock(&ftrace_lock); 8744 + guard(mutex)(&ftrace_lock); 8719 8745 8720 8746 if (unlikely(ftrace_disabled)) 8721 - goto out; 8747 + return -ENODEV; 8722 8748 8723 8749 ret = proc_dointvec(table, write, buffer, lenp, ppos); 8724 8750 8725 8751 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) 8726 - goto out; 8752 + return ret; 8727 8753 8728 8754 if (ftrace_enabled) { 8729 8755 ··· 8737 8763 } else { 8738 8764 if (is_permanent_ops_registered()) { 8739 8765 ftrace_enabled = true; 8740 - ret = -EBUSY; 8741 - goto out; 8766 + return -EBUSY; 8742 8767 } 8743 8768 8744 8769 /* stopping ftrace calls (just send to ftrace_stub) */ ··· 8747 8774 } 8748 8775 8749 8776 last_ftrace_enabled = !!ftrace_enabled; 8750 - out: 8751 - mutex_unlock(&ftrace_lock); 8752 - return ret; 8777 + return 0; 8753 8778 } 8754 8779 8755 8780 static struct ctl_table ftrace_sysctls[] = {