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