Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 1 | /* |
| 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 Gao | aae18db | 2012-11-05 15:10:39 +0800 | [diff] [blame] | 28 | * Kernel Commit id: 027ef6c87853b0a9df53175063028edb4950d476 |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 29 | * 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 Hrubis | f2ec5c6 | 2013-04-30 16:29:43 +0200 | [diff] [blame] | 47 | #include <errno.h> |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 48 | #include "mem.h" |
| 49 | #include "safe_macros.h" |
| 50 | #include "test.h" |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 51 | |
| 52 | char *TCID = "thp03"; |
| 53 | int TST_TOTAL = 1; |
| 54 | |
Wanlong Gao | de5332d | 2012-11-05 15:06:08 +0800 | [diff] [blame] | 55 | #ifdef MADV_MERGEABLE |
| 56 | |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 57 | static void thp_test(void); |
| 58 | |
| 59 | static long hugepage_size; |
| 60 | static long unaligned_size; |
| 61 | static long page_size; |
| 62 | |
| 63 | int main(int argc, char **argv) |
| 64 | { |
| 65 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 66 | const char *msg; |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 67 | |
| 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 Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 75 | tst_count = 0; |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 76 | |
| 77 | thp_test(); |
| 78 | } |
| 79 | tst_resm(TPASS, "system didn't crash, pass."); |
| 80 | cleanup(); |
| 81 | tst_exit(); |
| 82 | } |
| 83 | |
| 84 | static void thp_test(void) |
| 85 | { |
| 86 | void *p; |
| 87 | |
| 88 | p = mmap(NULL, unaligned_size, PROT_READ | PROT_WRITE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 90 | if (p == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 91 | tst_brkm(TBROK | TERRNO, cleanup, "mmap"); |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 92 | |
| 93 | memset(p, 0x00, unaligned_size); |
| 94 | if (mprotect(p, unaligned_size, PROT_NONE) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | tst_brkm(TBROK | TERRNO, cleanup, "mprotect"); |
Cyril Hrubis | f2ec5c6 | 2013-04-30 16:29:43 +0200 | [diff] [blame] | 96 | |
| 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 Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 105 | |
| 106 | switch (fork()) { |
| 107 | case -1: |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | tst_brkm(TBROK | TERRNO, cleanup, "fork"); |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 109 | case 0: |
| 110 | exit(0); |
| 111 | default: |
| 112 | if (waitpid(-1, NULL, 0) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | tst_brkm(TBROK | TERRNO, cleanup, "waitpid"); |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void 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 | |
| 127 | void cleanup(void) |
| 128 | { |
Madper Xie | e209c6b | 2012-10-30 10:07:26 +0800 | [diff] [blame] | 129 | } |
Wanlong Gao | de5332d | 2012-11-05 15:06:08 +0800 | [diff] [blame] | 130 | |
| 131 | #else |
| 132 | int main(void) |
| 133 | { |
| 134 | tst_brkm(TCONF, NULL, "Kernel doesn't support MADV_MERGEABLE" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | " or you need to update your glibc-headers"); |
Wanlong Gao | de5332d | 2012-11-05 15:06:08 +0800 | [diff] [blame] | 136 | } |
| 137 | #endif |