1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2012 Linux Test Project, Inc.
4 */
5
6 /*
7 * functional test for readahead() syscall
8 *
9 * This test is measuring effects of readahead syscall.
10 * It mmaps/reads a test file with and without prior call to readahead.
11 *
12 * The overlay part of the test is regression for:
13 * b833a3660394
14 * ("ovl: add ovl_fadvise()")
15 * Introduced by:
16 * 5b910bd615ba
17 * ("ovl: fix GPF in swapfile_activate of file from overlayfs over xfs")
18 */
19 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/syscall.h>
22 #include <sys/mman.h>
23 #include <sys/mount.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include "config.h"
33 #include "tst_test.h"
34 #include "tst_timer.h"
35 #include "lapi/syscalls.h"
36
37 static char testfile[PATH_MAX] = "testfile";
38 #define DROP_CACHES_FNAME "/proc/sys/vm/drop_caches"
39 #define MEMINFO_FNAME "/proc/meminfo"
40 #define PROC_IO_FNAME "/proc/self/io"
41 static size_t testfile_size = 64 * 1024 * 1024;
42 static char *opt_fsizestr;
43 static int pagesize;
44 static unsigned long cached_max;
45 static int ovl_mounted;
46 static int readahead_length = 4096;
47 static char sys_bdi_ra_path[PATH_MAX];
48 static int orig_bdi_limit;
49
50 static const char mntpoint[] = OVL_BASE_MNTPOINT;
51
libc_readahead(int fd,off_t offset,size_t len)52 static int libc_readahead(int fd, off_t offset, size_t len)
53 {
54 return readahead(fd, offset, len);
55 }
56
fadvise_willneed(int fd,off_t offset,size_t len)57 static int fadvise_willneed(int fd, off_t offset, size_t len)
58 {
59 /* Should have the same effect as readahead() syscall */
60 errno = posix_fadvise(fd, offset, len, POSIX_FADV_WILLNEED);
61 /* posix_fadvise returns error number (not in errno) */
62 return errno ? -1 : 0;
63 }
64
65 static struct tcase {
66 const char *tname;
67 int use_overlay;
68 int use_fadvise;
69 /* Use either readahead() syscall or POSIX_FADV_WILLNEED */
70 int (*readahead)(int, off_t, size_t);
71 } tcases[] = {
72 { "readahead on file", 0, 0, libc_readahead },
73 { "readahead on overlayfs file", 1, 0, libc_readahead },
74 { "POSIX_FADV_WILLNEED on file", 0, 1, fadvise_willneed },
75 { "POSIX_FADV_WILLNEED on overlayfs file", 1, 1, fadvise_willneed },
76 };
77
78 static int readahead_supported = 1;
79 static int fadvise_supported = 1;
80
has_file(const char * fname,int required)81 static int has_file(const char *fname, int required)
82 {
83 struct stat buf;
84
85 if (stat(fname, &buf) == -1) {
86 if (errno != ENOENT)
87 tst_brk(TBROK | TERRNO, "stat %s", fname);
88 if (required)
89 tst_brk(TCONF, "%s not available", fname);
90 return 0;
91 }
92 return 1;
93 }
94
drop_caches(void)95 static void drop_caches(void)
96 {
97 SAFE_FILE_PRINTF(DROP_CACHES_FNAME, "1");
98 }
99
get_bytes_read(void)100 static unsigned long get_bytes_read(void)
101 {
102 unsigned long ret;
103
104 SAFE_FILE_LINES_SCANF(PROC_IO_FNAME, "read_bytes: %lu", &ret);
105
106 return ret;
107 }
108
get_cached_size(void)109 static unsigned long get_cached_size(void)
110 {
111 unsigned long ret;
112
113 SAFE_FILE_LINES_SCANF(MEMINFO_FNAME, "Cached: %lu", &ret);
114
115 return ret;
116 }
117
create_testfile(int use_overlay)118 static void create_testfile(int use_overlay)
119 {
120 int fd;
121 char *tmp;
122 size_t i;
123
124 sprintf(testfile, "%s/testfile",
125 use_overlay ? OVL_MNT : OVL_BASE_MNTPOINT);
126 tst_res(TINFO, "creating test file of size: %zu", testfile_size);
127 tmp = SAFE_MALLOC(pagesize);
128
129 /* round to page size */
130 testfile_size = testfile_size & ~((long)pagesize - 1);
131
132 fd = SAFE_CREAT(testfile, 0644);
133 for (i = 0; i < testfile_size; i += pagesize)
134 SAFE_WRITE(1, fd, tmp, pagesize);
135 SAFE_FSYNC(fd);
136 SAFE_CLOSE(fd);
137 free(tmp);
138 }
139
140 /* read_testfile - mmap testfile and read every page.
141 * This functions measures how many I/O and time it takes to fully
142 * read contents of test file.
143 *
144 * @do_readahead: call readahead prior to reading file content?
145 * @fname: name of file to test
146 * @fsize: how many bytes to read/mmap
147 * @read_bytes: returns difference of bytes read, parsed from /proc/<pid>/io
148 * @usec: returns how many microsecond it took to go over fsize bytes
149 * @cached: returns cached kB from /proc/meminfo
150 */
read_testfile(struct tcase * tc,int do_readahead,const char * fname,size_t fsize,unsigned long * read_bytes,long long * usec,unsigned long * cached)151 static int read_testfile(struct tcase *tc, int do_readahead,
152 const char *fname, size_t fsize,
153 unsigned long *read_bytes, long long *usec,
154 unsigned long *cached)
155 {
156 int fd;
157 size_t i = 0;
158 long read_bytes_start;
159 unsigned char *p, tmp;
160 off_t offset = 0;
161
162 fd = SAFE_OPEN(fname, O_RDONLY);
163
164 if (do_readahead) {
165 do {
166 TEST(tc->readahead(fd, offset, fsize - offset));
167 if (TST_RET != 0) {
168 SAFE_CLOSE(fd);
169 return TST_ERR;
170 }
171
172 i++;
173 offset += readahead_length;
174 } while ((size_t)offset < fsize);
175 tst_res(TINFO, "readahead calls made: %zu", i);
176 *cached = get_cached_size();
177
178 /* offset of file shouldn't change after readahead */
179 offset = SAFE_LSEEK(fd, 0, SEEK_CUR);
180 if (offset == 0)
181 tst_res(TPASS, "offset is still at 0 as expected");
182 else
183 tst_res(TFAIL, "offset has changed to: %lu", offset);
184 }
185
186 tst_timer_start(CLOCK_MONOTONIC);
187 read_bytes_start = get_bytes_read();
188
189 p = SAFE_MMAP(NULL, fsize, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0);
190
191 /* for old kernels, where MAP_POPULATE doesn't work, touch each page */
192 tmp = 0;
193 for (i = 0; i < fsize; i += pagesize)
194 tmp = tmp ^ p[i];
195 /* prevent gcc from optimizing out loop above */
196 if (tmp != 0)
197 tst_brk(TBROK, "This line should not be reached");
198
199 if (!do_readahead)
200 *cached = get_cached_size();
201
202 SAFE_MUNMAP(p, fsize);
203
204 *read_bytes = get_bytes_read() - read_bytes_start;
205
206 tst_timer_stop();
207 *usec = tst_timer_elapsed_us();
208
209 SAFE_CLOSE(fd);
210 return 0;
211 }
212
test_readahead(unsigned int n)213 static void test_readahead(unsigned int n)
214 {
215 unsigned long read_bytes, read_bytes_ra;
216 long long usec, usec_ra;
217 unsigned long cached_high, cached_low, cached, cached_ra;
218 int ret;
219 struct tcase *tc = &tcases[n];
220
221 tst_res(TINFO, "Test #%d: %s", n, tc->tname);
222
223 if (tc->use_overlay && !ovl_mounted) {
224 tst_res(TCONF,
225 "overlayfs is not configured in this kernel.");
226 return;
227 }
228
229 create_testfile(tc->use_overlay);
230
231 /* find out how much can cache hold if we read whole file */
232 read_testfile(tc, 0, testfile, testfile_size, &read_bytes, &usec,
233 &cached);
234 cached_high = get_cached_size();
235 sync();
236 drop_caches();
237 cached_low = get_cached_size();
238 cached_max = MAX(cached_max, cached_high - cached_low);
239
240 tst_res(TINFO, "read_testfile(0)");
241 read_testfile(tc, 0, testfile, testfile_size, &read_bytes, &usec,
242 &cached);
243 if (cached > cached_low)
244 cached = cached - cached_low;
245 else
246 cached = 0;
247
248 sync();
249 drop_caches();
250 cached_low = get_cached_size();
251 tst_res(TINFO, "read_testfile(1)");
252 ret = read_testfile(tc, 1, testfile, testfile_size, &read_bytes_ra,
253 &usec_ra, &cached_ra);
254
255 if (ret == EINVAL) {
256 if (tc->use_fadvise &&
257 (!tc->use_overlay || !fadvise_supported)) {
258 fadvise_supported = 0;
259 tst_res(TCONF, "CONFIG_ADVISE_SYSCALLS not configured "
260 "in kernel?");
261 return;
262 }
263
264 if (!tc->use_overlay || !readahead_supported) {
265 readahead_supported = 0;
266 tst_res(TCONF, "readahead not supported on %s",
267 tst_device->fs_type);
268 return;
269 }
270 }
271
272 if (ret) {
273 tst_res(TFAIL | TTERRNO, "%s failed on %s",
274 tc->use_fadvise ? "fadvise" : "readahead",
275 tc->use_overlay ? "overlayfs" :
276 tst_device->fs_type);
277 return;
278 }
279
280 if (cached_ra > cached_low)
281 cached_ra = cached_ra - cached_low;
282 else
283 cached_ra = 0;
284
285 tst_res(TINFO, "read_testfile(0) took: %lli usec", usec);
286 tst_res(TINFO, "read_testfile(1) took: %lli usec", usec_ra);
287 if (has_file(PROC_IO_FNAME, 0)) {
288 tst_res(TINFO, "read_testfile(0) read: %ld bytes", read_bytes);
289 tst_res(TINFO, "read_testfile(1) read: %ld bytes",
290 read_bytes_ra);
291 /* actual number of read bytes depends on total RAM */
292 if (read_bytes_ra < read_bytes)
293 tst_res(TPASS, "readahead saved some I/O");
294 else
295 tst_res(TFAIL, "readahead failed to save any I/O");
296 } else {
297 tst_res(TCONF, "Your system doesn't have /proc/self/io,"
298 " unable to determine read bytes during test");
299 }
300
301 tst_res(TINFO, "cache can hold at least: %ld kB", cached_max);
302 tst_res(TINFO, "read_testfile(0) used cache: %ld kB", cached);
303 tst_res(TINFO, "read_testfile(1) used cache: %ld kB", cached_ra);
304
305 if (cached_max * 1024 >= testfile_size) {
306 /*
307 * if cache can hold ~testfile_size then cache increase
308 * for readahead should be at least testfile_size/2
309 */
310 if (cached_ra * 1024 > testfile_size / 2)
311 tst_res(TPASS, "using cache as expected");
312 else if (!cached_ra)
313 tst_res(TFAIL, "readahead failed to use any cache");
314 else
315 tst_res(TWARN, "using less cache than expected");
316 } else {
317 tst_res(TCONF, "Page cache on your system is too small "
318 "to hold whole testfile.");
319 }
320 }
321
322
323 /*
324 * We try raising bdi readahead limit as much as we can. We write
325 * and read back "read_ahead_kb" sysfs value, starting with filesize.
326 * If that fails, we try again with lower value.
327 * readahead_length used in the test is then set to MIN(bdi limit, 2M),
328 * to respect kernels prior to commit 600e19afc5f8a6c.
329 */
setup_readahead_length(void)330 static void setup_readahead_length(void)
331 {
332 struct stat sbuf;
333 char tmp[PATH_MAX], *backing_dev;
334 int ra_new_limit, ra_limit;
335
336 /* Find out backing device name */
337 SAFE_LSTAT(tst_device->dev, &sbuf);
338 if (S_ISLNK(sbuf.st_mode))
339 SAFE_READLINK(tst_device->dev, tmp, PATH_MAX);
340 else
341 strcpy(tmp, tst_device->dev);
342
343 backing_dev = basename(tmp);
344 sprintf(sys_bdi_ra_path, "/sys/class/block/%s/bdi/read_ahead_kb",
345 backing_dev);
346 if (access(sys_bdi_ra_path, F_OK))
347 return;
348
349 SAFE_FILE_SCANF(sys_bdi_ra_path, "%d", &orig_bdi_limit);
350
351 /* raise bdi limit as much as kernel allows */
352 ra_new_limit = testfile_size / 1024;
353 while (ra_new_limit > pagesize / 1024) {
354 FILE_PRINTF(sys_bdi_ra_path, "%d", ra_new_limit);
355 SAFE_FILE_SCANF(sys_bdi_ra_path, "%d", &ra_limit);
356
357 if (ra_limit == ra_new_limit) {
358 readahead_length = MIN(ra_new_limit * 1024,
359 2 * 1024 * 1024);
360 break;
361 }
362 ra_new_limit = ra_new_limit / 2;
363 }
364 }
365
setup(void)366 static void setup(void)
367 {
368 if (opt_fsizestr)
369 testfile_size = SAFE_STRTOL(opt_fsizestr, 1, INT_MAX);
370
371 if (access(PROC_IO_FNAME, F_OK))
372 tst_brk(TCONF, "Requires " PROC_IO_FNAME);
373
374 has_file(DROP_CACHES_FNAME, 1);
375 has_file(MEMINFO_FNAME, 1);
376
377 /* check if readahead is supported */
378 tst_syscall(__NR_readahead, 0, 0, 0);
379
380 pagesize = getpagesize();
381
382 setup_readahead_length();
383 tst_res(TINFO, "readahead length: %d", readahead_length);
384
385 ovl_mounted = TST_MOUNT_OVERLAY();
386 }
387
cleanup(void)388 static void cleanup(void)
389 {
390 if (ovl_mounted)
391 SAFE_UMOUNT(OVL_MNT);
392
393 if (orig_bdi_limit)
394 SAFE_FILE_PRINTF(sys_bdi_ra_path, "%d", orig_bdi_limit);
395 }
396
397 static struct tst_test test = {
398 .needs_root = 1,
399 .mount_device = 1,
400 .mntpoint = mntpoint,
401 .setup = setup,
402 .cleanup = cleanup,
403 .options = (struct tst_option[]) {
404 {"s:", &opt_fsizestr, "Testfile size (default 64MB)"},
405 {}
406 },
407 .test = test_readahead,
408 .tcnt = ARRAY_SIZE(tcases),
409 .tags = (const struct tst_tag[]) {
410 {"linux-git", "b833a3660394"},
411 {"linux-git", "5b910bd615ba"},
412 {}
413 }
414 };
415