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