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