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 #endif /* ZONE_DEBUG */
151 * Support for garbage collection of unused zone pages:
154 struct zone_page_table_entry
{
155 struct zone_page_table_entry
*next
;
160 extern struct zone_page_table_entry
* zone_page_table
;
162 #define lock_zone_page_table() simple_lock(&zone_page_table_lock)
163 #define unlock_zone_page_table() simple_unlock(&zone_page_table_lock)
165 #define zone_page(addr) \
166 (&(zone_page_table[(atop(((vm_offset_t)addr) - zone_map_min_address))]))
174 void zone_page_alloc(
178 void zone_add_free_page_list(
179 struct zone_page_table_entry
**free_list
,
182 void zone_page_dealloc(
186 void zone_page_in_use(
194 boolean_t
zone_page_collectable(
203 thread_call_param_t p0
,
204 thread_call_param_t p1
);
207 #if ZONE_DEBUG && MACH_KDB
211 #endif /* ZONE_DEBUG && MACH_KDB */
213 vm_map_t zone_map
= VM_MAP_NULL
;
215 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
218 * The VM system gives us an initial chunk of memory.
219 * It has to be big enough to allocate the zone_zone
223 vm_size_t zdata_size
;
225 #define lock_zone(zone) \
227 simple_lock(&zone->lock); \
230 #define unlock_zone(zone) \
232 simple_unlock(&zone->lock); \
235 #define lock_zone_init(zone) \
237 simple_lock_init(&zone->lock, ETAP_MISC_ZONE); \
240 #define lock_try_zone(zone) simple_lock_try(&zone->lock)
242 kern_return_t
zget_space(
244 vm_offset_t
*result
);
246 decl_simple_lock_data(,zget_space_lock
)
247 vm_offset_t zalloc_next_space
;
248 vm_offset_t zalloc_end_of_space
;
249 vm_size_t zalloc_wasted_space
;
252 * Garbage collection map information
254 decl_simple_lock_data(, zone_page_table_lock
)
255 struct zone_page_table_entry
* zone_page_table
;
256 vm_offset_t zone_map_min_address
;
257 vm_offset_t zone_map_max_address
;
258 integer_t zone_pages
;
261 * Exclude more than one concurrent garbage collection
263 decl_mutex_data(, zone_gc_lock
)
265 #define from_zone_map(addr) \
266 ((vm_offset_t)(addr) >= zone_map_min_address && \
267 (vm_offset_t)(addr) < zone_map_max_address)
269 #define ZONE_PAGE_USED 0
270 #define ZONE_PAGE_UNUSED -1
274 * Protects first_zone, last_zone, num_zones,
275 * and the next_zone field of zones.
277 decl_simple_lock_data(, all_zones_lock
)
282 boolean_t zone_gc_allowed
= TRUE
;
283 boolean_t zone_gc_forced
= FALSE
;
284 unsigned zone_gc_last_tick
= 0;
285 unsigned zone_gc_max_rate
= 0; /* in ticks */
289 * zinit initializes a new zone. The zone data structures themselves
290 * are stored in a zone, which is initially a static structure that
291 * is initialized by zone_init.
295 vm_size_t size
, /* the size of an element */
296 vm_size_t max
, /* maximum memory to use */
297 vm_size_t alloc
, /* allocation size */
298 char *name
) /* a name for the zone */
302 if (zone_zone
== ZONE_NULL
) {
303 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
307 z
= (zone_t
) zalloc(zone_zone
);
312 * Round off all the parameters appropriately.
314 if (size
< sizeof(z
->free_elements
))
315 size
= sizeof(z
->free_elements
);
316 size
= ((size
-1) + sizeof(z
->free_elements
)) -
317 ((size
-1) % sizeof(z
->free_elements
));
320 alloc
= round_page(alloc
);
321 max
= round_page(max
);
323 * We look for an allocation size with least fragmentation
324 * in the range of 1 - 5 pages. This size will be used unless
325 * the user suggestion is larger AND has less fragmentation
327 { vm_size_t best
, waste
; unsigned int i
;
330 for (i
= 2; i
<= 5; i
++){ vm_size_t tsize
, twaste
;
331 tsize
= i
* PAGE_SIZE
;
332 twaste
= tsize
% size
;
334 best
= tsize
, waste
= twaste
;
336 if (alloc
<= best
|| (alloc
% size
>= waste
))
339 if (max
&& (max
< alloc
))
342 z
->free_elements
= 0;
346 z
->alloc_size
= alloc
;
349 z
->doing_alloc
= FALSE
;
350 z
->exhaustible
= FALSE
;
351 z
->collectable
= TRUE
;
352 z
->allows_foreign
= FALSE
;
353 z
->expandable
= TRUE
;
355 z
->async_pending
= FALSE
;
358 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
359 zone_debug_enable(z
);
360 #endif /* ZONE_DEBUG */
364 * Add the zone to the all-zones list.
367 z
->next_zone
= ZONE_NULL
;
368 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
369 simple_lock(&all_zones_lock
);
371 last_zone
= &z
->next_zone
;
373 simple_unlock(&all_zones_lock
);
379 * Cram the given memory into the specified zone.
383 register zone_t zone
,
387 register vm_size_t elem_size
;
389 /* Basic sanity checks */
390 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
391 assert(!zone
->collectable
|| zone
->allows_foreign
392 || (from_zone_map(newmem
) && from_zone_map(newmem
+size
-1)));
394 elem_size
= zone
->elem_size
;
397 while (size
>= elem_size
) {
398 ADD_TO_ZONE(zone
, newmem
);
399 if (from_zone_map(newmem
))
400 zone_page_alloc(newmem
, elem_size
);
401 zone
->count
++; /* compensate for ADD_TO_ZONE */
404 zone
->cur_size
+= elem_size
;
410 * Contiguous space allocator for non-paged zones. Allocates "size" amount
411 * of memory from zone_map.
419 vm_offset_t new_space
= 0;
420 vm_size_t space_to_add
;
422 simple_lock(&zget_space_lock
);
423 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
425 * Add at least one page to allocation area.
428 space_to_add
= round_page(size
);
430 if (new_space
== 0) {
431 kern_return_t retval
;
433 * Memory cannot be wired down while holding
434 * any locks that the pageout daemon might
435 * need to free up pages. [Making the zget_space
436 * lock a complex lock does not help in this
439 * Unlock and allocate memory. Because several
440 * threads might try to do this at once, don't
441 * use the memory before checking for available
445 simple_unlock(&zget_space_lock
);
447 retval
= kernel_memory_allocate(zone_map
, &new_space
,
448 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
449 if (retval
!= KERN_SUCCESS
)
451 zone_page_init(new_space
, space_to_add
,
453 simple_lock(&zget_space_lock
);
459 * Memory was allocated in a previous iteration.
461 * Check whether the new region is contiguous
465 if (new_space
!= zalloc_end_of_space
) {
467 * Throw away the remainder of the
468 * old space, and start a new one.
470 zalloc_wasted_space
+=
471 zalloc_end_of_space
- zalloc_next_space
;
472 zalloc_next_space
= new_space
;
475 zalloc_end_of_space
= new_space
+ space_to_add
;
479 *result
= zalloc_next_space
;
480 zalloc_next_space
+= size
;
481 simple_unlock(&zget_space_lock
);
484 kmem_free(zone_map
, new_space
, space_to_add
);
486 return(KERN_SUCCESS
);
491 * Steal memory for the zone package. Called from
492 * vm_page_bootstrap().
495 zone_steal_memory(void)
497 zdata_size
= round_page(128*sizeof(struct zone
));
498 zdata
= pmap_steal_memory(zdata_size
);
503 * Fill a zone with enough memory to contain at least nelem elements.
504 * Memory is obtained with kmem_alloc_wired from the kernel_map.
505 * Return the number of elements actually put into the zone, which may
506 * be more than the caller asked for since the memory allocation is
507 * rounded up to a full page.
522 size
= nelem
* zone
->elem_size
;
523 size
= round_page(size
);
524 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
525 if (kr
!= KERN_SUCCESS
)
528 zone_change(zone
, Z_FOREIGN
, TRUE
);
529 zcram(zone
, memory
, size
);
530 nalloc
= size
/ zone
->elem_size
;
531 assert(nalloc
>= nelem
);
537 * Initialize the "zone of zones" which uses fixed memory allocated
538 * earlier in memory initialization. zone_bootstrap is called
544 vm_size_t zone_zone_size
;
545 vm_offset_t zone_zone_space
;
547 simple_lock_init(&all_zones_lock
, ETAP_MISC_ZONE_ALL
);
549 first_zone
= ZONE_NULL
;
550 last_zone
= &first_zone
;
553 simple_lock_init(&zget_space_lock
, ETAP_MISC_ZONE_GET
);
554 zalloc_next_space
= zdata
;
555 zalloc_end_of_space
= zdata
+ zdata_size
;
556 zalloc_wasted_space
= 0;
558 /* assertion: nobody else called zinit before us */
559 assert(zone_zone
== ZONE_NULL
);
560 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
561 sizeof(struct zone
), "zones");
562 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
563 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
564 zget_space(zone_zone_size
, &zone_zone_space
);
565 zcram(zone_zone
, zone_zone_space
, zone_zone_size
);
570 vm_size_t max_zonemap_size
)
572 kern_return_t retval
;
573 vm_offset_t zone_min
;
574 vm_offset_t zone_max
;
575 vm_size_t zone_table_size
;
577 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
578 FALSE
, TRUE
, &zone_map
);
579 if (retval
!= KERN_SUCCESS
)
580 panic("zone_init: kmem_suballoc failed");
581 zone_max
= zone_min
+ round_page(max_zonemap_size
);
583 * Setup garbage collection information:
585 zone_table_size
= atop(zone_max
- zone_min
) *
586 sizeof(struct zone_page_table_entry
);
587 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
588 zone_table_size
) != KERN_SUCCESS
)
590 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
591 zone_pages
= atop(zone_max
- zone_min
);
592 zone_map_min_address
= zone_min
;
593 zone_map_max_address
= zone_max
;
594 simple_lock_init(&zone_page_table_lock
, ETAP_MISC_ZONE_PTABLE
);
595 mutex_init(&zone_gc_lock
, ETAP_NO_TRACE
);
596 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
601 * zalloc returns an element from the specified zone.
605 register zone_t zone
,
609 kern_return_t retval
;
611 assert(zone
!= ZONE_NULL
);
612 check_simple_locks();
616 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
618 while ((addr
== 0) && canblock
) {
620 * If nothing was there, try to get more
622 if (zone
->doing_alloc
) {
624 * Someone is allocating memory for this zone.
625 * Wait for it to show up, then try again.
627 assert_wait((event_t
)zone
, THREAD_UNINT
);
628 zone
->waiting
= TRUE
;
630 thread_block((void (*)(void)) 0);
634 if ((zone
->cur_size
+ zone
->elem_size
) >
636 if (zone
->exhaustible
)
638 if (zone
->expandable
) {
640 * We're willing to overflow certain
641 * zones, but not without complaining.
643 * This is best used in conjunction
644 * with the collectable flag. What we
645 * want is an assurance we can get the
646 * memory back, assuming there's no
649 zone
->max_size
+= (zone
->max_size
>> 1);
653 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
656 zone
->doing_alloc
= TRUE
;
659 if (zone
->collectable
) {
661 vm_size_t alloc_size
;
665 round_page(zone
->elem_size
);
667 alloc_size
= zone
->alloc_size
;
669 retval
= kernel_memory_allocate(zone_map
,
670 &space
, alloc_size
, 0,
671 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
672 if (retval
== KERN_SUCCESS
) {
673 zone_page_init(space
, alloc_size
,
675 zcram(zone
, space
, alloc_size
);
676 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
677 /* would like to cause a zone_gc() */
682 zone
->doing_alloc
= FALSE
;
684 zone
->waiting
= FALSE
;
685 thread_wakeup((event_t
)zone
);
687 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
689 retval
== KERN_RESOURCE_SHORTAGE
) {
697 retval
= zget_space(zone
->elem_size
, &space
);
700 zone
->doing_alloc
= FALSE
;
702 zone
->waiting
= FALSE
;
703 thread_wakeup((event_t
)zone
);
705 if (retval
== KERN_SUCCESS
) {
707 zone
->cur_size
+= zone
->elem_size
;
709 if (zone_debug_enabled(zone
)) {
710 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
714 zone_page_alloc(space
, zone
->elem_size
);
716 if (zone_debug_enabled(zone
))
717 space
+= sizeof(queue_chain_t
);
721 if (retval
== KERN_RESOURCE_SHORTAGE
) {
732 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
735 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
736 zone
->async_pending
= TRUE
;
738 thread_call_enter(&zone
->call_async_alloc
);
740 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
744 if (addr
&& zone_debug_enabled(zone
)) {
745 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
746 addr
+= sizeof(queue_chain_t
);
758 register zone_t zone
)
760 return( zalloc_canblock(zone
, TRUE
) );
765 register zone_t zone
)
767 return( zalloc_canblock(zone
, FALSE
) );
772 thread_call_param_t p0
,
773 thread_call_param_t p1
)
777 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
778 zfree((zone_t
)p0
, elt
);
779 lock_zone(((zone_t
)p0
));
780 ((zone_t
)p0
)->async_pending
= FALSE
;
781 unlock_zone(((zone_t
)p0
));
786 * zget returns an element from the specified zone
787 * and immediately returns nothing if there is nothing there.
789 * This form should be used when you can not block (like when
790 * processing an interrupt).
794 register zone_t zone
)
796 register vm_offset_t addr
;
798 assert( zone
!= ZONE_NULL
);
800 if (!lock_try_zone(zone
))
801 return ((vm_offset_t
)0);
803 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
805 if (addr
&& zone_debug_enabled(zone
)) {
806 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
807 addr
+= sizeof(queue_chain_t
);
809 #endif /* ZONE_DEBUG */
815 /* Keep this FALSE by default. Large memory machine run orders of magnitude
816 slower in debug mode when true. Use debugger to enable if needed */
817 boolean_t zone_check
= FALSE
;
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!");
832 if (zone
->collectable
&& !zone
->allows_foreign
&&
833 (!from_zone_map(elem
) || !from_zone_map(elem
+zone
->elem_size
-1)))
834 panic("zfree: non-allocated memory in collectable zone!");
839 if (zone_debug_enabled(zone
)) {
842 elem
-= sizeof(queue_chain_t
);
844 /* check the zone's consistency */
846 for (tmp_elem
= queue_first(&zone
->active_zones
);
847 !queue_end(tmp_elem
, &zone
->active_zones
);
848 tmp_elem
= queue_next(tmp_elem
))
849 if (elem
== (vm_offset_t
)tmp_elem
)
851 if (elem
!= (vm_offset_t
)tmp_elem
)
852 panic("zfree()ing element from wrong zone");
854 remqueue(&zone
->active_zones
, (queue_t
) elem
);
856 #endif /* ZONE_DEBUG */
860 /* check the zone's consistency */
862 for (this = zone
->free_elements
;
864 this = * (vm_offset_t
*) this)
865 if (!pmap_kernel_va(this) || this == elem
)
868 ADD_TO_ZONE(zone
, elem
);
871 * If elements have one or more pages, and memory is low,
872 * request to run the garbage collection in the zone the next
873 * time the pageout thread runs.
875 if (zone
->elem_size
>= PAGE_SIZE
&&
877 zone_gc_forced
= TRUE
;
883 /* Change a zone's flags.
884 * This routine must be called immediately after zinit.
892 assert( zone
!= ZONE_NULL
);
893 assert( value
== TRUE
|| value
== FALSE
);
897 zone
->exhaustible
= value
;
900 zone
->collectable
= value
;
903 zone
->expandable
= value
;
906 zone
->allows_foreign
= value
;
910 panic("Zone_change: Wrong Item Type!");
914 lock_zone_init(zone
);
918 * Return the expected number of free elements in the zone.
919 * This calculation will be incorrect if items are zfree'd that
920 * were never zalloc'd/zget'd. The correct way to stuff memory
921 * into a zone is by zcram.
925 zone_free_count(zone_t zone
)
927 integer_t free_count
;
930 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
933 assert(free_count
>= 0);
939 * zprealloc preallocates wired memory, exanding the specified
940 * zone to the specified size
950 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
952 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
953 zcram(zone
, addr
, size
);
958 * Zone garbage collection subroutines
960 * These routines have in common the modification of entries in the
961 * zone_page_table. The latter contains one entry for every page
964 * For each page table entry in the given range:
966 * zone_page_collectable - test if one (in_free_list == alloc_count)
967 * zone_page_keep - reset in_free_list
968 * zone_page_in_use - decrements in_free_list
969 * zone_page_free - increments in_free_list
970 * zone_page_init - initializes in_free_list and alloc_count
971 * zone_page_alloc - increments alloc_count
972 * zone_page_dealloc - decrements alloc_count
973 * zone_add_free_page_list - adds the page to the free list
975 * Two counts are maintained for each page, the in_free_list count and
976 * alloc_count. The alloc_count is how many zone elements have been
977 * allocated from a page. (Note that the page could contain elements
978 * that span page boundaries. The count includes these elements so
979 * one element may be counted in two pages.) In_free_list is a count
980 * of how many zone elements are currently free. If in_free_list is
981 * equal to alloc_count then the page is eligible for garbage
984 * Alloc_count and in_free_list are initialized to the correct values
985 * for a particular zone when a page is zcram'ed into a zone. Subsequent
986 * gets and frees of zone elements will call zone_page_in_use and
987 * zone_page_free which modify the in_free_list count. When the zones
988 * garbage collector runs it will walk through a zones free element list,
989 * remove the elements that reside on collectable pages, and use
990 * zone_add_free_page_list to create a list of pages to be collected.
993 zone_page_collectable(
1000 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1001 panic("zone_page_collectable");
1004 i
= atop(addr
-zone_map_min_address
);
1005 j
= atop((addr
+size
-1) - zone_map_min_address
);
1006 lock_zone_page_table();
1007 for (; i
<= j
; i
++) {
1008 if (zone_page_table
[i
].in_free_list
==
1009 zone_page_table
[i
].alloc_count
) {
1010 unlock_zone_page_table();
1014 unlock_zone_page_table();
1026 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1027 panic("zone_page_keep");
1030 i
= atop(addr
-zone_map_min_address
);
1031 j
= atop((addr
+size
-1) - zone_map_min_address
);
1032 lock_zone_page_table();
1033 for (; i
<= j
; i
++) {
1034 zone_page_table
[i
].in_free_list
= 0;
1036 unlock_zone_page_table();
1047 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1048 panic("zone_page_in_use");
1051 i
= atop(addr
-zone_map_min_address
);
1052 j
= atop((addr
+size
-1) - zone_map_min_address
);
1053 lock_zone_page_table();
1054 for (; i
<= j
; i
++) {
1055 if (zone_page_table
[i
].in_free_list
> 0)
1056 zone_page_table
[i
].in_free_list
--;
1058 unlock_zone_page_table();
1069 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1070 panic("zone_page_free");
1073 i
= atop(addr
-zone_map_min_address
);
1074 j
= atop((addr
+size
-1) - zone_map_min_address
);
1075 lock_zone_page_table();
1076 for (; i
<= j
; i
++) {
1077 assert(zone_page_table
[i
].in_free_list
>= 0);
1078 zone_page_table
[i
].in_free_list
++;
1080 unlock_zone_page_table();
1092 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1093 panic("zone_page_init");
1096 i
= atop(addr
-zone_map_min_address
);
1097 j
= atop((addr
+size
-1) - zone_map_min_address
);
1098 lock_zone_page_table();
1099 for (; i
<= j
; i
++) {
1100 zone_page_table
[i
].alloc_count
= value
;
1101 zone_page_table
[i
].in_free_list
= 0;
1103 unlock_zone_page_table();
1114 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1115 panic("zone_page_alloc");
1118 i
= atop(addr
-zone_map_min_address
);
1119 j
= atop((addr
+size
-1) - zone_map_min_address
);
1120 lock_zone_page_table();
1121 for (; i
<= j
; i
++) {
1122 /* Set alloc_count to (ZONE_PAGE_USED + 1) if
1123 * it was previously set to ZONE_PAGE_UNUSED.
1125 if (zone_page_table
[i
].alloc_count
== ZONE_PAGE_UNUSED
) {
1126 zone_page_table
[i
].alloc_count
= 1;
1128 zone_page_table
[i
].alloc_count
++;
1131 unlock_zone_page_table();
1142 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1143 panic("zone_page_dealloc");
1146 i
= atop(addr
-zone_map_min_address
);
1147 j
= atop((addr
+size
-1) - zone_map_min_address
);
1148 lock_zone_page_table();
1149 for (; i
<= j
; i
++) {
1150 zone_page_table
[i
].alloc_count
--;
1152 unlock_zone_page_table();
1156 zone_add_free_page_list(
1157 struct zone_page_table_entry
**free_list
,
1164 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1165 panic("zone_add_free_page_list");
1168 i
= atop(addr
-zone_map_min_address
);
1169 j
= atop((addr
+size
-1) - zone_map_min_address
);
1170 lock_zone_page_table();
1171 for (; i
<= j
; i
++) {
1172 if (zone_page_table
[i
].alloc_count
== 0) {
1173 zone_page_table
[i
].next
= *free_list
;
1174 *free_list
= &zone_page_table
[i
];
1175 zone_page_table
[i
].alloc_count
= ZONE_PAGE_UNUSED
;
1176 zone_page_table
[i
].in_free_list
= 0;
1179 unlock_zone_page_table();
1183 /* This is used for walking through a zone's free element list.
1185 struct zone_free_entry
{
1186 struct zone_free_entry
* next
;
1189 int reclaim_page_count
= 0;
1191 /* Zone garbage collection
1193 * zone_gc will walk through all the free elements in all the
1194 * zones that are marked collectable looking for reclaimable
1195 * pages. zone_gc is called by consider_zone_gc when the system
1196 * begins to run out of memory.
1201 unsigned int max_zones
;
1204 struct zone_page_table_entry
*freep
;
1205 struct zone_page_table_entry
*zone_free_page_list
;
1207 mutex_lock(&zone_gc_lock
);
1210 * Note that this scheme of locking only to walk the zone list
1211 * assumes that zones are never freed (checked by zfree)
1213 simple_lock(&all_zones_lock
);
1214 max_zones
= num_zones
;
1216 simple_unlock(&all_zones_lock
);
1219 lock_zone_page_table();
1220 for (i
= 0; i
< zone_pages
; i
++)
1221 assert(zone_page_table
[i
].in_free_list
== 0);
1222 unlock_zone_page_table();
1223 #endif /* MACH_ASSERT */
1225 zone_free_page_list
= (struct zone_page_table_entry
*) 0;
1227 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1228 struct zone_free_entry
* prev
;
1229 struct zone_free_entry
* elt
;
1230 struct zone_free_entry
* end
;
1232 assert(z
!= ZONE_NULL
);
1234 if (!z
->collectable
)
1240 * Do a quick feasability check before we scan the zone:
1241 * skip unless there is likelihood of getting 1+ pages back.
1243 if ((z
->cur_size
- z
->count
* z
->elem_size
) <= (2*PAGE_SIZE
)){
1248 /* Count the free elements in each page. This loop
1249 * requires that all in_free_list entries are zero.
1251 * Exit the loop early if we need to hurry up and drop
1252 * the lock to allow preemption - but we must fully process
1253 * all elements we looked at so far.
1255 elt
= (struct zone_free_entry
*)(z
->free_elements
);
1256 while (!ast_urgency() && (elt
!= (struct zone_free_entry
*)0)) {
1257 if (from_zone_map(elt
))
1258 zone_page_free((vm_offset_t
)elt
, z
->elem_size
);
1263 /* Now determine which elements should be removed
1264 * from the free list and, after all the elements
1265 * on a page have been removed, add the element's
1266 * page to a list of pages to be freed.
1268 prev
= elt
= (struct zone_free_entry
*)(z
->free_elements
);
1269 while (elt
!= end
) {
1270 if (!from_zone_map(elt
)) {
1275 if (zone_page_collectable((vm_offset_t
)elt
,
1277 z
->cur_size
-= z
->elem_size
;
1278 zone_page_in_use((vm_offset_t
)elt
,
1280 zone_page_dealloc((vm_offset_t
)elt
,
1282 zone_add_free_page_list(&zone_free_page_list
,
1287 z
->free_elements
=(vm_offset_t
)elt
;
1290 prev
->next
= elt
->next
;
1294 /* This element is not eligible for collection
1295 * so clear in_free_list in preparation for a
1296 * subsequent garbage collection pass.
1298 zone_page_keep((vm_offset_t
)elt
, z
->elem_size
);
1302 } /* end while(elt != end) */
1307 for (freep
= zone_free_page_list
; freep
!= 0; freep
= freep
->next
) {
1308 vm_offset_t free_addr
;
1310 free_addr
= zone_map_min_address
+
1311 PAGE_SIZE
* (freep
- zone_page_table
);
1312 kmem_free(zone_map
, free_addr
, PAGE_SIZE
);
1313 reclaim_page_count
++;
1315 mutex_unlock(&zone_gc_lock
);
1321 * Called by the pageout daemon when the system needs more free pages.
1325 consider_zone_gc(void)
1328 * By default, don't attempt zone GC more frequently
1329 * than once a second.
1332 if (zone_gc_max_rate
== 0)
1333 zone_gc_max_rate
= (1 << SCHED_TICK_SHIFT
) + 1;
1335 if (zone_gc_allowed
&&
1336 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1338 zone_gc_forced
= FALSE
;
1339 zone_gc_last_tick
= sched_tick
;
1344 #include <mach/kern_return.h>
1345 #include <mach/machine/vm_types.h>
1346 #include <mach_debug/zone_info.h>
1347 #include <kern/host.h>
1348 #include <vm/vm_map.h>
1349 #include <vm/vm_kern.h>
1351 #include <mach/mach_host_server.h>
1356 zone_name_array_t
*namesp
,
1357 mach_msg_type_number_t
*namesCntp
,
1358 zone_info_array_t
*infop
,
1359 mach_msg_type_number_t
*infoCntp
)
1362 vm_offset_t names_addr
;
1363 vm_size_t names_size
;
1365 vm_offset_t info_addr
;
1366 vm_size_t info_size
;
1367 unsigned int max_zones
, i
;
1373 if (host
== HOST_NULL
)
1374 return KERN_INVALID_HOST
;
1377 * We assume that zones aren't freed once allocated.
1378 * We won't pick up any zones that are allocated later.
1381 simple_lock(&all_zones_lock
);
1383 max_zones
= num_zones
+ 4;
1385 max_zones
= num_zones
+ 2;
1388 simple_unlock(&all_zones_lock
);
1390 if (max_zones
<= *namesCntp
) {
1391 /* use in-line memory */
1395 names_size
= round_page(max_zones
* sizeof *names
);
1396 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1397 &names_addr
, names_size
);
1398 if (kr
!= KERN_SUCCESS
)
1400 names
= (zone_name_t
*) names_addr
;
1403 if (max_zones
<= *infoCntp
) {
1404 /* use in-line memory */
1408 info_size
= round_page(max_zones
* sizeof *info
);
1409 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1410 &info_addr
, info_size
);
1411 if (kr
!= KERN_SUCCESS
) {
1412 if (names
!= *namesp
)
1413 kmem_free(ipc_kernel_map
,
1414 names_addr
, names_size
);
1418 info
= (zone_info_t
*) info_addr
;
1423 for (i
= 0; i
< num_zones
; i
++) {
1426 assert(z
!= ZONE_NULL
);
1432 simple_lock(&all_zones_lock
);
1434 simple_unlock(&all_zones_lock
);
1436 /* assuming here the name data is static */
1437 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1438 sizeof zn
->zn_name
);
1440 zi
->zi_count
= zcopy
.count
;
1441 zi
->zi_cur_size
= zcopy
.cur_size
;
1442 zi
->zi_max_size
= zcopy
.max_size
;
1443 zi
->zi_elem_size
= zcopy
.elem_size
;
1444 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1445 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1446 zi
->zi_collectable
= zcopy
.collectable
;
1451 strcpy(zn
->zn_name
, "kernel_stacks");
1452 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1453 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1457 strcpy(zn
->zn_name
, "save_areas");
1458 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1459 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1463 strcpy(zn
->zn_name
, "pmap_mappings");
1464 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1465 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1469 strcpy(zn
->zn_name
, "kalloc.large");
1470 kalloc_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
);
1473 if (names
!= *namesp
) {
1477 used
= max_zones
* sizeof *names
;
1479 if (used
!= names_size
)
1480 bzero((char *) (names_addr
+ used
), names_size
- used
);
1482 kr
= vm_map_copyin(ipc_kernel_map
, names_addr
, names_size
,
1484 assert(kr
== KERN_SUCCESS
);
1486 *namesp
= (zone_name_t
*) copy
;
1488 *namesCntp
= max_zones
;
1490 if (info
!= *infop
) {
1494 used
= max_zones
* sizeof *info
;
1496 if (used
!= info_size
)
1497 bzero((char *) (info_addr
+ used
), info_size
- used
);
1499 kr
= vm_map_copyin(ipc_kernel_map
, info_addr
, info_size
,
1501 assert(kr
== KERN_SUCCESS
);
1503 *infop
= (zone_info_t
*) copy
;
1505 *infoCntp
= max_zones
;
1507 return KERN_SUCCESS
;
1511 #include <ddb/db_command.h>
1512 #include <ddb/db_output.h>
1513 #include <kern/kern_print.h>
1515 const char *zone_labels
=
1516 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1523 void db_zone_check_active(
1525 void db_zone_print_active(
1527 #endif /* ZONE_DEBUG */
1528 void db_zone_print_free(
1538 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1539 addr
, zcopy
.count
, zcopy
.cur_size
,
1540 zcopy
.max_size
, zcopy
.elem_size
,
1541 zcopy
.alloc_size
, zcopy
.zone_name
);
1542 if (zcopy
.exhaustible
)
1544 if (zcopy
.collectable
)
1546 if (zcopy
.expandable
)
1559 struct zone
*z
= (zone_t
)addr
;
1561 if (z
== ZONE_NULL
|| !have_addr
){
1562 db_error("No Zone\n");
1566 db_printf("%s\n", zone_labels
);
1582 * Don't risk hanging by unconditionally locking,
1583 * risk of incoherent data is small (zones aren't freed).
1585 have_addr
= simple_lock_try(&all_zones_lock
);
1589 simple_unlock(&all_zones_lock
);
1592 db_printf("%s\n", zone_labels
);
1593 for ( ; count
> 0; count
--) {
1595 db_error("Mangled Zone List\n");
1599 total
+= z
->cur_size
,
1601 have_addr
= simple_lock_try(&all_zones_lock
);
1604 simple_unlock(&all_zones_lock
);
1607 db_printf("\nTotal %8x", total
);
1608 db_printf("\n\nzone_gc() has reclaimed %d pages\n",
1609 reclaim_page_count
);
1614 db_zone_check_active(
1620 if (!zone_debug_enabled(zone
) || !zone_check
)
1622 tmp_elem
= queue_first(&zone
->active_zones
);
1623 while (count
< zone
->count
) {
1625 if (tmp_elem
== 0) {
1626 printf("unexpected zero element, zone=0x%x, count=%d\n",
1631 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1632 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1637 tmp_elem
= queue_next(tmp_elem
);
1639 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1640 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1647 db_zone_print_active(
1653 if (!zone_debug_enabled(zone
)) {
1654 printf("zone 0x%x debug not enabled\n", zone
);
1658 printf("zone_check FALSE\n");
1662 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1663 printf("active list:\n");
1664 tmp_elem
= queue_first(&zone
->active_zones
);
1665 while (count
< zone
->count
) {
1666 printf(" 0x%x", tmp_elem
);
1668 if ((count
% 6) == 0)
1670 if (tmp_elem
== 0) {
1671 printf("\nunexpected zero element, count=%d\n", count
);
1674 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1675 printf("\nunexpected queue_end, count=%d\n", count
);
1678 tmp_elem
= queue_next(tmp_elem
);
1680 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1681 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1685 #endif /* ZONE_DEBUG */
1695 freecount
= zone_free_count(zone
);
1696 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1697 printf("free list:\n");
1698 elem
= zone
->free_elements
;
1699 while (count
< freecount
) {
1700 printf(" 0x%x", elem
);
1702 if ((count
% 6) == 0)
1705 printf("\nunexpected zero element, count=%d\n", count
);
1708 elem
= *((vm_offset_t
*)elem
);
1711 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1716 #endif /* MACH_KDB */
1721 /* should we care about locks here ? */
1729 if (!zone_debug_enabled(z
))
1731 elt
-= sizeof(queue_chain_t
);
1732 elt
= (vm_offset_t
) queue_next((queue_t
) elt
);
1733 if ((queue_t
) elt
== &z
->active_zones
)
1735 elt
+= sizeof(queue_chain_t
);
1745 if (!zone_debug_enabled(z
))
1747 if (queue_empty(&z
->active_zones
))
1749 elt
= (vm_offset_t
) queue_first(&z
->active_zones
);
1750 elt
+= sizeof(queue_chain_t
);
1755 * Second arg controls how many zone elements are printed:
1758 * n, n > 0 => last n on active list
1767 boolean_t print
= (tail
!= 0);
1771 if (z
->count
< tail
)
1773 tail
= z
->count
- tail
;
1774 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1775 if (print
&& tail
<= count
)
1776 db_printf("%8x\n", elt
);
1779 assert(count
== z
->count
);
1782 #endif /* MACH_KDB */
1784 #define zone_in_use(z) ( z->count || z->free_elements )
1790 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1791 z
->alloc_size
< (z
->elem_size
+ sizeof(queue_chain_t
)))
1793 queue_init(&z
->active_zones
);
1794 z
->elem_size
+= sizeof(queue_chain_t
);
1801 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1803 z
->elem_size
-= sizeof(queue_chain_t
);
1804 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1806 #endif /* ZONE_DEBUG */