xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/oom/oom03.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 Memory Resource Controller
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 	testoom(0, 0, ENOMEM, 1);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (SAFE_CG_HAS(tst_cg, "memory.swap.max")) {
32*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "OOM on MEMCG with special memswap limitation:");
33*49cdfc7eSAndroid Build Coastguard Worker 		/*
34*49cdfc7eSAndroid Build Coastguard Worker 		 * Cgroup v2 tracks memory and swap in separate, which splits
35*49cdfc7eSAndroid Build Coastguard Worker 		 * memory and swap counter. So with swappiness enable (default
36*49cdfc7eSAndroid Build Coastguard Worker 		 * value is 60 on RHEL), it likely has part of memory swapping
37*49cdfc7eSAndroid Build Coastguard Worker 		 * out during the allocating.
38*49cdfc7eSAndroid Build Coastguard Worker 		 *
39*49cdfc7eSAndroid Build Coastguard Worker 		 * To get more opportunities to reach the swap limitation,
40*49cdfc7eSAndroid Build Coastguard Worker 		 * let's scale down the value of 'memory.swap.max' to only
41*49cdfc7eSAndroid Build Coastguard Worker 		 * 1MB for CGroup v2.
42*49cdfc7eSAndroid Build Coastguard Worker 		 */
43*49cdfc7eSAndroid Build Coastguard Worker 		if (!TST_CG_VER_IS_V1(tst_cg, "memory"))
44*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", MB);
45*49cdfc7eSAndroid Build Coastguard Worker 		else
46*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", TESTMEM + MB);
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 		testoom(0, 1, ENOMEM, 1);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 		if (TST_CG_VER_IS_V1(tst_cg, "memory"))
51*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CG_PRINTF(tst_cg, "memory.swap.max", "%lu", ~0UL);
52*49cdfc7eSAndroid Build Coastguard Worker 		else
53*49cdfc7eSAndroid Build Coastguard Worker 			SAFE_CG_PRINT(tst_cg, "memory.swap.max", "max");
54*49cdfc7eSAndroid Build Coastguard Worker 	}
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	/* OOM for MEMCG with mempolicy */
57*49cdfc7eSAndroid Build Coastguard Worker 	if (is_numa(NULL, NH_MEMS, 2)) {
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "OOM on MEMCG & mempolicy...");
59*49cdfc7eSAndroid Build Coastguard Worker 		testoom(MPOL_BIND, 0, ENOMEM, 1);
60*49cdfc7eSAndroid Build Coastguard Worker 		testoom(MPOL_INTERLEAVE, 0, ENOMEM, 1);
61*49cdfc7eSAndroid Build Coastguard Worker 		testoom(MPOL_PREFERRED, 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 static void setup(void)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
72*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
73*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
74*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = TST_UNLIMITED_RUNTIME,
75*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
76*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_oom,
77*49cdfc7eSAndroid Build Coastguard Worker 	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
78*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_compat = 1,
79*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
80*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/vm/overcommit_memory", "1", TST_SR_TBROK},
81*49cdfc7eSAndroid Build Coastguard Worker 		{}
82*49cdfc7eSAndroid Build Coastguard Worker 	},
83*49cdfc7eSAndroid Build Coastguard Worker };
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker #else
86*49cdfc7eSAndroid Build Coastguard Worker 	TST_TEST_TCONF(NUMA_ERROR_MSG);
87*49cdfc7eSAndroid Build Coastguard Worker #endif
88