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