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