subr_pool.c revision 1.128.2.5 1 /* $NetBSD: subr_pool.c,v 1.128.2.5 2007/07/29 11:34:47 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.5 2007/07/29 11:34:47 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 void
1620 pool_drain(void *arg)
1621 {
1622 struct pool *pp;
1623 int s;
1624
1625 pp = NULL;
1626 s = splvm(); /* XXX why? */
1627 mutex_enter(&pool_head_lock);
1628 if (drainpp == NULL) {
1629 drainpp = LIST_FIRST(&pool_head);
1630 }
1631 if (drainpp) {
1632 pp = drainpp;
1633 drainpp = LIST_NEXT(pp, pr_poollist);
1634 }
1635 mutex_exit(&pool_head_lock);
1636 if (pp)
1637 pool_reclaim(pp);
1638 splx(s);
1639 }
1640
1641 /*
1642 * Diagnostic helpers.
1643 */
1644 void
1645 pool_print(struct pool *pp, const char *modif)
1646 {
1647
1648 if (mutex_tryenter(&pp->pr_lock) == 0) {
1649 printf("pool %s is locked; try again later\n",
1650 pp->pr_wchan);
1651 return;
1652 }
1653 pool_print1(pp, modif, printf);
1654 mutex_exit(&pp->pr_lock);
1655 }
1656
1657 void
1658 pool_printall(const char *modif, void (*pr)(const char *, ...))
1659 {
1660 struct pool *pp;
1661
1662 if (mutex_tryenter(&pool_head_lock) == 0) {
1663 (*pr)("WARNING: pool_head_slock is locked\n");
1664 } else {
1665 mutex_exit(&pool_head_lock);
1666 }
1667
1668 LIST_FOREACH(pp, &pool_head, pr_poollist) {
1669 pool_printit(pp, modif, pr);
1670 }
1671 }
1672
1673 void
1674 pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1675 {
1676
1677 if (pp == NULL) {
1678 (*pr)("Must specify a pool to print.\n");
1679 return;
1680 }
1681
1682 /*
1683 * Called from DDB; interrupts should be blocked, and all
1684 * other processors should be paused. We can skip locking
1685 * the pool in this case.
1686 *
1687 * We do a mutex_tryenter() just to print the lock
1688 * status, however.
1689 */
1690
1691 if (mutex_tryenter(&pp->pr_lock) == 0)
1692 (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1693 else
1694 mutex_exit(&pp->pr_lock);
1695
1696 pool_print1(pp, modif, pr);
1697 }
1698
1699 static void
1700 pool_print_pagelist(struct pool *pp, struct pool_pagelist *pl,
1701 void (*pr)(const char *, ...))
1702 {
1703 struct pool_item_header *ph;
1704 #ifdef DIAGNOSTIC
1705 struct pool_item *pi;
1706 #endif
1707
1708 LIST_FOREACH(ph, pl, ph_pagelist) {
1709 (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1710 ph->ph_page, ph->ph_nmissing,
1711 (u_long)ph->ph_time.tv_sec,
1712 (u_long)ph->ph_time.tv_usec);
1713 #ifdef DIAGNOSTIC
1714 if (!(pp->pr_roflags & PR_NOTOUCH)) {
1715 LIST_FOREACH(pi, &ph->ph_itemlist, pi_list) {
1716 if (pi->pi_magic != PI_MAGIC) {
1717 (*pr)("\t\t\titem %p, magic 0x%x\n",
1718 pi, pi->pi_magic);
1719 }
1720 }
1721 }
1722 #endif
1723 }
1724 }
1725
1726 static void
1727 pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1728 {
1729 struct pool_item_header *ph;
1730 struct pool_cache *pc;
1731 struct pool_cache_group *pcg;
1732 int i, print_log = 0, print_pagelist = 0, print_cache = 0;
1733 char c;
1734
1735 while ((c = *modif++) != '\0') {
1736 if (c == 'l')
1737 print_log = 1;
1738 if (c == 'p')
1739 print_pagelist = 1;
1740 if (c == 'c')
1741 print_cache = 1;
1742 }
1743
1744 (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1745 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1746 pp->pr_roflags);
1747 (*pr)("\talloc %p\n", pp->pr_alloc);
1748 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1749 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1750 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1751 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1752
1753 (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1754 pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1755 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1756 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1757
1758 if (print_pagelist == 0)
1759 goto skip_pagelist;
1760
1761 if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL)
1762 (*pr)("\n\tempty page list:\n");
1763 pool_print_pagelist(pp, &pp->pr_emptypages, pr);
1764 if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL)
1765 (*pr)("\n\tfull page list:\n");
1766 pool_print_pagelist(pp, &pp->pr_fullpages, pr);
1767 if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL)
1768 (*pr)("\n\tpartial-page list:\n");
1769 pool_print_pagelist(pp, &pp->pr_partpages, pr);
1770
1771 if (pp->pr_curpage == NULL)
1772 (*pr)("\tno current page\n");
1773 else
1774 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1775
1776 skip_pagelist:
1777 if (print_log == 0)
1778 goto skip_log;
1779
1780 (*pr)("\n");
1781 if ((pp->pr_roflags & PR_LOGGING) == 0)
1782 (*pr)("\tno log\n");
1783 else {
1784 pr_printlog(pp, NULL, pr);
1785 }
1786
1787 skip_log:
1788 if (print_cache == 0)
1789 goto skip_cache;
1790
1791 #define PR_GROUPLIST(pcg) \
1792 (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); \
1793 for (i = 0; i < PCG_NOBJECTS; i++) { \
1794 if (pcg->pcg_objects[i].pcgo_pa != \
1795 POOL_PADDR_INVALID) { \
1796 (*pr)("\t\t\t%p, 0x%llx\n", \
1797 pcg->pcg_objects[i].pcgo_va, \
1798 (unsigned long long) \
1799 pcg->pcg_objects[i].pcgo_pa); \
1800 } else { \
1801 (*pr)("\t\t\t%p\n", \
1802 pcg->pcg_objects[i].pcgo_va); \
1803 } \
1804 }
1805
1806 LIST_FOREACH(pc, &pp->pr_cachelist, pc_poollist) {
1807 (*pr)("\tcache %p\n", pc);
1808 (*pr)("\t hits %lu misses %lu ngroups %lu nitems %lu\n",
1809 pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems);
1810 (*pr)("\t full groups:\n");
1811 LIST_FOREACH(pcg, &pc->pc_fullgroups, pcg_list) {
1812 PR_GROUPLIST(pcg);
1813 }
1814 (*pr)("\t partial groups:\n");
1815 LIST_FOREACH(pcg, &pc->pc_partgroups, pcg_list) {
1816 PR_GROUPLIST(pcg);
1817 }
1818 (*pr)("\t empty groups:\n");
1819 LIST_FOREACH(pcg, &pc->pc_emptygroups, pcg_list) {
1820 PR_GROUPLIST(pcg);
1821 }
1822 }
1823 #undef PR_GROUPLIST
1824
1825 skip_cache:
1826 pr_enter_check(pp, pr);
1827 }
1828
1829 static int
1830 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph)
1831 {
1832 struct pool_item *pi;
1833 void *page;
1834 int n;
1835
1836 if ((pp->pr_roflags & PR_NOALIGN) == 0) {
1837 page = (void *)((uintptr_t)ph & pp->pr_alloc->pa_pagemask);
1838 if (page != ph->ph_page &&
1839 (pp->pr_roflags & PR_PHINPAGE) != 0) {
1840 if (label != NULL)
1841 printf("%s: ", label);
1842 printf("pool(%p:%s): page inconsistency: page %p;"
1843 " at page head addr %p (p %p)\n", pp,
1844 pp->pr_wchan, ph->ph_page,
1845 ph, page);
1846 return 1;
1847 }
1848 }
1849
1850 if ((pp->pr_roflags & PR_NOTOUCH) != 0)
1851 return 0;
1852
1853 for (pi = LIST_FIRST(&ph->ph_itemlist), n = 0;
1854 pi != NULL;
1855 pi = LIST_NEXT(pi,pi_list), n++) {
1856
1857 #ifdef DIAGNOSTIC
1858 if (pi->pi_magic != PI_MAGIC) {
1859 if (label != NULL)
1860 printf("%s: ", label);
1861 printf("pool(%s): free list modified: magic=%x;"
1862 " page %p; item ordinal %d; addr %p\n",
1863 pp->pr_wchan, pi->pi_magic, ph->ph_page,
1864 n, pi);
1865 panic("pool");
1866 }
1867 #endif
1868 if ((pp->pr_roflags & PR_NOALIGN) != 0) {
1869 continue;
1870 }
1871 page = (void *)((uintptr_t)pi & pp->pr_alloc->pa_pagemask);
1872 if (page == ph->ph_page)
1873 continue;
1874
1875 if (label != NULL)
1876 printf("%s: ", label);
1877 printf("pool(%p:%s): page inconsistency: page %p;"
1878 " item ordinal %d; addr %p (p %p)\n", pp,
1879 pp->pr_wchan, ph->ph_page,
1880 n, pi, page);
1881 return 1;
1882 }
1883 return 0;
1884 }
1885
1886
1887 int
1888 pool_chk(struct pool *pp, const char *label)
1889 {
1890 struct pool_item_header *ph;
1891 int r = 0;
1892
1893 mutex_enter(&pp->pr_lock);
1894 LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) {
1895 r = pool_chk_page(pp, label, ph);
1896 if (r) {
1897 goto out;
1898 }
1899 }
1900 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) {
1901 r = pool_chk_page(pp, label, ph);
1902 if (r) {
1903 goto out;
1904 }
1905 }
1906 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) {
1907 r = pool_chk_page(pp, label, ph);
1908 if (r) {
1909 goto out;
1910 }
1911 }
1912
1913 out:
1914 mutex_exit(&pp->pr_lock);
1915 return (r);
1916 }
1917
1918 /*
1919 * pool_cache_init:
1920 *
1921 * Initialize a pool cache.
1922 *
1923 * NOTE: If the pool must be protected from interrupts, we expect
1924 * to be called at the appropriate interrupt priority level.
1925 */
1926 void
1927 pool_cache_init(struct pool_cache *pc, struct pool *pp,
1928 int (*ctor)(void *, void *, int),
1929 void (*dtor)(void *, void *),
1930 void *arg)
1931 {
1932
1933 LIST_INIT(&pc->pc_emptygroups);
1934 LIST_INIT(&pc->pc_fullgroups);
1935 LIST_INIT(&pc->pc_partgroups);
1936 mutex_init(&pc->pc_lock, MUTEX_DRIVER, pp->pr_ipl);
1937
1938 pc->pc_pool = pp;
1939
1940 pc->pc_ctor = ctor;
1941 pc->pc_dtor = dtor;
1942 pc->pc_arg = arg;
1943
1944 pc->pc_hits = 0;
1945 pc->pc_misses = 0;
1946
1947 pc->pc_ngroups = 0;
1948
1949 pc->pc_nitems = 0;
1950
1951 if (__predict_true(!cold)) {
1952 mutex_enter(&pp->pr_lock);
1953 LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1954 mutex_exit(&pp->pr_lock);
1955 } else
1956 LIST_INSERT_HEAD(&pp->pr_cachelist, pc, pc_poollist);
1957 }
1958
1959 /*
1960 * pool_cache_destroy:
1961 *
1962 * Destroy a pool cache.
1963 */
1964 void
1965 pool_cache_destroy(struct pool_cache *pc)
1966 {
1967 struct pool *pp = pc->pc_pool;
1968
1969 /* First, invalidate the entire cache. */
1970 pool_cache_invalidate(pc);
1971
1972 /* ...and remove it from the pool's cache list. */
1973 mutex_enter(&pp->pr_lock);
1974 LIST_REMOVE(pc, pc_poollist);
1975 mutex_exit(&pp->pr_lock);
1976
1977 mutex_destroy(&pc->pc_lock);
1978 }
1979
1980 static inline void *
1981 pcg_get(struct pool_cache_group *pcg, paddr_t *pap)
1982 {
1983 void *object;
1984 u_int idx;
1985
1986 KASSERT(pcg->pcg_avail <= PCG_NOBJECTS);
1987 KASSERT(pcg->pcg_avail != 0);
1988 idx = --pcg->pcg_avail;
1989
1990 KASSERT(pcg->pcg_objects[idx].pcgo_va != NULL);
1991 object = pcg->pcg_objects[idx].pcgo_va;
1992 if (pap != NULL)
1993 *pap = pcg->pcg_objects[idx].pcgo_pa;
1994 pcg->pcg_objects[idx].pcgo_va = NULL;
1995
1996 return (object);
1997 }
1998
1999 static inline void
2000 pcg_put(struct pool_cache_group *pcg, void *object, paddr_t pa)
2001 {
2002 u_int idx;
2003
2004 KASSERT(pcg->pcg_avail < PCG_NOBJECTS);
2005 idx = pcg->pcg_avail++;
2006
2007 KASSERT(pcg->pcg_objects[idx].pcgo_va == NULL);
2008 pcg->pcg_objects[idx].pcgo_va = object;
2009 pcg->pcg_objects[idx].pcgo_pa = pa;
2010 }
2011
2012 static void
2013 pcg_grouplist_free(struct pool_cache_grouplist *pcgl)
2014 {
2015 struct pool_cache_group *pcg;
2016
2017 while ((pcg = LIST_FIRST(pcgl)) != NULL) {
2018 LIST_REMOVE(pcg, pcg_list);
2019 pool_put(&pcgpool, pcg);
2020 }
2021 }
2022
2023 /*
2024 * pool_cache_get{,_paddr}:
2025 *
2026 * Get an object from a pool cache (optionally returning
2027 * the physical address of the object).
2028 */
2029 void *
2030 pool_cache_get_paddr(struct pool_cache *pc, int flags, paddr_t *pap)
2031 {
2032 struct pool_cache_group *pcg;
2033 void *object;
2034
2035 #ifdef LOCKDEBUG
2036 if (flags & PR_WAITOK)
2037 ASSERT_SLEEPABLE(NULL, "pool_cache_get(PR_WAITOK)");
2038 #endif
2039
2040 mutex_enter(&pc->pc_lock);
2041
2042 pcg = LIST_FIRST(&pc->pc_partgroups);
2043 if (pcg == NULL) {
2044 pcg = LIST_FIRST(&pc->pc_fullgroups);
2045 if (pcg != NULL) {
2046 LIST_REMOVE(pcg, pcg_list);
2047 LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
2048 }
2049 }
2050 if (pcg == NULL) {
2051
2052 /*
2053 * No groups with any available objects. Allocate
2054 * a new object, construct it, and return it to
2055 * the caller. We will allocate a group, if necessary,
2056 * when the object is freed back to the cache.
2057 */
2058 pc->pc_misses++;
2059 mutex_exit(&pc->pc_lock);
2060 object = pool_get(pc->pc_pool, flags);
2061 if (object != NULL && pc->pc_ctor != NULL) {
2062 if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) {
2063 pool_put(pc->pc_pool, object);
2064 return (NULL);
2065 }
2066 }
2067 KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
2068 (pc->pc_pool->pr_align - 1)) == 0);
2069 if (object != NULL && pap != NULL) {
2070 #ifdef POOL_VTOPHYS
2071 *pap = POOL_VTOPHYS(object);
2072 #else
2073 *pap = POOL_PADDR_INVALID;
2074 #endif
2075 }
2076
2077 FREECHECK_OUT(&pc->pc_freecheck, object);
2078 return (object);
2079 }
2080
2081 pc->pc_hits++;
2082 pc->pc_nitems--;
2083 object = pcg_get(pcg, pap);
2084
2085 if (pcg->pcg_avail == 0) {
2086 LIST_REMOVE(pcg, pcg_list);
2087 LIST_INSERT_HEAD(&pc->pc_emptygroups, pcg, pcg_list);
2088 }
2089 mutex_exit(&pc->pc_lock);
2090
2091 KASSERT((((vaddr_t)object + pc->pc_pool->pr_itemoffset) &
2092 (pc->pc_pool->pr_align - 1)) == 0);
2093 FREECHECK_OUT(&pc->pc_freecheck, object);
2094 return (object);
2095 }
2096
2097 /*
2098 * pool_cache_put{,_paddr}:
2099 *
2100 * Put an object back to the pool cache (optionally caching the
2101 * physical address of the object).
2102 */
2103 void
2104 pool_cache_put_paddr(struct pool_cache *pc, void *object, paddr_t pa)
2105 {
2106 struct pool_cache_group *pcg;
2107
2108 FREECHECK_IN(&pc->pc_freecheck, object);
2109
2110 if (__predict_false((pc->pc_pool->pr_flags & PR_WANTED) != 0)) {
2111 goto destruct;
2112 }
2113
2114 mutex_enter(&pc->pc_lock);
2115
2116 pcg = LIST_FIRST(&pc->pc_partgroups);
2117 if (pcg == NULL) {
2118 pcg = LIST_FIRST(&pc->pc_emptygroups);
2119 if (pcg != NULL) {
2120 LIST_REMOVE(pcg, pcg_list);
2121 LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
2122 }
2123 }
2124 if (pcg == NULL) {
2125
2126 /*
2127 * No empty groups to free the object to. Attempt to
2128 * allocate one.
2129 */
2130 mutex_exit(&pc->pc_lock);
2131 pcg = pool_get(&pcgpool, PR_NOWAIT);
2132 if (pcg == NULL) {
2133 destruct:
2134
2135 /*
2136 * Unable to allocate a cache group; destruct the object
2137 * and free it back to the pool.
2138 */
2139 pool_cache_destruct_object(pc, object);
2140 return;
2141 }
2142 memset(pcg, 0, sizeof(*pcg));
2143 mutex_enter(&pc->pc_lock);
2144 pc->pc_ngroups++;
2145 LIST_INSERT_HEAD(&pc->pc_partgroups, pcg, pcg_list);
2146 }
2147
2148 pc->pc_nitems++;
2149 pcg_put(pcg, object, pa);
2150
2151 if (pcg->pcg_avail == PCG_NOBJECTS) {
2152 LIST_REMOVE(pcg, pcg_list);
2153 LIST_INSERT_HEAD(&pc->pc_fullgroups, pcg, pcg_list);
2154 }
2155 mutex_exit(&pc->pc_lock);
2156 }
2157
2158 /*
2159 * pool_cache_destruct_object:
2160 *
2161 * Force destruction of an object and its release back into
2162 * the pool.
2163 */
2164 void
2165 pool_cache_destruct_object(struct pool_cache *pc, void *object)
2166 {
2167
2168 if (pc->pc_dtor != NULL)
2169 (*pc->pc_dtor)(pc->pc_arg, object);
2170 pool_put(pc->pc_pool, object);
2171 }
2172
2173 static void
2174 pool_do_cache_invalidate_grouplist(struct pool_cache_grouplist *pcgsl,
2175 struct pool_cache *pc, struct pool_pagelist *pq,
2176 struct pool_cache_grouplist *pcgdl)
2177 {
2178 struct pool_cache_group *pcg, *npcg;
2179 void *object;
2180
2181 for (pcg = LIST_FIRST(pcgsl); pcg != NULL; pcg = npcg) {
2182 npcg = LIST_NEXT(pcg, pcg_list);
2183 while (pcg->pcg_avail != 0) {
2184 pc->pc_nitems--;
2185 object = pcg_get(pcg, NULL);
2186 if (pc->pc_dtor != NULL)
2187 (*pc->pc_dtor)(pc->pc_arg, object);
2188 pool_do_put(pc->pc_pool, object, pq);
2189 }
2190 pc->pc_ngroups--;
2191 LIST_REMOVE(pcg, pcg_list);
2192 LIST_INSERT_HEAD(pcgdl, pcg, pcg_list);
2193 }
2194 }
2195
2196 static void
2197 pool_do_cache_invalidate(struct pool_cache *pc, struct pool_pagelist *pq,
2198 struct pool_cache_grouplist *pcgl)
2199 {
2200
2201 KASSERT(mutex_owned(&pc->pc_lock));
2202 KASSERT(mutex_owned(&pc->pc_pool->pr_lock));
2203
2204 pool_do_cache_invalidate_grouplist(&pc->pc_fullgroups, pc, pq, pcgl);
2205 pool_do_cache_invalidate_grouplist(&pc->pc_partgroups, pc, pq, pcgl);
2206
2207 KASSERT(LIST_EMPTY(&pc->pc_partgroups));
2208 KASSERT(LIST_EMPTY(&pc->pc_fullgroups));
2209 KASSERT(pc->pc_nitems == 0);
2210 }
2211
2212 /*
2213 * pool_cache_invalidate:
2214 *
2215 * Invalidate a pool cache (destruct and release all of the
2216 * cached objects).
2217 */
2218 void
2219 pool_cache_invalidate(struct pool_cache *pc)
2220 {
2221 struct pool_pagelist pq;
2222 struct pool_cache_grouplist pcgl;
2223
2224 LIST_INIT(&pq);
2225 LIST_INIT(&pcgl);
2226
2227 mutex_enter(&pc->pc_lock);
2228 mutex_enter(&pc->pc_pool->pr_lock);
2229
2230 pool_do_cache_invalidate(pc, &pq, &pcgl);
2231
2232 mutex_exit(&pc->pc_pool->pr_lock);
2233 mutex_exit(&pc->pc_lock);
2234
2235 pr_pagelist_free(pc->pc_pool, &pq);
2236 pcg_grouplist_free(&pcgl);
2237 }
2238
2239 /*
2240 * pool_cache_reclaim:
2241 *
2242 * Reclaim a pool cache for pool_reclaim().
2243 */
2244 static void
2245 pool_cache_reclaim(struct pool_cache *pc, struct pool_pagelist *pq,
2246 struct pool_cache_grouplist *pcgl)
2247 {
2248
2249 /*
2250 * We're locking in the wrong order (normally pool_cache -> pool,
2251 * but the pool is already locked when we get here), so we have
2252 * to use trylock. If we can't lock the pool_cache, it's not really
2253 * a big deal here.
2254 */
2255 if (mutex_tryenter(&pc->pc_lock) == 0)
2256 return;
2257
2258 pool_do_cache_invalidate(pc, pq, pcgl);
2259
2260 mutex_exit(&pc->pc_lock);
2261 }
2262
2263 /*
2264 * Pool backend allocators.
2265 *
2266 * Each pool has a backend allocator that handles allocation, deallocation,
2267 * and any additional draining that might be needed.
2268 *
2269 * We provide two standard allocators:
2270 *
2271 * pool_allocator_kmem - the default when no allocator is specified
2272 *
2273 * pool_allocator_nointr - used for pools that will not be accessed
2274 * in interrupt context.
2275 */
2276 void *pool_page_alloc(struct pool *, int);
2277 void pool_page_free(struct pool *, void *);
2278
2279 #ifdef POOL_SUBPAGE
2280 struct pool_allocator pool_allocator_kmem_fullpage = {
2281 pool_page_alloc, pool_page_free, 0,
2282 .pa_backingmapptr = &kmem_map,
2283 };
2284 #else
2285 struct pool_allocator pool_allocator_kmem = {
2286 pool_page_alloc, pool_page_free, 0,
2287 .pa_backingmapptr = &kmem_map,
2288 };
2289 #endif
2290
2291 void *pool_page_alloc_nointr(struct pool *, int);
2292 void pool_page_free_nointr(struct pool *, void *);
2293
2294 #ifdef POOL_SUBPAGE
2295 struct pool_allocator pool_allocator_nointr_fullpage = {
2296 pool_page_alloc_nointr, pool_page_free_nointr, 0,
2297 .pa_backingmapptr = &kernel_map,
2298 };
2299 #else
2300 struct pool_allocator pool_allocator_nointr = {
2301 pool_page_alloc_nointr, pool_page_free_nointr, 0,
2302 .pa_backingmapptr = &kernel_map,
2303 };
2304 #endif
2305
2306 #ifdef POOL_SUBPAGE
2307 void *pool_subpage_alloc(struct pool *, int);
2308 void pool_subpage_free(struct pool *, void *);
2309
2310 struct pool_allocator pool_allocator_kmem = {
2311 pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2312 .pa_backingmapptr = &kmem_map,
2313 };
2314
2315 void *pool_subpage_alloc_nointr(struct pool *, int);
2316 void pool_subpage_free_nointr(struct pool *, void *);
2317
2318 struct pool_allocator pool_allocator_nointr = {
2319 pool_subpage_alloc, pool_subpage_free, POOL_SUBPAGE,
2320 .pa_backingmapptr = &kmem_map,
2321 };
2322 #endif /* POOL_SUBPAGE */
2323
2324 static void *
2325 pool_allocator_alloc(struct pool *pp, int flags)
2326 {
2327 struct pool_allocator *pa = pp->pr_alloc;
2328 void *res;
2329
2330 res = (*pa->pa_alloc)(pp, flags);
2331 if (res == NULL && (flags & PR_WAITOK) == 0) {
2332 /*
2333 * We only run the drain hook here if PR_NOWAIT.
2334 * In other cases, the hook will be run in
2335 * pool_reclaim().
2336 */
2337 if (pp->pr_drain_hook != NULL) {
2338 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags);
2339 res = (*pa->pa_alloc)(pp, flags);
2340 }
2341 }
2342 return res;
2343 }
2344
2345 static void
2346 pool_allocator_free(struct pool *pp, void *v)
2347 {
2348 struct pool_allocator *pa = pp->pr_alloc;
2349
2350 (*pa->pa_free)(pp, v);
2351 }
2352
2353 void *
2354 pool_page_alloc(struct pool *pp, int flags)
2355 {
2356 bool waitok = (flags & PR_WAITOK) ? true : false;
2357
2358 return ((void *) uvm_km_alloc_poolpage_cache(kmem_map, waitok));
2359 }
2360
2361 void
2362 pool_page_free(struct pool *pp, void *v)
2363 {
2364
2365 uvm_km_free_poolpage_cache(kmem_map, (vaddr_t) v);
2366 }
2367
2368 static void *
2369 pool_page_alloc_meta(struct pool *pp, int flags)
2370 {
2371 bool waitok = (flags & PR_WAITOK) ? true : false;
2372
2373 return ((void *) uvm_km_alloc_poolpage(kmem_map, waitok));
2374 }
2375
2376 static void
2377 pool_page_free_meta(struct pool *pp, void *v)
2378 {
2379
2380 uvm_km_free_poolpage(kmem_map, (vaddr_t) v);
2381 }
2382
2383 #ifdef POOL_SUBPAGE
2384 /* Sub-page allocator, for machines with large hardware pages. */
2385 void *
2386 pool_subpage_alloc(struct pool *pp, int flags)
2387 {
2388 return pool_get(&psppool, flags);
2389 }
2390
2391 void
2392 pool_subpage_free(struct pool *pp, void *v)
2393 {
2394 pool_put(&psppool, v);
2395 }
2396
2397 /* We don't provide a real nointr allocator. Maybe later. */
2398 void *
2399 pool_subpage_alloc_nointr(struct pool *pp, int flags)
2400 {
2401
2402 return (pool_subpage_alloc(pp, flags));
2403 }
2404
2405 void
2406 pool_subpage_free_nointr(struct pool *pp, void *v)
2407 {
2408
2409 pool_subpage_free(pp, v);
2410 }
2411 #endif /* POOL_SUBPAGE */
2412 void *
2413 pool_page_alloc_nointr(struct pool *pp, int flags)
2414 {
2415 bool waitok = (flags & PR_WAITOK) ? true : false;
2416
2417 return ((void *) uvm_km_alloc_poolpage_cache(kernel_map, waitok));
2418 }
2419
2420 void
2421 pool_page_free_nointr(struct pool *pp, void *v)
2422 {
2423
2424 uvm_km_free_poolpage_cache(kernel_map, (vaddr_t) v);
2425 }
2426