aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWei Wang <wei.w.wang@intel.com>2018-08-26 21:32:17 -0400
committerMichael S. Tsirkin <mst@redhat.com>2018-10-24 20:57:55 -0400
commit86a559787e6f5cf662c081363f64a20cad654195 (patch)
tree472f5b00968b40256b31c5e39ba9d69e7c573da5
parentd7b31359ecef8d32540266f39d99892f61d17c4b (diff)
virtio-balloon: VIRTIO_BALLOON_F_FREE_PAGE_HINT
Negotiation of the VIRTIO_BALLOON_F_FREE_PAGE_HINT feature indicates the support of reporting hints of guest free pages to host via virtio-balloon. Currenlty, only free page blocks of MAX_ORDER - 1 are reported. They are obtained one by one from the mm free list via the regular allocation function. Host requests the guest to report free page hints by sending a new cmd id to the guest via the free_page_report_cmd_id configuration register. When the guest starts to report, it first sends a start cmd to host via the free page vq, which acks to host the cmd id received. When the guest finishes reporting free pages, a stop cmd is sent to host via the vq. Host may also send a stop cmd id to the guest to stop the reporting. VIRTIO_BALLOON_CMD_ID_STOP: Host sends this cmd to stop the guest reporting. VIRTIO_BALLOON_CMD_ID_DONE: Host sends this cmd to tell the guest that the reported pages are ready to be freed. Why does the guest free the reported pages when host tells it is ready to free? This is because freeing pages appears to be expensive for live migration. free_pages() dirties memory very quickly and makes the live migraion not converge in some cases. So it is good to delay the free_page operation when the migration is done, and host sends a command to guest about that. Why do we need the new VIRTIO_BALLOON_CMD_ID_DONE, instead of reusing VIRTIO_BALLOON_CMD_ID_STOP? This is because live migration is usually done in several rounds. At the end of each round, host needs to send a VIRTIO_BALLOON_CMD_ID_STOP cmd to the guest to stop (or say pause) the reporting. The guest resumes the reporting when it receives a new command id at the beginning of the next round. So we need a new cmd id to distinguish between "stop reporting" and "ready to free the reported pages". TODO: - Add a batch page allocation API to amortize the allocation overhead. Signed-off-by: Wei Wang <wei.w.wang@intel.com> Signed-off-by: Liang Li <liang.z.li@intel.com> Cc: Michael S. Tsirkin <mst@redhat.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
-rw-r--r--drivers/virtio/virtio_balloon.c364
-rw-r--r--include/uapi/linux/virtio_balloon.h5
2 files changed, 336 insertions, 33 deletions
diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index d1c1f6283729..a18567889289 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -41,13 +41,34 @@
41#define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256 41#define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256
42#define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80 42#define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80
43 43
44#define VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG (__GFP_NORETRY | __GFP_NOWARN | \
45 __GFP_NOMEMALLOC)
46/* The order of free page blocks to report to host */
47#define VIRTIO_BALLOON_FREE_PAGE_ORDER (MAX_ORDER - 1)
48/* The size of a free page block in bytes */
49#define VIRTIO_BALLOON_FREE_PAGE_SIZE \
50 (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT))
51
44#ifdef CONFIG_BALLOON_COMPACTION 52#ifdef CONFIG_BALLOON_COMPACTION
45static struct vfsmount *balloon_mnt; 53static struct vfsmount *balloon_mnt;
46#endif 54#endif
47 55
56enum virtio_balloon_vq {
57 VIRTIO_BALLOON_VQ_INFLATE,
58 VIRTIO_BALLOON_VQ_DEFLATE,
59 VIRTIO_BALLOON_VQ_STATS,
60 VIRTIO_BALLOON_VQ_FREE_PAGE,
61 VIRTIO_BALLOON_VQ_MAX
62};
63
48struct virtio_balloon { 64struct virtio_balloon {
49 struct virtio_device *vdev; 65 struct virtio_device *vdev;
50 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq; 66 struct virtqueue *inflate_vq, *deflate_vq, *stats_vq, *free_page_vq;
67
68 /* Balloon's own wq for cpu-intensive work items */
69 struct workqueue_struct *balloon_wq;
70 /* The free page reporting work item submitted to the balloon wq */
71 struct work_struct report_free_page_work;
51 72
52 /* The balloon servicing is delegated to a freezable workqueue. */ 73 /* The balloon servicing is delegated to a freezable workqueue. */
53 struct work_struct update_balloon_stats_work; 74 struct work_struct update_balloon_stats_work;
@@ -57,6 +78,18 @@ struct virtio_balloon {
57 spinlock_t stop_update_lock; 78 spinlock_t stop_update_lock;
58 bool stop_update; 79 bool stop_update;
59 80
81 /* The list of allocated free pages, waiting to be given back to mm */
82 struct list_head free_page_list;
83 spinlock_t free_page_list_lock;
84 /* The number of free page blocks on the above list */
85 unsigned long num_free_page_blocks;
86 /* The cmd id received from host */
87 u32 cmd_id_received;
88 /* The cmd id that is actively in use */
89 __virtio32 cmd_id_active;
90 /* Buffer to store the stop sign */
91 __virtio32 cmd_id_stop;
92
60 /* Waiting for host to ack the pages we released. */ 93 /* Waiting for host to ack the pages we released. */
61 wait_queue_head_t acked; 94 wait_queue_head_t acked;
62 95
@@ -320,17 +353,6 @@ static void stats_handle_request(struct virtio_balloon *vb)
320 virtqueue_kick(vq); 353 virtqueue_kick(vq);
321} 354}
322 355
323static void virtballoon_changed(struct virtio_device *vdev)
324{
325 struct virtio_balloon *vb = vdev->priv;
326 unsigned long flags;
327
328 spin_lock_irqsave(&vb->stop_update_lock, flags);
329 if (!vb->stop_update)
330 queue_work(system_freezable_wq, &vb->update_balloon_size_work);
331 spin_unlock_irqrestore(&vb->stop_update_lock, flags);
332}
333
334static inline s64 towards_target(struct virtio_balloon *vb) 356static inline s64 towards_target(struct virtio_balloon *vb)
335{ 357{
336 s64 target; 358 s64 target;
@@ -347,6 +369,60 @@ static inline s64 towards_target(struct virtio_balloon *vb)
347 return target - vb->num_pages; 369 return target - vb->num_pages;
348} 370}
349 371
372/* Gives back @num_to_return blocks of free pages to mm. */
373static unsigned long return_free_pages_to_mm(struct virtio_balloon *vb,
374 unsigned long num_to_return)
375{
376 struct page *page;
377 unsigned long num_returned;
378
379 spin_lock_irq(&vb->free_page_list_lock);
380 for (num_returned = 0; num_returned < num_to_return; num_returned++) {
381 page = balloon_page_pop(&vb->free_page_list);
382 if (!page)
383 break;
384 free_pages((unsigned long)page_address(page),
385 VIRTIO_BALLOON_FREE_PAGE_ORDER);
386 }
387 vb->num_free_page_blocks -= num_returned;
388 spin_unlock_irq(&vb->free_page_list_lock);
389
390 return num_returned;
391}
392
393static void virtballoon_changed(struct virtio_device *vdev)
394{
395 struct virtio_balloon *vb = vdev->priv;
396 unsigned long flags;
397 s64 diff = towards_target(vb);
398
399 if (diff) {
400 spin_lock_irqsave(&vb->stop_update_lock, flags);
401 if (!vb->stop_update)
402 queue_work(system_freezable_wq,
403 &vb->update_balloon_size_work);
404 spin_unlock_irqrestore(&vb->stop_update_lock, flags);
405 }
406
407 if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
408 virtio_cread(vdev, struct virtio_balloon_config,
409 free_page_report_cmd_id, &vb->cmd_id_received);
410 if (vb->cmd_id_received == VIRTIO_BALLOON_CMD_ID_DONE) {
411 /* Pass ULONG_MAX to give back all the free pages */
412 return_free_pages_to_mm(vb, ULONG_MAX);
413 } else if (vb->cmd_id_received != VIRTIO_BALLOON_CMD_ID_STOP &&
414 vb->cmd_id_received !=
415 virtio32_to_cpu(vdev, vb->cmd_id_active)) {
416 spin_lock_irqsave(&vb->stop_update_lock, flags);
417 if (!vb->stop_update) {
418 queue_work(vb->balloon_wq,
419 &vb->report_free_page_work);
420 }
421 spin_unlock_irqrestore(&vb->stop_update_lock, flags);
422 }
423 }
424}
425
350static void update_balloon_size(struct virtio_balloon *vb) 426static void update_balloon_size(struct virtio_balloon *vb)
351{ 427{
352 u32 actual = vb->num_pages; 428 u32 actual = vb->num_pages;
@@ -389,26 +465,44 @@ static void update_balloon_size_func(struct work_struct *work)
389 465
390static int init_vqs(struct virtio_balloon *vb) 466static int init_vqs(struct virtio_balloon *vb)
391{ 467{
392 struct virtqueue *vqs[3]; 468 struct virtqueue *vqs[VIRTIO_BALLOON_VQ_MAX];
393 vq_callback_t *callbacks[] = { balloon_ack, balloon_ack, stats_request }; 469 vq_callback_t *callbacks[VIRTIO_BALLOON_VQ_MAX];
394 static const char * const names[] = { "inflate", "deflate", "stats" }; 470 const char *names[VIRTIO_BALLOON_VQ_MAX];
395 int err, nvqs; 471 int err;
396 472
397 /* 473 /*
398 * We expect two virtqueues: inflate and deflate, and 474 * Inflateq and deflateq are used unconditionally. The names[]
399 * optionally stat. 475 * will be NULL if the related feature is not enabled, which will
476 * cause no allocation for the corresponding virtqueue in find_vqs.
400 */ 477 */
401 nvqs = virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ) ? 3 : 2; 478 callbacks[VIRTIO_BALLOON_VQ_INFLATE] = balloon_ack;
402 err = virtio_find_vqs(vb->vdev, nvqs, vqs, callbacks, names, NULL); 479 names[VIRTIO_BALLOON_VQ_INFLATE] = "inflate";
480 callbacks[VIRTIO_BALLOON_VQ_DEFLATE] = balloon_ack;
481 names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate";
482 names[VIRTIO_BALLOON_VQ_STATS] = NULL;
483 names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
484
485 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
486 names[VIRTIO_BALLOON_VQ_STATS] = "stats";
487 callbacks[VIRTIO_BALLOON_VQ_STATS] = stats_request;
488 }
489
490 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
491 names[VIRTIO_BALLOON_VQ_FREE_PAGE] = "free_page_vq";
492 callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL;
493 }
494
495 err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX,
496 vqs, callbacks, names, NULL, NULL);
403 if (err) 497 if (err)
404 return err; 498 return err;
405 499
406 vb->inflate_vq = vqs[0]; 500 vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE];
407 vb->deflate_vq = vqs[1]; 501 vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE];
408 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { 502 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) {
409 struct scatterlist sg; 503 struct scatterlist sg;
410 unsigned int num_stats; 504 unsigned int num_stats;
411 vb->stats_vq = vqs[2]; 505 vb->stats_vq = vqs[VIRTIO_BALLOON_VQ_STATS];
412 506
413 /* 507 /*
414 * Prime this virtqueue with one buffer so the hypervisor can 508 * Prime this virtqueue with one buffer so the hypervisor can
@@ -426,9 +520,145 @@ static int init_vqs(struct virtio_balloon *vb)
426 } 520 }
427 virtqueue_kick(vb->stats_vq); 521 virtqueue_kick(vb->stats_vq);
428 } 522 }
523
524 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
525 vb->free_page_vq = vqs[VIRTIO_BALLOON_VQ_FREE_PAGE];
526
527 return 0;
528}
529
530static int send_cmd_id_start(struct virtio_balloon *vb)
531{
532 struct scatterlist sg;
533 struct virtqueue *vq = vb->free_page_vq;
534 int err, unused;
535
536 /* Detach all the used buffers from the vq */
537 while (virtqueue_get_buf(vq, &unused))
538 ;
539
540 vb->cmd_id_active = cpu_to_virtio32(vb->vdev, vb->cmd_id_received);
541 sg_init_one(&sg, &vb->cmd_id_active, sizeof(vb->cmd_id_active));
542 err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_active, GFP_KERNEL);
543 if (!err)
544 virtqueue_kick(vq);
545 return err;
546}
547
548static int send_cmd_id_stop(struct virtio_balloon *vb)
549{
550 struct scatterlist sg;
551 struct virtqueue *vq = vb->free_page_vq;
552 int err, unused;
553
554 /* Detach all the used buffers from the vq */
555 while (virtqueue_get_buf(vq, &unused))
556 ;
557
558 sg_init_one(&sg, &vb->cmd_id_stop, sizeof(vb->cmd_id_stop));
559 err = virtqueue_add_outbuf(vq, &sg, 1, &vb->cmd_id_stop, GFP_KERNEL);
560 if (!err)
561 virtqueue_kick(vq);
562 return err;
563}
564
565static int get_free_page_and_send(struct virtio_balloon *vb)
566{
567 struct virtqueue *vq = vb->free_page_vq;
568 struct page *page;
569 struct scatterlist sg;
570 int err, unused;
571 void *p;
572
573 /* Detach all the used buffers from the vq */
574 while (virtqueue_get_buf(vq, &unused))
575 ;
576
577 page = alloc_pages(VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG,
578 VIRTIO_BALLOON_FREE_PAGE_ORDER);
579 /*
580 * When the allocation returns NULL, it indicates that we have got all
581 * the possible free pages, so return -EINTR to stop.
582 */
583 if (!page)
584 return -EINTR;
585
586 p = page_address(page);
587 sg_init_one(&sg, p, VIRTIO_BALLOON_FREE_PAGE_SIZE);
588 /* There is always 1 entry reserved for the cmd id to use. */
589 if (vq->num_free > 1) {
590 err = virtqueue_add_inbuf(vq, &sg, 1, p, GFP_KERNEL);
591 if (unlikely(err)) {
592 free_pages((unsigned long)p,
593 VIRTIO_BALLOON_FREE_PAGE_ORDER);
594 return err;
595 }
596 virtqueue_kick(vq);
597 spin_lock_irq(&vb->free_page_list_lock);
598 balloon_page_push(&vb->free_page_list, page);
599 vb->num_free_page_blocks++;
600 spin_unlock_irq(&vb->free_page_list_lock);
601 } else {
602 /*
603 * The vq has no available entry to add this page block, so
604 * just free it.
605 */
606 free_pages((unsigned long)p, VIRTIO_BALLOON_FREE_PAGE_ORDER);
607 }
608
429 return 0; 609 return 0;
430} 610}
431 611
612static int send_free_pages(struct virtio_balloon *vb)
613{
614 int err;
615 u32 cmd_id_active;
616
617 while (1) {
618 /*
619 * If a stop id or a new cmd id was just received from host,
620 * stop the reporting.
621 */
622 cmd_id_active = virtio32_to_cpu(vb->vdev, vb->cmd_id_active);
623 if (cmd_id_active != vb->cmd_id_received)
624 break;
625
626 /*
627 * The free page blocks are allocated and sent to host one by
628 * one.
629 */
630 err = get_free_page_and_send(vb);
631 if (err == -EINTR)
632 break;
633 else if (unlikely(err))
634 return err;
635 }
636
637 return 0;
638}
639
640static void report_free_page_func(struct work_struct *work)
641{
642 int err;
643 struct virtio_balloon *vb = container_of(work, struct virtio_balloon,
644 report_free_page_work);
645 struct device *dev = &vb->vdev->dev;
646
647 /* Start by sending the received cmd id to host with an outbuf. */
648 err = send_cmd_id_start(vb);
649 if (unlikely(err))
650 dev_err(dev, "Failed to send a start id, err = %d\n", err);
651
652 err = send_free_pages(vb);
653 if (unlikely(err))
654 dev_err(dev, "Failed to send a free page, err = %d\n", err);
655
656 /* End by sending a stop id to host with an outbuf. */
657 err = send_cmd_id_stop(vb);
658 if (unlikely(err))
659 dev_err(dev, "Failed to send a stop id, err = %d\n", err);
660}
661
432#ifdef CONFIG_BALLOON_COMPACTION 662#ifdef CONFIG_BALLOON_COMPACTION
433/* 663/*
434 * virtballoon_migratepage - perform the balloon page migration on behalf of 664 * virtballoon_migratepage - perform the balloon page migration on behalf of
@@ -512,14 +742,23 @@ static struct file_system_type balloon_fs = {
512 742
513#endif /* CONFIG_BALLOON_COMPACTION */ 743#endif /* CONFIG_BALLOON_COMPACTION */
514 744
515static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker, 745static unsigned long shrink_free_pages(struct virtio_balloon *vb,
516 struct shrink_control *sc) 746 unsigned long pages_to_free)
517{ 747{
518 unsigned long pages_to_free, pages_freed = 0; 748 unsigned long blocks_to_free, blocks_freed;
519 struct virtio_balloon *vb = container_of(shrinker,
520 struct virtio_balloon, shrinker);
521 749
522 pages_to_free = sc->nr_to_scan * VIRTIO_BALLOON_PAGES_PER_PAGE; 750 pages_to_free = round_up(pages_to_free,
751 1 << VIRTIO_BALLOON_FREE_PAGE_ORDER);
752 blocks_to_free = pages_to_free >> VIRTIO_BALLOON_FREE_PAGE_ORDER;
753 blocks_freed = return_free_pages_to_mm(vb, blocks_to_free);
754
755 return blocks_freed << VIRTIO_BALLOON_FREE_PAGE_ORDER;
756}
757
758static unsigned long shrink_balloon_pages(struct virtio_balloon *vb,
759 unsigned long pages_to_free)
760{
761 unsigned long pages_freed = 0;
523 762
524 /* 763 /*
525 * One invocation of leak_balloon can deflate at most 764 * One invocation of leak_balloon can deflate at most
@@ -527,12 +766,33 @@ static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
527 * multiple times to deflate pages till reaching pages_to_free. 766 * multiple times to deflate pages till reaching pages_to_free.
528 */ 767 */
529 while (vb->num_pages && pages_to_free) { 768 while (vb->num_pages && pages_to_free) {
769 pages_freed += leak_balloon(vb, pages_to_free) /
770 VIRTIO_BALLOON_PAGES_PER_PAGE;
530 pages_to_free -= pages_freed; 771 pages_to_free -= pages_freed;
531 pages_freed += leak_balloon(vb, pages_to_free);
532 } 772 }
533 update_balloon_size(vb); 773 update_balloon_size(vb);
534 774
535 return pages_freed / VIRTIO_BALLOON_PAGES_PER_PAGE; 775 return pages_freed;
776}
777
778static unsigned long virtio_balloon_shrinker_scan(struct shrinker *shrinker,
779 struct shrink_control *sc)
780{
781 unsigned long pages_to_free, pages_freed = 0;
782 struct virtio_balloon *vb = container_of(shrinker,
783 struct virtio_balloon, shrinker);
784
785 pages_to_free = sc->nr_to_scan * VIRTIO_BALLOON_PAGES_PER_PAGE;
786
787 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
788 pages_freed = shrink_free_pages(vb, pages_to_free);
789
790 if (pages_freed >= pages_to_free)
791 return pages_freed;
792
793 pages_freed += shrink_balloon_pages(vb, pages_to_free - pages_freed);
794
795 return pages_freed;
536} 796}
537 797
538static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker, 798static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
@@ -540,8 +800,12 @@ static unsigned long virtio_balloon_shrinker_count(struct shrinker *shrinker,
540{ 800{
541 struct virtio_balloon *vb = container_of(shrinker, 801 struct virtio_balloon *vb = container_of(shrinker,
542 struct virtio_balloon, shrinker); 802 struct virtio_balloon, shrinker);
803 unsigned long count;
543 804
544 return vb->num_pages / VIRTIO_BALLOON_PAGES_PER_PAGE; 805 count = vb->num_pages / VIRTIO_BALLOON_PAGES_PER_PAGE;
806 count += vb->num_free_page_blocks >> VIRTIO_BALLOON_FREE_PAGE_ORDER;
807
808 return count;
545} 809}
546 810
547static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb) 811static void virtio_balloon_unregister_shrinker(struct virtio_balloon *vb)
@@ -604,6 +868,31 @@ static int virtballoon_probe(struct virtio_device *vdev)
604 } 868 }
605 vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops; 869 vb->vb_dev_info.inode->i_mapping->a_ops = &balloon_aops;
606#endif 870#endif
871 if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
872 /*
873 * There is always one entry reserved for cmd id, so the ring
874 * size needs to be at least two to report free page hints.
875 */
876 if (virtqueue_get_vring_size(vb->free_page_vq) < 2) {
877 err = -ENOSPC;
878 goto out_del_vqs;
879 }
880 vb->balloon_wq = alloc_workqueue("balloon-wq",
881 WQ_FREEZABLE | WQ_CPU_INTENSIVE, 0);
882 if (!vb->balloon_wq) {
883 err = -ENOMEM;
884 goto out_del_vqs;
885 }
886 INIT_WORK(&vb->report_free_page_work, report_free_page_func);
887 vb->cmd_id_received = VIRTIO_BALLOON_CMD_ID_STOP;
888 vb->cmd_id_active = cpu_to_virtio32(vb->vdev,
889 VIRTIO_BALLOON_CMD_ID_STOP);
890 vb->cmd_id_stop = cpu_to_virtio32(vb->vdev,
891 VIRTIO_BALLOON_CMD_ID_STOP);
892 vb->num_free_page_blocks = 0;
893 spin_lock_init(&vb->free_page_list_lock);
894 INIT_LIST_HEAD(&vb->free_page_list);
895 }
607 /* 896 /*
608 * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a 897 * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a
609 * shrinker needs to be registered to relieve memory pressure. 898 * shrinker needs to be registered to relieve memory pressure.
@@ -611,7 +900,7 @@ static int virtballoon_probe(struct virtio_device *vdev)
611 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) { 900 if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) {
612 err = virtio_balloon_register_shrinker(vb); 901 err = virtio_balloon_register_shrinker(vb);
613 if (err) 902 if (err)
614 goto out_del_vqs; 903 goto out_del_balloon_wq;
615 } 904 }
616 virtio_device_ready(vdev); 905 virtio_device_ready(vdev);
617 906
@@ -619,6 +908,9 @@ static int virtballoon_probe(struct virtio_device *vdev)
619 virtballoon_changed(vdev); 908 virtballoon_changed(vdev);
620 return 0; 909 return 0;
621 910
911out_del_balloon_wq:
912 if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
913 destroy_workqueue(vb->balloon_wq);
622out_del_vqs: 914out_del_vqs:
623 vdev->config->del_vqs(vdev); 915 vdev->config->del_vqs(vdev);
624out_free_vb: 916out_free_vb:
@@ -652,6 +944,11 @@ static void virtballoon_remove(struct virtio_device *vdev)
652 cancel_work_sync(&vb->update_balloon_size_work); 944 cancel_work_sync(&vb->update_balloon_size_work);
653 cancel_work_sync(&vb->update_balloon_stats_work); 945 cancel_work_sync(&vb->update_balloon_stats_work);
654 946
947 if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) {
948 cancel_work_sync(&vb->report_free_page_work);
949 destroy_workqueue(vb->balloon_wq);
950 }
951
655 remove_common(vb); 952 remove_common(vb);
656#ifdef CONFIG_BALLOON_COMPACTION 953#ifdef CONFIG_BALLOON_COMPACTION
657 if (vb->vb_dev_info.inode) 954 if (vb->vb_dev_info.inode)
@@ -703,6 +1000,7 @@ static unsigned int features[] = {
703 VIRTIO_BALLOON_F_MUST_TELL_HOST, 1000 VIRTIO_BALLOON_F_MUST_TELL_HOST,
704 VIRTIO_BALLOON_F_STATS_VQ, 1001 VIRTIO_BALLOON_F_STATS_VQ,
705 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, 1002 VIRTIO_BALLOON_F_DEFLATE_ON_OOM,
1003 VIRTIO_BALLOON_F_FREE_PAGE_HINT,
706}; 1004};
707 1005
708static struct virtio_driver virtio_balloon_driver = { 1006static struct virtio_driver virtio_balloon_driver = {
diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h
index 13b8cb563892..47c9eb401c08 100644
--- a/include/uapi/linux/virtio_balloon.h
+++ b/include/uapi/linux/virtio_balloon.h
@@ -34,15 +34,20 @@
34#define VIRTIO_BALLOON_F_MUST_TELL_HOST 0 /* Tell before reclaiming pages */ 34#define VIRTIO_BALLOON_F_MUST_TELL_HOST 0 /* Tell before reclaiming pages */
35#define VIRTIO_BALLOON_F_STATS_VQ 1 /* Memory Stats virtqueue */ 35#define VIRTIO_BALLOON_F_STATS_VQ 1 /* Memory Stats virtqueue */
36#define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */ 36#define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */
37#define VIRTIO_BALLOON_F_FREE_PAGE_HINT 3 /* VQ to report free pages */
37 38
38/* Size of a PFN in the balloon interface. */ 39/* Size of a PFN in the balloon interface. */
39#define VIRTIO_BALLOON_PFN_SHIFT 12 40#define VIRTIO_BALLOON_PFN_SHIFT 12
40 41
42#define VIRTIO_BALLOON_CMD_ID_STOP 0
43#define VIRTIO_BALLOON_CMD_ID_DONE 1
41struct virtio_balloon_config { 44struct virtio_balloon_config {
42 /* Number of pages host wants Guest to give up. */ 45 /* Number of pages host wants Guest to give up. */
43 __u32 num_pages; 46 __u32 num_pages;
44 /* Number of pages we've actually got in balloon. */ 47 /* Number of pages we've actually got in balloon. */
45 __u32 actual; 48 __u32 actual;
49 /* Free page report command id, readonly by guest */
50 __u32 free_page_report_cmd_id;
46}; 51};
47 52
48#define VIRTIO_BALLOON_S_SWAP_IN 0 /* Amount of memory swapped in */ 53#define VIRTIO_BALLOON_S_SWAP_IN 0 /* Amount of memory swapped in */