1 /*
2 * Copyright (c) International Business Machines Corp., 2004
3 * Copyright (c) Linux Test Project, 2013-2016
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 */
15
16 /*
17 * This is a test case for madvise(2) system call.
18 * It tests madvise(2) with combinations of advice values.
19 * No error should be returned.
20 */
21
22 #include <sys/types.h>
23 #include <sys/mman.h>
24 #include <sys/stat.h>
25 #include <sys/mount.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "tst_test.h"
33 #include "lapi/mmap.h"
34
35 #define TMP_DIR "tmp_madvise"
36 #define TEST_FILE TMP_DIR"/testfile"
37 #define KSM_SYS_DIR "/sys/kernel/mm/ksm"
38 #define STR "abcdefghijklmnopqrstuvwxyz12345\n"
39
40 static char *sfile;
41 static char *pfile;
42 static struct stat st;
43
44 static struct tcase {
45 int advice;
46 char *name;
47 char **addr;
48 } tcases[] = {
49 {MADV_NORMAL, "MADV_NORMAL", &sfile},
50 {MADV_RANDOM, "MADV_RANDOM", &sfile},
51 {MADV_SEQUENTIAL, "MADV_SEQUENTIAL", &sfile},
52 {MADV_WILLNEED, "MADV_WILLNEED", &sfile},
53 {MADV_DONTNEED, "MADV_DONTNEED", &sfile},
54 {MADV_REMOVE, "MADV_REMOVE", &sfile}, /* since Linux 2.6.16 */
55 {MADV_DONTFORK, "MADV_DONTFORK", &sfile}, /* since Linux 2.6.16 */
56 {MADV_DOFORK, "MADV_DOFORK", &sfile}, /* since Linux 2.6.16 */
57 {MADV_HWPOISON, "MADV_HWPOISON", &sfile}, /* since Linux 2.6.32 */
58 {MADV_MERGEABLE, "MADV_MERGEABLE", &sfile}, /* since Linux 2.6.32 */
59 {MADV_UNMERGEABLE, "MADV_UNMERGEABLE", &sfile}, /* since Linux 2.6.32 */
60 {MADV_HUGEPAGE, "MADV_HUGEPAGE", &pfile}, /* since Linux 2.6.38 */
61 {MADV_NOHUGEPAGE, "MADV_NOHUGEPAGE", &pfile}, /* since Linux 2.6.38 */
62 {MADV_DONTDUMP, "MADV_DONTDUMP", &sfile}, /* since Linux 3.4 */
63 {MADV_DODUMP, "MADV_DODUMP", &sfile} /* since Linux 3.4 */
64 };
65
setup(void)66 static void setup(void)
67 {
68 unsigned int i;
69 int fd;
70
71 SAFE_MKDIR(TMP_DIR, 0664);
72 SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL);
73
74 fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
75
76 /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */
77 for (i = 0; i < 1280; i++)
78 SAFE_WRITE(1, fd, STR, strlen(STR));
79
80 SAFE_FSTAT(fd, &st);
81
82 /* Map the input file into shared memory */
83 sfile = SAFE_MMAP(NULL, st.st_size,
84 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
85
86 /* Map the input file into private memory. MADV_HUGEPAGE only works
87 * with private anonymous pages */
88 pfile = SAFE_MMAP(NULL, st.st_size,
89 PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
90
91 SAFE_CLOSE(fd);
92 }
93
cleanup(void)94 static void cleanup(void)
95 {
96 munmap(sfile, st.st_size);
97 munmap(pfile, st.st_size);
98 umount(TMP_DIR);
99 rmdir(TMP_DIR);
100 }
101
verify_madvise(unsigned int i)102 static void verify_madvise(unsigned int i)
103 {
104 struct tcase *tc = &tcases[i];
105
106 TEST(madvise(*(tc->addr), st.st_size, tc->advice));
107
108 if (TEST_RETURN == -1) {
109 if (TEST_ERRNO == EINVAL) {
110 tst_res(TCONF, "%s is not supported", tc->name);
111 } else {
112 tst_res(TFAIL, "madvise test for %s failed with "
113 "return = %ld, errno = %d : %s",
114 tc->name, TEST_RETURN, TEST_ERRNO,
115 tst_strerrno(TFAIL | TTERRNO));
116 }
117 } else {
118 tst_res(TPASS, "madvise test for %s PASSED", tc->name);
119 }
120 }
121
122 static struct tst_test test = {
123 .tid = "madvise01",
124 .tcnt = ARRAY_SIZE(tcases),
125 .test = verify_madvise,
126 .needs_tmpdir = 1,
127 .needs_root = 1,
128 .setup = setup,
129 .cleanup = cleanup,
130 };
131