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

From: David Sterba

Date: Thu Feb 26 2026 - 19:27:48 EST


On Tue, Feb 24, 2026 at 10:45:44PM +0100, Miquel Sabaté Solà wrote:
> Commit 2932ba8d9c99 ("slab: Introduce kmalloc_obj() and family")
> introduced, among many others, the kzalloc_objs() helper, which has some
> benefits over kcalloc(). Namely, internal introspection of the allocated
> type now becomes possible, allowing for future alignment-aware choices
> to be made by the allocator and future hardening work that can be type
> sensitive. Dropping 'sizeof' comes also as a nice side-effect.
>
> Moreover, this also allows us to be in line with the recent tree-wide
> migration to the kmalloc_obj() and family of helpers. See
> commit 69050f8d6d07 ("treewide: Replace kmalloc with kmalloc_obj for
> non-scalar types").
>
> Reviewed-by: Kees Cook <kees@xxxxxxxxxx>
> Signed-off-by: Miquel Sabaté Solà <mssola@xxxxxxxxxx>

Added to for-next, thanks.

> - buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
> + buf = kzalloc_objs(*buf, map->num_stripes, GFP_NOFS);

> - 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);

> - 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);

I've changed it to the type in the above cases so it's a direct
conversion that only removes the sizeof(). For the rest there are no
strong preferences so we'll keep it as is, and my preference for new code
is to use the types.