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