Merge commit '30f5041ef1ba534af9308d840bf359a50597ba5d' into upstream-merge
[qemu-kvm/stefanha.git] / hw / virtio-balloon.c
blob1e6be18664fe1f578d1a7805f826a0018df694b0
1 /*
2 * Virtio Block Device
4 * Copyright IBM, Corp. 2008
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "iov.h"
15 #include "qemu-common.h"
16 #include "virtio.h"
17 #include "pc.h"
18 #include "sysemu.h"
19 #include "cpu.h"
20 #include "monitor.h"
21 #include "balloon.h"
22 #include "virtio-balloon.h"
23 #include "kvm.h"
24 #include "qemu-kvm.h"
25 #include "qlist.h"
26 #include "qint.h"
27 #include "qstring.h"
29 #if defined(__linux__)
30 #include <sys/mman.h>
31 #endif
33 /* Disable guest-provided stats by now (https://bugzilla.redhat.com/show_bug.cgi?id=623903) */
34 #define ENABLE_GUEST_STATS 0
37 typedef struct VirtIOBalloon
39 VirtIODevice vdev;
40 VirtQueue *ivq, *dvq, *svq;
41 uint32_t num_pages;
42 uint32_t actual;
43 uint64_t stats[VIRTIO_BALLOON_S_NR];
44 VirtQueueElement stats_vq_elem;
45 size_t stats_vq_offset;
46 MonitorCompletion *stats_callback;
47 void *stats_opaque_callback_data;
48 } VirtIOBalloon;
50 static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
52 return (VirtIOBalloon *)vdev;
55 static void balloon_page(void *addr, int deflate)
57 #if defined(__linux__)
58 if (!kvm_enabled() || kvm_has_sync_mmu())
59 qemu_madvise(addr, TARGET_PAGE_SIZE,
60 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
61 #endif
65 * reset_stats - Mark all items in the stats array as unset
67 * This function needs to be called at device intialization and before
68 * before updating to a set of newly-generated stats. This will ensure that no
69 * stale values stick around in case the guest reports a subset of the supported
70 * statistics.
72 static inline void reset_stats(VirtIOBalloon *dev)
74 int i;
75 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
78 static void stat_put(QDict *dict, const char *label, uint64_t val)
80 if (val != -1)
81 qdict_put(dict, label, qint_from_int(val));
84 static QObject *get_stats_qobject(VirtIOBalloon *dev)
86 QDict *dict = qdict_new();
87 uint64_t actual = ram_size - ((uint64_t) dev->actual <<
88 VIRTIO_BALLOON_PFN_SHIFT);
90 stat_put(dict, "actual", actual);
91 #if ENABLE_GUEST_STATS
92 stat_put(dict, "mem_swapped_in", dev->stats[VIRTIO_BALLOON_S_SWAP_IN]);
93 stat_put(dict, "mem_swapped_out", dev->stats[VIRTIO_BALLOON_S_SWAP_OUT]);
94 stat_put(dict, "major_page_faults", dev->stats[VIRTIO_BALLOON_S_MAJFLT]);
95 stat_put(dict, "minor_page_faults", dev->stats[VIRTIO_BALLOON_S_MINFLT]);
96 stat_put(dict, "free_mem", dev->stats[VIRTIO_BALLOON_S_MEMFREE]);
97 stat_put(dict, "total_mem", dev->stats[VIRTIO_BALLOON_S_MEMTOT]);
98 #endif
100 return QOBJECT(dict);
103 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
105 VirtIOBalloon *s = to_virtio_balloon(vdev);
106 VirtQueueElement elem;
108 while (virtqueue_pop(vq, &elem)) {
109 size_t offset = 0;
110 uint32_t pfn;
112 while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) {
113 ram_addr_t pa;
114 ram_addr_t addr;
116 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
117 offset += 4;
119 addr = cpu_get_physical_page_desc(pa);
120 if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
121 continue;
123 /* Using qemu_get_ram_ptr is bending the rules a bit, but
124 should be OK because we only want a single page. */
125 balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq));
128 virtqueue_push(vq, &elem, offset);
129 virtio_notify(vdev, vq);
133 static void complete_stats_request(VirtIOBalloon *vb)
135 QObject *stats;
137 if (!vb->stats_opaque_callback_data)
138 return;
140 stats = get_stats_qobject(vb);
141 vb->stats_callback(vb->stats_opaque_callback_data, stats);
142 qobject_decref(stats);
143 vb->stats_opaque_callback_data = NULL;
144 vb->stats_callback = NULL;
147 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
149 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
150 VirtQueueElement *elem = &s->stats_vq_elem;
151 VirtIOBalloonStat stat;
152 size_t offset = 0;
154 if (!virtqueue_pop(vq, elem)) {
155 return;
158 /* Initialize the stats to get rid of any stale values. This is only
159 * needed to handle the case where a guest supports fewer stats than it
160 * used to (ie. it has booted into an old kernel).
162 reset_stats(s);
164 while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat))
165 == sizeof(stat)) {
166 uint16_t tag = tswap16(stat.tag);
167 uint64_t val = tswap64(stat.val);
169 offset += sizeof(stat);
170 if (tag < VIRTIO_BALLOON_S_NR)
171 s->stats[tag] = val;
173 s->stats_vq_offset = offset;
175 complete_stats_request(s);
178 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
180 VirtIOBalloon *dev = to_virtio_balloon(vdev);
181 struct virtio_balloon_config config;
183 config.num_pages = cpu_to_le32(dev->num_pages);
184 config.actual = cpu_to_le32(dev->actual);
186 memcpy(config_data, &config, 8);
189 static void virtio_balloon_set_config(VirtIODevice *vdev,
190 const uint8_t *config_data)
192 VirtIOBalloon *dev = to_virtio_balloon(vdev);
193 struct virtio_balloon_config config;
194 memcpy(&config, config_data, 8);
195 dev->actual = config.actual;
198 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
200 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
201 return f;
204 static void virtio_balloon_to_target(void *opaque, ram_addr_t target,
205 MonitorCompletion cb, void *cb_data)
207 VirtIOBalloon *dev = opaque;
209 if (target > ram_size)
210 target = ram_size;
212 if (target) {
213 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
214 virtio_notify_config(&dev->vdev);
215 } else {
216 /* For now, only allow one request at a time. This restriction can be
217 * removed later by queueing callback and data pairs.
219 if (dev->stats_callback != NULL) {
220 return;
222 dev->stats_callback = cb;
223 dev->stats_opaque_callback_data = cb_data;
224 if (ENABLE_GUEST_STATS && (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ))) {
225 virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
226 virtio_notify(&dev->vdev, dev->svq);
227 } else {
228 /* Stats are not supported. Clear out any stale values that might
229 * have been set by a more featureful guest kernel.
231 reset_stats(dev);
232 complete_stats_request(dev);
237 static void virtio_balloon_save(QEMUFile *f, void *opaque)
239 VirtIOBalloon *s = opaque;
241 virtio_save(&s->vdev, f);
243 qemu_put_be32(f, s->num_pages);
244 qemu_put_be32(f, s->actual);
247 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
249 VirtIOBalloon *s = opaque;
251 if (version_id != 1)
252 return -EINVAL;
254 virtio_load(&s->vdev, f);
256 s->num_pages = qemu_get_be32(f);
257 s->actual = qemu_get_be32(f);
258 return 0;
261 VirtIODevice *virtio_balloon_init(DeviceState *dev)
263 VirtIOBalloon *s;
265 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
266 VIRTIO_ID_BALLOON,
267 8, sizeof(VirtIOBalloon));
269 s->vdev.get_config = virtio_balloon_get_config;
270 s->vdev.set_config = virtio_balloon_set_config;
271 s->vdev.get_features = virtio_balloon_get_features;
273 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
274 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
275 s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
277 reset_stats(s);
278 qemu_add_balloon_handler(virtio_balloon_to_target, s);
280 register_savevm(dev, "virtio-balloon", -1, 1,
281 virtio_balloon_save, virtio_balloon_load, s);
283 return &s->vdev;