1 // SPDX-License-Identifier: GPL-2.0 2 #define _GNU_SOURCE 3 #include <sys/mman.h> 4 #include <linux/mman.h> 5 #include <stdint.h> 6 #include <unistd.h> 7 #include <string.h> 8 #include <sys/time.h> 9 #include <sys/resource.h> 10 #include <stdbool.h> 11 #include "kselftest.h" 12 #include "mlock2.h" 13 14 struct vm_boundaries { 15 unsigned long start; 16 unsigned long end; 17 }; 18 19 static int get_vm_area(unsigned long addr, struct vm_boundaries *area) 20 { 21 FILE *file; 22 int ret = 1; 23 char line[1024] = {0}; 24 unsigned long start; 25 unsigned long end; 26 27 if (!area) 28 return ret; 29 30 file = fopen("/proc/self/maps", "r"); 31 if (!file) { 32 perror("fopen"); 33 return ret; 34 } 35 36 memset(area, 0, sizeof(struct vm_boundaries)); 37 38 while(fgets(line, 1024, file)) { 39 if (sscanf(line, "%lx-%lx", &start, &end) != 2) { 40 ksft_print_msg("cannot parse /proc/self/maps\n"); 41 goto out; 42 } 43 44 if (start <= addr && end > addr) { 45 area->start = start; 46 area->end = end; 47 ret = 0; 48 goto out; 49 } 50 } 51 out: 52 fclose(file); 53 return ret; 54 } 55 56 #define VMFLAGS "VmFlags:" 57 58 static bool is_vmflag_set(unsigned long addr, const char *vmflag) 59 { 60 char *line = NULL; 61 char *flags; 62 size_t size = 0; 63 bool ret = false; 64 FILE *smaps; 65 66 smaps = seek_to_smaps_entry(addr); 67 if (!smaps) { 68 ksft_print_msg("Unable to parse /proc/self/smaps\n"); 69 goto out; 70 } 71 72 while (getline(&line, &size, smaps) > 0) { 73 if (!strstr(line, VMFLAGS)) { 74 free(line); 75 line = NULL; 76 size = 0; 77 continue; 78 } 79 80 flags = line + strlen(VMFLAGS); 81 ret = (strstr(flags, vmflag) != NULL); 82 goto out; 83 } 84 85 out: 86 free(line); 87 fclose(smaps); 88 return ret; 89 } 90 91 #define SIZE "Size:" 92 #define RSS "Rss:" 93 #define LOCKED "lo" 94 95 static unsigned long get_value_for_name(unsigned long addr, const char *name) 96 { 97 char *line = NULL; 98 size_t size = 0; 99 char *value_ptr; 100 FILE *smaps = NULL; 101 unsigned long value = -1UL; 102 103 smaps = seek_to_smaps_entry(addr); 104 if (!smaps) { 105 ksft_print_msg("Unable to parse /proc/self/smaps\n"); 106 goto out; 107 } 108 109 while (getline(&line, &size, smaps) > 0) { 110 if (!strstr(line, name)) { 111 free(line); 112 line = NULL; 113 size = 0; 114 continue; 115 } 116 117 value_ptr = line + strlen(name); 118 if (sscanf(value_ptr, "%lu kB", &value) < 1) { 119 ksft_print_msg("Unable to parse smaps entry for Size\n"); 120 goto out; 121 } 122 break; 123 } 124 125 out: 126 if (smaps) 127 fclose(smaps); 128 free(line); 129 return value; 130 } 131 132 static bool is_vma_lock_on_fault(unsigned long addr) 133 { 134 bool locked; 135 unsigned long vma_size, vma_rss; 136 137 locked = is_vmflag_set(addr, LOCKED); 138 if (!locked) 139 return false; 140 141 vma_size = get_value_for_name(addr, SIZE); 142 vma_rss = get_value_for_name(addr, RSS); 143 144 /* only one page is faulted in */ 145 return (vma_rss < vma_size); 146 } 147 148 #define PRESENT_BIT 0x8000000000000000ULL 149 #define PFN_MASK 0x007FFFFFFFFFFFFFULL 150 #define UNEVICTABLE_BIT (1UL << 18) 151 152 static int lock_check(unsigned long addr) 153 { 154 bool locked; 155 unsigned long vma_size, vma_rss; 156 157 locked = is_vmflag_set(addr, LOCKED); 158 if (!locked) 159 return false; 160 161 vma_size = get_value_for_name(addr, SIZE); 162 vma_rss = get_value_for_name(addr, RSS); 163 164 return (vma_rss == vma_size); 165 } 166 167 static int unlock_lock_check(char *map, bool mlock_supported) 168 { 169 if (!is_vmflag_set((unsigned long)map, LOCKED)) 170 return 0; 171 172 if (mlock_supported) 173 ksft_print_msg("VMA flag %s is present on page 1 after unlock\n", LOCKED); 174 else 175 ksft_print_msg("VMA flag %s is present on an unsupported VMA\n", LOCKED); 176 177 return 1; 178 } 179 180 static void test_mlock_lock(void) 181 { 182 char *map; 183 unsigned long page_size = getpagesize(); 184 185 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 186 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 187 if (map == MAP_FAILED) 188 ksft_exit_fail_msg("mmap error: %s", strerror(errno)); 189 190 if (mlock2_(map, 2 * page_size, 0)) { 191 munmap(map, 2 * page_size); 192 ksft_exit_fail_msg("mlock2(0): %s\n", strerror(errno)); 193 } 194 195 ksft_test_result(lock_check((unsigned long)map), "%s: Locked\n", __func__); 196 197 /* Now unlock and recheck attributes */ 198 if (munlock(map, 2 * page_size)) { 199 munmap(map, 2 * page_size); 200 ksft_exit_fail_msg("munlock(): %s\n", strerror(errno)); 201 } 202 203 ksft_test_result(!unlock_lock_check(map, true), "%s: Unlocked\n", __func__); 204 munmap(map, 2 * page_size); 205 } 206 207 static int onfault_check(char *map) 208 { 209 *map = 'a'; 210 if (!is_vma_lock_on_fault((unsigned long)map)) { 211 ksft_print_msg("VMA is not marked for lock on fault\n"); 212 return 1; 213 } 214 215 return 0; 216 } 217 218 static int unlock_onfault_check(char *map) 219 { 220 unsigned long page_size = getpagesize(); 221 222 if (is_vma_lock_on_fault((unsigned long)map) || 223 is_vma_lock_on_fault((unsigned long)map + page_size)) { 224 ksft_print_msg("VMA is still lock on fault after unlock\n"); 225 return 1; 226 } 227 228 return 0; 229 } 230 231 static void test_mlock_onfault(void) 232 { 233 char *map; 234 unsigned long page_size = getpagesize(); 235 236 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 237 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 238 if (map == MAP_FAILED) 239 ksft_exit_fail_msg("mmap error: %s", strerror(errno)); 240 241 if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) { 242 munmap(map, 2 * page_size); 243 ksft_exit_fail_msg("mlock2(MLOCK_ONFAULT): %s\n", strerror(errno)); 244 } 245 246 ksft_test_result(!onfault_check(map), "%s: VMA marked for lock on fault\n", __func__); 247 248 /* Now unlock and recheck attributes */ 249 if (munlock(map, 2 * page_size)) { 250 munmap(map, 2 * page_size); 251 ksft_exit_fail_msg("munlock(): %s\n", strerror(errno)); 252 } 253 254 ksft_test_result(!unlock_onfault_check(map), "VMA open lock after fault\n"); 255 munmap(map, 2 * page_size); 256 } 257 258 static void test_lock_onfault_of_present(void) 259 { 260 char *map; 261 unsigned long page_size = getpagesize(); 262 263 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 264 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 265 if (map == MAP_FAILED) 266 ksft_exit_fail_msg("mmap error: %s", strerror(errno)); 267 268 *map = 'a'; 269 270 if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) { 271 munmap(map, 2 * page_size); 272 ksft_test_result_fail("mlock2(MLOCK_ONFAULT) error: %s", strerror(errno)); 273 } 274 275 ksft_test_result(is_vma_lock_on_fault((unsigned long)map) || 276 is_vma_lock_on_fault((unsigned long)map + page_size), 277 "VMA with present pages is not marked lock on fault\n"); 278 munmap(map, 2 * page_size); 279 } 280 281 static void test_munlockall0(void) 282 { 283 char *map; 284 unsigned long page_size = getpagesize(); 285 286 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 287 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 288 if (map == MAP_FAILED) 289 ksft_exit_fail_msg("mmap error: %s\n", strerror(errno)); 290 291 if (mlockall(MCL_CURRENT)) { 292 munmap(map, 2 * page_size); 293 ksft_exit_fail_msg("mlockall(MCL_CURRENT): %s\n", strerror(errno)); 294 } 295 296 ksft_test_result(lock_check((unsigned long)map), "%s: Locked memory area\n", __func__); 297 298 if (munlockall()) { 299 munmap(map, 2 * page_size); 300 ksft_exit_fail_msg("munlockall(): %s\n", strerror(errno)); 301 } 302 303 ksft_test_result(!unlock_lock_check(map, true), "%s: No locked memory\n", __func__); 304 munmap(map, 2 * page_size); 305 } 306 307 static void test_munlockall1(void) 308 { 309 char *map; 310 unsigned long page_size = getpagesize(); 311 312 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 313 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 314 if (map == MAP_FAILED) 315 ksft_exit_fail_msg("mmap error: %s", strerror(errno)); 316 317 if (mlockall(MCL_CURRENT | MCL_ONFAULT)) { 318 munmap(map, 2 * page_size); 319 ksft_exit_fail_msg("mlockall(MCL_CURRENT | MCL_ONFAULT): %s\n", strerror(errno)); 320 } 321 322 ksft_test_result(!onfault_check(map), "%s: VMA marked for lock on fault\n", __func__); 323 324 if (munlockall()) { 325 munmap(map, 2 * page_size); 326 ksft_exit_fail_msg("munlockall(): %s\n", strerror(errno)); 327 } 328 329 ksft_test_result(!unlock_onfault_check(map), "%s: Unlocked\n", __func__); 330 331 if (mlockall(MCL_CURRENT | MCL_FUTURE)) { 332 munmap(map, 2 * page_size); 333 ksft_exit_fail_msg("mlockall(MCL_CURRENT | MCL_FUTURE): %s\n", strerror(errno)); 334 } 335 336 ksft_test_result(lock_check((unsigned long)map), "%s: Locked\n", __func__); 337 338 if (munlockall()) { 339 munmap(map, 2 * page_size); 340 ksft_exit_fail_msg("munlockall() %s\n", strerror(errno)); 341 } 342 343 ksft_test_result(!unlock_lock_check(map, true), "%s: No locked memory\n", __func__); 344 munmap(map, 2 * page_size); 345 } 346 347 /* Droppable memory should not be lockable. */ 348 static void test_mlock_droppable(void) 349 { 350 char *map; 351 unsigned long page_size = getpagesize(); 352 353 /* Ensure MCL_FUTURE is not set. */ 354 if (munlockall()) { 355 ksft_test_result_fail("munlockall() %s\n", strerror(errno)); 356 return; 357 } 358 359 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 360 MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0); 361 if (map == MAP_FAILED) { 362 if ((errno == EOPNOTSUPP) || (errno == EINVAL)) 363 ksft_test_result_skip("%s: MAP_DROPPABLE not supported\n", __func__); 364 else 365 ksft_test_result_fail("mmap error: %s\n", strerror(errno)); 366 return; 367 } 368 369 if (mlock2_(map, 2 * page_size, 0)) 370 ksft_test_result_fail("mlock2(0): %s\n", strerror(errno)); 371 else 372 ksft_test_result(!unlock_lock_check(map, false), 373 "%s: droppable memory not locked\n", __func__); 374 375 munmap(map, 2 * page_size); 376 } 377 378 static void test_mlockall_future_droppable(void) 379 { 380 char *map; 381 unsigned long page_size = getpagesize(); 382 383 if (mlockall(MCL_CURRENT | MCL_FUTURE)) { 384 ksft_test_result_fail("mlockall(MCL_CURRENT | MCL_FUTURE): %s\n", strerror(errno)); 385 return; 386 } 387 388 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE, 389 MAP_ANONYMOUS | MAP_DROPPABLE, -1, 0); 390 391 if (map == MAP_FAILED) { 392 if ((errno == EOPNOTSUPP) || (errno == EINVAL)) 393 ksft_test_result_skip("%s: MAP_DROPPABLE not supported\n", __func__); 394 else 395 ksft_test_result_fail("mmap error: %s\n", strerror(errno)); 396 munlockall(); 397 return; 398 } 399 400 ksft_test_result(!unlock_lock_check(map, false), "%s: droppable memory not locked\n", 401 __func__); 402 403 munlockall(); 404 munmap(map, 2 * page_size); 405 } 406 407 static void test_vma_management(bool call_mlock) 408 { 409 void *map; 410 unsigned long page_size = getpagesize(); 411 struct vm_boundaries page1; 412 struct vm_boundaries page2; 413 struct vm_boundaries page3; 414 415 map = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE, 416 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 417 if (map == MAP_FAILED) 418 ksft_exit_fail_msg("mmap error: %s", strerror(errno)); 419 420 if (call_mlock && mlock2_(map, 3 * page_size, MLOCK_ONFAULT)) { 421 munmap(map, 3 * page_size); 422 ksft_test_result_fail("mlock error: %s", strerror(errno)); 423 } 424 425 if (get_vm_area((unsigned long)map, &page1) || 426 get_vm_area((unsigned long)map + page_size, &page2) || 427 get_vm_area((unsigned long)map + page_size * 2, &page3)) { 428 munmap(map, 3 * page_size); 429 ksft_test_result_fail("couldn't find mapping in /proc/self/maps"); 430 } 431 432 /* 433 * Before we unlock a portion, we need to that all three pages are in 434 * the same VMA. If they are not we abort this test (Note that this is 435 * not a failure) 436 */ 437 if (page1.start != page2.start || page2.start != page3.start) { 438 munmap(map, 3 * page_size); 439 ksft_test_result_fail("VMAs are not merged to start, aborting test"); 440 } 441 442 if (munlock(map + page_size, page_size)) { 443 munmap(map, 3 * page_size); 444 ksft_test_result_fail("munlock(): %s", strerror(errno)); 445 } 446 447 if (get_vm_area((unsigned long)map, &page1) || 448 get_vm_area((unsigned long)map + page_size, &page2) || 449 get_vm_area((unsigned long)map + page_size * 2, &page3)) { 450 munmap(map, 3 * page_size); 451 ksft_test_result_fail("couldn't find mapping in /proc/self/maps"); 452 } 453 454 /* All three VMAs should be different */ 455 if (page1.start == page2.start || page2.start == page3.start) { 456 munmap(map, 3 * page_size); 457 ksft_test_result_fail("failed to split VMA for munlock"); 458 } 459 460 /* Now unlock the first and third page and check the VMAs again */ 461 if (munlock(map, page_size * 3)) { 462 munmap(map, 3 * page_size); 463 ksft_test_result_fail("munlock(): %s", strerror(errno)); 464 } 465 466 if (get_vm_area((unsigned long)map, &page1) || 467 get_vm_area((unsigned long)map + page_size, &page2) || 468 get_vm_area((unsigned long)map + page_size * 2, &page3)) { 469 munmap(map, 3 * page_size); 470 ksft_test_result_fail("couldn't find mapping in /proc/self/maps"); 471 } 472 473 /* Now all three VMAs should be the same */ 474 if (page1.start != page2.start || page2.start != page3.start) { 475 munmap(map, 3 * page_size); 476 ksft_test_result_fail("failed to merge VMAs after munlock"); 477 } 478 479 ksft_test_result_pass("%s call_mlock %d\n", __func__, call_mlock); 480 munmap(map, 3 * page_size); 481 } 482 483 static void test_mlockall(void) 484 { 485 if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE)) 486 ksft_exit_fail_msg("mlockall failed: %s\n", strerror(errno)); 487 488 test_vma_management(false); 489 munlockall(); 490 } 491 492 int main(int argc, char **argv) 493 { 494 int ret, size = 3 * getpagesize(); 495 void *map; 496 497 ksft_print_header(); 498 499 map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 500 if (map == MAP_FAILED) 501 ksft_exit_fail_msg("mmap error: %s", strerror(errno)); 502 503 ret = mlock2_(map, size, MLOCK_ONFAULT); 504 if (ret && errno == ENOSYS) 505 ksft_finished(); 506 507 munmap(map, size); 508 509 ksft_set_plan(15); 510 511 test_mlock_lock(); 512 test_mlock_onfault(); 513 test_munlockall0(); 514 test_munlockall1(); 515 test_lock_onfault_of_present(); 516 test_vma_management(true); 517 test_mlockall(); 518 test_mlock_droppable(); 519 test_mlockall_future_droppable(); 520 521 ksft_finished(); 522 } 523