Skip to contentSkip to conversation

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

PATCH Superseded Roger Pau Monne 4 participants 8 messages Mar 15, 2019 1 message not in the archive

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: Igor Druzhinin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: [email protected]

---
 hw/i386/xen/xen-mapcache.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/i386/xen/xen-mapcache.c +7 -1
index 349f72d00c..0e2870b320 100644
@@ -185,8 +185,14 @@ 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. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */
         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");
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 43 lines

-----Original Message-----
From: Roger Pau Monne [mailto:[email protected]]
Sent: 15 March 2019 08:59
To: [email protected]
Cc: Roger Pau Monne <[email protected]>; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; Igor Druzhinin <[email protected]>; Paolo Bonzini <[email protected]>; Richard Henderson <[email protected]>; Eduardo Habkost <[email protected]>; Michael S. Tsirkin <[email protected]>; Marcel Apfelbaum <[email protected]>; [email protected]
Subject: [PATCH] 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.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: Igor Druzhinin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: [email protected]

---
 hw/i386/xen/xen-mapcache.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/i386/xen/xen-mapcache.c +0 -0
index 349f72d00c..0e2870b320 100644
@@ -185,8 +185,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
     if (!dummy) {

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 196

Quoted 10 lines
hw/i386/xen/xen-mapcache.c +7 -1
+        /*
+         * If the caller has requested the mapping at a specific address use
+         * MAP_FIXED to make sure it's honored. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */
         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);

AFAICT xen_remap_bucket() is always called with a NULL vaddr argument if entry->vaddr_base == NULL, and called with vaddr == entry->vaddr_base in the other case, so I'd say the vaddr argument is superfluous. So, I think it would be better to just use entry->vaddr_base in the call above and I also wonder whether the mmap() below should only be allowed to happen if entry->vaddr_base == NULL.

Paul

