• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static int ps;
44 static long hps, size;
45 
46 /*
47  * Will try to do the following 4 mremaps cases:
48  *   mremap(p, size-ps, size-ps, flag, p2);
49  *   mremap(p, size-ps, size-ps, flag, p2+ps);
50  *   mremap(p+ps, size-ps, size-ps, flag, p2);
51  *   mremap(p+ps, size-ps, size-ps, flag, p2+ps);
52  */
do_child(int i)53 static void do_child(int i)
54 {
55 	long j, remap_size;
56 	unsigned char *p1, *p2, *ret, *old_addr, *new_addr;
57 
58 	p1 = SAFE_MEMALIGN(hps, size);
59 	p2 = SAFE_MEMALIGN(hps, size);
60 
61 	memset(p1, 0xff, size);
62 	memset(p2, 0x77, size);
63 
64 	old_addr = p1 + ps * (i >> 1);
65 	new_addr = p2 + ps * (i & 1);
66 	remap_size = size - ps;
67 
68 	tst_res(TINFO, "mremap (%p-%p) to (%p-%p)",
69 		old_addr, old_addr + remap_size,
70 		new_addr, new_addr + remap_size);
71 
72 	ret = mremap(old_addr, remap_size, remap_size,
73 		    MREMAP_FIXED | MREMAP_MAYMOVE, new_addr);
74 	if (ret == MAP_FAILED)
75 		tst_brk(TBROK | TERRNO, "mremap");
76 
77 	for (j = 0; j < remap_size; j++) {
78 		if (ret[j] != 0xff)
79 			tst_brk(TBROK, "mremap bug");
80 	}
81 
82 	exit(0);
83 }
84 
do_mremap(void)85 static void do_mremap(void)
86 {
87 	int i;
88 
89 	for (i = 0; i < 4; i++) {
90 		if (SAFE_FORK() == 0)
91 			do_child(i);
92 		tst_reap_children();
93 	}
94 	tst_res(TPASS, "Still alive.");
95 }
96 
setup(void)97 static void setup(void)
98 {
99 	long memfree;
100 
101 	if (access(PATH_THP, F_OK) == -1)
102 		tst_brk(TCONF, "THP not enabled in kernel?");
103 
104 	check_hugepage();
105 
106 	ps = sysconf(_SC_PAGESIZE);
107 	hps = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
108 	size = hps * 4;
109 
110 	memfree = (SAFE_READ_MEMINFO("MemFree:") * 1024 +
111 		SAFE_READ_MEMINFO("Cached:") * 1024);
112 	if (memfree < size * 2)
113 		tst_brk(TCONF, "not enough memory");
114 }
115 
116 static struct tst_test test = {
117 	.setup = setup,
118 	.test_all = do_mremap,
119 	.forks_child = 1,
120 };
121