virtio-balloon: fixes
v3 cover virtio-balloon: fixes
As we discovered yet another issue with current PBP code, we decided to use a local PBP only, limited to one series of inflation requests. This series supersedes:
- [PATCH-for-4.1 v2 0/3] virtio-balloon: fixes for PartialBalloonedPage
- [PATCH v1 0/3] virtio-balloon: PartialBalloonedPage rework
Patch #1 is a fix for a wrong sign extension (MST brought this up but wasn't sure if it is broen - I think it is indeed broken). Patch #2 fixed QEMU segfaults. Patch #3 and #4 are cleanups that make follow-up fixes easier. Patch #5 avoids using RAMBlock addresses as tokens and patch #6 fixes all kinds of issues related to using a global PBP.
We want to have all patches in 4.1 and backport them to 4.0. Patch #1 needs backports to basically all QEMU releases with virtio-balloon.
Did a quick sanity test, hopefully no other BUG sneeked in. Will do some more testing.
David Hildenbrand (6):
virtio-balloon: Fix wrong sign extension of PFNs
virtio-balloon: Fix QEMU crashes on pagesize > BALLOON_PAGE_SIZE
virtio-balloon: Simplify deflate with pbp
virtio-balloon: Better names for offset variables in inflate/deflate
code
virtio-balloon: Rework pbp tracking data
virtio-balloon: Use temporary PBP only
hw/virtio/virtio-balloon.c | 122 +++++++++++++++-------------- include/hw/virtio/virtio-balloon.h | 3 - 2 files changed, 62 insertions(+), 63 deletions(-)
Signature · 1 line
2.21.0
PATCH v3 5/6 virtio-balloon: Rework pbp tracking data
Using the address of a RAMBlock to test for a matching pbp is not really safe. Instead, let's use the guest physical address of the base page along with the page size (via the number of subpages).
Also, let's allocate the bitmap separately. This makes the code easier to read and maintain - we can reuse bitmap_new().
Prepare the code to move the PBP out of the device.
host page size")
with inflates & deflates")
--- hw/virtio/virtio-balloon.c | 69 +++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 23 deletions(-)
hw/virtio/virtio-balloon.c +46 -23
@@ -35,16 +35,44 @@#define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)struct PartiallyBalloonedPage { -RAMBlock *rb; -ram_addr_t base; -unsigned long bitmap[]; +ram_addr_t base_gpa; +long subpages; +unsigned long *bitmap; }; +static void virtio_balloon_pbp_free (PartiallyBalloonedPage *pbp) +{ +if (!pbp) { +return ; + } +g_free (pbp->bitmap); +g_free (pbp); +} + +static PartiallyBalloonedPage *virtio_balloon_pbp_alloc (ram_addr_t base_gpa, +long subpages) +{ +PartiallyBalloonedPage *pbp =g_new0 (PartiallyBalloonedPage,1 ); + + pbp->base_gpa = base_gpa; + pbp->subpages = subpages; + pbp->bitmap =bitmap_new (subpages); + +return pbp; +} + +static bool virtio_balloon_pbp_matches (PartiallyBalloonedPage *pbp, +ram_addr_t base_gpa,long subpages) +{ +return pbp->subpages == subpages && pbp->base_gpa == base_gpa; +} +static void balloon_inflate_page (VirtIOBalloon *balloon,MemoryRegion *mr,hwaddr mr_offset) {void *addr =memory_region_get_ram_ptr (mr) + mr_offset; -ram_addr_t rb_offset, rb_aligned_offset; +ram_addr_t rb_offset, rb_aligned_offset, base_gpa; +PartiallyBalloonedPage **pbp = &balloon->pbp;RAMBlock *rb;size_t rb_page_size;int subpages;
@@ -75,39 +103,34 @@ static void balloon_inflate_page(VirtIOBalloon *balloon, rb_aligned_offset =QEMU_ALIGN_DOWN (rb_offset, rb_page_size); subpages = rb_page_size /BALLOON_PAGE_SIZE ; + base_gpa =memory_region_get_ram_addr (mr) + mr_offset - + (rb_offset - rb_aligned_offset); -if (balloon->pbp - && (rb != balloon->pbp->rb - || rb_aligned_offset != balloon->pbp->base)) { +if (*pbp && !virtio_balloon_pbp_matches (*pbp, base_gpa, subpages)) {/* 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 */ -g_free (balloon->pbp); - balloon->pbp =NULL ; +virtio_balloon_pbp_free (*pbp); + *pbp =NULL ; } -if (!balloon->pbp) { -/* Starting on a new host page */ -size_t bitlen =BITS_TO_LONGS (subpages) *sizeof (unsigned long ); - balloon->pbp =g_malloc0 (sizeof (PartiallyBalloonedPage) + bitlen); - balloon->pbp->rb = rb; - balloon->pbp->base = rb_aligned_offset; +if (!*pbp) { + *pbp =virtio_balloon_pbp_alloc (base_gpa, subpages); } -set_bit ((rb_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , - balloon->pbp->bitmap); +set_bit ((rb_offset - rb_aligned_offset) /BALLOON_PAGE_SIZE , + (*pbp)->bitmap); -if (bitmap_full (balloon->pbp->bitmap, subpages)) { +if (bitmap_full ((*pbp)->bitmap, subpages)) {/* We've accumulated a full host page, we can actually discard * it now */ -ram_block_discard_range (rb, balloon->pbp->base, rb_page_size); +ram_block_discard_range (rb, rb_aligned_offset, rb_page_size);/* We ignore errors from ram_block_discard_range(), because it * has already reported them, and failing to discard a balloon * page is not fatal */ - -g_free (balloon->pbp); - balloon->pbp =NULL ; +virtio_balloon_pbp_free (*pbp); + *pbp =NULL ; } }
@@ -128,7 +151,7 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,if (balloon->pbp) {/* Let's play safe and always reset the pbp on deflation requests. */ -g_free (balloon->pbp); +virtio_balloon_pbp_free (balloon->pbp); balloon->pbp =NULL ; }
Signature · 1 line
2.21.0
PATCH v3 1/6 virtio-balloon: Fix wrong sign extension of PFNs
In reply to David HildenbrandIf we directly cast from int to uint64_t, we will first sign-extend to an int64_t, which is wrong. We actually want to treat the PFNs like unsigned values.
As far as I can see, this dates back to the initial virtio-balloon commit, but wasn't triggered as fairly big guests would be required.
--- hw/virtio/virtio-balloon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/virtio-balloon.c +1 -1
@@ -343,8 +343,8 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) }while (iov_to_buf (elem->out_sg, elem->out_num, offset, &pfn,4 ) ==4 ) { +unsigned int p =virtio_ldl_p (vdev, &pfn);hwaddr pa; -int p =virtio_ldl_p (vdev, &pfn); pa = (hwaddr ) p <<VIRTIO_BALLOON_PFN_SHIFT ; offset +=4 ;
Signature · 1 line
2.21.0
PATCH v3 3/6 virtio-balloon: Simplify deflate with pbp
In reply to David HildenbrandLet's simplify this - the case we are optimizing for is very hard to trigger and not worth the effort. If we're switching from inflation to deflation, let's reset the pbp.
--- hw/virtio/virtio-balloon.c | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-)
hw/virtio/virtio-balloon.c +5 -21
@@ -117,7 +117,7 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,void *addr =memory_region_get_ram_ptr (mr) + offset;RAMBlock *rb;size_t rb_page_size; -ram_addr_t ram_offset, host_page_base; +ram_addr_t ram_offset;void *host_addr;int ret;
@@ -125,27 +125,11 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,* host address? */ rb =qemu_ram_block_from_host (addr, false, &ram_offset); rb_page_size =qemu_ram_pagesize (rb); - host_page_base = ram_offset & ~(rb_page_size -1 ); - -if (balloon->pbp - && rb == balloon->pbp->rb - && host_page_base == balloon->pbp->base) { -int subpages = rb_page_size /BALLOON_PAGE_SIZE ; -/* -* This means the guest has asked to discard some of the 4kiB -* subpages of a host page, but then changed its mind and -* asked to keep them after all. It's exceedingly unlikely -* for a guest to do this in practice, but handle it anyway, -* since getting it wrong could mean discarding memory the -* guest is still using. */ -clear_bit ((ram_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , - balloon->pbp->bitmap); - -if (bitmap_empty (balloon->pbp->bitmap, subpages)) { -g_free (balloon->pbp); - balloon->pbp =NULL ; - } +if (balloon->pbp) { +/* Let's play safe and always reset the pbp on deflation requests. */ +g_free (balloon->pbp); + balloon->pbp =NULL ; } host_addr = (void *)((uintptr_t )addr & ~(rb_page_size -1 ));
Signature · 1 line
2.21.0
PATCH v3 2/6 virtio-balloon: Fix QEMU crashes on pagesize > BALLOON_PAGE_SIZE
In reply to David HildenbrandWe are using the wrong functions to set/clear bits, effectively touching multiple bits, writing out of range of the bitmap, resulting in memory corruptions. We have to use set_bit()/clear_bit() instead.
Can easily be reproduced by starting a qemu guest on hugetlbfs memory, inflating the balloon. QEMU crashes. This never could have worked properly - especially, also pages would have been discarded when the first sub-page would be inflated (the whole bitmap would be set).
While testing I realized, that on hugetlbfs it is pretty much impossible to discard a page - the guest just frees the 4k sub-pages in random order most of the time. I was only able to discard a hugepage a handful of times - so I hope that now works correctly.
host page size")
with inflates & deflates")
--- hw/virtio/virtio-balloon.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
hw/virtio/virtio-balloon.c +4 -6
@@ -94,9 +94,8 @@ static void balloon_inflate_page(VirtIOBalloon *balloon, balloon->pbp->base = host_page_base; } -bitmap_set (balloon->pbp->bitmap, - (ram_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , - subpages); +set_bit ((ram_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , + balloon->pbp->bitmap);if (bitmap_full (balloon->pbp->bitmap, subpages)) {/* We've accumulated a full host page, we can actually discard
@@ -140,9 +139,8 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,* for a guest to do this in practice, but handle it anyway, * since getting it wrong could mean discarding memory the * guest is still using. */ -bitmap_clear (balloon->pbp->bitmap, - (ram_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , - subpages); +clear_bit ((ram_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , + balloon->pbp->bitmap);if (bitmap_empty (balloon->pbp->bitmap, subpages)) {g_free (balloon->pbp);
Signature · 1 line
2.21.0
PATCH v3 4/6 virtio-balloon: Better names for offset variables in inflate/deflate code
In reply to David Hildenbrand"host_page_base" is really confusing, let's make this clearer, also rename the other offsets to indicate to which base they apply.
offset -> mr_offsetram_offset -> rb_offsethost_page_base -> rb_aligned_offset
While at it, use QEMU_ALIGN_DOWN() instead of a handcrafted computation and move the computation to the place where it is needed.
--- hw/virtio/virtio-balloon.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
hw/virtio/virtio-balloon.c +13 -13
@@ -41,24 +41,23 @@ struct PartiallyBalloonedPage { };static void balloon_inflate_page (VirtIOBalloon *balloon, -MemoryRegion *mr,hwaddr offset) +MemoryRegion *mr,hwaddr mr_offset) { -void *addr =memory_region_get_ram_ptr (mr) + offset; +void *addr =memory_region_get_ram_ptr (mr) + mr_offset; +ram_addr_t rb_offset, rb_aligned_offset;RAMBlock *rb;size_t rb_page_size;int subpages; -ram_addr_t ram_offset, host_page_base;/* XXX is there a better way to get to the RAMBlock than via a * host address? */ - rb =qemu_ram_block_from_host (addr, false, &ram_offset); + rb =qemu_ram_block_from_host (addr, false, &rb_offset); rb_page_size =qemu_ram_pagesize (rb); - host_page_base = ram_offset & ~(rb_page_size -1 );if (rb_page_size ==BALLOON_PAGE_SIZE ) {/* Easy case */ -ram_block_discard_range (rb, ram_offset, rb_page_size); +ram_block_discard_range (rb, rb_offset, rb_page_size);/* We ignore errors from ram_block_discard_range(), because it * has already reported them, and failing to discard a balloon * page is not fatal */
@@ -74,11 +73,12 @@ static void balloon_inflate_page(VirtIOBalloon *balloon,warn_report_once ("Balloon used with backing page size > 4kiB, this may not be reliable" ); + rb_aligned_offset =QEMU_ALIGN_DOWN (rb_offset, rb_page_size); subpages = rb_page_size /BALLOON_PAGE_SIZE ;if (balloon->pbp && (rb != balloon->pbp->rb - || host_page_base != balloon->pbp->base)) { + || rb_aligned_offset != balloon->pbp->base)) {/* 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 */
@@ -91,10 +91,10 @@ static void balloon_inflate_page(VirtIOBalloon *balloon,size_t bitlen =BITS_TO_LONGS (subpages) *sizeof (unsigned long ); balloon->pbp =g_malloc0 (sizeof (PartiallyBalloonedPage) + bitlen); balloon->pbp->rb = rb; - balloon->pbp->base = host_page_base; + balloon->pbp->base = rb_aligned_offset; } -set_bit ((ram_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , +set_bit ((rb_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , balloon->pbp->bitmap);if (bitmap_full (balloon->pbp->bitmap, subpages)) {
@@ -112,18 +112,18 @@ static void balloon_inflate_page(VirtIOBalloon *balloon, }static void balloon_deflate_page (VirtIOBalloon *balloon, -MemoryRegion *mr,hwaddr offset) +MemoryRegion *mr,hwaddr mr_offset) { -void *addr =memory_region_get_ram_ptr (mr) + offset; +void *addr =memory_region_get_ram_ptr (mr) + mr_offset; +ram_addr_t rb_offset;RAMBlock *rb;size_t rb_page_size; -ram_addr_t ram_offset;void *host_addr;int ret;/* XXX is there a better way to get to the RAMBlock than via a * host address? */ - rb =qemu_ram_block_from_host (addr, false, &ram_offset); + rb =qemu_ram_block_from_host (addr, false, &rb_offset); rb_page_size =qemu_ram_pagesize (rb);if (balloon->pbp) {
Signature · 1 line
2.21.0
PATCH v3 6/6 virtio-balloon: Use temporary PBP only
In reply to David HildenbrandWe still have multiple issues in the current code
- The PBP is not freed during unrealize()
- The PBP is not reset on device resets: After a reset, the PBP is stale.
- We are not indicating VIRTIO_BALLOON_F_MUST_TELL_HOST, therefore guests (esp. legacy guests) will reuse pages without deflating, turning the PBP stale. Adding that would require compat handling.
Instead, let's use the PBP only temporarily, when processing one bulk of inflation requests. This will keep guest_page_size > 4k working (with Linux guests). There is nothing to do for deflation requests anymore. The pbp is only used for a limited amount of time.
host page size")
--- hw/virtio/virtio-balloon.c | 21 +++++++++------------ include/hw/virtio/virtio-balloon.h | 3 --- 2 files changed, 9 insertions(+), 15 deletions(-)
hw/virtio/virtio-balloon.c +9 -12
@@ -34,11 +34,11 @@#define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT) -struct PartiallyBalloonedPage { +typedef struct PartiallyBalloonedPage {ram_addr_t base_gpa;long subpages;unsigned long *bitmap; -}; +}PartiallyBalloonedPage ;static void virtio_balloon_pbp_free (PartiallyBalloonedPage *pbp) {
@@ -68,11 +68,11 @@ static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp, }static void balloon_inflate_page (VirtIOBalloon *balloon, -MemoryRegion *mr,hwaddr mr_offset) +MemoryRegion *mr,hwaddr mr_offset, +PartiallyBalloonedPage **pbp) {void *addr =memory_region_get_ram_ptr (mr) + mr_offset;ram_addr_t rb_offset, rb_aligned_offset, base_gpa; -PartiallyBalloonedPage **pbp = &balloon->pbp;RAMBlock *rb;size_t rb_page_size;int subpages;
@@ -149,12 +149,6 @@ static void balloon_deflate_page(VirtIOBalloon *balloon, rb =qemu_ram_block_from_host (addr, false, &rb_offset); rb_page_size =qemu_ram_pagesize (rb); -if (balloon->pbp) { -/* Let's play safe and always reset the pbp on deflation requests. */ -virtio_balloon_pbp_free (balloon->pbp); - balloon->pbp =NULL ; - } - host_addr = (void *)((uintptr_t )addr & ~(rb_page_size -1 ));/* When a page is deflated, we hint the whole host page it lives
@@ -336,6 +330,7 @@ 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;
@@ -344,7 +339,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)uint32_t pfn; elem =virtqueue_pop (vq,sizeof (VirtQueueElement));if (!elem) { -return ; +break ; }while (iov_to_buf (elem->out_sg, elem->out_num, offset, &pfn,4 ) ==4 ) {
@@ -373,7 +368,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)if (!qemu_balloon_is_inhibited ()) {if (vq == s->ivq) {balloon_inflate_page (s, section.mr, - section.offset_within_region); + section.offset_within_region, &pbp); }else if (vq == s->dvq) {balloon_deflate_page (s, section.mr, section.offset_within_region); }else {
@@ -387,6 +382,8 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)virtio_notify (vdev, vq);g_free (elem); } + +virtio_balloon_pbp_free (pbp); }static void virtio_balloon_receive_stats (VirtIODevice *vdev,VirtQueue *vq)
include/hw/virtio/virtio-balloon.h +0 -3
@@ -33,8 +33,6 @@ typedef struct virtio_balloon_stat_modern {uint64_t val; } VirtIOBalloonStatModern; -typedef struct PartiallyBalloonedPage PartiallyBalloonedPage ; -enum virtio_balloon_free_page_report_status {FREE_PAGE_REPORT_S_STOP =0 ,FREE_PAGE_REPORT_S_REQUESTED =1 ,
@@ -70,7 +68,6 @@ typedef struct VirtIOBalloon {int64_t stats_last_update;int64_t stats_poll_interval;uint32_t host_features; -PartiallyBalloonedPage *pbp;bool qemu_4_0_config_size; } VirtIOBalloon;
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH-for-4.1 v3 1/6] virtio-balloon: Fix wrong sign extension of PFNs
In reply to David HildenbrandQuoted 10 lines · David Hildenbrand
If we directly cast from int to
uint64_t, we will first sign-extend to anint64_t, which is wrong. We actually want to treat the PFNs like unsigned values.As far as I can see, this dates back to the initial virtio-balloon commit, but wasn't triggered as fairly big guests would be required.
Reported-by Michael S. Tsirkin <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Quoted 18 lines
--- hw/virtio/virtio-balloon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/virtio/virtio-balloon.c +1
-1@@ -343,8 +343,8 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) }while (iov_to_buf (elem->out_sg, elem->out_num, offset, &pfn,4 ) ==4 ) { +unsigned int p =virtio_ldl_p (vdev, &pfn);hwaddr pa; -int p =virtio_ldl_p (vdev, &pfn); pa = (hwaddr ) p <<VIRTIO_BALLOON_PFN_SHIFT ; offset +=4 ;
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [Qemu-devel] [PATCH-for-4.1 v3 5/6] virtio-balloon: Rework pbp tracking data
In reply to David HildenbrandQuoted 50 lines · David Hildenbrand
Using the address of a RAMBlock to test for a matching pbp is not really safe. Instead, let's use the guest physical address of the base page along with the page size (via the number of subpages).
Also, let's allocate the bitmap separately. This makes the code easier to read and maintain - we can reuse
bitmap_new().Prepare the code to move the PBP out of the device.
Fixes ed48c59875b6 ("virtio-balloon: Safely handle BALLOON_PAGE_SIZE <host page size")
Fixes b27b32391404 ("virtio-balloon: Fix possible guest memory corruptionwith inflates & deflates")
Cc [email protected] #v4.0.0Signed-off-by David Hildenbrand <[email protected]>--- hw/virtio/virtio-balloon.c | 69 +++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 23 deletions(-)hw/virtio/virtio-balloon.c +18
-3@@ -35,16 +35,44 @@#define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)struct PartiallyBalloonedPage { -RAMBlock *rb; -ram_addr_t base; -unsigned long bitmap[]; +ram_addr_t base_gpa; +long subpages; +unsigned long *bitmap; }; +static void virtio_balloon_pbp_free (PartiallyBalloonedPage *pbp) +{ +if (!pbp) { +return ; + } +g_free (pbp->bitmap); +g_free (pbp); +} + +static PartiallyBalloonedPage *virtio_balloon_pbp_alloc (ram_addr_t base_gpa, +long subpages) +{ +PartiallyBalloonedPage *pbp =g_new0 (PartiallyBalloonedPage,1 ); + + pbp->base_gpa = base_gpa;
hw/virtio/virtio-balloon.c line 67
Quoted 10 lines
hw/virtio/virtio-balloon.c +10
-0+ pbp->subpages = subpages; + pbp->bitmap =bitmap_new (subpages); + +return pbp; +} + +static bool virtio_balloon_pbp_matches (PartiallyBalloonedPage *pbp, +ram_addr_t base_gpa,long subpages) +{ +return pbp->subpages == subpages && pbp->base_gpa == base_gpa;
I think the test on subpages is pointless here. You've (reasonably) rejected handling the edge case of a ramblock being removed and replugged - but the only thing this handles is an edge case of that edge case.
Otherwise, LGTM.
Quoted 74 lines
hw/virtio/virtio-balloon.c +18
-20+} +static void balloon_inflate_page (VirtIOBalloon *balloon,MemoryRegion *mr,hwaddr mr_offset) {void *addr =memory_region_get_ram_ptr (mr) + mr_offset; -ram_addr_t rb_offset, rb_aligned_offset; +ram_addr_t rb_offset, rb_aligned_offset, base_gpa; +PartiallyBalloonedPage **pbp = &balloon->pbp;RAMBlock *rb;size_t rb_page_size;int subpages;@@ -75,39 +103,34 @@ static void balloon_inflate_page(VirtIOBalloon *balloon, rb_aligned_offset =QEMU_ALIGN_DOWN (rb_offset, rb_page_size); subpages = rb_page_size /BALLOON_PAGE_SIZE ; + base_gpa =memory_region_get_ram_addr (mr) + mr_offset - + (rb_offset - rb_aligned_offset); -if (balloon->pbp - && (rb != balloon->pbp->rb - || rb_aligned_offset != balloon->pbp->base)) { +if (*pbp && !virtio_balloon_pbp_matches (*pbp, base_gpa, subpages)) {/* 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 */ -g_free (balloon->pbp); - balloon->pbp =NULL ; +virtio_balloon_pbp_free (*pbp); + *pbp =NULL ; } -if (!balloon->pbp) { -/* Starting on a new host page */ -size_t bitlen =BITS_TO_LONGS (subpages) *sizeof (unsigned long ); - balloon->pbp =g_malloc0 (sizeof (PartiallyBalloonedPage) + bitlen); - balloon->pbp->rb = rb; - balloon->pbp->base = rb_aligned_offset; +if (!*pbp) { + *pbp =virtio_balloon_pbp_alloc (base_gpa, subpages); } -set_bit ((rb_offset - balloon->pbp->base) /BALLOON_PAGE_SIZE , - balloon->pbp->bitmap); +set_bit ((rb_offset - rb_aligned_offset) /BALLOON_PAGE_SIZE , + (*pbp)->bitmap); -if (bitmap_full (balloon->pbp->bitmap, subpages)) { +if (bitmap_full ((*pbp)->bitmap, subpages)) {/* We've accumulated a full host page, we can actually discard * it now */ -ram_block_discard_range (rb, balloon->pbp->base, rb_page_size); +ram_block_discard_range (rb, rb_aligned_offset, rb_page_size);/* We ignore errors from ram_block_discard_range(), because it * has already reported them, and failing to discard a balloon * page is not fatal */ - -g_free (balloon->pbp); - balloon->pbp =NULL ; +virtio_balloon_pbp_free (*pbp); + *pbp =NULL ; } }@@ -128,7 +151,7 @@ static void balloon_deflate_page(VirtIOBalloon *balloon,if (balloon->pbp) {/* Let's play safe and always reset the pbp on deflation requests. */ -g_free (balloon->pbp); +virtio_balloon_pbp_free (balloon->pbp); balloon->pbp =NULL ; }
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [Qemu-devel] [PATCH-for-4.1 v3 6/6] virtio-balloon: Use temporary PBP only
In reply to David HildenbrandQuoted 12 lines · David Hildenbrand
We still have multiple issues in the current code
- The PBP is not freed duringunrealize()
- The PBP is not reset on device resets: After a reset, the PBP is stale.
- We are not indicatingVIRTIO_BALLOON_F_MUST_TELL_HOST, therefore guests (esp. legacy guests) will reuse pages without deflating, turning the PBP stale. Adding that would require compat handling.Instead, let's use the PBP only temporarily, when processing one bulk of inflation requests. This will keep
guest_page_size> 4k working (with Linux guests). There is nothing to do for deflation requests anymore. The pbp is only used for a limited amount of time.
Quoted 5 lines
Fixes ed48c59875b6 ("virtio-balloon: Safely handle BALLOON_PAGE_SIZE <host page size")
Cc [email protected] #v4.0.0Suggested-by Michael S. Tsirkin <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
Ah, nice idea.
Quoted 106 lines
--- hw/virtio/virtio-balloon.c | 21 +++++++++------------ include/hw/virtio/virtio-balloon.h | 3 --- 2 files changed, 9 insertions(+), 15 deletions(-)hw/virtio/virtio-balloon.c +9
-12@@ -34,11 +34,11 @@#define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT) -struct PartiallyBalloonedPage { +typedef struct PartiallyBalloonedPage {ram_addr_t base_gpa;long subpages;unsigned long *bitmap; -}; +}PartiallyBalloonedPage ;static void virtio_balloon_pbp_free (PartiallyBalloonedPage *pbp) {@@ -68,11 +68,11 @@ static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp, }static void balloon_inflate_page (VirtIOBalloon *balloon, -MemoryRegion *mr,hwaddr mr_offset) +MemoryRegion *mr,hwaddr mr_offset, +PartiallyBalloonedPage **pbp) {void *addr =memory_region_get_ram_ptr (mr) + mr_offset;ram_addr_t rb_offset, rb_aligned_offset, base_gpa; -PartiallyBalloonedPage **pbp = &balloon->pbp;RAMBlock *rb;size_t rb_page_size;int subpages;@@ -149,12 +149,6 @@ static void balloon_deflate_page(VirtIOBalloon *balloon, rb =qemu_ram_block_from_host (addr, false, &rb_offset); rb_page_size =qemu_ram_pagesize (rb); -if (balloon->pbp) { -/* Let's play safe and always reset the pbp on deflation requests. */ -virtio_balloon_pbp_free (balloon->pbp); - balloon->pbp =NULL ; - } - host_addr = (void *)((uintptr_t )addr & ~(rb_page_size -1 ));/* When a page is deflated, we hint the whole host page it lives @@ -336,6 +330,7 @@ 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;@@ -344,7 +339,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)uint32_t pfn; elem =virtqueue_pop (vq,sizeof (VirtQueueElement));if (!elem) { -return ; +break ; }while (iov_to_buf (elem->out_sg, elem->out_num, offset, &pfn,4 ) ==4 ) {@@ -373,7 +368,7 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)if (!qemu_balloon_is_inhibited ()) {if (vq == s->ivq) {balloon_inflate_page (s, section.mr, - section.offset_within_region); + section.offset_within_region, &pbp); }else if (vq == s->dvq) {balloon_deflate_page (s, section.mr, section.offset_within_region); }else {@@ -387,6 +382,8 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)virtio_notify (vdev, vq);g_free (elem); } + +virtio_balloon_pbp_free (pbp); }static void virtio_balloon_receive_stats (VirtIODevice *vdev,VirtQueue *vq)include/hw/virtio/virtio-balloon.h +0
-3@@ -33,8 +33,6 @@ typedef struct virtio_balloon_stat_modern {uint64_t val; } VirtIOBalloonStatModern; -typedef struct PartiallyBalloonedPage PartiallyBalloonedPage ; -enum virtio_balloon_free_page_report_status {FREE_PAGE_REPORT_S_STOP =0 ,FREE_PAGE_REPORT_S_REQUESTED =1 ,@@ -70,7 +68,6 @@ typedef struct VirtIOBalloon {int64_t stats_last_update;int64_t stats_poll_interval;uint32_t host_features; -PartiallyBalloonedPage *pbp;bool qemu_4_0_config_size; } VirtIOBalloon;
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [Qemu-devel] [PATCH-for-4.1 v3 5/6] virtio-balloon: Rework pbp tracking data
In reply to David GibsonQuoted 58 lines · David Gibson
Quoted 57 lines · David Hildenbrand
Using the address of a RAMBlock to test for a matching pbp is not really safe. Instead, let's use the guest physical address of the base page along with the page size (via the number of subpages).
Also, let's allocate the bitmap separately. This makes the code easier to read and maintain - we can reuse
bitmap_new().Prepare the code to move the PBP out of the device.
Fixes ed48c59875b6 ("virtio-balloon: Safely handle BALLOON_PAGE_SIZE <host page size")
Fixes b27b32391404 ("virtio-balloon: Fix possible guest memory corruptionwith inflates & deflates")
Cc [email protected] #v4.0.0Signed-off-by David Hildenbrand <[email protected]>--- hw/virtio/virtio-balloon.c | 69 +++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 23 deletions(-)hw/virtio/virtio-balloon.c +25
-3@@ -35,16 +35,44 @@#define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)struct PartiallyBalloonedPage { -RAMBlock *rb; -ram_addr_t base; -unsigned long bitmap[]; +ram_addr_t base_gpa; +long subpages; +unsigned long *bitmap; }; +static void virtio_balloon_pbp_free (PartiallyBalloonedPage *pbp) +{ +if (!pbp) { +return ; + } +g_free (pbp->bitmap); +g_free (pbp); +} + +static PartiallyBalloonedPage *virtio_balloon_pbp_alloc (ram_addr_t base_gpa, +long subpages) +{ +PartiallyBalloonedPage *pbp =g_new0 (PartiallyBalloonedPage,1 ); + + pbp->base_gpa = base_gpa; + pbp->subpages = subpages; + pbp->bitmap =bitmap_new (subpages); + +return pbp; +} + +static bool virtio_balloon_pbp_matches(PartiallyBalloonedPage *pbp,
Quoted 8 lines
hw/virtio/virtio-balloon.c line 67
Quoted 3 lines
hw/virtio/virtio-balloon.c +3
-0+ ram_addr_t base_gpa, long subpages) +{ +return pbp->subpages == subpages && pbp->base_gpa == base_gpa;I think the test on subpages is pointless here. You've (reasonably) rejected handling the edge case of a ramblock being removed and replugged - but the only thing this handles is an edge case of that edge case.
I think we have to leave it in place until patch #6. We could remove it after Patch #6 - replug cannot happen while processing the inflation requests.
Signature · 3 lines
Thanks, David / dhildenb