xref: /openbmc/qemu/hw/virtio/virtio-balloon.c (revision fa59483d)
1 /*
2  * Virtio Balloon Device
3  *
4  * Copyright IBM, Corp. 2008
5  * Copyright (C) 2011 Red Hat, Inc.
6  * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
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 
16 #include "qemu/osdep.h"
17 #include "qemu/iov.h"
18 #include "qemu/timer.h"
19 #include "qemu-common.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/mem/pc-dimm.h"
22 #include "sysemu/balloon.h"
23 #include "hw/virtio/virtio-balloon.h"
24 #include "exec/address-spaces.h"
25 #include "qapi/error.h"
26 #include "qapi/qapi-events-misc.h"
27 #include "qapi/visitor.h"
28 #include "trace.h"
29 #include "qemu/error-report.h"
30 #include "migration/misc.h"
31 
32 #include "hw/virtio/virtio-bus.h"
33 #include "hw/virtio/virtio-access.h"
34 
35 #define BALLOON_PAGE_SIZE  (1 << VIRTIO_BALLOON_PFN_SHIFT)
36 
37 struct PartiallyBalloonedPage {
38     RAMBlock *rb;
39     ram_addr_t base;
40     unsigned long bitmap[];
41 };
42 
43 static void balloon_inflate_page(VirtIOBalloon *balloon,
44                                  MemoryRegion *mr, hwaddr offset)
45 {
46     void *addr = memory_region_get_ram_ptr(mr) + offset;
47     RAMBlock *rb;
48     size_t rb_page_size;
49     int subpages;
50     ram_addr_t ram_offset, host_page_base;
51 
52     /* XXX is there a better way to get to the RAMBlock than via a
53      * host address? */
54     rb = qemu_ram_block_from_host(addr, false, &ram_offset);
55     rb_page_size = qemu_ram_pagesize(rb);
56     host_page_base = ram_offset & ~(rb_page_size - 1);
57 
58     if (rb_page_size == BALLOON_PAGE_SIZE) {
59         /* Easy case */
60 
61         ram_block_discard_range(rb, ram_offset, rb_page_size);
62         /* We ignore errors from ram_block_discard_range(), because it
63          * has already reported them, and failing to discard a balloon
64          * page is not fatal */
65         return;
66     }
67 
68     /* Hard case
69      *
70      * We've put a piece of a larger host page into the balloon - we
71      * need to keep track until we have a whole host page to
72      * discard
73      */
74     warn_report_once(
75 "Balloon used with backing page size > 4kiB, this may not be reliable");
76 
77     subpages = rb_page_size / BALLOON_PAGE_SIZE;
78 
79     if (balloon->pbp
80         && (rb != balloon->pbp->rb
81             || host_page_base != balloon->pbp->base)) {
82         /* We've partially ballooned part of a host page, but now
83          * we're trying to balloon part of a different one.  Too hard,
84          * give up on the old partial page */
85         free(balloon->pbp);
86         balloon->pbp = NULL;
87     }
88 
89     if (!balloon->pbp) {
90         /* Starting on a new host page */
91         size_t bitlen = BITS_TO_LONGS(subpages) * sizeof(unsigned long);
92         balloon->pbp = g_malloc0(sizeof(PartiallyBalloonedPage) + bitlen);
93         balloon->pbp->rb = rb;
94         balloon->pbp->base = host_page_base;
95     }
96 
97     bitmap_set(balloon->pbp->bitmap,
98                (ram_offset - balloon->pbp->base) / BALLOON_PAGE_SIZE,
99                subpages);
100 
101     if (bitmap_full(balloon->pbp->bitmap, subpages)) {
102         /* We've accumulated a full host page, we can actually discard
103          * it now */
104 
105         ram_block_discard_range(rb, balloon->pbp->base, rb_page_size);
106         /* We ignore errors from ram_block_discard_range(), because it
107          * has already reported them, and failing to discard a balloon
108          * page is not fatal */
109 
110         free(balloon->pbp);
111         balloon->pbp = NULL;
112     }
113 }
114 
115 static const char *balloon_stat_names[] = {
116    [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
117    [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
118    [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
119    [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
120    [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
121    [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
122    [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
123    [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches",
124    [VIRTIO_BALLOON_S_HTLB_PGALLOC] = "stat-htlb-pgalloc",
125    [VIRTIO_BALLOON_S_HTLB_PGFAIL] = "stat-htlb-pgfail",
126    [VIRTIO_BALLOON_S_NR] = NULL
127 };
128 
129 /*
130  * reset_stats - Mark all items in the stats array as unset
131  *
132  * This function needs to be called at device initialization and before
133  * updating to a set of newly-generated stats.  This will ensure that no
134  * stale values stick around in case the guest reports a subset of the supported
135  * statistics.
136  */
137 static inline void reset_stats(VirtIOBalloon *dev)
138 {
139     int i;
140     for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
141 }
142 
143 static bool balloon_stats_supported(const VirtIOBalloon *s)
144 {
145     VirtIODevice *vdev = VIRTIO_DEVICE(s);
146     return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
147 }
148 
149 static bool balloon_stats_enabled(const VirtIOBalloon *s)
150 {
151     return s->stats_poll_interval > 0;
152 }
153 
154 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
155 {
156     if (balloon_stats_enabled(s)) {
157         timer_del(s->stats_timer);
158         timer_free(s->stats_timer);
159         s->stats_timer = NULL;
160         s->stats_poll_interval = 0;
161     }
162 }
163 
164 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
165 {
166     timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
167 }
168 
169 static void balloon_stats_poll_cb(void *opaque)
170 {
171     VirtIOBalloon *s = opaque;
172     VirtIODevice *vdev = VIRTIO_DEVICE(s);
173 
174     if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
175         /* re-schedule */
176         balloon_stats_change_timer(s, s->stats_poll_interval);
177         return;
178     }
179 
180     virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
181     virtio_notify(vdev, s->svq);
182     g_free(s->stats_vq_elem);
183     s->stats_vq_elem = NULL;
184 }
185 
186 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
187                                   void *opaque, Error **errp)
188 {
189     Error *err = NULL;
190     VirtIOBalloon *s = opaque;
191     int i;
192 
193     visit_start_struct(v, name, NULL, 0, &err);
194     if (err) {
195         goto out;
196     }
197     visit_type_int(v, "last-update", &s->stats_last_update, &err);
198     if (err) {
199         goto out_end;
200     }
201 
202     visit_start_struct(v, "stats", NULL, 0, &err);
203     if (err) {
204         goto out_end;
205     }
206     for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
207         visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
208         if (err) {
209             goto out_nested;
210         }
211     }
212     visit_check_struct(v, &err);
213 out_nested:
214     visit_end_struct(v, NULL);
215 
216     if (!err) {
217         visit_check_struct(v, &err);
218     }
219 out_end:
220     visit_end_struct(v, NULL);
221 out:
222     error_propagate(errp, err);
223 }
224 
225 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
226                                             const char *name, void *opaque,
227                                             Error **errp)
228 {
229     VirtIOBalloon *s = opaque;
230     visit_type_int(v, name, &s->stats_poll_interval, errp);
231 }
232 
233 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
234                                             const char *name, void *opaque,
235                                             Error **errp)
236 {
237     VirtIOBalloon *s = opaque;
238     Error *local_err = NULL;
239     int64_t value;
240 
241     visit_type_int(v, name, &value, &local_err);
242     if (local_err) {
243         error_propagate(errp, local_err);
244         return;
245     }
246 
247     if (value < 0) {
248         error_setg(errp, "timer value must be greater than zero");
249         return;
250     }
251 
252     if (value > UINT32_MAX) {
253         error_setg(errp, "timer value is too big");
254         return;
255     }
256 
257     if (value == s->stats_poll_interval) {
258         return;
259     }
260 
261     if (value == 0) {
262         /* timer=0 disables the timer */
263         balloon_stats_destroy_timer(s);
264         return;
265     }
266 
267     if (balloon_stats_enabled(s)) {
268         /* timer interval change */
269         s->stats_poll_interval = value;
270         balloon_stats_change_timer(s, value);
271         return;
272     }
273 
274     /* create a new timer */
275     g_assert(s->stats_timer == NULL);
276     s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
277     s->stats_poll_interval = value;
278     balloon_stats_change_timer(s, 0);
279 }
280 
281 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
282 {
283     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
284     VirtQueueElement *elem;
285     MemoryRegionSection section;
286 
287     for (;;) {
288         size_t offset = 0;
289         uint32_t pfn;
290         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
291         if (!elem) {
292             return;
293         }
294 
295         while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
296             hwaddr pa;
297             int p = virtio_ldl_p(vdev, &pfn);
298 
299             pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT;
300             offset += 4;
301 
302             section = memory_region_find(get_system_memory(), pa,
303                                          BALLOON_PAGE_SIZE);
304             if (!section.mr) {
305                 trace_virtio_balloon_bad_addr(pa);
306                 continue;
307             }
308             if (!memory_region_is_ram(section.mr) ||
309                 memory_region_is_rom(section.mr) ||
310                 memory_region_is_romd(section.mr)) {
311                 trace_virtio_balloon_bad_addr(pa);
312                 memory_region_unref(section.mr);
313                 continue;
314             }
315 
316             trace_virtio_balloon_handle_output(memory_region_name(section.mr),
317                                                pa);
318             if (!qemu_balloon_is_inhibited() && vq != s->dvq) {
319                 balloon_inflate_page(s, section.mr, section.offset_within_region);
320             }
321             memory_region_unref(section.mr);
322         }
323 
324         virtqueue_push(vq, elem, offset);
325         virtio_notify(vdev, vq);
326         g_free(elem);
327     }
328 }
329 
330 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
331 {
332     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
333     VirtQueueElement *elem;
334     VirtIOBalloonStat stat;
335     size_t offset = 0;
336     qemu_timeval tv;
337 
338     elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
339     if (!elem) {
340         goto out;
341     }
342 
343     if (s->stats_vq_elem != NULL) {
344         /* This should never happen if the driver follows the spec. */
345         virtqueue_push(vq, s->stats_vq_elem, 0);
346         virtio_notify(vdev, vq);
347         g_free(s->stats_vq_elem);
348     }
349 
350     s->stats_vq_elem = elem;
351 
352     /* Initialize the stats to get rid of any stale values.  This is only
353      * needed to handle the case where a guest supports fewer stats than it
354      * used to (ie. it has booted into an old kernel).
355      */
356     reset_stats(s);
357 
358     while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
359            == sizeof(stat)) {
360         uint16_t tag = virtio_tswap16(vdev, stat.tag);
361         uint64_t val = virtio_tswap64(vdev, stat.val);
362 
363         offset += sizeof(stat);
364         if (tag < VIRTIO_BALLOON_S_NR)
365             s->stats[tag] = val;
366     }
367     s->stats_vq_offset = offset;
368 
369     if (qemu_gettimeofday(&tv) < 0) {
370         warn_report("%s: failed to get time of day", __func__);
371         goto out;
372     }
373 
374     s->stats_last_update = tv.tv_sec;
375 
376 out:
377     if (balloon_stats_enabled(s)) {
378         balloon_stats_change_timer(s, s->stats_poll_interval);
379     }
380 }
381 
382 static void virtio_balloon_handle_free_page_vq(VirtIODevice *vdev,
383                                                VirtQueue *vq)
384 {
385     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
386     qemu_bh_schedule(s->free_page_bh);
387 }
388 
389 static bool get_free_page_hints(VirtIOBalloon *dev)
390 {
391     VirtQueueElement *elem;
392     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
393     VirtQueue *vq = dev->free_page_vq;
394 
395     while (dev->block_iothread) {
396         qemu_cond_wait(&dev->free_page_cond, &dev->free_page_lock);
397     }
398 
399     elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
400     if (!elem) {
401         return false;
402     }
403 
404     if (elem->out_num) {
405         uint32_t id;
406         size_t size = iov_to_buf(elem->out_sg, elem->out_num, 0,
407                                  &id, sizeof(id));
408         virtqueue_push(vq, elem, size);
409         g_free(elem);
410 
411         virtio_tswap32s(vdev, &id);
412         if (unlikely(size != sizeof(id))) {
413             virtio_error(vdev, "received an incorrect cmd id");
414             return false;
415         }
416         if (id == dev->free_page_report_cmd_id) {
417             dev->free_page_report_status = FREE_PAGE_REPORT_S_START;
418         } else {
419             /*
420              * Stop the optimization only when it has started. This
421              * avoids a stale stop sign for the previous command.
422              */
423             if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
424                 dev->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
425             }
426         }
427     }
428 
429     if (elem->in_num) {
430         if (dev->free_page_report_status == FREE_PAGE_REPORT_S_START) {
431             qemu_guest_free_page_hint(elem->in_sg[0].iov_base,
432                                       elem->in_sg[0].iov_len);
433         }
434         virtqueue_push(vq, elem, 1);
435         g_free(elem);
436     }
437 
438     return true;
439 }
440 
441 static void virtio_ballloon_get_free_page_hints(void *opaque)
442 {
443     VirtIOBalloon *dev = opaque;
444     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
445     VirtQueue *vq = dev->free_page_vq;
446     bool continue_to_get_hints;
447 
448     do {
449         qemu_mutex_lock(&dev->free_page_lock);
450         virtio_queue_set_notification(vq, 0);
451         continue_to_get_hints = get_free_page_hints(dev);
452         qemu_mutex_unlock(&dev->free_page_lock);
453         virtio_notify(vdev, vq);
454       /*
455        * Start to poll the vq once the reporting started. Otherwise, continue
456        * only when there are entries on the vq, which need to be given back.
457        */
458     } while (continue_to_get_hints ||
459              dev->free_page_report_status == FREE_PAGE_REPORT_S_START);
460     virtio_queue_set_notification(vq, 1);
461 }
462 
463 static bool virtio_balloon_free_page_support(void *opaque)
464 {
465     VirtIOBalloon *s = opaque;
466     VirtIODevice *vdev = VIRTIO_DEVICE(s);
467 
468     return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT);
469 }
470 
471 static void virtio_balloon_free_page_start(VirtIOBalloon *s)
472 {
473     VirtIODevice *vdev = VIRTIO_DEVICE(s);
474 
475     /* For the stop and copy phase, we don't need to start the optimization */
476     if (!vdev->vm_running) {
477         return;
478     }
479 
480     if (s->free_page_report_cmd_id == UINT_MAX) {
481         s->free_page_report_cmd_id =
482                        VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
483     } else {
484         s->free_page_report_cmd_id++;
485     }
486 
487     s->free_page_report_status = FREE_PAGE_REPORT_S_REQUESTED;
488     virtio_notify_config(vdev);
489 }
490 
491 static void virtio_balloon_free_page_stop(VirtIOBalloon *s)
492 {
493     VirtIODevice *vdev = VIRTIO_DEVICE(s);
494 
495     if (s->free_page_report_status != FREE_PAGE_REPORT_S_STOP) {
496         /*
497          * The lock also guarantees us that the
498          * virtio_ballloon_get_free_page_hints exits after the
499          * free_page_report_status is set to S_STOP.
500          */
501         qemu_mutex_lock(&s->free_page_lock);
502         /*
503          * The guest hasn't done the reporting, so host sends a notification
504          * to the guest to actively stop the reporting.
505          */
506         s->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
507         qemu_mutex_unlock(&s->free_page_lock);
508         virtio_notify_config(vdev);
509     }
510 }
511 
512 static void virtio_balloon_free_page_done(VirtIOBalloon *s)
513 {
514     VirtIODevice *vdev = VIRTIO_DEVICE(s);
515 
516     s->free_page_report_status = FREE_PAGE_REPORT_S_DONE;
517     virtio_notify_config(vdev);
518 }
519 
520 static int
521 virtio_balloon_free_page_report_notify(NotifierWithReturn *n, void *data)
522 {
523     VirtIOBalloon *dev = container_of(n, VirtIOBalloon,
524                                       free_page_report_notify);
525     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
526     PrecopyNotifyData *pnd = data;
527 
528     if (!virtio_balloon_free_page_support(dev)) {
529         /*
530          * This is an optimization provided to migration, so just return 0 to
531          * have the normal migration process not affected when this feature is
532          * not supported.
533          */
534         return 0;
535     }
536 
537     switch (pnd->reason) {
538     case PRECOPY_NOTIFY_SETUP:
539         precopy_enable_free_page_optimization();
540         break;
541     case PRECOPY_NOTIFY_COMPLETE:
542     case PRECOPY_NOTIFY_CLEANUP:
543     case PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC:
544         virtio_balloon_free_page_stop(dev);
545         break;
546     case PRECOPY_NOTIFY_AFTER_BITMAP_SYNC:
547         if (vdev->vm_running) {
548             virtio_balloon_free_page_start(dev);
549         } else {
550             virtio_balloon_free_page_done(dev);
551         }
552         break;
553     default:
554         virtio_error(vdev, "%s: %d reason unknown", __func__, pnd->reason);
555     }
556 
557     return 0;
558 }
559 
560 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
561 {
562     VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
563     struct virtio_balloon_config config = {};
564 
565     config.num_pages = cpu_to_le32(dev->num_pages);
566     config.actual = cpu_to_le32(dev->actual);
567 
568     if (dev->free_page_report_status == FREE_PAGE_REPORT_S_REQUESTED) {
569         config.free_page_report_cmd_id =
570                        cpu_to_le32(dev->free_page_report_cmd_id);
571     } else if (dev->free_page_report_status == FREE_PAGE_REPORT_S_STOP) {
572         config.free_page_report_cmd_id =
573                        cpu_to_le32(VIRTIO_BALLOON_CMD_ID_STOP);
574     } else if (dev->free_page_report_status == FREE_PAGE_REPORT_S_DONE) {
575         config.free_page_report_cmd_id =
576                        cpu_to_le32(VIRTIO_BALLOON_CMD_ID_DONE);
577     }
578 
579     trace_virtio_balloon_get_config(config.num_pages, config.actual);
580     memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
581 }
582 
583 static int build_dimm_list(Object *obj, void *opaque)
584 {
585     GSList **list = opaque;
586 
587     if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
588         DeviceState *dev = DEVICE(obj);
589         if (dev->realized) { /* only realized DIMMs matter */
590             *list = g_slist_prepend(*list, dev);
591         }
592     }
593 
594     object_child_foreach(obj, build_dimm_list, opaque);
595     return 0;
596 }
597 
598 static ram_addr_t get_current_ram_size(void)
599 {
600     GSList *list = NULL, *item;
601     ram_addr_t size = ram_size;
602 
603     build_dimm_list(qdev_get_machine(), &list);
604     for (item = list; item; item = g_slist_next(item)) {
605         Object *obj = OBJECT(item->data);
606         if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
607             size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
608                                             &error_abort);
609         }
610     }
611     g_slist_free(list);
612 
613     return size;
614 }
615 
616 static void virtio_balloon_set_config(VirtIODevice *vdev,
617                                       const uint8_t *config_data)
618 {
619     VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
620     struct virtio_balloon_config config;
621     uint32_t oldactual = dev->actual;
622     ram_addr_t vm_ram_size = get_current_ram_size();
623 
624     memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
625     dev->actual = le32_to_cpu(config.actual);
626     if (dev->actual != oldactual) {
627         qapi_event_send_balloon_change(vm_ram_size -
628                         ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
629     }
630     trace_virtio_balloon_set_config(dev->actual, oldactual);
631 }
632 
633 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
634                                             Error **errp)
635 {
636     VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
637     f |= dev->host_features;
638     virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
639 
640     return f;
641 }
642 
643 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
644 {
645     VirtIOBalloon *dev = opaque;
646     info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
647                                              VIRTIO_BALLOON_PFN_SHIFT);
648 }
649 
650 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
651 {
652     VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
653     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
654     ram_addr_t vm_ram_size = get_current_ram_size();
655 
656     if (target > vm_ram_size) {
657         target = vm_ram_size;
658     }
659     if (target) {
660         dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
661         virtio_notify_config(vdev);
662     }
663     trace_virtio_balloon_to_target(target, dev->num_pages);
664 }
665 
666 static int virtio_balloon_post_load_device(void *opaque, int version_id)
667 {
668     VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
669 
670     if (balloon_stats_enabled(s)) {
671         balloon_stats_change_timer(s, s->stats_poll_interval);
672     }
673     return 0;
674 }
675 
676 static const VMStateDescription vmstate_virtio_balloon_free_page_report = {
677     .name = "virtio-balloon-device/free-page-report",
678     .version_id = 1,
679     .minimum_version_id = 1,
680     .needed = virtio_balloon_free_page_support,
681     .fields = (VMStateField[]) {
682         VMSTATE_UINT32(free_page_report_cmd_id, VirtIOBalloon),
683         VMSTATE_UINT32(free_page_report_status, VirtIOBalloon),
684         VMSTATE_END_OF_LIST()
685     }
686 };
687 
688 static const VMStateDescription vmstate_virtio_balloon_device = {
689     .name = "virtio-balloon-device",
690     .version_id = 1,
691     .minimum_version_id = 1,
692     .post_load = virtio_balloon_post_load_device,
693     .fields = (VMStateField[]) {
694         VMSTATE_UINT32(num_pages, VirtIOBalloon),
695         VMSTATE_UINT32(actual, VirtIOBalloon),
696         VMSTATE_END_OF_LIST()
697     },
698     .subsections = (const VMStateDescription * []) {
699         &vmstate_virtio_balloon_free_page_report,
700         NULL
701     }
702 };
703 
704 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
705 {
706     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
707     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
708     int ret;
709 
710     virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
711                 sizeof(struct virtio_balloon_config));
712 
713     ret = qemu_add_balloon_handler(virtio_balloon_to_target,
714                                    virtio_balloon_stat, s);
715 
716     if (ret < 0) {
717         error_setg(errp, "Only one balloon device is supported");
718         virtio_cleanup(vdev);
719         return;
720     }
721 
722     s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
723     s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
724     s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
725 
726     if (virtio_has_feature(s->host_features,
727                            VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
728         s->free_page_vq = virtio_add_queue(vdev, VIRTQUEUE_MAX_SIZE,
729                                            virtio_balloon_handle_free_page_vq);
730         s->free_page_report_status = FREE_PAGE_REPORT_S_STOP;
731         s->free_page_report_cmd_id =
732                            VIRTIO_BALLOON_FREE_PAGE_REPORT_CMD_ID_MIN;
733         s->free_page_report_notify.notify =
734                                        virtio_balloon_free_page_report_notify;
735         precopy_add_notifier(&s->free_page_report_notify);
736         if (s->iothread) {
737             object_ref(OBJECT(s->iothread));
738             s->free_page_bh = aio_bh_new(iothread_get_aio_context(s->iothread),
739                                        virtio_ballloon_get_free_page_hints, s);
740             qemu_mutex_init(&s->free_page_lock);
741             qemu_cond_init(&s->free_page_cond);
742             s->block_iothread = false;
743         } else {
744             /* Simply disable this feature if the iothread wasn't created. */
745             s->host_features &= ~(1 << VIRTIO_BALLOON_F_FREE_PAGE_HINT);
746             virtio_error(vdev, "iothread is missing");
747         }
748     }
749     reset_stats(s);
750 }
751 
752 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
753 {
754     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
755     VirtIOBalloon *s = VIRTIO_BALLOON(dev);
756 
757     if (virtio_balloon_free_page_support(s)) {
758         qemu_bh_delete(s->free_page_bh);
759         virtio_balloon_free_page_stop(s);
760         precopy_remove_notifier(&s->free_page_report_notify);
761     }
762     balloon_stats_destroy_timer(s);
763     qemu_remove_balloon_handler(s);
764     virtio_cleanup(vdev);
765 }
766 
767 static void virtio_balloon_device_reset(VirtIODevice *vdev)
768 {
769     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
770 
771     if (virtio_balloon_free_page_support(s)) {
772         virtio_balloon_free_page_stop(s);
773     }
774 
775     if (s->stats_vq_elem != NULL) {
776         virtqueue_unpop(s->svq, s->stats_vq_elem, 0);
777         g_free(s->stats_vq_elem);
778         s->stats_vq_elem = NULL;
779     }
780 }
781 
782 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
783 {
784     VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
785 
786     if (!s->stats_vq_elem && vdev->vm_running &&
787         (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) {
788         /* poll stats queue for the element we have discarded when the VM
789          * was stopped */
790         virtio_balloon_receive_stats(vdev, s->svq);
791     }
792 
793     if (virtio_balloon_free_page_support(s)) {
794         /*
795          * The VM is woken up and the iothread was blocked, so signal it to
796          * continue.
797          */
798         if (vdev->vm_running && s->block_iothread) {
799             qemu_mutex_lock(&s->free_page_lock);
800             s->block_iothread = false;
801             qemu_cond_signal(&s->free_page_cond);
802             qemu_mutex_unlock(&s->free_page_lock);
803         }
804 
805         /* The VM is stopped, block the iothread. */
806         if (!vdev->vm_running) {
807             qemu_mutex_lock(&s->free_page_lock);
808             s->block_iothread = true;
809             qemu_mutex_unlock(&s->free_page_lock);
810         }
811     }
812 }
813 
814 static void virtio_balloon_instance_init(Object *obj)
815 {
816     VirtIOBalloon *s = VIRTIO_BALLOON(obj);
817 
818     object_property_add(obj, "guest-stats", "guest statistics",
819                         balloon_stats_get_all, NULL, NULL, s, NULL);
820 
821     object_property_add(obj, "guest-stats-polling-interval", "int",
822                         balloon_stats_get_poll_interval,
823                         balloon_stats_set_poll_interval,
824                         NULL, s, NULL);
825 }
826 
827 static const VMStateDescription vmstate_virtio_balloon = {
828     .name = "virtio-balloon",
829     .minimum_version_id = 1,
830     .version_id = 1,
831     .fields = (VMStateField[]) {
832         VMSTATE_VIRTIO_DEVICE,
833         VMSTATE_END_OF_LIST()
834     },
835 };
836 
837 static Property virtio_balloon_properties[] = {
838     DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
839                     VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
840     DEFINE_PROP_BIT("free-page-hint", VirtIOBalloon, host_features,
841                     VIRTIO_BALLOON_F_FREE_PAGE_HINT, false),
842     DEFINE_PROP_LINK("iothread", VirtIOBalloon, iothread, TYPE_IOTHREAD,
843                      IOThread *),
844     DEFINE_PROP_END_OF_LIST(),
845 };
846 
847 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
848 {
849     DeviceClass *dc = DEVICE_CLASS(klass);
850     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
851 
852     dc->props = virtio_balloon_properties;
853     dc->vmsd = &vmstate_virtio_balloon;
854     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
855     vdc->realize = virtio_balloon_device_realize;
856     vdc->unrealize = virtio_balloon_device_unrealize;
857     vdc->reset = virtio_balloon_device_reset;
858     vdc->get_config = virtio_balloon_get_config;
859     vdc->set_config = virtio_balloon_set_config;
860     vdc->get_features = virtio_balloon_get_features;
861     vdc->set_status = virtio_balloon_set_status;
862     vdc->vmsd = &vmstate_virtio_balloon_device;
863 }
864 
865 static const TypeInfo virtio_balloon_info = {
866     .name = TYPE_VIRTIO_BALLOON,
867     .parent = TYPE_VIRTIO_DEVICE,
868     .instance_size = sizeof(VirtIOBalloon),
869     .instance_init = virtio_balloon_instance_init,
870     .class_init = virtio_balloon_class_init,
871 };
872 
873 static void virtio_register_types(void)
874 {
875     type_register_static(&virtio_balloon_info);
876 }
877 
878 type_init(virtio_register_types)
879