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

Configure Feed

Select the types of activity you want to include in your feed.

tracing: Clean up access to trace_event_file from a file pointer

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.

Remove the now unnecessary READ_ONCE() access to i_private in both
event_file_file() and event_file_data(). Inline the access to i_private in
remove_event_file_dir(), which allows event_file_data() to handle i_private
solely as a trace_event_file pointer. 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.

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Tom Zanussi <zanussi@kernel.org>
Link: https://patch.msgid.link/20260219162737.314231-5-petr.pavlu@suse.com
Signed-off-by: Petr Pavlu <petr.pavlu@suse.com>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

authored by

Petr Pavlu and committed by
Steven Rostedt (Google)
f0eaed27 f55c09da

+14 -9
+11 -6
kernel/trace/trace.h
··· 1802 1802 const char *system, 1803 1803 const char *event); 1804 1804 1805 - static inline void *event_file_data(struct file *filp) 1806 - { 1807 - return READ_ONCE(file_inode(filp)->i_private); 1808 - } 1809 - 1810 1805 extern struct mutex event_mutex; 1811 1806 extern struct list_head ftrace_events; 1812 1807 ··· 1822 1827 struct trace_event_file *file; 1823 1828 1824 1829 lockdep_assert_held(&event_mutex); 1825 - file = READ_ONCE(file_inode(filp)->i_private); 1830 + file = file_inode(filp)->i_private; 1826 1831 if (!file || file->flags & EVENT_FILE_FL_FREED) 1827 1832 return NULL; 1833 + return file; 1834 + } 1835 + 1836 + static inline void *event_file_data(struct file *filp) 1837 + { 1838 + struct trace_event_file *file; 1839 + 1840 + lockdep_assert_held(&event_mutex); 1841 + file = file_inode(filp)->i_private; 1842 + WARN_ON(!file || file->flags & EVENT_FILE_FL_FREED); 1828 1843 return file; 1829 1844 } 1830 1845
+3 -3
kernel/trace/trace_events.c
··· 2184 2184 static ssize_t 2185 2185 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos) 2186 2186 { 2187 - int id = (long)event_file_data(filp); 2187 + /* id is directly in i_private and available for inode's lifetime. */ 2188 + int id = (long)file_inode(filp)->i_private; 2188 2189 char buf[32]; 2189 2190 int len; 2190 2191 2191 - if (unlikely(!id)) 2192 - return -ENODEV; 2192 + WARN_ON(!id); 2193 2193 2194 2194 len = sprintf(buf, "%d\n", id); 2195 2195