1 /* $OpenBSD: subr_pool.c,v 1.96 2010/07/03 03:04:55 tedu 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 RB_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 RB_PROTOTYPE(phtree, pool_item_header, ph_node, phtree_compare); 153 RB_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 = RB_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 RB_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 into an RB tree, so we can match a returned item with 345 * 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 RB_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 /* pglistalloc/constraint parameters */ 397 pp->pr_crange = &no_constraint; 398 pp->pr_pa_nsegs = 0; 399 400 /* Insert this into the list of all pools. */ 401 TAILQ_INSERT_HEAD(&pool_head, pp, pr_poollist); 402 } 403 404 void 405 pool_setipl(struct pool *pp, int ipl) 406 { 407 pp->pr_ipl = ipl; 408 mtx_init(&pp->pr_mtx, ipl); 409 } 410 411 /* 412 * Decommission a pool resource. 413 */ 414 void 415 pool_destroy(struct pool *pp) 416 { 417 struct pool_item_header *ph; 418 419 #ifdef DIAGNOSTIC 420 if (pp->pr_nout != 0) 421 panic("pool_destroy: pool busy: still out: %u", pp->pr_nout); 422 #endif 423 424 /* Remove all pages */ 425 while ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) 426 pr_rmpage(pp, ph, NULL); 427 KASSERT(LIST_EMPTY(&pp->pr_fullpages)); 428 KASSERT(LIST_EMPTY(&pp->pr_partpages)); 429 430 /* Remove from global pool list */ 431 TAILQ_REMOVE(&pool_head, pp, pr_poollist); 432 } 433 434 struct pool_item_header * 435 pool_alloc_item_header(struct pool *pp, caddr_t storage, int flags) 436 { 437 struct pool_item_header *ph; 438 439 if ((pp->pr_roflags & PR_PHINPAGE) != 0) 440 ph = (struct pool_item_header *)(storage + pp->pr_phoffset); 441 else 442 ph = pool_get(&phpool, (flags & ~(PR_WAITOK | PR_ZERO)) | 443 PR_NOWAIT); 444 445 return (ph); 446 } 447 448 /* 449 * Grab an item from the pool; must be called at appropriate spl level 450 */ 451 void * 452 pool_get(struct pool *pp, int flags) 453 { 454 void *v; 455 456 #ifdef DIAGNOSTIC 457 if ((flags & PR_WAITOK) != 0) 458 splassert(IPL_NONE); 459 #endif /* DIAGNOSTIC */ 460 461 mtx_enter(&pp->pr_mtx); 462 v = pool_do_get(pp, flags); 463 mtx_leave(&pp->pr_mtx); 464 if (v == NULL) 465 return (v); 466 467 if (pp->pr_ctor) { 468 if (flags & PR_ZERO) 469 panic("pool_get: PR_ZERO when ctor set"); 470 if (pp->pr_ctor(pp->pr_arg, v, flags)) { 471 mtx_enter(&pp->pr_mtx); 472 pool_do_put(pp, v); 473 mtx_leave(&pp->pr_mtx); 474 v = NULL; 475 } 476 } else { 477 if (flags & PR_ZERO) 478 memset(v, 0, pp->pr_size); 479 } 480 if (v != NULL) 481 pp->pr_nget++; 482 return (v); 483 } 484 485 void * 486 pool_do_get(struct pool *pp, int flags) 487 { 488 struct pool_item *pi; 489 struct pool_item_header *ph; 490 void *v; 491 int slowdown = 0; 492 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG) 493 int i, *ip; 494 #endif 495 496 #ifdef MALLOC_DEBUG 497 if (pp->pr_roflags & PR_DEBUG) { 498 void *addr; 499 500 addr = NULL; 501 debug_malloc(pp->pr_size, M_DEBUG, 502 (flags & PR_WAITOK) ? M_WAITOK : M_NOWAIT, &addr); 503 return (addr); 504 } 505 #endif 506 507 startover: 508 /* 509 * Check to see if we've reached the hard limit. If we have, 510 * and we can wait, then wait until an item has been returned to 511 * the pool. 512 */ 513 #ifdef DIAGNOSTIC 514 if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) 515 panic("pool_do_get: %s: crossed hard limit", pp->pr_wchan); 516 #endif 517 if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) { 518 if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) { 519 /* 520 * XXX: A warning isn't logged in this case. Should 521 * it be? 522 */ 523 pp->pr_flags |= PR_WANTED; 524 pool_sleep(pp); 525 goto startover; 526 } 527 528 /* 529 * Log a message that the hard limit has been hit. 530 */ 531 if (pp->pr_hardlimit_warning != NULL && 532 ratecheck(&pp->pr_hardlimit_warning_last, 533 &pp->pr_hardlimit_ratecap)) 534 log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning); 535 536 pp->pr_nfail++; 537 return (NULL); 538 } 539 540 /* 541 * The convention we use is that if `curpage' is not NULL, then 542 * it points at a non-empty bucket. In particular, `curpage' 543 * never points at a page header which has PR_PHINPAGE set and 544 * has no items in its bucket. 545 */ 546 if ((ph = pp->pr_curpage) == NULL) { 547 #ifdef DIAGNOSTIC 548 if (pp->pr_nitems != 0) { 549 printf("pool_do_get: %s: curpage NULL, nitems %u\n", 550 pp->pr_wchan, pp->pr_nitems); 551 panic("pool_do_get: nitems inconsistent"); 552 } 553 #endif 554 555 /* 556 * Call the back-end page allocator for more memory. 557 */ 558 v = pool_allocator_alloc(pp, flags, &slowdown); 559 if (__predict_true(v != NULL)) 560 ph = pool_alloc_item_header(pp, v, flags); 561 562 if (__predict_false(v == NULL || ph == NULL)) { 563 if (v != NULL) 564 pool_allocator_free(pp, v); 565 566 if ((flags & PR_WAITOK) == 0) { 567 pp->pr_nfail++; 568 return (NULL); 569 } 570 571 /* 572 * Wait for items to be returned to this pool. 573 * 574 * XXX: maybe we should wake up once a second and 575 * try again? 576 */ 577 pp->pr_flags |= PR_WANTED; 578 pool_sleep(pp); 579 goto startover; 580 } 581 582 /* We have more memory; add it to the pool */ 583 pool_prime_page(pp, v, ph); 584 pp->pr_npagealloc++; 585 586 if (slowdown && (flags & PR_WAITOK)) { 587 mtx_leave(&pp->pr_mtx); 588 yield(); 589 mtx_enter(&pp->pr_mtx); 590 } 591 592 /* Start the allocation process over. */ 593 goto startover; 594 } 595 if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) { 596 panic("pool_do_get: %s: page empty", pp->pr_wchan); 597 } 598 #ifdef DIAGNOSTIC 599 if (__predict_false(pp->pr_nitems == 0)) { 600 printf("pool_do_get: %s: items on itemlist, nitems %u\n", 601 pp->pr_wchan, pp->pr_nitems); 602 panic("pool_do_get: nitems inconsistent"); 603 } 604 #endif 605 606 #ifdef DIAGNOSTIC 607 if (__predict_false(pi->pi_magic != PI_MAGIC)) 608 panic("pool_do_get(%s): free list modified: " 609 "page %p; item addr %p; offset 0x%x=0x%x", 610 pp->pr_wchan, ph->ph_page, pi, 0, pi->pi_magic); 611 #ifdef POOL_DEBUG 612 for (ip = (int *)pi, i = sizeof(*pi) / sizeof(int); 613 i < pp->pr_size / sizeof(int); i++) { 614 if (ip[i] != PI_MAGIC) { 615 panic("pool_do_get(%s): free list modified: " 616 "page %p; item addr %p; offset 0x%x=0x%x", 617 pp->pr_wchan, ph->ph_page, pi, 618 i * sizeof(int), ip[i]); 619 } 620 } 621 #endif /* POOL_DEBUG */ 622 #endif /* DIAGNOSTIC */ 623 624 /* 625 * Remove from item list. 626 */ 627 TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list); 628 pp->pr_nitems--; 629 pp->pr_nout++; 630 if (ph->ph_nmissing == 0) { 631 #ifdef DIAGNOSTIC 632 if (__predict_false(pp->pr_nidle == 0)) 633 panic("pool_do_get: nidle inconsistent"); 634 #endif 635 pp->pr_nidle--; 636 637 /* 638 * This page was previously empty. Move it to the list of 639 * partially-full pages. This page is already curpage. 640 */ 641 LIST_REMOVE(ph, ph_pagelist); 642 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); 643 } 644 ph->ph_nmissing++; 645 if (TAILQ_EMPTY(&ph->ph_itemlist)) { 646 #ifdef DIAGNOSTIC 647 if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) { 648 panic("pool_do_get: %s: nmissing inconsistent", 649 pp->pr_wchan); 650 } 651 #endif 652 /* 653 * This page is now full. Move it to the full list 654 * and select a new current page. 655 */ 656 LIST_REMOVE(ph, ph_pagelist); 657 LIST_INSERT_HEAD(&pp->pr_fullpages, ph, ph_pagelist); 658 pool_update_curpage(pp); 659 } 660 661 /* 662 * If we have a low water mark and we are now below that low 663 * water mark, add more items to the pool. 664 */ 665 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) { 666 /* 667 * XXX: Should we log a warning? Should we set up a timeout 668 * to try again in a second or so? The latter could break 669 * a caller's assumptions about interrupt protection, etc. 670 */ 671 } 672 return (v); 673 } 674 675 /* 676 * Return resource to the pool; must be called at appropriate spl level 677 */ 678 void 679 pool_put(struct pool *pp, void *v) 680 { 681 if (pp->pr_dtor) 682 pp->pr_dtor(pp->pr_arg, v); 683 mtx_enter(&pp->pr_mtx); 684 pool_do_put(pp, v); 685 mtx_leave(&pp->pr_mtx); 686 pp->pr_nput++; 687 } 688 689 /* 690 * Internal version of pool_put(). 691 */ 692 void 693 pool_do_put(struct pool *pp, void *v) 694 { 695 struct pool_item *pi = v; 696 struct pool_item_header *ph; 697 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG) 698 int i, *ip; 699 #endif 700 701 if (v == NULL) 702 panic("pool_put of NULL"); 703 704 #ifdef MALLOC_DEBUG 705 if (pp->pr_roflags & PR_DEBUG) { 706 debug_free(v, M_DEBUG); 707 return; 708 } 709 #endif 710 711 #ifdef DIAGNOSTIC 712 if (pp->pr_ipl != -1) 713 splassert(pp->pr_ipl); 714 715 if (__predict_false(pp->pr_nout == 0)) { 716 printf("pool %s: putting with none out\n", 717 pp->pr_wchan); 718 panic("pool_do_put"); 719 } 720 #endif 721 722 if (__predict_false((ph = pr_find_pagehead(pp, v)) == NULL)) { 723 panic("pool_do_put: %s: page header missing", pp->pr_wchan); 724 } 725 726 /* 727 * Return to item list. 728 */ 729 #ifdef DIAGNOSTIC 730 pi->pi_magic = PI_MAGIC; 731 #ifdef POOL_DEBUG 732 for (ip = (int *)pi, i = sizeof(*pi)/sizeof(int); 733 i < pp->pr_size / sizeof(int); i++) 734 ip[i] = PI_MAGIC; 735 #endif /* POOL_DEBUG */ 736 #endif /* DIAGNOSTIC */ 737 738 TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list); 739 ph->ph_nmissing--; 740 pp->pr_nitems++; 741 pp->pr_nout--; 742 743 /* Cancel "pool empty" condition if it exists */ 744 if (pp->pr_curpage == NULL) 745 pp->pr_curpage = ph; 746 747 if (pp->pr_flags & PR_WANTED) { 748 pp->pr_flags &= ~PR_WANTED; 749 if (ph->ph_nmissing == 0) 750 pp->pr_nidle++; 751 wakeup(pp); 752 return; 753 } 754 755 /* 756 * If this page is now empty, do one of two things: 757 * 758 * (1) If we have more pages than the page high water mark, 759 * free the page back to the system. 760 * 761 * (2) Otherwise, move the page to the empty page list. 762 * 763 * Either way, select a new current page (so we use a partially-full 764 * page if one is available). 765 */ 766 if (ph->ph_nmissing == 0) { 767 pp->pr_nidle++; 768 if (pp->pr_nidle > pp->pr_maxpages) { 769 pr_rmpage(pp, ph, NULL); 770 } else { 771 LIST_REMOVE(ph, ph_pagelist); 772 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); 773 } 774 pool_update_curpage(pp); 775 } 776 777 /* 778 * If the page was previously completely full, move it to the 779 * partially-full list and make it the current page. The next 780 * allocation will get the item from this page, instead of 781 * further fragmenting the pool. 782 */ 783 else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) { 784 LIST_REMOVE(ph, ph_pagelist); 785 LIST_INSERT_HEAD(&pp->pr_partpages, ph, ph_pagelist); 786 pp->pr_curpage = ph; 787 } 788 } 789 790 /* 791 * Add N items to the pool. 792 */ 793 int 794 pool_prime(struct pool *pp, int n) 795 { 796 struct pool_item_header *ph; 797 caddr_t cp; 798 int newpages; 799 int slowdown; 800 801 mtx_enter(&pp->pr_mtx); 802 newpages = roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 803 804 while (newpages-- > 0) { 805 cp = pool_allocator_alloc(pp, PR_NOWAIT, &slowdown); 806 if (__predict_true(cp != NULL)) 807 ph = pool_alloc_item_header(pp, cp, PR_NOWAIT); 808 if (__predict_false(cp == NULL || ph == NULL)) { 809 if (cp != NULL) 810 pool_allocator_free(pp, cp); 811 break; 812 } 813 814 pool_prime_page(pp, cp, ph); 815 pp->pr_npagealloc++; 816 pp->pr_minpages++; 817 } 818 819 if (pp->pr_minpages >= pp->pr_maxpages) 820 pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */ 821 822 mtx_leave(&pp->pr_mtx); 823 return (0); 824 } 825 826 /* 827 * Add a page worth of items to the pool. 828 * 829 * Note, we must be called with the pool descriptor LOCKED. 830 */ 831 void 832 pool_prime_page(struct pool *pp, caddr_t storage, struct pool_item_header *ph) 833 { 834 struct pool_item *pi; 835 caddr_t cp = storage; 836 unsigned int align = pp->pr_align; 837 unsigned int ioff = pp->pr_itemoffset; 838 int n; 839 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG) 840 int i, *ip; 841 #endif 842 843 /* 844 * Insert page header. 845 */ 846 LIST_INSERT_HEAD(&pp->pr_emptypages, ph, ph_pagelist); 847 TAILQ_INIT(&ph->ph_itemlist); 848 ph->ph_page = storage; 849 ph->ph_pagesize = pp->pr_alloc->pa_pagesz; 850 ph->ph_nmissing = 0; 851 if ((pp->pr_roflags & PR_PHINPAGE) == 0) 852 RB_INSERT(phtree, &pp->pr_phtree, ph); 853 854 pp->pr_nidle++; 855 856 /* 857 * Color this page. 858 */ 859 cp = (caddr_t)(cp + pp->pr_curcolor); 860 if ((pp->pr_curcolor += align) > pp->pr_maxcolor) 861 pp->pr_curcolor = 0; 862 863 /* 864 * Adjust storage to apply alignment to `pr_itemoffset' in each item. 865 */ 866 if (ioff != 0) 867 cp = (caddr_t)(cp + (align - ioff)); 868 ph->ph_colored = cp; 869 870 /* 871 * Insert remaining chunks on the bucket list. 872 */ 873 n = pp->pr_itemsperpage; 874 pp->pr_nitems += n; 875 876 while (n--) { 877 pi = (struct pool_item *)cp; 878 879 KASSERT(((((vaddr_t)pi) + ioff) & (align - 1)) == 0); 880 881 /* Insert on page list */ 882 TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list); 883 884 #ifdef DIAGNOSTIC 885 pi->pi_magic = PI_MAGIC; 886 #ifdef POOL_DEBUG 887 for (ip = (int *)pi, i = sizeof(*pi)/sizeof(int); 888 i < pp->pr_size / sizeof(int); i++) 889 ip[i] = PI_MAGIC; 890 #endif /* POOL_DEBUG */ 891 #endif /* DIAGNOSTIC */ 892 cp = (caddr_t)(cp + pp->pr_size); 893 } 894 895 /* 896 * If the pool was depleted, point at the new page. 897 */ 898 if (pp->pr_curpage == NULL) 899 pp->pr_curpage = ph; 900 901 if (++pp->pr_npages > pp->pr_hiwat) 902 pp->pr_hiwat = pp->pr_npages; 903 } 904 905 /* 906 * Used by pool_get() when nitems drops below the low water mark. This 907 * is used to catch up pr_nitems with the low water mark. 908 * 909 * Note we never wait for memory here, we let the caller decide what to do. 910 */ 911 int 912 pool_catchup(struct pool *pp) 913 { 914 struct pool_item_header *ph; 915 caddr_t cp; 916 int error = 0; 917 int slowdown; 918 919 while (POOL_NEEDS_CATCHUP(pp)) { 920 /* 921 * Call the page back-end allocator for more memory. 922 */ 923 cp = pool_allocator_alloc(pp, PR_NOWAIT, &slowdown); 924 if (__predict_true(cp != NULL)) 925 ph = pool_alloc_item_header(pp, cp, PR_NOWAIT); 926 if (__predict_false(cp == NULL || ph == NULL)) { 927 if (cp != NULL) 928 pool_allocator_free(pp, cp); 929 error = ENOMEM; 930 break; 931 } 932 pool_prime_page(pp, cp, ph); 933 pp->pr_npagealloc++; 934 } 935 936 return (error); 937 } 938 939 void 940 pool_update_curpage(struct pool *pp) 941 { 942 943 pp->pr_curpage = LIST_FIRST(&pp->pr_partpages); 944 if (pp->pr_curpage == NULL) { 945 pp->pr_curpage = LIST_FIRST(&pp->pr_emptypages); 946 } 947 } 948 949 void 950 pool_setlowat(struct pool *pp, int n) 951 { 952 953 pp->pr_minitems = n; 954 pp->pr_minpages = (n == 0) 955 ? 0 956 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 957 958 mtx_enter(&pp->pr_mtx); 959 /* Make sure we're caught up with the newly-set low water mark. */ 960 if (POOL_NEEDS_CATCHUP(pp) && pool_catchup(pp) != 0) { 961 /* 962 * XXX: Should we log a warning? Should we set up a timeout 963 * to try again in a second or so? The latter could break 964 * a caller's assumptions about interrupt protection, etc. 965 */ 966 } 967 mtx_leave(&pp->pr_mtx); 968 } 969 970 void 971 pool_sethiwat(struct pool *pp, int n) 972 { 973 974 pp->pr_maxpages = (n == 0) 975 ? 0 976 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 977 } 978 979 int 980 pool_sethardlimit(struct pool *pp, u_int n, const char *warnmsg, int ratecap) 981 { 982 int error = 0; 983 984 if (n < pp->pr_nout) { 985 error = EINVAL; 986 goto done; 987 } 988 989 pp->pr_hardlimit = n; 990 pp->pr_hardlimit_warning = warnmsg; 991 pp->pr_hardlimit_ratecap.tv_sec = ratecap; 992 pp->pr_hardlimit_warning_last.tv_sec = 0; 993 pp->pr_hardlimit_warning_last.tv_usec = 0; 994 995 /* 996 * In-line version of pool_sethiwat(). 997 */ 998 pp->pr_maxpages = (n == 0 || n == UINT_MAX) 999 ? n 1000 : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage; 1001 1002 done: 1003 return (error); 1004 } 1005 1006 void 1007 pool_set_constraints(struct pool *pp, struct uvm_constraint_range *range, 1008 int nsegs) 1009 { 1010 /* 1011 * Subsequent changes to the constrictions are only 1012 * allowed to make them _more_ strict. 1013 */ 1014 KASSERT(pp->pr_crange->ucr_high >= range->ucr_high && 1015 pp->pr_crange->ucr_low <= range->ucr_low); 1016 1017 pp->pr_crange = range; 1018 pp->pr_pa_nsegs = nsegs; 1019 } 1020 1021 void 1022 pool_set_ctordtor(struct pool *pp, int (*ctor)(void *, void *, int), 1023 void (*dtor)(void *, void *), void *arg) 1024 { 1025 pp->pr_ctor = ctor; 1026 pp->pr_dtor = dtor; 1027 pp->pr_arg = arg; 1028 } 1029 /* 1030 * Release all complete pages that have not been used recently. 1031 * 1032 * Returns non-zero if any pages have been reclaimed. 1033 */ 1034 int 1035 pool_reclaim(struct pool *pp) 1036 { 1037 struct pool_item_header *ph, *phnext; 1038 struct pool_pagelist pq; 1039 1040 LIST_INIT(&pq); 1041 1042 mtx_enter(&pp->pr_mtx); 1043 for (ph = LIST_FIRST(&pp->pr_emptypages); ph != NULL; ph = phnext) { 1044 phnext = LIST_NEXT(ph, ph_pagelist); 1045 1046 /* Check our minimum page claim */ 1047 if (pp->pr_npages <= pp->pr_minpages) 1048 break; 1049 1050 KASSERT(ph->ph_nmissing == 0); 1051 1052 /* 1053 * If freeing this page would put us below 1054 * the low water mark, stop now. 1055 */ 1056 if ((pp->pr_nitems - pp->pr_itemsperpage) < 1057 pp->pr_minitems) 1058 break; 1059 1060 pr_rmpage(pp, ph, &pq); 1061 } 1062 mtx_leave(&pp->pr_mtx); 1063 1064 if (LIST_EMPTY(&pq)) 1065 return (0); 1066 while ((ph = LIST_FIRST(&pq)) != NULL) { 1067 LIST_REMOVE(ph, ph_pagelist); 1068 pool_allocator_free(pp, ph->ph_page); 1069 if (pp->pr_roflags & PR_PHINPAGE) 1070 continue; 1071 pool_put(&phpool, ph); 1072 } 1073 1074 return (1); 1075 } 1076 1077 #ifdef DDB 1078 #include <machine/db_machdep.h> 1079 #include <ddb/db_interface.h> 1080 #include <ddb/db_output.h> 1081 1082 /* 1083 * Diagnostic helpers. 1084 */ 1085 void 1086 pool_printit(struct pool *pp, const char *modif, int (*pr)(const char *, ...)) 1087 { 1088 pool_print1(pp, modif, pr); 1089 } 1090 1091 void 1092 pool_print_pagelist(struct pool_pagelist *pl, int (*pr)(const char *, ...)) 1093 { 1094 struct pool_item_header *ph; 1095 #ifdef DIAGNOSTIC 1096 struct pool_item *pi; 1097 #endif 1098 1099 LIST_FOREACH(ph, pl, ph_pagelist) { 1100 (*pr)("\t\tpage %p, nmissing %d\n", 1101 ph->ph_page, ph->ph_nmissing); 1102 #ifdef DIAGNOSTIC 1103 TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { 1104 if (pi->pi_magic != PI_MAGIC) { 1105 (*pr)("\t\t\titem %p, magic 0x%x\n", 1106 pi, pi->pi_magic); 1107 } 1108 } 1109 #endif 1110 } 1111 } 1112 1113 void 1114 pool_print1(struct pool *pp, const char *modif, int (*pr)(const char *, ...)) 1115 { 1116 struct pool_item_header *ph; 1117 int print_pagelist = 0; 1118 char c; 1119 1120 while ((c = *modif++) != '\0') { 1121 if (c == 'p') 1122 print_pagelist = 1; 1123 modif++; 1124 } 1125 1126 (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n", 1127 pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset, 1128 pp->pr_roflags); 1129 (*pr)("\talloc %p\n", pp->pr_alloc); 1130 (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n", 1131 pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages); 1132 (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n", 1133 pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit); 1134 1135 (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n", 1136 pp->pr_nget, pp->pr_nfail, pp->pr_nput); 1137 (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n", 1138 pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle); 1139 1140 if (print_pagelist == 0) 1141 return; 1142 1143 if ((ph = LIST_FIRST(&pp->pr_emptypages)) != NULL) 1144 (*pr)("\n\tempty page list:\n"); 1145 pool_print_pagelist(&pp->pr_emptypages, pr); 1146 if ((ph = LIST_FIRST(&pp->pr_fullpages)) != NULL) 1147 (*pr)("\n\tfull page list:\n"); 1148 pool_print_pagelist(&pp->pr_fullpages, pr); 1149 if ((ph = LIST_FIRST(&pp->pr_partpages)) != NULL) 1150 (*pr)("\n\tpartial-page list:\n"); 1151 pool_print_pagelist(&pp->pr_partpages, pr); 1152 1153 if (pp->pr_curpage == NULL) 1154 (*pr)("\tno current page\n"); 1155 else 1156 (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page); 1157 } 1158 1159 void 1160 db_show_all_pools(db_expr_t expr, int haddr, db_expr_t count, char *modif) 1161 { 1162 struct pool *pp; 1163 char maxp[16]; 1164 int ovflw; 1165 char mode; 1166 1167 mode = modif[0]; 1168 if (mode != '\0' && mode != 'a') { 1169 db_printf("usage: show all pools [/a]\n"); 1170 return; 1171 } 1172 1173 if (mode == '\0') 1174 db_printf("%-10s%4s%9s%5s%9s%6s%6s%6s%6s%6s%6s%5s\n", 1175 "Name", 1176 "Size", 1177 "Requests", 1178 "Fail", 1179 "Releases", 1180 "Pgreq", 1181 "Pgrel", 1182 "Npage", 1183 "Hiwat", 1184 "Minpg", 1185 "Maxpg", 1186 "Idle"); 1187 else 1188 db_printf("%-10s %18s %18s\n", 1189 "Name", "Address", "Allocator"); 1190 1191 TAILQ_FOREACH(pp, &pool_head, pr_poollist) { 1192 if (mode == 'a') { 1193 db_printf("%-10s %18p %18p\n", pp->pr_wchan, pp, 1194 pp->pr_alloc); 1195 continue; 1196 } 1197 1198 if (!pp->pr_nget) 1199 continue; 1200 1201 if (pp->pr_maxpages == UINT_MAX) 1202 snprintf(maxp, sizeof maxp, "inf"); 1203 else 1204 snprintf(maxp, sizeof maxp, "%u", pp->pr_maxpages); 1205 1206 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 1207 (ovflw) += db_printf((fmt), \ 1208 (width) - (fixed) - (ovflw) > 0 ? \ 1209 (width) - (fixed) - (ovflw) : 0, \ 1210 (val)) - (width); \ 1211 if ((ovflw) < 0) \ 1212 (ovflw) = 0; \ 1213 } while (/* CONSTCOND */0) 1214 1215 ovflw = 0; 1216 PRWORD(ovflw, "%-*s", 10, 0, pp->pr_wchan); 1217 PRWORD(ovflw, " %*u", 4, 1, pp->pr_size); 1218 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nget); 1219 PRWORD(ovflw, " %*lu", 5, 1, pp->pr_nfail); 1220 PRWORD(ovflw, " %*lu", 9, 1, pp->pr_nput); 1221 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagealloc); 1222 PRWORD(ovflw, " %*lu", 6, 1, pp->pr_npagefree); 1223 PRWORD(ovflw, " %*d", 6, 1, pp->pr_npages); 1224 PRWORD(ovflw, " %*d", 6, 1, pp->pr_hiwat); 1225 PRWORD(ovflw, " %*d", 6, 1, pp->pr_minpages); 1226 PRWORD(ovflw, " %*s", 6, 1, maxp); 1227 PRWORD(ovflw, " %*lu\n", 5, 1, pp->pr_nidle); 1228 1229 pool_chk(pp, pp->pr_wchan); 1230 } 1231 } 1232 1233 int 1234 pool_chk_page(struct pool *pp, const char *label, struct pool_item_header *ph) 1235 { 1236 struct pool_item *pi; 1237 caddr_t page; 1238 int n; 1239 #if defined(DIAGNOSTIC) && defined(POOL_DEBUG) 1240 int i, *ip; 1241 #endif 1242 1243 page = (caddr_t)((u_long)ph & pp->pr_alloc->pa_pagemask); 1244 if (page != ph->ph_page && 1245 (pp->pr_roflags & PR_PHINPAGE) != 0) { 1246 if (label != NULL) 1247 printf("%s: ", label); 1248 printf("pool(%p:%s): page inconsistency: page %p; " 1249 "at page head addr %p (p %p)\n", 1250 pp, pp->pr_wchan, ph->ph_page, ph, page); 1251 return 1; 1252 } 1253 1254 for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0; 1255 pi != NULL; 1256 pi = TAILQ_NEXT(pi,pi_list), n++) { 1257 1258 #ifdef DIAGNOSTIC 1259 if (pi->pi_magic != PI_MAGIC) { 1260 if (label != NULL) 1261 printf("%s: ", label); 1262 printf("pool(%s): free list modified: " 1263 "page %p; item ordinal %d; addr %p " 1264 "(p %p); offset 0x%x=0x%x\n", 1265 pp->pr_wchan, ph->ph_page, n, pi, page, 1266 0, pi->pi_magic); 1267 } 1268 #ifdef POOL_DEBUG 1269 for (ip = (int *)pi, i = sizeof(*pi) / sizeof(int); 1270 i < pp->pr_size / sizeof(int); i++) { 1271 if (ip[i] != PI_MAGIC) { 1272 printf("pool(%s): free list modified: " 1273 "page %p; item ordinal %d; addr %p " 1274 "(p %p); offset 0x%x=0x%x\n", 1275 pp->pr_wchan, ph->ph_page, n, pi, 1276 page, i * sizeof(int), ip[i]); 1277 } 1278 } 1279 1280 #endif /* POOL_DEBUG */ 1281 #endif /* DIAGNOSTIC */ 1282 page = 1283 (caddr_t)((u_long)pi & pp->pr_alloc->pa_pagemask); 1284 if (page == ph->ph_page) 1285 continue; 1286 1287 if (label != NULL) 1288 printf("%s: ", label); 1289 printf("pool(%p:%s): page inconsistency: page %p;" 1290 " item ordinal %d; addr %p (p %p)\n", pp, 1291 pp->pr_wchan, ph->ph_page, n, pi, page); 1292 return 1; 1293 } 1294 return 0; 1295 } 1296 1297 int 1298 pool_chk(struct pool *pp, const char *label) 1299 { 1300 struct pool_item_header *ph; 1301 int r = 0; 1302 1303 LIST_FOREACH(ph, &pp->pr_emptypages, ph_pagelist) 1304 r += pool_chk_page(pp, label, ph); 1305 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) 1306 r += pool_chk_page(pp, label, ph); 1307 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) 1308 r += pool_chk_page(pp, label, ph); 1309 1310 return (r); 1311 } 1312 1313 void 1314 pool_walk(struct pool *pp, int full, int (*pr)(const char *, ...), 1315 void (*func)(void *, int, int (*)(const char *, ...))) 1316 { 1317 struct pool_item_header *ph; 1318 struct pool_item *pi; 1319 caddr_t cp; 1320 int n; 1321 1322 LIST_FOREACH(ph, &pp->pr_fullpages, ph_pagelist) { 1323 cp = ph->ph_colored; 1324 n = ph->ph_nmissing; 1325 1326 while (n--) { 1327 func(cp, full, pr); 1328 cp += pp->pr_size; 1329 } 1330 } 1331 1332 LIST_FOREACH(ph, &pp->pr_partpages, ph_pagelist) { 1333 cp = ph->ph_colored; 1334 n = ph->ph_nmissing; 1335 1336 do { 1337 TAILQ_FOREACH(pi, &ph->ph_itemlist, pi_list) { 1338 if (cp == (caddr_t)pi) 1339 break; 1340 } 1341 if (cp != (caddr_t)pi) { 1342 func(cp, full, pr); 1343 n--; 1344 } 1345 1346 cp += pp->pr_size; 1347 } while (n > 0); 1348 } 1349 } 1350 #endif 1351 1352 /* 1353 * We have three different sysctls. 1354 * kern.pool.npools - the number of pools. 1355 * kern.pool.pool.<pool#> - the pool struct for the pool#. 1356 * kern.pool.name.<pool#> - the name for pool#. 1357 */ 1358 int 1359 sysctl_dopool(int *name, u_int namelen, char *where, size_t *sizep) 1360 { 1361 struct pool *pp, *foundpool = NULL; 1362 size_t buflen = where != NULL ? *sizep : 0; 1363 int npools = 0, s; 1364 unsigned int lookfor; 1365 size_t len; 1366 1367 switch (*name) { 1368 case KERN_POOL_NPOOLS: 1369 if (namelen != 1 || buflen != sizeof(int)) 1370 return (EINVAL); 1371 lookfor = 0; 1372 break; 1373 case KERN_POOL_NAME: 1374 if (namelen != 2 || buflen < 1) 1375 return (EINVAL); 1376 lookfor = name[1]; 1377 break; 1378 case KERN_POOL_POOL: 1379 if (namelen != 2 || buflen != sizeof(struct pool)) 1380 return (EINVAL); 1381 lookfor = name[1]; 1382 break; 1383 default: 1384 return (EINVAL); 1385 } 1386 1387 s = splvm(); 1388 1389 TAILQ_FOREACH(pp, &pool_head, pr_poollist) { 1390 npools++; 1391 if (lookfor == pp->pr_serial) { 1392 foundpool = pp; 1393 break; 1394 } 1395 } 1396 1397 splx(s); 1398 1399 if (*name != KERN_POOL_NPOOLS && foundpool == NULL) 1400 return (ENOENT); 1401 1402 switch (*name) { 1403 case KERN_POOL_NPOOLS: 1404 return copyout(&npools, where, buflen); 1405 case KERN_POOL_NAME: 1406 len = strlen(foundpool->pr_wchan) + 1; 1407 if (*sizep < len) 1408 return (ENOMEM); 1409 *sizep = len; 1410 return copyout(foundpool->pr_wchan, where, len); 1411 case KERN_POOL_POOL: 1412 return copyout(foundpool, where, buflen); 1413 } 1414 /* NOTREACHED */ 1415 return (0); /* XXX - Stupid gcc */ 1416 } 1417 1418 /* 1419 * Pool backend allocators. 1420 * 1421 * Each pool has a backend allocator that handles allocation, deallocation 1422 */ 1423 void *pool_page_alloc(struct pool *, int, int *); 1424 void pool_page_free(struct pool *, void *); 1425 1426 /* 1427 * safe for interrupts, name preserved for compat this is the default 1428 * allocator 1429 */ 1430 struct pool_allocator pool_allocator_nointr = { 1431 pool_page_alloc, pool_page_free, 0, 1432 }; 1433 1434 /* 1435 * XXX - we have at least three different resources for the same allocation 1436 * and each resource can be depleted. First we have the ready elements in 1437 * the pool. Then we have the resource (typically a vm_map) for this 1438 * allocator, then we have physical memory. Waiting for any of these can 1439 * be unnecessary when any other is freed, but the kernel doesn't support 1440 * sleeping on multiple addresses, so we have to fake. The caller sleeps on 1441 * the pool (so that we can be awakened when an item is returned to the pool), 1442 * but we set PA_WANT on the allocator. When a page is returned to 1443 * the allocator and PA_WANT is set pool_allocator_free will wakeup all 1444 * sleeping pools belonging to this allocator. (XXX - thundering herd). 1445 * We also wake up the allocator in case someone without a pool (malloc) 1446 * is sleeping waiting for this allocator. 1447 */ 1448 1449 void * 1450 pool_allocator_alloc(struct pool *pp, int flags, int *slowdown) 1451 { 1452 boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE; 1453 void *v; 1454 1455 if (waitok) 1456 mtx_leave(&pp->pr_mtx); 1457 v = pp->pr_alloc->pa_alloc(pp, flags, slowdown); 1458 if (waitok) 1459 mtx_enter(&pp->pr_mtx); 1460 1461 return (v); 1462 } 1463 1464 void 1465 pool_allocator_free(struct pool *pp, void *v) 1466 { 1467 struct pool_allocator *pa = pp->pr_alloc; 1468 1469 (*pa->pa_free)(pp, v); 1470 } 1471 1472 void * 1473 pool_page_alloc(struct pool *pp, int flags, int *slowdown) 1474 { 1475 int kfl = (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT; 1476 1477 return (uvm_km_getpage_pla(kfl, slowdown, pp->pr_crange->ucr_low, 1478 pp->pr_crange->ucr_high, 0, 0)); 1479 } 1480 1481 void 1482 pool_page_free(struct pool *pp, void *v) 1483 { 1484 uvm_km_putpage(v); 1485 } 1486 1487 void * 1488 pool_large_alloc(struct pool *pp, int flags, int *slowdown) 1489 { 1490 int kfl = (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT; 1491 vaddr_t va; 1492 int s; 1493 1494 s = splvm(); 1495 va = uvm_km_kmemalloc_pla(kmem_map, NULL, pp->pr_alloc->pa_pagesz, 0, 1496 kfl, pp->pr_crange->ucr_low, pp->pr_crange->ucr_high, 1497 0, 0, pp->pr_pa_nsegs); 1498 splx(s); 1499 1500 return ((void *)va); 1501 } 1502 1503 void 1504 pool_large_free(struct pool *pp, void *v) 1505 { 1506 int s; 1507 1508 s = splvm(); 1509 uvm_km_free(kmem_map, (vaddr_t)v, pp->pr_alloc->pa_pagesz); 1510 splx(s); 1511 } 1512 1513 void * 1514 pool_large_alloc_ni(struct pool *pp, int flags, int *slowdown) 1515 { 1516 int kfl = (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT; 1517 1518 return ((void *)uvm_km_kmemalloc_pla(kernel_map, uvm.kernel_object, 1519 pp->pr_alloc->pa_pagesz, 0, kfl, 1520 pp->pr_crange->ucr_low, pp->pr_crange->ucr_high, 1521 0, 0, pp->pr_pa_nsegs)); 1522 } 1523 1524 void 1525 pool_large_free_ni(struct pool *pp, void *v) 1526 { 1527 uvm_km_free(kernel_map, (vaddr_t)v, pp->pr_alloc->pa_pagesz); 1528 } 1529