subr_pool.c revision 1.128.2.13 1 /* $NetBSD: subr_pool.c,v 1.128.2.13 2007/11/01 21:10:14 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1999, 2000, 2002, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center, and by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.128.2.13 2007/11/01 21:10:14 ad Exp $");
42
43 #include "opt_pool.h"
44 #include "opt_poollog.h"
45 #include "opt_lockdebug.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/lock.h>
54 #include <sys/pool.h>
55 #include <sys/syslog.h>
56 #include <sys/debug.h>
57 #include <sys/lockdebug.h>
58 #include <sys/xcall.h>
59
60 #include <uvm/uvm.h>
61
62 /*
63 * Pool resource management utility.
64 *
65 * Memory is allocated in pages which are split into pieces according to
66 * the pool item size. Each page is kept on one of three lists in the
67 * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
68 * for empty, full and partially-full pages respectively. The individual
69 * pool items are on a linked list headed by `ph_itemlist' in each page
70 * header. The memory for building the page list is either taken from
71 * the allocated pages themselves (for small pool items) or taken from
72 * an internal pool of page headers (`phpool').
73 */
74
75 /* List of all pools */
76 LIST_HEAD(,pool) pool_head = LIST_HEAD_INITIALIZER(pool_head);
77
78 /* List of all caches. */
79 LIST_HEAD(,pool_cache) pool_cache_head =
80 LIST_HEAD_INITIALIZER(pool_cache_head);
81
82 /* Private pool for page header structures */
83 #define PHPOOL_MAX 8
84 static struct pool phpool[PHPOOL_MAX];
85 #define PHPOOL_FREELIST_NELEM(idx) (((idx) == 0) ? 0 : (1 << (idx)))
86
87 #ifdef POOL_SUBPAGE
88 /* Pool of subpages for use by normal pools. */
89 static struct pool psppool;
90 #endif
91
92 static SLIST_HEAD(, pool_allocator) pa_deferinitq =
93 SLIST_HEAD_INITIALIZER(pa_deferinitq);
94
95 static void *pool_page_alloc_meta(struct pool *, int);
96 static void pool_page_free_meta(struct pool *, void *);
97
98 /* allocator for pool metadata */
99 struct pool_allocator pool_allocator_meta = {
100 pool_page_alloc_meta, pool_page_free_meta,
101 .pa_backingmapptr = &kmem_map,
102 };
103
104 /* # of seconds to retain page after last use */
105 int pool_inactive_time = 10;
106
107 /* Next candidate for drainage (see pool_drain()) */
108 static struct pool *drainpp;
109
110 /* This lock protects both pool_head and drainpp. */
111 static kmutex_t pool_head_lock;
112 static kcondvar_t pool_busy;
113
114 typedef uint8_t pool_item_freelist_t;
115
116 struct pool_item_header {
117 /* Page headers */
118 LIST_ENTRY(pool_item_header)
119 ph_pagelist; /* pool page list */
120 SPLAY_ENTRY(pool_item_header)
121 ph_node; /* Off-page page headers */
122 void * ph_page; /* this page's address */
123 struct timeval ph_time; /* last referenced */
124 union {
125 /* !PR_NOTOUCH */
126 struct {
127 LIST_HEAD(, pool_item)
128 phu_itemlist; /* chunk list for this page */
129 } phu_normal;
130 /* PR_NOTOUCH */
131 struct {
132 uint16_t
133 phu_off; /* start offset in page */
134 pool_item_freelist_t
135 phu_firstfree; /* first free item */
136 /*
137 * XXX it might be better to use
138 * a simple bitmap and ffs(3)
139 */
140 } phu_notouch;
141 } ph_u;
142 uint16_t ph_nmissing; /* # of chunks in use */
143 };
144 #define ph_itemlist ph_u.phu_normal.phu_itemlist
145 #define ph_off ph_u.phu_notouch.phu_off
146 #define ph_firstfree ph_u.phu_notouch.phu_firstfree
147
148 struct pool_item {
149 #ifdef DIAGNOSTIC
150 u_int pi_magic;
151 #endif
152 #define PI_MAGIC 0xdeaddeadU
153 /* Other entries use only this list entry */
154 LIST_ENTRY(pool_item) pi_list;
155 };
156
157 #define POOL_NEEDS_CATCHUP(pp) \
158 ((pp)->pr_nitems < (pp)->pr_minitems)
159
160 /*
161 * Pool cache management.
162 *
163 * Pool caches provide a way for constructed objects to be cached by the
164 * pool subsystem. This can lead to performance improvements by avoiding
165 * needless object construction/destruction; it is deferred until absolutely
166 * necessary.
167 *
168 * Caches are grouped into cache groups. Each cache group references up
169 * to PCG_NUMOBJECTS constructed objects. When a cache allocates an
170 * object from the pool, it calls the object's constructor and places it
171 * into a cache group. When a cache group frees an object back to the
172 * pool, it first calls the object's destructor. This allows the object
173 * to persist in constructed form while freed to the cache.
174 *
175 * The pool references each cache, so that when a pool is drained by the
176 * pagedaemon, it can drain each individual cache as well. Each time a
177 * cache is drained, the most idle cache group is freed to the pool in
178 * its entirety.
179 *
180 * Pool caches are layed on top of pools. By layering them, we can avoid
181 * the complexity of cache management for pools which would not benefit
182 * from it.
183 */
184
185 static struct pool pcgpool;
186 static struct pool cache_pool;
187 static struct pool cache_cpu_pool;
188
189 static pool_cache_cpu_t *pool_cache_put_slow(pool_cache_cpu_t *, int *,
190 void *, paddr_t);
191 static pool_cache_cpu_t *pool_cache_get_slow(pool_cache_cpu_t *, int *,
192 void **, paddr_t *, int);
193 static void pool_cache_cpu_init1(struct cpu_info *, pool_cache_t);
194 static void pool_cache_invalidate_groups(pool_cache_t, pcg_t *);
195 static void pool_cache_xcall(pool_cache_t);
196
197 static int pool_catchup(struct pool *);
198 static void pool_prime_page(struct pool *, void *,
199 struct pool_item_header *);
200 static void pool_update_curpage(struct pool *);
201
202 static int pool_grow(struct pool *, int);
203 static void *pool_allocator_alloc(struct pool *, int);
204 static void pool_allocator_free(struct pool *, void *);
205
206 static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
207 void (*)(const char *, ...));
208 static void pool_print1(struct pool *, const char *,
209 void (*)(const char *, ...));
210
211 static int pool_chk_page(struct pool *, const char *,
212 struct pool_item_header *);
213
214 /*
215 * Pool log entry. An array of these is allocated in pool_init().
216 */
217 struct pool_log {
218 const char *pl_file;
219 long pl_line;
220 int pl_action;
221 #define PRLOG_GET 1
222 #define PRLOG_PUT 2
223 void *pl_addr;
224 };
225
226 #ifdef POOL_DIAGNOSTIC
227 /* Number of entries in pool log buffers */
228 #ifndef POOL_LOGSIZE
229 #define POOL_LOGSIZE 10
230 #endif
231
232 int pool_logsize = POOL_LOGSIZE;
233
234 static inline void
235 pr_log(struct pool *pp, void *v, int action, const char *file, long line)
236 {
237 int n = pp->pr_curlogentry;
238 struct pool_log *pl;
239
240 if ((pp->pr_roflags & PR_LOGGING) == 0)
241 return;
242
243 /*
244 * Fill in the current entry. Wrap around and overwrite
245 * the oldest entry if necessary.
246 */
247 pl = &pp->pr_log[n];
248 pl->pl_file = file;
249 pl->pl_line = line;
250 pl->pl_action = action;
251 pl->pl_addr = v;
252 if (++n >= pp->pr_logsize)
253 n = 0;
254 pp->pr_curlogentry = n;
255 }
256
257 static void
258 pr_printlog(struct pool *pp, struct pool_item *pi,
259 void (*pr)(const char *, ...))
260 {
261 int i = pp->pr_logsize;
262 int n = pp->pr_curlogentry;
263
264 if ((pp->pr_roflags & PR_LOGGING) == 0)
265 return;
266
267 /*
268 * Print all entries in this pool's log.
269 */
270 while (i-- > 0) {
271 struct pool_log *pl = &pp->pr_log[n];
272 if (pl->pl_action != 0) {
273 if (pi == NULL || pi == pl->pl_addr) {
274 (*pr)("\tlog entry %d:\n", i);
275 (*pr)("\t\taction = %s, addr = %p\n",
276 pl->pl_action == PRLOG_GET ? "get" : "put",
277 pl->pl_addr);
278 (*pr)("\t\tfile: %s at line %lu\n",
279 pl->pl_file, pl->pl_line);
280 }
281 }
282 if (++n >= pp->pr_logsize)
283 n = 0;
284 }
285 }
286
287 static inline void
288 pr_enter(struct pool *pp, const char *file, long line)
289 {
290
291 if (__predict_false(pp->pr_entered_file != NULL)) {
292 printf("pool %s: reentrancy at file %s line %ld\n",
293 pp->pr_wchan, file, line);
294 printf(" previous entry at file %s line %ld\n",
295 pp->pr_entered_file, pp->pr_entered_line);
296 panic("pr_enter");
297 }
298
299 pp->pr_entered_file = file;
300 pp->pr_entered_line = line;
301 }
302
303 static inline void
304 pr_leave(struct pool *pp)
305 {
306
307 if (__predict_false(pp->pr_entered_file == NULL)) {
308 printf("pool %s not entered?\n", pp->pr_wchan);
309 panic("pr_leave");
310 }
311
312 pp->pr_entered_file = NULL;
313 pp->pr_entered_line = 0;
314 }
315
316 static inline void
317 pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
318 {
319
320 if (pp->pr_entered_file != NULL)
321 (*pr)("\n\tcurrently entered from file %s line %ld\n",
322 pp->pr_entered_file, pp->pr_entered_line);
323 }
324 #else
325 #define pr_log(pp, v, action, file, line)
326 #define pr_printlog(pp, pi, pr)
327 #define pr_enter(pp, file, line)
328 #define pr_leave(pp)
329 #define pr_enter_check(pp, pr)
330 #endif /* POOL_DIAGNOSTIC */
331
332 static inline int
333 pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
334 const void *v)
335 {
336 const char *cp = v;
337 int idx;
338
339 KASSERT(pp->pr_roflags & PR_NOTOUCH);
340 idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
341 KASSERT(idx < pp->pr_itemsperpage);
342 return idx;
343 }
344
345 #define PR_FREELIST_ALIGN(p) \
346 roundup((uintptr_t)(p), sizeof(pool_item_freelist_t))
347 #define PR_FREELIST(ph) ((pool_item_freelist_t *)PR_FREELIST_ALIGN((ph) + 1))
348 #define PR_INDEX_USED ((pool_item_freelist_t)-1)
349 #define PR_INDEX_EOL ((pool_item_freelist_t)-2)
350
351 static inline void
352 pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
353 void *obj)
354 {
355 int idx = pr_item_notouch_index(pp, ph, obj);
356 pool_item_freelist_t *freelist = PR_FREELIST(ph);
357
358 KASSERT(freelist[idx] == PR_INDEX_USED);
359 freelist[idx] = ph->ph_firstfree;
360 ph->ph_firstfree = idx;
361 }
362
363 static inline void *
364 pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
365 {
366 int idx = ph->ph_firstfree;
367 pool_item_freelist_t *freelist = PR_FREELIST(ph);
368
369 KASSERT(freelist[idx] != PR_INDEX_USED);
370 ph->ph_firstfree = freelist[idx];
371 freelist[idx] = PR_INDEX_USED;
372
373 return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
374 }
375
376 static inline int
377 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
378 {
379
380 /*
381 * we consider pool_item_header with smaller ph_page bigger.
382 * (this unnatural ordering is for the benefit of pr_find_pagehead.)
383 */
384
385 if (a->ph_page < b->ph_page)
386 return (1);
387 else if (a->ph_page > b->ph_page)
388 return (-1);
389 else
390 return (0);
391 }
392
393 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
394 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
395
396 /*
397 * Return the pool page header based on item address.
398 */
399 static inline struct pool_item_header *
400 pr_find_pagehead(struct pool *pp, void *v)
401 {
402 struct pool_item_header *ph, tmp;
403
404 if ((pp->pr_roflags & PR_NOALIGN) != 0) {
405 tmp.ph_page = (void *)(uintptr_t)v;
406 ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
407 if (ph == NULL) {
408 ph = SPLAY_ROOT(&pp->pr_phtree);
409 if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
410 ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
411 }
412 KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
413 }
414 } else {
415 void *page =
416 (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
417
418 if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
419 ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
420 } else {
421 tmp.ph_page = page;
422 ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
423 }
424 }
425
426 KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
427 ((char *)ph->ph_page <= (char *)v &&
428 (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
429 return ph;
430 }
431
432 static void
433 pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
434 {
435 struct pool_item_header *ph;
436
437 while ((ph = LIST_FIRST(pq)) != NULL) {
438 LIST_REMOVE(ph, ph_pagelist);
439 pool_allocator_free(pp, ph->ph_page);
440 if ((pp->pr_roflags & PR_PHINPAGE) == 0)
441 pool_put(pp->pr_phpool, ph);
442 }
443 }
444
445 /*
446 * Remove a page from the pool.
447 */
448 static inline void
449 pr_rmpage(struct pool *pp, struct pool_item_header *ph,
450 struct pool_pagelist *pq)
451 {
452
453 KASSERT(mutex_owned(&pp->pr_lock));
454
455 /*
456 * If the page was idle, decrement the idle page count.
457 */
458 if (ph->ph_nmissing == 0) {
459 #ifdef DIAGNOSTIC
460 if (pp->pr_nidle == 0)
461 panic("pr_rmpage: nidle inconsistent");
462 if (pp->pr_nitems < pp->pr_itemsperpage)
463 panic("pr_rmpage: nitems inconsistent");
464 #endif
465 pp->pr_nidle--;
466 }
467
468 pp->pr_nitems -= pp->pr_itemsperpage;
469
470 /*
471 * Unlink the page from the pool and queue it for release.
472 */
473 LIST_REMOVE(ph, ph_pagelist);
474 if ((pp->pr_roflags & PR_PHINPAGE) == 0)
475 SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
476 LIST_INSERT_HEAD(pq, ph, ph_pagelist);
477
478 pp->pr_npages--;
479 pp->pr_npagefree++;
480
481 pool_update_curpage(pp);
482 }
483
484 static bool
485 pa_starved_p(struct pool_allocator *pa)
486 {
487
488 if (pa->pa_backingmap != NULL) {
489 return vm_map_starved_p(pa->pa_backingmap);
490 }
491 return false;
492 }
493
494 static int
495 pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
496 {
497 struct pool *pp = obj;
498 struct pool_allocator *pa = pp->pr_alloc;
499
500 KASSERT(&pp->pr_reclaimerentry == ce);
501 pool_reclaim(pp);
502 if (!pa_starved_p(pa)) {
503 return CALLBACK_CHAIN_ABORT;
504 }
505 return CALLBACK_CHAIN_CONTINUE;
506 }
507
508 static void
509 pool_reclaim_register(struct pool *pp)
510 {
511 struct vm_map *map = pp->pr_alloc->pa_backingmap;
512 int s;
513
514 if (map == NULL) {
515 return;
516 }
517
518 s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
519 callback_register(&vm_map_to_kernel(map)->vmk_reclaim_callback,
520 &pp->pr_reclaimerentry, pp, pool_reclaim_callback);
521 splx(s);
522 }
523
524 static void
525 pool_reclaim_unregister(struct pool *pp)
526 {
527 struct vm_map *map = pp->pr_alloc->pa_backingmap;
528 int s;
529
530 if (map == NULL) {
531 return;
532 }
533
534 s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
535 callback_unregister(&vm_map_to_kernel(map)->vmk_reclaim_callback,
536 &pp->pr_reclaimerentry);
537 splx(s);
538 }
539
540 static void
541 pa_reclaim_register(struct pool_allocator *pa)
542 {
543 struct vm_map *map = *pa->pa_backingmapptr;
544 struct pool *pp;
545
546 KASSERT(pa->pa_backingmap == NULL);
547 if (map == NULL) {
548 SLIST_INSERT_HEAD(&pa_deferinitq, pa, pa_q);
549 return;
550 }
551 pa->pa_backingmap = map;
552 TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
553 pool_reclaim_register(pp);
554 }
555 }
556
557 /*
558 * Initialize all the pools listed in the "pools" link set.
559 */
560 void
561 pool_subsystem_init(void)
562 {
563 struct pool_allocator *pa;
564 __link_set_decl(pools, struct link_pool_init);
565 struct link_pool_init * const *pi;
566
567 mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
568 cv_init(&pool_busy, "poolbusy");
569
570 __link_set_foreach(pi, pools)
571 pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
572 (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
573 (*pi)->palloc, (*pi)->ipl);
574
575 while ((pa = SLIST_FIRST(&pa_deferinitq)) != NULL) {
576 KASSERT(pa->pa_backingmapptr != NULL);
577 KASSERT(*pa->pa_backingmapptr != NULL);
578 SLIST_REMOVE_HEAD(&pa_deferinitq, pa_q);
579 pa_reclaim_register(pa);
580 }
581
582 pool_init(&cache_pool, sizeof(struct pool_cache), CACHE_LINE_SIZE,
583 0, 0, "pcache", &pool_allocator_nointr, IPL_NONE);
584
585 pool_init(&cache_cpu_pool, sizeof(pool_cache_cpu_t), CACHE_LINE_SIZE,
586 0, 0, "pcachecpu", &pool_allocator_nointr, IPL_NONE);
587 }
588
589 /*
590 * Initialize the given pool resource structure.
591 *
592 * We export this routine to allow other kernel parts to declare
593 * static pools that must be initialized before malloc() is available.
594 */
595 void
596 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
597 const char *wchan, struct pool_allocator *palloc, int ipl)
598 {
599 #ifdef DEBUG
600 struct pool *pp1;
601 #endif
602 size_t trysize, phsize;
603 int off, slack;
604
605 KASSERT((1UL << (CHAR_BIT * sizeof(pool_item_freelist_t))) - 2 >=
606 PHPOOL_FREELIST_NELEM(PHPOOL_MAX - 1));
607
608 #ifdef DEBUG
609 /*
610 * Check that the pool hasn't already been initialised and
611 * added to the list of all pools.
612 */
613 LIST_FOREACH(pp1, &pool_head, pr_poollist) {
614 if (pp == pp1)
615 panic("pool_init: pool %s already initialised",
616 wchan);
617 }
618 #endif
619
620 #ifdef POOL_DIAGNOSTIC
621 /*
622 * Always log if POOL_DIAGNOSTIC is defined.
623 */
624 if (pool_logsize != 0)
625 flags |= PR_LOGGING;
626 #endif
627
628 if (palloc == NULL)
629 palloc = &pool_allocator_kmem;
630 #ifdef POOL_SUBPAGE
631 if (size > palloc->pa_pagesz) {
632 if (palloc == &pool_allocator_kmem)
633 palloc = &pool_allocator_kmem_fullpage;
634 else if (palloc == &pool_allocator_nointr)
635 palloc = &pool_allocator_nointr_fullpage;
636 }
637 #endif /* POOL_SUBPAGE */
638 if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
639 if (palloc->pa_pagesz == 0)
640 palloc->pa_pagesz = PAGE_SIZE;
641
642 TAILQ_INIT(&palloc->pa_list);
643
644 mutex_init(&palloc->pa_lock, MUTEX_DEFAULT, IPL_VM);
645 palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
646 palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
647
648 if (palloc->pa_backingmapptr != NULL) {
649 pa_reclaim_register(palloc);
650 }
651 palloc->pa_flags |= PA_INITIALIZED;
652 }
653
654 if (align == 0)
655 align = ALIGN(1);
656
657 if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
658 size = sizeof(struct pool_item);
659
660 size = roundup(size, align);
661 #ifdef DIAGNOSTIC
662 if (size > palloc->pa_pagesz)
663 panic("pool_init: pool item size (%zu) too large", size);
664 #endif
665
666 /*
667 * Initialize the pool structure.
668 */
669 LIST_INIT(&pp->pr_emptypages);
670 LIST_INIT(&pp->pr_fullpages);
671 LIST_INIT(&pp->pr_partpages);
672 pp->pr_cache = NULL;
673 pp->pr_curpage = NULL;
674 pp->pr_npages = 0;
675 pp->pr_minitems = 0;
676 pp->pr_minpages = 0;
677 pp->pr_maxpages = UINT_MAX;
678 pp->pr_roflags = flags;
679 pp->pr_flags = 0;
680 pp->pr_size = size;
681 pp->pr_align = align;
682 pp->pr_wchan = wchan;
683 pp->pr_alloc = palloc;
684 pp->pr_nitems = 0;
685 pp->pr_nout = 0;
686 pp->pr_hardlimit = UINT_MAX;
687 pp->pr_hardlimit_warning = NULL;
688 pp->pr_hardlimit_ratecap.tv_sec = 0;
689 pp->pr_hardlimit_ratecap.tv_usec = 0;
690 pp->pr_hardlimit_warning_last.tv_sec = 0;
691 pp->pr_hardlimit_warning_last.tv_usec = 0;
692 pp->pr_drain_hook = NULL;
693 pp->pr_drain_hook_arg = NULL;
694 pp->pr_freecheck = NULL;
695
696 /*
697 * Decide whether to put the page header off page to avoid
698 * wasting too large a part of the page or too big item.
699 * Off-page page headers go on a hash table, so we can match
700 * a returned item with its header based on the page address.
701 * We use 1/16 of the page size and about 8 times of the item
702 * size as the threshold (XXX: tune)
703 *
704 * However, we'll put the header into the page if we can put
705 * it without wasting any items.
706 *
707 * Silently enforce `0 <= ioff < align'.
708 */
709 pp->pr_itemoffset = ioff %= align;
710 /* See the comment below about reserved bytes. */
711 trysize = palloc->pa_pagesz - ((align - ioff) % align);
712 phsize = ALIGN(sizeof(struct pool_item_header));
713 if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
714 (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
715 trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
716 /* Use the end of the page for the page header */
717 pp->pr_roflags |= PR_PHINPAGE;
718 pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
719 } else {
720 /* The page header will be taken from our page header pool */
721 pp->pr_phoffset = 0;
722 off = palloc->pa_pagesz;
723 SPLAY_INIT(&pp->pr_phtree);
724 }
725
726 /*
727 * Alignment is to take place at `ioff' within the item. This means
728 * we must reserve up to `align - 1' bytes on the page to allow
729 * appropriate positioning of each item.
730 */
731 pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
732 KASSERT(pp->pr_itemsperpage != 0);
733 if ((pp->pr_roflags & PR_NOTOUCH)) {
734 int idx;
735
736 for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
737 idx++) {
738 /* nothing */
739 }
740 if (idx >= PHPOOL_MAX) {
741 /*
742 * if you see this panic, consider to tweak
743 * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
744 */
745 panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
746 pp->pr_wchan, pp->pr_itemsperpage);
747 }
748 pp->pr_phpool = &phpool[idx];
749 } else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
750 pp->pr_phpool = &phpool[0];
751 }
752 #if defined(DIAGNOSTIC)
753 else {
754 pp->pr_phpool = NULL;
755 }
756 #endif
757
758 /*
759 * Use the slack between the chunks and the page header
760 * for "cache coloring".
761 */
762 slack = off - pp->pr_itemsperpage * pp->pr_size;
763 pp->pr_maxcolor = (slack / align) * align;
764 pp->pr_curcolor = 0;
765
766 pp->pr_nget = 0;
767 pp->pr_nfail = 0;
768 pp->pr_nput = 0;
769 pp->pr_npagealloc = 0;
770 pp->pr_npagefree = 0;
771 pp->pr_hiwat = 0;
772 pp->pr_nidle = 0;
773 pp->pr_refcnt = 0;
774
775 #ifdef POOL_DIAGNOSTIC
776 if (flags & PR_LOGGING) {
777 if (kmem_map == NULL ||
778 (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
779 M_TEMP, M_NOWAIT)) == NULL)
780 pp->pr_roflags &= ~PR_LOGGING;
781 pp->pr_curlogentry = 0;
782 pp->pr_logsize = pool_logsize;
783 }
784 #endif
785
786 pp->pr_entered_file = NULL;
787 pp->pr_entered_line = 0;
788
789 mutex_init(&pp->pr_lock, MUTEX_DEFAULT, ipl);
790 cv_init(&pp->pr_cv, wchan);
791 pp->pr_ipl = ipl;
792
793 /*
794 * Initialize private page header pool and cache magazine pool if we
795 * haven't done so yet.
796 * XXX LOCKING.
797 */
798 if (phpool[0].pr_size == 0) {
799 int idx;
800 for (idx = 0; idx < PHPOOL_MAX; idx++) {
801 static char phpool_names[PHPOOL_MAX][6+1+6+1];
802 int nelem;
803 size_t sz;
804
805 nelem = PHPOOL_FREELIST_NELEM(idx);
806 snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
807 "phpool-%d", nelem);
808 sz = sizeof(struct pool_item_header);
809 if (nelem) {
810 sz = PR_FREELIST_ALIGN(sz)
811 + nelem * sizeof(pool_item_freelist_t);
812 }
813 pool_init(&phpool[idx], sz, 0, 0, 0,
814 phpool_names[idx], &pool_allocator_meta, IPL_VM);
815 }
816 #ifdef POOL_SUBPAGE
817 pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
818 PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
819 #endif
820 pool_init(&pcgpool, sizeof(pcg_t), CACHE_LINE_SIZE, 0, 0,
821 "cachegrp", &pool_allocator_meta, IPL_VM);
822 }
823
824 if (__predict_true(!cold)) {
825 /* Insert into the list of all pools. */
826 mutex_enter(&pool_head_lock);
827 LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
828 mutex_exit(&pool_head_lock);
829
830 /* Insert this into the list of pools using this allocator. */
831 mutex_enter(&palloc->pa_lock);
832 TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
833 mutex_exit(&palloc->pa_lock);
834 } else {
835 LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
836 TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
837 }
838
839 pool_reclaim_register(pp);
840 }
841
842 /*
843 * De-commision a pool resource.
844 */
845 void
846 pool_destroy(struct pool *pp)
847 {
848 struct pool_pagelist pq;
849 struct pool_item_header *ph;
850
851 /* Remove from global pool list */
852 mutex_enter(&pool_head_lock);
853 while (pp->pr_refcnt != 0)
854 cv_wait(&pool_busy, &pool_head_lock);
855 LIST_REMOVE(pp, pr_poollist);
856 if (drainpp == pp)
857 drainpp = NULL;
858 mutex_exit(&pool_head_lock);
859
860 /* Remove this pool from its allocator's list of pools. */
861 pool_reclaim_unregister(pp);
862 mutex_enter(&pp->pr_alloc->pa_lock);
863 TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
864 mutex_exit(&pp->pr_alloc->pa_lock);
865
866 mutex_enter(&pp->pr_lock);
867
868 KASSERT(pp->pr_cache == NULL);
869
870 #ifdef DIAGNOSTIC
871 if (pp->pr_nout != 0) {
872 pr_printlog(pp, NULL, printf);
873 panic("pool_destroy: pool busy: still out: %u",
874 pp->pr_nout);
875 }
876 #endif
877
878 KASSERT(LIST_EMPTY(&pp->pr_fullpages));
879 KASSERT(LIST_EMPTY(&pp->pr_partpages));
880
881 /* Remove all pages */
882 LIST_INIT(&pq);
883 while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
884 pr_rmpage(pp, ph, &pq);
885
886 mutex_exit(&pp->pr_lock);
887
888 pr_pagelist_free(pp, &pq);
889
890 #ifdef POOL_DIAGNOSTIC
891 if ((pp->pr_roflags & PR_LOGGING) != 0)
892 free(pp->pr_log, M_TEMP);
893 #endif
894
895 cv_destroy(&pp->pr_cv);
896 mutex_destroy(&pp->pr_lock);
897 }
898
899 void
900 pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
901 {
902
903 /* XXX no locking -- must be used just after pool_init() */
904 #ifdef DIAGNOSTIC
905 if (pp->pr_drain_hook != NULL)
906 panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
907 #endif
908 pp->pr_drain_hook = fn;
909 pp->pr_drain_hook_arg = arg;
910 }
911
912 static struct pool_item_header *
913 pool_alloc_item_header(struct pool *pp, void *storage, int flags)
914 {
915 struct pool_item_header *ph;
916
917 if ((pp->pr_roflags & PR_PHINPAGE) != 0)
918 ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
919 else
920 ph = pool_get(pp->pr_phpool, flags);
921
922 return (ph);
923 }
924
925 /*
926 * Grab an item from the pool.
927 */
928 void *
929 #ifdef POOL_DIAGNOSTIC
930 _pool_get(struct pool *pp, int flags, const char *file, long line)
931 #else
932 pool_get(struct pool *pp, int flags)
933 #endif
934 {
935 struct pool_item *pi;
936 struct pool_item_header *ph;
937 void *v;
938
939 #ifdef DIAGNOSTIC
940 if (__predict_false(pp->pr_itemsperpage == 0))
941 panic("pool_get: pool %p: pr_itemsperpage is zero, "
942 "pool not initialized?", pp);
943 if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
944 (flags & PR_WAITOK) != 0))
945 panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
946
947 #endif /* DIAGNOSTIC */
948 #ifdef LOCKDEBUG
949 if (flags & PR_WAITOK)
950 ASSERT_SLEEPABLE(NULL, "pool_get(PR_WAITOK)");
951 #endif
952
953 mutex_enter(&pp->pr_lock);
954 pr_enter(pp, file, line);
955
956 startover:
957 /*
958 * Check to see if we've reached the hard limit. If we have,
959 * and we can wait, then wait until an item has been returned to
960 * the pool.
961 */
962 #ifdef DIAGNOSTIC
963 if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
964 pr_leave(pp);
965 mutex_exit(&pp->pr_lock);
966 panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
967 }
968 #endif
969 if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
970 if (pp->pr_drain_hook != NULL) {
971 /*
972 * Since the drain hook is going to free things
973 * back to the pool, unlock, call the hook, re-lock,
974 * and check the hardlimit condition again.
975 */
976 pr_leave(pp);
977 mutex_exit(&pp->pr_lock);
978 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
979 mutex_enter(&pp->pr_lock);
980 pr_enter(pp, file, line);
981 if (pp->pr_nout < pp->pr_hardlimit)
982 goto startover;
983 }
984
985 if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
986 /*
987 * XXX: A warning isn't logged in this case. Should
988 * it be?
989 */
990 pp->pr_flags |= PR_WANTED;
991 pr_leave(pp);
992 cv_wait(&pp->pr_cv, &pp->pr_lock);
993 pr_enter(pp, file, line);
994 goto startover;
995 }
996
997 /*
998 * Log a message that the hard limit has been hit.
999 */
1000 if (pp->pr_hardlimit_warning != NULL &&
1001 ratecheck(&pp->pr_hardlimit_warning_last,
1002 &pp->pr_hardlimit_ratecap))
1003 log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1004
1005 pp->pr_nfail++;
1006
1007 pr_leave(pp);
1008 mutex_exit(&pp->pr_lock);
1009 return (NULL);
1010 }
1011
1012 /*
1013 * The convention we use is that if `curpage' is not NULL, then
1014 * it points at a non-empty bucket. In particular, `curpage'
1015 * never points at a page header which has PR_PHINPAGE set and
1016 * has no items in its bucket.
1017 */
1018 if ((ph = pp->pr_curpage) == NULL) {
1019 int error;
1020
1021 #ifdef DIAGNOSTIC
1022 if (pp->pr_nitems != 0) {
1023 mutex_exit(&pp->pr_lock);
1024 printf("pool_get: %s: curpage NULL, nitems %u\n",
1025 pp->pr_wchan, pp->pr_nitems);
1026 panic("pool_get: nitems inconsistent");
1027 }
1028 #endif
1029
1030 /*
1031 * Call the back-end page allocator for more memory.
1032 * Release the pool lock, as the back-end page allocator
1033 * may block.
1034 */
1035 pr_leave(pp);
1036 error = pool_grow(pp, flags);
1037 pr_enter(pp, file, line);
1038 if (error != 0) {
1039 /*
1040 * We were unable to allocate a page or item
1041 * header, but we released the lock during
1042 * allocation, so perhaps items were freed
1043 * back to the pool. Check for this case.
1044 */
1045 if (pp->pr_curpage != NULL)
1046 goto startover;
1047
1048 pp->pr_nfail++;
1049 pr_leave(pp);
1050 mutex_exit(&pp->pr_lock);
1051 return (NULL);
1052 }
1053
1054 /* Start the allocation process over. */
1055 goto startover;
1056 }
1057 if (pp->pr_roflags & PR_NOTOUCH) {
1058 #ifdef DIAGNOSTIC
1059 if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1060 pr_leave(pp);
1061 mutex_exit(&pp->pr_lock);
1062 panic("pool_get: %s: page empty", pp->pr_wchan);
1063 }
1064 #endif
1065 v = pr_item_notouch_get(pp, ph);
1066 #ifdef POOL_DIAGNOSTIC
1067 pr_log(pp, v, PRLOG_GET, file, line);
1068 #endif
1069 } else {
1070 v = pi = LIST_FIRST(&ph->ph_itemlist);
1071 if (__predict_false(v == NULL)) {
1072 pr_leave(pp);
1073 mutex_exit(&pp->pr_lock);
1074 panic("pool_get: %s: page empty", pp->pr_wchan);
1075 }
1076 #ifdef DIAGNOSTIC
1077 if (__predict_false(pp->pr_nitems == 0)) {
1078 pr_leave(pp);
1079 mutex_exit(&pp->pr_lock);
1080 printf("pool_get: %s: items on itemlist, nitems %u\n",
1081 pp->pr_wchan, pp->pr_nitems);
1082 panic("pool_get: nitems inconsistent");
1083 }
1084 #endif
1085
1086 #ifdef POOL_DIAGNOSTIC
1087 pr_log(pp, v, PRLOG_GET, file, line);
1088 #endif
1089
1090 #ifdef DIAGNOSTIC
1091 if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1092 pr_printlog(pp, pi, printf);
1093 panic("pool_get(%s): free list modified: "
1094 "magic=%x; page %p; item addr %p\n",
1095 pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1096 }
1097 #endif
1098
1099 /*
1100 * Remove from item list.
1101 */
1102 LIST_REMOVE(pi, pi_list);
1103 }
1104 pp->pr_nitems--;
1105 pp->pr_nout++;
1106 if (ph->ph_nmissing == 0) {
1107 #ifdef DIAGNOSTIC
1108 if (__predict_false(pp->pr_nidle == 0))
1109 panic("pool_get: nidle inconsistent");
1110 #endif
1111 pp->pr_nidle--;
1112
1113 /*
1114 * This page was previously empty. Move it to the list of
1115 * partially-full pages. This page is already curpage.
1116 */
1117 LIST_REMOVE(ph, ph_pagelist);
1118 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1119 }
1120 ph->ph_nmissing++;
1121 if (ph->ph_nmissing == pp->pr_itemsperpage) {
1122 #ifdef DIAGNOSTIC
1123 if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1124 !LIST_EMPTY(&ph->ph_itemlist))) {
1125 pr_leave(pp);
1126 mutex_exit(&pp->pr_lock);
1127 panic("pool_get: %s: nmissing inconsistent",
1128 pp->pr_wchan);
1129 }
1130 #endif
1131 /*
1132 * This page is now full. Move it to the full list
1133 * and select a new current page.
1134 */
1135 LIST_REMOVE(ph, ph_pagelist);
1136 LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
1137 pool_update_curpage(pp);
1138 }
1139
1140 pp->pr_nget++;
1141 pr_leave(pp);
1142
1143 /*
1144 * If we have a low water mark and we are now below that low
1145 * water mark, add more items to the pool.
1146 */
1147 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1148 /*
1149 * XXX: Should we log a warning? Should we set up a timeout
1150 * to try again in a second or so? The latter could break
1151 * a caller's assumptions about interrupt protection, etc.
1152 */
1153 }
1154
1155 mutex_exit(&pp->pr_lock);
1156 KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
1157 FREECHECK_OUT(&pp->pr_freecheck, v);
1158 return (v);
1159 }
1160
1161 /*
1162 * Internal version of pool_put(). Pool is already locked/entered.
1163 */
1164 static void
1165 pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1166 {
1167 struct pool_item *pi = v;
1168 struct pool_item_header *ph;
1169
1170 KASSERT(mutex_owned(&pp->pr_lock));
1171 FREECHECK_IN(&pp->pr_freecheck, v);
1172 LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1173
1174 #ifdef DIAGNOSTIC
1175 if (__predict_false(pp->pr_nout == 0)) {
1176 printf("pool %s: putting with none out\n",
1177 pp->pr_wchan);
1178 panic("pool_put");
1179 }
1180 #endif
1181
1182 if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1183 pr_printlog(pp, NULL, printf);
1184 panic("pool_put: %s: page header missing", pp->pr_wchan);
1185 }
1186
1187 /*
1188 * Return to item list.
1189 */
1190 if (pp->pr_roflags & PR_NOTOUCH) {
1191 pr_item_notouch_put(pp, ph, v);
1192 } else {
1193 #ifdef DIAGNOSTIC
1194 pi->pi_magic = PI_MAGIC;
1195 #endif
1196 #ifdef DEBUG
1197 {
1198 int i, *ip = v;
1199
1200 for (i = 0; i < pp->pr_size / sizeof(int); i++) {
1201 *ip++ = PI_MAGIC;
1202 }
1203 }
1204 #endif
1205
1206 LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1207 }
1208 KDASSERT(ph->ph_nmissing != 0);
1209 ph->ph_nmissing--;
1210 pp->pr_nput++;
1211 pp->pr_nitems++;
1212 pp->pr_nout--;
1213
1214 /* Cancel "pool empty" condition if it exists */
1215 if (pp->pr_curpage == NULL)
1216 pp->pr_curpage = ph;
1217
1218 if (pp->pr_flags & PR_WANTED) {
1219 pp->pr_flags &= ~PR_WANTED;
1220 if (ph->ph_nmissing == 0)
1221 pp->pr_nidle++;
1222 cv_broadcast(&pp->pr_cv);
1223 return;
1224 }
1225
1226 /*
1227 * If this page is now empty, do one of two things:
1228 *
1229 * (1) If we have more pages than the page high water mark,
1230 * free the page back to the system. ONLY CONSIDER
1231 * FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
1232 * CLAIM.
1233 *
1234 * (2) Otherwise, move the page to the empty page list.
1235 *
1236 * Either way, select a new current page (so we use a partially-full
1237 * page if one is available).
1238 */
1239 if (ph->ph_nmissing == 0) {
1240 pp->pr_nidle++;
1241 if (pp->pr_npages > pp->pr_minpages &&
1242 (pp->pr_npages > pp->pr_maxpages ||
1243 pa_starved_p(pp->pr_alloc))) {
1244 pr_rmpage(pp, ph, pq);
1245 } else {
1246 LIST_REMOVE(ph, ph_pagelist);
1247 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1248
1249 /*
1250 * Update the timestamp on the page. A page must
1251 * be idle for some period of time before it can
1252 * be reclaimed by the pagedaemon. This minimizes
1253 * ping-pong'ing for memory.
1254 */
1255 getmicrotime(&ph->ph_time);
1256 }
1257 pool_update_curpage(pp);
1258 }
1259
1260 /*
1261 * If the page was previously completely full, move it to the
1262 * partially-full list and make it the current page. The next
1263 * allocation will get the item from this page, instead of
1264 * further fragmenting the pool.
1265 */
1266 else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1267 LIST_REMOVE(ph, ph_pagelist);
1268 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1269 pp->pr_curpage = ph;
1270 }
1271 }
1272
1273 /*
1274 * Return resource to the pool.
1275 */
1276 #ifdef POOL_DIAGNOSTIC
1277 void
1278 _pool_put(struct pool *pp, void *v, const char *file, long line)
1279 {
1280 struct pool_pagelist pq;
1281
1282 LIST_INIT(&pq);
1283
1284 mutex_enter(&pp->pr_lock);
1285 pr_enter(pp, file, line);
1286
1287 pr_log(pp, v, PRLOG_PUT, file, line);
1288
1289 pool_do_put(pp, v, &pq);
1290
1291 pr_leave(pp);
1292 mutex_exit(&pp->pr_lock);
1293
1294 pr_pagelist_free(pp, &pq);
1295 }
1296 #undef pool_put
1297 #endif /* POOL_DIAGNOSTIC */
1298
1299 void
1300 pool_put(struct pool *pp, void *v)
1301 {
1302 struct pool_pagelist pq;
1303
1304 LIST_INIT(&pq);
1305
1306 mutex_enter(&pp->pr_lock);
1307 pool_do_put(pp, v, &pq);
1308 mutex_exit(&pp->pr_lock);
1309
1310 pr_pagelist_free(pp, &pq);
1311 }
1312
1313 #ifdef POOL_DIAGNOSTIC
1314 #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
1315 #endif
1316
1317 /*
1318 * pool_grow: grow a pool by a page.
1319 *
1320 * => called with pool locked.
1321 * => unlock and relock the pool.
1322 * => return with pool locked.
1323 */
1324
1325 static int
1326 pool_grow(struct pool *pp, int flags)
1327 {
1328 struct pool_item_header *ph = NULL;
1329 char *cp;
1330
1331 mutex_exit(&pp->pr_lock);
1332 cp = pool_allocator_alloc(pp, flags);
1333 if (__predict_true(cp != NULL)) {
1334 ph = pool_alloc_item_header(pp, cp, flags);
1335 }
1336 if (__predict_false(cp == NULL || ph == NULL)) {
1337 if (cp != NULL) {
1338 pool_allocator_free(pp, cp);
1339 }
1340 mutex_enter(&pp->pr_lock);
1341 return ENOMEM;
1342 }
1343
1344 mutex_enter(&pp->pr_lock);
1345 pool_prime_page(pp, cp, ph);
1346 pp->pr_npagealloc++;
1347 return 0;
1348 }
1349
1350 /*
1351 * Add N items to the pool.
1352 */
1353 int
1354 pool_prime(struct pool *pp, int n)
1355 {
1356 int newpages;
1357 int error = 0;
1358
1359 mutex_enter(&pp->pr_lock);
1360
1361 newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1362
1363 while (newpages-- > 0) {
1364 error = pool_grow(pp, PR_NOWAIT);
1365 if (error) {
1366 break;
1367 }
1368 pp->pr_minpages++;
1369 }
1370
1371 if (pp->pr_minpages >= pp->pr_maxpages)
1372 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1373
1374 mutex_exit(&pp->pr_lock);
1375 return error;
1376 }
1377
1378 /*
1379 * Add a page worth of items to the pool.
1380 *
1381 * Note, we must be called with the pool descriptor LOCKED.
1382 */
1383 static void
1384 pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1385 {
1386 struct pool_item *pi;
1387 void *cp = storage;
1388 const unsigned int align = pp->pr_align;
1389 const unsigned int ioff = pp->pr_itemoffset;
1390 int n;
1391
1392 KASSERT(mutex_owned(&pp->pr_lock));
1393
1394 #ifdef DIAGNOSTIC
1395 if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1396 ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1397 panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1398 #endif
1399
1400 /*
1401 * Insert page header.
1402 */
1403 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1404 LIST_INIT(&ph->ph_itemlist);
1405 ph->ph_page = storage;
1406 ph->ph_nmissing = 0;
1407 getmicrotime(&ph->ph_time);
1408 if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1409 SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1410
1411 pp->pr_nidle++;
1412
1413 /*
1414 * Color this page.
1415 */
1416 cp = (char *)cp + pp->pr_curcolor;
1417 if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1418 pp->pr_curcolor = 0;
1419
1420 /*
1421 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1422 */
1423 if (ioff != 0)
1424 cp = (char *)cp + align - ioff;
1425
1426 KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1427
1428 /*
1429 * Insert remaining chunks on the bucket list.
1430 */
1431 n = pp->pr_itemsperpage;
1432 pp->pr_nitems += n;
1433
1434 if (pp->pr_roflags & PR_NOTOUCH) {
1435 pool_item_freelist_t *freelist = PR_FREELIST(ph);
1436 int i;
1437
1438 ph->ph_off = (char *)cp - (char *)storage;
1439 ph->ph_firstfree = 0;
1440 for (i = 0; i < n - 1; i++)
1441 freelist[i] = i + 1;
1442 freelist[n - 1] = PR_INDEX_EOL;
1443 } else {
1444 while (n--) {
1445 pi = (struct pool_item *)cp;
1446
1447 KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1448
1449 /* Insert on page list */
1450 LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1451 #ifdef DIAGNOSTIC
1452 pi->pi_magic = PI_MAGIC;
1453 #endif
1454 cp = (char *)cp + pp->pr_size;
1455
1456 KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1457 }
1458 }
1459
1460 /*
1461 * If the pool was depleted, point at the new page.
1462 */
1463 if (pp->pr_curpage == NULL)
1464 pp->pr_curpage = ph;
1465
1466 if (++pp->pr_npages > pp->pr_hiwat)
1467 pp->pr_hiwat = pp->pr_npages;
1468 }
1469
1470 /*
1471 * Used by pool_get() when nitems drops below the low water mark. This
1472 * is used to catch up pr_nitems with the low water mark.
1473 *
1474 * Note 1, we never wait for memory here, we let the caller decide what to do.
1475 *
1476 * Note 2, we must be called with the pool already locked, and we return
1477 * with it locked.
1478 */
1479 static int
1480 pool_catchup(struct pool *pp)
1481 {
1482 int error = 0;
1483
1484 while (POOL_NEEDS_CATCHUP(pp)) {
1485 error = pool_grow(pp, PR_NOWAIT);
1486 if (error) {
1487 break;
1488 }
1489 }
1490 return error;
1491 }
1492
1493 static void
1494 pool_update_curpage(struct pool *pp)
1495 {
1496
1497 pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1498 if (pp->pr_curpage == NULL) {
1499 pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1500 }
1501 }
1502
1503 void
1504 pool_setlowat(struct pool *pp, int n)
1505 {
1506
1507 mutex_enter(&pp->pr_lock);
1508
1509 pp->pr_minitems = n;
1510 pp->pr_minpages = (n == 0)
1511 ? 0
1512 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1513
1514 /* Make sure we're caught up with the newly-set low water mark. */
1515 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1516 /*
1517 * XXX: Should we log a warning? Should we set up a timeout
1518 * to try again in a second or so? The latter could break
1519 * a caller's assumptions about interrupt protection, etc.
1520 */
1521 }
1522
1523 mutex_exit(&pp->pr_lock);
1524 }
1525
1526 void
1527 pool_sethiwat(struct pool *pp, int n)
1528 {
1529
1530 mutex_enter(&pp->pr_lock);
1531
1532 pp->pr_maxpages = (n == 0)
1533 ? 0
1534 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1535
1536 mutex_exit(&pp->pr_lock);
1537 }
1538
1539 void
1540 pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1541 {
1542
1543 mutex_enter(&pp->pr_lock);
1544
1545 pp->pr_hardlimit = n;
1546 pp->pr_hardlimit_warning = warnmess;
1547 pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1548 pp->pr_hardlimit_warning_last.tv_sec = 0;
1549 pp->pr_hardlimit_warning_last.tv_usec = 0;
1550
1551 /*
1552 * In-line version of pool_sethiwat(), because we don't want to
1553 * release the lock.
1554 */
1555 pp->pr_maxpages = (n == 0)
1556 ? 0
1557 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1558
1559 mutex_exit(&pp->pr_lock);
1560 }
1561
1562 /*
1563 * Release all complete pages that have not been used recently.
1564 */
1565 int
1566 #ifdef POOL_DIAGNOSTIC
1567 _pool_reclaim(struct pool *pp, const char *file, long line)
1568 #else
1569 pool_reclaim(struct pool *pp)
1570 #endif
1571 {
1572 struct pool_item_header *ph, *phnext;
1573 struct pool_pagelist pq;
1574 struct timeval curtime, diff;
1575 bool klock;
1576 int rv;
1577
1578 if (pp->pr_drain_hook != NULL) {
1579 /*
1580 * The drain hook must be called with the pool unlocked.
1581 */
1582 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1583 }
1584
1585 /*
1586 * XXXSMP Because mutexes at IPL_SOFTXXX are still spinlocks,
1587 * and we are called from the pagedaemon without kernel_lock.
1588 * Does not apply to IPL_SOFTBIO.
1589 */
1590 switch (pp->pr_ipl) {
1591 case IPL_SOFTNET:
1592 case IPL_SOFTCLOCK:
1593 case IPL_SOFTSERIAL:
1594 KERNEL_LOCK(1, NULL);
1595 klock = true;
1596 break;
1597 default:
1598 klock = false;
1599 break;
1600 }
1601
1602 /* Reclaim items from the pool's cache (if any). */
1603 if (pp->pr_cache != NULL)
1604 pool_cache_invalidate(pp->pr_cache);
1605
1606 if (mutex_tryenter(&pp->pr_lock) == 0) {
1607 if (klock) {
1608 KERNEL_UNLOCK_ONE(NULL);
1609 }
1610 return (0);
1611 }
1612 pr_enter(pp, file, line);
1613
1614 LIST_INIT(&pq);
1615
1616 getmicrotime(&curtime);
1617
1618 for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1619 phnext = LIST_NEXT(ph, ph_pagelist);
1620
1621 /* Check our minimum page claim */
1622 if (pp->pr_npages <= pp->pr_minpages)
1623 break;
1624
1625 KASSERT(ph->ph_nmissing == 0);
1626 timersub(&curtime, &ph->ph_time, &diff);
1627 if (diff.tv_sec < pool_inactive_time
1628 && !pa_starved_p(pp->pr_alloc))
1629 continue;
1630
1631 /*
1632 * If freeing this page would put us below
1633 * the low water mark, stop now.
1634 */
1635 if ((pp->pr_nitems - pp->pr_itemsperpage) <
1636 pp->pr_minitems)
1637 break;
1638
1639 pr_rmpage(pp, ph, &pq);
1640 }
1641
1642 pr_leave(pp);
1643 mutex_exit(&pp->pr_lock);
1644
1645 if (LIST_EMPTY(&pq))
1646 rv = 0;
1647 else {
1648 pr_pagelist_free(pp, &pq);
1649 rv = 1;
1650 }
1651
1652 if (klock) {
1653 KERNEL_UNLOCK_ONE(NULL);
1654 }
1655
1656 return (rv);
1657 }
1658
1659 /*
1660 * Drain pools, one at a time. This is a two stage process;
1661 * drain_start kicks off a cross call to drain CPU-level caches
1662 * if the pool has an associated pool_cache. drain_end waits
1663 * for those cross calls to finish, and then drains the cache
1664 * (if any) and pool.
1665 *
1666 * Note, must never be called from interrupt context.
1667 */
1668 void
1669 pool_drain_start(struct pool **ppp, uint64_t *wp)
1670 {
1671 struct pool *pp;
1672
1673 KASSERT(!LIST_EMPTY(&pool_head));
1674
1675 pp = NULL;
1676
1677 /* Find next pool to drain, and add a reference. */
1678 mutex_enter(&pool_head_lock);
1679 do {
1680 if (drainpp == NULL) {
1681 drainpp = LIST_FIRST(&pool_head);
1682 }
1683 if (drainpp != NULL) {
1684 pp = drainpp;
1685 drainpp = LIST_NEXT(pp, pr_poollist);
1686 }
1687 /*
1688 * Skip completely idle pools. We depend on at least
1689 * one pool in the system being active.
1690 */
1691 } while (pp == NULL || pp->pr_npages == 0);
1692 pp->pr_refcnt++;
1693 mutex_exit(&pool_head_lock);
1694
1695 /* If there is a pool_cache, drain CPU level caches. */
1696 *ppp = pp;
1697 if (pp->pr_cache != NULL) {
1698 *wp = xc_broadcast(0, (xcfunc_t)pool_cache_xcall,
1699 pp->pr_cache, NULL);
1700 }
1701 }
1702
1703 void
1704 pool_drain_end(struct pool *pp, uint64_t where)
1705 {
1706
1707 if (pp == NULL)
1708 return;
1709
1710 KASSERT(pp->pr_refcnt > 0);
1711
1712 /* Wait for remote draining to complete. */
1713 if (pp->pr_cache != NULL)
1714 xc_wait(where);
1715
1716 /* Drain the cache (if any) and pool.. */
1717 pool_reclaim(pp);
1718
1719 /* Finally, unlock the pool. */
1720 mutex_enter(&pool_head_lock);
1721 pp->pr_refcnt--;
1722 cv_broadcast(&pool_busy);
1723 mutex_exit(&pool_head_lock);
1724 }
1725
1726 /*
1727 * Diagnostic helpers.
1728 */
1729 void
1730 pool_print(struct pool *pp, const char *modif)
1731 {
1732
1733 pool_print1(pp, modif, printf);
1734 }
1735
1736 void
1737 pool_printall(const char *modif, void (*pr)(const char *, ...))
1738 {
1739 struct pool *pp;
1740
1741 LIST_FOREACH(pp, &pool_head, pr_poollist) {
1742 pool_printit(pp, modif, pr);
1743 }
1744 }
1745
1746 void
1747 pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1748 {
1749
1750 if (pp == NULL) {
1751 (*pr)("Must specify a pool to print.\n");
1752 return;
1753 }
1754
1755 pool_print1(pp, modif, pr);
1756 }
1757
1758 static void
1759 pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1760 void (*pr)(const char *, ...))
1761 {
1762 struct pool_item_header *ph;
1763 #ifdef DIAGNOSTIC
1764 struct pool_item *pi;
1765 #endif
1766
1767 LIST_FOREACH(ph, pl, ph_pagelist) {
1768 (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1769 ph->ph_page, ph->ph_nmissing,
1770 (u_long)ph->ph_time.tv_sec,
1771 (u_long)ph->ph_time.tv_usec);
1772 #ifdef DIAGNOSTIC
1773 if (!(pp->pr_roflags & PR_NOTOUCH)) {
1774 LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1775 if (pi->pi_magic != PI_MAGIC) {
1776 (*pr)("\t\t\titem %p, magic 0x%x\n",
1777 pi, pi->pi_magic);
1778 }
1779 }
1780 }
1781 #endif
1782 }
1783 }
1784
1785 static void
1786 pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1787 {
1788 struct pool_item_header *ph;
1789 pool_cache_t pc;
1790 pcg_t *pcg;
1791 pool_cache_cpu_t *cc;
1792 uint64_t cpuhit, cpumiss;
1793 int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1794 char c;
1795
1796 while ((c = *modif++) != '\0') {
1797 if (c == 'l')
1798 print_log = 1;
1799 if (c == 'p')
1800 print_pagelist = 1;
1801 if (c == 'c')
1802 print_cache = 1;
1803 }
1804
1805 if ((pc = pp->pr_cache) != NULL) {
1806 (*pr)("POOL CACHE");
1807 } else {
1808 (*pr)("POOL");
1809 }
1810
1811 (*pr)(" %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1812 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1813 pp->pr_roflags);
1814 (*pr)("\talloc %p\n", pp->pr_alloc);
1815 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1816 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1817 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1818 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1819
1820 (*pr)("\tnget %lu, nfail %lu, nput %lu\n",
1821 pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1822 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1823 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1824
1825 if (print_pagelist == 0)
1826 goto skip_pagelist;
1827
1828 if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1829 (*pr)("\n\tempty page list:\n");
1830 pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1831 if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1832 (*pr)("\n\tfull page list:\n");
1833 pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1834 if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1835 (*pr)("\n\tpartial-page list:\n");
1836 pool_print_pagelist(pp, &pp->pr_partpages, pr);
1837
1838 if (pp->pr_curpage == NULL)
1839 (*pr)("\tno current page\n");
1840 else
1841 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1842
1843 skip_pagelist:
1844 if (print_log == 0)
1845 goto skip_log;
1846
1847 (*pr)("\n");
1848 if ((pp->pr_roflags & PR_LOGGING) == 0)
1849 (*pr)("\tno log\n");
1850 else {
1851 pr_printlog(pp, NULL, pr);
1852 }
1853
1854 skip_log:
1855
1856 #define PR_GROUPLIST(pcg) \
1857 (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1858 for (i = 0; i < PCG_NOBJECTS; i++) { \
1859 if (pcg->pcg_objects[i].pcgo_pa != \
1860 POOL_PADDR_INVALID) { \
1861 (*pr)("\t\t\t%p, 0x%llx\n", \
1862 pcg->pcg_objects[i].pcgo_va, \
1863 (unsigned long long) \
1864 pcg->pcg_objects[i].pcgo_pa); \
1865 } else { \
1866 (*pr)("\t\t\t%p\n", \
1867 pcg->pcg_objects[i].pcgo_va); \
1868 } \
1869 }
1870
1871 if (pc != NULL) {
1872 cpuhit = 0;
1873 cpumiss = 0;
1874 for (i = 0; i < MAXCPUS; i++) {
1875 if ((cc = pc->pc_cpus[i]) == NULL)
1876 continue;
1877 cpuhit += cc->cc_hits;
1878 cpumiss += cc->cc_misses;
1879 }
1880 (*pr)("\tcpu layer hits %llu misses %llu\n", cpuhit, cpumiss);
1881 (*pr)("\tcache layer hits %llu misses %llu\n",
1882 pc->pc_hits, pc->pc_misses);
1883 (*pr)("\tcache layer entry uncontended %llu contended %llu\n",
1884 pc->pc_hits + pc->pc_misses - pc->pc_contended,
1885 pc->pc_contended);
1886 (*pr)("\tcache layer empty groups %u full groups %u\n",
1887 pc->pc_nempty, pc->pc_nfull);
1888 if (print_cache) {
1889 (*pr)("\tfull cache groups:\n");
1890 for (pcg = pc->pc_fullgroups; pcg != NULL;
1891 pcg = pcg->pcg_next) {
1892 PR_GROUPLIST(pcg);
1893 }
1894 (*pr)("\tempty cache groups:\n");
1895 for (pcg = pc->pc_emptygroups; pcg != NULL;
1896 pcg = pcg->pcg_next) {
1897 PR_GROUPLIST(pcg);
1898 }
1899 }
1900 }
1901 #undef PR_GROUPLIST
1902
1903 pr_enter_check(pp, pr);
1904 }
1905
1906 static int
1907 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1908 {
1909 struct pool_item *pi;
1910 void *page;
1911 int n;
1912
1913 if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1914 page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1915 if (page != ph->ph_page &&
1916 (pp->pr_roflags & PR_PHINPAGE) != 0) {
1917 if (label != NULL)
1918 printf("%s: ", label);
1919 printf("pool(%p:%s): page inconsistency: page %p;"
1920 " at page head addr %p (p %p)\n", pp,
1921 pp->pr_wchan, ph->ph_page,
1922 ph, page);
1923 return 1;
1924 }
1925 }
1926
1927 if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1928 return 0;
1929
1930 for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1931 pi != NULL;
1932 pi = LIST_NEXT(pi,pi_list), n++) {
1933
1934 #ifdef DIAGNOSTIC
1935 if (pi->pi_magic != PI_MAGIC) {
1936 if (label != NULL)
1937 printf("%s: ", label);
1938 printf("pool(%s): free list modified: magic=%x;"
1939 " page %p; item ordinal %d; addr %p\n",
1940 pp->pr_wchan, pi->pi_magic, ph->ph_page,
1941 n, pi);
1942 panic("pool");
1943 }
1944 #endif
1945 if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1946 continue;
1947 }
1948 page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1949 if (page == ph->ph_page)
1950 continue;
1951
1952 if (label != NULL)
1953 printf("%s: ", label);
1954 printf("pool(%p:%s): page inconsistency: page %p;"
1955 " item ordinal %d; addr %p (p %p)\n", pp,
1956 pp->pr_wchan, ph->ph_page,
1957 n, pi, page);
1958 return 1;
1959 }
1960 return 0;
1961 }
1962
1963
1964 int
1965 pool_chk(struct pool *pp, const char *label)
1966 {
1967 struct pool_item_header *ph;
1968 int r = 0;
1969
1970 mutex_enter(&pp->pr_lock);
1971 LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1972 r = pool_chk_page(pp, label, ph);
1973 if (r) {
1974 goto out;
1975 }
1976 }
1977 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1978 r = pool_chk_page(pp, label, ph);
1979 if (r) {
1980 goto out;
1981 }
1982 }
1983 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1984 r = pool_chk_page(pp, label, ph);
1985 if (r) {
1986 goto out;
1987 }
1988 }
1989
1990 out:
1991 mutex_exit(&pp->pr_lock);
1992 return (r);
1993 }
1994
1995 /*
1996 * pool_cache_init:
1997 *
1998 * Initialize a pool cache.
1999 */
2000 pool_cache_t
2001 pool_cache_init(size_t size, u_int align, u_int align_offset, u_int flags,
2002 const char *wchan, struct pool_allocator *palloc, int ipl,
2003 int (*ctor)(void *, void *, int), void (*dtor)(void *, void *), void *arg)
2004 {
2005 pool_cache_t pc;
2006
2007 pc = pool_get(&cache_pool, PR_WAITOK);
2008 if (pc == NULL)
2009 return NULL;
2010
2011 pool_cache_bootstrap(pc, size, align, align_offset, flags, wchan,
2012 palloc, ipl, ctor, dtor, arg);
2013
2014 return pc;
2015 }
2016
2017 /*
2018 * pool_cache_bootstrap:
2019 *
2020 * Kernel-private version of pool_cache_init(). The caller
2021 * provides initial storage.
2022 */
2023 void
2024 pool_cache_bootstrap(pool_cache_t pc, size_t size, u_int align,
2025 u_int align_offset, u_int flags, const char *wchan,
2026 struct pool_allocator *palloc, int ipl,
2027 int (*ctor)(void *, void *, int), void (*dtor)(void *, void *),
2028 void *arg)
2029 {
2030 CPU_INFO_ITERATOR cii;
2031 struct cpu_info *ci;
2032 struct pool *pp;
2033
2034 pp = &pc->pc_pool;
2035 if (palloc == NULL && ipl == IPL_NONE)
2036 palloc = &pool_allocator_nointr;
2037 pool_init(pp, size, align, align_offset, flags, wchan, palloc, ipl);
2038
2039 mutex_init(&pc->pc_lock, MUTEX_DEFAULT, pp->pr_ipl);
2040
2041 if (ctor == NULL) {
2042 ctor = (int (*)(void *, void *, int))nullop;
2043 }
2044 if (dtor == NULL) {
2045 dtor = (void (*)(void *, void *))nullop;
2046 }
2047
2048 pc->pc_emptygroups = NULL;
2049 pc->pc_fullgroups = NULL;
2050 pc->pc_partgroups = NULL;
2051 pc->pc_ctor = ctor;
2052 pc->pc_dtor = dtor;
2053 pc->pc_arg = arg;
2054 pc->pc_hits = 0;
2055 pc->pc_misses = 0;
2056 pc->pc_nempty = 0;
2057 pc->pc_npart = 0;
2058 pc->pc_nfull = 0;
2059 pc->pc_contended = 0;
2060 pc->pc_refcnt = 0;
2061
2062 /* Allocate per-CPU caches. */
2063 memset(pc->pc_cpus, 0, sizeof(pc->pc_cpus));
2064 pc->pc_ncpu = 0;
2065 for (CPU_INFO_FOREACH(cii, ci)) {
2066 pool_cache_cpu_init1(ci, pc);
2067 }
2068
2069 if (__predict_true(!cold)) {
2070 mutex_enter(&pp->pr_lock);
2071 pp->pr_cache = pc;
2072 mutex_exit(&pp->pr_lock);
2073 mutex_enter(&pool_head_lock);
2074 LIST_INSERT_HEAD(&pool_cache_head, pc, pc_cachelist);
2075 mutex_exit(&pool_head_lock);
2076 } else {
2077 pp->pr_cache = pc;
2078 LIST_INSERT_HEAD(&pool_cache_head, pc, pc_cachelist);
2079 }
2080 }
2081
2082 /*
2083 * pool_cache_destroy:
2084 *
2085 * Destroy a pool cache.
2086 */
2087 void
2088 pool_cache_destroy(pool_cache_t pc)
2089 {
2090 struct pool *pp = &pc->pc_pool;
2091 pool_cache_cpu_t *cc;
2092 pcg_t *pcg;
2093 int i;
2094
2095 /* Remove it from the global list. */
2096 mutex_enter(&pool_head_lock);
2097 while (pc->pc_refcnt != 0)
2098 cv_wait(&pool_busy, &pool_head_lock);
2099 LIST_REMOVE(pc, pc_cachelist);
2100 mutex_exit(&pool_head_lock);
2101
2102 /* First, invalidate the entire cache. */
2103 pool_cache_invalidate(pc);
2104
2105 /* Disassociate it from the pool. */
2106 mutex_enter(&pp->pr_lock);
2107 pp->pr_cache = NULL;
2108 mutex_exit(&pp->pr_lock);
2109
2110 /* Destroy per-CPU data */
2111 for (i = 0; i < MAXCPUS; i++) {
2112 if ((cc = pc->pc_cpus[i]) == NULL)
2113 continue;
2114 if ((pcg = cc->cc_current) != NULL) {
2115 pcg->pcg_next = NULL;
2116 pool_cache_invalidate_groups(pc, pcg);
2117 }
2118 if ((pcg = cc->cc_previous) != NULL) {
2119 pcg->pcg_next = NULL;
2120 pool_cache_invalidate_groups(pc, pcg);
2121 }
2122 if (cc != &pc->pc_cpu0)
2123 pool_put(&cache_cpu_pool, cc);
2124 }
2125
2126 /* Finally, destroy it. */
2127 mutex_destroy(&pc->pc_lock);
2128 pool_destroy(pp);
2129 pool_put(&cache_pool, pc);
2130 }
2131
2132 /*
2133 * pool_cache_cpu_init1:
2134 *
2135 * Called for each pool_cache whenever a new CPU is attached.
2136 */
2137 static void
2138 pool_cache_cpu_init1(struct cpu_info *ci, pool_cache_t pc)
2139 {
2140 pool_cache_cpu_t *cc;
2141
2142 KASSERT(((uintptr_t)pc->pc_cpus & (CACHE_LINE_SIZE - 1)) == 0);
2143
2144 if ((cc = pc->pc_cpus[ci->ci_index]) != NULL) {
2145 KASSERT(cc->cc_cpu = ci);
2146 return;
2147 }
2148
2149 /*
2150 * The first CPU is 'free'. This needs to be the case for
2151 * bootstrap - we may not be able to allocate yet.
2152 */
2153 if (pc->pc_ncpu == 0) {
2154 cc = &pc->pc_cpu0;
2155 pc->pc_ncpu = 1;
2156 } else {
2157 mutex_enter(&pc->pc_lock);
2158 pc->pc_ncpu++;
2159 mutex_exit(&pc->pc_lock);
2160 cc = pool_get(&cache_cpu_pool, PR_WAITOK);
2161 }
2162
2163 cc->cc_ipl = pc->pc_pool.pr_ipl;
2164 cc->cc_iplcookie = makeiplcookie(cc->cc_ipl);
2165 cc->cc_cache = pc;
2166 cc->cc_cpu = ci;
2167 cc->cc_hits = 0;
2168 cc->cc_misses = 0;
2169 cc->cc_current = NULL;
2170 cc->cc_previous = NULL;
2171
2172 pc->pc_cpus[ci->ci_index] = cc;
2173 }
2174
2175 /*
2176 * pool_cache_cpu_init:
2177 *
2178 * Called whenever a new CPU is attached.
2179 */
2180 void
2181 pool_cache_cpu_init(struct cpu_info *ci)
2182 {
2183 pool_cache_t pc;
2184
2185 mutex_enter(&pool_head_lock);
2186 LIST_FOREACH(pc, &pool_cache_head, pc_cachelist) {
2187 pc->pc_refcnt++;
2188 mutex_exit(&pool_head_lock);
2189
2190 pool_cache_cpu_init1(ci, pc);
2191
2192 mutex_enter(&pool_head_lock);
2193 pc->pc_refcnt--;
2194 cv_broadcast(&pool_busy);
2195 }
2196 mutex_exit(&pool_head_lock);
2197 }
2198
2199 /*
2200 * pool_cache_reclaim:
2201 *
2202 * Reclaim memory from a pool cache.
2203 */
2204 bool
2205 pool_cache_reclaim(pool_cache_t pc)
2206 {
2207
2208 return pool_reclaim(&pc->pc_pool);
2209 }
2210
2211 /*
2212 * pool_cache_destruct_object:
2213 *
2214 * Force destruction of an object and its release back into
2215 * the pool.
2216 */
2217 void
2218 pool_cache_destruct_object(pool_cache_t pc, void *object)
2219 {
2220
2221 (*pc->pc_dtor)(pc->pc_arg, object);
2222 pool_put(&pc->pc_pool, object);
2223 }
2224
2225 /*
2226 * pool_cache_invalidate_groups:
2227 *
2228 * Invalidate a chain of groups and destruct all objects.
2229 */
2230 static void
2231 pool_cache_invalidate_groups(pool_cache_t pc, pcg_t *pcg)
2232 {
2233 void *object;
2234 pcg_t *next;
2235 int i;
2236
2237 for (; pcg != NULL; pcg = next) {
2238 next = pcg->pcg_next;
2239
2240 for (i = 0; i < pcg->pcg_avail; i++) {
2241 object = pcg->pcg_objects[i].pcgo_va;
2242 pool_cache_destruct_object(pc, object);
2243 }
2244
2245 pool_put(&pcgpool, pcg);
2246 }
2247 }
2248
2249 /*
2250 * pool_cache_invalidate:
2251 *
2252 * Invalidate a pool cache (destruct and release all of the
2253 * cached objects). Does not reclaim objects from the pool.
2254 */
2255 void
2256 pool_cache_invalidate(pool_cache_t pc)
2257 {
2258 pcg_t *full, *empty, *part;
2259
2260 mutex_enter(&pc->pc_lock);
2261 full = pc->pc_fullgroups;
2262 empty = pc->pc_emptygroups;
2263 part = pc->pc_partgroups;
2264 pc->pc_fullgroups = NULL;
2265 pc->pc_emptygroups = NULL;
2266 pc->pc_partgroups = NULL;
2267 pc->pc_nfull = 0;
2268 pc->pc_nempty = 0;
2269 pc->pc_npart = 0;
2270 mutex_exit(&pc->pc_lock);
2271
2272 pool_cache_invalidate_groups(pc, full);
2273 pool_cache_invalidate_groups(pc, empty);
2274 pool_cache_invalidate_groups(pc, part);
2275 }
2276
2277 void
2278 pool_cache_set_drain_hook(pool_cache_t pc, void (*fn)(void *, int), void *arg)
2279 {
2280
2281 pool_set_drain_hook(&pc->pc_pool, fn, arg);
2282 }
2283
2284 void
2285 pool_cache_setlowat(pool_cache_t pc, int n)
2286 {
2287
2288 pool_setlowat(&pc->pc_pool, n);
2289 }
2290
2291 void
2292 pool_cache_sethiwat(pool_cache_t pc, int n)
2293 {
2294
2295 pool_sethiwat(&pc->pc_pool, n);
2296 }
2297
2298 void
2299 pool_cache_sethardlimit(pool_cache_t pc, int n, const char *warnmess, int ratecap)
2300 {
2301
2302 pool_sethardlimit(&pc->pc_pool, n, warnmess, ratecap);
2303 }
2304
2305 static inline pool_cache_cpu_t *
2306 pool_cache_cpu_enter(pool_cache_t pc, int *s)
2307 {
2308 pool_cache_cpu_t *cc;
2309 struct cpu_info *ci;
2310
2311 /*
2312 * Prevent other users of the cache from accessing our
2313 * CPU-local data. To avoid touching shared state, we
2314 * pull the neccessary information from CPU local data.
2315 */
2316 ci = curcpu();
2317 KASSERT(ci->ci_data.cpu_index < MAXCPUS);
2318 cc = pc->pc_cpus[ci->ci_data.cpu_index];
2319 KASSERT(cc->cc_cache == pc);
2320 if (cc->cc_ipl == IPL_NONE) {
2321 crit_enter();
2322 } else {
2323 *s = splraiseipl(cc->cc_iplcookie);
2324 }
2325
2326 /* Moved to another CPU before disabling preemption? */
2327 if (__predict_false(ci != curcpu())) {
2328 ci = curcpu();
2329 cc = pc->pc_cpus[ci->ci_data.cpu_index];
2330 }
2331
2332 #ifdef DIAGNOSTIC
2333 KASSERT(cc->cc_cpu == ci);
2334 KASSERT(((uintptr_t)cc & (CACHE_LINE_SIZE - 1)) == 0);
2335 #endif
2336
2337 return cc;
2338 }
2339
2340 static inline void
2341 pool_cache_cpu_exit(pool_cache_cpu_t *cc, int *s)
2342 {
2343
2344 /* No longer need exclusive access to the per-CPU data. */
2345 if (cc->cc_ipl == IPL_NONE) {
2346 crit_exit();
2347 } else {
2348 splx(*s);
2349 }
2350 }
2351
2352 #if __GNUC_PREREQ__(3, 0)
2353 __attribute ((noinline))
2354 #endif
2355 pool_cache_cpu_t *
2356 pool_cache_get_slow(pool_cache_cpu_t *cc, int *s, void **objectp,
2357 paddr_t *pap, int flags)
2358 {
2359 pcg_t *pcg, *cur;
2360 uint64_t ncsw;
2361 pool_cache_t pc;
2362 void *object;
2363
2364 pc = cc->cc_cache;
2365 cc->cc_misses++;
2366
2367 /*
2368 * Nothing was available locally. Try and grab a group
2369 * from the cache.
2370 */
2371 if (!mutex_tryenter(&pc->pc_lock)) {
2372 ncsw = curlwp->l_ncsw;
2373 mutex_enter(&pc->pc_lock);
2374 pc->pc_contended++;
2375
2376 /*
2377 * If we context switched while locking, then
2378 * our view of the per-CPU data is invalid:
2379 * retry.
2380 */
2381 if (curlwp->l_ncsw != ncsw) {
2382 mutex_exit(&pc->pc_lock);
2383 pool_cache_cpu_exit(cc, s);
2384 return pool_cache_cpu_enter(pc, s);
2385 }
2386 }
2387
2388 if ((pcg = pc->pc_fullgroups) != NULL) {
2389 /*
2390 * If there's a full group, release our empty
2391 * group back to the cache. Install the full
2392 * group as cc_current and return.
2393 */
2394 if ((cur = cc->cc_current) != NULL) {
2395 KASSERT(cur->pcg_avail == 0);
2396 cur->pcg_next = pc->pc_emptygroups;
2397 pc->pc_emptygroups = cur;
2398 pc->pc_nempty++;
2399 }
2400 KASSERT(pcg->pcg_avail == PCG_NOBJECTS);
2401 cc->cc_current = pcg;
2402 pc->pc_fullgroups = pcg->pcg_next;
2403 pc->pc_hits++;
2404 pc->pc_nfull--;
2405 mutex_exit(&pc->pc_lock);
2406 return cc;
2407 }
2408
2409 /*
2410 * Nothing available locally or in cache. Take the slow
2411 * path: fetch a new object from the pool and construct
2412 * it.
2413 */
2414 pc->pc_misses++;
2415 mutex_exit(&pc->pc_lock);
2416 pool_cache_cpu_exit(cc, s);
2417
2418 object = pool_get(&pc->pc_pool, flags);
2419 *objectp = object;
2420 if (object == NULL)
2421 return NULL;
2422
2423 if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
2424 pool_put(&pc->pc_pool, object);
2425 *objectp = NULL;
2426 return NULL;
2427 }
2428
2429 KASSERT((((vaddr_t)object + pc->pc_pool.pr_itemoffset) &
2430 (pc->pc_pool.pr_align - 1)) == 0);
2431
2432 if (pap != NULL) {
2433 #ifdef POOL_VTOPHYS
2434 *pap = POOL_VTOPHYS(object);
2435 #else
2436 *pap = POOL_PADDR_INVALID;
2437 #endif
2438 }
2439
2440 FREECHECK_OUT(&pc->pc_freecheck, object);
2441 return NULL;
2442 }
2443
2444 /*
2445 * pool_cache_get{,_paddr}:
2446 *
2447 * Get an object from a pool cache (optionally returning
2448 * the physical address of the object).
2449 */
2450 void *
2451 pool_cache_get_paddr(pool_cache_t pc, int flags, paddr_t *pap)
2452 {
2453 pool_cache_cpu_t *cc;
2454 pcg_t *pcg;
2455 void *object;
2456 int s;
2457
2458 #ifdef LOCKDEBUG
2459 if (flags & PR_WAITOK)
2460 ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)");
2461 #endif
2462
2463 cc = pool_cache_cpu_enter(pc, &s);
2464 do {
2465 /* Try and allocate an object from the current group. */
2466 pcg = cc->cc_current;
2467 if (pcg != NULL && pcg->pcg_avail > 0) {
2468 object = pcg->pcg_objects[--pcg->pcg_avail].pcgo_va;
2469 if (pap != NULL)
2470 *pap = pcg->pcg_objects[pcg->pcg_avail].pcgo_pa;
2471 pcg->pcg_objects[pcg->pcg_avail].pcgo_va = NULL;
2472 KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
2473 KASSERT(object != NULL);
2474 cc->cc_hits++;
2475 pool_cache_cpu_exit(cc, &s);
2476 FREECHECK_OUT(&pc->pc_freecheck, object);
2477 return object;
2478 }
2479
2480 /*
2481 * That failed. If the previous group isn't empty, swap
2482 * it with the current group and allocate from there.
2483 */
2484 pcg = cc->cc_previous;
2485 if (pcg != NULL && pcg->pcg_avail > 0) {
2486 cc->cc_previous = cc->cc_current;
2487 cc->cc_current = pcg;
2488 continue;
2489 }
2490
2491 /*
2492 * Can't allocate from either group: try the slow path.
2493 * If get_slow() allocated an object for us, or if
2494 * no more objects are available, it will return NULL.
2495 * Otherwise, we need to retry.
2496 */
2497 cc = pool_cache_get_slow(cc, &s, &object, pap, flags);
2498 } while (cc != NULL);
2499
2500 return object;
2501 }
2502
2503 #if __GNUC_PREREQ__(3, 0)
2504 __attribute ((noinline))
2505 #endif
2506 pool_cache_cpu_t *
2507 pool_cache_put_slow(pool_cache_cpu_t *cc, int *s, void *object, paddr_t pa)
2508 {
2509 pcg_t *pcg, *cur;
2510 uint64_t ncsw;
2511 pool_cache_t pc;
2512
2513 pc = cc->cc_cache;
2514 cc->cc_misses++;
2515
2516 /*
2517 * No free slots locally. Try to grab an empty, unused
2518 * group from the cache.
2519 */
2520 if (!mutex_tryenter(&pc->pc_lock)) {
2521 ncsw = curlwp->l_ncsw;
2522 mutex_enter(&pc->pc_lock);
2523 pc->pc_contended++;
2524
2525 /*
2526 * If we context switched while locking, then
2527 * our view of the per-CPU data is invalid:
2528 * retry.
2529 */
2530 if (curlwp->l_ncsw != ncsw) {
2531 mutex_exit(&pc->pc_lock);
2532 pool_cache_cpu_exit(cc, s);
2533 return pool_cache_cpu_enter(pc, s);
2534 }
2535 }
2536
2537 if ((pcg = pc->pc_emptygroups) != NULL) {
2538 /*
2539 * If there's a empty group, release our full
2540 * group back to the cache. Install the empty
2541 * group as cc_current and return.
2542 */
2543 if ((cur = cc->cc_current) != NULL) {
2544 KASSERT(cur->pcg_avail == PCG_NOBJECTS);
2545 cur->pcg_next = pc->pc_fullgroups;
2546 pc->pc_fullgroups = cur;
2547 pc->pc_nfull++;
2548 }
2549 KASSERT(pcg->pcg_avail == 0);
2550 cc->cc_current = pcg;
2551 pc->pc_emptygroups = pcg->pcg_next;
2552 pc->pc_hits++;
2553 pc->pc_nempty--;
2554 mutex_exit(&pc->pc_lock);
2555 return cc;
2556 }
2557
2558 /*
2559 * Nothing available locally or in cache. Take the
2560 * slow path and try to allocate a new group that we
2561 * can release to.
2562 */
2563 pc->pc_misses++;
2564 mutex_exit(&pc->pc_lock);
2565 pool_cache_cpu_exit(cc, s);
2566
2567 /*
2568 * If we can't allocate a new group, just throw the
2569 * object away.
2570 */
2571 pcg = pool_get(&pcgpool, PR_NOWAIT);
2572 if (pcg == NULL) {
2573 pool_cache_destruct_object(pc, object);
2574 return NULL;
2575 }
2576 #ifdef DIAGNOSTIC
2577 memset(pcg, 0, sizeof(*pcg));
2578 #else
2579 pcg->pcg_avail = 0;
2580 #endif
2581
2582 /*
2583 * Add the empty group to the cache and try again.
2584 */
2585 mutex_enter(&pc->pc_lock);
2586 pcg->pcg_next = pc->pc_emptygroups;
2587 pc->pc_emptygroups = pcg;
2588 pc->pc_nempty++;
2589 mutex_exit(&pc->pc_lock);
2590
2591 return pool_cache_cpu_enter(pc, s);
2592 }
2593
2594 /*
2595 * pool_cache_put{,_paddr}:
2596 *
2597 * Put an object back to the pool cache (optionally caching the
2598 * physical address of the object).
2599 */
2600 void
2601 pool_cache_put_paddr(pool_cache_t pc, void *object, paddr_t pa)
2602 {
2603 pool_cache_cpu_t *cc;
2604 pcg_t *pcg;
2605 int s;
2606
2607 FREECHECK_IN(&pc->pc_freecheck, object);
2608
2609 cc = pool_cache_cpu_enter(pc, &s);
2610 do {
2611 /* If the current group isn't full, release it there. */
2612 pcg = cc->cc_current;
2613 if (pcg != NULL && pcg->pcg_avail < PCG_NOBJECTS) {
2614 KASSERT(pcg->pcg_objects[pcg->pcg_avail].pcgo_va
2615 == NULL);
2616 pcg->pcg_objects[pcg->pcg_avail].pcgo_va = object;
2617 pcg->pcg_objects[pcg->pcg_avail].pcgo_pa = pa;
2618 pcg->pcg_avail++;
2619 cc->cc_hits++;
2620 pool_cache_cpu_exit(cc, &s);
2621 return;
2622 }
2623
2624 /*
2625 * That failed. If the previous group is empty, swap
2626 * it with the current group and try again.
2627 */
2628 pcg = cc->cc_previous;
2629 if (pcg != NULL && pcg->pcg_avail == 0) {
2630 cc->cc_previous = cc->cc_current;
2631 cc->cc_current = pcg;
2632 continue;
2633 }
2634
2635 /*
2636 * Can't free to either group: try the slow path.
2637 * If put_slow() releases the object for us, it
2638 * will return NULL. Otherwise we need to retry.
2639 */
2640 cc = pool_cache_put_slow(cc, &s, object, pa);
2641 } while (cc != NULL);
2642 }
2643
2644 /*
2645 * pool_cache_xcall:
2646 *
2647 * Transfer objects from the per-CPU cache to the global cache.
2648 * Run within a cross-call thread.
2649 */
2650 static void
2651 pool_cache_xcall(pool_cache_t pc)
2652 {
2653 pool_cache_cpu_t *cc;
2654 pcg_t *prev, *cur, **list;
2655 int s = 0; /* XXXgcc */
2656
2657 cc = pool_cache_cpu_enter(pc, &s);
2658 cur = cc->cc_current;
2659 cc->cc_current = NULL;
2660 prev = cc->cc_previous;
2661 cc->cc_previous = NULL;
2662 pool_cache_cpu_exit(cc, &s);
2663
2664 /*
2665 * XXXSMP Go to splvm to prevent kernel_lock from being taken,
2666 * because locks at IPL_SOFTXXX are still spinlocks. Does not
2667 * apply to IPL_SOFTBIO. Cross-call threads do not take the
2668 * kernel_lock.
2669 */
2670 s = splvm();
2671 mutex_enter(&pc->pc_lock);
2672 if (cur != NULL) {
2673 if (cur->pcg_avail == PCG_NOBJECTS) {
2674 list = &pc->pc_fullgroups;
2675 pc->pc_nfull++;
2676 } else if (cur->pcg_avail == 0) {
2677 list = &pc->pc_emptygroups;
2678 pc->pc_nempty++;
2679 } else {
2680 list = &pc->pc_partgroups;
2681 pc->pc_npart++;
2682 }
2683 cur->pcg_next = *list;
2684 *list = cur;
2685 }
2686 if (prev != NULL) {
2687 if (prev->pcg_avail == PCG_NOBJECTS) {
2688 list = &pc->pc_fullgroups;
2689 pc->pc_nfull++;
2690 } else if (prev->pcg_avail == 0) {
2691 list = &pc->pc_emptygroups;
2692 pc->pc_nempty++;
2693 } else {
2694 list = &pc->pc_partgroups;
2695 pc->pc_npart++;
2696 }
2697 prev->pcg_next = *list;
2698 *list = prev;
2699 }
2700 mutex_exit(&pc->pc_lock);
2701 splx(s);
2702 }
2703
2704 /*
2705 * Pool backend allocators.
2706 *
2707 * Each pool has a backend allocator that handles allocation, deallocation,
2708 * and any additional draining that might be needed.
2709 *
2710 * We provide two standard allocators:
2711 *
2712 * pool_allocator_kmem - the default when no allocator is specified
2713 *
2714 * pool_allocator_nointr - used for pools that will not be accessed
2715 * in interrupt context.
2716 */
2717 void *pool_page_alloc(struct pool *, int);
2718 void pool_page_free(struct pool *, void *);
2719
2720 #ifdef POOL_SUBPAGE
2721 struct pool_allocator pool_allocator_kmem_fullpage = {
2722 pool_page_alloc, pool_page_free, 0,
2723 .pa_backingmapptr = &kmem_map,
2724 };
2725 #else
2726 struct pool_allocator pool_allocator_kmem = {
2727 pool_page_alloc, pool_page_free, 0,
2728 .pa_backingmapptr = &kmem_map,
2729 };
2730 #endif
2731
2732 void *pool_page_alloc_nointr(struct pool *, int);
2733 void pool_page_free_nointr(struct pool *, void *);
2734
2735 #ifdef POOL_SUBPAGE
2736 struct pool_allocator pool_allocator_nointr_fullpage = {
2737 pool_page_alloc_nointr, pool_page_free_nointr, 0,
2738 .pa_backingmapptr = &kernel_map,
2739 };
2740 #else
2741 struct pool_allocator pool_allocator_nointr = {
2742 pool_page_alloc_nointr, pool_page_free_nointr, 0,
2743 .pa_backingmapptr = &kernel_map,
2744 };
2745 #endif
2746
2747 #ifdef POOL_SUBPAGE
2748 void *pool_subpage_alloc(struct pool *, int);
2749 void pool_subpage_free(struct pool *, void *);
2750
2751 struct pool_allocator pool_allocator_kmem = {
2752 pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2753 .pa_backingmapptr = &kmem_map,
2754 };
2755
2756 void *pool_subpage_alloc_nointr(struct pool *, int);
2757 void pool_subpage_free_nointr(struct pool *, void *);
2758
2759 struct pool_allocator pool_allocator_nointr = {
2760 pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2761 .pa_backingmapptr = &kmem_map,
2762 };
2763 #endif /* POOL_SUBPAGE */
2764
2765 static void *
2766 pool_allocator_alloc(struct pool *pp, int flags)
2767 {
2768 struct pool_allocator *pa = pp->pr_alloc;
2769 void *res;
2770
2771 res = (*pa->pa_alloc)(pp, flags);
2772 if (res == NULL && (flags & PR_WAITOK) == 0) {
2773 /*
2774 * We only run the drain hook here if PR_NOWAIT.
2775 * In other cases, the hook will be run in
2776 * pool_reclaim().
2777 */
2778 if (pp->pr_drain_hook != NULL) {
2779 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2780 res = (*pa->pa_alloc)(pp, flags);
2781 }
2782 }
2783 return res;
2784 }
2785
2786 static void
2787 pool_allocator_free(struct pool *pp, void *v)
2788 {
2789 struct pool_allocator *pa = pp->pr_alloc;
2790
2791 (*pa->pa_free)(pp, v);
2792 }
2793
2794 void *
2795 pool_page_alloc(struct pool *pp, int flags)
2796 {
2797 bool waitok = (flags & PR_WAITOK) ? true : false;
2798
2799 return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
2800 }
2801
2802 void
2803 pool_page_free(struct pool *pp, void *v)
2804 {
2805
2806 uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2807 }
2808
2809 static void *
2810 pool_page_alloc_meta(struct pool *pp, int flags)
2811 {
2812 bool waitok = (flags & PR_WAITOK) ? true : false;
2813
2814 return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
2815 }
2816
2817 static void
2818 pool_page_free_meta(struct pool *pp, void *v)
2819 {
2820
2821 uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
2822 }
2823
2824 #ifdef POOL_SUBPAGE
2825 /* Sub-page allocator, for machines with large hardware pages. */
2826 void *
2827 pool_subpage_alloc(struct pool *pp, int flags)
2828 {
2829 return pool_get(&psppool, flags);
2830 }
2831
2832 void
2833 pool_subpage_free(struct pool *pp, void *v)
2834 {
2835 pool_put(&psppool, v);
2836 }
2837
2838 /* We don't provide a real nointr allocator. Maybe later. */
2839 void *
2840 pool_subpage_alloc_nointr(struct pool *pp, int flags)
2841 {
2842
2843 return (pool_subpage_alloc(pp, flags));
2844 }
2845
2846 void
2847 pool_subpage_free_nointr(struct pool *pp, void *v)
2848 {
2849
2850 pool_subpage_free(pp, v);
2851 }
2852 #endif /* POOL_SUBPAGE */
2853 void *
2854 pool_page_alloc_nointr(struct pool *pp, int flags)
2855 {
2856 bool waitok = (flags & PR_WAITOK) ? true : false;
2857
2858 return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
2859 }
2860
2861 void
2862 pool_page_free_nointr(struct pool *pp, void *v)
2863 {
2864
2865 uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
2866 }
2867