Skip to contentSkip to conversation

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

PATCH In review Roger Pau Monne 4 participants 6 messages Mar 18, 2019 – Mar 21, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v3 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]
---
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(-)
hw/i386/xen/xen-mapcache.c +8 -2
index 349f72d00c..254759f776 100644
@@ -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);
Signature · 7 lines
2.17.2 (Apple Git-113)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v3 1/1
#1 lore raw
Quoted 54 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.

---
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]
---
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(-)
hw/i386/xen/xen-mapcache.c +6 -1
index 349f72d00c..254759f776 100644
@@ -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");

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

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

Igor

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

#2 lore raw
In reply to Roger Pau Monne
Quoted 2 lines · Roger Pau Monne

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.

Quoted 61 lines

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]
---
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(-)
hw/i386/xen/xen-mapcache.c +8 -2
index 349f72d00c..254759f776 100644
@@ -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);
Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

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

#3 lore raw
Quoted 6 lines · Marek Marczykowski-Górecki
Quoted 2 lines · Roger Pau Monne

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.

Quoted 4 lines

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.

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v3 1/1
#5 lore raw
In reply to Roger Pau Monne
Quoted 10 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.

Signature · 6 lines
Anthony PERARD

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw
In reply to Anthony PERARD
Quoted 11 lines · Anthony PERARD
Quoted 6 lines · Marek Marczykowski-Górecki
Quoted 2 lines · Roger Pau Monne

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.

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