virtio: move allocation to virtqueue_pop/vring_pop
[qemu/kevin.git] / hw / virtio / virtio-balloon.c
blob5c3020331cbeb6c37d99cf4559e198c58c101f55
1 /*
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>
8 * Authors:
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.
16 #include "qemu/osdep.h"
17 #include "qemu/iov.h"
18 #include "qemu/timer.h"
19 #include "qemu-common.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/i386/pc.h"
22 #include "cpu.h"
23 #include "sysemu/balloon.h"
24 #include "hw/virtio/virtio-balloon.h"
25 #include "sysemu/kvm.h"
26 #include "exec/address-spaces.h"
27 #include "qapi/visitor.h"
28 #include "qapi-event.h"
29 #include "trace.h"
31 #if defined(__linux__)
32 #include <sys/mman.h>
33 #endif
35 #include "hw/virtio/virtio-bus.h"
36 #include "hw/virtio/virtio-access.h"
38 static void balloon_page(void *addr, int deflate)
40 #if defined(__linux__)
41 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
42 kvm_has_sync_mmu())) {
43 qemu_madvise(addr, TARGET_PAGE_SIZE,
44 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
46 #endif
49 static const char *balloon_stat_names[] = {
50 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
51 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
52 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
53 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
54 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
55 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
56 [VIRTIO_BALLOON_S_NR] = NULL
60 * reset_stats - Mark all items in the stats array as unset
62 * This function needs to be called at device initialization and before
63 * updating to a set of newly-generated stats. This will ensure that no
64 * stale values stick around in case the guest reports a subset of the supported
65 * statistics.
67 static inline void reset_stats(VirtIOBalloon *dev)
69 int i;
70 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
73 static bool balloon_stats_supported(const VirtIOBalloon *s)
75 VirtIODevice *vdev = VIRTIO_DEVICE(s);
76 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
79 static bool balloon_stats_enabled(const VirtIOBalloon *s)
81 return s->stats_poll_interval > 0;
84 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
86 if (balloon_stats_enabled(s)) {
87 timer_del(s->stats_timer);
88 timer_free(s->stats_timer);
89 s->stats_timer = NULL;
90 s->stats_poll_interval = 0;
94 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
96 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
99 static void balloon_stats_poll_cb(void *opaque)
101 VirtIOBalloon *s = opaque;
102 VirtIODevice *vdev = VIRTIO_DEVICE(s);
104 if (!balloon_stats_supported(s)) {
105 /* re-schedule */
106 balloon_stats_change_timer(s, s->stats_poll_interval);
107 return;
110 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
111 virtio_notify(vdev, s->svq);
112 g_free(s->stats_vq_elem);
113 s->stats_vq_elem = NULL;
116 static void balloon_stats_get_all(Object *obj, struct Visitor *v,
117 void *opaque, const char *name, Error **errp)
119 Error *err = NULL;
120 VirtIOBalloon *s = opaque;
121 int i;
123 visit_start_struct(v, NULL, "guest-stats", name, 0, &err);
124 if (err) {
125 goto out;
127 visit_type_int(v, &s->stats_last_update, "last-update", &err);
128 if (err) {
129 goto out_end;
132 visit_start_struct(v, NULL, NULL, "stats", 0, &err);
133 if (err) {
134 goto out_end;
136 for (i = 0; !err && i < VIRTIO_BALLOON_S_NR; i++) {
137 visit_type_int64(v, (int64_t *) &s->stats[i], balloon_stat_names[i],
138 &err);
140 error_propagate(errp, err);
141 err = NULL;
142 visit_end_struct(v, &err);
144 out_end:
145 error_propagate(errp, err);
146 err = NULL;
147 visit_end_struct(v, &err);
148 out:
149 error_propagate(errp, err);
152 static void balloon_stats_get_poll_interval(Object *obj, struct Visitor *v,
153 void *opaque, const char *name,
154 Error **errp)
156 VirtIOBalloon *s = opaque;
157 visit_type_int(v, &s->stats_poll_interval, name, errp);
160 static void balloon_stats_set_poll_interval(Object *obj, struct Visitor *v,
161 void *opaque, const char *name,
162 Error **errp)
164 VirtIOBalloon *s = opaque;
165 Error *local_err = NULL;
166 int64_t value;
168 visit_type_int(v, &value, name, &local_err);
169 if (local_err) {
170 error_propagate(errp, local_err);
171 return;
174 if (value < 0) {
175 error_setg(errp, "timer value must be greater than zero");
176 return;
179 if (value > UINT32_MAX) {
180 error_setg(errp, "timer value is too big");
181 return;
184 if (value == s->stats_poll_interval) {
185 return;
188 if (value == 0) {
189 /* timer=0 disables the timer */
190 balloon_stats_destroy_timer(s);
191 return;
194 if (balloon_stats_enabled(s)) {
195 /* timer interval change */
196 s->stats_poll_interval = value;
197 balloon_stats_change_timer(s, value);
198 return;
201 /* create a new timer */
202 g_assert(s->stats_timer == NULL);
203 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
204 s->stats_poll_interval = value;
205 balloon_stats_change_timer(s, 0);
208 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
210 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
211 VirtQueueElement *elem;
212 MemoryRegionSection section;
214 for (;;) {
215 size_t offset = 0;
216 uint32_t pfn;
217 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
218 if (!elem) {
219 return;
222 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
223 ram_addr_t pa;
224 ram_addr_t addr;
225 int p = virtio_ldl_p(vdev, &pfn);
227 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
228 offset += 4;
230 /* FIXME: remove get_system_memory(), but how? */
231 section = memory_region_find(get_system_memory(), pa, 1);
232 if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
233 continue;
235 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
236 pa);
237 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
238 should be OK because we only want a single page. */
239 addr = section.offset_within_region;
240 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
241 !!(vq == s->dvq));
242 memory_region_unref(section.mr);
245 virtqueue_push(vq, elem, offset);
246 virtio_notify(vdev, vq);
247 g_free(elem);
251 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
253 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
254 VirtQueueElement *elem;
255 VirtIOBalloonStat stat;
256 size_t offset = 0;
257 qemu_timeval tv;
259 s->stats_vq_elem = elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
260 if (!elem) {
261 goto out;
264 /* Initialize the stats to get rid of any stale values. This is only
265 * needed to handle the case where a guest supports fewer stats than it
266 * used to (ie. it has booted into an old kernel).
268 reset_stats(s);
270 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
271 == sizeof(stat)) {
272 uint16_t tag = virtio_tswap16(vdev, stat.tag);
273 uint64_t val = virtio_tswap64(vdev, stat.val);
275 offset += sizeof(stat);
276 if (tag < VIRTIO_BALLOON_S_NR)
277 s->stats[tag] = val;
279 s->stats_vq_offset = offset;
281 if (qemu_gettimeofday(&tv) < 0) {
282 fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
283 goto out;
286 s->stats_last_update = tv.tv_sec;
288 out:
289 if (balloon_stats_enabled(s)) {
290 balloon_stats_change_timer(s, s->stats_poll_interval);
294 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
296 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
297 struct virtio_balloon_config config;
299 config.num_pages = cpu_to_le32(dev->num_pages);
300 config.actual = cpu_to_le32(dev->actual);
302 trace_virtio_balloon_get_config(config.num_pages, config.actual);
303 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
306 static void virtio_balloon_set_config(VirtIODevice *vdev,
307 const uint8_t *config_data)
309 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
310 struct virtio_balloon_config config;
311 uint32_t oldactual = dev->actual;
312 ram_addr_t vm_ram_size = get_current_ram_size();
314 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
315 dev->actual = le32_to_cpu(config.actual);
316 if (dev->actual != oldactual) {
317 qapi_event_send_balloon_change(vm_ram_size -
318 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
319 &error_abort);
321 trace_virtio_balloon_set_config(dev->actual, oldactual);
324 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
325 Error **errp)
327 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
328 f |= dev->host_features;
329 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
330 return f;
333 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
335 VirtIOBalloon *dev = opaque;
336 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
337 VIRTIO_BALLOON_PFN_SHIFT);
340 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
342 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
343 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
344 ram_addr_t vm_ram_size = get_current_ram_size();
346 if (target > vm_ram_size) {
347 target = vm_ram_size;
349 if (target) {
350 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
351 virtio_notify_config(vdev);
353 trace_virtio_balloon_to_target(target, dev->num_pages);
356 static void virtio_balloon_save(QEMUFile *f, void *opaque)
358 virtio_save(VIRTIO_DEVICE(opaque), f);
361 static void virtio_balloon_save_device(VirtIODevice *vdev, QEMUFile *f)
363 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
365 qemu_put_be32(f, s->num_pages);
366 qemu_put_be32(f, s->actual);
369 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
371 if (version_id != 1)
372 return -EINVAL;
374 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
377 static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
378 int version_id)
380 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
382 s->num_pages = qemu_get_be32(f);
383 s->actual = qemu_get_be32(f);
384 return 0;
387 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
389 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
390 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
391 int ret;
393 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
394 sizeof(struct virtio_balloon_config));
396 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
397 virtio_balloon_stat, s);
399 if (ret < 0) {
400 error_setg(errp, "Only one balloon device is supported");
401 virtio_cleanup(vdev);
402 return;
405 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
406 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
407 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
409 reset_stats(s);
411 register_savevm(dev, "virtio-balloon", -1, 1,
412 virtio_balloon_save, virtio_balloon_load, s);
415 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
417 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
418 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
420 balloon_stats_destroy_timer(s);
421 qemu_remove_balloon_handler(s);
422 unregister_savevm(dev, "virtio-balloon", s);
423 virtio_cleanup(vdev);
426 static void virtio_balloon_instance_init(Object *obj)
428 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
430 object_property_add(obj, "guest-stats", "guest statistics",
431 balloon_stats_get_all, NULL, NULL, s, NULL);
433 object_property_add(obj, "guest-stats-polling-interval", "int",
434 balloon_stats_get_poll_interval,
435 balloon_stats_set_poll_interval,
436 NULL, s, NULL);
439 static Property virtio_balloon_properties[] = {
440 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
441 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
442 DEFINE_PROP_END_OF_LIST(),
445 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
447 DeviceClass *dc = DEVICE_CLASS(klass);
448 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
450 dc->props = virtio_balloon_properties;
451 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
452 vdc->realize = virtio_balloon_device_realize;
453 vdc->unrealize = virtio_balloon_device_unrealize;
454 vdc->get_config = virtio_balloon_get_config;
455 vdc->set_config = virtio_balloon_set_config;
456 vdc->get_features = virtio_balloon_get_features;
457 vdc->save = virtio_balloon_save_device;
458 vdc->load = virtio_balloon_load_device;
461 static const TypeInfo virtio_balloon_info = {
462 .name = TYPE_VIRTIO_BALLOON,
463 .parent = TYPE_VIRTIO_DEVICE,
464 .instance_size = sizeof(VirtIOBalloon),
465 .instance_init = virtio_balloon_instance_init,
466 .class_init = virtio_balloon_class_init,
469 static void virtio_register_types(void)
471 type_register_static(&virtio_balloon_info);
474 type_init(virtio_register_types)