xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/madvise/madvise03.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) Huawei Technologies Co., Ltd. 2022. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zhao Gongyi <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Check that successful madvise(2) MADV_DONTNEED operation will result in
11*49cdfc7eSAndroid Build Coastguard Worker  * zero-fill-on-demand pages for anonymous private mappings.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #define MAP_SIZE (8 * 1024)
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker static char *addr;
19*49cdfc7eSAndroid Build Coastguard Worker 
run(void)20*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	int i;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker 	memset(addr, 1, MAP_SIZE);
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker 	TEST(madvise(addr, MAP_SIZE, MADV_DONTNEED));
27*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
28*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "madvise(%p, %d, 0x%x) failed",
29*49cdfc7eSAndroid Build Coastguard Worker 			addr, MAP_SIZE, MADV_DONTNEED);
30*49cdfc7eSAndroid Build Coastguard Worker 	}
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < MAP_SIZE; i++) {
33*49cdfc7eSAndroid Build Coastguard Worker 		if (addr[i]) {
34*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL,
35*49cdfc7eSAndroid Build Coastguard Worker 				"There are no zero-fill-on-demand pages "
36*49cdfc7eSAndroid Build Coastguard Worker 				"for anonymous private mappings");
37*49cdfc7eSAndroid Build Coastguard Worker 			return;
38*49cdfc7eSAndroid Build Coastguard Worker 		}
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	if (i == MAP_SIZE) {
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS,
43*49cdfc7eSAndroid Build Coastguard Worker 			"There are zero-fill-on-demand pages "
44*49cdfc7eSAndroid Build Coastguard Worker 			"for anonymous private mappings");
45*49cdfc7eSAndroid Build Coastguard Worker 	}
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)48*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	addr = SAFE_MMAP(NULL, MAP_SIZE,
51*49cdfc7eSAndroid Build Coastguard Worker 			PROT_READ | PROT_WRITE,
52*49cdfc7eSAndroid Build Coastguard Worker 			MAP_PRIVATE | MAP_ANONYMOUS,
53*49cdfc7eSAndroid Build Coastguard Worker 			-1, 0);
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)56*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	if (addr)
59*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(addr, MAP_SIZE);
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
63*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
64*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
65*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
66*49cdfc7eSAndroid Build Coastguard Worker };
67