| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <errno.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <signal.h> |
| 25 |
#include <sys/mman.h> |
| 26 |
#include <setjmp.h> |
| 27 |
|
| 28 |
#include <hugetlbfs.h> |
| 29 |
|
| 30 |
#include "hugetests.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#define RANDOM_CONSTANT 0x1234ABCD |
| 45 |
|
| 46 |
static sigjmp_buf sig_escape; |
| 47 |
|
| 48 |
static void sigbus_handler(int signum, siginfo_t *si, void *uc) |
| 49 |
{ |
| 50 |
siglongjmp(sig_escape, 17); |
| 51 |
} |
| 52 |
|
| 53 |
int main(int argc, char *argv[]) |
| 54 |
{ |
| 55 |
long hpage_size; |
| 56 |
int fd; |
| 57 |
void *p; |
| 58 |
volatile unsigned int *q; |
| 59 |
int err; |
| 60 |
int sigbus_count = 0; |
| 61 |
unsigned long initial_rsvd, rsvd; |
| 62 |
struct sigaction sa = { |
| 63 |
.sa_sigaction = sigbus_handler, |
| 64 |
.sa_flags = SA_SIGINFO, |
| 65 |
}; |
| 66 |
|
| 67 |
test_init(argc, argv); |
| 68 |
|
| 69 |
hpage_size = check_hugepagesize(); |
| 70 |
|
| 71 |
fd = hugetlbfs_unlinked_fd(); |
| 72 |
if (fd < 0) |
| 73 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 74 |
|
| 75 |
initial_rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD); |
| 76 |
verbose_printf("Reserve count before map: %lu\n", initial_rsvd); |
| 77 |
|
| 78 |
p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 79 |
fd, 0); |
| 80 |
if (p == MAP_FAILED) |
| 81 |
FAIL("mmap(): %s", strerror(errno)); |
| 82 |
q = p; |
| 83 |
|
| 84 |
verbose_printf("Reserve count after map: %lu\n", |
| 85 |
get_huge_page_counter(hpage_size, HUGEPAGES_RSVD)); |
| 86 |
|
| 87 |
*q = 0; |
| 88 |
verbose_printf("Reserve count after touch: %lu\n", |
| 89 |
get_huge_page_counter(hpage_size, HUGEPAGES_RSVD)); |
| 90 |
|
| 91 |
err = ftruncate(fd, 0); |
| 92 |
if (err) |
| 93 |
FAIL("ftruncate(): %s", strerror(errno)); |
| 94 |
|
| 95 |
rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD); |
| 96 |
verbose_printf("Reserve count after truncate: %lu\n", rsvd); |
| 97 |
if (rsvd != initial_rsvd) |
| 98 |
FAIL("Reserved count is not restored after truncate: %lu instead of %lu", |
| 99 |
rsvd, initial_rsvd); |
| 100 |
|
| 101 |
err = sigaction(SIGBUS, &sa, NULL); |
| 102 |
if (err) |
| 103 |
FAIL("sigaction(): %s", strerror(errno)); |
| 104 |
|
| 105 |
if (sigsetjmp(sig_escape, 1) == 0) |
| 106 |
*q; |
| 107 |
else |
| 108 |
sigbus_count++; |
| 109 |
|
| 110 |
if (sigbus_count != 1) |
| 111 |
FAIL("Didn't SIGBUS after truncate"); |
| 112 |
|
| 113 |
rsvd = get_huge_page_counter(hpage_size, HUGEPAGES_RSVD); |
| 114 |
verbose_printf("Reserve count after SIGBUS fault: %lu\n", rsvd); |
| 115 |
if (rsvd != initial_rsvd) |
| 116 |
FAIL("Reserved count is altered by SIGBUS fault: %lu instead of %lu", |
| 117 |
rsvd, initial_rsvd); |
| 118 |
|
| 119 |
munmap(p, hpage_size); |
| 120 |
|
| 121 |
verbose_printf("Reserve count after munmap(): %lu\n", |
| 122 |
get_huge_page_counter(hpage_size, HUGEPAGES_RSVD)); |
| 123 |
|
| 124 |
close(fd); |
| 125 |
|
| 126 |
verbose_printf("Reserve count after close(): %lu\n", |
| 127 |
get_huge_page_counter(hpage_size, HUGEPAGES_RSVD)); |
| 128 |
|
| 129 |
PASS(); |
| 130 |
} |