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