xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/madvise/madvise02.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) International Business Machines Corp., 2004
4*49cdfc7eSAndroid Build Coastguard Worker  *  Copyright (c) Linux Test Project, 2013-2016
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * This is a test for the madvise(2) system call. It is intended
9*49cdfc7eSAndroid Build Coastguard Worker  * to provide a complete exposure of the system call. It tests
10*49cdfc7eSAndroid Build Coastguard Worker  * madvise(2) for all error conditions to occur correctly.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * (A) Test Case for EINVAL
13*49cdfc7eSAndroid Build Coastguard Worker  *  1. start is not page-aligned
14*49cdfc7eSAndroid Build Coastguard Worker  *  2. advice is not a valid value
15*49cdfc7eSAndroid Build Coastguard Worker  *  3. application is attempting to release
16*49cdfc7eSAndroid Build Coastguard Worker  *     locked or shared pages (with MADV_DONTNEED)
17*49cdfc7eSAndroid Build Coastguard Worker  *  4. MADV_MERGEABLE or MADV_UNMERGEABLE was specified in advice,
18*49cdfc7eSAndroid Build Coastguard Worker  *     but the kernel was not configured with CONFIG_KSM.
19*49cdfc7eSAndroid Build Coastguard Worker  *  8|9. The MADV_FREE & MADV_WIPEONFORK operation can be applied
20*49cdfc7eSAndroid Build Coastguard Worker  *  	only to private anonymous pages.
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * (B) Test Case for ENOMEM
23*49cdfc7eSAndroid Build Coastguard Worker  *  5|6. addresses in the specified range are not currently mapped
24*49cdfc7eSAndroid Build Coastguard Worker  *     or are outside the address space of the process
25*49cdfc7eSAndroid Build Coastguard Worker  *  b. Not enough memory - paging in failed
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  * (C) Test Case for EBADF
28*49cdfc7eSAndroid Build Coastguard Worker  *  7. the map exists,
29*49cdfc7eSAndroid Build Coastguard Worker  *     but the area maps something that isn't a file.
30*49cdfc7eSAndroid Build Coastguard Worker  */
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/resource.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
44*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mmap.h"
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker #define MAP_SIZE (4 * 1024)
47*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "testfile"
48*49cdfc7eSAndroid Build Coastguard Worker #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
49*49cdfc7eSAndroid Build Coastguard Worker #define KSM_SYS_DIR	"/sys/kernel/mm/ksm"
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static struct stat st;
52*49cdfc7eSAndroid Build Coastguard Worker static long pagesize;
53*49cdfc7eSAndroid Build Coastguard Worker static char *file1;
54*49cdfc7eSAndroid Build Coastguard Worker static char *file2;
55*49cdfc7eSAndroid Build Coastguard Worker static char *file3;
56*49cdfc7eSAndroid Build Coastguard Worker static char *shared_anon;
57*49cdfc7eSAndroid Build Coastguard Worker static char *ptr_addr;
58*49cdfc7eSAndroid Build Coastguard Worker static char *tmp_addr;
59*49cdfc7eSAndroid Build Coastguard Worker static char *nonalign;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker #define TCASE(adv, nm, add, ee) {adv, nm, add, ee, 0}
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
64*49cdfc7eSAndroid Build Coastguard Worker 	int advice;
65*49cdfc7eSAndroid Build Coastguard Worker 	char *name;
66*49cdfc7eSAndroid Build Coastguard Worker 	char **addr;
67*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
68*49cdfc7eSAndroid Build Coastguard Worker 	int skip;
69*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
70*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_NORMAL,      "MADV_NORMAL",      &nonalign, EINVAL),
71*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(1212,             "MADV_NORMAL",      &file1,    EINVAL),
72*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_REMOVE,      "MADV_REMOVE",      &file1,    EINVAL),
73*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_DONTNEED,    "MADV_DONTNEED",    &file1,    EINVAL),
74*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_MERGEABLE,   "MADV_MERGEABLE",   &file1,    EINVAL),
75*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &file1,    EINVAL),
76*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_NORMAL,      "MADV_NORMAL",      &file2,    ENOMEM),
77*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_WILLNEED,    "MADV_WILLNEED",    &file2,    ENOMEM),
78*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_WILLNEED,    "MADV_WILLNEED",    &tmp_addr,  EBADF),
79*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_FREE,        "MADV_FREE",        &file1,    EINVAL),
80*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_WIPEONFORK,  "MADV_WIPEONFORK",  &file1,    EINVAL),
81*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_WIPEONFORK,  "MADV_WIPEONFORK shared_anon", &shared_anon, EINVAL),
82*49cdfc7eSAndroid Build Coastguard Worker 	TCASE(MADV_WIPEONFORK,  "MADV_WIPEONFORK private file backed", &file3, EINVAL),
83*49cdfc7eSAndroid Build Coastguard Worker };
84*49cdfc7eSAndroid Build Coastguard Worker 
tcases_filter(void)85*49cdfc7eSAndroid Build Coastguard Worker static void tcases_filter(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
90*49cdfc7eSAndroid Build Coastguard Worker 		struct tcase *tc = &tcases[i];
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 		switch (tc->advice) {
93*49cdfc7eSAndroid Build Coastguard Worker 		case MADV_DONTNEED:
94*49cdfc7eSAndroid Build Coastguard Worker 			if (mlock(file1, st.st_size) < 0)
95*49cdfc7eSAndroid Build Coastguard Worker 				tst_brk(TBROK | TERRNO, "mlock failed");
96*49cdfc7eSAndroid Build Coastguard Worker 		break;
97*49cdfc7eSAndroid Build Coastguard Worker 		case MADV_MERGEABLE:
98*49cdfc7eSAndroid Build Coastguard Worker 		case MADV_UNMERGEABLE:
99*49cdfc7eSAndroid Build Coastguard Worker 			/* kernel configured with CONFIG_KSM,
100*49cdfc7eSAndroid Build Coastguard Worker 			 * skip EINVAL test for MADV_MERGEABLE. */
101*49cdfc7eSAndroid Build Coastguard Worker 			if (access(KSM_SYS_DIR, F_OK) == 0)
102*49cdfc7eSAndroid Build Coastguard Worker 				tc->skip = 1;
103*49cdfc7eSAndroid Build Coastguard Worker 		break;
104*49cdfc7eSAndroid Build Coastguard Worker 		case MADV_WILLNEED:
105*49cdfc7eSAndroid Build Coastguard Worker 			/* In kernel commit 1998cc0, madvise(MADV_WILLNEED) to
106*49cdfc7eSAndroid Build Coastguard Worker 			 * anon mem doesn't return -EBADF now, as now we support
107*49cdfc7eSAndroid Build Coastguard Worker 			 * swap prefretch. */
108*49cdfc7eSAndroid Build Coastguard Worker 			if (tc->exp_errno == EBADF)
109*49cdfc7eSAndroid Build Coastguard Worker 				tc->skip = 1;
110*49cdfc7eSAndroid Build Coastguard Worker 		break;
111*49cdfc7eSAndroid Build Coastguard Worker 		case MADV_FREE:
112*49cdfc7eSAndroid Build Coastguard Worker 			if ((tst_kvercmp(4, 5, 0)) < 0)
113*49cdfc7eSAndroid Build Coastguard Worker 				tc->skip = 1;
114*49cdfc7eSAndroid Build Coastguard Worker 		break;
115*49cdfc7eSAndroid Build Coastguard Worker 		case MADV_WIPEONFORK:
116*49cdfc7eSAndroid Build Coastguard Worker 			if ((tst_kvercmp(4, 14, 0)) < 0)
117*49cdfc7eSAndroid Build Coastguard Worker 				tc->skip = 1;
118*49cdfc7eSAndroid Build Coastguard Worker 		break;
119*49cdfc7eSAndroid Build Coastguard Worker 		default:
120*49cdfc7eSAndroid Build Coastguard Worker 		break;
121*49cdfc7eSAndroid Build Coastguard Worker 		}
122*49cdfc7eSAndroid Build Coastguard Worker 	}
123*49cdfc7eSAndroid Build Coastguard Worker }
124*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)125*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
126*49cdfc7eSAndroid Build Coastguard Worker {
127*49cdfc7eSAndroid Build Coastguard Worker 	int i, fd;
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	pagesize = getpagesize();
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	/* Writing 16 pages of random data into this file */
134*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < (pagesize / 2); i++)
135*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WRITE(SAFE_WRITE_ALL, fd, STR, sizeof(STR) - 1);
136*49cdfc7eSAndroid Build Coastguard Worker 
137*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FSTAT(fd, &st);
138*49cdfc7eSAndroid Build Coastguard Worker 
139*49cdfc7eSAndroid Build Coastguard Worker 	file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
140*49cdfc7eSAndroid Build Coastguard Worker 	file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
141*49cdfc7eSAndroid Build Coastguard Worker 	file3 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
142*49cdfc7eSAndroid Build Coastguard Worker 	shared_anon = SAFE_MMAP(0, MAP_SIZE, PROT_READ, MAP_SHARED |
143*49cdfc7eSAndroid Build Coastguard Worker 			MAP_ANONYMOUS, -1, 0);
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 	nonalign = file1 + 100;
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 	ptr_addr = SAFE_MALLOC(st.st_size);
148*49cdfc7eSAndroid Build Coastguard Worker 	tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize);
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	/* unmap as last step to avoid subsequent mmap(s) pick same address */
151*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize);
152*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 	tcases_filter();
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker 
157*49cdfc7eSAndroid Build Coastguard Worker 
advice_test(unsigned int i)158*49cdfc7eSAndroid Build Coastguard Worker static void advice_test(unsigned int i)
159*49cdfc7eSAndroid Build Coastguard Worker {
160*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->skip == 1) {
163*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TCONF, "%s is not supported", tc->name);
164*49cdfc7eSAndroid Build Coastguard Worker 		return;
165*49cdfc7eSAndroid Build Coastguard Worker 	}
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 	TEST(madvise(*(tc->addr), st.st_size, tc->advice));
168*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
169*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_ERR == tc->exp_errno) {
170*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS | TTERRNO, "%s failed as expected", tc->name);
171*49cdfc7eSAndroid Build Coastguard Worker 		} else {
172*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO,
173*49cdfc7eSAndroid Build Coastguard Worker 					"%s failed unexpectedly; expected - %d : %s",
174*49cdfc7eSAndroid Build Coastguard Worker 					tc->name, tc->exp_errno,
175*49cdfc7eSAndroid Build Coastguard Worker 					tst_strerrno(TFAIL | TTERRNO));
176*49cdfc7eSAndroid Build Coastguard Worker 		}
177*49cdfc7eSAndroid Build Coastguard Worker 	} else {
178*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "madvise succeeded unexpectedly");
179*49cdfc7eSAndroid Build Coastguard Worker 	}
180*49cdfc7eSAndroid Build Coastguard Worker }
181*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)182*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
183*49cdfc7eSAndroid Build Coastguard Worker {
184*49cdfc7eSAndroid Build Coastguard Worker 	free(ptr_addr);
185*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(file1, st.st_size);
186*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(file2, st.st_size - pagesize);
187*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(file3, st.st_size);
188*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(shared_anon, MAP_SIZE);
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker 
191*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
192*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
193*49cdfc7eSAndroid Build Coastguard Worker 	.test = advice_test,
194*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
195*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
196*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
197*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
198*49cdfc7eSAndroid Build Coastguard Worker };
199