tracefs: Add read-only eventfs filesystem at /sys/kernel/events
Steven Rostedt
maintainerPATCH v2 1/1 tracefs: Add read-only eventfs filesystem at /sys/kernel/events
From: Anubhav Shelat <[email protected]>
Introduce a new read-only pseudo-filesystem "eventfs" mounted at /sys/kernel/events that exposes trace event format and id files (mode 0444) to unprivileged users. This allows tools like perf to discover event formats without requiring access to the full tracefs/debugfs mount.
The eventfs file system reuses the existing eventfs_inode lazy-lookup infrastructure. A new set of super_operations (eventfs_ro_super_operations) shares the tracefs inode allocator so that eventfs_get_inode() and get_tracefs() work on the RO superblock. The superblock is manually initialized to ensure the root inode is allocated with tracefs_alloc_inode, allowing the root to serve directly as the events directory without another events subdirectory.
Each qualifying event gets a subsystem directory containing format and id files. The top-level events directory also exposes header_page and header_event. Similar to tracefs, a change will need to be made in systemd to mount this filesystem automatically.
[
Steven Rostedt rewrote to use the lookup functions to determine read only files to show. The struct eventfs_entry now has a "readonly" field and if it is set, it will be displayed in the read only file system. The original code [1] duplicated the eventfs_inodes for every event and subsystem. The new approach uses the existing eventfs_inodes (without requiring more memory to store them) and uses the eventfs_entry arrays to show which files are allowed to be read readonly or not.
[1] https://patch.msgid.link/[email protected]
]
---
Changes since v1: (the above link)
- Rewrote to reuse the evnetfs_inodes with lookup functions. I kept Anubhav as author as there's still code unmodified from the original patch.
fs/tracefs/event_inode.c | 110 +++++++++++++++++++++++++++++++++--- fs/tracefs/inode.c | 95 ++++++++++++++++++++++++++++++- fs/tracefs/internal.h | 3 + include/linux/tracefs.h | 3 + include/uapi/linux/magic.h | 1 + kernel/trace/trace_events.c | 12 ++++ 6 files changed, 214 insertions(+), 10 deletions(-)
fs/tracefs/event_inode.c +102 -8
@@ -151,7 +151,11 @@ static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)static struct dentry *eventfs_root_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags); +static struct dentry *eventfs_root_lookup_ro (struct inode *dir, +struct dentry *dentry, +unsigned int flags);static int eventfs_iterate (struct file *file,struct dir_context *ctx); +static int eventfs_ro_iterate (struct file *file,struct dir_context *ctx);static void update_attr (struct eventfs_attr *attr,struct iattr *iattr) {
@@ -229,6 +233,10 @@ static const struct inode_operations eventfs_dir_inode_operations = { .setattr = eventfs_set_attr, }; +static const struct inode_operations eventfs_ro_dir_inode_operations = { + .lookup = eventfs_root_lookup_ro, +}; +static const struct inode_operations eventfs_file_inode_operations = { .setattr = eventfs_set_attr, };
@@ -239,6 +247,12 @@ static const struct file_operations eventfs_file_operations = { .llseek = generic_file_llseek, }; +static const struct file_operations eventfs_ro_file_operations = { + .read = generic_read_dir, + .iterate_shared = eventfs_ro_iterate, + .llseek = generic_file_llseek, +}; +static void eventfs_set_attrs (struct eventfs_inode *ei,bool update_uid,kuid_t uid,bool update_gid,kgid_t gid,int level) {
@@ -402,7 +416,7 @@ static struct dentry *lookup_file(struct eventfs_inode *parent_ei,* a eventfs_inode. */ static struct dentry *lookup_dir_entry (struct dentry *dentry, -struct eventfs_inode *pei,struct eventfs_inode *ei) +struct eventfs_inode *pei,struct eventfs_inode *ei,bool ro) {struct inode *inode;umode_t mode =S_IFDIR |S_IRWXU |S_IRUGO |S_IXUGO ;
@@ -411,8 +425,13 @@ static struct dentry *lookup_dir_entry(struct dentry *dentry,if (unlikely (!inode))return ERR_PTR (-ENOMEM ); - inode->i_op = &eventfs_dir_inode_operations; - inode->i_fop = &eventfs_file_operations; +if (ro) { + inode->i_op = &eventfs_ro_dir_inode_operations; + inode->i_fop = &eventfs_ro_file_operations; + }else { + inode->i_op = &eventfs_dir_inode_operations; + inode->i_fop = &eventfs_file_operations; + }/* All directories will have the same inode number */ inode->i_ino =eventfs_dir_ino (ei);
@@ -514,9 +533,10 @@ lookup_file_dentry(struct dentry *dentry,* list, if @dentry found go ahead and create the file/dir */ -static struct dentry *eventfs_root_lookup (struct inode *dir, -struct dentry *dentry, -unsigned int flags) +static struct dentry *__eventfs_root_lookup (struct inode *dir, +struct dentry *dentry, +unsigned int flags, +bool ro) {struct eventfs_inode *ei_child;struct tracefs_inode *ti;
@@ -539,7 +559,7 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,/* A child is freed and removed from the list at the same time */ if (WARN_ON_ONCE (ei_child->is_freed))return NULL ; -return lookup_dir_entry (dentry, ei, ei_child); +return lookup_dir_entry (dentry, ei, ei_child, ro); }for (int i =0 ; i < ei->nr_entries; i++) {
@@ -551,20 +571,40 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,if (strcmp (name, entry->name) !=0 )continue ; +if (ro && !entry->read_only) +return NULL ; + data = ei->data;if (entry->callback (name, &mode, &data, &fops) <=0 )return NULL ; +if (ro) + mode |=0444 ; +return lookup_file_dentry (dentry, ei, i, mode, data, fops); }return NULL ; } +static struct dentry *eventfs_root_lookup (struct inode *dir, +struct dentry *dentry, +unsigned int flags) +{ +return __eventfs_root_lookup (dir, dentry, flags, false); +} + +static struct dentry *eventfs_root_lookup_ro (struct inode *dir, +struct dentry *dentry, +unsigned int flags) +{ +return __eventfs_root_lookup (dir, dentry, flags, true); +} +/* * Walk the children of a eventfs_inode to fill in getdents(). */ -static int eventfs_iterate (struct file *file,struct dir_context *ctx) +static int __eventfs_iterate (struct file *file,struct dir_context *ctx,bool ro) {const struct file_operations *fops;struct inode *f_inode =file_inode (file);
@@ -606,6 +646,9 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) entry = &ei->entries[i]; name = entry->name; +if (ro && !entry->read_only) +continue ; +/* If ei->is_freed then just bail here, nothing more to do */ scoped_guard(mutex, &eventfs_mutex) {if (ei->is_freed)
@@ -650,6 +693,16 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)return 1 ; } +static int eventfs_iterate (struct file *file,struct dir_context *ctx) +{ +return __eventfs_iterate (file, ctx, false); +} + +static int eventfs_ro_iterate (struct file *file,struct dir_context *ctx) +{ +return __eventfs_iterate (file, ctx, true); +} +/** * eventfs_create_dir - Create the eventfs_inode for this directory * @name: The name of the directory to create.
@@ -812,6 +865,47 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentryreturn ERR_PTR (-ENOMEM ); } +/** +* eventfs_create_events_dir_ro - create a read-only events directory +* @name: The name of the top level directory to create. +* @entries: A list of entries that represent the files under this directory +* @size: The number of @entries +* @data: The default data to pass to the files (an entry may override it). +* +* This function configures the eventfs filesystem root as a read-only +* trace event directory using the existing eventfs_inode lazy-lookup +* infrastructure. +* +* See eventfs_create_dir() for use of @entries. +*/ +int eventfs_create_events_ro_copy (const char *name,struct eventfs_inode *ei) +{ +struct dentry *dentry; +struct tracefs_inode *ti; +struct inode *inode; + + dentry =eventfs_ro_get_root (); +if (IS_ERR (dentry)) +return PTR_ERR (dentry); + + inode =d_inode (dentry); + +INIT_LIST_HEAD (&ei->children); +INIT_LIST_HEAD (&ei->list); + + ti =get_tracefs (inode); + ti->flags |=TRACEFS_EVENT_INODE ; + ti->private = ei; + + inode->i_op = &eventfs_ro_dir_inode_operations; + inode->i_fop = &eventfs_ro_file_operations; + +/* This is never freed */ + dentry->d_fsdata =get_ei (ei); + +return 0 ; +} +/** * eventfs_remove_rec - remove eventfs dir or file from list * @ei: eventfs_inode to be removed.
fs/tracefs/inode.c +93 -2
119 lines not on this page · raw message
fs/tracefs/internal.h +3 -0
@@ -73,6 +73,9 @@ struct dentry *tracefs_end_creating(struct dentry *dentry);struct dentry *tracefs_failed_creating (struct dentry *dentry);struct inode *tracefs_get_inode (struct super_block *sb); +struct dentry *eventfs_ro_get_root (void ); +void eventfs_ro_put_root (void ); +void eventfs_remount (struct tracefs_inode *ti,bool update_uid,bool update_gid);void eventfs_d_release (struct dentry *dentry);
include/linux/tracefs.h +3 -0
@@ -75,6 +75,7 @@ struct eventfs_entry {const char *name;eventfs_callback callback;eventfs_release release; +bool read_only; };struct eventfs_inode ;
@@ -87,6 +88,8 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inodeconst struct eventfs_entry *entries, int size, void *data); +int eventfs_create_events_ro_copy (const char *name,struct eventfs_inode *ei); +void eventfs_remove_events_dir (struct eventfs_inode *ei);void eventfs_remove_dir (struct eventfs_inode *ei);
include/uapi/linux/magic.h +1 -0
@@ -75,6 +75,7 @@#define STACK_END_MAGIC 0x57AC6E9D#define TRACEFS_MAGIC 0x74726163 +#define EVENTFS_SUPER_MAGIC 0x65766673#define V9FS_MAGIC 0x01021997
kernel/trace/trace_events.c +12 -0
@@ -3142,11 +3142,13 @@ event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file) { .name = "format", .callback = event_callback, + .read_only = true, }, #ifdef CONFIG_PERF_EVENTS { .name = "id", .callback = event_callback, + .read_only = true, }, #endif #define NR_RO_EVENT_ENTRIES (1 + IS_ENABLED(CONFIG_PERF_EVENTS))
@@ -4545,6 +4547,7 @@ static int events_callback(const char *name, umode_t *mode, void **data,static int create_event_toplevel_files (struct dentry *parent,struct trace_array *tr) { +static bool event_dir_ro_created;struct eventfs_inode *e_events;struct dentry *entry;int nr_entries;
@@ -4552,10 +4555,12 @@ create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) { .name = "header_page", .callback = events_callback, + .read_only = true, }, { .name = "header_event", .callback = events_callback, + .read_only = true, }, #define NR_RO_TOP_ENTRIES 2 /* Readonly files must be above this line and counted by NR_RO_TOP_ENTRIES. */
@@ -4598,6 +4603,13 @@ create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) tr->event_dir = e_events; +if (!event_dir_ro_created && (tr->flags &TRACE_ARRAY_FL_GLOBAL )) { +int ret; + + ret =eventfs_create_events_ro_copy ("events" , e_events); +if (!ret) + event_dir_ro_created = true; + }return 0 ; }
Signature · 1 line
2.53.0
Steven Rostedt
maintainerReplying to Sashiko: https://sashiko.dev/#/patchset/20260808131104.2ad18f65%40robin
Quoted 50 lines
--- Patch [1]: [PATCH v2] tracefs: Add read-only eventfs filesystem at
/sys/kernel/events--- commit29ceffbc05b0ff3121930ed049314a6007ea355b
Author: Anubhav Shelat <[email protected]>tracefs: Add read-only eventfs filesystem at
/sys/kernel/eventsThis commit introduces a new read-only pseudo-filesystem "eventfs" mounted at
/sys/kernel/eventsthat exposes trace event format and id files to unprivileged users. The eventfs file system reuses the existingeventfs_inodelazy-lookup infrastructure.Quoted 4 lines
fs/tracefs/event_inode.c +0
-0[ ... ]
Quoted 17 lines
fs/tracefs/event_inode.c +17
-0+int eventfs_create_events_ro_copy (const char *name,struct eventfs_inode *ei) +{ +struct dentry *dentry; +struct tracefs_inode *ti; +struct inode *inode; + + dentry =eventfs_ro_get_root (); +if (IS_ERR (dentry)) +return PTR_ERR (dentry); + + inode =d_inode (dentry); + +INIT_LIST_HEAD (&ei->children); +INIT_LIST_HEAD (&ei->list); + + ti =get_tracefs (inode); + ti->flags |=TRACEFS_EVENT_INODE ;fs/tracefs/event_inode.c struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentry · line 907
Quoted 10 lines
fs/tracefs/event_inode.c +10
-0+ ti->private = ei; + + inode->i_op = &eventfs_ro_dir_inode_operations; + inode->i_fop = &eventfs_ro_file_operations; + +/* This is never freed */ + dentry->d_fsdata =get_ei (ei); + +return 0 ; +}Does this code leak the dentry reference returned by
eventfs_ro_get_root()?
Quoted 4 lines
Looking at
eventfs_ro_get_root()infs/tracefs/inode.c, it returns the dentry usingdget(eventfs_ro_mount->mnt_root). However,eventfs_create_events_ro_copy()never calls dput(dentry) to balance the reference count, nor does it save the dentry pointer to a global variable for later cleanup.
It's not really a leak. Yes it is not freed, but once allocated, it is never freed. That's fine because it lasts for the remander of the boot and has only a single instance.
The comment above assigning d_fsdata states this. I could add another comment bave the eventfs_ro_get_root(). (I wonder if Sashiko will understand it?)
Quoted 15 lines
Quoted 5 lines
fs/tracefs/inode.c +0
-0[ ... ]
fs/tracefs/inode.c static struct file_system_type trace_fs_type = { · line 608
Quoted 4 lines
fs/tracefs/inode.c +4
-0+void eventfs_ro_put_root (void ) +{ +simple_release_fs (&eventfs_ro_mount, &eventfs_ro_mount_count); +}Is
eventfs_ro_put_root()ever called?
Quoted 3 lines
This teardown function appears to be explicitly introduced to balance the internal mount pin created by
simple_pin_fs()ineventfs_ro_get_root(), but it seems to remain completely unreferenced in the codebase.
Hmm, right. This was leftover from Anubhav's first patch that could fail after the eventfs_ro_get_root(). This version does not fail so it doesn't need this function. I'll remove it.
-- Steve