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>
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"
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"
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
{
40 unsigned long *bitmap
;
41 } PartiallyBalloonedPage
;
43 static void virtio_balloon_pbp_free(PartiallyBalloonedPage
*pbp
)
52 static PartiallyBalloonedPage
*virtio_balloon_pbp_alloc(ram_addr_t base_gpa
,
55 PartiallyBalloonedPage
*pbp
= g_new0(PartiallyBalloonedPage
, 1);
57 pbp
->base_gpa
= base_gpa
;
58 pbp
->subpages
= subpages
;
59 pbp
->bitmap
= bitmap_new(subpages
);
64 static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage
*pbp
,
65 ram_addr_t base_gpa
, long subpages
)
67 return pbp
->subpages
== subpages
&& pbp
->base_gpa
== base_gpa
;
70 static void balloon_inflate_page(VirtIOBalloon
*balloon
,
71 MemoryRegion
*mr
, hwaddr mr_offset
,
72 PartiallyBalloonedPage
**pbp
)
74 void *addr
= memory_region_get_ram_ptr(mr
) + mr_offset
;
75 ram_addr_t rb_offset
, rb_aligned_offset
, base_gpa
;
80 /* XXX is there a better way to get to the RAMBlock than via a
82 rb
= qemu_ram_block_from_host(addr
, false, &rb_offset
);
83 rb_page_size
= qemu_ram_pagesize(rb
);
85 if (rb_page_size
== BALLOON_PAGE_SIZE
) {
88 ram_block_discard_range(rb
, rb_offset
, rb_page_size
);
89 /* We ignore errors from ram_block_discard_range(), because it
90 * has already reported them, and failing to discard a balloon
91 * page is not fatal */
97 * We've put a piece of a larger host page into the balloon - we
98 * need to keep track until we have a whole host page to
102 "Balloon used with backing page size > 4kiB, this may not be reliable");
104 rb_aligned_offset
= QEMU_ALIGN_DOWN(rb_offset
, rb_page_size
);
105 subpages
= rb_page_size
/ BALLOON_PAGE_SIZE
;
106 base_gpa
= memory_region_get_ram_addr(mr
) + mr_offset
-
107 (rb_offset
- rb_aligned_offset
);
109 if (*pbp
&& !virtio_balloon_pbp_matches(*pbp
, base_gpa
, subpages
)) {
110 /* We've partially ballooned part of a host page, but now
111 * we're trying to balloon part of a different one. Too hard,
112 * give up on the old partial page */
113 virtio_balloon_pbp_free(*pbp
);
118 *pbp
= virtio_balloon_pbp_alloc(base_gpa
, subpages
);
121 set_bit((rb_offset
- rb_aligned_offset
) / BALLOON_PAGE_SIZE
,
124 if (bitmap_full((*pbp
)->bitmap
, subpages
)) {
125 /* We've accumulated a full host page, we can actually discard
128 ram_block_discard_range(rb
, rb_aligned_offset
, rb_page_size
);
129 /* We ignore errors from ram_block_discard_range(), because it
130 * has already reported them, and failing to discard a balloon
131 * page is not fatal */
132 virtio_balloon_pbp_free(*pbp
);
137 static void balloon_deflate_page(VirtIOBalloon
*balloon
,
138 MemoryRegion
*mr
, hwaddr mr_offset
)
140 void *addr
= memory_region_get_ram_ptr(mr
) + mr_offset
;
141 ram_addr_t rb_offset
;
147 /* XXX is there a better way to get to the RAMBlock than via a
149 rb
= qemu_ram_block_from_host(addr
, false, &rb_offset
);
150 rb_page_size
= qemu_ram_pagesize(rb
);
152 host_addr
= (void *)((uintptr_t)addr
& ~(rb_page_size
- 1));
154 /* When a page is deflated, we hint the whole host page it lives
155 * on, since we can't do anything smaller */
156 ret
= qemu_madvise(host_addr
, rb_page_size
, QEMU_MADV_WILLNEED
);
158 warn_report("Couldn't MADV_WILLNEED on balloon deflate: %s",
160 /* Otherwise ignore, failing to page hint shouldn't be fatal */
164 static const char *balloon_stat_names
[] = {
165 [VIRTIO_BALLOON_S_SWAP_IN
] = "stat-swap-in",
166 [VIRTIO_BALLOON_S_SWAP_OUT
] = "stat-swap-out",
167 [VIRTIO_BALLOON_S_MAJFLT
] = "stat-major-faults",
168 [VIRTIO_BALLOON_S_MINFLT
] = "stat-minor-faults",
169 [VIRTIO_BALLOON_S_MEMFREE
] = "stat-free-memory",
170 [VIRTIO_BALLOON_S_MEMTOT
] = "stat-total-memory",
171 [VIRTIO_BALLOON_S_AVAIL
] = "stat-available-memory",
172 [VIRTIO_BALLOON_S_CACHES
] = "stat-disk-caches",
173 [VIRTIO_BALLOON_S_HTLB_PGALLOC
] = "stat-htlb-pgalloc",
174 [VIRTIO_BALLOON_S_HTLB_PGFAIL
] = "stat-htlb-pgfail",
175 [VIRTIO_BALLOON_S_NR
] = NULL
179 * reset_stats - Mark all items in the stats array as unset
181 * This function needs to be called at device initialization and before
182 * updating to a set of newly-generated stats. This will ensure that no
183 * stale values stick around in case the guest reports a subset of the supported
186 static inline void reset_stats(VirtIOBalloon
*dev
)
189 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
192 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
194 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
195 return virtio_vdev_has_feature(vdev
, VIRTIO_BALLOON_F_STATS_VQ
);
198 static bool balloon_stats_enabled(const VirtIOBalloon
*s
)
200 return s
->stats_poll_interval
> 0;
203 static void balloon_stats_destroy_timer(VirtIOBalloon
*s
)
205 if (balloon_stats_enabled(s
)) {
206 timer_del(s
->stats_timer
);
207 timer_free(s
->stats_timer
);
208 s
->stats_timer
= NULL
;
209 s
->stats_poll_interval
= 0;
213 static void balloon_stats_change_timer(VirtIOBalloon
*s
, int64_t secs
)
215 timer_mod(s
->stats_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + secs
* 1000);
218 static void balloon_stats_poll_cb(void *opaque
)
220 VirtIOBalloon
*s
= opaque
;
221 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
223 if (s
->stats_vq_elem
== NULL
|| !balloon_stats_supported(s
)) {
225 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
229 virtqueue_push(s
->svq
, s
->stats_vq_elem
, s
->stats_vq_offset
);
230 virtio_notify(vdev
, s
->svq
);
231 g_free(s
->stats_vq_elem
);
232 s
->stats_vq_elem
= NULL
;
235 static void balloon_stats_get_all(Object
*obj
, Visitor
*v
, const char *name
,
236 void *opaque
, Error
**errp
)
239 VirtIOBalloon
*s
= opaque
;
242 visit_start_struct(v
, name
, NULL
, 0, &err
);
246 visit_type_int(v
, "last-update", &s
->stats_last_update
, &err
);
251 visit_start_struct(v
, "stats", NULL
, 0, &err
);
255 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
256 visit_type_uint64(v
, balloon_stat_names
[i
], &s
->stats
[i
], &err
);
261 visit_check_struct(v
, &err
);
263 visit_end_struct(v
, NULL
);
266 visit_check_struct(v
, &err
);
269 visit_end_struct(v
, NULL
);
271 error_propagate(errp
, err
);
274 static void balloon_stats_get_poll_interval(Object
*obj
, Visitor
*v
,
275 const char *name
, void *opaque
,
278 VirtIOBalloon
*s
= opaque
;
279 visit_type_int(v
, name
, &s
->stats_poll_interval
, errp
);
282 static void balloon_stats_set_poll_interval(Object
*obj
, Visitor
*v
,
283 const char *name
, void *opaque
,
286 VirtIOBalloon
*s
= opaque
;
287 Error
*local_err
= NULL
;
290 visit_type_int(v
, name
, &value
, &local_err
);
292 error_propagate(errp
, local_err
);
297 error_setg(errp
, "timer value must be greater than zero");
301 if (value
> UINT32_MAX
) {
302 error_setg(errp
, "timer value is too big");
306 if (value
== s
->stats_poll_interval
) {
311 /* timer=0 disables the timer */
312 balloon_stats_destroy_timer(s
);
316 if (balloon_stats_enabled(s
)) {
317 /* timer interval change */
318 s
->stats_poll_interval
= value
;
319 balloon_stats_change_timer(s
, value
);
323 /* create a new timer */
324 g_assert(s
->stats_timer
== NULL
);
325 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
326 s
->stats_poll_interval
= value
;
327 balloon_stats_change_timer(s
, 0);
330 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
332 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
333 PartiallyBalloonedPage
*pbp
= NULL
;
334 VirtQueueElement
*elem
;
335 MemoryRegionSection section
;
340 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
345 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &pfn
, 4) == 4) {
346 unsigned int p
= virtio_ldl_p(vdev
, &pfn
);
349 pa
= (hwaddr
) p
<< VIRTIO_BALLOON_PFN_SHIFT
;
352 section
= memory_region_find(get_system_memory(), pa
,
355 trace_virtio_balloon_bad_addr(pa
);
358 if (!memory_region_is_ram(section
.mr
) ||
359 memory_region_is_rom(section
.mr
) ||
360 memory_region_is_romd(section
.mr
)) {
361 trace_virtio_balloon_bad_addr(pa
);
362 memory_region_unref(section
.mr
);
366 trace_virtio_balloon_handle_output(memory_region_name(section
.mr
),
368 if (!qemu_balloon_is_inhibited()) {
370 balloon_inflate_page(s
, section
.mr
,
371 section
.offset_within_region
, &pbp
);
372 } else if (vq
== s
->dvq
) {
373 balloon_deflate_page(s
, section
.mr
, section
.offset_within_region
);
375 g_assert_not_reached();
378 memory_region_unref(section
.mr
);
381 virtqueue_push(vq
, elem
, offset
);
382 virtio_notify(vdev
, vq
);
386 virtio_balloon_pbp_free(pbp
);
389 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
391 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
392 VirtQueueElement
*elem
;
393 VirtIOBalloonStat stat
;
397 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
402 if (s
->stats_vq_elem
!= NULL
) {
403 /* This should never happen if the driver follows the spec. */
404 virtqueue_push(vq
, s
->stats_vq_elem
, 0);
405 virtio_notify(vdev
, vq
);
406 g_free(s
->stats_vq_elem
);
409 s
->stats_vq_elem
= elem
;
411 /* Initialize the stats to get rid of any stale values. This is only
412 * needed to handle the case where a guest supports fewer stats than it
413 * used to (ie. it has booted into an old kernel).
417 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
419 uint16_t tag
= virtio_tswap16(vdev
, stat
.tag
);
420 uint64_t val
= virtio_tswap64(vdev
, stat
.val
);
422 offset
+= sizeof(stat
);
423 if (tag
< VIRTIO_BALLOON_S_NR
)
426 s
->stats_vq_offset
= offset
;
428 if (qemu_gettimeofday(&tv
) < 0) {
429 warn_report("%s: failed to get time of day", __func__
);
433 s
->stats_last_update
= tv
.tv_sec
;
436 if (balloon_stats_enabled(s
)) {
437 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
441 static void virtio_balloon_handle_free_page_vq(VirtIODevice
*vdev
,
444 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
445 qemu_bh_schedule(s
->free_page_bh
);
448 static bool get_free_page_hints(VirtIOBalloon
*dev
)
450 VirtQueueElement
*elem
;
451 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
452 VirtQueue
*vq
= dev
->free_page_vq
;
455 while (dev
->block_iothread
) {
456 qemu_cond_wait(&dev
->free_page_cond
, &dev
->free_page_lock
);
459 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
466 size_t size
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
469 virtio_tswap32s(vdev
, &id
);
470 if (unlikely(size
!= sizeof(id
))) {
471 virtio_error(vdev
, "received an incorrect cmd id");
475 if (id
== dev
->free_page_report_cmd_id
) {
476 dev
->free_page_report_status
= FREE_PAGE_REPORT_S_START
;
479 * Stop the optimization only when it has started. This
480 * avoids a stale stop sign for the previous command.
482 if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_START
) {
483 dev
->free_page_report_status
= FREE_PAGE_REPORT_S_STOP
;
489 if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_START
) {
490 qemu_guest_free_page_hint(elem
->in_sg
[0].iov_base
,
491 elem
->in_sg
[0].iov_len
);
496 virtqueue_push(vq
, elem
, 1);
501 static void virtio_ballloon_get_free_page_hints(void *opaque
)
503 VirtIOBalloon
*dev
= opaque
;
504 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
505 VirtQueue
*vq
= dev
->free_page_vq
;
506 bool continue_to_get_hints
;
509 qemu_mutex_lock(&dev
->free_page_lock
);
510 virtio_queue_set_notification(vq
, 0);
511 continue_to_get_hints
= get_free_page_hints(dev
);
512 qemu_mutex_unlock(&dev
->free_page_lock
);
513 virtio_notify(vdev
, vq
);
515 * Start to poll the vq once the reporting started. Otherwise, continue
516 * only when there are entries on the vq, which need to be given back.
518 } while (continue_to_get_hints
||
519 dev
->free_page_report_status
== FREE_PAGE_REPORT_S_START
);
520 virtio_queue_set_notification(vq
, 1);
523 static bool virtio_balloon_free_page_support(void *opaque
)
525 VirtIOBalloon
*s
= opaque
;
526 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
528 return virtio_vdev_has_feature(vdev
, VIRTIO_BALLOON_F_FREE_PAGE_HINT
);
531 static void virtio_balloon_free_page_start(VirtIOBalloon
*s
)
533 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
535 /* For the stop and copy phase, we don't need to start the optimization */
536 if (!vdev
->vm_running
) {
540 if (s
->free_page_report_cmd_id
== UINT_MAX
) {
541 s
->free_page_report_cmd_id
=
542 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN
;
544 s
->free_page_report_cmd_id
++;
547 s
->free_page_report_status
= FREE_PAGE_REPORT_S_REQUESTED
;
548 virtio_notify_config(vdev
);
551 static void virtio_balloon_free_page_stop(VirtIOBalloon
*s
)
553 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
555 if (s
->free_page_report_status
!= FREE_PAGE_REPORT_S_STOP
) {
557 * The lock also guarantees us that the
558 * virtio_ballloon_get_free_page_hints exits after the
559 * free_page_report_status is set to S_STOP.
561 qemu_mutex_lock(&s
->free_page_lock
);
563 * The guest hasn't done the reporting, so host sends a notification
564 * to the guest to actively stop the reporting.
566 s
->free_page_report_status
= FREE_PAGE_REPORT_S_STOP
;
567 qemu_mutex_unlock(&s
->free_page_lock
);
568 virtio_notify_config(vdev
);
572 static void virtio_balloon_free_page_done(VirtIOBalloon
*s
)
574 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
576 s
->free_page_report_status
= FREE_PAGE_REPORT_S_DONE
;
577 virtio_notify_config(vdev
);
581 virtio_balloon_free_page_report_notify(NotifierWithReturn
*n
, void *data
)
583 VirtIOBalloon
*dev
= container_of(n
, VirtIOBalloon
,
584 free_page_report_notify
);
585 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
586 PrecopyNotifyData
*pnd
= data
;
588 if (!virtio_balloon_free_page_support(dev
)) {
590 * This is an optimization provided to migration, so just return 0 to
591 * have the normal migration process not affected when this feature is
597 switch (pnd
->reason
) {
598 case PRECOPY_NOTIFY_SETUP
:
599 precopy_enable_free_page_optimization();
601 case PRECOPY_NOTIFY_COMPLETE
:
602 case PRECOPY_NOTIFY_CLEANUP
:
603 case PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC
:
604 virtio_balloon_free_page_stop(dev
);
606 case PRECOPY_NOTIFY_AFTER_BITMAP_SYNC
:
607 if (vdev
->vm_running
) {
608 virtio_balloon_free_page_start(dev
);
610 virtio_balloon_free_page_done(dev
);
614 virtio_error(vdev
, "%s: %d reason unknown", __func__
, pnd
->reason
);
620 static size_t virtio_balloon_config_size(VirtIOBalloon
*s
)
622 uint64_t features
= s
->host_features
;
624 if (s
->qemu_4_0_config_size
) {
625 return sizeof(struct virtio_balloon_config
);
627 if (virtio_has_feature(features
, VIRTIO_BALLOON_F_PAGE_POISON
)) {
628 return sizeof(struct virtio_balloon_config
);
630 if (virtio_has_feature(features
, VIRTIO_BALLOON_F_FREE_PAGE_HINT
)) {
631 return offsetof(struct virtio_balloon_config
, poison_val
);
633 return offsetof(struct virtio_balloon_config
, free_page_report_cmd_id
);
636 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
638 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
639 struct virtio_balloon_config config
= {};
641 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
642 config
.actual
= cpu_to_le32(dev
->actual
);
644 if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_REQUESTED
) {
645 config
.free_page_report_cmd_id
=
646 cpu_to_le32(dev
->free_page_report_cmd_id
);
647 } else if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_STOP
) {
648 config
.free_page_report_cmd_id
=
649 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_STOP
);
650 } else if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_DONE
) {
651 config
.free_page_report_cmd_id
=
652 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_DONE
);
655 trace_virtio_balloon_get_config(config
.num_pages
, config
.actual
);
656 memcpy(config_data
, &config
, virtio_balloon_config_size(dev
));
659 static int build_dimm_list(Object
*obj
, void *opaque
)
661 GSList
**list
= opaque
;
663 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
664 DeviceState
*dev
= DEVICE(obj
);
665 if (dev
->realized
) { /* only realized DIMMs matter */
666 *list
= g_slist_prepend(*list
, dev
);
670 object_child_foreach(obj
, build_dimm_list
, opaque
);
674 static ram_addr_t
get_current_ram_size(void)
676 GSList
*list
= NULL
, *item
;
677 ram_addr_t size
= ram_size
;
679 build_dimm_list(qdev_get_machine(), &list
);
680 for (item
= list
; item
; item
= g_slist_next(item
)) {
681 Object
*obj
= OBJECT(item
->data
);
682 if (!strcmp(object_get_typename(obj
), TYPE_PC_DIMM
)) {
683 size
+= object_property_get_int(obj
, PC_DIMM_SIZE_PROP
,
692 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
693 const uint8_t *config_data
)
695 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
696 struct virtio_balloon_config config
;
697 uint32_t oldactual
= dev
->actual
;
698 ram_addr_t vm_ram_size
= get_current_ram_size();
700 memcpy(&config
, config_data
, virtio_balloon_config_size(dev
));
701 dev
->actual
= le32_to_cpu(config
.actual
);
702 if (dev
->actual
!= oldactual
) {
703 qapi_event_send_balloon_change(vm_ram_size
-
704 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
706 trace_virtio_balloon_set_config(dev
->actual
, oldactual
);
709 static uint64_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint64_t f
,
712 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
713 f
|= dev
->host_features
;
714 virtio_add_feature(&f
, VIRTIO_BALLOON_F_STATS_VQ
);
719 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
721 VirtIOBalloon
*dev
= opaque
;
722 info
->actual
= get_current_ram_size() - ((uint64_t) dev
->actual
<<
723 VIRTIO_BALLOON_PFN_SHIFT
);
726 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
728 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
729 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
730 ram_addr_t vm_ram_size
= get_current_ram_size();
732 if (target
> vm_ram_size
) {
733 target
= vm_ram_size
;
736 dev
->num_pages
= (vm_ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
737 virtio_notify_config(vdev
);
739 trace_virtio_balloon_to_target(target
, dev
->num_pages
);
742 static int virtio_balloon_post_load_device(void *opaque
, int version_id
)
744 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
746 if (balloon_stats_enabled(s
)) {
747 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
752 static const VMStateDescription vmstate_virtio_balloon_free_page_report
= {
753 .name
= "virtio-balloon-device/free-page-report",
755 .minimum_version_id
= 1,
756 .needed
= virtio_balloon_free_page_support
,
757 .fields
= (VMStateField
[]) {
758 VMSTATE_UINT32(free_page_report_cmd_id
, VirtIOBalloon
),
759 VMSTATE_UINT32(free_page_report_status
, VirtIOBalloon
),
760 VMSTATE_END_OF_LIST()
764 static const VMStateDescription vmstate_virtio_balloon_device
= {
765 .name
= "virtio-balloon-device",
767 .minimum_version_id
= 1,
768 .post_load
= virtio_balloon_post_load_device
,
769 .fields
= (VMStateField
[]) {
770 VMSTATE_UINT32(num_pages
, VirtIOBalloon
),
771 VMSTATE_UINT32(actual
, VirtIOBalloon
),
772 VMSTATE_END_OF_LIST()
774 .subsections
= (const VMStateDescription
* []) {
775 &vmstate_virtio_balloon_free_page_report
,
780 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
782 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
783 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
786 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
787 virtio_balloon_config_size(s
));
789 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
790 virtio_balloon_stat
, s
);
793 error_setg(errp
, "Only one balloon device is supported");
794 virtio_cleanup(vdev
);
798 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
799 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
800 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
802 if (virtio_has_feature(s
->host_features
,
803 VIRTIO_BALLOON_F_FREE_PAGE_HINT
)) {
804 s
->free_page_vq
= virtio_add_queue(vdev
, VIRTQUEUE_MAX_SIZE
,
805 virtio_balloon_handle_free_page_vq
);
806 s
->free_page_report_status
= FREE_PAGE_REPORT_S_STOP
;
807 s
->free_page_report_cmd_id
=
808 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN
;
809 s
->free_page_report_notify
.notify
=
810 virtio_balloon_free_page_report_notify
;
811 precopy_add_notifier(&s
->free_page_report_notify
);
813 object_ref(OBJECT(s
->iothread
));
814 s
->free_page_bh
= aio_bh_new(iothread_get_aio_context(s
->iothread
),
815 virtio_ballloon_get_free_page_hints
, s
);
816 qemu_mutex_init(&s
->free_page_lock
);
817 qemu_cond_init(&s
->free_page_cond
);
818 s
->block_iothread
= false;
820 /* Simply disable this feature if the iothread wasn't created. */
821 s
->host_features
&= ~(1 << VIRTIO_BALLOON_F_FREE_PAGE_HINT
);
822 virtio_error(vdev
, "iothread is missing");
828 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
830 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
831 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
833 if (virtio_balloon_free_page_support(s
)) {
834 qemu_bh_delete(s
->free_page_bh
);
835 virtio_balloon_free_page_stop(s
);
836 precopy_remove_notifier(&s
->free_page_report_notify
);
838 balloon_stats_destroy_timer(s
);
839 qemu_remove_balloon_handler(s
);
840 virtio_cleanup(vdev
);
843 static void virtio_balloon_device_reset(VirtIODevice
*vdev
)
845 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
847 if (virtio_balloon_free_page_support(s
)) {
848 virtio_balloon_free_page_stop(s
);
851 if (s
->stats_vq_elem
!= NULL
) {
852 virtqueue_unpop(s
->svq
, s
->stats_vq_elem
, 0);
853 g_free(s
->stats_vq_elem
);
854 s
->stats_vq_elem
= NULL
;
858 static void virtio_balloon_set_status(VirtIODevice
*vdev
, uint8_t status
)
860 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
862 if (!s
->stats_vq_elem
&& vdev
->vm_running
&&
863 (status
& VIRTIO_CONFIG_S_DRIVER_OK
) && virtqueue_rewind(s
->svq
, 1)) {
864 /* poll stats queue for the element we have discarded when the VM
866 virtio_balloon_receive_stats(vdev
, s
->svq
);
869 if (virtio_balloon_free_page_support(s
)) {
871 * The VM is woken up and the iothread was blocked, so signal it to
874 if (vdev
->vm_running
&& s
->block_iothread
) {
875 qemu_mutex_lock(&s
->free_page_lock
);
876 s
->block_iothread
= false;
877 qemu_cond_signal(&s
->free_page_cond
);
878 qemu_mutex_unlock(&s
->free_page_lock
);
881 /* The VM is stopped, block the iothread. */
882 if (!vdev
->vm_running
) {
883 qemu_mutex_lock(&s
->free_page_lock
);
884 s
->block_iothread
= true;
885 qemu_mutex_unlock(&s
->free_page_lock
);
890 static void virtio_balloon_instance_init(Object
*obj
)
892 VirtIOBalloon
*s
= VIRTIO_BALLOON(obj
);
894 object_property_add(obj
, "guest-stats", "guest statistics",
895 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
897 object_property_add(obj
, "guest-stats-polling-interval", "int",
898 balloon_stats_get_poll_interval
,
899 balloon_stats_set_poll_interval
,
903 static const VMStateDescription vmstate_virtio_balloon
= {
904 .name
= "virtio-balloon",
905 .minimum_version_id
= 1,
907 .fields
= (VMStateField
[]) {
908 VMSTATE_VIRTIO_DEVICE
,
909 VMSTATE_END_OF_LIST()
913 static Property virtio_balloon_properties
[] = {
914 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon
, host_features
,
915 VIRTIO_BALLOON_F_DEFLATE_ON_OOM
, false),
916 DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon
, host_features
,
917 VIRTIO_BALLOON_F_FREE_PAGE_HINT
, false),
918 /* QEMU 4.0 accidentally changed the config size even when free-page-hint
919 * is disabled, resulting in QEMU 3.1 migration incompatibility. This
920 * property retains this quirk for QEMU 4.1 machine types.
922 DEFINE_PROP_BOOL("qemu-4-0-config-size", VirtIOBalloon
,
923 qemu_4_0_config_size
, false),
924 DEFINE_PROP_LINK("iothread", VirtIOBalloon
, iothread
, TYPE_IOTHREAD
,
926 DEFINE_PROP_END_OF_LIST(),
929 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
931 DeviceClass
*dc
= DEVICE_CLASS(klass
);
932 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
934 dc
->props
= virtio_balloon_properties
;
935 dc
->vmsd
= &vmstate_virtio_balloon
;
936 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
937 vdc
->realize
= virtio_balloon_device_realize
;
938 vdc
->unrealize
= virtio_balloon_device_unrealize
;
939 vdc
->reset
= virtio_balloon_device_reset
;
940 vdc
->get_config
= virtio_balloon_get_config
;
941 vdc
->set_config
= virtio_balloon_set_config
;
942 vdc
->get_features
= virtio_balloon_get_features
;
943 vdc
->set_status
= virtio_balloon_set_status
;
944 vdc
->vmsd
= &vmstate_virtio_balloon_device
;
947 static const TypeInfo virtio_balloon_info
= {
948 .name
= TYPE_VIRTIO_BALLOON
,
949 .parent
= TYPE_VIRTIO_DEVICE
,
950 .instance_size
= sizeof(VirtIOBalloon
),
951 .instance_init
= virtio_balloon_instance_init
,
952 .class_init
= virtio_balloon_class_init
,
955 static void virtio_register_types(void)
957 type_register_static(&virtio_balloon_info
);
960 type_init(virtio_register_types
)