Re: [PATCH 2/2] btrfs: return early if allocations fail on raid56

From: Qu Wenruo

Date: Fri Feb 27 2026 - 15:52:22 EST




在 2026/2/28 01:47, Miquel Sabaté Solà 写道:
In both the recover_sectors() and the recover_scrub_rbio() functions we
initialized two pointers by allocating them, and then returned early if
either of them failed. But we can simply allocate the first one and do
the check, and repeat for the second pointer. This way we return earlier
on allocation failures, and we don't perform unneeded kfree() calls.

Signed-off-by: Miquel Sabaté Solà <mssola@xxxxxxxxxx>

Again, the old pattern is just fine.

Thanks,
Qu

---
fs/btrfs/raid56.c | 29 ++++++++++++++++++-----------
1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
index e31d57d6ab1e..c8ece97259e3 100644
--- a/fs/btrfs/raid56.c
+++ b/fs/btrfs/raid56.c
@@ -2094,8 +2094,8 @@ static int recover_vertical(struct btrfs_raid_bio *rbio, int sector_nr,
static int recover_sectors(struct btrfs_raid_bio *rbio)
{
- void **pointers = NULL;
- void **unmap_array = NULL;
+ void **pointers;
+ void **unmap_array;
int sectornr;
int ret = 0;
@@ -2105,11 +2105,15 @@ static int recover_sectors(struct btrfs_raid_bio *rbio)
* @unmap_array stores copy of pointers that does not get reordered
* during reconstruction so that kunmap_local works.
*/
+
pointers = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);
+ if (!pointers)
+ return -ENOMEM;
+
unmap_array = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);
- if (!pointers || !unmap_array) {
- ret = -ENOMEM;
- goto out;
+ if (!unmap_array) {
+ kfree(pointers);
+ return -ENOMEM;
}
if (rbio->operation == BTRFS_RBIO_READ_REBUILD) {
@@ -2126,7 +2130,6 @@ static int recover_sectors(struct btrfs_raid_bio *rbio)
break;
}
-out:
kfree(pointers);
kfree(unmap_array);
return ret;
@@ -2828,8 +2831,8 @@ static inline int is_data_stripe(struct btrfs_raid_bio *rbio, int stripe)
static int recover_scrub_rbio(struct btrfs_raid_bio *rbio)
{
- void **pointers = NULL;
- void **unmap_array = NULL;
+ void **pointers;
+ void **unmap_array;
int sector_nr;
int ret = 0;
@@ -2839,11 +2842,15 @@ static int recover_scrub_rbio(struct btrfs_raid_bio *rbio)
* @unmap_array stores copy of pointers that does not get reordered
* during reconstruction so that kunmap_local works.
*/
+
pointers = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);
+ if (!pointers)
+ return -ENOMEM;
+
unmap_array = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);
- if (!pointers || !unmap_array) {
- ret = -ENOMEM;
- goto out;
+ if (!unmap_array) {
+ kfree(pointers);
+ return -ENOMEM;
}
for (sector_nr = 0; sector_nr < rbio->stripe_nsectors; sector_nr++) {