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