[PATCH v3] bcachefs: init freespace inited bits to 0 in bch2_fs_initialize

Piotr Zalewski posted 1 patch 1 month ago
fs/bcachefs/recovery.c | 8 ++++++++
1 file changed, 8 insertions(+)
[PATCH v3] bcachefs: init freespace inited bits to 0 in bch2_fs_initialize
Posted by Piotr Zalewski 1 month ago
Initialize freespace_initialized bits to 0 in member's flags and update
member's cached version for each device in bch2_fs_initialize.

It's possible for the bits to be set to 1 before fs is initialized and if
call to bch2_trans_mark_dev_sbs (just before bch2_fs_freespace_init) fails
bits remain to be 1 which can later indirectly trigger BUG condition in
bch2_bucket_alloc_freelist during shutdown.

Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
Suggested-by: Kent Overstreet <kent.overstreet@linux.dev>
Signed-off-by: Piotr Zalewski <pZ010001011111@proton.me>
---

Notes:
    changes in v3:
        - v2 rebased

    changes in v2:
        - unconditionally set freespace initialized bits to false at
          the top of bch2_fs_initialized instead of only if
          bch2_trans_mark_dev_sbs fails

    Link to v2: https://lore.kernel.org/linux-bcachefs/20241021174151.37692-2-pZ010001011111@proton.me/
    Link to v1: https://lore.kernel.org/linux-bcachefs/20241020170708.67044-2-pZ010001011111@proton.me/

 fs/bcachefs/recovery.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
index fdf2aa2ffc13..8d4c93a1f5aa 100644
--- a/fs/bcachefs/recovery.c
+++ b/fs/bcachefs/recovery.c
@@ -1029,6 +1029,7 @@ int bch2_fs_initialize(struct bch_fs *c)
 	struct bch_inode_unpacked root_inode, lostfound_inode;
 	struct bkey_inode_buf packed_inode;
 	struct qstr lostfound = QSTR("lost+found");
+	struct bch_member *m;
 	int ret;
 
 	bch_notice(c, "initializing new filesystem");
@@ -1045,6 +1046,13 @@ int bch2_fs_initialize(struct bch_fs *c)
 		SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
 		bch2_write_super(c);
 	}
+
+	for_each_member_device(c, ca) {
+		m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
+		SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, false);
+		ca->mi = bch2_mi_to_cpu(m);
+	}
+
 	mutex_unlock(&c->sb_lock);
 
 	c->curr_recovery_pass = BCH_RECOVERY_PASS_NR;
-- 
2.47.0
Re: [PATCH v3] bcachefs: init freespace inited bits to 0 in bch2_fs_initialize
Posted by Kent Overstreet 1 month ago
On Sat, Oct 26, 2024 at 12:15:49AM +0000, Piotr Zalewski wrote:
> Initialize freespace_initialized bits to 0 in member's flags and update
> member's cached version for each device in bch2_fs_initialize.
> 
> It's possible for the bits to be set to 1 before fs is initialized and if
> call to bch2_trans_mark_dev_sbs (just before bch2_fs_freespace_init) fails
> bits remain to be 1 which can later indirectly trigger BUG condition in
> bch2_bucket_alloc_freelist during shutdown.
> 
> Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
> Suggested-by: Kent Overstreet <kent.overstreet@linux.dev>
> Signed-off-by: Piotr Zalewski <pZ010001011111@proton.me>
> ---
> 
> Notes:
>     changes in v3:
>         - v2 rebased
> 
>     changes in v2:
>         - unconditionally set freespace initialized bits to false at
>           the top of bch2_fs_initialized instead of only if
>           bch2_trans_mark_dev_sbs fails
> 
>     Link to v2: https://lore.kernel.org/linux-bcachefs/20241021174151.37692-2-pZ010001011111@proton.me/
>     Link to v1: https://lore.kernel.org/linux-bcachefs/20241020170708.67044-2-pZ010001011111@proton.me/
> 
>  fs/bcachefs/recovery.c | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
> index fdf2aa2ffc13..8d4c93a1f5aa 100644
> --- a/fs/bcachefs/recovery.c
> +++ b/fs/bcachefs/recovery.c
> @@ -1029,6 +1029,7 @@ int bch2_fs_initialize(struct bch_fs *c)
>  	struct bch_inode_unpacked root_inode, lostfound_inode;
>  	struct bkey_inode_buf packed_inode;
>  	struct qstr lostfound = QSTR("lost+found");
> +	struct bch_member *m;
>  	int ret;
>  
>  	bch_notice(c, "initializing new filesystem");
> @@ -1045,6 +1046,13 @@ int bch2_fs_initialize(struct bch_fs *c)
>  		SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
>  		bch2_write_super(c);
>  	}
> +
> +	for_each_member_device(c, ca) {
> +		m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
> +		SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, false);
> +		ca->mi = bch2_mi_to_cpu(m);
> +	}
> +

I'm also adding a write_super() call here: that's what updates
ca->mi.freespace_initialized which the freespace init path will later
check.

>  	mutex_unlock(&c->sb_lock);
>  
>  	c->curr_recovery_pass = BCH_RECOVERY_PASS_NR;
> -- 
> 2.47.0
> 
>
Re: [PATCH v3] bcachefs: init freespace inited bits to 0 in bch2_fs_initialize
Posted by Piotr Zalewski 4 weeks, 1 day ago
On Saturday, October 26th, 2024 at 2:30 AM, Kent Overstreet <kent.overstreet@linux.dev> wrote:

> On Sat, Oct 26, 2024 at 12:15:49AM +0000, Piotr Zalewski wrote:
> 
> > Initialize freespace_initialized bits to 0 in member's flags and update
> > member's cached version for each device in bch2_fs_initialize.
> > 
> > It's possible for the bits to be set to 1 before fs is initialized and if
> > call to bch2_trans_mark_dev_sbs (just before bch2_fs_freespace_init) fails
> > bits remain to be 1 which can later indirectly trigger BUG condition in
> > bch2_bucket_alloc_freelist during shutdown.
> > 
> > Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
> > Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> > Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
> > Suggested-by: Kent Overstreet kent.overstreet@linux.dev
> > Signed-off-by: Piotr Zalewski pZ010001011111@proton.me
> > ---
> > 
> > Notes:
> > changes in v3:
> > - v2 rebased
> > 
> > changes in v2:
> > - unconditionally set freespace initialized bits to false at
> > the top of bch2_fs_initialized instead of only if
> > bch2_trans_mark_dev_sbs fails
> > 
> > Link to v2: https://lore.kernel.org/linux-bcachefs/20241021174151.37692-2-pZ010001011111@proton.me/
> > Link to v1: https://lore.kernel.org/linux-bcachefs/20241020170708.67044-2-pZ010001011111@proton.me/
> > 
> > fs/bcachefs/recovery.c | 8 ++++++++
> > 1 file changed, 8 insertions(+)
> > 
> > diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
> > index fdf2aa2ffc13..8d4c93a1f5aa 100644
> > --- a/fs/bcachefs/recovery.c
> > +++ b/fs/bcachefs/recovery.c
> > @@ -1029,6 +1029,7 @@ int bch2_fs_initialize(struct bch_fs *c)
> > struct bch_inode_unpacked root_inode, lostfound_inode;
> > struct bkey_inode_buf packed_inode;
> > struct qstr lostfound = QSTR("lost+found");
> > + struct bch_member *m;
> > int ret;
> > 
> > bch_notice(c, "initializing new filesystem");
> > @@ -1045,6 +1046,13 @@ int bch2_fs_initialize(struct bch_fs *c)
> > SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
> > bch2_write_super(c);
> > }
> > +
> > + for_each_member_device(c, ca) {
> > + m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
> > + SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, false);
> > + ca->mi = bch2_mi_to_cpu(m);
> > + }
> > +
> 
> 
> I'm also adding a write_super() call here: that's what updates
> ca->mi.freespace_initialized which the freespace init path will later
> 
> check.

Do you want me to send v4? Or you will add it with separate patch?

Best regards, Piotr Zalewski

> > mutex_unlock(&c->sb_lock);
> > 
> > c->curr_recovery_pass = BCH_RECOVERY_PASS_NR;
> > --
> > 2.47.0
Re: [PATCH v3] bcachefs: init freespace inited bits to 0 in bch2_fs_initialize
Posted by Kent Overstreet 4 weeks, 1 day ago
On Sat, Oct 26, 2024 at 01:40:47PM +0000, Piotr Zalewski wrote:
> On Saturday, October 26th, 2024 at 2:30 AM, Kent Overstreet <kent.overstreet@linux.dev> wrote:
> 
> > On Sat, Oct 26, 2024 at 12:15:49AM +0000, Piotr Zalewski wrote:
> > 
> > > Initialize freespace_initialized bits to 0 in member's flags and update
> > > member's cached version for each device in bch2_fs_initialize.
> > > 
> > > It's possible for the bits to be set to 1 before fs is initialized and if
> > > call to bch2_trans_mark_dev_sbs (just before bch2_fs_freespace_init) fails
> > > bits remain to be 1 which can later indirectly trigger BUG condition in
> > > bch2_bucket_alloc_freelist during shutdown.
> > > 
> > > Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
> > > Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> > > Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
> > > Suggested-by: Kent Overstreet kent.overstreet@linux.dev
> > > Signed-off-by: Piotr Zalewski pZ010001011111@proton.me
> > > ---
> > > 
> > > Notes:
> > > changes in v3:
> > > - v2 rebased
> > > 
> > > changes in v2:
> > > - unconditionally set freespace initialized bits to false at
> > > the top of bch2_fs_initialized instead of only if
> > > bch2_trans_mark_dev_sbs fails
> > > 
> > > Link to v2: https://lore.kernel.org/linux-bcachefs/20241021174151.37692-2-pZ010001011111@proton.me/
> > > Link to v1: https://lore.kernel.org/linux-bcachefs/20241020170708.67044-2-pZ010001011111@proton.me/
> > > 
> > > fs/bcachefs/recovery.c | 8 ++++++++
> > > 1 file changed, 8 insertions(+)
> > > 
> > > diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
> > > index fdf2aa2ffc13..8d4c93a1f5aa 100644
> > > --- a/fs/bcachefs/recovery.c
> > > +++ b/fs/bcachefs/recovery.c
> > > @@ -1029,6 +1029,7 @@ int bch2_fs_initialize(struct bch_fs *c)
> > > struct bch_inode_unpacked root_inode, lostfound_inode;
> > > struct bkey_inode_buf packed_inode;
> > > struct qstr lostfound = QSTR("lost+found");
> > > + struct bch_member *m;
> > > int ret;
> > > 
> > > bch_notice(c, "initializing new filesystem");
> > > @@ -1045,6 +1046,13 @@ int bch2_fs_initialize(struct bch_fs *c)
> > > SET_BCH_SB_VERSION_UPGRADE_COMPLETE(c->disk_sb.sb, bcachefs_metadata_version_current);
> > > bch2_write_super(c);
> > > }
> > > +
> > > + for_each_member_device(c, ca) {
> > > + m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
> > > + SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, false);
> > > + ca->mi = bch2_mi_to_cpu(m);
> > > + }
> > > +
> > 
> > 
> > I'm also adding a write_super() call here: that's what updates
> > ca->mi.freespace_initialized which the freespace init path will later
> > 
> > check.
> 
> Do you want me to send v4? Or you will add it with separate patch?

I did it as a fixup to your patch
Re: [PATCH v3] bcachefs: init freespace inited bits to 0 in bch2_fs_initialize
Posted by Kent Overstreet 1 month ago
On Sat, Oct 26, 2024 at 12:15:49AM +0000, Piotr Zalewski wrote:
> Initialize freespace_initialized bits to 0 in member's flags and update
> member's cached version for each device in bch2_fs_initialize.
> 
> It's possible for the bits to be set to 1 before fs is initialized and if
> call to bch2_trans_mark_dev_sbs (just before bch2_fs_freespace_init) fails
> bits remain to be 1 which can later indirectly trigger BUG condition in
> bch2_bucket_alloc_freelist during shutdown.
> 
> Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
> Suggested-by: Kent Overstreet <kent.overstreet@linux.dev>
> Signed-off-by: Piotr Zalewski <pZ010001011111@proton.me>

Thanks, applied