1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Red Hat, Inc.
4 */
5
6 /*\
7 * [Description]
8 *
9 * Page fault occurs in spite that madvise(WILLNEED) system call is called
10 * to prefetch the page. This issue is reproduced by running a program
11 * which sequentially accesses to a shared memory and calls madvise(WILLNEED)
12 * to the next page on a page fault.
13 *
14 * This bug is present in all RHEL7 versions. It looks like this was fixed in
15 * mainline kernel > v3.15 by the following patch:
16 *
17 * commit 55231e5c898c5c03c14194001e349f40f59bd300
18 * Author: Johannes Weiner <hannes@cmpxchg.org>
19 * Date: Thu May 22 11:54:17 2014 -0700
20 *
21 * mm: madvise: fix MADV_WILLNEED on shmem swapouts
22 *
23 * Two checks are performed, the first looks at how SwapCache
24 * changes during madvise. When the pages are dirtied, about half
25 * will be accounted for under Cached and the other half will be
26 * moved into Swap. When madvise is run it will cause the pages
27 * under Cached to also be moved to Swap while rotating the pages
28 * already in Swap into SwapCached. So we expect that SwapCached has
29 * roughly MEM_LIMIT bytes added to it, but for reliability the
30 * PASS_THRESHOLD is much lower than that.
31 *
32 * Secondly we run madvise again, but only on the first
33 * PASS_THRESHOLD bytes to ensure these are entirely in RAM. Then we
34 * dirty these pages and check there were (almost) no page
35 * faults. Two faults are allowed incase some tasklet or something
36 * else unexpected, but irrelevant procedure, registers a fault to
37 * our process.
38 */
39
40 #include <errno.h>
41 #include <stdio.h>
42 #include <sys/mount.h>
43 #include <sys/sysinfo.h>
44 #include "tst_test.h"
45 #include "tst_cgroup.h"
46
47 #define CHUNK_SZ (400*1024*1024L)
48 #define MEM_LIMIT (CHUNK_SZ / 2)
49 #define MEMSW_LIMIT (2 * CHUNK_SZ)
50 #define PASS_THRESHOLD (CHUNK_SZ / 4)
51 #define PASS_THRESHOLD_KB (PASS_THRESHOLD / 1024)
52
53 static const struct tst_cgroup_group *cg;
54
55 static const char drop_caches_fname[] = "/proc/sys/vm/drop_caches";
56 static int pg_sz, stat_refresh_sup;
57
58 static long init_swap, init_swap_cached, init_cached;
59
check_path(const char * path)60 static void check_path(const char *path)
61 {
62 if (access(path, R_OK | W_OK))
63 tst_brk(TCONF, "file needed: %s", path);
64 }
65
print_cgmem(const char * name)66 static void print_cgmem(const char *name)
67 {
68 long ret;
69
70 if (!SAFE_CGROUP_HAS(cg, name))
71 return;
72
73 SAFE_CGROUP_SCANF(cg, name, "%ld", &ret);
74 tst_res(TINFO, "\t%s: %ld Kb", name, ret / 1024);
75 }
76
meminfo_diag(const char * point)77 static void meminfo_diag(const char *point)
78 {
79 if (stat_refresh_sup)
80 SAFE_FILE_PRINTF("/proc/sys/vm/stat_refresh", "1");
81
82 tst_res(TINFO, "%s", point);
83 tst_res(TINFO, "\tSwap: %ld Kb",
84 SAFE_READ_MEMINFO("SwapTotal:") - SAFE_READ_MEMINFO("SwapFree:") - init_swap);
85 tst_res(TINFO, "\tSwapCached: %ld Kb",
86 SAFE_READ_MEMINFO("SwapCached:") - init_swap_cached);
87 tst_res(TINFO, "\tCached: %ld Kb",
88 SAFE_READ_MEMINFO("Cached:") - init_cached);
89
90 print_cgmem("memory.current");
91 print_cgmem("memory.swap.current");
92 print_cgmem("memory.kmem.usage_in_bytes");
93 }
94
setup(void)95 static void setup(void)
96 {
97 struct sysinfo sys_buf_start;
98
99 pg_sz = getpagesize();
100
101 tst_res(TINFO, "dropping caches");
102 sync();
103 SAFE_FILE_PRINTF(drop_caches_fname, "3");
104
105 sysinfo(&sys_buf_start);
106 if (sys_buf_start.freeram < 2 * CHUNK_SZ) {
107 tst_brk(TCONF, "System RAM is too small (%li bytes needed)",
108 2 * CHUNK_SZ);
109 }
110 if (sys_buf_start.freeswap < 2 * CHUNK_SZ) {
111 tst_brk(TCONF, "System swap is too small (%li bytes needed)",
112 2 * CHUNK_SZ);
113 }
114
115 check_path("/proc/self/oom_score_adj");
116 SAFE_FILE_PRINTF("/proc/self/oom_score_adj", "%d", -1000);
117
118 tst_cgroup_require("memory", NULL);
119 cg = tst_cgroup_get_test_group();
120
121 SAFE_CGROUP_PRINTF(cg, "memory.max", "%ld", MEM_LIMIT);
122 if (SAFE_CGROUP_HAS(cg, "memory.swap.max"))
123 SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%ld", MEMSW_LIMIT);
124
125 if (SAFE_CGROUP_HAS(cg, "memory.swappiness")) {
126 SAFE_CGROUP_PRINT(cg, "memory.swappiness", "60");
127 } else {
128 check_path("/proc/sys/vm/swappiness");
129 SAFE_FILE_PRINTF("/proc/sys/vm/swappiness", "%d", 60);
130 }
131
132 SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
133
134 meminfo_diag("Initial meminfo, later values are relative to this (except memcg)");
135 init_swap = SAFE_READ_MEMINFO("SwapTotal:") - SAFE_READ_MEMINFO("SwapFree:");
136 init_swap_cached = SAFE_READ_MEMINFO("SwapCached:");
137 init_cached = SAFE_READ_MEMINFO("Cached:");
138
139 if (!access("/proc/sys/vm/stat_refresh", W_OK))
140 stat_refresh_sup = 1;
141
142 tst_res(TINFO, "mapping %ld Kb (%ld pages), limit %ld Kb, pass threshold %ld Kb",
143 CHUNK_SZ / 1024, CHUNK_SZ / pg_sz, MEM_LIMIT / 1024, PASS_THRESHOLD_KB);
144 }
145
cleanup(void)146 static void cleanup(void)
147 {
148 tst_cgroup_cleanup();
149 }
150
dirty_pages(char * ptr,long size)151 static void dirty_pages(char *ptr, long size)
152 {
153 long i;
154 long pages = size / pg_sz;
155
156 for (i = 0; i < pages; i++)
157 ptr[i * pg_sz] = 'x';
158 }
159
get_page_fault_num(void)160 static int get_page_fault_num(void)
161 {
162 int pg;
163
164 SAFE_FILE_SCANF("/proc/self/stat",
165 "%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %d",
166 &pg);
167 return pg;
168 }
169
test_advice_willneed(void)170 static void test_advice_willneed(void)
171 {
172 int loops = 50, res;
173 char *target;
174 long swapcached_start, swapcached;
175 int page_fault_num_1, page_fault_num_2;
176
177 meminfo_diag("Before mmap");
178 tst_res(TINFO, "PageFault(before mmap): %d", get_page_fault_num());
179 target = SAFE_MMAP(NULL, CHUNK_SZ, PROT_READ | PROT_WRITE,
180 MAP_SHARED | MAP_ANONYMOUS,
181 -1, 0);
182 meminfo_diag("Before dirty");
183 tst_res(TINFO, "PageFault(before dirty): %d", get_page_fault_num());
184 dirty_pages(target, CHUNK_SZ);
185 tst_res(TINFO, "PageFault(after dirty): %d", get_page_fault_num());
186
187 meminfo_diag("Before madvise");
188 SAFE_FILE_LINES_SCANF("/proc/meminfo", "SwapCached: %ld",
189 &swapcached_start);
190
191 TEST(madvise(target, MEM_LIMIT, MADV_WILLNEED));
192 if (TST_RET == -1)
193 tst_brk(TBROK | TTERRNO, "madvise failed");
194
195 do {
196 loops--;
197 usleep(100000);
198 if (stat_refresh_sup)
199 SAFE_FILE_PRINTF("/proc/sys/vm/stat_refresh", "1");
200 SAFE_FILE_LINES_SCANF("/proc/meminfo", "SwapCached: %ld",
201 &swapcached);
202 } while (swapcached < swapcached_start + PASS_THRESHOLD_KB && loops > 0);
203
204 meminfo_diag("After madvise");
205 res = swapcached > swapcached_start + PASS_THRESHOLD_KB;
206 tst_res(res ? TPASS : TFAIL,
207 "%s than %ld Kb were moved to the swap cache",
208 res ? "more" : "less", PASS_THRESHOLD_KB);
209
210
211 TEST(madvise(target, PASS_THRESHOLD, MADV_WILLNEED));
212 if (TST_RET == -1)
213 tst_brk(TBROK | TTERRNO, "madvise failed");
214
215 page_fault_num_1 = get_page_fault_num();
216 tst_res(TINFO, "PageFault(madvice / no mem access): %d",
217 page_fault_num_1);
218 dirty_pages(target, PASS_THRESHOLD);
219 page_fault_num_2 = get_page_fault_num();
220 tst_res(TINFO, "PageFault(madvice / mem access): %d",
221 page_fault_num_2);
222 meminfo_diag("After page access");
223
224 res = page_fault_num_2 - page_fault_num_1;
225 tst_res(res < 3 ? TPASS : TFAIL,
226 "%d pages were faulted out of 2 max", res);
227
228 SAFE_MUNMAP(target, CHUNK_SZ);
229 }
230
231 static struct tst_test test = {
232 .test_all = test_advice_willneed,
233 .setup = setup,
234 .cleanup = cleanup,
235 .min_kver = "3.10.0",
236 .needs_tmpdir = 1,
237 .needs_root = 1,
238 .save_restore = (const char * const[]) {
239 "?/proc/sys/vm/swappiness",
240 NULL
241 },
242 .tags = (const struct tst_tag[]) {
243 {"linux-git", "55231e5c898c"},
244 {"linux-git", "8de15e920dc8"},
245 {}
246 }
247 };
248