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