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