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

@ -210,7 +210,7 @@ static int msg_insert(struct msg_msg *msg, struct mqueue_inode_info *info)
leaf = info->node_cache;
info->node_cache = NULL;
} else {
leaf = kmalloc(sizeof(*leaf), GFP_ATOMIC);
leaf = kmalloc_obj(*leaf, GFP_ATOMIC);
if (!leaf)
return -ENOMEM;
INIT_LIST_HEAD(&leaf->msg_list);
@ -449,7 +449,7 @@ static int mqueue_init_fs_context(struct fs_context *fc)
{
struct mqueue_fs_context *ctx;
ctx = kzalloc(sizeof(struct mqueue_fs_context), GFP_KERNEL);
ctx = kzalloc_obj(struct mqueue_fs_context, GFP_KERNEL);
if (!ctx)
return -ENOMEM;
@ -1088,7 +1088,7 @@ static int do_mq_timedsend(mqd_t mqdes, const char __user *u_msg_ptr,
* fall back to that if necessary.
*/
if (!info->node_cache)
new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
new_leaf = kmalloc_obj(*new_leaf, GFP_KERNEL);
spin_lock(&info->lock);
@ -1181,7 +1181,7 @@ static int do_mq_timedreceive(mqd_t mqdes, char __user *u_msg_ptr,
* fall back to that if necessary.
*/
if (!info->node_cache)
new_leaf = kmalloc(sizeof(*new_leaf), GFP_KERNEL);
new_leaf = kmalloc_obj(*new_leaf, GFP_KERNEL);
spin_lock(&info->lock);

View file

@ -148,7 +148,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
key_t key = params->key;
int msgflg = params->flg;
msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
msq = kmalloc_obj(*msq, GFP_KERNEL_ACCOUNT);
if (unlikely(!msq))
return -ENOMEM;

View file

@ -58,7 +58,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
}
err = -ENOMEM;
ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
ns = kzalloc_obj(struct ipc_namespace, GFP_KERNEL_ACCOUNT);
if (ns == NULL)
goto fail_dec;

View file

@ -514,7 +514,7 @@ static struct sem_array *sem_alloc(size_t nsems)
if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
return NULL;
sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
sma = kvzalloc_flex(*sma, sems, nsems, GFP_KERNEL_ACCOUNT);
if (unlikely(!sma))
return NULL;
@ -1853,7 +1853,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
undo_list = current->sysvsem.undo_list;
if (!undo_list) {
undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
undo_list = kzalloc_obj(*undo_list, GFP_KERNEL_ACCOUNT);
if (undo_list == NULL)
return -ENOMEM;
spin_lock_init(&undo_list->lock);
@ -1938,7 +1938,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
rcu_read_unlock();
/* step 2: allocate new undo structure */
new = kvzalloc(struct_size(new, semadj, nsems), GFP_KERNEL_ACCOUNT);
new = kvzalloc_flex(*new, semadj, nsems, GFP_KERNEL_ACCOUNT);
if (!new) {
ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
return ERR_PTR(-ENOMEM);
@ -2234,7 +2234,7 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
return -EINVAL;
if (nsops > SEMOPM_FAST) {
sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
sops = kvmalloc_objs(*sops, nsops, GFP_KERNEL);
if (sops == NULL)
return -ENOMEM;
}

View file

@ -722,7 +722,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
ns->shm_tot + numpages > ns->shm_ctlall)
return -ENOSPC;
shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
shp = kmalloc_obj(*shp, GFP_KERNEL_ACCOUNT);
if (unlikely(!shp))
return -ENOMEM;
@ -1618,7 +1618,7 @@ long do_shmat(int shmid, char __user *shmaddr, int shmflg,
rcu_read_unlock();
err = -ENOMEM;
sfd = kzalloc(sizeof(*sfd), GFP_KERNEL);
sfd = kzalloc_obj(*sfd, GFP_KERNEL);
if (!sfd) {
fput(base);
goto out_nattch;

View file

@ -141,7 +141,7 @@ void __init ipc_init_proc_interface(const char *path, const char *header,
struct proc_dir_entry *pde;
struct ipc_proc_iface *iface;
iface = kmalloc(sizeof(*iface), GFP_KERNEL);
iface = kmalloc_obj(*iface, GFP_KERNEL);
if (!iface)
return;
iface->path = path;