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 53bsdlabel -w md$mdstart auto 54newfs -n md${mdstart}$part > /dev/null 55mount /dev/md${mdstart}$part $mntpoint 56 57(cd $mntpoint; /tmp/bio) & 58pid1=$! 59sleep 5 60(cd ../testcases/swap; ./swap -t 5m -i 20 -k -l 100 -h) & 61pid2=$! 62 63while pgrep -q bio; do 64 sleep 2 65done 66 67while pgrep -q swap; do 68 pkill -9 swap 69done 70wait $pid2 71wait $pid1 72s=$? 73 74while mount | grep "on $mntpoint " | grep -q /dev/md; do 75 umount $mntpoint || sleep 1 76done 77mdconfig -d -u $mdstart 78rm -rf /tmp/bio 79exit $s 80 81EOF 82#include <sys/param.h> 83#include <sys/mman.h> 84#include <sys/wait.h> 85 86#include <machine/atomic.h> 87 88#include <err.h> 89#include <errno.h> 90#include <fcntl.h> 91#include <sched.h> 92#include <stdio.h> 93#include <stdlib.h> 94#include <time.h> 95#include <unistd.h> 96 97#define PS $ps /* From hw.pagesize */ 98#define SYN1 0 99#define SYN2 1 100#define INDX 2 101 102#define PAGES (512 * 1024 * 1024 / PS) /* 512MB file size */ 103#define PARALLEL 3 104#define RUNTIME (5 * 60) 105#define TIMEOUT (30 * 60) 106 107char buf[PS]; 108 109void 110test(int inx) 111{ 112 pid_t pid; 113 size_t i, len, slen; 114 time_t start; 115 volatile u_int *share; 116 int fd, r; 117 u_int *ip, val; 118 char file[80]; 119 120 slen = PS; 121 if ((share = mmap(NULL, slen, PROT_READ | PROT_WRITE, MAP_ANON | 122 MAP_SHARED, -1, 0)) == MAP_FAILED) 123 err(1, "mmap"); 124 125 snprintf(file, sizeof(file), "file.%06d", inx); 126 if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) < 0) 127 err(1, "%s", file); 128 129 for (i = 0; i < PAGES; i++) { 130 if (write(fd, buf, sizeof(buf)) != sizeof(buf)) 131 err(1, "write error"); 132 } 133 134 len = PS * PAGES * sizeof(u_int); 135 if ((ip = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, 136 fd, 0)) == MAP_FAILED) 137 err(1, "mmap"); 138 139 start = time(NULL); 140 if ((pid = fork()) == 0) { 141 alarm(2 * RUNTIME); 142 /* mmap read / write access */ 143 for (i = 0; i < (size_t)(PAGES * PS); i += PS) { 144 while (share[SYN1] == 0) 145 sched_yield(); 146 atomic_add_int(&share[SYN1], -1); 147 if (ip[share[INDX] / sizeof(u_int)] != share[INDX]) 148 warn("child expected %d, but got %d\n", 149 ip[share[INDX] / sizeof(u_int)], 150 share[INDX]); 151 share[INDX] += PS; 152 ip[share[INDX] / sizeof(u_int)] = share[INDX]; 153 atomic_add_int(&share[SYN2], 1); /* signal parent */ 154 if (i % 1000 == 0 && time(NULL) - start > TIMEOUT) 155 errx(1, "Timed out");; 156 } 157 _exit(0); 158 } 159 if (pid == -1) 160 err(1, "fork()"); 161 share[INDX] = 0; 162 atomic_add_int(&share[SYN2], 1); 163 alarm(2 * RUNTIME); 164 for (i = 0; i < (size_t)(PAGES * PS); i += PS) { 165 while (share[SYN2] == 0) 166 sched_yield(); 167 atomic_add_int(&share[SYN2], -1); 168 if (lseek(fd, share[INDX], SEEK_SET) == -1) 169 err(1, "lseek error"); 170 if ((r = read(fd, &val, sizeof(val))) != sizeof(val)) 171 err(1, "parent read read %d bytes", r); 172 if (val != share[INDX]) 173 warn("parent expected %d, but got %d\n", 174 share[INDX], val); 175 val += PS; 176 if (lseek(fd, val, SEEK_SET) == -1) 177 err(1, "lseek error"); 178 if (write(fd, &val, sizeof(val)) != sizeof(val)) 179 err(1, "write"); 180 181 atomic_add_int(&share[SYN1], 1); /* signal child */ 182 if (i % 1000 == 0 && time(NULL) - start > TIMEOUT) 183 errx(1, "Timed out");; 184 } 185 atomic_add_int(&share[SYN2], -1); 186 if (waitpid(pid, NULL, 0) != pid) 187 err(1, "wait"); 188 189 if (munmap(ip, len) == -1) 190 err(1, "unmap()"); 191 if (munmap((void *)share, slen) == -1) 192 err(1, "unmap()"); 193 close(fd); 194 if (unlink(file) == -1) 195 err(1, "unlink(%s)", file); 196 197 _exit(0); 198} 199 200int 201main(void) 202{ 203 pid_t pids[PARALLEL]; 204 time_t start; 205 int i, s, status; 206 207 start = time(NULL); 208 s = 0; 209 while ((time(NULL) - start) < RUNTIME && s == 0) { 210 for (i = 0; i < PARALLEL; i++) { 211 if ((pids[i] = fork()) == 0) 212 test(i); 213 } 214 for (i = 0; i < PARALLEL; i++) { 215 if (waitpid(pids[i], &status, 0) == -1) 216 err(1, "waitpid(%d)", pids[i]); 217 if (status != 0) 218 fprintf(stderr, "Child exit status = %d\n", 219 status); 220 s += status == 0 ? 0 : 1; 221 } 222 } 223 224 return (s); 225} 226