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