4 # Copyright (c) 2015 EMC Corp.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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
29 # contigmalloc(9) / contigfree(9) test scenario.
30 # Regression test for allocations >= 2 GiB.
31 # "panic: vm_page_insert_after: mpred doesn't precede pindex" seen.
34 [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35 [ -d /usr/src/sys ] || exit 0
36 builddir=`sysctl kern.version | grep @ | sed 's/.*://'`
37 [ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
38 export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
44 rm -rf $dir; mkdir -p $dir
45 cat > $dir/ctest2.c <<EOF
46 #include <sys/param.h>
47 #include <sys/syscall.h>
63 test(int argc, char *argv[])
72 if (argc != 3 || no == 0 || mw == 0)
73 errx(1, "Usage: %s <syscall number> <max wired>", argv[0]);
76 size = mw / 100 * 80 * ps; /* Use 80% of vm.max_user_wired */
78 res = syscall(no, TALLOC, &p, &size);
81 warn("contigmalloc(%lu pages) failed",
85 fprintf(stderr, "pre contigmalloc(%lu pages): %lu MiB\n",
86 size, size * ps / 1024 / 1024);
88 res = syscall(no, TFREE, &p, &size);
90 fprintf(stderr, "contigfree(%lu pages)\n",
99 main(int argc, char *argv[])
107 mycc -o /tmp/ctest2 -Wall -Wextra -O0 -g $dir/ctest2.c || exit 1
115 .include <bsd.kmod.mk>
118 sed '1,/^EOF2/d' < $odir/$0 > cmalloc2.c
119 make depend all || exit 1
120 kldload $dir/cmalloc2.ko || exit 1
123 mw=$((`sysctl -n vm.max_user_wired` - \
124 `sysctl -n vm.stats.vm.v_user_wire_count`)) || exit 1
125 /tmp/ctest2 `sysctl -n debug.cmalloc_offset` $mw #2>&1 | tail -5
126 kldunload $dir/cmalloc2.ko
127 rm -rf $dir /tmp/ctest2
131 #include <sys/param.h>
132 #include <sys/kernel.h>
133 #include <sys/malloc.h>
134 #include <sys/module.h>
135 #include <sys/proc.h>
136 #include <sys/sysctl.h>
137 #include <sys/sysent.h>
138 #include <sys/sysproto.h>
139 #include <sys/systm.h>
145 * Hook up a syscall for contigmalloc testing.
148 struct cmalloc_args {
155 cmalloc(struct thread *td, struct cmalloc_args *uap)
161 error = copyin(uap->a_size, &size, sizeof(size));
167 error = copyin(uap->a_ptr, &p, sizeof(p));
170 contigfree(p, size, M_TEMP);
175 p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0);
177 error = copyout(&p, uap->a_ptr, sizeof(p));
186 * The sysent for the new syscall
188 static struct sysent cmalloc_sysent = {
189 .sy_narg = 3, /* sy_narg */
190 .sy_call = (sy_call_t *) cmalloc /* sy_call */
194 * The offset in sysent where the syscall is allocated.
196 static int cmalloc_offset = NO_SYSCALL;
198 SYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0,
199 "cmalloc syscall number");
202 * The function called at load/unload.
206 cmalloc_load(struct module *module, int cmd, void *arg)
222 SYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent,