1 // SPDX-License-Identifier: GPL-2.0
8 #include <sys/resource.h>
12 struct vm_boundaries
{
17 static int get_vm_area(unsigned long addr
, struct vm_boundaries
*area
)
21 char line
[1024] = {0};
30 file
= fopen("/proc/self/maps", "r");
36 memset(area
, 0, sizeof(struct vm_boundaries
));
38 while(fgets(line
, 1024, file
)) {
39 end_addr
= strchr(line
, '-');
41 printf("cannot parse /proc/self/maps\n");
46 stop
= strchr(end_addr
, ' ');
48 printf("cannot parse /proc/self/maps\n");
53 sscanf(line
, "%lx", &start
);
54 sscanf(end_addr
, "%lx", &end
);
56 if (start
<= addr
&& end
> addr
) {
68 static uint64_t get_pageflags(unsigned long addr
)
74 file
= fopen("/proc/self/pagemap", "r");
76 perror("fopen pagemap");
80 offset
= addr
/ getpagesize() * sizeof(pfn
);
82 if (fseek(file
, offset
, SEEK_SET
)) {
83 perror("fseek pagemap");
87 if (fread(&pfn
, sizeof(pfn
), 1, file
) != 1) {
88 perror("fread pagemap");
96 static uint64_t get_kpageflags(unsigned long pfn
)
101 file
= fopen("/proc/kpageflags", "r");
103 perror("fopen kpageflags");
107 if (fseek(file
, pfn
* sizeof(flags
), SEEK_SET
)) {
108 perror("fseek kpageflags");
112 if (fread(&flags
, sizeof(flags
), 1, file
) != 1) {
113 perror("fread kpageflags");
121 #define VMFLAGS "VmFlags:"
123 static bool is_vmflag_set(unsigned long addr
, const char *vmflag
)
131 smaps
= seek_to_smaps_entry(addr
);
133 printf("Unable to parse /proc/self/smaps\n");
137 while (getline(&line
, &size
, smaps
) > 0) {
138 if (!strstr(line
, VMFLAGS
)) {
145 flags
= line
+ strlen(VMFLAGS
);
146 ret
= (strstr(flags
, vmflag
) != NULL
);
160 static bool is_vma_lock_on_fault(unsigned long addr
)
165 unsigned long vma_size
, vma_rss
;
170 locked
= is_vmflag_set(addr
, LOCKED
);
174 smaps
= seek_to_smaps_entry(addr
);
176 printf("Unable to parse /proc/self/smaps\n");
180 while (getline(&line
, &size
, smaps
) > 0) {
181 if (!strstr(line
, SIZE
)) {
188 value
= line
+ strlen(SIZE
);
189 if (sscanf(value
, "%lu kB", &vma_size
) < 1) {
190 printf("Unable to parse smaps entry for Size\n");
196 while (getline(&line
, &size
, smaps
) > 0) {
197 if (!strstr(line
, RSS
)) {
204 value
= line
+ strlen(RSS
);
205 if (sscanf(value
, "%lu kB", &vma_rss
) < 1) {
206 printf("Unable to parse smaps entry for Rss\n");
212 ret
= locked
&& (vma_rss
< vma_size
);
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");
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");
249 if (!is_vmflag_set((unsigned long)map
, LOCKED
)) {
250 printf("VMA flag %s is missing on page 1\n", LOCKED
);
254 if (!is_vmflag_set((unsigned long)map
+ page_size
, LOCKED
)) {
255 printf("VMA flag %s is missing on page 2\n", LOCKED
);
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");
277 if (is_vmflag_set((unsigned long)map
, LOCKED
)) {
278 printf("VMA flag %s is present on page 1 after unlock\n", LOCKED
);
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
);
290 static int test_mlock_lock()
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");
303 if (mlock2_(map
, 2 * page_size
, 0)) {
304 if (errno
== ENOSYS
) {
305 printf("Cannot call new mlock family, skipping test\n");
315 /* Now unlock and recheck attributes */
316 if (munlock(map
, 2 * page_size
)) {
321 ret
= unlock_lock_check(map
);
324 munmap(map
, 2 * page_size
);
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");
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");
351 } else if (page2_flags
& PRESENT_BIT
) {
352 printf("Page 2 was made present\n");
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");
364 if (!is_vma_lock_on_fault((unsigned long)map
)) {
365 printf("VMA is not marked for lock on fault\n");
369 if (!is_vma_lock_on_fault((unsigned long)map
+ page_size
)) {
370 printf("VMA is not marked for lock on fault\n");
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");
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");
399 static int test_mlock_onfault()
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");
412 if (mlock2_(map
, 2 * page_size
, MLOCK_ONFAULT
)) {
413 if (errno
== ENOSYS
) {
414 printf("Cannot call new mlock family, skipping test\n");
417 perror("mlock2(MLOCK_ONFAULT)");
421 if (onfault_check(map
))
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");
434 ret
= unlock_onfault_check(map
);
436 munmap(map
, 2 * page_size
);
441 static int test_lock_onfault_of_present()
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");
457 if (mlock2_(map
, 2 * page_size
, MLOCK_ONFAULT
)) {
458 if (errno
== ENOSYS
) {
459 printf("Cannot call new mlock family, skipping test\n");
462 perror("mlock2(MLOCK_ONFAULT)");
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");
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");
484 munmap(map
, 2 * page_size
);
489 static int test_munlockall()
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");
503 if (mlockall(MCL_CURRENT
)) {
504 perror("mlockall(MCL_CURRENT)");
512 perror("munlockall()");
516 if (unlock_lock_check(map
))
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");
529 if (mlockall(MCL_CURRENT
| MCL_ONFAULT
)) {
530 perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
534 if (onfault_check(map
))
538 perror("munlockall()");
542 if (unlock_onfault_check(map
))
545 if (mlockall(MCL_CURRENT
| MCL_FUTURE
)) {
546 perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
554 perror("munlockall()");
558 ret
= unlock_lock_check(map
);
561 munmap(map
, 2 * page_size
);
567 static int test_vma_management(bool call_mlock
)
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
) {
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");
588 perror("mlock(ONFAULT)\n");
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");
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
604 if (page1
.start
!= page2
.start
|| page2
.start
!= page3
.start
) {
605 printf("VMAs are not merged to start, aborting test\n");
610 if (munlock(map
+ page_size
, page_size
)) {
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");
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");
628 /* Now unlock the first and third page and check the VMAs again */
629 if (munlock(map
, page_size
* 3)) {
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");
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");
649 munmap(map
, 3 * page_size
);
653 static int test_mlockall(int (test_function
)(bool call_mlock
))
657 if (mlockall(MCL_CURRENT
| MCL_ONFAULT
| MCL_FUTURE
)) {
662 ret
= test_function(false);
667 int main(int argc
, char **argv
)
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
);