diff mbox series

btrfs: Call btrfs_set_header_generation() before btrfs_clean_tree_block()

Message ID 062d63c8-39bf-62d8-4562-625184e97b6c@I-love.SAKURA.ne.jp (mailing list archive)
State New, archived
Headers show
Series btrfs: Call btrfs_set_header_generation() before btrfs_clean_tree_block() | expand

Commit Message

Tetsuo Handa Sept. 20, 2022, 11:48 a.m. UTC
syzbot is reporting uninit-value in btrfs_clean_tree_block() [1], for
commit bc877d285ca3dba2 ("btrfs: Deduplicate extent_buffer init code")
missed that btrfs_set_header_generation() in btrfs_init_new_buffer() must
not be moved to after clean_tree_block() because clean_tree_block() is
calling btrfs_header_generation() since commit 55c69072d6bd5be1 ("Btrfs:
Fix extent_buffer usage when nodesize != leafsize").

Link: https://syzkaller.appspot.com/bug?extid=fba8e2116a12609b6c59 [1]
Reported-by: syzbot <syzbot+fba8e2116a12609b6c59@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Fixes: bc877d285ca3dba2 ("btrfs: Deduplicate extent_buffer init code")
---
This patch is not tested due to lack of reproducer.
I don't know whether initializing only generation field is sufficient.
Please check before applying.

 fs/btrfs/extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Johannes Thumshirn Sept. 20, 2022, 12:08 p.m. UTC | #1
On 20.09.22 13:49, Tetsuo Handa wrote:
> diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
> index 6914cd8024ba..9c7bf0ef6a5f 100644
> --- a/fs/btrfs/extent-tree.c
> +++ b/fs/btrfs/extent-tree.c
> @@ -4895,6 +4895,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
>  	 */
>  	btrfs_set_buffer_lockdep_class(lockdep_owner, buf, level);
>  
> +	btrfs_set_header_generation(buf, trans->transid);

Here you're setting the header generation into the extent buffer

>  	__btrfs_tree_lock(buf, nest);
>  	btrfs_clean_tree_block(buf);
>  	clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
> @@ -4905,7 +4906,6 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
>  	memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));

And here the extent buffer's part containing the header gets 
memzeroed resulting in header generation 0.

Fo rthis to poroperly work you'd need to bring the memzero_extent_buffer()
call before setting the header generation or re-set the generation after
clearing.


>  	btrfs_set_header_level(buf, level);
>  	btrfs_set_header_bytenr(buf, buf->start);
> -	btrfs_set_header_generation(buf, trans->transid);
>  	btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
>  	btrfs_set_header_owner(buf, owner);
>  	write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
diff mbox series

Patch

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 6914cd8024ba..9c7bf0ef6a5f 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4895,6 +4895,7 @@  btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	 */
 	btrfs_set_buffer_lockdep_class(lockdep_owner, buf, level);
 
+	btrfs_set_header_generation(buf, trans->transid);
 	__btrfs_tree_lock(buf, nest);
 	btrfs_clean_tree_block(buf);
 	clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
@@ -4905,7 +4906,6 @@  btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 	memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
 	btrfs_set_header_level(buf, level);
 	btrfs_set_header_bytenr(buf, buf->start);
-	btrfs_set_header_generation(buf, trans->transid);
 	btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
 	btrfs_set_header_owner(buf, owner);
 	write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);