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