Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / testing / selftests / vm / mlock2-tests.c
blob4997b9222cfa5055f9c07f4f1f0a1454bae89d6e
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <sys/mman.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <stdbool.h>
10 #include "mlock2.h"
12 struct vm_boundaries {
13 unsigned long start;
14 unsigned long end;
17 static int get_vm_area(unsigned long addr, struct vm_boundaries *area)
19 FILE *file;
20 int ret = 1;
21 char line[1024] = {0};
22 char *end_addr;
23 char *stop;
24 unsigned long start;
25 unsigned long end;
27 if (!area)
28 return ret;
30 file = fopen("/proc/self/maps", "r");
31 if (!file) {
32 perror("fopen");
33 return ret;
36 memset(area, 0, sizeof(struct vm_boundaries));
38 while(fgets(line, 1024, file)) {
39 end_addr = strchr(line, '-');
40 if (!end_addr) {
41 printf("cannot parse /proc/self/maps\n");
42 goto out;
44 *end_addr = '\0';
45 end_addr++;
46 stop = strchr(end_addr, ' ');
47 if (!stop) {
48 printf("cannot parse /proc/self/maps\n");
49 goto out;
51 stop = '\0';
53 sscanf(line, "%lx", &start);
54 sscanf(end_addr, "%lx", &end);
56 if (start <= addr && end > addr) {
57 area->start = start;
58 area->end = end;
59 ret = 0;
60 goto out;
63 out:
64 fclose(file);
65 return ret;
68 static uint64_t get_pageflags(unsigned long addr)
70 FILE *file;
71 uint64_t pfn;
72 unsigned long offset;
74 file = fopen("/proc/self/pagemap", "r");
75 if (!file) {
76 perror("fopen pagemap");
77 _exit(1);
80 offset = addr / getpagesize() * sizeof(pfn);
82 if (fseek(file, offset, SEEK_SET)) {
83 perror("fseek pagemap");
84 _exit(1);
87 if (fread(&pfn, sizeof(pfn), 1, file) != 1) {
88 perror("fread pagemap");
89 _exit(1);
92 fclose(file);
93 return pfn;
96 static uint64_t get_kpageflags(unsigned long pfn)
98 uint64_t flags;
99 FILE *file;
101 file = fopen("/proc/kpageflags", "r");
102 if (!file) {
103 perror("fopen kpageflags");
104 _exit(1);
107 if (fseek(file, pfn * sizeof(flags), SEEK_SET)) {
108 perror("fseek kpageflags");
109 _exit(1);
112 if (fread(&flags, sizeof(flags), 1, file) != 1) {
113 perror("fread kpageflags");
114 _exit(1);
117 fclose(file);
118 return flags;
121 #define VMFLAGS "VmFlags:"
123 static bool is_vmflag_set(unsigned long addr, const char *vmflag)
125 char *line = NULL;
126 char *flags;
127 size_t size = 0;
128 bool ret = false;
129 FILE *smaps;
131 smaps = seek_to_smaps_entry(addr);
132 if (!smaps) {
133 printf("Unable to parse /proc/self/smaps\n");
134 goto out;
137 while (getline(&line, &size, smaps) > 0) {
138 if (!strstr(line, VMFLAGS)) {
139 free(line);
140 line = NULL;
141 size = 0;
142 continue;
145 flags = line + strlen(VMFLAGS);
146 ret = (strstr(flags, vmflag) != NULL);
147 goto out;
150 out:
151 free(line);
152 fclose(smaps);
153 return ret;
156 #define SIZE "Size:"
157 #define RSS "Rss:"
158 #define LOCKED "lo"
160 static bool is_vma_lock_on_fault(unsigned long addr)
162 bool ret = false;
163 bool locked;
164 FILE *smaps = NULL;
165 unsigned long vma_size, vma_rss;
166 char *line = NULL;
167 char *value;
168 size_t size = 0;
170 locked = is_vmflag_set(addr, LOCKED);
171 if (!locked)
172 goto out;
174 smaps = seek_to_smaps_entry(addr);
175 if (!smaps) {
176 printf("Unable to parse /proc/self/smaps\n");
177 goto out;
180 while (getline(&line, &size, smaps) > 0) {
181 if (!strstr(line, SIZE)) {
182 free(line);
183 line = NULL;
184 size = 0;
185 continue;
188 value = line + strlen(SIZE);
189 if (sscanf(value, "%lu kB", &vma_size) < 1) {
190 printf("Unable to parse smaps entry for Size\n");
191 goto out;
193 break;
196 while (getline(&line, &size, smaps) > 0) {
197 if (!strstr(line, RSS)) {
198 free(line);
199 line = NULL;
200 size = 0;
201 continue;
204 value = line + strlen(RSS);
205 if (sscanf(value, "%lu kB", &vma_rss) < 1) {
206 printf("Unable to parse smaps entry for Rss\n");
207 goto out;
209 break;
212 ret = locked && (vma_rss < vma_size);
213 out:
214 free(line);
215 if (smaps)
216 fclose(smaps);
217 return ret;
220 #define PRESENT_BIT 0x8000000000000000ULL
221 #define PFN_MASK 0x007FFFFFFFFFFFFFULL
222 #define UNEVICTABLE_BIT (1UL << 18)
224 static int lock_check(char *map)
226 unsigned long page_size = getpagesize();
227 uint64_t page1_flags, page2_flags;
229 page1_flags = get_pageflags((unsigned long)map);
230 page2_flags = get_pageflags((unsigned long)map + page_size);
232 /* Both pages should be present */
233 if (((page1_flags & PRESENT_BIT) == 0) ||
234 ((page2_flags & PRESENT_BIT) == 0)) {
235 printf("Failed to make both pages present\n");
236 return 1;
239 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
240 page2_flags = get_kpageflags(page2_flags & PFN_MASK);
242 /* Both pages should be unevictable */
243 if (((page1_flags & UNEVICTABLE_BIT) == 0) ||
244 ((page2_flags & UNEVICTABLE_BIT) == 0)) {
245 printf("Failed to make both pages unevictable\n");
246 return 1;
249 if (!is_vmflag_set((unsigned long)map, LOCKED)) {
250 printf("VMA flag %s is missing on page 1\n", LOCKED);
251 return 1;
254 if (!is_vmflag_set((unsigned long)map + page_size, LOCKED)) {
255 printf("VMA flag %s is missing on page 2\n", LOCKED);
256 return 1;
259 return 0;
262 static int unlock_lock_check(char *map)
264 unsigned long page_size = getpagesize();
265 uint64_t page1_flags, page2_flags;
267 page1_flags = get_pageflags((unsigned long)map);
268 page2_flags = get_pageflags((unsigned long)map + page_size);
269 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
270 page2_flags = get_kpageflags(page2_flags & PFN_MASK);
272 if ((page1_flags & UNEVICTABLE_BIT) || (page2_flags & UNEVICTABLE_BIT)) {
273 printf("A page is still marked unevictable after unlock\n");
274 return 1;
277 if (is_vmflag_set((unsigned long)map, LOCKED)) {
278 printf("VMA flag %s is present on page 1 after unlock\n", LOCKED);
279 return 1;
282 if (is_vmflag_set((unsigned long)map + page_size, LOCKED)) {
283 printf("VMA flag %s is present on page 2 after unlock\n", LOCKED);
284 return 1;
287 return 0;
290 static int test_mlock_lock()
292 char *map;
293 int ret = 1;
294 unsigned long page_size = getpagesize();
296 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
297 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
298 if (map == MAP_FAILED) {
299 perror("test_mlock_locked mmap");
300 goto out;
303 if (mlock2_(map, 2 * page_size, 0)) {
304 if (errno == ENOSYS) {
305 printf("Cannot call new mlock family, skipping test\n");
306 _exit(0);
308 perror("mlock2(0)");
309 goto unmap;
312 if (lock_check(map))
313 goto unmap;
315 /* Now unlock and recheck attributes */
316 if (munlock(map, 2 * page_size)) {
317 perror("munlock()");
318 goto unmap;
321 ret = unlock_lock_check(map);
323 unmap:
324 munmap(map, 2 * page_size);
325 out:
326 return ret;
329 static int onfault_check(char *map)
331 unsigned long page_size = getpagesize();
332 uint64_t page1_flags, page2_flags;
334 page1_flags = get_pageflags((unsigned long)map);
335 page2_flags = get_pageflags((unsigned long)map + page_size);
337 /* Neither page should be present */
338 if ((page1_flags & PRESENT_BIT) || (page2_flags & PRESENT_BIT)) {
339 printf("Pages were made present by MLOCK_ONFAULT\n");
340 return 1;
343 *map = 'a';
344 page1_flags = get_pageflags((unsigned long)map);
345 page2_flags = get_pageflags((unsigned long)map + page_size);
347 /* Only page 1 should be present */
348 if ((page1_flags & PRESENT_BIT) == 0) {
349 printf("Page 1 is not present after fault\n");
350 return 1;
351 } else if (page2_flags & PRESENT_BIT) {
352 printf("Page 2 was made present\n");
353 return 1;
356 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
358 /* Page 1 should be unevictable */
359 if ((page1_flags & UNEVICTABLE_BIT) == 0) {
360 printf("Failed to make faulted page unevictable\n");
361 return 1;
364 if (!is_vma_lock_on_fault((unsigned long)map)) {
365 printf("VMA is not marked for lock on fault\n");
366 return 1;
369 if (!is_vma_lock_on_fault((unsigned long)map + page_size)) {
370 printf("VMA is not marked for lock on fault\n");
371 return 1;
374 return 0;
377 static int unlock_onfault_check(char *map)
379 unsigned long page_size = getpagesize();
380 uint64_t page1_flags;
382 page1_flags = get_pageflags((unsigned long)map);
383 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
385 if (page1_flags & UNEVICTABLE_BIT) {
386 printf("Page 1 is still marked unevictable after unlock\n");
387 return 1;
390 if (is_vma_lock_on_fault((unsigned long)map) ||
391 is_vma_lock_on_fault((unsigned long)map + page_size)) {
392 printf("VMA is still lock on fault after unlock\n");
393 return 1;
396 return 0;
399 static int test_mlock_onfault()
401 char *map;
402 int ret = 1;
403 unsigned long page_size = getpagesize();
405 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
406 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
407 if (map == MAP_FAILED) {
408 perror("test_mlock_locked mmap");
409 goto out;
412 if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
413 if (errno == ENOSYS) {
414 printf("Cannot call new mlock family, skipping test\n");
415 _exit(0);
417 perror("mlock2(MLOCK_ONFAULT)");
418 goto unmap;
421 if (onfault_check(map))
422 goto unmap;
424 /* Now unlock and recheck attributes */
425 if (munlock(map, 2 * page_size)) {
426 if (errno == ENOSYS) {
427 printf("Cannot call new mlock family, skipping test\n");
428 _exit(0);
430 perror("munlock()");
431 goto unmap;
434 ret = unlock_onfault_check(map);
435 unmap:
436 munmap(map, 2 * page_size);
437 out:
438 return ret;
441 static int test_lock_onfault_of_present()
443 char *map;
444 int ret = 1;
445 unsigned long page_size = getpagesize();
446 uint64_t page1_flags, page2_flags;
448 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
449 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
450 if (map == MAP_FAILED) {
451 perror("test_mlock_locked mmap");
452 goto out;
455 *map = 'a';
457 if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
458 if (errno == ENOSYS) {
459 printf("Cannot call new mlock family, skipping test\n");
460 _exit(0);
462 perror("mlock2(MLOCK_ONFAULT)");
463 goto unmap;
466 page1_flags = get_pageflags((unsigned long)map);
467 page2_flags = get_pageflags((unsigned long)map + page_size);
468 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
469 page2_flags = get_kpageflags(page2_flags & PFN_MASK);
471 /* Page 1 should be unevictable */
472 if ((page1_flags & UNEVICTABLE_BIT) == 0) {
473 printf("Failed to make present page unevictable\n");
474 goto unmap;
477 if (!is_vma_lock_on_fault((unsigned long)map) ||
478 !is_vma_lock_on_fault((unsigned long)map + page_size)) {
479 printf("VMA with present pages is not marked lock on fault\n");
480 goto unmap;
482 ret = 0;
483 unmap:
484 munmap(map, 2 * page_size);
485 out:
486 return ret;
489 static int test_munlockall()
491 char *map;
492 int ret = 1;
493 unsigned long page_size = getpagesize();
495 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
496 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
498 if (map == MAP_FAILED) {
499 perror("test_munlockall mmap");
500 goto out;
503 if (mlockall(MCL_CURRENT)) {
504 perror("mlockall(MCL_CURRENT)");
505 goto out;
508 if (lock_check(map))
509 goto unmap;
511 if (munlockall()) {
512 perror("munlockall()");
513 goto unmap;
516 if (unlock_lock_check(map))
517 goto unmap;
519 munmap(map, 2 * page_size);
521 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
522 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
524 if (map == MAP_FAILED) {
525 perror("test_munlockall second mmap");
526 goto out;
529 if (mlockall(MCL_CURRENT | MCL_ONFAULT)) {
530 perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
531 goto unmap;
534 if (onfault_check(map))
535 goto unmap;
537 if (munlockall()) {
538 perror("munlockall()");
539 goto unmap;
542 if (unlock_onfault_check(map))
543 goto unmap;
545 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
546 perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
547 goto out;
550 if (lock_check(map))
551 goto unmap;
553 if (munlockall()) {
554 perror("munlockall()");
555 goto unmap;
558 ret = unlock_lock_check(map);
560 unmap:
561 munmap(map, 2 * page_size);
562 out:
563 munlockall();
564 return ret;
567 static int test_vma_management(bool call_mlock)
569 int ret = 1;
570 void *map;
571 unsigned long page_size = getpagesize();
572 struct vm_boundaries page1;
573 struct vm_boundaries page2;
574 struct vm_boundaries page3;
576 map = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
577 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
578 if (map == MAP_FAILED) {
579 perror("mmap()");
580 return ret;
583 if (call_mlock && mlock2_(map, 3 * page_size, MLOCK_ONFAULT)) {
584 if (errno == ENOSYS) {
585 printf("Cannot call new mlock family, skipping test\n");
586 _exit(0);
588 perror("mlock(ONFAULT)\n");
589 goto out;
592 if (get_vm_area((unsigned long)map, &page1) ||
593 get_vm_area((unsigned long)map + page_size, &page2) ||
594 get_vm_area((unsigned long)map + page_size * 2, &page3)) {
595 printf("couldn't find mapping in /proc/self/maps\n");
596 goto out;
600 * Before we unlock a portion, we need to that all three pages are in
601 * the same VMA. If they are not we abort this test (Note that this is
602 * not a failure)
604 if (page1.start != page2.start || page2.start != page3.start) {
605 printf("VMAs are not merged to start, aborting test\n");
606 ret = 0;
607 goto out;
610 if (munlock(map + page_size, page_size)) {
611 perror("munlock()");
612 goto out;
615 if (get_vm_area((unsigned long)map, &page1) ||
616 get_vm_area((unsigned long)map + page_size, &page2) ||
617 get_vm_area((unsigned long)map + page_size * 2, &page3)) {
618 printf("couldn't find mapping in /proc/self/maps\n");
619 goto out;
622 /* All three VMAs should be different */
623 if (page1.start == page2.start || page2.start == page3.start) {
624 printf("failed to split VMA for munlock\n");
625 goto out;
628 /* Now unlock the first and third page and check the VMAs again */
629 if (munlock(map, page_size * 3)) {
630 perror("munlock()");
631 goto out;
634 if (get_vm_area((unsigned long)map, &page1) ||
635 get_vm_area((unsigned long)map + page_size, &page2) ||
636 get_vm_area((unsigned long)map + page_size * 2, &page3)) {
637 printf("couldn't find mapping in /proc/self/maps\n");
638 goto out;
641 /* Now all three VMAs should be the same */
642 if (page1.start != page2.start || page2.start != page3.start) {
643 printf("failed to merge VMAs after munlock\n");
644 goto out;
647 ret = 0;
648 out:
649 munmap(map, 3 * page_size);
650 return ret;
653 static int test_mlockall(int (test_function)(bool call_mlock))
655 int ret = 1;
657 if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE)) {
658 perror("mlockall");
659 return ret;
662 ret = test_function(false);
663 munlockall();
664 return ret;
667 int main(int argc, char **argv)
669 int ret = 0;
670 ret += test_mlock_lock();
671 ret += test_mlock_onfault();
672 ret += test_munlockall();
673 ret += test_lock_onfault_of_present();
674 ret += test_vma_management(true);
675 ret += test_mlockall(test_vma_management);
676 return ret;