Skip to content

Commit f39c580

Browse files
kvaneeshtorvalds
authored andcommitted
selftest/vm: fix map_fixed_noreplace test failure
On the latest RHEL the test fails due to executable mapped at 256MB address # ./map_fixed_noreplace mmap() @ 0x10000000-0x10050000 p=0xffffffffffffffff result=File exists 10000000-10010000 r-xp 00000000 fd:04 34905657 /root/rpmbuild/BUILD/kernel-5.14.0-56.el9/linux-5.14.0-56.el9.ppc64le/tools/testing/selftests/vm/map_fixed_noreplace 10010000-10020000 r--p 00000000 fd:04 34905657 /root/rpmbuild/BUILD/kernel-5.14.0-56.el9/linux-5.14.0-56.el9.ppc64le/tools/testing/selftests/vm/map_fixed_noreplace 10020000-10030000 rw-p 00010000 fd:04 34905657 /root/rpmbuild/BUILD/kernel-5.14.0-56.el9/linux-5.14.0-56.el9.ppc64le/tools/testing/selftests/vm/map_fixed_noreplace 10029b90000-10029bc0000 rw-p 00000000 00:00 0 [heap] 7fffbb510000-7fffbb750000 r-xp 00000000 fd:04 24534 /usr/lib64/libc.so.6 7fffbb750000-7fffbb760000 r--p 00230000 fd:04 24534 /usr/lib64/libc.so.6 7fffbb760000-7fffbb770000 rw-p 00240000 fd:04 24534 /usr/lib64/libc.so.6 7fffbb780000-7fffbb7a0000 r--p 00000000 00:00 0 [vvar] 7fffbb7a0000-7fffbb7b0000 r-xp 00000000 00:00 0 [vdso] 7fffbb7b0000-7fffbb800000 r-xp 00000000 fd:04 24514 /usr/lib64/ld64.so.2 7fffbb800000-7fffbb810000 r--p 00040000 fd:04 24514 /usr/lib64/ld64.so.2 7fffbb810000-7fffbb820000 rw-p 00050000 fd:04 24514 /usr/lib64/ld64.so.2 7fffd93f0000-7fffd9420000 rw-p 00000000 00:00 0 [stack] Error: couldn't map the space we need for the test Fix this by finding a free address using mmap instead of hardcoding BASE_ADDRESS. Link: https://lkml.kernel.org/r/20220217083417.373823-1-aneesh.kumar@linux.ibm.com Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Jann Horn <jannh@google.com> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f798a1d commit f39c580

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

tools/testing/selftests/vm/map_fixed_noreplace.c

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
#define MAP_FIXED_NOREPLACE 0x100000
1818
#endif
1919

20-
#define BASE_ADDRESS (256ul * 1024 * 1024)
21-
22-
2320
static void dump_maps(void)
2421
{
2522
char cmd[32];
@@ -28,18 +25,46 @@ static void dump_maps(void)
2825
system(cmd);
2926
}
3027

28+
static unsigned long find_base_addr(unsigned long size)
29+
{
30+
void *addr;
31+
unsigned long flags;
32+
33+
flags = MAP_PRIVATE | MAP_ANONYMOUS;
34+
addr = mmap(NULL, size, PROT_NONE, flags, -1, 0);
35+
if (addr == MAP_FAILED) {
36+
printf("Error: couldn't map the space we need for the test\n");
37+
return 0;
38+
}
39+
40+
if (munmap(addr, size) != 0) {
41+
printf("Error: couldn't map the space we need for the test\n");
42+
return 0;
43+
}
44+
return (unsigned long)addr;
45+
}
46+
3147
int main(void)
3248
{
49+
unsigned long base_addr;
3350
unsigned long flags, addr, size, page_size;
3451
char *p;
3552

3653
page_size = sysconf(_SC_PAGE_SIZE);
3754

55+
//let's find a base addr that is free before we start the tests
56+
size = 5 * page_size;
57+
base_addr = find_base_addr(size);
58+
if (!base_addr) {
59+
printf("Error: couldn't map the space we need for the test\n");
60+
return 1;
61+
}
62+
3863
flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE;
3964

4065
// Check we can map all the areas we need below
4166
errno = 0;
42-
addr = BASE_ADDRESS;
67+
addr = base_addr;
4368
size = 5 * page_size;
4469
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
4570

@@ -60,7 +85,7 @@ int main(void)
6085
printf("unmap() successful\n");
6186

6287
errno = 0;
63-
addr = BASE_ADDRESS + page_size;
88+
addr = base_addr + page_size;
6489
size = 3 * page_size;
6590
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
6691
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -80,7 +105,7 @@ int main(void)
80105
* +4 | free | new
81106
*/
82107
errno = 0;
83-
addr = BASE_ADDRESS;
108+
addr = base_addr;
84109
size = 5 * page_size;
85110
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
86111
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -101,7 +126,7 @@ int main(void)
101126
* +4 | free |
102127
*/
103128
errno = 0;
104-
addr = BASE_ADDRESS + (2 * page_size);
129+
addr = base_addr + (2 * page_size);
105130
size = page_size;
106131
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
107132
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -121,7 +146,7 @@ int main(void)
121146
* +4 | free | new
122147
*/
123148
errno = 0;
124-
addr = BASE_ADDRESS + (3 * page_size);
149+
addr = base_addr + (3 * page_size);
125150
size = 2 * page_size;
126151
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
127152
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -141,7 +166,7 @@ int main(void)
141166
* +4 | free |
142167
*/
143168
errno = 0;
144-
addr = BASE_ADDRESS;
169+
addr = base_addr;
145170
size = 2 * page_size;
146171
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
147172
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -161,7 +186,7 @@ int main(void)
161186
* +4 | free |
162187
*/
163188
errno = 0;
164-
addr = BASE_ADDRESS;
189+
addr = base_addr;
165190
size = page_size;
166191
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
167192
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -181,7 +206,7 @@ int main(void)
181206
* +4 | free | new
182207
*/
183208
errno = 0;
184-
addr = BASE_ADDRESS + (4 * page_size);
209+
addr = base_addr + (4 * page_size);
185210
size = page_size;
186211
p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0);
187212
printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p);
@@ -192,7 +217,7 @@ int main(void)
192217
return 1;
193218
}
194219

195-
addr = BASE_ADDRESS;
220+
addr = base_addr;
196221
size = 5 * page_size;
197222
if (munmap((void *)addr, size) != 0) {
198223
dump_maps();

0 commit comments

Comments
 (0)