Skip to contentSkip to conversation

linux-user/elfload: use MAP_FIXED in pgb_reserved_va

PATCH In review Alex Bennée 3 participants 4 messages Jun 30, 2020 – Jul 2, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user/elfload: use MAP_FIXED in pgb_reserved_va

Given we assert the requested address matches what we asked we should also make that clear in the mmap flags. Otherwise we see failures in the GitLab environment for some currently unknown but allowable reason.

---
 linux-user/elfload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/elfload.c +1 -1
index b5cb21384a1..be8facfbcc8 100644
@@ -2294,7 +2294,7 @@ static void pgb_dynamic(const char *image_name, long align)
 static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
                             abi_ulong guest_hiaddr, long align)
 {
-    const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
+    const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE | MAP_FIXED;
     void *addr, *test;
 
     if (guest_hiaddr > reserved_va) {
Signature · 1 line
2.20.1
#1 lore raw
Quoted 5 lines · Alex Bennée

Given we assert the requested address matches what we asked we should also make that clear in the mmap flags. Otherwise we see failures in the GitLab environment for some currently unknown but allowable reason.

Adding MAP_FIXED will mean that instead of failing if there's something else already at that address, the kernel will now silently blow that away in favour of the new mapping. Is that definitely what we want here ?

thanks
-- PMM

#2 lore raw
Quoted 11 lines · Peter Maydell
Quoted 5 lines · Alex Bennée

Given we assert the requested address matches what we asked we should also make that clear in the mmap flags. Otherwise we see failures in the GitLab environment for some currently unknown but allowable reason.

Adding MAP_FIXED will mean that instead of failing if there's something else already at that address, the kernel will now silently blow that away in favour of the new mapping. Is that definitely what we want here ?

Hmm maybe not. But hey I just noticed that we have MAP_FIXED_NOREPLACE (since Linux 4.17) which says:

This flag provides behavior that is similar to MAP_FIXED with
respect to the addr enforcement, but differs in that
MAP_FIXED_NOREPLACE never clobbers a preexisting mapped range. If the requested range would collide with an existing mapping,
then this call fails with the error EEXIST. This flag can
therefore be used as a way to atomically (with respect to other threads) attempt to map an address range: one thread will suc‐ ceed; all others will report failure.

Note that older kernels which do not recognize the
MAP_FIXED_NOREPLACE flag will typically (upon detecting a colli‐ sion with a preexisting mapping) fall back to a "non-MAP_FIXED" type of behavior: they will return an address that is different
from the requested address. Therefore, backward-compatible
software should check the returned address against the requested address.

So maybe that is what we should do?

Now you've pointed that out I wonder if we need to fix pgd_find_hole_fallback as well?

Quoted 3 lines

thanks
-- PMM

Signature · 1 line
Alex Bennée
#3 lore raw
Quoted 10 lines · Alex Bennée
Quoted 7 lines · Peter Maydell
Quoted 5 lines · Alex Bennée

Given we assert the requested address matches what we asked we should also make that clear in the mmap flags. Otherwise we see failures in the GitLab environment for some currently unknown but allowable reason.

Quoted 6 lines
Quoted 4 lines

Adding MAP_FIXED will mean that instead of failing if there's something else already at that address, the kernel will now silently blow that away in favour of the new mapping. Is that definitely what we want here ?

Hmm maybe not.

Definitely not.

Quoted 20 lines

But hey I just noticed that we have MAP_FIXED_NOREPLACE (since Linux 4.17) which says:

This flag provides behavior that is similar to MAP_FIXED with
respect to the addr enforcement, but differs in that
MAP_FIXED_NOREPLACE never clobbers a preexisting mapped range. If the requested range would collide with an existing mapping,
then this call fails with the error EEXIST. This flag can
therefore be used as a way to atomically (with respect to other threads) attempt to map an address range: one thread will suc‐ ceed; all others will report failure.

Note that older kernels which do not recognize the
MAP_FIXED_NOREPLACE flag will typically (upon detecting a colli‐ sion with a preexisting mapping) fall back to a "non-MAP_FIXED" type of behavior: they will return an address that is different
from the requested address. Therefore, backward-compatible
software should check the returned address against the requested address.

Quoted 1 line

So maybe that is what we should do?

Yes, that would be better, because those are the exact semantics that we want.
Though it would be Really Nice to know what's up with gitlab...

Quoted 2 lines

Now you've pointed that out I wonder if we need to fix pgd_find_hole_fallback as well?

Yes, that could benefit from MAP_FIXED_NOREPLACE.

I do think there's a way we could streamline the 32-on-64 case. At present we are groveling through /proc/self/maps, or mmaping+unmaping, and then mmaping. Whereas we could just mmap once and be done -- it's the 32-on-32 case that requires the song and dance.

r~