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