1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2011-2017 Red Hat, Inc.
4 */
5
6 /*\
7 * [Description]
8 *
9 * Test for detecting mremap bug when THP is enabled.
10 *
11 * There was a bug in mremap THP support, sometimes causing crash
12 * due to the following reason:
13 *
14 * "alloc_new_pmd was forcing the allocation of a pte before calling
15 * move_huge_page and that resulted in a VM_BUG_ON in move_huge_page
16 * because the pmd wasn't zero."
17 *
18 * There are 4 cases to test this bug:
19 *
20 * 1. old_addr hpage aligned, old_end not hpage aligned, new_addr
21 * hpage aligned
22 *
23 * 2. old_addr hpage aligned, old_end not hpage aligned, new_addr not
24 * hpage aligned
25 *
26 * 3. old_addr not hpage aligned, old_end hpage aligned, new_addr
27 * hpage aligned
28 *
29 * 4. old_addr not hpage aligned, old_end hpage aligned, new_addr not
30 * hpage aligned
31 */
32
33 #define _GNU_SOURCE
34 #include "config.h"
35 #include <sys/types.h>
36 #include <sys/mman.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "mem.h"
42
43 #ifdef HAVE_MREMAP_FIXED
44 static int ps;
45 static long hps, size;
46
47 /*
48 * Will try to do the following 4 mremaps cases:
49 * mremap(p, size-ps, size-ps, flag, p2);
50 * mremap(p, size-ps, size-ps, flag, p2+ps);
51 * mremap(p+ps, size-ps, size-ps, flag, p2);
52 * mremap(p+ps, size-ps, size-ps, flag, p2+ps);
53 */
do_child(int i)54 static void do_child(int i)
55 {
56 long j, remap_size;
57 unsigned char *p1, *p2, *ret, *old_addr, *new_addr;
58
59 p1 = SAFE_MEMALIGN(hps, size);
60 p2 = SAFE_MEMALIGN(hps, size);
61
62 memset(p1, 0xff, size);
63 memset(p2, 0x77, size);
64
65 old_addr = p1 + ps * (i >> 1);
66 new_addr = p2 + ps * (i & 1);
67 remap_size = size - ps;
68
69 tst_res(TINFO, "mremap (%p-%p) to (%p-%p)",
70 old_addr, old_addr + remap_size,
71 new_addr, new_addr + remap_size);
72
73 ret = mremap(old_addr, remap_size, remap_size,
74 MREMAP_FIXED | MREMAP_MAYMOVE, new_addr);
75 if (ret == MAP_FAILED)
76 tst_brk(TBROK | TERRNO, "mremap");
77
78 for (j = 0; j < remap_size; j++) {
79 if (ret[j] != 0xff)
80 tst_brk(TBROK, "mremap bug");
81 }
82
83 exit(0);
84 }
85
do_mremap(void)86 static void do_mremap(void)
87 {
88 int i;
89
90 for (i = 0; i < 4; i++) {
91 if (SAFE_FORK() == 0)
92 do_child(i);
93 tst_reap_children();
94 }
95 tst_res(TPASS, "Still alive.");
96 }
97
setup(void)98 static void setup(void)
99 {
100 long memfree;
101
102 if (access(PATH_THP, F_OK) == -1)
103 tst_brk(TCONF, "THP not enabled in kernel?");
104
105 check_hugepage();
106
107 ps = sysconf(_SC_PAGESIZE);
108 hps = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
109 size = hps * 4;
110
111 memfree = (SAFE_READ_MEMINFO("MemFree:") * 1024 +
112 SAFE_READ_MEMINFO("Cached:") * 1024);
113 if (memfree < size * 2)
114 tst_brk(TCONF, "not enough memory");
115 }
116
117 static struct tst_test test = {
118 .setup = setup,
119 .test_all = do_mremap,
120 .forks_child = 1,
121 };
122
123 #else
124 TST_TEST_TCONF("MREMAP_FIXED not present in <sys/mman.h>");
125 #endif /* HAVE_MREMAP_FIXED */
126