1 // SPDX-License-Identifier: GPL-2.0
8 #include <sys/resource.h>
12 #include "../kselftest.h"
14 struct vm_boundaries
{
19 static int get_vm_area(unsigned long addr
, struct vm_boundaries
*area
)
23 char line
[1024] = {0};
32 file
= fopen("/proc/self/maps", "r");
38 memset(area
, 0, sizeof(struct vm_boundaries
));
40 while(fgets(line
, 1024, file
)) {
41 end_addr
= strchr(line
, '-');
43 printf("cannot parse /proc/self/maps\n");
48 stop
= strchr(end_addr
, ' ');
50 printf("cannot parse /proc/self/maps\n");
55 sscanf(line
, "%lx", &start
);
56 sscanf(end_addr
, "%lx", &end
);
58 if (start
<= addr
&& end
> addr
) {
70 static uint64_t get_pageflags(unsigned long addr
)
76 file
= fopen("/proc/self/pagemap", "r");
78 perror("fopen pagemap");
82 offset
= addr
/ getpagesize() * sizeof(pfn
);
84 if (fseek(file
, offset
, SEEK_SET
)) {
85 perror("fseek pagemap");
89 if (fread(&pfn
, sizeof(pfn
), 1, file
) != 1) {
90 perror("fread pagemap");
98 static uint64_t get_kpageflags(unsigned long pfn
)
103 file
= fopen("/proc/kpageflags", "r");
105 perror("fopen kpageflags");
109 if (fseek(file
, pfn
* sizeof(flags
), SEEK_SET
)) {
110 perror("fseek kpageflags");
114 if (fread(&flags
, sizeof(flags
), 1, file
) != 1) {
115 perror("fread kpageflags");
123 #define VMFLAGS "VmFlags:"
125 static bool is_vmflag_set(unsigned long addr
, const char *vmflag
)
133 smaps
= seek_to_smaps_entry(addr
);
135 printf("Unable to parse /proc/self/smaps\n");
139 while (getline(&line
, &size
, smaps
) > 0) {
140 if (!strstr(line
, VMFLAGS
)) {
147 flags
= line
+ strlen(VMFLAGS
);
148 ret
= (strstr(flags
, vmflag
) != NULL
);
162 static bool is_vma_lock_on_fault(unsigned long addr
)
167 unsigned long vma_size
, vma_rss
;
172 locked
= is_vmflag_set(addr
, LOCKED
);
176 smaps
= seek_to_smaps_entry(addr
);
178 printf("Unable to parse /proc/self/smaps\n");
182 while (getline(&line
, &size
, smaps
) > 0) {
183 if (!strstr(line
, SIZE
)) {
190 value
= line
+ strlen(SIZE
);
191 if (sscanf(value
, "%lu kB", &vma_size
) < 1) {
192 printf("Unable to parse smaps entry for Size\n");
198 while (getline(&line
, &size
, smaps
) > 0) {
199 if (!strstr(line
, RSS
)) {
206 value
= line
+ strlen(RSS
);
207 if (sscanf(value
, "%lu kB", &vma_rss
) < 1) {
208 printf("Unable to parse smaps entry for Rss\n");
214 ret
= locked
&& (vma_rss
< vma_size
);
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");
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");
251 if (!is_vmflag_set((unsigned long)map
, LOCKED
)) {
252 printf("VMA flag %s is missing on page 1\n", LOCKED
);
256 if (!is_vmflag_set((unsigned long)map
+ page_size
, LOCKED
)) {
257 printf("VMA flag %s is missing on page 2\n", LOCKED
);
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");
279 if (is_vmflag_set((unsigned long)map
, LOCKED
)) {
280 printf("VMA flag %s is present on page 1 after unlock\n", LOCKED
);
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
);
292 static int test_mlock_lock()
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");
305 if (mlock2_(map
, 2 * page_size
, 0)) {
306 if (errno
== ENOSYS
) {
307 printf("Cannot call new mlock family, skipping test\n");
317 /* Now unlock and recheck attributes */
318 if (munlock(map
, 2 * page_size
)) {
323 ret
= unlock_lock_check(map
);
326 munmap(map
, 2 * page_size
);
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");
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");
353 } else if (page2_flags
& PRESENT_BIT
) {
354 printf("Page 2 was made present\n");
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");
366 if (!is_vma_lock_on_fault((unsigned long)map
)) {
367 printf("VMA is not marked for lock on fault\n");
371 if (!is_vma_lock_on_fault((unsigned long)map
+ page_size
)) {
372 printf("VMA is not marked for lock on fault\n");
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");
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");
401 static int test_mlock_onfault()
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");
414 if (mlock2_(map
, 2 * page_size
, MLOCK_ONFAULT
)) {
415 if (errno
== ENOSYS
) {
416 printf("Cannot call new mlock family, skipping test\n");
419 perror("mlock2(MLOCK_ONFAULT)");
423 if (onfault_check(map
))
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");
436 ret
= unlock_onfault_check(map
);
438 munmap(map
, 2 * page_size
);
443 static int test_lock_onfault_of_present()
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");
459 if (mlock2_(map
, 2 * page_size
, MLOCK_ONFAULT
)) {
460 if (errno
== ENOSYS
) {
461 printf("Cannot call new mlock family, skipping test\n");
464 perror("mlock2(MLOCK_ONFAULT)");
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");
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");
486 munmap(map
, 2 * page_size
);
491 static int test_munlockall()
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");
505 if (mlockall(MCL_CURRENT
)) {
506 perror("mlockall(MCL_CURRENT)");
514 perror("munlockall()");
518 if (unlock_lock_check(map
))
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");
531 if (mlockall(MCL_CURRENT
| MCL_ONFAULT
)) {
532 perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
536 if (onfault_check(map
))
540 perror("munlockall()");
544 if (unlock_onfault_check(map
))
547 if (mlockall(MCL_CURRENT
| MCL_FUTURE
)) {
548 perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
556 perror("munlockall()");
560 ret
= unlock_lock_check(map
);
563 munmap(map
, 2 * page_size
);
569 static int test_vma_management(bool call_mlock
)
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
) {
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");
590 perror("mlock(ONFAULT)\n");
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");
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
606 if (page1
.start
!= page2
.start
|| page2
.start
!= page3
.start
) {
607 printf("VMAs are not merged to start, aborting test\n");
612 if (munlock(map
+ page_size
, page_size
)) {
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");
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");
630 /* Now unlock the first and third page and check the VMAs again */
631 if (munlock(map
, page_size
* 3)) {
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");
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");
651 munmap(map
, 3 * page_size
);
655 static int test_mlockall(int (test_function
)(bool call_mlock
))
659 if (mlockall(MCL_CURRENT
| MCL_ONFAULT
| MCL_FUTURE
)) {
664 ret
= test_function(false);
669 int main(int argc
, char **argv
)
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
);