| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: kern_malloc.c,v 1.152 2018/08/22 12:42:06 maxv Exp $ */ | | 1 | /* $NetBSD: kern_malloc.c,v 1.153 2018/08/22 14:12:30 christos Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1987, 1991, 1993 | | 4 | * Copyright (c) 1987, 1991, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -60,31 +60,27 @@ | | | @@ -60,31 +60,27 @@ |
60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 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 | | 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 | | 62 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
63 | * SUCH DAMAGE. | | 63 | * SUCH DAMAGE. |
64 | * | | 64 | * |
65 | * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95 | | 65 | * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95 |
66 | */ | | 66 | */ |
67 | | | 67 | |
68 | /* | | 68 | /* |
69 | * Wrapper interface for obsolete malloc(9). | | 69 | * Wrapper interface for obsolete malloc(9). |
70 | */ | | 70 | */ |
71 | | | 71 | |
72 | #include <sys/cdefs.h> | | 72 | #include <sys/cdefs.h> |
73 | __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.152 2018/08/22 12:42:06 maxv Exp $"); | | 73 | __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.153 2018/08/22 14:12:30 christos Exp $"); |
74 | | | | |
75 | #ifdef _KERNEL_OPT | | | |
76 | #include "opt_kasan.h" | | | |
77 | #endif | | | |
78 | | | 74 | |
79 | #include <sys/param.h> | | 75 | #include <sys/param.h> |
80 | #include <sys/malloc.h> | | 76 | #include <sys/malloc.h> |
81 | #include <sys/kmem.h> | | 77 | #include <sys/kmem.h> |
82 | #include <sys/asan.h> | | 78 | #include <sys/asan.h> |
83 | | | 79 | |
84 | /* | | 80 | /* |
85 | * Built-in malloc types. Note: ought to be removed. | | 81 | * Built-in malloc types. Note: ought to be removed. |
86 | */ | | 82 | */ |
87 | MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); | | 83 | MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); |
88 | MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures"); | | 84 | MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures"); |
89 | MALLOC_DEFINE(M_FREE, "free", "should be on free list"); | | 85 | MALLOC_DEFINE(M_FREE, "free", "should be on free list"); |
90 | MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers"); | | 86 | MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers"); |
| @@ -105,73 +101,67 @@ struct malloc_header { | | | @@ -105,73 +101,67 @@ struct malloc_header { |
105 | } __aligned(ALIGNBYTES + 1); | | 101 | } __aligned(ALIGNBYTES + 1); |
106 | | | 102 | |
107 | void * | | 103 | void * |
108 | kern_malloc(unsigned long size, int flags) | | 104 | kern_malloc(unsigned long size, int flags) |
109 | { | | 105 | { |
110 | const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP; | | 106 | const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP; |
111 | #ifdef KASAN | | 107 | #ifdef KASAN |
112 | const size_t origsize = size; | | 108 | const size_t origsize = size; |
113 | #endif | | 109 | #endif |
114 | size_t allocsize, hdroffset; | | 110 | size_t allocsize, hdroffset; |
115 | struct malloc_header *mh; | | 111 | struct malloc_header *mh; |
116 | void *p; | | 112 | void *p; |
117 | | | 113 | |
118 | #ifdef KASAN | | | |
119 | kasan_add_redzone(&size); | | 114 | kasan_add_redzone(&size); |
120 | #endif | | | |
121 | | | 115 | |
122 | if (size >= PAGE_SIZE) { | | 116 | if (size >= PAGE_SIZE) { |
123 | if (size > (ULONG_MAX-PAGE_SIZE)) | | 117 | if (size > (ULONG_MAX-PAGE_SIZE)) |
124 | allocsize = ULONG_MAX; /* this will fail later */ | | 118 | allocsize = ULONG_MAX; /* this will fail later */ |
125 | else | | 119 | else |
126 | allocsize = PAGE_SIZE + size; /* for page alignment */ | | 120 | allocsize = PAGE_SIZE + size; /* for page alignment */ |
127 | hdroffset = PAGE_SIZE - sizeof(struct malloc_header); | | 121 | hdroffset = PAGE_SIZE - sizeof(struct malloc_header); |
128 | } else { | | 122 | } else { |
129 | allocsize = sizeof(struct malloc_header) + size; | | 123 | allocsize = sizeof(struct malloc_header) + size; |
130 | hdroffset = 0; | | 124 | hdroffset = 0; |
131 | } | | 125 | } |
132 | | | 126 | |
133 | p = kmem_intr_alloc(allocsize, kmflags); | | 127 | p = kmem_intr_alloc(allocsize, kmflags); |
134 | if (p == NULL) | | 128 | if (p == NULL) |
135 | return NULL; | | 129 | return NULL; |
136 | | | 130 | |
137 | if ((flags & M_ZERO) != 0) { | | 131 | if ((flags & M_ZERO) != 0) { |
138 | memset(p, 0, allocsize); | | 132 | memset(p, 0, allocsize); |
139 | } | | 133 | } |
140 | mh = (void *)((char *)p + hdroffset); | | 134 | mh = (void *)((char *)p + hdroffset); |
141 | mh->mh_size = allocsize - hdroffset; | | 135 | mh->mh_size = allocsize - hdroffset; |
142 | #ifdef KASAN | | 136 | #ifdef KASAN |
143 | mh->mh_rqsz = origsize; | | 137 | mh->mh_rqsz = origsize; |
144 | #endif | | 138 | #endif |
145 | mh++; | | 139 | mh++; |
146 | | | 140 | |
147 | #ifdef KASAN | | | |
148 | kasan_alloc(mh, origsize, size); | | 141 | kasan_alloc(mh, origsize, size); |
149 | #endif | | | |
150 | | | 142 | |
151 | return mh; | | 143 | return mh; |
152 | } | | 144 | } |
153 | | | 145 | |
154 | void | | 146 | void |
155 | kern_free(void *addr) | | 147 | kern_free(void *addr) |
156 | { | | 148 | { |
157 | struct malloc_header *mh; | | 149 | struct malloc_header *mh; |
158 | | | 150 | |
159 | mh = addr; | | 151 | mh = addr; |
160 | mh--; | | 152 | mh--; |
161 | | | 153 | |
162 | #ifdef KASAN | | | |
163 | kasan_free(addr, mh->mh_size); | | 154 | kasan_free(addr, mh->mh_size); |
164 | #endif | | | |
165 | | | 155 | |
166 | if (mh->mh_size >= PAGE_SIZE + sizeof(struct malloc_header)) | | 156 | if (mh->mh_size >= PAGE_SIZE + sizeof(struct malloc_header)) |
167 | kmem_intr_free((char *)addr - PAGE_SIZE, | | 157 | kmem_intr_free((char *)addr - PAGE_SIZE, |
168 | mh->mh_size + PAGE_SIZE - sizeof(struct malloc_header)); | | 158 | mh->mh_size + PAGE_SIZE - sizeof(struct malloc_header)); |
169 | else | | 159 | else |
170 | kmem_intr_free(mh, mh->mh_size); | | 160 | kmem_intr_free(mh, mh->mh_size); |
171 | } | | 161 | } |
172 | | | 162 | |
173 | void * | | 163 | void * |
174 | kern_realloc(void *curaddr, unsigned long newsize, int flags) | | 164 | kern_realloc(void *curaddr, unsigned long newsize, int flags) |
175 | { | | 165 | { |
176 | struct malloc_header *mh; | | 166 | struct malloc_header *mh; |
177 | unsigned long cursize; | | 167 | unsigned long cursize; |