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