1 // SPDX-License-Identifier: GPL-2.0
3 * It tests the mlock/mlock2() when they are invoked
4 * on randomly memory region.
7 #include <sys/resource.h>
8 #include <sys/capability.h>
17 #define CHUNK_UNIT (128 * 1024)
18 #define MLOCK_RLIMIT_SIZE (CHUNK_UNIT * 2)
19 #define MLOCK_WITHIN_LIMIT_SIZE CHUNK_UNIT
20 #define MLOCK_OUTOF_LIMIT_SIZE (CHUNK_UNIT * 3)
23 #define PAGE_ALIGN(size, ps) (((size) + ((ps) - 1)) & ~((ps) - 1))
25 int set_cap_limits(rlim_t max)
28 cap_t cap = cap_init();
32 if (setrlimit(RLIMIT_MEMLOCK, &new)) {
33 perror("setrlimit() returns error\n");
37 /* drop capabilities including CAP_IPC_LOCK */
38 if (cap_set_proc(cap)) {
39 perror("cap_set_proc() returns error\n");
46 int get_proc_locked_vm_size(void)
50 char line[1024] = {0};
51 unsigned long lock_size = 0;
53 f = fopen("/proc/self/status", "r");
59 while (fgets(line, 1024, f)) {
60 if (strstr(line, "VmLck")) {
61 ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
63 printf("sscanf() on VmLck error: %s: %d\n",
69 return (int)(lock_size << 10);
73 perror("cann't parse VmLck in /proc/self/status\n");
79 * Get the MMUPageSize of the memory region including input
80 * address from proc file.
82 * return value: on error case, 0 will be returned.
83 * Otherwise the page size(in bytes) is returned.
85 int get_proc_page_size(unsigned long addr)
89 unsigned long mmupage_size = 0;
92 smaps = seek_to_smaps_entry(addr);
94 printf("Unable to parse /proc/self/smaps\n");
98 while (getline(&line, &size, smaps) > 0) {
99 if (!strstr(line, "MMUPageSize")) {
106 /* found the MMUPageSize of this section */
107 if (sscanf(line, "MMUPageSize: %8lu kB",
108 &mmupage_size) < 1) {
109 printf("Unable to parse smaps entry for Size:%s\n",
118 return mmupage_size << 10;
122 * Test mlock/mlock2() on provided memory chunk.
123 * It expects the mlock/mlock2() to be successful (within rlimit)
125 * With allocated memory chunk [p, p + alloc_size), this
126 * test will choose start/len randomly to perform mlock/mlock2
127 * [start, start + len] memory range. The range is within range
128 * of the allocated chunk.
130 * The memory region size alloc_size is within the rlimit.
131 * So we always expect a success of mlock/mlock2.
133 * VmLck is assumed to be 0 before this test.
135 * return value: 0 - success
138 int test_mlock_within_limit(char *p, int alloc_size)
142 int locked_vm_size = 0;
146 getrlimit(RLIMIT_MEMLOCK, &cur);
147 if (cur.rlim_cur < alloc_size) {
148 printf("alloc_size[%d] < %u rlimit,lead to mlock failure\n",
149 alloc_size, (unsigned int)cur.rlim_cur);
154 for (i = 0; i < TEST_LOOP; i++) {
156 * - choose mlock/mlock2 randomly
157 * - choose lock_size randomly but lock_size < alloc_size
158 * - choose start_offset randomly but p+start_offset+lock_size
161 int is_mlock = !!(rand() % 2);
162 int lock_size = rand() % alloc_size;
163 int start_offset = rand() % (alloc_size - lock_size);
166 ret = mlock(p + start_offset, lock_size);
168 ret = mlock2_(p + start_offset, lock_size,
172 printf("%s() failure at |%p(%d)| mlock:|%p(%d)|\n",
173 is_mlock ? "mlock" : "mlock2",
175 p + start_offset, lock_size);
181 * Check VmLck left by the tests.
183 locked_vm_size = get_proc_locked_vm_size();
184 page_size = get_proc_page_size((unsigned long)p);
185 if (page_size == 0) {
186 printf("cannot get proc MMUPageSize\n");
190 if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size) {
191 printf("test_mlock_within_limit() left VmLck:%d on %d chunk\n",
192 locked_vm_size, alloc_size);
201 * We expect the mlock/mlock2() to be fail (outof limitation)
203 * With allocated memory chunk [p, p + alloc_size), this
204 * test will randomly choose start/len and perform mlock/mlock2
205 * on [start, start+len] range.
207 * The memory region size alloc_size is above the rlimit.
208 * And the len to be locked is higher than rlimit.
209 * So we always expect a failure of mlock/mlock2.
210 * No locked page number should be increased as a side effect.
212 * return value: 0 - success
215 int test_mlock_outof_limit(char *p, int alloc_size)
219 int locked_vm_size = 0, old_locked_vm_size = 0;
222 getrlimit(RLIMIT_MEMLOCK, &cur);
223 if (cur.rlim_cur >= alloc_size) {
224 printf("alloc_size[%d] >%u rlimit, violates test condition\n",
225 alloc_size, (unsigned int)cur.rlim_cur);
229 old_locked_vm_size = get_proc_locked_vm_size();
231 for (i = 0; i < TEST_LOOP; i++) {
232 int is_mlock = !!(rand() % 2);
233 int lock_size = (rand() % (alloc_size - cur.rlim_cur))
235 int start_offset = rand() % (alloc_size - lock_size);
238 ret = mlock(p + start_offset, lock_size);
240 ret = mlock2_(p + start_offset, lock_size,
243 printf("%s() succeeds? on %p(%d) mlock%p(%d)\n",
244 is_mlock ? "mlock" : "mlock2",
246 p + start_offset, lock_size);
251 locked_vm_size = get_proc_locked_vm_size();
252 if (locked_vm_size != old_locked_vm_size) {
253 printf("tests leads to new mlocked page: old[%d], new[%d]\n",
262 int main(int argc, char **argv)
267 if (set_cap_limits(MLOCK_RLIMIT_SIZE))
270 p = malloc(MLOCK_WITHIN_LIMIT_SIZE);
272 perror("malloc() failure\n");
275 ret = test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE);
278 munlock(p, MLOCK_WITHIN_LIMIT_SIZE);
282 p = malloc(MLOCK_OUTOF_LIMIT_SIZE);
284 perror("malloc() failure\n");
287 ret = test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE);
290 munlock(p, MLOCK_OUTOF_LIMIT_SIZE);