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 #define VMFLAGS "VmFlags:"
72 static bool is_vmflag_set(unsigned long addr
, const char *vmflag
)
80 smaps
= seek_to_smaps_entry(addr
);
82 printf("Unable to parse /proc/self/smaps\n");
86 while (getline(&line
, &size
, smaps
) > 0) {
87 if (!strstr(line
, VMFLAGS
)) {
94 flags
= line
+ strlen(VMFLAGS
);
95 ret
= (strstr(flags
, vmflag
) != NULL
);
109 static unsigned long get_value_for_name(unsigned long addr
, const char *name
)
115 unsigned long value
= -1UL;
117 smaps
= seek_to_smaps_entry(addr
);
119 printf("Unable to parse /proc/self/smaps\n");
123 while (getline(&line
, &size
, smaps
) > 0) {
124 if (!strstr(line
, name
)) {
131 value_ptr
= line
+ strlen(name
);
132 if (sscanf(value_ptr
, "%lu kB", &value
) < 1) {
133 printf("Unable to parse smaps entry for Size\n");
146 static bool is_vma_lock_on_fault(unsigned long addr
)
149 unsigned long vma_size
, vma_rss
;
151 locked
= is_vmflag_set(addr
, LOCKED
);
155 vma_size
= get_value_for_name(addr
, SIZE
);
156 vma_rss
= get_value_for_name(addr
, RSS
);
158 /* only one page is faulted in */
159 return (vma_rss
< vma_size
);
162 #define PRESENT_BIT 0x8000000000000000ULL
163 #define PFN_MASK 0x007FFFFFFFFFFFFFULL
164 #define UNEVICTABLE_BIT (1UL << 18)
166 static int lock_check(unsigned long addr
)
169 unsigned long vma_size
, vma_rss
;
171 locked
= is_vmflag_set(addr
, LOCKED
);
175 vma_size
= get_value_for_name(addr
, SIZE
);
176 vma_rss
= get_value_for_name(addr
, RSS
);
178 return (vma_rss
== vma_size
);
181 static int unlock_lock_check(char *map
)
183 if (is_vmflag_set((unsigned long)map
, LOCKED
)) {
184 printf("VMA flag %s is present on page 1 after unlock\n", LOCKED
);
191 static int test_mlock_lock()
195 unsigned long page_size
= getpagesize();
197 map
= mmap(NULL
, 2 * page_size
, PROT_READ
| PROT_WRITE
,
198 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
199 if (map
== MAP_FAILED
) {
200 perror("test_mlock_locked mmap");
204 if (mlock2_(map
, 2 * page_size
, 0)) {
205 if (errno
== ENOSYS
) {
206 printf("Cannot call new mlock family, skipping test\n");
213 if (!lock_check((unsigned long)map
))
216 /* Now unlock and recheck attributes */
217 if (munlock(map
, 2 * page_size
)) {
222 ret
= unlock_lock_check(map
);
225 munmap(map
, 2 * page_size
);
230 static int onfault_check(char *map
)
233 if (!is_vma_lock_on_fault((unsigned long)map
)) {
234 printf("VMA is not marked for lock on fault\n");
241 static int unlock_onfault_check(char *map
)
243 unsigned long page_size
= getpagesize();
245 if (is_vma_lock_on_fault((unsigned long)map
) ||
246 is_vma_lock_on_fault((unsigned long)map
+ page_size
)) {
247 printf("VMA is still lock on fault after unlock\n");
254 static int test_mlock_onfault()
258 unsigned long page_size
= getpagesize();
260 map
= mmap(NULL
, 2 * page_size
, PROT_READ
| PROT_WRITE
,
261 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
262 if (map
== MAP_FAILED
) {
263 perror("test_mlock_locked mmap");
267 if (mlock2_(map
, 2 * page_size
, MLOCK_ONFAULT
)) {
268 if (errno
== ENOSYS
) {
269 printf("Cannot call new mlock family, skipping test\n");
272 perror("mlock2(MLOCK_ONFAULT)");
276 if (onfault_check(map
))
279 /* Now unlock and recheck attributes */
280 if (munlock(map
, 2 * page_size
)) {
281 if (errno
== ENOSYS
) {
282 printf("Cannot call new mlock family, skipping test\n");
289 ret
= unlock_onfault_check(map
);
291 munmap(map
, 2 * page_size
);
296 static int test_lock_onfault_of_present()
300 unsigned long page_size
= getpagesize();
302 map
= mmap(NULL
, 2 * page_size
, PROT_READ
| PROT_WRITE
,
303 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
304 if (map
== MAP_FAILED
) {
305 perror("test_mlock_locked mmap");
311 if (mlock2_(map
, 2 * page_size
, MLOCK_ONFAULT
)) {
312 if (errno
== ENOSYS
) {
313 printf("Cannot call new mlock family, skipping test\n");
316 perror("mlock2(MLOCK_ONFAULT)");
320 if (!is_vma_lock_on_fault((unsigned long)map
) ||
321 !is_vma_lock_on_fault((unsigned long)map
+ page_size
)) {
322 printf("VMA with present pages is not marked lock on fault\n");
327 munmap(map
, 2 * page_size
);
332 static int test_munlockall()
336 unsigned long page_size
= getpagesize();
338 map
= mmap(NULL
, 2 * page_size
, PROT_READ
| PROT_WRITE
,
339 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
341 if (map
== MAP_FAILED
) {
342 perror("test_munlockall mmap");
346 if (mlockall(MCL_CURRENT
)) {
347 perror("mlockall(MCL_CURRENT)");
351 if (!lock_check((unsigned long)map
))
355 perror("munlockall()");
359 if (unlock_lock_check(map
))
362 munmap(map
, 2 * page_size
);
364 map
= mmap(NULL
, 2 * page_size
, PROT_READ
| PROT_WRITE
,
365 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
367 if (map
== MAP_FAILED
) {
368 perror("test_munlockall second mmap");
372 if (mlockall(MCL_CURRENT
| MCL_ONFAULT
)) {
373 perror("mlockall(MCL_CURRENT | MCL_ONFAULT)");
377 if (onfault_check(map
))
381 perror("munlockall()");
385 if (unlock_onfault_check(map
))
388 if (mlockall(MCL_CURRENT
| MCL_FUTURE
)) {
389 perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
393 if (!lock_check((unsigned long)map
))
397 perror("munlockall()");
401 ret
= unlock_lock_check(map
);
404 munmap(map
, 2 * page_size
);
410 static int test_vma_management(bool call_mlock
)
414 unsigned long page_size
= getpagesize();
415 struct vm_boundaries page1
;
416 struct vm_boundaries page2
;
417 struct vm_boundaries page3
;
419 map
= mmap(NULL
, 3 * page_size
, PROT_READ
| PROT_WRITE
,
420 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
421 if (map
== MAP_FAILED
) {
426 if (call_mlock
&& mlock2_(map
, 3 * page_size
, MLOCK_ONFAULT
)) {
427 if (errno
== ENOSYS
) {
428 printf("Cannot call new mlock family, skipping test\n");
431 perror("mlock(ONFAULT)\n");
435 if (get_vm_area((unsigned long)map
, &page1
) ||
436 get_vm_area((unsigned long)map
+ page_size
, &page2
) ||
437 get_vm_area((unsigned long)map
+ page_size
* 2, &page3
)) {
438 printf("couldn't find mapping in /proc/self/maps\n");
443 * Before we unlock a portion, we need to that all three pages are in
444 * the same VMA. If they are not we abort this test (Note that this is
447 if (page1
.start
!= page2
.start
|| page2
.start
!= page3
.start
) {
448 printf("VMAs are not merged to start, aborting test\n");
453 if (munlock(map
+ page_size
, page_size
)) {
458 if (get_vm_area((unsigned long)map
, &page1
) ||
459 get_vm_area((unsigned long)map
+ page_size
, &page2
) ||
460 get_vm_area((unsigned long)map
+ page_size
* 2, &page3
)) {
461 printf("couldn't find mapping in /proc/self/maps\n");
465 /* All three VMAs should be different */
466 if (page1
.start
== page2
.start
|| page2
.start
== page3
.start
) {
467 printf("failed to split VMA for munlock\n");
471 /* Now unlock the first and third page and check the VMAs again */
472 if (munlock(map
, page_size
* 3)) {
477 if (get_vm_area((unsigned long)map
, &page1
) ||
478 get_vm_area((unsigned long)map
+ page_size
, &page2
) ||
479 get_vm_area((unsigned long)map
+ page_size
* 2, &page3
)) {
480 printf("couldn't find mapping in /proc/self/maps\n");
484 /* Now all three VMAs should be the same */
485 if (page1
.start
!= page2
.start
|| page2
.start
!= page3
.start
) {
486 printf("failed to merge VMAs after munlock\n");
492 munmap(map
, 3 * page_size
);
496 static int test_mlockall(int (test_function
)(bool call_mlock
))
500 if (mlockall(MCL_CURRENT
| MCL_ONFAULT
| MCL_FUTURE
)) {
505 ret
= test_function(false);
510 int main(int argc
, char **argv
)
513 ret
+= test_mlock_lock();
514 ret
+= test_mlock_onfault();
515 ret
+= test_munlockall();
516 ret
+= test_lock_onfault_of_present();
517 ret
+= test_vma_management(true);
518 ret
+= test_mlockall(test_vma_management
);