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/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"
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
{
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
;
50 ram_addr_t ram_offset
, host_page_base
;
52 /* XXX is there a better way to get to the RAMBlock than via a
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
) {
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 */
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
75 "Balloon used with backing page size > 4kiB, this may not be reliable");
77 subpages
= rb_page_size
/ BALLOON_PAGE_SIZE
;
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 */
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
,
101 if (bitmap_full(balloon
->pbp
->bitmap
, subpages
)) {
102 /* We've accumulated a full host page, we can actually discard
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 */
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
137 static inline void reset_stats(VirtIOBalloon
*dev
)
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
)) {
176 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
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
)
190 VirtIOBalloon
*s
= opaque
;
193 visit_start_struct(v
, name
, NULL
, 0, &err
);
197 visit_type_int(v
, "last-update", &s
->stats_last_update
, &err
);
202 visit_start_struct(v
, "stats", NULL
, 0, &err
);
206 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
207 visit_type_uint64(v
, balloon_stat_names
[i
], &s
->stats
[i
], &err
);
212 visit_check_struct(v
, &err
);
214 visit_end_struct(v
, NULL
);
217 visit_check_struct(v
, &err
);
220 visit_end_struct(v
, NULL
);
222 error_propagate(errp
, err
);
225 static void balloon_stats_get_poll_interval(Object
*obj
, Visitor
*v
,
226 const char *name
, void *opaque
,
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
,
237 VirtIOBalloon
*s
= opaque
;
238 Error
*local_err
= NULL
;
241 visit_type_int(v
, name
, &value
, &local_err
);
243 error_propagate(errp
, local_err
);
248 error_setg(errp
, "timer value must be greater than zero");
252 if (value
> UINT32_MAX
) {
253 error_setg(errp
, "timer value is too big");
257 if (value
== s
->stats_poll_interval
) {
262 /* timer=0 disables the timer */
263 balloon_stats_destroy_timer(s
);
267 if (balloon_stats_enabled(s
)) {
268 /* timer interval change */
269 s
->stats_poll_interval
= value
;
270 balloon_stats_change_timer(s
, value
);
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
;
290 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
295 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &pfn
, 4) == 4) {
297 int p
= virtio_ldl_p(vdev
, &pfn
);
299 pa
= (hwaddr
) p
<< VIRTIO_BALLOON_PFN_SHIFT
;
302 section
= memory_region_find(get_system_memory(), pa
,
305 trace_virtio_balloon_bad_addr(pa
);
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
);
316 trace_virtio_balloon_handle_output(memory_region_name(section
.mr
),
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
);
330 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
332 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
333 VirtQueueElement
*elem
;
334 VirtIOBalloonStat stat
;
338 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
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).
358 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, 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
)
367 s
->stats_vq_offset
= offset
;
369 if (qemu_gettimeofday(&tv
) < 0) {
370 warn_report("%s: failed to get time of day", __func__
);
374 s
->stats_last_update
= tv
.tv_sec
;
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
,
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
;
396 while (dev
->block_iothread
) {
397 qemu_cond_wait(&dev
->free_page_cond
, &dev
->free_page_lock
);
400 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
407 size_t size
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
410 virtio_tswap32s(vdev
, &id
);
411 if (unlikely(size
!= sizeof(id
))) {
412 virtio_error(vdev
, "received an incorrect cmd id");
416 if (id
== dev
->free_page_report_cmd_id
) {
417 dev
->free_page_report_status
= FREE_PAGE_REPORT_S_START
;
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
;
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
);
437 virtqueue_push(vq
, elem
, 1);
442 static void virtio_ballloon_get_free_page_hints(void *opaque
)
444 VirtIOBalloon
*dev
= opaque
;
445 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
446 VirtQueue
*vq
= dev
->free_page_vq
;
447 bool continue_to_get_hints
;
450 qemu_mutex_lock(&dev
->free_page_lock
);
451 virtio_queue_set_notification(vq
, 0);
452 continue_to_get_hints
= get_free_page_hints(dev
);
453 qemu_mutex_unlock(&dev
->free_page_lock
);
454 virtio_notify(vdev
, vq
);
456 * Start to poll the vq once the reporting started. Otherwise, continue
457 * only when there are entries on the vq, which need to be given back.
459 } while (continue_to_get_hints
||
460 dev
->free_page_report_status
== FREE_PAGE_REPORT_S_START
);
461 virtio_queue_set_notification(vq
, 1);
464 static bool virtio_balloon_free_page_support(void *opaque
)
466 VirtIOBalloon
*s
= opaque
;
467 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
469 return virtio_vdev_has_feature(vdev
, VIRTIO_BALLOON_F_FREE_PAGE_HINT
);
472 static void virtio_balloon_free_page_start(VirtIOBalloon
*s
)
474 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
476 /* For the stop and copy phase, we don't need to start the optimization */
477 if (!vdev
->vm_running
) {
481 if (s
->free_page_report_cmd_id
== UINT_MAX
) {
482 s
->free_page_report_cmd_id
=
483 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN
;
485 s
->free_page_report_cmd_id
++;
488 s
->free_page_report_status
= FREE_PAGE_REPORT_S_REQUESTED
;
489 virtio_notify_config(vdev
);
492 static void virtio_balloon_free_page_stop(VirtIOBalloon
*s
)
494 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
496 if (s
->free_page_report_status
!= FREE_PAGE_REPORT_S_STOP
) {
498 * The lock also guarantees us that the
499 * virtio_ballloon_get_free_page_hints exits after the
500 * free_page_report_status is set to S_STOP.
502 qemu_mutex_lock(&s
->free_page_lock
);
504 * The guest hasn't done the reporting, so host sends a notification
505 * to the guest to actively stop the reporting.
507 s
->free_page_report_status
= FREE_PAGE_REPORT_S_STOP
;
508 qemu_mutex_unlock(&s
->free_page_lock
);
509 virtio_notify_config(vdev
);
513 static void virtio_balloon_free_page_done(VirtIOBalloon
*s
)
515 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
517 s
->free_page_report_status
= FREE_PAGE_REPORT_S_DONE
;
518 virtio_notify_config(vdev
);
522 virtio_balloon_free_page_report_notify(NotifierWithReturn
*n
, void *data
)
524 VirtIOBalloon
*dev
= container_of(n
, VirtIOBalloon
,
525 free_page_report_notify
);
526 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
527 PrecopyNotifyData
*pnd
= data
;
529 if (!virtio_balloon_free_page_support(dev
)) {
531 * This is an optimization provided to migration, so just return 0 to
532 * have the normal migration process not affected when this feature is
538 switch (pnd
->reason
) {
539 case PRECOPY_NOTIFY_SETUP
:
540 precopy_enable_free_page_optimization();
542 case PRECOPY_NOTIFY_COMPLETE
:
543 case PRECOPY_NOTIFY_CLEANUP
:
544 case PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC
:
545 virtio_balloon_free_page_stop(dev
);
547 case PRECOPY_NOTIFY_AFTER_BITMAP_SYNC
:
548 if (vdev
->vm_running
) {
549 virtio_balloon_free_page_start(dev
);
551 virtio_balloon_free_page_done(dev
);
555 virtio_error(vdev
, "%s: %d reason unknown", __func__
, pnd
->reason
);
561 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
563 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
564 struct virtio_balloon_config config
= {};
566 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
567 config
.actual
= cpu_to_le32(dev
->actual
);
569 if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_REQUESTED
) {
570 config
.free_page_report_cmd_id
=
571 cpu_to_le32(dev
->free_page_report_cmd_id
);
572 } else if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_STOP
) {
573 config
.free_page_report_cmd_id
=
574 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_STOP
);
575 } else if (dev
->free_page_report_status
== FREE_PAGE_REPORT_S_DONE
) {
576 config
.free_page_report_cmd_id
=
577 cpu_to_le32(VIRTIO_BALLOON_CMD_ID_DONE
);
580 trace_virtio_balloon_get_config(config
.num_pages
, config
.actual
);
581 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
584 static int build_dimm_list(Object
*obj
, void *opaque
)
586 GSList
**list
= opaque
;
588 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
589 DeviceState
*dev
= DEVICE(obj
);
590 if (dev
->realized
) { /* only realized DIMMs matter */
591 *list
= g_slist_prepend(*list
, dev
);
595 object_child_foreach(obj
, build_dimm_list
, opaque
);
599 static ram_addr_t
get_current_ram_size(void)
601 GSList
*list
= NULL
, *item
;
602 ram_addr_t size
= ram_size
;
604 build_dimm_list(qdev_get_machine(), &list
);
605 for (item
= list
; item
; item
= g_slist_next(item
)) {
606 Object
*obj
= OBJECT(item
->data
);
607 if (!strcmp(object_get_typename(obj
), TYPE_PC_DIMM
)) {
608 size
+= object_property_get_int(obj
, PC_DIMM_SIZE_PROP
,
617 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
618 const uint8_t *config_data
)
620 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
621 struct virtio_balloon_config config
;
622 uint32_t oldactual
= dev
->actual
;
623 ram_addr_t vm_ram_size
= get_current_ram_size();
625 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
626 dev
->actual
= le32_to_cpu(config
.actual
);
627 if (dev
->actual
!= oldactual
) {
628 qapi_event_send_balloon_change(vm_ram_size
-
629 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
631 trace_virtio_balloon_set_config(dev
->actual
, oldactual
);
634 static uint64_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint64_t f
,
637 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
638 f
|= dev
->host_features
;
639 virtio_add_feature(&f
, VIRTIO_BALLOON_F_STATS_VQ
);
644 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
646 VirtIOBalloon
*dev
= opaque
;
647 info
->actual
= get_current_ram_size() - ((uint64_t) dev
->actual
<<
648 VIRTIO_BALLOON_PFN_SHIFT
);
651 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
653 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
654 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
655 ram_addr_t vm_ram_size
= get_current_ram_size();
657 if (target
> vm_ram_size
) {
658 target
= vm_ram_size
;
661 dev
->num_pages
= (vm_ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
662 virtio_notify_config(vdev
);
664 trace_virtio_balloon_to_target(target
, dev
->num_pages
);
667 static int virtio_balloon_post_load_device(void *opaque
, int version_id
)
669 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
671 if (balloon_stats_enabled(s
)) {
672 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
677 static const VMStateDescription vmstate_virtio_balloon_free_page_report
= {
678 .name
= "virtio-balloon-device/free-page-report",
680 .minimum_version_id
= 1,
681 .needed
= virtio_balloon_free_page_support
,
682 .fields
= (VMStateField
[]) {
683 VMSTATE_UINT32(free_page_report_cmd_id
, VirtIOBalloon
),
684 VMSTATE_UINT32(free_page_report_status
, VirtIOBalloon
),
685 VMSTATE_END_OF_LIST()
689 static const VMStateDescription vmstate_virtio_balloon_device
= {
690 .name
= "virtio-balloon-device",
692 .minimum_version_id
= 1,
693 .post_load
= virtio_balloon_post_load_device
,
694 .fields
= (VMStateField
[]) {
695 VMSTATE_UINT32(num_pages
, VirtIOBalloon
),
696 VMSTATE_UINT32(actual
, VirtIOBalloon
),
697 VMSTATE_END_OF_LIST()
699 .subsections
= (const VMStateDescription
* []) {
700 &vmstate_virtio_balloon_free_page_report
,
705 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
707 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
708 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
711 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
712 sizeof(struct virtio_balloon_config
));
714 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
715 virtio_balloon_stat
, s
);
718 error_setg(errp
, "Only one balloon device is supported");
719 virtio_cleanup(vdev
);
723 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
724 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
725 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
727 if (virtio_has_feature(s
->host_features
,
728 VIRTIO_BALLOON_F_FREE_PAGE_HINT
)) {
729 s
->free_page_vq
= virtio_add_queue(vdev
, VIRTQUEUE_MAX_SIZE
,
730 virtio_balloon_handle_free_page_vq
);
731 s
->free_page_report_status
= FREE_PAGE_REPORT_S_STOP
;
732 s
->free_page_report_cmd_id
=
733 VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN
;
734 s
->free_page_report_notify
.notify
=
735 virtio_balloon_free_page_report_notify
;
736 precopy_add_notifier(&s
->free_page_report_notify
);
738 object_ref(OBJECT(s
->iothread
));
739 s
->free_page_bh
= aio_bh_new(iothread_get_aio_context(s
->iothread
),
740 virtio_ballloon_get_free_page_hints
, s
);
741 qemu_mutex_init(&s
->free_page_lock
);
742 qemu_cond_init(&s
->free_page_cond
);
743 s
->block_iothread
= false;
745 /* Simply disable this feature if the iothread wasn't created. */
746 s
->host_features
&= ~(1 << VIRTIO_BALLOON_F_FREE_PAGE_HINT
);
747 virtio_error(vdev
, "iothread is missing");
753 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
755 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
756 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
758 if (virtio_balloon_free_page_support(s
)) {
759 qemu_bh_delete(s
->free_page_bh
);
760 virtio_balloon_free_page_stop(s
);
761 precopy_remove_notifier(&s
->free_page_report_notify
);
763 balloon_stats_destroy_timer(s
);
764 qemu_remove_balloon_handler(s
);
765 virtio_cleanup(vdev
);
768 static void virtio_balloon_device_reset(VirtIODevice
*vdev
)
770 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
772 if (virtio_balloon_free_page_support(s
)) {
773 virtio_balloon_free_page_stop(s
);
776 if (s
->stats_vq_elem
!= NULL
) {
777 virtqueue_unpop(s
->svq
, s
->stats_vq_elem
, 0);
778 g_free(s
->stats_vq_elem
);
779 s
->stats_vq_elem
= NULL
;
783 static void virtio_balloon_set_status(VirtIODevice
*vdev
, uint8_t status
)
785 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
787 if (!s
->stats_vq_elem
&& vdev
->vm_running
&&
788 (status
& VIRTIO_CONFIG_S_DRIVER_OK
) && virtqueue_rewind(s
->svq
, 1)) {
789 /* poll stats queue for the element we have discarded when the VM
791 virtio_balloon_receive_stats(vdev
, s
->svq
);
794 if (virtio_balloon_free_page_support(s
)) {
796 * The VM is woken up and the iothread was blocked, so signal it to
799 if (vdev
->vm_running
&& s
->block_iothread
) {
800 qemu_mutex_lock(&s
->free_page_lock
);
801 s
->block_iothread
= false;
802 qemu_cond_signal(&s
->free_page_cond
);
803 qemu_mutex_unlock(&s
->free_page_lock
);
806 /* The VM is stopped, block the iothread. */
807 if (!vdev
->vm_running
) {
808 qemu_mutex_lock(&s
->free_page_lock
);
809 s
->block_iothread
= true;
810 qemu_mutex_unlock(&s
->free_page_lock
);
815 static void virtio_balloon_instance_init(Object
*obj
)
817 VirtIOBalloon
*s
= VIRTIO_BALLOON(obj
);
819 object_property_add(obj
, "guest-stats", "guest statistics",
820 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
822 object_property_add(obj
, "guest-stats-polling-interval", "int",
823 balloon_stats_get_poll_interval
,
824 balloon_stats_set_poll_interval
,
828 static const VMStateDescription vmstate_virtio_balloon
= {
829 .name
= "virtio-balloon",
830 .minimum_version_id
= 1,
832 .fields
= (VMStateField
[]) {
833 VMSTATE_VIRTIO_DEVICE
,
834 VMSTATE_END_OF_LIST()
838 static Property virtio_balloon_properties
[] = {
839 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon
, host_features
,
840 VIRTIO_BALLOON_F_DEFLATE_ON_OOM
, false),
841 DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon
, host_features
,
842 VIRTIO_BALLOON_F_FREE_PAGE_HINT
, false),
843 DEFINE_PROP_LINK("iothread", VirtIOBalloon
, iothread
, TYPE_IOTHREAD
,
845 DEFINE_PROP_END_OF_LIST(),
848 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
850 DeviceClass
*dc
= DEVICE_CLASS(klass
);
851 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
853 dc
->props
= virtio_balloon_properties
;
854 dc
->vmsd
= &vmstate_virtio_balloon
;
855 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
856 vdc
->realize
= virtio_balloon_device_realize
;
857 vdc
->unrealize
= virtio_balloon_device_unrealize
;
858 vdc
->reset
= virtio_balloon_device_reset
;
859 vdc
->get_config
= virtio_balloon_get_config
;
860 vdc
->set_config
= virtio_balloon_set_config
;
861 vdc
->get_features
= virtio_balloon_get_features
;
862 vdc
->set_status
= virtio_balloon_set_status
;
863 vdc
->vmsd
= &vmstate_virtio_balloon_device
;
866 static const TypeInfo virtio_balloon_info
= {
867 .name
= TYPE_VIRTIO_BALLOON
,
868 .parent
= TYPE_VIRTIO_DEVICE
,
869 .instance_size
= sizeof(VirtIOBalloon
),
870 .instance_init
= virtio_balloon_instance_init
,
871 .class_init
= virtio_balloon_class_init
,
874 static void virtio_register_types(void)
876 type_register_static(&virtio_balloon_info
);
879 type_init(virtio_register_types
)