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