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 volatile vm_size_t kalloc_largest_allocated
= 0;
97 vm_offset_t kalloc_map_min
;
98 vm_offset_t kalloc_map_max
;
101 * All allocations of size less than kalloc_max are rounded to the
102 * next highest power of 2. This allocator is built on top of
103 * the zone allocator. A zone is created for each potential size
104 * that we are willing to get in small blocks.
106 * We assume that kalloc_max is not greater than 64K;
107 * thus 16 is a safe array size for k_zone and k_zone_name.
109 * Note that kalloc_max is somewhat confusingly named.
110 * It represents the first power of two for which no zone exists.
111 * kalloc_max_prerounded is the smallest allocation size, before
112 * rounding, for which no zone exists.
113 * Also if the allocation size is more than kalloc_kernmap_size
114 * then allocate from kernel map rather than kalloc_map.
117 int first_k_zone
= -1;
118 struct zone
*k_zone
[16];
119 static const char *k_zone_name
[16] = {
120 "kalloc.1", "kalloc.2",
121 "kalloc.4", "kalloc.8",
122 "kalloc.16", "kalloc.32",
123 "kalloc.64", "kalloc.128",
124 "kalloc.256", "kalloc.512",
125 "kalloc.1024", "kalloc.2048",
126 "kalloc.4096", "kalloc.8192",
127 "kalloc.16384", "kalloc.32768"
131 * Max number of elements per zone. zinit rounds things up correctly
132 * Doing things this way permits each zone to have a different maximum size
133 * based on need, rather than just guessing; it also
134 * means its patchable in case you're wrong!
136 unsigned long k_zone_max
[16] = {
147 1024, /* 1024 Byte */
148 1024, /* 2048 Byte */
149 1024, /* 4096 Byte */
150 4096, /* 8192 Byte */
155 /* forward declarations */
156 void * kalloc_canblock(
161 /* OSMalloc local data declarations */
163 queue_head_t OSMalloc_tag_list
;
165 decl_simple_lock_data(static,OSMalloc_tag_lock
)
167 /* OSMalloc forward declarations */
168 void OSMalloc_init(void);
169 void OSMalloc_Tagref(OSMallocTag tag
);
170 void OSMalloc_Tagrele(OSMallocTag tag
);
173 * Initialize the memory allocator. This should be called only
174 * once on a system wide basis (i.e. first processor to get here
175 * does the initialization).
177 * This initializes all of the zones.
184 kern_return_t retval
;
186 vm_size_t size
, kalloc_map_size
;
190 * Scale the kalloc_map_size to physical memory size: stay below
191 * 1/8th the total zone map size, or 128 MB (for a 32-bit kernel).
193 kalloc_map_size
= (vm_size_t
)(sane_size
>> 5);
195 if (kalloc_map_size
> KALLOC_MAP_SIZE_MAX
)
196 kalloc_map_size
= KALLOC_MAP_SIZE_MAX
;
197 #endif /* !__LP64__ */
198 if (kalloc_map_size
< KALLOC_MAP_SIZE_MIN
)
199 kalloc_map_size
= KALLOC_MAP_SIZE_MIN
;
201 retval
= kmem_suballoc(kernel_map
, &min
, kalloc_map_size
,
202 FALSE
, VM_FLAGS_ANYWHERE
| VM_FLAGS_PERMANENT
,
205 if (retval
!= KERN_SUCCESS
)
206 panic("kalloc_init: kmem_suballoc failed");
208 kalloc_map_min
= min
;
209 kalloc_map_max
= min
+ kalloc_map_size
- 1;
212 * Ensure that zones up to size 8192 bytes exist.
213 * This is desirable because messages are allocated
214 * with kalloc, and messages up through size 8192 are common.
217 if (PAGE_SIZE
< 16*1024)
218 kalloc_max
= 16*1024;
220 kalloc_max
= PAGE_SIZE
;
221 kalloc_max_prerounded
= kalloc_max
/ 2 + 1;
222 /* size it to be more than 16 times kalloc_max (256k) for allocations from kernel map */
223 kalloc_kernmap_size
= (kalloc_max
* 16) + 1;
224 kalloc_largest_allocated
= kalloc_kernmap_size
;
227 * Allocate a zone for each size we are going to handle.
228 * We specify non-paged memory.
230 for (i
= 0, size
= 1; size
< kalloc_max
; i
++, size
<<= 1) {
231 if (size
< KALLOC_MINSIZE
) {
235 if (size
== KALLOC_MINSIZE
) {
238 k_zone
[i
] = zinit(size
, k_zone_max
[i
] * size
, size
,
250 register vm_size_t allocsize
;
251 vm_map_t alloc_map
= VM_MAP_NULL
;
254 * If size is too large for a zone, then use kmem_alloc.
255 * (We use kmem_alloc instead of kmem_alloc_kobject so that
256 * krealloc can use kmem_realloc.)
259 if (size
>= kalloc_max_prerounded
) {
262 /* kmem_alloc could block so we return if noblock */
267 if (size
>= kalloc_kernmap_size
) {
268 volatile vm_offset_t prev_largest
;
269 alloc_map
= kernel_map
;
270 /* Thread-safe version of the workaround for 4740071
274 prev_largest
= kalloc_largest_allocated
;
275 } while ((size
> prev_largest
) && !OSCompareAndSwap((UInt32
)prev_largest
, (UInt32
)size
, (volatile UInt32
*) &kalloc_largest_allocated
));
277 alloc_map
= kalloc_map
;
279 if (kmem_alloc(alloc_map
, (vm_offset_t
*)&addr
, size
) != KERN_SUCCESS
) {
280 if (alloc_map
!= kernel_map
) {
281 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&addr
, size
) != KERN_SUCCESS
)
289 kalloc_large_inuse
++;
290 kalloc_large_total
+= size
;
292 if (kalloc_large_total
> kalloc_large_max
)
293 kalloc_large_max
= kalloc_large_total
;
298 /* compute the size of the block that we will actually allocate */
300 allocsize
= KALLOC_MINSIZE
;
301 zindex
= first_k_zone
;
302 while (allocsize
< size
) {
307 /* allocate from the appropriate zone */
308 assert(allocsize
< kalloc_max
);
309 return(zalloc_canblock(k_zone
[zindex
], canblock
));
316 return( kalloc_canblock(size
, TRUE
) );
323 return( kalloc_canblock(size
, FALSE
) );
335 register vm_size_t allocsize
;
337 vm_map_t alloc_map
= VM_MAP_NULL
;
339 /* can only be used for increasing allocation size */
341 assert(new_size
> old_size
);
343 /* if old_size is zero, then we are simply allocating */
347 naddr
= kalloc(new_size
);
353 /* if old block was kmem_alloc'd, then use kmem_realloc if necessary */
355 if (old_size
>= kalloc_max_prerounded
) {
356 if (old_size
>= kalloc_kernmap_size
)
357 alloc_map
= kernel_map
;
359 alloc_map
= kalloc_map
;
361 old_size
= round_page(old_size
);
362 new_size
= round_page(new_size
);
363 if (new_size
> old_size
) {
365 if (KERN_SUCCESS
!= kmem_realloc(alloc_map
,
366 (vm_offset_t
)*addrp
, old_size
,
367 (vm_offset_t
*)&naddr
, new_size
))
368 panic("krealloc: kmem_realloc");
371 *addrp
= (void *) naddr
;
373 /* kmem_realloc() doesn't free old page range. */
374 kmem_free(alloc_map
, (vm_offset_t
)*addrp
, old_size
);
376 kalloc_large_total
+= (new_size
- old_size
);
378 if (kalloc_large_total
> kalloc_large_max
)
379 kalloc_large_max
= kalloc_large_total
;
385 /* compute the size of the block that we actually allocated */
387 allocsize
= KALLOC_MINSIZE
;
388 zindex
= first_k_zone
;
389 while (allocsize
< old_size
) {
394 /* if new size fits in old block, then return */
396 if (new_size
<= allocsize
) {
400 /* if new size does not fit in zone, kmem_alloc it, else zalloc it */
403 if (new_size
>= kalloc_max_prerounded
) {
404 if (new_size
>= kalloc_kernmap_size
)
405 alloc_map
= kernel_map
;
407 alloc_map
= kalloc_map
;
408 if (KERN_SUCCESS
!= kmem_alloc(alloc_map
,
409 (vm_offset_t
*)&naddr
, new_size
)) {
410 panic("krealloc: kmem_alloc");
415 kalloc_large_inuse
++;
416 kalloc_large_total
+= new_size
;
418 if (kalloc_large_total
> kalloc_large_max
)
419 kalloc_large_max
= kalloc_large_total
;
421 register int new_zindex
;
424 new_zindex
= zindex
+ 1;
425 while (allocsize
< new_size
) {
429 naddr
= zalloc(k_zone
[new_zindex
]);
433 /* copy existing data */
435 bcopy((const char *)*addrp
, (char *)naddr
, old_size
);
437 /* free old block, and return */
439 zfree(k_zone
[zindex
], *addrp
);
441 /* set up new address */
443 *addrp
= (void *) naddr
;
452 register vm_size_t allocsize
;
454 /* size must not be too large for a zone */
456 if (size
>= kalloc_max_prerounded
) {
457 /* This will never work, so we might as well panic */
461 /* compute the size of the block that we will actually allocate */
463 allocsize
= KALLOC_MINSIZE
;
464 zindex
= first_k_zone
;
465 while (allocsize
< size
) {
470 /* allocate from the appropriate zone */
472 assert(allocsize
< kalloc_max
);
473 return(zget(k_zone
[zindex
]));
476 volatile SInt32 kfree_nop_count
= 0;
484 register vm_size_t freesize
;
485 vm_map_t alloc_map
= kernel_map
;
487 /* if size was too large for a zone, then use kmem_free */
489 if (size
>= kalloc_max_prerounded
) {
490 if ((((vm_offset_t
) data
) >= kalloc_map_min
) && (((vm_offset_t
) data
) <= kalloc_map_max
))
491 alloc_map
= kalloc_map
;
492 if (size
> kalloc_largest_allocated
) {
494 * work around double FREEs of small MALLOCs
495 * this use to end up being a nop
496 * since the pointer being freed from an
497 * alloc backed by the zalloc world could
498 * never show up in the kalloc_map... however,
499 * the kernel_map is a different issue... since it
500 * was released back into the zalloc pool, a pointer
501 * would have gotten written over the 'size' that
502 * the MALLOC was retaining in the first 4 bytes of
503 * the underlying allocation... that pointer ends up
504 * looking like a really big size on the 2nd FREE and
505 * pushes the kfree into the kernel_map... we
506 * end up removing a ton of virutal space before we panic
507 * this check causes us to ignore the kfree for a size
508 * that must be 'bogus'... note that it might not be due
509 * to the above scenario, but it would still be wrong and
510 * cause serious damage.
513 OSAddAtomic(1, &kfree_nop_count
);
516 kmem_free(alloc_map
, (vm_offset_t
)data
, size
);
518 kalloc_large_total
-= size
;
519 kalloc_large_inuse
--;
524 /* compute the size of the block that we actually allocated from */
526 freesize
= KALLOC_MINSIZE
;
527 zindex
= first_k_zone
;
528 while (freesize
< size
) {
533 /* free to the appropriate zone */
535 assert(freesize
< kalloc_max
);
536 zfree(k_zone
[zindex
], data
);
544 register int zindex
= 0;
545 register vm_size_t allocsize
;
547 /* compute the size of the block that we will actually allocate */
550 if (size
<= kalloc_max
) {
551 allocsize
= KALLOC_MINSIZE
;
552 zindex
= first_k_zone
;
553 while (allocsize
< size
) {
557 return (k_zone
[zindex
]);
565 kalloc_fake_zone_info(int *count
, vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
,
566 vm_size_t
*alloc_size
, int *collectable
, int *exhaustable
)
568 *count
= kalloc_large_inuse
;
569 *cur_size
= kalloc_large_total
;
570 *max_size
= kalloc_large_max
;
571 *elem_size
= kalloc_large_total
/ kalloc_large_inuse
;
572 *alloc_size
= kalloc_large_total
/ kalloc_large_inuse
;
582 queue_init(&OSMalloc_tag_list
);
583 simple_lock_init(&OSMalloc_tag_lock
, 0);
593 OSMTag
= (OSMallocTag
)kalloc(sizeof(*OSMTag
));
595 bzero((void *)OSMTag
, sizeof(*OSMTag
));
597 if (flags
& OSMT_PAGEABLE
)
598 OSMTag
->OSMT_attr
= OSMT_ATTR_PAGEABLE
;
600 OSMTag
->OSMT_refcnt
= 1;
602 strncpy(OSMTag
->OSMT_name
, str
, OSMT_MAX_NAME
);
604 simple_lock(&OSMalloc_tag_lock
);
605 enqueue_tail(&OSMalloc_tag_list
, (queue_entry_t
)OSMTag
);
606 simple_unlock(&OSMalloc_tag_lock
);
607 OSMTag
->OSMT_state
= OSMT_VALID
;
615 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
616 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag
->OSMT_state
);
618 (void)hw_atomic_add(&tag
->OSMT_refcnt
, 1);
625 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
626 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag
->OSMT_state
);
628 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
629 if (hw_compare_and_store(OSMT_VALID
|OSMT_RELEASED
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
)) {
630 simple_lock(&OSMalloc_tag_lock
);
631 (void)remque((queue_entry_t
)tag
);
632 simple_unlock(&OSMalloc_tag_lock
);
633 kfree((void*)tag
, sizeof(*tag
));
635 panic("OSMalloc_Tagrele(): refcnt 0\n");
643 if (!hw_compare_and_store(OSMT_VALID
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
))
644 panic("OSMalloc_Tagfree(): bad state 0x%08X\n", tag
->OSMT_state
);
646 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
647 simple_lock(&OSMalloc_tag_lock
);
648 (void)remque((queue_entry_t
)tag
);
649 simple_unlock(&OSMalloc_tag_lock
);
650 kfree((void*)tag
, sizeof(*tag
));
662 OSMalloc_Tagref(tag
);
663 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
664 && (size
& ~PAGE_MASK
)) {
666 if ((kr
= kmem_alloc_pageable(kernel_map
, (vm_offset_t
*)&addr
, size
)) != KERN_SUCCESS
)
669 addr
= kalloc((vm_size_t
)size
);
672 OSMalloc_Tagrele(tag
);
684 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
687 OSMalloc_Tagref(tag
);
688 /* XXX: use non-blocking kalloc for now */
689 addr
= kalloc_noblock((vm_size_t
)size
);
691 OSMalloc_Tagrele(tag
);
703 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
706 OSMalloc_Tagref(tag
);
707 addr
= kalloc_noblock((vm_size_t
)size
);
709 OSMalloc_Tagrele(tag
);
720 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
721 && (size
& ~PAGE_MASK
)) {
722 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
724 kfree((void*)addr
, size
);
726 OSMalloc_Tagrele(tag
);