1 /* 2 * Virtio Balloon Device 3 * 4 * Copyright IBM, Corp. 2008 5 * Copyright (C) 2011 Red Hat, Inc. 6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com> 7 * 8 * Authors: 9 * Anthony Liguori <aliguori@us.ibm.com> 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2. See 12 * the COPYING file in the top-level directory. 13 * 14 */ 15 16 #include "qemu/osdep.h" 17 #include "qemu/iov.h" 18 #include "qemu/timer.h" 19 #include "qemu-common.h" 20 #include "hw/virtio/virtio.h" 21 #include "hw/mem/pc-dimm.h" 22 #include "sysemu/balloon.h" 23 #include "hw/virtio/virtio-balloon.h" 24 #include "exec/address-spaces.h" 25 #include "qapi/error.h" 26 #include "qapi/qapi-events-misc.h" 27 #include "qapi/visitor.h" 28 #include "trace.h" 29 #include "qemu/error-report.h" 30 31 #include "hw/virtio/virtio-bus.h" 32 #include "hw/virtio/virtio-access.h" 33 34 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT) 35 36 static void balloon_inflate_page(VirtIOBalloon *balloon, 37 MemoryRegion *mr, hwaddr offset) 38 { 39 void *addr = memory_region_get_ram_ptr(mr) + offset; 40 RAMBlock *rb; 41 size_t rb_page_size; 42 ram_addr_t ram_offset; 43 44 /* XXX is there a better way to get to the RAMBlock than via a 45 * host address? */ 46 rb = qemu_ram_block_from_host(addr, false, &ram_offset); 47 rb_page_size = qemu_ram_pagesize(rb); 48 49 /* Silently ignore hugepage RAM blocks */ 50 if (rb_page_size != getpagesize()) { 51 return; 52 } 53 54 /* Silently ignore unaligned requests */ 55 if (ram_offset & (rb_page_size - 1)) { 56 return; 57 } 58 59 ram_block_discard_range(rb, ram_offset, rb_page_size); 60 /* We ignore errors from ram_block_discard_range(), because it has 61 * already reported them, and failing to discard a balloon page is 62 * not fatal */ 63 } 64 65 static const char *balloon_stat_names[] = { 66 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in", 67 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out", 68 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults", 69 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults", 70 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory", 71 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory", 72 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory", 73 [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches", 74 [VIRTIO_BALLOON_S_HTLB_PGALLOC] = "stat-htlb-pgalloc", 75 [VIRTIO_BALLOON_S_HTLB_PGFAIL] = "stat-htlb-pgfail", 76 [VIRTIO_BALLOON_S_NR] = NULL 77 }; 78 79 /* 80 * reset_stats - Mark all items in the stats array as unset 81 * 82 * This function needs to be called at device initialization and before 83 * updating to a set of newly-generated stats. This will ensure that no 84 * stale values stick around in case the guest reports a subset of the supported 85 * statistics. 86 */ 87 static inline void reset_stats(VirtIOBalloon *dev) 88 { 89 int i; 90 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1); 91 } 92 93 static bool balloon_stats_supported(const VirtIOBalloon *s) 94 { 95 VirtIODevice *vdev = VIRTIO_DEVICE(s); 96 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ); 97 } 98 99 static bool balloon_stats_enabled(const VirtIOBalloon *s) 100 { 101 return s->stats_poll_interval > 0; 102 } 103 104 static void balloon_stats_destroy_timer(VirtIOBalloon *s) 105 { 106 if (balloon_stats_enabled(s)) { 107 timer_del(s->stats_timer); 108 timer_free(s->stats_timer); 109 s->stats_timer = NULL; 110 s->stats_poll_interval = 0; 111 } 112 } 113 114 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs) 115 { 116 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000); 117 } 118 119 static void balloon_stats_poll_cb(void *opaque) 120 { 121 VirtIOBalloon *s = opaque; 122 VirtIODevice *vdev = VIRTIO_DEVICE(s); 123 124 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) { 125 /* re-schedule */ 126 balloon_stats_change_timer(s, s->stats_poll_interval); 127 return; 128 } 129 130 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset); 131 virtio_notify(vdev, s->svq); 132 g_free(s->stats_vq_elem); 133 s->stats_vq_elem = NULL; 134 } 135 136 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name, 137 void *opaque, Error **errp) 138 { 139 Error *err = NULL; 140 VirtIOBalloon *s = opaque; 141 int i; 142 143 visit_start_struct(v, name, NULL, 0, &err); 144 if (err) { 145 goto out; 146 } 147 visit_type_int(v, "last-update", &s->stats_last_update, &err); 148 if (err) { 149 goto out_end; 150 } 151 152 visit_start_struct(v, "stats", NULL, 0, &err); 153 if (err) { 154 goto out_end; 155 } 156 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) { 157 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err); 158 if (err) { 159 goto out_nested; 160 } 161 } 162 visit_check_struct(v, &err); 163 out_nested: 164 visit_end_struct(v, NULL); 165 166 if (!err) { 167 visit_check_struct(v, &err); 168 } 169 out_end: 170 visit_end_struct(v, NULL); 171 out: 172 error_propagate(errp, err); 173 } 174 175 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v, 176 const char *name, void *opaque, 177 Error **errp) 178 { 179 VirtIOBalloon *s = opaque; 180 visit_type_int(v, name, &s->stats_poll_interval, errp); 181 } 182 183 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v, 184 const char *name, void *opaque, 185 Error **errp) 186 { 187 VirtIOBalloon *s = opaque; 188 Error *local_err = NULL; 189 int64_t value; 190 191 visit_type_int(v, name, &value, &local_err); 192 if (local_err) { 193 error_propagate(errp, local_err); 194 return; 195 } 196 197 if (value < 0) { 198 error_setg(errp, "timer value must be greater than zero"); 199 return; 200 } 201 202 if (value > UINT32_MAX) { 203 error_setg(errp, "timer value is too big"); 204 return; 205 } 206 207 if (value == s->stats_poll_interval) { 208 return; 209 } 210 211 if (value == 0) { 212 /* timer=0 disables the timer */ 213 balloon_stats_destroy_timer(s); 214 return; 215 } 216 217 if (balloon_stats_enabled(s)) { 218 /* timer interval change */ 219 s->stats_poll_interval = value; 220 balloon_stats_change_timer(s, value); 221 return; 222 } 223 224 /* create a new timer */ 225 g_assert(s->stats_timer == NULL); 226 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s); 227 s->stats_poll_interval = value; 228 balloon_stats_change_timer(s, 0); 229 } 230 231 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) 232 { 233 VirtIOBalloon *s = VIRTIO_BALLOON(vdev); 234 VirtQueueElement *elem; 235 MemoryRegionSection section; 236 237 for (;;) { 238 size_t offset = 0; 239 uint32_t pfn; 240 elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 241 if (!elem) { 242 return; 243 } 244 245 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) { 246 hwaddr pa; 247 int p = virtio_ldl_p(vdev, &pfn); 248 249 pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT; 250 offset += 4; 251 252 section = memory_region_find(get_system_memory(), pa, 253 BALLOON_PAGE_SIZE); 254 if (!section.mr) { 255 trace_virtio_balloon_bad_addr(pa); 256 continue; 257 } 258 if (!memory_region_is_ram(section.mr) || 259 memory_region_is_rom(section.mr) || 260 memory_region_is_romd(section.mr)) { 261 trace_virtio_balloon_bad_addr(pa); 262 memory_region_unref(section.mr); 263 continue; 264 } 265 266 trace_virtio_balloon_handle_output(memory_region_name(section.mr), 267 pa); 268 if (!qemu_balloon_is_inhibited() && vq != s->dvq) { 269 balloon_inflate_page(s, section.mr, section.offset_within_region); 270 } 271 memory_region_unref(section.mr); 272 } 273 274 virtqueue_push(vq, elem, offset); 275 virtio_notify(vdev, vq); 276 g_free(elem); 277 } 278 } 279 280 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) 281 { 282 VirtIOBalloon *s = VIRTIO_BALLOON(vdev); 283 VirtQueueElement *elem; 284 VirtIOBalloonStat stat; 285 size_t offset = 0; 286 qemu_timeval tv; 287 288 elem = virtqueue_pop(vq, sizeof(VirtQueueElement)); 289 if (!elem) { 290 goto out; 291 } 292 293 if (s->stats_vq_elem != NULL) { 294 /* This should never happen if the driver follows the spec. */ 295 virtqueue_push(vq, s->stats_vq_elem, 0); 296 virtio_notify(vdev, vq); 297 g_free(s->stats_vq_elem); 298 } 299 300 s->stats_vq_elem = elem; 301 302 /* Initialize the stats to get rid of any stale values. This is only 303 * needed to handle the case where a guest supports fewer stats than it 304 * used to (ie. it has booted into an old kernel). 305 */ 306 reset_stats(s); 307 308 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat)) 309 == sizeof(stat)) { 310 uint16_t tag = virtio_tswap16(vdev, stat.tag); 311 uint64_t val = virtio_tswap64(vdev, stat.val); 312 313 offset += sizeof(stat); 314 if (tag < VIRTIO_BALLOON_S_NR) 315 s->stats[tag] = val; 316 } 317 s->stats_vq_offset = offset; 318 319 if (qemu_gettimeofday(&tv) < 0) { 320 warn_report("%s: failed to get time of day", __func__); 321 goto out; 322 } 323 324 s->stats_last_update = tv.tv_sec; 325 326 out: 327 if (balloon_stats_enabled(s)) { 328 balloon_stats_change_timer(s, s->stats_poll_interval); 329 } 330 } 331 332 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) 333 { 334 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); 335 struct virtio_balloon_config config = {}; 336 337 config.num_pages = cpu_to_le32(dev->num_pages); 338 config.actual = cpu_to_le32(dev->actual); 339 340 trace_virtio_balloon_get_config(config.num_pages, config.actual); 341 memcpy(config_data, &config, sizeof(struct virtio_balloon_config)); 342 } 343 344 static int build_dimm_list(Object *obj, void *opaque) 345 { 346 GSList **list = opaque; 347 348 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) { 349 DeviceState *dev = DEVICE(obj); 350 if (dev->realized) { /* only realized DIMMs matter */ 351 *list = g_slist_prepend(*list, dev); 352 } 353 } 354 355 object_child_foreach(obj, build_dimm_list, opaque); 356 return 0; 357 } 358 359 static ram_addr_t get_current_ram_size(void) 360 { 361 GSList *list = NULL, *item; 362 ram_addr_t size = ram_size; 363 364 build_dimm_list(qdev_get_machine(), &list); 365 for (item = list; item; item = g_slist_next(item)) { 366 Object *obj = OBJECT(item->data); 367 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) { 368 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP, 369 &error_abort); 370 } 371 } 372 g_slist_free(list); 373 374 return size; 375 } 376 377 static void virtio_balloon_set_config(VirtIODevice *vdev, 378 const uint8_t *config_data) 379 { 380 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); 381 struct virtio_balloon_config config; 382 uint32_t oldactual = dev->actual; 383 ram_addr_t vm_ram_size = get_current_ram_size(); 384 385 memcpy(&config, config_data, sizeof(struct virtio_balloon_config)); 386 dev->actual = le32_to_cpu(config.actual); 387 if (dev->actual != oldactual) { 388 qapi_event_send_balloon_change(vm_ram_size - 389 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT)); 390 } 391 trace_virtio_balloon_set_config(dev->actual, oldactual); 392 } 393 394 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f, 395 Error **errp) 396 { 397 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); 398 f |= dev->host_features; 399 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ); 400 return f; 401 } 402 403 static void virtio_balloon_stat(void *opaque, BalloonInfo *info) 404 { 405 VirtIOBalloon *dev = opaque; 406 info->actual = get_current_ram_size() - ((uint64_t) dev->actual << 407 VIRTIO_BALLOON_PFN_SHIFT); 408 } 409 410 static void virtio_balloon_to_target(void *opaque, ram_addr_t target) 411 { 412 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque); 413 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 414 ram_addr_t vm_ram_size = get_current_ram_size(); 415 416 if (target > vm_ram_size) { 417 target = vm_ram_size; 418 } 419 if (target) { 420 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT; 421 virtio_notify_config(vdev); 422 } 423 trace_virtio_balloon_to_target(target, dev->num_pages); 424 } 425 426 static int virtio_balloon_post_load_device(void *opaque, int version_id) 427 { 428 VirtIOBalloon *s = VIRTIO_BALLOON(opaque); 429 430 if (balloon_stats_enabled(s)) { 431 balloon_stats_change_timer(s, s->stats_poll_interval); 432 } 433 return 0; 434 } 435 436 static const VMStateDescription vmstate_virtio_balloon_device = { 437 .name = "virtio-balloon-device", 438 .version_id = 1, 439 .minimum_version_id = 1, 440 .post_load = virtio_balloon_post_load_device, 441 .fields = (VMStateField[]) { 442 VMSTATE_UINT32(num_pages, VirtIOBalloon), 443 VMSTATE_UINT32(actual, VirtIOBalloon), 444 VMSTATE_END_OF_LIST() 445 }, 446 }; 447 448 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp) 449 { 450 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 451 VirtIOBalloon *s = VIRTIO_BALLOON(dev); 452 int ret; 453 454 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON, 455 sizeof(struct virtio_balloon_config)); 456 457 ret = qemu_add_balloon_handler(virtio_balloon_to_target, 458 virtio_balloon_stat, s); 459 460 if (ret < 0) { 461 error_setg(errp, "Only one balloon device is supported"); 462 virtio_cleanup(vdev); 463 return; 464 } 465 466 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); 467 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); 468 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); 469 470 reset_stats(s); 471 } 472 473 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp) 474 { 475 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 476 VirtIOBalloon *s = VIRTIO_BALLOON(dev); 477 478 balloon_stats_destroy_timer(s); 479 qemu_remove_balloon_handler(s); 480 virtio_cleanup(vdev); 481 } 482 483 static void virtio_balloon_device_reset(VirtIODevice *vdev) 484 { 485 VirtIOBalloon *s = VIRTIO_BALLOON(vdev); 486 487 if (s->stats_vq_elem != NULL) { 488 virtqueue_unpop(s->svq, s->stats_vq_elem, 0); 489 g_free(s->stats_vq_elem); 490 s->stats_vq_elem = NULL; 491 } 492 } 493 494 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status) 495 { 496 VirtIOBalloon *s = VIRTIO_BALLOON(vdev); 497 498 if (!s->stats_vq_elem && vdev->vm_running && 499 (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) { 500 /* poll stats queue for the element we have discarded when the VM 501 * was stopped */ 502 virtio_balloon_receive_stats(vdev, s->svq); 503 } 504 } 505 506 static void virtio_balloon_instance_init(Object *obj) 507 { 508 VirtIOBalloon *s = VIRTIO_BALLOON(obj); 509 510 object_property_add(obj, "guest-stats", "guest statistics", 511 balloon_stats_get_all, NULL, NULL, s, NULL); 512 513 object_property_add(obj, "guest-stats-polling-interval", "int", 514 balloon_stats_get_poll_interval, 515 balloon_stats_set_poll_interval, 516 NULL, s, NULL); 517 } 518 519 static const VMStateDescription vmstate_virtio_balloon = { 520 .name = "virtio-balloon", 521 .minimum_version_id = 1, 522 .version_id = 1, 523 .fields = (VMStateField[]) { 524 VMSTATE_VIRTIO_DEVICE, 525 VMSTATE_END_OF_LIST() 526 }, 527 }; 528 529 static Property virtio_balloon_properties[] = { 530 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features, 531 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false), 532 DEFINE_PROP_END_OF_LIST(), 533 }; 534 535 static void virtio_balloon_class_init(ObjectClass *klass, void *data) 536 { 537 DeviceClass *dc = DEVICE_CLASS(klass); 538 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 539 540 dc->props = virtio_balloon_properties; 541 dc->vmsd = &vmstate_virtio_balloon; 542 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 543 vdc->realize = virtio_balloon_device_realize; 544 vdc->unrealize = virtio_balloon_device_unrealize; 545 vdc->reset = virtio_balloon_device_reset; 546 vdc->get_config = virtio_balloon_get_config; 547 vdc->set_config = virtio_balloon_set_config; 548 vdc->get_features = virtio_balloon_get_features; 549 vdc->set_status = virtio_balloon_set_status; 550 vdc->vmsd = &vmstate_virtio_balloon_device; 551 } 552 553 static const TypeInfo virtio_balloon_info = { 554 .name = TYPE_VIRTIO_BALLOON, 555 .parent = TYPE_VIRTIO_DEVICE, 556 .instance_size = sizeof(VirtIOBalloon), 557 .instance_init = virtio_balloon_instance_init, 558 .class_init = virtio_balloon_class_init, 559 }; 560 561 static void virtio_register_types(void) 562 { 563 type_register_static(&virtio_balloon_info); 564 } 565 566 type_init(virtio_register_types) 567