Skip to content

Commit ae9cd0b

Browse files
devnexengregkh
authored andcommitted
eventfs: Hold eventfs_mutex and SRCU when remount walks events
commit 07004a8 upstream. Commit 340f0c7 ("eventfs: Update all the eventfs_inodes from the events descriptor") had eventfs_set_attrs() recurse through ei->children on remount. The walk only holds the rcu_read_lock() taken by tracefs_apply_options() over tracefs_inodes, which is wrong: - list_for_each_entry over ei->children races with the list_del_rcu() in eventfs_remove_rec() -- LIST_POISON1 deref, same shape as d260327. - eventfs_inodes are freed via call_srcu(&eventfs_srcu, ...). rcu_read_lock() does not extend an SRCU grace period, so ti->private can be reclaimed under the walk. - The writes to ei->attr race with eventfs_set_attr(), which holds eventfs_mutex. Reproducer: while :; do mount -o remount,uid=$((RANDOM%1000)) /sys/kernel/tracing; done & while :; do echo "p:kp submit_bio" > /sys/kernel/tracing/kprobe_events echo > /sys/kernel/tracing/kprobe_events done Wrap the events portion of tracefs_apply_options() in eventfs_remount_lock()/_unlock() that take eventfs_mutex and srcu_read_lock(&eventfs_srcu). eventfs_set_attrs() doesn't sleep so the nested rcu_read_lock() is fine; lockdep_assert_held() pins the contract. Comment in tracefs_drop_inode() said "RCU cycle" -- it is SRCU. Fixes: 340f0c7 ("eventfs: Update all the eventfs_inodes from the events descriptor") Cc: stable@vger.kernel.org Link: https://patch.msgid.link/20260418191737.10289-1-devnexen@gmail.com Signed-off-by: David Carlier <devnexen@gmail.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f0b0b09 commit ae9cd0b

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

fs/tracefs/event_inode.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ static void eventfs_set_attrs(struct eventfs_inode *ei, bool update_uid, kuid_t
310310
{
311311
struct eventfs_inode *ei_child;
312312

313+
lockdep_assert_held(&eventfs_mutex);
314+
313315
/* Update events/<system>/<event> */
314316
if (WARN_ON_ONCE(level > 3))
315317
return;
@@ -985,3 +987,15 @@ void eventfs_remove_events_dir(struct eventfs_inode *ei)
985987
d_invalidate(dentry);
986988
dput(dentry);
987989
}
990+
991+
int eventfs_remount_lock(void)
992+
{
993+
mutex_lock(&eventfs_mutex);
994+
return srcu_read_lock(&eventfs_srcu);
995+
}
996+
997+
void eventfs_remount_unlock(int srcu_idx)
998+
{
999+
srcu_read_unlock(&eventfs_srcu, srcu_idx);
1000+
mutex_unlock(&eventfs_mutex);
1001+
}

fs/tracefs/inode.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
362362
struct tracefs_mount_opts *opts = &fsi->mount_opts;
363363
struct tracefs_inode *ti;
364364
bool update_uid, update_gid;
365+
int srcu_idx;
365366
umode_t tmp_mode;
366367

367368
/*
@@ -386,6 +387,7 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
386387
update_uid = opts->opts & BIT(Opt_uid);
387388
update_gid = opts->opts & BIT(Opt_gid);
388389

390+
srcu_idx = eventfs_remount_lock();
389391
rcu_read_lock();
390392
list_for_each_entry_rcu(ti, &tracefs_inodes, list) {
391393
if (update_uid)
@@ -398,6 +400,7 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
398400
eventfs_remount(ti, update_uid, update_gid);
399401
}
400402
rcu_read_unlock();
403+
eventfs_remount_unlock(srcu_idx);
401404
}
402405

403406
return 0;
@@ -444,7 +447,7 @@ static int tracefs_drop_inode(struct inode *inode)
444447
* This inode is being freed and cannot be used for
445448
* eventfs. Clear the flag so that it doesn't call into
446449
* eventfs during the remount flag updates. The eventfs_inode
447-
* gets freed after an RCU cycle, so the content will still
450+
* gets freed after an SRCU cycle, so the content will still
448451
* be safe if the iteration is going on now.
449452
*/
450453
ti->flags &= ~TRACEFS_EVENT_INODE;

fs/tracefs/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,7 @@ struct inode *tracefs_get_inode(struct super_block *sb);
7676
void eventfs_remount(struct tracefs_inode *ti, bool update_uid, bool update_gid);
7777
void eventfs_d_release(struct dentry *dentry);
7878

79+
int eventfs_remount_lock(void);
80+
void eventfs_remount_unlock(int srcu_idx);
81+
7982
#endif /* _TRACEFS_INTERNAL_H */

0 commit comments

Comments
 (0)