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 #endif /* ZONE_DEBUG */
154 * Support for garbage collection of unused zone pages:
157 struct zone_page_table_entry
{
158 struct zone_page_table_entry
*next
;
163 extern struct zone_page_table_entry
* zone_page_table
;
165 #define lock_zone_page_table() simple_lock(&zone_page_table_lock)
166 #define unlock_zone_page_table() simple_unlock(&zone_page_table_lock)
168 #define zone_page(addr) \
169 (&(zone_page_table[(atop(((vm_offset_t)addr) - zone_map_min_address))]))
177 void zone_page_alloc(
181 void zone_add_free_page_list(
182 struct zone_page_table_entry
**free_list
,
185 void zone_page_dealloc(
189 void zone_page_in_use(
197 boolean_t
zone_page_collectable(
206 thread_call_param_t p0
,
207 thread_call_param_t p1
);
210 #if ZONE_DEBUG && MACH_KDB
214 #endif /* ZONE_DEBUG && MACH_KDB */
216 vm_map_t zone_map
= VM_MAP_NULL
;
218 zone_t zone_zone
= ZONE_NULL
; /* the zone containing other zones */
221 * The VM system gives us an initial chunk of memory.
222 * It has to be big enough to allocate the zone_zone
226 vm_size_t zdata_size
;
228 #define lock_zone(zone) \
230 simple_lock(&(zone)->lock); \
233 #define unlock_zone(zone) \
235 simple_unlock(&(zone)->lock); \
238 #define zone_wakeup(zone) thread_wakeup((event_t)(zone))
239 #define zone_sleep(zone) \
240 thread_sleep_simple_lock((event_t)(zone), \
244 #define lock_zone_init(zone) \
246 simple_lock_init(&zone->lock, ETAP_MISC_ZONE); \
249 #define lock_try_zone(zone) simple_lock_try(&zone->lock)
251 kern_return_t
zget_space(
253 vm_offset_t
*result
);
255 decl_simple_lock_data(,zget_space_lock
)
256 vm_offset_t zalloc_next_space
;
257 vm_offset_t zalloc_end_of_space
;
258 vm_size_t zalloc_wasted_space
;
261 * Garbage collection map information
263 decl_simple_lock_data(, zone_page_table_lock
)
264 struct zone_page_table_entry
* zone_page_table
;
265 vm_offset_t zone_map_min_address
;
266 vm_offset_t zone_map_max_address
;
267 integer_t zone_pages
;
270 * Exclude more than one concurrent garbage collection
272 decl_mutex_data(, zone_gc_lock
)
274 #define from_zone_map(addr) \
275 ((vm_offset_t)(addr) >= zone_map_min_address && \
276 (vm_offset_t)(addr) < zone_map_max_address)
278 #define ZONE_PAGE_USED 0
279 #define ZONE_PAGE_UNUSED -1
283 * Protects first_zone, last_zone, num_zones,
284 * and the next_zone field of zones.
286 decl_simple_lock_data(, all_zones_lock
)
291 boolean_t zone_gc_allowed
= TRUE
;
292 boolean_t zone_gc_forced
= FALSE
;
293 unsigned zone_gc_last_tick
= 0;
294 unsigned zone_gc_max_rate
= 0; /* in ticks */
298 * zinit initializes a new zone. The zone data structures themselves
299 * are stored in a zone, which is initially a static structure that
300 * is initialized by zone_init.
304 vm_size_t size
, /* the size of an element */
305 vm_size_t max
, /* maximum memory to use */
306 vm_size_t alloc
, /* allocation size */
307 char *name
) /* a name for the zone */
311 if (zone_zone
== ZONE_NULL
) {
312 if (zget_space(sizeof(struct zone
), (vm_offset_t
*)&z
)
316 z
= (zone_t
) zalloc(zone_zone
);
321 * Round off all the parameters appropriately.
323 if (size
< sizeof(z
->free_elements
))
324 size
= sizeof(z
->free_elements
);
325 size
= ((size
-1) + sizeof(z
->free_elements
)) -
326 ((size
-1) % sizeof(z
->free_elements
));
329 alloc
= round_page(alloc
);
330 max
= round_page(max
);
332 * We look for an allocation size with least fragmentation
333 * in the range of 1 - 5 pages. This size will be used unless
334 * the user suggestion is larger AND has less fragmentation
336 { vm_size_t best
, waste
; unsigned int i
;
339 for (i
= 2; i
<= 5; i
++){ vm_size_t tsize
, twaste
;
340 tsize
= i
* PAGE_SIZE
;
341 twaste
= tsize
% size
;
343 best
= tsize
, waste
= twaste
;
345 if (alloc
<= best
|| (alloc
% size
>= waste
))
348 if (max
&& (max
< alloc
))
351 z
->free_elements
= 0;
355 z
->alloc_size
= alloc
;
358 z
->doing_alloc
= FALSE
;
359 z
->exhaustible
= FALSE
;
360 z
->collectable
= TRUE
;
361 z
->allows_foreign
= FALSE
;
362 z
->expandable
= TRUE
;
364 z
->async_pending
= FALSE
;
367 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
368 zone_debug_enable(z
);
369 #endif /* ZONE_DEBUG */
373 * Add the zone to the all-zones list.
376 z
->next_zone
= ZONE_NULL
;
377 thread_call_setup(&z
->call_async_alloc
, zalloc_async
, z
);
378 simple_lock(&all_zones_lock
);
380 last_zone
= &z
->next_zone
;
382 simple_unlock(&all_zones_lock
);
388 * Cram the given memory into the specified zone.
392 register zone_t zone
,
396 register vm_size_t elem_size
;
398 /* Basic sanity checks */
399 assert(zone
!= ZONE_NULL
&& newmem
!= (vm_offset_t
)0);
400 assert(!zone
->collectable
|| zone
->allows_foreign
401 || (from_zone_map(newmem
) && from_zone_map(newmem
+size
-1)));
403 elem_size
= zone
->elem_size
;
406 while (size
>= elem_size
) {
407 ADD_TO_ZONE(zone
, newmem
);
408 if (from_zone_map(newmem
))
409 zone_page_alloc(newmem
, elem_size
);
410 zone
->count
++; /* compensate for ADD_TO_ZONE */
413 zone
->cur_size
+= elem_size
;
419 * Contiguous space allocator for non-paged zones. Allocates "size" amount
420 * of memory from zone_map.
428 vm_offset_t new_space
= 0;
429 vm_size_t space_to_add
;
431 simple_lock(&zget_space_lock
);
432 while ((zalloc_next_space
+ size
) > zalloc_end_of_space
) {
434 * Add at least one page to allocation area.
437 space_to_add
= round_page(size
);
439 if (new_space
== 0) {
440 kern_return_t retval
;
442 * Memory cannot be wired down while holding
443 * any locks that the pageout daemon might
444 * need to free up pages. [Making the zget_space
445 * lock a complex lock does not help in this
448 * Unlock and allocate memory. Because several
449 * threads might try to do this at once, don't
450 * use the memory before checking for available
454 simple_unlock(&zget_space_lock
);
456 retval
= kernel_memory_allocate(zone_map
, &new_space
,
457 space_to_add
, 0, KMA_KOBJECT
|KMA_NOPAGEWAIT
);
458 if (retval
!= KERN_SUCCESS
)
460 zone_page_init(new_space
, space_to_add
,
462 simple_lock(&zget_space_lock
);
468 * Memory was allocated in a previous iteration.
470 * Check whether the new region is contiguous
474 if (new_space
!= zalloc_end_of_space
) {
476 * Throw away the remainder of the
477 * old space, and start a new one.
479 zalloc_wasted_space
+=
480 zalloc_end_of_space
- zalloc_next_space
;
481 zalloc_next_space
= new_space
;
484 zalloc_end_of_space
= new_space
+ space_to_add
;
488 *result
= zalloc_next_space
;
489 zalloc_next_space
+= size
;
490 simple_unlock(&zget_space_lock
);
493 kmem_free(zone_map
, new_space
, space_to_add
);
495 return(KERN_SUCCESS
);
500 * Steal memory for the zone package. Called from
501 * vm_page_bootstrap().
504 zone_steal_memory(void)
506 zdata_size
= round_page(128*sizeof(struct zone
));
507 zdata
= pmap_steal_memory(zdata_size
);
512 * Fill a zone with enough memory to contain at least nelem elements.
513 * Memory is obtained with kmem_alloc_wired from the kernel_map.
514 * Return the number of elements actually put into the zone, which may
515 * be more than the caller asked for since the memory allocation is
516 * rounded up to a full page.
531 size
= nelem
* zone
->elem_size
;
532 size
= round_page(size
);
533 kr
= kmem_alloc_wired(kernel_map
, &memory
, size
);
534 if (kr
!= KERN_SUCCESS
)
537 zone_change(zone
, Z_FOREIGN
, TRUE
);
538 zcram(zone
, memory
, size
);
539 nalloc
= size
/ zone
->elem_size
;
540 assert(nalloc
>= nelem
);
546 * Initialize the "zone of zones" which uses fixed memory allocated
547 * earlier in memory initialization. zone_bootstrap is called
553 vm_size_t zone_zone_size
;
554 vm_offset_t zone_zone_space
;
556 simple_lock_init(&all_zones_lock
, ETAP_MISC_ZONE_ALL
);
558 first_zone
= ZONE_NULL
;
559 last_zone
= &first_zone
;
562 simple_lock_init(&zget_space_lock
, ETAP_MISC_ZONE_GET
);
563 zalloc_next_space
= zdata
;
564 zalloc_end_of_space
= zdata
+ zdata_size
;
565 zalloc_wasted_space
= 0;
567 /* assertion: nobody else called zinit before us */
568 assert(zone_zone
== ZONE_NULL
);
569 zone_zone
= zinit(sizeof(struct zone
), 128 * sizeof(struct zone
),
570 sizeof(struct zone
), "zones");
571 zone_change(zone_zone
, Z_COLLECT
, FALSE
);
572 zone_zone_size
= zalloc_end_of_space
- zalloc_next_space
;
573 zget_space(zone_zone_size
, &zone_zone_space
);
574 zcram(zone_zone
, zone_zone_space
, zone_zone_size
);
579 vm_size_t max_zonemap_size
)
581 kern_return_t retval
;
582 vm_offset_t zone_min
;
583 vm_offset_t zone_max
;
584 vm_size_t zone_table_size
;
586 retval
= kmem_suballoc(kernel_map
, &zone_min
, max_zonemap_size
,
587 FALSE
, TRUE
, &zone_map
);
588 if (retval
!= KERN_SUCCESS
)
589 panic("zone_init: kmem_suballoc failed");
590 zone_max
= zone_min
+ round_page(max_zonemap_size
);
592 * Setup garbage collection information:
594 zone_table_size
= atop(zone_max
- zone_min
) *
595 sizeof(struct zone_page_table_entry
);
596 if (kmem_alloc_wired(zone_map
, (vm_offset_t
*) &zone_page_table
,
597 zone_table_size
) != KERN_SUCCESS
)
599 zone_min
= (vm_offset_t
)zone_page_table
+ round_page(zone_table_size
);
600 zone_pages
= atop(zone_max
- zone_min
);
601 zone_map_min_address
= zone_min
;
602 zone_map_max_address
= zone_max
;
603 simple_lock_init(&zone_page_table_lock
, ETAP_MISC_ZONE_PTABLE
);
604 mutex_init(&zone_gc_lock
, ETAP_NO_TRACE
);
605 zone_page_init(zone_min
, zone_max
- zone_min
, ZONE_PAGE_UNUSED
);
610 * zalloc returns an element from the specified zone.
614 register zone_t zone
,
618 kern_return_t retval
;
620 assert(zone
!= ZONE_NULL
);
621 check_simple_locks();
625 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
627 while ((addr
== 0) && canblock
) {
629 * If nothing was there, try to get more
631 if (zone
->doing_alloc
) {
633 * Someone is allocating memory for this zone.
634 * Wait for it to show up, then try again.
636 zone
->waiting
= TRUE
;
640 if ((zone
->cur_size
+ zone
->elem_size
) >
642 if (zone
->exhaustible
)
644 if (zone
->expandable
) {
646 * We're willing to overflow certain
647 * zones, but not without complaining.
649 * This is best used in conjunction
650 * with the collectable flag. What we
651 * want is an assurance we can get the
652 * memory back, assuming there's no
655 zone
->max_size
+= (zone
->max_size
>> 1);
659 panic("zalloc: zone \"%s\" empty.", zone
->zone_name
);
662 zone
->doing_alloc
= TRUE
;
665 if (zone
->collectable
) {
667 vm_size_t alloc_size
;
671 round_page(zone
->elem_size
);
673 alloc_size
= zone
->alloc_size
;
675 retval
= kernel_memory_allocate(zone_map
,
676 &space
, alloc_size
, 0,
677 KMA_KOBJECT
|KMA_NOPAGEWAIT
);
678 if (retval
== KERN_SUCCESS
) {
679 zone_page_init(space
, alloc_size
,
681 zcram(zone
, space
, alloc_size
);
682 } else if (retval
!= KERN_RESOURCE_SHORTAGE
) {
683 /* would like to cause a zone_gc() */
688 zone
->doing_alloc
= FALSE
;
690 zone
->waiting
= FALSE
;
693 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
695 retval
== KERN_RESOURCE_SHORTAGE
) {
703 retval
= zget_space(zone
->elem_size
, &space
);
706 zone
->doing_alloc
= FALSE
;
708 zone
->waiting
= FALSE
;
709 thread_wakeup((event_t
)zone
);
711 if (retval
== KERN_SUCCESS
) {
713 zone
->cur_size
+= zone
->elem_size
;
715 if (zone_debug_enabled(zone
)) {
716 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)space
);
720 zone_page_alloc(space
, zone
->elem_size
);
722 if (zone_debug_enabled(zone
))
723 space
+= sizeof(queue_chain_t
);
727 if (retval
== KERN_RESOURCE_SHORTAGE
) {
738 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
741 if ((addr
== 0) && !canblock
&& (zone
->async_pending
== FALSE
) && (!vm_pool_low())) {
742 zone
->async_pending
= TRUE
;
744 thread_call_enter(&zone
->call_async_alloc
);
746 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
750 if (addr
&& zone_debug_enabled(zone
)) {
751 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
752 addr
+= sizeof(queue_chain_t
);
764 register zone_t zone
)
766 return( zalloc_canblock(zone
, TRUE
) );
771 register zone_t zone
)
773 return( zalloc_canblock(zone
, FALSE
) );
778 thread_call_param_t p0
,
779 thread_call_param_t p1
)
783 elt
= zalloc_canblock((zone_t
)p0
, TRUE
);
784 zfree((zone_t
)p0
, elt
);
785 lock_zone(((zone_t
)p0
));
786 ((zone_t
)p0
)->async_pending
= FALSE
;
787 unlock_zone(((zone_t
)p0
));
792 * zget returns an element from the specified zone
793 * and immediately returns nothing if there is nothing there.
795 * This form should be used when you can not block (like when
796 * processing an interrupt).
800 register zone_t zone
)
802 register vm_offset_t addr
;
804 assert( zone
!= ZONE_NULL
);
806 if (!lock_try_zone(zone
))
807 return ((vm_offset_t
)0);
809 REMOVE_FROM_ZONE(zone
, addr
, vm_offset_t
);
811 if (addr
&& zone_debug_enabled(zone
)) {
812 enqueue_tail(&zone
->active_zones
, (queue_entry_t
)addr
);
813 addr
+= sizeof(queue_chain_t
);
815 #endif /* ZONE_DEBUG */
821 /* Keep this FALSE by default. Large memory machine run orders of magnitude
822 slower in debug mode when true. Use debugger to enable if needed */
823 boolean_t zone_check
= FALSE
;
827 register zone_t zone
,
832 /* Basic sanity checks */
833 if (zone
== ZONE_NULL
|| elem
== (vm_offset_t
)0)
834 panic("zfree: NULL");
835 /* zone_gc assumes zones are never freed */
836 if (zone
== zone_zone
)
837 panic("zfree: freeing to zone_zone breaks zone_gc!");
838 if (zone
->collectable
&& !zone
->allows_foreign
&&
839 (!from_zone_map(elem
) || !from_zone_map(elem
+zone
->elem_size
-1)))
840 panic("zfree: non-allocated memory in collectable zone!");
845 if (zone_debug_enabled(zone
)) {
848 elem
-= sizeof(queue_chain_t
);
850 /* check the zone's consistency */
852 for (tmp_elem
= queue_first(&zone
->active_zones
);
853 !queue_end(tmp_elem
, &zone
->active_zones
);
854 tmp_elem
= queue_next(tmp_elem
))
855 if (elem
== (vm_offset_t
)tmp_elem
)
857 if (elem
!= (vm_offset_t
)tmp_elem
)
858 panic("zfree()ing element from wrong zone");
860 remqueue(&zone
->active_zones
, (queue_t
) elem
);
862 #endif /* ZONE_DEBUG */
866 /* check the zone's consistency */
868 for (this = zone
->free_elements
;
870 this = * (vm_offset_t
*) this)
871 if (!pmap_kernel_va(this) || this == elem
)
874 ADD_TO_ZONE(zone
, elem
);
877 * If elements have one or more pages, and memory is low,
878 * request to run the garbage collection in the zone the next
879 * time the pageout thread runs.
881 if (zone
->elem_size
>= PAGE_SIZE
&&
883 zone_gc_forced
= TRUE
;
889 /* Change a zone's flags.
890 * This routine must be called immediately after zinit.
898 assert( zone
!= ZONE_NULL
);
899 assert( value
== TRUE
|| value
== FALSE
);
903 zone
->exhaustible
= value
;
906 zone
->collectable
= value
;
909 zone
->expandable
= value
;
912 zone
->allows_foreign
= value
;
916 panic("Zone_change: Wrong Item Type!");
920 lock_zone_init(zone
);
924 * Return the expected number of free elements in the zone.
925 * This calculation will be incorrect if items are zfree'd that
926 * were never zalloc'd/zget'd. The correct way to stuff memory
927 * into a zone is by zcram.
931 zone_free_count(zone_t zone
)
933 integer_t free_count
;
936 free_count
= zone
->cur_size
/zone
->elem_size
- zone
->count
;
939 assert(free_count
>= 0);
945 * zprealloc preallocates wired memory, exanding the specified
946 * zone to the specified size
956 if (kmem_alloc_wired(zone_map
, &addr
, size
) != KERN_SUCCESS
)
958 zone_page_init(addr
, size
, ZONE_PAGE_USED
);
959 zcram(zone
, addr
, size
);
964 * Zone garbage collection subroutines
966 * These routines have in common the modification of entries in the
967 * zone_page_table. The latter contains one entry for every page
970 * For each page table entry in the given range:
972 * zone_page_collectable - test if one (in_free_list == alloc_count)
973 * zone_page_keep - reset in_free_list
974 * zone_page_in_use - decrements in_free_list
975 * zone_page_free - increments in_free_list
976 * zone_page_init - initializes in_free_list and alloc_count
977 * zone_page_alloc - increments alloc_count
978 * zone_page_dealloc - decrements alloc_count
979 * zone_add_free_page_list - adds the page to the free list
981 * Two counts are maintained for each page, the in_free_list count and
982 * alloc_count. The alloc_count is how many zone elements have been
983 * allocated from a page. (Note that the page could contain elements
984 * that span page boundaries. The count includes these elements so
985 * one element may be counted in two pages.) In_free_list is a count
986 * of how many zone elements are currently free. If in_free_list is
987 * equal to alloc_count then the page is eligible for garbage
990 * Alloc_count and in_free_list are initialized to the correct values
991 * for a particular zone when a page is zcram'ed into a zone. Subsequent
992 * gets and frees of zone elements will call zone_page_in_use and
993 * zone_page_free which modify the in_free_list count. When the zones
994 * garbage collector runs it will walk through a zones free element list,
995 * remove the elements that reside on collectable pages, and use
996 * zone_add_free_page_list to create a list of pages to be collected.
999 zone_page_collectable(
1006 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1007 panic("zone_page_collectable");
1010 i
= atop(addr
-zone_map_min_address
);
1011 j
= atop((addr
+size
-1) - zone_map_min_address
);
1012 lock_zone_page_table();
1013 for (; i
<= j
; i
++) {
1014 if (zone_page_table
[i
].in_free_list
==
1015 zone_page_table
[i
].alloc_count
) {
1016 unlock_zone_page_table();
1020 unlock_zone_page_table();
1032 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1033 panic("zone_page_keep");
1036 i
= atop(addr
-zone_map_min_address
);
1037 j
= atop((addr
+size
-1) - zone_map_min_address
);
1038 lock_zone_page_table();
1039 for (; i
<= j
; i
++) {
1040 zone_page_table
[i
].in_free_list
= 0;
1042 unlock_zone_page_table();
1053 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1054 panic("zone_page_in_use");
1057 i
= atop(addr
-zone_map_min_address
);
1058 j
= atop((addr
+size
-1) - zone_map_min_address
);
1059 lock_zone_page_table();
1060 for (; i
<= j
; i
++) {
1061 if (zone_page_table
[i
].in_free_list
> 0)
1062 zone_page_table
[i
].in_free_list
--;
1064 unlock_zone_page_table();
1075 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1076 panic("zone_page_free");
1079 i
= atop(addr
-zone_map_min_address
);
1080 j
= atop((addr
+size
-1) - zone_map_min_address
);
1081 lock_zone_page_table();
1082 for (; i
<= j
; i
++) {
1083 assert(zone_page_table
[i
].in_free_list
>= 0);
1084 zone_page_table
[i
].in_free_list
++;
1086 unlock_zone_page_table();
1098 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1099 panic("zone_page_init");
1102 i
= atop(addr
-zone_map_min_address
);
1103 j
= atop((addr
+size
-1) - zone_map_min_address
);
1104 lock_zone_page_table();
1105 for (; i
<= j
; i
++) {
1106 zone_page_table
[i
].alloc_count
= value
;
1107 zone_page_table
[i
].in_free_list
= 0;
1109 unlock_zone_page_table();
1120 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1121 panic("zone_page_alloc");
1124 i
= atop(addr
-zone_map_min_address
);
1125 j
= atop((addr
+size
-1) - zone_map_min_address
);
1126 lock_zone_page_table();
1127 for (; i
<= j
; i
++) {
1128 /* Set alloc_count to (ZONE_PAGE_USED + 1) if
1129 * it was previously set to ZONE_PAGE_UNUSED.
1131 if (zone_page_table
[i
].alloc_count
== ZONE_PAGE_UNUSED
) {
1132 zone_page_table
[i
].alloc_count
= 1;
1134 zone_page_table
[i
].alloc_count
++;
1137 unlock_zone_page_table();
1148 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1149 panic("zone_page_dealloc");
1152 i
= atop(addr
-zone_map_min_address
);
1153 j
= atop((addr
+size
-1) - zone_map_min_address
);
1154 lock_zone_page_table();
1155 for (; i
<= j
; i
++) {
1156 zone_page_table
[i
].alloc_count
--;
1158 unlock_zone_page_table();
1162 zone_add_free_page_list(
1163 struct zone_page_table_entry
**free_list
,
1170 if (!from_zone_map(addr
) || !from_zone_map(addr
+size
-1))
1171 panic("zone_add_free_page_list");
1174 i
= atop(addr
-zone_map_min_address
);
1175 j
= atop((addr
+size
-1) - zone_map_min_address
);
1176 lock_zone_page_table();
1177 for (; i
<= j
; i
++) {
1178 if (zone_page_table
[i
].alloc_count
== 0) {
1179 zone_page_table
[i
].next
= *free_list
;
1180 *free_list
= &zone_page_table
[i
];
1181 zone_page_table
[i
].alloc_count
= ZONE_PAGE_UNUSED
;
1182 zone_page_table
[i
].in_free_list
= 0;
1185 unlock_zone_page_table();
1189 /* This is used for walking through a zone's free element list.
1191 struct zone_free_entry
{
1192 struct zone_free_entry
* next
;
1195 int reclaim_page_count
= 0;
1197 /* Zone garbage collection
1199 * zone_gc will walk through all the free elements in all the
1200 * zones that are marked collectable looking for reclaimable
1201 * pages. zone_gc is called by consider_zone_gc when the system
1202 * begins to run out of memory.
1207 unsigned int max_zones
;
1210 struct zone_page_table_entry
*freep
;
1211 struct zone_page_table_entry
*zone_free_page_list
;
1213 mutex_lock(&zone_gc_lock
);
1216 * Note that this scheme of locking only to walk the zone list
1217 * assumes that zones are never freed (checked by zfree)
1219 simple_lock(&all_zones_lock
);
1220 max_zones
= num_zones
;
1222 simple_unlock(&all_zones_lock
);
1225 lock_zone_page_table();
1226 for (i
= 0; i
< zone_pages
; i
++)
1227 assert(zone_page_table
[i
].in_free_list
== 0);
1228 unlock_zone_page_table();
1229 #endif /* MACH_ASSERT */
1231 zone_free_page_list
= (struct zone_page_table_entry
*) 0;
1233 for (i
= 0; i
< max_zones
; i
++, z
= z
->next_zone
) {
1234 struct zone_free_entry
* prev
;
1235 struct zone_free_entry
* elt
;
1236 struct zone_free_entry
* end
;
1238 assert(z
!= ZONE_NULL
);
1240 if (!z
->collectable
)
1246 * Do a quick feasability check before we scan the zone:
1247 * skip unless there is likelihood of getting 1+ pages back.
1249 if ((z
->cur_size
- z
->count
* z
->elem_size
) <= (2*PAGE_SIZE
)){
1254 /* Count the free elements in each page. This loop
1255 * requires that all in_free_list entries are zero.
1257 * Exit the loop early if we need to hurry up and drop
1258 * the lock to allow preemption - but we must fully process
1259 * all elements we looked at so far.
1261 elt
= (struct zone_free_entry
*)(z
->free_elements
);
1262 while (!ast_urgency() && (elt
!= (struct zone_free_entry
*)0)) {
1263 if (from_zone_map(elt
))
1264 zone_page_free((vm_offset_t
)elt
, z
->elem_size
);
1269 /* Now determine which elements should be removed
1270 * from the free list and, after all the elements
1271 * on a page have been removed, add the element's
1272 * page to a list of pages to be freed.
1274 prev
= elt
= (struct zone_free_entry
*)(z
->free_elements
);
1275 while (elt
!= end
) {
1276 if (!from_zone_map(elt
)) {
1281 if (zone_page_collectable((vm_offset_t
)elt
,
1283 z
->cur_size
-= z
->elem_size
;
1284 zone_page_in_use((vm_offset_t
)elt
,
1286 zone_page_dealloc((vm_offset_t
)elt
,
1288 zone_add_free_page_list(&zone_free_page_list
,
1293 z
->free_elements
=(vm_offset_t
)elt
;
1296 prev
->next
= elt
->next
;
1300 /* This element is not eligible for collection
1301 * so clear in_free_list in preparation for a
1302 * subsequent garbage collection pass.
1304 zone_page_keep((vm_offset_t
)elt
, z
->elem_size
);
1308 } /* end while(elt != end) */
1313 for (freep
= zone_free_page_list
; freep
!= 0; freep
= freep
->next
) {
1314 vm_offset_t free_addr
;
1316 free_addr
= zone_map_min_address
+
1317 PAGE_SIZE
* (freep
- zone_page_table
);
1318 kmem_free(zone_map
, free_addr
, PAGE_SIZE
);
1319 reclaim_page_count
++;
1321 mutex_unlock(&zone_gc_lock
);
1327 * Called by the pageout daemon when the system needs more free pages.
1331 consider_zone_gc(void)
1334 * By default, don't attempt zone GC more frequently
1335 * than once a second.
1338 if (zone_gc_max_rate
== 0)
1339 zone_gc_max_rate
= (1 << SCHED_TICK_SHIFT
) + 1;
1341 if (zone_gc_allowed
&&
1342 ((sched_tick
> (zone_gc_last_tick
+ zone_gc_max_rate
)) ||
1344 zone_gc_forced
= FALSE
;
1345 zone_gc_last_tick
= sched_tick
;
1350 #include <mach/kern_return.h>
1351 #include <mach/machine/vm_types.h>
1352 #include <mach_debug/zone_info.h>
1353 #include <kern/host.h>
1354 #include <vm/vm_map.h>
1355 #include <vm/vm_kern.h>
1357 #include <mach/mach_host_server.h>
1362 zone_name_array_t
*namesp
,
1363 mach_msg_type_number_t
*namesCntp
,
1364 zone_info_array_t
*infop
,
1365 mach_msg_type_number_t
*infoCntp
)
1368 vm_offset_t names_addr
;
1369 vm_size_t names_size
;
1371 vm_offset_t info_addr
;
1372 vm_size_t info_size
;
1373 unsigned int max_zones
, i
;
1379 if (host
== HOST_NULL
)
1380 return KERN_INVALID_HOST
;
1383 * We assume that zones aren't freed once allocated.
1384 * We won't pick up any zones that are allocated later.
1387 simple_lock(&all_zones_lock
);
1389 max_zones
= num_zones
+ 4;
1391 max_zones
= num_zones
+ 2;
1394 simple_unlock(&all_zones_lock
);
1396 if (max_zones
<= *namesCntp
) {
1397 /* use in-line memory */
1401 names_size
= round_page(max_zones
* sizeof *names
);
1402 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1403 &names_addr
, names_size
);
1404 if (kr
!= KERN_SUCCESS
)
1406 names
= (zone_name_t
*) names_addr
;
1409 if (max_zones
<= *infoCntp
) {
1410 /* use in-line memory */
1414 info_size
= round_page(max_zones
* sizeof *info
);
1415 kr
= kmem_alloc_pageable(ipc_kernel_map
,
1416 &info_addr
, info_size
);
1417 if (kr
!= KERN_SUCCESS
) {
1418 if (names
!= *namesp
)
1419 kmem_free(ipc_kernel_map
,
1420 names_addr
, names_size
);
1424 info
= (zone_info_t
*) info_addr
;
1429 for (i
= 0; i
< num_zones
; i
++) {
1432 assert(z
!= ZONE_NULL
);
1438 simple_lock(&all_zones_lock
);
1440 simple_unlock(&all_zones_lock
);
1442 /* assuming here the name data is static */
1443 (void) strncpy(zn
->zn_name
, zcopy
.zone_name
,
1444 sizeof zn
->zn_name
);
1446 zi
->zi_count
= zcopy
.count
;
1447 zi
->zi_cur_size
= zcopy
.cur_size
;
1448 zi
->zi_max_size
= zcopy
.max_size
;
1449 zi
->zi_elem_size
= zcopy
.elem_size
;
1450 zi
->zi_alloc_size
= zcopy
.alloc_size
;
1451 zi
->zi_exhaustible
= zcopy
.exhaustible
;
1452 zi
->zi_collectable
= zcopy
.collectable
;
1457 strcpy(zn
->zn_name
, "kernel_stacks");
1458 stack_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
, "save_areas");
1464 save_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
, "pmap_mappings");
1470 mapping_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1471 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1475 strcpy(zn
->zn_name
, "kalloc.large");
1476 kalloc_fake_zone_info(&zi
->zi_count
, &zi
->zi_cur_size
, &zi
->zi_max_size
, &zi
->zi_elem_size
,
1477 &zi
->zi_alloc_size
, &zi
->zi_collectable
, &zi
->zi_exhaustible
);
1479 if (names
!= *namesp
) {
1483 used
= max_zones
* sizeof *names
;
1485 if (used
!= names_size
)
1486 bzero((char *) (names_addr
+ used
), names_size
- used
);
1488 kr
= vm_map_copyin(ipc_kernel_map
, names_addr
, names_size
,
1490 assert(kr
== KERN_SUCCESS
);
1492 *namesp
= (zone_name_t
*) copy
;
1494 *namesCntp
= max_zones
;
1496 if (info
!= *infop
) {
1500 used
= max_zones
* sizeof *info
;
1502 if (used
!= info_size
)
1503 bzero((char *) (info_addr
+ used
), info_size
- used
);
1505 kr
= vm_map_copyin(ipc_kernel_map
, info_addr
, info_size
,
1507 assert(kr
== KERN_SUCCESS
);
1509 *infop
= (zone_info_t
*) copy
;
1511 *infoCntp
= max_zones
;
1513 return KERN_SUCCESS
;
1517 #include <ddb/db_command.h>
1518 #include <ddb/db_output.h>
1519 #include <kern/kern_print.h>
1521 const char *zone_labels
=
1522 "ENTRY COUNT TOT_SZ MAX_SZ ELT_SZ ALLOC_SZ NAME";
1529 void db_zone_check_active(
1531 void db_zone_print_active(
1533 #endif /* ZONE_DEBUG */
1534 void db_zone_print_free(
1544 db_printf("%8x %8x %8x %8x %6x %8x %s ",
1545 addr
, zcopy
.count
, zcopy
.cur_size
,
1546 zcopy
.max_size
, zcopy
.elem_size
,
1547 zcopy
.alloc_size
, zcopy
.zone_name
);
1548 if (zcopy
.exhaustible
)
1550 if (zcopy
.collectable
)
1552 if (zcopy
.expandable
)
1565 struct zone
*z
= (zone_t
)addr
;
1567 if (z
== ZONE_NULL
|| !have_addr
){
1568 db_error("No Zone\n");
1572 db_printf("%s\n", zone_labels
);
1588 * Don't risk hanging by unconditionally locking,
1589 * risk of incoherent data is small (zones aren't freed).
1591 have_addr
= simple_lock_try(&all_zones_lock
);
1595 simple_unlock(&all_zones_lock
);
1598 db_printf("%s\n", zone_labels
);
1599 for ( ; count
> 0; count
--) {
1601 db_error("Mangled Zone List\n");
1605 total
+= z
->cur_size
,
1607 have_addr
= simple_lock_try(&all_zones_lock
);
1610 simple_unlock(&all_zones_lock
);
1613 db_printf("\nTotal %8x", total
);
1614 db_printf("\n\nzone_gc() has reclaimed %d pages\n",
1615 reclaim_page_count
);
1620 db_zone_check_active(
1626 if (!zone_debug_enabled(zone
) || !zone_check
)
1628 tmp_elem
= queue_first(&zone
->active_zones
);
1629 while (count
< zone
->count
) {
1631 if (tmp_elem
== 0) {
1632 printf("unexpected zero element, zone=0x%x, count=%d\n",
1637 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1638 printf("unexpected queue_end, zone=0x%x, count=%d\n",
1643 tmp_elem
= queue_next(tmp_elem
);
1645 if (!queue_end(tmp_elem
, &zone
->active_zones
)) {
1646 printf("not at queue_end, zone=0x%x, tmp_elem=0x%x\n",
1653 db_zone_print_active(
1659 if (!zone_debug_enabled(zone
)) {
1660 printf("zone 0x%x debug not enabled\n", zone
);
1664 printf("zone_check FALSE\n");
1668 printf("zone 0x%x, active elements %d\n", zone
, zone
->count
);
1669 printf("active list:\n");
1670 tmp_elem
= queue_first(&zone
->active_zones
);
1671 while (count
< zone
->count
) {
1672 printf(" 0x%x", tmp_elem
);
1674 if ((count
% 6) == 0)
1676 if (tmp_elem
== 0) {
1677 printf("\nunexpected zero element, count=%d\n", count
);
1680 if (queue_end(tmp_elem
, &zone
->active_zones
)) {
1681 printf("\nunexpected queue_end, count=%d\n", count
);
1684 tmp_elem
= queue_next(tmp_elem
);
1686 if (!queue_end(tmp_elem
, &zone
->active_zones
))
1687 printf("\nnot at queue_end, tmp_elem=0x%x\n", tmp_elem
);
1691 #endif /* ZONE_DEBUG */
1701 freecount
= zone_free_count(zone
);
1702 printf("zone 0x%x, free elements %d\n", zone
, freecount
);
1703 printf("free list:\n");
1704 elem
= zone
->free_elements
;
1705 while (count
< freecount
) {
1706 printf(" 0x%x", elem
);
1708 if ((count
% 6) == 0)
1711 printf("\nunexpected zero element, count=%d\n", count
);
1714 elem
= *((vm_offset_t
*)elem
);
1717 printf("\nnot at end of free list, elem=0x%x\n", elem
);
1722 #endif /* MACH_KDB */
1727 /* should we care about locks here ? */
1735 if (!zone_debug_enabled(z
))
1737 elt
-= sizeof(queue_chain_t
);
1738 elt
= (vm_offset_t
) queue_next((queue_t
) elt
);
1739 if ((queue_t
) elt
== &z
->active_zones
)
1741 elt
+= sizeof(queue_chain_t
);
1751 if (!zone_debug_enabled(z
))
1753 if (queue_empty(&z
->active_zones
))
1755 elt
= (vm_offset_t
) queue_first(&z
->active_zones
);
1756 elt
+= sizeof(queue_chain_t
);
1761 * Second arg controls how many zone elements are printed:
1764 * n, n > 0 => last n on active list
1773 boolean_t print
= (tail
!= 0);
1777 if (z
->count
< tail
)
1779 tail
= z
->count
- tail
;
1780 for (elt
= first_element(z
); elt
; elt
= next_element(z
, elt
)) {
1781 if (print
&& tail
<= count
)
1782 db_printf("%8x\n", elt
);
1785 assert(count
== z
->count
);
1788 #endif /* MACH_KDB */
1790 #define zone_in_use(z) ( z->count || z->free_elements )
1796 if (zone_debug_enabled(z
) || zone_in_use(z
) ||
1797 z
->alloc_size
< (z
->elem_size
+ sizeof(queue_chain_t
)))
1799 queue_init(&z
->active_zones
);
1800 z
->elem_size
+= sizeof(queue_chain_t
);
1807 if (!zone_debug_enabled(z
) || zone_in_use(z
))
1809 z
->elem_size
-= sizeof(queue_chain_t
);
1810 z
->active_zones
.next
= z
->active_zones
.prev
= 0;
1812 #endif /* ZONE_DEBUG */