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