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 6 messages Mar 18, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 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..23de5517db 100644
@@ -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);
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 33 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 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 +0 -0
index 349f72d00c..23de5517db 100644

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

Quoted 7 lines
hw/i386/xen/xen-mapcache.c +4 -0
@@ -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

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

#2 lore raw
Quoted 36 lines · Igor Druzhinin
Quoted 35 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 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 +0 -0
index 349f72d00c..23de5517db 100644
@@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 8 lines

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

Quoted 5 lines
hw/i386/xen/xen-mapcache.c +4 -0
     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.

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

#3 lore raw
In reply to Igor Druzhinin
Quoted 8 lines · Igor Druzhinin
Quoted 7 lines · Roger Pau Monne
hw/i386/xen/xen-mapcache.c +0 -0
index 349f72d00c..23de5517db 100644
@@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 8 lines

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

Quoted 5 lines
hw/i386/xen/xen-mapcache.c +4 -0
     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.

Signature · 6 lines
Anthony PERARD

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

-----Original Message-----
From: Anthony PERARD [mailto:[email protected]]
Sent: 18 March 2019 16:35
To: Igor Druzhinin <[email protected]>
Cc: Roger Pau Monne <[email protected]>; [email protected]; Stefano Stabellini <[email protected]>; Paul Durrant <[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: Re: [PATCH v2] xen-mapcache: use MAP_FIXED flag so the mmap address hint is always honored

Quoted 8 lines · Igor Druzhinin
Quoted 7 lines · Roger Pau Monne
hw/i386/xen/xen-mapcache.c +0 -0
index 349f72d00c..23de5517db 100644
@@ -185,8 +185,13 @@ static void xen_remap_bucket(MapCacheEntry *entry,
     }
 
Quoted 8 lines

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

Quoted 5 lines
hw/i386/xen/xen-mapcache.c +4 -0
     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?

Quoted 1 line

That sounds good to me.

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

Paul

Quoted 3 lines

--
Anthony PERARD

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

Reviewed-by on v2 1/1
#5 lore raw
In reply to Roger Pau Monne
Quoted 16 lines

-----Original Message-----
From: Roger Pau Monne [mailto:[email protected]]
Sent: 18 March 2019 15:46
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 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.

Quoted 4 lines

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

Quoted 49 lines

---
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 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 -3
index 349f72d00c..23de5517db 100644
@@ -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);
--
Quoted 1 line

2.17.2 (Apple Git-113)

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