xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/thp/thp03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2012-2017  Red Hat, Inc.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker /* thp03 - Case for spliting unaligned memory.
16*49cdfc7eSAndroid Build Coastguard Worker  *       - System will panic if failed.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * Modified form a reproducer for
19*49cdfc7eSAndroid Build Coastguard Worker  *          https://patchwork.kernel.org/patch/1358441/
20*49cdfc7eSAndroid Build Coastguard Worker  * Kernel Commit id: 027ef6c87853b0a9df53175063028edb4950d476
21*49cdfc7eSAndroid Build Coastguard Worker  * There was a bug in THP, will crash happened due to the following
22*49cdfc7eSAndroid Build Coastguard Worker  * reason according to developers:
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * most VM places are using pmd_none but a few are still using
25*49cdfc7eSAndroid Build Coastguard Worker  * pmd_present. The meaning is about the same for the pmd. However
26*49cdfc7eSAndroid Build Coastguard Worker  * pmd_present would return the wrong value on PROT_NONE ranges or in
27*49cdfc7eSAndroid Build Coastguard Worker  * case of a non reproducible race with split_huge_page.
28*49cdfc7eSAndroid Build Coastguard Worker  * When the code using pmd_present gets a false negative, the kernel will
29*49cdfc7eSAndroid Build Coastguard Worker  * crash. It's just an annoying DoS with a BUG_ON triggering: no memory
30*49cdfc7eSAndroid Build Coastguard Worker  * corruption and no data corruption (nor userland nor kernel).
31*49cdfc7eSAndroid Build Coastguard Worker  */
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include "mem.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/mmap.h"
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker static void thp_test(void);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker static long hugepage_size;
45*49cdfc7eSAndroid Build Coastguard Worker static long unaligned_size;
46*49cdfc7eSAndroid Build Coastguard Worker static long page_size;
47*49cdfc7eSAndroid Build Coastguard Worker 
thp_test(void)48*49cdfc7eSAndroid Build Coastguard Worker static void thp_test(void)
49*49cdfc7eSAndroid Build Coastguard Worker {
50*49cdfc7eSAndroid Build Coastguard Worker 	void *p;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	p = SAFE_MMAP(NULL, unaligned_size, PROT_READ | PROT_WRITE,
53*49cdfc7eSAndroid Build Coastguard Worker 		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	memset(p, 0x00, unaligned_size);
56*49cdfc7eSAndroid Build Coastguard Worker 	if (mprotect(p, unaligned_size, PROT_NONE) == -1)
57*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "mprotect");
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	if (madvise(p + hugepage_size, page_size, MADV_MERGEABLE) == -1) {
60*49cdfc7eSAndroid Build Coastguard Worker 		if (errno == EINVAL) {
61*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TCONF,
62*49cdfc7eSAndroid Build Coastguard Worker 			         "MADV_MERGEABLE is not enabled/supported");
63*49cdfc7eSAndroid Build Coastguard Worker 		} else {
64*49cdfc7eSAndroid Build Coastguard Worker 			tst_brk(TBROK | TERRNO, "madvise");
65*49cdfc7eSAndroid Build Coastguard Worker 		}
66*49cdfc7eSAndroid Build Coastguard Worker 	}
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	switch (SAFE_FORK()) {
69*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
70*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
71*49cdfc7eSAndroid Build Coastguard Worker 	default:
72*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAITPID(-1, NULL, 0);
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "system didn't crash, pass.");
76*49cdfc7eSAndroid Build Coastguard Worker 	munmap(p, unaligned_size);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)79*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	if (access(PATH_THP, F_OK) == -1)
82*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "THP not enabled in kernel?");
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	check_hugepage();
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	hugepage_size = SAFE_READ_MEMINFO("Hugepagesize:") * KB;
87*49cdfc7eSAndroid Build Coastguard Worker 	unaligned_size = hugepage_size * 4 - 1;
88*49cdfc7eSAndroid Build Coastguard Worker 	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
93*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
94*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
95*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = thp_test,
96*49cdfc7eSAndroid Build Coastguard Worker };
97