1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-3-Clause
3*22ce4affSfengbojiang *
4a9643ea8Slogwang * Copyright (c) 1987, 1993
5a9643ea8Slogwang * The Regents of the University of California.
6a9643ea8Slogwang * Copyright (c) 2005, 2009 Robert N. M. Watson
7a9643ea8Slogwang * All rights reserved.
8a9643ea8Slogwang *
9a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without
10a9643ea8Slogwang * modification, are permitted provided that the following conditions
11a9643ea8Slogwang * are met:
12a9643ea8Slogwang * 1. Redistributions of source code must retain the above copyright
13a9643ea8Slogwang * notice, this list of conditions and the following disclaimer.
14a9643ea8Slogwang * 2. Redistributions in binary form must reproduce the above copyright
15a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in the
16a9643ea8Slogwang * documentation and/or other materials provided with the distribution.
17*22ce4affSfengbojiang * 3. Neither the name of the University nor the names of its contributors
18a9643ea8Slogwang * may be used to endorse or promote products derived from this software
19a9643ea8Slogwang * without specific prior written permission.
20a9643ea8Slogwang *
21a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22a9643ea8Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23a9643ea8Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24a9643ea8Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25a9643ea8Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26a9643ea8Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27a9643ea8Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28a9643ea8Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29a9643ea8Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30a9643ea8Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a9643ea8Slogwang * SUCH DAMAGE.
32a9643ea8Slogwang *
33a9643ea8Slogwang * @(#)malloc.h 8.5 (Berkeley) 5/3/95
34a9643ea8Slogwang * $FreeBSD$
35a9643ea8Slogwang */
36a9643ea8Slogwang
37a9643ea8Slogwang #ifndef _SYS_MALLOC_H_
38a9643ea8Slogwang #define _SYS_MALLOC_H_
39a9643ea8Slogwang
40*22ce4affSfengbojiang #ifndef _STANDALONE
41a9643ea8Slogwang #include <sys/param.h>
42*22ce4affSfengbojiang #ifdef _KERNEL
43*22ce4affSfengbojiang #include <sys/systm.h>
44*22ce4affSfengbojiang #endif
45a9643ea8Slogwang #include <sys/queue.h>
46a9643ea8Slogwang #include <sys/_lock.h>
47a9643ea8Slogwang #include <sys/_mutex.h>
48*22ce4affSfengbojiang #include <machine/_limits.h>
49a9643ea8Slogwang
50a9643ea8Slogwang #define MINALLOCSIZE UMA_SMALLEST_UNIT
51a9643ea8Slogwang
52a9643ea8Slogwang /*
53*22ce4affSfengbojiang * Flags to memory allocation functions.
54a9643ea8Slogwang */
55a9643ea8Slogwang #define M_NOWAIT 0x0001 /* do not block */
56a9643ea8Slogwang #define M_WAITOK 0x0002 /* ok to block */
57a9643ea8Slogwang #define M_ZERO 0x0100 /* bzero the allocation */
58a9643ea8Slogwang #define M_NOVM 0x0200 /* don't ask VM for pages */
59a9643ea8Slogwang #define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */
60a9643ea8Slogwang #define M_NODUMP 0x0800 /* don't dump pages in this allocation */
61*22ce4affSfengbojiang #define M_FIRSTFIT 0x1000 /* only for vmem, fast fit */
62*22ce4affSfengbojiang #define M_BESTFIT 0x2000 /* only for vmem, low fragmentation */
63*22ce4affSfengbojiang #define M_EXEC 0x4000 /* allocate executable space */
64*22ce4affSfengbojiang #define M_NEXTFIT 0x8000 /* only for vmem, follow cursor */
65a9643ea8Slogwang
66*22ce4affSfengbojiang #define M_VERSION 2020110501
67a9643ea8Slogwang
68a9643ea8Slogwang /*
69a9643ea8Slogwang * Two malloc type structures are present: malloc_type, which is used by a
70a9643ea8Slogwang * type owner to declare the type, and malloc_type_internal, which holds
71a9643ea8Slogwang * malloc-owned statistics and other ABI-sensitive fields, such as the set of
72a9643ea8Slogwang * malloc statistics indexed by the compile-time MAXCPU constant.
73a9643ea8Slogwang * Applications should avoid introducing dependence on the allocator private
74a9643ea8Slogwang * data layout and size.
75a9643ea8Slogwang *
76a9643ea8Slogwang * The malloc_type ks_next field is protected by malloc_mtx. Other fields in
77a9643ea8Slogwang * malloc_type are static after initialization so unsynchronized.
78a9643ea8Slogwang *
79a9643ea8Slogwang * Statistics in malloc_type_stats are written only when holding a critical
80a9643ea8Slogwang * section and running on the CPU associated with the index into the stat
81a9643ea8Slogwang * array, but read lock-free resulting in possible (minor) races, which the
82a9643ea8Slogwang * monitoring app should take into account.
83a9643ea8Slogwang */
84a9643ea8Slogwang struct malloc_type_stats {
85a9643ea8Slogwang uint64_t mts_memalloced; /* Bytes allocated on CPU. */
86a9643ea8Slogwang uint64_t mts_memfreed; /* Bytes freed on CPU. */
87a9643ea8Slogwang uint64_t mts_numallocs; /* Number of allocates on CPU. */
88a9643ea8Slogwang uint64_t mts_numfrees; /* number of frees on CPU. */
89a9643ea8Slogwang uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */
90a9643ea8Slogwang uint64_t _mts_reserved1; /* Reserved field. */
91a9643ea8Slogwang uint64_t _mts_reserved2; /* Reserved field. */
92a9643ea8Slogwang uint64_t _mts_reserved3; /* Reserved field. */
93a9643ea8Slogwang };
94a9643ea8Slogwang
95*22ce4affSfengbojiang _Static_assert(sizeof(struct malloc_type_stats) == 64,
96*22ce4affSfengbojiang "allocations come from pcpu_zone_64");
97*22ce4affSfengbojiang
98a9643ea8Slogwang /*
99a9643ea8Slogwang * Index definitions for the mti_probes[] array.
100a9643ea8Slogwang */
101a9643ea8Slogwang #define DTMALLOC_PROBE_MALLOC 0
102a9643ea8Slogwang #define DTMALLOC_PROBE_FREE 1
103a9643ea8Slogwang #define DTMALLOC_PROBE_MAX 2
104a9643ea8Slogwang
105a9643ea8Slogwang struct malloc_type_internal {
106a9643ea8Slogwang uint32_t mti_probes[DTMALLOC_PROBE_MAX];
107a9643ea8Slogwang /* DTrace probe ID array. */
108a9643ea8Slogwang u_char mti_zone;
109*22ce4affSfengbojiang struct malloc_type_stats *mti_stats;
110*22ce4affSfengbojiang u_long mti_spare[8];
111a9643ea8Slogwang };
112a9643ea8Slogwang
113a9643ea8Slogwang /*
114*22ce4affSfengbojiang * Public data structure describing a malloc type.
115a9643ea8Slogwang */
116a9643ea8Slogwang struct malloc_type {
117a9643ea8Slogwang struct malloc_type *ks_next; /* Next in global chain. */
118*22ce4affSfengbojiang u_long ks_version; /* Detect programmer error. */
119a9643ea8Slogwang const char *ks_shortdesc; /* Printable type name. */
120*22ce4affSfengbojiang struct malloc_type_internal ks_mti;
121a9643ea8Slogwang };
122a9643ea8Slogwang
123a9643ea8Slogwang /*
124a9643ea8Slogwang * Statistics structure headers for user space. The kern.malloc sysctl
125a9643ea8Slogwang * exposes a structure stream consisting of a stream header, then a series of
126a9643ea8Slogwang * malloc type headers and statistics structures (quantity maxcpus). For
127a9643ea8Slogwang * convenience, the kernel will provide the current value of maxcpus at the
128a9643ea8Slogwang * head of the stream.
129a9643ea8Slogwang */
130a9643ea8Slogwang #define MALLOC_TYPE_STREAM_VERSION 0x00000001
131a9643ea8Slogwang struct malloc_type_stream_header {
132a9643ea8Slogwang uint32_t mtsh_version; /* Stream format version. */
133a9643ea8Slogwang uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */
134a9643ea8Slogwang uint32_t mtsh_count; /* Number of records. */
135a9643ea8Slogwang uint32_t _mtsh_pad; /* Pad/reserved field. */
136a9643ea8Slogwang };
137a9643ea8Slogwang
138a9643ea8Slogwang #define MALLOC_MAX_NAME 32
139a9643ea8Slogwang struct malloc_type_header {
140a9643ea8Slogwang char mth_name[MALLOC_MAX_NAME];
141a9643ea8Slogwang };
142a9643ea8Slogwang
143a9643ea8Slogwang #ifdef _KERNEL
144a9643ea8Slogwang #define MALLOC_DEFINE(type, shortdesc, longdesc) \
145a9643ea8Slogwang struct malloc_type type[1] = { \
146*22ce4affSfengbojiang { \
147*22ce4affSfengbojiang .ks_next = NULL, \
148*22ce4affSfengbojiang .ks_version = M_VERSION, \
149*22ce4affSfengbojiang .ks_shortdesc = shortdesc, \
150*22ce4affSfengbojiang } \
151a9643ea8Slogwang }; \
152a9643ea8Slogwang SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_THIRD, malloc_init, \
153a9643ea8Slogwang type); \
154a9643ea8Slogwang SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, \
155a9643ea8Slogwang malloc_uninit, type)
156a9643ea8Slogwang
157a9643ea8Slogwang #define MALLOC_DECLARE(type) \
158a9643ea8Slogwang extern struct malloc_type type[1]
159a9643ea8Slogwang
160a9643ea8Slogwang MALLOC_DECLARE(M_CACHE);
161a9643ea8Slogwang MALLOC_DECLARE(M_DEVBUF);
162a9643ea8Slogwang MALLOC_DECLARE(M_TEMP);
163a9643ea8Slogwang
164a9643ea8Slogwang /*
165a9643ea8Slogwang * XXX this should be declared in <sys/uio.h>, but that tends to fail
166a9643ea8Slogwang * because <sys/uio.h> is included in a header before the source file
167a9643ea8Slogwang * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
168a9643ea8Slogwang */
169a9643ea8Slogwang MALLOC_DECLARE(M_IOV);
170a9643ea8Slogwang
171*22ce4affSfengbojiang struct domainset;
172a9643ea8Slogwang extern struct mtx malloc_mtx;
173a9643ea8Slogwang
174a9643ea8Slogwang /*
175a9643ea8Slogwang * Function type used when iterating over the list of malloc types.
176a9643ea8Slogwang */
177a9643ea8Slogwang typedef void malloc_type_list_func_t(struct malloc_type *, void *);
178a9643ea8Slogwang
179a9643ea8Slogwang void contigfree(void *addr, unsigned long size, struct malloc_type *type);
180a9643ea8Slogwang void *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
181a9643ea8Slogwang vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
182a9643ea8Slogwang vm_paddr_t boundary) __malloc_like __result_use_check
183a9643ea8Slogwang __alloc_size(1) __alloc_align(6);
184*22ce4affSfengbojiang void *contigmalloc_domainset(unsigned long size, struct malloc_type *type,
185*22ce4affSfengbojiang struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high,
186*22ce4affSfengbojiang unsigned long alignment, vm_paddr_t boundary)
187*22ce4affSfengbojiang __malloc_like __result_use_check __alloc_size(1) __alloc_align(7);
188a9643ea8Slogwang void free(void *addr, struct malloc_type *type);
189*22ce4affSfengbojiang void zfree(void *addr, struct malloc_type *type);
190*22ce4affSfengbojiang void *malloc(size_t size, struct malloc_type *type, int flags) __malloc_like
191*22ce4affSfengbojiang __result_use_check __alloc_size(1);
192*22ce4affSfengbojiang
193*22ce4affSfengbojiang #ifndef FSTACK
194*22ce4affSfengbojiang /*
195*22ce4affSfengbojiang * Try to optimize malloc(..., ..., M_ZERO) allocations by doing zeroing in
196*22ce4affSfengbojiang * place if the size is known at compilation time.
197*22ce4affSfengbojiang *
198*22ce4affSfengbojiang * Passing the flag down requires malloc to blindly zero the entire object.
199*22ce4affSfengbojiang * In practice a lot of the zeroing can be avoided if most of the object
200*22ce4affSfengbojiang * gets explicitly initialized after the allocation. Letting the compiler
201*22ce4affSfengbojiang * zero in place gives it the opportunity to take advantage of this state.
202*22ce4affSfengbojiang *
203*22ce4affSfengbojiang * Note that the operation is only applicable if both flags and size are
204*22ce4affSfengbojiang * known at compilation time. If M_ZERO is passed but M_WAITOK is not, the
205*22ce4affSfengbojiang * allocation can fail and a NULL check is needed. However, if M_WAITOK is
206*22ce4affSfengbojiang * passed we know the allocation must succeed and the check can be elided.
207*22ce4affSfengbojiang *
208*22ce4affSfengbojiang * _malloc_item = malloc(_size, type, (flags) &~ M_ZERO);
209*22ce4affSfengbojiang * if (((flags) & M_WAITOK) != 0 || _malloc_item != NULL)
210*22ce4affSfengbojiang * bzero(_malloc_item, _size);
211*22ce4affSfengbojiang *
212*22ce4affSfengbojiang * If the flag is set, the compiler knows the left side is always true,
213*22ce4affSfengbojiang * therefore the entire statement is true and the callsite is:
214*22ce4affSfengbojiang *
215*22ce4affSfengbojiang * _malloc_item = malloc(_size, type, (flags) &~ M_ZERO);
216*22ce4affSfengbojiang * bzero(_malloc_item, _size);
217*22ce4affSfengbojiang *
218*22ce4affSfengbojiang * If the flag is not set, the compiler knows the left size is always false
219*22ce4affSfengbojiang * and the NULL check is needed, therefore the callsite is:
220*22ce4affSfengbojiang *
221*22ce4affSfengbojiang * _malloc_item = malloc(_size, type, (flags) &~ M_ZERO);
222*22ce4affSfengbojiang * if (_malloc_item != NULL)
223*22ce4affSfengbojiang * bzero(_malloc_item, _size);
224*22ce4affSfengbojiang *
225*22ce4affSfengbojiang * The implementation is a macro because of what appears to be a clang 6 bug:
226*22ce4affSfengbojiang * an inline function variant ended up being compiled to a mere malloc call
227*22ce4affSfengbojiang * regardless of argument. gcc generates expected code (like the above).
228*22ce4affSfengbojiang */
229*22ce4affSfengbojiang #define malloc(size, type, flags) ({ \
230*22ce4affSfengbojiang void *_malloc_item; \
231*22ce4affSfengbojiang size_t _size = (size); \
232*22ce4affSfengbojiang if (__builtin_constant_p(size) && __builtin_constant_p(flags) &&\
233*22ce4affSfengbojiang ((flags) & M_ZERO) != 0) { \
234*22ce4affSfengbojiang _malloc_item = malloc(_size, type, (flags) &~ M_ZERO); \
235*22ce4affSfengbojiang if (((flags) & M_WAITOK) != 0 || \
236*22ce4affSfengbojiang __predict_true(_malloc_item != NULL)) \
237*22ce4affSfengbojiang bzero(_malloc_item, _size); \
238*22ce4affSfengbojiang } else { \
239*22ce4affSfengbojiang _malloc_item = malloc(_size, type, flags); \
240*22ce4affSfengbojiang } \
241*22ce4affSfengbojiang _malloc_item; \
242*22ce4affSfengbojiang })
243*22ce4affSfengbojiang #endif
244*22ce4affSfengbojiang
245*22ce4affSfengbojiang void *malloc_domainset(size_t size, struct malloc_type *type,
246*22ce4affSfengbojiang struct domainset *ds, int flags) __malloc_like __result_use_check
247*22ce4affSfengbojiang __alloc_size(1);
248*22ce4affSfengbojiang void *mallocarray(size_t nmemb, size_t size, struct malloc_type *type,
249*22ce4affSfengbojiang int flags) __malloc_like __result_use_check
250*22ce4affSfengbojiang __alloc_size2(1, 2);
251*22ce4affSfengbojiang void *malloc_exec(size_t size, struct malloc_type *type, int flags) __malloc_like
252*22ce4affSfengbojiang __result_use_check __alloc_size(1);
253*22ce4affSfengbojiang void *malloc_domainset_exec(size_t size, struct malloc_type *type,
254*22ce4affSfengbojiang struct domainset *ds, int flags) __malloc_like __result_use_check
255*22ce4affSfengbojiang __alloc_size(1);
256a9643ea8Slogwang void malloc_init(void *);
257a9643ea8Slogwang void malloc_type_allocated(struct malloc_type *type, unsigned long size);
258a9643ea8Slogwang void malloc_type_freed(struct malloc_type *type, unsigned long size);
259a9643ea8Slogwang void malloc_type_list(malloc_type_list_func_t *, void *);
260a9643ea8Slogwang void malloc_uninit(void *);
261*22ce4affSfengbojiang size_t malloc_size(size_t);
262*22ce4affSfengbojiang size_t malloc_usable_size(const void *);
263*22ce4affSfengbojiang void *realloc(void *addr, size_t size, struct malloc_type *type, int flags)
264*22ce4affSfengbojiang __result_use_check __alloc_size(2);
265*22ce4affSfengbojiang void *reallocf(void *addr, size_t size, struct malloc_type *type, int flags)
266*22ce4affSfengbojiang __result_use_check __alloc_size(2);
267*22ce4affSfengbojiang void *malloc_domainset_aligned(size_t size, size_t align,
268*22ce4affSfengbojiang struct malloc_type *mtp, struct domainset *ds, int flags)
269*22ce4affSfengbojiang __malloc_like __result_use_check __alloc_size(1);
270a9643ea8Slogwang
271a9643ea8Slogwang struct malloc_type *malloc_desc2type(const char *desc);
272*22ce4affSfengbojiang
273*22ce4affSfengbojiang /*
274*22ce4affSfengbojiang * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
275*22ce4affSfengbojiang * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
276*22ce4affSfengbojiang */
277*22ce4affSfengbojiang #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 8 / 2))
278*22ce4affSfengbojiang static inline bool
WOULD_OVERFLOW(size_t nmemb,size_t size)279*22ce4affSfengbojiang WOULD_OVERFLOW(size_t nmemb, size_t size)
280*22ce4affSfengbojiang {
281*22ce4affSfengbojiang
282*22ce4affSfengbojiang return ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
283*22ce4affSfengbojiang nmemb > 0 && __SIZE_T_MAX / nmemb < size);
284*22ce4affSfengbojiang }
285*22ce4affSfengbojiang #undef MUL_NO_OVERFLOW
286a9643ea8Slogwang #endif /* _KERNEL */
287a9643ea8Slogwang
288*22ce4affSfengbojiang #else
289*22ce4affSfengbojiang /*
290*22ce4affSfengbojiang * The native stand malloc / free interface we're mapping to
291*22ce4affSfengbojiang */
292*22ce4affSfengbojiang extern void Free(void *p, const char *file, int line);
293*22ce4affSfengbojiang extern void *Malloc(size_t bytes, const char *file, int line);
294*22ce4affSfengbojiang
295*22ce4affSfengbojiang /*
296*22ce4affSfengbojiang * Minimal standalone malloc implementation / environment. None of the
297*22ce4affSfengbojiang * flags mean anything and there's no need declare malloc types.
298*22ce4affSfengbojiang * Define the simple alloc / free routines in terms of Malloc and
299*22ce4affSfengbojiang * Free. None of the kernel features that this stuff disables are needed.
300*22ce4affSfengbojiang *
301*22ce4affSfengbojiang * XXX we are setting ourselves up for a potential crash if we can't allocate
302*22ce4affSfengbojiang * memory for a M_WAITOK call.
303*22ce4affSfengbojiang */
304*22ce4affSfengbojiang #define M_WAITOK 0
305*22ce4affSfengbojiang #define M_ZERO 0
306*22ce4affSfengbojiang #define M_NOWAIT 0
307*22ce4affSfengbojiang #define MALLOC_DECLARE(x)
308*22ce4affSfengbojiang
309*22ce4affSfengbojiang #define kmem_zalloc(size, flags) Malloc((size), __FILE__, __LINE__)
310*22ce4affSfengbojiang #define kmem_free(p, size) Free(p, __FILE__, __LINE__)
311*22ce4affSfengbojiang
312*22ce4affSfengbojiang /*
313*22ce4affSfengbojiang * ZFS mem.h define that's the OpenZFS porting layer way of saying
314*22ce4affSfengbojiang * M_WAITOK. Given the above, it will also be a nop.
315*22ce4affSfengbojiang */
316*22ce4affSfengbojiang #define KM_SLEEP M_WAITOK
317*22ce4affSfengbojiang #endif /* _STANDALONE */
318a9643ea8Slogwang #endif /* !_SYS_MALLOC_H_ */
319