[RFC,3/5] virtio-balloon: Rework ballon_page() interface

Message ID 20181012032431.32693-4-david@gibson.dropbear.id.au
State New
Headers show
Series
  • Improve balloon handling of pagesizes other than 4kiB
Related show

Commit Message

David Gibson Oct. 12, 2018, 3:24 a.m.
This replaces the balloon_page() internal interface with
ballon_inflate_page(), with a slightly different interface.  The new
interface will make future alterations simpler.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 hw/virtio/virtio-balloon.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

Comments

David Hildenbrand Oct. 12, 2018, 7:46 a.m. | #1
On 12/10/2018 05:24, David Gibson wrote:
> This replaces the balloon_page() internal interface with
> ballon_inflate_page(), with a slightly different interface.  The new
> interface will make future alterations simpler.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  hw/virtio/virtio-balloon.c | 17 +++++++----------
>  1 file changed, 7 insertions(+), 10 deletions(-)
> 
> diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
> index e8611aab0e..7229afad6e 100644
> --- a/hw/virtio/virtio-balloon.c
> +++ b/hw/virtio/virtio-balloon.c
> @@ -33,11 +33,11 @@
>  
>  #define BALLOON_PAGE_SIZE  (1 << VIRTIO_BALLOON_PFN_SHIFT)
>  
> -static void balloon_page(void *addr, int deflate)
> +static void balloon_inflate_page(VirtIOBalloon *balloon,
> +                                 MemoryRegion *mr, hwaddr offset)
>  {
> -    if (!qemu_balloon_is_inhibited() && !deflate) {
> -        qemu_madvise(addr, BALLOON_PAGE_SIZE, QEMU_MADV_DONTNEED);
> -    }
> +    void *addr = memory_region_get_ram_ptr(mr) + offset;

I prefer an empty line here

> +    qemu_madvise(addr, BALLOON_PAGE_SIZE, QEMU_MADV_DONTNEED);
>  }
>  
>  static const char *balloon_stat_names[] = {
> @@ -222,7 +222,6 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
>  
>          while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
>              hwaddr pa;
> -            hwaddr addr;
>              int p = virtio_ldl_p(vdev, &pfn);
>  
>              pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT;
> @@ -244,11 +243,9 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
>  
>              trace_virtio_balloon_handle_output(memory_region_name(section.mr),
>                                                 pa);
> -            /* Using memory_region_get_ram_ptr is bending the rules a bit, but
> -               should be OK because we only want a single page.  */
> -            addr = section.offset_within_region;
> -            balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
> -                         !!(vq == s->dvq));
> +            if (!qemu_balloon_is_inhibited() && vq != s->dvq) {
> +                balloon_inflate_page(s, section.mr, section.offset_within_region);
> +            }
>              memory_region_unref(section.mr);
>          }
>  
> 

Reviewed-by: David Hildenbrand <david@redhat.com>
David Gibson Oct. 13, 2018, 6:29 a.m. | #2
On Fri, Oct 12, 2018 at 09:46:16AM +0200, David Hildenbrand wrote:
> On 12/10/2018 05:24, David Gibson wrote:
> > This replaces the balloon_page() internal interface with
> > ballon_inflate_page(), with a slightly different interface.  The new
> > interface will make future alterations simpler.
> > 
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  hw/virtio/virtio-balloon.c | 17 +++++++----------
> >  1 file changed, 7 insertions(+), 10 deletions(-)
> > 
> > diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
> > index e8611aab0e..7229afad6e 100644
> > --- a/hw/virtio/virtio-balloon.c
> > +++ b/hw/virtio/virtio-balloon.c
> > @@ -33,11 +33,11 @@
> >  
> >  #define BALLOON_PAGE_SIZE  (1 << VIRTIO_BALLOON_PFN_SHIFT)
> >  
> > -static void balloon_page(void *addr, int deflate)
> > +static void balloon_inflate_page(VirtIOBalloon *balloon,
> > +                                 MemoryRegion *mr, hwaddr offset)
> >  {
> > -    if (!qemu_balloon_is_inhibited() && !deflate) {
> > -        qemu_madvise(addr, BALLOON_PAGE_SIZE, QEMU_MADV_DONTNEED);
> > -    }
> > +    void *addr = memory_region_get_ram_ptr(mr) + offset;
> 
> I prefer an empty line here

Ok.

> > +    qemu_madvise(addr, BALLOON_PAGE_SIZE, QEMU_MADV_DONTNEED);
> >  }
> >  
> >  static const char *balloon_stat_names[] = {
> > @@ -222,7 +222,6 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
> >  
> >          while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
> >              hwaddr pa;
> > -            hwaddr addr;
> >              int p = virtio_ldl_p(vdev, &pfn);
> >  
> >              pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT;
> > @@ -244,11 +243,9 @@ static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
> >  
> >              trace_virtio_balloon_handle_output(memory_region_name(section.mr),
> >                                                 pa);
> > -            /* Using memory_region_get_ram_ptr is bending the rules a bit, but
> > -               should be OK because we only want a single page.  */
> > -            addr = section.offset_within_region;
> > -            balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
> > -                         !!(vq == s->dvq));
> > +            if (!qemu_balloon_is_inhibited() && vq != s->dvq) {
> > +                balloon_inflate_page(s, section.mr, section.offset_within_region);
> > +            }
> >              memory_region_unref(section.mr);
> >          }
> >  
> > 
> 
> Reviewed-by: David Hildenbrand <david@redhat.com>
>

Patch

diff --git a/hw/virtio/virtio-balloon.c b/hw/virtio/virtio-balloon.c
index e8611aab0e..7229afad6e 100644
--- a/hw/virtio/virtio-balloon.c
+++ b/hw/virtio/virtio-balloon.c
@@ -33,11 +33,11 @@ 
 
 #define BALLOON_PAGE_SIZE  (1 << VIRTIO_BALLOON_PFN_SHIFT)
 
-static void balloon_page(void *addr, int deflate)
+static void balloon_inflate_page(VirtIOBalloon *balloon,
+                                 MemoryRegion *mr, hwaddr offset)
 {
-    if (!qemu_balloon_is_inhibited() && !deflate) {
-        qemu_madvise(addr, BALLOON_PAGE_SIZE, QEMU_MADV_DONTNEED);
-    }
+    void *addr = memory_region_get_ram_ptr(mr) + offset;
+    qemu_madvise(addr, BALLOON_PAGE_SIZE, QEMU_MADV_DONTNEED);
 }
 
 static const char *balloon_stat_names[] = {
@@ -222,7 +222,6 @@  static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 
         while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
             hwaddr pa;
-            hwaddr addr;
             int p = virtio_ldl_p(vdev, &pfn);
 
             pa = (hwaddr) p << VIRTIO_BALLOON_PFN_SHIFT;
@@ -244,11 +243,9 @@  static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 
             trace_virtio_balloon_handle_output(memory_region_name(section.mr),
                                                pa);
-            /* Using memory_region_get_ram_ptr is bending the rules a bit, but
-               should be OK because we only want a single page.  */
-            addr = section.offset_within_region;
-            balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
-                         !!(vq == s->dvq));
+            if (!qemu_balloon_is_inhibited() && vq != s->dvq) {
+                balloon_inflate_page(s, section.mr, section.offset_within_region);
+            }
             memory_region_unref(section.mr);
         }