2 * Copyright (c) 2000 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 <kern/assert.h>
64 #include <kern/macro_help.h>
65 #include <kern/sched.h>
66 #include <kern/lock.h>
67 #include <kern/sched_prim.h>
68 #include <kern/misc_protos.h>
69 #include <kern/thread_call.h>
70 #include <kern/zalloc.h>
71 #include <mach/vm_param.h>
72 #include <vm/vm_kern.h>
73 #include <machine/machparam.h>
77 /* Detect use of zone elt after freeing it by two methods:
78 * (1) Range-check the free-list "next" ptr for sanity.
79 * (2) Store the ptr in two different words, and compare them against
80 * each other when re-using the zone elt, to detect modifications;
85 #define is_kernel_data_addr(a) \
86 (!(a) || IS_SYS_VA(a) && !((a) & (sizeof(long)-1)))
88 #else /* !defined(__alpha) */
90 #define is_kernel_data_addr(a) \
91 (!(a) || (a) >= VM_MIN_KERNEL_ADDRESS && !((a) & 0x3))
93 #endif /* defined(__alpha) */
95 /* Should we set all words of the zone element to an illegal address
96 * when it is freed, to help catch usage after freeing? The down-side
97 * is that this obscures the identity of the freed element.
99 boolean_t zfree_clear
= FALSE
;
101 #define ADD_TO_ZONE(zone, element) \
106 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
108 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
110 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
111 (zone)->free_elements = (vm_offset_t) (element); \
115 #define REMOVE_FROM_ZONE(zone, ret, type) \
117 (ret) = (type) (zone)->free_elements; \
118 if ((ret) != (type) 0) { \
119 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
120 panic("A freed zone element has been modified.\n"); \
123 (zone)->free_elements = *((vm_offset_t *)(ret)); \
126 #else /* MACH_ASSERT */
128 #define ADD_TO_ZONE(zone, element) \
130 *((vm_offset_t *)(element)) = (zone)->free_elements; \
131 (zone)->free_elements = (vm_offset_t) (element); \
135 #define REMOVE_FROM_ZONE(zone, ret, type) \
137 (ret) = (type) (zone)->free_elements; \
138 if ((ret) != (type) 0) { \
140 (zone)->free_elements = *((vm_offset_t *)(ret)); \
144 #endif /* MACH_ASSERT */
147 #define zone_debug_enabled(z) z->active_zones.next
148 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
149 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
150 #endif /* ZONE_DEBUG */
153 * Support for garbage collection of unused zone pages:
156 struct zone_page_table_entry
{
157 struct zone_page_table_entry
*link
;
168 void zone_page_alloc(
172 void zone_page_free_element(
173 struct zone_page_table_entry
**free_pages
,
177 void zone_page_collect(
181 boolean_t
zone_page_collectable(
190 thread_call_param_t p0
,
191 thread_call_param_t p1
);
194 #if ZONE_DEBUG && MACH_KDB
198 #endif /* ZONE_DEBUG && MACH_KDB */
200 vm_map_t zone_map
= VM_MAP_NULL
;
202 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
205 * The VM system gives us an initial chunk of memory.
206 * It has to be big enough to allocate the zone_zone
210 vm_size_t zdata_size
;
212 #define lock_zone(zone) \
214 simple_lock(&(zone)->lock); \
217 #define unlock_zone(zone) \
219 simple_unlock(&(zone)->lock); \
222 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
223 #define zone_sleep(zone) \
224 thread_sleep_simple_lock((event_t)(zone), \
228 #define lock_zone_init(zone) \
230 simple_lock_init(&zone->lock, ETAP_MISC_ZONE); \
233 #define lock_try_zone(zone) simple_lock_try(&zone->lock)
235 kern_return_t
zget_space(
237 vm_offset_t
*result
);
239 decl_simple_lock_data(,zget_space_lock
)
240 vm_offset_t zalloc_next_space
;
241 vm_offset_t zalloc_end_of_space
;
242 vm_size_t zalloc_wasted_space
;
245 * Garbage collection map information
247 struct zone_page_table_entry
* zone_page_table
;
248 vm_offset_t zone_map_min_address
;
249 vm_offset_t zone_map_max_address
;
250 integer_t zone_pages
;
253 * Exclude more than one concurrent garbage collection
255 decl_mutex_data(, zone_gc_lock
)
257 #define from_zone_map(addr, size) \
258 ((vm_offset_t)(addr) >= zone_map_min_address && \
259 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
261 #define ZONE_PAGE_USED 0
262 #define ZONE_PAGE_UNUSED -1
266 * Protects first_zone, last_zone, num_zones,
267 * and the next_zone field of zones.
269 decl_simple_lock_data(, all_zones_lock
)
274 boolean_t zone_gc_allowed
= TRUE
;
275 boolean_t zone_gc_forced
= FALSE
;
276 unsigned zone_gc_last_tick
= 0;
277 unsigned zone_gc_max_rate
= 0; /* in ticks */
281 * zinit initializes a new zone. The zone data structures themselves
282 * are stored in a zone, which is initially a static structure that
283 * is initialized by zone_init.
287 vm_size_t size
, /* the size of an element */
288 vm_size_t max
, /* maximum memory to use */
289 vm_size_t alloc
, /* allocation size */
290 char *name
) /* a name for the zone */
294 if (zone_zone
== ZONE_NULL
) {
295 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
299 z
= (zone_t
) zalloc(zone_zone
);
304 * Round off all the parameters appropriately.
306 if (size
< sizeof(z
->free_elements
))
307 size
= sizeof(z
->free_elements
);
308 size
= ((size
-1) + sizeof(z
->free_elements
)) -
309 ((size
-1) % sizeof(z
->free_elements
));
312 alloc
= round_page_32(alloc
);
313 max
= round_page_32(max
);
315 * We look for an allocation size with least fragmentation
316 * in the range of 1 - 5 pages. This size will be used unless
317 * the user suggestion is larger AND has less fragmentation
319 { vm_size_t best
, waste
; unsigned int i
;
322 for (i
= 2; i
<= 5; i
++){ vm_size_t tsize
, twaste
;
323 tsize
= i
* PAGE_SIZE
;
324 twaste
= tsize
% size
;
326 best
= tsize
, waste
= twaste
;
328 if (alloc
<= best
|| (alloc
% size
>= waste
))
331 if (max
&& (max
< alloc
))
334 z
->free_elements
= 0;
338 z
->alloc_size
= alloc
;
341 z
->doing_alloc
= FALSE
;
342 z
->exhaustible
= FALSE
;
343 z
->collectable
= TRUE
;
344 z
->allows_foreign
= FALSE
;
345 z
->expandable
= TRUE
;
347 z
->async_pending
= FALSE
;
350 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
351 zone_debug_enable(z
);
352 #endif /* ZONE_DEBUG */
356 * Add the zone to the all-zones list.
359 z
->next_zone
= ZONE_NULL
;
360 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
361 simple_lock(&all_zones_lock
);
363 last_zone
= &z
->next_zone
;
365 simple_unlock(&all_zones_lock
);
371 * Cram the given memory into the specified zone.
375 register zone_t zone
,
379 register vm_size_t elem_size
;
381 /* Basic sanity checks */
382 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
383 assert(!zone
->collectable
|| zone
->allows_foreign
384 || (from_zone_map(newmem
, size
)));
386 elem_size
= zone
->elem_size
;
389 while (size
>= elem_size
) {
390 ADD_TO_ZONE(zone
, newmem
);
391 if (from_zone_map(newmem
, elem_size
))
392 zone_page_alloc(newmem
, elem_size
);
393 zone
->count
++; /* compensate for ADD_TO_ZONE */
396 zone
->cur_size
+= elem_size
;
402 * Contiguous space allocator for non-paged zones. Allocates "size" amount
403 * of memory from zone_map.
411 vm_offset_t new_space
= 0;
412 vm_size_t space_to_add
;
414 simple_lock(&zget_space_lock
);
415 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
417 * Add at least one page to allocation area.
420 space_to_add
= round_page_32(size
);
422 if (new_space
== 0) {
423 kern_return_t retval
;
425 * Memory cannot be wired down while holding
426 * any locks that the pageout daemon might
427 * need to free up pages. [Making the zget_space
428 * lock a complex lock does not help in this
431 * Unlock and allocate memory. Because several
432 * threads might try to do this at once, don't
433 * use the memory before checking for available
437 simple_unlock(&zget_space_lock
);
439 retval
= kernel_memory_allocate(zone_map
, &new_space
,
440 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
441 if (retval
!= KERN_SUCCESS
)
443 zone_page_init(new_space
, space_to_add
,
445 simple_lock(&zget_space_lock
);
451 * Memory was allocated in a previous iteration.
453 * Check whether the new region is contiguous
457 if (new_space
!= zalloc_end_of_space
) {
459 * Throw away the remainder of the
460 * old space, and start a new one.
462 zalloc_wasted_space
+=
463 zalloc_end_of_space
- zalloc_next_space
;
464 zalloc_next_space
= new_space
;
467 zalloc_end_of_space
= new_space
+ space_to_add
;
471 *result
= zalloc_next_space
;
472 zalloc_next_space
+= size
;
473 simple_unlock(&zget_space_lock
);
476 kmem_free(zone_map
, new_space
, space_to_add
);
478 return(KERN_SUCCESS
);
483 * Steal memory for the zone package. Called from
484 * vm_page_bootstrap().
487 zone_steal_memory(void)
489 zdata_size
= round_page_32(128*sizeof(struct zone
));
490 zdata
= pmap_steal_memory(zdata_size
);
495 * Fill a zone with enough memory to contain at least nelem elements.
496 * Memory is obtained with kmem_alloc_wired from the kernel_map.
497 * Return the number of elements actually put into the zone, which may
498 * be more than the caller asked for since the memory allocation is
499 * rounded up to a full page.
514 size
= nelem
* zone
->elem_size
;
515 size
= round_page_32(size
);
516 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
517 if (kr
!= KERN_SUCCESS
)
520 zone_change(zone
, Z_FOREIGN
, TRUE
);
521 zcram(zone
, memory
, size
);
522 nalloc
= size
/ zone
->elem_size
;
523 assert(nalloc
>= nelem
);
529 * Initialize the "zone of zones" which uses fixed memory allocated
530 * earlier in memory initialization. zone_bootstrap is called
536 vm_size_t zone_zone_size
;
537 vm_offset_t zone_zone_space
;
539 simple_lock_init(&all_zones_lock
, ETAP_MISC_ZONE_ALL
);
541 first_zone
= ZONE_NULL
;
542 last_zone
= &first_zone
;
545 simple_lock_init(&zget_space_lock
, ETAP_MISC_ZONE_GET
);
546 zalloc_next_space
= zdata
;
547 zalloc_end_of_space
= zdata
+ zdata_size
;
548 zalloc_wasted_space
= 0;
550 /* assertion: nobody else called zinit before us */
551 assert(zone_zone
== ZONE_NULL
);
552 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
553 sizeof(struct zone
), "zones");
554 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
555 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
556 zget_space(zone_zone_size
, &zone_zone_space
);
557 zcram(zone_zone
, zone_zone_space
, zone_zone_size
);
562 vm_size_t max_zonemap_size
)
564 kern_return_t retval
;
565 vm_offset_t zone_min
;
566 vm_offset_t zone_max
;
567 vm_size_t zone_table_size
;
569 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
570 FALSE
, TRUE
, &zone_map
);
571 if (retval
!= KERN_SUCCESS
)
572 panic("zone_init: kmem_suballoc failed");
573 zone_max
= zone_min
+ round_page_32(max_zonemap_size
);
575 * Setup garbage collection information:
577 zone_table_size
= atop_32(zone_max
- zone_min
) *
578 sizeof(struct zone_page_table_entry
);
579 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
580 zone_table_size
) != KERN_SUCCESS
)
582 zone_min
= (vm_offset_t
)zone_page_table
+ round_page_32(zone_table_size
);
583 zone_pages
= atop_32(zone_max
- zone_min
);
584 zone_map_min_address
= zone_min
;
585 zone_map_max_address
= zone_max
;
586 mutex_init(&zone_gc_lock
, ETAP_NO_TRACE
);
587 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
592 * zalloc returns an element from the specified zone.
596 register zone_t zone
,
600 kern_return_t retval
;
602 assert(zone
!= ZONE_NULL
);
603 check_simple_locks();
607 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
609 while ((addr
== 0) && canblock
) {
611 * If nothing was there, try to get more
613 if (zone
->doing_alloc
) {
615 * Someone is allocating memory for this zone.
616 * Wait for it to show up, then try again.
618 zone
->waiting
= TRUE
;
622 if ((zone
->cur_size
+ zone
->elem_size
) >
624 if (zone
->exhaustible
)
626 if (zone
->expandable
) {
628 * We're willing to overflow certain
629 * zones, but not without complaining.
631 * This is best used in conjunction
632 * with the collectable flag. What we
633 * want is an assurance we can get the
634 * memory back, assuming there's no
637 zone
->max_size
+= (zone
->max_size
>> 1);
641 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
644 zone
->doing_alloc
= TRUE
;
647 if (zone
->collectable
) {
649 vm_size_t alloc_size
;
650 boolean_t retry
= FALSE
;
654 if (vm_pool_low() || retry
== TRUE
)
656 round_page_32(zone
->elem_size
);
658 alloc_size
= zone
->alloc_size
;
660 retval
= kernel_memory_allocate(zone_map
,
661 &space
, alloc_size
, 0,
662 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
663 if (retval
== KERN_SUCCESS
) {
664 zone_page_init(space
, alloc_size
,
666 zcram(zone
, space
, alloc_size
);
669 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
670 /* would like to cause a zone_gc() */
679 zone
->doing_alloc
= FALSE
;
681 zone
->waiting
= FALSE
;
684 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
686 retval
== KERN_RESOURCE_SHORTAGE
) {
694 retval
= zget_space(zone
->elem_size
, &space
);
697 zone
->doing_alloc
= FALSE
;
699 zone
->waiting
= FALSE
;
700 thread_wakeup((event_t
)zone
);
702 if (retval
== KERN_SUCCESS
) {
704 zone
->cur_size
+= zone
->elem_size
;
706 if (zone_debug_enabled(zone
)) {
707 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
711 zone_page_alloc(space
, zone
->elem_size
);
713 if (zone_debug_enabled(zone
))
714 space
+= ZONE_DEBUG_OFFSET
;
718 if (retval
== KERN_RESOURCE_SHORTAGE
) {
729 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
732 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
733 zone
->async_pending
= TRUE
;
735 thread_call_enter(&zone
->call_async_alloc
);
737 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
741 if (addr
&& zone_debug_enabled(zone
)) {
742 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
743 addr
+= ZONE_DEBUG_OFFSET
;
755 register zone_t zone
)
757 return( zalloc_canblock(zone
, TRUE
) );
762 register zone_t zone
)
764 return( zalloc_canblock(zone
, FALSE
) );
769 thread_call_param_t p0
,
770 thread_call_param_t p1
)
774 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
775 zfree((zone_t
)p0
, elt
);
776 lock_zone(((zone_t
)p0
));
777 ((zone_t
)p0
)->async_pending
= FALSE
;
778 unlock_zone(((zone_t
)p0
));
783 * zget returns an element from the specified zone
784 * and immediately returns nothing if there is nothing there.
786 * This form should be used when you can not block (like when
787 * processing an interrupt).
791 register zone_t zone
)
793 register vm_offset_t addr
;
795 assert( zone
!= ZONE_NULL
);
797 if (!lock_try_zone(zone
))
798 return ((vm_offset_t
)0);
800 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
802 if (addr
&& zone_debug_enabled(zone
)) {
803 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
804 addr
+= ZONE_DEBUG_OFFSET
;
806 #endif /* ZONE_DEBUG */
812 /* Keep this FALSE by default. Large memory machine run orders of magnitude
813 slower in debug mode when true. Use debugger to enable if needed */
814 /* static */ boolean_t zone_check
= FALSE
;
816 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
817 static vm_offset_t zone_last_bogus_elem
= 0;
821 register zone_t zone
,
826 /* Basic sanity checks */
827 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
828 panic("zfree: NULL");
829 /* zone_gc assumes zones are never freed */
830 if (zone
== zone_zone
)
831 panic("zfree: freeing to zone_zone breaks zone_gc!");
834 if (zone
->collectable
&& !zone
->allows_foreign
&&
835 !from_zone_map(elem
, zone
->elem_size
)) {
837 panic("zfree: non-allocated memory in collectable zone!");
839 zone_last_bogus_zone
= zone
;
840 zone_last_bogus_elem
= elem
;
847 if (zone_debug_enabled(zone
)) {
850 elem
-= ZONE_DEBUG_OFFSET
;
852 /* check the zone's consistency */
854 for (tmp_elem
= queue_first(&zone
->active_zones
);
855 !queue_end(tmp_elem
, &zone
->active_zones
);
856 tmp_elem
= queue_next(tmp_elem
))
857 if (elem
== (vm_offset_t
)tmp_elem
)
859 if (elem
!= (vm_offset_t
)tmp_elem
)
860 panic("zfree()ing element from wrong zone");
862 remqueue(&zone
->active_zones
, (queue_t
) elem
);
864 #endif /* ZONE_DEBUG */
868 /* check the zone's consistency */
870 for (this = zone
->free_elements
;
872 this = * (vm_offset_t
*) this)
873 if (!pmap_kernel_va(this) || this == elem
)
876 ADD_TO_ZONE(zone
, elem
);
879 * If elements have one or more pages, and memory is low,
880 * request to run the garbage collection in the zone the next
881 * time the pageout thread runs.
883 if (zone
->elem_size
>= PAGE_SIZE
&&
885 zone_gc_forced
= TRUE
;
891 /* Change a zone's flags.
892 * This routine must be called immediately after zinit.
900 assert( zone
!= ZONE_NULL
);
901 assert( value
== TRUE
|| value
== FALSE
);
905 zone
->exhaustible
= value
;
908 zone
->collectable
= value
;
911 zone
->expandable
= value
;
914 zone
->allows_foreign
= value
;
918 panic("Zone_change: Wrong Item Type!");
922 lock_zone_init(zone
);
926 * Return the expected number of free elements in the zone.
927 * This calculation will be incorrect if items are zfree'd that
928 * were never zalloc'd/zget'd. The correct way to stuff memory
929 * into a zone is by zcram.
933 zone_free_count(zone_t zone
)
935 integer_t free_count
;
938 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
941 assert(free_count
>= 0);
947 * zprealloc preallocates wired memory, exanding the specified
948 * zone to the specified size
958 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
960 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
961 zcram(zone
, addr
, size
);
966 * Zone garbage collection subroutines
970 zone_page_collectable(
974 struct zone_page_table_entry
*zp
;
978 if (!from_zone_map(addr
, size
))
979 panic("zone_page_collectable");
982 i
= atop_32(addr
-zone_map_min_address
);
983 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
985 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
986 if (zp
->collect_count
== zp
->alloc_count
)
997 struct zone_page_table_entry
*zp
;
1001 if (!from_zone_map(addr
, size
))
1002 panic("zone_page_keep");
1005 i
= atop_32(addr
-zone_map_min_address
);
1006 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1008 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1009 zp
->collect_count
= 0;
1017 struct zone_page_table_entry
*zp
;
1021 if (!from_zone_map(addr
, size
))
1022 panic("zone_page_collect");
1025 i
= atop_32(addr
-zone_map_min_address
);
1026 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1028 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1029 ++zp
->collect_count
;
1038 struct zone_page_table_entry
*zp
;
1042 if (!from_zone_map(addr
, size
))
1043 panic("zone_page_init");
1046 i
= atop_32(addr
-zone_map_min_address
);
1047 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1049 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1050 zp
->alloc_count
= value
;
1051 zp
->collect_count
= 0;
1060 struct zone_page_table_entry
*zp
;
1064 if (!from_zone_map(addr
, size
))
1065 panic("zone_page_alloc");
1068 i
= atop_32(addr
-zone_map_min_address
);
1069 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1071 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1073 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1074 * it was previously set to ZONE_PAGE_UNUSED.
1076 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1077 zp
->alloc_count
= 1;
1084 zone_page_free_element(
1085 struct zone_page_table_entry
**free_pages
,
1089 struct zone_page_table_entry
*zp
;
1093 if (!from_zone_map(addr
, size
))
1094 panic("zone_page_free_element");
1097 i
= atop_32(addr
-zone_map_min_address
);
1098 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1100 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1101 if (zp
->collect_count
> 0)
1102 --zp
->collect_count
;
1103 if (--zp
->alloc_count
== 0) {
1104 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1105 zp
->collect_count
= 0;
1107 zp
->link
= *free_pages
;
1114 /* This is used for walking through a zone's free element list.
1116 struct zone_free_element
{
1117 struct zone_free_element
* next
;
1123 uint32_t elems_collected
,
1128 /* Zone garbage collection
1130 * zone_gc will walk through all the free elements in all the
1131 * zones that are marked collectable looking for reclaimable
1132 * pages. zone_gc is called by consider_zone_gc when the system
1133 * begins to run out of memory.
1138 unsigned int max_zones
;
1141 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1143 mutex_lock(&zone_gc_lock
);
1145 simple_lock(&all_zones_lock
);
1146 max_zones
= num_zones
;
1148 simple_unlock(&all_zones_lock
);
1151 for (i
= 0; i
< zone_pages
; i
++)
1152 assert(zone_page_table
[i
].collect_count
== 0);
1153 #endif /* MACH_ASSERT */
1155 zone_free_pages
= NULL
;
1157 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1159 vm_size_t elt_size
, size_freed
;
1160 struct zone_free_element
*elt
, *prev
, *scan
, *keep
, *tail
;
1162 assert(z
!= ZONE_NULL
);
1164 if (!z
->collectable
)
1169 elt_size
= z
->elem_size
;
1172 * Do a quick feasability check before we scan the zone:
1173 * skip unless there is likelihood of getting 1+ pages back.
1175 if (z
->cur_size
- z
->count
* elt_size
<= 2 * PAGE_SIZE
){
1181 * Snatch all of the free elements away from the zone.
1184 scan
= (void *)z
->free_elements
;
1185 (void *)z
->free_elements
= NULL
;
1192 * Determine which elements we can attempt to collect
1193 * and count them up in the page table. Foreign elements
1194 * are returned to the zone.
1197 prev
= (void *)&scan
;
1199 n
= 0; tail
= keep
= NULL
;
1200 while (elt
!= NULL
) {
1201 if (from_zone_map(elt
, elt_size
)) {
1202 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1207 ++zgc_stats
.elems_collected
;
1213 tail
= tail
->next
= elt
;
1215 elt
= prev
->next
= elt
->next
;
1220 * Dribble back the elements we are keeping.
1223 if (++n
>= 50 && keep
!= NULL
) {
1226 tail
->next
= (void *)z
->free_elements
;
1227 (void *)z
->free_elements
= keep
;
1231 n
= 0; tail
= keep
= NULL
;
1236 * Return any remaining elements.
1242 tail
->next
= (void *)z
->free_elements
;
1243 (void *)z
->free_elements
= keep
;
1251 * Determine which pages we can reclaim and
1252 * free those elements.
1256 prev
= (void *)&scan
;
1258 n
= 0; tail
= keep
= NULL
;
1259 while (elt
!= NULL
) {
1260 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1261 size_freed
+= elt_size
;
1262 zone_page_free_element(&zone_free_pages
,
1263 (vm_offset_t
)elt
, elt_size
);
1265 elt
= prev
->next
= elt
->next
;
1267 ++zgc_stats
.elems_freed
;
1270 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1275 tail
= tail
->next
= elt
;
1277 elt
= prev
->next
= elt
->next
;
1280 ++zgc_stats
.elems_kept
;
1284 * Dribble back the elements we are keeping,
1285 * and update the zone size info.
1288 if (++n
>= 50 && keep
!= NULL
) {
1291 z
->cur_size
-= size_freed
;
1294 tail
->next
= (void *)z
->free_elements
;
1295 (void *)z
->free_elements
= keep
;
1299 n
= 0; tail
= keep
= NULL
;
1304 * Return any remaining elements, and update
1305 * the zone size info.
1308 if (size_freed
> 0 || keep
!= NULL
) {
1311 z
->cur_size
-= size_freed
;
1314 tail
->next
= (void *)z
->free_elements
;
1315 (void *)z
->free_elements
= keep
;
1323 * Reclaim the pages we are freeing.
1326 while ((zp
= zone_free_pages
) != NULL
) {
1327 zone_free_pages
= zp
->link
;
1328 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1329 (zp
- zone_page_table
), PAGE_SIZE
);
1330 ++zgc_stats
.pgs_freed
;
1333 mutex_unlock(&zone_gc_lock
);
1339 * Called by the pageout daemon when the system needs more free pages.
1343 consider_zone_gc(void)
1346 * By default, don't attempt zone GC more frequently
1347 * than once / 2 seconds.
1350 if (zone_gc_max_rate
== 0)
1351 zone_gc_max_rate
= (2 << SCHED_TICK_SHIFT
) + 1;
1353 if (zone_gc_allowed
&&
1354 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1356 zone_gc_forced
= FALSE
;
1357 zone_gc_last_tick
= sched_tick
;
1362 #include <mach/kern_return.h>
1363 #include <mach/machine/vm_types.h>
1364 #include <mach_debug/zone_info.h>
1365 #include <kern/host.h>
1366 #include <vm/vm_map.h>
1367 #include <vm/vm_kern.h>
1369 #include <mach/mach_host_server.h>
1374 zone_name_array_t
*namesp
,
1375 mach_msg_type_number_t
*namesCntp
,
1376 zone_info_array_t
*infop
,
1377 mach_msg_type_number_t
*infoCntp
)
1380 vm_offset_t names_addr
;
1381 vm_size_t names_size
;
1383 vm_offset_t info_addr
;
1384 vm_size_t info_size
;
1385 unsigned int max_zones
, i
;
1391 if (host
== HOST_NULL
)
1392 return KERN_INVALID_HOST
;
1395 * We assume that zones aren't freed once allocated.
1396 * We won't pick up any zones that are allocated later.
1399 simple_lock(&all_zones_lock
);
1401 max_zones
= num_zones
+ 4;
1403 max_zones
= num_zones
+ 2;
1406 simple_unlock(&all_zones_lock
);
1408 if (max_zones
<= *namesCntp
) {
1409 /* use in-line memory */
1413 names_size
= round_page_32(max_zones
* sizeof *names
);
1414 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1415 &names_addr
, names_size
);
1416 if (kr
!= KERN_SUCCESS
)
1418 names
= (zone_name_t
*) names_addr
;
1421 if (max_zones
<= *infoCntp
) {
1422 /* use in-line memory */
1426 info_size
= round_page_32(max_zones
* sizeof *info
);
1427 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1428 &info_addr
, info_size
);
1429 if (kr
!= KERN_SUCCESS
) {
1430 if (names
!= *namesp
)
1431 kmem_free(ipc_kernel_map
,
1432 names_addr
, names_size
);
1436 info
= (zone_info_t
*) info_addr
;
1441 for (i
= 0; i
< num_zones
; i
++) {
1444 assert(z
!= ZONE_NULL
);
1450 simple_lock(&all_zones_lock
);
1452 simple_unlock(&all_zones_lock
);
1454 /* assuming here the name data is static */
1455 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1456 sizeof zn
->zn_name
);
1458 zi
->zi_count
= zcopy
.count
;
1459 zi
->zi_cur_size
= zcopy
.cur_size
;
1460 zi
->zi_max_size
= zcopy
.max_size
;
1461 zi
->zi_elem_size
= zcopy
.elem_size
;
1462 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1463 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1464 zi
->zi_collectable
= zcopy
.collectable
;
1469 strcpy(zn
->zn_name
, "kernel_stacks");
1470 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1471 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1475 strcpy(zn
->zn_name
, "save_areas");
1476 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1477 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1481 strcpy(zn
->zn_name
, "pmap_mappings");
1482 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1483 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1487 strcpy(zn
->zn_name
, "kalloc.large");
1488 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1489 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1491 if (names
!= *namesp
) {
1495 used
= max_zones
* sizeof *names
;
1497 if (used
!= names_size
)
1498 bzero((char *) (names_addr
+ used
), names_size
- used
);
1500 kr
= vm_map_copyin(ipc_kernel_map
, names_addr
, names_size
,
1502 assert(kr
== KERN_SUCCESS
);
1504 *namesp
= (zone_name_t
*) copy
;
1506 *namesCntp
= max_zones
;
1508 if (info
!= *infop
) {
1512 used
= max_zones
* sizeof *info
;
1514 if (used
!= info_size
)
1515 bzero((char *) (info_addr
+ used
), info_size
- used
);
1517 kr
= vm_map_copyin(ipc_kernel_map
, info_addr
, info_size
,
1519 assert(kr
== KERN_SUCCESS
);
1521 *infop
= (zone_info_t
*) copy
;
1523 *infoCntp
= max_zones
;
1525 return KERN_SUCCESS
;
1529 #include <ddb/db_command.h>
1530 #include <ddb/db_output.h>
1531 #include <kern/kern_print.h>
1533 const char *zone_labels
=
1534 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1541 void db_zone_check_active(
1543 void db_zone_print_active(
1545 #endif /* ZONE_DEBUG */
1546 void db_zone_print_free(
1556 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1557 addr
, zcopy
.count
, zcopy
.cur_size
,
1558 zcopy
.max_size
, zcopy
.elem_size
,
1559 zcopy
.alloc_size
, zcopy
.zone_name
);
1560 if (zcopy
.exhaustible
)
1562 if (zcopy
.collectable
)
1564 if (zcopy
.expandable
)
1577 struct zone
*z
= (zone_t
)addr
;
1579 if (z
== ZONE_NULL
|| !have_addr
){
1580 db_error("No Zone\n");
1584 db_printf("%s\n", zone_labels
);
1600 * Don't risk hanging by unconditionally locking,
1601 * risk of incoherent data is small (zones aren't freed).
1603 have_addr
= simple_lock_try(&all_zones_lock
);
1607 simple_unlock(&all_zones_lock
);
1610 db_printf("%s\n", zone_labels
);
1611 for ( ; count
> 0; count
--) {
1613 db_error("Mangled Zone List\n");
1617 total
+= z
->cur_size
,
1619 have_addr
= simple_lock_try(&all_zones_lock
);
1622 simple_unlock(&all_zones_lock
);
1625 db_printf("\nTotal %8x", total
);
1626 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1631 db_zone_check_active(
1637 if (!zone_debug_enabled(zone
) || !zone_check
)
1639 tmp_elem
= queue_first(&zone
->active_zones
);
1640 while (count
< zone
->count
) {
1642 if (tmp_elem
== 0) {
1643 printf("unexpected zero element, zone=0x%x, count=%d\n",
1648 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1649 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1654 tmp_elem
= queue_next(tmp_elem
);
1656 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1657 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1664 db_zone_print_active(
1670 if (!zone_debug_enabled(zone
)) {
1671 printf("zone 0x%x debug not enabled\n", zone
);
1675 printf("zone_check FALSE\n");
1679 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1680 printf("active list:\n");
1681 tmp_elem
= queue_first(&zone
->active_zones
);
1682 while (count
< zone
->count
) {
1683 printf(" 0x%x", tmp_elem
);
1685 if ((count
% 6) == 0)
1687 if (tmp_elem
== 0) {
1688 printf("\nunexpected zero element, count=%d\n", count
);
1691 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1692 printf("\nunexpected queue_end, count=%d\n", count
);
1695 tmp_elem
= queue_next(tmp_elem
);
1697 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1698 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1702 #endif /* ZONE_DEBUG */
1712 freecount
= zone_free_count(zone
);
1713 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1714 printf("free list:\n");
1715 elem
= zone
->free_elements
;
1716 while (count
< freecount
) {
1717 printf(" 0x%x", elem
);
1719 if ((count
% 6) == 0)
1722 printf("\nunexpected zero element, count=%d\n", count
);
1725 elem
= *((vm_offset_t
*)elem
);
1728 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1733 #endif /* MACH_KDB */
1738 /* should we care about locks here ? */
1746 if (!zone_debug_enabled(z
))
1748 elt
-= ZONE_DEBUG_OFFSET
;
1749 elt
= (vm_offset_t
) queue_next((queue_t
) elt
);
1750 if ((queue_t
) elt
== &z
->active_zones
)
1752 elt
+= ZONE_DEBUG_OFFSET
;
1762 if (!zone_debug_enabled(z
))
1764 if (queue_empty(&z
->active_zones
))
1766 elt
= (vm_offset_t
) queue_first(&z
->active_zones
);
1767 elt
+= ZONE_DEBUG_OFFSET
;
1772 * Second arg controls how many zone elements are printed:
1775 * n, n > 0 => last n on active list
1784 boolean_t print
= (tail
!= 0);
1788 if (z
->count
< tail
)
1790 tail
= z
->count
- tail
;
1791 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1792 if (print
&& tail
<= count
)
1793 db_printf("%8x\n", elt
);
1796 assert(count
== z
->count
);
1799 #endif /* MACH_KDB */
1801 #define zone_in_use(z) ( z->count || z->free_elements )
1807 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1808 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
1810 queue_init(&z
->active_zones
);
1811 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
1818 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1820 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
1821 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1823 #endif /* ZONE_DEBUG */