xref: /netbsd-src/sys/kern/kern_malloc.c (revision 76c7fc5f6b13ed0b1508e6b313e88e59977ed78e)
1 /*	$NetBSD: kern_malloc.c,v 1.157 2019/04/07 09:20:04 maxv Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
32  */
33 
34 /*
35  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
66  */
67 
68 /*
69  * Wrapper interface for obsolete malloc(9).
70  */
71 
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.157 2019/04/07 09:20:04 maxv Exp $");
74 
75 #include <sys/param.h>
76 #include <sys/malloc.h>
77 #include <sys/kmem.h>
78 #include <sys/asan.h>
79 
80 /*
81  * Built-in malloc types.  Note: ought to be removed.
82  */
83 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
84 MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
85 MALLOC_DEFINE(M_FREE, "free", "should be on free list");
86 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
87 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
88 MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
89 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
90 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
91 MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
92 
93 /*
94  * Header contains total size, including the header itself.
95  */
96 struct malloc_header {
97 	size_t mh_size;
98 #ifdef KASAN
99 	size_t mh_rqsz;
100 #endif
101 } __aligned(ALIGNBYTES + 1);
102 
103 void *
104 kern_malloc(unsigned long reqsize, int flags)
105 {
106 	const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
107 #ifdef KASAN
108 	const size_t origsize = reqsize;
109 #endif
110 	size_t size = reqsize;
111 	size_t allocsize, hdroffset;
112 	struct malloc_header *mh;
113 	void *p;
114 
115 	kasan_add_redzone(&size);
116 
117 	if (size >= PAGE_SIZE) {
118 		if (size > (ULONG_MAX-PAGE_SIZE))
119 			allocsize = ULONG_MAX;	/* this will fail later */
120 		else
121 			allocsize = PAGE_SIZE + size; /* for page alignment */
122 		hdroffset = PAGE_SIZE - sizeof(struct malloc_header);
123 	} else {
124 		allocsize = sizeof(struct malloc_header) + size;
125 		hdroffset = 0;
126 	}
127 
128 	p = kmem_intr_alloc(allocsize, kmflags);
129 	if (p == NULL)
130 		return NULL;
131 
132 	if ((flags & M_ZERO) != 0) {
133 		memset(p, 0, allocsize);
134 	}
135 	mh = (void *)((char *)p + hdroffset);
136 	mh->mh_size = allocsize - hdroffset;
137 #ifdef KASAN
138 	mh->mh_rqsz = origsize;
139 #endif
140 	mh++;
141 
142 	kasan_mark(mh, origsize, size, KASAN_MALLOC_REDZONE);
143 
144 	return mh;
145 }
146 
147 void
148 kern_free(void *addr)
149 {
150 	struct malloc_header *mh;
151 
152 	mh = addr;
153 	mh--;
154 
155 	kasan_mark(addr, mh->mh_size - sizeof(struct malloc_header),
156 	    mh->mh_size - sizeof(struct malloc_header), KASAN_MALLOC_REDZONE);
157 
158 	if (mh->mh_size >= PAGE_SIZE + sizeof(struct malloc_header))
159 		kmem_intr_free((char *)addr - PAGE_SIZE,
160 		    mh->mh_size + PAGE_SIZE - sizeof(struct malloc_header));
161 	else
162 		kmem_intr_free(mh, mh->mh_size);
163 }
164 
165 void *
166 kern_realloc(void *curaddr, unsigned long newsize, int flags)
167 {
168 	struct malloc_header *mh;
169 	unsigned long cursize;
170 	void *newaddr;
171 
172 	/*
173 	 * realloc() with a NULL pointer is the same as malloc().
174 	 */
175 	if (curaddr == NULL)
176 		return malloc(newsize, ksp, flags);
177 
178 	/*
179 	 * realloc() with zero size is the same as free().
180 	 */
181 	if (newsize == 0) {
182 		free(curaddr, ksp);
183 		return NULL;
184 	}
185 
186 	if ((flags & M_NOWAIT) == 0) {
187 		ASSERT_SLEEPABLE();
188 	}
189 
190 	mh = curaddr;
191 	mh--;
192 
193 #ifdef KASAN
194 	cursize = mh->mh_rqsz;
195 #else
196 	cursize = mh->mh_size - sizeof(struct malloc_header);
197 #endif
198 
199 	/*
200 	 * If we already actually have as much as they want, we're done.
201 	 */
202 	if (newsize <= cursize)
203 		return curaddr;
204 
205 	/*
206 	 * Can't satisfy the allocation with the existing block.
207 	 * Allocate a new one and copy the data.
208 	 */
209 	newaddr = malloc(newsize, ksp, flags);
210 	if (__predict_false(newaddr == NULL)) {
211 		/*
212 		 * malloc() failed, because flags included M_NOWAIT.
213 		 * Return NULL to indicate that failure.  The old
214 		 * pointer is still valid.
215 		 */
216 		return NULL;
217 	}
218 	memcpy(newaddr, curaddr, cursize);
219 
220 	/*
221 	 * We were successful: free the old allocation and return
222 	 * the new one.
223 	 */
224 	free(curaddr, ksp);
225 	return newaddr;
226 }
227