2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
55 * Author: Avadis Tevanian, Jr.
57 * Zone-based memory allocator. A zone is a collection of fixed size
58 * data blocks for which quick allocation/deallocation is possible.
60 #include <zone_debug.h>
64 #include <mach/mach_types.h>
65 #include <mach/vm_param.h>
66 #include <mach/kern_return.h>
67 #include <mach/mach_host_server.h>
68 #include <mach/machine/vm_types.h>
69 #include <mach_debug/zone_info.h>
71 #include <kern/kern_types.h>
72 #include <kern/assert.h>
73 #include <kern/host.h>
74 #include <kern/macro_help.h>
75 #include <kern/sched.h>
76 #include <kern/lock.h>
77 #include <kern/sched_prim.h>
78 #include <kern/misc_protos.h>
79 #include <kern/thread_call.h>
80 #include <kern/zalloc.h>
81 #include <kern/kalloc.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_kern.h>
86 #include <vm/vm_page.h>
88 #include <machine/machparam.h>
91 /* for fake zone stat routines */
92 #include <ppc/savearea.h>
93 #include <ppc/mappings.h>
97 /* Detect use of zone elt after freeing it by two methods:
98 * (1) Range-check the free-list "next" ptr for sanity.
99 * (2) Store the ptr in two different words, and compare them against
100 * each other when re-using the zone elt, to detect modifications;
105 #define is_kernel_data_addr(a) \
106 (!(a) || (IS_SYS_VA(a) && !((a) & (sizeof(long)-1))))
108 #else /* !defined(__alpha) */
110 #define is_kernel_data_addr(a) \
111 (!(a) || ((a) >= VM_MIN_KERNEL_ADDRESS && !((a) & 0x3)))
113 #endif /* defined(__alpha) */
115 /* Should we set all words of the zone element to an illegal address
116 * when it is freed, to help catch usage after freeing? The down-side
117 * is that this obscures the identity of the freed element.
119 boolean_t zfree_clear
= FALSE
;
121 #define ADD_TO_ZONE(zone, element) \
126 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
128 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
130 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
131 (zone)->free_elements = (vm_offset_t) (element); \
135 #define REMOVE_FROM_ZONE(zone, ret, type) \
137 (ret) = (type) (zone)->free_elements; \
138 if ((ret) != (type) 0) { \
139 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
140 panic("A freed zone element has been modified.\n"); \
143 (zone)->free_elements = *((vm_offset_t *)(ret)); \
146 #else /* MACH_ASSERT */
148 #define ADD_TO_ZONE(zone, element) \
150 *((vm_offset_t *)(element)) = (zone)->free_elements; \
151 (zone)->free_elements = (vm_offset_t) (element); \
155 #define REMOVE_FROM_ZONE(zone, ret, type) \
157 (ret) = (type) (zone)->free_elements; \
158 if ((ret) != (type) 0) { \
160 (zone)->free_elements = *((vm_offset_t *)(ret)); \
164 #endif /* MACH_ASSERT */
167 #define zone_debug_enabled(z) z->active_zones.next
168 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
169 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
170 #endif /* ZONE_DEBUG */
173 * Support for garbage collection of unused zone pages:
176 struct zone_page_table_entry
{
177 struct zone_page_table_entry
*link
;
188 void zone_page_alloc(
192 void zone_page_free_element(
193 struct zone_page_table_entry
**free_pages
,
197 void zone_page_collect(
201 boolean_t
zone_page_collectable(
210 thread_call_param_t p0
,
211 thread_call_param_t p1
);
214 #if ZONE_DEBUG && MACH_KDB
218 #endif /* ZONE_DEBUG && MACH_KDB */
220 vm_map_t zone_map
= VM_MAP_NULL
;
222 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
225 * The VM system gives us an initial chunk of memory.
226 * It has to be big enough to allocate the zone_zone
230 vm_size_t zdata_size
;
232 #define lock_zone(zone) \
234 mutex_lock(&(zone)->lock); \
237 #define unlock_zone(zone) \
239 mutex_unlock(&(zone)->lock); \
242 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
243 #define zone_sleep(zone) \
244 thread_sleep_mutex((event_t)(zone), \
248 #define lock_zone_init(zone) \
250 mutex_init(&zone->lock, 0); \
253 #define lock_try_zone(zone) mutex_try(&zone->lock)
255 kern_return_t
zget_space(
257 vm_offset_t
*result
);
259 decl_simple_lock_data(,zget_space_lock
)
260 vm_offset_t zalloc_next_space
;
261 vm_offset_t zalloc_end_of_space
;
262 vm_size_t zalloc_wasted_space
;
265 * Garbage collection map information
267 struct zone_page_table_entry
* zone_page_table
;
268 vm_offset_t zone_map_min_address
;
269 vm_offset_t zone_map_max_address
;
270 unsigned int zone_pages
;
273 * Exclude more than one concurrent garbage collection
275 decl_mutex_data(, zone_gc_lock
)
277 #define from_zone_map(addr, size) \
278 ((vm_offset_t)(addr) >= zone_map_min_address && \
279 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
281 #define ZONE_PAGE_USED 0
282 #define ZONE_PAGE_UNUSED -1
286 * Protects first_zone, last_zone, num_zones,
287 * and the next_zone field of zones.
289 decl_simple_lock_data(, all_zones_lock
)
292 unsigned int num_zones
;
294 boolean_t zone_gc_allowed
= TRUE
;
295 boolean_t zone_gc_forced
= FALSE
;
296 unsigned zone_gc_last_tick
= 0;
297 unsigned zone_gc_max_rate
= 0; /* in ticks */
301 * zinit initializes a new zone. The zone data structures themselves
302 * are stored in a zone, which is initially a static structure that
303 * is initialized by zone_init.
307 vm_size_t size
, /* the size of an element */
308 vm_size_t max
, /* maximum memory to use */
309 vm_size_t alloc
, /* allocation size */
310 const char *name
) /* a name for the zone */
314 if (zone_zone
== ZONE_NULL
) {
315 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
319 z
= (zone_t
) zalloc(zone_zone
);
324 * Round off all the parameters appropriately.
326 if (size
< sizeof(z
->free_elements
))
327 size
= sizeof(z
->free_elements
);
328 size
= ((size
-1) + sizeof(z
->free_elements
)) -
329 ((size
-1) % sizeof(z
->free_elements
));
332 alloc
= round_page(alloc
);
333 max
= round_page(max
);
335 * we look for an allocation size with less than 1% waste
336 * up to 5 pages in size...
337 * otherwise, we look for an allocation size with least fragmentation
338 * in the range of 1 - 5 pages
339 * This size will be used unless
340 * the user suggestion is larger AND has less fragmentation
342 { vm_size_t best
, waste
; unsigned int i
;
346 for (i
= 1; i
<= 5; i
++) {
347 vm_size_t tsize
, twaste
;
349 tsize
= i
* PAGE_SIZE
;
351 if ((tsize
% size
) < (tsize
/ 100)) {
353 goto use_this_allocation
;
355 twaste
= tsize
% size
;
357 best
= tsize
, waste
= twaste
;
359 if (alloc
<= best
|| (alloc
% size
>= waste
))
363 if (max
&& (max
< alloc
))
366 z
->free_elements
= 0;
370 z
->alloc_size
= alloc
;
373 z
->doing_alloc
= FALSE
;
375 z
->exhaustible
= FALSE
;
376 z
->collectable
= TRUE
;
377 z
->allows_foreign
= FALSE
;
378 z
->expandable
= TRUE
;
380 z
->async_pending
= FALSE
;
383 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
384 zone_debug_enable(z
);
385 #endif /* ZONE_DEBUG */
389 * Add the zone to the all-zones list.
392 z
->next_zone
= ZONE_NULL
;
393 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
394 simple_lock(&all_zones_lock
);
396 last_zone
= &z
->next_zone
;
398 simple_unlock(&all_zones_lock
);
404 * Cram the given memory into the specified zone.
408 register zone_t zone
,
412 register vm_size_t elem_size
;
413 vm_offset_t newmem
= (vm_offset_t
) newaddr
;
415 /* Basic sanity checks */
416 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
417 assert(!zone
->collectable
|| zone
->allows_foreign
418 || (from_zone_map(newmem
, size
)));
420 elem_size
= zone
->elem_size
;
423 while (size
>= elem_size
) {
424 ADD_TO_ZONE(zone
, newmem
);
425 if (from_zone_map(newmem
, elem_size
))
426 zone_page_alloc(newmem
, elem_size
);
427 zone
->count
++; /* compensate for ADD_TO_ZONE */
430 zone
->cur_size
+= elem_size
;
436 * Contiguous space allocator for non-paged zones. Allocates "size" amount
437 * of memory from zone_map.
445 vm_offset_t new_space
= 0;
446 vm_size_t space_to_add
= 0;
448 simple_lock(&zget_space_lock
);
449 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
451 * Add at least one page to allocation area.
454 space_to_add
= round_page(size
);
456 if (new_space
== 0) {
457 kern_return_t retval
;
459 * Memory cannot be wired down while holding
460 * any locks that the pageout daemon might
461 * need to free up pages. [Making the zget_space
462 * lock a complex lock does not help in this
465 * Unlock and allocate memory. Because several
466 * threads might try to do this at once, don't
467 * use the memory before checking for available
471 simple_unlock(&zget_space_lock
);
473 retval
= kernel_memory_allocate(zone_map
, &new_space
,
474 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
475 if (retval
!= KERN_SUCCESS
)
477 zone_page_init(new_space
, space_to_add
,
479 simple_lock(&zget_space_lock
);
485 * Memory was allocated in a previous iteration.
487 * Check whether the new region is contiguous
491 if (new_space
!= zalloc_end_of_space
) {
493 * Throw away the remainder of the
494 * old space, and start a new one.
496 zalloc_wasted_space
+=
497 zalloc_end_of_space
- zalloc_next_space
;
498 zalloc_next_space
= new_space
;
501 zalloc_end_of_space
= new_space
+ space_to_add
;
505 *result
= zalloc_next_space
;
506 zalloc_next_space
+= size
;
507 simple_unlock(&zget_space_lock
);
510 kmem_free(zone_map
, new_space
, space_to_add
);
512 return(KERN_SUCCESS
);
517 * Steal memory for the zone package. Called from
518 * vm_page_bootstrap().
521 zone_steal_memory(void)
523 zdata_size
= round_page(128*sizeof(struct zone
));
524 zdata
= (vm_offset_t
)((char *)pmap_steal_memory(zdata_size
) - (char *)0);
529 * Fill a zone with enough memory to contain at least nelem elements.
530 * Memory is obtained with kmem_alloc_wired from the kernel_map.
531 * Return the number of elements actually put into the zone, which may
532 * be more than the caller asked for since the memory allocation is
533 * rounded up to a full page.
548 size
= nelem
* zone
->elem_size
;
549 size
= round_page(size
);
550 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
551 if (kr
!= KERN_SUCCESS
)
554 zone_change(zone
, Z_FOREIGN
, TRUE
);
555 zcram(zone
, (void *)memory
, size
);
556 nalloc
= size
/ zone
->elem_size
;
557 assert(nalloc
>= nelem
);
563 * Initialize the "zone of zones" which uses fixed memory allocated
564 * earlier in memory initialization. zone_bootstrap is called
570 vm_size_t zone_zone_size
;
571 vm_offset_t zone_zone_space
;
573 simple_lock_init(&all_zones_lock
, 0);
575 first_zone
= ZONE_NULL
;
576 last_zone
= &first_zone
;
579 simple_lock_init(&zget_space_lock
, 0);
580 zalloc_next_space
= zdata
;
581 zalloc_end_of_space
= zdata
+ zdata_size
;
582 zalloc_wasted_space
= 0;
584 /* assertion: nobody else called zinit before us */
585 assert(zone_zone
== ZONE_NULL
);
586 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
587 sizeof(struct zone
), "zones");
588 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
589 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
590 zget_space(zone_zone_size
, &zone_zone_space
);
591 zcram(zone_zone
, (void *)zone_zone_space
, zone_zone_size
);
596 vm_size_t max_zonemap_size
)
598 kern_return_t retval
;
599 vm_offset_t zone_min
;
600 vm_offset_t zone_max
;
601 vm_size_t zone_table_size
;
603 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
604 FALSE
, VM_FLAGS_ANYWHERE
, &zone_map
);
606 if (retval
!= KERN_SUCCESS
)
607 panic("zone_init: kmem_suballoc failed");
608 zone_max
= zone_min
+ round_page(max_zonemap_size
);
610 * Setup garbage collection information:
612 zone_table_size
= atop_32(zone_max
- zone_min
) *
613 sizeof(struct zone_page_table_entry
);
614 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
615 zone_table_size
) != KERN_SUCCESS
)
617 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
618 zone_pages
= atop_32(zone_max
- zone_min
);
619 zone_map_min_address
= zone_min
;
620 zone_map_max_address
= zone_max
;
621 mutex_init(&zone_gc_lock
, 0);
622 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
627 * zalloc returns an element from the specified zone.
631 register zone_t zone
,
635 kern_return_t retval
;
637 assert(zone
!= ZONE_NULL
);
641 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
643 while ((addr
== 0) && canblock
&& (zone
->doing_gc
)) {
644 zone
->waiting
= TRUE
;
646 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
649 while ((addr
== 0) && canblock
) {
651 * If nothing was there, try to get more
653 if (zone
->doing_alloc
) {
655 * Someone is allocating memory for this zone.
656 * Wait for it to show up, then try again.
658 zone
->waiting
= TRUE
;
662 if ((zone
->cur_size
+ zone
->elem_size
) >
664 if (zone
->exhaustible
)
666 if (zone
->expandable
) {
668 * We're willing to overflow certain
669 * zones, but not without complaining.
671 * This is best used in conjunction
672 * with the collectable flag. What we
673 * want is an assurance we can get the
674 * memory back, assuming there's no
677 zone
->max_size
+= (zone
->max_size
>> 1);
681 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
684 zone
->doing_alloc
= TRUE
;
687 if (zone
->collectable
) {
689 vm_size_t alloc_size
;
690 boolean_t retry
= FALSE
;
694 if (vm_pool_low() || retry
== TRUE
)
696 round_page(zone
->elem_size
);
698 alloc_size
= zone
->alloc_size
;
700 retval
= kernel_memory_allocate(zone_map
,
701 &space
, alloc_size
, 0,
702 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
703 if (retval
== KERN_SUCCESS
) {
704 zone_page_init(space
, alloc_size
,
706 zcram(zone
, (void *)space
, alloc_size
);
709 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
710 /* would like to cause a zone_gc() */
712 panic("zalloc: \"%s\" (%d elements) retry fail %d", zone
->zone_name
, zone
->count
, retval
);
719 zone
->doing_alloc
= FALSE
;
721 zone
->waiting
= FALSE
;
724 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
726 retval
== KERN_RESOURCE_SHORTAGE
) {
734 retval
= zget_space(zone
->elem_size
, &space
);
737 zone
->doing_alloc
= FALSE
;
739 zone
->waiting
= FALSE
;
740 thread_wakeup((event_t
)zone
);
742 if (retval
== KERN_SUCCESS
) {
744 zone
->cur_size
+= zone
->elem_size
;
746 if (zone_debug_enabled(zone
)) {
747 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
751 zone_page_alloc(space
, zone
->elem_size
);
753 if (zone_debug_enabled(zone
))
754 space
+= ZONE_DEBUG_OFFSET
;
756 return((void *)space
);
758 if (retval
== KERN_RESOURCE_SHORTAGE
) {
764 panic("zalloc: \"%s\" (%d elements) zget_space returned %d", zone
->zone_name
, zone
->count
, retval
);
769 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
772 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
773 zone
->async_pending
= TRUE
;
775 thread_call_enter(&zone
->call_async_alloc
);
777 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
781 if (addr
&& zone_debug_enabled(zone
)) {
782 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
783 addr
+= ZONE_DEBUG_OFFSET
;
789 return((void *)addr
);
795 register zone_t zone
)
797 return( zalloc_canblock(zone
, TRUE
) );
802 register zone_t zone
)
804 return( zalloc_canblock(zone
, FALSE
) );
809 thread_call_param_t p0
,
810 __unused thread_call_param_t p1
)
814 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
815 zfree((zone_t
)p0
, elt
);
816 lock_zone(((zone_t
)p0
));
817 ((zone_t
)p0
)->async_pending
= FALSE
;
818 unlock_zone(((zone_t
)p0
));
823 * zget returns an element from the specified zone
824 * and immediately returns nothing if there is nothing there.
826 * This form should be used when you can not block (like when
827 * processing an interrupt).
831 register zone_t zone
)
833 register vm_offset_t addr
;
835 assert( zone
!= ZONE_NULL
);
837 if (!lock_try_zone(zone
))
840 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
842 if (addr
&& zone_debug_enabled(zone
)) {
843 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
844 addr
+= ZONE_DEBUG_OFFSET
;
846 #endif /* ZONE_DEBUG */
849 return((void *) addr
);
852 /* Keep this FALSE by default. Large memory machine run orders of magnitude
853 slower in debug mode when true. Use debugger to enable if needed */
854 /* static */ boolean_t zone_check
= FALSE
;
856 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
857 static vm_offset_t zone_last_bogus_elem
= 0;
861 register zone_t zone
,
864 vm_offset_t elem
= (vm_offset_t
) addr
;
867 /* Basic sanity checks */
868 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
869 panic("zfree: NULL");
870 /* zone_gc assumes zones are never freed */
871 if (zone
== zone_zone
)
872 panic("zfree: freeing to zone_zone breaks zone_gc!");
875 if (zone
->collectable
&& !zone
->allows_foreign
&&
876 !from_zone_map(elem
, zone
->elem_size
)) {
878 panic("zfree: non-allocated memory in collectable zone!");
880 zone_last_bogus_zone
= zone
;
881 zone_last_bogus_elem
= elem
;
887 if (zone_debug_enabled(zone
)) {
890 elem
-= ZONE_DEBUG_OFFSET
;
892 /* check the zone's consistency */
894 for (tmp_elem
= queue_first(&zone
->active_zones
);
895 !queue_end(tmp_elem
, &zone
->active_zones
);
896 tmp_elem
= queue_next(tmp_elem
))
897 if (elem
== (vm_offset_t
)tmp_elem
)
899 if (elem
!= (vm_offset_t
)tmp_elem
)
900 panic("zfree()ing element from wrong zone");
902 remqueue(&zone
->active_zones
, (queue_t
) elem
);
904 #endif /* ZONE_DEBUG */
908 /* check the zone's consistency */
910 for (this = zone
->free_elements
;
912 this = * (vm_offset_t
*) this)
913 if (!pmap_kernel_va(this) || this == elem
)
916 ADD_TO_ZONE(zone
, elem
);
919 * If elements have one or more pages, and memory is low,
920 * request to run the garbage collection in the zone the next
921 * time the pageout thread runs.
923 if (zone
->elem_size
>= PAGE_SIZE
&&
925 zone_gc_forced
= TRUE
;
931 /* Change a zone's flags.
932 * This routine must be called immediately after zinit.
940 assert( zone
!= ZONE_NULL
);
941 assert( value
== TRUE
|| value
== FALSE
);
945 zone
->exhaustible
= value
;
948 zone
->collectable
= value
;
951 zone
->expandable
= value
;
954 zone
->allows_foreign
= value
;
958 panic("Zone_change: Wrong Item Type!");
962 lock_zone_init(zone
);
966 * Return the expected number of free elements in the zone.
967 * This calculation will be incorrect if items are zfree'd that
968 * were never zalloc'd/zget'd. The correct way to stuff memory
969 * into a zone is by zcram.
973 zone_free_count(zone_t zone
)
975 integer_t free_count
;
978 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
981 assert(free_count
>= 0);
987 * zprealloc preallocates wired memory, exanding the specified
988 * zone to the specified size
998 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
1000 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
1001 zcram(zone
, (void *)addr
, size
);
1006 * Zone garbage collection subroutines
1010 zone_page_collectable(
1014 struct zone_page_table_entry
*zp
;
1018 if (!from_zone_map(addr
, size
))
1019 panic("zone_page_collectable");
1022 i
= atop_32(addr
-zone_map_min_address
);
1023 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1025 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1026 if (zp
->collect_count
== zp
->alloc_count
)
1037 struct zone_page_table_entry
*zp
;
1041 if (!from_zone_map(addr
, size
))
1042 panic("zone_page_keep");
1045 i
= atop_32(addr
-zone_map_min_address
);
1046 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1048 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1049 zp
->collect_count
= 0;
1057 struct zone_page_table_entry
*zp
;
1061 if (!from_zone_map(addr
, size
))
1062 panic("zone_page_collect");
1065 i
= atop_32(addr
-zone_map_min_address
);
1066 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1068 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1069 ++zp
->collect_count
;
1078 struct zone_page_table_entry
*zp
;
1082 if (!from_zone_map(addr
, size
))
1083 panic("zone_page_init");
1086 i
= atop_32(addr
-zone_map_min_address
);
1087 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1089 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1090 zp
->alloc_count
= value
;
1091 zp
->collect_count
= 0;
1100 struct zone_page_table_entry
*zp
;
1104 if (!from_zone_map(addr
, size
))
1105 panic("zone_page_alloc");
1108 i
= atop_32(addr
-zone_map_min_address
);
1109 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1111 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1113 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1114 * it was previously set to ZONE_PAGE_UNUSED.
1116 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1117 zp
->alloc_count
= 1;
1124 zone_page_free_element(
1125 struct zone_page_table_entry
**free_pages
,
1129 struct zone_page_table_entry
*zp
;
1133 if (!from_zone_map(addr
, size
))
1134 panic("zone_page_free_element");
1137 i
= atop_32(addr
-zone_map_min_address
);
1138 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1140 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1141 if (zp
->collect_count
> 0)
1142 --zp
->collect_count
;
1143 if (--zp
->alloc_count
== 0) {
1144 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1145 zp
->collect_count
= 0;
1147 zp
->link
= *free_pages
;
1154 /* This is used for walking through a zone's free element list.
1156 struct zone_free_element
{
1157 struct zone_free_element
* next
;
1163 uint32_t elems_collected
,
1168 /* Zone garbage collection
1170 * zone_gc will walk through all the free elements in all the
1171 * zones that are marked collectable looking for reclaimable
1172 * pages. zone_gc is called by consider_zone_gc when the system
1173 * begins to run out of memory.
1178 unsigned int max_zones
;
1181 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1183 mutex_lock(&zone_gc_lock
);
1185 simple_lock(&all_zones_lock
);
1186 max_zones
= num_zones
;
1188 simple_unlock(&all_zones_lock
);
1191 for (i
= 0; i
< zone_pages
; i
++)
1192 assert(zone_page_table
[i
].collect_count
== 0);
1193 #endif /* MACH_ASSERT */
1195 zone_free_pages
= NULL
;
1197 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1199 vm_size_t elt_size
, size_freed
;
1200 struct zone_free_element
*elt
, *base_elt
, *base_prev
, *prev
, *scan
, *keep
, *tail
;
1202 assert(z
!= ZONE_NULL
);
1204 if (!z
->collectable
)
1209 elt_size
= z
->elem_size
;
1212 * Do a quick feasability check before we scan the zone:
1213 * skip unless there is likelihood of getting pages back
1214 * (i.e we need a whole allocation block's worth of free
1215 * elements before we can garbage collect) and
1216 * the zone has more than 10 percent of it's elements free
1218 if (((z
->cur_size
- z
->count
* elt_size
) <= (2 * z
->alloc_size
)) ||
1219 ((z
->cur_size
- z
->count
* elt_size
) <= (z
->cur_size
/ 10))) {
1227 * Snatch all of the free elements away from the zone.
1230 scan
= (void *)z
->free_elements
;
1231 (void *)z
->free_elements
= NULL
;
1238 * Determine which elements we can attempt to collect
1239 * and count them up in the page table. Foreign elements
1240 * are returned to the zone.
1243 prev
= (void *)&scan
;
1245 n
= 0; tail
= keep
= NULL
;
1246 while (elt
!= NULL
) {
1247 if (from_zone_map(elt
, elt_size
)) {
1248 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1253 ++zgc_stats
.elems_collected
;
1259 tail
= tail
->next
= elt
;
1261 elt
= prev
->next
= elt
->next
;
1266 * Dribble back the elements we are keeping.
1270 if (z
->waiting
== TRUE
) {
1274 tail
->next
= (void *)z
->free_elements
;
1275 (void *)z
->free_elements
= keep
;
1281 while ((elt
!= NULL
) && (++m
< 50)) {
1286 prev
->next
= (void *)z
->free_elements
;
1287 (void *)z
->free_elements
= (void *)base_elt
;
1288 base_prev
->next
= elt
;
1305 * Return any remaining elements.
1311 tail
->next
= (void *)z
->free_elements
;
1312 (void *)z
->free_elements
= keep
;
1320 * Determine which pages we can reclaim and
1321 * free those elements.
1325 prev
= (void *)&scan
;
1327 n
= 0; tail
= keep
= NULL
;
1328 while (elt
!= NULL
) {
1329 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1330 size_freed
+= elt_size
;
1331 zone_page_free_element(&zone_free_pages
,
1332 (vm_offset_t
)elt
, elt_size
);
1334 elt
= prev
->next
= elt
->next
;
1336 ++zgc_stats
.elems_freed
;
1339 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1344 tail
= tail
->next
= elt
;
1346 elt
= prev
->next
= elt
->next
;
1349 ++zgc_stats
.elems_kept
;
1353 * Dribble back the elements we are keeping,
1354 * and update the zone size info.
1360 z
->cur_size
-= size_freed
;
1364 tail
->next
= (void *)z
->free_elements
;
1365 (void *)z
->free_elements
= keep
;
1375 n
= 0; tail
= keep
= NULL
;
1380 * Return any remaining elements, and update
1381 * the zone size info.
1386 if (size_freed
> 0 || keep
!= NULL
) {
1388 z
->cur_size
-= size_freed
;
1391 tail
->next
= (void *)z
->free_elements
;
1392 (void *)z
->free_elements
= keep
;
1397 z
->doing_gc
= FALSE
;
1406 * Reclaim the pages we are freeing.
1409 while ((zp
= zone_free_pages
) != NULL
) {
1410 zone_free_pages
= zp
->link
;
1411 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1412 (zp
- zone_page_table
), PAGE_SIZE
);
1413 ++zgc_stats
.pgs_freed
;
1416 mutex_unlock(&zone_gc_lock
);
1422 * Called by the pageout daemon when the system needs more free pages.
1426 consider_zone_gc(void)
1429 * By default, don't attempt zone GC more frequently
1430 * than once / 1 minutes.
1433 if (zone_gc_max_rate
== 0)
1434 zone_gc_max_rate
= (60 << SCHED_TICK_SHIFT
) + 1;
1436 if (zone_gc_allowed
&&
1437 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1439 zone_gc_forced
= FALSE
;
1440 zone_gc_last_tick
= sched_tick
;
1449 zone_name_array_t
*namesp
,
1450 mach_msg_type_number_t
*namesCntp
,
1451 zone_info_array_t
*infop
,
1452 mach_msg_type_number_t
*infoCntp
)
1455 vm_offset_t names_addr
;
1456 vm_size_t names_size
;
1458 vm_offset_t info_addr
;
1459 vm_size_t info_size
;
1460 unsigned int max_zones
, i
;
1466 if (host
== HOST_NULL
)
1467 return KERN_INVALID_HOST
;
1470 * We assume that zones aren't freed once allocated.
1471 * We won't pick up any zones that are allocated later.
1474 simple_lock(&all_zones_lock
);
1476 max_zones
= num_zones
+ 4;
1478 max_zones
= num_zones
+ 2;
1481 simple_unlock(&all_zones_lock
);
1483 if (max_zones
<= *namesCntp
) {
1484 /* use in-line memory */
1485 names_size
= *namesCntp
* sizeof *names
;
1488 names_size
= round_page(max_zones
* sizeof *names
);
1489 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1490 &names_addr
, names_size
);
1491 if (kr
!= KERN_SUCCESS
)
1493 names
= (zone_name_t
*) names_addr
;
1496 if (max_zones
<= *infoCntp
) {
1497 /* use in-line memory */
1498 info_size
= *infoCntp
* sizeof *info
;
1501 info_size
= round_page(max_zones
* sizeof *info
);
1502 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1503 &info_addr
, info_size
);
1504 if (kr
!= KERN_SUCCESS
) {
1505 if (names
!= *namesp
)
1506 kmem_free(ipc_kernel_map
,
1507 names_addr
, names_size
);
1511 info
= (zone_info_t
*) info_addr
;
1516 for (i
= 0; i
< num_zones
; i
++) {
1519 assert(z
!= ZONE_NULL
);
1525 simple_lock(&all_zones_lock
);
1527 simple_unlock(&all_zones_lock
);
1529 /* assuming here the name data is static */
1530 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1531 sizeof zn
->zn_name
);
1533 zi
->zi_count
= zcopy
.count
;
1534 zi
->zi_cur_size
= zcopy
.cur_size
;
1535 zi
->zi_max_size
= zcopy
.max_size
;
1536 zi
->zi_elem_size
= zcopy
.elem_size
;
1537 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1538 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1539 zi
->zi_collectable
= zcopy
.collectable
;
1544 strcpy(zn
->zn_name
, "kernel_stacks");
1545 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1546 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1550 strcpy(zn
->zn_name
, "save_areas");
1551 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1552 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1556 strcpy(zn
->zn_name
, "pmap_mappings");
1557 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1558 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1562 strcpy(zn
->zn_name
, "kalloc.large");
1563 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1564 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1566 if (names
!= *namesp
) {
1570 used
= max_zones
* sizeof *names
;
1572 if (used
!= names_size
)
1573 bzero((char *) (names_addr
+ used
), names_size
- used
);
1575 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)names_addr
,
1576 (vm_map_size_t
)names_size
, TRUE
, ©
);
1577 assert(kr
== KERN_SUCCESS
);
1579 *namesp
= (zone_name_t
*) copy
;
1581 *namesCntp
= max_zones
;
1583 if (info
!= *infop
) {
1587 used
= max_zones
* sizeof *info
;
1589 if (used
!= info_size
)
1590 bzero((char *) (info_addr
+ used
), info_size
- used
);
1592 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)info_addr
,
1593 (vm_map_size_t
)info_size
, TRUE
, ©
);
1594 assert(kr
== KERN_SUCCESS
);
1596 *infop
= (zone_info_t
*) copy
;
1598 *infoCntp
= max_zones
;
1600 return KERN_SUCCESS
;
1604 #include <ddb/db_command.h>
1605 #include <ddb/db_output.h>
1606 #include <kern/kern_print.h>
1608 const char *zone_labels
=
1609 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1616 void db_zone_check_active(
1618 void db_zone_print_active(
1620 #endif /* ZONE_DEBUG */
1621 void db_zone_print_free(
1631 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1632 addr
, zcopy
.count
, zcopy
.cur_size
,
1633 zcopy
.max_size
, zcopy
.elem_size
,
1634 zcopy
.alloc_size
, zcopy
.zone_name
);
1635 if (zcopy
.exhaustible
)
1637 if (zcopy
.collectable
)
1639 if (zcopy
.expandable
)
1649 __unused db_expr_t count
,
1650 __unused
char * modif
)
1652 struct zone
*z
= (zone_t
)((char *)0 + addr
);
1654 if (z
== ZONE_NULL
|| !have_addr
){
1655 db_error("No Zone\n");
1659 db_printf("%s\n", zone_labels
);
1666 __unused db_expr_t addr
,
1669 __unused
char * modif
)
1675 * Don't risk hanging by unconditionally locking,
1676 * risk of incoherent data is small (zones aren't freed).
1678 have_addr
= simple_lock_try(&all_zones_lock
);
1682 simple_unlock(&all_zones_lock
);
1685 db_printf("%s\n", zone_labels
);
1686 for ( ; count
> 0; count
--) {
1688 db_error("Mangled Zone List\n");
1692 total
+= z
->cur_size
,
1694 have_addr
= simple_lock_try(&all_zones_lock
);
1697 simple_unlock(&all_zones_lock
);
1700 db_printf("\nTotal %8x", total
);
1701 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1706 db_zone_check_active(
1712 if (!zone_debug_enabled(zone
) || !zone_check
)
1714 tmp_elem
= queue_first(&zone
->active_zones
);
1715 while (count
< zone
->count
) {
1717 if (tmp_elem
== 0) {
1718 printf("unexpected zero element, zone=0x%x, count=%d\n",
1723 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1724 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1729 tmp_elem
= queue_next(tmp_elem
);
1731 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1732 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1739 db_zone_print_active(
1745 if (!zone_debug_enabled(zone
)) {
1746 printf("zone 0x%x debug not enabled\n", zone
);
1750 printf("zone_check FALSE\n");
1754 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1755 printf("active list:\n");
1756 tmp_elem
= queue_first(&zone
->active_zones
);
1757 while (count
< zone
->count
) {
1758 printf(" 0x%x", tmp_elem
);
1760 if ((count
% 6) == 0)
1762 if (tmp_elem
== 0) {
1763 printf("\nunexpected zero element, count=%d\n", count
);
1766 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1767 printf("\nunexpected queue_end, count=%d\n", count
);
1770 tmp_elem
= queue_next(tmp_elem
);
1772 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1773 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1777 #endif /* ZONE_DEBUG */
1787 freecount
= zone_free_count(zone
);
1788 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1789 printf("free list:\n");
1790 elem
= zone
->free_elements
;
1791 while (count
< freecount
) {
1792 printf(" 0x%x", elem
);
1794 if ((count
% 6) == 0)
1797 printf("\nunexpected zero element, count=%d\n", count
);
1800 elem
= *((vm_offset_t
*)elem
);
1803 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1808 #endif /* MACH_KDB */
1813 /* should we care about locks here ? */
1821 char *elt
= (char *)prev
;
1823 if (!zone_debug_enabled(z
))
1825 elt
-= ZONE_DEBUG_OFFSET
;
1826 elt
= (char *) queue_next((queue_t
) elt
);
1827 if ((queue_t
) elt
== &z
->active_zones
)
1829 elt
+= ZONE_DEBUG_OFFSET
;
1839 if (!zone_debug_enabled(z
))
1841 if (queue_empty(&z
->active_zones
))
1843 elt
= (char *)queue_first(&z
->active_zones
);
1844 elt
+= ZONE_DEBUG_OFFSET
;
1849 * Second arg controls how many zone elements are printed:
1852 * n, n > 0 => last n on active list
1861 boolean_t print
= (tail
!= 0);
1865 if (z
->count
< tail
)
1867 tail
= z
->count
- tail
;
1868 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1869 if (print
&& tail
<= count
)
1870 db_printf("%8x\n", elt
);
1873 assert(count
== z
->count
);
1876 #endif /* MACH_KDB */
1878 #define zone_in_use(z) ( z->count || z->free_elements )
1884 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1885 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
1887 queue_init(&z
->active_zones
);
1888 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
1895 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1897 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
1898 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1900 #endif /* ZONE_DEBUG */