Commit 980da6ce573b7c40886406674ff8f022a975e65e
Committed by
Marcelo Tosatti
1 parent
50eb2a3cd0
Exists in
master
and in
39 other branches
KVM: Simplify coalesced mmio initialization
- add destructor function - move related allocation into constructor - add stubs for !CONFIG_KVM_MMIO Signed-off-by: Avi Kivity <avi@redhat.com>
Showing 3 changed files with 34 additions and 8 deletions Side-by-side Diff
virt/kvm/coalesced_mmio.c
... | ... | @@ -92,11 +92,19 @@ |
92 | 92 | int kvm_coalesced_mmio_init(struct kvm *kvm) |
93 | 93 | { |
94 | 94 | struct kvm_coalesced_mmio_dev *dev; |
95 | + struct page *page; | |
95 | 96 | int ret; |
96 | 97 | |
98 | + ret = -ENOMEM; | |
99 | + page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
100 | + if (!page) | |
101 | + goto out_err; | |
102 | + kvm->coalesced_mmio_ring = page_address(page); | |
103 | + | |
104 | + ret = -ENOMEM; | |
97 | 105 | dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL); |
98 | 106 | if (!dev) |
99 | - return -ENOMEM; | |
107 | + goto out_free_page; | |
100 | 108 | spin_lock_init(&dev->lock); |
101 | 109 | kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); |
102 | 110 | dev->kvm = kvm; |
103 | 111 | |
... | ... | @@ -104,9 +112,22 @@ |
104 | 112 | |
105 | 113 | ret = kvm_io_bus_register_dev(kvm, &kvm->mmio_bus, &dev->dev); |
106 | 114 | if (ret < 0) |
107 | - kfree(dev); | |
115 | + goto out_free_dev; | |
108 | 116 | |
109 | 117 | return ret; |
118 | + | |
119 | +out_free_dev: | |
120 | + kfree(dev); | |
121 | +out_free_page: | |
122 | + __free_page(page); | |
123 | +out_err: | |
124 | + return ret; | |
125 | +} | |
126 | + | |
127 | +void kvm_coalesced_mmio_free(struct kvm *kvm) | |
128 | +{ | |
129 | + if (kvm->coalesced_mmio_ring) | |
130 | + free_page((unsigned long)kvm->coalesced_mmio_ring); | |
110 | 131 | } |
111 | 132 | |
112 | 133 | int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, |
virt/kvm/coalesced_mmio.h
... | ... | @@ -10,6 +10,8 @@ |
10 | 10 | * |
11 | 11 | */ |
12 | 12 | |
13 | +#ifdef CONFIG_KVM_MMIO | |
14 | + | |
13 | 15 | #define KVM_COALESCED_MMIO_ZONE_MAX 100 |
14 | 16 | |
15 | 17 | struct kvm_coalesced_mmio_dev { |
16 | 18 | |
... | ... | @@ -21,10 +23,18 @@ |
21 | 23 | }; |
22 | 24 | |
23 | 25 | int kvm_coalesced_mmio_init(struct kvm *kvm); |
26 | +void kvm_coalesced_mmio_free(struct kvm *kvm); | |
24 | 27 | int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm, |
25 | 28 | struct kvm_coalesced_mmio_zone *zone); |
26 | 29 | int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm, |
27 | 30 | struct kvm_coalesced_mmio_zone *zone); |
31 | + | |
32 | +#else | |
33 | + | |
34 | +static inline int kvm_coalesced_mmio_init(struct kvm *kvm) { return 0; } | |
35 | +static inline void kvm_coalesced_mmio_free(struct kvm *kvm) { } | |
36 | + | |
37 | +#endif | |
28 | 38 | |
29 | 39 | #endif |
virt/kvm/kvm_main.c
... | ... | @@ -51,9 +51,7 @@ |
51 | 51 | #include <asm/pgtable.h> |
52 | 52 | #include <asm-generic/bitops/le.h> |
53 | 53 | |
54 | -#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
55 | 54 | #include "coalesced_mmio.h" |
56 | -#endif | |
57 | 55 | |
58 | 56 | #define CREATE_TRACE_POINTS |
59 | 57 | #include <trace/events/kvm.h> |
... | ... | @@ -468,10 +466,7 @@ |
468 | 466 | kvm_free_irq_routing(kvm); |
469 | 467 | kvm_io_bus_destroy(&kvm->pio_bus); |
470 | 468 | kvm_io_bus_destroy(&kvm->mmio_bus); |
471 | -#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET | |
472 | - if (kvm->coalesced_mmio_ring != NULL) | |
473 | - free_page((unsigned long)kvm->coalesced_mmio_ring); | |
474 | -#endif | |
469 | + kvm_coalesced_mmio_free(kvm); | |
475 | 470 | #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER) |
476 | 471 | mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm); |
477 | 472 | #else |