]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_malloc.c
xnu-2422.110.17.tar.gz
[apple/xnu.git] / bsd / kern / kern_malloc.c
1 /*
2 * Copyright (c) 2000-2013 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 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1987, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
62 */
63 /*
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,
67 * Version 2.0.
68 */
69
70 #include <sys/param.h>
71 #include <sys/malloc.h>
72
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75
76 #include <net/route.h>
77
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/flow_divert.h>
83
84 #include <sys/event.h>
85 #include <sys/eventvar.h>
86
87 #include <sys/proc_internal.h>
88 #include <sys/mount_internal.h>
89 #include <sys/vnode_internal.h>
90 #include <sys/ubc_internal.h>
91 #include <sys/namei.h>
92 #include <sys/file_internal.h>
93 #include <sys/filedesc.h>
94 #include <sys/tty.h>
95 #include <sys/quota.h>
96 #include <sys/uio_internal.h>
97 #include <sys/resourcevar.h>
98 #include <sys/signalvar.h>
99
100 #include <hfs/hfs_cnode.h>
101
102 #include <miscfs/specfs/specdev.h>
103
104 #include <nfs/rpcv2.h>
105 #include <nfs/nfsproto.h>
106 #include <nfs/nfsnode.h>
107 #include <nfs/nfsmount.h>
108
109 #include <vfs/vfs_journal.h>
110
111 #include <mach/mach_types.h>
112
113 #include <kern/zalloc.h>
114 #include <kern/kalloc.h>
115
116 void kmeminit(void);
117
118 /* Strings corresponding to types of memory.
119 * Must be in synch with the #defines is sys/malloc.h
120 * NOTE - the reason we pass null strings in some cases is to reduce of foot
121 * print as much as possible for systems where a tiny kernel is needed.
122 * todo - We should probably redsign this and use enums for our types and only
123 * include types needed for that configuration of the kernel. This can't be
124 * done without some kind of kpi since several types are hardwired and exported
125 * (for example see types M_HFSMNT, M_UDFMNT, M_TEMP, etc in sys/malloc.h)
126 */
127 const char *memname[] = {
128 "free", /* 0 M_FREE */
129 "mbuf", /* 1 M_MBUF */
130 "devbuf", /* 2 M_DEVBUF */
131 "socket", /* 3 M_SOCKET */
132 "pcb", /* 4 M_PCB */
133 "routetbl", /* 5 M_RTABLE */
134 "hosttbl", /* 6 M_HTABLE */
135 "fragtbl", /* 7 M_FTABLE */
136 "zombie", /* 8 M_ZOMBIE */
137 "ifaddr", /* 9 M_IFADDR */
138 "soopts", /* 10 M_SOOPTS */
139 "soname", /* 11 M_SONAME */
140 "namei", /* 12 M_NAMEI */
141 "gprof", /* 13 M_GPROF */
142 "ioctlops", /* 14 M_IOCTLOPS */
143 "mapmem", /* 15 M_MAPMEM */
144 "cred", /* 16 M_CRED */
145 "pgrp", /* 17 M_PGRP */
146 "session", /* 18 M_SESSION */
147 "iov32", /* 19 M_IOV32 */
148 "mount", /* 20 M_MOUNT */
149 "fhandle", /* 21 M_FHANDLE */
150 #if (NFSCLIENT || NFSSERVER)
151 "NFS req", /* 22 M_NFSREQ */
152 "NFS mount", /* 23 M_NFSMNT */
153 "NFS node", /* 24 M_NFSNODE */
154 #else
155 "", /* 22 M_NFSREQ */
156 "", /* 23 M_NFSMNT */
157 "", /* 24 M_NFSNODE */
158 #endif
159 "vnodes", /* 25 M_VNODE */
160 "namecache", /* 26 M_CACHE */
161 #if QUOTA
162 "UFS quota", /* 27 M_DQUOT */
163 #else
164 "", /* 27 M_DQUOT */
165 #endif
166 "proc uuid policy", /* 28 M_PROC_UUID_POLICY */
167 #if (SYSV_SEM || SYSV_MSG || SYSV_SHM)
168 "shm", /* 29 M_SHM */
169 #else
170 "", /* 29 M_SHM */
171 #endif
172 "plimit", /* 30 M_VMMAP */
173 "sigacts", /* 31 M_VMMAPENT */
174 "VM object", /* 32 M_VMOBJ */
175 "VM objhash", /* 33 M_VMOBJHASH */
176 "VM pmap", /* 34 M_VMPMAP */
177 "VM pvmap", /* 35 M_VMPVENT */
178 "VM pager", /* 36 M_VMPAGER */
179 "VM pgdata", /* 37 M_VMPGDATA */
180 "fileproc", /* 38 M_FILEPROC */
181 "file desc", /* 39 M_FILEDESC */
182 "lockf", /* 40 M_LOCKF */
183 "proc", /* 41 M_PROC */
184 "pstats", /* 42 M_SUBPROC */
185 "LFS segment", /* 43 M_SEGMENT */
186 "LFS node", /* 44 M_LFSNODE */
187 "", /* 45 M_FFSNODE */
188 "MFS node", /* 46 M_MFSNODE */
189 "NQNFS Lease", /* 47 M_NQLEASE */
190 "NQNFS Host", /* 48 M_NQMHOST */
191 "Export Host", /* 49 M_NETADDR */
192 #if (NFSCLIENT || NFSSERVER)
193 "NFS srvsock", /* 50 M_NFSSVC */
194 "NFS uid", /* 51 M_NFSUID */
195 "NFS daemon", /* 52 M_NFSD */
196 #else
197 "", /* 50 M_NFSSVC */
198 "", /* 51 M_NFSUID */
199 "", /* 52 M_NFSD */
200 #endif
201 "ip_moptions", /* 53 M_IPMOPTS */
202 "in_multi", /* 54 M_IPMADDR */
203 "ether_multi", /* 55 M_IFMADDR */
204 "mrt", /* 56 M_MRTABLE */
205 "", /* 57 unused entry */
206 "", /* 58 unused entry */
207 #if (NFSCLIENT || NFSSERVER)
208 "NFSV3 srvdesc",/* 59 M_NFSRVDESC */
209 "NFSV3 diroff", /* 60 M_NFSDIROFF */
210 "NFSV3 bigfh", /* 61 M_NFSBIGFH */
211 #else
212 "", /* 59 M_NFSRVDESC */
213 "", /* 60 M_NFSDIROFF */
214 "", /* 61 M_NFSBIGFH */
215 #endif
216 "MSDOSFS mount",/* 62 M_MSDOSFSMNT */
217 "MSDOSFS fat", /* 63 M_MSDOSFSFAT */
218 "MSDOSFS node", /* 64 M_MSDOSFSNODE */
219 "ttys", /* 65 M_TTYS */
220 "exec", /* 66 M_EXEC */
221 "miscfs mount", /* 67 M_MISCFSMNT */
222 "miscfs node", /* 68 M_MISCFSNODE */
223 "adosfs mount", /* 69 M_ADOSFSMNT */
224 "adosfs node", /* 70 M_ADOSFSNODE */
225 "adosfs anode", /* 71 M_ANODE */
226 "buf hdrs", /* 72 M_BUFHDR */
227 "ofile tabl", /* 73 M_OFILETABL */
228 "mbuf clust", /* 74 M_MCLUST */
229 #if HFS
230 "HFS mount", /* 75 M_HFSMNT */
231 "HFS node", /* 76 M_HFSNODE */
232 "HFS fork", /* 77 M_HFSFORK */
233 #else
234 "", /* 75 M_HFSMNT */
235 "", /* 76 M_HFSNODE */
236 "", /* 77 M_HFSFORK */
237 #endif
238 "", /* 78 unused */
239 "", /* 79 unused */
240 "temp", /* 80 M_TEMP */
241 "key mgmt", /* 81 M_SECA */
242 "DEVFS", /* 82 M_DEVFS */
243 "IpFw/IpAcct", /* 83 M_IPFW */
244 "UDF node", /* 84 M_UDFNODE */
245 "UDF mount", /* 85 M_UDFMNT */
246 #if INET6
247 "IPv6 NDP", /* 86 M_IP6NDP */
248 "IPv6 options", /* 87 M_IP6OPT */
249 "IPv6 Misc", /* 88 M_IP6MISC */
250 #else
251 "", /* 86 M_IP6NDP */
252 "", /* 87 M_IP6OPT */
253 "", /* 88 M_IP6MISC */
254 #endif
255 "TCP Segment Q",/* 89 M_TSEGQ */
256 "IGMP state", /* 90 M_IGMP */
257 #if JOURNALING
258 "Journal", /* 91 M_JNL_JNL */
259 "Transaction", /* 92 M_JNL_TR */
260 #else
261 "", /* 91 M_JNL_JNL */
262 "", /* 92 M_JNL_TR */
263 #endif
264 "specinfo", /* 93 M_SPECINFO */
265 "kqueue", /* 94 M_KQUEUE */
266 #if HFS
267 "HFS dirhint", /* 95 M_HFSDIRHINT */
268 #else
269 "", /* 95 M_HFSDIRHINT */
270 #endif
271 "cluster_read", /* 96 M_CLRDAHEAD */
272 "cluster_write",/* 97 M_CLWRBEHIND */
273 "iov64", /* 98 M_IOV64 */
274 "fileglob", /* 99 M_FILEGLOB */
275 "kauth", /* 100 M_KAUTH */
276 "dummynet", /* 101 M_DUMMYNET */
277 "", /* 102 M_UNSAFEFS */
278 "macpipelabel", /* 103 M_MACPIPELABEL */
279 "mactemp", /* 104 M_MACTEMP */
280 "sbuf", /* 105 M_SBUF */
281 "extattr", /* 106 M_EXTATTR */
282 "lctx", /* 107 M_LCTX */
283 #if TRAFFIC_MGT
284 "traffic_mgt", /* 108 M_TRAFFIC_MGT */
285 #else
286 "", /* 108 M_TRAFFIC_MGT */
287 #endif
288 #if HFS_COMPRESSION
289 "decmpfs_cnode",/* 109 M_DECMPFS_CNODE */
290 #else
291 "", /* 109 M_DECMPFS_CNODE */
292 #endif /* HFS_COMPRESSION */
293 "ipmfilter", /* 110 M_INMFILTER */
294 "ipmsource", /* 111 M_IPMSOURCE */
295 "in6mfilter", /* 112 M_IN6MFILTER */
296 "ip6mopts", /* 113 M_IP6MOPTS */
297 "ip6msource", /* 114 M_IP6MSOURCE */
298 #if FLOW_DIVERT
299 "flow_divert_pcb", /* 115 M_FLOW_DIVERT_PCB */
300 "flow_divert_group", /* 116 M_FLOW_DIVERT_GROUP */
301 #else
302 "", /* 115 M_FLOW_DIVERT_PCB */
303 "", /* 116 M_FLOW_DIVERT_GROUP */
304 #endif
305 "ip6cga", /* 117 M_IP6CGA */
306 };
307
308 /* for use with kmzones.kz_zalloczone */
309 #define KMZ_CREATEZONE_ACCT ((void *)-3)
310 #define KMZ_CREATEZONE ((void *)-2)
311 #define KMZ_LOOKUPZONE ((void *)-1)
312 #define KMZ_MALLOC ((void *)0)
313 #define KMZ_SHAREZONE ((void *)1)
314
315 struct kmzones {
316 size_t kz_elemsize;
317 void *kz_zalloczone;
318 boolean_t kz_noencrypt;
319 } kmzones[M_LAST] = {
320 #define SOS(sname) sizeof (struct sname)
321 #define SOX(sname) -1
322 { -1, 0, FALSE }, /* 0 M_FREE */
323 { MSIZE, KMZ_CREATEZONE, FALSE }, /* 1 M_MBUF */
324 { 0, KMZ_MALLOC, FALSE }, /* 2 M_DEVBUF */
325 { SOS(socket), KMZ_CREATEZONE, TRUE }, /* 3 M_SOCKET */
326 { SOS(inpcb), KMZ_LOOKUPZONE, TRUE }, /* 4 M_PCB */
327 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 5 M_RTABLE */
328 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 6 M_HTABLE */
329 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 7 M_FTABLE */
330 { SOS(rusage), KMZ_CREATEZONE, TRUE }, /* 8 M_ZOMBIE */
331 { 0, KMZ_MALLOC, FALSE }, /* 9 M_IFADDR */
332 { M_MBUF, KMZ_SHAREZONE, FALSE }, /* 10 M_SOOPTS */
333 { 0, KMZ_MALLOC, FALSE }, /* 11 M_SONAME */
334 { MAXPATHLEN, KMZ_CREATEZONE, FALSE }, /* 12 M_NAMEI */
335 { 0, KMZ_MALLOC, FALSE }, /* 13 M_GPROF */
336 { 0, KMZ_MALLOC, FALSE }, /* 14 M_IOCTLOPS */
337 { 0, KMZ_MALLOC, FALSE }, /* 15 M_MAPMEM */
338 { SOS(ucred), KMZ_CREATEZONE, FALSE }, /* 16 M_CRED */
339 { SOS(pgrp), KMZ_CREATEZONE, FALSE }, /* 17 M_PGRP */
340 { SOS(session), KMZ_CREATEZONE, FALSE }, /* 18 M_SESSION */
341 { SOS(user32_iovec), KMZ_LOOKUPZONE, FALSE },/* 19 M_IOV32 */
342 { SOS(mount), KMZ_CREATEZONE, FALSE }, /* 20 M_MOUNT */
343 { 0, KMZ_MALLOC, FALSE }, /* 21 M_FHANDLE */
344 #if (NFSCLIENT || NFSSERVER)
345 { SOS(nfsreq), KMZ_CREATEZONE, FALSE }, /* 22 M_NFSREQ */
346 { SOS(nfsmount),KMZ_CREATEZONE, FALSE }, /* 23 M_NFSMNT */
347 { SOS(nfsnode), KMZ_CREATEZONE, FALSE }, /* 24 M_NFSNODE */
348 #else
349 { 0, KMZ_MALLOC, FALSE }, /* 22 M_NFSREQ */
350 { 0, KMZ_MALLOC, FALSE }, /* 23 M_NFSMNT */
351 { 0, KMZ_MALLOC, FALSE }, /* 24 M_NFSNODE */
352 #endif
353 { SOS(vnode), KMZ_CREATEZONE, TRUE }, /* 25 M_VNODE */
354 { SOS(namecache), KMZ_CREATEZONE, FALSE }, /* 26 M_CACHE */
355 #if QUOTA
356 { SOX(dquot), KMZ_LOOKUPZONE, FALSE }, /* 27 M_DQUOT */
357 #else
358 { 0, KMZ_MALLOC, FALSE }, /* 27 M_DQUOT */
359 #endif
360 { 0, KMZ_MALLOC, FALSE }, /* 28 M_PROC_UUID_POLICY */
361 { 0, KMZ_MALLOC, FALSE }, /* 29 M_SHM */
362 { SOS(plimit), KMZ_CREATEZONE, TRUE }, /* 30 M_PLIMIT */
363 { SOS(sigacts), KMZ_CREATEZONE_ACCT, TRUE }, /* 31 M_SIGACTS */
364 { 0, KMZ_MALLOC, FALSE }, /* 32 M_VMOBJ */
365 { 0, KMZ_MALLOC, FALSE }, /* 33 M_VMOBJHASH */
366 { 0, KMZ_MALLOC, FALSE }, /* 34 M_VMPMAP */
367 { 0, KMZ_MALLOC, FALSE }, /* 35 M_VMPVENT */
368 { 0, KMZ_MALLOC, FALSE }, /* 36 M_VMPAGER */
369 { 0, KMZ_MALLOC, FALSE }, /* 37 M_VMPGDATA */
370 { SOS(fileproc),KMZ_CREATEZONE_ACCT, TRUE }, /* 38 M_FILEPROC */
371 { SOS(filedesc),KMZ_CREATEZONE_ACCT, TRUE }, /* 39 M_FILEDESC */
372 { SOX(lockf), KMZ_CREATEZONE_ACCT, TRUE }, /* 40 M_LOCKF */
373 { SOS(proc), KMZ_CREATEZONE, FALSE }, /* 41 M_PROC */
374 { SOS(pstats), KMZ_CREATEZONE, TRUE }, /* 42 M_PSTATS */
375 { 0, KMZ_MALLOC, FALSE }, /* 43 M_SEGMENT */
376 { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 44 M_LFSNODE */
377 { 0, KMZ_MALLOC, FALSE }, /* 45 M_FFSNODE */
378 { M_FFSNODE, KMZ_SHAREZONE, FALSE }, /* 46 M_MFSNODE */
379 { 0, KMZ_MALLOC, FALSE }, /* 47 M_NQLEASE */
380 { 0, KMZ_MALLOC, FALSE }, /* 48 M_NQMHOST */
381 { 0, KMZ_MALLOC, FALSE }, /* 49 M_NETADDR */
382 #if (NFSCLIENT || NFSSERVER)
383 { SOX(nfsrv_sock),
384 KMZ_CREATEZONE_ACCT, FALSE }, /* 50 M_NFSSVC */
385 { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */
386 { SOX(nfsrvcache),
387 KMZ_CREATEZONE_ACCT, FALSE }, /* 52 M_NFSD */
388 #else
389 { 0, KMZ_MALLOC, FALSE }, /* 50 M_NFSSVC */
390 { 0, KMZ_MALLOC, FALSE }, /* 51 M_NFSUID */
391 { 0, KMZ_MALLOC, FALSE }, /* 52 M_NFSD */
392 #endif
393 { SOX(ip_moptions),
394 KMZ_LOOKUPZONE, FALSE }, /* 53 M_IPMOPTS */
395 { SOX(in_multi),KMZ_LOOKUPZONE, FALSE }, /* 54 M_IPMADDR */
396 { SOX(ether_multi),
397 KMZ_LOOKUPZONE, FALSE }, /* 55 M_IFMADDR */
398 { SOX(mrt), KMZ_CREATEZONE, TRUE }, /* 56 M_MRTABLE */
399 { 0, KMZ_MALLOC, FALSE }, /* 57 unused entry */
400 { 0, KMZ_MALLOC, FALSE }, /* 58 unused entry */
401 #if (NFSCLIENT || NFSSERVER)
402 { SOS(nfsrv_descript),
403 KMZ_CREATEZONE_ACCT, FALSE }, /* 59 M_NFSRVDESC */
404 { SOS(nfsdmap), KMZ_CREATEZONE, FALSE }, /* 60 M_NFSDIROFF */
405 { SOS(fhandle), KMZ_LOOKUPZONE, FALSE }, /* 61 M_NFSBIGFH */
406 #else
407 { 0, KMZ_MALLOC, FALSE }, /* 59 M_NFSRVDESC */
408 { 0, KMZ_MALLOC, FALSE }, /* 60 M_NFSDIROFF */
409 { 0, KMZ_MALLOC, FALSE }, /* 61 M_NFSBIGFH */
410 #endif
411 { 0, KMZ_MALLOC, FALSE }, /* 62 M_MSDOSFSMNT */
412 { 0, KMZ_MALLOC, FALSE }, /* 63 M_MSDOSFSFAT */
413 { 0, KMZ_MALLOC, FALSE }, /* 64 M_MSDOSFSNODE */
414 { SOS(tty), KMZ_CREATEZONE, FALSE }, /* 65 M_TTYS */
415 { 0, KMZ_MALLOC, FALSE }, /* 66 M_EXEC */
416 { 0, KMZ_MALLOC, FALSE }, /* 67 M_MISCFSMNT */
417 { 0, KMZ_MALLOC, FALSE }, /* 68 M_MISCFSNODE */
418 { 0, KMZ_MALLOC, FALSE }, /* 69 M_ADOSFSMNT */
419 { 0, KMZ_MALLOC, FALSE }, /* 70 M_ADOSFSNODE */
420 { 0, KMZ_MALLOC, FALSE }, /* 71 M_ANODE */
421 { 0, KMZ_MALLOC, TRUE }, /* 72 M_BUFHDR */
422 { (NDFILE * OFILESIZE),
423 KMZ_CREATEZONE_ACCT, FALSE }, /* 73 M_OFILETABL */
424 { MCLBYTES, KMZ_CREATEZONE, FALSE }, /* 74 M_MCLUST */
425 #if HFS
426 { SOX(hfsmount),KMZ_LOOKUPZONE, FALSE }, /* 75 M_HFSMNT */
427 { SOS(cnode), KMZ_CREATEZONE, TRUE }, /* 76 M_HFSNODE */
428 { SOS(filefork),KMZ_CREATEZONE, TRUE }, /* 77 M_HFSFORK */
429 #else
430 { 0, KMZ_MALLOC, FALSE }, /* 75 M_HFSMNT */
431 { 0, KMZ_MALLOC, FALSE }, /* 76 M_HFSNODE */
432 { 0, KMZ_MALLOC, FALSE }, /* 77 M_HFSFORK */
433 #endif
434 { 0, KMZ_MALLOC, FALSE }, /* 78 unused */
435 { 0, KMZ_MALLOC, FALSE }, /* 79 unused */
436 { 0, KMZ_MALLOC, FALSE }, /* 80 M_TEMP */
437 { 0, KMZ_MALLOC, FALSE }, /* 81 M_SECA */
438 { 0, KMZ_MALLOC, FALSE }, /* 82 M_DEVFS */
439 { 0, KMZ_MALLOC, FALSE }, /* 83 M_IPFW */
440 { 0, KMZ_MALLOC, FALSE }, /* 84 M_UDFNODE */
441 { 0, KMZ_MALLOC, FALSE }, /* 85 M_UDFMOUNT */
442 { 0, KMZ_MALLOC, FALSE }, /* 86 M_IP6NDP */
443 { 0, KMZ_MALLOC, FALSE }, /* 87 M_IP6OPT */
444 { 0, KMZ_MALLOC, FALSE }, /* 88 M_IP6MISC */
445 { 0, KMZ_MALLOC, FALSE }, /* 89 M_TSEGQ */
446 { 0, KMZ_MALLOC, FALSE }, /* 90 M_IGMP */
447 #if JOURNALING
448 { SOS(journal), KMZ_CREATEZONE, FALSE }, /* 91 M_JNL_JNL */
449 { SOS(transaction), KMZ_CREATEZONE, FALSE }, /* 92 M_JNL_TR */
450 #else
451 { 0, KMZ_MALLOC, FALSE }, /* 91 M_JNL_JNL */
452 { 0, KMZ_MALLOC, FALSE }, /* 92 M_JNL_TR */
453 #endif
454 { SOS(specinfo),KMZ_CREATEZONE, TRUE }, /* 93 M_SPECINFO */
455 { SOS(kqueue), KMZ_CREATEZONE, FALSE }, /* 94 M_KQUEUE */
456 #if HFS
457 { SOS(directoryhint), KMZ_CREATEZONE, TRUE }, /* 95 M_HFSDIRHINT */
458 #else
459 { 0, KMZ_MALLOC, FALSE }, /* 95 M_HFSDIRHINT */
460 #endif
461 { SOS(cl_readahead), KMZ_CREATEZONE, TRUE }, /* 96 M_CLRDAHEAD */
462 { SOS(cl_writebehind),KMZ_CREATEZONE, TRUE }, /* 97 M_CLWRBEHIND */
463 { SOS(user64_iovec), KMZ_LOOKUPZONE, FALSE },/* 98 M_IOV64 */
464 { SOS(fileglob), KMZ_CREATEZONE, TRUE }, /* 99 M_FILEGLOB */
465 { 0, KMZ_MALLOC, FALSE }, /* 100 M_KAUTH */
466 { 0, KMZ_MALLOC, FALSE }, /* 101 M_DUMMYNET */
467 { 0, KMZ_MALLOC, FALSE }, /* 102 M_UNSAFEFS */
468 { 0, KMZ_MALLOC, FALSE }, /* 103 M_MACPIPELABEL */
469 { 0, KMZ_MALLOC, FALSE }, /* 104 M_MACTEMP */
470 { 0, KMZ_MALLOC, FALSE }, /* 105 M_SBUF */
471 { 0, KMZ_MALLOC, FALSE }, /* 106 M_HFS_EXTATTR */
472 { 0, KMZ_MALLOC, FALSE }, /* 107 M_LCTX */
473 { 0, KMZ_MALLOC, FALSE }, /* 108 M_TRAFFIC_MGT */
474 #if HFS_COMPRESSION
475 { SOS(decmpfs_cnode),KMZ_CREATEZONE , FALSE}, /* 109 M_DECMPFS_CNODE */
476 #else
477 { 0, KMZ_MALLOC, FALSE }, /* 109 M_DECMPFS_CNODE */
478 #endif /* HFS_COMPRESSION */
479 { 0, KMZ_MALLOC, FALSE }, /* 110 M_INMFILTER */
480 { 0, KMZ_MALLOC, FALSE }, /* 111 M_IPMSOURCE */
481 { 0, KMZ_MALLOC, FALSE }, /* 112 M_IN6MFILTER */
482 { 0, KMZ_MALLOC, FALSE }, /* 113 M_IP6MOPTS */
483 { 0, KMZ_MALLOC, FALSE }, /* 114 M_IP6MSOURCE */
484 #if FLOW_DIVERT
485 { SOS(flow_divert_pcb), KMZ_CREATEZONE, TRUE }, /* 115 M_FLOW_DIVERT_PCB */
486 { SOS(flow_divert_group), KMZ_CREATEZONE, TRUE }, /* 116 M_FLOW_DIVERT_GROUP */
487 #else
488 { 0, KMZ_MALLOC, FALSE }, /* 115 M_FLOW_DIVERT_PCB */
489 { 0, KMZ_MALLOC, FALSE }, /* 116 M_FLOW_DIVERT_GROUP */
490 #endif /* FLOW_DIVERT */
491 { 0, KMZ_MALLOC, FALSE }, /* 117 M_IP6CGA */
492 #undef SOS
493 #undef SOX
494 };
495
496 extern zone_t kalloc_zone(vm_size_t); /* XXX */
497
498 /*
499 * Initialize the kernel memory allocator
500 */
501 void
502 kmeminit(void)
503 {
504 struct kmzones *kmz;
505
506 if ((sizeof(kmzones)/sizeof(kmzones[0])) != (sizeof(memname)/sizeof(memname[0]))) {
507 panic("kmeminit: kmzones has %lu elements but memname has %lu\n",
508 (sizeof(kmzones)/sizeof(kmzones[0])), (sizeof(memname)/sizeof(memname[0])));
509 }
510
511 kmz = kmzones;
512 while (kmz < &kmzones[M_LAST]) {
513 /* XXX */
514 if (kmz->kz_elemsize == (size_t)(-1))
515 ;
516 else
517 /* XXX */
518 if (kmz->kz_zalloczone == KMZ_CREATEZONE ||
519 kmz->kz_zalloczone == KMZ_CREATEZONE_ACCT) {
520 kmz->kz_zalloczone = zinit(kmz->kz_elemsize,
521 1024 * 1024, PAGE_SIZE,
522 memname[kmz - kmzones]);
523 zone_change(kmz->kz_zalloczone, Z_CALLERACCT,
524 (kmz->kz_zalloczone == KMZ_CREATEZONE_ACCT));
525
526 if (kmz->kz_noencrypt == TRUE)
527 zone_change(kmz->kz_zalloczone, Z_NOENCRYPT, TRUE);
528 }
529 else if (kmz->kz_zalloczone == KMZ_LOOKUPZONE)
530 kmz->kz_zalloczone = kalloc_zone(kmz->kz_elemsize);
531
532 kmz++;
533 }
534
535 kmz = kmzones;
536 while (kmz < &kmzones[M_LAST]) {
537 /* XXX */
538 if (kmz->kz_elemsize == (size_t)(-1))
539 ;
540 else
541 /* XXX */
542 if (kmz->kz_zalloczone == KMZ_SHAREZONE) {
543 kmz->kz_zalloczone =
544 kmzones[kmz->kz_elemsize].kz_zalloczone;
545 kmz->kz_elemsize =
546 kmzones[kmz->kz_elemsize].kz_elemsize;
547 }
548
549 kmz++;
550 }
551 }
552
553 struct _mhead {
554 size_t mlen;
555 char dat[0];
556 };
557
558 void *
559 _MALLOC(
560 size_t size,
561 int type,
562 int flags)
563 {
564 struct _mhead *hdr = NULL;
565 size_t memsize = sizeof (*hdr) + size;
566
567 if (type >= M_LAST)
568 panic("_malloc TYPE");
569
570 if (size == 0)
571 return (NULL);
572
573 if (flags & M_NOWAIT) {
574 if (size > memsize) /* overflow detected */
575 return (NULL);
576 else
577 hdr = (void *)kalloc_noblock(memsize);
578 } else {
579 if (size > memsize) {
580 /*
581 * We get here when the caller told us to block, waiting for memory but an overflow
582 * has been detected. The caller isn't expecting a NULL return code so we panic
583 * with a descriptive message.
584 */
585 panic("_MALLOC: overflow detected, size %llu ", (uint64_t) size);
586 }
587 else
588 hdr = (void *)kalloc(memsize);
589
590 if (hdr == NULL) {
591
592 /*
593 * We get here when the caller told us to block waiting for memory, but
594 * kalloc said there's no memory left to get. Generally, this means there's a
595 * leak or the caller asked for an impossibly large amount of memory. Since there's
596 * nothing left to wait for and the caller isn't expecting a NULL return code, we
597 * just panic. This is less than ideal, but returning NULL doesn't help since the
598 * majority of callers don't check the return value and will just dereference the pointer and
599 * trap anyway. We may as well get a more descriptive message out while we can.
600 */
601
602 panic("_MALLOC: kalloc returned NULL (potential leak), size %llu", (uint64_t) size);
603 }
604 }
605 if (!hdr)
606 return (0);
607
608 hdr->mlen = memsize;
609
610 if (flags & M_ZERO)
611 bzero(hdr->dat, size);
612
613 return (hdr->dat);
614 }
615
616 void
617 _FREE(
618 void *addr,
619 int type)
620 {
621 struct _mhead *hdr;
622
623 if (type >= M_LAST)
624 panic("_free TYPE");
625
626 if (!addr)
627 return; /* correct (convenient bsd kernel legacy) */
628
629 hdr = addr; hdr--;
630 kfree(hdr, hdr->mlen);
631 }
632
633 void *
634 _REALLOC(
635 void *addr,
636 size_t size,
637 int type,
638 int flags)
639 {
640 struct _mhead *hdr;
641 void *newaddr;
642 size_t alloc;
643
644 /* realloc(NULL, ...) is equivalent to malloc(...) */
645 if (addr == NULL)
646 return (_MALLOC(size, type, flags));
647
648 /* Allocate a new, bigger (or smaller) block */
649 if ((newaddr = _MALLOC(size, type, flags)) == NULL)
650 return (NULL);
651
652 hdr = addr;
653 --hdr;
654 alloc = hdr->mlen - sizeof (*hdr);
655
656 /* Copy over original contents */
657 bcopy(addr, newaddr, MIN(size, alloc));
658 _FREE(addr, type);
659
660 return (newaddr);
661 }
662
663 void *
664 _MALLOC_ZONE(
665 size_t size,
666 int type,
667 int flags)
668 {
669 struct kmzones *kmz;
670 void *elem;
671
672 if (type >= M_LAST)
673 panic("_malloc_zone TYPE");
674
675 kmz = &kmzones[type];
676 if (kmz->kz_zalloczone == KMZ_MALLOC)
677 panic("_malloc_zone ZONE: type = %d", type);
678
679 /* XXX */
680 if (kmz->kz_elemsize == (size_t)(-1))
681 panic("_malloc_zone XXX");
682 /* XXX */
683 if (size == kmz->kz_elemsize)
684 if (flags & M_NOWAIT) {
685 elem = (void *)zalloc_noblock(kmz->kz_zalloczone);
686 } else {
687 elem = (void *)zalloc(kmz->kz_zalloczone);
688 }
689 else
690 if (flags & M_NOWAIT) {
691 elem = (void *)kalloc_noblock(size);
692 } else {
693 elem = (void *)kalloc(size);
694 }
695
696 return (elem);
697 }
698
699 void
700 _FREE_ZONE(
701 void *elem,
702 size_t size,
703 int type)
704 {
705 struct kmzones *kmz;
706
707 if (type >= M_LAST)
708 panic("FREE_SIZE");
709
710 kmz = &kmzones[type];
711 if (kmz->kz_zalloczone == KMZ_MALLOC)
712 panic("free_zone ZONE");
713
714 /* XXX */
715 if (kmz->kz_elemsize == (size_t)(-1))
716 panic("FREE_SIZE XXX");
717 /* XXX */
718 if (size == kmz->kz_elemsize)
719 zfree(kmz->kz_zalloczone, elem);
720 else
721 kfree(elem, size);
722 }
723
724 #if CONFIG_ZLEAKS
725
726 SYSCTL_DECL(_kern_zleak);
727 SYSCTL_NODE(_kern, OID_AUTO, zleak, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "zleak");
728
729 /*
730 * kern.zleak.active
731 *
732 * Show the status of the zleak subsystem (0 = enabled, 1 = active,
733 * and -1 = failed), and if enabled, allow it to be activated immediately.
734 */
735 static int
736 sysctl_zleak_active SYSCTL_HANDLER_ARGS
737 {
738 #pragma unused(arg1, arg2)
739 int oldval, val, error;
740
741 val = oldval = get_zleak_state();
742 error = sysctl_handle_int(oidp, &val, 0, req);
743 if (error || !req->newptr)
744 return (error);
745 /*
746 * Can only be activated if it's off (and not failed.)
747 * Cannot be deactivated once it's on.
748 */
749 if (val == 1 && oldval == 0) {
750 kern_return_t kr = zleak_activate();
751
752 if (KERN_SUCCESS != kr)
753 printf("zleak_active: failed to activate "
754 "live zone leak debugging (%d).\n", kr);
755 } if (val == 0 && oldval == 1) {
756 printf("zleak_active: active, cannot be disabled.\n");
757 return (EINVAL);
758 }
759 return (0);
760 }
761
762 SYSCTL_PROC(_kern_zleak, OID_AUTO, active,
763 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
764 0, 0, sysctl_zleak_active, "I", "zleak activity");
765
766 /*
767 * kern.zleak.max_zonemap_size
768 *
769 * Read the value of the maximum zonemap size in bytes; useful
770 * as the maximum size that zleak.global_threshold and
771 * zleak.zone_threshold should be set to.
772 */
773 static int
774 sysctl_zleak_max_zonemap_size SYSCTL_HANDLER_ARGS
775 {
776 uint64_t zmap_max_size = *(vm_size_t *)arg1;
777
778 return sysctl_handle_quad(oidp, &zmap_max_size, arg2, req);
779 }
780
781 SYSCTL_PROC(_kern_zleak, OID_AUTO, max_zonemap_size,
782 CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
783 &zleak_max_zonemap_size, 0,
784 sysctl_zleak_max_zonemap_size, "Q", "zleak max zonemap size");
785
786
787 static int
788 sysctl_zleak_threshold SYSCTL_HANDLER_ARGS
789 {
790 #pragma unused(oidp, arg2)
791 int error;
792 uint64_t value = *(vm_size_t *)arg1;
793
794 error = sysctl_io_number(req, value, sizeof (value), &value, NULL);
795
796 if (error || !req->newptr)
797 return (error);
798
799 if (value > (uint64_t)zleak_max_zonemap_size)
800 return (ERANGE);
801
802 *(vm_size_t *)arg1 = value;
803 return (0);
804 }
805
806 /*
807 * kern.zleak.global_threshold
808 *
809 * Set the global zleak threshold size (in bytes). If the zone map
810 * grows larger than this value, zleaks are automatically activated.
811 *
812 * The default value is set in zleak_init().
813 */
814 SYSCTL_PROC(_kern_zleak, OID_AUTO, global_threshold,
815 CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
816 &zleak_global_tracking_threshold, 0,
817 sysctl_zleak_threshold, "Q", "zleak global threshold");
818
819 /*
820 * kern.zleak.zone_threshold
821 *
822 * Set the per-zone threshold size (in bytes) above which any
823 * zone will automatically start zleak tracking.
824 *
825 * The default value is set in zleak_init().
826 *
827 * Setting this variable will have no effect until zleak tracking is
828 * activated (See above.)
829 */
830 SYSCTL_PROC(_kern_zleak, OID_AUTO, zone_threshold,
831 CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED,
832 &zleak_per_zone_tracking_threshold, 0,
833 sysctl_zleak_threshold, "Q", "zleak per-zone threshold");
834
835 #endif /* CONFIG_ZLEAKS */