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