xref: /freebsd/tools/test/stress2/misc/sendfile15.sh (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
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
57bsdlabel -w md$mdstart auto
58newfs $newfs_flags md${mdstart}$part > /dev/null
59mount /dev/md${mdstart}$part $mntpoint
60set +e
61
62cd $mntpoint
63dd if=/dev/random of=input bs=4k count=1 status=none
64(cd $odir/../testcases/swap; ./swap -t 5m -i 20 > /dev/null) &
65$dir/sendfile15
66s=$?
67while pgrep -q swap; do
68	pkill swap
69done
70wait
71[ -f sendfile15.core -a $s -eq 0 ] &&
72    { ls -l sendfile15.core; mv sendfile15.core $dir; s=1; }
73cd $odir
74
75for i in `jot 6`; do
76	mount | grep -q "on $mntpoint " || break
77	umount $mntpoint && break || sleep 10
78	[ $i -eq 6 ] &&
79	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
80done
81mdconfig -d -u $mdstart
82rm -rf $dir/sendfile15
83exit $s
84
85EOF
86#include <sys/param.h>
87#include <sys/fcntl.h>
88#include <sys/mman.h>
89#include <sys/socket.h>
90#include <sys/stat.h>
91#include <sys/stat.h>
92#include <sys/uio.h>
93#include <sys/wait.h>
94
95#include <machine/atomic.h>
96
97#include <err.h>
98#include <errno.h>
99#include <fcntl.h>
100#include <stdio.h>
101#include <stdlib.h>
102#include <time.h>
103#include <unistd.h>
104
105static volatile u_int *share;
106
107#define PARALLEL 128
108#define RUNTIME (5 * 60)
109#define SYNC 0
110
111static void
112test(void)
113{
114	struct stat st;
115	off_t written, pos;
116	int child, error, from, n, status, sv[2];
117	char *buf;
118	const char *from_name;
119
120	atomic_add_int(&share[SYNC], 1);
121	while (share[SYNC] != PARALLEL)
122		;
123
124	from_name = "input";
125
126	if ((from = open(from_name, O_RDONLY)) == -1)
127		err(1, "open read %s", from_name);
128
129	if ((error = fstat(from, &st)) == -1)
130		err(1, "stat %s", from_name);
131
132	if ((error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) == -1)
133		err(1, "socketpair");
134
135	child = fork();
136	if (child == -1)
137		err(1, "fork");
138	else if (child != 0) {
139		setproctitle("parent");
140		close(sv[1]);
141		pos = 0;
142		for (;;) {
143			error = sendfile(from, sv[0], pos, st.st_size - pos,
144			    NULL, &written, 0);
145			if (error == -1) {
146				if (errno != EAGAIN)
147					err(1, "sendfile");
148			}
149			pos += written;
150			if (pos == st.st_size)
151				break;
152		}
153		close(sv[0]);
154		if (waitpid(child, &status, 0) != child)
155			err(1, "waitpid(%d)", child);
156	} else {
157		setproctitle("child");
158		close(sv[0]);
159		buf = malloc(st.st_size);
160		if (buf == NULL)
161			err(1, "malloc %jd", st.st_size);
162		pos = 0;
163		for (;;) {
164			written = st.st_size - pos;
165			n = read(sv[1], buf + pos, written);
166			if (n == -1)
167				err(1, "read");
168			else if (n == 0)
169				errx(1, "Short read");
170			pos += n;
171			if (pos == st.st_size)
172				break;
173		}
174		close(sv[1]);
175		_exit(0);
176	}
177
178	_exit(0);
179}
180
181int
182main(void)
183{
184	pid_t pids[PARALLEL];
185	size_t len;
186	time_t start;
187	int e, i, status;
188
189	e = 0;
190	len = PAGE_SIZE;
191	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
192	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
193		err(1, "mmap");
194
195	start = time(NULL);
196	while ((time(NULL) - start) < RUNTIME && e == 0) {
197		share[SYNC] = 0;
198		for (i = 0; i < PARALLEL; i++) {
199			if ((pids[i] = fork()) == 0)
200				test();
201			if (pids[i] == -1)
202				err(1, "fork()");
203		}
204		for (i = 0; i < PARALLEL; i++) {
205			if (waitpid(pids[i], &status, 0) == -1)
206				err(1, "waitpid(%d)", pids[i]);
207			if (status != 0) {
208				if (WIFSIGNALED(status))
209					fprintf(stderr,
210					    "pid %d exit signal %d\n",
211					    pids[i], WTERMSIG(status));
212			}
213			e += status == 0 ? 0 : 1;
214		}
215	}
216
217	return (e);
218}
219