4 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/slaballoc.h>
42 #include <sys/vmmeter.h>
44 #include <sys/thread.h>
45 #include <sys/globaldata.h>
46 #include <sys/mpipe.h>
47 #include <sys/kthread.h>
49 #include <sys/thread2.h>
51 struct mpipe_callback
{
52 STAILQ_ENTRY(mpipe_callback
) entry
;
53 void (*func
)(void *arg1
, void *arg2
);
58 static MALLOC_DEFINE(M_MPIPEARY
, "MPipe Array", "Auxiliary MPIPE structure");
60 static void mpipe_thread(void *arg
);
63 * Initialize a malloc pipeline for the specified malloc type and allocation
64 * size. Create an array to cache up to nom_count buffers and preallocate
68 mpipe_init(malloc_pipe_t mpipe
, malloc_type_t type
, int bytes
,
71 void (*construct
)(void *, void *),
72 void (*deconstruct
)(void *, void *),
80 nmax
= 0x7FFF0000; /* some very large number */
83 bzero(mpipe
, sizeof(struct malloc_pipe
));
86 mpipe
->mpflags
= mpflags
;
87 mpipe
->construct
= construct
;
88 mpipe
->deconstruct
= deconstruct
;
90 if ((mpflags
& MPF_NOZERO
) == 0)
91 mpipe
->mflags
|= M_ZERO
;
92 if (mpflags
& MPF_INT
)
93 mpipe
->mflags
|= M_USE_RESERVE
| M_USE_INTERRUPT_RESERVE
;
94 mpipe
->ary_count
= nnom
;
95 mpipe
->max_count
= nmax
;
96 mpipe
->array
= kmalloc(nnom
* sizeof(mpipe
->array
[0]), M_MPIPEARY
,
99 while (mpipe
->free_count
< nnom
) {
100 n
= mpipe
->free_count
;
101 mpipe
->array
[n
] = kmalloc(bytes
, mpipe
->type
, M_WAITOK
| mpipe
->mflags
);
103 construct(mpipe
->array
[n
], priv
);
105 ++mpipe
->total_count
;
107 STAILQ_INIT(&mpipe
->queue
);
109 lwkt_token_init(&mpipe
->token
, "mpipe token");
112 * Create a support thread for the mpipe queue
114 if (mpflags
& MPF_CALLBACK
) {
115 kthread_create(mpipe_thread
, mpipe
, &mpipe
->thread
,
116 "mpipe_%s", type
->ks_shortdesc
);
121 * Destroy a previously initialized mpipe. This routine can also safely be
122 * called on an uninitialized mpipe structure if it was zero'd or mpipe_done()
123 * was previously called on it.
126 mpipe_done(malloc_pipe_t mpipe
)
131 KKASSERT(mpipe
->free_count
== mpipe
->total_count
); /* no outstanding mem */
134 * Clean up the kthread
136 lwkt_gettoken(&mpipe
->token
);
137 mpipe
->mpflags
|= MPF_EXITING
;
138 while (mpipe
->thread
) {
139 wakeup(&mpipe
->queue
);
140 tsleep(mpipe
, 0, "mpipex", 1);
144 * Clean up the mpipe buffers
146 for (n
= mpipe
->free_count
- 1; n
>= 0; --n
) {
147 buf
= mpipe
->array
[n
];
148 mpipe
->array
[n
] = NULL
;
149 KKASSERT(buf
!= NULL
);
150 if (mpipe
->deconstruct
)
151 mpipe
->deconstruct(buf
, mpipe
->priv
);
152 kfree(buf
, mpipe
->type
);
154 mpipe
->free_count
= 0;
155 mpipe
->total_count
= 0;
157 kfree(mpipe
->array
, M_MPIPEARY
);
160 lwkt_reltoken(&mpipe
->token
);
161 lwkt_token_uninit(&mpipe
->token
);
165 * mpipe support thread for request failures when mpipe_alloc_callback()
169 mpipe_thread(void *arg
)
171 malloc_pipe_t mpipe
= arg
;
172 struct mpipe_callback
*mcb
;
174 lwkt_gettoken(&mpipe
->token
);
175 while ((mpipe
->mpflags
& MPF_EXITING
) == 0) {
176 while (mpipe
->free_count
&&
177 (mcb
= STAILQ_FIRST(&mpipe
->queue
)) != NULL
) {
178 STAILQ_REMOVE(&mpipe
->queue
, mcb
, mpipe_callback
, entry
);
179 mcb
->func(mcb
->arg1
, mcb
->arg2
);
180 kfree(mcb
, M_MPIPEARY
);
182 mpipe
->mpflags
|= MPF_QUEUEWAIT
;
183 tsleep(&mpipe
->queue
, 0, "wait", 0);
185 mpipe
->thread
= NULL
;
187 lwkt_reltoken(&mpipe
->token
);
192 * Allocate an entry (inline suppot routine). The allocation is guarenteed
193 * to return non-NULL up to the nominal count after which it may return NULL.
194 * Note that the implementation is defined to be allowed to block for short
197 * Use mpipe_alloc_callback() for non-blocking operation with a callback
198 * Use mpipe_alloc_nowait() for non-blocking operation without a callback
199 * Use mpipe_alloc_waitok() for blocking operation & guarenteed non-NULL
203 _mpipe_alloc_locked(malloc_pipe_t mpipe
, int mfailed
)
208 if ((n
= mpipe
->free_count
) != 0) {
210 * Use a free entry if it exists.
213 buf
= mpipe
->array
[n
];
214 mpipe
->array
[n
] = NULL
; /* sanity check, not absolutely needed */
215 mpipe
->free_count
= n
;
216 } else if (mpipe
->total_count
>= mpipe
->max_count
|| mfailed
) {
218 * Return NULL if we have hit our limit
223 * Otherwise try to malloc() non-blocking.
225 buf
= kmalloc(mpipe
->bytes
, mpipe
->type
, M_NOWAIT
| mpipe
->mflags
);
227 ++mpipe
->total_count
;
228 if (mpipe
->construct
)
229 mpipe
->construct(buf
, mpipe
->priv
);
236 * Nominal non-blocking mpipe allocation
239 mpipe_alloc_nowait(malloc_pipe_t mpipe
)
243 lwkt_gettoken(&mpipe
->token
);
244 buf
= _mpipe_alloc_locked(mpipe
, 0);
245 lwkt_reltoken(&mpipe
->token
);
251 * non-blocking mpipe allocation with callback for retry.
253 * If NULL is returned func(arg) is queued and will be called back when
254 * space is likely (but not necessarily) available.
256 * If non-NULL is returned func(arg) is ignored.
259 mpipe_alloc_callback(malloc_pipe_t mpipe
, void (*func
)(void *arg1
, void *arg2
),
260 void *arg1
, void *arg2
)
262 struct mpipe_callback
*mcb
;
265 lwkt_gettoken(&mpipe
->token
);
266 buf
= _mpipe_alloc_locked(mpipe
, 0);
268 mcb
= kmalloc(sizeof(*mcb
), M_MPIPEARY
, M_INTWAIT
);
269 buf
= _mpipe_alloc_locked(mpipe
, 0);
274 STAILQ_INSERT_TAIL(&mpipe
->queue
, mcb
, entry
);
276 kfree(mcb
, M_MPIPEARY
);
279 lwkt_reltoken(&mpipe
->token
);
285 * This function can be called to nominally wait until resources are
286 * available and mpipe_alloc_nowait() is likely to return non-NULL.
288 * NOTE: mpipe_alloc_nowait() can still return NULL.
291 mpipe_wait(malloc_pipe_t mpipe
)
293 if (mpipe
->free_count
== 0) {
294 lwkt_gettoken(&mpipe
->token
);
295 while ((mpipe
->mpflags
& MPF_EXITING
) == 0) {
296 if (mpipe
->free_count
)
298 mpipe
->mpflags
|= MPF_QUEUEWAIT
;
299 tsleep(&mpipe
->queue
, 0, "wait", 0);
301 lwkt_reltoken(&mpipe
->token
);
306 * Allocate an entry, block until the allocation succeeds. This may cause
307 * us to block waiting for a prior allocation to be freed.
310 mpipe_alloc_waitok(malloc_pipe_t mpipe
)
315 lwkt_gettoken(&mpipe
->token
);
317 while ((buf
= _mpipe_alloc_locked(mpipe
, mfailed
)) == NULL
) {
319 * Block if we have hit our limit
322 tsleep(mpipe
, 0, "mpipe1", 0);
325 lwkt_reltoken(&mpipe
->token
);
331 * Free an entry, unblock any waiters. Allow NULL.
334 mpipe_free(malloc_pipe_t mpipe
, void *buf
)
341 lwkt_gettoken(&mpipe
->token
);
342 if ((n
= mpipe
->free_count
) < mpipe
->ary_count
) {
344 * Free slot available in free array (LIFO)
346 mpipe
->array
[n
] = buf
;
348 if ((mpipe
->mpflags
& (MPF_CACHEDATA
|MPF_NOZERO
)) == 0)
349 bzero(buf
, mpipe
->bytes
);
350 if (mpipe
->mpflags
& MPF_QUEUEWAIT
) {
351 mpipe
->mpflags
&= ~MPF_QUEUEWAIT
;
352 lwkt_reltoken(&mpipe
->token
);
353 wakeup(&mpipe
->queue
);
355 lwkt_reltoken(&mpipe
->token
);
358 * Wakeup anyone blocked in mpipe_alloc_*().
360 if (mpipe
->pending
) {
366 * All the free slots are full, free the buffer directly.
368 --mpipe
->total_count
;
369 KKASSERT(mpipe
->total_count
>= mpipe
->free_count
);
370 if (mpipe
->deconstruct
)
371 mpipe
->deconstruct(buf
, mpipe
->priv
);
372 lwkt_reltoken(&mpipe
->token
);
373 kfree(buf
, mpipe
->type
);