| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define _GNU_SOURCE |
| 20 |
|
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/shm.h> |
| 23 |
#include <sys/wait.h> |
| 24 |
#include <sys/mman.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <sys/stat.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <hugetlbfs.h> |
| 34 |
#include "hugetests.h" |
| 35 |
|
| 36 |
extern int errno; |
| 37 |
|
| 38 |
#define P "mmap-cow" |
| 39 |
#define DESC \ |
| 40 |
"* Tests copy-on-write semantics of large pages where a number *\n"\ |
| 41 |
"* of threads map the same file with the MAP_PRIVATE flag. The *\n"\ |
| 42 |
"* threads then write into their copy of the mapping and recheck *\n"\ |
| 43 |
"* the contents to ensure they were not corrupted by the other *\n"\ |
| 44 |
"* threads. *"\ |
| 45 |
|
| 46 |
#define HTLB_FILE "mmap-cow" |
| 47 |
#define BUF_SZ 256 |
| 48 |
|
| 49 |
#define CHILD_FAIL(thread, fmt, ...) \ |
| 50 |
do { \ |
| 51 |
verbose_printf("Thread %d (pid=%d) FAIL: " fmt, \ |
| 52 |
thread, getpid(), __VA_ARGS__); \ |
| 53 |
exit(1); \ |
| 54 |
} while (0) |
| 55 |
|
| 56 |
|
| 57 |
static int nr_hugepages; |
| 58 |
static unsigned int threads; |
| 59 |
|
| 60 |
static int mmap_file(int fd, char **addr, size_t size, int type) |
| 61 |
{ |
| 62 |
int flags = 0; |
| 63 |
|
| 64 |
*addr = mmap(NULL, size, PROT_READ|PROT_WRITE, flags | type, fd, 0); |
| 65 |
if (*addr == MAP_FAILED) |
| 66 |
return -1; |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
static void do_work(int thread, size_t size, int fd) |
| 72 |
{ |
| 73 |
char *addr; |
| 74 |
size_t i; |
| 75 |
char pattern = thread+65; |
| 76 |
|
| 77 |
if (mmap_file(fd, &addr, size, MAP_PRIVATE)) |
| 78 |
CHILD_FAIL(thread, "mmap() failed: %s", strerror(errno)); |
| 79 |
|
| 80 |
verbose_printf("Thread %d (pid=%d): Mapped at address %p\n", |
| 81 |
thread, getpid(), addr); |
| 82 |
|
| 83 |
|
| 84 |
verbose_printf("Thread %d (pid=%d): Writing %c to the mapping\n", |
| 85 |
thread, getpid(), pattern); |
| 86 |
for (i = 0; i < size; i++) |
| 87 |
memcpy((char *)addr+i, &pattern, 1); |
| 88 |
|
| 89 |
if (msync(addr, size, MS_SYNC)) |
| 90 |
CHILD_FAIL(thread, "msync() failed: %s", strerror(errno)); |
| 91 |
|
| 92 |
|
| 93 |
for (i = 0; i < size; i++) |
| 94 |
if (addr[i] != pattern) |
| 95 |
CHILD_FAIL(thread, "Corruption at %p; " |
| 96 |
"Got %c, Expected %c", |
| 97 |
&addr[i], addr[i], pattern); |
| 98 |
|
| 99 |
verbose_printf("Thread %d (pid=%d): Pattern verified\n", |
| 100 |
thread, getpid()); |
| 101 |
|
| 102 |
|
| 103 |
munmap(addr, size); |
| 104 |
close(fd); |
| 105 |
exit(0); |
| 106 |
} |
| 107 |
|
| 108 |
int main(int argc, char ** argv) |
| 109 |
{ |
| 110 |
char *addr; |
| 111 |
long hpage_size; |
| 112 |
size_t size; |
| 113 |
int i, pid, status, fd, ret; |
| 114 |
pid_t *wait_list; |
| 115 |
|
| 116 |
test_init(argc, argv); |
| 117 |
|
| 118 |
if (argc < 3) |
| 119 |
CONFIG("Usage: mmap-cow <# threads> <# pages>\n"); |
| 120 |
|
| 121 |
nr_hugepages = atoi(argv[2]); |
| 122 |
threads = atoi(argv[1]); |
| 123 |
|
| 124 |
if ((threads+1) > nr_hugepages) |
| 125 |
CONFIG("Need more hugepages than threads\n"); |
| 126 |
|
| 127 |
wait_list = malloc(threads * sizeof(pid_t)); |
| 128 |
if (wait_list == NULL) |
| 129 |
CONFIG("Couldn't allocate memory for wait_list\n"); |
| 130 |
|
| 131 |
hpage_size = check_hugepagesize(); |
| 132 |
|
| 133 |
|
| 134 |
size = (nr_hugepages / (threads+1)) * hpage_size; |
| 135 |
verbose_printf("hpage_size is %lx, Size is %zu, threads: %u\n", |
| 136 |
hpage_size, size, threads); |
| 137 |
|
| 138 |
|
| 139 |
fd = hugetlbfs_unlinked_fd(); |
| 140 |
if (fd < 0) |
| 141 |
CONFIG("hugetlbfs_unlinked_fd() failed: %s\n", |
| 142 |
strerror(errno)); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (mmap_file(fd, &addr, size, MAP_SHARED)) |
| 150 |
FAIL("Failed to create shared mapping: %s", strerror(errno)); |
| 151 |
|
| 152 |
for (i = 0; i < size; i += 8) { |
| 153 |
memcpy(addr+i, "deadbeef", 8); |
| 154 |
} |
| 155 |
|
| 156 |
for (i=0; i<threads; i++) { |
| 157 |
if ((pid = fork()) < 0) |
| 158 |
FAIL("fork: %s", strerror(errno)); |
| 159 |
|
| 160 |
if (pid == 0) |
| 161 |
do_work(i, size, fd); |
| 162 |
|
| 163 |
wait_list[i] = pid; |
| 164 |
} |
| 165 |
for (i=0; i<threads; i++) { |
| 166 |
ret = waitpid(wait_list[i], &status, 0); |
| 167 |
if (ret < 0) |
| 168 |
FAIL("waitpid(): %s", strerror(errno)); |
| 169 |
if (WEXITSTATUS(status) != 0) |
| 170 |
FAIL("Thread %d (pid=%d) failed", i, wait_list[i]); |
| 171 |
|
| 172 |
if (WIFSIGNALED(status)) |
| 173 |
FAIL("Thread %d (pid=%d) received unhandled signal", i, |
| 174 |
wait_list[i]); |
| 175 |
} |
| 176 |
|
| 177 |
munmap(addr, size); |
| 178 |
close(fd); |
| 179 |
free(wait_list); |
| 180 |
|
| 181 |
PASS(); |
| 182 |
} |