xref: /dflybsd-src/sys/kern/kern_mpipe.c (revision c3c96e4421a1087a390825eac6c01c9ed9182387)
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $DragonFly: src/sys/kern/kern_mpipe.c,v 1.9 2006/09/05 00:55:45 dillon Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/slaballoc.h>
43 #include <sys/mbuf.h>
44 #include <sys/vmmeter.h>
45 #include <sys/lock.h>
46 #include <sys/thread.h>
47 #include <sys/globaldata.h>
48 #include <sys/mpipe.h>
49 
50 #include <sys/thread2.h>
51 
52 #define arysize(ary)	(sizeof(ary)/sizeof((ary)[0]))
53 
54 static MALLOC_DEFINE(M_MPIPEARY, "MPipe Array", "Auxillary MPIPE structure");
55 
56 static struct lwkt_token mpipe_token = LWKT_TOKEN_MP_INITIALIZER(mpipe_token);
57 
58 /*
59  * Initialize a malloc pipeline for the specified malloc type and allocation
60  * size.  Create an array to cache up to nom_count buffers and preallocate
61  * them.
62  */
63 void
64 mpipe_init(malloc_pipe_t mpipe, malloc_type_t type, int bytes,
65 	int nnom, int nmax,
66 	int mpflags, void (*deconstruct)(struct malloc_pipe *, void *))
67 {
68     int n;
69 
70     if (nnom < 1)
71 	nnom = 1;
72     if (nmax < 0)
73 	nmax = 0x7FFF0000;	/* some very large number */
74     if (nmax < nnom)
75 	nmax = nnom;
76     bzero(mpipe, sizeof(struct malloc_pipe));
77     mpipe->type = type;
78     mpipe->bytes = bytes;
79     mpipe->mpflags = mpflags;
80     mpipe->deconstruct = deconstruct;
81     if ((mpflags & MPF_NOZERO) == 0)
82 	mpipe->mflags |= M_ZERO;
83     if (mpflags & MPF_INT)
84 	mpipe->mflags |= M_USE_RESERVE | M_USE_INTERRUPT_RESERVE;
85     mpipe->ary_count = nnom;
86     mpipe->max_count = nmax;
87     mpipe->array = kmalloc(nnom * sizeof(mpipe->array[0]), M_MPIPEARY,
88 			    M_WAITOK | M_ZERO);
89 
90     while (mpipe->free_count < nnom) {
91 	n = mpipe->free_count;
92 	mpipe->array[n] = kmalloc(bytes, mpipe->type, M_WAITOK | mpipe->mflags);
93 	++mpipe->free_count;
94 	++mpipe->total_count;
95     }
96 }
97 
98 /*
99  * Destroy a previously initialized mpipe.  This routine can also safely be
100  * called on an uninitialized mpipe structure if it was zero'd or mpipe_done()
101  * was previously called on it.
102  */
103 void
104 mpipe_done(malloc_pipe_t mpipe)
105 {
106     void *buf;
107     int n;
108 
109     KKASSERT(mpipe->free_count == mpipe->total_count);	/* no outstanding mem */
110     for (n = mpipe->free_count - 1; n >= 0; --n) {
111 	buf = mpipe->array[n];
112 	mpipe->array[n] = NULL;
113 	KKASSERT(buf != NULL);
114 	if (mpipe->deconstruct)
115 	    mpipe->deconstruct(mpipe, buf);
116 	kfree(buf, mpipe->type);
117     }
118     mpipe->free_count = 0;
119     mpipe->total_count = 0;
120     if (mpipe->array) {
121 	kfree(mpipe->array, M_MPIPEARY);
122 	mpipe->array = NULL;
123     }
124 }
125 
126 /*
127  * Allocate an entry, nominally non-blocking.  The allocation is guarenteed
128  * to return non-NULL up to the nominal count after which it may return NULL.
129  * Note that the implementation is defined to be allowed to block for short
130  * periods of time.  Use mpipe_alloc_waitok() to guarentee the allocation.
131  */
132 void *
133 mpipe_alloc_nowait(malloc_pipe_t mpipe)
134 {
135     void *buf;
136     int n;
137 
138     lwkt_gettoken(&mpipe_token);
139     if ((n = mpipe->free_count) != 0) {
140 	/*
141 	 * Use a free entry if it exists.
142 	 */
143 	--n;
144 	buf = mpipe->array[n];
145 	mpipe->array[n] = NULL;	/* sanity check, not absolutely needed */
146 	mpipe->free_count = n;
147     } else if (mpipe->total_count >= mpipe->max_count) {
148 	/*
149 	 * Return NULL if we have hit our limit
150 	 */
151 	buf = NULL;
152     } else {
153 	/*
154 	 * Otherwise try to malloc() non-blocking.
155 	 */
156 	buf = kmalloc(mpipe->bytes, mpipe->type, M_NOWAIT | mpipe->mflags);
157 	if (buf)
158 	    ++mpipe->total_count;
159     }
160     lwkt_reltoken(&mpipe_token);
161     return(buf);
162 }
163 
164 /*
165  * Allocate an entry, block until the allocation succeeds.  This may cause
166  * us to block waiting for a prior allocation to be freed.
167  */
168 void *
169 mpipe_alloc_waitok(malloc_pipe_t mpipe)
170 {
171     void *buf;
172     int n;
173     int mfailed;
174 
175     lwkt_gettoken(&mpipe_token);
176     mfailed = 0;
177     for (;;) {
178 	if ((n = mpipe->free_count) != 0) {
179 	    /*
180 	     * Use a free entry if it exists.
181 	     */
182 	    --n;
183 	    buf = mpipe->array[n];
184 	    mpipe->array[n] = NULL;
185 	    mpipe->free_count = n;
186 	    break;
187 	}
188 	if (mpipe->total_count >= mpipe->max_count || mfailed) {
189 	    /*
190 	     * Block if we have hit our limit
191 	     */
192 	    mpipe->pending = 1;
193 	    tsleep(mpipe, 0, "mpipe1", 0);
194 	    continue;
195 	}
196 	/*
197 	 * Otherwise try to malloc() non-blocking.  If that fails loop to
198 	 * recheck, and block instead of trying to malloc() again.
199 	 */
200 	buf = kmalloc(mpipe->bytes, mpipe->type, M_NOWAIT | mpipe->mflags);
201 	if (buf) {
202 	    ++mpipe->total_count;
203 	    break;
204 	}
205 	mfailed = 1;
206     }
207     lwkt_reltoken(&mpipe_token);
208     return(buf);
209 }
210 
211 /*
212  * Free an entry, unblock any waiters.  Allow NULL.
213  */
214 void
215 mpipe_free(malloc_pipe_t mpipe, void *buf)
216 {
217     int n;
218 
219     if (buf == NULL)
220 	return;
221 
222     lwkt_gettoken(&mpipe_token);
223     if ((n = mpipe->free_count) < mpipe->ary_count) {
224 	/*
225 	 * Free slot available in free array (LIFO)
226 	 */
227 	mpipe->array[n] = buf;
228 	++mpipe->free_count;
229 	if ((mpipe->mpflags & (MPF_CACHEDATA|MPF_NOZERO)) == 0)
230 	    bzero(buf, mpipe->bytes);
231 	lwkt_reltoken(&mpipe_token);
232 
233 	/*
234 	 * Wakeup anyone blocked in mpipe_alloc_*().
235 	 */
236 	if (mpipe->pending) {
237 	    mpipe->pending = 0;
238 	    wakeup(mpipe);
239 	}
240     } else {
241 	/*
242 	 * All the free slots are full, free the buffer directly.
243 	 */
244 	--mpipe->total_count;
245 	KKASSERT(mpipe->total_count >= mpipe->free_count);
246 	if (mpipe->deconstruct)
247 	    mpipe->deconstruct(mpipe, buf);
248 	lwkt_reltoken(&mpipe_token);
249 	kfree(buf, mpipe->type);
250     }
251 }
252 
253