| 1 | |
| 2 | |
| 3 | @@ -11,3 +11,8 @@ |
| 4 | |
| 5 | bool whpx_allowed; |
| 6 | bool whpx_irqchip_in_kernel; |
| 7 | + |
| 8 | +int whpx_balloon_mapping(bool add, void *host, uint64_t guest, size_t size) |
| 9 | +{ |
| 10 | + return -ENOSYS; |
| 11 | +} |
| 12 | |
| 13 | |
| 14 | @@ -284,6 +284,25 @@ |
| 15 | * Memory support. |
| 16 | */ |
| 17 | |
| 18 | +int whpx_balloon_mapping(bool add, void *host, uint64_t guest, size_t size) |
| 19 | +{ |
| 20 | + HRESULT hr; |
| 21 | + |
| 22 | + if (add) { |
| 23 | + hr = whp_dispatch.WHvMapGpaRange(whpx_global.partition, host, guest, size, |
| 24 | + WHvMapGpaRangeFlagRead | WHvMapGpaRangeFlagWrite | |
| 25 | + WHvMapGpaRangeFlagExecute); |
| 26 | + } else { |
| 27 | + hr = whp_dispatch.WHvUnmapGpaRange(whpx_global.partition, guest, size); |
| 28 | + } |
| 29 | + if (FAILED(hr)) { |
| 30 | + error_report_once("WHPX balloon %s failed (0x%08lx)", |
| 31 | + add ? "map" : "unmap", (unsigned long)hr); |
| 32 | + return -EIO; |
| 33 | + } |
| 34 | + return 0; |
| 35 | +} |
| 36 | + |
| 37 | static void whpx_set_phys_mem(MemoryRegionSection *section, bool add) |
| 38 | { |
| 39 | struct whpx_state *whpx = &whpx_global; |
| 40 | |
| 41 | |
| 42 | @@ -18,6 +18,8 @@ |
| 43 | #include "qemu/module.h" |
| 44 | #include "qemu/timer.h" |
| 45 | #include "qemu/madvise.h" |
| 46 | +#include "qemu/main-loop.h" |
| 47 | +#include "system/whpx.h" |
| 48 | #include "hw/virtio/virtio.h" |
| 49 | #include "hw/mem/pc-dimm.h" |
| 50 | #include "hw/core/qdev-properties.h" |
| 51 | @@ -76,9 +78,114 @@ |
| 52 | migration_in_bg_snapshot(); |
| 53 | } |
| 54 | |
| 55 | +typedef struct BalloonDiscardBatch BalloonDiscardBatch; |
| 56 | + |
| 57 | +#ifdef _WIN32 |
| 58 | +typedef struct BalloonDiscardRange { |
| 59 | + RAMBlock *rb; |
| 60 | + MemoryRegion *mr; |
| 61 | + ram_addr_t offset; |
| 62 | + hwaddr guest; |
| 63 | + size_t size; |
| 64 | +} BalloonDiscardRange; |
| 65 | + |
| 66 | +struct BalloonDiscardBatch { |
| 67 | + BalloonDiscardRange ranges[256]; |
| 68 | + size_t count; |
| 69 | +}; |
| 70 | + |
| 71 | +static void balloon_windows_discard(RAMBlock *rb, ram_addr_t offset, |
| 72 | + hwaddr guest, size_t size) |
| 73 | +{ |
| 74 | + void *addr = qemu_ram_get_host_addr(rb) + offset; |
| 75 | + |
| 76 | + /* WHPX must release its backing before Windows can reclaim these pages. */ |
| 77 | + if (whpx_enabled() && whpx_balloon_mapping(false, addr, guest, size) < 0) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if (!VirtualFree(addr, size, MEM_DECOMMIT)) { |
| 81 | + error_report_once("Could not decommit Windows balloon pages (error %lu)", |
| 82 | + (unsigned long)GetLastError()); |
| 83 | + } else if (!VirtualAlloc(addr, size, MEM_COMMIT, PAGE_READWRITE)) { |
| 84 | + /* RAMBlock users require accessible host addresses even while the guest |
| 85 | + * lends these pages to the balloon. Windows allocates backing on touch. */ |
| 86 | + error_report("Could not recommit Windows balloon pages (error %lu)", |
| 87 | + (unsigned long)GetLastError()); |
| 88 | + exit(1); |
| 89 | + } |
| 90 | + if (whpx_enabled() && whpx_balloon_mapping(true, addr, guest, size) < 0) { |
| 91 | + /* Resume only after the guest can access the recommitted range. */ |
| 92 | + exit(1); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +static int balloon_discard_range_compare(const void *left, const void *right) |
| 97 | +{ |
| 98 | + const BalloonDiscardRange *a = left; |
| 99 | + const BalloonDiscardRange *b = right; |
| 100 | + |
| 101 | + if (a->rb != b->rb) { |
| 102 | + return (uintptr_t)a->rb < (uintptr_t)b->rb ? -1 : 1; |
| 103 | + } |
| 104 | + return (a->offset > b->offset) - (a->offset < b->offset); |
| 105 | +} |
| 106 | + |
| 107 | +static void balloon_discard_batch_flush(BalloonDiscardBatch *batch) |
| 108 | +{ |
| 109 | + size_t i = 0; |
| 110 | + |
| 111 | + qsort(batch->ranges, batch->count, sizeof(batch->ranges[0]), |
| 112 | + balloon_discard_range_compare); |
| 113 | + while (i < batch->count) { |
| 114 | + BalloonDiscardRange *first = &batch->ranges[i++]; |
| 115 | + ram_addr_t end = first->offset + first->size; |
| 116 | + |
| 117 | + while (i < batch->count && batch->ranges[i].rb == first->rb && |
| 118 | + batch->ranges[i].offset <= end && |
| 119 | + batch->ranges[i].guest == first->guest + |
| 120 | + batch->ranges[i].offset - first->offset) { |
| 121 | + end = MAX(end, batch->ranges[i].offset + batch->ranges[i].size); |
| 122 | + i++; |
| 123 | + } |
| 124 | + balloon_windows_discard(first->rb, first->offset, first->guest, |
| 125 | + end - first->offset); |
| 126 | + } |
| 127 | + for (i = 0; i < batch->count; i++) { |
| 128 | + memory_region_unref(batch->ranges[i].mr); |
| 129 | + } |
| 130 | + batch->count = 0; |
| 131 | +} |
| 132 | +#endif |
| 133 | + |
| 134 | +/* Balloon and free-page reporting hand over unused guest pages. Their |
| 135 | + * contents are unspecified when the guest takes them back. */ |
| 136 | +static void balloon_discard_range(RAMBlock *rb, ram_addr_t offset, size_t size, |
| 137 | + MemoryRegion *mr, BalloonDiscardBatch *batch, |
| 138 | + hwaddr guest) |
| 139 | +{ |
| 140 | +#ifdef _WIN32 |
| 141 | + /* Batch adjacent pages to reduce Windows memory operations. Retain each region |
| 142 | + * until the batch is complete, before acknowledging the virtqueue entry. */ |
| 143 | + if (batch) { |
| 144 | + if (batch->count == ARRAY_SIZE(batch->ranges)) { |
| 145 | + balloon_discard_batch_flush(batch); |
| 146 | + } |
| 147 | + memory_region_ref(mr); |
| 148 | + batch->ranges[batch->count++] = (BalloonDiscardRange) { |
| 149 | + .rb = rb, .mr = mr, .offset = offset, .guest = guest, .size = size, |
| 150 | + }; |
| 151 | + } else { |
| 152 | + balloon_windows_discard(rb, offset, guest, size); |
| 153 | + } |
| 154 | +#else |
| 155 | + ram_block_discard_range(rb, offset, size); |
| 156 | +#endif |
| 157 | +} |
| 158 | + |
| 159 | static void balloon_inflate_page(VirtIOBalloon *balloon, |
| 160 | MemoryRegion *mr, hwaddr mr_offset, |
| 161 | - PartiallyBalloonedPage *pbp) |
| 162 | + PartiallyBalloonedPage *pbp, |
| 163 | + BalloonDiscardBatch *batch, hwaddr guest) |
| 164 | { |
| 165 | void *addr = memory_region_get_ram_ptr(mr) + mr_offset; |
| 166 | ram_addr_t rb_offset, rb_aligned_offset, base_gpa; |
| 167 | @@ -94,7 +201,7 @@ |
| 168 | if (rb_page_size == BALLOON_PAGE_SIZE) { |
| 169 | /* Easy case */ |
| 170 | |
| 171 | - ram_block_discard_range(rb, rb_offset, rb_page_size); |
| 172 | + balloon_discard_range(rb, rb_offset, rb_page_size, mr, batch, guest); |
| 173 | /* We ignore errors from ram_block_discard_range(), because it |
| 174 | * has already reported them, and failing to discard a balloon |
| 175 | * page is not fatal */ |
| 176 | @@ -133,7 +240,8 @@ |
| 177 | /* We've accumulated a full host page, we can actually discard |
| 178 | * it now */ |
| 179 | |
| 180 | - ram_block_discard_range(rb, rb_aligned_offset, rb_page_size); |
| 181 | + balloon_discard_range(rb, rb_aligned_offset, rb_page_size, mr, batch, |
| 182 | + guest - (rb_offset - rb_aligned_offset)); |
| 183 | /* We ignore errors from ram_block_discard_range(), because it |
| 184 | * has already reported them, and failing to discard a balloon |
| 185 | * page is not fatal */ |
| 186 | @@ -144,6 +252,7 @@ |
| 187 | static void balloon_deflate_page(VirtIOBalloon *balloon, |
| 188 | MemoryRegion *mr, hwaddr mr_offset) |
| 189 | { |
| 190 | +#ifndef _WIN32 |
| 191 | void *addr = memory_region_get_ram_ptr(mr) + mr_offset; |
| 192 | ram_addr_t rb_offset; |
| 193 | RAMBlock *rb; |
| 194 | @@ -166,6 +275,8 @@ |
| 195 | strerror(errno)); |
| 196 | /* Otherwise ignore, failing to page hint shouldn't be fatal */ |
| 197 | } |
| 198 | +#endif |
| 199 | + /* Windows recommits and remaps during inflation. Backing is demand-zero. */ |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | @@ -389,7 +500,7 @@ |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | - ram_block_discard_range(rb, ram_offset, size); |
| 208 | + balloon_discard_range(rb, ram_offset, size, NULL, NULL, elem->in_addr[i]); |
| 209 | } |
| 210 | |
| 211 | skip_element: |
| 212 | @@ -407,6 +518,12 @@ |
| 213 | |
| 214 | for (;;) { |
| 215 | PartiallyBalloonedPage pbp = {}; |
| 216 | +#ifdef _WIN32 |
| 217 | + BalloonDiscardBatch batch = {}; |
| 218 | + BalloonDiscardBatch *discard_batch = &batch; |
| 219 | +#else |
| 220 | + BalloonDiscardBatch *discard_batch = NULL; |
| 221 | +#endif |
| 222 | size_t offset = 0; |
| 223 | uint32_t pfn; |
| 224 | |
| 225 | @@ -441,7 +558,9 @@ |
| 226 | if (!virtio_balloon_inhibited()) { |
| 227 | if (vq == s->ivq) { |
| 228 | balloon_inflate_page(s, section.mr, |
| 229 | - section.offset_within_region, &pbp); |
| 230 | + section.offset_within_region, &pbp, |
| 231 | + discard_batch, |
| 232 | + section.offset_within_address_space); |
| 233 | } else if (vq == s->dvq) { |
| 234 | balloon_deflate_page(s, section.mr, section.offset_within_region); |
| 235 | } else { |
| 236 | @@ -451,12 +570,31 @@ |
| 237 | memory_region_unref(section.mr); |
| 238 | } |
| 239 | |
| 240 | +#ifdef _WIN32 |
| 241 | + balloon_discard_batch_flush(&batch); |
| 242 | +#endif |
| 243 | virtqueue_push(vq, elem, 0); |
| 244 | virtio_notify(vdev, vq); |
| 245 | g_free(elem); |
| 246 | virtio_balloon_pbp_free(&pbp); |
| 247 | - } |
| 248 | -} |
| 249 | +#ifdef _WIN32 |
| 250 | + /* Let network and control I/O run between memory batches. */ |
| 251 | + if (vq == s->ivq) { |
| 252 | + qemu_bh_schedule(s->balloon_bh); |
| 253 | + break; |
| 254 | + } |
| 255 | +#endif |
| 256 | + } |
| 257 | +} |
| 258 | + |
| 259 | +#ifdef _WIN32 |
| 260 | +static void virtio_balloon_memory_bh(void *opaque) |
| 261 | +{ |
| 262 | + VirtIOBalloon *s = opaque; |
| 263 | + |
| 264 | + virtio_balloon_handle_output(VIRTIO_DEVICE(s), s->ivq); |
| 265 | +} |
| 266 | +#endif |
| 267 | |
| 268 | static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) |
| 269 | { |
| 270 | @@ -887,6 +1025,10 @@ |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | +#ifdef _WIN32 |
| 275 | + s->balloon_bh = qemu_bh_new_guarded(virtio_balloon_memory_bh, s, |
| 276 | + &dev->mem_reentrancy_guard); |
| 277 | +#endif |
| 278 | s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); |
| 279 | s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); |
| 280 | s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); |
| 281 | @@ -918,6 +1060,9 @@ |
| 282 | VirtIOBalloon *s = VIRTIO_BALLOON(dev); |
| 283 | |
| 284 | qemu_unregister_resettable(OBJECT(dev)); |
| 285 | +#ifdef _WIN32 |
| 286 | + qemu_bh_delete(s->balloon_bh); |
| 287 | +#endif |
| 288 | if (s->free_page_bh) { |
| 289 | qemu_bh_delete(s->free_page_bh); |
| 290 | object_unref(OBJECT(s->iothread)); |
| 291 | @@ -942,6 +1087,9 @@ |
| 292 | static void virtio_balloon_device_reset(VirtIODevice *vdev) |
| 293 | { |
| 294 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
| 295 | +#ifdef _WIN32 |
| 296 | + qemu_bh_cancel(s->balloon_bh); |
| 297 | +#endif |
| 298 | |
| 299 | if (virtio_balloon_free_page_support(s)) { |
| 300 | virtio_balloon_free_page_stop(s); |
| 301 | |
| 302 | |
| 303 | @@ -54,6 +54,9 @@ |
| 304 | QEMUTimer *stats_timer; |
| 305 | IOThread *iothread; |
| 306 | QEMUBH *free_page_bh; |
| 307 | +#ifdef _WIN32 |
| 308 | + QEMUBH *balloon_bh; |
| 309 | +#endif |
| 310 | /* |
| 311 | * Lock to synchronize threads to access the free page reporting related |
| 312 | * fields (e.g. free_page_hint_status). |
| 313 | |
| 314 | |
| 315 | @@ -24,6 +24,7 @@ |
| 316 | #endif /* COMPILING_PER_TARGET */ |
| 317 | |
| 318 | #ifdef CONFIG_WHPX_IS_POSSIBLE |
| 319 | +int whpx_balloon_mapping(bool add, void *host, uint64_t guest, size_t size); |
| 320 | extern bool whpx_allowed; |
| 321 | extern bool whpx_irqchip_in_kernel; |
| 322 | #define whpx_enabled() (whpx_allowed) |