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

@@ -927,7 +927,7 @@ static int virtballoon_probe(struct virtio_device *vdev)
return -EINVAL;
}
vdev->priv = vb = kzalloc(sizeof(*vb), GFP_KERNEL);
vdev->priv = vb = kzalloc_obj(*vb, GFP_KERNEL);
if (!vb) {
err = -ENOMEM;
goto out;

View File

@@ -83,7 +83,7 @@ static int virtinput_send_status(struct virtio_input *vi,
if (vi->idev->mt && type == EV_MSC && code == MSC_TIMESTAMP)
return 0;
stsbuf = kzalloc(sizeof(*stsbuf), GFP_ATOMIC);
stsbuf = kzalloc_obj(*stsbuf, GFP_ATOMIC);
if (!stsbuf)
return -ENOMEM;
@@ -229,7 +229,7 @@ static int virtinput_probe(struct virtio_device *vdev)
if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
return -ENODEV;
vi = kzalloc(sizeof(*vi), GFP_KERNEL);
vi = kzalloc_obj(*vi, GFP_KERNEL);
if (!vi)
return -ENOMEM;

View File

@@ -2940,7 +2940,7 @@ static int virtio_mem_probe(struct virtio_device *vdev)
BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);
vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
vdev->priv = vm = kzalloc_obj(*vm, GFP_KERNEL);
if (!vm)
return -ENOMEM;

View File

@@ -575,7 +575,7 @@ static int virtio_mmio_probe(struct platform_device *pdev)
unsigned long magic;
int rc;
vm_dev = kzalloc(sizeof(*vm_dev), GFP_KERNEL);
vm_dev = kzalloc_obj(*vm_dev, GFP_KERNEL);
if (!vm_dev)
return -ENOMEM;

View File

@@ -124,7 +124,7 @@ static int virtio_pci_admin_legacy_io_read(struct pci_dev *pdev, u16 opcode,
if (vf_id < 0)
return vf_id;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return -ENOMEM;
@@ -210,7 +210,7 @@ int virtio_pci_admin_legacy_io_notify_info(struct pci_dev *pdev,
if (vf_id < 0)
return vf_id;
result = kzalloc(sizeof(*result), GFP_KERNEL);
result = kzalloc_obj(*result, GFP_KERNEL);
if (!result)
return -ENOMEM;

View File

@@ -134,14 +134,13 @@ static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
vp_dev->msix_vectors = nvectors;
vp_dev->msix_names = kmalloc_array(nvectors,
sizeof(*vp_dev->msix_names),
GFP_KERNEL);
vp_dev->msix_names = kmalloc_objs(*vp_dev->msix_names, nvectors,
GFP_KERNEL);
if (!vp_dev->msix_names)
goto error;
vp_dev->msix_affinity_masks
= kcalloc(nvectors, sizeof(*vp_dev->msix_affinity_masks),
GFP_KERNEL);
= kzalloc_objs(*vp_dev->msix_affinity_masks, nvectors,
GFP_KERNEL);
if (!vp_dev->msix_affinity_masks)
goto error;
for (i = 0; i < nvectors; ++i)
@@ -211,7 +210,7 @@ static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned int in
struct virtio_pci_vq_info **p_info)
{
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
struct virtio_pci_vq_info *info = kmalloc_obj(*info, GFP_KERNEL);
struct virtqueue *vq;
unsigned long flags;
@@ -387,7 +386,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
bool per_vq_vectors;
u16 avq_num = 0;
vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
vp_dev->vqs = kzalloc_objs(*vp_dev->vqs, nvqs, GFP_KERNEL);
if (!vp_dev->vqs)
return -ENOMEM;
@@ -464,7 +463,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
struct virtqueue *vq;
u16 avq_num = 0;
vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
vp_dev->vqs = kzalloc_objs(*vp_dev->vqs, nvqs, GFP_KERNEL);
if (!vp_dev->vqs)
return -ENOMEM;
@@ -686,7 +685,7 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
int rc;
/* allocate our structure and fill it out */
vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
vp_dev = kzalloc_obj(struct virtio_pci_device, GFP_KERNEL);
if (!vp_dev)
return -ENOMEM;

View File

@@ -137,11 +137,11 @@ int vp_modern_admin_cmd_exec(struct virtio_device *vdev,
if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ))
return -EOPNOTSUPP;
va_status = kzalloc(sizeof(*va_status), GFP_KERNEL);
va_status = kzalloc_obj(*va_status, GFP_KERNEL);
if (!va_status)
return -ENOMEM;
va_hdr = kzalloc(sizeof(*va_hdr), GFP_KERNEL);
va_hdr = kzalloc_obj(*va_hdr, GFP_KERNEL);
if (!va_hdr) {
ret = -ENOMEM;
goto err_alloc;
@@ -204,7 +204,7 @@ static void virtio_pci_admin_cmd_list_init(struct virtio_device *virtio_dev)
__le64 *data;
int ret;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return;
@@ -246,11 +246,11 @@ virtio_pci_admin_cmd_dev_parts_objects_enable(struct virtio_device *virtio_dev)
u16 set_data_size;
int ret;
get_data = kzalloc(sizeof(*get_data), GFP_KERNEL);
get_data = kzalloc_obj(*get_data, GFP_KERNEL);
if (!get_data)
return;
result = kzalloc(sizeof(*result), GFP_KERNEL);
result = kzalloc_obj(*result, GFP_KERNEL);
if (!result)
goto end;
@@ -310,7 +310,7 @@ static void virtio_pci_admin_cmd_cap_init(struct virtio_device *virtio_dev)
struct scatterlist result_sg;
int ret;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return;
@@ -929,7 +929,7 @@ int virtio_pci_admin_mode_set(struct pci_dev *pdev, u8 flags)
if (vf_id < 0)
return vf_id;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return -ENOMEM;
@@ -1054,7 +1054,7 @@ int virtio_pci_admin_obj_destroy(struct pci_dev *pdev, u16 obj_type, u32 id)
if (obj_type != VIRTIO_RESOURCE_OBJ_DEV_PARTS)
return -EINVAL;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return -ENOMEM;
@@ -1109,11 +1109,11 @@ int virtio_pci_admin_dev_parts_metadata_get(struct pci_dev *pdev, u16 obj_type,
if (vf_id < 0)
return vf_id;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return -ENOMEM;
result = kzalloc(sizeof(*result), GFP_KERNEL);
result = kzalloc_obj(*result, GFP_KERNEL);
if (!result) {
ret = -ENOMEM;
goto end;
@@ -1173,7 +1173,7 @@ int virtio_pci_admin_dev_parts_get(struct pci_dev *pdev, u16 obj_type, u32 id,
if (vf_id < 0)
return vf_id;
data = kzalloc(sizeof(*data), GFP_KERNEL);
data = kzalloc_obj(*data, GFP_KERNEL);
if (!data)
return -ENOMEM;

View File

@@ -1209,7 +1209,7 @@ static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vring_spl
struct vring_desc_extra *extra;
u32 num = vring_split->vring.num;
state = kmalloc_array(num, sizeof(struct vring_desc_state_split), GFP_KERNEL);
state = kmalloc_objs(struct vring_desc_state_split, num, GFP_KERNEL);
if (!state)
goto err_state;
@@ -1308,7 +1308,7 @@ static struct virtqueue *__vring_new_virtqueue_split(unsigned int index,
struct vring_virtqueue *vq;
int err;
vq = kmalloc(sizeof(*vq), GFP_KERNEL);
vq = kmalloc_obj(*vq, GFP_KERNEL);
if (!vq)
return NULL;
@@ -2349,8 +2349,7 @@ static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num)
struct vring_desc_extra *desc_extra;
unsigned int i;
desc_extra = kmalloc_array(num, sizeof(struct vring_desc_extra),
GFP_KERNEL);
desc_extra = kmalloc_objs(struct vring_desc_extra, num, GFP_KERNEL);
if (!desc_extra)
return NULL;
@@ -2450,7 +2449,7 @@ static int vring_alloc_state_extra_packed(struct vring_virtqueue_packed *vring_p
struct vring_desc_extra *extra;
u32 num = vring_packed->vring.num;
state = kmalloc_array(num, sizeof(struct vring_desc_state_packed), GFP_KERNEL);
state = kmalloc_objs(struct vring_desc_state_packed, num, GFP_KERNEL);
if (!state)
goto err_desc_state;
@@ -2529,7 +2528,7 @@ static struct virtqueue *__vring_new_virtqueue_packed(unsigned int index,
struct vring_virtqueue *vq;
int err;
vq = kmalloc(sizeof(*vq), GFP_KERNEL);
vq = kmalloc_obj(*vq, GFP_KERNEL);
if (!vq)
return NULL;

View File

@@ -287,7 +287,7 @@ create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)
if (!affvecs)
return NULL;
masks = kcalloc(nvecs, sizeof(*masks), GFP_KERNEL);
masks = kzalloc_objs(*masks, nvecs, GFP_KERNEL);
if (!masks)
return NULL;
@@ -462,7 +462,7 @@ static int virtio_vdpa_probe(struct vdpa_device *vdpa)
struct virtio_vdpa_device *vd_dev, *reg_dev = NULL;
int ret = -EINVAL;
vd_dev = kzalloc(sizeof(*vd_dev), GFP_KERNEL);
vd_dev = kzalloc_obj(*vd_dev, GFP_KERNEL);
if (!vd_dev)
return -ENOMEM;