| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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 |
} |