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/virtio.h"
20 #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"
28 #if defined(__linux__)
32 #include "hw/virtio/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 initialization and before
57 * 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 timer_del(s
->stats_timer
);
82 timer_free(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 timer_mod(s
->stats_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 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
)
112 VirtIOBalloon
*s
= opaque
;
115 visit_start_struct(v
, NULL
, "guest-stats", name
, 0, &err
);
119 visit_type_int(v
, &s
->stats_last_update
, "last-update", &err
);
124 visit_start_struct(v
, NULL
, NULL
, "stats", 0, &err
);
128 for (i
= 0; !err
&& i
< VIRTIO_BALLOON_S_NR
; i
++) {
129 visit_type_int64(v
, (int64_t *) &s
->stats
[i
], balloon_stat_names
[i
],
132 error_propagate(errp
, err
);
134 visit_end_struct(v
, &err
);
137 error_propagate(errp
, err
);
139 visit_end_struct(v
, &err
);
141 error_propagate(errp
, err
);
144 static void balloon_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
145 void *opaque
, const char *name
,
148 VirtIOBalloon
*s
= opaque
;
149 visit_type_int(v
, &s
->stats_poll_interval
, name
, errp
);
152 static void balloon_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
153 void *opaque
, const char *name
,
156 VirtIOBalloon
*s
= opaque
;
157 Error
*local_err
= NULL
;
160 visit_type_int(v
, &value
, name
, &local_err
);
162 error_propagate(errp
, local_err
);
167 error_setg(errp
, "timer value must be greater than zero");
171 if (value
== s
->stats_poll_interval
) {
176 /* timer=0 disables the timer */
177 balloon_stats_destroy_timer(s
);
181 if (balloon_stats_enabled(s
)) {
182 /* timer interval change */
183 s
->stats_poll_interval
= value
;
184 balloon_stats_change_timer(s
, value
);
188 /* create a new timer */
189 g_assert(s
->stats_timer
== NULL
);
190 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
191 s
->stats_poll_interval
= value
;
192 balloon_stats_change_timer(s
, 0);
195 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
197 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
198 VirtQueueElement elem
;
199 MemoryRegionSection section
;
201 while (virtqueue_pop(vq
, &elem
)) {
205 while (iov_to_buf(elem
.out_sg
, elem
.out_num
, offset
, &pfn
, 4) == 4) {
209 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
212 /* FIXME: remove get_system_memory(), but how? */
213 section
= memory_region_find(get_system_memory(), pa
, 1);
214 if (!int128_nz(section
.size
) || !memory_region_is_ram(section
.mr
))
217 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
218 should be OK because we only want a single page. */
219 addr
= section
.offset_within_region
;
220 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
222 memory_region_unref(section
.mr
);
225 virtqueue_push(vq
, &elem
, offset
);
226 virtio_notify(vdev
, vq
);
230 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
232 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
233 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
234 VirtIOBalloonStat stat
;
238 if (!virtqueue_pop(vq
, elem
)) {
242 /* Initialize the stats to get rid of any stale values. This is only
243 * needed to handle the case where a guest supports fewer stats than it
244 * used to (ie. it has booted into an old kernel).
248 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
250 uint16_t tag
= tswap16(stat
.tag
);
251 uint64_t val
= tswap64(stat
.val
);
253 offset
+= sizeof(stat
);
254 if (tag
< VIRTIO_BALLOON_S_NR
)
257 s
->stats_vq_offset
= offset
;
259 if (qemu_gettimeofday(&tv
) < 0) {
260 fprintf(stderr
, "warning: %s: failed to get time of day\n", __func__
);
264 s
->stats_last_update
= tv
.tv_sec
;
267 if (balloon_stats_enabled(s
)) {
268 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
272 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
274 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
275 struct virtio_balloon_config config
;
277 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
278 config
.actual
= cpu_to_le32(dev
->actual
);
280 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
283 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
284 const uint8_t *config_data
)
286 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
287 struct virtio_balloon_config config
;
288 uint32_t oldactual
= dev
->actual
;
289 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
290 dev
->actual
= le32_to_cpu(config
.actual
);
291 if (dev
->actual
!= oldactual
) {
292 qemu_balloon_changed(ram_size
-
293 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
297 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
299 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
303 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
305 VirtIOBalloon
*dev
= opaque
;
306 info
->actual
= ram_size
- ((uint64_t) dev
->actual
<<
307 VIRTIO_BALLOON_PFN_SHIFT
);
310 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
312 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
313 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
315 if (target
> ram_size
) {
319 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
320 virtio_notify_config(vdev
);
324 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
326 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
327 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
329 virtio_save(vdev
, f
);
331 qemu_put_be32(f
, s
->num_pages
);
332 qemu_put_be32(f
, s
->actual
);
335 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
337 VirtIOBalloon
*s
= VIRTIO_BALLOON(opaque
);
338 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
344 ret
= virtio_load(vdev
, f
);
349 s
->num_pages
= qemu_get_be32(f
);
350 s
->actual
= qemu_get_be32(f
);
354 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
356 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
357 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
360 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
361 sizeof(struct virtio_balloon_config
));
363 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
364 virtio_balloon_stat
, s
);
367 error_setg(errp
, "Adding balloon handler failed");
368 virtio_cleanup(vdev
);
372 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
373 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
374 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
378 register_savevm(dev
, "virtio-balloon", -1, 1,
379 virtio_balloon_save
, virtio_balloon_load
, s
);
381 object_property_add(OBJECT(dev
), "guest-stats", "guest statistics",
382 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
384 object_property_add(OBJECT(dev
), "guest-stats-polling-interval", "int",
385 balloon_stats_get_poll_interval
,
386 balloon_stats_set_poll_interval
,
390 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
392 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
393 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
395 balloon_stats_destroy_timer(s
);
396 qemu_remove_balloon_handler(s
);
397 unregister_savevm(dev
, "virtio-balloon", s
);
398 virtio_cleanup(vdev
);
401 static Property virtio_balloon_properties
[] = {
402 DEFINE_PROP_END_OF_LIST(),
405 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
407 DeviceClass
*dc
= DEVICE_CLASS(klass
);
408 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
410 dc
->props
= virtio_balloon_properties
;
411 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
412 vdc
->realize
= virtio_balloon_device_realize
;
413 vdc
->unrealize
= virtio_balloon_device_unrealize
;
414 vdc
->get_config
= virtio_balloon_get_config
;
415 vdc
->set_config
= virtio_balloon_set_config
;
416 vdc
->get_features
= virtio_balloon_get_features
;
419 static const TypeInfo virtio_balloon_info
= {
420 .name
= TYPE_VIRTIO_BALLOON
,
421 .parent
= TYPE_VIRTIO_DEVICE
,
422 .instance_size
= sizeof(VirtIOBalloon
),
423 .class_init
= virtio_balloon_class_init
,
426 static void virtio_register_types(void)
428 type_register_static(&virtio_balloon_info
);
431 type_init(virtio_register_types
)