Skip to content

Commit 44e64d8

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 d271631 commit 44e64d8

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
@@ -250,6 +250,8 @@ static void eventfs_set_attrs(struct eventfs_inode *ei, bool update_uid, kuid_t
250250
{
251251
struct eventfs_inode *ei_child;
252252

253+
lockdep_assert_held(&eventfs_mutex);
254+
253255
/* Update events/<system>/<event> */
254256
if (WARN_ON_ONCE(level > 3))
255257
return;
@@ -912,3 +914,15 @@ void eventfs_remove_events_dir(struct eventfs_inode *ei)
912914
d_invalidate(dentry);
913915
dput(dentry);
914916
}
917+
918+
int eventfs_remount_lock(void)
919+
{
920+
mutex_lock(&eventfs_mutex);
921+
return srcu_read_lock(&eventfs_srcu);
922+
}
923+
924+
void eventfs_remount_unlock(int srcu_idx)
925+
{
926+
srcu_read_unlock(&eventfs_srcu, srcu_idx);
927+
mutex_unlock(&eventfs_mutex);
928+
}

fs/tracefs/inode.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
336336
struct inode *inode = d_inode(sb->s_root);
337337
struct tracefs_inode *ti;
338338
bool update_uid, update_gid;
339+
int srcu_idx;
339340
umode_t tmp_mode;
340341

341342
/*
@@ -360,6 +361,7 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
360361
update_uid = fsi->opts & BIT(Opt_uid);
361362
update_gid = fsi->opts & BIT(Opt_gid);
362363

364+
srcu_idx = eventfs_remount_lock();
363365
rcu_read_lock();
364366
list_for_each_entry_rcu(ti, &tracefs_inodes, list) {
365367
if (update_uid) {
@@ -381,6 +383,7 @@ static int tracefs_apply_options(struct super_block *sb, bool remount)
381383
eventfs_remount(ti, update_uid, update_gid);
382384
}
383385
rcu_read_unlock();
386+
eventfs_remount_unlock(srcu_idx);
384387
}
385388

386389
return 0;
@@ -426,7 +429,7 @@ static int tracefs_drop_inode(struct inode *inode)
426429
* This inode is being freed and cannot be used for
427430
* eventfs. Clear the flag so that it doesn't call into
428431
* eventfs during the remount flag updates. The eventfs_inode
429-
* gets freed after an RCU cycle, so the content will still
432+
* gets freed after an SRCU cycle, so the content will still
430433
* be safe if the iteration is going on now.
431434
*/
432435
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)