2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
62 * Author: Avadis Tevanian, Jr.
64 * Zone-based memory allocator. A zone is a collection of fixed size
65 * data blocks for which quick allocation/deallocation is possible.
67 #include <zone_debug.h>
71 #include <mach/mach_types.h>
72 #include <mach/vm_param.h>
73 #include <mach/kern_return.h>
74 #include <mach/mach_host_server.h>
75 #include <mach/machine/vm_types.h>
76 #include <mach_debug/zone_info.h>
78 #include <kern/kern_types.h>
79 #include <kern/assert.h>
80 #include <kern/host.h>
81 #include <kern/macro_help.h>
82 #include <kern/sched.h>
83 #include <kern/lock.h>
84 #include <kern/sched_prim.h>
85 #include <kern/misc_protos.h>
86 #include <kern/thread_call.h>
87 #include <kern/zalloc.h>
88 #include <kern/kalloc.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_page.h>
95 #include <machine/machparam.h>
98 /* for fake zone stat routines */
99 #include <ppc/savearea.h>
100 #include <ppc/mappings.h>
104 /* Detect use of zone elt after freeing it by two methods:
105 * (1) Range-check the free-list "next" ptr for sanity.
106 * (2) Store the ptr in two different words, and compare them against
107 * each other when re-using the zone elt, to detect modifications;
112 #define is_kernel_data_addr(a) \
113 (!(a) || (IS_SYS_VA(a) && !((a) & (sizeof(long)-1))))
115 #else /* !defined(__alpha) */
117 #define is_kernel_data_addr(a) \
118 (!(a) || ((a) >= VM_MIN_KERNEL_ADDRESS && !((a) & 0x3)))
120 #endif /* defined(__alpha) */
122 /* Should we set all words of the zone element to an illegal address
123 * when it is freed, to help catch usage after freeing? The down-side
124 * is that this obscures the identity of the freed element.
126 boolean_t zfree_clear
= FALSE
;
128 #define ADD_TO_ZONE(zone, element) \
133 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
135 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
137 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
138 (zone)->free_elements = (vm_offset_t) (element); \
142 #define REMOVE_FROM_ZONE(zone, ret, type) \
144 (ret) = (type) (zone)->free_elements; \
145 if ((ret) != (type) 0) { \
146 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
147 panic("A freed zone element has been modified.\n"); \
150 (zone)->free_elements = *((vm_offset_t *)(ret)); \
153 #else /* MACH_ASSERT */
155 #define ADD_TO_ZONE(zone, element) \
157 *((vm_offset_t *)(element)) = (zone)->free_elements; \
158 (zone)->free_elements = (vm_offset_t) (element); \
162 #define REMOVE_FROM_ZONE(zone, ret, type) \
164 (ret) = (type) (zone)->free_elements; \
165 if ((ret) != (type) 0) { \
167 (zone)->free_elements = *((vm_offset_t *)(ret)); \
171 #endif /* MACH_ASSERT */
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 mutex_lock(&(zone)->lock); \
244 #define unlock_zone(zone) \
246 mutex_unlock(&(zone)->lock); \
249 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
250 #define zone_sleep(zone) \
251 thread_sleep_mutex((event_t)(zone), \
255 #define lock_zone_init(zone) \
257 mutex_init(&zone->lock, 0); \
260 #define lock_try_zone(zone) mutex_try(&zone->lock)
262 kern_return_t
zget_space(
264 vm_offset_t
*result
);
266 decl_simple_lock_data(,zget_space_lock
)
267 vm_offset_t zalloc_next_space
;
268 vm_offset_t zalloc_end_of_space
;
269 vm_size_t zalloc_wasted_space
;
272 * Garbage collection map information
274 struct zone_page_table_entry
* zone_page_table
;
275 vm_offset_t zone_map_min_address
;
276 vm_offset_t zone_map_max_address
;
277 unsigned int zone_pages
;
280 * Exclude more than one concurrent garbage collection
282 decl_mutex_data(, zone_gc_lock
)
284 #define from_zone_map(addr, size) \
285 ((vm_offset_t)(addr) >= zone_map_min_address && \
286 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
288 #define ZONE_PAGE_USED 0
289 #define ZONE_PAGE_UNUSED -1
293 * Protects first_zone, last_zone, num_zones,
294 * and the next_zone field of zones.
296 decl_simple_lock_data(, all_zones_lock
)
299 unsigned int num_zones
;
301 boolean_t zone_gc_allowed
= TRUE
;
302 boolean_t zone_gc_forced
= FALSE
;
303 unsigned zone_gc_last_tick
= 0;
304 unsigned zone_gc_max_rate
= 0; /* in ticks */
308 * zinit initializes a new zone. The zone data structures themselves
309 * are stored in a zone, which is initially a static structure that
310 * is initialized by zone_init.
314 vm_size_t size
, /* the size of an element */
315 vm_size_t max
, /* maximum memory to use */
316 vm_size_t alloc
, /* allocation size */
317 const char *name
) /* a name for the zone */
321 if (zone_zone
== ZONE_NULL
) {
322 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
326 z
= (zone_t
) zalloc(zone_zone
);
331 * Round off all the parameters appropriately.
333 if (size
< sizeof(z
->free_elements
))
334 size
= sizeof(z
->free_elements
);
335 size
= ((size
-1) + sizeof(z
->free_elements
)) -
336 ((size
-1) % sizeof(z
->free_elements
));
339 alloc
= round_page(alloc
);
340 max
= round_page(max
);
342 * we look for an allocation size with less than 1% waste
343 * up to 5 pages in size...
344 * otherwise, we look for an allocation size with least fragmentation
345 * in the range of 1 - 5 pages
346 * This size will be used unless
347 * the user suggestion is larger AND has less fragmentation
349 { vm_size_t best
, waste
; unsigned int i
;
353 for (i
= 1; i
<= 5; i
++) {
354 vm_size_t tsize
, twaste
;
356 tsize
= i
* PAGE_SIZE
;
358 if ((tsize
% size
) < (tsize
/ 100)) {
360 goto use_this_allocation
;
362 twaste
= tsize
% size
;
364 best
= tsize
, waste
= twaste
;
366 if (alloc
<= best
|| (alloc
% size
>= waste
))
370 if (max
&& (max
< alloc
))
373 z
->free_elements
= 0;
377 z
->alloc_size
= alloc
;
380 z
->doing_alloc
= FALSE
;
382 z
->exhaustible
= FALSE
;
383 z
->collectable
= TRUE
;
384 z
->allows_foreign
= FALSE
;
385 z
->expandable
= TRUE
;
387 z
->async_pending
= FALSE
;
390 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
391 zone_debug_enable(z
);
392 #endif /* ZONE_DEBUG */
396 * Add the zone to the all-zones list.
399 z
->next_zone
= ZONE_NULL
;
400 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
401 simple_lock(&all_zones_lock
);
403 last_zone
= &z
->next_zone
;
405 simple_unlock(&all_zones_lock
);
411 * Cram the given memory into the specified zone.
415 register zone_t zone
,
419 register vm_size_t elem_size
;
420 vm_offset_t newmem
= (vm_offset_t
) newaddr
;
422 /* Basic sanity checks */
423 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
424 assert(!zone
->collectable
|| zone
->allows_foreign
425 || (from_zone_map(newmem
, size
)));
427 elem_size
= zone
->elem_size
;
430 while (size
>= elem_size
) {
431 ADD_TO_ZONE(zone
, newmem
);
432 if (from_zone_map(newmem
, elem_size
))
433 zone_page_alloc(newmem
, elem_size
);
434 zone
->count
++; /* compensate for ADD_TO_ZONE */
437 zone
->cur_size
+= elem_size
;
443 * Contiguous space allocator for non-paged zones. Allocates "size" amount
444 * of memory from zone_map.
452 vm_offset_t new_space
= 0;
453 vm_size_t space_to_add
= 0;
455 simple_lock(&zget_space_lock
);
456 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
458 * Add at least one page to allocation area.
461 space_to_add
= round_page(size
);
463 if (new_space
== 0) {
464 kern_return_t retval
;
466 * Memory cannot be wired down while holding
467 * any locks that the pageout daemon might
468 * need to free up pages. [Making the zget_space
469 * lock a complex lock does not help in this
472 * Unlock and allocate memory. Because several
473 * threads might try to do this at once, don't
474 * use the memory before checking for available
478 simple_unlock(&zget_space_lock
);
480 retval
= kernel_memory_allocate(zone_map
, &new_space
,
481 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
482 if (retval
!= KERN_SUCCESS
)
484 zone_page_init(new_space
, space_to_add
,
486 simple_lock(&zget_space_lock
);
492 * Memory was allocated in a previous iteration.
494 * Check whether the new region is contiguous
498 if (new_space
!= zalloc_end_of_space
) {
500 * Throw away the remainder of the
501 * old space, and start a new one.
503 zalloc_wasted_space
+=
504 zalloc_end_of_space
- zalloc_next_space
;
505 zalloc_next_space
= new_space
;
508 zalloc_end_of_space
= new_space
+ space_to_add
;
512 *result
= zalloc_next_space
;
513 zalloc_next_space
+= size
;
514 simple_unlock(&zget_space_lock
);
517 kmem_free(zone_map
, new_space
, space_to_add
);
519 return(KERN_SUCCESS
);
524 * Steal memory for the zone package. Called from
525 * vm_page_bootstrap().
528 zone_steal_memory(void)
530 zdata_size
= round_page(128*sizeof(struct zone
));
531 zdata
= (vm_offset_t
)((char *)pmap_steal_memory(zdata_size
) - (char *)0);
536 * Fill a zone with enough memory to contain at least nelem elements.
537 * Memory is obtained with kmem_alloc_wired from the kernel_map.
538 * Return the number of elements actually put into the zone, which may
539 * be more than the caller asked for since the memory allocation is
540 * rounded up to a full page.
555 size
= nelem
* zone
->elem_size
;
556 size
= round_page(size
);
557 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
558 if (kr
!= KERN_SUCCESS
)
561 zone_change(zone
, Z_FOREIGN
, TRUE
);
562 zcram(zone
, (void *)memory
, size
);
563 nalloc
= size
/ zone
->elem_size
;
564 assert(nalloc
>= nelem
);
570 * Initialize the "zone of zones" which uses fixed memory allocated
571 * earlier in memory initialization. zone_bootstrap is called
577 vm_size_t zone_zone_size
;
578 vm_offset_t zone_zone_space
;
580 simple_lock_init(&all_zones_lock
, 0);
582 first_zone
= ZONE_NULL
;
583 last_zone
= &first_zone
;
586 simple_lock_init(&zget_space_lock
, 0);
587 zalloc_next_space
= zdata
;
588 zalloc_end_of_space
= zdata
+ zdata_size
;
589 zalloc_wasted_space
= 0;
591 /* assertion: nobody else called zinit before us */
592 assert(zone_zone
== ZONE_NULL
);
593 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
594 sizeof(struct zone
), "zones");
595 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
596 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
597 zget_space(zone_zone_size
, &zone_zone_space
);
598 zcram(zone_zone
, (void *)zone_zone_space
, zone_zone_size
);
603 vm_size_t max_zonemap_size
)
605 kern_return_t retval
;
606 vm_offset_t zone_min
;
607 vm_offset_t zone_max
;
608 vm_size_t zone_table_size
;
610 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
611 FALSE
, VM_FLAGS_ANYWHERE
, &zone_map
);
613 if (retval
!= KERN_SUCCESS
)
614 panic("zone_init: kmem_suballoc failed");
615 zone_max
= zone_min
+ round_page(max_zonemap_size
);
617 * Setup garbage collection information:
619 zone_table_size
= atop_32(zone_max
- zone_min
) *
620 sizeof(struct zone_page_table_entry
);
621 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
622 zone_table_size
) != KERN_SUCCESS
)
624 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
625 zone_pages
= atop_32(zone_max
- zone_min
);
626 zone_map_min_address
= zone_min
;
627 zone_map_max_address
= zone_max
;
628 mutex_init(&zone_gc_lock
, 0);
629 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
634 * zalloc returns an element from the specified zone.
638 register zone_t zone
,
642 kern_return_t retval
;
644 assert(zone
!= ZONE_NULL
);
648 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
650 while ((addr
== 0) && canblock
&& (zone
->doing_gc
)) {
651 zone
->waiting
= TRUE
;
653 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
656 while ((addr
== 0) && canblock
) {
658 * If nothing was there, try to get more
660 if (zone
->doing_alloc
) {
662 * Someone is allocating memory for this zone.
663 * Wait for it to show up, then try again.
665 zone
->waiting
= TRUE
;
669 if ((zone
->cur_size
+ zone
->elem_size
) >
671 if (zone
->exhaustible
)
673 if (zone
->expandable
) {
675 * We're willing to overflow certain
676 * zones, but not without complaining.
678 * This is best used in conjunction
679 * with the collectable flag. What we
680 * want is an assurance we can get the
681 * memory back, assuming there's no
684 zone
->max_size
+= (zone
->max_size
>> 1);
688 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
691 zone
->doing_alloc
= TRUE
;
694 if (zone
->collectable
) {
696 vm_size_t alloc_size
;
697 boolean_t retry
= FALSE
;
701 if (vm_pool_low() || retry
== TRUE
)
703 round_page(zone
->elem_size
);
705 alloc_size
= zone
->alloc_size
;
707 retval
= kernel_memory_allocate(zone_map
,
708 &space
, alloc_size
, 0,
709 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
710 if (retval
== KERN_SUCCESS
) {
711 zone_page_init(space
, alloc_size
,
713 zcram(zone
, (void *)space
, alloc_size
);
716 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
717 /* would like to cause a zone_gc() */
719 panic("zalloc: \"%s\" (%d elements) retry fail %d", zone
->zone_name
, zone
->count
, retval
);
726 zone
->doing_alloc
= FALSE
;
728 zone
->waiting
= FALSE
;
731 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
733 retval
== KERN_RESOURCE_SHORTAGE
) {
741 retval
= zget_space(zone
->elem_size
, &space
);
744 zone
->doing_alloc
= FALSE
;
746 zone
->waiting
= FALSE
;
747 thread_wakeup((event_t
)zone
);
749 if (retval
== KERN_SUCCESS
) {
751 zone
->cur_size
+= zone
->elem_size
;
753 if (zone_debug_enabled(zone
)) {
754 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
758 zone_page_alloc(space
, zone
->elem_size
);
760 if (zone_debug_enabled(zone
))
761 space
+= ZONE_DEBUG_OFFSET
;
763 return((void *)space
);
765 if (retval
== KERN_RESOURCE_SHORTAGE
) {
771 panic("zalloc: \"%s\" (%d elements) zget_space returned %d", zone
->zone_name
, zone
->count
, retval
);
776 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
779 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
780 zone
->async_pending
= TRUE
;
782 thread_call_enter(&zone
->call_async_alloc
);
784 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
788 if (addr
&& zone_debug_enabled(zone
)) {
789 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
790 addr
+= ZONE_DEBUG_OFFSET
;
796 return((void *)addr
);
802 register zone_t zone
)
804 return( zalloc_canblock(zone
, TRUE
) );
809 register zone_t zone
)
811 return( zalloc_canblock(zone
, FALSE
) );
816 thread_call_param_t p0
,
817 __unused thread_call_param_t p1
)
821 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
822 zfree((zone_t
)p0
, elt
);
823 lock_zone(((zone_t
)p0
));
824 ((zone_t
)p0
)->async_pending
= FALSE
;
825 unlock_zone(((zone_t
)p0
));
830 * zget returns an element from the specified zone
831 * and immediately returns nothing if there is nothing there.
833 * This form should be used when you can not block (like when
834 * processing an interrupt).
838 register zone_t zone
)
840 register vm_offset_t addr
;
842 assert( zone
!= ZONE_NULL
);
844 if (!lock_try_zone(zone
))
847 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
849 if (addr
&& zone_debug_enabled(zone
)) {
850 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
851 addr
+= ZONE_DEBUG_OFFSET
;
853 #endif /* ZONE_DEBUG */
856 return((void *) addr
);
859 /* Keep this FALSE by default. Large memory machine run orders of magnitude
860 slower in debug mode when true. Use debugger to enable if needed */
861 /* static */ boolean_t zone_check
= FALSE
;
863 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
864 static vm_offset_t zone_last_bogus_elem
= 0;
868 register zone_t zone
,
871 vm_offset_t elem
= (vm_offset_t
) addr
;
874 /* Basic sanity checks */
875 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
876 panic("zfree: NULL");
877 /* zone_gc assumes zones are never freed */
878 if (zone
== zone_zone
)
879 panic("zfree: freeing to zone_zone breaks zone_gc!");
882 if (zone
->collectable
&& !zone
->allows_foreign
&&
883 !from_zone_map(elem
, zone
->elem_size
)) {
885 panic("zfree: non-allocated memory in collectable zone!");
887 zone_last_bogus_zone
= zone
;
888 zone_last_bogus_elem
= elem
;
894 if (zone_debug_enabled(zone
)) {
897 elem
-= ZONE_DEBUG_OFFSET
;
899 /* check the zone's consistency */
901 for (tmp_elem
= queue_first(&zone
->active_zones
);
902 !queue_end(tmp_elem
, &zone
->active_zones
);
903 tmp_elem
= queue_next(tmp_elem
))
904 if (elem
== (vm_offset_t
)tmp_elem
)
906 if (elem
!= (vm_offset_t
)tmp_elem
)
907 panic("zfree()ing element from wrong zone");
909 remqueue(&zone
->active_zones
, (queue_t
) elem
);
911 #endif /* ZONE_DEBUG */
915 /* check the zone's consistency */
917 for (this = zone
->free_elements
;
919 this = * (vm_offset_t
*) this)
920 if (!pmap_kernel_va(this) || this == elem
)
923 ADD_TO_ZONE(zone
, elem
);
926 * If elements have one or more pages, and memory is low,
927 * request to run the garbage collection in the zone the next
928 * time the pageout thread runs.
930 if (zone
->elem_size
>= PAGE_SIZE
&&
932 zone_gc_forced
= TRUE
;
938 /* Change a zone's flags.
939 * This routine must be called immediately after zinit.
947 assert( zone
!= ZONE_NULL
);
948 assert( value
== TRUE
|| value
== FALSE
);
952 zone
->exhaustible
= value
;
955 zone
->collectable
= value
;
958 zone
->expandable
= value
;
961 zone
->allows_foreign
= value
;
965 panic("Zone_change: Wrong Item Type!");
969 lock_zone_init(zone
);
973 * Return the expected number of free elements in the zone.
974 * This calculation will be incorrect if items are zfree'd that
975 * were never zalloc'd/zget'd. The correct way to stuff memory
976 * into a zone is by zcram.
980 zone_free_count(zone_t zone
)
982 integer_t free_count
;
985 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
988 assert(free_count
>= 0);
994 * zprealloc preallocates wired memory, exanding the specified
995 * zone to the specified size
1005 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
1007 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
1008 zcram(zone
, (void *)addr
, size
);
1013 * Zone garbage collection subroutines
1017 zone_page_collectable(
1021 struct zone_page_table_entry
*zp
;
1025 if (!from_zone_map(addr
, size
))
1026 panic("zone_page_collectable");
1029 i
= atop_32(addr
-zone_map_min_address
);
1030 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1032 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1033 if (zp
->collect_count
== zp
->alloc_count
)
1044 struct zone_page_table_entry
*zp
;
1048 if (!from_zone_map(addr
, size
))
1049 panic("zone_page_keep");
1052 i
= atop_32(addr
-zone_map_min_address
);
1053 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1055 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1056 zp
->collect_count
= 0;
1064 struct zone_page_table_entry
*zp
;
1068 if (!from_zone_map(addr
, size
))
1069 panic("zone_page_collect");
1072 i
= atop_32(addr
-zone_map_min_address
);
1073 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1075 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1076 ++zp
->collect_count
;
1085 struct zone_page_table_entry
*zp
;
1089 if (!from_zone_map(addr
, size
))
1090 panic("zone_page_init");
1093 i
= atop_32(addr
-zone_map_min_address
);
1094 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1096 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1097 zp
->alloc_count
= value
;
1098 zp
->collect_count
= 0;
1107 struct zone_page_table_entry
*zp
;
1111 if (!from_zone_map(addr
, size
))
1112 panic("zone_page_alloc");
1115 i
= atop_32(addr
-zone_map_min_address
);
1116 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1118 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1120 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1121 * it was previously set to ZONE_PAGE_UNUSED.
1123 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1124 zp
->alloc_count
= 1;
1131 zone_page_free_element(
1132 struct zone_page_table_entry
**free_pages
,
1136 struct zone_page_table_entry
*zp
;
1140 if (!from_zone_map(addr
, size
))
1141 panic("zone_page_free_element");
1144 i
= atop_32(addr
-zone_map_min_address
);
1145 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1147 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1148 if (zp
->collect_count
> 0)
1149 --zp
->collect_count
;
1150 if (--zp
->alloc_count
== 0) {
1151 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1152 zp
->collect_count
= 0;
1154 zp
->link
= *free_pages
;
1161 /* This is used for walking through a zone's free element list.
1163 struct zone_free_element
{
1164 struct zone_free_element
* next
;
1170 uint32_t elems_collected
,
1175 /* Zone garbage collection
1177 * zone_gc will walk through all the free elements in all the
1178 * zones that are marked collectable looking for reclaimable
1179 * pages. zone_gc is called by consider_zone_gc when the system
1180 * begins to run out of memory.
1185 unsigned int max_zones
;
1188 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1190 mutex_lock(&zone_gc_lock
);
1192 simple_lock(&all_zones_lock
);
1193 max_zones
= num_zones
;
1195 simple_unlock(&all_zones_lock
);
1198 for (i
= 0; i
< zone_pages
; i
++)
1199 assert(zone_page_table
[i
].collect_count
== 0);
1200 #endif /* MACH_ASSERT */
1202 zone_free_pages
= NULL
;
1204 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1206 vm_size_t elt_size
, size_freed
;
1207 struct zone_free_element
*elt
, *base_elt
, *base_prev
, *prev
, *scan
, *keep
, *tail
;
1209 assert(z
!= ZONE_NULL
);
1211 if (!z
->collectable
)
1216 elt_size
= z
->elem_size
;
1219 * Do a quick feasability check before we scan the zone:
1220 * skip unless there is likelihood of getting pages back
1221 * (i.e we need a whole allocation block's worth of free
1222 * elements before we can garbage collect) and
1223 * the zone has more than 10 percent of it's elements free
1225 if (((z
->cur_size
- z
->count
* elt_size
) <= (2 * z
->alloc_size
)) ||
1226 ((z
->cur_size
- z
->count
* elt_size
) <= (z
->cur_size
/ 10))) {
1234 * Snatch all of the free elements away from the zone.
1237 scan
= (void *)z
->free_elements
;
1238 (void *)z
->free_elements
= NULL
;
1245 * Determine which elements we can attempt to collect
1246 * and count them up in the page table. Foreign elements
1247 * are returned to the zone.
1250 prev
= (void *)&scan
;
1252 n
= 0; tail
= keep
= NULL
;
1253 while (elt
!= NULL
) {
1254 if (from_zone_map(elt
, elt_size
)) {
1255 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1260 ++zgc_stats
.elems_collected
;
1266 tail
= tail
->next
= elt
;
1268 elt
= prev
->next
= elt
->next
;
1273 * Dribble back the elements we are keeping.
1277 if (z
->waiting
== TRUE
) {
1281 tail
->next
= (void *)z
->free_elements
;
1282 (void *)z
->free_elements
= keep
;
1288 while ((elt
!= NULL
) && (++m
< 50)) {
1293 prev
->next
= (void *)z
->free_elements
;
1294 (void *)z
->free_elements
= (void *)base_elt
;
1295 base_prev
->next
= elt
;
1312 * Return any remaining elements.
1318 tail
->next
= (void *)z
->free_elements
;
1319 (void *)z
->free_elements
= keep
;
1327 * Determine which pages we can reclaim and
1328 * free those elements.
1332 prev
= (void *)&scan
;
1334 n
= 0; tail
= keep
= NULL
;
1335 while (elt
!= NULL
) {
1336 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1337 size_freed
+= elt_size
;
1338 zone_page_free_element(&zone_free_pages
,
1339 (vm_offset_t
)elt
, elt_size
);
1341 elt
= prev
->next
= elt
->next
;
1343 ++zgc_stats
.elems_freed
;
1346 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1351 tail
= tail
->next
= elt
;
1353 elt
= prev
->next
= elt
->next
;
1356 ++zgc_stats
.elems_kept
;
1360 * Dribble back the elements we are keeping,
1361 * and update the zone size info.
1367 z
->cur_size
-= size_freed
;
1371 tail
->next
= (void *)z
->free_elements
;
1372 (void *)z
->free_elements
= keep
;
1382 n
= 0; tail
= keep
= NULL
;
1387 * Return any remaining elements, and update
1388 * the zone size info.
1393 if (size_freed
> 0 || keep
!= NULL
) {
1395 z
->cur_size
-= size_freed
;
1398 tail
->next
= (void *)z
->free_elements
;
1399 (void *)z
->free_elements
= keep
;
1404 z
->doing_gc
= FALSE
;
1413 * Reclaim the pages we are freeing.
1416 while ((zp
= zone_free_pages
) != NULL
) {
1417 zone_free_pages
= zp
->link
;
1418 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1419 (zp
- zone_page_table
), PAGE_SIZE
);
1420 ++zgc_stats
.pgs_freed
;
1423 mutex_unlock(&zone_gc_lock
);
1429 * Called by the pageout daemon when the system needs more free pages.
1433 consider_zone_gc(void)
1436 * By default, don't attempt zone GC more frequently
1437 * than once / 1 minutes.
1440 if (zone_gc_max_rate
== 0)
1441 zone_gc_max_rate
= (60 << SCHED_TICK_SHIFT
) + 1;
1443 if (zone_gc_allowed
&&
1444 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1446 zone_gc_forced
= FALSE
;
1447 zone_gc_last_tick
= sched_tick
;
1456 zone_name_array_t
*namesp
,
1457 mach_msg_type_number_t
*namesCntp
,
1458 zone_info_array_t
*infop
,
1459 mach_msg_type_number_t
*infoCntp
)
1462 vm_offset_t names_addr
;
1463 vm_size_t names_size
;
1465 vm_offset_t info_addr
;
1466 vm_size_t info_size
;
1467 unsigned int max_zones
, i
;
1473 if (host
== HOST_NULL
)
1474 return KERN_INVALID_HOST
;
1477 * We assume that zones aren't freed once allocated.
1478 * We won't pick up any zones that are allocated later.
1481 simple_lock(&all_zones_lock
);
1483 max_zones
= num_zones
+ 4;
1485 max_zones
= num_zones
+ 2;
1488 simple_unlock(&all_zones_lock
);
1490 if (max_zones
<= *namesCntp
) {
1491 /* use in-line memory */
1492 names_size
= *namesCntp
* sizeof *names
;
1495 names_size
= round_page(max_zones
* sizeof *names
);
1496 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1497 &names_addr
, names_size
);
1498 if (kr
!= KERN_SUCCESS
)
1500 names
= (zone_name_t
*) names_addr
;
1503 if (max_zones
<= *infoCntp
) {
1504 /* use in-line memory */
1505 info_size
= *infoCntp
* sizeof *info
;
1508 info_size
= round_page(max_zones
* sizeof *info
);
1509 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1510 &info_addr
, info_size
);
1511 if (kr
!= KERN_SUCCESS
) {
1512 if (names
!= *namesp
)
1513 kmem_free(ipc_kernel_map
,
1514 names_addr
, names_size
);
1518 info
= (zone_info_t
*) info_addr
;
1523 for (i
= 0; i
< num_zones
; i
++) {
1526 assert(z
!= ZONE_NULL
);
1532 simple_lock(&all_zones_lock
);
1534 simple_unlock(&all_zones_lock
);
1536 /* assuming here the name data is static */
1537 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1538 sizeof zn
->zn_name
);
1540 zi
->zi_count
= zcopy
.count
;
1541 zi
->zi_cur_size
= zcopy
.cur_size
;
1542 zi
->zi_max_size
= zcopy
.max_size
;
1543 zi
->zi_elem_size
= zcopy
.elem_size
;
1544 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1545 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1546 zi
->zi_collectable
= zcopy
.collectable
;
1551 strcpy(zn
->zn_name
, "kernel_stacks");
1552 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1553 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1557 strcpy(zn
->zn_name
, "save_areas");
1558 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1559 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1563 strcpy(zn
->zn_name
, "pmap_mappings");
1564 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1565 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1569 strcpy(zn
->zn_name
, "kalloc.large");
1570 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1571 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1573 if (names
!= *namesp
) {
1577 used
= max_zones
* sizeof *names
;
1579 if (used
!= names_size
)
1580 bzero((char *) (names_addr
+ used
), names_size
- used
);
1582 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)names_addr
,
1583 (vm_map_size_t
)names_size
, TRUE
, ©
);
1584 assert(kr
== KERN_SUCCESS
);
1586 *namesp
= (zone_name_t
*) copy
;
1588 *namesCntp
= max_zones
;
1590 if (info
!= *infop
) {
1594 used
= max_zones
* sizeof *info
;
1596 if (used
!= info_size
)
1597 bzero((char *) (info_addr
+ used
), info_size
- used
);
1599 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)info_addr
,
1600 (vm_map_size_t
)info_size
, TRUE
, ©
);
1601 assert(kr
== KERN_SUCCESS
);
1603 *infop
= (zone_info_t
*) copy
;
1605 *infoCntp
= max_zones
;
1607 return KERN_SUCCESS
;
1611 #include <ddb/db_command.h>
1612 #include <ddb/db_output.h>
1613 #include <kern/kern_print.h>
1615 const char *zone_labels
=
1616 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1623 void db_zone_check_active(
1625 void db_zone_print_active(
1627 #endif /* ZONE_DEBUG */
1628 void db_zone_print_free(
1638 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1639 addr
, zcopy
.count
, zcopy
.cur_size
,
1640 zcopy
.max_size
, zcopy
.elem_size
,
1641 zcopy
.alloc_size
, zcopy
.zone_name
);
1642 if (zcopy
.exhaustible
)
1644 if (zcopy
.collectable
)
1646 if (zcopy
.expandable
)
1656 __unused db_expr_t count
,
1657 __unused
char * modif
)
1659 struct zone
*z
= (zone_t
)((char *)0 + addr
);
1661 if (z
== ZONE_NULL
|| !have_addr
){
1662 db_error("No Zone\n");
1666 db_printf("%s\n", zone_labels
);
1673 __unused db_expr_t addr
,
1676 __unused
char * modif
)
1682 * Don't risk hanging by unconditionally locking,
1683 * risk of incoherent data is small (zones aren't freed).
1685 have_addr
= simple_lock_try(&all_zones_lock
);
1689 simple_unlock(&all_zones_lock
);
1692 db_printf("%s\n", zone_labels
);
1693 for ( ; count
> 0; count
--) {
1695 db_error("Mangled Zone List\n");
1699 total
+= z
->cur_size
,
1701 have_addr
= simple_lock_try(&all_zones_lock
);
1704 simple_unlock(&all_zones_lock
);
1707 db_printf("\nTotal %8x", total
);
1708 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1713 db_zone_check_active(
1719 if (!zone_debug_enabled(zone
) || !zone_check
)
1721 tmp_elem
= queue_first(&zone
->active_zones
);
1722 while (count
< zone
->count
) {
1724 if (tmp_elem
== 0) {
1725 printf("unexpected zero element, zone=0x%x, count=%d\n",
1730 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1731 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1736 tmp_elem
= queue_next(tmp_elem
);
1738 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1739 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1746 db_zone_print_active(
1752 if (!zone_debug_enabled(zone
)) {
1753 printf("zone 0x%x debug not enabled\n", zone
);
1757 printf("zone_check FALSE\n");
1761 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1762 printf("active list:\n");
1763 tmp_elem
= queue_first(&zone
->active_zones
);
1764 while (count
< zone
->count
) {
1765 printf(" 0x%x", tmp_elem
);
1767 if ((count
% 6) == 0)
1769 if (tmp_elem
== 0) {
1770 printf("\nunexpected zero element, count=%d\n", count
);
1773 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1774 printf("\nunexpected queue_end, count=%d\n", count
);
1777 tmp_elem
= queue_next(tmp_elem
);
1779 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1780 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1784 #endif /* ZONE_DEBUG */
1794 freecount
= zone_free_count(zone
);
1795 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1796 printf("free list:\n");
1797 elem
= zone
->free_elements
;
1798 while (count
< freecount
) {
1799 printf(" 0x%x", elem
);
1801 if ((count
% 6) == 0)
1804 printf("\nunexpected zero element, count=%d\n", count
);
1807 elem
= *((vm_offset_t
*)elem
);
1810 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1815 #endif /* MACH_KDB */
1820 /* should we care about locks here ? */
1828 char *elt
= (char *)prev
;
1830 if (!zone_debug_enabled(z
))
1832 elt
-= ZONE_DEBUG_OFFSET
;
1833 elt
= (char *) queue_next((queue_t
) elt
);
1834 if ((queue_t
) elt
== &z
->active_zones
)
1836 elt
+= ZONE_DEBUG_OFFSET
;
1846 if (!zone_debug_enabled(z
))
1848 if (queue_empty(&z
->active_zones
))
1850 elt
= (char *)queue_first(&z
->active_zones
);
1851 elt
+= ZONE_DEBUG_OFFSET
;
1856 * Second arg controls how many zone elements are printed:
1859 * n, n > 0 => last n on active list
1868 boolean_t print
= (tail
!= 0);
1872 if (z
->count
< tail
)
1874 tail
= z
->count
- tail
;
1875 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1876 if (print
&& tail
<= count
)
1877 db_printf("%8x\n", elt
);
1880 assert(count
== z
->count
);
1883 #endif /* MACH_KDB */
1885 #define zone_in_use(z) ( z->count || z->free_elements )
1891 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1892 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
1894 queue_init(&z
->active_zones
);
1895 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
1902 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1904 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
1905 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1907 #endif /* ZONE_DEBUG */