]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: replace kcalloc() calls to kzalloc_objs()
authorMiquel Sabaté Solà <mssola@mssola.com>
Tue, 24 Feb 2026 21:45:44 +0000 (22:45 +0100)
committerDavid Sterba <dsterba@suse.com>
Tue, 7 Apr 2026 16:55:59 +0000 (18:55 +0200)
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@kernel.org>
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/block-group.c
fs/btrfs/raid56.c
fs/btrfs/tests/zoned-tests.c
fs/btrfs/volumes.c
fs/btrfs/zoned.c

index d21df4446e3d8b2cdc5ad34728af5410a3c2d958..b5c2747158092ab00dabeea231e19c19a9800868 100644 (file)
@@ -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(u64, map->num_stripes, GFP_NOFS);
        if (!buf) {
                ret = -ENOMEM;
                goto out;
index aacf5e4eb765cea09c9841009b0f3d00ba6fce67..e31d57d6ab1e796fbff4e6c68f241d5a2bbc91a0 100644 (file)
@@ -2105,8 +2105,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(void *, rbio->real_stripes, GFP_NOFS);
+       unmap_array = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);
        if (!pointers || !unmap_array) {
                ret = -ENOMEM;
                goto out;
@@ -2839,8 +2839,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(void *, rbio->real_stripes, GFP_NOFS);
+       unmap_array = kzalloc_objs(void *, rbio->real_stripes, GFP_NOFS);
        if (!pointers || !unmap_array) {
                ret = -ENOMEM;
                goto out;
index da21c7aea31afff9c2890d02bf0c1b04b43b54f6..2bc3b14baa4109f6858bdfcbfdbc89bdc9ded6ac 100644 (file)
@@ -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;
index 62e02cc1efc93261f83a0cb171821408e9f00f53..deaf1997a3a593c7d7c6fef8f9a05b5e413f68b2 100644 (file)
@@ -5508,8 +5508,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;
@@ -6076,8 +6075,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);
 
index 11f4bdd617bb3b0dbf3ac52b5cd502178fac5e9d..f7ca01a32a031499d1e8db766579105ac813d982 100644 (file)
@@ -1699,8 +1699,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;
 
@@ -1918,7 +1917,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;