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