xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/oom/oom01.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
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include "mem.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
verify_oom(void)21*49cdfc7eSAndroid Build Coastguard Worker static void verify_oom(void)
22*49cdfc7eSAndroid Build Coastguard Worker {
23*49cdfc7eSAndroid Build Coastguard Worker 	/* we expect mmap to fail before OOM is hit */
24*49cdfc7eSAndroid Build Coastguard Worker 	set_sys_tune("overcommit_memory", 2, 1);
25*49cdfc7eSAndroid Build Coastguard Worker 	oom(NORMAL, 0, ENOMEM, 0);
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker 	/* with overcommit_memory set to 0 or 1 there's no
28*49cdfc7eSAndroid Build Coastguard Worker 	 * guarantee that mmap fails before OOM */
29*49cdfc7eSAndroid Build Coastguard Worker 	set_sys_tune("overcommit_memory", 0, 1);
30*49cdfc7eSAndroid Build Coastguard Worker 	oom(NORMAL, 0, ENOMEM, 1);
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	set_sys_tune("overcommit_memory", 1, 1);
33*49cdfc7eSAndroid Build Coastguard Worker 	testoom(0, 0, ENOMEM, 1);
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
37*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
38*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
39*49cdfc7eSAndroid Build Coastguard Worker 	.max_runtime = TST_UNLIMITED_RUNTIME,
40*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_oom,
41*49cdfc7eSAndroid Build Coastguard Worker 	.skip_in_compat = 1,
42*49cdfc7eSAndroid Build Coastguard Worker 	.save_restore = (const struct tst_path_val[]) {
43*49cdfc7eSAndroid Build Coastguard Worker 		{"/proc/sys/vm/overcommit_memory", NULL, TST_SR_TBROK},
44*49cdfc7eSAndroid Build Coastguard Worker 		{}
45*49cdfc7eSAndroid Build Coastguard Worker 	},
46*49cdfc7eSAndroid Build Coastguard Worker };
47