public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [PATCH 14/14] tracing: Switch trace_stat.c code over to use guard()
Date: Thu, 19 Dec 2024 15:12:12 -0500	[thread overview]
Message-ID: <20241219201346.870318466@goodmis.org> (raw)
In-Reply-To: 20241219201158.193821672@goodmis.org

From: Steven Rostedt <rostedt@goodmis.org>

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.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 kernel/trace/trace_stat.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/kernel/trace/trace_stat.c b/kernel/trace/trace_stat.c
index bb247beec447..b3b5586f104d 100644
--- a/kernel/trace/trace_stat.c
+++ b/kernel/trace/trace_stat.c
@@ -128,7 +128,7 @@ static int stat_seq_init(struct stat_session *session)
 	int ret = 0;
 	int i;
 
-	mutex_lock(&session->stat_mutex);
+	guard(mutex)(&session->stat_mutex);
 	__reset_stat_session(session);
 
 	if (!ts->stat_cmp)
@@ -136,11 +136,11 @@ static int stat_seq_init(struct stat_session *session)
 
 	stat = ts->stat_start(ts);
 	if (!stat)
-		goto exit;
+		return 0;
 
 	ret = insert_stat(root, stat, ts->stat_cmp);
 	if (ret)
-		goto exit;
+		return ret;
 
 	/*
 	 * 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)
 			goto exit_free_rbtree;
 	}
 
-exit:
-	mutex_unlock(&session->stat_mutex);
 	return ret;
 
 exit_free_rbtree:
 	__reset_stat_session(session);
-	mutex_unlock(&session->stat_mutex);
 	return ret;
 }
 
@@ -308,7 +305,7 @@ static int init_stat_file(struct stat_session *session)
 int register_stat_tracer(struct tracer_stat *trace)
 {
 	struct stat_session *session, *node;
-	int ret = -EINVAL;
+	int ret;
 
 	if (!trace)
 		return -EINVAL;
@@ -316,18 +313,18 @@ int register_stat_tracer(struct tracer_stat *trace)
 	if (!trace->stat_start || !trace->stat_next || !trace->stat_show)
 		return -EINVAL;
 
+	guard(mutex)(&all_stat_sessions_mutex);
+
 	/* Already registered? */
-	mutex_lock(&all_stat_sessions_mutex);
 	list_for_each_entry(node, &all_stat_sessions, session_list) {
 		if (node->ts == trace)
-			goto out;
+			return -EINVAL;
 	}
 
-	ret = -ENOMEM;
 	/* Init the session */
 	session = kzalloc(sizeof(*session), GFP_KERNEL);
 	if (!session)
-		goto out;
+		return -ENOMEM;
 
 	session->ts = trace;
 	INIT_LIST_HEAD(&session->session_list);
@@ -336,16 +333,13 @@ int register_stat_tracer(struct tracer_stat *trace)
 	ret = init_stat_file(session);
 	if (ret) {
 		destroy_session(session);
-		goto out;
+		return ret;
 	}
 
-	ret = 0;
 	/* Register */
 	list_add_tail(&session->session_list, &all_stat_sessions);
- out:
-	mutex_unlock(&all_stat_sessions_mutex);
 
-	return ret;
+	return 0;
 }
 
 void unregister_stat_tracer(struct tracer_stat *trace)
-- 
2.45.2



      parent reply	other threads:[~2024-12-19 20:13 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-19 20:11 [PATCH 00/14] tracing: Convert over to guard() and __free() infrastructure Steven Rostedt
2024-12-19 20:11 ` [PATCH 01/14] tracing: Switch trace.c code over to use guard() Steven Rostedt
2024-12-20 16:33   ` Markus Elfring
2024-12-19 20:12 ` [PATCH 02/14] tracing: Return -EINVAL if a boot tracer tries to enable the mmiotracer at boot Steven Rostedt
2024-12-19 20:12 ` [PATCH 03/14] tracing: Have event_enable_write() just return error on error Steven Rostedt
2024-12-19 20:12 ` [PATCH 04/14] tracing: Simplify event_enable_func() goto out_free logic Steven Rostedt
2024-12-19 20:12 ` [PATCH 05/14] tracing: Simplify event_enable_func() goto_reg logic Steven Rostedt
2024-12-19 20:12 ` [PATCH 06/14] tracing: Switch trace_events.c code over to use guard() Steven Rostedt
2024-12-19 20:12 ` [PATCH 07/14] tracing: Switch trace_events_hist.c " Steven Rostedt
2024-12-19 20:12 ` [PATCH 08/14] tracing: Switch trace_events_trigger.c " Steven Rostedt
2024-12-20 13:38   ` Steven Rostedt
2024-12-20 16:56   ` Markus Elfring
2024-12-19 20:12 ` [PATCH 09/14] tracing/string: Create and use __free(argv_free) in trace_dynevent.c Steven Rostedt
     [not found]   ` <CAHp75Vc2t_yppC85dnYrzDEEkFBF=NnoCna_PA=8fFFtusg7Ow@mail.gmail.com>
2024-12-20 13:31     ` Steven Rostedt
     [not found]       ` <CAHp75VeWpS8ZUct5MJBbCkpPGmg0=9_t_-RhV5DD4-L4wRnvxQ@mail.gmail.com>
2024-12-20 15:28         ` Steven Rostedt
2024-12-19 20:12 ` [PATCH 10/14] tracing: Switch trace_events_filter.c code over to use guard() Steven Rostedt
2024-12-19 20:12 ` [PATCH 11/14] tracing: Switch trace_events_synth.c " Steven Rostedt
2024-12-19 20:12 ` [PATCH 12/14] tracing: Switch trace_osnoise.c code over to use guard() and __free() Steven Rostedt
2024-12-20 17:08   ` Markus Elfring
2024-12-19 20:12 ` [PATCH 13/14] tracing: Switch trace_stack.c code over to use guard() Steven Rostedt
2024-12-19 20:12 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241219201346.870318466@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox