public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] bcachefs: set freespace inited to false if trans_mark_dev_sbs fails
@ 2024-10-20 17:08 Piotr Zalewski
  2024-10-20 23:23 ` Kent Overstreet
  0 siblings, 1 reply; 3+ messages in thread
From: Piotr Zalewski @ 2024-10-20 17:08 UTC (permalink / raw)
  To: kent.overstreet, linux-bcachefs, linux-kernel
  Cc: skhan, Piotr Zalewski, syzbot+2b6a17991a6af64f9489

In bch2_fs_initialize if bch2_trans_mark_dev_sbs fails, set freespace
initialized bits to 0 in member's flags and update member cached version
for each device. bch2_trans_mark_dev_sbs fails just before freespace
init which can left freespace init bits set to true erroneously which later
can indirectly trigger BUG condition in bch2_bucket_alloc_freelist[1].

[1] https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489

Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
Signed-off-by: Piotr Zalewski <pZ010001011111@proton.me>
---
 fs/bcachefs/recovery.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
index 67bba156cce9..bcec79122f65 100644
--- a/fs/bcachefs/recovery.c
+++ b/fs/bcachefs/recovery.c
@@ -1031,6 +1031,7 @@ int bch2_fs_initialize(struct bch_fs *c)
 	struct bkey_inode_buf packed_inode;
 	struct qstr lostfound = QSTR("lost+found");
 	int ret;
+	struct bch_member *m;
 
 	bch_notice(c, "initializing new filesystem");
 	set_bit(BCH_FS_new_fs, &c->flags);
@@ -1086,8 +1087,17 @@ int bch2_fs_initialize(struct bch_fs *c)
 	bch_verbose(c, "marking superblocks");
 	ret = bch2_trans_mark_dev_sbs(c);
 	bch_err_msg(c, ret, "marking superblocks");
-	if (ret)
+	if (ret) {
+		mutex_lock(&c->sb_lock);
+		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);
+
 		goto err;
+	}
 
 	for_each_online_member(c, ca)
 		ca->new_fs_bucket_idx = 0;
-- 
2.47.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] bcachefs: set freespace inited to false if trans_mark_dev_sbs fails
  2024-10-20 17:08 [PATCH v1] bcachefs: set freespace inited to false if trans_mark_dev_sbs fails Piotr Zalewski
@ 2024-10-20 23:23 ` Kent Overstreet
  2024-10-21  8:05   ` Piotr Zalewski
  0 siblings, 1 reply; 3+ messages in thread
From: Kent Overstreet @ 2024-10-20 23:23 UTC (permalink / raw)
  To: Piotr Zalewski
  Cc: linux-bcachefs, linux-kernel, skhan, syzbot+2b6a17991a6af64f9489

On Sun, Oct 20, 2024 at 05:08:45PM +0000, Piotr Zalewski wrote:
> In bch2_fs_initialize if bch2_trans_mark_dev_sbs fails, set freespace
> initialized bits to 0 in member's flags and update member cached version
> for each device. bch2_trans_mark_dev_sbs fails just before freespace
> init which can left freespace init bits set to true erroneously which later
> can indirectly trigger BUG condition in bch2_bucket_alloc_freelist[1].

Err...

freespace_initialized shouldn't even be set at this point, it's set
later, in bch2_fs_freespace_init(), naturally.

So - syzbot is feeding us garbage, heh. BCH_MEMBER_FREESPACE_INITIALIZED
should be false on a non initialized filesystem.

So we actually should be unconditially clearing it (sanitizing our
input) at the top of bch2_fs_initialize().

> 
> [1] https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> 
> Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
> Signed-off-by: Piotr Zalewski <pZ010001011111@proton.me>
> ---
>  fs/bcachefs/recovery.c | 12 +++++++++++-
>  1 file changed, 11 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
> index 67bba156cce9..bcec79122f65 100644
> --- a/fs/bcachefs/recovery.c
> +++ b/fs/bcachefs/recovery.c
> @@ -1031,6 +1031,7 @@ int bch2_fs_initialize(struct bch_fs *c)
>  	struct bkey_inode_buf packed_inode;
>  	struct qstr lostfound = QSTR("lost+found");
>  	int ret;
> +	struct bch_member *m;
>  
>  	bch_notice(c, "initializing new filesystem");
>  	set_bit(BCH_FS_new_fs, &c->flags);
> @@ -1086,8 +1087,17 @@ int bch2_fs_initialize(struct bch_fs *c)
>  	bch_verbose(c, "marking superblocks");
>  	ret = bch2_trans_mark_dev_sbs(c);
>  	bch_err_msg(c, ret, "marking superblocks");
> -	if (ret)
> +	if (ret) {
> +		mutex_lock(&c->sb_lock);
> +		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);
> +
>  		goto err;
> +	}
>  
>  	for_each_online_member(c, ca)
>  		ca->new_fs_bucket_idx = 0;
> -- 
> 2.47.0
> 
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1] bcachefs: set freespace inited to false if trans_mark_dev_sbs fails
  2024-10-20 23:23 ` Kent Overstreet
@ 2024-10-21  8:05   ` Piotr Zalewski
  0 siblings, 0 replies; 3+ messages in thread
From: Piotr Zalewski @ 2024-10-21  8:05 UTC (permalink / raw)
  To: Kent Overstreet
  Cc: linux-bcachefs, linux-kernel, skhan, syzbot+2b6a17991a6af64f9489

Hi Kent,

On Monday, October 21st, 2024 at 1:23 AM, Kent Overstreet <kent.overstreet@linux.dev> wrote:

> On Sun, Oct 20, 2024 at 05:08:45PM +0000, Piotr Zalewski wrote:
> 
> > In bch2_fs_initialize if bch2_trans_mark_dev_sbs fails, set freespace
> > initialized bits to 0 in member's flags and update member cached version
> > for each device. bch2_trans_mark_dev_sbs fails just before freespace
> > init which can left freespace init bits set to true erroneously which later
> > can indirectly trigger BUG condition in bch2_bucket_alloc_freelist[1].
> 
> 
> Err...
> 
> freespace_initialized shouldn't even be set at this point, it's set
> later, in bch2_fs_freespace_init(), naturally.
> 
> So - syzbot is feeding us garbage, heh. BCH_MEMBER_FREESPACE_INITIALIZED
> should be false on a non initialized filesystem.
> 
> So we actually should be unconditially clearing it (sanitizing our
> input) at the top of bch2_fs_initialize().

Initially I added it at the beginning of bch2_fs_initialize. I changed it
to here since then the error triggered is ENOSPC_disk_reservation instead
of EIO. I thought that it's somewhat important to retain the error so moved
it to the if statement.

I will move it to the sb_lock at the top. After syzbot tests it I will send
v2.

> > [1] https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> > 
> > Reported-by: syzbot+2b6a17991a6af64f9489@syzkaller.appspotmail.com
> > Closes: https://syzkaller.appspot.com/bug?extid=2b6a17991a6af64f9489
> > Fixes: bbe682c76789 ("bcachefs: Ensure devices are always correctly initialized")
> > Signed-off-by: Piotr Zalewski pZ010001011111@proton.me
> > ---
> > fs/bcachefs/recovery.c | 12 +++++++++++-
> > 1 file changed, 11 insertions(+), 1 deletion(-)
> > 
> > diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c
> > index 67bba156cce9..bcec79122f65 100644
> > --- a/fs/bcachefs/recovery.c
> > +++ b/fs/bcachefs/recovery.c
> > @@ -1031,6 +1031,7 @@ int bch2_fs_initialize(struct bch_fs *c)
> > struct bkey_inode_buf packed_inode;
> > struct qstr lostfound = QSTR("lost+found");
> > int ret;
> > + struct bch_member *m;
> > 
> > bch_notice(c, "initializing new filesystem");
> > set_bit(BCH_FS_new_fs, &c->flags);
> > @@ -1086,8 +1087,17 @@ int bch2_fs_initialize(struct bch_fs *c)
> > bch_verbose(c, "marking superblocks");
> > ret = bch2_trans_mark_dev_sbs(c);
> > bch_err_msg(c, ret, "marking superblocks");
> > - if (ret)
> > + if (ret) {
> > + mutex_lock(&c->sb_lock);
> > + 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);
> > +
> > goto err;
> > + }
> > 
> > for_each_online_member(c, ca)
> > ca->new_fs_bucket_idx = 0;
> > --
> > 2.47.0

Best regards, Piotr Zalewski

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-21  8:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-20 17:08 [PATCH v1] bcachefs: set freespace inited to false if trans_mark_dev_sbs fails Piotr Zalewski
2024-10-20 23:23 ` Kent Overstreet
2024-10-21  8:05   ` Piotr Zalewski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox