]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio-balloon.c
Migration: Add i82801b11 migration data
[mirror_qemu.git] / hw / virtio / virtio-balloon.c
CommitLineData
bd322087 1/*
d4443cb6 2 * Virtio Balloon Device
bd322087
AL
3 *
4 * Copyright IBM, Corp. 2008
d4443cb6
AS
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
bd322087
AL
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
9b8bfe21 16#include "qemu/osdep.h"
1de7afc9 17#include "qemu/iov.h"
7e6ccd9c 18#include "qemu/timer.h"
bd322087 19#include "qemu-common.h"
0d09e41a
PB
20#include "hw/virtio/virtio.h"
21#include "hw/i386/pc.h"
bd322087 22#include "cpu.h"
9c17d615 23#include "sysemu/balloon.h"
0d09e41a 24#include "hw/virtio/virtio-balloon.h"
9c17d615 25#include "sysemu/kvm.h"
022c62cb 26#include "exec/address-spaces.h"
7e6ccd9c 27#include "qapi/visitor.h"
aef9d311 28#include "qapi-event.h"
6adfdc5a 29#include "trace.h"
bd322087
AL
30
31#if defined(__linux__)
32#include <sys/mman.h>
33#endif
34
0d09e41a 35#include "hw/virtio/virtio-bus.h"
8609d2a8 36#include "hw/virtio/virtio-access.h"
1ab461b5 37
bd322087
AL
38static void balloon_page(void *addr, int deflate)
39{
40#if defined(__linux__)
371ff5a3
DDAG
41 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
42 kvm_has_sync_mmu())) {
e78815a5
AF
43 qemu_madvise(addr, TARGET_PAGE_SIZE,
44 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
371ff5a3 45 }
bd322087
AL
46#endif
47}
48
7e6ccd9c
LC
49static 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",
a0d06486 56 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
7e6ccd9c
LC
57 [VIRTIO_BALLOON_S_NR] = NULL
58};
59
625a5bef
AL
60/*
61 * reset_stats - Mark all items in the stats array as unset
62 *
52f35022
SW
63 * This function needs to be called at device initialization and before
64 * updating to a set of newly-generated stats. This will ensure that no
625a5bef
AL
65 * stale values stick around in case the guest reports a subset of the supported
66 * statistics.
67 */
68static inline void reset_stats(VirtIOBalloon *dev)
69{
70 int i;
71 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
72}
73
7e6ccd9c
LC
74static bool balloon_stats_supported(const VirtIOBalloon *s)
75{
c96caced 76 VirtIODevice *vdev = VIRTIO_DEVICE(s);
95129d6f 77 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
7e6ccd9c
LC
78}
79
80static bool balloon_stats_enabled(const VirtIOBalloon *s)
81{
82 return s->stats_poll_interval > 0;
83}
84
85static void balloon_stats_destroy_timer(VirtIOBalloon *s)
86{
87 if (balloon_stats_enabled(s)) {
bc72ad67
AB
88 timer_del(s->stats_timer);
89 timer_free(s->stats_timer);
7e6ccd9c
LC
90 s->stats_timer = NULL;
91 s->stats_poll_interval = 0;
92 }
93}
94
1f9296b5 95static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
7e6ccd9c 96{
bc72ad67 97 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
7e6ccd9c
LC
98}
99
100static void balloon_stats_poll_cb(void *opaque)
101{
102 VirtIOBalloon *s = opaque;
c96caced 103 VirtIODevice *vdev = VIRTIO_DEVICE(s);
7e6ccd9c 104
4eae2a65 105 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
7e6ccd9c
LC
106 /* re-schedule */
107 balloon_stats_change_timer(s, s->stats_poll_interval);
108 return;
109 }
110
51b19ebe 111 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
c96caced 112 virtio_notify(vdev, s->svq);
51b19ebe
PB
113 g_free(s->stats_vq_elem);
114 s->stats_vq_elem = NULL;
7e6ccd9c
LC
115}
116
d7bce999
EB
117static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
118 void *opaque, Error **errp)
7e6ccd9c 119{
2ddb16a9 120 Error *err = NULL;
7e6ccd9c
LC
121 VirtIOBalloon *s = opaque;
122 int i;
123
337283df 124 visit_start_struct(v, name, NULL, 0, &err);
2ddb16a9
MA
125 if (err) {
126 goto out;
127 }
51e72bc1 128 visit_type_int(v, "last-update", &s->stats_last_update, &err);
297a3646
MA
129 if (err) {
130 goto out_end;
131 }
7e6ccd9c 132
337283df 133 visit_start_struct(v, "stats", NULL, 0, &err);
2ddb16a9
MA
134 if (err) {
135 goto out_end;
136 }
9dbb8fa7 137 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
51e72bc1 138 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
9dbb8fa7
EB
139 if (err) {
140 break;
141 }
7e6ccd9c 142 }
297a3646
MA
143 error_propagate(errp, err);
144 err = NULL;
2ddb16a9
MA
145 visit_end_struct(v, &err);
146
147out_end:
297a3646
MA
148 error_propagate(errp, err);
149 err = NULL;
2ddb16a9 150 visit_end_struct(v, &err);
2ddb16a9
MA
151out:
152 error_propagate(errp, err);
7e6ccd9c
LC
153}
154
4fa45492 155static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
d7bce999 156 const char *name, void *opaque,
7e6ccd9c
LC
157 Error **errp)
158{
159 VirtIOBalloon *s = opaque;
51e72bc1 160 visit_type_int(v, name, &s->stats_poll_interval, errp);
7e6ccd9c
LC
161}
162
4fa45492 163static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
d7bce999 164 const char *name, void *opaque,
7e6ccd9c
LC
165 Error **errp)
166{
167 VirtIOBalloon *s = opaque;
65cd9064 168 Error *local_err = NULL;
7e6ccd9c
LC
169 int64_t value;
170
51e72bc1 171 visit_type_int(v, name, &value, &local_err);
65cd9064
MA
172 if (local_err) {
173 error_propagate(errp, local_err);
7e6ccd9c
LC
174 return;
175 }
176
177 if (value < 0) {
178 error_setg(errp, "timer value must be greater than zero");
179 return;
180 }
181
22644cd2 182 if (value > UINT32_MAX) {
1f9296b5
LC
183 error_setg(errp, "timer value is too big");
184 return;
185 }
186
7e6ccd9c
LC
187 if (value == s->stats_poll_interval) {
188 return;
189 }
190
191 if (value == 0) {
192 /* timer=0 disables the timer */
193 balloon_stats_destroy_timer(s);
194 return;
195 }
196
197 if (balloon_stats_enabled(s)) {
198 /* timer interval change */
199 s->stats_poll_interval = value;
200 balloon_stats_change_timer(s, value);
201 return;
202 }
203
204 /* create a new timer */
205 g_assert(s->stats_timer == NULL);
bc72ad67 206 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
7e6ccd9c
LC
207 s->stats_poll_interval = value;
208 balloon_stats_change_timer(s, 0);
209}
210
bd322087
AL
211static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
212{
c96caced 213 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
51b19ebe 214 VirtQueueElement *elem;
b7c28c74 215 MemoryRegionSection section;
bd322087 216
51b19ebe 217 for (;;) {
bd322087
AL
218 size_t offset = 0;
219 uint32_t pfn;
51b19ebe
PB
220 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
221 if (!elem) {
222 return;
223 }
bd322087 224
51b19ebe 225 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
c227f099
AL
226 ram_addr_t pa;
227 ram_addr_t addr;
8609d2a8 228 int p = virtio_ldl_p(vdev, &pfn);
bd322087 229
8609d2a8 230 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
bd322087
AL
231 offset += 4;
232
b7c28c74
AK
233 /* FIXME: remove get_system_memory(), but how? */
234 section = memory_region_find(get_system_memory(), pa, 1);
052e87b0 235 if (!int128_nz(section.size) || !memory_region_is_ram(section.mr))
bd322087
AL
236 continue;
237
6adfdc5a
HZ
238 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
239 pa);
b7c28c74 240 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
5c130f65 241 should be OK because we only want a single page. */
b7c28c74
AK
242 addr = section.offset_within_region;
243 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
244 !!(vq == s->dvq));
dfde4e6e 245 memory_region_unref(section.mr);
bd322087
AL
246 }
247
51b19ebe 248 virtqueue_push(vq, elem, offset);
bd322087 249 virtio_notify(vdev, vq);
51b19ebe 250 g_free(elem);
bd322087
AL
251 }
252}
253
625a5bef
AL
254static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
255{
c96caced 256 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
51b19ebe 257 VirtQueueElement *elem;
625a5bef
AL
258 VirtIOBalloonStat stat;
259 size_t offset = 0;
7e6ccd9c 260 qemu_timeval tv;
625a5bef 261
4eae2a65 262 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
51b19ebe 263 if (!elem) {
7e6ccd9c 264 goto out;
625a5bef
AL
265 }
266
4eae2a65
LP
267 if (s->stats_vq_elem != NULL) {
268 /* This should never happen if the driver follows the spec. */
269 virtqueue_push(vq, s->stats_vq_elem, 0);
270 virtio_notify(vdev, vq);
271 g_free(s->stats_vq_elem);
272 }
273
274 s->stats_vq_elem = elem;
275
625a5bef
AL
276 /* Initialize the stats to get rid of any stale values. This is only
277 * needed to handle the case where a guest supports fewer stats than it
278 * used to (ie. it has booted into an old kernel).
279 */
280 reset_stats(s);
281
dcf6f5e1 282 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
fa6111f2 283 == sizeof(stat)) {
8609d2a8
RR
284 uint16_t tag = virtio_tswap16(vdev, stat.tag);
285 uint64_t val = virtio_tswap64(vdev, stat.val);
625a5bef
AL
286
287 offset += sizeof(stat);
288 if (tag < VIRTIO_BALLOON_S_NR)
289 s->stats[tag] = val;
290 }
291 s->stats_vq_offset = offset;
7e6ccd9c
LC
292
293 if (qemu_gettimeofday(&tv) < 0) {
294 fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
295 goto out;
296 }
297
298 s->stats_last_update = tv.tv_sec;
299
300out:
301 if (balloon_stats_enabled(s)) {
302 balloon_stats_change_timer(s, s->stats_poll_interval);
303 }
625a5bef
AL
304}
305
bd322087
AL
306static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
307{
c96caced 308 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
bd322087
AL
309 struct virtio_balloon_config config;
310
311 config.num_pages = cpu_to_le32(dev->num_pages);
312 config.actual = cpu_to_le32(dev->actual);
313
6adfdc5a 314 trace_virtio_balloon_get_config(config.num_pages, config.actual);
e6baf613 315 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
bd322087
AL
316}
317
2b75f848
VSO
318static int build_dimm_list(Object *obj, void *opaque)
319{
320 GSList **list = opaque;
321
322 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
323 DeviceState *dev = DEVICE(obj);
324 if (dev->realized) { /* only realized DIMMs matter */
325 *list = g_slist_prepend(*list, dev);
326 }
327 }
328
329 object_child_foreach(obj, build_dimm_list, opaque);
330 return 0;
331}
332
39de9984
VSO
333static ram_addr_t get_current_ram_size(void)
334{
e8dc06d2 335 GSList *list = NULL, *item;
39de9984
VSO
336 ram_addr_t size = ram_size;
337
2b75f848 338 build_dimm_list(qdev_get_machine(), &list);
e8dc06d2
VSO
339 for (item = list; item; item = g_slist_next(item)) {
340 Object *obj = OBJECT(item->data);
2b75f848
VSO
341 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
342 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
343 &error_abort);
344 }
39de9984 345 }
e8dc06d2 346 g_slist_free(list);
39de9984
VSO
347
348 return size;
349}
350
bd322087
AL
351static void virtio_balloon_set_config(VirtIODevice *vdev,
352 const uint8_t *config_data)
353{
c96caced 354 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
bd322087 355 struct virtio_balloon_config config;
973603a8 356 uint32_t oldactual = dev->actual;
463756d0
HZ
357 ram_addr_t vm_ram_size = get_current_ram_size();
358
e6baf613 359 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
e54f1771 360 dev->actual = le32_to_cpu(config.actual);
973603a8 361 if (dev->actual != oldactual) {
463756d0 362 qapi_event_send_balloon_change(vm_ram_size -
aef9d311
WX
363 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
364 &error_abort);
973603a8 365 }
6adfdc5a 366 trace_virtio_balloon_set_config(dev->actual, oldactual);
bd322087
AL
367}
368
9d5b731d
JW
369static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
370 Error **errp)
bd322087 371{
e3816255
DL
372 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
373 f |= dev->host_features;
40de55af 374 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
8172539d 375 return f;
bd322087
AL
376}
377
96637bcd 378static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
dce911c7
AS
379{
380 VirtIOBalloon *dev = opaque;
463756d0
HZ
381 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
382 VIRTIO_BALLOON_PFN_SHIFT);
dce911c7
AS
383}
384
30fb2ca6 385static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
bd322087 386{
c96caced
FK
387 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
388 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
463756d0 389 ram_addr_t vm_ram_size = get_current_ram_size();
bd322087 390
463756d0
HZ
391 if (target > vm_ram_size) {
392 target = vm_ram_size;
dce911c7 393 }
bd322087 394 if (target) {
463756d0 395 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
c96caced 396 virtio_notify_config(vdev);
bd322087 397 }
6adfdc5a 398 trace_virtio_balloon_to_target(target, dev->num_pages);
bd322087
AL
399}
400
401static void virtio_balloon_save(QEMUFile *f, void *opaque)
402{
9ea2511c
GK
403 virtio_save(VIRTIO_DEVICE(opaque), f);
404}
bd322087 405
9ea2511c
GK
406static void virtio_balloon_save_device(VirtIODevice *vdev, QEMUFile *f)
407{
408 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
bd322087
AL
409
410 qemu_put_be32(f, s->num_pages);
411 qemu_put_be32(f, s->actual);
412}
413
414static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
415{
bd322087
AL
416 if (version_id != 1)
417 return -EINVAL;
418
9ea2511c
GK
419 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
420}
421
422static int virtio_balloon_load_device(VirtIODevice *vdev, QEMUFile *f,
423 int version_id)
424{
425 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
bd322087
AL
426
427 s->num_pages = qemu_get_be32(f);
428 s->actual = qemu_get_be32(f);
bd322087
AL
429 return 0;
430}
431
74def47c 432static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
bd322087 433{
74def47c 434 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
a546fb17 435 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
f76f6655 436 int ret;
bd322087 437
e6baf613
LC
438 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
439 sizeof(struct virtio_balloon_config));
bd322087 440
f76f6655
AS
441 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
442 virtio_balloon_stat, s);
5c7d0962 443
1ab461b5 444 if (ret < 0) {
46abb812 445 error_setg(errp, "Only one balloon device is supported");
a546fb17 446 virtio_cleanup(vdev);
74def47c 447 return;
1ab461b5 448 }
f76f6655 449
5c7d0962
FK
450 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
451 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
452 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
bd322087 453
38dbd48b
JT
454 reset_stats(s);
455
a546fb17 456 register_savevm(dev, "virtio-balloon", -1, 1,
0be71e32 457 virtio_balloon_save, virtio_balloon_load, s);
1ab461b5
FK
458}
459
306ec6c3 460static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
1ab461b5 461{
306ec6c3
AF
462 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
463 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
1ab461b5
FK
464
465 balloon_stats_destroy_timer(s);
466 qemu_remove_balloon_handler(s);
306ec6c3 467 unregister_savevm(dev, "virtio-balloon", s);
6a1a8cc7 468 virtio_cleanup(vdev);
1ab461b5
FK
469}
470
4eae2a65
LP
471static void virtio_balloon_device_reset(VirtIODevice *vdev)
472{
473 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
474
475 if (s->stats_vq_elem != NULL) {
476 g_free(s->stats_vq_elem);
477 s->stats_vq_elem = NULL;
478 }
479}
480
1190044e
SZ
481static void virtio_balloon_instance_init(Object *obj)
482{
483 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
484
485 object_property_add(obj, "guest-stats", "guest statistics",
486 balloon_stats_get_all, NULL, NULL, s, NULL);
487
488 object_property_add(obj, "guest-stats-polling-interval", "int",
489 balloon_stats_get_poll_interval,
490 balloon_stats_set_poll_interval,
491 NULL, s, NULL);
492}
493
1ab461b5 494static Property virtio_balloon_properties[] = {
e3816255
DL
495 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
496 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
1ab461b5
FK
497 DEFINE_PROP_END_OF_LIST(),
498};
499
500static void virtio_balloon_class_init(ObjectClass *klass, void *data)
501{
502 DeviceClass *dc = DEVICE_CLASS(klass);
503 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
74def47c 504
1ab461b5 505 dc->props = virtio_balloon_properties;
125ee0ed 506 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
74def47c 507 vdc->realize = virtio_balloon_device_realize;
306ec6c3 508 vdc->unrealize = virtio_balloon_device_unrealize;
4eae2a65 509 vdc->reset = virtio_balloon_device_reset;
1ab461b5
FK
510 vdc->get_config = virtio_balloon_get_config;
511 vdc->set_config = virtio_balloon_set_config;
512 vdc->get_features = virtio_balloon_get_features;
9ea2511c
GK
513 vdc->save = virtio_balloon_save_device;
514 vdc->load = virtio_balloon_load_device;
1ab461b5
FK
515}
516
517static const TypeInfo virtio_balloon_info = {
518 .name = TYPE_VIRTIO_BALLOON,
519 .parent = TYPE_VIRTIO_DEVICE,
520 .instance_size = sizeof(VirtIOBalloon),
1190044e 521 .instance_init = virtio_balloon_instance_init,
1ab461b5
FK
522 .class_init = virtio_balloon_class_init,
523};
524
525static void virtio_register_types(void)
526{
527 type_register_static(&virtio_balloon_info);
528}
529
530type_init(virtio_register_types)