xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mlock2/mlock203.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  * If one memory is already locked by mlock2() with MLOCK_ONFAULT and then
9*49cdfc7eSAndroid Build Coastguard Worker  * it is locked again by mlock()(or mlock2() without MLOCK_ONFAULT), the
10*49cdfc7eSAndroid Build Coastguard Worker  * VmLck field in /proc/pid/status should increase once instead of twice.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * This issue has been fixed in kernel:
13*49cdfc7eSAndroid Build Coastguard Worker  * 'b155b4fde5bd("mm: mlock: avoid increase mm->locked_vm on mlock() when already mlock2(,MLOCK_ONFAULT)")'
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <linux/mman.h>
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mlock2.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker static unsigned long pgsz;
25*49cdfc7eSAndroid Build Coastguard Worker static char *addr;
26*49cdfc7eSAndroid Build Coastguard Worker 
verify_mlock2(void)27*49cdfc7eSAndroid Build Coastguard Worker static void verify_mlock2(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long bsize, asize1, asize2;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &bsize);
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_mlock2, addr, pgsz, MLOCK_ONFAULT));
34*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
35*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_ERR == EINVAL) {
36*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TCONF,
37*49cdfc7eSAndroid Build Coastguard Worker 				"mlock2() didn't support MLOCK_ONFAULT");
38*49cdfc7eSAndroid Build Coastguard Worker 		} else {
39*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO,
40*49cdfc7eSAndroid Build Coastguard Worker 				"mlock2(MLOCK_ONFAULT) failed");
41*49cdfc7eSAndroid Build Coastguard Worker 		}
42*49cdfc7eSAndroid Build Coastguard Worker 		return;
43*49cdfc7eSAndroid Build Coastguard Worker 	}
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &asize1);
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	if ((asize1 - bsize) * 1024 != pgsz) {
48*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL,
49*49cdfc7eSAndroid Build Coastguard Worker 			"mlock2(MLOCK_ONFAULT) locked %lu size, expected %lu",
50*49cdfc7eSAndroid Build Coastguard Worker 			(asize1 - bsize) * 1024, pgsz);
51*49cdfc7eSAndroid Build Coastguard Worker 		goto end;
52*49cdfc7eSAndroid Build Coastguard Worker 	}
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_mlock2, addr, pgsz, 0));
55*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != 0) {
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "mlock2() failed");
57*49cdfc7eSAndroid Build Coastguard Worker 		goto end;
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_LINES_SCANF("/proc/self/status", "VmLck: %lu", &asize2);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	if (asize1 != asize2) {
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Locking one memory again increased VmLck");
64*49cdfc7eSAndroid Build Coastguard Worker 	} else {
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS,
66*49cdfc7eSAndroid Build Coastguard Worker 			"Locking one memory again didn't increased VmLck");
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker end:
70*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNLOCK(addr, pgsz);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)73*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	pgsz = getpagesize();
76*49cdfc7eSAndroid Build Coastguard Worker 	addr = SAFE_MMAP(NULL, pgsz, PROT_WRITE,
77*49cdfc7eSAndroid Build Coastguard Worker 			 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	if (addr)
83*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(addr, pgsz);
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
87*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_mlock2,
88*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
89*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
90*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
91*49cdfc7eSAndroid Build Coastguard Worker };
92