xref: /openbsd/sys/sys/malloc.h (revision 419215b4)
1 /*	$OpenBSD: malloc.h,v 1.126 2023/12/19 14:40:04 deraadt Exp $	*/
2 /*	$NetBSD: malloc.h,v 1.39 1998/07/12 19:52:01 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 1987, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)malloc.h	8.5 (Berkeley) 5/3/95
33  */
34 
35 #ifndef _SYS_MALLOC_H_
36 #define	_SYS_MALLOC_H_
37 
38 #include <sys/queue.h>
39 
40 #define KERN_MALLOC_BUCKETS	1
41 #define KERN_MALLOC_BUCKET	2
42 #define KERN_MALLOC_KMEMNAMES	3
43 #define KERN_MALLOC_KMEMSTATS	4
44 #define KERN_MALLOC_MAXID	5
45 
46 #define CTL_KERN_MALLOC_NAMES { \
47 	{ 0, 0 }, \
48 	{ "buckets", CTLTYPE_STRING }, \
49 	{ "bucket", CTLTYPE_NODE }, \
50 	{ "kmemnames", CTLTYPE_STRING }, \
51 	{ "kmemstat", CTLTYPE_NODE }, \
52 }
53 
54 /*
55  * flags to malloc
56  */
57 #define	M_WAITOK	0x0001
58 #define	M_NOWAIT	0x0002
59 #define	M_CANFAIL	0x0004
60 #define	M_ZERO		0x0008
61 
62 /*
63  * Types of memory to be allocated
64  */
65 #define	M_FREE		0	/* should be on free list */
66 /* 1 - free */
67 #define	M_DEVBUF	2	/* device driver memory */
68 /* 3 - free */
69 #define	M_PCB		4	/* protocol control blocks */
70 #define	M_RTABLE	5	/* routing tables */
71 #define	M_PF		6	/* packet filter structures */
72 /* 7 - free */
73 /* 8 - free */
74 #define	M_IFADDR	9	/* interface addresses */
75 #define	M_IFGROUP	10	/* interface groups */
76 #define	M_SYSCTL	11	/* sysctl persistent buffers */
77 #define	M_COUNTERS	12	/* per-CPU counters via counters_alloc(9) */
78 /* 13 - free */
79 #define	M_IOCTLOPS	14	/* ioctl data buffers */
80 /* 15-18 - free */
81 #define	M_IOV		19	/* large IOVs */
82 #define	M_MOUNT		20	/* VFS mount structs */
83 /* 21 - free */
84 #define	M_NFSREQ	22	/* NFS request headers */
85 #define	M_NFSMNT	23	/* NFS mount structures */
86 #define	M_LOG		24	/* messages in kernel log stash */
87 #define	M_VNODE		25	/* Dynamically allocated vnodes */
88 /* 26 - free */
89 #define	M_DQUOT		27	/* UFS quota entries */
90 #define	M_UFSMNT	28	/* UFS mount structures */
91 #define	M_SHM		29	/* SVID compatible shared memory segments */
92 #define	M_VMMAP		30	/* VM map structures */
93 #define	M_SEM		31	/* SVID compatible semaphores */
94 #define	M_DIRHASH	32	/* UFS directory hash structures */
95 #define	M_ACPI		33	/* ACPI structures */
96 #define	M_VMPMAP	34	/* VM pmap data */
97 /* 35-38 - free */
98 #define	M_FILEDESC	39	/* open file descriptor tables */
99 #define	M_SIGIO		40	/* sigio structures */
100 #define	M_PROC		41	/* proc structures */
101 #define	M_SUBPROC	42	/* proc sub-structures */
102 /* 43-45 - free */
103 #define	M_MFSNODE	46	/* MFS vnode private part */
104 /* 47-48 - free */
105 #define	M_NETADDR	49	/* export host address structures */
106 #define	M_NFSSVC	50	/* NFS server structures */
107 /* 51 - free */
108 #define	M_NFSD		52	/* NFS server daemon structures */
109 #define	M_IPMOPTS	53	/* internet multicast options */
110 #define	M_IPMADDR	54	/* internet multicast addresses */
111 #define	M_IFMADDR	55	/* link-level multicast addresses */
112 #define	M_MRTABLE	56	/* multicast routing tables */
113 #define	M_ISOFSMNT	57	/* ISOFS mount structures */
114 #define	M_ISOFSNODE	58	/* ISOFS vnode private part */
115 #define	M_MSDOSFSMNT	59	/* MSDOS FS mount structures */
116 #define	M_MSDOSFSFAT	60	/* MSDOS FS FAT tables */
117 #define	M_MSDOSFSNODE	61	/* MSDOS FS vnode private part */
118 #define	M_TTYS		62	/* allocated tty structures */
119 #define	M_EXEC		63	/* argument lists & other mem used by exec */
120 #define	M_MISCFSMNT	64	/* miscellaneous FS mount structures */
121 #define	M_FUSEFS	65	/* FUSE FS mount structures */
122 /* 66-73 - free */
123 #define	M_PFKEY		74	/* pfkey data */
124 #define	M_TDB		75	/* transforms database */
125 #define	M_XDATA		76	/* IPsec data */
126 /* 77 - free */
127 #define	M_PAGEDEP	78	/* file page dependencies */
128 #define	M_INODEDEP	79	/* inode dependencies */
129 #define	M_NEWBLK	80	/* new block allocation */
130 /* 81-82 - free */
131 #define	M_INDIRDEP	83	/* indirect block dependencies */
132 /* 84-91 - free */
133 #define	M_VMSWAP	92	/* VM swap structures */
134 /* 93-97 - free */
135 #define	M_UVMAMAP	98	/* UVM amap and related */
136 #define	M_UVMAOBJ	99	/* UVM aobj and related */
137 #define	M_PINSYSCALL	100	/* pinsyscall */
138 #define	M_USB		101	/* USB general */
139 #define	M_USBDEV	102	/* USB device driver */
140 #define	M_USBHC		103	/* USB host controller */
141 #define	M_WITNESS	104	/* witness(4) memory */
142 #define	M_MEMDESC	105	/* memory range */
143 /* 106-107 - free */
144 #define	M_CRYPTO_DATA	108	/* crypto(9) data buffers */
145 /* 109 - free */
146 #define	M_CREDENTIALS	110	/* ipsec(4) related credentials */
147 /* 111-122 - free */
148 
149 /* KAME IPv6 */
150 #define	M_IP6OPT	123	/* IPv6 options */
151 #define	M_IP6NDP	124	/* IPv6 Neighbor Discovery structures */
152 /* 125-126 - free */
153 #define	M_TEMP		127	/* miscellaneous temporary data buffers */
154 
155 #define	M_NTFSMNT	128	/* NTFS mount structures */
156 #define	M_NTFSNTNODE	129	/* NTFS ntnode information */
157 #define	M_NTFSFNODE	130	/* NTFS fnode information */
158 #define	M_NTFSDIR	131	/* NTFS directory buffers */
159 #define	M_NTFSNTHASH	132	/* NTFS ntnode hash tables */
160 #define	M_NTFSNTVATTR	133	/* NTFS file attribute information */
161 #define	M_NTFSRDATA	134	/* NTFS resident data */
162 #define	M_NTFSDECOMP	135	/* NTFS decompression temporary storage */
163 #define	M_NTFSRUN	136	/* NTFS vrun storage */
164 
165 #define	M_KEVENT	137	/* kqueue(2) data structures */
166 
167 	/*		138	   free */
168 #define	M_SYNCACHE	139	/* SYN cache hash array */
169 
170 #define	M_UDFMOUNT	140	/* UDF mount structures */
171 #define	M_UDFFENTRY	141	/* UDF file entries */
172 #define	M_UDFFID	142	/* UDF file IDs */
173 
174 	/*		143	   free */
175 
176 #define	M_AGP		144	/* AGP memory */
177 
178 #define	M_DRM		145	/* Direct Rendering Manager */
179 
180 #define	M_LAST		146	/* Must be last type + 1 */
181 
182 #define	INITKMEMNAMES { \
183 	"free",		/* 0 M_FREE */ \
184 	NULL, \
185 	"devbuf",	/* 2 M_DEVBUF */ \
186 	NULL, \
187 	"pcb",		/* 4 M_PCB */ \
188 	"rtable",	/* 5 M_RTABLE */ \
189 	"pf",		/* 6 M_PF */ \
190 	NULL, \
191 	NULL, \
192 	"ifaddr",	/* 9 M_IFADDR */ \
193 	"ifgroup",	/* 10 M_IFGROUP */ \
194 	"sysctl",	/* 11 M_SYSCTL */ \
195 	"counters",	/* 12 M_COUNTERS */ \
196 	NULL, \
197 	"ioctlops",	/* 14 M_IOCTLOPS */ \
198 	NULL, \
199 	NULL, \
200 	NULL, \
201 	NULL, \
202 	"iov",		/* 19 M_IOV */ \
203 	"mount",	/* 20 M_MOUNT */ \
204 	NULL, \
205 	"NFS req",	/* 22 M_NFSREQ */ \
206 	"NFS mount",	/* 23 M_NFSMNT */ \
207 	"log",		/* 24 M_LOG */ \
208 	"vnodes",	/* 25 M_VNODE */ \
209 	NULL, \
210 	"UFS quota",	/* 27 M_DQUOT */ \
211 	"UFS mount",	/* 28 M_UFSMNT */ \
212 	"shm",		/* 29 M_SHM */ \
213 	"VM map",	/* 30 M_VMMAP */ \
214 	"sem",		/* 31 M_SEM */ \
215 	"dirhash",	/* 32 M_DIRHASH */ \
216 	"ACPI", 	/* 33 M_ACPI */ \
217 	"VM pmap",	/* 34 M_VMPMAP */ \
218 	NULL,	/* 35 */ \
219 	NULL,	/* 36 */ \
220 	NULL,	/* 37 */ \
221 	NULL, \
222 	"file desc",	/* 39 M_FILEDESC */ \
223 	"sigio",	/* 40 M_SIGIO */ \
224 	"proc",		/* 41 M_PROC */ \
225 	"subproc",	/* 42 M_SUBPROC */ \
226 	NULL, \
227 	NULL, \
228 	NULL, \
229 	"MFS node",	/* 46 M_MFSNODE */ \
230 	NULL, \
231 	NULL, \
232 	"Export Host",	/* 49 M_NETADDR */ \
233 	"NFS srvsock",	/* 50 M_NFSSVC */ \
234 	NULL, \
235 	"NFS daemon",	/* 52 M_NFSD */ \
236 	"ip_moptions",	/* 53 M_IPMOPTS */ \
237 	"in_multi",	/* 54 M_IPMADDR */ \
238 	"ether_multi",	/* 55 M_IFMADDR */ \
239 	"mrt",		/* 56 M_MRTABLE */ \
240 	"ISOFS mount",	/* 57 M_ISOFSMNT */ \
241 	"ISOFS node",	/* 58 M_ISOFSNODE */ \
242 	"MSDOSFS mount", /* 59 M_MSDOSFSMNT */ \
243 	"MSDOSFS fat",	/* 60 M_MSDOSFSFAT */ \
244 	"MSDOSFS node",	/* 61 M_MSDOSFSNODE */ \
245 	"ttys",		/* 62 M_TTYS */ \
246 	"exec",		/* 63 M_EXEC */ \
247 	"miscfs mount",	/* 64 M_MISCFSMNT */ \
248 	"fusefs mount", /* 65 M_FUSEFS */ \
249 	NULL, \
250 	NULL, \
251 	NULL, \
252 	NULL, \
253 	NULL, \
254 	NULL, \
255 	NULL, \
256 	NULL, \
257 	"pfkey data",	/* 74 M_PFKEY */ \
258 	"tdb",		/* 75 M_TDB */ \
259 	"xform_data",	/* 76 M_XDATA */ \
260 	NULL, \
261 	"pagedep",	/* 78 M_PAGEDEP */ \
262 	"inodedep",	/* 79 M_INODEDEP */ \
263 	"newblk",	/* 80 M_NEWBLK */ \
264 	NULL, \
265 	NULL, \
266 	"indirdep",	/* 83 M_INDIRDEP */ \
267 	NULL, NULL, NULL, NULL, \
268 	NULL, NULL, NULL, NULL, \
269 	"VM swap",	/* 92 M_VMSWAP */ \
270 	NULL, NULL, NULL, NULL, NULL, \
271 	"UVM amap",	/* 98 M_UVMAMAP */ \
272 	"UVM aobj",	/* 99 M_UVMAOBJ */ \
273 	"pinsyscall",	/* 100 M_PINSYSCALL */ \
274 	"USB",		/* 101 M_USB */ \
275 	"USB device",	/* 102 M_USBDEV */ \
276 	"USB HC",	/* 103 M_USBHC */ \
277 	"witness",	/* 104 M_WITNESS */ \
278 	"memdesc",	/* 105 M_MEMDESC */ \
279 	NULL,	/* 106 */ \
280 	NULL, \
281 	"crypto data",	/* 108 M_CRYPTO_DATA */ \
282 	NULL, \
283 	"IPsec creds",	/* 110 M_CREDENTIALS */ \
284 	NULL, NULL, NULL, NULL, \
285 	NULL, NULL, NULL, NULL, \
286 	NULL, NULL, NULL, NULL, \
287 	"ip6_options",	/* 123 M_IP6OPT */ \
288 	"NDP",		/* 124 M_IP6NDP */ \
289 	NULL, \
290 	NULL, \
291 	"temp",		/* 127 M_TEMP */ \
292 	"NTFS mount",	/* 128 M_NTFSMNT */ \
293 	"NTFS node",	/* 129 M_NTFSNTNODE */ \
294 	"NTFS fnode",	/* 130 M_NTFSFNODE */ \
295 	"NTFS dir",	/* 131 M_NTFSDIR */ \
296 	"NTFS hash",	/* 132 M_NTFSNTHASH */ \
297 	"NTFS attr",	/* 133 M_NTFSNTVATTR */ \
298 	"NTFS data",	/* 134 M_NTFSRDATA */ \
299 	"NTFS decomp",	/* 135 M_NTFSDECOMP */ \
300 	"NTFS vrun",	/* 136 M_NTFSRUN */ \
301 	"kqueue",	/* 137 M_KEVENT */ \
302 	NULL,	/* 138 free */ \
303 	"SYN cache",	/* 139 M_SYNCACHE */ \
304 	"UDF mount",	/* 140 M_UDFMOUNT */ \
305 	"UDF file entry",	/* 141 M_UDFFENTRY */ \
306 	"UDF file id",	/* 142 M_UDFFID */ \
307 	NULL,	/* 143 free */ \
308 	"AGP Memory",	/* 144 M_AGP */ \
309 	"DRM",	/* 145 M_DRM */ \
310 }
311 
312 struct kmemstats {
313 	long	ks_inuse;	/* # of packets of this type currently in use */
314 	long	ks_calls;	/* total packets of this type ever allocated */
315 	long 	ks_memuse;	/* total memory held in bytes */
316 	u_short	ks_limblocks;	/* number of times blocked for hitting limit */
317 	long	ks_maxused;	/* maximum number ever used */
318 	long	ks_limit;	/* most that are allowed to exist */
319 	long	ks_size;	/* sizes of this thing that are allocated */
320 	long	ks_spare;
321 };
322 
323 /*
324  * Array of descriptors that describe the contents of each page
325  */
326 struct kmemusage {
327 	short ku_indx;		/* bucket index */
328 	union {
329 		u_short freecnt;/* for small allocations, free pieces in page */
330 		u_short pagecnt;/* for large allocations, pages alloced */
331 	} ku_un;
332 };
333 #define	ku_freecnt ku_un.freecnt
334 #define	ku_pagecnt ku_un.pagecnt
335 
336 struct kmem_freelist;
337 
338 /*
339  * Set of buckets for each size of memory block that is retained
340  */
341 struct kmembuckets {
342 	XSIMPLEQ_HEAD(, kmem_freelist) kb_freelist; /* list of free blocks */
343 	u_int64_t kb_calls;	/* total calls to allocate this size */
344 	u_int64_t kb_total;	/* total number of blocks allocated */
345 	u_int64_t kb_totalfree;	/* # of free elements in this bucket */
346 	u_int64_t kb_elmpercl;	/* # of elements in this sized allocation */
347 	u_int64_t kb_highwat;	/* high water mark */
348 	u_int64_t kb_couldfree;	/* over high water mark and could free */
349 };
350 
351 /*
352  * Constants for setting the parameters of the kernel memory allocator.
353  *
354  * 2 ** MINBUCKET is the smallest unit of memory that will be
355  * allocated. It must be at least large enough to hold a pointer.
356  *
357  * Units of memory less or equal to MAXALLOCSAVE will permanently
358  * allocate physical memory; requests for these size pieces of
359  * memory are quite fast. Allocations greater than MAXALLOCSAVE must
360  * always allocate and free physical memory; requests for these
361  * size allocations should be done infrequently as they will be slow.
362  *
363  * Constraints: PAGE_SIZE <= MAXALLOCSAVE <= 2 ** (MINBUCKET + 14), and
364  * MAXALLOCSIZE must be a power of two.
365  */
366 #define MINBUCKET	4		/* 4 => min allocation of 16 bytes */
367 
368 #ifdef _KERNEL
369 
370 #define	MINALLOCSIZE	(1 << MINBUCKET)
371 #define	MAXALLOCSAVE	(2 * PAGE_SIZE)
372 
373 /*
374  * Turn virtual addresses into kmem map indices
375  */
376 #define	kmemxtob(alloc)	(kmembase + (alloc) * PAGE_SIZE)
377 #define	btokmemx(addr)	(((caddr_t)(addr) - kmembase) / PAGE_SIZE)
378 #define	btokup(addr)	(&kmemusage[((caddr_t)(addr) - kmembase) >> PAGE_SHIFT])
379 
380 extern struct kmemstats kmemstats[];
381 extern struct kmemusage *kmemusage;
382 extern char *kmembase;
383 extern struct kmembuckets bucket[];
384 
385 void	*malloc(size_t, int, int);
386 void	*mallocarray(size_t, size_t, int, int);
387 void	free(void *, int, size_t);
388 int	sysctl_malloc(int *, u_int, void *, size_t *, void *, size_t,
389 	    struct proc *);
390 
391 void	malloc_printit(int (*)(const char *, ...));
392 
393 void	poison_mem(void *, size_t);
394 int	poison_check(void *, size_t, size_t *, uint32_t *);
395 uint32_t poison_value(void *);
396 
397 #endif /* _KERNEL */
398 #endif /* !_SYS_MALLOC_H_ */
399