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