2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
63 * General kernel memory allocator. This allocator is designed
64 * to be used by the kernel to manage dynamic memory fast.
67 #include <zone_debug.h>
69 #include <mach/boolean.h>
70 #include <mach/machine/vm_types.h>
71 #include <mach/vm_param.h>
72 #include <kern/misc_protos.h>
73 #include <kern/zalloc.h>
74 #include <kern/kalloc.h>
75 #include <kern/lock.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_map.h>
79 #include <libkern/OSMalloc.h>
82 zone_t
kalloc_zone(vm_size_t
);
85 #define KALLOC_MAP_SIZE_MIN (16 * 1024 * 1024)
86 #define KALLOC_MAP_SIZE_MAX (128 * 1024 * 1024)
89 vm_size_t kalloc_max_prerounded
;
90 vm_size_t kalloc_kernmap_size
; /* size of kallocs that can come from kernel map */
92 unsigned int kalloc_large_inuse
;
93 vm_size_t kalloc_large_total
;
94 vm_size_t kalloc_large_max
;
95 vm_size_t kalloc_largest_allocated
= 0;
98 * All allocations of size less than kalloc_max are rounded to the
99 * next highest power of 2. This allocator is built on top of
100 * the zone allocator. A zone is created for each potential size
101 * that we are willing to get in small blocks.
103 * We assume that kalloc_max is not greater than 64K;
104 * thus 16 is a safe array size for k_zone and k_zone_name.
106 * Note that kalloc_max is somewhat confusingly named.
107 * It represents the first power of two for which no zone exists.
108 * kalloc_max_prerounded is the smallest allocation size, before
109 * rounding, for which no zone exists.
110 * Also if the allocation size is more than kalloc_kernmap_size
111 * then allocate from kernel map rather than kalloc_map.
114 int first_k_zone
= -1;
115 struct zone
*k_zone
[16];
116 static const char *k_zone_name
[16] = {
117 "kalloc.1", "kalloc.2",
118 "kalloc.4", "kalloc.8",
119 "kalloc.16", "kalloc.32",
120 "kalloc.64", "kalloc.128",
121 "kalloc.256", "kalloc.512",
122 "kalloc.1024", "kalloc.2048",
123 "kalloc.4096", "kalloc.8192",
124 "kalloc.16384", "kalloc.32768"
128 * Max number of elements per zone. zinit rounds things up correctly
129 * Doing things this way permits each zone to have a different maximum size
130 * based on need, rather than just guessing; it also
131 * means its patchable in case you're wrong!
133 unsigned long k_zone_max
[16] = {
144 1024, /* 1024 Byte */
145 1024, /* 2048 Byte */
146 1024, /* 4096 Byte */
147 4096, /* 8192 Byte */
152 /* forward declarations */
153 void * kalloc_canblock(
158 /* OSMalloc local data declarations */
160 queue_head_t OSMalloc_tag_list
;
162 decl_simple_lock_data(static,OSMalloc_tag_lock
)
164 /* OSMalloc forward declarations */
165 void OSMalloc_init(void);
166 void OSMalloc_Tagref(OSMallocTag tag
);
167 void OSMalloc_Tagrele(OSMallocTag tag
);
170 * Initialize the memory allocator. This should be called only
171 * once on a system wide basis (i.e. first processor to get here
172 * does the initialization).
174 * This initializes all of the zones.
181 kern_return_t retval
;
183 vm_size_t size
, kalloc_map_size
;
187 * Scale the kalloc_map_size to physical memory size: stay below
188 * 1/8th the total zone map size, or 128 MB.
190 kalloc_map_size
= sane_size
>> 5;
191 if (kalloc_map_size
> KALLOC_MAP_SIZE_MAX
)
192 kalloc_map_size
= KALLOC_MAP_SIZE_MAX
;
193 if (kalloc_map_size
< KALLOC_MAP_SIZE_MIN
)
194 kalloc_map_size
= KALLOC_MAP_SIZE_MIN
;
196 retval
= kmem_suballoc(kernel_map
, &min
, kalloc_map_size
,
197 FALSE
, VM_FLAGS_ANYWHERE
, &kalloc_map
);
199 if (retval
!= KERN_SUCCESS
)
200 panic("kalloc_init: kmem_suballoc failed");
203 * Ensure that zones up to size 8192 bytes exist.
204 * This is desirable because messages are allocated
205 * with kalloc, and messages up through size 8192 are common.
208 if (PAGE_SIZE
< 16*1024)
209 kalloc_max
= 16*1024;
211 kalloc_max
= PAGE_SIZE
;
212 kalloc_max_prerounded
= kalloc_max
/ 2 + 1;
213 /* size it to be more than 16 times kalloc_max (256k) for allocations from kernel map */
214 kalloc_kernmap_size
= (kalloc_max
* 16) + 1;
217 * Allocate a zone for each size we are going to handle.
218 * We specify non-paged memory.
220 for (i
= 0, size
= 1; size
< kalloc_max
; i
++, size
<<= 1) {
221 if (size
< KALLOC_MINSIZE
) {
225 if (size
== KALLOC_MINSIZE
) {
228 k_zone
[i
] = zinit(size
, k_zone_max
[i
] * size
, size
,
240 register vm_size_t allocsize
;
241 vm_map_t alloc_map
= VM_MAP_NULL
;
244 * If size is too large for a zone, then use kmem_alloc.
245 * (We use kmem_alloc instead of kmem_alloc_wired so that
246 * krealloc can use kmem_realloc.)
249 if (size
>= kalloc_max_prerounded
) {
252 /* kmem_alloc could block so we return if noblock */
257 if (size
>= kalloc_kernmap_size
) {
258 alloc_map
= kernel_map
;
260 if (size
> kalloc_largest_allocated
)
261 kalloc_largest_allocated
= size
;
263 alloc_map
= kalloc_map
;
265 if (kmem_alloc(alloc_map
, (vm_offset_t
*)&addr
, size
) != KERN_SUCCESS
)
269 kalloc_large_inuse
++;
270 kalloc_large_total
+= size
;
272 if (kalloc_large_total
> kalloc_large_max
)
273 kalloc_large_max
= kalloc_large_total
;
278 /* compute the size of the block that we will actually allocate */
280 allocsize
= KALLOC_MINSIZE
;
281 zindex
= first_k_zone
;
282 while (allocsize
< size
) {
287 /* allocate from the appropriate zone */
288 assert(allocsize
< kalloc_max
);
289 return(zalloc_canblock(k_zone
[zindex
], canblock
));
296 return( kalloc_canblock(size
, TRUE
) );
303 return( kalloc_canblock(size
, FALSE
) );
315 register vm_size_t allocsize
;
317 vm_map_t alloc_map
= VM_MAP_NULL
;
319 /* can only be used for increasing allocation size */
321 assert(new_size
> old_size
);
323 /* if old_size is zero, then we are simply allocating */
327 naddr
= kalloc(new_size
);
333 /* if old block was kmem_alloc'd, then use kmem_realloc if necessary */
335 if (old_size
>= kalloc_max_prerounded
) {
336 if (old_size
>= kalloc_kernmap_size
)
337 alloc_map
= kernel_map
;
339 alloc_map
= kalloc_map
;
341 old_size
= round_page(old_size
);
342 new_size
= round_page(new_size
);
343 if (new_size
> old_size
) {
345 if (KERN_SUCCESS
!= kmem_realloc(alloc_map
,
346 (vm_offset_t
)*addrp
, old_size
,
347 (vm_offset_t
*)&naddr
, new_size
))
348 panic("krealloc: kmem_realloc");
351 *addrp
= (void *) naddr
;
353 /* kmem_realloc() doesn't free old page range. */
354 kmem_free(alloc_map
, (vm_offset_t
)*addrp
, old_size
);
356 kalloc_large_total
+= (new_size
- old_size
);
358 if (kalloc_large_total
> kalloc_large_max
)
359 kalloc_large_max
= kalloc_large_total
;
365 /* compute the size of the block that we actually allocated */
367 allocsize
= KALLOC_MINSIZE
;
368 zindex
= first_k_zone
;
369 while (allocsize
< old_size
) {
374 /* if new size fits in old block, then return */
376 if (new_size
<= allocsize
) {
380 /* if new size does not fit in zone, kmem_alloc it, else zalloc it */
383 if (new_size
>= kalloc_max_prerounded
) {
384 if (new_size
>= kalloc_kernmap_size
)
385 alloc_map
= kernel_map
;
387 alloc_map
= kalloc_map
;
388 if (KERN_SUCCESS
!= kmem_alloc(alloc_map
,
389 (vm_offset_t
*)&naddr
, new_size
)) {
390 panic("krealloc: kmem_alloc");
395 kalloc_large_inuse
++;
396 kalloc_large_total
+= new_size
;
398 if (kalloc_large_total
> kalloc_large_max
)
399 kalloc_large_max
= kalloc_large_total
;
401 register int new_zindex
;
404 new_zindex
= zindex
+ 1;
405 while (allocsize
< new_size
) {
409 naddr
= zalloc(k_zone
[new_zindex
]);
413 /* copy existing data */
415 bcopy((const char *)*addrp
, (char *)naddr
, old_size
);
417 /* free old block, and return */
419 zfree(k_zone
[zindex
], *addrp
);
421 /* set up new address */
423 *addrp
= (void *) naddr
;
432 register vm_size_t allocsize
;
434 /* size must not be too large for a zone */
436 if (size
>= kalloc_max_prerounded
) {
437 /* This will never work, so we might as well panic */
441 /* compute the size of the block that we will actually allocate */
443 allocsize
= KALLOC_MINSIZE
;
444 zindex
= first_k_zone
;
445 while (allocsize
< size
) {
450 /* allocate from the appropriate zone */
452 assert(allocsize
< kalloc_max
);
453 return(zget(k_zone
[zindex
]));
462 register vm_size_t freesize
;
463 vm_map_t alloc_map
= VM_MAP_NULL
;
465 /* if size was too large for a zone, then use kmem_free */
467 if (size
>= kalloc_max_prerounded
) {
468 if (size
>= kalloc_kernmap_size
) {
469 alloc_map
= kernel_map
;
471 if (size
> kalloc_largest_allocated
)
473 * work around double FREEs of small MALLOCs
474 * this use to end up being a nop
475 * since the pointer being freed from an
476 * alloc backed by the zalloc world could
477 * never show up in the kalloc_map... however,
478 * the kernel_map is a different issue... since it
479 * was released back into the zalloc pool, a pointer
480 * would have gotten written over the 'size' that
481 * the MALLOC was retaining in the first 4 bytes of
482 * the underlying allocation... that pointer ends up
483 * looking like a really big size on the 2nd FREE and
484 * pushes the kfree into the kernel_map... we
485 * end up removing a ton of virutal space before we panic
486 * this check causes us to ignore the kfree for a size
487 * that must be 'bogus'... note that it might not be due
488 * to the above scenario, but it would still be wrong and
489 * cause serious damage.
493 alloc_map
= kalloc_map
;
494 kmem_free(alloc_map
, (vm_offset_t
)data
, size
);
496 kalloc_large_total
-= size
;
497 kalloc_large_inuse
--;
502 /* compute the size of the block that we actually allocated from */
504 freesize
= KALLOC_MINSIZE
;
505 zindex
= first_k_zone
;
506 while (freesize
< size
) {
511 /* free to the appropriate zone */
513 assert(freesize
< kalloc_max
);
514 zfree(k_zone
[zindex
], data
);
522 register int zindex
= 0;
523 register vm_size_t allocsize
;
525 /* compute the size of the block that we will actually allocate */
528 if (size
<= kalloc_max
) {
529 allocsize
= KALLOC_MINSIZE
;
530 zindex
= first_k_zone
;
531 while (allocsize
< size
) {
535 return (k_zone
[zindex
]);
543 kalloc_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
544 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
546 *count
= kalloc_large_inuse
;
547 *cur_size
= kalloc_large_total
;
548 *max_size
= kalloc_large_max
;
549 *elem_size
= kalloc_large_total
/ kalloc_large_inuse
;
550 *alloc_size
= kalloc_large_total
/ kalloc_large_inuse
;
560 queue_init(&OSMalloc_tag_list
);
561 simple_lock_init(&OSMalloc_tag_lock
, 0);
571 OSMTag
= (OSMallocTag
)kalloc(sizeof(*OSMTag
));
573 bzero((void *)OSMTag
, sizeof(*OSMTag
));
575 if (flags
& OSMT_PAGEABLE
)
576 OSMTag
->OSMT_attr
= OSMT_ATTR_PAGEABLE
;
578 OSMTag
->OSMT_refcnt
= 1;
580 strncpy(OSMTag
->OSMT_name
, str
, OSMT_MAX_NAME
);
582 simple_lock(&OSMalloc_tag_lock
);
583 enqueue_tail(&OSMalloc_tag_list
, (queue_entry_t
)OSMTag
);
584 simple_unlock(&OSMalloc_tag_lock
);
585 OSMTag
->OSMT_state
= OSMT_VALID
;
593 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
594 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag
->OSMT_state
);
596 (void)hw_atomic_add(&tag
->OSMT_refcnt
, 1);
603 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
604 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag
->OSMT_state
);
606 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
607 if (hw_compare_and_store(OSMT_VALID
|OSMT_RELEASED
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
)) {
608 simple_lock(&OSMalloc_tag_lock
);
609 (void)remque((queue_entry_t
)tag
);
610 simple_unlock(&OSMalloc_tag_lock
);
611 kfree((void*)tag
, sizeof(*tag
));
613 panic("OSMalloc_Tagrele(): refcnt 0\n");
621 if (!hw_compare_and_store(OSMT_VALID
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
))
622 panic("OSMalloc_Tagfree(): bad state 0x%08X\n", tag
->OSMT_state
);
624 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
625 simple_lock(&OSMalloc_tag_lock
);
626 (void)remque((queue_entry_t
)tag
);
627 simple_unlock(&OSMalloc_tag_lock
);
628 kfree((void*)tag
, sizeof(*tag
));
640 OSMalloc_Tagref(tag
);
641 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
642 && (size
& ~PAGE_MASK
)) {
644 if ((kr
= kmem_alloc_pageable(kernel_map
, (vm_offset_t
*)&addr
, size
)) != KERN_SUCCESS
)
647 addr
= kalloc((vm_size_t
)size
);
650 OSMalloc_Tagrele(tag
);
662 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
665 OSMalloc_Tagref(tag
);
666 /* XXX: use non-blocking kalloc for now */
667 addr
= kalloc_noblock((vm_size_t
)size
);
669 OSMalloc_Tagrele(tag
);
681 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
684 OSMalloc_Tagref(tag
);
685 addr
= kalloc_noblock((vm_size_t
)size
);
687 OSMalloc_Tagrele(tag
);
698 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
699 && (size
& ~PAGE_MASK
)) {
700 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
702 kfree((void*)addr
, size
);
704 OSMalloc_Tagrele(tag
);