| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <stdlib.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <sys/wait.h> |
| 26 |
#include <hugetlbfs.h> |
| 27 |
#include "hugetests.h" |
| 28 |
|
| 29 |
long oc_pool = -1; |
| 30 |
long hpagesize; |
| 31 |
|
| 32 |
void cleanup(void) |
| 33 |
{ |
| 34 |
if (oc_pool > 0) |
| 35 |
restore_overcommit_pages(hpagesize, oc_pool); |
| 36 |
} |
| 37 |
|
| 38 |
int main(int argc, char **argv) |
| 39 |
{ |
| 40 |
int freepages; |
| 41 |
long size1, size2; |
| 42 |
void *p1, *p2; |
| 43 |
int st, pid, rv; |
| 44 |
unsigned long long mapping_size; |
| 45 |
|
| 46 |
test_init(argc, argv); |
| 47 |
|
| 48 |
if (!getenv("HUGETLB_MORECORE")) |
| 49 |
CONFIG("Must have HUGETLB_MORECORE=yes"); |
| 50 |
|
| 51 |
hpagesize = check_hugepagesize(); |
| 52 |
|
| 53 |
|
| 54 |
check_must_be_root(); |
| 55 |
|
| 56 |
oc_pool = read_nr_overcommit(hpagesize); |
| 57 |
if (oc_pool > 0) |
| 58 |
set_nr_overcommit_hugepages(hpagesize, 0); |
| 59 |
|
| 60 |
freepages = get_huge_page_counter(hpagesize, HUGEPAGES_FREE); |
| 61 |
if (freepages < 3) |
| 62 |
CONFIG("Must have at least 3 free hugepages"); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
size1 = hpagesize; |
| 69 |
p1 = malloc(size1); |
| 70 |
if (!p1) |
| 71 |
FAIL("Couldn't malloc %ld bytes", size1); |
| 72 |
mapping_size = get_mapping_page_size(p1); |
| 73 |
if (mapping_size != hpagesize) |
| 74 |
FAIL("First allocation %p not on hugepages", p1); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
size2 = freepages * hpagesize; |
| 80 |
p2 = malloc(size2); |
| 81 |
if (!p2) |
| 82 |
FAIL("Couldn't malloc %ld bytes", size2); |
| 83 |
mapping_size = get_mapping_page_size(p1); |
| 84 |
st = (mapping_size == hpagesize); |
| 85 |
verbose_printf("Second allocation %p huge? %s\n", p2, st < 0 ? "??" : |
| 86 |
(st ? "yes" : "no")); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pid = fork(); |
| 93 |
if (pid < 0) |
| 94 |
FAIL("fork: %s", strerror(errno)); |
| 95 |
|
| 96 |
if (pid == 0) { |
| 97 |
memset(p1, 0, size1); |
| 98 |
memset(p2, 0, size2); |
| 99 |
exit(0); |
| 100 |
} |
| 101 |
|
| 102 |
rv = waitpid(pid, &st, 0); |
| 103 |
if (rv < 0) |
| 104 |
FAIL("waitpid: %s\n", strerror(errno)); |
| 105 |
if (WIFSIGNALED(st)) |
| 106 |
FAIL("Child killed by signal %d touching malloc'ed memory", |
| 107 |
WTERMSIG(st)); |
| 108 |
|
| 109 |
PASS(); |
| 110 |
} |