Skip to contentSkip to conversation

super: convert to a plain hashtable

PATCH In review Christian Brauner 2 participants 4 messages Oct 12

Subsystems (4)
lore
Share & tools
lore

Christian Brauner

author maintainer
#0 lore raw

cover super: convert to a plain hashtable

This sits on top of the superblock fixes for v7.3.

Convert the rhltable to a plain hashtable under a spinlock. The walks keep their place with a cursor entry in the chain. A claim is unhashed the moment it's dropped and a walk that finds a superblock that isn't born yet waits for it to be born or to die and looks again. Since hashing can't fail anymore the entry for the superblock's own s_dev is embedded in struct super_block and the preallocation in alloc_super() goes away. This all simplifies things quite a lot.

It also fixes a btrfs bug where fs_bdev_freeze() ended up freezing a btrfs filesystem through a stale member that open_ctree() had already released while the freeze was waiting for SB_BORN. The resume doesn't find a holder for that device anymore so the filesystem stays frozen for good.

The selftest freezes a device that several erofs superblocks share. That's the one case where a device legitimately carries claims from more than one superblock and it checks that a suspend freezes each of them exactly once and that mounts coming and going under a suspended device leave nothing frozen behind.

I modeled the table in TLA+ and checked the real list code with CBMC. Both agree with the design.

---
Christian Brauner (2):
selftests/filesystems: add a test for freezing a shared block device
super: convert to plain hashtable

 block/bdev.c                                       |   2 +-
 fs/internal.h                                      |   1 -
 fs/namespace.c                                     |   2 -
 fs/super.c                                         | 332 ++++++--------
 include/linux/fs/super_types.h                     |  11 +-
 .../selftests/filesystems/freeze/.gitignore        |   1 +
 .../testing/selftests/filesystems/freeze/Makefile  |   2 +-
 tools/testing/selftests/filesystems/freeze/config  |   1 +
 .../filesystems/freeze/shared_bdev_test.c          | 479 +++++++++++++++++++++
 9 files changed, 627 insertions(+), 204 deletions(-)

---
base-commit: 16efce55f70b6093dbe53bffb8e79fb0e08a95b6
change-id: 20261011-work-super-dev_hash-e30f79361408

author maintainer
#1 lore raw

PATCH 1/2 selftests/filesystems: add a test for freezing a shared block device

erofs claims its blob devices with the filesystem type as the holder so several erofs superblocks can share one. Merge two images from a common layer and a layer of their own, put the common layer on device-mapper, mount both and check that:

- a suspend freezes each of them exactly once

- the resume thaws both

- once one of them is unmounted only the other one is frozen

- BLKFLSBUF and ustat() on the shared device work

- mounts that come and go while the device is suspended and resumed leave nothing frozen behind

mkfs.erofs builds the images, the test is skipped without it.

---
 .../selftests/filesystems/freeze/.gitignore        |   1 +
 .../testing/selftests/filesystems/freeze/Makefile  |   2 +-
 tools/testing/selftests/filesystems/freeze/config  |   1 +
 .../filesystems/freeze/shared_bdev_test.c          | 479 +++++++++++++++++++++
 4 files changed, 482 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/freeze/.gitignore +1 -0
index 28f1fe73f19b..2f60f71c3d03 100644
@@ -2,3 +2,4 @@
 quotactl_test
 bdev_freeze_test
 bdev_thaw_test
+shared_bdev_test
tools/testing/selftests/filesystems/freeze/Makefile +1 -1
index acd246e1b14b..a2b956286b15 100644
@@ -3,6 +3,6 @@ CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
 LOCAL_HDRS += loop_util.h
 
 TEST_GEN_PROGS := quotactl_test
-TEST_GEN_PROGS += bdev_freeze_test bdev_thaw_test
+TEST_GEN_PROGS += bdev_freeze_test bdev_thaw_test shared_bdev_test
 
 include ../../lib.mk
