Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Aneesh Kumar K.V and committed by
Linus Torvalds
f39c5800 f798a1d4

+37 -12
+37 -12
tools/testing/selftests/vm/map_fixed_noreplace.c
··· 17 17 #define MAP_FIXED_NOREPLACE 0x100000 18 18 #endif 19 19 20 - #define BASE_ADDRESS (256ul * 1024 * 1024) 21 - 22 - 23 20 static void dump_maps(void) 24 21 { 25 22 char cmd[32]; ··· 25 28 system(cmd); 26 29 } 27 30 31 + static unsigned long find_base_addr(unsigned long size) 32 + { 33 + void *addr; 34 + unsigned long flags; 35 + 36 + flags = MAP_PRIVATE | MAP_ANONYMOUS; 37 + addr = mmap(NULL, size, PROT_NONE, flags, -1, 0); 38 + if (addr == MAP_FAILED) { 39 + printf("Error: couldn't map the space we need for the test\n"); 40 + return 0; 41 + } 42 + 43 + if (munmap(addr, size) != 0) { 44 + printf("Error: couldn't map the space we need for the test\n"); 45 + return 0; 46 + } 47 + return (unsigned long)addr; 48 + } 49 + 28 50 int main(void) 29 51 { 52 + unsigned long base_addr; 30 53 unsigned long flags, addr, size, page_size; 31 54 char *p; 32 55 33 56 page_size = sysconf(_SC_PAGE_SIZE); 34 57 58 + //let's find a base addr that is free before we start the tests 59 + size = 5 * page_size; 60 + base_addr = find_base_addr(size); 61 + if (!base_addr) { 62 + printf("Error: couldn't map the space we need for the test\n"); 63 + return 1; 64 + } 65 + 35 66 flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE; 36 67 37 68 // Check we can map all the areas we need below 38 69 errno = 0; 39 - addr = BASE_ADDRESS; 70 + addr = base_addr; 40 71 size = 5 * page_size; 41 72 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 42 73 ··· 85 60 printf("unmap() successful\n"); 86 61 87 62 errno = 0; 88 - addr = BASE_ADDRESS + page_size; 63 + addr = base_addr + page_size; 89 64 size = 3 * page_size; 90 65 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 91 66 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 105 80 * +4 | free | new 106 81 */ 107 82 errno = 0; 108 - addr = BASE_ADDRESS; 83 + addr = base_addr; 109 84 size = 5 * page_size; 110 85 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 111 86 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 126 101 * +4 | free | 127 102 */ 128 103 errno = 0; 129 - addr = BASE_ADDRESS + (2 * page_size); 104 + addr = base_addr + (2 * page_size); 130 105 size = page_size; 131 106 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 132 107 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 146 121 * +4 | free | new 147 122 */ 148 123 errno = 0; 149 - addr = BASE_ADDRESS + (3 * page_size); 124 + addr = base_addr + (3 * page_size); 150 125 size = 2 * page_size; 151 126 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 152 127 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 166 141 * +4 | free | 167 142 */ 168 143 errno = 0; 169 - addr = BASE_ADDRESS; 144 + addr = base_addr; 170 145 size = 2 * page_size; 171 146 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 172 147 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 186 161 * +4 | free | 187 162 */ 188 163 errno = 0; 189 - addr = BASE_ADDRESS; 164 + addr = base_addr; 190 165 size = page_size; 191 166 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 192 167 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 206 181 * +4 | free | new 207 182 */ 208 183 errno = 0; 209 - addr = BASE_ADDRESS + (4 * page_size); 184 + addr = base_addr + (4 * page_size); 210 185 size = page_size; 211 186 p = mmap((void *)addr, size, PROT_NONE, flags, -1, 0); 212 187 printf("mmap() @ 0x%lx-0x%lx p=%p result=%m\n", addr, addr + size, p); ··· 217 192 return 1; 218 193 } 219 194 220 - addr = BASE_ADDRESS; 195 + addr = base_addr; 221 196 size = 5 * page_size; 222 197 if (munmap((void *)addr, size) != 0) { 223 198 dump_maps();