xref: /linux/tools/testing/selftests/cgroup/test_memcontrol.c (revision eb69e955de7bdea6aa932fc4cce413ca439a4a5d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #define _GNU_SOURCE
3 
4 #include <linux/limits.h>
5 #include <linux/oom.h>
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <sys/inotify.h>
14 #include <sys/socket.h>
15 #include <sys/wait.h>
16 #include <arpa/inet.h>
17 #include <netinet/in.h>
18 #include <netdb.h>
19 #include <errno.h>
20 #include <sys/mman.h>
21 #include <sys/sysinfo.h>
22 
23 #include "kselftest.h"
24 #include "cgroup_util.h"
25 
26 /* enough page updates for a memory.stat read to flush, see MAX_VMSTAT_ERROR in test_kmem.c */
27 #define MEMCG_FLUSH_BYTES	\
28 	(2 * sysconf(_SC_PAGESIZE) * MEMCG_CHARGE_BATCH * get_nprocs())
29 
30 static bool has_localevents;
31 static bool has_recursiveprot;
32 static int page_size;
33 
34 int get_temp_fd(void)
35 {
36 	return open(".", O_TMPFILE | O_RDWR | O_EXCL, 0600);
37 }
38 
39 int alloc_pagecache(int fd, size_t size)
40 {
41 	char buf[BUF_SIZE];
42 	struct stat st;
43 	int i;
44 
45 	if (fstat(fd, &st))
46 		goto cleanup;
47 
48 	size += st.st_size;
49 
50 	if (ftruncate(fd, size))
51 		goto cleanup;
52 
53 	for (i = 0; i < size; i += sizeof(buf))
54 		read(fd, buf, sizeof(buf));
55 
56 	return 0;
57 
58 cleanup:
59 	return -1;
60 }
61 
62 static char *alloc_and_populate_anon(size_t size)
63 {
64 	char *buf, *ptr;
65 
66 	buf = malloc(size);
67 	if (buf == NULL) {
68 		fprintf(stderr, "malloc() failed\n");
69 		return NULL;
70 	}
71 
72 	for (ptr = buf; ptr < buf + size; ptr += page_size)
73 		*ptr = 0;
74 
75 	return buf;
76 }
77 
78 int alloc_anon(const char *cgroup, void *arg)
79 {
80 	size_t size = (unsigned long)arg;
81 	char *buf;
82 
83 	buf = alloc_and_populate_anon(size);
84 	if (!buf)
85 		return -1;
86 
87 	free(buf);
88 	return 0;
89 }
90 
91 int is_swap_enabled(void)
92 {
93 	char buf[BUF_SIZE];
94 	const char delim[] = "\n";
95 	int cnt = 0;
96 	char *line;
97 
98 	if (read_text("/proc/swaps", buf, sizeof(buf)) <= 0)
99 		return -1;
100 
101 	for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
102 		cnt++;
103 
104 	return cnt > 1;
105 }
106 
107 int set_oom_adj_score(int pid, int score)
108 {
109 	char path[PATH_MAX];
110 	int fd, len;
111 
112 	sprintf(path, "/proc/%d/oom_score_adj", pid);
113 
114 	fd = open(path, O_WRONLY | O_APPEND);
115 	if (fd < 0)
116 		return fd;
117 
118 	len = dprintf(fd, "%d", score);
119 	if (len < 0) {
120 		close(fd);
121 		return len;
122 	}
123 
124 	close(fd);
125 	return 0;
126 }
127 
128 /*
129  * This test creates two nested cgroups with and without enabling
130  * the memory controller.
131  */
132 static int test_memcg_subtree_control(const char *root)
133 {
134 	char *parent, *child, *parent2 = NULL, *child2 = NULL;
135 	int ret = KSFT_FAIL;
136 	char buf[BUF_SIZE];
137 
138 	/* Create two nested cgroups with the memory controller enabled */
139 	parent = cg_name(root, "memcg_test_0");
140 	child = cg_name(root, "memcg_test_0/memcg_test_1");
141 	if (!parent || !child)
142 		goto cleanup_free;
143 
144 	if (cg_create(parent))
145 		goto cleanup_free;
146 
147 	if (cg_write(parent, "cgroup.subtree_control", "+memory"))
148 		goto cleanup_parent;
149 
150 	if (cg_create(child))
151 		goto cleanup_parent;
152 
153 	if (cg_read_strstr(child, "cgroup.controllers", "memory"))
154 		goto cleanup_child;
155 
156 	/* Create two nested cgroups without enabling memory controller */
157 	parent2 = cg_name(root, "memcg_test_1");
158 	child2 = cg_name(root, "memcg_test_1/memcg_test_1");
159 	if (!parent2 || !child2)
160 		goto cleanup_free2;
161 
162 	if (cg_create(parent2))
163 		goto cleanup_free2;
164 
165 	if (cg_create(child2))
166 		goto cleanup_parent2;
167 
168 	if (cg_read(child2, "cgroup.controllers", buf, sizeof(buf)))
169 		goto cleanup_all;
170 
171 	if (!cg_read_strstr(child2, "cgroup.controllers", "memory"))
172 		goto cleanup_all;
173 
174 	ret = KSFT_PASS;
175 
176 cleanup_all:
177 	cg_destroy(child2);
178 cleanup_parent2:
179 	cg_destroy(parent2);
180 cleanup_free2:
181 	free(parent2);
182 	free(child2);
183 cleanup_child:
184 	cg_destroy(child);
185 cleanup_parent:
186 	cg_destroy(parent);
187 cleanup_free:
188 	free(parent);
189 	free(child);
190 
191 	return ret;
192 }
193 
194 static int alloc_anon_50M_check(const char *cgroup, void *arg)
195 {
196 	size_t size = MB(50);
197 	char *buf;
198 	long anon, current;
199 	int ret = -1;
200 
201 	buf = alloc_and_populate_anon(size);
202 	if (!buf)
203 		return -1;
204 
205 	current = cg_read_long(cgroup, "memory.current");
206 	if (current < size)
207 		goto cleanup;
208 
209 	if (!values_close(size, current, 3))
210 		goto cleanup;
211 
212 	anon = cg_read_key_long(cgroup, "memory.stat", "anon ");
213 	if (anon < 0)
214 		goto cleanup;
215 
216 	if (!values_close(anon, current, 3))
217 		goto cleanup;
218 
219 	ret = 0;
220 cleanup:
221 	free(buf);
222 	return ret;
223 }
224 
225 static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
226 {
227 	size_t size = MB(50);
228 	int ret = -1;
229 	long current, file;
230 	int fd;
231 
232 	fd = get_temp_fd();
233 	if (fd < 0)
234 		return -1;
235 
236 	if (alloc_pagecache(fd, size))
237 		goto cleanup;
238 
239 	current = cg_read_long(cgroup, "memory.current");
240 	if (current < size)
241 		goto cleanup;
242 
243 	file = cg_read_key_long(cgroup, "memory.stat", "file ");
244 	if (file < 0)
245 		goto cleanup;
246 
247 	if (!values_close(file, current, 10))
248 		goto cleanup;
249 
250 	ret = 0;
251 
252 cleanup:
253 	close(fd);
254 	return ret;
255 }
256 
257 /*
258  * This test create a memory cgroup, allocates
259  * some anonymous memory and some pagecache
260  * and checks memory.current, memory.peak, and some memory.stat values.
261  */
262 static int test_memcg_current_peak(const char *root)
263 {
264 	int ret = KSFT_FAIL;
265 	long current, peak, peak_reset;
266 	char *memcg;
267 	bool fd2_closed = false, fd3_closed = false, fd4_closed = false;
268 	int peak_fd = -1, peak_fd2 = -1, peak_fd3 = -1, peak_fd4 = -1;
269 	struct stat ss;
270 
271 	memcg = cg_name(root, "memcg_test");
272 	if (!memcg)
273 		goto cleanup;
274 
275 	if (cg_create(memcg))
276 		goto cleanup;
277 
278 	current = cg_read_long(memcg, "memory.current");
279 	if (current != 0)
280 		goto cleanup;
281 
282 	peak = cg_read_long(memcg, "memory.peak");
283 	if (peak != 0)
284 		goto cleanup;
285 
286 	if (cg_run(memcg, alloc_anon_50M_check, NULL))
287 		goto cleanup;
288 
289 	peak = cg_read_long(memcg, "memory.peak");
290 	if (peak < MB(50))
291 		goto cleanup;
292 
293 	/*
294 	 * We'll open a few FDs for the same memory.peak file to exercise the free-path
295 	 * We need at least three to be closed in a different order than writes occurred to test
296 	 * the linked-list handling.
297 	 */
298 	peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
299 
300 	if (peak_fd == -1) {
301 		if (errno == ENOENT)
302 			ret = KSFT_SKIP;
303 		goto cleanup;
304 	}
305 
306 	/*
307 	 * Before we try to use memory.peak's fd, try to figure out whether
308 	 * this kernel supports writing to that file in the first place. (by
309 	 * checking the writable bit on the file's st_mode)
310 	 */
311 	if (fstat(peak_fd, &ss))
312 		goto cleanup;
313 
314 	if ((ss.st_mode & S_IWUSR) == 0) {
315 		ret = KSFT_SKIP;
316 		goto cleanup;
317 	}
318 
319 	peak_fd2 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
320 
321 	if (peak_fd2 == -1)
322 		goto cleanup;
323 
324 	peak_fd3 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
325 
326 	if (peak_fd3 == -1)
327 		goto cleanup;
328 
329 	/* any non-empty string resets, but make it clear */
330 	static const char reset_string[] = "reset\n";
331 
332 	peak_reset = write(peak_fd, reset_string, sizeof(reset_string));
333 	if (peak_reset != sizeof(reset_string))
334 		goto cleanup;
335 
336 	peak_reset = write(peak_fd2, reset_string, sizeof(reset_string));
337 	if (peak_reset != sizeof(reset_string))
338 		goto cleanup;
339 
340 	peak_reset = write(peak_fd3, reset_string, sizeof(reset_string));
341 	if (peak_reset != sizeof(reset_string))
342 		goto cleanup;
343 
344 	/* Make sure a completely independent read isn't affected by our  FD-local reset above*/
345 	peak = cg_read_long(memcg, "memory.peak");
346 	if (peak < MB(50))
347 		goto cleanup;
348 
349 	fd2_closed = true;
350 	if (close(peak_fd2))
351 		goto cleanup;
352 
353 	peak_fd4 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
354 
355 	if (peak_fd4 == -1)
356 		goto cleanup;
357 
358 	peak_reset = write(peak_fd4, reset_string, sizeof(reset_string));
359 	if (peak_reset != sizeof(reset_string))
360 		goto cleanup;
361 
362 	peak = cg_read_long_fd(peak_fd);
363 	if (peak > MB(30) || peak < 0)
364 		goto cleanup;
365 
366 	if (cg_run(memcg, alloc_pagecache_50M_check, NULL))
367 		goto cleanup;
368 
369 	peak = cg_read_long(memcg, "memory.peak");
370 	if (peak < MB(50))
371 		goto cleanup;
372 
373 	/* Make sure everything is back to normal */
374 	peak = cg_read_long_fd(peak_fd);
375 	if (peak < MB(50))
376 		goto cleanup;
377 
378 	peak = cg_read_long_fd(peak_fd4);
379 	if (peak < MB(50))
380 		goto cleanup;
381 
382 	fd3_closed = true;
383 	if (close(peak_fd3))
384 		goto cleanup;
385 
386 	fd4_closed = true;
387 	if (close(peak_fd4))
388 		goto cleanup;
389 
390 	ret = KSFT_PASS;
391 
392 cleanup:
393 	close(peak_fd);
394 	if (!fd2_closed)
395 		close(peak_fd2);
396 	if (!fd3_closed)
397 		close(peak_fd3);
398 	if (!fd4_closed)
399 		close(peak_fd4);
400 	cg_destroy(memcg);
401 	free(memcg);
402 
403 	return ret;
404 }
405 
406 static int alloc_pagecache_50M_noexit(const char *cgroup, void *arg)
407 {
408 	int fd = (long)arg;
409 	int ppid = getppid();
410 
411 	if (alloc_pagecache(fd, MB(50)))
412 		return -1;
413 
414 	while (getppid() == ppid)
415 		sleep(1);
416 
417 	return 0;
418 }
419 
420 static int alloc_anon_noexit(const char *cgroup, void *arg)
421 {
422 	int ppid = getppid();
423 	size_t size = (unsigned long)arg;
424 	char *buf;
425 
426 	buf = alloc_and_populate_anon(size);
427 	if (!buf)
428 		return -1;
429 
430 	while (getppid() == ppid)
431 		sleep(1);
432 
433 	free(buf);
434 	return 0;
435 }
436 
437 /*
438  * Wait until processes are killed asynchronously by the OOM killer
439  * If we exceed a timeout, fail.
440  */
441 static int cg_test_proc_killed(const char *cgroup)
442 {
443 	int limit;
444 
445 	for (limit = 10; limit > 0; limit--) {
446 		if (cg_read_strcmp(cgroup, "cgroup.procs", "") == 0)
447 			return 0;
448 
449 		usleep(100000);
450 	}
451 	return -1;
452 }
453 
454 static bool reclaim_until(const char *memcg, long goal);
455 
456 /*
457  * First, this test creates the following hierarchy:
458  * A       memory.min = 0,    memory.max = 200M
459  * A/B     memory.min = 50M
460  * A/B/C   memory.min = 75M,  memory.current = 50M
461  * A/B/D   memory.min = 25M,  memory.current = 50M
462  * A/B/E   memory.min = 0,    memory.current = 50M
463  * A/B/F   memory.min = 500M, memory.current = 0
464  *
465  * (or memory.low if we test soft protection)
466  *
467  * Usages are pagecache and the test keeps a running
468  * process in every leaf cgroup.
469  * Then it creates A/G and creates a significant
470  * memory pressure in A.
471  *
472  * Then it checks actual memory usages and expects that:
473  * A/B    memory.current ~= 50M
474  * A/B/C  memory.current ~= 29M [memory.events:low > 0]
475  * A/B/D  memory.current ~= 21M [memory.events:low > 0]
476  * A/B/E  memory.current ~= 0   [memory.events:low == 0 if !memory_recursiveprot,
477  *				 undefined otherwise]
478  * A/B/F  memory.current  = 0   [memory.events:low == 0]
479  * (for origin of the numbers, see model in memcg_protection.m.)
480  *
481  * After that it tries to allocate more than there is
482  * unprotected memory in A available, and checks that:
483  * a) memory.min protects pagecache even in this case,
484  * b) memory.low allows reclaiming page cache with low events.
485  *
486  * Then we try to reclaim from A/B/C using memory.reclaim until its
487  * usage reaches 10M.
488  * This makes sure that:
489  * (a) We ignore the protection of the reclaim target memcg.
490  * (b) The previously calculated emin value (~29M) should be dismissed.
491  */
492 static int test_memcg_protection(const char *root, bool min)
493 {
494 	int ret = KSFT_FAIL, rc;
495 	char *parent[3] = {NULL};
496 	char *children[4] = {NULL};
497 	const char *attribute = min ? "memory.min" : "memory.low";
498 	long c[4];
499 	long current;
500 	int i, attempts;
501 	int fd;
502 
503 	fd = get_temp_fd();
504 	if (fd < 0)
505 		goto cleanup;
506 
507 	parent[0] = cg_name(root, "memcg_test_0");
508 	if (!parent[0])
509 		goto cleanup;
510 
511 	parent[1] = cg_name(parent[0], "memcg_test_1");
512 	if (!parent[1])
513 		goto cleanup;
514 
515 	parent[2] = cg_name(parent[0], "memcg_test_2");
516 	if (!parent[2])
517 		goto cleanup;
518 
519 	if (cg_create(parent[0]))
520 		goto cleanup;
521 
522 	if (cg_read_long(parent[0], attribute)) {
523 		/* No memory.min on older kernels is fine */
524 		if (min)
525 			ret = KSFT_SKIP;
526 		goto cleanup;
527 	}
528 
529 	if (cg_write(parent[0], "cgroup.subtree_control", "+memory"))
530 		goto cleanup;
531 
532 	if (cg_write(parent[0], "memory.max", "200M"))
533 		goto cleanup;
534 
535 	if (cg_write(parent[0], "memory.swap.max", "0"))
536 		goto cleanup;
537 
538 	if (cg_create(parent[1]))
539 		goto cleanup;
540 
541 	if (cg_write(parent[1], "cgroup.subtree_control", "+memory"))
542 		goto cleanup;
543 
544 	if (cg_create(parent[2]))
545 		goto cleanup;
546 
547 	for (i = 0; i < ARRAY_SIZE(children); i++) {
548 		children[i] = cg_name_indexed(parent[1], "child_memcg", i);
549 		if (!children[i])
550 			goto cleanup;
551 
552 		if (cg_create(children[i]))
553 			goto cleanup;
554 
555 		if (i > 2)
556 			continue;
557 
558 		cg_run_nowait(children[i], alloc_pagecache_50M_noexit,
559 			      (void *)(long)fd);
560 	}
561 
562 	if (cg_write(parent[1],   attribute, "50M"))
563 		goto cleanup;
564 	if (cg_write(children[0], attribute, "75M"))
565 		goto cleanup;
566 	if (cg_write(children[1], attribute, "25M"))
567 		goto cleanup;
568 	if (cg_write(children[2], attribute, "0"))
569 		goto cleanup;
570 	if (cg_write(children[3], attribute, "500M"))
571 		goto cleanup;
572 
573 	attempts = 0;
574 	while (!values_close(cg_read_long(parent[1], "memory.current"),
575 			     MB(150), 3)) {
576 		if (attempts++ > 5)
577 			break;
578 		sleep(1);
579 	}
580 
581 	if (cg_run(parent[2], alloc_anon, (void *)MB(148)))
582 		goto cleanup;
583 
584 	if (!values_close(cg_read_long(parent[1], "memory.current"), MB(50), 3))
585 		goto cleanup;
586 
587 	for (i = 0; i < ARRAY_SIZE(children); i++)
588 		c[i] = cg_read_long(children[i], "memory.current");
589 
590 	if (!values_close(c[0], MB(29), 15))
591 		goto cleanup;
592 
593 	if (!values_close(c[1], MB(21), 20))
594 		goto cleanup;
595 
596 	if (c[3] != 0)
597 		goto cleanup;
598 
599 	rc = cg_run(parent[2], alloc_anon, (void *)MB(170));
600 	if (min && !rc)
601 		goto cleanup;
602 	else if (!min && rc) {
603 		fprintf(stderr,
604 			"memory.low prevents from allocating anon memory\n");
605 		goto cleanup;
606 	}
607 
608 	current = min ? MB(50) : MB(30);
609 	if (!values_close(cg_read_long(parent[1], "memory.current"), current, 3))
610 		goto cleanup;
611 
612 	if (!reclaim_until(children[0], MB(10)))
613 		goto cleanup;
614 
615 	if (min) {
616 		ret = KSFT_PASS;
617 		goto cleanup;
618 	}
619 
620 	/*
621 	 * Child 2 has memory.low=0, but some low protection may still be
622 	 * distributed down from its parent with memory.low=50M if cgroup2
623 	 * memory_recursiveprot mount option is enabled. Ignore the low
624 	 * event count in this case.
625 	 */
626 	for (i = 0; i < ARRAY_SIZE(children); i++) {
627 		int ignore_low_events_index = has_recursiveprot ? 2 : -1;
628 		int no_low_events_index = 1;
629 		long low, oom;
630 
631 		oom = cg_read_key_long(children[i], "memory.events", "oom ");
632 		low = cg_read_key_long(children[i], "memory.events", "low ");
633 
634 		if (oom)
635 			goto cleanup;
636 		if (i == ignore_low_events_index)
637 			continue;
638 		if (i <= no_low_events_index && low <= 0)
639 			goto cleanup;
640 		if (i > no_low_events_index && low)
641 			goto cleanup;
642 
643 	}
644 
645 	ret = KSFT_PASS;
646 
647 cleanup:
648 	for (i = ARRAY_SIZE(children) - 1; i >= 0; i--) {
649 		if (!children[i])
650 			continue;
651 
652 		cg_destroy(children[i]);
653 		free(children[i]);
654 	}
655 
656 	for (i = ARRAY_SIZE(parent) - 1; i >= 0; i--) {
657 		if (!parent[i])
658 			continue;
659 
660 		cg_destroy(parent[i]);
661 		free(parent[i]);
662 	}
663 	close(fd);
664 	return ret;
665 }
666 
667 static int test_memcg_min(const char *root)
668 {
669 	return test_memcg_protection(root, true);
670 }
671 
672 static int test_memcg_low(const char *root)
673 {
674 	return test_memcg_protection(root, false);
675 }
676 
677 static int alloc_pagecache_max_30M(const char *cgroup, void *arg)
678 {
679 	size_t size = MB(50);
680 	int ret = -1;
681 	long current, high, max;
682 	int fd;
683 
684 	high = cg_read_long(cgroup, "memory.high");
685 	max = cg_read_long(cgroup, "memory.max");
686 	if (high != MB(30) && max != MB(30))
687 		return -1;
688 
689 	fd = get_temp_fd();
690 	if (fd < 0)
691 		return -1;
692 
693 	if (alloc_pagecache(fd, size))
694 		goto cleanup;
695 
696 	current = cg_read_long(cgroup, "memory.current");
697 	if (!values_close(current, MB(30), 5))
698 		goto cleanup;
699 
700 	ret = 0;
701 
702 cleanup:
703 	close(fd);
704 	return ret;
705 
706 }
707 
708 /*
709  * This test checks that memory.high limits the amount of
710  * memory which can be consumed by either anonymous memory
711  * or pagecache.
712  */
713 static int test_memcg_high(const char *root)
714 {
715 	int ret = KSFT_FAIL;
716 	char *memcg;
717 	long high;
718 
719 	memcg = cg_name(root, "memcg_test");
720 	if (!memcg)
721 		goto cleanup;
722 
723 	if (cg_create(memcg))
724 		goto cleanup;
725 
726 	if (cg_read_strcmp(memcg, "memory.high", "max\n"))
727 		goto cleanup;
728 
729 	if (cg_write(memcg, "memory.swap.max", "0"))
730 		goto cleanup;
731 
732 	if (cg_write(memcg, "memory.high", "30M"))
733 		goto cleanup;
734 
735 	if (cg_run(memcg, alloc_anon, (void *)MB(31)))
736 		goto cleanup;
737 
738 	if (!cg_run(memcg, alloc_pagecache_50M_check, NULL))
739 		goto cleanup;
740 
741 	if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
742 		goto cleanup;
743 
744 	high = cg_read_key_long(memcg, "memory.events", "high ");
745 	if (high <= 0)
746 		goto cleanup;
747 
748 	ret = KSFT_PASS;
749 
750 cleanup:
751 	cg_destroy(memcg);
752 	free(memcg);
753 
754 	return ret;
755 }
756 
757 static int alloc_anon_mlock(const char *cgroup, void *arg)
758 {
759 	size_t size = (size_t)arg;
760 	void *buf;
761 
762 	buf = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
763 		   0, 0);
764 	if (buf == MAP_FAILED)
765 		return -1;
766 
767 	mlock(buf, size);
768 	munmap(buf, size);
769 	return 0;
770 }
771 
772 /*
773  * This test checks that memory.high is able to throttle big single shot
774  * allocation i.e. large allocation within one kernel entry.
775  */
776 static int test_memcg_high_sync(const char *root)
777 {
778 	int ret = KSFT_FAIL, pid, fd = -1;
779 	char *memcg;
780 	long pre_high, pre_max;
781 	long post_high, post_max;
782 
783 	memcg = cg_name(root, "memcg_test");
784 	if (!memcg)
785 		goto cleanup;
786 
787 	if (cg_create(memcg))
788 		goto cleanup;
789 
790 	pre_high = cg_read_key_long(memcg, "memory.events", "high ");
791 	pre_max = cg_read_key_long(memcg, "memory.events", "max ");
792 	if (pre_high < 0 || pre_max < 0)
793 		goto cleanup;
794 
795 	if (cg_write(memcg, "memory.swap.max", "0"))
796 		goto cleanup;
797 
798 	if (cg_write(memcg, "memory.high", "30M"))
799 		goto cleanup;
800 
801 	if (cg_write(memcg, "memory.max", "140M"))
802 		goto cleanup;
803 
804 	fd = memcg_prepare_for_wait(memcg);
805 	if (fd < 0)
806 		goto cleanup;
807 
808 	pid = cg_run_nowait(memcg, alloc_anon_mlock, (void *)MB(200));
809 	if (pid < 0)
810 		goto cleanup;
811 
812 	cg_wait_for(fd);
813 
814 	post_high = cg_read_key_long(memcg, "memory.events", "high ");
815 	post_max = cg_read_key_long(memcg, "memory.events", "max ");
816 	if (post_high < 0 || post_max < 0)
817 		goto cleanup;
818 
819 	if (pre_high == post_high || pre_max != post_max)
820 		goto cleanup;
821 
822 	ret = KSFT_PASS;
823 
824 cleanup:
825 	if (fd >= 0)
826 		close(fd);
827 	cg_destroy(memcg);
828 	free(memcg);
829 
830 	return ret;
831 }
832 
833 /*
834  * This test checks that memory.max limits the amount of
835  * memory which can be consumed by either anonymous memory
836  * or pagecache.
837  */
838 static int test_memcg_max(const char *root)
839 {
840 	int ret = KSFT_FAIL;
841 	char *memcg;
842 	long current, max;
843 
844 	memcg = cg_name(root, "memcg_test");
845 	if (!memcg)
846 		goto cleanup;
847 
848 	if (cg_create(memcg))
849 		goto cleanup;
850 
851 	if (cg_read_strcmp(memcg, "memory.max", "max\n"))
852 		goto cleanup;
853 
854 	if (cg_write(memcg, "memory.swap.max", "0"))
855 		goto cleanup;
856 
857 	if (cg_write(memcg, "memory.max", "30M"))
858 		goto cleanup;
859 
860 	/* Should be killed by OOM killer */
861 	if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
862 		goto cleanup;
863 
864 	if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
865 		goto cleanup;
866 
867 	current = cg_read_long(memcg, "memory.current");
868 	if (current > MB(30) || !current)
869 		goto cleanup;
870 
871 	max = cg_read_key_long(memcg, "memory.events", "max ");
872 	if (max <= 0)
873 		goto cleanup;
874 
875 	ret = KSFT_PASS;
876 
877 cleanup:
878 	cg_destroy(memcg);
879 	free(memcg);
880 
881 	return ret;
882 }
883 
884 /*
885  * Reclaim from @memcg until usage reaches @goal by writing to
886  * memory.reclaim.
887  *
888  * This function will return false if the usage is already below the
889  * goal.
890  *
891  * This function assumes that writing to memory.reclaim is the only
892  * source of change in memory.current (no concurrent allocations or
893  * reclaim).
894  *
895  * This function makes sure memory.reclaim is sane. It will return
896  * false if memory.reclaim's error codes do not make sense, even if
897  * the usage goal was satisfied.
898  */
899 static bool reclaim_until(const char *memcg, long goal)
900 {
901 	char buf[64];
902 	int retries, err;
903 	long current, to_reclaim;
904 	bool reclaimed = false;
905 
906 	for (retries = 5; retries > 0; retries--) {
907 		current = cg_read_long(memcg, "memory.current");
908 
909 		if (current < goal || values_close(current, goal, 3))
910 			break;
911 		/* Did memory.reclaim return 0 incorrectly? */
912 		else if (reclaimed)
913 			return false;
914 
915 		to_reclaim = current - goal;
916 		snprintf(buf, sizeof(buf), "%ld", to_reclaim);
917 		err = cg_write(memcg, "memory.reclaim", buf);
918 		if (!err)
919 			reclaimed = true;
920 		else if (err != -EAGAIN)
921 			return false;
922 	}
923 	return reclaimed;
924 }
925 
926 /*
927  * This test checks that memory.reclaim reclaims the given
928  * amount of memory (from both anon and file, if possible).
929  */
930 static int test_memcg_reclaim(const char *root)
931 {
932 	int ret = KSFT_FAIL;
933 	int fd = -1;
934 	int retries;
935 	char *memcg;
936 	long current, expected_usage;
937 
938 	memcg = cg_name(root, "memcg_test");
939 	if (!memcg)
940 		goto cleanup;
941 
942 	if (cg_create(memcg))
943 		goto cleanup;
944 
945 	current = cg_read_long(memcg, "memory.current");
946 	if (current != 0)
947 		goto cleanup;
948 
949 	fd = get_temp_fd();
950 	if (fd < 0)
951 		goto cleanup;
952 
953 	cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd);
954 
955 	/*
956 	 * If swap is enabled, try to reclaim from both anon and file, else try
957 	 * to reclaim from file only.
958 	 */
959 	if (is_swap_enabled()) {
960 		cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50));
961 		expected_usage = MB(100);
962 	} else
963 		expected_usage = MB(50);
964 
965 	/*
966 	 * Wait until current usage reaches the expected usage (or we run out of
967 	 * retries).
968 	 */
969 	retries = 5;
970 	while (!values_close(cg_read_long(memcg, "memory.current"),
971 			    expected_usage, 10)) {
972 		if (retries--) {
973 			sleep(1);
974 			continue;
975 		} else {
976 			fprintf(stderr,
977 				"failed to allocate %ld for memcg reclaim test\n",
978 				expected_usage);
979 			goto cleanup;
980 		}
981 	}
982 
983 	/*
984 	 * Reclaim until current reaches 30M, this makes sure we hit both anon
985 	 * and file if swap is enabled.
986 	 */
987 	if (!reclaim_until(memcg, MB(30)))
988 		goto cleanup;
989 
990 	ret = KSFT_PASS;
991 cleanup:
992 	cg_destroy(memcg);
993 	free(memcg);
994 	close(fd);
995 
996 	return ret;
997 }
998 
999 static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
1000 {
1001 	long mem_max = (long)arg;
1002 	size_t size = MB(50);
1003 	char *buf;
1004 	long mem_current, swap_current;
1005 	int ret = -1;
1006 
1007 	buf = alloc_and_populate_anon(size);
1008 	if (!buf)
1009 		return -1;
1010 
1011 	mem_current = cg_read_long(cgroup, "memory.current");
1012 	if (!mem_current || !values_close(mem_current, mem_max, 3))
1013 		goto cleanup;
1014 
1015 	swap_current = cg_read_long(cgroup, "memory.swap.current");
1016 	if (!swap_current ||
1017 	    !values_close(mem_current + swap_current, size, 3))
1018 		goto cleanup;
1019 
1020 	ret = 0;
1021 cleanup:
1022 	free(buf);
1023 	return ret;
1024 }
1025 
1026 /*
1027  * This test checks that memory.swap.max limits the amount of
1028  * anonymous memory which can be swapped out. Additionally, it verifies that
1029  * memory.swap.peak reflects the high watermark and can be reset.
1030  */
1031 static int test_memcg_swap_max_peak(const char *root)
1032 {
1033 	int ret = KSFT_FAIL;
1034 	char *memcg;
1035 	long max, peak;
1036 	struct stat ss;
1037 	int swap_peak_fd = -1, mem_peak_fd = -1;
1038 
1039 	/* any non-empty string resets */
1040 	static const char reset_string[] = "foobarbaz";
1041 
1042 	if (!is_swap_enabled())
1043 		return KSFT_SKIP;
1044 
1045 	memcg = cg_name(root, "memcg_test");
1046 	if (!memcg)
1047 		goto cleanup;
1048 
1049 	if (cg_create(memcg))
1050 		goto cleanup;
1051 
1052 	if (cg_read_long(memcg, "memory.swap.current")) {
1053 		ret = KSFT_SKIP;
1054 		goto cleanup;
1055 	}
1056 
1057 	swap_peak_fd = cg_open(memcg, "memory.swap.peak",
1058 			       O_RDWR | O_APPEND | O_CLOEXEC);
1059 
1060 	if (swap_peak_fd == -1) {
1061 		if (errno == ENOENT)
1062 			ret = KSFT_SKIP;
1063 		goto cleanup;
1064 	}
1065 
1066 	/*
1067 	 * Before we try to use memory.swap.peak's fd, try to figure out
1068 	 * whether this kernel supports writing to that file in the first
1069 	 * place. (by checking the writable bit on the file's st_mode)
1070 	 */
1071 	if (fstat(swap_peak_fd, &ss))
1072 		goto cleanup;
1073 
1074 	if ((ss.st_mode & S_IWUSR) == 0) {
1075 		ret = KSFT_SKIP;
1076 		goto cleanup;
1077 	}
1078 
1079 	mem_peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC);
1080 
1081 	if (mem_peak_fd == -1)
1082 		goto cleanup;
1083 
1084 	if (cg_read_long(memcg, "memory.swap.peak"))
1085 		goto cleanup;
1086 
1087 	if (cg_read_long_fd(swap_peak_fd))
1088 		goto cleanup;
1089 
1090 	/* switch the swap and mem fds into local-peak tracking mode*/
1091 	int peak_reset = write(swap_peak_fd, reset_string, sizeof(reset_string));
1092 
1093 	if (peak_reset != sizeof(reset_string))
1094 		goto cleanup;
1095 
1096 	if (cg_read_long_fd(swap_peak_fd))
1097 		goto cleanup;
1098 
1099 	if (cg_read_long(memcg, "memory.peak"))
1100 		goto cleanup;
1101 
1102 	if (cg_read_long_fd(mem_peak_fd))
1103 		goto cleanup;
1104 
1105 	peak_reset = write(mem_peak_fd, reset_string, sizeof(reset_string));
1106 	if (peak_reset != sizeof(reset_string))
1107 		goto cleanup;
1108 
1109 	if (cg_read_long_fd(mem_peak_fd))
1110 		goto cleanup;
1111 
1112 	if (cg_read_strcmp(memcg, "memory.max", "max\n"))
1113 		goto cleanup;
1114 
1115 	if (cg_read_strcmp(memcg, "memory.swap.max", "max\n"))
1116 		goto cleanup;
1117 
1118 	if (cg_write(memcg, "memory.swap.max", "30M"))
1119 		goto cleanup;
1120 
1121 	if (cg_write(memcg, "memory.max", "30M"))
1122 		goto cleanup;
1123 
1124 	/* Should be killed by OOM killer */
1125 	if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
1126 		goto cleanup;
1127 
1128 	if (cg_read_key_long(memcg, "memory.events", "oom ") != 1)
1129 		goto cleanup;
1130 
1131 	if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1)
1132 		goto cleanup;
1133 
1134 	peak = cg_read_long(memcg, "memory.peak");
1135 	if (peak < MB(29))
1136 		goto cleanup;
1137 
1138 	peak = cg_read_long(memcg, "memory.swap.peak");
1139 	if (peak < MB(29))
1140 		goto cleanup;
1141 
1142 	peak = cg_read_long_fd(mem_peak_fd);
1143 	if (peak < MB(29))
1144 		goto cleanup;
1145 
1146 	peak = cg_read_long_fd(swap_peak_fd);
1147 	if (peak < MB(29))
1148 		goto cleanup;
1149 
1150 	/*
1151 	 * open, reset and close the peak swap on another FD to make sure
1152 	 * multiple extant fds don't corrupt the linked-list
1153 	 */
1154 	peak_reset = cg_write(memcg, "memory.swap.peak", (char *)reset_string);
1155 	if (peak_reset)
1156 		goto cleanup;
1157 
1158 	peak_reset = cg_write(memcg, "memory.peak", (char *)reset_string);
1159 	if (peak_reset)
1160 		goto cleanup;
1161 
1162 	/* actually reset on the fds */
1163 	peak_reset = write(swap_peak_fd, reset_string, sizeof(reset_string));
1164 	if (peak_reset != sizeof(reset_string))
1165 		goto cleanup;
1166 
1167 	peak_reset = write(mem_peak_fd, reset_string, sizeof(reset_string));
1168 	if (peak_reset != sizeof(reset_string))
1169 		goto cleanup;
1170 
1171 	peak = cg_read_long_fd(swap_peak_fd);
1172 	if (peak > MB(10))
1173 		goto cleanup;
1174 
1175 	/*
1176 	 * The cgroup is now empty, but there may be a page or two associated
1177 	 * with the open FD accounted to it.
1178 	 */
1179 	peak = cg_read_long_fd(mem_peak_fd);
1180 	if (peak > MB(1))
1181 		goto cleanup;
1182 
1183 	if (cg_read_long(memcg, "memory.peak") < MB(29))
1184 		goto cleanup;
1185 
1186 	if (cg_read_long(memcg, "memory.swap.peak") < MB(29))
1187 		goto cleanup;
1188 
1189 	if (cg_run(memcg, alloc_anon_50M_check_swap, (void *)MB(30)))
1190 		goto cleanup;
1191 
1192 	max = cg_read_key_long(memcg, "memory.events", "max ");
1193 	if (max <= 0)
1194 		goto cleanup;
1195 
1196 	peak = cg_read_long(memcg, "memory.peak");
1197 	if (peak < MB(29))
1198 		goto cleanup;
1199 
1200 	peak = cg_read_long(memcg, "memory.swap.peak");
1201 	if (peak < MB(29))
1202 		goto cleanup;
1203 
1204 	peak = cg_read_long_fd(mem_peak_fd);
1205 	if (peak < MB(29))
1206 		goto cleanup;
1207 
1208 	peak = cg_read_long_fd(swap_peak_fd);
1209 	if (peak < MB(19))
1210 		goto cleanup;
1211 
1212 	ret = KSFT_PASS;
1213 
1214 cleanup:
1215 	if (mem_peak_fd != -1 && close(mem_peak_fd))
1216 		ret = KSFT_FAIL;
1217 	if (swap_peak_fd != -1 && close(swap_peak_fd))
1218 		ret = KSFT_FAIL;
1219 	cg_destroy(memcg);
1220 	free(memcg);
1221 
1222 	return ret;
1223 }
1224 
1225 /*
1226  * This test disables swapping and tries to allocate anonymous memory
1227  * up to OOM. Then it checks for oom and oom_kill events in
1228  * memory.events.
1229  */
1230 static int test_memcg_oom_events(const char *root)
1231 {
1232 	int ret = KSFT_FAIL;
1233 	char *memcg;
1234 
1235 	memcg = cg_name(root, "memcg_test");
1236 	if (!memcg)
1237 		goto cleanup;
1238 
1239 	if (cg_create(memcg))
1240 		goto cleanup;
1241 
1242 	if (cg_write(memcg, "memory.max", "30M"))
1243 		goto cleanup;
1244 
1245 	if (cg_write(memcg, "memory.swap.max", "0"))
1246 		goto cleanup;
1247 
1248 	if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
1249 		goto cleanup;
1250 
1251 	if (cg_read_strcmp(memcg, "cgroup.procs", ""))
1252 		goto cleanup;
1253 
1254 	if (cg_read_key_long(memcg, "memory.events", "oom ") != 1)
1255 		goto cleanup;
1256 
1257 	if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1)
1258 		goto cleanup;
1259 
1260 	ret = KSFT_PASS;
1261 
1262 cleanup:
1263 	cg_destroy(memcg);
1264 	free(memcg);
1265 
1266 	return ret;
1267 }
1268 
1269 struct tcp_server_args {
1270 	unsigned short port;
1271 	int ctl[2];
1272 };
1273 
1274 static int tcp_server(const char *cgroup, void *arg)
1275 {
1276 	struct tcp_server_args *srv_args = arg;
1277 	struct sockaddr_in6 saddr = { 0 };
1278 	socklen_t slen = sizeof(saddr);
1279 	int sk, client_sk, ctl_fd, yes = 1, ret = -1;
1280 
1281 	close(srv_args->ctl[0]);
1282 	ctl_fd = srv_args->ctl[1];
1283 
1284 	saddr.sin6_family = AF_INET6;
1285 	saddr.sin6_addr = in6addr_any;
1286 	saddr.sin6_port = htons(srv_args->port);
1287 
1288 	sk = socket(AF_INET6, SOCK_STREAM, 0);
1289 	if (sk < 0) {
1290 		/* Pass back errno to the ctl_fd */
1291 		write(ctl_fd, &errno, sizeof(errno));
1292 		return ret;
1293 	}
1294 
1295 	if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
1296 		goto cleanup;
1297 
1298 	if (bind(sk, (struct sockaddr *)&saddr, slen)) {
1299 		write(ctl_fd, &errno, sizeof(errno));
1300 		goto cleanup;
1301 	}
1302 
1303 	if (listen(sk, 1))
1304 		goto cleanup;
1305 
1306 	ret = 0;
1307 	if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
1308 		ret = -1;
1309 		goto cleanup;
1310 	}
1311 
1312 	client_sk = accept(sk, NULL, NULL);
1313 	if (client_sk < 0)
1314 		goto cleanup;
1315 
1316 	ret = -1;
1317 	for (;;) {
1318 		uint8_t buf[0x100000];
1319 
1320 		if (write(client_sk, buf, sizeof(buf)) <= 0) {
1321 			if (errno == ECONNRESET)
1322 				ret = 0;
1323 			break;
1324 		}
1325 	}
1326 
1327 	close(client_sk);
1328 
1329 cleanup:
1330 	close(sk);
1331 	return ret;
1332 }
1333 
1334 static int tcp_client(const char *cgroup, unsigned short port)
1335 {
1336 	const char server[] = "localhost";
1337 	struct addrinfo *ai;
1338 	char servport[6];
1339 	long budget = 4 * MEMCG_FLUSH_BYTES;
1340 	int rcvbuf = 64 * 1024;
1341 	int sk, ret;
1342 	long allocated;
1343 
1344 	allocated = cg_read_long(cgroup, "memory.current");
1345 	snprintf(servport, sizeof(servport), "%hd", port);
1346 	ret = getaddrinfo(server, servport, NULL, &ai);
1347 	if (ret)
1348 		return ret;
1349 
1350 	sk = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1351 	if (sk < 0)
1352 		goto free_ainfo;
1353 
1354 	/* keep the data in the server's socket, the one charged to the cgroup */
1355 	ret = setsockopt(sk, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
1356 	if (ret < 0)
1357 		goto close_sk;
1358 
1359 	ret = connect(sk, ai->ai_addr, ai->ai_addrlen);
1360 	if (ret < 0)
1361 		goto close_sk;
1362 
1363 	ret = KSFT_FAIL;
1364 	while (budget > 0) {
1365 		uint8_t buf[0x100000];
1366 		long current, sock;
1367 		ssize_t len;
1368 
1369 		len = read(sk, buf, sizeof(buf));
1370 		if (len <= 0)
1371 			goto close_sk;
1372 		budget -= len;
1373 
1374 		current = cg_read_long(cgroup, "memory.current");
1375 		sock = cg_read_key_long(cgroup, "memory.stat", "sock ");
1376 
1377 		if (current < 0 || sock < 0)
1378 			goto close_sk;
1379 
1380 		/* exclude the memory not related to socket connection */
1381 		if (sock > 0 && values_close(current - allocated, sock, 10)) {
1382 			ret = KSFT_PASS;
1383 			break;
1384 		}
1385 	}
1386 
1387 close_sk:
1388 	close(sk);
1389 free_ainfo:
1390 	freeaddrinfo(ai);
1391 	return ret;
1392 }
1393 
1394 static bool memcg_sockets_disabled(void)
1395 {
1396 	char buf[4 * BUF_SIZE], *opt;
1397 
1398 	if (read_text("/proc/cmdline", buf, sizeof(buf)) <= 0)
1399 		return false;
1400 
1401 	for (opt = strstr(buf, "cgroup.memory="); opt;
1402 	     opt = strstr(opt + 1, "cgroup.memory=")) {
1403 		char *end = strchrnul(opt, ' ');
1404 		char *ns = strstr(opt, "nosocket");
1405 
1406 		if (ns && ns < end)
1407 			return true;
1408 	}
1409 	return false;
1410 }
1411 
1412 /*
1413  * This test checks socket memory accounting.
1414  * The test forks a TCP server listens on a random port between 1000
1415  * and 61000. Once it gets a client connection, it starts writing to
1416  * its socket.
1417  * The TCP client interleaves reads from the socket with check whether
1418  * memory.current and memory.stat.sock are similar.
1419  */
1420 static int test_memcg_sock(const char *root)
1421 {
1422 	int bind_retries = 5, ret = KSFT_FAIL, pid, err;
1423 	unsigned short port;
1424 	char *memcg;
1425 	long sock_post = -1;
1426 	int flush_rounds = 3;
1427 
1428 	if (memcg_sockets_disabled())
1429 		return KSFT_SKIP;
1430 
1431 	memcg = cg_name(root, "memcg_test");
1432 	if (!memcg)
1433 		goto cleanup;
1434 
1435 	if (cg_create(memcg))
1436 		goto cleanup;
1437 
1438 	while (bind_retries--) {
1439 		struct tcp_server_args args;
1440 
1441 		if (pipe(args.ctl))
1442 			goto cleanup;
1443 
1444 		port = args.port = 1000 + rand() % 60000;
1445 
1446 		pid = cg_run_nowait(memcg, tcp_server, &args);
1447 		if (pid < 0)
1448 			goto cleanup;
1449 
1450 		close(args.ctl[1]);
1451 		if (read(args.ctl[0], &err, sizeof(err)) != sizeof(err))
1452 			goto cleanup;
1453 		close(args.ctl[0]);
1454 
1455 		/* Skip if address family not supported by protocol */
1456 		if (err == EAFNOSUPPORT) {
1457 			ret = KSFT_SKIP;
1458 			goto cleanup;
1459 		}
1460 
1461 		if (!err)
1462 			break;
1463 		if (err != EADDRINUSE)
1464 			goto cleanup;
1465 
1466 		waitpid(pid, NULL, 0);
1467 	}
1468 
1469 	if (err == EADDRINUSE) {
1470 		ret = KSFT_SKIP;
1471 		goto cleanup;
1472 	}
1473 
1474 	if (tcp_client(memcg, port) != KSFT_PASS)
1475 		goto cleanup;
1476 
1477 	waitpid(pid, &err, 0);
1478 	if (WEXITSTATUS(err))
1479 		goto cleanup;
1480 
1481 	if (cg_read_long(memcg, "memory.current") < 0)
1482 		goto cleanup;
1483 
1484 	/* the uncharge is done, but a flush still walking the CPUs can hide it from a read */
1485 	while (flush_rounds--) {
1486 		if (cg_run(memcg, alloc_anon, (void *)MEMCG_FLUSH_BYTES))
1487 			goto cleanup;
1488 
1489 		sock_post = cg_read_key_long(memcg, "memory.stat", "sock ");
1490 		if (!sock_post)
1491 			break;
1492 	}
1493 	if (sock_post)
1494 		goto cleanup;
1495 
1496 	ret = KSFT_PASS;
1497 
1498 cleanup:
1499 	cg_destroy(memcg);
1500 	free(memcg);
1501 
1502 	return ret;
1503 }
1504 
1505 /*
1506  * This test disables swapping and tries to allocate anonymous memory
1507  * up to OOM with memory.group.oom set. Then it checks that all
1508  * processes in the leaf were killed. It also checks that oom_events
1509  * were propagated to the parent level.
1510  */
1511 static int test_memcg_oom_group_leaf_events(const char *root)
1512 {
1513 	int ret = KSFT_FAIL;
1514 	char *parent, *child;
1515 	long parent_oom_events;
1516 
1517 	parent = cg_name(root, "memcg_test_0");
1518 	child = cg_name(root, "memcg_test_0/memcg_test_1");
1519 
1520 	if (!parent || !child)
1521 		goto cleanup;
1522 
1523 	if (cg_create(parent))
1524 		goto cleanup;
1525 
1526 	if (cg_create(child))
1527 		goto cleanup;
1528 
1529 	if (cg_write(parent, "cgroup.subtree_control", "+memory"))
1530 		goto cleanup;
1531 
1532 	if (cg_write(child, "memory.max", "50M"))
1533 		goto cleanup;
1534 
1535 	if (cg_write(child, "memory.swap.max", "0"))
1536 		goto cleanup;
1537 
1538 	if (cg_write(child, "memory.oom.group", "1"))
1539 		goto cleanup;
1540 
1541 	cg_run_nowait(parent, alloc_anon_noexit, (void *) MB(60));
1542 	cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
1543 	cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
1544 	if (!cg_run(child, alloc_anon, (void *)MB(100)))
1545 		goto cleanup;
1546 
1547 	if (cg_test_proc_killed(child))
1548 		goto cleanup;
1549 
1550 	if (cg_read_key_long(child, "memory.events", "oom_kill ") <= 0)
1551 		goto cleanup;
1552 
1553 	parent_oom_events = cg_read_key_long(
1554 			parent, "memory.events", "oom_kill ");
1555 	/*
1556 	 * If memory_localevents is not enabled (the default), the parent should
1557 	 * count OOM events in its children groups. Otherwise, it should not
1558 	 * have observed any events.
1559 	 */
1560 	if (has_localevents && parent_oom_events != 0)
1561 		goto cleanup;
1562 	else if (!has_localevents && parent_oom_events <= 0)
1563 		goto cleanup;
1564 
1565 	ret = KSFT_PASS;
1566 
1567 cleanup:
1568 	if (child)
1569 		cg_destroy(child);
1570 	if (parent)
1571 		cg_destroy(parent);
1572 	free(child);
1573 	free(parent);
1574 
1575 	return ret;
1576 }
1577 
1578 /*
1579  * This test disables swapping and tries to allocate anonymous memory
1580  * up to OOM with memory.group.oom set. Then it checks that all
1581  * processes in the parent and leaf were killed.
1582  */
1583 static int test_memcg_oom_group_parent_events(const char *root)
1584 {
1585 	int ret = KSFT_FAIL;
1586 	char *parent, *child;
1587 
1588 	parent = cg_name(root, "memcg_test_0");
1589 	child = cg_name(root, "memcg_test_0/memcg_test_1");
1590 
1591 	if (!parent || !child)
1592 		goto cleanup;
1593 
1594 	if (cg_create(parent))
1595 		goto cleanup;
1596 
1597 	if (cg_create(child))
1598 		goto cleanup;
1599 
1600 	if (cg_write(parent, "memory.max", "80M"))
1601 		goto cleanup;
1602 
1603 	if (cg_write(parent, "memory.swap.max", "0"))
1604 		goto cleanup;
1605 
1606 	if (cg_write(parent, "memory.oom.group", "1"))
1607 		goto cleanup;
1608 
1609 	cg_run_nowait(parent, alloc_anon_noexit, (void *) MB(60));
1610 	cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
1611 	cg_run_nowait(child, alloc_anon_noexit, (void *) MB(1));
1612 
1613 	if (!cg_run(child, alloc_anon, (void *)MB(100)))
1614 		goto cleanup;
1615 
1616 	if (cg_test_proc_killed(child))
1617 		goto cleanup;
1618 	if (cg_test_proc_killed(parent))
1619 		goto cleanup;
1620 
1621 	ret = KSFT_PASS;
1622 
1623 cleanup:
1624 	if (child)
1625 		cg_destroy(child);
1626 	if (parent)
1627 		cg_destroy(parent);
1628 	free(child);
1629 	free(parent);
1630 
1631 	return ret;
1632 }
1633 
1634 /*
1635  * This test disables swapping and tries to allocate anonymous memory
1636  * up to OOM with memory.group.oom set. Then it checks that all
1637  * processes were killed except those set with OOM_SCORE_ADJ_MIN
1638  */
1639 static int test_memcg_oom_group_score_events(const char *root)
1640 {
1641 	int ret = KSFT_FAIL;
1642 	char *memcg;
1643 	int safe_pid;
1644 
1645 	memcg = cg_name(root, "memcg_test_0");
1646 
1647 	if (!memcg)
1648 		goto cleanup;
1649 
1650 	if (cg_create(memcg))
1651 		goto cleanup;
1652 
1653 	if (cg_write(memcg, "memory.max", "50M"))
1654 		goto cleanup;
1655 
1656 	if (cg_write(memcg, "memory.swap.max", "0"))
1657 		goto cleanup;
1658 
1659 	if (cg_write(memcg, "memory.oom.group", "1"))
1660 		goto cleanup;
1661 
1662 	safe_pid = cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(1));
1663 	if (set_oom_adj_score(safe_pid, OOM_SCORE_ADJ_MIN))
1664 		goto cleanup;
1665 
1666 	cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(1));
1667 	if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
1668 		goto cleanup;
1669 
1670 	if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 3)
1671 		goto cleanup;
1672 
1673 	if (kill(safe_pid, SIGKILL))
1674 		goto cleanup;
1675 
1676 	ret = KSFT_PASS;
1677 
1678 cleanup:
1679 	if (memcg)
1680 		cg_destroy(memcg);
1681 	free(memcg);
1682 
1683 	return ret;
1684 }
1685 
1686 static int read_event(int inotify_fd, int expected_event, int expected_wd)
1687 {
1688 	struct inotify_event event;
1689 	ssize_t len = 0;
1690 
1691 	len = read(inotify_fd, &event, sizeof(event));
1692 	if (len < (ssize_t)sizeof(event))
1693 		return -1;
1694 
1695 	if (event.mask != expected_event || event.wd != expected_wd) {
1696 		fprintf(stderr,
1697 			"event does not match expected values: mask %d (expected %d) wd %d (expected %d)\n",
1698 			event.mask, expected_event, event.wd, expected_wd);
1699 		return -1;
1700 	}
1701 
1702 	return 0;
1703 }
1704 
1705 static int test_memcg_inotify_delete_file(const char *root)
1706 {
1707 	int ret = KSFT_FAIL;
1708 	char *memcg = NULL, *events = NULL;
1709 	int fd = -1, wd;
1710 
1711 	memcg = cg_name(root, "memcg_test_0");
1712 
1713 	if (!memcg)
1714 		goto cleanup;
1715 
1716 	if (cg_create(memcg))
1717 		goto cleanup;
1718 
1719 	fd = inotify_init1(0);
1720 	if (fd == -1)
1721 		goto cleanup;
1722 
1723 	events = cg_control(memcg, "memory.events");
1724 	if (!events)
1725 		goto cleanup;
1726 
1727 	wd = inotify_add_watch(fd, events, IN_DELETE_SELF);
1728 	if (wd == -1)
1729 		goto cleanup;
1730 
1731 	if (cg_destroy(memcg))
1732 		goto cleanup;
1733 	free(memcg);
1734 	memcg = NULL;
1735 
1736 	if (read_event(fd, IN_DELETE_SELF, wd))
1737 		goto cleanup;
1738 
1739 	if (read_event(fd, IN_IGNORED, wd))
1740 		goto cleanup;
1741 
1742 	ret = KSFT_PASS;
1743 
1744 cleanup:
1745 	if (fd >= 0)
1746 		close(fd);
1747 	if (memcg)
1748 		cg_destroy(memcg);
1749 	free(events);
1750 	free(memcg);
1751 
1752 	return ret;
1753 }
1754 
1755 static int test_memcg_inotify_delete_dir(const char *root)
1756 {
1757 	int ret = KSFT_FAIL;
1758 	char *memcg = NULL;
1759 	int fd = -1, wd;
1760 
1761 	memcg = cg_name(root, "memcg_test_0");
1762 
1763 	if (!memcg)
1764 		goto cleanup;
1765 
1766 	if (cg_create(memcg))
1767 		goto cleanup;
1768 
1769 	fd = inotify_init1(0);
1770 	if (fd == -1)
1771 		goto cleanup;
1772 
1773 	wd = inotify_add_watch(fd, memcg, IN_DELETE_SELF);
1774 	if (wd == -1)
1775 		goto cleanup;
1776 
1777 	if (cg_destroy(memcg))
1778 		goto cleanup;
1779 	free(memcg);
1780 	memcg = NULL;
1781 
1782 	if (read_event(fd, IN_DELETE_SELF, wd))
1783 		goto cleanup;
1784 
1785 	if (read_event(fd, IN_IGNORED, wd))
1786 		goto cleanup;
1787 
1788 	ret = KSFT_PASS;
1789 
1790 cleanup:
1791 	if (fd >= 0)
1792 		close(fd);
1793 	if (memcg)
1794 		cg_destroy(memcg);
1795 	free(memcg);
1796 
1797 	return ret;
1798 }
1799 
1800 #define T(x) { x, #x }
1801 struct memcg_test {
1802 	int (*fn)(const char *root);
1803 	const char *name;
1804 } tests[] = {
1805 	T(test_memcg_subtree_control),
1806 	T(test_memcg_current_peak),
1807 	T(test_memcg_min),
1808 	T(test_memcg_low),
1809 	T(test_memcg_high),
1810 	T(test_memcg_high_sync),
1811 	T(test_memcg_max),
1812 	T(test_memcg_reclaim),
1813 	T(test_memcg_oom_events),
1814 	T(test_memcg_swap_max_peak),
1815 	T(test_memcg_sock),
1816 	T(test_memcg_oom_group_leaf_events),
1817 	T(test_memcg_oom_group_parent_events),
1818 	T(test_memcg_oom_group_score_events),
1819 	T(test_memcg_inotify_delete_file),
1820 	T(test_memcg_inotify_delete_dir),
1821 };
1822 #undef T
1823 
1824 int main(int argc, char **argv)
1825 {
1826 	char root[PATH_MAX];
1827 	int i, proc_status;
1828 
1829 	page_size = sysconf(_SC_PAGE_SIZE);
1830 	if (page_size <= 0)
1831 		page_size = BUF_SIZE;
1832 
1833 	ksft_print_header();
1834 	if (cg_find_unified_root(root, sizeof(root), NULL))
1835 		ksft_exit_skip("cgroup v2 isn't mounted\n");
1836 
1837 	/*
1838 	 * Check that memory controller is available:
1839 	 * memory is listed in cgroup.controllers
1840 	 */
1841 	if (cg_read_strstr(root, "cgroup.controllers", "memory"))
1842 		ksft_exit_skip("memory controller isn't available\n");
1843 
1844 	if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
1845 		if (cg_write(root, "cgroup.subtree_control", "+memory"))
1846 			ksft_exit_skip("Failed to set memory controller\n");
1847 
1848 	proc_status = proc_mount_contains("memory_recursiveprot");
1849 	if (proc_status < 0)
1850 		ksft_exit_skip("Failed to query cgroup mount option\n");
1851 	has_recursiveprot = proc_status;
1852 
1853 	proc_status = proc_mount_contains("memory_localevents");
1854 	if (proc_status < 0)
1855 		ksft_exit_skip("Failed to query cgroup mount option\n");
1856 	has_localevents = proc_status;
1857 
1858 	ksft_set_plan(ARRAY_SIZE(tests));
1859 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
1860 		switch (tests[i].fn(root)) {
1861 		case KSFT_PASS:
1862 			ksft_test_result_pass("%s\n", tests[i].name);
1863 			break;
1864 		case KSFT_SKIP:
1865 			ksft_test_result_skip("%s\n", tests[i].name);
1866 			break;
1867 		default:
1868 			ksft_test_result_fail("%s\n", tests[i].name);
1869 			break;
1870 		}
1871 	}
1872 
1873 	ksft_finished();
1874 }
1875