xref: /freebsd/tools/test/stress2/misc/sendfile18.sh (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2020 Mark Johnston <markj@FreeBSD.org>
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# "Fatal trap 9: general protection fault while in kernel mode" seen:
31# https://people.freebsd.org/~pho/stress/log/sendfile15-4.txt
32
33# Mark wrote:
34# It took me some time but I managed to get a repro.  Here is what I did:
35# - Create a malloc-backed md(4) device with a UFS filesystem.  Configure
36#   a gnop provider with -q 100 -d 10, i.e., always delay reads by 10ms.
37# - Write a small file (e.g., 4KB) to the filesystem.
38# - In a loop, unmount the filesystem, mount it, and run the test program.
39# The test program:
40# - creates a PF_LOCAL socket pair <s1, s2>,
41# - opens the input file,
42# - sends the file over s1,
43# - closes s1,
44# - sleeps for a second before exiting (and closing s2)
45#
46# Using a separate filesystem ensures that the input file is not cached
47# when the test program runs, so sendfile will perform an async getpages
48# and place the "future" mbufs in s2's receive buffer.  The gnop delay
49# ensures that the I/O request will not be completed before s1 is closed,
50# and because s1 is closed uipc_ready() will free the promised mbufs and
51# return ECONNRESET.  Because of the sleep, s2's receive buffer will not
52# be scanned until after the uipc_ready() call.
53
54# Fixed by r359778
55
56[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
57kldstat | grep -q geom_nop || { gnop load 2>/dev/null || exit 0 &&
58    notloaded=1; }
59gnop status || exit 1
60
61. ../default.cfg
62
63dir=/tmp
64odir=`pwd`
65cd $dir
66sed '1,/^EOF/d' < $odir/$0 > $dir/sendfile18.c
67mycc -o sendfile18 -Wall -Wextra -O0 -g sendfile18.c || exit 1
68rm -f sendfile18.c
69cd $odir
70
71set -e
72mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
73[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
74
75mdconfig -a -t swap -s 2g -u $mdstart
76gnop create /dev/md$mdstart
77newfs $newfs_flags /dev/md$mdstart.nop > /dev/null
78mount /dev/md$mdstart.nop $mntpoint
79chmod 777 $mntpoint
80gnop configure -q 100 -d 10 /dev/md$mdstart.nop
81set +e
82
83dd if=/dev/zero of=$mntpoint/file bs=4k count=1 status=none
84
85start=`date +%s`
86while [ $((`date +%s` - start)) -lt 5 ]; do
87	umount $mntpoint
88	mount /dev/md$mdstart.nop $mntpoint
89	/tmp/sendfile18 $mntpoint/file
90done
91umount $mntpoint
92
93gnop destroy /dev/md$mdstart.nop
94mdconfig -d -u $mdstart
95rm $dir/sendfile18
96[ $notloaded ] && gnop unload
97
98exit 0
99EOF
100#include <sys/socket.h>
101
102#include <err.h>
103#include <fcntl.h>
104#include <stdlib.h>
105#include <unistd.h>
106
107int
108main(int argc __unused, char **argv)
109{
110        int fd, sd[2];
111
112        if (socketpair(PF_LOCAL, SOCK_STREAM, 0, sd) != 0)
113                err(1, "socketpair");
114
115        fd = open(argv[1], O_RDONLY | O_DIRECT);
116        if (fd < 0)
117                err(1, "open");
118
119        if (sendfile(fd, sd[0], 0, 4096, NULL, NULL, 0) != 0)
120                err(1, "sendfile");
121        close(sd[0]);
122        sleep(1);
123
124        return (0);
125}
126