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

From: David Sterba

Date: Tue Feb 24 2026 - 08:53:06 EST


On Tue, Feb 24, 2026 at 03:12:07PM +1030, Qu Wenruo wrote:
> 在 2026/2/24 15:07, Qu Wenruo 写道:
> > 在 2026/2/24 10:14, Miquel Sabaté Solà 写道:
> >> Commit 2932ba8d9c99 ("slab: Introduce kmalloc_obj() and family")
> >> introduced, among many others, the kzalloc_objs() helper, which has some
> >> benefits over kcalloc().
> >>
> >> Cc: Kees Cook <kees@xxxxxxxxxx>
> >> Signed-off-by: Miquel Sabaté Solà <mssola@xxxxxxxxxx>
> >> ---
> >>   fs/btrfs/block-group.c       | 2 +-
> >>   fs/btrfs/raid56.c            | 8 ++++----
> >>   fs/btrfs/tests/zoned-tests.c | 2 +-
> >>   fs/btrfs/volumes.c           | 6 ++----
> >>   fs/btrfs/zoned.c             | 5 ++---
> >>   5 files changed, 10 insertions(+), 13 deletions(-)
> >>
> >> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
> >> index 37bea850b3f0..8d85b4707690 100644
> >> --- a/fs/btrfs/block-group.c
> >> +++ b/fs/btrfs/block-group.c
> >> @@ -2239,7 +2239,7 @@ int btrfs_rmap_block(struct btrfs_fs_info
> >> *fs_info, u64 chunk_start,
> >>       if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
> >>           io_stripe_size =
> >> btrfs_stripe_nr_to_offset(nr_data_stripes(map));
> >> -    buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
> >> +    buf = kzalloc_objs(*buf, map->num_stripes, GFP_NOFS);
> >
> > Not sure if we should use *buf for the type.
> >
> > I still remember we had some bugs related to incorrect type usage.
> >
> > Another thing is, we may not want to use the kzalloc version.
> > We don't want to waste CPU time just to zero out the content meanwhile
> > we're ensured to re-assign the contents.
> >
> > Thus kmalloc_objs() maybe better.
>
> Sorry I only mean for some particular call sites, like this one.
>
> Not all call sites can be converted to kmalloc version, and will need
> proper inspection one by one.

I'd rather keep the zeroing version, we do it almost everywhere as a
precaution. The CPU time spent on that is not significant.