1#!/bin/sh 2 3# 4# Copyright (c) 2015 EMC Corp. 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28 29# mmap() & read()/write() on same file. 30# Test scenario suggestion by: jeff@ 31 32# Out of VM deadlock seen: 33# https://people.freebsd.org/~pho/stress/log/jeff114.txt 34 35# panic: deadlkres: possible deadlock detected: 36# https://people.freebsd.org/~pho/stress/log/jeff115.txt 37 38. ../default.cfg 39[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 40 41dir=/tmp 42odir=`pwd` 43cd $dir 44ps=`sysctl -n hw.pagesize` 45sed "1,/^EOF/d;s/\$ps/$ps/" < $odir/$0 > $dir/bio.c 46mycc -o bio -Wall -Wextra -O0 -g bio.c || exit 1 47rm -f bio.c 48cd $odir 49 50mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint 51[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart 52mdconfig -a -t swap -s 2g -u $mdstart || exit 1 53newfs -n md$mdstart > /dev/null 54mount /dev/md$mdstart $mntpoint 55 56(cd $mntpoint; /tmp/bio) & 57pid1=$! 58sleep 5 59(cd ../testcases/swap; ./swap -t 5m -i 20 -k -l 100 -h) & 60pid2=$! 61 62while pgrep -q bio; do 63 sleep 2 64done 65 66while pgrep -q swap; do 67 pkill -9 swap 68done 69wait $pid2 70wait $pid1 71s=$? 72 73while mount | grep "on $mntpoint " | grep -q /dev/md; do 74 umount $mntpoint || sleep 1 75done 76mdconfig -d -u $mdstart 77rm -rf /tmp/bio 78exit $s 79 80EOF 81#include <sys/param.h> 82#include <sys/mman.h> 83#include <sys/wait.h> 84 85#include <machine/atomic.h> 86 87#include <err.h> 88#include <errno.h> 89#include <fcntl.h> 90#include <sched.h> 91#include <stdio.h> 92#include <stdlib.h> 93#include <time.h> 94#include <unistd.h> 95 96#define PS $ps /* From hw.pagesize */ 97#define SYN1 0 98#define SYN2 1 99#define INDX 2 100 101#define PAGES (512 * 1024 * 1024 / PS) /* 512MB file size */ 102#define PARALLEL 3 103#define RUNTIME (5 * 60) 104#define TIMEOUT (30 * 60) 105 106char buf[PS]; 107 108void 109test(int inx) 110{ 111 pid_t pid; 112 size_t i, len, slen; 113 time_t start; 114 volatile u_int *share; 115 int fd, r; 116 u_int *ip, val; 117 char file[80]; 118 119 slen = PS; 120 if ((share = mmap(NULL, slen, PROT_READ | PROT_WRITE, MAP_ANON | 121 MAP_SHARED, -1, 0)) == MAP_FAILED) 122 err(1, "mmap"); 123 124 snprintf(file, sizeof(file), "file.%06d", inx); 125 if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) < 0) 126 err(1, "%s", file); 127 128 for (i = 0; i < PAGES; i++) { 129 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) 130 err(1, "write error"); 131 } 132 133 len = PS * PAGES * sizeof(u_int); 134 if ((ip = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, 135 fd, 0)) == MAP_FAILED) 136 err(1, "mmap"); 137 138 start = time(NULL); 139 if ((pid = fork()) == 0) { 140 alarm(2 * RUNTIME); 141 /* mmap read / write access */ 142 for (i = 0; i < (size_t)(PAGES * PS); i += PS) { 143 while (share[SYN1] == 0) 144 sched_yield(); 145 atomic_add_int(&share[SYN1], -1); 146 if (ip[share[INDX] / sizeof(u_int)] != share[INDX]) 147 warn("child expected %d, but got %d\n", 148 ip[share[INDX] / sizeof(u_int)], 149 share[INDX]); 150 share[INDX] += PS; 151 ip[share[INDX] / sizeof(u_int)] = share[INDX]; 152 atomic_add_int(&share[SYN2], 1); /* signal parent */ 153 if (i % 1000 == 0 && time(NULL) - start > TIMEOUT) 154 errx(1, "Timed out");; 155 } 156 _exit(0); 157 } 158 if (pid == -1) 159 err(1, "fork()"); 160 share[INDX] = 0; 161 atomic_add_int(&share[SYN2], 1); 162 alarm(2 * RUNTIME); 163 for (i = 0; i < (size_t)(PAGES * PS); i += PS) { 164 while (share[SYN2] == 0) 165 sched_yield(); 166 atomic_add_int(&share[SYN2], -1); 167 if (lseek(fd, share[INDX], SEEK_SET) == -1) 168 err(1, "lseek error"); 169 if ((r = read(fd, &val, sizeof(val))) != sizeof(val)) 170 err(1, "parent read read %d bytes", r); 171 if (val != share[INDX]) 172 warn("parent expected %d, but got %d\n", 173 share[INDX], val); 174 val += PS; 175 if (lseek(fd, val, SEEK_SET) == -1) 176 err(1, "lseek error"); 177 if (write(fd, &val, sizeof(val)) != sizeof(val)) 178 err(1, "write"); 179 180 atomic_add_int(&share[SYN1], 1); /* signal child */ 181 if (i % 1000 == 0 && time(NULL) - start > TIMEOUT) 182 errx(1, "Timed out");; 183 } 184 atomic_add_int(&share[SYN2], -1); 185 if (waitpid(pid, NULL, 0) != pid) 186 err(1, "wait"); 187 188 if (munmap(ip, len) == -1) 189 err(1, "unmap()"); 190 if (munmap((void *)share, slen) == -1) 191 err(1, "unmap()"); 192 close(fd); 193 if (unlink(file) == -1) 194 err(1, "unlink(%s)", file); 195 196 _exit(0); 197} 198 199int 200main(void) 201{ 202 pid_t pids[PARALLEL]; 203 time_t start; 204 int i, s, status; 205 206 start = time(NULL); 207 s = 0; 208 while ((time(NULL) - start) < RUNTIME && s == 0) { 209 for (i = 0; i < PARALLEL; i++) { 210 if ((pids[i] = fork()) == 0) 211 test(i); 212 } 213 for (i = 0; i < PARALLEL; i++) { 214 if (waitpid(pids[i], &status, 0) == -1) 215 err(1, "waitpid(%d)", pids[i]); 216 if (status != 0) 217 fprintf(stderr, "Child exit status = %d\n", 218 status); 219 s += status == 0 ? 0 : 1; 220 } 221 } 222 223 return (s); 224} 225