2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr.
56 * Zone-based memory allocator. A zone is a collection of fixed size
57 * data blocks for which quick allocation/deallocation is possible.
59 #include <zone_debug.h>
63 #include <mach/mach_types.h>
64 #include <mach/vm_param.h>
65 #include <mach/kern_return.h>
66 #include <mach/mach_host_server.h>
67 #include <mach/machine/vm_types.h>
68 #include <mach_debug/zone_info.h>
70 #include <kern/kern_types.h>
71 #include <kern/assert.h>
72 #include <kern/host.h>
73 #include <kern/macro_help.h>
74 #include <kern/sched.h>
75 #include <kern/lock.h>
76 #include <kern/sched_prim.h>
77 #include <kern/misc_protos.h>
78 #include <kern/thread_call.h>
79 #include <kern/zalloc.h>
80 #include <kern/kalloc.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_kern.h>
85 #include <vm/vm_page.h>
87 #include <machine/machparam.h>
90 /* for fake zone stat routines */
91 #include <ppc/savearea.h>
92 #include <ppc/mappings.h>
96 /* Detect use of zone elt after freeing it by two methods:
97 * (1) Range-check the free-list "next" ptr for sanity.
98 * (2) Store the ptr in two different words, and compare them against
99 * each other when re-using the zone elt, to detect modifications;
104 #define is_kernel_data_addr(a) \
105 (!(a) || (IS_SYS_VA(a) && !((a) & (sizeof(long)-1))))
107 #else /* !defined(__alpha) */
109 #define is_kernel_data_addr(a) \
110 (!(a) || ((a) >= VM_MIN_KERNEL_ADDRESS && !((a) & 0x3)))
112 #endif /* defined(__alpha) */
114 /* Should we set all words of the zone element to an illegal address
115 * when it is freed, to help catch usage after freeing? The down-side
116 * is that this obscures the identity of the freed element.
118 boolean_t zfree_clear
= FALSE
;
120 #define ADD_TO_ZONE(zone, element) \
125 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
127 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
129 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
130 (zone)->free_elements = (vm_offset_t) (element); \
134 #define REMOVE_FROM_ZONE(zone, ret, type) \
136 (ret) = (type) (zone)->free_elements; \
137 if ((ret) != (type) 0) { \
138 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
139 panic("A freed zone element has been modified.\n"); \
142 (zone)->free_elements = *((vm_offset_t *)(ret)); \
145 #else /* MACH_ASSERT */
147 #define ADD_TO_ZONE(zone, element) \
149 *((vm_offset_t *)(element)) = (zone)->free_elements; \
150 (zone)->free_elements = (vm_offset_t) (element); \
154 #define REMOVE_FROM_ZONE(zone, ret, type) \
156 (ret) = (type) (zone)->free_elements; \
157 if ((ret) != (type) 0) { \
159 (zone)->free_elements = *((vm_offset_t *)(ret)); \
163 #endif /* MACH_ASSERT */
166 #define zone_debug_enabled(z) z->active_zones.next
167 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
168 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
169 #endif /* ZONE_DEBUG */
172 * Support for garbage collection of unused zone pages:
175 struct zone_page_table_entry
{
176 struct zone_page_table_entry
*link
;
187 void zone_page_alloc(
191 void zone_page_free_element(
192 struct zone_page_table_entry
**free_pages
,
196 void zone_page_collect(
200 boolean_t
zone_page_collectable(
209 thread_call_param_t p0
,
210 thread_call_param_t p1
);
213 #if ZONE_DEBUG && MACH_KDB
217 #endif /* ZONE_DEBUG && MACH_KDB */
219 vm_map_t zone_map
= VM_MAP_NULL
;
221 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
224 * The VM system gives us an initial chunk of memory.
225 * It has to be big enough to allocate the zone_zone
229 vm_size_t zdata_size
;
231 #define lock_zone(zone) \
233 mutex_lock(&(zone)->lock); \
236 #define unlock_zone(zone) \
238 mutex_unlock(&(zone)->lock); \
241 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
242 #define zone_sleep(zone) \
243 thread_sleep_mutex((event_t)(zone), \
247 #define lock_zone_init(zone) \
249 mutex_init(&zone->lock, 0); \
252 #define lock_try_zone(zone) mutex_try(&zone->lock)
254 kern_return_t
zget_space(
256 vm_offset_t
*result
);
258 decl_simple_lock_data(,zget_space_lock
)
259 vm_offset_t zalloc_next_space
;
260 vm_offset_t zalloc_end_of_space
;
261 vm_size_t zalloc_wasted_space
;
264 * Garbage collection map information
266 struct zone_page_table_entry
* zone_page_table
;
267 vm_offset_t zone_map_min_address
;
268 vm_offset_t zone_map_max_address
;
269 unsigned int zone_pages
;
272 * Exclude more than one concurrent garbage collection
274 decl_mutex_data(, zone_gc_lock
)
276 #define from_zone_map(addr, size) \
277 ((vm_offset_t)(addr) >= zone_map_min_address && \
278 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
280 #define ZONE_PAGE_USED 0
281 #define ZONE_PAGE_UNUSED -1
285 * Protects first_zone, last_zone, num_zones,
286 * and the next_zone field of zones.
288 decl_simple_lock_data(, all_zones_lock
)
291 unsigned int num_zones
;
293 boolean_t zone_gc_allowed
= TRUE
;
294 boolean_t zone_gc_forced
= FALSE
;
295 unsigned zone_gc_last_tick
= 0;
296 unsigned zone_gc_max_rate
= 0; /* in ticks */
300 * zinit initializes a new zone. The zone data structures themselves
301 * are stored in a zone, which is initially a static structure that
302 * is initialized by zone_init.
306 vm_size_t size
, /* the size of an element */
307 vm_size_t max
, /* maximum memory to use */
308 vm_size_t alloc
, /* allocation size */
309 const char *name
) /* a name for the zone */
313 if (zone_zone
== ZONE_NULL
) {
314 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
318 z
= (zone_t
) zalloc(zone_zone
);
323 * Round off all the parameters appropriately.
325 if (size
< sizeof(z
->free_elements
))
326 size
= sizeof(z
->free_elements
);
327 size
= ((size
-1) + sizeof(z
->free_elements
)) -
328 ((size
-1) % sizeof(z
->free_elements
));
331 alloc
= round_page(alloc
);
332 max
= round_page(max
);
334 * we look for an allocation size with less than 1% waste
335 * up to 5 pages in size...
336 * otherwise, we look for an allocation size with least fragmentation
337 * in the range of 1 - 5 pages
338 * This size will be used unless
339 * the user suggestion is larger AND has less fragmentation
341 { vm_size_t best
, waste
; unsigned int i
;
345 for (i
= 1; i
<= 5; i
++) {
346 vm_size_t tsize
, twaste
;
348 tsize
= i
* PAGE_SIZE
;
350 if ((tsize
% size
) < (tsize
/ 100)) {
352 goto use_this_allocation
;
354 twaste
= tsize
% size
;
356 best
= tsize
, waste
= twaste
;
358 if (alloc
<= best
|| (alloc
% size
>= waste
))
362 if (max
&& (max
< alloc
))
365 z
->free_elements
= 0;
369 z
->alloc_size
= alloc
;
372 z
->doing_alloc
= FALSE
;
374 z
->exhaustible
= FALSE
;
375 z
->collectable
= TRUE
;
376 z
->allows_foreign
= FALSE
;
377 z
->expandable
= TRUE
;
379 z
->async_pending
= FALSE
;
382 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
383 zone_debug_enable(z
);
384 #endif /* ZONE_DEBUG */
388 * Add the zone to the all-zones list.
391 z
->next_zone
= ZONE_NULL
;
392 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
393 simple_lock(&all_zones_lock
);
395 last_zone
= &z
->next_zone
;
397 simple_unlock(&all_zones_lock
);
403 * Cram the given memory into the specified zone.
407 register zone_t zone
,
411 register vm_size_t elem_size
;
412 vm_offset_t newmem
= (vm_offset_t
) newaddr
;
414 /* Basic sanity checks */
415 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
416 assert(!zone
->collectable
|| zone
->allows_foreign
417 || (from_zone_map(newmem
, size
)));
419 elem_size
= zone
->elem_size
;
422 while (size
>= elem_size
) {
423 ADD_TO_ZONE(zone
, newmem
);
424 if (from_zone_map(newmem
, elem_size
))
425 zone_page_alloc(newmem
, elem_size
);
426 zone
->count
++; /* compensate for ADD_TO_ZONE */
429 zone
->cur_size
+= elem_size
;
435 * Contiguous space allocator for non-paged zones. Allocates "size" amount
436 * of memory from zone_map.
444 vm_offset_t new_space
= 0;
445 vm_size_t space_to_add
= 0;
447 simple_lock(&zget_space_lock
);
448 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
450 * Add at least one page to allocation area.
453 space_to_add
= round_page(size
);
455 if (new_space
== 0) {
456 kern_return_t retval
;
458 * Memory cannot be wired down while holding
459 * any locks that the pageout daemon might
460 * need to free up pages. [Making the zget_space
461 * lock a complex lock does not help in this
464 * Unlock and allocate memory. Because several
465 * threads might try to do this at once, don't
466 * use the memory before checking for available
470 simple_unlock(&zget_space_lock
);
472 retval
= kernel_memory_allocate(zone_map
, &new_space
,
473 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
474 if (retval
!= KERN_SUCCESS
)
476 zone_page_init(new_space
, space_to_add
,
478 simple_lock(&zget_space_lock
);
484 * Memory was allocated in a previous iteration.
486 * Check whether the new region is contiguous
490 if (new_space
!= zalloc_end_of_space
) {
492 * Throw away the remainder of the
493 * old space, and start a new one.
495 zalloc_wasted_space
+=
496 zalloc_end_of_space
- zalloc_next_space
;
497 zalloc_next_space
= new_space
;
500 zalloc_end_of_space
= new_space
+ space_to_add
;
504 *result
= zalloc_next_space
;
505 zalloc_next_space
+= size
;
506 simple_unlock(&zget_space_lock
);
509 kmem_free(zone_map
, new_space
, space_to_add
);
511 return(KERN_SUCCESS
);
516 * Steal memory for the zone package. Called from
517 * vm_page_bootstrap().
520 zone_steal_memory(void)
522 zdata_size
= round_page(128*sizeof(struct zone
));
523 zdata
= (vm_offset_t
)((char *)pmap_steal_memory(zdata_size
) - (char *)0);
528 * Fill a zone with enough memory to contain at least nelem elements.
529 * Memory is obtained with kmem_alloc_wired from the kernel_map.
530 * Return the number of elements actually put into the zone, which may
531 * be more than the caller asked for since the memory allocation is
532 * rounded up to a full page.
547 size
= nelem
* zone
->elem_size
;
548 size
= round_page(size
);
549 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
550 if (kr
!= KERN_SUCCESS
)
553 zone_change(zone
, Z_FOREIGN
, TRUE
);
554 zcram(zone
, (void *)memory
, size
);
555 nalloc
= size
/ zone
->elem_size
;
556 assert(nalloc
>= nelem
);
562 * Initialize the "zone of zones" which uses fixed memory allocated
563 * earlier in memory initialization. zone_bootstrap is called
569 vm_size_t zone_zone_size
;
570 vm_offset_t zone_zone_space
;
572 simple_lock_init(&all_zones_lock
, 0);
574 first_zone
= ZONE_NULL
;
575 last_zone
= &first_zone
;
578 simple_lock_init(&zget_space_lock
, 0);
579 zalloc_next_space
= zdata
;
580 zalloc_end_of_space
= zdata
+ zdata_size
;
581 zalloc_wasted_space
= 0;
583 /* assertion: nobody else called zinit before us */
584 assert(zone_zone
== ZONE_NULL
);
585 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
586 sizeof(struct zone
), "zones");
587 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
588 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
589 zget_space(zone_zone_size
, &zone_zone_space
);
590 zcram(zone_zone
, (void *)zone_zone_space
, zone_zone_size
);
595 vm_size_t max_zonemap_size
)
597 kern_return_t retval
;
598 vm_offset_t zone_min
;
599 vm_offset_t zone_max
;
600 vm_size_t zone_table_size
;
602 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
603 FALSE
, VM_FLAGS_ANYWHERE
, &zone_map
);
605 if (retval
!= KERN_SUCCESS
)
606 panic("zone_init: kmem_suballoc failed");
607 zone_max
= zone_min
+ round_page(max_zonemap_size
);
609 * Setup garbage collection information:
611 zone_table_size
= atop_32(zone_max
- zone_min
) *
612 sizeof(struct zone_page_table_entry
);
613 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
614 zone_table_size
) != KERN_SUCCESS
)
616 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
617 zone_pages
= atop_32(zone_max
- zone_min
);
618 zone_map_min_address
= zone_min
;
619 zone_map_max_address
= zone_max
;
620 mutex_init(&zone_gc_lock
, 0);
621 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
626 * zalloc returns an element from the specified zone.
630 register zone_t zone
,
634 kern_return_t retval
;
636 assert(zone
!= ZONE_NULL
);
640 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
642 while ((addr
== 0) && canblock
&& (zone
->doing_gc
)) {
643 zone
->waiting
= TRUE
;
645 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
648 while ((addr
== 0) && canblock
) {
650 * If nothing was there, try to get more
652 if (zone
->doing_alloc
) {
654 * Someone is allocating memory for this zone.
655 * Wait for it to show up, then try again.
657 zone
->waiting
= TRUE
;
661 if ((zone
->cur_size
+ zone
->elem_size
) >
663 if (zone
->exhaustible
)
665 if (zone
->expandable
) {
667 * We're willing to overflow certain
668 * zones, but not without complaining.
670 * This is best used in conjunction
671 * with the collectable flag. What we
672 * want is an assurance we can get the
673 * memory back, assuming there's no
676 zone
->max_size
+= (zone
->max_size
>> 1);
680 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
683 zone
->doing_alloc
= TRUE
;
686 if (zone
->collectable
) {
688 vm_size_t alloc_size
;
689 boolean_t retry
= FALSE
;
693 if (vm_pool_low() || retry
== TRUE
)
695 round_page(zone
->elem_size
);
697 alloc_size
= zone
->alloc_size
;
699 retval
= kernel_memory_allocate(zone_map
,
700 &space
, alloc_size
, 0,
701 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
702 if (retval
== KERN_SUCCESS
) {
703 zone_page_init(space
, alloc_size
,
705 zcram(zone
, (void *)space
, alloc_size
);
708 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
709 /* would like to cause a zone_gc() */
711 panic("zalloc: \"%s\" (%d elements) retry fail %d", zone
->zone_name
, zone
->count
, retval
);
718 zone
->doing_alloc
= FALSE
;
720 zone
->waiting
= FALSE
;
723 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
725 retval
== KERN_RESOURCE_SHORTAGE
) {
733 retval
= zget_space(zone
->elem_size
, &space
);
736 zone
->doing_alloc
= FALSE
;
738 zone
->waiting
= FALSE
;
739 thread_wakeup((event_t
)zone
);
741 if (retval
== KERN_SUCCESS
) {
743 zone
->cur_size
+= zone
->elem_size
;
745 if (zone_debug_enabled(zone
)) {
746 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
750 zone_page_alloc(space
, zone
->elem_size
);
752 if (zone_debug_enabled(zone
))
753 space
+= ZONE_DEBUG_OFFSET
;
755 return((void *)space
);
757 if (retval
== KERN_RESOURCE_SHORTAGE
) {
763 panic("zalloc: \"%s\" (%d elements) zget_space returned %d", zone
->zone_name
, zone
->count
, retval
);
768 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
771 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
772 zone
->async_pending
= TRUE
;
774 thread_call_enter(&zone
->call_async_alloc
);
776 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
780 if (addr
&& zone_debug_enabled(zone
)) {
781 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
782 addr
+= ZONE_DEBUG_OFFSET
;
788 return((void *)addr
);
794 register zone_t zone
)
796 return( zalloc_canblock(zone
, TRUE
) );
801 register zone_t zone
)
803 return( zalloc_canblock(zone
, FALSE
) );
808 thread_call_param_t p0
,
809 __unused thread_call_param_t p1
)
813 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
814 zfree((zone_t
)p0
, elt
);
815 lock_zone(((zone_t
)p0
));
816 ((zone_t
)p0
)->async_pending
= FALSE
;
817 unlock_zone(((zone_t
)p0
));
822 * zget returns an element from the specified zone
823 * and immediately returns nothing if there is nothing there.
825 * This form should be used when you can not block (like when
826 * processing an interrupt).
830 register zone_t zone
)
832 register vm_offset_t addr
;
834 assert( zone
!= ZONE_NULL
);
836 if (!lock_try_zone(zone
))
839 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
841 if (addr
&& zone_debug_enabled(zone
)) {
842 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
843 addr
+= ZONE_DEBUG_OFFSET
;
845 #endif /* ZONE_DEBUG */
848 return((void *) addr
);
851 /* Keep this FALSE by default. Large memory machine run orders of magnitude
852 slower in debug mode when true. Use debugger to enable if needed */
853 /* static */ boolean_t zone_check
= FALSE
;
855 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
856 static vm_offset_t zone_last_bogus_elem
= 0;
860 register zone_t zone
,
863 vm_offset_t elem
= (vm_offset_t
) addr
;
866 /* Basic sanity checks */
867 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
868 panic("zfree: NULL");
869 /* zone_gc assumes zones are never freed */
870 if (zone
== zone_zone
)
871 panic("zfree: freeing to zone_zone breaks zone_gc!");
874 if (zone
->collectable
&& !zone
->allows_foreign
&&
875 !from_zone_map(elem
, zone
->elem_size
)) {
877 panic("zfree: non-allocated memory in collectable zone!");
879 zone_last_bogus_zone
= zone
;
880 zone_last_bogus_elem
= elem
;
886 if (zone_debug_enabled(zone
)) {
889 elem
-= ZONE_DEBUG_OFFSET
;
891 /* check the zone's consistency */
893 for (tmp_elem
= queue_first(&zone
->active_zones
);
894 !queue_end(tmp_elem
, &zone
->active_zones
);
895 tmp_elem
= queue_next(tmp_elem
))
896 if (elem
== (vm_offset_t
)tmp_elem
)
898 if (elem
!= (vm_offset_t
)tmp_elem
)
899 panic("zfree()ing element from wrong zone");
901 remqueue(&zone
->active_zones
, (queue_t
) elem
);
903 #endif /* ZONE_DEBUG */
907 /* check the zone's consistency */
909 for (this = zone
->free_elements
;
911 this = * (vm_offset_t
*) this)
912 if (!pmap_kernel_va(this) || this == elem
)
915 ADD_TO_ZONE(zone
, elem
);
918 * If elements have one or more pages, and memory is low,
919 * request to run the garbage collection in the zone the next
920 * time the pageout thread runs.
922 if (zone
->elem_size
>= PAGE_SIZE
&&
924 zone_gc_forced
= TRUE
;
930 /* Change a zone's flags.
931 * This routine must be called immediately after zinit.
939 assert( zone
!= ZONE_NULL
);
940 assert( value
== TRUE
|| value
== FALSE
);
944 zone
->exhaustible
= value
;
947 zone
->collectable
= value
;
950 zone
->expandable
= value
;
953 zone
->allows_foreign
= value
;
957 panic("Zone_change: Wrong Item Type!");
961 lock_zone_init(zone
);
965 * Return the expected number of free elements in the zone.
966 * This calculation will be incorrect if items are zfree'd that
967 * were never zalloc'd/zget'd. The correct way to stuff memory
968 * into a zone is by zcram.
972 zone_free_count(zone_t zone
)
974 integer_t free_count
;
977 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
980 assert(free_count
>= 0);
986 * zprealloc preallocates wired memory, exanding the specified
987 * zone to the specified size
997 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
999 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
1000 zcram(zone
, (void *)addr
, size
);
1005 * Zone garbage collection subroutines
1009 zone_page_collectable(
1013 struct zone_page_table_entry
*zp
;
1017 if (!from_zone_map(addr
, size
))
1018 panic("zone_page_collectable");
1021 i
= atop_32(addr
-zone_map_min_address
);
1022 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1024 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1025 if (zp
->collect_count
== zp
->alloc_count
)
1036 struct zone_page_table_entry
*zp
;
1040 if (!from_zone_map(addr
, size
))
1041 panic("zone_page_keep");
1044 i
= atop_32(addr
-zone_map_min_address
);
1045 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1047 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1048 zp
->collect_count
= 0;
1056 struct zone_page_table_entry
*zp
;
1060 if (!from_zone_map(addr
, size
))
1061 panic("zone_page_collect");
1064 i
= atop_32(addr
-zone_map_min_address
);
1065 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1067 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1068 ++zp
->collect_count
;
1077 struct zone_page_table_entry
*zp
;
1081 if (!from_zone_map(addr
, size
))
1082 panic("zone_page_init");
1085 i
= atop_32(addr
-zone_map_min_address
);
1086 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1088 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1089 zp
->alloc_count
= value
;
1090 zp
->collect_count
= 0;
1099 struct zone_page_table_entry
*zp
;
1103 if (!from_zone_map(addr
, size
))
1104 panic("zone_page_alloc");
1107 i
= atop_32(addr
-zone_map_min_address
);
1108 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1110 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1112 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1113 * it was previously set to ZONE_PAGE_UNUSED.
1115 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1116 zp
->alloc_count
= 1;
1123 zone_page_free_element(
1124 struct zone_page_table_entry
**free_pages
,
1128 struct zone_page_table_entry
*zp
;
1132 if (!from_zone_map(addr
, size
))
1133 panic("zone_page_free_element");
1136 i
= atop_32(addr
-zone_map_min_address
);
1137 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1139 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1140 if (zp
->collect_count
> 0)
1141 --zp
->collect_count
;
1142 if (--zp
->alloc_count
== 0) {
1143 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1144 zp
->collect_count
= 0;
1146 zp
->link
= *free_pages
;
1153 /* This is used for walking through a zone's free element list.
1155 struct zone_free_element
{
1156 struct zone_free_element
* next
;
1162 uint32_t elems_collected
,
1167 /* Zone garbage collection
1169 * zone_gc will walk through all the free elements in all the
1170 * zones that are marked collectable looking for reclaimable
1171 * pages. zone_gc is called by consider_zone_gc when the system
1172 * begins to run out of memory.
1177 unsigned int max_zones
;
1180 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1182 mutex_lock(&zone_gc_lock
);
1184 simple_lock(&all_zones_lock
);
1185 max_zones
= num_zones
;
1187 simple_unlock(&all_zones_lock
);
1190 for (i
= 0; i
< zone_pages
; i
++)
1191 assert(zone_page_table
[i
].collect_count
== 0);
1192 #endif /* MACH_ASSERT */
1194 zone_free_pages
= NULL
;
1196 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1198 vm_size_t elt_size
, size_freed
;
1199 struct zone_free_element
*elt
, *base_elt
, *base_prev
, *prev
, *scan
, *keep
, *tail
;
1201 assert(z
!= ZONE_NULL
);
1203 if (!z
->collectable
)
1208 elt_size
= z
->elem_size
;
1211 * Do a quick feasability check before we scan the zone:
1212 * skip unless there is likelihood of getting pages back
1213 * (i.e we need a whole allocation block's worth of free
1214 * elements before we can garbage collect) and
1215 * the zone has more than 10 percent of it's elements free
1217 if (((z
->cur_size
- z
->count
* elt_size
) <= (2 * z
->alloc_size
)) ||
1218 ((z
->cur_size
- z
->count
* elt_size
) <= (z
->cur_size
/ 10))) {
1226 * Snatch all of the free elements away from the zone.
1229 scan
= (void *)z
->free_elements
;
1230 (void *)z
->free_elements
= NULL
;
1237 * Determine which elements we can attempt to collect
1238 * and count them up in the page table. Foreign elements
1239 * are returned to the zone.
1242 prev
= (void *)&scan
;
1244 n
= 0; tail
= keep
= NULL
;
1245 while (elt
!= NULL
) {
1246 if (from_zone_map(elt
, elt_size
)) {
1247 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1252 ++zgc_stats
.elems_collected
;
1258 tail
= tail
->next
= elt
;
1260 elt
= prev
->next
= elt
->next
;
1265 * Dribble back the elements we are keeping.
1269 if (z
->waiting
== TRUE
) {
1273 tail
->next
= (void *)z
->free_elements
;
1274 (void *)z
->free_elements
= keep
;
1280 while ((elt
!= NULL
) && (++m
< 50)) {
1285 prev
->next
= (void *)z
->free_elements
;
1286 (void *)z
->free_elements
= (void *)base_elt
;
1287 base_prev
->next
= elt
;
1304 * Return any remaining elements.
1310 tail
->next
= (void *)z
->free_elements
;
1311 (void *)z
->free_elements
= keep
;
1319 * Determine which pages we can reclaim and
1320 * free those elements.
1324 prev
= (void *)&scan
;
1326 n
= 0; tail
= keep
= NULL
;
1327 while (elt
!= NULL
) {
1328 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1329 size_freed
+= elt_size
;
1330 zone_page_free_element(&zone_free_pages
,
1331 (vm_offset_t
)elt
, elt_size
);
1333 elt
= prev
->next
= elt
->next
;
1335 ++zgc_stats
.elems_freed
;
1338 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1343 tail
= tail
->next
= elt
;
1345 elt
= prev
->next
= elt
->next
;
1348 ++zgc_stats
.elems_kept
;
1352 * Dribble back the elements we are keeping,
1353 * and update the zone size info.
1359 z
->cur_size
-= size_freed
;
1363 tail
->next
= (void *)z
->free_elements
;
1364 (void *)z
->free_elements
= keep
;
1374 n
= 0; tail
= keep
= NULL
;
1379 * Return any remaining elements, and update
1380 * the zone size info.
1385 if (size_freed
> 0 || keep
!= NULL
) {
1387 z
->cur_size
-= size_freed
;
1390 tail
->next
= (void *)z
->free_elements
;
1391 (void *)z
->free_elements
= keep
;
1396 z
->doing_gc
= FALSE
;
1405 * Reclaim the pages we are freeing.
1408 while ((zp
= zone_free_pages
) != NULL
) {
1409 zone_free_pages
= zp
->link
;
1410 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1411 (zp
- zone_page_table
), PAGE_SIZE
);
1412 ++zgc_stats
.pgs_freed
;
1415 mutex_unlock(&zone_gc_lock
);
1421 * Called by the pageout daemon when the system needs more free pages.
1425 consider_zone_gc(void)
1428 * By default, don't attempt zone GC more frequently
1429 * than once / 1 minutes.
1432 if (zone_gc_max_rate
== 0)
1433 zone_gc_max_rate
= (60 << SCHED_TICK_SHIFT
) + 1;
1435 if (zone_gc_allowed
&&
1436 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1438 zone_gc_forced
= FALSE
;
1439 zone_gc_last_tick
= sched_tick
;
1448 zone_name_array_t
*namesp
,
1449 mach_msg_type_number_t
*namesCntp
,
1450 zone_info_array_t
*infop
,
1451 mach_msg_type_number_t
*infoCntp
)
1454 vm_offset_t names_addr
;
1455 vm_size_t names_size
;
1457 vm_offset_t info_addr
;
1458 vm_size_t info_size
;
1459 unsigned int max_zones
, i
;
1465 if (host
== HOST_NULL
)
1466 return KERN_INVALID_HOST
;
1469 * We assume that zones aren't freed once allocated.
1470 * We won't pick up any zones that are allocated later.
1473 simple_lock(&all_zones_lock
);
1475 max_zones
= num_zones
+ 4;
1477 max_zones
= num_zones
+ 2;
1480 simple_unlock(&all_zones_lock
);
1482 if (max_zones
<= *namesCntp
) {
1483 /* use in-line memory */
1484 names_size
= *namesCntp
* sizeof *names
;
1487 names_size
= round_page(max_zones
* sizeof *names
);
1488 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1489 &names_addr
, names_size
);
1490 if (kr
!= KERN_SUCCESS
)
1492 names
= (zone_name_t
*) names_addr
;
1495 if (max_zones
<= *infoCntp
) {
1496 /* use in-line memory */
1497 info_size
= *infoCntp
* sizeof *info
;
1500 info_size
= round_page(max_zones
* sizeof *info
);
1501 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1502 &info_addr
, info_size
);
1503 if (kr
!= KERN_SUCCESS
) {
1504 if (names
!= *namesp
)
1505 kmem_free(ipc_kernel_map
,
1506 names_addr
, names_size
);
1510 info
= (zone_info_t
*) info_addr
;
1515 for (i
= 0; i
< num_zones
; i
++) {
1518 assert(z
!= ZONE_NULL
);
1524 simple_lock(&all_zones_lock
);
1526 simple_unlock(&all_zones_lock
);
1528 /* assuming here the name data is static */
1529 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1530 sizeof zn
->zn_name
);
1532 zi
->zi_count
= zcopy
.count
;
1533 zi
->zi_cur_size
= zcopy
.cur_size
;
1534 zi
->zi_max_size
= zcopy
.max_size
;
1535 zi
->zi_elem_size
= zcopy
.elem_size
;
1536 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1537 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1538 zi
->zi_collectable
= zcopy
.collectable
;
1543 strcpy(zn
->zn_name
, "kernel_stacks");
1544 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1545 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1549 strcpy(zn
->zn_name
, "save_areas");
1550 save_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
, "pmap_mappings");
1556 mapping_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
, "kalloc.large");
1562 kalloc_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
);
1565 if (names
!= *namesp
) {
1569 used
= max_zones
* sizeof *names
;
1571 if (used
!= names_size
)
1572 bzero((char *) (names_addr
+ used
), names_size
- used
);
1574 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)names_addr
,
1575 (vm_map_size_t
)names_size
, TRUE
, ©
);
1576 assert(kr
== KERN_SUCCESS
);
1578 *namesp
= (zone_name_t
*) copy
;
1580 *namesCntp
= max_zones
;
1582 if (info
!= *infop
) {
1586 used
= max_zones
* sizeof *info
;
1588 if (used
!= info_size
)
1589 bzero((char *) (info_addr
+ used
), info_size
- used
);
1591 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)info_addr
,
1592 (vm_map_size_t
)info_size
, TRUE
, ©
);
1593 assert(kr
== KERN_SUCCESS
);
1595 *infop
= (zone_info_t
*) copy
;
1597 *infoCntp
= max_zones
;
1599 return KERN_SUCCESS
;
1603 #include <ddb/db_command.h>
1604 #include <ddb/db_output.h>
1605 #include <kern/kern_print.h>
1607 const char *zone_labels
=
1608 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1615 void db_zone_check_active(
1617 void db_zone_print_active(
1619 #endif /* ZONE_DEBUG */
1620 void db_zone_print_free(
1630 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1631 addr
, zcopy
.count
, zcopy
.cur_size
,
1632 zcopy
.max_size
, zcopy
.elem_size
,
1633 zcopy
.alloc_size
, zcopy
.zone_name
);
1634 if (zcopy
.exhaustible
)
1636 if (zcopy
.collectable
)
1638 if (zcopy
.expandable
)
1648 __unused db_expr_t count
,
1649 __unused
char * modif
)
1651 struct zone
*z
= (zone_t
)((char *)0 + addr
);
1653 if (z
== ZONE_NULL
|| !have_addr
){
1654 db_error("No Zone\n");
1658 db_printf("%s\n", zone_labels
);
1665 __unused db_expr_t addr
,
1668 __unused
char * modif
)
1674 * Don't risk hanging by unconditionally locking,
1675 * risk of incoherent data is small (zones aren't freed).
1677 have_addr
= simple_lock_try(&all_zones_lock
);
1681 simple_unlock(&all_zones_lock
);
1684 db_printf("%s\n", zone_labels
);
1685 for ( ; count
> 0; count
--) {
1687 db_error("Mangled Zone List\n");
1691 total
+= z
->cur_size
,
1693 have_addr
= simple_lock_try(&all_zones_lock
);
1696 simple_unlock(&all_zones_lock
);
1699 db_printf("\nTotal %8x", total
);
1700 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1705 db_zone_check_active(
1711 if (!zone_debug_enabled(zone
) || !zone_check
)
1713 tmp_elem
= queue_first(&zone
->active_zones
);
1714 while (count
< zone
->count
) {
1716 if (tmp_elem
== 0) {
1717 printf("unexpected zero element, zone=0x%x, count=%d\n",
1722 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1723 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1728 tmp_elem
= queue_next(tmp_elem
);
1730 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1731 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1738 db_zone_print_active(
1744 if (!zone_debug_enabled(zone
)) {
1745 printf("zone 0x%x debug not enabled\n", zone
);
1749 printf("zone_check FALSE\n");
1753 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1754 printf("active list:\n");
1755 tmp_elem
= queue_first(&zone
->active_zones
);
1756 while (count
< zone
->count
) {
1757 printf(" 0x%x", tmp_elem
);
1759 if ((count
% 6) == 0)
1761 if (tmp_elem
== 0) {
1762 printf("\nunexpected zero element, count=%d\n", count
);
1765 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1766 printf("\nunexpected queue_end, count=%d\n", count
);
1769 tmp_elem
= queue_next(tmp_elem
);
1771 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1772 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1776 #endif /* ZONE_DEBUG */
1786 freecount
= zone_free_count(zone
);
1787 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1788 printf("free list:\n");
1789 elem
= zone
->free_elements
;
1790 while (count
< freecount
) {
1791 printf(" 0x%x", elem
);
1793 if ((count
% 6) == 0)
1796 printf("\nunexpected zero element, count=%d\n", count
);
1799 elem
= *((vm_offset_t
*)elem
);
1802 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1807 #endif /* MACH_KDB */
1812 /* should we care about locks here ? */
1820 char *elt
= (char *)prev
;
1822 if (!zone_debug_enabled(z
))
1824 elt
-= ZONE_DEBUG_OFFSET
;
1825 elt
= (char *) queue_next((queue_t
) elt
);
1826 if ((queue_t
) elt
== &z
->active_zones
)
1828 elt
+= ZONE_DEBUG_OFFSET
;
1838 if (!zone_debug_enabled(z
))
1840 if (queue_empty(&z
->active_zones
))
1842 elt
= (char *)queue_first(&z
->active_zones
);
1843 elt
+= ZONE_DEBUG_OFFSET
;
1848 * Second arg controls how many zone elements are printed:
1851 * n, n > 0 => last n on active list
1860 boolean_t print
= (tail
!= 0);
1864 if (z
->count
< tail
)
1866 tail
= z
->count
- tail
;
1867 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1868 if (print
&& tail
<= count
)
1869 db_printf("%8x\n", elt
);
1872 assert(count
== z
->count
);
1875 #endif /* MACH_KDB */
1877 #define zone_in_use(z) ( z->count || z->free_elements )
1883 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1884 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
1886 queue_init(&z
->active_zones
);
1887 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
1894 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1896 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
1897 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1899 #endif /* ZONE_DEBUG */