[PATCH 4/5] tracing: Clean up access to trace_event_file from a file pointer

Petr Pavlu posted 5 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH 4/5] tracing: Clean up access to trace_event_file from a file pointer
Posted by Petr Pavlu 1 month, 3 weeks ago
The tracing code provides two functions event_file_file() and
event_file_data() to obtain a trace_event_file pointer from a file struct.
The primary method to use is event_file_file(), as it checks for the
EVENT_FILE_FL_FREED flag to determine whether the event is being removed.
The second function event_file_data() is an optimization for retrieving the
same data when the event_mutex is still held.

In the past, when removing an event directory in remove_event_file_dir(),
the code set i_private to NULL for all event files and readers were
expected to check for this state to recognize that the event is being
removed. In the case of event_id_read(), the value was read using
event_file_data() without acquiring the event_mutex. This required
event_file_data() to use READ_ONCE() when retrieving the i_private data.

With the introduction of eventfs, i_private is assigned when an eventfs
inode is allocated and remains set throughout its lifetime. A previous fix
also modified event_id_read() to read i_private while holding the
event_mutex.

Remove the now unnecessary READ_ONCE() access to i_private in both
event_file_file() and event_file_data(). Add a check in event_file_data()
to ensure that the event_mutex is held and that file->flags doesn't have
the EVENT_FILE_FL_FREED flag set. Finally, move event_file_data()
immediately after event_file_code() since the latter provides a comment
explaining how both functions should be used together.

Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
---
 kernel/trace/trace.h | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h
index c11edec5d8f5..c6b450de93bc 100644
--- a/kernel/trace/trace.h
+++ b/kernel/trace/trace.h
@@ -1739,11 +1739,6 @@ extern struct trace_event_file *find_event_file(struct trace_array *tr,
 						const char *system,
 						const char *event);
 
-static inline void *event_file_data(struct file *filp)
-{
-	return READ_ONCE(file_inode(filp)->i_private);
-}
-
 extern struct mutex event_mutex;
 extern struct list_head ftrace_events;
 
@@ -1764,12 +1759,22 @@ static inline struct trace_event_file *event_file_file(struct file *filp)
 	struct trace_event_file *file;
 
 	lockdep_assert_held(&event_mutex);
-	file = READ_ONCE(file_inode(filp)->i_private);
+	file = file_inode(filp)->i_private;
 	if (!file || file->flags & EVENT_FILE_FL_FREED)
 		return NULL;
 	return file;
 }
 
+static inline void *event_file_data(struct file *filp)
+{
+	struct trace_event_file *file;
+
+	lockdep_assert_held(&event_mutex);
+	file = file_inode(filp)->i_private;
+	WARN_ON(!file || file->flags & EVENT_FILE_FL_FREED);
+	return file;
+}
+
 extern const struct file_operations event_trigger_fops;
 extern const struct file_operations event_hist_fops;
 extern const struct file_operations event_hist_debug_fops;
-- 
2.52.0
Re: [PATCH 4/5] tracing: Clean up access to trace_event_file from a file pointer
Posted by Steven Rostedt 1 month, 2 weeks ago
On Tue, 10 Feb 2026 12:28:19 +0100
Petr Pavlu <petr.pavlu@suse.com> wrote:

> In the past, when removing an event directory in remove_event_file_dir(),
> the code set i_private to NULL for all event files and readers were
> expected to check for this state to recognize that the event is being
> removed. In the case of event_id_read(), the value was read using
> event_file_data() without acquiring the event_mutex. This required
> event_file_data() to use READ_ONCE() when retrieving the i_private data.

I'm OK with this change. Instead of removing the event_file_data() like
you did from event_id_read(), just open code it there. The id is
simple, let's keep it that way.

That is, something like:

	int id = (long)READ_ONCE(file_inode(filp)->i_private);

Feel free to add comments to explain it.

Thanks,

-- Steve