xref: /freebsd/sys/kern/kern_malloc.c (revision 5bb3134a8c21cb87b30e135ef168483f0333dabb)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1987, 1991, 1993
5  *	The Regents of the University of California.
6  * Copyright (c) 2005-2009 Robert N. M. Watson
7  * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> (mallocarray)
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
35  */
36 
37 /*
38  * Kernel malloc(9) implementation -- general purpose kernel memory allocator
39  * based on memory types.  Back end is implemented using the UMA(9) zone
40  * allocator.  A set of fixed-size buckets are used for smaller allocations,
41  * and a special UMA allocation interface is used for larger allocations.
42  * Callers declare memory types, and statistics are maintained independently
43  * for each memory type.  Statistics are maintained per-CPU for performance
44  * reasons.  See malloc(9) and comments in malloc.h for a detailed
45  * description.
46  */
47 
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include "opt_ddb.h"
52 #include "opt_vm.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/asan.h>
57 #include <sys/kdb.h>
58 #include <sys/kernel.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/msan.h>
62 #include <sys/mutex.h>
63 #include <sys/vmmeter.h>
64 #include <sys/proc.h>
65 #include <sys/queue.h>
66 #include <sys/sbuf.h>
67 #include <sys/smp.h>
68 #include <sys/sysctl.h>
69 #include <sys/time.h>
70 #include <sys/vmem.h>
71 #ifdef EPOCH_TRACE
72 #include <sys/epoch.h>
73 #endif
74 
75 #include <vm/vm.h>
76 #include <vm/pmap.h>
77 #include <vm/vm_domainset.h>
78 #include <vm/vm_pageout.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_phys.h>
85 #include <vm/vm_pagequeue.h>
86 #include <vm/uma.h>
87 #include <vm/uma_int.h>
88 #include <vm/uma_dbg.h>
89 
90 #ifdef DEBUG_MEMGUARD
91 #include <vm/memguard.h>
92 #endif
93 #ifdef DEBUG_REDZONE
94 #include <vm/redzone.h>
95 #endif
96 
97 #if defined(INVARIANTS) && defined(__i386__)
98 #include <machine/cpu.h>
99 #endif
100 
101 #include <ddb/ddb.h>
102 
103 #ifdef KDTRACE_HOOKS
104 #include <sys/dtrace_bsd.h>
105 
106 bool	__read_frequently			dtrace_malloc_enabled;
107 dtrace_malloc_probe_func_t __read_mostly	dtrace_malloc_probe;
108 #endif
109 
110 #if defined(INVARIANTS) || defined(MALLOC_MAKE_FAILURES) ||		\
111     defined(DEBUG_MEMGUARD) || defined(DEBUG_REDZONE)
112 #define	MALLOC_DEBUG	1
113 #endif
114 
115 #if defined(KASAN) || defined(DEBUG_REDZONE)
116 #define	DEBUG_REDZONE_ARG_DEF	, unsigned long osize
117 #define	DEBUG_REDZONE_ARG	, osize
118 #else
119 #define	DEBUG_REDZONE_ARG_DEF
120 #define	DEBUG_REDZONE_ARG
121 #endif
122 
123 /*
124  * When realloc() is called, if the new size is sufficiently smaller than
125  * the old size, realloc() will allocate a new, smaller block to avoid
126  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
127  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
128  */
129 #ifndef REALLOC_FRACTION
130 #define	REALLOC_FRACTION	1	/* new block if <= half the size */
131 #endif
132 
133 /*
134  * Centrally define some common malloc types.
135  */
136 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
137 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
138 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
139 
140 static struct malloc_type *kmemstatistics;
141 static int kmemcount;
142 
143 #define KMEM_ZSHIFT	4
144 #define KMEM_ZBASE	16
145 #define KMEM_ZMASK	(KMEM_ZBASE - 1)
146 
147 #define KMEM_ZMAX	65536
148 #define KMEM_ZSIZE	(KMEM_ZMAX >> KMEM_ZSHIFT)
149 static uint8_t kmemsize[KMEM_ZSIZE + 1];
150 
151 #ifndef MALLOC_DEBUG_MAXZONES
152 #define	MALLOC_DEBUG_MAXZONES	1
153 #endif
154 static int numzones = MALLOC_DEBUG_MAXZONES;
155 
156 /*
157  * Small malloc(9) memory allocations are allocated from a set of UMA buckets
158  * of various sizes.
159  *
160  * Warning: the layout of the struct is duplicated in libmemstat for KVM support.
161  *
162  * XXX: The comment here used to read "These won't be powers of two for
163  * long."  It's possible that a significant amount of wasted memory could be
164  * recovered by tuning the sizes of these buckets.
165  */
166 struct {
167 	int kz_size;
168 	const char *kz_name;
169 	uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES];
170 } kmemzones[] = {
171 	{16, "malloc-16", },
172 	{32, "malloc-32", },
173 	{64, "malloc-64", },
174 	{128, "malloc-128", },
175 	{256, "malloc-256", },
176 	{384, "malloc-384", },
177 	{512, "malloc-512", },
178 	{1024, "malloc-1024", },
179 	{2048, "malloc-2048", },
180 	{4096, "malloc-4096", },
181 	{8192, "malloc-8192", },
182 	{16384, "malloc-16384", },
183 	{32768, "malloc-32768", },
184 	{65536, "malloc-65536", },
185 	{0, NULL},
186 };
187 
188 u_long vm_kmem_size;
189 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0,
190     "Size of kernel memory");
191 
192 static u_long kmem_zmax = KMEM_ZMAX;
193 SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0,
194     "Maximum allocation size that malloc(9) would use UMA as backend");
195 
196 static u_long vm_kmem_size_min;
197 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0,
198     "Minimum size of kernel memory");
199 
200 static u_long vm_kmem_size_max;
201 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0,
202     "Maximum size of kernel memory");
203 
204 static u_int vm_kmem_size_scale;
205 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0,
206     "Scale factor for kernel memory size");
207 
208 static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS);
209 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size,
210     CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
211     sysctl_kmem_map_size, "LU", "Current kmem allocation size");
212 
213 static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS);
214 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free,
215     CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
216     sysctl_kmem_map_free, "LU", "Free space in kmem");
217 
218 static SYSCTL_NODE(_vm, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
219     "Malloc information");
220 
221 static u_int vm_malloc_zone_count = nitems(kmemzones);
222 SYSCTL_UINT(_vm_malloc, OID_AUTO, zone_count,
223     CTLFLAG_RD, &vm_malloc_zone_count, 0,
224     "Number of malloc zones");
225 
226 static int sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS);
227 SYSCTL_PROC(_vm_malloc, OID_AUTO, zone_sizes,
228     CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE, NULL, 0,
229     sysctl_vm_malloc_zone_sizes, "S", "Zone sizes used by malloc");
230 
231 /*
232  * The malloc_mtx protects the kmemstatistics linked list.
233  */
234 struct mtx malloc_mtx;
235 
236 static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
237 
238 #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1)
239 static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
240     "Kernel malloc debugging options");
241 #endif
242 
243 /*
244  * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
245  * the caller specifies M_NOWAIT.  If set to 0, no failures are caused.
246  */
247 #ifdef MALLOC_MAKE_FAILURES
248 static int malloc_failure_rate;
249 static int malloc_nowait_count;
250 static int malloc_failure_count;
251 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN,
252     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
253 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
254     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
255 #endif
256 
257 static int
258 sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS)
259 {
260 	u_long size;
261 
262 	size = uma_size();
263 	return (sysctl_handle_long(oidp, &size, 0, req));
264 }
265 
266 static int
267 sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS)
268 {
269 	u_long size, limit;
270 
271 	/* The sysctl is unsigned, implement as a saturation value. */
272 	size = uma_size();
273 	limit = uma_limit();
274 	if (size > limit)
275 		size = 0;
276 	else
277 		size = limit - size;
278 	return (sysctl_handle_long(oidp, &size, 0, req));
279 }
280 
281 static int
282 sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS)
283 {
284 	int sizes[nitems(kmemzones)];
285 	int i;
286 
287 	for (i = 0; i < nitems(kmemzones); i++) {
288 		sizes[i] = kmemzones[i].kz_size;
289 	}
290 
291 	return (SYSCTL_OUT(req, &sizes, sizeof(sizes)));
292 }
293 
294 /*
295  * malloc(9) uma zone separation -- sub-page buffer overruns in one
296  * malloc type will affect only a subset of other malloc types.
297  */
298 #if MALLOC_DEBUG_MAXZONES > 1
299 static void
300 tunable_set_numzones(void)
301 {
302 
303 	TUNABLE_INT_FETCH("debug.malloc.numzones",
304 	    &numzones);
305 
306 	/* Sanity check the number of malloc uma zones. */
307 	if (numzones <= 0)
308 		numzones = 1;
309 	if (numzones > MALLOC_DEBUG_MAXZONES)
310 		numzones = MALLOC_DEBUG_MAXZONES;
311 }
312 SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL);
313 SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
314     &numzones, 0, "Number of malloc uma subzones");
315 
316 /*
317  * Any number that changes regularly is an okay choice for the
318  * offset.  Build numbers are pretty good of you have them.
319  */
320 static u_int zone_offset = __FreeBSD_version;
321 TUNABLE_INT("debug.malloc.zone_offset", &zone_offset);
322 SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN,
323     &zone_offset, 0, "Separate malloc types by examining the "
324     "Nth character in the malloc type short description.");
325 
326 static void
327 mtp_set_subzone(struct malloc_type *mtp)
328 {
329 	struct malloc_type_internal *mtip;
330 	const char *desc;
331 	size_t len;
332 	u_int val;
333 
334 	mtip = &mtp->ks_mti;
335 	desc = mtp->ks_shortdesc;
336 	if (desc == NULL || (len = strlen(desc)) == 0)
337 		val = 0;
338 	else
339 		val = desc[zone_offset % len];
340 	mtip->mti_zone = (val % numzones);
341 }
342 
343 static inline u_int
344 mtp_get_subzone(struct malloc_type *mtp)
345 {
346 	struct malloc_type_internal *mtip;
347 
348 	mtip = &mtp->ks_mti;
349 
350 	KASSERT(mtip->mti_zone < numzones,
351 	    ("mti_zone %u out of range %d",
352 	    mtip->mti_zone, numzones));
353 	return (mtip->mti_zone);
354 }
355 #elif MALLOC_DEBUG_MAXZONES == 0
356 #error "MALLOC_DEBUG_MAXZONES must be positive."
357 #else
358 static void
359 mtp_set_subzone(struct malloc_type *mtp)
360 {
361 	struct malloc_type_internal *mtip;
362 
363 	mtip = &mtp->ks_mti;
364 	mtip->mti_zone = 0;
365 }
366 
367 static inline u_int
368 mtp_get_subzone(struct malloc_type *mtp)
369 {
370 
371 	return (0);
372 }
373 #endif /* MALLOC_DEBUG_MAXZONES > 1 */
374 
375 /*
376  * An allocation has succeeded -- update malloc type statistics for the
377  * amount of bucket size.  Occurs within a critical section so that the
378  * thread isn't preempted and doesn't migrate while updating per-PCU
379  * statistics.
380  */
381 static void
382 malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
383     int zindx)
384 {
385 	struct malloc_type_internal *mtip;
386 	struct malloc_type_stats *mtsp;
387 
388 	critical_enter();
389 	mtip = &mtp->ks_mti;
390 	mtsp = zpcpu_get(mtip->mti_stats);
391 	if (size > 0) {
392 		mtsp->mts_memalloced += size;
393 		mtsp->mts_numallocs++;
394 	}
395 	if (zindx != -1)
396 		mtsp->mts_size |= 1 << zindx;
397 
398 #ifdef KDTRACE_HOOKS
399 	if (__predict_false(dtrace_malloc_enabled)) {
400 		uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC];
401 		if (probe_id != 0)
402 			(dtrace_malloc_probe)(probe_id,
403 			    (uintptr_t) mtp, (uintptr_t) mtip,
404 			    (uintptr_t) mtsp, size, zindx);
405 	}
406 #endif
407 
408 	critical_exit();
409 }
410 
411 void
412 malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
413 {
414 
415 	if (size > 0)
416 		malloc_type_zone_allocated(mtp, size, -1);
417 }
418 
419 /*
420  * A free operation has occurred -- update malloc type statistics for the
421  * amount of the bucket size.  Occurs within a critical section so that the
422  * thread isn't preempted and doesn't migrate while updating per-CPU
423  * statistics.
424  */
425 void
426 malloc_type_freed(struct malloc_type *mtp, unsigned long size)
427 {
428 	struct malloc_type_internal *mtip;
429 	struct malloc_type_stats *mtsp;
430 
431 	critical_enter();
432 	mtip = &mtp->ks_mti;
433 	mtsp = zpcpu_get(mtip->mti_stats);
434 	mtsp->mts_memfreed += size;
435 	mtsp->mts_numfrees++;
436 
437 #ifdef KDTRACE_HOOKS
438 	if (__predict_false(dtrace_malloc_enabled)) {
439 		uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE];
440 		if (probe_id != 0)
441 			(dtrace_malloc_probe)(probe_id,
442 			    (uintptr_t) mtp, (uintptr_t) mtip,
443 			    (uintptr_t) mtsp, size, 0);
444 	}
445 #endif
446 
447 	critical_exit();
448 }
449 
450 /*
451  *	contigmalloc:
452  *
453  *	Allocate a block of physically contiguous memory.
454  *
455  *	If M_NOWAIT is set, this routine will not block and return NULL if
456  *	the allocation fails.
457  */
458 void *
459 contigmalloc(unsigned long size, struct malloc_type *type, int flags,
460     vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
461     vm_paddr_t boundary)
462 {
463 	void *ret;
464 
465 	ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment,
466 	    boundary, VM_MEMATTR_DEFAULT);
467 	if (ret != NULL)
468 		malloc_type_allocated(type, round_page(size));
469 	return (ret);
470 }
471 
472 void *
473 contigmalloc_domainset(unsigned long size, struct malloc_type *type,
474     struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high,
475     unsigned long alignment, vm_paddr_t boundary)
476 {
477 	void *ret;
478 
479 	ret = (void *)kmem_alloc_contig_domainset(ds, size, flags, low, high,
480 	    alignment, boundary, VM_MEMATTR_DEFAULT);
481 	if (ret != NULL)
482 		malloc_type_allocated(type, round_page(size));
483 	return (ret);
484 }
485 
486 /*
487  *	contigfree:
488  *
489  *	Free a block of memory allocated by contigmalloc.
490  *
491  *	This routine may not block.
492  */
493 void
494 contigfree(void *addr, unsigned long size, struct malloc_type *type)
495 {
496 
497 	kmem_free((vm_offset_t)addr, size);
498 	malloc_type_freed(type, round_page(size));
499 }
500 
501 #ifdef MALLOC_DEBUG
502 static int
503 malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp,
504     int flags)
505 {
506 #ifdef INVARIANTS
507 	int indx;
508 
509 	KASSERT(mtp->ks_version == M_VERSION, ("malloc: bad malloc type version"));
510 	/*
511 	 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
512 	 */
513 	indx = flags & (M_WAITOK | M_NOWAIT);
514 	if (indx != M_NOWAIT && indx != M_WAITOK) {
515 		static	struct timeval lasterr;
516 		static	int curerr, once;
517 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
518 			printf("Bad malloc flags: %x\n", indx);
519 			kdb_backtrace();
520 			flags |= M_WAITOK;
521 			once++;
522 		}
523 	}
524 #endif
525 #ifdef MALLOC_MAKE_FAILURES
526 	if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
527 		atomic_add_int(&malloc_nowait_count, 1);
528 		if ((malloc_nowait_count % malloc_failure_rate) == 0) {
529 			atomic_add_int(&malloc_failure_count, 1);
530 			*vap = NULL;
531 			return (EJUSTRETURN);
532 		}
533 	}
534 #endif
535 	if (flags & M_WAITOK) {
536 		KASSERT(curthread->td_intr_nesting_level == 0,
537 		   ("malloc(M_WAITOK) in interrupt context"));
538 		if (__predict_false(!THREAD_CAN_SLEEP())) {
539 #ifdef EPOCH_TRACE
540 			epoch_trace_list(curthread);
541 #endif
542 			KASSERT(0,
543 			    ("malloc(M_WAITOK) with sleeping prohibited"));
544 		}
545 	}
546 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
547 	    ("malloc: called with spinlock or critical section held"));
548 
549 #ifdef DEBUG_MEMGUARD
550 	if (memguard_cmp_mtp(mtp, *sizep)) {
551 		*vap = memguard_alloc(*sizep, flags);
552 		if (*vap != NULL)
553 			return (EJUSTRETURN);
554 		/* This is unfortunate but should not be fatal. */
555 	}
556 #endif
557 
558 #ifdef DEBUG_REDZONE
559 	*sizep = redzone_size_ntor(*sizep);
560 #endif
561 
562 	return (0);
563 }
564 #endif
565 
566 /*
567  * Handle large allocations and frees by using kmem_malloc directly.
568  */
569 static inline bool
570 malloc_large_slab(uma_slab_t slab)
571 {
572 	uintptr_t va;
573 
574 	va = (uintptr_t)slab;
575 	return ((va & 1) != 0);
576 }
577 
578 static inline size_t
579 malloc_large_size(uma_slab_t slab)
580 {
581 	uintptr_t va;
582 
583 	va = (uintptr_t)slab;
584 	return (va >> 1);
585 }
586 
587 static caddr_t __noinline
588 malloc_large(size_t size, struct malloc_type *mtp, struct domainset *policy,
589     int flags DEBUG_REDZONE_ARG_DEF)
590 {
591 	vm_offset_t kva;
592 	caddr_t va;
593 
594 	size = roundup(size, PAGE_SIZE);
595 	kva = kmem_malloc_domainset(policy, size, flags);
596 	if (kva != 0) {
597 		/* The low bit is unused for slab pointers. */
598 		vsetzoneslab(kva, NULL, (void *)((size << 1) | 1));
599 		uma_total_inc(size);
600 	}
601 	va = (caddr_t)kva;
602 	malloc_type_allocated(mtp, va == NULL ? 0 : size);
603 	if (__predict_false(va == NULL)) {
604 		KASSERT((flags & M_WAITOK) == 0,
605 		    ("malloc(M_WAITOK) returned NULL"));
606 	} else {
607 #ifdef DEBUG_REDZONE
608 		va = redzone_setup(va, osize);
609 #endif
610 		kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
611 	}
612 	return (va);
613 }
614 
615 static void
616 free_large(void *addr, size_t size)
617 {
618 
619 	kmem_free((vm_offset_t)addr, size);
620 	uma_total_dec(size);
621 }
622 
623 /*
624  *	malloc:
625  *
626  *	Allocate a block of memory.
627  *
628  *	If M_NOWAIT is set, this routine will not block and return NULL if
629  *	the allocation fails.
630  */
631 void *
632 (malloc)(size_t size, struct malloc_type *mtp, int flags)
633 {
634 	int indx;
635 	caddr_t va;
636 	uma_zone_t zone;
637 #if defined(DEBUG_REDZONE) || defined(KASAN)
638 	unsigned long osize = size;
639 #endif
640 
641 	MPASS((flags & M_EXEC) == 0);
642 
643 #ifdef MALLOC_DEBUG
644 	va = NULL;
645 	if (malloc_dbg(&va, &size, mtp, flags) != 0)
646 		return (va);
647 #endif
648 
649 	if (__predict_false(size > kmem_zmax))
650 		return (malloc_large(size, mtp, DOMAINSET_RR(), flags
651 		    DEBUG_REDZONE_ARG));
652 
653 	if (size & KMEM_ZMASK)
654 		size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
655 	indx = kmemsize[size >> KMEM_ZSHIFT];
656 	zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)];
657 	va = uma_zalloc(zone, flags);
658 	if (va != NULL) {
659 		size = zone->uz_size;
660 		if ((flags & M_ZERO) == 0) {
661 			kmsan_mark(va, size, KMSAN_STATE_UNINIT);
662 			kmsan_orig(va, size, KMSAN_TYPE_MALLOC, KMSAN_RET_ADDR);
663 		}
664 	}
665 	malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
666 	if (__predict_false(va == NULL)) {
667 		KASSERT((flags & M_WAITOK) == 0,
668 		    ("malloc(M_WAITOK) returned NULL"));
669 	}
670 #ifdef DEBUG_REDZONE
671 	if (va != NULL)
672 		va = redzone_setup(va, osize);
673 #endif
674 #ifdef KASAN
675 	if (va != NULL)
676 		kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
677 #endif
678 	return ((void *) va);
679 }
680 
681 static void *
682 malloc_domain(size_t *sizep, int *indxp, struct malloc_type *mtp, int domain,
683     int flags)
684 {
685 	uma_zone_t zone;
686 	caddr_t va;
687 	size_t size;
688 	int indx;
689 
690 	size = *sizep;
691 	KASSERT(size <= kmem_zmax && (flags & M_EXEC) == 0,
692 	    ("malloc_domain: Called with bad flag / size combination."));
693 	if (size & KMEM_ZMASK)
694 		size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
695 	indx = kmemsize[size >> KMEM_ZSHIFT];
696 	zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)];
697 	va = uma_zalloc_domain(zone, NULL, domain, flags);
698 	if (va != NULL)
699 		*sizep = zone->uz_size;
700 	*indxp = indx;
701 	return ((void *)va);
702 }
703 
704 void *
705 malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds,
706     int flags)
707 {
708 	struct vm_domainset_iter di;
709 	caddr_t va;
710 	int domain;
711 	int indx;
712 #if defined(KASAN) || defined(DEBUG_REDZONE)
713 	unsigned long osize = size;
714 #endif
715 
716 	MPASS((flags & M_EXEC) == 0);
717 
718 #ifdef MALLOC_DEBUG
719 	va = NULL;
720 	if (malloc_dbg(&va, &size, mtp, flags) != 0)
721 		return (va);
722 #endif
723 
724 	if (__predict_false(size > kmem_zmax))
725 		return (malloc_large(size, mtp, DOMAINSET_RR(), flags
726 		    DEBUG_REDZONE_ARG));
727 
728 	vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
729 	do {
730 		va = malloc_domain(&size, &indx, mtp, domain, flags);
731 	} while (va == NULL && vm_domainset_iter_policy(&di, &domain) == 0);
732 	malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
733 	if (__predict_false(va == NULL)) {
734 		KASSERT((flags & M_WAITOK) == 0,
735 		    ("malloc(M_WAITOK) returned NULL"));
736 	}
737 #ifdef DEBUG_REDZONE
738 	if (va != NULL)
739 		va = redzone_setup(va, osize);
740 #endif
741 #ifdef KASAN
742 	if (va != NULL)
743 		kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
744 #endif
745 #ifdef KMSAN
746 	if ((flags & M_ZERO) == 0) {
747 		kmsan_mark(va, size, KMSAN_STATE_UNINIT);
748 		kmsan_orig(va, size, KMSAN_TYPE_MALLOC, KMSAN_RET_ADDR);
749 	}
750 #endif
751 	return (va);
752 }
753 
754 /*
755  * Allocate an executable area.
756  */
757 void *
758 malloc_exec(size_t size, struct malloc_type *mtp, int flags)
759 {
760 
761 	return (malloc_domainset_exec(size, mtp, DOMAINSET_RR(), flags));
762 }
763 
764 void *
765 malloc_domainset_exec(size_t size, struct malloc_type *mtp, struct domainset *ds,
766     int flags)
767 {
768 #if defined(DEBUG_REDZONE) || defined(KASAN)
769 	unsigned long osize = size;
770 #endif
771 #ifdef MALLOC_DEBUG
772 	caddr_t va;
773 #endif
774 
775 	flags |= M_EXEC;
776 
777 #ifdef MALLOC_DEBUG
778 	va = NULL;
779 	if (malloc_dbg(&va, &size, mtp, flags) != 0)
780 		return (va);
781 #endif
782 
783 	return (malloc_large(size, mtp, ds, flags DEBUG_REDZONE_ARG));
784 }
785 
786 void *
787 malloc_aligned(size_t size, size_t align, struct malloc_type *type, int flags)
788 {
789 	return (malloc_domainset_aligned(size, align, type, DOMAINSET_RR(),
790 	    flags));
791 }
792 
793 void *
794 malloc_domainset_aligned(size_t size, size_t align,
795     struct malloc_type *mtp, struct domainset *ds, int flags)
796 {
797 	void *res;
798 	size_t asize;
799 
800 	KASSERT(powerof2(align),
801 	    ("malloc_domainset_aligned: wrong align %#zx size %#zx",
802 	    align, size));
803 	KASSERT(align <= PAGE_SIZE,
804 	    ("malloc_domainset_aligned: align %#zx (size %#zx) too large",
805 	    align, size));
806 
807 	/*
808 	 * Round the allocation size up to the next power of 2,
809 	 * because we can only guarantee alignment for
810 	 * power-of-2-sized allocations.  Further increase the
811 	 * allocation size to align if the rounded size is less than
812 	 * align, since malloc zones provide alignment equal to their
813 	 * size.
814 	 */
815 	if (size == 0)
816 		size = 1;
817 	asize = size <= align ? align : 1UL << flsl(size - 1);
818 
819 	res = malloc_domainset(asize, mtp, ds, flags);
820 	KASSERT(res == NULL || ((uintptr_t)res & (align - 1)) == 0,
821 	    ("malloc_domainset_aligned: result not aligned %p size %#zx "
822 	    "allocsize %#zx align %#zx", res, size, asize, align));
823 	return (res);
824 }
825 
826 void *
827 mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags)
828 {
829 
830 	if (WOULD_OVERFLOW(nmemb, size))
831 		panic("mallocarray: %zu * %zu overflowed", nmemb, size);
832 
833 	return (malloc(size * nmemb, type, flags));
834 }
835 
836 void *
837 mallocarray_domainset(size_t nmemb, size_t size, struct malloc_type *type,
838     struct domainset *ds, int flags)
839 {
840 
841 	if (WOULD_OVERFLOW(nmemb, size))
842 		panic("mallocarray_domainset: %zu * %zu overflowed", nmemb, size);
843 
844 	return (malloc_domainset(size * nmemb, type, ds, flags));
845 }
846 
847 #if defined(INVARIANTS) && !defined(KASAN)
848 static void
849 free_save_type(void *addr, struct malloc_type *mtp, u_long size)
850 {
851 	struct malloc_type **mtpp = addr;
852 
853 	/*
854 	 * Cache a pointer to the malloc_type that most recently freed
855 	 * this memory here.  This way we know who is most likely to
856 	 * have stepped on it later.
857 	 *
858 	 * This code assumes that size is a multiple of 8 bytes for
859 	 * 64 bit machines
860 	 */
861 	mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
862 	mtpp += (size - sizeof(struct malloc_type *)) /
863 	    sizeof(struct malloc_type *);
864 	*mtpp = mtp;
865 }
866 #endif
867 
868 #ifdef MALLOC_DEBUG
869 static int
870 free_dbg(void **addrp, struct malloc_type *mtp)
871 {
872 	void *addr;
873 
874 	addr = *addrp;
875 	KASSERT(mtp->ks_version == M_VERSION, ("free: bad malloc type version"));
876 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
877 	    ("free: called with spinlock or critical section held"));
878 
879 	/* free(NULL, ...) does nothing */
880 	if (addr == NULL)
881 		return (EJUSTRETURN);
882 
883 #ifdef DEBUG_MEMGUARD
884 	if (is_memguard_addr(addr)) {
885 		memguard_free(addr);
886 		return (EJUSTRETURN);
887 	}
888 #endif
889 
890 #ifdef DEBUG_REDZONE
891 	redzone_check(addr);
892 	*addrp = redzone_addr_ntor(addr);
893 #endif
894 
895 	return (0);
896 }
897 #endif
898 
899 /*
900  *	free:
901  *
902  *	Free a block of memory allocated by malloc.
903  *
904  *	This routine may not block.
905  */
906 void
907 free(void *addr, struct malloc_type *mtp)
908 {
909 	uma_zone_t zone;
910 	uma_slab_t slab;
911 	u_long size;
912 
913 #ifdef MALLOC_DEBUG
914 	if (free_dbg(&addr, mtp) != 0)
915 		return;
916 #endif
917 	/* free(NULL, ...) does nothing */
918 	if (addr == NULL)
919 		return;
920 
921 	vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
922 	if (slab == NULL)
923 		panic("free: address %p(%p) has not been allocated.\n",
924 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
925 
926 	if (__predict_true(!malloc_large_slab(slab))) {
927 		size = zone->uz_size;
928 #if defined(INVARIANTS) && !defined(KASAN)
929 		free_save_type(addr, mtp, size);
930 #endif
931 		uma_zfree_arg(zone, addr, slab);
932 	} else {
933 		size = malloc_large_size(slab);
934 		free_large(addr, size);
935 	}
936 	malloc_type_freed(mtp, size);
937 }
938 
939 /*
940  *	zfree:
941  *
942  *	Zero then free a block of memory allocated by malloc.
943  *
944  *	This routine may not block.
945  */
946 void
947 zfree(void *addr, struct malloc_type *mtp)
948 {
949 	uma_zone_t zone;
950 	uma_slab_t slab;
951 	u_long size;
952 
953 #ifdef MALLOC_DEBUG
954 	if (free_dbg(&addr, mtp) != 0)
955 		return;
956 #endif
957 	/* free(NULL, ...) does nothing */
958 	if (addr == NULL)
959 		return;
960 
961 	vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
962 	if (slab == NULL)
963 		panic("free: address %p(%p) has not been allocated.\n",
964 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
965 
966 	if (__predict_true(!malloc_large_slab(slab))) {
967 		size = zone->uz_size;
968 #if defined(INVARIANTS) && !defined(KASAN)
969 		free_save_type(addr, mtp, size);
970 #endif
971 		kasan_mark(addr, size, size, 0);
972 		explicit_bzero(addr, size);
973 		uma_zfree_arg(zone, addr, slab);
974 	} else {
975 		size = malloc_large_size(slab);
976 		kasan_mark(addr, size, size, 0);
977 		explicit_bzero(addr, size);
978 		free_large(addr, size);
979 	}
980 	malloc_type_freed(mtp, size);
981 }
982 
983 /*
984  *	realloc: change the size of a memory block
985  */
986 void *
987 realloc(void *addr, size_t size, struct malloc_type *mtp, int flags)
988 {
989 	uma_zone_t zone;
990 	uma_slab_t slab;
991 	unsigned long alloc;
992 	void *newaddr;
993 
994 	KASSERT(mtp->ks_version == M_VERSION,
995 	    ("realloc: bad malloc type version"));
996 	KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
997 	    ("realloc: called with spinlock or critical section held"));
998 
999 	/* realloc(NULL, ...) is equivalent to malloc(...) */
1000 	if (addr == NULL)
1001 		return (malloc(size, mtp, flags));
1002 
1003 	/*
1004 	 * XXX: Should report free of old memory and alloc of new memory to
1005 	 * per-CPU stats.
1006 	 */
1007 
1008 #ifdef DEBUG_MEMGUARD
1009 	if (is_memguard_addr(addr))
1010 		return (memguard_realloc(addr, size, mtp, flags));
1011 #endif
1012 
1013 #ifdef DEBUG_REDZONE
1014 	slab = NULL;
1015 	zone = NULL;
1016 	alloc = redzone_get_size(addr);
1017 #else
1018 	vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
1019 
1020 	/* Sanity check */
1021 	KASSERT(slab != NULL,
1022 	    ("realloc: address %p out of range", (void *)addr));
1023 
1024 	/* Get the size of the original block */
1025 	if (!malloc_large_slab(slab))
1026 		alloc = zone->uz_size;
1027 	else
1028 		alloc = malloc_large_size(slab);
1029 
1030 	/* Reuse the original block if appropriate */
1031 	if (size <= alloc &&
1032 	    (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) {
1033 		kasan_mark((void *)addr, size, alloc, KASAN_MALLOC_REDZONE);
1034 		return (addr);
1035 	}
1036 #endif /* !DEBUG_REDZONE */
1037 
1038 	/* Allocate a new, bigger (or smaller) block */
1039 	if ((newaddr = malloc(size, mtp, flags)) == NULL)
1040 		return (NULL);
1041 
1042 	/*
1043 	 * Copy over original contents.  For KASAN, the redzone must be marked
1044 	 * valid before performing the copy.
1045 	 */
1046 	kasan_mark(addr, alloc, alloc, 0);
1047 	bcopy(addr, newaddr, min(size, alloc));
1048 	free(addr, mtp);
1049 	return (newaddr);
1050 }
1051 
1052 /*
1053  *	reallocf: same as realloc() but free memory on failure.
1054  */
1055 void *
1056 reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags)
1057 {
1058 	void *mem;
1059 
1060 	if ((mem = realloc(addr, size, mtp, flags)) == NULL)
1061 		free(addr, mtp);
1062 	return (mem);
1063 }
1064 
1065 /*
1066  * 	malloc_size: returns the number of bytes allocated for a request of the
1067  * 		     specified size
1068  */
1069 size_t
1070 malloc_size(size_t size)
1071 {
1072 	int indx;
1073 
1074 	if (size > kmem_zmax)
1075 		return (0);
1076 	if (size & KMEM_ZMASK)
1077 		size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
1078 	indx = kmemsize[size >> KMEM_ZSHIFT];
1079 	return (kmemzones[indx].kz_size);
1080 }
1081 
1082 /*
1083  *	malloc_usable_size: returns the usable size of the allocation.
1084  */
1085 size_t
1086 malloc_usable_size(const void *addr)
1087 {
1088 #ifndef DEBUG_REDZONE
1089 	uma_zone_t zone;
1090 	uma_slab_t slab;
1091 #endif
1092 	u_long size;
1093 
1094 	if (addr == NULL)
1095 		return (0);
1096 
1097 #ifdef DEBUG_MEMGUARD
1098 	if (is_memguard_addr(__DECONST(void *, addr)))
1099 		return (memguard_get_req_size(addr));
1100 #endif
1101 
1102 #ifdef DEBUG_REDZONE
1103 	size = redzone_get_size(__DECONST(void *, addr));
1104 #else
1105 	vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
1106 	if (slab == NULL)
1107 		panic("malloc_usable_size: address %p(%p) is not allocated.\n",
1108 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
1109 
1110 	if (!malloc_large_slab(slab))
1111 		size = zone->uz_size;
1112 	else
1113 		size = malloc_large_size(slab);
1114 #endif
1115 
1116 	/*
1117 	 * Unmark the redzone to avoid reports from consumers who are
1118 	 * (presumably) about to use the full allocation size.
1119 	 */
1120 	kasan_mark(addr, size, size, 0);
1121 
1122 	return (size);
1123 }
1124 
1125 CTASSERT(VM_KMEM_SIZE_SCALE >= 1);
1126 
1127 /*
1128  * Initialize the kernel memory (kmem) arena.
1129  */
1130 void
1131 kmeminit(void)
1132 {
1133 	u_long mem_size;
1134 	u_long tmp;
1135 
1136 #ifdef VM_KMEM_SIZE
1137 	if (vm_kmem_size == 0)
1138 		vm_kmem_size = VM_KMEM_SIZE;
1139 #endif
1140 #ifdef VM_KMEM_SIZE_MIN
1141 	if (vm_kmem_size_min == 0)
1142 		vm_kmem_size_min = VM_KMEM_SIZE_MIN;
1143 #endif
1144 #ifdef VM_KMEM_SIZE_MAX
1145 	if (vm_kmem_size_max == 0)
1146 		vm_kmem_size_max = VM_KMEM_SIZE_MAX;
1147 #endif
1148 	/*
1149 	 * Calculate the amount of kernel virtual address (KVA) space that is
1150 	 * preallocated to the kmem arena.  In order to support a wide range
1151 	 * of machines, it is a function of the physical memory size,
1152 	 * specifically,
1153 	 *
1154 	 *	min(max(physical memory size / VM_KMEM_SIZE_SCALE,
1155 	 *	    VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX)
1156 	 *
1157 	 * Every architecture must define an integral value for
1158 	 * VM_KMEM_SIZE_SCALE.  However, the definitions of VM_KMEM_SIZE_MIN
1159 	 * and VM_KMEM_SIZE_MAX, which represent respectively the floor and
1160 	 * ceiling on this preallocation, are optional.  Typically,
1161 	 * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on
1162 	 * a given architecture.
1163 	 */
1164 	mem_size = vm_cnt.v_page_count;
1165 	if (mem_size <= 32768) /* delphij XXX 128MB */
1166 		kmem_zmax = PAGE_SIZE;
1167 
1168 	if (vm_kmem_size_scale < 1)
1169 		vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
1170 
1171 	/*
1172 	 * Check if we should use defaults for the "vm_kmem_size"
1173 	 * variable:
1174 	 */
1175 	if (vm_kmem_size == 0) {
1176 		vm_kmem_size = mem_size / vm_kmem_size_scale;
1177 		vm_kmem_size = vm_kmem_size * PAGE_SIZE < vm_kmem_size ?
1178 		    vm_kmem_size_max : vm_kmem_size * PAGE_SIZE;
1179 		if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min)
1180 			vm_kmem_size = vm_kmem_size_min;
1181 		if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
1182 			vm_kmem_size = vm_kmem_size_max;
1183 	}
1184 	if (vm_kmem_size == 0)
1185 		panic("Tune VM_KMEM_SIZE_* for the platform");
1186 
1187 	/*
1188 	 * The amount of KVA space that is preallocated to the
1189 	 * kmem arena can be set statically at compile-time or manually
1190 	 * through the kernel environment.  However, it is still limited to
1191 	 * twice the physical memory size, which has been sufficient to handle
1192 	 * the most severe cases of external fragmentation in the kmem arena.
1193 	 */
1194 	if (vm_kmem_size / 2 / PAGE_SIZE > mem_size)
1195 		vm_kmem_size = 2 * mem_size * PAGE_SIZE;
1196 
1197 	vm_kmem_size = round_page(vm_kmem_size);
1198 
1199 	/*
1200 	 * With KASAN or KMSAN enabled, dynamically allocated kernel memory is
1201 	 * shadowed.  Account for this when setting the UMA limit.
1202 	 */
1203 #if defined(KASAN)
1204 	vm_kmem_size = (vm_kmem_size * KASAN_SHADOW_SCALE) /
1205 	    (KASAN_SHADOW_SCALE + 1);
1206 #elif defined(KMSAN)
1207 	vm_kmem_size /= 3;
1208 #endif
1209 
1210 #ifdef DEBUG_MEMGUARD
1211 	tmp = memguard_fudge(vm_kmem_size, kernel_map);
1212 #else
1213 	tmp = vm_kmem_size;
1214 #endif
1215 	uma_set_limit(tmp);
1216 
1217 #ifdef DEBUG_MEMGUARD
1218 	/*
1219 	 * Initialize MemGuard if support compiled in.  MemGuard is a
1220 	 * replacement allocator used for detecting tamper-after-free
1221 	 * scenarios as they occur.  It is only used for debugging.
1222 	 */
1223 	memguard_init(kernel_arena);
1224 #endif
1225 }
1226 
1227 /*
1228  * Initialize the kernel memory allocator
1229  */
1230 /* ARGSUSED*/
1231 static void
1232 mallocinit(void *dummy)
1233 {
1234 	int i;
1235 	uint8_t indx;
1236 
1237 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
1238 
1239 	kmeminit();
1240 
1241 	if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX)
1242 		kmem_zmax = KMEM_ZMAX;
1243 
1244 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
1245 		int size = kmemzones[indx].kz_size;
1246 		const char *name = kmemzones[indx].kz_name;
1247 		size_t align;
1248 		int subzone;
1249 
1250 		align = UMA_ALIGN_PTR;
1251 		if (powerof2(size) && size > sizeof(void *))
1252 			align = MIN(size, PAGE_SIZE) - 1;
1253 		for (subzone = 0; subzone < numzones; subzone++) {
1254 			kmemzones[indx].kz_zone[subzone] =
1255 			    uma_zcreate(name, size,
1256 #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
1257 			    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
1258 #else
1259 			    NULL, NULL, NULL, NULL,
1260 #endif
1261 			    align, UMA_ZONE_MALLOC);
1262 		}
1263 		for (;i <= size; i+= KMEM_ZBASE)
1264 			kmemsize[i >> KMEM_ZSHIFT] = indx;
1265 	}
1266 }
1267 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL);
1268 
1269 void
1270 malloc_init(void *data)
1271 {
1272 	struct malloc_type_internal *mtip;
1273 	struct malloc_type *mtp;
1274 
1275 	KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init"));
1276 
1277 	mtp = data;
1278 	if (mtp->ks_version != M_VERSION)
1279 		panic("malloc_init: type %s with unsupported version %lu",
1280 		    mtp->ks_shortdesc, mtp->ks_version);
1281 
1282 	mtip = &mtp->ks_mti;
1283 	mtip->mti_stats = uma_zalloc_pcpu(pcpu_zone_64, M_WAITOK | M_ZERO);
1284 	mtp_set_subzone(mtp);
1285 
1286 	mtx_lock(&malloc_mtx);
1287 	mtp->ks_next = kmemstatistics;
1288 	kmemstatistics = mtp;
1289 	kmemcount++;
1290 	mtx_unlock(&malloc_mtx);
1291 }
1292 
1293 void
1294 malloc_uninit(void *data)
1295 {
1296 	struct malloc_type_internal *mtip;
1297 	struct malloc_type_stats *mtsp;
1298 	struct malloc_type *mtp, *temp;
1299 	long temp_allocs, temp_bytes;
1300 	int i;
1301 
1302 	mtp = data;
1303 	KASSERT(mtp->ks_version == M_VERSION,
1304 	    ("malloc_uninit: bad malloc type version"));
1305 
1306 	mtx_lock(&malloc_mtx);
1307 	mtip = &mtp->ks_mti;
1308 	if (mtp != kmemstatistics) {
1309 		for (temp = kmemstatistics; temp != NULL;
1310 		    temp = temp->ks_next) {
1311 			if (temp->ks_next == mtp) {
1312 				temp->ks_next = mtp->ks_next;
1313 				break;
1314 			}
1315 		}
1316 		KASSERT(temp,
1317 		    ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc));
1318 	} else
1319 		kmemstatistics = mtp->ks_next;
1320 	kmemcount--;
1321 	mtx_unlock(&malloc_mtx);
1322 
1323 	/*
1324 	 * Look for memory leaks.
1325 	 */
1326 	temp_allocs = temp_bytes = 0;
1327 	for (i = 0; i <= mp_maxid; i++) {
1328 		mtsp = zpcpu_get_cpu(mtip->mti_stats, i);
1329 		temp_allocs += mtsp->mts_numallocs;
1330 		temp_allocs -= mtsp->mts_numfrees;
1331 		temp_bytes += mtsp->mts_memalloced;
1332 		temp_bytes -= mtsp->mts_memfreed;
1333 	}
1334 	if (temp_allocs > 0 || temp_bytes > 0) {
1335 		printf("Warning: memory type %s leaked memory on destroy "
1336 		    "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
1337 		    temp_allocs, temp_bytes);
1338 	}
1339 
1340 	uma_zfree_pcpu(pcpu_zone_64, mtip->mti_stats);
1341 }
1342 
1343 struct malloc_type *
1344 malloc_desc2type(const char *desc)
1345 {
1346 	struct malloc_type *mtp;
1347 
1348 	mtx_assert(&malloc_mtx, MA_OWNED);
1349 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1350 		if (strcmp(mtp->ks_shortdesc, desc) == 0)
1351 			return (mtp);
1352 	}
1353 	return (NULL);
1354 }
1355 
1356 static int
1357 sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
1358 {
1359 	struct malloc_type_stream_header mtsh;
1360 	struct malloc_type_internal *mtip;
1361 	struct malloc_type_stats *mtsp, zeromts;
1362 	struct malloc_type_header mth;
1363 	struct malloc_type *mtp;
1364 	int error, i;
1365 	struct sbuf sbuf;
1366 
1367 	error = sysctl_wire_old_buffer(req, 0);
1368 	if (error != 0)
1369 		return (error);
1370 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
1371 	sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
1372 	mtx_lock(&malloc_mtx);
1373 
1374 	bzero(&zeromts, sizeof(zeromts));
1375 
1376 	/*
1377 	 * Insert stream header.
1378 	 */
1379 	bzero(&mtsh, sizeof(mtsh));
1380 	mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
1381 	mtsh.mtsh_maxcpus = MAXCPU;
1382 	mtsh.mtsh_count = kmemcount;
1383 	(void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh));
1384 
1385 	/*
1386 	 * Insert alternating sequence of type headers and type statistics.
1387 	 */
1388 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1389 		mtip = &mtp->ks_mti;
1390 
1391 		/*
1392 		 * Insert type header.
1393 		 */
1394 		bzero(&mth, sizeof(mth));
1395 		strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
1396 		(void)sbuf_bcat(&sbuf, &mth, sizeof(mth));
1397 
1398 		/*
1399 		 * Insert type statistics for each CPU.
1400 		 */
1401 		for (i = 0; i <= mp_maxid; i++) {
1402 			mtsp = zpcpu_get_cpu(mtip->mti_stats, i);
1403 			(void)sbuf_bcat(&sbuf, mtsp, sizeof(*mtsp));
1404 		}
1405 		/*
1406 		 * Fill in the missing CPUs.
1407 		 */
1408 		for (; i < MAXCPU; i++) {
1409 			(void)sbuf_bcat(&sbuf, &zeromts, sizeof(zeromts));
1410 		}
1411 	}
1412 	mtx_unlock(&malloc_mtx);
1413 	error = sbuf_finish(&sbuf);
1414 	sbuf_delete(&sbuf);
1415 	return (error);
1416 }
1417 
1418 SYSCTL_PROC(_kern, OID_AUTO, malloc_stats,
1419     CTLFLAG_RD | CTLTYPE_STRUCT | CTLFLAG_MPSAFE, 0, 0,
1420     sysctl_kern_malloc_stats, "s,malloc_type_ustats",
1421     "Return malloc types");
1422 
1423 SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
1424     "Count of kernel malloc types");
1425 
1426 void
1427 malloc_type_list(malloc_type_list_func_t *func, void *arg)
1428 {
1429 	struct malloc_type *mtp, **bufmtp;
1430 	int count, i;
1431 	size_t buflen;
1432 
1433 	mtx_lock(&malloc_mtx);
1434 restart:
1435 	mtx_assert(&malloc_mtx, MA_OWNED);
1436 	count = kmemcount;
1437 	mtx_unlock(&malloc_mtx);
1438 
1439 	buflen = sizeof(struct malloc_type *) * count;
1440 	bufmtp = malloc(buflen, M_TEMP, M_WAITOK);
1441 
1442 	mtx_lock(&malloc_mtx);
1443 
1444 	if (count < kmemcount) {
1445 		free(bufmtp, M_TEMP);
1446 		goto restart;
1447 	}
1448 
1449 	for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++)
1450 		bufmtp[i] = mtp;
1451 
1452 	mtx_unlock(&malloc_mtx);
1453 
1454 	for (i = 0; i < count; i++)
1455 		(func)(bufmtp[i], arg);
1456 
1457 	free(bufmtp, M_TEMP);
1458 }
1459 
1460 #ifdef DDB
1461 static int64_t
1462 get_malloc_stats(const struct malloc_type_internal *mtip, uint64_t *allocs,
1463     uint64_t *inuse)
1464 {
1465 	const struct malloc_type_stats *mtsp;
1466 	uint64_t frees, alloced, freed;
1467 	int i;
1468 
1469 	*allocs = 0;
1470 	frees = 0;
1471 	alloced = 0;
1472 	freed = 0;
1473 	for (i = 0; i <= mp_maxid; i++) {
1474 		mtsp = zpcpu_get_cpu(mtip->mti_stats, i);
1475 
1476 		*allocs += mtsp->mts_numallocs;
1477 		frees += mtsp->mts_numfrees;
1478 		alloced += mtsp->mts_memalloced;
1479 		freed += mtsp->mts_memfreed;
1480 	}
1481 	*inuse = *allocs - frees;
1482 	return (alloced - freed);
1483 }
1484 
1485 DB_SHOW_COMMAND(malloc, db_show_malloc)
1486 {
1487 	const char *fmt_hdr, *fmt_entry;
1488 	struct malloc_type *mtp;
1489 	uint64_t allocs, inuse;
1490 	int64_t size;
1491 	/* variables for sorting */
1492 	struct malloc_type *last_mtype, *cur_mtype;
1493 	int64_t cur_size, last_size;
1494 	int ties;
1495 
1496 	if (modif[0] == 'i') {
1497 		fmt_hdr = "%s,%s,%s,%s\n";
1498 		fmt_entry = "\"%s\",%ju,%jdK,%ju\n";
1499 	} else {
1500 		fmt_hdr = "%18s %12s  %12s %12s\n";
1501 		fmt_entry = "%18s %12ju %12jdK %12ju\n";
1502 	}
1503 
1504 	db_printf(fmt_hdr, "Type", "InUse", "MemUse", "Requests");
1505 
1506 	/* Select sort, largest size first. */
1507 	last_mtype = NULL;
1508 	last_size = INT64_MAX;
1509 	for (;;) {
1510 		cur_mtype = NULL;
1511 		cur_size = -1;
1512 		ties = 0;
1513 
1514 		for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1515 			/*
1516 			 * In the case of size ties, print out mtypes
1517 			 * in the order they are encountered.  That is,
1518 			 * when we encounter the most recently output
1519 			 * mtype, we have already printed all preceding
1520 			 * ties, and we must print all following ties.
1521 			 */
1522 			if (mtp == last_mtype) {
1523 				ties = 1;
1524 				continue;
1525 			}
1526 			size = get_malloc_stats(&mtp->ks_mti, &allocs,
1527 			    &inuse);
1528 			if (size > cur_size && size < last_size + ties) {
1529 				cur_size = size;
1530 				cur_mtype = mtp;
1531 			}
1532 		}
1533 		if (cur_mtype == NULL)
1534 			break;
1535 
1536 		size = get_malloc_stats(&cur_mtype->ks_mti, &allocs, &inuse);
1537 		db_printf(fmt_entry, cur_mtype->ks_shortdesc, inuse,
1538 		    howmany(size, 1024), allocs);
1539 
1540 		if (db_pager_quit)
1541 			break;
1542 
1543 		last_mtype = cur_mtype;
1544 		last_size = cur_size;
1545 	}
1546 }
1547 
1548 #if MALLOC_DEBUG_MAXZONES > 1
1549 DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches)
1550 {
1551 	struct malloc_type_internal *mtip;
1552 	struct malloc_type *mtp;
1553 	u_int subzone;
1554 
1555 	if (!have_addr) {
1556 		db_printf("Usage: show multizone_matches <malloc type/addr>\n");
1557 		return;
1558 	}
1559 	mtp = (void *)addr;
1560 	if (mtp->ks_version != M_VERSION) {
1561 		db_printf("Version %lx does not match expected %x\n",
1562 		    mtp->ks_version, M_VERSION);
1563 		return;
1564 	}
1565 
1566 	mtip = &mtp->ks_mti;
1567 	subzone = mtip->mti_zone;
1568 
1569 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1570 		mtip = &mtp->ks_mti;
1571 		if (mtip->mti_zone != subzone)
1572 			continue;
1573 		db_printf("%s\n", mtp->ks_shortdesc);
1574 		if (db_pager_quit)
1575 			break;
1576 	}
1577 }
1578 #endif /* MALLOC_DEBUG_MAXZONES > 1 */
1579 #endif /* DDB */
1580