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