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"
35 static void balloon_page(void *addr
, int deflate
)
37 #if defined(__linux__)
38 if (!kvm_enabled() || kvm_has_sync_mmu())
39 qemu_madvise(addr
, TARGET_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_NR
] = NULL
55 * reset_stats - Mark all items in the stats array as unset
57 * This function needs to be called at device initialization and before
58 * updating to a set of newly-generated stats. This will ensure that no
59 * stale values stick around in case the guest reports a subset of the supported
62 static inline void reset_stats(VirtIOBalloon
*dev
)
65 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
68 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
70 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
71 return vdev
->guest_features
& (1 << VIRTIO_BALLOON_F_STATS_VQ
);
74 static bool balloon_stats_enabled(const VirtIOBalloon
*s
)
76 return s
->stats_poll_interval
> 0;
79 static void balloon_stats_destroy_timer(VirtIOBalloon
*s
)
81 if (balloon_stats_enabled(s
)) {
82 timer_del(s
->stats_timer
);
83 timer_free(s
->stats_timer
);
84 s
->stats_timer
= NULL
;
85 s
->stats_poll_interval
= 0;
89 static void balloon_stats_change_timer(VirtIOBalloon
*s
, int secs
)
91 timer_mod(s
->stats_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + secs
* 1000);
94 static void balloon_stats_poll_cb(void *opaque
)
96 VirtIOBalloon
*s
= opaque
;
97 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
99 if (!balloon_stats_supported(s
)) {
101 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
105 virtqueue_push(s
->svq
, &s
->stats_vq_elem
, s
->stats_vq_offset
);
106 virtio_notify(vdev
, s
->svq
);
109 static void balloon_stats_get_all(Object
*obj
, struct Visitor
*v
,
110 void *opaque
, const char *name
, Error
**errp
)
113 VirtIOBalloon
*s
= opaque
;
116 visit_start_struct(v
, NULL
, "guest-stats", name
, 0, &err
);
120 visit_type_int(v
, &s
->stats_last_update
, "last-update", &err
);
125 visit_start_struct(v
, NULL
, NULL
, "stats", 0, &err
);
129 for (i
= 0; !err
&& i
< VIRTIO_BALLOON_S_NR
; i
++) {
130 visit_type_int64(v
, (int64_t *) &s
->stats
[i
], balloon_stat_names
[i
],
133 error_propagate(errp
, err
);
135 visit_end_struct(v
, &err
);
138 error_propagate(errp
, err
);
140 visit_end_struct(v
, &err
);
142 error_propagate(errp
, err
);
145 static void balloon_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
146 void *opaque
, const char *name
,
149 VirtIOBalloon
*s
= opaque
;
150 visit_type_int(v
, &s
->stats_poll_interval
, name
, errp
);
153 static void balloon_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
154 void *opaque
, const char *name
,
157 VirtIOBalloon
*s
= opaque
;
158 Error
*local_err
= NULL
;
161 visit_type_int(v
, &value
, name
, &local_err
);
163 error_propagate(errp
, local_err
);
168 error_setg(errp
, "timer value must be greater than zero");
172 if (value
== s
->stats_poll_interval
) {
177 /* timer=0 disables the timer */
178 balloon_stats_destroy_timer(s
);
182 if (balloon_stats_enabled(s
)) {
183 /* timer interval change */
184 s
->stats_poll_interval
= value
;
185 balloon_stats_change_timer(s
, value
);
189 /* create a new timer */
190 g_assert(s
->stats_timer
== NULL
);
191 s
->stats_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, balloon_stats_poll_cb
, s
);
192 s
->stats_poll_interval
= value
;
193 balloon_stats_change_timer(s
, 0);
196 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
198 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
199 VirtQueueElement elem
;
200 MemoryRegionSection section
;
202 while (virtqueue_pop(vq
, &elem
)) {
206 while (iov_to_buf(elem
.out_sg
, elem
.out_num
, offset
, &pfn
, 4) == 4) {
210 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
213 /* FIXME: remove get_system_memory(), but how? */
214 section
= memory_region_find(get_system_memory(), pa
, 1);
215 if (!int128_nz(section
.size
) || !memory_region_is_ram(section
.mr
))
218 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
219 should be OK because we only want a single page. */
220 addr
= section
.offset_within_region
;
221 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
223 memory_region_unref(section
.mr
);
226 virtqueue_push(vq
, &elem
, offset
);
227 virtio_notify(vdev
, vq
);
231 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
233 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
234 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
235 VirtIOBalloonStat stat
;
239 if (!virtqueue_pop(vq
, elem
)) {
243 /* Initialize the stats to get rid of any stale values. This is only
244 * needed to handle the case where a guest supports fewer stats than it
245 * used to (ie. it has booted into an old kernel).
249 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
251 uint16_t tag
= tswap16(stat
.tag
);
252 uint64_t val
= tswap64(stat
.val
);
254 offset
+= sizeof(stat
);
255 if (tag
< VIRTIO_BALLOON_S_NR
)
258 s
->stats_vq_offset
= offset
;
260 if (qemu_gettimeofday(&tv
) < 0) {
261 fprintf(stderr
, "warning: %s: failed to get time of day\n", __func__
);
265 s
->stats_last_update
= tv
.tv_sec
;
268 if (balloon_stats_enabled(s
)) {
269 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
273 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
275 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
276 struct virtio_balloon_config config
;
278 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
279 config
.actual
= cpu_to_le32(dev
->actual
);
281 memcpy(config_data
, &config
, sizeof(struct virtio_balloon_config
));
284 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
285 const uint8_t *config_data
)
287 VirtIOBalloon
*dev
= VIRTIO_BALLOON(vdev
);
288 struct virtio_balloon_config config
;
289 uint32_t oldactual
= dev
->actual
;
290 memcpy(&config
, config_data
, sizeof(struct virtio_balloon_config
));
291 dev
->actual
= le32_to_cpu(config
.actual
);
292 if (dev
->actual
!= oldactual
) {
293 qapi_event_send_balloon_change(ram_size
-
294 ((ram_addr_t
) dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
),
299 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
301 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
305 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
307 VirtIOBalloon
*dev
= opaque
;
308 info
->actual
= ram_size
- ((uint64_t) dev
->actual
<<
309 VIRTIO_BALLOON_PFN_SHIFT
);
312 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
314 VirtIOBalloon
*dev
= VIRTIO_BALLOON(opaque
);
315 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
317 if (target
> ram_size
) {
321 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
322 virtio_notify_config(vdev
);
326 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
328 virtio_save(VIRTIO_DEVICE(opaque
), f
);
331 static void virtio_balloon_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
333 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
335 qemu_put_be32(f
, s
->num_pages
);
336 qemu_put_be32(f
, s
->actual
);
339 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
344 return virtio_load(VIRTIO_DEVICE(opaque
), f
, version_id
);
347 static int virtio_balloon_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
350 VirtIOBalloon
*s
= VIRTIO_BALLOON(vdev
);
352 s
->num_pages
= qemu_get_be32(f
);
353 s
->actual
= qemu_get_be32(f
);
357 static void virtio_balloon_device_realize(DeviceState
*dev
, Error
**errp
)
359 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
360 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
363 virtio_init(vdev
, "virtio-balloon", VIRTIO_ID_BALLOON
,
364 sizeof(struct virtio_balloon_config
));
366 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
367 virtio_balloon_stat
, s
);
370 error_setg(errp
, "Adding balloon handler failed");
371 virtio_cleanup(vdev
);
375 s
->ivq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
376 s
->dvq
= virtio_add_queue(vdev
, 128, virtio_balloon_handle_output
);
377 s
->svq
= virtio_add_queue(vdev
, 128, virtio_balloon_receive_stats
);
381 register_savevm(dev
, "virtio-balloon", -1, 1,
382 virtio_balloon_save
, virtio_balloon_load
, s
);
384 object_property_add(OBJECT(dev
), "guest-stats", "guest statistics",
385 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
387 object_property_add(OBJECT(dev
), "guest-stats-polling-interval", "int",
388 balloon_stats_get_poll_interval
,
389 balloon_stats_set_poll_interval
,
393 static void virtio_balloon_device_unrealize(DeviceState
*dev
, Error
**errp
)
395 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
396 VirtIOBalloon
*s
= VIRTIO_BALLOON(dev
);
398 balloon_stats_destroy_timer(s
);
399 qemu_remove_balloon_handler(s
);
400 unregister_savevm(dev
, "virtio-balloon", s
);
401 virtio_cleanup(vdev
);
404 static Property virtio_balloon_properties
[] = {
405 DEFINE_PROP_END_OF_LIST(),
408 static void virtio_balloon_class_init(ObjectClass
*klass
, void *data
)
410 DeviceClass
*dc
= DEVICE_CLASS(klass
);
411 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
413 dc
->props
= virtio_balloon_properties
;
414 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
415 vdc
->realize
= virtio_balloon_device_realize
;
416 vdc
->unrealize
= virtio_balloon_device_unrealize
;
417 vdc
->get_config
= virtio_balloon_get_config
;
418 vdc
->set_config
= virtio_balloon_set_config
;
419 vdc
->get_features
= virtio_balloon_get_features
;
420 vdc
->save
= virtio_balloon_save_device
;
421 vdc
->load
= virtio_balloon_load_device
;
424 static const TypeInfo virtio_balloon_info
= {
425 .name
= TYPE_VIRTIO_BALLOON
,
426 .parent
= TYPE_VIRTIO_DEVICE
,
427 .instance_size
= sizeof(VirtIOBalloon
),
428 .class_init
= virtio_balloon_class_init
,
431 static void virtio_register_types(void)
433 type_register_static(&virtio_balloon_info
);
436 type_init(virtio_register_types
)