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 * Test Description: The kernel has bug for mremap() on some architecture.
11f08c3bdfSopenharmony_ci * mremap() can cause crashes on architectures with holes in the address
12f08c3bdfSopenharmony_ci * space (like ia64) and on powerpc with it's distinct page size "slices".
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * This test get the huge mapping address and mremap() normal mapping
15f08c3bdfSopenharmony_ci * near to this huge 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, page_size;
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic int do_remap(void *target)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	void *a, *b;
29f08c3bdfSopenharmony_ci	int ret;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
32f08c3bdfSopenharmony_ci		  MAP_SHARED|MAP_ANONYMOUS, -1, 0);
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	ret = do_readback(a, page_size, "base normal");
35f08c3bdfSopenharmony_ci	if (ret)
36f08c3bdfSopenharmony_ci		goto cleanup;
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	b = mremap(a, page_size, page_size, MREMAP_MAYMOVE | MREMAP_FIXED,
39f08c3bdfSopenharmony_ci		   target);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	if (b != MAP_FAILED) {
42f08c3bdfSopenharmony_ci		do_readback(b, page_size, "remapped");
43f08c3bdfSopenharmony_ci		a = b;
44f08c3bdfSopenharmony_ci	} else {
45f08c3bdfSopenharmony_ci		tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
46f08c3bdfSopenharmony_ci	}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cicleanup:
49f08c3bdfSopenharmony_ci	SAFE_MUNMAP(a, page_size);
50f08c3bdfSopenharmony_ci	return ret;
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void run_test(void)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	void *p;
56f08c3bdfSopenharmony_ci	int ret;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	fd = tst_creat_unlinked(MNTPOINT, 0);
59f08c3bdfSopenharmony_ci	p = SAFE_MMAP(NULL, 3*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	SAFE_MUNMAP(p, hpage_size);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	SAFE_MUNMAP(p + 2*hpage_size, hpage_size);
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	p = p + hpage_size;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	tst_res(TINFO, "Hugepage mapping at %p", p);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	ret = do_readback(p, hpage_size, "base hugepage");
70f08c3bdfSopenharmony_ci	if (ret)
71f08c3bdfSopenharmony_ci		goto cleanup;
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	ret = do_remap(p - page_size);
74f08c3bdfSopenharmony_ci	if (ret)
75f08c3bdfSopenharmony_ci		goto cleanup;
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	ret = do_remap(p + hpage_size);
78f08c3bdfSopenharmony_ci	if (ret == 0)
79f08c3bdfSopenharmony_ci		tst_res(TPASS, "Successfully tested mremap normal near hpage mapping");
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cicleanup:
82f08c3bdfSopenharmony_ci	SAFE_MUNMAP(p, hpage_size);
83f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
84f08c3bdfSopenharmony_ci}
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cistatic void setup(void)
87f08c3bdfSopenharmony_ci{
88f08c3bdfSopenharmony_ci	hpage_size = tst_get_hugepage_size();
89f08c3bdfSopenharmony_ci	page_size = getpagesize();
90f08c3bdfSopenharmony_ci}
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_cistatic void cleanup(void)
93f08c3bdfSopenharmony_ci{
94f08c3bdfSopenharmony_ci	if (fd >= 0)
95f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
96f08c3bdfSopenharmony_ci}
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_cistatic struct tst_test test = {
99f08c3bdfSopenharmony_ci	.needs_root = 1,
100f08c3bdfSopenharmony_ci	.mntpoint = MNTPOINT,
101f08c3bdfSopenharmony_ci	.needs_hugetlbfs = 1,
102f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
103f08c3bdfSopenharmony_ci	.setup = setup,
104f08c3bdfSopenharmony_ci	.cleanup = cleanup,
105f08c3bdfSopenharmony_ci	.test_all = run_test,
106f08c3bdfSopenharmony_ci	.hugepages = {3, TST_NEEDS},
107f08c3bdfSopenharmony_ci};
108