nbd: fix outdated qapi docs syntax for tls-creds
[qemu/ar7.git] / hw / virtio / virtio-balloon.c
blobe3a65940ef4c909a4e3cff5556c1dfcfc9b1d6c4
1 /*
2 * Virtio Balloon Device
4 * Copyright IBM, Corp. 2008
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
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 "exec/address-spaces.h"
25 #include "qapi/error.h"
26 #include "qapi/qapi-events-misc.h"
27 #include "qapi/visitor.h"
28 #include "trace.h"
29 #include "qemu/error-report.h"
30 #include "migration/misc.h"
32 #include "hw/virtio/virtio-bus.h"
33 #include "hw/virtio/virtio-access.h"
35 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)
37 struct PartiallyBalloonedPage {
38 RAMBlock *rb;
39 ram_addr_t base;
40 unsigned long bitmap[];
43 static void balloon_inflate_page(VirtIOBalloon *balloon,
44 MemoryRegion *mr, hwaddr offset)
46 void *addr = memory_region_get_ram_ptr(mr) + offset;
47 RAMBlock *rb;
48 size_t rb_page_size;
49 int subpages;
50 ram_addr_t ram_offset, host_page_base;
52 /* XXX is there a better way to get to the RAMBlock than via a
53 * host address? */
54 rb = qemu_ram_block_from_host(addr, false, &ram_offset);
55 rb_page_size = qemu_ram_pagesize(rb);
56 host_page_base = ram_offset & ~(rb_page_size - 1);
58 if (rb_page_size == BALLOON_PAGE_SIZE) {
59 /* Easy case */
61 ram_block_discard_range(rb, ram_offset, rb_page_size);
62 /* We ignore errors from ram_block_discard_range(), because it
63 * has already reported them, and failing to discard a balloon
64 * page is not fatal */
65 return;
68 /* Hard case
70 * We've put a piece of a larger host page into the balloon - we
71 * need to keep track until we have a whole host page to
72 * discard
74 warn_report_once(
75 "Balloon used with backing page size > 4kiB, this may not be reliable");
77 subpages = rb_page_size / BALLOON_PAGE_SIZE;
79 if (balloon->pbp
80 && (rb != balloon->pbp->rb
81 || host_page_base != balloon->pbp->base)) {
82 /* We've partially ballooned part of a host page, but now
83 * we're trying to balloon part of a different one. Too hard,
84 * give up on the old partial page */
85 free(balloon->pbp);
86 balloon->pbp = NULL;
89 if (!balloon->pbp) {
90 /* Starting on a new host page */
91 size_t bitlen = BITS_TO_LONGS(subpages) * sizeof(unsigned long);
92 balloon->pbp = g_malloc0(sizeof(PartiallyBalloonedPage) + bitlen);
93 balloon->pbp->rb = rb;
94 balloon->pbp->base = host_page_base;
97 bitmap_set(balloon->pbp->bitmap,
98 (ram_offset - balloon->pbp->base) / BALLOON_PAGE_SIZE,
99 subpages);
101 if (bitmap_full(balloon->pbp->bitmap, subpages)) {
102 /* We've accumulated a full host page, we can actually discard
103 * it now */
105 ram_block_discard_range(rb, balloon->pbp->base, rb_page_size);
106 /* We ignore errors from ram_block_discard_range(), because it
107 * has already reported them, and failing to discard a balloon
108 * page is not fatal */
110 free(balloon->pbp);
111 balloon->pbp = NULL;
115 static const char *balloon_stat_names[] = {
116 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
117 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
118 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
119 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
120 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
121 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
122 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
123 [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches",
124 [VIRTIO_BALLOON_S_HTLB_PGALLOC] = "stat-htlb-pgalloc",
125 [VIRTIO_BALLOON_S_HTLB_PGFAIL] = "stat-htlb-pgfail",
126 [VIRTIO_BALLOON_S_NR] = NULL
130 * reset_stats - Mark all items in the stats array as unset
132 * This function needs to be called at device initialization and before
133 * updating to a set of newly-generated stats. This will ensure that no
134 * stale values stick around in case the guest reports a subset of the supported
135 * statistics.
137 static inline void reset_stats(VirtIOBalloon *dev)
139 int i;
140 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
143 static bool balloon_stats_supported(const VirtIOBalloon *s)
145 VirtIODevice *vdev = VIRTIO_DEVICE(s);
146 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
149 static bool balloon_stats_enabled(const VirtIOBalloon *s)
151 return s->stats_poll_interval > 0;
154 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
156 if (balloon_stats_enabled(s)) {
157 timer_del(s->stats_timer);
158 timer_free(s->stats_timer);
159 s->stats_timer = NULL;
160 s->stats_poll_interval = 0;
164 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
166 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
169 static void balloon_stats_poll_cb(void *opaque)
171 VirtIOBalloon *s = opaque;
172 VirtIODevice *vdev = VIRTIO_DEVICE(s);
174 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
175 /* re-schedule */
176 balloon_stats_change_timer(s, s->stats_poll_interval);
177 return;
180 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
181 virtio_notify(vdev, s->svq);
182 g_free(s->stats_vq_elem);
183 s->stats_vq_elem = NULL;
186 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
187 void *opaque, Error **errp)
189 Error *err = NULL;
190 VirtIOBalloon *s = opaque;
191 int i;
193 visit_start_struct(v, name, NULL, 0, &err);
194 if (err) {
195 goto out;
197 visit_type_int(v, "last-update", &s->stats_last_update, &err);
198 if (err) {
199 goto out_end;
202 visit_start_struct(v, "stats", NULL, 0, &err);
203 if (err) {
204 goto out_end;
206 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
207 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
208 if (err) {
209 goto out_nested;
212 visit_check_struct(v, &err);
213 out_nested:
214 visit_end_struct(v, NULL);
216 if (!err) {
217 visit_check_struct(v, &err);
219 out_end:
220 visit_end_struct(v, NULL);
221 out:
222 error_propagate(errp, err);
225 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
226 const char *name, void *opaque,
227 Error **errp)
229 VirtIOBalloon *s = opaque;
230 visit_type_int(v, name, &s->stats_poll_interval, errp);
233 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
234 const char *name, void *opaque,
235 Error **errp)
237 VirtIOBalloon *s = opaque;
238 Error *local_err = NULL;
239 int64_t value;
241 visit_type_int(v, name, &value, &local_err);
242 if (local_err) {
243 error_propagate(errp, local_err);
244 return;
247 if (value < 0) {
248 error_setg(errp, "timer value must be greater than zero");
249 return;
252 if (value > UINT32_MAX) {
253 error_setg(errp, "timer value is too big");
254 return;
257 if (value == s->stats_poll_interval) {
258 return;
261 if (value == 0) {
262 /* timer=0 disables the timer */
263 balloon_stats_destroy_timer(s);
264 return;
267 if (balloon_stats_enabled(s)) {
268 /* timer interval change */
269 s->stats_poll_interval = value;
270 balloon_stats_change_timer(s, value);
271 return;
274 /* create a new timer */
275 g_assert(s->stats_timer == NULL);
276 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
277 s->stats_poll_interval = value;
278 balloon_stats_change_timer(s, 0);
281 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
283 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
284 VirtQueueElement *elem;
285 MemoryRegionSection section;
287 for (;;) {
288 size_t offset = 0;
289 uint32_t pfn;
290 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
291 if (!elem) {
292 return;
295 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
296 hwaddr pa;
297 int p = virtio_ldl_p(vdev, &pfn);
299 pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT;
300 offset += 4;
302 section = memory_region_find(get_system_memory(), pa,
303 BALLOON_PAGE_SIZE);
304 if (!section.mr) {
305 trace_virtio_balloon_bad_addr(pa);
306 continue;
308 if (!memory_region_is_ram(section.mr) ||
309 memory_region_is_rom(section.mr) ||
310 memory_region_is_romd(section.mr)) {
311 trace_virtio_balloon_bad_addr(pa);
312 memory_region_unref(section.mr);
313 continue;
316 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
317 pa);
318 if (!qemu_balloon_is_inhibited() && vq != s->dvq) {
319 balloon_inflate_page(s, section.mr, section.offset_within_region);
321 memory_region_unref(section.mr);
324 virtqueue_push(vq, elem, offset);
325 virtio_notify(vdev, vq);
326 g_free(elem);
330 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
332 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
333 VirtQueueElement *elem;
334 VirtIOBalloonStat stat;
335 size_t offset = 0;
336 qemu_timeval tv;
338 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
339 if (!elem) {
340 goto out;
343 if (s->stats_vq_elem != NULL) {
344 /* This should never happen if the driver follows the spec. */
345 virtqueue_push(vq, s->stats_vq_elem, 0);
346 virtio_notify(vdev, vq);
347 g_free(s->stats_vq_elem);
350 s->stats_vq_elem = elem;
352 /* Initialize the stats to get rid of any stale values. This is only
353 * needed to handle the case where a guest supports fewer stats than it
354 * used to (ie. it has booted into an old kernel).
356 reset_stats(s);
358 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
359 == sizeof(stat)) {
360 uint16_t tag = virtio_tswap16(vdev, stat.tag);
361 uint64_t val = virtio_tswap64(vdev, stat.val);
363 offset += sizeof(stat);
364 if (tag < VIRTIO_BALLOON_S_NR)
365 s->stats[tag] = val;
367 s->stats_vq_offset = offset;
369 if (qemu_gettimeofday(&tv) < 0) {
370 warn_report("%s: failed to get time of day", __func__);
371 goto out;
374 s->stats_last_update = tv.tv_sec;
376 out:
377 if (balloon_stats_enabled(s)) {
378 balloon_stats_change_timer(s, s->stats_poll_interval);
382 static void virtio_balloon_handle_free_page_vq(VirtIODevice *vdev,
383 VirtQueue *vq)
385 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
386 qemu_bh_schedule(s->free_page_bh);
389 static bool get_free_page_hints(VirtIOBalloon *dev)
391 VirtQueueElement *elem;
392 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
393 VirtQueue *vq = dev->free_page_vq;
395 while (dev->block_iothread) {
396 qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
399 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
400 if (!elem) {
401 return false;
404 if (elem->out_num) {
405 uint32_t id;
406 size_t size = iov_to_buf(elem->out_sg, elem->out_num, 0,
407 &id, sizeof(id));
408 virtqueue_push(vq, elem, size);
409 g_free(elem);
411 virtio_tswap32s(vdev, &id);
412 if (unlikely(size != sizeof(id))) {
413 virtio_error(vdev, "received an incorrect cmd id");
414 return false;
416 if (id == dev->free_page_report_cmd_id) {
417 dev->free_page_report_status = FREE_PAGE_REPORT_S_START;
418 } else {
420 * Stop the optimization only when it has started. This
421 * avoids a stale stop sign for the previous command.
423 if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
424 dev->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
429 if (elem->in_num) {
430 if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
431 qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
432 elem->in_sg[0].iov_len);
434 virtqueue_push(vq, elem, 1);
435 g_free(elem);
438 return true;
441 static void virtio_ballloon_get_free_page_hints(void *opaque)
443 VirtIOBalloon *dev = opaque;
444 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
445 VirtQueue *vq = dev->free_page_vq;
446 bool continue_to_get_hints;
448 do {
449 qemu_mutex_lock(&dev->free_page_lock);
450 virtio_queue_set_notification(vq, 0);
451 continue_to_get_hints = get_free_page_hints(dev);
452 qemu_mutex_unlock(&dev->free_page_lock);
453 virtio_notify(vdev, vq);
455 * Start to poll the vq once the reporting started. Otherwise, continue
456 * only when there are entries on the vq, which need to be given back.
458 } while (continue_to_get_hints ||
459 dev->free_page_report_status == FREE_PAGE_REPORT_S_START);
460 virtio_queue_set_notification(vq, 1);
463 static bool virtio_balloon_free_page_support(void *opaque)
465 VirtIOBalloon *s = opaque;
466 VirtIODevice *vdev = VIRTIO_DEVICE(s);
468 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT);
471 static void virtio_balloon_free_page_start(VirtIOBalloon *s)
473 VirtIODevice *vdev = VIRTIO_DEVICE(s);
475 /* For the stop and copy phase, we don't need to start the optimization */
476 if (!vdev->vm_running) {
477 return;
480 if (s->free_page_report_cmd_id == UINT_MAX) {
481 s->free_page_report_cmd_id =
482 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
483 } else {
484 s->free_page_report_cmd_id++;
487 s->free_page_report_status = FREE_PAGE_REPORT_S_REQUESTED;
488 virtio_notify_config(vdev);
491 static void virtio_balloon_free_page_stop(VirtIOBalloon *s)
493 VirtIODevice *vdev = VIRTIO_DEVICE(s);
495 if (s->free_page_report_status != FREE_PAGE_REPORT_S_STOP) {
497 * The lock also guarantees us that the
498 * virtio_ballloon_get_free_page_hints exits after the
499 * free_page_report_status is set to S_STOP.
501 qemu_mutex_lock(&s->free_page_lock);
503 * The guest hasn't done the reporting, so host sends a notification
504 * to the guest to actively stop the reporting.
506 s->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
507 qemu_mutex_unlock(&s->free_page_lock);
508 virtio_notify_config(vdev);
512 static void virtio_balloon_free_page_done(VirtIOBalloon *s)
514 VirtIODevice *vdev = VIRTIO_DEVICE(s);
516 s->free_page_report_status = FREE_PAGE_REPORT_S_DONE;
517 virtio_notify_config(vdev);
520 static int
521 virtio_balloon_free_page_report_notify(NotifierWithReturn *n, void *data)
523 VirtIOBalloon *dev = container_of(n, VirtIOBalloon,
524 free_page_report_notify);
525 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
526 PrecopyNotifyData *pnd = data;
528 if (!virtio_balloon_free_page_support(dev)) {
530 * This is an optimization provided to migration, so just return 0 to
531 * have the normal migration process not affected when this feature is
532 * not supported.
534 return 0;
537 switch (pnd->reason) {
538 case PRECOPY_NOTIFY_SETUP:
539 precopy_enable_free_page_optimization();
540 break;
541 case PRECOPY_NOTIFY_COMPLETE:
542 case PRECOPY_NOTIFY_CLEANUP:
543 case PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC:
544 virtio_balloon_free_page_stop(dev);
545 break;
546 case PRECOPY_NOTIFY_AFTER_BITMAP_SYNC:
547 if (vdev->vm_running) {
548 virtio_balloon_free_page_start(dev);
549 } else {
550 virtio_balloon_free_page_done(dev);
552 break;
553 default:
554 virtio_error(vdev, "%s: %d reason unknown", __func__, pnd->reason);
557 return 0;
560 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
562 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
563 struct virtio_balloon_config config = {};
565 config.num_pages = cpu_to_le32(dev->num_pages);
566 config.actual = cpu_to_le32(dev->actual);
568 if (dev->free_page_report_status == FREE_PAGE_REPORT_S_REQUESTED) {
569 config.free_page_report_cmd_id =
570 cpu_to_le32(dev->free_page_report_cmd_id);
571 } else if (dev->free_page_report_status == FREE_PAGE_REPORT_S_STOP) {
572 config.free_page_report_cmd_id =
573 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_STOP);
574 } else if (dev->free_page_report_status == FREE_PAGE_REPORT_S_DONE) {
575 config.free_page_report_cmd_id =
576 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_DONE);
579 trace_virtio_balloon_get_config(config.num_pages, config.actual);
580 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
583 static int build_dimm_list(Object *obj, void *opaque)
585 GSList **list = opaque;
587 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
588 DeviceState *dev = DEVICE(obj);
589 if (dev->realized) { /* only realized DIMMs matter */
590 *list = g_slist_prepend(*list, dev);
594 object_child_foreach(obj, build_dimm_list, opaque);
595 return 0;
598 static ram_addr_t get_current_ram_size(void)
600 GSList *list = NULL, *item;
601 ram_addr_t size = ram_size;
603 build_dimm_list(qdev_get_machine(), &list);
604 for (item = list; item; item = g_slist_next(item)) {
605 Object *obj = OBJECT(item->data);
606 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
607 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
608 &error_abort);
611 g_slist_free(list);
613 return size;
616 static void virtio_balloon_set_config(VirtIODevice *vdev,
617 const uint8_t *config_data)
619 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
620 struct virtio_balloon_config config;
621 uint32_t oldactual = dev->actual;
622 ram_addr_t vm_ram_size = get_current_ram_size();
624 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
625 dev->actual = le32_to_cpu(config.actual);
626 if (dev->actual != oldactual) {
627 qapi_event_send_balloon_change(vm_ram_size -
628 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
630 trace_virtio_balloon_set_config(dev->actual, oldactual);
633 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
634 Error **errp)
636 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
637 f |= dev->host_features;
638 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
640 return f;
643 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
645 VirtIOBalloon *dev = opaque;
646 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
647 VIRTIO_BALLOON_PFN_SHIFT);
650 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
652 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
653 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
654 ram_addr_t vm_ram_size = get_current_ram_size();
656 if (target > vm_ram_size) {
657 target = vm_ram_size;
659 if (target) {
660 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
661 virtio_notify_config(vdev);
663 trace_virtio_balloon_to_target(target, dev->num_pages);
666 static int virtio_balloon_post_load_device(void *opaque, int version_id)
668 VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
670 if (balloon_stats_enabled(s)) {
671 balloon_stats_change_timer(s, s->stats_poll_interval);
673 return 0;
676 static const VMStateDescription vmstate_virtio_balloon_free_page_report = {
677 .name = "virtio-balloon-device/free-page-report",
678 .version_id = 1,
679 .minimum_version_id = 1,
680 .needed = virtio_balloon_free_page_support,
681 .fields = (VMStateField[]) {
682 VMSTATE_UINT32(free_page_report_cmd_id, VirtIOBalloon),
683 VMSTATE_UINT32(free_page_report_status, VirtIOBalloon),
684 VMSTATE_END_OF_LIST()
688 static const VMStateDescription vmstate_virtio_balloon_device = {
689 .name = "virtio-balloon-device",
690 .version_id = 1,
691 .minimum_version_id = 1,
692 .post_load = virtio_balloon_post_load_device,
693 .fields = (VMStateField[]) {
694 VMSTATE_UINT32(num_pages, VirtIOBalloon),
695 VMSTATE_UINT32(actual, VirtIOBalloon),
696 VMSTATE_END_OF_LIST()
698 .subsections = (const VMStateDescription * []) {
699 &vmstate_virtio_balloon_free_page_report,
700 NULL
704 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
706 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
707 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
708 int ret;
710 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
711 sizeof(struct virtio_balloon_config));
713 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
714 virtio_balloon_stat, s);
716 if (ret < 0) {
717 error_setg(errp, "Only one balloon device is supported");
718 virtio_cleanup(vdev);
719 return;
722 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
723 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
724 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
726 if (virtio_has_feature(s->host_features,
727 VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
728 s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
729 virtio_balloon_handle_free_page_vq);
730 s->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
731 s->free_page_report_cmd_id =
732 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
733 s->free_page_report_notify.notify =
734 virtio_balloon_free_page_report_notify;
735 precopy_add_notifier(&s->free_page_report_notify);
736 if (s->iothread) {
737 object_ref(OBJECT(s->iothread));
738 s->free_page_bh = aio_bh_new(iothread_get_aio_context(s->iothread),
739 virtio_ballloon_get_free_page_hints, s);
740 qemu_mutex_init(&s->free_page_lock);
741 qemu_cond_init(&s->free_page_cond);
742 s->block_iothread = false;
743 } else {
744 /* Simply disable this feature if the iothread wasn't created. */
745 s->host_features &= ~(1 << VIRTIO_BALLOON_F_FREE_PAGE_HINT);
746 virtio_error(vdev, "iothread is missing");
749 reset_stats(s);
752 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
754 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
755 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
757 if (virtio_balloon_free_page_support(s)) {
758 qemu_bh_delete(s->free_page_bh);
759 virtio_balloon_free_page_stop(s);
760 precopy_remove_notifier(&s->free_page_report_notify);
762 balloon_stats_destroy_timer(s);
763 qemu_remove_balloon_handler(s);
764 virtio_cleanup(vdev);
767 static void virtio_balloon_device_reset(VirtIODevice *vdev)
769 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
771 if (virtio_balloon_free_page_support(s)) {
772 virtio_balloon_free_page_stop(s);
775 if (s->stats_vq_elem != NULL) {
776 virtqueue_unpop(s->svq, s->stats_vq_elem, 0);
777 g_free(s->stats_vq_elem);
778 s->stats_vq_elem = NULL;
782 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
784 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
786 if (!s->stats_vq_elem && vdev->vm_running &&
787 (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) {
788 /* poll stats queue for the element we have discarded when the VM
789 * was stopped */
790 virtio_balloon_receive_stats(vdev, s->svq);
793 if (virtio_balloon_free_page_support(s)) {
795 * The VM is woken up and the iothread was blocked, so signal it to
796 * continue.
798 if (vdev->vm_running && s->block_iothread) {
799 qemu_mutex_lock(&s->free_page_lock);
800 s->block_iothread = false;
801 qemu_cond_signal(&s->free_page_cond);
802 qemu_mutex_unlock(&s->free_page_lock);
805 /* The VM is stopped, block the iothread. */
806 if (!vdev->vm_running) {
807 qemu_mutex_lock(&s->free_page_lock);
808 s->block_iothread = true;
809 qemu_mutex_unlock(&s->free_page_lock);
814 static void virtio_balloon_instance_init(Object *obj)
816 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
818 object_property_add(obj, "guest-stats", "guest statistics",
819 balloon_stats_get_all, NULL, NULL, s, NULL);
821 object_property_add(obj, "guest-stats-polling-interval", "int",
822 balloon_stats_get_poll_interval,
823 balloon_stats_set_poll_interval,
824 NULL, s, NULL);
827 static const VMStateDescription vmstate_virtio_balloon = {
828 .name = "virtio-balloon",
829 .minimum_version_id = 1,
830 .version_id = 1,
831 .fields = (VMStateField[]) {
832 VMSTATE_VIRTIO_DEVICE,
833 VMSTATE_END_OF_LIST()
837 static Property virtio_balloon_properties[] = {
838 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
839 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
840 DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon, host_features,
841 VIRTIO_BALLOON_F_FREE_PAGE_HINT, false),
842 DEFINE_PROP_LINK("iothread", VirtIOBalloon, iothread, TYPE_IOTHREAD,
843 IOThread *),
844 DEFINE_PROP_END_OF_LIST(),
847 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
849 DeviceClass *dc = DEVICE_CLASS(klass);
850 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
852 dc->props = virtio_balloon_properties;
853 dc->vmsd = &vmstate_virtio_balloon;
854 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
855 vdc->realize = virtio_balloon_device_realize;
856 vdc->unrealize = virtio_balloon_device_unrealize;
857 vdc->reset = virtio_balloon_device_reset;
858 vdc->get_config = virtio_balloon_get_config;
859 vdc->set_config = virtio_balloon_set_config;
860 vdc->get_features = virtio_balloon_get_features;
861 vdc->set_status = virtio_balloon_set_status;
862 vdc->vmsd = &vmstate_virtio_balloon_device;
865 static const TypeInfo virtio_balloon_info = {
866 .name = TYPE_VIRTIO_BALLOON,
867 .parent = TYPE_VIRTIO_DEVICE,
868 .instance_size = sizeof(VirtIOBalloon),
869 .instance_init = virtio_balloon_instance_init,
870 .class_init = virtio_balloon_class_init,
873 static void virtio_register_types(void)
875 type_register_static(&virtio_balloon_info);
878 type_init(virtio_register_types)