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