1// SPDX-License-Identifier: GPL-2.0
2#define _GNU_SOURCE
3#include <sys/mman.h>
4#include <stdint.h>
5#include <unistd.h>
6#include <string.h>
7#include <sys/time.h>
8#include <sys/resource.h>
9#include <stdbool.h>
10#include "../kselftest.h"
11#include "mlock2.h"
12
13struct vm_boundaries {
14	unsigned long start;
15	unsigned long end;
16};
17
18static int get_vm_area(unsigned long addr, struct vm_boundaries *area)
19{
20	FILE *file;
21	int ret = 1;
22	char line[1024] = {0};
23	char *end_addr;
24	char *stop;
25	unsigned long start;
26	unsigned long end;
27
28	if (!area)
29		return ret;
30
31	file = fopen("/proc/self/maps", "r");
32	if (!file) {
33		perror("fopen");
34		return ret;
35	}
36
37	memset(area, 0, sizeof(struct vm_boundaries));
38
39	while(fgets(line, 1024, file)) {
40		end_addr = strchr(line, '-');
41		if (!end_addr) {
42			ksft_print_msg("cannot parse /proc/self/maps\n");
43			goto out;
44		}
45		*end_addr = '\0';
46		end_addr++;
47		stop = strchr(end_addr, ' ');
48		if (!stop) {
49			ksft_print_msg("cannot parse /proc/self/maps\n");
50			goto out;
51		}
52
53		sscanf(line, "%lx", &start);
54		sscanf(end_addr, "%lx", &end);
55
56		if (start <= addr && end > addr) {
57			area->start = start;
58			area->end = end;
59			ret = 0;
60			goto out;
61		}
62	}
63out:
64	fclose(file);
65	return ret;
66}
67
68#define VMFLAGS "VmFlags:"
69
70static bool is_vmflag_set(unsigned long addr, const char *vmflag)
71{
72	char *line = NULL;
73	char *flags;
74	size_t size = 0;
75	bool ret = false;
76	FILE *smaps;
77
78	smaps = seek_to_smaps_entry(addr);
79	if (!smaps) {
80		ksft_print_msg("Unable to parse /proc/self/smaps\n");
81		goto out;
82	}
83
84	while (getline(&line, &size, smaps) > 0) {
85		if (!strstr(line, VMFLAGS)) {
86			free(line);
87			line = NULL;
88			size = 0;
89			continue;
90		}
91
92		flags = line + strlen(VMFLAGS);
93		ret = (strstr(flags, vmflag) != NULL);
94		goto out;
95	}
96
97out:
98	free(line);
99	fclose(smaps);
100	return ret;
101}
102
103#define SIZE "Size:"
104#define RSS  "Rss:"
105#define LOCKED "lo"
106
107static unsigned long get_value_for_name(unsigned long addr, const char *name)
108{
109	char *line = NULL;
110	size_t size = 0;
111	char *value_ptr;
112	FILE *smaps = NULL;
113	unsigned long value = -1UL;
114
115	smaps = seek_to_smaps_entry(addr);
116	if (!smaps) {
117		ksft_print_msg("Unable to parse /proc/self/smaps\n");
118		goto out;
119	}
120
121	while (getline(&line, &size, smaps) > 0) {
122		if (!strstr(line, name)) {
123			free(line);
124			line = NULL;
125			size = 0;
126			continue;
127		}
128
129		value_ptr = line + strlen(name);
130		if (sscanf(value_ptr, "%lu kB", &value) < 1) {
131			ksft_print_msg("Unable to parse smaps entry for Size\n");
132			goto out;
133		}
134		break;
135	}
136
137out:
138	if (smaps)
139		fclose(smaps);
140	free(line);
141	return value;
142}
143
144static bool is_vma_lock_on_fault(unsigned long addr)
145{
146	bool locked;
147	unsigned long vma_size, vma_rss;
148
149	locked = is_vmflag_set(addr, LOCKED);
150	if (!locked)
151		return false;
152
153	vma_size = get_value_for_name(addr, SIZE);
154	vma_rss = get_value_for_name(addr, RSS);
155
156	/* only one page is faulted in */
157	return (vma_rss < vma_size);
158}
159
160#define PRESENT_BIT     0x8000000000000000ULL
161#define PFN_MASK        0x007FFFFFFFFFFFFFULL
162#define UNEVICTABLE_BIT (1UL << 18)
163
164static int lock_check(unsigned long addr)
165{
166	bool locked;
167	unsigned long vma_size, vma_rss;
168
169	locked = is_vmflag_set(addr, LOCKED);
170	if (!locked)
171		return false;
172
173	vma_size = get_value_for_name(addr, SIZE);
174	vma_rss = get_value_for_name(addr, RSS);
175
176	return (vma_rss == vma_size);
177}
178
179static int unlock_lock_check(char *map)
180{
181	if (is_vmflag_set((unsigned long)map, LOCKED)) {
182		ksft_print_msg("VMA flag %s is present on page 1 after unlock\n", LOCKED);
183		return 1;
184	}
185
186	return 0;
187}
188
189static void test_mlock_lock(void)
190{
191	char *map;
192	unsigned long page_size = getpagesize();
193
194	map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
195		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
196	if (map == MAP_FAILED)
197		ksft_exit_fail_msg("mmap error: %s", strerror(errno));
198
199	if (mlock2_(map, 2 * page_size, 0)) {
200		munmap(map, 2 * page_size);
201		ksft_exit_fail_msg("mlock2(0): %s\n", strerror(errno));
202	}
203
204	ksft_test_result(lock_check((unsigned long)map), "%s: Locked\n", __func__);
205
206	/* Now unlock and recheck attributes */
207	if (munlock(map, 2 * page_size)) {
208		munmap(map, 2 * page_size);
209		ksft_exit_fail_msg("munlock(): %s\n", strerror(errno));
210	}
211
212	ksft_test_result(!unlock_lock_check(map), "%s: Locked\n", __func__);
213	munmap(map, 2 * page_size);
214}
215
216static int onfault_check(char *map)
217{
218	*map = 'a';
219	if (!is_vma_lock_on_fault((unsigned long)map)) {
220		ksft_print_msg("VMA is not marked for lock on fault\n");
221		return 1;
222	}
223
224	return 0;
225}
226
227static int unlock_onfault_check(char *map)
228{
229	unsigned long page_size = getpagesize();
230
231	if (is_vma_lock_on_fault((unsigned long)map) ||
232	    is_vma_lock_on_fault((unsigned long)map + page_size)) {
233		ksft_print_msg("VMA is still lock on fault after unlock\n");
234		return 1;
235	}
236
237	return 0;
238}
239
240static void test_mlock_onfault(void)
241{
242	char *map;
243	unsigned long page_size = getpagesize();
244
245	map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
246		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
247	if (map == MAP_FAILED)
248		ksft_exit_fail_msg("mmap error: %s", strerror(errno));
249
250	if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
251		munmap(map, 2 * page_size);
252		ksft_exit_fail_msg("mlock2(MLOCK_ONFAULT): %s\n", strerror(errno));
253	}
254
255	ksft_test_result(!onfault_check(map), "%s: VMA marked for lock on fault\n", __func__);
256
257	/* Now unlock and recheck attributes */
258	if (munlock(map, 2 * page_size)) {
259		munmap(map, 2 * page_size);
260		ksft_exit_fail_msg("munlock(): %s\n", strerror(errno));
261	}
262
263	ksft_test_result(!unlock_onfault_check(map), "VMA open lock after fault\n");
264	munmap(map, 2 * page_size);
265}
266
267static void test_lock_onfault_of_present(void)
268{
269	char *map;
270	unsigned long page_size = getpagesize();
271
272	map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
273		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
274	if (map == MAP_FAILED)
275		ksft_exit_fail_msg("mmap error: %s", strerror(errno));
276
277	*map = 'a';
278
279	if (mlock2_(map, 2 * page_size, MLOCK_ONFAULT)) {
280		munmap(map, 2 * page_size);
281		ksft_test_result_fail("mlock2(MLOCK_ONFAULT) error: %s", strerror(errno));
282	}
283
284	ksft_test_result(is_vma_lock_on_fault((unsigned long)map) ||
285			 is_vma_lock_on_fault((unsigned long)map + page_size),
286			 "VMA with present pages is not marked lock on fault\n");
287	munmap(map, 2 * page_size);
288}
289
290static void test_munlockall0(void)
291{
292	char *map;
293	unsigned long page_size = getpagesize();
294
295	map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
296		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
297	if (map == MAP_FAILED)
298		ksft_exit_fail_msg("mmap error: %s\n", strerror(errno));
299
300	if (mlockall(MCL_CURRENT)) {
301		munmap(map, 2 * page_size);
302		ksft_exit_fail_msg("mlockall(MCL_CURRENT): %s\n", strerror(errno));
303	}
304
305	ksft_test_result(lock_check((unsigned long)map), "%s: Locked memory area\n", __func__);
306
307	if (munlockall()) {
308		munmap(map, 2 * page_size);
309		ksft_exit_fail_msg("munlockall(): %s\n", strerror(errno));
310	}
311
312	ksft_test_result(!unlock_lock_check(map), "%s: No locked memory\n", __func__);
313	munmap(map, 2 * page_size);
314}
315
316static void test_munlockall1(void)
317{
318	char *map;
319	unsigned long page_size = getpagesize();
320
321	map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
322		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
323	if (map == MAP_FAILED)
324		ksft_exit_fail_msg("mmap error: %s", strerror(errno));
325
326	if (mlockall(MCL_CURRENT | MCL_ONFAULT)) {
327		munmap(map, 2 * page_size);
328		ksft_exit_fail_msg("mlockall(MCL_CURRENT | MCL_ONFAULT): %s\n", strerror(errno));
329	}
330
331	ksft_test_result(!onfault_check(map), "%s: VMA marked for lock on fault\n", __func__);
332
333	if (munlockall()) {
334		munmap(map, 2 * page_size);
335		ksft_exit_fail_msg("munlockall(): %s\n", strerror(errno));
336	}
337
338	ksft_test_result(!unlock_onfault_check(map), "%s: Unlocked\n", __func__);
339
340	if (mlockall(MCL_CURRENT | MCL_FUTURE)) {
341		munmap(map, 2 * page_size);
342		ksft_exit_fail_msg("mlockall(MCL_CURRENT | MCL_FUTURE): %s\n", strerror(errno));
343	}
344
345	ksft_test_result(lock_check((unsigned long)map), "%s: Locked\n", __func__);
346
347	if (munlockall()) {
348		munmap(map, 2 * page_size);
349		ksft_exit_fail_msg("munlockall() %s\n", strerror(errno));
350	}
351
352	ksft_test_result(!unlock_lock_check(map), "%s: No locked memory\n", __func__);
353	munmap(map, 2 * page_size);
354}
355
356static void test_vma_management(bool call_mlock)
357{
358	void *map;
359	unsigned long page_size = getpagesize();
360	struct vm_boundaries page1;
361	struct vm_boundaries page2;
362	struct vm_boundaries page3;
363
364	map = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
365		   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
366	if (map == MAP_FAILED)
367		ksft_exit_fail_msg("mmap error: %s", strerror(errno));
368
369	if (call_mlock && mlock2_(map, 3 * page_size, MLOCK_ONFAULT)) {
370		munmap(map, 3 * page_size);
371		ksft_test_result_fail("mlock error: %s", strerror(errno));
372	}
373
374	if (get_vm_area((unsigned long)map, &page1) ||
375	    get_vm_area((unsigned long)map + page_size, &page2) ||
376	    get_vm_area((unsigned long)map + page_size * 2, &page3)) {
377		munmap(map, 3 * page_size);
378		ksft_test_result_fail("couldn't find mapping in /proc/self/maps");
379	}
380
381	/*
382	 * Before we unlock a portion, we need to that all three pages are in
383	 * the same VMA.  If they are not we abort this test (Note that this is
384	 * not a failure)
385	 */
386	if (page1.start != page2.start || page2.start != page3.start) {
387		munmap(map, 3 * page_size);
388		ksft_test_result_fail("VMAs are not merged to start, aborting test");
389	}
390
391	if (munlock(map + page_size, page_size)) {
392		munmap(map, 3 * page_size);
393		ksft_test_result_fail("munlock(): %s", strerror(errno));
394	}
395
396	if (get_vm_area((unsigned long)map, &page1) ||
397	    get_vm_area((unsigned long)map + page_size, &page2) ||
398	    get_vm_area((unsigned long)map + page_size * 2, &page3)) {
399		munmap(map, 3 * page_size);
400		ksft_test_result_fail("couldn't find mapping in /proc/self/maps");
401	}
402
403	/* All three VMAs should be different */
404	if (page1.start == page2.start || page2.start == page3.start) {
405		munmap(map, 3 * page_size);
406		ksft_test_result_fail("failed to split VMA for munlock");
407	}
408
409	/* Now unlock the first and third page and check the VMAs again */
410	if (munlock(map, page_size * 3)) {
411		munmap(map, 3 * page_size);
412		ksft_test_result_fail("munlock(): %s", strerror(errno));
413	}
414
415	if (get_vm_area((unsigned long)map, &page1) ||
416	    get_vm_area((unsigned long)map + page_size, &page2) ||
417	    get_vm_area((unsigned long)map + page_size * 2, &page3)) {
418		munmap(map, 3 * page_size);
419		ksft_test_result_fail("couldn't find mapping in /proc/self/maps");
420	}
421
422	/* Now all three VMAs should be the same */
423	if (page1.start != page2.start || page2.start != page3.start) {
424		munmap(map, 3 * page_size);
425		ksft_test_result_fail("failed to merge VMAs after munlock");
426	}
427
428	ksft_test_result_pass("%s call_mlock %d\n", __func__, call_mlock);
429	munmap(map, 3 * page_size);
430}
431
432static void test_mlockall(void)
433{
434	if (mlockall(MCL_CURRENT | MCL_ONFAULT | MCL_FUTURE))
435		ksft_exit_fail_msg("mlockall failed: %s\n", strerror(errno));
436
437	test_vma_management(false);
438	munlockall();
439}
440
441int main(int argc, char **argv)
442{
443	int ret, size = 3 * getpagesize();
444	void *map;
445
446	ksft_print_header();
447
448	map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
449	if (map == MAP_FAILED)
450		ksft_exit_fail_msg("mmap error: %s", strerror(errno));
451
452	ret = mlock2_(map, size, MLOCK_ONFAULT);
453	if (ret && errno == ENOSYS)
454		ksft_finished();
455
456	munmap(map, size);
457
458	ksft_set_plan(13);
459
460	test_mlock_lock();
461	test_mlock_onfault();
462	test_munlockall0();
463	test_munlockall1();
464	test_lock_onfault_of_present();
465	test_vma_management(true);
466	test_mlockall();
467
468	ksft_finished();
469}
470