2 * Copyright (c) 2000-2007 Apple 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.
62 * Zone-based memory allocator. A zone is a collection of fixed size
63 * data blocks for which quick allocation/deallocation is possible.
65 #include <zone_debug.h>
66 #include <zone_alias_addr.h>
70 #include <mach/mach_types.h>
71 #include <mach/vm_param.h>
72 #include <mach/kern_return.h>
73 #include <mach/mach_host_server.h>
74 #include <mach/machine/vm_types.h>
75 #include <mach_debug/zone_info.h>
77 #include <kern/kern_types.h>
78 #include <kern/assert.h>
79 #include <kern/host.h>
80 #include <kern/macro_help.h>
81 #include <kern/sched.h>
82 #include <kern/lock.h>
83 #include <kern/sched_prim.h>
84 #include <kern/misc_protos.h>
85 #include <kern/thread_call.h>
86 #include <kern/zalloc.h>
87 #include <kern/kalloc.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_page.h>
94 #include <machine/machparam.h>
96 #include <libkern/OSDebug.h>
97 #include <sys/kdebug.h>
100 /* for fake zone stat routines */
101 #include <ppc/savearea.h>
102 #include <ppc/mappings.h>
105 int check_freed_element
= 0;
108 /* Detect use of zone elt after freeing it by two methods:
109 * (1) Range-check the free-list "next" ptr for sanity.
110 * (2) Store the ptr in two different words, and compare them against
111 * each other when re-using the zone elt, to detect modifications;
116 #define is_kernel_data_addr(a) \
117 (!(a) || (IS_SYS_VA(a) && !((a) & (sizeof(long)-1))))
119 #else /* !defined(__alpha) */
121 #define is_kernel_data_addr(a) \
122 (!(a) || ((a) >= vm_min_kernel_address && !((a) & 0x3)))
124 #endif /* defined(__alpha) */
126 /* Should we set all words of the zone element to an illegal address
127 * when it is freed, to help catch usage after freeing? The down-side
128 * is that this obscures the identity of the freed element.
130 boolean_t zfree_clear
= FALSE
;
132 #define ADD_TO_ZONE(zone, element) \
137 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
139 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
141 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
142 (zone)->free_elements = (vm_offset_t) (element); \
146 #define REMOVE_FROM_ZONE(zone, ret, type) \
148 (ret) = (type) (zone)->free_elements; \
149 if ((ret) != (type) 0) { \
150 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
151 panic("A freed zone element has been modified.\n"); \
154 (zone)->free_elements = *((vm_offset_t *)(ret)); \
157 #else /* MACH_ASSERT */
159 #define ADD_TO_ZONE(zone, element) \
161 *((vm_offset_t *)(element)) = (zone)->free_elements; \
162 if (check_freed_element) { \
163 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t))) \
164 ((vm_offset_t *)(element))[((zone)->elem_size/sizeof(vm_offset_t))-1] = \
165 (zone)->free_elements; \
167 (zone)->free_elements = (vm_offset_t) (element); \
171 #define REMOVE_FROM_ZONE(zone, ret, type) \
173 (ret) = (type) (zone)->free_elements; \
174 if ((ret) != (type) 0) { \
175 if (check_freed_element) { \
176 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t)) && \
177 ((vm_offset_t *)(ret))[((zone)->elem_size/sizeof(vm_offset_t))-1] != \
178 ((vm_offset_t *)(ret))[0]) \
179 panic("a freed zone element has been modified");\
182 (zone)->free_elements = *((vm_offset_t *)(ret)); \
186 #endif /* MACH_ASSERT */
189 #define zone_debug_enabled(z) z->active_zones.next
190 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
191 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
192 #endif /* ZONE_DEBUG */
195 * Support for garbage collection of unused zone pages:
198 struct zone_page_table_entry
{
199 struct zone_page_table_entry
*link
;
210 void zone_page_alloc(
214 void zone_page_free_element(
215 struct zone_page_table_entry
**free_pages
,
219 void zone_page_collect(
223 boolean_t
zone_page_collectable(
232 thread_call_param_t p0
,
233 thread_call_param_t p1
);
236 #if ZONE_DEBUG && MACH_KDB
240 #endif /* ZONE_DEBUG && MACH_KDB */
242 vm_map_t zone_map
= VM_MAP_NULL
;
244 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
247 * The VM system gives us an initial chunk of memory.
248 * It has to be big enough to allocate the zone_zone
252 vm_size_t zdata_size
;
254 #define lock_zone(zone) \
256 lck_mtx_lock(&(zone)->lock); \
259 #define unlock_zone(zone) \
261 lck_mtx_unlock(&(zone)->lock); \
264 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
265 #define zone_sleep(zone) \
266 (void) lck_mtx_sleep(&(zone)->lock, 0, (event_t)(zone), THREAD_UNINT);
268 extern int snprintf(char *, size_t, const char *, ...) __printflike(3,4);
270 #define lock_zone_init(zone) \
273 (void) snprintf(_name, sizeof (_name), "zone.%s", (zone)->zone_name); \
274 lck_grp_attr_setdefault(&(zone)->lock_grp_attr); \
275 lck_grp_init(&(zone)->lock_grp, _name, &(zone)->lock_grp_attr); \
276 lck_attr_setdefault(&(zone)->lock_attr); \
277 lck_mtx_init_ext(&(zone)->lock, &(zone)->lock_ext, \
278 &(zone)->lock_grp, &(zone)->lock_attr); \
281 #define lock_try_zone(zone) lck_mtx_try_lock(&zone->lock)
283 kern_return_t
zget_space(
285 vm_offset_t
*result
);
287 decl_simple_lock_data(,zget_space_lock
)
288 vm_offset_t zalloc_next_space
;
289 vm_offset_t zalloc_end_of_space
;
290 vm_size_t zalloc_wasted_space
;
293 * Garbage collection map information
295 struct zone_page_table_entry
* zone_page_table
;
296 vm_offset_t zone_map_min_address
;
297 vm_offset_t zone_map_max_address
;
298 unsigned int zone_pages
;
301 * Exclude more than one concurrent garbage collection
303 decl_mutex_data(, zone_gc_lock
)
306 #define from_zone_map(addr, size) \
307 ((vm_offset_t)(addr) >= zone_map_min_address && \
308 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
310 #define from_zone_map(addr, size) \
311 ((vm_offset_t)(zone_virtual_addr((vm_map_address_t)addr)) >= zone_map_min_address && \
312 ((vm_offset_t)(zone_virtual_addr((vm_map_address_t)addr)) + size -1) < zone_map_max_address)
315 #define ZONE_PAGE_USED 0
316 #define ZONE_PAGE_UNUSED -1
320 * Protects first_zone, last_zone, num_zones,
321 * and the next_zone field of zones.
323 decl_simple_lock_data(, all_zones_lock
)
326 unsigned int num_zones
;
328 boolean_t zone_gc_allowed
= TRUE
;
329 boolean_t zone_gc_forced
= FALSE
;
330 unsigned zone_gc_last_tick
= 0;
331 unsigned zone_gc_max_rate
= 0; /* in ticks */
335 * zinit initializes a new zone. The zone data structures themselves
336 * are stored in a zone, which is initially a static structure that
337 * is initialized by zone_init.
341 vm_size_t size
, /* the size of an element */
342 vm_size_t max
, /* maximum memory to use */
343 vm_size_t alloc
, /* allocation size */
344 const char *name
) /* a name for the zone */
348 if (zone_zone
== ZONE_NULL
) {
349 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
353 z
= (zone_t
) zalloc(zone_zone
);
358 * Round off all the parameters appropriately.
360 if (size
< sizeof(z
->free_elements
))
361 size
= sizeof(z
->free_elements
);
362 size
= ((size
-1) + sizeof(z
->free_elements
)) -
363 ((size
-1) % sizeof(z
->free_elements
));
366 alloc
= round_page(alloc
);
367 max
= round_page(max
);
369 * we look for an allocation size with less than 1% waste
370 * up to 5 pages in size...
371 * otherwise, we look for an allocation size with least fragmentation
372 * in the range of 1 - 5 pages
373 * This size will be used unless
374 * the user suggestion is larger AND has less fragmentation
377 if ((size
< PAGE_SIZE
) && (PAGE_SIZE
% size
<= PAGE_SIZE
/ 10))
381 { vm_size_t best
, waste
; unsigned int i
;
385 for (i
= 1; i
<= 5; i
++) {
386 vm_size_t tsize
, twaste
;
388 tsize
= i
* PAGE_SIZE
;
390 if ((tsize
% size
) < (tsize
/ 100)) {
392 goto use_this_allocation
;
394 twaste
= tsize
% size
;
396 best
= tsize
, waste
= twaste
;
398 if (alloc
<= best
|| (alloc
% size
>= waste
))
402 if (max
&& (max
< alloc
))
405 z
->free_elements
= 0;
409 z
->alloc_size
= alloc
;
412 z
->doing_alloc
= FALSE
;
414 z
->exhaustible
= FALSE
;
415 z
->collectable
= TRUE
;
416 z
->allows_foreign
= FALSE
;
417 z
->expandable
= TRUE
;
419 z
->async_pending
= FALSE
;
422 z
->active_zones
.next
= z
->active_zones
.prev
= NULL
;
423 zone_debug_enable(z
);
424 #endif /* ZONE_DEBUG */
428 * Add the zone to the all-zones list.
431 z
->next_zone
= ZONE_NULL
;
432 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
433 simple_lock(&all_zones_lock
);
435 last_zone
= &z
->next_zone
;
437 simple_unlock(&all_zones_lock
);
443 * Cram the given memory into the specified zone.
447 register zone_t zone
,
451 register vm_size_t elem_size
;
452 vm_offset_t newmem
= (vm_offset_t
) newaddr
;
454 /* Basic sanity checks */
455 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
456 assert(!zone
->collectable
|| zone
->allows_foreign
457 || (from_zone_map(newmem
, size
)));
459 elem_size
= zone
->elem_size
;
462 while (size
>= elem_size
) {
463 ADD_TO_ZONE(zone
, newmem
);
464 if (from_zone_map(newmem
, elem_size
))
465 zone_page_alloc(newmem
, elem_size
);
466 zone
->count
++; /* compensate for ADD_TO_ZONE */
469 zone
->cur_size
+= elem_size
;
475 * Contiguous space allocator for non-paged zones. Allocates "size" amount
476 * of memory from zone_map.
484 vm_offset_t new_space
= 0;
485 vm_size_t space_to_add
= 0;
487 simple_lock(&zget_space_lock
);
488 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
490 * Add at least one page to allocation area.
493 space_to_add
= round_page(size
);
495 if (new_space
== 0) {
496 kern_return_t retval
;
498 * Memory cannot be wired down while holding
499 * any locks that the pageout daemon might
500 * need to free up pages. [Making the zget_space
501 * lock a complex lock does not help in this
504 * Unlock and allocate memory. Because several
505 * threads might try to do this at once, don't
506 * use the memory before checking for available
510 simple_unlock(&zget_space_lock
);
512 retval
= kernel_memory_allocate(zone_map
, &new_space
,
513 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
514 if (retval
!= KERN_SUCCESS
)
517 if (space_to_add
== PAGE_SIZE
)
518 new_space
= zone_alias_addr(new_space
);
520 zone_page_init(new_space
, space_to_add
,
522 simple_lock(&zget_space_lock
);
528 * Memory was allocated in a previous iteration.
530 * Check whether the new region is contiguous
534 if (new_space
!= zalloc_end_of_space
) {
536 * Throw away the remainder of the
537 * old space, and start a new one.
539 zalloc_wasted_space
+=
540 zalloc_end_of_space
- zalloc_next_space
;
541 zalloc_next_space
= new_space
;
544 zalloc_end_of_space
= new_space
+ space_to_add
;
548 *result
= zalloc_next_space
;
549 zalloc_next_space
+= size
;
550 simple_unlock(&zget_space_lock
);
553 kmem_free(zone_map
, new_space
, space_to_add
);
555 return(KERN_SUCCESS
);
560 * Steal memory for the zone package. Called from
561 * vm_page_bootstrap().
564 zone_steal_memory(void)
566 zdata_size
= round_page(128*sizeof(struct zone
));
567 zdata
= (vm_offset_t
)((char *)pmap_steal_memory(zdata_size
) - (char *)0);
572 * Fill a zone with enough memory to contain at least nelem elements.
573 * Memory is obtained with kmem_alloc_wired from the kernel_map.
574 * Return the number of elements actually put into the zone, which may
575 * be more than the caller asked for since the memory allocation is
576 * rounded up to a full page.
591 size
= nelem
* zone
->elem_size
;
592 size
= round_page(size
);
593 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
594 if (kr
!= KERN_SUCCESS
)
597 zone_change(zone
, Z_FOREIGN
, TRUE
);
598 zcram(zone
, (void *)memory
, size
);
599 nalloc
= size
/ zone
->elem_size
;
600 assert(nalloc
>= nelem
);
606 * Initialize the "zone of zones" which uses fixed memory allocated
607 * earlier in memory initialization. zone_bootstrap is called
613 vm_size_t zone_zone_size
;
614 vm_offset_t zone_zone_space
;
617 /* see if we want freed zone element checking */
618 if (PE_parse_boot_arg("-zc", temp_buf
)) {
619 check_freed_element
= 1;
622 simple_lock_init(&all_zones_lock
, 0);
624 first_zone
= ZONE_NULL
;
625 last_zone
= &first_zone
;
628 simple_lock_init(&zget_space_lock
, 0);
629 zalloc_next_space
= zdata
;
630 zalloc_end_of_space
= zdata
+ zdata_size
;
631 zalloc_wasted_space
= 0;
633 /* assertion: nobody else called zinit before us */
634 assert(zone_zone
== ZONE_NULL
);
635 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
636 sizeof(struct zone
), "zones");
637 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
638 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
639 zget_space(zone_zone_size
, &zone_zone_space
);
640 zcram(zone_zone
, (void *)zone_zone_space
, zone_zone_size
);
645 vm_size_t max_zonemap_size
)
647 kern_return_t retval
;
648 vm_offset_t zone_min
;
649 vm_offset_t zone_max
;
650 vm_size_t zone_table_size
;
652 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
653 FALSE
, VM_FLAGS_ANYWHERE
, &zone_map
);
655 if (retval
!= KERN_SUCCESS
)
656 panic("zone_init: kmem_suballoc failed");
657 zone_max
= zone_min
+ round_page(max_zonemap_size
);
659 * Setup garbage collection information:
661 zone_table_size
= atop_32(zone_max
- zone_min
) *
662 sizeof(struct zone_page_table_entry
);
663 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
664 zone_table_size
) != KERN_SUCCESS
)
666 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
667 zone_pages
= atop_32(zone_max
- zone_min
);
668 zone_map_min_address
= zone_min
;
669 zone_map_max_address
= zone_max
;
670 mutex_init(&zone_gc_lock
, 0);
671 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
676 * zalloc returns an element from the specified zone.
680 register zone_t zone
,
684 kern_return_t retval
;
686 assert(zone
!= ZONE_NULL
);
690 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
692 while ((addr
== 0) && canblock
&& (zone
->doing_gc
)) {
693 zone
->waiting
= TRUE
;
695 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
698 while ((addr
== 0) && canblock
) {
700 * If nothing was there, try to get more
702 if (zone
->doing_alloc
) {
704 * Someone is allocating memory for this zone.
705 * Wait for it to show up, then try again.
707 zone
->waiting
= TRUE
;
711 if ((zone
->cur_size
+ zone
->elem_size
) >
713 if (zone
->exhaustible
)
715 if (zone
->expandable
) {
717 * We're willing to overflow certain
718 * zones, but not without complaining.
720 * This is best used in conjunction
721 * with the collectable flag. What we
722 * want is an assurance we can get the
723 * memory back, assuming there's no
726 zone
->max_size
+= (zone
->max_size
>> 1);
730 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
733 zone
->doing_alloc
= TRUE
;
736 if (zone
->collectable
) {
738 vm_size_t alloc_size
;
743 if (vm_pool_low() || retry
>= 1)
745 round_page(zone
->elem_size
);
747 alloc_size
= zone
->alloc_size
;
749 retval
= kernel_memory_allocate(zone_map
,
750 &space
, alloc_size
, 0,
751 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
752 if (retval
== KERN_SUCCESS
) {
754 if (alloc_size
== PAGE_SIZE
)
755 space
= zone_alias_addr(space
);
757 zone_page_init(space
, alloc_size
,
759 zcram(zone
, (void *)space
, alloc_size
);
762 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
767 printf("zalloc did gc\n");
770 panic("zalloc: \"%s\" (%d elements) retry fail %d", zone
->zone_name
, zone
->count
, retval
);
776 zone
->doing_alloc
= FALSE
;
778 zone
->waiting
= FALSE
;
781 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
783 retval
== KERN_RESOURCE_SHORTAGE
) {
791 retval
= zget_space(zone
->elem_size
, &space
);
794 zone
->doing_alloc
= FALSE
;
796 zone
->waiting
= FALSE
;
797 thread_wakeup((event_t
)zone
);
799 if (retval
== KERN_SUCCESS
) {
801 zone
->cur_size
+= zone
->elem_size
;
803 if (zone_debug_enabled(zone
)) {
804 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
808 zone_page_alloc(space
, zone
->elem_size
);
810 if (zone_debug_enabled(zone
))
811 space
+= ZONE_DEBUG_OFFSET
;
816 if (retval
== KERN_RESOURCE_SHORTAGE
) {
822 panic("zalloc: \"%s\" (%d elements) zget_space returned %d", zone
->zone_name
, zone
->count
, retval
);
827 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
830 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (zone
->exhaustible
== FALSE
) && (!vm_pool_low())) {
831 zone
->async_pending
= TRUE
;
833 thread_call_enter(&zone
->call_async_alloc
);
835 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
839 if (addr
&& zone_debug_enabled(zone
)) {
840 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
841 addr
+= ZONE_DEBUG_OFFSET
;
848 TRACE_MACHLEAKS(ZALLOC_CODE
, ZALLOC_CODE_2
, zone
->elem_size
, addr
);
850 return((void *)addr
);
856 register zone_t zone
)
858 return( zalloc_canblock(zone
, TRUE
) );
863 register zone_t zone
)
865 return( zalloc_canblock(zone
, FALSE
) );
870 thread_call_param_t p0
,
871 __unused thread_call_param_t p1
)
875 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
876 zfree((zone_t
)p0
, elt
);
877 lock_zone(((zone_t
)p0
));
878 ((zone_t
)p0
)->async_pending
= FALSE
;
879 unlock_zone(((zone_t
)p0
));
884 * zget returns an element from the specified zone
885 * and immediately returns nothing if there is nothing there.
887 * This form should be used when you can not block (like when
888 * processing an interrupt).
892 register zone_t zone
)
894 register vm_offset_t addr
;
896 assert( zone
!= ZONE_NULL
);
898 if (!lock_try_zone(zone
))
901 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
903 if (addr
&& zone_debug_enabled(zone
)) {
904 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
905 addr
+= ZONE_DEBUG_OFFSET
;
907 #endif /* ZONE_DEBUG */
910 return((void *) addr
);
913 /* Keep this FALSE by default. Large memory machine run orders of magnitude
914 slower in debug mode when true. Use debugger to enable if needed */
915 /* static */ boolean_t zone_check
= FALSE
;
917 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
918 static vm_offset_t zone_last_bogus_elem
= 0;
922 register zone_t zone
,
925 vm_offset_t elem
= (vm_offset_t
) addr
;
928 /* Basic sanity checks */
929 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
930 panic("zfree: NULL");
931 /* zone_gc assumes zones are never freed */
932 if (zone
== zone_zone
)
933 panic("zfree: freeing to zone_zone breaks zone_gc!");
936 TRACE_MACHLEAKS(ZFREE_CODE
, ZFREE_CODE_2
, zone
->elem_size
, (int)addr
);
938 if (zone
->collectable
&& !zone
->allows_foreign
&&
939 !from_zone_map(elem
, zone
->elem_size
)) {
941 panic("zfree: non-allocated memory in collectable zone!");
943 zone_last_bogus_zone
= zone
;
944 zone_last_bogus_elem
= elem
;
950 if (zone_debug_enabled(zone
)) {
953 elem
-= ZONE_DEBUG_OFFSET
;
955 /* check the zone's consistency */
957 for (tmp_elem
= queue_first(&zone
->active_zones
);
958 !queue_end(tmp_elem
, &zone
->active_zones
);
959 tmp_elem
= queue_next(tmp_elem
))
960 if (elem
== (vm_offset_t
)tmp_elem
)
962 if (elem
!= (vm_offset_t
)tmp_elem
)
963 panic("zfree()ing element from wrong zone");
965 remqueue(&zone
->active_zones
, (queue_t
) elem
);
967 #endif /* ZONE_DEBUG */
971 /* check the zone's consistency */
973 for (this = zone
->free_elements
;
975 this = * (vm_offset_t
*) this)
976 if (!pmap_kernel_va(this) || this == elem
)
979 ADD_TO_ZONE(zone
, elem
);
982 * If elements have one or more pages, and memory is low,
983 * request to run the garbage collection in the zone the next
984 * time the pageout thread runs.
986 if (zone
->elem_size
>= PAGE_SIZE
&&
988 zone_gc_forced
= TRUE
;
994 /* Change a zone's flags.
995 * This routine must be called immediately after zinit.
1003 assert( zone
!= ZONE_NULL
);
1004 assert( value
== TRUE
|| value
== FALSE
);
1008 zone
->exhaustible
= value
;
1011 zone
->collectable
= value
;
1014 zone
->expandable
= value
;
1017 zone
->allows_foreign
= value
;
1021 panic("Zone_change: Wrong Item Type!");
1028 * Return the expected number of free elements in the zone.
1029 * This calculation will be incorrect if items are zfree'd that
1030 * were never zalloc'd/zget'd. The correct way to stuff memory
1031 * into a zone is by zcram.
1035 zone_free_count(zone_t zone
)
1037 integer_t free_count
;
1040 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
1043 assert(free_count
>= 0);
1049 * zprealloc preallocates wired memory, exanding the specified
1050 * zone to the specified size
1060 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
1062 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
1063 zcram(zone
, (void *)addr
, size
);
1068 * Zone garbage collection subroutines
1072 zone_page_collectable(
1076 struct zone_page_table_entry
*zp
;
1080 addr
= zone_virtual_addr(addr
);
1083 if (!from_zone_map(addr
, size
))
1084 panic("zone_page_collectable");
1087 i
= atop_32(addr
-zone_map_min_address
);
1088 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1090 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1091 if (zp
->collect_count
== zp
->alloc_count
)
1102 struct zone_page_table_entry
*zp
;
1106 addr
= zone_virtual_addr(addr
);
1109 if (!from_zone_map(addr
, size
))
1110 panic("zone_page_keep");
1113 i
= atop_32(addr
-zone_map_min_address
);
1114 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1116 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1117 zp
->collect_count
= 0;
1125 struct zone_page_table_entry
*zp
;
1129 addr
= zone_virtual_addr(addr
);
1132 if (!from_zone_map(addr
, size
))
1133 panic("zone_page_collect");
1136 i
= atop_32(addr
-zone_map_min_address
);
1137 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1139 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1140 ++zp
->collect_count
;
1149 struct zone_page_table_entry
*zp
;
1153 addr
= zone_virtual_addr(addr
);
1156 if (!from_zone_map(addr
, size
))
1157 panic("zone_page_init");
1160 i
= atop_32(addr
-zone_map_min_address
);
1161 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1163 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1164 zp
->alloc_count
= value
;
1165 zp
->collect_count
= 0;
1174 struct zone_page_table_entry
*zp
;
1178 addr
= zone_virtual_addr(addr
);
1181 if (!from_zone_map(addr
, size
))
1182 panic("zone_page_alloc");
1185 i
= atop_32(addr
-zone_map_min_address
);
1186 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1188 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1190 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1191 * it was previously set to ZONE_PAGE_UNUSED.
1193 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1194 zp
->alloc_count
= 1;
1201 zone_page_free_element(
1202 struct zone_page_table_entry
**free_pages
,
1206 struct zone_page_table_entry
*zp
;
1210 addr
= zone_virtual_addr(addr
);
1213 if (!from_zone_map(addr
, size
))
1214 panic("zone_page_free_element");
1217 i
= atop_32(addr
-zone_map_min_address
);
1218 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1220 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1221 if (zp
->collect_count
> 0)
1222 --zp
->collect_count
;
1223 if (--zp
->alloc_count
== 0) {
1224 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1225 zp
->collect_count
= 0;
1227 zp
->link
= *free_pages
;
1234 /* This is used for walking through a zone's free element list.
1236 struct zone_free_element
{
1237 struct zone_free_element
* next
;
1241 * Add a linked list of pages starting at base back into the zone
1242 * free list. Tail points to the last element on the list.
1245 #define ADD_LIST_TO_ZONE(zone, base, tail) \
1247 (tail)->next = (void *)((zone)->free_elements); \
1248 if (check_freed_element) { \
1249 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t))) \
1250 ((vm_offset_t *)(tail))[((zone)->elem_size/sizeof(vm_offset_t))-1] = \
1251 (zone)->free_elements; \
1253 (zone)->free_elements = (unsigned long)(base); \
1257 * Add an element to the chain pointed to by prev.
1260 #define ADD_ELEMENT(zone, prev, elem) \
1262 (prev)->next = (elem); \
1263 if (check_freed_element) { \
1264 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t))) \
1265 ((vm_offset_t *)(prev))[((zone)->elem_size/sizeof(vm_offset_t))-1] = \
1266 (vm_offset_t)(elem); \
1273 uint32_t elems_collected
,
1278 /* Zone garbage collection
1280 * zone_gc will walk through all the free elements in all the
1281 * zones that are marked collectable looking for reclaimable
1282 * pages. zone_gc is called by consider_zone_gc when the system
1283 * begins to run out of memory.
1288 unsigned int max_zones
;
1291 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1293 mutex_lock(&zone_gc_lock
);
1295 simple_lock(&all_zones_lock
);
1296 max_zones
= num_zones
;
1298 simple_unlock(&all_zones_lock
);
1301 for (i
= 0; i
< zone_pages
; i
++)
1302 assert(zone_page_table
[i
].collect_count
== 0);
1303 #endif /* MACH_ASSERT */
1305 zone_free_pages
= NULL
;
1307 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1309 vm_size_t elt_size
, size_freed
;
1310 struct zone_free_element
*elt
, *base_elt
, *base_prev
, *prev
, *scan
, *keep
, *tail
;
1312 assert(z
!= ZONE_NULL
);
1314 if (!z
->collectable
)
1319 elt_size
= z
->elem_size
;
1322 * Do a quick feasability check before we scan the zone:
1323 * skip unless there is likelihood of getting pages back
1324 * (i.e we need a whole allocation block's worth of free
1325 * elements before we can garbage collect) and
1326 * the zone has more than 10 percent of it's elements free
1327 * or the element size is a multiple of the PAGE_SIZE
1329 if ((elt_size
& PAGE_MASK
) &&
1330 (((z
->cur_size
- z
->count
* elt_size
) <= (2 * z
->alloc_size
)) ||
1331 ((z
->cur_size
- z
->count
* elt_size
) <= (z
->cur_size
/ 10)))) {
1339 * Snatch all of the free elements away from the zone.
1342 scan
= (void *)z
->free_elements
;
1343 z
->free_elements
= 0;
1350 * Determine which elements we can attempt to collect
1351 * and count them up in the page table. Foreign elements
1352 * are returned to the zone.
1355 prev
= (void *)&scan
;
1357 n
= 0; tail
= keep
= NULL
;
1358 while (elt
!= NULL
) {
1359 if (from_zone_map(elt
, elt_size
)) {
1360 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1365 ++zgc_stats
.elems_collected
;
1371 ADD_ELEMENT(z
, tail
, elt
);
1375 ADD_ELEMENT(z
, prev
, elt
->next
);
1377 ADD_ELEMENT(z
, tail
, NULL
);
1381 * Dribble back the elements we are keeping.
1385 if (z
->waiting
== TRUE
) {
1389 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1395 while ((elt
!= NULL
) && (++m
< 50)) {
1400 ADD_LIST_TO_ZONE(z
, base_elt
, prev
);
1401 ADD_ELEMENT(z
, base_prev
, elt
);
1418 * Return any remaining elements.
1424 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1432 * Determine which pages we can reclaim and
1433 * free those elements.
1438 n
= 0; tail
= keep
= NULL
;
1439 while (elt
!= NULL
) {
1440 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1441 size_freed
+= elt_size
;
1442 zone_page_free_element(&zone_free_pages
,
1443 (vm_offset_t
)elt
, elt_size
);
1447 ++zgc_stats
.elems_freed
;
1450 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1455 ADD_ELEMENT(z
, tail
, elt
);
1460 ADD_ELEMENT(z
, tail
, NULL
);
1462 ++zgc_stats
.elems_kept
;
1466 * Dribble back the elements we are keeping,
1467 * and update the zone size info.
1473 z
->cur_size
-= size_freed
;
1477 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1487 n
= 0; tail
= keep
= NULL
;
1492 * Return any remaining elements, and update
1493 * the zone size info.
1498 if (size_freed
> 0 || keep
!= NULL
) {
1500 z
->cur_size
-= size_freed
;
1503 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1508 z
->doing_gc
= FALSE
;
1517 * Reclaim the pages we are freeing.
1520 while ((zp
= zone_free_pages
) != NULL
) {
1521 zone_free_pages
= zp
->link
;
1523 z
= zone_virtual_addr((vm_map_address_t
)z
);
1525 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1526 (zp
- zone_page_table
), PAGE_SIZE
);
1527 ++zgc_stats
.pgs_freed
;
1530 mutex_unlock(&zone_gc_lock
);
1536 * Called by the pageout daemon when the system needs more free pages.
1540 consider_zone_gc(void)
1543 * By default, don't attempt zone GC more frequently
1544 * than once / 1 minutes.
1547 if (zone_gc_max_rate
== 0)
1548 zone_gc_max_rate
= (60 << SCHED_TICK_SHIFT
) + 1;
1550 if (zone_gc_allowed
&&
1551 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1553 zone_gc_forced
= FALSE
;
1554 zone_gc_last_tick
= sched_tick
;
1559 struct fake_zone_info
{
1561 void (*func
)(int *, vm_size_t
*, vm_size_t
*, vm_size_t
*, vm_size_t
*,
1565 static struct fake_zone_info fake_zones
[] = {
1567 .name
= "kernel_stacks",
1568 .func
= stack_fake_zone_info
,
1572 .name
= "save_areas",
1573 .func
= save_fake_zone_info
,
1576 .name
= "pmap_mappings",
1577 .func
= mapping_fake_zone_info
,
1582 .name
= "page_tables",
1583 .func
= pt_fake_zone_info
,
1587 .name
= "kalloc.large",
1588 .func
= kalloc_fake_zone_info
,
1595 zone_name_array_t
*namesp
,
1596 mach_msg_type_number_t
*namesCntp
,
1597 zone_info_array_t
*infop
,
1598 mach_msg_type_number_t
*infoCntp
)
1601 vm_offset_t names_addr
;
1602 vm_size_t names_size
;
1604 vm_offset_t info_addr
;
1605 vm_size_t info_size
;
1606 unsigned int max_zones
, i
;
1611 size_t num_fake_zones
;
1613 if (host
== HOST_NULL
)
1614 return KERN_INVALID_HOST
;
1616 num_fake_zones
= sizeof fake_zones
/ sizeof fake_zones
[0];
1619 * We assume that zones aren't freed once allocated.
1620 * We won't pick up any zones that are allocated later.
1623 simple_lock(&all_zones_lock
);
1624 max_zones
= num_zones
+ num_fake_zones
;
1626 simple_unlock(&all_zones_lock
);
1628 if (max_zones
<= *namesCntp
) {
1629 /* use in-line memory */
1630 names_size
= *namesCntp
* sizeof *names
;
1633 names_size
= round_page(max_zones
* sizeof *names
);
1634 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1635 &names_addr
, names_size
);
1636 if (kr
!= KERN_SUCCESS
)
1638 names
= (zone_name_t
*) names_addr
;
1641 if (max_zones
<= *infoCntp
) {
1642 /* use in-line memory */
1643 info_size
= *infoCntp
* sizeof *info
;
1646 info_size
= round_page(max_zones
* sizeof *info
);
1647 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1648 &info_addr
, info_size
);
1649 if (kr
!= KERN_SUCCESS
) {
1650 if (names
!= *namesp
)
1651 kmem_free(ipc_kernel_map
,
1652 names_addr
, names_size
);
1656 info
= (zone_info_t
*) info_addr
;
1661 for (i
= 0; i
< num_zones
; i
++) {
1664 assert(z
!= ZONE_NULL
);
1670 simple_lock(&all_zones_lock
);
1672 simple_unlock(&all_zones_lock
);
1674 /* assuming here the name data is static */
1675 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1676 sizeof zn
->zn_name
);
1677 zn
->zn_name
[sizeof zn
->zn_name
- 1] = '\0';
1679 zi
->zi_count
= zcopy
.count
;
1680 zi
->zi_cur_size
= zcopy
.cur_size
;
1681 zi
->zi_max_size
= zcopy
.max_size
;
1682 zi
->zi_elem_size
= zcopy
.elem_size
;
1683 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1684 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1685 zi
->zi_collectable
= zcopy
.collectable
;
1692 * loop through the fake zones and fill them using the specialized
1695 for (i
= 0; i
< num_fake_zones
; i
++) {
1696 strncpy(zn
->zn_name
, fake_zones
[i
].name
, sizeof zn
->zn_name
);
1697 zn
->zn_name
[sizeof zn
->zn_name
- 1] = '\0';
1698 fake_zones
[i
].func(&zi
->zi_count
, &zi
->zi_cur_size
,
1699 &zi
->zi_max_size
, &zi
->zi_elem_size
,
1700 &zi
->zi_alloc_size
, &zi
->zi_collectable
,
1701 &zi
->zi_exhaustible
);
1706 if (names
!= *namesp
) {
1710 used
= max_zones
* sizeof *names
;
1712 if (used
!= names_size
)
1713 bzero((char *) (names_addr
+ used
), names_size
- used
);
1715 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)names_addr
,
1716 (vm_map_size_t
)names_size
, TRUE
, ©
);
1717 assert(kr
== KERN_SUCCESS
);
1719 *namesp
= (zone_name_t
*) copy
;
1721 *namesCntp
= max_zones
;
1723 if (info
!= *infop
) {
1727 used
= max_zones
* sizeof *info
;
1729 if (used
!= info_size
)
1730 bzero((char *) (info_addr
+ used
), info_size
- used
);
1732 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)info_addr
,
1733 (vm_map_size_t
)info_size
, TRUE
, ©
);
1734 assert(kr
== KERN_SUCCESS
);
1736 *infop
= (zone_info_t
*) copy
;
1738 *infoCntp
= max_zones
;
1740 return KERN_SUCCESS
;
1744 #include <ddb/db_command.h>
1745 #include <ddb/db_output.h>
1746 #include <kern/kern_print.h>
1748 const char *zone_labels
=
1749 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1756 void db_zone_check_active(
1758 void db_zone_print_active(
1760 #endif /* ZONE_DEBUG */
1761 void db_zone_print_free(
1771 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1772 addr
, zcopy
.count
, zcopy
.cur_size
,
1773 zcopy
.max_size
, zcopy
.elem_size
,
1774 zcopy
.alloc_size
, zcopy
.zone_name
);
1775 if (zcopy
.exhaustible
)
1777 if (zcopy
.collectable
)
1779 if (zcopy
.expandable
)
1786 db_show_one_zone(db_expr_t addr
, boolean_t have_addr
,
1787 __unused db_expr_t count
, __unused
char *modif
)
1789 struct zone
*z
= (zone_t
)((char *)0 + addr
);
1791 if (z
== ZONE_NULL
|| !have_addr
){
1792 db_error("No Zone\n");
1796 db_printf("%s\n", zone_labels
);
1802 db_show_all_zones(__unused db_expr_t addr
, boolean_t have_addr
, db_expr_t count
,
1803 __unused
char *modif
)
1809 * Don't risk hanging by unconditionally locking,
1810 * risk of incoherent data is small (zones aren't freed).
1812 have_addr
= simple_lock_try(&all_zones_lock
);
1816 simple_unlock(&all_zones_lock
);
1819 db_printf("%s\n", zone_labels
);
1820 for ( ; count
> 0; count
--) {
1822 db_error("Mangled Zone List\n");
1826 total
+= z
->cur_size
,
1828 have_addr
= simple_lock_try(&all_zones_lock
);
1831 simple_unlock(&all_zones_lock
);
1834 db_printf("\nTotal %8x", total
);
1835 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1840 db_zone_check_active(
1846 if (!zone_debug_enabled(zone
) || !zone_check
)
1848 tmp_elem
= queue_first(&zone
->active_zones
);
1849 while (count
< zone
->count
) {
1851 if (tmp_elem
== 0) {
1852 printf("unexpected zero element, zone=%p, count=%d\n",
1857 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1858 printf("unexpected queue_end, zone=%p, count=%d\n",
1863 tmp_elem
= queue_next(tmp_elem
);
1865 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1866 printf("not at queue_end, zone=%p, tmp_elem=%p\n",
1873 db_zone_print_active(
1879 if (!zone_debug_enabled(zone
)) {
1880 printf("zone %p debug not enabled\n", zone
);
1884 printf("zone_check FALSE\n");
1888 printf("zone %p, active elements %d\n", zone
, zone
->count
);
1889 printf("active list:\n");
1890 tmp_elem
= queue_first(&zone
->active_zones
);
1891 while (count
< zone
->count
) {
1892 printf(" %p", tmp_elem
);
1894 if ((count
% 6) == 0)
1896 if (tmp_elem
== 0) {
1897 printf("\nunexpected zero element, count=%d\n", count
);
1900 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1901 printf("\nunexpected queue_end, count=%d\n", count
);
1904 tmp_elem
= queue_next(tmp_elem
);
1906 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1907 printf("\nnot at queue_end, tmp_elem=%p\n", tmp_elem
);
1911 #endif /* ZONE_DEBUG */
1921 freecount
= zone_free_count(zone
);
1922 printf("zone %p, free elements %d\n", zone
, freecount
);
1923 printf("free list:\n");
1924 elem
= zone
->free_elements
;
1925 while (count
< freecount
) {
1926 printf(" 0x%x", elem
);
1928 if ((count
% 6) == 0)
1931 printf("\nunexpected zero element, count=%d\n", count
);
1934 elem
= *((vm_offset_t
*)elem
);
1937 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1942 #endif /* MACH_KDB */
1947 /* should we care about locks here ? */
1955 char *elt
= (char *)prev
;
1957 if (!zone_debug_enabled(z
))
1959 elt
-= ZONE_DEBUG_OFFSET
;
1960 elt
= (char *) queue_next((queue_t
) elt
);
1961 if ((queue_t
) elt
== &z
->active_zones
)
1963 elt
+= ZONE_DEBUG_OFFSET
;
1973 if (!zone_debug_enabled(z
))
1975 if (queue_empty(&z
->active_zones
))
1977 elt
= (char *)queue_first(&z
->active_zones
);
1978 elt
+= ZONE_DEBUG_OFFSET
;
1983 * Second arg controls how many zone elements are printed:
1986 * n, n > 0 => last n on active list
1995 boolean_t print
= (tail
!= 0);
1999 if (z
->count
< tail
)
2001 tail
= z
->count
- tail
;
2002 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
2003 if (print
&& tail
<= count
)
2004 db_printf("%8x\n", elt
);
2007 assert(count
== z
->count
);
2010 #endif /* MACH_KDB */
2012 #define zone_in_use(z) ( z->count || z->free_elements )
2018 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
2019 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
2021 queue_init(&z
->active_zones
);
2022 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
2029 if (!zone_debug_enabled(z
) || zone_in_use(z
))
2031 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
2032 z
->active_zones
.next
= z
->active_zones
.prev
= NULL
;
2034 #endif /* ZONE_DEBUG */