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