summaryrefslogtreecommitdiff
path: root/virt
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-02-20 23:49:23 -0800
committerKees Cook <kees@kernel.org>2026-02-21 01:02:28 -0800
commit69050f8d6d075dc01af7a5f2f550a8067510366f (patch)
treebb265f94d9dfa7876c06a5d9f88673d496a15341 /virt
parentd39a1d7486d98668dd34aaa6732aad7977c45f5a (diff)
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>
Diffstat (limited to 'virt')
-rw-r--r--virt/kvm/coalesced_mmio.c3
-rw-r--r--virt/kvm/eventfd.c7
-rw-r--r--virt/kvm/guest_memfd.c2
-rw-r--r--virt/kvm/irqchip.c6
-rw-r--r--virt/kvm/kvm_main.c34
-rw-r--r--virt/kvm/vfio.c4
6 files changed, 27 insertions, 29 deletions
diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c
index 375d6285475e..6b1d90161099 100644
--- a/virt/kvm/coalesced_mmio.c
+++ b/virt/kvm/coalesced_mmio.c
@@ -128,8 +128,7 @@ int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
if (zone->pio != 1 && zone->pio != 0)
return -EINVAL;
- dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev),
- GFP_KERNEL_ACCOUNT);
+ dev = kzalloc_obj(struct kvm_coalesced_mmio_dev, GFP_KERNEL_ACCOUNT);
if (!dev)
return -ENOMEM;
diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c
index a369b20d47f0..0e8b8a2c5b79 100644
--- a/virt/kvm/eventfd.c
+++ b/virt/kvm/eventfd.c
@@ -382,7 +382,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
if (!kvm_arch_irqfd_allowed(kvm, args))
return -EINVAL;
- irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL_ACCOUNT);
+ irqfd = kzalloc_obj(*irqfd, GFP_KERNEL_ACCOUNT);
if (!irqfd)
return -ENOMEM;
@@ -430,8 +430,7 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
}
if (!irqfd->resampler) {
- resampler = kzalloc(sizeof(*resampler),
- GFP_KERNEL_ACCOUNT);
+ resampler = kzalloc_obj(*resampler, GFP_KERNEL_ACCOUNT);
if (!resampler) {
ret = -ENOMEM;
mutex_unlock(&kvm->irqfds.resampler_lock);
@@ -874,7 +873,7 @@ static int kvm_assign_ioeventfd_idx(struct kvm *kvm,
if (IS_ERR(eventfd))
return PTR_ERR(eventfd);
- p = kzalloc(sizeof(*p), GFP_KERNEL_ACCOUNT);
+ p = kzalloc_obj(*p, GFP_KERNEL_ACCOUNT);
if (!p) {
ret = -ENOMEM;
goto fail;
diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c
index 923c51a3a525..e73339295a44 100644
--- a/virt/kvm/guest_memfd.c
+++ b/virt/kvm/guest_memfd.c
@@ -568,7 +568,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags)
if (fd < 0)
return fd;
- f = kzalloc(sizeof(*f), GFP_KERNEL);
+ f = kzalloc_obj(*f, GFP_KERNEL);
if (!f) {
err = -ENOMEM;
goto err_fd;
diff --git a/virt/kvm/irqchip.c b/virt/kvm/irqchip.c
index 6ccabfd32287..462c70621247 100644
--- a/virt/kvm/irqchip.c
+++ b/virt/kvm/irqchip.c
@@ -183,7 +183,7 @@ int kvm_set_irq_routing(struct kvm *kvm,
nr_rt_entries += 1;
- new = kzalloc(struct_size(new, map, nr_rt_entries), GFP_KERNEL_ACCOUNT);
+ new = kzalloc_flex(*new, map, nr_rt_entries, GFP_KERNEL_ACCOUNT);
if (!new)
return -ENOMEM;
@@ -194,7 +194,7 @@ int kvm_set_irq_routing(struct kvm *kvm,
for (i = 0; i < nr; ++i) {
r = -ENOMEM;
- e = kzalloc(sizeof(*e), GFP_KERNEL_ACCOUNT);
+ e = kzalloc_obj(*e, GFP_KERNEL_ACCOUNT);
if (!e)
goto out;
@@ -246,7 +246,7 @@ int kvm_init_irq_routing(struct kvm *kvm)
struct kvm_irq_routing_table *new;
int chip_size;
- new = kzalloc(struct_size(new, map, 1), GFP_KERNEL_ACCOUNT);
+ new = kzalloc_flex(*new, map, 1, GFP_KERNEL_ACCOUNT);
if (!new)
return -ENOMEM;
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 61dca8d37abc..b798903540b6 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1043,15 +1043,15 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
return 0;
kvm->debugfs_dentry = dent;
- kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
- sizeof(*kvm->debugfs_stat_data),
- GFP_KERNEL_ACCOUNT);
+ kvm->debugfs_stat_data = kzalloc_objs(*kvm->debugfs_stat_data,
+ kvm_debugfs_num_entries,
+ GFP_KERNEL_ACCOUNT);
if (!kvm->debugfs_stat_data)
goto out_err;
for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
pdesc = &kvm_vm_stats_desc[i];
- stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
+ stat_data = kzalloc_obj(*stat_data, GFP_KERNEL_ACCOUNT);
if (!stat_data)
goto out_err;
@@ -1066,7 +1066,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
for (i = 0; i < kvm_vcpu_stats_header.num_desc; ++i) {
pdesc = &kvm_vcpu_stats_desc[i];
- stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
+ stat_data = kzalloc_obj(*stat_data, GFP_KERNEL_ACCOUNT);
if (!stat_data)
goto out_err;
@@ -1185,7 +1185,7 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
r = -ENOMEM;
for (i = 0; i < KVM_NR_BUSES; i++) {
rcu_assign_pointer(kvm->buses[i],
- kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
+ kzalloc_obj(struct kvm_io_bus, GFP_KERNEL_ACCOUNT));
if (!kvm->buses[i])
goto out_err_no_arch_destroy_vm;
}
@@ -1944,7 +1944,7 @@ static int kvm_set_memslot(struct kvm *kvm,
* invalidation needs to be reverted.
*/
if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
- invalid_slot = kzalloc(sizeof(*invalid_slot), GFP_KERNEL_ACCOUNT);
+ invalid_slot = kzalloc_obj(*invalid_slot, GFP_KERNEL_ACCOUNT);
if (!invalid_slot) {
mutex_unlock(&kvm->slots_arch_lock);
return -ENOMEM;
@@ -2117,7 +2117,7 @@ static int kvm_set_memory_region(struct kvm *kvm,
return -EEXIST;
/* Allocate a slot that will persist in the memslot. */
- new = kzalloc(sizeof(*new), GFP_KERNEL_ACCOUNT);
+ new = kzalloc_obj(*new, GFP_KERNEL_ACCOUNT);
if (!new)
return -ENOMEM;
@@ -4505,7 +4505,7 @@ static long kvm_vcpu_ioctl(struct file *filp,
struct kvm_regs *kvm_regs;
r = -ENOMEM;
- kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL);
+ kvm_regs = kzalloc_obj(struct kvm_regs, GFP_KERNEL);
if (!kvm_regs)
goto out;
r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
@@ -4532,7 +4532,7 @@ out_free1:
break;
}
case KVM_GET_SREGS: {
- kvm_sregs = kzalloc(sizeof(struct kvm_sregs), GFP_KERNEL);
+ kvm_sregs = kzalloc_obj(struct kvm_sregs, GFP_KERNEL);
r = -ENOMEM;
if (!kvm_sregs)
goto out;
@@ -4624,7 +4624,7 @@ out_free1:
break;
}
case KVM_GET_FPU: {
- fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL);
+ fpu = kzalloc_obj(struct kvm_fpu, GFP_KERNEL);
r = -ENOMEM;
if (!fpu)
goto out;
@@ -4844,7 +4844,7 @@ static int kvm_ioctl_create_device(struct kvm *kvm,
if (test)
return 0;
- dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT);
+ dev = kzalloc_obj(*dev, GFP_KERNEL_ACCOUNT);
if (!dev)
return -ENOMEM;
@@ -6006,8 +6006,8 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
return -ENOSPC;
- new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1),
- GFP_KERNEL_ACCOUNT);
+ new_bus = kmalloc_flex(*bus, range, bus->dev_count + 1,
+ GFP_KERNEL_ACCOUNT);
if (!new_bus)
return -ENOMEM;
@@ -6053,8 +6053,8 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
if (i == bus->dev_count)
return 0;
- new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
- GFP_KERNEL_ACCOUNT);
+ new_bus = kmalloc_flex(*bus, range, bus->dev_count - 1,
+ GFP_KERNEL_ACCOUNT);
if (new_bus) {
memcpy(new_bus, bus, struct_size(bus, range, i));
new_bus->dev_count--;
@@ -6326,7 +6326,7 @@ static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
active = kvm_active_vms;
mutex_unlock(&kvm_lock);
- env = kzalloc(sizeof(*env), GFP_KERNEL);
+ env = kzalloc_obj(*env, GFP_KERNEL);
if (!env)
return;
diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c
index be50514bbd11..9f9acb66cc1e 100644
--- a/virt/kvm/vfio.c
+++ b/virt/kvm/vfio.c
@@ -166,7 +166,7 @@ static int kvm_vfio_file_add(struct kvm_device *dev, unsigned int fd)
}
}
- kvf = kzalloc(sizeof(*kvf), GFP_KERNEL_ACCOUNT);
+ kvf = kzalloc_obj(*kvf, GFP_KERNEL_ACCOUNT);
if (!kvf) {
ret = -ENOMEM;
goto out_unlock;
@@ -364,7 +364,7 @@ static int kvm_vfio_create(struct kvm_device *dev, u32 type)
if (tmp->ops == &kvm_vfio_ops)
return -EBUSY;
- kv = kzalloc(sizeof(*kv), GFP_KERNEL_ACCOUNT);
+ kv = kzalloc_obj(*kv, GFP_KERNEL_ACCOUNT);
if (!kv)
return -ENOMEM;