Skip to contentSkip to conversation

tracing: Use refcount for trace_event_file reference counter

PATCH In review Steven Rostedt 2 participants 4 messages Jul 26, 2024 – Jul 30, 2024 Tracing

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

PATCH 1/1 tracing: Use refcount for trace_event_file reference counter

From: Steven Rostedt <[email protected]>

Instead of using an atomic counter for the trace_event_file reference counter, use the refcount interface. It has various checks to make sure the reference counting is correct, and will warn if it detects an error (like refcount_inc() on '0').

---
 include/linux/trace_events.h | 2 +-
 kernel/trace/trace_events.c  | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/trace_events.h +1 -1
index 9df3e2973626..fed58e54f15e 100644
@@ -680,7 +680,7 @@ struct trace_event_file {
 	 * caching and such. Which is mostly OK ;-)
 	 */
 	unsigned long		flags;
-	atomic_t		ref;	/* ref count for opened files */
+	refcount_t		ref;	/* ref count for opened files */
 	atomic_t		sm_ref;	/* soft-mode reference counter */
 	atomic_t		tm_ref;	/* trigger-mode reference counter */
 };
kernel/trace/trace_events.c +4 -4
index 852643d957de..81ade9ddcbe5 100644
@@ -992,18 +992,18 @@ static void remove_subsystem(struct trace_subsystem_dir *dir)
 
 void event_file_get(struct trace_event_file *file)
 {
-	atomic_inc(&file->ref);
+	refcount_inc(&file->ref);
 }
 
 void event_file_put(struct trace_event_file *file)
 {
-	if (WARN_ON_ONCE(!atomic_read(&file->ref))) {
+	if (WARN_ON_ONCE(!refcount_read(&file->ref))) {
 		if (file->flags & EVENT_FILE_FL_FREED)
 			kmem_cache_free(file_cachep, file);
 		return;
 	}
 
-	if (atomic_dec_and_test(&file->ref)) {
+	if (refcount_dec_and_test(&file->ref)) {
 		/* Count should only go to zero when it is freed */
 		if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED)))
 			return;
@@ -2999,7 +2999,7 @@ trace_create_new_event(struct trace_event_call *call,
 	atomic_set(&file->tm_ref, 0);
 	INIT_LIST_HEAD(&file->triggers);
 	list_add(&file->list, &tr->events);
-	event_file_get(file);
+	refcount_set(&file->ref, 1);
 
 	return file;
 }
Signature · 1 line
2.43.0
#1 lore raw
Quoted 8 lines · On Fri, 26 Jul 2024 14:42:08 -0400 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

Instead of using an atomic counter for the trace_event_file reference counter, use the refcount interface. It has various checks to make sure the reference counting is correct, and will warn if it detects an error (like refcount_inc() on '0').

Looks good to me.

Masami Hiramatsu (Google) <[email protected]>

Thanks,

Quoted 56 lines
---
 include/linux/trace_events.h | 2 +-
 kernel/trace/trace_events.c  | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/linux/trace_events.h +1 -1
index 9df3e2973626..fed58e54f15e 100644
@@ -680,7 +680,7 @@ struct trace_event_file {
 	 * caching and such. Which is mostly OK ;-)
 	 */
 	unsigned long		flags;
-	atomic_t		ref;	/* ref count for opened files */
+	refcount_t		ref;	/* ref count for opened files */
 	atomic_t		sm_ref;	/* soft-mode reference counter */
 	atomic_t		tm_ref;	/* trigger-mode reference counter */
 };
kernel/trace/trace_events.c +4 -4
index 852643d957de..81ade9ddcbe5 100644
@@ -992,18 +992,18 @@ static void remove_subsystem(struct trace_subsystem_dir *dir)
 
 void event_file_get(struct trace_event_file *file)
 {
-	atomic_inc(&file->ref);
+	refcount_inc(&file->ref);
 }
 
 void event_file_put(struct trace_event_file *file)
 {
-	if (WARN_ON_ONCE(!atomic_read(&file->ref))) {
+	if (WARN_ON_ONCE(!refcount_read(&file->ref))) {
 		if (file->flags & EVENT_FILE_FL_FREED)
 			kmem_cache_free(file_cachep, file);
 		return;
 	}
 
-	if (atomic_dec_and_test(&file->ref)) {
+	if (refcount_dec_and_test(&file->ref)) {
 		/* Count should only go to zero when it is freed */
 		if (WARN_ON_ONCE(!(file->flags & EVENT_FILE_FL_FREED)))
 			return;
@@ -2999,7 +2999,7 @@ trace_create_new_event(struct trace_event_call *call,
 	atomic_set(&file->tm_ref, 0);
 	INIT_LIST_HEAD(&file->triggers);
 	list_add(&file->list, &tr->events);
-	event_file_get(file);
+	refcount_set(&file->ref, 1);
 
 	return file;
 }
Signature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#2 lore raw
Quoted 10 lines · On Mon, 29 Jul 2024 23:49:24 +0900 Masami Hiramatsu (Google)…
Quoted 7 lines · On Fri, 26 Jul 2024 14:42:08 -0400 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

Instead of using an atomic counter for the trace_event_file reference counter, use the refcount interface. It has various checks to make sure the reference counting is correct, and will warn if it detects an error (like refcount_inc() on '0').

Quoted 5 lines
Quoted 1 line

Looks good to me.

Masami Hiramatsu (Google) <[email protected]>

Is that an ack or a review? ;-)

-- Steve

maintainer Acked-by
#3 lore raw
Quoted 13 lines · On Mon, 29 Jul 2024 19:02:41 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines · On Mon, 29 Jul 2024 23:49:24 +0900 Masami Hiramatsu (Google)…
Quoted 7 lines · On Fri, 26 Jul 2024 14:42:08 -0400 Steven Rostedt <[email protected]> wrote:

From: Steven Rostedt <[email protected]>

Instead of using an atomic counter for the trace_event_file reference counter, use the refcount interface. It has various checks to make sure the reference counting is correct, and will warn if it detects an error (like refcount_inc() on '0').

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Looks good to me.

Masami Hiramatsu (Google) <[email protected]>

Is that an ack or a review? ;-)

It was Ack,

Sorry about that. I might be too sleep.

Quoted 3 lines

-- Steve

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>