2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
57 * Author: Avadis Tevanian, Jr.
59 * Zone-based memory allocator. A zone is a collection of fixed size
60 * data blocks for which quick allocation/deallocation is possible.
62 #include <zone_debug.h>
66 #include <kern/assert.h>
67 #include <kern/macro_help.h>
68 #include <kern/sched.h>
69 #include <kern/lock.h>
70 #include <kern/sched_prim.h>
71 #include <kern/misc_protos.h>
72 #include <kern/thread_call.h>
73 #include <kern/zalloc.h>
74 #include <mach/vm_param.h>
75 #include <vm/vm_kern.h>
76 #include <machine/machparam.h>
80 /* Detect use of zone elt after freeing it by two methods:
81 * (1) Range-check the free-list "next" ptr for sanity.
82 * (2) Store the ptr in two different words, and compare them against
83 * each other when re-using the zone elt, to detect modifications;
88 #define is_kernel_data_addr(a) \
89 (!(a) || IS_SYS_VA(a) && !((a) & (sizeof(long)-1)))
91 #else /* !defined(__alpha) */
93 #define is_kernel_data_addr(a) \
94 (!(a) || (a) >= VM_MIN_KERNEL_ADDRESS && !((a) & 0x3))
96 #endif /* defined(__alpha) */
98 /* Should we set all words of the zone element to an illegal address
99 * when it is freed, to help catch usage after freeing? The down-side
100 * is that this obscures the identity of the freed element.
102 boolean_t zfree_clear
= FALSE
;
104 #define ADD_TO_ZONE(zone, element) \
109 i < zone->elem_size/sizeof(vm_offset_t) - 1; \
111 ((vm_offset_t *)(element))[i] = 0xdeadbeef; \
113 ((vm_offset_t *)(element))[0] = (zone)->free_elements; \
114 (zone)->free_elements = (vm_offset_t) (element); \
118 #define REMOVE_FROM_ZONE(zone, ret, type) \
120 (ret) = (type) (zone)->free_elements; \
121 if ((ret) != (type) 0) { \
122 if (!is_kernel_data_addr(((vm_offset_t *)(ret))[0])) { \
123 panic("A freed zone element has been modified.\n"); \
126 (zone)->free_elements = *((vm_offset_t *)(ret)); \
129 #else /* MACH_ASSERT */
131 #define ADD_TO_ZONE(zone, element) \
133 *((vm_offset_t *)(element)) = (zone)->free_elements; \
134 (zone)->free_elements = (vm_offset_t) (element); \
138 #define REMOVE_FROM_ZONE(zone, ret, type) \
140 (ret) = (type) (zone)->free_elements; \
141 if ((ret) != (type) 0) { \
143 (zone)->free_elements = *((vm_offset_t *)(ret)); \
147 #endif /* MACH_ASSERT */
150 #define zone_debug_enabled(z) z->active_zones.next
151 #define ROUNDUP(x,y) ((((x)+(y)-1)/(y))*(y))
152 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
153 #endif /* ZONE_DEBUG */
156 * Support for garbage collection of unused zone pages:
159 struct zone_page_table_entry
{
160 struct zone_page_table_entry
*link
;
171 void zone_page_alloc(
175 void zone_page_free_element(
176 struct zone_page_table_entry
**free_pages
,
180 void zone_page_collect(
184 boolean_t
zone_page_collectable(
193 thread_call_param_t p0
,
194 thread_call_param_t p1
);
197 #if ZONE_DEBUG && MACH_KDB
201 #endif /* ZONE_DEBUG && MACH_KDB */
203 vm_map_t zone_map
= VM_MAP_NULL
;
205 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
208 * The VM system gives us an initial chunk of memory.
209 * It has to be big enough to allocate the zone_zone
213 vm_size_t zdata_size
;
215 #define lock_zone(zone) \
217 simple_lock(&(zone)->lock); \
220 #define unlock_zone(zone) \
222 simple_unlock(&(zone)->lock); \
225 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
226 #define zone_sleep(zone) \
227 thread_sleep_simple_lock((event_t)(zone), \
231 #define lock_zone_init(zone) \
233 simple_lock_init(&zone->lock, ETAP_MISC_ZONE); \
236 #define lock_try_zone(zone) simple_lock_try(&zone->lock)
238 kern_return_t
zget_space(
240 vm_offset_t
*result
);
242 decl_simple_lock_data(,zget_space_lock
)
243 vm_offset_t zalloc_next_space
;
244 vm_offset_t zalloc_end_of_space
;
245 vm_size_t zalloc_wasted_space
;
248 * Garbage collection map information
250 struct zone_page_table_entry
* zone_page_table
;
251 vm_offset_t zone_map_min_address
;
252 vm_offset_t zone_map_max_address
;
253 integer_t zone_pages
;
256 * Exclude more than one concurrent garbage collection
258 decl_mutex_data(, zone_gc_lock
)
260 #define from_zone_map(addr, size) \
261 ((vm_offset_t)(addr) >= zone_map_min_address && \
262 ((vm_offset_t)(addr) + size -1) < zone_map_max_address)
264 #define ZONE_PAGE_USED 0
265 #define ZONE_PAGE_UNUSED -1
269 * Protects first_zone, last_zone, num_zones,
270 * and the next_zone field of zones.
272 decl_simple_lock_data(, all_zones_lock
)
277 boolean_t zone_gc_allowed
= TRUE
;
278 boolean_t zone_gc_forced
= FALSE
;
279 unsigned zone_gc_last_tick
= 0;
280 unsigned zone_gc_max_rate
= 0; /* in ticks */
284 * zinit initializes a new zone. The zone data structures themselves
285 * are stored in a zone, which is initially a static structure that
286 * is initialized by zone_init.
290 vm_size_t size
, /* the size of an element */
291 vm_size_t max
, /* maximum memory to use */
292 vm_size_t alloc
, /* allocation size */
293 char *name
) /* a name for the zone */
297 if (zone_zone
== ZONE_NULL
) {
298 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
302 z
= (zone_t
) zalloc(zone_zone
);
307 * Round off all the parameters appropriately.
309 if (size
< sizeof(z
->free_elements
))
310 size
= sizeof(z
->free_elements
);
311 size
= ((size
-1) + sizeof(z
->free_elements
)) -
312 ((size
-1) % sizeof(z
->free_elements
));
315 alloc
= round_page_32(alloc
);
316 max
= round_page_32(max
);
318 * We look for an allocation size with least fragmentation
319 * in the range of 1 - 5 pages. This size will be used unless
320 * the user suggestion is larger AND has less fragmentation
322 { vm_size_t best
, waste
; unsigned int i
;
325 for (i
= 2; i
<= 5; i
++){ vm_size_t tsize
, twaste
;
326 tsize
= i
* PAGE_SIZE
;
327 twaste
= tsize
% size
;
329 best
= tsize
, waste
= twaste
;
331 if (alloc
<= best
|| (alloc
% size
>= waste
))
334 if (max
&& (max
< alloc
))
337 z
->free_elements
= 0;
341 z
->alloc_size
= alloc
;
344 z
->doing_alloc
= FALSE
;
345 z
->exhaustible
= FALSE
;
346 z
->collectable
= TRUE
;
347 z
->allows_foreign
= FALSE
;
348 z
->expandable
= TRUE
;
350 z
->async_pending
= FALSE
;
353 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
354 zone_debug_enable(z
);
355 #endif /* ZONE_DEBUG */
359 * Add the zone to the all-zones list.
362 z
->next_zone
= ZONE_NULL
;
363 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
364 simple_lock(&all_zones_lock
);
366 last_zone
= &z
->next_zone
;
368 simple_unlock(&all_zones_lock
);
374 * Cram the given memory into the specified zone.
378 register zone_t zone
,
382 register vm_size_t elem_size
;
384 /* Basic sanity checks */
385 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
386 assert(!zone
->collectable
|| zone
->allows_foreign
387 || (from_zone_map(newmem
, size
)));
389 elem_size
= zone
->elem_size
;
392 while (size
>= elem_size
) {
393 ADD_TO_ZONE(zone
, newmem
);
394 if (from_zone_map(newmem
, elem_size
))
395 zone_page_alloc(newmem
, elem_size
);
396 zone
->count
++; /* compensate for ADD_TO_ZONE */
399 zone
->cur_size
+= elem_size
;
405 * Contiguous space allocator for non-paged zones. Allocates "size" amount
406 * of memory from zone_map.
414 vm_offset_t new_space
= 0;
415 vm_size_t space_to_add
;
417 simple_lock(&zget_space_lock
);
418 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
420 * Add at least one page to allocation area.
423 space_to_add
= round_page_32(size
);
425 if (new_space
== 0) {
426 kern_return_t retval
;
428 * Memory cannot be wired down while holding
429 * any locks that the pageout daemon might
430 * need to free up pages. [Making the zget_space
431 * lock a complex lock does not help in this
434 * Unlock and allocate memory. Because several
435 * threads might try to do this at once, don't
436 * use the memory before checking for available
440 simple_unlock(&zget_space_lock
);
442 retval
= kernel_memory_allocate(zone_map
, &new_space
,
443 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
444 if (retval
!= KERN_SUCCESS
)
446 zone_page_init(new_space
, space_to_add
,
448 simple_lock(&zget_space_lock
);
454 * Memory was allocated in a previous iteration.
456 * Check whether the new region is contiguous
460 if (new_space
!= zalloc_end_of_space
) {
462 * Throw away the remainder of the
463 * old space, and start a new one.
465 zalloc_wasted_space
+=
466 zalloc_end_of_space
- zalloc_next_space
;
467 zalloc_next_space
= new_space
;
470 zalloc_end_of_space
= new_space
+ space_to_add
;
474 *result
= zalloc_next_space
;
475 zalloc_next_space
+= size
;
476 simple_unlock(&zget_space_lock
);
479 kmem_free(zone_map
, new_space
, space_to_add
);
481 return(KERN_SUCCESS
);
486 * Steal memory for the zone package. Called from
487 * vm_page_bootstrap().
490 zone_steal_memory(void)
492 zdata_size
= round_page_32(128*sizeof(struct zone
));
493 zdata
= pmap_steal_memory(zdata_size
);
498 * Fill a zone with enough memory to contain at least nelem elements.
499 * Memory is obtained with kmem_alloc_wired from the kernel_map.
500 * Return the number of elements actually put into the zone, which may
501 * be more than the caller asked for since the memory allocation is
502 * rounded up to a full page.
517 size
= nelem
* zone
->elem_size
;
518 size
= round_page_32(size
);
519 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
520 if (kr
!= KERN_SUCCESS
)
523 zone_change(zone
, Z_FOREIGN
, TRUE
);
524 zcram(zone
, memory
, size
);
525 nalloc
= size
/ zone
->elem_size
;
526 assert(nalloc
>= nelem
);
532 * Initialize the "zone of zones" which uses fixed memory allocated
533 * earlier in memory initialization. zone_bootstrap is called
539 vm_size_t zone_zone_size
;
540 vm_offset_t zone_zone_space
;
542 simple_lock_init(&all_zones_lock
, ETAP_MISC_ZONE_ALL
);
544 first_zone
= ZONE_NULL
;
545 last_zone
= &first_zone
;
548 simple_lock_init(&zget_space_lock
, ETAP_MISC_ZONE_GET
);
549 zalloc_next_space
= zdata
;
550 zalloc_end_of_space
= zdata
+ zdata_size
;
551 zalloc_wasted_space
= 0;
553 /* assertion: nobody else called zinit before us */
554 assert(zone_zone
== ZONE_NULL
);
555 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
556 sizeof(struct zone
), "zones");
557 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
558 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
559 zget_space(zone_zone_size
, &zone_zone_space
);
560 zcram(zone_zone
, zone_zone_space
, zone_zone_size
);
565 vm_size_t max_zonemap_size
)
567 kern_return_t retval
;
568 vm_offset_t zone_min
;
569 vm_offset_t zone_max
;
570 vm_size_t zone_table_size
;
572 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
573 FALSE
, TRUE
, &zone_map
);
574 if (retval
!= KERN_SUCCESS
)
575 panic("zone_init: kmem_suballoc failed");
576 zone_max
= zone_min
+ round_page_32(max_zonemap_size
);
578 * Setup garbage collection information:
580 zone_table_size
= atop_32(zone_max
- zone_min
) *
581 sizeof(struct zone_page_table_entry
);
582 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
583 zone_table_size
) != KERN_SUCCESS
)
585 zone_min
= (vm_offset_t
)zone_page_table
+ round_page_32(zone_table_size
);
586 zone_pages
= atop_32(zone_max
- zone_min
);
587 zone_map_min_address
= zone_min
;
588 zone_map_max_address
= zone_max
;
589 mutex_init(&zone_gc_lock
, ETAP_NO_TRACE
);
590 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
595 * zalloc returns an element from the specified zone.
599 register zone_t zone
,
603 kern_return_t retval
;
605 assert(zone
!= ZONE_NULL
);
606 check_simple_locks();
610 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
612 while ((addr
== 0) && canblock
) {
614 * If nothing was there, try to get more
616 if (zone
->doing_alloc
) {
618 * Someone is allocating memory for this zone.
619 * Wait for it to show up, then try again.
621 zone
->waiting
= TRUE
;
625 if ((zone
->cur_size
+ zone
->elem_size
) >
627 if (zone
->exhaustible
)
629 if (zone
->expandable
) {
631 * We're willing to overflow certain
632 * zones, but not without complaining.
634 * This is best used in conjunction
635 * with the collectable flag. What we
636 * want is an assurance we can get the
637 * memory back, assuming there's no
640 zone
->max_size
+= (zone
->max_size
>> 1);
644 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
647 zone
->doing_alloc
= TRUE
;
650 if (zone
->collectable
) {
652 vm_size_t alloc_size
;
653 boolean_t retry
= FALSE
;
657 if (vm_pool_low() || retry
== TRUE
)
659 round_page_32(zone
->elem_size
);
661 alloc_size
= zone
->alloc_size
;
663 retval
= kernel_memory_allocate(zone_map
,
664 &space
, alloc_size
, 0,
665 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
666 if (retval
== KERN_SUCCESS
) {
667 zone_page_init(space
, alloc_size
,
669 zcram(zone
, space
, alloc_size
);
672 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
673 /* would like to cause a zone_gc() */
680 zone
->doing_alloc
= FALSE
;
682 zone
->waiting
= FALSE
;
685 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
687 retval
== KERN_RESOURCE_SHORTAGE
) {
695 retval
= zget_space(zone
->elem_size
, &space
);
698 zone
->doing_alloc
= FALSE
;
700 zone
->waiting
= FALSE
;
701 thread_wakeup((event_t
)zone
);
703 if (retval
== KERN_SUCCESS
) {
705 zone
->cur_size
+= zone
->elem_size
;
707 if (zone_debug_enabled(zone
)) {
708 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
712 zone_page_alloc(space
, zone
->elem_size
);
714 if (zone_debug_enabled(zone
))
715 space
+= ZONE_DEBUG_OFFSET
;
719 if (retval
== KERN_RESOURCE_SHORTAGE
) {
730 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
733 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
734 zone
->async_pending
= TRUE
;
736 thread_call_enter(&zone
->call_async_alloc
);
738 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
742 if (addr
&& zone_debug_enabled(zone
)) {
743 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
744 addr
+= ZONE_DEBUG_OFFSET
;
756 register zone_t zone
)
758 return( zalloc_canblock(zone
, TRUE
) );
763 register zone_t zone
)
765 return( zalloc_canblock(zone
, FALSE
) );
770 thread_call_param_t p0
,
771 thread_call_param_t p1
)
775 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
776 zfree((zone_t
)p0
, elt
);
777 lock_zone(((zone_t
)p0
));
778 ((zone_t
)p0
)->async_pending
= FALSE
;
779 unlock_zone(((zone_t
)p0
));
784 * zget returns an element from the specified zone
785 * and immediately returns nothing if there is nothing there.
787 * This form should be used when you can not block (like when
788 * processing an interrupt).
792 register zone_t zone
)
794 register vm_offset_t addr
;
796 assert( zone
!= ZONE_NULL
);
798 if (!lock_try_zone(zone
))
799 return ((vm_offset_t
)0);
801 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
803 if (addr
&& zone_debug_enabled(zone
)) {
804 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
805 addr
+= ZONE_DEBUG_OFFSET
;
807 #endif /* ZONE_DEBUG */
813 /* Keep this FALSE by default. Large memory machine run orders of magnitude
814 slower in debug mode when true. Use debugger to enable if needed */
815 /* static */ boolean_t zone_check
= FALSE
;
817 static zone_t zone_last_bogus_zone
= ZONE_NULL
;
818 static vm_offset_t zone_last_bogus_elem
= 0;
822 register zone_t zone
,
827 /* Basic sanity checks */
828 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
829 panic("zfree: NULL");
830 /* zone_gc assumes zones are never freed */
831 if (zone
== zone_zone
)
832 panic("zfree: freeing to zone_zone breaks zone_gc!");
835 if (zone
->collectable
&& !zone
->allows_foreign
&&
836 !from_zone_map(elem
, zone
->elem_size
)) {
838 panic("zfree: non-allocated memory in collectable zone!");
840 zone_last_bogus_zone
= zone
;
841 zone_last_bogus_elem
= elem
;
848 if (zone_debug_enabled(zone
)) {
851 elem
-= ZONE_DEBUG_OFFSET
;
853 /* check the zone's consistency */
855 for (tmp_elem
= queue_first(&zone
->active_zones
);
856 !queue_end(tmp_elem
, &zone
->active_zones
);
857 tmp_elem
= queue_next(tmp_elem
))
858 if (elem
== (vm_offset_t
)tmp_elem
)
860 if (elem
!= (vm_offset_t
)tmp_elem
)
861 panic("zfree()ing element from wrong zone");
863 remqueue(&zone
->active_zones
, (queue_t
) elem
);
865 #endif /* ZONE_DEBUG */
869 /* check the zone's consistency */
871 for (this = zone
->free_elements
;
873 this = * (vm_offset_t
*) this)
874 if (!pmap_kernel_va(this) || this == elem
)
877 ADD_TO_ZONE(zone
, elem
);
880 * If elements have one or more pages, and memory is low,
881 * request to run the garbage collection in the zone the next
882 * time the pageout thread runs.
884 if (zone
->elem_size
>= PAGE_SIZE
&&
886 zone_gc_forced
= TRUE
;
892 /* Change a zone's flags.
893 * This routine must be called immediately after zinit.
901 assert( zone
!= ZONE_NULL
);
902 assert( value
== TRUE
|| value
== FALSE
);
906 zone
->exhaustible
= value
;
909 zone
->collectable
= value
;
912 zone
->expandable
= value
;
915 zone
->allows_foreign
= value
;
919 panic("Zone_change: Wrong Item Type!");
923 lock_zone_init(zone
);
927 * Return the expected number of free elements in the zone.
928 * This calculation will be incorrect if items are zfree'd that
929 * were never zalloc'd/zget'd. The correct way to stuff memory
930 * into a zone is by zcram.
934 zone_free_count(zone_t zone
)
936 integer_t free_count
;
939 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
942 assert(free_count
>= 0);
948 * zprealloc preallocates wired memory, exanding the specified
949 * zone to the specified size
959 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
961 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
962 zcram(zone
, addr
, size
);
967 * Zone garbage collection subroutines
971 zone_page_collectable(
975 struct zone_page_table_entry
*zp
;
979 if (!from_zone_map(addr
, size
))
980 panic("zone_page_collectable");
983 i
= atop_32(addr
-zone_map_min_address
);
984 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
986 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
987 if (zp
->collect_count
== zp
->alloc_count
)
998 struct zone_page_table_entry
*zp
;
1002 if (!from_zone_map(addr
, size
))
1003 panic("zone_page_keep");
1006 i
= atop_32(addr
-zone_map_min_address
);
1007 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1009 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1010 zp
->collect_count
= 0;
1018 struct zone_page_table_entry
*zp
;
1022 if (!from_zone_map(addr
, size
))
1023 panic("zone_page_collect");
1026 i
= atop_32(addr
-zone_map_min_address
);
1027 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1029 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++)
1030 ++zp
->collect_count
;
1039 struct zone_page_table_entry
*zp
;
1043 if (!from_zone_map(addr
, size
))
1044 panic("zone_page_init");
1047 i
= atop_32(addr
-zone_map_min_address
);
1048 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1050 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1051 zp
->alloc_count
= value
;
1052 zp
->collect_count
= 0;
1061 struct zone_page_table_entry
*zp
;
1065 if (!from_zone_map(addr
, size
))
1066 panic("zone_page_alloc");
1069 i
= atop_32(addr
-zone_map_min_address
);
1070 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1072 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1074 * Set alloc_count to (ZONE_PAGE_USED + 1) if
1075 * it was previously set to ZONE_PAGE_UNUSED.
1077 if (zp
->alloc_count
== ZONE_PAGE_UNUSED
)
1078 zp
->alloc_count
= 1;
1085 zone_page_free_element(
1086 struct zone_page_table_entry
**free_pages
,
1090 struct zone_page_table_entry
*zp
;
1094 if (!from_zone_map(addr
, size
))
1095 panic("zone_page_free_element");
1098 i
= atop_32(addr
-zone_map_min_address
);
1099 j
= atop_32((addr
+size
-1) - zone_map_min_address
);
1101 for (zp
= zone_page_table
+ i
; i
<= j
; zp
++, i
++) {
1102 if (zp
->collect_count
> 0)
1103 --zp
->collect_count
;
1104 if (--zp
->alloc_count
== 0) {
1105 zp
->alloc_count
= ZONE_PAGE_UNUSED
;
1106 zp
->collect_count
= 0;
1108 zp
->link
= *free_pages
;
1115 /* This is used for walking through a zone's free element list.
1117 struct zone_free_element
{
1118 struct zone_free_element
* next
;
1124 uint32_t elems_collected
,
1129 /* Zone garbage collection
1131 * zone_gc will walk through all the free elements in all the
1132 * zones that are marked collectable looking for reclaimable
1133 * pages. zone_gc is called by consider_zone_gc when the system
1134 * begins to run out of memory.
1139 unsigned int max_zones
;
1142 struct zone_page_table_entry
*zp
, *zone_free_pages
;
1144 mutex_lock(&zone_gc_lock
);
1146 simple_lock(&all_zones_lock
);
1147 max_zones
= num_zones
;
1149 simple_unlock(&all_zones_lock
);
1152 for (i
= 0; i
< zone_pages
; i
++)
1153 assert(zone_page_table
[i
].collect_count
== 0);
1154 #endif /* MACH_ASSERT */
1156 zone_free_pages
= NULL
;
1158 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1160 vm_size_t elt_size
, size_freed
;
1161 struct zone_free_element
*elt
, *prev
, *scan
, *keep
, *tail
;
1163 assert(z
!= ZONE_NULL
);
1165 if (!z
->collectable
)
1170 elt_size
= z
->elem_size
;
1173 * Do a quick feasability check before we scan the zone:
1174 * skip unless there is likelihood of getting 1+ pages back.
1176 if (z
->cur_size
- z
->count
* elt_size
<= 2 * PAGE_SIZE
){
1182 * Snatch all of the free elements away from the zone.
1185 scan
= (void *)z
->free_elements
;
1186 (void *)z
->free_elements
= NULL
;
1193 * Determine which elements we can attempt to collect
1194 * and count them up in the page table. Foreign elements
1195 * are returned to the zone.
1198 prev
= (void *)&scan
;
1200 n
= 0; tail
= keep
= NULL
;
1201 while (elt
!= NULL
) {
1202 if (from_zone_map(elt
, elt_size
)) {
1203 zone_page_collect((vm_offset_t
)elt
, elt_size
);
1208 ++zgc_stats
.elems_collected
;
1214 tail
= tail
->next
= elt
;
1216 elt
= prev
->next
= elt
->next
;
1221 * Dribble back the elements we are keeping.
1224 if (++n
>= 50 && keep
!= NULL
) {
1227 tail
->next
= (void *)z
->free_elements
;
1228 (void *)z
->free_elements
= keep
;
1232 n
= 0; tail
= keep
= NULL
;
1237 * Return any remaining elements.
1243 tail
->next
= (void *)z
->free_elements
;
1244 (void *)z
->free_elements
= keep
;
1252 * Determine which pages we can reclaim and
1253 * free those elements.
1257 prev
= (void *)&scan
;
1259 n
= 0; tail
= keep
= NULL
;
1260 while (elt
!= NULL
) {
1261 if (zone_page_collectable((vm_offset_t
)elt
, elt_size
)) {
1262 size_freed
+= elt_size
;
1263 zone_page_free_element(&zone_free_pages
,
1264 (vm_offset_t
)elt
, elt_size
);
1266 elt
= prev
->next
= elt
->next
;
1268 ++zgc_stats
.elems_freed
;
1271 zone_page_keep((vm_offset_t
)elt
, elt_size
);
1276 tail
= tail
->next
= elt
;
1278 elt
= prev
->next
= elt
->next
;
1281 ++zgc_stats
.elems_kept
;
1285 * Dribble back the elements we are keeping,
1286 * and update the zone size info.
1289 if (++n
>= 50 && keep
!= NULL
) {
1292 z
->cur_size
-= size_freed
;
1295 tail
->next
= (void *)z
->free_elements
;
1296 (void *)z
->free_elements
= keep
;
1300 n
= 0; tail
= keep
= NULL
;
1305 * Return any remaining elements, and update
1306 * the zone size info.
1309 if (size_freed
> 0 || keep
!= NULL
) {
1312 z
->cur_size
-= size_freed
;
1315 tail
->next
= (void *)z
->free_elements
;
1316 (void *)z
->free_elements
= keep
;
1324 * Reclaim the pages we are freeing.
1327 while ((zp
= zone_free_pages
) != NULL
) {
1328 zone_free_pages
= zp
->link
;
1329 kmem_free(zone_map
, zone_map_min_address
+ PAGE_SIZE
*
1330 (zp
- zone_page_table
), PAGE_SIZE
);
1331 ++zgc_stats
.pgs_freed
;
1334 mutex_unlock(&zone_gc_lock
);
1340 * Called by the pageout daemon when the system needs more free pages.
1344 consider_zone_gc(void)
1347 * By default, don't attempt zone GC more frequently
1348 * than once / 2 seconds.
1351 if (zone_gc_max_rate
== 0)
1352 zone_gc_max_rate
= (2 << SCHED_TICK_SHIFT
) + 1;
1354 if (zone_gc_allowed
&&
1355 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1357 zone_gc_forced
= FALSE
;
1358 zone_gc_last_tick
= sched_tick
;
1363 #include <mach/kern_return.h>
1364 #include <mach/machine/vm_types.h>
1365 #include <mach_debug/zone_info.h>
1366 #include <kern/host.h>
1367 #include <vm/vm_map.h>
1368 #include <vm/vm_kern.h>
1370 #include <mach/mach_host_server.h>
1375 zone_name_array_t
*namesp
,
1376 mach_msg_type_number_t
*namesCntp
,
1377 zone_info_array_t
*infop
,
1378 mach_msg_type_number_t
*infoCntp
)
1381 vm_offset_t names_addr
;
1382 vm_size_t names_size
;
1384 vm_offset_t info_addr
;
1385 vm_size_t info_size
;
1386 unsigned int max_zones
, i
;
1392 if (host
== HOST_NULL
)
1393 return KERN_INVALID_HOST
;
1396 * We assume that zones aren't freed once allocated.
1397 * We won't pick up any zones that are allocated later.
1400 simple_lock(&all_zones_lock
);
1402 max_zones
= num_zones
+ 4;
1404 max_zones
= num_zones
+ 2;
1407 simple_unlock(&all_zones_lock
);
1409 if (max_zones
<= *namesCntp
) {
1410 /* use in-line memory */
1414 names_size
= round_page_32(max_zones
* sizeof *names
);
1415 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1416 &names_addr
, names_size
);
1417 if (kr
!= KERN_SUCCESS
)
1419 names
= (zone_name_t
*) names_addr
;
1422 if (max_zones
<= *infoCntp
) {
1423 /* use in-line memory */
1427 info_size
= round_page_32(max_zones
* sizeof *info
);
1428 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1429 &info_addr
, info_size
);
1430 if (kr
!= KERN_SUCCESS
) {
1431 if (names
!= *namesp
)
1432 kmem_free(ipc_kernel_map
,
1433 names_addr
, names_size
);
1437 info
= (zone_info_t
*) info_addr
;
1442 for (i
= 0; i
< num_zones
; i
++) {
1445 assert(z
!= ZONE_NULL
);
1451 simple_lock(&all_zones_lock
);
1453 simple_unlock(&all_zones_lock
);
1455 /* assuming here the name data is static */
1456 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1457 sizeof zn
->zn_name
);
1459 zi
->zi_count
= zcopy
.count
;
1460 zi
->zi_cur_size
= zcopy
.cur_size
;
1461 zi
->zi_max_size
= zcopy
.max_size
;
1462 zi
->zi_elem_size
= zcopy
.elem_size
;
1463 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1464 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1465 zi
->zi_collectable
= zcopy
.collectable
;
1470 strcpy(zn
->zn_name
, "kernel_stacks");
1471 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1472 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1476 strcpy(zn
->zn_name
, "save_areas");
1477 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1478 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1482 strcpy(zn
->zn_name
, "pmap_mappings");
1483 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1484 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1488 strcpy(zn
->zn_name
, "kalloc.large");
1489 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1490 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1492 if (names
!= *namesp
) {
1496 used
= max_zones
* sizeof *names
;
1498 if (used
!= names_size
)
1499 bzero((char *) (names_addr
+ used
), names_size
- used
);
1501 kr
= vm_map_copyin(ipc_kernel_map
, names_addr
, names_size
,
1503 assert(kr
== KERN_SUCCESS
);
1505 *namesp
= (zone_name_t
*) copy
;
1507 *namesCntp
= max_zones
;
1509 if (info
!= *infop
) {
1513 used
= max_zones
* sizeof *info
;
1515 if (used
!= info_size
)
1516 bzero((char *) (info_addr
+ used
), info_size
- used
);
1518 kr
= vm_map_copyin(ipc_kernel_map
, info_addr
, info_size
,
1520 assert(kr
== KERN_SUCCESS
);
1522 *infop
= (zone_info_t
*) copy
;
1524 *infoCntp
= max_zones
;
1526 return KERN_SUCCESS
;
1530 #include <ddb/db_command.h>
1531 #include <ddb/db_output.h>
1532 #include <kern/kern_print.h>
1534 const char *zone_labels
=
1535 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1542 void db_zone_check_active(
1544 void db_zone_print_active(
1546 #endif /* ZONE_DEBUG */
1547 void db_zone_print_free(
1557 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1558 addr
, zcopy
.count
, zcopy
.cur_size
,
1559 zcopy
.max_size
, zcopy
.elem_size
,
1560 zcopy
.alloc_size
, zcopy
.zone_name
);
1561 if (zcopy
.exhaustible
)
1563 if (zcopy
.collectable
)
1565 if (zcopy
.expandable
)
1578 struct zone
*z
= (zone_t
)addr
;
1580 if (z
== ZONE_NULL
|| !have_addr
){
1581 db_error("No Zone\n");
1585 db_printf("%s\n", zone_labels
);
1601 * Don't risk hanging by unconditionally locking,
1602 * risk of incoherent data is small (zones aren't freed).
1604 have_addr
= simple_lock_try(&all_zones_lock
);
1608 simple_unlock(&all_zones_lock
);
1611 db_printf("%s\n", zone_labels
);
1612 for ( ; count
> 0; count
--) {
1614 db_error("Mangled Zone List\n");
1618 total
+= z
->cur_size
,
1620 have_addr
= simple_lock_try(&all_zones_lock
);
1623 simple_unlock(&all_zones_lock
);
1626 db_printf("\nTotal %8x", total
);
1627 db_printf("\n\nzone_gc() has reclaimed %d pages\n", zgc_stats
.pgs_freed
);
1632 db_zone_check_active(
1638 if (!zone_debug_enabled(zone
) || !zone_check
)
1640 tmp_elem
= queue_first(&zone
->active_zones
);
1641 while (count
< zone
->count
) {
1643 if (tmp_elem
== 0) {
1644 printf("unexpected zero element, zone=0x%x, count=%d\n",
1649 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1650 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1655 tmp_elem
= queue_next(tmp_elem
);
1657 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1658 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1665 db_zone_print_active(
1671 if (!zone_debug_enabled(zone
)) {
1672 printf("zone 0x%x debug not enabled\n", zone
);
1676 printf("zone_check FALSE\n");
1680 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1681 printf("active list:\n");
1682 tmp_elem
= queue_first(&zone
->active_zones
);
1683 while (count
< zone
->count
) {
1684 printf(" 0x%x", tmp_elem
);
1686 if ((count
% 6) == 0)
1688 if (tmp_elem
== 0) {
1689 printf("\nunexpected zero element, count=%d\n", count
);
1692 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1693 printf("\nunexpected queue_end, count=%d\n", count
);
1696 tmp_elem
= queue_next(tmp_elem
);
1698 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1699 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1703 #endif /* ZONE_DEBUG */
1713 freecount
= zone_free_count(zone
);
1714 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1715 printf("free list:\n");
1716 elem
= zone
->free_elements
;
1717 while (count
< freecount
) {
1718 printf(" 0x%x", elem
);
1720 if ((count
% 6) == 0)
1723 printf("\nunexpected zero element, count=%d\n", count
);
1726 elem
= *((vm_offset_t
*)elem
);
1729 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1734 #endif /* MACH_KDB */
1739 /* should we care about locks here ? */
1747 if (!zone_debug_enabled(z
))
1749 elt
-= ZONE_DEBUG_OFFSET
;
1750 elt
= (vm_offset_t
) queue_next((queue_t
) elt
);
1751 if ((queue_t
) elt
== &z
->active_zones
)
1753 elt
+= ZONE_DEBUG_OFFSET
;
1763 if (!zone_debug_enabled(z
))
1765 if (queue_empty(&z
->active_zones
))
1767 elt
= (vm_offset_t
) queue_first(&z
->active_zones
);
1768 elt
+= ZONE_DEBUG_OFFSET
;
1773 * Second arg controls how many zone elements are printed:
1776 * n, n > 0 => last n on active list
1785 boolean_t print
= (tail
!= 0);
1789 if (z
->count
< tail
)
1791 tail
= z
->count
- tail
;
1792 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1793 if (print
&& tail
<= count
)
1794 db_printf("%8x\n", elt
);
1797 assert(count
== z
->count
);
1800 #endif /* MACH_KDB */
1802 #define zone_in_use(z) ( z->count || z->free_elements )
1808 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1809 z
->alloc_size
< (z
->elem_size
+ ZONE_DEBUG_OFFSET
))
1811 queue_init(&z
->active_zones
);
1812 z
->elem_size
+= ZONE_DEBUG_OFFSET
;
1819 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1821 z
->elem_size
-= ZONE_DEBUG_OFFSET
;
1822 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1824 #endif /* ZONE_DEBUG */