1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2013-2017 Red Hat, Inc.
4 * Copyright (c) Linux Test Project, 2014-2023
5 */
6 /*\
7 * [Description]
8 *
9 * Out Of Memory (OOM) test for MEMCG and CPUSET
10 */
11
12 #include "config.h"
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <stdio.h>
18 #if HAVE_NUMA_H
19 #include <numa.h>
20 #endif
21
22 #include "numa_helper.h"
23 #include "mem.h"
24
25 #ifdef HAVE_NUMA_V2
26
verify_oom(void)27 static void verify_oom(void)
28 {
29 tst_res(TINFO, "OOM on CPUSET & MEMCG...");
30 testoom(0, 0, ENOMEM, 1);
31
32 /*
33 * Under NUMA system, the migration of cpuset's memory
34 * is in charge of cpuset.memory_migrate, we can write
35 * 1 to cpuset.memory_migrate to enable the migration.
36 */
37 if (is_numa(NULL, NH_MEMS, 2) &&
38 SAFE_CG_HAS(tst_cg, "cpuset.memory_migrate")) {
39 SAFE_CG_PRINT(tst_cg, "cpuset.memory_migrate", "1");
40 tst_res(TINFO, "OOM on CPUSET & MEMCG with "
41 "cpuset.memory_migrate=1");
42 testoom(0, 0, ENOMEM, 1);
43 }
44
45 if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
46 tst_res(TINFO, "OOM on CPUSET & MEMCG with "
47 "special memswap limitation:");
48 if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
49 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", MB);
50 else
51 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", TESTMEM + MB);
52
53 testoom(0, 1, ENOMEM, 1);
54
55 tst_res(TINFO, "OOM on CPUSET & MEMCG with "
56 "disabled memswap limitation:");
57 if (TST_CG_VER_IS_V1(tst_cg, "memory"))
58 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
59 else
60 SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
61 testoom(0, 0, ENOMEM, 1);
62 }
63 }
64
setup(void)65 void setup(void)
66 {
67 int ret, memnode;
68
69 if (!is_numa(NULL, NH_MEMS, 1))
70 tst_brk(TCONF, "requires NUMA with at least 1 node");
71
72 /*
73 * Some nodes do not contain memory, so use
74 * get_allowed_nodes(NH_MEMS) to get a memory
75 * node. This operation also applies to Non-NUMA
76 * systems.
77 */
78 ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
79 if (ret < 0)
80 tst_brk(TBROK, "Failed to get a memory node "
81 "using get_allowed_nodes()");
82
83 write_cpusets(tst_cg, memnode);
84 SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
85 SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
86 }
87
88 static struct tst_test test = {
89 .needs_root = 1,
90 .forks_child = 1,
91 .max_runtime = TST_UNLIMITED_RUNTIME,
92 .setup = setup,
93 .test_all = verify_oom,
94 .needs_cgroup_ctrls = (const char *const []){
95 "memory", "cpuset", NULL
96 },
97 .skip_in_compat = 1,
98 .save_restore = (const struct tst_path_val[]) {
99 {"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
100 {}
101 },
102 };
103
104 #else
105 TST_TEST_TCONF(NUMA_ERROR_MSG);
106 #endif
107