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