Debian Codegraph @

tests/brk_near_huge.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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25
26 #include <hugetlbfs.h>
27
28 #include "hugetests.h"
29
30 /*
31 * Test rationale:
32 *
33 * Certain kernels have a bug where brk() does not perform the same
34 * checks that a MAP_FIXED mmap() will, allowing brk() to create a
35 * normal page VMA in a hugepage only address region. This can lead
36 * to oopses or other badness.
37 */
38
39 /* Possibly these functions should go in the library itself.. */
40 #ifdef __powerpc64__
41 void *next_chunk(void *addr)
42 {
43 if (!arch_has_slice_support())
44 return PALIGN(addr, gethugepagesize());
45
46 if ((unsigned long)addr < 0x100000000UL)
47 /* 256M segments below 4G */
48 return PALIGN(addr, 0x10000000UL);
49 else
50 /* 1TB segments above */
51 return PALIGN(addr, 0x10000000000UL);
52 }
53 #elif defined(__powerpc__) && !defined(PPC_NO_SEGMENTS)
54 void *next_chunk(void *addr)
55 {
56 return PALIGN(addr, 0x10000000UL);
57 }
58 #elif defined(__ia64__)
59 void *next_chunk(void *addr)
60 {
61 return PALIGN(addr, 0x8000000000000000UL);
62 }
63 #else
64 void *next_chunk(void *addr)
65 {
66 return PALIGN(addr, gethugepagesize());
67 }
68 #endif
69
70 int main(int argc, char *argv[])
71 {
72 long hpage_size;
73 int fd;
74 void *brk0, *hugemap_addr, *newbrk;
75 char *p;
76 int err;
77
78 test_init(argc, argv);
79
80 hpage_size = check_hugepagesize();
81
82 fd = hugetlbfs_unlinked_fd();
83 if (fd < 0)
84 FAIL("hugetlbfs_unlinked_fd()");
85
86 brk0 = sbrk(0);
87 verbose_printf("Initial break at %p\n", brk0);
88
89 hugemap_addr = next_chunk(brk0) + hpage_size;
90
91 p = mmap(hugemap_addr, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED,
92 fd, 0);
93 if (p == MAP_FAILED)
94 FAIL("mmap(): %s", strerror(errno));
95 if (p != hugemap_addr)
96 FAIL("mmap() at unexpected address %p instead of %p\n", p,
97 hugemap_addr);
98
99 verbose_printf("Hugepage mapped at %p-%p\n", p, p+hpage_size-1);
100
101 err = test_addr_huge((void *)p);
102 if (err != 1)
103 FAIL("Mapped address is not hugepage");
104
105 newbrk = next_chunk(brk0) + getpagesize();
106 err = brk((void *)newbrk);
107 if (err == -1)
108 /* Failing the brk() is an acceptable kernel response */
109 PASS();
110
111 /* Suceeding the brk() is acceptable iff the new memory is
112 * properly accesible and we don't have a kernel blow up when
113 * we touch it. */
114 memset(brk0, 0, newbrk-brk0);
115
116 PASS();
117 }