Debian Codegraph @

tests/map_high_truncate_2.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #define _LARGEFILE64_SOURCE
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <sys/mman.h>
28
29 #include <hugetlbfs.h>
30
31 #include "hugetests.h"
32
33 /*
34 * Test rationale:
35 *
36 * At one stage, a misconversion of hugetlb_vmtruncate_list to a
37 * prio_tree meant that on 32-bit machines, certain combinations of
38 * mapping and truncations could truncate incorrect pages, or
39 * overwrite pmds from other VMAs, triggering BUG_ON()s or other
40 * wierdness.
41 *
42 * Test adapted to the libhugetlbfs framework from an example by
43 * Kenneth Chen <[email protected]>
44 *
45 * WARNING: The offsets and addresses used within are specifically
46 * calculated to trigger the bug as it existed. Don't mess with them
47 * unless you *really* know what you're doing.
48 *
49 * The kernel bug in question was fixed with commit
50 * 856fc29505556cf263f3dcda2533cf3766c14ab6.
51 */
52 #define MAP_LENGTH (4 * hpage_size)
53 #if defined(__s390__) && __WORDSIZE == 32
54 #define TRUNCATE_POINT 0x20000000UL
55 #else
56 #define TRUNCATE_POINT 0x60000000UL
57 #endif
58 #define HIGH_ADDR 0xa0000000UL
59 #define FOURGIG ((off64_t)0x100000000ULL)
60
61 int main(int argc, char *argv[])
62 {
63 long hpage_size;
64 int fd;
65 char *p, *q;
66 unsigned long i;
67 int err;
68
69 test_init(argc, argv);
70
71 hpage_size = check_hugepagesize();
72
73 if (hpage_size > TRUNCATE_POINT)
74 CONFIG("Huge page size is too large");
75
76 if (TRUNCATE_POINT % hpage_size)
77 CONFIG("Truncation point is not aligned to huge page size");
78
79 check_free_huge_pages(4);
80
81 fd = hugetlbfs_unlinked_fd();
82 if (fd < 0)
83 FAIL("hugetlbfs_unlinked_fd()");
84
85 /* First mapping */
86 p = mmap(0, MAP_LENGTH + TRUNCATE_POINT, PROT_READ | PROT_WRITE,
87 MAP_PRIVATE | MAP_NORESERVE, fd, 0);
88 if (p == MAP_FAILED)
89 FAIL("mmap() 1: %s", strerror(errno));
90
91 munmap(p, 4*hpage_size + TRUNCATE_POINT);
92
93 q = mmap((void *)HIGH_ADDR, MAP_LENGTH, PROT_READ | PROT_WRITE,
94 MAP_PRIVATE, fd, 0);
95 if (q == MAP_FAILED)
96 FAIL("mmap() 2: %s", strerror(errno));
97
98 verbose_printf("High map at %p\n", q);
99
100 for (i = 0; i < MAP_LENGTH; i += hpage_size)
101 q[i] = 1;
102
103 err = ftruncate(fd, TRUNCATE_POINT);
104 if (err != 0)
105 FAIL("ftruncate(): %s", strerror(errno));
106
107 if (q[0] != 1)
108 FAIL("data mismatch");
109
110 PASS();
111 }