KVM: Use kmemdup() instead of kmalloc/memcpy
authorSasha Levin <levinsasha928@gmail.com>
Sun, 4 Dec 2011 17:36:28 +0000 (19:36 +0200)
committerAvi Kivity <avi@redhat.com>
Tue, 27 Dec 2011 09:22:20 +0000 (11:22 +0200)
Switch to kmemdup() in two places to shorten the code and avoid possible bugs.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/x86.c
virt/kvm/kvm_main.c

index d55a94f1155a28389731b1682c29a15bfadbc22a..03042d60a8fcca745c5076c12edfc453e979ec4f 100644 (file)
@@ -3031,10 +3031,10 @@ int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
                memset(dirty_bitmap_head, 0, n);
 
                r = -ENOMEM;
-               slots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
+               slots = kmemdup(kvm->memslots, sizeof(*kvm->memslots), GFP_KERNEL);
                if (!slots)
                        goto out;
-               memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
+
                memslot = id_to_memslot(slots, log->slot);
                memslot->nr_dirty_pages = 0;
                memslot->dirty_bitmap = dirty_bitmap_head;
index e289486edc6d98c1a85919476f8afa94f6025dad..a6e612fced737b034c61f90a4ea22fbd87a956da 100644 (file)
@@ -2591,13 +2591,12 @@ int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
        int i, r;
        struct kvm_io_bus *new_bus, *bus;
 
-       new_bus = kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL);
+       bus = kvm->buses[bus_idx];
+
+       new_bus = kmemdup(bus, sizeof(*bus), GFP_KERNEL);
        if (!new_bus)
                return -ENOMEM;
 
-       bus = kvm->buses[bus_idx];
-       memcpy(new_bus, bus, sizeof(struct kvm_io_bus));
-
        r = -ENOENT;
        for (i = 0; i < new_bus->dev_count; i++)
                if (new_bus->range[i].dev == dev) {