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/i386/pc.h"
22 #include "sysemu/balloon.h"
23 #include "hw/virtio/virtio-balloon.h"
24 #include "sysemu/kvm.h"
25 #include "exec/address-spaces.h"
26 #include "qapi/visitor.h"
27 #include "qapi-event.h"
30 #include "hw/virtio/virtio-bus.h"
31 #include "hw/virtio/virtio-access.h"
33 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)
35 static void balloon_page(void *addr
, int deflate
)
37 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
38 kvm_has_sync_mmu())) {
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_NR
] = NULL
56 * reset_stats - Mark all items in the stats array as unset
58 * This function needs to be called at device initialization and before
59 * updating to a set of newly-generated stats. This will ensure that no
60 * stale values stick around in case the guest reports a subset of the supported
63 static inline void reset_stats(VirtIOBalloon
*dev
)
66 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
69 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
71 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
72 return virtio_vdev_has_feature(vdev
, VIRTIO_BALLOON_F_STATS_VQ
);
75 static bool balloon_stats_enabled(const VirtIOBalloon
*s
)
77 return s
->stats_poll_interval
> 0;
80 static void balloon_stats_destroy_timer(VirtIOBalloon
*s
)
82 if (balloon_stats_enabled(s
)) {
83 timer_del(s
->stats_timer
);
84 timer_free(s
->stats_timer
);
85 s
->stats_timer
= NULL
;
86 s
->stats_poll_interval
= 0;
90 static void balloon_stats_change_timer(VirtIOBalloon
*s
, int64_t secs
)
92 timer_mod(s
->stats_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + secs
* 1000);
95 static void balloon_stats_poll_cb(void *opaque
)
97 VirtIOBalloon
*s
= opaque
;
98 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
100 if (s
->stats_vq_elem
== NULL
|| !balloon_stats_supported(s
)) {
102 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
106 virtqueue_push(s
->svq
, s
->stats_vq_elem
, s
->stats_vq_offset
);
107 virtio_notify(vdev
, s
->svq
);
108 g_free(s
->stats_vq_elem
);
109 s
->stats_vq_elem
= NULL
;
112 static void balloon_stats_get_all(Object
*obj
, Visitor
*v
, const char *name
,
113 void *opaque
, Error
**errp
)
116 VirtIOBalloon
*s
= opaque
;
119 visit_start_struct(v
, name
, NULL
, 0, &err
);
123 visit_type_int(v
, "last-update", &s
->stats_last_update
, &err
);
128 visit_start_struct(v
, "stats", NULL
, 0, &err
);
132 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
133 visit_type_uint64(v
, balloon_stat_names
[i
], &s
->stats
[i
], &err
);
138 visit_check_struct(v
, &err
);
140 visit_end_struct(v
, NULL
);
143 visit_check_struct(v
, &err
);
146 visit_end_struct(v
, NULL
);
148 error_propagate(errp
, err
);
151 static void balloon_stats_get_poll_interval(Object
*obj
, Visitor
*v
,
152 const char *name
, void *opaque
,
155 VirtIOBalloon
*s
= opaque
;
156 visit_type_int(v
, name
, &s
->stats_poll_interval
, errp
);
159 static void balloon_stats_set_poll_interval(Object
*obj
, Visitor
*v
,
160 const char *name
, void *opaque
,
163 VirtIOBalloon
*s
= opaque
;
164 Error
*local_err
= NULL
;
167 visit_type_int(v
, name
, &value
, &local_err
);
169 error_propagate(errp
, local_err
);
174 error_setg(errp
, "timer value must be greater than zero");
178 if (value
> UINT32_MAX
) {
179 error_setg(errp
, "timer value is too big");
183 if (value
== s
->stats_poll_interval
) {
188 /* timer=0 disables the timer */
189 balloon_stats_destroy_timer(s
);
193 if (balloon_stats_enabled(s
)) {
194 /* timer interval change */
195 s
->stats_poll_interval
= value
;
196 balloon_stats_change_timer(s
, value
);
200 /* create a new timer */
201 g_assert(s
->stats_timer
== NULL
);
202 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
203 s
->stats_poll_interval
= value
;
204 balloon_stats_change_timer(s
, 0);
207 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
209 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
210 VirtQueueElement
*elem
;
211 MemoryRegionSection section
;
216 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
221 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &pfn
, 4) == 4) {
224 int p
= virtio_ldl_p(vdev
, &pfn
);
226 pa
= (ram_addr_t
) p
<< VIRTIO_BALLOON_PFN_SHIFT
;
229 /* FIXME: remove get_system_memory(), but how? */
230 section
= memory_region_find(get_system_memory(), pa
, 1);
231 if (!int128_nz(section
.size
) ||
232 !memory_region_is_ram(section
.mr
) ||
233 memory_region_is_rom(section
.mr
) ||
234 memory_region_is_romd(section
.mr
)) {
235 trace_virtio_balloon_bad_addr(pa
);
239 trace_virtio_balloon_handle_output(memory_region_name(section
.mr
),
241 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
242 should be OK because we only want a single page. */
243 addr
= section
.offset_within_region
;
244 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
246 memory_region_unref(section
.mr
);
249 virtqueue_push(vq
, elem
, offset
);
250 virtio_notify(vdev
, vq
);
255 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
257 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
258 VirtQueueElement
*elem
;
259 VirtIOBalloonStat stat
;
263 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
268 if (s
->stats_vq_elem
!= NULL
) {
269 /* This should never happen if the driver follows the spec. */
270 virtqueue_push(vq
, s
->stats_vq_elem
, 0);
271 virtio_notify(vdev
, vq
);
272 g_free(s
->stats_vq_elem
);
275 s
->stats_vq_elem
= elem
;
277 /* Initialize the stats to get rid of any stale values. This is only
278 * needed to handle the case where a guest supports fewer stats than it
279 * used to (ie. it has booted into an old kernel).
283 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
285 uint16_t tag
= virtio_tswap16(vdev
, stat
.tag
);
286 uint64_t val
= virtio_tswap64(vdev
, stat
.val
);
288 offset
+= sizeof(stat
);
289 if (tag
< VIRTIO_BALLOON_S_NR
)
292 s
->stats_vq_offset
= offset
;
294 if (qemu_gettimeofday(&tv
) < 0) {
295 fprintf(stderr
, "warning: %s: failed to get time of day\n", __func__
);
299 s
->stats_last_update
= tv
.tv_sec
;
302 if (balloon_stats_enabled(s
)) {
303 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
307 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
309 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
310 struct virtio_balloon_config config
;
312 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
313 config
.actual
= cpu_to_le32(dev
->actual
);
315 trace_virtio_balloon_get_config(config
.num_pages
, config
.actual
);
316 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
319 static int build_dimm_list(Object
*obj
, void *opaque
)
321 GSList
**list
= opaque
;
323 if (object_dynamic_cast(obj
, TYPE_PC_DIMM
)) {
324 DeviceState
*dev
= DEVICE(obj
);
325 if (dev
->realized
) { /* only realized DIMMs matter */
326 *list
= g_slist_prepend(*list
, dev
);
330 object_child_foreach(obj
, build_dimm_list
, opaque
);
334 static ram_addr_t
get_current_ram_size(void)
336 GSList
*list
= NULL
, *item
;
337 ram_addr_t size
= ram_size
;
339 build_dimm_list(qdev_get_machine(), &list
);
340 for (item
= list
; item
; item
= g_slist_next(item
)) {
341 Object
*obj
= OBJECT(item
->data
);
342 if (!strcmp(object_get_typename(obj
), TYPE_PC_DIMM
)) {
343 size
+= object_property_get_int(obj
, PC_DIMM_SIZE_PROP
,
352 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
353 const uint8_t *config_data
)
355 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
356 struct virtio_balloon_config config
;
357 uint32_t oldactual
= dev
->actual
;
358 ram_addr_t vm_ram_size
= get_current_ram_size();
360 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
361 dev
->actual
= le32_to_cpu(config
.actual
);
362 if (dev
->actual
!= oldactual
) {
363 qapi_event_send_balloon_change(vm_ram_size
-
364 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
),
367 trace_virtio_balloon_set_config(dev
->actual
, oldactual
);
370 static uint64_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint64_t f
,
373 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
374 f
|= dev
->host_features
;
375 virtio_add_feature(&f
, VIRTIO_BALLOON_F_STATS_VQ
);
379 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
381 VirtIOBalloon
*dev
= opaque
;
382 info
->actual
= get_current_ram_size() - ((uint64_t) dev
->actual
<<
383 VIRTIO_BALLOON_PFN_SHIFT
);
386 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
388 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
389 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
390 ram_addr_t vm_ram_size
= get_current_ram_size();
392 if (target
> vm_ram_size
) {
393 target
= vm_ram_size
;
396 dev
->num_pages
= (vm_ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
397 virtio_notify_config(vdev
);
399 trace_virtio_balloon_to_target(target
, dev
->num_pages
);
402 static int virtio_balloon_post_load_device(void *opaque
, int version_id
)
404 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
406 if (balloon_stats_enabled(s
)) {
407 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
412 static const VMStateDescription vmstate_virtio_balloon_device
= {
413 .name
= "virtio-balloon-device",
415 .minimum_version_id
= 1,
416 .post_load
= virtio_balloon_post_load_device
,
417 .fields
= (VMStateField
[]) {
418 VMSTATE_UINT32(num_pages
, VirtIOBalloon
),
419 VMSTATE_UINT32(actual
, VirtIOBalloon
),
420 VMSTATE_END_OF_LIST()
424 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
426 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
427 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
430 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
431 sizeof(struct virtio_balloon_config
));
433 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
434 virtio_balloon_stat
, s
);
437 error_setg(errp
, "Only one balloon device is supported");
438 virtio_cleanup(vdev
);
442 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
443 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
444 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
449 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
451 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
452 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
454 balloon_stats_destroy_timer(s
);
455 qemu_remove_balloon_handler(s
);
456 virtio_cleanup(vdev
);
459 static void virtio_balloon_device_reset(VirtIODevice
*vdev
)
461 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
463 if (s
->stats_vq_elem
!= NULL
) {
464 virtqueue_unpop(s
->svq
, s
->stats_vq_elem
, 0);
465 g_free(s
->stats_vq_elem
);
466 s
->stats_vq_elem
= NULL
;
470 static void virtio_balloon_set_status(VirtIODevice
*vdev
, uint8_t status
)
472 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
474 if (!s
->stats_vq_elem
&& vdev
->vm_running
&&
475 (status
& VIRTIO_CONFIG_S_DRIVER_OK
) && virtqueue_rewind(s
->svq
, 1)) {
476 /* poll stats queue for the element we have discarded when the VM
478 virtio_balloon_receive_stats(vdev
, s
->svq
);
482 static void virtio_balloon_instance_init(Object
*obj
)
484 VirtIOBalloon
*s
= VIRTIO_BALLOON(obj
);
486 object_property_add(obj
, "guest-stats", "guest statistics",
487 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
489 object_property_add(obj
, "guest-stats-polling-interval", "int",
490 balloon_stats_get_poll_interval
,
491 balloon_stats_set_poll_interval
,
495 static const VMStateDescription vmstate_virtio_balloon
= {
496 .name
= "virtio-balloon",
497 .minimum_version_id
= 1,
499 .fields
= (VMStateField
[]) {
500 VMSTATE_VIRTIO_DEVICE
,
501 VMSTATE_END_OF_LIST()
505 static Property virtio_balloon_properties
[] = {
506 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon
, host_features
,
507 VIRTIO_BALLOON_F_DEFLATE_ON_OOM
, false),
508 DEFINE_PROP_END_OF_LIST(),
511 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
513 DeviceClass
*dc
= DEVICE_CLASS(klass
);
514 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
516 dc
->props
= virtio_balloon_properties
;
517 dc
->vmsd
= &vmstate_virtio_balloon
;
518 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
519 vdc
->realize
= virtio_balloon_device_realize
;
520 vdc
->unrealize
= virtio_balloon_device_unrealize
;
521 vdc
->reset
= virtio_balloon_device_reset
;
522 vdc
->get_config
= virtio_balloon_get_config
;
523 vdc
->set_config
= virtio_balloon_set_config
;
524 vdc
->get_features
= virtio_balloon_get_features
;
525 vdc
->set_status
= virtio_balloon_set_status
;
526 vdc
->vmsd
= &vmstate_virtio_balloon_device
;
529 static const TypeInfo virtio_balloon_info
= {
530 .name
= TYPE_VIRTIO_BALLOON
,
531 .parent
= TYPE_VIRTIO_DEVICE
,
532 .instance_size
= sizeof(VirtIOBalloon
),
533 .instance_init
= virtio_balloon_instance_init
,
534 .class_init
= virtio_balloon_class_init
,
537 static void virtio_register_types(void)
539 type_register_static(&virtio_balloon_info
);
542 type_init(virtio_register_types
)