diff mbox series

[v3] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored

Message ID 20190318173731.14494-1-roger.pau@citrix.com (mailing list archive)
State New, archived
Headers show
Series [v3] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored | expand

Commit Message

Roger Pau Monne March 18, 2019, 5:37 p.m. UTC
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 v2:
 - Move comment position.

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(-)

Comments

Igor Druzhinin March 18, 2019, 5:39 p.m. UTC | #1
On 18/03/2019 17:37, 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>
> 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 v2:
>  - Move comment position.
> 
> 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..254759f776 100644
> --- a/hw/i386/xen/xen-mapcache.c
> +++ b/hw/i386/xen/xen-mapcache.c
> @@ -184,9 +184,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>          pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
>      }
>  
> +    /*
> +     * If the caller has requested the mapping at a specific address use
> +     * MAP_FIXED to make sure it's honored.
> +     */
>      if (!dummy) {
>          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);
> 

Reviewed-by: Igor Druzhinin <igor.druzhinin@cirtix.com>

Igor
Marek Marczykowski-Górecki March 18, 2019, 9:43 p.m. UTC | #2
On Mon, Mar 18, 2019 at 06:37:31PM +0100, Roger Pau Monne wrote:
> Or if it's not possible to honor the hinted address an error is returned
> instead. 

Just to be sure: MAP_FIXED will cause to map at specified address, even
if something is mapped there already. From mmap(2):

    If the memory region specified by addr and len overlaps pages of any
    existing mapping(s), then the overlapped part of the existing map‐
    ping(s) will be discarded.  If the specified address cannot be used,
    mmap() will fail.

> 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 v2:
>  - Move comment position.
> 
> 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..254759f776 100644
> --- a/hw/i386/xen/xen-mapcache.c
> +++ b/hw/i386/xen/xen-mapcache.c
> @@ -184,9 +184,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>          pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
>      }
>  
> +    /*
> +     * If the caller has requested the mapping at a specific address use
> +     * MAP_FIXED to make sure it's honored.
> +     */
>      if (!dummy) {
>          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);
Anthony PERARD March 19, 2019, 2:16 p.m. UTC | #3
On Mon, Mar 18, 2019 at 10:43:12PM +0100, Marek Marczykowski-Górecki wrote:
> On Mon, Mar 18, 2019 at 06:37:31PM +0100, Roger Pau Monne wrote:
> > Or if it's not possible to honor the hinted address an error is returned
> > instead. 
> 
> Just to be sure: MAP_FIXED will cause to map at specified address, even
> if something is mapped there already. From mmap(2):

That should be fine. We do want to replace an exiting mapping (which is
munmap before the mmap call), but it would have been nice to know when
something is already mapped, to detect programming error.

>     If the memory region specified by addr and len overlaps pages of any
>     existing mapping(s), then the overlapped part of the existing map‐
>     ping(s) will be discarded.  If the specified address cannot be used,
>     mmap() will fail.
Anthony PERARD March 19, 2019, 3:06 p.m. UTC | #4
On Mon, Mar 18, 2019 at 06:37:31PM +0100, 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>
> Reviewed-by: Paul Durrant <paul.durrant@citrix.com>

Acked-by: Anthony PERARD <anthony.perard@citrix.com>
Roger Pau Monne March 21, 2019, 2:08 a.m. UTC | #5
On Tue, Mar 19, 2019 at 02:16:44PM +0000, Anthony PERARD wrote:
> On Mon, Mar 18, 2019 at 10:43:12PM +0100, Marek Marczykowski-Górecki wrote:
> > On Mon, Mar 18, 2019 at 06:37:31PM +0100, Roger Pau Monne wrote:
> > > Or if it's not possible to honor the hinted address an error is returned
> > > instead. 
> > 
> > Just to be sure: MAP_FIXED will cause to map at specified address, even
> > if something is mapped there already. From mmap(2):
> 
> That should be fine. We do want to replace an exiting mapping (which is
> munmap before the mmap call), but it would have been nice to know when
> something is already mapped, to detect programming error.

FreeBSD has MAP_EXCL to be used in conjunction with MAP_FIXED, and
Linux has MAP_FIXED_NOREPLACE to achieve this behavior. I think the
current fix is fine for now, but we might want to look into using
those if further issues are found.

Thanks, Roger.
diff mbox series

Patch

diff --git a/hw/i386/xen/xen-mapcache.c b/hw/i386/xen/xen-mapcache.c
index 349f72d00c..254759f776 100644
--- a/hw/i386/xen/xen-mapcache.c
+++ b/hw/i386/xen/xen-mapcache.c
@@ -184,9 +184,14 @@  static void xen_remap_bucket(MapCacheEntry *entry,
         pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
     }
 
+    /*
+     * If the caller has requested the mapping at a specific address use
+     * MAP_FIXED to make sure it's honored.
+     */
     if (!dummy) {
         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);