staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / vm / mlock2-tests.c
blob637b6d0ac0d0bf63d88ff5f5782a65453b486a7a
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 #include "../kselftest.h"
14 struct vm_boundaries {
15 unsigned long start;
16 unsigned long end;
19 static int get_vm_area(unsigned long addr, struct vm_boundaries *area)
21 FILE *file;
22 int ret = 1;
23 char line[1024] = {0};
24 char *end_addr;
25 char *stop;
26 unsigned long start;
27 unsigned long end;
29 if (!area)
30 return ret;
32 file = fopen("/proc/self/maps", "r");
33 if (!file) {
34 perror("fopen");
35 return ret;
38 memset(area, 0, sizeof(struct vm_boundaries));
40 while(fgets(line, 1024, file)) {
41 end_addr = strchr(line, '-');
42 if (!end_addr) {
43 printf("cannot parse /proc/self/maps\n");
44 goto out;
46 *end_addr = '\0';
47 end_addr++;
48 stop = strchr(end_addr, ' ');
49 if (!stop) {
50 printf("cannot parse /proc/self/maps\n");
51 goto out;
53 stop = '\0';
55 sscanf(line, "%lx", &start);
56 sscanf(end_addr, "%lx", &end);
58 if (start <= addr && end > addr) {
59 area->start = start;
60 area->end = end;
61 ret = 0;
62 goto out;
65 out:
66 fclose(file);
67 return ret;
70 static uint64_t get_pageflags(unsigned long addr)
72 FILE *file;
73 uint64_t pfn;
74 unsigned long offset;
76 file = fopen("/proc/self/pagemap", "r");
77 if (!file) {
78 perror("fopen pagemap");
79 _exit(1);
82 offset = addr / getpagesize() * sizeof(pfn);
84 if (fseek(file, offset, SEEK_SET)) {
85 perror("fseek pagemap");
86 _exit(1);
89 if (fread(&pfn, sizeof(pfn), 1, file) != 1) {
90 perror("fread pagemap");
91 _exit(1);
94 fclose(file);
95 return pfn;
98 static uint64_t get_kpageflags(unsigned long pfn)
100 uint64_t flags;
101 FILE *file;
103 file = fopen("/proc/kpageflags", "r");
104 if (!file) {
105 perror("fopen kpageflags");
106 _exit(1);
109 if (fseek(file, pfn * sizeof(flags), SEEK_SET)) {
110 perror("fseek kpageflags");
111 _exit(1);
114 if (fread(&flags, sizeof(flags), 1, file) != 1) {
115 perror("fread kpageflags");
116 _exit(1);
119 fclose(file);
120 return flags;
123 #define VMFLAGS "VmFlags:"
125 static bool is_vmflag_set(unsigned long addr, const char *vmflag)
127 char *line = NULL;
128 char *flags;
129 size_t size = 0;
130 bool ret = false;
131 FILE *smaps;
133 smaps = seek_to_smaps_entry(addr);
134 if (!smaps) {
135 printf("Unable to parse /proc/self/smaps\n");
136 goto out;
139 while (getline(&line, &size, smaps) > 0) {
140 if (!strstr(line, VMFLAGS)) {
141 free(line);
142 line = NULL;
143 size = 0;
144 continue;
147 flags = line + strlen(VMFLAGS);
148 ret = (strstr(flags, vmflag) != NULL);
149 goto out;
152 out:
153 free(line);
154 fclose(smaps);
155 return ret;
158 #define SIZE "Size:"
159 #define RSS "Rss:"
160 #define LOCKED "lo"
162 static bool is_vma_lock_on_fault(unsigned long addr)
164 bool ret = false;
165 bool locked;
166 FILE *smaps = NULL;
167 unsigned long vma_size, vma_rss;
168 char *line = NULL;
169 char *value;
170 size_t size = 0;
172 locked = is_vmflag_set(addr, LOCKED);
173 if (!locked)
174 goto out;
176 smaps = seek_to_smaps_entry(addr);
177 if (!smaps) {
178 printf("Unable to parse /proc/self/smaps\n");
179 goto out;
182 while (getline(&line, &size, smaps) > 0) {
183 if (!strstr(line, SIZE)) {
184 free(line);
185 line = NULL;
186 size = 0;
187 continue;
190 value = line + strlen(SIZE);
191 if (sscanf(value, "%lu kB", &vma_size) < 1) {
192 printf("Unable to parse smaps entry for Size\n");
193 goto out;
195 break;
198 while (getline(&line, &size, smaps) > 0) {
199 if (!strstr(line, RSS)) {
200 free(line);
201 line = NULL;
202 size = 0;
203 continue;
206 value = line + strlen(RSS);
207 if (sscanf(value, "%lu kB", &vma_rss) < 1) {
208 printf("Unable to parse smaps entry for Rss\n");
209 goto out;
211 break;
214 ret = locked && (vma_rss < vma_size);
215 out:
216 free(line);
217 if (smaps)
218 fclose(smaps);
219 return ret;
222 #define PRESENT_BIT 0x8000000000000000ULL
223 #define PFN_MASK 0x007FFFFFFFFFFFFFULL
224 #define UNEVICTABLE_BIT (1UL << 18)
226 static int lock_check(char *map)
228 unsigned long page_size = getpagesize();
229 uint64_t page1_flags, page2_flags;
231 page1_flags = get_pageflags((unsigned long)map);
232 page2_flags = get_pageflags((unsigned long)map + page_size);
234 /* Both pages should be present */
235 if (((page1_flags & PRESENT_BIT) == 0) ||
236 ((page2_flags & PRESENT_BIT) == 0)) {
237 printf("Failed to make both pages present\n");
238 return 1;
241 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
242 page2_flags = get_kpageflags(page2_flags & PFN_MASK);
244 /* Both pages should be unevictable */
245 if (((page1_flags & UNEVICTABLE_BIT) == 0) ||
246 ((page2_flags & UNEVICTABLE_BIT) == 0)) {
247 printf("Failed to make both pages unevictable\n");
248 return 1;
251 if (!is_vmflag_set((unsigned long)map, LOCKED)) {
252 printf("VMA flag %s is missing on page 1\n", LOCKED);
253 return 1;
256 if (!is_vmflag_set((unsigned long)map + page_size, LOCKED)) {
257 printf("VMA flag %s is missing on page 2\n", LOCKED);
258 return 1;
261 return 0;
264 static int unlock_lock_check(char *map)
266 unsigned long page_size = getpagesize();
267 uint64_t page1_flags, page2_flags;
269 page1_flags = get_pageflags((unsigned long)map);
270 page2_flags = get_pageflags((unsigned long)map + page_size);
271 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
272 page2_flags = get_kpageflags(page2_flags & PFN_MASK);
274 if ((page1_flags & UNEVICTABLE_BIT) || (page2_flags & UNEVICTABLE_BIT)) {
275 printf("A page is still marked unevictable after unlock\n");
276 return 1;
279 if (is_vmflag_set((unsigned long)map, LOCKED)) {
280 printf("VMA flag %s is present on page 1 after unlock\n", LOCKED);
281 return 1;
284 if (is_vmflag_set((unsigned long)map + page_size, LOCKED)) {
285 printf("VMA flag %s is present on page 2 after unlock\n", LOCKED);
286 return 1;
289 return 0;
292 static int test_mlock_lock()
294 char *map;
295 int ret = 1;
296 unsigned long page_size = getpagesize();
298 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
299 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
300 if (map == MAP_FAILED) {
301 perror("test_mlock_locked mmap");
302 goto out;
305 if (mlock2_(map, 2 * page_size, 0)) {
306 if (errno == ENOSYS) {
307 printf("Cannot call new mlock family, skipping test\n");
308 _exit(KSFT_SKIP);
310 perror("mlock2(0)");
311 goto unmap;
314 if (lock_check(map))
315 goto unmap;
317 /* Now unlock and recheck attributes */
318 if (munlock(map, 2 * page_size)) {
319 perror("munlock()");
320 goto unmap;
323 ret = unlock_lock_check(map);
325 unmap:
326 munmap(map, 2 * page_size);
327 out:
328 return ret;
331 static int onfault_check(char *map)
333 unsigned long page_size = getpagesize();
334 uint64_t page1_flags, page2_flags;
336 page1_flags = get_pageflags((unsigned long)map);
337 page2_flags = get_pageflags((unsigned long)map + page_size);
339 /* Neither page should be present */
340 if ((page1_flags & PRESENT_BIT) || (page2_flags & PRESENT_BIT)) {
341 printf("Pages were made present by MLOCK_ONFAULT\n");
342 return 1;
345 *map = 'a';
346 page1_flags = get_pageflags((unsigned long)map);
347 page2_flags = get_pageflags((unsigned long)map + page_size);
349 /* Only page 1 should be present */
350 if ((page1_flags & PRESENT_BIT) == 0) {
351 printf("Page 1 is not present after fault\n");
352 return 1;
353 } else if (page2_flags & PRESENT_BIT) {
354 printf("Page 2 was made present\n");
355 return 1;
358 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
360 /* Page 1 should be unevictable */
361 if ((page1_flags & UNEVICTABLE_BIT) == 0) {
362 printf("Failed to make faulted page unevictable\n");
363 return 1;
366 if (!is_vma_lock_on_fault((unsigned long)map)) {
367 printf("VMA is not marked for lock on fault\n");
368 return 1;
371 if (!is_vma_lock_on_fault((unsigned long)map + page_size)) {
372 printf("VMA is not marked for lock on fault\n");
373 return 1;
376 return 0;
379 static int unlock_onfault_check(char *map)
381 unsigned long page_size = getpagesize();
382 uint64_t page1_flags;
384 page1_flags = get_pageflags((unsigned long)map);
385 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
387 if (page1_flags & UNEVICTABLE_BIT) {
388 printf("Page 1 is still marked unevictable after unlock\n");
389 return 1;
392 if (is_vma_lock_on_fault((unsigned long)map) ||
393 is_vma_lock_on_fault((unsigned long)map + page_size)) {
394 printf("VMA is still lock on fault after unlock\n");
395 return 1;
398 return 0;
401 static int test_mlock_onfault()
403 char *map;
404 int ret = 1;
405 unsigned long page_size = getpagesize();
407 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
408 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
409 if (map == MAP_FAILED) {
410 perror("test_mlock_locked mmap");
411 goto out;
414 if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
415 if (errno == ENOSYS) {
416 printf("Cannot call new mlock family, skipping test\n");
417 _exit(KSFT_SKIP);
419 perror("mlock2(MLOCK_ONFAULT)");
420 goto unmap;
423 if (onfault_check(map))
424 goto unmap;
426 /* Now unlock and recheck attributes */
427 if (munlock(map, 2 * page_size)) {
428 if (errno == ENOSYS) {
429 printf("Cannot call new mlock family, skipping test\n");
430 _exit(KSFT_SKIP);
432 perror("munlock()");
433 goto unmap;
436 ret = unlock_onfault_check(map);
437 unmap:
438 munmap(map, 2 * page_size);
439 out:
440 return ret;
443 static int test_lock_onfault_of_present()
445 char *map;
446 int ret = 1;
447 unsigned long page_size = getpagesize();
448 uint64_t page1_flags, page2_flags;
450 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
451 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
452 if (map == MAP_FAILED) {
453 perror("test_mlock_locked mmap");
454 goto out;
457 *map = 'a';
459 if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
460 if (errno == ENOSYS) {
461 printf("Cannot call new mlock family, skipping test\n");
462 _exit(KSFT_SKIP);
464 perror("mlock2(MLOCK_ONFAULT)");
465 goto unmap;
468 page1_flags = get_pageflags((unsigned long)map);
469 page2_flags = get_pageflags((unsigned long)map + page_size);
470 page1_flags = get_kpageflags(page1_flags & PFN_MASK);
471 page2_flags = get_kpageflags(page2_flags & PFN_MASK);
473 /* Page 1 should be unevictable */
474 if ((page1_flags & UNEVICTABLE_BIT) == 0) {
475 printf("Failed to make present page unevictable\n");
476 goto unmap;
479 if (!is_vma_lock_on_fault((unsigned long)map) ||
480 !is_vma_lock_on_fault((unsigned long)map + page_size)) {
481 printf("VMA with present pages is not marked lock on fault\n");
482 goto unmap;
484 ret = 0;
485 unmap:
486 munmap(map, 2 * page_size);
487 out:
488 return ret;
491 static int test_munlockall()
493 char *map;
494 int ret = 1;
495 unsigned long page_size = getpagesize();
497 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
498 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
500 if (map == MAP_FAILED) {
501 perror("test_munlockall mmap");
502 goto out;
505 if (mlockall(MCL_CURRENT)) {
506 perror("mlockall(MCL_CURRENT)");
507 goto out;
510 if (lock_check(map))
511 goto unmap;
513 if (munlockall()) {
514 perror("munlockall()");
515 goto unmap;
518 if (unlock_lock_check(map))
519 goto unmap;
521 munmap(map, 2 * page_size);
523 map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
524 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
526 if (map == MAP_FAILED) {
527 perror("test_munlockall second mmap");
528 goto out;
531 if (mlockall(MCL_CURRENT | MCL_ONFAULT)) {
532 perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
533 goto unmap;
536 if (onfault_check(map))
537 goto unmap;
539 if (munlockall()) {
540 perror("munlockall()");
541 goto unmap;
544 if (unlock_onfault_check(map))
545 goto unmap;
547 if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
548 perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
549 goto out;
552 if (lock_check(map))
553 goto unmap;
555 if (munlockall()) {
556 perror("munlockall()");
557 goto unmap;
560 ret = unlock_lock_check(map);
562 unmap:
563 munmap(map, 2 * page_size);
564 out:
565 munlockall();
566 return ret;
569 static int test_vma_management(bool call_mlock)
571 int ret = 1;
572 void *map;
573 unsigned long page_size = getpagesize();
574 struct vm_boundaries page1;
575 struct vm_boundaries page2;
576 struct vm_boundaries page3;
578 map = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
579 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
580 if (map == MAP_FAILED) {
581 perror("mmap()");
582 return ret;
585 if (call_mlock && mlock2_(map, 3 * page_size, MLOCK_ONFAULT)) {
586 if (errno == ENOSYS) {
587 printf("Cannot call new mlock family, skipping test\n");
588 _exit(KSFT_SKIP);
590 perror("mlock(ONFAULT)\n");
591 goto out;
594 if (get_vm_area((unsigned long)map, &page1) ||
595 get_vm_area((unsigned long)map + page_size, &page2) ||
596 get_vm_area((unsigned long)map + page_size * 2, &page3)) {
597 printf("couldn't find mapping in /proc/self/maps\n");
598 goto out;
602 * Before we unlock a portion, we need to that all three pages are in
603 * the same VMA. If they are not we abort this test (Note that this is
604 * not a failure)
606 if (page1.start != page2.start || page2.start != page3.start) {
607 printf("VMAs are not merged to start, aborting test\n");
608 ret = 0;
609 goto out;
612 if (munlock(map + page_size, page_size)) {
613 perror("munlock()");
614 goto out;
617 if (get_vm_area((unsigned long)map, &page1) ||
618 get_vm_area((unsigned long)map + page_size, &page2) ||
619 get_vm_area((unsigned long)map + page_size * 2, &page3)) {
620 printf("couldn't find mapping in /proc/self/maps\n");
621 goto out;
624 /* All three VMAs should be different */
625 if (page1.start == page2.start || page2.start == page3.start) {
626 printf("failed to split VMA for munlock\n");
627 goto out;
630 /* Now unlock the first and third page and check the VMAs again */
631 if (munlock(map, page_size * 3)) {
632 perror("munlock()");
633 goto out;
636 if (get_vm_area((unsigned long)map, &page1) ||
637 get_vm_area((unsigned long)map + page_size, &page2) ||
638 get_vm_area((unsigned long)map + page_size * 2, &page3)) {
639 printf("couldn't find mapping in /proc/self/maps\n");
640 goto out;
643 /* Now all three VMAs should be the same */
644 if (page1.start != page2.start || page2.start != page3.start) {
645 printf("failed to merge VMAs after munlock\n");
646 goto out;
649 ret = 0;
650 out:
651 munmap(map, 3 * page_size);
652 return ret;
655 static int test_mlockall(int (test_function)(bool call_mlock))
657 int ret = 1;
659 if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE)) {
660 perror("mlockall");
661 return ret;
664 ret = test_function(false);
665 munlockall();
666 return ret;
669 int main(int argc, char **argv)
671 int ret = 0;
672 ret += test_mlock_lock();
673 ret += test_mlock_onfault();
674 ret += test_munlockall();
675 ret += test_lock_onfault_of_present();
676 ret += test_vma_management(true);
677 ret += test_mlockall(test_vma_management);
678 return ret;