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 case for madvise(2) system call.
9 * It tests madvise(2) with combinations of advice values.
10 * No error should be returned.
11 */
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/mount.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include "tst_test.h"
23 #include "lapi/mmap.h"
24
25 #define TMP_DIR "tmp_madvise"
26 #define TEST_FILE TMP_DIR"/testfile"
27 #define KSM_SYS_DIR "/sys/kernel/mm/ksm"
28 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
29
30 static char *sfile;
31 static char *amem;
32 static struct stat st;
33
34 static struct tcase {
35 int advice;
36 char *name;
37 char **addr;
38 } tcases[] = {
39 {MADV_NORMAL, "MADV_NORMAL", &sfile},
40 {MADV_RANDOM, "MADV_RANDOM", &sfile},
41 {MADV_SEQUENTIAL, "MADV_SEQUENTIAL", &sfile},
42 {MADV_WILLNEED, "MADV_WILLNEED", &sfile},
43 {MADV_DONTNEED, "MADV_DONTNEED", &sfile},
44 {MADV_REMOVE, "MADV_REMOVE", &sfile}, /* since Linux 2.6.16 */
45 {MADV_DONTFORK, "MADV_DONTFORK", &sfile}, /* since Linux 2.6.16 */
46 {MADV_DOFORK, "MADV_DOFORK", &sfile}, /* since Linux 2.6.16 */
47 {MADV_HWPOISON, "MADV_HWPOISON", &sfile}, /* since Linux 2.6.32 */
48 {MADV_MERGEABLE, "MADV_MERGEABLE", &sfile}, /* since Linux 2.6.32 */
49 {MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &sfile}, /* since Linux 2.6.32 */
50 {MADV_HUGEPAGE, "MADV_HUGEPAGE", &amem}, /* since Linux 2.6.38 */
51 {MADV_NOHUGEPAGE, "MADV_NOHUGEPAGE", &amem}, /* since Linux 2.6.38 */
52 {MADV_DONTDUMP, "MADV_DONTDUMP", &sfile}, /* since Linux 3.4 */
53 {MADV_DODUMP, "MADV_DODUMP", &sfile}, /* since Linux 3.4 */
54 {MADV_FREE, "MADV_FREE", &amem}, /* since Linux 4.5 */
55 {MADV_WIPEONFORK, "MADV_WIPEONFORK", &amem}, /* since Linux 4.14 */
56 {MADV_KEEPONFORK, "MADV_KEEPONFORK", &amem}, /* since Linux 4.14 */
57 {MADV_COLD, "MADV_COLD", &amem}, /* since Linux 5.4 */
58 {MADV_PAGEOUT, "MADV_PAGEOUT", &amem}, /* since Linux 5.4 */
59
60 };
61
setup(void)62 static void setup(void)
63 {
64 unsigned int i;
65 int fd;
66
67 SAFE_MKDIR(TMP_DIR, 0664);
68 SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL);
69
70 fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
71
72 /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */
73 for (i = 0; i < 1280; i++)
74 SAFE_WRITE(SAFE_WRITE_ALL, fd, STR, strlen(STR));
75
76 SAFE_FSTAT(fd, &st);
77
78 /* Map the input file into shared memory */
79 sfile = SAFE_MMAP(NULL, st.st_size,
80 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
81
82 /* Map the input file into private memory. MADV_HUGEPAGE only works
83 * with private anonymous pages */
84 amem = SAFE_MMAP(NULL, st.st_size,
85 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
86
87 SAFE_CLOSE(fd);
88 }
89
cleanup(void)90 static void cleanup(void)
91 {
92 SAFE_MUNMAP(sfile, st.st_size);
93 SAFE_MUNMAP(amem, st.st_size);
94 SAFE_UMOUNT(TMP_DIR);
95 }
96
verify_madvise(unsigned int i)97 static void verify_madvise(unsigned int i)
98 {
99 struct tcase *tc = &tcases[i];
100
101 TEST(madvise(*(tc->addr), st.st_size, tc->advice));
102
103 if (TST_RET == -1) {
104 if (TST_ERR == EINVAL) {
105 tst_res(TCONF, "%s is not supported", tc->name);
106 } else {
107 tst_res(TFAIL, "madvise test for %s failed with "
108 "return = %ld, errno = %d : %s",
109 tc->name, TST_RET, TST_ERR,
110 tst_strerrno(TFAIL | TTERRNO));
111 }
112 } else {
113 tst_res(TPASS, "madvise test for %s PASSED", tc->name);
114 }
115 }
116
117 static struct tst_test test = {
118 .tcnt = ARRAY_SIZE(tcases),
119 .test = verify_madvise,
120 .needs_tmpdir = 1,
121 .needs_root = 1,
122 .setup = setup,
123 .cleanup = cleanup,
124 };
125