treewide: Replace kmalloc with kmalloc_obj for non-scalar types

This is the result of running the Coccinelle script from
scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to
avoid scalar types (which need careful case-by-case checking), and
instead replace kmalloc-family calls that allocate struct or union
object instances:

Single allocations:	kmalloc(sizeof(TYPE), ...)
are replaced with:	kmalloc_obj(TYPE, ...)

Array allocations:	kmalloc_array(COUNT, sizeof(TYPE), ...)
are replaced with:	kmalloc_objs(TYPE, COUNT, ...)

Flex array allocations:	kmalloc(struct_size(PTR, FAM, COUNT), ...)
are replaced with:	kmalloc_flex(*PTR, FAM, COUNT, ...)

(where TYPE may also be *VAR)

The resulting allocations no longer return "void *", instead returning
"TYPE *".

Signed-off-by: Kees Cook <kees@kernel.org>
This commit is contained in:
Kees Cook
2026-02-20 23:49:23 -08:00
parent d39a1d7486
commit 69050f8d6d
8016 changed files with 20055 additions and 20913 deletions

View File

@@ -32,7 +32,7 @@ static struct block_device **fscrypt_get_devices(struct super_block *sb,
if (devs)
return devs;
}
devs = kmalloc(sizeof(*devs), GFP_KERNEL);
devs = kmalloc_obj(*devs, GFP_KERNEL);
if (!devs)
return ERR_PTR(-ENOMEM);
devs[0] = sb->s_bdev;
@@ -169,7 +169,7 @@ int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key,
unsigned int i;
int err;
blk_key = kmalloc(sizeof(*blk_key), GFP_KERNEL);
blk_key = kmalloc_obj(*blk_key, GFP_KERNEL);
if (!blk_key)
return -ENOMEM;

View File

@@ -209,7 +209,7 @@ static int allocate_filesystem_keyring(struct super_block *sb)
if (sb->s_master_keys)
return 0;
keyring = kzalloc(sizeof(*keyring), GFP_KERNEL);
keyring = kzalloc_obj(*keyring, GFP_KERNEL);
if (!keyring)
return -ENOMEM;
spin_lock_init(&keyring->lock);
@@ -434,7 +434,7 @@ static int add_new_master_key(struct super_block *sb,
struct fscrypt_master_key *mk;
int err;
mk = kzalloc(sizeof(*mk), GFP_KERNEL);
mk = kzalloc_obj(*mk, GFP_KERNEL);
if (!mk)
return -ENOMEM;

View File

@@ -221,7 +221,7 @@ fscrypt_get_direct_key(const struct fscrypt_inode_info *ci, const u8 *raw_key)
return dk;
/* Nope, allocate one. */
dk = kzalloc(sizeof(*dk), GFP_KERNEL);
dk = kzalloc_obj(*dk, GFP_KERNEL);
if (!dk)
return ERR_PTR(-ENOMEM);
dk->dk_sb = ci->ci_inode->i_sb;

View File

@@ -813,7 +813,7 @@ int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
if (param->type == fs_value_is_string && *param->string)
arg = param->string;
policy = kzalloc(sizeof(*policy), GFP_KERNEL);
policy = kzalloc_obj(*policy, GFP_KERNEL);
if (!policy)
return -ENOMEM;