Re: [PATCH RFC v2] btrfs: commit current transaction in btrfs_relocate_block_group

Bartosz Chronowski <[email protected]> Fri, 24 Jul 2026 19:09:00 +0200
Newsgroups dev.linux.lists.syzbot
Message-ID <hrj474vzjx3km6vsa24qhap7u4p2by7jfvt2pyqvzz6ihoc4y3@xenyhxrzl42r>
The v2 code change is correct: btrfs_commit_current_transaction() is in the
right place after reloc_chunk_start() and before btrfs_inc_block_group_ro(),
and the goto out branch preserves the required cleanup if the transaction
commit fails. The reproducer supports the fix in the tested scenario: the
target warning appeared in three of four unpatched VMs and in none of four
patched VMs. The patch is not ready yet because the Fixes: trailer and commit
message still need correction.

The new transaction drain does not replace the existing
btrfs_commit_transaction() in prepare_to_relocate(). The new call drains
earlier transaction work while the target block group is still available for
allocation. The existing call commits the relocation setup after
create_reloc_tree is enabled and reloc_ctl is published; relocation-root and
backref-cache handling depend on that transaction boundary. The warning
appears in this existing later call, but the ordering bug is introduced
earlier. Removing the existing call or changing only its error handling would
move or hide the failure.

The full build and both normal and strict checkpatch.pl runs passed.

Two parts of the commit message still need fixing:

- The Fixes tag should be:
Fixes: 3fd0a5585eb9 ("Btrfs: Metadata ENOSPC handling for balance")

72bd2323ec87 changed btrfs_update_root() error handling and did not
introduce the relocation ordering changed by this patch.

- Please describe the new call as draining the current transaction before
the block group becomes read-only. It should not claim global settlement
of every reservation or later transaction. Please also say why the setup
commit in prepare_to_relocate() remains necessary.

On Thu, Jul 23, 2026 at 04:17:10PM +0000, syzbot wrote:
> During block group relocation, btrfs_relocate_block_group() is called to
> relocate a block group. It first makes the block group read-only by calling
> btrfs_inc_block_group_ro(). If there are pending space reservations or
> pinned extents in the current transaction, making the block group read-only
> or subsequent relocation steps can fail, leading to a transaction abort and
> a warning in cleanup_transaction() during the transaction commit in
> prepare_to_relocate().
> 
> To fix this, commit the current transaction before calling
> btrfs_inc_block_group_ro() in btrfs_relocate_block_group(). This ensures
> that all pending extents are unpinned and space reservations are committed,
> providing a clean state for marking the block group read-only.
> 
> WARNING: fs/btrfs/transaction.c:2068 at cleanup_transaction+0x727/0x7c0
> Call Trace:
>  <TASK>
>  btrfs_commit_transaction+0x262c/0x30b0 fs/btrfs/transaction.c:2664
>  prepare_to_relocate+0x3dd/0x4e0 fs/btrfs/relocation.c:3541
>  relocate_block_group+0x141/0xe90 fs/btrfs/relocation.c:3566
>  do_nonremap_reloc+0xa7/0x560 fs/btrfs/relocation.c:5323
>  btrfs_relocate_block_group+0x6e2/0xaf0 fs/btrfs/relocation.c:5490
>  btrfs_relocate_chunk+0x114/0x830 fs/btrfs/volumes.c:3647
>  __btrfs_balance+0x1b6e/0x29e0 fs/btrfs/volumes.c:4586
>  btrfs_balance+0xaa6/0x1180 fs/btrfs/volumes.c:4973
>  btrfs_ioctl_balance+0x3dd/0x640 fs/btrfs/ioctl.c:3474
>  </TASK>
> 
> Fixes: 72bd2323ec87 ("Btrfs: do not abort transaction at btrfs_update_root() after failure to COW path")
> Assisted-by: Gemini:gemini-3.5-flash Gemini:gemini-3.1-pro-preview syzbot
> Reported-by: [email protected]
> Closes: https://syzkaller.appspot.com/bug?extid=021d10c4d4edc87daa03
> Link: https://syzkaller.appspot.com/ai_job?id=1627ee87-ad22-4097-8936-8e307d6ddb4b
> To: "Chris Mason" <[email protected]>
> To: "David Sterba" <[email protected]>
> To: <[email protected]>
> To: "Filipe Manana" <[email protected]>
> Cc: <[email protected]>
> 
> ---
> v2:
> - Replaced the transaction abort changes in the commit path with committing the current transaction before making the block group read-only during relocation.
> - Removed changes to fs/btrfs/qgroup.c, fs/btrfs/root-tree.c, fs/btrfs/transaction.c, and fs/btrfs/transaction.h.
> - Added a call to btrfs_commit_current_transaction() in btrfs_relocate_block_group() before calling btrfs_inc_block_group_ro().
> 
> v1:
> https://lore.kernel.org/all/[email protected]/T/
> ---
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index fb85bc8b3..05f5110c2 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c
> @@ -5430,6 +5430,10 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start,
>  	if (ret < 0)
>  		goto out_put_rc;
>  
> +	ret = btrfs_commit_current_transaction(extent_root);
> +	if (ret)
> +		goto out;
> +
>  	ret = btrfs_inc_block_group_ro(rc->block_group, true);
>  	if (ret)
>  		goto out;
> 
> 
> base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
> -- 
> This is an AI-generated patch subject to moderation.
> Reply with '#syz upstream' to Sign-off the patch as a human author
> and send it to the upstream kernel mailing lists.
> Reply with '#syz reject' to reject it ('#syz unreject' to undo).
> 
> See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
> You can comment on the patch as usual, syzbot will try to address
> the comments and send a new version of the patch if necessary.
> syzbot engineers can be reached at [email protected].