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