xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock2/mlock201.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * Description:
8*49cdfc7eSAndroid Build Coastguard Worker  * Check the basic functionality of the mlock2(2) since kernel v2.6.9:
9*49cdfc7eSAndroid Build Coastguard Worker  * 1) When we use mlock2() without MLOCK_ONFAULT to lock memory in the
10*49cdfc7eSAndroid Build Coastguard Worker  *    specified range that is multiples of page size or not, we can
11*49cdfc7eSAndroid Build Coastguard Worker  *    show correct size of locked memory by VmLck from /proc/PID/status
12*49cdfc7eSAndroid Build Coastguard Worker  *    and lock all pages including non-present.
13*49cdfc7eSAndroid Build Coastguard Worker  * 2) When we use mlock2() with MLOCK_ONFAULT to lock memory in the
14*49cdfc7eSAndroid Build Coastguard Worker  *    specified range that is multiples of page size or not, we can
15*49cdfc7eSAndroid Build Coastguard Worker  *    show correct size of locked memory by VmLck from /proc/PID/status
16*49cdfc7eSAndroid Build Coastguard Worker  *    and just lock present pages.
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <linux/mman.h>
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mlock2.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include "pgsize_helpers.h"
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #define PAGES	8
31*49cdfc7eSAndroid Build Coastguard Worker #define HPAGES	(PAGES / 2)
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker static size_t pgsz;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static DECLARE_MINCORE_VECTOR(vec, PAGES+1);
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
38*49cdfc7eSAndroid Build Coastguard Worker 	size_t populate_pages;
39*49cdfc7eSAndroid Build Coastguard Worker 	size_t lock_pages;
40*49cdfc7eSAndroid Build Coastguard Worker 	ssize_t offset; /* Can be negative */
41*49cdfc7eSAndroid Build Coastguard Worker 	size_t exp_vmlcks;
42*49cdfc7eSAndroid Build Coastguard Worker 	size_t exp_present_pages;
43*49cdfc7eSAndroid Build Coastguard Worker 	int flag;
44*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
45*49cdfc7eSAndroid Build Coastguard Worker 	/* lock single page, expect it to be locked and present */
46*49cdfc7eSAndroid Build Coastguard Worker 	{0, 1, 0, 1, 1, 0},
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	/* lock all pages, expect all to be locked and present */
49*49cdfc7eSAndroid Build Coastguard Worker 	{0, PAGES, 0, PAGES, PAGES, 0},
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker 	/* mlock2() locks 3 pages if the specified
52*49cdfc7eSAndroid Build Coastguard Worker 	 * range is little more than 2 pages.
53*49cdfc7eSAndroid Build Coastguard Worker 	 */
54*49cdfc7eSAndroid Build Coastguard Worker 	{0, 2, 1, 3, 3, 0},
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	/* mlock2() locks 2 pages if the specified
57*49cdfc7eSAndroid Build Coastguard Worker 	 * range is little less than 2 pages.
58*49cdfc7eSAndroid Build Coastguard Worker 	 */
59*49cdfc7eSAndroid Build Coastguard Worker 	{0, 2, -1, 2, 2, 0},
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	/* mlock2() with MLOCK_ONFAULT just lock present
62*49cdfc7eSAndroid Build Coastguard Worker 	 * pages populated by data.
63*49cdfc7eSAndroid Build Coastguard Worker 	 */
64*49cdfc7eSAndroid Build Coastguard Worker 	{0, 1, 0, 1, 0, MLOCK_ONFAULT},
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	/* fault-in half of pages, lock all with MLOCK_ONFAULT */
67*49cdfc7eSAndroid Build Coastguard Worker 	{HPAGES, PAGES, 0, PAGES, HPAGES, MLOCK_ONFAULT},
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	/* fault-in 1 page, lock half of pages */
70*49cdfc7eSAndroid Build Coastguard Worker 	{1, HPAGES, 1, HPAGES + 1, 1, MLOCK_ONFAULT},
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	/* fault-in half, lock little less than 2 pages */
73*49cdfc7eSAndroid Build Coastguard Worker 	{HPAGES, HPAGES, -1, HPAGES, HPAGES, MLOCK_ONFAULT},
74*49cdfc7eSAndroid Build Coastguard Worker };
75*49cdfc7eSAndroid Build Coastguard Worker 
check_locked_pages(char * addr,size_t len,size_t num_pgs)76*49cdfc7eSAndroid Build Coastguard Worker static size_t check_locked_pages(char *addr, size_t len, size_t num_pgs)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	size_t n;
79*49cdfc7eSAndroid Build Coastguard Worker 	size_t act_pages = 0;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MINCORE(addr, len, vec);
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	for (n = 0; n < nr_pgs_to_nr_kernel_pgs(num_pgs); n++) {
84*49cdfc7eSAndroid Build Coastguard Worker 		if (vec[n] & 1)
85*49cdfc7eSAndroid Build Coastguard Worker 			act_pages++;
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	return nr_kernel_pgs_to_nr_pgs(act_pages);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker 
verify_mlock2(unsigned int n)91*49cdfc7eSAndroid Build Coastguard Worker static void verify_mlock2(unsigned int n)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
94*49cdfc7eSAndroid Build Coastguard Worker 	size_t bsize, asize, act_vmlcks, act_pgs;
95*49cdfc7eSAndroid Build Coastguard Worker 	char *addr;
96*49cdfc7eSAndroid Build Coastguard Worker 
97*49cdfc7eSAndroid Build Coastguard Worker 	addr = SAFE_MMAP(NULL, PAGES * pgsz, PROT_WRITE,
98*49cdfc7eSAndroid Build Coastguard Worker 			 MAP_SHARED | MAP_ANONYMOUS, 0, 0);
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->populate_pages)
101*49cdfc7eSAndroid Build Coastguard Worker 		memset(addr, 0, tc->populate_pages * pgsz);
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &bsize);
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_mlock2, addr, tc->lock_pages * pgsz + tc->offset,
106*49cdfc7eSAndroid Build Coastguard Worker 			 tc->flag));
107*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %ld", &asize);
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
110*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->flag && TST_ERR == EINVAL)
111*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TCONF, "mlock2() didn't support MLOCK_ONFAULT");
112*49cdfc7eSAndroid Build Coastguard Worker 		else
113*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO, "mlock2() failed");
114*49cdfc7eSAndroid Build Coastguard Worker 		goto end2;
115*49cdfc7eSAndroid Build Coastguard Worker 	}
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	act_vmlcks = (asize - bsize) * 1024 / pgsz;
118*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_vmlcks != act_vmlcks) {
119*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "VmLck showed wrong %ld pages, expected %ld",
120*49cdfc7eSAndroid Build Coastguard Worker 			act_vmlcks, tc->exp_vmlcks);
121*49cdfc7eSAndroid Build Coastguard Worker 		goto end1;
122*49cdfc7eSAndroid Build Coastguard Worker 	}
123*49cdfc7eSAndroid Build Coastguard Worker 
124*49cdfc7eSAndroid Build Coastguard Worker 	act_pgs = check_locked_pages(addr, PAGES * pgsz, PAGES);
125*49cdfc7eSAndroid Build Coastguard Worker 	if (act_pgs != tc->exp_present_pages) {
126*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "mlock2(%d) locked %ld pages, expected %ld",
127*49cdfc7eSAndroid Build Coastguard Worker 			tc->flag, act_pgs, tc->exp_present_pages);
128*49cdfc7eSAndroid Build Coastguard Worker 	} else {
129*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "mlock2(%d) succeeded in locking %ld pages",
130*49cdfc7eSAndroid Build Coastguard Worker 			tc->flag, tc->exp_present_pages);
131*49cdfc7eSAndroid Build Coastguard Worker 	}
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker end1:
134*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNLOCK(addr, tc->lock_pages * pgsz + tc->offset);
135*49cdfc7eSAndroid Build Coastguard Worker end2:
136*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(addr, PAGES * pgsz);
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)139*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker 	pgsz = getpagesize();
142*49cdfc7eSAndroid Build Coastguard Worker 	MLOCK_PAGE_SIZE_EMULATION_OFFSET(tcases);
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
146*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
147*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_mlock2,
148*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
149*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
150*49cdfc7eSAndroid Build Coastguard Worker };
151