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.
17 #include "qemu/timer.h"
18 #include "qemu-common.h"
19 #include "hw/virtio.h"
22 #include "sysemu/balloon.h"
23 #include "hw/virtio-balloon.h"
24 #include "sysemu/kvm.h"
25 #include "exec/address-spaces.h"
26 #include "qapi/visitor.h"
28 #if defined(__linux__)
32 #include "hw/virtio-bus.h"
34 static void balloon_page(void *addr
, int deflate
)
36 #if defined(__linux__)
37 if (!kvm_enabled() || kvm_has_sync_mmu())
38 qemu_madvise(addr
, TARGET_PAGE_SIZE
,
39 deflate
? QEMU_MADV_WILLNEED
: QEMU_MADV_DONTNEED
);
43 static const char *balloon_stat_names
[] = {
44 [VIRTIO_BALLOON_S_SWAP_IN
] = "stat-swap-in",
45 [VIRTIO_BALLOON_S_SWAP_OUT
] = "stat-swap-out",
46 [VIRTIO_BALLOON_S_MAJFLT
] = "stat-major-faults",
47 [VIRTIO_BALLOON_S_MINFLT
] = "stat-minor-faults",
48 [VIRTIO_BALLOON_S_MEMFREE
] = "stat-free-memory",
49 [VIRTIO_BALLOON_S_MEMTOT
] = "stat-total-memory",
50 [VIRTIO_BALLOON_S_NR
] = NULL
54 * reset_stats - Mark all items in the stats array as unset
56 * This function needs to be called at device intialization and before
57 * before updating to a set of newly-generated stats. This will ensure that no
58 * stale values stick around in case the guest reports a subset of the supported
61 static inline void reset_stats(VirtIOBalloon
*dev
)
64 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
67 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
69 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
70 return vdev
->guest_features
& (1 << VIRTIO_BALLOON_F_STATS_VQ
);
73 static bool balloon_stats_enabled(const VirtIOBalloon
*s
)
75 return s
->stats_poll_interval
> 0;
78 static void balloon_stats_destroy_timer(VirtIOBalloon
*s
)
80 if (balloon_stats_enabled(s
)) {
81 qemu_del_timer(s
->stats_timer
);
82 qemu_free_timer(s
->stats_timer
);
83 s
->stats_timer
= NULL
;
84 s
->stats_poll_interval
= 0;
88 static void balloon_stats_change_timer(VirtIOBalloon
*s
, int secs
)
90 qemu_mod_timer(s
->stats_timer
, qemu_get_clock_ms(vm_clock
) + secs
* 1000);
93 static void balloon_stats_poll_cb(void *opaque
)
95 VirtIOBalloon
*s
= opaque
;
96 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
98 if (!balloon_stats_supported(s
)) {
100 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
104 virtqueue_push(s
->svq
, &s
->stats_vq_elem
, s
->stats_vq_offset
);
105 virtio_notify(vdev
, s
->svq
);
108 static void balloon_stats_get_all(Object
*obj
, struct Visitor
*v
,
109 void *opaque
, const char *name
, Error
**errp
)
111 VirtIOBalloon
*s
= opaque
;
114 if (!s
->stats_last_update
) {
115 error_setg(errp
, "guest hasn't updated any stats yet");
119 visit_start_struct(v
, NULL
, "guest-stats", name
, 0, errp
);
120 visit_type_int(v
, &s
->stats_last_update
, "last-update", errp
);
122 visit_start_struct(v
, NULL
, NULL
, "stats", 0, errp
);
123 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
124 visit_type_int64(v
, (int64_t *) &s
->stats
[i
], balloon_stat_names
[i
],
127 visit_end_struct(v
, errp
);
129 visit_end_struct(v
, errp
);
132 static void balloon_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
133 void *opaque
, const char *name
,
136 VirtIOBalloon
*s
= opaque
;
137 visit_type_int(v
, &s
->stats_poll_interval
, name
, errp
);
140 static void balloon_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
141 void *opaque
, const char *name
,
144 VirtIOBalloon
*s
= opaque
;
147 visit_type_int(v
, &value
, name
, errp
);
148 if (error_is_set(errp
)) {
153 error_setg(errp
, "timer value must be greater than zero");
157 if (value
== s
->stats_poll_interval
) {
162 /* timer=0 disables the timer */
163 balloon_stats_destroy_timer(s
);
167 if (balloon_stats_enabled(s
)) {
168 /* timer interval change */
169 s
->stats_poll_interval
= value
;
170 balloon_stats_change_timer(s
, value
);
174 /* create a new timer */
175 g_assert(s
->stats_timer
== NULL
);
176 s
->stats_timer
= qemu_new_timer_ms(vm_clock
, balloon_stats_poll_cb
, s
);
177 s
->stats_poll_interval
= value
;
178 balloon_stats_change_timer(s
, 0);
181 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
183 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
184 VirtQueueElement elem
;
185 MemoryRegionSection section
;
187 while (virtqueue_pop(vq
, &elem
)) {
191 while (iov_to_buf(elem
.out_sg
, elem
.out_num
, offset
, &pfn
, 4) == 4) {
195 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
198 /* FIXME: remove get_system_memory(), but how? */
199 section
= memory_region_find(get_system_memory(), pa
, 1);
200 if (!section
.size
|| !memory_region_is_ram(section
.mr
))
203 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
204 should be OK because we only want a single page. */
205 addr
= section
.offset_within_region
;
206 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
210 virtqueue_push(vq
, &elem
, offset
);
211 virtio_notify(vdev
, vq
);
215 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
217 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
218 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
219 VirtIOBalloonStat stat
;
223 if (!virtqueue_pop(vq
, elem
)) {
227 /* Initialize the stats to get rid of any stale values. This is only
228 * needed to handle the case where a guest supports fewer stats than it
229 * used to (ie. it has booted into an old kernel).
233 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
235 uint16_t tag
= tswap16(stat
.tag
);
236 uint64_t val
= tswap64(stat
.val
);
238 offset
+= sizeof(stat
);
239 if (tag
< VIRTIO_BALLOON_S_NR
)
242 s
->stats_vq_offset
= offset
;
244 if (qemu_gettimeofday(&tv
) < 0) {
245 fprintf(stderr
, "warning: %s: failed to get time of day\n", __func__
);
249 s
->stats_last_update
= tv
.tv_sec
;
252 if (balloon_stats_enabled(s
)) {
253 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
257 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
259 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
260 struct virtio_balloon_config config
;
262 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
263 config
.actual
= cpu_to_le32(dev
->actual
);
265 memcpy(config_data
, &config
, 8);
268 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
269 const uint8_t *config_data
)
271 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
272 struct virtio_balloon_config config
;
273 uint32_t oldactual
= dev
->actual
;
274 memcpy(&config
, config_data
, 8);
275 dev
->actual
= le32_to_cpu(config
.actual
);
276 if (dev
->actual
!= oldactual
) {
277 qemu_balloon_changed(ram_size
-
278 (dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
282 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
284 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
288 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
290 VirtIOBalloon
*dev
= opaque
;
291 info
->actual
= ram_size
- ((uint64_t) dev
->actual
<<
292 VIRTIO_BALLOON_PFN_SHIFT
);
295 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
297 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
298 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
300 if (target
> ram_size
) {
304 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
305 virtio_notify_config(vdev
);
309 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
311 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
312 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
314 virtio_save(vdev
, f
);
316 qemu_put_be32(f
, s
->num_pages
);
317 qemu_put_be32(f
, s
->actual
);
320 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
322 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
323 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
329 ret
= virtio_load(vdev
, f
);
334 s
->num_pages
= qemu_get_be32(f
);
335 s
->actual
= qemu_get_be32(f
);
339 static int virtio_balloon_device_init(VirtIODevice
*vdev
)
341 DeviceState
*qdev
= DEVICE(vdev
);
342 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
345 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
, 8);
347 vdev
->get_config
= virtio_balloon_get_config
;
348 vdev
->set_config
= virtio_balloon_set_config
;
349 vdev
->get_features
= virtio_balloon_get_features
;
351 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
352 virtio_balloon_stat
, s
);
355 virtio_common_cleanup(VIRTIO_DEVICE(s
));
359 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
360 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
361 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
363 register_savevm(qdev
, "virtio-balloon", -1, 1,
364 virtio_balloon_save
, virtio_balloon_load
, s
);
366 object_property_add(OBJECT(qdev
), "guest-stats", "guest statistics",
367 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
369 object_property_add(OBJECT(qdev
), "guest-stats-polling-interval", "int",
370 balloon_stats_get_poll_interval
,
371 balloon_stats_set_poll_interval
,
376 static int virtio_balloon_device_exit(DeviceState
*qdev
)
378 VirtIOBalloon
*s
= VIRTIO_BALLOON(qdev
);
379 VirtIODevice
*vdev
= VIRTIO_DEVICE(qdev
);
381 balloon_stats_destroy_timer(s
);
382 qemu_remove_balloon_handler(s
);
383 unregister_savevm(qdev
, "virtio-balloon", s
);
384 virtio_common_cleanup(vdev
);
388 static Property virtio_balloon_properties
[] = {
389 DEFINE_PROP_END_OF_LIST(),
392 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
394 DeviceClass
*dc
= DEVICE_CLASS(klass
);
395 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
396 dc
->exit
= virtio_balloon_device_exit
;
397 dc
->props
= virtio_balloon_properties
;
398 vdc
->init
= virtio_balloon_device_init
;
399 vdc
->get_config
= virtio_balloon_get_config
;
400 vdc
->set_config
= virtio_balloon_set_config
;
401 vdc
->get_features
= virtio_balloon_get_features
;
404 static const TypeInfo virtio_balloon_info
= {
405 .name
= TYPE_VIRTIO_BALLOON
,
406 .parent
= TYPE_VIRTIO_DEVICE
,
407 .instance_size
= sizeof(VirtIOBalloon
),
408 .class_init
= virtio_balloon_class_init
,
411 static void virtio_register_types(void)
413 type_register_static(&virtio_balloon_info
);
416 type_init(virtio_register_types
)