2 * Copyright (c) 2000-2011 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/ledger.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 #include <sys/kdebug.h>
82 #include <san/kasan.h>
85 zone_t
kalloc_zone(vm_size_t
);
88 #define KALLOC_MAP_SIZE_MIN (16 * 1024 * 1024)
89 #define KALLOC_MAP_SIZE_MAX (128 * 1024 * 1024)
92 vm_size_t kalloc_max_prerounded
;
93 vm_size_t kalloc_kernmap_size
; /* size of kallocs that can come from kernel map */
95 /* how many times we couldn't allocate out of kalloc_map and fell back to kernel_map */
96 unsigned long kalloc_fallback_count
;
98 unsigned int kalloc_large_inuse
;
99 vm_size_t kalloc_large_total
;
100 vm_size_t kalloc_large_max
;
101 vm_size_t kalloc_largest_allocated
= 0;
102 uint64_t kalloc_large_sum
;
104 int kalloc_fake_zone_index
= -1; /* index of our fake zone in statistics arrays */
106 vm_offset_t kalloc_map_min
;
107 vm_offset_t kalloc_map_max
;
111 * Diagnostic code to track mutexes separately rather than via the 2^ zones
117 KALLOC_ZINFO_SALLOC(vm_size_t bytes
)
119 thread_t thr
= current_thread();
120 ledger_debit(thr
->t_ledger
, task_ledgers
.tkm_shared
, bytes
);
124 KALLOC_ZINFO_SFREE(vm_size_t bytes
)
126 thread_t thr
= current_thread();
127 ledger_credit(thr
->t_ledger
, task_ledgers
.tkm_shared
, bytes
);
131 * All allocations of size less than kalloc_max are rounded to the
132 * next nearest sized zone. This allocator is built on top of
133 * the zone allocator. A zone is created for each potential size
134 * that we are willing to get in small blocks.
136 * We assume that kalloc_max is not greater than 64K;
138 * Note that kalloc_max is somewhat confusingly named.
139 * It represents the first power of two for which no zone exists.
140 * kalloc_max_prerounded is the smallest allocation size, before
141 * rounding, for which no zone exists.
143 * Also if the allocation size is more than kalloc_kernmap_size
144 * then allocate from kernel map rather than kalloc_map.
147 #if KALLOC_MINSIZE == 16 && KALLOC_LOG2_MINALIGN == 4
149 #define K_ZONE_SIZES \
175 #define K_ZONE_NAMES \
179 "kalloc.64", /* 2^6 */ \
182 "kalloc.128", /* 2^7 */ \
186 "kalloc.256", /* 2^8 */\
190 "kalloc.512", /* 2^9 */ \
193 "kalloc.1024", /* 2^10 */ \
197 "kalloc.2048", /* 2^11 */ \
198 "kalloc.4096", /* 2^12 */ \
201 #elif KALLOC_MINSIZE == 8 && KALLOC_LOG2_MINALIGN == 3
204 * Tweaked for ARM (and x64) in 04/2011
207 #define K_ZONE_SIZES \
211 /* 6 */ 64, 72, 88, 112, \
213 256, 288, 384, 440, \
214 /* 9 */ 512, 576, 768, \
219 #define K_ZONE_NAMES \
220 /* 3 */ "kalloc.8", \
221 "kalloc.16", "kalloc.24", \
222 "kalloc.32", "kalloc.40", "kalloc.48", \
223 /* 6 */ "kalloc.64", "kalloc.72", "kalloc.88", "kalloc.112", \
224 "kalloc.128", "kalloc.192", \
225 "kalloc.256", "kalloc.288", "kalloc.384", "kalloc.440", \
226 /* 9 */ "kalloc.512", "kalloc.576", "kalloc.768", \
227 "kalloc.1024", "kalloc.1152", "kalloc.1536", \
228 "kalloc.2048", "kalloc.2128", "kalloc.3072", \
229 "kalloc.4096", "kalloc.6144"
232 #error missing or invalid zone size parameters for kalloc
235 #define KALLOC_MINALIGN (1 << KALLOC_LOG2_MINALIGN)
236 #define KiB(x) (1024 * (x))
238 static const int k_zone_size
[] = {
245 #define MAX_K_ZONE (sizeof (k_zone_size) / sizeof (k_zone_size[0]))
247 static const char *k_zone_name
[MAX_K_ZONE
] = {
256 * Many kalloc() allocations are for small structures containing a few
257 * pointers and longs - the k_zone_dlut[] direct lookup table, indexed by
258 * size normalized to the minimum alignment, finds the right zone index
259 * for them in one dereference.
262 #define INDEX_ZDLUT(size) \
263 (((size) + KALLOC_MINALIGN - 1) / KALLOC_MINALIGN)
264 #define N_K_ZDLUT (2048 / KALLOC_MINALIGN)
265 /* covers sizes [0 .. 2048 - KALLOC_MINALIGN] */
266 #define MAX_SIZE_ZDLUT ((N_K_ZDLUT - 1) * KALLOC_MINALIGN)
268 static int8_t k_zone_dlut
[N_K_ZDLUT
]; /* table of indices into k_zone[] */
271 * If there's no hit in the DLUT, then start searching from k_zindex_start.
273 static int k_zindex_start
;
275 static zone_t k_zone
[MAX_K_ZONE
];
277 /* #define KALLOC_DEBUG 1 */
279 /* forward declarations */
281 lck_grp_t kalloc_lck_grp
;
282 lck_mtx_t kalloc_lock
;
284 #define kalloc_spin_lock() lck_mtx_lock_spin(&kalloc_lock)
285 #define kalloc_unlock() lck_mtx_unlock(&kalloc_lock)
288 /* OSMalloc local data declarations */
290 queue_head_t OSMalloc_tag_list
;
292 lck_grp_t
*OSMalloc_tag_lck_grp
;
293 lck_mtx_t OSMalloc_tag_lock
;
295 #define OSMalloc_tag_spin_lock() lck_mtx_lock_spin(&OSMalloc_tag_lock)
296 #define OSMalloc_tag_unlock() lck_mtx_unlock(&OSMalloc_tag_lock)
299 /* OSMalloc forward declarations */
300 void OSMalloc_init(void);
301 void OSMalloc_Tagref(OSMallocTag tag
);
302 void OSMalloc_Tagrele(OSMallocTag tag
);
305 * Initialize the memory allocator. This should be called only
306 * once on a system wide basis (i.e. first processor to get here
307 * does the initialization).
309 * This initializes all of the zones.
316 kern_return_t retval
;
318 vm_size_t size
, kalloc_map_size
;
320 vm_map_kernel_flags_t vmk_flags
;
323 * Scale the kalloc_map_size to physical memory size: stay below
324 * 1/8th the total zone map size, or 128 MB (for a 32-bit kernel).
326 kalloc_map_size
= (vm_size_t
)(sane_size
>> 5);
328 if (kalloc_map_size
> KALLOC_MAP_SIZE_MAX
)
329 kalloc_map_size
= KALLOC_MAP_SIZE_MAX
;
330 #endif /* !__LP64__ */
331 if (kalloc_map_size
< KALLOC_MAP_SIZE_MIN
)
332 kalloc_map_size
= KALLOC_MAP_SIZE_MIN
;
334 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
335 vmk_flags
.vmkf_permanent
= TRUE
;
337 retval
= kmem_suballoc(kernel_map
, &min
, kalloc_map_size
,
341 VM_KERN_MEMORY_KALLOC
,
344 if (retval
!= KERN_SUCCESS
)
345 panic("kalloc_init: kmem_suballoc failed");
347 kalloc_map_min
= min
;
348 kalloc_map_max
= min
+ kalloc_map_size
- 1;
351 * Create zones up to a least 2 pages because small page-multiples are common
352 * allocations. Also ensure that zones up to size 8192 bytes exist. This is
353 * desirable because messages are allocated with kalloc(), and messages up
354 * through size 8192 are common.
356 kalloc_max
= PAGE_SIZE
<< 2;
357 if (kalloc_max
< KiB(16)) {
358 kalloc_max
= KiB(16);
360 assert(kalloc_max
<= KiB(64)); /* assumption made in size arrays */
362 kalloc_max_prerounded
= kalloc_max
/ 2 + 1;
363 /* allocations larger than 16 times kalloc_max go directly to kernel map */
364 kalloc_kernmap_size
= (kalloc_max
* 16) + 1;
365 kalloc_largest_allocated
= kalloc_kernmap_size
;
368 * Allocate a zone for each size we are going to handle. Don't charge the
369 * caller for the allocation, as we aren't sure how the memory will be
372 for (i
= 0; i
< (int)MAX_K_ZONE
&& (size
= k_zone_size
[i
]) < kalloc_max
; i
++) {
373 k_zone
[i
] = zinit(size
, size
, size
, k_zone_name
[i
]);
374 zone_change(k_zone
[i
], Z_CALLERACCT
, FALSE
);
376 if (zone_tagging_on
) zone_change(k_zone
[i
], Z_TAGS_ENABLED
, TRUE
);
378 zone_change(k_zone
[i
], Z_KASAN_QUARANTINE
, FALSE
);
382 * Build the Direct LookUp Table for small allocations
384 for (i
= 0, size
= 0; i
<= N_K_ZDLUT
; i
++, size
+= KALLOC_MINALIGN
) {
387 while ((vm_size_t
)k_zone_size
[zindex
] < size
)
390 if (i
== N_K_ZDLUT
) {
391 k_zindex_start
= zindex
;
394 k_zone_dlut
[i
] = (int8_t)zindex
;
398 printf("kalloc_init: k_zindex_start %d\n", k_zindex_start
);
401 * Do a quick synthesis to see how well/badly we can
402 * find-a-zone for a given size.
403 * Useful when debugging/tweaking the array of zone sizes.
404 * Cache misses probably more critical than compare-branches!
406 for (i
= 0; i
< (int)MAX_K_ZONE
; i
++) {
407 vm_size_t testsize
= (vm_size_t
)k_zone_size
[i
] - 1;
411 if (testsize
< MAX_SIZE_ZDLUT
) {
412 compare
+= 1; /* 'if' (T) */
414 long dindex
= INDEX_ZDLUT(testsize
);
415 zindex
= (int)k_zone_dlut
[dindex
];
417 } else if (testsize
< kalloc_max_prerounded
) {
419 compare
+= 2; /* 'if' (F), 'if' (T) */
421 zindex
= k_zindex_start
;
422 while ((vm_size_t
)k_zone_size
[zindex
] < testsize
) {
424 compare
++; /* 'while' (T) */
426 compare
++; /* 'while' (F) */
428 break; /* not zone-backed */
430 zone_t z
= k_zone
[zindex
];
431 printf("kalloc_init: req size %4lu: %11s took %d compare%s\n",
432 (unsigned long)testsize
, z
->zone_name
, compare
,
433 compare
== 1 ? "" : "s");
437 lck_grp_init(&kalloc_lck_grp
, "kalloc.large", LCK_GRP_ATTR_NULL
);
438 lck_mtx_init(&kalloc_lock
, &kalloc_lck_grp
, LCK_ATTR_NULL
);
441 lck_mtx_zone
= zinit(sizeof(struct _lck_mtx_
), 1024*256, 4096, "lck_mtx");
446 * Given an allocation size, return the kalloc zone it belongs to.
447 * Direct LookUp Table variant.
449 static __inline zone_t
450 get_zone_dlut(vm_size_t size
)
452 long dindex
= INDEX_ZDLUT(size
);
453 int zindex
= (int)k_zone_dlut
[dindex
];
454 return (k_zone
[zindex
]);
457 /* As above, but linear search k_zone_size[] for the next zone that fits. */
459 static __inline zone_t
460 get_zone_search(vm_size_t size
, int zindex
)
462 assert(size
< kalloc_max_prerounded
);
464 while ((vm_size_t
)k_zone_size
[zindex
] < size
)
467 assert((unsigned)zindex
< MAX_K_ZONE
&&
468 (vm_size_t
)k_zone_size
[zindex
] < kalloc_max
);
470 return (k_zone
[zindex
]);
474 vm_map_lookup_kalloc_entry_locked(
479 vm_map_entry_t vm_entry
= NULL
;
481 ret
= vm_map_lookup_entry(map
, (vm_map_offset_t
)addr
, &vm_entry
);
483 panic("Attempting to lookup/free an address not allocated via kalloc! (vm_map_lookup_entry() failed map: %p, addr: %p)\n",
486 if (vm_entry
->vme_start
!= (vm_map_offset_t
)addr
) {
487 panic("Attempting to lookup/free the middle of a kalloc'ed element! (map: %p, addr: %p, entry: %p)\n",
488 map
, addr
, vm_entry
);
490 if (!vm_entry
->vme_atomic
) {
491 panic("Attempting to lookup/free an address not managed by kalloc! (map: %p, addr: %p, entry: %p)\n",
492 map
, addr
, vm_entry
);
494 return (vm_entry
->vme_end
- vm_entry
->vme_start
);
499 * KASAN kalloc stashes the original user-requested size away in the poisoned
500 * area. Return that directly.
503 kalloc_size(void *addr
)
505 (void)vm_map_lookup_kalloc_entry_locked
; /* silence warning */
506 return kasan_user_size((vm_offset_t
)addr
);
516 size
= zone_element_size(addr
, NULL
);
520 if (((vm_offset_t
)addr
>= kalloc_map_min
) && ((vm_offset_t
)addr
< kalloc_map_max
)) {
525 vm_map_lock_read(map
);
526 size
= vm_map_lookup_kalloc_entry_locked(map
, addr
);
527 vm_map_unlock_read(map
);
539 if (size
< MAX_SIZE_ZDLUT
) {
540 z
= get_zone_dlut(size
);
544 if (size
< kalloc_max_prerounded
) {
545 z
= get_zone_search(size
, k_zindex_start
);
549 if (size
>= kalloc_kernmap_size
)
554 return vm_map_round_page(size
, VM_MAP_PAGE_MASK(map
));
559 kfree_addr(void *addr
)
561 vm_size_t origsz
= kalloc_size(addr
);
575 size
= zone_element_size(addr
, &z
);
581 if (((vm_offset_t
)addr
>= kalloc_map_min
) && ((vm_offset_t
)addr
< kalloc_map_max
)) {
586 if ((vm_offset_t
)addr
< VM_MIN_KERNEL_AND_KEXT_ADDRESS
) {
587 panic("kfree on an address not in the kernel & kext address range! addr: %p\n", addr
);
591 size
= vm_map_lookup_kalloc_entry_locked(map
, addr
);
592 ret
= vm_map_remove_locked(map
,
593 vm_map_trunc_page((vm_map_offset_t
)addr
,
594 VM_MAP_PAGE_MASK(map
)),
595 vm_map_round_page((vm_map_offset_t
)addr
+ size
,
596 VM_MAP_PAGE_MASK(map
)),
597 VM_MAP_REMOVE_KUNWIRE
);
598 if (ret
!= KERN_SUCCESS
) {
599 panic("vm_map_remove_locked() failed for kalloc vm_entry! addr: %p, map: %p ret: %d\n",
605 kalloc_large_total
-= size
;
606 kalloc_large_inuse
--;
609 KALLOC_ZINFO_SFREE(size
);
618 vm_allocation_site_t
* site
)
625 tag
= VM_KERN_MEMORY_KALLOC
;
629 /* expand the allocation to accomodate redzones */
630 vm_size_t req_size
= size
;
631 size
= kasan_alloc_resize(req_size
);
634 if (size
< MAX_SIZE_ZDLUT
)
635 z
= get_zone_dlut(size
);
636 else if (size
< kalloc_max_prerounded
)
637 z
= get_zone_search(size
, k_zindex_start
);
640 * If size is too large for a zone, then use kmem_alloc.
641 * (We use kmem_alloc instead of kmem_alloc_kobject so that
642 * krealloc can use kmem_realloc.)
646 /* kmem_alloc could block so we return if noblock */
652 /* large allocation - use guard pages instead of small redzones */
653 size
= round_page(req_size
+ 2 * PAGE_SIZE
);
654 assert(size
>= MAX_SIZE_ZDLUT
&& size
>= kalloc_max_prerounded
);
657 if (size
>= kalloc_kernmap_size
)
658 alloc_map
= kernel_map
;
660 alloc_map
= kalloc_map
;
662 if (site
) tag
= vm_tag_alloc(site
);
664 if (kmem_alloc_flags(alloc_map
, (vm_offset_t
*)&addr
, size
, tag
, KMA_ATOMIC
) != KERN_SUCCESS
) {
665 if (alloc_map
!= kernel_map
) {
666 if (kalloc_fallback_count
++ == 0) {
667 printf("%s: falling back to kernel_map\n", __func__
);
669 if (kmem_alloc_flags(kernel_map
, (vm_offset_t
*)&addr
, size
, tag
, KMA_ATOMIC
) != KERN_SUCCESS
)
679 * Thread-safe version of the workaround for 4740071
682 if (size
> kalloc_largest_allocated
)
683 kalloc_largest_allocated
= size
;
685 kalloc_large_inuse
++;
686 kalloc_large_total
+= size
;
687 kalloc_large_sum
+= size
;
689 if (kalloc_large_total
> kalloc_large_max
)
690 kalloc_large_max
= kalloc_large_total
;
694 KALLOC_ZINFO_SALLOC(size
);
697 /* fixup the return address to skip the redzone */
698 addr
= (void *)kasan_alloc((vm_offset_t
)addr
, size
, req_size
, PAGE_SIZE
);
700 *psize
= round_page(size
);
705 if (size
> z
->elem_size
)
706 panic("%s: z %p (%s) but requested size %lu", __func__
,
707 z
, z
->zone_name
, (unsigned long)size
);
710 assert(size
<= z
->elem_size
);
715 tag
= vm_tag_alloc(site
);
716 if (!canblock
&& !vm_allocation_zone_totals
[tag
]) tag
= VM_KERN_MEMORY_KALLOC
;
720 addr
= zalloc_canblock_tag(z
, canblock
, size
, tag
);
723 /* fixup the return address to skip the redzone */
724 addr
= (void *)kasan_alloc((vm_offset_t
)addr
, z
->elem_size
, req_size
, KASAN_GUARD_SIZE
);
726 /* For KASan, the redzone lives in any additional space, so don't
727 * expand the allocation. */
729 *psize
= z
->elem_size
;
742 return( kalloc_tag_bt(size
, VM_KERN_MEMORY_KALLOC
) );
745 volatile SInt32 kfree_nop_count
= 0;
756 * Resize back to the real allocation size and hand off to the KASan
757 * quarantine. `data` may then point to a different allocation.
759 vm_size_t user_size
= size
;
760 kasan_check_free((vm_address_t
)data
, size
, KASAN_HEAP_KALLOC
);
761 data
= (void *)kasan_dealloc((vm_address_t
)data
, &size
);
762 kasan_free(&data
, &size
, KASAN_HEAP_KALLOC
, NULL
, user_size
, true);
768 if (size
< MAX_SIZE_ZDLUT
)
769 z
= get_zone_dlut(size
);
770 else if (size
< kalloc_max_prerounded
)
771 z
= get_zone_search(size
, k_zindex_start
);
773 /* if size was too large for a zone, then use kmem_free */
775 vm_map_t alloc_map
= kernel_map
;
777 if ((((vm_offset_t
) data
) >= kalloc_map_min
) && (((vm_offset_t
) data
) <= kalloc_map_max
))
778 alloc_map
= kalloc_map
;
779 if (size
> kalloc_largest_allocated
) {
781 * work around double FREEs of small MALLOCs
782 * this used to end up being a nop
783 * since the pointer being freed from an
784 * alloc backed by the zalloc world could
785 * never show up in the kalloc_map... however,
786 * the kernel_map is a different issue... since it
787 * was released back into the zalloc pool, a pointer
788 * would have gotten written over the 'size' that
789 * the MALLOC was retaining in the first 4 bytes of
790 * the underlying allocation... that pointer ends up
791 * looking like a really big size on the 2nd FREE and
792 * pushes the kfree into the kernel_map... we
793 * end up removing a ton of virtual space before we panic
794 * this check causes us to ignore the kfree for a size
795 * that must be 'bogus'... note that it might not be due
796 * to the above scenario, but it would still be wrong and
797 * cause serious damage.
800 OSAddAtomic(1, &kfree_nop_count
);
803 kmem_free(alloc_map
, (vm_offset_t
)data
, size
);
806 kalloc_large_total
-= size
;
807 kalloc_large_inuse
--;
811 KALLOC_ZINFO_SFREE(size
);
815 /* free to the appropriate zone */
817 if (size
> z
->elem_size
)
818 panic("%s: z %p (%s) but requested size %lu", __func__
,
819 z
, z
->zone_name
, (unsigned long)size
);
821 assert(size
<= z
->elem_size
);
830 if (size
< MAX_SIZE_ZDLUT
)
831 return (get_zone_dlut(size
));
832 if (size
<= kalloc_max
)
833 return (get_zone_search(size
, k_zindex_start
));
842 queue_init(&OSMalloc_tag_list
);
844 OSMalloc_tag_lck_grp
= lck_grp_alloc_init("OSMalloc_tag", LCK_GRP_ATTR_NULL
);
845 lck_mtx_init(&OSMalloc_tag_lock
, OSMalloc_tag_lck_grp
, LCK_ATTR_NULL
);
855 OSMTag
= (OSMallocTag
)kalloc(sizeof(*OSMTag
));
857 bzero((void *)OSMTag
, sizeof(*OSMTag
));
859 if (flags
& OSMT_PAGEABLE
)
860 OSMTag
->OSMT_attr
= OSMT_ATTR_PAGEABLE
;
862 OSMTag
->OSMT_refcnt
= 1;
864 strlcpy(OSMTag
->OSMT_name
, str
, OSMT_MAX_NAME
);
866 OSMalloc_tag_spin_lock();
867 enqueue_tail(&OSMalloc_tag_list
, (queue_entry_t
)OSMTag
);
868 OSMalloc_tag_unlock();
869 OSMTag
->OSMT_state
= OSMT_VALID
;
877 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
878 panic("OSMalloc_Tagref():'%s' has bad state 0x%08X\n", tag
->OSMT_name
, tag
->OSMT_state
);
880 (void)hw_atomic_add(&tag
->OSMT_refcnt
, 1);
887 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
888 panic("OSMalloc_Tagref():'%s' has bad state 0x%08X\n", tag
->OSMT_name
, tag
->OSMT_state
);
890 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
891 if (hw_compare_and_store(OSMT_VALID
|OSMT_RELEASED
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
)) {
892 OSMalloc_tag_spin_lock();
893 (void)remque((queue_entry_t
)tag
);
894 OSMalloc_tag_unlock();
895 kfree((void*)tag
, sizeof(*tag
));
897 panic("OSMalloc_Tagrele():'%s' has refcnt 0\n", tag
->OSMT_name
);
905 if (!hw_compare_and_store(OSMT_VALID
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
))
906 panic("OSMalloc_Tagfree():'%s' has bad state 0x%08X \n", tag
->OSMT_name
, tag
->OSMT_state
);
908 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
909 OSMalloc_tag_spin_lock();
910 (void)remque((queue_entry_t
)tag
);
911 OSMalloc_tag_unlock();
912 kfree((void*)tag
, sizeof(*tag
));
924 OSMalloc_Tagref(tag
);
925 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
926 && (size
& ~PAGE_MASK
)) {
927 if ((kr
= kmem_alloc_pageable_external(kernel_map
, (vm_offset_t
*)&addr
, size
)) != KERN_SUCCESS
)
930 addr
= kalloc_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
933 OSMalloc_Tagrele(tag
);
945 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
948 OSMalloc_Tagref(tag
);
949 /* XXX: use non-blocking kalloc for now */
950 addr
= kalloc_noblock_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
952 OSMalloc_Tagrele(tag
);
964 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
967 OSMalloc_Tagref(tag
);
968 addr
= kalloc_noblock_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
970 OSMalloc_Tagrele(tag
);
981 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
982 && (size
& ~PAGE_MASK
)) {
983 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
985 kfree((void *)addr
, size
);
987 OSMalloc_Tagrele(tag
);
994 return (uint32_t)kalloc_size(addr
);