Path: blob/main/tools/test/stress2/misc/contigmalloc3.sh
39537 views
#!/bin/sh12#3# Copyright (c) 2014 EMC Corp.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# contigmalloc(9) / free(9) test scenario.29# Test allocation with 1GB3031# "panic: Bad link elm 0x6766fbc next->prev != elm" seen:32# https://people.freebsd.org/~pho/stress/log/kostik1094.txt33# Fixed by r3312473435[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 136[ -d /usr/src/sys ] || exit 037builddir=`sysctl kern.version | grep @ | sed 's/.*://'`38[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 039export SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`4041. ../default.cfg4243odir=`pwd`44dir=/tmp/contigmalloc45rm -rf $dir; mkdir -p $dir46cat > $dir/ctest.c <<EOF47#include <sys/param.h>48#include <sys/syscall.h>4950#include <err.h>51#include <stdio.h>52#include <stdlib.h>53#include <time.h>54#include <unistd.h>5556#define MAXBUF (1LL * 1024 * 1024 * 1024) /* Max buffer size */57#define TALLOC 158#define TFREE 25960void61test(int argc, char *argv[])62{63long mw, size;64int no, ps, res;65char *cp;6667if (argc == 3) {68no = atoi(argv[1]);69mw = atol(argv[2]);70}71if (argc != 3 || no == 0 || mw == 0)72errx(1, "Usage: %s <syscall number> <max wired>", argv[0]);7374ps = getpagesize();75if (mw < MAXBUF / ps) {76fprintf(stderr, "max_user_wired too small for this test\n");77exit (0);78}79size = round_page(MAXBUF);80res = syscall(no, TALLOC, &cp, &size);81if (res == -1) {82err(1, "contigmalloc(%lu MB) failed",83size / 1024 / 1024);84} else {85#if defined(TEST)86fprintf(stderr, "contigmalloc(%lu pages) %luMB\n",87size / ps, size / 1024 / 1024);88#endif89}9091res = syscall(no, TFREE, &cp, &size);92#if defined(TEST)93fprintf(stderr, "free(%lu pages) %luMB\n",94size / ps, size / 1024 / 1024);95#endif96}9798int99main(int argc, char *argv[])100{101102test(argc, argv);103104return (0);105}106107EOF108mycc -o /tmp/ctest -Wall -Wextra -O0 -g $dir/ctest.c || exit 1109rm $dir/ctest.c110111cd $dir112cat > Makefile <<EOF113KMOD= cmalloc114SRCS= cmalloc.c115116.include <bsd.kmod.mk>117EOF118119sed '1,/^EOF2/d' < $odir/$0 > cmalloc.c120make || exit 1121kldload $dir/cmalloc.ko || exit 1122123cd $odir124mw=`sysctl -n vm.max_user_wired` || exit 1125/tmp/ctest `sysctl -n debug.cmalloc_offset` $mw 2>&1 | tail -5126kldunload $dir/cmalloc.ko127rm -rf $dir /tmp/ctest128exit 0129130EOF2131#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>140141#define TALLOC 1142#define TFREE 2143144/*145* Hook up a syscall for contigmalloc testing.146*/147148struct cmalloc_args {149int a_op;150void *a_ptr;151void *a_size;152};153154static int155cmalloc(struct thread *td, struct cmalloc_args *uap)156{157void *p;158unsigned long size;159int error;160161error = copyin(uap->a_size, &size, sizeof(size));162if (error != 0) {163return (error);164}165switch (uap->a_op) {166case TFREE:167error = copyin(uap->a_ptr, &p, sizeof(p));168if (error == 0)169free(p, M_TEMP);170return (error);171172case TALLOC:173p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0);174if (p != NULL) {175error = copyout(&p, uap->a_ptr, sizeof(p));176return (error);177}178return (ENOMEM);179}180return (EINVAL);181}182183/*184* The sysent for the new syscall185*/186static struct sysent cmalloc_sysent = {187.sy_narg = 3, /* sy_narg */188.sy_call = (sy_call_t *) cmalloc /* sy_call */189};190191/*192* The offset in sysent where the syscall is allocated.193*/194static int cmalloc_offset = NO_SYSCALL;195196SYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0,197"cmalloc syscall number");198199/*200* The function called at load/unload.201*/202203static int204cmalloc_load(struct module *module, int cmd, void *arg)205{206int error = 0;207208switch (cmd) {209case MOD_LOAD :210break;211case MOD_UNLOAD :212break;213default :214error = EOPNOTSUPP;215break;216}217return (error);218}219220SYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent,221cmalloc_load, NULL);222223224