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>
83 zone_t
kalloc_zone(vm_size_t
);
86 #define KALLOC_MAP_SIZE_MIN (16 * 1024 * 1024)
87 #define KALLOC_MAP_SIZE_MAX (128 * 1024 * 1024)
90 vm_size_t kalloc_max_prerounded
;
91 vm_size_t kalloc_kernmap_size
; /* size of kallocs that can come from kernel map */
93 /* how many times we couldn't allocate out of kalloc_map and fell back to kernel_map */
94 unsigned long kalloc_fallback_count
;
96 unsigned int kalloc_large_inuse
;
97 vm_size_t kalloc_large_total
;
98 vm_size_t kalloc_large_max
;
99 vm_size_t kalloc_largest_allocated
= 0;
100 uint64_t kalloc_large_sum
;
102 int kalloc_fake_zone_index
= -1; /* index of our fake zone in statistics arrays */
104 vm_offset_t kalloc_map_min
;
105 vm_offset_t kalloc_map_max
;
109 * Diagnostic code to track mutexes separately rather than via the 2^ zones
115 KALLOC_ZINFO_SALLOC(vm_size_t bytes
)
117 thread_t thr
= current_thread();
118 ledger_debit(thr
->t_ledger
, task_ledgers
.tkm_shared
, bytes
);
122 KALLOC_ZINFO_SFREE(vm_size_t bytes
)
124 thread_t thr
= current_thread();
125 ledger_credit(thr
->t_ledger
, task_ledgers
.tkm_shared
, bytes
);
129 * All allocations of size less than kalloc_max are rounded to the
130 * next nearest sized zone. This allocator is built on top of
131 * the zone allocator. A zone is created for each potential size
132 * that we are willing to get in small blocks.
134 * We assume that kalloc_max is not greater than 64K;
136 * Note that kalloc_max is somewhat confusingly named.
137 * It represents the first power of two for which no zone exists.
138 * kalloc_max_prerounded is the smallest allocation size, before
139 * rounding, for which no zone exists.
141 * Also if the allocation size is more than kalloc_kernmap_size
142 * then allocate from kernel map rather than kalloc_map.
145 #if KALLOC_MINSIZE == 16 && KALLOC_LOG2_MINALIGN == 4
147 #define K_ZONE_SIZES \
164 #define K_ZONE_NAMES \
168 /* 3 */ "kalloc.64", \
171 /* 6 */ "kalloc.128", \
175 /* 9 */ "kalloc.288", \
180 /* C */ "kalloc.1280", \
184 #elif KALLOC_MINSIZE == 8 && KALLOC_LOG2_MINALIGN == 3
187 * Tweaked for ARM (and x64) in 04/2011
190 #define K_ZONE_SIZES \
194 /* 6 */ 64, 72, 88, 112, \
196 256, 288, 384, 440, \
197 /* 9 */ 512, 576, 768, \
202 #define K_ZONE_NAMES \
203 /* 3 */ "kalloc.8", \
204 "kalloc.16", "kalloc.24", \
205 "kalloc.32", "kalloc.40", "kalloc.48", \
206 /* 6 */ "kalloc.64", "kalloc.72", "kalloc.88", "kalloc.112", \
207 "kalloc.128", "kalloc.192", \
208 "kalloc.256", "kalloc.288", "kalloc.384", "kalloc.440", \
209 /* 9 */ "kalloc.512", "kalloc.576", "kalloc.768", \
210 "kalloc.1024", "kalloc.1152", "kalloc.1536", \
211 "kalloc.2048", "kalloc.2128", "kalloc.3072", \
212 "kalloc.4096", "kalloc.6144"
215 #error missing zone size parameters for kalloc
218 #define KALLOC_MINALIGN (1 << KALLOC_LOG2_MINALIGN)
219 #define KiB(x) (1024 * (x))
221 static const int k_zone_size
[] = {
228 #define MAX_K_ZONE (sizeof (k_zone_size) / sizeof (k_zone_size[0]))
230 static const char *k_zone_name
[MAX_K_ZONE
] = {
239 * Many kalloc() allocations are for small structures containing a few
240 * pointers and longs - the k_zone_dlut[] direct lookup table, indexed by
241 * size normalized to the minimum alignment, finds the right zone index
242 * for them in one dereference.
245 #define INDEX_ZDLUT(size) \
246 (((size) + KALLOC_MINALIGN - 1) / KALLOC_MINALIGN)
247 #define N_K_ZDLUT (2048 / KALLOC_MINALIGN)
248 /* covers sizes [0 .. 2048 - KALLOC_MINALIGN] */
249 #define MAX_SIZE_ZDLUT ((N_K_ZDLUT - 1) * KALLOC_MINALIGN)
251 static int8_t k_zone_dlut
[N_K_ZDLUT
]; /* table of indices into k_zone[] */
254 * If there's no hit in the DLUT, then start searching from k_zindex_start.
256 static int k_zindex_start
;
258 static zone_t k_zone
[MAX_K_ZONE
];
260 /* #define KALLOC_DEBUG 1 */
262 /* forward declarations */
264 lck_grp_t kalloc_lck_grp
;
265 lck_mtx_t kalloc_lock
;
267 #define kalloc_spin_lock() lck_mtx_lock_spin(&kalloc_lock)
268 #define kalloc_unlock() lck_mtx_unlock(&kalloc_lock)
271 /* OSMalloc local data declarations */
273 queue_head_t OSMalloc_tag_list
;
275 lck_grp_t
*OSMalloc_tag_lck_grp
;
276 lck_mtx_t OSMalloc_tag_lock
;
278 #define OSMalloc_tag_spin_lock() lck_mtx_lock_spin(&OSMalloc_tag_lock)
279 #define OSMalloc_tag_unlock() lck_mtx_unlock(&OSMalloc_tag_lock)
282 /* OSMalloc forward declarations */
283 void OSMalloc_init(void);
284 void OSMalloc_Tagref(OSMallocTag tag
);
285 void OSMalloc_Tagrele(OSMallocTag tag
);
288 * Initialize the memory allocator. This should be called only
289 * once on a system wide basis (i.e. first processor to get here
290 * does the initialization).
292 * This initializes all of the zones.
299 kern_return_t retval
;
301 vm_size_t size
, kalloc_map_size
;
305 * Scale the kalloc_map_size to physical memory size: stay below
306 * 1/8th the total zone map size, or 128 MB (for a 32-bit kernel).
308 kalloc_map_size
= (vm_size_t
)(sane_size
>> 5);
310 if (kalloc_map_size
> KALLOC_MAP_SIZE_MAX
)
311 kalloc_map_size
= KALLOC_MAP_SIZE_MAX
;
312 #endif /* !__LP64__ */
313 if (kalloc_map_size
< KALLOC_MAP_SIZE_MIN
)
314 kalloc_map_size
= KALLOC_MAP_SIZE_MIN
;
316 retval
= kmem_suballoc(kernel_map
, &min
, kalloc_map_size
,
317 FALSE
, VM_FLAGS_ANYWHERE
| VM_FLAGS_PERMANENT
| VM_MAKE_TAG(0),
320 if (retval
!= KERN_SUCCESS
)
321 panic("kalloc_init: kmem_suballoc failed");
323 kalloc_map_min
= min
;
324 kalloc_map_max
= min
+ kalloc_map_size
- 1;
327 * Create zones up to a least 2 pages because small page-multiples are common
328 * allocations. Also ensure that zones up to size 8192 bytes exist. This is
329 * desirable because messages are allocated with kalloc(), and messages up
330 * through size 8192 are common.
332 kalloc_max
= PAGE_SIZE
<< 2;
333 if (kalloc_max
< KiB(16)) {
334 kalloc_max
= KiB(16);
336 assert(kalloc_max
<= KiB(64)); /* assumption made in size arrays */
338 kalloc_max_prerounded
= kalloc_max
/ 2 + 1;
339 /* allocations larger than 16 times kalloc_max go directly to kernel map */
340 kalloc_kernmap_size
= (kalloc_max
* 16) + 1;
341 kalloc_largest_allocated
= kalloc_kernmap_size
;
344 * Allocate a zone for each size we are going to handle. Don't charge the
345 * caller for the allocation, as we aren't sure how the memory will be
348 for (i
= 0; i
< (int)MAX_K_ZONE
&& (size
= k_zone_size
[i
]) < kalloc_max
; i
++) {
349 k_zone
[i
] = zinit(size
, size
, size
, k_zone_name
[i
]);
350 zone_change(k_zone
[i
], Z_CALLERACCT
, FALSE
);
354 * Build the Direct LookUp Table for small allocations
356 for (i
= 0, size
= 0; i
<= N_K_ZDLUT
; i
++, size
+= KALLOC_MINALIGN
) {
359 while ((vm_size_t
)k_zone_size
[zindex
] < size
)
362 if (i
== N_K_ZDLUT
) {
363 k_zindex_start
= zindex
;
366 k_zone_dlut
[i
] = (int8_t)zindex
;
370 printf("kalloc_init: k_zindex_start %d\n", k_zindex_start
);
373 * Do a quick synthesis to see how well/badly we can
374 * find-a-zone for a given size.
375 * Useful when debugging/tweaking the array of zone sizes.
376 * Cache misses probably more critical than compare-branches!
378 for (i
= 0; i
< (int)MAX_K_ZONE
; i
++) {
379 vm_size_t testsize
= (vm_size_t
)k_zone_size
[i
] - 1;
383 if (testsize
< MAX_SIZE_ZDLUT
) {
384 compare
+= 1; /* 'if' (T) */
386 long dindex
= INDEX_ZDLUT(testsize
);
387 zindex
= (int)k_zone_dlut
[dindex
];
389 } else if (testsize
< kalloc_max_prerounded
) {
391 compare
+= 2; /* 'if' (F), 'if' (T) */
393 zindex
= k_zindex_start
;
394 while ((vm_size_t
)k_zone_size
[zindex
] < testsize
) {
396 compare
++; /* 'while' (T) */
398 compare
++; /* 'while' (F) */
400 break; /* not zone-backed */
402 zone_t z
= k_zone
[zindex
];
403 printf("kalloc_init: req size %4lu: %11s took %d compare%s\n",
404 (unsigned long)testsize
, z
->zone_name
, compare
,
405 compare
== 1 ? "" : "s");
409 lck_grp_init(&kalloc_lck_grp
, "kalloc.large", LCK_GRP_ATTR_NULL
);
410 lck_mtx_init(&kalloc_lock
, &kalloc_lck_grp
, LCK_ATTR_NULL
);
413 lck_mtx_zone
= zinit(sizeof(struct _lck_mtx_
), 1024*256, 4096, "lck_mtx");
418 * Given an allocation size, return the kalloc zone it belongs to.
419 * Direct LookUp Table variant.
421 static __inline zone_t
422 get_zone_dlut(vm_size_t size
)
424 long dindex
= INDEX_ZDLUT(size
);
425 int zindex
= (int)k_zone_dlut
[dindex
];
426 return (k_zone
[zindex
]);
429 /* As above, but linear search k_zone_size[] for the next zone that fits. */
431 static __inline zone_t
432 get_zone_search(vm_size_t size
, int zindex
)
434 assert(size
< kalloc_max_prerounded
);
436 while ((vm_size_t
)k_zone_size
[zindex
] < size
)
439 assert((unsigned)zindex
< MAX_K_ZONE
&&
440 (vm_size_t
)k_zone_size
[zindex
] < kalloc_max
);
442 return (k_zone
[zindex
]);
446 vm_map_lookup_kalloc_entry_locked(
451 vm_map_entry_t vm_entry
= NULL
;
453 ret
= vm_map_lookup_entry(map
, (vm_map_offset_t
)addr
, &vm_entry
);
455 panic("Attempting to lookup/free an address not allocated via kalloc! (vm_map_lookup_entry() failed map: %p, addr: %p)\n",
458 if (vm_entry
->vme_start
!= (vm_map_offset_t
)addr
) {
459 panic("Attempting to lookup/free the middle of a kalloc'ed element! (map: %p, addr: %p, entry: %p)\n",
460 map
, addr
, vm_entry
);
462 if (!vm_entry
->vme_atomic
) {
463 panic("Attempting to lookup/free an address not managed by kalloc! (map: %p, addr: %p, entry: %p)\n",
464 map
, addr
, vm_entry
);
466 return (vm_entry
->vme_end
- vm_entry
->vme_start
);
476 size
= zone_element_size(addr
, NULL
);
480 if (((vm_offset_t
)addr
>= kalloc_map_min
) && ((vm_offset_t
)addr
< kalloc_map_max
)) {
485 vm_map_lock_read(map
);
486 size
= vm_map_lookup_kalloc_entry_locked(map
, addr
);
487 vm_map_unlock_read(map
);
498 if (size
< MAX_SIZE_ZDLUT
) {
499 z
= get_zone_dlut(size
);
503 if (size
< kalloc_max_prerounded
) {
504 z
= get_zone_search(size
, k_zindex_start
);
508 if (size
>= kalloc_kernmap_size
)
513 return vm_map_round_page(size
, VM_MAP_PAGE_MASK(map
));
525 size
= zone_element_size(addr
, &z
);
531 if (((vm_offset_t
)addr
>= kalloc_map_min
) && ((vm_offset_t
)addr
< kalloc_map_max
)) {
536 if ((vm_offset_t
)addr
< VM_MIN_KERNEL_AND_KEXT_ADDRESS
) {
537 panic("kfree on an address not in the kernel & kext address range! addr: %p\n", addr
);
541 size
= vm_map_lookup_kalloc_entry_locked(map
, addr
);
542 ret
= vm_map_remove_locked(map
,
543 vm_map_trunc_page((vm_map_offset_t
)addr
,
544 VM_MAP_PAGE_MASK(map
)),
545 vm_map_round_page((vm_map_offset_t
)addr
+ size
,
546 VM_MAP_PAGE_MASK(map
)),
547 VM_MAP_REMOVE_KUNWIRE
);
548 if (ret
!= KERN_SUCCESS
) {
549 panic("vm_map_remove_locked() failed for kalloc vm_entry! addr: %p, map: %p ret: %d\n",
555 kalloc_large_total
-= size
;
556 kalloc_large_inuse
--;
559 KALLOC_ZINFO_SFREE(size
);
567 vm_allocation_site_t
* site
)
574 if (size
< MAX_SIZE_ZDLUT
)
575 z
= get_zone_dlut(size
);
576 else if (size
< kalloc_max_prerounded
)
577 z
= get_zone_search(size
, k_zindex_start
);
580 * If size is too large for a zone, then use kmem_alloc.
581 * (We use kmem_alloc instead of kmem_alloc_kobject so that
582 * krealloc can use kmem_realloc.)
587 /* kmem_alloc could block so we return if noblock */
592 if (size
>= kalloc_kernmap_size
)
593 alloc_map
= kernel_map
;
595 alloc_map
= kalloc_map
;
598 tag
= (site
? tag
= vm_tag_alloc(site
) : VM_KERN_MEMORY_KALLOC
);
600 if (kmem_alloc_flags(alloc_map
, (vm_offset_t
*)&addr
, size
, tag
, KMA_ATOMIC
) != KERN_SUCCESS
) {
601 if (alloc_map
!= kernel_map
) {
602 if (kalloc_fallback_count
++ == 0) {
603 printf("%s: falling back to kernel_map\n", __func__
);
605 if (kmem_alloc_flags(kernel_map
, (vm_offset_t
*)&addr
, size
, tag
, KMA_ATOMIC
) != KERN_SUCCESS
)
615 * Thread-safe version of the workaround for 4740071
618 if (size
> kalloc_largest_allocated
)
619 kalloc_largest_allocated
= size
;
621 kalloc_large_inuse
++;
622 kalloc_large_total
+= size
;
623 kalloc_large_sum
+= size
;
625 if (kalloc_large_total
> kalloc_large_max
)
626 kalloc_large_max
= kalloc_large_total
;
630 KALLOC_ZINFO_SALLOC(size
);
632 *psize
= round_page(size
);
636 if (size
> z
->elem_size
)
637 panic("%s: z %p (%s) but requested size %lu", __func__
,
638 z
, z
->zone_name
, (unsigned long)size
);
640 assert(size
<= z
->elem_size
);
641 *psize
= z
->elem_size
;
642 void *addr
= zalloc_canblock(z
, canblock
);
653 return( kalloc_tag_bt(size
, VM_KERN_MEMORY_KALLOC
) );
656 volatile SInt32 kfree_nop_count
= 0;
665 if (size
< MAX_SIZE_ZDLUT
)
666 z
= get_zone_dlut(size
);
667 else if (size
< kalloc_max_prerounded
)
668 z
= get_zone_search(size
, k_zindex_start
);
670 /* if size was too large for a zone, then use kmem_free */
672 vm_map_t alloc_map
= kernel_map
;
674 if ((((vm_offset_t
) data
) >= kalloc_map_min
) && (((vm_offset_t
) data
) <= kalloc_map_max
))
675 alloc_map
= kalloc_map
;
676 if (size
> kalloc_largest_allocated
) {
678 * work around double FREEs of small MALLOCs
679 * this used to end up being a nop
680 * since the pointer being freed from an
681 * alloc backed by the zalloc world could
682 * never show up in the kalloc_map... however,
683 * the kernel_map is a different issue... since it
684 * was released back into the zalloc pool, a pointer
685 * would have gotten written over the 'size' that
686 * the MALLOC was retaining in the first 4 bytes of
687 * the underlying allocation... that pointer ends up
688 * looking like a really big size on the 2nd FREE and
689 * pushes the kfree into the kernel_map... we
690 * end up removing a ton of virtual space before we panic
691 * this check causes us to ignore the kfree for a size
692 * that must be 'bogus'... note that it might not be due
693 * to the above scenario, but it would still be wrong and
694 * cause serious damage.
697 OSAddAtomic(1, &kfree_nop_count
);
700 kmem_free(alloc_map
, (vm_offset_t
)data
, size
);
703 kalloc_large_total
-= size
;
704 kalloc_large_inuse
--;
708 KALLOC_ZINFO_SFREE(size
);
712 /* free to the appropriate zone */
714 if (size
> z
->elem_size
)
715 panic("%s: z %p (%s) but requested size %lu", __func__
,
716 z
, z
->zone_name
, (unsigned long)size
);
718 assert(size
<= z
->elem_size
);
727 if (size
< MAX_SIZE_ZDLUT
)
728 return (get_zone_dlut(size
));
729 if (size
<= kalloc_max
)
730 return (get_zone_search(size
, k_zindex_start
));
736 kalloc_fake_zone_init(int zone_index
)
738 kalloc_fake_zone_index
= zone_index
;
742 kalloc_fake_zone_info(int *count
,
743 vm_size_t
*cur_size
, vm_size_t
*max_size
, vm_size_t
*elem_size
, vm_size_t
*alloc_size
,
744 uint64_t *sum_size
, int *collectable
, int *exhaustable
, int *caller_acct
)
746 *count
= kalloc_large_inuse
;
747 *cur_size
= kalloc_large_total
;
748 *max_size
= kalloc_large_max
;
750 if (kalloc_large_inuse
) {
751 *elem_size
= kalloc_large_total
/ kalloc_large_inuse
;
752 *alloc_size
= kalloc_large_total
/ kalloc_large_inuse
;
757 *sum_size
= kalloc_large_sum
;
768 queue_init(&OSMalloc_tag_list
);
770 OSMalloc_tag_lck_grp
= lck_grp_alloc_init("OSMalloc_tag", LCK_GRP_ATTR_NULL
);
771 lck_mtx_init(&OSMalloc_tag_lock
, OSMalloc_tag_lck_grp
, LCK_ATTR_NULL
);
781 OSMTag
= (OSMallocTag
)kalloc(sizeof(*OSMTag
));
783 bzero((void *)OSMTag
, sizeof(*OSMTag
));
785 if (flags
& OSMT_PAGEABLE
)
786 OSMTag
->OSMT_attr
= OSMT_ATTR_PAGEABLE
;
788 OSMTag
->OSMT_refcnt
= 1;
790 strlcpy(OSMTag
->OSMT_name
, str
, OSMT_MAX_NAME
);
792 OSMalloc_tag_spin_lock();
793 enqueue_tail(&OSMalloc_tag_list
, (queue_entry_t
)OSMTag
);
794 OSMalloc_tag_unlock();
795 OSMTag
->OSMT_state
= OSMT_VALID
;
803 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
804 panic("OSMalloc_Tagref():'%s' has bad state 0x%08X\n", tag
->OSMT_name
, tag
->OSMT_state
);
806 (void)hw_atomic_add(&tag
->OSMT_refcnt
, 1);
813 if (!((tag
->OSMT_state
& OSMT_VALID_MASK
) == OSMT_VALID
))
814 panic("OSMalloc_Tagref():'%s' has bad state 0x%08X\n", tag
->OSMT_name
, tag
->OSMT_state
);
816 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
817 if (hw_compare_and_store(OSMT_VALID
|OSMT_RELEASED
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
)) {
818 OSMalloc_tag_spin_lock();
819 (void)remque((queue_entry_t
)tag
);
820 OSMalloc_tag_unlock();
821 kfree((void*)tag
, sizeof(*tag
));
823 panic("OSMalloc_Tagrele():'%s' has refcnt 0\n", tag
->OSMT_name
);
831 if (!hw_compare_and_store(OSMT_VALID
, OSMT_VALID
|OSMT_RELEASED
, &tag
->OSMT_state
))
832 panic("OSMalloc_Tagfree():'%s' has bad state 0x%08X \n", tag
->OSMT_name
, tag
->OSMT_state
);
834 if (hw_atomic_sub(&tag
->OSMT_refcnt
, 1) == 0) {
835 OSMalloc_tag_spin_lock();
836 (void)remque((queue_entry_t
)tag
);
837 OSMalloc_tag_unlock();
838 kfree((void*)tag
, sizeof(*tag
));
850 OSMalloc_Tagref(tag
);
851 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
852 && (size
& ~PAGE_MASK
)) {
853 if ((kr
= kmem_alloc_pageable_external(kernel_map
, (vm_offset_t
*)&addr
, size
)) != KERN_SUCCESS
)
856 addr
= kalloc_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
859 OSMalloc_Tagrele(tag
);
871 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
874 OSMalloc_Tagref(tag
);
875 /* XXX: use non-blocking kalloc for now */
876 addr
= kalloc_noblock_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
878 OSMalloc_Tagrele(tag
);
890 if (tag
->OSMT_attr
& OSMT_PAGEABLE
)
893 OSMalloc_Tagref(tag
);
894 addr
= kalloc_noblock_tag_bt((vm_size_t
)size
, VM_KERN_MEMORY_KALLOC
);
896 OSMalloc_Tagrele(tag
);
907 if ((tag
->OSMT_attr
& OSMT_PAGEABLE
)
908 && (size
& ~PAGE_MASK
)) {
909 kmem_free(kernel_map
, (vm_offset_t
)addr
, size
);
911 kfree((void *)addr
, size
);
913 OSMalloc_Tagrele(tag
);
920 return (uint32_t)kalloc_size(addr
);