| 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 |
#define _GNU_SOURCE |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <errno.h> |
| 39 |
#include <unistd.h> |
| 40 |
#include <sys/mman.h> |
| 41 |
#include <sys/types.h> |
| 42 |
#include <sched.h> |
| 43 |
#include <signal.h> |
| 44 |
#include <sys/wait.h> |
| 45 |
#include <pthread.h> |
| 46 |
#include <linux/unistd.h> |
| 47 |
|
| 48 |
#include <hugetlbfs.h> |
| 49 |
|
| 50 |
#include "hugetests.h" |
| 51 |
|
| 52 |
pid_t gettid(void) |
| 53 |
{ |
| 54 |
return syscall(__NR_gettid); |
| 55 |
} |
| 56 |
|
| 57 |
static long hpage_size; |
| 58 |
static pid_t child1, child2; |
| 59 |
static pthread_t thread1, thread2; |
| 60 |
|
| 61 |
void cleanup(void) |
| 62 |
{ |
| 63 |
if (child1) |
| 64 |
kill(child1, SIGKILL); |
| 65 |
if (child2) |
| 66 |
kill(child2, SIGKILL); |
| 67 |
} |
| 68 |
|
| 69 |
static int one_racer(void *p, int cpu, |
| 70 |
volatile int *mytrigger, volatile int *othertrigger) |
| 71 |
{ |
| 72 |
volatile int *pi = p; |
| 73 |
cpu_set_t cpuset; |
| 74 |
int err; |
| 75 |
|
| 76 |
|
| 77 |
CPU_ZERO(&cpuset); |
| 78 |
CPU_SET(cpu, &cpuset); |
| 79 |
|
| 80 |
err = sched_setaffinity(gettid(), CPU_SETSIZE/8, &cpuset); |
| 81 |
if (err != 0) |
| 82 |
CONFIG("sched_setaffinity(cpu%d): %s", cpu, strerror(errno)); |
| 83 |
|
| 84 |
|
| 85 |
*mytrigger = 1; |
| 86 |
|
| 87 |
while (! *othertrigger) |
| 88 |
; |
| 89 |
|
| 90 |
|
| 91 |
*pi = 1; |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
static void proc_racer(void *p, int cpu, |
| 97 |
volatile int *mytrigger, volatile int *othertrigger) |
| 98 |
{ |
| 99 |
exit(one_racer(p, cpu, mytrigger, othertrigger)); |
| 100 |
} |
| 101 |
|
| 102 |
struct racer_info { |
| 103 |
void *p; |
| 104 |
int cpu; |
| 105 |
int race_type; |
| 106 |
volatile int *mytrigger; |
| 107 |
volatile int *othertrigger; |
| 108 |
int status; |
| 109 |
}; |
| 110 |
|
| 111 |
static void *thread_racer(void *info) |
| 112 |
{ |
| 113 |
struct racer_info *ri = info; |
| 114 |
|
| 115 |
one_racer(ri->p, ri->cpu, ri->mytrigger, ri->othertrigger); |
| 116 |
return ri; |
| 117 |
} |
| 118 |
static void run_race(void *syncarea, int race_type) |
| 119 |
{ |
| 120 |
volatile int *trigger1, *trigger2; |
| 121 |
int fd; |
| 122 |
void *p; |
| 123 |
int status1, status2; |
| 124 |
int online_cpus[2], ret; |
| 125 |
|
| 126 |
check_online_cpus(online_cpus, 2); |
| 127 |
|
| 128 |
memset(syncarea, 0, sizeof(*trigger1) + sizeof(*trigger2)); |
| 129 |
trigger1 = syncarea; |
| 130 |
trigger2 = trigger1 + 1; |
| 131 |
|
| 132 |
|
| 133 |
fd = hugetlbfs_unlinked_fd(); |
| 134 |
if (fd < 0) |
| 135 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 136 |
|
| 137 |
verbose_printf("Mapping final page.. "); |
| 138 |
p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, race_type, fd, 0); |
| 139 |
if (p == MAP_FAILED) |
| 140 |
FAIL("mmap(): %s", strerror(errno)); |
| 141 |
verbose_printf("%p\n", p); |
| 142 |
|
| 143 |
if (race_type == MAP_SHARED) { |
| 144 |
child1 = fork(); |
| 145 |
if (child1 < 0) |
| 146 |
FAIL("fork(): %s", strerror(errno)); |
| 147 |
if (child1 == 0) |
| 148 |
proc_racer(p, online_cpus[0], trigger1, trigger2); |
| 149 |
|
| 150 |
child2 = fork(); |
| 151 |
if (child2 < 0) |
| 152 |
FAIL("fork(): %s", strerror(errno)); |
| 153 |
if (child2 == 0) |
| 154 |
proc_racer(p, online_cpus[1], trigger2, trigger1); |
| 155 |
|
| 156 |
|
| 157 |
ret = waitpid(child1, &status1, 0); |
| 158 |
if (ret < 0) |
| 159 |
FAIL("waitpid() child 1: %s", strerror(errno)); |
| 160 |
verbose_printf("Child 1 status: %x\n", status1); |
| 161 |
|
| 162 |
|
| 163 |
ret = waitpid(child2, &status2, 0); |
| 164 |
if (ret < 0) |
| 165 |
FAIL("waitpid() child 2: %s", strerror(errno)); |
| 166 |
verbose_printf("Child 2 status: %x\n", status2); |
| 167 |
|
| 168 |
if (WIFSIGNALED(status1)) |
| 169 |
FAIL("Child 1 killed by signal %s", |
| 170 |
strsignal(WTERMSIG(status1))); |
| 171 |
if (WIFSIGNALED(status2)) |
| 172 |
FAIL("Child 2 killed by signal %s", |
| 173 |
strsignal(WTERMSIG(status2))); |
| 174 |
|
| 175 |
status1 = WEXITSTATUS(status1); |
| 176 |
status2 = WEXITSTATUS(status2); |
| 177 |
} else { |
| 178 |
struct racer_info ri1 = { |
| 179 |
.p = p, |
| 180 |
.cpu = online_cpus[0], |
| 181 |
.mytrigger = trigger1, |
| 182 |
.othertrigger = trigger2, |
| 183 |
}; |
| 184 |
struct racer_info ri2 = { |
| 185 |
.p = p, |
| 186 |
.cpu = online_cpus[1], |
| 187 |
.mytrigger = trigger2, |
| 188 |
.othertrigger = trigger1, |
| 189 |
}; |
| 190 |
void *tret1, *tret2; |
| 191 |
|
| 192 |
ret = pthread_create(&thread1, NULL, thread_racer, &ri1); |
| 193 |
if (ret != 0) |
| 194 |
FAIL("pthread_create() 1: %s\n", strerror(errno)); |
| 195 |
|
| 196 |
ret = pthread_create(&thread2, NULL, thread_racer, &ri2); |
| 197 |
if (ret != 0) |
| 198 |
FAIL("pthread_create() 2: %s\n", strerror(errno)); |
| 199 |
|
| 200 |
ret = pthread_join(thread1, &tret1); |
| 201 |
if (ret != 0) |
| 202 |
FAIL("pthread_join() 1: %s\n", strerror(errno)); |
| 203 |
if (tret1 != &ri1) |
| 204 |
FAIL("Thread 1 returned %p not %p, killed?\n", |
| 205 |
tret1, &ri1); |
| 206 |
ret = pthread_join(thread2, &tret2); |
| 207 |
if (ret != 0) |
| 208 |
FAIL("pthread_join() 2: %s\n", strerror(errno)); |
| 209 |
if (tret2 != &ri2) |
| 210 |
FAIL("Thread 2 returned %p not %p, killed?\n", |
| 211 |
tret2, &ri2); |
| 212 |
|
| 213 |
status1 = ri1.status; |
| 214 |
status2 = ri2.status; |
| 215 |
} |
| 216 |
|
| 217 |
if (status1 != 0) |
| 218 |
FAIL("Racer 1 terminated with code %d", status1); |
| 219 |
|
| 220 |
if (status2 != 0) |
| 221 |
FAIL("Racer 2 terminated with code %d", status2); |
| 222 |
} |
| 223 |
|
| 224 |
int main(int argc, char *argv[]) |
| 225 |
{ |
| 226 |
unsigned long totpages; |
| 227 |
int fd; |
| 228 |
void *p, *q; |
| 229 |
unsigned long i; |
| 230 |
int race_type; |
| 231 |
|
| 232 |
test_init(argc, argv); |
| 233 |
|
| 234 |
if (argc != 2) |
| 235 |
CONFIG("Usage: alloc-instantiate-race <private|shared>"); |
| 236 |
|
| 237 |
hpage_size = check_hugepagesize(); |
| 238 |
totpages = get_huge_page_counter(hpage_size, HUGEPAGES_FREE); |
| 239 |
|
| 240 |
if (strcmp(argv[1], "shared") == 0) { |
| 241 |
race_type = MAP_SHARED; |
| 242 |
} else if (strcmp(argv[1], "private") == 0) { |
| 243 |
race_type = MAP_PRIVATE; |
| 244 |
} else { |
| 245 |
CONFIG("Usage: alloc-instantiate-race <private|shared>"); |
| 246 |
} |
| 247 |
|
| 248 |
fd = hugetlbfs_unlinked_fd(); |
| 249 |
if (fd < 0) |
| 250 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 251 |
|
| 252 |
|
| 253 |
verbose_printf("Mapping synchronization area.."); |
| 254 |
q = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, |
| 255 |
MAP_SHARED|MAP_ANONYMOUS, -1, 0); |
| 256 |
if (q == MAP_FAILED) |
| 257 |
FAIL("mmap() sync area: %s", strerror(errno)); |
| 258 |
verbose_printf("done\n"); |
| 259 |
|
| 260 |
verbose_printf("Mapping %ld/%ld pages.. ", totpages-1, totpages); |
| 261 |
p = mmap(NULL, (totpages-1)*hpage_size, PROT_READ|PROT_WRITE, |
| 262 |
MAP_SHARED, fd, 0); |
| 263 |
if (p == MAP_FAILED) |
| 264 |
FAIL("mmap() 1: %s", strerror(errno)); |
| 265 |
|
| 266 |
|
| 267 |
verbose_printf("instantiating.. "); |
| 268 |
for (i = 0; i < (totpages - 1); i++) |
| 269 |
memset(p + (i * hpage_size), 0, sizeof(int)); |
| 270 |
verbose_printf("done\n"); |
| 271 |
|
| 272 |
run_race(q, race_type); |
| 273 |
|
| 274 |
PASS(); |
| 275 |
} |