1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2005-2006 IBM Corporation.
4 * Author: David Gibson & Adam Litke
5 */
6
7/*\
8 * [Description]
9 *
10 * The test do mmap() with shared mapping and write. It matches the data
11 * with private mmap() and then change it with other data. It checks
12 * shared mapping data if data is not contaiminated by private mapping.
13 * Similiarly checks for private data if it is not contaminated by changing
14 * shared mmap data.
15 */
16
17#include "hugetlb.h"
18
19#define C1 0x1234ABCD
20#define C2 0xFEDC9876
21
22#define MNTPOINT "hugetlbfs/"
23static unsigned long hpage_size;
24static int fd = -1;
25
26static void run_test(void)
27{
28	void *p, *q;
29	unsigned int *pl, *ql;
30	unsigned long i;
31
32	fd = tst_creat_unlinked(MNTPOINT, 0);
33	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
34		 fd, 0);
35
36	pl = p;
37	for (i = 0; i < (hpage_size / sizeof(*pl)); i++)
38		pl[i] = C1 ^ i;
39
40	q = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
41		 fd, 0);
42
43	ql = q;
44	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
45		if (ql[i] != (C1 ^ i)) {
46			tst_res(TFAIL, "Mismatch at offset %lu, got: %u, expected: %lu",
47					i, ql[i], C1 ^ i);
48			goto cleanup;
49		}
50	}
51
52	for (i = 0; i < (hpage_size / sizeof(*ql)); i++)
53		ql[i] = C2 ^ i;
54
55	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
56		if (ql[i] != (C2 ^ i)) {
57			tst_res(TFAIL, "PRIVATE mismatch at offset %lu, got: %u, expected: %lu",
58					i, ql[i], C2 ^ i);
59			goto cleanup;
60		}
61	}
62
63	for (i = 0; i < (hpage_size / sizeof(*pl)); i++) {
64		if (pl[i] != (C1 ^ i)) {
65			tst_res(TFAIL, "SHARED map contaminated at offset %lu, "
66					"got: %u, expected: %lu", i, pl[i], C1 ^ i);
67			goto cleanup;
68		}
69	}
70
71	memset(p, 0, hpage_size);
72
73	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
74		if (ql[i] != (C2 ^ i)) {
75			tst_res(TFAIL, "PRIVATE map contaminated at offset %lu, "
76					"got: %u, expected: %lu", i, ql[i], C2 ^ i);
77			goto cleanup;
78		}
79	}
80	tst_res(TPASS, "Successfully tested shared/private mmaping and its data");
81cleanup:
82	SAFE_MUNMAP(p, hpage_size);
83	SAFE_MUNMAP(q, hpage_size);
84	SAFE_CLOSE(fd);
85}
86
87static void setup(void)
88{
89	hpage_size = tst_get_hugepage_size();
90}
91
92static void cleanup(void)
93{
94	if (fd >= 0)
95		SAFE_CLOSE(fd);
96}
97
98static struct tst_test test = {
99	.needs_root = 1,
100	.mntpoint = MNTPOINT,
101	.needs_hugetlbfs = 1,
102	.needs_tmpdir = 1,
103	.setup = setup,
104	.cleanup = cleanup,
105	.test_all = run_test,
106	.hugepages = {2, TST_NEEDS},
107};
108