subr_pool.c revision 1.128.2.6 1 /* $NetBSD: subr_pool.c,v 1.128.2.6 2007/08/20 21:27:37 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1999, 2000, 2002, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: subr_pool.c,v 1.128.2.6 2007/08/20 21:27:37 ad Exp $");
42
43 #include "opt_pool.h"
44 #include "opt_poollog.h"
45 #include "opt_lockdebug.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/errno.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/lock.h>
54 #include <sys/pool.h>
55 #include <sys/syslog.h>
56 #include <sys/debug.h>
57 #include <sys/lockdebug.h>
58
59 #include <uvm/uvm.h>
60
61 /*
62 * Pool resource management utility.
63 *
64 * Memory is allocated in pages which are split into pieces according to
65 * the pool item size. Each page is kept on one of three lists in the
66 * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages',
67 * for empty, full and partially-full pages respectively. The individual
68 * pool items are on a linked list headed by `ph_itemlist' in each page
69 * header. The memory for building the page list is either taken from
70 * the allocated pages themselves (for small pool items) or taken from
71 * an internal pool of page headers (`phpool').
72 */
73
74 /* List of all pools */
75 LIST_HEAD(,pool) pool_head = LIST_HEAD_INITIALIZER(pool_head);
76
77 /* Private pool for page header structures */
78 #define PHPOOL_MAX 8
79 static struct pool phpool[PHPOOL_MAX];
80 #define PHPOOL_FREELIST_NELEM(idx) (((idx) == 0) ? 0 : (1 << (idx)))
81
82 #ifdef POOL_SUBPAGE
83 /* Pool of subpages for use by normal pools. */
84 static struct pool psppool;
85 #endif
86
87 static SLIST_HEAD(, pool_allocator) pa_deferinitq =
88 SLIST_HEAD_INITIALIZER(pa_deferinitq);
89
90 static void *pool_page_alloc_meta(struct pool *, int);
91 static void pool_page_free_meta(struct pool *, void *);
92
93 /* allocator for pool metadata */
94 static struct pool_allocator pool_allocator_meta = {
95 pool_page_alloc_meta, pool_page_free_meta,
96 .pa_backingmapptr = &kmem_map,
97 };
98
99 /* # of seconds to retain page after last use */
100 int pool_inactive_time = 10;
101
102 /* Next candidate for drainage (see pool_drain()) */
103 static struct pool *drainpp;
104
105 /* This lock protects both pool_head and drainpp. */
106 static kmutex_t pool_head_lock;
107
108 typedef uint8_t pool_item_freelist_t;
109
110 struct pool_item_header {
111 /* Page headers */
112 LIST_ENTRY(pool_item_header)
113 ph_pagelist; /* pool page list */
114 SPLAY_ENTRY(pool_item_header)
115 ph_node; /* Off-page page headers */
116 void * ph_page; /* this page's address */
117 struct timeval ph_time; /* last referenced */
118 union {
119 /* !PR_NOTOUCH */
120 struct {
121 LIST_HEAD(, pool_item)
122 phu_itemlist; /* chunk list for this page */
123 } phu_normal;
124 /* PR_NOTOUCH */
125 struct {
126 uint16_t
127 phu_off; /* start offset in page */
128 pool_item_freelist_t
129 phu_firstfree; /* first free item */
130 /*
131 * XXX it might be better to use
132 * a simple bitmap and ffs(3)
133 */
134 } phu_notouch;
135 } ph_u;
136 uint16_t ph_nmissing; /* # of chunks in use */
137 };
138 #define ph_itemlist ph_u.phu_normal.phu_itemlist
139 #define ph_off ph_u.phu_notouch.phu_off
140 #define ph_firstfree ph_u.phu_notouch.phu_firstfree
141
142 struct pool_item {
143 #ifdef DIAGNOSTIC
144 u_int pi_magic;
145 #endif
146 #define PI_MAGIC 0xdeadbeefU
147 /* Other entries use only this list entry */
148 LIST_ENTRY(pool_item) pi_list;
149 };
150
151 #define POOL_NEEDS_CATCHUP(pp) \
152 ((pp)->pr_nitems < (pp)->pr_minitems)
153
154 /*
155 * Pool cache management.
156 *
157 * Pool caches provide a way for constructed objects to be cached by the
158 * pool subsystem. This can lead to performance improvements by avoiding
159 * needless object construction/destruction; it is deferred until absolutely
160 * necessary.
161 *
162 * Caches are grouped into cache groups. Each cache group references
163 * up to 16 constructed objects. When a cache allocates an object
164 * from the pool, it calls the object's constructor and places it into
165 * a cache group. When a cache group frees an object back to the pool,
166 * it first calls the object's destructor. This allows the object to
167 * persist in constructed form while freed to the cache.
168 *
169 * Multiple caches may exist for each pool. This allows a single
170 * object type to have multiple constructed forms. The pool references
171 * each cache, so that when a pool is drained by the pagedaemon, it can
172 * drain each individual cache as well. Each time a cache is drained,
173 * the most idle cache group is freed to the pool in its entirety.
174 *
175 * Pool caches are layed on top of pools. By layering them, we can avoid
176 * the complexity of cache management for pools which would not benefit
177 * from it.
178 */
179
180 /* The cache group pool. */
181 static struct pool pcgpool;
182
183 static void pool_cache_reclaim(struct pool_cache *, struct pool_pagelist *,
184 struct pool_cache_grouplist *);
185 static void pcg_grouplist_free(struct pool_cache_grouplist *);
186
187 static int pool_catchup(struct pool *);
188 static void pool_prime_page(struct pool *, void *,
189 struct pool_item_header *);
190 static void pool_update_curpage(struct pool *);
191
192 static int pool_grow(struct pool *, int);
193 static void *pool_allocator_alloc(struct pool *, int);
194 static void pool_allocator_free(struct pool *, void *);
195
196 static void pool_print_pagelist(struct pool *, struct pool_pagelist *,
197 void (*)(const char *, ...));
198 static void pool_print1(struct pool *, const char *,
199 void (*)(const char *, ...));
200
201 static int pool_chk_page(struct pool *, const char *,
202 struct pool_item_header *);
203
204 /*
205 * Pool log entry. An array of these is allocated in pool_init().
206 */
207 struct pool_log {
208 const char *pl_file;
209 long pl_line;
210 int pl_action;
211 #define PRLOG_GET 1
212 #define PRLOG_PUT 2
213 void *pl_addr;
214 };
215
216 #ifdef POOL_DIAGNOSTIC
217 /* Number of entries in pool log buffers */
218 #ifndef POOL_LOGSIZE
219 #define POOL_LOGSIZE 10
220 #endif
221
222 int pool_logsize = POOL_LOGSIZE;
223
224 static inline void
225 pr_log(struct pool *pp, void *v, int action, const char *file, long line)
226 {
227 int n = pp->pr_curlogentry;
228 struct pool_log *pl;
229
230 if ((pp->pr_roflags & PR_LOGGING) == 0)
231 return;
232
233 /*
234 * Fill in the current entry. Wrap around and overwrite
235 * the oldest entry if necessary.
236 */
237 pl = &pp->pr_log[n];
238 pl->pl_file = file;
239 pl->pl_line = line;
240 pl->pl_action = action;
241 pl->pl_addr = v;
242 if (++n >= pp->pr_logsize)
243 n = 0;
244 pp->pr_curlogentry = n;
245 }
246
247 static void
248 pr_printlog(struct pool *pp, struct pool_item *pi,
249 void (*pr)(const char *, ...))
250 {
251 int i = pp->pr_logsize;
252 int n = pp->pr_curlogentry;
253
254 if ((pp->pr_roflags & PR_LOGGING) == 0)
255 return;
256
257 /*
258 * Print all entries in this pool's log.
259 */
260 while (i-- > 0) {
261 struct pool_log *pl = &pp->pr_log[n];
262 if (pl->pl_action != 0) {
263 if (pi == NULL || pi == pl->pl_addr) {
264 (*pr)("\tlog entry %d:\n", i);
265 (*pr)("\t\taction = %s, addr = %p\n",
266 pl->pl_action == PRLOG_GET ? "get" : "put",
267 pl->pl_addr);
268 (*pr)("\t\tfile: %s at line %lu\n",
269 pl->pl_file, pl->pl_line);
270 }
271 }
272 if (++n >= pp->pr_logsize)
273 n = 0;
274 }
275 }
276
277 static inline void
278 pr_enter(struct pool *pp, const char *file, long line)
279 {
280
281 if (__predict_false(pp->pr_entered_file != NULL)) {
282 printf("pool %s: reentrancy at file %s line %ld\n",
283 pp->pr_wchan, file, line);
284 printf(" previous entry at file %s line %ld\n",
285 pp->pr_entered_file, pp->pr_entered_line);
286 panic("pr_enter");
287 }
288
289 pp->pr_entered_file = file;
290 pp->pr_entered_line = line;
291 }
292
293 static inline void
294 pr_leave(struct pool *pp)
295 {
296
297 if (__predict_false(pp->pr_entered_file == NULL)) {
298 printf("pool %s not entered?\n", pp->pr_wchan);
299 panic("pr_leave");
300 }
301
302 pp->pr_entered_file = NULL;
303 pp->pr_entered_line = 0;
304 }
305
306 static inline void
307 pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
308 {
309
310 if (pp->pr_entered_file != NULL)
311 (*pr)("\n\tcurrently entered from file %s line %ld\n",
312 pp->pr_entered_file, pp->pr_entered_line);
313 }
314 #else
315 #define pr_log(pp, v, action, file, line)
316 #define pr_printlog(pp, pi, pr)
317 #define pr_enter(pp, file, line)
318 #define pr_leave(pp)
319 #define pr_enter_check(pp, pr)
320 #endif /* POOL_DIAGNOSTIC */
321
322 static inline int
323 pr_item_notouch_index(const struct pool *pp, const struct pool_item_header *ph,
324 const void *v)
325 {
326 const char *cp = v;
327 int idx;
328
329 KASSERT(pp->pr_roflags & PR_NOTOUCH);
330 idx = (cp - (char *)ph->ph_page - ph->ph_off) / pp->pr_size;
331 KASSERT(idx < pp->pr_itemsperpage);
332 return idx;
333 }
334
335 #define PR_FREELIST_ALIGN(p) \
336 roundup((uintptr_t)(p), sizeof(pool_item_freelist_t))
337 #define PR_FREELIST(ph) ((pool_item_freelist_t *)PR_FREELIST_ALIGN((ph) + 1))
338 #define PR_INDEX_USED ((pool_item_freelist_t)-1)
339 #define PR_INDEX_EOL ((pool_item_freelist_t)-2)
340
341 static inline void
342 pr_item_notouch_put(const struct pool *pp, struct pool_item_header *ph,
343 void *obj)
344 {
345 int idx = pr_item_notouch_index(pp, ph, obj);
346 pool_item_freelist_t *freelist = PR_FREELIST(ph);
347
348 KASSERT(freelist[idx] == PR_INDEX_USED);
349 freelist[idx] = ph->ph_firstfree;
350 ph->ph_firstfree = idx;
351 }
352
353 static inline void *
354 pr_item_notouch_get(const struct pool *pp, struct pool_item_header *ph)
355 {
356 int idx = ph->ph_firstfree;
357 pool_item_freelist_t *freelist = PR_FREELIST(ph);
358
359 KASSERT(freelist[idx] != PR_INDEX_USED);
360 ph->ph_firstfree = freelist[idx];
361 freelist[idx] = PR_INDEX_USED;
362
363 return (char *)ph->ph_page + ph->ph_off + idx * pp->pr_size;
364 }
365
366 static inline int
367 phtree_compare(struct pool_item_header *a, struct pool_item_header *b)
368 {
369
370 /*
371 * we consider pool_item_header with smaller ph_page bigger.
372 * (this unnatural ordering is for the benefit of pr_find_pagehead.)
373 */
374
375 if (a->ph_page < b->ph_page)
376 return (1);
377 else if (a->ph_page > b->ph_page)
378 return (-1);
379 else
380 return (0);
381 }
382
383 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare);
384 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare);
385
386 /*
387 * Return the pool page header based on item address.
388 */
389 static inline struct pool_item_header *
390 pr_find_pagehead(struct pool *pp, void *v)
391 {
392 struct pool_item_header *ph, tmp;
393
394 if ((pp->pr_roflags & PR_NOALIGN) != 0) {
395 tmp.ph_page = (void *)(uintptr_t)v;
396 ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
397 if (ph == NULL) {
398 ph = SPLAY_ROOT(&pp->pr_phtree);
399 if (ph != NULL && phtree_compare(&tmp, ph) >= 0) {
400 ph = SPLAY_NEXT(phtree, &pp->pr_phtree, ph);
401 }
402 KASSERT(ph == NULL || phtree_compare(&tmp, ph) < 0);
403 }
404 } else {
405 void *page =
406 (void *)((uintptr_t)v & pp->pr_alloc->pa_pagemask);
407
408 if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
409 ph = (struct pool_item_header *)((char *)page + pp->pr_phoffset);
410 } else {
411 tmp.ph_page = page;
412 ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp);
413 }
414 }
415
416 KASSERT(ph == NULL || ((pp->pr_roflags & PR_PHINPAGE) != 0) ||
417 ((char *)ph->ph_page <= (char *)v &&
418 (char *)v < (char *)ph->ph_page + pp->pr_alloc->pa_pagesz));
419 return ph;
420 }
421
422 static void
423 pr_pagelist_free(struct pool *pp, struct pool_pagelist *pq)
424 {
425 struct pool_item_header *ph;
426
427 while ((ph = LIST_FIRST(pq)) != NULL) {
428 LIST_REMOVE(ph, ph_pagelist);
429 pool_allocator_free(pp, ph->ph_page);
430 if ((pp->pr_roflags & PR_PHINPAGE) == 0)
431 pool_put(pp->pr_phpool, ph);
432 }
433 }
434
435 /*
436 * Remove a page from the pool.
437 */
438 static inline void
439 pr_rmpage(struct pool *pp, struct pool_item_header *ph,
440 struct pool_pagelist *pq)
441 {
442
443 KASSERT(mutex_owned(&pp->pr_lock));
444
445 /*
446 * If the page was idle, decrement the idle page count.
447 */
448 if (ph->ph_nmissing == 0) {
449 #ifdef DIAGNOSTIC
450 if (pp->pr_nidle == 0)
451 panic("pr_rmpage: nidle inconsistent");
452 if (pp->pr_nitems < pp->pr_itemsperpage)
453 panic("pr_rmpage: nitems inconsistent");
454 #endif
455 pp->pr_nidle--;
456 }
457
458 pp->pr_nitems -= pp->pr_itemsperpage;
459
460 /*
461 * Unlink the page from the pool and queue it for release.
462 */
463 LIST_REMOVE(ph, ph_pagelist);
464 if ((pp->pr_roflags & PR_PHINPAGE) == 0)
465 SPLAY_REMOVE(phtree, &pp->pr_phtree, ph);
466 LIST_INSERT_HEAD(pq, ph, ph_pagelist);
467
468 pp->pr_npages--;
469 pp->pr_npagefree++;
470
471 pool_update_curpage(pp);
472 }
473
474 static bool
475 pa_starved_p(struct pool_allocator *pa)
476 {
477
478 if (pa->pa_backingmap != NULL) {
479 return vm_map_starved_p(pa->pa_backingmap);
480 }
481 return false;
482 }
483
484 static int
485 pool_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)
486 {
487 struct pool *pp = obj;
488 struct pool_allocator *pa = pp->pr_alloc;
489
490 KASSERT(&pp->pr_reclaimerentry == ce);
491 pool_reclaim(pp);
492 if (!pa_starved_p(pa)) {
493 return CALLBACK_CHAIN_ABORT;
494 }
495 return CALLBACK_CHAIN_CONTINUE;
496 }
497
498 static void
499 pool_reclaim_register(struct pool *pp)
500 {
501 struct vm_map *map = pp->pr_alloc->pa_backingmap;
502 int s;
503
504 if (map == NULL) {
505 return;
506 }
507
508 s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
509 callback_register(&vm_map_to_kernel(map)->vmk_reclaim_callback,
510 &pp->pr_reclaimerentry, pp, pool_reclaim_callback);
511 splx(s);
512 }
513
514 static void
515 pool_reclaim_unregister(struct pool *pp)
516 {
517 struct vm_map *map = pp->pr_alloc->pa_backingmap;
518 int s;
519
520 if (map == NULL) {
521 return;
522 }
523
524 s = splvm(); /* not necessary for INTRSAFE maps, but don't care. */
525 callback_unregister(&vm_map_to_kernel(map)->vmk_reclaim_callback,
526 &pp->pr_reclaimerentry);
527 splx(s);
528 }
529
530 static void
531 pa_reclaim_register(struct pool_allocator *pa)
532 {
533 struct vm_map *map = *pa->pa_backingmapptr;
534 struct pool *pp;
535
536 KASSERT(pa->pa_backingmap == NULL);
537 if (map == NULL) {
538 SLIST_INSERT_HEAD(&pa_deferinitq, pa, pa_q);
539 return;
540 }
541 pa->pa_backingmap = map;
542 TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) {
543 pool_reclaim_register(pp);
544 }
545 }
546
547 /*
548 * Initialize all the pools listed in the "pools" link set.
549 */
550 void
551 pool_subsystem_init(void)
552 {
553 struct pool_allocator *pa;
554 __link_set_decl(pools, struct link_pool_init);
555 struct link_pool_init * const *pi;
556
557 mutex_init(&pool_head_lock, MUTEX_DEFAULT, IPL_NONE);
558
559 __link_set_foreach(pi, pools)
560 pool_init((*pi)->pp, (*pi)->size, (*pi)->align,
561 (*pi)->align_offset, (*pi)->flags, (*pi)->wchan,
562 (*pi)->palloc, (*pi)->ipl);
563
564 while ((pa = SLIST_FIRST(&pa_deferinitq)) != NULL) {
565 KASSERT(pa->pa_backingmapptr != NULL);
566 KASSERT(*pa->pa_backingmapptr != NULL);
567 SLIST_REMOVE_HEAD(&pa_deferinitq, pa_q);
568 pa_reclaim_register(pa);
569 }
570 }
571
572 /*
573 * Initialize the given pool resource structure.
574 *
575 * We export this routine to allow other kernel parts to declare
576 * static pools that must be initialized before malloc() is available.
577 */
578 void
579 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
580 const char *wchan, struct pool_allocator *palloc, int ipl)
581 {
582 #ifdef DEBUG
583 struct pool *pp1;
584 #endif
585 size_t trysize, phsize;
586 int off, slack;
587
588 KASSERT((1UL << (CHAR_BIT * sizeof(pool_item_freelist_t))) - 2 >=
589 PHPOOL_FREELIST_NELEM(PHPOOL_MAX - 1));
590
591 #ifdef DEBUG
592 /*
593 * Check that the pool hasn't already been initialised and
594 * added to the list of all pools.
595 */
596 LIST_FOREACH(pp1, &pool_head, pr_poollist) {
597 if (pp == pp1)
598 panic("pool_init: pool %s already initialised",
599 wchan);
600 }
601 #endif
602
603 #ifdef POOL_DIAGNOSTIC
604 /*
605 * Always log if POOL_DIAGNOSTIC is defined.
606 */
607 if (pool_logsize != 0)
608 flags |= PR_LOGGING;
609 #endif
610
611 if (palloc == NULL)
612 palloc = &pool_allocator_kmem;
613 #ifdef POOL_SUBPAGE
614 if (size > palloc->pa_pagesz) {
615 if (palloc == &pool_allocator_kmem)
616 palloc = &pool_allocator_kmem_fullpage;
617 else if (palloc == &pool_allocator_nointr)
618 palloc = &pool_allocator_nointr_fullpage;
619 }
620 #endif /* POOL_SUBPAGE */
621 if ((palloc->pa_flags & PA_INITIALIZED) == 0) {
622 if (palloc->pa_pagesz == 0)
623 palloc->pa_pagesz = PAGE_SIZE;
624
625 TAILQ_INIT(&palloc->pa_list);
626
627 mutex_init(&palloc->pa_lock, MUTEX_DRIVER, IPL_VM);
628 palloc->pa_pagemask = ~(palloc->pa_pagesz - 1);
629 palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1;
630
631 if (palloc->pa_backingmapptr != NULL) {
632 pa_reclaim_register(palloc);
633 }
634 palloc->pa_flags |= PA_INITIALIZED;
635 }
636
637 if (align == 0)
638 align = ALIGN(1);
639
640 if ((flags & PR_NOTOUCH) == 0 && size < sizeof(struct pool_item))
641 size = sizeof(struct pool_item);
642
643 size = roundup(size, align);
644 #ifdef DIAGNOSTIC
645 if (size > palloc->pa_pagesz)
646 panic("pool_init: pool item size (%zu) too large", size);
647 #endif
648
649 /*
650 * Initialize the pool structure.
651 */
652 LIST_INIT(&pp->pr_emptypages);
653 LIST_INIT(&pp->pr_fullpages);
654 LIST_INIT(&pp->pr_partpages);
655 LIST_INIT(&pp->pr_cachelist);
656 pp->pr_curpage = NULL;
657 pp->pr_npages = 0;
658 pp->pr_minitems = 0;
659 pp->pr_minpages = 0;
660 pp->pr_maxpages = UINT_MAX;
661 pp->pr_roflags = flags;
662 pp->pr_flags = 0;
663 pp->pr_size = size;
664 pp->pr_align = align;
665 pp->pr_wchan = wchan;
666 pp->pr_alloc = palloc;
667 pp->pr_nitems = 0;
668 pp->pr_nout = 0;
669 pp->pr_hardlimit = UINT_MAX;
670 pp->pr_hardlimit_warning = NULL;
671 pp->pr_hardlimit_ratecap.tv_sec = 0;
672 pp->pr_hardlimit_ratecap.tv_usec = 0;
673 pp->pr_hardlimit_warning_last.tv_sec = 0;
674 pp->pr_hardlimit_warning_last.tv_usec = 0;
675 pp->pr_drain_hook = NULL;
676 pp->pr_drain_hook_arg = NULL;
677 pp->pr_freecheck = NULL;
678
679 /*
680 * Decide whether to put the page header off page to avoid
681 * wasting too large a part of the page or too big item.
682 * Off-page page headers go on a hash table, so we can match
683 * a returned item with its header based on the page address.
684 * We use 1/16 of the page size and about 8 times of the item
685 * size as the threshold (XXX: tune)
686 *
687 * However, we'll put the header into the page if we can put
688 * it without wasting any items.
689 *
690 * Silently enforce `0 <= ioff < align'.
691 */
692 pp->pr_itemoffset = ioff %= align;
693 /* See the comment below about reserved bytes. */
694 trysize = palloc->pa_pagesz - ((align - ioff) % align);
695 phsize = ALIGN(sizeof(struct pool_item_header));
696 if ((pp->pr_roflags & (PR_NOTOUCH | PR_NOALIGN)) == 0 &&
697 (pp->pr_size < MIN(palloc->pa_pagesz / 16, phsize << 3) ||
698 trysize / pp->pr_size == (trysize - phsize) / pp->pr_size)) {
699 /* Use the end of the page for the page header */
700 pp->pr_roflags |= PR_PHINPAGE;
701 pp->pr_phoffset = off = palloc->pa_pagesz - phsize;
702 } else {
703 /* The page header will be taken from our page header pool */
704 pp->pr_phoffset = 0;
705 off = palloc->pa_pagesz;
706 SPLAY_INIT(&pp->pr_phtree);
707 }
708
709 /*
710 * Alignment is to take place at `ioff' within the item. This means
711 * we must reserve up to `align - 1' bytes on the page to allow
712 * appropriate positioning of each item.
713 */
714 pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
715 KASSERT(pp->pr_itemsperpage != 0);
716 if ((pp->pr_roflags & PR_NOTOUCH)) {
717 int idx;
718
719 for (idx = 0; pp->pr_itemsperpage > PHPOOL_FREELIST_NELEM(idx);
720 idx++) {
721 /* nothing */
722 }
723 if (idx >= PHPOOL_MAX) {
724 /*
725 * if you see this panic, consider to tweak
726 * PHPOOL_MAX and PHPOOL_FREELIST_NELEM.
727 */
728 panic("%s: too large itemsperpage(%d) for PR_NOTOUCH",
729 pp->pr_wchan, pp->pr_itemsperpage);
730 }
731 pp->pr_phpool = &phpool[idx];
732 } else if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
733 pp->pr_phpool = &phpool[0];
734 }
735 #if defined(DIAGNOSTIC)
736 else {
737 pp->pr_phpool = NULL;
738 }
739 #endif
740
741 /*
742 * Use the slack between the chunks and the page header
743 * for "cache coloring".
744 */
745 slack = off - pp->pr_itemsperpage * pp->pr_size;
746 pp->pr_maxcolor = (slack / align) * align;
747 pp->pr_curcolor = 0;
748
749 pp->pr_nget = 0;
750 pp->pr_nfail = 0;
751 pp->pr_nput = 0;
752 pp->pr_npagealloc = 0;
753 pp->pr_npagefree = 0;
754 pp->pr_hiwat = 0;
755 pp->pr_nidle = 0;
756
757 #ifdef POOL_DIAGNOSTIC
758 if (flags & PR_LOGGING) {
759 if (kmem_map == NULL ||
760 (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
761 M_TEMP, M_NOWAIT)) == NULL)
762 pp->pr_roflags &= ~PR_LOGGING;
763 pp->pr_curlogentry = 0;
764 pp->pr_logsize = pool_logsize;
765 }
766 #endif
767
768 pp->pr_entered_file = NULL;
769 pp->pr_entered_line = 0;
770
771 mutex_init(&pp->pr_lock, MUTEX_DRIVER, ipl);
772 cv_init(&pp->pr_cv, wchan);
773 pp->pr_ipl = ipl;
774
775 /*
776 * Initialize private page header pool and cache magazine pool if we
777 * haven't done so yet.
778 * XXX LOCKING.
779 */
780 if (phpool[0].pr_size == 0) {
781 int idx;
782 for (idx = 0; idx < PHPOOL_MAX; idx++) {
783 static char phpool_names[PHPOOL_MAX][6+1+6+1];
784 int nelem;
785 size_t sz;
786
787 nelem = PHPOOL_FREELIST_NELEM(idx);
788 snprintf(phpool_names[idx], sizeof(phpool_names[idx]),
789 "phpool-%d", nelem);
790 sz = sizeof(struct pool_item_header);
791 if (nelem) {
792 sz = PR_FREELIST_ALIGN(sz)
793 + nelem * sizeof(pool_item_freelist_t);
794 }
795 pool_init(&phpool[idx], sz, 0, 0, 0,
796 phpool_names[idx], &pool_allocator_meta, IPL_VM);
797 }
798 #ifdef POOL_SUBPAGE
799 pool_init(&psppool, POOL_SUBPAGE, POOL_SUBPAGE, 0,
800 PR_RECURSIVE, "psppool", &pool_allocator_meta, IPL_VM);
801 #endif
802 pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0,
803 0, "pcgpool", &pool_allocator_meta, IPL_VM);
804 }
805
806 if (__predict_true(!cold)) {
807 /* Insert into the list of all pools. */
808 mutex_enter(&pool_head_lock);
809 LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
810 mutex_exit(&pool_head_lock);
811
812 /* Insert this into the list of pools using this allocator. */
813 mutex_enter(&palloc->pa_lock);
814 TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
815 mutex_exit(&palloc->pa_lock);
816 } else {
817 LIST_INSERT_HEAD(&pool_head, pp, pr_poollist);
818 TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list);
819 }
820
821 pool_reclaim_register(pp);
822 }
823
824 /*
825 * De-commision a pool resource.
826 */
827 void
828 pool_destroy(struct pool *pp)
829 {
830 struct pool_pagelist pq;
831 struct pool_item_header *ph;
832
833 /* Remove from global pool list */
834 mutex_enter(&pool_head_lock);
835 LIST_REMOVE(pp, pr_poollist);
836 if (drainpp == pp)
837 drainpp = NULL;
838 mutex_exit(&pool_head_lock);
839
840 /* Remove this pool from its allocator's list of pools. */
841 pool_reclaim_unregister(pp);
842 mutex_enter(&pp->pr_alloc->pa_lock);
843 TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list);
844 mutex_exit(&pp->pr_alloc->pa_lock);
845
846 mutex_enter(&pp->pr_lock);
847
848 KASSERT(LIST_EMPTY(&pp->pr_cachelist));
849
850 #ifdef DIAGNOSTIC
851 if (pp->pr_nout != 0) {
852 pr_printlog(pp, NULL, printf);
853 panic("pool_destroy: pool busy: still out: %u",
854 pp->pr_nout);
855 }
856 #endif
857
858 KASSERT(LIST_EMPTY(&pp->pr_fullpages));
859 KASSERT(LIST_EMPTY(&pp->pr_partpages));
860
861 /* Remove all pages */
862 LIST_INIT(&pq);
863 while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
864 pr_rmpage(pp, ph, &pq);
865
866 mutex_exit(&pp->pr_lock);
867
868 pr_pagelist_free(pp, &pq);
869
870 #ifdef POOL_DIAGNOSTIC
871 if ((pp->pr_roflags & PR_LOGGING) != 0)
872 free(pp->pr_log, M_TEMP);
873 #endif
874
875 cv_destroy(&pp->pr_cv);
876 mutex_destroy(&pp->pr_lock);
877 }
878
879 void
880 pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg)
881 {
882
883 /* XXX no locking -- must be used just after pool_init() */
884 #ifdef DIAGNOSTIC
885 if (pp->pr_drain_hook != NULL)
886 panic("pool_set_drain_hook(%s): already set", pp->pr_wchan);
887 #endif
888 pp->pr_drain_hook = fn;
889 pp->pr_drain_hook_arg = arg;
890 }
891
892 static struct pool_item_header *
893 pool_alloc_item_header(struct pool *pp, void *storage, int flags)
894 {
895 struct pool_item_header *ph;
896
897 if ((pp->pr_roflags & PR_PHINPAGE) != 0)
898 ph = (struct pool_item_header *) ((char *)storage + pp->pr_phoffset);
899 else
900 ph = pool_get(pp->pr_phpool, flags);
901
902 return (ph);
903 }
904
905 /*
906 * Grab an item from the pool; must be called at appropriate spl level
907 */
908 void *
909 #ifdef POOL_DIAGNOSTIC
910 _pool_get(struct pool *pp, int flags, const char *file, long line)
911 #else
912 pool_get(struct pool *pp, int flags)
913 #endif
914 {
915 struct pool_item *pi;
916 struct pool_item_header *ph;
917 void *v;
918
919 #ifdef DIAGNOSTIC
920 if (__predict_false(pp->pr_itemsperpage == 0))
921 panic("pool_get: pool %p: pr_itemsperpage is zero, "
922 "pool not initialized?", pp);
923 if (__predict_false(curlwp == NULL && doing_shutdown == 0 &&
924 (flags & PR_WAITOK) != 0))
925 panic("pool_get: %s: must have NOWAIT", pp->pr_wchan);
926
927 #endif /* DIAGNOSTIC */
928 #ifdef LOCKDEBUG
929 if (flags & PR_WAITOK)
930 ASSERT_SLEEPABLE(NULL, "pool_get(PR_WAITOK)");
931 #endif
932
933 mutex_enter(&pp->pr_lock);
934 pr_enter(pp, file, line);
935
936 startover:
937 /*
938 * Check to see if we've reached the hard limit. If we have,
939 * and we can wait, then wait until an item has been returned to
940 * the pool.
941 */
942 #ifdef DIAGNOSTIC
943 if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
944 pr_leave(pp);
945 mutex_exit(&pp->pr_lock);
946 panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
947 }
948 #endif
949 if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
950 if (pp->pr_drain_hook != NULL) {
951 /*
952 * Since the drain hook is going to free things
953 * back to the pool, unlock, call the hook, re-lock,
954 * and check the hardlimit condition again.
955 */
956 pr_leave(pp);
957 mutex_exit(&pp->pr_lock);
958 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
959 mutex_enter(&pp->pr_lock);
960 pr_enter(pp, file, line);
961 if (pp->pr_nout < pp->pr_hardlimit)
962 goto startover;
963 }
964
965 if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
966 /*
967 * XXX: A warning isn't logged in this case. Should
968 * it be?
969 */
970 pp->pr_flags |= PR_WANTED;
971 pr_leave(pp);
972 cv_wait(&pp->pr_cv, &pp->pr_lock);
973 pr_enter(pp, file, line);
974 goto startover;
975 }
976
977 /*
978 * Log a message that the hard limit has been hit.
979 */
980 if (pp->pr_hardlimit_warning != NULL &&
981 ratecheck(&pp->pr_hardlimit_warning_last,
982 &pp->pr_hardlimit_ratecap))
983 log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
984
985 pp->pr_nfail++;
986
987 pr_leave(pp);
988 mutex_exit(&pp->pr_lock);
989 return (NULL);
990 }
991
992 /*
993 * The convention we use is that if `curpage' is not NULL, then
994 * it points at a non-empty bucket. In particular, `curpage'
995 * never points at a page header which has PR_PHINPAGE set and
996 * has no items in its bucket.
997 */
998 if ((ph = pp->pr_curpage) == NULL) {
999 int error;
1000
1001 #ifdef DIAGNOSTIC
1002 if (pp->pr_nitems != 0) {
1003 mutex_exit(&pp->pr_lock);
1004 printf("pool_get: %s: curpage NULL, nitems %u\n",
1005 pp->pr_wchan, pp->pr_nitems);
1006 panic("pool_get: nitems inconsistent");
1007 }
1008 #endif
1009
1010 /*
1011 * Call the back-end page allocator for more memory.
1012 * Release the pool lock, as the back-end page allocator
1013 * may block.
1014 */
1015 pr_leave(pp);
1016 error = pool_grow(pp, flags);
1017 pr_enter(pp, file, line);
1018 if (error != 0) {
1019 /*
1020 * We were unable to allocate a page or item
1021 * header, but we released the lock during
1022 * allocation, so perhaps items were freed
1023 * back to the pool. Check for this case.
1024 */
1025 if (pp->pr_curpage != NULL)
1026 goto startover;
1027
1028 pp->pr_nfail++;
1029 pr_leave(pp);
1030 mutex_exit(&pp->pr_lock);
1031 return (NULL);
1032 }
1033
1034 /* Start the allocation process over. */
1035 goto startover;
1036 }
1037 if (pp->pr_roflags & PR_NOTOUCH) {
1038 #ifdef DIAGNOSTIC
1039 if (__predict_false(ph->ph_nmissing == pp->pr_itemsperpage)) {
1040 pr_leave(pp);
1041 mutex_exit(&pp->pr_lock);
1042 panic("pool_get: %s: page empty", pp->pr_wchan);
1043 }
1044 #endif
1045 v = pr_item_notouch_get(pp, ph);
1046 #ifdef POOL_DIAGNOSTIC
1047 pr_log(pp, v, PRLOG_GET, file, line);
1048 #endif
1049 } else {
1050 v = pi = LIST_FIRST(&ph->ph_itemlist);
1051 if (__predict_false(v == NULL)) {
1052 pr_leave(pp);
1053 mutex_exit(&pp->pr_lock);
1054 panic("pool_get: %s: page empty", pp->pr_wchan);
1055 }
1056 #ifdef DIAGNOSTIC
1057 if (__predict_false(pp->pr_nitems == 0)) {
1058 pr_leave(pp);
1059 mutex_exit(&pp->pr_lock);
1060 printf("pool_get: %s: items on itemlist, nitems %u\n",
1061 pp->pr_wchan, pp->pr_nitems);
1062 panic("pool_get: nitems inconsistent");
1063 }
1064 #endif
1065
1066 #ifdef POOL_DIAGNOSTIC
1067 pr_log(pp, v, PRLOG_GET, file, line);
1068 #endif
1069
1070 #ifdef DIAGNOSTIC
1071 if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1072 pr_printlog(pp, pi, printf);
1073 panic("pool_get(%s): free list modified: "
1074 "magic=%x; page %p; item addr %p\n",
1075 pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
1076 }
1077 #endif
1078
1079 /*
1080 * Remove from item list.
1081 */
1082 LIST_REMOVE(pi, pi_list);
1083 }
1084 pp->pr_nitems--;
1085 pp->pr_nout++;
1086 if (ph->ph_nmissing == 0) {
1087 #ifdef DIAGNOSTIC
1088 if (__predict_false(pp->pr_nidle == 0))
1089 panic("pool_get: nidle inconsistent");
1090 #endif
1091 pp->pr_nidle--;
1092
1093 /*
1094 * This page was previously empty. Move it to the list of
1095 * partially-full pages. This page is already curpage.
1096 */
1097 LIST_REMOVE(ph, ph_pagelist);
1098 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1099 }
1100 ph->ph_nmissing++;
1101 if (ph->ph_nmissing == pp->pr_itemsperpage) {
1102 #ifdef DIAGNOSTIC
1103 if (__predict_false((pp->pr_roflags & PR_NOTOUCH) == 0 &&
1104 !LIST_EMPTY(&ph->ph_itemlist))) {
1105 pr_leave(pp);
1106 mutex_exit(&pp->pr_lock);
1107 panic("pool_get: %s: nmissing inconsistent",
1108 pp->pr_wchan);
1109 }
1110 #endif
1111 /*
1112 * This page is now full. Move it to the full list
1113 * and select a new current page.
1114 */
1115 LIST_REMOVE(ph, ph_pagelist);
1116 LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist);
1117 pool_update_curpage(pp);
1118 }
1119
1120 pp->pr_nget++;
1121 pr_leave(pp);
1122
1123 /*
1124 * If we have a low water mark and we are now below that low
1125 * water mark, add more items to the pool.
1126 */
1127 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1128 /*
1129 * XXX: Should we log a warning? Should we set up a timeout
1130 * to try again in a second or so? The latter could break
1131 * a caller's assumptions about interrupt protection, etc.
1132 */
1133 }
1134
1135 mutex_exit(&pp->pr_lock);
1136 KASSERT((((vaddr_t)v + pp->pr_itemoffset) & (pp->pr_align - 1)) == 0);
1137 FREECHECK_OUT(&pp->pr_freecheck, v);
1138 return (v);
1139 }
1140
1141 /*
1142 * Internal version of pool_put(). Pool is already locked/entered.
1143 */
1144 static void
1145 pool_do_put(struct pool *pp, void *v, struct pool_pagelist *pq)
1146 {
1147 struct pool_item *pi = v;
1148 struct pool_item_header *ph;
1149
1150 KASSERT(mutex_owned(&pp->pr_lock));
1151 FREECHECK_IN(&pp->pr_freecheck, v);
1152 LOCKDEBUG_MEM_CHECK(v, pp->pr_size);
1153
1154 #ifdef DIAGNOSTIC
1155 if (__predict_false(pp->pr_nout == 0)) {
1156 printf("pool %s: putting with none out\n",
1157 pp->pr_wchan);
1158 panic("pool_put");
1159 }
1160 #endif
1161
1162 if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) {
1163 pr_printlog(pp, NULL, printf);
1164 panic("pool_put: %s: page header missing", pp->pr_wchan);
1165 }
1166
1167 /*
1168 * Return to item list.
1169 */
1170 if (pp->pr_roflags & PR_NOTOUCH) {
1171 pr_item_notouch_put(pp, ph, v);
1172 } else {
1173 #ifdef DIAGNOSTIC
1174 pi->pi_magic = PI_MAGIC;
1175 #endif
1176 #ifdef DEBUG
1177 {
1178 int i, *ip = v;
1179
1180 for (i = 0; i < pp->pr_size / sizeof(int); i++) {
1181 *ip++ = PI_MAGIC;
1182 }
1183 }
1184 #endif
1185
1186 LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1187 }
1188 KDASSERT(ph->ph_nmissing != 0);
1189 ph->ph_nmissing--;
1190 pp->pr_nput++;
1191 pp->pr_nitems++;
1192 pp->pr_nout--;
1193
1194 /* Cancel "pool empty" condition if it exists */
1195 if (pp->pr_curpage == NULL)
1196 pp->pr_curpage = ph;
1197
1198 if (pp->pr_flags & PR_WANTED) {
1199 pp->pr_flags &= ~PR_WANTED;
1200 if (ph->ph_nmissing == 0)
1201 pp->pr_nidle++;
1202 cv_broadcast(&pp->pr_cv);
1203 return;
1204 }
1205
1206 /*
1207 * If this page is now empty, do one of two things:
1208 *
1209 * (1) If we have more pages than the page high water mark,
1210 * free the page back to the system. ONLY CONSIDER
1211 * FREEING BACK A PAGE IF WE HAVE MORE THAN OUR MINIMUM PAGE
1212 * CLAIM.
1213 *
1214 * (2) Otherwise, move the page to the empty page list.
1215 *
1216 * Either way, select a new current page (so we use a partially-full
1217 * page if one is available).
1218 */
1219 if (ph->ph_nmissing == 0) {
1220 pp->pr_nidle++;
1221 if (pp->pr_npages > pp->pr_minpages &&
1222 (pp->pr_npages > pp->pr_maxpages ||
1223 pa_starved_p(pp->pr_alloc))) {
1224 pr_rmpage(pp, ph, pq);
1225 } else {
1226 LIST_REMOVE(ph, ph_pagelist);
1227 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1228
1229 /*
1230 * Update the timestamp on the page. A page must
1231 * be idle for some period of time before it can
1232 * be reclaimed by the pagedaemon. This minimizes
1233 * ping-pong'ing for memory.
1234 */
1235 getmicrotime(&ph->ph_time);
1236 }
1237 pool_update_curpage(pp);
1238 }
1239
1240 /*
1241 * If the page was previously completely full, move it to the
1242 * partially-full list and make it the current page. The next
1243 * allocation will get the item from this page, instead of
1244 * further fragmenting the pool.
1245 */
1246 else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
1247 LIST_REMOVE(ph, ph_pagelist);
1248 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist);
1249 pp->pr_curpage = ph;
1250 }
1251 }
1252
1253 /*
1254 * Return resource to the pool; must be called at appropriate spl level
1255 */
1256 #ifdef POOL_DIAGNOSTIC
1257 void
1258 _pool_put(struct pool *pp, void *v, const char *file, long line)
1259 {
1260 struct pool_pagelist pq;
1261
1262 LIST_INIT(&pq);
1263
1264 mutex_enter(&pp->pr_lock);
1265 pr_enter(pp, file, line);
1266
1267 pr_log(pp, v, PRLOG_PUT, file, line);
1268
1269 pool_do_put(pp, v, &pq);
1270
1271 pr_leave(pp);
1272 mutex_exit(&pp->pr_lock);
1273
1274 pr_pagelist_free(pp, &pq);
1275 }
1276 #undef pool_put
1277 #endif /* POOL_DIAGNOSTIC */
1278
1279 void
1280 pool_put(struct pool *pp, void *v)
1281 {
1282 struct pool_pagelist pq;
1283
1284 LIST_INIT(&pq);
1285
1286 mutex_enter(&pp->pr_lock);
1287 pool_do_put(pp, v, &pq);
1288 mutex_exit(&pp->pr_lock);
1289
1290 pr_pagelist_free(pp, &pq);
1291 }
1292
1293 #ifdef POOL_DIAGNOSTIC
1294 #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__)
1295 #endif
1296
1297 /*
1298 * pool_grow: grow a pool by a page.
1299 *
1300 * => called with pool locked.
1301 * => unlock and relock the pool.
1302 * => return with pool locked.
1303 */
1304
1305 static int
1306 pool_grow(struct pool *pp, int flags)
1307 {
1308 struct pool_item_header *ph = NULL;
1309 char *cp;
1310
1311 mutex_exit(&pp->pr_lock);
1312 cp = pool_allocator_alloc(pp, flags);
1313 if (__predict_true(cp != NULL)) {
1314 ph = pool_alloc_item_header(pp, cp, flags);
1315 }
1316 if (__predict_false(cp == NULL || ph == NULL)) {
1317 if (cp != NULL) {
1318 pool_allocator_free(pp, cp);
1319 }
1320 mutex_enter(&pp->pr_lock);
1321 return ENOMEM;
1322 }
1323
1324 mutex_enter(&pp->pr_lock);
1325 pool_prime_page(pp, cp, ph);
1326 pp->pr_npagealloc++;
1327 return 0;
1328 }
1329
1330 /*
1331 * Add N items to the pool.
1332 */
1333 int
1334 pool_prime(struct pool *pp, int n)
1335 {
1336 int newpages;
1337 int error = 0;
1338
1339 mutex_enter(&pp->pr_lock);
1340
1341 newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1342
1343 while (newpages-- > 0) {
1344 error = pool_grow(pp, PR_NOWAIT);
1345 if (error) {
1346 break;
1347 }
1348 pp->pr_minpages++;
1349 }
1350
1351 if (pp->pr_minpages >= pp->pr_maxpages)
1352 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
1353
1354 mutex_exit(&pp->pr_lock);
1355 return error;
1356 }
1357
1358 /*
1359 * Add a page worth of items to the pool.
1360 *
1361 * Note, we must be called with the pool descriptor LOCKED.
1362 */
1363 static void
1364 pool_prime_page(struct pool *pp, void *storage, struct pool_item_header *ph)
1365 {
1366 struct pool_item *pi;
1367 void *cp = storage;
1368 const unsigned int align = pp->pr_align;
1369 const unsigned int ioff = pp->pr_itemoffset;
1370 int n;
1371
1372 KASSERT(mutex_owned(&pp->pr_lock));
1373
1374 #ifdef DIAGNOSTIC
1375 if ((pp->pr_roflags & PR_NOALIGN) == 0 &&
1376 ((uintptr_t)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0)
1377 panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1378 #endif
1379
1380 /*
1381 * Insert page header.
1382 */
1383 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist);
1384 LIST_INIT(&ph->ph_itemlist);
1385 ph->ph_page = storage;
1386 ph->ph_nmissing = 0;
1387 getmicrotime(&ph->ph_time);
1388 if ((pp->pr_roflags & PR_PHINPAGE) == 0)
1389 SPLAY_INSERT(phtree, &pp->pr_phtree, ph);
1390
1391 pp->pr_nidle++;
1392
1393 /*
1394 * Color this page.
1395 */
1396 cp = (char *)cp + pp->pr_curcolor;
1397 if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
1398 pp->pr_curcolor = 0;
1399
1400 /*
1401 * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1402 */
1403 if (ioff != 0)
1404 cp = (char *)cp + align - ioff;
1405
1406 KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1407
1408 /*
1409 * Insert remaining chunks on the bucket list.
1410 */
1411 n = pp->pr_itemsperpage;
1412 pp->pr_nitems += n;
1413
1414 if (pp->pr_roflags & PR_NOTOUCH) {
1415 pool_item_freelist_t *freelist = PR_FREELIST(ph);
1416 int i;
1417
1418 ph->ph_off = (char *)cp - (char *)storage;
1419 ph->ph_firstfree = 0;
1420 for (i = 0; i < n - 1; i++)
1421 freelist[i] = i + 1;
1422 freelist[n - 1] = PR_INDEX_EOL;
1423 } else {
1424 while (n--) {
1425 pi = (struct pool_item *)cp;
1426
1427 KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0);
1428
1429 /* Insert on page list */
1430 LIST_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
1431 #ifdef DIAGNOSTIC
1432 pi->pi_magic = PI_MAGIC;
1433 #endif
1434 cp = (char *)cp + pp->pr_size;
1435
1436 KASSERT((((vaddr_t)cp + ioff) & (align - 1)) == 0);
1437 }
1438 }
1439
1440 /*
1441 * If the pool was depleted, point at the new page.
1442 */
1443 if (pp->pr_curpage == NULL)
1444 pp->pr_curpage = ph;
1445
1446 if (++pp->pr_npages > pp->pr_hiwat)
1447 pp->pr_hiwat = pp->pr_npages;
1448 }
1449
1450 /*
1451 * Used by pool_get() when nitems drops below the low water mark. This
1452 * is used to catch up pr_nitems with the low water mark.
1453 *
1454 * Note 1, we never wait for memory here, we let the caller decide what to do.
1455 *
1456 * Note 2, we must be called with the pool already locked, and we return
1457 * with it locked.
1458 */
1459 static int
1460 pool_catchup(struct pool *pp)
1461 {
1462 int error = 0;
1463
1464 while (POOL_NEEDS_CATCHUP(pp)) {
1465 error = pool_grow(pp, PR_NOWAIT);
1466 if (error) {
1467 break;
1468 }
1469 }
1470 return error;
1471 }
1472
1473 static void
1474 pool_update_curpage(struct pool *pp)
1475 {
1476
1477 pp->pr_curpage = LIST_FIRST(&pp->pr_partpages);
1478 if (pp->pr_curpage == NULL) {
1479 pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages);
1480 }
1481 }
1482
1483 void
1484 pool_setlowat(struct pool *pp, int n)
1485 {
1486
1487 mutex_enter(&pp->pr_lock);
1488
1489 pp->pr_minitems = n;
1490 pp->pr_minpages = (n == 0)
1491 ? 0
1492 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1493
1494 /* Make sure we're caught up with the newly-set low water mark. */
1495 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) {
1496 /*
1497 * XXX: Should we log a warning? Should we set up a timeout
1498 * to try again in a second or so? The latter could break
1499 * a caller's assumptions about interrupt protection, etc.
1500 */
1501 }
1502
1503 mutex_exit(&pp->pr_lock);
1504 }
1505
1506 void
1507 pool_sethiwat(struct pool *pp, int n)
1508 {
1509
1510 mutex_enter(&pp->pr_lock);
1511
1512 pp->pr_maxpages = (n == 0)
1513 ? 0
1514 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1515
1516 mutex_exit(&pp->pr_lock);
1517 }
1518
1519 void
1520 pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1521 {
1522
1523 mutex_enter(&pp->pr_lock);
1524
1525 pp->pr_hardlimit = n;
1526 pp->pr_hardlimit_warning = warnmess;
1527 pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1528 pp->pr_hardlimit_warning_last.tv_sec = 0;
1529 pp->pr_hardlimit_warning_last.tv_usec = 0;
1530
1531 /*
1532 * In-line version of pool_sethiwat(), because we don't want to
1533 * release the lock.
1534 */
1535 pp->pr_maxpages = (n == 0)
1536 ? 0
1537 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1538
1539 mutex_exit(&pp->pr_lock);
1540 }
1541
1542 /*
1543 * Release all complete pages that have not been used recently.
1544 */
1545 int
1546 #ifdef POOL_DIAGNOSTIC
1547 _pool_reclaim(struct pool *pp, const char *file, long line)
1548 #else
1549 pool_reclaim(struct pool *pp)
1550 #endif
1551 {
1552 struct pool_item_header *ph, *phnext;
1553 struct pool_cache *pc;
1554 struct pool_pagelist pq;
1555 struct pool_cache_grouplist pcgl;
1556 struct timeval curtime, diff;
1557
1558 if (pp->pr_drain_hook != NULL) {
1559 /*
1560 * The drain hook must be called with the pool unlocked.
1561 */
1562 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT);
1563 }
1564
1565 if (mutex_tryenter(&pp->pr_lock) == 0)
1566 return (0);
1567 pr_enter(pp, file, line);
1568
1569 LIST_INIT(&pq);
1570 LIST_INIT(&pcgl);
1571
1572 /*
1573 * Reclaim items from the pool's caches.
1574 */
1575 LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist)
1576 pool_cache_reclaim(pc, &pq, &pcgl);
1577
1578 getmicrotime(&curtime);
1579
1580 for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) {
1581 phnext = LIST_NEXT(ph, ph_pagelist);
1582
1583 /* Check our minimum page claim */
1584 if (pp->pr_npages <= pp->pr_minpages)
1585 break;
1586
1587 KASSERT(ph->ph_nmissing == 0);
1588 timersub(&curtime, &ph->ph_time, &diff);
1589 if (diff.tv_sec < pool_inactive_time
1590 && !pa_starved_p(pp->pr_alloc))
1591 continue;
1592
1593 /*
1594 * If freeing this page would put us below
1595 * the low water mark, stop now.
1596 */
1597 if ((pp->pr_nitems - pp->pr_itemsperpage) <
1598 pp->pr_minitems)
1599 break;
1600
1601 pr_rmpage(pp, ph, &pq);
1602 }
1603
1604 pr_leave(pp);
1605 mutex_exit(&pp->pr_lock);
1606 if (LIST_EMPTY(&pq) && LIST_EMPTY(&pcgl))
1607 return 0;
1608
1609 pr_pagelist_free(pp, &pq);
1610 pcg_grouplist_free(&pcgl);
1611 return (1);
1612 }
1613
1614 /*
1615 * Drain pools, one at a time.
1616 *
1617 * Note, we must never be called from an interrupt context.
1618 *
1619 * XXX Pool can disappear while draining.
1620 */
1621 void
1622 pool_drain(void *arg)
1623 {
1624 struct pool *pp;
1625 int s;
1626
1627 pp = NULL;
1628 s = splvm(); /* XXX why? */
1629 mutex_enter(&pool_head_lock);
1630 if (drainpp == NULL) {
1631 drainpp = LIST_FIRST(&pool_head);
1632 }
1633 if (drainpp) {
1634 pp = drainpp;
1635 drainpp = LIST_NEXT(pp, pr_poollist);
1636 }
1637 mutex_exit(&pool_head_lock);
1638 if (pp)
1639 pool_reclaim(pp);
1640 splx(s);
1641 }
1642
1643 /*
1644 * Diagnostic helpers.
1645 */
1646 void
1647 pool_print(struct pool *pp, const char *modif)
1648 {
1649
1650 if (mutex_tryenter(&pp->pr_lock) == 0) {
1651 printf("pool %s is locked; try again later\n",
1652 pp->pr_wchan);
1653 return;
1654 }
1655 pool_print1(pp, modif, printf);
1656 mutex_exit(&pp->pr_lock);
1657 }
1658
1659 void
1660 pool_printall(const char *modif, void (*pr)(const char *, ...))
1661 {
1662 struct pool *pp;
1663
1664 if (mutex_tryenter(&pool_head_lock) == 0) {
1665 (*pr)("WARNING: pool_head_slock is locked\n");
1666 } else {
1667 mutex_exit(&pool_head_lock);
1668 }
1669
1670 LIST_FOREACH(pp, &pool_head, pr_poollist) {
1671 pool_printit(pp, modif, pr);
1672 }
1673 }
1674
1675 void
1676 pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1677 {
1678
1679 if (pp == NULL) {
1680 (*pr)("Must specify a pool to print.\n");
1681 return;
1682 }
1683
1684 /*
1685 * Called from DDB; interrupts should be blocked, and all
1686 * other processors should be paused. We can skip locking
1687 * the pool in this case.
1688 *
1689 * We do a mutex_tryenter() just to print the lock
1690 * status, however.
1691 */
1692
1693 if (mutex_tryenter(&pp->pr_lock) == 0)
1694 (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1695 else
1696 mutex_exit(&pp->pr_lock);
1697
1698 pool_print1(pp, modif, pr);
1699 }
1700
1701 static void
1702 pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1703 void (*pr)(const char *, ...))
1704 {
1705 struct pool_item_header *ph;
1706 #ifdef DIAGNOSTIC
1707 struct pool_item *pi;
1708 #endif
1709
1710 LIST_FOREACH(ph, pl, ph_pagelist) {
1711 (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1712 ph->ph_page, ph->ph_nmissing,
1713 (u_long)ph->ph_time.tv_sec,
1714 (u_long)ph->ph_time.tv_usec);
1715 #ifdef DIAGNOSTIC
1716 if (!(pp->pr_roflags & PR_NOTOUCH)) {
1717 LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1718 if (pi->pi_magic != PI_MAGIC) {
1719 (*pr)("\t\t\titem %p, magic 0x%x\n",
1720 pi, pi->pi_magic);
1721 }
1722 }
1723 }
1724 #endif
1725 }
1726 }
1727
1728 static void
1729 pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1730 {
1731 struct pool_item_header *ph;
1732 struct pool_cache *pc;
1733 struct pool_cache_group *pcg;
1734 int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1735 char c;
1736
1737 while ((c = *modif++) != '\0') {
1738 if (c == 'l')
1739 print_log = 1;
1740 if (c == 'p')
1741 print_pagelist = 1;
1742 if (c == 'c')
1743 print_cache = 1;
1744 }
1745
1746 (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1747 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1748 pp->pr_roflags);
1749 (*pr)("\talloc %p\n", pp->pr_alloc);
1750 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1751 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1752 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1753 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1754
1755 (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1756 pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1757 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1758 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1759
1760 if (print_pagelist == 0)
1761 goto skip_pagelist;
1762
1763 if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1764 (*pr)("\n\tempty page list:\n");
1765 pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1766 if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1767 (*pr)("\n\tfull page list:\n");
1768 pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1769 if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1770 (*pr)("\n\tpartial-page list:\n");
1771 pool_print_pagelist(pp, &pp->pr_partpages, pr);
1772
1773 if (pp->pr_curpage == NULL)
1774 (*pr)("\tno current page\n");
1775 else
1776 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1777
1778 skip_pagelist:
1779 if (print_log == 0)
1780 goto skip_log;
1781
1782 (*pr)("\n");
1783 if ((pp->pr_roflags & PR_LOGGING) == 0)
1784 (*pr)("\tno log\n");
1785 else {
1786 pr_printlog(pp, NULL, pr);
1787 }
1788
1789 skip_log:
1790 if (print_cache == 0)
1791 goto skip_cache;
1792
1793 #define PR_GROUPLIST(pcg) \
1794 (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1795 for (i = 0; i < PCG_NOBJECTS; i++) { \
1796 if (pcg->pcg_objects[i].pcgo_pa != \
1797 POOL_PADDR_INVALID) { \
1798 (*pr)("\t\t\t%p, 0x%llx\n", \
1799 pcg->pcg_objects[i].pcgo_va, \
1800 (unsigned long long) \
1801 pcg->pcg_objects[i].pcgo_pa); \
1802 } else { \
1803 (*pr)("\t\t\t%p\n", \
1804 pcg->pcg_objects[i].pcgo_va); \
1805 } \
1806 }
1807
1808 LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
1809 (*pr)("\tcache %p\n", pc);
1810 (*pr)("\t hits %lu misses %lu ngroups %lu nitems %lu\n",
1811 pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1812 (*pr)("\t full groups:\n");
1813 LIST_FOREACH(pcg, &pc->pc_fullgroups, pcg_list) {
1814 PR_GROUPLIST(pcg);
1815 }
1816 (*pr)("\t partial groups:\n");
1817 LIST_FOREACH(pcg, &pc->pc_partgroups, pcg_list) {
1818 PR_GROUPLIST(pcg);
1819 }
1820 (*pr)("\t empty groups:\n");
1821 LIST_FOREACH(pcg, &pc->pc_emptygroups, pcg_list) {
1822 PR_GROUPLIST(pcg);
1823 }
1824 }
1825 #undef PR_GROUPLIST
1826
1827 skip_cache:
1828 pr_enter_check(pp, pr);
1829 }
1830
1831 static int
1832 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1833 {
1834 struct pool_item *pi;
1835 void *page;
1836 int n;
1837
1838 if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1839 page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1840 if (page != ph->ph_page &&
1841 (pp->pr_roflags & PR_PHINPAGE) != 0) {
1842 if (label != NULL)
1843 printf("%s: ", label);
1844 printf("pool(%p:%s): page inconsistency: page %p;"
1845 " at page head addr %p (p %p)\n", pp,
1846 pp->pr_wchan, ph->ph_page,
1847 ph, page);
1848 return 1;
1849 }
1850 }
1851
1852 if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1853 return 0;
1854
1855 for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1856 pi != NULL;
1857 pi = LIST_NEXT(pi,pi_list), n++) {
1858
1859 #ifdef DIAGNOSTIC
1860 if (pi->pi_magic != PI_MAGIC) {
1861 if (label != NULL)
1862 printf("%s: ", label);
1863 printf("pool(%s): free list modified: magic=%x;"
1864 " page %p; item ordinal %d; addr %p\n",
1865 pp->pr_wchan, pi->pi_magic, ph->ph_page,
1866 n, pi);
1867 panic("pool");
1868 }
1869 #endif
1870 if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1871 continue;
1872 }
1873 page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1874 if (page == ph->ph_page)
1875 continue;
1876
1877 if (label != NULL)
1878 printf("%s: ", label);
1879 printf("pool(%p:%s): page inconsistency: page %p;"
1880 " item ordinal %d; addr %p (p %p)\n", pp,
1881 pp->pr_wchan, ph->ph_page,
1882 n, pi, page);
1883 return 1;
1884 }
1885 return 0;
1886 }
1887
1888
1889 int
1890 pool_chk(struct pool *pp, const char *label)
1891 {
1892 struct pool_item_header *ph;
1893 int r = 0;
1894
1895 mutex_enter(&pp->pr_lock);
1896 LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1897 r = pool_chk_page(pp, label, ph);
1898 if (r) {
1899 goto out;
1900 }
1901 }
1902 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1903 r = pool_chk_page(pp, label, ph);
1904 if (r) {
1905 goto out;
1906 }
1907 }
1908 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1909 r = pool_chk_page(pp, label, ph);
1910 if (r) {
1911 goto out;
1912 }
1913 }
1914
1915 out:
1916 mutex_exit(&pp->pr_lock);
1917 return (r);
1918 }
1919
1920 /*
1921 * pool_cache_init:
1922 *
1923 * Initialize a pool cache.
1924 *
1925 * NOTE: If the pool must be protected from interrupts, we expect
1926 * to be called at the appropriate interrupt priority level.
1927 */
1928 void
1929 pool_cache_init(struct pool_cache *pc, struct pool *pp,
1930 int (*ctor)(void *, void *, int),
1931 void (*dtor)(void *, void *),
1932 void *arg)
1933 {
1934
1935 LIST_INIT(&pc->pc_emptygroups);
1936 LIST_INIT(&pc->pc_fullgroups);
1937 LIST_INIT(&pc->pc_partgroups);
1938 mutex_init(&pc->pc_lock, MUTEX_DRIVER, pp->pr_ipl);
1939
1940 pc->pc_pool = pp;
1941
1942 pc->pc_ctor = ctor;
1943 pc->pc_dtor = dtor;
1944 pc->pc_arg = arg;
1945
1946 pc->pc_hits = 0;
1947 pc->pc_misses = 0;
1948
1949 pc->pc_ngroups = 0;
1950
1951 pc->pc_nitems = 0;
1952
1953 if (__predict_true(!cold)) {
1954 mutex_enter(&pp->pr_lock);
1955 LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1956 mutex_exit(&pp->pr_lock);
1957 } else
1958 LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1959 }
1960
1961 /*
1962 * pool_cache_destroy:
1963 *
1964 * Destroy a pool cache.
1965 */
1966 void
1967 pool_cache_destroy(struct pool_cache *pc)
1968 {
1969 struct pool *pp = pc->pc_pool;
1970
1971 /* First, invalidate the entire cache. */
1972 pool_cache_invalidate(pc);
1973
1974 /* ...and remove it from the pool's cache list. */
1975 mutex_enter(&pp->pr_lock);
1976 LIST_REMOVE(pc, pc_poollist);
1977 mutex_exit(&pp->pr_lock);
1978
1979 mutex_destroy(&pc->pc_lock);
1980 }
1981
1982 static inline void *
1983 pcg_get(struct pool_cache_group *pcg, paddr_t *pap)
1984 {
1985 void *object;
1986 u_int idx;
1987
1988 KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1989 KASSERT(pcg->pcg_avail != 0);
1990 idx = --pcg->pcg_avail;
1991
1992 KASSERT(pcg->pcg_objects[idx].pcgo_va != NULL);
1993 object = pcg->pcg_objects[idx].pcgo_va;
1994 if (pap != NULL)
1995 *pap = pcg->pcg_objects[idx].pcgo_pa;
1996 pcg->pcg_objects[idx].pcgo_va = NULL;
1997
1998 return (object);
1999 }
2000
2001 static inline void
2002 pcg_put(struct pool_cache_group *pcg, void *object, paddr_t pa)
2003 {
2004 u_int idx;
2005
2006 KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
2007 idx = pcg->pcg_avail++;
2008
2009 KASSERT(pcg->pcg_objects[idx].pcgo_va == NULL);
2010 pcg->pcg_objects[idx].pcgo_va = object;
2011 pcg->pcg_objects[idx].pcgo_pa = pa;
2012 }
2013
2014 static void
2015 pcg_grouplist_free(struct pool_cache_grouplist *pcgl)
2016 {
2017 struct pool_cache_group *pcg;
2018
2019 while ((pcg = LIST_FIRST(pcgl)) != NULL) {
2020 LIST_REMOVE(pcg, pcg_list);
2021 pool_put(&pcgpool, pcg);
2022 }
2023 }
2024
2025 /*
2026 * pool_cache_get{,_paddr}:
2027 *
2028 * Get an object from a pool cache (optionally returning
2029 * the physical address of the object).
2030 */
2031 void *
2032 pool_cache_get_paddr(struct pool_cache *pc, int flags, paddr_t *pap)
2033 {
2034 struct pool_cache_group *pcg;
2035 void *object;
2036
2037 #ifdef LOCKDEBUG
2038 if (flags & PR_WAITOK)
2039 ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)");
2040 #endif
2041
2042 mutex_enter(&pc->pc_lock);
2043
2044 pcg = LIST_FIRST(&pc->pc_partgroups);
2045 if (pcg == NULL) {
2046 pcg = LIST_FIRST(&pc->pc_fullgroups);
2047 if (pcg != NULL) {
2048 LIST_REMOVE(pcg, pcg_list);
2049 LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
2050 }
2051 }
2052 if (pcg == NULL) {
2053
2054 /*
2055 * No groups with any available objects. Allocate
2056 * a new object, construct it, and return it to
2057 * the caller. We will allocate a group, if necessary,
2058 * when the object is freed back to the cache.
2059 */
2060 pc->pc_misses++;
2061 mutex_exit(&pc->pc_lock);
2062 object = pool_get(pc->pc_pool, flags);
2063 if (object != NULL && pc->pc_ctor != NULL) {
2064 if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
2065 pool_put(pc->pc_pool, object);
2066 return (NULL);
2067 }
2068 }
2069 KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
2070 (pc->pc_pool->pr_align - 1)) == 0);
2071 if (object != NULL && pap != NULL) {
2072 #ifdef POOL_VTOPHYS
2073 *pap = POOL_VTOPHYS(object);
2074 #else
2075 *pap = POOL_PADDR_INVALID;
2076 #endif
2077 }
2078
2079 FREECHECK_OUT(&pc->pc_freecheck, object);
2080 return (object);
2081 }
2082
2083 pc->pc_hits++;
2084 pc->pc_nitems--;
2085 object = pcg_get(pcg, pap);
2086
2087 if (pcg->pcg_avail == 0) {
2088 LIST_REMOVE(pcg, pcg_list);
2089 LIST_INSERT_HEAD(&pc->pc_emptygroups, pcg, pcg_list);
2090 }
2091 mutex_exit(&pc->pc_lock);
2092
2093 KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
2094 (pc->pc_pool->pr_align - 1)) == 0);
2095 FREECHECK_OUT(&pc->pc_freecheck, object);
2096 return (object);
2097 }
2098
2099 /*
2100 * pool_cache_put{,_paddr}:
2101 *
2102 * Put an object back to the pool cache (optionally caching the
2103 * physical address of the object).
2104 */
2105 void
2106 pool_cache_put_paddr(struct pool_cache *pc, void *object, paddr_t pa)
2107 {
2108 struct pool_cache_group *pcg;
2109
2110 FREECHECK_IN(&pc->pc_freecheck, object);
2111
2112 if (__predict_false((pc->pc_pool->pr_flags & PR_WANTED) != 0)) {
2113 goto destruct;
2114 }
2115
2116 mutex_enter(&pc->pc_lock);
2117
2118 pcg = LIST_FIRST(&pc->pc_partgroups);
2119 if (pcg == NULL) {
2120 pcg = LIST_FIRST(&pc->pc_emptygroups);
2121 if (pcg != NULL) {
2122 LIST_REMOVE(pcg, pcg_list);
2123 LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
2124 }
2125 }
2126 if (pcg == NULL) {
2127
2128 /*
2129 * No empty groups to free the object to. Attempt to
2130 * allocate one.
2131 */
2132 mutex_exit(&pc->pc_lock);
2133 pcg = pool_get(&pcgpool, PR_NOWAIT);
2134 if (pcg == NULL) {
2135 destruct:
2136
2137 /*
2138 * Unable to allocate a cache group; destruct the object
2139 * and free it back to the pool.
2140 */
2141 pool_cache_destruct_object(pc, object);
2142 return;
2143 }
2144 memset(pcg, 0, sizeof(*pcg));
2145 mutex_enter(&pc->pc_lock);
2146 pc->pc_ngroups++;
2147 LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
2148 }
2149
2150 pc->pc_nitems++;
2151 pcg_put(pcg, object, pa);
2152
2153 if (pcg->pcg_avail == PCG_NOBJECTS) {
2154 LIST_REMOVE(pcg, pcg_list);
2155 LIST_INSERT_HEAD(&pc->pc_fullgroups, pcg, pcg_list);
2156 }
2157 mutex_exit(&pc->pc_lock);
2158 }
2159
2160 /*
2161 * pool_cache_destruct_object:
2162 *
2163 * Force destruction of an object and its release back into
2164 * the pool.
2165 */
2166 void
2167 pool_cache_destruct_object(struct pool_cache *pc, void *object)
2168 {
2169
2170 if (pc->pc_dtor != NULL)
2171 (*pc->pc_dtor)(pc->pc_arg, object);
2172 pool_put(pc->pc_pool, object);
2173 }
2174
2175 /*
2176 * pool_do_cache_invalidate_grouplist:
2177 *
2178 * Invalidate a single grouplist and destruct all objects.
2179 * XXX This is too expensive. We should swap the list then
2180 * unlock.
2181 */
2182 static void
2183 pool_do_cache_invalidate_grouplist(struct pool_cache_grouplist *pcgsl,
2184 struct pool_cache *pc, struct pool_pagelist *pq,
2185 struct pool_cache_grouplist *pcgdl)
2186 {
2187 struct pool_cache_group *pcg;
2188 void *object;
2189
2190 KASSERT(mutex_owned(&pc->pc_lock));
2191 KASSERT(mutex_owned(&pc->pc_pool->pr_lock));
2192
2193 while ((pcg = LIST_FIRST(pcgsl)) != NULL) {
2194 pc->pc_ngroups--;
2195 LIST_REMOVE(pcg, pcg_list);
2196 LIST_INSERT_HEAD(pcgdl, pcg, pcg_list);
2197 pc->pc_nitems -= pcg->pcg_avail;
2198 mutex_exit(&pc->pc_pool->pr_lock);
2199 mutex_exit(&pc->pc_lock);
2200
2201 while (pcg->pcg_avail != 0) {
2202 object = pcg_get(pcg, NULL);
2203 if (pc->pc_dtor != NULL)
2204 (*pc->pc_dtor)(pc->pc_arg, object);
2205 mutex_enter(&pc->pc_pool->pr_lock);
2206 pool_do_put(pc->pc_pool, object, pq);
2207 mutex_exit(&pc->pc_pool->pr_lock);
2208 }
2209
2210 mutex_enter(&pc->pc_lock);
2211 mutex_enter(&pc->pc_pool->pr_lock);
2212 }
2213 }
2214
2215 static void
2216 pool_do_cache_invalidate(struct pool_cache *pc, struct pool_pagelist *pq,
2217 struct pool_cache_grouplist *pcgl)
2218 {
2219
2220 KASSERT(mutex_owned(&pc->pc_lock));
2221 KASSERT(mutex_owned(&pc->pc_pool->pr_lock));
2222
2223 pool_do_cache_invalidate_grouplist(&pc->pc_fullgroups, pc, pq, pcgl);
2224 pool_do_cache_invalidate_grouplist(&pc->pc_partgroups, pc, pq, pcgl);
2225
2226 KASSERT(LIST_EMPTY(&pc->pc_partgroups));
2227 KASSERT(LIST_EMPTY(&pc->pc_fullgroups));
2228 KASSERT(pc->pc_nitems == 0);
2229 }
2230
2231 /*
2232 * pool_cache_invalidate:
2233 *
2234 * Invalidate a pool cache (destruct and release all of the
2235 * cached objects).
2236 */
2237 void
2238 pool_cache_invalidate(struct pool_cache *pc)
2239 {
2240 struct pool_pagelist pq;
2241 struct pool_cache_grouplist pcgl;
2242
2243 LIST_INIT(&pq);
2244 LIST_INIT(&pcgl);
2245
2246 mutex_enter(&pc->pc_lock);
2247 mutex_enter(&pc->pc_pool->pr_lock);
2248
2249 pool_do_cache_invalidate(pc, &pq, &pcgl);
2250
2251 mutex_exit(&pc->pc_pool->pr_lock);
2252 mutex_exit(&pc->pc_lock);
2253
2254 pr_pagelist_free(pc->pc_pool, &pq);
2255 pcg_grouplist_free(&pcgl);
2256 }
2257
2258 /*
2259 * pool_cache_reclaim:
2260 *
2261 * Reclaim a pool cache for pool_reclaim().
2262 */
2263 static void
2264 pool_cache_reclaim(struct pool_cache *pc, struct pool_pagelist *pq,
2265 struct pool_cache_grouplist *pcgl)
2266 {
2267
2268 /*
2269 * We're locking in the wrong order (normally pool_cache -> pool,
2270 * but the pool is already locked when we get here), so we have
2271 * to use trylock. If we can't lock the pool_cache, it's not really
2272 * a big deal here.
2273 */
2274 if (mutex_tryenter(&pc->pc_lock) == 0)
2275 return;
2276
2277 pool_do_cache_invalidate(pc, pq, pcgl);
2278
2279 mutex_exit(&pc->pc_lock);
2280 }
2281
2282 /*
2283 * Pool backend allocators.
2284 *
2285 * Each pool has a backend allocator that handles allocation, deallocation,
2286 * and any additional draining that might be needed.
2287 *
2288 * We provide two standard allocators:
2289 *
2290 * pool_allocator_kmem - the default when no allocator is specified
2291 *
2292 * pool_allocator_nointr - used for pools that will not be accessed
2293 * in interrupt context.
2294 */
2295 void *pool_page_alloc(struct pool *, int);
2296 void pool_page_free(struct pool *, void *);
2297
2298 #ifdef POOL_SUBPAGE
2299 struct pool_allocator pool_allocator_kmem_fullpage = {
2300 pool_page_alloc, pool_page_free, 0,
2301 .pa_backingmapptr = &kmem_map,
2302 };
2303 #else
2304 struct pool_allocator pool_allocator_kmem = {
2305 pool_page_alloc, pool_page_free, 0,
2306 .pa_backingmapptr = &kmem_map,
2307 };
2308 #endif
2309
2310 void *pool_page_alloc_nointr(struct pool *, int);
2311 void pool_page_free_nointr(struct pool *, void *);
2312
2313 #ifdef POOL_SUBPAGE
2314 struct pool_allocator pool_allocator_nointr_fullpage = {
2315 pool_page_alloc_nointr, pool_page_free_nointr, 0,
2316 .pa_backingmapptr = &kernel_map,
2317 };
2318 #else
2319 struct pool_allocator pool_allocator_nointr = {
2320 pool_page_alloc_nointr, pool_page_free_nointr, 0,
2321 .pa_backingmapptr = &kernel_map,
2322 };
2323 #endif
2324
2325 #ifdef POOL_SUBPAGE
2326 void *pool_subpage_alloc(struct pool *, int);
2327 void pool_subpage_free(struct pool *, void *);
2328
2329 struct pool_allocator pool_allocator_kmem = {
2330 pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2331 .pa_backingmapptr = &kmem_map,
2332 };
2333
2334 void *pool_subpage_alloc_nointr(struct pool *, int);
2335 void pool_subpage_free_nointr(struct pool *, void *);
2336
2337 struct pool_allocator pool_allocator_nointr = {
2338 pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2339 .pa_backingmapptr = &kmem_map,
2340 };
2341 #endif /* POOL_SUBPAGE */
2342
2343 static void *
2344 pool_allocator_alloc(struct pool *pp, int flags)
2345 {
2346 struct pool_allocator *pa = pp->pr_alloc;
2347 void *res;
2348
2349 res = (*pa->pa_alloc)(pp, flags);
2350 if (res == NULL && (flags & PR_WAITOK) == 0) {
2351 /*
2352 * We only run the drain hook here if PR_NOWAIT.
2353 * In other cases, the hook will be run in
2354 * pool_reclaim().
2355 */
2356 if (pp->pr_drain_hook != NULL) {
2357 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2358 res = (*pa->pa_alloc)(pp, flags);
2359 }
2360 }
2361 return res;
2362 }
2363
2364 static void
2365 pool_allocator_free(struct pool *pp, void *v)
2366 {
2367 struct pool_allocator *pa = pp->pr_alloc;
2368
2369 (*pa->pa_free)(pp, v);
2370 }
2371
2372 void *
2373 pool_page_alloc(struct pool *pp, int flags)
2374 {
2375 bool waitok = (flags & PR_WAITOK) ? true : false;
2376
2377 return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
2378 }
2379
2380 void
2381 pool_page_free(struct pool *pp, void *v)
2382 {
2383
2384 uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2385 }
2386
2387 static void *
2388 pool_page_alloc_meta(struct pool *pp, int flags)
2389 {
2390 bool waitok = (flags & PR_WAITOK) ? true : false;
2391
2392 return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
2393 }
2394
2395 static void
2396 pool_page_free_meta(struct pool *pp, void *v)
2397 {
2398
2399 uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
2400 }
2401
2402 #ifdef POOL_SUBPAGE
2403 /* Sub-page allocator, for machines with large hardware pages. */
2404 void *
2405 pool_subpage_alloc(struct pool *pp, int flags)
2406 {
2407 return pool_get(&psppool, flags);
2408 }
2409
2410 void
2411 pool_subpage_free(struct pool *pp, void *v)
2412 {
2413 pool_put(&psppool, v);
2414 }
2415
2416 /* We don't provide a real nointr allocator. Maybe later. */
2417 void *
2418 pool_subpage_alloc_nointr(struct pool *pp, int flags)
2419 {
2420
2421 return (pool_subpage_alloc(pp, flags));
2422 }
2423
2424 void
2425 pool_subpage_free_nointr(struct pool *pp, void *v)
2426 {
2427
2428 pool_subpage_free(pp, v);
2429 }
2430 #endif /* POOL_SUBPAGE */
2431 void *
2432 pool_page_alloc_nointr(struct pool *pp, int flags)
2433 {
2434 bool waitok = (flags & PR_WAITOK) ? true : false;
2435
2436 return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
2437 }
2438
2439 void
2440 pool_page_free_nointr(struct pool *pp, void *v)
2441 {
2442
2443 uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
2444 }
2445