* [Qemu-devel] [PATCH v4] virtio-balloon: free pbp more aggressively
@ 2019-07-25 15:18 Michael S. Tsirkin
2019-07-25 15:21 ` David Hildenbrand
0 siblings, 1 reply; 2+ messages in thread
From: Michael S. Tsirkin @ 2019-07-25 15:18 UTC (permalink / raw)
To: qemu-devel
Cc: Eduardo Habkost, David Hildenbrand, qemu-stable, Paolo Bonzini,
Igor Mammedov, Richard Henderson
Previous patches switched to a temporary pbp but that does not go far
enough: after device uses a buffer, guest is free to reuse it, so
tracking the page and freeing it later is wrong.
Free and reset the pbp after we push each element.
Fixes: ed48c59875b6 ("virtio-balloon: Safely handle BALLOON_PAGE_SIZE < host page size")
Cc: qemu-stable@nongnu.org #v4.0.0
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
changes from v2, v3:
v3 was same as v2, sent by mistake
v4 fixes a typo
hw/virtio/virtio-balloon.c | 37 ++++++++++++++++---------------------
diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index fe9664e42c..25de154307 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -41,22 +41,19 @@ typedef struct PartiallyBalloonedPage {
static void virtio_balloon_pbp_free(PartiallyBalloonedPage *pbp)
{
- if (!pbp) {
+ if (!pbp->bitmap) {
return;
}
g_free(pbp->bitmap);
- g_free(pbp);
+ pbp->bitmap = NULL;
}
-static PartiallyBalloonedPage *virtio_balloon_pbp_alloc(ram_addr_t base_gpa,
- long subpages)
+static void virtio_balloon_pbp_alloc(PartiallyBalloonedPage *pbp,
+ ram_addr_t base_gpa,
+ long subpages)
{
- PartiallyBalloonedPage *pbp = g_new0(PartiallyBalloonedPage, 1);
-
pbp->base_gpa = base_gpa;
pbp->bitmap = bitmap_new(subpages);
-
- return pbp;
}
static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp,
@@ -67,7 +64,7 @@ static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp,
static void balloon_inflate_page(VirtIOBalloon *balloon,
MemoryRegion *mr, hwaddr mr_offset,
- PartiallyBalloonedPage **pbp)
+ PartiallyBalloonedPage *pbp)
{
void *addr = memory_region_get_ram_ptr(mr) + mr_offset;
ram_addr_t rb_offset, rb_aligned_offset, base_gpa;
@@ -104,22 +101,21 @@ static void balloon_inflate_page(VirtIOBalloon *balloon,
base_gpa = memory_region_get_ram_addr(mr) + mr_offset -
(rb_offset - rb_aligned_offset);
- if (*pbp && !virtio_balloon_pbp_matches(*pbp, base_gpa)) {
+ if (pbp->bitmap && !virtio_balloon_pbp_matches(pbp, base_gpa)) {
/* We've partially ballooned part of a host page, but now
* we're trying to balloon part of a different one. Too hard,
* give up on the old partial page */
- virtio_balloon_pbp_free(*pbp);
- *pbp = NULL;
+ virtio_balloon_pbp_free(pbp);
}
- if (!*pbp) {
- *pbp = virtio_balloon_pbp_alloc(base_gpa, subpages);
+ if (!pbp->bitmap) {
+ virtio_balloon_pbp_alloc(pbp, base_gpa, subpages);
}
set_bit((rb_offset - rb_aligned_offset) / BALLOON_PAGE_SIZE,
- (*pbp)->bitmap);
+ pbp->bitmap);
- if (bitmap_full((*pbp)->bitmap, subpages)) {
+ if (bitmap_full(pbp->bitmap, subpages)) {
/* We've accumulated a full host page, we can actually discard
* it now */
@@ -127,8 +123,7 @@ static void balloon_inflate_page(VirtIOBalloon *balloon,
/* We ignore errors from ram_block_discard_range(), because it
* has already reported them, and failing to discard a balloon
* page is not fatal */
- virtio_balloon_pbp_free(*pbp);
- *pbp = NULL;
+ virtio_balloon_pbp_free(pbp);
}
}
@@ -328,13 +323,14 @@ static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
{
VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
- PartiallyBalloonedPage *pbp = NULL;
VirtQueueElement *elem;
MemoryRegionSection section;
for (;;) {
+ PartiallyBalloonedPage pbp = {};
size_t offset = 0;
uint32_t pfn;
+
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
if (!elem) {
break;
@@ -379,9 +375,8 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
virtqueue_push(vq, elem, offset);
virtio_notify(vdev, vq);
g_free(elem);
+ virtio_balloon_pbp_free(&pbp);
}
-
- virtio_balloon_pbp_free(pbp);
}
static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
--
MST
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH v4] virtio-balloon: free pbp more aggressively
2019-07-25 15:18 [Qemu-devel] [PATCH v4] virtio-balloon: free pbp more aggressively Michael S. Tsirkin
@ 2019-07-25 15:21 ` David Hildenbrand
0 siblings, 0 replies; 2+ messages in thread
From: David Hildenbrand @ 2019-07-25 15:21 UTC (permalink / raw)
To: Michael S. Tsirkin, qemu-devel
Cc: Eduardo Habkost, qemu-stable, Paolo Bonzini, Igor Mammedov,
Richard Henderson
On 25.07.19 17:18, Michael S. Tsirkin wrote:
> Previous patches switched to a temporary pbp but that does not go far
> enough: after device uses a buffer, guest is free to reuse it, so
> tracking the page and freeing it later is wrong.
>
> Free and reset the pbp after we push each element.
>
> Fixes: ed48c59875b6 ("virtio-balloon: Safely handle BALLOON_PAGE_SIZE < host page size")
> Cc: qemu-stable@nongnu.org #v4.0.0
> Cc: David Hildenbrand <david@redhat.com>
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
>
> changes from v2, v3:
> v3 was same as v2, sent by mistake
> v4 fixes a typo
>
> hw/virtio/virtio-balloon.c | 37 ++++++++++++++++---------------------
>
> diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
> index fe9664e42c..25de154307 100644
> --- a/hw/virtio/virtio-balloon.c
> +++ b/hw/virtio/virtio-balloon.c
> @@ -41,22 +41,19 @@ typedef struct PartiallyBalloonedPage {
>
> static void virtio_balloon_pbp_free(PartiallyBalloonedPage *pbp)
> {
> - if (!pbp) {
> + if (!pbp->bitmap) {
> return;
> }
> g_free(pbp->bitmap);
> - g_free(pbp);
> + pbp->bitmap = NULL;
> }
>
> -static PartiallyBalloonedPage *virtio_balloon_pbp_alloc(ram_addr_t base_gpa,
> - long subpages)
> +static void virtio_balloon_pbp_alloc(PartiallyBalloonedPage *pbp,
> + ram_addr_t base_gpa,
> + long subpages)
> {
> - PartiallyBalloonedPage *pbp = g_new0(PartiallyBalloonedPage, 1);
> -
> pbp->base_gpa = base_gpa;
> pbp->bitmap = bitmap_new(subpages);
> -
> - return pbp;
> }
>
> static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp,
> @@ -67,7 +64,7 @@ static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp,
>
> static void balloon_inflate_page(VirtIOBalloon *balloon,
> MemoryRegion *mr, hwaddr mr_offset,
> - PartiallyBalloonedPage **pbp)
> + PartiallyBalloonedPage *pbp)
> {
> void *addr = memory_region_get_ram_ptr(mr) + mr_offset;
> ram_addr_t rb_offset, rb_aligned_offset, base_gpa;
> @@ -104,22 +101,21 @@ static void balloon_inflate_page(VirtIOBalloon *balloon,
> base_gpa = memory_region_get_ram_addr(mr) + mr_offset -
> (rb_offset - rb_aligned_offset);
>
> - if (*pbp && !virtio_balloon_pbp_matches(*pbp, base_gpa)) {
> + if (pbp->bitmap && !virtio_balloon_pbp_matches(pbp, base_gpa)) {
> /* We've partially ballooned part of a host page, but now
> * we're trying to balloon part of a different one. Too hard,
> * give up on the old partial page */
> - virtio_balloon_pbp_free(*pbp);
> - *pbp = NULL;
> + virtio_balloon_pbp_free(pbp);
> }
>
> - if (!*pbp) {
> - *pbp = virtio_balloon_pbp_alloc(base_gpa, subpages);
> + if (!pbp->bitmap) {
> + virtio_balloon_pbp_alloc(pbp, base_gpa, subpages);
> }
>
> set_bit((rb_offset - rb_aligned_offset) / BALLOON_PAGE_SIZE,
> - (*pbp)->bitmap);
> + pbp->bitmap);
>
> - if (bitmap_full((*pbp)->bitmap, subpages)) {
> + if (bitmap_full(pbp->bitmap, subpages)) {
> /* We've accumulated a full host page, we can actually discard
> * it now */
>
> @@ -127,8 +123,7 @@ static void balloon_inflate_page(VirtIOBalloon *balloon,
> /* We ignore errors from ram_block_discard_range(), because it
> * has already reported them, and failing to discard a balloon
> * page is not fatal */
> - virtio_balloon_pbp_free(*pbp);
> - *pbp = NULL;
> + virtio_balloon_pbp_free(pbp);
> }
> }
>
> @@ -328,13 +323,14 @@ static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
> static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
> {
> VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
> - PartiallyBalloonedPage *pbp = NULL;
> VirtQueueElement *elem;
> MemoryRegionSection section;
>
> for (;;) {
> + PartiallyBalloonedPage pbp = {};
> size_t offset = 0;
> uint32_t pfn;
> +
> elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
> if (!elem) {
> break;
> @@ -379,9 +375,8 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
> virtqueue_push(vq, elem, offset);
> virtio_notify(vdev, vq);
> g_free(elem);
> + virtio_balloon_pbp_free(&pbp);
> }
> -
> - virtio_balloon_pbp_free(pbp);
> }
>
> static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
>
Works with hugetlbfs on x86 and LGTM:
Reviewed-by: David Hildenbrand <david@redhat.com>
--
Thanks,
David / dhildenb
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-07-25 15:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-07-25 15:18 [Qemu-devel] [PATCH v4] virtio-balloon: free pbp more aggressively Michael S. Tsirkin
2019-07-25 15:21 ` David Hildenbrand
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).