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 static void balloon_page(void *addr
, int deflate
)
38 if (!qemu_balloon_is_inhibited()) {
39 qemu_madvise(addr
, BALLOON_PAGE_SIZE
,
40 deflate
? QEMU_MADV_WILLNEED
: QEMU_MADV_DONTNEED
);
44 static const char *balloon_stat_names
[] = {
45 [VIRTIO_BALLOON_S_SWAP_IN
] = "stat-swap-in",
46 [VIRTIO_BALLOON_S_SWAP_OUT
] = "stat-swap-out",
47 [VIRTIO_BALLOON_S_MAJFLT
] = "stat-major-faults",
48 [VIRTIO_BALLOON_S_MINFLT
] = "stat-minor-faults",
49 [VIRTIO_BALLOON_S_MEMFREE
] = "stat-free-memory",
50 [VIRTIO_BALLOON_S_MEMTOT
] = "stat-total-memory",
51 [VIRTIO_BALLOON_S_AVAIL
] = "stat-available-memory",
52 [VIRTIO_BALLOON_S_CACHES
] = "stat-disk-caches",
53 [VIRTIO_BALLOON_S_HTLB_PGALLOC
] = "stat-htlb-pgalloc",
54 [VIRTIO_BALLOON_S_HTLB_PGFAIL
] = "stat-htlb-pgfail",
55 [VIRTIO_BALLOON_S_NR
] = NULL
59 * reset_stats - Mark all items in the stats array as unset
61 * This function needs to be called at device initialization and before
62 * updating to a set of newly-generated stats. This will ensure that no
63 * stale values stick around in case the guest reports a subset of the supported
66 static inline void reset_stats(VirtIOBalloon
*dev
)
69 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
72 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
74 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
75 return virtio_vdev_has_feature(vdev
, VIRTIO_BALLOON_F_STATS_VQ
);
78 static bool balloon_stats_enabled(const VirtIOBalloon
*s
)
80 return s
->stats_poll_interval
> 0;
83 static void balloon_stats_destroy_timer(VirtIOBalloon
*s
)
85 if (balloon_stats_enabled(s
)) {
86 timer_del(s
->stats_timer
);
87 timer_free(s
->stats_timer
);
88 s
->stats_timer
= NULL
;
89 s
->stats_poll_interval
= 0;
93 static void balloon_stats_change_timer(VirtIOBalloon
*s
, int64_t secs
)
95 timer_mod(s
->stats_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + secs
* 1000);
98 static void balloon_stats_poll_cb(void *opaque
)
100 VirtIOBalloon
*s
= opaque
;
101 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
103 if (s
->stats_vq_elem
== NULL
|| !balloon_stats_supported(s
)) {
105 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
109 virtqueue_push(s
->svq
, s
->stats_vq_elem
, s
->stats_vq_offset
);
110 virtio_notify(vdev
, s
->svq
);
111 g_free(s
->stats_vq_elem
);
112 s
->stats_vq_elem
= NULL
;
115 static void balloon_stats_get_all(Object
*obj
, Visitor
*v
, const char *name
,
116 void *opaque
, Error
**errp
)
119 VirtIOBalloon
*s
= opaque
;
122 visit_start_struct(v
, name
, NULL
, 0, &err
);
126 visit_type_int(v
, "last-update", &s
->stats_last_update
, &err
);
131 visit_start_struct(v
, "stats", NULL
, 0, &err
);
135 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
136 visit_type_uint64(v
, balloon_stat_names
[i
], &s
->stats
[i
], &err
);
141 visit_check_struct(v
, &err
);
143 visit_end_struct(v
, NULL
);
146 visit_check_struct(v
, &err
);
149 visit_end_struct(v
, NULL
);
151 error_propagate(errp
, err
);
154 static void balloon_stats_get_poll_interval(Object
*obj
, Visitor
*v
,
155 const char *name
, void *opaque
,
158 VirtIOBalloon
*s
= opaque
;
159 visit_type_int(v
, name
, &s
->stats_poll_interval
, errp
);
162 static void balloon_stats_set_poll_interval(Object
*obj
, Visitor
*v
,
163 const char *name
, void *opaque
,
166 VirtIOBalloon
*s
= opaque
;
167 Error
*local_err
= NULL
;
170 visit_type_int(v
, name
, &value
, &local_err
);
172 error_propagate(errp
, local_err
);
177 error_setg(errp
, "timer value must be greater than zero");
181 if (value
> UINT32_MAX
) {
182 error_setg(errp
, "timer value is too big");
186 if (value
== s
->stats_poll_interval
) {
191 /* timer=0 disables the timer */
192 balloon_stats_destroy_timer(s
);
196 if (balloon_stats_enabled(s
)) {
197 /* timer interval change */
198 s
->stats_poll_interval
= value
;
199 balloon_stats_change_timer(s
, value
);
203 /* create a new timer */
204 g_assert(s
->stats_timer
== NULL
);
205 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
206 s
->stats_poll_interval
= value
;
207 balloon_stats_change_timer(s
, 0);
210 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
212 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
213 VirtQueueElement
*elem
;
214 MemoryRegionSection section
;
219 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
224 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &pfn
, 4) == 4) {
227 int p
= virtio_ldl_p(vdev
, &pfn
);
229 pa
= (ram_addr_t
) p
<< VIRTIO_BALLOON_PFN_SHIFT
;
232 /* FIXME: remove get_system_memory(), but how? */
233 section
= memory_region_find(get_system_memory(), pa
, 1);
234 if (!int128_nz(section
.size
) ||
235 !memory_region_is_ram(section
.mr
) ||
236 memory_region_is_rom(section
.mr
) ||
237 memory_region_is_romd(section
.mr
)) {
238 trace_virtio_balloon_bad_addr(pa
);
239 memory_region_unref(section
.mr
);
243 trace_virtio_balloon_handle_output(memory_region_name(section
.mr
),
245 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
246 should be OK because we only want a single page. */
247 addr
= section
.offset_within_region
;
248 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
250 memory_region_unref(section
.mr
);
253 virtqueue_push(vq
, elem
, offset
);
254 virtio_notify(vdev
, vq
);
259 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
261 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
262 VirtQueueElement
*elem
;
263 VirtIOBalloonStat stat
;
267 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
272 if (s
->stats_vq_elem
!= NULL
) {
273 /* This should never happen if the driver follows the spec. */
274 virtqueue_push(vq
, s
->stats_vq_elem
, 0);
275 virtio_notify(vdev
, vq
);
276 g_free(s
->stats_vq_elem
);
279 s
->stats_vq_elem
= elem
;
281 /* Initialize the stats to get rid of any stale values. This is only
282 * needed to handle the case where a guest supports fewer stats than it
283 * used to (ie. it has booted into an old kernel).
287 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
289 uint16_t tag
= virtio_tswap16(vdev
, stat
.tag
);
290 uint64_t val
= virtio_tswap64(vdev
, stat
.val
);
292 offset
+= sizeof(stat
);
293 if (tag
< VIRTIO_BALLOON_S_NR
)
296 s
->stats_vq_offset
= offset
;
298 if (qemu_gettimeofday(&tv
) < 0) {
299 warn_report("%s: failed to get time of day", __func__
);
303 s
->stats_last_update
= tv
.tv_sec
;
306 if (balloon_stats_enabled(s
)) {
307 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
311 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
313 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
314 struct virtio_balloon_config config
;
316 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
317 config
.actual
= cpu_to_le32(dev
->actual
);
319 trace_virtio_balloon_get_config(config
.num_pages
, config
.actual
);
320 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
323 static int build_dimm_list(Object
*obj
, void *opaque
)
325 GSList
**list
= opaque
;
327 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
328 DeviceState
*dev
= DEVICE(obj
);
329 if (dev
->realized
) { /* only realized DIMMs matter */
330 *list
= g_slist_prepend(*list
, dev
);
334 object_child_foreach(obj
, build_dimm_list
, opaque
);
338 static ram_addr_t
get_current_ram_size(void)
340 GSList
*list
= NULL
, *item
;
341 ram_addr_t size
= ram_size
;
343 build_dimm_list(qdev_get_machine(), &list
);
344 for (item
= list
; item
; item
= g_slist_next(item
)) {
345 Object
*obj
= OBJECT(item
->data
);
346 if (!strcmp(object_get_typename(obj
), TYPE_PC_DIMM
)) {
347 size
+= object_property_get_int(obj
, PC_DIMM_SIZE_PROP
,
356 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
357 const uint8_t *config_data
)
359 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
360 struct virtio_balloon_config config
;
361 uint32_t oldactual
= dev
->actual
;
362 ram_addr_t vm_ram_size
= get_current_ram_size();
364 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
365 dev
->actual
= le32_to_cpu(config
.actual
);
366 if (dev
->actual
!= oldactual
) {
367 qapi_event_send_balloon_change(vm_ram_size
-
368 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
370 trace_virtio_balloon_set_config(dev
->actual
, oldactual
);
373 static uint64_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint64_t f
,
376 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
377 f
|= dev
->host_features
;
378 virtio_add_feature(&f
, VIRTIO_BALLOON_F_STATS_VQ
);
382 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
384 VirtIOBalloon
*dev
= opaque
;
385 info
->actual
= get_current_ram_size() - ((uint64_t) dev
->actual
<<
386 VIRTIO_BALLOON_PFN_SHIFT
);
389 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
391 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
392 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
393 ram_addr_t vm_ram_size
= get_current_ram_size();
395 if (target
> vm_ram_size
) {
396 target
= vm_ram_size
;
399 dev
->num_pages
= (vm_ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
400 virtio_notify_config(vdev
);
402 trace_virtio_balloon_to_target(target
, dev
->num_pages
);
405 static int virtio_balloon_post_load_device(void *opaque
, int version_id
)
407 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
409 if (balloon_stats_enabled(s
)) {
410 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
415 static const VMStateDescription vmstate_virtio_balloon_device
= {
416 .name
= "virtio-balloon-device",
418 .minimum_version_id
= 1,
419 .post_load
= virtio_balloon_post_load_device
,
420 .fields
= (VMStateField
[]) {
421 VMSTATE_UINT32(num_pages
, VirtIOBalloon
),
422 VMSTATE_UINT32(actual
, VirtIOBalloon
),
423 VMSTATE_END_OF_LIST()
427 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
429 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
430 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
433 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
434 sizeof(struct virtio_balloon_config
));
436 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
437 virtio_balloon_stat
, s
);
440 error_setg(errp
, "Only one balloon device is supported");
441 virtio_cleanup(vdev
);
445 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
446 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
447 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
452 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
454 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
455 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
457 balloon_stats_destroy_timer(s
);
458 qemu_remove_balloon_handler(s
);
459 virtio_cleanup(vdev
);
462 static void virtio_balloon_device_reset(VirtIODevice
*vdev
)
464 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
466 if (s
->stats_vq_elem
!= NULL
) {
467 virtqueue_unpop(s
->svq
, s
->stats_vq_elem
, 0);
468 g_free(s
->stats_vq_elem
);
469 s
->stats_vq_elem
= NULL
;
473 static void virtio_balloon_set_status(VirtIODevice
*vdev
, uint8_t status
)
475 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
477 if (!s
->stats_vq_elem
&& vdev
->vm_running
&&
478 (status
& VIRTIO_CONFIG_S_DRIVER_OK
) && virtqueue_rewind(s
->svq
, 1)) {
479 /* poll stats queue for the element we have discarded when the VM
481 virtio_balloon_receive_stats(vdev
, s
->svq
);
485 static void virtio_balloon_instance_init(Object
*obj
)
487 VirtIOBalloon
*s
= VIRTIO_BALLOON(obj
);
489 object_property_add(obj
, "guest-stats", "guest statistics",
490 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
492 object_property_add(obj
, "guest-stats-polling-interval", "int",
493 balloon_stats_get_poll_interval
,
494 balloon_stats_set_poll_interval
,
498 static const VMStateDescription vmstate_virtio_balloon
= {
499 .name
= "virtio-balloon",
500 .minimum_version_id
= 1,
502 .fields
= (VMStateField
[]) {
503 VMSTATE_VIRTIO_DEVICE
,
504 VMSTATE_END_OF_LIST()
508 static Property virtio_balloon_properties
[] = {
509 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon
, host_features
,
510 VIRTIO_BALLOON_F_DEFLATE_ON_OOM
, false),
511 DEFINE_PROP_END_OF_LIST(),
514 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
516 DeviceClass
*dc
= DEVICE_CLASS(klass
);
517 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
519 dc
->props
= virtio_balloon_properties
;
520 dc
->vmsd
= &vmstate_virtio_balloon
;
521 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
522 vdc
->realize
= virtio_balloon_device_realize
;
523 vdc
->unrealize
= virtio_balloon_device_unrealize
;
524 vdc
->reset
= virtio_balloon_device_reset
;
525 vdc
->get_config
= virtio_balloon_get_config
;
526 vdc
->set_config
= virtio_balloon_set_config
;
527 vdc
->get_features
= virtio_balloon_get_features
;
528 vdc
->set_status
= virtio_balloon_set_status
;
529 vdc
->vmsd
= &vmstate_virtio_balloon_device
;
532 static const TypeInfo virtio_balloon_info
= {
533 .name
= TYPE_VIRTIO_BALLOON
,
534 .parent
= TYPE_VIRTIO_DEVICE
,
535 .instance_size
= sizeof(VirtIOBalloon
),
536 .instance_init
= virtio_balloon_instance_init
,
537 .class_init
= virtio_balloon_class_init
,
540 static void virtio_register_types(void)
542 type_register_static(&virtio_balloon_info
);
545 type_init(virtio_register_types
)