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