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