1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/virtio.h>
23#include <linux/virtio_balloon.h>
24#include <linux/swap.h>
25#include <linux/workqueue.h>
26#include <linux/delay.h>
27#include <linux/slab.h>
28#include <linux/module.h>
29#include <linux/balloon_compaction.h>
30#include <linux/oom.h>
31#include <linux/mm.h>
32#include <linux/wait.h>
33
34
35
36
37
38
39#define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT)
40#define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
41#define OOM_VBALLOON_DEFAULT_PAGES 256
42#define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80
43
44static int oom_pages = OOM_VBALLOON_DEFAULT_PAGES;
45module_param(oom_pages, int, S_IRUSR | S_IWUSR);
46MODULE_PARM_DESC(oom_pages, "pages to free on OOM");
47
48struct virtio_balloon {
49 struct virtio_device *vdev;
50 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq;
51
52
53 struct work_struct update_balloon_stats_work;
54 struct work_struct update_balloon_size_work;
55
56
57 spinlock_t stop_update_lock;
58 bool stop_update;
59
60
61 wait_queue_head_t acked;
62
63
64 unsigned int num_pages;
65
66
67
68
69
70
71 struct balloon_dev_info vb_dev_info;
72
73
74 struct mutex balloon_lock;
75
76
77 unsigned int num_pfns;
78 __virtio32 pfns[VIRTIO_BALLOON_ARRAY_PFNS_MAX];
79
80
81 struct virtio_balloon_stat stats[VIRTIO_BALLOON_S_NR];
82
83
84 struct notifier_block nb;
85};
86
87static struct virtio_device_id id_table[] = {
88 { VIRTIO_ID_BALLOON, VIRTIO_DEV_ANY_ID },
89 { 0 },
90};
91
92static u32 page_to_balloon_pfn(struct page *page)
93{
94 unsigned long pfn = page_to_pfn(page);
95
96 BUILD_BUG_ON(PAGE_SHIFT < VIRTIO_BALLOON_PFN_SHIFT);
97
98 return pfn * VIRTIO_BALLOON_PAGES_PER_PAGE;
99}
100
101static void balloon_ack(struct virtqueue *vq)
102{
103 struct virtio_balloon *vb = vq->vdev->priv;
104
105 wake_up(&vb->acked);
106}
107
108static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq)
109{
110 struct scatterlist sg;
111 unsigned int len;
112
113 sg_init_one(&sg, vb->pfns, sizeof(vb->pfns[0]) * vb->num_pfns);
114
115
116 virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
117 virtqueue_kick(vq);
118
119
120 wait_event(vb->acked, virtqueue_get_buf(vq, &len));
121
122}
123
124static void set_page_pfns(struct virtio_balloon *vb,
125 __virtio32 pfns[], struct page *page)
126{
127 unsigned int i;
128
129
130
131
132
133 for (i = 0; i < VIRTIO_BALLOON_PAGES_PER_PAGE; i++)
134 pfns[i] = cpu_to_virtio32(vb->vdev,
135 page_to_balloon_pfn(page) + i);
136}
137
138static unsigned fill_balloon(struct virtio_balloon *vb, size_t num)
139{
140 unsigned num_allocated_pages;
141 unsigned num_pfns;
142 struct page *page;
143 LIST_HEAD(pages);
144
145
146 num = min(num, ARRAY_SIZE(vb->pfns));
147
148 for (num_pfns = 0; num_pfns < num;
149 num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
150 struct page *page = balloon_page_alloc();
151
152 if (!page) {
153 dev_info_ratelimited(&vb->vdev->dev,
154 "Out of puff! Can't get %u pages\n",
155 VIRTIO_BALLOON_PAGES_PER_PAGE);
156
157 msleep(200);
158 break;
159 }
160
161 balloon_page_push(&pages, page);
162 }
163
164 mutex_lock(&vb->balloon_lock);
165
166 vb->num_pfns = 0;
167
168 while ((page = balloon_page_pop(&pages))) {
169 balloon_page_enqueue(&vb->vb_dev_info, page);
170
171 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
172 vb->num_pages += VIRTIO_BALLOON_PAGES_PER_PAGE;
173 if (!virtio_has_feature(vb->vdev,
174 VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
175 adjust_managed_page_count(page, -1);
176 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE;
177 }
178
179 num_allocated_pages = vb->num_pfns;
180
181 if (vb->num_pfns != 0)
182 tell_host(vb, vb->inflate_vq);
183 mutex_unlock(&vb->balloon_lock);
184
185 return num_allocated_pages;
186}
187
188static void release_pages_balloon(struct virtio_balloon *vb,
189 struct list_head *pages)
190{
191 struct page *page, *next;
192
193 list_for_each_entry_safe(page, next, pages, lru) {
194 if (!virtio_has_feature(vb->vdev,
195 VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
196 adjust_managed_page_count(page, 1);
197 list_del(&page->lru);
198 put_page(page);
199 }
200}
201
202static unsigned leak_balloon(struct virtio_balloon *vb, size_t num)
203{
204 unsigned num_freed_pages;
205 struct page *page;
206 struct balloon_dev_info *vb_dev_info = &vb->vb_dev_info;
207 LIST_HEAD(pages);
208
209
210 num = min(num, ARRAY_SIZE(vb->pfns));
211
212 mutex_lock(&vb->balloon_lock);
213
214 num = min(num, (size_t)vb->num_pages);
215 for (vb->num_pfns = 0; vb->num_pfns < num;
216 vb->num_pfns += VIRTIO_BALLOON_PAGES_PER_PAGE) {
217 page = balloon_page_dequeue(vb_dev_info);
218 if (!page)
219 break;
220 set_page_pfns(vb, vb->pfns + vb->num_pfns, page);
221 list_add(&page->lru, &pages);
222 vb->num_pages -= VIRTIO_BALLOON_PAGES_PER_PAGE;
223 }
224
225 num_freed_pages = vb->num_pfns;
226
227
228
229
230
231 if (vb->num_pfns != 0)
232 tell_host(vb, vb->deflate_vq);
233 release_pages_balloon(vb, &pages);
234 mutex_unlock(&vb->balloon_lock);
235 return num_freed_pages;
236}
237
238static inline void update_stat(struct virtio_balloon *vb, int idx,
239 u16 tag, u64 val)
240{
241 BUG_ON(idx >= VIRTIO_BALLOON_S_NR);
242 vb->stats[idx].tag = cpu_to_virtio16(vb->vdev, tag);
243 vb->stats[idx].val = cpu_to_virtio64(vb->vdev, val);
244}
245
246#define pages_to_bytes(x) ((u64)(x) << PAGE_SHIFT)
247
248static unsigned int update_balloon_stats(struct virtio_balloon *vb)
249{
250 unsigned long events[NR_VM_EVENT_ITEMS];
251 struct sysinfo i;
252 unsigned int idx = 0;
253 long available;
254
255 all_vm_events(events);
256 si_meminfo(&i);
257
258 available = si_mem_available();
259
260#ifdef CONFIG_VM_EVENT_COUNTERS
261 update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_IN,
262 pages_to_bytes(events[PSWPIN]));
263 update_stat(vb, idx++, VIRTIO_BALLOON_S_SWAP_OUT,
264 pages_to_bytes(events[PSWPOUT]));
265 update_stat(vb, idx++, VIRTIO_BALLOON_S_MAJFLT, events[PGMAJFAULT]);
266 update_stat(vb, idx++, VIRTIO_BALLOON_S_MINFLT, events[PGFAULT]);
267#endif
268 update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMFREE,
269 pages_to_bytes(i.freeram));
270 update_stat(vb, idx++, VIRTIO_BALLOON_S_MEMTOT,
271 pages_to_bytes(i.totalram));
272 update_stat(vb, idx++, VIRTIO_BALLOON_S_AVAIL,
273 pages_to_bytes(available));
274
275 return idx;
276}
277
278
279
280
281
282
283
284
285
286
287static void stats_request(struct virtqueue *vq)
288{
289 struct virtio_balloon *vb = vq->vdev->priv;
290
291 spin_lock(&vb->stop_update_lock);
292 if (!vb->stop_update)
293 queue_work(system_freezable_wq, &vb->update_balloon_stats_work);
294 spin_unlock(&vb->stop_update_lock);
295}
296
297static void stats_handle_request(struct virtio_balloon *vb)
298{
299 struct virtqueue *vq;
300 struct scatterlist sg;
301 unsigned int len, num_stats;
302
303 num_stats = update_balloon_stats(vb);
304
305 vq = vb->stats_vq;
306 if (!virtqueue_get_buf(vq, &len))
307 return;
308 sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
309 virtqueue_add_outbuf(vq, &sg, 1, vb, GFP_KERNEL);
310 virtqueue_kick(vq);
311}
312
313static void virtballoon_changed(struct virtio_device *vdev)
314{
315 struct virtio_balloon *vb = vdev->priv;
316 unsigned long flags;
317
318 spin_lock_irqsave(&vb->stop_update_lock, flags);
319 if (!vb->stop_update)
320 queue_work(system_freezable_wq, &vb->update_balloon_size_work);
321 spin_unlock_irqrestore(&vb->stop_update_lock, flags);
322}
323
324static inline s64 towards_target(struct virtio_balloon *vb)
325{
326 s64 target;
327 u32 num_pages;
328
329 virtio_cread(vb->vdev, struct virtio_balloon_config, num_pages,
330 &num_pages);
331
332
333 if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
334 num_pages = le32_to_cpu((__force __le32)num_pages);
335
336 target = num_pages;
337 return target - vb->num_pages;
338}
339
340static void update_balloon_size(struct virtio_balloon *vb)
341{
342 u32 actual = vb->num_pages;
343
344
345 if (!virtio_has_feature(vb->vdev, VIRTIO_F_VERSION_1))
346 actual = (__force u32)cpu_to_le32(actual);
347
348 virtio_cwrite(vb->vdev, struct virtio_balloon_config, actual,
349 &actual);
350}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365static int virtballoon_oom_notify(struct notifier_block *self,
366 unsigned long dummy, void *parm)
367{
368 struct virtio_balloon *vb;
369 unsigned long *freed;
370 unsigned num_freed_pages;
371
372 vb = container_of(self, struct virtio_balloon, nb);
373 if (!virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
374 return NOTIFY_OK;
375
376 freed = parm;
377 num_freed_pages = leak_balloon(vb, oom_pages);
378 update_balloon_size(vb);
379 *freed += num_freed_pages;
380
381 return NOTIFY_OK;
382}
383
384static void update_balloon_stats_func(struct work_struct *work)
385{
386 struct virtio_balloon *vb;
387
388 vb = container_of(work, struct virtio_balloon,
389 update_balloon_stats_work);
390 stats_handle_request(vb);
391}
392
393static void update_balloon_size_func(struct work_struct *work)
394{
395 struct virtio_balloon *vb;
396 s64 diff;
397
398 vb = container_of(work, struct virtio_balloon,
399 update_balloon_size_work);
400 diff = towards_target(vb);
401
402 if (diff > 0)
403 diff -= fill_balloon(vb, diff);
404 else if (diff < 0)
405 diff += leak_balloon(vb, -diff);
406 update_balloon_size(vb);
407
408 if (diff)
409 queue_work(system_freezable_wq, work);
410}
411
412static int init_vqs(struct virtio_balloon *vb)
413{
414 struct virtqueue *vqs[3];
415 vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request };
416 static const char * const names[] = { "inflate", "deflate", "stats" };
417 int err, nvqs;
418
419
420
421
422
423 nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2;
424 err = vb->vdev->config->find_vqs(vb->vdev, nvqs, vqs, callbacks, names);
425 if (err)
426 return err;
427
428 vb->inflate_vq = vqs[0];
429 vb->deflate_vq = vqs[1];
430 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
431 struct scatterlist sg;
432 unsigned int num_stats;
433 vb->stats_vq = vqs[2];
434
435
436
437
438
439 num_stats = update_balloon_stats(vb);
440
441 sg_init_one(&sg, vb->stats, sizeof(vb->stats[0]) * num_stats);
442 if (virtqueue_add_outbuf(vb->stats_vq, &sg, 1, vb, GFP_KERNEL)
443 < 0)
444 BUG();
445 virtqueue_kick(vb->stats_vq);
446 }
447 return 0;
448}
449
450#ifdef CONFIG_BALLOON_COMPACTION
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469static int virtballoon_migratepage(struct balloon_dev_info *vb_dev_info,
470 struct page *newpage, struct page *page, enum migrate_mode mode)
471{
472 struct virtio_balloon *vb = container_of(vb_dev_info,
473 struct virtio_balloon, vb_dev_info);
474 unsigned long flags;
475
476
477
478
479
480
481
482
483
484 if (!mutex_trylock(&vb->balloon_lock))
485 return -EAGAIN;
486
487 get_page(newpage);
488
489
490 spin_lock_irqsave(&vb_dev_info->pages_lock, flags);
491 balloon_page_insert(vb_dev_info, newpage);
492 vb_dev_info->isolated_pages--;
493 __count_vm_event(BALLOON_MIGRATE);
494 spin_unlock_irqrestore(&vb_dev_info->pages_lock, flags);
495 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
496 set_page_pfns(vb, vb->pfns, newpage);
497 tell_host(vb, vb->inflate_vq);
498
499
500 balloon_page_delete(page);
501 vb->num_pfns = VIRTIO_BALLOON_PAGES_PER_PAGE;
502 set_page_pfns(vb, vb->pfns, page);
503 tell_host(vb, vb->deflate_vq);
504
505 mutex_unlock(&vb->balloon_lock);
506
507 put_page(page);
508
509 return MIGRATEPAGE_SUCCESS;
510}
511#endif
512
513static int virtballoon_probe(struct virtio_device *vdev)
514{
515 struct virtio_balloon *vb;
516 int err;
517
518 if (!vdev->config->get) {
519 dev_err(&vdev->dev, "%s failure: config access disabled\n",
520 __func__);
521 return -EINVAL;
522 }
523
524 vdev->priv = vb = kmalloc(sizeof(*vb), GFP_KERNEL);
525 if (!vb) {
526 err = -ENOMEM;
527 goto out;
528 }
529
530 INIT_WORK(&vb->update_balloon_stats_work, update_balloon_stats_func);
531 INIT_WORK(&vb->update_balloon_size_work, update_balloon_size_func);
532 spin_lock_init(&vb->stop_update_lock);
533 vb->stop_update = false;
534 vb->num_pages = 0;
535 mutex_init(&vb->balloon_lock);
536 init_waitqueue_head(&vb->acked);
537 vb->vdev = vdev;
538
539 balloon_devinfo_init(&vb->vb_dev_info);
540#ifdef CONFIG_BALLOON_COMPACTION
541 vb->vb_dev_info.migratepage = virtballoon_migratepage;
542#endif
543
544 err = init_vqs(vb);
545 if (err)
546 goto out_free_vb;
547
548 vb->nb.notifier_call = virtballoon_oom_notify;
549 vb->nb.priority = VIRTBALLOON_OOM_NOTIFY_PRIORITY;
550 err = register_oom_notifier(&vb->nb);
551 if (err < 0)
552 goto out_oom_notify;
553
554 virtio_device_ready(vdev);
555
556 if (towards_target(vb))
557 virtballoon_changed(vdev);
558 return 0;
559
560out_oom_notify:
561 vdev->config->del_vqs(vdev);
562out_free_vb:
563 kfree(vb);
564out:
565 return err;
566}
567
568static void remove_common(struct virtio_balloon *vb)
569{
570
571 while (vb->num_pages)
572 leak_balloon(vb, vb->num_pages);
573 update_balloon_size(vb);
574
575
576 vb->vdev->config->reset(vb->vdev);
577
578 vb->vdev->config->del_vqs(vb->vdev);
579}
580
581static void virtballoon_remove(struct virtio_device *vdev)
582{
583 struct virtio_balloon *vb = vdev->priv;
584
585 unregister_oom_notifier(&vb->nb);
586
587 spin_lock_irq(&vb->stop_update_lock);
588 vb->stop_update = true;
589 spin_unlock_irq(&vb->stop_update_lock);
590 cancel_work_sync(&vb->update_balloon_size_work);
591 cancel_work_sync(&vb->update_balloon_stats_work);
592
593 remove_common(vb);
594 kfree(vb);
595}
596
597#ifdef CONFIG_PM_SLEEP
598static int virtballoon_freeze(struct virtio_device *vdev)
599{
600 struct virtio_balloon *vb = vdev->priv;
601
602
603
604
605
606 remove_common(vb);
607 return 0;
608}
609
610static int virtballoon_restore(struct virtio_device *vdev)
611{
612 struct virtio_balloon *vb = vdev->priv;
613 int ret;
614
615 ret = init_vqs(vdev->priv);
616 if (ret)
617 return ret;
618
619 virtio_device_ready(vdev);
620
621 if (towards_target(vb))
622 virtballoon_changed(vdev);
623 update_balloon_size(vb);
624 return 0;
625}
626#endif
627
628static int virtballoon_validate(struct virtio_device *vdev)
629{
630 __virtio_clear_bit(vdev, VIRTIO_F_IOMMU_PLATFORM);
631 return 0;
632}
633
634static unsigned int features[] = {
635 VIRTIO_BALLOON_F_MUST_TELL_HOST,
636 VIRTIO_BALLOON_F_STATS_VQ,
637 VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
638};
639
640static struct virtio_driver virtio_balloon_driver = {
641 .feature_table = features,
642 .feature_table_size = ARRAY_SIZE(features),
643 .driver.name = KBUILD_MODNAME,
644 .driver.owner = THIS_MODULE,
645 .id_table = id_table,
646 .validate = virtballoon_validate,
647 .probe = virtballoon_probe,
648 .remove = virtballoon_remove,
649 .config_changed = virtballoon_changed,
650#ifdef CONFIG_PM_SLEEP
651 .freeze = virtballoon_freeze,
652 .restore = virtballoon_restore,
653#endif
654};
655
656module_virtio_driver(virtio_balloon_driver);
657MODULE_DEVICE_TABLE(virtio, id_table);
658MODULE_DESCRIPTION("Virtio balloon driver");
659MODULE_LICENSE("GPL");
660