Debian Codegraph @

tests/truncate_reserve_wraparound.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 <signal.h>
25 #include <sys/mman.h>
26 #include <setjmp.h>
27
28 #include <hugetlbfs.h>
29
30 #include "hugetests.h"
31
32 /*
33 * Test rationale:
34 *
35 * At one stage, improper handling of tests against i_size could mess
36 * up accounting of reserved hugepages on certain truncate
37 * operations.
38 *
39 * This bug was fixed with a band-aid (enough to pass this test) in
40 * commit ebed4bfc8da8df5b6b0bc4a5064a949f04683509. A more complete
41 * fix still pending as of 3d4248885b9fca818e7fe6b66328e714876d36ad.
42 */
43
44 #define RANDOM_CONSTANT 0x1234ABCD
45
46 static sigjmp_buf sig_escape;
47
48 static void sigbus_handler(int signum, siginfo_t *si, void *uc)
49 {
50 siglongjmp(sig_escape, 17);
51 }
52
53 int main(int argc, char *argv[])
54 {
55 long hpage_size;
56 int fd;
57 void *p;
58 volatile unsigned int *q;
59 int err;
60 int sigbus_count = 0;
61 unsigned long initial_rsvd, rsvd;
62 struct sigaction sa = {
63 .sa_sigaction = sigbus_handler,
64 .sa_flags = SA_SIGINFO,
65 };
66
67 test_init(argc, argv);
68
69 hpage_size = check_hugepagesize();
70
71 fd = hugetlbfs_unlinked_fd();
72 if (fd < 0)
73 FAIL("hugetlbfs_unlinked_fd()");
74
75 initial_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
76 verbose_printf("Reserve count before map: %lu\n", initial_rsvd);
77
78 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
79 fd, 0);
80 if (p == MAP_FAILED)
81 FAIL("mmap(): %s", strerror(errno));
82 q = p;
83
84 verbose_printf("Reserve count after map: %lu\n",
85 get_huge_page_counter(hpage_size, HUGEPAGES_RSVD));
86
87 *q = 0;
88 verbose_printf("Reserve count after touch: %lu\n",
89 get_huge_page_counter(hpage_size, HUGEPAGES_RSVD));
90
91 err = ftruncate(fd, 0);
92 if (err)
93 FAIL("ftruncate(): %s", strerror(errno));
94
95 rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
96 verbose_printf("Reserve count after truncate: %lu\n", rsvd);
97 if (rsvd != initial_rsvd)
98 FAIL("Reserved count is not restored after truncate: %lu instead of %lu",
99 rsvd, initial_rsvd);
100
101 err = sigaction(SIGBUS, &sa, NULL);
102 if (err)
103 FAIL("sigaction(): %s", strerror(errno));
104
105 if (sigsetjmp(sig_escape, 1) == 0)
106 *q; /* Fault, triggering a SIGBUS */
107 else
108 sigbus_count++;
109
110 if (sigbus_count != 1)
111 FAIL("Didn't SIGBUS after truncate");
112
113 rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
114 verbose_printf("Reserve count after SIGBUS fault: %lu\n", rsvd);
115 if (rsvd != initial_rsvd)
116 FAIL("Reserved count is altered by SIGBUS fault: %lu instead of %lu",
117 rsvd, initial_rsvd);
118
119 munmap(p, hpage_size);
120
121 verbose_printf("Reserve count after munmap(): %lu\n",
122 get_huge_page_counter(hpage_size, HUGEPAGES_RSVD));
123
124 close(fd);
125
126 verbose_printf("Reserve count after close(): %lu\n",
127 get_huge_page_counter(hpage_size, HUGEPAGES_RSVD));
128
129 PASS();
130 }