blob: 3932df864ddea7d947fccf5b2fbf9e7a6fbfaa82 [file] [log] [blame]
Madper Xiee209c6b2012-10-30 10:07:26 +08001/*
2 * Copyright (C) 2012 Red Hat, Inc.
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it would be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 *
11 * Further, this software is distributed without any warranty that it
12 * is free of the rightful claim of any third person regarding
13 * infringement or the like. Any license provided herein, whether
14 * implied or otherwise, applies only to this software file. Patent
15 * licenses, if any, provided herein do not apply to combinations of
16 * this program with other software, or any other product whatsoever.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 * thp03 - Case for spliting unaligned memory.
24 * - System will panic if failed.
25 *
26 * Modified form a reproducer for
27 * https://patchwork.kernel.org/patch/1358441/
Wanlong Gaoaae18db2012-11-05 15:10:39 +080028 * Kernel Commit id: 027ef6c87853b0a9df53175063028edb4950d476
Madper Xiee209c6b2012-10-30 10:07:26 +080029 * There was a bug in THP, will crash happened due to the following
30 * reason according to developers:
31 *
32 * most VM places are using pmd_none but a few are still using
33 * pmd_present. The meaning is about the same for the pmd. However
34 * pmd_present would return the wrong value on PROT_NONE ranges or in
35 * case of a non reproducible race with split_huge_page.
36 * When the code using pmd_present gets a false negative, the kernel will
37 * crash. It's just an annoying DoS with a BUG_ON triggering: no memory
38 * corruption and no data corruption (nor userland nor kernel).
39 */
40
41#include <sys/mman.h>
42#include <sys/types.h>
43#include <sys/wait.h>
44#include <fcntl.h>
45#include <stdlib.h>
46#include <string.h>
Cyril Hrubisf2ec5c62013-04-30 16:29:43 +020047#include <errno.h>
Madper Xiee209c6b2012-10-30 10:07:26 +080048#include "mem.h"
49#include "safe_macros.h"
50#include "test.h"
Madper Xiee209c6b2012-10-30 10:07:26 +080051
52char *TCID = "thp03";
53int TST_TOTAL = 1;
54
Wanlong Gaode5332d2012-11-05 15:06:08 +080055#ifdef MADV_MERGEABLE
56
Madper Xiee209c6b2012-10-30 10:07:26 +080057static void thp_test(void);
58
59static long hugepage_size;
60static long unaligned_size;
61static long page_size;
62
63int main(int argc, char **argv)
64{
65 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020066 const char *msg;
Madper Xiee209c6b2012-10-30 10:07:26 +080067
68 msg = parse_opts(argc, argv, NULL, NULL);
69 if (msg != NULL)
70 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
71
72 setup();
73
74 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 tst_count = 0;
Madper Xiee209c6b2012-10-30 10:07:26 +080076
77 thp_test();
78 }
79 tst_resm(TPASS, "system didn't crash, pass.");
80 cleanup();
81 tst_exit();
82}
83
84static void thp_test(void)
85{
86 void *p;
87
88 p = mmap(NULL, unaligned_size, PROT_READ | PROT_WRITE,
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Madper Xiee209c6b2012-10-30 10:07:26 +080090 if (p == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +080091 tst_brkm(TBROK | TERRNO, cleanup, "mmap");
Madper Xiee209c6b2012-10-30 10:07:26 +080092
93 memset(p, 0x00, unaligned_size);
94 if (mprotect(p, unaligned_size, PROT_NONE) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 tst_brkm(TBROK | TERRNO, cleanup, "mprotect");
Cyril Hrubisf2ec5c62013-04-30 16:29:43 +020096
97 if (madvise(p + hugepage_size, page_size, MADV_MERGEABLE) == -1) {
98 if (errno == EINVAL) {
99 tst_brkm(TCONF, cleanup,
100 "MADV_MERGEABLE is not enabled/supported");
101 } else {
102 tst_brkm(TBROK | TERRNO, cleanup, "madvise");
103 }
104 }
Madper Xiee209c6b2012-10-30 10:07:26 +0800105
106 switch (fork()) {
107 case -1:
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_brkm(TBROK | TERRNO, cleanup, "fork");
Madper Xiee209c6b2012-10-30 10:07:26 +0800109 case 0:
110 exit(0);
111 default:
112 if (waitpid(-1, NULL, 0) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
Madper Xiee209c6b2012-10-30 10:07:26 +0800114 }
115}
116
117void setup(void)
118{
119 hugepage_size = read_meminfo("Hugepagesize:") * KB;
120 unaligned_size = hugepage_size * 4 - 1;
121 page_size = SAFE_SYSCONF(NULL, _SC_PAGESIZE);
122
123 tst_sig(FORK, DEF_HANDLER, cleanup);
124 TEST_PAUSE;
125}
126
127void cleanup(void)
128{
Madper Xiee209c6b2012-10-30 10:07:26 +0800129}
Wanlong Gaode5332d2012-11-05 15:06:08 +0800130
131#else
132int main(void)
133{
134 tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 " or you need to update your glibc-headers");
Wanlong Gaode5332d2012-11-05 15:06:08 +0800136}
137#endif