xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/thp/thp03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (C) 2012-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 /* thp03 - Case for spliting unaligned memory.
16  *       - System will panic if failed.
17  *
18  * Modified form a reproducer for
19  *          https://patchwork.kernel.org/patch/1358441/
20  * Kernel Commit id: 027ef6c87853b0a9df53175063028edb4950d476
21  * There was a bug in THP, will crash happened due to the following
22  * reason according to developers:
23  *
24  * most VM places are using pmd_none but a few are still using
25  * pmd_present. The meaning is about the same for the pmd. However
26  * pmd_present would return the wrong value on PROT_NONE ranges or in
27  * case of a non reproducible race with split_huge_page.
28  * When the code using pmd_present gets a false negative, the kernel will
29  * crash. It's just an annoying DoS with a BUG_ON triggering: no memory
30  * corruption and no data corruption (nor userland nor kernel).
31  */
32 
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include "mem.h"
40 #include "lapi/mmap.h"
41 
42 static void thp_test(void);
43 
44 static long hugepage_size;
45 static long unaligned_size;
46 static long page_size;
47 
thp_test(void)48 static void thp_test(void)
49 {
50 	void *p;
51 
52 	p = SAFE_MMAP(NULL, unaligned_size, PROT_READ | PROT_WRITE,
53 		 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
54 
55 	memset(p, 0x00, unaligned_size);
56 	if (mprotect(p, unaligned_size, PROT_NONE) == -1)
57 		tst_brk(TBROK | TERRNO, "mprotect");
58 
59 	if (madvise(p + hugepage_size, page_size, MADV_MERGEABLE) == -1) {
60 		if (errno == EINVAL) {
61 			tst_brk(TCONF,
62 			         "MADV_MERGEABLE is not enabled/supported");
63 		} else {
64 			tst_brk(TBROK | TERRNO, "madvise");
65 		}
66 	}
67 
68 	switch (SAFE_FORK()) {
69 	case 0:
70 		exit(0);
71 	default:
72 		SAFE_WAITPID(-1, NULL, 0);
73 	}
74 
75 	tst_res(TPASS, "system didn't crash, pass.");
76 	munmap(p, unaligned_size);
77 }
78 
setup(void)79 static void setup(void)
80 {
81 	if (access(PATH_THP, F_OK) == -1)
82 		tst_brk(TCONF, "THP not enabled in kernel?");
83 
84 	check_hugepage();
85 
86 	hugepage_size = SAFE_READ_MEMINFO("Hugepagesize:") * KB;
87 	unaligned_size = hugepage_size * 4 - 1;
88 	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
89 }
90 
91 static struct tst_test test = {
92 	.needs_root = 1,
93 	.forks_child = 1,
94 	.setup = setup,
95 	.test_all = thp_test,
96 };
97