2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
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>
69 #include <mach/mach_types.h>
70 #include <mach/vm_param.h>
71 #include <mach/kern_return.h>
72 #include <mach/mach_host_server.h>
73 #include <mach/machine/vm_types.h>
74 #include <mach_debug/zone_info.h>
76 #include <kern/kern_types.h>
77 #include <kern/assert.h>
78 #include <kern/host.h>
79 #include <kern/macro_help.h>
80 #include <kern/sched.h>
81 #include <kern/lock.h>
82 #include <kern/sched_prim.h>
83 #include <kern/misc_protos.h>
84 #include <kern/thread_call.h>
85 #include <kern/zalloc.h>
86 #include <kern/kalloc.h>
89 #include <vm/vm_map.h>
90 #include <vm/vm_kern.h>
91 #include <vm/vm_page.h>
93 #include <machine/machparam.h>
96 /* for fake zone stat routines */
97 #include <ppc/savearea.h>
98 #include <ppc/mappings.h>
102 /* Detect use of zone elt after freeing it by two methods:
103 * (1) Range-check the free-list "next" ptr for sanity.
104 * (2) Store the ptr in two different words, and compare them against
105 * each other when re-using the zone elt, to detect modifications;
110 #define is_kernel_data_addr(a) \
111 (!(a) || (IS_SYS_VA(a) && !((a) & (sizeof(long)-1))))
113 #else /* !defined(__alpha) */
115 #define is_kernel_data_addr(a) \
116 (!(a) || ((a) >= vm_min_kernel_address && !((a) & 0x3)))
118 #endif /* defined(__alpha) */
120 /* Should we set all words of the zone element to an illegal address
121 * when it is freed, to help catch usage after freeing? The down-side
122 * is that this obscures the identity of the freed element.
124 boolean_t zfree_clear
= FALSE
;
126 #define ADD_TO_ZONE(zone, element) \
131 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
133 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
135 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
136 (zone)->free_elements = (vm_offset_t) (element); \
140 #define REMOVE_FROM_ZONE(zone, ret, type) \
142 (ret) = (type) (zone)->free_elements; \
143 if ((ret) != (type) 0) { \
144 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
145 panic("A freed zone element has been modified.\n"); \
148 (zone)->free_elements = *((vm_offset_t *)(ret)); \
151 #else /* MACH_ASSERT */
153 #define ADD_TO_ZONE(zone, element) \
155 *((vm_offset_t *)(element)) = (zone)->free_elements; \
156 (zone)->free_elements = (vm_offset_t) (element); \
160 #define REMOVE_FROM_ZONE(zone, ret, type) \
162 (ret) = (type) (zone)->free_elements; \
163 if ((ret) != (type) 0) { \
165 (zone)->free_elements = *((vm_offset_t *)(ret)); \
169 #endif /* MACH_ASSERT */
172 #define zone_debug_enabled(z) z->active_zones.next
173 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
174 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
175 #endif /* ZONE_DEBUG */
178 * Support for garbage collection of unused zone pages:
181 struct zone_page_table_entry
{
182 struct zone_page_table_entry
*link
;
193 void zone_page_alloc(
197 void zone_page_free_element(
198 struct zone_page_table_entry
**free_pages
,
202 void zone_page_collect(
206 boolean_t
zone_page_collectable(
215 thread_call_param_t p0
,
216 thread_call_param_t p1
);
219 #if ZONE_DEBUG && MACH_KDB
223 #endif /* ZONE_DEBUG && MACH_KDB */
225 vm_map_t zone_map
= VM_MAP_NULL
;
227 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
230 * The VM system gives us an initial chunk of memory.
231 * It has to be big enough to allocate the zone_zone
235 vm_size_t zdata_size
;
237 #define lock_zone(zone) \
239 mutex_lock(&(zone)->lock); \
242 #define unlock_zone(zone) \
244 mutex_unlock(&(zone)->lock); \
247 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
248 #define zone_sleep(zone) \
249 thread_sleep_mutex((event_t)(zone), \
253 #define lock_zone_init(zone) \
255 mutex_init(&zone->lock, 0); \
258 #define lock_try_zone(zone) mutex_try(&zone->lock)
260 kern_return_t
zget_space(
262 vm_offset_t
*result
);
264 decl_simple_lock_data(,zget_space_lock
)
265 vm_offset_t zalloc_next_space
;
266 vm_offset_t zalloc_end_of_space
;
267 vm_size_t zalloc_wasted_space
;
270 * Garbage collection map information
272 struct zone_page_table_entry
* zone_page_table
;
273 vm_offset_t zone_map_min_address
;
274 vm_offset_t zone_map_max_address
;
275 unsigned int zone_pages
;
278 * Exclude more than one concurrent garbage collection
280 decl_mutex_data(, zone_gc_lock
)
282 #define from_zone_map(addr, size) \
283 ((vm_offset_t)(addr) >= zone_map_min_address && \
284 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
286 #define ZONE_PAGE_USED 0
287 #define ZONE_PAGE_UNUSED -1
291 * Protects first_zone, last_zone, num_zones,
292 * and the next_zone field of zones.
294 decl_simple_lock_data(, all_zones_lock
)
297 unsigned int num_zones
;
299 boolean_t zone_gc_allowed
= TRUE
;
300 boolean_t zone_gc_forced
= FALSE
;
301 unsigned zone_gc_last_tick
= 0;
302 unsigned zone_gc_max_rate
= 0; /* in ticks */
306 * zinit initializes a new zone. The zone data structures themselves
307 * are stored in a zone, which is initially a static structure that
308 * is initialized by zone_init.
312 vm_size_t size
, /* the size of an element */
313 vm_size_t max
, /* maximum memory to use */
314 vm_size_t alloc
, /* allocation size */
315 const char *name
) /* a name for the zone */
319 if (zone_zone
== ZONE_NULL
) {
320 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
324 z
= (zone_t
) zalloc(zone_zone
);
329 * Round off all the parameters appropriately.
331 if (size
< sizeof(z
->free_elements
))
332 size
= sizeof(z
->free_elements
);
333 size
= ((size
-1) + sizeof(z
->free_elements
)) -
334 ((size
-1) % sizeof(z
->free_elements
));
337 alloc
= round_page(alloc
);
338 max
= round_page(max
);
340 * we look for an allocation size with less than 1% waste
341 * up to 5 pages in size...
342 * otherwise, we look for an allocation size with least fragmentation
343 * in the range of 1 - 5 pages
344 * This size will be used unless
345 * the user suggestion is larger AND has less fragmentation
347 { vm_size_t best
, waste
; unsigned int i
;
351 for (i
= 1; i
<= 5; i
++) {
352 vm_size_t tsize
, twaste
;
354 tsize
= i
* PAGE_SIZE
;
356 if ((tsize
% size
) < (tsize
/ 100)) {
358 goto use_this_allocation
;
360 twaste
= tsize
% size
;
362 best
= tsize
, waste
= twaste
;
364 if (alloc
<= best
|| (alloc
% size
>= waste
))
368 if (max
&& (max
< alloc
))
371 z
->free_elements
= 0;
375 z
->alloc_size
= alloc
;
378 z
->doing_alloc
= FALSE
;
380 z
->exhaustible
= FALSE
;
381 z
->collectable
= TRUE
;
382 z
->allows_foreign
= FALSE
;
383 z
->expandable
= TRUE
;
385 z
->async_pending
= FALSE
;
388 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
389 zone_debug_enable(z
);
390 #endif /* ZONE_DEBUG */
394 * Add the zone to the all-zones list.
397 z
->next_zone
= ZONE_NULL
;
398 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
399 simple_lock(&all_zones_lock
);
401 last_zone
= &z
->next_zone
;
403 simple_unlock(&all_zones_lock
);
409 * Cram the given memory into the specified zone.
413 register zone_t zone
,
417 register vm_size_t elem_size
;
418 vm_offset_t newmem
= (vm_offset_t
) newaddr
;
420 /* Basic sanity checks */
421 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
422 assert(!zone
->collectable
|| zone
->allows_foreign
423 || (from_zone_map(newmem
, size
)));
425 elem_size
= zone
->elem_size
;
428 while (size
>= elem_size
) {
429 ADD_TO_ZONE(zone
, newmem
);
430 if (from_zone_map(newmem
, elem_size
))
431 zone_page_alloc(newmem
, elem_size
);
432 zone
->count
++; /* compensate for ADD_TO_ZONE */
435 zone
->cur_size
+= elem_size
;
441 * Contiguous space allocator for non-paged zones. Allocates "size" amount
442 * of memory from zone_map.
450 vm_offset_t new_space
= 0;
451 vm_size_t space_to_add
= 0;
453 simple_lock(&zget_space_lock
);
454 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
456 * Add at least one page to allocation area.
459 space_to_add
= round_page(size
);
461 if (new_space
== 0) {
462 kern_return_t retval
;
464 * Memory cannot be wired down while holding
465 * any locks that the pageout daemon might
466 * need to free up pages. [Making the zget_space
467 * lock a complex lock does not help in this
470 * Unlock and allocate memory. Because several
471 * threads might try to do this at once, don't
472 * use the memory before checking for available
476 simple_unlock(&zget_space_lock
);
478 retval
= kernel_memory_allocate(zone_map
, &new_space
,
479 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
480 if (retval
!= KERN_SUCCESS
)
482 zone_page_init(new_space
, space_to_add
,
484 simple_lock(&zget_space_lock
);
490 * Memory was allocated in a previous iteration.
492 * Check whether the new region is contiguous
496 if (new_space
!= zalloc_end_of_space
) {
498 * Throw away the remainder of the
499 * old space, and start a new one.
501 zalloc_wasted_space
+=
502 zalloc_end_of_space
- zalloc_next_space
;
503 zalloc_next_space
= new_space
;
506 zalloc_end_of_space
= new_space
+ space_to_add
;
510 *result
= zalloc_next_space
;
511 zalloc_next_space
+= size
;
512 simple_unlock(&zget_space_lock
);
515 kmem_free(zone_map
, new_space
, space_to_add
);
517 return(KERN_SUCCESS
);
522 * Steal memory for the zone package. Called from
523 * vm_page_bootstrap().
526 zone_steal_memory(void)
528 zdata_size
= round_page(128*sizeof(struct zone
));
529 zdata
= (vm_offset_t
)((char *)pmap_steal_memory(zdata_size
) - (char *)0);
534 * Fill a zone with enough memory to contain at least nelem elements.
535 * Memory is obtained with kmem_alloc_wired from the kernel_map.
536 * Return the number of elements actually put into the zone, which may
537 * be more than the caller asked for since the memory allocation is
538 * rounded up to a full page.
553 size
= nelem
* zone
->elem_size
;
554 size
= round_page(size
);
555 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
556 if (kr
!= KERN_SUCCESS
)
559 zone_change(zone
, Z_FOREIGN
, TRUE
);
560 zcram(zone
, (void *)memory
, size
);
561 nalloc
= size
/ zone
->elem_size
;
562 assert(nalloc
>= nelem
);
568 * Initialize the "zone of zones" which uses fixed memory allocated
569 * earlier in memory initialization. zone_bootstrap is called
575 vm_size_t zone_zone_size
;
576 vm_offset_t zone_zone_space
;
578 simple_lock_init(&all_zones_lock
, 0);
580 first_zone
= ZONE_NULL
;
581 last_zone
= &first_zone
;
584 simple_lock_init(&zget_space_lock
, 0);
585 zalloc_next_space
= zdata
;
586 zalloc_end_of_space
= zdata
+ zdata_size
;
587 zalloc_wasted_space
= 0;
589 /* assertion: nobody else called zinit before us */
590 assert(zone_zone
== ZONE_NULL
);
591 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
592 sizeof(struct zone
), "zones");
593 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
594 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
595 zget_space(zone_zone_size
, &zone_zone_space
);
596 zcram(zone_zone
, (void *)zone_zone_space
, zone_zone_size
);
601 vm_size_t max_zonemap_size
)
603 kern_return_t retval
;
604 vm_offset_t zone_min
;
605 vm_offset_t zone_max
;
606 vm_size_t zone_table_size
;
608 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
609 FALSE
, VM_FLAGS_ANYWHERE
, &zone_map
);
611 if (retval
!= KERN_SUCCESS
)
612 panic("zone_init: kmem_suballoc failed");
613 zone_max
= zone_min
+ round_page(max_zonemap_size
);
615 * Setup garbage collection information:
617 zone_table_size
= atop_32(zone_max
- zone_min
) *
618 sizeof(struct zone_page_table_entry
);
619 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
620 zone_table_size
) != KERN_SUCCESS
)
622 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
623 zone_pages
= atop_32(zone_max
- zone_min
);
624 zone_map_min_address
= zone_min
;
625 zone_map_max_address
= zone_max
;
626 mutex_init(&zone_gc_lock
, 0);
627 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
632 * zalloc returns an element from the specified zone.
636 register zone_t zone
,
640 kern_return_t retval
;
642 assert(zone
!= ZONE_NULL
);
646 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
648 while ((addr
== 0) && canblock
&& (zone
->doing_gc
)) {
649 zone
->waiting
= TRUE
;
651 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
654 while ((addr
== 0) && canblock
) {
656 * If nothing was there, try to get more
658 if (zone
->doing_alloc
) {
660 * Someone is allocating memory for this zone.
661 * Wait for it to show up, then try again.
663 zone
->waiting
= TRUE
;
667 if ((zone
->cur_size
+ zone
->elem_size
) >
669 if (zone
->exhaustible
)
671 if (zone
->expandable
) {
673 * We're willing to overflow certain
674 * zones, but not without complaining.
676 * This is best used in conjunction
677 * with the collectable flag. What we
678 * want is an assurance we can get the
679 * memory back, assuming there's no
682 zone
->max_size
+= (zone
->max_size
>> 1);
686 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
689 zone
->doing_alloc
= TRUE
;
692 if (zone
->collectable
) {
694 vm_size_t alloc_size
;
695 boolean_t retry
= FALSE
;
699 if (vm_pool_low() || retry
== TRUE
)
701 round_page(zone
->elem_size
);
703 alloc_size
= zone
->alloc_size
;
705 retval
= kernel_memory_allocate(zone_map
,
706 &space
, alloc_size
, 0,
707 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
708 if (retval
== KERN_SUCCESS
) {
709 zone_page_init(space
, alloc_size
,
711 zcram(zone
, (void *)space
, alloc_size
);
714 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
715 /* would like to cause a zone_gc() */
717 panic("zalloc: \"%s\" (%d elements) retry fail %d", zone
->zone_name
, zone
->count
, retval
);
724 zone
->doing_alloc
= FALSE
;
726 zone
->waiting
= FALSE
;
729 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
731 retval
== KERN_RESOURCE_SHORTAGE
) {
739 retval
= zget_space(zone
->elem_size
, &space
);
742 zone
->doing_alloc
= FALSE
;
744 zone
->waiting
= FALSE
;
745 thread_wakeup((event_t
)zone
);
747 if (retval
== KERN_SUCCESS
) {
749 zone
->cur_size
+= zone
->elem_size
;
751 if (zone_debug_enabled(zone
)) {
752 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
756 zone_page_alloc(space
, zone
->elem_size
);
758 if (zone_debug_enabled(zone
))
759 space
+= ZONE_DEBUG_OFFSET
;
761 return((void *)space
);
763 if (retval
== KERN_RESOURCE_SHORTAGE
) {
769 panic("zalloc: \"%s\" (%d elements) zget_space returned %d", zone
->zone_name
, zone
->count
, retval
);
774 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
777 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
778 zone
->async_pending
= TRUE
;
780 thread_call_enter(&zone
->call_async_alloc
);
782 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
786 if (addr
&& zone_debug_enabled(zone
)) {
787 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
788 addr
+= ZONE_DEBUG_OFFSET
;
794 return((void *)addr
);
800 register zone_t zone
)
802 return( zalloc_canblock(zone
, TRUE
) );
807 register zone_t zone
)
809 return( zalloc_canblock(zone
, FALSE
) );
814 thread_call_param_t p0
,
815 __unused thread_call_param_t p1
)
819 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
820 zfree((zone_t
)p0
, elt
);
821 lock_zone(((zone_t
)p0
));
822 ((zone_t
)p0
)->async_pending
= FALSE
;
823 unlock_zone(((zone_t
)p0
));
828 * zget returns an element from the specified zone
829 * and immediately returns nothing if there is nothing there.
831 * This form should be used when you can not block (like when
832 * processing an interrupt).
836 register zone_t zone
)
838 register vm_offset_t addr
;
840 assert( zone
!= ZONE_NULL
);
842 if (!lock_try_zone(zone
))
845 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
847 if (addr
&& zone_debug_enabled(zone
)) {
848 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
849 addr
+= ZONE_DEBUG_OFFSET
;
851 #endif /* ZONE_DEBUG */
854 return((void *) addr
);
857 /* Keep this FALSE by default. Large memory machine run orders of magnitude
858 slower in debug mode when true. Use debugger to enable if needed */
859 /* static */ boolean_t zone_check
= FALSE
;
861 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
862 static vm_offset_t zone_last_bogus_elem
= 0;
866 register zone_t zone
,
869 vm_offset_t elem
= (vm_offset_t
) addr
;
872 /* Basic sanity checks */
873 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
874 panic("zfree: NULL");
875 /* zone_gc assumes zones are never freed */
876 if (zone
== zone_zone
)
877 panic("zfree: freeing to zone_zone breaks zone_gc!");
880 if (zone
->collectable
&& !zone
->allows_foreign
&&
881 !from_zone_map(elem
, zone
->elem_size
)) {
883 panic("zfree: non-allocated memory in collectable zone!");
885 zone_last_bogus_zone
= zone
;
886 zone_last_bogus_elem
= elem
;
892 if (zone_debug_enabled(zone
)) {
895 elem
-= ZONE_DEBUG_OFFSET
;
897 /* check the zone's consistency */
899 for (tmp_elem
= queue_first(&zone
->active_zones
);
900 !queue_end(tmp_elem
, &zone
->active_zones
);
901 tmp_elem
= queue_next(tmp_elem
))
902 if (elem
== (vm_offset_t
)tmp_elem
)
904 if (elem
!= (vm_offset_t
)tmp_elem
)
905 panic("zfree()ing element from wrong zone");
907 remqueue(&zone
->active_zones
, (queue_t
) elem
);
909 #endif /* ZONE_DEBUG */
913 /* check the zone's consistency */
915 for (this = zone
->free_elements
;
917 this = * (vm_offset_t
*) this)
918 if (!pmap_kernel_va(this) || this == elem
)
921 ADD_TO_ZONE(zone
, elem
);
924 * If elements have one or more pages, and memory is low,
925 * request to run the garbage collection in the zone the next
926 * time the pageout thread runs.
928 if (zone
->elem_size
>= PAGE_SIZE
&&
930 zone_gc_forced
= TRUE
;
936 /* Change a zone's flags.
937 * This routine must be called immediately after zinit.
945 assert( zone
!= ZONE_NULL
);
946 assert( value
== TRUE
|| value
== FALSE
);
950 zone
->exhaustible
= value
;
953 zone
->collectable
= value
;
956 zone
->expandable
= value
;
959 zone
->allows_foreign
= value
;
963 panic("Zone_change: Wrong Item Type!");
967 lock_zone_init(zone
);
971 * Return the expected number of free elements in the zone.
972 * This calculation will be incorrect if items are zfree'd that
973 * were never zalloc'd/zget'd. The correct way to stuff memory
974 * into a zone is by zcram.
978 zone_free_count(zone_t zone
)
980 integer_t free_count
;
983 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
986 assert(free_count
>= 0);
992 * zprealloc preallocates wired memory, exanding the specified
993 * zone to the specified size
1003 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
1005 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
1006 zcram(zone
, (void *)addr
, size
);
1011 * Zone garbage collection subroutines
1015 zone_page_collectable(
1019 struct zone_page_table_entry
*zp
;
1023 if (!from_zone_map(addr
, size
))
1024 panic("zone_page_collectable");
1027 i
= atop_32(addr
-zone_map_min_address
);
1028 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1030 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1031 if (zp
->collect_count
== zp
->alloc_count
)
1042 struct zone_page_table_entry
*zp
;
1046 if (!from_zone_map(addr
, size
))
1047 panic("zone_page_keep");
1050 i
= atop_32(addr
-zone_map_min_address
);
1051 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1053 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1054 zp
->collect_count
= 0;
1062 struct zone_page_table_entry
*zp
;
1066 if (!from_zone_map(addr
, size
))
1067 panic("zone_page_collect");
1070 i
= atop_32(addr
-zone_map_min_address
);
1071 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1073 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1074 ++zp
->collect_count
;
1083 struct zone_page_table_entry
*zp
;
1087 if (!from_zone_map(addr
, size
))
1088 panic("zone_page_init");
1091 i
= atop_32(addr
-zone_map_min_address
);
1092 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1094 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1095 zp
->alloc_count
= value
;
1096 zp
->collect_count
= 0;
1105 struct zone_page_table_entry
*zp
;
1109 if (!from_zone_map(addr
, size
))
1110 panic("zone_page_alloc");
1113 i
= atop_32(addr
-zone_map_min_address
);
1114 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1116 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1118 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1119 * it was previously set to ZONE_PAGE_UNUSED.
1121 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1122 zp
->alloc_count
= 1;
1129 zone_page_free_element(
1130 struct zone_page_table_entry
**free_pages
,
1134 struct zone_page_table_entry
*zp
;
1138 if (!from_zone_map(addr
, size
))
1139 panic("zone_page_free_element");
1142 i
= atop_32(addr
-zone_map_min_address
);
1143 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1145 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1146 if (zp
->collect_count
> 0)
1147 --zp
->collect_count
;
1148 if (--zp
->alloc_count
== 0) {
1149 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1150 zp
->collect_count
= 0;
1152 zp
->link
= *free_pages
;
1159 /* This is used for walking through a zone's free element list.
1161 struct zone_free_element
{
1162 struct zone_free_element
* next
;
1168 uint32_t elems_collected
,
1173 /* Zone garbage collection
1175 * zone_gc will walk through all the free elements in all the
1176 * zones that are marked collectable looking for reclaimable
1177 * pages. zone_gc is called by consider_zone_gc when the system
1178 * begins to run out of memory.
1183 unsigned int max_zones
;
1186 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1188 mutex_lock(&zone_gc_lock
);
1190 simple_lock(&all_zones_lock
);
1191 max_zones
= num_zones
;
1193 simple_unlock(&all_zones_lock
);
1196 for (i
= 0; i
< zone_pages
; i
++)
1197 assert(zone_page_table
[i
].collect_count
== 0);
1198 #endif /* MACH_ASSERT */
1200 zone_free_pages
= NULL
;
1202 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1204 vm_size_t elt_size
, size_freed
;
1205 struct zone_free_element
*elt
, *base_elt
, *base_prev
, *prev
, *scan
, *keep
, *tail
;
1207 assert(z
!= ZONE_NULL
);
1209 if (!z
->collectable
)
1214 elt_size
= z
->elem_size
;
1217 * Do a quick feasability check before we scan the zone:
1218 * skip unless there is likelihood of getting pages back
1219 * (i.e we need a whole allocation block's worth of free
1220 * elements before we can garbage collect) and
1221 * the zone has more than 10 percent of it's elements free
1223 if (((z
->cur_size
- z
->count
* elt_size
) <= (2 * z
->alloc_size
)) ||
1224 ((z
->cur_size
- z
->count
* elt_size
) <= (z
->cur_size
/ 10))) {
1232 * Snatch all of the free elements away from the zone.
1235 scan
= (void *)z
->free_elements
;
1236 z
->free_elements
= 0;
1243 * Determine which elements we can attempt to collect
1244 * and count them up in the page table. Foreign elements
1245 * are returned to the zone.
1248 prev
= (void *)&scan
;
1250 n
= 0; tail
= keep
= NULL
;
1251 while (elt
!= NULL
) {
1252 if (from_zone_map(elt
, elt_size
)) {
1253 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1258 ++zgc_stats
.elems_collected
;
1264 tail
= tail
->next
= elt
;
1266 elt
= prev
->next
= elt
->next
;
1271 * Dribble back the elements we are keeping.
1275 if (z
->waiting
== TRUE
) {
1279 tail
->next
= (void *)z
->free_elements
;
1280 z
->free_elements
= (vm_offset_t
) keep
;
1286 while ((elt
!= NULL
) && (++m
< 50)) {
1291 prev
->next
= (void *)z
->free_elements
;
1292 z
->free_elements
= (vm_offset_t
) base_elt
;
1293 base_prev
->next
= elt
;
1310 * Return any remaining elements.
1316 tail
->next
= (void *)z
->free_elements
;
1317 z
->free_elements
= (vm_offset_t
) keep
;
1325 * Determine which pages we can reclaim and
1326 * free those elements.
1330 prev
= (void *)&scan
;
1332 n
= 0; tail
= keep
= NULL
;
1333 while (elt
!= NULL
) {
1334 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1335 size_freed
+= elt_size
;
1336 zone_page_free_element(&zone_free_pages
,
1337 (vm_offset_t
)elt
, elt_size
);
1339 elt
= prev
->next
= elt
->next
;
1341 ++zgc_stats
.elems_freed
;
1344 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1349 tail
= tail
->next
= elt
;
1351 elt
= prev
->next
= elt
->next
;
1354 ++zgc_stats
.elems_kept
;
1358 * Dribble back the elements we are keeping,
1359 * and update the zone size info.
1365 z
->cur_size
-= size_freed
;
1369 tail
->next
= (void *)z
->free_elements
;
1370 z
->free_elements
= (vm_offset_t
) keep
;
1380 n
= 0; tail
= keep
= NULL
;
1385 * Return any remaining elements, and update
1386 * the zone size info.
1391 if (size_freed
> 0 || keep
!= NULL
) {
1393 z
->cur_size
-= size_freed
;
1396 tail
->next
= (void *)z
->free_elements
;
1397 z
->free_elements
= (vm_offset_t
) keep
;
1402 z
->doing_gc
= FALSE
;
1411 * Reclaim the pages we are freeing.
1414 while ((zp
= zone_free_pages
) != NULL
) {
1415 zone_free_pages
= zp
->link
;
1416 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1417 (zp
- zone_page_table
), PAGE_SIZE
);
1418 ++zgc_stats
.pgs_freed
;
1421 mutex_unlock(&zone_gc_lock
);
1427 * Called by the pageout daemon when the system needs more free pages.
1431 consider_zone_gc(void)
1434 * By default, don't attempt zone GC more frequently
1435 * than once / 1 minutes.
1438 if (zone_gc_max_rate
== 0)
1439 zone_gc_max_rate
= (60 << SCHED_TICK_SHIFT
) + 1;
1441 if (zone_gc_allowed
&&
1442 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1444 zone_gc_forced
= FALSE
;
1445 zone_gc_last_tick
= sched_tick
;
1454 zone_name_array_t
*namesp
,
1455 mach_msg_type_number_t
*namesCntp
,
1456 zone_info_array_t
*infop
,
1457 mach_msg_type_number_t
*infoCntp
)
1460 vm_offset_t names_addr
;
1461 vm_size_t names_size
;
1463 vm_offset_t info_addr
;
1464 vm_size_t info_size
;
1465 unsigned int max_zones
, i
;
1471 if (host
== HOST_NULL
)
1472 return KERN_INVALID_HOST
;
1475 * We assume that zones aren't freed once allocated.
1476 * We won't pick up any zones that are allocated later.
1479 simple_lock(&all_zones_lock
);
1481 max_zones
= num_zones
+ 4;
1483 max_zones
= num_zones
+ 3; /* ATN: count the number below!! */
1486 simple_unlock(&all_zones_lock
);
1488 if (max_zones
<= *namesCntp
) {
1489 /* use in-line memory */
1490 names_size
= *namesCntp
* sizeof *names
;
1493 names_size
= round_page(max_zones
* sizeof *names
);
1494 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1495 &names_addr
, names_size
);
1496 if (kr
!= KERN_SUCCESS
)
1498 names
= (zone_name_t
*) names_addr
;
1501 if (max_zones
<= *infoCntp
) {
1502 /* use in-line memory */
1503 info_size
= *infoCntp
* sizeof *info
;
1506 info_size
= round_page(max_zones
* sizeof *info
);
1507 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1508 &info_addr
, info_size
);
1509 if (kr
!= KERN_SUCCESS
) {
1510 if (names
!= *namesp
)
1511 kmem_free(ipc_kernel_map
,
1512 names_addr
, names_size
);
1516 info
= (zone_info_t
*) info_addr
;
1521 for (i
= 0; i
< num_zones
; i
++) {
1524 assert(z
!= ZONE_NULL
);
1530 simple_lock(&all_zones_lock
);
1532 simple_unlock(&all_zones_lock
);
1534 /* assuming here the name data is static */
1535 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1536 sizeof zn
->zn_name
);
1538 zi
->zi_count
= zcopy
.count
;
1539 zi
->zi_cur_size
= zcopy
.cur_size
;
1540 zi
->zi_max_size
= zcopy
.max_size
;
1541 zi
->zi_elem_size
= zcopy
.elem_size
;
1542 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1543 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1544 zi
->zi_collectable
= zcopy
.collectable
;
1549 strcpy(zn
->zn_name
, "kernel_stacks");
1550 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1551 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1555 strcpy(zn
->zn_name
, "save_areas");
1556 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1557 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1561 strcpy(zn
->zn_name
, "pmap_mappings");
1562 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1563 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1569 strcpy(zn
->zn_name
, "page_tables");
1570 pt_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
);
1576 strcpy(zn
->zn_name
, "kalloc.large");
1577 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1578 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1580 if (names
!= *namesp
) {
1584 used
= max_zones
* sizeof *names
;
1586 if (used
!= names_size
)
1587 bzero((char *) (names_addr
+ used
), names_size
- used
);
1589 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)names_addr
,
1590 (vm_map_size_t
)names_size
, TRUE
, ©
);
1591 assert(kr
== KERN_SUCCESS
);
1593 *namesp
= (zone_name_t
*) copy
;
1595 *namesCntp
= max_zones
;
1597 if (info
!= *infop
) {
1601 used
= max_zones
* sizeof *info
;
1603 if (used
!= info_size
)
1604 bzero((char *) (info_addr
+ used
), info_size
- used
);
1606 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)info_addr
,
1607 (vm_map_size_t
)info_size
, TRUE
, ©
);
1608 assert(kr
== KERN_SUCCESS
);
1610 *infop
= (zone_info_t
*) copy
;
1612 *infoCntp
= max_zones
;
1614 return KERN_SUCCESS
;
1618 #include <ddb/db_command.h>
1619 #include <ddb/db_output.h>
1620 #include <kern/kern_print.h>
1622 const char *zone_labels
=
1623 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1630 void db_zone_check_active(
1632 void db_zone_print_active(
1634 #endif /* ZONE_DEBUG */
1635 void db_zone_print_free(
1645 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1646 addr
, zcopy
.count
, zcopy
.cur_size
,
1647 zcopy
.max_size
, zcopy
.elem_size
,
1648 zcopy
.alloc_size
, zcopy
.zone_name
);
1649 if (zcopy
.exhaustible
)
1651 if (zcopy
.collectable
)
1653 if (zcopy
.expandable
)
1663 __unused db_expr_t count
,
1664 __unused
char * modif
)
1666 struct zone
*z
= (zone_t
)((char *)0 + addr
);
1668 if (z
== ZONE_NULL
|| !have_addr
){
1669 db_error("No Zone\n");
1673 db_printf("%s\n", zone_labels
);
1680 __unused db_expr_t addr
,
1683 __unused
char * modif
)
1689 * Don't risk hanging by unconditionally locking,
1690 * risk of incoherent data is small (zones aren't freed).
1692 have_addr
= simple_lock_try(&all_zones_lock
);
1696 simple_unlock(&all_zones_lock
);
1699 db_printf("%s\n", zone_labels
);
1700 for ( ; count
> 0; count
--) {
1702 db_error("Mangled Zone List\n");
1706 total
+= z
->cur_size
,
1708 have_addr
= simple_lock_try(&all_zones_lock
);
1711 simple_unlock(&all_zones_lock
);
1714 db_printf("\nTotal %8x", total
);
1715 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1720 db_zone_check_active(
1726 if (!zone_debug_enabled(zone
) || !zone_check
)
1728 tmp_elem
= queue_first(&zone
->active_zones
);
1729 while (count
< zone
->count
) {
1731 if (tmp_elem
== 0) {
1732 printf("unexpected zero element, zone=0x%x, count=%d\n",
1737 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1738 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1743 tmp_elem
= queue_next(tmp_elem
);
1745 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1746 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1753 db_zone_print_active(
1759 if (!zone_debug_enabled(zone
)) {
1760 printf("zone 0x%x debug not enabled\n", zone
);
1764 printf("zone_check FALSE\n");
1768 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1769 printf("active list:\n");
1770 tmp_elem
= queue_first(&zone
->active_zones
);
1771 while (count
< zone
->count
) {
1772 printf(" 0x%x", tmp_elem
);
1774 if ((count
% 6) == 0)
1776 if (tmp_elem
== 0) {
1777 printf("\nunexpected zero element, count=%d\n", count
);
1780 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1781 printf("\nunexpected queue_end, count=%d\n", count
);
1784 tmp_elem
= queue_next(tmp_elem
);
1786 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1787 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1791 #endif /* ZONE_DEBUG */
1801 freecount
= zone_free_count(zone
);
1802 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1803 printf("free list:\n");
1804 elem
= zone
->free_elements
;
1805 while (count
< freecount
) {
1806 printf(" 0x%x", elem
);
1808 if ((count
% 6) == 0)
1811 printf("\nunexpected zero element, count=%d\n", count
);
1814 elem
= *((vm_offset_t
*)elem
);
1817 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1822 #endif /* MACH_KDB */
1827 /* should we care about locks here ? */
1835 char *elt
= (char *)prev
;
1837 if (!zone_debug_enabled(z
))
1839 elt
-= ZONE_DEBUG_OFFSET
;
1840 elt
= (char *) queue_next((queue_t
) elt
);
1841 if ((queue_t
) elt
== &z
->active_zones
)
1843 elt
+= ZONE_DEBUG_OFFSET
;
1853 if (!zone_debug_enabled(z
))
1855 if (queue_empty(&z
->active_zones
))
1857 elt
= (char *)queue_first(&z
->active_zones
);
1858 elt
+= ZONE_DEBUG_OFFSET
;
1863 * Second arg controls how many zone elements are printed:
1866 * n, n > 0 => last n on active list
1875 boolean_t print
= (tail
!= 0);
1879 if (z
->count
< tail
)
1881 tail
= z
->count
- tail
;
1882 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1883 if (print
&& tail
<= count
)
1884 db_printf("%8x\n", elt
);
1887 assert(count
== z
->count
);
1890 #endif /* MACH_KDB */
1892 #define zone_in_use(z) ( z->count || z->free_elements )
1898 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1899 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
1901 queue_init(&z
->active_zones
);
1902 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
1909 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1911 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
1912 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1914 #endif /* ZONE_DEBUG */