1 /*
2 * Out Of Memory (OOM) for Memory Resource Controller
3 *
4 * The program is designed to cope with unpredictable like amount and
5 * system physical memory, swap size and other VMM technology like KSM,
6 * memcg, memory hotplug and so on which may affect the OOM
7 * behaviours. It simply increase the memory consumption 3G each time
8 * until all the available memory is consumed and OOM is triggered.
9 *
10 * Copyright (C) 2010-2017 Red Hat, Inc.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
20 * the GNU General Public License for more details.
21 */
22
23 #include "config.h"
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #if HAVE_NUMA_H
30 #include <numa.h>
31 #endif
32
33 #include "lapi/abisize.h"
34 #include "numa_helper.h"
35 #include "mem.h"
36
37 #ifdef HAVE_NUMA_V2
38
39 static const struct tst_cgroup_group *cg;
40
verify_oom(void)41 static void verify_oom(void)
42 {
43 #ifdef TST_ABI32
44 tst_brk(TCONF, "test is not designed for 32-bit system.");
45 #endif
46 testoom(0, 0, ENOMEM, 1);
47
48 if (SAFE_CGROUP_HAS(cg, "memory.swap.max")) {
49 tst_res(TINFO, "OOM on MEMCG with special memswap limitation:");
50 /*
51 * Cgroup v2 tracks memory and swap in separate, which splits
52 * memory and swap counter. So with swappiness enable (default
53 * value is 60 on RHEL), it likely has part of memory swapping
54 * out during the allocating.
55 *
56 * To get more opportunities to reach the swap limitation,
57 * let's scale down the value of 'memory.swap.max' to only
58 * 1MB for CGroup v2.
59 */
60 if (!TST_CGROUP_VER_IS_V1(cg, "memory"))
61 SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", MB);
62 else
63 SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", TESTMEM + MB);
64
65 testoom(0, 1, ENOMEM, 1);
66
67 if (TST_CGROUP_VER_IS_V1(cg, "memory"))
68 SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", ~0UL);
69 else
70 SAFE_CGROUP_PRINT(cg, "memory.swap.max", "max");
71 }
72
73 /* OOM for MEMCG with mempolicy */
74 if (is_numa(NULL, NH_MEMS, 2)) {
75 tst_res(TINFO, "OOM on MEMCG & mempolicy...");
76 testoom(MPOL_BIND, 0, ENOMEM, 1);
77 testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
78 testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
79 }
80 }
81
setup(void)82 static void setup(void)
83 {
84 overcommit = get_sys_tune("overcommit_memory");
85 set_sys_tune("overcommit_memory", 1, 1);
86
87 tst_cgroup_require("memory", NULL);
88 cg = tst_cgroup_get_test_group();
89 SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
90 SAFE_CGROUP_PRINTF(cg, "memory.max", "%lu", TESTMEM);
91 }
92
cleanup(void)93 static void cleanup(void)
94 {
95 if (overcommit != -1)
96 set_sys_tune("overcommit_memory", overcommit, 0);
97 tst_cgroup_cleanup();
98 }
99
100 static struct tst_test test = {
101 .needs_root = 1,
102 .forks_child = 1,
103 .timeout = -1,
104 .setup = setup,
105 .cleanup = cleanup,
106 .test_all = verify_oom,
107 };
108
109 #else
110 TST_TEST_TCONF(NUMA_ERROR_MSG);
111 #endif
112