Debian Codegraph @

tests/madvise_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 #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 #include "hugetests.h"
28
29 /*
30 * Test rationale:
31 *
32 * madvise() on some kernels can cause the reservation counter to get
33 * corrupted. The problem is that the patches are allocated for the
34 * reservation but not faulted in at the time of allocation. The
35 * counters do not get updated and effectively "leak". This test
36 * identifies whether the kernel is vunerable to the problem or not.
37 * It is fixed in kernel by commit f2deae9d4e70793568ef9e85d227abb7bef5b622
38 */
39 int main(int argc, char *argv[])
40 {
41 long hpage_size;
42 int fd;
43 void *p;
44 unsigned long initial_rsvd, map_rsvd, madvise_rsvd, end_rsvd;
45
46 test_init(argc, argv);
47
48 /* Setup */
49 hpage_size = check_hugepagesize();
50 fd = hugetlbfs_unlinked_fd();
51 if (fd < 0)
52 FAIL("hugetlbfs_unlinked_fd()");
53 initial_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
54 verbose_printf("Reserve count before map: %lu\n", initial_rsvd);
55
56 /* mmap a region and record reservations */
57 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
58 fd, 0);
59 if (p == MAP_FAILED)
60 FAIL("mmap(): %s", strerror(errno));
61 map_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
62 verbose_printf("Reserve count after map: %lu\n", map_rsvd);
63
64 /* madvise the region and record reservations */
65 if (madvise(p, hpage_size, MADV_WILLNEED) == -1)
66 FAIL("madvise(): %s", strerror(errno));
67 madvise_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
68 verbose_printf("Reserve count after madvise: %lu\n", madvise_rsvd);
69
70 /* Free region */
71 munmap(p, hpage_size);
72 close(fd);
73 end_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD);
74 verbose_printf("Reserve count after close(): %lu\n", end_rsvd);
75
76 /* Reserve count should match initial reserve count */
77 if (end_rsvd != initial_rsvd)
78 FAIL("Reserve leaked: %lu != %lu\n", end_rsvd, initial_rsvd);
79
80 PASS();
81 }