From: Qu Wenruo <wqu@suse.com>
To: "Miquel Sabaté Solà" <mssola@mssola.com>,
"Qu Wenruo" <quwenruo.btrfs@gmx.com>
Cc: dsterba@suse.com, clm@fb.com, naohiro.aota@wdc.com,
linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
kees@kernel.org
Subject: Re: [PATCH] btrfs: replace kcalloc() calls to kzalloc_objs()
Date: Tue, 24 Feb 2026 17:18:31 +1030 [thread overview]
Message-ID: <e21ea0cf-b392-487f-843f-962efedcf10c@suse.com> (raw)
In-Reply-To: <699d4704.050a0220.1a6450.86d7SMTPIN_ADDED_BROKEN@mx.google.com>
在 2026/2/24 17:06, Miquel Sabaté Solà 写道:
> Qu Wenruo @ 2026-02-24 15:07 +1030:
>
>> 在 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@kernel.org>
>>> Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
>>> ---
>>> 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.
>
> Considering the type of 'buf' and how kzalloc_objs() will resolve the
> first argument '*buf', it should really just be equivalent to what was
> written before.
Good luck if you miss the '*' for a structure pointer, and compiler
won't give you any warning.
I still remembered that Johannes exposed such bug for me.
Unfortunately I didn't have exact lore link for it.
>
>>
>> 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.
>
> Yes, having a second look at this function, it looks like kmalloc_objs()
> might just be enough. If you don't mind, I will add another commit in v2
> translating kzalloc_objs() into kmalloc_objs() wherever I see we can do
> this from the ones I've touched. This way we can easily revert in case
> things go south :)
>
>>
>> Thanks,
>> Qu
>
> Thanks,
> Miquel
>
>>
>>
>>> if (!buf) {
>>> ret = -ENOMEM;
>>> goto out;
>>> 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;
>>> @@ -2844,8 +2844,8 @@ 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 = 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;
>>> diff --git a/fs/btrfs/tests/zoned-tests.c b/fs/btrfs/tests/zoned-tests.c
>>> index da21c7aea31a..2bc3b14baa41 100644
>>> --- a/fs/btrfs/tests/zoned-tests.c
>>> +++ b/fs/btrfs/tests/zoned-tests.c
>>> @@ -58,7 +58,7 @@ static int test_load_zone_info(struct btrfs_fs_info *fs_info,
>>> return -ENOMEM;
>>> }
>>> - zone_info = kcalloc(test->num_stripes, sizeof(*zone_info), GFP_KERNEL);
>>> + zone_info = kzalloc_objs(*zone_info, test->num_stripes, GFP_KERNEL);
>>> if (!zone_info) {
>>> test_err("cannot allocate zone info");
>>> return -ENOMEM;
>>> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
>>> index e15e138c515b..c0cf8f7c5a8e 100644
>>> --- a/fs/btrfs/volumes.c
>>> +++ b/fs/btrfs/volumes.c
>>> @@ -5499,8 +5499,7 @@ static int calc_one_profile_avail(struct btrfs_fs_info *fs_info,
>>> goto out;
>>> }
>>> - devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
>>> - GFP_NOFS);
>>> + devices_info = kzalloc_objs(*devices_info, fs_devices->rw_devices, GFP_NOFS);
>>> if (!devices_info) {
>>> ret = -ENOMEM;
>>> goto out;
>>> @@ -6067,8 +6066,7 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
>>> ctl.space_info = space_info;
>>> init_alloc_chunk_ctl(fs_devices, &ctl);
>>> - devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
>>> - GFP_NOFS);
>>> + devices_info = kzalloc_objs(*devices_info, fs_devices->rw_devices, GFP_NOFS);
>>> if (!devices_info)
>>> return ERR_PTR(-ENOMEM);
>>> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
>>> index ab330ec957bc..851b0de7bed7 100644
>>> --- a/fs/btrfs/zoned.c
>>> +++ b/fs/btrfs/zoned.c
>>> @@ -1697,8 +1697,7 @@ static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
>>> return -EINVAL;
>>> }
>>> - raid0_allocs = kcalloc(map->num_stripes / map->sub_stripes,
>>> sizeof(*raid0_allocs),
>>> - GFP_NOFS);
>>> + raid0_allocs = kzalloc_objs(*raid0_allocs, map->num_stripes / map->sub_stripes, GFP_NOFS);
>>> if (!raid0_allocs)
>>> return -ENOMEM;
>>> @@ -1916,7 +1915,7 @@ int btrfs_load_block_group_zone_info(struct
>>> btrfs_block_group *cache, bool new)
>>> cache->physical_map = map;
>>> - zone_info = kcalloc(map->num_stripes, sizeof(*zone_info), GFP_NOFS);
>>> + zone_info = kzalloc_objs(*zone_info, map->num_stripes, GFP_NOFS);
>>> if (!zone_info) {
>>> ret = -ENOMEM;
>>> goto out;
next prev parent reply other threads:[~2026-02-24 6:48 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-23 23:44 [PATCH] btrfs: replace kcalloc() calls to kzalloc_objs() Miquel Sabaté Solà
2026-02-24 0:06 ` Kees Cook
2026-02-24 6:23 ` Miquel Sabaté Solà
[not found] ` <699d43e6.170a0220.3a6e96.a235SMTPIN_ADDED_BROKEN@mx.google.com>
2026-02-24 11:29 ` David Sterba
2026-02-24 12:23 ` Miquel Sabaté Solà
2026-02-24 4:37 ` Qu Wenruo
2026-02-24 4:42 ` Qu Wenruo
2026-02-24 13:52 ` David Sterba
2026-02-24 6:36 ` Miquel Sabaté Solà
[not found] ` <699d4704.050a0220.1a6450.86d7SMTPIN_ADDED_BROKEN@mx.google.com>
2026-02-24 6:48 ` Qu Wenruo [this message]
2026-02-24 8:59 ` Miquel Sabaté Solà
2026-02-24 14:55 ` David Laight
2026-02-25 14:44 ` David Sterba
2026-02-25 17:11 ` David Laight
2026-02-24 6:32 ` Johannes Thumshirn
2026-02-24 6:46 ` Miquel Sabaté Solà
2026-02-24 6:54 ` Qu Wenruo
2026-02-24 9:04 ` Miquel Sabaté Solà
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e21ea0cf-b392-487f-843f-962efedcf10c@suse.com \
--to=wqu@suse.com \
--cc=clm@fb.com \
--cc=dsterba@suse.com \
--cc=kees@kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mssola@mssola.com \
--cc=naohiro.aota@wdc.com \
--cc=quwenruo.btrfs@gmx.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox