xref: /freebsd/tools/test/stress2/misc/mlockall5.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2015 EMC Corp.
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# Regression test for r287591:
308a272653SPeter Holm
318a272653SPeter Holm# From the commit log:
328a272653SPeter Holm# Remove a check which caused spurious SIGSEGV on usermode access to the
338a272653SPeter Holm# mapped address without valid pte installed, when parallel wiring of
348a272653SPeter Holm# the entry happen.  The entry must be copy on write.  If entry is COW
358a272653SPeter Holm# but was already copied, and parallel wiring set
368a272653SPeter Holm# MAP_ENTRY_IN_TRANSITION, vm_fault() would sleep waiting for the
378a272653SPeter Holm# MAP_ENTRY_IN_TRANSITION flag to clear.  After that, the fault handler
388a272653SPeter Holm# is restarted and vm_map_lookup() or vm_map_lookup_locked() trip over
398a272653SPeter Holm# the check.  Note that this is race, if the address is accessed after
408a272653SPeter Holm# the wiring is done, the entry does not fault at all.
418a272653SPeter Holm
428a272653SPeter Holm# Test scenario by kib@.
438a272653SPeter Holm
448a272653SPeter Holm. ../default.cfg
458a272653SPeter Holm
468a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
478a272653SPeter Holm
488a272653SPeter Holmdir=/tmp
498a272653SPeter Holmodir=`pwd`
508a272653SPeter Holmcd $dir
518a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/mlockall5.c
528a272653SPeter Holmmycc -o mlockall5 -Wall -Wextra -O0 -g mlockall5.c -lpthread || exit 1
538a272653SPeter Holmrm -f mlockall5.c
548a272653SPeter Holmcd $odir
558a272653SPeter Holm
568a272653SPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
578a272653SPeter Holm[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
588a272653SPeter Holmmdconfig -a -t swap -s 512m -u $mdstart || exit 1
59*608c97bfSPeter Holmnewfs $newfs_flags -n md$mdstart > /dev/null
60*608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint || exit 1
618a272653SPeter Holm
628a272653SPeter Holm(cd $mntpoint; /tmp/mlockall5 || echo FAIL)
638a272653SPeter Holm
648a272653SPeter Holmn=0
658a272653SPeter Holmwhile mount | grep "on $mntpoint " | grep -q /dev/md; do
668a272653SPeter Holm	umount $mntpoint || sleep 1
678a272653SPeter Holm	n=$((n + 1))
688a272653SPeter Holm	[ $n -gt 10 ] && { echo FAIL; exit 1; }
698a272653SPeter Holmdone
708a272653SPeter Holmmdconfig -d -u $mdstart
718a272653SPeter Holmrm -rf /tmp/mlockall5
728a272653SPeter Holmexit
738a272653SPeter Holm
748a272653SPeter HolmEOF
758a272653SPeter Holm#include <sys/param.h>
768a272653SPeter Holm#include <sys/mman.h>
778a272653SPeter Holm#include <sys/stat.h>
788a272653SPeter Holm#include <sys/wait.h>
798a272653SPeter Holm
808a272653SPeter Holm#include <err.h>
818a272653SPeter Holm#include <errno.h>
828a272653SPeter Holm#include <fcntl.h>
838a272653SPeter Holm#include <pthread.h>
848a272653SPeter Holm#include <stdio.h>
858a272653SPeter Holm#include <stdlib.h>
868a272653SPeter Holm#include <unistd.h>
878a272653SPeter Holm
888a272653SPeter Holmsize_t clen;
898a272653SPeter Holmvolatile u_int share;
908a272653SPeter Holmint ps;
918a272653SPeter Holmchar *c;
928a272653SPeter Holm
938a272653SPeter Holmvoid *
948a272653SPeter Holmtouch(void *arg __unused)
958a272653SPeter Holm{
968a272653SPeter Holm
978a272653SPeter Holm	int i;
988a272653SPeter Holm
998a272653SPeter Holm	while (share == 0)
1008a272653SPeter Holm		;
1018a272653SPeter Holm	for (i = 0; i < (int)clen; i += ps)
1028a272653SPeter Holm		c[i] = 1;
1038a272653SPeter Holm
1048a272653SPeter Holm	return (NULL);
1058a272653SPeter Holm}
1068a272653SPeter Holm
1078a272653SPeter Holmvoid *
1088a272653SPeter Holmml(void *arg __unused)
1098a272653SPeter Holm{
1108a272653SPeter Holm	while (share == 0)
1118a272653SPeter Holm		;
1128a272653SPeter Holm	if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
1138a272653SPeter Holm		err(1, "mlock");
1148a272653SPeter Holm
1158a272653SPeter Holm	return (NULL);
1168a272653SPeter Holm}
1178a272653SPeter Holm
1188a272653SPeter Holmint
1198a272653SPeter Holmtest(void)
1208a272653SPeter Holm{
1218a272653SPeter Holm	pthread_t tid[2];
1228a272653SPeter Holm	int i, rc, status;
1238a272653SPeter Holm
1248a272653SPeter Holm	if (fork() == 0) {
1258a272653SPeter Holm		alarm(60);
1268a272653SPeter Holm		rc = pthread_create(&tid[0], NULL, touch, NULL);
1278a272653SPeter Holm		if (rc != 0)
1288a272653SPeter Holm			errc(1, rc, "pthread_create()");
1298a272653SPeter Holm		rc = pthread_create(&tid[1], NULL, ml, NULL);
1308a272653SPeter Holm		if (rc != 0)
1318a272653SPeter Holm			errc(1, rc, "pthread_create()");
1328a272653SPeter Holm		share = 1;
1338a272653SPeter Holm		for (i = 0; i < (int)nitems(tid); i++) {
1348a272653SPeter Holm			rc = pthread_join(tid[i], NULL);
1358a272653SPeter Holm			if (rc != 0)
1368a272653SPeter Holm				errc(1, rc, "pthread_join(%d)", i);
1378a272653SPeter Holm		}
1388a272653SPeter Holm		_exit(0);
1398a272653SPeter Holm	}
1408a272653SPeter Holm
1418a272653SPeter Holm	if (wait(&status) == -1)
1428a272653SPeter Holm		err(1, "wait");
1438a272653SPeter Holm
1448a272653SPeter Holm	return (WTERMSIG(status));
1458a272653SPeter Holm}
1468a272653SPeter Holm
1478a272653SPeter Holmint
1488a272653SPeter Holmmain(void)
1498a272653SPeter Holm{
1508a272653SPeter Holm	int s;
1518a272653SPeter Holm
1528a272653SPeter Holm	ps = getpagesize();
1538a272653SPeter Holm	clen = 32 * 1024;
1548a272653SPeter Holm	if ((c = mmap(NULL, clen, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
1558a272653SPeter Holm	    -1, 0)) == MAP_FAILED)
1568a272653SPeter Holm		err(1, "mmap");
1578a272653SPeter Holm
1588a272653SPeter Holm	s = test();
1598a272653SPeter Holm
1608a272653SPeter Holm	return (s);
1618a272653SPeter Holm}
162