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