xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock2/mlock201.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4  * Author: Xiao Yang <[email protected]>
5  */
6 /*
7  * Description:
8  * Check the basic functionality of the mlock2(2) since kernel v2.6.9:
9  * 1) When we use mlock2() without MLOCK_ONFAULT to lock memory in the
10  *    specified range that is multiples of page size or not, we can
11  *    show correct size of locked memory by VmLck from /proc/PID/status
12  *    and lock all pages including non-present.
13  * 2) When we use mlock2() with MLOCK_ONFAULT to lock memory in the
14  *    specified range that is multiples of page size or not, we can
15  *    show correct size of locked memory by VmLck from /proc/PID/status
16  *    and just lock present pages.
17  */
18 #include <errno.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <sys/mman.h>
23 #include <linux/mman.h>
24 
25 #include "tst_test.h"
26 #include "lapi/syscalls.h"
27 #include "lapi/mlock2.h"
28 #include "pgsize_helpers.h"
29 
30 #define PAGES	8
31 #define HPAGES	(PAGES / 2)
32 
33 static size_t pgsz;
34 
35 static DECLARE_MINCORE_VECTOR(vec, PAGES+1);
36 
37 static struct tcase {
38 	size_t populate_pages;
39 	size_t lock_pages;
40 	ssize_t offset; /* Can be negative */
41 	size_t exp_vmlcks;
42 	size_t exp_present_pages;
43 	int flag;
44 } tcases[] = {
45 	/* lock single page, expect it to be locked and present */
46 	{0, 1, 0, 1, 1, 0},
47 
48 	/* lock all pages, expect all to be locked and present */
49 	{0, PAGES, 0, PAGES, PAGES, 0},
50 
51 	/* mlock2() locks 3 pages if the specified
52 	 * range is little more than 2 pages.
53 	 */
54 	{0, 2, 1, 3, 3, 0},
55 
56 	/* mlock2() locks 2 pages if the specified
57 	 * range is little less than 2 pages.
58 	 */
59 	{0, 2, -1, 2, 2, 0},
60 
61 	/* mlock2() with MLOCK_ONFAULT just lock present
62 	 * pages populated by data.
63 	 */
64 	{0, 1, 0, 1, 0, MLOCK_ONFAULT},
65 
66 	/* fault-in half of pages, lock all with MLOCK_ONFAULT */
67 	{HPAGES, PAGES, 0, PAGES, HPAGES, MLOCK_ONFAULT},
68 
69 	/* fault-in 1 page, lock half of pages */
70 	{1, HPAGES, 1, HPAGES + 1, 1, MLOCK_ONFAULT},
71 
72 	/* fault-in half, lock little less than 2 pages */
73 	{HPAGES, HPAGES, -1, HPAGES, HPAGES, MLOCK_ONFAULT},
74 };
75 
check_locked_pages(char * addr,size_t len,size_t num_pgs)76 static size_t check_locked_pages(char *addr, size_t len, size_t num_pgs)
77 {
78 	size_t n;
79 	size_t act_pages = 0;
80 
81 	SAFE_MINCORE(addr, len, vec);
82 
83 	for (n = 0; n < nr_pgs_to_nr_kernel_pgs(num_pgs); n++) {
84 		if (vec[n] & 1)
85 			act_pages++;
86 	}
87 
88 	return nr_kernel_pgs_to_nr_pgs(act_pages);
89 }
90 
verify_mlock2(unsigned int n)91 static void verify_mlock2(unsigned int n)
92 {
93 	struct tcase *tc = &tcases[n];
94 	size_t bsize, asize, act_vmlcks, act_pgs;
95 	char *addr;
96 
97 	addr = SAFE_MMAP(NULL, PAGES * pgsz, PROT_WRITE,
98 			 MAP_SHARED | MAP_ANONYMOUS, 0, 0);
99 
100 	if (tc->populate_pages)
101 		memset(addr, 0, tc->populate_pages * pgsz);
102 
103 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &bsize);
104 
105 	TEST(tst_syscall(__NR_mlock2, addr, tc->lock_pages * pgsz + tc->offset,
106 			 tc->flag));
107 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &asize);
108 
109 	if (TST_RET != 0) {
110 		if (tc->flag && TST_ERR == EINVAL)
111 			tst_res(TCONF, "mlock2() didn't support MLOCK_ONFAULT");
112 		else
113 			tst_res(TFAIL | TTERRNO, "mlock2() failed");
114 		goto end2;
115 	}
116 
117 	act_vmlcks = (asize - bsize) * 1024 / pgsz;
118 	if (tc->exp_vmlcks != act_vmlcks) {
119 		tst_res(TFAIL, "VmLck showed wrong %ld pages, expected %ld",
120 			act_vmlcks, tc->exp_vmlcks);
121 		goto end1;
122 	}
123 
124 	act_pgs = check_locked_pages(addr, PAGES * pgsz, PAGES);
125 	if (act_pgs != tc->exp_present_pages) {
126 		tst_res(TFAIL, "mlock2(%d) locked %ld pages, expected %ld",
127 			tc->flag, act_pgs, tc->exp_present_pages);
128 	} else {
129 		tst_res(TPASS, "mlock2(%d) succeeded in locking %ld pages",
130 			tc->flag, tc->exp_present_pages);
131 	}
132 
133 end1:
134 	SAFE_MUNLOCK(addr, tc->lock_pages * pgsz + tc->offset);
135 end2:
136 	SAFE_MUNMAP(addr, PAGES * pgsz);
137 }
138 
setup(void)139 static void setup(void)
140 {
141 	pgsz = getpagesize();
142 	MLOCK_PAGE_SIZE_EMULATION_OFFSET(tcases);
143 }
144 
145 static struct tst_test test = {
146 	.tcnt = ARRAY_SIZE(tcases),
147 	.test = verify_mlock2,
148 	.setup = setup,
149 	.needs_root = 1,
150 };
151