[PATCH] btrfs: Optimize the timing of prealloc memory allocation

Edward Adam Davis posted 1 patch 1 week, 3 days ago
fs/btrfs/qgroup.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
[PATCH] btrfs: Optimize the timing of prealloc memory allocation
Posted by Edward Adam Davis 1 week, 3 days ago
Too many abnormal exits can cause the prealloc assertion to fail, as
reported by syzbot in [1].

Move the prealloc memory allocation to before it is actually used.

[1]
assertion failed: prealloc == NULL :: 0, in fs/btrfs/qgroup.c:3529
kernel BUG at fs/btrfs/qgroup.c:3529!
Call Trace:
 <TASK>
 create_subvol+0x5ad/0x18f0 fs/btrfs/ioctl.c:570
 btrfs_mksubvol+0x6e4/0x12c0 fs/btrfs/ioctl.c:928
 __btrfs_ioctl_snap_create+0x2b2/0x730 fs/btrfs/ioctl.c:1201
 btrfs_ioctl_snap_create+0x131/0x180 fs/btrfs/ioctl.c:1259
 btrfs_ioctl+0xb4d/0xd00 fs/btrfs/ioctl.c:-1

Fixes: 252877a87015 ("btrfs: add ASSERTs on prealloc in qgroup functions")
Reported-by: syzbot+b44d4a4885bc82af2a06@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=b44d4a4885bc82af2a06
Tested-by: syzbot+b44d4a4885bc82af2a06@syzkaller.appspotmail.com
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
---
 fs/btrfs/qgroup.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index 9e2b53e90dcb..ac5380520152 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -3289,10 +3289,6 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
 	if (!btrfs_qgroup_enabled(fs_info))
 		return 0;
 
-	prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
-	if (!prealloc)
-		return -ENOMEM;
-
 	/*
 	 * There are only two callers of this function.
 	 *
@@ -3388,6 +3384,12 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
 		}
 	}
 
+	prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
+	if (!prealloc) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
 	spin_lock(&fs_info->qgroup_lock);
 
 	dstgroup = add_qgroup_rb(fs_info, prealloc, objectid);
-- 
2.43.0
Re: [PATCH] btrfs: Optimize the timing of prealloc memory allocation
Posted by Qu Wenruo 1 week, 3 days ago

在 2025/12/8 21:35, Edward Adam Davis 写道:
> Too many abnormal exits can cause the prealloc assertion to fail, as
> reported by syzbot in [1].
> 
> Move the prealloc memory allocation to before it is actually used.
> 
> [1]
> assertion failed: prealloc == NULL :: 0, in fs/btrfs/qgroup.c:3529
> kernel BUG at fs/btrfs/qgroup.c:3529!
> Call Trace:
>   <TASK>
>   create_subvol+0x5ad/0x18f0 fs/btrfs/ioctl.c:570
>   btrfs_mksubvol+0x6e4/0x12c0 fs/btrfs/ioctl.c:928
>   __btrfs_ioctl_snap_create+0x2b2/0x730 fs/btrfs/ioctl.c:1201
>   btrfs_ioctl_snap_create+0x131/0x180 fs/btrfs/ioctl.c:1259
>   btrfs_ioctl+0xb4d/0xd00 fs/btrfs/ioctl.c:-1

There is already a full revert, and your fix misses all the other error 
cases.

https://lore.kernel.org/linux-btrfs/20251208195407.GC4859@twin.jikos.cz/T/#t


> 
> Fixes: 252877a87015 ("btrfs: add ASSERTs on prealloc in qgroup functions")
> Reported-by: syzbot+b44d4a4885bc82af2a06@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=b44d4a4885bc82af2a06
> Tested-by: syzbot+b44d4a4885bc82af2a06@syzkaller.appspotmail.com
> Signed-off-by: Edward Adam Davis <eadavis@qq.com>
> ---
>   fs/btrfs/qgroup.c | 10 ++++++----
>   1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
> index 9e2b53e90dcb..ac5380520152 100644
> --- a/fs/btrfs/qgroup.c
> +++ b/fs/btrfs/qgroup.c
> @@ -3289,10 +3289,6 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
>   	if (!btrfs_qgroup_enabled(fs_info))
>   		return 0;
>   
> -	prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
> -	if (!prealloc)
> -		return -ENOMEM;
> -
>   	/*
>   	 * There are only two callers of this function.
>   	 *
> @@ -3388,6 +3384,12 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans, u64 srcid,
>   		}
>   	}
>   
> +	prealloc = kzalloc(sizeof(*prealloc), GFP_NOFS);
> +	if (!prealloc) {
> +		ret = -ENOMEM;
> +		goto out;
> +	}
> +
>   	spin_lock(&fs_info->qgroup_lock);
>   
>   	dstgroup = add_qgroup_rb(fs_info, prealloc, objectid);