2 * Copyright (c) 2000-2013 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@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1987, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <sys/param.h>
71 #include <sys/malloc.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
76 #include <net/route.h>
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/flow_divert.h>
85 #include <sys/event.h>
86 #include <sys/eventvar.h>
88 #include <sys/proc_internal.h>
89 #include <sys/mount_internal.h>
90 #include <sys/vnode_internal.h>
91 #include <sys/ubc_internal.h>
92 #include <sys/namei.h>
93 #include <sys/file_internal.h>
94 #include <sys/filedesc.h>
96 #include <sys/quota.h>
97 #include <sys/uio_internal.h>
98 #include <sys/resourcevar.h>
99 #include <sys/signalvar.h>
100 #include <sys/decmpfs.h>
102 #include <miscfs/specfs/specdev.h>
104 #include <nfs/nfs_conf.h>
105 #include <nfs/rpcv2.h>
106 #include <nfs/nfsproto.h>
107 #include <nfs/nfsnode.h>
108 #include <nfs/nfsmount.h>
110 #include <mach/mach_types.h>
112 #include <kern/zalloc.h>
113 #include <kern/kalloc.h>
117 /* Strings corresponding to types of memory.
118 * Must be in synch with the #defines is sys/malloc.h
119 * NOTE - the reason we pass null strings in some cases is to reduce of foot
120 * print as much as possible for systems where a tiny kernel is needed.
121 * todo - We should probably redesign this and use enums for our types and only
122 * include types needed for that configuration of the kernel. This can't be
123 * done without some kind of kpi since several types are hardwired and exported
124 * (for example see types M_UDFMNT, M_TEMP, etc in sys/malloc.h)
126 const char *memname
[] = {
127 "free", /* 0 M_FREE */
128 "mbuf", /* 1 M_MBUF */
129 "devbuf", /* 2 M_DEVBUF */
130 "socket", /* 3 M_SOCKET */
132 "routetbl", /* 5 M_RTABLE */
133 "hosttbl", /* 6 M_HTABLE */
134 "fragtbl", /* 7 M_FTABLE */
135 "zombie", /* 8 M_ZOMBIE */
136 "ifaddr", /* 9 M_IFADDR */
137 "soopts", /* 10 M_SOOPTS */
138 "soname", /* 11 M_SONAME */
139 "namei", /* 12 M_NAMEI */
140 "gprof", /* 13 M_GPROF */
141 "ioctlops", /* 14 M_IOCTLOPS */
142 "mapmem", /* 15 M_MAPMEM */
143 "cred", /* 16 M_CRED */
144 "pgrp", /* 17 M_PGRP */
145 "session", /* 18 M_SESSION */
146 "iov32", /* 19 M_IOV32 */
147 "mount", /* 20 M_MOUNT */
148 "fhandle", /* 21 M_FHANDLE */
150 "NFS req", /* 22 M_NFSREQ */
151 "NFS mount", /* 23 M_NFSMNT */
152 "NFS node", /* 24 M_NFSNODE */
154 "", /* 22 M_NFSREQ */
155 "", /* 23 M_NFSMNT */
156 "", /* 24 M_NFSNODE */
158 "vnodes", /* 25 M_VNODE */
159 "namecache", /* 26 M_CACHE */
161 "UFS quota", /* 27 M_DQUOT */
165 "proc uuid policy", /* 28 M_PROC_UUID_POLICY */
166 #if (SYSV_SEM || SYSV_MSG || SYSV_SHM)
167 "shm", /* 29 M_SHM */
171 "plimit", /* 30 M_VMMAP */
172 "sigacts", /* 31 M_VMMAPENT */
173 "VM object", /* 32 M_VMOBJ */
174 "VM objhash", /* 33 M_VMOBJHASH */
175 "VM pmap", /* 34 M_VMPMAP */
176 "VM pvmap", /* 35 M_VMPVENT */
177 "VM pager", /* 36 M_VMPAGER */
178 "VM pgdata", /* 37 M_VMPGDATA */
179 "fileproc", /* 38 M_FILEPROC */
180 "file desc", /* 39 M_FILEDESC */
181 "lockf", /* 40 M_LOCKF */
182 "proc", /* 41 M_PROC */
183 "pstats", /* 42 M_SUBPROC */
184 "LFS segment", /* 43 M_SEGMENT */
185 "LFS node", /* 44 M_LFSNODE */
186 "", /* 45 M_FFSNODE */
187 "MFS node", /* 46 M_MFSNODE */
188 "NQNFS Lease", /* 47 M_NQLEASE */
189 "NQNFS Host", /* 48 M_NQMHOST */
190 "Export Host", /* 49 M_NETADDR */
192 "NFS srvsock", /* 50 M_NFSSVC */
193 "NFS uid", /* 51 M_NFSUID */
194 "NFS daemon", /* 52 M_NFSD */
196 "", /* 50 M_NFSSVC */
197 "", /* 51 M_NFSUID */
200 "ip_moptions", /* 53 M_IPMOPTS */
201 "in_multi", /* 54 M_IPMADDR */
202 "ether_multi", /* 55 M_IFMADDR */
203 "mrt", /* 56 M_MRTABLE */
204 "", /* 57 unused entry */
205 "", /* 58 unused entry */
207 "NFSV3 srvdesc",/* 59 M_NFSRVDESC */
208 "NFSV3 diroff", /* 60 M_NFSDIROFF */
209 "NFSV3 bigfh", /* 61 M_NFSBIGFH */
211 "", /* 59 M_NFSRVDESC */
212 "", /* 60 M_NFSDIROFF */
213 "", /* 61 M_NFSBIGFH */
215 "MSDOSFS mount",/* 62 M_MSDOSFSMNT */
216 "MSDOSFS fat", /* 63 M_MSDOSFSFAT */
217 "MSDOSFS node", /* 64 M_MSDOSFSNODE */
218 "ttys", /* 65 M_TTYS */
219 "exec", /* 66 M_EXEC */
220 "miscfs mount", /* 67 M_MISCFSMNT */
221 "miscfs node", /* 68 M_MISCFSNODE */
222 "adosfs mount", /* 69 M_ADOSFSMNT */
223 "adosfs node", /* 70 M_ADOSFSNODE */
224 "adosfs anode", /* 71 M_ANODE */
225 "buf hdrs", /* 72 M_BUFHDR */
226 "ofile tabl", /* 73 M_OFILETABL */
227 "mbuf clust", /* 74 M_MCLUST */
233 "temp", /* 80 M_TEMP */
234 "key mgmt", /* 81 M_SECA */
235 "DEVFS", /* 82 M_DEVFS */
236 "IpFw/IpAcct", /* 83 M_IPFW */
237 "UDF node", /* 84 M_UDFNODE */
238 "UDF mount", /* 85 M_UDFMNT */
240 "IPv6 NDP", /* 86 M_IP6NDP */
241 "IPv6 options", /* 87 M_IP6OPT */
242 "IPv6 Misc", /* 88 M_IP6MISC */
244 "", /* 86 M_IP6NDP */
245 "", /* 87 M_IP6OPT */
246 "", /* 88 M_IP6MISC */
248 "TCP Segment Q",/* 89 M_TSEGQ */
249 "IGMP state", /* 90 M_IGMP */
252 "specinfo", /* 93 M_SPECINFO */
253 "kqueue", /* 94 M_KQUEUE */
255 "cluster_read", /* 96 M_CLRDAHEAD */
256 "cluster_write",/* 97 M_CLWRBEHIND */
257 "iov64", /* 98 M_IOV64 */
258 "fileglob", /* 99 M_FILEGLOB */
259 "kauth", /* 100 M_KAUTH */
260 "dummynet", /* 101 M_DUMMYNET */
261 "", /* 102 M_UNSAFEFS */
262 "macpipelabel", /* 103 M_MACPIPELABEL */
263 "mactemp", /* 104 M_MACTEMP */
264 "sbuf", /* 105 M_SBUF */
265 "extattr", /* 106 M_EXTATTR */
266 "select", /* 107 M_SELECT */
268 "traffic_mgt", /* 108 M_TRAFFIC_MGT */
270 "", /* 108 M_TRAFFIC_MGT */
273 "decmpfs_cnode",/* 109 M_DECMPFS_CNODE */
275 "", /* 109 M_DECMPFS_CNODE */
276 #endif /* FS_COMPRESSION */
277 "ipmfilter", /* 110 M_INMFILTER */
278 "ipmsource", /* 111 M_IPMSOURCE */
279 "in6mfilter", /* 112 M_IN6MFILTER */
280 "ip6mopts", /* 113 M_IP6MOPTS */
281 "ip6msource", /* 114 M_IP6MSOURCE */
283 "flow_divert_pcb", /* 115 M_FLOW_DIVERT_PCB */
284 "flow_divert_group", /* 116 M_FLOW_DIVERT_GROUP */
286 "", /* 115 M_FLOW_DIVERT_PCB */
287 "", /* 116 M_FLOW_DIVERT_GROUP */
289 "ip6cga", /* 117 M_IP6CGA */
291 "necp", /* 118 M_NECP */
292 "necp_session_policy", /* 119 M_NECP_SESSION_POLICY */
293 "necp_socket_policy", /* 120 M_NECP_SOCKET_POLICY */
294 "necp_ip_policy", /* 121 M_NECP_IP_POLICY */
297 "", /* 119 M_NECP_SESSION_POLICY */
298 "", /* 120 M_NECP_SOCKET_POLICY */
299 "", /* 121 M_NECP_IP_POLICY */
301 "fdvnodedata" /* 122 M_FD_VN_DATA */
302 "fddirbuf", /* 123 M_FD_DIRBUF */
303 "netagent", /* 124 M_NETAGENT */
304 "Event Handler",/* 125 M_EVENTHANDLER */
305 "Link Layer Table", /* 126 M_LLTABLE */
306 "Network Work Queue", /* 127 M_NWKWQ */
307 "Content Filter", /* 128 M_CFIL */
311 /* for use with kmzones.kz_zalloczone */
312 #define KMZ_CREATEZONE_ACCT ((void *)-3)
313 #define KMZ_CREATEZONE ((void *)-2)
314 #define KMZ_LOOKUPZONE ((void *)-1)
315 #define KMZ_MALLOC ((void *)0)
316 #define KMZ_SHAREZONE ((void *)1)
321 boolean_t kz_noencrypt
;
322 } kmzones
[M_LAST
] = {
323 #define SOS(sname) sizeof (struct sname)
324 #define SOX(sname) -1
325 { -1, 0, FALSE
}, /* 0 M_FREE */
326 { MSIZE
, KMZ_CREATEZONE
, FALSE
}, /* 1 M_MBUF */
327 { 0, KMZ_MALLOC
, FALSE
}, /* 2 M_DEVBUF */
328 { SOS(socket
), KMZ_CREATEZONE
, TRUE
}, /* 3 M_SOCKET */
329 { SOS(inpcb
), KMZ_LOOKUPZONE
, TRUE
}, /* 4 M_PCB */
330 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 5 M_RTABLE */
331 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 6 M_HTABLE */
332 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 7 M_FTABLE */
333 { SOS(rusage
), KMZ_CREATEZONE
, TRUE
}, /* 8 M_ZOMBIE */
334 { 0, KMZ_MALLOC
, FALSE
}, /* 9 M_IFADDR */
335 { M_MBUF
, KMZ_SHAREZONE
, FALSE
}, /* 10 M_SOOPTS */
336 { 0, KMZ_MALLOC
, FALSE
}, /* 11 M_SONAME */
337 { MAXPATHLEN
, KMZ_CREATEZONE
, FALSE
}, /* 12 M_NAMEI */
338 { 0, KMZ_MALLOC
, FALSE
}, /* 13 M_GPROF */
339 { 0, KMZ_MALLOC
, FALSE
}, /* 14 M_IOCTLOPS */
340 { 0, KMZ_MALLOC
, FALSE
}, /* 15 M_MAPMEM */
341 { SOS(ucred
), KMZ_CREATEZONE
, FALSE
}, /* 16 M_CRED */
342 { SOS(pgrp
), KMZ_CREATEZONE
, FALSE
}, /* 17 M_PGRP */
343 { SOS(session
), KMZ_CREATEZONE
, FALSE
}, /* 18 M_SESSION */
344 { SOS(user32_iovec
), KMZ_LOOKUPZONE
, FALSE
}, /* 19 M_IOV32 */
345 { SOS(mount
), KMZ_CREATEZONE
, FALSE
}, /* 20 M_MOUNT */
346 { 0, KMZ_MALLOC
, FALSE
}, /* 21 M_FHANDLE */
348 { SOS(nfsreq
), KMZ_CREATEZONE
, FALSE
}, /* 22 M_NFSREQ */
349 { SOS(nfsmount
), KMZ_CREATEZONE
, FALSE
}, /* 23 M_NFSMNT */
350 { SOS(nfsnode
), KMZ_CREATEZONE
, FALSE
}, /* 24 M_NFSNODE */
352 { 0, KMZ_MALLOC
, FALSE
}, /* 22 M_NFSREQ */
353 { 0, KMZ_MALLOC
, FALSE
}, /* 23 M_NFSMNT */
354 { 0, KMZ_MALLOC
, FALSE
}, /* 24 M_NFSNODE */
356 { SOS(vnode
), KMZ_CREATEZONE
, TRUE
}, /* 25 M_VNODE */
357 { SOS(namecache
), KMZ_CREATEZONE
, FALSE
}, /* 26 M_CACHE */
359 { SOX(dquot
), KMZ_LOOKUPZONE
, FALSE
}, /* 27 M_DQUOT */
361 { 0, KMZ_MALLOC
, FALSE
}, /* 27 M_DQUOT */
363 { 0, KMZ_MALLOC
, FALSE
}, /* 28 M_PROC_UUID_POLICY */
364 { 0, KMZ_MALLOC
, FALSE
}, /* 29 M_SHM */
365 { SOS(plimit
), KMZ_CREATEZONE
, TRUE
}, /* 30 M_PLIMIT */
366 { SOS(sigacts
), KMZ_CREATEZONE_ACCT
, TRUE
}, /* 31 M_SIGACTS */
367 { 0, KMZ_MALLOC
, FALSE
}, /* 32 M_VMOBJ */
368 { 0, KMZ_MALLOC
, FALSE
}, /* 33 M_VMOBJHASH */
369 { 0, KMZ_MALLOC
, FALSE
}, /* 34 M_VMPMAP */
370 { 0, KMZ_MALLOC
, FALSE
}, /* 35 M_VMPVENT */
371 { 0, KMZ_MALLOC
, FALSE
}, /* 36 M_VMPAGER */
372 { 0, KMZ_MALLOC
, FALSE
}, /* 37 M_VMPGDATA */
373 { SOS(fileproc
), KMZ_CREATEZONE_ACCT
, TRUE
}, /* 38 M_FILEPROC */
374 { SOS(filedesc
), KMZ_CREATEZONE_ACCT
, TRUE
}, /* 39 M_FILEDESC */
375 { SOX(lockf
), KMZ_CREATEZONE_ACCT
, TRUE
}, /* 40 M_LOCKF */
376 { SOS(proc
), KMZ_CREATEZONE
, FALSE
}, /* 41 M_PROC */
377 { SOS(pstats
), KMZ_CREATEZONE
, TRUE
}, /* 42 M_PSTATS */
378 { 0, KMZ_MALLOC
, FALSE
}, /* 43 M_SEGMENT */
379 { M_FFSNODE
, KMZ_SHAREZONE
, FALSE
}, /* 44 M_LFSNODE */
380 { 0, KMZ_MALLOC
, FALSE
}, /* 45 M_FFSNODE */
381 { M_FFSNODE
, KMZ_SHAREZONE
, FALSE
}, /* 46 M_MFSNODE */
382 { 0, KMZ_MALLOC
, FALSE
}, /* 47 M_NQLEASE */
383 { 0, KMZ_MALLOC
, FALSE
}, /* 48 M_NQMHOST */
384 { 0, KMZ_MALLOC
, FALSE
}, /* 49 M_NETADDR */
387 KMZ_CREATEZONE_ACCT
, FALSE
}, /* 50 M_NFSSVC */
388 { 0, KMZ_MALLOC
, FALSE
}, /* 51 M_NFSUID */
390 KMZ_CREATEZONE_ACCT
, FALSE
}, /* 52 M_NFSD */
392 { 0, KMZ_MALLOC
, FALSE
}, /* 50 M_NFSSVC */
393 { 0, KMZ_MALLOC
, FALSE
}, /* 51 M_NFSUID */
394 { 0, KMZ_MALLOC
, FALSE
}, /* 52 M_NFSD */
397 KMZ_LOOKUPZONE
, FALSE
}, /* 53 M_IPMOPTS */
398 { SOX(in_multi
), KMZ_LOOKUPZONE
, FALSE
}, /* 54 M_IPMADDR */
400 KMZ_LOOKUPZONE
, FALSE
}, /* 55 M_IFMADDR */
401 { SOX(mrt
), KMZ_CREATEZONE
, TRUE
}, /* 56 M_MRTABLE */
402 { 0, KMZ_MALLOC
, FALSE
}, /* 57 unused entry */
403 { 0, KMZ_MALLOC
, FALSE
}, /* 58 unused entry */
405 { SOS(nfsrv_descript
),
406 KMZ_CREATEZONE_ACCT
, FALSE
}, /* 59 M_NFSRVDESC */
407 { SOS(nfsdmap
), KMZ_CREATEZONE
, FALSE
}, /* 60 M_NFSDIROFF */
408 { SOS(fhandle
), KMZ_LOOKUPZONE
, FALSE
}, /* 61 M_NFSBIGFH */
410 { 0, KMZ_MALLOC
, FALSE
}, /* 59 M_NFSRVDESC */
411 { 0, KMZ_MALLOC
, FALSE
}, /* 60 M_NFSDIROFF */
412 { 0, KMZ_MALLOC
, FALSE
}, /* 61 M_NFSBIGFH */
414 { 0, KMZ_MALLOC
, FALSE
}, /* 62 M_MSDOSFSMNT */
415 { 0, KMZ_MALLOC
, FALSE
}, /* 63 M_MSDOSFSFAT */
416 { 0, KMZ_MALLOC
, FALSE
}, /* 64 M_MSDOSFSNODE */
417 { SOS(tty
), KMZ_CREATEZONE
, FALSE
}, /* 65 M_TTYS */
418 { 0, KMZ_MALLOC
, FALSE
}, /* 66 M_EXEC */
419 { 0, KMZ_MALLOC
, FALSE
}, /* 67 M_MISCFSMNT */
420 { 0, KMZ_MALLOC
, FALSE
}, /* 68 M_MISCFSNODE */
421 { 0, KMZ_MALLOC
, FALSE
}, /* 69 M_ADOSFSMNT */
422 { 0, KMZ_MALLOC
, FALSE
}, /* 70 M_ADOSFSNODE */
423 { 0, KMZ_MALLOC
, FALSE
}, /* 71 M_ANODE */
424 { 0, KMZ_MALLOC
, TRUE
}, /* 72 M_BUFHDR */
425 { (NDFILE
* OFILESIZE
),
426 KMZ_CREATEZONE_ACCT
, FALSE
}, /* 73 M_OFILETABL */
427 { MCLBYTES
, KMZ_CREATEZONE
, FALSE
}, /* 74 M_MCLUST */
428 { 0, KMZ_MALLOC
, FALSE
}, /* 75 unused */
429 { 0, KMZ_MALLOC
, FALSE
}, /* 76 unused */
430 { 0, KMZ_MALLOC
, FALSE
}, /* 77 unused */
431 { 0, KMZ_MALLOC
, FALSE
}, /* 78 unused */
432 { 0, KMZ_MALLOC
, FALSE
}, /* 79 unused */
433 { 0, KMZ_MALLOC
, FALSE
}, /* 80 M_TEMP */
434 { 0, KMZ_MALLOC
, FALSE
}, /* 81 M_SECA */
435 { 0, KMZ_MALLOC
, FALSE
}, /* 82 M_DEVFS */
436 { 0, KMZ_MALLOC
, FALSE
}, /* 83 M_IPFW */
437 { 0, KMZ_MALLOC
, FALSE
}, /* 84 M_UDFNODE */
438 { 0, KMZ_MALLOC
, FALSE
}, /* 85 M_UDFMOUNT */
439 { 0, KMZ_MALLOC
, FALSE
}, /* 86 M_IP6NDP */
440 { 0, KMZ_MALLOC
, FALSE
}, /* 87 M_IP6OPT */
441 { 0, KMZ_MALLOC
, FALSE
}, /* 88 M_IP6MISC */
442 { 0, KMZ_MALLOC
, FALSE
}, /* 89 M_TSEGQ */
443 { 0, KMZ_MALLOC
, FALSE
}, /* 90 M_IGMP */
444 { 0, KMZ_MALLOC
, FALSE
}, /* 91 unused */
445 { 0, KMZ_MALLOC
, FALSE
}, /* 92 unused */
446 { SOS(specinfo
), KMZ_CREATEZONE
, TRUE
}, /* 93 M_SPECINFO */
447 { SOS(kqueue
), KMZ_CREATEZONE
, FALSE
}, /* 94 M_KQUEUE */
448 { 0, KMZ_MALLOC
, FALSE
}, /* 95 unused */
449 { SOS(cl_readahead
), KMZ_CREATEZONE
, TRUE
}, /* 96 M_CLRDAHEAD */
450 { SOS(cl_writebehind
), KMZ_CREATEZONE
, TRUE
}, /* 97 M_CLWRBEHIND */
451 { SOS(user64_iovec
), KMZ_LOOKUPZONE
, FALSE
}, /* 98 M_IOV64 */
452 { SOS(fileglob
), KMZ_CREATEZONE
, TRUE
}, /* 99 M_FILEGLOB */
453 { 0, KMZ_MALLOC
, FALSE
}, /* 100 M_KAUTH */
454 { 0, KMZ_MALLOC
, FALSE
}, /* 101 M_DUMMYNET */
455 { 0, KMZ_MALLOC
, FALSE
}, /* 102 M_UNSAFEFS */
456 { 0, KMZ_MALLOC
, FALSE
}, /* 103 M_MACPIPELABEL */
457 { 0, KMZ_MALLOC
, FALSE
}, /* 104 M_MACTEMP */
458 { 0, KMZ_MALLOC
, FALSE
}, /* 105 M_SBUF */
459 { 0, KMZ_MALLOC
, FALSE
}, /* 106 M_HFS_EXTATTR */
460 { 0, KMZ_MALLOC
, FALSE
}, /* 107 M_SELECT */
461 { 0, KMZ_MALLOC
, FALSE
}, /* 108 M_TRAFFIC_MGT */
463 { SOS(decmpfs_cnode
), KMZ_CREATEZONE
, FALSE
}, /* 109 M_DECMPFS_CNODE */
465 { 0, KMZ_MALLOC
, FALSE
}, /* 109 M_DECMPFS_CNODE */
466 #endif /* FS_COMPRESSION */
467 { 0, KMZ_MALLOC
, FALSE
}, /* 110 M_INMFILTER */
468 { 0, KMZ_MALLOC
, FALSE
}, /* 111 M_IPMSOURCE */
469 { 0, KMZ_MALLOC
, FALSE
}, /* 112 M_IN6MFILTER */
470 { 0, KMZ_MALLOC
, FALSE
}, /* 113 M_IP6MOPTS */
471 { 0, KMZ_MALLOC
, FALSE
}, /* 114 M_IP6MSOURCE */
473 { SOS(flow_divert_pcb
), KMZ_CREATEZONE
, TRUE
}, /* 115 M_FLOW_DIVERT_PCB */
474 { SOS(flow_divert_group
), KMZ_CREATEZONE
, TRUE
}, /* 116 M_FLOW_DIVERT_GROUP */
476 { 0, KMZ_MALLOC
, FALSE
}, /* 115 M_FLOW_DIVERT_PCB */
477 { 0, KMZ_MALLOC
, FALSE
}, /* 116 M_FLOW_DIVERT_GROUP */
478 #endif /* FLOW_DIVERT */
479 { 0, KMZ_MALLOC
, FALSE
}, /* 117 M_IP6CGA */
480 { 0, KMZ_MALLOC
, FALSE
}, /* 118 M_NECP */
482 { SOS(necp_session_policy
), KMZ_CREATEZONE
, TRUE
}, /* 119 M_NECP_SESSION_POLICY */
483 { SOS(necp_kernel_socket_policy
), KMZ_CREATEZONE
, TRUE
}, /* 120 M_NECP_SOCKET_POLICY */
484 { SOS(necp_kernel_ip_output_policy
), KMZ_CREATEZONE
, TRUE
}, /* 121 M_NECP_IP_POLICY */
486 { 0, KMZ_MALLOC
, FALSE
}, /* 119 M_NECP_SESSION_POLICY */
487 { 0, KMZ_MALLOC
, FALSE
}, /* 120 M_NECP_SOCKET_POLICY */
488 { 0, KMZ_MALLOC
, FALSE
}, /* 121 M_NECP_IP_POLICY */
490 { 0, KMZ_MALLOC
, FALSE
}, /* 122 M_FD_VN_DATA */
491 { 0, KMZ_MALLOC
, FALSE
}, /* 123 M_FD_DIRBUF */
492 { 0, KMZ_MALLOC
, FALSE
}, /* 124 M_NETAGENT */
493 { 0, KMZ_MALLOC
, FALSE
}, /* 125 M_EVENTHANDLER */
494 { 0, KMZ_MALLOC
, FALSE
}, /* 126 M_LLTABLE */
495 { 0, KMZ_MALLOC
, FALSE
}, /* 127 M_NWKWQ */
496 { 0, KMZ_MALLOC
, FALSE
}, /* 128 M_CFIL */
501 extern zone_t
kalloc_zone(vm_size_t
); /* XXX */
504 * Initialize the kernel memory allocator
511 if ((sizeof(kmzones
) / sizeof(kmzones
[0])) != (sizeof(memname
) / sizeof(memname
[0]))) {
512 panic("kmeminit: kmzones has %lu elements but memname has %lu\n",
513 (sizeof(kmzones
) / sizeof(kmzones
[0])), (sizeof(memname
) / sizeof(memname
[0])));
517 while (kmz
< &kmzones
[M_LAST
]) {
519 if (kmz
->kz_elemsize
== (size_t)(-1)) {
523 if (kmz
->kz_zalloczone
== KMZ_CREATEZONE
||
524 kmz
->kz_zalloczone
== KMZ_CREATEZONE_ACCT
) {
525 kmz
->kz_zalloczone
= zinit(kmz
->kz_elemsize
,
526 1024 * 1024, PAGE_SIZE
,
527 memname
[kmz
- kmzones
]);
528 zone_change(kmz
->kz_zalloczone
, Z_CALLERACCT
,
529 (kmz
->kz_zalloczone
== KMZ_CREATEZONE_ACCT
));
531 if (kmz
->kz_noencrypt
== TRUE
) {
532 zone_change(kmz
->kz_zalloczone
, Z_NOENCRYPT
, TRUE
);
534 } else if (kmz
->kz_zalloczone
== KMZ_LOOKUPZONE
) {
535 kmz
->kz_zalloczone
= kalloc_zone(kmz
->kz_elemsize
);
542 while (kmz
< &kmzones
[M_LAST
]) {
544 if (kmz
->kz_elemsize
== (size_t)(-1)) {
548 if (kmz
->kz_zalloczone
== KMZ_SHAREZONE
) {
550 kmzones
[kmz
->kz_elemsize
].kz_zalloczone
;
552 kmzones
[kmz
->kz_elemsize
].kz_elemsize
;
570 static vm_allocation_site_t site
= { .tag
= VM_KERN_MEMORY_KALLOC
, .flags
= VM_TAG_BT
};
571 return __MALLOC(size
, type
, flags
, &site
);
579 vm_allocation_site_t
*site
)
582 vm_size_t msize
= size
;
584 if (type
>= M_LAST
) {
585 panic("_malloc TYPE");
593 panic("Requested size to __MALLOC is too large (%llx)!\n", (uint64_t)size
);
596 if (flags
& M_NOWAIT
) {
597 addr
= (void *)kalloc_canblock(&msize
, FALSE
, site
);
599 addr
= (void *)kalloc_canblock(&msize
, TRUE
, site
);
602 * We get here when the caller told us to block waiting for memory, but
603 * kalloc said there's no memory left to get. Generally, this means there's a
604 * leak or the caller asked for an impossibly large amount of memory. If the caller
605 * is expecting a NULL return code then it should explicitly set the flag M_NULL.
606 * If the caller isn't expecting a NULL return code, we just panic. This is less
607 * than ideal, but returning NULL when the caller isn't expecting it doesn't help
608 * since the majority of callers don't check the return value and will just
609 * dereference the pointer and trap anyway. We may as well get a more
610 * descriptive message out while we can.
612 if (flags
& M_NULL
) {
615 panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size
);
622 if (flags
& M_ZERO
) {
634 if (type
>= M_LAST
) {
639 return; /* correct (convenient bsd kernel legacy) */
650 vm_allocation_site_t
*site
)
655 /* realloc(NULL, ...) is equivalent to malloc(...) */
657 return __MALLOC(size
, type
, flags
, site
);
660 alloc
= kalloc_size(addr
);
662 * Find out the size of the bucket in which the new sized allocation
663 * would land. If it matches the bucket of the original allocation,
664 * simply return the address.
666 if (kalloc_bucket_size(size
) == alloc
) {
667 if (flags
& M_ZERO
) {
669 bzero(addr
+ alloc
, (size
- alloc
));
671 bzero(addr
+ size
, (alloc
- size
));
677 /* Allocate a new, bigger (or smaller) block */
678 if ((newaddr
= __MALLOC(size
, type
, flags
, site
)) == NULL
) {
682 /* Copy over original contents */
683 bcopy(addr
, newaddr
, MIN(size
, alloc
));
690 _MALLOC_ZONE_external(
695 _MALLOC_ZONE_external(
700 return __MALLOC_ZONE(size
, type
, flags
, NULL
);
708 vm_allocation_site_t
*site
)
713 if (type
>= M_LAST
) {
714 panic("_malloc_zone TYPE");
717 kmz
= &kmzones
[type
];
718 if (kmz
->kz_zalloczone
== KMZ_MALLOC
) {
719 panic("_malloc_zone ZONE: type = %d", type
);
723 if (kmz
->kz_elemsize
== (size_t)(-1)) {
724 panic("_malloc_zone XXX");
727 if (size
== kmz
->kz_elemsize
) {
728 if (flags
& M_NOWAIT
) {
729 elem
= (void *)zalloc_noblock(kmz
->kz_zalloczone
);
731 elem
= (void *)zalloc(kmz
->kz_zalloczone
);
734 vm_size_t kalloc_size
= size
;
735 if (size
> kalloc_size
) {
737 } else if (flags
& M_NOWAIT
) {
738 elem
= (void *)kalloc_canblock(&kalloc_size
, FALSE
, site
);
740 elem
= (void *)kalloc_canblock(&kalloc_size
, TRUE
, site
);
744 if (elem
&& (flags
& M_ZERO
)) {
759 if (type
>= M_LAST
) {
763 kmz
= &kmzones
[type
];
764 if (kmz
->kz_zalloczone
== KMZ_MALLOC
) {
765 panic("free_zone ZONE");
769 if (kmz
->kz_elemsize
== (size_t)(-1)) {
770 panic("FREE_SIZE XXX");
773 if (size
== kmz
->kz_elemsize
) {
774 zfree(kmz
->kz_zalloczone
, elem
);
780 #if DEBUG || DEVELOPMENT
782 extern unsigned int zone_map_jetsam_limit
;
785 sysctl_zone_map_jetsam_limit SYSCTL_HANDLER_ARGS
787 #pragma unused(oidp, arg1, arg2)
788 int oldval
= 0, val
= 0, error
= 0;
790 oldval
= zone_map_jetsam_limit
;
791 error
= sysctl_io_number(req
, oldval
, sizeof(int), &val
, NULL
);
792 if (error
|| !req
->newptr
) {
796 if (val
<= 0 || val
> 100) {
797 printf("sysctl_zone_map_jetsam_limit: new jetsam limit value is invalid.\n");
801 zone_map_jetsam_limit
= val
;
805 SYSCTL_PROC(_kern
, OID_AUTO
, zone_map_jetsam_limit
, CTLTYPE_INT
| CTLFLAG_RW
, 0, 0,
806 sysctl_zone_map_jetsam_limit
, "I", "Zone map jetsam limit");
809 extern void get_zone_map_size(uint64_t *current_size
, uint64_t *capacity
);
812 sysctl_zone_map_size_and_capacity SYSCTL_HANDLER_ARGS
814 #pragma unused(oidp, arg1, arg2)
816 get_zone_map_size(&zstats
[0], &zstats
[1]);
818 return SYSCTL_OUT(req
, &zstats
, sizeof(zstats
));
821 SYSCTL_PROC(_kern
, OID_AUTO
, zone_map_size_and_capacity
,
822 CTLTYPE_QUAD
| CTLFLAG_RD
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
823 0, 0, &sysctl_zone_map_size_and_capacity
, "Q", "Current size and capacity of the zone map");
826 extern boolean_t
run_zone_test(void);
829 sysctl_run_zone_test SYSCTL_HANDLER_ARGS
831 #pragma unused(oidp, arg1, arg2)
832 /* require setting this sysctl to prevent sysctl -a from running this */
837 int ret_val
= run_zone_test();
838 return SYSCTL_OUT(req
, &ret_val
, sizeof(ret_val
));
841 SYSCTL_PROC(_kern
, OID_AUTO
, run_zone_test
,
842 CTLTYPE_INT
| CTLFLAG_WR
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
843 0, 0, &sysctl_run_zone_test
, "I", "Test zone allocator KPI");
845 #endif /* DEBUG || DEVELOPMENT */
849 SYSCTL_DECL(_kern_zleak
);
850 SYSCTL_NODE(_kern
, OID_AUTO
, zleak
, CTLFLAG_RW
| CTLFLAG_LOCKED
, 0, "zleak");
855 * Show the status of the zleak subsystem (0 = enabled, 1 = active,
856 * and -1 = failed), and if enabled, allow it to be activated immediately.
859 sysctl_zleak_active SYSCTL_HANDLER_ARGS
861 #pragma unused(arg1, arg2)
862 int oldval
, val
, error
;
864 val
= oldval
= get_zleak_state();
865 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
866 if (error
|| !req
->newptr
) {
870 * Can only be activated if it's off (and not failed.)
871 * Cannot be deactivated once it's on.
873 if (val
== 1 && oldval
== 0) {
874 kern_return_t kr
= zleak_activate();
876 if (KERN_SUCCESS
!= kr
) {
877 printf("zleak_active: failed to activate "
878 "live zone leak debugging (%d).\n", kr
);
881 if (val
== 0 && oldval
== 1) {
882 printf("zleak_active: active, cannot be disabled.\n");
888 SYSCTL_PROC(_kern_zleak
, OID_AUTO
, active
,
889 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
890 0, 0, sysctl_zleak_active
, "I", "zleak activity");
893 * kern.zleak.max_zonemap_size
895 * Read the value of the maximum zonemap size in bytes; useful
896 * as the maximum size that zleak.global_threshold and
897 * zleak.zone_threshold should be set to.
900 sysctl_zleak_max_zonemap_size SYSCTL_HANDLER_ARGS
902 uint64_t zmap_max_size
= *(vm_size_t
*)arg1
;
904 return sysctl_handle_quad(oidp
, &zmap_max_size
, arg2
, req
);
907 SYSCTL_PROC(_kern_zleak
, OID_AUTO
, max_zonemap_size
,
908 CTLTYPE_QUAD
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
909 &zleak_max_zonemap_size
, 0,
910 sysctl_zleak_max_zonemap_size
, "Q", "zleak max zonemap size");
914 sysctl_zleak_threshold SYSCTL_HANDLER_ARGS
916 #pragma unused(oidp, arg2)
918 uint64_t value
= *(vm_size_t
*)arg1
;
920 error
= sysctl_io_number(req
, value
, sizeof(value
), &value
, NULL
);
922 if (error
|| !req
->newptr
) {
926 if (value
> (uint64_t)zleak_max_zonemap_size
) {
930 *(vm_size_t
*)arg1
= value
;
935 * kern.zleak.global_threshold
937 * Set the global zleak threshold size (in bytes). If the zone map
938 * grows larger than this value, zleaks are automatically activated.
940 * The default value is set in zleak_init().
942 SYSCTL_PROC(_kern_zleak
, OID_AUTO
, global_threshold
,
943 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
944 &zleak_global_tracking_threshold
, 0,
945 sysctl_zleak_threshold
, "Q", "zleak global threshold");
948 * kern.zleak.zone_threshold
950 * Set the per-zone threshold size (in bytes) above which any
951 * zone will automatically start zleak tracking.
953 * The default value is set in zleak_init().
955 * Setting this variable will have no effect until zleak tracking is
956 * activated (See above.)
958 SYSCTL_PROC(_kern_zleak
, OID_AUTO
, zone_threshold
,
959 CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
960 &zleak_per_zone_tracking_threshold
, 0,
961 sysctl_zleak_threshold
, "Q", "zleak per-zone threshold");
963 #endif /* CONFIG_ZLEAKS */
965 extern uint64_t get_zones_collectable_bytes(void);
968 sysctl_zones_collectable_bytes SYSCTL_HANDLER_ARGS
970 #pragma unused(oidp, arg1, arg2)
971 uint64_t zones_free_mem
= get_zones_collectable_bytes();
973 return SYSCTL_OUT(req
, &zones_free_mem
, sizeof(zones_free_mem
));
976 SYSCTL_PROC(_kern
, OID_AUTO
, zones_collectable_bytes
,
977 CTLTYPE_QUAD
| CTLFLAG_RD
| CTLFLAG_MASKED
| CTLFLAG_LOCKED
,
978 0, 0, &sysctl_zones_collectable_bytes
, "Q", "Collectable memory in zones");