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