• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mem.h"
45 
46 /* allow swapping 1 * phy_mem in maximum */
47 #define COE_DELTA       1
48 /* will try to alloc 1.3 * phy_mem */
49 #define COE_SLIGHT_OVER 0.3
50 
51 static void init_meminfo(void);
52 static void do_alloc(int allow_raise);
53 static void check_swapping(void);
54 
55 static long mem_available_init;
56 static long swap_free_init;
57 static long mem_over;
58 static long mem_over_max;
59 static pid_t pid;
60 static unsigned int start_runtime;
61 
test_swapping(void)62 static void test_swapping(void)
63 {
64 	FILE *file;
65 	char line[PATH_MAX];
66 
67 	start_runtime = tst_remaining_runtime();
68 
69 	file = SAFE_FOPEN("/proc/swaps", "r");
70 	while (fgets(line, sizeof(line), file)) {
71 		if (strstr(line, "/dev/zram")) {
72 			SAFE_FCLOSE(file);
73 			tst_brk(TCONF, "zram-swap is being used!");
74 		}
75 	}
76 	SAFE_FCLOSE(file);
77 
78 	init_meminfo();
79 
80 	switch (pid = SAFE_FORK()) {
81 	case 0:
82 		TST_PRINT_MEMINFO();
83 		do_alloc(0);
84 		TST_PRINT_MEMINFO();
85 		do_alloc(1);
86 		exit(0);
87 	default:
88 		check_swapping();
89 	}
90 }
91 
init_meminfo(void)92 static void init_meminfo(void)
93 {
94 	swap_free_init = SAFE_READ_MEMINFO("SwapFree:");
95 	mem_available_init = tst_available_mem();
96 	mem_over = mem_available_init * COE_SLIGHT_OVER;
97 	mem_over_max = mem_available_init * COE_DELTA;
98 
99 	if (swap_free_init < mem_over_max)
100 		tst_brk(TCONF, "Not enough swap space to test: swap_free_init(%ldkB) < mem_over_max(%ldkB)",
101 				swap_free_init, mem_over_max);
102 }
103 
do_alloc(int allow_raise)104 static void do_alloc(int allow_raise)
105 {
106 	long mem_count;
107 	void *s;
108 
109 	if (allow_raise == 1)
110 		tst_res(TINFO, "available physical memory: %ld MB",
111 				mem_available_init / 1024);
112 
113 	mem_count = mem_available_init + mem_over;
114 
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 
120 	if ((allow_raise == 1) && (raise(SIGSTOP) == -1)) {
121 		tst_res(TINFO, "memory allocated: %ld MB", mem_count / 1024);
122 		tst_brk(TBROK | TERRNO, "kill");
123 	}
124 
125 	free(s);
126 }
127 
check_swapping(void)128 static void check_swapping(void)
129 {
130 	int status;
131 	long swap_free_now, swapped;
132 
133 	/* wait child stop */
134 	SAFE_WAITPID(pid, &status, WUNTRACED);
135 	if (!WIFSTOPPED(status))
136 		tst_brk(TBROK, "child was not stopped.");
137 
138 	/* Still occupying memory, loop for a while */
139 	while (tst_remaining_runtime() > start_runtime/2) {
140 		swap_free_now = SAFE_READ_MEMINFO("SwapFree:");
141 		sleep(1);
142 		long diff = labs(swap_free_now - SAFE_READ_MEMINFO("SwapFree:"));
143 
144 		if (diff < 10)
145 			break;
146 
147 		tst_res(TINFO, "SwapFree difference %li", diff);
148 	}
149 
150 	swapped = SAFE_READ_PROC_STATUS(pid, "VmSwap:");
151 	if (swapped > mem_over_max) {
152 		TST_PRINT_MEMINFO();
153 		kill(pid, SIGCONT);
154 		tst_brk(TFAIL, "heavy swapping detected: %ld MB swapped",
155 				swapped / 1024);
156 	}
157 
158 	tst_res(TPASS, "no heavy swapping detected, %ld MB swapped.",
159 		 swapped / 1024);
160 	kill(pid, SIGCONT);
161 	/* wait child exit */
162 	SAFE_WAITPID(pid, &status, 0);
163 }
164 
165 static struct tst_test test = {
166 	.needs_root = 1,
167 	.forks_child = 1,
168 	.min_mem_avail = 10,
169 	.max_runtime = 600,
170 	.test_all = test_swapping,
171 	.skip_in_compat = 1,
172 	.needs_kconfigs = (const char *[]) {
173 		"CONFIG_SWAP=y",
174 		NULL
175 	},
176 	.tags = (const struct tst_tag[]) {
177 		{"linux-git", "50a15981a1fa"},
178 		{}
179 	}
180 };
181