]>
Commit | Line | Data |
---|---|---|
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 |
34 | static 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 |
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 | ||
625a5bef AL |
53 | /* |
54 | * reset_stats - Mark all items in the stats array as unset | |
55 | * | |
56 | * This function needs to be called at device intialization and before | |
57 | * before 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 | ||
7e6ccd9c LC |
67 | static 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 | ||
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 | qemu_del_timer(s->stats_timer); | |
82 | qemu_free_timer(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 | qemu_mod_timer(s->stats_timer, qemu_get_clock_ms(vm_clock) + secs * 1000); | |
91 | } | |
92 | ||
93 | static 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 | ||
108 | static void balloon_stats_get_all(Object *obj, struct Visitor *v, | |
109 | void *opaque, const char *name, Error **errp) | |
110 | { | |
111 | VirtIOBalloon *s = opaque; | |
112 | int i; | |
113 | ||
114 | if (!s->stats_last_update) { | |
115 | error_setg(errp, "guest hasn't updated any stats yet"); | |
116 | return; | |
117 | } | |
118 | ||
119 | visit_start_struct(v, NULL, "guest-stats", name, 0, errp); | |
120 | visit_type_int(v, &s->stats_last_update, "last-update", errp); | |
121 | ||
122 | visit_start_struct(v, NULL, NULL, "stats", 0, errp); | |
123 | for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) { | |
124 | visit_type_int64(v, (int64_t *) &s->stats[i], balloon_stat_names[i], | |
125 | errp); | |
126 | } | |
127 | visit_end_struct(v, errp); | |
128 | ||
129 | visit_end_struct(v, errp); | |
130 | } | |
131 | ||
132 | static void balloon_stats_get_poll_interval(Object *obj, struct Visitor *v, | |
133 | void *opaque, const char *name, | |
134 | Error **errp) | |
135 | { | |
136 | VirtIOBalloon *s = opaque; | |
137 | visit_type_int(v, &s->stats_poll_interval, name, errp); | |
138 | } | |
139 | ||
140 | static void balloon_stats_set_poll_interval(Object *obj, struct Visitor *v, | |
141 | void *opaque, const char *name, | |
142 | Error **errp) | |
143 | { | |
144 | VirtIOBalloon *s = opaque; | |
145 | int64_t value; | |
146 | ||
147 | visit_type_int(v, &value, name, errp); | |
148 | if (error_is_set(errp)) { | |
149 | return; | |
150 | } | |
151 | ||
152 | if (value < 0) { | |
153 | error_setg(errp, "timer value must be greater than zero"); | |
154 | return; | |
155 | } | |
156 | ||
157 | if (value == s->stats_poll_interval) { | |
158 | return; | |
159 | } | |
160 | ||
161 | if (value == 0) { | |
162 | /* timer=0 disables the timer */ | |
163 | balloon_stats_destroy_timer(s); | |
164 | return; | |
165 | } | |
166 | ||
167 | if (balloon_stats_enabled(s)) { | |
168 | /* timer interval change */ | |
169 | s->stats_poll_interval = value; | |
170 | balloon_stats_change_timer(s, value); | |
171 | return; | |
172 | } | |
173 | ||
174 | /* create a new timer */ | |
175 | g_assert(s->stats_timer == NULL); | |
176 | s->stats_timer = qemu_new_timer_ms(vm_clock, balloon_stats_poll_cb, s); | |
177 | s->stats_poll_interval = value; | |
178 | balloon_stats_change_timer(s, 0); | |
179 | } | |
180 | ||
bd322087 AL |
181 | static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
182 | { | |
c96caced | 183 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
bd322087 | 184 | VirtQueueElement elem; |
b7c28c74 | 185 | MemoryRegionSection section; |
bd322087 AL |
186 | |
187 | while (virtqueue_pop(vq, &elem)) { | |
188 | size_t offset = 0; | |
189 | uint32_t pfn; | |
190 | ||
dcf6f5e1 | 191 | while (iov_to_buf(elem.out_sg, elem.out_num, offset, &pfn, 4) == 4) { |
c227f099 AL |
192 | ram_addr_t pa; |
193 | ram_addr_t addr; | |
bd322087 | 194 | |
c227f099 | 195 | pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT; |
bd322087 AL |
196 | offset += 4; |
197 | ||
b7c28c74 AK |
198 | /* FIXME: remove get_system_memory(), but how? */ |
199 | section = memory_region_find(get_system_memory(), pa, 1); | |
052e87b0 | 200 | if (!int128_nz(section.size) || !memory_region_is_ram(section.mr)) |
bd322087 AL |
201 | continue; |
202 | ||
b7c28c74 | 203 | /* Using memory_region_get_ram_ptr is bending the rules a bit, but |
5c130f65 | 204 | should be OK because we only want a single page. */ |
b7c28c74 AK |
205 | addr = section.offset_within_region; |
206 | balloon_page(memory_region_get_ram_ptr(section.mr) + addr, | |
207 | !!(vq == s->dvq)); | |
bd322087 AL |
208 | } |
209 | ||
210 | virtqueue_push(vq, &elem, offset); | |
211 | virtio_notify(vdev, vq); | |
212 | } | |
213 | } | |
214 | ||
625a5bef AL |
215 | static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) |
216 | { | |
c96caced | 217 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); |
625a5bef AL |
218 | VirtQueueElement *elem = &s->stats_vq_elem; |
219 | VirtIOBalloonStat stat; | |
220 | size_t offset = 0; | |
7e6ccd9c | 221 | qemu_timeval tv; |
625a5bef AL |
222 | |
223 | if (!virtqueue_pop(vq, elem)) { | |
7e6ccd9c | 224 | goto out; |
625a5bef AL |
225 | } |
226 | ||
227 | /* Initialize the stats to get rid of any stale values. This is only | |
228 | * needed to handle the case where a guest supports fewer stats than it | |
229 | * used to (ie. it has booted into an old kernel). | |
230 | */ | |
231 | reset_stats(s); | |
232 | ||
dcf6f5e1 | 233 | while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat)) |
fa6111f2 | 234 | == sizeof(stat)) { |
625a5bef AL |
235 | uint16_t tag = tswap16(stat.tag); |
236 | uint64_t val = tswap64(stat.val); | |
237 | ||
238 | offset += sizeof(stat); | |
239 | if (tag < VIRTIO_BALLOON_S_NR) | |
240 | s->stats[tag] = val; | |
241 | } | |
242 | s->stats_vq_offset = offset; | |
7e6ccd9c LC |
243 | |
244 | if (qemu_gettimeofday(&tv) < 0) { | |
245 | fprintf(stderr, "warning: %s: failed to get time of day\n", __func__); | |
246 | goto out; | |
247 | } | |
248 | ||
249 | s->stats_last_update = tv.tv_sec; | |
250 | ||
251 | out: | |
252 | if (balloon_stats_enabled(s)) { | |
253 | balloon_stats_change_timer(s, s->stats_poll_interval); | |
254 | } | |
625a5bef AL |
255 | } |
256 | ||
bd322087 AL |
257 | static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) |
258 | { | |
c96caced | 259 | VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); |
bd322087 AL |
260 | struct virtio_balloon_config config; |
261 | ||
262 | config.num_pages = cpu_to_le32(dev->num_pages); | |
263 | config.actual = cpu_to_le32(dev->actual); | |
264 | ||
265 | memcpy(config_data, &config, 8); | |
266 | } | |
267 | ||
268 | static void virtio_balloon_set_config(VirtIODevice *vdev, | |
269 | const uint8_t *config_data) | |
270 | { | |
c96caced | 271 | VirtIOBalloon *dev = VIRTIO_BALLOON(vdev); |
bd322087 | 272 | struct virtio_balloon_config config; |
973603a8 | 273 | uint32_t oldactual = dev->actual; |
bd322087 | 274 | memcpy(&config, config_data, 8); |
e54f1771 | 275 | dev->actual = le32_to_cpu(config.actual); |
973603a8 DB |
276 | if (dev->actual != oldactual) { |
277 | qemu_balloon_changed(ram_size - | |
dcc6ceff | 278 | ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT)); |
973603a8 | 279 | } |
bd322087 AL |
280 | } |
281 | ||
8172539d | 282 | static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f) |
bd322087 | 283 | { |
625a5bef | 284 | f |= (1 << VIRTIO_BALLOON_F_STATS_VQ); |
8172539d | 285 | return f; |
bd322087 AL |
286 | } |
287 | ||
96637bcd | 288 | static void virtio_balloon_stat(void *opaque, BalloonInfo *info) |
dce911c7 AS |
289 | { |
290 | VirtIOBalloon *dev = opaque; | |
96637bcd LC |
291 | info->actual = ram_size - ((uint64_t) dev->actual << |
292 | VIRTIO_BALLOON_PFN_SHIFT); | |
dce911c7 AS |
293 | } |
294 | ||
30fb2ca6 | 295 | static void virtio_balloon_to_target(void *opaque, ram_addr_t target) |
bd322087 | 296 | { |
c96caced FK |
297 | VirtIOBalloon *dev = VIRTIO_BALLOON(opaque); |
298 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
bd322087 | 299 | |
dce911c7 | 300 | if (target > ram_size) { |
bd322087 | 301 | target = ram_size; |
dce911c7 | 302 | } |
bd322087 AL |
303 | if (target) { |
304 | dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT; | |
c96caced | 305 | virtio_notify_config(vdev); |
bd322087 | 306 | } |
bd322087 AL |
307 | } |
308 | ||
309 | static void virtio_balloon_save(QEMUFile *f, void *opaque) | |
310 | { | |
c96caced FK |
311 | VirtIOBalloon *s = VIRTIO_BALLOON(opaque); |
312 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
bd322087 | 313 | |
c96caced | 314 | virtio_save(vdev, f); |
bd322087 AL |
315 | |
316 | qemu_put_be32(f, s->num_pages); | |
317 | qemu_put_be32(f, s->actual); | |
318 | } | |
319 | ||
320 | static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id) | |
321 | { | |
c96caced FK |
322 | VirtIOBalloon *s = VIRTIO_BALLOON(opaque); |
323 | VirtIODevice *vdev = VIRTIO_DEVICE(s); | |
2a633c46 | 324 | int ret; |
bd322087 AL |
325 | |
326 | if (version_id != 1) | |
327 | return -EINVAL; | |
328 | ||
c96caced | 329 | ret = virtio_load(vdev, f); |
2a633c46 OW |
330 | if (ret) { |
331 | return ret; | |
332 | } | |
bd322087 AL |
333 | |
334 | s->num_pages = qemu_get_be32(f); | |
335 | s->actual = qemu_get_be32(f); | |
bd322087 AL |
336 | return 0; |
337 | } | |
338 | ||
5c7d0962 | 339 | static int virtio_balloon_device_init(VirtIODevice *vdev) |
bd322087 | 340 | { |
5c7d0962 FK |
341 | DeviceState *qdev = DEVICE(vdev); |
342 | VirtIOBalloon *s = VIRTIO_BALLOON(vdev); | |
f76f6655 | 343 | int ret; |
bd322087 | 344 | |
5c7d0962 | 345 | virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON, 8); |
bd322087 | 346 | |
f76f6655 AS |
347 | ret = qemu_add_balloon_handler(virtio_balloon_to_target, |
348 | virtio_balloon_stat, s); | |
5c7d0962 | 349 | |
1ab461b5 | 350 | if (ret < 0) { |
6a1a8cc7 | 351 | virtio_cleanup(VIRTIO_DEVICE(s)); |
5c7d0962 | 352 | return -1; |
1ab461b5 | 353 | } |
f76f6655 | 354 | |
5c7d0962 FK |
355 | s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); |
356 | s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output); | |
357 | s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats); | |
bd322087 | 358 | |
5c7d0962 | 359 | register_savevm(qdev, "virtio-balloon", -1, 1, |
0be71e32 | 360 | virtio_balloon_save, virtio_balloon_load, s); |
bd322087 | 361 | |
5c7d0962 | 362 | object_property_add(OBJECT(qdev), "guest-stats", "guest statistics", |
7e6ccd9c LC |
363 | balloon_stats_get_all, NULL, NULL, s, NULL); |
364 | ||
5c7d0962 | 365 | object_property_add(OBJECT(qdev), "guest-stats-polling-interval", "int", |
7e6ccd9c LC |
366 | balloon_stats_get_poll_interval, |
367 | balloon_stats_set_poll_interval, | |
368 | NULL, s, NULL); | |
1ab461b5 FK |
369 | return 0; |
370 | } | |
371 | ||
372 | static int virtio_balloon_device_exit(DeviceState *qdev) | |
373 | { | |
374 | VirtIOBalloon *s = VIRTIO_BALLOON(qdev); | |
375 | VirtIODevice *vdev = VIRTIO_DEVICE(qdev); | |
376 | ||
377 | balloon_stats_destroy_timer(s); | |
378 | qemu_remove_balloon_handler(s); | |
379 | unregister_savevm(qdev, "virtio-balloon", s); | |
6a1a8cc7 | 380 | virtio_cleanup(vdev); |
1ab461b5 FK |
381 | return 0; |
382 | } | |
383 | ||
384 | static Property virtio_balloon_properties[] = { | |
385 | DEFINE_PROP_END_OF_LIST(), | |
386 | }; | |
387 | ||
388 | static void virtio_balloon_class_init(ObjectClass *klass, void *data) | |
389 | { | |
390 | DeviceClass *dc = DEVICE_CLASS(klass); | |
391 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
392 | dc->exit = virtio_balloon_device_exit; | |
393 | dc->props = virtio_balloon_properties; | |
394 | vdc->init = virtio_balloon_device_init; | |
395 | vdc->get_config = virtio_balloon_get_config; | |
396 | vdc->set_config = virtio_balloon_set_config; | |
397 | vdc->get_features = virtio_balloon_get_features; | |
398 | } | |
399 | ||
400 | static const TypeInfo virtio_balloon_info = { | |
401 | .name = TYPE_VIRTIO_BALLOON, | |
402 | .parent = TYPE_VIRTIO_DEVICE, | |
403 | .instance_size = sizeof(VirtIOBalloon), | |
404 | .class_init = virtio_balloon_class_init, | |
405 | }; | |
406 | ||
407 | static void virtio_register_types(void) | |
408 | { | |
409 | type_register_static(&virtio_balloon_info); | |
410 | } | |
411 | ||
412 | type_init(virtio_register_types) |