Debian Codegraph @

tests/noresv-preserve-resv-page.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2013 Joonsoo Kim, LG Electronics.
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 <unistd.h>
22 #include <signal.h>
23 #include <setjmp.h>
24 #include <sys/mman.h>
25 #include <hugetlbfs.h>
26 #include "hugetests.h"
27
28 #define P "noresv-preserve-resv-page"
29 #define DESC \
30 "* Test to preserve a reserved page against no-reserved maping. *\n"\
31 "* If all hugepages are reserved, access to no-reserved shared *\n"\
32 "* mapping cause a process die, instead of stealing a hugepage *\n"\
33 "* which is reserved for other process *\n"
34
35 static sigjmp_buf sig_escape;
36 static void *sig_expected = MAP_FAILED;
37
38 static void sig_handler(int signum, siginfo_t *si, void *uc)
39 {
40 if (signum == SIGBUS) {
41 verbose_printf("SIGBUS at %p (sig_expected=%p)\n", si->si_addr,
42 sig_expected);
43 if (si->si_addr == sig_expected) {
44 siglongjmp(sig_escape, 1);
45 }
46 FAIL("SIGBUS somewhere unexpected");
47 }
48 FAIL("Unexpected signal %s", strsignal(signum));
49 }
50
51 static void test_write(void *p)
52 {
53 volatile char *pl = p;
54
55 if (sigsetjmp(sig_escape, 1)) {
56 /* We got a SIGBUS */
57 PASS();
58 }
59
60 sig_expected = p;
61 barrier();
62 *pl = 's';
63 }
64
65 int main(int argc, char *argv[])
66 {
67 long hpage_size;
68 int nr_hugepages;
69 int surp_hugepages;
70 int fd1, fd2, err;
71 char *p, *q;
72 struct sigaction sa = {
73 .sa_sigaction = sig_handler,
74 .sa_flags = SA_SIGINFO,
75 };
76
77 test_init(argc, argv);
78
79 hpage_size = check_hugepagesize();
80 nr_hugepages = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
81
82 fd1 = hugetlbfs_unlinked_fd();
83 if (fd1 < 0)
84 FAIL("hugetlbfs_unlinked_fd()");
85
86 fd2 = hugetlbfs_unlinked_fd();
87 if (fd2 < 0)
88 FAIL("hugetlbfs_unlinked_fd()");
89
90 err = sigaction(SIGBUS, &sa, NULL);
91 if (err)
92 FAIL("Can't install SIGBUS handler: %s", strerror(errno));
93
94 p = mmap(NULL, hpage_size * nr_hugepages,
95 PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
96 if (p == MAP_FAILED)
97 FAIL("mmap() 1: %s", strerror(errno));
98
99 verbose_printf("Reserve all hugepages %d\n", nr_hugepages);
100
101 q = mmap(NULL, hpage_size,
102 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd2, 0);
103 if (q == MAP_FAILED)
104 FAIL("mmap() 2: %s", strerror(errno));
105
106 verbose_printf("Write to %p to steal reserved page\n", q);
107
108 surp_hugepages = get_huge_page_counter(hpage_size, HUGEPAGES_SURP);
109 test_write(q);
110
111 /* Provisioning succeeded because of overcommit */
112 if (get_huge_page_counter(hpage_size, HUGEPAGES_SURP) ==
113 surp_hugepages + 1)
114 PASS();
115
116 FAIL("Steal reserved page");
117 }