Skip to content

Commit 7449613

Browse files
Igor Mammedovmatosatti
authored andcommitted
kvm: avoid page allocation failure in kvm_set_memory_region()
KVM guest can fail to startup with following trace on host: qemu-system-x86: page allocation failure: order:4, mode:0x40d0 Call Trace: dump_stack+0x47/0x67 warn_alloc_failed+0xee/0x150 __alloc_pages_direct_compact+0x14a/0x150 __alloc_pages_nodemask+0x776/0xb80 alloc_kmem_pages+0x3a/0x110 kmalloc_order+0x13/0x50 kmemdup+0x1b/0x40 __kvm_set_memory_region+0x24a/0x9f0 [kvm] kvm_set_ioapic+0x130/0x130 [kvm] kvm_set_memory_region+0x21/0x40 [kvm] kvm_vm_ioctl+0x43f/0x750 [kvm] Failure happens when attempting to allocate pages for 'struct kvm_memslots', however it doesn't have to be present in physically contiguous (kmalloc-ed) address space, change allocation to kvm_kvzalloc() so that it will be vmalloc-ed when its size is more then a page. Signed-off-by: Igor Mammedov <imammedo@redhat.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent c806a6a commit 7449613

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

virt/kvm/kvm_main.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
471471
BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
472472

473473
r = -ENOMEM;
474-
kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
474+
kvm->memslots = kvm_kvzalloc(sizeof(struct kvm_memslots));
475475
if (!kvm->memslots)
476476
goto out_err_no_srcu;
477477

@@ -522,7 +522,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
522522
out_err_no_disable:
523523
for (i = 0; i < KVM_NR_BUSES; i++)
524524
kfree(kvm->buses[i]);
525-
kfree(kvm->memslots);
525+
kvfree(kvm->memslots);
526526
kvm_arch_free_vm(kvm);
527527
return ERR_PTR(r);
528528
}
@@ -578,7 +578,7 @@ static void kvm_free_physmem(struct kvm *kvm)
578578
kvm_for_each_memslot(memslot, slots)
579579
kvm_free_physmem_slot(kvm, memslot, NULL);
580580

581-
kfree(kvm->memslots);
581+
kvfree(kvm->memslots);
582582
}
583583

584584
static void kvm_destroy_devices(struct kvm *kvm)
@@ -871,10 +871,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
871871
goto out_free;
872872
}
873873

874-
slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
875-
GFP_KERNEL);
874+
slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
876875
if (!slots)
877876
goto out_free;
877+
memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
878878

879879
if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
880880
slot = id_to_memslot(slots, mem->slot);
@@ -917,7 +917,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
917917
kvm_arch_commit_memory_region(kvm, mem, &old, change);
918918

919919
kvm_free_physmem_slot(kvm, &old, &new);
920-
kfree(old_memslots);
920+
kvfree(old_memslots);
921921

922922
/*
923923
* IOMMU mapping: New slots need to be mapped. Old slots need to be
@@ -936,7 +936,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
936936
return 0;
937937

938938
out_slots:
939-
kfree(slots);
939+
kvfree(slots);
940940
out_free:
941941
kvm_free_physmem_slot(kvm, &new, &old);
942942
out:

0 commit comments

Comments
 (0)