The eventfs inodes and directories are allocated when referenced. But this
leaves the issue of keeping consistent inode numbers and the number is
only saved in the inode structure itself. When the inode is no longer
referenced, it can be freed. When the file that the inode was representing
is referenced again, the inode is once again created, but the inode number
needs to be the same as it was before.
Just making the inode numbers the same for all files is fine, but that
does not work with directories. The find command will check for loops via
the inode number and having the same inode number for directories triggers:
# find /sys/kernel/tracing
find: File system loop detected;
'/sys/kernel/debug/tracing/events/initcall/initcall_finish' is part of the same file system loop as
'/sys/kernel/debug/tracing/events/initcall'.
[..]
Linus pointed out that the eventfs_inode structure ends with a single
32bit int, and on 64 bit machines, there's likely a 4 byte hole due to
alignment. We can use this hole to store the inode number for the
eventfs_inode. All directories in eventfs are represented by an
eventfs_inode and that data structure can hold its inode number.
That last int was also purposely placed at the end of the structure to
prevent holes from within. Now that there's a 4 byte number to hold the
inode, both the inode number and the last integer can be moved up in the
structure for better cache locality, where the llist and rcu fields can be
moved to the end as they are only used when the eventfs_inode is being
deleted.
Link: https://lore.kernel.org/all/CAMuHMdXKiorg-jiuKoZpfZyDJ3Ynrfb8=X+c7x0Eewxn-YRdCA@mail.gmail.com/
Link: https://lore.kernel.org/linux-trace-kernel/20240122152748.46897388@gandalf.local.home
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>
Tested-by: Geert Uytterhoeven <geert+renesas@glider.be>
Fixes: 53c41052ba
("eventfs: Have the inodes all for files and directories all be the same")
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
88 lines
2.8 KiB
C
88 lines
2.8 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
#ifndef _TRACEFS_INTERNAL_H
|
|
#define _TRACEFS_INTERNAL_H
|
|
|
|
enum {
|
|
TRACEFS_EVENT_INODE = BIT(1),
|
|
TRACEFS_EVENT_TOP_INODE = BIT(2),
|
|
TRACEFS_GID_PERM_SET = BIT(3),
|
|
TRACEFS_UID_PERM_SET = BIT(4),
|
|
TRACEFS_INSTANCE_INODE = BIT(5),
|
|
};
|
|
|
|
struct tracefs_inode {
|
|
unsigned long flags;
|
|
void *private;
|
|
struct inode vfs_inode;
|
|
};
|
|
|
|
/*
|
|
* struct eventfs_attr - cache the mode and ownership of a eventfs entry
|
|
* @mode: saved mode plus flags of what is saved
|
|
* @uid: saved uid if changed
|
|
* @gid: saved gid if changed
|
|
*/
|
|
struct eventfs_attr {
|
|
int mode;
|
|
kuid_t uid;
|
|
kgid_t gid;
|
|
};
|
|
|
|
/*
|
|
* struct eventfs_inode - hold the properties of the eventfs directories.
|
|
* @list: link list into the parent directory
|
|
* @entries: the array of entries representing the files in the directory
|
|
* @name: the name of the directory to create
|
|
* @children: link list into the child eventfs_inode
|
|
* @dentry: the dentry of the directory
|
|
* @d_parent: pointer to the parent's dentry
|
|
* @d_children: The array of dentries to represent the files when created
|
|
* @entry_attrs: Saved mode and ownership of the @d_children
|
|
* @attr: Saved mode and ownership of eventfs_inode itself
|
|
* @data: The private data to pass to the callbacks
|
|
* @is_freed: Flag set if the eventfs is on its way to be freed
|
|
* Note if is_freed is set, then dentry is corrupted.
|
|
* @nr_entries: The number of items in @entries
|
|
*/
|
|
struct eventfs_inode {
|
|
struct list_head list;
|
|
const struct eventfs_entry *entries;
|
|
const char *name;
|
|
struct list_head children;
|
|
struct dentry *dentry; /* Check is_freed to access */
|
|
struct dentry *d_parent;
|
|
struct dentry **d_children;
|
|
struct eventfs_attr *entry_attrs;
|
|
struct eventfs_attr attr;
|
|
void *data;
|
|
unsigned int is_freed:1;
|
|
unsigned int is_events:1;
|
|
unsigned int nr_entries:30;
|
|
unsigned int ino;
|
|
/*
|
|
* Union - used for deletion
|
|
* @llist: for calling dput() if needed after RCU
|
|
* @rcu: eventfs_inode to delete in RCU
|
|
*/
|
|
union {
|
|
struct llist_node llist;
|
|
struct rcu_head rcu;
|
|
};
|
|
};
|
|
|
|
static inline struct tracefs_inode *get_tracefs(const struct inode *inode)
|
|
{
|
|
return container_of(inode, struct tracefs_inode, vfs_inode);
|
|
}
|
|
|
|
struct dentry *tracefs_start_creating(const char *name, struct dentry *parent);
|
|
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_start_creating(const char *name, struct dentry *parent);
|
|
struct dentry *eventfs_failed_creating(struct dentry *dentry);
|
|
struct dentry *eventfs_end_creating(struct dentry *dentry);
|
|
void eventfs_update_gid(struct dentry *dentry, kgid_t gid);
|
|
void eventfs_set_ei_status_free(struct tracefs_inode *ti, struct dentry *dentry);
|
|
|
|
#endif /* _TRACEFS_INTERNAL_H */
|