• 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/resource.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "tst_test.h"
44 #include "lapi/mmap.h"
45 
46 #define MAP_SIZE (4 * 1024)
47 #define TEST_FILE "testfile"
48 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
49 #define KSM_SYS_DIR	"/sys/kernel/mm/ksm"
50 
51 static struct stat st;
52 static long pagesize;
53 static char *file1;
54 static char *file2;
55 static char *file3;
56 static char *shared_anon;
57 static char *ptr_addr;
58 static char *tmp_addr;
59 static char *nonalign;
60 
61 static struct tcase {
62 	int advice;
63 	char *name;
64 	char **addr;
65 	int exp_errno;
66 	int skip;
67 } tcases[] = {
68 	{MADV_NORMAL,      "MADV_NORMAL",      &nonalign, EINVAL, 0},
69 	{1212,             "MADV_NORMAL",      &file1,    EINVAL, 0},
70 	{MADV_REMOVE,      "MADV_REMOVE",      &file1,    EINVAL, 0},
71 	{MADV_DONTNEED,    "MADV_DONTNEED",    &file1,    EINVAL, 1},
72 	{MADV_MERGEABLE,   "MADV_MERGEABLE",   &file1,    EINVAL, 0},
73 	{MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &file1,    EINVAL, 0},
74 	{MADV_NORMAL,      "MADV_NORMAL",      &file2,    ENOMEM, 0},
75 	{MADV_WILLNEED,    "MADV_WILLNEED",    &file2,    ENOMEM, 0},
76 	{MADV_WILLNEED,    "MADV_WILLNEED",    &tmp_addr,  EBADF, 0},
77 	{MADV_FREE,        "MADV_FREE",        &file1,    EINVAL, 0},
78 	{MADV_WIPEONFORK,  "MADV_WIPEONFORK",  &file1,    EINVAL, 0},
79 	{MADV_WIPEONFORK,  "MADV_WIPEONFORK shared_anon", &shared_anon, EINVAL, 0},
80 	{MADV_WIPEONFORK,  "MADV_WIPEONFORK private file backed", &file3, EINVAL, 0},
81 };
82 
tcases_filter(void)83 static void tcases_filter(void)
84 {
85 	unsigned int i;
86 
87 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
88 		struct tcase *tc = &tcases[i];
89 
90 		switch (tc->advice) {
91 		case MADV_DONTNEED:
92 #if !defined(UCLINUX)
93 			if (mlock(file1, st.st_size) < 0)
94 				tst_brk(TBROK | TERRNO, "mlock failed");
95 			tc->skip = 0;
96 #endif /* if !defined(UCLINUX) */
97 		break;
98 		case MADV_MERGEABLE:
99 		case MADV_UNMERGEABLE:
100 			/* kernel configured with CONFIG_KSM,
101 			 * skip EINVAL test for MADV_MERGEABLE. */
102 			if (access(KSM_SYS_DIR, F_OK) == 0)
103 				tc->skip = 1;
104 		break;
105 		case MADV_WILLNEED:
106 			/* In kernel commit 1998cc0, madvise(MADV_WILLNEED) to
107 			 * anon mem doesn't return -EBADF now, as now we support
108 			 * swap prefretch. */
109 			if (tc->exp_errno == EBADF)
110 				tc->skip = 1;
111 		break;
112 		case MADV_FREE:
113 			if ((tst_kvercmp(4, 5, 0)) < 0)
114 				tc->skip = 1;
115 		break;
116 		case MADV_WIPEONFORK:
117 			if ((tst_kvercmp(4, 14, 0)) < 0)
118 				tc->skip = 1;
119 		break;
120 		default:
121 		break;
122 		}
123 	}
124 }
125 
setup(void)126 static void setup(void)
127 {
128 	int i, fd;
129 
130 	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
131 
132 	pagesize = getpagesize();
133 
134 	/* Writing 16 pages of random data into this file */
135 	for (i = 0; i < (pagesize / 2); i++)
136 		SAFE_WRITE(SAFE_WRITE_ALL, fd, STR, sizeof(STR) - 1);
137 
138 	SAFE_FSTAT(fd, &st);
139 
140 	file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
141 	file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
142 	file3 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
143 	shared_anon = SAFE_MMAP(0, MAP_SIZE, PROT_READ, MAP_SHARED |
144 			MAP_ANONYMOUS, -1, 0);
145 
146 	nonalign = file1 + 100;
147 
148 	ptr_addr = SAFE_MALLOC(st.st_size);
149 	tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize);
150 
151 	/* unmap as last step to avoid subsequent mmap(s) pick same address */
152 	SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize);
153 	SAFE_CLOSE(fd);
154 
155 	tcases_filter();
156 }
157 
158 
advice_test(unsigned int i)159 static void advice_test(unsigned int i)
160 {
161 	struct tcase *tc = &tcases[i];
162 
163 	if (tc->skip == 1) {
164 		tst_res(TCONF, "%s is not supported", tc->name);
165 		return;
166 	}
167 
168 	TEST(madvise(*(tc->addr), st.st_size, tc->advice));
169 	if (TST_RET == -1) {
170 		if (TST_ERR == tc->exp_errno) {
171 			tst_res(TPASS | TTERRNO, "%s failed as expected", tc->name);
172 		} else {
173 			tst_res(TFAIL | TTERRNO,
174 					"%s failed unexpectedly; expected - %d : %s",
175 					tc->name, tc->exp_errno,
176 					tst_strerrno(TFAIL | TTERRNO));
177 		}
178 	} else {
179 		tst_res(TFAIL, "madvise succeeded unexpectedly");
180 	}
181 }
182 
cleanup(void)183 static void cleanup(void)
184 {
185 	free(ptr_addr);
186 	SAFE_MUNMAP(file1, st.st_size);
187 	SAFE_MUNMAP(file2, st.st_size - pagesize);
188 	SAFE_MUNMAP(file3, st.st_size);
189 	SAFE_MUNMAP(shared_anon, MAP_SIZE);
190 }
191 
192 static struct tst_test test = {
193 	.tcnt = ARRAY_SIZE(tcases),
194 	.test = advice_test,
195 	.needs_tmpdir = 1,
196 	.needs_root = 1,
197 	.setup = setup,
198 	.cleanup = cleanup,
199 };
200