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"
31 #include "hw/virtio/virtio-bus.h"
32 #include "hw/virtio/virtio-access.h"
34 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)
36 struct PartiallyBalloonedPage
{
39 unsigned long bitmap
[];
42 static void balloon_inflate_page(VirtIOBalloon
*balloon
,
43 MemoryRegion
*mr
, hwaddr offset
)
45 void *addr
= memory_region_get_ram_ptr(mr
) + offset
;
49 ram_addr_t ram_offset
, host_page_base
;
51 /* XXX is there a better way to get to the RAMBlock than via a
53 rb
= qemu_ram_block_from_host(addr
, false, &ram_offset
);
54 rb_page_size
= qemu_ram_pagesize(rb
);
55 host_page_base
= ram_offset
& ~(rb_page_size
- 1);
57 if (rb_page_size
== BALLOON_PAGE_SIZE
) {
60 ram_block_discard_range(rb
, ram_offset
, rb_page_size
);
61 /* We ignore errors from ram_block_discard_range(), because it
62 * has already reported them, and failing to discard a balloon
63 * page is not fatal */
69 * We've put a piece of a larger host page into the balloon - we
70 * need to keep track until we have a whole host page to
74 "Balloon used with backing page size > 4kiB, this may not be reliable");
76 subpages
= rb_page_size
/ BALLOON_PAGE_SIZE
;
79 && (rb
!= balloon
->pbp
->rb
80 || host_page_base
!= balloon
->pbp
->base
)) {
81 /* We've partially ballooned part of a host page, but now
82 * we're trying to balloon part of a different one. Too hard,
83 * give up on the old partial page */
89 /* Starting on a new host page */
90 size_t bitlen
= BITS_TO_LONGS(subpages
) * sizeof(unsigned long);
91 balloon
->pbp
= g_malloc0(sizeof(PartiallyBalloonedPage
) + bitlen
);
92 balloon
->pbp
->rb
= rb
;
93 balloon
->pbp
->base
= host_page_base
;
96 bitmap_set(balloon
->pbp
->bitmap
,
97 (ram_offset
- balloon
->pbp
->base
) / BALLOON_PAGE_SIZE
,
100 if (bitmap_full(balloon
->pbp
->bitmap
, subpages
)) {
101 /* We've accumulated a full host page, we can actually discard
104 ram_block_discard_range(rb
, balloon
->pbp
->base
, rb_page_size
);
105 /* We ignore errors from ram_block_discard_range(), because it
106 * has already reported them, and failing to discard a balloon
107 * page is not fatal */
114 static const char *balloon_stat_names
[] = {
115 [VIRTIO_BALLOON_S_SWAP_IN
] = "stat-swap-in",
116 [VIRTIO_BALLOON_S_SWAP_OUT
] = "stat-swap-out",
117 [VIRTIO_BALLOON_S_MAJFLT
] = "stat-major-faults",
118 [VIRTIO_BALLOON_S_MINFLT
] = "stat-minor-faults",
119 [VIRTIO_BALLOON_S_MEMFREE
] = "stat-free-memory",
120 [VIRTIO_BALLOON_S_MEMTOT
] = "stat-total-memory",
121 [VIRTIO_BALLOON_S_AVAIL
] = "stat-available-memory",
122 [VIRTIO_BALLOON_S_CACHES
] = "stat-disk-caches",
123 [VIRTIO_BALLOON_S_HTLB_PGALLOC
] = "stat-htlb-pgalloc",
124 [VIRTIO_BALLOON_S_HTLB_PGFAIL
] = "stat-htlb-pgfail",
125 [VIRTIO_BALLOON_S_NR
] = NULL
129 * reset_stats - Mark all items in the stats array as unset
131 * This function needs to be called at device initialization and before
132 * updating to a set of newly-generated stats. This will ensure that no
133 * stale values stick around in case the guest reports a subset of the supported
136 static inline void reset_stats(VirtIOBalloon
*dev
)
139 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
142 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
144 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
145 return virtio_vdev_has_feature(vdev
, VIRTIO_BALLOON_F_STATS_VQ
);
148 static bool balloon_stats_enabled(const VirtIOBalloon
*s
)
150 return s
->stats_poll_interval
> 0;
153 static void balloon_stats_destroy_timer(VirtIOBalloon
*s
)
155 if (balloon_stats_enabled(s
)) {
156 timer_del(s
->stats_timer
);
157 timer_free(s
->stats_timer
);
158 s
->stats_timer
= NULL
;
159 s
->stats_poll_interval
= 0;
163 static void balloon_stats_change_timer(VirtIOBalloon
*s
, int64_t secs
)
165 timer_mod(s
->stats_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + secs
* 1000);
168 static void balloon_stats_poll_cb(void *opaque
)
170 VirtIOBalloon
*s
= opaque
;
171 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
173 if (s
->stats_vq_elem
== NULL
|| !balloon_stats_supported(s
)) {
175 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
179 virtqueue_push(s
->svq
, s
->stats_vq_elem
, s
->stats_vq_offset
);
180 virtio_notify(vdev
, s
->svq
);
181 g_free(s
->stats_vq_elem
);
182 s
->stats_vq_elem
= NULL
;
185 static void balloon_stats_get_all(Object
*obj
, Visitor
*v
, const char *name
,
186 void *opaque
, Error
**errp
)
189 VirtIOBalloon
*s
= opaque
;
192 visit_start_struct(v
, name
, NULL
, 0, &err
);
196 visit_type_int(v
, "last-update", &s
->stats_last_update
, &err
);
201 visit_start_struct(v
, "stats", NULL
, 0, &err
);
205 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
206 visit_type_uint64(v
, balloon_stat_names
[i
], &s
->stats
[i
], &err
);
211 visit_check_struct(v
, &err
);
213 visit_end_struct(v
, NULL
);
216 visit_check_struct(v
, &err
);
219 visit_end_struct(v
, NULL
);
221 error_propagate(errp
, err
);
224 static void balloon_stats_get_poll_interval(Object
*obj
, Visitor
*v
,
225 const char *name
, void *opaque
,
228 VirtIOBalloon
*s
= opaque
;
229 visit_type_int(v
, name
, &s
->stats_poll_interval
, errp
);
232 static void balloon_stats_set_poll_interval(Object
*obj
, Visitor
*v
,
233 const char *name
, void *opaque
,
236 VirtIOBalloon
*s
= opaque
;
237 Error
*local_err
= NULL
;
240 visit_type_int(v
, name
, &value
, &local_err
);
242 error_propagate(errp
, local_err
);
247 error_setg(errp
, "timer value must be greater than zero");
251 if (value
> UINT32_MAX
) {
252 error_setg(errp
, "timer value is too big");
256 if (value
== s
->stats_poll_interval
) {
261 /* timer=0 disables the timer */
262 balloon_stats_destroy_timer(s
);
266 if (balloon_stats_enabled(s
)) {
267 /* timer interval change */
268 s
->stats_poll_interval
= value
;
269 balloon_stats_change_timer(s
, value
);
273 /* create a new timer */
274 g_assert(s
->stats_timer
== NULL
);
275 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
276 s
->stats_poll_interval
= value
;
277 balloon_stats_change_timer(s
, 0);
280 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
282 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
283 VirtQueueElement
*elem
;
284 MemoryRegionSection section
;
289 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
294 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &pfn
, 4) == 4) {
296 int p
= virtio_ldl_p(vdev
, &pfn
);
298 pa
= (hwaddr
) p
<< VIRTIO_BALLOON_PFN_SHIFT
;
301 section
= memory_region_find(get_system_memory(), pa
,
304 trace_virtio_balloon_bad_addr(pa
);
307 if (!memory_region_is_ram(section
.mr
) ||
308 memory_region_is_rom(section
.mr
) ||
309 memory_region_is_romd(section
.mr
)) {
310 trace_virtio_balloon_bad_addr(pa
);
311 memory_region_unref(section
.mr
);
315 trace_virtio_balloon_handle_output(memory_region_name(section
.mr
),
317 if (!qemu_balloon_is_inhibited() && vq
!= s
->dvq
) {
318 balloon_inflate_page(s
, section
.mr
, section
.offset_within_region
);
320 memory_region_unref(section
.mr
);
323 virtqueue_push(vq
, elem
, offset
);
324 virtio_notify(vdev
, vq
);
329 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
331 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
332 VirtQueueElement
*elem
;
333 VirtIOBalloonStat stat
;
337 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
342 if (s
->stats_vq_elem
!= NULL
) {
343 /* This should never happen if the driver follows the spec. */
344 virtqueue_push(vq
, s
->stats_vq_elem
, 0);
345 virtio_notify(vdev
, vq
);
346 g_free(s
->stats_vq_elem
);
349 s
->stats_vq_elem
= elem
;
351 /* Initialize the stats to get rid of any stale values. This is only
352 * needed to handle the case where a guest supports fewer stats than it
353 * used to (ie. it has booted into an old kernel).
357 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
359 uint16_t tag
= virtio_tswap16(vdev
, stat
.tag
);
360 uint64_t val
= virtio_tswap64(vdev
, stat
.val
);
362 offset
+= sizeof(stat
);
363 if (tag
< VIRTIO_BALLOON_S_NR
)
366 s
->stats_vq_offset
= offset
;
368 if (qemu_gettimeofday(&tv
) < 0) {
369 warn_report("%s: failed to get time of day", __func__
);
373 s
->stats_last_update
= tv
.tv_sec
;
376 if (balloon_stats_enabled(s
)) {
377 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
381 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
383 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
384 struct virtio_balloon_config config
= {};
386 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
387 config
.actual
= cpu_to_le32(dev
->actual
);
389 trace_virtio_balloon_get_config(config
.num_pages
, config
.actual
);
390 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
393 static int build_dimm_list(Object
*obj
, void *opaque
)
395 GSList
**list
= opaque
;
397 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
398 DeviceState
*dev
= DEVICE(obj
);
399 if (dev
->realized
) { /* only realized DIMMs matter */
400 *list
= g_slist_prepend(*list
, dev
);
404 object_child_foreach(obj
, build_dimm_list
, opaque
);
408 static ram_addr_t
get_current_ram_size(void)
410 GSList
*list
= NULL
, *item
;
411 ram_addr_t size
= ram_size
;
413 build_dimm_list(qdev_get_machine(), &list
);
414 for (item
= list
; item
; item
= g_slist_next(item
)) {
415 Object
*obj
= OBJECT(item
->data
);
416 if (!strcmp(object_get_typename(obj
), TYPE_PC_DIMM
)) {
417 size
+= object_property_get_int(obj
, PC_DIMM_SIZE_PROP
,
426 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
427 const uint8_t *config_data
)
429 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
430 struct virtio_balloon_config config
;
431 uint32_t oldactual
= dev
->actual
;
432 ram_addr_t vm_ram_size
= get_current_ram_size();
434 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
435 dev
->actual
= le32_to_cpu(config
.actual
);
436 if (dev
->actual
!= oldactual
) {
437 qapi_event_send_balloon_change(vm_ram_size
-
438 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
440 trace_virtio_balloon_set_config(dev
->actual
, oldactual
);
443 static uint64_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint64_t f
,
446 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
447 f
|= dev
->host_features
;
448 virtio_add_feature(&f
, VIRTIO_BALLOON_F_STATS_VQ
);
452 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
454 VirtIOBalloon
*dev
= opaque
;
455 info
->actual
= get_current_ram_size() - ((uint64_t) dev
->actual
<<
456 VIRTIO_BALLOON_PFN_SHIFT
);
459 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
461 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
462 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
463 ram_addr_t vm_ram_size
= get_current_ram_size();
465 if (target
> vm_ram_size
) {
466 target
= vm_ram_size
;
469 dev
->num_pages
= (vm_ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
470 virtio_notify_config(vdev
);
472 trace_virtio_balloon_to_target(target
, dev
->num_pages
);
475 static int virtio_balloon_post_load_device(void *opaque
, int version_id
)
477 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
479 if (balloon_stats_enabled(s
)) {
480 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
485 static const VMStateDescription vmstate_virtio_balloon_device
= {
486 .name
= "virtio-balloon-device",
488 .minimum_version_id
= 1,
489 .post_load
= virtio_balloon_post_load_device
,
490 .fields
= (VMStateField
[]) {
491 VMSTATE_UINT32(num_pages
, VirtIOBalloon
),
492 VMSTATE_UINT32(actual
, VirtIOBalloon
),
493 VMSTATE_END_OF_LIST()
497 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
499 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
500 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
503 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
504 sizeof(struct virtio_balloon_config
));
506 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
507 virtio_balloon_stat
, s
);
510 error_setg(errp
, "Only one balloon device is supported");
511 virtio_cleanup(vdev
);
515 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
516 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
517 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
522 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
524 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
525 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
527 balloon_stats_destroy_timer(s
);
528 qemu_remove_balloon_handler(s
);
529 virtio_cleanup(vdev
);
532 static void virtio_balloon_device_reset(VirtIODevice
*vdev
)
534 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
536 if (s
->stats_vq_elem
!= NULL
) {
537 virtqueue_unpop(s
->svq
, s
->stats_vq_elem
, 0);
538 g_free(s
->stats_vq_elem
);
539 s
->stats_vq_elem
= NULL
;
543 static void virtio_balloon_set_status(VirtIODevice
*vdev
, uint8_t status
)
545 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
547 if (!s
->stats_vq_elem
&& vdev
->vm_running
&&
548 (status
& VIRTIO_CONFIG_S_DRIVER_OK
) && virtqueue_rewind(s
->svq
, 1)) {
549 /* poll stats queue for the element we have discarded when the VM
551 virtio_balloon_receive_stats(vdev
, s
->svq
);
555 static void virtio_balloon_instance_init(Object
*obj
)
557 VirtIOBalloon
*s
= VIRTIO_BALLOON(obj
);
559 object_property_add(obj
, "guest-stats", "guest statistics",
560 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
562 object_property_add(obj
, "guest-stats-polling-interval", "int",
563 balloon_stats_get_poll_interval
,
564 balloon_stats_set_poll_interval
,
568 static const VMStateDescription vmstate_virtio_balloon
= {
569 .name
= "virtio-balloon",
570 .minimum_version_id
= 1,
572 .fields
= (VMStateField
[]) {
573 VMSTATE_VIRTIO_DEVICE
,
574 VMSTATE_END_OF_LIST()
578 static Property virtio_balloon_properties
[] = {
579 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon
, host_features
,
580 VIRTIO_BALLOON_F_DEFLATE_ON_OOM
, false),
581 DEFINE_PROP_END_OF_LIST(),
584 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
586 DeviceClass
*dc
= DEVICE_CLASS(klass
);
587 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
589 dc
->props
= virtio_balloon_properties
;
590 dc
->vmsd
= &vmstate_virtio_balloon
;
591 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
592 vdc
->realize
= virtio_balloon_device_realize
;
593 vdc
->unrealize
= virtio_balloon_device_unrealize
;
594 vdc
->reset
= virtio_balloon_device_reset
;
595 vdc
->get_config
= virtio_balloon_get_config
;
596 vdc
->set_config
= virtio_balloon_set_config
;
597 vdc
->get_features
= virtio_balloon_get_features
;
598 vdc
->set_status
= virtio_balloon_set_status
;
599 vdc
->vmsd
= &vmstate_virtio_balloon_device
;
602 static const TypeInfo virtio_balloon_info
= {
603 .name
= TYPE_VIRTIO_BALLOON
,
604 .parent
= TYPE_VIRTIO_DEVICE
,
605 .instance_size
= sizeof(VirtIOBalloon
),
606 .instance_init
= virtio_balloon_instance_init
,
607 .class_init
= virtio_balloon_class_init
,
610 static void virtio_register_types(void)
612 type_register_static(&virtio_balloon_info
);
615 type_init(virtio_register_types
)