1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2017 Red Hat, Inc.
4 */
5
6 /*\
7 * [Description]
8 *
9 * Detect heavy swapping during first time swap use.
10 *
11 * This case is used for testing kernel commit:
12 * 50a15981a1fa ("[S390] reference bit testing for unmapped pages")
13 *
14 * The upstream commit fixed a issue on s390/x platform that heavy
15 * swapping might occur in some condition, however since the patch
16 * was quite general, this testcase will be run on all supported
17 * platforms to ensure no regression been introduced.
18 *
19 * Details of the kernel fix:
20 *
21 * On x86 a page without a mapper is by definition not referenced / old.
22 * The s390 architecture keeps the reference bit in the storage key and
23 * the current code will check the storage key for page without a mapper.
24 * This leads to an interesting effect: the first time an s390 system
25 * needs to write pages to swap it only finds referenced pages. This
26 * causes a lot of pages to get added and written to the swap device.
27 * To avoid this behaviour change page_referenced to query the storage
28 * key only if there is a mapper of the page.
29 *
30 * [Algorithm]
31 *
32 * Try to allocate memory which size is slightly larger than current
33 * available memory. After allocation done, continue loop for a while
34 * and calculate the used swap size. The used swap size should be small
35 * enough, else it indicates that heavy swapping is occurred unexpectedly.
36 */
37
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include "tst_safe_stdio.h"
44 #include "lapi/abisize.h"
45 #include "mem.h"
46
47 /* allow swapping 1 * phy_mem in maximum */
48 #define COE_DELTA 1
49 /* will try to alloc 1.3 * phy_mem */
50 #define COE_SLIGHT_OVER 0.3
51
52 static void init_meminfo(void);
53 static void do_alloc(int allow_raise);
54 static void check_swapping(void);
55
56 static long mem_available_init;
57 static long swap_free_init;
58 static long mem_over;
59 static long mem_over_max;
60 static pid_t pid;
61 static unsigned int start_runtime;
62
test_swapping(void)63 static void test_swapping(void)
64 {
65 #ifdef TST_ABI32
66 tst_brk(TCONF, "test is not designed for 32-bit system.");
67 #endif
68 FILE *file;
69 char line[PATH_MAX];
70
71 start_runtime = tst_remaining_runtime();
72
73 file = SAFE_FOPEN("/proc/swaps", "r");
74 while (fgets(line, sizeof(line), file)) {
75 if (strstr(line, "/dev/zram")) {
76 SAFE_FCLOSE(file);
77 tst_brk(TCONF, "zram-swap is being used!");
78 }
79 }
80 SAFE_FCLOSE(file);
81
82 init_meminfo();
83
84 switch (pid = SAFE_FORK()) {
85 case 0:
86 do_alloc(0);
87 do_alloc(1);
88 exit(0);
89 default:
90 check_swapping();
91 }
92 }
93
init_meminfo(void)94 static void init_meminfo(void)
95 {
96 swap_free_init = SAFE_READ_MEMINFO("SwapFree:");
97 mem_available_init = tst_available_mem();
98 mem_over = mem_available_init * COE_SLIGHT_OVER;
99 mem_over_max = mem_available_init * COE_DELTA;
100
101 if (swap_free_init < mem_over_max)
102 tst_brk(TCONF, "Not enough swap space to test: swap_free_init(%ldkB) < mem_over_max(%ldkB)",
103 swap_free_init, mem_over_max);
104 }
105
do_alloc(int allow_raise)106 static void do_alloc(int allow_raise)
107 {
108 long mem_count;
109 void *s;
110
111 if (allow_raise == 1)
112 tst_res(TINFO, "available physical memory: %ld MB",
113 mem_available_init / 1024);
114 mem_count = mem_available_init + mem_over;
115 if (allow_raise == 1)
116 tst_res(TINFO, "try to allocate: %ld MB", mem_count / 1024);
117 s = SAFE_MALLOC(mem_count * 1024);
118 memset(s, 1, mem_count * 1024);
119 if ((allow_raise == 1) && (raise(SIGSTOP) == -1)) {
120 tst_res(TINFO, "memory allocated: %ld MB", mem_count / 1024);
121 tst_brk(TBROK | TERRNO, "kill");
122 }
123 free(s);
124 }
125
check_swapping(void)126 static void check_swapping(void)
127 {
128 int status;
129 long swap_free_now, swapped;
130
131 /* wait child stop */
132 SAFE_WAITPID(pid, &status, WUNTRACED);
133 if (!WIFSTOPPED(status))
134 tst_brk(TBROK, "child was not stopped.");
135
136 /* Still occupying memory, loop for a while */
137 while (tst_remaining_runtime() > start_runtime/2) {
138 swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
139 sleep(1);
140 long diff = labs(swap_free_now - SAFE_READ_MEMINFO("SwapFree:"));
141 if (diff < 10)
142 break;
143
144 tst_res(TINFO, "SwapFree difference %li", diff);
145 }
146
147 swapped = SAFE_READ_PROC_STATUS(pid, "VmSwap:");
148 if (swapped > mem_over_max) {
149 kill(pid, SIGCONT);
150 tst_brk(TFAIL, "heavy swapping detected: "
151 "%ld MB swapped.", swapped / 1024);
152 }
153
154 tst_res(TPASS, "no heavy swapping detected, %ld MB swapped.",
155 swapped / 1024);
156 kill(pid, SIGCONT);
157 /* wait child exit */
158 SAFE_WAITPID(pid, &status, 0);
159 }
160
161 static struct tst_test test = {
162 .needs_root = 1,
163 .forks_child = 1,
164 .min_mem_avail = 10,
165 .max_runtime = 600,
166 .test_all = test_swapping,
167 .needs_kconfigs = (const char *[]) {
168 "CONFIG_SWAP=y",
169 NULL
170 },
171 .tags = (const struct tst_tag[]) {
172 {"linux-git", "50a15981a1fa"},
173 {}
174 }
175 };
176