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