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
verify_oom(void)39 static void verify_oom(void)
40 {
41 #ifdef TST_ABI32
42 tst_brk(TCONF, "test is not designed for 32-bit system.");
43 #endif
44 testoom(0, 0, ENOMEM, 1);
45
46 if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
47 tst_res(TINFO, "OOM on MEMCG with special memswap limitation:");
48 /*
49 * Cgroup v2 tracks memory and swap in separate, which splits
50 * memory and swap counter. So with swappiness enable (default
51 * value is 60 on RHEL), it likely has part of memory swapping
52 * out during the allocating.
53 *
54 * To get more opportunities to reach the swap limitation,
55 * let's scale down the value of 'memory.swap.max' to only
56 * 1MB for CGroup v2.
57 */
58 if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
59 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", MB);
60 else
61 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", TESTMEM + MB);
62
63 testoom(0, 1, ENOMEM, 1);
64
65 if (TST_CG_VER_IS_V1(tst_cg, "memory"))
66 SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
67 else
68 SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
69 }
70
71 /* OOM for MEMCG with mempolicy */
72 if (is_numa(NULL, NH_MEMS, 2)) {
73 tst_res(TINFO, "OOM on MEMCG & mempolicy...");
74 testoom(MPOL_BIND, 0, ENOMEM, 1);
75 testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
76 testoom(MPOL_PREFERRED, 0, ENOMEM, 1);
77 }
78 }
79
setup(void)80 static void setup(void)
81 {
82 SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
83 SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
84 }
85
86 static struct tst_test test = {
87 .needs_root = 1,
88 .forks_child = 1,
89 .max_runtime = TST_UNLIMITED_RUNTIME,
90 .setup = setup,
91 .test_all = verify_oom,
92 .needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
93 .save_restore = (const struct tst_path_val[]) {
94 {"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
95 {}
96 },
97 };
98
99 #else
100 TST_TEST_TCONF(NUMA_ERROR_MSG);
101 #endif
102