Skip to content

Commit

Permalink
KVM: irqchip: Use struct_size() in kzalloc()
Browse files Browse the repository at this point in the history
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct foo {
   int stuff;
   struct boo entry[];
};

instance = kzalloc(sizeof(struct foo) + count * sizeof(struct boo), GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
GustavoARSilva authored and bonzini committed Jun 5, 2019
1 parent 5a25355 commit b3ffd74
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions virt/kvm/irqchip.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ int kvm_set_irq_routing(struct kvm *kvm,

nr_rt_entries += 1;

new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head)),
GFP_KERNEL_ACCOUNT);

new = kzalloc(struct_size(new, map, nr_rt_entries), GFP_KERNEL_ACCOUNT);
if (!new)
return -ENOMEM;

Expand Down

0 comments on commit b3ffd74

Please sign in to comment.