]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/kalloc.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / kern / kalloc.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: kern/kalloc.c
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1985
62 *
63 * General kernel memory allocator. This allocator is designed
64 * to be used by the kernel to manage dynamic memory fast.
65 */
66
67 #include <zone_debug.h>
68
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>
80
81 #ifdef MACH_BSD
82 zone_t kalloc_zone(vm_size_t);
83 #endif
84
85 #define KALLOC_MAP_SIZE_MIN (16 * 1024 * 1024)
86 #define KALLOC_MAP_SIZE_MAX (128 * 1024 * 1024)
87 vm_map_t kalloc_map;
88 vm_size_t kalloc_max;
89 vm_size_t kalloc_max_prerounded;
90 vm_size_t kalloc_kernmap_size; /* size of kallocs that can come from kernel map */
91
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;
96
97 vm_offset_t kalloc_map_min;
98 vm_offset_t kalloc_map_max;
99
100 /*
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.
105 *
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.
108 *
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.
115 */
116
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"
128 };
129
130 /*
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!
135 */
136 unsigned long k_zone_max[16] = {
137 1024, /* 1 Byte */
138 1024, /* 2 Byte */
139 1024, /* 4 Byte */
140 1024, /* 8 Byte */
141 1024, /* 16 Byte */
142 4096, /* 32 Byte */
143 4096, /* 64 Byte */
144 4096, /* 128 Byte */
145 4096, /* 256 Byte */
146 1024, /* 512 Byte */
147 1024, /* 1024 Byte */
148 1024, /* 2048 Byte */
149 1024, /* 4096 Byte */
150 4096, /* 8192 Byte */
151 64, /* 16384 Byte */
152 64, /* 32768 Byte */
153 };
154
155 /* forward declarations */
156 void * kalloc_canblock(
157 vm_size_t size,
158 boolean_t canblock);
159
160
161 /* OSMalloc local data declarations */
162 static
163 queue_head_t OSMalloc_tag_list;
164
165 decl_simple_lock_data(static,OSMalloc_tag_lock)
166
167 /* OSMalloc forward declarations */
168 void OSMalloc_init(void);
169 void OSMalloc_Tagref(OSMallocTag tag);
170 void OSMalloc_Tagrele(OSMallocTag tag);
171
172 /*
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).
176 *
177 * This initializes all of the zones.
178 */
179
180 void
181 kalloc_init(
182 void)
183 {
184 kern_return_t retval;
185 vm_offset_t min;
186 vm_size_t size, kalloc_map_size;
187 register int i;
188
189 /*
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).
192 */
193 kalloc_map_size = (vm_size_t)(sane_size >> 5);
194 #if !__LP64__
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;
200
201 retval = kmem_suballoc(kernel_map, &min, kalloc_map_size,
202 FALSE, VM_FLAGS_ANYWHERE | VM_FLAGS_PERMANENT,
203 &kalloc_map);
204
205 if (retval != KERN_SUCCESS)
206 panic("kalloc_init: kmem_suballoc failed");
207
208 kalloc_map_min = min;
209 kalloc_map_max = min + kalloc_map_size - 1;
210
211 /*
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.
215 */
216
217 if (PAGE_SIZE < 16*1024)
218 kalloc_max = 16*1024;
219 else
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;
225
226 /*
227 * Allocate a zone for each size we are going to handle.
228 * We specify non-paged memory.
229 */
230 for (i = 0, size = 1; size < kalloc_max; i++, size <<= 1) {
231 if (size < KALLOC_MINSIZE) {
232 k_zone[i] = NULL;
233 continue;
234 }
235 if (size == KALLOC_MINSIZE) {
236 first_k_zone = i;
237 }
238 k_zone[i] = zinit(size, k_zone_max[i] * size, size,
239 k_zone_name[i]);
240 }
241 OSMalloc_init();
242 }
243
244 void *
245 kalloc_canblock(
246 vm_size_t size,
247 boolean_t canblock)
248 {
249 register int zindex;
250 register vm_size_t allocsize;
251 vm_map_t alloc_map = VM_MAP_NULL;
252
253 /*
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.)
257 */
258
259 if (size >= kalloc_max_prerounded) {
260 void *addr;
261
262 /* kmem_alloc could block so we return if noblock */
263 if (!canblock) {
264 return(NULL);
265 }
266
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
271 * (a double FREE())
272 */
273 do {
274 prev_largest = kalloc_largest_allocated;
275 } while ((size > prev_largest) && !OSCompareAndSwap((UInt32)prev_largest, (UInt32)size, (volatile UInt32 *) &kalloc_largest_allocated));
276 } else
277 alloc_map = kalloc_map;
278
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)
282 addr = NULL;
283 }
284 else
285 addr = NULL;
286 }
287
288 if (addr != NULL) {
289 kalloc_large_inuse++;
290 kalloc_large_total += size;
291
292 if (kalloc_large_total > kalloc_large_max)
293 kalloc_large_max = kalloc_large_total;
294 }
295 return(addr);
296 }
297
298 /* compute the size of the block that we will actually allocate */
299
300 allocsize = KALLOC_MINSIZE;
301 zindex = first_k_zone;
302 while (allocsize < size) {
303 allocsize <<= 1;
304 zindex++;
305 }
306
307 /* allocate from the appropriate zone */
308 assert(allocsize < kalloc_max);
309 return(zalloc_canblock(k_zone[zindex], canblock));
310 }
311
312 void *
313 kalloc(
314 vm_size_t size)
315 {
316 return( kalloc_canblock(size, TRUE) );
317 }
318
319 void *
320 kalloc_noblock(
321 vm_size_t size)
322 {
323 return( kalloc_canblock(size, FALSE) );
324 }
325
326
327 void
328 krealloc(
329 void **addrp,
330 vm_size_t old_size,
331 vm_size_t new_size,
332 simple_lock_t lock)
333 {
334 register int zindex;
335 register vm_size_t allocsize;
336 void *naddr;
337 vm_map_t alloc_map = VM_MAP_NULL;
338
339 /* can only be used for increasing allocation size */
340
341 assert(new_size > old_size);
342
343 /* if old_size is zero, then we are simply allocating */
344
345 if (old_size == 0) {
346 simple_unlock(lock);
347 naddr = kalloc(new_size);
348 simple_lock(lock);
349 *addrp = naddr;
350 return;
351 }
352
353 /* if old block was kmem_alloc'd, then use kmem_realloc if necessary */
354
355 if (old_size >= kalloc_max_prerounded) {
356 if (old_size >= kalloc_kernmap_size)
357 alloc_map = kernel_map;
358 else
359 alloc_map = kalloc_map;
360
361 old_size = round_page(old_size);
362 new_size = round_page(new_size);
363 if (new_size > old_size) {
364
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");
369
370 simple_lock(lock);
371 *addrp = (void *) naddr;
372
373 /* kmem_realloc() doesn't free old page range. */
374 kmem_free(alloc_map, (vm_offset_t)*addrp, old_size);
375
376 kalloc_large_total += (new_size - old_size);
377
378 if (kalloc_large_total > kalloc_large_max)
379 kalloc_large_max = kalloc_large_total;
380
381 }
382 return;
383 }
384
385 /* compute the size of the block that we actually allocated */
386
387 allocsize = KALLOC_MINSIZE;
388 zindex = first_k_zone;
389 while (allocsize < old_size) {
390 allocsize <<= 1;
391 zindex++;
392 }
393
394 /* if new size fits in old block, then return */
395
396 if (new_size <= allocsize) {
397 return;
398 }
399
400 /* if new size does not fit in zone, kmem_alloc it, else zalloc it */
401
402 simple_unlock(lock);
403 if (new_size >= kalloc_max_prerounded) {
404 if (new_size >= kalloc_kernmap_size)
405 alloc_map = kernel_map;
406 else
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");
411 simple_lock(lock);
412 *addrp = NULL;
413 return;
414 }
415 kalloc_large_inuse++;
416 kalloc_large_total += new_size;
417
418 if (kalloc_large_total > kalloc_large_max)
419 kalloc_large_max = kalloc_large_total;
420 } else {
421 register int new_zindex;
422
423 allocsize <<= 1;
424 new_zindex = zindex + 1;
425 while (allocsize < new_size) {
426 allocsize <<= 1;
427 new_zindex++;
428 }
429 naddr = zalloc(k_zone[new_zindex]);
430 }
431 simple_lock(lock);
432
433 /* copy existing data */
434
435 bcopy((const char *)*addrp, (char *)naddr, old_size);
436
437 /* free old block, and return */
438
439 zfree(k_zone[zindex], *addrp);
440
441 /* set up new address */
442
443 *addrp = (void *) naddr;
444 }
445
446
447 void *
448 kget(
449 vm_size_t size)
450 {
451 register int zindex;
452 register vm_size_t allocsize;
453
454 /* size must not be too large for a zone */
455
456 if (size >= kalloc_max_prerounded) {
457 /* This will never work, so we might as well panic */
458 panic("kget");
459 }
460
461 /* compute the size of the block that we will actually allocate */
462
463 allocsize = KALLOC_MINSIZE;
464 zindex = first_k_zone;
465 while (allocsize < size) {
466 allocsize <<= 1;
467 zindex++;
468 }
469
470 /* allocate from the appropriate zone */
471
472 assert(allocsize < kalloc_max);
473 return(zget(k_zone[zindex]));
474 }
475
476 volatile SInt32 kfree_nop_count = 0;
477
478 void
479 kfree(
480 void *data,
481 vm_size_t size)
482 {
483 register int zindex;
484 register vm_size_t freesize;
485 vm_map_t alloc_map = kernel_map;
486
487 /* if size was too large for a zone, then use kmem_free */
488
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) {
493 /*
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.
511 */
512
513 OSAddAtomic(1, &kfree_nop_count);
514 return;
515 }
516 kmem_free(alloc_map, (vm_offset_t)data, size);
517
518 kalloc_large_total -= size;
519 kalloc_large_inuse--;
520
521 return;
522 }
523
524 /* compute the size of the block that we actually allocated from */
525
526 freesize = KALLOC_MINSIZE;
527 zindex = first_k_zone;
528 while (freesize < size) {
529 freesize <<= 1;
530 zindex++;
531 }
532
533 /* free to the appropriate zone */
534
535 assert(freesize < kalloc_max);
536 zfree(k_zone[zindex], data);
537 }
538
539 #ifdef MACH_BSD
540 zone_t
541 kalloc_zone(
542 vm_size_t size)
543 {
544 register int zindex = 0;
545 register vm_size_t allocsize;
546
547 /* compute the size of the block that we will actually allocate */
548
549 allocsize = size;
550 if (size <= kalloc_max) {
551 allocsize = KALLOC_MINSIZE;
552 zindex = first_k_zone;
553 while (allocsize < size) {
554 allocsize <<= 1;
555 zindex++;
556 }
557 return (k_zone[zindex]);
558 }
559 return (ZONE_NULL);
560 }
561 #endif
562
563
564 void
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)
567 {
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;
573 *collectable = 0;
574 *exhaustable = 0;
575 }
576
577
578 void
579 OSMalloc_init(
580 void)
581 {
582 queue_init(&OSMalloc_tag_list);
583 simple_lock_init(&OSMalloc_tag_lock, 0);
584 }
585
586 OSMallocTag
587 OSMalloc_Tagalloc(
588 const char *str,
589 uint32_t flags)
590 {
591 OSMallocTag OSMTag;
592
593 OSMTag = (OSMallocTag)kalloc(sizeof(*OSMTag));
594
595 bzero((void *)OSMTag, sizeof(*OSMTag));
596
597 if (flags & OSMT_PAGEABLE)
598 OSMTag->OSMT_attr = OSMT_ATTR_PAGEABLE;
599
600 OSMTag->OSMT_refcnt = 1;
601
602 strncpy(OSMTag->OSMT_name, str, OSMT_MAX_NAME);
603
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;
608 return(OSMTag);
609 }
610
611 void
612 OSMalloc_Tagref(
613 OSMallocTag tag)
614 {
615 if (!((tag->OSMT_state & OSMT_VALID_MASK) == OSMT_VALID))
616 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag->OSMT_state);
617
618 (void)hw_atomic_add(&tag->OSMT_refcnt, 1);
619 }
620
621 void
622 OSMalloc_Tagrele(
623 OSMallocTag tag)
624 {
625 if (!((tag->OSMT_state & OSMT_VALID_MASK) == OSMT_VALID))
626 panic("OSMalloc_Tagref(): bad state 0x%08X\n",tag->OSMT_state);
627
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));
634 } else
635 panic("OSMalloc_Tagrele(): refcnt 0\n");
636 }
637 }
638
639 void
640 OSMalloc_Tagfree(
641 OSMallocTag tag)
642 {
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);
645
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));
651 }
652 }
653
654 void *
655 OSMalloc(
656 uint32_t size,
657 OSMallocTag tag)
658 {
659 void *addr=NULL;
660 kern_return_t kr;
661
662 OSMalloc_Tagref(tag);
663 if ((tag->OSMT_attr & OSMT_PAGEABLE)
664 && (size & ~PAGE_MASK)) {
665
666 if ((kr = kmem_alloc_pageable(kernel_map, (vm_offset_t *)&addr, size)) != KERN_SUCCESS)
667 addr = NULL;
668 } else
669 addr = kalloc((vm_size_t)size);
670
671 if (!addr)
672 OSMalloc_Tagrele(tag);
673
674 return(addr);
675 }
676
677 void *
678 OSMalloc_nowait(
679 uint32_t size,
680 OSMallocTag tag)
681 {
682 void *addr=NULL;
683
684 if (tag->OSMT_attr & OSMT_PAGEABLE)
685 return(NULL);
686
687 OSMalloc_Tagref(tag);
688 /* XXX: use non-blocking kalloc for now */
689 addr = kalloc_noblock((vm_size_t)size);
690 if (addr == NULL)
691 OSMalloc_Tagrele(tag);
692
693 return(addr);
694 }
695
696 void *
697 OSMalloc_noblock(
698 uint32_t size,
699 OSMallocTag tag)
700 {
701 void *addr=NULL;
702
703 if (tag->OSMT_attr & OSMT_PAGEABLE)
704 return(NULL);
705
706 OSMalloc_Tagref(tag);
707 addr = kalloc_noblock((vm_size_t)size);
708 if (addr == NULL)
709 OSMalloc_Tagrele(tag);
710
711 return(addr);
712 }
713
714 void
715 OSFree(
716 void *addr,
717 uint32_t size,
718 OSMallocTag tag)
719 {
720 if ((tag->OSMT_attr & OSMT_PAGEABLE)
721 && (size & ~PAGE_MASK)) {
722 kmem_free(kernel_map, (vm_offset_t)addr, size);
723 } else
724 kfree((void*)addr, size);
725
726 OSMalloc_Tagrele(tag);
727 }