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