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 * This test is basic shared mapping test. Two shared mappings are created
11 * with same offset on a file. It checks if writing to one mapping can be
12 * seen to other mapping or not?
13 */
14
15#include "hugetlb.h"
16
17#define RANDOM_CONSTANT 0x1234ABCD
18#define MNTPOINT "hugetlbfs/"
19
20static long hpage_size;
21static int fd = -1;
22
23static void run_test(void)
24{
25	void *p, *q;
26	unsigned long *pl, *ql;
27	unsigned long i;
28
29	fd = tst_creat_unlinked(MNTPOINT, 0);
30	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
31		 fd, 0);
32
33	q = 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] = RANDOM_CONSTANT ^ i;
39
40	ql = q;
41	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
42		if (ql[i] != (RANDOM_CONSTANT ^ i)) {
43			tst_res(TFAIL, "Mismatch at offset %lu, Got: %lu, Expected: %lu",
44					i, ql[i], RANDOM_CONSTANT ^ i);
45			goto cleanup;
46		}
47	}
48
49	tst_res(TPASS, "Successfully tested data between two shared mappings");
50cleanup:
51	SAFE_MUNMAP(p, hpage_size);
52	SAFE_MUNMAP(q, hpage_size);
53	SAFE_CLOSE(fd);
54}
55
56static void setup(void)
57{
58	hpage_size = tst_get_hugepage_size();
59}
60
61static void cleanup(void)
62{
63	if (fd >= 0)
64		SAFE_CLOSE(fd);
65}
66
67static struct tst_test test = {
68	.needs_root = 1,
69	.mntpoint = MNTPOINT,
70	.needs_hugetlbfs = 1,
71	.needs_tmpdir = 1,
72	.setup = setup,
73	.cleanup = cleanup,
74	.test_all = run_test,
75	.hugepages = {2, TST_NEEDS},
76};
77