xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/oom/oom04.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) 2010-2017  Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2011-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 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...");
30*49cdfc7eSAndroid Build Coastguard Worker 	testoom(0, 0, ENOMEM, 1);
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (is_numa(NULL, NH_MEMS, 2) &&
33*49cdfc7eSAndroid Build Coastguard Worker 	    SAFE_CG_HAS(tst_cg, "cpuset.memory_migrate")) {
34*49cdfc7eSAndroid Build Coastguard Worker 		/*
35*49cdfc7eSAndroid Build Coastguard Worker 		 * Under NUMA system, the migration of cpuset's memory
36*49cdfc7eSAndroid Build Coastguard Worker 		 * is in charge of cpuset.memory_migrate, we can write
37*49cdfc7eSAndroid Build Coastguard Worker 		 * 1 to cpuset.memory_migrate to enable the migration.
38*49cdfc7eSAndroid Build Coastguard Worker 		 */
39*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CG_PRINT(tst_cg, "cpuset.memory_migrate", "1");
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "OOM on CPUSET with mem migrate:");
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 
setup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	int memnode, ret;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	if (!is_numa(NULL, NH_MEMS, 1))
51*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "requires NUMA with at least 1 node");
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	/*
54*49cdfc7eSAndroid Build Coastguard Worker 	 * Some nodes do not contain memory, so use
55*49cdfc7eSAndroid Build Coastguard Worker 	 * get_allowed_nodes(NH_MEMS) to get a memory
56*49cdfc7eSAndroid Build Coastguard Worker 	 * node. This operation also applies to Non-NUMA
57*49cdfc7eSAndroid Build Coastguard Worker 	 * systems.
58*49cdfc7eSAndroid Build Coastguard Worker 	 */
59*49cdfc7eSAndroid Build Coastguard Worker 	ret = get_allowed_nodes(NH_MEMS, 1, &memnode);
60*49cdfc7eSAndroid Build Coastguard Worker 	if (ret < 0)
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Failed to get a memory node "
62*49cdfc7eSAndroid Build Coastguard Worker 			      "using get_allowed_nodes()");
63*49cdfc7eSAndroid Build Coastguard Worker 	write_cpusets(tst_cg, memnode);
64*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
68*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
69*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
70*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = TST_UNLIMITED_RUNTIME,
71*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
72*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_oom,
73*49cdfc7eSAndroid Build Coastguard Worker 	.needs_cgroup_ctrls = (const char *const []){ "cpuset", NULL },
74*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_compat = 1,
75*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
76*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
77*49cdfc7eSAndroid Build Coastguard Worker 		{}
78*49cdfc7eSAndroid Build Coastguard Worker 	},
79*49cdfc7eSAndroid Build Coastguard Worker };
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker #else
82*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF(NUMA_ERROR_MSG);
83*49cdfc7eSAndroid Build Coastguard Worker #endif
84