blob: f456cea2e718ad66f851c75fe411d3d4ec6202e6 [file] [log] [blame]
aliguoribd322082008-12-04 20:33:06 +00001/*
Amit Shahd4443cb2011-07-20 13:37:01 +05302 * Virtio Balloon Device
aliguoribd322082008-12-04 20:33:06 +00003 *
4 * Copyright IBM, Corp. 2008
Amit Shahd4443cb2011-07-20 13:37:01 +05305 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
aliguoribd322082008-12-04 20:33:06 +00007 *
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
Peter Maydell9b8bfe22016-01-26 18:17:07 +000016#include "qemu/osdep.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/iov.h"
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020018#include "qemu/timer.h"
aliguoribd322082008-12-04 20:33:06 +000019#include "qemu-common.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010020#include "hw/virtio/virtio.h"
Philippe Mathieu-Daudé2070aae2017-10-17 13:44:13 -030021#include "hw/mem/pc-dimm.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/balloon.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010023#include "hw/virtio/virtio-balloon.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/kvm.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010025#include "exec/address-spaces.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010026#include "qapi/error.h"
Markus Armbruster112ed242018-02-26 17:13:27 -060027#include "qapi/qapi-events-misc.h"
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020028#include "qapi/visitor.h"
zhanghailiang6adfdc52014-11-17 13:11:10 +080029#include "trace.h"
Alistair Francis2ab4b132017-09-11 12:52:50 -070030#include "qemu/error-report.h"
aliguoribd322082008-12-04 20:33:06 +000031
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/virtio/virtio-bus.h"
Rusty Russell8609d2a2014-06-24 19:43:22 +020033#include "hw/virtio/virtio-access.h"
KONRAD Frederic1ab461b2013-03-27 10:49:10 +010034
Thomas Huth01310e22016-04-14 10:50:07 +020035#define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)
36
aliguoribd322082008-12-04 20:33:06 +000037static void balloon_page(void *addr, int deflate)
38{
Dr. David Alan Gilbert371ff5a2015-11-05 18:11:23 +000039 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
40 kvm_has_sync_mmu())) {
Thomas Huth01310e22016-04-14 10:50:07 +020041 qemu_madvise(addr, BALLOON_PAGE_SIZE,
Andreas Färbere78815a2010-09-25 11:26:05 +000042 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
Dr. David Alan Gilbert371ff5a2015-11-05 18:11:23 +000043 }
aliguoribd322082008-12-04 20:33:06 +000044}
45
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020046static const char *balloon_stat_names[] = {
47 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
48 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
49 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
50 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
51 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
52 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
Denis V. Luneva0d06482016-02-24 10:50:48 +030053 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
Tomáš Golembiovskýbf1e7142017-12-05 13:14:46 +010054 [VIRTIO_BALLOON_S_CACHES] = "stat-disk-caches",
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020055 [VIRTIO_BALLOON_S_NR] = NULL
56};
57
Adam Litke625a5be2010-01-26 14:17:35 -060058/*
59 * reset_stats - Mark all items in the stats array as unset
60 *
Stefan Weil52f35022013-07-24 19:48:56 +020061 * This function needs to be called at device initialization and before
62 * updating to a set of newly-generated stats. This will ensure that no
Adam Litke625a5be2010-01-26 14:17:35 -060063 * stale values stick around in case the guest reports a subset of the supported
64 * statistics.
65 */
66static inline void reset_stats(VirtIOBalloon *dev)
67{
68 int i;
69 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
70}
71
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020072static bool balloon_stats_supported(const VirtIOBalloon *s)
73{
KONRAD Fredericc96cace2013-03-27 10:49:14 +010074 VirtIODevice *vdev = VIRTIO_DEVICE(s);
Cornelia Huck95129d62015-08-17 11:48:29 +020075 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020076}
77
78static bool balloon_stats_enabled(const VirtIOBalloon *s)
79{
80 return s->stats_poll_interval > 0;
81}
82
83static void balloon_stats_destroy_timer(VirtIOBalloon *s)
84{
85 if (balloon_stats_enabled(s)) {
Alex Blighbc72ad62013-08-21 16:03:08 +010086 timer_del(s->stats_timer);
87 timer_free(s->stats_timer);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020088 s->stats_timer = NULL;
89 s->stats_poll_interval = 0;
90 }
91}
92
Luiz Capitulino1f9296b2014-09-15 12:00:11 -040093static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020094{
Alex Blighbc72ad62013-08-21 16:03:08 +010095 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -020096}
97
98static void balloon_stats_poll_cb(void *opaque)
99{
100 VirtIOBalloon *s = opaque;
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100101 VirtIODevice *vdev = VIRTIO_DEVICE(s);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200102
Ladi Prosek4eae2a62016-03-01 12:14:03 +0100103 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200104 /* re-schedule */
105 balloon_stats_change_timer(s, s->stats_poll_interval);
106 return;
107 }
108
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200109 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100110 virtio_notify(vdev, s->svq);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200111 g_free(s->stats_vq_elem);
112 s->stats_vq_elem = NULL;
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200113}
114
Eric Blaked7bce992016-01-29 06:48:55 -0700115static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
116 void *opaque, Error **errp)
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200117{
Markus Armbruster2ddb16a2014-05-07 09:53:52 +0200118 Error *err = NULL;
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200119 VirtIOBalloon *s = opaque;
120 int i;
121
Eric Blake337283d2016-01-29 06:48:57 -0700122 visit_start_struct(v, name, NULL, 0, &err);
Markus Armbruster2ddb16a2014-05-07 09:53:52 +0200123 if (err) {
124 goto out;
125 }
Eric Blake51e72bc2016-01-29 06:48:54 -0700126 visit_type_int(v, "last-update", &s->stats_last_update, &err);
Markus Armbruster297a3642014-05-07 09:53:54 +0200127 if (err) {
128 goto out_end;
129 }
Markus Armbruster2ddb16a2014-05-07 09:53:52 +0200130
Eric Blake337283d2016-01-29 06:48:57 -0700131 visit_start_struct(v, "stats", NULL, 0, &err);
Markus Armbruster2ddb16a2014-05-07 09:53:52 +0200132 if (err) {
133 goto out_end;
134 }
Eric Blake9dbb8fa2016-01-29 06:48:45 -0700135 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
Eric Blake51e72bc2016-01-29 06:48:54 -0700136 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
Eric Blake9dbb8fa2016-01-29 06:48:45 -0700137 if (err) {
Eric Blake15c2f662016-04-28 15:45:27 -0600138 goto out_nested;
Eric Blake9dbb8fa2016-01-29 06:48:45 -0700139 }
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200140 }
Eric Blake15c2f662016-04-28 15:45:27 -0600141 visit_check_struct(v, &err);
142out_nested:
Eric Blake1158bb22016-06-09 10:48:34 -0600143 visit_end_struct(v, NULL);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200144
Eric Blake15c2f662016-04-28 15:45:27 -0600145 if (!err) {
146 visit_check_struct(v, &err);
147 }
Markus Armbruster2ddb16a2014-05-07 09:53:52 +0200148out_end:
Eric Blake1158bb22016-06-09 10:48:34 -0600149 visit_end_struct(v, NULL);
Markus Armbruster2ddb16a2014-05-07 09:53:52 +0200150out:
151 error_propagate(errp, err);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200152}
153
Eric Blake4fa45492016-01-29 06:48:53 -0700154static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
Eric Blaked7bce992016-01-29 06:48:55 -0700155 const char *name, void *opaque,
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200156 Error **errp)
157{
158 VirtIOBalloon *s = opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -0700159 visit_type_int(v, name, &s->stats_poll_interval, errp);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200160}
161
Eric Blake4fa45492016-01-29 06:48:53 -0700162static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
Eric Blaked7bce992016-01-29 06:48:55 -0700163 const char *name, void *opaque,
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200164 Error **errp)
165{
166 VirtIOBalloon *s = opaque;
Markus Armbruster65cd9062014-04-25 12:44:22 +0200167 Error *local_err = NULL;
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200168 int64_t value;
169
Eric Blake51e72bc2016-01-29 06:48:54 -0700170 visit_type_int(v, name, &value, &local_err);
Markus Armbruster65cd9062014-04-25 12:44:22 +0200171 if (local_err) {
172 error_propagate(errp, local_err);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200173 return;
174 }
175
176 if (value < 0) {
177 error_setg(errp, "timer value must be greater than zero");
178 return;
179 }
180
Markus Armbruster22644cd2014-10-01 18:43:44 +0200181 if (value > UINT32_MAX) {
Luiz Capitulino1f9296b2014-09-15 12:00:11 -0400182 error_setg(errp, "timer value is too big");
183 return;
184 }
185
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200186 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);
Alex Blighbc72ad62013-08-21 16:03:08 +0100205 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200206 s->stats_poll_interval = value;
207 balloon_stats_change_timer(s, 0);
208}
209
aliguoribd322082008-12-04 20:33:06 +0000210static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
211{
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100212 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200213 VirtQueueElement *elem;
Avi Kivityb7c28c72011-12-19 13:18:13 +0200214 MemoryRegionSection section;
aliguoribd322082008-12-04 20:33:06 +0000215
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200216 for (;;) {
aliguoribd322082008-12-04 20:33:06 +0000217 size_t offset = 0;
218 uint32_t pfn;
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200219 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
220 if (!elem) {
221 return;
222 }
aliguoribd322082008-12-04 20:33:06 +0000223
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200224 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
Anthony Liguoric227f092009-10-01 16:12:16 -0500225 ram_addr_t pa;
226 ram_addr_t addr;
Rusty Russell8609d2a2014-06-24 19:43:22 +0200227 int p = virtio_ldl_p(vdev, &pfn);
aliguoribd322082008-12-04 20:33:06 +0000228
Rusty Russell8609d2a2014-06-24 19:43:22 +0200229 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
aliguoribd322082008-12-04 20:33:06 +0000230 offset += 4;
231
Avi Kivityb7c28c72011-12-19 13:18:13 +0200232 /* FIXME: remove get_system_memory(), but how? */
233 section = memory_region_find(get_system_memory(), pa, 1);
Dr. David Alan Gilbertf2fd57d2016-12-16 11:41:55 +0000234 if (!int128_nz(section.size) ||
235 !memory_region_is_ram(section.mr) ||
236 memory_region_is_rom(section.mr) ||
237 memory_region_is_romd(section.mr)) {
238 trace_virtio_balloon_bad_addr(pa);
Tiwei Bieb86107a2018-01-25 15:12:43 +0800239 memory_region_unref(section.mr);
aliguoribd322082008-12-04 20:33:06 +0000240 continue;
Dr. David Alan Gilbertf2fd57d2016-12-16 11:41:55 +0000241 }
aliguoribd322082008-12-04 20:33:06 +0000242
zhanghailiang6adfdc52014-11-17 13:11:10 +0800243 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
244 pa);
Avi Kivityb7c28c72011-12-19 13:18:13 +0200245 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
pbrook5c130f62009-04-10 14:29:45 +0000246 should be OK because we only want a single page. */
Avi Kivityb7c28c72011-12-19 13:18:13 +0200247 addr = section.offset_within_region;
248 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
249 !!(vq == s->dvq));
Paolo Bonzinidfde4e62013-05-06 10:46:11 +0200250 memory_region_unref(section.mr);
aliguoribd322082008-12-04 20:33:06 +0000251 }
252
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200253 virtqueue_push(vq, elem, offset);
aliguoribd322082008-12-04 20:33:06 +0000254 virtio_notify(vdev, vq);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200255 g_free(elem);
aliguoribd322082008-12-04 20:33:06 +0000256 }
257}
258
Adam Litke625a5be2010-01-26 14:17:35 -0600259static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
260{
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100261 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200262 VirtQueueElement *elem;
Adam Litke625a5be2010-01-26 14:17:35 -0600263 VirtIOBalloonStat stat;
264 size_t offset = 0;
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200265 qemu_timeval tv;
Adam Litke625a5be2010-01-26 14:17:35 -0600266
Ladi Prosek4eae2a62016-03-01 12:14:03 +0100267 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
Paolo Bonzini51b19eb2016-02-04 16:26:51 +0200268 if (!elem) {
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200269 goto out;
Adam Litke625a5be2010-01-26 14:17:35 -0600270 }
271
Ladi Prosek4eae2a62016-03-01 12:14:03 +0100272 if (s->stats_vq_elem != NULL) {
273 /* This should never happen if the driver follows the spec. */
274 virtqueue_push(vq, s->stats_vq_elem, 0);
275 virtio_notify(vdev, vq);
276 g_free(s->stats_vq_elem);
277 }
278
279 s->stats_vq_elem = elem;
280
Adam Litke625a5be2010-01-26 14:17:35 -0600281 /* Initialize the stats to get rid of any stale values. This is only
282 * needed to handle the case where a guest supports fewer stats than it
283 * used to (ie. it has booted into an old kernel).
284 */
285 reset_stats(s);
286
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400287 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
Amit Shahfa6111f2010-04-27 18:04:06 +0530288 == sizeof(stat)) {
Rusty Russell8609d2a2014-06-24 19:43:22 +0200289 uint16_t tag = virtio_tswap16(vdev, stat.tag);
290 uint64_t val = virtio_tswap64(vdev, stat.val);
Adam Litke625a5be2010-01-26 14:17:35 -0600291
292 offset += sizeof(stat);
293 if (tag < VIRTIO_BALLOON_S_NR)
294 s->stats[tag] = val;
295 }
296 s->stats_vq_offset = offset;
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200297
298 if (qemu_gettimeofday(&tv) < 0) {
Alistair Francis2ab4b132017-09-11 12:52:50 -0700299 warn_report("%s: failed to get time of day", __func__);
Luiz Capitulino7e6ccd92012-12-01 00:14:57 -0200300 goto out;
301 }
302
303 s->stats_last_update = tv.tv_sec;
304
305out:
306 if (balloon_stats_enabled(s)) {
307 balloon_stats_change_timer(s, s->stats_poll_interval);
308 }
Adam Litke625a5be2010-01-26 14:17:35 -0600309}
310
aliguoribd322082008-12-04 20:33:06 +0000311static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
312{
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100313 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
aliguoribd322082008-12-04 20:33:06 +0000314 struct virtio_balloon_config config;
315
316 config.num_pages = cpu_to_le32(dev->num_pages);
317 config.actual = cpu_to_le32(dev->actual);
318
zhanghailiang6adfdc52014-11-17 13:11:10 +0800319 trace_virtio_balloon_get_config(config.num_pages, config.actual);
Luiz Capitulinoe6baf612014-01-09 09:58:16 -0500320 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
aliguoribd322082008-12-04 20:33:06 +0000321}
322
Vladimir Sementsov-Ogievskiy2b75f842016-02-10 11:49:26 +0300323static int build_dimm_list(Object *obj, void *opaque)
324{
325 GSList **list = opaque;
326
327 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
328 DeviceState *dev = DEVICE(obj);
329 if (dev->realized) { /* only realized DIMMs matter */
330 *list = g_slist_prepend(*list, dev);
331 }
332 }
333
334 object_child_foreach(obj, build_dimm_list, opaque);
335 return 0;
336}
337
Vladimir Sementsov-Ogievskiy39de9982016-02-10 11:49:22 +0300338static ram_addr_t get_current_ram_size(void)
339{
Vladimir Sementsov-Ogievskiye8dc06d2016-02-10 11:49:25 +0300340 GSList *list = NULL, *item;
Vladimir Sementsov-Ogievskiy39de9982016-02-10 11:49:22 +0300341 ram_addr_t size = ram_size;
342
Vladimir Sementsov-Ogievskiy2b75f842016-02-10 11:49:26 +0300343 build_dimm_list(qdev_get_machine(), &list);
Vladimir Sementsov-Ogievskiye8dc06d2016-02-10 11:49:25 +0300344 for (item = list; item; item = g_slist_next(item)) {
345 Object *obj = OBJECT(item->data);
Vladimir Sementsov-Ogievskiy2b75f842016-02-10 11:49:26 +0300346 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
347 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
348 &error_abort);
349 }
Vladimir Sementsov-Ogievskiy39de9982016-02-10 11:49:22 +0300350 }
Vladimir Sementsov-Ogievskiye8dc06d2016-02-10 11:49:25 +0300351 g_slist_free(list);
Vladimir Sementsov-Ogievskiy39de9982016-02-10 11:49:22 +0300352
353 return size;
354}
355
aliguoribd322082008-12-04 20:33:06 +0000356static void virtio_balloon_set_config(VirtIODevice *vdev,
357 const uint8_t *config_data)
358{
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100359 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
aliguoribd322082008-12-04 20:33:06 +0000360 struct virtio_balloon_config config;
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100361 uint32_t oldactual = dev->actual;
zhanghailiang463756d2014-11-17 13:11:09 +0800362 ram_addr_t vm_ram_size = get_current_ram_size();
363
Luiz Capitulinoe6baf612014-01-09 09:58:16 -0500364 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
Alexey Kardashevskiye54f1772011-04-07 13:02:04 +1000365 dev->actual = le32_to_cpu(config.actual);
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100366 if (dev->actual != oldactual) {
zhanghailiang463756d2014-11-17 13:11:09 +0800367 qapi_event_send_balloon_change(vm_ram_size -
Wenchao Xiaaef9d312014-06-18 08:43:51 +0200368 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
369 &error_abort);
Daniel P. Berrange973603a2012-06-14 18:12:56 +0100370 }
zhanghailiang6adfdc52014-11-17 13:11:10 +0800371 trace_virtio_balloon_set_config(dev->actual, oldactual);
aliguoribd322082008-12-04 20:33:06 +0000372}
373
Jason Wang9d5b7312015-07-27 17:49:19 +0800374static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
375 Error **errp)
aliguoribd322082008-12-04 20:33:06 +0000376{
Denis V. Luneve3816252015-06-15 13:52:52 +0300377 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
378 f |= dev->host_features;
Cornelia Huck40de55a2015-06-04 12:34:32 +0200379 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200380 return f;
aliguoribd322082008-12-04 20:33:06 +0000381}
382
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200383static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
Amit Shahdce911c2011-07-20 13:19:07 +0530384{
385 VirtIOBalloon *dev = opaque;
zhanghailiang463756d2014-11-17 13:11:09 +0800386 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
387 VIRTIO_BALLOON_PFN_SHIFT);
Amit Shahdce911c2011-07-20 13:19:07 +0530388}
389
Amit Shah30fb2ca2011-07-20 13:30:56 +0530390static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
aliguoribd322082008-12-04 20:33:06 +0000391{
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100392 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
393 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
zhanghailiang463756d2014-11-17 13:11:09 +0800394 ram_addr_t vm_ram_size = get_current_ram_size();
aliguoribd322082008-12-04 20:33:06 +0000395
zhanghailiang463756d2014-11-17 13:11:09 +0800396 if (target > vm_ram_size) {
397 target = vm_ram_size;
Amit Shahdce911c2011-07-20 13:19:07 +0530398 }
aliguoribd322082008-12-04 20:33:06 +0000399 if (target) {
zhanghailiang463756d2014-11-17 13:11:09 +0800400 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
KONRAD Fredericc96cace2013-03-27 10:49:14 +0100401 virtio_notify_config(vdev);
aliguoribd322082008-12-04 20:33:06 +0000402 }
zhanghailiang6adfdc52014-11-17 13:11:10 +0800403 trace_virtio_balloon_to_target(target, dev->num_pages);
aliguoribd322082008-12-04 20:33:06 +0000404}
405
Dr. David Alan Gilbert019518a2016-10-27 18:36:37 +0100406static int virtio_balloon_post_load_device(void *opaque, int version_id)
Greg Kurz9ea25112014-06-24 19:20:08 +0200407{
Dr. David Alan Gilbert019518a2016-10-27 18:36:37 +0100408 VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
Pavel Butsykinfecb48f2016-03-29 17:00:49 +0300409
410 if (balloon_stats_enabled(s)) {
411 balloon_stats_change_timer(s, s->stats_poll_interval);
412 }
aliguoribd322082008-12-04 20:33:06 +0000413 return 0;
414}
415
Dr. David Alan Gilbert019518a2016-10-27 18:36:37 +0100416static const VMStateDescription vmstate_virtio_balloon_device = {
417 .name = "virtio-balloon-device",
418 .version_id = 1,
419 .minimum_version_id = 1,
420 .post_load = virtio_balloon_post_load_device,
421 .fields = (VMStateField[]) {
422 VMSTATE_UINT32(num_pages, VirtIOBalloon),
423 VMSTATE_UINT32(actual, VirtIOBalloon),
424 VMSTATE_END_OF_LIST()
425 },
426};
427
Andreas Färber74def472013-07-30 02:51:37 +0200428static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
aliguoribd322082008-12-04 20:33:06 +0000429{
Andreas Färber74def472013-07-30 02:51:37 +0200430 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
Andreas Färbera546fb12013-07-30 05:33:58 +0200431 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
Amit Shahf76f6652011-07-27 12:29:33 +0530432 int ret;
aliguoribd322082008-12-04 20:33:06 +0000433
Luiz Capitulinoe6baf612014-01-09 09:58:16 -0500434 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
435 sizeof(struct virtio_balloon_config));
aliguoribd322082008-12-04 20:33:06 +0000436
Amit Shahf76f6652011-07-27 12:29:33 +0530437 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
438 virtio_balloon_stat, s);
KONRAD Frederic5c7d0962013-03-27 10:49:13 +0100439
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100440 if (ret < 0) {
Luiz Capitulino46abb812015-03-31 13:00:26 -0400441 error_setg(errp, "Only one balloon device is supported");
Andreas Färbera546fb12013-07-30 05:33:58 +0200442 virtio_cleanup(vdev);
Andreas Färber74def472013-07-30 02:51:37 +0200443 return;
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100444 }
Amit Shahf76f6652011-07-27 12:29:33 +0530445
KONRAD Frederic5c7d0962013-03-27 10:49:13 +0100446 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
447 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
448 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
aliguoribd322082008-12-04 20:33:06 +0000449
Ján Tomko38dbd482014-05-21 11:03:47 +0200450 reset_stats(s);
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100451}
452
Andreas Färber306ec6c2013-07-30 03:50:44 +0200453static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100454{
Andreas Färber306ec6c2013-07-30 03:50:44 +0200455 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
456 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100457
458 balloon_stats_destroy_timer(s);
459 qemu_remove_balloon_handler(s);
KONRAD Frederic6a1a8cc2013-04-24 10:21:22 +0200460 virtio_cleanup(vdev);
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100461}
462
Ladi Prosek4eae2a62016-03-01 12:14:03 +0100463static void virtio_balloon_device_reset(VirtIODevice *vdev)
464{
465 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
466
467 if (s->stats_vq_elem != NULL) {
Ladi Prosek27e57ef2016-11-03 09:55:49 +0100468 virtqueue_unpop(s->svq, s->stats_vq_elem, 0);
Ladi Prosek4eae2a62016-03-01 12:14:03 +0100469 g_free(s->stats_vq_elem);
470 s->stats_vq_elem = NULL;
471 }
472}
473
Ladi Prosek4a1e48b2016-09-07 17:20:49 +0200474static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
475{
476 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
477
478 if (!s->stats_vq_elem && vdev->vm_running &&
479 (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) {
480 /* poll stats queue for the element we have discarded when the VM
481 * was stopped */
482 virtio_balloon_receive_stats(vdev, s->svq);
483 }
484}
485
Shannon Zhao11900442015-05-11 17:34:05 +0800486static void virtio_balloon_instance_init(Object *obj)
487{
488 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
489
490 object_property_add(obj, "guest-stats", "guest statistics",
491 balloon_stats_get_all, NULL, NULL, s, NULL);
492
493 object_property_add(obj, "guest-stats-polling-interval", "int",
494 balloon_stats_get_poll_interval,
495 balloon_stats_set_poll_interval,
496 NULL, s, NULL);
497}
498
Halil Pasicc5dc16b2016-10-06 14:55:47 +0200499static const VMStateDescription vmstate_virtio_balloon = {
500 .name = "virtio-balloon",
501 .minimum_version_id = 1,
502 .version_id = 1,
503 .fields = (VMStateField[]) {
504 VMSTATE_VIRTIO_DEVICE,
505 VMSTATE_END_OF_LIST()
506 },
507};
Dr. David Alan Gilbert7f1ca9b2016-07-14 18:22:49 +0100508
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100509static Property virtio_balloon_properties[] = {
Denis V. Luneve3816252015-06-15 13:52:52 +0300510 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
511 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100512 DEFINE_PROP_END_OF_LIST(),
513};
514
515static void virtio_balloon_class_init(ObjectClass *klass, void *data)
516{
517 DeviceClass *dc = DEVICE_CLASS(klass);
518 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
Andreas Färber74def472013-07-30 02:51:37 +0200519
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100520 dc->props = virtio_balloon_properties;
Dr. David Alan Gilbert7f1ca9b2016-07-14 18:22:49 +0100521 dc->vmsd = &vmstate_virtio_balloon;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300522 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Andreas Färber74def472013-07-30 02:51:37 +0200523 vdc->realize = virtio_balloon_device_realize;
Andreas Färber306ec6c2013-07-30 03:50:44 +0200524 vdc->unrealize = virtio_balloon_device_unrealize;
Ladi Prosek4eae2a62016-03-01 12:14:03 +0100525 vdc->reset = virtio_balloon_device_reset;
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100526 vdc->get_config = virtio_balloon_get_config;
527 vdc->set_config = virtio_balloon_set_config;
528 vdc->get_features = virtio_balloon_get_features;
Ladi Prosek4a1e48b2016-09-07 17:20:49 +0200529 vdc->set_status = virtio_balloon_set_status;
Dr. David Alan Gilbert019518a2016-10-27 18:36:37 +0100530 vdc->vmsd = &vmstate_virtio_balloon_device;
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100531}
532
533static const TypeInfo virtio_balloon_info = {
534 .name = TYPE_VIRTIO_BALLOON,
535 .parent = TYPE_VIRTIO_DEVICE,
536 .instance_size = sizeof(VirtIOBalloon),
Shannon Zhao11900442015-05-11 17:34:05 +0800537 .instance_init = virtio_balloon_instance_init,
KONRAD Frederic1ab461b2013-03-27 10:49:10 +0100538 .class_init = virtio_balloon_class_init,
539};
540
541static void virtio_register_types(void)
542{
543 type_register_static(&virtio_balloon_info);
544}
545
546type_init(virtio_register_types)