Re: [PATCH] btrfs: replace kcalloc() calls to kzalloc_objs()

From: Kees Cook

Date: Mon Feb 23 2026 - 19:06:34 EST


On Tue, Feb 24, 2026 at 12:44:51AM +0100, Miquel Sabaté Solà wrote:
> diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
> index 02105d68accb..1ebfed8f0a0a 100644
> --- a/fs/btrfs/raid56.c
> +++ b/fs/btrfs/raid56.c
> @@ -2110,8 +2110,8 @@ 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 = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
> - unmap_array = kcalloc(rbio->real_stripes, sizeof(void *), GFP_NOFS);
> + pointers = kzalloc_objs(*pointers, rbio->real_stripes, GFP_NOFS);
> + unmap_array = kzalloc_objs(*unmap_array, rbio->real_stripes, GFP_NOFS);
> if (!pointers || !unmap_array) {
> ret = -ENOMEM;
> goto out;

Just as a style option, I wanted to point out (for at least the above,
I didn't check the rest), you can do the definition and declaration at
once with "auto" and put the type in the alloc:

auto pointers = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);

But either way is fine. :) This patch looks good to me!

Reviewed-by: Kees Cook <kees@xxxxxxxxxx>

--
Kees Cook