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>
107 * Zone Corruption Debugging
109 * We provide three methods to detect use of a zone element after it's been freed. These
110 * checks are enabled by specifying "-zc" and/or "-zp" in the boot-args:
112 * (1) Range-check the free-list "next" ptr for sanity.
113 * (2) Store the ptr in two different words, and compare them against
114 * each other when re-using the zone element, to detect modifications.
115 * (3) poison the freed memory by overwriting it with 0xdeadbeef.
117 * The first two checks are farily light weight and are enabled by specifying "-zc"
118 * in the boot-args. If you want more aggressive checking for use-after-free bugs
119 * and you don't mind the additional overhead, then turn on poisoning by adding
120 * "-zp" to the boot-args in addition to "-zc". If you specify -zp without -zc,
121 * it still poisons the memory when it's freed, but doesn't check if the memory
122 * has been altered later when it's reallocated.
125 boolean_t check_freed_element
= FALSE
; /* enabled by -zc in boot-args */
126 boolean_t zfree_clear
= FALSE
; /* enabled by -zp in boot-args */
128 #define is_kernel_data_addr(a) (!(a) || ((a) >= vm_min_kernel_address && !((a) & 0x3)))
130 #define ADD_TO_ZONE(zone, element) \
135 i < zone->elem_size/sizeof(uint32_t); \
137 ((uint32_t *)(element))[i] = 0xdeadbeef; \
139 *((vm_offset_t *)(element)) = (zone)->free_elements; \
140 if (check_freed_element) { \
141 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t))) \
142 ((vm_offset_t *)(element))[((zone)->elem_size/sizeof(vm_offset_t))-1] = \
143 (zone)->free_elements; \
145 (zone)->free_elements = (vm_offset_t) (element); \
149 #define REMOVE_FROM_ZONE(zone, ret, type) \
151 (ret) = (type) (zone)->free_elements; \
152 if ((ret) != (type) 0) { \
153 if (check_freed_element) { \
154 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0]) || \
155 ((zone)->elem_size >= (2 * sizeof(vm_offset_t)) && \
156 ((vm_offset_t *)(ret))[((zone)->elem_size/sizeof(vm_offset_t))-1] != \
157 ((vm_offset_t *)(ret))[0])) \
158 panic("a freed zone element has been modified");\
161 for (ii = sizeof(vm_offset_t) / sizeof(uint32_t); \
162 ii < zone->elem_size/sizeof(uint32_t) - sizeof(vm_offset_t) / sizeof(uint32_t); \
164 if (((uint32_t *)(ret))[ii] != (uint32_t)0xdeadbeef) \
165 panic("a freed zone element has been modified");\
169 (zone)->free_elements = *((vm_offset_t *)(ret)); \
174 #define zone_debug_enabled(z) z->active_zones.next
175 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
176 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
177 #endif /* ZONE_DEBUG */
180 * Support for garbage collection of unused zone pages:
183 struct zone_page_table_entry
{
184 struct zone_page_table_entry
*link
;
195 void zone_page_alloc(
199 void zone_page_free_element(
200 struct zone_page_table_entry
**free_pages
,
204 void zone_page_collect(
208 boolean_t
zone_page_collectable(
217 thread_call_param_t p0
,
218 thread_call_param_t p1
);
221 #if ZONE_DEBUG && MACH_KDB
225 #endif /* ZONE_DEBUG && MACH_KDB */
227 vm_map_t zone_map
= VM_MAP_NULL
;
229 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
232 * The VM system gives us an initial chunk of memory.
233 * It has to be big enough to allocate the zone_zone
237 vm_size_t zdata_size
;
239 #define lock_zone(zone) \
241 lck_mtx_lock(&(zone)->lock); \
244 #define unlock_zone(zone) \
246 lck_mtx_unlock(&(zone)->lock); \
249 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
250 #define zone_sleep(zone) \
251 (void) lck_mtx_sleep(&(zone)->lock, 0, (event_t)(zone), THREAD_UNINT);
254 #define lock_zone_init(zone) \
257 (void) snprintf(_name, sizeof (_name), "zone.%s", (zone)->zone_name); \
258 lck_grp_attr_setdefault(&(zone)->lock_grp_attr); \
259 lck_grp_init(&(zone)->lock_grp, _name, &(zone)->lock_grp_attr); \
260 lck_attr_setdefault(&(zone)->lock_attr); \
261 lck_mtx_init_ext(&(zone)->lock, &(zone)->lock_ext, \
262 &(zone)->lock_grp, &(zone)->lock_attr); \
265 #define lock_try_zone(zone) lck_mtx_try_lock(&zone->lock)
267 kern_return_t
zget_space(
269 vm_offset_t
*result
);
271 decl_simple_lock_data(,zget_space_lock
)
272 vm_offset_t zalloc_next_space
;
273 vm_offset_t zalloc_end_of_space
;
274 vm_size_t zalloc_wasted_space
;
277 * Garbage collection map information
279 struct zone_page_table_entry
* zone_page_table
;
280 vm_offset_t zone_map_min_address
;
281 vm_offset_t zone_map_max_address
;
282 unsigned int zone_pages
;
285 * Exclude more than one concurrent garbage collection
287 decl_mutex_data(, zone_gc_lock
)
290 #define from_zone_map(addr, size) \
291 ((vm_offset_t)(addr) >= zone_map_min_address && \
292 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
294 #define from_zone_map(addr, size) \
295 ((vm_offset_t)(zone_virtual_addr((vm_map_address_t)addr)) >= zone_map_min_address && \
296 ((vm_offset_t)(zone_virtual_addr((vm_map_address_t)addr)) + size -1) < zone_map_max_address)
299 #define ZONE_PAGE_USED 0
300 #define ZONE_PAGE_UNUSED -1
304 * Protects first_zone, last_zone, num_zones,
305 * and the next_zone field of zones.
307 decl_simple_lock_data(, all_zones_lock
)
310 unsigned int num_zones
;
312 boolean_t zone_gc_allowed
= TRUE
;
313 boolean_t zone_gc_forced
= FALSE
;
314 boolean_t panic_include_zprint
= FALSE
;
315 unsigned zone_gc_last_tick
= 0;
316 unsigned zone_gc_max_rate
= 0; /* in ticks */
319 * Zone leak debugging code
321 * When enabled, this code keeps a log to track allocations to a particular zone that have not
322 * yet been freed. Examining this log will reveal the source of a zone leak. The log is allocated
323 * only when logging is enabled, so there is no effect on the system when it's turned off. Logging is
326 * Enable the logging via the boot-args. Add the parameter "zlog=<zone>" to boot-args where <zone>
327 * is the name of the zone you wish to log.
329 * This code only tracks one zone, so you need to identify which one is leaking first.
330 * Generally, you'll know you have a leak when you get a "zalloc retry failed 3" panic from the zone
331 * garbage collector. Note that the zone name printed in the panic message is not necessarily the one
332 * containing the leak. So do a zprint from gdb and locate the zone with the bloated size. This
333 * is most likely the problem zone, so set zlog in boot-args to this zone name, reboot and re-run the test. The
334 * next time it panics with this message, examine the log using the kgmacros zstack, findoldest and countpcs.
335 * See the help in the kgmacros for usage info.
338 * Zone corruption logging
340 * Logging can also be used to help identify the source of a zone corruption. First, identify the zone
341 * that is being corrupted, then add "-zc zlog=<zone name>" to the boot-args. When -zc is used in conjunction
342 * with zlog, it changes the logging style to track both allocations and frees to the zone. So when the
343 * corruption is detected, examining the log will show you the stack traces of the callers who last allocated
344 * and freed any particular element in the zone. Use the findelem kgmacro with the address of the element that's been
345 * corrupted to examine its history. This should lead to the source of the corruption.
348 static int log_records
; /* size of the log, expressed in number of records */
350 #define MAX_ZONE_NAME 32 /* max length of a zone name we can take from the boot-args */
352 static char zone_name_to_log
[MAX_ZONE_NAME
] = ""; /* the zone name we're logging, if any */
355 * The number of records in the log is configurable via the zrecs parameter in boot-args. Set this to
356 * the number of records you want in the log. For example, "zrecs=1000" sets it to 1000 records. Note
357 * that the larger the size of the log, the slower the system will run due to linear searching in the log,
358 * but one doesn't generally care about performance when tracking down a leak. The log is capped at 8000
359 * records since going much larger than this tends to make the system unresponsive and unbootable on small
360 * memory configurations. The default value is 4000 records.
362 * MAX_DEPTH configures how deep of a stack trace is taken on each zalloc in the zone of interrest. 15
363 * levels is usually enough to get past all the layers of code in kalloc and IOKit and see who the actual
364 * caller is up above these lower levels.
367 #define ZRECORDS_MAX 8000 /* Max records allowed in the log */
368 #define ZRECORDS_DEFAULT 4000 /* default records in log if zrecs is not specificed in boot-args */
369 #define MAX_DEPTH 15 /* number of levels of the stack trace to record */
372 * Each record in the log contains a pointer to the zone element it refers to, a "time" number that allows
373 * the records to be ordered chronologically, and a small array to hold the pc's from the stack trace. A
374 * record is added to the log each time a zalloc() is done in the zone_of_interest. For leak debugging,
375 * the record is cleared when a zfree() is done. For corruption debugging, the log tracks both allocs and frees.
376 * If the log fills, old records are replaced as if it were a circular buffer.
380 void *z_element
; /* the element that was zalloc'ed of zfree'ed */
381 uint32_t z_opcode
:1, /* whether it was a zalloc or zfree */
382 z_time
:31; /* time index when operation was done */
383 void *z_pc
[MAX_DEPTH
]; /* stack trace of caller */
387 * Opcodes for the z_opcode field:
394 * The allocation log and all the related variables are protected by the zone lock for the zone_of_interest
397 static struct zrecord
*zrecords
; /* the log itself, dynamically allocated when logging is enabled */
398 static int zcurrent
= 0; /* index of the next slot in the log to use */
399 static int zrecorded
= 0; /* number of allocations recorded in the log */
400 static unsigned int ztime
= 0; /* a timestamp of sorts */
401 static zone_t zone_of_interest
= NULL
; /* the zone being watched; corresponds to zone_name_to_log */
404 * Decide if we want to log this zone by doing a string compare between a zone name and the name
405 * of the zone to log. Return true if the strings are equal, false otherwise. Because it's not
406 * possible to include spaces in strings passed in via the boot-args, a period in the logname will
407 * match a space in the zone name.
411 log_this_zone(const char *zonename
, const char *logname
)
414 const char *zc
= zonename
;
415 const char *lc
= logname
;
418 * Compare the strings. We bound the compare by MAX_ZONE_NAME.
421 for (len
= 1; len
<= MAX_ZONE_NAME
; zc
++, lc
++, len
++) {
424 * If the current characters don't match, check for a space in
425 * in the zone name and a corresponding period in the log name.
426 * If that's not there, then the strings don't match.
429 if (*zc
!= *lc
&& !(*zc
== ' ' && *lc
== '.'))
433 * The strings are equal so far. If we're at the end, then it's a match.
445 * Test if we want to log this zalloc/zfree event. We log if this is the zone we're interested in and
446 * the buffer for the records has been allocated.
449 #define DO_LOGGING(z) (zrecords && (z) == zone_of_interest)
451 extern boolean_t zlog_ready
;
455 * zinit initializes a new zone. The zone data structures themselves
456 * are stored in a zone, which is initially a static structure that
457 * is initialized by zone_init.
461 vm_size_t size
, /* the size of an element */
462 vm_size_t max
, /* maximum memory to use */
463 vm_size_t alloc
, /* allocation size */
464 const char *name
) /* a name for the zone */
468 if (zone_zone
== ZONE_NULL
) {
469 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
473 z
= (zone_t
) zalloc(zone_zone
);
478 * Round off all the parameters appropriately.
480 if (size
< sizeof(z
->free_elements
))
481 size
= sizeof(z
->free_elements
);
482 size
= ((size
-1) + sizeof(z
->free_elements
)) -
483 ((size
-1) % sizeof(z
->free_elements
));
486 alloc
= round_page(alloc
);
487 max
= round_page(max
);
489 * we look for an allocation size with less than 1% waste
490 * up to 5 pages in size...
491 * otherwise, we look for an allocation size with least fragmentation
492 * in the range of 1 - 5 pages
493 * This size will be used unless
494 * the user suggestion is larger AND has less fragmentation
497 if ((size
< PAGE_SIZE
) && (PAGE_SIZE
% size
<= PAGE_SIZE
/ 10))
501 { vm_size_t best
, waste
; unsigned int i
;
505 for (i
= 1; i
<= 5; i
++) {
506 vm_size_t tsize
, twaste
;
508 tsize
= i
* PAGE_SIZE
;
510 if ((tsize
% size
) < (tsize
/ 100)) {
512 goto use_this_allocation
;
514 twaste
= tsize
% size
;
516 best
= tsize
, waste
= twaste
;
518 if (alloc
<= best
|| (alloc
% size
>= waste
))
522 if (max
&& (max
< alloc
))
525 z
->free_elements
= 0;
529 z
->alloc_size
= alloc
;
532 z
->doing_alloc
= FALSE
;
534 z
->exhaustible
= FALSE
;
535 z
->collectable
= TRUE
;
536 z
->allows_foreign
= FALSE
;
537 z
->expandable
= TRUE
;
539 z
->async_pending
= FALSE
;
542 z
->active_zones
.next
= z
->active_zones
.prev
= NULL
;
543 zone_debug_enable(z
);
544 #endif /* ZONE_DEBUG */
548 * Add the zone to the all-zones list.
551 z
->next_zone
= ZONE_NULL
;
552 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
553 simple_lock(&all_zones_lock
);
555 last_zone
= &z
->next_zone
;
557 simple_unlock(&all_zones_lock
);
560 * Check if we should be logging this zone. If so, remember the zone pointer.
563 if (log_this_zone(z
->zone_name
, zone_name_to_log
)) {
564 zone_of_interest
= z
;
568 * If we want to log a zone, see if we need to allocate buffer space for the log. Some vm related zones are
569 * zinit'ed before we can do a kmem_alloc, so we have to defer allocation in that case. zlog_ready is set to
570 * TRUE once enough of the VM system is up and running to allow a kmem_alloc to work. If we want to log one
571 * of the VM related zones that's set up early on, we will skip allocation of the log until zinit is called again
572 * later on some other zone. So note we may be allocating a buffer to log a zone other than the one being initialized
576 if (zone_of_interest
!= NULL
&& zrecords
== NULL
&& zlog_ready
) {
577 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&zrecords
, log_records
* sizeof(struct zrecord
)) == KERN_SUCCESS
) {
580 * We got the memory for the log. Zero it out since the code needs this to identify unused records.
581 * At this point, everything is set up and we're ready to start logging this zone.
584 bzero((void *)zrecords
, log_records
* sizeof(struct zrecord
));
585 printf("zone: logging started for zone %s (%p)\n", zone_of_interest
->zone_name
, zone_of_interest
);
588 printf("zone: couldn't allocate memory for zrecords, turning off zleak logging\n");
589 zone_of_interest
= NULL
;
597 * Cram the given memory into the specified zone.
601 register zone_t zone
,
605 register vm_size_t elem_size
;
606 vm_offset_t newmem
= (vm_offset_t
) newaddr
;
608 /* Basic sanity checks */
609 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
610 assert(!zone
->collectable
|| zone
->allows_foreign
611 || (from_zone_map(newmem
, size
)));
613 elem_size
= zone
->elem_size
;
616 while (size
>= elem_size
) {
617 ADD_TO_ZONE(zone
, newmem
);
618 if (from_zone_map(newmem
, elem_size
))
619 zone_page_alloc(newmem
, elem_size
);
620 zone
->count
++; /* compensate for ADD_TO_ZONE */
623 zone
->cur_size
+= elem_size
;
629 * Contiguous space allocator for non-paged zones. Allocates "size" amount
630 * of memory from zone_map.
638 vm_offset_t new_space
= 0;
639 vm_size_t space_to_add
= 0;
641 simple_lock(&zget_space_lock
);
642 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
644 * Add at least one page to allocation area.
647 space_to_add
= round_page(size
);
649 if (new_space
== 0) {
650 kern_return_t retval
;
652 * Memory cannot be wired down while holding
653 * any locks that the pageout daemon might
654 * need to free up pages. [Making the zget_space
655 * lock a complex lock does not help in this
658 * Unlock and allocate memory. Because several
659 * threads might try to do this at once, don't
660 * use the memory before checking for available
664 simple_unlock(&zget_space_lock
);
666 retval
= kernel_memory_allocate(zone_map
, &new_space
,
667 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
668 if (retval
!= KERN_SUCCESS
)
671 if (space_to_add
== PAGE_SIZE
)
672 new_space
= zone_alias_addr(new_space
);
674 zone_page_init(new_space
, space_to_add
,
676 simple_lock(&zget_space_lock
);
682 * Memory was allocated in a previous iteration.
684 * Check whether the new region is contiguous
688 if (new_space
!= zalloc_end_of_space
) {
690 * Throw away the remainder of the
691 * old space, and start a new one.
693 zalloc_wasted_space
+=
694 zalloc_end_of_space
- zalloc_next_space
;
695 zalloc_next_space
= new_space
;
698 zalloc_end_of_space
= new_space
+ space_to_add
;
702 *result
= zalloc_next_space
;
703 zalloc_next_space
+= size
;
704 simple_unlock(&zget_space_lock
);
707 kmem_free(zone_map
, new_space
, space_to_add
);
709 return(KERN_SUCCESS
);
714 * Steal memory for the zone package. Called from
715 * vm_page_bootstrap().
718 zone_steal_memory(void)
720 zdata_size
= round_page(128*sizeof(struct zone
));
721 zdata
= (vm_offset_t
)((char *)pmap_steal_memory(zdata_size
) - (char *)0);
726 * Fill a zone with enough memory to contain at least nelem elements.
727 * Memory is obtained with kmem_alloc_wired from the kernel_map.
728 * Return the number of elements actually put into the zone, which may
729 * be more than the caller asked for since the memory allocation is
730 * rounded up to a full page.
745 size
= nelem
* zone
->elem_size
;
746 size
= round_page(size
);
747 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
748 if (kr
!= KERN_SUCCESS
)
751 zone_change(zone
, Z_FOREIGN
, TRUE
);
752 zcram(zone
, (void *)memory
, size
);
753 nalloc
= size
/ zone
->elem_size
;
754 assert(nalloc
>= nelem
);
760 * Initialize the "zone of zones" which uses fixed memory allocated
761 * earlier in memory initialization. zone_bootstrap is called
767 vm_size_t zone_zone_size
;
768 vm_offset_t zone_zone_space
;
771 /* see if we want freed zone element checking and/or poisoning */
772 if (PE_parse_boot_argn("-zc", temp_buf
, sizeof (temp_buf
))) {
773 check_freed_element
= TRUE
;
776 if (PE_parse_boot_argn("-zp", temp_buf
, sizeof (temp_buf
))) {
781 * Check for and set up zone leak detection if requested via boot-args. We recognized two
785 * zrecs=<num_records_in_log>
787 * The zlog arg is used to specify the zone name that should be logged, and zrecs is used to
788 * control the size of the log. If zrecs is not specified, a default value is used.
791 if (PE_parse_boot_argn("zlog", zone_name_to_log
, sizeof(zone_name_to_log
)) == TRUE
) {
792 if (PE_parse_boot_argn("zrecs", &log_records
, sizeof(log_records
)) == TRUE
) {
795 * Don't allow more than ZRECORDS_MAX records even if the user asked for more.
796 * This prevents accidentally hogging too much kernel memory and making the system
800 log_records
= MIN(ZRECORDS_MAX
, log_records
);
803 log_records
= ZRECORDS_DEFAULT
;
807 simple_lock_init(&all_zones_lock
, 0);
809 first_zone
= ZONE_NULL
;
810 last_zone
= &first_zone
;
813 simple_lock_init(&zget_space_lock
, 0);
814 zalloc_next_space
= zdata
;
815 zalloc_end_of_space
= zdata
+ zdata_size
;
816 zalloc_wasted_space
= 0;
818 /* assertion: nobody else called zinit before us */
819 assert(zone_zone
== ZONE_NULL
);
820 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
821 sizeof(struct zone
), "zones");
822 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
823 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
824 zget_space(zone_zone_size
, &zone_zone_space
);
825 zcram(zone_zone
, (void *)zone_zone_space
, zone_zone_size
);
830 vm_size_t max_zonemap_size
)
832 kern_return_t retval
;
833 vm_offset_t zone_min
;
834 vm_offset_t zone_max
;
835 vm_size_t zone_table_size
;
837 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
838 FALSE
, VM_FLAGS_ANYWHERE
, &zone_map
);
840 if (retval
!= KERN_SUCCESS
)
841 panic("zone_init: kmem_suballoc failed");
842 zone_max
= zone_min
+ round_page(max_zonemap_size
);
844 * Setup garbage collection information:
846 zone_table_size
= atop_32(zone_max
- zone_min
) *
847 sizeof(struct zone_page_table_entry
);
848 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
849 zone_table_size
) != KERN_SUCCESS
)
851 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
852 zone_pages
= atop_32(zone_max
- zone_min
);
853 zone_map_min_address
= zone_min
;
854 zone_map_max_address
= zone_max
;
855 mutex_init(&zone_gc_lock
, 0);
856 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
861 * zalloc returns an element from the specified zone.
865 register zone_t zone
,
869 kern_return_t retval
;
870 void *bt
[MAX_DEPTH
]; /* only used if zone logging is enabled */
874 assert(zone
!= ZONE_NULL
);
877 * If zone logging is turned on and this is the zone we're tracking, grab a backtrace.
880 if (DO_LOGGING(zone
))
881 numsaved
= OSBacktrace(&bt
[0], MAX_DEPTH
);
885 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
887 while ((addr
== 0) && canblock
&& (zone
->doing_gc
)) {
888 zone
->waiting
= TRUE
;
890 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
893 while ((addr
== 0) && canblock
) {
895 * If nothing was there, try to get more
897 if (zone
->doing_alloc
) {
899 * Someone is allocating memory for this zone.
900 * Wait for it to show up, then try again.
902 zone
->waiting
= TRUE
;
906 if ((zone
->cur_size
+ zone
->elem_size
) >
908 if (zone
->exhaustible
)
910 if (zone
->expandable
) {
912 * We're willing to overflow certain
913 * zones, but not without complaining.
915 * This is best used in conjunction
916 * with the collectable flag. What we
917 * want is an assurance we can get the
918 * memory back, assuming there's no
921 zone
->max_size
+= (zone
->max_size
>> 1);
925 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
928 zone
->doing_alloc
= TRUE
;
931 if (zone
->collectable
) {
933 vm_size_t alloc_size
;
938 if (vm_pool_low() || retry
>= 1)
940 round_page(zone
->elem_size
);
942 alloc_size
= zone
->alloc_size
;
944 retval
= kernel_memory_allocate(zone_map
,
945 &space
, alloc_size
, 0,
946 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
947 if (retval
== KERN_SUCCESS
) {
949 if (alloc_size
== PAGE_SIZE
)
950 space
= zone_alias_addr(space
);
952 zone_page_init(space
, alloc_size
,
954 zcram(zone
, (void *)space
, alloc_size
);
957 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
962 printf("zalloc did gc\n");
965 panic_include_zprint
= TRUE
;
966 panic("zalloc: \"%s\" (%d elements) retry fail %d", zone
->zone_name
, zone
->count
, retval
);
973 zone
->doing_alloc
= FALSE
;
975 zone
->waiting
= FALSE
;
978 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
980 retval
== KERN_RESOURCE_SHORTAGE
) {
988 retval
= zget_space(zone
->elem_size
, &space
);
991 zone
->doing_alloc
= FALSE
;
993 zone
->waiting
= FALSE
;
994 thread_wakeup((event_t
)zone
);
996 if (retval
== KERN_SUCCESS
) {
998 zone
->cur_size
+= zone
->elem_size
;
1000 if (zone_debug_enabled(zone
)) {
1001 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
1005 zone_page_alloc(space
, zone
->elem_size
);
1007 if (zone_debug_enabled(zone
))
1008 space
+= ZONE_DEBUG_OFFSET
;
1013 if (retval
== KERN_RESOURCE_SHORTAGE
) {
1019 panic("zalloc: \"%s\" (%d elements) zget_space returned %d", zone
->zone_name
, zone
->count
, retval
);
1024 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
1028 * See if we should be logging allocations in this zone. Logging is rarely done except when a leak is
1029 * suspected, so this code rarely executes. We need to do this code while still holding the zone lock
1030 * since it protects the various log related data structures.
1033 if (DO_LOGGING(zone
) && addr
) {
1036 * Look for a place to record this new allocation. We implement two different logging strategies
1037 * depending on whether we're looking for the source of a zone leak or a zone corruption. When looking
1038 * for a leak, we want to log as many allocations as possible in order to clearly identify the leaker
1039 * among all the records. So we look for an unused slot in the log and fill that in before overwriting
1040 * an old entry. When looking for a corrution however, it's better to have a chronological log of all
1041 * the allocations and frees done in the zone so that the history of operations for a specific zone
1042 * element can be inspected. So in this case, we treat the log as a circular buffer and overwrite the
1043 * oldest entry whenever a new one needs to be added.
1045 * The check_freed_element flag tells us what style of logging to do. It's set if we're supposed to be
1046 * doing corruption style logging (indicated via -zc in the boot-args).
1049 if (!check_freed_element
&& zrecords
[zcurrent
].z_element
&& zrecorded
< log_records
) {
1052 * If we get here, we're doing leak style logging and there's still some unused entries in
1053 * the log (since zrecorded is smaller than the size of the log). Look for an unused slot
1054 * starting at zcurrent and wrap-around if we reach the end of the buffer. If the buffer
1055 * is already full, we just fall through and overwrite the element indexed by zcurrent.
1058 for (i
= zcurrent
; i
< log_records
; i
++) {
1059 if (zrecords
[i
].z_element
== NULL
) {
1065 for (i
= 0; i
< zcurrent
; i
++) {
1066 if (zrecords
[i
].z_element
== NULL
) {
1074 * Save a record of this allocation
1078 if (zrecords
[zcurrent
].z_element
== NULL
)
1081 zrecords
[zcurrent
].z_element
= (void *)addr
;
1082 zrecords
[zcurrent
].z_time
= ztime
++;
1083 zrecords
[zcurrent
].z_opcode
= ZOP_ALLOC
;
1085 for (i
= 0; i
< numsaved
; i
++)
1086 zrecords
[zcurrent
].z_pc
[i
] = bt
[i
];
1088 for (; i
< MAX_DEPTH
; i
++)
1089 zrecords
[zcurrent
].z_pc
[i
] = 0;
1093 if (zcurrent
>= log_records
)
1097 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (zone
->exhaustible
== FALSE
) && (!vm_pool_low())) {
1098 zone
->async_pending
= TRUE
;
1100 thread_call_enter(&zone
->call_async_alloc
);
1102 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
1106 if (addr
&& zone_debug_enabled(zone
)) {
1107 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
1108 addr
+= ZONE_DEBUG_OFFSET
;
1115 TRACE_MACHLEAKS(ZALLOC_CODE
, ZALLOC_CODE_2
, zone
->elem_size
, addr
);
1117 return((void *)addr
);
1123 register zone_t zone
)
1125 return( zalloc_canblock(zone
, TRUE
) );
1130 register zone_t zone
)
1132 return( zalloc_canblock(zone
, FALSE
) );
1137 thread_call_param_t p0
,
1138 __unused thread_call_param_t p1
)
1142 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
1143 zfree((zone_t
)p0
, elt
);
1144 lock_zone(((zone_t
)p0
));
1145 ((zone_t
)p0
)->async_pending
= FALSE
;
1146 unlock_zone(((zone_t
)p0
));
1151 * zget returns an element from the specified zone
1152 * and immediately returns nothing if there is nothing there.
1154 * This form should be used when you can not block (like when
1155 * processing an interrupt).
1159 register zone_t zone
)
1161 register vm_offset_t addr
;
1163 assert( zone
!= ZONE_NULL
);
1165 if (!lock_try_zone(zone
))
1168 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
1170 if (addr
&& zone_debug_enabled(zone
)) {
1171 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
1172 addr
+= ZONE_DEBUG_OFFSET
;
1174 #endif /* ZONE_DEBUG */
1177 return((void *) addr
);
1180 /* Keep this FALSE by default. Large memory machine run orders of magnitude
1181 slower in debug mode when true. Use debugger to enable if needed */
1182 /* static */ boolean_t zone_check
= FALSE
;
1184 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
1185 static vm_offset_t zone_last_bogus_elem
= 0;
1189 register zone_t zone
,
1192 vm_offset_t elem
= (vm_offset_t
) addr
;
1193 void *bt
[MAX_DEPTH
]; /* only used if zone logging is enable via boot-args */
1196 assert(zone
!= ZONE_NULL
);
1199 * If zone logging is turned on and this is the zone we're tracking, grab a backtrace.
1202 if (DO_LOGGING(zone
))
1203 numsaved
= OSBacktrace(&bt
[0], MAX_DEPTH
);
1206 /* Basic sanity checks */
1207 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
1208 panic("zfree: NULL");
1209 /* zone_gc assumes zones are never freed */
1210 if (zone
== zone_zone
)
1211 panic("zfree: freeing to zone_zone breaks zone_gc!");
1214 TRACE_MACHLEAKS(ZFREE_CODE
, ZFREE_CODE_2
, zone
->elem_size
, (int)addr
);
1216 if (zone
->collectable
&& !zone
->allows_foreign
&&
1217 !from_zone_map(elem
, zone
->elem_size
)) {
1219 panic("zfree: non-allocated memory in collectable zone!");
1221 zone_last_bogus_zone
= zone
;
1222 zone_last_bogus_elem
= elem
;
1229 * See if we're doing logging on this zone. There are two styles of logging used depending on
1230 * whether we're trying to catch a leak or corruption. See comments above in zalloc for details.
1233 if (DO_LOGGING(zone
)) {
1236 if (check_freed_element
) {
1239 * We're logging to catch a corruption. Add a record of this zfree operation
1243 if (zrecords
[zcurrent
].z_element
== NULL
)
1246 zrecords
[zcurrent
].z_element
= (void *)addr
;
1247 zrecords
[zcurrent
].z_time
= ztime
++;
1248 zrecords
[zcurrent
].z_opcode
= ZOP_FREE
;
1250 for (i
= 0; i
< numsaved
; i
++)
1251 zrecords
[zcurrent
].z_pc
[i
] = bt
[i
];
1253 for (; i
< MAX_DEPTH
; i
++)
1254 zrecords
[zcurrent
].z_pc
[i
] = 0;
1258 if (zcurrent
>= log_records
)
1264 * We're logging to catch a leak. Remove any record we might have for this
1265 * element since it's being freed. Note that we may not find it if the buffer
1266 * overflowed and that's OK. Since the log is of a limited size, old records
1267 * get overwritten if there are more zallocs than zfrees.
1270 for (i
= 0; i
< log_records
; i
++) {
1271 if (zrecords
[i
].z_element
== addr
) {
1272 zrecords
[i
].z_element
= NULL
;
1283 if (zone_debug_enabled(zone
)) {
1286 elem
-= ZONE_DEBUG_OFFSET
;
1288 /* check the zone's consistency */
1290 for (tmp_elem
= queue_first(&zone
->active_zones
);
1291 !queue_end(tmp_elem
, &zone
->active_zones
);
1292 tmp_elem
= queue_next(tmp_elem
))
1293 if (elem
== (vm_offset_t
)tmp_elem
)
1295 if (elem
!= (vm_offset_t
)tmp_elem
)
1296 panic("zfree()ing element from wrong zone");
1298 remqueue(&zone
->active_zones
, (queue_t
) elem
);
1300 #endif /* ZONE_DEBUG */
1304 /* check the zone's consistency */
1306 for (this = zone
->free_elements
;
1308 this = * (vm_offset_t
*) this)
1309 if (!pmap_kernel_va(this) || this == elem
)
1312 ADD_TO_ZONE(zone
, elem
);
1315 * If elements have one or more pages, and memory is low,
1316 * request to run the garbage collection in the zone the next
1317 * time the pageout thread runs.
1319 if (zone
->elem_size
>= PAGE_SIZE
&&
1321 zone_gc_forced
= TRUE
;
1327 /* Change a zone's flags.
1328 * This routine must be called immediately after zinit.
1336 assert( zone
!= ZONE_NULL
);
1337 assert( value
== TRUE
|| value
== FALSE
);
1341 zone
->exhaustible
= value
;
1344 zone
->collectable
= value
;
1347 zone
->expandable
= value
;
1350 zone
->allows_foreign
= value
;
1354 panic("Zone_change: Wrong Item Type!");
1361 * Return the expected number of free elements in the zone.
1362 * This calculation will be incorrect if items are zfree'd that
1363 * were never zalloc'd/zget'd. The correct way to stuff memory
1364 * into a zone is by zcram.
1368 zone_free_count(zone_t zone
)
1370 integer_t free_count
;
1373 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
1376 assert(free_count
>= 0);
1382 * zprealloc preallocates wired memory, exanding the specified
1383 * zone to the specified size
1393 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
1395 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
1396 zcram(zone
, (void *)addr
, size
);
1401 * Zone garbage collection subroutines
1405 zone_page_collectable(
1409 struct zone_page_table_entry
*zp
;
1413 addr
= zone_virtual_addr(addr
);
1416 if (!from_zone_map(addr
, size
))
1417 panic("zone_page_collectable");
1420 i
= atop_32(addr
-zone_map_min_address
);
1421 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1423 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1424 if (zp
->collect_count
== zp
->alloc_count
)
1435 struct zone_page_table_entry
*zp
;
1439 addr
= zone_virtual_addr(addr
);
1442 if (!from_zone_map(addr
, size
))
1443 panic("zone_page_keep");
1446 i
= atop_32(addr
-zone_map_min_address
);
1447 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1449 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1450 zp
->collect_count
= 0;
1458 struct zone_page_table_entry
*zp
;
1462 addr
= zone_virtual_addr(addr
);
1465 if (!from_zone_map(addr
, size
))
1466 panic("zone_page_collect");
1469 i
= atop_32(addr
-zone_map_min_address
);
1470 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1472 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1473 ++zp
->collect_count
;
1482 struct zone_page_table_entry
*zp
;
1486 addr
= zone_virtual_addr(addr
);
1489 if (!from_zone_map(addr
, size
))
1490 panic("zone_page_init");
1493 i
= atop_32(addr
-zone_map_min_address
);
1494 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1496 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1497 zp
->alloc_count
= value
;
1498 zp
->collect_count
= 0;
1507 struct zone_page_table_entry
*zp
;
1511 addr
= zone_virtual_addr(addr
);
1514 if (!from_zone_map(addr
, size
))
1515 panic("zone_page_alloc");
1518 i
= atop_32(addr
-zone_map_min_address
);
1519 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1521 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1523 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1524 * it was previously set to ZONE_PAGE_UNUSED.
1526 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1527 zp
->alloc_count
= 1;
1534 zone_page_free_element(
1535 struct zone_page_table_entry
**free_pages
,
1539 struct zone_page_table_entry
*zp
;
1543 addr
= zone_virtual_addr(addr
);
1546 if (!from_zone_map(addr
, size
))
1547 panic("zone_page_free_element");
1550 i
= atop_32(addr
-zone_map_min_address
);
1551 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1553 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1554 if (zp
->collect_count
> 0)
1555 --zp
->collect_count
;
1556 if (--zp
->alloc_count
== 0) {
1557 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1558 zp
->collect_count
= 0;
1560 zp
->link
= *free_pages
;
1567 /* This is used for walking through a zone's free element list.
1569 struct zone_free_element
{
1570 struct zone_free_element
* next
;
1574 * Add a linked list of pages starting at base back into the zone
1575 * free list. Tail points to the last element on the list.
1578 #define ADD_LIST_TO_ZONE(zone, base, tail) \
1580 (tail)->next = (void *)((zone)->free_elements); \
1581 if (check_freed_element) { \
1582 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t))) \
1583 ((vm_offset_t *)(tail))[((zone)->elem_size/sizeof(vm_offset_t))-1] = \
1584 (zone)->free_elements; \
1586 (zone)->free_elements = (unsigned long)(base); \
1590 * Add an element to the chain pointed to by prev.
1593 #define ADD_ELEMENT(zone, prev, elem) \
1595 (prev)->next = (elem); \
1596 if (check_freed_element) { \
1597 if ((zone)->elem_size >= (2 * sizeof(vm_offset_t))) \
1598 ((vm_offset_t *)(prev))[((zone)->elem_size/sizeof(vm_offset_t))-1] = \
1599 (vm_offset_t)(elem); \
1606 uint32_t elems_collected
,
1611 /* Zone garbage collection
1613 * zone_gc will walk through all the free elements in all the
1614 * zones that are marked collectable looking for reclaimable
1615 * pages. zone_gc is called by consider_zone_gc when the system
1616 * begins to run out of memory.
1621 unsigned int max_zones
;
1624 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1626 mutex_lock(&zone_gc_lock
);
1628 simple_lock(&all_zones_lock
);
1629 max_zones
= num_zones
;
1631 simple_unlock(&all_zones_lock
);
1634 for (i
= 0; i
< zone_pages
; i
++)
1635 assert(zone_page_table
[i
].collect_count
== 0);
1636 #endif /* MACH_ASSERT */
1638 zone_free_pages
= NULL
;
1640 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1642 vm_size_t elt_size
, size_freed
;
1643 struct zone_free_element
*elt
, *base_elt
, *base_prev
, *prev
, *scan
, *keep
, *tail
;
1645 assert(z
!= ZONE_NULL
);
1647 if (!z
->collectable
)
1652 elt_size
= z
->elem_size
;
1655 * Do a quick feasability check before we scan the zone:
1656 * skip unless there is likelihood of getting pages back
1657 * (i.e we need a whole allocation block's worth of free
1658 * elements before we can garbage collect) and
1659 * the zone has more than 10 percent of it's elements free
1660 * or the element size is a multiple of the PAGE_SIZE
1662 if ((elt_size
& PAGE_MASK
) &&
1663 (((z
->cur_size
- z
->count
* elt_size
) <= (2 * z
->alloc_size
)) ||
1664 ((z
->cur_size
- z
->count
* elt_size
) <= (z
->cur_size
/ 10)))) {
1672 * Snatch all of the free elements away from the zone.
1675 scan
= (void *)z
->free_elements
;
1676 z
->free_elements
= 0;
1683 * Determine which elements we can attempt to collect
1684 * and count them up in the page table. Foreign elements
1685 * are returned to the zone.
1688 prev
= (void *)&scan
;
1690 n
= 0; tail
= keep
= NULL
;
1691 while (elt
!= NULL
) {
1692 if (from_zone_map(elt
, elt_size
)) {
1693 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1698 ++zgc_stats
.elems_collected
;
1704 ADD_ELEMENT(z
, tail
, elt
);
1708 ADD_ELEMENT(z
, prev
, elt
->next
);
1710 ADD_ELEMENT(z
, tail
, NULL
);
1714 * Dribble back the elements we are keeping.
1718 if (z
->waiting
== TRUE
) {
1722 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1728 while ((elt
!= NULL
) && (++m
< 50)) {
1733 ADD_LIST_TO_ZONE(z
, base_elt
, prev
);
1734 ADD_ELEMENT(z
, base_prev
, elt
);
1751 * Return any remaining elements.
1757 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1765 * Determine which pages we can reclaim and
1766 * free those elements.
1771 n
= 0; tail
= keep
= NULL
;
1772 while (elt
!= NULL
) {
1773 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1774 size_freed
+= elt_size
;
1775 zone_page_free_element(&zone_free_pages
,
1776 (vm_offset_t
)elt
, elt_size
);
1780 ++zgc_stats
.elems_freed
;
1783 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1788 ADD_ELEMENT(z
, tail
, elt
);
1793 ADD_ELEMENT(z
, tail
, NULL
);
1795 ++zgc_stats
.elems_kept
;
1799 * Dribble back the elements we are keeping,
1800 * and update the zone size info.
1806 z
->cur_size
-= size_freed
;
1810 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1820 n
= 0; tail
= keep
= NULL
;
1825 * Return any remaining elements, and update
1826 * the zone size info.
1831 if (size_freed
> 0 || keep
!= NULL
) {
1833 z
->cur_size
-= size_freed
;
1836 ADD_LIST_TO_ZONE(z
, keep
, tail
);
1841 z
->doing_gc
= FALSE
;
1850 * Reclaim the pages we are freeing.
1853 while ((zp
= zone_free_pages
) != NULL
) {
1854 zone_free_pages
= zp
->link
;
1856 z
= zone_virtual_addr((vm_map_address_t
)z
);
1858 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1859 (zp
- zone_page_table
), PAGE_SIZE
);
1860 ++zgc_stats
.pgs_freed
;
1863 mutex_unlock(&zone_gc_lock
);
1869 * Called by the pageout daemon when the system needs more free pages.
1873 consider_zone_gc(void)
1876 * By default, don't attempt zone GC more frequently
1877 * than once / 1 minutes.
1880 if (zone_gc_max_rate
== 0)
1881 zone_gc_max_rate
= (60 << SCHED_TICK_SHIFT
) + 1;
1883 if (zone_gc_allowed
&&
1884 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1886 zone_gc_forced
= FALSE
;
1887 zone_gc_last_tick
= sched_tick
;
1892 struct fake_zone_info
{
1894 void (*func
)(int *, vm_size_t
*, vm_size_t
*, vm_size_t
*, vm_size_t
*,
1898 static struct fake_zone_info fake_zones
[] = {
1900 .name
= "kernel_stacks",
1901 .func
= stack_fake_zone_info
,
1905 .name
= "save_areas",
1906 .func
= save_fake_zone_info
,
1909 .name
= "pmap_mappings",
1910 .func
= mapping_fake_zone_info
,
1915 .name
= "page_tables",
1916 .func
= pt_fake_zone_info
,
1920 .name
= "kalloc.large",
1921 .func
= kalloc_fake_zone_info
,
1928 zone_name_array_t
*namesp
,
1929 mach_msg_type_number_t
*namesCntp
,
1930 zone_info_array_t
*infop
,
1931 mach_msg_type_number_t
*infoCntp
)
1934 vm_offset_t names_addr
;
1935 vm_size_t names_size
;
1937 vm_offset_t info_addr
;
1938 vm_size_t info_size
;
1939 unsigned int max_zones
, i
;
1944 size_t num_fake_zones
;
1946 if (host
== HOST_NULL
)
1947 return KERN_INVALID_HOST
;
1949 num_fake_zones
= sizeof fake_zones
/ sizeof fake_zones
[0];
1952 * We assume that zones aren't freed once allocated.
1953 * We won't pick up any zones that are allocated later.
1956 simple_lock(&all_zones_lock
);
1957 max_zones
= num_zones
+ num_fake_zones
;
1959 simple_unlock(&all_zones_lock
);
1961 if (max_zones
<= *namesCntp
) {
1962 /* use in-line memory */
1963 names_size
= *namesCntp
* sizeof *names
;
1966 names_size
= round_page(max_zones
* sizeof *names
);
1967 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1968 &names_addr
, names_size
);
1969 if (kr
!= KERN_SUCCESS
)
1971 names
= (zone_name_t
*) names_addr
;
1974 if (max_zones
<= *infoCntp
) {
1975 /* use in-line memory */
1976 info_size
= *infoCntp
* sizeof *info
;
1979 info_size
= round_page(max_zones
* sizeof *info
);
1980 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1981 &info_addr
, info_size
);
1982 if (kr
!= KERN_SUCCESS
) {
1983 if (names
!= *namesp
)
1984 kmem_free(ipc_kernel_map
,
1985 names_addr
, names_size
);
1989 info
= (zone_info_t
*) info_addr
;
1994 for (i
= 0; i
< num_zones
; i
++) {
1997 assert(z
!= ZONE_NULL
);
2003 simple_lock(&all_zones_lock
);
2005 simple_unlock(&all_zones_lock
);
2007 /* assuming here the name data is static */
2008 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
2009 sizeof zn
->zn_name
);
2010 zn
->zn_name
[sizeof zn
->zn_name
- 1] = '\0';
2012 zi
->zi_count
= zcopy
.count
;
2013 zi
->zi_cur_size
= zcopy
.cur_size
;
2014 zi
->zi_max_size
= zcopy
.max_size
;
2015 zi
->zi_elem_size
= zcopy
.elem_size
;
2016 zi
->zi_alloc_size
= zcopy
.alloc_size
;
2017 zi
->zi_exhaustible
= zcopy
.exhaustible
;
2018 zi
->zi_collectable
= zcopy
.collectable
;
2025 * loop through the fake zones and fill them using the specialized
2028 for (i
= 0; i
< num_fake_zones
; i
++) {
2029 strncpy(zn
->zn_name
, fake_zones
[i
].name
, sizeof zn
->zn_name
);
2030 zn
->zn_name
[sizeof zn
->zn_name
- 1] = '\0';
2031 fake_zones
[i
].func(&zi
->zi_count
, &zi
->zi_cur_size
,
2032 &zi
->zi_max_size
, &zi
->zi_elem_size
,
2033 &zi
->zi_alloc_size
, &zi
->zi_collectable
,
2034 &zi
->zi_exhaustible
);
2039 if (names
!= *namesp
) {
2043 used
= max_zones
* sizeof *names
;
2045 if (used
!= names_size
)
2046 bzero((char *) (names_addr
+ used
), names_size
- used
);
2048 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)names_addr
,
2049 (vm_map_size_t
)names_size
, TRUE
, ©
);
2050 assert(kr
== KERN_SUCCESS
);
2052 *namesp
= (zone_name_t
*) copy
;
2054 *namesCntp
= max_zones
;
2056 if (info
!= *infop
) {
2060 used
= max_zones
* sizeof *info
;
2062 if (used
!= info_size
)
2063 bzero((char *) (info_addr
+ used
), info_size
- used
);
2065 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)info_addr
,
2066 (vm_map_size_t
)info_size
, TRUE
, ©
);
2067 assert(kr
== KERN_SUCCESS
);
2069 *infop
= (zone_info_t
*) copy
;
2071 *infoCntp
= max_zones
;
2073 return KERN_SUCCESS
;
2077 #include <ddb/db_command.h>
2078 #include <ddb/db_output.h>
2079 #include <kern/kern_print.h>
2081 const char *zone_labels
=
2082 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
2089 void db_zone_check_active(
2091 void db_zone_print_active(
2093 #endif /* ZONE_DEBUG */
2094 void db_zone_print_free(
2104 db_printf("%8x %8x %8x %8x %6x %8x %s ",
2105 addr
, zcopy
.count
, zcopy
.cur_size
,
2106 zcopy
.max_size
, zcopy
.elem_size
,
2107 zcopy
.alloc_size
, zcopy
.zone_name
);
2108 if (zcopy
.exhaustible
)
2110 if (zcopy
.collectable
)
2112 if (zcopy
.expandable
)
2119 db_show_one_zone(db_expr_t addr
, boolean_t have_addr
,
2120 __unused db_expr_t count
, __unused
char *modif
)
2122 struct zone
*z
= (zone_t
)((char *)0 + addr
);
2124 if (z
== ZONE_NULL
|| !have_addr
){
2125 db_error("No Zone\n");
2129 db_printf("%s\n", zone_labels
);
2135 db_show_all_zones(__unused db_expr_t addr
, boolean_t have_addr
, db_expr_t count
,
2136 __unused
char *modif
)
2142 * Don't risk hanging by unconditionally locking,
2143 * risk of incoherent data is small (zones aren't freed).
2145 have_addr
= simple_lock_try(&all_zones_lock
);
2149 simple_unlock(&all_zones_lock
);
2152 db_printf("%s\n", zone_labels
);
2153 for ( ; count
> 0; count
--) {
2155 db_error("Mangled Zone List\n");
2159 total
+= z
->cur_size
,
2161 have_addr
= simple_lock_try(&all_zones_lock
);
2164 simple_unlock(&all_zones_lock
);
2167 db_printf("\nTotal %8x", total
);
2168 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
2173 db_zone_check_active(
2179 if (!zone_debug_enabled(zone
) || !zone_check
)
2181 tmp_elem
= queue_first(&zone
->active_zones
);
2182 while (count
< zone
->count
) {
2184 if (tmp_elem
== 0) {
2185 printf("unexpected zero element, zone=%p, count=%d\n",
2190 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
2191 printf("unexpected queue_end, zone=%p, count=%d\n",
2196 tmp_elem
= queue_next(tmp_elem
);
2198 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
2199 printf("not at queue_end, zone=%p, tmp_elem=%p\n",
2206 db_zone_print_active(
2212 if (!zone_debug_enabled(zone
)) {
2213 printf("zone %p debug not enabled\n", zone
);
2217 printf("zone_check FALSE\n");
2221 printf("zone %p, active elements %d\n", zone
, zone
->count
);
2222 printf("active list:\n");
2223 tmp_elem
= queue_first(&zone
->active_zones
);
2224 while (count
< zone
->count
) {
2225 printf(" %p", tmp_elem
);
2227 if ((count
% 6) == 0)
2229 if (tmp_elem
== 0) {
2230 printf("\nunexpected zero element, count=%d\n", count
);
2233 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
2234 printf("\nunexpected queue_end, count=%d\n", count
);
2237 tmp_elem
= queue_next(tmp_elem
);
2239 if (!queue_end(tmp_elem
, &zone
->active_zones
))
2240 printf("\nnot at queue_end, tmp_elem=%p\n", tmp_elem
);
2244 #endif /* ZONE_DEBUG */
2254 freecount
= zone_free_count(zone
);
2255 printf("zone %p, free elements %d\n", zone
, freecount
);
2256 printf("free list:\n");
2257 elem
= zone
->free_elements
;
2258 while (count
< freecount
) {
2259 printf(" 0x%x", elem
);
2261 if ((count
% 6) == 0)
2264 printf("\nunexpected zero element, count=%d\n", count
);
2267 elem
= *((vm_offset_t
*)elem
);
2270 printf("\nnot at end of free list, elem=0x%x\n", elem
);
2275 #endif /* MACH_KDB */
2280 /* should we care about locks here ? */
2288 char *elt
= (char *)prev
;
2290 if (!zone_debug_enabled(z
))
2292 elt
-= ZONE_DEBUG_OFFSET
;
2293 elt
= (char *) queue_next((queue_t
) elt
);
2294 if ((queue_t
) elt
== &z
->active_zones
)
2296 elt
+= ZONE_DEBUG_OFFSET
;
2306 if (!zone_debug_enabled(z
))
2308 if (queue_empty(&z
->active_zones
))
2310 elt
= (char *)queue_first(&z
->active_zones
);
2311 elt
+= ZONE_DEBUG_OFFSET
;
2316 * Second arg controls how many zone elements are printed:
2319 * n, n > 0 => last n on active list
2328 boolean_t print
= (tail
!= 0);
2332 if (z
->count
< tail
)
2334 tail
= z
->count
- tail
;
2335 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
2336 if (print
&& tail
<= count
)
2337 db_printf("%8x\n", elt
);
2340 assert(count
== z
->count
);
2343 #endif /* MACH_KDB */
2345 #define zone_in_use(z) ( z->count || z->free_elements )
2351 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
2352 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
2354 queue_init(&z
->active_zones
);
2355 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
2362 if (!zone_debug_enabled(z
) || zone_in_use(z
))
2364 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
2365 z
->active_zones
.next
= z
->active_zones
.prev
= NULL
;
2367 #endif /* ZONE_DEBUG */