Skip to content

Commit b94e91c

Browse files
Igor Mammedovsashalevin
authored andcommitted
kvm: avoid page allocation failure in kvm_set_memory_region()
[ Upstream commit 7449613 ] 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> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent 48ca7d7 commit b94e91c

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

‎virt/kvm/kvm_main.c‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
478478
BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
479479

480480
r = -ENOMEM;
481-
kvm->memslots = kzalloc(sizeof(struct kvm_memslots), GFP_KERNEL);
481+
kvm->memslots = kvm_kvzalloc(sizeof(struct kvm_memslots));
482482
if (!kvm->memslots)
483483
goto out_err_no_srcu;
484484

@@ -529,7 +529,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
529529
out_err_no_disable:
530530
for (i = 0; i < KVM_NR_BUSES; i++)
531531
kfree(kvm->buses[i]);
532-
kfree(kvm->memslots);
532+
kvfree(kvm->memslots);
533533
kvm_arch_free_vm(kvm);
534534
return ERR_PTR(r);
535535
}
@@ -585,7 +585,7 @@ static void kvm_free_physmem(struct kvm *kvm)
585585
kvm_for_each_memslot(memslot, slots)
586586
kvm_free_physmem_slot(kvm, memslot, NULL);
587587

588-
kfree(kvm->memslots);
588+
kvfree(kvm->memslots);
589589
}
590590

591591
static void kvm_destroy_devices(struct kvm *kvm)
@@ -867,10 +867,11 @@ int __kvm_set_memory_region(struct kvm *kvm,
867867
}
868868

869869
if ((change == KVM_MR_DELETE) || (change == KVM_MR_MOVE)) {
870-
slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
871-
GFP_KERNEL);
870+
slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
872871
if (!slots)
873872
goto out_free;
873+
memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
874+
874875
slot = id_to_memslot(slots, mem->slot);
875876
slot->flags |= KVM_MEMSLOT_INVALID;
876877

@@ -900,10 +901,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
900901
* will get overwritten by update_memslots anyway.
901902
*/
902903
if (!slots) {
903-
slots = kmemdup(kvm->memslots, sizeof(struct kvm_memslots),
904-
GFP_KERNEL);
904+
slots = kvm_kvzalloc(sizeof(struct kvm_memslots));
905905
if (!slots)
906906
goto out_free;
907+
memcpy(slots, kvm->memslots, sizeof(struct kvm_memslots));
907908
}
908909

909910
/* actual memory is freed via old in kvm_free_physmem_slot below */
@@ -917,7 +918,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
917918
kvm_arch_commit_memory_region(kvm, mem, &old, change);
918919

919920
kvm_free_physmem_slot(kvm, &old, &new);
920-
kfree(old_memslots);
921+
kvfree(old_memslots);
921922

922923
/*
923924
* IOMMU mapping: New slots need to be mapped. Old slots need to be
@@ -936,7 +937,7 @@ int __kvm_set_memory_region(struct kvm *kvm,
936937
return 0;
937938

938939
out_slots:
939-
kfree(slots);
940+
kvfree(slots);
940941
out_free:
941942
kvm_free_physmem_slot(kvm, &new, &old);
942943
out:

0 commit comments

Comments
 (0)