power: supply: bq24190_charger: Add disable-reset device-property
[linux/fpc-iii.git] / tools / testing / selftests / vm / mlock-random-test.c
blob83de4f58d2629cda7964520188bf8e4138be8ad1
1 /*
2 * It tests the mlock/mlock2() when they are invoked
3 * on randomly memory region.
4 */
5 #include <unistd.h>
6 #include <sys/resource.h>
7 #include <sys/capability.h>
8 #include <sys/mman.h>
9 #include <fcntl.h>
10 #include <string.h>
11 #include <sys/ipc.h>
12 #include <sys/shm.h>
13 #include <time.h>
14 #include "mlock2.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)
21 #define TEST_LOOP 100
22 #define PAGE_ALIGN(size, ps) (((size) + ((ps) - 1)) & ~((ps) - 1))
24 int set_cap_limits(rlim_t max)
26 struct rlimit new;
27 cap_t cap = cap_init();
29 new.rlim_cur = max;
30 new.rlim_max = max;
31 if (setrlimit(RLIMIT_MEMLOCK, &new)) {
32 perror("setrlimit() returns error\n");
33 return -1;
36 /* drop capabilities including CAP_IPC_LOCK */
37 if (cap_set_proc(cap)) {
38 perror("cap_set_proc() returns error\n");
39 return -2;
42 return 0;
45 int get_proc_locked_vm_size(void)
47 FILE *f;
48 int ret = -1;
49 char line[1024] = {0};
50 unsigned long lock_size = 0;
52 f = fopen("/proc/self/status", "r");
53 if (!f) {
54 perror("fopen");
55 return -1;
58 while (fgets(line, 1024, f)) {
59 if (strstr(line, "VmLck")) {
60 ret = sscanf(line, "VmLck:\t%8lu kB", &lock_size);
61 if (ret <= 0) {
62 printf("sscanf() on VmLck error: %s: %d\n",
63 line, ret);
64 fclose(f);
65 return -1;
67 fclose(f);
68 return (int)(lock_size << 10);
72 perror("cann't parse VmLck in /proc/self/status\n");
73 fclose(f);
74 return -1;
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)
86 FILE *smaps;
87 char *line;
88 unsigned long mmupage_size = 0;
89 size_t size;
91 smaps = seek_to_smaps_entry(addr);
92 if (!smaps) {
93 printf("Unable to parse /proc/self/smaps\n");
94 return 0;
97 while (getline(&line, &size, smaps) > 0) {
98 if (!strstr(line, "MMUPageSize")) {
99 free(line);
100 line = NULL;
101 size = 0;
102 continue;
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",
109 line);
110 break;
114 free(line);
115 if (smaps)
116 fclose(smaps);
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
135 * else: failure
137 int test_mlock_within_limit(char *p, int alloc_size)
139 int i;
140 int ret = 0;
141 int locked_vm_size = 0;
142 struct rlimit cur;
143 int page_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);
149 return -1;
152 srand(time(NULL));
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
158 * < p+alloc_size
160 int is_mlock = !!(rand() % 2);
161 int lock_size = rand() % alloc_size;
162 int start_offset = rand() % (alloc_size - lock_size);
164 if (is_mlock)
165 ret = mlock(p + start_offset, lock_size);
166 else
167 ret = mlock2_(p + start_offset, lock_size,
168 MLOCK_ONFAULT);
170 if (ret) {
171 printf("%s() failure at |%p(%d)| mlock:|%p(%d)|\n",
172 is_mlock ? "mlock" : "mlock2",
173 p, alloc_size,
174 p + start_offset, lock_size);
175 return ret;
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");
186 return -1;
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);
192 return -1;
195 return 0;
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
212 * else: failure
214 int test_mlock_outof_limit(char *p, int alloc_size)
216 int i;
217 int ret = 0;
218 int locked_vm_size = 0, old_locked_vm_size = 0;
219 struct rlimit cur;
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);
225 return -1;
228 old_locked_vm_size = get_proc_locked_vm_size();
229 srand(time(NULL));
230 for (i = 0; i < TEST_LOOP; i++) {
231 int is_mlock = !!(rand() % 2);
232 int lock_size = (rand() % (alloc_size - cur.rlim_cur))
233 + cur.rlim_cur;
234 int start_offset = rand() % (alloc_size - lock_size);
236 if (is_mlock)
237 ret = mlock(p + start_offset, lock_size);
238 else
239 ret = mlock2_(p + start_offset, lock_size,
240 MLOCK_ONFAULT);
241 if (ret == 0) {
242 printf("%s() succeeds? on %p(%d) mlock%p(%d)\n",
243 is_mlock ? "mlock" : "mlock2",
244 p, alloc_size,
245 p + start_offset, lock_size);
246 return -1;
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",
253 old_locked_vm_size,
254 locked_vm_size);
255 return -1;
258 return 0;
261 int main(int argc, char **argv)
263 char *p = NULL;
264 int ret = 0;
266 if (set_cap_limits(MLOCK_RLIMIT_SIZE))
267 return -1;
269 p = malloc(MLOCK_WITHIN_LIMIT_SIZE);
270 if (p == NULL) {
271 perror("malloc() failure\n");
272 return -1;
274 ret = test_mlock_within_limit(p, MLOCK_WITHIN_LIMIT_SIZE);
275 if (ret)
276 return ret;
277 munlock(p, MLOCK_WITHIN_LIMIT_SIZE);
278 free(p);
281 p = malloc(MLOCK_OUTOF_LIMIT_SIZE);
282 if (p == NULL) {
283 perror("malloc() failure\n");
284 return -1;
286 ret = test_mlock_outof_limit(p, MLOCK_OUTOF_LIMIT_SIZE);
287 if (ret)
288 return ret;
289 munlock(p, MLOCK_OUTOF_LIMIT_SIZE);
290 free(p);
292 return 0;