xref: /dflybsd-src/sys/kern/kern_mpipe.c (revision e575e508287dfdeb2baf0e6acb1bc7010bef340d)
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 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/slaballoc.h>
41 #include <sys/mbuf.h>
42 #include <sys/vmmeter.h>
43 #include <sys/lock.h>
44 #include <sys/thread.h>
45 #include <sys/globaldata.h>
46 #include <sys/mpipe.h>
47 #include <sys/thread2.h>
48 
49 static MALLOC_DEFINE(M_MPIPEARY, "MPipe Array", "Auxillary MPIPE structure");
50 
51 /*
52  * Initialize a malloc pipeline for the specified malloc type and allocation
53  * size.  Create an array to cache up to nom_count buffers and preallocate
54  * them.
55  */
56 void
57 mpipe_init(malloc_pipe_t mpipe, malloc_type_t type, int bytes,
58 	int nnom, int nmax,
59 	int mpflags,
60 	void (*construct)(void *, void *),
61 	void (*deconstruct)(void *, void *),
62 	void *priv)
63 {
64     int n;
65 
66     if (nnom < 1)
67 	nnom = 1;
68     if (nmax < 0)
69 	nmax = 0x7FFF0000;	/* some very large number */
70     if (nmax < nnom)
71 	nmax = nnom;
72     bzero(mpipe, sizeof(struct malloc_pipe));
73     mpipe->type = type;
74     mpipe->bytes = bytes;
75     mpipe->mpflags = mpflags;
76     mpipe->construct = construct;
77     mpipe->deconstruct = deconstruct;
78     mpipe->priv = priv;
79     if ((mpflags & MPF_NOZERO) == 0)
80 	mpipe->mflags |= M_ZERO;
81     if (mpflags & MPF_INT)
82 	mpipe->mflags |= M_USE_RESERVE | M_USE_INTERRUPT_RESERVE;
83     mpipe->ary_count = nnom;
84     mpipe->max_count = nmax;
85     mpipe->array = kmalloc(nnom * sizeof(mpipe->array[0]), M_MPIPEARY,
86 			    M_WAITOK | M_ZERO);
87 
88     while (mpipe->free_count < nnom) {
89 	n = mpipe->free_count;
90 	mpipe->array[n] = kmalloc(bytes, mpipe->type, M_WAITOK | mpipe->mflags);
91 	if (construct)
92 	    construct(mpipe->array[n], priv);
93 	++mpipe->free_count;
94 	++mpipe->total_count;
95     }
96 
97     lwkt_token_init(&mpipe->token, 1, "mpipe token");
98 }
99 
100 /*
101  * Destroy a previously initialized mpipe.  This routine can also safely be
102  * called on an uninitialized mpipe structure if it was zero'd or mpipe_done()
103  * was previously called on it.
104  */
105 void
106 mpipe_done(malloc_pipe_t mpipe)
107 {
108     void *buf;
109     int n;
110 
111     KKASSERT(mpipe->free_count == mpipe->total_count);	/* no outstanding mem */
112     for (n = mpipe->free_count - 1; n >= 0; --n) {
113 	buf = mpipe->array[n];
114 	mpipe->array[n] = NULL;
115 	KKASSERT(buf != NULL);
116 	if (mpipe->deconstruct)
117 	    mpipe->deconstruct(buf, mpipe->priv);
118 	kfree(buf, mpipe->type);
119     }
120     mpipe->free_count = 0;
121     mpipe->total_count = 0;
122     if (mpipe->array) {
123 	kfree(mpipe->array, M_MPIPEARY);
124 	mpipe->array = NULL;
125     }
126 
127     lwkt_token_uninit(&mpipe->token);
128 }
129 
130 /*
131  * Allocate an entry, nominally non-blocking.  The allocation is guarenteed
132  * to return non-NULL up to the nominal count after which it may return NULL.
133  * Note that the implementation is defined to be allowed to block for short
134  * periods of time.  Use mpipe_alloc_waitok() to guarentee the allocation.
135  */
136 void *
137 mpipe_alloc_nowait(malloc_pipe_t mpipe)
138 {
139     void *buf;
140     int n;
141 
142     lwkt_gettoken(&mpipe->token);
143     if ((n = mpipe->free_count) != 0) {
144 	/*
145 	 * Use a free entry if it exists.
146 	 */
147 	--n;
148 	buf = mpipe->array[n];
149 	mpipe->array[n] = NULL;	/* sanity check, not absolutely needed */
150 	mpipe->free_count = n;
151     } else if (mpipe->total_count >= mpipe->max_count) {
152 	/*
153 	 * Return NULL if we have hit our limit
154 	 */
155 	buf = NULL;
156     } else {
157 	/*
158 	 * Otherwise try to malloc() non-blocking.
159 	 */
160 	buf = kmalloc(mpipe->bytes, mpipe->type, M_NOWAIT | mpipe->mflags);
161 	if (buf) {
162 	    ++mpipe->total_count;
163 	    if (mpipe->construct)
164 	        mpipe->construct(buf, mpipe->priv);
165 	}
166     }
167     lwkt_reltoken(&mpipe->token);
168     return(buf);
169 }
170 
171 /*
172  * Allocate an entry, block until the allocation succeeds.  This may cause
173  * us to block waiting for a prior allocation to be freed.
174  */
175 void *
176 mpipe_alloc_waitok(malloc_pipe_t mpipe)
177 {
178     void *buf;
179     int n;
180     int mfailed;
181 
182     lwkt_gettoken(&mpipe->token);
183     mfailed = 0;
184     for (;;) {
185 	if ((n = mpipe->free_count) != 0) {
186 	    /*
187 	     * Use a free entry if it exists.
188 	     */
189 	    --n;
190 	    buf = mpipe->array[n];
191 	    mpipe->array[n] = NULL;
192 	    mpipe->free_count = n;
193 	    break;
194 	}
195 	if (mpipe->total_count >= mpipe->max_count || mfailed) {
196 	    /*
197 	     * Block if we have hit our limit
198 	     */
199 	    mpipe->pending = 1;
200 	    tsleep(mpipe, 0, "mpipe1", 0);
201 	    continue;
202 	}
203 	/*
204 	 * Otherwise try to malloc() non-blocking.  If that fails loop to
205 	 * recheck, and block instead of trying to malloc() again.
206 	 */
207 	buf = kmalloc(mpipe->bytes, mpipe->type, M_NOWAIT | mpipe->mflags);
208 	if (buf) {
209 	    ++mpipe->total_count;
210 	    if (mpipe->construct)
211 	        mpipe->construct(buf, mpipe->priv);
212 	    break;
213 	}
214 	mfailed = 1;
215     }
216     lwkt_reltoken(&mpipe->token);
217     return(buf);
218 }
219 
220 /*
221  * Free an entry, unblock any waiters.  Allow NULL.
222  */
223 void
224 mpipe_free(malloc_pipe_t mpipe, void *buf)
225 {
226     int n;
227 
228     if (buf == NULL)
229 	return;
230 
231     lwkt_gettoken(&mpipe->token);
232     if ((n = mpipe->free_count) < mpipe->ary_count) {
233 	/*
234 	 * Free slot available in free array (LIFO)
235 	 */
236 	mpipe->array[n] = buf;
237 	++mpipe->free_count;
238 	if ((mpipe->mpflags & (MPF_CACHEDATA|MPF_NOZERO)) == 0)
239 	    bzero(buf, mpipe->bytes);
240 	lwkt_reltoken(&mpipe->token);
241 
242 	/*
243 	 * Wakeup anyone blocked in mpipe_alloc_*().
244 	 */
245 	if (mpipe->pending) {
246 	    mpipe->pending = 0;
247 	    wakeup(mpipe);
248 	}
249     } else {
250 	/*
251 	 * All the free slots are full, free the buffer directly.
252 	 */
253 	--mpipe->total_count;
254 	KKASSERT(mpipe->total_count >= mpipe->free_count);
255 	if (mpipe->deconstruct)
256 	    mpipe->deconstruct(buf, mpipe->priv);
257 	lwkt_reltoken(&mpipe->token);
258 	kfree(buf, mpipe->type);
259     }
260 }
261 
262