Commit | Line | Data |
---|---|---|
1e214a5c SL |
1 | /* |
2 | * Virtio balloon implementation, inspired by Dor Laor and Marcelo | |
6b35e407 RR |
3 | * Tosatti's implementations. |
4 | * | |
5 | * Copyright 2008 Rusty Russell IBM Corporation | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
1e214a5c | 21 | |
6b35e407 RR |
22 | #include <linux/virtio.h> |
23 | #include <linux/virtio_balloon.h> | |
24 | #include <linux/swap.h> | |
fad7b7b2 | 25 | #include <linux/workqueue.h> |
6659a0f0 | 26 | #include <linux/delay.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
b5a2c4f1 | 28 | #include <linux/module.h> |
e2250429 | 29 | #include <linux/balloon_compaction.h> |
5a10b7db | 30 | #include <linux/oom.h> |
3d2a3774 | 31 | #include <linux/wait.h> |
5057dcd0 | 32 | #include <linux/mm.h> |
b1123ea6 | 33 | #include <linux/mount.h> |
6b35e407 | 34 | |
3ccc9372 MT |
35 | /* |
36 | * Balloon device works in 4K page units. So each page is pointed to by | |
37 | * multiple balloon pages. All memory counters in this driver are in balloon | |
38 | * page units. | |
39 | */ | |
e2250429 RA |
40 | #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT) |
41 | #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256 | |
5a10b7db RM |
42 | #define OOM_VBALLOON_DEFAULT_PAGES 256 |
43 | #define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80 | |
44 | ||
45 | static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES; | |
46 | module_param(oom_pages, int, S_IRUSR | S_IWUSR); | |
47 | MODULE_PARM_DESC(oom_pages, "pages to free on OOM"); | |
3ccc9372 | 48 | |
b1123ea6 MK |
49 | #ifdef CONFIG_BALLOON_COMPACTION |
50 | static struct vfsmount *balloon_mnt; | |
51 | #endif | |
52 | ||
25e65e4e | 53 | struct virtio_balloon { |
6b35e407 | 54 | struct virtio_device *vdev; |
9564e138 | 55 | struct virtqueue *inflate_vq, *deflate_vq, *stats_vq; |
6b35e407 | 56 | |
fad7b7b2 | 57 | /* The balloon servicing is delegated to a freezable workqueue. */ |
fd0e21c3 PM |
58 | struct work_struct update_balloon_stats_work; |
59 | struct work_struct update_balloon_size_work; | |
6b35e407 | 60 | |
fad7b7b2 PM |
61 | /* Prevent updating balloon when it is being canceled. */ |
62 | spinlock_t stop_update_lock; | |
63 | bool stop_update; | |
6b35e407 RR |
64 | |
65 | /* Waiting for host to ack the pages we released. */ | |
9c378abc | 66 | wait_queue_head_t acked; |
6b35e407 | 67 | |
3ccc9372 | 68 | /* Number of balloon pages we've told the Host we're not using. */ |
6b35e407 | 69 | unsigned int num_pages; |
3ccc9372 | 70 | /* |
e2250429 RA |
71 | * The pages we've told the Host we're not using are enqueued |
72 | * at vb_dev_info->pages list. | |
3ccc9372 MT |
73 | * Each page on this list adds VIRTIO_BALLOON_PAGES_PER_PAGE |
74 | * to num_pages above. | |
75 | */ | |
9d1ba805 | 76 | struct balloon_dev_info vb_dev_info; |
e2250429 RA |
77 | |
78 | /* Synchronize access/update to this struct virtio_balloon elements */ | |
79 | struct mutex balloon_lock; | |
6b35e407 RR |
80 | |
81 | /* The array of pfns we tell the Host about. */ | |
82 | unsigned int num_pfns; | |
87c9403b | 83 | __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX]; |
9564e138 AL |
84 | |
85 | /* Memory statistics */ | |
86 | struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR]; | |
5a10b7db RM |
87 | |
88 | /* To register callback in oom notifier call chain */ | |
89 | struct notifier_block nb; | |
6b35e407 RR |
90 | }; |
91 | ||
92 | static struct virtio_device_id id_table[] = { | |
93 | { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID }, | |
94 | { 0 }, | |
95 | }; | |
96 | ||
1b4aa2fa HB |
97 | static u32 page_to_balloon_pfn(struct page *page) |
98 | { | |
99 | unsigned long pfn = page_to_pfn(page); | |
100 | ||
101 | BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT); | |
102 | /* Convert pfn from Linux page size to balloon page size. */ | |
3ccc9372 MT |
103 | return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE; |
104 | } | |
105 | ||
106 | static struct page *balloon_pfn_to_page(u32 pfn) | |
107 | { | |
108 | BUG_ON(pfn % VIRTIO_BALLOON_PAGES_PER_PAGE); | |
109 | return pfn_to_page(pfn / VIRTIO_BALLOON_PAGES_PER_PAGE); | |
1b4aa2fa HB |
110 | } |
111 | ||
6b35e407 RR |
112 | static void balloon_ack(struct virtqueue *vq) |
113 | { | |
9c378abc | 114 | struct virtio_balloon *vb = vq->vdev->priv; |
6b35e407 | 115 | |
9c378abc | 116 | wake_up(&vb->acked); |
6b35e407 RR |
117 | } |
118 | ||
119 | static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) | |
120 | { | |
121 | struct scatterlist sg; | |
9c378abc | 122 | unsigned int len; |
6b35e407 RR |
123 | |
124 | sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns); | |
125 | ||
6b35e407 | 126 | /* We should always be able to add one buffer to an empty queue. */ |
4951cc90 | 127 | virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL); |
946cfe0e | 128 | virtqueue_kick(vq); |
6b35e407 RR |
129 | |
130 | /* When host has read buffer, this completes via balloon_ack */ | |
9c378abc | 131 | wait_event(vb->acked, virtqueue_get_buf(vq, &len)); |
fd0e21c3 | 132 | |
6b35e407 RR |
133 | } |
134 | ||
87c9403b MT |
135 | static void set_page_pfns(struct virtio_balloon *vb, |
136 | __virtio32 pfns[], struct page *page) | |
3ccc9372 MT |
137 | { |
138 | unsigned int i; | |
139 | ||
140 | /* Set balloon pfns pointing at this page. | |
141 | * Note that the first pfn points at start of the page. */ | |
142 | for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++) | |
87c9403b MT |
143 | pfns[i] = cpu_to_virtio32(vb->vdev, |
144 | page_to_balloon_pfn(page) + i); | |
3ccc9372 MT |
145 | } |
146 | ||
fad7b7b2 | 147 | static unsigned fill_balloon(struct virtio_balloon *vb, size_t num) |
6b35e407 | 148 | { |
9d1ba805 | 149 | struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info; |
fad7b7b2 | 150 | unsigned num_allocated_pages; |
e2250429 | 151 | |
6b35e407 RR |
152 | /* We can only do one array worth at a time. */ |
153 | num = min(num, ARRAY_SIZE(vb->pfns)); | |
154 | ||
e2250429 | 155 | mutex_lock(&vb->balloon_lock); |
3ccc9372 MT |
156 | for (vb->num_pfns = 0; vb->num_pfns < num; |
157 | vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) { | |
e2250429 RA |
158 | struct page *page = balloon_page_enqueue(vb_dev_info); |
159 | ||
6b35e407 | 160 | if (!page) { |
800ba5ea | 161 | dev_info_ratelimited(&vb->vdev->dev, |
b7dfde95 LT |
162 | "Out of puff! Can't get %u pages\n", |
163 | VIRTIO_BALLOON_PAGES_PER_PAGE); | |
6b35e407 RR |
164 | /* Sleep for at least 1/5 of a second before retry. */ |
165 | msleep(200); | |
166 | break; | |
167 | } | |
87c9403b | 168 | set_page_pfns(vb, vb->pfns + vb->num_pfns, page); |
3ccc9372 | 169 | vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE; |
997e1208 DL |
170 | if (!virtio_has_feature(vb->vdev, |
171 | VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) | |
172 | adjust_managed_page_count(page, -1); | |
6b35e407 RR |
173 | } |
174 | ||
fad7b7b2 | 175 | num_allocated_pages = vb->num_pfns; |
e2250429 RA |
176 | /* Did we get any? */ |
177 | if (vb->num_pfns != 0) | |
178 | tell_host(vb, vb->inflate_vq); | |
179 | mutex_unlock(&vb->balloon_lock); | |
fad7b7b2 PM |
180 | |
181 | return num_allocated_pages; | |
6b35e407 RR |
182 | } |
183 | ||
b4d34037 | 184 | static void release_pages_balloon(struct virtio_balloon *vb) |
6b35e407 RR |
185 | { |
186 | unsigned int i; | |
87c9403b | 187 | struct page *page; |
6b35e407 | 188 | |
3ccc9372 | 189 | /* Find pfns pointing at start of each page, get pages and free them. */ |
b4d34037 | 190 | for (i = 0; i < vb->num_pfns; i += VIRTIO_BALLOON_PAGES_PER_PAGE) { |
87c9403b MT |
191 | page = balloon_pfn_to_page(virtio32_to_cpu(vb->vdev, |
192 | vb->pfns[i])); | |
997e1208 DL |
193 | if (!virtio_has_feature(vb->vdev, |
194 | VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) | |
195 | adjust_managed_page_count(page, 1); | |
d6d86c0a | 196 | put_page(page); /* balloon reference */ |
6b35e407 RR |
197 | } |
198 | } | |
199 | ||
1fd9c672 | 200 | static unsigned leak_balloon(struct virtio_balloon *vb, size_t num) |
6b35e407 | 201 | { |
1fd9c672 | 202 | unsigned num_freed_pages; |
6b35e407 | 203 | struct page *page; |
9d1ba805 | 204 | struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info; |
6b35e407 RR |
205 | |
206 | /* We can only do one array worth at a time. */ | |
207 | num = min(num, ARRAY_SIZE(vb->pfns)); | |
208 | ||
e2250429 | 209 | mutex_lock(&vb->balloon_lock); |
3ccc9372 MT |
210 | for (vb->num_pfns = 0; vb->num_pfns < num; |
211 | vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) { | |
e2250429 RA |
212 | page = balloon_page_dequeue(vb_dev_info); |
213 | if (!page) | |
214 | break; | |
87c9403b | 215 | set_page_pfns(vb, vb->pfns + vb->num_pfns, page); |
3ccc9372 | 216 | vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE; |
6b35e407 RR |
217 | } |
218 | ||
1fd9c672 | 219 | num_freed_pages = vb->num_pfns; |
bf50e69f DH |
220 | /* |
221 | * Note that if | |
222 | * virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST); | |
223 | * is true, we *have* to do it in this order | |
224 | */ | |
8c6bab4f LC |
225 | if (vb->num_pfns != 0) |
226 | tell_host(vb, vb->deflate_vq); | |
b4d34037 | 227 | release_pages_balloon(vb); |
f68b992b | 228 | mutex_unlock(&vb->balloon_lock); |
1fd9c672 | 229 | return num_freed_pages; |
6b35e407 RR |
230 | } |
231 | ||
9564e138 AL |
232 | static inline void update_stat(struct virtio_balloon *vb, int idx, |
233 | u16 tag, u64 val) | |
234 | { | |
235 | BUG_ON(idx >= VIRTIO_BALLOON_S_NR); | |
df81b29c MT |
236 | vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag); |
237 | vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val); | |
9564e138 AL |
238 | } |
239 | ||
240 | #define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT) | |
241 | ||
242 | static void update_balloon_stats(struct virtio_balloon *vb) | |
243 | { | |
244 | unsigned long events[NR_VM_EVENT_ITEMS]; | |
245 | struct sysinfo i; | |
246 | int idx = 0; | |
5057dcd0 | 247 | long available; |
9564e138 AL |
248 | |
249 | all_vm_events(events); | |
250 | si_meminfo(&i); | |
251 | ||
5057dcd0 IR |
252 | available = si_mem_available(); |
253 | ||
9564e138 AL |
254 | update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN, |
255 | pages_to_bytes(events[PSWPIN])); | |
256 | update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT, | |
257 | pages_to_bytes(events[PSWPOUT])); | |
258 | update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]); | |
259 | update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]); | |
260 | update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE, | |
261 | pages_to_bytes(i.freeram)); | |
262 | update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT, | |
263 | pages_to_bytes(i.totalram)); | |
5057dcd0 IR |
264 | update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL, |
265 | pages_to_bytes(available)); | |
9564e138 AL |
266 | } |
267 | ||
268 | /* | |
269 | * While most virtqueues communicate guest-initiated requests to the hypervisor, | |
270 | * the stats queue operates in reverse. The driver initializes the virtqueue | |
271 | * with a single buffer. From that point forward, all conversations consist of | |
272 | * a hypervisor request (a call to this function) which directs us to refill | |
1f34c71a | 273 | * the virtqueue with a fresh stats buffer. Since stats collection can sleep, |
fad7b7b2 PM |
274 | * we delegate the job to a freezable workqueue that will do the actual work via |
275 | * stats_handle_request(). | |
9564e138 | 276 | */ |
1f34c71a | 277 | static void stats_request(struct virtqueue *vq) |
9564e138 | 278 | { |
9c378abc | 279 | struct virtio_balloon *vb = vq->vdev->priv; |
9564e138 | 280 | |
fad7b7b2 PM |
281 | spin_lock(&vb->stop_update_lock); |
282 | if (!vb->stop_update) | |
fd0e21c3 | 283 | queue_work(system_freezable_wq, &vb->update_balloon_stats_work); |
fad7b7b2 | 284 | spin_unlock(&vb->stop_update_lock); |
1f34c71a AL |
285 | } |
286 | ||
287 | static void stats_handle_request(struct virtio_balloon *vb) | |
288 | { | |
289 | struct virtqueue *vq; | |
290 | struct scatterlist sg; | |
9c378abc | 291 | unsigned int len; |
9564e138 AL |
292 | |
293 | update_balloon_stats(vb); | |
294 | ||
1f34c71a | 295 | vq = vb->stats_vq; |
9c378abc MT |
296 | if (!virtqueue_get_buf(vq, &len)) |
297 | return; | |
9564e138 | 298 | sg_init_one(&sg, vb->stats, sizeof(vb->stats)); |
4951cc90 | 299 | virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL); |
946cfe0e | 300 | virtqueue_kick(vq); |
9564e138 AL |
301 | } |
302 | ||
6b35e407 RR |
303 | static void virtballoon_changed(struct virtio_device *vdev) |
304 | { | |
305 | struct virtio_balloon *vb = vdev->priv; | |
fad7b7b2 | 306 | unsigned long flags; |
6b35e407 | 307 | |
fad7b7b2 PM |
308 | spin_lock_irqsave(&vb->stop_update_lock, flags); |
309 | if (!vb->stop_update) | |
fd0e21c3 | 310 | queue_work(system_freezable_wq, &vb->update_balloon_size_work); |
fad7b7b2 | 311 | spin_unlock_irqrestore(&vb->stop_update_lock, flags); |
6b35e407 RR |
312 | } |
313 | ||
bdc1681c | 314 | static inline s64 towards_target(struct virtio_balloon *vb) |
6b35e407 | 315 | { |
1a87228f | 316 | s64 target; |
df81b29c | 317 | u32 num_pages; |
1a87228f | 318 | |
df81b29c MT |
319 | virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages, |
320 | &num_pages); | |
855e0c52 | 321 | |
df81b29c MT |
322 | /* Legacy balloon config space is LE, unlike all other devices. */ |
323 | if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1)) | |
324 | num_pages = le32_to_cpu((__force __le32)num_pages); | |
325 | ||
326 | target = num_pages; | |
1a87228f | 327 | return target - vb->num_pages; |
6b35e407 RR |
328 | } |
329 | ||
330 | static void update_balloon_size(struct virtio_balloon *vb) | |
331 | { | |
df81b29c MT |
332 | u32 actual = vb->num_pages; |
333 | ||
334 | /* Legacy balloon config space is LE, unlike all other devices. */ | |
335 | if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1)) | |
336 | actual = (__force u32)cpu_to_le32(actual); | |
6b35e407 | 337 | |
3459f11a | 338 | virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual, |
855e0c52 | 339 | &actual); |
6b35e407 RR |
340 | } |
341 | ||
5a10b7db RM |
342 | /* |
343 | * virtballoon_oom_notify - release pages when system is under severe | |
344 | * memory pressure (called from out_of_memory()) | |
345 | * @self : notifier block struct | |
346 | * @dummy: not used | |
347 | * @parm : returned - number of freed pages | |
348 | * | |
349 | * The balancing of memory by use of the virtio balloon should not cause | |
350 | * the termination of processes while there are pages in the balloon. | |
351 | * If virtio balloon manages to release some memory, it will make the | |
352 | * system return and retry the allocation that forced the OOM killer | |
353 | * to run. | |
354 | */ | |
355 | static int virtballoon_oom_notify(struct notifier_block *self, | |
356 | unsigned long dummy, void *parm) | |
357 | { | |
358 | struct virtio_balloon *vb; | |
359 | unsigned long *freed; | |
360 | unsigned num_freed_pages; | |
361 | ||
362 | vb = container_of(self, struct virtio_balloon, nb); | |
363 | if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) | |
364 | return NOTIFY_OK; | |
365 | ||
366 | freed = parm; | |
367 | num_freed_pages = leak_balloon(vb, oom_pages); | |
368 | update_balloon_size(vb); | |
369 | *freed += num_freed_pages; | |
370 | ||
371 | return NOTIFY_OK; | |
372 | } | |
373 | ||
fd0e21c3 | 374 | static void update_balloon_stats_func(struct work_struct *work) |
6b35e407 | 375 | { |
fd0e21c3 | 376 | struct virtio_balloon *vb; |
3d2a3774 | 377 | |
fd0e21c3 PM |
378 | vb = container_of(work, struct virtio_balloon, |
379 | update_balloon_stats_work); | |
380 | stats_handle_request(vb); | |
381 | } | |
1f74ef0f | 382 | |
fd0e21c3 | 383 | static void update_balloon_size_func(struct work_struct *work) |
6b35e407 | 384 | { |
fad7b7b2 PM |
385 | struct virtio_balloon *vb; |
386 | s64 diff; | |
3d2a3774 | 387 | |
fd0e21c3 PM |
388 | vb = container_of(work, struct virtio_balloon, |
389 | update_balloon_size_work); | |
fad7b7b2 | 390 | diff = towards_target(vb); |
1f74ef0f | 391 | |
fad7b7b2 PM |
392 | if (diff > 0) |
393 | diff -= fill_balloon(vb, diff); | |
394 | else if (diff < 0) | |
395 | diff += leak_balloon(vb, -diff); | |
396 | update_balloon_size(vb); | |
397 | ||
398 | if (diff) | |
399 | queue_work(system_freezable_wq, work); | |
6b35e407 RR |
400 | } |
401 | ||
be91c33d | 402 | static int init_vqs(struct virtio_balloon *vb) |
6b35e407 | 403 | { |
9564e138 | 404 | struct virtqueue *vqs[3]; |
1f34c71a | 405 | vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request }; |
f7ad26ff | 406 | static const char * const names[] = { "inflate", "deflate", "stats" }; |
9564e138 | 407 | int err, nvqs; |
6b35e407 | 408 | |
be91c33d AS |
409 | /* |
410 | * We expect two virtqueues: inflate and deflate, and | |
411 | * optionally stat. | |
412 | */ | |
9564e138 | 413 | nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2; |
be91c33d | 414 | err = vb->vdev->config->find_vqs(vb->vdev, nvqs, vqs, callbacks, names); |
d2a7ddda | 415 | if (err) |
be91c33d | 416 | return err; |
6b35e407 | 417 | |
d2a7ddda MT |
418 | vb->inflate_vq = vqs[0]; |
419 | vb->deflate_vq = vqs[1]; | |
9564e138 AL |
420 | if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { |
421 | struct scatterlist sg; | |
422 | vb->stats_vq = vqs[2]; | |
423 | ||
424 | /* | |
425 | * Prime this virtqueue with one buffer so the hypervisor can | |
4951cc90 | 426 | * use it to signal us later (it can't be broken yet!). |
9564e138 AL |
427 | */ |
428 | sg_init_one(&sg, vb->stats, sizeof vb->stats); | |
92549abc | 429 | if (virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb, GFP_KERNEL) |
f96fde41 | 430 | < 0) |
9564e138 | 431 | BUG(); |
946cfe0e | 432 | virtqueue_kick(vb->stats_vq); |
9564e138 | 433 | } |
be91c33d AS |
434 | return 0; |
435 | } | |
436 | ||
e2250429 RA |
437 | #ifdef CONFIG_BALLOON_COMPACTION |
438 | /* | |
439 | * virtballoon_migratepage - perform the balloon page migration on behalf of | |
440 | * a compation thread. (called under page lock) | |
9d1ba805 | 441 | * @vb_dev_info: the balloon device |
e2250429 RA |
442 | * @newpage: page that will replace the isolated page after migration finishes. |
443 | * @page : the isolated (old) page that is about to be migrated to newpage. | |
444 | * @mode : compaction mode -- not used for balloon page migration. | |
445 | * | |
446 | * After a ballooned page gets isolated by compaction procedures, this is the | |
447 | * function that performs the page migration on behalf of a compaction thread | |
448 | * The page migration for virtio balloon is done in a simple swap fashion which | |
449 | * follows these two macro steps: | |
450 | * 1) insert newpage into vb->pages list and update the host about it; | |
451 | * 2) update the host about the old page removed from vb->pages list; | |
452 | * | |
453 | * This function preforms the balloon page migration task. | |
454 | * Called through balloon_mapping->a_ops->migratepage | |
455 | */ | |
9d1ba805 | 456 | static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info, |
e2250429 RA |
457 | struct page *newpage, struct page *page, enum migrate_mode mode) |
458 | { | |
9d1ba805 KK |
459 | struct virtio_balloon *vb = container_of(vb_dev_info, |
460 | struct virtio_balloon, vb_dev_info); | |
e2250429 RA |
461 | unsigned long flags; |
462 | ||
e2250429 RA |
463 | /* |
464 | * In order to avoid lock contention while migrating pages concurrently | |
465 | * to leak_balloon() or fill_balloon() we just give up the balloon_lock | |
466 | * this turn, as it is easier to retry the page migration later. | |
467 | * This also prevents fill_balloon() getting stuck into a mutex | |
468 | * recursion in the case it ends up triggering memory compaction | |
469 | * while it is attempting to inflate the ballon. | |
470 | */ | |
471 | if (!mutex_trylock(&vb->balloon_lock)) | |
472 | return -EAGAIN; | |
473 | ||
d6d86c0a KK |
474 | get_page(newpage); /* balloon reference */ |
475 | ||
e2250429 RA |
476 | /* balloon's page migration 1st step -- inflate "newpage" */ |
477 | spin_lock_irqsave(&vb_dev_info->pages_lock, flags); | |
9d1ba805 | 478 | balloon_page_insert(vb_dev_info, newpage); |
e2250429 | 479 | vb_dev_info->isolated_pages--; |
09316c09 | 480 | __count_vm_event(BALLOON_MIGRATE); |
e2250429 RA |
481 | spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags); |
482 | vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE; | |
87c9403b | 483 | set_page_pfns(vb, vb->pfns, newpage); |
e2250429 RA |
484 | tell_host(vb, vb->inflate_vq); |
485 | ||
d6d86c0a | 486 | /* balloon's page migration 2nd step -- deflate "page" */ |
e2250429 RA |
487 | balloon_page_delete(page); |
488 | vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE; | |
87c9403b | 489 | set_page_pfns(vb, vb->pfns, page); |
e2250429 RA |
490 | tell_host(vb, vb->deflate_vq); |
491 | ||
492 | mutex_unlock(&vb->balloon_lock); | |
493 | ||
d6d86c0a KK |
494 | put_page(page); /* balloon reference */ |
495 | ||
dd4123f3 | 496 | return MIGRATEPAGE_SUCCESS; |
e2250429 | 497 | } |
b1123ea6 MK |
498 | |
499 | static struct dentry *balloon_mount(struct file_system_type *fs_type, | |
500 | int flags, const char *dev_name, void *data) | |
501 | { | |
502 | static const struct dentry_operations ops = { | |
503 | .d_dname = simple_dname, | |
504 | }; | |
505 | ||
506 | return mount_pseudo(fs_type, "balloon-kvm:", NULL, &ops, | |
507 | BALLOON_KVM_MAGIC); | |
508 | } | |
509 | ||
510 | static struct file_system_type balloon_fs = { | |
511 | .name = "balloon-kvm", | |
512 | .mount = balloon_mount, | |
513 | .kill_sb = kill_anon_super, | |
514 | }; | |
515 | ||
e2250429 RA |
516 | #endif /* CONFIG_BALLOON_COMPACTION */ |
517 | ||
be91c33d AS |
518 | static int virtballoon_probe(struct virtio_device *vdev) |
519 | { | |
520 | struct virtio_balloon *vb; | |
521 | int err; | |
522 | ||
2d9becc1 MT |
523 | if (!vdev->config->get) { |
524 | dev_err(&vdev->dev, "%s failure: config access disabled\n", | |
525 | __func__); | |
526 | return -EINVAL; | |
527 | } | |
528 | ||
be91c33d AS |
529 | vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL); |
530 | if (!vb) { | |
531 | err = -ENOMEM; | |
532 | goto out; | |
533 | } | |
534 | ||
fd0e21c3 PM |
535 | INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func); |
536 | INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func); | |
fad7b7b2 PM |
537 | spin_lock_init(&vb->stop_update_lock); |
538 | vb->stop_update = false; | |
be91c33d | 539 | vb->num_pages = 0; |
e2250429 | 540 | mutex_init(&vb->balloon_lock); |
9c378abc | 541 | init_waitqueue_head(&vb->acked); |
be91c33d | 542 | vb->vdev = vdev; |
be91c33d | 543 | |
9d1ba805 | 544 | balloon_devinfo_init(&vb->vb_dev_info); |
e2250429 | 545 | |
be91c33d AS |
546 | err = init_vqs(vb); |
547 | if (err) | |
9d1ba805 | 548 | goto out_free_vb; |
6b35e407 | 549 | |
5a10b7db RM |
550 | vb->nb.notifier_call = virtballoon_oom_notify; |
551 | vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY; | |
552 | err = register_oom_notifier(&vb->nb); | |
553 | if (err < 0) | |
b1123ea6 MK |
554 | goto out_del_vqs; |
555 | ||
556 | #ifdef CONFIG_BALLOON_COMPACTION | |
557 | balloon_mnt = kern_mount(&balloon_fs); | |
558 | if (IS_ERR(balloon_mnt)) { | |
559 | err = PTR_ERR(balloon_mnt); | |
560 | unregister_oom_notifier(&vb->nb); | |
561 | goto out_del_vqs; | |
562 | } | |
563 | ||
564 | vb->vb_dev_info.migratepage = virtballoon_migratepage; | |
565 | vb->vb_dev_info.inode = alloc_anon_inode(balloon_mnt->mnt_sb); | |
566 | if (IS_ERR(vb->vb_dev_info.inode)) { | |
567 | err = PTR_ERR(vb->vb_dev_info.inode); | |
568 | kern_unmount(balloon_mnt); | |
569 | unregister_oom_notifier(&vb->nb); | |
570 | vb->vb_dev_info.inode = NULL; | |
571 | goto out_del_vqs; | |
572 | } | |
573 | vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops; | |
574 | #endif | |
5a10b7db | 575 | |
88660f7f MT |
576 | virtio_device_ready(vdev); |
577 | ||
6b35e407 RR |
578 | return 0; |
579 | ||
b1123ea6 | 580 | out_del_vqs: |
d2a7ddda | 581 | vdev->config->del_vqs(vdev); |
6b35e407 RR |
582 | out_free_vb: |
583 | kfree(vb); | |
584 | out: | |
585 | return err; | |
586 | } | |
587 | ||
c877bab5 | 588 | static void remove_common(struct virtio_balloon *vb) |
6b35e407 | 589 | { |
6b35e407 RR |
590 | /* There might be pages left in the balloon: free them. */ |
591 | while (vb->num_pages) | |
592 | leak_balloon(vb, vb->num_pages); | |
b8ae0eb3 | 593 | update_balloon_size(vb); |
6b35e407 RR |
594 | |
595 | /* Now we reset the device so we can clean up the queues. */ | |
c877bab5 | 596 | vb->vdev->config->reset(vb->vdev); |
6b35e407 | 597 | |
c877bab5 AS |
598 | vb->vdev->config->del_vqs(vb->vdev); |
599 | } | |
600 | ||
8590dbc7 | 601 | static void virtballoon_remove(struct virtio_device *vdev) |
c877bab5 AS |
602 | { |
603 | struct virtio_balloon *vb = vdev->priv; | |
604 | ||
5a10b7db | 605 | unregister_oom_notifier(&vb->nb); |
fad7b7b2 PM |
606 | |
607 | spin_lock_irq(&vb->stop_update_lock); | |
608 | vb->stop_update = true; | |
609 | spin_unlock_irq(&vb->stop_update_lock); | |
fd0e21c3 PM |
610 | cancel_work_sync(&vb->update_balloon_size_work); |
611 | cancel_work_sync(&vb->update_balloon_stats_work); | |
fad7b7b2 | 612 | |
c877bab5 | 613 | remove_common(vb); |
b1123ea6 MK |
614 | if (vb->vb_dev_info.inode) |
615 | iput(vb->vb_dev_info.inode); | |
6b35e407 RR |
616 | kfree(vb); |
617 | } | |
618 | ||
89107000 | 619 | #ifdef CONFIG_PM_SLEEP |
e562966d AS |
620 | static int virtballoon_freeze(struct virtio_device *vdev) |
621 | { | |
4eb05d56 AS |
622 | struct virtio_balloon *vb = vdev->priv; |
623 | ||
e562966d | 624 | /* |
fad7b7b2 | 625 | * The workqueue is already frozen by the PM core before this |
e562966d AS |
626 | * function is called. |
627 | */ | |
c877bab5 | 628 | remove_common(vb); |
e562966d AS |
629 | return 0; |
630 | } | |
631 | ||
c45b4166 | 632 | static int virtballoon_restore(struct virtio_device *vdev) |
4eb05d56 AS |
633 | { |
634 | struct virtio_balloon *vb = vdev->priv; | |
635 | int ret; | |
636 | ||
637 | ret = init_vqs(vdev->priv); | |
638 | if (ret) | |
639 | return ret; | |
640 | ||
486d2e63 MT |
641 | virtio_device_ready(vdev); |
642 | ||
fad7b7b2 PM |
643 | if (towards_target(vb)) |
644 | virtballoon_changed(vdev); | |
4eb05d56 AS |
645 | update_balloon_size(vb); |
646 | return 0; | |
647 | } | |
e562966d AS |
648 | #endif |
649 | ||
9564e138 AL |
650 | static unsigned int features[] = { |
651 | VIRTIO_BALLOON_F_MUST_TELL_HOST, | |
652 | VIRTIO_BALLOON_F_STATS_VQ, | |
5a10b7db | 653 | VIRTIO_BALLOON_F_DEFLATE_ON_OOM, |
9564e138 | 654 | }; |
c45a6816 | 655 | |
d817cd52 | 656 | static struct virtio_driver virtio_balloon_driver = { |
c45a6816 RR |
657 | .feature_table = features, |
658 | .feature_table_size = ARRAY_SIZE(features), | |
6b35e407 RR |
659 | .driver.name = KBUILD_MODNAME, |
660 | .driver.owner = THIS_MODULE, | |
661 | .id_table = id_table, | |
662 | .probe = virtballoon_probe, | |
8590dbc7 | 663 | .remove = virtballoon_remove, |
6b35e407 | 664 | .config_changed = virtballoon_changed, |
89107000 | 665 | #ifdef CONFIG_PM_SLEEP |
e562966d AS |
666 | .freeze = virtballoon_freeze, |
667 | .restore = virtballoon_restore, | |
e562966d | 668 | #endif |
6b35e407 RR |
669 | }; |
670 | ||
b2a17029 | 671 | module_virtio_driver(virtio_balloon_driver); |
6b35e407 RR |
672 | MODULE_DEVICE_TABLE(virtio, id_table); |
673 | MODULE_DESCRIPTION("Virtio balloon driver"); | |
674 | MODULE_LICENSE("GPL"); |