FreeBSD/Linux Kernel Cross Reference
sys/vm/uma_core.c
1 /*-
2 * Copyright (c) 2002-2005, 2009 Jeffrey Roberson <jeff@FreeBSD.org>
3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4 * Copyright (c) 2004-2006 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * uma_core.c Implementation of the Universal Memory allocator
31 *
32 * This allocator is intended to replace the multitude of similar object caches
33 * in the standard FreeBSD kernel. The intent is to be flexible as well as
34 * effecient. A primary design goal is to return unused memory to the rest of
35 * the system. This will make the system as a whole more flexible due to the
36 * ability to move memory to subsystems which most need it instead of leaving
37 * pools of reserved memory unused.
38 *
39 * The basic ideas stem from similar slab/zone based allocators whose algorithms
40 * are well known.
41 *
42 */
43
44 /*
45 * TODO:
46 * - Improve memory usage for large allocations
47 * - Investigate cache size adjustments
48 */
49
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD: releng/8.2/sys/vm/uma_core.c 215529 2010-11-19 16:52:18Z jhb $");
52
53 /* I should really use ktr.. */
54 /*
55 #define UMA_DEBUG 1
56 #define UMA_DEBUG_ALLOC 1
57 #define UMA_DEBUG_ALLOC_1 1
58 */
59
60 #include "opt_ddb.h"
61 #include "opt_param.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/malloc.h>
69 #include <sys/ktr.h>
70 #include <sys/lock.h>
71 #include <sys/sysctl.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/sbuf.h>
75 #include <sys/smp.h>
76 #include <sys/vmmeter.h>
77
78 #include <vm/vm.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_extern.h>
85 #include <vm/uma.h>
86 #include <vm/uma_int.h>
87 #include <vm/uma_dbg.h>
88
89 #include <machine/vmparam.h>
90
91 #include <ddb/ddb.h>
92
93 /*
94 * This is the zone and keg from which all zones are spawned. The idea is that
95 * even the zone & keg heads are allocated from the allocator, so we use the
96 * bss section to bootstrap us.
97 */
98 static struct uma_keg masterkeg;
99 static struct uma_zone masterzone_k;
100 static struct uma_zone masterzone_z;
101 static uma_zone_t kegs = &masterzone_k;
102 static uma_zone_t zones = &masterzone_z;
103
104 /* This is the zone from which all of uma_slab_t's are allocated. */
105 static uma_zone_t slabzone;
106 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
107
108 /*
109 * The initial hash tables come out of this zone so they can be allocated
110 * prior to malloc coming up.
111 */
112 static uma_zone_t hashzone;
113
114 /* The boot-time adjusted value for cache line alignment. */
115 static int uma_align_cache = 64 - 1;
116
117 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
118
119 /*
120 * Are we allowed to allocate buckets?
121 */
122 static int bucketdisable = 1;
123
124 /* Linked list of all kegs in the system */
125 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
126
127 /* This mutex protects the keg list */
128 static struct mtx uma_mtx;
129
130 /* Linked list of boot time pages */
131 static LIST_HEAD(,uma_slab) uma_boot_pages =
132 LIST_HEAD_INITIALIZER(uma_boot_pages);
133
134 /* This mutex protects the boot time pages list */
135 static struct mtx uma_boot_pages_mtx;
136
137 /* Is the VM done starting up? */
138 static int booted = 0;
139
140 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
141 static u_int uma_max_ipers;
142 static u_int uma_max_ipers_ref;
143
144 /*
145 * This is the handle used to schedule events that need to happen
146 * outside of the allocation fast path.
147 */
148 static struct callout uma_callout;
149 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
150
151 /*
152 * This structure is passed as the zone ctor arg so that I don't have to create
153 * a special allocation function just for zones.
154 */
155 struct uma_zctor_args {
156 char *name;
157 size_t size;
158 uma_ctor ctor;
159 uma_dtor dtor;
160 uma_init uminit;
161 uma_fini fini;
162 uma_keg_t keg;
163 int align;
164 u_int32_t flags;
165 };
166
167 struct uma_kctor_args {
168 uma_zone_t zone;
169 size_t size;
170 uma_init uminit;
171 uma_fini fini;
172 int align;
173 u_int32_t flags;
174 };
175
176 struct uma_bucket_zone {
177 uma_zone_t ubz_zone;
178 char *ubz_name;
179 int ubz_entries;
180 };
181
182 #define BUCKET_MAX 128
183
184 struct uma_bucket_zone bucket_zones[] = {
185 { NULL, "16 Bucket", 16 },
186 { NULL, "32 Bucket", 32 },
187 { NULL, "64 Bucket", 64 },
188 { NULL, "128 Bucket", 128 },
189 { NULL, NULL, 0}
190 };
191
192 #define BUCKET_SHIFT 4
193 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
194
195 /*
196 * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
197 * of approximately the right size.
198 */
199 static uint8_t bucket_size[BUCKET_ZONES];
200
201 /*
202 * Flags and enumerations to be passed to internal functions.
203 */
204 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
205
206 #define ZFREE_STATFAIL 0x00000001 /* Update zone failure statistic. */
207 #define ZFREE_STATFREE 0x00000002 /* Update zone free statistic. */
208
209 /* Prototypes.. */
210
211 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
212 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
213 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
214 static void page_free(void *, int, u_int8_t);
215 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
216 static void cache_drain(uma_zone_t);
217 static void bucket_drain(uma_zone_t, uma_bucket_t);
218 static void bucket_cache_drain(uma_zone_t zone);
219 static int keg_ctor(void *, int, void *, int);
220 static void keg_dtor(void *, int, void *);
221 static int zone_ctor(void *, int, void *, int);
222 static void zone_dtor(void *, int, void *);
223 static int zero_init(void *, int, int);
224 static void keg_small_init(uma_keg_t keg);
225 static void keg_large_init(uma_keg_t keg);
226 static void zone_foreach(void (*zfunc)(uma_zone_t));
227 static void zone_timeout(uma_zone_t zone);
228 static int hash_alloc(struct uma_hash *);
229 static int hash_expand(struct uma_hash *, struct uma_hash *);
230 static void hash_free(struct uma_hash *hash);
231 static void uma_timeout(void *);
232 static void uma_startup3(void);
233 static void *zone_alloc_item(uma_zone_t, void *, int);
234 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip,
235 int);
236 static void bucket_enable(void);
237 static void bucket_init(void);
238 static uma_bucket_t bucket_alloc(int, int);
239 static void bucket_free(uma_bucket_t);
240 static void bucket_zone_drain(void);
241 static int zone_alloc_bucket(uma_zone_t zone, int flags);
242 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
243 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
244 static void *slab_alloc_item(uma_zone_t zone, uma_slab_t slab);
245 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
246 uma_fini fini, int align, u_int32_t flags);
247 static inline void zone_relock(uma_zone_t zone, uma_keg_t keg);
248 static inline void keg_relock(uma_keg_t keg, uma_zone_t zone);
249
250 void uma_print_zone(uma_zone_t);
251 void uma_print_stats(void);
252 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
253 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
254
255 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
256
257 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
258 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
259
260 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
261 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
262
263 /*
264 * This routine checks to see whether or not it's safe to enable buckets.
265 */
266
267 static void
268 bucket_enable(void)
269 {
270 if (cnt.v_free_count < cnt.v_free_min)
271 bucketdisable = 1;
272 else
273 bucketdisable = 0;
274 }
275
276 /*
277 * Initialize bucket_zones, the array of zones of buckets of various sizes.
278 *
279 * For each zone, calculate the memory required for each bucket, consisting
280 * of the header and an array of pointers. Initialize bucket_size[] to point
281 * the range of appropriate bucket sizes at the zone.
282 */
283 static void
284 bucket_init(void)
285 {
286 struct uma_bucket_zone *ubz;
287 int i;
288 int j;
289
290 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
291 int size;
292
293 ubz = &bucket_zones[j];
294 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
295 size += sizeof(void *) * ubz->ubz_entries;
296 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
297 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
298 UMA_ZFLAG_INTERNAL | UMA_ZFLAG_BUCKET);
299 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
300 bucket_size[i >> BUCKET_SHIFT] = j;
301 }
302 }
303
304 /*
305 * Given a desired number of entries for a bucket, return the zone from which
306 * to allocate the bucket.
307 */
308 static struct uma_bucket_zone *
309 bucket_zone_lookup(int entries)
310 {
311 int idx;
312
313 idx = howmany(entries, 1 << BUCKET_SHIFT);
314 return (&bucket_zones[bucket_size[idx]]);
315 }
316
317 static uma_bucket_t
318 bucket_alloc(int entries, int bflags)
319 {
320 struct uma_bucket_zone *ubz;
321 uma_bucket_t bucket;
322
323 /*
324 * This is to stop us from allocating per cpu buckets while we're
325 * running out of vm.boot_pages. Otherwise, we would exhaust the
326 * boot pages. This also prevents us from allocating buckets in
327 * low memory situations.
328 */
329 if (bucketdisable)
330 return (NULL);
331
332 ubz = bucket_zone_lookup(entries);
333 bucket = zone_alloc_item(ubz->ubz_zone, NULL, bflags);
334 if (bucket) {
335 #ifdef INVARIANTS
336 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
337 #endif
338 bucket->ub_cnt = 0;
339 bucket->ub_entries = ubz->ubz_entries;
340 }
341
342 return (bucket);
343 }
344
345 static void
346 bucket_free(uma_bucket_t bucket)
347 {
348 struct uma_bucket_zone *ubz;
349
350 ubz = bucket_zone_lookup(bucket->ub_entries);
351 zone_free_item(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
352 ZFREE_STATFREE);
353 }
354
355 static void
356 bucket_zone_drain(void)
357 {
358 struct uma_bucket_zone *ubz;
359
360 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
361 zone_drain(ubz->ubz_zone);
362 }
363
364 static inline uma_keg_t
365 zone_first_keg(uma_zone_t zone)
366 {
367
368 return (LIST_FIRST(&zone->uz_kegs)->kl_keg);
369 }
370
371 static void
372 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
373 {
374 uma_klink_t klink;
375
376 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
377 kegfn(klink->kl_keg);
378 }
379
380 /*
381 * Routine called by timeout which is used to fire off some time interval
382 * based calculations. (stats, hash size, etc.)
383 *
384 * Arguments:
385 * arg Unused
386 *
387 * Returns:
388 * Nothing
389 */
390 static void
391 uma_timeout(void *unused)
392 {
393 bucket_enable();
394 zone_foreach(zone_timeout);
395
396 /* Reschedule this event */
397 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
398 }
399
400 /*
401 * Routine to perform timeout driven calculations. This expands the
402 * hashes and does per cpu statistics aggregation.
403 *
404 * Returns nothing.
405 */
406 static void
407 keg_timeout(uma_keg_t keg)
408 {
409
410 KEG_LOCK(keg);
411 /*
412 * Expand the keg hash table.
413 *
414 * This is done if the number of slabs is larger than the hash size.
415 * What I'm trying to do here is completely reduce collisions. This
416 * may be a little aggressive. Should I allow for two collisions max?
417 */
418 if (keg->uk_flags & UMA_ZONE_HASH &&
419 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
420 struct uma_hash newhash;
421 struct uma_hash oldhash;
422 int ret;
423
424 /*
425 * This is so involved because allocating and freeing
426 * while the keg lock is held will lead to deadlock.
427 * I have to do everything in stages and check for
428 * races.
429 */
430 newhash = keg->uk_hash;
431 KEG_UNLOCK(keg);
432 ret = hash_alloc(&newhash);
433 KEG_LOCK(keg);
434 if (ret) {
435 if (hash_expand(&keg->uk_hash, &newhash)) {
436 oldhash = keg->uk_hash;
437 keg->uk_hash = newhash;
438 } else
439 oldhash = newhash;
440
441 KEG_UNLOCK(keg);
442 hash_free(&oldhash);
443 KEG_LOCK(keg);
444 }
445 }
446 KEG_UNLOCK(keg);
447 }
448
449 static void
450 zone_timeout(uma_zone_t zone)
451 {
452
453 zone_foreach_keg(zone, &keg_timeout);
454 }
455
456 /*
457 * Allocate and zero fill the next sized hash table from the appropriate
458 * backing store.
459 *
460 * Arguments:
461 * hash A new hash structure with the old hash size in uh_hashsize
462 *
463 * Returns:
464 * 1 on sucess and 0 on failure.
465 */
466 static int
467 hash_alloc(struct uma_hash *hash)
468 {
469 int oldsize;
470 int alloc;
471
472 oldsize = hash->uh_hashsize;
473
474 /* We're just going to go to a power of two greater */
475 if (oldsize) {
476 hash->uh_hashsize = oldsize * 2;
477 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
478 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
479 M_UMAHASH, M_NOWAIT);
480 } else {
481 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
482 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
483 M_WAITOK);
484 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
485 }
486 if (hash->uh_slab_hash) {
487 bzero(hash->uh_slab_hash, alloc);
488 hash->uh_hashmask = hash->uh_hashsize - 1;
489 return (1);
490 }
491
492 return (0);
493 }
494
495 /*
496 * Expands the hash table for HASH zones. This is done from zone_timeout
497 * to reduce collisions. This must not be done in the regular allocation
498 * path, otherwise, we can recurse on the vm while allocating pages.
499 *
500 * Arguments:
501 * oldhash The hash you want to expand
502 * newhash The hash structure for the new table
503 *
504 * Returns:
505 * Nothing
506 *
507 * Discussion:
508 */
509 static int
510 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
511 {
512 uma_slab_t slab;
513 int hval;
514 int i;
515
516 if (!newhash->uh_slab_hash)
517 return (0);
518
519 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
520 return (0);
521
522 /*
523 * I need to investigate hash algorithms for resizing without a
524 * full rehash.
525 */
526
527 for (i = 0; i < oldhash->uh_hashsize; i++)
528 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
529 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
530 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
531 hval = UMA_HASH(newhash, slab->us_data);
532 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
533 slab, us_hlink);
534 }
535
536 return (1);
537 }
538
539 /*
540 * Free the hash bucket to the appropriate backing store.
541 *
542 * Arguments:
543 * slab_hash The hash bucket we're freeing
544 * hashsize The number of entries in that hash bucket
545 *
546 * Returns:
547 * Nothing
548 */
549 static void
550 hash_free(struct uma_hash *hash)
551 {
552 if (hash->uh_slab_hash == NULL)
553 return;
554 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
555 zone_free_item(hashzone,
556 hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
557 else
558 free(hash->uh_slab_hash, M_UMAHASH);
559 }
560
561 /*
562 * Frees all outstanding items in a bucket
563 *
564 * Arguments:
565 * zone The zone to free to, must be unlocked.
566 * bucket The free/alloc bucket with items, cpu queue must be locked.
567 *
568 * Returns:
569 * Nothing
570 */
571
572 static void
573 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
574 {
575 void *item;
576
577 if (bucket == NULL)
578 return;
579
580 while (bucket->ub_cnt > 0) {
581 bucket->ub_cnt--;
582 item = bucket->ub_bucket[bucket->ub_cnt];
583 #ifdef INVARIANTS
584 bucket->ub_bucket[bucket->ub_cnt] = NULL;
585 KASSERT(item != NULL,
586 ("bucket_drain: botched ptr, item is NULL"));
587 #endif
588 zone_free_item(zone, item, NULL, SKIP_DTOR, 0);
589 }
590 }
591
592 /*
593 * Drains the per cpu caches for a zone.
594 *
595 * NOTE: This may only be called while the zone is being turn down, and not
596 * during normal operation. This is necessary in order that we do not have
597 * to migrate CPUs to drain the per-CPU caches.
598 *
599 * Arguments:
600 * zone The zone to drain, must be unlocked.
601 *
602 * Returns:
603 * Nothing
604 */
605 static void
606 cache_drain(uma_zone_t zone)
607 {
608 uma_cache_t cache;
609 int cpu;
610
611 /*
612 * XXX: It is safe to not lock the per-CPU caches, because we're
613 * tearing down the zone anyway. I.e., there will be no further use
614 * of the caches at this point.
615 *
616 * XXX: It would good to be able to assert that the zone is being
617 * torn down to prevent improper use of cache_drain().
618 *
619 * XXX: We lock the zone before passing into bucket_cache_drain() as
620 * it is used elsewhere. Should the tear-down path be made special
621 * there in some form?
622 */
623 for (cpu = 0; cpu <= mp_maxid; cpu++) {
624 if (CPU_ABSENT(cpu))
625 continue;
626 cache = &zone->uz_cpu[cpu];
627 bucket_drain(zone, cache->uc_allocbucket);
628 bucket_drain(zone, cache->uc_freebucket);
629 if (cache->uc_allocbucket != NULL)
630 bucket_free(cache->uc_allocbucket);
631 if (cache->uc_freebucket != NULL)
632 bucket_free(cache->uc_freebucket);
633 cache->uc_allocbucket = cache->uc_freebucket = NULL;
634 }
635 ZONE_LOCK(zone);
636 bucket_cache_drain(zone);
637 ZONE_UNLOCK(zone);
638 }
639
640 /*
641 * Drain the cached buckets from a zone. Expects a locked zone on entry.
642 */
643 static void
644 bucket_cache_drain(uma_zone_t zone)
645 {
646 uma_bucket_t bucket;
647
648 /*
649 * Drain the bucket queues and free the buckets, we just keep two per
650 * cpu (alloc/free).
651 */
652 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
653 LIST_REMOVE(bucket, ub_link);
654 ZONE_UNLOCK(zone);
655 bucket_drain(zone, bucket);
656 bucket_free(bucket);
657 ZONE_LOCK(zone);
658 }
659
660 /* Now we do the free queue.. */
661 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
662 LIST_REMOVE(bucket, ub_link);
663 bucket_free(bucket);
664 }
665 }
666
667 /*
668 * Frees pages from a keg back to the system. This is done on demand from
669 * the pageout daemon.
670 *
671 * Returns nothing.
672 */
673 static void
674 keg_drain(uma_keg_t keg)
675 {
676 struct slabhead freeslabs = { 0 };
677 uma_slab_t slab;
678 uma_slab_t n;
679 u_int8_t flags;
680 u_int8_t *mem;
681 int i;
682
683 /*
684 * We don't want to take pages from statically allocated kegs at this
685 * time
686 */
687 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
688 return;
689
690 #ifdef UMA_DEBUG
691 printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
692 #endif
693 KEG_LOCK(keg);
694 if (keg->uk_free == 0)
695 goto finished;
696
697 slab = LIST_FIRST(&keg->uk_free_slab);
698 while (slab) {
699 n = LIST_NEXT(slab, us_link);
700
701 /* We have no where to free these to */
702 if (slab->us_flags & UMA_SLAB_BOOT) {
703 slab = n;
704 continue;
705 }
706
707 LIST_REMOVE(slab, us_link);
708 keg->uk_pages -= keg->uk_ppera;
709 keg->uk_free -= keg->uk_ipers;
710
711 if (keg->uk_flags & UMA_ZONE_HASH)
712 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
713
714 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
715
716 slab = n;
717 }
718 finished:
719 KEG_UNLOCK(keg);
720
721 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
722 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
723 if (keg->uk_fini)
724 for (i = 0; i < keg->uk_ipers; i++)
725 keg->uk_fini(
726 slab->us_data + (keg->uk_rsize * i),
727 keg->uk_size);
728 flags = slab->us_flags;
729 mem = slab->us_data;
730
731 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
732 vm_object_t obj;
733
734 if (flags & UMA_SLAB_KMEM)
735 obj = kmem_object;
736 else if (flags & UMA_SLAB_KERNEL)
737 obj = kernel_object;
738 else
739 obj = NULL;
740 for (i = 0; i < keg->uk_ppera; i++)
741 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
742 obj);
743 }
744 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
745 zone_free_item(keg->uk_slabzone, slab, NULL,
746 SKIP_NONE, ZFREE_STATFREE);
747 #ifdef UMA_DEBUG
748 printf("%s: Returning %d bytes.\n",
749 keg->uk_name, UMA_SLAB_SIZE * keg->uk_ppera);
750 #endif
751 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
752 }
753 }
754
755 static void
756 zone_drain_wait(uma_zone_t zone, int waitok)
757 {
758
759 /*
760 * Set draining to interlock with zone_dtor() so we can release our
761 * locks as we go. Only dtor() should do a WAITOK call since it
762 * is the only call that knows the structure will still be available
763 * when it wakes up.
764 */
765 ZONE_LOCK(zone);
766 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
767 if (waitok == M_NOWAIT)
768 goto out;
769 mtx_unlock(&uma_mtx);
770 msleep(zone, zone->uz_lock, PVM, "zonedrain", 1);
771 mtx_lock(&uma_mtx);
772 }
773 zone->uz_flags |= UMA_ZFLAG_DRAINING;
774 bucket_cache_drain(zone);
775 ZONE_UNLOCK(zone);
776 /*
777 * The DRAINING flag protects us from being freed while
778 * we're running. Normally the uma_mtx would protect us but we
779 * must be able to release and acquire the right lock for each keg.
780 */
781 zone_foreach_keg(zone, &keg_drain);
782 ZONE_LOCK(zone);
783 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
784 wakeup(zone);
785 out:
786 ZONE_UNLOCK(zone);
787 }
788
789 void
790 zone_drain(uma_zone_t zone)
791 {
792
793 zone_drain_wait(zone, M_NOWAIT);
794 }
795
796 /*
797 * Allocate a new slab for a keg. This does not insert the slab onto a list.
798 *
799 * Arguments:
800 * wait Shall we wait?
801 *
802 * Returns:
803 * The slab that was allocated or NULL if there is no memory and the
804 * caller specified M_NOWAIT.
805 */
806 static uma_slab_t
807 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
808 {
809 uma_slabrefcnt_t slabref;
810 uma_alloc allocf;
811 uma_slab_t slab;
812 u_int8_t *mem;
813 u_int8_t flags;
814 int i;
815
816 mtx_assert(&keg->uk_lock, MA_OWNED);
817 slab = NULL;
818
819 #ifdef UMA_DEBUG
820 printf("slab_zalloc: Allocating a new slab for %s\n", keg->uk_name);
821 #endif
822 allocf = keg->uk_allocf;
823 KEG_UNLOCK(keg);
824
825 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
826 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
827 if (slab == NULL) {
828 KEG_LOCK(keg);
829 return NULL;
830 }
831 }
832
833 /*
834 * This reproduces the old vm_zone behavior of zero filling pages the
835 * first time they are added to a zone.
836 *
837 * Malloced items are zeroed in uma_zalloc.
838 */
839
840 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
841 wait |= M_ZERO;
842 else
843 wait &= ~M_ZERO;
844
845 /* zone is passed for legacy reasons. */
846 mem = allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE, &flags, wait);
847 if (mem == NULL) {
848 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
849 zone_free_item(keg->uk_slabzone, slab, NULL,
850 SKIP_NONE, ZFREE_STATFREE);
851 KEG_LOCK(keg);
852 return (NULL);
853 }
854
855 /* Point the slab into the allocated memory */
856 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
857 slab = (uma_slab_t )(mem + keg->uk_pgoff);
858
859 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
860 for (i = 0; i < keg->uk_ppera; i++)
861 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
862
863 slab->us_keg = keg;
864 slab->us_data = mem;
865 slab->us_freecount = keg->uk_ipers;
866 slab->us_firstfree = 0;
867 slab->us_flags = flags;
868
869 if (keg->uk_flags & UMA_ZONE_REFCNT) {
870 slabref = (uma_slabrefcnt_t)slab;
871 for (i = 0; i < keg->uk_ipers; i++) {
872 slabref->us_freelist[i].us_refcnt = 0;
873 slabref->us_freelist[i].us_item = i+1;
874 }
875 } else {
876 for (i = 0; i < keg->uk_ipers; i++)
877 slab->us_freelist[i].us_item = i+1;
878 }
879
880 if (keg->uk_init != NULL) {
881 for (i = 0; i < keg->uk_ipers; i++)
882 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
883 keg->uk_size, wait) != 0)
884 break;
885 if (i != keg->uk_ipers) {
886 if (keg->uk_fini != NULL) {
887 for (i--; i > -1; i--)
888 keg->uk_fini(slab->us_data +
889 (keg->uk_rsize * i),
890 keg->uk_size);
891 }
892 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
893 vm_object_t obj;
894
895 if (flags & UMA_SLAB_KMEM)
896 obj = kmem_object;
897 else if (flags & UMA_SLAB_KERNEL)
898 obj = kernel_object;
899 else
900 obj = NULL;
901 for (i = 0; i < keg->uk_ppera; i++)
902 vsetobj((vm_offset_t)mem +
903 (i * PAGE_SIZE), obj);
904 }
905 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
906 zone_free_item(keg->uk_slabzone, slab,
907 NULL, SKIP_NONE, ZFREE_STATFREE);
908 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
909 flags);
910 KEG_LOCK(keg);
911 return (NULL);
912 }
913 }
914 KEG_LOCK(keg);
915
916 if (keg->uk_flags & UMA_ZONE_HASH)
917 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
918
919 keg->uk_pages += keg->uk_ppera;
920 keg->uk_free += keg->uk_ipers;
921
922 return (slab);
923 }
924
925 /*
926 * This function is intended to be used early on in place of page_alloc() so
927 * that we may use the boot time page cache to satisfy allocations before
928 * the VM is ready.
929 */
930 static void *
931 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
932 {
933 uma_keg_t keg;
934 uma_slab_t tmps;
935 int pages, check_pages;
936
937 keg = zone_first_keg(zone);
938 pages = howmany(bytes, PAGE_SIZE);
939 check_pages = pages - 1;
940 KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
941
942 /*
943 * Check our small startup cache to see if it has pages remaining.
944 */
945 mtx_lock(&uma_boot_pages_mtx);
946
947 /* First check if we have enough room. */
948 tmps = LIST_FIRST(&uma_boot_pages);
949 while (tmps != NULL && check_pages-- > 0)
950 tmps = LIST_NEXT(tmps, us_link);
951 if (tmps != NULL) {
952 /*
953 * It's ok to lose tmps references. The last one will
954 * have tmps->us_data pointing to the start address of
955 * "pages" contiguous pages of memory.
956 */
957 while (pages-- > 0) {
958 tmps = LIST_FIRST(&uma_boot_pages);
959 LIST_REMOVE(tmps, us_link);
960 }
961 mtx_unlock(&uma_boot_pages_mtx);
962 *pflag = tmps->us_flags;
963 return (tmps->us_data);
964 }
965 mtx_unlock(&uma_boot_pages_mtx);
966 if (booted == 0)
967 panic("UMA: Increase vm.boot_pages");
968 /*
969 * Now that we've booted reset these users to their real allocator.
970 */
971 #ifdef UMA_MD_SMALL_ALLOC
972 keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
973 #else
974 keg->uk_allocf = page_alloc;
975 #endif
976 return keg->uk_allocf(zone, bytes, pflag, wait);
977 }
978
979 /*
980 * Allocates a number of pages from the system
981 *
982 * Arguments:
983 * bytes The number of bytes requested
984 * wait Shall we wait?
985 *
986 * Returns:
987 * A pointer to the alloced memory or possibly
988 * NULL if M_NOWAIT is set.
989 */
990 static void *
991 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
992 {
993 void *p; /* Returned page */
994
995 *pflag = UMA_SLAB_KMEM;
996 p = (void *) kmem_malloc(kmem_map, bytes, wait);
997
998 return (p);
999 }
1000
1001 /*
1002 * Allocates a number of pages from within an object
1003 *
1004 * Arguments:
1005 * bytes The number of bytes requested
1006 * wait Shall we wait?
1007 *
1008 * Returns:
1009 * A pointer to the alloced memory or possibly
1010 * NULL if M_NOWAIT is set.
1011 */
1012 static void *
1013 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
1014 {
1015 vm_object_t object;
1016 vm_offset_t retkva, zkva;
1017 vm_page_t p;
1018 int pages, startpages;
1019 uma_keg_t keg;
1020
1021 keg = zone_first_keg(zone);
1022 object = keg->uk_obj;
1023 retkva = 0;
1024
1025 /*
1026 * This looks a little weird since we're getting one page at a time.
1027 */
1028 VM_OBJECT_LOCK(object);
1029 p = TAILQ_LAST(&object->memq, pglist);
1030 pages = p != NULL ? p->pindex + 1 : 0;
1031 startpages = pages;
1032 zkva = keg->uk_kva + pages * PAGE_SIZE;
1033 for (; bytes > 0; bytes -= PAGE_SIZE) {
1034 p = vm_page_alloc(object, pages,
1035 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
1036 if (p == NULL) {
1037 if (pages != startpages)
1038 pmap_qremove(retkva, pages - startpages);
1039 while (pages != startpages) {
1040 pages--;
1041 p = TAILQ_LAST(&object->memq, pglist);
1042 vm_page_lock_queues();
1043 vm_page_unwire(p, 0);
1044 vm_page_free(p);
1045 vm_page_unlock_queues();
1046 }
1047 retkva = 0;
1048 goto done;
1049 }
1050 pmap_qenter(zkva, &p, 1);
1051 if (retkva == 0)
1052 retkva = zkva;
1053 zkva += PAGE_SIZE;
1054 pages += 1;
1055 }
1056 done:
1057 VM_OBJECT_UNLOCK(object);
1058 *flags = UMA_SLAB_PRIV;
1059
1060 return ((void *)retkva);
1061 }
1062
1063 /*
1064 * Frees a number of pages to the system
1065 *
1066 * Arguments:
1067 * mem A pointer to the memory to be freed
1068 * size The size of the memory being freed
1069 * flags The original p->us_flags field
1070 *
1071 * Returns:
1072 * Nothing
1073 */
1074 static void
1075 page_free(void *mem, int size, u_int8_t flags)
1076 {
1077 vm_map_t map;
1078
1079 if (flags & UMA_SLAB_KMEM)
1080 map = kmem_map;
1081 else if (flags & UMA_SLAB_KERNEL)
1082 map = kernel_map;
1083 else
1084 panic("UMA: page_free used with invalid flags %d", flags);
1085
1086 kmem_free(map, (vm_offset_t)mem, size);
1087 }
1088
1089 /*
1090 * Zero fill initializer
1091 *
1092 * Arguments/Returns follow uma_init specifications
1093 */
1094 static int
1095 zero_init(void *mem, int size, int flags)
1096 {
1097 bzero(mem, size);
1098 return (0);
1099 }
1100
1101 /*
1102 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1103 *
1104 * Arguments
1105 * keg The zone we should initialize
1106 *
1107 * Returns
1108 * Nothing
1109 */
1110 static void
1111 keg_small_init(uma_keg_t keg)
1112 {
1113 u_int rsize;
1114 u_int memused;
1115 u_int wastedspace;
1116 u_int shsize;
1117
1118 KASSERT(keg != NULL, ("Keg is null in keg_small_init"));
1119 rsize = keg->uk_size;
1120
1121 if (rsize < UMA_SMALLEST_UNIT)
1122 rsize = UMA_SMALLEST_UNIT;
1123 if (rsize & keg->uk_align)
1124 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1125
1126 keg->uk_rsize = rsize;
1127 keg->uk_ppera = 1;
1128
1129 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1130 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1131 shsize = sizeof(struct uma_slab_refcnt);
1132 } else {
1133 rsize += UMA_FRITM_SZ; /* Account for linkage */
1134 shsize = sizeof(struct uma_slab);
1135 }
1136
1137 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1138 KASSERT(keg->uk_ipers != 0, ("keg_small_init: ipers is 0"));
1139 memused = keg->uk_ipers * rsize + shsize;
1140 wastedspace = UMA_SLAB_SIZE - memused;
1141
1142 /*
1143 * We can't do OFFPAGE if we're internal or if we've been
1144 * asked to not go to the VM for buckets. If we do this we
1145 * may end up going to the VM (kmem_map) for slabs which we
1146 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1147 * result of UMA_ZONE_VM, which clearly forbids it.
1148 */
1149 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1150 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1151 return;
1152
1153 if ((wastedspace >= UMA_MAX_WASTE) &&
1154 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1155 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1156 KASSERT(keg->uk_ipers <= 255,
1157 ("keg_small_init: keg->uk_ipers too high!"));
1158 #ifdef UMA_DEBUG
1159 printf("UMA decided we need offpage slab headers for "
1160 "keg: %s, calculated wastedspace = %d, "
1161 "maximum wasted space allowed = %d, "
1162 "calculated ipers = %d, "
1163 "new wasted space = %d\n", keg->uk_name, wastedspace,
1164 UMA_MAX_WASTE, keg->uk_ipers,
1165 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1166 #endif
1167 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1168 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1169 keg->uk_flags |= UMA_ZONE_HASH;
1170 }
1171 }
1172
1173 /*
1174 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1175 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1176 * more complicated.
1177 *
1178 * Arguments
1179 * keg The keg we should initialize
1180 *
1181 * Returns
1182 * Nothing
1183 */
1184 static void
1185 keg_large_init(uma_keg_t keg)
1186 {
1187 int pages;
1188
1189 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1190 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1191 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1192
1193 pages = keg->uk_size / UMA_SLAB_SIZE;
1194
1195 /* Account for remainder */
1196 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1197 pages++;
1198
1199 keg->uk_ppera = pages;
1200 keg->uk_ipers = 1;
1201 keg->uk_rsize = keg->uk_size;
1202
1203 /* We can't do OFFPAGE if we're internal, bail out here. */
1204 if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1205 return;
1206
1207 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1208 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1209 keg->uk_flags |= UMA_ZONE_HASH;
1210 }
1211
1212 static void
1213 keg_cachespread_init(uma_keg_t keg)
1214 {
1215 int alignsize;
1216 int trailer;
1217 int pages;
1218 int rsize;
1219
1220 alignsize = keg->uk_align + 1;
1221 rsize = keg->uk_size;
1222 /*
1223 * We want one item to start on every align boundary in a page. To
1224 * do this we will span pages. We will also extend the item by the
1225 * size of align if it is an even multiple of align. Otherwise, it
1226 * would fall on the same boundary every time.
1227 */
1228 if (rsize & keg->uk_align)
1229 rsize = (rsize & ~keg->uk_align) + alignsize;
1230 if ((rsize & alignsize) == 0)
1231 rsize += alignsize;
1232 trailer = rsize - keg->uk_size;
1233 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1234 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1235 keg->uk_rsize = rsize;
1236 keg->uk_ppera = pages;
1237 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1238 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1239 KASSERT(keg->uk_ipers <= uma_max_ipers,
1240 ("keg_small_init: keg->uk_ipers too high(%d) increase max_ipers",
1241 keg->uk_ipers));
1242 }
1243
1244 /*
1245 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1246 * the keg onto the global keg list.
1247 *
1248 * Arguments/Returns follow uma_ctor specifications
1249 * udata Actually uma_kctor_args
1250 */
1251 static int
1252 keg_ctor(void *mem, int size, void *udata, int flags)
1253 {
1254 struct uma_kctor_args *arg = udata;
1255 uma_keg_t keg = mem;
1256 uma_zone_t zone;
1257
1258 bzero(keg, size);
1259 keg->uk_size = arg->size;
1260 keg->uk_init = arg->uminit;
1261 keg->uk_fini = arg->fini;
1262 keg->uk_align = arg->align;
1263 keg->uk_free = 0;
1264 keg->uk_pages = 0;
1265 keg->uk_flags = arg->flags;
1266 keg->uk_allocf = page_alloc;
1267 keg->uk_freef = page_free;
1268 keg->uk_recurse = 0;
1269 keg->uk_slabzone = NULL;
1270
1271 /*
1272 * The master zone is passed to us at keg-creation time.
1273 */
1274 zone = arg->zone;
1275 keg->uk_name = zone->uz_name;
1276
1277 if (arg->flags & UMA_ZONE_VM)
1278 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1279
1280 if (arg->flags & UMA_ZONE_ZINIT)
1281 keg->uk_init = zero_init;
1282
1283 if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1284 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1285
1286 /*
1287 * The +UMA_FRITM_SZ added to uk_size is to account for the
1288 * linkage that is added to the size in keg_small_init(). If
1289 * we don't account for this here then we may end up in
1290 * keg_small_init() with a calculated 'ipers' of 0.
1291 */
1292 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1293 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1294 keg_cachespread_init(keg);
1295 else if ((keg->uk_size+UMA_FRITMREF_SZ) >
1296 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1297 keg_large_init(keg);
1298 else
1299 keg_small_init(keg);
1300 } else {
1301 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1302 keg_cachespread_init(keg);
1303 else if ((keg->uk_size+UMA_FRITM_SZ) >
1304 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1305 keg_large_init(keg);
1306 else
1307 keg_small_init(keg);
1308 }
1309
1310 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1311 if (keg->uk_flags & UMA_ZONE_REFCNT)
1312 keg->uk_slabzone = slabrefzone;
1313 else
1314 keg->uk_slabzone = slabzone;
1315 }
1316
1317 /*
1318 * If we haven't booted yet we need allocations to go through the
1319 * startup cache until the vm is ready.
1320 */
1321 if (keg->uk_ppera == 1) {
1322 #ifdef UMA_MD_SMALL_ALLOC
1323 keg->uk_allocf = uma_small_alloc;
1324 keg->uk_freef = uma_small_free;
1325 #endif
1326 if (booted == 0)
1327 keg->uk_allocf = startup_alloc;
1328 } else if (booted == 0 && (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1329 keg->uk_allocf = startup_alloc;
1330
1331 /*
1332 * Initialize keg's lock (shared among zones).
1333 */
1334 if (arg->flags & UMA_ZONE_MTXCLASS)
1335 KEG_LOCK_INIT(keg, 1);
1336 else
1337 KEG_LOCK_INIT(keg, 0);
1338
1339 /*
1340 * If we're putting the slab header in the actual page we need to
1341 * figure out where in each page it goes. This calculates a right
1342 * justified offset into the memory on an ALIGN_PTR boundary.
1343 */
1344 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1345 u_int totsize;
1346
1347 /* Size of the slab struct and free list */
1348 if (keg->uk_flags & UMA_ZONE_REFCNT)
1349 totsize = sizeof(struct uma_slab_refcnt) +
1350 keg->uk_ipers * UMA_FRITMREF_SZ;
1351 else
1352 totsize = sizeof(struct uma_slab) +
1353 keg->uk_ipers * UMA_FRITM_SZ;
1354
1355 if (totsize & UMA_ALIGN_PTR)
1356 totsize = (totsize & ~UMA_ALIGN_PTR) +
1357 (UMA_ALIGN_PTR + 1);
1358 keg->uk_pgoff = (UMA_SLAB_SIZE * keg->uk_ppera) - totsize;
1359
1360 if (keg->uk_flags & UMA_ZONE_REFCNT)
1361 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1362 + keg->uk_ipers * UMA_FRITMREF_SZ;
1363 else
1364 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1365 + keg->uk_ipers * UMA_FRITM_SZ;
1366
1367 /*
1368 * The only way the following is possible is if with our
1369 * UMA_ALIGN_PTR adjustments we are now bigger than
1370 * UMA_SLAB_SIZE. I haven't checked whether this is
1371 * mathematically possible for all cases, so we make
1372 * sure here anyway.
1373 */
1374 if (totsize > UMA_SLAB_SIZE * keg->uk_ppera) {
1375 printf("zone %s ipers %d rsize %d size %d\n",
1376 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1377 keg->uk_size);
1378 panic("UMA slab won't fit.");
1379 }
1380 }
1381
1382 if (keg->uk_flags & UMA_ZONE_HASH)
1383 hash_alloc(&keg->uk_hash);
1384
1385 #ifdef UMA_DEBUG
1386 printf("UMA: %s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
1387 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1388 keg->uk_ipers, keg->uk_ppera,
1389 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1390 #endif
1391
1392 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1393
1394 mtx_lock(&uma_mtx);
1395 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1396 mtx_unlock(&uma_mtx);
1397 return (0);
1398 }
1399
1400 /*
1401 * Zone header ctor. This initializes all fields, locks, etc.
1402 *
1403 * Arguments/Returns follow uma_ctor specifications
1404 * udata Actually uma_zctor_args
1405 */
1406 static int
1407 zone_ctor(void *mem, int size, void *udata, int flags)
1408 {
1409 struct uma_zctor_args *arg = udata;
1410 uma_zone_t zone = mem;
1411 uma_zone_t z;
1412 uma_keg_t keg;
1413
1414 bzero(zone, size);
1415 zone->uz_name = arg->name;
1416 zone->uz_ctor = arg->ctor;
1417 zone->uz_dtor = arg->dtor;
1418 zone->uz_slab = zone_fetch_slab;
1419 zone->uz_init = NULL;
1420 zone->uz_fini = NULL;
1421 zone->uz_allocs = 0;
1422 zone->uz_frees = 0;
1423 zone->uz_fails = 0;
1424 zone->uz_fills = zone->uz_count = 0;
1425 zone->uz_flags = 0;
1426 keg = arg->keg;
1427
1428 if (arg->flags & UMA_ZONE_SECONDARY) {
1429 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1430 zone->uz_init = arg->uminit;
1431 zone->uz_fini = arg->fini;
1432 zone->uz_lock = &keg->uk_lock;
1433 zone->uz_flags |= UMA_ZONE_SECONDARY;
1434 mtx_lock(&uma_mtx);
1435 ZONE_LOCK(zone);
1436 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1437 if (LIST_NEXT(z, uz_link) == NULL) {
1438 LIST_INSERT_AFTER(z, zone, uz_link);
1439 break;
1440 }
1441 }
1442 ZONE_UNLOCK(zone);
1443 mtx_unlock(&uma_mtx);
1444 } else if (keg == NULL) {
1445 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1446 arg->align, arg->flags)) == NULL)
1447 return (ENOMEM);
1448 } else {
1449 struct uma_kctor_args karg;
1450 int error;
1451
1452 /* We should only be here from uma_startup() */
1453 karg.size = arg->size;
1454 karg.uminit = arg->uminit;
1455 karg.fini = arg->fini;
1456 karg.align = arg->align;
1457 karg.flags = arg->flags;
1458 karg.zone = zone;
1459 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1460 flags);
1461 if (error)
1462 return (error);
1463 }
1464 /*
1465 * Link in the first keg.
1466 */
1467 zone->uz_klink.kl_keg = keg;
1468 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1469 zone->uz_lock = &keg->uk_lock;
1470 zone->uz_size = keg->uk_size;
1471 zone->uz_flags |= (keg->uk_flags &
1472 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1473
1474 /*
1475 * Some internal zones don't have room allocated for the per cpu
1476 * caches. If we're internal, bail out here.
1477 */
1478 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1479 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1480 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1481 return (0);
1482 }
1483
1484 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1485 zone->uz_count = BUCKET_MAX;
1486 else if (keg->uk_ipers <= BUCKET_MAX)
1487 zone->uz_count = keg->uk_ipers;
1488 else
1489 zone->uz_count = BUCKET_MAX;
1490 return (0);
1491 }
1492
1493 /*
1494 * Keg header dtor. This frees all data, destroys locks, frees the hash
1495 * table and removes the keg from the global list.
1496 *
1497 * Arguments/Returns follow uma_dtor specifications
1498 * udata unused
1499 */
1500 static void
1501 keg_dtor(void *arg, int size, void *udata)
1502 {
1503 uma_keg_t keg;
1504
1505 keg = (uma_keg_t)arg;
1506 KEG_LOCK(keg);
1507 if (keg->uk_free != 0) {
1508 printf("Freed UMA keg was not empty (%d items). "
1509 " Lost %d pages of memory.\n",
1510 keg->uk_free, keg->uk_pages);
1511 }
1512 KEG_UNLOCK(keg);
1513
1514 hash_free(&keg->uk_hash);
1515
1516 KEG_LOCK_FINI(keg);
1517 }
1518
1519 /*
1520 * Zone header dtor.
1521 *
1522 * Arguments/Returns follow uma_dtor specifications
1523 * udata unused
1524 */
1525 static void
1526 zone_dtor(void *arg, int size, void *udata)
1527 {
1528 uma_klink_t klink;
1529 uma_zone_t zone;
1530 uma_keg_t keg;
1531
1532 zone = (uma_zone_t)arg;
1533 keg = zone_first_keg(zone);
1534
1535 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1536 cache_drain(zone);
1537
1538 mtx_lock(&uma_mtx);
1539 LIST_REMOVE(zone, uz_link);
1540 mtx_unlock(&uma_mtx);
1541 /*
1542 * XXX there are some races here where
1543 * the zone can be drained but zone lock
1544 * released and then refilled before we
1545 * remove it... we dont care for now
1546 */
1547 zone_drain_wait(zone, M_WAITOK);
1548 /*
1549 * Unlink all of our kegs.
1550 */
1551 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1552 klink->kl_keg = NULL;
1553 LIST_REMOVE(klink, kl_link);
1554 if (klink == &zone->uz_klink)
1555 continue;
1556 free(klink, M_TEMP);
1557 }
1558 /*
1559 * We only destroy kegs from non secondary zones.
1560 */
1561 if ((zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1562 mtx_lock(&uma_mtx);
1563 LIST_REMOVE(keg, uk_link);
1564 mtx_unlock(&uma_mtx);
1565 zone_free_item(kegs, keg, NULL, SKIP_NONE,
1566 ZFREE_STATFREE);
1567 }
1568 }
1569
1570 /*
1571 * Traverses every zone in the system and calls a callback
1572 *
1573 * Arguments:
1574 * zfunc A pointer to a function which accepts a zone
1575 * as an argument.
1576 *
1577 * Returns:
1578 * Nothing
1579 */
1580 static void
1581 zone_foreach(void (*zfunc)(uma_zone_t))
1582 {
1583 uma_keg_t keg;
1584 uma_zone_t zone;
1585
1586 mtx_lock(&uma_mtx);
1587 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1588 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1589 zfunc(zone);
1590 }
1591 mtx_unlock(&uma_mtx);
1592 }
1593
1594 /* Public functions */
1595 /* See uma.h */
1596 void
1597 uma_startup(void *bootmem, int boot_pages)
1598 {
1599 struct uma_zctor_args args;
1600 uma_slab_t slab;
1601 u_int slabsize;
1602 u_int objsize, totsize, wsize;
1603 int i;
1604
1605 #ifdef UMA_DEBUG
1606 printf("Creating uma keg headers zone and keg.\n");
1607 #endif
1608 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1609
1610 /*
1611 * Figure out the maximum number of items-per-slab we'll have if
1612 * we're using the OFFPAGE slab header to track free items, given
1613 * all possible object sizes and the maximum desired wastage
1614 * (UMA_MAX_WASTE).
1615 *
1616 * We iterate until we find an object size for
1617 * which the calculated wastage in keg_small_init() will be
1618 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1619 * is an overall increasing see-saw function, we find the smallest
1620 * objsize such that the wastage is always acceptable for objects
1621 * with that objsize or smaller. Since a smaller objsize always
1622 * generates a larger possible uma_max_ipers, we use this computed
1623 * objsize to calculate the largest ipers possible. Since the
1624 * ipers calculated for OFFPAGE slab headers is always larger than
1625 * the ipers initially calculated in keg_small_init(), we use
1626 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1627 * obtain the maximum ipers possible for offpage slab headers.
1628 *
1629 * It should be noted that ipers versus objsize is an inversly
1630 * proportional function which drops off rather quickly so as
1631 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1632 * falls into the portion of the inverse relation AFTER the steep
1633 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1634 *
1635 * Note that we have 8-bits (1 byte) to use as a freelist index
1636 * inside the actual slab header itself and this is enough to
1637 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1638 * object with offpage slab header would have ipers =
1639 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1640 * 1 greater than what our byte-integer freelist index can
1641 * accomodate, but we know that this situation never occurs as
1642 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1643 * that we need to go to offpage slab headers. Or, if we do,
1644 * then we trap that condition below and panic in the INVARIANTS case.
1645 */
1646 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1647 totsize = wsize;
1648 objsize = UMA_SMALLEST_UNIT;
1649 while (totsize >= wsize) {
1650 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1651 (objsize + UMA_FRITM_SZ);
1652 totsize *= (UMA_FRITM_SZ + objsize);
1653 objsize++;
1654 }
1655 if (objsize > UMA_SMALLEST_UNIT)
1656 objsize--;
1657 uma_max_ipers = MAX(UMA_SLAB_SIZE / objsize, 64);
1658
1659 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1660 totsize = wsize;
1661 objsize = UMA_SMALLEST_UNIT;
1662 while (totsize >= wsize) {
1663 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1664 (objsize + UMA_FRITMREF_SZ);
1665 totsize *= (UMA_FRITMREF_SZ + objsize);
1666 objsize++;
1667 }
1668 if (objsize > UMA_SMALLEST_UNIT)
1669 objsize--;
1670 uma_max_ipers_ref = MAX(UMA_SLAB_SIZE / objsize, 64);
1671
1672 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1673 ("uma_startup: calculated uma_max_ipers values too large!"));
1674
1675 #ifdef UMA_DEBUG
1676 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1677 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1678 uma_max_ipers_ref);
1679 #endif
1680
1681 /* "manually" create the initial zone */
1682 args.name = "UMA Kegs";
1683 args.size = sizeof(struct uma_keg);
1684 args.ctor = keg_ctor;
1685 args.dtor = keg_dtor;
1686 args.uminit = zero_init;
1687 args.fini = NULL;
1688 args.keg = &masterkeg;
1689 args.align = 32 - 1;
1690 args.flags = UMA_ZFLAG_INTERNAL;
1691 /* The initial zone has no Per cpu queues so it's smaller */
1692 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1693
1694 #ifdef UMA_DEBUG
1695 printf("Filling boot free list.\n");
1696 #endif
1697 for (i = 0; i < boot_pages; i++) {
1698 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1699 slab->us_data = (u_int8_t *)slab;
1700 slab->us_flags = UMA_SLAB_BOOT;
1701 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1702 }
1703 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1704
1705 #ifdef UMA_DEBUG
1706 printf("Creating uma zone headers zone and keg.\n");
1707 #endif
1708 args.name = "UMA Zones";
1709 args.size = sizeof(struct uma_zone) +
1710 (sizeof(struct uma_cache) * (mp_maxid + 1));
1711 args.ctor = zone_ctor;
1712 args.dtor = zone_dtor;
1713 args.uminit = zero_init;
1714 args.fini = NULL;
1715 args.keg = NULL;
1716 args.align = 32 - 1;
1717 args.flags = UMA_ZFLAG_INTERNAL;
1718 /* The initial zone has no Per cpu queues so it's smaller */
1719 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1720
1721 #ifdef UMA_DEBUG
1722 printf("Initializing pcpu cache locks.\n");
1723 #endif
1724 #ifdef UMA_DEBUG
1725 printf("Creating slab and hash zones.\n");
1726 #endif
1727
1728 /*
1729 * This is the max number of free list items we'll have with
1730 * offpage slabs.
1731 */
1732 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1733 slabsize += sizeof(struct uma_slab);
1734
1735 /* Now make a zone for slab headers */
1736 slabzone = uma_zcreate("UMA Slabs",
1737 slabsize,
1738 NULL, NULL, NULL, NULL,
1739 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1740
1741 /*
1742 * We also create a zone for the bigger slabs with reference
1743 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1744 */
1745 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1746 slabsize += sizeof(struct uma_slab_refcnt);
1747 slabrefzone = uma_zcreate("UMA RCntSlabs",
1748 slabsize,
1749 NULL, NULL, NULL, NULL,
1750 UMA_ALIGN_PTR,
1751 UMA_ZFLAG_INTERNAL);
1752
1753 hashzone = uma_zcreate("UMA Hash",
1754 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1755 NULL, NULL, NULL, NULL,
1756 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1757
1758 bucket_init();
1759
1760 #if defined(UMA_MD_SMALL_ALLOC) && !defined(UMA_MD_SMALL_ALLOC_NEEDS_VM)
1761 booted = 1;
1762 #endif
1763
1764 #ifdef UMA_DEBUG
1765 printf("UMA startup complete.\n");
1766 #endif
1767 }
1768
1769 /* see uma.h */
1770 void
1771 uma_startup2(void)
1772 {
1773 booted = 1;
1774 bucket_enable();
1775 #ifdef UMA_DEBUG
1776 printf("UMA startup2 complete.\n");
1777 #endif
1778 }
1779
1780 /*
1781 * Initialize our callout handle
1782 *
1783 */
1784
1785 static void
1786 uma_startup3(void)
1787 {
1788 #ifdef UMA_DEBUG
1789 printf("Starting callout.\n");
1790 #endif
1791 callout_init(&uma_callout, CALLOUT_MPSAFE);
1792 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1793 #ifdef UMA_DEBUG
1794 printf("UMA startup3 complete.\n");
1795 #endif
1796 }
1797
1798 static uma_keg_t
1799 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1800 int align, u_int32_t flags)
1801 {
1802 struct uma_kctor_args args;
1803
1804 args.size = size;
1805 args.uminit = uminit;
1806 args.fini = fini;
1807 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1808 args.flags = flags;
1809 args.zone = zone;
1810 return (zone_alloc_item(kegs, &args, M_WAITOK));
1811 }
1812
1813 /* See uma.h */
1814 void
1815 uma_set_align(int align)
1816 {
1817
1818 if (align != UMA_ALIGN_CACHE)
1819 uma_align_cache = align;
1820 }
1821
1822 /* See uma.h */
1823 uma_zone_t
1824 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1825 uma_init uminit, uma_fini fini, int align, u_int32_t flags)
1826
1827 {
1828 struct uma_zctor_args args;
1829
1830 /* This stuff is essential for the zone ctor */
1831 args.name = name;
1832 args.size = size;
1833 args.ctor = ctor;
1834 args.dtor = dtor;
1835 args.uminit = uminit;
1836 args.fini = fini;
1837 args.align = align;
1838 args.flags = flags;
1839 args.keg = NULL;
1840
1841 return (zone_alloc_item(zones, &args, M_WAITOK));
1842 }
1843
1844 /* See uma.h */
1845 uma_zone_t
1846 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1847 uma_init zinit, uma_fini zfini, uma_zone_t master)
1848 {
1849 struct uma_zctor_args args;
1850 uma_keg_t keg;
1851
1852 keg = zone_first_keg(master);
1853 args.name = name;
1854 args.size = keg->uk_size;
1855 args.ctor = ctor;
1856 args.dtor = dtor;
1857 args.uminit = zinit;
1858 args.fini = zfini;
1859 args.align = keg->uk_align;
1860 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1861 args.keg = keg;
1862
1863 /* XXX Attaches only one keg of potentially many. */
1864 return (zone_alloc_item(zones, &args, M_WAITOK));
1865 }
1866
1867 static void
1868 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1869 {
1870 if (a < b) {
1871 ZONE_LOCK(a);
1872 mtx_lock_flags(b->uz_lock, MTX_DUPOK);
1873 } else {
1874 ZONE_LOCK(b);
1875 mtx_lock_flags(a->uz_lock, MTX_DUPOK);
1876 }
1877 }
1878
1879 static void
1880 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1881 {
1882
1883 ZONE_UNLOCK(a);
1884 ZONE_UNLOCK(b);
1885 }
1886
1887 int
1888 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1889 {
1890 uma_klink_t klink;
1891 uma_klink_t kl;
1892 int error;
1893
1894 error = 0;
1895 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1896
1897 zone_lock_pair(zone, master);
1898 /*
1899 * zone must use vtoslab() to resolve objects and must already be
1900 * a secondary.
1901 */
1902 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1903 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1904 error = EINVAL;
1905 goto out;
1906 }
1907 /*
1908 * The new master must also use vtoslab().
1909 */
1910 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1911 error = EINVAL;
1912 goto out;
1913 }
1914 /*
1915 * Both must either be refcnt, or not be refcnt.
1916 */
1917 if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1918 (master->uz_flags & UMA_ZONE_REFCNT)) {
1919 error = EINVAL;
1920 goto out;
1921 }
1922 /*
1923 * The underlying object must be the same size. rsize
1924 * may be different.
1925 */
1926 if (master->uz_size != zone->uz_size) {
1927 error = E2BIG;
1928 goto out;
1929 }
1930 /*
1931 * Put it at the end of the list.
1932 */
1933 klink->kl_keg = zone_first_keg(master);
1934 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1935 if (LIST_NEXT(kl, kl_link) == NULL) {
1936 LIST_INSERT_AFTER(kl, klink, kl_link);
1937 break;
1938 }
1939 }
1940 klink = NULL;
1941 zone->uz_flags |= UMA_ZFLAG_MULTI;
1942 zone->uz_slab = zone_fetch_slab_multi;
1943
1944 out:
1945 zone_unlock_pair(zone, master);
1946 if (klink != NULL)
1947 free(klink, M_TEMP);
1948
1949 return (error);
1950 }
1951
1952
1953 /* See uma.h */
1954 void
1955 uma_zdestroy(uma_zone_t zone)
1956 {
1957
1958 zone_free_item(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1959 }
1960
1961 /* See uma.h */
1962 void *
1963 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1964 {
1965 void *item;
1966 uma_cache_t cache;
1967 uma_bucket_t bucket;
1968 int cpu;
1969
1970 /* This is the fast path allocation */
1971 #ifdef UMA_DEBUG_ALLOC_1
1972 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1973 #endif
1974 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1975 zone->uz_name, flags);
1976
1977 if (flags & M_WAITOK) {
1978 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1979 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1980 }
1981
1982 /*
1983 * If possible, allocate from the per-CPU cache. There are two
1984 * requirements for safe access to the per-CPU cache: (1) the thread
1985 * accessing the cache must not be preempted or yield during access,
1986 * and (2) the thread must not migrate CPUs without switching which
1987 * cache it accesses. We rely on a critical section to prevent
1988 * preemption and migration. We release the critical section in
1989 * order to acquire the zone mutex if we are unable to allocate from
1990 * the current cache; when we re-acquire the critical section, we
1991 * must detect and handle migration if it has occurred.
1992 */
1993 zalloc_restart:
1994 critical_enter();
1995 cpu = curcpu;
1996 cache = &zone->uz_cpu[cpu];
1997
1998 zalloc_start:
1999 bucket = cache->uc_allocbucket;
2000
2001 if (bucket) {
2002 if (bucket->ub_cnt > 0) {
2003 bucket->ub_cnt--;
2004 item = bucket->ub_bucket[bucket->ub_cnt];
2005 #ifdef INVARIANTS
2006 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2007 #endif
2008 KASSERT(item != NULL,
2009 ("uma_zalloc: Bucket pointer mangled."));
2010 cache->uc_allocs++;
2011 critical_exit();
2012 #ifdef INVARIANTS
2013 ZONE_LOCK(zone);
2014 uma_dbg_alloc(zone, NULL, item);
2015 ZONE_UNLOCK(zone);
2016 #endif
2017 if (zone->uz_ctor != NULL) {
2018 if (zone->uz_ctor(item, zone->uz_size,
2019 udata, flags) != 0) {
2020 zone_free_item(zone, item, udata,
2021 SKIP_DTOR, ZFREE_STATFAIL |
2022 ZFREE_STATFREE);
2023 return (NULL);
2024 }
2025 }
2026 if (flags & M_ZERO)
2027 bzero(item, zone->uz_size);
2028 return (item);
2029 } else if (cache->uc_freebucket) {
2030 /*
2031 * We have run out of items in our allocbucket.
2032 * See if we can switch with our free bucket.
2033 */
2034 if (cache->uc_freebucket->ub_cnt > 0) {
2035 #ifdef UMA_DEBUG_ALLOC
2036 printf("uma_zalloc: Swapping empty with"
2037 " alloc.\n");
2038 #endif
2039 bucket = cache->uc_freebucket;
2040 cache->uc_freebucket = cache->uc_allocbucket;
2041 cache->uc_allocbucket = bucket;
2042
2043 goto zalloc_start;
2044 }
2045 }
2046 }
2047 /*
2048 * Attempt to retrieve the item from the per-CPU cache has failed, so
2049 * we must go back to the zone. This requires the zone lock, so we
2050 * must drop the critical section, then re-acquire it when we go back
2051 * to the cache. Since the critical section is released, we may be
2052 * preempted or migrate. As such, make sure not to maintain any
2053 * thread-local state specific to the cache from prior to releasing
2054 * the critical section.
2055 */
2056 critical_exit();
2057 ZONE_LOCK(zone);
2058 critical_enter();
2059 cpu = curcpu;
2060 cache = &zone->uz_cpu[cpu];
2061 bucket = cache->uc_allocbucket;
2062 if (bucket != NULL) {
2063 if (bucket->ub_cnt > 0) {
2064 ZONE_UNLOCK(zone);
2065 goto zalloc_start;
2066 }
2067 bucket = cache->uc_freebucket;
2068 if (bucket != NULL && bucket->ub_cnt > 0) {
2069 ZONE_UNLOCK(zone);
2070 goto zalloc_start;
2071 }
2072 }
2073
2074 /* Since we have locked the zone we may as well send back our stats */
2075 zone->uz_allocs += cache->uc_allocs;
2076 cache->uc_allocs = 0;
2077 zone->uz_frees += cache->uc_frees;
2078 cache->uc_frees = 0;
2079
2080 /* Our old one is now a free bucket */
2081 if (cache->uc_allocbucket) {
2082 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
2083 ("uma_zalloc_arg: Freeing a non free bucket."));
2084 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2085 cache->uc_allocbucket, ub_link);
2086 cache->uc_allocbucket = NULL;
2087 }
2088
2089 /* Check the free list for a new alloc bucket */
2090 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
2091 KASSERT(bucket->ub_cnt != 0,
2092 ("uma_zalloc_arg: Returning an empty bucket."));
2093
2094 LIST_REMOVE(bucket, ub_link);
2095 cache->uc_allocbucket = bucket;
2096 ZONE_UNLOCK(zone);
2097 goto zalloc_start;
2098 }
2099 /* We are no longer associated with this CPU. */
2100 critical_exit();
2101
2102 /* Bump up our uz_count so we get here less */
2103 if (zone->uz_count < BUCKET_MAX)
2104 zone->uz_count++;
2105
2106 /*
2107 * Now lets just fill a bucket and put it on the free list. If that
2108 * works we'll restart the allocation from the begining.
2109 */
2110 if (zone_alloc_bucket(zone, flags)) {
2111 ZONE_UNLOCK(zone);
2112 goto zalloc_restart;
2113 }
2114 ZONE_UNLOCK(zone);
2115 /*
2116 * We may not be able to get a bucket so return an actual item.
2117 */
2118 #ifdef UMA_DEBUG
2119 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2120 #endif
2121
2122 item = zone_alloc_item(zone, udata, flags);
2123 return (item);
2124 }
2125
2126 static uma_slab_t
2127 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2128 {
2129 uma_slab_t slab;
2130
2131 mtx_assert(&keg->uk_lock, MA_OWNED);
2132 slab = NULL;
2133
2134 for (;;) {
2135 /*
2136 * Find a slab with some space. Prefer slabs that are partially
2137 * used over those that are totally full. This helps to reduce
2138 * fragmentation.
2139 */
2140 if (keg->uk_free != 0) {
2141 if (!LIST_EMPTY(&keg->uk_part_slab)) {
2142 slab = LIST_FIRST(&keg->uk_part_slab);
2143 } else {
2144 slab = LIST_FIRST(&keg->uk_free_slab);
2145 LIST_REMOVE(slab, us_link);
2146 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2147 us_link);
2148 }
2149 MPASS(slab->us_keg == keg);
2150 return (slab);
2151 }
2152
2153 /*
2154 * M_NOVM means don't ask at all!
2155 */
2156 if (flags & M_NOVM)
2157 break;
2158
2159 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2160 keg->uk_flags |= UMA_ZFLAG_FULL;
2161 /*
2162 * If this is not a multi-zone, set the FULL bit.
2163 * Otherwise slab_multi() takes care of it.
2164 */
2165 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0)
2166 zone->uz_flags |= UMA_ZFLAG_FULL;
2167 if (flags & M_NOWAIT)
2168 break;
2169 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2170 continue;
2171 }
2172 keg->uk_recurse++;
2173 slab = keg_alloc_slab(keg, zone, flags);
2174 keg->uk_recurse--;
2175 /*
2176 * If we got a slab here it's safe to mark it partially used
2177 * and return. We assume that the caller is going to remove
2178 * at least one item.
2179 */
2180 if (slab) {
2181 MPASS(slab->us_keg == keg);
2182 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2183 return (slab);
2184 }
2185 /*
2186 * We might not have been able to get a slab but another cpu
2187 * could have while we were unlocked. Check again before we
2188 * fail.
2189 */
2190 flags |= M_NOVM;
2191 }
2192 return (slab);
2193 }
2194
2195 static inline void
2196 zone_relock(uma_zone_t zone, uma_keg_t keg)
2197 {
2198 if (zone->uz_lock != &keg->uk_lock) {
2199 KEG_UNLOCK(keg);
2200 ZONE_LOCK(zone);
2201 }
2202 }
2203
2204 static inline void
2205 keg_relock(uma_keg_t keg, uma_zone_t zone)
2206 {
2207 if (zone->uz_lock != &keg->uk_lock) {
2208 ZONE_UNLOCK(zone);
2209 KEG_LOCK(keg);
2210 }
2211 }
2212
2213 static uma_slab_t
2214 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2215 {
2216 uma_slab_t slab;
2217
2218 if (keg == NULL)
2219 keg = zone_first_keg(zone);
2220 /*
2221 * This is to prevent us from recursively trying to allocate
2222 * buckets. The problem is that if an allocation forces us to
2223 * grab a new bucket we will call page_alloc, which will go off
2224 * and cause the vm to allocate vm_map_entries. If we need new
2225 * buckets there too we will recurse in kmem_alloc and bad
2226 * things happen. So instead we return a NULL bucket, and make
2227 * the code that allocates buckets smart enough to deal with it
2228 */
2229 if (keg->uk_flags & UMA_ZFLAG_BUCKET && keg->uk_recurse != 0)
2230 return (NULL);
2231
2232 for (;;) {
2233 slab = keg_fetch_slab(keg, zone, flags);
2234 if (slab)
2235 return (slab);
2236 if (flags & (M_NOWAIT | M_NOVM))
2237 break;
2238 }
2239 return (NULL);
2240 }
2241
2242 /*
2243 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2244 * with the keg locked. Caller must call zone_relock() afterwards if the
2245 * zone lock is required. On NULL the zone lock is held.
2246 *
2247 * The last pointer is used to seed the search. It is not required.
2248 */
2249 static uma_slab_t
2250 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2251 {
2252 uma_klink_t klink;
2253 uma_slab_t slab;
2254 uma_keg_t keg;
2255 int flags;
2256 int empty;
2257 int full;
2258
2259 /*
2260 * Don't wait on the first pass. This will skip limit tests
2261 * as well. We don't want to block if we can find a provider
2262 * without blocking.
2263 */
2264 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2265 /*
2266 * Use the last slab allocated as a hint for where to start
2267 * the search.
2268 */
2269 if (last) {
2270 slab = keg_fetch_slab(last, zone, flags);
2271 if (slab)
2272 return (slab);
2273 zone_relock(zone, last);
2274 last = NULL;
2275 }
2276 /*
2277 * Loop until we have a slab incase of transient failures
2278 * while M_WAITOK is specified. I'm not sure this is 100%
2279 * required but we've done it for so long now.
2280 */
2281 for (;;) {
2282 empty = 0;
2283 full = 0;
2284 /*
2285 * Search the available kegs for slabs. Be careful to hold the
2286 * correct lock while calling into the keg layer.
2287 */
2288 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2289 keg = klink->kl_keg;
2290 keg_relock(keg, zone);
2291 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2292 slab = keg_fetch_slab(keg, zone, flags);
2293 if (slab)
2294 return (slab);
2295 }
2296 if (keg->uk_flags & UMA_ZFLAG_FULL)
2297 full++;
2298 else
2299 empty++;
2300 zone_relock(zone, keg);
2301 }
2302 if (rflags & (M_NOWAIT | M_NOVM))
2303 break;
2304 flags = rflags;
2305 /*
2306 * All kegs are full. XXX We can't atomically check all kegs
2307 * and sleep so just sleep for a short period and retry.
2308 */
2309 if (full && !empty) {
2310 zone->uz_flags |= UMA_ZFLAG_FULL;
2311 msleep(zone, zone->uz_lock, PVM, "zonelimit", hz/100);
2312 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2313 continue;
2314 }
2315 }
2316 return (NULL);
2317 }
2318
2319 static void *
2320 slab_alloc_item(uma_zone_t zone, uma_slab_t slab)
2321 {
2322 uma_keg_t keg;
2323 uma_slabrefcnt_t slabref;
2324 void *item;
2325 u_int8_t freei;
2326
2327 keg = slab->us_keg;
2328 mtx_assert(&keg->uk_lock, MA_OWNED);
2329
2330 freei = slab->us_firstfree;
2331 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2332 slabref = (uma_slabrefcnt_t)slab;
2333 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2334 } else {
2335 slab->us_firstfree = slab->us_freelist[freei].us_item;
2336 }
2337 item = slab->us_data + (keg->uk_rsize * freei);
2338
2339 slab->us_freecount--;
2340 keg->uk_free--;
2341 #ifdef INVARIANTS
2342 uma_dbg_alloc(zone, slab, item);
2343 #endif
2344 /* Move this slab to the full list */
2345 if (slab->us_freecount == 0) {
2346 LIST_REMOVE(slab, us_link);
2347 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2348 }
2349
2350 return (item);
2351 }
2352
2353 static int
2354 zone_alloc_bucket(uma_zone_t zone, int flags)
2355 {
2356 uma_bucket_t bucket;
2357 uma_slab_t slab;
2358 uma_keg_t keg;
2359 int16_t saved;
2360 int max, origflags = flags;
2361
2362 /*
2363 * Try this zone's free list first so we don't allocate extra buckets.
2364 */
2365 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2366 KASSERT(bucket->ub_cnt == 0,
2367 ("zone_alloc_bucket: Bucket on free list is not empty."));
2368 LIST_REMOVE(bucket, ub_link);
2369 } else {
2370 int bflags;
2371
2372 bflags = (flags & ~M_ZERO);
2373 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2374 bflags |= M_NOVM;
2375
2376 ZONE_UNLOCK(zone);
2377 bucket = bucket_alloc(zone->uz_count, bflags);
2378 ZONE_LOCK(zone);
2379 }
2380
2381 if (bucket == NULL) {
2382 return (0);
2383 }
2384
2385 #ifdef SMP
2386 /*
2387 * This code is here to limit the number of simultaneous bucket fills
2388 * for any given zone to the number of per cpu caches in this zone. This
2389 * is done so that we don't allocate more memory than we really need.
2390 */
2391 if (zone->uz_fills >= mp_ncpus)
2392 goto done;
2393
2394 #endif
2395 zone->uz_fills++;
2396
2397 max = MIN(bucket->ub_entries, zone->uz_count);
2398 /* Try to keep the buckets totally full */
2399 saved = bucket->ub_cnt;
2400 slab = NULL;
2401 keg = NULL;
2402 while (bucket->ub_cnt < max &&
2403 (slab = zone->uz_slab(zone, keg, flags)) != NULL) {
2404 keg = slab->us_keg;
2405 while (slab->us_freecount && bucket->ub_cnt < max) {
2406 bucket->ub_bucket[bucket->ub_cnt++] =
2407 slab_alloc_item(zone, slab);
2408 }
2409
2410 /* Don't block on the next fill */
2411 flags |= M_NOWAIT;
2412 }
2413 if (slab)
2414 zone_relock(zone, keg);
2415
2416 /*
2417 * We unlock here because we need to call the zone's init.
2418 * It should be safe to unlock because the slab dealt with
2419 * above is already on the appropriate list within the keg
2420 * and the bucket we filled is not yet on any list, so we
2421 * own it.
2422 */
2423 if (zone->uz_init != NULL) {
2424 int i;
2425
2426 ZONE_UNLOCK(zone);
2427 for (i = saved; i < bucket->ub_cnt; i++)
2428 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2429 origflags) != 0)
2430 break;
2431 /*
2432 * If we couldn't initialize the whole bucket, put the
2433 * rest back onto the freelist.
2434 */
2435 if (i != bucket->ub_cnt) {
2436 int j;
2437
2438 for (j = i; j < bucket->ub_cnt; j++) {
2439 zone_free_item(zone, bucket->ub_bucket[j],
2440 NULL, SKIP_FINI, 0);
2441 #ifdef INVARIANTS
2442 bucket->ub_bucket[j] = NULL;
2443 #endif
2444 }
2445 bucket->ub_cnt = i;
2446 }
2447 ZONE_LOCK(zone);
2448 }
2449
2450 zone->uz_fills--;
2451 if (bucket->ub_cnt != 0) {
2452 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2453 bucket, ub_link);
2454 return (1);
2455 }
2456 #ifdef SMP
2457 done:
2458 #endif
2459 bucket_free(bucket);
2460
2461 return (0);
2462 }
2463 /*
2464 * Allocates an item for an internal zone
2465 *
2466 * Arguments
2467 * zone The zone to alloc for.
2468 * udata The data to be passed to the constructor.
2469 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2470 *
2471 * Returns
2472 * NULL if there is no memory and M_NOWAIT is set
2473 * An item if successful
2474 */
2475
2476 static void *
2477 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2478 {
2479 uma_slab_t slab;
2480 void *item;
2481
2482 item = NULL;
2483
2484 #ifdef UMA_DEBUG_ALLOC
2485 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2486 #endif
2487 ZONE_LOCK(zone);
2488
2489 slab = zone->uz_slab(zone, NULL, flags);
2490 if (slab == NULL) {
2491 zone->uz_fails++;
2492 ZONE_UNLOCK(zone);
2493 return (NULL);
2494 }
2495
2496 item = slab_alloc_item(zone, slab);
2497
2498 zone_relock(zone, slab->us_keg);
2499 zone->uz_allocs++;
2500 ZONE_UNLOCK(zone);
2501
2502 /*
2503 * We have to call both the zone's init (not the keg's init)
2504 * and the zone's ctor. This is because the item is going from
2505 * a keg slab directly to the user, and the user is expecting it
2506 * to be both zone-init'd as well as zone-ctor'd.
2507 */
2508 if (zone->uz_init != NULL) {
2509 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2510 zone_free_item(zone, item, udata, SKIP_FINI,
2511 ZFREE_STATFAIL | ZFREE_STATFREE);
2512 return (NULL);
2513 }
2514 }
2515 if (zone->uz_ctor != NULL) {
2516 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2517 zone_free_item(zone, item, udata, SKIP_DTOR,
2518 ZFREE_STATFAIL | ZFREE_STATFREE);
2519 return (NULL);
2520 }
2521 }
2522 if (flags & M_ZERO)
2523 bzero(item, zone->uz_size);
2524
2525 return (item);
2526 }
2527
2528 /* See uma.h */
2529 void
2530 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2531 {
2532 uma_cache_t cache;
2533 uma_bucket_t bucket;
2534 int bflags;
2535 int cpu;
2536
2537 #ifdef UMA_DEBUG_ALLOC_1
2538 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2539 #endif
2540 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2541 zone->uz_name);
2542
2543 /* uma_zfree(..., NULL) does nothing, to match free(9). */
2544 if (item == NULL)
2545 return;
2546
2547 if (zone->uz_dtor)
2548 zone->uz_dtor(item, zone->uz_size, udata);
2549
2550 #ifdef INVARIANTS
2551 ZONE_LOCK(zone);
2552 if (zone->uz_flags & UMA_ZONE_MALLOC)
2553 uma_dbg_free(zone, udata, item);
2554 else
2555 uma_dbg_free(zone, NULL, item);
2556 ZONE_UNLOCK(zone);
2557 #endif
2558 /*
2559 * The race here is acceptable. If we miss it we'll just have to wait
2560 * a little longer for the limits to be reset.
2561 */
2562 if (zone->uz_flags & UMA_ZFLAG_FULL)
2563 goto zfree_internal;
2564
2565 /*
2566 * If possible, free to the per-CPU cache. There are two
2567 * requirements for safe access to the per-CPU cache: (1) the thread
2568 * accessing the cache must not be preempted or yield during access,
2569 * and (2) the thread must not migrate CPUs without switching which
2570 * cache it accesses. We rely on a critical section to prevent
2571 * preemption and migration. We release the critical section in
2572 * order to acquire the zone mutex if we are unable to free to the
2573 * current cache; when we re-acquire the critical section, we must
2574 * detect and handle migration if it has occurred.
2575 */
2576 zfree_restart:
2577 critical_enter();
2578 cpu = curcpu;
2579 cache = &zone->uz_cpu[cpu];
2580
2581 zfree_start:
2582 bucket = cache->uc_freebucket;
2583
2584 if (bucket) {
2585 /*
2586 * Do we have room in our bucket? It is OK for this uz count
2587 * check to be slightly out of sync.
2588 */
2589
2590 if (bucket->ub_cnt < bucket->ub_entries) {
2591 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2592 ("uma_zfree: Freeing to non free bucket index."));
2593 bucket->ub_bucket[bucket->ub_cnt] = item;
2594 bucket->ub_cnt++;
2595 cache->uc_frees++;
2596 critical_exit();
2597 return;
2598 } else if (cache->uc_allocbucket) {
2599 #ifdef UMA_DEBUG_ALLOC
2600 printf("uma_zfree: Swapping buckets.\n");
2601 #endif
2602 /*
2603 * We have run out of space in our freebucket.
2604 * See if we can switch with our alloc bucket.
2605 */
2606 if (cache->uc_allocbucket->ub_cnt <
2607 cache->uc_freebucket->ub_cnt) {
2608 bucket = cache->uc_freebucket;
2609 cache->uc_freebucket = cache->uc_allocbucket;
2610 cache->uc_allocbucket = bucket;
2611 goto zfree_start;
2612 }
2613 }
2614 }
2615 /*
2616 * We can get here for two reasons:
2617 *
2618 * 1) The buckets are NULL
2619 * 2) The alloc and free buckets are both somewhat full.
2620 *
2621 * We must go back the zone, which requires acquiring the zone lock,
2622 * which in turn means we must release and re-acquire the critical
2623 * section. Since the critical section is released, we may be
2624 * preempted or migrate. As such, make sure not to maintain any
2625 * thread-local state specific to the cache from prior to releasing
2626 * the critical section.
2627 */
2628 critical_exit();
2629 ZONE_LOCK(zone);
2630 critical_enter();
2631 cpu = curcpu;
2632 cache = &zone->uz_cpu[cpu];
2633 if (cache->uc_freebucket != NULL) {
2634 if (cache->uc_freebucket->ub_cnt <
2635 cache->uc_freebucket->ub_entries) {
2636 ZONE_UNLOCK(zone);
2637 goto zfree_start;
2638 }
2639 if (cache->uc_allocbucket != NULL &&
2640 (cache->uc_allocbucket->ub_cnt <
2641 cache->uc_freebucket->ub_cnt)) {
2642 ZONE_UNLOCK(zone);
2643 goto zfree_start;
2644 }
2645 }
2646
2647 /* Since we have locked the zone we may as well send back our stats */
2648 zone->uz_allocs += cache->uc_allocs;
2649 cache->uc_allocs = 0;
2650 zone->uz_frees += cache->uc_frees;
2651 cache->uc_frees = 0;
2652
2653 bucket = cache->uc_freebucket;
2654 cache->uc_freebucket = NULL;
2655
2656 /* Can we throw this on the zone full list? */
2657 if (bucket != NULL) {
2658 #ifdef UMA_DEBUG_ALLOC
2659 printf("uma_zfree: Putting old bucket on the free list.\n");
2660 #endif
2661 /* ub_cnt is pointing to the last free item */
2662 KASSERT(bucket->ub_cnt != 0,
2663 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2664 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2665 bucket, ub_link);
2666 }
2667 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2668 LIST_REMOVE(bucket, ub_link);
2669 ZONE_UNLOCK(zone);
2670 cache->uc_freebucket = bucket;
2671 goto zfree_start;
2672 }
2673 /* We are no longer associated with this CPU. */
2674 critical_exit();
2675
2676 /* And the zone.. */
2677 ZONE_UNLOCK(zone);
2678
2679 #ifdef UMA_DEBUG_ALLOC
2680 printf("uma_zfree: Allocating new free bucket.\n");
2681 #endif
2682 bflags = M_NOWAIT;
2683
2684 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2685 bflags |= M_NOVM;
2686 bucket = bucket_alloc(zone->uz_count, bflags);
2687 if (bucket) {
2688 ZONE_LOCK(zone);
2689 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2690 bucket, ub_link);
2691 ZONE_UNLOCK(zone);
2692 goto zfree_restart;
2693 }
2694
2695 /*
2696 * If nothing else caught this, we'll just do an internal free.
2697 */
2698 zfree_internal:
2699 zone_free_item(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2700
2701 return;
2702 }
2703
2704 /*
2705 * Frees an item to an INTERNAL zone or allocates a free bucket
2706 *
2707 * Arguments:
2708 * zone The zone to free to
2709 * item The item we're freeing
2710 * udata User supplied data for the dtor
2711 * skip Skip dtors and finis
2712 */
2713 static void
2714 zone_free_item(uma_zone_t zone, void *item, void *udata,
2715 enum zfreeskip skip, int flags)
2716 {
2717 uma_slab_t slab;
2718 uma_slabrefcnt_t slabref;
2719 uma_keg_t keg;
2720 u_int8_t *mem;
2721 u_int8_t freei;
2722 int clearfull;
2723
2724 if (skip < SKIP_DTOR && zone->uz_dtor)
2725 zone->uz_dtor(item, zone->uz_size, udata);
2726
2727 if (skip < SKIP_FINI && zone->uz_fini)
2728 zone->uz_fini(item, zone->uz_size);
2729
2730 ZONE_LOCK(zone);
2731
2732 if (flags & ZFREE_STATFAIL)
2733 zone->uz_fails++;
2734 if (flags & ZFREE_STATFREE)
2735 zone->uz_frees++;
2736
2737 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2738 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2739 keg = zone_first_keg(zone); /* Must only be one. */
2740 if (zone->uz_flags & UMA_ZONE_HASH) {
2741 slab = hash_sfind(&keg->uk_hash, mem);
2742 } else {
2743 mem += keg->uk_pgoff;
2744 slab = (uma_slab_t)mem;
2745 }
2746 } else {
2747 /* This prevents redundant lookups via free(). */
2748 if ((zone->uz_flags & UMA_ZONE_MALLOC) && udata != NULL)
2749 slab = (uma_slab_t)udata;
2750 else
2751 slab = vtoslab((vm_offset_t)item);
2752 keg = slab->us_keg;
2753 keg_relock(keg, zone);
2754 }
2755 MPASS(keg == slab->us_keg);
2756
2757 /* Do we need to remove from any lists? */
2758 if (slab->us_freecount+1 == keg->uk_ipers) {
2759 LIST_REMOVE(slab, us_link);
2760 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2761 } else if (slab->us_freecount == 0) {
2762 LIST_REMOVE(slab, us_link);
2763 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2764 }
2765
2766 /* Slab management stuff */
2767 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2768 / keg->uk_rsize;
2769
2770 #ifdef INVARIANTS
2771 if (!skip)
2772 uma_dbg_free(zone, slab, item);
2773 #endif
2774
2775 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2776 slabref = (uma_slabrefcnt_t)slab;
2777 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2778 } else {
2779 slab->us_freelist[freei].us_item = slab->us_firstfree;
2780 }
2781 slab->us_firstfree = freei;
2782 slab->us_freecount++;
2783
2784 /* Zone statistics */
2785 keg->uk_free++;
2786
2787 clearfull = 0;
2788 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2789 if (keg->uk_pages < keg->uk_maxpages) {
2790 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2791 clearfull = 1;
2792 }
2793
2794 /*
2795 * We can handle one more allocation. Since we're clearing ZFLAG_FULL,
2796 * wake up all procs blocked on pages. This should be uncommon, so
2797 * keeping this simple for now (rather than adding count of blocked
2798 * threads etc).
2799 */
2800 wakeup(keg);
2801 }
2802 if (clearfull) {
2803 zone_relock(zone, keg);
2804 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2805 wakeup(zone);
2806 ZONE_UNLOCK(zone);
2807 } else
2808 KEG_UNLOCK(keg);
2809 }
2810
2811 /* See uma.h */
2812 void
2813 uma_zone_set_max(uma_zone_t zone, int nitems)
2814 {
2815 uma_keg_t keg;
2816
2817 ZONE_LOCK(zone);
2818 keg = zone_first_keg(zone);
2819 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2820 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2821 keg->uk_maxpages += keg->uk_ppera;
2822
2823 ZONE_UNLOCK(zone);
2824 }
2825
2826 /* See uma.h */
2827 int
2828 uma_zone_get_max(uma_zone_t zone)
2829 {
2830 int nitems;
2831 uma_keg_t keg;
2832
2833 ZONE_LOCK(zone);
2834 keg = zone_first_keg(zone);
2835 nitems = keg->uk_maxpages * keg->uk_ipers;
2836 ZONE_UNLOCK(zone);
2837
2838 return (nitems);
2839 }
2840
2841 /* See uma.h */
2842 int
2843 uma_zone_get_cur(uma_zone_t zone)
2844 {
2845 int64_t nitems;
2846 u_int i;
2847
2848 ZONE_LOCK(zone);
2849 nitems = zone->uz_allocs - zone->uz_frees;
2850 CPU_FOREACH(i) {
2851 /*
2852 * See the comment in sysctl_vm_zone_stats() regarding the
2853 * safety of accessing the per-cpu caches. With the zone lock
2854 * held, it is safe, but can potentially result in stale data.
2855 */
2856 nitems += zone->uz_cpu[i].uc_allocs -
2857 zone->uz_cpu[i].uc_frees;
2858 }
2859 ZONE_UNLOCK(zone);
2860
2861 return (nitems < 0 ? 0 : nitems);
2862 }
2863
2864 /* See uma.h */
2865 void
2866 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2867 {
2868 uma_keg_t keg;
2869
2870 ZONE_LOCK(zone);
2871 keg = zone_first_keg(zone);
2872 KASSERT(keg->uk_pages == 0,
2873 ("uma_zone_set_init on non-empty keg"));
2874 keg->uk_init = uminit;
2875 ZONE_UNLOCK(zone);
2876 }
2877
2878 /* See uma.h */
2879 void
2880 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2881 {
2882 uma_keg_t keg;
2883
2884 ZONE_LOCK(zone);
2885 keg = zone_first_keg(zone);
2886 KASSERT(keg->uk_pages == 0,
2887 ("uma_zone_set_fini on non-empty keg"));
2888 keg->uk_fini = fini;
2889 ZONE_UNLOCK(zone);
2890 }
2891
2892 /* See uma.h */
2893 void
2894 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2895 {
2896 ZONE_LOCK(zone);
2897 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2898 ("uma_zone_set_zinit on non-empty keg"));
2899 zone->uz_init = zinit;
2900 ZONE_UNLOCK(zone);
2901 }
2902
2903 /* See uma.h */
2904 void
2905 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2906 {
2907 ZONE_LOCK(zone);
2908 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2909 ("uma_zone_set_zfini on non-empty keg"));
2910 zone->uz_fini = zfini;
2911 ZONE_UNLOCK(zone);
2912 }
2913
2914 /* See uma.h */
2915 /* XXX uk_freef is not actually used with the zone locked */
2916 void
2917 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2918 {
2919
2920 ZONE_LOCK(zone);
2921 zone_first_keg(zone)->uk_freef = freef;
2922 ZONE_UNLOCK(zone);
2923 }
2924
2925 /* See uma.h */
2926 /* XXX uk_allocf is not actually used with the zone locked */
2927 void
2928 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2929 {
2930 uma_keg_t keg;
2931
2932 ZONE_LOCK(zone);
2933 keg = zone_first_keg(zone);
2934 keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2935 keg->uk_allocf = allocf;
2936 ZONE_UNLOCK(zone);
2937 }
2938
2939 /* See uma.h */
2940 int
2941 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2942 {
2943 uma_keg_t keg;
2944 vm_offset_t kva;
2945 int pages;
2946
2947 keg = zone_first_keg(zone);
2948 pages = count / keg->uk_ipers;
2949
2950 if (pages * keg->uk_ipers < count)
2951 pages++;
2952
2953 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2954
2955 if (kva == 0)
2956 return (0);
2957 if (obj == NULL) {
2958 obj = vm_object_allocate(OBJT_DEFAULT,
2959 pages);
2960 } else {
2961 VM_OBJECT_LOCK_INIT(obj, "uma object");
2962 _vm_object_allocate(OBJT_DEFAULT,
2963 pages, obj);
2964 }
2965 ZONE_LOCK(zone);
2966 keg->uk_kva = kva;
2967 keg->uk_obj = obj;
2968 keg->uk_maxpages = pages;
2969 keg->uk_allocf = obj_alloc;
2970 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2971 ZONE_UNLOCK(zone);
2972 return (1);
2973 }
2974
2975 /* See uma.h */
2976 void
2977 uma_prealloc(uma_zone_t zone, int items)
2978 {
2979 int slabs;
2980 uma_slab_t slab;
2981 uma_keg_t keg;
2982
2983 keg = zone_first_keg(zone);
2984 ZONE_LOCK(zone);
2985 slabs = items / keg->uk_ipers;
2986 if (slabs * keg->uk_ipers < items)
2987 slabs++;
2988 while (slabs > 0) {
2989 slab = keg_alloc_slab(keg, zone, M_WAITOK);
2990 if (slab == NULL)
2991 break;
2992 MPASS(slab->us_keg == keg);
2993 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2994 slabs--;
2995 }
2996 ZONE_UNLOCK(zone);
2997 }
2998
2999 /* See uma.h */
3000 u_int32_t *
3001 uma_find_refcnt(uma_zone_t zone, void *item)
3002 {
3003 uma_slabrefcnt_t slabref;
3004 uma_keg_t keg;
3005 u_int32_t *refcnt;
3006 int idx;
3007
3008 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
3009 (~UMA_SLAB_MASK));
3010 keg = slabref->us_keg;
3011 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
3012 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3013 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
3014 / keg->uk_rsize;
3015 refcnt = &slabref->us_freelist[idx].us_refcnt;
3016 return refcnt;
3017 }
3018
3019 /* See uma.h */
3020 void
3021 uma_reclaim(void)
3022 {
3023 #ifdef UMA_DEBUG
3024 printf("UMA: vm asked us to release pages!\n");
3025 #endif
3026 bucket_enable();
3027 zone_foreach(zone_drain);
3028 /*
3029 * Some slabs may have been freed but this zone will be visited early
3030 * we visit again so that we can free pages that are empty once other
3031 * zones are drained. We have to do the same for buckets.
3032 */
3033 zone_drain(slabzone);
3034 zone_drain(slabrefzone);
3035 bucket_zone_drain();
3036 }
3037
3038 /* See uma.h */
3039 int
3040 uma_zone_exhausted(uma_zone_t zone)
3041 {
3042 int full;
3043
3044 ZONE_LOCK(zone);
3045 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3046 ZONE_UNLOCK(zone);
3047 return (full);
3048 }
3049
3050 int
3051 uma_zone_exhausted_nolock(uma_zone_t zone)
3052 {
3053 return (zone->uz_flags & UMA_ZFLAG_FULL);
3054 }
3055
3056 void *
3057 uma_large_malloc(int size, int wait)
3058 {
3059 void *mem;
3060 uma_slab_t slab;
3061 u_int8_t flags;
3062
3063 slab = zone_alloc_item(slabzone, NULL, wait);
3064 if (slab == NULL)
3065 return (NULL);
3066 mem = page_alloc(NULL, size, &flags, wait);
3067 if (mem) {
3068 vsetslab((vm_offset_t)mem, slab);
3069 slab->us_data = mem;
3070 slab->us_flags = flags | UMA_SLAB_MALLOC;
3071 slab->us_size = size;
3072 } else {
3073 zone_free_item(slabzone, slab, NULL, SKIP_NONE,
3074 ZFREE_STATFAIL | ZFREE_STATFREE);
3075 }
3076
3077 return (mem);
3078 }
3079
3080 void
3081 uma_large_free(uma_slab_t slab)
3082 {
3083 vsetobj((vm_offset_t)slab->us_data, kmem_object);
3084 page_free(slab->us_data, slab->us_size, slab->us_flags);
3085 zone_free_item(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
3086 }
3087
3088 void
3089 uma_print_stats(void)
3090 {
3091 zone_foreach(uma_print_zone);
3092 }
3093
3094 static void
3095 slab_print(uma_slab_t slab)
3096 {
3097 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
3098 slab->us_keg, slab->us_data, slab->us_freecount,
3099 slab->us_firstfree);
3100 }
3101
3102 static void
3103 cache_print(uma_cache_t cache)
3104 {
3105 printf("alloc: %p(%d), free: %p(%d)\n",
3106 cache->uc_allocbucket,
3107 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3108 cache->uc_freebucket,
3109 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3110 }
3111
3112 static void
3113 uma_print_keg(uma_keg_t keg)
3114 {
3115 uma_slab_t slab;
3116
3117 printf("keg: %s(%p) size %d(%d) flags %d ipers %d ppera %d "
3118 "out %d free %d limit %d\n",
3119 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3120 keg->uk_ipers, keg->uk_ppera,
3121 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3122 (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3123 printf("Part slabs:\n");
3124 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3125 slab_print(slab);
3126 printf("Free slabs:\n");
3127 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3128 slab_print(slab);
3129 printf("Full slabs:\n");
3130 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3131 slab_print(slab);
3132 }
3133
3134 void
3135 uma_print_zone(uma_zone_t zone)
3136 {
3137 uma_cache_t cache;
3138 uma_klink_t kl;
3139 int i;
3140
3141 printf("zone: %s(%p) size %d flags %d\n",
3142 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3143 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3144 uma_print_keg(kl->kl_keg);
3145 for (i = 0; i <= mp_maxid; i++) {
3146 if (CPU_ABSENT(i))
3147 continue;
3148 cache = &zone->uz_cpu[i];
3149 printf("CPU %d Cache:\n", i);
3150 cache_print(cache);
3151 }
3152 }
3153
3154 #ifdef DDB
3155 /*
3156 * Generate statistics across both the zone and its per-cpu cache's. Return
3157 * desired statistics if the pointer is non-NULL for that statistic.
3158 *
3159 * Note: does not update the zone statistics, as it can't safely clear the
3160 * per-CPU cache statistic.
3161 *
3162 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3163 * safe from off-CPU; we should modify the caches to track this information
3164 * directly so that we don't have to.
3165 */
3166 static void
3167 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
3168 u_int64_t *freesp)
3169 {
3170 uma_cache_t cache;
3171 u_int64_t allocs, frees;
3172 int cachefree, cpu;
3173
3174 allocs = frees = 0;
3175 cachefree = 0;
3176 for (cpu = 0; cpu <= mp_maxid; cpu++) {
3177 if (CPU_ABSENT(cpu))
3178 continue;
3179 cache = &z->uz_cpu[cpu];
3180 if (cache->uc_allocbucket != NULL)
3181 cachefree += cache->uc_allocbucket->ub_cnt;
3182 if (cache->uc_freebucket != NULL)
3183 cachefree += cache->uc_freebucket->ub_cnt;
3184 allocs += cache->uc_allocs;
3185 frees += cache->uc_frees;
3186 }
3187 allocs += z->uz_allocs;
3188 frees += z->uz_frees;
3189 if (cachefreep != NULL)
3190 *cachefreep = cachefree;
3191 if (allocsp != NULL)
3192 *allocsp = allocs;
3193 if (freesp != NULL)
3194 *freesp = frees;
3195 }
3196 #endif /* DDB */
3197
3198 static int
3199 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3200 {
3201 uma_keg_t kz;
3202 uma_zone_t z;
3203 int count;
3204
3205 count = 0;
3206 mtx_lock(&uma_mtx);
3207 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3208 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3209 count++;
3210 }
3211 mtx_unlock(&uma_mtx);
3212 return (sysctl_handle_int(oidp, &count, 0, req));
3213 }
3214
3215 static int
3216 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3217 {
3218 struct uma_stream_header ush;
3219 struct uma_type_header uth;
3220 struct uma_percpu_stat ups;
3221 uma_bucket_t bucket;
3222 struct sbuf sbuf;
3223 uma_cache_t cache;
3224 uma_klink_t kl;
3225 uma_keg_t kz;
3226 uma_zone_t z;
3227 uma_keg_t k;
3228 char *buffer;
3229 int buflen, count, error, i;
3230
3231 mtx_lock(&uma_mtx);
3232 restart:
3233 mtx_assert(&uma_mtx, MA_OWNED);
3234 count = 0;
3235 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3236 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3237 count++;
3238 }
3239 mtx_unlock(&uma_mtx);
3240
3241 buflen = sizeof(ush) + count * (sizeof(uth) + sizeof(ups) *
3242 (mp_maxid + 1)) + 1;
3243 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
3244
3245 mtx_lock(&uma_mtx);
3246 i = 0;
3247 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3248 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3249 i++;
3250 }
3251 if (i > count) {
3252 free(buffer, M_TEMP);
3253 goto restart;
3254 }
3255 count = i;
3256
3257 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
3258
3259 /*
3260 * Insert stream header.
3261 */
3262 bzero(&ush, sizeof(ush));
3263 ush.ush_version = UMA_STREAM_VERSION;
3264 ush.ush_maxcpus = (mp_maxid + 1);
3265 ush.ush_count = count;
3266 if (sbuf_bcat(&sbuf, &ush, sizeof(ush)) < 0) {
3267 mtx_unlock(&uma_mtx);
3268 error = ENOMEM;
3269 goto out;
3270 }
3271
3272 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3273 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3274 bzero(&uth, sizeof(uth));
3275 ZONE_LOCK(z);
3276 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3277 uth.uth_align = kz->uk_align;
3278 uth.uth_size = kz->uk_size;
3279 uth.uth_rsize = kz->uk_rsize;
3280 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3281 k = kl->kl_keg;
3282 uth.uth_maxpages += k->uk_maxpages;
3283 uth.uth_pages += k->uk_pages;
3284 uth.uth_keg_free += k->uk_free;
3285 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3286 * k->uk_ipers;
3287 }
3288
3289 /*
3290 * A zone is secondary is it is not the first entry
3291 * on the keg's zone list.
3292 */
3293 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3294 (LIST_FIRST(&kz->uk_zones) != z))
3295 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3296
3297 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3298 uth.uth_zone_free += bucket->ub_cnt;
3299 uth.uth_allocs = z->uz_allocs;
3300 uth.uth_frees = z->uz_frees;
3301 uth.uth_fails = z->uz_fails;
3302 if (sbuf_bcat(&sbuf, &uth, sizeof(uth)) < 0) {
3303 ZONE_UNLOCK(z);
3304 mtx_unlock(&uma_mtx);
3305 error = ENOMEM;
3306 goto out;
3307 }
3308 /*
3309 * While it is not normally safe to access the cache
3310 * bucket pointers while not on the CPU that owns the
3311 * cache, we only allow the pointers to be exchanged
3312 * without the zone lock held, not invalidated, so
3313 * accept the possible race associated with bucket
3314 * exchange during monitoring.
3315 */
3316 for (i = 0; i < (mp_maxid + 1); i++) {
3317 bzero(&ups, sizeof(ups));
3318 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3319 goto skip;
3320 if (CPU_ABSENT(i))
3321 goto skip;
3322 cache = &z->uz_cpu[i];
3323 if (cache->uc_allocbucket != NULL)
3324 ups.ups_cache_free +=
3325 cache->uc_allocbucket->ub_cnt;
3326 if (cache->uc_freebucket != NULL)
3327 ups.ups_cache_free +=
3328 cache->uc_freebucket->ub_cnt;
3329 ups.ups_allocs = cache->uc_allocs;
3330 ups.ups_frees = cache->uc_frees;
3331 skip:
3332 if (sbuf_bcat(&sbuf, &ups, sizeof(ups)) < 0) {
3333 ZONE_UNLOCK(z);
3334 mtx_unlock(&uma_mtx);
3335 error = ENOMEM;
3336 goto out;
3337 }
3338 }
3339 ZONE_UNLOCK(z);
3340 }
3341 }
3342 mtx_unlock(&uma_mtx);
3343 sbuf_finish(&sbuf);
3344 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
3345 out:
3346 free(buffer, M_TEMP);
3347 return (error);
3348 }
3349
3350 #ifdef DDB
3351 DB_SHOW_COMMAND(uma, db_show_uma)
3352 {
3353 u_int64_t allocs, frees;
3354 uma_bucket_t bucket;
3355 uma_keg_t kz;
3356 uma_zone_t z;
3357 int cachefree;
3358
3359 db_printf("%18s %8s %8s %8s %12s\n", "Zone", "Size", "Used", "Free",
3360 "Requests");
3361 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3362 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3363 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3364 allocs = z->uz_allocs;
3365 frees = z->uz_frees;
3366 cachefree = 0;
3367 } else
3368 uma_zone_sumstat(z, &cachefree, &allocs,
3369 &frees);
3370 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3371 (LIST_FIRST(&kz->uk_zones) != z)))
3372 cachefree += kz->uk_free;
3373 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3374 cachefree += bucket->ub_cnt;
3375 db_printf("%18s %8ju %8jd %8d %12ju\n", z->uz_name,
3376 (uintmax_t)kz->uk_size,
3377 (intmax_t)(allocs - frees), cachefree,
3378 (uintmax_t)allocs);
3379 }
3380 }
3381 }
3382 #endif
Cache object: 4aad96602edadbff87ae8949d9674ea7
|