1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2010-2017 Red Hat, Inc.
4 * Copyright (c) Linux Test Project, 2011-2023
5 */
6 /*\
7 * [Description]
8 *
9 * Out Of Memory (OOM) test for Memory Resource Controller
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 testoom(0, 0, ENOMEM, 1);
30
31 if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
32 tst_res(TINFO, "OOM on MEMCG with special memswap limitation:");
33 /*
34 * Cgroup v2 tracks memory and swap in separate, which splits
35 * memory and swap counter. So with swappiness enable (default
36 * value is 60 on RHEL), it likely has part of memory swapping
37 * out during the allocating.
38 *
39 * To get more opportunities to reach the swap limitation,
40 * let's scale down the value of 'memory.swap.max' to only
41 * 1MB for CGroup v2.
42 */
43 if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
44 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", MB);
45 else
46 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", TESTMEM + MB);
47
48 testoom(0, 1, ENOMEM, 1);
49
50 if (TST_CG_VER_IS_V1(tst_cg, "memory"))
51 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
52 else
53 SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
54 }
55
56 /* OOM for MEMCG with mempolicy */
57 if (is_numa(NULL, NH_MEMS, 2)) {
58 tst_res(TINFO, "OOM on MEMCG & mempolicy...");
59 testoom(MPOL_BIND, 0, ENOMEM, 1);
60 testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
61 testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
62 }
63 }
64
setup(void)65 static void setup(void)
66 {
67 SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
68 SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
69 }
70
71 static struct tst_test test = {
72 .needs_root = 1,
73 .forks_child = 1,
74 .max_runtime = TST_UNLIMITED_RUNTIME,
75 .setup = setup,
76 .test_all = verify_oom,
77 .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
78 .skip_in_compat = 1,
79 .save_restore = (const struct tst_path_val[]) {
80 {"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
81 {}
82 },
83 };
84
85 #else
86 TST_TEST_TCONF(NUMA_ERROR_MSG);
87 #endif
88