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-common.h"
22 #include "virtio-balloon.h"
23 #include "virtio-transport.h"
25 #include "exec-memory.h"
27 #if defined(__linux__)
31 typedef struct VirtIOBalloon
34 VirtQueue *ivq, *dvq, *svq;
37 uint64_t stats[VIRTIO_BALLOON_S_NR];
38 VirtQueueElement stats_vq_elem;
39 size_t stats_vq_offset;
43 static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
45 return (VirtIOBalloon *)vdev;
48 static void balloon_page(void *addr, int deflate)
50 #if defined(__linux__)
51 if (!kvm_enabled() || kvm_has_sync_mmu())
52 qemu_madvise(addr, TARGET_PAGE_SIZE,
53 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
58 * reset_stats - Mark all items in the stats array as unset
60 * This function needs to be called at device intialization and before
61 * before updating to a set of newly-generated stats. This will ensure that no
62 * stale values stick around in case the guest reports a subset of the supported
65 static inline void reset_stats(VirtIOBalloon *dev)
68 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
71 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
73 VirtIOBalloon *s = to_virtio_balloon(vdev);
74 VirtQueueElement elem;
75 MemoryRegionSection section;
77 while (virtqueue_pop(vq, &elem)) {
81 while (iov_to_buf(elem.out_sg, elem.out_num, offset, &pfn, 4) == 4) {
85 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
88 /* FIXME: remove get_system_memory(), but how? */
89 section = memory_region_find(get_system_memory(), pa, 1);
90 if (!section.size || !memory_region_is_ram(section.mr))
93 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
94 should be OK because we only want a single page. */
95 addr = section.offset_within_region;
96 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
100 virtqueue_push(vq, &elem, offset);
101 virtio_notify(vdev, vq);
105 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
107 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
108 VirtQueueElement *elem = &s->stats_vq_elem;
109 VirtIOBalloonStat stat;
112 if (!virtqueue_pop(vq, elem)) {
116 /* Initialize the stats to get rid of any stale values. This is only
117 * needed to handle the case where a guest supports fewer stats than it
118 * used to (ie. it has booted into an old kernel).
122 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
124 uint16_t tag = tswap16(stat.tag);
125 uint64_t val = tswap64(stat.val);
127 offset += sizeof(stat);
128 if (tag < VIRTIO_BALLOON_S_NR)
131 s->stats_vq_offset = offset;
134 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
136 VirtIOBalloon *dev = to_virtio_balloon(vdev);
137 struct virtio_balloon_config config;
139 config.num_pages = cpu_to_le32(dev->num_pages);
140 config.actual = cpu_to_le32(dev->actual);
142 memcpy(config_data, &config, 8);
145 static void virtio_balloon_set_config(VirtIODevice *vdev,
146 const uint8_t *config_data)
148 VirtIOBalloon *dev = to_virtio_balloon(vdev);
149 struct virtio_balloon_config config;
150 uint32_t oldactual = dev->actual;
151 memcpy(&config, config_data, 8);
152 dev->actual = le32_to_cpu(config.actual);
153 if (dev->actual != oldactual) {
154 qemu_balloon_changed(ram_size -
155 (dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
159 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
161 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
165 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
167 VirtIOBalloon *dev = opaque;
170 /* Disable guest-provided stats for now. For more details please check:
171 * https://bugzilla.redhat.com/show_bug.cgi?id=623903
173 * If you do enable it (which is probably not going to happen as we
174 * need a new command for it), remember that you also need to fill the
175 * appropriate members of the BalloonInfo structure so that the stats
176 * are returned to the client.
178 if (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ)) {
179 virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
180 virtio_notify(&dev->vdev, dev->svq);
185 /* Stats are not supported. Clear out any stale values that might
186 * have been set by a more featureful guest kernel.
190 info->actual = ram_size - ((uint64_t) dev->actual <<
191 VIRTIO_BALLOON_PFN_SHIFT);
194 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
196 VirtIOBalloon *dev = opaque;
198 if (target > ram_size) {
202 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
203 virtio_notify_config(&dev->vdev);
207 static void virtio_balloon_save(QEMUFile *f, void *opaque)
209 VirtIOBalloon *s = opaque;
211 virtio_save(&s->vdev, f);
213 qemu_put_be32(f, s->num_pages);
214 qemu_put_be32(f, s->actual);
217 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
219 VirtIOBalloon *s = opaque;
225 ret = virtio_load(&s->vdev, f);
230 s->num_pages = qemu_get_be32(f);
231 s->actual = qemu_get_be32(f);
235 VirtIODevice *virtio_balloon_init(DeviceState *dev)
240 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
242 8, sizeof(VirtIOBalloon));
244 s->vdev.get_config = virtio_balloon_get_config;
245 s->vdev.set_config = virtio_balloon_set_config;
246 s->vdev.get_features = virtio_balloon_get_features;
248 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
249 virtio_balloon_stat, s);
251 virtio_cleanup(&s->vdev);
255 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
256 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
257 s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
262 register_savevm(dev, "virtio-balloon", -1, 1,
263 virtio_balloon_save, virtio_balloon_load, s);
268 void virtio_balloon_exit(VirtIODevice *vdev)
270 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
272 qemu_remove_balloon_handler(s);
273 unregister_savevm(s->qdev, "virtio-balloon", s);
274 virtio_cleanup(vdev);
277 /******************** VirtIOBaloon Device **********************/
279 static int virtio_balloondev_init(DeviceState *dev)
282 VirtIOBaloonState *s = VIRTIO_BALLOON_FROM_QDEV(dev);
283 vdev = virtio_balloon_init(dev);
288 assert(s->trl != NULL);
290 return virtio_call_backend_init_cb(dev, s->trl, vdev);
293 static Property virtio_balloon_properties[] = {
294 DEFINE_PROP_END_OF_LIST(),
297 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
299 DeviceClass *dc = DEVICE_CLASS(klass);
300 dc->init = virtio_balloondev_init;
301 dc->props = virtio_balloon_properties;
304 static TypeInfo virtio_balloon_info = {
305 .name = "virtio-balloon",
306 .parent = TYPE_DEVICE,
307 .instance_size = sizeof(VirtIOBaloonState),
308 .class_init = virtio_balloon_class_init,
311 static void virtio_baloon_register_types(void)
313 type_register_static(&virtio_balloon_info);
316 type_init(virtio_baloon_register_types)