[PATCH v1 2/2] Drivers: virtio: balloon: Report inflated memory

From: Alexander Atanasov
Date: Tue Aug 09 2022 - 05:54:35 EST


Update the value in page_alloc on balloon fill/leak.

Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: Wei Liu <wei.liu@xxxxxxxxxx>
Cc: Nadav Amit <namit@xxxxxxxxxx>

Signed-off-by: Alexander Atanasov <alexander.atanasov@xxxxxxxxxxxxx>
---
drivers/virtio/virtio_balloon.c | 13 +++++++++++++
1 file changed, 13 insertions(+)

Firts user, other balloons i will do if it is accepted to avoid too much emails.


diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
index b9737da6c4dd..e2693ffbd48b 100644
--- a/drivers/virtio/virtio_balloon.c
+++ b/drivers/virtio/virtio_balloon.c
@@ -208,6 +208,16 @@ static void set_page_pfns(struct virtio_balloon *vb,
page_to_balloon_pfn(page) + i);
}

+static void update_meminfo(struct virtio_balloon *vb)
+{
+ long inflated_kb = vb->num_pages << (VIRTIO_BALLOON_PFN_SHIFT - 10);
+
+ if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM))
+ inflated_kb = -inflated_kb;
+
+ atomic_long_set(&mem_balloon_inflated_kb, inflated_kb);
+}
+
static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num)
{
unsigned int num_allocated_pages;
@@ -250,6 +260,7 @@ static unsigned int fill_balloon(struct virtio_balloon *vb, size_t num)
}

num_allocated_pages = vb->num_pfns;
+ update_meminfo(vb);
/* Did we get any? */
if (vb->num_pfns != 0)
tell_host(vb, vb->inflate_vq);
@@ -296,6 +307,7 @@ static unsigned int leak_balloon(struct virtio_balloon *vb, size_t num)
}

num_freed_pages = vb->num_pfns;
+ update_meminfo(vb);
/*
* Note that if
* virtio_has_feature(vdev, VIRTIO_BALLOON_F_MUST_TELL_HOST);
@@ -1089,6 +1101,7 @@ static void virtballoon_remove(struct virtio_device *vdev)

kern_unmount(balloon_mnt);
#endif
+ update_meminfo(0);
kfree(vb);
}

--
2.31.1