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