1 // SPDX-License-Identifier: GPL-2.0+
3 * kselftest suite for mincore().
5 * Copyright (C) 2020 Collabora, Ltd.
18 #include "../kselftest.h"
19 #include "../kselftest_harness.h"
21 /* Default test file size: 4MB */
22 #define MB (1UL << 20)
23 #define FILE_SIZE (4 * MB)
27 * Tests the user interface. This test triggers most of the documented
28 * error conditions in mincore().
37 page_size = sysconf(_SC_PAGESIZE);
39 /* Query a 0 byte sized range */
40 retval = mincore(0, 0, vec);
43 /* Addresses in the specified range are invalid or unmapped */
45 retval = mincore(NULL, page_size, vec);
46 EXPECT_EQ(-1, retval);
47 EXPECT_EQ(ENOMEM, errno);
50 addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
51 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
52 ASSERT_NE(MAP_FAILED, addr) {
53 TH_LOG("mmap error: %s", strerror(errno));
56 /* <addr> argument is not page-aligned */
58 retval = mincore(addr + 1, page_size, vec);
59 EXPECT_EQ(-1, retval);
60 EXPECT_EQ(EINVAL, errno);
62 /* <length> argument is too large */
64 retval = mincore(addr, -1, vec);
65 EXPECT_EQ(-1, retval);
66 EXPECT_EQ(ENOMEM, errno);
68 /* <vec> argument points to an illegal address */
70 retval = mincore(addr, page_size, NULL);
71 EXPECT_EQ(-1, retval);
72 EXPECT_EQ(EFAULT, errno);
73 munmap(addr, page_size);
78 * Test mincore() behavior on a private anonymous page mapping.
79 * Check that the page is not loaded into memory right after the mapping
80 * but after accessing it (on-demand allocation).
81 * Then free the page and check that it's not memory-resident.
83 TEST(check_anonymous_locked_pages)
90 page_size = sysconf(_SC_PAGESIZE);
92 /* Map one page and check it's not memory-resident */
94 addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
95 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
96 ASSERT_NE(MAP_FAILED, addr) {
97 TH_LOG("mmap error: %s", strerror(errno));
99 retval = mincore(addr, page_size, vec);
100 ASSERT_EQ(0, retval);
101 ASSERT_EQ(0, vec[0]) {
102 TH_LOG("Page found in memory before use");
105 /* Touch the page and check again. It should now be in memory */
107 mlock(addr, page_size);
108 retval = mincore(addr, page_size, vec);
109 ASSERT_EQ(0, retval);
110 ASSERT_EQ(1, vec[0]) {
111 TH_LOG("Page not found in memory after use");
115 * It shouldn't be memory-resident after unlocking it and
116 * marking it as unneeded.
118 munlock(addr, page_size);
119 madvise(addr, page_size, MADV_DONTNEED);
120 retval = mincore(addr, page_size, vec);
121 ASSERT_EQ(0, retval);
122 ASSERT_EQ(0, vec[0]) {
123 TH_LOG("Page in memory after being zapped");
125 munmap(addr, page_size);
130 * Check mincore() behavior on huge pages.
131 * This test will be skipped if the mapping fails (ie. if there are no
132 * huge pages available).
134 * Make sure the system has at least one free huge page, check
135 * "HugePages_Free" in /proc/meminfo.
136 * Increment /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages if
139 TEST(check_huge_pages)
141 unsigned char vec[1];
146 page_size = sysconf(_SC_PAGESIZE);
149 addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
150 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB,
152 if (addr == MAP_FAILED) {
154 SKIP(return, "No huge pages available.");
156 TH_LOG("mmap error: %s", strerror(errno));
158 retval = mincore(addr, page_size, vec);
159 ASSERT_EQ(0, retval);
160 ASSERT_EQ(0, vec[0]) {
161 TH_LOG("Page found in memory before use");
165 mlock(addr, page_size);
166 retval = mincore(addr, page_size, vec);
167 ASSERT_EQ(0, retval);
168 ASSERT_EQ(1, vec[0]) {
169 TH_LOG("Page not found in memory after use");
172 munlock(addr, page_size);
173 munmap(addr, page_size);
178 * Test mincore() behavior on a file-backed page.
179 * No pages should be loaded into memory right after the mapping. Then,
180 * accessing any address in the mapping range should load the page
181 * containing the address and a number of subsequent pages (readahead).
183 * The actual readahead settings depend on the test environment, so we
184 * can't make a lot of assumptions about that. This test covers the most
187 TEST(check_file_mmap)
198 page_size = sysconf(_SC_PAGESIZE);
199 vec_size = FILE_SIZE / page_size;
200 if (FILE_SIZE % page_size)
203 vec = calloc(vec_size, sizeof(unsigned char));
204 ASSERT_NE(NULL, vec) {
205 TH_LOG("Can't allocate array");
209 fd = open(".", O_TMPFILE | O_RDWR, 0600);
211 ASSERT_EQ(errno, EOPNOTSUPP) {
212 TH_LOG("Can't create temporary file: %s",
215 SKIP(goto out_free, "O_TMPFILE not supported by filesystem.");
218 retval = fallocate(fd, 0, 0, FILE_SIZE);
220 ASSERT_EQ(errno, EOPNOTSUPP) {
221 TH_LOG("Error allocating space for the temporary file: %s",
224 SKIP(goto out_close, "fallocate not supported by filesystem.");
228 * Map the whole file, the pages shouldn't be fetched yet.
231 addr = mmap(NULL, FILE_SIZE, PROT_READ | PROT_WRITE,
233 ASSERT_NE(MAP_FAILED, addr) {
234 TH_LOG("mmap error: %s", strerror(errno));
236 retval = mincore(addr, FILE_SIZE, vec);
237 ASSERT_EQ(0, retval);
238 for (i = 0; i < vec_size; i++) {
239 ASSERT_EQ(0, vec[i]) {
240 TH_LOG("Unexpected page in memory");
245 * Touch a page in the middle of the mapping. We expect the next
246 * few pages (the readahead window) to be populated too.
248 addr[FILE_SIZE / 2] = 1;
249 retval = mincore(addr, FILE_SIZE, vec);
250 ASSERT_EQ(0, retval);
251 ASSERT_EQ(1, vec[FILE_SIZE / 2 / page_size]) {
252 TH_LOG("Page not found in memory after use");
255 i = FILE_SIZE / 2 / page_size + 1;
256 while (i < vec_size && vec[i]) {
260 EXPECT_GT(ra_pages, 0) {
261 TH_LOG("No read-ahead pages found in memory");
264 EXPECT_LT(i, vec_size) {
265 TH_LOG("Read-ahead pages reached the end of the file");
268 * End of the readahead window. The rest of the pages shouldn't
272 while (i < vec_size && !vec[i])
274 EXPECT_EQ(vec_size, i) {
275 TH_LOG("Unexpected page in memory beyond readahead window");
279 munmap(addr, FILE_SIZE);
288 * Test mincore() behavior on a page backed by a tmpfs file. This test
289 * performs the same steps as the previous one. However, we don't expect
290 * any readahead in this case.
292 TEST(check_tmpfs_mmap)
303 page_size = sysconf(_SC_PAGESIZE);
304 vec_size = FILE_SIZE / page_size;
305 if (FILE_SIZE % page_size)
308 vec = calloc(vec_size, sizeof(unsigned char));
309 ASSERT_NE(NULL, vec) {
310 TH_LOG("Can't allocate array");
314 fd = open("/dev/shm", O_TMPFILE | O_RDWR, 0600);
316 TH_LOG("Can't create temporary file: %s",
320 retval = fallocate(fd, 0, 0, FILE_SIZE);
321 ASSERT_EQ(0, retval) {
322 TH_LOG("Error allocating space for the temporary file: %s",
327 * Map the whole file, the pages shouldn't be fetched yet.
330 addr = mmap(NULL, FILE_SIZE, PROT_READ | PROT_WRITE,
332 ASSERT_NE(MAP_FAILED, addr) {
333 TH_LOG("mmap error: %s", strerror(errno));
335 retval = mincore(addr, FILE_SIZE, vec);
336 ASSERT_EQ(0, retval);
337 for (i = 0; i < vec_size; i++) {
338 ASSERT_EQ(0, vec[i]) {
339 TH_LOG("Unexpected page in memory");
344 * Touch a page in the middle of the mapping. We expect only
345 * that page to be fetched into memory.
347 addr[FILE_SIZE / 2] = 1;
348 retval = mincore(addr, FILE_SIZE, vec);
349 ASSERT_EQ(0, retval);
350 ASSERT_EQ(1, vec[FILE_SIZE / 2 / page_size]) {
351 TH_LOG("Page not found in memory after use");
354 i = FILE_SIZE / 2 / page_size + 1;
355 while (i < vec_size && vec[i]) {
359 ASSERT_EQ(ra_pages, 0) {
360 TH_LOG("Read-ahead pages found in memory");
363 munmap(addr, FILE_SIZE);