Quoted 4 lines
hw/i386/xen/xen-mapcache.c +0 -1
         if (vaddr_base == NULL) {
             perror("xenforeignmemory_map2");
--

2.17.2 (Apple Git-113)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 42 lines · Paul Durrant
Quoted 42 lines

-----Original Message-----
From: Roger Pau Monne [mailto:[email protected]]
Sent: 15 March 2019 08:59
To: [email protected]
Cc: Roger Pau Monne <[email protected]>; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; Igor Druzhinin <[email protected]>; Paolo Bonzini <[email protected]>; Richard Henderson <[email protected]>; Eduardo Habkost <[email protected]>; Michael S. Tsirkin <[email protected]>; Marcel Apfelbaum <[email protected]>; [email protected]
Subject: [PATCH] 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.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: Igor Druzhinin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: [email protected]

---
 hw/i386/xen/xen-mapcache.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/i386/xen/xen-mapcache.c +0 -0
index 349f72d00c..0e2870b320 100644
@@ -185,8 +185,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 14 lines

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 196

Quoted 11 lines
hw/i386/xen/xen-mapcache.c +7 -1
     if (!dummy) {
+        /*
+         * If the caller has requested the mapping at a specific address use
+         * MAP_FIXED to make sure it's honored. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */
         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);

AFAICT xen_remap_bucket() is always called with a NULL vaddr argument if entry->vaddr_base == NULL, and called with vaddr == entry->vaddr_base in the other case, so I'd say the vaddr argument is superfluous. So, I think it would be better to just use entry->vaddr_base in the call above and I also wonder whether the mmap() below should only be allowed to happen if entry->vaddr_base == NULL.

The suggested changes above look reasonable to me (note I'm not familiar with qemu Xen mapcache), however they should be separate patch(es) IMO, since they are not directly related to the MAP_FIXED issue this patch attempts to fix.

I would prefer to keep this patch as-is if possible, so that it's easier and safer for me to backport in order to fix qemu in Xen 4.11 for FreeBSD.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 73 lines

-----Original Message-----
From: Roger Pau Monne
Sent: 15 March 2019 10:10
To: Paul Durrant <[email protected]>
Cc: [email protected]; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Igor Druzhinin <[email protected]>; Paolo Bonzini <[email protected]>; Richard Henderson <[email protected]>; Eduardo Habkost <[email protected]>; Michael S. Tsirkin <[email protected]>; Marcel Apfelbaum <[email protected]>; xen- [email protected]
Subject: Re: [PATCH] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored

Quoted 7 lines · Paul Durrant
Quoted 7 lines

-----Original Message-----
From: Roger Pau Monne [mailto:[email protected]]
Sent: 15 March 2019 08:59
To: [email protected]
Cc: Roger Pau Monne <[email protected]>; Stefano Stabellini <[email protected]>; Anthony Perard <[email protected]>; Paul Durrant <[email protected]>; Igor Druzhinin <[email protected]>; Paolo Bonzini <[email protected]>; Richard Henderson

<[email protected]>;

Quoted 35 lines
Quoted 35 lines

Eduardo Habkost <[email protected]>; Michael S. Tsirkin <[email protected]>; Marcel Apfelbaum <[email protected]>; [email protected]
Subject: [PATCH] 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.

---
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: Igor Druzhinin <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: [email protected]

---
 hw/i386/xen/xen-mapcache.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/i386/xen/xen-mapcache.c +0 -0
index 349f72d00c..0e2870b320 100644
@@ -185,8 +185,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 13 lines

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 196

Quoted 11 lines
hw/i386/xen/xen-mapcache.c +7 -1
     if (!dummy) {
+        /*
+         * If the caller has requested the mapping at a specific address use
+         * MAP_FIXED to make sure it's honored. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */
         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);

AFAICT xen_remap_bucket() is always called with a NULL vaddr argument if entry->vaddr_base == NULL,

and called with vaddr == entry->vaddr_base in the other case, so I'd say the vaddr argument is superfluous.

Quoted 1 line

So, I think it would be better to just use entry->vaddr_base in the call above and I also wonder

whether the mmap() below should only be allowed to happen if entry->vaddr_base == NULL.

Quoted 8 lines

The suggested changes above look reasonable to me (note I'm not familiar with qemu Xen mapcache), however they should be separate patch(es) IMO, since they are not directly related to the MAP_FIXED issue this patch attempts to fix.

I would prefer to keep this patch as-is if possible, so that it's easier and safer for me to backport in order to fix qemu in Xen 4.11 for FreeBSD.

I think it does indeed DTRT as-is. Let's see what Anthony says.

Paul

Quoted 2 lines

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Paul Durrant
Quoted 4 lines · Paul Durrant

AFAICT xen_remap_bucket() is always called with a NULL vaddr argument if entry->vaddr_base == NULL, and called with vaddr == entry->vaddr_base in the other case, so I'd say the vaddr argument is superfluous.

I don't think that's true. The call at line 312 [1] may be called with vaddr_base != NULL. Then, xen_remap_bucket will unmap that entry before replacing it.

We could maybe figure out if vaddr_base needs to be replaced in-place based on the flags XEN_MAPCACHE_ENTRY_DUMMY, but that seems more convoluted than the current approche.

[1] https://git.qemu.org/?p=qemu.git;a=blob;f=hw/i386/xen/xen-mapcache.c;h=349f72d00cc2c9fc134df8cff7dd051b1fc2fa41;hb=HEAD#l312

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/1
#5 lore raw
In reply to Roger Pau Monne
Quoted 9 lines · Roger Pau Monne

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.

The patch looks fine, and MAP_FIXED seems to be the expected behavior even on Linux.

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 192

Quoted 10 lines
hw/i386/xen/xen-mapcache.c +5 -0
@@ -185,8 +185,14 @@ 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. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */

I wonder if the note about FreeBSD is actually useful here. Even Linux may map at a different address without MAP_FIXED, if I read the man page correctly. Do you mind if it is removed?

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 196

Quoted 5 lines
hw/i386/xen/xen-mapcache.c +2 -1
         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);

Thanks,

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw
Quoted 20 lines · Anthony PERARD
Quoted 9 lines · Roger Pau Monne

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.

The patch looks fine, and MAP_FIXED seems to be the expected behavior even on Linux.

Quoted 4 lines
hw/i386/xen/xen-mapcache.c +0 -0
@@ -185,8 +185,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 11 lines

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 196

Quoted 11 lines
hw/i386/xen/xen-mapcache.c +7 -1
     if (!dummy) {
+        /*
+         * If the caller has requested the mapping at a specific address use
+         * MAP_FIXED to make sure it's honored. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */
         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);

I've noticed that there's a mmap call just after which has also vaddr as hint, should that second call also have MAP_FIXED as flags?

On the other hand, it seems that when vaddr!=NULL, dummy==false, so that second mmap call will never be called with vaddr!=NULL. But it might be worse fixing this in the same patch.

Quoted 7 lines

} else {
/*
* We create dummy mappings where we are unable to create a foreign
* 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);

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 21 lines · Anthony PERARD
Quoted 20 lines · Anthony PERARD
Quoted 9 lines · Roger Pau Monne

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.

The patch looks fine, and MAP_FIXED seems to be the expected behavior even on Linux.

Quoted 4 lines
hw/i386/xen/xen-mapcache.c +0 -0
@@ -185,8 +185,14 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 14 lines
Quoted 11 lines

hw/i386/xen/xen-mapcache.c static void xen_remap_bucket(MapCacheEntry *entry, · line 196

Quoted 11 lines
hw/i386/xen/xen-mapcache.c +7 -1
     if (!dummy) {
+        /*
+         * If the caller has requested the mapping at a specific address use
+         * MAP_FIXED to make sure it's honored. Note that with MAP_FIXED at
+         * least FreeBSD will try harder to honor the passed address.
+         */
         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);

I've noticed that there's a mmap call just after which has also vaddr as hint, should that second call also have MAP_FIXED as flags?

I think so, yes. The intended usage of xen_remap_bucket() is to create a mapcache entry - either dummy or real. So this patch fixes the present problem for real entry now but not for dummy. In future, there might be xen_remap_bucket() calls to create dummy entries at a predefined location so, I think, MAP_FIXED should be passed to mmap() call as well.

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel