]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/zalloc.h
xnu-6153.11.26.tar.gz
[apple/xnu.git] / osfmk / kern / zalloc.h
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: zalloc.h
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1985
62 *
63 */
64
65 #ifdef KERNEL_PRIVATE
66
67 #ifndef _KERN_ZALLOC_H_
68 #define _KERN_ZALLOC_H_
69
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>
74
75 #ifdef MACH_KERNEL_PRIVATE
76
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>
82
83 #if KASAN
84 #include <sys/queue.h>
85 #include <san/kasan.h>
86 #endif
87
88 #ifdef CONFIG_ZCACHE
89 #include <kern/zcache.h>
90 #endif
91
92 #if CONFIG_GZALLOC
93 typedef struct gzalloc_data {
94 uint32_t gzfc_index;
95 vm_offset_t *gzfc;
96 } gzalloc_data_t;
97 #endif
98
99 /*
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.
104 *
105 */
106
107 struct zone_free_element;
108 struct zone_page_metadata;
109
110 struct zone {
111 #ifdef CONFIG_ZCACHE
112 struct zone_cache *zcache;
113 #endif /* CONFIG_ZCACHE */
114 struct zone_free_element *free_elements; /* free elements directly linked */
115 struct {
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) */
133 uint64_t
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;
161
162 int index; /* index into zone_info arrays for this zone */
163 const char *zone_name; /* a name for the zone */
164
165 #if CONFIG_ZLEAKS
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;
171 #if CONFIG_GZALLOC
172 gzalloc_data_t gz;
173 #endif /* CONFIG_GZALLOC */
174
175 #if KASAN_ZALLOC
176 vm_size_t kasan_redzone;
177 #endif
178
179 btlog_t *zlog_btlog; /* zone logging structure to hold stacks and element references to those stacks. */
180 };
181
182 /*
183 * structure for tracking zone usage
184 * Used either one per task/thread for all zones or <per-task,per-zone>.
185 */
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;
191
192 /*
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.
196 */
197 extern uint64_t get_zones_collectable_bytes(void);
198
199 /*
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.
203 */
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);
207
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);
211
212 /* Bootstrap zone module (create zone zone) */
213 extern void zone_bootstrap(void);
214
215 /* Init zone module */
216 extern void zone_init(
217 vm_size_t map_size);
218
219 /* Stack use statistics */
220 extern void stack_fake_zone_init(int zone_index);
221 extern void stack_fake_zone_info(
222 int *count,
223 vm_size_t *cur_size,
224 vm_size_t *max_size,
225 vm_size_t *elem_size,
226 vm_size_t *alloc_size,
227 uint64_t *sum_size,
228 int *collectable,
229 int *exhaustable,
230 int *caller_acct);
231
232 #if ZONE_DEBUG
233
234 extern void zone_debug_enable(
235 zone_t z);
236
237 extern void zone_debug_disable(
238 zone_t z);
239
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 */
244
245 extern unsigned int num_zones;
246 extern struct zone zone_array[];
247
248 /* zindex and page_count must pack into 16 bits
249 * update tools/lldbmacros/memory.py:GetRealMetadata
250 * when these values change */
251
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)
256
257 /*
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.
260 */
261 #define ZONE_CHUNK_MAXELEMENTS (UINT16_MAX)
262
263 #endif /* MACH_KERNEL_PRIVATE */
264
265 __BEGIN_DECLS
266
267
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 */
284
285 #ifdef XNU_KERNEL_PRIVATE
286
287 extern vm_offset_t zone_map_min_address;
288 extern vm_offset_t zone_map_max_address;
289
290 /* free an element with no regard for gzalloc, zleaks, or kasan*/
291 extern void zfree_direct( zone_t zone,
292 vm_offset_t elem);
293
294 /* attempts to allocate an element with no regard for gzalloc, zleaks, or kasan*/
295 extern void * zalloc_attempt( zone_t zone);
296
297 /* Non-waiting for memory version of zalloc */
298 extern void * zalloc_nopagewait(
299 zone_t zone);
300
301 /* selective version of zalloc */
302 extern void * zalloc_canblock(
303 zone_t zone,
304 boolean_t canblock);
305
306 /* selective version of zalloc */
307 extern void * zalloc_canblock_tag(
308 zone_t zone,
309 boolean_t canblock,
310 vm_size_t reqsize,
311 vm_tag_t tag);
312
313 /* Get from zone free list */
314 extern void * zget(
315 zone_t zone);
316
317 /* Fill zone with memory */
318 extern void zcram(
319 zone_t zone,
320 vm_offset_t newmem,
321 vm_size_t size);
322
323 /* Initially fill zone with specified number of elements */
324 extern int zfill(
325 zone_t zone,
326 int nelem);
327
328 extern void zone_prio_refill_configure(zone_t, vm_size_t);
329
330 /* See above/top of file. Z_* definitions moved so they would be usable by kexts */
331
332 /* Preallocate space for zone from zone map */
333 extern void zprealloc(
334 zone_t zone,
335 vm_size_t size);
336
337 extern integer_t zone_free_count(
338 zone_t zone);
339
340 extern vm_size_t zone_element_size(
341 void *addr,
342 zone_t *z);
343
344 /*
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.
348 */
349 struct ztrace {
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 */
355 };
356
357 #if CONFIG_ZLEAKS
358
359 /* support for the kern.zleak.* sysctls */
360
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;
365
366 extern int get_zleak_state(void);
367
368 #endif /* CONFIG_ZLEAKS */
369
370 #ifndef VM_MAX_TAG_ZONES
371 #error MAX_TAG_ZONES
372 #endif
373
374 #if VM_MAX_TAG_ZONES
375
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);
378
379 #endif /* VM_MAX_TAG_ZONES */
380
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);
385
386 #define lock_zone(zone) \
387 MACRO_BEGIN \
388 lck_mtx_lock_spin_always(&(zone)->lock); \
389 MACRO_END
390
391 #define unlock_zone(zone) \
392 MACRO_BEGIN \
393 lck_mtx_unlock(&(zone)->lock); \
394 MACRO_END
395
396 #if CONFIG_GZALLOC
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);
403
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 */
408
409 /* Callbacks for btlog lock/unlock */
410 void zlog_btlog_lock(__unused void *);
411 void zlog_btlog_unlock(__unused void *);
412
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);
416 #endif
417
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 */
422
423 #endif /* XNU_KERNEL_PRIVATE */
424
425 /* Allocate from zone */
426 extern void * zalloc(
427 zone_t zone);
428
429 /* Non-blocking version of zalloc */
430 extern void * zalloc_noblock(
431 zone_t zone);
432
433 /* Free zone element */
434 extern void zfree(
435 zone_t zone,
436 void *elem);
437
438 #ifdef XNU_KERNEL_PRIVATE
439 #define zfree(zone, elem) \
440 _Pragma("clang diagnostic push") \
441 _Pragma("clang diagnostic ignored \"-Wshadow\"") \
442 do { \
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); \
448 } while (0) \
449 _Pragma("clang diagnostic pop")
450 #endif /* XNU_KERNEL_PRIVATE */
451
452 /* Create zone */
453 extern zone_t zinit(
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 */
458
459 /* Change zone parameters */
460 extern void zone_change(
461 zone_t zone,
462 unsigned int item,
463 boolean_t value);
464
465 /* Destroy the zone */
466 extern void zdestroy(
467 zone_t zone);
468
469 #ifdef XNU_KERNEL_PRIVATE
470
471 /* Panic if a pointer is not mapped to the zone specified */
472 extern void zone_require(
473 void *addr,
474 zone_t expected_zone);
475
476 #endif /* XNU_KERNEL_PRIVATE */
477
478 __END_DECLS
479
480 #endif /* _KERN_ZALLOC_H_ */
481
482 #endif /* KERNEL_PRIVATE */