1 /* $OpenBSD: subr_pool.c,v 1.79 2009/04/22 01:16:11 dlg 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 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/proc.h> 37 #include <sys/errno.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/pool.h> 41 #include <sys/syslog.h> 42 #include <sys/sysctl.h> 43 44 #include <uvm/uvm.h> 45 46 47 /* 48 * Pool resource management utility. 49 * 50 * Memory is allocated in pages which are split into pieces according to 51 * the pool item size. Each page is kept on one of three lists in the 52 * pool structure: `pr_emptypages', `pr_fullpages' and `pr_partpages', 53 * for empty, full and partially-full pages respectively. The individual 54 * pool items are on a linked list headed by `ph_itemlist' in each page 55 * header. The memory for building the page list is either taken from 56 * the allocated pages themselves (for small pool items) or taken from 57 * an internal pool of page headers (`phpool'). 58 */ 59 60 /* List of all pools */ 61 TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head); 62 63 /* Private pool for page header structures */ 64 struct pool phpool; 65 66 struct pool_item_header { 67 /* Page headers */ 68 LIST_ENTRY(pool_item_header) 69 ph_pagelist; /* pool page list */ 70 TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */ 71 SPLAY_ENTRY(pool_item_header) 72 ph_node; /* Off-page page headers */ 73 int ph_nmissing; /* # of chunks in use */ 74 caddr_t ph_page; /* this page's address */ 75 caddr_t ph_colored; /* page's colored address */ 76 int ph_pagesize; 77 }; 78 79 struct pool_item { 80 #ifdef DIAGNOSTIC 81 u_int32_t pi_magic; 82 #endif 83 /* Other entries use only this list entry */ 84 TAILQ_ENTRY(pool_item) pi_list; 85 }; 86 87 #ifdef DEADBEEF1 88 #define PI_MAGIC DEADBEEF1 89 #else 90 #define PI_MAGIC 0xdeafbeef 91 #endif 92 93 #define POOL_NEEDS_CATCHUP(pp) \ 94 ((pp)->pr_nitems < (pp)->pr_minitems) 95 96 /* 97 * Every pool gets a unique serial number assigned to it. If this counter 98 * wraps, we're screwed, but we shouldn't create so many pools anyway. 99 */ 100 unsigned int pool_serial; 101 102 int pool_catchup(struct pool *); 103 void pool_prime_page(struct pool *, caddr_t, struct pool_item_header *); 104 void pool_update_curpage(struct pool *); 105 void *pool_do_get(struct pool *, int); 106 void pool_do_put(struct pool *, void *); 107 void pr_rmpage(struct pool *, struct pool_item_header *, 108 struct pool_pagelist *); 109 int pool_chk_page(struct pool *, const char *, struct pool_item_header *); 110 struct pool_item_header *pool_alloc_item_header(struct pool *, caddr_t , int); 111 112 void *pool_allocator_alloc(struct pool *, int, int *); 113 void pool_allocator_free(struct pool *, void *); 114 115 /* 116 * XXX - quick hack. For pools with large items we want to use a special 117 * allocator. For now, instead of having the allocator figure out 118 * the allocation size from the pool (which can be done trivially 119 * with round_page(pr_itemsperpage * pr_size)) which would require 120 * lots of changes everywhere, we just create allocators for each 121 * size. We limit those to 128 pages. 122 */ 123 #define POOL_LARGE_MAXPAGES 128 124 struct pool_allocator pool_allocator_large[POOL_LARGE_MAXPAGES]; 125 struct pool_allocator pool_allocator_large_ni[POOL_LARGE_MAXPAGES]; 126 void *pool_large_alloc(struct pool *, int, int *); 127 void pool_large_free(struct pool *, void *); 128 void *pool_large_alloc_ni(struct pool *, int, int *); 129 void pool_large_free_ni(struct pool *, void *); 130 131 132 #ifdef DDB 133 void pool_print_pagelist(struct pool_pagelist *, 134 int (*)(const char *, ...)); 135 void pool_print1(struct pool *, const char *, int (*)(const char *, ...)); 136 #endif 137 138 #define pool_sleep(pl) msleep(pl, &pl->pr_mtx, PSWP, pl->pr_wchan, 0) 139 140 static __inline int 141 phtree_compare(struct pool_item_header *a, struct pool_item_header *b) 142 { 143 long diff = (vaddr_t)a->ph_page - (vaddr_t)b->ph_page; 144 if (diff < 0) 145 return -(-diff >= a->ph_pagesize); 146 else if (diff > 0) 147 return (diff >= b->ph_pagesize); 148 else 149 return (0); 150 } 151 152 SPLAY_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare); 153 SPLAY_GENERATE(phtree, pool_item_header, ph_node, phtree_compare); 154 155 /* 156 * Return the pool page header based on page address. 157 */ 158 static __inline struct pool_item_header * 159 pr_find_pagehead(struct pool *pp, void *v) 160 { 161 struct pool_item_header *ph, tmp; 162 163 if ((pp->pr_roflags & PR_PHINPAGE) != 0) { 164 caddr_t page; 165 166 page = (caddr_t)((vaddr_t)v & pp->pr_alloc->pa_pagemask); 167 168 return ((struct pool_item_header *)(page + pp->pr_phoffset)); 169 } 170 171 /* 172 * The trick we're using in the tree compare function is to compare 173 * two elements equal when they overlap. We want to return the 174 * page header that belongs to the element just before this address. 175 * We don't want this element to compare equal to the next element, 176 * so the compare function takes the pagesize from the lower element. 177 * If this header is the lower, its pagesize is zero, so it can't 178 * overlap with the next header. But if the header we're looking for 179 * is lower, we'll use its pagesize and it will overlap and return 180 * equal. 181 */ 182 tmp.ph_page = v; 183 tmp.ph_pagesize = 0; 184 ph = SPLAY_FIND(phtree, &pp->pr_phtree, &tmp); 185 186 if (ph) { 187 KASSERT(ph->ph_page <= (caddr_t)v); 188 KASSERT(ph->ph_page + ph->ph_pagesize > (caddr_t)v); 189 } 190 return ph; 191 } 192 193 /* 194 * Remove a page from the pool. 195 */ 196 void 197 pr_rmpage(struct pool *pp, struct pool_item_header *ph, 198 struct pool_pagelist *pq) 199 { 200 201 /* 202 * If the page was idle, decrement the idle page count. 203 */ 204 if (ph->ph_nmissing == 0) { 205 #ifdef DIAGNOSTIC 206 if (pp->pr_nidle == 0) 207 panic("pr_rmpage: nidle inconsistent"); 208 if (pp->pr_nitems < pp->pr_itemsperpage) 209 panic("pr_rmpage: nitems inconsistent"); 210 #endif 211 pp->pr_nidle--; 212 } 213 214 pp->pr_nitems -= pp->pr_itemsperpage; 215 216 /* 217 * Unlink a page from the pool and release it (or queue it for release). 218 */ 219 LIST_REMOVE(ph, ph_pagelist); 220 if ((pp->pr_roflags & PR_PHINPAGE) == 0) 221 SPLAY_REMOVE(phtree, &pp->pr_phtree, ph); 222 if (pq) { 223 LIST_INSERT_HEAD(pq, ph, ph_pagelist); 224 } else { 225 pool_allocator_free(pp, ph->ph_page); 226 if ((pp->pr_roflags & PR_PHINPAGE) == 0) 227 pool_put(&phpool, ph); 228 } 229 pp->pr_npages--; 230 pp->pr_npagefree++; 231 232 pool_update_curpage(pp); 233 } 234 235 /* 236 * Initialize the given pool resource structure. 237 * 238 * We export this routine to allow other kernel parts to declare 239 * static pools that must be initialized before malloc() is available. 240 */ 241 void 242 pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags, 243 const char *wchan, struct pool_allocator *palloc) 244 { 245 int off, slack; 246 247 #ifdef MALLOC_DEBUG 248 if ((flags & PR_DEBUG) && (ioff != 0 || align != 0)) 249 flags &= ~PR_DEBUG; 250 #endif 251 /* 252 * Check arguments and construct default values. 253 */ 254 if (palloc == NULL) { 255 if (size > PAGE_SIZE) { 256 int psize; 257 258 /* 259 * XXX - should take align into account as well. 260 */ 261 if (size == round_page(size)) 262 psize = size / PAGE_SIZE; 263 else 264 psize = PAGE_SIZE / roundup(size % PAGE_SIZE, 265 1024); 266 if (psize > POOL_LARGE_MAXPAGES) 267 psize = POOL_LARGE_MAXPAGES; 268 if (flags & PR_WAITOK) 269 palloc = &pool_allocator_large_ni[psize-1]; 270 else 271 palloc = &pool_allocator_large[psize-1]; 272 if (palloc->pa_pagesz == 0) { 273 palloc->pa_pagesz = psize * PAGE_SIZE; 274 if (flags & PR_WAITOK) { 275 palloc->pa_alloc = pool_large_alloc_ni; 276 palloc->pa_free = pool_large_free_ni; 277 } else { 278 palloc->pa_alloc = pool_large_alloc; 279 palloc->pa_free = pool_large_free; 280 } 281 } 282 } else { 283 palloc = &pool_allocator_nointr; 284 } 285 } 286 if (palloc->pa_pagesz == 0) { 287 palloc->pa_pagesz = PAGE_SIZE; 288 } 289 if (palloc->pa_pagemask == 0) { 290 palloc->pa_pagemask = ~(palloc->pa_pagesz - 1); 291 palloc->pa_pageshift = ffs(palloc->pa_pagesz) - 1; 292 } 293 294 if (align == 0) 295 align = ALIGN(1); 296 297 if (size < sizeof(struct pool_item)) 298 size = sizeof(struct pool_item); 299 300 size = roundup(size, align); 301 #ifdef DIAGNOSTIC 302 if (size > palloc->pa_pagesz) 303 panic("pool_init: pool item size (%lu) too large", 304 (u_long)size); 305 #endif 306 307 /* 308 * Initialize the pool structure. 309 */ 310 LIST_INIT(&pp->pr_emptypages); 311 LIST_INIT(&pp->pr_fullpages); 312 LIST_INIT(&pp->pr_partpages); 313 pp->pr_curpage = NULL; 314 pp->pr_npages = 0; 315 pp->pr_minitems = 0; 316 pp->pr_minpages = 0; 317 pp->pr_maxpages = 8; 318 pp->pr_roflags = flags; 319 pp->pr_flags = 0; 320 pp->pr_size = size; 321 pp->pr_align = align; 322 pp->pr_wchan = wchan; 323 pp->pr_alloc = palloc; 324 pp->pr_nitems = 0; 325 pp->pr_nout = 0; 326 pp->pr_hardlimit = UINT_MAX; 327 pp->pr_hardlimit_warning = NULL; 328 pp->pr_hardlimit_ratecap.tv_sec = 0; 329 pp->pr_hardlimit_ratecap.tv_usec = 0; 330 pp->pr_hardlimit_warning_last.tv_sec = 0; 331 pp->pr_hardlimit_warning_last.tv_usec = 0; 332 pp->pr_serial = ++pool_serial; 333 if (pool_serial == 0) 334 panic("pool_init: too much uptime"); 335 336 /* constructor, destructor, and arg */ 337 pp->pr_ctor = NULL; 338 pp->pr_dtor = NULL; 339 pp->pr_arg = NULL; 340 341 /* 342 * Decide whether to put the page header off page to avoid 343 * wasting too large a part of the page. Off-page page headers 344 * go on a hash table, so we can match a returned item 345 * with its header based on the page address. 346 * We use 1/16 of the page size as the threshold (XXX: tune) 347 */ 348 if (pp->pr_size < palloc->pa_pagesz/16 && pp->pr_size < PAGE_SIZE) { 349 /* Use the end of the page for the page header */ 350 pp->pr_roflags |= PR_PHINPAGE; 351 pp->pr_phoffset = off = palloc->pa_pagesz - 352 ALIGN(sizeof(struct pool_item_header)); 353 } else { 354 /* The page header will be taken from our page header pool */ 355 pp->pr_phoffset = 0; 356 off = palloc->pa_pagesz; 357 SPLAY_INIT(&pp->pr_phtree); 358 } 359 360 /* 361 * Alignment is to take place at `ioff' within the item. This means 362 * we must reserve up to `align - 1' bytes on the page to allow 363 * appropriate positioning of each item. 364 * 365 * Silently enforce `0 <= ioff < align'. 366 */ 367 pp->pr_itemoffset = ioff = ioff % align; 368 pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size; 369 KASSERT(pp->pr_itemsperpage != 0); 370 371 /* 372 * Use the slack between the chunks and the page header 373 * for "cache coloring". 374 */ 375 slack = off - pp->pr_itemsperpage * pp->pr_size; 376 pp->pr_maxcolor = (slack / align) * align; 377 pp->pr_curcolor = 0; 378 379 pp->pr_nget = 0; 380 pp->pr_nfail = 0; 381 pp->pr_nput = 0; 382 pp->pr_npagealloc = 0; 383 pp->pr_npagefree = 0; 384 pp->pr_hiwat = 0; 385 pp->pr_nidle = 0; 386 387 pp->pr_ipl = -1; 388 mtx_init(&pp->pr_mtx, IPL_NONE); 389 390 if (phpool.pr_size == 0) { 391 pool_init(&phpool, sizeof(struct pool_item_header), 0, 0, 392 0, "phpool", NULL); 393 pool_setipl(&phpool, IPL_HIGH); 394 } 395 396 /* Insert this into the list of all pools. */ 397 TAILQ_INSERT_HEAD(&pool_head, pp, pr_poollist); 398 } 399 400 void 401 pool_setipl(struct pool *pp, int ipl) 402 { 403 pp->pr_ipl = ipl; 404 mtx_init(&pp->pr_mtx, ipl); 405 } 406 407 /* 408 * Decommission a pool resource. 409 */ 410 void 411 pool_destroy(struct pool *pp) 412 { 413 struct pool_item_header *ph; 414 415 #ifdef DIAGNOSTIC 416 if (pp->pr_nout != 0) 417 panic("pool_destroy: pool busy: still out: %u", pp->pr_nout); 418 #endif 419 420 /* Remove all pages */ 421 while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) 422 pr_rmpage(pp, ph, NULL); 423 KASSERT(LIST_EMPTY(&pp->pr_fullpages)); 424 KASSERT(LIST_EMPTY(&pp->pr_partpages)); 425 426 /* Remove from global pool list */ 427 TAILQ_REMOVE(&pool_head, pp, pr_poollist); 428 } 429 430 struct pool_item_header * 431 pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags) 432 { 433 struct pool_item_header *ph; 434 435 if ((pp->pr_roflags & PR_PHINPAGE) != 0) 436 ph = (struct pool_item_header *)(storage + pp->pr_phoffset); 437 else { 438 ph = pool_get(&phpool, flags); 439 } 440 441 return (ph); 442 } 443 444 /* 445 * Grab an item from the pool; must be called at appropriate spl level 446 */ 447 void * 448 pool_get(struct pool *pp, int flags) 449 { 450 void *v; 451 452 mtx_enter(&pp->pr_mtx); 453 v = pool_do_get(pp, flags); 454 mtx_leave(&pp->pr_mtx); 455 if (v && pp->pr_ctor && pp->pr_ctor(pp->pr_arg, v, flags)) { 456 mtx_enter(&pp->pr_mtx); 457 pool_do_put(pp, v); 458 mtx_leave(&pp->pr_mtx); 459 v = NULL; 460 } 461 if (v) { 462 pp->pr_nget++; 463 if (flags & PR_ZERO) 464 memset(v, 0, pp->pr_size); 465 } 466 return (v); 467 } 468 469 void * 470 pool_do_get(struct pool *pp, int flags) 471 { 472 struct pool_item *pi; 473 struct pool_item_header *ph; 474 void *v; 475 int slowdown = 0; 476 #ifdef POOL_DEBUG 477 int i, *ip; 478 #endif 479 480 #ifdef DIAGNOSTIC 481 if ((flags & PR_WAITOK) != 0) 482 splassert(IPL_NONE); 483 if (pp->pr_ipl != -1) 484 splassert(pp->pr_ipl); 485 #endif /* DIAGNOSTIC */ 486 487 #ifdef MALLOC_DEBUG 488 if (pp->pr_roflags & PR_DEBUG) { 489 void *addr; 490 491 addr = NULL; 492 debug_malloc(pp->pr_size, M_DEBUG, 493 (flags & PR_WAITOK) ? M_WAITOK : M_NOWAIT, &addr); 494 return (addr); 495 } 496 #endif 497 498 startover: 499 /* 500 * Check to see if we've reached the hard limit. If we have, 501 * and we can wait, then wait until an item has been returned to 502 * the pool. 503 */ 504 #ifdef DIAGNOSTIC 505 if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) 506 panic("pool_do_get: %s: crossed hard limit", pp->pr_wchan); 507 #endif 508 if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) { 509 if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) { 510 /* 511 * XXX: A warning isn't logged in this case. Should 512 * it be? 513 */ 514 pp->pr_flags |= PR_WANTED; 515 pool_sleep(pp); 516 goto startover; 517 } 518 519 /* 520 * Log a message that the hard limit has been hit. 521 */ 522 if (pp->pr_hardlimit_warning != NULL && 523 ratecheck(&pp->pr_hardlimit_warning_last, 524 &pp->pr_hardlimit_ratecap)) 525 log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning); 526 527 pp->pr_nfail++; 528 return (NULL); 529 } 530 531 /* 532 * The convention we use is that if `curpage' is not NULL, then 533 * it points at a non-empty bucket. In particular, `curpage' 534 * never points at a page header which has PR_PHINPAGE set and 535 * has no items in its bucket. 536 */ 537 if ((ph = pp->pr_curpage) == NULL) { 538 #ifdef DIAGNOSTIC 539 if (pp->pr_nitems != 0) { 540 printf("pool_do_get: %s: curpage NULL, nitems %u\n", 541 pp->pr_wchan, pp->pr_nitems); 542 panic("pool_do_get: nitems inconsistent"); 543 } 544 #endif 545 546 /* 547 * Call the back-end page allocator for more memory. 548 */ 549 v = pool_allocator_alloc(pp, flags, &slowdown); 550 if (__predict_true(v != NULL)) 551 ph = pool_alloc_item_header(pp, v, flags); 552 553 if (__predict_false(v == NULL || ph == NULL)) { 554 if (v != NULL) 555 pool_allocator_free(pp, v); 556 557 if ((flags & PR_WAITOK) == 0) { 558 pp->pr_nfail++; 559 return (NULL); 560 } 561 562 /* 563 * Wait for items to be returned to this pool. 564 * 565 * XXX: maybe we should wake up once a second and 566 * try again? 567 */ 568 pp->pr_flags |= PR_WANTED; 569 pool_sleep(pp); 570 goto startover; 571 } 572 573 /* We have more memory; add it to the pool */ 574 pool_prime_page(pp, v, ph); 575 pp->pr_npagealloc++; 576 577 if (slowdown && (flags & PR_WAITOK)) { 578 mtx_leave(&pp->pr_mtx); 579 yield(); 580 mtx_enter(&pp->pr_mtx); 581 } 582 583 /* Start the allocation process over. */ 584 goto startover; 585 } 586 if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) { 587 panic("pool_do_get: %s: page empty", pp->pr_wchan); 588 } 589 #ifdef DIAGNOSTIC 590 if (__predict_false(pp->pr_nitems == 0)) { 591 printf("pool_do_get: %s: items on itemlist, nitems %u\n", 592 pp->pr_wchan, pp->pr_nitems); 593 panic("pool_do_get: nitems inconsistent"); 594 } 595 #endif 596 597 #ifdef DIAGNOSTIC 598 if (__predict_false(pi->pi_magic != PI_MAGIC)) 599 panic("pool_do_get(%s): free list modified: " 600 "page %p; item addr %p; offset 0x%x=0x%x", 601 pp->pr_wchan, ph->ph_page, pi, 0, pi->pi_magic); 602 #ifdef POOL_DEBUG 603 for (ip = (int *)pi, i = sizeof(*pi) / sizeof(int); 604 i < pp->pr_size / sizeof(int); i++) { 605 if (ip[i] != PI_MAGIC) { 606 panic("pool_do_get(%s): free list modified: " 607 "page %p; item addr %p; offset 0x%x=0x%x", 608 pp->pr_wchan, ph->ph_page, pi, 609 i * sizeof(int), ip[i]); 610 } 611 } 612 #endif /* POOL_DEBUG */ 613 #endif /* DIAGNOSTIC */ 614 615 /* 616 * Remove from item list. 617 */ 618 TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list); 619 pp->pr_nitems--; 620 pp->pr_nout++; 621 if (ph->ph_nmissing == 0) { 622 #ifdef DIAGNOSTIC 623 if (__predict_false(pp->pr_nidle == 0)) 624 panic("pool_do_get: nidle inconsistent"); 625 #endif 626 pp->pr_nidle--; 627 628 /* 629 * This page was previously empty. Move it to the list of 630 * partially-full pages. This page is already curpage. 631 */ 632 LIST_REMOVE(ph, ph_pagelist); 633 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); 634 } 635 ph->ph_nmissing++; 636 if (TAILQ_EMPTY(&ph->ph_itemlist)) { 637 #ifdef DIAGNOSTIC 638 if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) { 639 panic("pool_do_get: %s: nmissing inconsistent", 640 pp->pr_wchan); 641 } 642 #endif 643 /* 644 * This page is now full. Move it to the full list 645 * and select a new current page. 646 */ 647 LIST_REMOVE(ph, ph_pagelist); 648 LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist); 649 pool_update_curpage(pp); 650 } 651 652 /* 653 * If we have a low water mark and we are now below that low 654 * water mark, add more items to the pool. 655 */ 656 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) { 657 /* 658 * XXX: Should we log a warning? Should we set up a timeout 659 * to try again in a second or so? The latter could break 660 * a caller's assumptions about interrupt protection, etc. 661 */ 662 } 663 return (v); 664 } 665 666 /* 667 * Return resource to the pool; must be called at appropriate spl level 668 */ 669 void 670 pool_put(struct pool *pp, void *v) 671 { 672 if (pp->pr_dtor) 673 pp->pr_dtor(pp->pr_arg, v); 674 mtx_enter(&pp->pr_mtx); 675 pool_do_put(pp, v); 676 mtx_leave(&pp->pr_mtx); 677 pp->pr_nput++; 678 } 679 680 /* 681 * Internal version of pool_put(). 682 */ 683 void 684 pool_do_put(struct pool *pp, void *v) 685 { 686 struct pool_item *pi = v; 687 struct pool_item_header *ph; 688 #ifdef POOL_DEBUG 689 int i, *ip; 690 #endif 691 692 if (v == NULL) 693 panic("pool_put of NULL"); 694 695 #ifdef MALLOC_DEBUG 696 if (pp->pr_roflags & PR_DEBUG) { 697 debug_free(v, M_DEBUG); 698 return; 699 } 700 #endif 701 702 #ifdef DIAGNOSTIC 703 if (pp->pr_ipl != -1) 704 splassert(pp->pr_ipl); 705 706 if (__predict_false(pp->pr_nout == 0)) { 707 printf("pool %s: putting with none out\n", 708 pp->pr_wchan); 709 panic("pool_do_put"); 710 } 711 #endif 712 713 if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) { 714 panic("pool_do_put: %s: page header missing", pp->pr_wchan); 715 } 716 717 /* 718 * Return to item list. 719 */ 720 #ifdef DIAGNOSTIC 721 pi->pi_magic = PI_MAGIC; 722 #ifdef POOL_DEBUG 723 for (ip = (int *)pi, i = sizeof(*pi)/sizeof(int); 724 i < pp->pr_size / sizeof(int); i++) 725 ip[i] = PI_MAGIC; 726 #endif /* POOL_DEBUG */ 727 #endif /* DIAGNOSTIC */ 728 729 TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list); 730 ph->ph_nmissing--; 731 pp->pr_nitems++; 732 pp->pr_nout--; 733 734 /* Cancel "pool empty" condition if it exists */ 735 if (pp->pr_curpage == NULL) 736 pp->pr_curpage = ph; 737 738 if (pp->pr_flags & PR_WANTED) { 739 pp->pr_flags &= ~PR_WANTED; 740 if (ph->ph_nmissing == 0) 741 pp->pr_nidle++; 742 wakeup(pp); 743 return; 744 } 745 746 /* 747 * If this page is now empty, do one of two things: 748 * 749 * (1) If we have more pages than the page high water mark, 750 * free the page back to the system. 751 * 752 * (2) Otherwise, move the page to the empty page list. 753 * 754 * Either way, select a new current page (so we use a partially-full 755 * page if one is available). 756 */ 757 if (ph->ph_nmissing == 0) { 758 pp->pr_nidle++; 759 if (pp->pr_nidle > pp->pr_maxpages) { 760 pr_rmpage(pp, ph, NULL); 761 } else { 762 LIST_REMOVE(ph, ph_pagelist); 763 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); 764 } 765 pool_update_curpage(pp); 766 } 767 768 /* 769 * If the page was previously completely full, move it to the 770 * partially-full list and make it the current page. The next 771 * allocation will get the item from this page, instead of 772 * further fragmenting the pool. 773 */ 774 else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) { 775 LIST_REMOVE(ph, ph_pagelist); 776 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); 777 pp->pr_curpage = ph; 778 } 779 } 780 781 /* 782 * Add N items to the pool. 783 */ 784 int 785 pool_prime(struct pool *pp, int n) 786 { 787 struct pool_item_header *ph; 788 caddr_t cp; 789 int newpages; 790 int slowdown; 791 792 mtx_enter(&pp->pr_mtx); 793 newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 794 795 while (newpages-- > 0) { 796 cp = pool_allocator_alloc(pp, PR_NOWAIT, &slowdown); 797 if (__predict_true(cp != NULL)) 798 ph = pool_alloc_item_header(pp, cp, PR_NOWAIT); 799 if (__predict_false(cp == NULL || ph == NULL)) { 800 if (cp != NULL) 801 pool_allocator_free(pp, cp); 802 break; 803 } 804 805 pool_prime_page(pp, cp, ph); 806 pp->pr_npagealloc++; 807 pp->pr_minpages++; 808 } 809 810 if (pp->pr_minpages >= pp->pr_maxpages) 811 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */ 812 813 mtx_leave(&pp->pr_mtx); 814 return (0); 815 } 816 817 /* 818 * Add a page worth of items to the pool. 819 * 820 * Note, we must be called with the pool descriptor LOCKED. 821 */ 822 void 823 pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph) 824 { 825 struct pool_item *pi; 826 caddr_t cp = storage; 827 unsigned int align = pp->pr_align; 828 unsigned int ioff = pp->pr_itemoffset; 829 int n; 830 #ifdef POOL_DEBUG 831 int i, *ip; 832 #endif 833 834 /* 835 * Insert page header. 836 */ 837 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); 838 TAILQ_INIT(&ph->ph_itemlist); 839 ph->ph_page = storage; 840 ph->ph_pagesize = pp->pr_alloc->pa_pagesz; 841 ph->ph_nmissing = 0; 842 if ((pp->pr_roflags & PR_PHINPAGE) == 0) 843 SPLAY_INSERT(phtree, &pp->pr_phtree, ph); 844 845 pp->pr_nidle++; 846 847 /* 848 * Color this page. 849 */ 850 cp = (caddr_t)(cp + pp->pr_curcolor); 851 if ((pp->pr_curcolor += align) > pp->pr_maxcolor) 852 pp->pr_curcolor = 0; 853 854 /* 855 * Adjust storage to apply aligment to `pr_itemoffset' in each item. 856 */ 857 if (ioff != 0) 858 cp = (caddr_t)(cp + (align - ioff)); 859 ph->ph_colored = cp; 860 861 /* 862 * Insert remaining chunks on the bucket list. 863 */ 864 n = pp->pr_itemsperpage; 865 pp->pr_nitems += n; 866 867 while (n--) { 868 pi = (struct pool_item *)cp; 869 870 KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0); 871 872 /* Insert on page list */ 873 TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list); 874 875 #ifdef DIAGNOSTIC 876 pi->pi_magic = PI_MAGIC; 877 #ifdef POOL_DEBUG 878 for (ip = (int *)pi, i = sizeof(*pi)/sizeof(int); 879 i < pp->pr_size / sizeof(int); i++) 880 ip[i] = PI_MAGIC; 881 #endif /* POOL_DEBUG */ 882 #endif /* DIAGNOSTIC */ 883 cp = (caddr_t)(cp + pp->pr_size); 884 } 885 886 /* 887 * If the pool was depleted, point at the new page. 888 */ 889 if (pp->pr_curpage == NULL) 890 pp->pr_curpage = ph; 891 892 if (++pp->pr_npages > pp->pr_hiwat) 893 pp->pr_hiwat = pp->pr_npages; 894 } 895 896 /* 897 * Used by pool_get() when nitems drops below the low water mark. This 898 * is used to catch up pr_nitems with the low water mark. 899 * 900 * Note we never wait for memory here, we let the caller decide what to do. 901 */ 902 int 903 pool_catchup(struct pool *pp) 904 { 905 struct pool_item_header *ph; 906 caddr_t cp; 907 int error = 0; 908 int slowdown; 909 910 while (POOL_NEEDS_CATCHUP(pp)) { 911 /* 912 * Call the page back-end allocator for more memory. 913 */ 914 cp = pool_allocator_alloc(pp, PR_NOWAIT, &slowdown); 915 if (__predict_true(cp != NULL)) 916 ph = pool_alloc_item_header(pp, cp, PR_NOWAIT); 917 if (__predict_false(cp == NULL || ph == NULL)) { 918 if (cp != NULL) 919 pool_allocator_free(pp, cp); 920 error = ENOMEM; 921 break; 922 } 923 pool_prime_page(pp, cp, ph); 924 pp->pr_npagealloc++; 925 } 926 927 return (error); 928 } 929 930 void 931 pool_update_curpage(struct pool *pp) 932 { 933 934 pp->pr_curpage = LIST_FIRST(&pp->pr_partpages); 935 if (pp->pr_curpage == NULL) { 936 pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages); 937 } 938 } 939 940 void 941 pool_setlowat(struct pool *pp, int n) 942 { 943 944 pp->pr_minitems = n; 945 pp->pr_minpages = (n == 0) 946 ? 0 947 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 948 949 mtx_enter(&pp->pr_mtx); 950 /* Make sure we're caught up with the newly-set low water mark. */ 951 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) { 952 /* 953 * XXX: Should we log a warning? Should we set up a timeout 954 * to try again in a second or so? The latter could break 955 * a caller's assumptions about interrupt protection, etc. 956 */ 957 } 958 mtx_leave(&pp->pr_mtx); 959 } 960 961 void 962 pool_sethiwat(struct pool *pp, int n) 963 { 964 965 pp->pr_maxpages = (n == 0) 966 ? 0 967 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 968 } 969 970 int 971 pool_sethardlimit(struct pool *pp, u_int n, const char *warnmsg, int ratecap) 972 { 973 int error = 0; 974 975 if (n < pp->pr_nout) { 976 error = EINVAL; 977 goto done; 978 } 979 980 pp->pr_hardlimit = n; 981 pp->pr_hardlimit_warning = warnmsg; 982 pp->pr_hardlimit_ratecap.tv_sec = ratecap; 983 pp->pr_hardlimit_warning_last.tv_sec = 0; 984 pp->pr_hardlimit_warning_last.tv_usec = 0; 985 986 /* 987 * In-line version of pool_sethiwat(). 988 */ 989 pp->pr_maxpages = (n == 0 || n == UINT_MAX) 990 ? n 991 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 992 993 done: 994 return (error); 995 } 996 997 void 998 pool_set_ctordtor(struct pool *pp, int (*ctor)(void *, void *, int), 999 void (*dtor)(void *, void *), void *arg) 1000 { 1001 pp->pr_ctor = ctor; 1002 pp->pr_dtor = dtor; 1003 pp->pr_arg = arg; 1004 } 1005 /* 1006 * Release all complete pages that have not been used recently. 1007 * 1008 * Returns non-zero if any pages have been reclaimed. 1009 */ 1010 int 1011 pool_reclaim(struct pool *pp) 1012 { 1013 struct pool_item_header *ph, *phnext; 1014 struct pool_pagelist pq; 1015 1016 LIST_INIT(&pq); 1017 1018 mtx_enter(&pp->pr_mtx); 1019 for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) { 1020 phnext = LIST_NEXT(ph, ph_pagelist); 1021 1022 /* Check our minimum page claim */ 1023 if (pp->pr_npages <= pp->pr_minpages) 1024 break; 1025 1026 KASSERT(ph->ph_nmissing == 0); 1027 1028 /* 1029 * If freeing this page would put us below 1030 * the low water mark, stop now. 1031 */ 1032 if ((pp->pr_nitems - pp->pr_itemsperpage) < 1033 pp->pr_minitems) 1034 break; 1035 1036 pr_rmpage(pp, ph, &pq); 1037 } 1038 mtx_leave(&pp->pr_mtx); 1039 1040 if (LIST_EMPTY(&pq)) 1041 return (0); 1042 while ((ph = LIST_FIRST(&pq)) != NULL) { 1043 LIST_REMOVE(ph, ph_pagelist); 1044 pool_allocator_free(pp, ph->ph_page); 1045 if (pp->pr_roflags & PR_PHINPAGE) 1046 continue; 1047 pool_put(&phpool, ph); 1048 } 1049 1050 return (1); 1051 } 1052 1053 #ifdef DDB 1054 #include <machine/db_machdep.h> 1055 #include <ddb/db_interface.h> 1056 #include <ddb/db_output.h> 1057 1058 /* 1059 * Diagnostic helpers. 1060 */ 1061 void 1062 pool_printit(struct pool *pp, const char *modif, int (*pr)(const char *, ...)) 1063 { 1064 pool_print1(pp, modif, pr); 1065 } 1066 1067 void 1068 pool_print_pagelist(struct pool_pagelist *pl, int (*pr)(const char *, ...)) 1069 { 1070 struct pool_item_header *ph; 1071 #ifdef DIAGNOSTIC 1072 struct pool_item *pi; 1073 #endif 1074 1075 LIST_FOREACH(ph, pl, ph_pagelist) { 1076 (*pr)("\t\tpage %p, nmissing %d\n", 1077 ph->ph_page, ph->ph_nmissing); 1078 #ifdef DIAGNOSTIC 1079 TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { 1080 if (pi->pi_magic != PI_MAGIC) { 1081 (*pr)("\t\t\titem %p, magic 0x%x\n", 1082 pi, pi->pi_magic); 1083 } 1084 } 1085 #endif 1086 } 1087 } 1088 1089 void 1090 pool_print1(struct pool *pp, const char *modif, int (*pr)(const char *, ...)) 1091 { 1092 struct pool_item_header *ph; 1093 int print_pagelist = 0; 1094 char c; 1095 1096 while ((c = *modif++) != '\0') { 1097 if (c == 'p') 1098 print_pagelist = 1; 1099 modif++; 1100 } 1101 1102 (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n", 1103 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset, 1104 pp->pr_roflags); 1105 (*pr)("\talloc %p\n", pp->pr_alloc); 1106 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n", 1107 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages); 1108 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n", 1109 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit); 1110 1111 (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n", 1112 pp->pr_nget, pp->pr_nfail, pp->pr_nput); 1113 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n", 1114 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle); 1115 1116 if (print_pagelist == 0) 1117 return; 1118 1119 if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) 1120 (*pr)("\n\tempty page list:\n"); 1121 pool_print_pagelist(&pp->pr_emptypages, pr); 1122 if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL) 1123 (*pr)("\n\tfull page list:\n"); 1124 pool_print_pagelist(&pp->pr_fullpages, pr); 1125 if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL) 1126 (*pr)("\n\tpartial-page list:\n"); 1127 pool_print_pagelist(&pp->pr_partpages, pr); 1128 1129 if (pp->pr_curpage == NULL) 1130 (*pr)("\tno current page\n"); 1131 else 1132 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page); 1133 } 1134 1135 void 1136 db_show_all_pools(db_expr_t expr, int haddr, db_expr_t count, char *modif) 1137 { 1138 struct pool *pp; 1139 char maxp[16]; 1140 int ovflw; 1141 char mode; 1142 1143 mode = modif[0]; 1144 if (mode != '\0' && mode != 'a') { 1145 db_printf("usage: show all pools [/a]\n"); 1146 return; 1147 } 1148 1149 if (mode == '\0') 1150 db_printf("%-10s%4s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n", 1151 "Name", 1152 "Size", 1153 "Requests", 1154 "Fail", 1155 "Releases", 1156 "Pgreq", 1157 "Pgrel", 1158 "Npage", 1159 "Hiwat", 1160 "Minpg", 1161 "Maxpg", 1162 "Idle"); 1163 else 1164 db_printf("%-10s %18s %18s\n", 1165 "Name", "Address", "Allocator"); 1166 1167 TAILQ_FOREACH(pp, &pool_head, pr_poollist) { 1168 if (mode == 'a') { 1169 db_printf("%-10s %18p %18p\n", pp->pr_wchan, pp, 1170 pp->pr_alloc); 1171 continue; 1172 } 1173 1174 if (!pp->pr_nget) 1175 continue; 1176 1177 if (pp->pr_maxpages == UINT_MAX) 1178 snprintf(maxp, sizeof maxp, "inf"); 1179 else 1180 snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages); 1181 1182 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 1183 (ovflw) += db_printf((fmt), \ 1184 (width) - (fixed) - (ovflw) > 0 ? \ 1185 (width) - (fixed) - (ovflw) : 0, \ 1186 (val)) - (width); \ 1187 if ((ovflw) < 0) \ 1188 (ovflw) = 0; \ 1189 } while (/* CONSTCOND */0) 1190 1191 ovflw = 0; 1192 PRWORD(ovflw, "%-*s", 10, 0, pp->pr_wchan); 1193 PRWORD(ovflw, " %*u", 4, 1, pp->pr_size); 1194 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget); 1195 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail); 1196 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput); 1197 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc); 1198 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree); 1199 PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages); 1200 PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat); 1201 PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages); 1202 PRWORD(ovflw, " %*s", 6, 1, maxp); 1203 PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle); 1204 1205 pool_chk(pp, pp->pr_wchan); 1206 } 1207 } 1208 1209 int 1210 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph) 1211 { 1212 struct pool_item *pi; 1213 caddr_t page; 1214 int n; 1215 #ifdef POOL_DEBUG 1216 int i, *ip; 1217 #endif 1218 1219 page = (caddr_t)((u_long)ph & pp->pr_alloc->pa_pagemask); 1220 if (page != ph->ph_page && 1221 (pp->pr_roflags & PR_PHINPAGE) != 0) { 1222 if (label != NULL) 1223 printf("%s: ", label); 1224 printf("pool(%p:%s): page inconsistency: page %p; " 1225 "at page head addr %p (p %p)\n", 1226 pp, pp->pr_wchan, ph->ph_page, ph, page); 1227 return 1; 1228 } 1229 1230 for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0; 1231 pi != NULL; 1232 pi = TAILQ_NEXT(pi,pi_list), n++) { 1233 1234 #ifdef DIAGNOSTIC 1235 if (pi->pi_magic != PI_MAGIC) { 1236 if (label != NULL) 1237 printf("%s: ", label); 1238 printf("pool(%s): free list modified: " 1239 "page %p; item ordinal %d; addr %p " 1240 "(p %p); offset 0x%x=0x%x\n", 1241 pp->pr_wchan, ph->ph_page, n, pi, page, 1242 0, pi->pi_magic); 1243 } 1244 #ifdef POOL_DEBUG 1245 for (ip = (int *)pi, i = sizeof(*pi) / sizeof(int); 1246 i < pp->pr_size / sizeof(int); i++) { 1247 if (ip[i] != PI_MAGIC) { 1248 printf("pool(%s): free list modified: " 1249 "page %p; item ordinal %d; addr %p " 1250 "(p %p); offset 0x%x=0x%x\n", 1251 pp->pr_wchan, ph->ph_page, n, pi, 1252 page, i * sizeof(int), ip[i]); 1253 } 1254 } 1255 1256 #endif /* POOL_DEBUG */ 1257 #endif /* DIAGNOSTIC */ 1258 page = 1259 (caddr_t)((u_long)pi & pp->pr_alloc->pa_pagemask); 1260 if (page == ph->ph_page) 1261 continue; 1262 1263 if (label != NULL) 1264 printf("%s: ", label); 1265 printf("pool(%p:%s): page inconsistency: page %p;" 1266 " item ordinal %d; addr %p (p %p)\n", pp, 1267 pp->pr_wchan, ph->ph_page, n, pi, page); 1268 return 1; 1269 } 1270 return 0; 1271 } 1272 1273 int 1274 pool_chk(struct pool *pp, const char *label) 1275 { 1276 struct pool_item_header *ph; 1277 int r = 0; 1278 1279 LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) 1280 r += pool_chk_page(pp, label, ph); 1281 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) 1282 r += pool_chk_page(pp, label, ph); 1283 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) 1284 r += pool_chk_page(pp, label, ph); 1285 1286 return (r); 1287 } 1288 1289 void 1290 pool_walk(struct pool *pp, void (*func)(void *)) 1291 { 1292 struct pool_item_header *ph; 1293 struct pool_item *pi; 1294 caddr_t cp; 1295 int n; 1296 1297 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) { 1298 cp = ph->ph_colored; 1299 n = ph->ph_nmissing; 1300 1301 while (n--) { 1302 func(cp); 1303 cp += pp->pr_size; 1304 } 1305 } 1306 1307 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) { 1308 cp = ph->ph_colored; 1309 n = ph->ph_nmissing; 1310 1311 do { 1312 TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { 1313 if (cp == (caddr_t)pi) 1314 break; 1315 } 1316 if (cp != (caddr_t)pi) { 1317 func(cp); 1318 n--; 1319 } 1320 1321 cp += pp->pr_size; 1322 } while (n > 0); 1323 } 1324 } 1325 #endif 1326 1327 /* 1328 * We have three different sysctls. 1329 * kern.pool.npools - the number of pools. 1330 * kern.pool.pool.<pool#> - the pool struct for the pool#. 1331 * kern.pool.name.<pool#> - the name for pool#. 1332 */ 1333 int 1334 sysctl_dopool(int *name, u_int namelen, char *where, size_t *sizep) 1335 { 1336 struct pool *pp, *foundpool = NULL; 1337 size_t buflen = where != NULL ? *sizep : 0; 1338 int npools = 0, s; 1339 unsigned int lookfor; 1340 size_t len; 1341 1342 switch (*name) { 1343 case KERN_POOL_NPOOLS: 1344 if (namelen != 1 || buflen != sizeof(int)) 1345 return (EINVAL); 1346 lookfor = 0; 1347 break; 1348 case KERN_POOL_NAME: 1349 if (namelen != 2 || buflen < 1) 1350 return (EINVAL); 1351 lookfor = name[1]; 1352 break; 1353 case KERN_POOL_POOL: 1354 if (namelen != 2 || buflen != sizeof(struct pool)) 1355 return (EINVAL); 1356 lookfor = name[1]; 1357 break; 1358 default: 1359 return (EINVAL); 1360 } 1361 1362 s = splvm(); 1363 1364 TAILQ_FOREACH(pp, &pool_head, pr_poollist) { 1365 npools++; 1366 if (lookfor == pp->pr_serial) { 1367 foundpool = pp; 1368 break; 1369 } 1370 } 1371 1372 splx(s); 1373 1374 if (*name != KERN_POOL_NPOOLS && foundpool == NULL) 1375 return (ENOENT); 1376 1377 switch (*name) { 1378 case KERN_POOL_NPOOLS: 1379 return copyout(&npools, where, buflen); 1380 case KERN_POOL_NAME: 1381 len = strlen(foundpool->pr_wchan) + 1; 1382 if (*sizep < len) 1383 return (ENOMEM); 1384 *sizep = len; 1385 return copyout(foundpool->pr_wchan, where, len); 1386 case KERN_POOL_POOL: 1387 return copyout(foundpool, where, buflen); 1388 } 1389 /* NOTREACHED */ 1390 return (0); /* XXX - Stupid gcc */ 1391 } 1392 1393 /* 1394 * Pool backend allocators. 1395 * 1396 * Each pool has a backend allocator that handles allocation, deallocation 1397 */ 1398 void *pool_page_alloc(struct pool *, int, int *); 1399 void pool_page_free(struct pool *, void *); 1400 1401 /* 1402 * safe for interrupts, name preserved for compat this is the default 1403 * allocator 1404 */ 1405 struct pool_allocator pool_allocator_nointr = { 1406 pool_page_alloc, pool_page_free, 0, 1407 }; 1408 1409 /* 1410 * XXX - we have at least three different resources for the same allocation 1411 * and each resource can be depleted. First we have the ready elements in 1412 * the pool. Then we have the resource (typically a vm_map) for this 1413 * allocator, then we have physical memory. Waiting for any of these can 1414 * be unnecessary when any other is freed, but the kernel doesn't support 1415 * sleeping on multiple addresses, so we have to fake. The caller sleeps on 1416 * the pool (so that we can be awakened when an item is returned to the pool), 1417 * but we set PA_WANT on the allocator. When a page is returned to 1418 * the allocator and PA_WANT is set pool_allocator_free will wakeup all 1419 * sleeping pools belonging to this allocator. (XXX - thundering herd). 1420 * We also wake up the allocator in case someone without a pool (malloc) 1421 * is sleeping waiting for this allocator. 1422 */ 1423 1424 void * 1425 pool_allocator_alloc(struct pool *pp, int flags, int *slowdown) 1426 { 1427 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; 1428 void *v; 1429 1430 if (waitok) 1431 mtx_leave(&pp->pr_mtx); 1432 v = pp->pr_alloc->pa_alloc(pp, flags, slowdown); 1433 if (waitok) 1434 mtx_enter(&pp->pr_mtx); 1435 1436 return (v); 1437 } 1438 1439 void 1440 pool_allocator_free(struct pool *pp, void *v) 1441 { 1442 struct pool_allocator *pa = pp->pr_alloc; 1443 1444 (*pa->pa_free)(pp, v); 1445 } 1446 1447 void * 1448 pool_page_alloc(struct pool *pp, int flags, int *slowdown) 1449 { 1450 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; 1451 1452 return (uvm_km_getpage(waitok, slowdown)); 1453 } 1454 1455 void 1456 pool_page_free(struct pool *pp, void *v) 1457 { 1458 1459 uvm_km_putpage(v); 1460 } 1461 1462 void * 1463 pool_large_alloc(struct pool *pp, int flags, int *slowdown) 1464 { 1465 int kfl = (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT; 1466 vaddr_t va; 1467 int s; 1468 1469 s = splvm(); 1470 va = uvm_km_kmemalloc(kmem_map, NULL, pp->pr_alloc->pa_pagesz, kfl); 1471 splx(s); 1472 1473 return ((void *)va); 1474 } 1475 1476 void 1477 pool_large_free(struct pool *pp, void *v) 1478 { 1479 int s; 1480 1481 s = splvm(); 1482 uvm_km_free(kmem_map, (vaddr_t)v, pp->pr_alloc->pa_pagesz); 1483 splx(s); 1484 } 1485 1486 void * 1487 pool_large_alloc_ni(struct pool *pp, int flags, int *slowdown) 1488 { 1489 int kfl = (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT; 1490 1491 return ((void *)uvm_km_kmemalloc(kernel_map, uvm.kernel_object, 1492 pp->pr_alloc->pa_pagesz, kfl)); 1493 } 1494 1495 void 1496 pool_large_free_ni(struct pool *pp, void *v) 1497 { 1498 uvm_km_free(kernel_map, (vaddr_t)v, pp->pr_alloc->pa_pagesz); 1499 } 1500