diff options
-rw-r--r-- | virt/kvm/kvm_main.c | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 7e140683ff14..1148c96a4817 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c | |||
@@ -516,16 +516,32 @@ out_err_nodisable: | |||
516 | return ERR_PTR(r); | 516 | return ERR_PTR(r); |
517 | } | 517 | } |
518 | 518 | ||
519 | /* | ||
520 | * Avoid using vmalloc for a small buffer. | ||
521 | * Should not be used when the size is statically known. | ||
522 | */ | ||
523 | static void *kvm_kvzalloc(unsigned long size) | ||
524 | { | ||
525 | if (size > PAGE_SIZE) | ||
526 | return vzalloc(size); | ||
527 | else | ||
528 | return kzalloc(size, GFP_KERNEL); | ||
529 | } | ||
530 | |||
531 | static void kvm_kvfree(const void *addr) | ||
532 | { | ||
533 | if (is_vmalloc_addr(addr)) | ||
534 | vfree(addr); | ||
535 | else | ||
536 | kfree(addr); | ||
537 | } | ||
538 | |||
519 | static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) | 539 | static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) |
520 | { | 540 | { |
521 | if (!memslot->dirty_bitmap) | 541 | if (!memslot->dirty_bitmap) |
522 | return; | 542 | return; |
523 | 543 | ||
524 | if (2 * kvm_dirty_bitmap_bytes(memslot) > PAGE_SIZE) | 544 | kvm_kvfree(memslot->dirty_bitmap); |
525 | vfree(memslot->dirty_bitmap); | ||
526 | else | ||
527 | kfree(memslot->dirty_bitmap); | ||
528 | |||
529 | memslot->dirty_bitmap = NULL; | 545 | memslot->dirty_bitmap = NULL; |
530 | } | 546 | } |
531 | 547 | ||
@@ -617,11 +633,7 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) | |||
617 | #ifndef CONFIG_S390 | 633 | #ifndef CONFIG_S390 |
618 | unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); | 634 | unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot); |
619 | 635 | ||
620 | if (dirty_bytes > PAGE_SIZE) | 636 | memslot->dirty_bitmap = kvm_kvzalloc(dirty_bytes); |
621 | memslot->dirty_bitmap = vzalloc(dirty_bytes); | ||
622 | else | ||
623 | memslot->dirty_bitmap = kzalloc(dirty_bytes, GFP_KERNEL); | ||
624 | |||
625 | if (!memslot->dirty_bitmap) | 637 | if (!memslot->dirty_bitmap) |
626 | return -ENOMEM; | 638 | return -ENOMEM; |
627 | 639 | ||