util/hbitmap: update orig_size on truncate
[qemu/ar7.git] / hw / virtio / virtio-balloon.c
blob25de15430710065d7a8da3c2b6b2531f1b64b235
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/module.h"
19 #include "qemu/timer.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 typedef struct PartiallyBalloonedPage {
38 ram_addr_t base_gpa;
39 unsigned long *bitmap;
40 } PartiallyBalloonedPage;
42 static void virtio_balloon_pbp_free(PartiallyBalloonedPage *pbp)
44 if (!pbp->bitmap) {
45 return;
47 g_free(pbp->bitmap);
48 pbp->bitmap = NULL;
51 static void virtio_balloon_pbp_alloc(PartiallyBalloonedPage *pbp,
52 ram_addr_t base_gpa,
53 long subpages)
55 pbp->base_gpa = base_gpa;
56 pbp->bitmap = bitmap_new(subpages);
59 static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp,
60 ram_addr_t base_gpa)
62 return pbp->base_gpa == base_gpa;
65 static void balloon_inflate_page(VirtIOBalloon *balloon,
66 MemoryRegion *mr, hwaddr mr_offset,
67 PartiallyBalloonedPage *pbp)
69 void *addr = memory_region_get_ram_ptr(mr) + mr_offset;
70 ram_addr_t rb_offset, rb_aligned_offset, base_gpa;
71 RAMBlock *rb;
72 size_t rb_page_size;
73 int subpages;
75 /* XXX is there a better way to get to the RAMBlock than via a
76 * host address? */
77 rb = qemu_ram_block_from_host(addr, false, &rb_offset);
78 rb_page_size = qemu_ram_pagesize(rb);
80 if (rb_page_size == BALLOON_PAGE_SIZE) {
81 /* Easy case */
83 ram_block_discard_range(rb, rb_offset, rb_page_size);
84 /* We ignore errors from ram_block_discard_range(), because it
85 * has already reported them, and failing to discard a balloon
86 * page is not fatal */
87 return;
90 /* Hard case
92 * We've put a piece of a larger host page into the balloon - we
93 * need to keep track until we have a whole host page to
94 * discard
96 warn_report_once(
97 "Balloon used with backing page size > 4kiB, this may not be reliable");
99 rb_aligned_offset = QEMU_ALIGN_DOWN(rb_offset, rb_page_size);
100 subpages = rb_page_size / BALLOON_PAGE_SIZE;
101 base_gpa = memory_region_get_ram_addr(mr) + mr_offset -
102 (rb_offset - rb_aligned_offset);
104 if (pbp->bitmap && !virtio_balloon_pbp_matches(pbp, base_gpa)) {
105 /* We've partially ballooned part of a host page, but now
106 * we're trying to balloon part of a different one. Too hard,
107 * give up on the old partial page */
108 virtio_balloon_pbp_free(pbp);
111 if (!pbp->bitmap) {
112 virtio_balloon_pbp_alloc(pbp, base_gpa, subpages);
115 set_bit((rb_offset - rb_aligned_offset) / BALLOON_PAGE_SIZE,
116 pbp->bitmap);
118 if (bitmap_full(pbp->bitmap, subpages)) {
119 /* We've accumulated a full host page, we can actually discard
120 * it now */
122 ram_block_discard_range(rb, rb_aligned_offset, rb_page_size);
123 /* We ignore errors from ram_block_discard_range(), because it
124 * has already reported them, and failing to discard a balloon
125 * page is not fatal */
126 virtio_balloon_pbp_free(pbp);
130 static void balloon_deflate_page(VirtIOBalloon *balloon,
131 MemoryRegion *mr, hwaddr mr_offset)
133 void *addr = memory_region_get_ram_ptr(mr) + mr_offset;
134 ram_addr_t rb_offset;
135 RAMBlock *rb;
136 size_t rb_page_size;
137 void *host_addr;
138 int ret;
140 /* XXX is there a better way to get to the RAMBlock than via a
141 * host address? */
142 rb = qemu_ram_block_from_host(addr, false, &rb_offset);
143 rb_page_size = qemu_ram_pagesize(rb);
145 host_addr = (void *)((uintptr_t)addr & ~(rb_page_size - 1));
147 /* When a page is deflated, we hint the whole host page it lives
148 * on, since we can't do anything smaller */
149 ret = qemu_madvise(host_addr, rb_page_size, QEMU_MADV_WILLNEED);
150 if (ret != 0) {
151 warn_report("Couldn't MADV_WILLNEED on balloon deflate: %s",
152 strerror(errno));
153 /* Otherwise ignore, failing to page hint shouldn't be fatal */
157 static const char *balloon_stat_names[] = {
158 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
159 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
160 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
161 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
162 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
163 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
164 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
165 [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches",
166 [VIRTIO_BALLOON_S_HTLB_PGALLOC] = "stat-htlb-pgalloc",
167 [VIRTIO_BALLOON_S_HTLB_PGFAIL] = "stat-htlb-pgfail",
168 [VIRTIO_BALLOON_S_NR] = NULL
172 * reset_stats - Mark all items in the stats array as unset
174 * This function needs to be called at device initialization and before
175 * updating to a set of newly-generated stats. This will ensure that no
176 * stale values stick around in case the guest reports a subset of the supported
177 * statistics.
179 static inline void reset_stats(VirtIOBalloon *dev)
181 int i;
182 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
185 static bool balloon_stats_supported(const VirtIOBalloon *s)
187 VirtIODevice *vdev = VIRTIO_DEVICE(s);
188 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
191 static bool balloon_stats_enabled(const VirtIOBalloon *s)
193 return s->stats_poll_interval > 0;
196 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
198 if (balloon_stats_enabled(s)) {
199 timer_del(s->stats_timer);
200 timer_free(s->stats_timer);
201 s->stats_timer = NULL;
202 s->stats_poll_interval = 0;
206 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
208 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
211 static void balloon_stats_poll_cb(void *opaque)
213 VirtIOBalloon *s = opaque;
214 VirtIODevice *vdev = VIRTIO_DEVICE(s);
216 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
217 /* re-schedule */
218 balloon_stats_change_timer(s, s->stats_poll_interval);
219 return;
222 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
223 virtio_notify(vdev, s->svq);
224 g_free(s->stats_vq_elem);
225 s->stats_vq_elem = NULL;
228 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
229 void *opaque, Error **errp)
231 Error *err = NULL;
232 VirtIOBalloon *s = opaque;
233 int i;
235 visit_start_struct(v, name, NULL, 0, &err);
236 if (err) {
237 goto out;
239 visit_type_int(v, "last-update", &s->stats_last_update, &err);
240 if (err) {
241 goto out_end;
244 visit_start_struct(v, "stats", NULL, 0, &err);
245 if (err) {
246 goto out_end;
248 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
249 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
250 if (err) {
251 goto out_nested;
254 visit_check_struct(v, &err);
255 out_nested:
256 visit_end_struct(v, NULL);
258 if (!err) {
259 visit_check_struct(v, &err);
261 out_end:
262 visit_end_struct(v, NULL);
263 out:
264 error_propagate(errp, err);
267 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
268 const char *name, void *opaque,
269 Error **errp)
271 VirtIOBalloon *s = opaque;
272 visit_type_int(v, name, &s->stats_poll_interval, errp);
275 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
276 const char *name, void *opaque,
277 Error **errp)
279 VirtIOBalloon *s = opaque;
280 Error *local_err = NULL;
281 int64_t value;
283 visit_type_int(v, name, &value, &local_err);
284 if (local_err) {
285 error_propagate(errp, local_err);
286 return;
289 if (value < 0) {
290 error_setg(errp, "timer value must be greater than zero");
291 return;
294 if (value > UINT32_MAX) {
295 error_setg(errp, "timer value is too big");
296 return;
299 if (value == s->stats_poll_interval) {
300 return;
303 if (value == 0) {
304 /* timer=0 disables the timer */
305 balloon_stats_destroy_timer(s);
306 return;
309 if (balloon_stats_enabled(s)) {
310 /* timer interval change */
311 s->stats_poll_interval = value;
312 balloon_stats_change_timer(s, value);
313 return;
316 /* create a new timer */
317 g_assert(s->stats_timer == NULL);
318 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
319 s->stats_poll_interval = value;
320 balloon_stats_change_timer(s, 0);
323 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
325 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
326 VirtQueueElement *elem;
327 MemoryRegionSection section;
329 for (;;) {
330 PartiallyBalloonedPage pbp = {};
331 size_t offset = 0;
332 uint32_t pfn;
334 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
335 if (!elem) {
336 break;
339 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
340 unsigned int p = virtio_ldl_p(vdev, &pfn);
341 hwaddr pa;
343 pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT;
344 offset += 4;
346 section = memory_region_find(get_system_memory(), pa,
347 BALLOON_PAGE_SIZE);
348 if (!section.mr) {
349 trace_virtio_balloon_bad_addr(pa);
350 continue;
352 if (!memory_region_is_ram(section.mr) ||
353 memory_region_is_rom(section.mr) ||
354 memory_region_is_romd(section.mr)) {
355 trace_virtio_balloon_bad_addr(pa);
356 memory_region_unref(section.mr);
357 continue;
360 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
361 pa);
362 if (!qemu_balloon_is_inhibited()) {
363 if (vq == s->ivq) {
364 balloon_inflate_page(s, section.mr,
365 section.offset_within_region, &pbp);
366 } else if (vq == s->dvq) {
367 balloon_deflate_page(s, section.mr, section.offset_within_region);
368 } else {
369 g_assert_not_reached();
372 memory_region_unref(section.mr);
375 virtqueue_push(vq, elem, offset);
376 virtio_notify(vdev, vq);
377 g_free(elem);
378 virtio_balloon_pbp_free(&pbp);
382 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
384 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
385 VirtQueueElement *elem;
386 VirtIOBalloonStat stat;
387 size_t offset = 0;
388 qemu_timeval tv;
390 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
391 if (!elem) {
392 goto out;
395 if (s->stats_vq_elem != NULL) {
396 /* This should never happen if the driver follows the spec. */
397 virtqueue_push(vq, s->stats_vq_elem, 0);
398 virtio_notify(vdev, vq);
399 g_free(s->stats_vq_elem);
402 s->stats_vq_elem = elem;
404 /* Initialize the stats to get rid of any stale values. This is only
405 * needed to handle the case where a guest supports fewer stats than it
406 * used to (ie. it has booted into an old kernel).
408 reset_stats(s);
410 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
411 == sizeof(stat)) {
412 uint16_t tag = virtio_tswap16(vdev, stat.tag);
413 uint64_t val = virtio_tswap64(vdev, stat.val);
415 offset += sizeof(stat);
416 if (tag < VIRTIO_BALLOON_S_NR)
417 s->stats[tag] = val;
419 s->stats_vq_offset = offset;
421 if (qemu_gettimeofday(&tv) < 0) {
422 warn_report("%s: failed to get time of day", __func__);
423 goto out;
426 s->stats_last_update = tv.tv_sec;
428 out:
429 if (balloon_stats_enabled(s)) {
430 balloon_stats_change_timer(s, s->stats_poll_interval);
434 static void virtio_balloon_handle_free_page_vq(VirtIODevice *vdev,
435 VirtQueue *vq)
437 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
438 qemu_bh_schedule(s->free_page_bh);
441 static bool get_free_page_hints(VirtIOBalloon *dev)
443 VirtQueueElement *elem;
444 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
445 VirtQueue *vq = dev->free_page_vq;
446 bool ret = true;
448 while (dev->block_iothread) {
449 qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
452 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
453 if (!elem) {
454 return false;
457 if (elem->out_num) {
458 uint32_t id;
459 size_t size = iov_to_buf(elem->out_sg, elem->out_num, 0,
460 &id, sizeof(id));
462 virtio_tswap32s(vdev, &id);
463 if (unlikely(size != sizeof(id))) {
464 virtio_error(vdev, "received an incorrect cmd id");
465 ret = false;
466 goto out;
468 if (id == dev->free_page_report_cmd_id) {
469 dev->free_page_report_status = FREE_PAGE_REPORT_S_START;
470 } else {
472 * Stop the optimization only when it has started. This
473 * avoids a stale stop sign for the previous command.
475 if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
476 dev->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
481 if (elem->in_num) {
482 if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
483 qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
484 elem->in_sg[0].iov_len);
488 out:
489 virtqueue_push(vq, elem, 1);
490 g_free(elem);
491 return ret;
494 static void virtio_ballloon_get_free_page_hints(void *opaque)
496 VirtIOBalloon *dev = opaque;
497 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
498 VirtQueue *vq = dev->free_page_vq;
499 bool continue_to_get_hints;
501 do {
502 qemu_mutex_lock(&dev->free_page_lock);
503 virtio_queue_set_notification(vq, 0);
504 continue_to_get_hints = get_free_page_hints(dev);
505 qemu_mutex_unlock(&dev->free_page_lock);
506 virtio_notify(vdev, vq);
508 * Start to poll the vq once the reporting started. Otherwise, continue
509 * only when there are entries on the vq, which need to be given back.
511 } while (continue_to_get_hints ||
512 dev->free_page_report_status == FREE_PAGE_REPORT_S_START);
513 virtio_queue_set_notification(vq, 1);
516 static bool virtio_balloon_free_page_support(void *opaque)
518 VirtIOBalloon *s = opaque;
519 VirtIODevice *vdev = VIRTIO_DEVICE(s);
521 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT);
524 static void virtio_balloon_free_page_start(VirtIOBalloon *s)
526 VirtIODevice *vdev = VIRTIO_DEVICE(s);
528 /* For the stop and copy phase, we don't need to start the optimization */
529 if (!vdev->vm_running) {
530 return;
533 if (s->free_page_report_cmd_id == UINT_MAX) {
534 s->free_page_report_cmd_id =
535 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
536 } else {
537 s->free_page_report_cmd_id++;
540 s->free_page_report_status = FREE_PAGE_REPORT_S_REQUESTED;
541 virtio_notify_config(vdev);
544 static void virtio_balloon_free_page_stop(VirtIOBalloon *s)
546 VirtIODevice *vdev = VIRTIO_DEVICE(s);
548 if (s->free_page_report_status != FREE_PAGE_REPORT_S_STOP) {
550 * The lock also guarantees us that the
551 * virtio_ballloon_get_free_page_hints exits after the
552 * free_page_report_status is set to S_STOP.
554 qemu_mutex_lock(&s->free_page_lock);
556 * The guest hasn't done the reporting, so host sends a notification
557 * to the guest to actively stop the reporting.
559 s->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
560 qemu_mutex_unlock(&s->free_page_lock);
561 virtio_notify_config(vdev);
565 static void virtio_balloon_free_page_done(VirtIOBalloon *s)
567 VirtIODevice *vdev = VIRTIO_DEVICE(s);
569 s->free_page_report_status = FREE_PAGE_REPORT_S_DONE;
570 virtio_notify_config(vdev);
573 static int
574 virtio_balloon_free_page_report_notify(NotifierWithReturn *n, void *data)
576 VirtIOBalloon *dev = container_of(n, VirtIOBalloon,
577 free_page_report_notify);
578 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
579 PrecopyNotifyData *pnd = data;
581 if (!virtio_balloon_free_page_support(dev)) {
583 * This is an optimization provided to migration, so just return 0 to
584 * have the normal migration process not affected when this feature is
585 * not supported.
587 return 0;
590 switch (pnd->reason) {
591 case PRECOPY_NOTIFY_SETUP:
592 precopy_enable_free_page_optimization();
593 break;
594 case PRECOPY_NOTIFY_COMPLETE:
595 case PRECOPY_NOTIFY_CLEANUP:
596 case PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC:
597 virtio_balloon_free_page_stop(dev);
598 break;
599 case PRECOPY_NOTIFY_AFTER_BITMAP_SYNC:
600 if (vdev->vm_running) {
601 virtio_balloon_free_page_start(dev);
602 } else {
603 virtio_balloon_free_page_done(dev);
605 break;
606 default:
607 virtio_error(vdev, "%s: %d reason unknown", __func__, pnd->reason);
610 return 0;
613 static size_t virtio_balloon_config_size(VirtIOBalloon *s)
615 uint64_t features = s->host_features;
617 if (s->qemu_4_0_config_size) {
618 return sizeof(struct virtio_balloon_config);
620 if (virtio_has_feature(features, VIRTIO_BALLOON_F_PAGE_POISON)) {
621 return sizeof(struct virtio_balloon_config);
623 if (virtio_has_feature(features, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
624 return offsetof(struct virtio_balloon_config, poison_val);
626 return offsetof(struct virtio_balloon_config, free_page_report_cmd_id);
629 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
631 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
632 struct virtio_balloon_config config = {};
634 config.num_pages = cpu_to_le32(dev->num_pages);
635 config.actual = cpu_to_le32(dev->actual);
637 if (dev->free_page_report_status == FREE_PAGE_REPORT_S_REQUESTED) {
638 config.free_page_report_cmd_id =
639 cpu_to_le32(dev->free_page_report_cmd_id);
640 } else if (dev->free_page_report_status == FREE_PAGE_REPORT_S_STOP) {
641 config.free_page_report_cmd_id =
642 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_STOP);
643 } else if (dev->free_page_report_status == FREE_PAGE_REPORT_S_DONE) {
644 config.free_page_report_cmd_id =
645 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_DONE);
648 trace_virtio_balloon_get_config(config.num_pages, config.actual);
649 memcpy(config_data, &config, virtio_balloon_config_size(dev));
652 static int build_dimm_list(Object *obj, void *opaque)
654 GSList **list = opaque;
656 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
657 DeviceState *dev = DEVICE(obj);
658 if (dev->realized) { /* only realized DIMMs matter */
659 *list = g_slist_prepend(*list, dev);
663 object_child_foreach(obj, build_dimm_list, opaque);
664 return 0;
667 static ram_addr_t get_current_ram_size(void)
669 GSList *list = NULL, *item;
670 ram_addr_t size = ram_size;
672 build_dimm_list(qdev_get_machine(), &list);
673 for (item = list; item; item = g_slist_next(item)) {
674 Object *obj = OBJECT(item->data);
675 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
676 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
677 &error_abort);
680 g_slist_free(list);
682 return size;
685 static void virtio_balloon_set_config(VirtIODevice *vdev,
686 const uint8_t *config_data)
688 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
689 struct virtio_balloon_config config;
690 uint32_t oldactual = dev->actual;
691 ram_addr_t vm_ram_size = get_current_ram_size();
693 memcpy(&config, config_data, virtio_balloon_config_size(dev));
694 dev->actual = le32_to_cpu(config.actual);
695 if (dev->actual != oldactual) {
696 qapi_event_send_balloon_change(vm_ram_size -
697 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
699 trace_virtio_balloon_set_config(dev->actual, oldactual);
702 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
703 Error **errp)
705 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
706 f |= dev->host_features;
707 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
709 return f;
712 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
714 VirtIOBalloon *dev = opaque;
715 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
716 VIRTIO_BALLOON_PFN_SHIFT);
719 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
721 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
722 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
723 ram_addr_t vm_ram_size = get_current_ram_size();
725 if (target > vm_ram_size) {
726 target = vm_ram_size;
728 if (target) {
729 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
730 virtio_notify_config(vdev);
732 trace_virtio_balloon_to_target(target, dev->num_pages);
735 static int virtio_balloon_post_load_device(void *opaque, int version_id)
737 VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
739 if (balloon_stats_enabled(s)) {
740 balloon_stats_change_timer(s, s->stats_poll_interval);
742 return 0;
745 static const VMStateDescription vmstate_virtio_balloon_free_page_report = {
746 .name = "virtio-balloon-device/free-page-report",
747 .version_id = 1,
748 .minimum_version_id = 1,
749 .needed = virtio_balloon_free_page_support,
750 .fields = (VMStateField[]) {
751 VMSTATE_UINT32(free_page_report_cmd_id, VirtIOBalloon),
752 VMSTATE_UINT32(free_page_report_status, VirtIOBalloon),
753 VMSTATE_END_OF_LIST()
757 static const VMStateDescription vmstate_virtio_balloon_device = {
758 .name = "virtio-balloon-device",
759 .version_id = 1,
760 .minimum_version_id = 1,
761 .post_load = virtio_balloon_post_load_device,
762 .fields = (VMStateField[]) {
763 VMSTATE_UINT32(num_pages, VirtIOBalloon),
764 VMSTATE_UINT32(actual, VirtIOBalloon),
765 VMSTATE_END_OF_LIST()
767 .subsections = (const VMStateDescription * []) {
768 &vmstate_virtio_balloon_free_page_report,
769 NULL
773 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
775 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
776 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
777 int ret;
779 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
780 virtio_balloon_config_size(s));
782 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
783 virtio_balloon_stat, s);
785 if (ret < 0) {
786 error_setg(errp, "Only one balloon device is supported");
787 virtio_cleanup(vdev);
788 return;
791 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
792 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
793 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
795 if (virtio_has_feature(s->host_features,
796 VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
797 s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
798 virtio_balloon_handle_free_page_vq);
799 s->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
800 s->free_page_report_cmd_id =
801 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
802 s->free_page_report_notify.notify =
803 virtio_balloon_free_page_report_notify;
804 precopy_add_notifier(&s->free_page_report_notify);
805 if (s->iothread) {
806 object_ref(OBJECT(s->iothread));
807 s->free_page_bh = aio_bh_new(iothread_get_aio_context(s->iothread),
808 virtio_ballloon_get_free_page_hints, s);
809 qemu_mutex_init(&s->free_page_lock);
810 qemu_cond_init(&s->free_page_cond);
811 s->block_iothread = false;
812 } else {
813 /* Simply disable this feature if the iothread wasn't created. */
814 s->host_features &= ~(1 << VIRTIO_BALLOON_F_FREE_PAGE_HINT);
815 virtio_error(vdev, "iothread is missing");
818 reset_stats(s);
821 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
823 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
824 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
826 if (virtio_balloon_free_page_support(s)) {
827 qemu_bh_delete(s->free_page_bh);
828 virtio_balloon_free_page_stop(s);
829 precopy_remove_notifier(&s->free_page_report_notify);
831 balloon_stats_destroy_timer(s);
832 qemu_remove_balloon_handler(s);
833 virtio_cleanup(vdev);
836 static void virtio_balloon_device_reset(VirtIODevice *vdev)
838 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
840 if (virtio_balloon_free_page_support(s)) {
841 virtio_balloon_free_page_stop(s);
844 if (s->stats_vq_elem != NULL) {
845 virtqueue_unpop(s->svq, s->stats_vq_elem, 0);
846 g_free(s->stats_vq_elem);
847 s->stats_vq_elem = NULL;
851 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
853 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
855 if (!s->stats_vq_elem && vdev->vm_running &&
856 (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) {
857 /* poll stats queue for the element we have discarded when the VM
858 * was stopped */
859 virtio_balloon_receive_stats(vdev, s->svq);
862 if (virtio_balloon_free_page_support(s)) {
864 * The VM is woken up and the iothread was blocked, so signal it to
865 * continue.
867 if (vdev->vm_running && s->block_iothread) {
868 qemu_mutex_lock(&s->free_page_lock);
869 s->block_iothread = false;
870 qemu_cond_signal(&s->free_page_cond);
871 qemu_mutex_unlock(&s->free_page_lock);
874 /* The VM is stopped, block the iothread. */
875 if (!vdev->vm_running) {
876 qemu_mutex_lock(&s->free_page_lock);
877 s->block_iothread = true;
878 qemu_mutex_unlock(&s->free_page_lock);
883 static void virtio_balloon_instance_init(Object *obj)
885 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
887 object_property_add(obj, "guest-stats", "guest statistics",
888 balloon_stats_get_all, NULL, NULL, s, NULL);
890 object_property_add(obj, "guest-stats-polling-interval", "int",
891 balloon_stats_get_poll_interval,
892 balloon_stats_set_poll_interval,
893 NULL, s, NULL);
896 static const VMStateDescription vmstate_virtio_balloon = {
897 .name = "virtio-balloon",
898 .minimum_version_id = 1,
899 .version_id = 1,
900 .fields = (VMStateField[]) {
901 VMSTATE_VIRTIO_DEVICE,
902 VMSTATE_END_OF_LIST()
906 static Property virtio_balloon_properties[] = {
907 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
908 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
909 DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon, host_features,
910 VIRTIO_BALLOON_F_FREE_PAGE_HINT, false),
911 /* QEMU 4.0 accidentally changed the config size even when free-page-hint
912 * is disabled, resulting in QEMU 3.1 migration incompatibility. This
913 * property retains this quirk for QEMU 4.1 machine types.
915 DEFINE_PROP_BOOL("qemu-4-0-config-size", VirtIOBalloon,
916 qemu_4_0_config_size, false),
917 DEFINE_PROP_LINK("iothread", VirtIOBalloon, iothread, TYPE_IOTHREAD,
918 IOThread *),
919 DEFINE_PROP_END_OF_LIST(),
922 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
924 DeviceClass *dc = DEVICE_CLASS(klass);
925 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
927 dc->props = virtio_balloon_properties;
928 dc->vmsd = &vmstate_virtio_balloon;
929 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
930 vdc->realize = virtio_balloon_device_realize;
931 vdc->unrealize = virtio_balloon_device_unrealize;
932 vdc->reset = virtio_balloon_device_reset;
933 vdc->get_config = virtio_balloon_get_config;
934 vdc->set_config = virtio_balloon_set_config;
935 vdc->get_features = virtio_balloon_get_features;
936 vdc->set_status = virtio_balloon_set_status;
937 vdc->vmsd = &vmstate_virtio_balloon_device;
940 static const TypeInfo virtio_balloon_info = {
941 .name = TYPE_VIRTIO_BALLOON,
942 .parent = TYPE_VIRTIO_DEVICE,
943 .instance_size = sizeof(VirtIOBalloon),
944 .instance_init = virtio_balloon_instance_init,
945 .class_init = virtio_balloon_class_init,
948 static void virtio_register_types(void)
950 type_register_static(&virtio_balloon_info);
953 type_init(virtio_register_types)