18a272653SPeter Holm#!/bin/sh 28a272653SPeter Holm 38a272653SPeter Holm# 48a272653SPeter Holm# Copyright (c) 2014 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 29*d1bdc282SBjoern A. Zeeb# contigmalloc(9) / free(9) test scenario. 308a272653SPeter Holm# Test allocation with 1GB 318a272653SPeter Holm 328a272653SPeter Holm# "panic: Bad link elm 0x6766fbc next->prev != elm" seen: 338a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik1094.txt 348a272653SPeter Holm# Fixed by r331247 358a272653SPeter Holm 368a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 378a272653SPeter Holm[ -d /usr/src/sys ] || exit 0 388a272653SPeter Holmbuilddir=`sysctl kern.version | grep @ | sed 's/.*://'` 398a272653SPeter Holm[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0 408a272653SPeter Holmexport SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'` 418a272653SPeter Holm 428a272653SPeter Holm. ../default.cfg 438a272653SPeter Holm 448a272653SPeter Holmodir=`pwd` 458a272653SPeter Holmdir=/tmp/contigmalloc 468a272653SPeter Holmrm -rf $dir; mkdir -p $dir 478a272653SPeter Holmcat > $dir/ctest.c <<EOF 488a272653SPeter Holm#include <sys/param.h> 498a272653SPeter Holm#include <sys/syscall.h> 508a272653SPeter Holm 518a272653SPeter Holm#include <err.h> 528a272653SPeter Holm#include <stdio.h> 538a272653SPeter Holm#include <stdlib.h> 548a272653SPeter Holm#include <time.h> 558a272653SPeter Holm#include <unistd.h> 568a272653SPeter Holm 578a272653SPeter Holm#define MAXBUF (1LL * 1024 * 1024 * 1024) /* Max buffer size */ 588a272653SPeter Holm#define TALLOC 1 598a272653SPeter Holm#define TFREE 2 608a272653SPeter Holm 618a272653SPeter Holmvoid 628a272653SPeter Holmtest(int argc, char *argv[]) 638a272653SPeter Holm{ 648a272653SPeter Holm long mw, size; 658d72c409SPeter Holm int no, ps, res; 668a272653SPeter Holm char *cp; 678a272653SPeter Holm 688a272653SPeter Holm if (argc == 3) { 698a272653SPeter Holm no = atoi(argv[1]); 708a272653SPeter Holm mw = atol(argv[2]); 718a272653SPeter Holm } 728a272653SPeter Holm if (argc != 3 || no == 0 || mw == 0) 738a272653SPeter Holm errx(1, "Usage: %s <syscall number> <max wired>", argv[0]); 748a272653SPeter Holm 758a272653SPeter Holm ps = getpagesize(); 768a272653SPeter Holm if (mw < MAXBUF / ps) { 778a272653SPeter Holm fprintf(stderr, "max_user_wired too small for this test\n"); 788a272653SPeter Holm exit (0); 798a272653SPeter Holm } 808a272653SPeter Holm size = round_page(MAXBUF); 818a272653SPeter Holm res = syscall(no, TALLOC, &cp, &size); 828a272653SPeter Holm if (res == -1) { 838a272653SPeter Holm err(1, "contigmalloc(%lu MB) failed", 848a272653SPeter Holm size / 1024 / 1024); 858a272653SPeter Holm } else { 868a272653SPeter Holm#if defined(TEST) 878a272653SPeter Holm fprintf(stderr, "contigmalloc(%lu pages) %luMB\n", 888a272653SPeter Holm size / ps, size / 1024 / 1024); 898a272653SPeter Holm#endif 908a272653SPeter Holm } 918a272653SPeter Holm 928a272653SPeter Holm res = syscall(no, TFREE, &cp, &size); 938a272653SPeter Holm#if defined(TEST) 94*d1bdc282SBjoern A. Zeeb fprintf(stderr, "free(%lu pages) %luMB\n", 958a272653SPeter Holm size / ps, size / 1024 / 1024); 968a272653SPeter Holm#endif 978a272653SPeter Holm} 988a272653SPeter Holm 998a272653SPeter Holmint 1008a272653SPeter Holmmain(int argc, char *argv[]) 1018a272653SPeter Holm{ 1028a272653SPeter Holm 1038a272653SPeter Holm test(argc, argv); 1048a272653SPeter Holm 1058a272653SPeter Holm return (0); 1068a272653SPeter Holm} 1078a272653SPeter Holm 1088a272653SPeter HolmEOF 1098a272653SPeter Holmmycc -o /tmp/ctest -Wall -Wextra -O0 -g $dir/ctest.c || exit 1 1108a272653SPeter Holmrm $dir/ctest.c 1118a272653SPeter Holm 1128a272653SPeter Holmcd $dir 1138a272653SPeter Holmcat > Makefile <<EOF 1148a272653SPeter HolmKMOD= cmalloc 1158a272653SPeter HolmSRCS= cmalloc.c 1168a272653SPeter Holm 1178a272653SPeter Holm.include <bsd.kmod.mk> 1188a272653SPeter HolmEOF 1198a272653SPeter Holm 1208a272653SPeter Holmsed '1,/^EOF2/d' < $odir/$0 > cmalloc.c 1218a272653SPeter Holmmake || exit 1 1228a272653SPeter Holmkldload $dir/cmalloc.ko || exit 1 1238a272653SPeter Holm 1248a272653SPeter Holmcd $odir 1258a272653SPeter Holmmw=`sysctl -n vm.max_user_wired` || exit 1 1268a272653SPeter Holm/tmp/ctest `sysctl -n debug.cmalloc_offset` $mw 2>&1 | tail -5 1278a272653SPeter Holmkldunload $dir/cmalloc.ko 1288a272653SPeter Holmrm -rf $dir /tmp/ctest 1298a272653SPeter Holmexit 0 1308a272653SPeter Holm 1318a272653SPeter HolmEOF2 1328a272653SPeter Holm#include <sys/param.h> 1338a272653SPeter Holm#include <sys/kernel.h> 1348a272653SPeter Holm#include <sys/malloc.h> 1358a272653SPeter Holm#include <sys/module.h> 1368a272653SPeter Holm#include <sys/proc.h> 1378a272653SPeter Holm#include <sys/sysctl.h> 1388a272653SPeter Holm#include <sys/sysent.h> 1398a272653SPeter Holm#include <sys/sysproto.h> 1408a272653SPeter Holm#include <sys/systm.h> 1418a272653SPeter Holm 1428a272653SPeter Holm#define TALLOC 1 1438a272653SPeter Holm#define TFREE 2 1448a272653SPeter Holm 1458a272653SPeter Holm/* 1468a272653SPeter Holm * Hook up a syscall for contigmalloc testing. 1478a272653SPeter Holm */ 1488a272653SPeter Holm 1498a272653SPeter Holmstruct cmalloc_args { 1508a272653SPeter Holm int a_op; 1518a272653SPeter Holm void *a_ptr; 1528a272653SPeter Holm void *a_size; 1538a272653SPeter Holm}; 1548a272653SPeter Holm 1558a272653SPeter Holmstatic int 1568a272653SPeter Holmcmalloc(struct thread *td, struct cmalloc_args *uap) 1578a272653SPeter Holm{ 1588a272653SPeter Holm void *p; 1598a272653SPeter Holm unsigned long size; 1608a272653SPeter Holm int error; 1618a272653SPeter Holm 1628a272653SPeter Holm error = copyin(uap->a_size, &size, sizeof(size)); 1638a272653SPeter Holm if (error != 0) { 1648a272653SPeter Holm return (error); 1658a272653SPeter Holm } 1668a272653SPeter Holm switch (uap->a_op) { 1678a272653SPeter Holm case TFREE: 1688a272653SPeter Holm error = copyin(uap->a_ptr, &p, sizeof(p)); 169*d1bdc282SBjoern A. Zeeb if (error == 0) 170*d1bdc282SBjoern A. Zeeb free(p, M_TEMP); 1718a272653SPeter Holm return (error); 1728a272653SPeter Holm 1738a272653SPeter Holm case TALLOC: 1748a272653SPeter Holm p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0); 1758a272653SPeter Holm if (p != NULL) { 1768a272653SPeter Holm error = copyout(&p, uap->a_ptr, sizeof(p)); 1778a272653SPeter Holm return (error); 1788a272653SPeter Holm } 1798a272653SPeter Holm return (ENOMEM); 1808a272653SPeter Holm } 1818a272653SPeter Holm return (EINVAL); 1828a272653SPeter Holm} 1838a272653SPeter Holm 1848a272653SPeter Holm/* 1858a272653SPeter Holm * The sysent for the new syscall 1868a272653SPeter Holm */ 1878a272653SPeter Holmstatic struct sysent cmalloc_sysent = { 1888a272653SPeter Holm .sy_narg = 3, /* sy_narg */ 1898a272653SPeter Holm .sy_call = (sy_call_t *) cmalloc /* sy_call */ 1908a272653SPeter Holm}; 1918a272653SPeter Holm 1928a272653SPeter Holm/* 1938a272653SPeter Holm * The offset in sysent where the syscall is allocated. 1948a272653SPeter Holm */ 1958a272653SPeter Holmstatic int cmalloc_offset = NO_SYSCALL; 1968a272653SPeter Holm 1978a272653SPeter HolmSYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0, 1988a272653SPeter Holm "cmalloc syscall number"); 1998a272653SPeter Holm 2008a272653SPeter Holm/* 2018a272653SPeter Holm * The function called at load/unload. 2028a272653SPeter Holm */ 2038a272653SPeter Holm 2048a272653SPeter Holmstatic int 2058a272653SPeter Holmcmalloc_load(struct module *module, int cmd, void *arg) 2068a272653SPeter Holm{ 2078a272653SPeter Holm int error = 0; 2088a272653SPeter Holm 2098a272653SPeter Holm switch (cmd) { 2108a272653SPeter Holm case MOD_LOAD : 2118a272653SPeter Holm break; 2128a272653SPeter Holm case MOD_UNLOAD : 2138a272653SPeter Holm break; 2148a272653SPeter Holm default : 2158a272653SPeter Holm error = EOPNOTSUPP; 2168a272653SPeter Holm break; 2178a272653SPeter Holm } 2188a272653SPeter Holm return (error); 2198a272653SPeter Holm} 2208a272653SPeter Holm 2218a272653SPeter HolmSYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent, 2228a272653SPeter Holm cmalloc_load, NULL); 223