xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/madvise/madvise09.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker  * Check that memory marked with MADV_FREE is freed on memory pressure.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * o Fork a child and move it into a memory cgroup
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * o Allocate pages and fill them with a pattern
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * o Madvise pages with MADV_FREE
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * o Check that madvised pages were not freed immediately
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * o Write to some of the madvised pages again, these must not be freed
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * o Set memory limits
20*49cdfc7eSAndroid Build Coastguard Worker  *   - limit_in_bytes = 8MB
21*49cdfc7eSAndroid Build Coastguard Worker  *   - memsw.limit_in_bytes = 16MB
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  *   The reason for doubling the limit_in_bytes is to have safe margin
24*49cdfc7eSAndroid Build Coastguard Worker  *   for forking the memory hungy child etc. And the reason to setting
25*49cdfc7eSAndroid Build Coastguard Worker  *   memsw.limit_in_bytes to twice of that is to give the system chance
26*49cdfc7eSAndroid Build Coastguard Worker  *   to try to free some memory before cgroup OOM kicks in and kills
27*49cdfc7eSAndroid Build Coastguard Worker  *   the memory hungry child.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * o Run a memory hungry child that allocates memory in loop until it's
30*49cdfc7eSAndroid Build Coastguard Worker  *   killed by cgroup OOM
31*49cdfc7eSAndroid Build Coastguard Worker  *
32*49cdfc7eSAndroid Build Coastguard Worker  * o Once the child is killed the MADV_FREE pages that were not written to
33*49cdfc7eSAndroid Build Coastguard Worker  *   should be freed, the test passes if there is at least one
34*49cdfc7eSAndroid Build Coastguard Worker  */
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <ctype.h>
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
46*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mmap.h"
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #define MEMCG_PATH "/sys/fs/cgroup/memory/"
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker static char cgroup_path[PATH_MAX];
51*49cdfc7eSAndroid Build Coastguard Worker static char tasks_path[PATH_MAX];
52*49cdfc7eSAndroid Build Coastguard Worker static char limit_in_bytes_path[PATH_MAX];
53*49cdfc7eSAndroid Build Coastguard Worker static char memsw_limit_in_bytes_path[PATH_MAX];
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker static size_t page_size;
56*49cdfc7eSAndroid Build Coastguard Worker static int sleep_between_faults;
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker static int swap_accounting_enabled;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker #define PAGES 128
61*49cdfc7eSAndroid Build Coastguard Worker #define TOUCHED_PAGE1 0
62*49cdfc7eSAndroid Build Coastguard Worker #define TOUCHED_PAGE2 10
63*49cdfc7eSAndroid Build Coastguard Worker 
memory_pressure_child(void)64*49cdfc7eSAndroid Build Coastguard Worker static void memory_pressure_child(void)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker 	size_t i, page_size = getpagesize();
67*49cdfc7eSAndroid Build Coastguard Worker 	char *ptr;
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	for (;;) {
70*49cdfc7eSAndroid Build Coastguard Worker 		ptr = mmap(NULL, 500 * page_size, PROT_READ | PROT_WRITE,
71*49cdfc7eSAndroid Build Coastguard Worker 			   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < 500; i++) {
74*49cdfc7eSAndroid Build Coastguard Worker 			ptr[i * page_size] = i % 100;
75*49cdfc7eSAndroid Build Coastguard Worker 			usleep(sleep_between_faults);
76*49cdfc7eSAndroid Build Coastguard Worker 		}
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 		/* If swap accounting is disabled exit after process swapped out 100MB */
79*49cdfc7eSAndroid Build Coastguard Worker 		if (!swap_accounting_enabled) {
80*49cdfc7eSAndroid Build Coastguard Worker 			int swapped;
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_FILE_LINES_SCANF("/proc/self/status", "VmSwap: %d", &swapped);
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 			if (swapped > 100 * 1024)
85*49cdfc7eSAndroid Build Coastguard Worker 				exit(0);
86*49cdfc7eSAndroid Build Coastguard Worker 		}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	}
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	abort();
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker 
setup_cgroup_paths(int pid)93*49cdfc7eSAndroid Build Coastguard Worker static void setup_cgroup_paths(int pid)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(cgroup_path, sizeof(cgroup_path),
96*49cdfc7eSAndroid Build Coastguard Worker 		 MEMCG_PATH "ltp_madvise09_%i/", pid);
97*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(tasks_path, sizeof(tasks_path), "%s/tasks", cgroup_path);
98*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(limit_in_bytes_path, sizeof(limit_in_bytes_path),
99*49cdfc7eSAndroid Build Coastguard Worker 		 "%s/memory.limit_in_bytes", cgroup_path);
100*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(memsw_limit_in_bytes_path, sizeof(memsw_limit_in_bytes_path),
101*49cdfc7eSAndroid Build Coastguard Worker 		 "%s/memory.memsw.limit_in_bytes", cgroup_path);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
count_freed(char * ptr)104*49cdfc7eSAndroid Build Coastguard Worker static int count_freed(char *ptr)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker 	int i, ret = 0;
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < PAGES; i++) {
109*49cdfc7eSAndroid Build Coastguard Worker 		if (!ptr[i * page_size])
110*49cdfc7eSAndroid Build Coastguard Worker 			ret++;
111*49cdfc7eSAndroid Build Coastguard Worker 	}
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker 
check_page_baaa(char * ptr)116*49cdfc7eSAndroid Build Coastguard Worker static int check_page_baaa(char *ptr)
117*49cdfc7eSAndroid Build Coastguard Worker {
118*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	if (ptr[0] != 'b') {
121*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "%p unexpected %c (%i) at 0 expected 'b'",
122*49cdfc7eSAndroid Build Coastguard Worker 			ptr, isprint(ptr[0]) ? ptr[0] : ' ', ptr[0]);
123*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
124*49cdfc7eSAndroid Build Coastguard Worker 	}
125*49cdfc7eSAndroid Build Coastguard Worker 
126*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 1; i < page_size; i++) {
127*49cdfc7eSAndroid Build Coastguard Worker 		if (ptr[i] != 'a') {
128*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO,
129*49cdfc7eSAndroid Build Coastguard Worker 				"%p unexpected %c (%i) at %i expected 'a'",
130*49cdfc7eSAndroid Build Coastguard Worker 				ptr, isprint(ptr[i]) ? ptr[i] : ' ',
131*49cdfc7eSAndroid Build Coastguard Worker 				ptr[i], i);
132*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
133*49cdfc7eSAndroid Build Coastguard Worker 		}
134*49cdfc7eSAndroid Build Coastguard Worker 	}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker 
check_page(char * ptr,char val)139*49cdfc7eSAndroid Build Coastguard Worker static int check_page(char *ptr, char val)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < page_size; i++) {
144*49cdfc7eSAndroid Build Coastguard Worker 		if (ptr[i] != val) {
145*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TINFO,
146*49cdfc7eSAndroid Build Coastguard Worker 				"%p unexpected %c (%i) at %i expected %c (%i)",
147*49cdfc7eSAndroid Build Coastguard Worker 				ptr, isprint(ptr[i]) ? ptr[i] : ' ', ptr[i], i,
148*49cdfc7eSAndroid Build Coastguard Worker 				isprint(val) ? val : ' ', val);
149*49cdfc7eSAndroid Build Coastguard Worker 			return 1;
150*49cdfc7eSAndroid Build Coastguard Worker 		}
151*49cdfc7eSAndroid Build Coastguard Worker 	}
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
154*49cdfc7eSAndroid Build Coastguard Worker }
155*49cdfc7eSAndroid Build Coastguard Worker 
child(void)156*49cdfc7eSAndroid Build Coastguard Worker static void child(void)
157*49cdfc7eSAndroid Build Coastguard Worker {
158*49cdfc7eSAndroid Build Coastguard Worker 	size_t i;
159*49cdfc7eSAndroid Build Coastguard Worker 	char *ptr;
160*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int usage, old_limit, old_memsw_limit;
161*49cdfc7eSAndroid Build Coastguard Worker 	int status, pid, retries = 0;
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cgroup_path, 0777);
164*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(tasks_path, "%i", getpid());
165*49cdfc7eSAndroid Build Coastguard Worker 
166*49cdfc7eSAndroid Build Coastguard Worker 	ptr = SAFE_MMAP(NULL, PAGES * page_size, PROT_READ | PROT_WRITE,
167*49cdfc7eSAndroid Build Coastguard Worker 	                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < PAGES * page_size; i++)
170*49cdfc7eSAndroid Build Coastguard Worker 		ptr[i] = 'a';
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	if (madvise(ptr, PAGES * page_size, MADV_FREE)) {
173*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == EINVAL)
174*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF | TERRNO, "MADV_FREE is not supported");
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "MADV_FREE failed");
177*49cdfc7eSAndroid Build Coastguard Worker 	}
178*49cdfc7eSAndroid Build Coastguard Worker 
179*49cdfc7eSAndroid Build Coastguard Worker 	if (ptr[page_size] != 'a')
180*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "MADV_FREE pages were freed immediately");
181*49cdfc7eSAndroid Build Coastguard Worker 	else
182*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "MADV_FREE pages were not freed immediately");
183*49cdfc7eSAndroid Build Coastguard Worker 
184*49cdfc7eSAndroid Build Coastguard Worker 	ptr[TOUCHED_PAGE1 * page_size] = 'b';
185*49cdfc7eSAndroid Build Coastguard Worker 	ptr[TOUCHED_PAGE2 * page_size] = 'b';
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 	usage = 8 * 1024 * 1024;
188*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Setting memory limits to %u %u", usage, 2 * usage);
189*49cdfc7eSAndroid Build Coastguard Worker 
190*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_SCANF(limit_in_bytes_path, "%u", &old_limit);
191*49cdfc7eSAndroid Build Coastguard Worker 
192*49cdfc7eSAndroid Build Coastguard Worker 	if (swap_accounting_enabled)
193*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_SCANF(memsw_limit_in_bytes_path, "%u", &old_memsw_limit);
194*49cdfc7eSAndroid Build Coastguard Worker 
195*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(limit_in_bytes_path, "%u", usage);
196*49cdfc7eSAndroid Build Coastguard Worker 
197*49cdfc7eSAndroid Build Coastguard Worker 	if (swap_accounting_enabled)
198*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(memsw_limit_in_bytes_path, "%u", 2 * usage);
199*49cdfc7eSAndroid Build Coastguard Worker 
200*49cdfc7eSAndroid Build Coastguard Worker 	do {
201*49cdfc7eSAndroid Build Coastguard Worker 		sleep_between_faults++;
202*49cdfc7eSAndroid Build Coastguard Worker 
203*49cdfc7eSAndroid Build Coastguard Worker 		pid = SAFE_FORK();
204*49cdfc7eSAndroid Build Coastguard Worker 		if (!pid)
205*49cdfc7eSAndroid Build Coastguard Worker 			memory_pressure_child();
206*49cdfc7eSAndroid Build Coastguard Worker 
207*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Memory hungry child %i started, try %i", pid, retries);
208*49cdfc7eSAndroid Build Coastguard Worker 
209*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAIT(&status);
210*49cdfc7eSAndroid Build Coastguard Worker 	} while (retries++ < 10 && count_freed(ptr) == 0);
211*49cdfc7eSAndroid Build Coastguard Worker 
212*49cdfc7eSAndroid Build Coastguard Worker 	char map[PAGES+1];
213*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int freed = 0;
214*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int corrupted = 0;
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < PAGES; i++) {
217*49cdfc7eSAndroid Build Coastguard Worker 		char exp_val;
218*49cdfc7eSAndroid Build Coastguard Worker 
219*49cdfc7eSAndroid Build Coastguard Worker 		if (ptr[i * page_size]) {
220*49cdfc7eSAndroid Build Coastguard Worker 			exp_val = 'a';
221*49cdfc7eSAndroid Build Coastguard Worker 			map[i] = 'p';
222*49cdfc7eSAndroid Build Coastguard Worker 		} else {
223*49cdfc7eSAndroid Build Coastguard Worker 			exp_val = 0;
224*49cdfc7eSAndroid Build Coastguard Worker 			map[i] = '_';
225*49cdfc7eSAndroid Build Coastguard Worker 			freed++;
226*49cdfc7eSAndroid Build Coastguard Worker 		}
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker 		if (i != TOUCHED_PAGE1 && i != TOUCHED_PAGE2) {
229*49cdfc7eSAndroid Build Coastguard Worker 			if (check_page(ptr + i * page_size, exp_val)) {
230*49cdfc7eSAndroid Build Coastguard Worker 				map[i] = '?';
231*49cdfc7eSAndroid Build Coastguard Worker 				corrupted++;
232*49cdfc7eSAndroid Build Coastguard Worker 			}
233*49cdfc7eSAndroid Build Coastguard Worker 		} else {
234*49cdfc7eSAndroid Build Coastguard Worker 			if (check_page_baaa(ptr + i * page_size)) {
235*49cdfc7eSAndroid Build Coastguard Worker 				map[i] = '?';
236*49cdfc7eSAndroid Build Coastguard Worker 				corrupted++;
237*49cdfc7eSAndroid Build Coastguard Worker 			}
238*49cdfc7eSAndroid Build Coastguard Worker 		}
239*49cdfc7eSAndroid Build Coastguard Worker 	}
240*49cdfc7eSAndroid Build Coastguard Worker 	map[PAGES] = '\0';
241*49cdfc7eSAndroid Build Coastguard Worker 
242*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Memory map: %s", map);
243*49cdfc7eSAndroid Build Coastguard Worker 
244*49cdfc7eSAndroid Build Coastguard Worker 	if (freed)
245*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Pages MADV_FREE were freed on low memory");
246*49cdfc7eSAndroid Build Coastguard Worker 	else
247*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "No MADV_FREE page was freed on low memory");
248*49cdfc7eSAndroid Build Coastguard Worker 
249*49cdfc7eSAndroid Build Coastguard Worker 	if (corrupted)
250*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Found corrupted page");
251*49cdfc7eSAndroid Build Coastguard Worker 	else
252*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "All pages have expected content");
253*49cdfc7eSAndroid Build Coastguard Worker 
254*49cdfc7eSAndroid Build Coastguard Worker 	if (swap_accounting_enabled)
255*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_FILE_PRINTF(memsw_limit_in_bytes_path, "%u", old_memsw_limit);
256*49cdfc7eSAndroid Build Coastguard Worker 
257*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(limit_in_bytes_path, "%u", old_limit);
258*49cdfc7eSAndroid Build Coastguard Worker 
259*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(ptr, PAGES);
260*49cdfc7eSAndroid Build Coastguard Worker 
261*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
262*49cdfc7eSAndroid Build Coastguard Worker }
263*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)264*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
265*49cdfc7eSAndroid Build Coastguard Worker {
266*49cdfc7eSAndroid Build Coastguard Worker 	if (cgroup_path[0] && !access(cgroup_path, F_OK))
267*49cdfc7eSAndroid Build Coastguard Worker 		rmdir(cgroup_path);
268*49cdfc7eSAndroid Build Coastguard Worker }
269*49cdfc7eSAndroid Build Coastguard Worker 
run(void)270*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
271*49cdfc7eSAndroid Build Coastguard Worker {
272*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
273*49cdfc7eSAndroid Build Coastguard Worker 	int status;
274*49cdfc7eSAndroid Build Coastguard Worker 
275*49cdfc7eSAndroid Build Coastguard Worker retry:
276*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
277*49cdfc7eSAndroid Build Coastguard Worker 
278*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid) {
279*49cdfc7eSAndroid Build Coastguard Worker 		setup_cgroup_paths(getpid());
280*49cdfc7eSAndroid Build Coastguard Worker 		child();
281*49cdfc7eSAndroid Build Coastguard Worker 	}
282*49cdfc7eSAndroid Build Coastguard Worker 
283*49cdfc7eSAndroid Build Coastguard Worker 	setup_cgroup_paths(pid);
284*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAIT(&status);
285*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
286*49cdfc7eSAndroid Build Coastguard Worker 
287*49cdfc7eSAndroid Build Coastguard Worker 	/*
288*49cdfc7eSAndroid Build Coastguard Worker 	 * Rarely cgroup OOM kills both children not only the one that allocates
289*49cdfc7eSAndroid Build Coastguard Worker 	 * memory in loop, hence we retry here if that happens.
290*49cdfc7eSAndroid Build Coastguard Worker 	 */
291*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFSIGNALED(status)) {
292*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Both children killed, retrying...");
293*49cdfc7eSAndroid Build Coastguard Worker 		goto retry;
294*49cdfc7eSAndroid Build Coastguard Worker 	}
295*49cdfc7eSAndroid Build Coastguard Worker 
296*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status) && WEXITSTATUS(status) == TCONF)
297*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "MADV_FREE is not supported");
298*49cdfc7eSAndroid Build Coastguard Worker 
299*49cdfc7eSAndroid Build Coastguard Worker 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
300*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Child %s", tst_strstatus(status));
301*49cdfc7eSAndroid Build Coastguard Worker }
302*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)303*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
304*49cdfc7eSAndroid Build Coastguard Worker {
305*49cdfc7eSAndroid Build Coastguard Worker 	long int swap_total;
306*49cdfc7eSAndroid Build Coastguard Worker 
307*49cdfc7eSAndroid Build Coastguard Worker 	if (access(MEMCG_PATH, F_OK)) {
308*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "'" MEMCG_PATH
309*49cdfc7eSAndroid Build Coastguard Worker 			"' not present, CONFIG_MEMCG missing?");
310*49cdfc7eSAndroid Build Coastguard Worker 	}
311*49cdfc7eSAndroid Build Coastguard Worker 
312*49cdfc7eSAndroid Build Coastguard Worker 	if (!access(MEMCG_PATH "memory.memsw.limit_in_bytes", F_OK))
313*49cdfc7eSAndroid Build Coastguard Worker 		swap_accounting_enabled = 1;
314*49cdfc7eSAndroid Build Coastguard Worker 	else
315*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Swap accounting is disabled");
316*49cdfc7eSAndroid Build Coastguard Worker 
317*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_LINES_SCANF("/proc/meminfo", "SwapTotal: %ld", &swap_total);
318*49cdfc7eSAndroid Build Coastguard Worker 	if (swap_total <= 0)
319*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "MADV_FREE does not work without swap");
320*49cdfc7eSAndroid Build Coastguard Worker 
321*49cdfc7eSAndroid Build Coastguard Worker 	page_size = getpagesize();
322*49cdfc7eSAndroid Build Coastguard Worker }
323*49cdfc7eSAndroid Build Coastguard Worker 
324*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
325*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
326*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
327*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
328*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
329*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
330*49cdfc7eSAndroid Build Coastguard Worker };
331