Skip to content

Commit 2c18a63

Browse files
committed
super: wait until we passed kill super
Recent rework moved block device closing out of sb->put_super() and into sb->kill_sb() to avoid deadlocks as s_umount is held in put_super() and blkdev_put() can end up taking s_umount again. That means we need to move the removal of the superblock from @fs_supers out of generic_shutdown_super() and into deactivate_locked_super() to ensure that concurrent mounters don't fail to open block devices that are still in use because blkdev_put() in sb->kill_sb() hasn't been called yet. We can now do this as we can make iterators through @fs_super and @super_blocks wait without holding s_umount. Concurrent mounts will wait until a dying superblock is fully dead so until sb->kill_sb() has been called and SB_DEAD been set. Concurrent iterators can already discard any SB_DYING superblock. Reviewed-by: Jan Kara <jack@suse.cz> Message-Id: <20230818-vfs-super-fixes-v3-v3-4-9f0b1876e46b@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 5e87491 commit 2c18a63

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

fs/super.c

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ static inline bool super_lock_excl(struct super_block *sb)
153153
}
154154

155155
/* wake waiters */
156-
#define SUPER_WAKE_FLAGS (SB_BORN | SB_DYING)
156+
#define SUPER_WAKE_FLAGS (SB_BORN | SB_DYING | SB_DEAD)
157157
static void super_wake(struct super_block *sb, unsigned int flag)
158158
{
159159
WARN_ON_ONCE((flag & ~SUPER_WAKE_FLAGS));
@@ -461,6 +461,25 @@ void deactivate_locked_super(struct super_block *s)
461461
list_lru_destroy(&s->s_dentry_lru);
462462
list_lru_destroy(&s->s_inode_lru);
463463

464+
/*
465+
* Remove it from @fs_supers so it isn't found by new
466+
* sget{_fc}() walkers anymore. Any concurrent mounter still
467+
* managing to grab a temporary reference is guaranteed to
468+
* already see SB_DYING and will wait until we notify them about
469+
* SB_DEAD.
470+
*/
471+
spin_lock(&sb_lock);
472+
hlist_del_init(&s->s_instances);
473+
spin_unlock(&sb_lock);
474+
475+
/*
476+
* Let concurrent mounts know that this thing is really dead.
477+
* We don't need @sb->s_umount here as every concurrent caller
478+
* will see SB_DYING and either discard the superblock or wait
479+
* for SB_DEAD.
480+
*/
481+
super_wake(s, SB_DEAD);
482+
464483
put_filesystem(fs);
465484
put_super(s);
466485
} else {
@@ -517,6 +536,45 @@ static int grab_super(struct super_block *s) __releases(sb_lock)
517536
return 0;
518537
}
519538

539+
static inline bool wait_dead(struct super_block *sb)
540+
{
541+
unsigned int flags;
542+
543+
/*
544+
* Pairs with memory barrier in super_wake() and ensures
545+
* that we see SB_DEAD after we're woken.
546+
*/
547+
flags = smp_load_acquire(&sb->s_flags);
548+
return flags & SB_DEAD;
549+
}
550+
551+
/**
552+
* grab_super_dead - acquire an active reference to a superblock
553+
* @sb: superblock to acquire
554+
*
555+
* Acquire a temporary reference on a superblock and try to trade it for
556+
* an active reference. This is used in sget{_fc}() to wait for a
557+
* superblock to either become SB_BORN or for it to pass through
558+
* sb->kill() and be marked as SB_DEAD.
559+
*
560+
* Return: This returns true if an active reference could be acquired,
561+
* false if not.
562+
*/
563+
static bool grab_super_dead(struct super_block *sb)
564+
{
565+
566+
sb->s_count++;
567+
if (grab_super(sb)) {
568+
put_super(sb);
569+
lockdep_assert_held(&sb->s_umount);
570+
return true;
571+
}
572+
wait_var_event(&sb->s_flags, wait_dead(sb));
573+
put_super(sb);
574+
lockdep_assert_not_held(&sb->s_umount);
575+
return false;
576+
}
577+
520578
/*
521579
* super_trylock_shared - try to grab ->s_umount shared
522580
* @sb: reference we are trying to grab
@@ -643,15 +701,14 @@ void generic_shutdown_super(struct super_block *sb)
643701
spin_unlock(&sb->s_inode_list_lock);
644702
}
645703
}
646-
spin_lock(&sb_lock);
647-
/* should be initialized for __put_super_and_need_restart() */
648-
hlist_del_init(&sb->s_instances);
649-
spin_unlock(&sb_lock);
650704
/*
651705
* Broadcast to everyone that grabbed a temporary reference to this
652706
* superblock before we removed it from @fs_supers that the superblock
653707
* is dying. Every walker of @fs_supers outside of sget{_fc}() will now
654708
* discard this superblock and treat it as dead.
709+
*
710+
* We leave the superblock on @fs_supers so it can be found by
711+
* sget{_fc}() until we passed sb->kill_sb().
655712
*/
656713
super_wake(sb, SB_DYING);
657714
super_unlock_excl(sb);
@@ -746,7 +803,7 @@ struct super_block *sget_fc(struct fs_context *fc,
746803
destroy_unused_super(s);
747804
return ERR_PTR(-EBUSY);
748805
}
749-
if (!grab_super(old))
806+
if (!grab_super_dead(old))
750807
goto retry;
751808
destroy_unused_super(s);
752809
return old;
@@ -790,7 +847,7 @@ struct super_block *sget(struct file_system_type *type,
790847
destroy_unused_super(s);
791848
return ERR_PTR(-EBUSY);
792849
}
793-
if (!grab_super(old))
850+
if (!grab_super_dead(old))
794851
goto retry;
795852
destroy_unused_super(s);
796853
return old;

include/linux/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ extern int send_sigurg(struct fown_struct *fown);
10951095
#define SB_LAZYTIME BIT(25) /* Update the on-disk [acm]times lazily */
10961096

10971097
/* These sb flags are internal to the kernel */
1098+
#define SB_DEAD BIT(21)
10981099
#define SB_DYING BIT(24)
10991100
#define SB_SUBMOUNT BIT(26)
11001101
#define SB_FORCE BIT(27)

0 commit comments

Comments
 (0)