Annotation of src/sys/kern/subr_pool.c, Revision 1.42
1.42 ! thorpej 1: /* $NetBSD: subr_pool.c,v 1.41 2000/11/19 00:29:51 sommerfeld Exp $ */
1.1 pk 2:
3: /*-
1.20 thorpej 4: * Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
1.1 pk 5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
1.20 thorpej 8: * by Paul Kranenburg; by Jason R. Thorpe of the Numerical Aerospace
9: * Simulation Facility, NASA Ames Research Center.
1.1 pk 10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
1.13 christos 21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
1.1 pk 23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
1.24 scottr 39:
1.25 thorpej 40: #include "opt_pool.h"
1.24 scottr 41: #include "opt_poollog.h"
1.28 thorpej 42: #include "opt_lockdebug.h"
1.1 pk 43:
44: #include <sys/param.h>
45: #include <sys/systm.h>
46: #include <sys/proc.h>
47: #include <sys/errno.h>
48: #include <sys/kernel.h>
49: #include <sys/malloc.h>
50: #include <sys/lock.h>
51: #include <sys/pool.h>
1.20 thorpej 52: #include <sys/syslog.h>
1.3 pk 53:
54: #include <uvm/uvm.h>
55:
1.1 pk 56: /*
57: * Pool resource management utility.
1.3 pk 58: *
59: * Memory is allocated in pages which are split into pieces according
60: * to the pool item size. Each page is kept on a list headed by `pr_pagelist'
61: * in the pool structure and the individual pool items are on a linked list
62: * headed by `ph_itemlist' in each page header. The memory for building
63: * the page list is either taken from the allocated pages themselves (for
64: * small pool items) or taken from an internal pool of page headers (`phpool').
1.1 pk 65: */
66:
1.3 pk 67: /* List of all pools */
1.5 thorpej 68: TAILQ_HEAD(,pool) pool_head = TAILQ_HEAD_INITIALIZER(pool_head);
1.3 pk 69:
70: /* Private pool for page header structures */
71: static struct pool phpool;
72:
73: /* # of seconds to retain page after last use */
74: int pool_inactive_time = 10;
75:
76: /* Next candidate for drainage (see pool_drain()) */
1.23 thorpej 77: static struct pool *drainpp;
78:
79: /* This spin lock protects both pool_head and drainpp. */
80: struct simplelock pool_head_slock = SIMPLELOCK_INITIALIZER;
1.3 pk 81:
82: struct pool_item_header {
83: /* Page headers */
84: TAILQ_ENTRY(pool_item_header)
85: ph_pagelist; /* pool page list */
86: TAILQ_HEAD(,pool_item) ph_itemlist; /* chunk list for this page */
87: LIST_ENTRY(pool_item_header)
88: ph_hashlist; /* Off-page page headers */
89: int ph_nmissing; /* # of chunks in use */
90: caddr_t ph_page; /* this page's address */
91: struct timeval ph_time; /* last referenced */
92: };
93:
1.1 pk 94: struct pool_item {
1.3 pk 95: #ifdef DIAGNOSTIC
96: int pi_magic;
1.33 chs 97: #endif
1.25 thorpej 98: #define PI_MAGIC 0xdeadbeef
1.3 pk 99: /* Other entries use only this list entry */
100: TAILQ_ENTRY(pool_item) pi_list;
101: };
102:
103:
1.25 thorpej 104: #define PR_HASH_INDEX(pp,addr) \
1.3 pk 105: (((u_long)(addr) >> (pp)->pr_pageshift) & (PR_HASHTABSIZE - 1))
106:
107:
108:
1.42 ! thorpej 109: static int pool_catchup(struct pool *);
! 110: static void pool_prime_page(struct pool *, caddr_t);
! 111: static void *pool_page_alloc(unsigned long, int, int);
! 112: static void pool_page_free(void *, unsigned long, int);
1.3 pk 113:
1.42 ! thorpej 114: static void pool_print1(struct pool *, const char *,
! 115: void (*)(const char *, ...));
1.3 pk 116:
117: /*
118: * Pool log entry. An array of these is allocated in pool_create().
119: */
120: struct pool_log {
121: const char *pl_file;
122: long pl_line;
123: int pl_action;
1.25 thorpej 124: #define PRLOG_GET 1
125: #define PRLOG_PUT 2
1.3 pk 126: void *pl_addr;
1.1 pk 127: };
128:
1.3 pk 129: /* Number of entries in pool log buffers */
1.17 thorpej 130: #ifndef POOL_LOGSIZE
131: #define POOL_LOGSIZE 10
132: #endif
133:
134: int pool_logsize = POOL_LOGSIZE;
1.1 pk 135:
1.25 thorpej 136: #ifdef DIAGNOSTIC
1.42 ! thorpej 137: static __inline void
! 138: pr_log(struct pool *pp, void *v, int action, const char *file, long line)
1.3 pk 139: {
140: int n = pp->pr_curlogentry;
141: struct pool_log *pl;
142:
1.20 thorpej 143: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 144: return;
145:
146: /*
147: * Fill in the current entry. Wrap around and overwrite
148: * the oldest entry if necessary.
149: */
150: pl = &pp->pr_log[n];
151: pl->pl_file = file;
152: pl->pl_line = line;
153: pl->pl_action = action;
154: pl->pl_addr = v;
155: if (++n >= pp->pr_logsize)
156: n = 0;
157: pp->pr_curlogentry = n;
158: }
159:
160: static void
1.42 ! thorpej 161: pr_printlog(struct pool *pp, struct pool_item *pi,
! 162: void (*pr)(const char *, ...))
1.3 pk 163: {
164: int i = pp->pr_logsize;
165: int n = pp->pr_curlogentry;
166:
1.20 thorpej 167: if ((pp->pr_roflags & PR_LOGGING) == 0)
1.3 pk 168: return;
169:
170: /*
171: * Print all entries in this pool's log.
172: */
173: while (i-- > 0) {
174: struct pool_log *pl = &pp->pr_log[n];
175: if (pl->pl_action != 0) {
1.25 thorpej 176: if (pi == NULL || pi == pl->pl_addr) {
177: (*pr)("\tlog entry %d:\n", i);
178: (*pr)("\t\taction = %s, addr = %p\n",
179: pl->pl_action == PRLOG_GET ? "get" : "put",
180: pl->pl_addr);
181: (*pr)("\t\tfile: %s at line %lu\n",
182: pl->pl_file, pl->pl_line);
183: }
1.3 pk 184: }
185: if (++n >= pp->pr_logsize)
186: n = 0;
187: }
188: }
1.25 thorpej 189:
1.42 ! thorpej 190: static __inline void
! 191: pr_enter(struct pool *pp, const char *file, long line)
1.25 thorpej 192: {
193:
1.34 thorpej 194: if (__predict_false(pp->pr_entered_file != NULL)) {
1.25 thorpej 195: printf("pool %s: reentrancy at file %s line %ld\n",
196: pp->pr_wchan, file, line);
197: printf(" previous entry at file %s line %ld\n",
198: pp->pr_entered_file, pp->pr_entered_line);
199: panic("pr_enter");
200: }
201:
202: pp->pr_entered_file = file;
203: pp->pr_entered_line = line;
204: }
205:
1.42 ! thorpej 206: static __inline void
! 207: pr_leave(struct pool *pp)
1.25 thorpej 208: {
209:
1.34 thorpej 210: if (__predict_false(pp->pr_entered_file == NULL)) {
1.25 thorpej 211: printf("pool %s not entered?\n", pp->pr_wchan);
212: panic("pr_leave");
213: }
214:
215: pp->pr_entered_file = NULL;
216: pp->pr_entered_line = 0;
217: }
218:
1.42 ! thorpej 219: static __inline void
! 220: pr_enter_check(struct pool *pp, void (*pr)(const char *, ...))
1.25 thorpej 221: {
222:
223: if (pp->pr_entered_file != NULL)
224: (*pr)("\n\tcurrently entered from file %s line %ld\n",
225: pp->pr_entered_file, pp->pr_entered_line);
226: }
1.3 pk 227: #else
1.25 thorpej 228: #define pr_log(pp, v, action, file, line)
229: #define pr_printlog(pp, pi, pr)
230: #define pr_enter(pp, file, line)
231: #define pr_leave(pp)
232: #define pr_enter_check(pp, pr)
233: #endif /* DIAGNOSTIC */
1.3 pk 234:
235: /*
236: * Return the pool page header based on page address.
237: */
1.42 ! thorpej 238: static __inline struct pool_item_header *
! 239: pr_find_pagehead(struct pool *pp, caddr_t page)
1.3 pk 240: {
241: struct pool_item_header *ph;
242:
1.20 thorpej 243: if ((pp->pr_roflags & PR_PHINPAGE) != 0)
1.3 pk 244: return ((struct pool_item_header *)(page + pp->pr_phoffset));
245:
246: for (ph = LIST_FIRST(&pp->pr_hashtab[PR_HASH_INDEX(pp, page)]);
247: ph != NULL;
248: ph = LIST_NEXT(ph, ph_hashlist)) {
249: if (ph->ph_page == page)
250: return (ph);
251: }
252: return (NULL);
253: }
254:
255: /*
256: * Remove a page from the pool.
257: */
1.42 ! thorpej 258: static __inline void
! 259: pr_rmpage(struct pool *pp, struct pool_item_header *ph)
1.3 pk 260: {
261:
262: /*
1.7 thorpej 263: * If the page was idle, decrement the idle page count.
1.3 pk 264: */
1.6 thorpej 265: if (ph->ph_nmissing == 0) {
266: #ifdef DIAGNOSTIC
267: if (pp->pr_nidle == 0)
268: panic("pr_rmpage: nidle inconsistent");
1.20 thorpej 269: if (pp->pr_nitems < pp->pr_itemsperpage)
270: panic("pr_rmpage: nitems inconsistent");
1.6 thorpej 271: #endif
272: pp->pr_nidle--;
273: }
1.7 thorpej 274:
1.20 thorpej 275: pp->pr_nitems -= pp->pr_itemsperpage;
276:
1.7 thorpej 277: /*
278: * Unlink a page from the pool and release it.
279: */
280: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
281: (*pp->pr_free)(ph->ph_page, pp->pr_pagesz, pp->pr_mtype);
282: pp->pr_npages--;
283: pp->pr_npagefree++;
1.6 thorpej 284:
1.22 chs 285: if ((pp->pr_roflags & PR_PHINPAGE) == 0) {
1.27 pk 286: int s;
1.22 chs 287: LIST_REMOVE(ph, ph_hashlist);
1.27 pk 288: s = splhigh();
1.22 chs 289: pool_put(&phpool, ph);
1.27 pk 290: splx(s);
1.22 chs 291: }
292:
1.3 pk 293: if (pp->pr_curpage == ph) {
294: /*
295: * Find a new non-empty page header, if any.
296: * Start search from the page head, to increase the
297: * chance for "high water" pages to be freed.
298: */
299: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
300: ph = TAILQ_NEXT(ph, ph_pagelist))
301: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
302: break;
303:
304: pp->pr_curpage = ph;
1.21 thorpej 305: }
1.3 pk 306: }
307:
308: /*
309: * Allocate and initialize a pool.
310: */
1.1 pk 311: struct pool *
1.42 ! thorpej 312: pool_create(size_t size, u_int align, u_int ioff, int nitems,
! 313: const char *wchan, size_t pagesz,
! 314: void *(*alloc)(unsigned long, int, int),
! 315: void (*release)(void *, unsigned long, int),
! 316: int mtype)
1.1 pk 317: {
318: struct pool *pp;
1.3 pk 319: int flags;
1.1 pk 320:
1.3 pk 321: pp = (struct pool *)malloc(sizeof(*pp), M_POOL, M_NOWAIT);
322: if (pp == NULL)
1.1 pk 323: return (NULL);
1.3 pk 324:
325: flags = PR_FREEHEADER;
326: pool_init(pp, size, align, ioff, flags, wchan, pagesz,
327: alloc, release, mtype);
328:
329: if (nitems != 0) {
330: if (pool_prime(pp, nitems, NULL) != 0) {
331: pool_destroy(pp);
332: return (NULL);
333: }
1.1 pk 334: }
335:
1.3 pk 336: return (pp);
337: }
338:
339: /*
340: * Initialize the given pool resource structure.
341: *
342: * We export this routine to allow other kernel parts to declare
343: * static pools that must be initialized before malloc() is available.
344: */
345: void
1.42 ! thorpej 346: pool_init(struct pool *pp, size_t size, u_int align, u_int ioff, int flags,
! 347: const char *wchan, size_t pagesz,
! 348: void *(*alloc)(unsigned long, int, int),
! 349: void (*release)(void *, unsigned long, int),
! 350: int mtype)
1.3 pk 351: {
1.16 briggs 352: int off, slack, i;
1.3 pk 353:
1.25 thorpej 354: #ifdef POOL_DIAGNOSTIC
355: /*
356: * Always log if POOL_DIAGNOSTIC is defined.
357: */
358: if (pool_logsize != 0)
359: flags |= PR_LOGGING;
360: #endif
361:
1.3 pk 362: /*
363: * Check arguments and construct default values.
364: */
1.36 pk 365: if (!powerof2(pagesz))
1.3 pk 366: panic("pool_init: page size invalid (%lx)\n", (u_long)pagesz);
367:
1.4 thorpej 368: if (alloc == NULL && release == NULL) {
1.3 pk 369: alloc = pool_page_alloc;
370: release = pool_page_free;
1.4 thorpej 371: pagesz = PAGE_SIZE; /* Rounds to PAGE_SIZE anyhow. */
372: } else if ((alloc != NULL && release != NULL) == 0) {
373: /* If you specifiy one, must specify both. */
374: panic("pool_init: must specify alloc and release together");
375: }
376:
1.3 pk 377: if (pagesz == 0)
378: pagesz = PAGE_SIZE;
379:
380: if (align == 0)
381: align = ALIGN(1);
1.14 thorpej 382:
383: if (size < sizeof(struct pool_item))
384: size = sizeof(struct pool_item);
1.3 pk 385:
1.35 pk 386: size = ALIGN(size);
387: if (size >= pagesz)
388: panic("pool_init: pool item size (%lu) too large",
389: (u_long)size);
390:
1.3 pk 391: /*
392: * Initialize the pool structure.
393: */
394: TAILQ_INIT(&pp->pr_pagelist);
395: pp->pr_curpage = NULL;
396: pp->pr_npages = 0;
397: pp->pr_minitems = 0;
398: pp->pr_minpages = 0;
399: pp->pr_maxpages = UINT_MAX;
1.20 thorpej 400: pp->pr_roflags = flags;
401: pp->pr_flags = 0;
1.35 pk 402: pp->pr_size = size;
1.3 pk 403: pp->pr_align = align;
404: pp->pr_wchan = wchan;
405: pp->pr_mtype = mtype;
406: pp->pr_alloc = alloc;
407: pp->pr_free = release;
408: pp->pr_pagesz = pagesz;
409: pp->pr_pagemask = ~(pagesz - 1);
410: pp->pr_pageshift = ffs(pagesz) - 1;
1.20 thorpej 411: pp->pr_nitems = 0;
412: pp->pr_nout = 0;
413: pp->pr_hardlimit = UINT_MAX;
414: pp->pr_hardlimit_warning = NULL;
1.31 thorpej 415: pp->pr_hardlimit_ratecap.tv_sec = 0;
416: pp->pr_hardlimit_ratecap.tv_usec = 0;
417: pp->pr_hardlimit_warning_last.tv_sec = 0;
418: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.3 pk 419:
420: /*
421: * Decide whether to put the page header off page to avoid
422: * wasting too large a part of the page. Off-page page headers
423: * go on a hash table, so we can match a returned item
424: * with its header based on the page address.
425: * We use 1/16 of the page size as the threshold (XXX: tune)
426: */
427: if (pp->pr_size < pagesz/16) {
428: /* Use the end of the page for the page header */
1.20 thorpej 429: pp->pr_roflags |= PR_PHINPAGE;
1.3 pk 430: pp->pr_phoffset = off =
431: pagesz - ALIGN(sizeof(struct pool_item_header));
1.2 pk 432: } else {
1.3 pk 433: /* The page header will be taken from our page header pool */
434: pp->pr_phoffset = 0;
435: off = pagesz;
1.16 briggs 436: for (i = 0; i < PR_HASHTABSIZE; i++) {
437: LIST_INIT(&pp->pr_hashtab[i]);
438: }
1.2 pk 439: }
1.1 pk 440:
1.3 pk 441: /*
442: * Alignment is to take place at `ioff' within the item. This means
443: * we must reserve up to `align - 1' bytes on the page to allow
444: * appropriate positioning of each item.
445: *
446: * Silently enforce `0 <= ioff < align'.
447: */
448: pp->pr_itemoffset = ioff = ioff % align;
449: pp->pr_itemsperpage = (off - ((align - ioff) % align)) / pp->pr_size;
450:
451: /*
452: * Use the slack between the chunks and the page header
453: * for "cache coloring".
454: */
455: slack = off - pp->pr_itemsperpage * pp->pr_size;
456: pp->pr_maxcolor = (slack / align) * align;
457: pp->pr_curcolor = 0;
458:
459: pp->pr_nget = 0;
460: pp->pr_nfail = 0;
461: pp->pr_nput = 0;
462: pp->pr_npagealloc = 0;
463: pp->pr_npagefree = 0;
1.1 pk 464: pp->pr_hiwat = 0;
1.8 thorpej 465: pp->pr_nidle = 0;
1.3 pk 466:
1.25 thorpej 467: if (flags & PR_LOGGING) {
468: if (kmem_map == NULL ||
469: (pp->pr_log = malloc(pool_logsize * sizeof(struct pool_log),
470: M_TEMP, M_NOWAIT)) == NULL)
1.20 thorpej 471: pp->pr_roflags &= ~PR_LOGGING;
1.3 pk 472: pp->pr_curlogentry = 0;
473: pp->pr_logsize = pool_logsize;
474: }
1.25 thorpej 475:
476: pp->pr_entered_file = NULL;
477: pp->pr_entered_line = 0;
1.3 pk 478:
1.21 thorpej 479: simple_lock_init(&pp->pr_slock);
1.1 pk 480:
1.3 pk 481: /*
482: * Initialize private page header pool if we haven't done so yet.
1.23 thorpej 483: * XXX LOCKING.
1.3 pk 484: */
485: if (phpool.pr_size == 0) {
486: pool_init(&phpool, sizeof(struct pool_item_header), 0, 0,
487: 0, "phpool", 0, 0, 0, 0);
1.1 pk 488: }
489:
1.23 thorpej 490: /* Insert into the list of all pools. */
491: simple_lock(&pool_head_slock);
492: TAILQ_INSERT_TAIL(&pool_head, pp, pr_poollist);
493: simple_unlock(&pool_head_slock);
1.1 pk 494: }
495:
496: /*
497: * De-commision a pool resource.
498: */
499: void
1.42 ! thorpej 500: pool_destroy(struct pool *pp)
1.1 pk 501: {
1.3 pk 502: struct pool_item_header *ph;
503:
504: #ifdef DIAGNOSTIC
1.20 thorpej 505: if (pp->pr_nout != 0) {
1.25 thorpej 506: pr_printlog(pp, NULL, printf);
1.20 thorpej 507: panic("pool_destroy: pool busy: still out: %u\n",
508: pp->pr_nout);
1.3 pk 509: }
510: #endif
1.1 pk 511:
1.3 pk 512: /* Remove all pages */
1.20 thorpej 513: if ((pp->pr_roflags & PR_STATIC) == 0)
1.3 pk 514: while ((ph = pp->pr_pagelist.tqh_first) != NULL)
515: pr_rmpage(pp, ph);
516:
517: /* Remove from global pool list */
1.23 thorpej 518: simple_lock(&pool_head_slock);
1.3 pk 519: TAILQ_REMOVE(&pool_head, pp, pr_poollist);
1.23 thorpej 520: /* XXX Only clear this if we were drainpp? */
1.3 pk 521: drainpp = NULL;
1.23 thorpej 522: simple_unlock(&pool_head_slock);
1.3 pk 523:
1.20 thorpej 524: if ((pp->pr_roflags & PR_LOGGING) != 0)
1.3 pk 525: free(pp->pr_log, M_TEMP);
1.2 pk 526:
1.20 thorpej 527: if (pp->pr_roflags & PR_FREEHEADER)
1.3 pk 528: free(pp, M_POOL);
1.1 pk 529: }
530:
531:
532: /*
1.3 pk 533: * Grab an item from the pool; must be called at appropriate spl level
1.1 pk 534: */
1.3 pk 535: void *
1.42 ! thorpej 536: _pool_get(struct pool *pp, int flags, const char *file, long line)
1.1 pk 537: {
538: void *v;
539: struct pool_item *pi;
1.3 pk 540: struct pool_item_header *ph;
1.1 pk 541:
1.2 pk 542: #ifdef DIAGNOSTIC
1.34 thorpej 543: if (__predict_false((pp->pr_roflags & PR_STATIC) &&
544: (flags & PR_MALLOCOK))) {
1.25 thorpej 545: pr_printlog(pp, NULL, printf);
1.2 pk 546: panic("pool_get: static");
1.3 pk 547: }
1.2 pk 548: #endif
549:
1.37 sommerfe 550: if (__predict_false(curproc == NULL && doing_shutdown == 0 &&
551: (flags & PR_WAITOK) != 0))
1.3 pk 552: panic("pool_get: must have NOWAIT");
1.1 pk 553:
1.21 thorpej 554: simple_lock(&pp->pr_slock);
1.25 thorpej 555: pr_enter(pp, file, line);
1.20 thorpej 556:
557: startover:
558: /*
559: * Check to see if we've reached the hard limit. If we have,
560: * and we can wait, then wait until an item has been returned to
561: * the pool.
562: */
563: #ifdef DIAGNOSTIC
1.34 thorpej 564: if (__predict_false(pp->pr_nout > pp->pr_hardlimit)) {
1.25 thorpej 565: pr_leave(pp);
1.21 thorpej 566: simple_unlock(&pp->pr_slock);
1.20 thorpej 567: panic("pool_get: %s: crossed hard limit", pp->pr_wchan);
568: }
569: #endif
1.34 thorpej 570: if (__predict_false(pp->pr_nout == pp->pr_hardlimit)) {
1.29 sommerfe 571: if ((flags & PR_WAITOK) && !(flags & PR_LIMITFAIL)) {
1.20 thorpej 572: /*
573: * XXX: A warning isn't logged in this case. Should
574: * it be?
575: */
576: pp->pr_flags |= PR_WANTED;
1.25 thorpej 577: pr_leave(pp);
1.40 sommerfe 578: ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25 thorpej 579: pr_enter(pp, file, line);
1.20 thorpej 580: goto startover;
581: }
1.31 thorpej 582:
583: /*
584: * Log a message that the hard limit has been hit.
585: */
586: if (pp->pr_hardlimit_warning != NULL &&
587: ratecheck(&pp->pr_hardlimit_warning_last,
588: &pp->pr_hardlimit_ratecap))
589: log(LOG_ERR, "%s\n", pp->pr_hardlimit_warning);
1.21 thorpej 590:
591: if (flags & PR_URGENT)
592: panic("pool_get: urgent");
593:
594: pp->pr_nfail++;
595:
1.25 thorpej 596: pr_leave(pp);
1.21 thorpej 597: simple_unlock(&pp->pr_slock);
1.20 thorpej 598: return (NULL);
599: }
600:
1.3 pk 601: /*
602: * The convention we use is that if `curpage' is not NULL, then
603: * it points at a non-empty bucket. In particular, `curpage'
604: * never points at a page header which has PR_PHINPAGE set and
605: * has no items in its bucket.
606: */
1.20 thorpej 607: if ((ph = pp->pr_curpage) == NULL) {
1.15 pk 608: void *v;
609:
1.20 thorpej 610: #ifdef DIAGNOSTIC
611: if (pp->pr_nitems != 0) {
1.21 thorpej 612: simple_unlock(&pp->pr_slock);
1.20 thorpej 613: printf("pool_get: %s: curpage NULL, nitems %u\n",
614: pp->pr_wchan, pp->pr_nitems);
615: panic("pool_get: nitems inconsistent\n");
616: }
617: #endif
618:
1.21 thorpej 619: /*
620: * Call the back-end page allocator for more memory.
621: * Release the pool lock, as the back-end page allocator
622: * may block.
623: */
1.25 thorpej 624: pr_leave(pp);
1.21 thorpej 625: simple_unlock(&pp->pr_slock);
626: v = (*pp->pr_alloc)(pp->pr_pagesz, flags, pp->pr_mtype);
627: simple_lock(&pp->pr_slock);
1.25 thorpej 628: pr_enter(pp, file, line);
1.15 pk 629:
1.21 thorpej 630: if (v == NULL) {
631: /*
632: * We were unable to allocate a page, but
633: * we released the lock during allocation,
634: * so perhaps items were freed back to the
635: * pool. Check for this case.
636: */
637: if (pp->pr_curpage != NULL)
638: goto startover;
1.15 pk 639:
1.3 pk 640: if (flags & PR_URGENT)
641: panic("pool_get: urgent");
1.21 thorpej 642:
1.3 pk 643: if ((flags & PR_WAITOK) == 0) {
644: pp->pr_nfail++;
1.25 thorpej 645: pr_leave(pp);
1.21 thorpej 646: simple_unlock(&pp->pr_slock);
1.1 pk 647: return (NULL);
1.3 pk 648: }
649:
1.15 pk 650: /*
651: * Wait for items to be returned to this pool.
1.21 thorpej 652: *
1.15 pk 653: * XXX: we actually want to wait just until
654: * the page allocator has memory again. Depending
655: * on this pool's usage, we might get stuck here
656: * for a long time.
1.20 thorpej 657: *
658: * XXX: maybe we should wake up once a second and
659: * try again?
1.15 pk 660: */
1.1 pk 661: pp->pr_flags |= PR_WANTED;
1.25 thorpej 662: pr_leave(pp);
1.40 sommerfe 663: ltsleep(pp, PSWP, pp->pr_wchan, 0, &pp->pr_slock);
1.25 thorpej 664: pr_enter(pp, file, line);
1.20 thorpej 665: goto startover;
1.1 pk 666: }
1.3 pk 667:
1.15 pk 668: /* We have more memory; add it to the pool */
669: pp->pr_npagealloc++;
670: pool_prime_page(pp, v);
671:
1.20 thorpej 672: /* Start the allocation process over. */
673: goto startover;
1.3 pk 674: }
675:
1.34 thorpej 676: if (__predict_false((v = pi = TAILQ_FIRST(&ph->ph_itemlist)) == NULL)) {
1.25 thorpej 677: pr_leave(pp);
1.21 thorpej 678: simple_unlock(&pp->pr_slock);
1.3 pk 679: panic("pool_get: %s: page empty", pp->pr_wchan);
1.21 thorpej 680: }
1.20 thorpej 681: #ifdef DIAGNOSTIC
1.34 thorpej 682: if (__predict_false(pp->pr_nitems == 0)) {
1.25 thorpej 683: pr_leave(pp);
1.21 thorpej 684: simple_unlock(&pp->pr_slock);
1.20 thorpej 685: printf("pool_get: %s: items on itemlist, nitems %u\n",
686: pp->pr_wchan, pp->pr_nitems);
687: panic("pool_get: nitems inconsistent\n");
688: }
689: #endif
1.3 pk 690: pr_log(pp, v, PRLOG_GET, file, line);
691:
692: #ifdef DIAGNOSTIC
1.34 thorpej 693: if (__predict_false(pi->pi_magic != PI_MAGIC)) {
1.25 thorpej 694: pr_printlog(pp, pi, printf);
1.3 pk 695: panic("pool_get(%s): free list modified: magic=%x; page %p;"
696: " item addr %p\n",
697: pp->pr_wchan, pi->pi_magic, ph->ph_page, pi);
698: }
699: #endif
700:
701: /*
702: * Remove from item list.
703: */
704: TAILQ_REMOVE(&ph->ph_itemlist, pi, pi_list);
1.20 thorpej 705: pp->pr_nitems--;
706: pp->pr_nout++;
1.6 thorpej 707: if (ph->ph_nmissing == 0) {
708: #ifdef DIAGNOSTIC
1.34 thorpej 709: if (__predict_false(pp->pr_nidle == 0))
1.6 thorpej 710: panic("pool_get: nidle inconsistent");
711: #endif
712: pp->pr_nidle--;
713: }
1.3 pk 714: ph->ph_nmissing++;
715: if (TAILQ_FIRST(&ph->ph_itemlist) == NULL) {
1.21 thorpej 716: #ifdef DIAGNOSTIC
1.34 thorpej 717: if (__predict_false(ph->ph_nmissing != pp->pr_itemsperpage)) {
1.25 thorpej 718: pr_leave(pp);
1.21 thorpej 719: simple_unlock(&pp->pr_slock);
720: panic("pool_get: %s: nmissing inconsistent",
721: pp->pr_wchan);
722: }
723: #endif
1.3 pk 724: /*
725: * Find a new non-empty page header, if any.
726: * Start search from the page head, to increase
727: * the chance for "high water" pages to be freed.
728: *
1.21 thorpej 729: * Migrate empty pages to the end of the list. This
730: * will speed the update of curpage as pages become
731: * idle. Empty pages intermingled with idle pages
732: * is no big deal. As soon as a page becomes un-empty,
733: * it will move back to the head of the list.
1.3 pk 734: */
735: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
1.21 thorpej 736: TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
737: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
738: ph = TAILQ_NEXT(ph, ph_pagelist))
1.3 pk 739: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
740: break;
741:
742: pp->pr_curpage = ph;
1.1 pk 743: }
1.3 pk 744:
745: pp->pr_nget++;
1.20 thorpej 746:
747: /*
748: * If we have a low water mark and we are now below that low
749: * water mark, add more items to the pool.
750: */
751: if (pp->pr_nitems < pp->pr_minitems && pool_catchup(pp) != 0) {
752: /*
753: * XXX: Should we log a warning? Should we set up a timeout
754: * to try again in a second or so? The latter could break
755: * a caller's assumptions about interrupt protection, etc.
756: */
757: }
758:
1.25 thorpej 759: pr_leave(pp);
1.21 thorpej 760: simple_unlock(&pp->pr_slock);
1.1 pk 761: return (v);
762: }
763:
764: /*
1.3 pk 765: * Return resource to the pool; must be called at appropriate spl level
1.1 pk 766: */
1.3 pk 767: void
1.42 ! thorpej 768: _pool_put(struct pool *pp, void *v, const char *file, long line)
1.1 pk 769: {
770: struct pool_item *pi = v;
1.3 pk 771: struct pool_item_header *ph;
772: caddr_t page;
1.21 thorpej 773: int s;
1.3 pk 774:
775: page = (caddr_t)((u_long)v & pp->pr_pagemask);
1.1 pk 776:
1.21 thorpej 777: simple_lock(&pp->pr_slock);
1.25 thorpej 778: pr_enter(pp, file, line);
1.30 thorpej 779:
780: #ifdef DIAGNOSTIC
1.34 thorpej 781: if (__predict_false(pp->pr_nout == 0)) {
1.30 thorpej 782: printf("pool %s: putting with none out\n",
783: pp->pr_wchan);
784: panic("pool_put");
785: }
786: #endif
1.3 pk 787:
788: pr_log(pp, v, PRLOG_PUT, file, line);
789:
1.34 thorpej 790: if (__predict_false((ph = pr_find_pagehead(pp, page)) == NULL)) {
1.25 thorpej 791: pr_printlog(pp, NULL, printf);
1.3 pk 792: panic("pool_put: %s: page header missing", pp->pr_wchan);
793: }
1.28 thorpej 794:
795: #ifdef LOCKDEBUG
796: /*
797: * Check if we're freeing a locked simple lock.
798: */
799: simple_lock_freecheck((caddr_t)pi, ((caddr_t)pi) + pp->pr_size);
800: #endif
1.3 pk 801:
802: /*
803: * Return to item list.
804: */
1.2 pk 805: #ifdef DIAGNOSTIC
1.3 pk 806: pi->pi_magic = PI_MAGIC;
807: #endif
1.32 chs 808: #ifdef DEBUG
809: {
810: int i, *ip = v;
811:
812: for (i = 0; i < pp->pr_size / sizeof(int); i++) {
813: *ip++ = PI_MAGIC;
814: }
815: }
816: #endif
817:
1.3 pk 818: TAILQ_INSERT_HEAD(&ph->ph_itemlist, pi, pi_list);
819: ph->ph_nmissing--;
820: pp->pr_nput++;
1.20 thorpej 821: pp->pr_nitems++;
822: pp->pr_nout--;
1.3 pk 823:
824: /* Cancel "pool empty" condition if it exists */
825: if (pp->pr_curpage == NULL)
826: pp->pr_curpage = ph;
827:
828: if (pp->pr_flags & PR_WANTED) {
829: pp->pr_flags &= ~PR_WANTED;
1.15 pk 830: if (ph->ph_nmissing == 0)
831: pp->pr_nidle++;
1.25 thorpej 832: pr_leave(pp);
1.21 thorpej 833: simple_unlock(&pp->pr_slock);
1.3 pk 834: wakeup((caddr_t)pp);
835: return;
836: }
837:
838: /*
1.21 thorpej 839: * If this page is now complete, do one of two things:
840: *
841: * (1) If we have more pages than the page high water
842: * mark, free the page back to the system.
843: *
844: * (2) Move it to the end of the page list, so that
845: * we minimize our chances of fragmenting the
846: * pool. Idle pages migrate to the end (along with
847: * completely empty pages, so that we find un-empty
848: * pages more quickly when we update curpage) of the
849: * list so they can be more easily swept up by
850: * the pagedaemon when pages are scarce.
1.3 pk 851: */
852: if (ph->ph_nmissing == 0) {
1.6 thorpej 853: pp->pr_nidle++;
1.3 pk 854: if (pp->pr_npages > pp->pr_maxpages) {
855: pr_rmpage(pp, ph);
856: } else {
857: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
858: TAILQ_INSERT_TAIL(&pp->pr_pagelist, ph, ph_pagelist);
859:
1.21 thorpej 860: /*
861: * Update the timestamp on the page. A page must
862: * be idle for some period of time before it can
863: * be reclaimed by the pagedaemon. This minimizes
864: * ping-pong'ing for memory.
865: */
866: s = splclock();
867: ph->ph_time = mono_time;
868: splx(s);
869:
870: /*
871: * Update the current page pointer. Just look for
872: * the first page with any free items.
873: *
874: * XXX: Maybe we want an option to look for the
875: * page with the fewest available items, to minimize
876: * fragmentation?
877: */
1.3 pk 878: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
879: ph = TAILQ_NEXT(ph, ph_pagelist))
880: if (TAILQ_FIRST(&ph->ph_itemlist) != NULL)
881: break;
1.1 pk 882:
1.3 pk 883: pp->pr_curpage = ph;
1.1 pk 884: }
885: }
1.21 thorpej 886: /*
887: * If the page has just become un-empty, move it to the head of
888: * the list, and make it the current page. The next allocation
889: * will get the item from this page, instead of further fragmenting
890: * the pool.
891: */
892: else if (ph->ph_nmissing == (pp->pr_itemsperpage - 1)) {
893: TAILQ_REMOVE(&pp->pr_pagelist, ph, ph_pagelist);
894: TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
895: pp->pr_curpage = ph;
896: }
897:
1.25 thorpej 898: pr_leave(pp);
1.21 thorpej 899: simple_unlock(&pp->pr_slock);
1.3 pk 900:
1.1 pk 901: }
902:
903: /*
1.3 pk 904: * Add N items to the pool.
1.1 pk 905: */
906: int
1.42 ! thorpej 907: pool_prime(struct pool *pp, int n, caddr_t storage)
1.1 pk 908: {
1.3 pk 909: caddr_t cp;
910: int newnitems, newpages;
1.2 pk 911:
912: #ifdef DIAGNOSTIC
1.34 thorpej 913: if (__predict_false(storage && !(pp->pr_roflags & PR_STATIC)))
1.2 pk 914: panic("pool_prime: static");
915: /* !storage && static caught below */
916: #endif
1.1 pk 917:
1.21 thorpej 918: simple_lock(&pp->pr_slock);
919:
1.3 pk 920: newnitems = pp->pr_minitems + n;
921: newpages =
1.18 thorpej 922: roundup(newnitems, pp->pr_itemsperpage) / pp->pr_itemsperpage
1.3 pk 923: - pp->pr_minpages;
924:
925: while (newpages-- > 0) {
1.20 thorpej 926: if (pp->pr_roflags & PR_STATIC) {
1.3 pk 927: cp = storage;
928: storage += pp->pr_pagesz;
929: } else {
1.21 thorpej 930: simple_unlock(&pp->pr_slock);
1.3 pk 931: cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1.21 thorpej 932: simple_lock(&pp->pr_slock);
1.3 pk 933: }
1.2 pk 934:
1.3 pk 935: if (cp == NULL) {
1.21 thorpej 936: simple_unlock(&pp->pr_slock);
1.1 pk 937: return (ENOMEM);
938: }
939:
1.26 thorpej 940: pp->pr_npagealloc++;
1.3 pk 941: pool_prime_page(pp, cp);
942: pp->pr_minpages++;
1.1 pk 943: }
1.3 pk 944:
945: pp->pr_minitems = newnitems;
946:
947: if (pp->pr_minpages >= pp->pr_maxpages)
948: pp->pr_maxpages = pp->pr_minpages + 1; /* XXX */
949:
1.21 thorpej 950: simple_unlock(&pp->pr_slock);
1.1 pk 951: return (0);
952: }
1.3 pk 953:
954: /*
955: * Add a page worth of items to the pool.
1.21 thorpej 956: *
957: * Note, we must be called with the pool descriptor LOCKED.
1.3 pk 958: */
1.21 thorpej 959: static void
1.42 ! thorpej 960: pool_prime_page(struct pool *pp, caddr_t storage)
1.3 pk 961: {
962: struct pool_item *pi;
963: struct pool_item_header *ph;
964: caddr_t cp = storage;
965: unsigned int align = pp->pr_align;
966: unsigned int ioff = pp->pr_itemoffset;
1.27 pk 967: int s, n;
1.36 pk 968:
969: if (((u_long)cp & (pp->pr_pagesz - 1)) != 0)
970: panic("pool_prime_page: %s: unaligned page", pp->pr_wchan);
1.3 pk 971:
1.20 thorpej 972: if ((pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3 pk 973: ph = (struct pool_item_header *)(cp + pp->pr_phoffset);
974: } else {
1.27 pk 975: s = splhigh();
1.3 pk 976: ph = pool_get(&phpool, PR_URGENT);
1.27 pk 977: splx(s);
1.3 pk 978: LIST_INSERT_HEAD(&pp->pr_hashtab[PR_HASH_INDEX(pp, cp)],
979: ph, ph_hashlist);
980: }
981:
982: /*
983: * Insert page header.
984: */
985: TAILQ_INSERT_HEAD(&pp->pr_pagelist, ph, ph_pagelist);
986: TAILQ_INIT(&ph->ph_itemlist);
987: ph->ph_page = storage;
988: ph->ph_nmissing = 0;
1.21 thorpej 989: memset(&ph->ph_time, 0, sizeof(ph->ph_time));
1.3 pk 990:
1.6 thorpej 991: pp->pr_nidle++;
992:
1.3 pk 993: /*
994: * Color this page.
995: */
996: cp = (caddr_t)(cp + pp->pr_curcolor);
997: if ((pp->pr_curcolor += align) > pp->pr_maxcolor)
998: pp->pr_curcolor = 0;
999:
1000: /*
1001: * Adjust storage to apply aligment to `pr_itemoffset' in each item.
1002: */
1003: if (ioff != 0)
1004: cp = (caddr_t)(cp + (align - ioff));
1005:
1006: /*
1007: * Insert remaining chunks on the bucket list.
1008: */
1009: n = pp->pr_itemsperpage;
1.20 thorpej 1010: pp->pr_nitems += n;
1.3 pk 1011:
1012: while (n--) {
1013: pi = (struct pool_item *)cp;
1014:
1015: /* Insert on page list */
1016: TAILQ_INSERT_TAIL(&ph->ph_itemlist, pi, pi_list);
1017: #ifdef DIAGNOSTIC
1018: pi->pi_magic = PI_MAGIC;
1019: #endif
1020: cp = (caddr_t)(cp + pp->pr_size);
1021: }
1022:
1023: /*
1024: * If the pool was depleted, point at the new page.
1025: */
1026: if (pp->pr_curpage == NULL)
1027: pp->pr_curpage = ph;
1028:
1029: if (++pp->pr_npages > pp->pr_hiwat)
1030: pp->pr_hiwat = pp->pr_npages;
1031: }
1032:
1.20 thorpej 1033: /*
1034: * Like pool_prime(), except this is used by pool_get() when nitems
1035: * drops below the low water mark. This is used to catch up nitmes
1036: * with the low water mark.
1037: *
1.21 thorpej 1038: * Note 1, we never wait for memory here, we let the caller decide what to do.
1.20 thorpej 1039: *
1040: * Note 2, this doesn't work with static pools.
1041: *
1042: * Note 3, we must be called with the pool already locked, and we return
1043: * with it locked.
1044: */
1045: static int
1.42 ! thorpej 1046: pool_catchup(struct pool *pp)
1.20 thorpej 1047: {
1048: caddr_t cp;
1049: int error = 0;
1050:
1051: if (pp->pr_roflags & PR_STATIC) {
1052: /*
1053: * We dropped below the low water mark, and this is not a
1054: * good thing. Log a warning.
1.21 thorpej 1055: *
1056: * XXX: rate-limit this?
1.20 thorpej 1057: */
1058: printf("WARNING: static pool `%s' dropped below low water "
1059: "mark\n", pp->pr_wchan);
1060: return (0);
1061: }
1062:
1.21 thorpej 1063: while (pp->pr_nitems < pp->pr_minitems) {
1.20 thorpej 1064: /*
1.21 thorpej 1065: * Call the page back-end allocator for more memory.
1066: *
1067: * XXX: We never wait, so should we bother unlocking
1068: * the pool descriptor?
1.20 thorpej 1069: */
1.21 thorpej 1070: simple_unlock(&pp->pr_slock);
1.20 thorpej 1071: cp = (*pp->pr_alloc)(pp->pr_pagesz, 0, pp->pr_mtype);
1.21 thorpej 1072: simple_lock(&pp->pr_slock);
1.34 thorpej 1073: if (__predict_false(cp == NULL)) {
1.20 thorpej 1074: error = ENOMEM;
1075: break;
1076: }
1.26 thorpej 1077: pp->pr_npagealloc++;
1.20 thorpej 1078: pool_prime_page(pp, cp);
1079: }
1080:
1081: return (error);
1082: }
1083:
1.3 pk 1084: void
1.42 ! thorpej 1085: pool_setlowat(struct pool *pp, int n)
1.3 pk 1086: {
1.20 thorpej 1087: int error;
1.15 pk 1088:
1.21 thorpej 1089: simple_lock(&pp->pr_slock);
1090:
1.3 pk 1091: pp->pr_minitems = n;
1.15 pk 1092: pp->pr_minpages = (n == 0)
1093: ? 0
1.18 thorpej 1094: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.20 thorpej 1095:
1096: /* Make sure we're caught up with the newly-set low water mark. */
1.41 sommerfe 1097: if ((pp->pr_nitems < pp->pr_minitems) &&
1098: (error = pool_catchup(pp)) != 0) {
1.20 thorpej 1099: /*
1100: * XXX: Should we log a warning? Should we set up a timeout
1101: * to try again in a second or so? The latter could break
1102: * a caller's assumptions about interrupt protection, etc.
1103: */
1104: }
1.21 thorpej 1105:
1106: simple_unlock(&pp->pr_slock);
1.3 pk 1107: }
1108:
1109: void
1.42 ! thorpej 1110: pool_sethiwat(struct pool *pp, int n)
1.3 pk 1111: {
1.15 pk 1112:
1.21 thorpej 1113: simple_lock(&pp->pr_slock);
1114:
1.15 pk 1115: pp->pr_maxpages = (n == 0)
1116: ? 0
1.18 thorpej 1117: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1118:
1119: simple_unlock(&pp->pr_slock);
1.3 pk 1120: }
1121:
1.20 thorpej 1122: void
1.42 ! thorpej 1123: pool_sethardlimit(struct pool *pp, int n, const char *warnmess, int ratecap)
1.20 thorpej 1124: {
1125:
1.21 thorpej 1126: simple_lock(&pp->pr_slock);
1.20 thorpej 1127:
1128: pp->pr_hardlimit = n;
1129: pp->pr_hardlimit_warning = warnmess;
1.31 thorpej 1130: pp->pr_hardlimit_ratecap.tv_sec = ratecap;
1131: pp->pr_hardlimit_warning_last.tv_sec = 0;
1132: pp->pr_hardlimit_warning_last.tv_usec = 0;
1.20 thorpej 1133:
1134: /*
1.21 thorpej 1135: * In-line version of pool_sethiwat(), because we don't want to
1136: * release the lock.
1.20 thorpej 1137: */
1138: pp->pr_maxpages = (n == 0)
1139: ? 0
1140: : roundup(n, pp->pr_itemsperpage) / pp->pr_itemsperpage;
1.21 thorpej 1141:
1142: simple_unlock(&pp->pr_slock);
1.20 thorpej 1143: }
1.3 pk 1144:
1145: /*
1146: * Default page allocator.
1147: */
1148: static void *
1.42 ! thorpej 1149: pool_page_alloc(unsigned long sz, int flags, int mtype)
1.3 pk 1150: {
1.11 thorpej 1151: boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1.3 pk 1152:
1.11 thorpej 1153: return ((void *)uvm_km_alloc_poolpage(waitok));
1.3 pk 1154: }
1155:
1156: static void
1.42 ! thorpej 1157: pool_page_free(void *v, unsigned long sz, int mtype)
1.3 pk 1158: {
1159:
1.10 eeh 1160: uvm_km_free_poolpage((vaddr_t)v);
1.3 pk 1161: }
1.12 thorpej 1162:
1163: /*
1164: * Alternate pool page allocator for pools that know they will
1165: * never be accessed in interrupt context.
1166: */
1167: void *
1.42 ! thorpej 1168: pool_page_alloc_nointr(unsigned long sz, int flags, int mtype)
1.12 thorpej 1169: {
1170: boolean_t waitok = (flags & PR_WAITOK) ? TRUE : FALSE;
1171:
1172: return ((void *)uvm_km_alloc_poolpage1(kernel_map, uvm.kernel_object,
1173: waitok));
1174: }
1175:
1176: void
1.42 ! thorpej 1177: pool_page_free_nointr(void *v, unsigned long sz, int mtype)
1.12 thorpej 1178: {
1179:
1180: uvm_km_free_poolpage1(kernel_map, (vaddr_t)v);
1181: }
1182:
1.3 pk 1183:
1184: /*
1185: * Release all complete pages that have not been used recently.
1186: */
1187: void
1.42 ! thorpej 1188: _pool_reclaim(struct pool *pp, const char *file, long line)
1.3 pk 1189: {
1190: struct pool_item_header *ph, *phnext;
1.21 thorpej 1191: struct timeval curtime;
1192: int s;
1.3 pk 1193:
1.20 thorpej 1194: if (pp->pr_roflags & PR_STATIC)
1.3 pk 1195: return;
1196:
1.21 thorpej 1197: if (simple_lock_try(&pp->pr_slock) == 0)
1.3 pk 1198: return;
1.25 thorpej 1199: pr_enter(pp, file, line);
1.3 pk 1200:
1.21 thorpej 1201: s = splclock();
1202: curtime = mono_time;
1203: splx(s);
1204:
1.3 pk 1205: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL; ph = phnext) {
1206: phnext = TAILQ_NEXT(ph, ph_pagelist);
1207:
1208: /* Check our minimum page claim */
1209: if (pp->pr_npages <= pp->pr_minpages)
1210: break;
1211:
1212: if (ph->ph_nmissing == 0) {
1213: struct timeval diff;
1214: timersub(&curtime, &ph->ph_time, &diff);
1215: if (diff.tv_sec < pool_inactive_time)
1216: continue;
1.21 thorpej 1217:
1218: /*
1219: * If freeing this page would put us below
1220: * the low water mark, stop now.
1221: */
1222: if ((pp->pr_nitems - pp->pr_itemsperpage) <
1223: pp->pr_minitems)
1224: break;
1225:
1.3 pk 1226: pr_rmpage(pp, ph);
1227: }
1228: }
1229:
1.25 thorpej 1230: pr_leave(pp);
1.21 thorpej 1231: simple_unlock(&pp->pr_slock);
1.3 pk 1232: }
1233:
1234:
1235: /*
1236: * Drain pools, one at a time.
1.21 thorpej 1237: *
1238: * Note, we must never be called from an interrupt context.
1.3 pk 1239: */
1240: void
1.42 ! thorpej 1241: pool_drain(void *arg)
1.3 pk 1242: {
1243: struct pool *pp;
1.23 thorpej 1244: int s;
1.3 pk 1245:
1.23 thorpej 1246: s = splimp();
1247: simple_lock(&pool_head_slock);
1248:
1249: if (drainpp == NULL && (drainpp = TAILQ_FIRST(&pool_head)) == NULL)
1250: goto out;
1.3 pk 1251:
1252: pp = drainpp;
1253: drainpp = TAILQ_NEXT(pp, pr_poollist);
1254:
1255: pool_reclaim(pp);
1.23 thorpej 1256:
1257: out:
1258: simple_unlock(&pool_head_slock);
1.3 pk 1259: splx(s);
1260: }
1261:
1262:
1263: /*
1264: * Diagnostic helpers.
1265: */
1266: void
1.42 ! thorpej 1267: pool_print(struct pool *pp, const char *modif)
1.21 thorpej 1268: {
1269: int s;
1270:
1271: s = splimp();
1.25 thorpej 1272: if (simple_lock_try(&pp->pr_slock) == 0) {
1273: printf("pool %s is locked; try again later\n",
1274: pp->pr_wchan);
1275: splx(s);
1276: return;
1277: }
1278: pool_print1(pp, modif, printf);
1.21 thorpej 1279: simple_unlock(&pp->pr_slock);
1280: splx(s);
1281: }
1282:
1.25 thorpej 1283: void
1.42 ! thorpej 1284: pool_printit(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.25 thorpej 1285: {
1286: int didlock = 0;
1287:
1288: if (pp == NULL) {
1289: (*pr)("Must specify a pool to print.\n");
1290: return;
1291: }
1292:
1293: /*
1294: * Called from DDB; interrupts should be blocked, and all
1295: * other processors should be paused. We can skip locking
1296: * the pool in this case.
1297: *
1298: * We do a simple_lock_try() just to print the lock
1299: * status, however.
1300: */
1301:
1302: if (simple_lock_try(&pp->pr_slock) == 0)
1303: (*pr)("WARNING: pool %s is locked\n", pp->pr_wchan);
1304: else
1305: didlock = 1;
1306:
1307: pool_print1(pp, modif, pr);
1308:
1309: if (didlock)
1310: simple_unlock(&pp->pr_slock);
1311: }
1312:
1.21 thorpej 1313: static void
1.42 ! thorpej 1314: pool_print1(struct pool *pp, const char *modif, void (*pr)(const char *, ...))
1.3 pk 1315: {
1.25 thorpej 1316: struct pool_item_header *ph;
1317: #ifdef DIAGNOSTIC
1318: struct pool_item *pi;
1319: #endif
1320: int print_log = 0, print_pagelist = 0;
1321: char c;
1322:
1323: while ((c = *modif++) != '\0') {
1324: if (c == 'l')
1325: print_log = 1;
1326: if (c == 'p')
1327: print_pagelist = 1;
1328: modif++;
1329: }
1330:
1331: (*pr)("POOL %s: size %u, align %u, ioff %u, roflags 0x%08x\n",
1332: pp->pr_wchan, pp->pr_size, pp->pr_align, pp->pr_itemoffset,
1333: pp->pr_roflags);
1334: (*pr)("\tpagesz %u, mtype %d\n", pp->pr_pagesz, pp->pr_mtype);
1335: (*pr)("\talloc %p, release %p\n", pp->pr_alloc, pp->pr_free);
1336: (*pr)("\tminitems %u, minpages %u, maxpages %u, npages %u\n",
1337: pp->pr_minitems, pp->pr_minpages, pp->pr_maxpages, pp->pr_npages);
1338: (*pr)("\titemsperpage %u, nitems %u, nout %u, hardlimit %u\n",
1339: pp->pr_itemsperpage, pp->pr_nitems, pp->pr_nout, pp->pr_hardlimit);
1340:
1341: (*pr)("\n\tnget %lu, nfail %lu, nput %lu\n",
1342: pp->pr_nget, pp->pr_nfail, pp->pr_nput);
1343: (*pr)("\tnpagealloc %lu, npagefree %lu, hiwat %u, nidle %lu\n",
1344: pp->pr_npagealloc, pp->pr_npagefree, pp->pr_hiwat, pp->pr_nidle);
1345:
1346: if (print_pagelist == 0)
1347: goto skip_pagelist;
1348:
1349: if ((ph = TAILQ_FIRST(&pp->pr_pagelist)) != NULL)
1350: (*pr)("\n\tpage list:\n");
1351: for (; ph != NULL; ph = TAILQ_NEXT(ph, ph_pagelist)) {
1352: (*pr)("\t\tpage %p, nmissing %d, time %lu,%lu\n",
1353: ph->ph_page, ph->ph_nmissing,
1354: (u_long)ph->ph_time.tv_sec,
1355: (u_long)ph->ph_time.tv_usec);
1356: #ifdef DIAGNOSTIC
1357: for (pi = TAILQ_FIRST(&ph->ph_itemlist); pi != NULL;
1358: pi = TAILQ_NEXT(pi, pi_list)) {
1359: if (pi->pi_magic != PI_MAGIC) {
1360: (*pr)("\t\t\titem %p, magic 0x%x\n",
1361: pi, pi->pi_magic);
1362: }
1363: }
1364: #endif
1365: }
1366: if (pp->pr_curpage == NULL)
1367: (*pr)("\tno current page\n");
1368: else
1369: (*pr)("\tcurpage %p\n", pp->pr_curpage->ph_page);
1370:
1371: skip_pagelist:
1372:
1373: if (print_log == 0)
1374: goto skip_log;
1375:
1376: (*pr)("\n");
1377: if ((pp->pr_roflags & PR_LOGGING) == 0)
1378: (*pr)("\tno log\n");
1379: else
1380: pr_printlog(pp, NULL, pr);
1.3 pk 1381:
1.25 thorpej 1382: skip_log:
1.3 pk 1383:
1.25 thorpej 1384: pr_enter_check(pp, pr);
1.3 pk 1385: }
1386:
1387: int
1.42 ! thorpej 1388: pool_chk(struct pool *pp, const char *label)
1.3 pk 1389: {
1390: struct pool_item_header *ph;
1391: int r = 0;
1392:
1.21 thorpej 1393: simple_lock(&pp->pr_slock);
1.3 pk 1394:
1395: for (ph = TAILQ_FIRST(&pp->pr_pagelist); ph != NULL;
1396: ph = TAILQ_NEXT(ph, ph_pagelist)) {
1397:
1398: struct pool_item *pi;
1399: int n;
1400: caddr_t page;
1401:
1402: page = (caddr_t)((u_long)ph & pp->pr_pagemask);
1.20 thorpej 1403: if (page != ph->ph_page &&
1404: (pp->pr_roflags & PR_PHINPAGE) != 0) {
1.3 pk 1405: if (label != NULL)
1406: printf("%s: ", label);
1.16 briggs 1407: printf("pool(%p:%s): page inconsistency: page %p;"
1408: " at page head addr %p (p %p)\n", pp,
1.3 pk 1409: pp->pr_wchan, ph->ph_page,
1410: ph, page);
1411: r++;
1412: goto out;
1413: }
1414:
1415: for (pi = TAILQ_FIRST(&ph->ph_itemlist), n = 0;
1416: pi != NULL;
1417: pi = TAILQ_NEXT(pi,pi_list), n++) {
1418:
1419: #ifdef DIAGNOSTIC
1420: if (pi->pi_magic != PI_MAGIC) {
1421: if (label != NULL)
1422: printf("%s: ", label);
1423: printf("pool(%s): free list modified: magic=%x;"
1424: " page %p; item ordinal %d;"
1425: " addr %p (p %p)\n",
1426: pp->pr_wchan, pi->pi_magic, ph->ph_page,
1427: n, pi, page);
1428: panic("pool");
1429: }
1430: #endif
1431: page = (caddr_t)((u_long)pi & pp->pr_pagemask);
1432: if (page == ph->ph_page)
1433: continue;
1434:
1435: if (label != NULL)
1436: printf("%s: ", label);
1.16 briggs 1437: printf("pool(%p:%s): page inconsistency: page %p;"
1438: " item ordinal %d; addr %p (p %p)\n", pp,
1.3 pk 1439: pp->pr_wchan, ph->ph_page,
1440: n, pi, page);
1441: r++;
1442: goto out;
1443: }
1444: }
1445: out:
1.21 thorpej 1446: simple_unlock(&pp->pr_slock);
1.3 pk 1447: return (r);
1448: }
CVSweb <webmaster@jp.NetBSD.org>