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