1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2019 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# "panic: Lock (rw) vm object not locked @ vm/vm_page.c:1013" seen:
31# https://people.freebsd.org/~pho/stress/log/mlockall6-2.txt
32
33# "panic: vm_page_unwire: wire count underflow for page..." seen:
34# https://people.freebsd.org/~pho/stress/log/log0430.txt
35
36. ../default.cfg
37
38[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
39[ `swapinfo | wc -l` -eq 1 ] && exit 0
40
41dir=/tmp
42odir=`pwd`
43cd $dir
44sed '1,/^EOF/d' < $odir/$0 > $dir/mlockall6.c
45mycc -o mlockall6 -Wall -Wextra -O0 -g mlockall6.c || exit 1
46rm -f mlockall6.c
47cd $odir
48
49mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
50mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
51mdconfig -a -t swap -s 512m -u $mdstart || exit 1
52newfs $newfs_flags -n md$mdstart > /dev/null
53mount /dev/md$mdstart $mntpoint || exit 1
54
55daemon sh -c "(cd $odir/../testcases/swap; ./swap -t 20m -i 20 -l 100)" \
56    > /dev/null 2>&1
57sleep 2
58
59(cd $mntpoint; /tmp/mlockall6 || echo FAIL)
60
61while pgrep -q swap; do
62	pkill -9 swap
63done
64
65n=0
66while mount | grep "on $mntpoint " | grep -q /dev/md; do
67	umount $mntpoint || sleep 1
68	n=$((n + 1))
69	[ $n -gt 10 ] && { echo FAIL; exit 1; }
70done
71mdconfig -d -u $mdstart
72rm -rf /tmp/mlockall6
73exit 0
74
75EOF
76#include <sys/param.h>
77#include <sys/mman.h>
78#include <sys/stat.h>
79#include <sys/wait.h>
80
81#include <machine/atomic.h>
82
83#include <err.h>
84#include <errno.h>
85#include <fcntl.h>
86#include <stdio.h>
87#include <stdlib.h>
88#include <time.h>
89#include <unistd.h>
90
91#define LOOPS 2
92#define PARALLEL 8
93#define R0 0
94#define R1 1
95#define R2 2
96#define RUNTIME (10 * 60)
97
98static volatile u_int *share;
99static int ps;
100static char c[32 * 1024 * 1024];
101
102static void
103touch(void)
104{
105	int i;
106
107	for (i = 0; i < (int)sizeof(c); i += ps)
108		c[i] = 1;
109}
110
111static void
112test2(void)
113{
114	pid_t pid;
115	volatile u_int *share2;
116	size_t len;
117	int i, status;
118
119	len = ps;
120	if ((share2 = mmap(NULL, len, PROT_READ | PROT_WRITE,
121	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
122		err(1, "mmap");
123	touch();
124	usleep(arc4random() % 100000);
125	alarm(600);
126	if ((pid = fork()) == 0) {
127		alarm(600);
128		while (share2[R1] == 0)	/* Wait for parent */
129			;
130		atomic_add_int(&share2[R1], 1);
131		if (arc4random() % 100 < 50)
132			usleep(arc4random() % 1000);
133		if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
134			err(1, "mlock");
135		touch();
136		atomic_add_int(&share2[R2], 1);
137		_exit(0);
138	}
139	atomic_add_int(&share2[R1], 1);
140	while (share2[R1] != 2)	/* Wait for child */
141		;
142
143	for (i = 0; i < 100000 && share2[R2] == 0; i++)
144		touch();	/* while child is running */
145
146	if (waitpid(pid, &status, 0) != pid)
147		err(1, "wait");
148
149	if (status != 0)
150		fprintf(stderr, "Got signal %d\n", WTERMSIG(status));
151	_exit(WTERMSIG(status));
152}
153
154static void
155test(void)
156{
157	pid_t pid;
158	int i, s, status;
159
160	while (share[R0] == 0)
161		;
162	s = 0;
163	for (i = 0; i < LOOPS; i++) {
164		if ((pid = fork()) == 0)
165			test2();
166		waitpid(pid, &status, 0);
167		s = (s == 0) ? status : s;
168	}
169	_exit(s);
170}
171
172int
173main(void)
174{
175	pid_t pids[PARALLEL];
176	size_t len;
177	time_t start;
178	int i, s, status;
179
180	ps = getpagesize();
181	len = ps;
182	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
183	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
184		err(1, "mmap");
185	start = time(NULL);
186	s = 0;
187	while (s == 0 && (time(NULL) - start) < RUNTIME) {
188		for (i = 0; i < PARALLEL; i++) {
189			if ((pids[i] = fork()) == 0)
190				test();
191		}
192		atomic_add_int(&share[R0], 1);	/* Start test() runs */
193		for (i = 0; i < PARALLEL; i++) {
194			if (waitpid(pids[i], &status, 0) != pids[i])
195				err(1, "wait");
196			if (status != 0) {
197				fprintf(stderr, "FAIL: status = %d\n",
198				    status);
199			}
200			s = (s == 0) ? status : s;
201		}
202		atomic_add_int(&share[R0], -1);
203	}
204
205	return (s);
206}
207