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>
71 #include <mach/machine/vm_types.h>
72 #include <mach/vm_param.h>
73 #include <kern/misc_protos.h>
74 #include <kern/zalloc.h>
75 #include <kern/kalloc.h>
76 #include <kern/ledger.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_map.h>
80 #include <libkern/OSMalloc.h>
81 #include <sys/kdebug.h>
83 #include <san/kasan.h>
86 zone_t
kalloc_zone(vm_size_t
);
89 #define KALLOC_MAP_SIZE_MIN (16 * 1024 * 1024)
90 #define KALLOC_MAP_SIZE_MAX (128 * 1024 * 1024)
93 vm_size_t kalloc_max_prerounded
;
94 vm_size_t kalloc_kernmap_size
; /* size of kallocs that can come from kernel map */
96 /* how many times we couldn't allocate out of kalloc_map and fell back to kernel_map */
97 unsigned long kalloc_fallback_count
;
99 unsigned int kalloc_large_inuse
;
100 vm_size_t kalloc_large_total
;
101 vm_size_t kalloc_large_max
;
102 vm_size_t kalloc_largest_allocated
= 0;
103 uint64_t kalloc_large_sum
;
105 int kalloc_fake_zone_index
= -1; /* index of our fake zone in statistics arrays */
107 vm_offset_t kalloc_map_min
;
108 vm_offset_t kalloc_map_max
;
112 * Diagnostic code to track mutexes separately rather than via the 2^ zones
118 KALLOC_ZINFO_SALLOC(vm_size_t bytes
)
120 thread_t thr
= current_thread();
121 ledger_debit(thr
->t_ledger
, task_ledgers
.tkm_shared
, bytes
);
125 KALLOC_ZINFO_SFREE(vm_size_t bytes
)
127 thread_t thr
= current_thread();
128 ledger_credit(thr
->t_ledger
, task_ledgers
.tkm_shared
, bytes
);
132 * All allocations of size less than kalloc_max are rounded to the next nearest
133 * sized zone. This allocator is built on top of the zone allocator. A zone
134 * is created for each potential size that we are willing to get in small
137 * We assume that kalloc_max is not greater than 64K;
139 * Note that kalloc_max is somewhat confusingly named. It represents the first
140 * power of two for which no zone exists. kalloc_max_prerounded is the
141 * smallest allocation size, before rounding, for which no zone exists.
143 * Also if the allocation size is more than kalloc_kernmap_size then allocate
144 * from kernel map rather than kalloc_map.
147 #define KALLOC_MINALIGN (1 << KALLOC_LOG2_MINALIGN)
148 #define KiB(x) (1024 * (x))
150 static const struct kalloc_zone_config
{
152 const char *kzc_name
;
153 } k_zone_config
[] = {
154 #define KZC_ENTRY(SIZE) { .kzc_size = (SIZE), .kzc_name = "kalloc." #SIZE }
156 #if KALLOC_MINSIZE == 16 && KALLOC_LOG2_MINALIGN == 4
157 /* 64-bit targets, generally */
180 #elif KALLOC_MINSIZE == 8 && KALLOC_LOG2_MINALIGN == 3
181 /* 32-bit targets, generally */
208 #error missing or invalid zone size parameters for kalloc
211 /* all configurations get these zones */
220 #define MAX_K_ZONE (int)(sizeof(k_zone_config) / sizeof(k_zone_config[0]))
223 * Many kalloc() allocations are for small structures containing a few
224 * pointers and longs - the k_zone_dlut[] direct lookup table, indexed by
225 * size normalized to the minimum alignment, finds the right zone index
226 * for them in one dereference.
229 #define INDEX_ZDLUT(size) \
230 (((size) + KALLOC_MINALIGN - 1) / KALLOC_MINALIGN)
231 #define N_K_ZDLUT (2048 / KALLOC_MINALIGN)
232 /* covers sizes [0 .. 2048 - KALLOC_MINALIGN] */
233 #define MAX_SIZE_ZDLUT ((N_K_ZDLUT - 1) * KALLOC_MINALIGN)
235 static int8_t k_zone_dlut
[N_K_ZDLUT
]; /* table of indices into k_zone[] */
238 * If there's no hit in the DLUT, then start searching from k_zindex_start.
240 static int k_zindex_start
;
242 static zone_t k_zone
[MAX_K_ZONE
];
244 /* #define KALLOC_DEBUG 1 */
246 /* forward declarations */
248 lck_grp_t kalloc_lck_grp
;
249 lck_mtx_t kalloc_lock
;
251 #define kalloc_spin_lock() lck_mtx_lock_spin(&kalloc_lock)
252 #define kalloc_unlock() lck_mtx_unlock(&kalloc_lock)
255 /* OSMalloc local data declarations */
257 queue_head_t OSMalloc_tag_list
;
259 lck_grp_t
*OSMalloc_tag_lck_grp
;
260 lck_mtx_t OSMalloc_tag_lock
;
262 #define OSMalloc_tag_spin_lock() lck_mtx_lock_spin(&OSMalloc_tag_lock)
263 #define OSMalloc_tag_unlock() lck_mtx_unlock(&OSMalloc_tag_lock)
266 /* OSMalloc forward declarations */
267 void OSMalloc_init(void);
268 void OSMalloc_Tagref(OSMallocTag tag
);
269 void OSMalloc_Tagrele(OSMallocTag tag
);
272 * Initialize the memory allocator. This should be called only
273 * once on a system wide basis (i.e. first processor to get here
274 * does the initialization).
276 * This initializes all of the zones.
283 kern_return_t retval
;
285 vm_size_t size
, kalloc_map_size
;
286 vm_map_kernel_flags_t vmk_flags
;
289 * Scale the kalloc_map_size to physical memory size: stay below
290 * 1/8th the total zone map size, or 128 MB (for a 32-bit kernel).
292 kalloc_map_size
= (vm_size_t
)(sane_size
>> 5);
294 if (kalloc_map_size
> KALLOC_MAP_SIZE_MAX
)
295 kalloc_map_size
= KALLOC_MAP_SIZE_MAX
;
296 #endif /* !__LP64__ */
297 if (kalloc_map_size
< KALLOC_MAP_SIZE_MIN
)
298 kalloc_map_size
= KALLOC_MAP_SIZE_MIN
;
300 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
301 vmk_flags
.vmkf_permanent
= TRUE
;
303 retval
= kmem_suballoc(kernel_map
, &min
, kalloc_map_size
,
307 VM_KERN_MEMORY_KALLOC
,
310 if (retval
!= KERN_SUCCESS
)
311 panic("kalloc_init: kmem_suballoc failed");
313 kalloc_map_min
= min
;
314 kalloc_map_max
= min
+ kalloc_map_size
- 1;
317 * Create zones up to a least 4 pages because small page-multiples are
318 * common allocations. Also ensure that zones up to size 16KB bytes exist.
319 * This is desirable because messages are allocated with kalloc(), and
320 * messages up through size 8192 are common.
322 kalloc_max
= PAGE_SIZE
<< 2;
323 if (kalloc_max
< KiB(16)) {
324 kalloc_max
= KiB(16);
326 assert(kalloc_max
<= KiB(64)); /* assumption made in size arrays */
328 kalloc_max_prerounded
= kalloc_max
/ 2 + 1;
329 /* allocations larger than 16 times kalloc_max go directly to kernel map */
330 kalloc_kernmap_size
= (kalloc_max
* 16) + 1;
331 kalloc_largest_allocated
= kalloc_kernmap_size
;
334 * Allocate a zone for each size we are going to handle.
336 for (int i
= 0; i
< MAX_K_ZONE
&& (size
= k_zone_config
[i
].kzc_size
) < kalloc_max
; i
++) {
337 k_zone
[i
] = zinit(size
, size
, size
, k_zone_config
[i
].kzc_name
);
340 * Don't charge the caller for the allocation, as we aren't sure how
341 * the memory will be handled.
343 zone_change(k_zone
[i
], Z_CALLERACCT
, FALSE
);
345 if (zone_tagging_on
) zone_change(k_zone
[i
], Z_TAGS_ENABLED
, TRUE
);
347 zone_change(k_zone
[i
], Z_KASAN_QUARANTINE
, FALSE
);
351 * Build the Direct LookUp Table for small allocations
354 for (int i
= 0; i
<= N_K_ZDLUT
; i
++, size
+= KALLOC_MINALIGN
) {
357 while ((vm_size_t
)k_zone_config
[zindex
].kzc_size
< size
)
360 if (i
== N_K_ZDLUT
) {
361 k_zindex_start
= zindex
;
364 k_zone_dlut
[i
] = (int8_t)zindex
;
368 printf("kalloc_init: k_zindex_start %d\n", k_zindex_start
);
371 * Do a quick synthesis to see how well/badly we can
372 * find-a-zone for a given size.
373 * Useful when debugging/tweaking the array of zone sizes.
374 * Cache misses probably more critical than compare-branches!
376 for (int i
= 0; i
< MAX_K_ZONE
; i
++) {
377 vm_size_t testsize
= (vm_size_t
)k_zone_config
[i
].kzc_size
- 1;
381 if (testsize
< MAX_SIZE_ZDLUT
) {
382 compare
+= 1; /* 'if' (T) */
384 long dindex
= INDEX_ZDLUT(testsize
);
385 zindex
= (int)k_zone_dlut
[dindex
];
387 } else if (testsize
< kalloc_max_prerounded
) {
389 compare
+= 2; /* 'if' (F), 'if' (T) */
391 zindex
= k_zindex_start
;
392 while ((vm_size_t
)k_zone_config
[zindex
].kzc_size
< testsize
) {
394 compare
++; /* 'while' (T) */
396 compare
++; /* 'while' (F) */
398 break; /* not zone-backed */
400 zone_t z
= k_zone
[zindex
];
401 printf("kalloc_init: req size %4lu: %11s took %d compare%s\n",
402 (unsigned long)testsize
, z
->zone_name
, compare
,
403 compare
== 1 ? "" : "s");
407 lck_grp_init(&kalloc_lck_grp
, "kalloc.large", LCK_GRP_ATTR_NULL
);
408 lck_mtx_init(&kalloc_lock
, &kalloc_lck_grp
, LCK_ATTR_NULL
);
411 lck_mtx_zone
= zinit(sizeof(struct _lck_mtx_
), 1024*256, 4096, "lck_mtx");
416 * Given an allocation size, return the kalloc zone it belongs to.
417 * Direct LookUp Table variant.
419 static __inline zone_t
420 get_zone_dlut(vm_size_t size
)
422 long dindex
= INDEX_ZDLUT(size
);
423 int zindex
= (int)k_zone_dlut
[dindex
];
424 return (k_zone
[zindex
]);
427 /* As above, but linear search k_zone_config[] for the next zone that fits. */
429 static __inline zone_t
430 get_zone_search(vm_size_t size
, int zindex
)
432 assert(size
< kalloc_max_prerounded
);
434 while ((vm_size_t
)k_zone_config
[zindex
].kzc_size
< size
)
437 assert(zindex
< MAX_K_ZONE
&&
438 (vm_size_t
)k_zone_config
[zindex
].kzc_size
< kalloc_max
);
440 return (k_zone
[zindex
]);
444 vm_map_lookup_kalloc_entry_locked(
449 vm_map_entry_t vm_entry
= NULL
;
451 ret
= vm_map_lookup_entry(map
, (vm_map_offset_t
)addr
, &vm_entry
);
453 panic("Attempting to lookup/free an address not allocated via kalloc! (vm_map_lookup_entry() failed map: %p, addr: %p)\n",
456 if (vm_entry
->vme_start
!= (vm_map_offset_t
)addr
) {
457 panic("Attempting to lookup/free the middle of a kalloc'ed element! (map: %p, addr: %p, entry: %p)\n",
458 map
, addr
, vm_entry
);
460 if (!vm_entry
->vme_atomic
) {
461 panic("Attempting to lookup/free an address not managed by kalloc! (map: %p, addr: %p, entry: %p)\n",
462 map
, addr
, vm_entry
);
464 return (vm_entry
->vme_end
- vm_entry
->vme_start
);
469 * KASAN kalloc stashes the original user-requested size away in the poisoned
470 * area. Return that directly.
473 kalloc_size(void *addr
)
475 (void)vm_map_lookup_kalloc_entry_locked
; /* silence warning */
476 return kasan_user_size((vm_offset_t
)addr
);
486 size
= zone_element_size(addr
, NULL
);
490 if (((vm_offset_t
)addr
>= kalloc_map_min
) && ((vm_offset_t
)addr
< kalloc_map_max
)) {
495 vm_map_lock_read(map
);
496 size
= vm_map_lookup_kalloc_entry_locked(map
, addr
);
497 vm_map_unlock_read(map
);
509 if (size
< MAX_SIZE_ZDLUT
) {
510 z
= get_zone_dlut(size
);
514 if (size
< kalloc_max_prerounded
) {
515 z
= get_zone_search(size
, k_zindex_start
);
519 if (size
>= kalloc_kernmap_size
)
524 return vm_map_round_page(size
, VM_MAP_PAGE_MASK(map
));
529 kfree_addr(void *addr
)
531 vm_size_t origsz
= kalloc_size(addr
);
545 size
= zone_element_size(addr
, &z
);
547 DTRACE_VM3(kfree
, vm_size_t
, -1, vm_size_t
, z
->elem_size
, void*, addr
);
552 if (((vm_offset_t
)addr
>= kalloc_map_min
) && ((vm_offset_t
)addr
< kalloc_map_max
)) {
557 if ((vm_offset_t
)addr
< VM_MIN_KERNEL_AND_KEXT_ADDRESS
) {
558 panic("kfree on an address not in the kernel & kext address range! addr: %p\n", addr
);
562 size
= vm_map_lookup_kalloc_entry_locked(map
, addr
);
563 ret
= vm_map_remove_locked(map
,
564 vm_map_trunc_page((vm_map_offset_t
)addr
,
565 VM_MAP_PAGE_MASK(map
)),
566 vm_map_round_page((vm_map_offset_t
)addr
+ size
,
567 VM_MAP_PAGE_MASK(map
)),
568 VM_MAP_REMOVE_KUNWIRE
);
569 if (ret
!= KERN_SUCCESS
) {
570 panic("vm_map_remove_locked() failed for kalloc vm_entry! addr: %p, map: %p ret: %d\n",
574 DTRACE_VM3(kfree
, vm_size_t
, -1, vm_size_t
, size
, void*, addr
);
577 kalloc_large_total
-= size
;
578 kalloc_large_inuse
--;
581 KALLOC_ZINFO_SFREE(size
);
590 vm_allocation_site_t
* site
)
597 tag
= VM_KERN_MEMORY_KALLOC
;
601 /* expand the allocation to accomodate redzones */
602 vm_size_t req_size
= size
;
603 size
= kasan_alloc_resize(req_size
);
606 if (size
< MAX_SIZE_ZDLUT
)
607 z
= get_zone_dlut(size
);
608 else if (size
< kalloc_max_prerounded
)
609 z
= get_zone_search(size
, k_zindex_start
);
612 * If size is too large for a zone, then use kmem_alloc.
613 * (We use kmem_alloc instead of kmem_alloc_kobject so that
614 * krealloc can use kmem_realloc.)
618 /* kmem_alloc could block so we return if noblock */
624 /* large allocation - use guard pages instead of small redzones */
625 size
= round_page(req_size
+ 2 * PAGE_SIZE
);
626 assert(size
>= MAX_SIZE_ZDLUT
&& size
>= kalloc_max_prerounded
);
629 if (size
>= kalloc_kernmap_size
)
630 alloc_map
= kernel_map
;
632 alloc_map
= kalloc_map
;
634 if (site
) tag
= vm_tag_alloc(site
);
636 if (kmem_alloc_flags(alloc_map
, (vm_offset_t
*)&addr
, size
, tag
, KMA_ATOMIC
) != KERN_SUCCESS
) {
637 if (alloc_map
!= kernel_map
) {
638 if (kalloc_fallback_count
++ == 0) {
639 printf("%s: falling back to kernel_map\n", __func__
);
641 if (kmem_alloc_flags(kernel_map
, (vm_offset_t
*)&addr
, size
, tag
, KMA_ATOMIC
) != KERN_SUCCESS
)
651 * Thread-safe version of the workaround for 4740071
654 if (size
> kalloc_largest_allocated
)
655 kalloc_largest_allocated
= size
;
657 kalloc_large_inuse
++;
658 kalloc_large_total
+= size
;
659 kalloc_large_sum
+= size
;
661 if (kalloc_large_total
> kalloc_large_max
)
662 kalloc_large_max
= kalloc_large_total
;
666 KALLOC_ZINFO_SALLOC(size
);
669 /* fixup the return address to skip the redzone */
670 addr
= (void *)kasan_alloc((vm_offset_t
)addr
, size
, req_size
, PAGE_SIZE
);
672 *psize
= round_page(size
);
674 DTRACE_VM3(kalloc
, vm_size_t
, size
, vm_size_t
, *psize
, void*, addr
);
678 if (size
> z
->elem_size
)
679 panic("%s: z %p (%s) but requested size %lu", __func__
,
680 z
, z
->zone_name
, (unsigned long)size
);
683 assert(size
<= z
->elem_size
);
688 tag
= vm_tag_alloc(site
);
689 if (!canblock
&& !vm_allocation_zone_totals
[tag
]) tag
= VM_KERN_MEMORY_KALLOC
;
693 addr
= zalloc_canblock_tag(z
, canblock
, size
, tag
);
696 /* fixup the return address to skip the redzone */
697 addr
= (void *)kasan_alloc((vm_offset_t
)addr
, z
->elem_size
, req_size
, KASAN_GUARD_SIZE
);
699 /* For KASan, the redzone lives in any additional space, so don't
700 * expand the allocation. */
702 *psize
= z
->elem_size
;
705 DTRACE_VM3(kalloc
, vm_size_t
, size
, vm_size_t
, *psize
, void*, addr
);
716 return( kalloc_tag_bt(size
, VM_KERN_MEMORY_KALLOC
) );
728 * Resize back to the real allocation size and hand off to the KASan
729 * quarantine. `data` may then point to a different allocation.
731 vm_size_t user_size
= size
;
732 kasan_check_free((vm_address_t
)data
, size
, KASAN_HEAP_KALLOC
);
733 data
= (void *)kasan_dealloc((vm_address_t
)data
, &size
);
734 kasan_free(&data
, &size
, KASAN_HEAP_KALLOC
, NULL
, user_size
, true);
740 if (size
< MAX_SIZE_ZDLUT
)
741 z
= get_zone_dlut(size
);
742 else if (size
< kalloc_max_prerounded
)
743 z
= get_zone_search(size
, k_zindex_start
);
745 /* if size was too large for a zone, then use kmem_free */
747 vm_map_t alloc_map
= kernel_map
;
749 if ((((vm_offset_t
) data
) >= kalloc_map_min
) && (((vm_offset_t
) data
) <= kalloc_map_max
))
750 alloc_map
= kalloc_map
;
751 if (size
> kalloc_largest_allocated
) {
752 panic("kfree: size %lu > kalloc_largest_allocated %lu", (unsigned long)size
, (unsigned long)kalloc_largest_allocated
);
754 kmem_free(alloc_map
, (vm_offset_t
)data
, size
);
757 kalloc_large_total
-= size
;
758 kalloc_large_inuse
--;
763 DTRACE_VM3(kfree
, vm_size_t
, size
, vm_size_t
, size
, void*, data
);
766 KALLOC_ZINFO_SFREE(size
);
770 /* free to the appropriate zone */
772 if (size
> z
->elem_size
)
773 panic("%s: z %p (%s) but requested size %lu", __func__
,
774 z
, z
->zone_name
, (unsigned long)size
);
776 assert(size
<= z
->elem_size
);
778 DTRACE_VM3(kfree
, vm_size_t
, size
, vm_size_t
, z
->elem_size
, void*, data
);
788 if (size
< MAX_SIZE_ZDLUT
)
789 return (get_zone_dlut(size
));
790 if (size
<= kalloc_max
)
791 return (get_zone_search(size
, k_zindex_start
));
800 queue_init(&OSMalloc_tag_list
);
802 OSMalloc_tag_lck_grp
= lck_grp_alloc_init("OSMalloc_tag", LCK_GRP_ATTR_NULL
);
803 lck_mtx_init(&OSMalloc_tag_lock
, OSMalloc_tag_lck_grp
, LCK_ATTR_NULL
);
813 OSMTag
= (OSMallocTag
)kalloc(sizeof(*OSMTag
));
815 bzero((void *)OSMTag
, sizeof(*OSMTag
));
817 if (flags
& OSMT_PAGEABLE
)
818 OSMTag
->OSMT_attr
= OSMT_ATTR_PAGEABLE
;
820 OSMTag
->OSMT_refcnt
= 1;
822 strlcpy(OSMTag
->OSMT_name
, str
, OSMT_MAX_NAME
);
824 OSMalloc_tag_spin_lock();
825 enqueue_tail(&OSMalloc_tag_list
, (queue_entry_t
)OSMTag
);
826 OSMalloc_tag_unlock();
827 OSMTag
->OSMT_state
= OSMT_VALID
;
835 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
836 panic("OSMalloc_Tagref():'%s' has bad state 0x%08X\n", tag
->OSMT_name
, tag
->OSMT_state
);
838 (void)hw_atomic_add(&tag
->OSMT_refcnt
, 1);
845 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
846 panic("OSMalloc_Tagref():'%s' has bad state 0x%08X\n", tag
->OSMT_name
, tag
->OSMT_state
);
848 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
849 if (hw_compare_and_store(OSMT_VALID
|OSMT_RELEASED
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
)) {
850 OSMalloc_tag_spin_lock();
851 (void)remque((queue_entry_t
)tag
);
852 OSMalloc_tag_unlock();
853 kfree((void*)tag
, sizeof(*tag
));
855 panic("OSMalloc_Tagrele():'%s' has refcnt 0\n", tag
->OSMT_name
);
863 if (!hw_compare_and_store(OSMT_VALID
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
))
864 panic("OSMalloc_Tagfree():'%s' has bad state 0x%08X \n", tag
->OSMT_name
, tag
->OSMT_state
);
866 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
867 OSMalloc_tag_spin_lock();
868 (void)remque((queue_entry_t
)tag
);
869 OSMalloc_tag_unlock();
870 kfree((void*)tag
, sizeof(*tag
));
882 OSMalloc_Tagref(tag
);
883 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
884 && (size
& ~PAGE_MASK
)) {
885 if ((kr
= kmem_alloc_pageable_external(kernel_map
, (vm_offset_t
*)&addr
, size
)) != KERN_SUCCESS
)
888 addr
= kalloc_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
891 OSMalloc_Tagrele(tag
);
903 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
906 OSMalloc_Tagref(tag
);
907 /* XXX: use non-blocking kalloc for now */
908 addr
= kalloc_noblock_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
910 OSMalloc_Tagrele(tag
);
922 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
925 OSMalloc_Tagref(tag
);
926 addr
= kalloc_noblock_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
928 OSMalloc_Tagrele(tag
);
939 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
940 && (size
& ~PAGE_MASK
)) {
941 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
943 kfree((void *)addr
, size
);
945 OSMalloc_Tagrele(tag
);
952 return (uint32_t)kalloc_size(addr
);