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