1 /* SPDX-License-Identifier: GPL-2.0 */
4 #include <linux/limits.h>
10 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include <netinet/in.h>
19 #include "../kselftest.h"
20 #include "cgroup_util.h"
23 * This test creates two nested cgroups with and without enabling
24 * the memory controller.
26 static int test_memcg_subtree_control(const char *root)
28 char *parent, *child, *parent2, *child2;
32 /* Create two nested cgroups with the memory controller enabled */
33 parent = cg_name(root, "memcg_test_0");
34 child = cg_name(root, "memcg_test_0/memcg_test_1");
35 if (!parent || !child)
38 if (cg_create(parent))
41 if (cg_write(parent, "cgroup.subtree_control", "+memory"))
47 if (cg_read_strstr(child, "cgroup.controllers", "memory"))
50 /* Create two nested cgroups without enabling memory controller */
51 parent2 = cg_name(root, "memcg_test_1");
52 child2 = cg_name(root, "memcg_test_1/memcg_test_1");
53 if (!parent2 || !child2)
56 if (cg_create(parent2))
59 if (cg_create(child2))
62 if (cg_read(child2, "cgroup.controllers", buf, sizeof(buf)))
65 if (!cg_read_strstr(child2, "cgroup.controllers", "memory"))
84 static int alloc_anon_50M_check(const char *cgroup, void *arg)
92 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
95 current = cg_read_long(cgroup, "memory.current");
99 if (!values_close(size, current, 3))
102 anon = cg_read_key_long(cgroup, "memory.stat", "anon ");
106 if (!values_close(anon, current, 3))
115 static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
117 size_t size = MB(50);
126 if (alloc_pagecache(fd, size))
129 current = cg_read_long(cgroup, "memory.current");
133 file = cg_read_key_long(cgroup, "memory.stat", "file ");
137 if (!values_close(file, current, 10))
148 * This test create a memory cgroup, allocates
149 * some anonymous memory and some pagecache
150 * and check memory.current and some memory.stat values.
152 static int test_memcg_current(const char *root)
158 memcg = cg_name(root, "memcg_test");
162 if (cg_create(memcg))
165 current = cg_read_long(memcg, "memory.current");
169 if (cg_run(memcg, alloc_anon_50M_check, NULL))
172 if (cg_run(memcg, alloc_pagecache_50M_check, NULL))
184 static int alloc_pagecache_50M(const char *cgroup, void *arg)
188 return alloc_pagecache(fd, MB(50));
191 static int alloc_pagecache_50M_noexit(const char *cgroup, void *arg)
194 int ppid = getppid();
196 if (alloc_pagecache(fd, MB(50)))
199 while (getppid() == ppid)
206 * First, this test creates the following hierarchy:
207 * A memory.min = 50M, memory.max = 200M
208 * A/B memory.min = 50M, memory.current = 50M
209 * A/B/C memory.min = 75M, memory.current = 50M
210 * A/B/D memory.min = 25M, memory.current = 50M
211 * A/B/E memory.min = 500M, memory.current = 0
212 * A/B/F memory.min = 0, memory.current = 50M
214 * Usages are pagecache, but the test keeps a running
215 * process in every leaf cgroup.
216 * Then it creates A/G and creates a significant
217 * memory pressure in it.
219 * A/B memory.current ~= 50M
220 * A/B/C memory.current ~= 33M
221 * A/B/D memory.current ~= 17M
222 * A/B/E memory.current ~= 0
224 * After that it tries to allocate more than there is
225 * unprotected memory in A available, and checks
226 * checks that memory.min protects pagecache even
229 static int test_memcg_min(const char *root)
232 char *parent[3] = {NULL};
233 char *children[4] = {NULL};
242 parent[0] = cg_name(root, "memcg_test_0");
246 parent[1] = cg_name(parent[0], "memcg_test_1");
250 parent[2] = cg_name(parent[0], "memcg_test_2");
254 if (cg_create(parent[0]))
257 if (cg_read_long(parent[0], "memory.min")) {
262 if (cg_write(parent[0], "cgroup.subtree_control", "+memory"))
265 if (cg_write(parent[0], "memory.max", "200M"))
268 if (cg_write(parent[0], "memory.swap.max", "0"))
271 if (cg_create(parent[1]))
274 if (cg_write(parent[1], "cgroup.subtree_control", "+memory"))
277 if (cg_create(parent[2]))
280 for (i = 0; i < ARRAY_SIZE(children); i++) {
281 children[i] = cg_name_indexed(parent[1], "child_memcg", i);
285 if (cg_create(children[i]))
291 cg_run_nowait(children[i], alloc_pagecache_50M_noexit,
295 if (cg_write(parent[0], "memory.min", "50M"))
297 if (cg_write(parent[1], "memory.min", "50M"))
299 if (cg_write(children[0], "memory.min", "75M"))
301 if (cg_write(children[1], "memory.min", "25M"))
303 if (cg_write(children[2], "memory.min", "500M"))
305 if (cg_write(children[3], "memory.min", "0"))
309 while (!values_close(cg_read_long(parent[1], "memory.current"),
316 if (cg_run(parent[2], alloc_anon, (void *)MB(148)))
319 if (!values_close(cg_read_long(parent[1], "memory.current"), MB(50), 3))
322 for (i = 0; i < ARRAY_SIZE(children); i++)
323 c[i] = cg_read_long(children[i], "memory.current");
325 if (!values_close(c[0], MB(33), 10))
328 if (!values_close(c[1], MB(17), 10))
331 if (!values_close(c[2], 0, 1))
334 if (!cg_run(parent[2], alloc_anon, (void *)MB(170)))
337 if (!values_close(cg_read_long(parent[1], "memory.current"), MB(50), 3))
343 for (i = ARRAY_SIZE(children) - 1; i >= 0; i--) {
347 cg_destroy(children[i]);
351 for (i = ARRAY_SIZE(parent) - 1; i >= 0; i--) {
355 cg_destroy(parent[i]);
363 * First, this test creates the following hierarchy:
364 * A memory.low = 50M, memory.max = 200M
365 * A/B memory.low = 50M, memory.current = 50M
366 * A/B/C memory.low = 75M, memory.current = 50M
367 * A/B/D memory.low = 25M, memory.current = 50M
368 * A/B/E memory.low = 500M, memory.current = 0
369 * A/B/F memory.low = 0, memory.current = 50M
371 * Usages are pagecache.
372 * Then it creates A/G an creates a significant
373 * memory pressure in it.
375 * Then it checks actual memory usages and expects that:
376 * A/B memory.current ~= 50M
377 * A/B/ memory.current ~= 33M
378 * A/B/D memory.current ~= 17M
379 * A/B/E memory.current ~= 0
381 * After that it tries to allocate more than there is
382 * unprotected memory in A available,
383 * and checks low and oom events in memory.events.
385 static int test_memcg_low(const char *root)
388 char *parent[3] = {NULL};
389 char *children[4] = {NULL};
399 parent[0] = cg_name(root, "memcg_test_0");
403 parent[1] = cg_name(parent[0], "memcg_test_1");
407 parent[2] = cg_name(parent[0], "memcg_test_2");
411 if (cg_create(parent[0]))
414 if (cg_read_long(parent[0], "memory.low"))
417 if (cg_write(parent[0], "cgroup.subtree_control", "+memory"))
420 if (cg_write(parent[0], "memory.max", "200M"))
423 if (cg_write(parent[0], "memory.swap.max", "0"))
426 if (cg_create(parent[1]))
429 if (cg_write(parent[1], "cgroup.subtree_control", "+memory"))
432 if (cg_create(parent[2]))
435 for (i = 0; i < ARRAY_SIZE(children); i++) {
436 children[i] = cg_name_indexed(parent[1], "child_memcg", i);
440 if (cg_create(children[i]))
446 if (cg_run(children[i], alloc_pagecache_50M, (void *)(long)fd))
450 if (cg_write(parent[0], "memory.low", "50M"))
452 if (cg_write(parent[1], "memory.low", "50M"))
454 if (cg_write(children[0], "memory.low", "75M"))
456 if (cg_write(children[1], "memory.low", "25M"))
458 if (cg_write(children[2], "memory.low", "500M"))
460 if (cg_write(children[3], "memory.low", "0"))
463 if (cg_run(parent[2], alloc_anon, (void *)MB(148)))
466 if (!values_close(cg_read_long(parent[1], "memory.current"), MB(50), 3))
469 for (i = 0; i < ARRAY_SIZE(children); i++)
470 c[i] = cg_read_long(children[i], "memory.current");
472 if (!values_close(c[0], MB(33), 10))
475 if (!values_close(c[1], MB(17), 10))
478 if (!values_close(c[2], 0, 1))
481 if (cg_run(parent[2], alloc_anon, (void *)MB(166))) {
483 "memory.low prevents from allocating anon memory\n");
487 for (i = 0; i < ARRAY_SIZE(children); i++) {
488 oom = cg_read_key_long(children[i], "memory.events", "oom ");
489 low = cg_read_key_long(children[i], "memory.events", "low ");
493 if (i < 2 && low <= 0)
502 for (i = ARRAY_SIZE(children) - 1; i >= 0; i--) {
506 cg_destroy(children[i]);
510 for (i = ARRAY_SIZE(parent) - 1; i >= 0; i--) {
514 cg_destroy(parent[i]);
521 static int alloc_pagecache_max_30M(const char *cgroup, void *arg)
523 size_t size = MB(50);
532 if (alloc_pagecache(fd, size))
535 current = cg_read_long(cgroup, "memory.current");
536 if (current <= MB(29) || current > MB(30))
548 * This test checks that memory.high limits the amount of
549 * memory which can be consumed by either anonymous memory
552 static int test_memcg_high(const char *root)
558 memcg = cg_name(root, "memcg_test");
562 if (cg_create(memcg))
565 if (cg_read_strcmp(memcg, "memory.high", "max\n"))
568 if (cg_write(memcg, "memory.swap.max", "0"))
571 if (cg_write(memcg, "memory.high", "30M"))
574 if (cg_run(memcg, alloc_anon, (void *)MB(100)))
577 if (!cg_run(memcg, alloc_pagecache_50M_check, NULL))
580 if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
583 high = cg_read_key_long(memcg, "memory.events", "high ");
597 * This test checks that memory.max limits the amount of
598 * memory which can be consumed by either anonymous memory
601 static int test_memcg_max(const char *root)
607 memcg = cg_name(root, "memcg_test");
611 if (cg_create(memcg))
614 if (cg_read_strcmp(memcg, "memory.max", "max\n"))
617 if (cg_write(memcg, "memory.swap.max", "0"))
620 if (cg_write(memcg, "memory.max", "30M"))
623 /* Should be killed by OOM killer */
624 if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
627 if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
630 current = cg_read_long(memcg, "memory.current");
631 if (current > MB(30) || !current)
634 max = cg_read_key_long(memcg, "memory.events", "max ");
647 static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
649 long mem_max = (long)arg;
650 size_t size = MB(50);
652 long mem_current, swap_current;
656 for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE)
659 mem_current = cg_read_long(cgroup, "memory.current");
660 if (!mem_current || !values_close(mem_current, mem_max, 3))
663 swap_current = cg_read_long(cgroup, "memory.swap.current");
665 !values_close(mem_current + swap_current, size, 3))
675 * This test checks that memory.swap.max limits the amount of
676 * anonymous memory which can be swapped out.
678 static int test_memcg_swap_max(const char *root)
684 if (!is_swap_enabled())
687 memcg = cg_name(root, "memcg_test");
691 if (cg_create(memcg))
694 if (cg_read_long(memcg, "memory.swap.current")) {
699 if (cg_read_strcmp(memcg, "memory.max", "max\n"))
702 if (cg_read_strcmp(memcg, "memory.swap.max", "max\n"))
705 if (cg_write(memcg, "memory.swap.max", "30M"))
708 if (cg_write(memcg, "memory.max", "30M"))
711 /* Should be killed by OOM killer */
712 if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
715 if (cg_read_key_long(memcg, "memory.events", "oom ") != 1)
718 if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1)
721 if (cg_run(memcg, alloc_anon_50M_check_swap, (void *)MB(30)))
724 max = cg_read_key_long(memcg, "memory.events", "max ");
738 * This test disables swapping and tries to allocate anonymous memory
739 * up to OOM. Then it checks for oom and oom_kill events in
742 static int test_memcg_oom_events(const char *root)
747 memcg = cg_name(root, "memcg_test");
751 if (cg_create(memcg))
754 if (cg_write(memcg, "memory.max", "30M"))
757 if (cg_write(memcg, "memory.swap.max", "0"))
760 if (!cg_run(memcg, alloc_anon, (void *)MB(100)))
763 if (cg_read_strcmp(memcg, "cgroup.procs", ""))
766 if (cg_read_key_long(memcg, "memory.events", "oom ") != 1)
769 if (cg_read_key_long(memcg, "memory.events", "oom_kill ") != 1)
781 struct tcp_server_args {
786 static int tcp_server(const char *cgroup, void *arg)
788 struct tcp_server_args *srv_args = arg;
789 struct sockaddr_in6 saddr = { 0 };
790 socklen_t slen = sizeof(saddr);
791 int sk, client_sk, ctl_fd, yes = 1, ret = -1;
793 close(srv_args->ctl[0]);
794 ctl_fd = srv_args->ctl[1];
796 saddr.sin6_family = AF_INET6;
797 saddr.sin6_addr = in6addr_any;
798 saddr.sin6_port = htons(srv_args->port);
800 sk = socket(AF_INET6, SOCK_STREAM, 0);
804 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
807 if (bind(sk, (struct sockaddr *)&saddr, slen)) {
808 write(ctl_fd, &errno, sizeof(errno));
816 if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
821 client_sk = accept(sk, NULL, NULL);
827 uint8_t buf[0x100000];
829 if (write(client_sk, buf, sizeof(buf)) <= 0) {
830 if (errno == ECONNRESET)
843 static int tcp_client(const char *cgroup, unsigned short port)
845 const char server[] = "localhost";
848 int retries = 0x10; /* nice round number */
851 snprintf(servport, sizeof(servport), "%hd", port);
852 ret = getaddrinfo(server, servport, NULL, &ai);
856 sk = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
860 ret = connect(sk, ai->ai_addr, ai->ai_addrlen);
866 uint8_t buf[0x100000];
869 if (read(sk, buf, sizeof(buf)) <= 0)
872 current = cg_read_long(cgroup, "memory.current");
873 sock = cg_read_key_long(cgroup, "memory.stat", "sock ");
875 if (current < 0 || sock < 0)
881 if (values_close(current, sock, 10)) {
895 * This test checks socket memory accounting.
896 * The test forks a TCP server listens on a random port between 1000
897 * and 61000. Once it gets a client connection, it starts writing to
899 * The TCP client interleaves reads from the socket with check whether
900 * memory.current and memory.stat.sock are similar.
902 static int test_memcg_sock(const char *root)
904 int bind_retries = 5, ret = KSFT_FAIL, pid, err;
908 memcg = cg_name(root, "memcg_test");
912 if (cg_create(memcg))
915 while (bind_retries--) {
916 struct tcp_server_args args;
921 port = args.port = 1000 + rand() % 60000;
923 pid = cg_run_nowait(memcg, tcp_server, &args);
928 if (read(args.ctl[0], &err, sizeof(err)) != sizeof(err))
934 if (err != EADDRINUSE)
937 waitpid(pid, NULL, 0);
940 if (err == EADDRINUSE) {
945 if (tcp_client(memcg, port) != KSFT_PASS)
948 waitpid(pid, &err, 0);
949 if (WEXITSTATUS(err))
952 if (cg_read_long(memcg, "memory.current") < 0)
955 if (cg_read_key_long(memcg, "memory.stat", "sock "))
967 #define T(x) { x, #x }
969 int (*fn)(const char *root);
972 T(test_memcg_subtree_control),
973 T(test_memcg_current),
978 T(test_memcg_oom_events),
979 T(test_memcg_swap_max),
984 int main(int argc, char **argv)
987 int i, ret = EXIT_SUCCESS;
989 if (cg_find_unified_root(root, sizeof(root)))
990 ksft_exit_skip("cgroup v2 isn't mounted\n");
993 * Check that memory controller is available:
994 * memory is listed in cgroup.controllers
996 if (cg_read_strstr(root, "cgroup.controllers", "memory"))
997 ksft_exit_skip("memory controller isn't available\n");
999 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1000 switch (tests[i].fn(root)) {
1002 ksft_test_result_pass("%s\n", tests[i].name);
1005 ksft_test_result_skip("%s\n", tests[i].name);
1009 ksft_test_result_fail("%s\n", tests[i].name);