2 * Copyright (c) 2000-2019 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.
67 #ifndef _KERN_ZALLOC_H_
68 #define _KERN_ZALLOC_H_
70 #include <mach/machine/vm_types.h>
71 #include <mach_debug/zone_info.h>
72 #include <kern/kern_types.h>
73 #include <sys/cdefs.h>
75 #ifdef MACH_KERNEL_PRIVATE
77 #include <zone_debug.h>
78 #include <kern/locks.h>
79 #include <kern/queue.h>
80 #include <kern/thread_call.h>
81 #include <kern/btlog.h>
84 #include <sys/queue.h>
85 #include <san/kasan.h>
89 #include <kern/zcache.h>
93 typedef struct gzalloc_data
{
100 * A zone is a collection of fixed size blocks for which there
101 * is fast allocation/deallocation access. Kernel routines can
102 * use zones to manage data structures dynamically, creating a zone
103 * for each type of data structure to be managed.
107 struct zone_free_element
;
108 struct zone_page_metadata
;
112 struct zone_cache
*zcache
;
113 #endif /* CONFIG_ZCACHE */
114 struct zone_free_element
*free_elements
; /* free elements directly linked */
116 queue_head_t any_free_foreign
; /* foreign pages crammed into zone */
117 queue_head_t all_free
;
118 queue_head_t intermediate
;
119 queue_head_t all_used
;
120 } pages
; /* list of zone_page_metadata structs, which maintain per-page free element lists */
121 int count
; /* Number of elements used now */
122 int countfree
; /* Number of free elements */
123 int count_all_free_pages
; /* Number of pages collectable by GC */
124 lck_attr_t lock_attr
; /* zone lock attribute */
125 decl_lck_mtx_data(, lock
); /* zone lock */
126 lck_mtx_ext_t lock_ext
; /* placeholder for indirect mutex */
127 vm_size_t cur_size
; /* current memory utilization */
128 vm_size_t max_size
; /* how large can this zone grow */
129 vm_size_t elem_size
; /* size of an element */
130 vm_size_t alloc_size
; /* size used for more memory */
131 uint64_t page_count
__attribute__((aligned(8))); /* number of pages used by this zone */
132 uint64_t sum_count
; /* count of allocs (life of zone) */
134 /* boolean_t */ exhaustible
:1, /* (F) merely return if empty? */
135 /* boolean_t */ collectable
:1, /* (F) garbage collect empty pages */
136 /* boolean_t */ expandable
:1, /* (T) expand zone (with message)? */
137 /* boolean_t */ allows_foreign
:1, /* (F) allow non-zalloc space */
138 /* boolean_t */ doing_alloc_without_vm_priv
:1, /* is zone expanding now via a non-vm_privileged thread? */
139 /* boolean_t */ doing_alloc_with_vm_priv
:1, /* is zone expanding now via a vm_privileged thread? */
140 /* boolean_t */ waiting
:1, /* is thread waiting for expansion? */
141 /* boolean_t */ async_pending
:1, /* asynchronous allocation pending? */
142 /* boolean_t */ zleak_on
:1, /* Are we collecting allocation information? */
143 /* boolean_t */ caller_acct
:1, /* do we account allocation/free to the caller? */
144 /* boolean_t */ noencrypt
:1,
145 /* boolean_t */ no_callout
:1,
146 /* boolean_t */ async_prio_refill
:1,
147 /* boolean_t */ gzalloc_exempt
:1,
148 /* boolean_t */ alignment_required
:1,
149 /* boolean_t */ zone_logging
:1, /* Enable zone logging for this zone. */
150 /* boolean_t */ zone_replenishing
:1,
151 /* boolean_t */ kasan_quarantine
:1,
152 /* boolean_t */ tags
:1,
153 /* boolean_t */ tags_inline
:1,
154 /* future */ tag_zone_index
:6,
155 /* boolean_t */ zone_valid
:1,
156 /* boolean_t */ cpu_cache_enable_when_ready
:1,
157 /* boolean_t */ cpu_cache_enabled
:1,
158 /* boolean_t */ clear_memory
:1,
159 /* boolean_t */ zone_destruction
:1,
160 /* future */ _reserved
:33;
162 int index
; /* index into zone_info arrays for this zone */
163 const char *zone_name
; /* a name for the zone */
166 uint32_t zleak_capture
; /* per-zone counter for capturing every N allocations */
167 #endif /* CONFIG_ZLEAKS */
168 uint32_t zp_count
; /* counter for poisoning every N frees */
169 vm_size_t prio_refill_watermark
;
170 thread_t zone_replenish_thread
;
173 #endif /* CONFIG_GZALLOC */
176 vm_size_t kasan_redzone
;
179 btlog_t
*zlog_btlog
; /* zone logging structure to hold stacks and element references to those stacks. */
183 * structure for tracking zone usage
184 * Used either one per task/thread for all zones or <per-task,per-zone>.
186 typedef struct zinfo_usage_store_t
{
187 /* These fields may be updated atomically, and so must be 8 byte aligned */
188 uint64_t alloc
__attribute__((aligned(8))); /* allocation counter */
189 uint64_t free
__attribute__((aligned(8))); /* free counter */
190 } zinfo_usage_store_t
;
193 * For sysctl kern.zones_collectable_bytes used by memory_maintenance to check if a
194 * userspace reboot is needed. The only other way to query for this information
195 * is via mach_memory_info() which is unavailable on release kernels.
197 extern uint64_t get_zones_collectable_bytes(void);
200 * zone_gc also checks if the zone_map is getting close to full and triggers jetsams if needed, provided
201 * consider_jetsams is set to TRUE. To avoid deadlocks, we only pass a value of TRUE from within the
202 * vm_pageout_garbage_collect thread.
204 extern void zone_gc(boolean_t consider_jetsams
);
205 extern void consider_zone_gc(boolean_t consider_jetsams
);
206 extern void drop_free_elements(zone_t z
);
208 /* Debug logging for zone-map-exhaustion jetsams. */
209 extern void get_zone_map_size(uint64_t *current_size
, uint64_t *capacity
);
210 extern void get_largest_zone_info(char *zone_name
, size_t zone_name_len
, uint64_t *zone_size
);
212 /* Bootstrap zone module (create zone zone) */
213 extern void zone_bootstrap(void);
215 /* Init zone module */
216 extern void zone_init(
219 /* Stack use statistics */
220 extern void stack_fake_zone_init(int zone_index
);
221 extern void stack_fake_zone_info(
225 vm_size_t
*elem_size
,
226 vm_size_t
*alloc_size
,
234 extern void zone_debug_enable(
237 extern void zone_debug_disable(
240 #define zone_debug_enabled(z) z->active_zones.next
241 #define ROUNDUP(x, y) ((((x)+(y)-1)/(y))*(y))
242 #define ZONE_DEBUG_OFFSET ROUNDUP(sizeof(queue_chain_t),16)
243 #endif /* ZONE_DEBUG */
245 extern unsigned int num_zones
;
246 extern struct zone zone_array
[];
248 /* zindex and page_count must pack into 16 bits
249 * update tools/lldbmacros/memory.py:GetRealMetadata
250 * when these values change */
252 #define ZINDEX_BITS (10U)
253 #define PAGECOUNT_BITS (16U - ZINDEX_BITS)
254 #define MULTIPAGE_METADATA_MAGIC ((1UL << ZINDEX_BITS) - 1)
255 #define ZONE_CHUNK_MAXPAGES ((1UL << PAGECOUNT_BITS) - 1)
258 * The max # of elements in a chunk should fit into zone_page_metadata.free_count (uint16_t).
259 * Update this if the type of free_count changes.
261 #define ZONE_CHUNK_MAXELEMENTS (UINT16_MAX)
263 #endif /* MACH_KERNEL_PRIVATE */
268 /* Item definitions for zalloc/zinit/zone_change */
269 #define Z_EXHAUST 1 /* Make zone exhaustible */
270 #define Z_COLLECT 2 /* Make zone collectable */
271 #define Z_EXPAND 3 /* Make zone expandable */
272 #define Z_FOREIGN 4 /* Allow collectable zone to contain foreign elements */
273 #define Z_CALLERACCT 5 /* Account alloc/free against the caller */
274 #define Z_NOENCRYPT 6 /* Don't encrypt zone during hibernation */
275 #define Z_NOCALLOUT 7 /* Don't asynchronously replenish the zone via callouts */
276 #define Z_ALIGNMENT_REQUIRED 8
277 #define Z_GZALLOC_EXEMPT 9 /* Not tracked in guard allocation mode */
278 #define Z_KASAN_QUARANTINE 10 /* Allow zone elements to be quarantined on free */
279 #ifdef XNU_KERNEL_PRIVATE
280 #define Z_TAGS_ENABLED 11 /* Store tags */
281 #endif /* XNU_KERNEL_PRIVATE */
282 #define Z_CACHING_ENABLED 12 /*enable and initialize per-cpu caches for the zone*/
283 #define Z_CLEARMEMORY 13 /* Use KMA_ZERO on new allocations */
285 #ifdef XNU_KERNEL_PRIVATE
287 extern vm_offset_t zone_map_min_address
;
288 extern vm_offset_t zone_map_max_address
;
290 /* free an element with no regard for gzalloc, zleaks, or kasan*/
291 extern void zfree_direct( zone_t zone
,
294 /* attempts to allocate an element with no regard for gzalloc, zleaks, or kasan*/
295 extern void * zalloc_attempt( zone_t zone
);
297 /* Non-waiting for memory version of zalloc */
298 extern void * zalloc_nopagewait(
301 /* selective version of zalloc */
302 extern void * zalloc_canblock(
306 /* selective version of zalloc */
307 extern void * zalloc_canblock_tag(
313 /* Get from zone free list */
317 /* Fill zone with memory */
323 /* Initially fill zone with specified number of elements */
328 extern void zone_prio_refill_configure(zone_t
, vm_size_t
);
330 /* See above/top of file. Z_* definitions moved so they would be usable by kexts */
332 /* Preallocate space for zone from zone map */
333 extern void zprealloc(
337 extern integer_t
zone_free_count(
340 extern vm_size_t
zone_element_size(
345 * Structure for keeping track of a backtrace, used for leak detection.
346 * This is in the .h file because it is used during panic, see kern/debug.c
347 * A non-zero size indicates that the trace is in use.
350 vm_size_t zt_size
; /* How much memory are all the allocations referring to this trace taking up? */
351 uint32_t zt_depth
; /* depth of stack (0 to MAX_ZTRACE_DEPTH) */
352 void* zt_stack
[MAX_ZTRACE_DEPTH
]; /* series of return addresses from OSBacktrace */
353 uint32_t zt_collisions
; /* How many times did a different stack land here while it was occupied? */
354 uint32_t zt_hit_count
; /* for determining effectiveness of hash function */
359 /* support for the kern.zleak.* sysctls */
361 extern kern_return_t
zleak_activate(void);
362 extern vm_size_t zleak_max_zonemap_size
;
363 extern vm_size_t zleak_global_tracking_threshold
;
364 extern vm_size_t zleak_per_zone_tracking_threshold
;
366 extern int get_zleak_state(void);
368 #endif /* CONFIG_ZLEAKS */
370 #ifndef VM_MAX_TAG_ZONES
376 extern boolean_t zone_tagging_on
;
377 extern uint32_t zone_index_from_tag_index(uint32_t tag_zone_index
, vm_size_t
* elem_size
);
379 #endif /* VM_MAX_TAG_ZONES */
381 /* These functions used for leak detection both in zalloc.c and mbuf.c */
382 extern uintptr_t hash_mix(uintptr_t);
383 extern uint32_t hashbacktrace(uintptr_t *, uint32_t, uint32_t);
384 extern uint32_t hashaddr(uintptr_t, uint32_t);
386 #define lock_zone(zone) \
388 lck_mtx_lock_spin_always(&(zone)->lock); \
391 #define unlock_zone(zone) \
393 lck_mtx_unlock(&(zone)->lock); \
397 void gzalloc_init(vm_size_t
);
398 void gzalloc_zone_init(zone_t
);
399 void gzalloc_configure(void);
400 void gzalloc_reconfigure(zone_t
);
401 void gzalloc_empty_free_cache(zone_t
);
402 boolean_t
gzalloc_enabled(void);
404 vm_offset_t
gzalloc_alloc(zone_t
, boolean_t
);
405 boolean_t
gzalloc_free(zone_t
, void *);
406 boolean_t
gzalloc_element_size(void *, zone_t
*, vm_size_t
*);
407 #endif /* CONFIG_GZALLOC */
409 /* Callbacks for btlog lock/unlock */
410 void zlog_btlog_lock(__unused
void *);
411 void zlog_btlog_unlock(__unused
void *);
413 #ifdef MACH_KERNEL_PRIVATE
414 #define MAX_ZONE_NAME 32 /* max length of a zone name we can take from the boot-args */
415 int track_this_zone(const char *zonename
, const char *logname
);
418 #if DEBUG || DEVELOPMENT
419 extern boolean_t
run_zone_test(void);
420 extern vm_size_t
zone_element_info(void *addr
, vm_tag_t
* ptag
);
421 #endif /* DEBUG || DEVELOPMENT */
423 #endif /* XNU_KERNEL_PRIVATE */
425 /* Allocate from zone */
426 extern void * zalloc(
429 /* Non-blocking version of zalloc */
430 extern void * zalloc_noblock(
433 /* Free zone element */
438 #ifdef XNU_KERNEL_PRIVATE
439 #define zfree(zone, elem) \
440 _Pragma("clang diagnostic push") \
441 _Pragma("clang diagnostic ignored \"-Wshadow\"") \
443 _Static_assert(sizeof (elem) == sizeof (void *) || sizeof (elem) == sizeof (mach_vm_address_t), "elem is not a pointer"); \
444 void *__tmp_addr = (void *) elem; \
445 zone_t __tmp_zone = zone; \
446 elem = (__typeof__(elem)) NULL; \
447 (zfree)(__tmp_zone, __tmp_addr); \
449 _Pragma("clang diagnostic pop")
450 #endif /* XNU_KERNEL_PRIVATE */
454 vm_size_t size
, /* the size of an element */
455 vm_size_t maxmem
, /* maximum memory to use */
456 vm_size_t alloc
, /* allocation size */
457 const char *name
); /* a name for the zone */
459 /* Change zone parameters */
460 extern void zone_change(
465 /* Destroy the zone */
466 extern void zdestroy(
469 #ifdef XNU_KERNEL_PRIVATE
471 /* Panic if a pointer is not mapped to the zone specified */
472 extern void zone_require(
474 zone_t expected_zone
);
476 #endif /* XNU_KERNEL_PRIVATE */
480 #endif /* _KERN_ZALLOC_H_ */
482 #endif /* KERNEL_PRIVATE */