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