xref: /freebsd/tools/test/stress2/misc/mlockall5.sh (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Regression test for r287591:
30
31# From the commit log:
32# Remove a check which caused spurious SIGSEGV on usermode access to the
33# mapped address without valid pte installed, when parallel wiring of
34# the entry happen.  The entry must be copy on write.  If entry is COW
35# but was already copied, and parallel wiring set
36# MAP_ENTRY_IN_TRANSITION, vm_fault() would sleep waiting for the
37# MAP_ENTRY_IN_TRANSITION flag to clear.  After that, the fault handler
38# is restarted and vm_map_lookup() or vm_map_lookup_locked() trip over
39# the check.  Note that this is race, if the address is accessed after
40# the wiring is done, the entry does not fault at all.
41
42# Test scenario by kib@.
43
44. ../default.cfg
45
46[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
47
48dir=/tmp
49odir=`pwd`
50cd $dir
51sed '1,/^EOF/d' < $odir/$0 > $dir/mlockall5.c
52mycc -o mlockall5 -Wall -Wextra -O0 -g mlockall5.c -lpthread || exit 1
53rm -f mlockall5.c
54cd $odir
55
56mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
57[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
58mdconfig -a -t swap -s 512m -u $mdstart || exit 1
59bsdlabel -w md$mdstart auto
60newfs $newfs_flags -n md${mdstart}$part > /dev/null
61mount /dev/md${mdstart}$part $mntpoint || exit 1
62
63(cd $mntpoint; /tmp/mlockall5 || echo FAIL)
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/mlockall5
73exit
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 <err.h>
82#include <errno.h>
83#include <fcntl.h>
84#include <pthread.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <unistd.h>
88
89size_t clen;
90volatile u_int share;
91int ps;
92char *c;
93
94void *
95touch(void *arg __unused)
96{
97
98	int i;
99
100	while (share == 0)
101		;
102	for (i = 0; i < (int)clen; i += ps)
103		c[i] = 1;
104
105	return (NULL);
106}
107
108void *
109ml(void *arg __unused)
110{
111	while (share == 0)
112		;
113	if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
114		err(1, "mlock");
115
116	return (NULL);
117}
118
119int
120test(void)
121{
122	pthread_t tid[2];
123	int i, rc, status;
124
125	if (fork() == 0) {
126		alarm(60);
127		rc = pthread_create(&tid[0], NULL, touch, NULL);
128		if (rc != 0)
129			errc(1, rc, "pthread_create()");
130		rc = pthread_create(&tid[1], NULL, ml, NULL);
131		if (rc != 0)
132			errc(1, rc, "pthread_create()");
133		share = 1;
134		for (i = 0; i < (int)nitems(tid); i++) {
135			rc = pthread_join(tid[i], NULL);
136			if (rc != 0)
137				errc(1, rc, "pthread_join(%d)", i);
138		}
139		_exit(0);
140	}
141
142	if (wait(&status) == -1)
143		err(1, "wait");
144
145	return (WTERMSIG(status));
146}
147
148int
149main(void)
150{
151	int s;
152
153	ps = getpagesize();
154	clen = 32 * 1024;
155	if ((c = mmap(NULL, clen, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
156	    -1, 0)) == MAP_FAILED)
157		err(1, "mmap");
158
159	s = test();
160
161	return (s);
162}
163