1 /* $OpenBSD: subr_pool.c,v 1.45 2004/07/29 09:18:17 mickey Exp $ */ 2 /* $NetBSD: subr_pool.c,v 1.61 2001/09/26 07:14:56 chs Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1999, 2000 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. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/proc.h> 44 #include <sys/errno.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/lock.h> 48 #include <sys/pool.h> 49 #include <sys/syslog.h> 50 #include <sys/sysctl.h> 51 52 #include <uvm/uvm.h> 53 54 /* 55 * XXX - for now. 56 */ 57 #ifdef LOCKDEBUG 58 #define simple_lock_freecheck(a, s) do { /* nothing */ } while (0) 59 #define simple_lock_only_held(lkp, str) do { /* nothing */ } while (0) 60 #endif 61 62 /* 63 * Pool resource management utility. 64 * 65 * Memory is allocated in pages which are split into pieces according to 66 * the pool item size. Each page is kept on one of three lists in the 67 * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages', 68 * for empty, full and partially-full pages respectively. The individual 69 * pool items are on a linked list headed by `ph_itemlist' in each page 70 * header. The memory for building the page list is either taken from 71 * the allocated pages themselves (for small pool items) or taken from 72 * an internal pool of page headers (`phpool'). 73 */ 74 75 /* List of all pools */ 76 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head); 77 78 /* Private pool for page header structures */ 79 static struct pool phpool; 80 81 /* # of seconds to retain page after last use */ 82 int pool_inactive_time = 10; 83 84 /* Next candidate for drainage (see pool_drain()) */ 85 static struct pool *drainpp; 86 87 /* This spin lock protects both pool_head and drainpp. */ 88 struct simplelock pool_head_slock; 89 90 struct pool_item_header { 91 /* Page headers */ 92 LIST_ENTRY(pool_item_header) 93 ph_pagelist; /* pool page list */ 94 TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */ 95 SPLAY_ENTRY(pool_item_header) 96 ph_node; /* Off-page page headers */ 97 int ph_nmissing; /* # of chunks in use */ 98 caddr_t ph_page; /* this page's address */ 99 struct timeval ph_time; /* last referenced */ 100 }; 101 102 struct pool_item { 103 #ifdef DIAGNOSTIC 104 int pi_magic; 105 #endif 106 #define PI_MAGIC 0xdeafbeef 107 /* Other entries use only this list entry */ 108 TAILQ_ENTRY(pool_item) pi_list; 109 }; 110 111 #define POOL_NEEDS_CATCHUP(pp) \ 112 ((pp)->pr_nitems < (pp)->pr_minitems) 113 114 /* 115 * Every pool get a unique serial number assigned to it. If this counter 116 * wraps, we're screwed, but we shouldn't create so many pools anyway. 117 */ 118 unsigned int pool_serial; 119 120 /* 121 * Pool cache management. 122 * 123 * Pool caches provide a way for constructed objects to be cached by the 124 * pool subsystem. This can lead to performance improvements by avoiding 125 * needless object construction/destruction; it is deferred until absolutely 126 * necessary. 127 * 128 * Caches are grouped into cache groups. Each cache group references 129 * up to 16 constructed objects. When a cache allocates an object 130 * from the pool, it calls the object's constructor and places it into 131 * a cache group. When a cache group frees an object back to the pool, 132 * it first calls the object's destructor. This allows the object to 133 * persist in constructed form while freed to the cache. 134 * 135 * Multiple caches may exist for each pool. This allows a single 136 * object type to have multiple constructed forms. The pool references 137 * each cache, so that when a pool is drained by the pagedaemon, it can 138 * drain each individual cache as well. Each time a cache is drained, 139 * the most idle cache group is freed to the pool in its entirety. 140 * 141 * Pool caches are layed on top of pools. By layering them, we can avoid 142 * the complexity of cache management for pools which would not benefit 143 * from it. 144 */ 145 146 /* The cache group pool. */ 147 static struct pool pcgpool; 148 149 /* The pool cache group. */ 150 #define PCG_NOBJECTS 16 151 struct pool_cache_group { 152 TAILQ_ENTRY(pool_cache_group) 153 pcg_list; /* link in the pool cache's group list */ 154 u_int pcg_avail; /* # available objects */ 155 /* pointers to the objects */ 156 void *pcg_objects[PCG_NOBJECTS]; 157 }; 158 159 void pool_cache_reclaim(struct pool_cache *); 160 void pool_cache_do_invalidate(struct pool_cache *, int, 161 void (*)(struct pool *, void *)); 162 163 int pool_catchup(struct pool *); 164 void pool_prime_page(struct pool *, caddr_t, struct pool_item_header *); 165 void pool_update_curpage(struct pool *); 166 void pool_do_put(struct pool *, void *); 167 void pr_rmpage(struct pool *, struct pool_item_header *, 168 struct pool_pagelist *); 169 int pool_chk_page(struct pool *, const char *, struct pool_item_header *); 170 171 void *pool_allocator_alloc(struct pool *, int); 172 void pool_allocator_free(struct pool *, void *); 173 174 #ifdef DDB 175 void pool_print_pagelist(struct pool_pagelist *, int (*)(const char *, ...)); 176 void pool_print1(struct pool *, const char *, int (*)(const char *, ...)); 177 #endif 178 179 180 /* 181 * Pool log entry. An array of these is allocated in pool_init(). 182 */ 183 struct pool_log { 184 const char *pl_file; 185 long pl_line; 186 int pl_action; 187 #define PRLOG_GET 1 188 #define PRLOG_PUT 2 189 void *pl_addr; 190 }; 191 192 /* Number of entries in pool log buffers */ 193 #ifndef POOL_LOGSIZE 194 #define POOL_LOGSIZE 10 195 #endif 196 197 int pool_logsize = POOL_LOGSIZE; 198 199 #ifdef POOL_DIAGNOSTIC 200 static __inline void 201 pr_log(struct pool *pp, void *v, int action, const char *file, long line) 202 { 203 int n = pp->pr_curlogentry; 204 struct pool_log *pl; 205 206 if ((pp->pr_roflags & PR_LOGGING) == 0) 207 return; 208 209 /* 210 * Fill in the current entry. Wrap around and overwrite 211 * the oldest entry if necessary. 212 */ 213 pl = &pp->pr_log[n]; 214 pl->pl_file = file; 215 pl->pl_line = line; 216 pl->pl_action = action; 217 pl->pl_addr = v; 218 if (++n >= pp->pr_logsize) 219 n = 0; 220 pp->pr_curlogentry = n; 221 } 222 223 static void 224 pr_printlog(struct pool *pp, struct pool_item *pi, 225 int (*pr)(const char *, ...)) 226 { 227 int i = pp->pr_logsize; 228 int n = pp->pr_curlogentry; 229 230 if ((pp->pr_roflags & PR_LOGGING) == 0) 231 return; 232 233 /* 234 * Print all entries in this pool's log. 235 */ 236 while (i-- > 0) { 237 struct pool_log *pl = &pp->pr_log[n]; 238 if (pl->pl_action != 0) { 239 if (pi == NULL || pi == pl->pl_addr) { 240 (*pr)("\tlog entry %d:\n", i); 241 (*pr)("\t\taction = %s, addr = %p\n", 242 pl->pl_action == PRLOG_GET ? "get" : "put", 243 pl->pl_addr); 244 (*pr)("\t\tfile: %s at line %lu\n", 245 pl->pl_file, pl->pl_line); 246 } 247 } 248 if (++n >= pp->pr_logsize) 249 n = 0; 250 } 251 } 252 253 static __inline void 254 pr_enter(struct pool *pp, const char *file, long line) 255 { 256 257 if (__predict_false(pp->pr_entered_file != NULL)) { 258 printf("pool %s: reentrancy at file %s line %ld\n", 259 pp->pr_wchan, file, line); 260 printf(" previous entry at file %s line %ld\n", 261 pp->pr_entered_file, pp->pr_entered_line); 262 panic("pr_enter"); 263 } 264 265 pp->pr_entered_file = file; 266 pp->pr_entered_line = line; 267 } 268 269 static __inline void 270 pr_leave(struct pool *pp) 271 { 272 273 if (__predict_false(pp->pr_entered_file == NULL)) { 274 printf("pool %s not entered?\n", pp->pr_wchan); 275 panic("pr_leave"); 276 } 277 278 pp->pr_entered_file = NULL; 279 pp->pr_entered_line = 0; 280 } 281 282 static __inline void 283 pr_enter_check(struct pool *pp, int (*pr)(const char *, ...)) 284 { 285 286 if (pp->pr_entered_file != NULL) 287 (*pr)("\n\tcurrently entered from file %s line %ld\n", 288 pp->pr_entered_file, pp->pr_entered_line); 289 } 290 #else 291 #define pr_log(pp, v, action, file, line) 292 #define pr_printlog(pp, pi, pr) 293 #define pr_enter(pp, file, line) 294 #define pr_leave(pp) 295 #define pr_enter_check(pp, pr) 296 #endif /* POOL_DIAGNOSTIC */ 297 298 static __inline int 299 phtree_compare(struct pool_item_header *a, struct pool_item_header *b) 300 { 301 if (a->ph_page < b->ph_page) 302 return (-1); 303 else if (a->ph_page > b->ph_page) 304 return (1); 305 else 306 return (0); 307 } 308 309 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare); 310 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare); 311 312 /* 313 * Return the pool page header based on page address. 314 */ 315 static __inline struct pool_item_header * 316 pr_find_pagehead(struct pool *pp, caddr_t page) 317 { 318 struct pool_item_header *ph, tmp; 319 320 if ((pp->pr_roflags & PR_PHINPAGE) != 0) 321 return ((struct pool_item_header *)(page + pp->pr_phoffset)); 322 323 tmp.ph_page = page; 324 ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp); 325 return ph; 326 } 327 328 /* 329 * Remove a page from the pool. 330 */ 331 void 332 pr_rmpage(struct pool *pp, struct pool_item_header *ph, 333 struct pool_pagelist *pq) 334 { 335 int s; 336 337 /* 338 * If the page was idle, decrement the idle page count. 339 */ 340 if (ph->ph_nmissing == 0) { 341 #ifdef DIAGNOSTIC 342 if (pp->pr_nidle == 0) 343 panic("pr_rmpage: nidle inconsistent"); 344 if (pp->pr_nitems < pp->pr_itemsperpage) 345 panic("pr_rmpage: nitems inconsistent"); 346 #endif 347 pp->pr_nidle--; 348 } 349 350 pp->pr_nitems -= pp->pr_itemsperpage; 351 352 /* 353 * Unlink a page from the pool and release it (or queue it for release). 354 */ 355 LIST_REMOVE(ph, ph_pagelist); 356 if (pq) { 357 LIST_INSERT_HEAD(pq, ph, ph_pagelist); 358 } else { 359 pool_allocator_free(pp, ph->ph_page); 360 if ((pp->pr_roflags & PR_PHINPAGE) == 0) { 361 SPLAY_REMOVE(phtree, &pp->pr_phtree, ph); 362 s = splhigh(); 363 pool_put(&phpool, ph); 364 splx(s); 365 } 366 } 367 pp->pr_npages--; 368 pp->pr_npagefree++; 369 370 pool_update_curpage(pp); 371 } 372 373 /* 374 * Initialize the given pool resource structure. 375 * 376 * We export this routine to allow other kernel parts to declare 377 * static pools that must be initialized before malloc() is available. 378 */ 379 void 380 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags, 381 const char *wchan, struct pool_allocator *palloc) 382 { 383 int off, slack; 384 385 #ifdef POOL_DIAGNOSTIC 386 /* 387 * Always log if POOL_DIAGNOSTIC is defined. 388 */ 389 if (pool_logsize != 0) 390 flags |= PR_LOGGING; 391 #endif 392 393 #ifdef MALLOC_DEBUG 394 if ((flags & PR_DEBUG) && (ioff != 0 || align != 0)) 395 flags &= ~PR_DEBUG; 396 #endif 397 /* 398 * Check arguments and construct default values. 399 */ 400 if (palloc == NULL) 401 palloc = &pool_allocator_nointr; 402 if ((palloc->pa_flags & PA_INITIALIZED) == 0) { 403 if (palloc->pa_pagesz == 0) 404 palloc->pa_pagesz = PAGE_SIZE; 405 406 TAILQ_INIT(&palloc->pa_list); 407 408 simple_lock_init(&palloc->pa_slock); 409 palloc->pa_pagemask = ~(palloc->pa_pagesz - 1); 410 palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1; 411 palloc->pa_flags |= PA_INITIALIZED; 412 } 413 414 if (align == 0) 415 align = ALIGN(1); 416 417 if (size < sizeof(struct pool_item)) 418 size = sizeof(struct pool_item); 419 420 size = roundup(size, align); 421 #ifdef DIAGNOSTIC 422 if (size > palloc->pa_pagesz) 423 panic("pool_init: pool item size (%lu) too large", 424 (u_long)size); 425 #endif 426 427 /* 428 * Initialize the pool structure. 429 */ 430 LIST_INIT(&pp->pr_emptypages); 431 LIST_INIT(&pp->pr_fullpages); 432 LIST_INIT(&pp->pr_partpages); 433 TAILQ_INIT(&pp->pr_cachelist); 434 pp->pr_curpage = NULL; 435 pp->pr_npages = 0; 436 pp->pr_minitems = 0; 437 pp->pr_minpages = 0; 438 pp->pr_maxpages = 8; 439 pp->pr_roflags = flags; 440 pp->pr_flags = 0; 441 pp->pr_size = size; 442 pp->pr_align = align; 443 pp->pr_wchan = wchan; 444 pp->pr_alloc = palloc; 445 pp->pr_nitems = 0; 446 pp->pr_nout = 0; 447 pp->pr_hardlimit = UINT_MAX; 448 pp->pr_hardlimit_warning = NULL; 449 pp->pr_hardlimit_ratecap.tv_sec = 0; 450 pp->pr_hardlimit_ratecap.tv_usec = 0; 451 pp->pr_hardlimit_warning_last.tv_sec = 0; 452 pp->pr_hardlimit_warning_last.tv_usec = 0; 453 pp->pr_drain_hook = NULL; 454 pp->pr_drain_hook_arg = NULL; 455 pp->pr_serial = ++pool_serial; 456 if (pool_serial == 0) 457 panic("pool_init: too much uptime"); 458 459 /* 460 * Decide whether to put the page header off page to avoid 461 * wasting too large a part of the page. Off-page page headers 462 * go on a hash table, so we can match a returned item 463 * with its header based on the page address. 464 * We use 1/16 of the page size as the threshold (XXX: tune) 465 */ 466 if (pp->pr_size < palloc->pa_pagesz/16) { 467 /* Use the end of the page for the page header */ 468 pp->pr_roflags |= PR_PHINPAGE; 469 pp->pr_phoffset = off = palloc->pa_pagesz - 470 ALIGN(sizeof(struct pool_item_header)); 471 } else { 472 /* The page header will be taken from our page header pool */ 473 pp->pr_phoffset = 0; 474 off = palloc->pa_pagesz; 475 SPLAY_INIT(&pp->pr_phtree); 476 } 477 478 /* 479 * Alignment is to take place at `ioff' within the item. This means 480 * we must reserve up to `align - 1' bytes on the page to allow 481 * appropriate positioning of each item. 482 * 483 * Silently enforce `0 <= ioff < align'. 484 */ 485 pp->pr_itemoffset = ioff = ioff % align; 486 pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size; 487 KASSERT(pp->pr_itemsperpage != 0); 488 489 /* 490 * Use the slack between the chunks and the page header 491 * for "cache coloring". 492 */ 493 slack = off - pp->pr_itemsperpage * pp->pr_size; 494 pp->pr_maxcolor = (slack / align) * align; 495 pp->pr_curcolor = 0; 496 497 pp->pr_nget = 0; 498 pp->pr_nfail = 0; 499 pp->pr_nput = 0; 500 pp->pr_npagealloc = 0; 501 pp->pr_npagefree = 0; 502 pp->pr_hiwat = 0; 503 pp->pr_nidle = 0; 504 505 #ifdef POOL_DIAGNOSTIC 506 if (flags & PR_LOGGING) { 507 if (kmem_map == NULL || 508 (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log), 509 M_TEMP, M_NOWAIT)) == NULL) 510 pp->pr_roflags &= ~PR_LOGGING; 511 pp->pr_curlogentry = 0; 512 pp->pr_logsize = pool_logsize; 513 } 514 #endif 515 516 pp->pr_entered_file = NULL; 517 pp->pr_entered_line = 0; 518 519 simple_lock_init(&pp->pr_slock); 520 521 /* 522 * Initialize private page header pool and cache magazine pool if we 523 * haven't done so yet. 524 * XXX LOCKING. 525 */ 526 if (phpool.pr_size == 0) { 527 pool_init(&phpool, sizeof(struct pool_item_header), 0, 0, 528 0, "phpool", NULL); 529 pool_init(&pcgpool, sizeof(struct pool_cache_group), 0, 0, 530 0, "pcgpool", NULL); 531 } 532 533 simple_lock_init(&pool_head_slock); 534 535 /* Insert this into the list of all pools. */ 536 simple_lock(&pool_head_slock); 537 TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist); 538 simple_unlock(&pool_head_slock); 539 540 /* Insert into the list of pools using this allocator. */ 541 simple_lock(&palloc->pa_slock); 542 TAILQ_INSERT_TAIL(&palloc->pa_list, pp, pr_alloc_list); 543 simple_unlock(&palloc->pa_slock); 544 } 545 546 /* 547 * De-commision a pool resource. 548 */ 549 void 550 pool_destroy(struct pool *pp) 551 { 552 struct pool_item_header *ph; 553 struct pool_cache *pc; 554 555 /* Locking order: pool_allocator -> pool */ 556 simple_lock(&pp->pr_alloc->pa_slock); 557 TAILQ_REMOVE(&pp->pr_alloc->pa_list, pp, pr_alloc_list); 558 simple_unlock(&pp->pr_alloc->pa_slock); 559 560 /* Destroy all caches for this pool. */ 561 while ((pc = TAILQ_FIRST(&pp->pr_cachelist)) != NULL) 562 pool_cache_destroy(pc); 563 564 #ifdef DIAGNOSTIC 565 if (pp->pr_nout != 0) { 566 pr_printlog(pp, NULL, printf); 567 panic("pool_destroy: pool busy: still out: %u", 568 pp->pr_nout); 569 } 570 #endif 571 572 /* Remove all pages */ 573 while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) 574 pr_rmpage(pp, ph, NULL); 575 KASSERT(LIST_EMPTY(&pp->pr_fullpages)); 576 KASSERT(LIST_EMPTY(&pp->pr_partpages)); 577 578 /* Remove from global pool list */ 579 simple_lock(&pool_head_slock); 580 TAILQ_REMOVE(&pool_head, pp, pr_poollist); 581 if (drainpp == pp) { 582 drainpp = NULL; 583 } 584 simple_unlock(&pool_head_slock); 585 586 #ifdef POOL_DIAGNOSTIC 587 if ((pp->pr_roflags & PR_LOGGING) != 0) 588 free(pp->pr_log, M_TEMP); 589 #endif 590 } 591 592 void 593 pool_set_drain_hook(struct pool *pp, void (*fn)(void *, int), void *arg) 594 { 595 /* XXX no locking -- must be used just after pool_init() */ 596 #ifdef DIAGNOSTIC 597 if (pp->pr_drain_hook != NULL) 598 panic("pool_set_drain_hook(%s): already set", pp->pr_wchan); 599 #endif 600 pp->pr_drain_hook = fn; 601 pp->pr_drain_hook_arg = arg; 602 } 603 604 static struct pool_item_header * 605 pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags) 606 { 607 struct pool_item_header *ph; 608 int s; 609 610 LOCK_ASSERT(simple_lock_held(&pp->pr_slock) == 0); 611 612 if ((pp->pr_roflags & PR_PHINPAGE) != 0) 613 ph = (struct pool_item_header *) (storage + pp->pr_phoffset); 614 else { 615 s = splhigh(); 616 ph = pool_get(&phpool, flags); 617 splx(s); 618 } 619 620 return (ph); 621 } 622 623 /* 624 * Grab an item from the pool; must be called at appropriate spl level 625 */ 626 void * 627 #ifdef POOL_DIAGNOSTIC 628 _pool_get(struct pool *pp, int flags, const char *file, long line) 629 #else 630 pool_get(struct pool *pp, int flags) 631 #endif 632 { 633 struct pool_item *pi; 634 struct pool_item_header *ph; 635 void *v; 636 637 #ifdef DIAGNOSTIC 638 if ((flags & PR_WAITOK) != 0) 639 splassert(IPL_NONE); 640 if (__predict_false(curproc == NULL && /* doing_shutdown == 0 && XXX*/ 641 (flags & PR_WAITOK) != 0)) 642 panic("pool_get: %s:must have NOWAIT", pp->pr_wchan); 643 644 #ifdef LOCKDEBUG 645 if (flags & PR_WAITOK) 646 simple_lock_only_held(NULL, "pool_get(PR_WAITOK)"); 647 #endif 648 #endif /* DIAGNOSTIC */ 649 650 #ifdef MALLOC_DEBUG 651 if (pp->pr_roflags & PR_DEBUG) { 652 void *addr; 653 654 addr = NULL; 655 debug_malloc(pp->pr_size, M_DEBUG, 656 (flags & PR_WAITOK) ? M_WAITOK : M_NOWAIT, &addr); 657 return (addr); 658 } 659 #endif 660 661 simple_lock(&pp->pr_slock); 662 pr_enter(pp, file, line); 663 664 startover: 665 /* 666 * Check to see if we've reached the hard limit. If we have, 667 * and we can wait, then wait until an item has been returned to 668 * the pool. 669 */ 670 #ifdef DIAGNOSTIC 671 if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) { 672 pr_leave(pp); 673 simple_unlock(&pp->pr_slock); 674 panic("pool_get: %s: crossed hard limit", pp->pr_wchan); 675 } 676 #endif 677 if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) { 678 if (pp->pr_drain_hook != NULL) { 679 /* 680 * Since the drain hook is going to free things 681 * back to the pool, unlock, call hook, re-lock 682 * and check hardlimit condition again. 683 */ 684 pr_leave(pp); 685 simple_unlock(&pp->pr_slock); 686 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, flags); 687 simple_lock(&pp->pr_slock); 688 pr_enter(pp, file, line); 689 if (pp->pr_nout < pp->pr_hardlimit) 690 goto startover; 691 } 692 693 if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) { 694 /* 695 * XXX: A warning isn't logged in this case. Should 696 * it be? 697 */ 698 pp->pr_flags |= PR_WANTED; 699 pr_leave(pp); 700 ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock); 701 pr_enter(pp, file, line); 702 goto startover; 703 } 704 705 /* 706 * Log a message that the hard limit has been hit. 707 */ 708 if (pp->pr_hardlimit_warning != NULL && 709 ratecheck(&pp->pr_hardlimit_warning_last, 710 &pp->pr_hardlimit_ratecap)) 711 log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning); 712 713 pp->pr_nfail++; 714 715 pr_leave(pp); 716 simple_unlock(&pp->pr_slock); 717 return (NULL); 718 } 719 720 /* 721 * The convention we use is that if `curpage' is not NULL, then 722 * it points at a non-empty bucket. In particular, `curpage' 723 * never points at a page header which has PR_PHINPAGE set and 724 * has no items in its bucket. 725 */ 726 if ((ph = pp->pr_curpage) == NULL) { 727 #ifdef DIAGNOSTIC 728 if (pp->pr_nitems != 0) { 729 simple_unlock(&pp->pr_slock); 730 printf("pool_get: %s: curpage NULL, nitems %u\n", 731 pp->pr_wchan, pp->pr_nitems); 732 panic("pool_get: nitems inconsistent"); 733 } 734 #endif 735 736 /* 737 * Call the back-end page allocator for more memory. 738 * Release the pool lock, as the back-end page allocator 739 * may block. 740 */ 741 pr_leave(pp); 742 simple_unlock(&pp->pr_slock); 743 v = pool_allocator_alloc(pp, flags); 744 if (__predict_true(v != NULL)) 745 ph = pool_alloc_item_header(pp, v, flags); 746 simple_lock(&pp->pr_slock); 747 pr_enter(pp, file, line); 748 749 if (__predict_false(v == NULL || ph == NULL)) { 750 if (v != NULL) 751 pool_allocator_free(pp, v); 752 753 /* 754 * We were unable to allocate a page or item 755 * header, but we released the lock during 756 * allocation, so perhaps items were freed 757 * back to the pool. Check for this case. 758 */ 759 if (pp->pr_curpage != NULL) 760 goto startover; 761 762 if ((flags & PR_WAITOK) == 0) { 763 pp->pr_nfail++; 764 pr_leave(pp); 765 simple_unlock(&pp->pr_slock); 766 return (NULL); 767 } 768 769 /* 770 * Wait for items to be returned to this pool. 771 * 772 * XXX: maybe we should wake up once a second and 773 * try again? 774 */ 775 pp->pr_flags |= PR_WANTED; 776 /* PA_WANTED is already set on the allocator. */ 777 pr_leave(pp); 778 ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock); 779 pr_enter(pp, file, line); 780 goto startover; 781 } 782 783 /* We have more memory; add it to the pool */ 784 pool_prime_page(pp, v, ph); 785 pp->pr_npagealloc++; 786 787 /* Start the allocation process over. */ 788 goto startover; 789 } 790 if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) { 791 pr_leave(pp); 792 simple_unlock(&pp->pr_slock); 793 panic("pool_get: %s: page empty", pp->pr_wchan); 794 } 795 #ifdef DIAGNOSTIC 796 if (__predict_false(pp->pr_nitems == 0)) { 797 pr_leave(pp); 798 simple_unlock(&pp->pr_slock); 799 printf("pool_get: %s: items on itemlist, nitems %u\n", 800 pp->pr_wchan, pp->pr_nitems); 801 panic("pool_get: nitems inconsistent"); 802 } 803 #endif 804 805 #ifdef POOL_DIAGNOSTIC 806 pr_log(pp, v, PRLOG_GET, file, line); 807 #endif 808 809 #ifdef DIAGNOSTIC 810 if (__predict_false(pi->pi_magic != PI_MAGIC)) { 811 pr_printlog(pp, pi, printf); 812 panic("pool_get(%s): free list modified: magic=%x; page %p;" 813 " item addr %p", 814 pp->pr_wchan, pi->pi_magic, ph->ph_page, pi); 815 } 816 #endif 817 818 /* 819 * Remove from item list. 820 */ 821 TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list); 822 pp->pr_nitems--; 823 pp->pr_nout++; 824 if (ph->ph_nmissing == 0) { 825 #ifdef DIAGNOSTIC 826 if (__predict_false(pp->pr_nidle == 0)) 827 panic("pool_get: nidle inconsistent"); 828 #endif 829 pp->pr_nidle--; 830 831 /* 832 * This page was previously empty. Move it to the list of 833 * partially-full pages. This page is already curpage. 834 */ 835 LIST_REMOVE(ph, ph_pagelist); 836 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); 837 } 838 ph->ph_nmissing++; 839 if (TAILQ_EMPTY(&ph->ph_itemlist)) { 840 #ifdef DIAGNOSTIC 841 if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) { 842 pr_leave(pp); 843 simple_unlock(&pp->pr_slock); 844 panic("pool_get: %s: nmissing inconsistent", 845 pp->pr_wchan); 846 } 847 #endif 848 /* 849 * This page is now full. Move it to the full list 850 * and select a new current page. 851 */ 852 LIST_REMOVE(ph, ph_pagelist); 853 LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist); 854 pool_update_curpage(pp); 855 } 856 857 pp->pr_nget++; 858 859 /* 860 * If we have a low water mark and we are now below that low 861 * water mark, add more items to the pool. 862 */ 863 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) { 864 /* 865 * XXX: Should we log a warning? Should we set up a timeout 866 * to try again in a second or so? The latter could break 867 * a caller's assumptions about interrupt protection, etc. 868 */ 869 } 870 871 pr_leave(pp); 872 simple_unlock(&pp->pr_slock); 873 return (v); 874 } 875 876 /* 877 * Internal version of pool_put(). Pool is already locked/entered. 878 */ 879 void 880 pool_do_put(struct pool *pp, void *v) 881 { 882 struct pool_item *pi = v; 883 struct pool_item_header *ph; 884 caddr_t page; 885 886 #ifdef MALLOC_DEBUG 887 if (pp->pr_roflags & PR_DEBUG) { 888 debug_free(v, M_DEBUG); 889 return; 890 } 891 #endif 892 893 LOCK_ASSERT(simple_lock_held(&pp->pr_slock)); 894 895 page = (caddr_t)((vaddr_t)v & pp->pr_alloc->pa_pagemask); 896 897 #ifdef DIAGNOSTIC 898 if (__predict_false(pp->pr_nout == 0)) { 899 printf("pool %s: putting with none out\n", 900 pp->pr_wchan); 901 panic("pool_put"); 902 } 903 #endif 904 905 if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) { 906 pr_printlog(pp, NULL, printf); 907 panic("pool_put: %s: page header missing", pp->pr_wchan); 908 } 909 910 #ifdef LOCKDEBUG 911 /* 912 * Check if we're freeing a locked simple lock. 913 */ 914 simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size); 915 #endif 916 917 /* 918 * Return to item list. 919 */ 920 #ifdef DIAGNOSTIC 921 pi->pi_magic = PI_MAGIC; 922 #endif 923 #ifdef DEBUG 924 { 925 int i, *ip = v; 926 927 for (i = 0; i < pp->pr_size / sizeof(int); i++) { 928 *ip++ = PI_MAGIC; 929 } 930 } 931 #endif 932 933 TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list); 934 ph->ph_nmissing--; 935 pp->pr_nput++; 936 pp->pr_nitems++; 937 pp->pr_nout--; 938 939 /* Cancel "pool empty" condition if it exists */ 940 if (pp->pr_curpage == NULL) 941 pp->pr_curpage = ph; 942 943 if (pp->pr_flags & PR_WANTED) { 944 pp->pr_flags &= ~PR_WANTED; 945 if (ph->ph_nmissing == 0) 946 pp->pr_nidle++; 947 wakeup((caddr_t)pp); 948 return; 949 } 950 951 /* 952 * If this page is now empty, do one of two things: 953 * 954 * (1) If we have more pages than the page high water mark, 955 * free the page back to the system. 956 * 957 * (2) Otherwise, move the page to the empty page list. 958 * 959 * Either way, select a new current page (so we use a partially-full 960 * page if one is available). 961 */ 962 if (ph->ph_nmissing == 0) { 963 pp->pr_nidle++; 964 if (pp->pr_nidle > pp->pr_maxpages || 965 (pp->pr_alloc->pa_flags & PA_WANT) != 0) { 966 pr_rmpage(pp, ph, NULL); 967 } else { 968 LIST_REMOVE(ph, ph_pagelist); 969 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); 970 971 /* 972 * Update the timestamp on the page. A page must 973 * be idle for some period of time before it can 974 * be reclaimed by the pagedaemon. This minimizes 975 * ping-pong'ing for memory. 976 */ 977 microuptime(&ph->ph_time); 978 } 979 pool_update_curpage(pp); 980 } 981 982 /* 983 * If the page was previously completely full, move it to the 984 * partially-full list and make it the current page. The next 985 * allocation will get the item from this page, instead of 986 * further fragmenting the pool. 987 */ 988 else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) { 989 LIST_REMOVE(ph, ph_pagelist); 990 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); 991 pp->pr_curpage = ph; 992 } 993 } 994 995 /* 996 * Return resource to the pool; must be called at appropriate spl level 997 */ 998 #ifdef POOL_DIAGNOSTIC 999 void 1000 _pool_put(struct pool *pp, void *v, const char *file, long line) 1001 { 1002 1003 simple_lock(&pp->pr_slock); 1004 pr_enter(pp, file, line); 1005 1006 pr_log(pp, v, PRLOG_PUT, file, line); 1007 1008 pool_do_put(pp, v); 1009 1010 pr_leave(pp); 1011 simple_unlock(&pp->pr_slock); 1012 } 1013 #undef pool_put 1014 #endif /* POOL_DIAGNOSTIC */ 1015 1016 void 1017 pool_put(struct pool *pp, void *v) 1018 { 1019 1020 simple_lock(&pp->pr_slock); 1021 1022 pool_do_put(pp, v); 1023 1024 simple_unlock(&pp->pr_slock); 1025 } 1026 1027 #ifdef POOL_DIAGNOSTIC 1028 #define pool_put(h, v) _pool_put((h), (v), __FILE__, __LINE__) 1029 #endif 1030 1031 /* 1032 * Add N items to the pool. 1033 */ 1034 int 1035 pool_prime(struct pool *pp, int n) 1036 { 1037 struct pool_item_header *ph; 1038 caddr_t cp; 1039 int newpages; 1040 1041 simple_lock(&pp->pr_slock); 1042 1043 newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 1044 1045 while (newpages-- > 0) { 1046 simple_unlock(&pp->pr_slock); 1047 cp = pool_allocator_alloc(pp, PR_NOWAIT); 1048 if (__predict_true(cp != NULL)) 1049 ph = pool_alloc_item_header(pp, cp, PR_NOWAIT); 1050 simple_lock(&pp->pr_slock); 1051 1052 if (__predict_false(cp == NULL || ph == NULL)) { 1053 if (cp != NULL) 1054 pool_allocator_free(pp, cp); 1055 break; 1056 } 1057 1058 pool_prime_page(pp, cp, ph); 1059 pp->pr_npagealloc++; 1060 pp->pr_minpages++; 1061 } 1062 1063 if (pp->pr_minpages >= pp->pr_maxpages) 1064 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */ 1065 1066 simple_unlock(&pp->pr_slock); 1067 return (0); 1068 } 1069 1070 /* 1071 * Add a page worth of items to the pool. 1072 * 1073 * Note, we must be called with the pool descriptor LOCKED. 1074 */ 1075 void 1076 pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph) 1077 { 1078 struct pool_item *pi; 1079 caddr_t cp = storage; 1080 unsigned int align = pp->pr_align; 1081 unsigned int ioff = pp->pr_itemoffset; 1082 int n; 1083 1084 #ifdef DIAGNOSTIC 1085 if (((u_long)cp & (pp->pr_alloc->pa_pagesz - 1)) != 0) 1086 panic("pool_prime_page: %s: unaligned page", pp->pr_wchan); 1087 #endif 1088 1089 /* 1090 * Insert page header. 1091 */ 1092 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); 1093 TAILQ_INIT(&ph->ph_itemlist); 1094 ph->ph_page = storage; 1095 ph->ph_nmissing = 0; 1096 memset(&ph->ph_time, 0, sizeof(ph->ph_time)); 1097 if ((pp->pr_roflags & PR_PHINPAGE) == 0) 1098 SPLAY_INSERT(phtree, &pp->pr_phtree, ph); 1099 1100 pp->pr_nidle++; 1101 1102 /* 1103 * Color this page. 1104 */ 1105 cp = (caddr_t)(cp + pp->pr_curcolor); 1106 if ((pp->pr_curcolor += align) > pp->pr_maxcolor) 1107 pp->pr_curcolor = 0; 1108 1109 /* 1110 * Adjust storage to apply aligment to `pr_itemoffset' in each item. 1111 */ 1112 if (ioff != 0) 1113 cp = (caddr_t)(cp + (align - ioff)); 1114 1115 /* 1116 * Insert remaining chunks on the bucket list. 1117 */ 1118 n = pp->pr_itemsperpage; 1119 pp->pr_nitems += n; 1120 1121 while (n--) { 1122 pi = (struct pool_item *)cp; 1123 1124 KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0); 1125 1126 /* Insert on page list */ 1127 TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list); 1128 #ifdef DIAGNOSTIC 1129 pi->pi_magic = PI_MAGIC; 1130 #endif 1131 cp = (caddr_t)(cp + pp->pr_size); 1132 } 1133 1134 /* 1135 * If the pool was depleted, point at the new page. 1136 */ 1137 if (pp->pr_curpage == NULL) 1138 pp->pr_curpage = ph; 1139 1140 if (++pp->pr_npages > pp->pr_hiwat) 1141 pp->pr_hiwat = pp->pr_npages; 1142 } 1143 1144 /* 1145 * Used by pool_get() when nitems drops below the low water mark. This 1146 * is used to catch up pr_nitems with the low water mark. 1147 * 1148 * Note 1, we never wait for memory here, we let the caller decide what to do. 1149 * 1150 * Note 2, we must be called with the pool already locked, and we return 1151 * with it locked. 1152 */ 1153 int 1154 pool_catchup(struct pool *pp) 1155 { 1156 struct pool_item_header *ph; 1157 caddr_t cp; 1158 int error = 0; 1159 1160 while (POOL_NEEDS_CATCHUP(pp)) { 1161 /* 1162 * Call the page back-end allocator for more memory. 1163 * 1164 * XXX: We never wait, so should we bother unlocking 1165 * the pool descriptor? 1166 */ 1167 simple_unlock(&pp->pr_slock); 1168 cp = pool_allocator_alloc(pp, PR_NOWAIT); 1169 if (__predict_true(cp != NULL)) 1170 ph = pool_alloc_item_header(pp, cp, PR_NOWAIT); 1171 simple_lock(&pp->pr_slock); 1172 if (__predict_false(cp == NULL || ph == NULL)) { 1173 if (cp != NULL) 1174 pool_allocator_free(pp, cp); 1175 error = ENOMEM; 1176 break; 1177 } 1178 pool_prime_page(pp, cp, ph); 1179 pp->pr_npagealloc++; 1180 } 1181 1182 return (error); 1183 } 1184 1185 void 1186 pool_update_curpage(struct pool *pp) 1187 { 1188 1189 pp->pr_curpage = LIST_FIRST(&pp->pr_partpages); 1190 if (pp->pr_curpage == NULL) { 1191 pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages); 1192 } 1193 } 1194 1195 void 1196 pool_setlowat(struct pool *pp, int n) 1197 { 1198 1199 simple_lock(&pp->pr_slock); 1200 1201 pp->pr_minitems = n; 1202 pp->pr_minpages = (n == 0) 1203 ? 0 1204 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 1205 1206 /* Make sure we're caught up with the newly-set low water mark. */ 1207 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) { 1208 /* 1209 * XXX: Should we log a warning? Should we set up a timeout 1210 * to try again in a second or so? The latter could break 1211 * a caller's assumptions about interrupt protection, etc. 1212 */ 1213 } 1214 1215 simple_unlock(&pp->pr_slock); 1216 } 1217 1218 void 1219 pool_sethiwat(struct pool *pp, int n) 1220 { 1221 1222 simple_lock(&pp->pr_slock); 1223 1224 pp->pr_maxpages = (n == 0) 1225 ? 0 1226 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 1227 1228 simple_unlock(&pp->pr_slock); 1229 } 1230 1231 int 1232 pool_sethardlimit(struct pool *pp, unsigned n, const char *warnmess, int ratecap) 1233 { 1234 int error = 0; 1235 1236 simple_lock(&pp->pr_slock); 1237 1238 if (n < pp->pr_nout) { 1239 error = EINVAL; 1240 goto done; 1241 } 1242 1243 pp->pr_hardlimit = n; 1244 pp->pr_hardlimit_warning = warnmess; 1245 pp->pr_hardlimit_ratecap.tv_sec = ratecap; 1246 pp->pr_hardlimit_warning_last.tv_sec = 0; 1247 pp->pr_hardlimit_warning_last.tv_usec = 0; 1248 1249 /* 1250 * In-line version of pool_sethiwat(), because we don't want to 1251 * release the lock. 1252 */ 1253 pp->pr_maxpages = (n == 0 || n == UINT_MAX) 1254 ? n 1255 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 1256 1257 done: 1258 simple_unlock(&pp->pr_slock); 1259 1260 return (error); 1261 } 1262 1263 /* 1264 * Release all complete pages that have not been used recently. 1265 * 1266 * Returns non-zero if any pages have been reclaimed. 1267 */ 1268 int 1269 #ifdef POOL_DIAGNOSTIC 1270 _pool_reclaim(struct pool *pp, const char *file, long line) 1271 #else 1272 pool_reclaim(struct pool *pp) 1273 #endif 1274 { 1275 struct pool_item_header *ph, *phnext; 1276 struct pool_cache *pc; 1277 struct timeval curtime; 1278 struct pool_pagelist pq; 1279 struct timeval diff; 1280 int s; 1281 1282 if (pp->pr_drain_hook != NULL) { 1283 /* 1284 * The drain hook must be called with the pool unlocked. 1285 */ 1286 (*pp->pr_drain_hook)(pp->pr_drain_hook_arg, PR_NOWAIT); 1287 } 1288 1289 if (simple_lock_try(&pp->pr_slock) == 0) 1290 return (0); 1291 pr_enter(pp, file, line); 1292 1293 LIST_INIT(&pq); 1294 1295 /* 1296 * Reclaim items from the pool's caches. 1297 */ 1298 TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist) 1299 pool_cache_reclaim(pc); 1300 1301 microuptime(&curtime); 1302 1303 for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) { 1304 phnext = LIST_NEXT(ph, ph_pagelist); 1305 1306 /* Check our minimum page claim */ 1307 if (pp->pr_npages <= pp->pr_minpages) 1308 break; 1309 1310 KASSERT(ph->ph_nmissing == 0); 1311 timersub(&curtime, &ph->ph_time, &diff); 1312 if (diff.tv_sec < pool_inactive_time) 1313 continue; 1314 1315 /* 1316 * If freeing this page would put us below 1317 * the low water mark, stop now. 1318 */ 1319 if ((pp->pr_nitems - pp->pr_itemsperpage) < 1320 pp->pr_minitems) 1321 break; 1322 1323 pr_rmpage(pp, ph, &pq); 1324 } 1325 1326 pr_leave(pp); 1327 simple_unlock(&pp->pr_slock); 1328 if (LIST_EMPTY(&pq)) 1329 return (0); 1330 while ((ph = LIST_FIRST(&pq)) != NULL) { 1331 LIST_REMOVE(ph, ph_pagelist); 1332 pool_allocator_free(pp, ph->ph_page); 1333 if (pp->pr_roflags & PR_PHINPAGE) { 1334 continue; 1335 } 1336 SPLAY_REMOVE(phtree, &pp->pr_phtree, ph); 1337 s = splhigh(); 1338 pool_put(&phpool, ph); 1339 splx(s); 1340 } 1341 1342 return (1); 1343 } 1344 1345 1346 /* 1347 * Drain pools, one at a time. 1348 * 1349 * Note, we must never be called from an interrupt context. 1350 */ 1351 void 1352 pool_drain(void *arg) 1353 { 1354 struct pool *pp; 1355 int s; 1356 1357 pp = NULL; 1358 s = splvm(); 1359 simple_lock(&pool_head_slock); 1360 if (drainpp == NULL) { 1361 drainpp = TAILQ_FIRST(&pool_head); 1362 } 1363 if (drainpp) { 1364 pp = drainpp; 1365 drainpp = TAILQ_NEXT(pp, pr_poollist); 1366 } 1367 simple_unlock(&pool_head_slock); 1368 pool_reclaim(pp); 1369 splx(s); 1370 } 1371 1372 #ifdef DDB 1373 /* 1374 * Diagnostic helpers. 1375 */ 1376 void 1377 pool_printit(struct pool *pp, const char *modif, int (*pr)(const char *, ...)) 1378 { 1379 int s; 1380 1381 s = splvm(); 1382 if (simple_lock_try(&pp->pr_slock) == 0) { 1383 pr("pool %s is locked; try again later\n", 1384 pp->pr_wchan); 1385 splx(s); 1386 return; 1387 } 1388 pool_print1(pp, modif, pr); 1389 simple_unlock(&pp->pr_slock); 1390 splx(s); 1391 } 1392 1393 void 1394 pool_print_pagelist(struct pool_pagelist *pl, int (*pr)(const char *, ...)) 1395 { 1396 struct pool_item_header *ph; 1397 #ifdef DIAGNOSTIC 1398 struct pool_item *pi; 1399 #endif 1400 1401 LIST_FOREACH(ph, pl, ph_pagelist) { 1402 (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n", 1403 ph->ph_page, ph->ph_nmissing, 1404 (u_long)ph->ph_time.tv_sec, 1405 (u_long)ph->ph_time.tv_usec); 1406 #ifdef DIAGNOSTIC 1407 TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { 1408 if (pi->pi_magic != PI_MAGIC) { 1409 (*pr)("\t\t\titem %p, magic 0x%x\n", 1410 pi, pi->pi_magic); 1411 } 1412 } 1413 #endif 1414 } 1415 } 1416 1417 void 1418 pool_print1(struct pool *pp, const char *modif, int (*pr)(const char *, ...)) 1419 { 1420 struct pool_item_header *ph; 1421 struct pool_cache *pc; 1422 struct pool_cache_group *pcg; 1423 int i, print_log = 0, print_pagelist = 0, print_cache = 0; 1424 char c; 1425 1426 while ((c = *modif++) != '\0') { 1427 if (c == 'l') 1428 print_log = 1; 1429 if (c == 'p') 1430 print_pagelist = 1; 1431 if (c == 'c') 1432 print_cache = 1; 1433 modif++; 1434 } 1435 1436 (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n", 1437 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset, 1438 pp->pr_roflags); 1439 (*pr)("\talloc %p\n", pp->pr_alloc); 1440 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n", 1441 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages); 1442 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n", 1443 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit); 1444 1445 (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n", 1446 pp->pr_nget, pp->pr_nfail, pp->pr_nput); 1447 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n", 1448 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle); 1449 1450 if (print_pagelist == 0) 1451 goto skip_pagelist; 1452 1453 if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) 1454 (*pr)("\n\tempty page list:\n"); 1455 pool_print_pagelist(&pp->pr_emptypages, pr); 1456 if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL) 1457 (*pr)("\n\tfull page list:\n"); 1458 pool_print_pagelist(&pp->pr_fullpages, pr); 1459 if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL) 1460 (*pr)("\n\tpartial-page list:\n"); 1461 pool_print_pagelist(&pp->pr_partpages, pr); 1462 1463 if (pp->pr_curpage == NULL) 1464 (*pr)("\tno current page\n"); 1465 else 1466 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page); 1467 1468 skip_pagelist: 1469 if (print_log == 0) 1470 goto skip_log; 1471 1472 (*pr)("\n"); 1473 if ((pp->pr_roflags & PR_LOGGING) == 0) 1474 (*pr)("\tno log\n"); 1475 else 1476 pr_printlog(pp, NULL, pr); 1477 1478 skip_log: 1479 if (print_cache == 0) 1480 goto skip_cache; 1481 1482 TAILQ_FOREACH(pc, &pp->pr_cachelist, pc_poollist) { 1483 (*pr)("\tcache %p: allocfrom %p freeto %p\n", pc, 1484 pc->pc_allocfrom, pc->pc_freeto); 1485 (*pr)("\t hits %lu misses %lu ngroups %lu nitems %lu\n", 1486 pc->pc_hits, pc->pc_misses, pc->pc_ngroups, pc->pc_nitems); 1487 TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) { 1488 (*pr)("\t\tgroup %p: avail %d\n", pcg, pcg->pcg_avail); 1489 for (i = 0; i < PCG_NOBJECTS; i++) 1490 (*pr)("\t\t\t%p\n", pcg->pcg_objects[i]); 1491 } 1492 } 1493 1494 skip_cache: 1495 pr_enter_check(pp, pr); 1496 } 1497 1498 int 1499 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph) 1500 { 1501 struct pool_item *pi; 1502 caddr_t page; 1503 int n; 1504 1505 page = (caddr_t)((u_long)ph & pp->pr_alloc->pa_pagemask); 1506 if (page != ph->ph_page && 1507 (pp->pr_roflags & PR_PHINPAGE) != 0) { 1508 if (label != NULL) 1509 printf("%s: ", label); 1510 printf("pool(%p:%s): page inconsistency: page %p;" 1511 " at page head addr %p (p %p)\n", pp, 1512 pp->pr_wchan, ph->ph_page, 1513 ph, page); 1514 return 1; 1515 } 1516 1517 for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0; 1518 pi != NULL; 1519 pi = TAILQ_NEXT(pi,pi_list), n++) { 1520 1521 #ifdef DIAGNOSTIC 1522 if (pi->pi_magic != PI_MAGIC) { 1523 if (label != NULL) 1524 printf("%s: ", label); 1525 printf("pool(%s): free list modified: magic=%x;" 1526 " page %p; item ordinal %d;" 1527 " addr %p (p %p)\n", 1528 pp->pr_wchan, pi->pi_magic, ph->ph_page, 1529 n, pi, page); 1530 panic("pool"); 1531 } 1532 #endif 1533 page = 1534 (caddr_t)((u_long)pi & pp->pr_alloc->pa_pagemask); 1535 if (page == ph->ph_page) 1536 continue; 1537 1538 if (label != NULL) 1539 printf("%s: ", label); 1540 printf("pool(%p:%s): page inconsistency: page %p;" 1541 " item ordinal %d; addr %p (p %p)\n", pp, 1542 pp->pr_wchan, ph->ph_page, 1543 n, pi, page); 1544 return 1; 1545 } 1546 return 0; 1547 } 1548 1549 int 1550 pool_chk(struct pool *pp, const char *label) 1551 { 1552 struct pool_item_header *ph; 1553 int r = 0; 1554 1555 simple_lock(&pp->pr_slock); 1556 LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) { 1557 r = pool_chk_page(pp, label, ph); 1558 if (r) { 1559 goto out; 1560 } 1561 } 1562 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) { 1563 r = pool_chk_page(pp, label, ph); 1564 if (r) { 1565 goto out; 1566 } 1567 } 1568 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) { 1569 r = pool_chk_page(pp, label, ph); 1570 if (r) { 1571 goto out; 1572 } 1573 } 1574 1575 out: 1576 simple_unlock(&pp->pr_slock); 1577 return (r); 1578 } 1579 #endif 1580 1581 /* 1582 * pool_cache_init: 1583 * 1584 * Initialize a pool cache. 1585 * 1586 * NOTE: If the pool must be protected from interrupts, we expect 1587 * to be called at the appropriate interrupt priority level. 1588 */ 1589 void 1590 pool_cache_init(struct pool_cache *pc, struct pool *pp, 1591 int (*ctor)(void *, void *, int), 1592 void (*dtor)(void *, void *), 1593 void *arg) 1594 { 1595 1596 TAILQ_INIT(&pc->pc_grouplist); 1597 simple_lock_init(&pc->pc_slock); 1598 1599 pc->pc_allocfrom = NULL; 1600 pc->pc_freeto = NULL; 1601 pc->pc_pool = pp; 1602 1603 pc->pc_ctor = ctor; 1604 pc->pc_dtor = dtor; 1605 pc->pc_arg = arg; 1606 1607 pc->pc_hits = 0; 1608 pc->pc_misses = 0; 1609 1610 pc->pc_ngroups = 0; 1611 1612 pc->pc_nitems = 0; 1613 1614 simple_lock(&pp->pr_slock); 1615 TAILQ_INSERT_TAIL(&pp->pr_cachelist, pc, pc_poollist); 1616 simple_unlock(&pp->pr_slock); 1617 } 1618 1619 /* 1620 * pool_cache_destroy: 1621 * 1622 * Destroy a pool cache. 1623 */ 1624 void 1625 pool_cache_destroy(struct pool_cache *pc) 1626 { 1627 struct pool *pp = pc->pc_pool; 1628 1629 /* First, invalidate the entire cache. */ 1630 pool_cache_invalidate(pc); 1631 1632 /* ...and remove it from the pool's cache list. */ 1633 simple_lock(&pp->pr_slock); 1634 TAILQ_REMOVE(&pp->pr_cachelist, pc, pc_poollist); 1635 simple_unlock(&pp->pr_slock); 1636 } 1637 1638 static __inline void * 1639 pcg_get(struct pool_cache_group *pcg) 1640 { 1641 void *object; 1642 u_int idx; 1643 1644 KASSERT(pcg->pcg_avail <= PCG_NOBJECTS); 1645 KASSERT(pcg->pcg_avail != 0); 1646 idx = --pcg->pcg_avail; 1647 1648 KASSERT(pcg->pcg_objects[idx] != NULL); 1649 object = pcg->pcg_objects[idx]; 1650 pcg->pcg_objects[idx] = NULL; 1651 1652 return (object); 1653 } 1654 1655 static __inline void 1656 pcg_put(struct pool_cache_group *pcg, void *object) 1657 { 1658 u_int idx; 1659 1660 KASSERT(pcg->pcg_avail < PCG_NOBJECTS); 1661 idx = pcg->pcg_avail++; 1662 1663 KASSERT(pcg->pcg_objects[idx] == NULL); 1664 pcg->pcg_objects[idx] = object; 1665 } 1666 1667 /* 1668 * pool_cache_get: 1669 * 1670 * Get an object from a pool cache. 1671 */ 1672 void * 1673 pool_cache_get(struct pool_cache *pc, int flags) 1674 { 1675 struct pool_cache_group *pcg; 1676 void *object; 1677 1678 #ifdef LOCKDEBUG 1679 if (flags & PR_WAITOK) 1680 simple_lock_only_held(NULL, "pool_cache_get(PR_WAITOK)"); 1681 #endif 1682 1683 simple_lock(&pc->pc_slock); 1684 1685 if ((pcg = pc->pc_allocfrom) == NULL) { 1686 TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) { 1687 if (pcg->pcg_avail != 0) { 1688 pc->pc_allocfrom = pcg; 1689 goto have_group; 1690 } 1691 } 1692 1693 /* 1694 * No groups with any available objects. Allocate 1695 * a new object, construct it, and return it to 1696 * the caller. We will allocate a group, if necessary, 1697 * when the object is freed back to the cache. 1698 */ 1699 pc->pc_misses++; 1700 simple_unlock(&pc->pc_slock); 1701 object = pool_get(pc->pc_pool, flags); 1702 if (object != NULL && pc->pc_ctor != NULL) { 1703 if ((*pc->pc_ctor)(pc->pc_arg, object, flags) != 0) { 1704 pool_put(pc->pc_pool, object); 1705 return (NULL); 1706 } 1707 } 1708 return (object); 1709 } 1710 1711 have_group: 1712 pc->pc_hits++; 1713 pc->pc_nitems--; 1714 object = pcg_get(pcg); 1715 1716 if (pcg->pcg_avail == 0) 1717 pc->pc_allocfrom = NULL; 1718 1719 simple_unlock(&pc->pc_slock); 1720 1721 return (object); 1722 } 1723 1724 /* 1725 * pool_cache_put: 1726 * 1727 * Put an object back to the pool cache. 1728 */ 1729 void 1730 pool_cache_put(struct pool_cache *pc, void *object) 1731 { 1732 struct pool_cache_group *pcg; 1733 int s; 1734 1735 simple_lock(&pc->pc_slock); 1736 1737 if ((pcg = pc->pc_freeto) == NULL) { 1738 TAILQ_FOREACH(pcg, &pc->pc_grouplist, pcg_list) { 1739 if (pcg->pcg_avail != PCG_NOBJECTS) { 1740 pc->pc_freeto = pcg; 1741 goto have_group; 1742 } 1743 } 1744 1745 /* 1746 * No empty groups to free the object to. Attempt to 1747 * allocate one. 1748 */ 1749 simple_unlock(&pc->pc_slock); 1750 s = splvm(); 1751 pcg = pool_get(&pcgpool, PR_NOWAIT); 1752 splx(s); 1753 if (pcg != NULL) { 1754 memset(pcg, 0, sizeof(*pcg)); 1755 simple_lock(&pc->pc_slock); 1756 pc->pc_ngroups++; 1757 TAILQ_INSERT_TAIL(&pc->pc_grouplist, pcg, pcg_list); 1758 if (pc->pc_freeto == NULL) 1759 pc->pc_freeto = pcg; 1760 goto have_group; 1761 } 1762 1763 /* 1764 * Unable to allocate a cache group; destruct the object 1765 * and free it back to the pool. 1766 */ 1767 pool_cache_destruct_object(pc, object); 1768 return; 1769 } 1770 1771 have_group: 1772 pc->pc_nitems++; 1773 pcg_put(pcg, object); 1774 1775 if (pcg->pcg_avail == PCG_NOBJECTS) 1776 pc->pc_freeto = NULL; 1777 1778 simple_unlock(&pc->pc_slock); 1779 } 1780 1781 /* 1782 * pool_cache_destruct_object: 1783 * 1784 * Force destruction of an object and its release back into 1785 * the pool. 1786 */ 1787 void 1788 pool_cache_destruct_object(struct pool_cache *pc, void *object) 1789 { 1790 1791 if (pc->pc_dtor != NULL) 1792 (*pc->pc_dtor)(pc->pc_arg, object); 1793 pool_put(pc->pc_pool, object); 1794 } 1795 1796 /* 1797 * pool_cache_do_invalidate: 1798 * 1799 * This internal function implements pool_cache_invalidate() and 1800 * pool_cache_reclaim(). 1801 */ 1802 void 1803 pool_cache_do_invalidate(struct pool_cache *pc, int free_groups, 1804 void (*putit)(struct pool *, void *)) 1805 { 1806 struct pool_cache_group *pcg, *npcg; 1807 void *object; 1808 int s; 1809 1810 for (pcg = TAILQ_FIRST(&pc->pc_grouplist); pcg != NULL; 1811 pcg = npcg) { 1812 npcg = TAILQ_NEXT(pcg, pcg_list); 1813 while (pcg->pcg_avail != 0) { 1814 pc->pc_nitems--; 1815 object = pcg_get(pcg); 1816 if (pcg->pcg_avail == 0 && pc->pc_allocfrom == pcg) 1817 pc->pc_allocfrom = NULL; 1818 if (pc->pc_dtor != NULL) 1819 (*pc->pc_dtor)(pc->pc_arg, object); 1820 (*putit)(pc->pc_pool, object); 1821 } 1822 if (free_groups) { 1823 pc->pc_ngroups--; 1824 TAILQ_REMOVE(&pc->pc_grouplist, pcg, pcg_list); 1825 if (pc->pc_freeto == pcg) 1826 pc->pc_freeto = NULL; 1827 s = splvm(); 1828 pool_put(&pcgpool, pcg); 1829 splx(s); 1830 } 1831 } 1832 } 1833 1834 /* 1835 * pool_cache_invalidate: 1836 * 1837 * Invalidate a pool cache (destruct and release all of the 1838 * cached objects). 1839 */ 1840 void 1841 pool_cache_invalidate(struct pool_cache *pc) 1842 { 1843 1844 simple_lock(&pc->pc_slock); 1845 pool_cache_do_invalidate(pc, 0, pool_put); 1846 simple_unlock(&pc->pc_slock); 1847 } 1848 1849 /* 1850 * pool_cache_reclaim: 1851 * 1852 * Reclaim a pool cache for pool_reclaim(). 1853 */ 1854 void 1855 pool_cache_reclaim(struct pool_cache *pc) 1856 { 1857 1858 simple_lock(&pc->pc_slock); 1859 pool_cache_do_invalidate(pc, 1, pool_do_put); 1860 simple_unlock(&pc->pc_slock); 1861 } 1862 1863 /* 1864 * We have three different sysctls. 1865 * kern.pool.npools - the number of pools. 1866 * kern.pool.pool.<pool#> - the pool struct for the pool#. 1867 * kern.pool.name.<pool#> - the name for pool#.[6~ 1868 */ 1869 int 1870 sysctl_dopool(int *name, u_int namelen, char *where, size_t *sizep) 1871 { 1872 struct pool *pp, *foundpool = NULL; 1873 size_t buflen = where != NULL ? *sizep : 0; 1874 int npools = 0, s; 1875 unsigned int lookfor; 1876 size_t len; 1877 1878 switch (*name) { 1879 case KERN_POOL_NPOOLS: 1880 if (namelen != 1 || buflen != sizeof(int)) 1881 return (EINVAL); 1882 lookfor = 0; 1883 break; 1884 case KERN_POOL_NAME: 1885 if (namelen != 2 || buflen < 1) 1886 return (EINVAL); 1887 lookfor = name[1]; 1888 break; 1889 case KERN_POOL_POOL: 1890 if (namelen != 2 || buflen != sizeof(struct pool)) 1891 return (EINVAL); 1892 lookfor = name[1]; 1893 break; 1894 default: 1895 return (EINVAL); 1896 } 1897 1898 s = splvm(); 1899 simple_lock(&pool_head_slock); 1900 1901 TAILQ_FOREACH(pp, &pool_head, pr_poollist) { 1902 npools++; 1903 if (lookfor == pp->pr_serial) { 1904 foundpool = pp; 1905 break; 1906 } 1907 } 1908 1909 simple_unlock(&pool_head_slock); 1910 splx(s); 1911 1912 if (lookfor != 0 && foundpool == NULL) 1913 return (ENOENT); 1914 1915 switch (*name) { 1916 case KERN_POOL_NPOOLS: 1917 return copyout(&npools, where, buflen); 1918 case KERN_POOL_NAME: 1919 len = strlen(foundpool->pr_wchan) + 1; 1920 if (*sizep < len) 1921 return (ENOMEM); 1922 *sizep = len; 1923 return copyout(foundpool->pr_wchan, where, len); 1924 case KERN_POOL_POOL: 1925 return copyout(foundpool, where, buflen); 1926 } 1927 /* NOTREACHED */ 1928 return (0); /* XXX - Stupid gcc */ 1929 } 1930 1931 /* 1932 * Pool backend allocators. 1933 * 1934 * Each pool has a backend allocator that handles allocation, deallocation 1935 * and any additional draining that might be needed. 1936 */ 1937 void *pool_page_alloc_kmem(struct pool *, int); 1938 void pool_page_free_kmem(struct pool *, void *); 1939 void *pool_page_alloc_oldnointr(struct pool *, int); 1940 void pool_page_free_oldnointr(struct pool *, void *); 1941 void *pool_page_alloc(struct pool *, int); 1942 void pool_page_free(struct pool *, void *); 1943 1944 /* old default allocator, interrupt safe */ 1945 struct pool_allocator pool_allocator_kmem = { 1946 pool_page_alloc_kmem, pool_page_free_kmem, 0, 1947 }; 1948 /* previous nointr. handles large allocations safely */ 1949 struct pool_allocator pool_allocator_oldnointr = { 1950 pool_page_alloc_oldnointr, pool_page_free_oldnointr, 0, 1951 }; 1952 /* safe for interrupts, name preserved for compat 1953 * this is the default allocator */ 1954 struct pool_allocator pool_allocator_nointr = { 1955 pool_page_alloc, pool_page_free, 0, 1956 }; 1957 1958 /* 1959 * XXX - we have at least three different resources for the same allocation 1960 * and each resource can be depleted. First we have the ready elements in 1961 * the pool. Then we have the resource (typically a vm_map) for this 1962 * allocator, then we have physical memory. Waiting for any of these can 1963 * be unnecessary when any other is freed, but the kernel doesn't support 1964 * sleeping on multiple addresses, so we have to fake. The caller sleeps on 1965 * the pool (so that we can be awakened when an item is returned to the pool), 1966 * but we set PA_WANT on the allocator. When a page is returned to 1967 * the allocator and PA_WANT is set pool_allocator_free will wakeup all 1968 * sleeping pools belonging to this allocator. (XXX - thundering herd). 1969 * We also wake up the allocator in case someone without a pool (malloc) 1970 * is sleeping waiting for this allocator. 1971 */ 1972 1973 void * 1974 pool_allocator_alloc(struct pool *org, int flags) 1975 { 1976 struct pool_allocator *pa = org->pr_alloc; 1977 int freed; 1978 void *res; 1979 int s; 1980 1981 do { 1982 if ((res = (*pa->pa_alloc)(org, flags)) != NULL) 1983 return (res); 1984 if ((flags & PR_WAITOK) == 0) { 1985 /* 1986 * We only run the drain hook here if PR_NOWAIT. 1987 * In other cases the hook will be run in 1988 * pool_reclaim. 1989 */ 1990 if (org->pr_drain_hook != NULL) { 1991 (*org->pr_drain_hook)(org->pr_drain_hook_arg, 1992 flags); 1993 if ((res = (*pa->pa_alloc)(org, flags)) != NULL) 1994 return (res); 1995 } 1996 break; 1997 } 1998 s = splvm(); 1999 simple_lock(&pa->pa_slock); 2000 freed = pool_allocator_drain(pa, org, 1); 2001 simple_unlock(&pa->pa_slock); 2002 splx(s); 2003 } while (freed); 2004 return (NULL); 2005 } 2006 2007 void 2008 pool_allocator_free(struct pool *pp, void *v) 2009 { 2010 struct pool_allocator *pa = pp->pr_alloc; 2011 int s; 2012 2013 (*pa->pa_free)(pp, v); 2014 2015 s = splvm(); 2016 simple_lock(&pa->pa_slock); 2017 if ((pa->pa_flags & PA_WANT) == 0) { 2018 simple_unlock(&pa->pa_slock); 2019 splx(s); 2020 return; 2021 } 2022 2023 TAILQ_FOREACH(pp, &pa->pa_list, pr_alloc_list) { 2024 simple_lock(&pp->pr_slock); 2025 if ((pp->pr_flags & PR_WANTED) != 0) { 2026 pp->pr_flags &= ~PR_WANTED; 2027 wakeup(pp); 2028 } 2029 simple_unlock(&pp->pr_slock); 2030 } 2031 pa->pa_flags &= ~PA_WANT; 2032 simple_unlock(&pa->pa_slock); 2033 splx(s); 2034 } 2035 2036 /* 2037 * Drain all pools, except 'org', that use this allocator. 2038 * 2039 * Must be called at appropriate spl level and with the allocator locked. 2040 * 2041 * We do this to reclaim va space. pa_alloc is responsible 2042 * for waiting for physical memory. 2043 * XXX - we risk looping forever if start if someone calls 2044 * pool_destroy on 'start'. But there is no other way to 2045 * have potentially sleeping pool_reclaim, non-sleeping 2046 * locks on pool_allocator and some stirring of drained 2047 * pools in the allocator. 2048 * XXX - maybe we should use pool_head_slock for locking 2049 * the allocators? 2050 */ 2051 int 2052 pool_allocator_drain(struct pool_allocator *pa, struct pool *org, int need) 2053 { 2054 struct pool *pp, *start; 2055 int freed; 2056 2057 freed = 0; 2058 2059 pp = start = TAILQ_FIRST(&pa->pa_list); 2060 do { 2061 TAILQ_REMOVE(&pa->pa_list, pp, pr_alloc_list); 2062 TAILQ_INSERT_TAIL(&pa->pa_list, pp, pr_alloc_list); 2063 if (pp == org) 2064 continue; 2065 simple_unlock(&pa->pa_slock); 2066 freed = pool_reclaim(pp); 2067 simple_lock(&pa->pa_slock); 2068 } while ((pp = TAILQ_FIRST(&pa->pa_list)) != start && (freed < need)); 2069 2070 if (!freed) { 2071 /* 2072 * We set PA_WANT here, the caller will most likely 2073 * sleep waiting for pages (if not, this won't hurt 2074 * that much) and there is no way to set this in the 2075 * caller without violating locking order. 2076 */ 2077 pa->pa_flags |= PA_WANT; 2078 } 2079 2080 return (freed); 2081 } 2082 2083 void * 2084 pool_page_alloc(struct pool *pp, int flags) 2085 { 2086 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; 2087 2088 return (uvm_km_getpage(waitok)); 2089 } 2090 2091 void 2092 pool_page_free(struct pool *pp, void *v) 2093 { 2094 2095 uvm_km_putpage(v); 2096 } 2097 2098 void * 2099 pool_page_alloc_kmem(struct pool *pp, int flags) 2100 { 2101 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; 2102 2103 return ((void *)uvm_km_alloc_poolpage1(kmem_map, uvmexp.kmem_object, 2104 waitok)); 2105 } 2106 2107 void 2108 pool_page_free_kmem(struct pool *pp, void *v) 2109 { 2110 2111 uvm_km_free_poolpage1(kmem_map, (vaddr_t)v); 2112 } 2113 2114 void * 2115 pool_page_alloc_oldnointr(struct pool *pp, int flags) 2116 { 2117 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; 2118 2119 splassert(IPL_NONE); 2120 2121 return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object, 2122 waitok)); 2123 } 2124 2125 void 2126 pool_page_free_oldnointr(struct pool *pp, void *v) 2127 { 2128 splassert(IPL_NONE); 2129 2130 uvm_km_free_poolpage1(kernel_map, (vaddr_t)v); 2131 } 2132