[PATCH 5/6] virtio-balloon: reporting hugetlb free page to host

From: Liang Li
Date: Tue Jan 05 2021 - 22:51:59 EST


Free page reporting only supports buddy pages, it can't report the
free pages reserved for hugetlbfs case. On the other hand, hugetlbfs
is a good choice for a system with a huge amount of RAM, because it
can help to reduce the memory management overhead and improve system
performance. This patch add support for reporting free hugepage to
host when guest use hugetlbfs.

Cc: Alexander Duyck <alexander.h.duyck@xxxxxxxxxxxxxxx>
Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx>
Cc: Andrea Arcangeli <aarcange@xxxxxxxxxx>
Cc: Dan Williams <dan.j.williams@xxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxx>
Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Alex Williamson <alex.williamson@xxxxxxxxxx>
Cc: Michael S. Tsirkin <mst@xxxxxxxxxx>
Cc: Liang Li <liliang324@xxxxxxxxx>
Signed-off-by: Liang Li <liliangleo@xxxxxxxxxxxxxx>
---
drivers/virtio/virtio_balloon.c | 55 +++++++++++++++++++++++++++++++--
1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index 684bcc39ef5a..7bd7fcacee8c 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -126,6 +126,10 @@ struct virtio_balloon {
/* Free page reporting device */
struct virtqueue *reporting_vq;
struct page_reporting_dev_info pr_dev_info;
+
+ /* Free hugepage reporting device */
+ struct page_reporting_dev_info hpr_dev_info;
+ struct mutex mtx_report;
};

static const struct virtio_device_id id_table[] = {
@@ -173,6 +177,38 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i
struct virtqueue *vq = vb->reporting_vq;
unsigned int unused, err;

+ mutex_lock(&vb->mtx_report);
+ /* We should always be able to add these buffers to an empty queue. */
+ err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN);
+
+ /*
+ * In the extremely unlikely case that something has occurred and we
+ * are able to trigger an error we will simply display a warning
+ * and exit without actually processing the pages.
+ */
+ if (WARN_ON_ONCE(err)) {
+ mutex_unlock(&vb->mtx_report);
+ return err;
+ }
+
+ virtqueue_kick(vq);
+
+ /* When host has read buffer, this completes via balloon_ack */
+ wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
+ mutex_unlock(&vb->mtx_report);
+
+ return 0;
+}
+
+static int virtballoon_free_hugepage_report(struct page_reporting_dev_info *hpr_dev_info,
+ struct scatterlist *sg, unsigned int nents)
+{
+ struct virtio_balloon *vb =
+ container_of(hpr_dev_info, struct virtio_balloon, hpr_dev_info);
+ struct virtqueue *vq = vb->reporting_vq;
+ unsigned int unused, err;
+
+ mutex_lock(&vb->mtx_report);
/* We should always be able to add these buffers to an empty queue. */
err = virtqueue_add_inbuf(vq, sg, nents, vb, GFP_NOWAIT | __GFP_NOWARN);

@@ -181,13 +217,16 @@ static int virtballoon_free_page_report(struct page_reporting_dev_info *pr_dev_i
* are able to trigger an error we will simply display a warning
* and exit without actually processing the pages.
*/
- if (WARN_ON_ONCE(err))
+ if (WARN_ON_ONCE(err)) {
+ mutex_unlock(&vb->mtx_report);
return err;
+ }

virtqueue_kick(vq);

/* When host has read buffer, this completes via balloon_ack */
wait_event(vb->acked, virtqueue_get_buf(vq, &unused));
+ mutex_unlock(&vb->mtx_report);

return 0;
}
@@ -984,9 +1023,11 @@ static int virtballoon_probe(struct virtio_device *vdev)
}

vb->pr_dev_info.report = virtballoon_free_page_report;
+ vb->hpr_dev_info.report = virtballoon_free_hugepage_report;
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
unsigned int capacity;

+ mutex_init(&vb->mtx_report);
capacity = virtqueue_get_vring_size(vb->reporting_vq);
if (capacity < PAGE_REPORTING_CAPACITY) {
err = -ENOSPC;
@@ -999,6 +1040,14 @@ static int virtballoon_probe(struct virtio_device *vdev)
err = page_reporting_register(&vb->pr_dev_info);
if (err)
goto out_unregister_oom;
+
+ vb->hpr_dev_info.mini_order = MAX_ORDER - 1;
+ vb->hpr_dev_info.batch_size = 16 * 1024 * 1024; /* 16M */
+ vb->hpr_dev_info.delay_jiffies = 2 * HZ; /* 2 seconds */
+ err = hugepage_reporting_register(&vb->hpr_dev_info);
+ if (err)
+ goto out_unregister_oom;
+
}

virtio_device_ready(vdev);
@@ -1051,8 +1100,10 @@ static void virtballoon_remove(struct virtio_device *vdev)
{
struct virtio_balloon *vb = vdev->priv;

- if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING))
+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) {
page_reporting_unregister(&vb->pr_dev_info);
+ hugepage_reporting_unregister(&vb->hpr_dev_info);
+ }
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
unregister_oom_notifier(&vb->oom_nb);
if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT))
--
2.18.2