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 zone_wakeup(zone) thread_wakeup((event_t)(zone))
236 #define zone_sleep(zone) \
237 thread_sleep_simple_lock((event_t)(zone), \
241 #define lock_zone_init(zone) \
243 simple_lock_init(&zone->lock, ETAP_MISC_ZONE); \
246 #define lock_try_zone(zone) simple_lock_try(&zone->lock)
248 kern_return_t
zget_space(
250 vm_offset_t
*result
);
252 decl_simple_lock_data(,zget_space_lock
)
253 vm_offset_t zalloc_next_space
;
254 vm_offset_t zalloc_end_of_space
;
255 vm_size_t zalloc_wasted_space
;
258 * Garbage collection map information
260 decl_simple_lock_data(, zone_page_table_lock
)
261 struct zone_page_table_entry
* zone_page_table
;
262 vm_offset_t zone_map_min_address
;
263 vm_offset_t zone_map_max_address
;
264 integer_t zone_pages
;
267 * Exclude more than one concurrent garbage collection
269 decl_mutex_data(, zone_gc_lock
)
271 #define from_zone_map(addr) \
272 ((vm_offset_t)(addr) >= zone_map_min_address && \
273 (vm_offset_t)(addr) < zone_map_max_address)
275 #define ZONE_PAGE_USED 0
276 #define ZONE_PAGE_UNUSED -1
280 * Protects first_zone, last_zone, num_zones,
281 * and the next_zone field of zones.
283 decl_simple_lock_data(, all_zones_lock
)
288 boolean_t zone_gc_allowed
= TRUE
;
289 boolean_t zone_gc_forced
= FALSE
;
290 unsigned zone_gc_last_tick
= 0;
291 unsigned zone_gc_max_rate
= 0; /* in ticks */
295 * zinit initializes a new zone. The zone data structures themselves
296 * are stored in a zone, which is initially a static structure that
297 * is initialized by zone_init.
301 vm_size_t size
, /* the size of an element */
302 vm_size_t max
, /* maximum memory to use */
303 vm_size_t alloc
, /* allocation size */
304 char *name
) /* a name for the zone */
308 if (zone_zone
== ZONE_NULL
) {
309 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
313 z
= (zone_t
) zalloc(zone_zone
);
318 * Round off all the parameters appropriately.
320 if (size
< sizeof(z
->free_elements
))
321 size
= sizeof(z
->free_elements
);
322 size
= ((size
-1) + sizeof(z
->free_elements
)) -
323 ((size
-1) % sizeof(z
->free_elements
));
326 alloc
= round_page(alloc
);
327 max
= round_page(max
);
329 * We look for an allocation size with least fragmentation
330 * in the range of 1 - 5 pages. This size will be used unless
331 * the user suggestion is larger AND has less fragmentation
333 { vm_size_t best
, waste
; unsigned int i
;
336 for (i
= 2; i
<= 5; i
++){ vm_size_t tsize
, twaste
;
337 tsize
= i
* PAGE_SIZE
;
338 twaste
= tsize
% size
;
340 best
= tsize
, waste
= twaste
;
342 if (alloc
<= best
|| (alloc
% size
>= waste
))
345 if (max
&& (max
< alloc
))
348 z
->free_elements
= 0;
352 z
->alloc_size
= alloc
;
355 z
->doing_alloc
= FALSE
;
356 z
->exhaustible
= FALSE
;
357 z
->collectable
= TRUE
;
358 z
->allows_foreign
= FALSE
;
359 z
->expandable
= TRUE
;
361 z
->async_pending
= FALSE
;
364 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
365 zone_debug_enable(z
);
366 #endif /* ZONE_DEBUG */
370 * Add the zone to the all-zones list.
373 z
->next_zone
= ZONE_NULL
;
374 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
375 simple_lock(&all_zones_lock
);
377 last_zone
= &z
->next_zone
;
379 simple_unlock(&all_zones_lock
);
385 * Cram the given memory into the specified zone.
389 register zone_t zone
,
393 register vm_size_t elem_size
;
395 /* Basic sanity checks */
396 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
397 assert(!zone
->collectable
|| zone
->allows_foreign
398 || (from_zone_map(newmem
) && from_zone_map(newmem
+size
-1)));
400 elem_size
= zone
->elem_size
;
403 while (size
>= elem_size
) {
404 ADD_TO_ZONE(zone
, newmem
);
405 if (from_zone_map(newmem
))
406 zone_page_alloc(newmem
, elem_size
);
407 zone
->count
++; /* compensate for ADD_TO_ZONE */
410 zone
->cur_size
+= elem_size
;
416 * Contiguous space allocator for non-paged zones. Allocates "size" amount
417 * of memory from zone_map.
425 vm_offset_t new_space
= 0;
426 vm_size_t space_to_add
;
428 simple_lock(&zget_space_lock
);
429 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
431 * Add at least one page to allocation area.
434 space_to_add
= round_page(size
);
436 if (new_space
== 0) {
437 kern_return_t retval
;
439 * Memory cannot be wired down while holding
440 * any locks that the pageout daemon might
441 * need to free up pages. [Making the zget_space
442 * lock a complex lock does not help in this
445 * Unlock and allocate memory. Because several
446 * threads might try to do this at once, don't
447 * use the memory before checking for available
451 simple_unlock(&zget_space_lock
);
453 retval
= kernel_memory_allocate(zone_map
, &new_space
,
454 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
455 if (retval
!= KERN_SUCCESS
)
457 zone_page_init(new_space
, space_to_add
,
459 simple_lock(&zget_space_lock
);
465 * Memory was allocated in a previous iteration.
467 * Check whether the new region is contiguous
471 if (new_space
!= zalloc_end_of_space
) {
473 * Throw away the remainder of the
474 * old space, and start a new one.
476 zalloc_wasted_space
+=
477 zalloc_end_of_space
- zalloc_next_space
;
478 zalloc_next_space
= new_space
;
481 zalloc_end_of_space
= new_space
+ space_to_add
;
485 *result
= zalloc_next_space
;
486 zalloc_next_space
+= size
;
487 simple_unlock(&zget_space_lock
);
490 kmem_free(zone_map
, new_space
, space_to_add
);
492 return(KERN_SUCCESS
);
497 * Steal memory for the zone package. Called from
498 * vm_page_bootstrap().
501 zone_steal_memory(void)
503 zdata_size
= round_page(128*sizeof(struct zone
));
504 zdata
= pmap_steal_memory(zdata_size
);
509 * Fill a zone with enough memory to contain at least nelem elements.
510 * Memory is obtained with kmem_alloc_wired from the kernel_map.
511 * Return the number of elements actually put into the zone, which may
512 * be more than the caller asked for since the memory allocation is
513 * rounded up to a full page.
528 size
= nelem
* zone
->elem_size
;
529 size
= round_page(size
);
530 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
531 if (kr
!= KERN_SUCCESS
)
534 zone_change(zone
, Z_FOREIGN
, TRUE
);
535 zcram(zone
, memory
, size
);
536 nalloc
= size
/ zone
->elem_size
;
537 assert(nalloc
>= nelem
);
543 * Initialize the "zone of zones" which uses fixed memory allocated
544 * earlier in memory initialization. zone_bootstrap is called
550 vm_size_t zone_zone_size
;
551 vm_offset_t zone_zone_space
;
553 simple_lock_init(&all_zones_lock
, ETAP_MISC_ZONE_ALL
);
555 first_zone
= ZONE_NULL
;
556 last_zone
= &first_zone
;
559 simple_lock_init(&zget_space_lock
, ETAP_MISC_ZONE_GET
);
560 zalloc_next_space
= zdata
;
561 zalloc_end_of_space
= zdata
+ zdata_size
;
562 zalloc_wasted_space
= 0;
564 /* assertion: nobody else called zinit before us */
565 assert(zone_zone
== ZONE_NULL
);
566 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
567 sizeof(struct zone
), "zones");
568 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
569 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
570 zget_space(zone_zone_size
, &zone_zone_space
);
571 zcram(zone_zone
, zone_zone_space
, zone_zone_size
);
576 vm_size_t max_zonemap_size
)
578 kern_return_t retval
;
579 vm_offset_t zone_min
;
580 vm_offset_t zone_max
;
581 vm_size_t zone_table_size
;
583 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
584 FALSE
, TRUE
, &zone_map
);
585 if (retval
!= KERN_SUCCESS
)
586 panic("zone_init: kmem_suballoc failed");
587 zone_max
= zone_min
+ round_page(max_zonemap_size
);
589 * Setup garbage collection information:
591 zone_table_size
= atop(zone_max
- zone_min
) *
592 sizeof(struct zone_page_table_entry
);
593 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
594 zone_table_size
) != KERN_SUCCESS
)
596 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
597 zone_pages
= atop(zone_max
- zone_min
);
598 zone_map_min_address
= zone_min
;
599 zone_map_max_address
= zone_max
;
600 simple_lock_init(&zone_page_table_lock
, ETAP_MISC_ZONE_PTABLE
);
601 mutex_init(&zone_gc_lock
, ETAP_NO_TRACE
);
602 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
607 * zalloc returns an element from the specified zone.
611 register zone_t zone
,
615 kern_return_t retval
;
617 assert(zone
!= ZONE_NULL
);
618 check_simple_locks();
622 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
624 while ((addr
== 0) && canblock
) {
626 * If nothing was there, try to get more
628 if (zone
->doing_alloc
) {
630 * Someone is allocating memory for this zone.
631 * Wait for it to show up, then try again.
633 zone
->waiting
= TRUE
;
637 if ((zone
->cur_size
+ zone
->elem_size
) >
639 if (zone
->exhaustible
)
641 if (zone
->expandable
) {
643 * We're willing to overflow certain
644 * zones, but not without complaining.
646 * This is best used in conjunction
647 * with the collectable flag. What we
648 * want is an assurance we can get the
649 * memory back, assuming there's no
652 zone
->max_size
+= (zone
->max_size
>> 1);
656 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
659 zone
->doing_alloc
= TRUE
;
662 if (zone
->collectable
) {
664 vm_size_t alloc_size
;
668 round_page(zone
->elem_size
);
670 alloc_size
= zone
->alloc_size
;
672 retval
= kernel_memory_allocate(zone_map
,
673 &space
, alloc_size
, 0,
674 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
675 if (retval
== KERN_SUCCESS
) {
676 zone_page_init(space
, alloc_size
,
678 zcram(zone
, space
, alloc_size
);
679 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
680 /* would like to cause a zone_gc() */
685 zone
->doing_alloc
= FALSE
;
687 zone
->waiting
= FALSE
;
690 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
692 retval
== KERN_RESOURCE_SHORTAGE
) {
700 retval
= zget_space(zone
->elem_size
, &space
);
703 zone
->doing_alloc
= FALSE
;
705 zone
->waiting
= FALSE
;
706 thread_wakeup((event_t
)zone
);
708 if (retval
== KERN_SUCCESS
) {
710 zone
->cur_size
+= zone
->elem_size
;
712 if (zone_debug_enabled(zone
)) {
713 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
717 zone_page_alloc(space
, zone
->elem_size
);
719 if (zone_debug_enabled(zone
))
720 space
+= sizeof(queue_chain_t
);
724 if (retval
== KERN_RESOURCE_SHORTAGE
) {
735 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
738 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
739 zone
->async_pending
= TRUE
;
741 thread_call_enter(&zone
->call_async_alloc
);
743 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
747 if (addr
&& zone_debug_enabled(zone
)) {
748 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
749 addr
+= sizeof(queue_chain_t
);
761 register zone_t zone
)
763 return( zalloc_canblock(zone
, TRUE
) );
768 register zone_t zone
)
770 return( zalloc_canblock(zone
, FALSE
) );
775 thread_call_param_t p0
,
776 thread_call_param_t p1
)
780 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
781 zfree((zone_t
)p0
, elt
);
782 lock_zone(((zone_t
)p0
));
783 ((zone_t
)p0
)->async_pending
= FALSE
;
784 unlock_zone(((zone_t
)p0
));
789 * zget returns an element from the specified zone
790 * and immediately returns nothing if there is nothing there.
792 * This form should be used when you can not block (like when
793 * processing an interrupt).
797 register zone_t zone
)
799 register vm_offset_t addr
;
801 assert( zone
!= ZONE_NULL
);
803 if (!lock_try_zone(zone
))
804 return ((vm_offset_t
)0);
806 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
808 if (addr
&& zone_debug_enabled(zone
)) {
809 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
810 addr
+= sizeof(queue_chain_t
);
812 #endif /* ZONE_DEBUG */
818 /* Keep this FALSE by default. Large memory machine run orders of magnitude
819 slower in debug mode when true. Use debugger to enable if needed */
820 boolean_t zone_check
= FALSE
;
824 register zone_t zone
,
829 /* Basic sanity checks */
830 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
831 panic("zfree: NULL");
832 /* zone_gc assumes zones are never freed */
833 if (zone
== zone_zone
)
834 panic("zfree: freeing to zone_zone breaks zone_gc!");
835 if (zone
->collectable
&& !zone
->allows_foreign
&&
836 (!from_zone_map(elem
) || !from_zone_map(elem
+zone
->elem_size
-1)))
837 panic("zfree: non-allocated memory in collectable zone!");
842 if (zone_debug_enabled(zone
)) {
845 elem
-= sizeof(queue_chain_t
);
847 /* check the zone's consistency */
849 for (tmp_elem
= queue_first(&zone
->active_zones
);
850 !queue_end(tmp_elem
, &zone
->active_zones
);
851 tmp_elem
= queue_next(tmp_elem
))
852 if (elem
== (vm_offset_t
)tmp_elem
)
854 if (elem
!= (vm_offset_t
)tmp_elem
)
855 panic("zfree()ing element from wrong zone");
857 remqueue(&zone
->active_zones
, (queue_t
) elem
);
859 #endif /* ZONE_DEBUG */
863 /* check the zone's consistency */
865 for (this = zone
->free_elements
;
867 this = * (vm_offset_t
*) this)
868 if (!pmap_kernel_va(this) || this == elem
)
871 ADD_TO_ZONE(zone
, elem
);
874 * If elements have one or more pages, and memory is low,
875 * request to run the garbage collection in the zone the next
876 * time the pageout thread runs.
878 if (zone
->elem_size
>= PAGE_SIZE
&&
880 zone_gc_forced
= TRUE
;
886 /* Change a zone's flags.
887 * This routine must be called immediately after zinit.
895 assert( zone
!= ZONE_NULL
);
896 assert( value
== TRUE
|| value
== FALSE
);
900 zone
->exhaustible
= value
;
903 zone
->collectable
= value
;
906 zone
->expandable
= value
;
909 zone
->allows_foreign
= value
;
913 panic("Zone_change: Wrong Item Type!");
917 lock_zone_init(zone
);
921 * Return the expected number of free elements in the zone.
922 * This calculation will be incorrect if items are zfree'd that
923 * were never zalloc'd/zget'd. The correct way to stuff memory
924 * into a zone is by zcram.
928 zone_free_count(zone_t zone
)
930 integer_t free_count
;
933 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
936 assert(free_count
>= 0);
942 * zprealloc preallocates wired memory, exanding the specified
943 * zone to the specified size
953 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
955 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
956 zcram(zone
, addr
, size
);
961 * Zone garbage collection subroutines
963 * These routines have in common the modification of entries in the
964 * zone_page_table. The latter contains one entry for every page
967 * For each page table entry in the given range:
969 * zone_page_collectable - test if one (in_free_list == alloc_count)
970 * zone_page_keep - reset in_free_list
971 * zone_page_in_use - decrements in_free_list
972 * zone_page_free - increments in_free_list
973 * zone_page_init - initializes in_free_list and alloc_count
974 * zone_page_alloc - increments alloc_count
975 * zone_page_dealloc - decrements alloc_count
976 * zone_add_free_page_list - adds the page to the free list
978 * Two counts are maintained for each page, the in_free_list count and
979 * alloc_count. The alloc_count is how many zone elements have been
980 * allocated from a page. (Note that the page could contain elements
981 * that span page boundaries. The count includes these elements so
982 * one element may be counted in two pages.) In_free_list is a count
983 * of how many zone elements are currently free. If in_free_list is
984 * equal to alloc_count then the page is eligible for garbage
987 * Alloc_count and in_free_list are initialized to the correct values
988 * for a particular zone when a page is zcram'ed into a zone. Subsequent
989 * gets and frees of zone elements will call zone_page_in_use and
990 * zone_page_free which modify the in_free_list count. When the zones
991 * garbage collector runs it will walk through a zones free element list,
992 * remove the elements that reside on collectable pages, and use
993 * zone_add_free_page_list to create a list of pages to be collected.
996 zone_page_collectable(
1003 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1004 panic("zone_page_collectable");
1007 i
= atop(addr
-zone_map_min_address
);
1008 j
= atop((addr
+size
-1) - zone_map_min_address
);
1009 lock_zone_page_table();
1010 for (; i
<= j
; i
++) {
1011 if (zone_page_table
[i
].in_free_list
==
1012 zone_page_table
[i
].alloc_count
) {
1013 unlock_zone_page_table();
1017 unlock_zone_page_table();
1029 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1030 panic("zone_page_keep");
1033 i
= atop(addr
-zone_map_min_address
);
1034 j
= atop((addr
+size
-1) - zone_map_min_address
);
1035 lock_zone_page_table();
1036 for (; i
<= j
; i
++) {
1037 zone_page_table
[i
].in_free_list
= 0;
1039 unlock_zone_page_table();
1050 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1051 panic("zone_page_in_use");
1054 i
= atop(addr
-zone_map_min_address
);
1055 j
= atop((addr
+size
-1) - zone_map_min_address
);
1056 lock_zone_page_table();
1057 for (; i
<= j
; i
++) {
1058 if (zone_page_table
[i
].in_free_list
> 0)
1059 zone_page_table
[i
].in_free_list
--;
1061 unlock_zone_page_table();
1072 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1073 panic("zone_page_free");
1076 i
= atop(addr
-zone_map_min_address
);
1077 j
= atop((addr
+size
-1) - zone_map_min_address
);
1078 lock_zone_page_table();
1079 for (; i
<= j
; i
++) {
1080 assert(zone_page_table
[i
].in_free_list
>= 0);
1081 zone_page_table
[i
].in_free_list
++;
1083 unlock_zone_page_table();
1095 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1096 panic("zone_page_init");
1099 i
= atop(addr
-zone_map_min_address
);
1100 j
= atop((addr
+size
-1) - zone_map_min_address
);
1101 lock_zone_page_table();
1102 for (; i
<= j
; i
++) {
1103 zone_page_table
[i
].alloc_count
= value
;
1104 zone_page_table
[i
].in_free_list
= 0;
1106 unlock_zone_page_table();
1117 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1118 panic("zone_page_alloc");
1121 i
= atop(addr
-zone_map_min_address
);
1122 j
= atop((addr
+size
-1) - zone_map_min_address
);
1123 lock_zone_page_table();
1124 for (; i
<= j
; i
++) {
1125 /* Set alloc_count to (ZONE_PAGE_USED + 1) if
1126 * it was previously set to ZONE_PAGE_UNUSED.
1128 if (zone_page_table
[i
].alloc_count
== ZONE_PAGE_UNUSED
) {
1129 zone_page_table
[i
].alloc_count
= 1;
1131 zone_page_table
[i
].alloc_count
++;
1134 unlock_zone_page_table();
1145 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1146 panic("zone_page_dealloc");
1149 i
= atop(addr
-zone_map_min_address
);
1150 j
= atop((addr
+size
-1) - zone_map_min_address
);
1151 lock_zone_page_table();
1152 for (; i
<= j
; i
++) {
1153 zone_page_table
[i
].alloc_count
--;
1155 unlock_zone_page_table();
1159 zone_add_free_page_list(
1160 struct zone_page_table_entry
**free_list
,
1167 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1168 panic("zone_add_free_page_list");
1171 i
= atop(addr
-zone_map_min_address
);
1172 j
= atop((addr
+size
-1) - zone_map_min_address
);
1173 lock_zone_page_table();
1174 for (; i
<= j
; i
++) {
1175 if (zone_page_table
[i
].alloc_count
== 0) {
1176 zone_page_table
[i
].next
= *free_list
;
1177 *free_list
= &zone_page_table
[i
];
1178 zone_page_table
[i
].alloc_count
= ZONE_PAGE_UNUSED
;
1179 zone_page_table
[i
].in_free_list
= 0;
1182 unlock_zone_page_table();
1186 /* This is used for walking through a zone's free element list.
1188 struct zone_free_entry
{
1189 struct zone_free_entry
* next
;
1192 int reclaim_page_count
= 0;
1194 /* Zone garbage collection
1196 * zone_gc will walk through all the free elements in all the
1197 * zones that are marked collectable looking for reclaimable
1198 * pages. zone_gc is called by consider_zone_gc when the system
1199 * begins to run out of memory.
1204 unsigned int max_zones
;
1207 struct zone_page_table_entry
*freep
;
1208 struct zone_page_table_entry
*zone_free_page_list
;
1210 mutex_lock(&zone_gc_lock
);
1213 * Note that this scheme of locking only to walk the zone list
1214 * assumes that zones are never freed (checked by zfree)
1216 simple_lock(&all_zones_lock
);
1217 max_zones
= num_zones
;
1219 simple_unlock(&all_zones_lock
);
1222 lock_zone_page_table();
1223 for (i
= 0; i
< zone_pages
; i
++)
1224 assert(zone_page_table
[i
].in_free_list
== 0);
1225 unlock_zone_page_table();
1226 #endif /* MACH_ASSERT */
1228 zone_free_page_list
= (struct zone_page_table_entry
*) 0;
1230 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1231 struct zone_free_entry
* prev
;
1232 struct zone_free_entry
* elt
;
1233 struct zone_free_entry
* end
;
1235 assert(z
!= ZONE_NULL
);
1237 if (!z
->collectable
)
1243 * Do a quick feasability check before we scan the zone:
1244 * skip unless there is likelihood of getting 1+ pages back.
1246 if ((z
->cur_size
- z
->count
* z
->elem_size
) <= (2*PAGE_SIZE
)){
1251 /* Count the free elements in each page. This loop
1252 * requires that all in_free_list entries are zero.
1254 * Exit the loop early if we need to hurry up and drop
1255 * the lock to allow preemption - but we must fully process
1256 * all elements we looked at so far.
1258 elt
= (struct zone_free_entry
*)(z
->free_elements
);
1259 while (!ast_urgency() && (elt
!= (struct zone_free_entry
*)0)) {
1260 if (from_zone_map(elt
))
1261 zone_page_free((vm_offset_t
)elt
, z
->elem_size
);
1266 /* Now determine which elements should be removed
1267 * from the free list and, after all the elements
1268 * on a page have been removed, add the element's
1269 * page to a list of pages to be freed.
1271 prev
= elt
= (struct zone_free_entry
*)(z
->free_elements
);
1272 while (elt
!= end
) {
1273 if (!from_zone_map(elt
)) {
1278 if (zone_page_collectable((vm_offset_t
)elt
,
1280 z
->cur_size
-= z
->elem_size
;
1281 zone_page_in_use((vm_offset_t
)elt
,
1283 zone_page_dealloc((vm_offset_t
)elt
,
1285 zone_add_free_page_list(&zone_free_page_list
,
1290 z
->free_elements
=(vm_offset_t
)elt
;
1293 prev
->next
= elt
->next
;
1297 /* This element is not eligible for collection
1298 * so clear in_free_list in preparation for a
1299 * subsequent garbage collection pass.
1301 zone_page_keep((vm_offset_t
)elt
, z
->elem_size
);
1305 } /* end while(elt != end) */
1310 for (freep
= zone_free_page_list
; freep
!= 0; freep
= freep
->next
) {
1311 vm_offset_t free_addr
;
1313 free_addr
= zone_map_min_address
+
1314 PAGE_SIZE
* (freep
- zone_page_table
);
1315 kmem_free(zone_map
, free_addr
, PAGE_SIZE
);
1316 reclaim_page_count
++;
1318 mutex_unlock(&zone_gc_lock
);
1324 * Called by the pageout daemon when the system needs more free pages.
1328 consider_zone_gc(void)
1331 * By default, don't attempt zone GC more frequently
1332 * than once a second.
1335 if (zone_gc_max_rate
== 0)
1336 zone_gc_max_rate
= (1 << SCHED_TICK_SHIFT
) + 1;
1338 if (zone_gc_allowed
&&
1339 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1341 zone_gc_forced
= FALSE
;
1342 zone_gc_last_tick
= sched_tick
;
1347 #include <mach/kern_return.h>
1348 #include <mach/machine/vm_types.h>
1349 #include <mach_debug/zone_info.h>
1350 #include <kern/host.h>
1351 #include <vm/vm_map.h>
1352 #include <vm/vm_kern.h>
1354 #include <mach/mach_host_server.h>
1359 zone_name_array_t
*namesp
,
1360 mach_msg_type_number_t
*namesCntp
,
1361 zone_info_array_t
*infop
,
1362 mach_msg_type_number_t
*infoCntp
)
1365 vm_offset_t names_addr
;
1366 vm_size_t names_size
;
1368 vm_offset_t info_addr
;
1369 vm_size_t info_size
;
1370 unsigned int max_zones
, i
;
1376 if (host
== HOST_NULL
)
1377 return KERN_INVALID_HOST
;
1380 * We assume that zones aren't freed once allocated.
1381 * We won't pick up any zones that are allocated later.
1384 simple_lock(&all_zones_lock
);
1386 max_zones
= num_zones
+ 4;
1388 max_zones
= num_zones
+ 2;
1391 simple_unlock(&all_zones_lock
);
1393 if (max_zones
<= *namesCntp
) {
1394 /* use in-line memory */
1398 names_size
= round_page(max_zones
* sizeof *names
);
1399 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1400 &names_addr
, names_size
);
1401 if (kr
!= KERN_SUCCESS
)
1403 names
= (zone_name_t
*) names_addr
;
1406 if (max_zones
<= *infoCntp
) {
1407 /* use in-line memory */
1411 info_size
= round_page(max_zones
* sizeof *info
);
1412 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1413 &info_addr
, info_size
);
1414 if (kr
!= KERN_SUCCESS
) {
1415 if (names
!= *namesp
)
1416 kmem_free(ipc_kernel_map
,
1417 names_addr
, names_size
);
1421 info
= (zone_info_t
*) info_addr
;
1426 for (i
= 0; i
< num_zones
; i
++) {
1429 assert(z
!= ZONE_NULL
);
1435 simple_lock(&all_zones_lock
);
1437 simple_unlock(&all_zones_lock
);
1439 /* assuming here the name data is static */
1440 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1441 sizeof zn
->zn_name
);
1443 zi
->zi_count
= zcopy
.count
;
1444 zi
->zi_cur_size
= zcopy
.cur_size
;
1445 zi
->zi_max_size
= zcopy
.max_size
;
1446 zi
->zi_elem_size
= zcopy
.elem_size
;
1447 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1448 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1449 zi
->zi_collectable
= zcopy
.collectable
;
1454 strcpy(zn
->zn_name
, "kernel_stacks");
1455 stack_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1456 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1460 strcpy(zn
->zn_name
, "save_areas");
1461 save_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1462 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1466 strcpy(zn
->zn_name
, "pmap_mappings");
1467 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1468 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1472 strcpy(zn
->zn_name
, "kalloc.large");
1473 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1474 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1476 if (names
!= *namesp
) {
1480 used
= max_zones
* sizeof *names
;
1482 if (used
!= names_size
)
1483 bzero((char *) (names_addr
+ used
), names_size
- used
);
1485 kr
= vm_map_copyin(ipc_kernel_map
, names_addr
, names_size
,
1487 assert(kr
== KERN_SUCCESS
);
1489 *namesp
= (zone_name_t
*) copy
;
1491 *namesCntp
= max_zones
;
1493 if (info
!= *infop
) {
1497 used
= max_zones
* sizeof *info
;
1499 if (used
!= info_size
)
1500 bzero((char *) (info_addr
+ used
), info_size
- used
);
1502 kr
= vm_map_copyin(ipc_kernel_map
, info_addr
, info_size
,
1504 assert(kr
== KERN_SUCCESS
);
1506 *infop
= (zone_info_t
*) copy
;
1508 *infoCntp
= max_zones
;
1510 return KERN_SUCCESS
;
1514 #include <ddb/db_command.h>
1515 #include <ddb/db_output.h>
1516 #include <kern/kern_print.h>
1518 const char *zone_labels
=
1519 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1526 void db_zone_check_active(
1528 void db_zone_print_active(
1530 #endif /* ZONE_DEBUG */
1531 void db_zone_print_free(
1541 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1542 addr
, zcopy
.count
, zcopy
.cur_size
,
1543 zcopy
.max_size
, zcopy
.elem_size
,
1544 zcopy
.alloc_size
, zcopy
.zone_name
);
1545 if (zcopy
.exhaustible
)
1547 if (zcopy
.collectable
)
1549 if (zcopy
.expandable
)
1562 struct zone
*z
= (zone_t
)addr
;
1564 if (z
== ZONE_NULL
|| !have_addr
){
1565 db_error("No Zone\n");
1569 db_printf("%s\n", zone_labels
);
1585 * Don't risk hanging by unconditionally locking,
1586 * risk of incoherent data is small (zones aren't freed).
1588 have_addr
= simple_lock_try(&all_zones_lock
);
1592 simple_unlock(&all_zones_lock
);
1595 db_printf("%s\n", zone_labels
);
1596 for ( ; count
> 0; count
--) {
1598 db_error("Mangled Zone List\n");
1602 total
+= z
->cur_size
,
1604 have_addr
= simple_lock_try(&all_zones_lock
);
1607 simple_unlock(&all_zones_lock
);
1610 db_printf("\nTotal %8x", total
);
1611 db_printf("\n\nzone_gc() has reclaimed %d pages\n",
1612 reclaim_page_count
);
1617 db_zone_check_active(
1623 if (!zone_debug_enabled(zone
) || !zone_check
)
1625 tmp_elem
= queue_first(&zone
->active_zones
);
1626 while (count
< zone
->count
) {
1628 if (tmp_elem
== 0) {
1629 printf("unexpected zero element, zone=0x%x, count=%d\n",
1634 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1635 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1640 tmp_elem
= queue_next(tmp_elem
);
1642 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1643 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1650 db_zone_print_active(
1656 if (!zone_debug_enabled(zone
)) {
1657 printf("zone 0x%x debug not enabled\n", zone
);
1661 printf("zone_check FALSE\n");
1665 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1666 printf("active list:\n");
1667 tmp_elem
= queue_first(&zone
->active_zones
);
1668 while (count
< zone
->count
) {
1669 printf(" 0x%x", tmp_elem
);
1671 if ((count
% 6) == 0)
1673 if (tmp_elem
== 0) {
1674 printf("\nunexpected zero element, count=%d\n", count
);
1677 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1678 printf("\nunexpected queue_end, count=%d\n", count
);
1681 tmp_elem
= queue_next(tmp_elem
);
1683 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1684 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1688 #endif /* ZONE_DEBUG */
1698 freecount
= zone_free_count(zone
);
1699 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1700 printf("free list:\n");
1701 elem
= zone
->free_elements
;
1702 while (count
< freecount
) {
1703 printf(" 0x%x", elem
);
1705 if ((count
% 6) == 0)
1708 printf("\nunexpected zero element, count=%d\n", count
);
1711 elem
= *((vm_offset_t
*)elem
);
1714 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1719 #endif /* MACH_KDB */
1724 /* should we care about locks here ? */
1732 if (!zone_debug_enabled(z
))
1734 elt
-= sizeof(queue_chain_t
);
1735 elt
= (vm_offset_t
) queue_next((queue_t
) elt
);
1736 if ((queue_t
) elt
== &z
->active_zones
)
1738 elt
+= sizeof(queue_chain_t
);
1748 if (!zone_debug_enabled(z
))
1750 if (queue_empty(&z
->active_zones
))
1752 elt
= (vm_offset_t
) queue_first(&z
->active_zones
);
1753 elt
+= sizeof(queue_chain_t
);
1758 * Second arg controls how many zone elements are printed:
1761 * n, n > 0 => last n on active list
1770 boolean_t print
= (tail
!= 0);
1774 if (z
->count
< tail
)
1776 tail
= z
->count
- tail
;
1777 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1778 if (print
&& tail
<= count
)
1779 db_printf("%8x\n", elt
);
1782 assert(count
== z
->count
);
1785 #endif /* MACH_KDB */
1787 #define zone_in_use(z) ( z->count || z->free_elements )
1793 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1794 z
->alloc_size
< (z
->elem_size
+ sizeof(queue_chain_t
)))
1796 queue_init(&z
->active_zones
);
1797 z
->elem_size
+= sizeof(queue_chain_t
);
1804 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1806 z
->elem_size
-= sizeof(queue_chain_t
);
1807 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1809 #endif /* ZONE_DEBUG */