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