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"
27 #include "qapi-event.h"
29 #if defined(__linux__)
33 #include "hw/virtio/virtio-bus.h"
34 #include "hw/virtio/virtio-access.h"
36 static void balloon_page(void *addr
, int deflate
)
38 #if defined(__linux__)
39 if (!kvm_enabled() || kvm_has_sync_mmu())
40 qemu_madvise(addr
, TARGET_PAGE_SIZE
,
41 deflate
? QEMU_MADV_WILLNEED
: QEMU_MADV_DONTNEED
);
45 static const char *balloon_stat_names
[] = {
46 [VIRTIO_BALLOON_S_SWAP_IN
] = "stat-swap-in",
47 [VIRTIO_BALLOON_S_SWAP_OUT
] = "stat-swap-out",
48 [VIRTIO_BALLOON_S_MAJFLT
] = "stat-major-faults",
49 [VIRTIO_BALLOON_S_MINFLT
] = "stat-minor-faults",
50 [VIRTIO_BALLOON_S_MEMFREE
] = "stat-free-memory",
51 [VIRTIO_BALLOON_S_MEMTOT
] = "stat-total-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 vdev
->guest_features
& (1 << 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
, int 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 (!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
);
110 static void balloon_stats_get_all(Object
*obj
, struct Visitor
*v
,
111 void *opaque
, const char *name
, Error
**errp
)
114 VirtIOBalloon
*s
= opaque
;
117 visit_start_struct(v
, NULL
, "guest-stats", name
, 0, &err
);
121 visit_type_int(v
, &s
->stats_last_update
, "last-update", &err
);
126 visit_start_struct(v
, NULL
, NULL
, "stats", 0, &err
);
130 for (i
= 0; !err
&& i
< VIRTIO_BALLOON_S_NR
; i
++) {
131 visit_type_int64(v
, (int64_t *) &s
->stats
[i
], balloon_stat_names
[i
],
134 error_propagate(errp
, err
);
136 visit_end_struct(v
, &err
);
139 error_propagate(errp
, err
);
141 visit_end_struct(v
, &err
);
143 error_propagate(errp
, err
);
146 static void balloon_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
147 void *opaque
, const char *name
,
150 VirtIOBalloon
*s
= opaque
;
151 visit_type_int(v
, &s
->stats_poll_interval
, name
, errp
);
154 static void balloon_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
155 void *opaque
, const char *name
,
158 VirtIOBalloon
*s
= opaque
;
159 Error
*local_err
= NULL
;
162 visit_type_int(v
, &value
, name
, &local_err
);
164 error_propagate(errp
, local_err
);
169 error_setg(errp
, "timer value must be greater than zero");
173 if (value
== s
->stats_poll_interval
) {
178 /* timer=0 disables the timer */
179 balloon_stats_destroy_timer(s
);
183 if (balloon_stats_enabled(s
)) {
184 /* timer interval change */
185 s
->stats_poll_interval
= value
;
186 balloon_stats_change_timer(s
, value
);
190 /* create a new timer */
191 g_assert(s
->stats_timer
== NULL
);
192 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
193 s
->stats_poll_interval
= value
;
194 balloon_stats_change_timer(s
, 0);
197 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
199 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
200 VirtQueueElement elem
;
201 MemoryRegionSection section
;
203 while (virtqueue_pop(vq
, &elem
)) {
207 while (iov_to_buf(elem
.out_sg
, elem
.out_num
, offset
, &pfn
, 4) == 4) {
210 int p
= virtio_ldl_p(vdev
, &pfn
);
212 pa
= (ram_addr_t
) p
<< VIRTIO_BALLOON_PFN_SHIFT
;
215 /* FIXME: remove get_system_memory(), but how? */
216 section
= memory_region_find(get_system_memory(), pa
, 1);
217 if (!int128_nz(section
.size
) || !memory_region_is_ram(section
.mr
))
220 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
221 should be OK because we only want a single page. */
222 addr
= section
.offset_within_region
;
223 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
225 memory_region_unref(section
.mr
);
228 virtqueue_push(vq
, &elem
, offset
);
229 virtio_notify(vdev
, vq
);
233 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
235 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
236 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
237 VirtIOBalloonStat stat
;
241 if (!virtqueue_pop(vq
, elem
)) {
245 /* Initialize the stats to get rid of any stale values. This is only
246 * needed to handle the case where a guest supports fewer stats than it
247 * used to (ie. it has booted into an old kernel).
251 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
253 uint16_t tag
= virtio_tswap16(vdev
, stat
.tag
);
254 uint64_t val
= virtio_tswap64(vdev
, stat
.val
);
256 offset
+= sizeof(stat
);
257 if (tag
< VIRTIO_BALLOON_S_NR
)
260 s
->stats_vq_offset
= offset
;
262 if (qemu_gettimeofday(&tv
) < 0) {
263 fprintf(stderr
, "warning: %s: failed to get time of day\n", __func__
);
267 s
->stats_last_update
= tv
.tv_sec
;
270 if (balloon_stats_enabled(s
)) {
271 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
275 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
277 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
278 struct virtio_balloon_config config
;
280 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
281 config
.actual
= cpu_to_le32(dev
->actual
);
283 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
286 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
287 const uint8_t *config_data
)
289 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
290 struct virtio_balloon_config config
;
291 uint32_t oldactual
= dev
->actual
;
292 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
293 dev
->actual
= le32_to_cpu(config
.actual
);
294 if (dev
->actual
!= oldactual
) {
295 qapi_event_send_balloon_change(ram_size
-
296 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
),
301 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
303 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
307 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
309 VirtIOBalloon
*dev
= opaque
;
310 info
->actual
= ram_size
- ((uint64_t) dev
->actual
<<
311 VIRTIO_BALLOON_PFN_SHIFT
);
314 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
316 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
317 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
319 if (target
> ram_size
) {
323 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
324 virtio_notify_config(vdev
);
328 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
330 virtio_save(VIRTIO_DEVICE(opaque
), f
);
333 static void virtio_balloon_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
335 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
337 qemu_put_be32(f
, s
->num_pages
);
338 qemu_put_be32(f
, s
->actual
);
341 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
346 return virtio_load(VIRTIO_DEVICE(opaque
), f
, version_id
);
349 static int virtio_balloon_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
352 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
354 s
->num_pages
= qemu_get_be32(f
);
355 s
->actual
= qemu_get_be32(f
);
359 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
361 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
362 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
365 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
366 sizeof(struct virtio_balloon_config
));
368 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
369 virtio_balloon_stat
, s
);
372 error_setg(errp
, "Adding balloon handler failed");
373 virtio_cleanup(vdev
);
377 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
378 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
379 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
383 register_savevm(dev
, "virtio-balloon", -1, 1,
384 virtio_balloon_save
, virtio_balloon_load
, s
);
386 object_property_add(OBJECT(dev
), "guest-stats", "guest statistics",
387 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
389 object_property_add(OBJECT(dev
), "guest-stats-polling-interval", "int",
390 balloon_stats_get_poll_interval
,
391 balloon_stats_set_poll_interval
,
395 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
397 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
398 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
400 balloon_stats_destroy_timer(s
);
401 qemu_remove_balloon_handler(s
);
402 unregister_savevm(dev
, "virtio-balloon", s
);
403 virtio_cleanup(vdev
);
406 static Property virtio_balloon_properties
[] = {
407 DEFINE_PROP_END_OF_LIST(),
410 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
412 DeviceClass
*dc
= DEVICE_CLASS(klass
);
413 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
415 dc
->props
= virtio_balloon_properties
;
416 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
417 vdc
->realize
= virtio_balloon_device_realize
;
418 vdc
->unrealize
= virtio_balloon_device_unrealize
;
419 vdc
->get_config
= virtio_balloon_get_config
;
420 vdc
->set_config
= virtio_balloon_set_config
;
421 vdc
->get_features
= virtio_balloon_get_features
;
422 vdc
->save
= virtio_balloon_save_device
;
423 vdc
->load
= virtio_balloon_load_device
;
426 static const TypeInfo virtio_balloon_info
= {
427 .name
= TYPE_VIRTIO_BALLOON
,
428 .parent
= TYPE_VIRTIO_DEVICE
,
429 .instance_size
= sizeof(VirtIOBalloon
),
430 .class_init
= virtio_balloon_class_init
,
433 static void virtio_register_types(void)
435 type_register_static(&virtio_balloon_info
);
438 type_init(virtio_register_types
)