tools/testing/selftests/filesystems/freeze/config +1 -0
index d03720cda9a9..28e568e5d670 100644
@@ -1,6 +1,7 @@
 CONFIG_BLK_DEV_DM=y
 CONFIG_BLK_DEV_LOOP=y
 CONFIG_DEBUG_FS=y
+CONFIG_EROFS_FS=y
 CONFIG_EXT4_FS=y
 CONFIG_FAIL_MAKE_REQUEST=y
 CONFIG_FAULT_INJECTION=y
tools/testing/selftests/filesystems/freeze/shared_bdev_test.c new file +479 -0

479 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/2 super: convert to plain hashtable

In reply to Christian Brauner

Convert the rhltable to a plain hashtable under a spinlock. The walks keep their place with a cursor entry in the chain. A claim is unhashed the moment it's dropped and a walk that finds a superblock that isn't born yet waits for it to be born or to die and looks again. Since hashing can't fail anymore the entry for the superblock's own s_dev is embedded in struct super_block and the preallocation in alloc_super() goes away. This all simplifies things quite a lot.

It also fixes a btrfs bug where fs_bdev_freeze() ended up freezing a btrfs filesystem through a stale member that open_ctree() had already released while the freeze was waiting for SB_BORN. The resume doesn't find a holder for that device anymore so the filesystem stays frozen for good.

The walks return superblocks instead of table entries now and user_get_super() hands out the passive reference the walk took.

Reproducer, a three device btrfs with a stale copy of a removed member. dm-delay under the remaining members stretches the window between btrfs_open_devices() and btrfs_free_extra_devids():

  truncate -s 1G a1 a2 b
  A1=$(losetup -f --show a1); A2=$(losetup -f --show a2); B=$(losetup -f --show b)
  mkfs.btrfs -f -d single -m single $A1 $A2 $B
  cp b b.old
  mount $A1 /mnt && btrfs device remove 3 /mnt && umount /mnt
  OLD=$(losetup -f --show b.old)
  dmsetup create d1 --table "0 $(blockdev --getsz $A1) delay $A1 0 200"
  dmsetup create d2 --table "0 $(blockdev --getsz $A2) delay $A2 0 200"
  dmsetup create old --table "0 $(blockdev --getsz $OLD) linear $OLD 0"
  mount -o device=/dev/mapper/d2,device=/dev/mapper/old /dev/mapper/d1 /mnt &
  sleep 1; dmsetup suspend old; wait
  dmsetup resume old
  touch /mnt/foo     # hangs until fsfreeze -u /mnt

That left btrfs frozen in 5 of 5 rounds with the same tables issued through raw dm ioctls. The -o degraded variant needs the mount held between the insertion and the bd_fsfreeze_count check in fs_bdev_register() with a kprobe.

Fixes cdb5146f8d5f ("fs: look up superblocks via the device table in fs_holder_ops")
---
 block/bdev.c                   |   2 +-
 fs/internal.h                  |   1 -
 fs/namespace.c                 |   2 -
 fs/super.c                     | 332 +++++++++++++++++------------------------
 include/linux/fs/super_types.h |  11 +-
 5 files changed, 145 insertions(+), 203 deletions(-)
block/bdev.c +1 -1
index cd8323083740..5e996f86d2ff 100644
@@ -1283,7 +1283,7 @@ int lookup_bdev(const char *pathname, dev_t *dev)
 	 * registers major 0.  Fake block device inodes (e.g. fuse with
 	 * rootmode=S_IFBLK) can expose i_rdev == 0, and letting that
 	 * propagate would confuse superblock lookup and trigger warnings
-	 * in the device-to-superblock table (super_dev_register).
+	 * in the device-to-superblock table (sget_fc).
 	 */
 	error = -ENODEV;
 	if (!inode->i_rdev)
fs/internal.h +0 -1
index c658c8a5ebd5..67aa0444351b 100644
@@ -137,7 +137,6 @@ extern int reconfigure_super(struct fs_context *);
 extern bool super_trylock_shared(struct super_block *sb);
 struct super_block *user_get_super(dev_t, bool excl);
 void put_super(struct super_block *sb);
-void __init super_dev_init(void);
 extern bool mount_capable(struct fs_context *);
 
 /*
fs/namespace.c +0 -2
index 3ed3766f3a18..4c3816256ae2 100644
@@ -6470,8 +6470,6 @@ void __init mnt_init(void)
 				HASH_ZERO,
 				&mp_hash_shift, &mp_hash_mask, 0, 0);
 
-	super_dev_init();
-
 	kernfs_init();
 
 	err = sysfs_init();
fs/super.c +135 -197

516 lines not on this page · raw message

include/linux/fs/super_types.h +9 -2
index ecd96aeb1cee..72f4288e16c5 100644
@@ -30,7 +30,6 @@ struct mount;
 struct mtd_info;
 struct quotactl_ops;
 struct shrinker;
-struct super_dev;
 struct unicode_map;
 struct user_namespace;
 struct workqueue_struct;
@@ -132,10 +131,18 @@ struct super_operations {
 	void (*report_error)(const struct fserror_event *event);
 };
 
+/* A superblock's claim on a device, see fs/super.c. */
+struct super_dev {
+	struct hlist_node			sd_hash;
+	struct super_block			*sd_sb;
+	dev_t					sd_dev;
+	unsigned int				sd_claims;
+};
+
 struct super_block {
 	struct list_head			s_list;		/* Keep this first */
 	dev_t					s_dev;		/* search index; _not_ kdev_t */
-	struct super_dev			*s_super_dev;	/* sget_fc()'s device table claim */
+	struct super_dev			s_super_dev;	/* claim on s_dev */
 	unsigned char				s_blocksize_bits;
 	unsigned long				s_blocksize;
 	loff_t					s_maxbytes;	/* Max file size */
Signature · 1 line
2.53.0
reviewer
#3 lore raw

Re: [PATCH 2/2] super: convert to plain hashtable

Quoted 35 lines · Christian Brauner

Convert the rhltable to a plain hashtable under a spinlock. The walks keep their place with a cursor entry in the chain. A claim is unhashed the moment it's dropped and a walk that finds a superblock that isn't born yet waits for it to be born or to die and looks again. Since hashing can't fail anymore the entry for the superblock's own s_dev is embedded in struct super_block and the preallocation in alloc_super() goes away. This all simplifies things quite a lot.

It also fixes a btrfs bug where fs_bdev_freeze() ended up freezing a btrfs filesystem through a stale member that open_ctree() had already released while the freeze was waiting for SB_BORN. The resume doesn't find a holder for that device anymore so the filesystem stays frozen for good.

The walks return superblocks instead of table entries now and user_get_super() hands out the passive reference the walk took.

Reproducer, a three device btrfs with a stale copy of a removed member. dm-delay under the remaining members stretches the window between btrfs_open_devices() and btrfs_free_extra_devids():

  truncate -s 1G a1 a2 b
  A1=$(losetup -f --show a1); A2=$(losetup -f --show a2); B=$(losetup -f --show b)
  mkfs.btrfs -f -d single -m single $A1 $A2 $B
  cp b b.old
  mount $A1 /mnt && btrfs device remove 3 /mnt && umount /mnt
  OLD=$(losetup -f --show b.old)
  dmsetup create d1 --table "0 $(blockdev --getsz $A1) delay $A1 0 200"
  dmsetup create d2 --table "0 $(blockdev --getsz $A2) delay $A2 0 200"
  dmsetup create old --table "0 $(blockdev --getsz $OLD) linear $OLD 0"
  mount -o device=/dev/mapper/d2,device=/dev/mapper/old /dev/mapper/d1 /mnt &
  sleep 1; dmsetup suspend old; wait
  dmsetup resume old
  touch /mnt/foo     # hangs until fsfreeze -u /mnt
Quoted 7 lines

That left btrfs frozen in 5 of 5 rounds with the same tables issued through raw dm ioctls. The -o degraded variant needs the mount held between the insertion and the bd_fsfreeze_count check in fs_bdev_register() with a kprobe.

Fixes cdb5146f8d5f ("fs: look up superblocks via the device table in fs_holder_ops")

Overall looks good to me and indeed the code looks simpler. One suggestion below:

Quoted 5 lines
fs/super.c +2 -2
+/* Unhash the cursor of a walk that stops early. */
+static void super_dev_walk_end(struct super_dev *cursor)
 {
-	struct super_dev *sb_dev;
-

fs/super.c void put_super(struct super_block *s) · line 516

Quoted 10 lines
fs/super.c +4 -4
-	rcu_read_lock();
-	sb_dev = super_dev_get(rhltable_lookup(&super_dev_table, &dev, super_dev_params));
-	rcu_read_unlock();
-	return sb_dev;
+	guard(spinlock)(&super_dev_lock);
+	hash_del(&cursor->sd_hash);
 }
 
+#define for_each_dev_super(sb, cursor) \
+	for (sb = super_dev_next(cursor, NULL); sb; sb = super_dev_next(cursor, sb))

So the 'cursor' is unused by the users of for_each_dev_super() except for calling super_dev_walk_end() when they abort the loop. So why not pass just 'dev' into the macro where 'cursor' will be internal to for() loop and setup __cleanup to call super_dev_walk_end() when the cursor goes out of scope? It will require a bit of a macro magic but should be more ergonomic to use and no chance to forget calling super_dev_walk_end() at appropriate places.

Honza

Quoted 362 lines

static void kill_super_notify(struct super_block *sb) {

fs/super.c +67 -104
@@ -548,11 +523,8 @@ static void kill_super_notify(struct super_block *sb)
 	if (sb->s_flags & SB_DEAD)
 		return;
 
-	/* Drop sget_fc()'s claim; a never-registered entry stays with the sb. */
-	if (sb->s_super_dev->sd_dev) {
-		super_dev_put(sb->s_super_dev);
-		sb->s_super_dev = NULL;
-	}
+	/* Drop sget_fc()'s claim on s_dev. */
+	super_dev_put(&sb->s_super_dev);
 
 	/*
 	 * Let concurrent mounts know that this thing is really dead.
@@ -880,13 +852,12 @@ struct super_block *sget_fc(struct fs_context *fc,
 	s->s_fs_info = fc->s_fs_info;
 	err = set(s, fc);
 	if (err) {
-		VFS_WARN_ON_ONCE(s->s_super_dev->sd_dev);
 		s->s_fs_info = NULL;
 		spin_unlock(&sb_lock);
 		destroy_unused_super(s);
 		return ERR_PTR(err);
 	}
-	VFS_WARN_ON_ONCE(!s->s_super_dev->sd_dev);
+	VFS_WARN_ON_ONCE(!s->s_dev);
 	fc->s_fs_info = NULL;
 	s->s_type = fc->fs_type;
 	s->s_iflags |= fc->s_iflags;
@@ -899,6 +870,8 @@ struct super_block *sget_fc(struct fs_context *fc,
 	list_add_tail(&s->s_list, &super_blocks);
 	hlist_add_head(&s->s_instances, &s->s_type->fs_supers);
 	spin_unlock(&sb_lock);
+	scoped_guard(spinlock, &super_dev_lock)
+		super_dev_add(&s->s_super_dev, s, s->s_dev);
 	get_filesystem(s->s_type);
 	shrinker_register(s->s_shrink);
 	return s;
@@ -1040,18 +1013,14 @@ EXPORT_SYMBOL(iterate_supers_type);
 
 struct super_block *user_get_super(dev_t dev, bool excl)
 {
-	struct super_dev *sb_dev;
-
-	for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
-		struct super_block *sb = sb_dev->sd_sb;
-
-		if (!super_lock(sb, excl))
-			continue;
+	struct super_dev cursor = { .sd_dev = dev };
+	struct super_block *sb;
 
-		/* The pinned entry holds a passive reference, take our own. */
-		refcount_inc(&sb->s_passive);
-		super_dev_put(sb_dev);
-		return sb;
+	for_each_dev_super(sb, &cursor) {
+		if (super_lock(sb, excl)) {
+			super_dev_walk_end(&cursor);
+			return sb;
+		}
 	}
 	return NULL;
 }
@@ -1353,16 +1322,7 @@ EXPORT_SYMBOL(free_anon_bdev);
 
 int set_anon_super(struct super_block *s, void *data)
 {
-	int error;
-
-	error = get_anon_bdev(&s->s_dev);
-	if (error)
-		return error;
-
-	error = super_dev_register(s);
-	if (error)
-		free_anon_bdev(s->s_dev);
-	return error;
+	return get_anon_bdev(&s->s_dev);
 }
 EXPORT_SYMBOL(set_anon_super);
 
@@ -1448,7 +1408,7 @@ EXPORT_SYMBOL(get_tree_keyed);
 static int set_bdev_super(struct super_block *s, void *data)
 {
 	s->s_dev = *(dev_t *)data;
-	return super_dev_register(s);
+	return 0;
 }
 
 static int super_s_dev_set(struct super_block *s, struct fs_context *fc)
@@ -1527,14 +1487,12 @@ static int fs_super_thaw(struct super_block *sb)
 
 static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
 {
-	struct super_dev *sb_dev;
-	dev_t dev = bdev->bd_dev;
+	struct super_dev cursor = { .sd_dev = bdev->bd_dev };
+	struct super_block *sb;
 
 	mutex_unlock(&bdev->bd_holder_lock);
 
-	for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
-		struct super_block *sb = sb_dev->sd_sb;
-
+	for_each_dev_super(sb, &cursor) {
 		if (!super_lock_shared(sb))
 			continue;
 		if (sb->s_root && (sb->s_flags & SB_ACTIVE)) {
@@ -1554,14 +1512,12 @@ static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise)
 
 static void fs_bdev_sync(struct block_device *bdev)
 {
-	struct super_dev *sb_dev;
-	dev_t dev = bdev->bd_dev;
+	struct super_dev cursor = { .sd_dev = bdev->bd_dev };
+	struct super_block *sb;
 
 	mutex_unlock(&bdev->bd_holder_lock);
 
-	for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
-		struct super_block *sb = sb_dev->sd_sb;
-
+	for_each_dev_super(sb, &cursor) {
 		if (!super_lock_shared(sb))
 			continue;
 		if (sb->s_root && (sb->s_flags & SB_ACTIVE))
@@ -1585,8 +1541,8 @@ static void fs_bdev_sync(struct block_device *bdev)
  */
 static int fs_bdev_freeze(struct block_device *bdev)
 {
-	dev_t dev = bdev->bd_dev;
-	struct super_dev *sb_dev;
+	struct super_dev cursor = { .sd_dev = bdev->bd_dev };
+	struct super_block *sb;
 	unsigned int count = 0;
 	int error = 0, err;
 
@@ -1594,13 +1550,13 @@ static int fs_bdev_freeze(struct block_device *bdev)
 
 	mutex_unlock(&bdev->bd_holder_lock);
 
-	for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
-		if (!get_active_super(sb_dev->sd_sb))
+	for_each_dev_super(sb, &cursor) {
+		if (!get_active_super(sb))
 			continue;
-		err = fs_super_freeze(sb_dev->sd_sb);
+		err = fs_super_freeze(sb);
 		if (err && !error)
 			error = err;
-		deactivate_super(sb_dev->sd_sb);
+		deactivate_super(sb);
 		count++;
 	}
 
@@ -1640,8 +1596,8 @@ static int fs_bdev_freeze(struct block_device *bdev)
  */
 static int fs_bdev_thaw(struct block_device *bdev)
 {
-	dev_t dev = bdev->bd_dev;
-	struct super_dev *sb_dev;
+	struct super_dev cursor = { .sd_dev = bdev->bd_dev };
+	struct super_block *sb;
 	unsigned int count = 0;
 	int error = 0, err;
 
@@ -1649,13 +1605,13 @@ static int fs_bdev_thaw(struct block_device *bdev)
 
 	mutex_unlock(&bdev->bd_holder_lock);
 
-	for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) {
-		if (!get_active_super(sb_dev->sd_sb))
+	for_each_dev_super(sb, &cursor) {
+		if (!get_active_super(sb))
 			continue;
-		err = fs_super_thaw(sb_dev->sd_sb);
+		err = fs_super_thaw(sb);
 		if (err && !error)
 			error = err;
-		deactivate_super(sb_dev->sd_sb);
+		deactivate_super(sb);
 		count++;
 	}
 
@@ -1672,55 +1628,39 @@ static const struct blk_holder_ops fs_holder_ops = {
 	.thaw			= fs_bdev_thaw,
 };
 
-static struct super_dev *super_dev_lookup(dev_t dev, struct super_block *sb)
+static struct super_dev *super_dev_find(dev_t dev, const struct super_block *sb)
 {
-	struct super_dev *it;
-	struct rhlist_head *list, *pos;
+	struct super_dev *sd;
 
-	RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious super_dev_lookup() usage");
-	VFS_WARN_ON_ONCE(!dev);
-	VFS_WARN_ON_ONCE(!sb);
+	lockdep_assert_held(&super_dev_lock);
 
-	list = rhltable_lookup(&super_dev_table, &dev, super_dev_params);
-	rhl_for_each_entry_rcu(it, pos, list, sd_node) {
-		if (it->sd_sb == sb)
-			return it;
+	hash_for_each_possible(super_dev_hash, sd, sd_hash, dev) {
+		if (sd->sd_dev == dev && sd->sd_sb == sb)
+			return sd;
 	}
-
 	return NULL;
 }
 
-static int fs_bdev_register(struct file *bdev_file, struct super_block *sb)
+static int fs_bdev_register(struct block_device *bdev, struct super_block *sb)
 {
-	struct super_dev *sb_dev __free(kfree) = NULL;
-	dev_t dev = file_bdev(bdev_file)->bd_dev;
-	int err;
-
-	scoped_guard(rcu) {
-		sb_dev = super_dev_lookup(dev, sb);
-		if (sb_dev && refcount_inc_not_zero(&sb_dev->sd_ref)) {
-			retain_and_null_ptr(sb_dev);
-			return 0;
-		}
-	}
-
-	sb_dev = super_dev_alloc(dev, sb);
-	if (!sb_dev)
-		return -ENOMEM;
+	struct super_dev *new __free(kfree) = NULL;
+	struct super_dev *sd;
 
-	err = super_dev_insert(sb_dev);
-	if (err)
-		return err;
+	new = kzalloc_obj(*new);
 
-	/* Publish the entry before reading the count; pairs with bdev_freeze(). */
-	smp_mb();
-	if (atomic_read(&file_bdev(bdev_file)->bd_fsfreeze_count) > 0) {
-		err = -EBUSY;
-		super_dev_put(sb_dev);
+	guard(spinlock)(&super_dev_lock);
+	sd = super_dev_find(bdev->bd_dev, sb);
+	if (sd) {
+		sd->sd_claims++;
+		return 0;
 	}
-
-	retain_and_null_ptr(sb_dev);
-	return err;
+	/* bdev_freeze() raises the count before its walk takes the lock. */
+	if (atomic_read(&bdev->bd_fsfreeze_count) > 0)
+		return -EBUSY;
+	if (!new)
+		return -ENOMEM;
+	super_dev_add(no_free_ptr(new), sb, bdev->bd_dev);
+	return 0;
 }
 
 /**
@@ -1747,7 +1687,7 @@ struct file *fs_bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder,
 	if (IS_ERR(bdev_file))
 		return bdev_file;
 
-	err = fs_bdev_register(bdev_file, sb);
+	err = fs_bdev_register(file_bdev(bdev_file), sb);
 	if (err) {
 		bdev_fput(bdev_file);
 		return ERR_PTR(err);
@@ -1781,7 +1721,7 @@ struct file *fs_bdev_file_open_by_path(const char *path, blk_mode_t mode,
 	if (IS_ERR(bdev_file))
 		return bdev_file;
 
-	err = fs_bdev_register(bdev_file, sb);
+	err = fs_bdev_register(file_bdev(bdev_file), sb);
 	if (err) {
 		bdev_fput(bdev_file);
 		return ERR_PTR(err);
@@ -1796,21 +1736,20 @@ EXPORT_SYMBOL_GPL(fs_bdev_file_open_by_path);
  * @sb: superblock the device was claimed for
  *
  * The inverse of fs_bdev_register(): drop one claim on the {dev, @sb} entry
- * (the last claim unregisters it; a pinning cursor defers the actual unlink)
- * without closing the device.  A caller that must act on the still-open device
- * between unregistering and closing - e.g. re-allow freezing one denied for a
- * membership change - pairs this with bdev_fput().  fs_bdev_file_release() is
- * the common unregister-and-close.
+ * (the last claim unregisters it) without closing the device.  A caller that
+ * must act on the still-open device between unregistering and closing - e.g.
+ * re-allow freezing one denied for a membership change - pairs this with
+ * bdev_fput().  fs_bdev_file_release() is the common unregister-and-close.
  */
 void fs_bdev_unregister(struct file *bdev_file, struct super_block *sb)
 {
-	dev_t dev = file_bdev(bdev_file)->bd_dev;
-	struct super_dev *sb_dev;
+	struct super_dev *sd;
 
-	rcu_read_lock();
-	sb_dev = super_dev_lookup(dev, sb);
-	rcu_read_unlock();
-	super_dev_put(sb_dev);
+	/* The claim we're dropping keeps the entry hashed. */
+	scoped_guard(spinlock, &super_dev_lock)
+		sd = super_dev_find(file_bdev(bdev_file)->bd_dev, sb);
+	if (sd)
+		super_dev_put(sd);
 }
 EXPORT_SYMBOL_GPL(fs_bdev_unregister);
 
@@ -1853,8 +1792,7 @@ int setup_bdev_super(struct super_block *sb, int sb_flags,
 		return -EACCES;
 	}
 
-	/* The sget_fc() entry is already published; pairs with bdev_freeze(). */
-	smp_mb();
+	/* sget_fc() hashed @sb, super_dev_lock orders that against bdev_freeze(). */
 	if (atomic_read(&bdev->bd_fsfreeze_count) > 0) {
 		if (fc)
 			warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev);
include/linux/fs/super_types.h +9 -2
index ecd96aeb1cee..72f4288e16c5 100644
@@ -30,7 +30,6 @@ struct mount;
 struct mtd_info;
 struct quotactl_ops;
 struct shrinker;
-struct super_dev;
 struct unicode_map;
 struct user_namespace;
 struct workqueue_struct;
@@ -132,10 +131,18 @@ struct super_operations {
 	void (*report_error)(const struct fserror_event *event);
 };
 
+/* A superblock's claim on a device, see fs/super.c. */
+struct super_dev {
+	struct hlist_node			sd_hash;
+	struct super_block			*sd_sb;
+	dev_t					sd_dev;
+	unsigned int				sd_claims;
+};
+
 struct super_block {
 	struct list_head			s_list;		/* Keep this first */
 	dev_t					s_dev;		/* search index; _not_ kdev_t */
-	struct super_dev			*s_super_dev;	/* sget_fc()'s device table claim */
+	struct super_dev			s_super_dev;	/* claim on s_dev */
 	unsigned char				s_blocksize_bits;
 	unsigned long				s_blocksize;
 	loff_t					s_maxbytes;	/* Max file size */
Signature · 2 lines
-- 
2.53.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR