Debian Codegraph @

tests/misaligned_offset.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 * Copyright (C) 2006 Hugh Dickins <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <signal.h>
26 #include <sys/mman.h>
27
28 #include <hugetlbfs.h>
29
30 #include "hugetests.h"
31
32 /*
33 * Test rationale:
34 *
35 * At one stage, a misconversion of hugetlb_vmtruncate_list to a
36 * prio_tree meant that on 32-bit machines, truncates at or above 4GB
37 * could truncate lower pages, resulting in BUG_ON()s.
38 *
39 * WARNING: The offsets and addresses used within are specifically
40 * calculated to trigger the bug as it existed. Don't mess with them
41 * unless you *really* know what you're doing.
42 *
43 * The kernel bug in question was fixed with commit
44 * 856fc29505556cf263f3dcda2533cf3766c14ab6.
45 */
46
47 #define RANDOM_CONSTANT 0x1234ABCD
48
49 int main(int argc, char *argv[])
50 {
51 int page_size;
52 long hpage_size;
53 off_t buggy_offset;
54 int fd;
55 void *p, *q;
56 volatile int *pi;
57 int err;
58
59 test_init(argc, argv);
60
61 page_size = getpagesize();
62 hpage_size = check_hugepagesize();
63
64 fd = hugetlbfs_unlinked_fd();
65 if (fd < 0)
66 FAIL("hugetlbfs_unlinked_fd()");
67
68 /* First, we make a 2 page sane hugepage mapping. Then we
69 * memset() it to ensure that the ptes are instantiated for
70 * it. Then we attempt to replace the second half of the map
71 * with one at a bogus offset. We leave the first page of
72 * sane mapping in place to ensure that the corresponding
73 * pud/pmd/whatever entries aren't cleaned away. It's those
74 * bad entries which can trigger bad_pud() checks if the
75 * backout path for the bogus mapping is buggy, which it was
76 * in some kernels. */
77
78 verbose_printf("Free hugepages: %lu\n",
79 get_huge_page_counter(hpage_size, HUGEPAGES_FREE));
80
81 verbose_printf("Mapping reference map...");
82 /* First get arena of three hpages size, at file offset 4GB */
83 p = mmap(NULL, 2*hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
84 if (p == MAP_FAILED)
85 FAIL("mmap() offset 4GB: %s", strerror(errno));
86 verbose_printf("%p-%p\n", p, p+2*hpage_size-1);
87
88 verbose_printf("Free hugepages: %lu\n",
89 get_huge_page_counter(hpage_size, HUGEPAGES_FREE));
90
91 /* Instantiate the pages */
92 verbose_printf("Instantiating...");
93 memset(p, 0, 2*hpage_size);
94 pi = p;
95 *pi = RANDOM_CONSTANT;
96 verbose_printf("done.\n");
97
98 verbose_printf("Free hugepages: %lu\n",
99 get_huge_page_counter(hpage_size, HUGEPAGES_FREE));
100
101 /* Toggle the permissions on the first page. This forces TLB
102 * entries (including hash page table on powerpc) to be
103 * flushed, so that the page tables must be accessed for the
104 * test further down. In the buggy case, those page tables
105 * can get thrown away by a pud_clear() */
106 err = mprotect(p, hpage_size, PROT_READ);
107 if (err)
108 FAIL("mprotect(%p, 0x%lx, PROT_READ): %s", p, hpage_size,
109 strerror(errno));
110
111 /* Replace top hpage by hpage mapping at confusing file offset */
112 buggy_offset = page_size;
113 verbose_printf("Replacing map at %p with map from offset 0x%lx...",
114 p + hpage_size, (unsigned long)buggy_offset);
115 q = mmap(p + hpage_size, hpage_size, PROT_READ|PROT_WRITE,
116 MAP_FIXED|MAP_PRIVATE, fd, buggy_offset);
117 if (q != MAP_FAILED)
118 FAIL("bogus offset mmap() succeeded at %p: %s", q, strerror(errno));
119 if (errno != EINVAL)
120 FAIL("bogus mmap() failed with \"%s\" instead of \"%s\"",
121 strerror(errno), strerror(EINVAL));
122 verbose_printf("%s\n", strerror(errno));
123
124 verbose_printf("Free hugepages: %lu\n",
125 get_huge_page_counter(hpage_size, HUGEPAGES_FREE));
126
127 if (*pi != RANDOM_CONSTANT)
128 FAIL("Pre-existing mapping clobbered: %x instead of %x",
129 *pi, RANDOM_CONSTANT);
130
131 verbose_printf("Free hugepages: %lu\n",
132 get_huge_page_counter(hpage_size, HUGEPAGES_FREE));
133
134 /* The real test is whether we got a bad_pud() or similar
135 * during the run. The check above, combined with the earlier
136 * mprotect()s to flush the TLB are supposed to catch it, but
137 * it's hard to be certain. Once bad_pud() is called
138 * behaviour can be very strange. */
139 PASS_INCONCLUSIVE();
140 }