• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 *amem;
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",    &amem},  /* since Linux 2.6.38 */
61 	{MADV_NOHUGEPAGE,  "MADV_NOHUGEPAGE",  &amem},  /* 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 	{MADV_FREE,        "MADV_FREE",        &amem},  /* since Linux 4.5 */
65 	{MADV_WIPEONFORK,  "MADV_WIPEONFORK",  &amem},  /* since Linux 4.14 */
66 	{MADV_KEEPONFORK,  "MADV_KEEPONFORK",  &amem},  /* since Linux 4.14 */
67 
68 };
69 
setup(void)70 static void setup(void)
71 {
72 	unsigned int i;
73 	int fd;
74 
75 	SAFE_MKDIR(TMP_DIR, 0664);
76 	SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL);
77 
78 	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);
79 
80 	/* Writing 40 KB of random data into this file [32 * 1280 = 40960] */
81 	for (i = 0; i < 1280; i++)
82 		SAFE_WRITE(1, fd, STR, strlen(STR));
83 
84 	SAFE_FSTAT(fd, &st);
85 
86 	/* Map the input file into shared memory */
87 	sfile = SAFE_MMAP(NULL, st.st_size,
88 			PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
89 
90 	/* Map the input file into private memory. MADV_HUGEPAGE only works
91 	 * with private anonymous pages */
92 	amem = SAFE_MMAP(NULL, st.st_size,
93 			PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
94 
95 	SAFE_CLOSE(fd);
96 }
97 
cleanup(void)98 static void cleanup(void)
99 {
100 	SAFE_MUNMAP(sfile, st.st_size);
101 	SAFE_MUNMAP(amem, st.st_size);
102 	SAFE_UMOUNT(TMP_DIR);
103 }
104 
verify_madvise(unsigned int i)105 static void verify_madvise(unsigned int i)
106 {
107 	struct tcase *tc = &tcases[i];
108 
109 	TEST(madvise(*(tc->addr), st.st_size, tc->advice));
110 
111 	if (TST_RET == -1) {
112 		if (TST_ERR == EINVAL) {
113 			tst_res(TCONF, "%s is not supported", tc->name);
114 		} else {
115 			tst_res(TFAIL, "madvise test for %s failed with "
116 					"return = %ld, errno = %d : %s",
117 					tc->name, TST_RET, TST_ERR,
118 					tst_strerrno(TFAIL | TTERRNO));
119 		}
120 	} else {
121 		tst_res(TPASS, "madvise test for %s PASSED", tc->name);
122 	}
123 }
124 
125 static struct tst_test test = {
126 	.tcnt = ARRAY_SIZE(tcases),
127 	.test = verify_madvise,
128 	.needs_tmpdir = 1,
129 	.needs_root = 1,
130 	.setup = setup,
131 	.cleanup = cleanup,
132 };
133