Debian Codegraph @

tests/chunk-overcommit.c

raw symbols SCIP
1 /*
2 * Test rationale:
3 *
4 * Some kernel versions after hugepage demand allocation was added
5 * used a dubious heuristic to check if there was enough hugepage
6 * space available for a given mapping. The number of
7 * not-already-instantiated pages in the mapping was compared against
8 * the total hugepage free pool. It was very easy to confuse this
9 * heuristic into overcommitting by allocating hugepage memory in
10 * chunks, each less than the total available pool size but together
11 * more than available. This would generally lead to OOM SIGKILLs of
12 * one process or another when it tried to instantiate pages beyond
13 * the available pool.
14 *
15 *
16 * libhugetlbfs - Easy use of Linux hugepages
17 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
18 *
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public License
21 * as published by the Free Software Foundation; either version 2.1 of
22 * the License, or (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 */
33
34 #define _GNU_SOURCE
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <unistd.h>
41 #include <sys/mman.h>
42 #include <sys/wait.h>
43 #include <signal.h>
44
45 #include <hugetlbfs.h>
46
47 #include "hugetests.h"
48
49 #define RANDOM_CONSTANT 0x1234ABCD
50
51 int main(int argc, char *argv[])
52 {
53 long hpage_size;
54 unsigned long totpages, chunk1, chunk2;
55 int fd;
56 void *p, *q;
57 pid_t child, ret;
58 int status;
59
60 test_init(argc, argv);
61
62 hpage_size = check_hugepagesize();
63 totpages = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
64
65 fd = hugetlbfs_unlinked_fd();
66 if (fd < 0)
67 FAIL("hugetlbfs_unlinked_fd()");
68
69 chunk1 = (totpages / 2) + 1;
70 chunk2 = totpages - chunk1 + 1;
71
72 verbose_printf("overcommit: %ld hugepages available: "
73 "chunk1=%ld chunk2=%ld\n", totpages, chunk1, chunk2);
74
75 p = mmap(NULL, chunk1*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
76 fd, 0);
77 if (p == MAP_FAILED)
78 FAIL("mmap() chunk1: %s", strerror(errno));
79
80 q = mmap(NULL, chunk2*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
81 fd, chunk1*hpage_size);
82 if (q == MAP_FAILED) {
83 if (errno != ENOMEM)
84 FAIL("mmap() chunk2: %s", strerror(errno));
85 else
86 PASS();
87 }
88
89 verbose_printf("Looks like we've overcommitted, testing...\n");
90
91 /* Looks like we're overcommited, but we need to confirm that
92 * this is bad. We touch it all in a child process because an
93 * overcommit will generally lead to a SIGKILL which we can't
94 * handle, of course. */
95 child = fork();
96 if (child < 0)
97 FAIL("fork(): %s", strerror(errno));
98
99 if (child == 0) {
100 memset(p, 0, chunk1*hpage_size);
101 memset(q, 0, chunk2*hpage_size);
102 exit(0);
103 }
104
105 ret = waitpid(child, &status, 0);
106 if (ret < 0)
107 FAIL("waitpid(): %s", strerror(errno));
108
109 if (WIFSIGNALED(status))
110 FAIL("Killed by signal \"%s\" due to overcommit",
111 strsignal(WTERMSIG(status)));
112
113 PASS();
114 }