Debian Codegraph @

tests/fadvise_reserve.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 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 _XOPEN_SOURCE 600
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27
28 #include <hugetlbfs.h>
29 #include "hugetests.h"
30
31 /*
32 * Test rationale:
33 *
34 * fadvise() on some kernels can cause the reservation counter to get
35 * corrupted. The problem is that the patches are allocated for the
36 * reservation but not faulted in at the time of allocation. The
37 * counters do not get updated and effectively "leak". This test
38 * identifies whether the kernel is vunerable to the problem or not.
39 * It's fixed in kernel by commit f2deae9d4e70793568ef9e85d227abb7bef5b622.
40 */
41 int main(int argc, char *argv[])
42 {
43 long hpage_size;
44 int fd;
45 void *p;
46 unsigned long initial_rsvd, map_rsvd, fadvise_rsvd, end_rsvd;
47
48 test_init(argc, argv);
49
50 /* Setup */
51 hpage_size = check_hugepagesize();
52 fd = hugetlbfs_unlinked_fd();
53 if (fd < 0)
54 FAIL("hugetlbfs_unlinked_fd()");
55 initial_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
56 verbose_printf("Reserve count before map: %lu\n", initial_rsvd);
57
58 /* mmap a region and record reservations */
59 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
60 fd, 0);
61 if (p == MAP_FAILED)
62 FAIL("mmap(): %s", strerror(errno));
63 map_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
64 verbose_printf("Reserve count after map: %lu\n", map_rsvd);
65
66 /* fadvise the region and record reservations */
67 if (posix_fadvise(fd, 0, hpage_size, POSIX_FADV_WILLNEED) == -1)
68 FAIL("fadvise(): %s", strerror(errno));
69 fadvise_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
70 verbose_printf("Reserve count after fadvise: %lu\n", fadvise_rsvd);
71
72 /* Write the region */
73 memset(p, 1, hpage_size);
74
75 /* Free region */
76 munmap(p, hpage_size);
77 close(fd);
78 end_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
79 verbose_printf("Reserve count after close(): %lu\n", end_rsvd);
80
81 /* Reserve count should match initial reserve count */
82 if (end_rsvd != initial_rsvd)
83 FAIL("Reserve leaked: %lu != %lu\n", end_rsvd, initial_rsvd);
84
85 PASS();
86 }