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