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