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>
10 #include <linux/mman.h>
16 #include "../kselftest.h"
19 #define CHUNK_UNIT (128 * 1024)
20 #define MLOCK_RLIMIT_SIZE (CHUNK_UNIT * 2)
21 #define MLOCK_WITHIN_LIMIT_SIZE CHUNK_UNIT
22 #define MLOCK_OUTOF_LIMIT_SIZE (CHUNK_UNIT * 3)
25 #define PAGE_ALIGN(size, ps) (((size) + ((ps) - 1)) & ~((ps) - 1))
27 int set_cap_limits(rlim_t max)
30 cap_t cap = cap_init();
34 if (setrlimit(RLIMIT_MEMLOCK, &new)) {
35 ksft_perror("setrlimit() returns error\n");
39 /* drop capabilities including CAP_IPC_LOCK */
40 if (cap_set_proc(cap)) {
41 ksft_perror("cap_set_proc() returns error\n");
48 int get_proc_locked_vm_size(void)
52 char line[1024] = {0};
53 unsigned long lock_size = 0;
55 f = fopen("/proc/self/status", "r");
57 ksft_exit_fail_msg("fopen: %s\n", strerror(errno));
59 while (fgets(line, 1024, f)) {
60 if (strstr(line, "VmLck")) {
61 ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
64 ksft_exit_fail_msg("sscanf() on VmLck error: %s: %d\n",
68 return (int)(lock_size << 10);
73 ksft_exit_fail_msg("cannot parse VmLck in /proc/self/status: %s\n", strerror(errno));
78 * Get the MMUPageSize of the memory region including input
79 * address from proc file.
81 * return value: on error case, 0 will be returned.
82 * Otherwise the page size(in bytes) is returned.
84 int get_proc_page_size(unsigned long addr)
88 unsigned long mmupage_size = 0;
91 smaps = seek_to_smaps_entry(addr);
93 ksft_exit_fail_msg("Unable to parse /proc/self/smaps\n");
95 while (getline(&line, &size, smaps) > 0) {
96 if (!strstr(line, "MMUPageSize")) {
103 /* found the MMUPageSize of this section */
104 if (sscanf(line, "MMUPageSize: %8lu kB", &mmupage_size) < 1)
105 ksft_exit_fail_msg("Unable to parse smaps entry for Size:%s\n",
112 return mmupage_size << 10;
116 * Test mlock/mlock2() on provided memory chunk.
117 * It expects the mlock/mlock2() to be successful (within rlimit)
119 * With allocated memory chunk [p, p + alloc_size), this
120 * test will choose start/len randomly to perform mlock/mlock2
121 * [start, start + len] memory range. The range is within range
122 * of the allocated chunk.
124 * The memory region size alloc_size is within the rlimit.
125 * So we always expect a success of mlock/mlock2.
127 * VmLck is assumed to be 0 before this test.
129 * return value: 0 - success
132 static void test_mlock_within_limit(char *p, int alloc_size)
136 int locked_vm_size = 0;
140 getrlimit(RLIMIT_MEMLOCK, &cur);
141 if (cur.rlim_cur < alloc_size)
142 ksft_exit_fail_msg("alloc_size[%d] < %u rlimit,lead to mlock failure\n",
143 alloc_size, (unsigned int)cur.rlim_cur);
146 for (i = 0; i < TEST_LOOP; i++) {
148 * - choose mlock/mlock2 randomly
149 * - choose lock_size randomly but lock_size < alloc_size
150 * - choose start_offset randomly but p+start_offset+lock_size
153 int is_mlock = !!(rand() % 2);
154 int lock_size = rand() % alloc_size;
155 int start_offset = rand() % (alloc_size - lock_size);
158 ret = mlock(p + start_offset, lock_size);
160 ret = mlock2_(p + start_offset, lock_size,
164 ksft_exit_fail_msg("%s() failure at |%p(%d)| mlock:|%p(%d)|\n",
165 is_mlock ? "mlock" : "mlock2",
167 p + start_offset, lock_size);
171 * Check VmLck left by the tests.
173 locked_vm_size = get_proc_locked_vm_size();
174 page_size = get_proc_page_size((unsigned long)p);
176 if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size)
177 ksft_exit_fail_msg("%s left VmLck:%d on %d chunk\n",
178 __func__, locked_vm_size, alloc_size);
180 ksft_test_result_pass("%s\n", __func__);
185 * We expect the mlock/mlock2() to be fail (outof limitation)
187 * With allocated memory chunk [p, p + alloc_size), this
188 * test will randomly choose start/len and perform mlock/mlock2
189 * on [start, start+len] range.
191 * The memory region size alloc_size is above the rlimit.
192 * And the len to be locked is higher than rlimit.
193 * So we always expect a failure of mlock/mlock2.
194 * No locked page number should be increased as a side effect.
196 * return value: 0 - success
199 static void test_mlock_outof_limit(char *p, int alloc_size)
203 int locked_vm_size = 0, old_locked_vm_size = 0;
206 getrlimit(RLIMIT_MEMLOCK, &cur);
207 if (cur.rlim_cur >= alloc_size)
208 ksft_exit_fail_msg("alloc_size[%d] >%u rlimit, violates test condition\n",
209 alloc_size, (unsigned int)cur.rlim_cur);
211 old_locked_vm_size = get_proc_locked_vm_size();
213 for (i = 0; i < TEST_LOOP; i++) {
214 int is_mlock = !!(rand() % 2);
215 int lock_size = (rand() % (alloc_size - cur.rlim_cur))
217 int start_offset = rand() % (alloc_size - lock_size);
220 ret = mlock(p + start_offset, lock_size);
222 ret = mlock2_(p + start_offset, lock_size,
225 ksft_exit_fail_msg("%s() succeeds? on %p(%d) mlock%p(%d)\n",
226 is_mlock ? "mlock" : "mlock2",
227 p, alloc_size, p + start_offset, lock_size);
230 locked_vm_size = get_proc_locked_vm_size();
231 if (locked_vm_size != old_locked_vm_size)
232 ksft_exit_fail_msg("tests leads to new mlocked page: old[%d], new[%d]\n",
236 ksft_test_result_pass("%s\n", __func__);
239 int main(int argc, char **argv)
245 if (set_cap_limits(MLOCK_RLIMIT_SIZE))
250 p = malloc(MLOCK_WITHIN_LIMIT_SIZE);
252 ksft_exit_fail_msg("malloc() failure: %s\n", strerror(errno));
254 test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE);
255 munlock(p, MLOCK_WITHIN_LIMIT_SIZE);
258 p = malloc(MLOCK_OUTOF_LIMIT_SIZE);
260 ksft_exit_fail_msg("malloc() failure: %s\n", strerror(errno));
262 test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE);
263 munlock(p, MLOCK_OUTOF_LIMIT_SIZE);