Wed Aug 22 14:12:30 2018 UTC ()
- opt_kasan.h is included from <sys/asan.h>
- now that we are not using inlines, we need one more ifdef.


(christos)
diff -r1.152 -r1.153 src/sys/kern/kern_malloc.c
diff -r1.70 -r1.71 src/sys/kern/subr_kmem.c

cvs diff -r1.152 -r1.153 src/sys/kern/kern_malloc.c (expand / switch to unified diff)

--- src/sys/kern/kern_malloc.c 2018/08/22 12:42:06 1.152
+++ src/sys/kern/kern_malloc.c 2018/08/22 14:12:30 1.153
@@ -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 */
87MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 83MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
88MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures"); 84MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
89MALLOC_DEFINE(M_FREE, "free", "should be on free list"); 85MALLOC_DEFINE(M_FREE, "free", "should be on free list");
90MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers"); 86MALLOC_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
107void * 103void *
108kern_malloc(unsigned long size, int flags) 104kern_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
154void 146void
155kern_free(void *addr) 147kern_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
173void * 163void *
174kern_realloc(void *curaddr, unsigned long newsize, int flags) 164kern_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;

cvs diff -r1.70 -r1.71 src/sys/kern/subr_kmem.c (expand / switch to unified diff)

--- src/sys/kern/subr_kmem.c 2018/08/22 09:38:21 1.70
+++ src/sys/kern/subr_kmem.c 2018/08/22 14:12:30 1.71
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: subr_kmem.c,v 1.70 2018/08/22 09:38:21 maxv Exp $ */ 1/* $NetBSD: subr_kmem.c,v 1.71 2018/08/22 14:12:30 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2009-2015 The NetBSD Foundation, Inc. 4 * Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Maxime Villard. 8 * by Andrew Doran and Maxime Villard.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -82,31 +82,30 @@ @@ -82,31 +82,30 @@
82 * A kernel with "option DEBUG" has "kmem_guard" debugging feature compiled 82 * A kernel with "option DEBUG" has "kmem_guard" debugging feature compiled
83 * in. See the comment below for what kind of bugs it tries to detect. Even 83 * in. See the comment below for what kind of bugs it tries to detect. Even
84 * if compiled in, it's disabled by default because it's very expensive. 84 * if compiled in, it's disabled by default because it's very expensive.
85 * You can enable it on boot by: 85 * You can enable it on boot by:
86 * boot -d 86 * boot -d
87 * db> w kmem_guard_depth 0t30000 87 * db> w kmem_guard_depth 0t30000
88 * db> c 88 * db> c
89 * 89 *
90 * The default value of kmem_guard_depth is 0, which means disabled. 90 * The default value of kmem_guard_depth is 0, which means disabled.
91 * It can be changed by KMEM_GUARD_DEPTH kernel config option. 91 * It can be changed by KMEM_GUARD_DEPTH kernel config option.
92 */ 92 */
93 93
94#include <sys/cdefs.h> 94#include <sys/cdefs.h>
95__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.70 2018/08/22 09:38:21 maxv Exp $"); 95__KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.71 2018/08/22 14:12:30 christos Exp $");
96 96
97#ifdef _KERNEL_OPT 97#ifdef _KERNEL_OPT
98#include "opt_kmem.h" 98#include "opt_kmem.h"
99#include "opt_kasan.h" 
100#endif 99#endif
101 100
102#include <sys/param.h> 101#include <sys/param.h>
103#include <sys/callback.h> 102#include <sys/callback.h>
104#include <sys/kmem.h> 103#include <sys/kmem.h>
105#include <sys/pool.h> 104#include <sys/pool.h>
106#include <sys/debug.h> 105#include <sys/debug.h>
107#include <sys/lockdebug.h> 106#include <sys/lockdebug.h>
108#include <sys/cpu.h> 107#include <sys/cpu.h>
109#include <sys/asan.h> 108#include <sys/asan.h>
110 109
111#include <uvm/uvm_extern.h> 110#include <uvm/uvm_extern.h>
112#include <uvm/uvm_map.h> 111#include <uvm/uvm_map.h>
@@ -214,27 +213,29 @@ static bool kmem_guard_enabled; @@ -214,27 +213,29 @@ static bool kmem_guard_enabled;
214static struct kmem_guard kmem_guard; 213static struct kmem_guard kmem_guard;
215#endif /* defined(KMEM_GUARD) */ 214#endif /* defined(KMEM_GUARD) */
216 215
217CTASSERT(KM_SLEEP == PR_WAITOK); 216CTASSERT(KM_SLEEP == PR_WAITOK);
218CTASSERT(KM_NOSLEEP == PR_NOWAIT); 217CTASSERT(KM_NOSLEEP == PR_NOWAIT);
219 218
220/* 219/*
221 * kmem_intr_alloc: allocate wired memory. 220 * kmem_intr_alloc: allocate wired memory.
222 */ 221 */
223 222
224void * 223void *
225kmem_intr_alloc(size_t requested_size, km_flag_t kmflags) 224kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
226{ 225{
 226#ifdef KASAN
227 const size_t origsize = requested_size; 227 const size_t origsize = requested_size;
 228#endif
228 size_t allocsz, index; 229 size_t allocsz, index;
229 size_t size; 230 size_t size;
230 pool_cache_t pc; 231 pool_cache_t pc;
231 uint8_t *p; 232 uint8_t *p;
232 233
233 KASSERT(requested_size > 0); 234 KASSERT(requested_size > 0);
234 235
235 KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP)); 236 KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
236 KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP)); 237 KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
237 238
238#ifdef KMEM_GUARD 239#ifdef KMEM_GUARD
239 if (kmem_guard_enabled) { 240 if (kmem_guard_enabled) {
240 return kmem_guard_alloc(&kmem_guard, requested_size, 241 return kmem_guard_alloc(&kmem_guard, requested_size,