2 * It tests the mlock/mlock2() when they are invoked
3 * on randomly memory region.
6 #include <sys/resource.h>
7 #include <sys/capability.h>
16 #define CHUNK_UNIT (128 * 1024)
17 #define MLOCK_RLIMIT_SIZE (CHUNK_UNIT * 2)
18 #define MLOCK_WITHIN_LIMIT_SIZE CHUNK_UNIT
19 #define MLOCK_OUTOF_LIMIT_SIZE (CHUNK_UNIT * 3)
22 #define PAGE_ALIGN(size, ps) (((size) + ((ps) - 1)) & ~((ps) - 1))
24 int set_cap_limits(rlim_t max)
27 cap_t cap = cap_init();
31 if (setrlimit(RLIMIT_MEMLOCK, &new)) {
32 perror("setrlimit() returns error\n");
36 /* drop capabilities including CAP_IPC_LOCK */
37 if (cap_set_proc(cap)) {
38 perror("cap_set_proc() returns error\n");
45 int get_proc_locked_vm_size(void)
49 char line[1024] = {0};
50 unsigned long lock_size = 0;
52 f = fopen("/proc/self/status", "r");
58 while (fgets(line, 1024, f)) {
59 if (strstr(line, "VmLck")) {
60 ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
62 printf("sscanf() on VmLck error: %s: %d\n",
68 return (int)(lock_size << 10);
72 perror("cann't parse VmLck in /proc/self/status\n");
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 printf("Unable to parse /proc/self/smaps\n");
97 while (getline(&line, &size, smaps) > 0) {
98 if (!strstr(line, "MMUPageSize")) {
105 /* found the MMUPageSize of this section */
106 if (sscanf(line, "MMUPageSize: %8lu kB",
107 &mmupage_size) < 1) {
108 printf("Unable to parse smaps entry for Size:%s\n",
117 return mmupage_size << 10;
121 * Test mlock/mlock2() on provided memory chunk.
122 * It expects the mlock/mlock2() to be successful (within rlimit)
124 * With allocated memory chunk [p, p + alloc_size), this
125 * test will choose start/len randomly to perform mlock/mlock2
126 * [start, start + len] memory range. The range is within range
127 * of the allocated chunk.
129 * The memory region size alloc_size is within the rlimit.
130 * So we always expect a success of mlock/mlock2.
132 * VmLck is assumed to be 0 before this test.
134 * return value: 0 - success
137 int test_mlock_within_limit(char *p, int alloc_size)
141 int locked_vm_size = 0;
145 getrlimit(RLIMIT_MEMLOCK, &cur);
146 if (cur.rlim_cur < alloc_size) {
147 printf("alloc_size[%d] < %u rlimit,lead to mlock failure\n",
148 alloc_size, (unsigned int)cur.rlim_cur);
153 for (i = 0; i < TEST_LOOP; i++) {
155 * - choose mlock/mlock2 randomly
156 * - choose lock_size randomly but lock_size < alloc_size
157 * - choose start_offset randomly but p+start_offset+lock_size
160 int is_mlock = !!(rand() % 2);
161 int lock_size = rand() % alloc_size;
162 int start_offset = rand() % (alloc_size - lock_size);
165 ret = mlock(p + start_offset, lock_size);
167 ret = mlock2_(p + start_offset, lock_size,
171 printf("%s() failure at |%p(%d)| mlock:|%p(%d)|\n",
172 is_mlock ? "mlock" : "mlock2",
174 p + start_offset, lock_size);
180 * Check VmLck left by the tests.
182 locked_vm_size = get_proc_locked_vm_size();
183 page_size = get_proc_page_size((unsigned long)p);
184 if (page_size == 0) {
185 printf("cannot get proc MMUPageSize\n");
189 if (locked_vm_size > PAGE_ALIGN(alloc_size, page_size) + page_size) {
190 printf("test_mlock_within_limit() left VmLck:%d on %d chunk\n",
191 locked_vm_size, alloc_size);
200 * We expect the mlock/mlock2() to be fail (outof limitation)
202 * With allocated memory chunk [p, p + alloc_size), this
203 * test will randomly choose start/len and perform mlock/mlock2
204 * on [start, start+len] range.
206 * The memory region size alloc_size is above the rlimit.
207 * And the len to be locked is higher than rlimit.
208 * So we always expect a failure of mlock/mlock2.
209 * No locked page number should be increased as a side effect.
211 * return value: 0 - success
214 int test_mlock_outof_limit(char *p, int alloc_size)
218 int locked_vm_size = 0, old_locked_vm_size = 0;
221 getrlimit(RLIMIT_MEMLOCK, &cur);
222 if (cur.rlim_cur >= alloc_size) {
223 printf("alloc_size[%d] >%u rlimit, violates test condition\n",
224 alloc_size, (unsigned int)cur.rlim_cur);
228 old_locked_vm_size = get_proc_locked_vm_size();
230 for (i = 0; i < TEST_LOOP; i++) {
231 int is_mlock = !!(rand() % 2);
232 int lock_size = (rand() % (alloc_size - cur.rlim_cur))
234 int start_offset = rand() % (alloc_size - lock_size);
237 ret = mlock(p + start_offset, lock_size);
239 ret = mlock2_(p + start_offset, lock_size,
242 printf("%s() succeeds? on %p(%d) mlock%p(%d)\n",
243 is_mlock ? "mlock" : "mlock2",
245 p + start_offset, lock_size);
250 locked_vm_size = get_proc_locked_vm_size();
251 if (locked_vm_size != old_locked_vm_size) {
252 printf("tests leads to new mlocked page: old[%d], new[%d]\n",
261 int main(int argc, char **argv)
266 if (set_cap_limits(MLOCK_RLIMIT_SIZE))
269 p = malloc(MLOCK_WITHIN_LIMIT_SIZE);
271 perror("malloc() failure\n");
274 ret = test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE);
277 munlock(p, MLOCK_WITHIN_LIMIT_SIZE);
281 p = malloc(MLOCK_OUTOF_LIMIT_SIZE);
283 perror("malloc() failure\n");
286 ret = test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE);
289 munlock(p, MLOCK_OUTOF_LIMIT_SIZE);