Bug 27245 - btrfs: move transaction aborts to the error site in add_block_group_free_space()
Summary: btrfs: move transaction aborts to the error site in add_block_group_free_space()
Status: RESOLVED FIXED
Alias: None
Product: ANCK 6.6 Dev
Classification: ANCK
Component: fs (show other bugs) fs
Version: unspecified
Hardware: All Linux
: P3-Medium S3-normal
Target Milestone: ---
Assignee: chuguangqing
QA Contact: shuming
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-11-17 09:24 UTC by chuguangqing
Modified: 2025-11-17 18:52 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description chuguangqing inspur_group 2025-11-17 09:24:40 UTC
There's a race between a task disabling quotas and another running the
rescan ioctl that can result in a use-after-free of qgroup records from
the fs_info->qgroup_tree rbtree.

This happens as follows:

1) Task A enters btrfs_ioctl_quota_rescan() -> btrfs_qgroup_rescan();

2) Task B enters btrfs_quota_disable() and calls
   btrfs_qgroup_wait_for_completion(), which does nothing because at that
   point fs_info->qgroup_rescan_running is false (it wasn't set yet by
   task A);

3) Task B calls btrfs_free_qgroup_config() which starts freeing qgroups
   from fs_info->qgroup_tree without taking the lock fs_info->qgroup_lock;

4) Task A enters qgroup_rescan_zero_tracking() which starts iterating
   the fs_info->qgroup_tree tree while holding fs_info->qgroup_lock,
   but task B is freeing qgroup records from that tree without holding
   the lock, resulting in a use-after-free.

Fix this by taking fs_info->qgroup_lock at btrfs_free_qgroup_config().
Also at btrfs_qgroup_rescan() don't start the rescan worker if quotas
were already disabled.
Comment 1 小龙 admin 2025-11-17 09:28:44 UTC
The PR Link: https://gitee.com/anolis/cloud-kernel/pulls/6025
Comment 2 chuguangqing inspur_group 2025-11-17 09:33:38 UTC
btrfs: move transaction aborts to the error site in add_block_group_free_space()
Comment 3 小龙 admin 2025-11-17 09:36:09 UTC
The PR Link: https://gitee.com/anolis/cloud-kernel/pulls/6026
Comment 4 Joseph Qi alibaba_cloud_group 2025-11-17 18:52:01 UTC
(In reply to 小龙 from comment #3)
> The PR Link: https://gitee.com/anolis/cloud-kernel/pulls/6026

merged