1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: LGPL-2.1-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (C) 2009 IBM Corporation.
4f08c3bdfSopenharmony_ci * Author: David Gibson
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * The kernel has bug for mremap() on some architecture. mremap() can
11f08c3bdfSopenharmony_ci * cause crashes on architectures with holes in the address space
12f08c3bdfSopenharmony_ci * (like ia64) and on powerpc with it's distinct page size "slices".
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * This test get the normal mapping address and mremap() hugepage mapping
15f08c3bdfSopenharmony_ci * near to this normal mapping.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define _GNU_SOURCE
19f08c3bdfSopenharmony_ci#include "hugetlb.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define MNTPOINT "hugetlbfs/"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic int fd = -1;
24f08c3bdfSopenharmony_cistatic long hpage_size;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic int do_remap(int fd, void *target)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	void *a, *b;
29f08c3bdfSopenharmony_ci	int ret;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	a = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	ret = do_readback(a, hpage_size, "base huge");
34f08c3bdfSopenharmony_ci	if (ret)
35f08c3bdfSopenharmony_ci		goto cleanup;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	b = mremap(a, hpage_size, hpage_size, MREMAP_MAYMOVE | MREMAP_FIXED,
38f08c3bdfSopenharmony_ci		   target);
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if (b != MAP_FAILED) {
41f08c3bdfSopenharmony_ci		ret = do_readback(b, hpage_size, "remapped");
42f08c3bdfSopenharmony_ci		a = b;
43f08c3bdfSopenharmony_ci	} else {
44f08c3bdfSopenharmony_ci		tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
45f08c3bdfSopenharmony_ci	}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cicleanup:
48f08c3bdfSopenharmony_ci	SAFE_MUNMAP(a, hpage_size);
49f08c3bdfSopenharmony_ci	return ret;
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void *map_align(size_t size, size_t align)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	unsigned long xsize = size + align - getpagesize();
55f08c3bdfSopenharmony_ci	size_t t;
56f08c3bdfSopenharmony_ci	void *p, *q;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	p = SAFE_MMAP(NULL, xsize, PROT_READ|PROT_WRITE,
59f08c3bdfSopenharmony_ci		 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	q = PALIGN(p, align);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	t = q - p;
64f08c3bdfSopenharmony_ci	if (t)
65f08c3bdfSopenharmony_ci		SAFE_MUNMAP(p, t);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	t = p + xsize - (q + size);
68f08c3bdfSopenharmony_ci	if (t)
69f08c3bdfSopenharmony_ci		SAFE_MUNMAP(q + size, t);
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	return q;
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cistatic void run_test(void)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	void *p;
77f08c3bdfSopenharmony_ci	int ret;
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	fd = tst_creat_unlinked(MNTPOINT, 0);
80f08c3bdfSopenharmony_ci	p = map_align(3*hpage_size, hpage_size);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	SAFE_MUNMAP(p, hpage_size);
83f08c3bdfSopenharmony_ci	SAFE_MUNMAP(p + 2*hpage_size, hpage_size);
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	p = p + hpage_size;
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	tst_res(TINFO, "Normal mapping at %p", p);
88f08c3bdfSopenharmony_ci	ret = do_readback(p, hpage_size, "base normal page");
89f08c3bdfSopenharmony_ci	if (ret)
90f08c3bdfSopenharmony_ci		goto cleanup;
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	ret = do_remap(fd, p - hpage_size);
93f08c3bdfSopenharmony_ci	if (ret)
94f08c3bdfSopenharmony_ci		goto cleanup;
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	ret = do_remap(fd, p + hpage_size);
97f08c3bdfSopenharmony_ci	if (ret == 0)
98f08c3bdfSopenharmony_ci		tst_res(TPASS, "Successfully tested mremap hpage near normal mapping");
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_cicleanup:
101f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
102f08c3bdfSopenharmony_ci}
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_cistatic void setup(void)
105f08c3bdfSopenharmony_ci{
106f08c3bdfSopenharmony_ci	hpage_size = tst_get_hugepage_size();
107f08c3bdfSopenharmony_ci}
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_cistatic void cleanup(void)
110f08c3bdfSopenharmony_ci{
111f08c3bdfSopenharmony_ci	if (fd >= 0)
112f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
113f08c3bdfSopenharmony_ci}
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_cistatic struct tst_test test = {
116f08c3bdfSopenharmony_ci	.needs_root = 1,
117f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
118f08c3bdfSopenharmony_ci	.needs_hugetlbfs = 1,
119f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
120f08c3bdfSopenharmony_ci	.setup = setup,
121f08c3bdfSopenharmony_ci	.cleanup = cleanup,
122f08c3bdfSopenharmony_ci	.test_all = run_test,
123f08c3bdfSopenharmony_ci	.hugepages = {3, TST_NEEDS},
124f08c3bdfSopenharmony_ci};
125