]> git.proxmox.com Git - qemu.git/blame - hw/virtio-balloon.c
balloon: Fix header comment; add Copyright
[qemu.git] / hw / virtio-balloon.c
CommitLineData
bd322087
AL
1/*
2 * Virtio Block Device
3 *
4 * Copyright IBM, Corp. 2008
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
fa6111f2 14#include "iov.h"
bd322087
AL
15#include "qemu-common.h"
16#include "virtio.h"
17#include "pc.h"
bd322087 18#include "cpu.h"
625a5bef 19#include "monitor.h"
bd322087
AL
20#include "balloon.h"
21#include "virtio-balloon.h"
22#include "kvm.h"
625a5bef
AL
23#include "qlist.h"
24#include "qint.h"
25#include "qstring.h"
bd322087
AL
26
27#if defined(__linux__)
28#include <sys/mman.h>
29#endif
30
07b0403d
EH
31/* Disable guest-provided stats by now (https://bugzilla.redhat.com/show_bug.cgi?id=623903) */
32#define ENABLE_GUEST_STATS 0
33
34
bd322087
AL
35typedef struct VirtIOBalloon
36{
37 VirtIODevice vdev;
625a5bef 38 VirtQueue *ivq, *dvq, *svq;
bd322087
AL
39 uint32_t num_pages;
40 uint32_t actual;
625a5bef
AL
41 uint64_t stats[VIRTIO_BALLOON_S_NR];
42 VirtQueueElement stats_vq_elem;
43 size_t stats_vq_offset;
44 MonitorCompletion *stats_callback;
45 void *stats_opaque_callback_data;
bd322087
AL
46} VirtIOBalloon;
47
48static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
49{
50 return (VirtIOBalloon *)vdev;
51}
52
53static void balloon_page(void *addr, int deflate)
54{
55#if defined(__linux__)
56 if (!kvm_enabled() || kvm_has_sync_mmu())
e78815a5
AF
57 qemu_madvise(addr, TARGET_PAGE_SIZE,
58 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
bd322087
AL
59#endif
60}
61
625a5bef
AL
62/*
63 * reset_stats - Mark all items in the stats array as unset
64 *
65 * This function needs to be called at device intialization and before
66 * before updating to a set of newly-generated stats. This will ensure that no
67 * stale values stick around in case the guest reports a subset of the supported
68 * statistics.
69 */
70static inline void reset_stats(VirtIOBalloon *dev)
71{
72 int i;
73 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
74}
75
76static void stat_put(QDict *dict, const char *label, uint64_t val)
77{
78 if (val != -1)
79 qdict_put(dict, label, qint_from_int(val));
80}
81
82static QObject *get_stats_qobject(VirtIOBalloon *dev)
83{
84 QDict *dict = qdict_new();
bd12ff9d
AL
85 uint64_t actual = ram_size - ((uint64_t) dev->actual <<
86 VIRTIO_BALLOON_PFN_SHIFT);
625a5bef
AL
87
88 stat_put(dict, "actual", actual);
07b0403d 89#if ENABLE_GUEST_STATS
625a5bef
AL
90 stat_put(dict, "mem_swapped_in", dev->stats[VIRTIO_BALLOON_S_SWAP_IN]);
91 stat_put(dict, "mem_swapped_out", dev->stats[VIRTIO_BALLOON_S_SWAP_OUT]);
92 stat_put(dict, "major_page_faults", dev->stats[VIRTIO_BALLOON_S_MAJFLT]);
93 stat_put(dict, "minor_page_faults", dev->stats[VIRTIO_BALLOON_S_MINFLT]);
94 stat_put(dict, "free_mem", dev->stats[VIRTIO_BALLOON_S_MEMFREE]);
95 stat_put(dict, "total_mem", dev->stats[VIRTIO_BALLOON_S_MEMTOT]);
07b0403d 96#endif
625a5bef
AL
97
98 return QOBJECT(dict);
99}
100
bd322087
AL
101static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
102{
103 VirtIOBalloon *s = to_virtio_balloon(vdev);
104 VirtQueueElement elem;
105
106 while (virtqueue_pop(vq, &elem)) {
107 size_t offset = 0;
108 uint32_t pfn;
109
fa6111f2 110 while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) {
c227f099
AL
111 ram_addr_t pa;
112 ram_addr_t addr;
bd322087 113
c227f099 114 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
bd322087
AL
115 offset += 4;
116
117 addr = cpu_get_physical_page_desc(pa);
118 if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
119 continue;
120
5c130f65
PB
121 /* Using qemu_get_ram_ptr is bending the rules a bit, but
122 should be OK because we only want a single page. */
123 balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq));
bd322087
AL
124 }
125
126 virtqueue_push(vq, &elem, offset);
127 virtio_notify(vdev, vq);
128 }
129}
130
625a5bef
AL
131static void complete_stats_request(VirtIOBalloon *vb)
132{
133 QObject *stats;
134
135 if (!vb->stats_opaque_callback_data)
136 return;
137
138 stats = get_stats_qobject(vb);
139 vb->stats_callback(vb->stats_opaque_callback_data, stats);
140 qobject_decref(stats);
141 vb->stats_opaque_callback_data = NULL;
142 vb->stats_callback = NULL;
143}
144
145static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
146{
147 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
148 VirtQueueElement *elem = &s->stats_vq_elem;
149 VirtIOBalloonStat stat;
150 size_t offset = 0;
151
152 if (!virtqueue_pop(vq, elem)) {
153 return;
154 }
155
156 /* Initialize the stats to get rid of any stale values. This is only
157 * needed to handle the case where a guest supports fewer stats than it
158 * used to (ie. it has booted into an old kernel).
159 */
160 reset_stats(s);
161
fa6111f2
AS
162 while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat))
163 == sizeof(stat)) {
625a5bef
AL
164 uint16_t tag = tswap16(stat.tag);
165 uint64_t val = tswap64(stat.val);
166
167 offset += sizeof(stat);
168 if (tag < VIRTIO_BALLOON_S_NR)
169 s->stats[tag] = val;
170 }
171 s->stats_vq_offset = offset;
172
173 complete_stats_request(s);
174}
175
bd322087
AL
176static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
177{
178 VirtIOBalloon *dev = to_virtio_balloon(vdev);
179 struct virtio_balloon_config config;
180
181 config.num_pages = cpu_to_le32(dev->num_pages);
182 config.actual = cpu_to_le32(dev->actual);
183
184 memcpy(config_data, &config, 8);
185}
186
187static void virtio_balloon_set_config(VirtIODevice *vdev,
188 const uint8_t *config_data)
189{
190 VirtIOBalloon *dev = to_virtio_balloon(vdev);
191 struct virtio_balloon_config config;
192 memcpy(&config, config_data, 8);
e54f1771 193 dev->actual = le32_to_cpu(config.actual);
bd322087
AL
194}
195
8172539d 196static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
bd322087 197{
625a5bef 198 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
8172539d 199 return f;
bd322087
AL
200}
201
dce911c7
AS
202static void virtio_balloon_stat(void *opaque, MonitorCompletion cb,
203 void *cb_data)
204{
205 VirtIOBalloon *dev = opaque;
206
207 /* For now, only allow one request at a time. This restriction can be
208 * removed later by queueing callback and data pairs.
209 */
210 if (dev->stats_callback != NULL) {
211 return;
212 }
213 dev->stats_callback = cb;
214 dev->stats_opaque_callback_data = cb_data;
215
216 if (ENABLE_GUEST_STATS
217 && (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ))) {
218 virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
219 virtio_notify(&dev->vdev, dev->svq);
220 return;
221 }
222
223 /* Stats are not supported. Clear out any stale values that might
224 * have been set by a more featureful guest kernel.
225 */
226 reset_stats(dev);
227 complete_stats_request(dev);
228}
229
30fb2ca6 230static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
bd322087
AL
231{
232 VirtIOBalloon *dev = opaque;
233
dce911c7 234 if (target > ram_size) {
bd322087 235 target = ram_size;
dce911c7 236 }
bd322087
AL
237 if (target) {
238 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
239 virtio_notify_config(&dev->vdev);
240 }
bd322087
AL
241}
242
243static void virtio_balloon_save(QEMUFile *f, void *opaque)
244{
245 VirtIOBalloon *s = opaque;
246
247 virtio_save(&s->vdev, f);
248
249 qemu_put_be32(f, s->num_pages);
250 qemu_put_be32(f, s->actual);
251}
252
253static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
254{
255 VirtIOBalloon *s = opaque;
256
257 if (version_id != 1)
258 return -EINVAL;
259
260 virtio_load(&s->vdev, f);
261
262 s->num_pages = qemu_get_be32(f);
263 s->actual = qemu_get_be32(f);
bd322087
AL
264 return 0;
265}
266
53c25cea 267VirtIODevice *virtio_balloon_init(DeviceState *dev)
bd322087
AL
268{
269 VirtIOBalloon *s;
270
53c25cea
PB
271 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
272 VIRTIO_ID_BALLOON,
273 8, sizeof(VirtIOBalloon));
bd322087
AL
274
275 s->vdev.get_config = virtio_balloon_get_config;
276 s->vdev.set_config = virtio_balloon_set_config;
277 s->vdev.get_features = virtio_balloon_get_features;
278
279 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
280 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
625a5bef 281 s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
bd322087 282
625a5bef 283 reset_stats(s);
30fb2ca6 284 qemu_add_balloon_handler(virtio_balloon_to_target, virtio_balloon_stat, s);
bd322087 285
0be71e32
AW
286 register_savevm(dev, "virtio-balloon", -1, 1,
287 virtio_balloon_save, virtio_balloon_load, s);
bd322087 288
53c25cea 289 return &s->vdev;
bd322087 290}