]> git.proxmox.com Git - mirror_qemu.git/blob - hw/virtio/virtio-balloon.c
virtio-balloon: Fix balloon not working correctly when hotplug memory
[mirror_qemu.git] / hw / virtio / virtio-balloon.c
1 /*
2 * Virtio Balloon Device
3 *
4 * Copyright IBM, Corp. 2008
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 */
15
16 #include "qemu/iov.h"
17 #include "qemu/timer.h"
18 #include "qemu-common.h"
19 #include "hw/virtio/virtio.h"
20 #include "hw/i386/pc.h"
21 #include "cpu.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"
28
29 #if defined(__linux__)
30 #include <sys/mman.h>
31 #endif
32
33 #include "hw/virtio/virtio-bus.h"
34 #include "hw/virtio/virtio-access.h"
35
36 static void balloon_page(void *addr, int deflate)
37 {
38 #if defined(__linux__)
39 if (!kvm_enabled() || kvm_has_sync_mmu())
40 qemu_madvise(addr, TARGET_PAGE_SIZE,
41 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
42 #endif
43 }
44
45 static const char *balloon_stat_names[] = {
46 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
47 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
48 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
49 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
50 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
51 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
52 [VIRTIO_BALLOON_S_NR] = NULL
53 };
54
55 /*
56 * reset_stats - Mark all items in the stats array as unset
57 *
58 * This function needs to be called at device initialization and before
59 * updating to a set of newly-generated stats. This will ensure that no
60 * stale values stick around in case the guest reports a subset of the supported
61 * statistics.
62 */
63 static inline void reset_stats(VirtIOBalloon *dev)
64 {
65 int i;
66 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
67 }
68
69 static bool balloon_stats_supported(const VirtIOBalloon *s)
70 {
71 VirtIODevice *vdev = VIRTIO_DEVICE(s);
72 return vdev->guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ);
73 }
74
75 static bool balloon_stats_enabled(const VirtIOBalloon *s)
76 {
77 return s->stats_poll_interval > 0;
78 }
79
80 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
81 {
82 if (balloon_stats_enabled(s)) {
83 timer_del(s->stats_timer);
84 timer_free(s->stats_timer);
85 s->stats_timer = NULL;
86 s->stats_poll_interval = 0;
87 }
88 }
89
90 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
91 {
92 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
93 }
94
95 static void balloon_stats_poll_cb(void *opaque)
96 {
97 VirtIOBalloon *s = opaque;
98 VirtIODevice *vdev = VIRTIO_DEVICE(s);
99
100 if (!balloon_stats_supported(s)) {
101 /* re-schedule */
102 balloon_stats_change_timer(s, s->stats_poll_interval);
103 return;
104 }
105
106 virtqueue_push(s->svq, &s->stats_vq_elem, s->stats_vq_offset);
107 virtio_notify(vdev, s->svq);
108 }
109
110 static void balloon_stats_get_all(Object *obj, struct Visitor *v,
111 void *opaque, const char *name, Error **errp)
112 {
113 Error *err = NULL;
114 VirtIOBalloon *s = opaque;
115 int i;
116
117 visit_start_struct(v, NULL, "guest-stats", name, 0, &err);
118 if (err) {
119 goto out;
120 }
121 visit_type_int(v, &s->stats_last_update, "last-update", &err);
122 if (err) {
123 goto out_end;
124 }
125
126 visit_start_struct(v, NULL, NULL, "stats", 0, &err);
127 if (err) {
128 goto out_end;
129 }
130 for (i = 0; !err && i < VIRTIO_BALLOON_S_NR; i++) {
131 visit_type_int64(v, (int64_t *) &s->stats[i], balloon_stat_names[i],
132 &err);
133 }
134 error_propagate(errp, err);
135 err = NULL;
136 visit_end_struct(v, &err);
137
138 out_end:
139 error_propagate(errp, err);
140 err = NULL;
141 visit_end_struct(v, &err);
142 out:
143 error_propagate(errp, err);
144 }
145
146 static void balloon_stats_get_poll_interval(Object *obj, struct Visitor *v,
147 void *opaque, const char *name,
148 Error **errp)
149 {
150 VirtIOBalloon *s = opaque;
151 visit_type_int(v, &s->stats_poll_interval, name, errp);
152 }
153
154 static void balloon_stats_set_poll_interval(Object *obj, struct Visitor *v,
155 void *opaque, const char *name,
156 Error **errp)
157 {
158 VirtIOBalloon *s = opaque;
159 Error *local_err = NULL;
160 int64_t value;
161
162 visit_type_int(v, &value, name, &local_err);
163 if (local_err) {
164 error_propagate(errp, local_err);
165 return;
166 }
167
168 if (value < 0) {
169 error_setg(errp, "timer value must be greater than zero");
170 return;
171 }
172
173 if (value > UINT32_MAX) {
174 error_setg(errp, "timer value is too big");
175 return;
176 }
177
178 if (value == s->stats_poll_interval) {
179 return;
180 }
181
182 if (value == 0) {
183 /* timer=0 disables the timer */
184 balloon_stats_destroy_timer(s);
185 return;
186 }
187
188 if (balloon_stats_enabled(s)) {
189 /* timer interval change */
190 s->stats_poll_interval = value;
191 balloon_stats_change_timer(s, value);
192 return;
193 }
194
195 /* create a new timer */
196 g_assert(s->stats_timer == NULL);
197 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
198 s->stats_poll_interval = value;
199 balloon_stats_change_timer(s, 0);
200 }
201
202 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
203 {
204 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
205 VirtQueueElement elem;
206 MemoryRegionSection section;
207
208 while (virtqueue_pop(vq, &elem)) {
209 size_t offset = 0;
210 uint32_t pfn;
211
212 while (iov_to_buf(elem.out_sg, elem.out_num, offset, &pfn, 4) == 4) {
213 ram_addr_t pa;
214 ram_addr_t addr;
215 int p = virtio_ldl_p(vdev, &pfn);
216
217 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
218 offset += 4;
219
220 /* FIXME: remove get_system_memory(), but how? */
221 section = memory_region_find(get_system_memory(), pa, 1);
222 if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
223 continue;
224
225 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
226 should be OK because we only want a single page. */
227 addr = section.offset_within_region;
228 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
229 !!(vq == s->dvq));
230 memory_region_unref(section.mr);
231 }
232
233 virtqueue_push(vq, &elem, offset);
234 virtio_notify(vdev, vq);
235 }
236 }
237
238 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
239 {
240 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
241 VirtQueueElement *elem = &s->stats_vq_elem;
242 VirtIOBalloonStat stat;
243 size_t offset = 0;
244 qemu_timeval tv;
245
246 if (!virtqueue_pop(vq, elem)) {
247 goto out;
248 }
249
250 /* Initialize the stats to get rid of any stale values. This is only
251 * needed to handle the case where a guest supports fewer stats than it
252 * used to (ie. it has booted into an old kernel).
253 */
254 reset_stats(s);
255
256 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
257 == sizeof(stat)) {
258 uint16_t tag = virtio_tswap16(vdev, stat.tag);
259 uint64_t val = virtio_tswap64(vdev, stat.val);
260
261 offset += sizeof(stat);
262 if (tag < VIRTIO_BALLOON_S_NR)
263 s->stats[tag] = val;
264 }
265 s->stats_vq_offset = offset;
266
267 if (qemu_gettimeofday(&tv) < 0) {
268 fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
269 goto out;
270 }
271
272 s->stats_last_update = tv.tv_sec;
273
274 out:
275 if (balloon_stats_enabled(s)) {
276 balloon_stats_change_timer(s, s->stats_poll_interval);
277 }
278 }
279
280 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
281 {
282 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
283 struct virtio_balloon_config config;
284
285 config.num_pages = cpu_to_le32(dev->num_pages);
286 config.actual = cpu_to_le32(dev->actual);
287
288 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
289 }
290
291 static void virtio_balloon_set_config(VirtIODevice *vdev,
292 const uint8_t *config_data)
293 {
294 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
295 struct virtio_balloon_config config;
296 uint32_t oldactual = dev->actual;
297 ram_addr_t vm_ram_size = get_current_ram_size();
298
299 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
300 dev->actual = le32_to_cpu(config.actual);
301 if (dev->actual != oldactual) {
302 qapi_event_send_balloon_change(vm_ram_size -
303 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
304 &error_abort);
305 }
306 }
307
308 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
309 {
310 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
311 return f;
312 }
313
314 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
315 {
316 VirtIOBalloon *dev = opaque;
317 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
318 VIRTIO_BALLOON_PFN_SHIFT);
319 }
320
321 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
322 {
323 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
324 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
325 ram_addr_t vm_ram_size = get_current_ram_size();
326
327 if (target > vm_ram_size) {
328 target = vm_ram_size;
329 }
330 if (target) {
331 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
332 virtio_notify_config(vdev);
333 }
334 }
335
336 static void virtio_balloon_save(QEMUFile *f, void *opaque)
337 {
338 virtio_save(VIRTIO_DEVICE(opaque), f);
339 }
340
341 static void virtio_balloon_save_device(VirtIODevice *vdev, QEMUFile *f)
342 {
343 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
344
345 qemu_put_be32(f, s->num_pages);
346 qemu_put_be32(f, s->actual);
347 }
348
349 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
350 {
351 if (version_id != 1)
352 return -EINVAL;
353
354 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
355 }
356
357 static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
358 int version_id)
359 {
360 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
361
362 s->num_pages = qemu_get_be32(f);
363 s->actual = qemu_get_be32(f);
364 return 0;
365 }
366
367 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
368 {
369 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
370 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
371 int ret;
372
373 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
374 sizeof(struct virtio_balloon_config));
375
376 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
377 virtio_balloon_stat, s);
378
379 if (ret < 0) {
380 error_setg(errp, "Adding balloon handler failed");
381 virtio_cleanup(vdev);
382 return;
383 }
384
385 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
386 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
387 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
388
389 reset_stats(s);
390
391 register_savevm(dev, "virtio-balloon", -1, 1,
392 virtio_balloon_save, virtio_balloon_load, s);
393
394 object_property_add(OBJECT(dev), "guest-stats", "guest statistics",
395 balloon_stats_get_all, NULL, NULL, s, NULL);
396
397 object_property_add(OBJECT(dev), "guest-stats-polling-interval", "int",
398 balloon_stats_get_poll_interval,
399 balloon_stats_set_poll_interval,
400 NULL, s, NULL);
401 }
402
403 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
404 {
405 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
406 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
407
408 balloon_stats_destroy_timer(s);
409 qemu_remove_balloon_handler(s);
410 unregister_savevm(dev, "virtio-balloon", s);
411 virtio_cleanup(vdev);
412 }
413
414 static Property virtio_balloon_properties[] = {
415 DEFINE_PROP_END_OF_LIST(),
416 };
417
418 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
419 {
420 DeviceClass *dc = DEVICE_CLASS(klass);
421 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
422
423 dc->props = virtio_balloon_properties;
424 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
425 vdc->realize = virtio_balloon_device_realize;
426 vdc->unrealize = virtio_balloon_device_unrealize;
427 vdc->get_config = virtio_balloon_get_config;
428 vdc->set_config = virtio_balloon_set_config;
429 vdc->get_features = virtio_balloon_get_features;
430 vdc->save = virtio_balloon_save_device;
431 vdc->load = virtio_balloon_load_device;
432 }
433
434 static const TypeInfo virtio_balloon_info = {
435 .name = TYPE_VIRTIO_BALLOON,
436 .parent = TYPE_VIRTIO_DEVICE,
437 .instance_size = sizeof(VirtIOBalloon),
438 .class_init = virtio_balloon_class_init,
439 };
440
441 static void virtio_register_types(void)
442 {
443 type_register_static(&virtio_balloon_info);
444 }
445
446 type_init(virtio_register_types)