xref: /freebsd/tools/test/stress2/misc/sendfile15.sh (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2018 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# sendfile() over socket pairs test.
31
32# Page fault in unp_dispose+0x90 seen:
33# https://people.freebsd.org/~pho/stress/log/sendfile15.txt
34
35# "panic: bad pte va 800657000 pte 0" seen:
36# https://people.freebsd.org/~pho/stress/log/sendfile15-2.txt
37
38# "panic: vm_page_free_prep 0xfffff817e0efac10 PG_ZERO 87 ...":
39# https://people.freebsd.org/~pho/stress/log/sendfile15-2.txt
40# Note that r334783 seems to alleviate the problem.
41
42. ../default.cfg
43[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
44
45dir=/tmp
46odir=`pwd`
47cd $dir
48sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile15.c
49mycc -o sendfile15 -Wall -Wextra -O0 -g sendfile15.c || exit 1
50rm -f sendfile15.c
51cd $odir
52
53set -e
54mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
55[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
56mdconfig -a -t swap -s 2g -u $mdstart
57newfs $newfs_flags md$mdstart > /dev/null
58mount /dev/md$mdstart $mntpoint
59set +e
60
61cd $mntpoint
62dd if=/dev/random of=input bs=4k count=1 status=none
63(cd $odir/../testcases/swap; ./swap -t 5m -i 20 > /dev/null) &
64$dir/sendfile15
65s=$?
66while pgrep -q swap; do
67	pkill swap
68done
69wait
70[ -f sendfile15.core -a $s -eq 0 ] &&
71    { ls -l sendfile15.core; mv sendfile15.core $dir; s=1; }
72cd $odir
73
74for i in `jot 6`; do
75	mount | grep -q "on $mntpoint " || break
76	umount $mntpoint && break || sleep 10
77	[ $i -eq 6 ] &&
78	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
79done
80mdconfig -d -u $mdstart
81rm -rf $dir/sendfile15
82exit $s
83
84EOF
85#include <sys/param.h>
86#include <sys/fcntl.h>
87#include <sys/mman.h>
88#include <sys/socket.h>
89#include <sys/stat.h>
90#include <sys/stat.h>
91#include <sys/uio.h>
92#include <sys/wait.h>
93
94#include <machine/atomic.h>
95
96#include <err.h>
97#include <errno.h>
98#include <fcntl.h>
99#include <stdio.h>
100#include <stdlib.h>
101#include <time.h>
102#include <unistd.h>
103
104static volatile u_int *share;
105
106#define PARALLEL 128
107#define RUNTIME (5 * 60)
108#define SYNC 0
109
110static void
111test(void)
112{
113	struct stat st;
114	off_t written, pos;
115	int child, error, from, n, status, sv[2];
116	char *buf;
117	const char *from_name;
118
119	atomic_add_int(&share[SYNC], 1);
120	while (share[SYNC] != PARALLEL)
121		;
122
123	from_name = "input";
124
125	if ((from = open(from_name, O_RDONLY)) == -1)
126		err(1, "open read %s", from_name);
127
128	if ((error = fstat(from, &st)) == -1)
129		err(1, "stat %s", from_name);
130
131	if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
132		err(1, "socketpair");
133
134	child = fork();
135	if (child == -1)
136		err(1, "fork");
137	else if (child != 0) {
138		setproctitle("parent");
139		close(sv[1]);
140		pos = 0;
141		for (;;) {
142			error = sendfile(from, sv[0], pos, st.st_size - pos,
143			    NULL, &written, 0);
144			if (error == -1) {
145				if (errno != EAGAIN)
146					err(1, "sendfile");
147			}
148			pos += written;
149			if (pos == st.st_size)
150				break;
151		}
152		close(sv[0]);
153		if (waitpid(child, &status, 0) != child)
154			err(1, "waitpid(%d)", child);
155	} else {
156		setproctitle("child");
157		close(sv[0]);
158		buf = malloc(st.st_size);
159		if (buf == NULL)
160			err(1, "malloc %jd", st.st_size);
161		pos = 0;
162		for (;;) {
163			written = st.st_size - pos;
164			n = read(sv[1], buf + pos, written);
165			if (n == -1)
166				err(1, "read");
167			else if (n == 0)
168				errx(1, "Short read");
169			pos += n;
170			if (pos == st.st_size)
171				break;
172		}
173		close(sv[1]);
174		_exit(0);
175	}
176
177	_exit(0);
178}
179
180int
181main(void)
182{
183	pid_t pids[PARALLEL];
184	size_t len;
185	time_t start;
186	int e, i, status;
187
188	e = 0;
189	len = PAGE_SIZE;
190	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
191	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
192		err(1, "mmap");
193
194	start = time(NULL);
195	while ((time(NULL) - start) < RUNTIME && e == 0) {
196		share[SYNC] = 0;
197		for (i = 0; i < PARALLEL; i++) {
198			if ((pids[i] = fork()) == 0)
199				test();
200			if (pids[i] == -1)
201				err(1, "fork()");
202		}
203		for (i = 0; i < PARALLEL; i++) {
204			if (waitpid(pids[i], &status, 0) == -1)
205				err(1, "waitpid(%d)", pids[i]);
206			if (status != 0) {
207				if (WIFSIGNALED(status))
208					fprintf(stderr,
209					    "pid %d exit signal %d\n",
210					    pids[i], WTERMSIG(status));
211			}
212			e += status == 0 ? 0 : 1;
213		}
214	}
215
216	return (e);
217}
218