2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * Author: Avadis Tevanian, Jr.
62 * Zone-based memory allocator. A zone is a collection of fixed size
63 * data blocks for which quick allocation/deallocation is possible.
66 #define ZALLOC_ALLOW_DEPRECATED 1
68 #include <mach/mach_types.h>
69 #include <mach/vm_param.h>
70 #include <mach/kern_return.h>
71 #include <mach/mach_host_server.h>
72 #include <mach/task_server.h>
73 #include <mach/machine/vm_types.h>
74 #include <mach/vm_map.h>
77 #include <kern/bits.h>
78 #include <kern/startup.h>
79 #include <kern/kern_types.h>
80 #include <kern/assert.h>
81 #include <kern/backtrace.h>
82 #include <kern/host.h>
83 #include <kern/macro_help.h>
84 #include <kern/sched.h>
85 #include <kern/locks.h>
86 #include <kern/sched_prim.h>
87 #include <kern/misc_protos.h>
88 #include <kern/thread_call.h>
89 #include <kern/zalloc_internal.h>
90 #include <kern/kalloc.h>
92 #include <prng/random.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_compressor.h> /* C_SLOT_PACKED_PTR* */
101 #include <pexpert/pexpert.h>
103 #include <machine/machparam.h>
104 #include <machine/machine_routines.h> /* ml_cpu_get_info */
106 #include <os/atomic.h>
108 #include <libkern/OSDebug.h>
109 #include <libkern/OSAtomic.h>
110 #include <libkern/section_keywords.h>
111 #include <sys/kdebug.h>
113 #include <san/kasan.h>
117 * Set to 0 to debug poisoning and ZC_ZFREE_CLEARMEM validation under kasan.
118 * Otherwise they are double-duty with what kasan already does.
120 #define ZALLOC_ENABLE_POISONING 0
121 #define ZONE_ENABLE_LOGGING 0
122 #elif DEBUG || DEVELOPMENT
123 #define ZALLOC_ENABLE_POISONING 1
124 #define ZONE_ENABLE_LOGGING 1
126 #define ZALLOC_ENABLE_POISONING 1
127 #define ZONE_ENABLE_LOGGING 0
131 #define ZALLOC_EARLY_GAPS 1
133 #define ZALLOC_EARLY_GAPS 0
137 #define z_debug_assert(expr) assert(expr)
139 #define z_debug_assert(expr) (void)(expr)
142 extern void vm_pageout_garbage_collect(int collect
);
144 /* Returns pid of the task with the largest number of VM map entries. */
145 extern pid_t
find_largest_process_vm_map_entries(void);
148 * Callout to jetsam. If pid is -1, we wake up the memorystatus thread to do asynchronous kills.
149 * For any other pid we try to kill that process synchronously.
151 extern boolean_t
memorystatus_kill_on_zone_map_exhaustion(pid_t pid
);
153 extern zone_t vm_map_entry_zone
;
154 extern zone_t vm_object_zone
;
156 #define ZONE_MIN_ELEM_SIZE sizeof(uint64_t)
157 #define ZONE_MAX_ALLOC_SIZE (32 * 1024)
159 struct zone_page_metadata
{
160 /* The index of the zone this metadata page belongs to */
161 zone_id_t zm_index
: 11;
163 /* Whether `zm_bitmap` is an inline bitmap or a packed bitmap reference */
164 uint16_t zm_inline_bitmap
: 1;
167 * Zones allocate in "chunks" of zone_t::z_chunk_pages consecutive
168 * pages, or zpercpu_count() pages if the zone is percpu.
170 * The first page of it has its metadata set with:
171 * - 0 if none of the pages are currently wired
172 * - the number of wired pages in the chunk (not scaled for percpu).
174 * Other pages in the chunk have their zm_chunk_len set to
175 * ZM_SECONDARY_PAGE or ZM_SECONDARY_PCPU_PAGE depending on whether
176 * the zone is percpu or not. For those, zm_page_index holds the
177 * index of that page in the run.
179 uint16_t zm_chunk_len
: 4;
180 #define ZM_CHUNK_LEN_MAX 0x8
181 #define ZM_SECONDARY_PAGE 0xe
182 #define ZM_SECONDARY_PCPU_PAGE 0xf
185 #define ZM_ALLOC_SIZE_LOCK 1u
186 uint16_t zm_alloc_size
; /* first page only */
187 uint16_t zm_page_index
; /* secondary pages only */
190 uint32_t zm_bitmap
; /* most zones */
191 uint32_t zm_bump
; /* permanent zones */
194 zone_pva_t zm_page_next
;
195 zone_pva_t zm_page_prev
;
197 static_assert(sizeof(struct zone_page_metadata
) == 16, "validate packing");
199 __enum_closed_decl(zone_addr_kind_t
, bool, {
203 #define ZONE_ADDR_KIND_COUNT 2
206 * @typedef zone_element_t
209 * Type that represents a "resolved" zone element.
212 * This type encodes an element pointer as a tuple of:
213 * { chunk base, element index, element protection }.
215 * The chunk base is extracted with @c trunc_page()
216 * as it is always page aligned, and occupies the bits above @c PAGE_SHIFT.
218 * The low two bits encode the protection mode (see @c zprot_mode_t).
220 * The other bits encode the element index in the chunk rather than its address.
222 typedef struct zone_element
{
223 vm_offset_t ze_value
;
227 * @typedef zone_magazine_t
230 * Magazine of cached allocations.
232 * @field zm_cur how many elements this magazine holds (unused while loaded).
233 * @field zm_link linkage used by magazine depots.
234 * @field zm_elems an array of @c zc_mag_size() elements.
236 typedef struct zone_magazine
{
238 STAILQ_ENTRY(zone_magazine
) zm_link
;
239 zone_element_t zm_elems
[0];
243 * @typedef zone_cache_t
246 * Magazine of cached allocations.
249 * Below is a diagram of the caching system. This design is inspired by the
250 * paper "Magazines and Vmem: Extending the Slab Allocator to Many CPUs and
251 * Arbitrary Resources" by Jeff Bonwick and Jonathan Adams and the FreeBSD UMA
252 * zone allocator (itself derived from this seminal work).
254 * It is divided into 3 layers:
255 * - the per-cpu layer,
256 * - the recirculation depot layer,
257 * - the Zone Allocator.
259 * The per-cpu and recirculation depot layer use magazines (@c zone_magazine_t),
260 * which are stacks of up to @c zc_mag_size() elements.
264 * The CPU layer (@c zone_cache_t) looks like this:
266 * ╭─ a ─ f ─┬───────── zm_depot ──────────╮
267 * │ ╭─╮ ╭─╮ │ ╭─╮ ╭─╮ ╭─╮ ╭─╮ ╭─╮ │
268 * │ │#│ │#│ │ │#│ │#│ │#│ │#│ │#│ │
269 * │ │#│ │ │ │ │#│ │#│ │#│ │#│ │#│ │
270 * │ │ │ │ │ │ │#│ │#│ │#│ │#│ │#│ │
271 * │ ╰─╯ ╰─╯ │ ╰─╯ ╰─╯ ╰─╯ ╰─╯ ╰─╯ │
272 * ╰─────────┴─────────────────────────────╯
274 * It has two pre-loaded magazines (a)lloc and (f)ree which we allocate from,
275 * or free to. Serialization is achieved through disabling preemption, and only
276 * the current CPU can acces those allocations. This is represented on the left
277 * hand side of the diagram above.
279 * The right hand side is the per-cpu depot. It consists of @c zm_depot_count
280 * full magazines, and is protected by the @c zm_depot_lock for access.
281 * The lock is expected to absolutely never be contended, as only the local CPU
282 * tends to access the local per-cpu depot in regular operation mode.
284 * However unlike UMA, our implementation allows for the zone GC to reclaim
285 * per-CPU magazines aggresively, which is serialized with the @c zm_depot_lock.
288 * <h2>Recirculation Depot</h2>
290 * The recirculation depot layer is a list similar to the per-cpu depot,
291 * however it is different in two fundamental ways:
293 * - it is protected by the regular zone lock,
294 * - elements referenced by the magazines in that layer appear free
298 * <h2>Magazine circulation and sizing</h2>
300 * The caching system sizes itself dynamically. Operations that allocate/free
301 * a single element call @c zone_lock_nopreempt_check_contention() which records
302 * contention on the lock by doing a trylock and recording its success.
304 * This information is stored in the @c z_contention_cur field of the zone,
305 * and a windoed moving average is maintained in @c z_contention_wma.
306 * Each time a CPU registers any contention, it will also allow its own per-cpu
307 * cache to grow, incrementing @c zc_depot_max, which is how the per-cpu layer
308 * might grow into using its local depot.
310 * Note that @c zc_depot_max assume that the (a) and (f) pre-loaded magazines
311 * on average contain @c zc_mag_size() elements.
313 * When a per-cpu layer cannot hold more full magazines in its depot,
314 * then it will overflow about 1/3 of its depot into the recirculation depot
315 * (see @c zfree_cached_slow(). Conversely, when a depot is empty, then it will
316 * refill its per-cpu depot to about 1/3 of its size from the recirculation
317 * depot (see @c zalloc_cached_slow()).
319 * Lastly, the zone layer keeps track of the high and low watermark of how many
320 * elements have been free per period of time (including being part of the
321 * recirculation depot) in the @c z_elems_free_min and @c z_elems_free_max
322 * fields. A weighted moving average of the amplitude of this is maintained in
323 * the @c z_elems_free_wss which informs the zone GC on how to gently trim
324 * zones without hurting performance.
327 * <h2>Security considerations</h2>
329 * The zone caching layer has been designed to avoid returning elements in
330 * a strict LIFO behavior: @c zalloc() will allocate from the (a) magazine,
331 * and @c zfree() free to the (f) magazine, and only swap them when the
332 * requested operation cannot be fulfilled.
334 * The per-cpu overflow depot or the recirculation depots are similarly used
337 * More importantly, when magazines flow through the recirculation depot,
338 * the elements they contain are marked as "free" in the zone layer bitmaps.
339 * Because allocations out of per-cpu caches verify the bitmaps at allocation
340 * time, this acts as a poor man's double-free quarantine. The magazines
341 * allow to avoid the cost of the bit-scanning involved in the zone-level
342 * @c zalloc_item() codepath.
345 * @field zc_alloc_cur denormalized number of elements in the (a) magazine
346 * @field zc_free_cur denormalized number of elements in the (f) magazine
347 * @field zc_alloc_elems a pointer to the array of elements in (a)
348 * @field zc_free_elems a pointer to the array of elements in (f)
350 * @field zc_depot_lock a lock to access @c zc_depot, @c zc_depot_cur.
351 * @field zc_depot a list of @c zc_depot_cur full magazines
352 * @field zc_depot_cur number of magazines in @c zc_depot
353 * @field zc_depot_max the maximum number of elements in @c zc_depot,
354 * protected by the zone lock.
356 typedef struct zone_cache
{
357 uint16_t zc_alloc_cur
;
358 uint16_t zc_free_cur
;
359 uint16_t zc_depot_cur
;
360 uint16_t __zc_padding
;
361 zone_element_t
*zc_alloc_elems
;
362 zone_element_t
*zc_free_elems
;
363 hw_lock_bit_t zc_depot_lock
;
364 uint32_t zc_depot_max
;
365 struct zone_depot zc_depot
;
368 static __security_const_late
struct {
369 struct zone_map_range zi_map_range
[ZONE_ADDR_KIND_COUNT
];
370 struct zone_map_range zi_meta_range
; /* debugging only */
371 struct zone_map_range zi_bits_range
; /* bits buddy allocator */
374 * The metadata lives within the zi_meta_range address range.
376 * The correct formula to find a metadata index is:
377 * absolute_page_index - page_index(MIN(zi_map_range[*].min_address))
379 * And then this index is used to dereference zi_meta_range.min_address
380 * as a `struct zone_page_metadata` array.
382 * To avoid doing that substraction all the time in the various fast-paths,
383 * zi_meta_base are pre-offset with that minimum page index to avoid redoing
384 * that math all the time.
386 * Do note that the array might have a hole punched in the middle,
387 * see zone_metadata_init().
389 struct zone_page_metadata
*zi_meta_base
;
393 * Initial array of metadata for stolen memory.
395 * The numbers here have to be kept in sync with vm_map_steal_memory()
396 * so that we have reserved enough metadata.
398 * After zone_init() has run (which happens while the kernel is still single
399 * threaded), the metadata is moved to its final dynamic location, and
400 * this array is unmapped with the rest of __startup_data at lockdown.
403 #define ZONE_FOREIGN_META_INLINE_COUNT 20032
405 #define ZONE_FOREIGN_META_INLINE_COUNT 64
408 static struct zone_page_metadata
409 zone_foreign_meta_array_startup
[ZONE_FOREIGN_META_INLINE_COUNT
];
412 * The zone_locks_grp allows for collecting lock statistics.
413 * All locks are associated to this group in zinit.
414 * Look at tools/lockstat for debugging lock contention.
416 static LCK_GRP_DECLARE(zone_locks_grp
, "zone_locks");
417 static LCK_MTX_EARLY_DECLARE(zone_metadata_region_lck
, &zone_locks_grp
);
420 * Exclude more than one concurrent garbage collection
422 static LCK_GRP_DECLARE(zone_gc_lck_grp
, "zone_gc");
423 static LCK_MTX_EARLY_DECLARE(zone_gc_lock
, &zone_gc_lck_grp
);
425 bool panic_include_zprint
= FALSE
;
426 mach_memory_info_t
*panic_kext_memory_info
= NULL
;
427 vm_size_t panic_kext_memory_size
= 0;
430 * Protects zone_array, num_zones, num_zones_in_use, and
431 * zone_destroyed_bitmap
433 static SIMPLE_LOCK_DECLARE(all_zones_lock
, 0);
434 static zone_id_t num_zones_in_use
;
435 zone_id_t _Atomic num_zones
;
436 SECURITY_READ_ONLY_LATE(unsigned int) zone_view_count
;
439 #define MAX_ZONES 566
440 #else /* !KASAN_ZALLOC */
441 #define MAX_ZONES 402
442 #endif/* !KASAN_ZALLOC */
445 * Initial globals for zone stats until we can allocate the real ones.
446 * Those get migrated inside the per-CPU ones during zone_init() and
447 * this array is unmapped with the rest of __startup_data at lockdown.
450 /* zone to allocate zone_magazine structs from */
451 static SECURITY_READ_ONLY_LATE(zone_t
) zc_magazine_zone
;
453 * Until pid1 is made, zone caching is off,
454 * until compute_zone_working_set_size() runs for the firt time.
456 * -1 represents the "never enabled yet" value.
458 static int8_t zone_caching_disabled
= -1;
461 static struct zone_cache zone_cache_startup
[MAX_ZONES
];
463 static struct zone_stats zone_stats_startup
[MAX_ZONES
];
464 struct zone zone_array
[MAX_ZONES
];
466 /* Initialized in zone_bootstrap(), how many "copies" the per-cpu system does */
467 static SECURITY_READ_ONLY_LATE(unsigned) zpercpu_early_count
;
469 /* Used to keep track of destroyed slots in the zone_array */
470 static bitmap_t zone_destroyed_bitmap
[BITMAP_LEN(MAX_ZONES
)];
472 /* number of zone mapped pages used by all zones */
473 static long _Atomic zones_phys_page_mapped_count
;
476 * Turn ZSECURITY_OPTIONS_STRICT_IOKIT_FREE off on x86 so as not
477 * not break third party kexts that haven't yet been recompiled
478 * to use the new iokit macros.
480 #if XNU_TARGET_OS_OSX && __x86_64__
481 #define ZSECURITY_OPTIONS_STRICT_IOKIT_FREE_DEFAULT 0
483 #define ZSECURITY_OPTIONS_STRICT_IOKIT_FREE_DEFAULT \
484 ZSECURITY_OPTIONS_STRICT_IOKIT_FREE
487 #define ZSECURITY_DEFAULT ( \
488 ZSECURITY_OPTIONS_SEQUESTER | \
489 ZSECURITY_OPTIONS_SUBMAP_USER_DATA | \
490 ZSECURITY_OPTIONS_SEQUESTER_KEXT_KALLOC | \
491 ZSECURITY_OPTIONS_STRICT_IOKIT_FREE_DEFAULT | \
493 TUNABLE(zone_security_options_t
, zsecurity_options
, "zs", ZSECURITY_DEFAULT
);
496 /* enable tags for zones that ask for it */
497 static TUNABLE(bool, zone_tagging_on
, "-zt", false);
498 #endif /* VM_MAX_TAG_ZONES */
500 #if DEBUG || DEVELOPMENT
501 TUNABLE(bool, zalloc_disable_copyio_check
, "-no-copyio-zalloc-check", false);
502 #endif /* DEBUG || DEVELOPMENT */
504 /* Making pointer scanning leaks detection possible for all zones */
505 static TUNABLE(bool, zone_leaks_scan_enable
, "-zl", false);
507 #define zone_leaks_scan_enable false
510 /*! @enum zprot_mode_t
513 * Zone element corruption detection mode.
516 * We use four techniques to detect modification of a zone element
517 * after it's been freed.
519 * Elements that are in zones can be in 3 possible states:
520 * - zeroed out (@c ZPM_ZERO)
521 * - poisoned (@c ZPM_POISON) with the @c ZONE_POISON pattern
522 * - with a left and right canary (@c ZPM_CANARY).
524 * @c ZPM_AUTO is used when the actual protection for the element is unknown,
525 * and will be detected looking at the last word of the allocation at validation
528 * The mode of an element in zones is discovered by looking at its last
529 * pointer-sized value:
530 * - 0 means that it is zeroed out
531 * - @c ZONE_POISON means it is poisoned
532 * - any other value means it is using canaries.
534 * Elements are zeroed if:
535 * - the element size is smaller than @c zp_min_size,
536 * - the owning zone has the @c z_free_zeroes flag set,
537 * - the chunk backing store is fresh (and was just allocated).
539 * Elements are poisoned periodically for every N frees (counted per-zone),
540 * if the elements aren't otherwise zeroed out.
541 * If -zp is passed as a boot arg, poisoning occurs for every free.
543 * Else elements use canaries. When canaries are used, the first and last
544 * pointer sized values in the allocation are set to values derived from the
545 * element address and the @c zp_canary nonce. The first @c zp_min_size
546 * bytes of the elment are also cleared.
548 * Performance slowdown is inversely proportional to the frequency of poisoning,
549 * with a 4-5% hit around N=1, down to ~0.3% at N=16 and just "noise" at N=32
550 * and higher. You can expect to find a 100% reproducible bug in an average of
551 * N tries, with a standard deviation of about N, but you will want to set
552 * "-zp" to always poison every free if you are attempting to reproduce
555 * For a more heavyweight, but finer-grained method of detecting misuse
556 * of zone memory, look up the "Guard mode" zone allocator in gzalloc.c.
558 __enum_closed_decl(zprot_mode_t
, vm_offset_t
, {
559 ZPM_AUTO
, /* element is indeterminate */
560 ZPM_ZERO
, /* element is zeroed */
561 ZPM_POISON
, /* element is poisoned */
562 ZPM_CANARY
, /* element extremities have a canary */
564 #define ZPM_MASK ((zprot_mode_t)0x3)
568 * set by zp-factor=N boot arg
570 * A zp_factor of 0 indicates zone poisoning is disabled and can also be set by
571 * passing the -no-zp boot-arg.
573 * A zp_factor of 1 indicates zone poisoning is on for all elements and can be
574 * set by passing the -zp boot-arg.
576 static TUNABLE(uint32_t, zp_factor
, "zp-factor", 16);
578 /* set by zp-scale=N boot arg, scales zp_factor by zone size */
579 static TUNABLE(uint32_t, zp_scale
, "zp-scale", 4);
582 * Zone caching tunables
585 * size of magazines, larger to reduce contention at the expense of memory
587 * zc_auto_enable_threshold
588 * number of contentions per second after which zone caching engages
594 * numer of contentions per second after which the per-cpu depot layer
595 * grows at each newly observed contention without restriction.
600 * denominator of the fraction of per-cpu depot to migrate to/from
601 * the recirculation depot layer at a time. Default 3 (1/3).
604 * percentage of the working set to recirc size below which
605 * the zone is defragmented. Default is 50%.
608 * The size of batches of frees/reclaim that can be done keeping
609 * the zone lock held (and preemption disabled).
611 static TUNABLE(uint16_t, zc_magazine_size
, "zc_mag_size()", 8);
612 static TUNABLE(uint32_t, zc_auto_threshold
, "zc_auto_enable_threshold", 20);
613 static TUNABLE(uint32_t, zc_grow_threshold
, "zc_grow_threshold", 8);
614 static TUNABLE(uint32_t, zc_recirc_denom
, "zc_recirc_denom", 3);
615 static TUNABLE(uint32_t, zc_defrag_ratio
, "zc_defrag_ratio", 50);
616 static TUNABLE(uint32_t, zc_free_batch_size
, "zc_free_batch_size", 1024);
618 static SECURITY_READ_ONLY_LATE(uintptr_t) zp_canary
;
620 * Perf results for zeroing all non data zones and 2K of data zones
621 * showed little regression, therefore setting zp_min_size to 2048
623 static TUNABLE(uint32_t, zp_min_size
, "zclear_size", 2048);
624 static SECURITY_READ_ONLY_LATE(uint32_t) zone_phys_mapped_max_pages
;
625 static SECURITY_READ_ONLY_LATE(vm_map_t
) zone_submaps
[Z_SUBMAP_IDX_COUNT
];
626 static SECURITY_READ_ONLY_LATE(uint32_t) zone_last_submap_idx
;
628 static zone_t
zone_find_largest(void);
630 #endif /* !ZALLOC_TEST */
631 #pragma mark Zone metadata
634 static inline zone_id_t
637 return (zone_id_t
)(z
- zone_array
);
641 zone_has_index(zone_t z
, zone_id_t zid
)
643 return zone_array
+ zid
== z
;
646 static zone_element_t
647 zone_element_encode(vm_offset_t base
, vm_offset_t eidx
, zprot_mode_t zpm
)
649 return (zone_element_t
){ .ze_value
= base
| (eidx
<< 2) | zpm
};
653 zone_element_base(zone_element_t ze
)
655 return trunc_page(ze
.ze_value
);
659 zone_element_idx(zone_element_t ze
)
661 return (ze
.ze_value
& PAGE_MASK
) >> 2;
664 #if ZALLOC_ENABLE_POISONING
666 zone_element_prot(zone_element_t ze
)
668 return (zprot_mode_t
)(ze
.ze_value
& ZPM_MASK
);
673 zone_element_addr(zone_element_t ze
, vm_offset_t esize
)
675 return zone_element_base(ze
) + esize
* zone_element_idx(ze
);
680 zone_metadata_corruption(zone_t zone
, struct zone_page_metadata
*meta
,
683 panic("zone metadata corruption: %s (meta %p, zone %s%s)",
684 kind
, meta
, zone_heap_name(zone
), zone
->z_name
);
689 zone_invalid_element_addr_panic(zone_t zone
, vm_offset_t addr
)
691 panic("zone element pointer validation failed (addr: %p, zone %s%s)",
692 (void *)addr
, zone_heap_name(zone
), zone
->z_name
);
697 zone_invalid_element_panic(zone_t zone
, zone_element_t ze
)
699 panic("zone element pointer validation failed (elem: %p,%d, zone %s%s)",
700 (void *)zone_element_base(ze
), (int)zone_element_idx(ze
),
701 zone_heap_name(zone
), zone
->z_name
);
706 zone_page_metadata_index_confusion_panic(zone_t zone
, vm_offset_t addr
,
707 struct zone_page_metadata
*meta
)
709 panic("%p not in the expected zone %s%s (%d != %d)",
710 (void *)addr
, zone_heap_name(zone
), zone
->z_name
,
711 meta
->zm_index
, zone_index(zone
));
716 zone_page_metadata_native_queue_corruption(zone_t zone
, zone_pva_t
*queue
)
718 panic("foreign metadata index %d enqueued in native head %p from zone %s%s",
719 queue
->packed_address
, queue
, zone_heap_name(zone
),
725 zone_page_metadata_list_corruption(zone_t zone
, struct zone_page_metadata
*meta
)
727 panic("metadata list corruption through element %p detected in zone %s%s",
728 meta
, zone_heap_name(zone
), zone
->z_name
);
733 zone_invalid_foreign_addr_panic(zone_t zone
, vm_offset_t addr
)
735 panic("addr %p being freed to foreign zone %s%s not from foreign range",
736 (void *)addr
, zone_heap_name(zone
), zone
->z_name
);
741 zone_page_meta_accounting_panic(zone_t zone
, struct zone_page_metadata
*meta
,
744 panic("accounting mismatch (%s) for zone %s%s, meta %p", kind
,
745 zone_heap_name(zone
), zone
->z_name
, meta
);
750 zone_meta_double_free_panic(zone_t zone
, zone_element_t ze
, const char *caller
)
752 panic("%s: double free of %p to zone %s%s", caller
,
753 (void *)zone_element_addr(ze
, zone_elem_size(zone
)),
754 zone_heap_name(zone
), zone
->z_name
);
759 zone_accounting_panic(zone_t zone
, const char *kind
)
761 panic("accounting mismatch (%s) for zone %s%s", kind
,
762 zone_heap_name(zone
), zone
->z_name
);
765 #define zone_counter_sub(z, stat, value) ({ \
766 if (os_sub_overflow((z)->stat, value, &(z)->stat)) { \
767 zone_accounting_panic(z, #stat " wrap-around"); \
773 zone_elems_free_add(zone_t z
, uint32_t count
)
775 uint32_t n
= (z
->z_elems_free
+= count
);
776 if (z
->z_elems_free_max
< n
) {
777 z
->z_elems_free_max
= n
;
782 zone_elems_free_sub(zone_t z
, uint32_t count
)
784 uint32_t n
= zone_counter_sub(z
, z_elems_free
, count
);
786 if (z
->z_elems_free_min
> n
) {
787 z
->z_elems_free_min
= n
;
791 static inline uint16_t
792 zone_meta_alloc_size_add(zone_t z
, struct zone_page_metadata
*m
,
795 if (os_add_overflow(m
->zm_alloc_size
, (uint16_t)esize
, &m
->zm_alloc_size
)) {
796 zone_page_meta_accounting_panic(z
, m
, "alloc_size wrap-around");
798 return m
->zm_alloc_size
;
801 static inline uint16_t
802 zone_meta_alloc_size_sub(zone_t z
, struct zone_page_metadata
*m
,
805 if (os_sub_overflow(m
->zm_alloc_size
, esize
, &m
->zm_alloc_size
)) {
806 zone_page_meta_accounting_panic(z
, m
, "alloc_size wrap-around");
808 return m
->zm_alloc_size
;
813 zone_nofail_panic(zone_t zone
)
815 panic("zalloc(Z_NOFAIL) can't be satisfied for zone %s%s (potential leak)",
816 zone_heap_name(zone
), zone
->z_name
);
820 // <rdar://problem/48304934> arm64 doesn't use ldp when I'd expect it to
821 #define zone_range_load(r, rmin, rmax) \
822 asm("ldp %[rmin], %[rmax], [%[range]]" \
823 : [rmin] "=r"(rmin), [rmax] "=r"(rmax) \
826 #define zone_range_load(r, rmin, rmax) \
827 ({ rmin = (r)->min_address; rmax = (r)->max_address; })
830 __header_always_inline
bool
831 zone_range_contains(const struct zone_map_range
*r
, vm_offset_t addr
, vm_offset_t size
)
833 vm_offset_t rmin
, rmax
;
836 * The `&` is not a typo: we really expect the check to pass,
837 * so encourage the compiler to eagerly load and test without branches
839 zone_range_load(r
, rmin
, rmax
);
840 return (addr
>= rmin
) & (addr
+ size
>= rmin
) & (addr
+ size
<= rmax
);
843 __header_always_inline vm_size_t
844 zone_range_size(const struct zone_map_range
*r
)
846 vm_offset_t rmin
, rmax
;
848 zone_range_load(r
, rmin
, rmax
);
852 #define from_zone_map(addr, size, kind) \
853 zone_range_contains(&zone_info.zi_map_range[kind], \
854 (vm_offset_t)(addr), size)
856 #define zone_native_size() \
857 zone_range_size(&zone_info.zi_map_range[ZONE_ADDR_NATIVE])
859 #define zone_foreign_size() \
860 zone_range_size(&zone_info.zi_map_range[ZONE_ADDR_FOREIGN])
862 __header_always_inline
bool
863 zone_pva_is_null(zone_pva_t page
)
865 return page
.packed_address
== 0;
868 __header_always_inline
bool
869 zone_pva_is_queue(zone_pva_t page
)
871 // actual kernel pages have the top bit set
872 return (int32_t)page
.packed_address
> 0;
875 __header_always_inline
bool
876 zone_pva_is_equal(zone_pva_t pva1
, zone_pva_t pva2
)
878 return pva1
.packed_address
== pva2
.packed_address
;
881 __header_always_inline
void
882 zone_queue_set_head(zone_t z
, zone_pva_t queue
, zone_pva_t oldv
,
883 struct zone_page_metadata
*meta
)
885 zone_pva_t
*queue_head
= &((zone_pva_t
*)zone_array
)[queue
.packed_address
];
887 if (!zone_pva_is_equal(*queue_head
, oldv
)) {
888 zone_page_metadata_list_corruption(z
, meta
);
890 *queue_head
= meta
->zm_page_next
;
893 __header_always_inline zone_pva_t
894 zone_queue_encode(zone_pva_t
*headp
)
896 return (zone_pva_t
){ (uint32_t)(headp
- (zone_pva_t
*)zone_array
) };
899 __header_always_inline zone_pva_t
900 zone_pva_from_addr(vm_address_t addr
)
902 // cannot use atop() because we want to maintain the sign bit
903 return (zone_pva_t
){ (uint32_t)((intptr_t)addr
>> PAGE_SHIFT
) };
906 __header_always_inline zone_pva_t
907 zone_pva_from_element(zone_element_t ze
)
909 return zone_pva_from_addr(ze
.ze_value
);
912 __header_always_inline vm_address_t
913 zone_pva_to_addr(zone_pva_t page
)
915 // cause sign extension so that we end up with the right address
916 return (vm_offset_t
)(int32_t)page
.packed_address
<< PAGE_SHIFT
;
919 __header_always_inline
struct zone_page_metadata
*
920 zone_pva_to_meta(zone_pva_t page
)
922 return &zone_info
.zi_meta_base
[page
.packed_address
];
925 __header_always_inline zone_pva_t
926 zone_pva_from_meta(struct zone_page_metadata
*meta
)
928 return (zone_pva_t
){ (uint32_t)(meta
- zone_info
.zi_meta_base
) };
931 __header_always_inline
struct zone_page_metadata
*
932 zone_meta_from_addr(vm_offset_t addr
)
934 return zone_pva_to_meta(zone_pva_from_addr(addr
));
937 __header_always_inline
struct zone_page_metadata
*
938 zone_meta_from_element(zone_element_t ze
)
940 return zone_pva_to_meta(zone_pva_from_element(ze
));
943 __header_always_inline zone_id_t
944 zone_index_from_ptr(const void *ptr
)
946 return zone_pva_to_meta(zone_pva_from_addr((vm_offset_t
)ptr
))->zm_index
;
949 __header_always_inline vm_offset_t
950 zone_meta_to_addr(struct zone_page_metadata
*meta
)
952 return ptoa((int32_t)(meta
- zone_info
.zi_meta_base
));
955 __header_always_inline
void
956 zone_meta_queue_push(zone_t z
, zone_pva_t
*headp
,
957 struct zone_page_metadata
*meta
)
959 zone_pva_t head
= *headp
;
960 zone_pva_t queue_pva
= zone_queue_encode(headp
);
961 struct zone_page_metadata
*tmp
;
963 meta
->zm_page_next
= head
;
964 if (!zone_pva_is_null(head
)) {
965 tmp
= zone_pva_to_meta(head
);
966 if (!zone_pva_is_equal(tmp
->zm_page_prev
, queue_pva
)) {
967 zone_page_metadata_list_corruption(z
, meta
);
969 tmp
->zm_page_prev
= zone_pva_from_meta(meta
);
971 meta
->zm_page_prev
= queue_pva
;
972 *headp
= zone_pva_from_meta(meta
);
975 __header_always_inline
struct zone_page_metadata
*
976 zone_meta_queue_pop_native(zone_t z
, zone_pva_t
*headp
, vm_offset_t
*page_addrp
)
978 zone_pva_t head
= *headp
;
979 struct zone_page_metadata
*meta
= zone_pva_to_meta(head
);
980 vm_offset_t page_addr
= zone_pva_to_addr(head
);
981 struct zone_page_metadata
*tmp
;
983 if (!from_zone_map(page_addr
, 1, ZONE_ADDR_NATIVE
)) {
984 zone_page_metadata_native_queue_corruption(z
, headp
);
987 if (!zone_pva_is_null(meta
->zm_page_next
)) {
988 tmp
= zone_pva_to_meta(meta
->zm_page_next
);
989 if (!zone_pva_is_equal(tmp
->zm_page_prev
, head
)) {
990 zone_page_metadata_list_corruption(z
, meta
);
992 tmp
->zm_page_prev
= meta
->zm_page_prev
;
994 *headp
= meta
->zm_page_next
;
996 meta
->zm_page_next
= meta
->zm_page_prev
= (zone_pva_t
){ 0 };
997 *page_addrp
= page_addr
;
999 if (!zone_has_index(z
, meta
->zm_index
)) {
1000 zone_page_metadata_index_confusion_panic(z
,
1001 zone_meta_to_addr(meta
), meta
);
1006 __header_always_inline
void
1007 zone_meta_remqueue(zone_t z
, struct zone_page_metadata
*meta
)
1009 zone_pva_t meta_pva
= zone_pva_from_meta(meta
);
1010 struct zone_page_metadata
*tmp
;
1012 if (!zone_pva_is_null(meta
->zm_page_next
)) {
1013 tmp
= zone_pva_to_meta(meta
->zm_page_next
);
1014 if (!zone_pva_is_equal(tmp
->zm_page_prev
, meta_pva
)) {
1015 zone_page_metadata_list_corruption(z
, meta
);
1017 tmp
->zm_page_prev
= meta
->zm_page_prev
;
1019 if (zone_pva_is_queue(meta
->zm_page_prev
)) {
1020 zone_queue_set_head(z
, meta
->zm_page_prev
, meta_pva
, meta
);
1022 tmp
= zone_pva_to_meta(meta
->zm_page_prev
);
1023 if (!zone_pva_is_equal(tmp
->zm_page_next
, meta_pva
)) {
1024 zone_page_metadata_list_corruption(z
, meta
);
1026 tmp
->zm_page_next
= meta
->zm_page_next
;
1029 meta
->zm_page_next
= meta
->zm_page_prev
= (zone_pva_t
){ 0 };
1032 __header_always_inline
void
1033 zone_meta_requeue(zone_t z
, zone_pva_t
*headp
,
1034 struct zone_page_metadata
*meta
)
1036 zone_meta_remqueue(z
, meta
);
1037 zone_meta_queue_push(z
, headp
, meta
);
1040 /* prevents a given metadata from ever reaching the z_pageq_empty queue */
1042 zone_meta_lock_in_partial(zone_t z
, struct zone_page_metadata
*m
, uint32_t len
)
1044 uint16_t new_size
= zone_meta_alloc_size_add(z
, m
, ZM_ALLOC_SIZE_LOCK
);
1046 assert(new_size
% sizeof(vm_offset_t
) == ZM_ALLOC_SIZE_LOCK
);
1047 if (new_size
== ZM_ALLOC_SIZE_LOCK
) {
1048 zone_meta_requeue(z
, &z
->z_pageq_partial
, m
);
1049 zone_counter_sub(z
, z_wired_empty
, len
);
1053 /* allows a given metadata to reach the z_pageq_empty queue again */
1055 zone_meta_unlock_from_partial(zone_t z
, struct zone_page_metadata
*m
, uint32_t len
)
1057 uint16_t new_size
= zone_meta_alloc_size_sub(z
, m
, ZM_ALLOC_SIZE_LOCK
);
1059 assert(new_size
% sizeof(vm_offset_t
) == 0);
1060 if (new_size
== 0) {
1061 zone_meta_requeue(z
, &z
->z_pageq_empty
, m
);
1062 z
->z_wired_empty
+= len
;
1067 * Routine to populate a page backing metadata in the zone_metadata_region.
1068 * Must be called without the zone lock held as it might potentially block.
1071 zone_meta_populate(vm_offset_t base
, vm_size_t size
)
1073 struct zone_page_metadata
*from
= zone_meta_from_addr(base
);
1074 struct zone_page_metadata
*to
= from
+ atop(size
);
1075 vm_offset_t page_addr
= trunc_page(from
);
1077 for (; page_addr
< (vm_offset_t
)to
; page_addr
+= PAGE_SIZE
) {
1080 * This can race with another thread doing a populate on the same metadata
1081 * page, where we see an updated pmap but unmapped KASan shadow, causing a
1082 * fault in the shadow when we first access the metadata page. Avoid this
1083 * by always synchronizing on the zone_metadata_region lock with KASan.
1085 if (pmap_find_phys(kernel_pmap
, page_addr
)) {
1091 kern_return_t ret
= KERN_SUCCESS
;
1093 /* All updates to the zone_metadata_region are done under the zone_metadata_region_lck */
1094 lck_mtx_lock(&zone_metadata_region_lck
);
1095 if (0 == pmap_find_phys(kernel_pmap
, page_addr
)) {
1096 ret
= kernel_memory_populate(kernel_map
, page_addr
,
1097 PAGE_SIZE
, KMA_NOPAGEWAIT
| KMA_KOBJECT
| KMA_ZERO
,
1098 VM_KERN_MEMORY_OSFMK
);
1100 lck_mtx_unlock(&zone_metadata_region_lck
);
1102 if (ret
== KERN_SUCCESS
) {
1107 * We can't pass KMA_NOPAGEWAIT under a global lock as it leads
1108 * to bad system deadlocks, so if the allocation failed,
1109 * we need to do the VM_PAGE_WAIT() outside of the lock.
1116 __header_always_inline
1117 struct zone_page_metadata
*
1118 zone_element_validate(zone_t zone
, zone_element_t ze
)
1120 struct zone_page_metadata
*meta
;
1121 vm_offset_t page
= zone_element_base(ze
);
1123 if (!from_zone_map(page
, 1, ZONE_ADDR_NATIVE
) &&
1124 !from_zone_map(page
, 1, ZONE_ADDR_FOREIGN
)) {
1125 zone_invalid_element_panic(zone
, ze
);
1127 meta
= zone_meta_from_addr(page
);
1129 if (meta
->zm_chunk_len
> ZM_CHUNK_LEN_MAX
) {
1130 zone_invalid_element_panic(zone
, ze
);
1132 if (zone_element_idx(ze
) >= zone
->z_chunk_elems
) {
1133 zone_invalid_element_panic(zone
, ze
);
1136 if (!zone_has_index(zone
, meta
->zm_index
)) {
1137 vm_offset_t addr
= zone_element_addr(ze
, zone_elem_size(zone
));
1138 zone_page_metadata_index_confusion_panic(zone
, addr
, meta
);
1144 __attribute__((always_inline
))
1145 static struct zone_page_metadata
*
1146 zone_element_resolve(zone_t zone
, vm_offset_t addr
, vm_offset_t esize
,
1149 struct zone_page_metadata
*meta
;
1150 vm_offset_t page
, eidx
;
1152 if (!from_zone_map(addr
, esize
, ZONE_ADDR_NATIVE
) &&
1153 !from_zone_map(addr
, esize
, ZONE_ADDR_FOREIGN
)) {
1154 zone_invalid_element_addr_panic(zone
, addr
);
1156 page
= trunc_page(addr
);
1157 meta
= zone_meta_from_addr(addr
);
1159 if (meta
->zm_chunk_len
== ZM_SECONDARY_PCPU_PAGE
) {
1160 zone_invalid_element_addr_panic(zone
, addr
);
1162 if (meta
->zm_chunk_len
== ZM_SECONDARY_PAGE
) {
1163 page
-= ptoa(meta
->zm_page_index
);
1164 meta
-= meta
->zm_page_index
;
1167 eidx
= (addr
- page
) / esize
;
1168 if ((addr
- page
) % esize
) {
1169 zone_invalid_element_addr_panic(zone
, addr
);
1172 if (!zone_has_index(zone
, meta
->zm_index
)) {
1173 zone_page_metadata_index_confusion_panic(zone
, addr
, meta
);
1176 *ze
= zone_element_encode(page
, eidx
, ZPM_AUTO
);
1180 /* Routine to get the size of a zone allocated address.
1181 * If the address doesnt belong to the zone maps, returns 0.
1184 zone_element_size(void *addr
, zone_t
*z
)
1186 struct zone
*src_zone
;
1188 if (from_zone_map(addr
, sizeof(void *), ZONE_ADDR_NATIVE
) ||
1189 from_zone_map(addr
, sizeof(void *), ZONE_ADDR_FOREIGN
)) {
1190 src_zone
= &zone_array
[zone_index_from_ptr(addr
)];
1194 return zone_elem_size(src_zone
);
1198 if (__improbable(gzalloc_enabled())) {
1200 if (gzalloc_element_size(addr
, z
, &gzsize
)) {
1204 #endif /* CONFIG_GZALLOC */
1209 /* This function just formats the reason for the panics by redoing the checks */
1212 zone_require_panic(zone_t zone
, void *addr
)
1217 if (!from_zone_map(addr
, zone_elem_size(zone
), ZONE_ADDR_NATIVE
)) {
1218 panic("zone_require failed: address not in a zone (addr: %p)", addr
);
1221 zindex
= zone_index_from_ptr(addr
);
1222 other
= &zone_array
[zindex
];
1223 if (zindex
>= os_atomic_load(&num_zones
, relaxed
) || !other
->z_self
) {
1224 panic("zone_require failed: invalid zone index %d "
1225 "(addr: %p, expected: %s%s)", zindex
,
1226 addr
, zone_heap_name(zone
), zone
->z_name
);
1228 panic("zone_require failed: address in unexpected zone id %d (%s%s) "
1229 "(addr: %p, expected: %s%s)",
1230 zindex
, zone_heap_name(other
), other
->z_name
,
1231 addr
, zone_heap_name(zone
), zone
->z_name
);
1237 zone_id_require_panic(zone_id_t zid
, void *addr
)
1239 zone_require_panic(&zone_array
[zid
], addr
);
1243 * Routines to panic if a pointer is not mapped to an expected zone.
1244 * This can be used as a means of pinning an object to the zone it is expected
1245 * to be a part of. Causes a panic if the address does not belong to any
1246 * specified zone, does not belong to any zone, has been freed and therefore
1247 * unmapped from the zone, or the pointer contains an uninitialized value that
1248 * does not belong to any zone.
1250 * Note that this can only work with collectable zones without foreign pages.
1253 zone_require(zone_t zone
, void *addr
)
1255 vm_size_t esize
= zone_elem_size(zone
);
1257 if (__probable(from_zone_map(addr
, esize
, ZONE_ADDR_NATIVE
))) {
1258 if (zone_has_index(zone
, zone_index_from_ptr(addr
))) {
1262 } else if (__probable(zone
->gzalloc_tracked
)) {
1266 zone_require_panic(zone
, addr
);
1270 zone_id_require(zone_id_t zid
, vm_size_t esize
, void *addr
)
1272 if (__probable(from_zone_map(addr
, esize
, ZONE_ADDR_NATIVE
))) {
1273 if (zid
== zone_index_from_ptr(addr
)) {
1277 } else if (__probable(zone_array
[zid
].gzalloc_tracked
)) {
1281 zone_id_require_panic(zid
, addr
);
1285 zone_id_require_allow_foreign(zone_id_t zid
, vm_size_t esize
, void *addr
)
1287 if (__probable(from_zone_map(addr
, esize
, ZONE_ADDR_NATIVE
) ||
1288 from_zone_map(addr
, esize
, ZONE_ADDR_FOREIGN
))) {
1289 if (zid
== zone_index_from_ptr(addr
)) {
1293 } else if (__probable(zone_array
[zid
].gzalloc_tracked
)) {
1297 zone_id_require_panic(zid
, addr
);
1301 zone_owns(zone_t zone
, void *addr
)
1303 vm_size_t esize
= zone_elem_size(zone
);
1305 if (__probable(from_zone_map(addr
, esize
, ZONE_ADDR_NATIVE
))) {
1306 return zone_has_index(zone
, zone_index_from_ptr(addr
));
1308 } else if (__probable(zone
->gzalloc_tracked
)) {
1315 #endif /* !ZALLOC_TEST */
1316 #pragma mark Zone bits allocator
1319 * @defgroup Zone Bitmap allocator
1323 * Functions implementing the zone bitmap allocator
1326 * The zone allocator maintains which elements are allocated or free in bitmaps.
1328 * When the number of elements per page is smaller than 32, it is stored inline
1329 * on the @c zone_page_metadata structure (@c zm_inline_bitmap is set,
1330 * and @c zm_bitmap used for storage).
1332 * When the number of elements is larger, then a bitmap is allocated from
1333 * a buddy allocator (impelemented under the @c zba_* namespace). Pointers
1334 * to bitmaps are implemented as a packed 32 bit bitmap reference, stored in
1335 * @c zm_bitmap. The low 3 bits encode the scale (order) of the allocation in
1336 * @c ZBA_GRANULE units, and hence actual allocations encoded with that scheme
1337 * cannot be larger than 1024 bytes (8192 bits).
1339 * This buddy allocator can actually accomodate allocations as large
1340 * as 8k on 16k systems and 2k on 4k systems.
1342 * Note: @c zba_* functions are implementation details not meant to be used
1343 * outside of the allocation of the allocator itself. Interfaces to the rest of
1344 * the zone allocator are documented and not @c zba_* prefixed.
1347 #define ZBA_CHUNK_SIZE PAGE_MAX_SIZE
1348 #define ZBA_GRANULE sizeof(uint64_t)
1349 #define ZBA_GRANULE_BITS (8 * sizeof(uint64_t))
1350 #define ZBA_MAX_ORDER (PAGE_MAX_SHIFT - 4)
1351 #define ZBA_MAX_ALLOC_ORDER 7
1352 #define ZBA_SLOTS (ZBA_CHUNK_SIZE / ZBA_GRANULE)
1353 static_assert(2ul * ZBA_GRANULE
<< ZBA_MAX_ORDER
== ZBA_CHUNK_SIZE
, "chunk sizes");
1354 static_assert(ZBA_MAX_ALLOC_ORDER
<= ZBA_MAX_ORDER
, "ZBA_MAX_ORDER is enough");
1356 struct zone_bits_chain
{
1359 } __attribute__((aligned(ZBA_GRANULE
)));
1361 struct zone_bits_head
{
1363 uint32_t zbh_unused
;
1364 } __attribute__((aligned(ZBA_GRANULE
)));
1366 static_assert(sizeof(struct zone_bits_chain
) == ZBA_GRANULE
, "zbc size");
1367 static_assert(sizeof(struct zone_bits_head
) == ZBA_GRANULE
, "zbh size");
1369 struct zone_bits_allocator_meta
{
1370 uint32_t zbam_chunks
;
1371 uint32_t __zbam_padding
;
1372 struct zone_bits_head zbam_lists
[ZBA_MAX_ORDER
+ 1];
1375 struct zone_bits_allocator_header
{
1376 uint64_t zbah_bits
[ZBA_SLOTS
/ (8 * sizeof(uint64_t))];
1380 static struct zalloc_bits_allocator_test_setup
{
1381 vm_offset_t zbats_base
;
1382 void (*zbats_populate
)(vm_address_t addr
, vm_size_t size
);
1385 static struct zone_bits_allocator_header
*
1386 zba_base_header(void)
1388 return (struct zone_bits_allocator_header
*)zba_test_info
.zbats_base
;
1392 zba_populate(uint32_t n
)
1394 vm_address_t base
= zba_test_info
.zbats_base
;
1395 zba_test_info
.zbats_populate(base
+ n
* ZBA_CHUNK_SIZE
, ZBA_CHUNK_SIZE
);
1399 static uint8_t zba_chunk_startup
[ZBA_CHUNK_SIZE
]
1400 __attribute__((aligned(ZBA_CHUNK_SIZE
)));
1401 static LCK_MTX_EARLY_DECLARE(zba_mtx
, &zone_locks_grp
);
1403 static struct zone_bits_allocator_header
*
1404 zba_base_header(void)
1406 return (struct zone_bits_allocator_header
*)zone_info
.zi_bits_range
.min_address
;
1412 lck_mtx_lock(&zba_mtx
);
1418 lck_mtx_unlock(&zba_mtx
);
1422 zba_populate(uint32_t n
)
1424 vm_size_t size
= ZBA_CHUNK_SIZE
;
1427 addr
= zone_info
.zi_bits_range
.min_address
+ n
* size
;
1428 if (addr
>= zone_info
.zi_bits_range
.max_address
) {
1429 zone_t z
= zone_find_largest();
1430 panic("zba_populate: out of bitmap space, "
1431 "likely due to memory leak in zone [%s%s] "
1432 "(%luM, %d elements allocated)",
1433 zone_heap_name(z
), zone_name(z
),
1434 (unsigned long)zone_size_wired(z
) >> 20,
1435 zone_count_allocated(z
));
1439 kern_return_t kr
= KERN_SUCCESS
;
1441 if (0 == pmap_find_phys(kernel_pmap
, addr
)) {
1442 kr
= kernel_memory_populate(kernel_map
, addr
, size
,
1443 KMA_NOPAGEWAIT
| KMA_KOBJECT
| KMA_ZERO
,
1444 VM_KERN_MEMORY_OSFMK
);
1447 if (kr
== KERN_SUCCESS
) {
1459 static struct zone_bits_allocator_meta
*
1462 return (struct zone_bits_allocator_meta
*)&zba_base_header()[1];
1469 return (uint64_t *)zba_base_header();
1474 zba_page_addr(uint32_t n
)
1476 return (vm_address_t
)zba_base_header() + n
* ZBA_CHUNK_SIZE
;
1480 static struct zone_bits_head
*
1481 zba_head(uint32_t order
)
1483 return &zba_meta()->zbam_lists
[order
];
1488 zba_head_index(uint32_t order
)
1490 uint32_t hdr_size
= sizeof(struct zone_bits_allocator_header
) +
1491 offsetof(struct zone_bits_allocator_meta
, zbam_lists
);
1492 return (hdr_size
/ ZBA_GRANULE
) + order
;
1496 static struct zone_bits_chain
*
1497 zba_chain_for_index(uint32_t index
)
1499 return (struct zone_bits_chain
*)(zba_slot_base() + index
);
1504 zba_chain_to_index(const struct zone_bits_chain
*zbc
)
1506 return (uint32_t)((const uint64_t *)zbc
- zba_slot_base());
1511 zba_head_corruption_panic(uint32_t order
)
1513 panic("zone bits allocator head[%d:%p] is corrupt", order
,
1519 zba_chain_corruption_panic(struct zone_bits_chain
*a
, struct zone_bits_chain
*b
)
1521 panic("zone bits allocator freelist is corrupt (%p <-> %p)", a
, b
);
1525 zba_push_block(struct zone_bits_chain
*zbc
, uint32_t order
)
1527 struct zone_bits_head
*hd
= zba_head(order
);
1528 uint32_t hd_index
= zba_head_index(order
);
1529 uint32_t index
= zba_chain_to_index(zbc
);
1530 struct zone_bits_chain
*next
;
1533 next
= zba_chain_for_index(hd
->zbh_next
);
1534 if (next
->zbc_prev
!= hd_index
) {
1535 zba_head_corruption_panic(order
);
1537 next
->zbc_prev
= index
;
1539 zbc
->zbc_next
= hd
->zbh_next
;
1540 zbc
->zbc_prev
= hd_index
;
1541 hd
->zbh_next
= index
;
1545 zba_remove_block(struct zone_bits_chain
*zbc
)
1547 struct zone_bits_chain
*prev
= zba_chain_for_index(zbc
->zbc_prev
);
1548 uint32_t index
= zba_chain_to_index(zbc
);
1550 if (prev
->zbc_next
!= index
) {
1551 zba_chain_corruption_panic(prev
, zbc
);
1553 if ((prev
->zbc_next
= zbc
->zbc_next
)) {
1554 struct zone_bits_chain
*next
= zba_chain_for_index(zbc
->zbc_next
);
1555 if (next
->zbc_prev
!= index
) {
1556 zba_chain_corruption_panic(zbc
, next
);
1558 next
->zbc_prev
= zbc
->zbc_prev
;
1563 zba_try_pop_block(uint32_t order
)
1565 struct zone_bits_head
*hd
= zba_head(order
);
1566 struct zone_bits_chain
*zbc
;
1568 if (hd
->zbh_next
== 0) {
1572 zbc
= zba_chain_for_index(hd
->zbh_next
);
1573 zba_remove_block(zbc
);
1574 return (vm_address_t
)zbc
;
1577 static struct zone_bits_allocator_header
*
1578 zba_header(vm_offset_t addr
)
1580 addr
&= -(vm_offset_t
)ZBA_CHUNK_SIZE
;
1581 return (struct zone_bits_allocator_header
*)addr
;
1585 zba_node_parent(size_t node
)
1587 return (node
- 1) / 2;
1591 zba_node_left_child(size_t node
)
1593 return node
* 2 + 1;
1597 zba_node_buddy(size_t node
)
1599 return ((node
- 1) ^ 1) + 1;
1603 zba_node(vm_offset_t addr
, uint32_t order
)
1605 vm_offset_t offs
= (addr
% ZBA_CHUNK_SIZE
) / ZBA_GRANULE
;
1606 return (offs
>> order
) + (1 << (ZBA_MAX_ORDER
- order
+ 1)) - 1;
1609 static struct zone_bits_chain
*
1610 zba_chain_for_node(struct zone_bits_allocator_header
*zbah
, size_t node
, uint32_t order
)
1612 vm_offset_t offs
= (node
- (1 << (ZBA_MAX_ORDER
- order
+ 1)) + 1) << order
;
1613 return (struct zone_bits_chain
*)((vm_offset_t
)zbah
+ offs
* ZBA_GRANULE
);
1617 zba_node_flip_split(struct zone_bits_allocator_header
*zbah
, size_t node
)
1619 zbah
->zbah_bits
[node
/ 64] ^= 1ull << (node
% 64);
1623 zba_node_is_split(struct zone_bits_allocator_header
*zbah
, size_t node
)
1625 return zbah
->zbah_bits
[node
/ 64] & (1ull << (node
% 64));
1629 zba_free(vm_offset_t addr
, uint32_t order
)
1631 struct zone_bits_allocator_header
*zbah
= zba_header(addr
);
1632 struct zone_bits_chain
*zbc
;
1633 size_t node
= zba_node(addr
, order
);
1636 size_t parent
= zba_node_parent(node
);
1638 zba_node_flip_split(zbah
, parent
);
1639 if (zba_node_is_split(zbah
, parent
)) {
1643 zbc
= zba_chain_for_node(zbah
, zba_node_buddy(node
), order
);
1644 zba_remove_block(zbc
);
1649 zba_push_block(zba_chain_for_node(zbah
, node
, order
), order
);
1653 zba_chunk_header_size(uint32_t n
)
1655 vm_size_t hdr_size
= sizeof(struct zone_bits_allocator_header
);
1657 hdr_size
+= sizeof(struct zone_bits_allocator_meta
);
1663 zba_init_chunk(uint32_t n
)
1665 vm_size_t hdr_size
= zba_chunk_header_size(n
);
1666 vm_offset_t page
= zba_page_addr(n
);
1667 struct zone_bits_allocator_header
*zbah
= zba_header(page
);
1668 vm_size_t size
= ZBA_CHUNK_SIZE
;
1671 for (uint32_t o
= ZBA_MAX_ORDER
+ 1; o
-- > 0;) {
1672 if (size
< hdr_size
+ (ZBA_GRANULE
<< o
)) {
1675 size
-= ZBA_GRANULE
<< o
;
1676 node
= zba_node(page
+ size
, o
);
1677 zba_node_flip_split(zbah
, zba_node_parent(node
));
1678 zba_push_block(zba_chain_for_node(zbah
, node
, o
), o
);
1681 zba_meta()->zbam_chunks
= n
+ 1;
1684 __attribute__((noinline
))
1688 uint32_t chunk
= zba_meta()->zbam_chunks
;
1690 zba_populate(chunk
);
1691 if (zba_meta()->zbam_chunks
== chunk
) {
1692 zba_init_chunk(chunk
);
1697 zba_alloc(uint32_t order
)
1699 struct zone_bits_allocator_header
*zbah
;
1700 uint32_t cur
= order
;
1704 while ((addr
= zba_try_pop_block(cur
)) == 0) {
1705 if (cur
++ >= ZBA_MAX_ORDER
) {
1711 zbah
= zba_header(addr
);
1712 node
= zba_node(addr
, cur
);
1713 zba_node_flip_split(zbah
, zba_node_parent(node
));
1714 while (cur
> order
) {
1716 zba_node_flip_split(zbah
, node
);
1717 node
= zba_node_left_child(node
);
1718 zba_push_block(zba_chain_for_node(zbah
, node
+ 1, cur
), cur
);
1724 #define zba_map_index(type, n) (n / (8 * sizeof(type)))
1725 #define zba_map_bit(type, n) ((type)1 << (n % (8 * sizeof(type))))
1726 #define zba_map_mask_lt(type, n) (zba_map_bit(type, n) - 1)
1727 #define zba_map_mask_ge(type, n) ((type)-zba_map_bit(type, n))
1731 zba_bits_ref_order(uint32_t bref
)
1737 zba_bits_ref_ptr(uint32_t bref
)
1739 return zba_slot_base() + (bref
>> 3);
1743 zba_scan_bitmap_inline(zone_t zone
, struct zone_page_metadata
*meta
,
1746 size_t i
= eidx
/ 32;
1750 map
= meta
[i
].zm_bitmap
& zba_map_mask_ge(uint32_t, eidx
);
1752 eidx
= __builtin_ctz(map
);
1753 meta
[i
].zm_bitmap
^= 1u << eidx
;
1754 return i
* 32 + eidx
;
1759 uint32_t chunk_len
= meta
->zm_chunk_len
;
1760 if (chunk_len
== 1 && zone
->z_percpu
) {
1761 chunk_len
= zpercpu_count();
1763 for (int j
= 0; j
< chunk_len
; j
++, i
++) {
1764 if (i
>= chunk_len
) {
1767 if (__probable(map
= meta
[i
].zm_bitmap
)) {
1768 meta
[i
].zm_bitmap
&= map
- 1;
1769 return i
* 32 + __builtin_ctz(map
);
1773 zone_page_meta_accounting_panic(zone
, meta
, "zm_bitmap");
1777 zba_scan_bitmap_ref(zone_t zone
, struct zone_page_metadata
*meta
,
1780 uint32_t bits_size
= 1 << zba_bits_ref_order(meta
->zm_bitmap
);
1781 bitmap_t
*bits
= zba_bits_ref_ptr(meta
->zm_bitmap
);
1782 size_t i
= eidx
/ 64;
1786 map
= bits
[i
] & zba_map_mask_ge(uint64_t, eidx
);
1788 eidx
= __builtin_ctzll(map
);
1789 bits
[i
] ^= 1ull << eidx
;
1790 return i
* 64 + eidx
;
1795 for (int j
= 0; j
< bits_size
; i
++, j
++) {
1796 if (i
>= bits_size
) {
1799 if (__probable(map
= bits
[i
])) {
1801 return i
* 64 + __builtin_ctzll(map
);
1805 zone_page_meta_accounting_panic(zone
, meta
, "zm_bitmap");
1809 * @function zone_meta_find_and_clear_bit
1812 * The core of the bitmap allocator: find a bit set in the bitmaps.
1815 * This method will round robin through available allocations,
1816 * with a per-core memory of the last allocated element index allocated.
1818 * This is done in order to avoid a fully LIFO behavior which makes exploiting
1819 * double-free bugs way too practical.
1821 * @param zone The zone we're allocating from.
1822 * @param meta The main metadata for the chunk being allocated from.
1825 zone_meta_find_and_clear_bit(zone_t zone
, struct zone_page_metadata
*meta
)
1827 zone_stats_t zs
= zpercpu_get(zone
->z_stats
);
1828 vm_offset_t eidx
= zs
->zs_alloc_rr
+ 1;
1830 if (meta
->zm_inline_bitmap
) {
1831 eidx
= zba_scan_bitmap_inline(zone
, meta
, eidx
);
1833 eidx
= zba_scan_bitmap_ref(zone
, meta
, eidx
);
1835 zs
->zs_alloc_rr
= (uint16_t)eidx
;
1840 * @function zone_meta_bits_init
1843 * Initializes the zm_bitmap field(s) for a newly assigned chunk.
1845 * @param meta The main metadata for the initialized chunk.
1846 * @param count The number of elements the chunk can hold
1847 * (which might be partial for partially populated chunks).
1848 * @param nbits The maximum nuber of bits that will be used.
1851 zone_meta_bits_init(struct zone_page_metadata
*meta
,
1852 uint32_t count
, uint32_t nbits
)
1854 static_assert(ZONE_MAX_ALLOC_SIZE
/ ZONE_MIN_ELEM_SIZE
<=
1855 ZBA_GRANULE_BITS
<< ZBA_MAX_ORDER
, "bitmaps will be large enough");
1857 if (meta
->zm_inline_bitmap
) {
1859 * We're called with the metadata zm_bitmap fields already
1862 for (size_t i
= 0; 32 * i
< count
; i
++) {
1863 if (32 * i
+ 32 <= count
) {
1864 meta
[i
].zm_bitmap
= ~0u;
1866 meta
[i
].zm_bitmap
= zba_map_mask_lt(uint32_t, count
);
1870 uint32_t order
= flsll((nbits
- 1) / ZBA_GRANULE_BITS
);
1873 assert(order
<= ZBA_MAX_ALLOC_ORDER
);
1874 assert(count
<= ZBA_GRANULE_BITS
<< order
);
1877 bits
= (uint64_t *)zba_alloc(order
);
1880 for (size_t i
= 0; i
< 1u << order
; i
++) {
1881 if (64 * i
+ 64 <= count
) {
1883 } else if (64 * i
< count
) {
1884 bits
[i
] = zba_map_mask_lt(uint64_t, count
);
1890 meta
->zm_bitmap
= (uint32_t)((vm_offset_t
)bits
-
1891 (vm_offset_t
)zba_slot_base()) + order
;
1896 * @function zone_meta_bits_merge
1899 * Adds elements <code>[start, end)</code> to a chunk being extended.
1901 * @param meta The main metadata for the extended chunk.
1902 * @param start The index of the first element to add to the chunk.
1903 * @param end The index of the last (exclusive) element to add.
1906 zone_meta_bits_merge(struct zone_page_metadata
*meta
,
1907 uint32_t start
, uint32_t end
)
1909 if (meta
->zm_inline_bitmap
) {
1910 while (start
< end
) {
1911 size_t s_i
= start
/ 32;
1912 size_t s_e
= end
/ 32;
1915 meta
[s_i
].zm_bitmap
|= zba_map_mask_lt(uint32_t, end
) &
1916 zba_map_mask_ge(uint32_t, start
);
1920 meta
[s_i
].zm_bitmap
|= zba_map_mask_ge(uint32_t, start
);
1921 start
+= 32 - (start
% 32);
1924 uint64_t *bits
= zba_bits_ref_ptr(meta
->zm_bitmap
);
1926 while (start
< end
) {
1927 size_t s_i
= start
/ 64;
1928 size_t s_e
= end
/ 64;
1931 bits
[s_i
] |= zba_map_mask_lt(uint64_t, end
) &
1932 zba_map_mask_ge(uint64_t, start
);
1935 bits
[s_i
] |= zba_map_mask_ge(uint64_t, start
);
1936 start
+= 64 - (start
% 64);
1942 * @function zone_bits_free
1945 * Frees a bitmap to the zone bitmap allocator.
1948 * A bitmap reference set by @c zone_meta_bits_init() in a @c zm_bitmap field.
1951 zone_bits_free(uint32_t bref
)
1954 zba_free((vm_offset_t
)zba_bits_ref_ptr(bref
), zba_bits_ref_order(bref
));
1959 * @function zone_meta_is_free
1962 * Returns whether a given element appears free.
1965 zone_meta_is_free(struct zone_page_metadata
*meta
, zone_element_t ze
)
1967 vm_offset_t eidx
= zone_element_idx(ze
);
1968 if (meta
->zm_inline_bitmap
) {
1969 uint32_t bit
= zba_map_bit(uint32_t, eidx
);
1970 return meta
[zba_map_index(uint32_t, eidx
)].zm_bitmap
& bit
;
1972 bitmap_t
*bits
= zba_bits_ref_ptr(meta
->zm_bitmap
);
1973 uint64_t bit
= zba_map_bit(uint64_t, eidx
);
1974 return bits
[zba_map_index(uint64_t, eidx
)] & bit
;
1979 * @function zone_meta_mark_free
1982 * Marks an element as free and returns whether it was marked as used.
1985 zone_meta_mark_free(struct zone_page_metadata
*meta
, zone_element_t ze
)
1987 vm_offset_t eidx
= zone_element_idx(ze
);
1989 if (meta
->zm_inline_bitmap
) {
1990 uint32_t bit
= zba_map_bit(uint32_t, eidx
);
1991 if (meta
[zba_map_index(uint32_t, eidx
)].zm_bitmap
& bit
) {
1994 meta
[zba_map_index(uint32_t, eidx
)].zm_bitmap
^= bit
;
1996 bitmap_t
*bits
= zba_bits_ref_ptr(meta
->zm_bitmap
);
1997 uint64_t bit
= zba_map_bit(uint64_t, eidx
);
1998 if (bits
[zba_map_index(uint64_t, eidx
)] & bit
) {
2001 bits
[zba_map_index(uint64_t, eidx
)] ^= bit
;
2007 * @function zone_meta_mark_used
2010 * Marks an element as used and returns whether it was marked as free
2013 zone_meta_mark_used(struct zone_page_metadata
*meta
, zone_element_t ze
)
2015 vm_offset_t eidx
= zone_element_idx(ze
);
2017 if (meta
->zm_inline_bitmap
) {
2018 uint32_t bit
= zba_map_bit(uint32_t, eidx
);
2019 if (meta
[zba_map_index(uint32_t, eidx
)].zm_bitmap
& bit
) {
2020 meta
[zba_map_index(uint32_t, eidx
)].zm_bitmap
^= bit
;
2024 bitmap_t
*bits
= zba_bits_ref_ptr(meta
->zm_bitmap
);
2025 uint64_t bit
= zba_map_bit(uint64_t, eidx
);
2026 if (bits
[zba_map_index(uint64_t, eidx
)] & bit
) {
2027 bits
[zba_map_index(uint64_t, eidx
)] ^= bit
;
2034 #endif /* !ZALLOC_TEST */
2038 #if VM_MAX_TAG_ZONES
2040 * Zone tagging allows for per "tag" accounting of allocations for the kalloc
2043 * There are 3 kinds of tags that can be used:
2044 * - pre-registered VM_KERN_MEMORY_*
2045 * - dynamic tags allocated per call sites in core-kernel (using vm_tag_alloc())
2046 * - per-kext tags computed by IOKit (using the magic VM_TAG_BT marker).
2048 * The VM tracks the statistics in lazily allocated structures.
2049 * See vm_tag_will_update_zone(), vm_tag_update_zone_size().
2051 * If for some reason the requested tag cannot be accounted for,
2052 * the tag is forced to VM_KERN_MEMORY_KALLOC which is pre-allocated.
2054 * Each allocated element also remembers the tag it was assigned,
2055 * in its ztSlot() which lets zalloc/zfree update statistics correctly.
2058 // for zones with tagging enabled:
2060 // calculate a pointer to the tag base entry,
2061 // holding either a uint32_t the first tag offset for a page in the zone map,
2062 // or two uint16_t tags if the page can only hold one or two elements
2064 #define ZTAGBASE(zone, element) \
2065 (&((uint32_t *)zone_tagbase_min)[atop((element) - \
2066 zone_info.zi_map_range[ZONE_ADDR_NATIVE].min_address)])
2068 static vm_offset_t zone_tagbase_min
;
2069 static vm_offset_t zone_tagbase_max
;
2070 static vm_offset_t zone_tagbase_map_size
;
2071 static vm_map_t zone_tagbase_map
;
2073 static vm_offset_t zone_tags_min
;
2074 static vm_offset_t zone_tags_max
;
2075 static vm_offset_t zone_tags_map_size
;
2076 static vm_map_t zone_tags_map
;
2078 // simple heap allocator for allocating the tags for new memory
2080 static LCK_MTX_EARLY_DECLARE(ztLock
, &zone_locks_grp
); /* heap lock */
2083 ztFreeIndexCount
= 8,
2084 ztFreeIndexMax
= (ztFreeIndexCount
- 1),
2089 #if __LITTLE_ENDIAN__
2095 // ztBlock needs free bit least significant
2096 #error !__LITTLE_ENDIAN__
2099 typedef struct ztBlock ztBlock
;
2101 static ztBlock
* ztBlocks
;
2102 static uint32_t ztBlocksCount
;
2103 static uint32_t ztBlocksFree
;
2106 ztLog2up(uint32_t size
)
2111 size
= 32 - __builtin_clz(size
- 1);
2116 // pointer to the tag for an element
2118 ztSlot(zone_t zone
, vm_offset_t element
)
2121 if (zone
->tags_inline
) {
2122 result
= (vm_tag_t
*)ZTAGBASE(zone
, element
);
2123 if ((PAGE_MASK
& element
) >= zone_elem_size(zone
)) {
2127 result
= &((vm_tag_t
*)zone_tags_min
)[ZTAGBASE(zone
, element
)[0] +
2128 (element
& PAGE_MASK
) / zone_elem_size(zone
)];
2134 ztLog2down(uint32_t size
)
2136 size
= 31 - __builtin_clz(size
);
2141 ztFault(vm_map_t map
, const void * address
, size_t size
, uint32_t flags
)
2143 vm_map_offset_t addr
= (vm_map_offset_t
) address
;
2144 vm_map_offset_t page
, end
;
2146 page
= trunc_page(addr
);
2147 end
= round_page(addr
+ size
);
2149 for (; page
< end
; page
+= page_size
) {
2150 if (!pmap_find_phys(kernel_pmap
, page
)) {
2151 kern_return_t __unused
2152 ret
= kernel_memory_populate(map
, page
, PAGE_SIZE
,
2153 KMA_KOBJECT
| flags
, VM_KERN_MEMORY_DIAG
);
2154 assert(ret
== KERN_SUCCESS
);
2160 ztPresent(const void * address
, size_t size
)
2162 vm_map_offset_t addr
= (vm_map_offset_t
) address
;
2163 vm_map_offset_t page
, end
;
2166 page
= trunc_page(addr
);
2167 end
= round_page(addr
+ size
);
2168 for (result
= TRUE
; (page
< end
); page
+= page_size
) {
2169 result
= pmap_find_phys(kernel_pmap
, page
);
2179 ztDump(boolean_t sanity
);
2181 ztDump(boolean_t sanity
)
2185 for (q
= 0; q
<= ztFreeIndexMax
; q
++) {
2189 cq
= ztLog2down(ztBlocks
[p
].size
);
2190 if (cq
> ztFreeIndexMax
) {
2191 cq
= ztFreeIndexMax
;
2193 if (!ztBlocks
[p
].free
2194 || ((p
!= q
) && (q
!= cq
))
2195 || (ztBlocks
[ztBlocks
[p
].next
].prev
!= p
)
2196 || (ztBlocks
[ztBlocks
[p
].prev
].next
!= p
)) {
2197 kprintf("zterror at %d", p
);
2199 kprintf("zterror at %d", p
);
2204 kprintf("zt[%03d]%c %d, %d, %d\n",
2205 p
, ztBlocks
[p
].free
? 'F' : 'A',
2206 ztBlocks
[p
].next
, ztBlocks
[p
].prev
,
2208 p
= ztBlocks
[p
].next
;
2218 printf("-----------------------\n");
2224 #define ZTBDEQ(idx) \
2225 ztBlocks[ztBlocks[(idx)].prev].next = ztBlocks[(idx)].next; \
2226 ztBlocks[ztBlocks[(idx)].next].prev = ztBlocks[(idx)].prev;
2229 ztFree(zone_t zone __unused
, uint32_t index
, uint32_t count
)
2231 uint32_t q
, w
, p
, size
, merge
;
2234 ztBlocksFree
+= count
;
2236 // merge with preceding
2237 merge
= (index
+ count
);
2238 if ((merge
< ztBlocksCount
)
2239 && ztPresent(&ztBlocks
[merge
], sizeof(ztBlocks
[merge
]))
2240 && ztBlocks
[merge
].free
) {
2242 count
+= ztBlocks
[merge
].size
;
2245 // merge with following
2246 merge
= (index
- 1);
2247 if ((merge
> ztFreeIndexMax
)
2248 && ztPresent(&ztBlocks
[merge
], sizeof(ztBlocks
[merge
]))
2249 && ztBlocks
[merge
].free
) {
2250 size
= ztBlocks
[merge
].size
;
2256 q
= ztLog2down(count
);
2257 if (q
> ztFreeIndexMax
) {
2261 // queue in order of size
2263 p
= ztBlocks
[w
].next
;
2267 if (ztBlocks
[p
].size
>= count
) {
2272 ztBlocks
[p
].prev
= index
;
2273 ztBlocks
[w
].next
= index
;
2276 ztFault(zone_tags_map
, &ztBlocks
[index
], sizeof(ztBlocks
[index
]), 0);
2278 // mark first & last with free flag and size
2279 ztBlocks
[index
].free
= TRUE
;
2280 ztBlocks
[index
].size
= count
;
2281 ztBlocks
[index
].prev
= w
;
2282 ztBlocks
[index
].next
= p
;
2284 index
+= (count
- 1);
2286 ztFault(zone_tags_map
, &ztBlocks
[index
], sizeof(ztBlocks
[index
]), 0);
2287 ztBlocks
[index
].free
= TRUE
;
2288 ztBlocks
[index
].size
= count
;
2293 ztAlloc(zone_t zone
, uint32_t count
)
2295 uint32_t q
, w
, p
, leftover
;
2299 q
= ztLog2up(count
);
2300 if (q
> ztFreeIndexMax
) {
2306 p
= ztBlocks
[w
].next
;
2310 if (ztBlocks
[p
].size
>= count
) {
2311 // dequeue, mark both ends allocated
2312 ztBlocks
[w
].next
= ztBlocks
[p
].next
;
2313 ztBlocks
[ztBlocks
[p
].next
].prev
= w
;
2314 ztBlocks
[p
].free
= FALSE
;
2315 ztBlocksFree
-= ztBlocks
[p
].size
;
2316 if (ztBlocks
[p
].size
> 1) {
2317 ztBlocks
[p
+ ztBlocks
[p
].size
- 1].free
= FALSE
;
2320 // fault all the allocation
2321 ztFault(zone_tags_map
, &ztBlocks
[p
], count
* sizeof(ztBlocks
[p
]), 0);
2322 // mark last as allocated
2324 ztBlocks
[p
+ count
- 1].free
= FALSE
;
2327 leftover
= ztBlocks
[p
].size
- count
;
2329 ztFree(zone
, p
+ ztBlocks
[p
].size
- leftover
, leftover
);
2337 }while (q
<= ztFreeIndexMax
);
2344 zone_tagging_init(vm_size_t max_zonemap_size
)
2347 vm_map_kernel_flags_t vmk_flags
;
2350 // allocate submaps VM_KERN_MEMORY_DIAG
2352 zone_tagbase_map_size
= atop(max_zonemap_size
) * sizeof(uint32_t);
2353 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
2354 vmk_flags
.vmkf_permanent
= TRUE
;
2355 ret
= kmem_suballoc(kernel_map
, &zone_tagbase_min
, zone_tagbase_map_size
,
2356 FALSE
, VM_FLAGS_ANYWHERE
, vmk_flags
, VM_KERN_MEMORY_DIAG
,
2359 if (ret
!= KERN_SUCCESS
) {
2360 panic("zone_init: kmem_suballoc failed");
2362 zone_tagbase_max
= zone_tagbase_min
+ round_page(zone_tagbase_map_size
);
2364 zone_tags_map_size
= 2048 * 1024 * sizeof(vm_tag_t
);
2365 vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
2366 vmk_flags
.vmkf_permanent
= TRUE
;
2367 ret
= kmem_suballoc(kernel_map
, &zone_tags_min
, zone_tags_map_size
,
2368 FALSE
, VM_FLAGS_ANYWHERE
, vmk_flags
, VM_KERN_MEMORY_DIAG
,
2371 if (ret
!= KERN_SUCCESS
) {
2372 panic("zone_init: kmem_suballoc failed");
2374 zone_tags_max
= zone_tags_min
+ round_page(zone_tags_map_size
);
2376 ztBlocks
= (ztBlock
*) zone_tags_min
;
2377 ztBlocksCount
= (uint32_t)(zone_tags_map_size
/ sizeof(ztBlock
));
2379 // initialize the qheads
2380 lck_mtx_lock(&ztLock
);
2382 ztFault(zone_tags_map
, &ztBlocks
[0], sizeof(ztBlocks
[0]), 0);
2383 for (idx
= 0; idx
< ztFreeIndexCount
; idx
++) {
2384 ztBlocks
[idx
].free
= TRUE
;
2385 ztBlocks
[idx
].next
= idx
;
2386 ztBlocks
[idx
].prev
= idx
;
2387 ztBlocks
[idx
].size
= 0;
2389 // free remaining space
2390 ztFree(NULL
, ztFreeIndexCount
, ztBlocksCount
- ztFreeIndexCount
);
2392 lck_mtx_unlock(&ztLock
);
2396 ztMemoryAdd(zone_t zone
, vm_offset_t mem
, vm_size_t size
)
2399 uint32_t count
, block
, blocks
, idx
;
2403 tagbase
= ZTAGBASE(zone
, mem
);
2405 lck_mtx_lock(&ztLock
);
2408 ztFault(zone_tagbase_map
, tagbase
, pages
* sizeof(uint32_t), 0);
2410 if (!zone
->tags_inline
) {
2412 count
= (uint32_t)(size
/ zone_elem_size(zone
));
2413 blocks
= ((count
+ ztTagsPerBlock
- 1) / ztTagsPerBlock
);
2414 block
= ztAlloc(zone
, blocks
);
2418 assert(-1U != block
);
2421 lck_mtx_unlock(&ztLock
);
2423 if (!zone
->tags_inline
) {
2424 // set tag base for each page
2425 block
*= ztTagsPerBlock
;
2426 for (idx
= 0; idx
< pages
; idx
++) {
2427 vm_offset_t esize
= zone_elem_size(zone
);
2428 tagbase
[idx
] = block
+ (uint32_t)((ptoa(idx
) + esize
- 1) / esize
);
2434 ztMemoryRemove(zone_t zone
, vm_offset_t mem
, vm_size_t size
)
2437 uint32_t count
, block
, blocks
, idx
;
2440 // set tag base for each page
2442 tagbase
= ZTAGBASE(zone
, mem
);
2444 for (idx
= 0; idx
< pages
; idx
++) {
2445 tagbase
[idx
] = 0xFFFFFFFF;
2448 lck_mtx_lock(&ztLock
);
2449 if (!zone
->tags_inline
) {
2450 count
= (uint32_t)(size
/ zone_elem_size(zone
));
2451 blocks
= ((count
+ ztTagsPerBlock
- 1) / ztTagsPerBlock
);
2452 assert(block
!= 0xFFFFFFFF);
2453 block
/= ztTagsPerBlock
;
2454 ztFree(NULL
/* zone is unlocked */, block
, blocks
);
2457 lck_mtx_unlock(&ztLock
);
2461 zone_index_from_tag_index(uint32_t tag_zone_index
, vm_size_t
* elem_size
)
2463 simple_lock(&all_zones_lock
, &zone_locks_grp
);
2465 zone_index_foreach(idx
) {
2466 zone_t z
= &zone_array
[idx
];
2470 if (tag_zone_index
!= z
->tag_zone_index
) {
2474 *elem_size
= zone_elem_size(z
);
2475 simple_unlock(&all_zones_lock
);
2479 simple_unlock(&all_zones_lock
);
2484 #endif /* VM_MAX_TAG_ZONES */
2485 #endif /* !ZALLOC_TEST */
2486 #pragma mark zalloc helpers
2490 static inline uint16_t
2493 return zc_magazine_size
;
2496 __attribute__((noinline
, cold
))
2498 zone_lock_was_contended(zone_t zone
, zone_cache_t zc
)
2500 lck_spin_lock_nopreempt(&zone
->z_lock
);
2503 * If zone caching has been disabled due to memory pressure,
2504 * then recording contention is not useful, give the system
2507 if (__improbable(zone_caching_disabled
)) {
2511 zone
->z_contention_cur
++;
2513 if (zc
== NULL
|| zc
->zc_depot_max
>= INT16_MAX
* zc_mag_size()) {
2518 * Let the depot grow based on how bad the contention is,
2519 * and how populated the zone is.
2521 if (zone
->z_contention_wma
< 2 * Z_CONTENTION_WMA_UNIT
) {
2522 if (zc
->zc_depot_max
* zpercpu_count() * 20u >=
2523 zone
->z_elems_avail
) {
2527 if (zone
->z_contention_wma
< 4 * Z_CONTENTION_WMA_UNIT
) {
2528 if (zc
->zc_depot_max
* zpercpu_count() * 10u >=
2529 zone
->z_elems_avail
) {
2533 if (!zc_grow_threshold
|| zone
->z_contention_wma
<
2534 zc_grow_threshold
* Z_CONTENTION_WMA_UNIT
) {
2542 zone_lock_nopreempt_check_contention(zone_t zone
, zone_cache_t zc
)
2544 if (lck_spin_try_lock_nopreempt(&zone
->z_lock
)) {
2548 zone_lock_was_contended(zone
, zc
);
2552 zone_lock_check_contention(zone_t zone
, zone_cache_t zc
)
2554 disable_preemption();
2555 zone_lock_nopreempt_check_contention(zone
, zc
);
2559 zone_unlock_nopreempt(zone_t zone
)
2561 lck_spin_unlock_nopreempt(&zone
->z_lock
);
2565 zone_depot_lock_nopreempt(zone_cache_t zc
)
2567 hw_lock_bit_nopreempt(&zc
->zc_depot_lock
, 0, &zone_locks_grp
);
2571 zone_depot_unlock_nopreempt(zone_cache_t zc
)
2573 hw_unlock_bit_nopreempt(&zc
->zc_depot_lock
, 0);
2577 zone_depot_lock(zone_cache_t zc
)
2579 hw_lock_bit(&zc
->zc_depot_lock
, 0, &zone_locks_grp
);
2583 zone_depot_unlock(zone_cache_t zc
)
2585 hw_unlock_bit(&zc
->zc_depot_lock
, 0);
2595 zone_heap_name(zone_t z
)
2597 if (__probable(z
->kalloc_heap
< KHEAP_ID_COUNT
)) {
2598 return kalloc_heap_names
[z
->kalloc_heap
];
2604 zone_alloc_pages_for_nelems(zone_t z
, vm_size_t max_elems
)
2606 vm_size_t elem_count
, chunks
;
2608 elem_count
= ptoa(z
->z_percpu
? 1 : z
->z_chunk_pages
) / zone_elem_size(z
);
2609 chunks
= (max_elems
+ elem_count
- 1) / elem_count
;
2611 return (uint32_t)MIN(UINT32_MAX
, chunks
* z
->z_chunk_pages
);
2614 static inline vm_size_t
2615 zone_submaps_approx_size(void)
2619 for (unsigned idx
= 0; idx
<= zone_last_submap_idx
; idx
++) {
2620 size
+= zone_submaps
[idx
]->size
;
2627 zone_cache_swap_magazines(zone_cache_t cache
)
2629 uint16_t count_a
= cache
->zc_alloc_cur
;
2630 uint16_t count_f
= cache
->zc_free_cur
;
2631 zone_element_t
*elems_a
= cache
->zc_alloc_elems
;
2632 zone_element_t
*elems_f
= cache
->zc_free_elems
;
2634 z_debug_assert(count_a
<= zc_mag_size());
2635 z_debug_assert(count_f
<= zc_mag_size());
2637 cache
->zc_alloc_cur
= count_f
;
2638 cache
->zc_free_cur
= count_a
;
2639 cache
->zc_alloc_elems
= elems_f
;
2640 cache
->zc_free_elems
= elems_a
;
2644 * @function zone_magazine_load
2647 * Cache the value of @c zm_cur on the cache to avoid a dependent load
2648 * on the allocation fastpath.
2651 zone_magazine_load(uint16_t *count
, zone_element_t
**elems
, zone_magazine_t mag
)
2653 z_debug_assert(mag
->zm_cur
<= zc_mag_size());
2654 *count
= mag
->zm_cur
;
2655 *elems
= mag
->zm_elems
;
2659 * @function zone_magazine_replace
2662 * Unlod a magazine and load a new one instead.
2664 static zone_magazine_t
2665 zone_magazine_replace(uint16_t *count
, zone_element_t
**elems
,
2666 zone_magazine_t mag
)
2668 zone_magazine_t old
;
2670 old
= (zone_magazine_t
)((uintptr_t)*elems
-
2671 offsetof(struct zone_magazine
, zm_elems
));
2672 old
->zm_cur
= *count
;
2673 z_debug_assert(old
->zm_cur
<= zc_mag_size());
2674 zone_magazine_load(count
, elems
, mag
);
2679 static zone_magazine_t
2680 zone_magazine_alloc(zalloc_flags_t flags
)
2682 return zalloc_ext(zc_magazine_zone
, zc_magazine_zone
->z_stats
,
2687 zone_magazine_free(zone_magazine_t mag
)
2689 zfree_ext(zc_magazine_zone
, zc_magazine_zone
->z_stats
, mag
);
2693 zone_enable_caching(zone_t zone
)
2695 zone_cache_t caches
;
2697 caches
= zalloc_percpu_permanent_type(struct zone_cache
);
2698 zpercpu_foreach(zc
, caches
) {
2699 zone_magazine_load(&zc
->zc_alloc_cur
, &zc
->zc_alloc_elems
,
2700 zone_magazine_alloc(Z_WAITOK
| Z_NOFAIL
));
2701 zone_magazine_load(&zc
->zc_free_cur
, &zc
->zc_free_elems
,
2702 zone_magazine_alloc(Z_WAITOK
| Z_NOFAIL
));
2703 STAILQ_INIT(&zc
->zc_depot
);
2706 if (os_atomic_xchg(&zone
->z_pcpu_cache
, caches
, release
)) {
2707 panic("allocating caches for zone %s twice", zone
->z_name
);
2712 zone_maps_owned(vm_address_t addr
, vm_size_t size
)
2714 return from_zone_map(addr
, size
, ZONE_ADDR_NATIVE
);
2719 vm_map_size_t
*psize
,
2720 vm_map_size_t
*pfree
,
2721 vm_map_size_t
*plargest_free
)
2723 vm_map_size_t size
, free
, largest
;
2725 vm_map_sizes(zone_submaps
[0], psize
, pfree
, plargest_free
);
2727 for (uint32_t i
= 1; i
<= zone_last_submap_idx
; i
++) {
2728 vm_map_sizes(zone_submaps
[i
], &size
, &free
, &largest
);
2731 *plargest_free
= MAX(*plargest_free
, largest
);
2735 __attribute__((always_inline
))
2737 zone_submap(zone_t zone
)
2739 return zone_submaps
[zone
->z_submap_idx
];
2745 return zpercpu_early_count
;
2749 track_this_zone(const char *zonename
, const char *logname
)
2752 const char *zc
= zonename
;
2753 const char *lc
= logname
;
2756 * Compare the strings. We bound the compare by MAX_ZONE_NAME.
2759 for (len
= 1; len
<= MAX_ZONE_NAME
; zc
++, lc
++, len
++) {
2761 * If the current characters don't match, check for a space in
2762 * in the zone name and a corresponding period in the log name.
2763 * If that's not there, then the strings don't match.
2766 if (*zc
!= *lc
&& !(*zc
== ' ' && *lc
== '.')) {
2771 * The strings are equal so far. If we're at the end, then it's a match.
2782 #if DEBUG || DEVELOPMENT
2785 zone_element_info(void *addr
, vm_tag_t
* ptag
)
2788 vm_tag_t tag
= VM_KERN_MEMORY_NONE
;
2789 struct zone
*src_zone
;
2791 if (from_zone_map(addr
, sizeof(void *), ZONE_ADDR_NATIVE
) ||
2792 from_zone_map(addr
, sizeof(void *), ZONE_ADDR_FOREIGN
)) {
2793 src_zone
= &zone_array
[zone_index_from_ptr(addr
)];
2794 #if VM_MAX_TAG_ZONES
2795 if (__improbable(src_zone
->tags
)) {
2796 tag
= *ztSlot(src_zone
, (vm_offset_t
)addr
) >> 1;
2798 #endif /* VM_MAX_TAG_ZONES */
2799 size
= zone_elem_size(src_zone
);
2802 gzalloc_element_size(addr
, NULL
, &size
);
2803 #endif /* CONFIG_GZALLOC */
2809 #endif /* DEBUG || DEVELOPMENT */
2811 /* The backup pointer is stored in the last pointer-sized location in an element. */
2812 __header_always_inline vm_offset_t
*
2813 get_primary_ptr(vm_offset_t elem
)
2815 return (vm_offset_t
*)elem
;
2818 __header_always_inline vm_offset_t
*
2819 get_backup_ptr(vm_offset_t elem
, vm_size_t elem_size
)
2821 return (vm_offset_t
*)(elem
+ elem_size
- sizeof(vm_offset_t
));
2824 #endif /* !ZALLOC_TEST */
2825 #pragma mark Zone poisoning/zeroing and early random
2828 #define ZONE_ENTROPY_CNT 2
2829 static struct zone_bool_gen
{
2830 struct bool_gen zbg_bg
;
2831 uint32_t zbg_entropy
[ZONE_ENTROPY_CNT
];
2832 } zone_bool_gen
[MAX_CPUS
];
2835 * Initialize zone poisoning
2836 * called from zone_bootstrap before any allocations are made from zalloc
2845 * Initialize canary random cookie.
2847 * Make sure that (zp_canary ^ pointer) have non zero low bits (01)
2848 * different from ZONE_POISON (11).
2850 * On LP64, have (zp_canary ^ pointer) have the high bits equal 0xC0FFEE...
2852 static_assert(ZONE_POISON
% 4 == 3);
2853 zp_canary
= (uintptr_t)early_random();
2855 zp_canary
&= 0x000000fffffffffc;
2856 zp_canary
|= 0xc0ffee0000000001 ^ 0xffffff0000000000;
2858 zp_canary
&= 0xfffffffc;
2859 zp_canary
|= 0x00000001;
2862 /* -zp: enable poisoning for every alloc and free */
2863 if (PE_parse_boot_argn("-zp", temp_buf
, sizeof(temp_buf
))) {
2867 /* -no-zp: disable poisoning */
2868 if (PE_parse_boot_argn("-no-zp", temp_buf
, sizeof(temp_buf
))) {
2870 printf("Zone poisoning disabled\n");
2873 zpercpu_foreach_cpu(cpu
) {
2874 random_bool_init(&zone_bool_gen
[cpu
].zbg_bg
);
2878 static inline uint32_t
2879 zone_poison_count_init(zone_t zone
)
2881 return zp_factor
+ (((uint32_t)zone_elem_size(zone
)) >> zp_scale
) ^
2882 (mach_absolute_time() & 0x7);
2886 * Zero the element if zone has z_free_zeroes flag set else poison
2887 * the element if zs_poison_seqno hits 0.
2890 zfree_clear_or_poison(zone_t zone
, vm_offset_t addr
, vm_offset_t elem_size
)
2892 if (zone
->z_free_zeroes
) {
2893 if (zone
->z_percpu
) {
2894 zpercpu_foreach_cpu(i
) {
2895 bzero((void *)(addr
+ ptoa(i
)), elem_size
);
2898 bzero((void *)addr
, elem_size
);
2903 zprot_mode_t poison
= ZPM_AUTO
;
2904 #if ZALLOC_ENABLE_POISONING
2905 if (__improbable(zp_factor
== 1)) {
2906 poison
= ZPM_POISON
;
2907 } else if (__probable(zp_factor
!= 0)) {
2908 uint32_t *seqnop
= &zpercpu_get(zone
->z_stats
)->zs_poison_seqno
;
2909 uint32_t seqno
= os_atomic_load(seqnop
, relaxed
);
2911 os_atomic_store(seqnop
, zone_poison_count_init(zone
), relaxed
);
2912 poison
= ZPM_POISON
;
2914 os_atomic_store(seqnop
, seqno
- 1, relaxed
);
2917 if (poison
== ZPM_POISON
) {
2918 /* memset_pattern{4|8} could help make this faster: <rdar://problem/4662004> */
2919 for (size_t i
= 0; i
< elem_size
/ sizeof(vm_offset_t
); i
++) {
2920 ((vm_offset_t
*)addr
)[i
] = ZONE_POISON
;
2924 * Set a canary at the extremities.
2926 * Zero first zp_min_size bytes of elements that aren't being
2929 * Element size is larger than zp_min_size in this path,
2930 * zones with smaller elements have z_free_zeroes set.
2932 *get_primary_ptr(addr
) = zp_canary
^ (uintptr_t)addr
;
2933 bzero((void *)addr
+ sizeof(vm_offset_t
),
2934 zp_min_size
- sizeof(vm_offset_t
));
2935 *get_backup_ptr(addr
, elem_size
) = zp_canary
^ (uintptr_t)addr
;
2937 poison
= ZPM_CANARY
;
2939 #endif /* ZALLOC_ENABLE_POISONING */
2944 #if ZALLOC_ENABLE_POISONING
2948 zalloc_uaf_panic(zone_t z
, uintptr_t elem
, size_t size
, zprot_mode_t zpm
)
2950 uint32_t esize
= (uint32_t)zone_elem_size(z
);
2951 uint32_t first_offs
= ~0u;
2952 uintptr_t first_bits
= 0, v
;
2958 #define ZPF "0x%016lx"
2960 #define ZPF "0x%08lx"
2965 if (zpm
== ZPM_CANARY
) {
2968 v
= *get_primary_ptr(elem
);
2969 if (v
!= (elem
^ zp_canary
)) {
2970 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, "\n"
2971 "%5d: got "ZPF
", want "ZPF
" (xor: "ZPF
")",
2972 0, v
, (elem
^ zp_canary
), (v
^ elem
^ zp_canary
));
2973 if (first_offs
> 0) {
2979 v
= *get_backup_ptr(elem
, esize
);
2980 if (v
!= (elem
^ zp_canary
)) {
2981 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, "\n"
2982 "%5d: got "ZPF
", want "ZPF
" (xor: "ZPF
")",
2983 esize
- (int)sizeof(v
), v
, (elem
^ zp_canary
),
2984 (v
^ elem
^ zp_canary
));
2985 if (first_offs
> esize
- sizeof(v
)) {
2986 first_offs
= esize
- sizeof(v
);
2991 for (uint32_t o
= sizeof(v
); o
< zp_min_size
; o
+= sizeof(v
)) {
2992 if ((v
= *(uintptr_t *)(elem
+ o
)) == 0) {
2995 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, "\n"
2997 if (first_offs
> o
) {
3002 } else if (zpm
== ZPM_ZERO
) {
3005 for (uint32_t o
= 0; o
< size
; o
+= sizeof(v
)) {
3006 if ((v
= *(uintptr_t *)(elem
+ o
)) == 0) {
3009 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, "\n"
3011 if (first_offs
> o
) {
3019 for (uint32_t o
= 0; o
< size
; o
+= sizeof(v
)) {
3020 if ((v
= *(uintptr_t *)(elem
+ o
)) == ZONE_POISON
) {
3023 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, "\n"
3024 "%5d: "ZPF
" (xor: "ZPF
")",
3025 o
, v
, (v
^ ZONE_POISON
));
3026 if (first_offs
> o
) {
3033 (panic
)("[%s%s]: element modified after free "
3034 "(off:%d, val:"ZPF
", sz:%d, ptr:%p, prot:%s)%s",
3035 zone_heap_name(z
), zone_name(z
),
3036 first_offs
, first_bits
, esize
, (void *)elem
, how
, buf
);
3042 zalloc_validate_element_zero(zone_t zone
, vm_offset_t elem
, vm_size_t size
)
3044 if (memcmp_zero_ptr_aligned((void *)elem
, size
)) {
3045 zalloc_uaf_panic(zone
, elem
, size
, ZPM_ZERO
);
3047 if (!zone
->z_percpu
) {
3050 for (size_t i
= zpercpu_count(); --i
> 0;) {
3052 if (memcmp_zero_ptr_aligned((void *)elem
, size
)) {
3053 zalloc_uaf_panic(zone
, elem
, size
, ZPM_ZERO
);
3058 #if __arm64__ || __arm__
3059 typedef __attribute__((ext_vector_type(2))) vm_offset_t zpair_t
;
3068 __attribute__((noinline
))
3070 zalloc_validate_element_poison(zone_t zone
, vm_offset_t elem
, vm_size_t size
)
3072 vm_offset_t p
= elem
;
3073 vm_offset_t end
= elem
+ size
;
3075 const zpair_t poison
= { ZONE_POISON
, ZONE_POISON
};
3078 a
.x
= *(const vm_offset_t
*)p
;
3079 a
.y
= *(const vm_offset_t
*)(end
- sizeof(vm_offset_t
));
3085 * align p to the next double-wide boundary
3086 * align end to the previous double-wide boundary
3088 p
= (p
+ sizeof(zpair_t
) - 1) & -sizeof(zpair_t
);
3089 end
&= -sizeof(zpair_t
);
3091 if ((end
- p
) % (2 * sizeof(zpair_t
)) == 0) {
3095 end
-= sizeof(zpair_t
);
3096 b
.x
= ((zpair_t
*)end
)[0].x
^ poison
.x
;
3097 b
.y
= ((zpair_t
*)end
)[0].y
^ poison
.y
;
3100 for (; p
< end
; p
+= 2 * sizeof(zpair_t
)) {
3101 a
.x
|= ((zpair_t
*)p
)[0].x
^ poison
.x
;
3102 a
.y
|= ((zpair_t
*)p
)[0].y
^ poison
.y
;
3103 b
.x
|= ((zpair_t
*)p
)[1].x
^ poison
.x
;
3104 b
.y
|= ((zpair_t
*)p
)[1].y
^ poison
.y
;
3111 zalloc_uaf_panic(zone
, elem
, size
, ZPM_POISON
);
3116 zalloc_validate_element(zone_t zone
, vm_offset_t elem
, vm_size_t size
,
3119 vm_offset_t
*primary
= get_primary_ptr(elem
);
3120 vm_offset_t
*backup
= get_backup_ptr(elem
, size
);
3123 if (zone
->gzalloc_tracked
) {
3126 #endif /* CONFIG_GZALLOC */
3128 if (zone
->z_free_zeroes
) {
3129 return zalloc_validate_element_zero(zone
, elem
, size
);
3135 size
-= sizeof(vm_size_t
);
3136 return zalloc_validate_element_zero(zone
, elem
, size
);
3138 if (*backup
== ZONE_POISON
) {
3139 size
-= sizeof(vm_size_t
);
3140 return zalloc_validate_element_poison(zone
, elem
, size
);
3145 if ((*primary
^ zp_canary
) != elem
|| (*backup
^ zp_canary
) != elem
) {
3146 zalloc_uaf_panic(zone
, elem
, size
, ZPM_CANARY
);
3148 *primary
= *backup
= 0;
3153 return zalloc_validate_element_zero(zone
, elem
, size
);
3156 return zalloc_validate_element_poison(zone
, elem
, size
);
3160 #endif /* ZALLOC_ENABLE_POISONING */
3161 #if ZALLOC_EARLY_GAPS
3163 __attribute__((noinline
))
3165 zone_early_gap_drop(int n
)
3168 zone_t zone0
= &zone_array
[0];
3169 struct zone_page_metadata
*meta
= NULL
;
3174 lck_mtx_lock(&zone_metadata_region_lck
);
3176 if (!zone_pva_is_null(zone0
->z_pageq_va
)) {
3177 meta
= zone_meta_queue_pop_native(zone0
,
3178 &zone0
->z_pageq_va
, &addr
);
3179 map
= zone_submaps
[meta
->zm_chunk_len
];
3180 pages
= meta
->zm_alloc_size
;
3181 __builtin_bzero(meta
, sizeof(struct zone_page_metadata
));
3184 lck_mtx_unlock(&zone_metadata_region_lck
);
3190 kmem_free(map
, addr
, ptoa(pages
));
3195 zone_early_gap_add(zone_t z
, uint16_t pages
)
3197 struct zone_page_metadata
*meta
= NULL
;
3198 zone_t zone0
= &zone_array
[0];
3202 kma_flags_t kmaflags
= KMA_KOBJECT
| KMA_ZERO
| KMA_VAONLY
;
3203 if (z
->z_submap_idx
== Z_SUBMAP_IDX_GENERAL
&&
3204 z
->kalloc_heap
!= KHEAP_ID_NONE
) {
3205 kmaflags
|= KMA_KHEAP
;
3208 kr
= kernel_memory_allocate(zone_submap(z
), &addr
, ptoa(pages
), 0,
3209 kmaflags
, VM_KERN_MEMORY_ZONE
);
3211 if (kr
!= KERN_SUCCESS
) {
3212 panic("unable to allocate early gap (%d pages): %d", pages
, kr
);
3215 zone_meta_populate(addr
, ptoa(pages
));
3217 meta
= zone_meta_from_addr(addr
);
3218 meta
->zm_alloc_size
= pages
;
3219 meta
->zm_chunk_len
= z
->z_submap_idx
;
3221 lck_mtx_lock(&zone_metadata_region_lck
);
3222 zone_meta_queue_push(zone0
, &zone0
->z_pageq_va
, meta
);
3223 lck_mtx_unlock(&zone_metadata_region_lck
);
3227 * Roughly until pd1 is made, introduce random gaps
3228 * between allocated pages.
3230 * This way the early boot allocations are not in a completely
3231 * predictible order and relative position.
3233 * Those gaps are returned to the maps afterwards.
3235 * We abuse the zone 0 (which is unused) "va" pageq to remember
3238 __attribute__((noinline
))
3240 zone_allocate_random_early_gap(zone_t z
)
3242 int16_t pages
= early_random() % 16;
3245 * 6% of the time: drop 2 gaps
3246 * 25% of the time: drop 1 gap
3247 * 37% of the time: do nothing
3248 * 18% of the time: add 1 gap
3249 * 12% of the time: add 2 gaps
3252 zone_early_gap_drop(pages
== 15 ? 2 : 1);
3255 /* values are 6 through 16 */
3256 zone_early_gap_add(z
, 6 + 2 * pages
);
3259 zone_early_gap_add(z
, 6 + early_random() % 16);
3264 zone_cleanup_early_gaps_if_needed(void)
3266 if (__improbable(!zone_pva_is_null(zone_array
[0].z_pageq_va
))) {
3267 zone_early_gap_drop(10);
3271 #endif /* ZALLOC_EARLY_GAPS */
3274 zone_early_scramble_rr(zone_t zone
, zone_stats_t zstats
)
3276 int cpu
= cpu_number();
3277 zone_stats_t zs
= zpercpu_get_cpu(zstats
, cpu
);
3280 bits
= random_bool_gen_bits(&zone_bool_gen
[cpu
].zbg_bg
,
3281 zone_bool_gen
[cpu
].zbg_entropy
, ZONE_ENTROPY_CNT
, 8);
3283 zs
->zs_alloc_rr
+= bits
;
3284 zs
->zs_alloc_rr
%= zone
->z_chunk_elems
;
3287 #endif /* !ZALLOC_TEST */
3288 #pragma mark Zone Leak Detection
3292 * Zone leak debugging code
3294 * When enabled, this code keeps a log to track allocations to a particular zone that have not
3295 * yet been freed. Examining this log will reveal the source of a zone leak. The log is allocated
3296 * only when logging is enabled, so there is no effect on the system when it's turned off. Logging is
3299 * Enable the logging via the boot-args. Add the parameter "zlog=<zone>" to boot-args where <zone>
3300 * is the name of the zone you wish to log.
3302 * This code only tracks one zone, so you need to identify which one is leaking first.
3303 * Generally, you'll know you have a leak when you get a "zalloc retry failed 3" panic from the zone
3304 * garbage collector. Note that the zone name printed in the panic message is not necessarily the one
3305 * containing the leak. So do a zprint from gdb and locate the zone with the bloated size. This
3306 * is most likely the problem zone, so set zlog in boot-args to this zone name, reboot and re-run the test. The
3307 * next time it panics with this message, examine the log using the kgmacros zstack, findoldest and countpcs.
3308 * See the help in the kgmacros for usage info.
3311 * Zone corruption logging
3313 * Logging can also be used to help identify the source of a zone corruption. First, identify the zone
3314 * that is being corrupted, then add "-zc zlog=<zone name>" to the boot-args. When -zc is used in conjunction
3315 * with zlog, it changes the logging style to track both allocations and frees to the zone. So when the
3316 * corruption is detected, examining the log will show you the stack traces of the callers who last allocated
3317 * and freed any particular element in the zone. Use the findelem kgmacro with the address of the element that's been
3318 * corrupted to examine its history. This should lead to the source of the corruption.
3321 /* Returns TRUE if we rolled over the counter at factor */
3322 __header_always_inline
bool
3323 sample_counter(volatile uint32_t *count_p
, uint32_t factor
)
3325 uint32_t old_count
, new_count
= 0;
3326 if (count_p
!= NULL
) {
3327 os_atomic_rmw_loop(count_p
, old_count
, new_count
, relaxed
, {
3328 new_count
= old_count
+ 1;
3329 if (new_count
>= factor
) {
3335 return new_count
== 0;
3338 #if ZONE_ENABLE_LOGGING
3339 /* Log allocations and frees to help debug a zone element corruption */
3340 static TUNABLE(bool, corruption_debug_flag
, "-zc", false);
3342 #define MAX_NUM_ZONES_ALLOWED_LOGGING 10 /* Maximum 10 zones can be logged at once */
3344 static int max_num_zones_to_log
= MAX_NUM_ZONES_ALLOWED_LOGGING
;
3345 static int num_zones_logged
= 0;
3348 * The number of records in the log is configurable via the zrecs parameter in boot-args. Set this to
3349 * the number of records you want in the log. For example, "zrecs=10" sets it to 10 records. Since this
3350 * is the number of stacks suspected of leaking, we don't need many records.
3353 #if defined(__LP64__)
3354 #define ZRECORDS_MAX 2560 /* Max records allowed in the log */
3356 #define ZRECORDS_MAX 1536 /* Max records allowed in the log */
3358 #define ZRECORDS_DEFAULT 1024 /* default records in log if zrecs is not specificed in boot-args */
3360 static TUNABLE(uint32_t, log_records
, "zrecs", ZRECORDS_DEFAULT
);
3363 zone_enable_logging(zone_t z
)
3365 z
->zlog_btlog
= btlog_create(log_records
, MAX_ZTRACE_DEPTH
,
3366 (corruption_debug_flag
== FALSE
) /* caller_will_remove_entries_for_element? */);
3368 if (z
->zlog_btlog
) {
3369 printf("zone: logging started for zone %s%s\n",
3370 zone_heap_name(z
), z
->z_name
);
3372 printf("zone: couldn't allocate memory for zrecords, turning off zleak logging\n");
3373 z
->zone_logging
= false;
3378 * @function zone_setup_logging
3381 * Optionally sets up a zone for logging.
3384 * We recognized two boot-args:
3386 * zlog=<zone_to_log>
3387 * zrecs=<num_records_in_log>
3389 * The zlog arg is used to specify the zone name that should be logged,
3390 * and zrecs is used to control the size of the log.
3392 * If zrecs is not specified, a default value is used.
3395 zone_setup_logging(zone_t z
)
3397 char zone_name
[MAX_ZONE_NAME
]; /* Temp. buffer for the zone name */
3398 char zlog_name
[MAX_ZONE_NAME
]; /* Temp. buffer to create the strings zlog1, zlog2 etc... */
3399 char zlog_val
[MAX_ZONE_NAME
]; /* the zone name we're logging, if any */
3402 * Don't allow more than ZRECORDS_MAX records even if the user asked for more.
3404 * This prevents accidentally hogging too much kernel memory
3405 * and making the system unusable.
3407 if (log_records
> ZRECORDS_MAX
) {
3408 log_records
= ZRECORDS_MAX
;
3412 * Append kalloc heap name to zone name (if zone is used by kalloc)
3414 snprintf(zone_name
, MAX_ZONE_NAME
, "%s%s", zone_heap_name(z
), z
->z_name
);
3416 /* zlog0 isn't allowed. */
3417 for (int i
= 1; i
<= max_num_zones_to_log
; i
++) {
3418 snprintf(zlog_name
, MAX_ZONE_NAME
, "zlog%d", i
);
3420 if (PE_parse_boot_argn(zlog_name
, zlog_val
, sizeof(zlog_val
)) &&
3421 track_this_zone(zone_name
, zlog_val
)) {
3422 z
->zone_logging
= true;
3429 * Backwards compat. with the old boot-arg used to specify single zone
3430 * logging i.e. zlog Needs to happen after the newer zlogn checks
3431 * because the prefix will match all the zlogn
3434 if (!z
->zone_logging
&&
3435 PE_parse_boot_argn("zlog", zlog_val
, sizeof(zlog_val
)) &&
3436 track_this_zone(zone_name
, zlog_val
)) {
3437 z
->zone_logging
= true;
3443 * If we want to log a zone, see if we need to allocate buffer space for
3446 * Some vm related zones are zinit'ed before we can do a kmem_alloc, so
3447 * we have to defer allocation in that case.
3449 * zone_init() will finish the job.
3451 * If we want to log one of the VM related zones that's set up early on,
3452 * we will skip allocation of the log until zinit is called again later
3453 * on some other zone.
3455 if (z
->zone_logging
&& startup_phase
>= STARTUP_SUB_KMEM_ALLOC
) {
3456 zone_enable_logging(z
);
3461 * Each record in the log contains a pointer to the zone element it refers to,
3462 * and a small array to hold the pc's from the stack trace. A
3463 * record is added to the log each time a zalloc() is done in the zone_of_interest. For leak debugging,
3464 * the record is cleared when a zfree() is done. For corruption debugging, the log tracks both allocs and frees.
3465 * If the log fills, old records are replaced as if it were a circular buffer.
3470 * Decide if we want to log this zone by doing a string compare between a zone name and the name
3471 * of the zone to log. Return true if the strings are equal, false otherwise. Because it's not
3472 * possible to include spaces in strings passed in via the boot-args, a period in the logname will
3473 * match a space in the zone name.
3477 * Test if we want to log this zalloc/zfree event. We log if this is the zone we're interested in and
3478 * the buffer for the records has been allocated.
3481 #define DO_LOGGING(z) (z->zlog_btlog != NULL)
3482 #else /* !ZONE_ENABLE_LOGGING */
3483 #define DO_LOGGING(z) 0
3484 #endif /* !ZONE_ENABLE_LOGGING */
3488 * The zone leak detector, abbreviated 'zleak', keeps track of a subset of the currently outstanding
3489 * allocations made by the zone allocator. Every zleak_sample_factor allocations in each zone, we capture a
3490 * backtrace. Every free, we examine the table and determine if the allocation was being tracked,
3491 * and stop tracking it if it was being tracked.
3493 * We track the allocations in the zallocations hash table, which stores the address that was returned from
3494 * the zone allocator. Each stored entry in the zallocations table points to an entry in the ztraces table, which
3495 * stores the backtrace associated with that allocation. This provides uniquing for the relatively large
3496 * backtraces - we don't store them more than once.
3498 * Data collection begins when the zone map is 50% full, and only occurs for zones that are taking up
3499 * a large amount of virtual space.
3501 #define ZLEAK_STATE_ENABLED 0x01 /* Zone leak monitoring should be turned on if zone_map fills up. */
3502 #define ZLEAK_STATE_ACTIVE 0x02 /* We are actively collecting traces. */
3503 #define ZLEAK_STATE_ACTIVATING 0x04 /* Some thread is doing setup; others should move along. */
3504 #define ZLEAK_STATE_FAILED 0x08 /* Attempt to allocate tables failed. We will not try again. */
3505 static uint32_t zleak_state
= 0; /* State of collection, as above */
3506 static unsigned int zleak_sample_factor
= 1000; /* Allocations per sample attempt */
3508 bool panic_include_ztrace
= FALSE
; /* Enable zleak logging on panic */
3509 vm_size_t zleak_global_tracking_threshold
; /* Size of zone map at which to start collecting data */
3510 vm_size_t zleak_per_zone_tracking_threshold
; /* Size a zone will have before we will collect data on it */
3513 * Counters for allocation statistics.
3516 /* Times two active records want to occupy the same spot */
3517 static unsigned int z_alloc_collisions
= 0;
3518 static unsigned int z_trace_collisions
= 0;
3520 /* Times a new record lands on a spot previously occupied by a freed allocation */
3521 static unsigned int z_alloc_overwrites
= 0;
3522 static unsigned int z_trace_overwrites
= 0;
3524 /* Times a new alloc or trace is put into the hash table */
3525 static unsigned int z_alloc_recorded
= 0;
3526 static unsigned int z_trace_recorded
= 0;
3528 /* Times zleak_log returned false due to not being able to acquire the lock */
3529 static unsigned int z_total_conflicts
= 0;
3532 * Structure for keeping track of an allocation
3533 * An allocation bucket is in use if its element is not NULL
3535 struct zallocation
{
3536 uintptr_t za_element
; /* the element that was zalloc'ed or zfree'ed, NULL if bucket unused */
3537 vm_size_t za_size
; /* how much memory did this allocation take up? */
3538 uint32_t za_trace_index
; /* index into ztraces for backtrace associated with allocation */
3539 /* TODO: #if this out */
3540 uint32_t za_hit_count
; /* for determining effectiveness of hash function */
3543 /* Size must be a power of two for the zhash to be able to just mask off bits instead of mod */
3544 static uint32_t zleak_alloc_buckets
= CONFIG_ZLEAK_ALLOCATION_MAP_NUM
;
3545 static uint32_t zleak_trace_buckets
= CONFIG_ZLEAK_TRACE_MAP_NUM
;
3547 vm_size_t zleak_max_zonemap_size
;
3549 /* Hashmaps of allocations and their corresponding traces */
3550 static struct zallocation
* zallocations
;
3551 static struct ztrace
* ztraces
;
3553 /* not static so that panic can see this, see kern/debug.c */
3554 struct ztrace
* top_ztrace
;
3556 /* Lock to protect zallocations, ztraces, and top_ztrace from concurrent modification. */
3557 static LCK_GRP_DECLARE(zleak_lock_grp
, "zleak_lock");
3558 static LCK_SPIN_DECLARE(zleak_lock
, &zleak_lock_grp
);
3561 * Initializes the zone leak monitor. Called from zone_init()
3565 zleak_init(vm_size_t max_zonemap_size
)
3567 char scratch_buf
[16];
3568 boolean_t zleak_enable_flag
= FALSE
;
3570 zleak_max_zonemap_size
= max_zonemap_size
;
3571 zleak_global_tracking_threshold
= max_zonemap_size
/ 2;
3572 zleak_per_zone_tracking_threshold
= zleak_global_tracking_threshold
/ 8;
3575 if (PE_parse_boot_argn("-zleakon", scratch_buf
, sizeof(scratch_buf
))) {
3576 zleak_enable_flag
= TRUE
;
3577 printf("zone leak detection enabled\n");
3579 zleak_enable_flag
= FALSE
;
3580 printf("zone leak detection disabled\n");
3582 #else /* CONFIG_EMBEDDED */
3583 /* -zleakoff (flag to disable zone leak monitor) */
3584 if (PE_parse_boot_argn("-zleakoff", scratch_buf
, sizeof(scratch_buf
))) {
3585 zleak_enable_flag
= FALSE
;
3586 printf("zone leak detection disabled\n");
3588 zleak_enable_flag
= TRUE
;
3589 printf("zone leak detection enabled\n");
3591 #endif /* CONFIG_EMBEDDED */
3593 /* zfactor=XXXX (override how often to sample the zone allocator) */
3594 if (PE_parse_boot_argn("zfactor", &zleak_sample_factor
, sizeof(zleak_sample_factor
))) {
3595 printf("Zone leak factor override: %u\n", zleak_sample_factor
);
3598 /* zleak-allocs=XXXX (override number of buckets in zallocations) */
3599 if (PE_parse_boot_argn("zleak-allocs", &zleak_alloc_buckets
, sizeof(zleak_alloc_buckets
))) {
3600 printf("Zone leak alloc buckets override: %u\n", zleak_alloc_buckets
);
3601 /* uses 'is power of 2' trick: (0x01000 & 0x00FFF == 0) */
3602 if (zleak_alloc_buckets
== 0 || (zleak_alloc_buckets
& (zleak_alloc_buckets
- 1))) {
3603 printf("Override isn't a power of two, bad things might happen!\n");
3607 /* zleak-traces=XXXX (override number of buckets in ztraces) */
3608 if (PE_parse_boot_argn("zleak-traces", &zleak_trace_buckets
, sizeof(zleak_trace_buckets
))) {
3609 printf("Zone leak trace buckets override: %u\n", zleak_trace_buckets
);
3610 /* uses 'is power of 2' trick: (0x01000 & 0x00FFF == 0) */
3611 if (zleak_trace_buckets
== 0 || (zleak_trace_buckets
& (zleak_trace_buckets
- 1))) {
3612 printf("Override isn't a power of two, bad things might happen!\n");
3616 if (zleak_enable_flag
) {
3617 zleak_state
= ZLEAK_STATE_ENABLED
;
3622 * Support for kern.zleak.active sysctl - a simplified
3623 * version of the zleak_state variable.
3626 get_zleak_state(void)
3628 if (zleak_state
& ZLEAK_STATE_FAILED
) {
3631 if (zleak_state
& ZLEAK_STATE_ACTIVE
) {
3638 zleak_activate(void)
3640 kern_return_t retval
;
3641 vm_size_t z_alloc_size
= zleak_alloc_buckets
* sizeof(struct zallocation
);
3642 vm_size_t z_trace_size
= zleak_trace_buckets
* sizeof(struct ztrace
);
3643 void *allocations_ptr
= NULL
;
3644 void *traces_ptr
= NULL
;
3646 /* Only one thread attempts to activate at a time */
3647 if (zleak_state
& (ZLEAK_STATE_ACTIVE
| ZLEAK_STATE_ACTIVATING
| ZLEAK_STATE_FAILED
)) {
3648 return KERN_SUCCESS
;
3651 /* Indicate that we're doing the setup */
3652 lck_spin_lock(&zleak_lock
);
3653 if (zleak_state
& (ZLEAK_STATE_ACTIVE
| ZLEAK_STATE_ACTIVATING
| ZLEAK_STATE_FAILED
)) {
3654 lck_spin_unlock(&zleak_lock
);
3655 return KERN_SUCCESS
;
3658 zleak_state
|= ZLEAK_STATE_ACTIVATING
;
3659 lck_spin_unlock(&zleak_lock
);
3661 /* Allocate and zero tables */
3662 retval
= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)&allocations_ptr
, z_alloc_size
, VM_KERN_MEMORY_DIAG
);
3663 if (retval
!= KERN_SUCCESS
) {
3667 retval
= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*)&traces_ptr
, z_trace_size
, VM_KERN_MEMORY_DIAG
);
3668 if (retval
!= KERN_SUCCESS
) {
3672 bzero(allocations_ptr
, z_alloc_size
);
3673 bzero(traces_ptr
, z_trace_size
);
3675 /* Everything's set. Install tables, mark active. */
3676 zallocations
= allocations_ptr
;
3677 ztraces
= traces_ptr
;
3680 * Initialize the top_ztrace to the first entry in ztraces,
3681 * so we don't have to check for null in zleak_log
3683 top_ztrace
= &ztraces
[0];
3686 * Note that we do need a barrier between installing
3687 * the tables and setting the active flag, because the zfree()
3688 * path accesses the table without a lock if we're active.
3690 lck_spin_lock(&zleak_lock
);
3691 zleak_state
|= ZLEAK_STATE_ACTIVE
;
3692 zleak_state
&= ~ZLEAK_STATE_ACTIVATING
;
3693 lck_spin_unlock(&zleak_lock
);
3699 * If we fail to allocate memory, don't further tax
3700 * the system by trying again.
3702 lck_spin_lock(&zleak_lock
);
3703 zleak_state
|= ZLEAK_STATE_FAILED
;
3704 zleak_state
&= ~ZLEAK_STATE_ACTIVATING
;
3705 lck_spin_unlock(&zleak_lock
);
3707 if (allocations_ptr
!= NULL
) {
3708 kmem_free(kernel_map
, (vm_offset_t
)allocations_ptr
, z_alloc_size
);
3711 if (traces_ptr
!= NULL
) {
3712 kmem_free(kernel_map
, (vm_offset_t
)traces_ptr
, z_trace_size
);
3719 zleak_activate_if_needed(void)
3721 if (__probable((zleak_state
& ZLEAK_STATE_ENABLED
) == 0)) {
3724 if (zleak_state
& ZLEAK_STATE_ACTIVE
) {
3727 if (zone_submaps_approx_size() < zleak_global_tracking_threshold
) {
3731 kern_return_t kr
= zleak_activate();
3732 if (kr
!= KERN_SUCCESS
) {
3733 printf("Failed to activate live zone leak debugging (%d).\n", kr
);
3738 zleak_track_if_needed(zone_t z
)
3740 if (__improbable(zleak_state
& ZLEAK_STATE_ACTIVE
)) {
3742 zone_size_wired(z
) >= zleak_per_zone_tracking_threshold
) {
3749 * TODO: What about allocations that never get deallocated,
3750 * especially ones with unique backtraces? Should we wait to record
3751 * until after boot has completed?
3752 * (How many persistent zallocs are there?)
3756 * This function records the allocation in the allocations table,
3757 * and stores the associated backtrace in the traces table
3758 * (or just increments the refcount if the trace is already recorded)
3759 * If the allocation slot is in use, the old allocation is replaced with the new allocation, and
3760 * the associated trace's refcount is decremented.
3761 * If the trace slot is in use, it returns.
3762 * The refcount is incremented by the amount of memory the allocation consumes.
3763 * The return value indicates whether to try again next time.
3766 zleak_log(uintptr_t* bt
,
3769 vm_size_t allocation_size
)
3771 /* Quit if there's someone else modifying the hash tables */
3772 if (!lck_spin_try_lock(&zleak_lock
)) {
3773 z_total_conflicts
++;
3777 struct zallocation
* allocation
= &zallocations
[hashaddr(addr
, zleak_alloc_buckets
)];
3779 uint32_t trace_index
= hashbacktrace(bt
, depth
, zleak_trace_buckets
);
3780 struct ztrace
* trace
= &ztraces
[trace_index
];
3782 allocation
->za_hit_count
++;
3783 trace
->zt_hit_count
++;
3786 * If the allocation bucket we want to be in is occupied, and if the occupier
3787 * has the same trace as us, just bail.
3789 if (allocation
->za_element
!= (uintptr_t) 0 && trace_index
== allocation
->za_trace_index
) {
3790 z_alloc_collisions
++;
3792 lck_spin_unlock(&zleak_lock
);
3796 /* STEP 1: Store the backtrace in the traces array. */
3797 /* A size of zero indicates that the trace bucket is free. */
3799 if (trace
->zt_size
> 0 && bcmp(trace
->zt_stack
, bt
, (depth
* sizeof(uintptr_t))) != 0) {
3801 * Different unique trace with same hash!
3802 * Just bail - if we're trying to record the leaker, hopefully the other trace will be deallocated
3803 * and get out of the way for later chances
3805 trace
->zt_collisions
++;
3806 z_trace_collisions
++;
3808 lck_spin_unlock(&zleak_lock
);
3810 } else if (trace
->zt_size
> 0) {
3811 /* Same trace, already added, so increment refcount */
3812 trace
->zt_size
+= allocation_size
;
3814 /* Found an unused trace bucket, record the trace here! */
3815 if (trace
->zt_depth
!= 0) { /* if this slot was previously used but not currently in use */
3816 z_trace_overwrites
++;
3820 trace
->zt_size
= allocation_size
;
3821 memcpy(trace
->zt_stack
, bt
, (depth
* sizeof(uintptr_t)));
3823 trace
->zt_depth
= depth
;
3824 trace
->zt_collisions
= 0;
3827 /* STEP 2: Store the allocation record in the allocations array. */
3829 if (allocation
->za_element
!= (uintptr_t) 0) {
3831 * Straight up replace any allocation record that was there. We don't want to do the work
3832 * to preserve the allocation entries that were there, because we only record a subset of the
3833 * allocations anyways.
3836 z_alloc_collisions
++;
3838 struct ztrace
* associated_trace
= &ztraces
[allocation
->za_trace_index
];
3839 /* Knock off old allocation's size, not the new allocation */
3840 associated_trace
->zt_size
-= allocation
->za_size
;
3841 } else if (allocation
->za_trace_index
!= 0) {
3842 /* Slot previously used but not currently in use */
3843 z_alloc_overwrites
++;
3846 allocation
->za_element
= addr
;
3847 allocation
->za_trace_index
= trace_index
;
3848 allocation
->za_size
= allocation_size
;
3852 if (top_ztrace
->zt_size
< trace
->zt_size
) {
3856 lck_spin_unlock(&zleak_lock
);
3861 * Free the allocation record and release the stacktrace.
3862 * This should be as fast as possible because it will be called for every free.
3864 __attribute__((noinline
))
3866 zleak_free(uintptr_t addr
,
3867 vm_size_t allocation_size
)
3869 if (addr
== (uintptr_t) 0) {
3873 struct zallocation
* allocation
= &zallocations
[hashaddr(addr
, zleak_alloc_buckets
)];
3875 /* Double-checked locking: check to find out if we're interested, lock, check to make
3876 * sure it hasn't changed, then modify it, and release the lock.
3879 if (allocation
->za_element
== addr
&& allocation
->za_trace_index
< zleak_trace_buckets
) {
3880 /* if the allocation was the one, grab the lock, check again, then delete it */
3881 lck_spin_lock(&zleak_lock
);
3883 if (allocation
->za_element
== addr
&& allocation
->za_trace_index
< zleak_trace_buckets
) {
3884 struct ztrace
*trace
;
3886 /* allocation_size had better match what was passed into zleak_log - otherwise someone is freeing into the wrong zone! */
3887 if (allocation
->za_size
!= allocation_size
) {
3888 panic("Freeing as size %lu memory that was allocated with size %lu\n",
3889 (uintptr_t)allocation_size
, (uintptr_t)allocation
->za_size
);
3892 trace
= &ztraces
[allocation
->za_trace_index
];
3894 /* size of 0 indicates trace bucket is unused */
3895 if (trace
->zt_size
> 0) {
3896 trace
->zt_size
-= allocation_size
;
3899 /* A NULL element means the allocation bucket is unused */
3900 allocation
->za_element
= 0;
3902 lck_spin_unlock(&zleak_lock
);
3908 zleak_activate_if_needed(void)
3913 zleak_track_if_needed(__unused zone_t z
)
3916 #endif /* CONFIG_ZLEAKS */
3917 #if ZONE_ENABLE_LOGGING || CONFIG_ZLEAKS
3919 __attribute__((noinline
))
3921 zalloc_log_or_trace_leaks(zone_t zone
, vm_offset_t addr
, void *fp
)
3923 uintptr_t zbt
[MAX_ZTRACE_DEPTH
]; /* used in zone leak logging and zone leak detection */
3924 unsigned int numsaved
= 0;
3926 #if ZONE_ENABLE_LOGGING
3927 if (DO_LOGGING(zone
)) {
3928 numsaved
= backtrace(zbt
, MAX_ZTRACE_DEPTH
, NULL
);
3929 btlog_add_entry(zone
->zlog_btlog
, (void *)addr
,
3930 ZOP_ALLOC
, (void **)zbt
, numsaved
);
3932 #endif /* ZONE_ENABLE_LOGGING */
3936 * Zone leak detection: capture a backtrace every zleak_sample_factor
3937 * allocations in this zone.
3939 if (__improbable(zone
->zleak_on
)) {
3940 if (sample_counter(&zone
->zleak_capture
, zleak_sample_factor
)) {
3941 /* Avoid backtracing twice if zone logging is on */
3942 if (numsaved
== 0) {
3943 numsaved
= backtrace_frame(zbt
, MAX_ZTRACE_DEPTH
, fp
, NULL
);
3945 /* Sampling can fail if another sample is happening at the same time in a different zone. */
3946 if (!zleak_log(zbt
, addr
, numsaved
, zone_elem_size(zone
))) {
3947 /* If it failed, roll back the counter so we sample the next allocation instead. */
3948 zone
->zleak_capture
= zleak_sample_factor
;
3953 if (__improbable(zone_leaks_scan_enable
&&
3954 !(zone_elem_size(zone
) & (sizeof(uintptr_t) - 1)))) {
3955 unsigned int count
, idx
;
3956 /* Fill element, from tail, with backtrace in reverse order */
3957 if (numsaved
== 0) {
3958 numsaved
= backtrace_frame(zbt
, MAX_ZTRACE_DEPTH
, fp
, NULL
);
3960 count
= (unsigned int)(zone_elem_size(zone
) / sizeof(uintptr_t));
3961 if (count
>= numsaved
) {
3962 count
= numsaved
- 1;
3964 for (idx
= 0; idx
< count
; idx
++) {
3965 ((uintptr_t *)addr
)[count
- 1 - idx
] = zbt
[idx
+ 1];
3968 #endif /* CONFIG_ZLEAKS */
3972 zalloc_should_log_or_trace_leaks(zone_t zone
, vm_size_t elem_size
)
3974 #if ZONE_ENABLE_LOGGING
3975 if (DO_LOGGING(zone
)) {
3978 #endif /* ZONE_ENABLE_LOGGING */
3981 * Zone leak detection: capture a backtrace every zleak_sample_factor
3982 * allocations in this zone.
3984 if (zone
->zleak_on
) {
3987 if (zone_leaks_scan_enable
&& !(elem_size
& (sizeof(uintptr_t) - 1))) {
3990 #endif /* CONFIG_ZLEAKS */
3994 #endif /* ZONE_ENABLE_LOGGING || CONFIG_ZLEAKS */
3995 #if ZONE_ENABLE_LOGGING
3997 __attribute__((noinline
))
3999 zfree_log_trace(zone_t zone
, vm_offset_t addr
, void *fp
)
4002 * See if we're doing logging on this zone.
4004 * There are two styles of logging used depending on
4005 * whether we're trying to catch a leak or corruption.
4007 if (__improbable(DO_LOGGING(zone
))) {
4008 if (corruption_debug_flag
) {
4009 uintptr_t zbt
[MAX_ZTRACE_DEPTH
];
4010 unsigned int numsaved
;
4012 * We're logging to catch a corruption.
4014 * Add a record of this zfree operation to log.
4016 numsaved
= backtrace_frame(zbt
, MAX_ZTRACE_DEPTH
, fp
, NULL
);
4017 btlog_add_entry(zone
->zlog_btlog
, (void *)addr
, ZOP_FREE
,
4018 (void **)zbt
, numsaved
);
4021 * We're logging to catch a leak.
4023 * Remove any record we might have for this element
4024 * since it's being freed. Note that we may not find it
4025 * if the buffer overflowed and that's OK.
4027 * Since the log is of a limited size, old records get
4028 * overwritten if there are more zallocs than zfrees.
4030 btlog_remove_entries_for_element(zone
->zlog_btlog
, (void *)addr
);
4035 #endif /* ZONE_ENABLE_LOGGING */
4037 /* These functions outside of CONFIG_ZLEAKS because they are also used in
4038 * mbuf.c for mbuf leak-detection. This is why they lack the z_ prefix.
4041 /* "Thomas Wang's 32/64 bit mix functions." http://www.concentric.net/~Ttwang/tech/inthash.htm */
4043 hash_mix(uintptr_t x
)
4066 hashbacktrace(uintptr_t* bt
, uint32_t depth
, uint32_t max_size
)
4069 uintptr_t mask
= max_size
- 1;
4072 hash
+= bt
[--depth
];
4075 hash
= hash_mix(hash
) & mask
;
4077 assert(hash
< max_size
);
4079 return (uint32_t) hash
;
4083 * TODO: Determine how well distributed this is
4084 * max_size must be a power of 2. i.e 0x10000 because 0x10000-1 is 0x0FFFF which is a great bitmask
4087 hashaddr(uintptr_t pt
, uint32_t max_size
)
4090 uintptr_t mask
= max_size
- 1;
4092 hash
= hash_mix(pt
) & mask
;
4094 assert(hash
< max_size
);
4096 return (uint32_t) hash
;
4099 #endif /* !ZALLOC_TEST */
4100 #pragma mark zone (re)fill
4104 * @defgroup Zone Refill
4108 * Functions handling The zone refill machinery.
4111 * Zones are refilled based on 3 mechanisms: direct expansion, async expansion,
4112 * VM-specific replenishment. Zones using VM-specific replenishment are marked
4113 * with the @c z_replenishes property set.
4115 * @c zalloc_ext() is the codepath that kicks the zone refill when the zone is
4116 * dropping below half of its @c z_elems_rsv (0 for most zones) and will:
4118 * - call @c zone_expand_locked() directly if the caller is allowed to block,
4120 * - wakeup the asynchroous expansion thread call if the caller is not allowed
4123 * - call @c zone_replenish_locked() to kick the replenish state machine.
4126 * <h2>Synchronous expansion</h2>
4128 * This mechanism is actually the only one that may refill a zone, and all the
4129 * other ones funnel through this one eventually.
4131 * @c zone_expand_locked() implements the core of the expansion mechanism,
4132 * and will do so while a caller specified predicate is true.
4134 * Zone expansion allows for up to 2 threads to concurrently refill the zone:
4135 * - one VM privileged thread,
4136 * - one regular thread.
4138 * Regular threads that refill will put down their identity in @c z_expander,
4139 * so that priority inversion avoidance can be implemented.
4141 * However, VM privileged threads are allowed to use VM page reserves,
4142 * which allows for the system to recover from extreme memory pressure
4143 * situations, allowing for the few allocations that @c zone_gc() or
4144 * killing processes require.
4146 * When a VM privileged thread is also expanding, the @c z_expander_vm_priv bit
4147 * is set. @c z_expander is not necessarily the identity of this VM privileged
4148 * thread (it is if the VM privileged thread came in first, but wouldn't be, and
4149 * could even be @c THREAD_NULL otherwise).
4151 * Note that the pageout-scan daemon might be BG and is VM privileged. To avoid
4152 * spending a whole pointer on priority inheritance for VM privileged threads
4153 * (and other issues related to having two owners), we use the rwlock boost as
4154 * a stop gap to avoid priority inversions.
4157 * <h2>Chunk wiring policies</h2>
4159 * Zones allocate memory in chunks of @c zone_t::z_chunk_pages pages at a time
4160 * to try to minimize fragmentation relative to element sizes not aligning with
4161 * a chunk size well. However, this can grow large and be hard to fulfill on
4162 * a system under a lot of memory pressure (chunks can be as long as 8 pages on
4165 * This is why, when under memory pressure the system allows chunks to be
4166 * partially populated. The metadata of the first page in the chunk maintains
4167 * the count of actually populated pages.
4169 * The metadata for addresses assigned to a zone are found of 4 queues:
4170 * - @c z_pageq_empty has chunk heads with populated pages and no allocated
4171 * elements (those can be targeted by @c zone_gc()),
4172 * - @c z_pageq_partial has chunk heads with populated pages that are partially
4174 * - @c z_pageq_full has chunk heads with populated pages with no free elements
4176 * - @c z_pageq_va has either chunk heads for sequestered VA space assigned to
4177 * the zone forever (if @c z_va_sequester is enabled), or the first secondary
4178 * metadata for a chunk whose corresponding page is not populated in the
4181 * When new pages need to be wired/populated, chunks from the @c z_pageq_va
4182 * queues are preferred.
4185 * <h2>Asynchronous expansion</h2>
4187 * This mechanism allows for refilling zones used mostly with non blocking
4188 * callers. It relies on a thread call (@c zone_expand_callout) which will
4189 * iterate all zones and refill the ones marked with @c z_async_refilling.
4191 * NOTE: If the calling thread for zalloc_noblock is lower priority than
4192 * the thread_call, then zalloc_noblock to an empty zone may succeed.
4195 * <h2>Dealing with zone allocations from the mach VM code</h2>
4197 * The implementation of the mach VM itself uses the zone allocator
4198 * for things like the vm_map_entry data structure. In order to prevent
4199 * an infinite recursion problem when adding more pages to a zone, @c zalloc
4200 * uses a replenish thread to refill the VM layer's zones before they have
4201 * too few remaining free entries. The reserved remaining free entries
4202 * guarantee that the VM routines can get entries from already mapped pages.
4204 * In order for that to work, the amount of allocations in the nested
4205 * case have to be bounded. There are currently 2 replenish zones, and
4206 * if each needs 1 element of each zone to add a new page to itself, that
4207 * gives us a minumum reserve of 2 elements.
4209 * There is also a deadlock issue with the zone garbage collection thread,
4210 * or any thread that is trying to free zone pages. While holding
4211 * the kernel's map lock they may need to allocate new VM map entries, hence
4212 * we need enough reserve to allow them to get past the point of holding the
4213 * map lock. After freeing that page, the GC thread will wait in
4214 * @c zone_reclaim() until the replenish threads can finish.
4215 * Since there's only 1 GC thread at a time, that adds a minimum of 1 to the
4218 * Since the minumum amount you can add to a zone is 1 page,
4219 * we'll use 16K (from ARM) as the refill size on all platforms.
4221 * When a refill zone drops to half that available, i.e. REFILL_SIZE / 2,
4222 * @c zalloc_ext() will wake the replenish thread. The replenish thread runs
4223 * until at least REFILL_SIZE worth of free elements exist, before sleeping again.
4224 * In the meantime threads may continue to use the reserve until there are only
4225 * REFILL_SIZE / 4 elements left. Below that point only the replenish threads
4226 * themselves and the GC thread may continue to use from the reserve.
4229 static thread_call_data_t zone_expand_callout
;
4231 static inline kma_flags_t
4232 zone_kma_flags(zone_t z
, zalloc_flags_t flags
)
4234 kma_flags_t kmaflags
= KMA_KOBJECT
| KMA_ZERO
;
4236 if (z
->z_noencrypt
) {
4237 kmaflags
|= KMA_NOENCRYPT
;
4239 if (flags
& Z_NOPAGEWAIT
) {
4240 kmaflags
|= KMA_NOPAGEWAIT
;
4242 if (z
->z_permanent
|| (!z
->z_destructible
&& z
->z_va_sequester
)) {
4243 kmaflags
|= KMA_PERMANENT
;
4245 if (z
->z_submap_idx
== Z_SUBMAP_IDX_GENERAL
&&
4246 z
->kalloc_heap
!= KHEAP_ID_NONE
) {
4247 kmaflags
|= KMA_KHEAP
;
4254 * @function zcram_and_lock()
4257 * Prepare some memory for being usable for allocation purposes.
4260 * Prepare memory in <code>[addr + ptoa(pg_start), addr + ptoa(pg_end))</code>
4261 * to be usable in the zone.
4263 * This function assumes the metadata is already populated for the range.
4265 * Calling this function with @c pg_start being 0 means that the memory
4266 * is either a partial chunk, or a full chunk, that isn't published anywhere
4267 * and the initialization can happen without locks held.
4269 * Calling this function with a non zero @c pg_start means that we are extending
4270 * an existing chunk: the memory in <code>[addr, addr + ptoa(pg_start))</code>,
4271 * is already usable and published in the zone, so extending it requires holding
4274 * @param zone The zone to cram new populated pages into
4275 * @param addr The base address for the chunk(s)
4276 * @param pg_va_new The number of virtual pages newly assigned to the zone
4277 * @param pg_start The first newly populated page relative to @a addr.
4278 * @param pg_end The after-last newly populated page relative to @a addr.
4279 * @param kind The kind of memory assigned to the zone.
4282 zcram_and_lock(zone_t zone
, vm_offset_t addr
, uint32_t pg_va_new
,
4283 uint32_t pg_start
, uint32_t pg_end
, zone_addr_kind_t kind
)
4285 zone_id_t zindex
= zone_index(zone
);
4286 vm_offset_t elem_size
= zone_elem_size(zone
);
4287 uint32_t free_start
= 0, free_end
= 0;
4289 struct zone_page_metadata
*meta
= zone_meta_from_addr(addr
);
4290 uint32_t chunk_pages
= zone
->z_chunk_pages
;
4292 assert(pg_start
< pg_end
&& pg_end
<= chunk_pages
);
4294 if (pg_start
== 0) {
4295 uint16_t chunk_len
= (uint16_t)pg_end
;
4296 uint16_t secondary_len
= ZM_SECONDARY_PAGE
;
4297 bool inline_bitmap
= false;
4299 if (zone
->z_percpu
) {
4301 secondary_len
= ZM_SECONDARY_PCPU_PAGE
;
4302 assert(pg_end
== zpercpu_count());
4304 if (!zone
->z_permanent
) {
4305 inline_bitmap
= zone
->z_chunk_elems
<= 32 * chunk_pages
;
4308 meta
[0] = (struct zone_page_metadata
){
4310 .zm_inline_bitmap
= inline_bitmap
,
4311 .zm_chunk_len
= chunk_len
,
4313 if (kind
== ZONE_ADDR_FOREIGN
) {
4314 /* Never hit z_pageq_empty */
4315 meta
[0].zm_alloc_size
= ZM_ALLOC_SIZE_LOCK
;
4318 for (uint16_t i
= 1; i
< chunk_pages
; i
++) {
4319 meta
[i
] = (struct zone_page_metadata
){
4321 .zm_inline_bitmap
= inline_bitmap
,
4322 .zm_chunk_len
= secondary_len
,
4327 free_end
= (uint32_t)ptoa(chunk_len
) / elem_size
;
4328 if (!zone
->z_permanent
) {
4329 zone_meta_bits_init(meta
, free_end
, zone
->z_chunk_elems
);
4332 assert(!zone
->z_percpu
&& !zone
->z_permanent
);
4334 free_end
= (uint32_t)ptoa(pg_end
) / elem_size
;
4335 free_start
= (uint32_t)ptoa(pg_start
) / elem_size
;
4338 #if VM_MAX_TAG_ZONES
4339 if (__improbable(zone
->tags
)) {
4340 assert(kind
== ZONE_ADDR_NATIVE
&& !zone
->z_percpu
);
4341 ztMemoryAdd(zone
, addr
+ ptoa(pg_start
),
4342 ptoa(pg_end
- pg_start
));
4344 #endif /* VM_MAX_TAG_ZONES */
4347 * Insert the initialized pages / metadatas into the right lists.
4351 assert(zone
->z_self
== zone
);
4353 if (pg_start
!= 0) {
4354 assert(meta
->zm_chunk_len
== pg_start
);
4356 zone_meta_bits_merge(meta
, free_start
, free_end
);
4357 meta
->zm_chunk_len
= (uint16_t)pg_end
;
4360 * consume the zone_meta_lock_in_partial()
4361 * done in zone_expand_locked()
4363 zone_meta_alloc_size_sub(zone
, meta
, ZM_ALLOC_SIZE_LOCK
);
4364 zone_meta_remqueue(zone
, meta
);
4367 if (zone
->z_permanent
|| meta
->zm_alloc_size
) {
4368 zone_meta_queue_push(zone
, &zone
->z_pageq_partial
, meta
);
4370 zone_meta_queue_push(zone
, &zone
->z_pageq_empty
, meta
);
4371 zone
->z_wired_empty
+= zone
->z_percpu
? 1 : pg_end
;
4373 if (pg_end
< chunk_pages
) {
4374 /* push any non populated residual VA on z_pageq_va */
4375 zone_meta_queue_push(zone
, &zone
->z_pageq_va
, meta
+ pg_end
);
4378 zone_elems_free_add(zone
, free_end
- free_start
);
4379 zone
->z_elems_avail
+= free_end
- free_start
;
4380 zone
->z_wired_cur
+= zone
->z_percpu
? 1 : pg_end
- pg_start
;
4382 zone
->z_va_cur
+= zone
->z_percpu
? 1 : pg_va_new
;
4384 if (zone
->z_wired_hwm
< zone
->z_wired_cur
) {
4385 zone
->z_wired_hwm
= zone
->z_wired_cur
;
4388 os_atomic_add(&zones_phys_page_mapped_count
, pg_end
- pg_start
, relaxed
);
4392 zcram(zone_t zone
, vm_offset_t addr
, uint32_t pages
, zone_addr_kind_t kind
)
4394 uint32_t chunk_pages
= zone
->z_chunk_pages
;
4396 assert(pages
% chunk_pages
== 0);
4397 for (; pages
> 0; pages
-= chunk_pages
, addr
+= ptoa(chunk_pages
)) {
4398 zcram_and_lock(zone
, addr
, chunk_pages
, 0, chunk_pages
, kind
);
4404 zone_cram_foreign(zone_t zone
, vm_offset_t newmem
, vm_size_t size
)
4406 uint32_t pages
= (uint32_t)atop(size
);
4408 if (!from_zone_map(newmem
, size
, ZONE_ADDR_FOREIGN
)) {
4409 panic("zone_cram_foreign: foreign memory [%p] being crammed is "
4410 "outside of expected range", (void *)newmem
);
4412 if (!zone
->z_allows_foreign
) {
4413 panic("zone_cram_foreign: foreign memory [%p] being crammed in "
4414 "zone '%s%s' not expecting it", (void *)newmem
,
4415 zone_heap_name(zone
), zone_name(zone
));
4417 if (size
% ptoa(zone
->z_chunk_pages
)) {
4418 panic("zone_cram_foreign: foreign memory [%p] being crammed has "
4419 "invalid size %zx", (void *)newmem
, (size_t)size
);
4421 if (startup_phase
>= STARTUP_SUB_ZALLOC
) {
4422 panic("zone_cram_foreign: foreign memory [%p] being crammed "
4423 "after zalloc is initialized", (void *)newmem
);
4426 bzero((void *)newmem
, size
);
4427 zcram(zone
, newmem
, pages
, ZONE_ADDR_FOREIGN
);
4431 zone_fill_initially(zone_t zone
, vm_size_t nelems
)
4433 kma_flags_t kmaflags
;
4438 assert(!zone
->z_permanent
&& !zone
->collectable
&& !zone
->z_destructible
);
4439 assert(zone
->z_elems_avail
== 0);
4441 kmaflags
= zone_kma_flags(zone
, Z_WAITOK
) | KMA_PERMANENT
;
4442 pages
= zone_alloc_pages_for_nelems(zone
, nelems
);
4443 kr
= kernel_memory_allocate(zone_submap(zone
), &addr
, ptoa(pages
),
4444 0, kmaflags
, VM_KERN_MEMORY_ZONE
);
4445 if (kr
!= KERN_SUCCESS
) {
4446 panic("kernel_memory_allocate() of %u pages failed", pages
);
4449 zone_meta_populate(addr
, ptoa(pages
));
4450 zcram(zone
, addr
, pages
, ZONE_ADDR_NATIVE
);
4454 zone_allocate_va(zone_t z
, zalloc_flags_t flags
)
4456 kma_flags_t kmaflags
= zone_kma_flags(z
, flags
) | KMA_VAONLY
;
4457 vm_size_t size
= ptoa(z
->z_chunk_pages
);
4461 kr
= kernel_memory_allocate(zone_submap(z
), &addr
, size
, 0,
4462 kmaflags
, VM_KERN_MEMORY_ZONE
);
4465 if (kr
== KERN_NO_SPACE
&& z
->z_replenishes
) {
4467 * On 32bit the zone submaps do not have as much VA
4468 * available, so use the VA reserved map for this
4471 vm_map_t map
= zone_submaps
[Z_SUBMAP_IDX_VA_RESERVE
];
4472 kr
= kernel_memory_allocate(map
, &addr
, size
, 0,
4473 kmaflags
, VM_KERN_MEMORY_ZONE
);
4477 if (kr
== KERN_SUCCESS
) {
4478 #if ZALLOC_EARLY_GAPS
4479 if (__improbable(zone_caching_disabled
< 0)) {
4480 zone_allocate_random_early_gap(z
);
4482 #endif /* ZALLOC_EARLY_GAPS */
4483 zone_meta_populate(addr
, size
);
4487 panic_include_zprint
= TRUE
;
4489 if ((zleak_state
& ZLEAK_STATE_ACTIVE
)) {
4490 panic_include_ztrace
= TRUE
;
4492 #endif /* CONFIG_ZLEAKS */
4493 zone_t zone_largest
= zone_find_largest();
4494 panic("zalloc: zone map exhausted while allocating from zone [%s%s], "
4495 "likely due to memory leak in zone [%s%s] "
4496 "(%luM, %d elements allocated)",
4497 zone_heap_name(z
), zone_name(z
),
4498 zone_heap_name(zone_largest
), zone_name(zone_largest
),
4499 (unsigned long)zone_size_wired(zone_largest
) >> 20,
4500 zone_count_allocated(zone_largest
));
4504 zone_expand_pred_nope(__unused zone_t z
)
4510 ZONE_TRACE_VM_KERN_REQUEST_START(vm_size_t size
)
4512 #if DEBUG || DEVELOPMENT
4513 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_START
,
4521 ZONE_TRACE_VM_KERN_REQUEST_END(uint32_t pages
)
4523 #if DEBUG || DEVELOPMENT
4524 task_t task
= current_task();
4525 if (pages
&& task
) {
4526 ledger_credit(task
->ledger
, task_ledgers
.pages_grabbed_kern
, pages
);
4528 VM_DEBUG_CONSTANT_EVENT(vm_kern_request
, VM_KERN_REQUEST
, DBG_FUNC_END
,
4536 zone_expand_locked(zone_t z
, zalloc_flags_t flags
, bool (*pred
)(zone_t
))
4538 thread_t self
= current_thread();
4539 bool vm_priv
= (self
->options
& TH_OPT_VMPRIV
);
4544 /* NULL pred means "try just once" */
4545 pred
= zone_expand_pred_nope
;
4546 } else if (!pred(z
)) {
4550 if (vm_priv
&& !z
->z_expander_vm_priv
) {
4552 * Claim the vm priv overcommit slot
4554 * We do not track exact ownership for VM privileged
4555 * threads, so use the rwlock boost as a stop-gap
4558 set_thread_rwlock_boost();
4559 z
->z_expander_vm_priv
= true;
4560 clear_vm_priv
= true;
4562 clear_vm_priv
= false;
4565 if (z
->z_expander
== NULL
) {
4566 z
->z_expander
= self
;
4569 if (clear_vm_priv
) {
4573 if (flags
& Z_NOPAGEWAIT
) {
4577 z
->z_expanding_wait
= true;
4578 lck_spin_sleep_with_inheritor(&z
->z_lock
, LCK_SLEEP_DEFAULT
,
4579 &z
->z_expander
, z
->z_expander
,
4580 TH_UNINT
, TIMEOUT_WAIT_FOREVER
);
4584 struct zone_page_metadata
*meta
= NULL
;
4585 uint32_t new_va
= 0, cur_pages
= 0, min_pages
= 0, pages
= 0;
4586 vm_page_t page_list
= NULL
;
4587 vm_offset_t addr
= 0;
4591 * While we hold the zone lock, look if there's VA we can:
4592 * - complete from partial pages,
4593 * - reuse from the sequester list.
4595 * When the page is being populated we pretend we allocated
4596 * an extra element so that zone_gc() can't attempt to free
4597 * the chunk (as it could become empty while we wait for pages).
4599 if (!zone_pva_is_null(z
->z_pageq_va
)) {
4600 meta
= zone_meta_queue_pop_native(z
,
4601 &z
->z_pageq_va
, &addr
);
4602 if (meta
->zm_chunk_len
== ZM_SECONDARY_PAGE
) {
4603 cur_pages
= meta
->zm_page_index
;
4605 addr
-= ptoa(cur_pages
);
4606 zone_meta_lock_in_partial(z
, meta
, cur_pages
);
4612 * Do the zone leak activation here because zleak_activate()
4613 * may block, and can't be done on the way out.
4615 * Trigger jetsams via the vm_pageout_garbage_collect thread if
4616 * we're running out of zone memory
4618 zleak_activate_if_needed();
4619 if (zone_map_nearing_exhaustion()) {
4620 thread_wakeup((event_t
)&vm_pageout_garbage_collect
);
4624 * And now allocate pages to populate our VA.
4627 min_pages
= z
->z_chunk_pages
;
4629 min_pages
= (uint32_t)atop(round_page(zone_elem_size(z
)));
4632 ZONE_TRACE_VM_KERN_REQUEST_START(ptoa(z
->z_chunk_pages
- cur_pages
));
4634 while (pages
< z
->z_chunk_pages
- cur_pages
) {
4635 vm_page_t m
= vm_page_grab();
4639 m
->vmp_snext
= page_list
;
4641 vm_page_zero_fill(m
);
4645 if (pages
>= min_pages
&& (vm_pool_low() || waited
)) {
4649 if ((flags
& Z_NOPAGEWAIT
) == 0) {
4656 * Undo everything and bail out:
4659 * - undo the fake allocation if any
4660 * - put the VA back on the VA page queue.
4662 vm_page_free_list(page_list
, FALSE
);
4663 ZONE_TRACE_VM_KERN_REQUEST_END(pages
);
4668 zone_meta_unlock_from_partial(z
, meta
, cur_pages
);
4671 zone_meta_queue_push(z
, &z
->z_pageq_va
,
4678 * If we didn't find pre-allocated VA, then allocate a chunk
4682 addr
= zone_allocate_va(z
, flags
);
4683 meta
= zone_meta_from_addr(addr
);
4684 new_va
= z
->z_chunk_pages
;
4687 kernel_memory_populate_with_pages(zone_submap(z
),
4688 addr
+ ptoa(cur_pages
), ptoa(pages
), page_list
,
4689 zone_kma_flags(z
, flags
), VM_KERN_MEMORY_ZONE
);
4691 ZONE_TRACE_VM_KERN_REQUEST_END(pages
);
4693 zcram_and_lock(z
, addr
, new_va
, cur_pages
, cur_pages
+ pages
,
4698 zleak_track_if_needed(z
);
4700 if (clear_vm_priv
) {
4701 z
->z_expander_vm_priv
= false;
4702 clear_thread_rwlock_boost();
4704 if (z
->z_expander
== self
) {
4705 z
->z_expander
= THREAD_NULL
;
4707 if (z
->z_expanding_wait
) {
4708 z
->z_expanding_wait
= false;
4709 wakeup_all_with_inheritor(&z
->z_expander
, THREAD_AWAKENED
);
4714 zalloc_needs_refill(zone_t zone
)
4716 if (zone
->z_elems_free
> zone
->z_elems_rsv
) {
4719 if (zone
->z_wired_cur
< zone
->z_wired_max
) {
4722 if (zone
->exhaustible
) {
4725 if (zone
->expandable
) {
4727 * If we're expandable, just don't go through this again.
4729 zone
->z_wired_max
= ~0u;
4734 panic_include_zprint
= true;
4736 if (zleak_state
& ZLEAK_STATE_ACTIVE
) {
4737 panic_include_ztrace
= true;
4739 #endif /* CONFIG_ZLEAKS */
4740 panic("zone '%s%s' exhausted", zone_heap_name(zone
), zone_name(zone
));
4744 zone_expand_async(__unused thread_call_param_t p0
, __unused thread_call_param_t p1
)
4747 if (z
->no_callout
) {
4748 /* z_async_refilling will never be set */
4752 if (z
->z_replenishes
) {
4753 /* those use the zone_replenish_thread */
4758 if (z
->z_self
&& z
->z_async_refilling
) {
4759 z
->z_async_refilling
= false;
4760 zone_expand_locked(z
, Z_WAITOK
, zalloc_needs_refill
);
4767 zone_expand_async_schedule_if_needed(zone_t zone
)
4769 if (zone
->z_elems_free
> zone
->z_elems_rsv
|| zone
->z_async_refilling
||
4774 if (!zone
->expandable
&& zone
->z_wired_cur
>= zone
->z_wired_max
) {
4778 if (zone
->z_elems_free
== 0 || !vm_pool_low()) {
4779 zone
->z_async_refilling
= true;
4780 thread_call_enter(&zone_expand_callout
);
4784 #endif /* !ZALLOC_TEST */
4785 #pragma mark zone replenishing (VM allocations)
4789 * Tracks how many zone_replenish threads are active, because zone_gc() wants
4790 * for those to be finished before it proceeds.
4792 * This counts how many replenish threads are active in
4793 * ZONE_REPLENISH_ACTIVE_INC increments,
4794 * and uses the low bit to track if there are any waiters.
4796 #define ZONE_REPLENISH_ACTIVE_NONE 0u
4797 #define ZONE_REPLENISH_ACTIVE_WAITER_BIT 1u
4798 #define ZONE_REPLENISH_ACTIVE_INC 2u
4799 #define ZONE_REPLENISH_ACTIVE_MASK (~ZONE_REPLENISH_ACTIVE_WAITER_BIT)
4800 static unsigned _Atomic zone_replenish_active
;
4801 static unsigned zone_replenish_wakeups
;
4802 static unsigned zone_replenish_wakeups_initiated
;
4803 static unsigned zone_replenish_throttle_count
;
4805 #define ZONE_REPLENISH_TARGET (16 * 1024)
4808 zone_replenish_wait_if_needed(void)
4811 * This check can be racy, the reserves ought to be enough
4812 * to compensate for a little race
4814 while (os_atomic_load(&zone_replenish_active
, relaxed
) !=
4815 ZONE_REPLENISH_ACTIVE_NONE
) {
4816 unsigned o_active
, n_active
;
4818 assert_wait(&zone_replenish_active
, THREAD_UNINT
);
4820 os_atomic_rmw_loop(&zone_replenish_active
, o_active
, n_active
, relaxed
, {
4821 if (o_active
== ZONE_REPLENISH_ACTIVE_NONE
) {
4822 os_atomic_rmw_loop_give_up({
4823 clear_wait(current_thread(), THREAD_AWAKENED
);
4827 if (o_active
& ZONE_REPLENISH_ACTIVE_WAITER_BIT
) {
4828 os_atomic_rmw_loop_give_up(break);
4830 n_active
= o_active
| ZONE_REPLENISH_ACTIVE_WAITER_BIT
;
4832 thread_block(THREAD_CONTINUE_NULL
);
4836 __attribute__((noinline
))
4838 zone_replenish_locked(zone_t zone
)
4840 thread_t thr
= current_thread();
4843 zone_replenish_wakeups
++;
4846 * We'll let threads continue to allocate under the reserve:
4847 * - until it depleted to 50% for regular threads,
4848 * - until it depleted to 25% for VM_PRIV threads.
4850 * After that only TH_OPT_ZONE_PRIV threads may continue.
4852 if (thr
->options
& TH_OPT_VMPRIV
) {
4853 min_free
= zone
->z_elems_rsv
/ 4;
4855 min_free
= zone
->z_elems_rsv
/ 2;
4858 while (zone
->z_elems_free
<= zone
->z_elems_rsv
) {
4860 * Wakeup the replenish thread if not running.
4862 if (!zone
->z_async_refilling
) {
4863 os_atomic_add(&zone_replenish_active
,
4864 ZONE_REPLENISH_ACTIVE_INC
, relaxed
);
4865 zone
->z_async_refilling
= true;
4866 zone_replenish_wakeups_initiated
++;
4867 thread_wakeup(&zone
->z_elems_rsv
);
4870 if (zone
->z_elems_free
> min_free
) {
4875 * TH_OPT_ZONE_PRIV threads are the GC thread and a replenish
4878 * Replenish threads *need* to use the reserve. GC threads need
4879 * to get through the current allocation, but then will wait at
4880 * a higher level after they've dropped any locks which would
4881 * deadlock the replenish thread.
4883 * The value of (refill_level / 2) in the previous bit of code
4884 * should have given us headroom even though this thread didn't
4887 if (thr
->options
& TH_OPT_ZONE_PRIV
) {
4888 assert(zone
->z_elems_free
!= 0);
4892 if (startup_phase
< STARTUP_SUB_MACH_IPC
) {
4893 panic("vm_map_steal_memory didn't steal enough memory: "
4894 "trying to grow [%s%s] before the scheduler has started",
4895 zone_heap_name(zone
), zone_name(zone
));
4899 * Wait for the replenish threads to add more elements
4900 * for us to allocate from.
4902 zone_replenish_throttle_count
++;
4903 zone
->z_replenish_wait
= true;
4904 assert_wait_timeout(zone
, THREAD_UNINT
, 1, NSEC_PER_MSEC
);
4906 thread_block(THREAD_CONTINUE_NULL
);
4908 zone
->z_replenish_wait
= false;
4910 assert(zone
->z_self
== zone
);
4915 zone_replenish_needed(zone_t z
)
4917 return z
->z_elems_free
<= z
->z_elems_rsv
;
4921 * High priority VM privileged thread used to asynchronously refill a given zone.
4922 * These are needed for data structures used by the lower level VM itself. The
4923 * replenish thread maintains a reserve of elements, so that the VM will never
4924 * block in the zone allocator.
4928 zone_replenish_thread(void *_z
, wait_result_t __unused wr
)
4930 unsigned o_active
, n_active
;
4934 assert(z
->z_self
== z
);
4935 assert(z
->z_async_refilling
&& z
->z_replenishes
);
4937 zone_expand_locked(z
, Z_WAITOK
, zone_replenish_needed
);
4939 if (z
->z_replenish_wait
) {
4940 /* Wakeup any potentially throttled allocations */
4941 z
->z_replenish_wait
= false;
4945 /* wakeup zone_reclaim() callers that were possibly waiting */
4946 os_atomic_rmw_loop(&zone_replenish_active
, o_active
, n_active
, relaxed
, {
4947 if (os_sub_overflow(o_active
, ZONE_REPLENISH_ACTIVE_INC
, &n_active
)) {
4948 panic("zone_replenish_active corrupt: %d", o_active
);
4950 if ((n_active
& ZONE_REPLENISH_ACTIVE_MASK
) == 0) {
4951 n_active
= ZONE_REPLENISH_ACTIVE_NONE
;
4955 if (n_active
== ZONE_REPLENISH_ACTIVE_NONE
&&
4956 (o_active
& ZONE_REPLENISH_ACTIVE_WAITER_BIT
)) {
4957 thread_wakeup(&zone_replenish_active
);
4960 z
->z_async_refilling
= false;
4961 assert_wait(&z
->z_elems_rsv
, THREAD_UNINT
);
4965 thread_block_parameter(zone_replenish_thread
, z
);
4966 __builtin_unreachable();
4970 zone_replenish_configure(zone_t z
)
4974 char name
[MAXTHREADNAMESIZE
];
4977 assert(!z
->z_replenishes
&& !z
->z_destructible
);
4978 z
->z_elems_rsv
= (uint16_t)(ZONE_REPLENISH_TARGET
/ zone_elem_size(z
));
4979 z
->z_replenishes
= true;
4980 os_atomic_add(&zone_replenish_active
, ZONE_REPLENISH_ACTIVE_INC
, relaxed
);
4981 z
->z_async_refilling
= true;
4984 kr
= kernel_thread_create(zone_replenish_thread
, z
, MAXPRI_KERNEL
, &th
);
4985 if (kr
!= KERN_SUCCESS
) {
4986 panic("zone_replenish_configure, thread create: 0x%x", kr
);
4988 /* make sure this thread can't lose its stack */
4989 assert(th
->reserved_stack
== th
->kernel_stack
);
4991 snprintf(name
, sizeof(name
), "z_replenish(%s)", zone_name(z
));
4992 thread_set_thread_name(th
, name
);
4994 thread_mtx_lock(th
);
4995 th
->options
|= TH_OPT_VMPRIV
| TH_OPT_ZONE_PRIV
;
4997 thread_mtx_unlock(th
);
4999 thread_deallocate(th
);
5003 #endif /* !ZALLOC_TEST */
5004 #pragma mark zone jetsam integration
5008 * We're being very conservative here and picking a value of 95%. We might need to lower this if
5009 * we find that we're not catching the problem and are still hitting zone map exhaustion panics.
5011 #define ZONE_MAP_JETSAM_LIMIT_DEFAULT 95
5014 * Trigger zone-map-exhaustion jetsams if the zone map is X% full, where X=zone_map_jetsam_limit.
5015 * Can be set via boot-arg "zone_map_jetsam_limit". Set to 95% by default.
5017 TUNABLE_WRITEABLE(unsigned int, zone_map_jetsam_limit
, "zone_map_jetsam_limit",
5018 ZONE_MAP_JETSAM_LIMIT_DEFAULT
);
5021 get_zone_map_size(uint64_t *current_size
, uint64_t *capacity
)
5023 vm_offset_t phys_pages
= os_atomic_load(&zones_phys_page_mapped_count
, relaxed
);
5024 *current_size
= ptoa_64(phys_pages
);
5025 *capacity
= ptoa_64(zone_phys_mapped_max_pages
);
5029 get_largest_zone_info(char *zone_name
, size_t zone_name_len
, uint64_t *zone_size
)
5031 zone_t largest_zone
= zone_find_largest();
5034 * Append kalloc heap name to zone name (if zone is used by kalloc)
5036 snprintf(zone_name
, zone_name_len
, "%s%s",
5037 zone_heap_name(largest_zone
), largest_zone
->z_name
);
5039 *zone_size
= zone_size_wired(largest_zone
);
5043 zone_map_nearing_exhaustion(void)
5045 uint64_t phys_pages
= os_atomic_load(&zones_phys_page_mapped_count
, relaxed
);
5046 return phys_pages
* 100 > zone_phys_mapped_max_pages
* zone_map_jetsam_limit
;
5050 #define VMENTRY_TO_VMOBJECT_COMPARISON_RATIO 98
5053 * Tries to kill a single process if it can attribute one to the largest zone. If not, wakes up the memorystatus thread
5054 * to walk through the jetsam priority bands and kill processes.
5057 kill_process_in_largest_zone(void)
5060 zone_t largest_zone
= zone_find_largest();
5062 printf("zone_map_exhaustion: Zone mapped %lld of %lld, used %lld, capacity %lld [jetsam limit %d%%]\n",
5063 ptoa_64(os_atomic_load(&zones_phys_page_mapped_count
, relaxed
)),
5064 ptoa_64(zone_phys_mapped_max_pages
),
5065 (uint64_t)zone_submaps_approx_size(),
5066 (uint64_t)(zone_foreign_size() + zone_native_size()),
5067 zone_map_jetsam_limit
);
5068 printf("zone_map_exhaustion: Largest zone %s%s, size %lu\n", zone_heap_name(largest_zone
),
5069 largest_zone
->z_name
, (uintptr_t)zone_size_wired(largest_zone
));
5072 * We want to make sure we don't call this function from userspace.
5073 * Or we could end up trying to synchronously kill the process
5074 * whose context we're in, causing the system to hang.
5076 assert(current_task() == kernel_task
);
5079 * If vm_object_zone is the largest, check to see if the number of
5080 * elements in vm_map_entry_zone is comparable.
5082 * If so, consider vm_map_entry_zone as the largest. This lets us target
5083 * a specific process to jetsam to quickly recover from the zone map
5086 if (largest_zone
== vm_object_zone
) {
5087 unsigned int vm_object_zone_count
= zone_count_allocated(vm_object_zone
);
5088 unsigned int vm_map_entry_zone_count
= zone_count_allocated(vm_map_entry_zone
);
5089 /* Is the VM map entries zone count >= 98% of the VM objects zone count? */
5090 if (vm_map_entry_zone_count
>= ((vm_object_zone_count
* VMENTRY_TO_VMOBJECT_COMPARISON_RATIO
) / 100)) {
5091 largest_zone
= vm_map_entry_zone
;
5092 printf("zone_map_exhaustion: Picking VM map entries as the zone to target, size %lu\n",
5093 (uintptr_t)zone_size_wired(largest_zone
));
5097 /* TODO: Extend this to check for the largest process in other zones as well. */
5098 if (largest_zone
== vm_map_entry_zone
) {
5099 pid
= find_largest_process_vm_map_entries();
5101 printf("zone_map_exhaustion: Nothing to do for the largest zone [%s%s]. "
5102 "Waking up memorystatus thread.\n", zone_heap_name(largest_zone
),
5103 largest_zone
->z_name
);
5105 if (!memorystatus_kill_on_zone_map_exhaustion(pid
)) {
5106 printf("zone_map_exhaustion: Call to memorystatus failed, victim pid: %d\n", pid
);
5110 #endif /* !ZALLOC_TEST */
5120 * The codepath for zone frees.
5123 * There are 4 major ways to allocate memory that end up in the zone allocator:
5125 * - @c zfree_percpu()
5127 * - @c zfree_permanent()
5129 * While permanent zones have their own allocation scheme, all other codepaths
5130 * will eventually go through the @c zfree_ext() choking point.
5132 * Ignoring the @c gzalloc_free() codepath, the decision tree looks like this:
5135 * ├───> zfree_cached() ────────────────╮
5138 * │ ├───> zfree_cached_slow() ───┤
5141 * ╰───────┴───> zfree_item() ──────────┴───>
5144 * @c zfree_ext() takes care of all the generic work to perform on an element
5145 * before it is freed (zeroing, logging, tagging, ...) then will hand it off to:
5146 * - @c zfree_item() if zone caching is off
5147 * - @c zfree_cached() if zone caching is on.
5149 * @c zfree_cached can take a number of decisions:
5150 * - a fast path if the (f) or (a) magazines have space (preemption disabled),
5151 * - using the cpu local or recirculation depot calling @c zfree_cached_slow(),
5152 * - falling back to @c zfree_item() when CPU caching has been disabled.
5156 * Called from zfree() to add the element being freed to the KASan quarantine.
5158 * Returns true if the newly-freed element made it into the quarantine without
5159 * displacing another, false otherwise. In the latter case, addrp points to the
5160 * address of the displaced element, which will be freed by the zone.
5163 kasan_quarantine_freed_element(
5164 zone_t
*zonep
, /* the zone the element is being freed to */
5165 void **addrp
) /* address of the element being freed */
5167 zone_t zone
= *zonep
;
5168 void *addr
= *addrp
;
5171 * Resize back to the real allocation size and hand off to the KASan
5172 * quarantine. `addr` may then point to a different allocation, if the
5173 * current element replaced another in the quarantine. The zone then
5174 * takes ownership of the swapped out free element.
5176 vm_size_t usersz
= zone_elem_size(zone
) - 2 * zone
->z_kasan_redzone
;
5177 vm_size_t sz
= usersz
;
5179 if (addr
&& zone
->z_kasan_redzone
) {
5180 kasan_check_free((vm_address_t
)addr
, usersz
, KASAN_HEAP_ZALLOC
);
5181 addr
= (void *)kasan_dealloc((vm_address_t
)addr
, &sz
);
5182 assert(sz
== zone_elem_size(zone
));
5184 if (addr
&& !zone
->kasan_noquarantine
) {
5185 kasan_free(&addr
, &sz
, KASAN_HEAP_ZALLOC
, zonep
, usersz
, true);
5190 if (addr
&& zone
->kasan_noquarantine
) {
5191 kasan_unpoison(addr
, zone_elem_size(zone
));
5197 #endif /* KASAN_ZALLOC */
5199 __header_always_inline
void
5200 zfree_drop(zone_t zone
, struct zone_page_metadata
*meta
, zone_element_t ze
,
5203 vm_offset_t esize
= zone_elem_size(zone
);
5205 if (zone_meta_mark_free(meta
, ze
) == recirc
) {
5206 zone_meta_double_free_panic(zone
, ze
, __func__
);
5209 vm_offset_t old_size
= meta
->zm_alloc_size
;
5210 vm_offset_t max_size
= ptoa(meta
->zm_chunk_len
) + ZM_ALLOC_SIZE_LOCK
;
5211 vm_offset_t new_size
= zone_meta_alloc_size_sub(zone
, meta
, esize
);
5213 if (new_size
== 0) {
5214 /* whether the page was on the intermediate or all_used, queue, move it to free */
5215 zone_meta_requeue(zone
, &zone
->z_pageq_empty
, meta
);
5216 zone
->z_wired_empty
+= meta
->zm_chunk_len
;
5217 } else if (old_size
+ esize
> max_size
) {
5218 /* first free element on page, move from all_used */
5219 zone_meta_requeue(zone
, &zone
->z_pageq_partial
, meta
);
5224 zfree_item(zone_t zone
, struct zone_page_metadata
*meta
, zone_element_t ze
)
5226 /* transfer preemption count to lock */
5227 zone_lock_nopreempt_check_contention(zone
, NULL
);
5229 zfree_drop(zone
, meta
, ze
, false);
5230 zone_elems_free_add(zone
, 1);
5235 __attribute__((noinline
))
5237 zfree_cached_slow(zone_t zone
, struct zone_page_metadata
*meta
,
5238 zone_element_t ze
, zone_cache_t cache
)
5240 struct zone_depot mags
= STAILQ_HEAD_INITIALIZER(mags
);
5241 zone_magazine_t mag
= NULL
;
5244 if (zone_meta_is_free(meta
, ze
)) {
5245 zone_meta_double_free_panic(zone
, ze
, __func__
);
5248 if (zone
== zc_magazine_zone
) {
5249 mag
= (zone_magazine_t
)zone_element_addr(ze
,
5250 zone_elem_size(zone
));
5252 kasan_poison_range((vm_offset_t
)mag
, zone_elem_size(zone
),
5256 mag
= zone_magazine_alloc(Z_NOWAIT
);
5257 if (__improbable(mag
== NULL
)) {
5258 return zfree_item(zone
, meta
, ze
);
5261 mag
->zm_elems
[0] = ze
;
5264 mag
= zone_magazine_replace(&cache
->zc_free_cur
,
5265 &cache
->zc_free_elems
, mag
);
5267 z_debug_assert(cache
->zc_free_cur
<= 1);
5268 z_debug_assert(mag
->zm_cur
== zc_mag_size());
5270 STAILQ_INSERT_HEAD(&mags
, mag
, zm_link
);
5273 if (cache
->zc_depot_max
>= 2 * zc_mag_size()) {
5275 * If we can use the local depot (zc_depot_max allows for
5276 * 2 magazines worth of elements) then:
5278 * 1. if we have space for an extra depot locally,
5279 * push it, and leave.
5281 * 2. if we overflow, then take (1 / zc_recirc_denom)
5282 * of the depot out, in order to migrate it to the
5283 * recirculation depot.
5285 zone_depot_lock_nopreempt(cache
);
5287 if ((cache
->zc_depot_cur
+ 2) * zc_mag_size() <=
5288 cache
->zc_depot_max
) {
5289 cache
->zc_depot_cur
++;
5290 STAILQ_INSERT_TAIL(&cache
->zc_depot
, mag
, zm_link
);
5291 return zone_depot_unlock(cache
);
5294 while (zc_recirc_denom
* cache
->zc_depot_cur
* zc_mag_size() >=
5295 (zc_recirc_denom
- 1) * cache
->zc_depot_max
) {
5296 mag
= STAILQ_FIRST(&cache
->zc_depot
);
5297 STAILQ_REMOVE_HEAD(&cache
->zc_depot
, zm_link
);
5298 STAILQ_INSERT_TAIL(&mags
, mag
, zm_link
);
5299 cache
->zc_depot_cur
--;
5303 zone_depot_unlock(cache
);
5305 enable_preemption();
5309 * Preflight validity of all the elements before we touch the zone
5310 * metadata, and then insert them into the recirculation depot.
5312 STAILQ_FOREACH(mag
, &mags
, zm_link
) {
5313 for (uint16_t i
= 0; i
< zc_mag_size(); i
++) {
5314 zone_element_validate(zone
, mag
->zm_elems
[i
]);
5318 zone_lock_check_contention(zone
, cache
);
5320 STAILQ_FOREACH(mag
, &mags
, zm_link
) {
5321 for (uint16_t i
= 0; i
< zc_mag_size(); i
++) {
5322 zone_element_t e
= mag
->zm_elems
[i
];
5324 if (!zone_meta_mark_free(zone_meta_from_element(e
), e
)) {
5325 zone_meta_double_free_panic(zone
, e
, __func__
);
5329 STAILQ_CONCAT(&zone
->z_recirc
, &mags
);
5330 zone
->z_recirc_cur
+= n
;
5332 zone_elems_free_add(zone
, n
* zc_mag_size());
5338 zfree_cached(zone_t zone
, struct zone_page_metadata
*meta
, zone_element_t ze
)
5340 zone_cache_t cache
= zpercpu_get(zone
->z_pcpu_cache
);
5342 if (cache
->zc_free_cur
>= zc_mag_size()) {
5343 if (cache
->zc_alloc_cur
>= zc_mag_size()) {
5344 return zfree_cached_slow(zone
, meta
, ze
, cache
);
5346 zone_cache_swap_magazines(cache
);
5349 if (__improbable(cache
->zc_alloc_elems
== NULL
)) {
5350 return zfree_item(zone
, meta
, ze
);
5353 if (zone_meta_is_free(meta
, ze
)) {
5354 zone_meta_double_free_panic(zone
, ze
, __func__
);
5357 uint16_t idx
= cache
->zc_free_cur
++;
5358 if (idx
>= zc_mag_size()) {
5359 zone_accounting_panic(zone
, "zc_free_cur overflow");
5361 cache
->zc_free_elems
[idx
] = ze
;
5363 enable_preemption();
5367 * The function is noinline when zlog can be used so that the backtracing can
5368 * reliably skip the zfree_ext() and zfree_log_trace()
5371 #if ZONE_ENABLE_LOGGING
5372 __attribute__((noinline
))
5373 #endif /* ZONE_ENABLE_LOGGING */
5375 zfree_ext(zone_t zone
, zone_stats_t zstats
, void *addr
)
5377 struct zone_page_metadata
*page_meta
;
5378 vm_offset_t elem
= (vm_offset_t
)addr
;
5379 vm_size_t elem_size
= zone_elem_size(zone
);
5382 DTRACE_VM2(zfree
, zone_t
, zone
, void*, addr
);
5383 TRACE_MACHLEAKS(ZFREE_CODE
, ZFREE_CODE_2
, elem_size
, elem
);
5384 #if VM_MAX_TAG_ZONES
5385 if (__improbable(zone
->tags
)) {
5386 vm_tag_t tag
= *ztSlot(zone
, elem
) >> 1;
5387 // set the tag with b0 clear so the block remains inuse
5388 *ztSlot(zone
, elem
) = 0xFFFE;
5389 vm_tag_update_zone_size(tag
, zone
->tag_zone_index
,
5392 #endif /* VM_MAX_TAG_ZONES */
5395 if (kasan_quarantine_freed_element(&zone
, &addr
)) {
5399 * kasan_quarantine_freed_element() might return a different
5400 * {zone, addr} than the one being freed for kalloc heaps.
5402 * Make sure we reload everything.
5404 elem
= (vm_offset_t
)addr
;
5405 elem_size
= zone_elem_size(zone
);
5409 * Zone leak detection: un-track the allocation
5411 if (__improbable(zone
->zleak_on
)) {
5412 zleak_free(elem
, elem_size
);
5414 #endif /* CONFIG_ZLEAKS */
5415 #if ZONE_ENABLE_LOGGING
5416 if (__improbable(DO_LOGGING(zone
))) {
5417 zfree_log_trace(zone
, elem
, __builtin_frame_address(0));
5419 #endif /* ZONE_ENABLE_LOGGING */
5421 if (__improbable(zone
->gzalloc_tracked
)) {
5422 return gzalloc_free(zone
, zstats
, addr
);
5424 #endif /* CONFIG_GZALLOC */
5426 page_meta
= zone_element_resolve(zone
, elem
, elem_size
, &ze
);
5427 ze
.ze_value
|= zfree_clear_or_poison(zone
, elem
, elem_size
);
5429 if (zone
->z_percpu
) {
5430 zpercpu_foreach_cpu(i
) {
5431 kasan_poison_range(elem
+ ptoa(i
), elem_size
,
5435 kasan_poison_range(elem
, elem_size
, ASAN_HEAP_FREED
);
5439 disable_preemption();
5440 zpercpu_get(zstats
)->zs_mem_freed
+= elem_size
;
5442 if (zone
->z_pcpu_cache
) {
5443 return zfree_cached(zone
, page_meta
, ze
);
5446 return zfree_item(zone
, page_meta
, ze
);
5450 (zfree
)(union zone_or_view zov
, void *addr
)
5452 zone_t zone
= zov
.zov_view
->zv_zone
;
5453 zone_stats_t zstats
= zov
.zov_view
->zv_stats
;
5454 assert(!zone
->z_percpu
);
5455 zfree_ext(zone
, zstats
, addr
);
5459 zfree_percpu(union zone_or_view zov
, void *addr
)
5461 zone_t zone
= zov
.zov_view
->zv_zone
;
5462 zone_stats_t zstats
= zov
.zov_view
->zv_stats
;
5463 assert(zone
->z_percpu
);
5464 zfree_ext(zone
, zstats
, (void *)__zpcpu_demangle(addr
));
5468 #endif /* !ZALLOC_TEST */
5477 * The codepath for zone allocations.
5480 * There are 4 major ways to allocate memory that end up in the zone allocator:
5481 * - @c zalloc(), @c zalloc_flags(), ...
5482 * - @c zalloc_percpu()
5484 * - @c zalloc_permanent()
5486 * While permanent zones have their own allocation scheme, all other codepaths
5487 * will eventually go through the @c zalloc_ext() choking point.
5489 * Ignoring the @c zalloc_gz() codepath, the decision tree looks like this:
5493 * ├───> zalloc_cached() ──────> zalloc_cached_fast() ───╮
5496 * │ ╰───> zalloc_cached_slow() ───╯ │
5498 * │<─────────────────╮ ├─────────────╮ │
5501 * │<───────╮ ╭──> zalloc_item_slow() ────┤ │
5504 * ╰───> zalloc_item() ──────────> zalloc_item_fast() ───┤
5511 * The @c zalloc_item() track is used when zone caching is off:
5512 * - @c zalloc_item_fast() is used when there are enough elements available,
5513 * - @c zalloc_item_slow() is used when a refill is needed, which can cause
5514 * the zone to grow. This is the only codepath that refills.
5516 * This track uses the zone lock for serialization:
5517 * - taken in @c zalloc_item(),
5518 * - maintained during @c zalloc_item_slow() (possibly dropped and re-taken),
5519 * - dropped in @c zalloc_item_fast().
5522 * The @c zalloc_cached() track is used when zone caching is on:
5523 * - @c zalloc_cached_fast() is taken when the cache has elements,
5524 * - @c zalloc_cached_slow() is taken if a cache refill is needed.
5525 * It can chose many strategies:
5526 * ~ @c zalloc_cached_from_depot() to try to reuse cpu stashed magazines,
5527 * ~ using the global recirculation depot @c z_recirc,
5528 * ~ using zalloc_import() if the zone has enough elements,
5529 * ~ falling back to the @c zalloc_item() track if zone caching is disabled
5530 * due to VM pressure or the zone has no available elements.
5532 * This track disables preemption for serialization:
5533 * - preemption is disabled in @c zalloc_cached(),
5534 * - kept disabled during @c zalloc_cached_slow(), converted into a zone lock
5535 * if switching to @c zalloc_item_slow(),
5536 * - preemption is reenabled in @c zalloc_cached_fast().
5538 * @c zalloc_cached_from_depot() also takes depot locks (taken by the caller,
5539 * released by @c zalloc_cached_from_depot().
5541 * In general the @c zalloc_*_slow() codepaths deal with refilling and will
5542 * tail call into the @c zalloc_*_fast() code to perform the actual allocation.
5544 * @c zalloc_return() is the final function everyone tail calls into,
5545 * which prepares the element for consumption by the caller and deals with
5546 * common treatment (zone logging, tags, kasan, validation, ...).
5550 * @function zalloc_import
5553 * Import @c n elements in the specified array, opposite of @c zfree_drop().
5555 * @param zone The zone to import elements from
5556 * @param elems The array to import into
5557 * @param n The number of elements to import. Must be non zero,
5558 * and smaller than @c zone->z_elems_free.
5560 __header_always_inline
void
5561 zalloc_import(zone_t zone
, zone_element_t
*elems
, uint32_t n
)
5563 vm_size_t esize
= zone_elem_size(zone
);
5566 assertf(STAILQ_EMPTY(&zone
->z_recirc
),
5567 "Trying to import from zone %p [%s%s] with non empty recirc",
5568 zone
, zone_heap_name(zone
), zone_name(zone
));
5571 vm_offset_t page
, eidx
, size
= 0;
5572 struct zone_page_metadata
*meta
;
5574 if (!zone_pva_is_null(zone
->z_pageq_partial
)) {
5575 meta
= zone_pva_to_meta(zone
->z_pageq_partial
);
5576 page
= zone_pva_to_addr(zone
->z_pageq_partial
);
5577 } else if (!zone_pva_is_null(zone
->z_pageq_empty
)) {
5578 meta
= zone_pva_to_meta(zone
->z_pageq_empty
);
5579 page
= zone_pva_to_addr(zone
->z_pageq_empty
);
5580 zone_counter_sub(zone
, z_wired_empty
, meta
->zm_chunk_len
);
5582 zone_accounting_panic(zone
, "z_elems_free corruption");
5585 if (!zone_has_index(zone
, meta
->zm_index
)) {
5586 zone_page_metadata_index_confusion_panic(zone
, page
, meta
);
5589 vm_offset_t old_size
= meta
->zm_alloc_size
;
5590 vm_offset_t max_size
= ptoa(meta
->zm_chunk_len
) + ZM_ALLOC_SIZE_LOCK
;
5593 eidx
= zone_meta_find_and_clear_bit(zone
, meta
);
5594 elems
[i
++] = zone_element_encode(page
, eidx
, ZPM_AUTO
);
5596 } while (i
< n
&& old_size
+ size
+ esize
<= max_size
);
5598 vm_offset_t new_size
= zone_meta_alloc_size_add(zone
, meta
, size
);
5600 if (new_size
+ esize
> max_size
) {
5601 zone_meta_requeue(zone
, &zone
->z_pageq_full
, meta
);
5602 } else if (old_size
== 0) {
5603 /* remove from free, move to intermediate */
5604 zone_meta_requeue(zone
, &zone
->z_pageq_partial
, meta
);
5610 * @function zalloc_return
5613 * Performs the tail-end of the work required on allocations before the caller
5617 * This function is called without any zone lock held,
5618 * and preemption back to the state it had when @c zalloc_ext() was called.
5620 * @param zone The zone we're allocating from.
5621 * @param ze The encoded element we just allocated.
5622 * @param flags The flags passed to @c zalloc_ext() (for Z_ZERO).
5623 * @param elem_size The element size for this zone.
5624 * @param freemag An optional magazine that needs to be freed.
5626 __attribute__((noinline
))
5628 zalloc_return(zone_t zone
, zone_element_t ze
, zalloc_flags_t flags
,
5629 vm_offset_t elem_size
, zone_magazine_t freemag
)
5631 vm_offset_t addr
= zone_element_addr(ze
, elem_size
);
5634 if (zone
->z_percpu
) {
5635 zpercpu_foreach_cpu(i
) {
5636 kasan_poison_range(addr
+ ptoa(i
), elem_size
,
5640 kasan_poison_range(addr
, elem_size
, ASAN_VALID
);
5643 #if ZALLOC_ENABLE_POISONING
5644 zalloc_validate_element(zone
, addr
, elem_size
, zone_element_prot(ze
));
5645 #endif /* ZALLOC_ENABLE_POISONING */
5646 #if ZONE_ENABLE_LOGGING || CONFIG_ZLEAKS
5647 if (__improbable(zalloc_should_log_or_trace_leaks(zone
, elem_size
))) {
5648 zalloc_log_or_trace_leaks(zone
, addr
, __builtin_frame_address(0));
5650 #endif /* ZONE_ENABLE_LOGGING || CONFIG_ZLEAKS */
5652 if (zone
->z_kasan_redzone
) {
5653 addr
= kasan_alloc(addr
, elem_size
,
5654 elem_size
- 2 * zone
->z_kasan_redzone
,
5655 zone
->z_kasan_redzone
);
5656 elem_size
-= 2 * zone
->z_kasan_redzone
;
5659 * Initialize buffer with unique pattern only if memory
5660 * wasn't expected to be zeroed.
5662 if (!zone
->z_free_zeroes
&& !(flags
& Z_ZERO
)) {
5663 kasan_leak_init(addr
, elem_size
);
5665 #endif /* KASAN_ZALLOC */
5666 if ((flags
& Z_ZERO
) && !zone
->z_free_zeroes
) {
5667 bzero((void *)addr
, elem_size
);
5670 #if VM_MAX_TAG_ZONES
5671 if (__improbable(zone
->tags
)) {
5672 vm_tag_t tag
= zalloc_flags_get_tag(flags
);
5673 if (tag
== VM_KERN_MEMORY_NONE
) {
5674 tag
= VM_KERN_MEMORY_KALLOC
;
5676 // set the tag with b0 clear so the block remains inuse
5677 *ztSlot(zone
, addr
) = (vm_tag_t
)(tag
<< 1);
5678 vm_tag_update_zone_size(tag
, zone
->tag_zone_index
,
5681 #endif /* VM_MAX_TAG_ZONES */
5683 TRACE_MACHLEAKS(ZALLOC_CODE
, ZALLOC_CODE_2
, elem_size
, addr
);
5684 DTRACE_VM2(zalloc
, zone_t
, zone
, void*, addr
);
5686 zone_magazine_free(freemag
);
5688 return (void *)addr
;
5693 * @function zalloc_gz
5696 * Performs allocations for zones using gzalloc.
5699 * This function is noinline so that it doesn't affect the codegen
5702 __attribute__((noinline
))
5704 zalloc_gz(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
)
5706 vm_offset_t addr
= gzalloc_alloc(zone
, zstats
, flags
);
5707 return zalloc_return(zone
, zone_element_encode(addr
, 0, ZPM_AUTO
),
5708 flags
, zone_elem_size(zone
), NULL
);
5710 #endif /* CONFIG_GZALLOC */
5713 zalloc_item_fast(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
)
5715 vm_size_t esize
= zone_elem_size(zone
);
5718 zalloc_import(zone
, &ze
, 1);
5719 zone_elems_free_sub(zone
, 1);
5720 zpercpu_get(zstats
)->zs_mem_allocated
+= esize
;
5723 return zalloc_return(zone
, ze
, flags
, esize
, NULL
);
5727 * @function zalloc_item_slow
5730 * Performs allocations when the zone is out of elements.
5733 * This function might drop the lock and reenable preemption,
5734 * which means the per-CPU caching layer or recirculation depot
5735 * might have received elements.
5737 __attribute__((noinline
))
5739 zalloc_item_slow(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
)
5741 if (zone
->z_replenishes
) {
5742 zone_replenish_locked(zone
);
5744 if ((flags
& Z_NOWAIT
) == 0) {
5745 zone_expand_locked(zone
, flags
, zalloc_needs_refill
);
5747 if (flags
& (Z_NOWAIT
| Z_NOPAGEWAIT
)) {
5748 zone_expand_async_schedule_if_needed(zone
);
5750 if (__improbable(zone
->z_elems_free
== 0)) {
5752 if (__improbable(flags
& Z_NOFAIL
)) {
5753 zone_nofail_panic(zone
);
5755 DTRACE_VM2(zalloc
, zone_t
, zone
, void*, NULL
);
5761 * We might have changed core or got preempted/blocked while expanding
5762 * the zone. Allocating from the zone when the recirculation depot
5763 * is not empty is not allowed.
5765 * It will be rare but possible for the depot to refill while we were
5766 * waiting for pages. If that happens we need to start over.
5768 if (!STAILQ_EMPTY(&zone
->z_recirc
)) {
5770 return zalloc_ext(zone
, zstats
, flags
);
5773 return zalloc_item_fast(zone
, zstats
, flags
);
5777 * @function zalloc_item
5780 * Performs allocations when zone caching is off.
5783 * This function calls @c zalloc_item_slow() when refilling the zone
5784 * is needed, or @c zalloc_item_fast() if the zone has enough free elements.
5787 zalloc_item(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
)
5789 zone_lock_check_contention(zone
, NULL
);
5792 * When we commited to the zalloc_item() path,
5793 * zone caching might have been flipped/enabled.
5795 * If we got preempted for long enough, the recirculation layer
5796 * can have been populated, and allocating from the zone would be
5799 * So double check for this extremely rare race here.
5801 if (__improbable(!STAILQ_EMPTY(&zone
->z_recirc
))) {
5803 return zalloc_ext(zone
, zstats
, flags
);
5806 if (__improbable(zone
->z_elems_free
<= zone
->z_elems_rsv
)) {
5807 return zalloc_item_slow(zone
, zstats
, flags
);
5810 return zalloc_item_fast(zone
, zstats
, flags
);
5814 zalloc_cached_fast(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
,
5815 zone_cache_t cache
, zone_magazine_t freemag
)
5817 vm_offset_t esize
= zone_elem_size(zone
);
5821 index
= --cache
->zc_alloc_cur
;
5822 if (index
>= zc_mag_size()) {
5823 zone_accounting_panic(zone
, "zc_alloc_cur wrap around");
5825 ze
= cache
->zc_alloc_elems
[index
];
5826 cache
->zc_alloc_elems
[index
].ze_value
= 0;
5828 zpercpu_get(zstats
)->zs_mem_allocated
+= esize
;
5829 enable_preemption();
5831 if (zone_meta_is_free(zone_meta_from_element(ze
), ze
)) {
5832 zone_meta_double_free_panic(zone
, ze
, __func__
);
5835 return zalloc_return(zone
, ze
, flags
, esize
, freemag
);
5839 zalloc_cached_from_depot(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
,
5840 zone_cache_t cache
, zone_cache_t depot
, zone_magazine_t mag
)
5842 STAILQ_REMOVE_HEAD(&depot
->zc_depot
, zm_link
);
5843 if (depot
->zc_depot_cur
-- == 0) {
5844 zone_accounting_panic(zone
, "zc_depot_cur wrap-around");
5846 zone_depot_unlock_nopreempt(depot
);
5848 mag
= zone_magazine_replace(&cache
->zc_alloc_cur
,
5849 &cache
->zc_alloc_elems
, mag
);
5851 z_debug_assert(cache
->zc_alloc_cur
== zc_mag_size());
5852 z_debug_assert(mag
->zm_cur
== 0);
5854 if (zone
== zc_magazine_zone
) {
5855 enable_preemption();
5856 bzero(mag
, zone_elem_size(zone
));
5860 return zalloc_cached_fast(zone
, zstats
, flags
, cache
, mag
);
5863 __attribute__((noinline
))
5865 zalloc_cached_slow(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
,
5868 zone_magazine_t mag
= NULL
;
5869 struct zone_depot mags
= STAILQ_HEAD_INITIALIZER(mags
);
5872 * Try to allocate from our local depot, if there's one.
5874 if (STAILQ_FIRST(&cache
->zc_depot
)) {
5875 zone_depot_lock_nopreempt(cache
);
5877 if ((mag
= STAILQ_FIRST(&cache
->zc_depot
)) != NULL
) {
5878 return zalloc_cached_from_depot(zone
, zstats
, flags
,
5882 zone_depot_unlock_nopreempt(cache
);
5885 zone_lock_nopreempt_check_contention(zone
, cache
);
5888 * If the recirculation depot is empty, we'll need to import.
5889 * The system is tuned for this to be extremely rare.
5891 if (__improbable(STAILQ_EMPTY(&zone
->z_recirc
))) {
5892 uint16_t n_elems
= zc_mag_size();
5894 if (zone
->z_elems_free
< n_elems
+ zone
->z_elems_rsv
/ 2 &&
5895 os_sub_overflow(zone
->z_elems_free
,
5896 zone
->z_elems_rsv
/ 2, &n_elems
)) {
5900 z_debug_assert(n_elems
<= zc_mag_size());
5902 if (__improbable(n_elems
== 0)) {
5904 * If importing elements would deplete the zone,
5905 * call zalloc_item_slow()
5907 return zalloc_item_slow(zone
, zstats
, flags
);
5910 if (__improbable(zone_caching_disabled
)) {
5911 if (__improbable(zone_caching_disabled
< 0)) {
5913 * In the first 10s after boot, mess with
5914 * the scan position in order to make early
5915 * allocations patterns less predictible.
5917 zone_early_scramble_rr(zone
, zstats
);
5919 return zalloc_item_fast(zone
, zstats
, flags
);
5922 zalloc_import(zone
, cache
->zc_alloc_elems
, n_elems
);
5924 cache
->zc_alloc_cur
= n_elems
;
5925 zone_elems_free_sub(zone
, n_elems
);
5927 zone_unlock_nopreempt(zone
);
5929 return zalloc_cached_fast(zone
, zstats
, flags
, cache
, NULL
);
5932 uint16_t n_mags
= 0;
5935 * If the recirculation depot has elements, then try to fill
5936 * the local per-cpu depot to (1 / zc_recirc_denom)
5939 mag
= STAILQ_FIRST(&zone
->z_recirc
);
5940 STAILQ_REMOVE_HEAD(&zone
->z_recirc
, zm_link
);
5941 STAILQ_INSERT_TAIL(&mags
, mag
, zm_link
);
5944 for (uint16_t i
= 0; i
< zc_mag_size(); i
++) {
5945 zone_element_t e
= mag
->zm_elems
[i
];
5947 if (!zone_meta_mark_used(zone_meta_from_element(e
), e
)) {
5948 zone_meta_double_free_panic(zone
, e
, __func__
);
5951 } while (!STAILQ_EMPTY(&zone
->z_recirc
) &&
5952 zc_recirc_denom
* n_mags
* zc_mag_size() <= cache
->zc_depot_max
);
5954 zone_elems_free_sub(zone
, n_mags
* zc_mag_size());
5955 zone_counter_sub(zone
, z_recirc_cur
, n_mags
);
5957 zone_unlock_nopreempt(zone
);
5960 * And then incorporate everything into our per-cpu layer.
5962 mag
= STAILQ_FIRST(&mags
);
5963 STAILQ_REMOVE_HEAD(&mags
, zm_link
);
5964 mag
= zone_magazine_replace(&cache
->zc_alloc_cur
,
5965 &cache
->zc_alloc_elems
, mag
);
5966 z_debug_assert(cache
->zc_alloc_cur
== zc_mag_size());
5967 z_debug_assert(mag
->zm_cur
== 0);
5970 zone_depot_lock_nopreempt(cache
);
5971 cache
->zc_depot_cur
+= n_mags
;
5972 STAILQ_CONCAT(&cache
->zc_depot
, &mags
);
5973 zone_depot_unlock_nopreempt(cache
);
5976 return zalloc_cached_fast(zone
, zstats
, flags
, cache
, mag
);
5980 * @function zalloc_cached
5983 * Performs allocations when zone caching is on.
5986 * This function calls @c zalloc_cached_fast() when the caches have elements
5989 * Else it will call @c zalloc_cached_slow() so that the cache is refilled,
5990 * which might switch to the @c zalloc_item_slow() track when the backing zone
5991 * needs to be refilled.
5994 zalloc_cached(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
)
5998 disable_preemption();
5999 cache
= zpercpu_get(zone
->z_pcpu_cache
);
6001 if (cache
->zc_alloc_cur
== 0) {
6002 if (__improbable(cache
->zc_free_cur
== 0)) {
6003 return zalloc_cached_slow(zone
, zstats
, flags
, cache
);
6005 zone_cache_swap_magazines(cache
);
6008 return zalloc_cached_fast(zone
, zstats
, flags
, cache
, NULL
);
6012 * @function zalloc_ext
6015 * The core implementation of @c zalloc(), @c zalloc_flags(), @c zalloc_percpu().
6018 zalloc_ext(zone_t zone
, zone_stats_t zstats
, zalloc_flags_t flags
)
6021 * KASan uses zalloc() for fakestack, which can be called anywhere.
6022 * However, we make sure these calls can never block.
6024 assert(zone
->kasan_fakestacks
||
6025 ml_get_interrupts_enabled() ||
6026 ml_is_quiescing() ||
6027 debug_mode_active() ||
6028 startup_phase
< STARTUP_SUB_EARLY_BOOT
);
6031 * Make sure Z_NOFAIL was not obviously misused
6033 if (zone
->z_replenishes
) {
6034 assert((flags
& (Z_NOWAIT
| Z_NOPAGEWAIT
)) == 0);
6035 } else if (flags
& Z_NOFAIL
) {
6036 assert(!zone
->exhaustible
&&
6037 (flags
& (Z_NOWAIT
| Z_NOPAGEWAIT
)) == 0);
6041 if (__improbable(zone
->gzalloc_tracked
)) {
6042 return zalloc_gz(zone
, zstats
, flags
);
6044 #endif /* CONFIG_GZALLOC */
6046 if (zone
->z_pcpu_cache
) {
6047 return zalloc_cached(zone
, zstats
, flags
);
6050 return zalloc_item(zone
, zstats
, flags
);
6054 zalloc(union zone_or_view zov
)
6056 return zalloc_flags(zov
, Z_WAITOK
);
6060 zalloc_noblock(union zone_or_view zov
)
6062 return zalloc_flags(zov
, Z_NOWAIT
);
6066 zalloc_flags(union zone_or_view zov
, zalloc_flags_t flags
)
6068 zone_t zone
= zov
.zov_view
->zv_zone
;
6069 zone_stats_t zstats
= zov
.zov_view
->zv_stats
;
6070 assert(!zone
->z_percpu
);
6071 return zalloc_ext(zone
, zstats
, flags
);
6075 zalloc_percpu(union zone_or_view zov
, zalloc_flags_t flags
)
6077 zone_t zone
= zov
.zov_view
->zv_zone
;
6078 zone_stats_t zstats
= zov
.zov_view
->zv_stats
;
6079 assert(zone
->z_percpu
);
6080 return (void *)__zpcpu_mangle(zalloc_ext(zone
, zstats
, flags
));
6084 _zalloc_permanent(zone_t zone
, vm_size_t size
, vm_offset_t mask
)
6086 struct zone_page_metadata
*page_meta
;
6087 vm_offset_t offs
, addr
;
6090 assert(ml_get_interrupts_enabled() ||
6091 ml_is_quiescing() ||
6092 debug_mode_active() ||
6093 startup_phase
< STARTUP_SUB_EARLY_BOOT
);
6095 size
= (size
+ mask
) & ~mask
;
6096 assert(size
<= PAGE_SIZE
);
6099 assert(zone
->z_self
== zone
);
6102 pva
= zone
->z_pageq_partial
;
6103 while (!zone_pva_is_null(pva
)) {
6104 page_meta
= zone_pva_to_meta(pva
);
6105 if (page_meta
->zm_bump
+ size
<= PAGE_SIZE
) {
6108 pva
= page_meta
->zm_page_next
;
6111 zone_expand_locked(zone
, Z_WAITOK
, NULL
);
6115 offs
= (uint16_t)((page_meta
->zm_bump
+ mask
) & ~mask
);
6116 page_meta
->zm_bump
= (uint16_t)(offs
+ size
);
6117 page_meta
->zm_alloc_size
+= size
;
6118 zone
->z_elems_free
-= size
;
6119 zpercpu_get(zone
->z_stats
)->zs_mem_allocated
+= size
;
6121 if (page_meta
->zm_alloc_size
>= PAGE_SIZE
- sizeof(vm_offset_t
)) {
6122 zone_meta_requeue(zone
, &zone
->z_pageq_full
, page_meta
);
6127 addr
= offs
+ zone_pva_to_addr(pva
);
6129 DTRACE_VM2(zalloc
, zone_t
, zone
, void*, addr
);
6130 return (void *)addr
;
6134 _zalloc_permanent_large(size_t size
, vm_offset_t mask
)
6139 kr
= kernel_memory_allocate(kernel_map
, &addr
, size
, mask
,
6140 KMA_KOBJECT
| KMA_PERMANENT
| KMA_ZERO
,
6141 VM_KERN_MEMORY_KALLOC
);
6143 panic("zalloc_permanent: unable to allocate %zd bytes (%d)",
6146 return (void *)addr
;
6150 zalloc_permanent(vm_size_t size
, vm_offset_t mask
)
6152 if (size
<= PAGE_SIZE
) {
6153 zone_t zone
= &zone_array
[ZONE_ID_PERMANENT
];
6154 return _zalloc_permanent(zone
, size
, mask
);
6156 return _zalloc_permanent_large(size
, mask
);
6160 zalloc_percpu_permanent(vm_size_t size
, vm_offset_t mask
)
6162 zone_t zone
= &zone_array
[ZONE_ID_PERCPU_PERMANENT
];
6163 return (void *)__zpcpu_mangle(_zalloc_permanent(zone
, size
, mask
));
6167 #endif /* !ZALLOC_TEST */
6168 #pragma mark zone GC / trimming
6171 static thread_call_data_t zone_defrag_callout
;
6174 zone_reclaim_chunk(zone_t z
, struct zone_page_metadata
*meta
, uint32_t free_count
)
6176 vm_address_t page_addr
;
6177 vm_size_t size_to_free
;
6178 uint32_t bitmap_ref
;
6179 uint32_t page_count
;
6180 bool sequester
= z
->z_va_sequester
&& !z
->z_destroyed
;
6182 zone_meta_queue_pop_native(z
, &z
->z_pageq_empty
, &page_addr
);
6184 page_count
= meta
->zm_chunk_len
;
6186 if (meta
->zm_alloc_size
) {
6187 zone_metadata_corruption(z
, meta
, "alloc_size");
6190 if (page_count
!= 1) {
6191 zone_metadata_corruption(z
, meta
, "page_count");
6193 size_to_free
= ptoa(z
->z_chunk_pages
);
6194 os_atomic_sub(&zones_phys_page_mapped_count
,
6195 z
->z_chunk_pages
, relaxed
);
6197 if (page_count
> z
->z_chunk_pages
) {
6198 zone_metadata_corruption(z
, meta
, "page_count");
6200 if (page_count
< z
->z_chunk_pages
) {
6201 /* Dequeue non populated VA from z_pageq_va */
6202 zone_meta_remqueue(z
, meta
+ page_count
);
6204 size_to_free
= ptoa(page_count
);
6205 os_atomic_sub(&zones_phys_page_mapped_count
, page_count
, relaxed
);
6208 zone_counter_sub(z
, z_elems_free
, free_count
);
6209 zone_counter_sub(z
, z_elems_avail
, free_count
);
6210 zone_counter_sub(z
, z_wired_empty
, page_count
);
6211 zone_counter_sub(z
, z_wired_cur
, page_count
);
6212 if (z
->z_elems_free_min
< free_count
) {
6213 z
->z_elems_free_min
= 0;
6215 z
->z_elems_free_min
-= free_count
;
6217 if (z
->z_elems_free_max
< free_count
) {
6218 z
->z_elems_free_max
= 0;
6220 z
->z_elems_free_max
-= free_count
;
6225 if (meta
->zm_inline_bitmap
) {
6226 for (int i
= 0; i
< meta
->zm_chunk_len
; i
++) {
6227 meta
[i
].zm_bitmap
= 0;
6230 bitmap_ref
= meta
->zm_bitmap
;
6231 meta
->zm_bitmap
= 0;
6233 meta
->zm_chunk_len
= 0;
6235 if (!meta
->zm_inline_bitmap
) {
6236 bitmap_ref
= meta
->zm_bitmap
;
6238 zone_counter_sub(z
, z_va_cur
, z
->z_percpu
? 1 : z
->z_chunk_pages
);
6239 bzero(meta
, sizeof(*meta
) * z
->z_chunk_pages
);
6245 zone_bits_free(bitmap_ref
);
6248 /* Free the pages for metadata and account for them */
6250 kasan_poison_range(page_addr
, size_to_free
, ASAN_VALID
);
6252 #if VM_MAX_TAG_ZONES
6254 ztMemoryRemove(z
, page_addr
, size_to_free
);
6256 #endif /* VM_MAX_TAG_ZONES */
6259 kernel_memory_depopulate(zone_submap(z
), page_addr
,
6260 size_to_free
, KMA_KOBJECT
, VM_KERN_MEMORY_ZONE
);
6262 kmem_free(zone_submap(z
), page_addr
, ptoa(z
->z_chunk_pages
));
6266 * Freeing memory sometimes needs some (for example vm map entries
6267 * to represent holes).
6269 * If there are any active replenish threads, we need to let them work
6270 * while we hold no locks. Only do so right after we just freed memory
6271 * once however to give them even more chances to find fresh pages.
6273 zone_replenish_wait_if_needed();
6275 thread_yield_to_preemption();
6280 zone_meta_queue_push(z
, &z
->z_pageq_va
, meta
);
6285 zone_reclaim_elements(zone_t z
, uint16_t *count
, zone_element_t
*elems
)
6287 uint16_t n
= *count
;
6289 z_debug_assert(n
<= zc_mag_size());
6291 for (uint16_t i
= 0; i
< n
; i
++) {
6292 zone_element_t ze
= elems
[i
];
6293 elems
[i
].ze_value
= 0;
6294 zfree_drop(z
, zone_element_validate(z
, ze
), ze
, false);
6302 zone_reclaim_recirc_magazine(zone_t z
, struct zone_depot
*mags
)
6304 zone_magazine_t mag
= STAILQ_FIRST(&z
->z_recirc
);
6306 STAILQ_REMOVE_HEAD(&z
->z_recirc
, zm_link
);
6307 STAILQ_INSERT_TAIL(mags
, mag
, zm_link
);
6308 zone_counter_sub(z
, z_recirc_cur
, 1);
6310 z_debug_assert(mag
->zm_cur
== zc_mag_size());
6312 for (uint16_t i
= 0; i
< zc_mag_size(); i
++) {
6313 zone_element_t ze
= mag
->zm_elems
[i
];
6314 mag
->zm_elems
[i
].ze_value
= 0;
6315 zfree_drop(z
, zone_element_validate(z
, ze
), ze
, true);
6320 return zc_mag_size();
6324 zone_depot_trim(zone_cache_t zc
, struct zone_depot
*head
)
6326 zone_magazine_t mag
;
6328 if (zc
->zc_depot_cur
== 0 ||
6329 2 * (zc
->zc_depot_cur
+ 1) * zc_mag_size() <= zc
->zc_depot_max
) {
6333 zone_depot_lock(zc
);
6335 while (zc
->zc_depot_cur
&&
6336 2 * (zc
->zc_depot_cur
+ 1) * zc_mag_size() > zc
->zc_depot_max
) {
6337 mag
= STAILQ_FIRST(&zc
->zc_depot
);
6338 STAILQ_REMOVE_HEAD(&zc
->zc_depot
, zm_link
);
6339 STAILQ_INSERT_TAIL(head
, mag
, zm_link
);
6343 zone_depot_unlock(zc
);
6346 __enum_decl(zone_reclaim_mode_t
, uint32_t, {
6349 ZONE_RECLAIM_DESTROY
,
6353 * @function zone_reclaim
6356 * Drains or trim the zone.
6359 * Draining the zone will free it from all its elements.
6361 * Trimming the zone tries to respect the working set size, and avoids draining
6362 * the depot when it's not necessary.
6364 * @param z The zone to reclaim from
6365 * @param mode The purpose of this reclaim.
6368 zone_reclaim(zone_t z
, zone_reclaim_mode_t mode
)
6370 struct zone_depot mags
= STAILQ_HEAD_INITIALIZER(mags
);
6371 zone_magazine_t mag
, tmp
;
6375 if (mode
== ZONE_RECLAIM_DESTROY
) {
6376 if (!z
->z_destructible
|| z
->z_pcpu_cache
||
6377 z
->z_elems_rsv
|| z
->z_allows_foreign
) {
6378 panic("zdestroy: Zone %s%s isn't destructible",
6379 zone_heap_name(z
), z
->z_name
);
6382 if (!z
->z_self
|| z
->z_expander
|| z
->z_expander_vm_priv
||
6383 z
->z_async_refilling
|| z
->z_expanding_wait
) {
6384 panic("zdestroy: Zone %s%s in an invalid state for destruction",
6385 zone_heap_name(z
), z
->z_name
);
6390 * Unset the valid bit. We'll hit an assert failure on further
6391 * operations on this zone, until zinit() is called again.
6393 * Leave the zone valid for KASan as we will see zfree's on
6394 * quarantined free elements even after the zone is destroyed.
6398 z
->z_destroyed
= true;
6399 } else if (z
->z_destroyed
) {
6400 return zone_unlock(z
);
6401 } else if (z
->z_replenishes
&& z
->z_async_refilling
) {
6403 * If the zone is replenishing, leave it alone.
6405 return zone_unlock(z
);
6408 if (z
->z_pcpu_cache
) {
6409 if (mode
!= ZONE_RECLAIM_TRIM
) {
6410 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6411 zc
->zc_depot_max
/= 2;
6414 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6415 if (zc
->zc_depot_max
> 0) {
6423 if (mode
== ZONE_RECLAIM_TRIM
) {
6424 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6425 zone_depot_trim(zc
, &mags
);
6428 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6429 zone_depot_lock(zc
);
6430 STAILQ_CONCAT(&mags
, &zc
->zc_depot
);
6431 zc
->zc_depot_cur
= 0;
6432 zone_depot_unlock(zc
);
6440 STAILQ_FOREACH(mag
, &mags
, zm_link
) {
6441 freed
+= zone_reclaim_elements(z
,
6442 &mag
->zm_cur
, mag
->zm_elems
);
6444 if (freed
>= zc_free_batch_size
) {
6445 z
->z_elems_free_min
+= freed
;
6446 z
->z_elems_free_max
+= freed
;
6447 z
->z_elems_free
+= freed
;
6449 thread_yield_to_preemption();
6455 if (mode
== ZONE_RECLAIM_DESTROY
) {
6456 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6457 freed
+= zone_reclaim_elements(z
,
6458 &zc
->zc_alloc_cur
, zc
->zc_alloc_elems
);
6459 freed
+= zone_reclaim_elements(z
,
6460 &zc
->zc_free_cur
, zc
->zc_free_elems
);
6463 z
->z_elems_free_wss
= 0;
6464 z
->z_elems_free_min
= 0;
6465 z
->z_elems_free_max
= 0;
6466 z
->z_contention_cur
= 0;
6467 z
->z_contention_wma
= 0;
6469 z
->z_elems_free_min
+= freed
;
6470 z
->z_elems_free_max
+= freed
;
6472 z
->z_elems_free
+= freed
;
6476 struct zone_page_metadata
*meta
;
6477 uint32_t count
, goal
, freed
= 0;
6479 goal
= z
->z_elems_rsv
;
6480 if (mode
== ZONE_RECLAIM_TRIM
) {
6482 * When trimming, only free elements in excess
6483 * of the working set estimate.
6485 * However if we are in a situation where the working
6486 * set estimate is clearly growing, ignore the estimate
6487 * as the next working set update will grow it and
6488 * we want to avoid churn.
6490 goal
= MAX(goal
, MAX(z
->z_elems_free_wss
,
6491 z
->z_elems_free
- z
->z_elems_free_min
));
6494 * Add some slop to account for "the last partial chunk in flight"
6495 * so that we do not deplete the recirculation depot too harshly.
6497 goal
+= z
->z_chunk_elems
/ 2;
6500 if (z
->z_elems_free
<= goal
) {
6505 * If we're above target, but we have no free page, then drain
6506 * the recirculation depot until we get a free chunk or exhaust
6509 * This is rather abrupt but also somehow will reduce
6510 * fragmentation anyway, and the zone code will import
6513 while (z
->z_recirc_cur
) {
6514 if (z
->z_recirc_cur
* zc_mag_size() <= goal
&&
6515 !zone_pva_is_null(z
->z_pageq_empty
)) {
6518 if (freed
>= zc_free_batch_size
) {
6520 thread_yield_to_preemption();
6523 /* we dropped the lock, needs to reassess */
6526 freed
+= zone_reclaim_recirc_magazine(z
, &mags
);
6529 if (zone_pva_is_null(z
->z_pageq_empty
)) {
6533 meta
= zone_pva_to_meta(z
->z_pageq_empty
);
6534 count
= (uint32_t)ptoa(meta
->zm_chunk_len
) / zone_elem_size(z
);
6536 if (z
->z_elems_free
- count
< goal
) {
6540 zone_reclaim_chunk(z
, meta
, count
);
6545 STAILQ_FOREACH_SAFE(mag
, &mags
, zm_link
, tmp
) {
6546 zone_magazine_free(mag
);
6551 zone_reclam_all(zone_reclaim_mode_t mode
)
6554 * Start with zones with VA sequester since depopulating
6555 * pages will not need to allocate vm map entries for holes,
6556 * which will give memory back to the system faster.
6559 if (z
== zc_magazine_zone
) {
6562 if (z
->z_va_sequester
&& z
->collectable
) {
6563 zone_reclaim(z
, mode
);
6568 if (z
== zc_magazine_zone
) {
6571 if (!z
->z_va_sequester
&& z
->collectable
) {
6572 zone_reclaim(z
, mode
);
6576 zone_reclaim(zc_magazine_zone
, mode
);
6580 zone_gc(zone_gc_level_t level
)
6582 zone_reclaim_mode_t mode
;
6586 mode
= ZONE_RECLAIM_TRIM
;
6589 mode
= ZONE_RECLAIM_DRAIN
;
6591 case ZONE_GC_JETSAM
:
6592 kill_process_in_largest_zone();
6593 mode
= ZONE_RECLAIM_TRIM
;
6597 current_thread()->options
|= TH_OPT_ZONE_PRIV
;
6598 lck_mtx_lock(&zone_gc_lock
);
6600 zone_reclam_all(mode
);
6602 if (level
== ZONE_GC_JETSAM
&& zone_map_nearing_exhaustion()) {
6604 * If we possibly killed a process, but we're still critical,
6605 * we need to drain harder.
6607 zone_reclam_all(ZONE_RECLAIM_DRAIN
);
6610 lck_mtx_unlock(&zone_gc_lock
);
6611 current_thread()->options
&= ~TH_OPT_ZONE_PRIV
;
6617 zone_gc(ZONE_GC_TRIM
);
6623 zone_gc(ZONE_GC_DRAIN
);
6627 zone_defrag_needed(zone_t z
)
6629 uint32_t recirc_size
= z
->z_recirc_cur
* zc_mag_size();
6631 if (recirc_size
<= z
->z_chunk_elems
/ 2) {
6634 return recirc_size
* zc_defrag_ratio
> z
->z_elems_free_wss
* 100;
6638 * @function zone_defrag_async
6641 * Resize the recirculation depot to match the working set size.
6644 * When zones grow very large due to a spike in usage, and then some of those
6645 * elements get freed, the elements in magazines in the recirculation depot
6646 * are in no particular order.
6648 * In order to control fragmentation, we need to detect "empty" pages so that
6649 * they get onto the @c z_pageq_empty freelist, so that allocations re-pack
6652 * This is done very gently, never in excess of the working set and some slop.
6655 zone_defrag_async(__unused thread_call_param_t p0
, __unused thread_call_param_t p1
)
6658 struct zone_depot mags
= STAILQ_HEAD_INITIALIZER(mags
);
6659 zone_magazine_t mag
, tmp
;
6660 uint32_t freed
= 0, goal
= 0;
6662 if (!z
->collectable
|| !zone_defrag_needed(z
)) {
6668 goal
= z
->z_elems_free_wss
+ z
->z_chunk_elems
/ 2 +
6671 while (z
->z_recirc_cur
* zc_mag_size() > goal
) {
6672 if (freed
>= zc_free_batch_size
) {
6674 thread_yield_to_preemption();
6677 /* we dropped the lock, needs to reassess */
6680 freed
+= zone_reclaim_recirc_magazine(z
, &mags
);
6685 STAILQ_FOREACH_SAFE(mag
, &mags
, zm_link
, tmp
) {
6686 zone_magazine_free(mag
);
6692 compute_zone_working_set_size(__unused
void *param
)
6694 uint32_t zc_auto
= zc_auto_threshold
;
6695 bool kick_defrag
= false;
6698 * Keep zone caching disabled until the first proc is made.
6700 if (__improbable(zone_caching_disabled
< 0)) {
6704 zone_caching_disabled
= vm_pool_low();
6705 #if ZALLOC_EARLY_GAPS
6706 zone_cleanup_early_gaps_if_needed();
6709 if (os_mul_overflow(zc_auto
, Z_CONTENTION_WMA_UNIT
, &zc_auto
)) {
6715 bool needs_caching
= false;
6717 if (z
->z_self
!= z
) {
6723 wma
= z
->z_elems_free_max
- z
->z_elems_free_min
;
6724 wma
= (3 * wma
+ z
->z_elems_free_wss
) / 4;
6725 z
->z_elems_free_max
= z
->z_elems_free_min
= z
->z_elems_free
;
6726 z
->z_elems_free_wss
= wma
;
6728 if (!kick_defrag
&& zone_defrag_needed(z
)) {
6732 /* fixed point decimal of contentions per second */
6733 wma
= z
->z_contention_cur
* Z_CONTENTION_WMA_UNIT
/
6734 ZONE_WSS_UPDATE_PERIOD
;
6735 z
->z_contention_cur
= 0;
6736 z
->z_contention_wma
= (3 * wma
+ z
->z_contention_wma
) / 4;
6739 * If the zone seems to be very quiet,
6740 * gently lower its cpu-local depot size.
6742 if (z
->z_pcpu_cache
&& wma
< Z_CONTENTION_WMA_UNIT
/ 2 &&
6743 z
->z_contention_wma
< Z_CONTENTION_WMA_UNIT
/ 2) {
6744 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6745 if (zc
->zc_depot_max
> zc_mag_size()) {
6752 * If the zone has been contending like crazy for two periods,
6753 * and is eligible, maybe it's time to enable caching.
6755 if (!z
->z_nocaching
&& !z
->z_pcpu_cache
&& !z
->exhaustible
&&
6756 zc_auto
&& z
->z_contention_wma
>= zc_auto
&& wma
>= zc_auto
) {
6757 needs_caching
= true;
6762 if (needs_caching
) {
6763 zone_enable_caching(z
);
6768 thread_call_enter(&zone_defrag_callout
);
6772 #endif /* !ZALLOC_TEST */
6773 #pragma mark vm integration, MIG routines
6777 * Creates a vm_map_copy_t to return to the caller of mach_* MIG calls
6778 * requesting zone information.
6779 * Frees unused pages towards the end of the region, and zero'es out unused
6780 * space on the last page.
6782 static vm_map_copy_t
6784 vm_offset_t start_addr
,
6785 vm_size_t total_size
,
6786 vm_size_t used_size
)
6789 vm_offset_t end_addr
;
6790 vm_size_t free_size
;
6793 if (used_size
!= total_size
) {
6794 end_addr
= start_addr
+ used_size
;
6795 free_size
= total_size
- (round_page(end_addr
) - start_addr
);
6797 if (free_size
>= PAGE_SIZE
) {
6798 kmem_free(ipc_kernel_map
,
6799 round_page(end_addr
), free_size
);
6801 bzero((char *) end_addr
, round_page(end_addr
) - end_addr
);
6804 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)start_addr
,
6805 (vm_map_size_t
)used_size
, TRUE
, ©
);
6806 assert(kr
== KERN_SUCCESS
);
6814 mach_zone_name_t
*zn
,
6815 mach_zone_info_t
*zi
)
6818 vm_size_t cached
= 0;
6820 assert(z
!= ZONE_NULL
);
6827 if (z
->z_pcpu_cache
) {
6828 zpercpu_foreach(zc
, z
->z_pcpu_cache
) {
6829 cached
+= zc
->zc_alloc_cur
+ zc
->zc_free_cur
;
6830 cached
+= zc
->zc_depot_cur
* zc_mag_size();
6837 * Append kalloc heap name to zone name (if zone is used by kalloc)
6839 char temp_zone_name
[MAX_ZONE_NAME
] = "";
6840 snprintf(temp_zone_name
, MAX_ZONE_NAME
, "%s%s",
6841 zone_heap_name(z
), z
->z_name
);
6843 /* assuming here the name data is static */
6844 (void) __nosan_strlcpy(zn
->mzn_name
, temp_zone_name
,
6845 strlen(temp_zone_name
) + 1);
6849 *zi
= (mach_zone_info_t
) {
6850 .mzi_count
= zone_count_allocated(&zcopy
) - cached
,
6851 .mzi_cur_size
= ptoa_64(zone_scale_for_percpu(&zcopy
, zcopy
.z_wired_cur
)),
6852 // max_size for zprint is now high-watermark of pages used
6853 .mzi_max_size
= ptoa_64(zone_scale_for_percpu(&zcopy
, zcopy
.z_wired_hwm
)),
6854 .mzi_elem_size
= zone_scale_for_percpu(&zcopy
, zcopy
.z_elem_size
),
6855 .mzi_alloc_size
= ptoa_64(zcopy
.z_chunk_pages
),
6856 .mzi_exhaustible
= (uint64_t)zcopy
.exhaustible
,
6858 zpercpu_foreach(zs
, zcopy
.z_stats
) {
6859 zi
->mzi_sum_size
+= zs
->zs_mem_allocated
;
6861 if (zcopy
.collectable
) {
6862 SET_MZI_COLLECTABLE_BYTES(zi
->mzi_collectable
,
6863 ptoa_64(zone_scale_for_percpu(&zcopy
, zcopy
.z_wired_empty
)));
6864 SET_MZI_COLLECTABLE_FLAG(zi
->mzi_collectable
, TRUE
);
6873 __unused task_t task
,
6874 __unused mach_zone_name_array_t
*namesp
,
6875 __unused mach_msg_type_number_t
*namesCntp
,
6876 __unused task_zone_info_array_t
*infop
,
6877 __unused mach_msg_type_number_t
*infoCntp
)
6879 return KERN_FAILURE
;
6885 mach_zone_name_array_t
*namesp
,
6886 mach_msg_type_number_t
*namesCntp
,
6887 mach_zone_info_array_t
*infop
,
6888 mach_msg_type_number_t
*infoCntp
)
6890 return mach_memory_info(host
, namesp
, namesCntp
, infop
, infoCntp
, NULL
, NULL
);
6897 mach_zone_name_array_t
*namesp
,
6898 mach_msg_type_number_t
*namesCntp
,
6899 mach_zone_info_array_t
*infop
,
6900 mach_msg_type_number_t
*infoCntp
,
6901 mach_memory_info_array_t
*memoryInfop
,
6902 mach_msg_type_number_t
*memoryInfoCntp
)
6904 mach_zone_name_t
*names
;
6905 vm_offset_t names_addr
;
6906 vm_size_t names_size
;
6908 mach_zone_info_t
*info
;
6909 vm_offset_t info_addr
;
6910 vm_size_t info_size
;
6912 mach_memory_info_t
*memory_info
;
6913 vm_offset_t memory_info_addr
;
6914 vm_size_t memory_info_size
;
6915 vm_size_t memory_info_vmsize
;
6916 unsigned int num_info
;
6918 unsigned int max_zones
, used_zones
, i
;
6919 mach_zone_name_t
*zn
;
6920 mach_zone_info_t
*zi
;
6923 uint64_t zones_collectable_bytes
= 0;
6925 if (host
== HOST_NULL
) {
6926 return KERN_INVALID_HOST
;
6928 #if CONFIG_DEBUGGER_FOR_ZONE_INFO
6929 if (!PE_i_can_has_debugger(NULL
)) {
6930 return KERN_INVALID_HOST
;
6935 * We assume that zones aren't freed once allocated.
6936 * We won't pick up any zones that are allocated later.
6939 max_zones
= os_atomic_load(&num_zones
, relaxed
);
6941 names_size
= round_page(max_zones
* sizeof *names
);
6942 kr
= kmem_alloc_pageable(ipc_kernel_map
,
6943 &names_addr
, names_size
, VM_KERN_MEMORY_IPC
);
6944 if (kr
!= KERN_SUCCESS
) {
6947 names
= (mach_zone_name_t
*) names_addr
;
6949 info_size
= round_page(max_zones
* sizeof *info
);
6950 kr
= kmem_alloc_pageable(ipc_kernel_map
,
6951 &info_addr
, info_size
, VM_KERN_MEMORY_IPC
);
6952 if (kr
!= KERN_SUCCESS
) {
6953 kmem_free(ipc_kernel_map
,
6954 names_addr
, names_size
);
6957 info
= (mach_zone_info_t
*) info_addr
;
6962 used_zones
= max_zones
;
6963 for (i
= 0; i
< max_zones
; i
++) {
6964 if (!get_zone_info(&(zone_array
[i
]), zn
, zi
)) {
6968 zones_collectable_bytes
+= GET_MZI_COLLECTABLE_BYTES(zi
->mzi_collectable
);
6973 *namesp
= (mach_zone_name_t
*) create_vm_map_copy(names_addr
, names_size
, used_zones
* sizeof *names
);
6974 *namesCntp
= used_zones
;
6976 *infop
= (mach_zone_info_t
*) create_vm_map_copy(info_addr
, info_size
, used_zones
* sizeof *info
);
6977 *infoCntp
= used_zones
;
6980 memory_info_addr
= 0;
6982 if (memoryInfop
&& memoryInfoCntp
) {
6984 num_info
= vm_page_diagnose_estimate();
6985 memory_info_size
= num_info
* sizeof(*memory_info
);
6986 memory_info_vmsize
= round_page(memory_info_size
);
6987 kr
= kmem_alloc_pageable(ipc_kernel_map
,
6988 &memory_info_addr
, memory_info_vmsize
, VM_KERN_MEMORY_IPC
);
6989 if (kr
!= KERN_SUCCESS
) {
6993 kr
= vm_map_wire_kernel(ipc_kernel_map
, memory_info_addr
, memory_info_addr
+ memory_info_vmsize
,
6994 VM_PROT_READ
| VM_PROT_WRITE
, VM_KERN_MEMORY_IPC
, FALSE
);
6995 assert(kr
== KERN_SUCCESS
);
6997 memory_info
= (mach_memory_info_t
*) memory_info_addr
;
6998 vm_page_diagnose(memory_info
, num_info
, zones_collectable_bytes
);
7000 kr
= vm_map_unwire(ipc_kernel_map
, memory_info_addr
, memory_info_addr
+ memory_info_vmsize
, FALSE
);
7001 assert(kr
== KERN_SUCCESS
);
7003 kr
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)memory_info_addr
,
7004 (vm_map_size_t
)memory_info_size
, TRUE
, ©
);
7005 assert(kr
== KERN_SUCCESS
);
7007 *memoryInfop
= (mach_memory_info_t
*) copy
;
7008 *memoryInfoCntp
= num_info
;
7011 return KERN_SUCCESS
;
7015 mach_zone_info_for_zone(
7017 mach_zone_name_t name
,
7018 mach_zone_info_t
*infop
)
7022 if (host
== HOST_NULL
) {
7023 return KERN_INVALID_HOST
;
7025 #if CONFIG_DEBUGGER_FOR_ZONE_INFO
7026 if (!PE_i_can_has_debugger(NULL
)) {
7027 return KERN_INVALID_HOST
;
7031 if (infop
== NULL
) {
7032 return KERN_INVALID_ARGUMENT
;
7035 zone_ptr
= ZONE_NULL
;
7038 * Append kalloc heap name to zone name (if zone is used by kalloc)
7040 char temp_zone_name
[MAX_ZONE_NAME
] = "";
7041 snprintf(temp_zone_name
, MAX_ZONE_NAME
, "%s%s",
7042 zone_heap_name(z
), z
->z_name
);
7044 /* Find the requested zone by name */
7045 if (track_this_zone(temp_zone_name
, name
.mzn_name
)) {
7051 /* No zones found with the requested zone name */
7052 if (zone_ptr
== ZONE_NULL
) {
7053 return KERN_INVALID_ARGUMENT
;
7056 if (get_zone_info(zone_ptr
, NULL
, infop
)) {
7057 return KERN_SUCCESS
;
7059 return KERN_FAILURE
;
7063 mach_zone_info_for_largest_zone(
7065 mach_zone_name_t
*namep
,
7066 mach_zone_info_t
*infop
)
7068 if (host
== HOST_NULL
) {
7069 return KERN_INVALID_HOST
;
7071 #if CONFIG_DEBUGGER_FOR_ZONE_INFO
7072 if (!PE_i_can_has_debugger(NULL
)) {
7073 return KERN_INVALID_HOST
;
7077 if (namep
== NULL
|| infop
== NULL
) {
7078 return KERN_INVALID_ARGUMENT
;
7081 if (get_zone_info(zone_find_largest(), namep
, infop
)) {
7082 return KERN_SUCCESS
;
7084 return KERN_FAILURE
;
7088 get_zones_collectable_bytes(void)
7090 uint64_t zones_collectable_bytes
= 0;
7091 mach_zone_info_t zi
;
7094 if (get_zone_info(z
, NULL
, &zi
)) {
7095 zones_collectable_bytes
+=
7096 GET_MZI_COLLECTABLE_BYTES(zi
.mzi_collectable
);
7100 return zones_collectable_bytes
;
7104 mach_zone_get_zlog_zones(
7106 mach_zone_name_array_t
*namesp
,
7107 mach_msg_type_number_t
*namesCntp
)
7109 #if ZONE_ENABLE_LOGGING
7110 unsigned int max_zones
, logged_zones
, i
;
7113 mach_zone_name_t
*names
;
7114 vm_offset_t names_addr
;
7115 vm_size_t names_size
;
7117 if (host
== HOST_NULL
) {
7118 return KERN_INVALID_HOST
;
7121 if (namesp
== NULL
|| namesCntp
== NULL
) {
7122 return KERN_INVALID_ARGUMENT
;
7125 max_zones
= os_atomic_load(&num_zones
, relaxed
);
7127 names_size
= round_page(max_zones
* sizeof *names
);
7128 kr
= kmem_alloc_pageable(ipc_kernel_map
,
7129 &names_addr
, names_size
, VM_KERN_MEMORY_IPC
);
7130 if (kr
!= KERN_SUCCESS
) {
7133 names
= (mach_zone_name_t
*) names_addr
;
7135 zone_ptr
= ZONE_NULL
;
7137 for (i
= 0; i
< max_zones
; i
++) {
7138 zone_t z
= &(zone_array
[i
]);
7139 assert(z
!= ZONE_NULL
);
7141 /* Copy out the zone name if zone logging is enabled */
7142 if (z
->zlog_btlog
) {
7143 get_zone_info(z
, &names
[logged_zones
], NULL
);
7148 *namesp
= (mach_zone_name_t
*) create_vm_map_copy(names_addr
, names_size
, logged_zones
* sizeof *names
);
7149 *namesCntp
= logged_zones
;
7151 return KERN_SUCCESS
;
7153 #else /* ZONE_ENABLE_LOGGING */
7154 #pragma unused(host, namesp, namesCntp)
7155 return KERN_FAILURE
;
7156 #endif /* ZONE_ENABLE_LOGGING */
7160 mach_zone_get_btlog_records(
7162 mach_zone_name_t name
,
7163 zone_btrecord_array_t
*recsp
,
7164 mach_msg_type_number_t
*recsCntp
)
7166 #if DEBUG || DEVELOPMENT
7167 unsigned int numrecs
= 0;
7168 zone_btrecord_t
*recs
;
7171 vm_offset_t recs_addr
;
7172 vm_size_t recs_size
;
7174 if (host
== HOST_NULL
) {
7175 return KERN_INVALID_HOST
;
7178 if (recsp
== NULL
|| recsCntp
== NULL
) {
7179 return KERN_INVALID_ARGUMENT
;
7182 zone_ptr
= ZONE_NULL
;
7185 * Append kalloc heap name to zone name (if zone is used by kalloc)
7187 char temp_zone_name
[MAX_ZONE_NAME
] = "";
7188 snprintf(temp_zone_name
, MAX_ZONE_NAME
, "%s%s",
7189 zone_heap_name(z
), z
->z_name
);
7191 /* Find the requested zone by name */
7192 if (track_this_zone(temp_zone_name
, name
.mzn_name
)) {
7198 /* No zones found with the requested zone name */
7199 if (zone_ptr
== ZONE_NULL
) {
7200 return KERN_INVALID_ARGUMENT
;
7203 /* Logging not turned on for the requested zone */
7204 if (!DO_LOGGING(zone_ptr
)) {
7205 return KERN_FAILURE
;
7208 /* Allocate memory for btlog records */
7209 numrecs
= (unsigned int)(get_btlog_records_count(zone_ptr
->zlog_btlog
));
7210 recs_size
= round_page(numrecs
* sizeof *recs
);
7212 kr
= kmem_alloc_pageable(ipc_kernel_map
, &recs_addr
, recs_size
, VM_KERN_MEMORY_IPC
);
7213 if (kr
!= KERN_SUCCESS
) {
7218 * We will call get_btlog_records() below which populates this region while holding a spinlock
7219 * (the btlog lock). So these pages need to be wired.
7221 kr
= vm_map_wire_kernel(ipc_kernel_map
, recs_addr
, recs_addr
+ recs_size
,
7222 VM_PROT_READ
| VM_PROT_WRITE
, VM_KERN_MEMORY_IPC
, FALSE
);
7223 assert(kr
== KERN_SUCCESS
);
7225 recs
= (zone_btrecord_t
*)recs_addr
;
7226 get_btlog_records(zone_ptr
->zlog_btlog
, recs
, &numrecs
);
7228 kr
= vm_map_unwire(ipc_kernel_map
, recs_addr
, recs_addr
+ recs_size
, FALSE
);
7229 assert(kr
== KERN_SUCCESS
);
7231 *recsp
= (zone_btrecord_t
*) create_vm_map_copy(recs_addr
, recs_size
, numrecs
* sizeof *recs
);
7232 *recsCntp
= numrecs
;
7234 return KERN_SUCCESS
;
7236 #else /* DEBUG || DEVELOPMENT */
7237 #pragma unused(host, name, recsp, recsCntp)
7238 return KERN_FAILURE
;
7239 #endif /* DEBUG || DEVELOPMENT */
7243 #if DEBUG || DEVELOPMENT
7246 mach_memory_info_check(void)
7248 mach_memory_info_t
* memory_info
;
7249 mach_memory_info_t
* info
;
7250 unsigned int num_info
;
7251 vm_offset_t memory_info_addr
;
7253 size_t memory_info_size
, memory_info_vmsize
;
7254 uint64_t top_wired
, zonestotal
, total
;
7256 num_info
= vm_page_diagnose_estimate();
7257 memory_info_size
= num_info
* sizeof(*memory_info
);
7258 memory_info_vmsize
= round_page(memory_info_size
);
7259 kr
= kmem_alloc(kernel_map
, &memory_info_addr
, memory_info_vmsize
, VM_KERN_MEMORY_DIAG
);
7260 assert(kr
== KERN_SUCCESS
);
7262 memory_info
= (mach_memory_info_t
*) memory_info_addr
;
7263 vm_page_diagnose(memory_info
, num_info
, 0);
7265 top_wired
= total
= zonestotal
= 0;
7267 zonestotal
+= zone_size_wired(z
);
7270 for (uint32_t idx
= 0; idx
< num_info
; idx
++) {
7271 info
= &memory_info
[idx
];
7275 if (VM_KERN_COUNT_WIRED
== info
->site
) {
7276 top_wired
= info
->size
;
7278 if (VM_KERN_SITE_HIDE
& info
->flags
) {
7281 if (!(VM_KERN_SITE_WIRED
& info
->flags
)) {
7284 total
+= info
->size
;
7286 total
+= zonestotal
;
7288 printf("vm_page_diagnose_check %qd of %qd, zones %qd, short 0x%qx\n",
7289 total
, top_wired
, zonestotal
, top_wired
- total
);
7291 kmem_free(kernel_map
, memory_info_addr
, memory_info_vmsize
);
7296 extern boolean_t(*volatile consider_buffer_cache_collect
)(int);
7298 #endif /* DEBUG || DEVELOPMENT */
7304 if (host
== HOST_NULL
) {
7305 return KERN_INVALID_HOST
;
7308 #if DEBUG || DEVELOPMENT
7309 /* Callout to buffer cache GC to drop elements in the apfs zones */
7310 if (consider_buffer_cache_collect
!= NULL
) {
7311 (void)(*consider_buffer_cache_collect
)(0);
7313 zone_gc(ZONE_GC_DRAIN
);
7314 #endif /* DEBUG || DEVELOPMENT */
7315 return KERN_SUCCESS
;
7319 zone_find_largest(void)
7321 uint32_t largest_idx
= 0;
7322 vm_offset_t largest_size
= zone_size_wired(&zone_array
[0]);
7324 zone_index_foreach(i
) {
7325 vm_offset_t size
= zone_size_wired(&zone_array
[i
]);
7326 if (size
> largest_size
) {
7328 largest_size
= size
;
7332 return &zone_array
[largest_idx
];
7335 #endif /* !ZALLOC_TEST */
7336 #pragma mark zone creation, configuration, destruction
7340 zone_init_defaults(zone_id_t zid
)
7342 zone_t z
= &zone_array
[zid
];
7344 z
->z_wired_max
= ~0u;
7345 z
->collectable
= true;
7346 z
->expandable
= true;
7347 z
->z_submap_idx
= Z_SUBMAP_IDX_GENERAL
;
7349 lck_spin_init(&z
->z_lock
, &zone_locks_grp
, LCK_ATTR_NULL
);
7350 STAILQ_INIT(&z
->z_recirc
);
7355 zone_is_initializing(zone_t z
)
7357 return !z
->z_self
&& !z
->z_destroyed
;
7361 zone_set_submap_idx(zone_t zone
, unsigned int sub_map_idx
)
7363 if (!zone_is_initializing(zone
)) {
7364 panic("%s: called after zone_create()", __func__
);
7366 if (sub_map_idx
> zone_last_submap_idx
) {
7367 panic("zone_set_submap_idx(%d) > %d", sub_map_idx
, zone_last_submap_idx
);
7369 zone
->z_submap_idx
= sub_map_idx
;
7373 zone_set_noexpand(zone_t zone
, vm_size_t nelems
)
7375 if (!zone_is_initializing(zone
)) {
7376 panic("%s: called after zone_create()", __func__
);
7378 zone
->expandable
= false;
7379 zone
->z_wired_max
= zone_alloc_pages_for_nelems(zone
, nelems
);
7383 zone_set_exhaustible(zone_t zone
, vm_size_t nelems
)
7385 if (!zone_is_initializing(zone
)) {
7386 panic("%s: called after zone_create()", __func__
);
7388 zone
->expandable
= false;
7389 zone
->exhaustible
= true;
7390 zone
->z_wired_max
= zone_alloc_pages_for_nelems(zone
, nelems
);
7394 * @function zone_create_find
7397 * Finds an unused zone for the given name and element size.
7399 * @param name the zone name
7400 * @param size the element size (including redzones, ...)
7401 * @param flags the flags passed to @c zone_create*
7402 * @param zid_inout the desired zone ID or ZONE_ID_ANY
7404 * @returns a zone to initialize further.
7410 zone_create_flags_t flags
,
7411 zone_id_t
*zid_inout
)
7413 zone_id_t nzones
, zid
= *zid_inout
;
7416 simple_lock(&all_zones_lock
, &zone_locks_grp
);
7418 nzones
= (zone_id_t
)os_atomic_load(&num_zones
, relaxed
);
7419 assert(num_zones_in_use
<= nzones
&& nzones
< MAX_ZONES
);
7421 if (__improbable(nzones
< ZONE_ID__FIRST_DYNAMIC
)) {
7423 * The first time around, make sure the reserved zone IDs
7424 * have an initialized lock as zone_index_foreach() will
7427 while (nzones
< ZONE_ID__FIRST_DYNAMIC
) {
7428 zone_init_defaults(nzones
++);
7431 os_atomic_store(&num_zones
, nzones
, release
);
7434 if (zid
!= ZONE_ID_ANY
) {
7435 if (zid
>= ZONE_ID__FIRST_DYNAMIC
) {
7436 panic("zone_create: invalid desired zone ID %d for %s",
7439 if (flags
& ZC_DESTRUCTIBLE
) {
7440 panic("zone_create: ID %d (%s) must be permanent", zid
, name
);
7442 if (zone_array
[zid
].z_self
) {
7443 panic("zone_create: creating zone ID %d (%s) twice", zid
, name
);
7445 z
= &zone_array
[zid
];
7447 if (flags
& ZC_DESTRUCTIBLE
) {
7449 * If possible, find a previously zdestroy'ed zone in the
7450 * zone_array that we can reuse.
7452 for (int i
= bitmap_first(zone_destroyed_bitmap
, MAX_ZONES
);
7453 i
>= 0; i
= bitmap_next(zone_destroyed_bitmap
, i
)) {
7457 * If the zone name and the element size are the
7458 * same, we can just reuse the old zone struct.
7460 if (strcmp(z
->z_name
, name
) || zone_elem_size(z
) != size
) {
7463 bitmap_clear(zone_destroyed_bitmap
, i
);
7464 z
->z_destroyed
= false;
7472 z
= zone_init_defaults(zid
);
7475 * The release barrier pairs with the acquire in
7476 * zone_index_foreach() and makes sure that enumeration loops
7477 * always see an initialized zone lock.
7479 os_atomic_store(&num_zones
, nzones
, release
);
7484 simple_unlock(&all_zones_lock
);
7492 zone_create_panic(const char *name
, const char *f1
, const char *f2
)
7494 panic("zone_create: creating zone %s: flag %s and %s are incompatible",
7497 #define zone_create_assert_not_both(name, flags, current_flag, forbidden_flag) \
7498 if ((flags) & forbidden_flag) { \
7499 zone_create_panic(name, #current_flag, #forbidden_flag); \
7503 * Adjusts the size of the element based on minimum size, alignment
7504 * and kasan redzones
7507 zone_elem_adjust_size(
7508 const char *name __unused
,
7509 vm_size_t elem_size
,
7510 zone_create_flags_t flags __unused
,
7511 uint32_t *redzone __unused
)
7515 * Adjust element size for minimum size and pointer alignment
7517 size
= (elem_size
+ sizeof(vm_offset_t
) - 1) & -sizeof(vm_offset_t
);
7518 if (size
< ZONE_MIN_ELEM_SIZE
) {
7519 size
= ZONE_MIN_ELEM_SIZE
;
7524 * Expand the zone allocation size to include the redzones.
7526 * For page-multiple zones add a full guard page because they
7527 * likely require alignment.
7529 uint32_t redzone_tmp
;
7530 if (flags
& (ZC_KASAN_NOREDZONE
| ZC_PERCPU
)) {
7532 } else if ((size
& PAGE_MASK
) == 0) {
7533 if (size
!= PAGE_SIZE
&& (flags
& ZC_ALIGNMENT_REQUIRED
)) {
7534 panic("zone_create: zone %s can't provide more than PAGE_SIZE"
7537 redzone_tmp
= PAGE_SIZE
;
7538 } else if (flags
& ZC_ALIGNMENT_REQUIRED
) {
7541 redzone_tmp
= KASAN_GUARD_SIZE
;
7543 size
+= redzone_tmp
* 2;
7545 *redzone
= redzone_tmp
;
7552 * Returns the allocation chunk size that has least framentation
7555 zone_get_min_alloc_granule(
7556 vm_size_t elem_size
,
7557 zone_create_flags_t flags
)
7559 vm_size_t alloc_granule
= PAGE_SIZE
;
7560 if (flags
& ZC_PERCPU
) {
7561 alloc_granule
= PAGE_SIZE
* zpercpu_count();
7562 if (PAGE_SIZE
% elem_size
> 256) {
7563 panic("zone_create: per-cpu zone has too much fragmentation");
7565 } else if ((elem_size
& PAGE_MASK
) == 0) {
7566 /* zero fragmentation by definition */
7567 alloc_granule
= elem_size
;
7568 } else if (alloc_granule
% elem_size
== 0) {
7569 /* zero fragmentation by definition */
7571 vm_size_t frag
= (alloc_granule
% elem_size
) * 100 / alloc_granule
;
7572 vm_size_t alloc_tmp
= PAGE_SIZE
;
7573 while ((alloc_tmp
+= PAGE_SIZE
) <= ZONE_MAX_ALLOC_SIZE
) {
7574 vm_size_t frag_tmp
= (alloc_tmp
% elem_size
) * 100 / alloc_tmp
;
7575 if (frag_tmp
< frag
) {
7577 alloc_granule
= alloc_tmp
;
7581 return alloc_granule
;
7585 zone_get_foreign_alloc_size(
7586 const char *name __unused
,
7587 vm_size_t elem_size
,
7588 zone_create_flags_t flags
,
7591 vm_size_t adjusted_size
= zone_elem_adjust_size(name
, elem_size
, flags
,
7593 vm_size_t alloc_granule
= zone_get_min_alloc_granule(adjusted_size
,
7595 vm_size_t min_size
= min_pages
* PAGE_SIZE
;
7597 * Round up min_size to a multiple of alloc_granule
7599 return ((min_size
+ alloc_granule
- 1) / alloc_granule
)
7607 zone_create_flags_t flags
,
7609 void (^extra_setup
)(zone_t
))
7615 if (size
> ZONE_MAX_ALLOC_SIZE
) {
7616 panic("zone_create: element size too large: %zd", (size_t)size
);
7619 if (size
< 2 * sizeof(vm_size_t
)) {
7620 /* Elements are too small for kasan. */
7621 flags
|= ZC_KASAN_NOQUARANTINE
| ZC_KASAN_NOREDZONE
;
7624 size
= zone_elem_adjust_size(name
, size
, flags
, &redzone
);
7626 * Allocate the zone slot, return early if we found an older match.
7628 z
= zone_create_find(name
, size
, flags
, &zid
);
7629 if (__improbable(z
->z_self
)) {
7630 /* We found a zone to reuse */
7635 * Initialize the zone properly.
7639 * If the kernel is post lockdown, copy the zone name passed in.
7640 * Else simply maintain a pointer to the name string as it can only
7641 * be a core XNU zone (no unloadable kext exists before lockdown).
7643 if (startup_phase
>= STARTUP_SUB_LOCKDOWN
) {
7644 size_t nsz
= MIN(strlen(name
) + 1, MACH_ZONE_NAME_MAX_LEN
);
7645 char *buf
= zalloc_permanent(nsz
, ZALIGN_NONE
);
7646 strlcpy(buf
, name
, nsz
);
7651 if (__probable(zone_array
[ZONE_ID_PERCPU_PERMANENT
].z_self
)) {
7652 z
->z_stats
= zalloc_percpu_permanent_type(struct zone_stats
);
7655 * zone_init() hasn't run yet, use the storage provided by
7656 * zone_stats_startup(), and zone_init() will replace it
7657 * with the final value once the PERCPU zone exists.
7659 z
->z_stats
= __zpcpu_mangle_for_boot(&zone_stats_startup
[zone_index(z
)]);
7662 alloc
= zone_get_min_alloc_granule(size
, flags
);
7664 if (flags
& ZC_KALLOC_HEAP
) {
7665 size_t rem
= (alloc
% size
) / (alloc
/ size
);
7668 * Try to grow the elements size and spread them more if the remaining
7669 * space is large enough.
7671 size
+= rem
& ~(KALLOC_MINALIGN
- 1);
7674 z
->z_elem_size
= (uint16_t)size
;
7675 z
->z_chunk_pages
= (uint16_t)atop(alloc
);
7676 if (flags
& ZC_PERCPU
) {
7677 z
->z_chunk_elems
= (uint16_t)(PAGE_SIZE
/ z
->z_elem_size
);
7679 z
->z_chunk_elems
= (uint16_t)(alloc
/ z
->z_elem_size
);
7681 if (zone_element_idx(zone_element_encode(0,
7682 z
->z_chunk_elems
- 1, ZPM_AUTO
)) != z
->z_chunk_elems
- 1) {
7683 panic("zone_element_encode doesn't work for zone [%s]", name
);
7687 z
->z_kasan_redzone
= redzone
;
7688 if (strncmp(name
, "fakestack.", sizeof("fakestack.") - 1) == 0) {
7689 z
->kasan_fakestacks
= true;
7697 if (flags
& ZC_SEQUESTER
) {
7698 z
->z_va_sequester
= true;
7701 /* ZC_CACHING applied after all configuration is done */
7702 if (flags
& ZC_NOCACHING
) {
7703 z
->z_nocaching
= true;
7706 if (flags
& ZC_PERCPU
) {
7708 * ZC_ZFREE_CLEARMEM is forced because per-cpu zones allow for
7709 * pointer-sized allocations which poisoning doesn't support.
7711 zone_create_assert_not_both(name
, flags
, ZC_PERCPU
, ZC_ALLOW_FOREIGN
);
7713 z
->gzalloc_exempt
= true;
7714 z
->z_free_zeroes
= true;
7716 if (flags
& ZC_ZFREE_CLEARMEM
) {
7717 z
->z_free_zeroes
= true;
7719 if (flags
& ZC_NOGC
) {
7720 z
->collectable
= false;
7722 if (flags
& ZC_NOENCRYPT
) {
7723 z
->z_noencrypt
= true;
7725 if (flags
& ZC_ALIGNMENT_REQUIRED
) {
7726 z
->alignment_required
= true;
7728 if (flags
& ZC_NOGZALLOC
) {
7729 z
->gzalloc_exempt
= true;
7731 if (flags
& ZC_NOCALLOUT
) {
7732 z
->no_callout
= true;
7734 if (flags
& ZC_DESTRUCTIBLE
) {
7735 zone_create_assert_not_both(name
, flags
, ZC_DESTRUCTIBLE
, ZC_ALLOW_FOREIGN
);
7736 z
->z_destructible
= true;
7740 * Handle Internal flags
7742 if (flags
& ZC_ALLOW_FOREIGN
) {
7743 z
->z_allows_foreign
= true;
7745 if ((ZSECURITY_OPTIONS_SUBMAP_USER_DATA
& zsecurity_options
) &&
7746 (flags
& ZC_DATA_BUFFERS
)) {
7747 z
->z_submap_idx
= Z_SUBMAP_IDX_BAG_OF_BYTES
;
7749 if (flags
& ZC_KASAN_NOQUARANTINE
) {
7750 z
->kasan_noquarantine
= true;
7752 /* ZC_KASAN_NOREDZONE already handled */
7755 * Then if there's extra tuning, do it
7762 * Configure debugging features
7765 gzalloc_zone_init(z
); /* might set z->gzalloc_tracked */
7766 if (z
->gzalloc_tracked
) {
7767 z
->z_nocaching
= true;
7770 #if ZONE_ENABLE_LOGGING
7771 if (!z
->gzalloc_tracked
&& num_zones_logged
< max_num_zones_to_log
) {
7773 * Check for and set up zone leak detection if requested via boot-args.
7774 * might set z->zone_logging
7776 zone_setup_logging(z
);
7778 #endif /* ZONE_ENABLE_LOGGING */
7779 #if VM_MAX_TAG_ZONES
7780 if (!z
->gzalloc_tracked
&& z
->kalloc_heap
&& zone_tagging_on
) {
7781 static int tag_zone_index
;
7782 vm_offset_t esize
= zone_elem_size(z
);
7784 z
->tags_inline
= (((page_size
+ esize
- 1) / esize
) <=
7785 (sizeof(uint32_t) / sizeof(uint16_t)));
7786 z
->tag_zone_index
= os_atomic_inc_orig(&tag_zone_index
, relaxed
);
7787 assert(z
->tag_zone_index
< VM_MAX_TAG_ZONES
);
7792 * Finally, fixup properties based on security policies, boot-args, ...
7794 if ((ZSECURITY_OPTIONS_SUBMAP_USER_DATA
& zsecurity_options
) &&
7795 z
->kalloc_heap
== KHEAP_ID_DATA_BUFFERS
) {
7796 z
->z_submap_idx
= Z_SUBMAP_IDX_BAG_OF_BYTES
;
7799 if ((ZSECURITY_OPTIONS_SEQUESTER
& zsecurity_options
) &&
7800 (flags
& ZC_NOSEQUESTER
) == 0 &&
7801 z
->z_submap_idx
== Z_SUBMAP_IDX_GENERAL
) {
7802 z
->z_va_sequester
= true;
7806 * Clear entire element for non data zones and upto zp_min_size for
7809 if (z
->z_submap_idx
!= Z_SUBMAP_IDX_BAG_OF_BYTES
) {
7810 z
->z_free_zeroes
= true;
7811 } else if (size
<= zp_min_size
) {
7812 z
->z_free_zeroes
= true;
7815 if ((flags
& ZC_CACHING
) && !z
->z_nocaching
) {
7817 * If zcache hasn't been initialized yet, remember our decision,
7819 * zone_enable_caching() will be called again by
7820 * zcache_bootstrap(), while the system is still single
7821 * threaded, to build the missing caches.
7823 if (__probable(zc_magazine_zone
)) {
7824 zone_enable_caching(z
);
7827 __zpcpu_mangle_for_boot(&zone_cache_startup
[zid
]);
7831 if (zp_factor
!= 0 && !z
->z_free_zeroes
) {
7832 if (__probable(zone_array
[ZONE_ID_PERCPU_PERMANENT
].z_self
)) {
7833 zpercpu_foreach(zs
, z
->z_stats
) {
7834 zs
->zs_poison_seqno
= zone_poison_count_init(z
);
7837 zone_stats_startup
[zid
].zs_poison_seqno
=
7838 zone_poison_count_init(z
);
7851 zone_create_startup(struct zone_create_startup_spec
*spec
)
7853 *spec
->z_var
= zone_create_ext(spec
->z_name
, spec
->z_size
,
7854 spec
->z_flags
, spec
->z_zid
, spec
->z_setup
);
7858 * The 4 first field of a zone_view and a zone alias, so that the zone_or_view_t
7859 * union works. trust but verify.
7861 #define zalloc_check_zov_alias(f1, f2) \
7862 static_assert(offsetof(struct zone, f1) == offsetof(struct zone_view, f2))
7863 zalloc_check_zov_alias(z_self
, zv_zone
);
7864 zalloc_check_zov_alias(z_stats
, zv_stats
);
7865 zalloc_check_zov_alias(z_name
, zv_name
);
7866 zalloc_check_zov_alias(z_views
, zv_next
);
7867 #undef zalloc_check_zov_alias
7871 zone_view_startup_init(struct zone_view_startup_spec
*spec
)
7873 struct kalloc_heap
*heap
= NULL
;
7874 zone_view_t zv
= spec
->zv_view
;
7877 switch (spec
->zv_heapid
) {
7878 case KHEAP_ID_DEFAULT
:
7879 heap
= KHEAP_DEFAULT
;
7881 case KHEAP_ID_DATA_BUFFERS
:
7882 heap
= KHEAP_DATA_BUFFERS
;
7892 z
= kalloc_heap_zone_for_size(heap
, spec
->zv_size
);
7896 assert(spec
->zv_size
<= zone_elem_size(z
));
7900 zv
->zv_stats
= zalloc_percpu_permanent_type(struct zone_stats
);
7901 zv
->zv_next
= z
->z_views
;
7902 if (z
->z_views
== NULL
&& z
->kalloc_heap
== KHEAP_ID_NONE
) {
7904 * count the raw view for zones not in a heap,
7905 * kalloc_heap_init() already counts it for its members.
7907 zone_view_count
+= 2;
7909 zone_view_count
+= 1;
7918 zone_create_flags_t flags
)
7920 return zone_create_ext(name
, size
, flags
, ZONE_ID_ANY
, NULL
);
7925 vm_size_t size
, /* the size of an element */
7926 vm_size_t max
, /* maximum memory to use */
7927 vm_size_t alloc __unused
, /* allocation size */
7928 const char *name
) /* a name for the zone */
7930 zone_t z
= zone_create(name
, size
, ZC_DESTRUCTIBLE
);
7931 z
->z_wired_max
= zone_alloc_pages_for_nelems(z
, max
/ size
);
7938 unsigned int zindex
= zone_index(z
);
7940 current_thread()->options
|= TH_OPT_ZONE_PRIV
;
7941 lck_mtx_lock(&zone_gc_lock
);
7943 zone_reclaim(z
, ZONE_RECLAIM_DESTROY
);
7945 lck_mtx_unlock(&zone_gc_lock
);
7946 current_thread()->options
&= ~TH_OPT_ZONE_PRIV
;
7949 if (__improbable(z
->gzalloc_tracked
)) {
7950 /* If the zone is gzalloc managed dump all the elements in the free cache */
7951 gzalloc_empty_free_cache(z
);
7957 while (!zone_pva_is_null(z
->z_pageq_va
)) {
7958 struct zone_page_metadata
*meta
;
7959 vm_offset_t free_addr
;
7961 zone_counter_sub(z
, z_va_cur
, z
->z_percpu
? 1 : z
->z_chunk_pages
);
7962 meta
= zone_meta_queue_pop_native(z
, &z
->z_pageq_va
, &free_addr
);
7963 assert(meta
->zm_chunk_len
<= ZM_CHUNK_LEN_MAX
);
7964 bzero(meta
, sizeof(*meta
) * z
->z_chunk_pages
);
7966 kmem_free(zone_submap(z
), free_addr
, ptoa(z
->z_chunk_pages
));
7971 /* Assert that all counts are zero */
7972 if (z
->z_elems_avail
|| z
->z_elems_free
||
7973 zone_size_wired(z
) || z
->z_va_cur
) {
7974 panic("zdestroy: Zone %s%s isn't empty at zdestroy() time",
7975 zone_heap_name(z
), z
->z_name
);
7978 /* consistency check: make sure everything is indeed empty */
7979 assert(zone_pva_is_null(z
->z_pageq_empty
));
7980 assert(zone_pva_is_null(z
->z_pageq_partial
));
7981 assert(zone_pva_is_null(z
->z_pageq_full
));
7982 assert(zone_pva_is_null(z
->z_pageq_va
));
7987 simple_lock(&all_zones_lock
, &zone_locks_grp
);
7989 assert(!bitmap_test(zone_destroyed_bitmap
, zindex
));
7990 /* Mark the zone as empty in the bitmap */
7991 bitmap_set(zone_destroyed_bitmap
, zindex
);
7993 assert(num_zones_in_use
> 0);
7995 simple_unlock(&all_zones_lock
);
7998 #endif /* !ZALLOC_TEST */
7999 #pragma mark zalloc module init
8003 * Initialize the "zone of zones" which uses fixed memory allocated
8004 * earlier in memory initialization. zone_bootstrap is called
8009 zone_bootstrap(void)
8011 /* Validate struct zone_packed_virtual_address expectations */
8012 static_assert((intptr_t)VM_MIN_KERNEL_ADDRESS
< 0, "the top bit must be 1");
8013 if (VM_KERNEL_POINTER_SIGNIFICANT_BITS
- PAGE_SHIFT
> 31) {
8014 panic("zone_pva_t can't pack a kernel page address in 31 bits");
8017 zpercpu_early_count
= ml_early_cpu_max_number() + 1;
8019 /* Set up zone element poisoning */
8023 * the KASAN quarantine for kalloc doesn't understand heaps
8024 * and trips the heap confusion panics. At the end of the day,
8025 * all these security measures are double duty with KASAN.
8027 * On 32bit kernels, these protections are just too expensive.
8029 #if !defined(__LP64__) || KASAN_ZALLOC
8030 zsecurity_options
&= ~ZSECURITY_OPTIONS_SEQUESTER
;
8031 zsecurity_options
&= ~ZSECURITY_OPTIONS_SUBMAP_USER_DATA
;
8032 zsecurity_options
&= ~ZSECURITY_OPTIONS_SEQUESTER_KEXT_KALLOC
;
8035 thread_call_setup_with_options(&zone_expand_callout
,
8036 zone_expand_async
, NULL
, THREAD_CALL_PRIORITY_HIGH
,
8037 THREAD_CALL_OPTIONS_ONCE
);
8039 thread_call_setup_with_options(&zone_defrag_callout
,
8040 zone_defrag_async
, NULL
, THREAD_CALL_PRIORITY_USER
,
8041 THREAD_CALL_OPTIONS_ONCE
);
8045 #if ARM_LARGE_MEMORY || __x86_64__
8046 #define ZONE_MAP_VIRTUAL_SIZE_LP64 (128ULL * 1024ULL * 1024 * 1024)
8048 #define ZONE_MAP_VIRTUAL_SIZE_LP64 (32ULL * 1024ULL * 1024 * 1024)
8050 #endif /* __LP64__ */
8052 #define ZONE_GUARD_SIZE (64UL << 10)
8055 static inline vm_offset_t
8056 zone_restricted_va_max(void)
8058 vm_offset_t compressor_max
= VM_PACKING_MAX_PACKABLE(C_SLOT_PACKED_PTR
);
8059 vm_offset_t vm_page_max
= VM_PACKING_MAX_PACKABLE(VM_PAGE_PACKED_PTR
);
8061 return trunc_page(MIN(compressor_max
, vm_page_max
));
8067 zone_tunables_fixup(void)
8069 if (zone_map_jetsam_limit
== 0 || zone_map_jetsam_limit
> 100) {
8070 zone_map_jetsam_limit
= ZONE_MAP_JETSAM_LIMIT_DEFAULT
;
8072 if (zc_magazine_size
> PAGE_SIZE
/ ZONE_MIN_ELEM_SIZE
) {
8073 zc_magazine_size
= (uint16_t)(PAGE_SIZE
/ ZONE_MIN_ELEM_SIZE
);
8076 STARTUP(TUNABLES
, STARTUP_RANK_MIDDLE
, zone_tunables_fixup
);
8080 zone_phys_size_max(void)
8085 if (PE_parse_boot_argn("zsize", &zsizearg
, sizeof(zsizearg
))) {
8086 zsize
= zsizearg
* (1024ULL * 1024);
8088 /* Set target zone size as 1/4 of physical memory */
8089 zsize
= (vm_size_t
)(sane_size
>> 2);
8090 #if defined(__LP64__)
8091 zsize
+= zsize
>> 1;
8092 #endif /* __LP64__ */
8095 if (zsize
< CONFIG_ZONE_MAP_MIN
) {
8096 zsize
= CONFIG_ZONE_MAP_MIN
; /* Clamp to min */
8098 if (zsize
> sane_size
>> 1) {
8099 zsize
= (vm_size_t
)(sane_size
>> 1); /* Clamp to half of RAM max */
8101 if (zsizearg
== 0 && zsize
> ZONE_MAP_MAX
) {
8102 /* if zsize boot-arg not present and zsize exceeds platform maximum, clip zsize */
8103 printf("NOTE: zonemap size reduced from 0x%lx to 0x%lx\n",
8104 (uintptr_t)zsize
, (uintptr_t)ZONE_MAP_MAX
);
8105 zsize
= ZONE_MAP_MAX
;
8108 return (vm_size_t
)trunc_page(zsize
);
8111 __options_decl(zone_init_allocate_flags_t
, unsigned, {
8112 ZIA_NONE
= 0x00000000,
8113 ZIA_REPLACE
= 0x00000001, /* replace a previous non permanent range */
8114 ZIA_RANDOM
= 0x00000002, /* place at a random address */
8115 ZIA_PERMANENT
= 0x00000004, /* permanent allocation */
8116 ZIA_GUARD
= 0x00000008, /* will be used as a guard */
8120 static struct zone_map_range
8121 zone_init_allocate_va(vm_map_address_t addr
, vm_size_t size
,
8122 zone_init_allocate_flags_t flags
)
8124 vm_map_kernel_flags_t vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
8125 int vm_alloc_flags
= 0;
8126 struct zone_map_range r
;
8129 if (flags
& ZIA_REPLACE
) {
8130 vm_alloc_flags
|= VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
;
8132 vm_alloc_flags
|= VM_FLAGS_ANYWHERE
;
8134 if (flags
& ZIA_RANDOM
) {
8135 vm_alloc_flags
|= VM_FLAGS_RANDOM_ADDR
;
8137 if (flags
& ZIA_PERMANENT
) {
8138 vmk_flags
.vmkf_permanent
= true;
8141 vm_object_reference(kernel_object
);
8143 kr
= vm_map_enter(kernel_map
, &addr
, size
, 0,
8144 vm_alloc_flags
, vmk_flags
, VM_KERN_MEMORY_ZONE
,
8145 kernel_object
, 0, FALSE
,
8146 (flags
& ZIA_GUARD
) ? VM_PROT_NONE
: VM_PROT_DEFAULT
,
8147 (flags
& ZIA_GUARD
) ? VM_PROT_NONE
: VM_PROT_DEFAULT
,
8150 if (KERN_SUCCESS
!= kr
) {
8151 panic("vm_map_enter(0x%zx) failed: %d", (size_t)size
, kr
);
8154 r
.min_address
= (vm_offset_t
)addr
;
8155 r
.max_address
= (vm_offset_t
)addr
+ size
;
8162 vm_offset_t
*submap_min
,
8164 uint64_t zone_sub_map_numer
,
8165 uint64_t *remaining_denom
,
8166 vm_offset_t
*remaining_size
,
8167 vm_size_t guard_size
)
8169 vm_offset_t submap_start
, submap_end
;
8170 vm_size_t submap_size
;
8174 submap_size
= trunc_page(zone_sub_map_numer
* *remaining_size
/
8176 submap_start
= *submap_min
;
8177 submap_end
= submap_start
+ submap_size
;
8179 #if defined(__LP64__)
8180 if (idx
== Z_SUBMAP_IDX_VA_RESTRICTED
) {
8181 vm_offset_t restricted_va_max
= zone_restricted_va_max();
8182 if (submap_end
> restricted_va_max
) {
8183 #if DEBUG || DEVELOPMENT
8184 printf("zone_init: submap[%d] clipped to %zdM of %zdM\n", idx
,
8185 (size_t)(restricted_va_max
- submap_start
) >> 20,
8186 (size_t)submap_size
>> 20);
8187 #endif /* DEBUG || DEVELOPMENT */
8188 guard_size
+= submap_end
- restricted_va_max
;
8189 *remaining_size
-= submap_end
- restricted_va_max
;
8190 submap_end
= restricted_va_max
;
8191 submap_size
= restricted_va_max
- submap_start
;
8194 vm_packing_verify_range("vm_compressor",
8195 submap_start
, submap_end
, VM_PACKING_PARAMS(C_SLOT_PACKED_PTR
));
8196 vm_packing_verify_range("vm_page",
8197 submap_start
, submap_end
, VM_PACKING_PARAMS(VM_PAGE_PACKED_PTR
));
8199 #endif /* defined(__LP64__) */
8201 vm_map_kernel_flags_t vmk_flags
= VM_MAP_KERNEL_FLAGS_NONE
;
8202 vmk_flags
.vmkf_permanent
= TRUE
;
8203 kr
= kmem_suballoc(kernel_map
, submap_min
, submap_size
,
8204 FALSE
, VM_FLAGS_FIXED
| VM_FLAGS_OVERWRITE
, vmk_flags
,
8205 VM_KERN_MEMORY_ZONE
, &submap
);
8206 if (kr
!= KERN_SUCCESS
) {
8207 panic("kmem_suballoc(kernel_map[%d] %p:%p) failed: %d",
8208 idx
, (void *)submap_start
, (void *)submap_end
, kr
);
8211 #if DEBUG || DEVELOPMENT
8212 printf("zone_init: submap[%d] %p:%p (%zuM)\n",
8213 idx
, (void *)submap_start
, (void *)submap_end
,
8214 (size_t)submap_size
>> 20);
8215 #endif /* DEBUG || DEVELOPMENT */
8217 zone_init_allocate_va(submap_end
, guard_size
,
8218 ZIA_PERMANENT
| ZIA_GUARD
| ZIA_REPLACE
);
8220 zone_submaps
[idx
] = submap
;
8221 *submap_min
= submap_end
+ guard_size
;
8222 *remaining_size
-= submap_size
;
8223 *remaining_denom
-= zone_sub_map_numer
;
8227 * Allocate metadata array and migrate foreign initial metadata.
8229 * So that foreign pages and native pages have the same scheme,
8230 * we allocate VA space that covers both foreign and native pages.
8234 zone_metadata_init(void)
8236 struct zone_map_range r0
= zone_info
.zi_map_range
[0];
8237 struct zone_map_range r1
= zone_info
.zi_map_range
[1];
8238 struct zone_map_range mr
, br
;
8239 vm_size_t meta_size
, bits_size
, foreign_base
;
8240 vm_offset_t hstart
, hend
;
8242 if (r0
.min_address
> r1
.min_address
) {
8243 r0
= zone_info
.zi_map_range
[1];
8244 r1
= zone_info
.zi_map_range
[0];
8247 meta_size
= round_page(atop(r1
.max_address
- r0
.min_address
) *
8248 sizeof(struct zone_page_metadata
)) + ZONE_GUARD_SIZE
* 2;
8251 * Allocations can't be smaller than 8 bytes, which is 128b / 16B per 1k
8252 * of physical memory (16M per 1G).
8254 * Let's preallocate for the worst to avoid weird panics.
8256 bits_size
= round_page(16 * (ptoa(zone_phys_mapped_max_pages
) >> 10));
8259 * Compute the size of the "hole" in the middle of the range.
8261 * If it is smaller than 256k, just leave it be, with this layout:
8263 * [G][ r0 meta ][ hole ][ r1 meta ][ bits ][G]
8265 * else punch a hole with guard pages around the hole, and place the
8266 * bits in the hole if it fits, or after r1 otherwise, yielding either
8267 * of the following layouts:
8269 * |__________________hend____________|
8271 * [G][ r0 meta ][ bits ][G]..........[G][ r1 meta ][G]
8272 * [G][ r0 meta ][G]..................[G][ r1 meta ][ bits ][G]
8274 hstart
= round_page(atop(r0
.max_address
- r0
.min_address
) *
8275 sizeof(struct zone_page_metadata
));
8276 hend
= trunc_page(atop(r1
.min_address
- r0
.min_address
) *
8277 sizeof(struct zone_page_metadata
));
8279 if (hstart
>= hend
|| hend
- hstart
< (256ul << 10)) {
8280 mr
= zone_init_allocate_va(0, meta_size
+ bits_size
,
8281 ZIA_PERMANENT
| ZIA_RANDOM
);
8282 mr
.min_address
+= ZONE_GUARD_SIZE
;
8283 mr
.max_address
-= ZONE_GUARD_SIZE
;
8284 br
.max_address
= mr
.max_address
;
8285 mr
.max_address
-= bits_size
;
8286 br
.min_address
= mr
.max_address
;
8288 #if DEBUG || DEVELOPMENT
8289 printf("zone_init: metadata %p:%p (%zuK)\n",
8290 (void *)mr
.min_address
, (void *)mr
.max_address
,
8291 (size_t)zone_range_size(&mr
) >> 10);
8292 printf("zone_init: metabits %p:%p (%zuK)\n",
8293 (void *)br
.min_address
, (void *)br
.max_address
,
8294 (size_t)zone_range_size(&br
) >> 10);
8295 #endif /* DEBUG || DEVELOPMENT */
8297 vm_size_t size
, alloc_size
= meta_size
;
8299 bool bits_in_middle
= true;
8301 if (hend
- hstart
- 2 * ZONE_GUARD_SIZE
< bits_size
) {
8302 alloc_size
+= bits_size
;
8303 bits_in_middle
= false;
8306 mr
= zone_init_allocate_va(0, alloc_size
, ZIA_RANDOM
);
8308 base
= mr
.min_address
;
8309 size
= ZONE_GUARD_SIZE
+ hstart
+ ZONE_GUARD_SIZE
;
8310 if (bits_in_middle
) {
8312 br
.min_address
= base
+ ZONE_GUARD_SIZE
+ hstart
;
8313 br
.max_address
= br
.min_address
+ bits_size
;
8315 zone_init_allocate_va(base
, size
, ZIA_PERMANENT
| ZIA_REPLACE
);
8318 size
= mr
.min_address
+ hend
- base
;
8319 kmem_free(kernel_map
, base
, size
);
8321 base
= mr
.min_address
+ hend
;
8322 size
= mr
.max_address
- base
;
8323 zone_init_allocate_va(base
, size
, ZIA_PERMANENT
| ZIA_REPLACE
);
8325 mr
.min_address
+= ZONE_GUARD_SIZE
;
8326 mr
.max_address
-= ZONE_GUARD_SIZE
;
8327 if (!bits_in_middle
) {
8328 br
.max_address
= mr
.max_address
;
8329 mr
.max_address
-= bits_size
;
8330 br
.min_address
= mr
.max_address
;
8333 #if DEBUG || DEVELOPMENT
8334 printf("zone_init: metadata0 %p:%p (%zuK)\n",
8335 (void *)mr
.min_address
, (void *)(mr
.min_address
+ hstart
),
8336 (size_t)hstart
>> 10);
8337 printf("zone_init: metadata1 %p:%p (%zuK)\n",
8338 (void *)(mr
.min_address
+ hend
), (void *)mr
.max_address
,
8339 (size_t)(zone_range_size(&mr
) - hend
) >> 10);
8340 printf("zone_init: metabits %p:%p (%zuK)\n",
8341 (void *)br
.min_address
, (void *)br
.max_address
,
8342 (size_t)zone_range_size(&br
) >> 10);
8343 #endif /* DEBUG || DEVELOPMENT */
8346 br
.min_address
= (br
.min_address
+ ZBA_CHUNK_SIZE
- 1) & -ZBA_CHUNK_SIZE
;
8347 br
.max_address
= br
.max_address
& -ZBA_CHUNK_SIZE
;
8349 zone_info
.zi_meta_range
= mr
;
8350 zone_info
.zi_bits_range
= br
;
8353 * Migrate the original static metadata into its new location.
8355 zone_info
.zi_meta_base
= (struct zone_page_metadata
*)mr
.min_address
-
8356 zone_pva_from_addr(r0
.min_address
).packed_address
;
8357 foreign_base
= zone_info
.zi_map_range
[ZONE_ADDR_FOREIGN
].min_address
;
8358 zone_meta_populate(foreign_base
, zone_foreign_size());
8359 memcpy(zone_meta_from_addr(foreign_base
),
8360 zone_foreign_meta_array_startup
,
8361 atop(zone_foreign_size()) * sizeof(struct zone_page_metadata
));
8364 memcpy(zba_base_header(), zba_chunk_startup
,
8365 sizeof(zba_chunk_startup
));
8368 /* Global initialization of Zone Allocator.
8369 * Runs after zone_bootstrap.
8375 vm_size_t zone_map_size
;
8376 vm_size_t remaining_size
;
8377 vm_offset_t submap_min
= 0;
8379 uint64_t submap_ratios
[Z_SUBMAP_IDX_COUNT
] = {
8381 [Z_SUBMAP_IDX_VA_RESTRICTED
] = 20,
8383 [Z_SUBMAP_IDX_VA_RESERVE
] = 10,
8384 #endif /* defined(__LP64__) */
8385 [Z_SUBMAP_IDX_GENERAL
] = 40,
8386 [Z_SUBMAP_IDX_BAG_OF_BYTES
] = 40,
8389 if (ZSECURITY_OPTIONS_SUBMAP_USER_DATA
& zsecurity_options
) {
8390 zone_last_submap_idx
= Z_SUBMAP_IDX_BAG_OF_BYTES
;
8392 zone_last_submap_idx
= Z_SUBMAP_IDX_GENERAL
;
8394 zone_phys_mapped_max_pages
= (uint32_t)atop(zone_phys_size_max());
8396 for (unsigned idx
= 0; idx
<= zone_last_submap_idx
; idx
++) {
8397 #if DEBUG || DEVELOPMENT
8398 char submap_name
[1 + sizeof("submap")];
8399 snprintf(submap_name
, sizeof(submap_name
), "submap%d", idx
);
8400 PE_parse_boot_argn(submap_name
, &submap_ratios
[idx
], sizeof(uint64_t));
8402 denom
+= submap_ratios
[idx
];
8406 zone_map_size
= ZONE_MAP_VIRTUAL_SIZE_LP64
;
8408 zone_map_size
= ptoa(zone_phys_mapped_max_pages
*
8409 (denom
+ submap_ratios
[Z_SUBMAP_IDX_VA_RESERVE
]) / denom
);
8412 remaining_size
= zone_map_size
-
8413 ZONE_GUARD_SIZE
* (zone_last_submap_idx
+ 1);
8416 * And now allocate the various pieces of VA and submaps.
8418 * Make a first allocation of contiguous VA, that we'll deallocate,
8419 * and we'll carve-out memory in that range again linearly.
8420 * The kernel is stil single threaded at this stage.
8423 struct zone_map_range
*map_range
=
8424 &zone_info
.zi_map_range
[ZONE_ADDR_NATIVE
];
8426 *map_range
= zone_init_allocate_va(0, zone_map_size
, ZIA_NONE
);
8427 submap_min
= map_range
->min_address
;
8430 * Allocate the submaps
8432 for (unsigned idx
= 0; idx
<= zone_last_submap_idx
; idx
++) {
8433 zone_submap_init(&submap_min
, idx
, submap_ratios
[idx
],
8434 &denom
, &remaining_size
, ZONE_GUARD_SIZE
);
8437 assert(submap_min
== map_range
->max_address
);
8439 zone_metadata_init();
8441 #if VM_MAX_TAG_ZONES
8442 if (zone_tagging_on
) {
8443 zone_tagging_init(zone_map_size
);
8447 gzalloc_init(zone_map_size
);
8450 zone_create_flags_t kma_flags
= ZC_NOCACHING
|
8451 ZC_NOGC
| ZC_NOENCRYPT
| ZC_NOGZALLOC
| ZC_NOCALLOUT
|
8452 ZC_KASAN_NOQUARANTINE
| ZC_KASAN_NOREDZONE
;
8454 (void)zone_create_ext("vm.permanent", 1, kma_flags
,
8455 ZONE_ID_PERMANENT
, ^(zone_t z
){
8456 z
->z_permanent
= true;
8458 #if defined(__LP64__)
8459 z
->z_submap_idx
= Z_SUBMAP_IDX_VA_RESTRICTED
;
8462 (void)zone_create_ext("vm.permanent.percpu", 1, kma_flags
| ZC_PERCPU
,
8463 ZONE_ID_PERCPU_PERMANENT
, ^(zone_t z
){
8464 z
->z_permanent
= true;
8466 #if defined(__LP64__)
8467 z
->z_submap_idx
= Z_SUBMAP_IDX_VA_RESTRICTED
;
8472 * Now migrate the startup statistics into their final storage.
8474 int cpu
= cpu_number();
8475 zone_index_foreach(idx
) {
8476 zone_t tz
= &zone_array
[idx
];
8478 if (tz
->z_stats
== __zpcpu_mangle_for_boot(&zone_stats_startup
[idx
])) {
8479 zone_stats_t zs
= zalloc_percpu_permanent_type(struct zone_stats
);
8481 *zpercpu_get_cpu(zs
, cpu
) = *zpercpu_get_cpu(tz
->z_stats
, cpu
);
8483 #if ZONE_ENABLE_LOGGING
8484 if (tz
->zone_logging
&& !tz
->zlog_btlog
) {
8485 zone_enable_logging(tz
);
8487 #endif /* ZONE_ENABLE_LOGGING */
8493 * Initialize the zone leak monitor
8495 zleak_init(zone_map_size
);
8496 #endif /* CONFIG_ZLEAKS */
8498 #if VM_MAX_TAG_ZONES
8499 if (zone_tagging_on
) {
8500 vm_allocation_zones_init();
8504 STARTUP(ZALLOC
, STARTUP_RANK_FIRST
, zone_init
);
8508 zone_cache_bootstrap(void)
8512 magzone
= zone_create("zcc_magazine_zone", sizeof(struct zone_magazine
) +
8513 zc_mag_size() * sizeof(zone_element_t
),
8514 ZC_NOGZALLOC
| ZC_KASAN_NOREDZONE
| ZC_KASAN_NOQUARANTINE
|
8515 ZC_SEQUESTER
| ZC_CACHING
| ZC_ZFREE_CLEARMEM
);
8516 magzone
->z_elems_rsv
= (uint16_t)(2 * zpercpu_count());
8518 os_atomic_store(&zc_magazine_zone
, magzone
, compiler_acq_rel
);
8521 * Now that we are initialized, we can enable zone caching for zones that
8522 * were made before zcache_bootstrap() was called.
8524 * The system is still single threaded so we don't need to take the lock.
8526 zone_index_foreach(i
) {
8527 zone_t z
= &zone_array
[i
];
8528 if (z
->z_pcpu_cache
) {
8529 z
->z_pcpu_cache
= NULL
;
8530 zone_enable_caching(z
);
8534 STARTUP(ZALLOC
, STARTUP_RANK_FOURTH
, zone_cache_bootstrap
);
8537 zalloc_first_proc_made(void)
8539 zone_caching_disabled
= 0;
8544 zone_foreign_mem_init(vm_size_t size
)
8548 if (atop(size
) > ZONE_FOREIGN_META_INLINE_COUNT
) {
8549 panic("ZONE_FOREIGN_META_INLINE_COUNT has become too small: "
8550 "%d > %d", (int)atop(size
), ZONE_FOREIGN_META_INLINE_COUNT
);
8553 mem
= (vm_offset_t
)pmap_steal_memory(size
);
8555 zone_info
.zi_meta_base
= zone_foreign_meta_array_startup
-
8556 zone_pva_from_addr(mem
).packed_address
;
8557 zone_info
.zi_map_range
[ZONE_ADDR_FOREIGN
].min_address
= mem
;
8558 zone_info
.zi_map_range
[ZONE_ADDR_FOREIGN
].max_address
= mem
+ size
;
8560 zone_info
.zi_bits_range
= (struct zone_map_range
){
8561 .min_address
= (vm_offset_t
)zba_chunk_startup
,
8562 .max_address
= (vm_offset_t
)zba_chunk_startup
+
8563 sizeof(zba_chunk_startup
),
8570 #endif /* !ZALLOC_TEST */
8571 #pragma mark - tests
8572 #if DEBUG || DEVELOPMENT
8575 * Used for sysctl kern.run_zone_test which is not thread-safe. Ensure only one
8576 * thread goes through at a time. Or we can end up with multiple test zones (if
8577 * a second zinit() comes through before zdestroy()), which could lead us to
8580 static SIMPLE_LOCK_DECLARE(zone_test_lock
, 0);
8581 static boolean_t zone_test_running
= FALSE
;
8582 static zone_t test_zone_ptr
= NULL
;
8585 zone_copy_allocations(zone_t z
, uintptr_t *elems
, zone_pva_t page_index
)
8587 vm_offset_t elem_size
= zone_elem_size(z
);
8589 struct zone_page_metadata
*meta
;
8591 while (!zone_pva_is_null(page_index
)) {
8592 base
= zone_pva_to_addr(page_index
);
8593 meta
= zone_pva_to_meta(page_index
);
8595 if (meta
->zm_inline_bitmap
) {
8596 for (size_t i
= 0; i
< meta
->zm_chunk_len
; i
++) {
8597 uint32_t map
= meta
[i
].zm_bitmap
;
8599 for (; map
; map
&= map
- 1) {
8600 *elems
++ = INSTANCE_PUT(base
+
8601 elem_size
* __builtin_clz(map
));
8603 base
+= elem_size
* 32;
8606 uint32_t order
= zba_bits_ref_order(meta
->zm_bitmap
);
8607 bitmap_t
*bits
= zba_bits_ref_ptr(meta
->zm_bitmap
);
8608 for (size_t i
= 0; i
< (1u << order
); i
++) {
8609 uint64_t map
= bits
[i
];
8611 for (; map
; map
&= map
- 1) {
8612 *elems
++ = INSTANCE_PUT(base
+
8613 elem_size
* __builtin_clzll(map
));
8615 base
+= elem_size
* 64;
8619 page_index
= meta
->zm_page_next
;
8625 zone_leaks(const char * zoneName
, uint32_t nameLen
, leak_site_proc proc
, void * refCon
)
8627 uintptr_t zbt
[MAX_ZTRACE_DEPTH
];
8631 uintptr_t element
, bt
;
8632 uint32_t idx
, count
, found
;
8633 uint32_t btidx
, btcount
, nobtcount
, btfound
;
8639 if (!strncmp(zoneName
, z
->z_name
, nameLen
)) {
8645 return KERN_INVALID_NAME
;
8648 elemSize
= (uint32_t)zone_elem_size(zone
);
8649 maxElems
= (zone
->z_elems_avail
+ 1) & ~1ul;
8651 if ((ptoa(zone
->z_percpu
? 1 : zone
->z_chunk_pages
) % elemSize
) &&
8652 !zone_leaks_scan_enable
) {
8653 return KERN_INVALID_CAPABILITY
;
8656 kr
= kmem_alloc_kobject(kernel_map
, (vm_offset_t
*) &array
,
8657 maxElems
* sizeof(uintptr_t), VM_KERN_MEMORY_DIAG
);
8658 if (KERN_SUCCESS
!= kr
) {
8665 next
= zone_copy_allocations(zone
, next
, zone
->z_pageq_partial
);
8666 next
= zone_copy_allocations(zone
, next
, zone
->z_pageq_full
);
8667 count
= (uint32_t)(next
- array
);
8671 zone_leaks_scan(array
, count
, (uint32_t)zone_elem_size(zone
), &found
);
8672 assert(found
<= count
);
8674 for (idx
= 0; idx
< count
; idx
++) {
8675 element
= array
[idx
];
8676 if (kInstanceFlagReferenced
& element
) {
8679 element
= INSTANCE_PUT(element
) & ~kInstanceFlags
;
8682 #if ZONE_ENABLE_LOGGING
8683 if (zone
->zlog_btlog
&& !corruption_debug_flag
) {
8684 // btlog_copy_backtraces_for_elements will set kInstanceFlagReferenced on elements it found
8685 btlog_copy_backtraces_for_elements(zone
->zlog_btlog
, array
, &count
, elemSize
, proc
, refCon
);
8687 #endif /* ZONE_ENABLE_LOGGING */
8689 for (nobtcount
= idx
= 0; idx
< count
; idx
++) {
8690 element
= array
[idx
];
8694 if (kInstanceFlagReferenced
& element
) {
8697 element
= INSTANCE_PUT(element
) & ~kInstanceFlags
;
8699 // see if we can find any backtrace left in the element
8700 btcount
= (typeof(btcount
))(zone_elem_size(zone
) / sizeof(uintptr_t));
8701 if (btcount
>= MAX_ZTRACE_DEPTH
) {
8702 btcount
= MAX_ZTRACE_DEPTH
- 1;
8704 for (btfound
= btidx
= 0; btidx
< btcount
; btidx
++) {
8705 bt
= ((uintptr_t *)element
)[btcount
- 1 - btidx
];
8706 if (!VM_KERNEL_IS_SLID(bt
)) {
8709 zbt
[btfound
++] = bt
;
8712 (*proc
)(refCon
, 1, elemSize
, &zbt
[0], btfound
);
8718 // fake backtrace when we found nothing
8719 zbt
[0] = (uintptr_t) &zalloc
;
8720 (*proc
)(refCon
, nobtcount
, elemSize
, &zbt
[0], 1);
8723 kmem_free(kernel_map
, (vm_offset_t
) array
, maxElems
* sizeof(uintptr_t));
8725 return KERN_SUCCESS
;
8731 unsigned int i
= 0, max_iter
= 5;
8734 zone_t test_pcpu_zone
;
8737 simple_lock(&zone_test_lock
, &zone_locks_grp
);
8738 if (!zone_test_running
) {
8739 zone_test_running
= TRUE
;
8741 simple_unlock(&zone_test_lock
);
8742 printf("run_zone_test: Test already running.\n");
8745 simple_unlock(&zone_test_lock
);
8747 printf("run_zone_test: Testing zinit(), zalloc(), zfree() and zdestroy() on zone \"test_zone_sysctl\"\n");
8749 /* zinit() and zdestroy() a zone with the same name a bunch of times, verify that we get back the same zone each time */
8751 test_zone
= zinit(sizeof(uint64_t), 100 * sizeof(uint64_t), sizeof(uint64_t), "test_zone_sysctl");
8752 if (test_zone
== NULL
) {
8753 printf("run_zone_test: zinit() failed\n");
8758 if (test_zone_ptr
== NULL
&& test_zone
->z_elems_free
!= 0) {
8760 if (test_zone
->z_elems_free
!= 0) {
8762 printf("run_zone_test: free count is not zero\n");
8766 if (test_zone_ptr
== NULL
) {
8767 /* Stash the zone pointer returned on the fist zinit */
8768 printf("run_zone_test: zone created for the first time\n");
8769 test_zone_ptr
= test_zone
;
8770 } else if (test_zone
!= test_zone_ptr
) {
8771 printf("run_zone_test: old zone pointer and new zone pointer don't match\n");
8775 test_ptr
= zalloc(test_zone
);
8776 if (test_ptr
== NULL
) {
8777 printf("run_zone_test: zalloc() failed\n");
8780 zfree(test_zone
, test_ptr
);
8782 zdestroy(test_zone
);
8785 printf("run_zone_test: Iteration %d successful\n", i
);
8786 } while (i
< max_iter
);
8788 /* test Z_VA_SEQUESTER */
8789 if (zsecurity_options
& ZSECURITY_OPTIONS_SEQUESTER
) {
8790 int idx
, num_allocs
= 8;
8791 vm_size_t elem_size
= 2 * PAGE_SIZE
/ num_allocs
;
8792 void *allocs
[num_allocs
];
8794 vm_offset_t phys_pages
= os_atomic_load(&zones_phys_page_mapped_count
, relaxed
);
8796 test_zone
= zone_create("test_zone_sysctl", elem_size
,
8797 ZC_DESTRUCTIBLE
| ZC_SEQUESTER
);
8800 test_pcpu_zone
= zone_create("test_zone_sysctl.pcpu", sizeof(uint64_t),
8801 ZC_DESTRUCTIBLE
| ZC_SEQUESTER
| ZC_PERCPU
);
8802 assert(test_pcpu_zone
);
8804 for (idx
= 0; idx
< num_allocs
; idx
++) {
8805 allocs
[idx
] = zalloc(test_zone
);
8806 assert(NULL
!= allocs
[idx
]);
8807 printf("alloc[%d] %p\n", idx
, allocs
[idx
]);
8809 for (idx
= 0; idx
< num_allocs
; idx
++) {
8810 zfree(test_zone
, allocs
[idx
]);
8812 assert(!zone_pva_is_null(test_zone
->z_pageq_empty
));
8814 kr
= kernel_memory_allocate(kernel_map
,
8815 (vm_address_t
*)&allocs_pcpu
, PAGE_SIZE
,
8816 0, KMA_ZERO
| KMA_KOBJECT
, VM_KERN_MEMORY_DIAG
);
8817 assert(kr
== KERN_SUCCESS
);
8819 for (idx
= 0; idx
< PAGE_SIZE
/ sizeof(uint64_t); idx
++) {
8820 allocs_pcpu
[idx
] = zalloc_percpu(test_pcpu_zone
,
8822 assert(NULL
!= allocs_pcpu
[idx
]);
8824 for (idx
= 0; idx
< PAGE_SIZE
/ sizeof(uint64_t); idx
++) {
8825 zfree_percpu(test_pcpu_zone
, allocs_pcpu
[idx
]);
8827 assert(!zone_pva_is_null(test_pcpu_zone
->z_pageq_empty
));
8829 printf("vm_page_wire_count %d, vm_page_free_count %d, p to v %ld%%\n",
8830 vm_page_wire_count
, vm_page_free_count
,
8831 100L * phys_pages
/ zone_phys_mapped_max_pages
);
8832 zone_gc(ZONE_GC_DRAIN
);
8833 printf("vm_page_wire_count %d, vm_page_free_count %d, p to v %ld%%\n",
8834 vm_page_wire_count
, vm_page_free_count
,
8835 100L * phys_pages
/ zone_phys_mapped_max_pages
);
8837 unsigned int allva
= 0;
8841 allva
+= z
->z_wired_cur
;
8842 if (zone_pva_is_null(z
->z_pageq_va
)) {
8848 zone_pva_t pg
= z
->z_pageq_va
;
8849 struct zone_page_metadata
*page_meta
;
8850 while (pg
.packed_address
) {
8851 page_meta
= zone_pva_to_meta(pg
);
8852 count
+= z
->z_percpu
? 1 : z
->z_chunk_pages
;
8853 if (page_meta
->zm_chunk_len
== ZM_SECONDARY_PAGE
) {
8854 count
-= page_meta
->zm_page_index
;
8856 pg
= page_meta
->zm_page_next
;
8858 assert(z
->z_wired_cur
+ count
== z
->z_va_cur
);
8859 size
= zone_size_wired(z
);
8863 printf("%s%s: seq %d, res %d, %qd %%\n",
8864 zone_heap_name(z
), z
->z_name
, z
->z_va_cur
- z
->z_wired_cur
,
8865 z
->z_wired_cur
, zone_size_allocated(z
) * 100ULL / size
);
8869 printf("total va: %d\n", allva
);
8871 assert(zone_pva_is_null(test_zone
->z_pageq_empty
));
8872 assert(zone_pva_is_null(test_zone
->z_pageq_partial
));
8873 assert(!zone_pva_is_null(test_zone
->z_pageq_va
));
8874 assert(zone_pva_is_null(test_pcpu_zone
->z_pageq_empty
));
8875 assert(zone_pva_is_null(test_pcpu_zone
->z_pageq_partial
));
8876 assert(!zone_pva_is_null(test_pcpu_zone
->z_pageq_va
));
8878 for (idx
= 0; idx
< num_allocs
; idx
++) {
8879 assert(0 == pmap_find_phys(kernel_pmap
, (addr64_t
)(uintptr_t) allocs
[idx
]));
8882 /* make sure the zone is still usable after a GC */
8884 for (idx
= 0; idx
< num_allocs
; idx
++) {
8885 allocs
[idx
] = zalloc(test_zone
);
8886 assert(allocs
[idx
]);
8887 printf("alloc[%d] %p\n", idx
, allocs
[idx
]);
8889 assert(zone_pva_is_null(test_zone
->z_pageq_va
));
8890 assert(test_zone
->z_wired_cur
== test_zone
->z_va_cur
);
8891 for (idx
= 0; idx
< num_allocs
; idx
++) {
8892 zfree(test_zone
, allocs
[idx
]);
8895 for (idx
= 0; idx
< PAGE_SIZE
/ sizeof(uint64_t); idx
++) {
8896 allocs_pcpu
[idx
] = zalloc_percpu(test_pcpu_zone
,
8898 assert(NULL
!= allocs_pcpu
[idx
]);
8900 for (idx
= 0; idx
< PAGE_SIZE
/ sizeof(uint64_t); idx
++) {
8901 zfree_percpu(test_pcpu_zone
, allocs_pcpu
[idx
]);
8904 assert(!zone_pva_is_null(test_pcpu_zone
->z_pageq_empty
));
8905 assert(zone_pva_is_null(test_pcpu_zone
->z_pageq_va
));
8907 kmem_free(kernel_map
, (vm_address_t
)allocs_pcpu
, PAGE_SIZE
);
8909 zdestroy(test_zone
);
8910 zdestroy(test_pcpu_zone
);
8912 printf("run_zone_test: skipping sequester test (not enabled)\n");
8915 printf("run_zone_test: Test passed\n");
8917 simple_lock(&zone_test_lock
, &zone_locks_grp
);
8918 zone_test_running
= FALSE
;
8919 simple_unlock(&zone_test_lock
);
8925 * Routines to test that zone garbage collection and zone replenish threads
8926 * running at the same time don't cause problems.
8930 zone_gc_replenish_test(void)
8932 zone_gc(ZONE_GC_DRAIN
);
8937 zone_alloc_replenish_test(void)
8940 struct data
{ struct data
*next
; } *node
, *list
= NULL
;
8943 * Find a zone that has a replenish thread
8945 zone_index_foreach(i
) {
8947 if (z
->z_replenishes
&& zone_elem_size(z
) >= sizeof(struct data
)) {
8953 printf("Couldn't find a replenish zone\n");
8957 for (uint32_t i
= 0; i
< 2000; ++i
) { /* something big enough to go past replenishment */
8964 * release the memory we allocated
8966 while (list
!= NULL
) {
8973 #endif /* DEBUG || DEVELOPMENT */