xref: /freebsd/tools/test/stress2/misc/contigmalloc3.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2014 EMC Corp.
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# contigmalloc(9) / contigfree(9) test scenario.
30*8a272653SPeter Holm# Test allocation with 1GB
31*8a272653SPeter Holm
32*8a272653SPeter Holm# "panic: Bad link elm 0x6766fbc next->prev != elm" seen:
33*8a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/kostik1094.txt
34*8a272653SPeter Holm# Fixed by r331247
35*8a272653SPeter Holm
36*8a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
37*8a272653SPeter Holm[ -d /usr/src/sys ] || exit 0
38*8a272653SPeter Holmbuilddir=`sysctl kern.version | grep @ | sed 's/.*://'`
39*8a272653SPeter Holm[ -d "$builddir" ] && export KERNBUILDDIR=$builddir || exit 0
40*8a272653SPeter Holmexport SYSDIR=`echo $builddir | sed 's#/sys.*#/sys#'`
41*8a272653SPeter Holm
42*8a272653SPeter Holm. ../default.cfg
43*8a272653SPeter Holm
44*8a272653SPeter Holmodir=`pwd`
45*8a272653SPeter Holmdir=/tmp/contigmalloc
46*8a272653SPeter Holmrm -rf $dir; mkdir -p $dir
47*8a272653SPeter Holmcat > $dir/ctest.c <<EOF
48*8a272653SPeter Holm#include <sys/param.h>
49*8a272653SPeter Holm#include <sys/syscall.h>
50*8a272653SPeter Holm
51*8a272653SPeter Holm#include <err.h>
52*8a272653SPeter Holm#include <stdio.h>
53*8a272653SPeter Holm#include <stdlib.h>
54*8a272653SPeter Holm#include <time.h>
55*8a272653SPeter Holm#include <unistd.h>
56*8a272653SPeter Holm
57*8a272653SPeter Holm#define MAXBUF (1LL * 1024 * 1024 * 1024)	/* Max buffer size */
58*8a272653SPeter Holm#define TALLOC 1
59*8a272653SPeter Holm#define TFREE  2
60*8a272653SPeter Holm
61*8a272653SPeter Holmvoid
62*8a272653SPeter Holmtest(int argc, char *argv[])
63*8a272653SPeter Holm{
64*8a272653SPeter Holm	long mw, size;
65*8a272653SPeter Holm	int i, no, ps, res;
66*8a272653SPeter Holm	char *cp;
67*8a272653SPeter Holm
68*8a272653SPeter Holm	if (argc == 3) {
69*8a272653SPeter Holm		no = atoi(argv[1]);
70*8a272653SPeter Holm		mw = atol(argv[2]);
71*8a272653SPeter Holm	}
72*8a272653SPeter Holm	if (argc != 3 || no == 0 || mw == 0)
73*8a272653SPeter Holm		errx(1, "Usage: %s <syscall number> <max wired>", argv[0]);
74*8a272653SPeter Holm
75*8a272653SPeter Holm	ps = getpagesize();
76*8a272653SPeter Holm	if (mw < MAXBUF / ps) {
77*8a272653SPeter Holm		fprintf(stderr, "max_user_wired too small for this test\n");
78*8a272653SPeter Holm		exit (0);
79*8a272653SPeter Holm	}
80*8a272653SPeter Holm	i = 0;
81*8a272653SPeter Holm	size = round_page(MAXBUF);
82*8a272653SPeter Holm	res = syscall(no, TALLOC, &cp, &size);
83*8a272653SPeter Holm	if (res == -1) {
84*8a272653SPeter Holm		err(1, "contigmalloc(%lu MB) failed",
85*8a272653SPeter Holm		    size / 1024 / 1024);
86*8a272653SPeter Holm	} else {
87*8a272653SPeter Holm#if defined(TEST)
88*8a272653SPeter Holm		fprintf(stderr, "contigmalloc(%lu pages) %luMB\n",
89*8a272653SPeter Holm		    size / ps, size / 1024 / 1024);
90*8a272653SPeter Holm#endif
91*8a272653SPeter Holm	}
92*8a272653SPeter Holm
93*8a272653SPeter Holm	res = syscall(no, TFREE, &cp, &size);
94*8a272653SPeter Holm#if defined(TEST)
95*8a272653SPeter Holm	fprintf(stderr, "contigfree(%lu pages) %luMB\n",
96*8a272653SPeter Holm	    size / ps, size / 1024 / 1024);
97*8a272653SPeter Holm#endif
98*8a272653SPeter Holm}
99*8a272653SPeter Holm
100*8a272653SPeter Holmint
101*8a272653SPeter Holmmain(int argc, char *argv[])
102*8a272653SPeter Holm{
103*8a272653SPeter Holm
104*8a272653SPeter Holm	test(argc, argv);
105*8a272653SPeter Holm
106*8a272653SPeter Holm	return (0);
107*8a272653SPeter Holm}
108*8a272653SPeter Holm
109*8a272653SPeter HolmEOF
110*8a272653SPeter Holmmycc -o /tmp/ctest -Wall -Wextra -O0 -g $dir/ctest.c || exit 1
111*8a272653SPeter Holmrm $dir/ctest.c
112*8a272653SPeter Holm
113*8a272653SPeter Holmcd $dir
114*8a272653SPeter Holmcat > Makefile <<EOF
115*8a272653SPeter HolmKMOD= cmalloc
116*8a272653SPeter HolmSRCS= cmalloc.c
117*8a272653SPeter Holm
118*8a272653SPeter Holm.include <bsd.kmod.mk>
119*8a272653SPeter HolmEOF
120*8a272653SPeter Holm
121*8a272653SPeter Holmsed '1,/^EOF2/d' < $odir/$0 > cmalloc.c
122*8a272653SPeter Holmmake || exit 1
123*8a272653SPeter Holmkldload $dir/cmalloc.ko || exit 1
124*8a272653SPeter Holm
125*8a272653SPeter Holmcd $odir
126*8a272653SPeter Holmmw=`sysctl -n vm.max_user_wired` || exit 1
127*8a272653SPeter Holm/tmp/ctest `sysctl -n debug.cmalloc_offset` $mw 2>&1 | tail -5
128*8a272653SPeter Holmkldunload $dir/cmalloc.ko
129*8a272653SPeter Holmrm -rf $dir /tmp/ctest
130*8a272653SPeter Holmexit 0
131*8a272653SPeter Holm
132*8a272653SPeter HolmEOF2
133*8a272653SPeter Holm#include <sys/param.h>
134*8a272653SPeter Holm#include <sys/kernel.h>
135*8a272653SPeter Holm#include <sys/malloc.h>
136*8a272653SPeter Holm#include <sys/module.h>
137*8a272653SPeter Holm#include <sys/proc.h>
138*8a272653SPeter Holm#include <sys/sysctl.h>
139*8a272653SPeter Holm#include <sys/sysent.h>
140*8a272653SPeter Holm#include <sys/sysproto.h>
141*8a272653SPeter Holm#include <sys/systm.h>
142*8a272653SPeter Holm
143*8a272653SPeter Holm#define TALLOC 1
144*8a272653SPeter Holm#define TFREE  2
145*8a272653SPeter Holm
146*8a272653SPeter Holm/*
147*8a272653SPeter Holm * Hook up a syscall for contigmalloc testing.
148*8a272653SPeter Holm */
149*8a272653SPeter Holm
150*8a272653SPeter Holmstruct cmalloc_args {
151*8a272653SPeter Holm        int a_op;
152*8a272653SPeter Holm	void *a_ptr;
153*8a272653SPeter Holm	void *a_size;
154*8a272653SPeter Holm};
155*8a272653SPeter Holm
156*8a272653SPeter Holmstatic int
157*8a272653SPeter Holmcmalloc(struct thread *td, struct cmalloc_args *uap)
158*8a272653SPeter Holm{
159*8a272653SPeter Holm	void *p;
160*8a272653SPeter Holm	unsigned long size;
161*8a272653SPeter Holm	int error;
162*8a272653SPeter Holm
163*8a272653SPeter Holm	error = copyin(uap->a_size, &size, sizeof(size));
164*8a272653SPeter Holm	if (error != 0) {
165*8a272653SPeter Holm		return (error);
166*8a272653SPeter Holm	}
167*8a272653SPeter Holm	switch (uap->a_op) {
168*8a272653SPeter Holm	case TFREE:
169*8a272653SPeter Holm		error = copyin(uap->a_ptr, &p, sizeof(p));
170*8a272653SPeter Holm		if (error == 0) {
171*8a272653SPeter Holm			if (p != NULL)
172*8a272653SPeter Holm				contigfree(p, size, M_TEMP);
173*8a272653SPeter Holm		}
174*8a272653SPeter Holm		return (error);
175*8a272653SPeter Holm
176*8a272653SPeter Holm	case TALLOC:
177*8a272653SPeter Holm		p = contigmalloc(size, M_TEMP, M_NOWAIT, 0ul, ~0ul, 4096, 0);
178*8a272653SPeter Holm		if (p != NULL) {
179*8a272653SPeter Holm			error = copyout(&p, uap->a_ptr, sizeof(p));
180*8a272653SPeter Holm			return (error);
181*8a272653SPeter Holm		}
182*8a272653SPeter Holm		return (ENOMEM);
183*8a272653SPeter Holm	}
184*8a272653SPeter Holm        return (EINVAL);
185*8a272653SPeter Holm}
186*8a272653SPeter Holm
187*8a272653SPeter Holm/*
188*8a272653SPeter Holm * The sysent for the new syscall
189*8a272653SPeter Holm */
190*8a272653SPeter Holmstatic struct sysent cmalloc_sysent = {
191*8a272653SPeter Holm	.sy_narg =  3,				/* sy_narg */
192*8a272653SPeter Holm	.sy_call = (sy_call_t *) cmalloc	/* sy_call */
193*8a272653SPeter Holm};
194*8a272653SPeter Holm
195*8a272653SPeter Holm/*
196*8a272653SPeter Holm * The offset in sysent where the syscall is allocated.
197*8a272653SPeter Holm */
198*8a272653SPeter Holmstatic int cmalloc_offset = NO_SYSCALL;
199*8a272653SPeter Holm
200*8a272653SPeter HolmSYSCTL_INT(_debug, OID_AUTO, cmalloc_offset, CTLFLAG_RD, &cmalloc_offset, 0,
201*8a272653SPeter Holm    "cmalloc syscall number");
202*8a272653SPeter Holm
203*8a272653SPeter Holm/*
204*8a272653SPeter Holm * The function called at load/unload.
205*8a272653SPeter Holm */
206*8a272653SPeter Holm
207*8a272653SPeter Holmstatic int
208*8a272653SPeter Holmcmalloc_load(struct module *module, int cmd, void *arg)
209*8a272653SPeter Holm{
210*8a272653SPeter Holm        int error = 0;
211*8a272653SPeter Holm
212*8a272653SPeter Holm        switch (cmd) {
213*8a272653SPeter Holm        case MOD_LOAD :
214*8a272653SPeter Holm                break;
215*8a272653SPeter Holm        case MOD_UNLOAD :
216*8a272653SPeter Holm                break;
217*8a272653SPeter Holm        default :
218*8a272653SPeter Holm                error = EOPNOTSUPP;
219*8a272653SPeter Holm                break;
220*8a272653SPeter Holm        }
221*8a272653SPeter Holm        return (error);
222*8a272653SPeter Holm}
223*8a272653SPeter Holm
224*8a272653SPeter HolmSYSCALL_MODULE(cmalloc_syscall, &cmalloc_offset, &cmalloc_sysent,
225*8a272653SPeter Holm    cmalloc_load, NULL);
226