Skip to content

Commit 2667e06

Browse files
committed
linux-user: don't use MAP_FIXED in pgd_find_hole_fallback
Plain MAP_FIXED has the undesirable behaviour of splatting exiting maps so we don't actually achieve what we want when looking for gaps. We should be using MAP_FIXED_NOREPLACE. As this isn't always available we need to potentially check the returned address to see if the kernel gave us what we asked for. Fixes: ad592e3 ("linux-user: provide fallback pgd_find_hole for bare chroots") Signed-off-by: Alex Bennée <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Message-Id: <[email protected]>
1 parent 163b3d1 commit 2667e06

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

include/qemu/osdep.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ extern int daemon(int, int);
173173
#ifndef MAP_ANONYMOUS
174174
#define MAP_ANONYMOUS MAP_ANON
175175
#endif
176+
#ifndef MAP_FIXED_NOREPLACE
177+
#define MAP_FIXED_NOREPLACE 0
178+
#endif
176179
#ifndef ENOMEDIUM
177180
#define ENOMEDIUM ENODEV
178181
#endif

linux-user/elfload.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,12 +2134,15 @@ static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
21342134
/* we have run out of space */
21352135
return -1;
21362136
} else {
2137-
int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE | MAP_FIXED;
2137+
int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
2138+
MAP_FIXED_NOREPLACE;
21382139
void * mmap_start = mmap((void *) align_start, guest_size,
21392140
PROT_NONE, flags, -1, 0);
21402141
if (mmap_start != MAP_FAILED) {
21412142
munmap((void *) align_start, guest_size);
2142-
return (uintptr_t) mmap_start + offset;
2143+
if (MAP_FIXED_NOREPLACE || mmap_start == (void *) align_start) {
2144+
return (uintptr_t) mmap_start + offset;
2145+
}
21432146
}
21442147
base += qemu_host_page_size;
21452148
}
@@ -2307,9 +2310,8 @@ static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
23072310
/* Widen the "image" to the entire reserved address space. */
23082311
pgb_static(image_name, 0, reserved_va, align);
23092312

2310-
#ifdef MAP_FIXED_NOREPLACE
2313+
/* osdep.h defines this as 0 if it's missing */
23112314
flags |= MAP_FIXED_NOREPLACE;
2312-
#endif
23132315

23142316
/* Reserve the memory on the host. */
23152317
assert(guest_base != 0);

0 commit comments

Comments
 (0)