fs,block: yield devices
Subsystems (4)
Christian Brauner
maintainercover fs,block: yield devices
Hey,
This is a mechanism that allows the holder of a block device to yield device access before actually closing the block device.
If a someone yields a device then any concurrent opener claiming the device exclusively with the same blk_holder_ops as the current owner can wait for the device to be given up. Filesystems by default use fs_holder_ps and so can wait on each other.
This mechanism allows us to simplify superblock handling quite a bit at the expense of requiring filesystems to yield devices. A filesytems must yield devices under s_umount. This allows costly work to be done outside of s_umount.
There's nothing wrong with the way we currently do things but this does allow us to simplify things and kills a whole class of theoretical UAF when walking the superblock list.
I had originally considered doing it this way but wasn't able (time-wise) to code that up but since we recently had that discussion again here it is.
Survives both xfstests and blktests. Also tested this by introducing custom delays into kill_block_super() to widen the race where a superblock is removed from the instance list and the device is fully closed and synced.
Based on on vfs.super and the freezer work sent out earlier. Very barebones commit messages and less analyzed then usually for possible side-effects.
Thanks!
Christian
---
Christian Brauner (6):
fs: simplify setup_bdev_super() calls
xfs: simplify device handling
ext4: simplify device handling
bdev: simplify waiting for concurrent claimers
block: mark device as about to be released
fs: add ->yield_devices()
block/bdev.c | 54 +++++++++++++++++++++++++++----------- fs/ext4/super.c | 15 ++++++++--- fs/super.c | 67 ++++++++++++++--------------------------------- fs/xfs/xfs_super.c | 46 +++++++++++++++++++++----------- include/linux/blk_types.h | 8 +++++- include/linux/blkdev.h | 1 + include/linux/fs.h | 1 + 7 files changed, 109 insertions(+), 83 deletions(-)
---
base-commit: c6cc4b13e95115c13433136a17150768d562a54c
change-id: 20231024-vfs-super-rework-ca447a3240c9
Christian Brauner
maintainerRFC PATCH 1/6 fs: simplify setup_bdev_super() calls
There's no need to drop s_umount anymore now that we removed all sources where s_umount is taken beneath open_mutex or bd_holder_lock.
--- fs/super.c | 16 ---------------- 1 file changed, 16 deletions(-)
fs/super.c +0 -16
@@ -1613,15 +1613,7 @@ int get_tree_bdev(struct fs_context *fc,return -EBUSY ; } }else { -/* -* We drop s_umount here because we need to open the bdev and -* bdev->open_mutex ranks above s_umount (blkdev_put() -> -* bdev_mark_dead()). It is safe because we have active sb -* reference and SB_BORN is not set yet. -*/ -super_unlock_excl (s); error =setup_bdev_super (s, fc->sb_flags, fc); -__super_lock_excl (s);if (!error) error =fill_super (s, fc);if (error) {
@@ -1665,15 +1657,7 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,return ERR_PTR (-EBUSY ); } }else { -/* -* We drop s_umount here because we need to open the bdev and -* bdev->open_mutex ranks above s_umount (blkdev_put() -> -* bdev_mark_dead()). It is safe because we have active sb -* reference and SB_BORN is not set yet. -*/ -super_unlock_excl (s); error =setup_bdev_super (s, flags,NULL ); -__super_lock_excl (s);if (!error) error =fill_super (s, data, flags &SB_SILENT ?1 :0 );if (error) {
Signature · 1 line
2.34.1
Re: [PATCH RFC 1/6] fs: simplify setup_bdev_super() calls
Quoted 4 lines · Christian Brauner
There's no need to drop
s_umountanymore now that we removed all sources wheres_umountis taken beneathopen_mutexorbd_holder_lock.Signed-off-by Christian Brauner <[email protected]>
Yay. Feel free to add:
Honza
Quoted 44 lines
--- fs/super.c | 16 ---------------- 1 file changed, 16 deletions(-)fs/super.c +0
-16@@ -1613,15 +1613,7 @@ int get_tree_bdev(struct fs_context *fc,return -EBUSY ; } }else { -/* -* We drop s_umount here because we need to open the bdev and -* bdev->open_mutex ranks above s_umount (blkdev_put() -> -* bdev_mark_dead()). It is safe because we have active sb -* reference and SB_BORN is not set yet. -*/ -super_unlock_excl (s); error =setup_bdev_super (s, fc->sb_flags, fc); -__super_lock_excl (s);if (!error) error =fill_super (s, fc);if (error) {@@ -1665,15 +1657,7 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,return ERR_PTR (-EBUSY ); } }else { -/* -* We drop s_umount here because we need to open the bdev and -* bdev->open_mutex ranks above s_umount (blkdev_put() -> -* bdev_mark_dead()). It is safe because we have active sb -* reference and SB_BORN is not set yet. -*/ -super_unlock_excl (s); error =setup_bdev_super (s, flags,NULL ); -__super_lock_excl (s);if (!error) error =fill_super (s, data, flags &SB_SILENT ?1 :0 );if (error) {Signature · 2 lines
-- 2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC 1/6] fs: simplify setup_bdev_super() calls
In reply to Christian BraunerSo while I still think we should not be holding s_umount when setting up a new unborn sb to start with, there is no point in dropping it now:
Christian Brauner
maintainerRFC PATCH 2/6 xfs: simplify device handling
In reply to Christian BraunerWe removed all codepaths where s_umount is taken beneath open_mutex and bd_holder_lock so don't make things more complicated than they need to be and hold s_umount over block device opening.
--- fs/xfs/xfs_super.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)
fs/xfs/xfs_super.c +3 -16
@@ -437,19 +437,13 @@ xfs_open_devices(struct bdev_handle *logdev_handle =NULL , *rtdev_handle =NULL ;int error; -/* -* blkdev_put() can't be called under s_umount, see the comment -* in get_tree_bdev() for more details -*/ -up_write (&sb->s_umount); -/* * Open real time and log devices - order is important. */ if (mp->m_logname) { error =xfs_blkdev_get (mp, mp->m_logname, &logdev_handle);if (error) - goto out_relock; +return error; }if (mp->m_rtname) {
@@ -492,10 +486,7 @@ xfs_open_devices(bdev_release (logdev_handle); } - error =0 ; -out_relock: -down_write (&sb->s_umount); -return error; +return 0 ; out_free_rtdev_targ:if (mp->m_rtdev_targp)
@@ -508,7 +499,7 @@ xfs_open_devices( out_close_logdev:if (logdev_handle)bdev_release (logdev_handle); - goto out_relock; +return error; }/*
@@ -758,10 +749,6 @@ static voidxfs_mount_free ( struct xfs_mount *mp) { -/* -* Free the buftargs here because blkdev_put needs to be called outside -* of sb->s_umount, which is held around the call to ->put_super. -*/ if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)xfs_free_buftarg (mp->m_logdev_targp);if (mp->m_rtdev_targp)
Signature · 1 line
2.34.1
Re: [PATCH RFC 2/6] xfs: simplify device handling
Quoted 5 lines · Christian Brauner
We removed all codepaths where
s_umountis taken beneathopen_mutexandbd_holder_lockso don't make things more complicated than they need to be and holds_umountover block device opening.Signed-off-by Christian Brauner <[email protected]>
Looks good. Feel free to add:
Honza
Quoted 65 lines
--- fs/xfs/xfs_super.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-)fs/xfs/xfs_super.c +3
-16@@ -437,19 +437,13 @@ xfs_open_devices(struct bdev_handle *logdev_handle =NULL , *rtdev_handle =NULL ;int error; -/* -* blkdev_put() can't be called under s_umount, see the comment -* in get_tree_bdev() for more details -*/ -up_write (&sb->s_umount); -/* * Open real time and log devices - order is important. */ if (mp->m_logname) { error =xfs_blkdev_get (mp, mp->m_logname, &logdev_handle);if (error) - goto out_relock; +return error; }if (mp->m_rtname) {@@ -492,10 +486,7 @@ xfs_open_devices(bdev_release (logdev_handle); } - error =0 ; -out_relock: -down_write (&sb->s_umount); -return error; +return 0 ; out_free_rtdev_targ:if (mp->m_rtdev_targp)@@ -508,7 +499,7 @@ xfs_open_devices( out_close_logdev:if (logdev_handle)bdev_release (logdev_handle); - goto out_relock; +return error; }/* @@ -758,10 +749,6 @@ static voidxfs_mount_free ( struct xfs_mount *mp) { -/* -* Free the buftargs here because blkdev_put needs to be called outside -* of sb->s_umount, which is held around the call to ->put_super. -*/ if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp)xfs_free_buftarg (mp->m_logdev_targp);if (mp->m_rtdev_targp)Signature · 2 lines
-- 2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC 2/6] xfs: simplify device handling
In reply to Christian BraunerLooks good:
Christian Brauner
maintainerRFC PATCH 3/6 ext4: simplify device handling
In reply to Christian BraunerWe removed all codepaths where s_umount is taken beneath open_mutex and bd_holder_lock so don't make things more complicated than they need to be and hold s_umount over block device opening.
--- fs/ext4/super.c | 3 --- 1 file changed, 3 deletions(-)
fs/ext4/super.c +0 -3
@@ -5855,11 +5855,8 @@ static struct bdev_handle *ext4_get_journal_blkdev(struct super_block *sb,struct ext4_super_block *es;int errno; -/* see get_tree_bdev why this is needed and safe */ -up_write (&sb->s_umount); bdev_handle =bdev_open_by_dev (j_dev,BLK_OPEN_READ |BLK_OPEN_WRITE , sb, &fs_holder_ops); -down_write (&sb->s_umount);if (IS_ERR (bdev_handle)) { ext4_msg(sb, KERN_ERR, "failed to open journal device unknown-block(%u,%u) %ld",
Signature · 1 line
2.34.1
Re: [PATCH RFC 3/6] ext4: simplify device handling
Quoted 5 lines · Christian Brauner
We removed all codepaths where
s_umountis taken beneathopen_mutexandbd_holder_lockso don't make things more complicated than they need to be and holds_umountover block device opening.Signed-off-by Christian Brauner <[email protected]>
Nice. Feel free to add:
Honza
Quoted 24 lines
--- fs/ext4/super.c | 3 --- 1 file changed, 3 deletions(-)fs/ext4/super.c +0
-3@@ -5855,11 +5855,8 @@ static struct bdev_handle *ext4_get_journal_blkdev(struct super_block *sb,struct ext4_super_block *es;int errno; -/* see get_tree_bdev why this is needed and safe */ -up_write (&sb->s_umount); bdev_handle =bdev_open_by_dev (j_dev,BLK_OPEN_READ |BLK_OPEN_WRITE , sb, &fs_holder_ops); -down_write (&sb->s_umount);if (IS_ERR (bdev_handle)) { ext4_msg(sb, KERN_ERR, "failed to open journal device unknown-block(%u,%u) %ld",Signature · 2 lines
-- 2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC 3/6] ext4: simplify device handling
In reply to Christian BraunerLooks good:
Christian Brauner
maintainerRFC PATCH 4/6 bdev: simplify waiting for concurrent claimers
In reply to Christian BraunerSimplify the mechanism to wait for concurrent block devices claimers and make it possible to introduce an additional state in the following patches.
--- block/bdev.c | 34 ++++++++++++++++++---------------- include/linux/blk_types.h | 7 ++++++- 2 files changed, 24 insertions(+), 17 deletions(-)
block/bdev.c +18 -16
@@ -482,6 +482,14 @@ static bool bd_may_claim(struct block_device *bdev, void *holder,return true; } +static bool wait_claimable (const struct block_device *bdev) +{ +enum bd_claim bd_claim; + + bd_claim =smp_load_acquire (&bdev->bd_claim); +return bd_claim ==BD_CLAIM_DEFAULT ; +} +/** * bd_prepare_to_claim - claim a block device * @bdev: block device of interest
@@ -490,7 +498,7 @@ static bool bd_may_claim(struct block_device *bdev, void *holder,* * Claim @bdev. This function fails if @bdev is already claimed by another * holder and waits if another claiming is in progress. return, the caller -* has ownership of bd_claiming and bd_holder[s]. +* has ownership of bd_claim and bd_holder[s]. * * RETURNS: * 0 if @bdev can be claimed, -EBUSY otherwise.
@@ -511,31 +519,25 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder, }/* if claiming is already in progress, wait for it to finish */ -if (whole->bd_claiming) { -wait_queue_head_t *wq =bit_waitqueue (&whole->bd_claiming,0 ); -DEFINE_WAIT (wait); - -prepare_to_wait (wq, &wait,TASK_UNINTERRUPTIBLE ); +if (whole->bd_claim) {mutex_unlock (&bdev_lock); -schedule (); -finish_wait (wq, &wait); +wait_var_event (&whole->bd_claim,wait_claimable (whole)); goto retry; }/* yay, all mine */ - whole->bd_claiming = holder; + whole->bd_claim =BD_CLAIM_ACQUIRE ;mutex_unlock (&bdev_lock);return 0 ; }EXPORT_SYMBOL_GPL (bd_prepare_to_claim);/* only for the loop driver */ -static void bd_clear_claiming (struct block_device *whole,void *holder) +static void bd_clear_claiming (struct block_device *whole) {lockdep_assert_held (&bdev_lock); -/* tell others that we're done */ -BUG_ON (whole->bd_claiming != holder); - whole->bd_claiming =NULL ; -wake_up_bit (&whole->bd_claiming,0 ); +smp_store_release (&whole->bd_claim,BD_CLAIM_DEFAULT ); +smp_mb (); +wake_up_var (&whole->bd_claim); }/**
@@ -565,7 +567,7 @@ static void bd_finish_claiming(struct block_device *bdev, void *holder, bdev->bd_holder = holder; bdev->bd_holder_ops = hops;mutex_unlock (&bdev->bd_holder_lock); -bd_clear_claiming (whole, holder); +bd_clear_claiming (whole);mutex_unlock (&bdev_lock); }
@@ -581,7 +583,7 @@ static void bd_finish_claiming(struct block_device *bdev, void *holder,void bd_abort_claiming (struct block_device *bdev,void *holder) {mutex_lock (&bdev_lock); -bd_clear_claiming (bdev_whole (bdev), holder); +bd_clear_claiming (bdev_whole (bdev));mutex_unlock (&bdev_lock); }EXPORT_SYMBOL (bd_abort_claiming);
include/linux/blk_types.h +6 -1
@@ -37,6 +37,11 @@ struct bio_crypt_ctx;#define PAGE_SECTORS (1 << PAGE_SECTORS_SHIFT)#define SECTOR_MASK (PAGE_SECTORS - 1) +enum bd_claim { +BD_CLAIM_DEFAULT =0 , +BD_CLAIM_ACQUIRE =1 , +}; +struct block_device {sector_t bd_start_sect;sector_t bd_nr_sectors;
@@ -52,7 +57,7 @@ struct block_device {atomic_t bd_openers;spinlock_t bd_size_lock;/* for bd_inode->i_size updates */ struct inode * bd_inode;/* will die */ -void * bd_claiming; +enum bd_claim bd_claim;void * bd_holder;const struct blk_holder_ops *bd_holder_ops;struct mutex bd_holder_lock;
Signature · 1 line
2.34.1
Jan Kara
reviewerRe: [PATCH RFC 4/6] bdev: simplify waiting for concurrent claimers
Quoted 5 lines · Christian Brauner
Simplify the mechanism to wait for concurrent block devices claimers and make it possible to introduce an additional state in the following patches.
Signed-off-by Christian Brauner <[email protected]>
The simplification looks good but a few notes below:
Quoted 5 lines
block/bdev.c +0
-0
block/bdev.c static bool bd_may_claim(struct block_device *bdev, void *holder, · line 491
Quoted 10 lines
block/bdev.c +7
-0@@ -482,6 +482,14 @@ static bool bd_may_claim(struct block_device *bdev, void *holder,return true; } +static bool wait_claimable (const struct block_device *bdev) +{ +enum bd_claim bd_claim; + + bd_claim =smp_load_acquire (&bdev->bd_claim); +return bd_claim ==BD_CLAIM_DEFAULT ; +}
Aren't you overdoing it here a bit? Given this is used only in a retry loop and all the checks that need to be reliable are done under bdev_lock, I'd say having:
return READ_ONCE(bdev->bd_claim) == BD_CLAIM_DEFAULT;
shound be fine here? And probably just inline that into the wait_var_event() call...
block/bdev.c int bd_prepare_to_claim(struct block_device *bdev, void *holder, · line 522
Quoted 10 lines
block/bdev.c +1
-5@@ -511,31 +519,25 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder, }/* if claiming is already in progress, wait for it to finish */ -if (whole->bd_claiming) { -wait_queue_head_t *wq =bit_waitqueue (&whole->bd_claiming,0 ); -DEFINE_WAIT (wait); - -prepare_to_wait (wq, &wait,TASK_UNINTERRUPTIBLE ); +if (whole->bd_claim) {
This test implicitely assumes that 0 is BD_CLAIM_DEFAULT. I guess that's fine although I somewhat prefer explicit value test like:
if (whole->bd_claim != BD_CLAIM_DEFAULT)
block/bdev.c int bd_prepare_to_claim(struct block_device *bdev, void *holder, · line 529
Quoted 10 lines
block/bdev.c +2
-3mutex_unlock (&bdev_lock); -schedule (); -finish_wait (wq, &wait); +wait_var_event (&whole->bd_claim,wait_claimable (whole)); goto retry; }/* yay, all mine */ - whole->bd_claiming = holder; + whole->bd_claim =BD_CLAIM_ACQUIRE ;
Here I'd use WRITE_ONCE() to avoid KCSAN warnings and having to think whether this can race with wait_claimable() or not.
Quoted 6 lines
block/bdev.c +0
-1mutex_unlock (&bdev_lock);return 0 ; }EXPORT_SYMBOL_GPL (bd_prepare_to_claim);/* only for the loop driver */ -static void bd_clear_claiming (struct block_device *whole,void *holder)
block/bdev.c int bd_prepare_to_claim(struct block_device *bdev, void *holder, · line 540
Quoted 10 lines
block/bdev.c +4
-4+static void bd_clear_claiming (struct block_device *whole) {lockdep_assert_held (&bdev_lock); -/* tell others that we're done */ -BUG_ON (whole->bd_claiming != holder); - whole->bd_claiming =NULL ; -wake_up_bit (&whole->bd_claiming,0 ); +smp_store_release (&whole->bd_claim,BD_CLAIM_DEFAULT ); +smp_mb (); +wake_up_var (&whole->bd_claim);
And here since we are under bdev_lock and the waiter is going to check under bdev_lock as well, we should be able to do:
WRITE_ONCE(whole->bd_claim, BD_CLAIM_DEFAULT); /* Pairs with barrier in prepare_to_wait_event() -> set_current_state() */ smp_mb(); wake_up_var(&whole->bd_claim); Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH RFC 4/6] bdev: simplify waiting for concurrent claimers
Quoted 4 lines · Jan Kara
This test implicitely assumes that 0 is
BD_CLAIM_DEFAULT. I guess that's fine although I somewhat prefer explicit value test like:if (whole->bd_claim != BD_CLAIM_DEFAULT)
I find the BD_CLAIM_DEFAULT confusing to be honest. I'd expect null to just be check as:
if (whole->bd_claim)
That being said, instead of doing all the manual atomic magic, why not add an
unsigned long bd_state;
to struct block_device instead of bd_claim, then define a single bit for a device being clamed and simply everything while also giving us space for more bits if we ever need them?
Christian Brauner
maintainerRFC PATCH 5/6 block: mark device as about to be released
In reply to Christian BraunerMake it possible for the exclusive holder of a block device to mark it as about to be closed and exclusive ownership given up. Any concurrent opener trying to claim the device with the same holder ops can wait until the device is free to be reclaimed. Requiring the same holder ops makes it possible to easily define groups of openers that can wait for each other.
--- block/bdev.c | 20 ++++++++++++++++++++ include/linux/blk_types.h | 1 + include/linux/blkdev.h | 1 + 3 files changed, 22 insertions(+)
block/bdev.c +20 -0
@@ -469,6 +469,11 @@ static bool bd_may_claim(struct block_device *bdev, void *holder,return false;return true; } + +if ((whole->bd_claim ==BD_CLAIM_YIELD ) && + (bdev->bd_holder_ops == hops)) +return true; +return false; }
@@ -608,6 +613,7 @@ static void bd_end_claim(struct block_device *bdev, void *holder)mutex_unlock (&bdev->bd_holder_lock);if (bdev->bd_write_holder) unblock = true; +bd_clear_claiming (whole); }if (!whole->bd_holders) whole->bd_holder =NULL ;
@@ -954,6 +960,20 @@ void bdev_release(struct bdev_handle *handle) }EXPORT_SYMBOL (bdev_release); +void bdev_yield (struct bdev_handle *handle) +{ +struct block_device *bdev = handle->bdev; +struct block_device *whole =bdev_whole (bdev); + +mutex_lock (&bdev_lock); +WARN_ON_ONCE (bdev->bd_holders ==0 ); +WARN_ON_ONCE (bdev->bd_holder != handle->holder); +WARN_ON_ONCE (whole->bd_claim); + whole->bd_claim =BD_CLAIM_YIELD ; +mutex_unlock (&bdev_lock); +} +EXPORT_SYMBOL (bdev_yield); +/** * lookup_bdev() - Look up a struct block_device by name. * @pathname: Name of the block device in the filesystem.
include/linux/blk_types.h +1 -0
@@ -40,6 +40,7 @@ struct bio_crypt_ctx;enum bd_claim {BD_CLAIM_DEFAULT =0 ,BD_CLAIM_ACQUIRE =1 , +BD_CLAIM_YIELD =2 , };struct block_device {
include/linux/blkdev.h +1 -0
@@ -1513,6 +1513,7 @@ int bd_prepare_to_claim(struct block_device *bdev, void *holder,void bd_abort_claiming (struct block_device *bdev,void *holder);void blkdev_put (struct block_device *bdev,void *holder);void bdev_release (struct bdev_handle *handle); +void bdev_yield (struct bdev_handle *handle);/* just for blk-cgroup, don't use elsewhere */ struct block_device *blkdev_get_no_open (dev_t dev);
Signature · 1 line
2.34.1
Christian Brauner
maintainerRFC PATCH 6/6 fs: add ->yield_devices()
In reply to Christian Braunerand allow filesystems to mark devices as about to be released allowing concurrent openers to wait until the device is reclaimable.
--- fs/ext4/super.c | 12 ++++++++++++ fs/super.c | 51 ++++++++++++++++++++------------------------------- fs/xfs/xfs_super.c | 27 +++++++++++++++++++++++++++ include/linux/fs.h | 1 + 4 files changed, 60 insertions(+), 31 deletions(-)
fs/ext4/super.c +12 -0
@@ -1477,6 +1477,17 @@ static void ext4_shutdown(struct super_block *sb)ext4_force_shutdown (sb,EXT4_GOING_FLAGS_NOLOGFLUSH ); } +static void ext4_yield_devices (struct super_block *sb) +{ +struct ext4_sb_info *sbi = sb->s_fs_info; +struct bdev_handle *journal_bdev_handle = + sbi ? sbi->s_journal_bdev_handle :NULL ; + +if (journal_bdev_handle) +bdev_yield (journal_bdev_handle); +bdev_yield (sb->s_bdev_handle); +} +static void init_once (void *foo) {struct ext4_inode_info *ei = foo;
@@ -1638,6 +1649,7 @@ static const struct super_operations ext4_sops = { .statfs = ext4_statfs, .show_options = ext4_show_options, .shutdown = ext4_shutdown, + .yield_devices = ext4_yield_devices, #ifdef CONFIG_QUOTA .quota_read = ext4_quota_read, .quota_write = ext4_quota_write,
fs/super.c +20 -31
133 lines not on this page · raw message
fs/xfs/xfs_super.c +27 -0
@@ -1170,6 +1170,32 @@ xfs_fs_shutdown(xfs_force_shutdown (XFS_M (sb),SHUTDOWN_DEVICE_REMOVED ); } +static void xfs_fs_bdev_yield (struct bdev_handle *handle, +struct super_block *sb) +{ +if (handle != sb->s_bdev_handle) +bdev_yield (handle); +} + +static void +xfs_fs_yield_devices ( +struct super_block *sb) +{ +struct xfs_mount *mp =XFS_M (sb); + +if (mp) { +if (mp->m_logdev_targp && + mp->m_logdev_targp != mp->m_ddev_targp) +xfs_fs_bdev_yield (mp->m_logdev_targp->bt_bdev_handle, sb); +if (mp->m_rtdev_targp) +xfs_fs_bdev_yield (mp->m_rtdev_targp->bt_bdev_handle, sb); +if (mp->m_ddev_targp) +xfs_fs_bdev_yield (mp->m_ddev_targp->bt_bdev_handle, sb); + } + +bdev_yield (sb->s_bdev_handle); +} +static const struct super_operations xfs_super_operations = { .alloc_inode = xfs_fs_alloc_inode, .destroy_inode = xfs_fs_destroy_inode,
@@ -1184,6 +1210,7 @@ static const struct super_operations xfs_super_operations = { .nr_cached_objects = xfs_fs_nr_cached_objects, .free_cached_objects = xfs_fs_free_cached_objects, .shutdown = xfs_fs_shutdown, + .yield_devices = xfs_fs_yield_devices, }; static int
include/linux/fs.h +1 -0
@@ -2026,6 +2026,7 @@ struct super_operations {long (*free_cached_objects)(struct super_block *,struct shrink_control *);void (*shutdown)(struct super_block *sb); +void (*yield_devices)(struct super_block *sb); };/*
Signature · 1 line
2.34.1
Jan Kara
reviewerHello!
Quoted 8 lines · Christian Brauner
This is a mechanism that allows the holder of a block device to yield device access before actually closing the block device.
If a someone yields a device then any concurrent opener claiming the device exclusively with the same
blk_holder_opsas the current owner can wait for the device to be given up. Filesystems by default usefs_holder_psand so can wait on each other.
Quoted 8 lines
This mechanism allows us to simplify superblock handling quite a bit at the expense of requiring filesystems to yield devices. A filesytems must yield devices under
s_umount. This allows costly work to be done outside ofs_umount.There's nothing wrong with the way we currently do things but this does allow us to simplify things and kills a whole class of theoretical UAF when walking the superblock list.
I'm not sure why is it better to create new ->yield callback called under sb->s_umount rather than just move blkdev_put() calls back into ->put_super? Or at least yielding could be done in ->put_super instead of a new callback if for some reason that is better than releasing the devices right away in ->put_super.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 16 lines · Jan Kara
Hello!
Quoted 13 lines · Christian Brauner
This is a mechanism that allows the holder of a block device to yield device access before actually closing the block device.
If a someone yields a device then any concurrent opener claiming the device exclusively with the same
blk_holder_opsas the current owner can wait for the device to be given up. Filesystems by default usefs_holder_psand so can wait on each other.This mechanism allows us to simplify superblock handling quite a bit at the expense of requiring filesystems to yield devices. A filesytems must yield devices under
s_umount. This allows costly work to be done outside ofs_umount.
Quoted 7 lines
Quoted 3 lines
There's nothing wrong with the way we currently do things but this does allow us to simplify things and kills a whole class of theoretical UAF when walking the superblock list.
I'm not sure why is it better to create new ->yield callback called under
sb->s_umountrather than just moveblkdev_put()calls back into ->put_super? Or at least yielding could be done in ->put_super instead of
The main reason was to not call potentially expensive blkdev_put()/bdev_release() under s_umount. If we don't care about this though then this shouldn't be a problem. And yes, then we need to move blkdev_put()/bdev_release() under s_umount including the main block device. IOW, we need to ensure that all bdev calls are done under s_umount before we remove the superblock from the instance list. I think that should be fine but I wanted to propose an alternative to that as
well: cheap mark-for-release under s_umount and heavy-duty without s_umount. But I guess it doesn't matter because most filesystems did use to close devices under s_umount before anyway. Let me know what you think makes the most sense.
Jan Kara
reviewerQuoted 21 lines · Christian Brauner
Quoted 20 lines · Jan Kara
Hello!
Quoted 16 lines · Christian Brauner
This is a mechanism that allows the holder of a block device to yield device access before actually closing the block device.
If a someone yields a device then any concurrent opener claiming the device exclusively with the same
blk_holder_opsas the current owner can wait for the device to be given up. Filesystems by default usefs_holder_psand so can wait on each other.This mechanism allows us to simplify superblock handling quite a bit at the expense of requiring filesystems to yield devices. A filesytems must yield devices under
s_umount. This allows costly work to be done outside ofs_umount.There's nothing wrong with the way we currently do things but this does allow us to simplify things and kills a whole class of theoretical UAF when walking the superblock list.
Quoted 7 lines
Quoted 3 lines
I'm not sure why is it better to create new ->yield callback called under
sb->s_umountrather than just moveblkdev_put()calls back into ->put_super? Or at least yielding could be done in ->put_super instead ofThe main reason was to not call potentially expensive blkdev_put()/bdev_release() under
s_umount. If we don't care about this though then this shouldn't be a problem.
So I would not be really concerned about performance here. The superblock is dying, nobody can do anything about that until the superblock is fully dead and cleaned up. Maybe some places could skip such superblocks more quickly but so far I'm not convinced it matters in practice (e.g. writeback holds s_umount over the whole sync(1) time and nobody complains). And as you mention below, we have been doing this for a long time without anybody really complaining.
Quoted 4 lines
And yes, then we need to move blkdev_put()/bdev_release() under
s_umountincluding the main block device. IOW, we need to ensure that all bdev calls are done unders_umountbefore we remove the superblock from the instance list.
This is about those seemingly spurious "device busy" errors when the superblock hasn't closed its devices yet, isn't it? But we now remove superblock from s_instances list in kill_super_notify() and until that moment SB_DYING is protecting us from racing mounts. So is there some other problem?
Quoted 6 lines
I think
that should be fine but I wanted to propose an alternative to that as
well: cheap mark-for-release unders_umountand heavy-duty withouts_umount. But I guess it doesn't matter because most filesystems did use to close devices unders_umountbefore anyway. Let me know what you think makes the most sense.
I think we should make it as simple as possible for filesystems. As I said above I don't think s_umount hold time really matters so the only thing that limits us are locking constraints. During superblock shutdown the only thing that currently cannot be done under s_umount (that I'm aware of) is the teardown of the sb->s_bdi because that waits for writeback threads and those can be blocked waiting for s_umount.
So if we wanted we could just move ext4 & xfs bdev closing back into ->put_super to avoid ext4_kill_sb() and somewhat slim down xfs_mount_free() but otherwise I don't see much space for simplification or need for adding more callbacks :)
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 30 lines · Jan Kara
Quoted 21 lines · Christian Brauner
Quoted 20 lines · Jan Kara
Hello!
Quoted 16 lines · Christian Brauner
This is a mechanism that allows the holder of a block device to yield device access before actually closing the block device.
If a someone yields a device then any concurrent opener claiming the device exclusively with the same
blk_holder_opsas the current owner can wait for the device to be given up. Filesystems by default usefs_holder_psand so can wait on each other.This mechanism allows us to simplify superblock handling quite a bit at the expense of requiring filesystems to yield devices. A filesytems must yield devices under
s_umount. This allows costly work to be done outside ofs_umount.There's nothing wrong with the way we currently do things but this does allow us to simplify things and kills a whole class of theoretical UAF when walking the superblock list.
Quoted 7 lines
Quoted 3 lines
I'm not sure why is it better to create new ->yield callback called under
sb->s_umountrather than just moveblkdev_put()calls back into ->put_super? Or at least yielding could be done in ->put_super instead ofThe main reason was to not call potentially expensive blkdev_put()/bdev_release() under
s_umount. If we don't care about this though then this shouldn't be a problem.
Quoted 7 lines
So I would not be really concerned about performance here. The superblock is dying, nobody can do anything about that until the superblock is fully dead and cleaned up. Maybe some places could skip such superblocks more quickly but so far I'm not convinced it matters in practice (e.g. writeback holds
s_umountover the whole sync(1) time and nobody complains). And as you mention below, we have been doing this for a long time without anybody really complaining.
Ok, that's a good point.
Quoted 8 lines
Quoted 5 lines
And yes, then we need to move blkdev_put()/bdev_release() under
s_umountincluding the main block device. IOW, we need to ensure that all bdev calls are done unders_umountbefore we remove the superblock from the instance list.This is about those seemingly spurious "device busy" errors when the superblock hasn't closed its devices yet, isn't it? But we now remove
Yes, because we tie superblock and block device neatly together.
Quoted 3 lines
superblock from
s_instanceslist inkill_super_notify()and until that momentSB_DYINGis protecting us from racing mounts. So is there some other problem?
No, there isn't a problem at all. It's all working fine but it was initially a little annoying as we had to update filesystems to ensure that sb->s_fs_info is kept alive. But it's all fixed.
The possible advantage is that if we drop all block devices under s_umount then we can remove the superblock from fs_type->s_instances in the old location again. I'm not convinced it's worth it but it's a possible simplification. I'm not even arguing it's objectively better I think it's a matter of taste in the end.
Quoted 9 lines
Quoted 7 lines
I think
that should be fine but I wanted to propose an alternative to that as
well: cheap mark-for-release unders_umountand heavy-duty withouts_umount. But I guess it doesn't matter because most filesystems did use to close devices unders_umountbefore anyway. Let me know what you think makes the most sense.I think we should make it as simple as possible for filesystems. As I said
Yes, I fully agree.
The really good thing about the current mechanism is that it's completely vfs-only. With s_umount/open_mutex ordering fixed filesystems can now close block devices wherever and the vfs will ensure that you don't get spurious ebusy. And the filesystem doesn't have to know a thing about it or take any care.
Quoted 5 lines
above I don't think
s_umounthold time really matters so the only thing that limits us are locking constraints. During superblock shutdown the only thing that currently cannot be done unders_umount(that I'm aware of) is the teardown of thesb->s_bdibecause that waits for writeback threads and those can be blocked waiting fors_umount.
Which we don't need to change if it's working fine.
Quoted 5 lines
So if we wanted we could just move ext4 & xfs bdev closing back into ->put_super to avoid
ext4_kill_sb()and somewhat slim downxfs_mount_free()but otherwise I don't see much space for simplification or need for adding more callbacks :)
I mean, that I would only think is useful if we really wanted to close all block devices under s_umount to possible be remove the waiting mechanism we have right now. Otherwise I think it's churn for the sake of churn and I quite like what we have right now.
Jan Kara
reviewerQuoted 14 lines · Christian Brauner
Quoted 2 lines · Jan Kara
This is about those seemingly spurious "device busy" errors when the superblock hasn't closed its devices yet, isn't it? But we now remove
Yes, because we tie superblock and block device neatly together.
Quoted 3 lines
superblock from
s_instanceslist inkill_super_notify()and until that momentSB_DYINGis protecting us from racing mounts. So is there some other problem?No, there isn't a problem at all. It's all working fine but it was initially a little annoying as we had to update filesystems to ensure that
sb->s_fs_infois kept alive. But it's all fixed.
Quoted 5 lines
The possible advantage is that if we drop all block devices under
s_umountthen we can remove the superblock fromfs_type->s_instancesin the old location again. I'm not convinced it's worth it but it's a possible simplification. I'm not even arguing it's objectively better I think it's a matter of taste in the end.
Yes. But dropping the main bdev under s_umount is not easy to do with the current callback structure. We have kill_block_super() calling into generic_shutdown_super(). Now logically generic_shutdown_super() wants to teardown bdi (for which it needs to drop s_umount) but bdev is closed only in kill_block_super() because that's the sb-on-bdev specific call. Previously we got away with this without spurious EBUSY errors because the bdev holder was the filesystem type and not the superblock. But after changing the holder it isn't fine anymore and we have to play these games with s_instances and SB_DYING.
What we could probably do is to have something like:
if (sb->s_bdev) {
blkdev_put(sb->s_bdev);
sb->s_bdev = NULL;
}
if (sb->s_mtd) {
put_mtd_device(sb->s_mtd);
sb->s_mtd = NULL;
}in generic_shutdown_super() and then remove sb from s_instances, drop s_umount and cleanup bdi.
Then we can get rid of SB_DYING, kill_super_notify() and stuff but based on your taste it could be also viewed as kind of layering violation so I'm not 100% convinced this is definitely a way to go.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Christian Brauner
maintainerQuoted 2 lines
your taste it could be also viewed as kind of layering violation so I'm not 100% convinced this is definitely a way to go.
Yeah, I'm not convinced either. As I said, I really like that right now ti's a vfs thing only and we don't have specific requirements about how devices are closed which is really nice. So let's just leave it as is?
Jan Kara
reviewerQuoted 6 lines · Christian Brauner
Quoted 2 lines
your taste it could be also viewed as kind of layering violation so I'm not 100% convinced this is definitely a way to go.
Yeah, I'm not convinced either. As I said, I really like that right now ti's a vfs thing only and we don't have specific requirements about how devices are closed which is really nice. So let's just leave it as is?
Yes, fine by me.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 6 lines · Christian Brauner
Quoted 2 lines
your taste it could be also viewed as kind of layering violation so I'm not 100% convinced this is definitely a way to go.
Yeah, I'm not convinced either. As I said, I really like that right now ti's a vfs thing only and we don't have specific requirements about how devices are closed which is really nice. So let's just leave it as is?
Yes, I can't really get excited about this change in any way.
Re: (subset) [PATCH RFC 0/6] fs,block: yield devices
In reply to Christian BraunerQuoted 11 lines · Christian Brauner
Hey,
This is a mechanism that allows the holder of a block device to yield device access before actually closing the block device.
If a someone yields a device then any concurrent opener claiming the device exclusively with the same
blk_holder_opsas the current owner can wait for the device to be given up. Filesystems by default usefs_holder_psand so can wait on each other.[...]
for v6.8
---
Applied to the vfs.super branch of the vfs/vfs.git tree. Patches in the vfs.super branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.super
[1/6] fs: simplify setup_bdev_super() calls
https://git.kernel.org/vfs/vfs/c/f15112134111 [2/6] xfs: simplify device handling https://git.kernel.org/vfs/vfs/c/cbded4a095bd [3/6] ext4: simplify device handling https://git.kernel.org/vfs/vfs/c/ac85cf49ed93