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