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 static VirtIOBalloon
*to_virtio_balloon(VirtIODevice
*vdev
)
34 return (VirtIOBalloon
*)vdev
;
37 static void balloon_page(void *addr
, int deflate
)
39 #if defined(__linux__)
40 if (!kvm_enabled() || kvm_has_sync_mmu())
41 qemu_madvise(addr
, TARGET_PAGE_SIZE
,
42 deflate
? QEMU_MADV_WILLNEED
: QEMU_MADV_DONTNEED
);
46 static const char *balloon_stat_names
[] = {
47 [VIRTIO_BALLOON_S_SWAP_IN
] = "stat-swap-in",
48 [VIRTIO_BALLOON_S_SWAP_OUT
] = "stat-swap-out",
49 [VIRTIO_BALLOON_S_MAJFLT
] = "stat-major-faults",
50 [VIRTIO_BALLOON_S_MINFLT
] = "stat-minor-faults",
51 [VIRTIO_BALLOON_S_MEMFREE
] = "stat-free-memory",
52 [VIRTIO_BALLOON_S_MEMTOT
] = "stat-total-memory",
53 [VIRTIO_BALLOON_S_NR
] = NULL
57 * reset_stats - Mark all items in the stats array as unset
59 * This function needs to be called at device intialization and before
60 * before updating to a set of newly-generated stats. This will ensure that no
61 * stale values stick around in case the guest reports a subset of the supported
64 static inline void reset_stats(VirtIOBalloon
*dev
)
67 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; dev
->stats
[i
++] = -1);
70 static bool balloon_stats_supported(const VirtIOBalloon
*s
)
72 return s
->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 qemu_del_timer(s
->stats_timer
);
84 qemu_free_timer(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 qemu_mod_timer(s
->stats_timer
, qemu_get_clock_ms(vm_clock
) + secs
* 1000);
95 static void balloon_stats_poll_cb(void *opaque
)
97 VirtIOBalloon
*s
= opaque
;
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(&s
->vdev
, s
->svq
);
109 static void balloon_stats_get_all(Object
*obj
, struct Visitor
*v
,
110 void *opaque
, const char *name
, Error
**errp
)
112 VirtIOBalloon
*s
= opaque
;
115 if (!s
->stats_last_update
) {
116 error_setg(errp
, "guest hasn't updated any stats yet");
120 visit_start_struct(v
, NULL
, "guest-stats", name
, 0, errp
);
121 visit_type_int(v
, &s
->stats_last_update
, "last-update", errp
);
123 visit_start_struct(v
, NULL
, NULL
, "stats", 0, errp
);
124 for (i
= 0; i
< VIRTIO_BALLOON_S_NR
; i
++) {
125 visit_type_int64(v
, (int64_t *) &s
->stats
[i
], balloon_stat_names
[i
],
128 visit_end_struct(v
, errp
);
130 visit_end_struct(v
, errp
);
133 static void balloon_stats_get_poll_interval(Object
*obj
, struct Visitor
*v
,
134 void *opaque
, const char *name
,
137 VirtIOBalloon
*s
= opaque
;
138 visit_type_int(v
, &s
->stats_poll_interval
, name
, errp
);
141 static void balloon_stats_set_poll_interval(Object
*obj
, struct Visitor
*v
,
142 void *opaque
, const char *name
,
145 VirtIOBalloon
*s
= opaque
;
148 visit_type_int(v
, &value
, name
, errp
);
149 if (error_is_set(errp
)) {
154 error_setg(errp
, "timer value must be greater than zero");
158 if (value
== s
->stats_poll_interval
) {
163 /* timer=0 disables the timer */
164 balloon_stats_destroy_timer(s
);
168 if (balloon_stats_enabled(s
)) {
169 /* timer interval change */
170 s
->stats_poll_interval
= value
;
171 balloon_stats_change_timer(s
, value
);
175 /* create a new timer */
176 g_assert(s
->stats_timer
== NULL
);
177 s
->stats_timer
= qemu_new_timer_ms(vm_clock
, balloon_stats_poll_cb
, s
);
178 s
->stats_poll_interval
= value
;
179 balloon_stats_change_timer(s
, 0);
182 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
184 VirtIOBalloon
*s
= to_virtio_balloon(vdev
);
185 VirtQueueElement elem
;
186 MemoryRegionSection section
;
188 while (virtqueue_pop(vq
, &elem
)) {
192 while (iov_to_buf(elem
.out_sg
, elem
.out_num
, offset
, &pfn
, 4) == 4) {
196 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
199 /* FIXME: remove get_system_memory(), but how? */
200 section
= memory_region_find(get_system_memory(), pa
, 1);
201 if (!section
.size
|| !memory_region_is_ram(section
.mr
))
204 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
205 should be OK because we only want a single page. */
206 addr
= section
.offset_within_region
;
207 balloon_page(memory_region_get_ram_ptr(section
.mr
) + addr
,
211 virtqueue_push(vq
, &elem
, offset
);
212 virtio_notify(vdev
, vq
);
216 static void virtio_balloon_receive_stats(VirtIODevice
*vdev
, VirtQueue
*vq
)
218 VirtIOBalloon
*s
= DO_UPCAST(VirtIOBalloon
, vdev
, vdev
);
219 VirtQueueElement
*elem
= &s
->stats_vq_elem
;
220 VirtIOBalloonStat stat
;
224 if (!virtqueue_pop(vq
, elem
)) {
228 /* Initialize the stats to get rid of any stale values. This is only
229 * needed to handle the case where a guest supports fewer stats than it
230 * used to (ie. it has booted into an old kernel).
234 while (iov_to_buf(elem
->out_sg
, elem
->out_num
, offset
, &stat
, sizeof(stat
))
236 uint16_t tag
= tswap16(stat
.tag
);
237 uint64_t val
= tswap64(stat
.val
);
239 offset
+= sizeof(stat
);
240 if (tag
< VIRTIO_BALLOON_S_NR
)
243 s
->stats_vq_offset
= offset
;
245 if (qemu_gettimeofday(&tv
) < 0) {
246 fprintf(stderr
, "warning: %s: failed to get time of day\n", __func__
);
250 s
->stats_last_update
= tv
.tv_sec
;
253 if (balloon_stats_enabled(s
)) {
254 balloon_stats_change_timer(s
, s
->stats_poll_interval
);
258 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
260 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
261 struct virtio_balloon_config config
;
263 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
264 config
.actual
= cpu_to_le32(dev
->actual
);
266 memcpy(config_data
, &config
, 8);
269 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
270 const uint8_t *config_data
)
272 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
273 struct virtio_balloon_config config
;
274 uint32_t oldactual
= dev
->actual
;
275 memcpy(&config
, config_data
, 8);
276 dev
->actual
= le32_to_cpu(config
.actual
);
277 if (dev
->actual
!= oldactual
) {
278 qemu_balloon_changed(ram_size
-
279 (dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
));
283 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
, uint32_t f
)
285 f
|= (1 << VIRTIO_BALLOON_F_STATS_VQ
);
289 static void virtio_balloon_stat(void *opaque
, BalloonInfo
*info
)
291 VirtIOBalloon
*dev
= opaque
;
292 info
->actual
= ram_size
- ((uint64_t) dev
->actual
<<
293 VIRTIO_BALLOON_PFN_SHIFT
);
296 static void virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
298 VirtIOBalloon
*dev
= opaque
;
300 if (target
> ram_size
) {
304 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
305 virtio_notify_config(&dev
->vdev
);
309 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
311 VirtIOBalloon
*s
= opaque
;
313 virtio_save(&s
->vdev
, f
);
315 qemu_put_be32(f
, s
->num_pages
);
316 qemu_put_be32(f
, s
->actual
);
319 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
321 VirtIOBalloon
*s
= opaque
;
327 ret
= virtio_load(&s
->vdev
, f
);
332 s
->num_pages
= qemu_get_be32(f
);
333 s
->actual
= qemu_get_be32(f
);
337 VirtIODevice
*virtio_balloon_init(DeviceState
*dev
)
342 s
= (VirtIOBalloon
*)virtio_common_init("virtio-balloon",
344 8, sizeof(VirtIOBalloon
));
346 s
->vdev
.get_config
= virtio_balloon_get_config
;
347 s
->vdev
.set_config
= virtio_balloon_set_config
;
348 s
->vdev
.get_features
= virtio_balloon_get_features
;
350 ret
= qemu_add_balloon_handler(virtio_balloon_to_target
,
351 virtio_balloon_stat
, s
);
353 virtio_cleanup(&s
->vdev
);
357 s
->ivq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
358 s
->dvq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
359 s
->svq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_receive_stats
);
362 register_savevm(dev
, "virtio-balloon", -1, 1,
363 virtio_balloon_save
, virtio_balloon_load
, s
);
365 object_property_add(OBJECT(dev
), "guest-stats", "guest statistics",
366 balloon_stats_get_all
, NULL
, NULL
, s
, NULL
);
368 object_property_add(OBJECT(dev
), "guest-stats-polling-interval", "int",
369 balloon_stats_get_poll_interval
,
370 balloon_stats_set_poll_interval
,
376 void virtio_balloon_exit(VirtIODevice
*vdev
)
378 VirtIOBalloon
*s
= DO_UPCAST(VirtIOBalloon
, vdev
, vdev
);
380 balloon_stats_destroy_timer(s
);
381 qemu_remove_balloon_handler(s
);
382 unregister_savevm(s
->qdev
, "virtio-balloon", s
);
383 virtio_cleanup(vdev
);