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

@@ -312,7 +312,7 @@ static struct sg_table *vmalloc_to_sgt(char *data, uint32_t size, int *sg_ents)
if (WARN_ON(!PAGE_ALIGNED(data)))
return NULL;
sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
sgt = kmalloc_obj(*sgt, GFP_KERNEL);
if (!sgt)
return NULL;
@@ -936,8 +936,7 @@ int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev)
struct virtio_gpu_vbuffer *vbuf;
void *resp_buf;
resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_display_info),
GFP_KERNEL);
resp_buf = kzalloc_obj(struct virtio_gpu_resp_display_info, GFP_KERNEL);
if (!resp_buf)
return -ENOMEM;
@@ -959,8 +958,7 @@ int virtio_gpu_cmd_get_capset_info(struct virtio_gpu_device *vgdev, int idx)
struct virtio_gpu_vbuffer *vbuf;
void *resp_buf;
resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_capset_info),
GFP_KERNEL);
resp_buf = kzalloc_obj(struct virtio_gpu_resp_capset_info, GFP_KERNEL);
if (!resp_buf)
return -ENOMEM;
@@ -995,7 +993,7 @@ int virtio_gpu_cmd_get_capset(struct virtio_gpu_device *vgdev,
if (version > vgdev->capsets[idx].max_version)
return -EINVAL;
cache_ent = kzalloc(sizeof(*cache_ent), GFP_KERNEL);
cache_ent = kzalloc_obj(*cache_ent, GFP_KERNEL);
if (!cache_ent)
return -ENOMEM;
@@ -1063,8 +1061,7 @@ int virtio_gpu_cmd_get_edids(struct virtio_gpu_device *vgdev)
return -EINVAL;
for (scanout = 0; scanout < vgdev->num_scanouts; scanout++) {
resp_buf = kzalloc(sizeof(struct virtio_gpu_resp_edid),
GFP_KERNEL);
resp_buf = kzalloc_obj(struct virtio_gpu_resp_edid, GFP_KERNEL);
if (!resp_buf)
return -ENOMEM;
@@ -1341,7 +1338,7 @@ virtio_gpu_cmd_resource_assign_uuid(struct virtio_gpu_device *vgdev,
struct virtio_gpu_vbuffer *vbuf;
struct virtio_gpu_resp_resource_uuid *resp_buf;
resp_buf = kzalloc(sizeof(*resp_buf), GFP_KERNEL);
resp_buf = kzalloc_obj(*resp_buf, GFP_KERNEL);
if (!resp_buf) {
spin_lock(&vgdev->resource_export_lock);
bo->uuid_state = STATE_ERR;
@@ -1394,7 +1391,7 @@ int virtio_gpu_cmd_map(struct virtio_gpu_device *vgdev,
struct virtio_gpu_vbuffer *vbuf;
struct virtio_gpu_resp_map_info *resp_buf;
resp_buf = kzalloc(sizeof(*resp_buf), GFP_KERNEL);
resp_buf = kzalloc_obj(*resp_buf, GFP_KERNEL);
if (!resp_buf)
return -ENOMEM;