[Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored

Roger Pau Monne posted 1 patch 5 years, 1 month ago
Test docker-clang@ubuntu passed
Test asan passed
Test checkpatch passed
Test docker-mingw@fedora passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20190318154545.13974-1-roger.pau@citrix.com
There is a newer version of this series
hw/i386/xen/xen-mapcache.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
[Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
Posted by Roger Pau Monne 5 years, 1 month ago
Or if it's not possible to honor the hinted address an error is returned
instead. This makes it easier to spot the actual failure, instead of
failing later on when the caller of xen_remap_bucket realizes the
mapping has not been created at the requested address.

Also note that at least on FreeBSD using MAP_FIXED will cause mmap to
try harder to honor the passed address.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Anthony Perard <anthony.perard@citrix.com>
Cc: Paul Durrant <paul.durrant@citrix.com>
Cc: Igor Druzhinin <igor.druzhinin@citrix.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Cc: xen-devel@lists.xenproject.org
---
Changes since v1:
 - Use MAP_FIXED for the dummy mmap call also if a specific virtual
   address is requested.
---
 hw/i386/xen/xen-mapcache.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
index 349f72d00c..23de5517db 100644
--- a/hw/i386/xen/xen-mapcache.c
+++ b/hw/i386/xen/xen-mapcache.c
@@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
     if (!dummy) {
+        /*
+         * If the caller has requested the mapping at a specific address use
+         * MAP_FIXED to make sure it's honored.
+         */
         vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr,
-                                           PROT_READ | PROT_WRITE, 0,
+                                           PROT_READ | PROT_WRITE,
+                                           vaddr ? MAP_FIXED : 0,
                                            nb_pfn, pfns, err);
         if (vaddr_base == NULL) {
             perror("xenforeignmemory_map2");
@@ -198,7 +203,8 @@ static void xen_remap_bucket(MapCacheEntry *entry,
          * mapping immediately due to certain circumstances (i.e. on resume now)
          */
         vaddr_base = mmap(vaddr, size, PROT_READ | PROT_WRITE,
-                          MAP_ANON | MAP_SHARED, -1, 0);
+                          MAP_ANON | MAP_SHARED | (vaddr ? MAP_FIXED : 0),
+                          -1, 0);
         if (vaddr_base == MAP_FAILED) {
             perror("mmap");
             exit(-1);
-- 
2.17.2 (Apple Git-113)


Re: [Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
Posted by Paul Durrant 5 years, 1 month ago
> -----Original Message-----
> From: Roger Pau Monne [mailto:roger.pau@citrix.com]
> Sent: 18 March 2019 15:46
> To: qemu-devel@nongnu.org
> Cc: Roger Pau Monne <roger.pau@citrix.com>; Stefano Stabellini <sstabellini@kernel.org>; Anthony
> Perard <anthony.perard@citrix.com>; Paul Durrant <Paul.Durrant@citrix.com>; Igor Druzhinin
> <igor.druzhinin@citrix.com>; Paolo Bonzini <pbonzini@redhat.com>; Richard Henderson <rth@twiddle.net>;
> Eduardo Habkost <ehabkost@redhat.com>; Michael S. Tsirkin <mst@redhat.com>; Marcel Apfelbaum
> <marcel.apfelbaum@gmail.com>; xen-devel@lists.xenproject.org
> Subject: [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
> 
> Or if it's not possible to honor the hinted address an error is returned
> instead. This makes it easier to spot the actual failure, instead of
> failing later on when the caller of xen_remap_bucket realizes the
> mapping has not been created at the requested address.
> 
> Also note that at least on FreeBSD using MAP_FIXED will cause mmap to
> try harder to honor the passed address.
> 
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>

Reviewed-by: Paul Durrant <paul.durrant@citrix.com>

> ---
> Cc: Stefano Stabellini <sstabellini@kernel.org>
> Cc: Anthony Perard <anthony.perard@citrix.com>
> Cc: Paul Durrant <paul.durrant@citrix.com>
> Cc: Igor Druzhinin <igor.druzhinin@citrix.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Richard Henderson <rth@twiddle.net>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
> Cc: xen-devel@lists.xenproject.org
> ---
> Changes since v1:
>  - Use MAP_FIXED for the dummy mmap call also if a specific virtual
>    address is requested.
> ---
>  hw/i386/xen/xen-mapcache.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
> index 349f72d00c..23de5517db 100644
> --- a/hw/i386/xen/xen-mapcache.c
> +++ b/hw/i386/xen/xen-mapcache.c
> @@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>      }
> 
>      if (!dummy) {
> +        /*
> +         * If the caller has requested the mapping at a specific address use
> +         * MAP_FIXED to make sure it's honored.
> +         */
>          vaddr_base = xenforeignmemory_map2(xen_fmem, xen_domid, vaddr,
> -                                           PROT_READ | PROT_WRITE, 0,
> +                                           PROT_READ | PROT_WRITE,
> +                                           vaddr ? MAP_FIXED : 0,
>                                             nb_pfn, pfns, err);
>          if (vaddr_base == NULL) {
>              perror("xenforeignmemory_map2");
> @@ -198,7 +203,8 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>           * mapping immediately due to certain circumstances (i.e. on resume now)
>           */
>          vaddr_base = mmap(vaddr, size, PROT_READ | PROT_WRITE,
> -                          MAP_ANON | MAP_SHARED, -1, 0);
> +                          MAP_ANON | MAP_SHARED | (vaddr ? MAP_FIXED : 0),
> +                          -1, 0);
>          if (vaddr_base == MAP_FAILED) {
>              perror("mmap");
>              exit(-1);
> --
> 2.17.2 (Apple Git-113)

Re: [Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
Posted by Igor Druzhinin 5 years, 1 month ago
On 18/03/2019 15:45, Roger Pau Monne wrote:
> Or if it's not possible to honor the hinted address an error is returned
> instead. This makes it easier to spot the actual failure, instead of
> failing later on when the caller of xen_remap_bucket realizes the
> mapping has not been created at the requested address.
> 
> Also note that at least on FreeBSD using MAP_FIXED will cause mmap to
> try harder to honor the passed address.
> 
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> ---
> Cc: Stefano Stabellini <sstabellini@kernel.org>
> Cc: Anthony Perard <anthony.perard@citrix.com>
> Cc: Paul Durrant <paul.durrant@citrix.com>
> Cc: Igor Druzhinin <igor.druzhinin@citrix.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Richard Henderson <rth@twiddle.net>
> Cc: Eduardo Habkost <ehabkost@redhat.com>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
> Cc: xen-devel@lists.xenproject.org
> ---
> Changes since v1:
>  - Use MAP_FIXED for the dummy mmap call also if a specific virtual
>    address is requested.
> ---
>  hw/i386/xen/xen-mapcache.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
> index 349f72d00c..23de5517db 100644
> --- a/hw/i386/xen/xen-mapcache.c
> +++ b/hw/i386/xen/xen-mapcache.c
> @@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>      }
>  
>      if (!dummy) {
> +        /*
> +         * If the caller has requested the mapping at a specific address use
> +         * MAP_FIXED to make sure it's honored.
> +         */

Since the comment now applied to both invocation - could it be moved
outside the if statement then?

Igor

Re: [Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
Posted by Roger Pau Monné 5 years, 1 month ago
On Mon, Mar 18, 2019 at 03:48:59PM +0000, Igor Druzhinin wrote:
> On 18/03/2019 15:45, Roger Pau Monne wrote:
> > Or if it's not possible to honor the hinted address an error is returned
> > instead. This makes it easier to spot the actual failure, instead of
> > failing later on when the caller of xen_remap_bucket realizes the
> > mapping has not been created at the requested address.
> > 
> > Also note that at least on FreeBSD using MAP_FIXED will cause mmap to
> > try harder to honor the passed address.
> > 
> > Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> > ---
> > Cc: Stefano Stabellini <sstabellini@kernel.org>
> > Cc: Anthony Perard <anthony.perard@citrix.com>
> > Cc: Paul Durrant <paul.durrant@citrix.com>
> > Cc: Igor Druzhinin <igor.druzhinin@citrix.com>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Richard Henderson <rth@twiddle.net>
> > Cc: Eduardo Habkost <ehabkost@redhat.com>
> > Cc: "Michael S. Tsirkin" <mst@redhat.com>
> > Cc: Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
> > Cc: xen-devel@lists.xenproject.org
> > ---
> > Changes since v1:
> >  - Use MAP_FIXED for the dummy mmap call also if a specific virtual
> >    address is requested.
> > ---
> >  hw/i386/xen/xen-mapcache.c | 10 ++++++++--
> >  1 file changed, 8 insertions(+), 2 deletions(-)
> > 
> > diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
> > index 349f72d00c..23de5517db 100644
> > --- a/hw/i386/xen/xen-mapcache.c
> > +++ b/hw/i386/xen/xen-mapcache.c
> > @@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
> >      }
> >  
> >      if (!dummy) {
> > +        /*
> > +         * If the caller has requested the mapping at a specific address use
> > +         * MAP_FIXED to make sure it's honored.
> > +         */
> 
> Since the comment now applied to both invocation - could it be moved
> outside the if statement then?

I felt that was too far from the actual call, but I can move it if
there's consensus.

Thanks, Roger.

Re: [Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
Posted by Anthony PERARD 5 years, 1 month ago
On Mon, Mar 18, 2019 at 03:48:59PM +0000, Igor Druzhinin wrote:
> On 18/03/2019 15:45, Roger Pau Monne wrote:
> > diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
> > index 349f72d00c..23de5517db 100644
> > --- a/hw/i386/xen/xen-mapcache.c
> > +++ b/hw/i386/xen/xen-mapcache.c
> > @@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
> >      }
> >  
> >      if (!dummy) {
> > +        /*
> > +         * If the caller has requested the mapping at a specific address use
> > +         * MAP_FIXED to make sure it's honored.
> > +         */
> 
> Since the comment now applied to both invocation - could it be moved
> outside the if statement then?

That sounds good to me.

-- 
Anthony PERARD

Re: [Qemu-devel] [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
Posted by Paul Durrant 5 years, 1 month ago
> -----Original Message-----
> From: Anthony PERARD [mailto:anthony.perard@citrix.com]
> Sent: 18 March 2019 16:35
> To: Igor Druzhinin <igor.druzhinin@citrix.com>
> Cc: Roger Pau Monne <roger.pau@citrix.com>; qemu-devel@nongnu.org; Stefano Stabellini
> <sstabellini@kernel.org>; Paul Durrant <Paul.Durrant@citrix.com>; Paolo Bonzini <pbonzini@redhat.com>;
> Richard Henderson <rth@twiddle.net>; Eduardo Habkost <ehabkost@redhat.com>; Michael S. Tsirkin
> <mst@redhat.com>; Marcel Apfelbaum <marcel.apfelbaum@gmail.com>; xen-devel@lists.xenproject.org
> Subject: Re: [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored
> 
> On Mon, Mar 18, 2019 at 03:48:59PM +0000, Igor Druzhinin wrote:
> > On 18/03/2019 15:45, Roger Pau Monne wrote:
> > > diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
> > > index 349f72d00c..23de5517db 100644
> > > --- a/hw/i386/xen/xen-mapcache.c
> > > +++ b/hw/i386/xen/xen-mapcache.c
> > > @@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
> > >      }
> > >
> > >      if (!dummy) {
> > > +        /*
> > > +         * If the caller has requested the mapping at a specific address use
> > > +         * MAP_FIXED to make sure it's honored.
> > > +         */
> >
> > Since the comment now applied to both invocation - could it be moved
> > outside the if statement then?
> 
> That sounds good to me.

I have no problem and my R-b still stands.

  Paul

> 
> --
> Anthony PERARD