]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
f427ee49 | 2 | * Copyright (c) 1998-2020 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 A |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1982, 1986, 1988, 1991, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. All advertising materials mentioning features or use of this software | |
42 | * must display the following acknowledgement: | |
43 | * This product includes software developed by the University of | |
44 | * California, Berkeley and its contributors. | |
45 | * 4. Neither the name of the University nor the names of its contributors | |
46 | * may be used to endorse or promote products derived from this software | |
47 | * without specific prior written permission. | |
48 | * | |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94 | |
62 | */ | |
2d21ac55 A |
63 | /* |
64 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
65 | * support for mandatory and extensible security protections. This notice | |
66 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
67 | * Version 2.0. | |
1c79356b A |
68 | */ |
69 | ||
f427ee49 A |
70 | #include <ptrauth.h> |
71 | ||
1c79356b A |
72 | #include <sys/param.h> |
73 | #include <sys/systm.h> | |
74 | #include <sys/malloc.h> | |
75 | #include <sys/mbuf.h> | |
76 | #include <sys/kernel.h> | |
91447636 | 77 | #include <sys/sysctl.h> |
1c79356b A |
78 | #include <sys/syslog.h> |
79 | #include <sys/protosw.h> | |
80 | #include <sys/domain.h> | |
2d21ac55 | 81 | #include <sys/queue.h> |
b0d623f7 | 82 | #include <sys/proc.h> |
1c79356b | 83 | |
39236c6e A |
84 | #include <dev/random/randomdev.h> |
85 | ||
9bccf70c | 86 | #include <kern/kern_types.h> |
2d21ac55 A |
87 | #include <kern/simple_lock.h> |
88 | #include <kern/queue.h> | |
9bccf70c | 89 | #include <kern/sched_prim.h> |
39037602 | 90 | #include <kern/backtrace.h> |
2d21ac55 | 91 | #include <kern/cpu_number.h> |
6d2010ae | 92 | #include <kern/zalloc.h> |
2d21ac55 A |
93 | |
94 | #include <libkern/OSAtomic.h> | |
39236c6e | 95 | #include <libkern/OSDebug.h> |
2d21ac55 | 96 | #include <libkern/libkern.h> |
9bccf70c | 97 | |
a39ff7e2 A |
98 | #include <os/log.h> |
99 | ||
55e303ae A |
100 | #include <IOKit/IOMapper.h> |
101 | ||
2d21ac55 A |
102 | #include <machine/limits.h> |
103 | #include <machine/machine_routines.h> | |
55e303ae | 104 | |
2d21ac55 | 105 | #include <sys/mcache.h> |
fe8ab488 | 106 | #include <net/ntstat.h> |
1c79356b | 107 | |
2d21ac55 A |
108 | /* |
109 | * MBUF IMPLEMENTATION NOTES. | |
110 | * | |
111 | * There is a total of 5 per-CPU caches: | |
112 | * | |
113 | * MC_MBUF: | |
114 | * This is a cache of rudimentary objects of MSIZE in size; each | |
115 | * object represents an mbuf structure. This cache preserves only | |
116 | * the m_type field of the mbuf during its transactions. | |
117 | * | |
118 | * MC_CL: | |
119 | * This is a cache of rudimentary objects of MCLBYTES in size; each | |
120 | * object represents a mcluster structure. This cache does not | |
121 | * preserve the contents of the objects during its transactions. | |
122 | * | |
123 | * MC_BIGCL: | |
6d2010ae | 124 | * This is a cache of rudimentary objects of MBIGCLBYTES in size; each |
2d21ac55 A |
125 | * object represents a mbigcluster structure. This cache does not |
126 | * preserve the contents of the objects during its transaction. | |
127 | * | |
128 | * MC_MBUF_CL: | |
129 | * This is a cache of mbufs each having a cluster attached to it. | |
130 | * It is backed by MC_MBUF and MC_CL rudimentary caches. Several | |
131 | * fields of the mbuf related to the external cluster are preserved | |
132 | * during transactions. | |
133 | * | |
134 | * MC_MBUF_BIGCL: | |
135 | * This is a cache of mbufs each having a big cluster attached to it. | |
136 | * It is backed by MC_MBUF and MC_BIGCL rudimentary caches. Several | |
137 | * fields of the mbuf related to the external cluster are preserved | |
138 | * during transactions. | |
139 | * | |
140 | * OBJECT ALLOCATION: | |
141 | * | |
142 | * Allocation requests are handled first at the per-CPU (mcache) layer | |
143 | * before falling back to the slab layer. Performance is optimal when | |
144 | * the request is satisfied at the CPU layer because global data/lock | |
145 | * never gets accessed. When the slab layer is entered for allocation, | |
146 | * the slab freelist will be checked first for available objects before | |
147 | * the VM backing store is invoked. Slab layer operations are serialized | |
148 | * for all of the caches as the mbuf global lock is held most of the time. | |
149 | * Allocation paths are different depending on the class of objects: | |
150 | * | |
151 | * a. Rudimentary object: | |
152 | * | |
153 | * { m_get_common(), m_clattach(), m_mclget(), | |
154 | * m_mclalloc(), m_bigalloc(), m_copym_with_hdrs(), | |
155 | * composite object allocation } | |
156 | * | ^ | |
157 | * | | | |
158 | * | +-----------------------+ | |
159 | * v | | |
160 | * mcache_alloc/mcache_alloc_ext() mbuf_slab_audit() | |
161 | * | ^ | |
162 | * v | | |
163 | * [CPU cache] -------> (found?) -------+ | |
164 | * | | | |
165 | * v | | |
166 | * mbuf_slab_alloc() | | |
167 | * | | | |
168 | * v | | |
169 | * +---------> [freelist] -------> (found?) -------+ | |
170 | * | | | |
171 | * | v | |
172 | * | m_clalloc() | |
173 | * | | | |
174 | * | v | |
175 | * +---<<---- kmem_mb_alloc() | |
176 | * | |
177 | * b. Composite object: | |
178 | * | |
179 | * { m_getpackets_internal(), m_allocpacket_internal() } | |
180 | * | ^ | |
181 | * | | | |
182 | * | +------ (done) ---------+ | |
183 | * v | | |
184 | * mcache_alloc/mcache_alloc_ext() mbuf_cslab_audit() | |
185 | * | ^ | |
186 | * v | | |
187 | * [CPU cache] -------> (found?) -------+ | |
188 | * | | | |
189 | * v | | |
190 | * mbuf_cslab_alloc() | | |
191 | * | | | |
192 | * v | | |
193 | * [freelist] -------> (found?) -------+ | |
194 | * | | | |
195 | * v | | |
196 | * (rudimentary object) | | |
197 | * mcache_alloc/mcache_alloc_ext() ------>>-----+ | |
198 | * | |
199 | * Auditing notes: If auditing is enabled, buffers will be subjected to | |
200 | * integrity checks by the audit routine. This is done by verifying their | |
201 | * contents against DEADBEEF (free) pattern before returning them to caller. | |
202 | * As part of this step, the routine will also record the transaction and | |
203 | * pattern-fill the buffers with BADDCAFE (uninitialized) pattern. It will | |
204 | * also restore any constructed data structure fields if necessary. | |
205 | * | |
206 | * OBJECT DEALLOCATION: | |
207 | * | |
208 | * Freeing an object simply involves placing it into the CPU cache; this | |
209 | * pollutes the cache to benefit subsequent allocations. The slab layer | |
210 | * will only be entered if the object is to be purged out of the cache. | |
211 | * During normal operations, this happens only when the CPU layer resizes | |
212 | * its bucket while it's adjusting to the allocation load. Deallocation | |
213 | * paths are different depending on the class of objects: | |
214 | * | |
215 | * a. Rudimentary object: | |
216 | * | |
217 | * { m_free(), m_freem_list(), composite object deallocation } | |
218 | * | ^ | |
219 | * | | | |
220 | * | +------ (done) ---------+ | |
221 | * v | | |
222 | * mcache_free/mcache_free_ext() | | |
223 | * | | | |
224 | * v | | |
225 | * mbuf_slab_audit() | | |
226 | * | | | |
227 | * v | | |
228 | * [CPU cache] ---> (not purging?) -----+ | |
229 | * | | | |
230 | * v | | |
231 | * mbuf_slab_free() | | |
232 | * | | | |
233 | * v | | |
234 | * [freelist] ----------->>------------+ | |
3e170ce0 | 235 | * (objects get purged to VM only on demand) |
2d21ac55 A |
236 | * |
237 | * b. Composite object: | |
238 | * | |
239 | * { m_free(), m_freem_list() } | |
240 | * | ^ | |
241 | * | | | |
242 | * | +------ (done) ---------+ | |
243 | * v | | |
244 | * mcache_free/mcache_free_ext() | | |
245 | * | | | |
246 | * v | | |
247 | * mbuf_cslab_audit() | | |
248 | * | | | |
249 | * v | | |
250 | * [CPU cache] ---> (not purging?) -----+ | |
251 | * | | | |
252 | * v | | |
253 | * mbuf_cslab_free() | | |
254 | * | | | |
255 | * v | | |
256 | * [freelist] ---> (not purging?) -----+ | |
257 | * | | | |
258 | * v | | |
259 | * (rudimentary object) | | |
260 | * mcache_free/mcache_free_ext() ------->>------+ | |
261 | * | |
262 | * Auditing notes: If auditing is enabled, the audit routine will save | |
263 | * any constructed data structure fields (if necessary) before filling the | |
264 | * contents of the buffers with DEADBEEF (free) pattern and recording the | |
265 | * transaction. Buffers that are freed (whether at CPU or slab layer) are | |
266 | * expected to contain the free pattern. | |
267 | * | |
268 | * DEBUGGING: | |
269 | * | |
270 | * Debugging can be enabled by adding "mbuf_debug=0x3" to boot-args; this | |
271 | * translates to the mcache flags (MCF_VERIFY | MCF_AUDIT). Additionally, | |
272 | * the CPU layer cache can be disabled by setting the MCF_NOCPUCACHE flag, | |
6d2010ae A |
273 | * i.e. modify the boot argument parameter to "mbuf_debug=0x13". Leak |
274 | * detection may also be disabled by setting the MCF_NOLEAKLOG flag, e.g. | |
275 | * "mbuf_debug=0x113". Note that debugging consumes more CPU and memory. | |
2d21ac55 A |
276 | * |
277 | * Each object is associated with exactly one mcache_audit_t structure that | |
278 | * contains the information related to its last buffer transaction. Given | |
279 | * an address of an object, the audit structure can be retrieved by finding | |
280 | * the position of the object relevant to the base address of the cluster: | |
281 | * | |
282 | * +------------+ +=============+ | |
283 | * | mbuf addr | | mclaudit[i] | | |
284 | * +------------+ +=============+ | |
285 | * | | cl_audit[0] | | |
6d2010ae | 286 | * i = MTOBG(addr) +-------------+ |
2d21ac55 | 287 | * | +-----> | cl_audit[1] | -----> mcache_audit_t |
6d2010ae | 288 | * b = BGTOM(i) | +-------------+ |
2d21ac55 A |
289 | * | | | ... | |
290 | * x = MCLIDX(b, addr) | +-------------+ | |
291 | * | | | cl_audit[7] | | |
292 | * +-----------------+ +-------------+ | |
293 | * (e.g. x == 1) | |
294 | * | |
295 | * The mclaudit[] array is allocated at initialization time, but its contents | |
6d2010ae | 296 | * get populated when the corresponding cluster is created. Because a page |
3e170ce0 | 297 | * can be turned into NMBPG number of mbufs, we preserve enough space for the |
6d2010ae | 298 | * mbufs so that there is a 1-to-1 mapping between them. A page that never |
2d21ac55 | 299 | * gets (or has not yet) turned into mbufs will use only cl_audit[0] with the |
6d2010ae A |
300 | * remaining entries unused. For 16KB cluster, only one entry from the first |
301 | * page is allocated and used for the entire object. | |
2d21ac55 | 302 | */ |
91447636 | 303 | |
2d21ac55 A |
304 | /* TODO: should be in header file */ |
305 | /* kernel translater */ | |
cc8bc92a | 306 | extern vm_offset_t kmem_mb_alloc(vm_map_t, int, int, kern_return_t *); |
2d21ac55 | 307 | extern ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va); |
0a7de745 | 308 | extern vm_map_t mb_map; /* special map */ |
2d21ac55 | 309 | |
cc8bc92a A |
310 | static uint32_t mb_kmem_contig_failed; |
311 | static uint32_t mb_kmem_failed; | |
312 | static uint32_t mb_kmem_one_failed; | |
313 | /* Timestamp of allocation failures. */ | |
314 | static uint64_t mb_kmem_contig_failed_ts; | |
315 | static uint64_t mb_kmem_failed_ts; | |
316 | static uint64_t mb_kmem_one_failed_ts; | |
317 | static uint64_t mb_kmem_contig_failed_size; | |
318 | static uint64_t mb_kmem_failed_size; | |
319 | static uint32_t mb_kmem_stats[6]; | |
320 | static const char *mb_kmem_stats_labels[] = { "INVALID_ARGUMENT", | |
0a7de745 A |
321 | "INVALID_ADDRESS", |
322 | "RESOURCE_SHORTAGE", | |
323 | "NO_SPACE", | |
324 | "KERN_FAILURE", | |
325 | "OTHERS" }; | |
cc8bc92a | 326 | |
2d21ac55 | 327 | /* Global lock */ |
316670eb A |
328 | decl_lck_mtx_data(static, mbuf_mlock_data); |
329 | static lck_mtx_t *mbuf_mlock = &mbuf_mlock_data; | |
2d21ac55 A |
330 | static lck_attr_t *mbuf_mlock_attr; |
331 | static lck_grp_t *mbuf_mlock_grp; | |
332 | static lck_grp_attr_t *mbuf_mlock_grp_attr; | |
333 | ||
334 | /* Back-end (common) layer */ | |
cc8bc92a A |
335 | static uint64_t mb_expand_cnt; |
336 | static uint64_t mb_expand_cl_cnt; | |
337 | static uint64_t mb_expand_cl_total; | |
338 | static uint64_t mb_expand_bigcl_cnt; | |
339 | static uint64_t mb_expand_bigcl_total; | |
340 | static uint64_t mb_expand_16kcl_cnt; | |
341 | static uint64_t mb_expand_16kcl_total; | |
39037602 | 342 | static boolean_t mbuf_worker_needs_wakeup; /* wait channel for mbuf worker */ |
cc8bc92a A |
343 | static uint32_t mbuf_worker_run_cnt; |
344 | static uint64_t mbuf_worker_last_runtime; | |
d9a64523 | 345 | static uint64_t mbuf_drain_last_runtime; |
0a7de745 | 346 | static int mbuf_worker_ready; /* worker thread is runnable */ |
f427ee49 | 347 | static unsigned int ncpu; /* number of CPUs */ |
0a7de745 A |
348 | static ppnum_t *mcl_paddr; /* Array of cluster physical addresses */ |
349 | static ppnum_t mcl_pages; /* Size of array (# physical pages) */ | |
350 | static ppnum_t mcl_paddr_base; /* Handle returned by IOMapper::iovmAlloc() */ | |
351 | static mcache_t *ref_cache; /* Cache of cluster reference & flags */ | |
2d21ac55 | 352 | static mcache_t *mcl_audit_con_cache; /* Audit contents cache */ |
0a7de745 | 353 | static unsigned int mbuf_debug; /* patchable mbuf mcache flags */ |
2d21ac55 | 354 | static unsigned int mb_normalized; /* number of packets "normalized" */ |
b0d623f7 | 355 | |
0a7de745 A |
356 | #define MB_GROWTH_AGGRESSIVE 1 /* Threshold: 1/2 of total */ |
357 | #define MB_GROWTH_NORMAL 2 /* Threshold: 3/4 of total */ | |
2d21ac55 A |
358 | |
359 | typedef enum { | |
0a7de745 A |
360 | MC_MBUF = 0, /* Regular mbuf */ |
361 | MC_CL, /* Cluster */ | |
362 | MC_BIGCL, /* Large (4KB) cluster */ | |
363 | MC_16KCL, /* Jumbo (16KB) cluster */ | |
364 | MC_MBUF_CL, /* mbuf + cluster */ | |
365 | MC_MBUF_BIGCL, /* mbuf + large (4KB) cluster */ | |
366 | MC_MBUF_16KCL /* mbuf + jumbo (16KB) cluster */ | |
2d21ac55 A |
367 | } mbuf_class_t; |
368 | ||
0a7de745 A |
369 | #define MBUF_CLASS_MIN MC_MBUF |
370 | #define MBUF_CLASS_MAX MC_MBUF_16KCL | |
371 | #define MBUF_CLASS_LAST MC_16KCL | |
372 | #define MBUF_CLASS_VALID(c) \ | |
2d21ac55 | 373 | ((int)(c) >= MBUF_CLASS_MIN && (int)(c) <= MBUF_CLASS_MAX) |
0a7de745 | 374 | #define MBUF_CLASS_COMPOSITE(c) \ |
2d21ac55 | 375 | ((int)(c) > MBUF_CLASS_LAST) |
91447636 | 376 | |
9bccf70c | 377 | |
2d21ac55 A |
378 | /* |
379 | * mbuf specific mcache allocation request flags. | |
380 | */ | |
0a7de745 | 381 | #define MCR_COMP MCR_USR1 /* for MC_MBUF_{CL,BIGCL,16KCL} caches */ |
9bccf70c | 382 | |
2d21ac55 A |
383 | /* |
384 | * Per-cluster slab structure. | |
385 | * | |
386 | * A slab is a cluster control structure that contains one or more object | |
387 | * chunks; the available chunks are chained in the slab's freelist (sl_head). | |
388 | * Each time a chunk is taken out of the slab, the slab's reference count | |
389 | * gets incremented. When all chunks have been taken out, the empty slab | |
390 | * gets removed (SLF_DETACHED) from the class's slab list. A chunk that is | |
391 | * returned to a slab causes the slab's reference count to be decremented; | |
392 | * it also causes the slab to be reinserted back to class's slab list, if | |
393 | * it's not already done. | |
394 | * | |
395 | * Compartmentalizing of the object chunks into slabs allows us to easily | |
396 | * merge one or more slabs together when the adjacent slabs are idle, as | |
397 | * well as to convert or move a slab from one class to another; e.g. the | |
398 | * mbuf cluster slab can be converted to a regular cluster slab when all | |
399 | * mbufs in the slab have been freed. | |
400 | * | |
401 | * A slab may also span across multiple clusters for chunks larger than | |
402 | * a cluster's size. In this case, only the slab of the first cluster is | |
403 | * used. The rest of the slabs are marked with SLF_PARTIAL to indicate | |
404 | * that they are part of the larger slab. | |
6d2010ae A |
405 | * |
406 | * Each slab controls a page of memory. | |
2d21ac55 A |
407 | */ |
408 | typedef struct mcl_slab { | |
0a7de745 A |
409 | struct mcl_slab *sl_next; /* neighboring slab */ |
410 | u_int8_t sl_class; /* controlling mbuf class */ | |
411 | int8_t sl_refcnt; /* outstanding allocations */ | |
412 | int8_t sl_chunks; /* chunks (bufs) in this slab */ | |
413 | u_int16_t sl_flags; /* slab flags (see below) */ | |
414 | u_int16_t sl_len; /* slab length */ | |
415 | void *sl_base; /* base of allocated memory */ | |
416 | void *sl_head; /* first free buffer */ | |
417 | TAILQ_ENTRY(mcl_slab) sl_link; /* next/prev slab on freelist */ | |
2d21ac55 A |
418 | } mcl_slab_t; |
419 | ||
0a7de745 A |
420 | #define SLF_MAPPED 0x0001 /* backed by a mapped page */ |
421 | #define SLF_PARTIAL 0x0002 /* part of another slab */ | |
422 | #define SLF_DETACHED 0x0004 /* not in slab freelist */ | |
1c79356b | 423 | |
2d21ac55 A |
424 | /* |
425 | * The array of slabs are broken into groups of arrays per 1MB of kernel | |
426 | * memory to reduce the footprint. Each group is allocated on demand | |
427 | * whenever a new piece of memory mapped in from the VM crosses the 1MB | |
428 | * boundary. | |
429 | */ | |
0a7de745 | 430 | #define NSLABSPMB ((1 << MBSHIFT) >> PAGE_SHIFT) |
91447636 | 431 | |
2d21ac55 | 432 | typedef struct mcl_slabg { |
0a7de745 | 433 | mcl_slab_t *slg_slab; /* group of slabs */ |
2d21ac55 | 434 | } mcl_slabg_t; |
1c79356b | 435 | |
6d2010ae A |
436 | /* |
437 | * Number of slabs needed to control a 16KB cluster object. | |
438 | */ | |
0a7de745 | 439 | #define NSLABSP16KB (M16KCLBYTES >> PAGE_SHIFT) |
6d2010ae | 440 | |
2d21ac55 A |
441 | /* |
442 | * Per-cluster audit structure. | |
443 | */ | |
444 | typedef struct { | |
0a7de745 | 445 | mcache_audit_t **cl_audit; /* array of audits */ |
2d21ac55 | 446 | } mcl_audit_t; |
91447636 | 447 | |
39236c6e | 448 | typedef struct { |
0a7de745 A |
449 | struct thread *msa_thread; /* thread doing transaction */ |
450 | struct thread *msa_pthread; /* previous transaction thread */ | |
451 | uint32_t msa_tstamp; /* transaction timestamp (ms) */ | |
452 | uint32_t msa_ptstamp; /* prev transaction timestamp (ms) */ | |
453 | uint16_t msa_depth; /* pc stack depth */ | |
454 | uint16_t msa_pdepth; /* previous transaction pc stack */ | |
455 | void *msa_stack[MCACHE_STACK_DEPTH]; | |
456 | void *msa_pstack[MCACHE_STACK_DEPTH]; | |
39236c6e A |
457 | } mcl_scratch_audit_t; |
458 | ||
459 | typedef struct { | |
460 | /* | |
461 | * Size of data from the beginning of an mbuf that covers m_hdr, | |
462 | * pkthdr and m_ext structures. If auditing is enabled, we allocate | |
463 | * a shadow mbuf structure of this size inside each audit structure, | |
464 | * and the contents of the real mbuf gets copied into it when the mbuf | |
465 | * is freed. This allows us to pattern-fill the mbuf for integrity | |
466 | * check, and to preserve any constructed mbuf fields (e.g. mbuf + | |
467 | * cluster cache case). Note that we don't save the contents of | |
468 | * clusters when they are freed; we simply pattern-fill them. | |
469 | */ | |
0a7de745 A |
470 | u_int8_t sc_mbuf[(MSIZE - _MHLEN) + sizeof(_m_ext_t)]; |
471 | mcl_scratch_audit_t sc_scratch __attribute__((aligned(8))); | |
39236c6e A |
472 | } mcl_saved_contents_t; |
473 | ||
0a7de745 | 474 | #define AUDIT_CONTENTS_SIZE (sizeof (mcl_saved_contents_t)) |
39236c6e | 475 | |
0a7de745 A |
476 | #define MCA_SAVED_MBUF_PTR(_mca) \ |
477 | ((struct mbuf *)(void *)((mcl_saved_contents_t *) \ | |
39236c6e | 478 | (_mca)->mca_contents)->sc_mbuf) |
0a7de745 | 479 | #define MCA_SAVED_MBUF_SIZE \ |
39236c6e | 480 | (sizeof (((mcl_saved_contents_t *)0)->sc_mbuf)) |
0a7de745 | 481 | #define MCA_SAVED_SCRATCH_PTR(_mca) \ |
39236c6e | 482 | (&((mcl_saved_contents_t *)(_mca)->mca_contents)->sc_scratch) |
fa4905b1 | 483 | |
2d21ac55 A |
484 | /* |
485 | * mbuf specific mcache audit flags | |
486 | */ | |
0a7de745 A |
487 | #define MB_INUSE 0x01 /* object has not been returned to slab */ |
488 | #define MB_COMP_INUSE 0x02 /* object has not been returned to cslab */ | |
489 | #define MB_SCVALID 0x04 /* object has valid saved contents */ | |
fa4905b1 | 490 | |
2d21ac55 A |
491 | /* |
492 | * Each of the following two arrays hold up to nmbclusters elements. | |
493 | */ | |
0a7de745 A |
494 | static mcl_audit_t *mclaudit; /* array of cluster audit information */ |
495 | static unsigned int maxclaudit; /* max # of entries in audit table */ | |
496 | static mcl_slabg_t **slabstbl; /* cluster slabs table */ | |
497 | static unsigned int maxslabgrp; /* max # of entries in slabs table */ | |
498 | static unsigned int slabgrp; /* # of entries in slabs table */ | |
2d21ac55 A |
499 | |
500 | /* Globals */ | |
0a7de745 A |
501 | int nclusters; /* # of clusters for non-jumbo (legacy) sizes */ |
502 | int njcl; /* # of clusters for jumbo sizes */ | |
503 | int njclbytes; /* size of a jumbo cluster */ | |
504 | unsigned char *mbutl; /* first mapped cluster address */ | |
505 | unsigned char *embutl; /* ending virtual address of mclusters */ | |
506 | int _max_linkhdr; /* largest link-level header */ | |
507 | int _max_protohdr; /* largest protocol header */ | |
508 | int max_hdr; /* largest link+protocol header */ | |
509 | int max_datalen; /* MHLEN - max_hdr */ | |
510 | ||
511 | static boolean_t mclverify; /* debug: pattern-checking */ | |
512 | static boolean_t mcltrace; /* debug: stack tracing */ | |
513 | static boolean_t mclfindleak; /* debug: leak detection */ | |
514 | static boolean_t mclexpleak; /* debug: expose leak info to user space */ | |
515 | ||
516 | static struct timeval mb_start; /* beginning of time */ | |
39236c6e | 517 | |
6d2010ae A |
518 | /* mbuf leak detection variables */ |
519 | static struct mleak_table mleak_table; | |
520 | static mleak_stat_t *mleak_stat; | |
521 | ||
0a7de745 | 522 | #define MLEAK_STAT_SIZE(n) \ |
5ba3f43e | 523 | __builtin_offsetof(mleak_stat_t, ml_trace[n]) |
6d2010ae A |
524 | |
525 | struct mallocation { | |
0a7de745 A |
526 | mcache_obj_t *element; /* the alloc'ed element, NULL if unused */ |
527 | u_int32_t trace_index; /* mtrace index for corresponding backtrace */ | |
528 | u_int32_t count; /* How many objects were requested */ | |
529 | u_int64_t hitcount; /* for determining hash effectiveness */ | |
6d2010ae A |
530 | }; |
531 | ||
532 | struct mtrace { | |
0a7de745 A |
533 | u_int64_t collisions; |
534 | u_int64_t hitcount; | |
535 | u_int64_t allocs; | |
536 | u_int64_t depth; | |
537 | uintptr_t addr[MLEAK_STACK_DEPTH]; | |
6d2010ae A |
538 | }; |
539 | ||
540 | /* Size must be a power of two for the zhash to be able to just mask off bits */ | |
0a7de745 A |
541 | #define MLEAK_ALLOCATION_MAP_NUM 512 |
542 | #define MLEAK_TRACE_MAP_NUM 256 | |
6d2010ae A |
543 | |
544 | /* | |
545 | * Sample factor for how often to record a trace. This is overwritable | |
546 | * by the boot-arg mleak_sample_factor. | |
547 | */ | |
0a7de745 | 548 | #define MLEAK_SAMPLE_FACTOR 500 |
6d2010ae A |
549 | |
550 | /* | |
551 | * Number of top leakers recorded. | |
552 | */ | |
0a7de745 | 553 | #define MLEAK_NUM_TRACES 5 |
6d2010ae | 554 | |
0a7de745 | 555 | #define MB_LEAK_SPACING_64 " " |
316670eb A |
556 | #define MB_LEAK_SPACING_32 " " |
557 | ||
558 | ||
0a7de745 | 559 | #define MB_LEAK_HDR_32 "\n\ |
316670eb A |
560 | trace [1] trace [2] trace [3] trace [4] trace [5] \n\ |
561 | ---------- ---------- ---------- ---------- ---------- \n\ | |
562 | " | |
563 | ||
0a7de745 | 564 | #define MB_LEAK_HDR_64 "\n\ |
316670eb | 565 | trace [1] trace [2] trace [3] \ |
0a7de745 | 566 | trace [4] trace [5] \n\ |
316670eb A |
567 | ------------------ ------------------ ------------------ \ |
568 | ------------------ ------------------ \n\ | |
569 | " | |
570 | ||
6d2010ae A |
571 | static uint32_t mleak_alloc_buckets = MLEAK_ALLOCATION_MAP_NUM; |
572 | static uint32_t mleak_trace_buckets = MLEAK_TRACE_MAP_NUM; | |
573 | ||
574 | /* Hashmaps of allocations and their corresponding traces */ | |
575 | static struct mallocation *mleak_allocations; | |
576 | static struct mtrace *mleak_traces; | |
577 | static struct mtrace *mleak_top_trace[MLEAK_NUM_TRACES]; | |
578 | ||
579 | /* Lock to protect mleak tables from concurrent modification */ | |
316670eb A |
580 | decl_lck_mtx_data(static, mleak_lock_data); |
581 | static lck_mtx_t *mleak_lock = &mleak_lock_data; | |
6d2010ae A |
582 | static lck_attr_t *mleak_lock_attr; |
583 | static lck_grp_t *mleak_lock_grp; | |
584 | static lck_grp_attr_t *mleak_lock_grp_attr; | |
585 | ||
5c9f4661 A |
586 | /* *Failed* large allocations. */ |
587 | struct mtracelarge { | |
0a7de745 A |
588 | uint64_t size; |
589 | uint64_t depth; | |
590 | uintptr_t addr[MLEAK_STACK_DEPTH]; | |
5c9f4661 A |
591 | }; |
592 | ||
0a7de745 | 593 | #define MTRACELARGE_NUM_TRACES 5 |
5c9f4661 A |
594 | static struct mtracelarge mtracelarge_table[MTRACELARGE_NUM_TRACES]; |
595 | ||
596 | static void mtracelarge_register(size_t size); | |
597 | ||
39037602 A |
598 | /* Lock to protect the completion callback table */ |
599 | static lck_grp_attr_t *mbuf_tx_compl_tbl_lck_grp_attr = NULL; | |
600 | static lck_attr_t *mbuf_tx_compl_tbl_lck_attr = NULL; | |
601 | static lck_grp_t *mbuf_tx_compl_tbl_lck_grp = NULL; | |
602 | decl_lck_rw_data(, mbuf_tx_compl_tbl_lck_rw_data); | |
603 | lck_rw_t *mbuf_tx_compl_tbl_lock = &mbuf_tx_compl_tbl_lck_rw_data; | |
604 | ||
b0d623f7 A |
605 | extern u_int32_t high_sb_max; |
606 | ||
2d21ac55 | 607 | /* The minimum number of objects that are allocated, to start. */ |
0a7de745 A |
608 | #define MINCL 32 |
609 | #define MINBIGCL (MINCL >> 1) | |
610 | #define MIN16KCL (MINCL >> 2) | |
2d21ac55 A |
611 | |
612 | /* Low watermarks (only map in pages once free counts go below) */ | |
0a7de745 A |
613 | #define MBIGCL_LOWAT MINBIGCL |
614 | #define M16KCL_LOWAT MIN16KCL | |
2d21ac55 A |
615 | |
616 | typedef struct { | |
0a7de745 A |
617 | mbuf_class_t mtbl_class; /* class type */ |
618 | mcache_t *mtbl_cache; /* mcache for this buffer class */ | |
2d21ac55 | 619 | TAILQ_HEAD(mcl_slhead, mcl_slab) mtbl_slablist; /* slab list */ |
0a7de745 A |
620 | mcache_obj_t *mtbl_cobjlist; /* composite objects freelist */ |
621 | mb_class_stat_t *mtbl_stats; /* statistics fetchable via sysctl */ | |
622 | u_int32_t mtbl_maxsize; /* maximum buffer size */ | |
623 | int mtbl_minlimit; /* minimum allowed */ | |
624 | int mtbl_maxlimit; /* maximum allowed */ | |
625 | u_int32_t mtbl_wantpurge; /* purge during next reclaim */ | |
626 | uint32_t mtbl_avgtotal; /* average total on iOS */ | |
627 | u_int32_t mtbl_expand; /* worker should expand the class */ | |
2d21ac55 A |
628 | } mbuf_table_t; |
629 | ||
0a7de745 A |
630 | #define m_class(c) mbuf_table[c].mtbl_class |
631 | #define m_cache(c) mbuf_table[c].mtbl_cache | |
632 | #define m_slablist(c) mbuf_table[c].mtbl_slablist | |
633 | #define m_cobjlist(c) mbuf_table[c].mtbl_cobjlist | |
634 | #define m_maxsize(c) mbuf_table[c].mtbl_maxsize | |
635 | #define m_minlimit(c) mbuf_table[c].mtbl_minlimit | |
636 | #define m_maxlimit(c) mbuf_table[c].mtbl_maxlimit | |
637 | #define m_wantpurge(c) mbuf_table[c].mtbl_wantpurge | |
638 | #define m_cname(c) mbuf_table[c].mtbl_stats->mbcl_cname | |
639 | #define m_size(c) mbuf_table[c].mtbl_stats->mbcl_size | |
640 | #define m_total(c) mbuf_table[c].mtbl_stats->mbcl_total | |
641 | #define m_active(c) mbuf_table[c].mtbl_stats->mbcl_active | |
642 | #define m_infree(c) mbuf_table[c].mtbl_stats->mbcl_infree | |
643 | #define m_slab_cnt(c) mbuf_table[c].mtbl_stats->mbcl_slab_cnt | |
644 | #define m_alloc_cnt(c) mbuf_table[c].mtbl_stats->mbcl_alloc_cnt | |
645 | #define m_free_cnt(c) mbuf_table[c].mtbl_stats->mbcl_free_cnt | |
646 | #define m_notified(c) mbuf_table[c].mtbl_stats->mbcl_notified | |
647 | #define m_purge_cnt(c) mbuf_table[c].mtbl_stats->mbcl_purge_cnt | |
648 | #define m_fail_cnt(c) mbuf_table[c].mtbl_stats->mbcl_fail_cnt | |
649 | #define m_ctotal(c) mbuf_table[c].mtbl_stats->mbcl_ctotal | |
650 | #define m_peak(c) mbuf_table[c].mtbl_stats->mbcl_peak_reported | |
651 | #define m_release_cnt(c) mbuf_table[c].mtbl_stats->mbcl_release_cnt | |
652 | #define m_region_expand(c) mbuf_table[c].mtbl_expand | |
2d21ac55 A |
653 | |
654 | static mbuf_table_t mbuf_table[] = { | |
655 | /* | |
656 | * The caches for mbufs, regular clusters and big clusters. | |
fe8ab488 A |
657 | * The average total values were based on data gathered by actual |
658 | * usage patterns on iOS. | |
2d21ac55 A |
659 | */ |
660 | { MC_MBUF, NULL, TAILQ_HEAD_INITIALIZER(m_slablist(MC_MBUF)), | |
0a7de745 | 661 | NULL, NULL, 0, 0, 0, 0, 3000, 0 }, |
2d21ac55 | 662 | { MC_CL, NULL, TAILQ_HEAD_INITIALIZER(m_slablist(MC_CL)), |
0a7de745 | 663 | NULL, NULL, 0, 0, 0, 0, 2000, 0 }, |
2d21ac55 | 664 | { MC_BIGCL, NULL, TAILQ_HEAD_INITIALIZER(m_slablist(MC_BIGCL)), |
0a7de745 | 665 | NULL, NULL, 0, 0, 0, 0, 1000, 0 }, |
2d21ac55 | 666 | { MC_16KCL, NULL, TAILQ_HEAD_INITIALIZER(m_slablist(MC_16KCL)), |
0a7de745 | 667 | NULL, NULL, 0, 0, 0, 0, 200, 0 }, |
2d21ac55 A |
668 | /* |
669 | * The following are special caches; they serve as intermediate | |
670 | * caches backed by the above rudimentary caches. Each object | |
671 | * in the cache is an mbuf with a cluster attached to it. Unlike | |
672 | * the above caches, these intermediate caches do not directly | |
673 | * deal with the slab structures; instead, the constructed | |
674 | * cached elements are simply stored in the freelists. | |
675 | */ | |
5ba3f43e A |
676 | { MC_MBUF_CL, NULL, { NULL, NULL }, NULL, NULL, 0, 0, 0, 0, 2000, 0 }, |
677 | { MC_MBUF_BIGCL, NULL, { NULL, NULL }, NULL, NULL, 0, 0, 0, 0, 1000, 0 }, | |
678 | { MC_MBUF_16KCL, NULL, { NULL, NULL }, NULL, NULL, 0, 0, 0, 0, 200, 0 }, | |
2d21ac55 A |
679 | }; |
680 | ||
0a7de745 | 681 | #define NELEM(a) (sizeof (a) / sizeof ((a)[0])) |
2d21ac55 | 682 | |
a39ff7e2 A |
683 | |
684 | static uint32_t | |
685 | m_avgtotal(mbuf_class_t c) | |
686 | { | |
0a7de745 | 687 | return mbuf_table[c].mtbl_avgtotal; |
a39ff7e2 A |
688 | } |
689 | ||
0a7de745 A |
690 | static void *mb_waitchan = &mbuf_table; /* wait channel for all caches */ |
691 | static int mb_waiters; /* number of waiters */ | |
6d2010ae | 692 | |
fe8ab488 A |
693 | boolean_t mb_peak_newreport = FALSE; |
694 | boolean_t mb_peak_firstreport = FALSE; | |
695 | ||
696 | /* generate a report by default after 1 week of uptime */ | |
0a7de745 | 697 | #define MBUF_PEAK_FIRST_REPORT_THRESHOLD 604800 |
fe8ab488 | 698 | |
0a7de745 A |
699 | #define MB_WDT_MAXTIME 10 /* # of secs before watchdog panic */ |
700 | static struct timeval mb_wdtstart; /* watchdog start timestamp */ | |
316670eb A |
701 | static char *mbuf_dump_buf; |
702 | ||
0a7de745 | 703 | #define MBUF_DUMP_BUF_SIZE 4096 |
6d2010ae A |
704 | |
705 | /* | |
0a7de745 A |
706 | * mbuf watchdog is enabled by default. It is also toggeable via the |
707 | * kern.ipc.mb_watchdog sysctl. | |
708 | * Garbage collection is enabled by default on embedded platforms. | |
fe8ab488 | 709 | * mb_drain_maxint controls the amount of time to wait (in seconds) before |
d9a64523 | 710 | * consecutive calls to mbuf_drain(). |
6d2010ae | 711 | */ |
f427ee49 | 712 | #if !XNU_TARGET_OS_OSX || DEVELOPMENT || DEBUG |
5ba3f43e | 713 | static unsigned int mb_watchdog = 1; |
f427ee49 | 714 | #else /* XNU_TARGET_OS_OSX && !DEVELOPMENT && !DEBUG */ |
6d2010ae | 715 | static unsigned int mb_watchdog = 0; |
f427ee49 A |
716 | #endif /* XNU_TARGET_OS_OSX && !DEVELOPMENT && !DEBUG */ |
717 | #if !XNU_TARGET_OS_OSX | |
0a7de745 | 718 | static unsigned int mb_drain_maxint = 60; |
f427ee49 | 719 | #else /* XNU_TARGET_OS_OSX */ |
fe8ab488 | 720 | static unsigned int mb_drain_maxint = 0; |
f427ee49 | 721 | #endif /* XNU_TARGET_OS_OSX */ |
2a1bd2d3 | 722 | static unsigned int mb_memory_pressure_percentage = 80; |
39236c6e | 723 | |
813fb2f6 A |
724 | uintptr_t mb_obscure_extfree __attribute__((visibility("hidden"))); |
725 | uintptr_t mb_obscure_extref __attribute__((visibility("hidden"))); | |
726 | ||
39236c6e A |
727 | /* Red zone */ |
728 | static u_int32_t mb_redzone_cookie; | |
729 | static void m_redzone_init(struct mbuf *); | |
730 | static void m_redzone_verify(struct mbuf *m); | |
2d21ac55 A |
731 | |
732 | /* The following are used to serialize m_clalloc() */ | |
733 | static boolean_t mb_clalloc_busy; | |
734 | static void *mb_clalloc_waitchan = &mb_clalloc_busy; | |
735 | static int mb_clalloc_waiters; | |
736 | ||
6d2010ae | 737 | static void mbuf_mtypes_sync(boolean_t); |
2d21ac55 | 738 | static int mbstat_sysctl SYSCTL_HANDLER_ARGS; |
6d2010ae | 739 | static void mbuf_stat_sync(void); |
2d21ac55 | 740 | static int mb_stat_sysctl SYSCTL_HANDLER_ARGS; |
6d2010ae A |
741 | static int mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS; |
742 | static int mleak_table_sysctl SYSCTL_HANDLER_ARGS; | |
743 | static char *mbuf_dump(void); | |
2d21ac55 A |
744 | static void mbuf_table_init(void); |
745 | static inline void m_incref(struct mbuf *); | |
39037602 | 746 | static inline u_int16_t m_decref(struct mbuf *); |
2d21ac55 A |
747 | static int m_clalloc(const u_int32_t, const int, const u_int32_t); |
748 | static void mbuf_worker_thread_init(void); | |
749 | static mcache_obj_t *slab_alloc(mbuf_class_t, int); | |
750 | static void slab_free(mbuf_class_t, mcache_obj_t *); | |
751 | static unsigned int mbuf_slab_alloc(void *, mcache_obj_t ***, | |
752 | unsigned int, int); | |
753 | static void mbuf_slab_free(void *, mcache_obj_t *, int); | |
754 | static void mbuf_slab_audit(void *, mcache_obj_t *, boolean_t); | |
755 | static void mbuf_slab_notify(void *, u_int32_t); | |
756 | static unsigned int cslab_alloc(mbuf_class_t, mcache_obj_t ***, | |
757 | unsigned int); | |
758 | static unsigned int cslab_free(mbuf_class_t, mcache_obj_t *, int); | |
759 | static unsigned int mbuf_cslab_alloc(void *, mcache_obj_t ***, | |
760 | unsigned int, int); | |
761 | static void mbuf_cslab_free(void *, mcache_obj_t *, int); | |
762 | static void mbuf_cslab_audit(void *, mcache_obj_t *, boolean_t); | |
763 | static int freelist_populate(mbuf_class_t, unsigned int, int); | |
6d2010ae | 764 | static void freelist_init(mbuf_class_t); |
2d21ac55 A |
765 | static boolean_t mbuf_cached_above(mbuf_class_t, int); |
766 | static boolean_t mbuf_steal(mbuf_class_t, unsigned int); | |
767 | static void m_reclaim(mbuf_class_t, unsigned int, boolean_t); | |
768 | static int m_howmany(int, size_t); | |
769 | static void mbuf_worker_thread(void); | |
6d2010ae | 770 | static void mbuf_watchdog(void); |
2d21ac55 A |
771 | static boolean_t mbuf_sleep(mbuf_class_t, unsigned int, int); |
772 | ||
773 | static void mcl_audit_init(void *, mcache_audit_t **, mcache_obj_t **, | |
774 | size_t, unsigned int); | |
fe8ab488 | 775 | static void mcl_audit_free(void *, unsigned int); |
2d21ac55 A |
776 | static mcache_audit_t *mcl_audit_buf2mca(mbuf_class_t, mcache_obj_t *); |
777 | static void mcl_audit_mbuf(mcache_audit_t *, void *, boolean_t, boolean_t); | |
778 | static void mcl_audit_cluster(mcache_audit_t *, void *, size_t, boolean_t, | |
779 | boolean_t); | |
780 | static void mcl_audit_restore_mbuf(struct mbuf *, mcache_audit_t *, boolean_t); | |
781 | static void mcl_audit_save_mbuf(struct mbuf *, mcache_audit_t *); | |
39236c6e | 782 | static void mcl_audit_scratch(mcache_audit_t *); |
2d21ac55 A |
783 | static void mcl_audit_mcheck_panic(struct mbuf *); |
784 | static void mcl_audit_verify_nextptr(void *, mcache_audit_t *); | |
785 | ||
6d2010ae A |
786 | static void mleak_activate(void); |
787 | static void mleak_logger(u_int32_t, mcache_obj_t *, boolean_t); | |
788 | static boolean_t mleak_log(uintptr_t *, mcache_obj_t *, uint32_t, int); | |
789 | static void mleak_free(mcache_obj_t *); | |
316670eb A |
790 | static void mleak_sort_traces(void); |
791 | static void mleak_update_stats(void); | |
6d2010ae | 792 | |
2d21ac55 A |
793 | static mcl_slab_t *slab_get(void *); |
794 | static void slab_init(mcl_slab_t *, mbuf_class_t, u_int32_t, | |
795 | void *, void *, unsigned int, int, int); | |
796 | static void slab_insert(mcl_slab_t *, mbuf_class_t); | |
797 | static void slab_remove(mcl_slab_t *, mbuf_class_t); | |
798 | static boolean_t slab_inrange(mcl_slab_t *, void *); | |
799 | static void slab_nextptr_panic(mcl_slab_t *, void *); | |
800 | static void slab_detach(mcl_slab_t *); | |
801 | static boolean_t slab_is_detached(mcl_slab_t *); | |
802 | ||
b0d623f7 A |
803 | static int m_copyback0(struct mbuf **, int, int, const void *, int, int); |
804 | static struct mbuf *m_split0(struct mbuf *, int, int, int); | |
fe8ab488 A |
805 | __private_extern__ void mbuf_report_peak_usage(void); |
806 | static boolean_t mbuf_report_usage(mbuf_class_t); | |
d9a64523 A |
807 | #if DEBUG || DEVELOPMENT |
808 | #define mbwdog_logger(fmt, ...) _mbwdog_logger(__func__, __LINE__, fmt, ## __VA_ARGS__) | |
809 | static void _mbwdog_logger(const char *func, const int line, const char *fmt, ...); | |
810 | static char *mbwdog_logging; | |
811 | const unsigned mbwdog_logging_size = 4096; | |
812 | static size_t mbwdog_logging_used; | |
813 | #else | |
814 | #define mbwdog_logger(fmt, ...) do { } while (0) | |
815 | #endif | |
816 | static void mbuf_drain_locked(boolean_t); | |
b0d623f7 A |
817 | |
818 | /* flags for m_copyback0 */ | |
0a7de745 A |
819 | #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */ |
820 | #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */ | |
821 | #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */ | |
822 | #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */ | |
b0d623f7 | 823 | |
2d21ac55 A |
824 | /* |
825 | * This flag is set for all mbufs that come out of and into the composite | |
826 | * mbuf + cluster caches, i.e. MC_MBUF_CL and MC_MBUF_BIGCL. mbufs that | |
827 | * are marked with such a flag have clusters attached to them, and will be | |
828 | * treated differently when they are freed; instead of being placed back | |
829 | * into the mbuf and cluster freelists, the composite mbuf + cluster objects | |
830 | * are placed back into the appropriate composite cache's freelist, and the | |
831 | * actual freeing is deferred until the composite objects are purged. At | |
832 | * such a time, this flag will be cleared from the mbufs and the objects | |
833 | * will be freed into their own separate freelists. | |
834 | */ | |
0a7de745 | 835 | #define EXTF_COMPOSITE 0x1 |
1c79356b | 836 | |
6d2010ae A |
837 | /* |
838 | * This flag indicates that the external cluster is read-only, i.e. it is | |
839 | * or was referred to by more than one mbufs. Once set, this flag is never | |
840 | * cleared. | |
841 | */ | |
0a7de745 | 842 | #define EXTF_READONLY 0x2 |
39037602 A |
843 | /* |
844 | * This flag indicates that the external cluster is paired with the mbuf. | |
845 | * Pairing implies an external free routine defined which will be invoked | |
846 | * when the reference count drops to the minimum at m_free time. This | |
847 | * flag is never cleared. | |
848 | */ | |
0a7de745 | 849 | #define EXTF_PAIRED 0x4 |
39037602 | 850 | |
0a7de745 | 851 | #define EXTF_MASK \ |
39037602 | 852 | (EXTF_COMPOSITE | EXTF_READONLY | EXTF_PAIRED) |
6d2010ae | 853 | |
0a7de745 A |
854 | #define MEXT_MINREF(m) ((m_get_rfa(m))->minref) |
855 | #define MEXT_REF(m) ((m_get_rfa(m))->refcnt) | |
856 | #define MEXT_PREF(m) ((m_get_rfa(m))->prefcnt) | |
857 | #define MEXT_FLAGS(m) ((m_get_rfa(m))->flags) | |
858 | #define MEXT_PRIV(m) ((m_get_rfa(m))->priv) | |
859 | #define MEXT_PMBUF(m) ((m_get_rfa(m))->paired) | |
860 | #define MEXT_TOKEN(m) ((m_get_rfa(m))->ext_token) | |
861 | #define MBUF_IS_COMPOSITE(m) \ | |
862 | (MEXT_REF(m) == MEXT_MINREF(m) && \ | |
39037602 A |
863 | (MEXT_FLAGS(m) & EXTF_MASK) == EXTF_COMPOSITE) |
864 | /* | |
865 | * This macro can be used to test if the mbuf is paired to an external | |
866 | * cluster. The test for MEXT_PMBUF being equal to the mbuf in subject | |
867 | * is important, as EXTF_PAIRED alone is insufficient since it is immutable, | |
868 | * and thus survives calls to m_free_paired. | |
869 | */ | |
0a7de745 A |
870 | #define MBUF_IS_PAIRED(m) \ |
871 | (((m)->m_flags & M_EXT) && \ | |
872 | (MEXT_FLAGS(m) & EXTF_MASK) == EXTF_PAIRED && \ | |
39037602 | 873 | MEXT_PMBUF(m) == (m)) |
1c79356b | 874 | |
2d21ac55 A |
875 | /* |
876 | * Macros used to verify the integrity of the mbuf. | |
877 | */ | |
0a7de745 A |
878 | #define _MCHECK(m) { \ |
879 | if ((m)->m_type != MT_FREE && !MBUF_IS_PAIRED(m)) { \ | |
880 | if (mclaudit == NULL) \ | |
881 | panic("MCHECK: m_type=%d m=%p", \ | |
882 | (u_int16_t)(m)->m_type, m); \ | |
883 | else \ | |
884 | mcl_audit_mcheck_panic(m); \ | |
885 | } \ | |
886 | } | |
887 | ||
888 | #define MBUF_IN_MAP(addr) \ | |
889 | ((unsigned char *)(addr) >= mbutl && \ | |
3e170ce0 | 890 | (unsigned char *)(addr) < embutl) |
55e303ae | 891 | |
0a7de745 A |
892 | #define MRANGE(addr) { \ |
893 | if (!MBUF_IN_MAP(addr)) \ | |
894 | panic("MRANGE: address out of range 0x%p", addr); \ | |
1c79356b A |
895 | } |
896 | ||
897 | /* | |
2d21ac55 | 898 | * Macro version of mtod. |
1c79356b | 899 | */ |
0a7de745 | 900 | #define MTOD(m, t) ((t)((m)->m_data)) |
1c79356b | 901 | |
2d21ac55 | 902 | /* |
3e170ce0 | 903 | * Macros to obtain page index given a base cluster address |
6d2010ae | 904 | */ |
0a7de745 A |
905 | #define MTOPG(x) (((unsigned char *)x - mbutl) >> PAGE_SHIFT) |
906 | #define PGTOM(x) (mbutl + (x << PAGE_SHIFT)) | |
6d2010ae A |
907 | |
908 | /* | |
909 | * Macro to find the mbuf index relative to a base. | |
2d21ac55 | 910 | */ |
0a7de745 | 911 | #define MBPAGEIDX(c, m) \ |
3e170ce0 | 912 | (((unsigned char *)(m) - (unsigned char *)(c)) >> MSIZESHIFT) |
1c79356b | 913 | |
2d21ac55 | 914 | /* |
6d2010ae | 915 | * Same thing for 2KB cluster index. |
2d21ac55 | 916 | */ |
0a7de745 | 917 | #define CLPAGEIDX(c, m) \ |
3e170ce0 A |
918 | (((unsigned char *)(m) - (unsigned char *)(c)) >> MCLSHIFT) |
919 | ||
920 | /* | |
921 | * Macro to find 4KB cluster index relative to a base | |
922 | */ | |
923 | #define BCLPAGEIDX(c, m) \ | |
924 | (((unsigned char *)(m) - (unsigned char *)(c)) >> MBIGCLSHIFT) | |
91447636 | 925 | |
2d21ac55 A |
926 | /* |
927 | * Macros used during mbuf and cluster initialization. | |
928 | */ | |
0a7de745 A |
929 | #define MBUF_INIT_PKTHDR(m) { \ |
930 | (m)->m_pkthdr.rcvif = NULL; \ | |
931 | (m)->m_pkthdr.pkt_hdr = NULL; \ | |
932 | (m)->m_pkthdr.len = 0; \ | |
933 | (m)->m_pkthdr.csum_flags = 0; \ | |
934 | (m)->m_pkthdr.csum_data = 0; \ | |
935 | (m)->m_pkthdr.vlan_tag = 0; \ | |
f427ee49 | 936 | (m)->m_pkthdr.comp_gencnt = 0; \ |
0a7de745 A |
937 | m_classifier_init(m, 0); \ |
938 | m_tag_init(m, 1); \ | |
939 | m_scratch_init(m); \ | |
940 | m_redzone_init(m); \ | |
941 | } | |
942 | ||
943 | #define MBUF_INIT(m, pkthdr, type) { \ | |
944 | _MCHECK(m); \ | |
945 | (m)->m_next = (m)->m_nextpkt = NULL; \ | |
946 | (m)->m_len = 0; \ | |
947 | (m)->m_type = type; \ | |
948 | if ((pkthdr) == 0) { \ | |
949 | (m)->m_data = (m)->m_dat; \ | |
950 | (m)->m_flags = 0; \ | |
951 | } else { \ | |
952 | (m)->m_data = (m)->m_pktdat; \ | |
953 | (m)->m_flags = M_PKTHDR; \ | |
954 | MBUF_INIT_PKTHDR(m); \ | |
955 | } \ | |
956 | } | |
957 | ||
958 | #define MEXT_INIT(m, buf, size, free, arg, rfa, min, ref, pref, flag, \ | |
959 | priv, pm) { \ | |
960 | (m)->m_data = (m)->m_ext.ext_buf = (buf); \ | |
961 | (m)->m_flags |= M_EXT; \ | |
962 | m_set_ext((m), (rfa), (free), (arg)); \ | |
963 | (m)->m_ext.ext_size = (size); \ | |
964 | MEXT_MINREF(m) = (min); \ | |
965 | MEXT_REF(m) = (ref); \ | |
966 | MEXT_PREF(m) = (pref); \ | |
967 | MEXT_FLAGS(m) = (flag); \ | |
968 | MEXT_PRIV(m) = (priv); \ | |
969 | MEXT_PMBUF(m) = (pm); \ | |
970 | } | |
971 | ||
972 | #define MBUF_CL_INIT(m, buf, rfa, ref, flag) \ | |
973 | MEXT_INIT(m, buf, m_maxsize(MC_CL), NULL, NULL, rfa, 0, \ | |
39037602 | 974 | ref, 0, flag, 0, NULL) |
2d21ac55 | 975 | |
0a7de745 A |
976 | #define MBUF_BIGCL_INIT(m, buf, rfa, ref, flag) \ |
977 | MEXT_INIT(m, buf, m_maxsize(MC_BIGCL), m_bigfree, NULL, rfa, 0, \ | |
39037602 | 978 | ref, 0, flag, 0, NULL) |
2d21ac55 | 979 | |
0a7de745 A |
980 | #define MBUF_16KCL_INIT(m, buf, rfa, ref, flag) \ |
981 | MEXT_INIT(m, buf, m_maxsize(MC_16KCL), m_16kfree, NULL, rfa, 0, \ | |
39037602 | 982 | ref, 0, flag, 0, NULL) |
2d21ac55 | 983 | |
1c79356b | 984 | /* |
2d21ac55 | 985 | * Macro to convert BSD malloc sleep flag to mcache's |
1c79356b | 986 | */ |
0a7de745 | 987 | #define MSLEEPF(f) ((!((f) & M_DONTWAIT)) ? MCR_SLEEP : MCR_NOSLEEP) |
1c79356b | 988 | |
2d21ac55 A |
989 | /* |
990 | * The structure that holds all mbuf class statistics exportable via sysctl. | |
991 | * Similar to mbstat structure, the mb_stat structure is protected by the | |
992 | * global mbuf lock. It contains additional information about the classes | |
993 | * that allows for a more accurate view of the state of the allocator. | |
994 | */ | |
995 | struct mb_stat *mb_stat; | |
0a7de745 | 996 | struct omb_stat *omb_stat; /* For backwards compatibility */ |
1c79356b | 997 | |
0a7de745 | 998 | #define MB_STAT_SIZE(n) \ |
5ba3f43e | 999 | __builtin_offsetof(mb_stat_t, mbs_class[n]) |
0a7de745 | 1000 | #define OMB_STAT_SIZE(n) \ |
cb323159 | 1001 | __builtin_offsetof(struct omb_stat, mbs_class[n]) |
1c79356b A |
1002 | |
1003 | /* | |
2d21ac55 A |
1004 | * The legacy structure holding all of the mbuf allocation statistics. |
1005 | * The actual statistics used by the kernel are stored in the mbuf_table | |
1006 | * instead, and are updated atomically while the global mbuf lock is held. | |
1007 | * They are mirrored in mbstat to support legacy applications (e.g. netstat). | |
1008 | * Unlike before, the kernel no longer relies on the contents of mbstat for | |
1009 | * its operations (e.g. cluster expansion) because the structure is exposed | |
1010 | * to outside and could possibly be modified, therefore making it unsafe. | |
1011 | * With the exception of the mbstat.m_mtypes array (see below), all of the | |
1012 | * statistics are updated as they change. | |
1c79356b | 1013 | */ |
2d21ac55 | 1014 | struct mbstat mbstat; |
1c79356b | 1015 | |
0a7de745 | 1016 | #define MBSTAT_MTYPES_MAX \ |
2d21ac55 | 1017 | (sizeof (mbstat.m_mtypes) / sizeof (mbstat.m_mtypes[0])) |
1c79356b A |
1018 | |
1019 | /* | |
2d21ac55 A |
1020 | * Allocation statistics related to mbuf types (up to MT_MAX-1) are updated |
1021 | * atomically and stored in a per-CPU structure which is lock-free; this is | |
1022 | * done in order to avoid writing to the global mbstat data structure which | |
1023 | * would cause false sharing. During sysctl request for kern.ipc.mbstat, | |
1024 | * the statistics across all CPUs will be converged into the mbstat.m_mtypes | |
1025 | * array and returned to the application. Any updates for types greater or | |
1026 | * equal than MT_MAX would be done atomically to the mbstat; this slows down | |
1027 | * performance but is okay since the kernel uses only up to MT_MAX-1 while | |
1028 | * anything beyond that (up to type 255) is considered a corner case. | |
1c79356b | 1029 | */ |
2d21ac55 | 1030 | typedef struct { |
0a7de745 | 1031 | unsigned int cpu_mtypes[MT_MAX]; |
39236c6e | 1032 | } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE), packed)) mtypes_cpu_t; |
1c79356b | 1033 | |
2d21ac55 | 1034 | typedef struct { |
0a7de745 | 1035 | mtypes_cpu_t mbs_cpu[1]; |
2d21ac55 | 1036 | } mbuf_mtypes_t; |
1c79356b | 1037 | |
0a7de745 | 1038 | static mbuf_mtypes_t *mbuf_mtypes; /* per-CPU statistics */ |
2d21ac55 | 1039 | |
0a7de745 | 1040 | #define MBUF_MTYPES_SIZE(n) \ |
cb323159 | 1041 | __builtin_offsetof(mbuf_mtypes_t, mbs_cpu[n]) |
2d21ac55 | 1042 | |
0a7de745 | 1043 | #define MTYPES_CPU(p) \ |
316670eb | 1044 | ((mtypes_cpu_t *)(void *)((char *)(p) + MBUF_MTYPES_SIZE(cpu_number()))) |
2d21ac55 | 1045 | |
0a7de745 A |
1046 | #define mtype_stat_add(type, n) { \ |
1047 | if ((unsigned)(type) < MT_MAX) { \ | |
1048 | mtypes_cpu_t *mbs = MTYPES_CPU(mbuf_mtypes); \ | |
1049 | atomic_add_32(&mbs->cpu_mtypes[type], n); \ | |
1050 | } else if ((unsigned)(type) < (unsigned)MBSTAT_MTYPES_MAX) { \ | |
1051 | atomic_add_16((int16_t *)&mbstat.m_mtypes[type], n); \ | |
1052 | } \ | |
1c79356b A |
1053 | } |
1054 | ||
0a7de745 A |
1055 | #define mtype_stat_sub(t, n) mtype_stat_add(t, -(n)) |
1056 | #define mtype_stat_inc(t) mtype_stat_add(t, 1) | |
1057 | #define mtype_stat_dec(t) mtype_stat_sub(t, 1) | |
91447636 | 1058 | |
6d2010ae A |
1059 | static void |
1060 | mbuf_mtypes_sync(boolean_t locked) | |
2d21ac55 | 1061 | { |
2d21ac55 A |
1062 | int m, n; |
1063 | mtypes_cpu_t mtc; | |
1c79356b | 1064 | |
0a7de745 | 1065 | if (locked) { |
5ba3f43e | 1066 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 1067 | } |
6d2010ae | 1068 | |
0a7de745 | 1069 | bzero(&mtc, sizeof(mtc)); |
2d21ac55 A |
1070 | for (m = 0; m < ncpu; m++) { |
1071 | mtypes_cpu_t *scp = &mbuf_mtypes->mbs_cpu[m]; | |
1072 | mtypes_cpu_t temp; | |
9bccf70c | 1073 | |
2d21ac55 | 1074 | bcopy(&scp->cpu_mtypes, &temp.cpu_mtypes, |
0a7de745 | 1075 | sizeof(temp.cpu_mtypes)); |
91447636 | 1076 | |
0a7de745 | 1077 | for (n = 0; n < MT_MAX; n++) { |
2d21ac55 | 1078 | mtc.cpu_mtypes[n] += temp.cpu_mtypes[n]; |
0a7de745 | 1079 | } |
2d21ac55 | 1080 | } |
0a7de745 | 1081 | if (!locked) { |
6d2010ae | 1082 | lck_mtx_lock(mbuf_mlock); |
0a7de745 A |
1083 | } |
1084 | for (n = 0; n < MT_MAX; n++) { | |
2d21ac55 | 1085 | mbstat.m_mtypes[n] = mtc.cpu_mtypes[n]; |
0a7de745 A |
1086 | } |
1087 | if (!locked) { | |
6d2010ae | 1088 | lck_mtx_unlock(mbuf_mlock); |
0a7de745 | 1089 | } |
1c79356b A |
1090 | } |
1091 | ||
2d21ac55 | 1092 | static int |
6d2010ae | 1093 | mbstat_sysctl SYSCTL_HANDLER_ARGS |
1c79356b | 1094 | { |
2d21ac55 | 1095 | #pragma unused(oidp, arg1, arg2) |
6d2010ae A |
1096 | mbuf_mtypes_sync(FALSE); |
1097 | ||
0a7de745 | 1098 | return SYSCTL_OUT(req, &mbstat, sizeof(mbstat)); |
6d2010ae A |
1099 | } |
1100 | ||
1101 | static void | |
1102 | mbuf_stat_sync(void) | |
1103 | { | |
2d21ac55 | 1104 | mb_class_stat_t *sp; |
6d2010ae A |
1105 | mcache_cpu_t *ccp; |
1106 | mcache_t *cp; | |
1107 | int k, m, bktsize; | |
1108 | ||
5ba3f43e | 1109 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 1110 | |
2d21ac55 A |
1111 | for (k = 0; k < NELEM(mbuf_table); k++) { |
1112 | cp = m_cache(k); | |
1113 | ccp = &cp->mc_cpu[0]; | |
1114 | bktsize = ccp->cc_bktsize; | |
1115 | sp = mbuf_table[k].mtbl_stats; | |
1116 | ||
0a7de745 | 1117 | if (cp->mc_flags & MCF_NOCPUCACHE) { |
2d21ac55 | 1118 | sp->mbcl_mc_state = MCS_DISABLED; |
0a7de745 | 1119 | } else if (cp->mc_purge_cnt > 0) { |
2d21ac55 | 1120 | sp->mbcl_mc_state = MCS_PURGING; |
0a7de745 | 1121 | } else if (bktsize == 0) { |
2d21ac55 | 1122 | sp->mbcl_mc_state = MCS_OFFLINE; |
0a7de745 | 1123 | } else { |
2d21ac55 | 1124 | sp->mbcl_mc_state = MCS_ONLINE; |
0a7de745 | 1125 | } |
2d21ac55 A |
1126 | |
1127 | sp->mbcl_mc_cached = 0; | |
1128 | for (m = 0; m < ncpu; m++) { | |
1129 | ccp = &cp->mc_cpu[m]; | |
0a7de745 | 1130 | if (ccp->cc_objs > 0) { |
2d21ac55 | 1131 | sp->mbcl_mc_cached += ccp->cc_objs; |
0a7de745 A |
1132 | } |
1133 | if (ccp->cc_pobjs > 0) { | |
2d21ac55 | 1134 | sp->mbcl_mc_cached += ccp->cc_pobjs; |
0a7de745 | 1135 | } |
2d21ac55 A |
1136 | } |
1137 | sp->mbcl_mc_cached += (cp->mc_full.bl_total * bktsize); | |
1138 | sp->mbcl_active = sp->mbcl_total - sp->mbcl_mc_cached - | |
1139 | sp->mbcl_infree; | |
1140 | ||
1141 | sp->mbcl_mc_waiter_cnt = cp->mc_waiter_cnt; | |
1142 | sp->mbcl_mc_wretry_cnt = cp->mc_wretry_cnt; | |
1143 | sp->mbcl_mc_nwretry_cnt = cp->mc_nwretry_cnt; | |
1144 | ||
1145 | /* Calculate total count specific to each class */ | |
1146 | sp->mbcl_ctotal = sp->mbcl_total; | |
1147 | switch (m_class(k)) { | |
1148 | case MC_MBUF: | |
1149 | /* Deduct mbufs used in composite caches */ | |
1150 | sp->mbcl_ctotal -= (m_total(MC_MBUF_CL) + | |
1151 | m_total(MC_MBUF_BIGCL)); | |
1152 | break; | |
91447636 | 1153 | |
2d21ac55 | 1154 | case MC_CL: |
6d2010ae A |
1155 | /* Deduct clusters used in composite cache */ |
1156 | sp->mbcl_ctotal -= m_total(MC_MBUF_CL); | |
2d21ac55 | 1157 | break; |
91447636 | 1158 | |
2d21ac55 A |
1159 | case MC_BIGCL: |
1160 | /* Deduct clusters used in composite cache */ | |
1161 | sp->mbcl_ctotal -= m_total(MC_MBUF_BIGCL); | |
1162 | break; | |
1c79356b | 1163 | |
2d21ac55 A |
1164 | case MC_16KCL: |
1165 | /* Deduct clusters used in composite cache */ | |
1166 | sp->mbcl_ctotal -= m_total(MC_MBUF_16KCL); | |
1167 | break; | |
1168 | ||
1169 | default: | |
1170 | break; | |
1171 | } | |
1172 | } | |
6d2010ae A |
1173 | } |
1174 | ||
1175 | static int | |
1176 | mb_stat_sysctl SYSCTL_HANDLER_ARGS | |
1177 | { | |
1178 | #pragma unused(oidp, arg1, arg2) | |
1179 | void *statp; | |
1180 | int k, statsz, proc64 = proc_is64bit(req->p); | |
1181 | ||
1182 | lck_mtx_lock(mbuf_mlock); | |
1183 | mbuf_stat_sync(); | |
b0d623f7 A |
1184 | |
1185 | if (!proc64) { | |
1186 | struct omb_class_stat *oc; | |
1187 | struct mb_class_stat *c; | |
1188 | ||
1189 | omb_stat->mbs_cnt = mb_stat->mbs_cnt; | |
1190 | oc = &omb_stat->mbs_class[0]; | |
1191 | c = &mb_stat->mbs_class[0]; | |
1192 | for (k = 0; k < omb_stat->mbs_cnt; k++, oc++, c++) { | |
0a7de745 | 1193 | (void) snprintf(oc->mbcl_cname, sizeof(oc->mbcl_cname), |
b0d623f7 A |
1194 | "%s", c->mbcl_cname); |
1195 | oc->mbcl_size = c->mbcl_size; | |
1196 | oc->mbcl_total = c->mbcl_total; | |
1197 | oc->mbcl_active = c->mbcl_active; | |
1198 | oc->mbcl_infree = c->mbcl_infree; | |
1199 | oc->mbcl_slab_cnt = c->mbcl_slab_cnt; | |
1200 | oc->mbcl_alloc_cnt = c->mbcl_alloc_cnt; | |
1201 | oc->mbcl_free_cnt = c->mbcl_free_cnt; | |
1202 | oc->mbcl_notified = c->mbcl_notified; | |
1203 | oc->mbcl_purge_cnt = c->mbcl_purge_cnt; | |
1204 | oc->mbcl_fail_cnt = c->mbcl_fail_cnt; | |
1205 | oc->mbcl_ctotal = c->mbcl_ctotal; | |
fe8ab488 | 1206 | oc->mbcl_release_cnt = c->mbcl_release_cnt; |
b0d623f7 A |
1207 | oc->mbcl_mc_state = c->mbcl_mc_state; |
1208 | oc->mbcl_mc_cached = c->mbcl_mc_cached; | |
1209 | oc->mbcl_mc_waiter_cnt = c->mbcl_mc_waiter_cnt; | |
1210 | oc->mbcl_mc_wretry_cnt = c->mbcl_mc_wretry_cnt; | |
1211 | oc->mbcl_mc_nwretry_cnt = c->mbcl_mc_nwretry_cnt; | |
1212 | } | |
1213 | statp = omb_stat; | |
1214 | statsz = OMB_STAT_SIZE(NELEM(mbuf_table)); | |
1215 | } else { | |
1216 | statp = mb_stat; | |
1217 | statsz = MB_STAT_SIZE(NELEM(mbuf_table)); | |
1218 | } | |
1219 | ||
2d21ac55 | 1220 | lck_mtx_unlock(mbuf_mlock); |
9bccf70c | 1221 | |
0a7de745 | 1222 | return SYSCTL_OUT(req, statp, statsz); |
2d21ac55 | 1223 | } |
91447636 | 1224 | |
6d2010ae A |
1225 | static int |
1226 | mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS | |
1227 | { | |
1228 | #pragma unused(oidp, arg1, arg2) | |
6d2010ae A |
1229 | int i; |
1230 | ||
1231 | /* Ensure leak tracing turned on */ | |
0a7de745 A |
1232 | if (!mclfindleak || !mclexpleak) { |
1233 | return ENXIO; | |
1234 | } | |
6d2010ae | 1235 | |
6d2010ae | 1236 | lck_mtx_lock(mleak_lock); |
316670eb | 1237 | mleak_update_stats(); |
6d2010ae A |
1238 | i = SYSCTL_OUT(req, mleak_stat, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES)); |
1239 | lck_mtx_unlock(mleak_lock); | |
1240 | ||
0a7de745 | 1241 | return i; |
6d2010ae A |
1242 | } |
1243 | ||
1244 | static int | |
1245 | mleak_table_sysctl SYSCTL_HANDLER_ARGS | |
1246 | { | |
1247 | #pragma unused(oidp, arg1, arg2) | |
1248 | int i = 0; | |
1249 | ||
1250 | /* Ensure leak tracing turned on */ | |
0a7de745 A |
1251 | if (!mclfindleak || !mclexpleak) { |
1252 | return ENXIO; | |
1253 | } | |
6d2010ae A |
1254 | |
1255 | lck_mtx_lock(mleak_lock); | |
0a7de745 | 1256 | i = SYSCTL_OUT(req, &mleak_table, sizeof(mleak_table)); |
6d2010ae A |
1257 | lck_mtx_unlock(mleak_lock); |
1258 | ||
0a7de745 | 1259 | return i; |
6d2010ae A |
1260 | } |
1261 | ||
2d21ac55 A |
1262 | static inline void |
1263 | m_incref(struct mbuf *m) | |
1264 | { | |
39037602 A |
1265 | UInt16 old, new; |
1266 | volatile UInt16 *addr = (volatile UInt16 *)&MEXT_REF(m); | |
91447636 | 1267 | |
2d21ac55 A |
1268 | do { |
1269 | old = *addr; | |
1270 | new = old + 1; | |
cb323159 | 1271 | VERIFY(new != 0); |
39037602 | 1272 | } while (!OSCompareAndSwap16(old, new, addr)); |
6d2010ae A |
1273 | |
1274 | /* | |
1275 | * If cluster is shared, mark it with (sticky) EXTF_READONLY; | |
39037602 A |
1276 | * we don't clear the flag when the refcount goes back to the |
1277 | * minimum, to simplify code calling m_mclhasreference(). | |
6d2010ae | 1278 | */ |
0a7de745 | 1279 | if (new > (MEXT_MINREF(m) + 1) && !(MEXT_FLAGS(m) & EXTF_READONLY)) { |
39037602 | 1280 | (void) OSBitOrAtomic16(EXTF_READONLY, &MEXT_FLAGS(m)); |
0a7de745 | 1281 | } |
1c79356b A |
1282 | } |
1283 | ||
39037602 | 1284 | static inline u_int16_t |
2d21ac55 | 1285 | m_decref(struct mbuf *m) |
1c79356b | 1286 | { |
39037602 A |
1287 | UInt16 old, new; |
1288 | volatile UInt16 *addr = (volatile UInt16 *)&MEXT_REF(m); | |
1c79356b | 1289 | |
2d21ac55 A |
1290 | do { |
1291 | old = *addr; | |
1292 | new = old - 1; | |
cb323159 | 1293 | VERIFY(old != 0); |
39037602 | 1294 | } while (!OSCompareAndSwap16(old, new, addr)); |
2d21ac55 | 1295 | |
0a7de745 | 1296 | return new; |
1c79356b A |
1297 | } |
1298 | ||
2d21ac55 A |
1299 | static void |
1300 | mbuf_table_init(void) | |
1c79356b | 1301 | { |
6d2010ae | 1302 | unsigned int b, c, s; |
3e170ce0 | 1303 | int m, config_mbuf_jumbo = 0; |
91447636 | 1304 | |
b0d623f7 A |
1305 | MALLOC(omb_stat, struct omb_stat *, OMB_STAT_SIZE(NELEM(mbuf_table)), |
1306 | M_TEMP, M_WAITOK | M_ZERO); | |
1307 | VERIFY(omb_stat != NULL); | |
1308 | ||
2d21ac55 A |
1309 | MALLOC(mb_stat, mb_stat_t *, MB_STAT_SIZE(NELEM(mbuf_table)), |
1310 | M_TEMP, M_WAITOK | M_ZERO); | |
1311 | VERIFY(mb_stat != NULL); | |
1c79356b | 1312 | |
2d21ac55 | 1313 | mb_stat->mbs_cnt = NELEM(mbuf_table); |
0a7de745 | 1314 | for (m = 0; m < NELEM(mbuf_table); m++) { |
2d21ac55 | 1315 | mbuf_table[m].mtbl_stats = &mb_stat->mbs_class[m]; |
0a7de745 | 1316 | } |
1c79356b | 1317 | |
2d21ac55 | 1318 | #if CONFIG_MBUF_JUMBO |
3e170ce0 | 1319 | config_mbuf_jumbo = 1; |
2d21ac55 | 1320 | #endif /* CONFIG_MBUF_JUMBO */ |
9bccf70c | 1321 | |
3e170ce0 A |
1322 | if (config_mbuf_jumbo == 1 || PAGE_SIZE == M16KCLBYTES) { |
1323 | /* | |
1324 | * Set aside 1/3 of the mbuf cluster map for jumbo | |
1325 | * clusters; we do this only on platforms where jumbo | |
1326 | * cluster pool is enabled. | |
1327 | */ | |
1328 | njcl = nmbclusters / 3; | |
1329 | njclbytes = M16KCLBYTES; | |
1330 | } | |
1331 | ||
2d21ac55 | 1332 | /* |
6d2010ae A |
1333 | * nclusters holds both the 2KB and 4KB pools, so ensure it's |
1334 | * a multiple of 4KB clusters. | |
2d21ac55 | 1335 | */ |
3e170ce0 | 1336 | nclusters = P2ROUNDDOWN(nmbclusters - njcl, NCLPG); |
2d21ac55 A |
1337 | if (njcl > 0) { |
1338 | /* | |
6d2010ae A |
1339 | * Each jumbo cluster takes 8 2KB clusters, so make |
1340 | * sure that the pool size is evenly divisible by 8; | |
1341 | * njcl is in 2KB unit, hence treated as such. | |
2d21ac55 | 1342 | */ |
3e170ce0 | 1343 | njcl = P2ROUNDDOWN(nmbclusters - nclusters, NCLPJCL); |
1c79356b | 1344 | |
6d2010ae | 1345 | /* Update nclusters with rounded down value of njcl */ |
3e170ce0 | 1346 | nclusters = P2ROUNDDOWN(nmbclusters - njcl, NCLPG); |
9bccf70c | 1347 | } |
2d21ac55 A |
1348 | |
1349 | /* | |
3e170ce0 A |
1350 | * njcl is valid only on platforms with 16KB jumbo clusters or |
1351 | * with 16KB pages, where it is configured to 1/3 of the pool | |
1352 | * size. On these platforms, the remaining is used for 2KB | |
1353 | * and 4KB clusters. On platforms without 16KB jumbo clusters, | |
1354 | * the entire pool is used for both 2KB and 4KB clusters. A 4KB | |
1355 | * cluster can either be splitted into 16 mbufs, or into 2 2KB | |
1356 | * clusters. | |
6d2010ae A |
1357 | * |
1358 | * +---+---+------------ ... -----------+------- ... -------+ | |
1359 | * | c | b | s | njcl | | |
1360 | * +---+---+------------ ... -----------+------- ... -------+ | |
1361 | * | |
1362 | * 1/32th of the shared region is reserved for pure 2KB and 4KB | |
1363 | * clusters (1/64th each.) | |
1364 | */ | |
0a7de745 | 1365 | c = P2ROUNDDOWN((nclusters >> 6), NCLPG); /* in 2KB unit */ |
3e170ce0 | 1366 | b = P2ROUNDDOWN((nclusters >> (6 + NCLPBGSHIFT)), NBCLPG); /* in 4KB unit */ |
0a7de745 | 1367 | s = nclusters - (c + (b << NCLPBGSHIFT)); /* in 2KB unit */ |
6d2010ae A |
1368 | |
1369 | /* | |
1370 | * 1/64th (c) is reserved for 2KB clusters. | |
2d21ac55 | 1371 | */ |
6d2010ae | 1372 | m_minlimit(MC_CL) = c; |
0a7de745 | 1373 | m_maxlimit(MC_CL) = s + c; /* in 2KB unit */ |
2d21ac55 A |
1374 | m_maxsize(MC_CL) = m_size(MC_CL) = MCLBYTES; |
1375 | (void) snprintf(m_cname(MC_CL), MAX_MBUF_CNAME, "cl"); | |
1376 | ||
1377 | /* | |
6d2010ae A |
1378 | * Another 1/64th (b) of the map is reserved for 4KB clusters. |
1379 | * It cannot be turned into 2KB clusters or mbufs. | |
2d21ac55 | 1380 | */ |
6d2010ae | 1381 | m_minlimit(MC_BIGCL) = b; |
0a7de745 | 1382 | m_maxlimit(MC_BIGCL) = (s >> NCLPBGSHIFT) + b; /* in 4KB unit */ |
6d2010ae A |
1383 | m_maxsize(MC_BIGCL) = m_size(MC_BIGCL) = MBIGCLBYTES; |
1384 | (void) snprintf(m_cname(MC_BIGCL), MAX_MBUF_CNAME, "bigcl"); | |
2d21ac55 A |
1385 | |
1386 | /* | |
6d2010ae | 1387 | * The remaining 31/32ths (s) are all-purpose (mbufs, 2KB, or 4KB) |
2d21ac55 | 1388 | */ |
6d2010ae | 1389 | m_minlimit(MC_MBUF) = 0; |
0a7de745 | 1390 | m_maxlimit(MC_MBUF) = (s << NMBPCLSHIFT); /* in mbuf unit */ |
6d2010ae A |
1391 | m_maxsize(MC_MBUF) = m_size(MC_MBUF) = MSIZE; |
1392 | (void) snprintf(m_cname(MC_MBUF), MAX_MBUF_CNAME, "mbuf"); | |
2d21ac55 A |
1393 | |
1394 | /* | |
1395 | * Set limits for the composite classes. | |
1396 | */ | |
1397 | m_minlimit(MC_MBUF_CL) = 0; | |
6d2010ae | 1398 | m_maxlimit(MC_MBUF_CL) = m_maxlimit(MC_CL); |
2d21ac55 A |
1399 | m_maxsize(MC_MBUF_CL) = MCLBYTES; |
1400 | m_size(MC_MBUF_CL) = m_size(MC_MBUF) + m_size(MC_CL); | |
1401 | (void) snprintf(m_cname(MC_MBUF_CL), MAX_MBUF_CNAME, "mbuf_cl"); | |
1402 | ||
1403 | m_minlimit(MC_MBUF_BIGCL) = 0; | |
1404 | m_maxlimit(MC_MBUF_BIGCL) = m_maxlimit(MC_BIGCL); | |
6d2010ae | 1405 | m_maxsize(MC_MBUF_BIGCL) = MBIGCLBYTES; |
2d21ac55 A |
1406 | m_size(MC_MBUF_BIGCL) = m_size(MC_MBUF) + m_size(MC_BIGCL); |
1407 | (void) snprintf(m_cname(MC_MBUF_BIGCL), MAX_MBUF_CNAME, "mbuf_bigcl"); | |
1408 | ||
1409 | /* | |
1410 | * And for jumbo classes. | |
1411 | */ | |
1412 | m_minlimit(MC_16KCL) = 0; | |
0a7de745 | 1413 | m_maxlimit(MC_16KCL) = (njcl >> NCLPJCLSHIFT); /* in 16KB unit */ |
2d21ac55 A |
1414 | m_maxsize(MC_16KCL) = m_size(MC_16KCL) = M16KCLBYTES; |
1415 | (void) snprintf(m_cname(MC_16KCL), MAX_MBUF_CNAME, "16kcl"); | |
1416 | ||
1417 | m_minlimit(MC_MBUF_16KCL) = 0; | |
1418 | m_maxlimit(MC_MBUF_16KCL) = m_maxlimit(MC_16KCL); | |
1419 | m_maxsize(MC_MBUF_16KCL) = M16KCLBYTES; | |
1420 | m_size(MC_MBUF_16KCL) = m_size(MC_MBUF) + m_size(MC_16KCL); | |
1421 | (void) snprintf(m_cname(MC_MBUF_16KCL), MAX_MBUF_CNAME, "mbuf_16kcl"); | |
1422 | ||
1423 | /* | |
1424 | * Initialize the legacy mbstat structure. | |
1425 | */ | |
0a7de745 | 1426 | bzero(&mbstat, sizeof(mbstat)); |
2d21ac55 A |
1427 | mbstat.m_msize = m_maxsize(MC_MBUF); |
1428 | mbstat.m_mclbytes = m_maxsize(MC_CL); | |
1429 | mbstat.m_minclsize = MINCLSIZE; | |
1430 | mbstat.m_mlen = MLEN; | |
1431 | mbstat.m_mhlen = MHLEN; | |
1432 | mbstat.m_bigmclbytes = m_maxsize(MC_BIGCL); | |
1433 | } | |
1434 | ||
2a1bd2d3 A |
1435 | int |
1436 | mbuf_get_class(struct mbuf *m) | |
1437 | { | |
1438 | if (m->m_flags & M_EXT) { | |
1439 | uint32_t composite = (MEXT_FLAGS(m) & EXTF_COMPOSITE); | |
1440 | m_ext_free_func_t m_free_func = m_get_ext_free(m); | |
1441 | ||
1442 | if (m_free_func == NULL) { | |
1443 | if (composite) { | |
1444 | return MC_MBUF_CL; | |
1445 | } else { | |
1446 | return MC_CL; | |
1447 | } | |
1448 | } else if (m_free_func == m_bigfree) { | |
1449 | if (composite) { | |
1450 | return MC_MBUF_BIGCL; | |
1451 | } else { | |
1452 | return MC_BIGCL; | |
1453 | } | |
1454 | } else if (m_free_func == m_16kfree) { | |
1455 | if (composite) { | |
1456 | return MC_MBUF_16KCL; | |
1457 | } else { | |
1458 | return MC_16KCL; | |
1459 | } | |
1460 | } | |
1461 | } | |
1462 | ||
1463 | return MC_MBUF; | |
1464 | } | |
1465 | ||
1466 | bool | |
1467 | mbuf_class_under_pressure(struct mbuf *m) | |
1468 | { | |
1469 | int mclass = mbuf_get_class(m); // TODO - how can we get the class easily??? | |
1470 | ||
1471 | if (m_total(mclass) >= (m_maxlimit(mclass) * mb_memory_pressure_percentage) / 100) { | |
1472 | os_log(OS_LOG_DEFAULT, | |
1473 | "%s memory-pressure on mbuf due to class %u, total %u max %u", | |
1474 | __func__, mclass, m_total(mclass), m_maxlimit(mclass)); | |
1475 | return true; | |
1476 | } | |
1477 | ||
1478 | return false; | |
1479 | } | |
1480 | ||
b0d623f7 A |
1481 | #if defined(__LP64__) |
1482 | typedef struct ncl_tbl { | |
0a7de745 A |
1483 | uint64_t nt_maxmem; /* memory (sane) size */ |
1484 | uint32_t nt_mbpool; /* mbuf pool size */ | |
b0d623f7 A |
1485 | } ncl_tbl_t; |
1486 | ||
f427ee49 | 1487 | static const ncl_tbl_t ncl_table[] = { |
0a7de745 | 1488 | { (1ULL << GBSHIFT) /* 1 GB */, (64 << MBSHIFT) /* 64 MB */ }, |
f427ee49 A |
1489 | { (1ULL << (GBSHIFT + 2)) /* 4 GB */, (96 << MBSHIFT) /* 96 MB */ }, |
1490 | { (1ULL << (GBSHIFT + 3)) /* 8 GB */, (128 << MBSHIFT) /* 128 MB */ }, | |
1491 | { (1ULL << (GBSHIFT + 4)) /* 16 GB */, (256 << MBSHIFT) /* 256 MB */ }, | |
1492 | { (1ULL << (GBSHIFT + 5)) /* 32 GB */, (512 << MBSHIFT) /* 512 MB */ }, | |
b0d623f7 A |
1493 | { 0, 0 } |
1494 | }; | |
1495 | #endif /* __LP64__ */ | |
1496 | ||
1497 | __private_extern__ unsigned int | |
f427ee49 | 1498 | mbuf_default_ncl(uint64_t mem) |
b0d623f7 A |
1499 | { |
1500 | #if !defined(__LP64__) | |
b0d623f7 A |
1501 | unsigned int n; |
1502 | /* | |
1503 | * 32-bit kernel (default to 64MB of mbuf pool for >= 1GB RAM). | |
1504 | */ | |
0a7de745 | 1505 | if ((n = ((mem / 16) / MCLBYTES)) > 32768) { |
6d2010ae | 1506 | n = 32768; |
0a7de745 | 1507 | } |
b0d623f7 A |
1508 | #else |
1509 | unsigned int n, i; | |
b0d623f7 A |
1510 | /* |
1511 | * 64-bit kernel (mbuf pool size based on table). | |
1512 | */ | |
f427ee49 A |
1513 | n = ncl_table[0].nt_mbpool; |
1514 | for (i = 0; ncl_table[i].nt_mbpool != 0; i++) { | |
1515 | if (mem < ncl_table[i].nt_maxmem) { | |
b0d623f7 | 1516 | break; |
0a7de745 | 1517 | } |
f427ee49 | 1518 | n = ncl_table[i].nt_mbpool; |
b0d623f7 A |
1519 | } |
1520 | n >>= MCLSHIFT; | |
1521 | #endif /* !__LP64__ */ | |
0a7de745 | 1522 | return n; |
b0d623f7 A |
1523 | } |
1524 | ||
2d21ac55 A |
1525 | __private_extern__ void |
1526 | mbinit(void) | |
1527 | { | |
1528 | unsigned int m; | |
6d2010ae | 1529 | unsigned int initmcl = 0; |
2d21ac55 | 1530 | void *buf; |
b0d623f7 | 1531 | thread_t thread = THREAD_NULL; |
2d21ac55 | 1532 | |
39236c6e A |
1533 | microuptime(&mb_start); |
1534 | ||
316670eb A |
1535 | /* |
1536 | * These MBUF_ values must be equal to their private counterparts. | |
1537 | */ | |
1538 | _CASSERT(MBUF_EXT == M_EXT); | |
1539 | _CASSERT(MBUF_PKTHDR == M_PKTHDR); | |
1540 | _CASSERT(MBUF_EOR == M_EOR); | |
1541 | _CASSERT(MBUF_LOOP == M_LOOP); | |
1542 | _CASSERT(MBUF_BCAST == M_BCAST); | |
1543 | _CASSERT(MBUF_MCAST == M_MCAST); | |
1544 | _CASSERT(MBUF_FRAG == M_FRAG); | |
1545 | _CASSERT(MBUF_FIRSTFRAG == M_FIRSTFRAG); | |
1546 | _CASSERT(MBUF_LASTFRAG == M_LASTFRAG); | |
1547 | _CASSERT(MBUF_PROMISC == M_PROMISC); | |
1548 | _CASSERT(MBUF_HASFCS == M_HASFCS); | |
1549 | ||
1550 | _CASSERT(MBUF_TYPE_FREE == MT_FREE); | |
1551 | _CASSERT(MBUF_TYPE_DATA == MT_DATA); | |
1552 | _CASSERT(MBUF_TYPE_HEADER == MT_HEADER); | |
1553 | _CASSERT(MBUF_TYPE_SOCKET == MT_SOCKET); | |
1554 | _CASSERT(MBUF_TYPE_PCB == MT_PCB); | |
1555 | _CASSERT(MBUF_TYPE_RTABLE == MT_RTABLE); | |
1556 | _CASSERT(MBUF_TYPE_HTABLE == MT_HTABLE); | |
1557 | _CASSERT(MBUF_TYPE_ATABLE == MT_ATABLE); | |
1558 | _CASSERT(MBUF_TYPE_SONAME == MT_SONAME); | |
1559 | _CASSERT(MBUF_TYPE_SOOPTS == MT_SOOPTS); | |
1560 | _CASSERT(MBUF_TYPE_FTABLE == MT_FTABLE); | |
1561 | _CASSERT(MBUF_TYPE_RIGHTS == MT_RIGHTS); | |
1562 | _CASSERT(MBUF_TYPE_IFADDR == MT_IFADDR); | |
1563 | _CASSERT(MBUF_TYPE_CONTROL == MT_CONTROL); | |
1564 | _CASSERT(MBUF_TYPE_OOBDATA == MT_OOBDATA); | |
1565 | ||
1566 | _CASSERT(MBUF_TSO_IPV4 == CSUM_TSO_IPV4); | |
1567 | _CASSERT(MBUF_TSO_IPV6 == CSUM_TSO_IPV6); | |
39236c6e | 1568 | _CASSERT(MBUF_CSUM_REQ_SUM16 == CSUM_PARTIAL); |
316670eb | 1569 | _CASSERT(MBUF_CSUM_TCP_SUM16 == MBUF_CSUM_REQ_SUM16); |
5ba3f43e | 1570 | _CASSERT(MBUF_CSUM_REQ_ZERO_INVERT == CSUM_ZERO_INVERT); |
316670eb A |
1571 | _CASSERT(MBUF_CSUM_REQ_IP == CSUM_IP); |
1572 | _CASSERT(MBUF_CSUM_REQ_TCP == CSUM_TCP); | |
1573 | _CASSERT(MBUF_CSUM_REQ_UDP == CSUM_UDP); | |
1574 | _CASSERT(MBUF_CSUM_REQ_TCPIPV6 == CSUM_TCPIPV6); | |
1575 | _CASSERT(MBUF_CSUM_REQ_UDPIPV6 == CSUM_UDPIPV6); | |
1576 | _CASSERT(MBUF_CSUM_DID_IP == CSUM_IP_CHECKED); | |
1577 | _CASSERT(MBUF_CSUM_IP_GOOD == CSUM_IP_VALID); | |
1578 | _CASSERT(MBUF_CSUM_DID_DATA == CSUM_DATA_VALID); | |
1579 | _CASSERT(MBUF_CSUM_PSEUDO_HDR == CSUM_PSEUDO_HDR); | |
1580 | ||
1581 | _CASSERT(MBUF_WAITOK == M_WAIT); | |
1582 | _CASSERT(MBUF_DONTWAIT == M_DONTWAIT); | |
1583 | _CASSERT(MBUF_COPYALL == M_COPYALL); | |
1584 | ||
316670eb A |
1585 | _CASSERT(MBUF_SC2TC(MBUF_SC_BK_SYS) == MBUF_TC_BK); |
1586 | _CASSERT(MBUF_SC2TC(MBUF_SC_BK) == MBUF_TC_BK); | |
1587 | _CASSERT(MBUF_SC2TC(MBUF_SC_BE) == MBUF_TC_BE); | |
1588 | _CASSERT(MBUF_SC2TC(MBUF_SC_RD) == MBUF_TC_BE); | |
1589 | _CASSERT(MBUF_SC2TC(MBUF_SC_OAM) == MBUF_TC_BE); | |
1590 | _CASSERT(MBUF_SC2TC(MBUF_SC_AV) == MBUF_TC_VI); | |
1591 | _CASSERT(MBUF_SC2TC(MBUF_SC_RV) == MBUF_TC_VI); | |
1592 | _CASSERT(MBUF_SC2TC(MBUF_SC_VI) == MBUF_TC_VI); | |
d9a64523 | 1593 | _CASSERT(MBUF_SC2TC(MBUF_SC_SIG) == MBUF_TC_VI); |
316670eb A |
1594 | _CASSERT(MBUF_SC2TC(MBUF_SC_VO) == MBUF_TC_VO); |
1595 | _CASSERT(MBUF_SC2TC(MBUF_SC_CTL) == MBUF_TC_VO); | |
1596 | ||
1597 | _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BK) == SCVAL_BK); | |
1598 | _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BE) == SCVAL_BE); | |
1599 | _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VI) == SCVAL_VI); | |
1600 | _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VO) == SCVAL_VO); | |
1601 | ||
39236c6e A |
1602 | /* Module specific scratch space (32-bit alignment requirement) */ |
1603 | _CASSERT(!(offsetof(struct mbuf, m_pkthdr.pkt_mpriv) % | |
0a7de745 | 1604 | sizeof(uint32_t))); |
39236c6e | 1605 | |
f427ee49 A |
1606 | /* pktdata needs to start at 128-bit offset! */ |
1607 | _CASSERT((offsetof(struct mbuf, m_pktdat) % 16) == 0); | |
1608 | ||
39236c6e | 1609 | /* Initialize random red zone cookie value */ |
0a7de745 A |
1610 | _CASSERT(sizeof(mb_redzone_cookie) == |
1611 | sizeof(((struct pkthdr *)0)->redzone)); | |
1612 | read_random(&mb_redzone_cookie, sizeof(mb_redzone_cookie)); | |
1613 | read_random(&mb_obscure_extref, sizeof(mb_obscure_extref)); | |
1614 | read_random(&mb_obscure_extfree, sizeof(mb_obscure_extfree)); | |
813fb2f6 A |
1615 | mb_obscure_extref |= 0x3; |
1616 | mb_obscure_extfree |= 0x3; | |
39236c6e A |
1617 | |
1618 | /* Make sure we don't save more than we should */ | |
0a7de745 | 1619 | _CASSERT(MCA_SAVED_MBUF_SIZE <= sizeof(struct mbuf)); |
39236c6e | 1620 | |
0a7de745 | 1621 | if (nmbclusters == 0) { |
2d21ac55 | 1622 | nmbclusters = NMBCLUSTERS; |
0a7de745 | 1623 | } |
2d21ac55 | 1624 | |
6d2010ae A |
1625 | /* This should be a sane (at least even) value by now */ |
1626 | VERIFY(nmbclusters != 0 && !(nmbclusters & 0x1)); | |
1627 | ||
2d21ac55 A |
1628 | /* Setup the mbuf table */ |
1629 | mbuf_table_init(); | |
1630 | ||
1631 | /* Global lock for common layer */ | |
1632 | mbuf_mlock_grp_attr = lck_grp_attr_alloc_init(); | |
1633 | mbuf_mlock_grp = lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr); | |
1634 | mbuf_mlock_attr = lck_attr_alloc_init(); | |
316670eb | 1635 | lck_mtx_init(mbuf_mlock, mbuf_mlock_grp, mbuf_mlock_attr); |
2d21ac55 | 1636 | |
6d2010ae A |
1637 | /* |
1638 | * Allocate cluster slabs table: | |
1639 | * | |
1640 | * maxslabgrp = (N * 2048) / (1024 * 1024) | |
1641 | * | |
1642 | * Where N is nmbclusters rounded up to the nearest 512. This yields | |
1643 | * mcl_slab_g_t units, each one representing a MB of memory. | |
1644 | */ | |
1645 | maxslabgrp = | |
3e170ce0 | 1646 | (P2ROUNDUP(nmbclusters, (MBSIZE >> MCLSHIFT)) << MCLSHIFT) >> MBSHIFT; |
0a7de745 | 1647 | MALLOC(slabstbl, mcl_slabg_t * *, maxslabgrp * sizeof(mcl_slabg_t *), |
2d21ac55 A |
1648 | M_TEMP, M_WAITOK | M_ZERO); |
1649 | VERIFY(slabstbl != NULL); | |
1650 | ||
6d2010ae A |
1651 | /* |
1652 | * Allocate audit structures, if needed: | |
1653 | * | |
3e170ce0 | 1654 | * maxclaudit = (maxslabgrp * 1024 * 1024) / PAGE_SIZE |
6d2010ae A |
1655 | * |
1656 | * This yields mcl_audit_t units, each one representing a page. | |
1657 | */ | |
0a7de745 | 1658 | PE_parse_boot_argn("mbuf_debug", &mbuf_debug, sizeof(mbuf_debug)); |
2d21ac55 | 1659 | mbuf_debug |= mcache_getflags(); |
6d2010ae | 1660 | if (mbuf_debug & MCF_DEBUG) { |
3e170ce0 A |
1661 | int l; |
1662 | mcl_audit_t *mclad; | |
1663 | maxclaudit = ((maxslabgrp << MBSHIFT) >> PAGE_SHIFT); | |
0a7de745 | 1664 | MALLOC(mclaudit, mcl_audit_t *, maxclaudit * sizeof(*mclaudit), |
6d2010ae | 1665 | M_TEMP, M_WAITOK | M_ZERO); |
2d21ac55 | 1666 | VERIFY(mclaudit != NULL); |
3e170ce0 | 1667 | for (l = 0, mclad = mclaudit; l < maxclaudit; l++) { |
0a7de745 | 1668 | MALLOC(mclad[l].cl_audit, mcache_audit_t * *, |
3e170ce0 A |
1669 | NMBPG * sizeof(mcache_audit_t *), |
1670 | M_TEMP, M_WAITOK | M_ZERO); | |
1671 | VERIFY(mclad[l].cl_audit != NULL); | |
1672 | } | |
2d21ac55 A |
1673 | |
1674 | mcl_audit_con_cache = mcache_create("mcl_audit_contents", | |
0a7de745 | 1675 | AUDIT_CONTENTS_SIZE, sizeof(u_int64_t), 0, MCR_SLEEP); |
2d21ac55 A |
1676 | VERIFY(mcl_audit_con_cache != NULL); |
1677 | } | |
6d2010ae A |
1678 | mclverify = (mbuf_debug & MCF_VERIFY); |
1679 | mcltrace = (mbuf_debug & MCF_TRACE); | |
1680 | mclfindleak = !(mbuf_debug & MCF_NOLEAKLOG); | |
316670eb | 1681 | mclexpleak = mclfindleak && (mbuf_debug & MCF_EXPLEAKLOG); |
6d2010ae A |
1682 | |
1683 | /* Enable mbuf leak logging, with a lock to protect the tables */ | |
1684 | ||
1685 | mleak_lock_grp_attr = lck_grp_attr_alloc_init(); | |
1686 | mleak_lock_grp = lck_grp_alloc_init("mleak_lock", mleak_lock_grp_attr); | |
1687 | mleak_lock_attr = lck_attr_alloc_init(); | |
316670eb | 1688 | lck_mtx_init(mleak_lock, mleak_lock_grp, mleak_lock_attr); |
6d2010ae A |
1689 | |
1690 | mleak_activate(); | |
2d21ac55 | 1691 | |
5ba3f43e A |
1692 | /* |
1693 | * Allocate structure for per-CPU statistics that's aligned | |
1694 | * on the CPU cache boundary; this code assumes that we never | |
1695 | * uninitialize this framework, since the original address | |
1696 | * before alignment is not saved. | |
1697 | */ | |
f427ee49 | 1698 | ncpu = ml_wait_max_cpus(); |
5ba3f43e A |
1699 | MALLOC(buf, void *, MBUF_MTYPES_SIZE(ncpu) + CPU_CACHE_LINE_SIZE, |
1700 | M_TEMP, M_WAITOK); | |
1701 | VERIFY(buf != NULL); | |
1702 | ||
1703 | mbuf_mtypes = (mbuf_mtypes_t *)P2ROUNDUP((intptr_t)buf, | |
1704 | CPU_CACHE_LINE_SIZE); | |
1705 | bzero(mbuf_mtypes, MBUF_MTYPES_SIZE(ncpu)); | |
1706 | ||
2d21ac55 | 1707 | /* Calculate the number of pages assigned to the cluster pool */ |
3e170ce0 | 1708 | mcl_pages = (nmbclusters << MCLSHIFT) / PAGE_SIZE; |
0a7de745 | 1709 | MALLOC(mcl_paddr, ppnum_t *, mcl_pages * sizeof(ppnum_t), |
b0d623f7 | 1710 | M_TEMP, M_WAITOK); |
2d21ac55 A |
1711 | VERIFY(mcl_paddr != NULL); |
1712 | ||
1713 | /* Register with the I/O Bus mapper */ | |
1714 | mcl_paddr_base = IOMapperIOVMAlloc(mcl_pages); | |
0a7de745 | 1715 | bzero((char *)mcl_paddr, mcl_pages * sizeof(ppnum_t)); |
2d21ac55 | 1716 | |
3e170ce0 A |
1717 | embutl = (mbutl + (nmbclusters * MCLBYTES)); |
1718 | VERIFY(((embutl - mbutl) % MBIGCLBYTES) == 0); | |
2d21ac55 | 1719 | |
6d2010ae | 1720 | /* Prime up the freelist */ |
0a7de745 | 1721 | PE_parse_boot_argn("initmcl", &initmcl, sizeof(initmcl)); |
6d2010ae | 1722 | if (initmcl != 0) { |
0a7de745 A |
1723 | initmcl >>= NCLPBGSHIFT; /* become a 4K unit */ |
1724 | if (initmcl > m_maxlimit(MC_BIGCL)) { | |
6d2010ae | 1725 | initmcl = m_maxlimit(MC_BIGCL); |
0a7de745 | 1726 | } |
6d2010ae | 1727 | } |
0a7de745 | 1728 | if (initmcl < m_minlimit(MC_BIGCL)) { |
6d2010ae | 1729 | initmcl = m_minlimit(MC_BIGCL); |
0a7de745 | 1730 | } |
2d21ac55 A |
1731 | |
1732 | lck_mtx_lock(mbuf_mlock); | |
1733 | ||
6d2010ae A |
1734 | /* |
1735 | * For classes with non-zero minimum limits, populate their freelists | |
1736 | * so that m_total(class) is at least m_minlimit(class). | |
1737 | */ | |
1738 | VERIFY(m_total(MC_BIGCL) == 0 && m_minlimit(MC_BIGCL) != 0); | |
1739 | freelist_populate(m_class(MC_BIGCL), initmcl, M_WAIT); | |
1740 | VERIFY(m_total(MC_BIGCL) >= m_minlimit(MC_BIGCL)); | |
1741 | freelist_init(m_class(MC_CL)); | |
1742 | ||
1743 | for (m = 0; m < NELEM(mbuf_table); m++) { | |
1744 | /* Make sure we didn't miss any */ | |
1745 | VERIFY(m_minlimit(m_class(m)) == 0 || | |
1746 | m_total(m_class(m)) >= m_minlimit(m_class(m))); | |
fe8ab488 A |
1747 | |
1748 | /* populate the initial sizes and report from there on */ | |
1749 | m_peak(m_class(m)) = m_total(m_class(m)); | |
6d2010ae | 1750 | } |
fe8ab488 | 1751 | mb_peak_newreport = FALSE; |
2d21ac55 A |
1752 | |
1753 | lck_mtx_unlock(mbuf_mlock); | |
1754 | ||
6d2010ae A |
1755 | (void) kernel_thread_start((thread_continue_t)mbuf_worker_thread_init, |
1756 | NULL, &thread); | |
b0d623f7 | 1757 | thread_deallocate(thread); |
2d21ac55 | 1758 | |
0a7de745 | 1759 | ref_cache = mcache_create("mext_ref", sizeof(struct ext_ref), |
2d21ac55 A |
1760 | 0, 0, MCR_SLEEP); |
1761 | ||
1762 | /* Create the cache for each class */ | |
1763 | for (m = 0; m < NELEM(mbuf_table); m++) { | |
6d2010ae | 1764 | void *allocfunc, *freefunc, *auditfunc, *logfunc; |
2d21ac55 A |
1765 | u_int32_t flags; |
1766 | ||
1767 | flags = mbuf_debug; | |
1768 | if (m_class(m) == MC_MBUF_CL || m_class(m) == MC_MBUF_BIGCL || | |
1769 | m_class(m) == MC_MBUF_16KCL) { | |
1770 | allocfunc = mbuf_cslab_alloc; | |
1771 | freefunc = mbuf_cslab_free; | |
1772 | auditfunc = mbuf_cslab_audit; | |
6d2010ae | 1773 | logfunc = mleak_logger; |
2d21ac55 A |
1774 | } else { |
1775 | allocfunc = mbuf_slab_alloc; | |
1776 | freefunc = mbuf_slab_free; | |
1777 | auditfunc = mbuf_slab_audit; | |
6d2010ae | 1778 | logfunc = mleak_logger; |
2d21ac55 A |
1779 | } |
1780 | ||
1781 | /* | |
1782 | * Disable per-CPU caches for jumbo classes if there | |
1783 | * is no jumbo cluster pool available in the system. | |
1784 | * The cache itself is still created (but will never | |
1785 | * be populated) since it simplifies the code. | |
1786 | */ | |
1787 | if ((m_class(m) == MC_MBUF_16KCL || m_class(m) == MC_16KCL) && | |
0a7de745 | 1788 | njcl == 0) { |
2d21ac55 | 1789 | flags |= MCF_NOCPUCACHE; |
0a7de745 | 1790 | } |
2d21ac55 | 1791 | |
0a7de745 | 1792 | if (!mclfindleak) { |
6d2010ae | 1793 | flags |= MCF_NOLEAKLOG; |
0a7de745 | 1794 | } |
6d2010ae | 1795 | |
2d21ac55 | 1796 | m_cache(m) = mcache_create_ext(m_cname(m), m_maxsize(m), |
6d2010ae | 1797 | allocfunc, freefunc, auditfunc, logfunc, mbuf_slab_notify, |
b0d623f7 | 1798 | (void *)(uintptr_t)m, flags, MCR_SLEEP); |
2d21ac55 A |
1799 | } |
1800 | ||
6d2010ae A |
1801 | /* |
1802 | * Set the max limit on sb_max to be 1/16 th of the size of | |
b0d623f7 A |
1803 | * memory allocated for mbuf clusters. |
1804 | */ | |
6d2010ae | 1805 | high_sb_max = (nmbclusters << (MCLSHIFT - 4)); |
b0d623f7 A |
1806 | if (high_sb_max < sb_max) { |
1807 | /* sb_max is too large for this configuration, scale it down */ | |
6d2010ae | 1808 | if (high_sb_max > (1 << MBSHIFT)) { |
b0d623f7 A |
1809 | /* We have atleast 16 M of mbuf pool */ |
1810 | sb_max = high_sb_max; | |
1811 | } else if ((nmbclusters << MCLSHIFT) > (1 << MBSHIFT)) { | |
6d2010ae A |
1812 | /* |
1813 | * If we have more than 1M of mbufpool, cap the size of | |
b0d623f7 | 1814 | * max sock buf at 1M |
6d2010ae | 1815 | */ |
b0d623f7 A |
1816 | sb_max = high_sb_max = (1 << MBSHIFT); |
1817 | } else { | |
1818 | sb_max = high_sb_max; | |
1819 | } | |
1820 | } | |
1821 | ||
316670eb A |
1822 | /* allocate space for mbuf_dump_buf */ |
1823 | MALLOC(mbuf_dump_buf, char *, MBUF_DUMP_BUF_SIZE, M_TEMP, M_WAITOK); | |
1824 | VERIFY(mbuf_dump_buf != NULL); | |
1825 | ||
39236c6e A |
1826 | if (mbuf_debug & MCF_DEBUG) { |
1827 | printf("%s: MLEN %d, MHLEN %d\n", __func__, | |
1828 | (int)_MLEN, (int)_MHLEN); | |
1829 | } | |
1830 | ||
1831 | printf("%s: done [%d MB total pool size, (%d/%d) split]\n", __func__, | |
6d2010ae A |
1832 | (nmbclusters << MCLSHIFT) >> MBSHIFT, |
1833 | (nclusters << MCLSHIFT) >> MBSHIFT, | |
1834 | (njcl << MCLSHIFT) >> MBSHIFT); | |
39037602 A |
1835 | |
1836 | /* initialize lock form tx completion callback table */ | |
1837 | mbuf_tx_compl_tbl_lck_grp_attr = lck_grp_attr_alloc_init(); | |
1838 | if (mbuf_tx_compl_tbl_lck_grp_attr == NULL) { | |
1839 | panic("%s: lck_grp_attr_alloc_init failed", __func__); | |
1840 | /* NOTREACHED */ | |
1841 | } | |
1842 | mbuf_tx_compl_tbl_lck_grp = lck_grp_alloc_init("mbuf_tx_compl_tbl", | |
1843 | mbuf_tx_compl_tbl_lck_grp_attr); | |
1844 | if (mbuf_tx_compl_tbl_lck_grp == NULL) { | |
1845 | panic("%s: lck_grp_alloc_init failed", __func__); | |
1846 | /* NOTREACHED */ | |
1847 | } | |
1848 | mbuf_tx_compl_tbl_lck_attr = lck_attr_alloc_init(); | |
1849 | if (mbuf_tx_compl_tbl_lck_attr == NULL) { | |
1850 | panic("%s: lck_attr_alloc_init failed", __func__); | |
1851 | /* NOTREACHED */ | |
1852 | } | |
1853 | lck_rw_init(mbuf_tx_compl_tbl_lock, mbuf_tx_compl_tbl_lck_grp, | |
1854 | mbuf_tx_compl_tbl_lck_attr); | |
2d21ac55 A |
1855 | } |
1856 | ||
1857 | /* | |
1858 | * Obtain a slab of object(s) from the class's freelist. | |
1859 | */ | |
1860 | static mcache_obj_t * | |
1861 | slab_alloc(mbuf_class_t class, int wait) | |
1862 | { | |
1863 | mcl_slab_t *sp; | |
1864 | mcache_obj_t *buf; | |
1865 | ||
5ba3f43e | 1866 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 1867 | |
2d21ac55 A |
1868 | /* This should always be NULL for us */ |
1869 | VERIFY(m_cobjlist(class) == NULL); | |
1870 | ||
1871 | /* | |
1872 | * Treat composite objects as having longer lifespan by using | |
1873 | * a slab from the reverse direction, in hoping that this could | |
1874 | * reduce the probability of fragmentation for slabs that hold | |
1875 | * more than one buffer chunks (e.g. mbuf slabs). For other | |
1876 | * slabs, this probably doesn't make much of a difference. | |
1877 | */ | |
3e170ce0 | 1878 | if ((class == MC_MBUF || class == MC_CL || class == MC_BIGCL) |
0a7de745 | 1879 | && (wait & MCR_COMP)) { |
2d21ac55 | 1880 | sp = (mcl_slab_t *)TAILQ_LAST(&m_slablist(class), mcl_slhead); |
0a7de745 | 1881 | } else { |
2d21ac55 | 1882 | sp = (mcl_slab_t *)TAILQ_FIRST(&m_slablist(class)); |
0a7de745 | 1883 | } |
2d21ac55 A |
1884 | |
1885 | if (sp == NULL) { | |
1886 | VERIFY(m_infree(class) == 0 && m_slab_cnt(class) == 0); | |
1887 | /* The slab list for this class is empty */ | |
0a7de745 | 1888 | return NULL; |
2d21ac55 A |
1889 | } |
1890 | ||
1891 | VERIFY(m_infree(class) > 0); | |
1892 | VERIFY(!slab_is_detached(sp)); | |
1893 | VERIFY(sp->sl_class == class && | |
1894 | (sp->sl_flags & (SLF_MAPPED | SLF_PARTIAL)) == SLF_MAPPED); | |
1895 | buf = sp->sl_head; | |
1896 | VERIFY(slab_inrange(sp, buf) && sp == slab_get(buf)); | |
3e170ce0 A |
1897 | sp->sl_head = buf->obj_next; |
1898 | /* Increment slab reference */ | |
1899 | sp->sl_refcnt++; | |
1900 | ||
1901 | VERIFY(sp->sl_head != NULL || sp->sl_refcnt == sp->sl_chunks); | |
2d21ac55 | 1902 | |
2d21ac55 A |
1903 | if (sp->sl_head != NULL && !slab_inrange(sp, sp->sl_head)) { |
1904 | slab_nextptr_panic(sp, sp->sl_head); | |
1905 | /* In case sl_head is in the map but not in the slab */ | |
1906 | VERIFY(slab_inrange(sp, sp->sl_head)); | |
1907 | /* NOTREACHED */ | |
1908 | } | |
1909 | ||
2d21ac55 A |
1910 | if (mclaudit != NULL) { |
1911 | mcache_audit_t *mca = mcl_audit_buf2mca(class, buf); | |
1912 | mca->mca_uflags = 0; | |
1913 | /* Save contents on mbuf objects only */ | |
0a7de745 | 1914 | if (class == MC_MBUF) { |
2d21ac55 | 1915 | mca->mca_uflags |= MB_SCVALID; |
0a7de745 | 1916 | } |
2d21ac55 A |
1917 | } |
1918 | ||
1919 | if (class == MC_CL) { | |
1920 | mbstat.m_clfree = (--m_infree(MC_CL)) + m_infree(MC_MBUF_CL); | |
1921 | /* | |
3e170ce0 | 1922 | * A 2K cluster slab can have at most NCLPG references. |
2d21ac55 | 1923 | */ |
3e170ce0 A |
1924 | VERIFY(sp->sl_refcnt >= 1 && sp->sl_refcnt <= NCLPG && |
1925 | sp->sl_chunks == NCLPG && sp->sl_len == PAGE_SIZE); | |
1926 | VERIFY(sp->sl_refcnt < NCLPG || sp->sl_head == NULL); | |
2d21ac55 | 1927 | } else if (class == MC_BIGCL) { |
2d21ac55 A |
1928 | mbstat.m_bigclfree = (--m_infree(MC_BIGCL)) + |
1929 | m_infree(MC_MBUF_BIGCL); | |
1930 | /* | |
3e170ce0 | 1931 | * A 4K cluster slab can have NBCLPG references. |
2d21ac55 | 1932 | */ |
3e170ce0 | 1933 | VERIFY(sp->sl_refcnt >= 1 && sp->sl_chunks == NBCLPG && |
39037602 | 1934 | sp->sl_len == PAGE_SIZE && |
3e170ce0 | 1935 | (sp->sl_refcnt < NBCLPG || sp->sl_head == NULL)); |
2d21ac55 A |
1936 | } else if (class == MC_16KCL) { |
1937 | mcl_slab_t *nsp; | |
1938 | int k; | |
1939 | ||
1940 | --m_infree(MC_16KCL); | |
1941 | VERIFY(sp->sl_refcnt == 1 && sp->sl_chunks == 1 && | |
6d2010ae | 1942 | sp->sl_len == m_maxsize(class) && sp->sl_head == NULL); |
2d21ac55 | 1943 | /* |
6d2010ae A |
1944 | * Increment 2nd-Nth slab reference, where N is NSLABSP16KB. |
1945 | * A 16KB big cluster takes NSLABSP16KB slabs, each having at | |
1946 | * most 1 reference. | |
2d21ac55 | 1947 | */ |
6d2010ae | 1948 | for (nsp = sp, k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
1949 | nsp = nsp->sl_next; |
1950 | /* Next slab must already be present */ | |
1951 | VERIFY(nsp != NULL); | |
1952 | nsp->sl_refcnt++; | |
1953 | VERIFY(!slab_is_detached(nsp)); | |
1954 | VERIFY(nsp->sl_class == MC_16KCL && | |
1955 | nsp->sl_flags == (SLF_MAPPED | SLF_PARTIAL) && | |
1956 | nsp->sl_refcnt == 1 && nsp->sl_chunks == 0 && | |
1957 | nsp->sl_len == 0 && nsp->sl_base == sp->sl_base && | |
1958 | nsp->sl_head == NULL); | |
1959 | } | |
1960 | } else { | |
6d2010ae | 1961 | VERIFY(class == MC_MBUF); |
2d21ac55 A |
1962 | --m_infree(MC_MBUF); |
1963 | /* | |
1964 | * If auditing is turned on, this check is | |
1965 | * deferred until later in mbuf_slab_audit(). | |
1966 | */ | |
0a7de745 | 1967 | if (mclaudit == NULL) { |
2d21ac55 | 1968 | _MCHECK((struct mbuf *)buf); |
0a7de745 | 1969 | } |
2d21ac55 A |
1970 | /* |
1971 | * Since we have incremented the reference count above, | |
6d2010ae | 1972 | * an mbuf slab (formerly a 4KB cluster slab that was cut |
2d21ac55 | 1973 | * up into mbufs) must have a reference count between 1 |
3e170ce0 | 1974 | * and NMBPG at this point. |
2d21ac55 | 1975 | */ |
3e170ce0 A |
1976 | VERIFY(sp->sl_refcnt >= 1 && sp->sl_refcnt <= NMBPG && |
1977 | sp->sl_chunks == NMBPG && | |
1978 | sp->sl_len == PAGE_SIZE); | |
1979 | VERIFY(sp->sl_refcnt < NMBPG || sp->sl_head == NULL); | |
2d21ac55 A |
1980 | } |
1981 | ||
1982 | /* If empty, remove this slab from the class's freelist */ | |
1983 | if (sp->sl_head == NULL) { | |
3e170ce0 A |
1984 | VERIFY(class != MC_MBUF || sp->sl_refcnt == NMBPG); |
1985 | VERIFY(class != MC_CL || sp->sl_refcnt == NCLPG); | |
1986 | VERIFY(class != MC_BIGCL || sp->sl_refcnt == NBCLPG); | |
2d21ac55 A |
1987 | slab_remove(sp, class); |
1988 | } | |
1989 | ||
0a7de745 | 1990 | return buf; |
2d21ac55 A |
1991 | } |
1992 | ||
1993 | /* | |
1994 | * Place a slab of object(s) back into a class's slab list. | |
1995 | */ | |
1996 | static void | |
1997 | slab_free(mbuf_class_t class, mcache_obj_t *buf) | |
1998 | { | |
1999 | mcl_slab_t *sp; | |
3e170ce0 A |
2000 | boolean_t reinit_supercl = false; |
2001 | mbuf_class_t super_class; | |
2d21ac55 | 2002 | |
5ba3f43e | 2003 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
2004 | |
2005 | VERIFY(class != MC_16KCL || njcl > 0); | |
2006 | VERIFY(buf->obj_next == NULL); | |
3e170ce0 | 2007 | |
cc8bc92a A |
2008 | /* |
2009 | * Synchronizing with m_clalloc, as it reads m_total, while we here | |
2010 | * are modifying m_total. | |
2011 | */ | |
2012 | while (mb_clalloc_busy) { | |
2013 | mb_clalloc_waiters++; | |
2014 | (void) msleep(mb_clalloc_waitchan, mbuf_mlock, | |
0a7de745 | 2015 | (PZERO - 1), "m_clalloc", NULL); |
cc8bc92a A |
2016 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2017 | } | |
2018 | ||
2019 | /* We are busy now; tell everyone else to go away */ | |
2020 | mb_clalloc_busy = TRUE; | |
2021 | ||
2d21ac55 A |
2022 | sp = slab_get(buf); |
2023 | VERIFY(sp->sl_class == class && slab_inrange(sp, buf) && | |
2024 | (sp->sl_flags & (SLF_MAPPED | SLF_PARTIAL)) == SLF_MAPPED); | |
2025 | ||
2026 | /* Decrement slab reference */ | |
2027 | sp->sl_refcnt--; | |
2028 | ||
6d2010ae | 2029 | if (class == MC_CL) { |
2d21ac55 A |
2030 | VERIFY(IS_P2ALIGNED(buf, MCLBYTES)); |
2031 | /* | |
6d2010ae A |
2032 | * A slab that has been splitted for 2KB clusters can have |
2033 | * at most 1 outstanding reference at this point. | |
2034 | */ | |
3e170ce0 A |
2035 | VERIFY(sp->sl_refcnt >= 0 && sp->sl_refcnt <= (NCLPG - 1) && |
2036 | sp->sl_chunks == NCLPG && sp->sl_len == PAGE_SIZE); | |
2037 | VERIFY(sp->sl_refcnt < (NCLPG - 1) || | |
6d2010ae A |
2038 | (slab_is_detached(sp) && sp->sl_head == NULL)); |
2039 | } else if (class == MC_BIGCL) { | |
3e170ce0 A |
2040 | VERIFY(IS_P2ALIGNED(buf, MBIGCLBYTES)); |
2041 | ||
2042 | /* A 4KB cluster slab can have NBCLPG references at most */ | |
2043 | VERIFY(sp->sl_refcnt >= 0 && sp->sl_chunks == NBCLPG); | |
2044 | VERIFY(sp->sl_refcnt < (NBCLPG - 1) || | |
2045 | (slab_is_detached(sp) && sp->sl_head == NULL)); | |
2d21ac55 A |
2046 | } else if (class == MC_16KCL) { |
2047 | mcl_slab_t *nsp; | |
2048 | int k; | |
2049 | /* | |
6d2010ae | 2050 | * A 16KB cluster takes NSLABSP16KB slabs, all must |
2d21ac55 A |
2051 | * now have 0 reference. |
2052 | */ | |
3e170ce0 | 2053 | VERIFY(IS_P2ALIGNED(buf, PAGE_SIZE)); |
2d21ac55 | 2054 | VERIFY(sp->sl_refcnt == 0 && sp->sl_chunks == 1 && |
6d2010ae | 2055 | sp->sl_len == m_maxsize(class) && sp->sl_head == NULL); |
2d21ac55 | 2056 | VERIFY(slab_is_detached(sp)); |
6d2010ae | 2057 | for (nsp = sp, k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
2058 | nsp = nsp->sl_next; |
2059 | /* Next slab must already be present */ | |
2060 | VERIFY(nsp != NULL); | |
2061 | nsp->sl_refcnt--; | |
2062 | VERIFY(slab_is_detached(nsp)); | |
2063 | VERIFY(nsp->sl_class == MC_16KCL && | |
2064 | (nsp->sl_flags & (SLF_MAPPED | SLF_PARTIAL)) && | |
2065 | nsp->sl_refcnt == 0 && nsp->sl_chunks == 0 && | |
2066 | nsp->sl_len == 0 && nsp->sl_base == sp->sl_base && | |
2067 | nsp->sl_head == NULL); | |
2068 | } | |
2069 | } else { | |
2070 | /* | |
3e170ce0 A |
2071 | * A slab that has been splitted for mbufs has at most |
2072 | * NMBPG reference counts. Since we have decremented | |
2073 | * one reference above, it must now be between 0 and | |
2074 | * NMBPG-1. | |
2d21ac55 | 2075 | */ |
6d2010ae | 2076 | VERIFY(class == MC_MBUF); |
3e170ce0 A |
2077 | VERIFY(sp->sl_refcnt >= 0 && |
2078 | sp->sl_refcnt <= (NMBPG - 1) && | |
2079 | sp->sl_chunks == NMBPG && | |
2080 | sp->sl_len == PAGE_SIZE); | |
2081 | VERIFY(sp->sl_refcnt < (NMBPG - 1) || | |
2d21ac55 A |
2082 | (slab_is_detached(sp) && sp->sl_head == NULL)); |
2083 | } | |
2084 | ||
2085 | /* | |
2086 | * When auditing is enabled, ensure that the buffer still | |
2087 | * contains the free pattern. Otherwise it got corrupted | |
2088 | * while at the CPU cache layer. | |
2089 | */ | |
2090 | if (mclaudit != NULL) { | |
2091 | mcache_audit_t *mca = mcl_audit_buf2mca(class, buf); | |
6d2010ae | 2092 | if (mclverify) { |
3e170ce0 A |
2093 | mcache_audit_free_verify(mca, buf, 0, |
2094 | m_maxsize(class)); | |
6d2010ae | 2095 | } |
2d21ac55 A |
2096 | mca->mca_uflags &= ~MB_SCVALID; |
2097 | } | |
2098 | ||
2099 | if (class == MC_CL) { | |
2100 | mbstat.m_clfree = (++m_infree(MC_CL)) + m_infree(MC_MBUF_CL); | |
6d2010ae | 2101 | buf->obj_next = sp->sl_head; |
2d21ac55 A |
2102 | } else if (class == MC_BIGCL) { |
2103 | mbstat.m_bigclfree = (++m_infree(MC_BIGCL)) + | |
2104 | m_infree(MC_MBUF_BIGCL); | |
3e170ce0 | 2105 | buf->obj_next = sp->sl_head; |
2d21ac55 A |
2106 | } else if (class == MC_16KCL) { |
2107 | ++m_infree(MC_16KCL); | |
2108 | } else { | |
2109 | ++m_infree(MC_MBUF); | |
2110 | buf->obj_next = sp->sl_head; | |
2111 | } | |
2112 | sp->sl_head = buf; | |
2113 | ||
6d2010ae | 2114 | /* |
3e170ce0 A |
2115 | * If a slab has been split to either one which holds 2KB clusters, |
2116 | * or one which holds mbufs, turn it back to one which holds a | |
2117 | * 4 or 16 KB cluster depending on the page size. | |
6d2010ae | 2118 | */ |
3e170ce0 A |
2119 | if (m_maxsize(MC_BIGCL) == PAGE_SIZE) { |
2120 | super_class = MC_BIGCL; | |
2121 | } else { | |
2122 | VERIFY(PAGE_SIZE == m_maxsize(MC_16KCL)); | |
2123 | super_class = MC_16KCL; | |
2124 | } | |
6d2010ae | 2125 | if (class == MC_MBUF && sp->sl_refcnt == 0 && |
3e170ce0 A |
2126 | m_total(class) >= (m_minlimit(class) + NMBPG) && |
2127 | m_total(super_class) < m_maxlimit(super_class)) { | |
2128 | int i = NMBPG; | |
6d2010ae | 2129 | |
3e170ce0 | 2130 | m_total(MC_MBUF) -= NMBPG; |
2d21ac55 | 2131 | mbstat.m_mbufs = m_total(MC_MBUF); |
3e170ce0 A |
2132 | m_infree(MC_MBUF) -= NMBPG; |
2133 | mtype_stat_add(MT_FREE, -((unsigned)NMBPG)); | |
2d21ac55 A |
2134 | |
2135 | while (i--) { | |
2136 | struct mbuf *m = sp->sl_head; | |
2137 | VERIFY(m != NULL); | |
2138 | sp->sl_head = m->m_next; | |
2139 | m->m_next = NULL; | |
2140 | } | |
3e170ce0 | 2141 | reinit_supercl = true; |
6d2010ae | 2142 | } else if (class == MC_CL && sp->sl_refcnt == 0 && |
0a7de745 | 2143 | m_total(class) >= (m_minlimit(class) + NCLPG) && |
3e170ce0 A |
2144 | m_total(super_class) < m_maxlimit(super_class)) { |
2145 | int i = NCLPG; | |
6d2010ae | 2146 | |
3e170ce0 | 2147 | m_total(MC_CL) -= NCLPG; |
6d2010ae | 2148 | mbstat.m_clusters = m_total(MC_CL); |
3e170ce0 | 2149 | m_infree(MC_CL) -= NCLPG; |
6d2010ae A |
2150 | |
2151 | while (i--) { | |
2152 | union mcluster *c = sp->sl_head; | |
2153 | VERIFY(c != NULL); | |
2154 | sp->sl_head = c->mcl_next; | |
2155 | c->mcl_next = NULL; | |
2156 | } | |
3e170ce0 A |
2157 | reinit_supercl = true; |
2158 | } else if (class == MC_BIGCL && super_class != MC_BIGCL && | |
2159 | sp->sl_refcnt == 0 && | |
2160 | m_total(class) >= (m_minlimit(class) + NBCLPG) && | |
2161 | m_total(super_class) < m_maxlimit(super_class)) { | |
2162 | int i = NBCLPG; | |
2163 | ||
2164 | VERIFY(super_class == MC_16KCL); | |
2165 | m_total(MC_BIGCL) -= NBCLPG; | |
2166 | mbstat.m_bigclusters = m_total(MC_BIGCL); | |
2167 | m_infree(MC_BIGCL) -= NBCLPG; | |
6d2010ae | 2168 | |
3e170ce0 A |
2169 | while (i--) { |
2170 | union mbigcluster *bc = sp->sl_head; | |
2171 | VERIFY(bc != NULL); | |
2172 | sp->sl_head = bc->mbc_next; | |
2173 | bc->mbc_next = NULL; | |
2174 | } | |
2175 | reinit_supercl = true; | |
2176 | } | |
2177 | ||
2178 | if (reinit_supercl) { | |
2179 | VERIFY(sp->sl_head == NULL); | |
2180 | VERIFY(m_total(class) >= m_minlimit(class)); | |
6d2010ae A |
2181 | slab_remove(sp, class); |
2182 | ||
3e170ce0 A |
2183 | /* Reinitialize it as a cluster for the super class */ |
2184 | m_total(super_class)++; | |
2185 | m_infree(super_class)++; | |
2186 | VERIFY(sp->sl_flags == (SLF_MAPPED | SLF_DETACHED) && | |
2187 | sp->sl_len == PAGE_SIZE && sp->sl_refcnt == 0); | |
6d2010ae | 2188 | |
3e170ce0 A |
2189 | slab_init(sp, super_class, SLF_MAPPED, sp->sl_base, |
2190 | sp->sl_base, PAGE_SIZE, 0, 1); | |
0a7de745 | 2191 | if (mclverify) { |
6d2010ae | 2192 | mcache_set_pattern(MCACHE_FREE_PATTERN, |
3e170ce0 | 2193 | (caddr_t)sp->sl_base, sp->sl_len); |
0a7de745 | 2194 | } |
3e170ce0 A |
2195 | ((mcache_obj_t *)(sp->sl_base))->obj_next = NULL; |
2196 | ||
2197 | if (super_class == MC_BIGCL) { | |
2198 | mbstat.m_bigclusters = m_total(MC_BIGCL); | |
2199 | mbstat.m_bigclfree = m_infree(MC_BIGCL) + | |
2200 | m_infree(MC_MBUF_BIGCL); | |
6d2010ae | 2201 | } |
2d21ac55 A |
2202 | |
2203 | VERIFY(slab_is_detached(sp)); | |
3e170ce0 A |
2204 | VERIFY(m_total(super_class) <= m_maxlimit(super_class)); |
2205 | ||
2d21ac55 | 2206 | /* And finally switch class */ |
3e170ce0 | 2207 | class = super_class; |
2d21ac55 A |
2208 | } |
2209 | ||
2210 | /* Reinsert the slab to the class's slab list */ | |
0a7de745 | 2211 | if (slab_is_detached(sp)) { |
2d21ac55 | 2212 | slab_insert(sp, class); |
0a7de745 | 2213 | } |
cc8bc92a A |
2214 | |
2215 | /* We're done; let others enter */ | |
2216 | mb_clalloc_busy = FALSE; | |
2217 | if (mb_clalloc_waiters > 0) { | |
2218 | mb_clalloc_waiters = 0; | |
2219 | wakeup(mb_clalloc_waitchan); | |
2220 | } | |
2d21ac55 A |
2221 | } |
2222 | ||
2223 | /* | |
2224 | * Common allocator for rudimentary objects called by the CPU cache layer | |
2225 | * during an allocation request whenever there is no available element in the | |
2226 | * bucket layer. It returns one or more elements from the appropriate global | |
2227 | * freelist. If the freelist is empty, it will attempt to populate it and | |
2228 | * retry the allocation. | |
2229 | */ | |
2230 | static unsigned int | |
2231 | mbuf_slab_alloc(void *arg, mcache_obj_t ***plist, unsigned int num, int wait) | |
2232 | { | |
2233 | mbuf_class_t class = (mbuf_class_t)arg; | |
2234 | unsigned int need = num; | |
2235 | mcache_obj_t **list = *plist; | |
2236 | ||
2237 | ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class)); | |
2238 | ASSERT(need > 0); | |
2239 | ||
2240 | lck_mtx_lock(mbuf_mlock); | |
2241 | ||
2242 | for (;;) { | |
2243 | if ((*list = slab_alloc(class, wait)) != NULL) { | |
2244 | (*list)->obj_next = NULL; | |
2245 | list = *plist = &(*list)->obj_next; | |
2246 | ||
2247 | if (--need == 0) { | |
2248 | /* | |
2249 | * If the number of elements in freelist has | |
2250 | * dropped below low watermark, asynchronously | |
2251 | * populate the freelist now rather than doing | |
2252 | * it later when we run out of elements. | |
2253 | */ | |
2254 | if (!mbuf_cached_above(class, wait) && | |
3e170ce0 | 2255 | m_infree(class) < (m_total(class) >> 5)) { |
2d21ac55 A |
2256 | (void) freelist_populate(class, 1, |
2257 | M_DONTWAIT); | |
2258 | } | |
2259 | break; | |
2260 | } | |
2261 | } else { | |
2262 | VERIFY(m_infree(class) == 0 || class == MC_CL); | |
2263 | ||
2264 | (void) freelist_populate(class, 1, | |
2265 | (wait & MCR_NOSLEEP) ? M_DONTWAIT : M_WAIT); | |
2266 | ||
0a7de745 | 2267 | if (m_infree(class) > 0) { |
2d21ac55 | 2268 | continue; |
0a7de745 | 2269 | } |
2d21ac55 A |
2270 | |
2271 | /* Check if there's anything at the cache layer */ | |
0a7de745 | 2272 | if (mbuf_cached_above(class, wait)) { |
2d21ac55 | 2273 | break; |
0a7de745 | 2274 | } |
2d21ac55 | 2275 | |
6d2010ae A |
2276 | /* watchdog checkpoint */ |
2277 | mbuf_watchdog(); | |
2278 | ||
2d21ac55 A |
2279 | /* We have nothing and cannot block; give up */ |
2280 | if (wait & MCR_NOSLEEP) { | |
2281 | if (!(wait & MCR_TRYHARD)) { | |
2282 | m_fail_cnt(class)++; | |
2283 | mbstat.m_drops++; | |
2284 | break; | |
2285 | } | |
2286 | } | |
2287 | ||
2288 | /* | |
2289 | * If the freelist is still empty and the caller is | |
2290 | * willing to be blocked, sleep on the wait channel | |
2291 | * until an element is available. Otherwise, if | |
2292 | * MCR_TRYHARD is set, do our best to satisfy the | |
2293 | * request without having to go to sleep. | |
2294 | */ | |
2295 | if (mbuf_worker_ready && | |
0a7de745 | 2296 | mbuf_sleep(class, need, wait)) { |
2d21ac55 | 2297 | break; |
0a7de745 | 2298 | } |
2d21ac55 | 2299 | |
5ba3f43e | 2300 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
2301 | } |
2302 | } | |
2303 | ||
2304 | m_alloc_cnt(class) += num - need; | |
2305 | lck_mtx_unlock(mbuf_mlock); | |
2306 | ||
0a7de745 | 2307 | return num - need; |
2d21ac55 A |
2308 | } |
2309 | ||
2310 | /* | |
2311 | * Common de-allocator for rudimentary objects called by the CPU cache | |
2312 | * layer when one or more elements need to be returned to the appropriate | |
2313 | * global freelist. | |
2314 | */ | |
2315 | static void | |
2316 | mbuf_slab_free(void *arg, mcache_obj_t *list, __unused int purged) | |
2317 | { | |
2318 | mbuf_class_t class = (mbuf_class_t)arg; | |
2319 | mcache_obj_t *nlist; | |
2320 | unsigned int num = 0; | |
2321 | int w; | |
2322 | ||
2323 | ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class)); | |
2324 | ||
2325 | lck_mtx_lock(mbuf_mlock); | |
2326 | ||
2327 | for (;;) { | |
2328 | nlist = list->obj_next; | |
2329 | list->obj_next = NULL; | |
2330 | slab_free(class, list); | |
2331 | ++num; | |
0a7de745 | 2332 | if ((list = nlist) == NULL) { |
2d21ac55 | 2333 | break; |
0a7de745 | 2334 | } |
2d21ac55 A |
2335 | } |
2336 | m_free_cnt(class) += num; | |
2337 | ||
0a7de745 | 2338 | if ((w = mb_waiters) > 0) { |
2d21ac55 | 2339 | mb_waiters = 0; |
0a7de745 | 2340 | } |
d9a64523 A |
2341 | if (w) { |
2342 | mbwdog_logger("waking up all threads"); | |
2343 | } | |
2d21ac55 A |
2344 | lck_mtx_unlock(mbuf_mlock); |
2345 | ||
0a7de745 | 2346 | if (w != 0) { |
2d21ac55 | 2347 | wakeup(mb_waitchan); |
0a7de745 | 2348 | } |
2d21ac55 A |
2349 | } |
2350 | ||
2351 | /* | |
2352 | * Common auditor for rudimentary objects called by the CPU cache layer | |
2353 | * during an allocation or free request. For the former, this is called | |
2354 | * after the objects are obtained from either the bucket or slab layer | |
2355 | * and before they are returned to the caller. For the latter, this is | |
2356 | * called immediately during free and before placing the objects into | |
2357 | * the bucket or slab layer. | |
2358 | */ | |
2359 | static void | |
2360 | mbuf_slab_audit(void *arg, mcache_obj_t *list, boolean_t alloc) | |
2361 | { | |
2362 | mbuf_class_t class = (mbuf_class_t)arg; | |
2363 | mcache_audit_t *mca; | |
2364 | ||
2365 | ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class)); | |
2366 | ||
2367 | while (list != NULL) { | |
2368 | lck_mtx_lock(mbuf_mlock); | |
2369 | mca = mcl_audit_buf2mca(class, list); | |
2370 | ||
2371 | /* Do the sanity checks */ | |
2372 | if (class == MC_MBUF) { | |
2373 | mcl_audit_mbuf(mca, list, FALSE, alloc); | |
2374 | ASSERT(mca->mca_uflags & MB_SCVALID); | |
2375 | } else { | |
2376 | mcl_audit_cluster(mca, list, m_maxsize(class), | |
2377 | alloc, TRUE); | |
2378 | ASSERT(!(mca->mca_uflags & MB_SCVALID)); | |
2379 | } | |
2380 | /* Record this transaction */ | |
0a7de745 | 2381 | if (mcltrace) { |
39236c6e | 2382 | mcache_buffer_log(mca, list, m_cache(class), &mb_start); |
0a7de745 | 2383 | } |
6d2010ae | 2384 | |
0a7de745 | 2385 | if (alloc) { |
2d21ac55 | 2386 | mca->mca_uflags |= MB_INUSE; |
0a7de745 | 2387 | } else { |
2d21ac55 | 2388 | mca->mca_uflags &= ~MB_INUSE; |
0a7de745 | 2389 | } |
2d21ac55 A |
2390 | /* Unpair the object (unconditionally) */ |
2391 | mca->mca_uptr = NULL; | |
2392 | lck_mtx_unlock(mbuf_mlock); | |
2393 | ||
2394 | list = list->obj_next; | |
2395 | } | |
2396 | } | |
2397 | ||
2398 | /* | |
2399 | * Common notify routine for all caches. It is called by mcache when | |
2400 | * one or more objects get freed. We use this indication to trigger | |
2401 | * the wakeup of any sleeping threads so that they can retry their | |
2402 | * allocation requests. | |
2403 | */ | |
2404 | static void | |
2405 | mbuf_slab_notify(void *arg, u_int32_t reason) | |
2406 | { | |
2407 | mbuf_class_t class = (mbuf_class_t)arg; | |
2408 | int w; | |
2409 | ||
2410 | ASSERT(MBUF_CLASS_VALID(class)); | |
2411 | ||
0a7de745 | 2412 | if (reason != MCN_RETRYALLOC) { |
2d21ac55 | 2413 | return; |
0a7de745 | 2414 | } |
2d21ac55 A |
2415 | |
2416 | lck_mtx_lock(mbuf_mlock); | |
2417 | if ((w = mb_waiters) > 0) { | |
2418 | m_notified(class)++; | |
2419 | mb_waiters = 0; | |
2420 | } | |
d9a64523 A |
2421 | if (w) { |
2422 | mbwdog_logger("waking up all threads"); | |
2423 | } | |
2d21ac55 A |
2424 | lck_mtx_unlock(mbuf_mlock); |
2425 | ||
0a7de745 | 2426 | if (w != 0) { |
2d21ac55 | 2427 | wakeup(mb_waitchan); |
0a7de745 | 2428 | } |
2d21ac55 A |
2429 | } |
2430 | ||
2431 | /* | |
2432 | * Obtain object(s) from the composite class's freelist. | |
2433 | */ | |
2434 | static unsigned int | |
2435 | cslab_alloc(mbuf_class_t class, mcache_obj_t ***plist, unsigned int num) | |
2436 | { | |
2437 | unsigned int need = num; | |
2438 | mcl_slab_t *sp, *clsp, *nsp; | |
2439 | struct mbuf *m; | |
2440 | mcache_obj_t **list = *plist; | |
2441 | void *cl; | |
2442 | ||
2443 | VERIFY(need > 0); | |
2444 | VERIFY(class != MC_MBUF_16KCL || njcl > 0); | |
5ba3f43e | 2445 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
2446 | |
2447 | /* Get what we can from the freelist */ | |
2448 | while ((*list = m_cobjlist(class)) != NULL) { | |
2449 | MRANGE(*list); | |
2450 | ||
2451 | m = (struct mbuf *)*list; | |
2452 | sp = slab_get(m); | |
2453 | cl = m->m_ext.ext_buf; | |
2454 | clsp = slab_get(cl); | |
2455 | VERIFY(m->m_flags == M_EXT && cl != NULL); | |
813fb2f6 | 2456 | VERIFY(m_get_rfa(m) != NULL && MBUF_IS_COMPOSITE(m)); |
6d2010ae A |
2457 | |
2458 | if (class == MC_MBUF_CL) { | |
2459 | VERIFY(clsp->sl_refcnt >= 1 && | |
3e170ce0 | 2460 | clsp->sl_refcnt <= NCLPG); |
6d2010ae | 2461 | } else { |
3e170ce0 A |
2462 | VERIFY(clsp->sl_refcnt >= 1 && |
2463 | clsp->sl_refcnt <= NBCLPG); | |
6d2010ae A |
2464 | } |
2465 | ||
2466 | if (class == MC_MBUF_16KCL) { | |
2d21ac55 | 2467 | int k; |
6d2010ae | 2468 | for (nsp = clsp, k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
2469 | nsp = nsp->sl_next; |
2470 | /* Next slab must already be present */ | |
2471 | VERIFY(nsp != NULL); | |
2472 | VERIFY(nsp->sl_refcnt == 1); | |
2473 | } | |
2474 | } | |
2475 | ||
2476 | if ((m_cobjlist(class) = (*list)->obj_next) != NULL && | |
2477 | !MBUF_IN_MAP(m_cobjlist(class))) { | |
2478 | slab_nextptr_panic(sp, m_cobjlist(class)); | |
2479 | /* NOTREACHED */ | |
2480 | } | |
2481 | (*list)->obj_next = NULL; | |
2482 | list = *plist = &(*list)->obj_next; | |
2483 | ||
0a7de745 | 2484 | if (--need == 0) { |
2d21ac55 | 2485 | break; |
0a7de745 | 2486 | } |
2d21ac55 A |
2487 | } |
2488 | m_infree(class) -= (num - need); | |
2489 | ||
0a7de745 | 2490 | return num - need; |
2d21ac55 A |
2491 | } |
2492 | ||
2493 | /* | |
2494 | * Place object(s) back into a composite class's freelist. | |
2495 | */ | |
2496 | static unsigned int | |
2497 | cslab_free(mbuf_class_t class, mcache_obj_t *list, int purged) | |
2498 | { | |
2499 | mcache_obj_t *o, *tail; | |
2500 | unsigned int num = 0; | |
2501 | struct mbuf *m, *ms; | |
2502 | mcache_audit_t *mca = NULL; | |
2503 | mcache_obj_t *ref_list = NULL; | |
2504 | mcl_slab_t *clsp, *nsp; | |
2505 | void *cl; | |
6d2010ae | 2506 | mbuf_class_t cl_class; |
2d21ac55 A |
2507 | |
2508 | ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class)); | |
2509 | VERIFY(class != MC_MBUF_16KCL || njcl > 0); | |
5ba3f43e | 2510 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 2511 | |
6d2010ae A |
2512 | if (class == MC_MBUF_CL) { |
2513 | cl_class = MC_CL; | |
2514 | } else if (class == MC_MBUF_BIGCL) { | |
2515 | cl_class = MC_BIGCL; | |
2516 | } else { | |
2517 | VERIFY(class == MC_MBUF_16KCL); | |
2518 | cl_class = MC_16KCL; | |
2519 | } | |
2520 | ||
2d21ac55 A |
2521 | o = tail = list; |
2522 | ||
2523 | while ((m = ms = (struct mbuf *)o) != NULL) { | |
2524 | mcache_obj_t *rfa, *nexto = o->obj_next; | |
2525 | ||
2526 | /* Do the mbuf sanity checks */ | |
2527 | if (mclaudit != NULL) { | |
2528 | mca = mcl_audit_buf2mca(MC_MBUF, (mcache_obj_t *)m); | |
6d2010ae A |
2529 | if (mclverify) { |
2530 | mcache_audit_free_verify(mca, m, 0, | |
2531 | m_maxsize(MC_MBUF)); | |
2532 | } | |
39236c6e | 2533 | ms = MCA_SAVED_MBUF_PTR(mca); |
2d21ac55 A |
2534 | } |
2535 | ||
2536 | /* Do the cluster sanity checks */ | |
2537 | cl = ms->m_ext.ext_buf; | |
2538 | clsp = slab_get(cl); | |
6d2010ae A |
2539 | if (mclverify) { |
2540 | size_t size = m_maxsize(cl_class); | |
2541 | mcache_audit_free_verify(mcl_audit_buf2mca(cl_class, | |
2d21ac55 A |
2542 | (mcache_obj_t *)cl), cl, 0, size); |
2543 | } | |
2544 | VERIFY(ms->m_type == MT_FREE); | |
2545 | VERIFY(ms->m_flags == M_EXT); | |
813fb2f6 | 2546 | VERIFY(m_get_rfa(ms) != NULL && MBUF_IS_COMPOSITE(ms)); |
6d2010ae A |
2547 | if (cl_class == MC_CL) { |
2548 | VERIFY(clsp->sl_refcnt >= 1 && | |
3e170ce0 | 2549 | clsp->sl_refcnt <= NCLPG); |
6d2010ae | 2550 | } else { |
39037602 | 2551 | VERIFY(clsp->sl_refcnt >= 1 && |
3e170ce0 | 2552 | clsp->sl_refcnt <= NBCLPG); |
6d2010ae A |
2553 | } |
2554 | if (cl_class == MC_16KCL) { | |
2d21ac55 | 2555 | int k; |
6d2010ae | 2556 | for (nsp = clsp, k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
2557 | nsp = nsp->sl_next; |
2558 | /* Next slab must already be present */ | |
2559 | VERIFY(nsp != NULL); | |
2560 | VERIFY(nsp->sl_refcnt == 1); | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | /* | |
2565 | * If we're asked to purge, restore the actual mbuf using | |
2566 | * contents of the shadow structure (if auditing is enabled) | |
2567 | * and clear EXTF_COMPOSITE flag from the mbuf, as we are | |
2568 | * about to free it and the attached cluster into their caches. | |
2569 | */ | |
2570 | if (purged) { | |
2571 | /* Restore constructed mbuf fields */ | |
0a7de745 | 2572 | if (mclaudit != NULL) { |
2d21ac55 | 2573 | mcl_audit_restore_mbuf(m, mca, TRUE); |
0a7de745 | 2574 | } |
2d21ac55 | 2575 | |
39037602 | 2576 | MEXT_MINREF(m) = 0; |
2d21ac55 | 2577 | MEXT_REF(m) = 0; |
39037602 | 2578 | MEXT_PREF(m) = 0; |
2d21ac55 | 2579 | MEXT_FLAGS(m) = 0; |
39037602 A |
2580 | MEXT_PRIV(m) = 0; |
2581 | MEXT_PMBUF(m) = NULL; | |
813fb2f6 | 2582 | MEXT_TOKEN(m) = 0; |
2d21ac55 | 2583 | |
813fb2f6 A |
2584 | rfa = (mcache_obj_t *)(void *)m_get_rfa(m); |
2585 | m_set_ext(m, NULL, NULL, NULL); | |
2d21ac55 A |
2586 | rfa->obj_next = ref_list; |
2587 | ref_list = rfa; | |
2d21ac55 A |
2588 | |
2589 | m->m_type = MT_FREE; | |
2590 | m->m_flags = m->m_len = 0; | |
2591 | m->m_next = m->m_nextpkt = NULL; | |
2592 | ||
2593 | /* Save mbuf fields and make auditing happy */ | |
0a7de745 | 2594 | if (mclaudit != NULL) { |
2d21ac55 | 2595 | mcl_audit_mbuf(mca, o, FALSE, FALSE); |
0a7de745 | 2596 | } |
2d21ac55 A |
2597 | |
2598 | VERIFY(m_total(class) > 0); | |
2599 | m_total(class)--; | |
2600 | ||
2601 | /* Free the mbuf */ | |
2602 | o->obj_next = NULL; | |
2603 | slab_free(MC_MBUF, o); | |
2604 | ||
2605 | /* And free the cluster */ | |
2606 | ((mcache_obj_t *)cl)->obj_next = NULL; | |
0a7de745 | 2607 | if (class == MC_MBUF_CL) { |
2d21ac55 | 2608 | slab_free(MC_CL, cl); |
0a7de745 | 2609 | } else if (class == MC_MBUF_BIGCL) { |
2d21ac55 | 2610 | slab_free(MC_BIGCL, cl); |
0a7de745 | 2611 | } else { |
2d21ac55 | 2612 | slab_free(MC_16KCL, cl); |
0a7de745 | 2613 | } |
2d21ac55 A |
2614 | } |
2615 | ||
2616 | ++num; | |
2617 | tail = o; | |
2618 | o = nexto; | |
2619 | } | |
2620 | ||
2621 | if (!purged) { | |
2622 | tail->obj_next = m_cobjlist(class); | |
2623 | m_cobjlist(class) = list; | |
2624 | m_infree(class) += num; | |
2625 | } else if (ref_list != NULL) { | |
2626 | mcache_free_ext(ref_cache, ref_list); | |
2627 | } | |
2628 | ||
0a7de745 | 2629 | return num; |
2d21ac55 A |
2630 | } |
2631 | ||
2632 | /* | |
2633 | * Common allocator for composite objects called by the CPU cache layer | |
2634 | * during an allocation request whenever there is no available element in | |
2635 | * the bucket layer. It returns one or more composite elements from the | |
2636 | * appropriate global freelist. If the freelist is empty, it will attempt | |
2637 | * to obtain the rudimentary objects from their caches and construct them | |
2638 | * into composite mbuf + cluster objects. | |
2639 | */ | |
2640 | static unsigned int | |
2641 | mbuf_cslab_alloc(void *arg, mcache_obj_t ***plist, unsigned int needed, | |
2642 | int wait) | |
2643 | { | |
2644 | mbuf_class_t class = (mbuf_class_t)arg; | |
6d2010ae | 2645 | mbuf_class_t cl_class = 0; |
2d21ac55 A |
2646 | unsigned int num = 0, cnum = 0, want = needed; |
2647 | mcache_obj_t *ref_list = NULL; | |
2648 | mcache_obj_t *mp_list = NULL; | |
2649 | mcache_obj_t *clp_list = NULL; | |
2650 | mcache_obj_t **list; | |
2651 | struct ext_ref *rfa; | |
2652 | struct mbuf *m; | |
2653 | void *cl; | |
2654 | ||
2655 | ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class)); | |
2656 | ASSERT(needed > 0); | |
2657 | ||
2658 | VERIFY(class != MC_MBUF_16KCL || njcl > 0); | |
2659 | ||
2660 | /* There should not be any slab for this class */ | |
2661 | VERIFY(m_slab_cnt(class) == 0 && | |
2662 | m_slablist(class).tqh_first == NULL && | |
2663 | m_slablist(class).tqh_last == NULL); | |
2664 | ||
2665 | lck_mtx_lock(mbuf_mlock); | |
2666 | ||
2667 | /* Try using the freelist first */ | |
2668 | num = cslab_alloc(class, plist, needed); | |
2669 | list = *plist; | |
2670 | if (num == needed) { | |
2671 | m_alloc_cnt(class) += num; | |
2672 | lck_mtx_unlock(mbuf_mlock); | |
0a7de745 | 2673 | return needed; |
2d21ac55 A |
2674 | } |
2675 | ||
2676 | lck_mtx_unlock(mbuf_mlock); | |
2677 | ||
2678 | /* | |
2679 | * We could not satisfy the request using the freelist alone; | |
2680 | * allocate from the appropriate rudimentary caches and use | |
2681 | * whatever we can get to construct the composite objects. | |
2682 | */ | |
2683 | needed -= num; | |
2684 | ||
2685 | /* | |
2686 | * Mark these allocation requests as coming from a composite cache. | |
2687 | * Also, if the caller is willing to be blocked, mark the request | |
2688 | * with MCR_FAILOK such that we don't end up sleeping at the mbuf | |
2689 | * slab layer waiting for the individual object when one or more | |
2690 | * of the already-constructed composite objects are available. | |
2691 | */ | |
2692 | wait |= MCR_COMP; | |
0a7de745 | 2693 | if (!(wait & MCR_NOSLEEP)) { |
2d21ac55 | 2694 | wait |= MCR_FAILOK; |
0a7de745 | 2695 | } |
2d21ac55 | 2696 | |
6d2010ae | 2697 | /* allocate mbufs */ |
2d21ac55 A |
2698 | needed = mcache_alloc_ext(m_cache(MC_MBUF), &mp_list, needed, wait); |
2699 | if (needed == 0) { | |
2700 | ASSERT(mp_list == NULL); | |
2701 | goto fail; | |
2702 | } | |
6d2010ae A |
2703 | |
2704 | /* allocate clusters */ | |
2705 | if (class == MC_MBUF_CL) { | |
2706 | cl_class = MC_CL; | |
2707 | } else if (class == MC_MBUF_BIGCL) { | |
2708 | cl_class = MC_BIGCL; | |
2709 | } else { | |
2710 | VERIFY(class == MC_MBUF_16KCL); | |
2711 | cl_class = MC_16KCL; | |
2712 | } | |
2713 | needed = mcache_alloc_ext(m_cache(cl_class), &clp_list, needed, wait); | |
2d21ac55 A |
2714 | if (needed == 0) { |
2715 | ASSERT(clp_list == NULL); | |
2716 | goto fail; | |
2717 | } | |
6d2010ae | 2718 | |
2d21ac55 A |
2719 | needed = mcache_alloc_ext(ref_cache, &ref_list, needed, wait); |
2720 | if (needed == 0) { | |
2721 | ASSERT(ref_list == NULL); | |
2722 | goto fail; | |
2723 | } | |
2724 | ||
2725 | /* | |
2726 | * By this time "needed" is MIN(mbuf, cluster, ref). Any left | |
2727 | * overs will get freed accordingly before we return to caller. | |
2728 | */ | |
2729 | for (cnum = 0; cnum < needed; cnum++) { | |
2730 | struct mbuf *ms; | |
2731 | ||
2732 | m = ms = (struct mbuf *)mp_list; | |
2733 | mp_list = mp_list->obj_next; | |
2734 | ||
2735 | cl = clp_list; | |
2736 | clp_list = clp_list->obj_next; | |
2737 | ((mcache_obj_t *)cl)->obj_next = NULL; | |
2738 | ||
2739 | rfa = (struct ext_ref *)ref_list; | |
2740 | ref_list = ref_list->obj_next; | |
316670eb | 2741 | ((mcache_obj_t *)(void *)rfa)->obj_next = NULL; |
2d21ac55 A |
2742 | |
2743 | /* | |
2744 | * If auditing is enabled, construct the shadow mbuf | |
2745 | * in the audit structure instead of in the actual one. | |
2746 | * mbuf_cslab_audit() will take care of restoring the | |
2747 | * contents after the integrity check. | |
2748 | */ | |
2749 | if (mclaudit != NULL) { | |
2750 | mcache_audit_t *mca, *cl_mca; | |
2d21ac55 A |
2751 | |
2752 | lck_mtx_lock(mbuf_mlock); | |
2753 | mca = mcl_audit_buf2mca(MC_MBUF, (mcache_obj_t *)m); | |
39236c6e | 2754 | ms = MCA_SAVED_MBUF_PTR(mca); |
3e170ce0 A |
2755 | cl_mca = mcl_audit_buf2mca(cl_class, |
2756 | (mcache_obj_t *)cl); | |
2d21ac55 A |
2757 | |
2758 | /* | |
2759 | * Pair them up. Note that this is done at the time | |
2760 | * the mbuf+cluster objects are constructed. This | |
2761 | * information should be treated as "best effort" | |
2762 | * debugging hint since more than one mbufs can refer | |
2763 | * to a cluster. In that case, the cluster might not | |
2764 | * be freed along with the mbuf it was paired with. | |
2765 | */ | |
2766 | mca->mca_uptr = cl_mca; | |
2767 | cl_mca->mca_uptr = mca; | |
2768 | ||
2769 | ASSERT(mca->mca_uflags & MB_SCVALID); | |
2770 | ASSERT(!(cl_mca->mca_uflags & MB_SCVALID)); | |
2771 | lck_mtx_unlock(mbuf_mlock); | |
2772 | ||
2773 | /* Technically, they are in the freelist */ | |
6d2010ae A |
2774 | if (mclverify) { |
2775 | size_t size; | |
2776 | ||
2777 | mcache_set_pattern(MCACHE_FREE_PATTERN, m, | |
2778 | m_maxsize(MC_MBUF)); | |
2779 | ||
0a7de745 | 2780 | if (class == MC_MBUF_CL) { |
6d2010ae | 2781 | size = m_maxsize(MC_CL); |
0a7de745 | 2782 | } else if (class == MC_MBUF_BIGCL) { |
6d2010ae | 2783 | size = m_maxsize(MC_BIGCL); |
0a7de745 | 2784 | } else { |
6d2010ae | 2785 | size = m_maxsize(MC_16KCL); |
0a7de745 | 2786 | } |
6d2010ae A |
2787 | |
2788 | mcache_set_pattern(MCACHE_FREE_PATTERN, cl, | |
2789 | size); | |
2790 | } | |
2d21ac55 A |
2791 | } |
2792 | ||
2793 | MBUF_INIT(ms, 0, MT_FREE); | |
2794 | if (class == MC_MBUF_16KCL) { | |
2795 | MBUF_16KCL_INIT(ms, cl, rfa, 0, EXTF_COMPOSITE); | |
2796 | } else if (class == MC_MBUF_BIGCL) { | |
2797 | MBUF_BIGCL_INIT(ms, cl, rfa, 0, EXTF_COMPOSITE); | |
2798 | } else { | |
2799 | MBUF_CL_INIT(ms, cl, rfa, 0, EXTF_COMPOSITE); | |
2800 | } | |
2801 | VERIFY(ms->m_flags == M_EXT); | |
813fb2f6 | 2802 | VERIFY(m_get_rfa(ms) != NULL && MBUF_IS_COMPOSITE(ms)); |
2d21ac55 A |
2803 | |
2804 | *list = (mcache_obj_t *)m; | |
2805 | (*list)->obj_next = NULL; | |
2806 | list = *plist = &(*list)->obj_next; | |
2807 | } | |
2808 | ||
2809 | fail: | |
2810 | /* | |
2811 | * Free up what's left of the above. | |
2812 | */ | |
0a7de745 | 2813 | if (mp_list != NULL) { |
2d21ac55 | 2814 | mcache_free_ext(m_cache(MC_MBUF), mp_list); |
0a7de745 A |
2815 | } |
2816 | if (clp_list != NULL) { | |
6d2010ae | 2817 | mcache_free_ext(m_cache(cl_class), clp_list); |
0a7de745 A |
2818 | } |
2819 | if (ref_list != NULL) { | |
2d21ac55 | 2820 | mcache_free_ext(ref_cache, ref_list); |
0a7de745 | 2821 | } |
2d21ac55 A |
2822 | |
2823 | lck_mtx_lock(mbuf_mlock); | |
2824 | if (num > 0 || cnum > 0) { | |
2825 | m_total(class) += cnum; | |
2826 | VERIFY(m_total(class) <= m_maxlimit(class)); | |
2827 | m_alloc_cnt(class) += num + cnum; | |
2828 | } | |
0a7de745 | 2829 | if ((num + cnum) < want) { |
2d21ac55 | 2830 | m_fail_cnt(class) += (want - (num + cnum)); |
0a7de745 | 2831 | } |
2d21ac55 A |
2832 | lck_mtx_unlock(mbuf_mlock); |
2833 | ||
0a7de745 | 2834 | return num + cnum; |
2d21ac55 A |
2835 | } |
2836 | ||
2837 | /* | |
2838 | * Common de-allocator for composite objects called by the CPU cache | |
2839 | * layer when one or more elements need to be returned to the appropriate | |
2840 | * global freelist. | |
2841 | */ | |
2842 | static void | |
2843 | mbuf_cslab_free(void *arg, mcache_obj_t *list, int purged) | |
2844 | { | |
2845 | mbuf_class_t class = (mbuf_class_t)arg; | |
2846 | unsigned int num; | |
2847 | int w; | |
2848 | ||
2849 | ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class)); | |
2850 | ||
2851 | lck_mtx_lock(mbuf_mlock); | |
2852 | ||
2853 | num = cslab_free(class, list, purged); | |
2854 | m_free_cnt(class) += num; | |
2855 | ||
0a7de745 | 2856 | if ((w = mb_waiters) > 0) { |
2d21ac55 | 2857 | mb_waiters = 0; |
0a7de745 | 2858 | } |
d9a64523 A |
2859 | if (w) { |
2860 | mbwdog_logger("waking up all threads"); | |
2861 | } | |
2d21ac55 A |
2862 | |
2863 | lck_mtx_unlock(mbuf_mlock); | |
2864 | ||
0a7de745 | 2865 | if (w != 0) { |
2d21ac55 | 2866 | wakeup(mb_waitchan); |
0a7de745 | 2867 | } |
2d21ac55 A |
2868 | } |
2869 | ||
2870 | /* | |
2871 | * Common auditor for composite objects called by the CPU cache layer | |
2872 | * during an allocation or free request. For the former, this is called | |
2873 | * after the objects are obtained from either the bucket or slab layer | |
2874 | * and before they are returned to the caller. For the latter, this is | |
2875 | * called immediately during free and before placing the objects into | |
2876 | * the bucket or slab layer. | |
2877 | */ | |
2878 | static void | |
2879 | mbuf_cslab_audit(void *arg, mcache_obj_t *list, boolean_t alloc) | |
2880 | { | |
3e170ce0 | 2881 | mbuf_class_t class = (mbuf_class_t)arg, cl_class; |
2d21ac55 A |
2882 | mcache_audit_t *mca; |
2883 | struct mbuf *m, *ms; | |
2884 | mcl_slab_t *clsp, *nsp; | |
3e170ce0 | 2885 | size_t cl_size; |
2d21ac55 A |
2886 | void *cl; |
2887 | ||
2888 | ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class)); | |
0a7de745 | 2889 | if (class == MC_MBUF_CL) { |
3e170ce0 | 2890 | cl_class = MC_CL; |
0a7de745 | 2891 | } else if (class == MC_MBUF_BIGCL) { |
3e170ce0 | 2892 | cl_class = MC_BIGCL; |
0a7de745 | 2893 | } else { |
3e170ce0 | 2894 | cl_class = MC_16KCL; |
0a7de745 | 2895 | } |
3e170ce0 | 2896 | cl_size = m_maxsize(cl_class); |
2d21ac55 A |
2897 | |
2898 | while ((m = ms = (struct mbuf *)list) != NULL) { | |
2899 | lck_mtx_lock(mbuf_mlock); | |
2900 | /* Do the mbuf sanity checks and record its transaction */ | |
2901 | mca = mcl_audit_buf2mca(MC_MBUF, (mcache_obj_t *)m); | |
2902 | mcl_audit_mbuf(mca, m, TRUE, alloc); | |
0a7de745 | 2903 | if (mcltrace) { |
39236c6e | 2904 | mcache_buffer_log(mca, m, m_cache(class), &mb_start); |
0a7de745 | 2905 | } |
6d2010ae | 2906 | |
0a7de745 | 2907 | if (alloc) { |
2d21ac55 | 2908 | mca->mca_uflags |= MB_COMP_INUSE; |
0a7de745 | 2909 | } else { |
2d21ac55 | 2910 | mca->mca_uflags &= ~MB_COMP_INUSE; |
0a7de745 | 2911 | } |
2d21ac55 A |
2912 | |
2913 | /* | |
2914 | * Use the shadow mbuf in the audit structure if we are | |
2915 | * freeing, since the contents of the actual mbuf has been | |
2916 | * pattern-filled by the above call to mcl_audit_mbuf(). | |
2917 | */ | |
0a7de745 | 2918 | if (!alloc && mclverify) { |
39236c6e | 2919 | ms = MCA_SAVED_MBUF_PTR(mca); |
0a7de745 | 2920 | } |
2d21ac55 A |
2921 | |
2922 | /* Do the cluster sanity checks and record its transaction */ | |
2923 | cl = ms->m_ext.ext_buf; | |
2924 | clsp = slab_get(cl); | |
2925 | VERIFY(ms->m_flags == M_EXT && cl != NULL); | |
813fb2f6 | 2926 | VERIFY(m_get_rfa(ms) != NULL && MBUF_IS_COMPOSITE(ms)); |
0a7de745 | 2927 | if (class == MC_MBUF_CL) { |
6d2010ae | 2928 | VERIFY(clsp->sl_refcnt >= 1 && |
3e170ce0 | 2929 | clsp->sl_refcnt <= NCLPG); |
0a7de745 | 2930 | } else { |
3e170ce0 A |
2931 | VERIFY(clsp->sl_refcnt >= 1 && |
2932 | clsp->sl_refcnt <= NBCLPG); | |
0a7de745 | 2933 | } |
6d2010ae A |
2934 | |
2935 | if (class == MC_MBUF_16KCL) { | |
2d21ac55 | 2936 | int k; |
6d2010ae | 2937 | for (nsp = clsp, k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
2938 | nsp = nsp->sl_next; |
2939 | /* Next slab must already be present */ | |
2940 | VERIFY(nsp != NULL); | |
2941 | VERIFY(nsp->sl_refcnt == 1); | |
2942 | } | |
2943 | } | |
2944 | ||
3e170ce0 A |
2945 | |
2946 | mca = mcl_audit_buf2mca(cl_class, cl); | |
2947 | mcl_audit_cluster(mca, cl, cl_size, alloc, FALSE); | |
0a7de745 | 2948 | if (mcltrace) { |
39236c6e | 2949 | mcache_buffer_log(mca, cl, m_cache(class), &mb_start); |
0a7de745 | 2950 | } |
6d2010ae | 2951 | |
0a7de745 | 2952 | if (alloc) { |
2d21ac55 | 2953 | mca->mca_uflags |= MB_COMP_INUSE; |
0a7de745 | 2954 | } else { |
2d21ac55 | 2955 | mca->mca_uflags &= ~MB_COMP_INUSE; |
0a7de745 | 2956 | } |
2d21ac55 A |
2957 | lck_mtx_unlock(mbuf_mlock); |
2958 | ||
2959 | list = list->obj_next; | |
2960 | } | |
2961 | } | |
2962 | ||
cc8bc92a A |
2963 | static void |
2964 | m_vm_error_stats(uint32_t *cnt, uint64_t *ts, uint64_t *size, | |
0a7de745 | 2965 | uint64_t alloc_size, kern_return_t error) |
cc8bc92a | 2966 | { |
cc8bc92a A |
2967 | *cnt = *cnt + 1; |
2968 | *ts = net_uptime(); | |
2969 | if (size) { | |
2970 | *size = alloc_size; | |
2971 | } | |
2972 | _CASSERT(sizeof(mb_kmem_stats) / sizeof(mb_kmem_stats[0]) == | |
2973 | sizeof(mb_kmem_stats_labels) / sizeof(mb_kmem_stats_labels[0])); | |
2974 | switch (error) { | |
2975 | case KERN_SUCCESS: | |
2976 | break; | |
2977 | case KERN_INVALID_ARGUMENT: | |
2978 | mb_kmem_stats[0]++; | |
2979 | break; | |
2980 | case KERN_INVALID_ADDRESS: | |
2981 | mb_kmem_stats[1]++; | |
2982 | break; | |
2983 | case KERN_RESOURCE_SHORTAGE: | |
2984 | mb_kmem_stats[2]++; | |
2985 | break; | |
2986 | case KERN_NO_SPACE: | |
2987 | mb_kmem_stats[3]++; | |
2988 | break; | |
2989 | case KERN_FAILURE: | |
2990 | mb_kmem_stats[4]++; | |
2991 | break; | |
2992 | default: | |
2993 | mb_kmem_stats[5]++; | |
2994 | break; | |
2995 | } | |
2996 | } | |
2997 | ||
2d21ac55 A |
2998 | /* |
2999 | * Allocate some number of mbuf clusters and place on cluster freelist. | |
3000 | */ | |
3001 | static int | |
3002 | m_clalloc(const u_int32_t num, const int wait, const u_int32_t bufsize) | |
3003 | { | |
3e170ce0 | 3004 | int i, count = 0; |
2d21ac55 | 3005 | vm_size_t size = 0; |
3e170ce0 | 3006 | int numpages = 0, large_buffer; |
2d21ac55 A |
3007 | vm_offset_t page = 0; |
3008 | mcache_audit_t *mca_list = NULL; | |
3009 | mcache_obj_t *con_list = NULL; | |
3010 | mcl_slab_t *sp; | |
3e170ce0 | 3011 | mbuf_class_t class; |
cc8bc92a | 3012 | kern_return_t error; |
2d21ac55 | 3013 | |
3e170ce0 A |
3014 | /* Set if a buffer allocation needs allocation of multiple pages */ |
3015 | large_buffer = ((bufsize == m_maxsize(MC_16KCL)) && | |
0a7de745 | 3016 | PAGE_SIZE < M16KCLBYTES); |
6d2010ae A |
3017 | VERIFY(bufsize == m_maxsize(MC_BIGCL) || |
3018 | bufsize == m_maxsize(MC_16KCL)); | |
2d21ac55 | 3019 | |
3e170ce0 A |
3020 | VERIFY((bufsize == PAGE_SIZE) || |
3021 | (bufsize > PAGE_SIZE && bufsize == m_maxsize(MC_16KCL))); | |
3022 | ||
0a7de745 | 3023 | if (bufsize == m_size(MC_BIGCL)) { |
3e170ce0 | 3024 | class = MC_BIGCL; |
0a7de745 | 3025 | } else { |
3e170ce0 | 3026 | class = MC_16KCL; |
0a7de745 | 3027 | } |
3e170ce0 | 3028 | |
5ba3f43e | 3029 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
3030 | |
3031 | /* | |
3032 | * Multiple threads may attempt to populate the cluster map one | |
3033 | * after another. Since we drop the lock below prior to acquiring | |
3034 | * the physical page(s), our view of the cluster map may no longer | |
3035 | * be accurate, and we could end up over-committing the pages beyond | |
3036 | * the maximum allowed for each class. To prevent it, this entire | |
3037 | * operation (including the page mapping) is serialized. | |
3038 | */ | |
3039 | while (mb_clalloc_busy) { | |
3040 | mb_clalloc_waiters++; | |
3041 | (void) msleep(mb_clalloc_waitchan, mbuf_mlock, | |
0a7de745 | 3042 | (PZERO - 1), "m_clalloc", NULL); |
5ba3f43e | 3043 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
3044 | } |
3045 | ||
3046 | /* We are busy now; tell everyone else to go away */ | |
3047 | mb_clalloc_busy = TRUE; | |
3048 | ||
3049 | /* | |
3050 | * Honor the caller's wish to block or not block. We have a way | |
3051 | * to grow the pool asynchronously using the mbuf worker thread. | |
3052 | */ | |
3053 | i = m_howmany(num, bufsize); | |
0a7de745 | 3054 | if (i <= 0 || (wait & M_DONTWAIT)) { |
2d21ac55 | 3055 | goto out; |
0a7de745 | 3056 | } |
2d21ac55 A |
3057 | |
3058 | lck_mtx_unlock(mbuf_mlock); | |
3059 | ||
b0d623f7 | 3060 | size = round_page(i * bufsize); |
cc8bc92a | 3061 | page = kmem_mb_alloc(mb_map, size, large_buffer, &error); |
b0d623f7 A |
3062 | |
3063 | /* | |
6d2010ae | 3064 | * If we did ask for "n" 16KB physically contiguous chunks |
b0d623f7 A |
3065 | * and didn't get them, then please try again without this |
3066 | * restriction. | |
3067 | */ | |
cc8bc92a A |
3068 | net_update_uptime(); |
3069 | if (large_buffer && page == 0) { | |
3070 | m_vm_error_stats(&mb_kmem_contig_failed, | |
3071 | &mb_kmem_contig_failed_ts, | |
3072 | &mb_kmem_contig_failed_size, | |
3073 | size, error); | |
3074 | page = kmem_mb_alloc(mb_map, size, 0, &error); | |
3075 | } | |
2d21ac55 A |
3076 | |
3077 | if (page == 0) { | |
cc8bc92a A |
3078 | m_vm_error_stats(&mb_kmem_failed, |
3079 | &mb_kmem_failed_ts, | |
3080 | &mb_kmem_failed_size, | |
3081 | size, error); | |
3082 | #if PAGE_SIZE == 4096 | |
6d2010ae | 3083 | if (bufsize == m_maxsize(MC_BIGCL)) { |
cc8bc92a A |
3084 | #else |
3085 | if (bufsize >= m_maxsize(MC_BIGCL)) { | |
3086 | #endif | |
3e170ce0 A |
3087 | /* Try for 1 page if failed */ |
3088 | size = PAGE_SIZE; | |
cc8bc92a | 3089 | page = kmem_mb_alloc(mb_map, size, 0, &error); |
5c9f4661 A |
3090 | if (page == 0) { |
3091 | m_vm_error_stats(&mb_kmem_one_failed, | |
3092 | &mb_kmem_one_failed_ts, | |
3093 | NULL, size, error); | |
3094 | } | |
2d21ac55 A |
3095 | } |
3096 | ||
3097 | if (page == 0) { | |
3098 | lck_mtx_lock(mbuf_mlock); | |
3099 | goto out; | |
3100 | } | |
3101 | } | |
3102 | ||
3e170ce0 A |
3103 | VERIFY(IS_P2ALIGNED(page, PAGE_SIZE)); |
3104 | numpages = size / PAGE_SIZE; | |
2d21ac55 A |
3105 | |
3106 | /* If auditing is enabled, allocate the audit structures now */ | |
3107 | if (mclaudit != NULL) { | |
3108 | int needed; | |
3109 | ||
3110 | /* | |
3111 | * Yes, I realize this is a waste of memory for clusters | |
3112 | * that never get transformed into mbufs, as we may end | |
3e170ce0 | 3113 | * up with NMBPG-1 unused audit structures per cluster. |
2d21ac55 A |
3114 | * But doing so tremendously simplifies the allocation |
3115 | * strategy, since at this point we are not holding the | |
6d2010ae | 3116 | * mbuf lock and the caller is okay to be blocked. |
2d21ac55 | 3117 | */ |
3e170ce0 A |
3118 | if (bufsize == PAGE_SIZE) { |
3119 | needed = numpages * NMBPG; | |
2d21ac55 A |
3120 | |
3121 | i = mcache_alloc_ext(mcl_audit_con_cache, | |
3122 | &con_list, needed, MCR_SLEEP); | |
3123 | ||
3124 | VERIFY(con_list != NULL && i == needed); | |
2d21ac55 | 3125 | } else { |
3e170ce0 A |
3126 | /* |
3127 | * if multiple 4K pages are being used for a | |
39037602 A |
3128 | * 16K cluster |
3129 | */ | |
6d2010ae | 3130 | needed = numpages / NSLABSP16KB; |
2d21ac55 A |
3131 | } |
3132 | ||
3133 | i = mcache_alloc_ext(mcache_audit_cache, | |
3134 | (mcache_obj_t **)&mca_list, needed, MCR_SLEEP); | |
3135 | ||
3136 | VERIFY(mca_list != NULL && i == needed); | |
3137 | } | |
3138 | ||
3139 | lck_mtx_lock(mbuf_mlock); | |
3140 | ||
3e170ce0 A |
3141 | for (i = 0; i < numpages; i++, page += PAGE_SIZE) { |
3142 | ppnum_t offset = | |
3143 | ((unsigned char *)page - mbutl) >> PAGE_SHIFT; | |
99c3a104 | 3144 | ppnum_t new_page = pmap_find_phys(kernel_pmap, page); |
2d21ac55 A |
3145 | |
3146 | /* | |
3e170ce0 A |
3147 | * If there is a mapper the appropriate I/O page is |
3148 | * returned; zero out the page to discard its past | |
3149 | * contents to prevent exposing leftover kernel memory. | |
2d21ac55 | 3150 | */ |
b0d623f7 | 3151 | VERIFY(offset < mcl_pages); |
39236c6e | 3152 | if (mcl_paddr_base != 0) { |
3e170ce0 | 3153 | bzero((void *)(uintptr_t) page, PAGE_SIZE); |
39236c6e A |
3154 | new_page = IOMapperInsertPage(mcl_paddr_base, |
3155 | offset, new_page); | |
99c3a104 | 3156 | } |
39236c6e | 3157 | mcl_paddr[offset] = new_page; |
2d21ac55 A |
3158 | |
3159 | /* Pattern-fill this fresh page */ | |
6d2010ae | 3160 | if (mclverify) { |
2d21ac55 | 3161 | mcache_set_pattern(MCACHE_FREE_PATTERN, |
3e170ce0 | 3162 | (caddr_t)page, PAGE_SIZE); |
6d2010ae | 3163 | } |
3e170ce0 A |
3164 | if (bufsize == PAGE_SIZE) { |
3165 | mcache_obj_t *buf; | |
2d21ac55 | 3166 | /* One for the entire page */ |
3e170ce0 | 3167 | sp = slab_get((void *)page); |
6d2010ae | 3168 | if (mclaudit != NULL) { |
3e170ce0 A |
3169 | mcl_audit_init((void *)page, |
3170 | &mca_list, &con_list, | |
3171 | AUDIT_CONTENTS_SIZE, NMBPG); | |
6d2010ae | 3172 | } |
2d21ac55 | 3173 | VERIFY(sp->sl_refcnt == 0 && sp->sl_flags == 0); |
3e170ce0 A |
3174 | slab_init(sp, class, SLF_MAPPED, (void *)page, |
3175 | (void *)page, PAGE_SIZE, 0, 1); | |
3176 | buf = (mcache_obj_t *)page; | |
3177 | buf->obj_next = NULL; | |
2d21ac55 | 3178 | |
2d21ac55 | 3179 | /* Insert this slab */ |
3e170ce0 A |
3180 | slab_insert(sp, class); |
3181 | ||
3182 | /* Update stats now since slab_get drops the lock */ | |
3183 | ++m_infree(class); | |
3184 | ++m_total(class); | |
3185 | VERIFY(m_total(class) <= m_maxlimit(class)); | |
3186 | if (class == MC_BIGCL) { | |
3187 | mbstat.m_bigclfree = m_infree(MC_BIGCL) + | |
3188 | m_infree(MC_MBUF_BIGCL); | |
3189 | mbstat.m_bigclusters = m_total(MC_BIGCL); | |
3190 | } | |
3191 | ++count; | |
3192 | } else if ((bufsize > PAGE_SIZE) && | |
3193 | (i % NSLABSP16KB) == 0) { | |
2d21ac55 A |
3194 | union m16kcluster *m16kcl = (union m16kcluster *)page; |
3195 | mcl_slab_t *nsp; | |
3196 | int k; | |
39037602 | 3197 | |
2d21ac55 A |
3198 | /* One for the entire 16KB */ |
3199 | sp = slab_get(m16kcl); | |
0a7de745 | 3200 | if (mclaudit != NULL) { |
2d21ac55 | 3201 | mcl_audit_init(m16kcl, &mca_list, NULL, 0, 1); |
0a7de745 | 3202 | } |
2d21ac55 A |
3203 | |
3204 | VERIFY(sp->sl_refcnt == 0 && sp->sl_flags == 0); | |
3205 | slab_init(sp, MC_16KCL, SLF_MAPPED, | |
3206 | m16kcl, m16kcl, bufsize, 0, 1); | |
3e170ce0 | 3207 | m16kcl->m16kcl_next = NULL; |
2d21ac55 | 3208 | |
6d2010ae A |
3209 | /* |
3210 | * 2nd-Nth page's slab is part of the first one, | |
3211 | * where N is NSLABSP16KB. | |
3212 | */ | |
3213 | for (k = 1; k < NSLABSP16KB; k++) { | |
3214 | nsp = slab_get(((union mbigcluster *)page) + k); | |
2d21ac55 A |
3215 | VERIFY(nsp->sl_refcnt == 0 && |
3216 | nsp->sl_flags == 0); | |
3217 | slab_init(nsp, MC_16KCL, | |
3218 | SLF_MAPPED | SLF_PARTIAL, | |
3219 | m16kcl, NULL, 0, 0, 0); | |
3220 | } | |
2d21ac55 A |
3221 | /* Insert this slab */ |
3222 | slab_insert(sp, MC_16KCL); | |
3223 | ||
3e170ce0 A |
3224 | /* Update stats now since slab_get drops the lock */ |
3225 | ++m_infree(MC_16KCL); | |
3226 | ++m_total(MC_16KCL); | |
2d21ac55 | 3227 | VERIFY(m_total(MC_16KCL) <= m_maxlimit(MC_16KCL)); |
3e170ce0 | 3228 | ++count; |
2d21ac55 A |
3229 | } |
3230 | } | |
3231 | VERIFY(mca_list == NULL && con_list == NULL); | |
3232 | ||
0a7de745 | 3233 | if (!mb_peak_newreport && mbuf_report_usage(class)) { |
3e170ce0 | 3234 | mb_peak_newreport = TRUE; |
0a7de745 | 3235 | } |
3e170ce0 | 3236 | |
2d21ac55 A |
3237 | /* We're done; let others enter */ |
3238 | mb_clalloc_busy = FALSE; | |
3239 | if (mb_clalloc_waiters > 0) { | |
3240 | mb_clalloc_waiters = 0; | |
3241 | wakeup(mb_clalloc_waitchan); | |
3242 | } | |
3243 | ||
0a7de745 | 3244 | return count; |
2d21ac55 | 3245 | out: |
5ba3f43e | 3246 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 3247 | |
5c9f4661 A |
3248 | mtracelarge_register(size); |
3249 | ||
2d21ac55 A |
3250 | /* We're done; let others enter */ |
3251 | mb_clalloc_busy = FALSE; | |
3252 | if (mb_clalloc_waiters > 0) { | |
3253 | mb_clalloc_waiters = 0; | |
3254 | wakeup(mb_clalloc_waitchan); | |
3255 | } | |
3256 | ||
3257 | /* | |
3258 | * When non-blocking we kick a thread if we have to grow the | |
3259 | * pool or if the number of free clusters is less than requested. | |
3260 | */ | |
39037602 | 3261 | if (i > 0 && mbuf_worker_ready && mbuf_worker_needs_wakeup) { |
d9a64523 A |
3262 | mbwdog_logger("waking up the worker thread to to grow %s by %d", |
3263 | m_cname(class), i); | |
39037602 A |
3264 | wakeup((caddr_t)&mbuf_worker_needs_wakeup); |
3265 | mbuf_worker_needs_wakeup = FALSE; | |
3266 | } | |
3e170ce0 | 3267 | if (class == MC_BIGCL) { |
2d21ac55 A |
3268 | if (i > 0) { |
3269 | /* | |
3270 | * Remember total number of 4KB clusters needed | |
3271 | * at this time. | |
3272 | */ | |
3273 | i += m_total(MC_BIGCL); | |
5ba3f43e A |
3274 | if (i > m_region_expand(MC_BIGCL)) { |
3275 | m_region_expand(MC_BIGCL) = i; | |
2d21ac55 A |
3276 | } |
3277 | } | |
0a7de745 A |
3278 | if (m_infree(MC_BIGCL) >= num) { |
3279 | return 1; | |
3280 | } | |
2d21ac55 A |
3281 | } else { |
3282 | if (i > 0) { | |
3283 | /* | |
3284 | * Remember total number of 16KB clusters needed | |
3285 | * at this time. | |
3286 | */ | |
3287 | i += m_total(MC_16KCL); | |
5ba3f43e A |
3288 | if (i > m_region_expand(MC_16KCL)) { |
3289 | m_region_expand(MC_16KCL) = i; | |
2d21ac55 A |
3290 | } |
3291 | } | |
0a7de745 A |
3292 | if (m_infree(MC_16KCL) >= num) { |
3293 | return 1; | |
3294 | } | |
2d21ac55 | 3295 | } |
0a7de745 | 3296 | return 0; |
2d21ac55 A |
3297 | } |
3298 | ||
3299 | /* | |
3300 | * Populate the global freelist of the corresponding buffer class. | |
3301 | */ | |
3302 | static int | |
3303 | freelist_populate(mbuf_class_t class, unsigned int num, int wait) | |
3304 | { | |
3305 | mcache_obj_t *o = NULL; | |
6d2010ae | 3306 | int i, numpages = 0, count; |
3e170ce0 | 3307 | mbuf_class_t super_class; |
2d21ac55 A |
3308 | |
3309 | VERIFY(class == MC_MBUF || class == MC_CL || class == MC_BIGCL || | |
3310 | class == MC_16KCL); | |
3311 | ||
5ba3f43e | 3312 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 3313 | |
3e170ce0 A |
3314 | VERIFY(PAGE_SIZE == m_maxsize(MC_BIGCL) || |
3315 | PAGE_SIZE == m_maxsize(MC_16KCL)); | |
2d21ac55 | 3316 | |
0a7de745 A |
3317 | if (m_maxsize(class) >= PAGE_SIZE) { |
3318 | return m_clalloc(num, wait, m_maxsize(class)) != 0; | |
3319 | } | |
2d21ac55 | 3320 | |
3e170ce0 A |
3321 | /* |
3322 | * The rest of the function will allocate pages and will slice | |
3323 | * them up into the right size | |
3324 | */ | |
2d21ac55 | 3325 | |
3e170ce0 A |
3326 | numpages = (num * m_size(class) + PAGE_SIZE - 1) / PAGE_SIZE; |
3327 | ||
3328 | /* Currently assume that pages are 4K or 16K */ | |
0a7de745 | 3329 | if (PAGE_SIZE == m_maxsize(MC_BIGCL)) { |
3e170ce0 | 3330 | super_class = MC_BIGCL; |
0a7de745 | 3331 | } else { |
3e170ce0 | 3332 | super_class = MC_16KCL; |
0a7de745 | 3333 | } |
2d21ac55 | 3334 | |
3e170ce0 A |
3335 | i = m_clalloc(numpages, wait, m_maxsize(super_class)); |
3336 | ||
6d2010ae | 3337 | /* how many objects will we cut the page into? */ |
3e170ce0 | 3338 | int numobj = PAGE_SIZE / m_maxsize(class); |
6d2010ae A |
3339 | |
3340 | for (count = 0; count < numpages; count++) { | |
6d2010ae | 3341 | /* respect totals, minlimit, maxlimit */ |
3e170ce0 | 3342 | if (m_total(super_class) <= m_minlimit(super_class) || |
0a7de745 | 3343 | m_total(class) >= m_maxlimit(class)) { |
6d2010ae | 3344 | break; |
0a7de745 | 3345 | } |
6d2010ae | 3346 | |
0a7de745 | 3347 | if ((o = slab_alloc(super_class, wait)) == NULL) { |
6d2010ae | 3348 | break; |
0a7de745 | 3349 | } |
6d2010ae | 3350 | |
2d21ac55 | 3351 | struct mbuf *m = (struct mbuf *)o; |
6d2010ae | 3352 | union mcluster *c = (union mcluster *)o; |
3e170ce0 | 3353 | union mbigcluster *mbc = (union mbigcluster *)o; |
2d21ac55 | 3354 | mcl_slab_t *sp = slab_get(o); |
6d2010ae | 3355 | mcache_audit_t *mca = NULL; |
2d21ac55 | 3356 | |
3e170ce0 A |
3357 | /* |
3358 | * since one full page will be converted to MC_MBUF or | |
3359 | * MC_CL, verify that the reference count will match that | |
3360 | * assumption | |
3361 | */ | |
39037602 | 3362 | VERIFY(sp->sl_refcnt == 1 && slab_is_detached(sp)); |
3e170ce0 | 3363 | VERIFY((sp->sl_flags & (SLF_MAPPED | SLF_PARTIAL)) == SLF_MAPPED); |
6d2010ae A |
3364 | /* |
3365 | * Make sure that the cluster is unmolested | |
3366 | * while in freelist | |
3367 | */ | |
3368 | if (mclverify) { | |
3e170ce0 A |
3369 | mca = mcl_audit_buf2mca(super_class, |
3370 | (mcache_obj_t *)o); | |
3371 | mcache_audit_free_verify(mca, | |
3372 | (mcache_obj_t *)o, 0, m_maxsize(super_class)); | |
2d21ac55 A |
3373 | } |
3374 | ||
3e170ce0 | 3375 | /* Reinitialize it as an mbuf or 2K or 4K slab */ |
6d2010ae | 3376 | slab_init(sp, class, sp->sl_flags, |
3e170ce0 | 3377 | sp->sl_base, NULL, PAGE_SIZE, 0, numobj); |
2d21ac55 | 3378 | |
2d21ac55 A |
3379 | VERIFY(sp->sl_head == NULL); |
3380 | ||
3e170ce0 A |
3381 | VERIFY(m_total(super_class) >= 1); |
3382 | m_total(super_class)--; | |
3383 | ||
0a7de745 | 3384 | if (super_class == MC_BIGCL) { |
3e170ce0 | 3385 | mbstat.m_bigclusters = m_total(MC_BIGCL); |
0a7de745 | 3386 | } |
2d21ac55 | 3387 | |
6d2010ae | 3388 | m_total(class) += numobj; |
5ba3f43e | 3389 | VERIFY(m_total(class) <= m_maxlimit(class)); |
6d2010ae A |
3390 | m_infree(class) += numobj; |
3391 | ||
0a7de745 | 3392 | if (!mb_peak_newreport && mbuf_report_usage(class)) { |
fe8ab488 | 3393 | mb_peak_newreport = TRUE; |
0a7de745 | 3394 | } |
6d2010ae A |
3395 | |
3396 | i = numobj; | |
3397 | if (class == MC_MBUF) { | |
3398 | mbstat.m_mbufs = m_total(MC_MBUF); | |
3e170ce0 | 3399 | mtype_stat_add(MT_FREE, NMBPG); |
6d2010ae A |
3400 | while (i--) { |
3401 | /* | |
3402 | * If auditing is enabled, construct the | |
3403 | * shadow mbuf in the audit structure | |
3404 | * instead of the actual one. | |
3405 | * mbuf_slab_audit() will take care of | |
3406 | * restoring the contents after the | |
3407 | * integrity check. | |
3408 | */ | |
3409 | if (mclaudit != NULL) { | |
3410 | struct mbuf *ms; | |
3411 | mca = mcl_audit_buf2mca(MC_MBUF, | |
3412 | (mcache_obj_t *)m); | |
39236c6e | 3413 | ms = MCA_SAVED_MBUF_PTR(mca); |
6d2010ae A |
3414 | ms->m_type = MT_FREE; |
3415 | } else { | |
3416 | m->m_type = MT_FREE; | |
3417 | } | |
3418 | m->m_next = sp->sl_head; | |
3419 | sp->sl_head = (void *)m++; | |
3420 | } | |
3e170ce0 | 3421 | } else if (class == MC_CL) { /* MC_CL */ |
6d2010ae A |
3422 | mbstat.m_clfree = |
3423 | m_infree(MC_CL) + m_infree(MC_MBUF_CL); | |
3424 | mbstat.m_clusters = m_total(MC_CL); | |
3425 | while (i--) { | |
3426 | c->mcl_next = sp->sl_head; | |
3427 | sp->sl_head = (void *)c++; | |
2d21ac55 | 3428 | } |
3e170ce0 A |
3429 | } else { |
3430 | VERIFY(class == MC_BIGCL); | |
3431 | mbstat.m_bigclusters = m_total(MC_BIGCL); | |
3432 | mbstat.m_bigclfree = m_infree(MC_BIGCL) + | |
3433 | m_infree(MC_MBUF_BIGCL); | |
3434 | while (i--) { | |
3435 | mbc->mbc_next = sp->sl_head; | |
3436 | sp->sl_head = (void *)mbc++; | |
3437 | } | |
2d21ac55 A |
3438 | } |
3439 | ||
3e170ce0 | 3440 | /* Insert into the mbuf or 2k or 4k slab list */ |
6d2010ae | 3441 | slab_insert(sp, class); |
2d21ac55 | 3442 | |
0a7de745 | 3443 | if ((i = mb_waiters) > 0) { |
2d21ac55 | 3444 | mb_waiters = 0; |
0a7de745 | 3445 | } |
d9a64523 A |
3446 | if (i != 0) { |
3447 | mbwdog_logger("waking up all threads"); | |
2d21ac55 | 3448 | wakeup(mb_waitchan); |
d9a64523 | 3449 | } |
2d21ac55 | 3450 | } |
0a7de745 | 3451 | return count != 0; |
6d2010ae | 3452 | } |
2d21ac55 | 3453 | |
6d2010ae A |
3454 | /* |
3455 | * For each class, initialize the freelist to hold m_minlimit() objects. | |
3456 | */ | |
3457 | static void | |
3458 | freelist_init(mbuf_class_t class) | |
3459 | { | |
5ba3f43e | 3460 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
6d2010ae A |
3461 | |
3462 | VERIFY(class == MC_CL || class == MC_BIGCL); | |
3463 | VERIFY(m_total(class) == 0); | |
3464 | VERIFY(m_minlimit(class) > 0); | |
3465 | ||
0a7de745 | 3466 | while (m_total(class) < m_minlimit(class)) { |
6d2010ae | 3467 | (void) freelist_populate(class, m_minlimit(class), M_WAIT); |
0a7de745 | 3468 | } |
6d2010ae A |
3469 | |
3470 | VERIFY(m_total(class) >= m_minlimit(class)); | |
2d21ac55 A |
3471 | } |
3472 | ||
3473 | /* | |
3474 | * (Inaccurately) check if it might be worth a trip back to the | |
3475 | * mcache layer due the availability of objects there. We'll | |
3476 | * end up back here if there's nothing up there. | |
3477 | */ | |
3478 | static boolean_t | |
3479 | mbuf_cached_above(mbuf_class_t class, int wait) | |
3480 | { | |
3481 | switch (class) { | |
3482 | case MC_MBUF: | |
0a7de745 A |
3483 | if (wait & MCR_COMP) { |
3484 | return !mcache_bkt_isempty(m_cache(MC_MBUF_CL)) || | |
3485 | !mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL)); | |
3486 | } | |
2d21ac55 A |
3487 | break; |
3488 | ||
3489 | case MC_CL: | |
0a7de745 A |
3490 | if (wait & MCR_COMP) { |
3491 | return !mcache_bkt_isempty(m_cache(MC_MBUF_CL)); | |
3492 | } | |
2d21ac55 A |
3493 | break; |
3494 | ||
3495 | case MC_BIGCL: | |
0a7de745 A |
3496 | if (wait & MCR_COMP) { |
3497 | return !mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL)); | |
3498 | } | |
2d21ac55 A |
3499 | break; |
3500 | ||
3501 | case MC_16KCL: | |
0a7de745 A |
3502 | if (wait & MCR_COMP) { |
3503 | return !mcache_bkt_isempty(m_cache(MC_MBUF_16KCL)); | |
3504 | } | |
2d21ac55 A |
3505 | break; |
3506 | ||
3507 | case MC_MBUF_CL: | |
3508 | case MC_MBUF_BIGCL: | |
3509 | case MC_MBUF_16KCL: | |
3510 | break; | |
3511 | ||
3512 | default: | |
3513 | VERIFY(0); | |
3514 | /* NOTREACHED */ | |
3515 | } | |
3516 | ||
0a7de745 | 3517 | return !mcache_bkt_isempty(m_cache(class)); |
2d21ac55 A |
3518 | } |
3519 | ||
3520 | /* | |
3521 | * If possible, convert constructed objects to raw ones. | |
3522 | */ | |
3523 | static boolean_t | |
3524 | mbuf_steal(mbuf_class_t class, unsigned int num) | |
3525 | { | |
3526 | mcache_obj_t *top = NULL; | |
3527 | mcache_obj_t **list = ⊤ | |
3528 | unsigned int tot = 0; | |
3529 | ||
5ba3f43e | 3530 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
3531 | |
3532 | switch (class) { | |
3533 | case MC_MBUF: | |
3534 | case MC_CL: | |
3535 | case MC_BIGCL: | |
3536 | case MC_16KCL: | |
0a7de745 | 3537 | return FALSE; |
2d21ac55 A |
3538 | |
3539 | case MC_MBUF_CL: | |
3540 | case MC_MBUF_BIGCL: | |
3541 | case MC_MBUF_16KCL: | |
3542 | /* Get the required number of constructed objects if possible */ | |
3543 | if (m_infree(class) > m_minlimit(class)) { | |
3544 | tot = cslab_alloc(class, &list, | |
3545 | MIN(num, m_infree(class))); | |
3546 | } | |
3547 | ||
3548 | /* And destroy them to get back the raw objects */ | |
0a7de745 | 3549 | if (top != NULL) { |
2d21ac55 | 3550 | (void) cslab_free(class, top, 1); |
0a7de745 | 3551 | } |
2d21ac55 A |
3552 | break; |
3553 | ||
3554 | default: | |
3555 | VERIFY(0); | |
3556 | /* NOTREACHED */ | |
3557 | } | |
3558 | ||
0a7de745 | 3559 | return tot == num; |
2d21ac55 A |
3560 | } |
3561 | ||
3562 | static void | |
3563 | m_reclaim(mbuf_class_t class, unsigned int num, boolean_t comp) | |
3564 | { | |
3565 | int m, bmap = 0; | |
3566 | ||
5ba3f43e | 3567 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
3568 | |
3569 | VERIFY(m_total(MC_CL) <= m_maxlimit(MC_CL)); | |
3570 | VERIFY(m_total(MC_BIGCL) <= m_maxlimit(MC_BIGCL)); | |
3571 | VERIFY(m_total(MC_16KCL) <= m_maxlimit(MC_16KCL)); | |
3572 | ||
3573 | /* | |
3574 | * This logic can be made smarter; for now, simply mark | |
3575 | * all other related classes as potential victims. | |
3576 | */ | |
3577 | switch (class) { | |
3578 | case MC_MBUF: | |
3579 | m_wantpurge(MC_CL)++; | |
6d2010ae | 3580 | m_wantpurge(MC_BIGCL)++; |
2d21ac55 A |
3581 | m_wantpurge(MC_MBUF_CL)++; |
3582 | m_wantpurge(MC_MBUF_BIGCL)++; | |
3583 | break; | |
3584 | ||
3585 | case MC_CL: | |
3586 | m_wantpurge(MC_MBUF)++; | |
6d2010ae A |
3587 | m_wantpurge(MC_BIGCL)++; |
3588 | m_wantpurge(MC_MBUF_BIGCL)++; | |
0a7de745 | 3589 | if (!comp) { |
2d21ac55 | 3590 | m_wantpurge(MC_MBUF_CL)++; |
0a7de745 | 3591 | } |
2d21ac55 A |
3592 | break; |
3593 | ||
3594 | case MC_BIGCL: | |
6d2010ae A |
3595 | m_wantpurge(MC_MBUF)++; |
3596 | m_wantpurge(MC_CL)++; | |
3597 | m_wantpurge(MC_MBUF_CL)++; | |
0a7de745 | 3598 | if (!comp) { |
2d21ac55 | 3599 | m_wantpurge(MC_MBUF_BIGCL)++; |
0a7de745 | 3600 | } |
2d21ac55 A |
3601 | break; |
3602 | ||
3603 | case MC_16KCL: | |
0a7de745 | 3604 | if (!comp) { |
2d21ac55 | 3605 | m_wantpurge(MC_MBUF_16KCL)++; |
0a7de745 | 3606 | } |
2d21ac55 A |
3607 | break; |
3608 | ||
3609 | default: | |
3610 | VERIFY(0); | |
3611 | /* NOTREACHED */ | |
3612 | } | |
3613 | ||
3614 | /* | |
3615 | * Run through each marked class and check if we really need to | |
3616 | * purge (and therefore temporarily disable) the per-CPU caches | |
3617 | * layer used by the class. If so, remember the classes since | |
3618 | * we are going to drop the lock below prior to purging. | |
3619 | */ | |
3620 | for (m = 0; m < NELEM(mbuf_table); m++) { | |
3621 | if (m_wantpurge(m) > 0) { | |
3622 | m_wantpurge(m) = 0; | |
3623 | /* | |
3624 | * Try hard to steal the required number of objects | |
3625 | * from the freelist of other mbuf classes. Only | |
3626 | * purge and disable the per-CPU caches layer when | |
3627 | * we don't have enough; it's the last resort. | |
3628 | */ | |
0a7de745 | 3629 | if (!mbuf_steal(m, num)) { |
2d21ac55 | 3630 | bmap |= (1 << m); |
0a7de745 | 3631 | } |
2d21ac55 A |
3632 | } |
3633 | } | |
3634 | ||
3635 | lck_mtx_unlock(mbuf_mlock); | |
3636 | ||
3637 | if (bmap != 0) { | |
39236c6e A |
3638 | /* signal the domains to drain */ |
3639 | net_drain_domains(); | |
2d21ac55 A |
3640 | |
3641 | /* Sigh; we have no other choices but to ask mcache to purge */ | |
3642 | for (m = 0; m < NELEM(mbuf_table); m++) { | |
3643 | if ((bmap & (1 << m)) && | |
fe8ab488 | 3644 | mcache_purge_cache(m_cache(m), TRUE)) { |
2d21ac55 A |
3645 | lck_mtx_lock(mbuf_mlock); |
3646 | m_purge_cnt(m)++; | |
3647 | mbstat.m_drain++; | |
3648 | lck_mtx_unlock(mbuf_mlock); | |
3649 | } | |
3650 | } | |
3651 | } else { | |
3652 | /* | |
3653 | * Request mcache to reap extra elements from all of its caches; | |
3654 | * note that all reaps are serialized and happen only at a fixed | |
3655 | * interval. | |
3656 | */ | |
3657 | mcache_reap(); | |
3658 | } | |
3659 | lck_mtx_lock(mbuf_mlock); | |
3660 | } | |
3661 | ||
3662 | static inline struct mbuf * | |
3663 | m_get_common(int wait, short type, int hdr) | |
3664 | { | |
3665 | struct mbuf *m; | |
3666 | int mcflags = MSLEEPF(wait); | |
3667 | ||
3668 | /* Is this due to a non-blocking retry? If so, then try harder */ | |
0a7de745 | 3669 | if (mcflags & MCR_NOSLEEP) { |
2d21ac55 | 3670 | mcflags |= MCR_TRYHARD; |
0a7de745 | 3671 | } |
2d21ac55 A |
3672 | |
3673 | m = mcache_alloc(m_cache(MC_MBUF), mcflags); | |
3674 | if (m != NULL) { | |
3675 | MBUF_INIT(m, hdr, type); | |
3676 | mtype_stat_inc(type); | |
3677 | mtype_stat_dec(MT_FREE); | |
2d21ac55 | 3678 | } |
0a7de745 | 3679 | return m; |
2d21ac55 A |
3680 | } |
3681 | ||
3682 | /* | |
3683 | * Space allocation routines; these are also available as macros | |
3684 | * for critical paths. | |
3685 | */ | |
0a7de745 A |
3686 | #define _M_GET(wait, type) m_get_common(wait, type, 0) |
3687 | #define _M_GETHDR(wait, type) m_get_common(wait, type, 1) | |
3688 | #define _M_RETRY(wait, type) _M_GET(wait, type) | |
3689 | #define _M_RETRYHDR(wait, type) _M_GETHDR(wait, type) | |
3690 | #define _MGET(m, how, type) ((m) = _M_GET(how, type)) | |
3691 | #define _MGETHDR(m, how, type) ((m) = _M_GETHDR(how, type)) | |
2d21ac55 A |
3692 | |
3693 | struct mbuf * | |
3694 | m_get(int wait, int type) | |
3695 | { | |
0a7de745 | 3696 | return _M_GET(wait, type); |
2d21ac55 A |
3697 | } |
3698 | ||
3699 | struct mbuf * | |
3700 | m_gethdr(int wait, int type) | |
3701 | { | |
0a7de745 | 3702 | return _M_GETHDR(wait, type); |
2d21ac55 A |
3703 | } |
3704 | ||
3705 | struct mbuf * | |
3706 | m_retry(int wait, int type) | |
3707 | { | |
0a7de745 | 3708 | return _M_RETRY(wait, type); |
2d21ac55 A |
3709 | } |
3710 | ||
3711 | struct mbuf * | |
3712 | m_retryhdr(int wait, int type) | |
3713 | { | |
0a7de745 | 3714 | return _M_RETRYHDR(wait, type); |
2d21ac55 A |
3715 | } |
3716 | ||
3717 | struct mbuf * | |
3718 | m_getclr(int wait, int type) | |
3719 | { | |
3720 | struct mbuf *m; | |
3721 | ||
3722 | _MGET(m, wait, type); | |
0a7de745 | 3723 | if (m != NULL) { |
2d21ac55 | 3724 | bzero(MTOD(m, caddr_t), MLEN); |
0a7de745 A |
3725 | } |
3726 | return m; | |
2d21ac55 A |
3727 | } |
3728 | ||
39037602 A |
3729 | static int |
3730 | m_free_paired(struct mbuf *m) | |
3731 | { | |
3732 | VERIFY((m->m_flags & M_EXT) && (MEXT_FLAGS(m) & EXTF_PAIRED)); | |
3733 | ||
3734 | membar_sync(); | |
3735 | if (MEXT_PMBUF(m) == m) { | |
3736 | volatile UInt16 *addr = (volatile UInt16 *)&MEXT_PREF(m); | |
3737 | int16_t oprefcnt, prefcnt; | |
3738 | ||
3739 | /* | |
3740 | * Paired ref count might be negative in case we lose | |
3741 | * against another thread clearing MEXT_PMBUF, in the | |
3742 | * event it occurs after the above memory barrier sync. | |
3743 | * In that case just ignore as things have been unpaired. | |
3744 | */ | |
3745 | do { | |
3746 | oprefcnt = *addr; | |
3747 | prefcnt = oprefcnt - 1; | |
3748 | } while (!OSCompareAndSwap16(oprefcnt, prefcnt, addr)); | |
3749 | ||
3750 | if (prefcnt > 1) { | |
0a7de745 | 3751 | return 1; |
39037602 | 3752 | } else if (prefcnt == 1) { |
813fb2f6 A |
3753 | (*(m_get_ext_free(m)))(m->m_ext.ext_buf, |
3754 | m->m_ext.ext_size, m_get_ext_arg(m)); | |
0a7de745 | 3755 | return 1; |
39037602 A |
3756 | } else if (prefcnt == 0) { |
3757 | VERIFY(MBUF_IS_PAIRED(m)); | |
3758 | ||
3759 | /* | |
3760 | * Restore minref to its natural value, so that | |
3761 | * the caller will be able to free the cluster | |
3762 | * as appropriate. | |
3763 | */ | |
3764 | MEXT_MINREF(m) = 0; | |
3765 | ||
3766 | /* | |
3767 | * Clear MEXT_PMBUF, but leave EXTF_PAIRED intact | |
3768 | * as it is immutable. atomic_set_ptr also causes | |
3769 | * memory barrier sync. | |
3770 | */ | |
3771 | atomic_set_ptr(&MEXT_PMBUF(m), NULL); | |
3772 | ||
3773 | switch (m->m_ext.ext_size) { | |
3774 | case MCLBYTES: | |
813fb2f6 | 3775 | m_set_ext(m, m_get_rfa(m), NULL, NULL); |
39037602 A |
3776 | break; |
3777 | ||
3778 | case MBIGCLBYTES: | |
813fb2f6 | 3779 | m_set_ext(m, m_get_rfa(m), m_bigfree, NULL); |
39037602 A |
3780 | break; |
3781 | ||
3782 | case M16KCLBYTES: | |
813fb2f6 | 3783 | m_set_ext(m, m_get_rfa(m), m_16kfree, NULL); |
39037602 A |
3784 | break; |
3785 | ||
3786 | default: | |
3787 | VERIFY(0); | |
3788 | /* NOTREACHED */ | |
3789 | } | |
3790 | } | |
3791 | } | |
3792 | ||
3793 | /* | |
3794 | * Tell caller the unpair has occurred, and that the reference | |
3795 | * count on the external cluster held for the paired mbuf should | |
3796 | * now be dropped. | |
3797 | */ | |
0a7de745 | 3798 | return 0; |
39037602 A |
3799 | } |
3800 | ||
2d21ac55 A |
3801 | struct mbuf * |
3802 | m_free(struct mbuf *m) | |
3803 | { | |
3804 | struct mbuf *n = m->m_next; | |
3805 | ||
0a7de745 | 3806 | if (m->m_type == MT_FREE) { |
2d21ac55 | 3807 | panic("m_free: freeing an already freed mbuf"); |
0a7de745 | 3808 | } |
2d21ac55 | 3809 | |
2d21ac55 | 3810 | if (m->m_flags & M_PKTHDR) { |
39236c6e A |
3811 | /* Check for scratch area overflow */ |
3812 | m_redzone_verify(m); | |
3813 | /* Free the aux data and tags if there is any */ | |
2d21ac55 | 3814 | m_tag_delete_chain(m, NULL); |
39037602 A |
3815 | |
3816 | m_do_tx_compl_callback(m, NULL); | |
2d21ac55 A |
3817 | } |
3818 | ||
3819 | if (m->m_flags & M_EXT) { | |
2a1bd2d3 A |
3820 | uint16_t refcnt; |
3821 | uint32_t composite; | |
813fb2f6 | 3822 | m_ext_free_func_t m_free_func; |
2d21ac55 | 3823 | |
0a7de745 A |
3824 | if (MBUF_IS_PAIRED(m) && m_free_paired(m)) { |
3825 | return n; | |
3826 | } | |
39037602 | 3827 | |
2d21ac55 | 3828 | refcnt = m_decref(m); |
6d2010ae | 3829 | composite = (MEXT_FLAGS(m) & EXTF_COMPOSITE); |
813fb2f6 | 3830 | m_free_func = m_get_ext_free(m); |
39037602 A |
3831 | |
3832 | if (refcnt == MEXT_MINREF(m) && !composite) { | |
813fb2f6 | 3833 | if (m_free_func == NULL) { |
2d21ac55 | 3834 | mcache_free(m_cache(MC_CL), m->m_ext.ext_buf); |
813fb2f6 | 3835 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
3836 | mcache_free(m_cache(MC_BIGCL), |
3837 | m->m_ext.ext_buf); | |
813fb2f6 | 3838 | } else if (m_free_func == m_16kfree) { |
2d21ac55 A |
3839 | mcache_free(m_cache(MC_16KCL), |
3840 | m->m_ext.ext_buf); | |
3841 | } else { | |
813fb2f6 A |
3842 | (*m_free_func)(m->m_ext.ext_buf, |
3843 | m->m_ext.ext_size, m_get_ext_arg(m)); | |
2d21ac55 | 3844 | } |
813fb2f6 A |
3845 | mcache_free(ref_cache, m_get_rfa(m)); |
3846 | m_set_ext(m, NULL, NULL, NULL); | |
39037602 A |
3847 | } else if (refcnt == MEXT_MINREF(m) && composite) { |
3848 | VERIFY(!(MEXT_FLAGS(m) & EXTF_PAIRED)); | |
2d21ac55 A |
3849 | VERIFY(m->m_type != MT_FREE); |
3850 | ||
3851 | mtype_stat_dec(m->m_type); | |
3852 | mtype_stat_inc(MT_FREE); | |
3853 | ||
3854 | m->m_type = MT_FREE; | |
3855 | m->m_flags = M_EXT; | |
3856 | m->m_len = 0; | |
3857 | m->m_next = m->m_nextpkt = NULL; | |
3858 | ||
6d2010ae A |
3859 | MEXT_FLAGS(m) &= ~EXTF_READONLY; |
3860 | ||
2d21ac55 | 3861 | /* "Free" into the intermediate cache */ |
813fb2f6 | 3862 | if (m_free_func == NULL) { |
2d21ac55 | 3863 | mcache_free(m_cache(MC_MBUF_CL), m); |
813fb2f6 | 3864 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
3865 | mcache_free(m_cache(MC_MBUF_BIGCL), m); |
3866 | } else { | |
813fb2f6 | 3867 | VERIFY(m_free_func == m_16kfree); |
2d21ac55 A |
3868 | mcache_free(m_cache(MC_MBUF_16KCL), m); |
3869 | } | |
0a7de745 | 3870 | return n; |
2d21ac55 A |
3871 | } |
3872 | } | |
3873 | ||
3874 | if (m->m_type != MT_FREE) { | |
3875 | mtype_stat_dec(m->m_type); | |
3876 | mtype_stat_inc(MT_FREE); | |
3877 | } | |
3878 | ||
3879 | m->m_type = MT_FREE; | |
3880 | m->m_flags = m->m_len = 0; | |
3881 | m->m_next = m->m_nextpkt = NULL; | |
3882 | ||
3883 | mcache_free(m_cache(MC_MBUF), m); | |
3884 | ||
0a7de745 | 3885 | return n; |
2d21ac55 A |
3886 | } |
3887 | ||
3888 | __private_extern__ struct mbuf * | |
3889 | m_clattach(struct mbuf *m, int type, caddr_t extbuf, | |
3890 | void (*extfree)(caddr_t, u_int, caddr_t), u_int extsize, caddr_t extarg, | |
39037602 | 3891 | int wait, int pair) |
2d21ac55 A |
3892 | { |
3893 | struct ext_ref *rfa = NULL; | |
3894 | ||
39037602 A |
3895 | /* |
3896 | * If pairing is requested and an existing mbuf is provided, reject | |
3897 | * it if it's already been paired to another cluster. Otherwise, | |
3898 | * allocate a new one or free any existing below. | |
3899 | */ | |
3900 | if ((m != NULL && MBUF_IS_PAIRED(m)) || | |
0a7de745 A |
3901 | (m == NULL && (m = _M_GETHDR(wait, type)) == NULL)) { |
3902 | return NULL; | |
3903 | } | |
2d21ac55 A |
3904 | |
3905 | if (m->m_flags & M_EXT) { | |
39037602 | 3906 | u_int16_t refcnt; |
6d2010ae | 3907 | u_int32_t composite; |
813fb2f6 | 3908 | m_ext_free_func_t m_free_func; |
2d21ac55 A |
3909 | |
3910 | refcnt = m_decref(m); | |
6d2010ae | 3911 | composite = (MEXT_FLAGS(m) & EXTF_COMPOSITE); |
39037602 | 3912 | VERIFY(!(MEXT_FLAGS(m) & EXTF_PAIRED) && MEXT_PMBUF(m) == NULL); |
813fb2f6 | 3913 | m_free_func = m_get_ext_free(m); |
39037602 | 3914 | if (refcnt == MEXT_MINREF(m) && !composite) { |
813fb2f6 | 3915 | if (m_free_func == NULL) { |
2d21ac55 | 3916 | mcache_free(m_cache(MC_CL), m->m_ext.ext_buf); |
813fb2f6 | 3917 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
3918 | mcache_free(m_cache(MC_BIGCL), |
3919 | m->m_ext.ext_buf); | |
813fb2f6 | 3920 | } else if (m_free_func == m_16kfree) { |
2d21ac55 A |
3921 | mcache_free(m_cache(MC_16KCL), |
3922 | m->m_ext.ext_buf); | |
3923 | } else { | |
813fb2f6 A |
3924 | (*m_free_func)(m->m_ext.ext_buf, |
3925 | m->m_ext.ext_size, m_get_ext_arg(m)); | |
2d21ac55 A |
3926 | } |
3927 | /* Re-use the reference structure */ | |
813fb2f6 | 3928 | rfa = m_get_rfa(m); |
39037602 | 3929 | } else if (refcnt == MEXT_MINREF(m) && composite) { |
2d21ac55 A |
3930 | VERIFY(m->m_type != MT_FREE); |
3931 | ||
3932 | mtype_stat_dec(m->m_type); | |
3933 | mtype_stat_inc(MT_FREE); | |
3934 | ||
3935 | m->m_type = MT_FREE; | |
3936 | m->m_flags = M_EXT; | |
3937 | m->m_len = 0; | |
3938 | m->m_next = m->m_nextpkt = NULL; | |
6d2010ae A |
3939 | |
3940 | MEXT_FLAGS(m) &= ~EXTF_READONLY; | |
3941 | ||
2d21ac55 | 3942 | /* "Free" into the intermediate cache */ |
813fb2f6 | 3943 | if (m_free_func == NULL) { |
2d21ac55 | 3944 | mcache_free(m_cache(MC_MBUF_CL), m); |
813fb2f6 | 3945 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
3946 | mcache_free(m_cache(MC_MBUF_BIGCL), m); |
3947 | } else { | |
813fb2f6 | 3948 | VERIFY(m_free_func == m_16kfree); |
2d21ac55 A |
3949 | mcache_free(m_cache(MC_MBUF_16KCL), m); |
3950 | } | |
3951 | /* | |
3952 | * Allocate a new mbuf, since we didn't divorce | |
3953 | * the composite mbuf + cluster pair above. | |
3954 | */ | |
0a7de745 A |
3955 | if ((m = _M_GETHDR(wait, type)) == NULL) { |
3956 | return NULL; | |
3957 | } | |
2d21ac55 A |
3958 | } |
3959 | } | |
3960 | ||
3961 | if (rfa == NULL && | |
3962 | (rfa = mcache_alloc(ref_cache, MSLEEPF(wait))) == NULL) { | |
3963 | m_free(m); | |
0a7de745 | 3964 | return NULL; |
2d21ac55 A |
3965 | } |
3966 | ||
39037602 A |
3967 | if (!pair) { |
3968 | MEXT_INIT(m, extbuf, extsize, extfree, extarg, rfa, | |
3969 | 0, 1, 0, 0, 0, NULL); | |
3970 | } else { | |
3971 | MEXT_INIT(m, extbuf, extsize, extfree, (caddr_t)m, rfa, | |
3972 | 1, 1, 1, EXTF_PAIRED, 0, m); | |
3973 | } | |
2d21ac55 | 3974 | |
0a7de745 | 3975 | return m; |
2d21ac55 A |
3976 | } |
3977 | ||
b0d623f7 A |
3978 | /* |
3979 | * Perform `fast' allocation mbuf clusters from a cache of recently-freed | |
3980 | * clusters. (If the cache is empty, new clusters are allocated en-masse.) | |
3981 | */ | |
3982 | struct mbuf * | |
3983 | m_getcl(int wait, int type, int flags) | |
3984 | { | |
3985 | struct mbuf *m; | |
3986 | int mcflags = MSLEEPF(wait); | |
3987 | int hdr = (flags & M_PKTHDR); | |
3988 | ||
3989 | /* Is this due to a non-blocking retry? If so, then try harder */ | |
0a7de745 | 3990 | if (mcflags & MCR_NOSLEEP) { |
b0d623f7 | 3991 | mcflags |= MCR_TRYHARD; |
0a7de745 | 3992 | } |
b0d623f7 | 3993 | |
6d2010ae A |
3994 | m = mcache_alloc(m_cache(MC_MBUF_CL), mcflags); |
3995 | if (m != NULL) { | |
39037602 | 3996 | u_int16_t flag; |
6d2010ae A |
3997 | struct ext_ref *rfa; |
3998 | void *cl; | |
3999 | ||
4000 | VERIFY(m->m_type == MT_FREE && m->m_flags == M_EXT); | |
4001 | cl = m->m_ext.ext_buf; | |
813fb2f6 | 4002 | rfa = m_get_rfa(m); |
6d2010ae A |
4003 | |
4004 | ASSERT(cl != NULL && rfa != NULL); | |
813fb2f6 | 4005 | VERIFY(MBUF_IS_COMPOSITE(m) && m_get_ext_free(m) == NULL); |
6d2010ae A |
4006 | |
4007 | flag = MEXT_FLAGS(m); | |
4008 | ||
b0d623f7 | 4009 | MBUF_INIT(m, hdr, type); |
6d2010ae A |
4010 | MBUF_CL_INIT(m, cl, rfa, 1, flag); |
4011 | ||
b0d623f7 A |
4012 | mtype_stat_inc(type); |
4013 | mtype_stat_dec(MT_FREE); | |
b0d623f7 | 4014 | } |
0a7de745 | 4015 | return m; |
b0d623f7 A |
4016 | } |
4017 | ||
2d21ac55 A |
4018 | /* m_mclget() add an mbuf cluster to a normal mbuf */ |
4019 | struct mbuf * | |
4020 | m_mclget(struct mbuf *m, int wait) | |
4021 | { | |
4022 | struct ext_ref *rfa; | |
4023 | ||
0a7de745 A |
4024 | if ((rfa = mcache_alloc(ref_cache, MSLEEPF(wait))) == NULL) { |
4025 | return m; | |
4026 | } | |
2d21ac55 A |
4027 | |
4028 | m->m_ext.ext_buf = m_mclalloc(wait); | |
4029 | if (m->m_ext.ext_buf != NULL) { | |
4030 | MBUF_CL_INIT(m, m->m_ext.ext_buf, rfa, 1, 0); | |
4031 | } else { | |
4032 | mcache_free(ref_cache, rfa); | |
4033 | } | |
0a7de745 | 4034 | return m; |
2d21ac55 A |
4035 | } |
4036 | ||
4037 | /* Allocate an mbuf cluster */ | |
4038 | caddr_t | |
4039 | m_mclalloc(int wait) | |
4040 | { | |
4041 | int mcflags = MSLEEPF(wait); | |
4042 | ||
4043 | /* Is this due to a non-blocking retry? If so, then try harder */ | |
0a7de745 | 4044 | if (mcflags & MCR_NOSLEEP) { |
2d21ac55 | 4045 | mcflags |= MCR_TRYHARD; |
0a7de745 | 4046 | } |
2d21ac55 | 4047 | |
0a7de745 | 4048 | return mcache_alloc(m_cache(MC_CL), mcflags); |
2d21ac55 A |
4049 | } |
4050 | ||
4051 | /* Free an mbuf cluster */ | |
4052 | void | |
4053 | m_mclfree(caddr_t p) | |
4054 | { | |
4055 | mcache_free(m_cache(MC_CL), p); | |
4056 | } | |
4057 | ||
4058 | /* | |
4059 | * mcl_hasreference() checks if a cluster of an mbuf is referenced by | |
6d2010ae | 4060 | * another mbuf; see comments in m_incref() regarding EXTF_READONLY. |
2d21ac55 A |
4061 | */ |
4062 | int | |
4063 | m_mclhasreference(struct mbuf *m) | |
4064 | { | |
0a7de745 A |
4065 | if (!(m->m_flags & M_EXT)) { |
4066 | return 0; | |
4067 | } | |
9bccf70c | 4068 | |
813fb2f6 | 4069 | ASSERT(m_get_rfa(m) != NULL); |
2d21ac55 | 4070 | |
0a7de745 | 4071 | return (MEXT_FLAGS(m) & EXTF_READONLY) ? 1 : 0; |
9bccf70c A |
4072 | } |
4073 | ||
2d21ac55 A |
4074 | __private_extern__ caddr_t |
4075 | m_bigalloc(int wait) | |
9bccf70c | 4076 | { |
2d21ac55 | 4077 | int mcflags = MSLEEPF(wait); |
91447636 | 4078 | |
2d21ac55 | 4079 | /* Is this due to a non-blocking retry? If so, then try harder */ |
0a7de745 | 4080 | if (mcflags & MCR_NOSLEEP) { |
2d21ac55 | 4081 | mcflags |= MCR_TRYHARD; |
0a7de745 | 4082 | } |
91447636 | 4083 | |
0a7de745 | 4084 | return mcache_alloc(m_cache(MC_BIGCL), mcflags); |
9bccf70c A |
4085 | } |
4086 | ||
2d21ac55 A |
4087 | __private_extern__ void |
4088 | m_bigfree(caddr_t p, __unused u_int size, __unused caddr_t arg) | |
9bccf70c | 4089 | { |
2d21ac55 | 4090 | mcache_free(m_cache(MC_BIGCL), p); |
9bccf70c A |
4091 | } |
4092 | ||
2d21ac55 A |
4093 | /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */ |
4094 | __private_extern__ struct mbuf * | |
4095 | m_mbigget(struct mbuf *m, int wait) | |
4096 | { | |
4097 | struct ext_ref *rfa; | |
4098 | ||
0a7de745 A |
4099 | if ((rfa = mcache_alloc(ref_cache, MSLEEPF(wait))) == NULL) { |
4100 | return m; | |
4101 | } | |
2d21ac55 A |
4102 | |
4103 | m->m_ext.ext_buf = m_bigalloc(wait); | |
4104 | if (m->m_ext.ext_buf != NULL) { | |
4105 | MBUF_BIGCL_INIT(m, m->m_ext.ext_buf, rfa, 1, 0); | |
91447636 | 4106 | } else { |
2d21ac55 | 4107 | mcache_free(ref_cache, rfa); |
91447636 | 4108 | } |
0a7de745 | 4109 | return m; |
2d21ac55 A |
4110 | } |
4111 | ||
4112 | __private_extern__ caddr_t | |
4113 | m_16kalloc(int wait) | |
4114 | { | |
4115 | int mcflags = MSLEEPF(wait); | |
4116 | ||
4117 | /* Is this due to a non-blocking retry? If so, then try harder */ | |
0a7de745 | 4118 | if (mcflags & MCR_NOSLEEP) { |
2d21ac55 | 4119 | mcflags |= MCR_TRYHARD; |
0a7de745 | 4120 | } |
2d21ac55 | 4121 | |
0a7de745 | 4122 | return mcache_alloc(m_cache(MC_16KCL), mcflags); |
91447636 A |
4123 | } |
4124 | ||
4125 | __private_extern__ void | |
2d21ac55 | 4126 | m_16kfree(caddr_t p, __unused u_int size, __unused caddr_t arg) |
91447636 | 4127 | { |
2d21ac55 | 4128 | mcache_free(m_cache(MC_16KCL), p); |
91447636 A |
4129 | } |
4130 | ||
2d21ac55 | 4131 | /* m_m16kget() add a 16KB mbuf cluster to a normal mbuf */ |
91447636 | 4132 | __private_extern__ struct mbuf * |
2d21ac55 | 4133 | m_m16kget(struct mbuf *m, int wait) |
91447636 | 4134 | { |
2d21ac55 A |
4135 | struct ext_ref *rfa; |
4136 | ||
0a7de745 A |
4137 | if ((rfa = mcache_alloc(ref_cache, MSLEEPF(wait))) == NULL) { |
4138 | return m; | |
4139 | } | |
2d21ac55 A |
4140 | |
4141 | m->m_ext.ext_buf = m_16kalloc(wait); | |
4142 | if (m->m_ext.ext_buf != NULL) { | |
4143 | MBUF_16KCL_INIT(m, m->m_ext.ext_buf, rfa, 1, 0); | |
4144 | } else { | |
4145 | mcache_free(ref_cache, rfa); | |
91447636 | 4146 | } |
0a7de745 | 4147 | return m; |
91447636 A |
4148 | } |
4149 | ||
b0d623f7 A |
4150 | /* |
4151 | * "Move" mbuf pkthdr from "from" to "to". | |
4152 | * "from" must have M_PKTHDR set, and "to" must be empty. | |
4153 | */ | |
9bccf70c | 4154 | void |
2d21ac55 | 4155 | m_copy_pkthdr(struct mbuf *to, struct mbuf *from) |
9bccf70c | 4156 | { |
39236c6e A |
4157 | VERIFY(from->m_flags & M_PKTHDR); |
4158 | ||
4159 | /* Check for scratch area overflow */ | |
4160 | m_redzone_verify(from); | |
4161 | ||
4162 | if (to->m_flags & M_PKTHDR) { | |
4163 | /* Check for scratch area overflow */ | |
4164 | m_redzone_verify(to); | |
4165 | /* We will be taking over the tags of 'to' */ | |
2d21ac55 | 4166 | m_tag_delete_chain(to, NULL); |
39236c6e | 4167 | } |
0a7de745 A |
4168 | to->m_pkthdr = from->m_pkthdr; /* especially tags */ |
4169 | m_classifier_init(from, 0); /* purge classifier info */ | |
4170 | m_tag_init(from, 1); /* purge all tags from src */ | |
4171 | m_scratch_init(from); /* clear src scratch area */ | |
935ed37a | 4172 | to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); |
0a7de745 | 4173 | if ((to->m_flags & M_EXT) == 0) { |
935ed37a | 4174 | to->m_data = to->m_pktdat; |
0a7de745 A |
4175 | } |
4176 | m_redzone_init(to); /* setup red zone on dst */ | |
9bccf70c A |
4177 | } |
4178 | ||
91447636 A |
4179 | /* |
4180 | * Duplicate "from"'s mbuf pkthdr in "to". | |
4181 | * "from" must have M_PKTHDR set, and "to" must be empty. | |
4182 | * In particular, this does a deep copy of the packet tags. | |
4183 | */ | |
3a60a9f5 | 4184 | static int |
91447636 A |
4185 | m_dup_pkthdr(struct mbuf *to, struct mbuf *from, int how) |
4186 | { | |
39236c6e A |
4187 | VERIFY(from->m_flags & M_PKTHDR); |
4188 | ||
4189 | /* Check for scratch area overflow */ | |
4190 | m_redzone_verify(from); | |
4191 | ||
4192 | if (to->m_flags & M_PKTHDR) { | |
4193 | /* Check for scratch area overflow */ | |
4194 | m_redzone_verify(to); | |
4195 | /* We will be taking over the tags of 'to' */ | |
2d21ac55 | 4196 | m_tag_delete_chain(to, NULL); |
39236c6e | 4197 | } |
2d21ac55 | 4198 | to->m_flags = (from->m_flags & M_COPYFLAGS) | (to->m_flags & M_EXT); |
0a7de745 | 4199 | if ((to->m_flags & M_EXT) == 0) { |
2d21ac55 | 4200 | to->m_data = to->m_pktdat; |
0a7de745 | 4201 | } |
2d21ac55 | 4202 | to->m_pkthdr = from->m_pkthdr; |
0a7de745 A |
4203 | m_redzone_init(to); /* setup red zone on dst */ |
4204 | m_tag_init(to, 0); /* preserve dst static tags */ | |
4205 | return m_tag_copy_chain(to, from, how); | |
91447636 | 4206 | } |
fa4905b1 | 4207 | |
316670eb A |
4208 | void |
4209 | m_copy_pftag(struct mbuf *to, struct mbuf *from) | |
4210 | { | |
39037602 | 4211 | memcpy(m_pftag(to), m_pftag(from), sizeof(struct pf_mtag)); |
39236c6e | 4212 | #if PF_ECN |
39037602 | 4213 | m_pftag(to)->pftag_hdr = NULL; |
0a7de745 | 4214 | m_pftag(to)->pftag_flags &= ~(PF_TAG_HDR_INET | PF_TAG_HDR_INET6); |
39236c6e A |
4215 | #endif /* PF_ECN */ |
4216 | } | |
4217 | ||
2a1bd2d3 A |
4218 | void |
4219 | m_copy_necptag(struct mbuf *to, struct mbuf *from) | |
4220 | { | |
4221 | memcpy(m_necptag(to), m_necptag(from), sizeof(struct necp_mtag_)); | |
4222 | } | |
4223 | ||
39236c6e A |
4224 | void |
4225 | m_classifier_init(struct mbuf *m, uint32_t pktf_mask) | |
4226 | { | |
4227 | VERIFY(m->m_flags & M_PKTHDR); | |
4228 | ||
4229 | m->m_pkthdr.pkt_proto = 0; | |
4230 | m->m_pkthdr.pkt_flowsrc = 0; | |
4231 | m->m_pkthdr.pkt_flowid = 0; | |
0a7de745 | 4232 | m->m_pkthdr.pkt_flags &= pktf_mask; /* caller-defined mask */ |
39236c6e | 4233 | /* preserve service class and interface info for loopback packets */ |
0a7de745 | 4234 | if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP)) { |
39236c6e | 4235 | (void) m_set_service_class(m, MBUF_SC_BE); |
0a7de745 A |
4236 | } |
4237 | if (!(m->m_pkthdr.pkt_flags & PKTF_IFAINFO)) { | |
39236c6e | 4238 | m->m_pkthdr.pkt_ifainfo = 0; |
0a7de745 | 4239 | } |
5ba3f43e A |
4240 | /* |
4241 | * Preserve timestamp if requested | |
4242 | */ | |
0a7de745 | 4243 | if (!(m->m_pkthdr.pkt_flags & PKTF_TS_VALID)) { |
5ba3f43e | 4244 | m->m_pkthdr.pkt_timestamp = 0; |
0a7de745 | 4245 | } |
39236c6e A |
4246 | } |
4247 | ||
4248 | void | |
4249 | m_copy_classifier(struct mbuf *to, struct mbuf *from) | |
4250 | { | |
4251 | VERIFY(to->m_flags & M_PKTHDR); | |
4252 | VERIFY(from->m_flags & M_PKTHDR); | |
4253 | ||
4254 | to->m_pkthdr.pkt_proto = from->m_pkthdr.pkt_proto; | |
4255 | to->m_pkthdr.pkt_flowsrc = from->m_pkthdr.pkt_flowsrc; | |
4256 | to->m_pkthdr.pkt_flowid = from->m_pkthdr.pkt_flowid; | |
4257 | to->m_pkthdr.pkt_flags = from->m_pkthdr.pkt_flags; | |
4258 | (void) m_set_service_class(to, from->m_pkthdr.pkt_svc); | |
4259 | to->m_pkthdr.pkt_ifainfo = from->m_pkthdr.pkt_ifainfo; | |
316670eb A |
4260 | } |
4261 | ||
9bccf70c | 4262 | /* |
2d21ac55 A |
4263 | * Return a list of mbuf hdrs that point to clusters. Try for num_needed; |
4264 | * if wantall is not set, return whatever number were available. Set up the | |
4265 | * first num_with_pkthdrs with mbuf hdrs configured as packet headers; these | |
4266 | * are chained on the m_nextpkt field. Any packets requested beyond this | |
4267 | * are chained onto the last packet header's m_next field. The size of | |
4268 | * the cluster is controlled by the parameter bufsize. | |
9bccf70c | 4269 | */ |
91447636 | 4270 | __private_extern__ struct mbuf * |
2d21ac55 A |
4271 | m_getpackets_internal(unsigned int *num_needed, int num_with_pkthdrs, |
4272 | int wait, int wantall, size_t bufsize) | |
fa4905b1 A |
4273 | { |
4274 | struct mbuf *m; | |
4275 | struct mbuf **np, *top; | |
2d21ac55 A |
4276 | unsigned int pnum, needed = *num_needed; |
4277 | mcache_obj_t *mp_list = NULL; | |
4278 | int mcflags = MSLEEPF(wait); | |
39037602 | 4279 | u_int16_t flag; |
2d21ac55 A |
4280 | struct ext_ref *rfa; |
4281 | mcache_t *cp; | |
4282 | void *cl; | |
4283 | ||
4284 | ASSERT(bufsize == m_maxsize(MC_CL) || | |
4285 | bufsize == m_maxsize(MC_BIGCL) || | |
4286 | bufsize == m_maxsize(MC_16KCL)); | |
4287 | ||
4288 | /* | |
4289 | * Caller must first check for njcl because this | |
4290 | * routine is internal and not exposed/used via KPI. | |
4291 | */ | |
4292 | VERIFY(bufsize != m_maxsize(MC_16KCL) || njcl > 0); | |
4293 | ||
fa4905b1 A |
4294 | top = NULL; |
4295 | np = ⊤ | |
2d21ac55 | 4296 | pnum = 0; |
fa4905b1 | 4297 | |
2d21ac55 A |
4298 | /* |
4299 | * The caller doesn't want all the requested buffers; only some. | |
4300 | * Try hard to get what we can, but don't block. This effectively | |
4301 | * overrides MCR_SLEEP, since this thread will not go to sleep | |
4302 | * if we can't get all the buffers. | |
4303 | */ | |
0a7de745 | 4304 | if (!wantall || (mcflags & MCR_NOSLEEP)) { |
2d21ac55 | 4305 | mcflags |= MCR_TRYHARD; |
0a7de745 | 4306 | } |
2d21ac55 A |
4307 | |
4308 | /* Allocate the composite mbuf + cluster elements from the cache */ | |
0a7de745 | 4309 | if (bufsize == m_maxsize(MC_CL)) { |
2d21ac55 | 4310 | cp = m_cache(MC_MBUF_CL); |
0a7de745 | 4311 | } else if (bufsize == m_maxsize(MC_BIGCL)) { |
2d21ac55 | 4312 | cp = m_cache(MC_MBUF_BIGCL); |
0a7de745 | 4313 | } else { |
2d21ac55 | 4314 | cp = m_cache(MC_MBUF_16KCL); |
0a7de745 | 4315 | } |
2d21ac55 A |
4316 | needed = mcache_alloc_ext(cp, &mp_list, needed, mcflags); |
4317 | ||
4318 | for (pnum = 0; pnum < needed; pnum++) { | |
4319 | m = (struct mbuf *)mp_list; | |
4320 | mp_list = mp_list->obj_next; | |
4321 | ||
4322 | VERIFY(m->m_type == MT_FREE && m->m_flags == M_EXT); | |
4323 | cl = m->m_ext.ext_buf; | |
813fb2f6 | 4324 | rfa = m_get_rfa(m); |
2d21ac55 A |
4325 | |
4326 | ASSERT(cl != NULL && rfa != NULL); | |
4327 | VERIFY(MBUF_IS_COMPOSITE(m)); | |
4328 | ||
4329 | flag = MEXT_FLAGS(m); | |
4330 | ||
4331 | MBUF_INIT(m, num_with_pkthdrs, MT_DATA); | |
4332 | if (bufsize == m_maxsize(MC_16KCL)) { | |
4333 | MBUF_16KCL_INIT(m, cl, rfa, 1, flag); | |
4334 | } else if (bufsize == m_maxsize(MC_BIGCL)) { | |
4335 | MBUF_BIGCL_INIT(m, cl, rfa, 1, flag); | |
91447636 | 4336 | } else { |
2d21ac55 A |
4337 | MBUF_CL_INIT(m, cl, rfa, 1, flag); |
4338 | } | |
4339 | ||
4340 | if (num_with_pkthdrs > 0) { | |
4341 | --num_with_pkthdrs; | |
91447636 | 4342 | } |
2d21ac55 A |
4343 | |
4344 | *np = m; | |
0a7de745 | 4345 | if (num_with_pkthdrs > 0) { |
91447636 | 4346 | np = &m->m_nextpkt; |
0a7de745 | 4347 | } else { |
91447636 | 4348 | np = &m->m_next; |
0a7de745 | 4349 | } |
91447636 | 4350 | } |
2d21ac55 | 4351 | ASSERT(pnum != *num_needed || mp_list == NULL); |
0a7de745 | 4352 | if (mp_list != NULL) { |
2d21ac55 | 4353 | mcache_free_ext(cp, mp_list); |
0a7de745 | 4354 | } |
2d21ac55 A |
4355 | |
4356 | if (pnum > 0) { | |
4357 | mtype_stat_add(MT_DATA, pnum); | |
4358 | mtype_stat_sub(MT_FREE, pnum); | |
4359 | } | |
4360 | ||
4361 | if (wantall && (pnum != *num_needed)) { | |
0a7de745 | 4362 | if (top != NULL) { |
2d21ac55 | 4363 | m_freem_list(top); |
0a7de745 A |
4364 | } |
4365 | return NULL; | |
91447636 | 4366 | } |
fa4905b1 | 4367 | |
316670eb A |
4368 | if (pnum > *num_needed) { |
4369 | printf("%s: File a radar related to <rdar://10146739>. \ | |
4370 | needed = %u, pnum = %u, num_needed = %u \n", | |
0a7de745 | 4371 | __func__, needed, pnum, *num_needed); |
39037602 | 4372 | } |
316670eb | 4373 | |
2d21ac55 | 4374 | *num_needed = pnum; |
0a7de745 | 4375 | return top; |
2d21ac55 | 4376 | } |
fa4905b1 | 4377 | |
91447636 | 4378 | /* |
2d21ac55 A |
4379 | * Return list of mbuf linked by m_nextpkt. Try for numlist, and if |
4380 | * wantall is not set, return whatever number were available. The size of | |
4381 | * each mbuf in the list is controlled by the parameter packetlen. Each | |
4382 | * mbuf of the list may have a chain of mbufs linked by m_next. Each mbuf | |
4383 | * in the chain is called a segment. If maxsegments is not null and the | |
4384 | * value pointed to is not null, this specify the maximum number of segments | |
4385 | * for a chain of mbufs. If maxsegments is zero or the value pointed to | |
4386 | * is zero the caller does not have any restriction on the number of segments. | |
4387 | * The actual number of segments of a mbuf chain is return in the value | |
4388 | * pointed to by maxsegments. | |
91447636 | 4389 | */ |
91447636 | 4390 | __private_extern__ struct mbuf * |
2d21ac55 A |
4391 | m_allocpacket_internal(unsigned int *numlist, size_t packetlen, |
4392 | unsigned int *maxsegments, int wait, int wantall, size_t wantsize) | |
91447636 | 4393 | { |
2d21ac55 A |
4394 | struct mbuf **np, *top, *first = NULL; |
4395 | size_t bufsize, r_bufsize; | |
4396 | unsigned int num = 0; | |
4397 | unsigned int nsegs = 0; | |
4398 | unsigned int needed, resid; | |
4399 | int mcflags = MSLEEPF(wait); | |
4400 | mcache_obj_t *mp_list = NULL, *rmp_list = NULL; | |
4401 | mcache_t *cp = NULL, *rcp = NULL; | |
4402 | ||
0a7de745 A |
4403 | if (*numlist == 0) { |
4404 | return NULL; | |
4405 | } | |
fa4905b1 | 4406 | |
91447636 A |
4407 | top = NULL; |
4408 | np = ⊤ | |
2d21ac55 | 4409 | |
91447636 | 4410 | if (wantsize == 0) { |
2d21ac55 | 4411 | if (packetlen <= MINCLSIZE) { |
91447636 | 4412 | bufsize = packetlen; |
2d21ac55 A |
4413 | } else if (packetlen > m_maxsize(MC_CL)) { |
4414 | /* Use 4KB if jumbo cluster pool isn't available */ | |
0a7de745 | 4415 | if (packetlen <= m_maxsize(MC_BIGCL) || njcl == 0) { |
2d21ac55 | 4416 | bufsize = m_maxsize(MC_BIGCL); |
0a7de745 | 4417 | } else { |
2d21ac55 | 4418 | bufsize = m_maxsize(MC_16KCL); |
0a7de745 | 4419 | } |
2d21ac55 A |
4420 | } else { |
4421 | bufsize = m_maxsize(MC_CL); | |
4422 | } | |
4423 | } else if (wantsize == m_maxsize(MC_CL) || | |
4424 | wantsize == m_maxsize(MC_BIGCL) || | |
4425 | (wantsize == m_maxsize(MC_16KCL) && njcl > 0)) { | |
91447636 | 4426 | bufsize = wantsize; |
2d21ac55 | 4427 | } else { |
f427ee49 | 4428 | *numlist = 0; |
0a7de745 | 4429 | return NULL; |
2d21ac55 | 4430 | } |
91447636 A |
4431 | |
4432 | if (bufsize <= MHLEN) { | |
2d21ac55 | 4433 | nsegs = 1; |
91447636 A |
4434 | } else if (bufsize <= MINCLSIZE) { |
4435 | if (maxsegments != NULL && *maxsegments == 1) { | |
2d21ac55 A |
4436 | bufsize = m_maxsize(MC_CL); |
4437 | nsegs = 1; | |
91447636 | 4438 | } else { |
2d21ac55 | 4439 | nsegs = 2; |
fa4905b1 | 4440 | } |
2d21ac55 A |
4441 | } else if (bufsize == m_maxsize(MC_16KCL)) { |
4442 | VERIFY(njcl > 0); | |
3e170ce0 | 4443 | nsegs = ((packetlen - 1) >> M16KCLSHIFT) + 1; |
2d21ac55 | 4444 | } else if (bufsize == m_maxsize(MC_BIGCL)) { |
3e170ce0 | 4445 | nsegs = ((packetlen - 1) >> MBIGCLSHIFT) + 1; |
91447636 | 4446 | } else { |
2d21ac55 | 4447 | nsegs = ((packetlen - 1) >> MCLSHIFT) + 1; |
91447636 A |
4448 | } |
4449 | if (maxsegments != NULL) { | |
2d21ac55 A |
4450 | if (*maxsegments && nsegs > *maxsegments) { |
4451 | *maxsegments = nsegs; | |
f427ee49 | 4452 | *numlist = 0; |
0a7de745 | 4453 | return NULL; |
91447636 | 4454 | } |
2d21ac55 | 4455 | *maxsegments = nsegs; |
91447636 | 4456 | } |
91447636 | 4457 | |
2d21ac55 A |
4458 | /* |
4459 | * The caller doesn't want all the requested buffers; only some. | |
4460 | * Try hard to get what we can, but don't block. This effectively | |
4461 | * overrides MCR_SLEEP, since this thread will not go to sleep | |
4462 | * if we can't get all the buffers. | |
4463 | */ | |
0a7de745 | 4464 | if (!wantall || (mcflags & MCR_NOSLEEP)) { |
2d21ac55 | 4465 | mcflags |= MCR_TRYHARD; |
0a7de745 | 4466 | } |
2d21ac55 A |
4467 | |
4468 | /* | |
4469 | * Simple case where all elements in the lists/chains are mbufs. | |
4470 | * Unless bufsize is greater than MHLEN, each segment chain is made | |
4471 | * up of exactly 1 mbuf. Otherwise, each segment chain is made up | |
4472 | * of 2 mbufs; the second one is used for the residual data, i.e. | |
4473 | * the remaining data that cannot fit into the first mbuf. | |
4474 | */ | |
4475 | if (bufsize <= MINCLSIZE) { | |
4476 | /* Allocate the elements in one shot from the mbuf cache */ | |
4477 | ASSERT(bufsize <= MHLEN || nsegs == 2); | |
4478 | cp = m_cache(MC_MBUF); | |
4479 | needed = mcache_alloc_ext(cp, &mp_list, | |
4480 | (*numlist) * nsegs, mcflags); | |
4481 | ||
4482 | /* | |
4483 | * The number of elements must be even if we are to use an | |
4484 | * mbuf (instead of a cluster) to store the residual data. | |
4485 | * If we couldn't allocate the requested number of mbufs, | |
4486 | * trim the number down (if it's odd) in order to avoid | |
4487 | * creating a partial segment chain. | |
4488 | */ | |
0a7de745 | 4489 | if (bufsize > MHLEN && (needed & 0x1)) { |
2d21ac55 | 4490 | needed--; |
0a7de745 | 4491 | } |
91447636 | 4492 | |
2d21ac55 A |
4493 | while (num < needed) { |
4494 | struct mbuf *m; | |
91447636 | 4495 | |
2d21ac55 A |
4496 | m = (struct mbuf *)mp_list; |
4497 | mp_list = mp_list->obj_next; | |
4498 | ASSERT(m != NULL); | |
91447636 | 4499 | |
2d21ac55 | 4500 | MBUF_INIT(m, 1, MT_DATA); |
2d21ac55 A |
4501 | num++; |
4502 | if (bufsize > MHLEN) { | |
4503 | /* A second mbuf for this segment chain */ | |
4504 | m->m_next = (struct mbuf *)mp_list; | |
4505 | mp_list = mp_list->obj_next; | |
4506 | ASSERT(m->m_next != NULL); | |
4507 | ||
4508 | MBUF_INIT(m->m_next, 0, MT_DATA); | |
4509 | num++; | |
91447636 | 4510 | } |
2d21ac55 A |
4511 | *np = m; |
4512 | np = &m->m_nextpkt; | |
4513 | } | |
4514 | ASSERT(num != *numlist || mp_list == NULL); | |
4515 | ||
4516 | if (num > 0) { | |
4517 | mtype_stat_add(MT_DATA, num); | |
4518 | mtype_stat_sub(MT_FREE, num); | |
4519 | } | |
4520 | num /= nsegs; | |
4521 | ||
4522 | /* We've got them all; return to caller */ | |
0a7de745 A |
4523 | if (num == *numlist) { |
4524 | return top; | |
4525 | } | |
2d21ac55 A |
4526 | |
4527 | goto fail; | |
4528 | } | |
4529 | ||
4530 | /* | |
4531 | * Complex cases where elements are made up of one or more composite | |
4532 | * mbufs + cluster, depending on packetlen. Each N-segment chain can | |
4533 | * be illustrated as follows: | |
4534 | * | |
4535 | * [mbuf + cluster 1] [mbuf + cluster 2] ... [mbuf + cluster N] | |
4536 | * | |
4537 | * Every composite mbuf + cluster element comes from the intermediate | |
4538 | * cache (either MC_MBUF_CL or MC_MBUF_BIGCL). For space efficiency, | |
4539 | * the last composite element will come from the MC_MBUF_CL cache, | |
4540 | * unless the residual data is larger than 2KB where we use the | |
4541 | * big cluster composite cache (MC_MBUF_BIGCL) instead. Residual | |
4542 | * data is defined as extra data beyond the first element that cannot | |
4543 | * fit into the previous element, i.e. there is no residual data if | |
4544 | * the chain only has 1 segment. | |
4545 | */ | |
4546 | r_bufsize = bufsize; | |
4547 | resid = packetlen > bufsize ? packetlen % bufsize : 0; | |
4548 | if (resid > 0) { | |
4549 | /* There is residual data; figure out the cluster size */ | |
4550 | if (wantsize == 0 && packetlen > MINCLSIZE) { | |
4551 | /* | |
4552 | * Caller didn't request that all of the segments | |
4553 | * in the chain use the same cluster size; use the | |
4554 | * smaller of the cluster sizes. | |
4555 | */ | |
0a7de745 | 4556 | if (njcl > 0 && resid > m_maxsize(MC_BIGCL)) { |
2d21ac55 | 4557 | r_bufsize = m_maxsize(MC_16KCL); |
0a7de745 | 4558 | } else if (resid > m_maxsize(MC_CL)) { |
2d21ac55 | 4559 | r_bufsize = m_maxsize(MC_BIGCL); |
0a7de745 | 4560 | } else { |
2d21ac55 | 4561 | r_bufsize = m_maxsize(MC_CL); |
0a7de745 | 4562 | } |
2d21ac55 A |
4563 | } else { |
4564 | /* Use the same cluster size as the other segments */ | |
4565 | resid = 0; | |
4566 | } | |
4567 | } | |
4568 | ||
4569 | needed = *numlist; | |
4570 | if (resid > 0) { | |
4571 | /* | |
4572 | * Attempt to allocate composite mbuf + cluster elements for | |
4573 | * the residual data in each chain; record the number of such | |
4574 | * elements that can be allocated so that we know how many | |
4575 | * segment chains we can afford to create. | |
4576 | */ | |
0a7de745 | 4577 | if (r_bufsize <= m_maxsize(MC_CL)) { |
2d21ac55 | 4578 | rcp = m_cache(MC_MBUF_CL); |
0a7de745 | 4579 | } else if (r_bufsize <= m_maxsize(MC_BIGCL)) { |
2d21ac55 | 4580 | rcp = m_cache(MC_MBUF_BIGCL); |
0a7de745 | 4581 | } else { |
2d21ac55 | 4582 | rcp = m_cache(MC_MBUF_16KCL); |
0a7de745 | 4583 | } |
2d21ac55 A |
4584 | needed = mcache_alloc_ext(rcp, &rmp_list, *numlist, mcflags); |
4585 | ||
0a7de745 | 4586 | if (needed == 0) { |
2d21ac55 | 4587 | goto fail; |
0a7de745 | 4588 | } |
2d21ac55 A |
4589 | |
4590 | /* This is temporarily reduced for calculation */ | |
4591 | ASSERT(nsegs > 1); | |
4592 | nsegs--; | |
4593 | } | |
4594 | ||
4595 | /* | |
4596 | * Attempt to allocate the rest of the composite mbuf + cluster | |
4597 | * elements for the number of segment chains that we need. | |
4598 | */ | |
0a7de745 | 4599 | if (bufsize <= m_maxsize(MC_CL)) { |
2d21ac55 | 4600 | cp = m_cache(MC_MBUF_CL); |
0a7de745 | 4601 | } else if (bufsize <= m_maxsize(MC_BIGCL)) { |
2d21ac55 | 4602 | cp = m_cache(MC_MBUF_BIGCL); |
0a7de745 | 4603 | } else { |
2d21ac55 | 4604 | cp = m_cache(MC_MBUF_16KCL); |
0a7de745 | 4605 | } |
2d21ac55 A |
4606 | needed = mcache_alloc_ext(cp, &mp_list, needed * nsegs, mcflags); |
4607 | ||
4608 | /* Round it down to avoid creating a partial segment chain */ | |
4609 | needed = (needed / nsegs) * nsegs; | |
0a7de745 | 4610 | if (needed == 0) { |
2d21ac55 | 4611 | goto fail; |
0a7de745 | 4612 | } |
2d21ac55 A |
4613 | |
4614 | if (resid > 0) { | |
4615 | /* | |
4616 | * We're about to construct the chain(s); take into account | |
4617 | * the number of segments we have created above to hold the | |
4618 | * residual data for each chain, as well as restore the | |
4619 | * original count of segments per chain. | |
4620 | */ | |
4621 | ASSERT(nsegs > 0); | |
4622 | needed += needed / nsegs; | |
4623 | nsegs++; | |
4624 | } | |
4625 | ||
4626 | for (;;) { | |
4627 | struct mbuf *m; | |
39037602 | 4628 | u_int16_t flag; |
2d21ac55 A |
4629 | struct ext_ref *rfa; |
4630 | void *cl; | |
4631 | int pkthdr; | |
813fb2f6 | 4632 | m_ext_free_func_t m_free_func; |
2d21ac55 A |
4633 | |
4634 | ++num; | |
4635 | if (nsegs == 1 || (num % nsegs) != 0 || resid == 0) { | |
4636 | m = (struct mbuf *)mp_list; | |
4637 | mp_list = mp_list->obj_next; | |
4638 | } else { | |
4639 | m = (struct mbuf *)rmp_list; | |
4640 | rmp_list = rmp_list->obj_next; | |
4641 | } | |
813fb2f6 | 4642 | m_free_func = m_get_ext_free(m); |
2d21ac55 A |
4643 | ASSERT(m != NULL); |
4644 | VERIFY(m->m_type == MT_FREE && m->m_flags == M_EXT); | |
813fb2f6 A |
4645 | VERIFY(m_free_func == NULL || m_free_func == m_bigfree || |
4646 | m_free_func == m_16kfree); | |
2d21ac55 A |
4647 | |
4648 | cl = m->m_ext.ext_buf; | |
813fb2f6 | 4649 | rfa = m_get_rfa(m); |
2d21ac55 A |
4650 | |
4651 | ASSERT(cl != NULL && rfa != NULL); | |
4652 | VERIFY(MBUF_IS_COMPOSITE(m)); | |
4653 | ||
4654 | flag = MEXT_FLAGS(m); | |
4655 | ||
4656 | pkthdr = (nsegs == 1 || (num % nsegs) == 1); | |
0a7de745 | 4657 | if (pkthdr) { |
2d21ac55 | 4658 | first = m; |
0a7de745 | 4659 | } |
2d21ac55 | 4660 | MBUF_INIT(m, pkthdr, MT_DATA); |
813fb2f6 | 4661 | if (m_free_func == m_16kfree) { |
2d21ac55 | 4662 | MBUF_16KCL_INIT(m, cl, rfa, 1, flag); |
813fb2f6 | 4663 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
4664 | MBUF_BIGCL_INIT(m, cl, rfa, 1, flag); |
4665 | } else { | |
4666 | MBUF_CL_INIT(m, cl, rfa, 1, flag); | |
4667 | } | |
2d21ac55 A |
4668 | |
4669 | *np = m; | |
0a7de745 | 4670 | if ((num % nsegs) == 0) { |
2d21ac55 | 4671 | np = &first->m_nextpkt; |
0a7de745 | 4672 | } else { |
2d21ac55 | 4673 | np = &m->m_next; |
0a7de745 | 4674 | } |
2d21ac55 | 4675 | |
0a7de745 | 4676 | if (num == needed) { |
2d21ac55 | 4677 | break; |
0a7de745 | 4678 | } |
2d21ac55 A |
4679 | } |
4680 | ||
4681 | if (num > 0) { | |
4682 | mtype_stat_add(MT_DATA, num); | |
4683 | mtype_stat_sub(MT_FREE, num); | |
91447636 | 4684 | } |
2d21ac55 A |
4685 | |
4686 | num /= nsegs; | |
4687 | ||
4688 | /* We've got them all; return to caller */ | |
4689 | if (num == *numlist) { | |
4690 | ASSERT(mp_list == NULL && rmp_list == NULL); | |
0a7de745 | 4691 | return top; |
2d21ac55 A |
4692 | } |
4693 | ||
91447636 | 4694 | fail: |
2d21ac55 | 4695 | /* Free up what's left of the above */ |
0a7de745 | 4696 | if (mp_list != NULL) { |
2d21ac55 | 4697 | mcache_free_ext(cp, mp_list); |
0a7de745 A |
4698 | } |
4699 | if (rmp_list != NULL) { | |
2d21ac55 | 4700 | mcache_free_ext(rcp, rmp_list); |
0a7de745 | 4701 | } |
2d21ac55 | 4702 | if (wantall && top != NULL) { |
cb323159 | 4703 | m_freem_list(top); |
f427ee49 | 4704 | *numlist = 0; |
0a7de745 | 4705 | return NULL; |
91447636 | 4706 | } |
2d21ac55 | 4707 | *numlist = num; |
0a7de745 | 4708 | return top; |
91447636 | 4709 | } |
fa4905b1 | 4710 | |
2d21ac55 A |
4711 | /* |
4712 | * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated | |
4713 | * packets on receive ring. | |
91447636 A |
4714 | */ |
4715 | __private_extern__ struct mbuf * | |
2d21ac55 | 4716 | m_getpacket_how(int wait) |
91447636 A |
4717 | { |
4718 | unsigned int num_needed = 1; | |
2d21ac55 | 4719 | |
0a7de745 A |
4720 | return m_getpackets_internal(&num_needed, 1, wait, 1, |
4721 | m_maxsize(MC_CL)); | |
91447636 | 4722 | } |
fa4905b1 | 4723 | |
2d21ac55 A |
4724 | /* |
4725 | * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated | |
4726 | * packets on receive ring. | |
91447636 A |
4727 | */ |
4728 | struct mbuf * | |
4729 | m_getpacket(void) | |
4730 | { | |
4731 | unsigned int num_needed = 1; | |
9bccf70c | 4732 | |
0a7de745 A |
4733 | return m_getpackets_internal(&num_needed, 1, M_WAIT, 1, |
4734 | m_maxsize(MC_CL)); | |
91447636 | 4735 | } |
fa4905b1 | 4736 | |
91447636 | 4737 | /* |
2d21ac55 A |
4738 | * Return a list of mbuf hdrs that point to clusters. Try for num_needed; |
4739 | * if this can't be met, return whatever number were available. Set up the | |
4740 | * first num_with_pkthdrs with mbuf hdrs configured as packet headers. These | |
4741 | * are chained on the m_nextpkt field. Any packets requested beyond this are | |
4742 | * chained onto the last packet header's m_next field. | |
91447636 A |
4743 | */ |
4744 | struct mbuf * | |
4745 | m_getpackets(int num_needed, int num_with_pkthdrs, int how) | |
4746 | { | |
4747 | unsigned int n = num_needed; | |
fa4905b1 | 4748 | |
0a7de745 A |
4749 | return m_getpackets_internal(&n, num_with_pkthdrs, how, 0, |
4750 | m_maxsize(MC_CL)); | |
2d21ac55 | 4751 | } |
fa4905b1 | 4752 | |
9bccf70c | 4753 | /* |
2d21ac55 A |
4754 | * Return a list of mbuf hdrs set up as packet hdrs chained together |
4755 | * on the m_nextpkt field | |
9bccf70c | 4756 | */ |
fa4905b1 A |
4757 | struct mbuf * |
4758 | m_getpackethdrs(int num_needed, int how) | |
4759 | { | |
4760 | struct mbuf *m; | |
4761 | struct mbuf **np, *top; | |
4762 | ||
4763 | top = NULL; | |
4764 | np = ⊤ | |
4765 | ||
fa4905b1 | 4766 | while (num_needed--) { |
2d21ac55 | 4767 | m = _M_RETRYHDR(how, MT_DATA); |
0a7de745 | 4768 | if (m == NULL) { |
2d21ac55 | 4769 | break; |
0a7de745 | 4770 | } |
2d21ac55 A |
4771 | |
4772 | *np = m; | |
4773 | np = &m->m_nextpkt; | |
4774 | } | |
fa4905b1 | 4775 | |
0a7de745 | 4776 | return top; |
fa4905b1 A |
4777 | } |
4778 | ||
2d21ac55 A |
4779 | /* |
4780 | * Free an mbuf list (m_nextpkt) while following m_next. Returns the count | |
4781 | * for mbufs packets freed. Used by the drivers. | |
1c79356b | 4782 | */ |
2d21ac55 A |
4783 | int |
4784 | m_freem_list(struct mbuf *m) | |
1c79356b A |
4785 | { |
4786 | struct mbuf *nextpkt; | |
2d21ac55 A |
4787 | mcache_obj_t *mp_list = NULL; |
4788 | mcache_obj_t *mcl_list = NULL; | |
4789 | mcache_obj_t *mbc_list = NULL; | |
4790 | mcache_obj_t *m16k_list = NULL; | |
4791 | mcache_obj_t *m_mcl_list = NULL; | |
4792 | mcache_obj_t *m_mbc_list = NULL; | |
4793 | mcache_obj_t *m_m16k_list = NULL; | |
4794 | mcache_obj_t *ref_list = NULL; | |
4795 | int pktcount = 0; | |
4796 | int mt_free = 0, mt_data = 0, mt_header = 0, mt_soname = 0, mt_tag = 0; | |
4797 | ||
4798 | while (m != NULL) { | |
4799 | pktcount++; | |
4800 | ||
4801 | nextpkt = m->m_nextpkt; | |
4802 | m->m_nextpkt = NULL; | |
4803 | ||
4804 | while (m != NULL) { | |
4805 | struct mbuf *next = m->m_next; | |
4806 | mcache_obj_t *o, *rfa; | |
39037602 A |
4807 | u_int32_t composite; |
4808 | u_int16_t refcnt; | |
813fb2f6 | 4809 | m_ext_free_func_t m_free_func; |
fa4905b1 | 4810 | |
0a7de745 | 4811 | if (m->m_type == MT_FREE) { |
2d21ac55 | 4812 | panic("m_free: freeing an already freed mbuf"); |
0a7de745 | 4813 | } |
9bccf70c | 4814 | |
2d21ac55 | 4815 | if (m->m_flags & M_PKTHDR) { |
39236c6e A |
4816 | /* Check for scratch area overflow */ |
4817 | m_redzone_verify(m); | |
4818 | /* Free the aux data and tags if there is any */ | |
91447636 | 4819 | m_tag_delete_chain(m, NULL); |
91447636 | 4820 | } |
9bccf70c | 4821 | |
39037602 A |
4822 | if (!(m->m_flags & M_EXT)) { |
4823 | mt_free++; | |
2d21ac55 | 4824 | goto simple_free; |
39037602 A |
4825 | } |
4826 | ||
4827 | if (MBUF_IS_PAIRED(m) && m_free_paired(m)) { | |
4828 | m = next; | |
4829 | continue; | |
4830 | } | |
4831 | ||
4832 | mt_free++; | |
2d21ac55 | 4833 | |
316670eb | 4834 | o = (mcache_obj_t *)(void *)m->m_ext.ext_buf; |
2d21ac55 | 4835 | refcnt = m_decref(m); |
6d2010ae | 4836 | composite = (MEXT_FLAGS(m) & EXTF_COMPOSITE); |
813fb2f6 | 4837 | m_free_func = m_get_ext_free(m); |
39037602 | 4838 | if (refcnt == MEXT_MINREF(m) && !composite) { |
813fb2f6 | 4839 | if (m_free_func == NULL) { |
2d21ac55 A |
4840 | o->obj_next = mcl_list; |
4841 | mcl_list = o; | |
813fb2f6 | 4842 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
4843 | o->obj_next = mbc_list; |
4844 | mbc_list = o; | |
813fb2f6 | 4845 | } else if (m_free_func == m_16kfree) { |
2d21ac55 A |
4846 | o->obj_next = m16k_list; |
4847 | m16k_list = o; | |
4848 | } else { | |
813fb2f6 | 4849 | (*(m_free_func))((caddr_t)o, |
2d21ac55 | 4850 | m->m_ext.ext_size, |
813fb2f6 | 4851 | m_get_ext_arg(m)); |
2d21ac55 | 4852 | } |
813fb2f6 | 4853 | rfa = (mcache_obj_t *)(void *)m_get_rfa(m); |
2d21ac55 A |
4854 | rfa->obj_next = ref_list; |
4855 | ref_list = rfa; | |
813fb2f6 | 4856 | m_set_ext(m, NULL, NULL, NULL); |
39037602 A |
4857 | } else if (refcnt == MEXT_MINREF(m) && composite) { |
4858 | VERIFY(!(MEXT_FLAGS(m) & EXTF_PAIRED)); | |
2d21ac55 A |
4859 | VERIFY(m->m_type != MT_FREE); |
4860 | /* | |
4861 | * Amortize the costs of atomic operations | |
4862 | * by doing them at the end, if possible. | |
4863 | */ | |
0a7de745 | 4864 | if (m->m_type == MT_DATA) { |
2d21ac55 | 4865 | mt_data++; |
0a7de745 | 4866 | } else if (m->m_type == MT_HEADER) { |
2d21ac55 | 4867 | mt_header++; |
0a7de745 | 4868 | } else if (m->m_type == MT_SONAME) { |
2d21ac55 | 4869 | mt_soname++; |
0a7de745 | 4870 | } else if (m->m_type == MT_TAG) { |
2d21ac55 | 4871 | mt_tag++; |
0a7de745 | 4872 | } else { |
2d21ac55 | 4873 | mtype_stat_dec(m->m_type); |
0a7de745 | 4874 | } |
fa4905b1 | 4875 | |
2d21ac55 A |
4876 | m->m_type = MT_FREE; |
4877 | m->m_flags = M_EXT; | |
4878 | m->m_len = 0; | |
4879 | m->m_next = m->m_nextpkt = NULL; | |
4880 | ||
6d2010ae A |
4881 | MEXT_FLAGS(m) &= ~EXTF_READONLY; |
4882 | ||
2d21ac55 A |
4883 | /* "Free" into the intermediate cache */ |
4884 | o = (mcache_obj_t *)m; | |
813fb2f6 | 4885 | if (m_free_func == NULL) { |
2d21ac55 A |
4886 | o->obj_next = m_mcl_list; |
4887 | m_mcl_list = o; | |
813fb2f6 | 4888 | } else if (m_free_func == m_bigfree) { |
2d21ac55 A |
4889 | o->obj_next = m_mbc_list; |
4890 | m_mbc_list = o; | |
1c79356b | 4891 | } else { |
813fb2f6 | 4892 | VERIFY(m_free_func == m_16kfree); |
2d21ac55 A |
4893 | o->obj_next = m_m16k_list; |
4894 | m_m16k_list = o; | |
1c79356b | 4895 | } |
2d21ac55 A |
4896 | m = next; |
4897 | continue; | |
1c79356b | 4898 | } |
2d21ac55 A |
4899 | simple_free: |
4900 | /* | |
4901 | * Amortize the costs of atomic operations | |
4902 | * by doing them at the end, if possible. | |
4903 | */ | |
0a7de745 | 4904 | if (m->m_type == MT_DATA) { |
2d21ac55 | 4905 | mt_data++; |
0a7de745 | 4906 | } else if (m->m_type == MT_HEADER) { |
2d21ac55 | 4907 | mt_header++; |
0a7de745 | 4908 | } else if (m->m_type == MT_SONAME) { |
2d21ac55 | 4909 | mt_soname++; |
0a7de745 | 4910 | } else if (m->m_type == MT_TAG) { |
2d21ac55 | 4911 | mt_tag++; |
0a7de745 | 4912 | } else if (m->m_type != MT_FREE) { |
2d21ac55 | 4913 | mtype_stat_dec(m->m_type); |
0a7de745 | 4914 | } |
2d21ac55 | 4915 | |
1c79356b | 4916 | m->m_type = MT_FREE; |
2d21ac55 A |
4917 | m->m_flags = m->m_len = 0; |
4918 | m->m_next = m->m_nextpkt = NULL; | |
fa4905b1 | 4919 | |
2d21ac55 A |
4920 | ((mcache_obj_t *)m)->obj_next = mp_list; |
4921 | mp_list = (mcache_obj_t *)m; | |
4922 | ||
4923 | m = next; | |
4924 | } | |
fa4905b1 | 4925 | |
2d21ac55 A |
4926 | m = nextpkt; |
4927 | } | |
fa4905b1 | 4928 | |
0a7de745 | 4929 | if (mt_free > 0) { |
2d21ac55 | 4930 | mtype_stat_add(MT_FREE, mt_free); |
0a7de745 A |
4931 | } |
4932 | if (mt_data > 0) { | |
2d21ac55 | 4933 | mtype_stat_sub(MT_DATA, mt_data); |
0a7de745 A |
4934 | } |
4935 | if (mt_header > 0) { | |
2d21ac55 | 4936 | mtype_stat_sub(MT_HEADER, mt_header); |
0a7de745 A |
4937 | } |
4938 | if (mt_soname > 0) { | |
2d21ac55 | 4939 | mtype_stat_sub(MT_SONAME, mt_soname); |
0a7de745 A |
4940 | } |
4941 | if (mt_tag > 0) { | |
2d21ac55 | 4942 | mtype_stat_sub(MT_TAG, mt_tag); |
0a7de745 | 4943 | } |
2d21ac55 | 4944 | |
0a7de745 | 4945 | if (mp_list != NULL) { |
2d21ac55 | 4946 | mcache_free_ext(m_cache(MC_MBUF), mp_list); |
0a7de745 A |
4947 | } |
4948 | if (mcl_list != NULL) { | |
2d21ac55 | 4949 | mcache_free_ext(m_cache(MC_CL), mcl_list); |
0a7de745 A |
4950 | } |
4951 | if (mbc_list != NULL) { | |
2d21ac55 | 4952 | mcache_free_ext(m_cache(MC_BIGCL), mbc_list); |
0a7de745 A |
4953 | } |
4954 | if (m16k_list != NULL) { | |
2d21ac55 | 4955 | mcache_free_ext(m_cache(MC_16KCL), m16k_list); |
0a7de745 A |
4956 | } |
4957 | if (m_mcl_list != NULL) { | |
2d21ac55 | 4958 | mcache_free_ext(m_cache(MC_MBUF_CL), m_mcl_list); |
0a7de745 A |
4959 | } |
4960 | if (m_mbc_list != NULL) { | |
2d21ac55 | 4961 | mcache_free_ext(m_cache(MC_MBUF_BIGCL), m_mbc_list); |
0a7de745 A |
4962 | } |
4963 | if (m_m16k_list != NULL) { | |
2d21ac55 | 4964 | mcache_free_ext(m_cache(MC_MBUF_16KCL), m_m16k_list); |
0a7de745 A |
4965 | } |
4966 | if (ref_list != NULL) { | |
2d21ac55 | 4967 | mcache_free_ext(ref_cache, ref_list); |
0a7de745 | 4968 | } |
2d21ac55 | 4969 | |
0a7de745 | 4970 | return pktcount; |
1c79356b A |
4971 | } |
4972 | ||
4973 | void | |
2d21ac55 | 4974 | m_freem(struct mbuf *m) |
1c79356b | 4975 | { |
0a7de745 | 4976 | while (m != NULL) { |
1c79356b | 4977 | m = m_free(m); |
0a7de745 | 4978 | } |
1c79356b A |
4979 | } |
4980 | ||
4981 | /* | |
4982 | * Mbuffer utility routines. | |
4983 | */ | |
4984 | /* | |
d9a64523 A |
4985 | * Set the m_data pointer of a newly allocated mbuf to place an object of the |
4986 | * specified size at the end of the mbuf, longword aligned. | |
4987 | * | |
4988 | * NB: Historically, we had M_ALIGN(), MH_ALIGN(), and MEXT_ALIGN() as | |
4989 | * separate macros, each asserting that it was called at the proper moment. | |
4990 | * This required callers to themselves test the storage type and call the | |
4991 | * right one. Rather than require callers to be aware of those layout | |
4992 | * decisions, we centralize here. | |
1c79356b | 4993 | */ |
d9a64523 A |
4994 | void |
4995 | m_align(struct mbuf *m, int len) | |
1c79356b | 4996 | { |
d9a64523 | 4997 | int adjust = 0; |
1c79356b | 4998 | |
d9a64523 A |
4999 | /* At this point data must point to start */ |
5000 | VERIFY(m->m_data == M_START(m)); | |
5001 | VERIFY(len >= 0); | |
5002 | VERIFY(len <= M_SIZE(m)); | |
5003 | adjust = M_SIZE(m) - len; | |
0a7de745 | 5004 | m->m_data += adjust & ~(sizeof(long) - 1); |
1c79356b A |
5005 | } |
5006 | ||
5007 | /* | |
2d21ac55 A |
5008 | * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain, |
5009 | * copy junk along. Does not adjust packet header length. | |
1c79356b A |
5010 | */ |
5011 | struct mbuf * | |
2d21ac55 | 5012 | m_prepend(struct mbuf *m, int len, int how) |
1c79356b A |
5013 | { |
5014 | struct mbuf *mn; | |
5015 | ||
2d21ac55 A |
5016 | _MGET(mn, how, m->m_type); |
5017 | if (mn == NULL) { | |
1c79356b | 5018 | m_freem(m); |
0a7de745 | 5019 | return NULL; |
1c79356b A |
5020 | } |
5021 | if (m->m_flags & M_PKTHDR) { | |
5022 | M_COPY_PKTHDR(mn, m); | |
5023 | m->m_flags &= ~M_PKTHDR; | |
5024 | } | |
5025 | mn->m_next = m; | |
5026 | m = mn; | |
3e170ce0 A |
5027 | if (m->m_flags & M_PKTHDR) { |
5028 | VERIFY(len <= MHLEN); | |
1c79356b | 5029 | MH_ALIGN(m, len); |
3e170ce0 A |
5030 | } else { |
5031 | VERIFY(len <= MLEN); | |
5032 | M_ALIGN(m, len); | |
5033 | } | |
1c79356b | 5034 | m->m_len = len; |
0a7de745 | 5035 | return m; |
1c79356b A |
5036 | } |
5037 | ||
9bccf70c | 5038 | /* |
2d21ac55 A |
5039 | * Replacement for old M_PREPEND macro: allocate new mbuf to prepend to |
5040 | * chain, copy junk along, and adjust length. | |
9bccf70c A |
5041 | */ |
5042 | struct mbuf * | |
3e170ce0 | 5043 | m_prepend_2(struct mbuf *m, int len, int how, int align) |
2d21ac55 | 5044 | { |
3e170ce0 A |
5045 | if (M_LEADINGSPACE(m) >= len && |
5046 | (!align || IS_P2ALIGNED((m->m_data - len), sizeof(u_int32_t)))) { | |
2d21ac55 A |
5047 | m->m_data -= len; |
5048 | m->m_len += len; | |
5049 | } else { | |
9bccf70c | 5050 | m = m_prepend(m, len, how); |
2d21ac55 | 5051 | } |
0a7de745 | 5052 | if ((m) && (m->m_flags & M_PKTHDR)) { |
2d21ac55 | 5053 | m->m_pkthdr.len += len; |
0a7de745 A |
5054 | } |
5055 | return m; | |
9bccf70c A |
5056 | } |
5057 | ||
1c79356b A |
5058 | /* |
5059 | * Make a copy of an mbuf chain starting "off0" bytes from the beginning, | |
5060 | * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf. | |
5061 | * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller. | |
5062 | */ | |
5063 | int MCFail; | |
5064 | ||
5065 | struct mbuf * | |
39236c6e | 5066 | m_copym_mode(struct mbuf *m, int off0, int len, int wait, uint32_t mode) |
1c79356b | 5067 | { |
2d21ac55 | 5068 | struct mbuf *n, *mhdr = NULL, **np; |
91447636 | 5069 | int off = off0; |
1c79356b A |
5070 | struct mbuf *top; |
5071 | int copyhdr = 0; | |
5072 | ||
0a7de745 | 5073 | if (off < 0 || len < 0) { |
2d21ac55 | 5074 | panic("m_copym: invalid offset %d or len %d", off, len); |
0a7de745 | 5075 | } |
2d21ac55 | 5076 | |
fe8ab488 A |
5077 | VERIFY((mode != M_COPYM_MUST_COPY_HDR && |
5078 | mode != M_COPYM_MUST_MOVE_HDR) || (m->m_flags & M_PKTHDR)); | |
5079 | ||
5080 | if ((off == 0 && (m->m_flags & M_PKTHDR)) || | |
5081 | mode == M_COPYM_MUST_COPY_HDR || mode == M_COPYM_MUST_MOVE_HDR) { | |
2d21ac55 | 5082 | mhdr = m; |
1c79356b | 5083 | copyhdr = 1; |
2d21ac55 | 5084 | } |
fa4905b1 A |
5085 | |
5086 | while (off >= m->m_len) { | |
0a7de745 | 5087 | if (m->m_next == NULL) { |
2d21ac55 | 5088 | panic("m_copym: invalid mbuf chain"); |
0a7de745 | 5089 | } |
1c79356b A |
5090 | off -= m->m_len; |
5091 | m = m->m_next; | |
5092 | } | |
5093 | np = ⊤ | |
2d21ac55 | 5094 | top = NULL; |
fa4905b1 | 5095 | |
1c79356b | 5096 | while (len > 0) { |
2d21ac55 | 5097 | if (m == NULL) { |
0a7de745 | 5098 | if (len != M_COPYALL) { |
2d21ac55 | 5099 | panic("m_copym: len != M_COPYALL"); |
0a7de745 | 5100 | } |
1c79356b A |
5101 | break; |
5102 | } | |
2d21ac55 | 5103 | |
0a7de745 | 5104 | if (copyhdr) { |
fe8ab488 | 5105 | n = _M_RETRYHDR(wait, m->m_type); |
0a7de745 | 5106 | } else { |
fe8ab488 | 5107 | n = _M_RETRY(wait, m->m_type); |
0a7de745 | 5108 | } |
1c79356b | 5109 | *np = n; |
fa4905b1 | 5110 | |
0a7de745 | 5111 | if (n == NULL) { |
1c79356b | 5112 | goto nospace; |
0a7de745 | 5113 | } |
2d21ac55 A |
5114 | |
5115 | if (copyhdr != 0) { | |
fe8ab488 A |
5116 | if ((mode == M_COPYM_MOVE_HDR) || |
5117 | (mode == M_COPYM_MUST_MOVE_HDR)) { | |
39236c6e | 5118 | M_COPY_PKTHDR(n, mhdr); |
fe8ab488 A |
5119 | } else if ((mode == M_COPYM_COPY_HDR) || |
5120 | (mode == M_COPYM_MUST_COPY_HDR)) { | |
0a7de745 | 5121 | if (m_dup_pkthdr(n, mhdr, wait) == 0) { |
39236c6e | 5122 | goto nospace; |
0a7de745 | 5123 | } |
39236c6e | 5124 | } |
0a7de745 | 5125 | if (len == M_COPYALL) { |
1c79356b | 5126 | n->m_pkthdr.len -= off0; |
0a7de745 | 5127 | } else { |
1c79356b | 5128 | n->m_pkthdr.len = len; |
0a7de745 | 5129 | } |
1c79356b | 5130 | copyhdr = 0; |
fe8ab488 A |
5131 | /* |
5132 | * There is data to copy from the packet header mbuf | |
5133 | * if it is empty or it is before the starting offset | |
5134 | */ | |
5135 | if (mhdr != m) { | |
5136 | np = &n->m_next; | |
5137 | continue; | |
2d21ac55 | 5138 | } |
1c79356b | 5139 | } |
2d21ac55 | 5140 | n->m_len = MIN(len, (m->m_len - off)); |
1c79356b | 5141 | if (m->m_flags & M_EXT) { |
1c79356b | 5142 | n->m_ext = m->m_ext; |
2d21ac55 | 5143 | m_incref(m); |
1c79356b A |
5144 | n->m_data = m->m_data + off; |
5145 | n->m_flags |= M_EXT; | |
fa4905b1 | 5146 | } else { |
fe8ab488 A |
5147 | /* |
5148 | * Limit to the capacity of the destination | |
5149 | */ | |
0a7de745 | 5150 | if (n->m_flags & M_PKTHDR) { |
fe8ab488 | 5151 | n->m_len = MIN(n->m_len, MHLEN); |
0a7de745 | 5152 | } else { |
fe8ab488 | 5153 | n->m_len = MIN(n->m_len, MLEN); |
0a7de745 | 5154 | } |
fe8ab488 | 5155 | |
0a7de745 | 5156 | if (MTOD(n, char *) + n->m_len > ((char *)n) + MSIZE) { |
39037602 | 5157 | panic("%s n %p copy overflow", |
0a7de745 A |
5158 | __func__, n); |
5159 | } | |
fe8ab488 | 5160 | |
0a7de745 | 5161 | bcopy(MTOD(m, caddr_t) + off, MTOD(n, caddr_t), |
1c79356b | 5162 | (unsigned)n->m_len); |
fa4905b1 | 5163 | } |
0a7de745 | 5164 | if (len != M_COPYALL) { |
1c79356b | 5165 | len -= n->m_len; |
0a7de745 | 5166 | } |
1c79356b A |
5167 | off = 0; |
5168 | m = m->m_next; | |
5169 | np = &n->m_next; | |
5170 | } | |
fa4905b1 | 5171 | |
0a7de745 | 5172 | if (top == NULL) { |
1c79356b | 5173 | MCFail++; |
0a7de745 | 5174 | } |
fa4905b1 | 5175 | |
0a7de745 | 5176 | return top; |
1c79356b | 5177 | nospace: |
fa4905b1 | 5178 | |
1c79356b A |
5179 | m_freem(top); |
5180 | MCFail++; | |
0a7de745 | 5181 | return NULL; |
1c79356b A |
5182 | } |
5183 | ||
39236c6e A |
5184 | |
5185 | struct mbuf * | |
5186 | m_copym(struct mbuf *m, int off0, int len, int wait) | |
5187 | { | |
0a7de745 | 5188 | return m_copym_mode(m, off0, len, wait, M_COPYM_MOVE_HDR); |
39236c6e A |
5189 | } |
5190 | ||
9bccf70c | 5191 | /* |
2d21ac55 A |
5192 | * Equivalent to m_copym except that all necessary mbuf hdrs are allocated |
5193 | * within this routine also, the last mbuf and offset accessed are passed | |
5194 | * out and can be passed back in to avoid having to rescan the entire mbuf | |
5195 | * list (normally hung off of the socket) | |
9bccf70c | 5196 | */ |
fa4905b1 | 5197 | struct mbuf * |
fe8ab488 | 5198 | m_copym_with_hdrs(struct mbuf *m0, int off0, int len0, int wait, |
39236c6e | 5199 | struct mbuf **m_lastm, int *m_off, uint32_t mode) |
2d21ac55 | 5200 | { |
fe8ab488 | 5201 | struct mbuf *m = m0, *n, **np = NULL; |
2d21ac55 A |
5202 | int off = off0, len = len0; |
5203 | struct mbuf *top = NULL; | |
5204 | int mcflags = MSLEEPF(wait); | |
fa4905b1 | 5205 | int copyhdr = 0; |
2d21ac55 A |
5206 | int type = 0; |
5207 | mcache_obj_t *list = NULL; | |
5208 | int needed = 0; | |
fa4905b1 | 5209 | |
0a7de745 | 5210 | if (off == 0 && (m->m_flags & M_PKTHDR)) { |
fa4905b1 | 5211 | copyhdr = 1; |
0a7de745 | 5212 | } |
39037602 | 5213 | |
fe8ab488 | 5214 | if (m_lastm != NULL && *m_lastm != NULL) { |
6d2010ae | 5215 | m = *m_lastm; |
fa4905b1 A |
5216 | off = *m_off; |
5217 | } else { | |
2d21ac55 A |
5218 | while (off >= m->m_len) { |
5219 | off -= m->m_len; | |
fa4905b1 A |
5220 | m = m->m_next; |
5221 | } | |
5222 | } | |
91447636 | 5223 | |
2d21ac55 A |
5224 | n = m; |
5225 | while (len > 0) { | |
5226 | needed++; | |
5227 | ASSERT(n != NULL); | |
5228 | len -= MIN(len, (n->m_len - ((needed == 1) ? off : 0))); | |
5229 | n = n->m_next; | |
5230 | } | |
5231 | needed++; | |
5232 | len = len0; | |
5233 | ||
5234 | /* | |
5235 | * If the caller doesn't want to be put to sleep, mark it with | |
5236 | * MCR_TRYHARD so that we may reclaim buffers from other places | |
5237 | * before giving up. | |
5238 | */ | |
0a7de745 | 5239 | if (mcflags & MCR_NOSLEEP) { |
2d21ac55 | 5240 | mcflags |= MCR_TRYHARD; |
0a7de745 | 5241 | } |
2d21ac55 A |
5242 | |
5243 | if (mcache_alloc_ext(m_cache(MC_MBUF), &list, needed, | |
0a7de745 | 5244 | mcflags) != needed) { |
2d21ac55 | 5245 | goto nospace; |
0a7de745 | 5246 | } |
fa4905b1 | 5247 | |
2d21ac55 | 5248 | needed = 0; |
fa4905b1 | 5249 | while (len > 0) { |
2d21ac55 A |
5250 | n = (struct mbuf *)list; |
5251 | list = list->obj_next; | |
5252 | ASSERT(n != NULL && m != NULL); | |
5253 | ||
5254 | type = (top == NULL) ? MT_HEADER : m->m_type; | |
5255 | MBUF_INIT(n, (top == NULL), type); | |
2d21ac55 A |
5256 | |
5257 | if (top == NULL) { | |
5258 | top = n; | |
fa4905b1 A |
5259 | np = &top->m_next; |
5260 | continue; | |
2d21ac55 A |
5261 | } else { |
5262 | needed++; | |
5263 | *np = n; | |
5264 | } | |
fa4905b1 A |
5265 | |
5266 | if (copyhdr) { | |
fe8ab488 A |
5267 | if ((mode == M_COPYM_MOVE_HDR) || |
5268 | (mode == M_COPYM_MUST_MOVE_HDR)) { | |
39236c6e | 5269 | M_COPY_PKTHDR(n, m); |
fe8ab488 A |
5270 | } else if ((mode == M_COPYM_COPY_HDR) || |
5271 | (mode == M_COPYM_MUST_COPY_HDR)) { | |
0a7de745 | 5272 | if (m_dup_pkthdr(n, m, wait) == 0) { |
39236c6e | 5273 | goto nospace; |
0a7de745 | 5274 | } |
39236c6e | 5275 | } |
fa4905b1 A |
5276 | n->m_pkthdr.len = len; |
5277 | copyhdr = 0; | |
5278 | } | |
2d21ac55 | 5279 | n->m_len = MIN(len, (m->m_len - off)); |
fa4905b1 A |
5280 | |
5281 | if (m->m_flags & M_EXT) { | |
5282 | n->m_ext = m->m_ext; | |
2d21ac55 | 5283 | m_incref(m); |
fa4905b1 A |
5284 | n->m_data = m->m_data + off; |
5285 | n->m_flags |= M_EXT; | |
5286 | } else { | |
0a7de745 | 5287 | if (MTOD(n, char *) + n->m_len > ((char *)n) + MSIZE) { |
39037602 | 5288 | panic("%s n %p copy overflow", |
0a7de745 A |
5289 | __func__, n); |
5290 | } | |
fe8ab488 | 5291 | |
0a7de745 | 5292 | bcopy(MTOD(m, caddr_t) + off, MTOD(n, caddr_t), |
fa4905b1 A |
5293 | (unsigned)n->m_len); |
5294 | } | |
5295 | len -= n->m_len; | |
2d21ac55 | 5296 | |
fa4905b1 | 5297 | if (len == 0) { |
fe8ab488 A |
5298 | if (m_lastm != NULL && m_off != NULL) { |
5299 | if ((off + n->m_len) == m->m_len) { | |
5300 | *m_lastm = m->m_next; | |
5301 | *m_off = 0; | |
5302 | } else { | |
5303 | *m_lastm = m; | |
5304 | *m_off = off + n->m_len; | |
5305 | } | |
fa4905b1 | 5306 | } |
2d21ac55 | 5307 | break; |
fa4905b1 A |
5308 | } |
5309 | off = 0; | |
5310 | m = m->m_next; | |
5311 | np = &n->m_next; | |
5312 | } | |
fa4905b1 | 5313 | |
2d21ac55 A |
5314 | mtype_stat_inc(MT_HEADER); |
5315 | mtype_stat_add(type, needed); | |
5316 | mtype_stat_sub(MT_FREE, needed + 1); | |
5317 | ||
5318 | ASSERT(list == NULL); | |
0a7de745 | 5319 | return top; |
fa4905b1 | 5320 | |
2d21ac55 | 5321 | nospace: |
0a7de745 | 5322 | if (list != NULL) { |
2d21ac55 | 5323 | mcache_free_ext(m_cache(MC_MBUF), list); |
0a7de745 A |
5324 | } |
5325 | if (top != NULL) { | |
2d21ac55 | 5326 | m_freem(top); |
0a7de745 | 5327 | } |
fa4905b1 | 5328 | MCFail++; |
0a7de745 | 5329 | return NULL; |
fa4905b1 A |
5330 | } |
5331 | ||
1c79356b A |
5332 | /* |
5333 | * Copy data from an mbuf chain starting "off" bytes from the beginning, | |
5334 | * continuing for "len" bytes, into the indicated buffer. | |
5335 | */ | |
2d21ac55 | 5336 | void |
b0d623f7 | 5337 | m_copydata(struct mbuf *m, int off, int len, void *vp) |
1c79356b | 5338 | { |
5ba3f43e A |
5339 | int off0 = off, len0 = len; |
5340 | struct mbuf *m0 = m; | |
91447636 | 5341 | unsigned count; |
b0d623f7 | 5342 | char *cp = vp; |
1c79356b | 5343 | |
5ba3f43e A |
5344 | if (__improbable(off < 0 || len < 0)) { |
5345 | panic("%s: invalid offset %d or len %d", __func__, off, len); | |
5346 | /* NOTREACHED */ | |
5347 | } | |
2d21ac55 | 5348 | |
1c79356b | 5349 | while (off > 0) { |
5ba3f43e A |
5350 | if (__improbable(m == NULL)) { |
5351 | panic("%s: invalid mbuf chain %p [off %d, len %d]", | |
5352 | __func__, m0, off0, len0); | |
5353 | /* NOTREACHED */ | |
5354 | } | |
0a7de745 | 5355 | if (off < m->m_len) { |
1c79356b | 5356 | break; |
0a7de745 | 5357 | } |
1c79356b A |
5358 | off -= m->m_len; |
5359 | m = m->m_next; | |
5360 | } | |
5361 | while (len > 0) { | |
5ba3f43e A |
5362 | if (__improbable(m == NULL)) { |
5363 | panic("%s: invalid mbuf chain %p [off %d, len %d]", | |
5364 | __func__, m0, off0, len0); | |
5365 | /* NOTREACHED */ | |
5366 | } | |
2d21ac55 A |
5367 | count = MIN(m->m_len - off, len); |
5368 | bcopy(MTOD(m, caddr_t) + off, cp, count); | |
1c79356b A |
5369 | len -= count; |
5370 | cp += count; | |
5371 | off = 0; | |
5372 | m = m->m_next; | |
5373 | } | |
5374 | } | |
5375 | ||
5376 | /* | |
2d21ac55 A |
5377 | * Concatenate mbuf chain n to m. Both chains must be of the same type |
5378 | * (e.g. MT_DATA). Any m_pkthdr is not updated. | |
1c79356b | 5379 | */ |
2d21ac55 A |
5380 | void |
5381 | m_cat(struct mbuf *m, struct mbuf *n) | |
1c79356b | 5382 | { |
0a7de745 | 5383 | while (m->m_next) { |
1c79356b | 5384 | m = m->m_next; |
0a7de745 | 5385 | } |
1c79356b | 5386 | while (n) { |
2d21ac55 | 5387 | if ((m->m_flags & M_EXT) || |
1c79356b A |
5388 | m->m_data + m->m_len + n->m_len >= &m->m_dat[MLEN]) { |
5389 | /* just join the two chains */ | |
5390 | m->m_next = n; | |
5391 | return; | |
5392 | } | |
5393 | /* splat the data from one into the other */ | |
2d21ac55 | 5394 | bcopy(MTOD(n, caddr_t), MTOD(m, caddr_t) + m->m_len, |
1c79356b A |
5395 | (u_int)n->m_len); |
5396 | m->m_len += n->m_len; | |
5397 | n = m_free(n); | |
5398 | } | |
5399 | } | |
5400 | ||
5401 | void | |
2d21ac55 | 5402 | m_adj(struct mbuf *mp, int req_len) |
1c79356b | 5403 | { |
91447636 A |
5404 | int len = req_len; |
5405 | struct mbuf *m; | |
5406 | int count; | |
1c79356b | 5407 | |
0a7de745 | 5408 | if ((m = mp) == NULL) { |
1c79356b | 5409 | return; |
0a7de745 | 5410 | } |
1c79356b A |
5411 | if (len >= 0) { |
5412 | /* | |
5413 | * Trim from head. | |
5414 | */ | |
5415 | while (m != NULL && len > 0) { | |
5416 | if (m->m_len <= len) { | |
5417 | len -= m->m_len; | |
5418 | m->m_len = 0; | |
5419 | m = m->m_next; | |
5420 | } else { | |
5421 | m->m_len -= len; | |
5422 | m->m_data += len; | |
5423 | len = 0; | |
5424 | } | |
5425 | } | |
5426 | m = mp; | |
0a7de745 | 5427 | if (m->m_flags & M_PKTHDR) { |
1c79356b | 5428 | m->m_pkthdr.len -= (req_len - len); |
0a7de745 | 5429 | } |
1c79356b A |
5430 | } else { |
5431 | /* | |
5432 | * Trim from tail. Scan the mbuf chain, | |
5433 | * calculating its length and finding the last mbuf. | |
5434 | * If the adjustment only affects this mbuf, then just | |
5435 | * adjust and return. Otherwise, rescan and truncate | |
5436 | * after the remaining size. | |
5437 | */ | |
5438 | len = -len; | |
5439 | count = 0; | |
5440 | for (;;) { | |
5441 | count += m->m_len; | |
0a7de745 | 5442 | if (m->m_next == (struct mbuf *)0) { |
1c79356b | 5443 | break; |
0a7de745 | 5444 | } |
1c79356b A |
5445 | m = m->m_next; |
5446 | } | |
5447 | if (m->m_len >= len) { | |
5448 | m->m_len -= len; | |
5449 | m = mp; | |
0a7de745 | 5450 | if (m->m_flags & M_PKTHDR) { |
1c79356b | 5451 | m->m_pkthdr.len -= len; |
0a7de745 | 5452 | } |
1c79356b A |
5453 | return; |
5454 | } | |
5455 | count -= len; | |
0a7de745 | 5456 | if (count < 0) { |
1c79356b | 5457 | count = 0; |
0a7de745 | 5458 | } |
1c79356b A |
5459 | /* |
5460 | * Correct length for chain is "count". | |
5461 | * Find the mbuf with last data, adjust its length, | |
5462 | * and toss data from remaining mbufs on chain. | |
5463 | */ | |
5464 | m = mp; | |
0a7de745 | 5465 | if (m->m_flags & M_PKTHDR) { |
1c79356b | 5466 | m->m_pkthdr.len = count; |
0a7de745 | 5467 | } |
1c79356b A |
5468 | for (; m; m = m->m_next) { |
5469 | if (m->m_len >= count) { | |
5470 | m->m_len = count; | |
5471 | break; | |
5472 | } | |
5473 | count -= m->m_len; | |
5474 | } | |
0a7de745 | 5475 | while ((m = m->m_next)) { |
1c79356b | 5476 | m->m_len = 0; |
0a7de745 | 5477 | } |
1c79356b A |
5478 | } |
5479 | } | |
5480 | ||
5481 | /* | |
5482 | * Rearange an mbuf chain so that len bytes are contiguous | |
5483 | * and in the data area of an mbuf (so that mtod and dtom | |
5484 | * will work for a structure of size len). Returns the resulting | |
5485 | * mbuf chain on success, frees it and returns null on failure. | |
5486 | * If there is room, it will add up to max_protohdr-len extra bytes to the | |
5487 | * contiguous region in an attempt to avoid being called next time. | |
5488 | */ | |
5489 | int MPFail; | |
5490 | ||
5491 | struct mbuf * | |
2d21ac55 | 5492 | m_pullup(struct mbuf *n, int len) |
1c79356b | 5493 | { |
91447636 A |
5494 | struct mbuf *m; |
5495 | int count; | |
1c79356b A |
5496 | int space; |
5497 | ||
a39ff7e2 A |
5498 | /* check invalid arguments */ |
5499 | if (n == NULL) { | |
0a7de745 | 5500 | panic("%s: n == NULL", __func__); |
a39ff7e2 A |
5501 | } |
5502 | if (len < 0) { | |
5503 | os_log_info(OS_LOG_DEFAULT, "%s: failed negative len %d", | |
5504 | __func__, len); | |
5505 | goto bad; | |
5506 | } | |
d9a64523 A |
5507 | if (len > MLEN) { |
5508 | os_log_info(OS_LOG_DEFAULT, "%s: failed len %d too big", | |
5509 | __func__, len); | |
5510 | goto bad; | |
5511 | } | |
5512 | if ((n->m_flags & M_EXT) == 0 && | |
5513 | n->m_data >= &n->m_dat[MLEN]) { | |
5514 | os_log_info(OS_LOG_DEFAULT, "%s: m_data out of bounds", | |
5515 | __func__); | |
5516 | goto bad; | |
5517 | } | |
a39ff7e2 | 5518 | |
1c79356b A |
5519 | /* |
5520 | * If first mbuf has no cluster, and has room for len bytes | |
5521 | * without shifting current data, pullup into it, | |
5522 | * otherwise allocate a new mbuf to prepend to the chain. | |
5523 | */ | |
5524 | if ((n->m_flags & M_EXT) == 0 && | |
d9a64523 | 5525 | len < &n->m_dat[MLEN] - n->m_data && n->m_next != NULL) { |
0a7de745 A |
5526 | if (n->m_len >= len) { |
5527 | return n; | |
5528 | } | |
1c79356b A |
5529 | m = n; |
5530 | n = n->m_next; | |
5531 | len -= m->m_len; | |
5532 | } else { | |
0a7de745 | 5533 | if (len > MHLEN) { |
1c79356b | 5534 | goto bad; |
0a7de745 | 5535 | } |
2d21ac55 | 5536 | _MGET(m, M_DONTWAIT, n->m_type); |
0a7de745 | 5537 | if (m == 0) { |
1c79356b | 5538 | goto bad; |
0a7de745 | 5539 | } |
1c79356b A |
5540 | m->m_len = 0; |
5541 | if (n->m_flags & M_PKTHDR) { | |
5542 | M_COPY_PKTHDR(m, n); | |
5543 | n->m_flags &= ~M_PKTHDR; | |
5544 | } | |
5545 | } | |
5546 | space = &m->m_dat[MLEN] - (m->m_data + m->m_len); | |
5547 | do { | |
2d21ac55 A |
5548 | count = MIN(MIN(MAX(len, max_protohdr), space), n->m_len); |
5549 | bcopy(MTOD(n, caddr_t), MTOD(m, caddr_t) + m->m_len, | |
5550 | (unsigned)count); | |
1c79356b A |
5551 | len -= count; |
5552 | m->m_len += count; | |
5553 | n->m_len -= count; | |
5554 | space -= count; | |
0a7de745 | 5555 | if (n->m_len != 0) { |
1c79356b | 5556 | n->m_data += count; |
0a7de745 | 5557 | } else { |
1c79356b | 5558 | n = m_free(n); |
0a7de745 | 5559 | } |
d9a64523 | 5560 | } while (len > 0 && n != NULL); |
1c79356b A |
5561 | if (len > 0) { |
5562 | (void) m_free(m); | |
5563 | goto bad; | |
5564 | } | |
5565 | m->m_next = n; | |
0a7de745 | 5566 | return m; |
1c79356b A |
5567 | bad: |
5568 | m_freem(n); | |
5569 | MPFail++; | |
0a7de745 | 5570 | return 0; |
1c79356b A |
5571 | } |
5572 | ||
6d2010ae A |
5573 | /* |
5574 | * Like m_pullup(), except a new mbuf is always allocated, and we allow | |
5575 | * the amount of empty space before the data in the new mbuf to be specified | |
5576 | * (in the event that the caller expects to prepend later). | |
5577 | */ | |
5578 | __private_extern__ int MSFail = 0; | |
5579 | ||
5580 | __private_extern__ struct mbuf * | |
5581 | m_copyup(struct mbuf *n, int len, int dstoff) | |
5582 | { | |
5583 | struct mbuf *m; | |
5584 | int count, space; | |
5585 | ||
cb323159 A |
5586 | VERIFY(len >= 0 && dstoff >= 0); |
5587 | ||
0a7de745 | 5588 | if (len > (MHLEN - dstoff)) { |
6d2010ae | 5589 | goto bad; |
0a7de745 | 5590 | } |
6d2010ae | 5591 | MGET(m, M_DONTWAIT, n->m_type); |
0a7de745 | 5592 | if (m == NULL) { |
6d2010ae | 5593 | goto bad; |
0a7de745 | 5594 | } |
6d2010ae A |
5595 | m->m_len = 0; |
5596 | if (n->m_flags & M_PKTHDR) { | |
5597 | m_copy_pkthdr(m, n); | |
5598 | n->m_flags &= ~M_PKTHDR; | |
5599 | } | |
5600 | m->m_data += dstoff; | |
5601 | space = &m->m_dat[MLEN] - (m->m_data + m->m_len); | |
5602 | do { | |
5603 | count = min(min(max(len, max_protohdr), space), n->m_len); | |
5604 | memcpy(mtod(m, caddr_t) + m->m_len, mtod(n, caddr_t), | |
5605 | (unsigned)count); | |
5606 | len -= count; | |
5607 | m->m_len += count; | |
5608 | n->m_len -= count; | |
5609 | space -= count; | |
0a7de745 | 5610 | if (n->m_len) { |
6d2010ae | 5611 | n->m_data += count; |
0a7de745 | 5612 | } else { |
6d2010ae | 5613 | n = m_free(n); |
0a7de745 | 5614 | } |
6d2010ae A |
5615 | } while (len > 0 && n); |
5616 | if (len > 0) { | |
5617 | (void) m_free(m); | |
5618 | goto bad; | |
5619 | } | |
5620 | m->m_next = n; | |
0a7de745 | 5621 | return m; |
6d2010ae A |
5622 | bad: |
5623 | m_freem(n); | |
5624 | MSFail++; | |
0a7de745 | 5625 | return NULL; |
6d2010ae A |
5626 | } |
5627 | ||
1c79356b A |
5628 | /* |
5629 | * Partition an mbuf chain in two pieces, returning the tail -- | |
5630 | * all but the first len0 bytes. In case of failure, it returns NULL and | |
5631 | * attempts to restore the chain to its original state. | |
5632 | */ | |
5633 | struct mbuf * | |
2d21ac55 | 5634 | m_split(struct mbuf *m0, int len0, int wait) |
b0d623f7 | 5635 | { |
0a7de745 | 5636 | return m_split0(m0, len0, wait, 1); |
b0d623f7 A |
5637 | } |
5638 | ||
5639 | static struct mbuf * | |
5640 | m_split0(struct mbuf *m0, int len0, int wait, int copyhdr) | |
1c79356b | 5641 | { |
91447636 | 5642 | struct mbuf *m, *n; |
1c79356b A |
5643 | unsigned len = len0, remain; |
5644 | ||
d9a64523 A |
5645 | /* |
5646 | * First iterate to the mbuf which contains the first byte of | |
5647 | * data at offset len0 | |
5648 | */ | |
0a7de745 | 5649 | for (m = m0; m && len > m->m_len; m = m->m_next) { |
1c79356b | 5650 | len -= m->m_len; |
0a7de745 A |
5651 | } |
5652 | if (m == NULL) { | |
5653 | return NULL; | |
5654 | } | |
d9a64523 A |
5655 | /* |
5656 | * len effectively is now the offset in the current | |
5657 | * mbuf where we have to perform split. | |
5658 | * | |
5659 | * remain becomes the tail length. | |
5660 | * Note that len can also be == m->m_len | |
5661 | */ | |
1c79356b | 5662 | remain = m->m_len - len; |
d9a64523 A |
5663 | |
5664 | /* | |
5665 | * If current mbuf len contains the entire remaining offset len, | |
5666 | * just make the second mbuf chain pointing to next mbuf onwards | |
5667 | * and return after making necessary adjustments | |
5668 | */ | |
5669 | if (copyhdr && (m0->m_flags & M_PKTHDR) && remain == 0) { | |
5670 | _MGETHDR(n, wait, m0->m_type); | |
0a7de745 A |
5671 | if (n == NULL) { |
5672 | return NULL; | |
5673 | } | |
d9a64523 A |
5674 | n->m_next = m->m_next; |
5675 | m->m_next = NULL; | |
5676 | n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; | |
5677 | n->m_pkthdr.len = m0->m_pkthdr.len - len0; | |
5678 | m0->m_pkthdr.len = len0; | |
0a7de745 A |
5679 | return n; |
5680 | } | |
5681 | if (copyhdr && (m0->m_flags & M_PKTHDR)) { | |
2d21ac55 | 5682 | _MGETHDR(n, wait, m0->m_type); |
0a7de745 A |
5683 | if (n == NULL) { |
5684 | return NULL; | |
5685 | } | |
1c79356b A |
5686 | n->m_pkthdr.rcvif = m0->m_pkthdr.rcvif; |
5687 | n->m_pkthdr.len = m0->m_pkthdr.len - len0; | |
5688 | m0->m_pkthdr.len = len0; | |
d9a64523 A |
5689 | |
5690 | /* | |
5691 | * If current points to external storage | |
5692 | * then it can be shared by making last mbuf | |
5693 | * of head chain and first mbuf of current chain | |
5694 | * pointing to different data offsets | |
5695 | */ | |
0a7de745 | 5696 | if (m->m_flags & M_EXT) { |
1c79356b | 5697 | goto extpacket; |
0a7de745 | 5698 | } |
1c79356b A |
5699 | if (remain > MHLEN) { |
5700 | /* m can't be the lead packet */ | |
5701 | MH_ALIGN(n, 0); | |
5702 | n->m_next = m_split(m, len, wait); | |
2d21ac55 | 5703 | if (n->m_next == NULL) { |
1c79356b | 5704 | (void) m_free(n); |
0a7de745 A |
5705 | return NULL; |
5706 | } else { | |
5707 | return n; | |
5708 | } | |
5709 | } else { | |
1c79356b | 5710 | MH_ALIGN(n, remain); |
0a7de745 | 5711 | } |
1c79356b A |
5712 | } else if (remain == 0) { |
5713 | n = m->m_next; | |
2d21ac55 | 5714 | m->m_next = NULL; |
0a7de745 | 5715 | return n; |
1c79356b | 5716 | } else { |
2d21ac55 | 5717 | _MGET(n, wait, m->m_type); |
0a7de745 A |
5718 | if (n == NULL) { |
5719 | return NULL; | |
5720 | } | |
d9a64523 A |
5721 | |
5722 | if ((m->m_flags & M_EXT) == 0) { | |
5723 | VERIFY(remain <= MLEN); | |
5724 | M_ALIGN(n, remain); | |
5725 | } | |
1c79356b A |
5726 | } |
5727 | extpacket: | |
5728 | if (m->m_flags & M_EXT) { | |
5729 | n->m_flags |= M_EXT; | |
0b4e3aa0 | 5730 | n->m_ext = m->m_ext; |
2d21ac55 | 5731 | m_incref(m); |
1c79356b A |
5732 | n->m_data = m->m_data + len; |
5733 | } else { | |
2d21ac55 | 5734 | bcopy(MTOD(m, caddr_t) + len, MTOD(n, caddr_t), remain); |
1c79356b A |
5735 | } |
5736 | n->m_len = remain; | |
5737 | m->m_len = len; | |
5738 | n->m_next = m->m_next; | |
2d21ac55 | 5739 | m->m_next = NULL; |
0a7de745 | 5740 | return n; |
1c79356b | 5741 | } |
2d21ac55 | 5742 | |
1c79356b A |
5743 | /* |
5744 | * Routine to copy from device local memory into mbufs. | |
5745 | */ | |
5746 | struct mbuf * | |
2d21ac55 A |
5747 | m_devget(char *buf, int totlen, int off0, struct ifnet *ifp, |
5748 | void (*copy)(const void *, void *, size_t)) | |
1c79356b | 5749 | { |
91447636 | 5750 | struct mbuf *m; |
2d21ac55 | 5751 | struct mbuf *top = NULL, **mp = ⊤ |
91447636 A |
5752 | int off = off0, len; |
5753 | char *cp; | |
1c79356b A |
5754 | char *epkt; |
5755 | ||
5756 | cp = buf; | |
5757 | epkt = cp + totlen; | |
5758 | if (off) { | |
5759 | /* | |
5760 | * If 'off' is non-zero, packet is trailer-encapsulated, | |
5761 | * so we have to skip the type and length fields. | |
5762 | */ | |
0a7de745 A |
5763 | cp += off + 2 * sizeof(u_int16_t); |
5764 | totlen -= 2 * sizeof(u_int16_t); | |
1c79356b | 5765 | } |
2d21ac55 | 5766 | _MGETHDR(m, M_DONTWAIT, MT_DATA); |
0a7de745 A |
5767 | if (m == NULL) { |
5768 | return NULL; | |
5769 | } | |
1c79356b A |
5770 | m->m_pkthdr.rcvif = ifp; |
5771 | m->m_pkthdr.len = totlen; | |
5772 | m->m_len = MHLEN; | |
5773 | ||
5774 | while (totlen > 0) { | |
2d21ac55 A |
5775 | if (top != NULL) { |
5776 | _MGET(m, M_DONTWAIT, MT_DATA); | |
5777 | if (m == NULL) { | |
1c79356b | 5778 | m_freem(top); |
0a7de745 | 5779 | return NULL; |
1c79356b A |
5780 | } |
5781 | m->m_len = MLEN; | |
5782 | } | |
2d21ac55 | 5783 | len = MIN(totlen, epkt - cp); |
1c79356b A |
5784 | if (len >= MINCLSIZE) { |
5785 | MCLGET(m, M_DONTWAIT); | |
2d21ac55 A |
5786 | if (m->m_flags & M_EXT) { |
5787 | m->m_len = len = MIN(len, m_maxsize(MC_CL)); | |
5788 | } else { | |
5789 | /* give up when it's out of cluster mbufs */ | |
0a7de745 | 5790 | if (top != NULL) { |
2d21ac55 | 5791 | m_freem(top); |
0a7de745 | 5792 | } |
1c79356b | 5793 | m_freem(m); |
0a7de745 | 5794 | return NULL; |
1c79356b A |
5795 | } |
5796 | } else { | |
5797 | /* | |
5798 | * Place initial small packet/header at end of mbuf. | |
5799 | */ | |
5800 | if (len < m->m_len) { | |
2d21ac55 | 5801 | if (top == NULL && |
0a7de745 | 5802 | len + max_linkhdr <= m->m_len) { |
1c79356b | 5803 | m->m_data += max_linkhdr; |
0a7de745 | 5804 | } |
1c79356b | 5805 | m->m_len = len; |
2d21ac55 | 5806 | } else { |
1c79356b | 5807 | len = m->m_len; |
2d21ac55 | 5808 | } |
1c79356b | 5809 | } |
0a7de745 | 5810 | if (copy) { |
2d21ac55 | 5811 | copy(cp, MTOD(m, caddr_t), (unsigned)len); |
0a7de745 | 5812 | } else { |
2d21ac55 | 5813 | bcopy(cp, MTOD(m, caddr_t), (unsigned)len); |
0a7de745 | 5814 | } |
1c79356b A |
5815 | cp += len; |
5816 | *mp = m; | |
5817 | mp = &m->m_next; | |
5818 | totlen -= len; | |
0a7de745 | 5819 | if (cp == epkt) { |
1c79356b | 5820 | cp = buf; |
0a7de745 | 5821 | } |
1c79356b | 5822 | } |
0a7de745 | 5823 | return top; |
1c79356b A |
5824 | } |
5825 | ||
6d2010ae | 5826 | #ifndef MBUF_GROWTH_NORMAL_THRESH |
0a7de745 | 5827 | #define MBUF_GROWTH_NORMAL_THRESH 25 |
6d2010ae | 5828 | #endif |
b0d623f7 | 5829 | |
1c79356b | 5830 | /* |
2d21ac55 | 5831 | * Cluster freelist allocation check. |
1c79356b A |
5832 | */ |
5833 | static int | |
91447636 | 5834 | m_howmany(int num, size_t bufsize) |
1c79356b | 5835 | { |
2d21ac55 | 5836 | int i = 0, j = 0; |
6d2010ae A |
5837 | u_int32_t m_mbclusters, m_clusters, m_bigclusters, m_16kclusters; |
5838 | u_int32_t m_mbfree, m_clfree, m_bigclfree, m_16kclfree; | |
5839 | u_int32_t sumclusters, freeclusters; | |
5840 | u_int32_t percent_pool, percent_kmem; | |
5841 | u_int32_t mb_growth, mb_growth_thresh; | |
5842 | ||
5843 | VERIFY(bufsize == m_maxsize(MC_BIGCL) || | |
5844 | bufsize == m_maxsize(MC_16KCL)); | |
2d21ac55 | 5845 | |
5ba3f43e | 5846 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 | 5847 | |
6d2010ae A |
5848 | /* Numbers in 2K cluster units */ |
5849 | m_mbclusters = m_total(MC_MBUF) >> NMBPCLSHIFT; | |
2d21ac55 | 5850 | m_clusters = m_total(MC_CL); |
6d2010ae | 5851 | m_bigclusters = m_total(MC_BIGCL) << NCLPBGSHIFT; |
2d21ac55 | 5852 | m_16kclusters = m_total(MC_16KCL); |
6d2010ae A |
5853 | sumclusters = m_mbclusters + m_clusters + m_bigclusters; |
5854 | ||
5855 | m_mbfree = m_infree(MC_MBUF) >> NMBPCLSHIFT; | |
2d21ac55 | 5856 | m_clfree = m_infree(MC_CL); |
6d2010ae | 5857 | m_bigclfree = m_infree(MC_BIGCL) << NCLPBGSHIFT; |
2d21ac55 | 5858 | m_16kclfree = m_infree(MC_16KCL); |
6d2010ae | 5859 | freeclusters = m_mbfree + m_clfree + m_bigclfree; |
2d21ac55 | 5860 | |
91447636 | 5861 | /* Bail if we've maxed out the mbuf memory map */ |
6d2010ae | 5862 | if ((bufsize == m_maxsize(MC_BIGCL) && sumclusters >= nclusters) || |
2d21ac55 | 5863 | (njcl > 0 && bufsize == m_maxsize(MC_16KCL) && |
6d2010ae | 5864 | (m_16kclusters << NCLPJCLSHIFT) >= njcl)) { |
d9a64523 A |
5865 | mbwdog_logger("maxed out nclusters (%u >= %u) or njcl (%u >= %u)", |
5866 | sumclusters, nclusters, | |
5867 | (m_16kclusters << NCLPJCLSHIFT), njcl); | |
0a7de745 | 5868 | return 0; |
2d21ac55 A |
5869 | } |
5870 | ||
6d2010ae | 5871 | if (bufsize == m_maxsize(MC_BIGCL)) { |
2d21ac55 | 5872 | /* Under minimum */ |
0a7de745 A |
5873 | if (m_bigclusters < m_minlimit(MC_BIGCL)) { |
5874 | return m_minlimit(MC_BIGCL) - m_bigclusters; | |
5875 | } | |
6d2010ae A |
5876 | |
5877 | percent_pool = | |
5878 | ((sumclusters - freeclusters) * 100) / sumclusters; | |
5879 | percent_kmem = (sumclusters * 100) / nclusters; | |
5880 | ||
5881 | /* | |
5882 | * If a light/normal user, grow conservatively (75%) | |
5883 | * If a heavy user, grow aggressively (50%) | |
5884 | */ | |
0a7de745 | 5885 | if (percent_kmem < MBUF_GROWTH_NORMAL_THRESH) { |
6d2010ae | 5886 | mb_growth = MB_GROWTH_NORMAL; |
0a7de745 | 5887 | } else { |
6d2010ae | 5888 | mb_growth = MB_GROWTH_AGGRESSIVE; |
0a7de745 | 5889 | } |
6d2010ae A |
5890 | |
5891 | if (percent_kmem < 5) { | |
5892 | /* For initial allocations */ | |
5893 | i = num; | |
5894 | } else { | |
5895 | /* Return if >= MBIGCL_LOWAT clusters available */ | |
5896 | if (m_infree(MC_BIGCL) >= MBIGCL_LOWAT && | |
5897 | m_total(MC_BIGCL) >= | |
0a7de745 A |
5898 | MBIGCL_LOWAT + m_minlimit(MC_BIGCL)) { |
5899 | return 0; | |
5900 | } | |
6d2010ae A |
5901 | |
5902 | /* Ensure at least num clusters are accessible */ | |
0a7de745 | 5903 | if (num >= m_infree(MC_BIGCL)) { |
6d2010ae | 5904 | i = num - m_infree(MC_BIGCL); |
0a7de745 A |
5905 | } |
5906 | if (num > m_total(MC_BIGCL) - m_minlimit(MC_BIGCL)) { | |
6d2010ae A |
5907 | j = num - (m_total(MC_BIGCL) - |
5908 | m_minlimit(MC_BIGCL)); | |
0a7de745 | 5909 | } |
6d2010ae | 5910 | |
2d21ac55 | 5911 | i = MAX(i, j); |
6d2010ae A |
5912 | |
5913 | /* | |
5914 | * Grow pool if percent_pool > 75 (normal growth) | |
5915 | * or percent_pool > 50 (aggressive growth). | |
5916 | */ | |
5917 | mb_growth_thresh = 100 - (100 / (1 << mb_growth)); | |
0a7de745 | 5918 | if (percent_pool > mb_growth_thresh) { |
6d2010ae A |
5919 | j = ((sumclusters + num) >> mb_growth) - |
5920 | freeclusters; | |
0a7de745 | 5921 | } |
2d21ac55 | 5922 | i = MAX(i, j); |
2d21ac55 | 5923 | } |
6d2010ae A |
5924 | |
5925 | /* Check to ensure we didn't go over limits */ | |
0a7de745 | 5926 | if (i + m_bigclusters >= m_maxlimit(MC_BIGCL)) { |
6d2010ae | 5927 | i = m_maxlimit(MC_BIGCL) - m_bigclusters; |
0a7de745 A |
5928 | } |
5929 | if ((i << 1) + sumclusters >= nclusters) { | |
6d2010ae | 5930 | i = (nclusters - sumclusters) >> 1; |
0a7de745 | 5931 | } |
2d21ac55 | 5932 | VERIFY((m_total(MC_BIGCL) + i) <= m_maxlimit(MC_BIGCL)); |
6d2010ae | 5933 | VERIFY(sumclusters + (i << 1) <= nclusters); |
6d2010ae | 5934 | } else { /* 16K CL */ |
2d21ac55 | 5935 | VERIFY(njcl > 0); |
6d2010ae | 5936 | /* Ensure at least num clusters are available */ |
0a7de745 | 5937 | if (num >= m_16kclfree) { |
6d2010ae | 5938 | i = num - m_16kclfree; |
0a7de745 | 5939 | } |
6d2010ae A |
5940 | |
5941 | /* Always grow 16KCL pool aggressively */ | |
0a7de745 | 5942 | if (((m_16kclusters + num) >> 1) > m_16kclfree) { |
6d2010ae | 5943 | j = ((m_16kclusters + num) >> 1) - m_16kclfree; |
0a7de745 | 5944 | } |
6d2010ae A |
5945 | i = MAX(i, j); |
5946 | ||
5947 | /* Check to ensure we don't go over limit */ | |
0a7de745 | 5948 | if ((i + m_total(MC_16KCL)) >= m_maxlimit(MC_16KCL)) { |
5ba3f43e | 5949 | i = m_maxlimit(MC_16KCL) - m_total(MC_16KCL); |
0a7de745 | 5950 | } |
91447636 | 5951 | } |
0a7de745 | 5952 | return i; |
1c79356b | 5953 | } |
b0d623f7 A |
5954 | /* |
5955 | * Return the number of bytes in the mbuf chain, m. | |
6d2010ae A |
5956 | */ |
5957 | unsigned int | |
b0d623f7 A |
5958 | m_length(struct mbuf *m) |
5959 | { | |
5960 | struct mbuf *m0; | |
5961 | unsigned int pktlen; | |
5962 | ||
0a7de745 A |
5963 | if (m->m_flags & M_PKTHDR) { |
5964 | return m->m_pkthdr.len; | |
5965 | } | |
b0d623f7 A |
5966 | |
5967 | pktlen = 0; | |
0a7de745 | 5968 | for (m0 = m; m0 != NULL; m0 = m0->m_next) { |
b0d623f7 | 5969 | pktlen += m0->m_len; |
0a7de745 A |
5970 | } |
5971 | return pktlen; | |
b0d623f7 A |
5972 | } |
5973 | ||
1c79356b A |
5974 | /* |
5975 | * Copy data from a buffer back into the indicated mbuf chain, | |
5976 | * starting "off" bytes from the beginning, extending the mbuf | |
5977 | * chain if necessary. | |
5978 | */ | |
5979 | void | |
b0d623f7 | 5980 | m_copyback(struct mbuf *m0, int off, int len, const void *cp) |
1c79356b | 5981 | { |
b0d623f7 A |
5982 | #if DEBUG |
5983 | struct mbuf *origm = m0; | |
5984 | int error; | |
5985 | #endif /* DEBUG */ | |
1c79356b | 5986 | |
0a7de745 | 5987 | if (m0 == NULL) { |
1c79356b | 5988 | return; |
0a7de745 | 5989 | } |
b0d623f7 A |
5990 | |
5991 | #if DEBUG | |
5992 | error = | |
5993 | #endif /* DEBUG */ | |
5994 | m_copyback0(&m0, off, len, cp, | |
5995 | M_COPYBACK0_COPYBACK | M_COPYBACK0_EXTEND, M_DONTWAIT); | |
5996 | ||
5997 | #if DEBUG | |
0a7de745 | 5998 | if (error != 0 || (m0 != NULL && origm != m0)) { |
b0d623f7 | 5999 | panic("m_copyback"); |
0a7de745 | 6000 | } |
b0d623f7 A |
6001 | #endif /* DEBUG */ |
6002 | } | |
6003 | ||
6004 | struct mbuf * | |
6005 | m_copyback_cow(struct mbuf *m0, int off, int len, const void *cp, int how) | |
6006 | { | |
6007 | int error; | |
6008 | ||
6009 | /* don't support chain expansion */ | |
6010 | VERIFY(off + len <= m_length(m0)); | |
6011 | ||
6012 | error = m_copyback0(&m0, off, len, cp, | |
6013 | M_COPYBACK0_COPYBACK | M_COPYBACK0_COW, how); | |
6014 | if (error) { | |
6015 | /* | |
6016 | * no way to recover from partial success. | |
6017 | * just free the chain. | |
6018 | */ | |
6019 | m_freem(m0); | |
0a7de745 | 6020 | return NULL; |
b0d623f7 | 6021 | } |
0a7de745 | 6022 | return m0; |
b0d623f7 A |
6023 | } |
6024 | ||
6025 | /* | |
6026 | * m_makewritable: ensure the specified range writable. | |
6027 | */ | |
6028 | int | |
6029 | m_makewritable(struct mbuf **mp, int off, int len, int how) | |
6030 | { | |
6031 | int error; | |
6032 | #if DEBUG | |
6033 | struct mbuf *n; | |
6034 | int origlen, reslen; | |
6035 | ||
6036 | origlen = m_length(*mp); | |
6037 | #endif /* DEBUG */ | |
6038 | ||
6039 | #if 0 /* M_COPYALL is large enough */ | |
0a7de745 | 6040 | if (len == M_COPYALL) { |
b0d623f7 | 6041 | len = m_length(*mp) - off; /* XXX */ |
0a7de745 | 6042 | } |
b0d623f7 A |
6043 | #endif |
6044 | ||
6045 | error = m_copyback0(mp, off, len, NULL, | |
6046 | M_COPYBACK0_PRESERVE | M_COPYBACK0_COW, how); | |
6047 | ||
6048 | #if DEBUG | |
6049 | reslen = 0; | |
0a7de745 | 6050 | for (n = *mp; n; n = n->m_next) { |
b0d623f7 | 6051 | reslen += n->m_len; |
0a7de745 A |
6052 | } |
6053 | if (origlen != reslen) { | |
b0d623f7 | 6054 | panic("m_makewritable: length changed"); |
0a7de745 A |
6055 | } |
6056 | if (((*mp)->m_flags & M_PKTHDR) && reslen != (*mp)->m_pkthdr.len) { | |
b0d623f7 | 6057 | panic("m_makewritable: inconsist"); |
0a7de745 | 6058 | } |
b0d623f7 A |
6059 | #endif /* DEBUG */ |
6060 | ||
0a7de745 | 6061 | return error; |
b0d623f7 A |
6062 | } |
6063 | ||
6064 | static int | |
6065 | m_copyback0(struct mbuf **mp0, int off, int len, const void *vp, int flags, | |
6066 | int how) | |
6067 | { | |
6068 | int mlen; | |
6069 | struct mbuf *m, *n; | |
6070 | struct mbuf **mp; | |
6071 | int totlen = 0; | |
6072 | const char *cp = vp; | |
6073 | ||
6074 | VERIFY(mp0 != NULL); | |
6075 | VERIFY(*mp0 != NULL); | |
6076 | VERIFY((flags & M_COPYBACK0_PRESERVE) == 0 || cp == NULL); | |
6077 | VERIFY((flags & M_COPYBACK0_COPYBACK) == 0 || cp != NULL); | |
6078 | ||
6079 | /* | |
6080 | * we don't bother to update "totlen" in the case of M_COPYBACK0_COW, | |
6081 | * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive. | |
6082 | */ | |
6083 | ||
0a7de745 | 6084 | VERIFY((~flags & (M_COPYBACK0_EXTEND | M_COPYBACK0_COW)) != 0); |
b0d623f7 A |
6085 | |
6086 | mp = mp0; | |
6087 | m = *mp; | |
1c79356b A |
6088 | while (off > (mlen = m->m_len)) { |
6089 | off -= mlen; | |
6090 | totlen += mlen; | |
2d21ac55 | 6091 | if (m->m_next == NULL) { |
b0d623f7 A |
6092 | int tspace; |
6093 | extend: | |
0a7de745 | 6094 | if (!(flags & M_COPYBACK0_EXTEND)) { |
1c79356b | 6095 | goto out; |
0a7de745 | 6096 | } |
b0d623f7 A |
6097 | |
6098 | /* | |
6099 | * try to make some space at the end of "m". | |
6100 | */ | |
6101 | ||
6102 | mlen = m->m_len; | |
6103 | if (off + len >= MINCLSIZE && | |
6104 | !(m->m_flags & M_EXT) && m->m_len == 0) { | |
6105 | MCLGET(m, how); | |
6106 | } | |
6107 | tspace = M_TRAILINGSPACE(m); | |
6108 | if (tspace > 0) { | |
6109 | tspace = MIN(tspace, off + len); | |
6110 | VERIFY(tspace > 0); | |
6111 | bzero(mtod(m, char *) + m->m_len, | |
6112 | MIN(off, tspace)); | |
6113 | m->m_len += tspace; | |
6114 | off += mlen; | |
6115 | totlen -= mlen; | |
6116 | continue; | |
6117 | } | |
6118 | ||
6119 | /* | |
6120 | * need to allocate an mbuf. | |
6121 | */ | |
6122 | ||
6123 | if (off + len >= MINCLSIZE) { | |
6124 | n = m_getcl(how, m->m_type, 0); | |
6125 | } else { | |
6126 | n = _M_GET(how, m->m_type); | |
6127 | } | |
6128 | if (n == NULL) { | |
6129 | goto out; | |
6130 | } | |
6131 | n->m_len = 0; | |
6132 | n->m_len = MIN(M_TRAILINGSPACE(n), off + len); | |
6133 | bzero(mtod(n, char *), MIN(n->m_len, off)); | |
1c79356b A |
6134 | m->m_next = n; |
6135 | } | |
b0d623f7 | 6136 | mp = &m->m_next; |
1c79356b A |
6137 | m = m->m_next; |
6138 | } | |
6139 | while (len > 0) { | |
b0d623f7 A |
6140 | mlen = m->m_len - off; |
6141 | if (mlen != 0 && m_mclhasreference(m)) { | |
6142 | char *datap; | |
6143 | int eatlen; | |
6144 | ||
6145 | /* | |
6146 | * this mbuf is read-only. | |
6147 | * allocate a new writable mbuf and try again. | |
6148 | */ | |
6149 | ||
39236c6e | 6150 | #if DIAGNOSTIC |
0a7de745 | 6151 | if (!(flags & M_COPYBACK0_COW)) { |
b0d623f7 | 6152 | panic("m_copyback0: read-only"); |
0a7de745 | 6153 | } |
39236c6e | 6154 | #endif /* DIAGNOSTIC */ |
b0d623f7 A |
6155 | |
6156 | /* | |
6157 | * if we're going to write into the middle of | |
6158 | * a mbuf, split it first. | |
6159 | */ | |
6160 | if (off > 0 && len < mlen) { | |
6161 | n = m_split0(m, off, how, 0); | |
0a7de745 | 6162 | if (n == NULL) { |
b0d623f7 | 6163 | goto enobufs; |
0a7de745 | 6164 | } |
b0d623f7 A |
6165 | m->m_next = n; |
6166 | mp = &m->m_next; | |
6167 | m = n; | |
6168 | off = 0; | |
6169 | continue; | |
6170 | } | |
6171 | ||
6172 | /* | |
6173 | * XXX TODO coalesce into the trailingspace of | |
6174 | * the previous mbuf when possible. | |
6175 | */ | |
6176 | ||
6177 | /* | |
6178 | * allocate a new mbuf. copy packet header if needed. | |
6179 | */ | |
6180 | n = _M_GET(how, m->m_type); | |
0a7de745 | 6181 | if (n == NULL) { |
b0d623f7 | 6182 | goto enobufs; |
0a7de745 | 6183 | } |
b0d623f7 A |
6184 | if (off == 0 && (m->m_flags & M_PKTHDR)) { |
6185 | M_COPY_PKTHDR(n, m); | |
6186 | n->m_len = MHLEN; | |
6187 | } else { | |
0a7de745 | 6188 | if (len >= MINCLSIZE) { |
b0d623f7 | 6189 | MCLGET(n, M_DONTWAIT); |
0a7de745 | 6190 | } |
b0d623f7 A |
6191 | n->m_len = |
6192 | (n->m_flags & M_EXT) ? MCLBYTES : MLEN; | |
6193 | } | |
0a7de745 | 6194 | if (n->m_len > len) { |
b0d623f7 | 6195 | n->m_len = len; |
0a7de745 | 6196 | } |
b0d623f7 A |
6197 | |
6198 | /* | |
6199 | * free the region which has been overwritten. | |
6200 | * copying data from old mbufs if requested. | |
6201 | */ | |
0a7de745 | 6202 | if (flags & M_COPYBACK0_PRESERVE) { |
b0d623f7 | 6203 | datap = mtod(n, char *); |
0a7de745 | 6204 | } else { |
b0d623f7 | 6205 | datap = NULL; |
0a7de745 | 6206 | } |
b0d623f7 A |
6207 | eatlen = n->m_len; |
6208 | VERIFY(off == 0 || eatlen >= mlen); | |
6209 | if (off > 0) { | |
6210 | VERIFY(len >= mlen); | |
6211 | m->m_len = off; | |
6212 | m->m_next = n; | |
6213 | if (datap) { | |
6214 | m_copydata(m, off, mlen, datap); | |
6215 | datap += mlen; | |
6216 | } | |
6217 | eatlen -= mlen; | |
6218 | mp = &m->m_next; | |
6219 | m = m->m_next; | |
6220 | } | |
6221 | while (m != NULL && m_mclhasreference(m) && | |
6222 | n->m_type == m->m_type && eatlen > 0) { | |
6223 | mlen = MIN(eatlen, m->m_len); | |
6224 | if (datap) { | |
6225 | m_copydata(m, 0, mlen, datap); | |
6226 | datap += mlen; | |
6227 | } | |
6228 | m->m_data += mlen; | |
6229 | m->m_len -= mlen; | |
6230 | eatlen -= mlen; | |
0a7de745 | 6231 | if (m->m_len == 0) { |
b0d623f7 | 6232 | *mp = m = m_free(m); |
0a7de745 | 6233 | } |
b0d623f7 | 6234 | } |
0a7de745 | 6235 | if (eatlen > 0) { |
b0d623f7 | 6236 | n->m_len -= eatlen; |
0a7de745 | 6237 | } |
b0d623f7 A |
6238 | n->m_next = m; |
6239 | *mp = m = n; | |
6240 | continue; | |
6241 | } | |
6242 | mlen = MIN(mlen, len); | |
6243 | if (flags & M_COPYBACK0_COPYBACK) { | |
6244 | bcopy(cp, mtod(m, caddr_t) + off, (unsigned)mlen); | |
6245 | cp += mlen; | |
6246 | } | |
1c79356b A |
6247 | len -= mlen; |
6248 | mlen += off; | |
6249 | off = 0; | |
6250 | totlen += mlen; | |
0a7de745 | 6251 | if (len == 0) { |
1c79356b | 6252 | break; |
0a7de745 | 6253 | } |
2d21ac55 | 6254 | if (m->m_next == NULL) { |
b0d623f7 | 6255 | goto extend; |
1c79356b | 6256 | } |
b0d623f7 | 6257 | mp = &m->m_next; |
1c79356b A |
6258 | m = m->m_next; |
6259 | } | |
2d21ac55 | 6260 | out: |
b0d623f7 A |
6261 | if (((m = *mp0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen)) { |
6262 | VERIFY(flags & M_COPYBACK0_EXTEND); | |
1c79356b | 6263 | m->m_pkthdr.len = totlen; |
b0d623f7 A |
6264 | } |
6265 | ||
0a7de745 | 6266 | return 0; |
b0d623f7 A |
6267 | |
6268 | enobufs: | |
0a7de745 | 6269 | return ENOBUFS; |
1c79356b A |
6270 | } |
6271 | ||
39236c6e | 6272 | uint64_t |
2d21ac55 A |
6273 | mcl_to_paddr(char *addr) |
6274 | { | |
b0d623f7 | 6275 | vm_offset_t base_phys; |
1c79356b | 6276 | |
0a7de745 A |
6277 | if (!MBUF_IN_MAP(addr)) { |
6278 | return 0; | |
6279 | } | |
39236c6e | 6280 | base_phys = mcl_paddr[atop_64(addr - (char *)mbutl)]; |
1c79356b | 6281 | |
0a7de745 A |
6282 | if (base_phys == 0) { |
6283 | return 0; | |
6284 | } | |
6285 | return (uint64_t)(ptoa_64(base_phys) | ((uint64_t)addr & PAGE_MASK)); | |
1c79356b A |
6286 | } |
6287 | ||
6288 | /* | |
6289 | * Dup the mbuf chain passed in. The whole thing. No cute additional cruft. | |
6290 | * And really copy the thing. That way, we don't "precompute" checksums | |
2d21ac55 A |
6291 | * for unsuspecting consumers. Assumption: m->m_nextpkt == 0. Trick: for |
6292 | * small packets, don't dup into a cluster. That way received packets | |
6293 | * don't take up too much room in the sockbuf (cf. sbspace()). | |
1c79356b A |
6294 | */ |
6295 | int MDFail; | |
6296 | ||
6297 | struct mbuf * | |
91447636 | 6298 | m_dup(struct mbuf *m, int how) |
2d21ac55 | 6299 | { |
91447636 | 6300 | struct mbuf *n, **np; |
1c79356b A |
6301 | struct mbuf *top; |
6302 | int copyhdr = 0; | |
6303 | ||
6304 | np = ⊤ | |
2d21ac55 | 6305 | top = NULL; |
0a7de745 | 6306 | if (m->m_flags & M_PKTHDR) { |
1c79356b | 6307 | copyhdr = 1; |
0a7de745 | 6308 | } |
1c79356b A |
6309 | |
6310 | /* | |
6311 | * Quick check: if we have one mbuf and its data fits in an | |
6312 | * mbuf with packet header, just copy and go. | |
6313 | */ | |
2d21ac55 A |
6314 | if (m->m_next == NULL) { |
6315 | /* Then just move the data into an mbuf and be done... */ | |
6316 | if (copyhdr) { | |
6317 | if (m->m_pkthdr.len <= MHLEN && m->m_len <= MHLEN) { | |
0a7de745 A |
6318 | if ((n = _M_GETHDR(how, m->m_type)) == NULL) { |
6319 | return NULL; | |
6320 | } | |
1c79356b | 6321 | n->m_len = m->m_len; |
3a60a9f5 A |
6322 | m_dup_pkthdr(n, m, how); |
6323 | bcopy(m->m_data, n->m_data, m->m_len); | |
0a7de745 | 6324 | return n; |
1c79356b | 6325 | } |
2d21ac55 | 6326 | } else if (m->m_len <= MLEN) { |
0a7de745 A |
6327 | if ((n = _M_GET(how, m->m_type)) == NULL) { |
6328 | return NULL; | |
6329 | } | |
1c79356b A |
6330 | bcopy(m->m_data, n->m_data, m->m_len); |
6331 | n->m_len = m->m_len; | |
0a7de745 | 6332 | return n; |
1c79356b A |
6333 | } |
6334 | } | |
2d21ac55 | 6335 | while (m != NULL) { |
1c79356b | 6336 | #if BLUE_DEBUG |
39037602 | 6337 | printf("<%x: %x, %x, %x\n", m, m->m_flags, m->m_len, |
2d21ac55 | 6338 | m->m_data); |
1c79356b | 6339 | #endif |
0a7de745 | 6340 | if (copyhdr) { |
2d21ac55 | 6341 | n = _M_GETHDR(how, m->m_type); |
0a7de745 | 6342 | } else { |
2d21ac55 | 6343 | n = _M_GET(how, m->m_type); |
0a7de745 A |
6344 | } |
6345 | if (n == NULL) { | |
1c79356b | 6346 | goto nospace; |
0a7de745 | 6347 | } |
2d21ac55 | 6348 | if (m->m_flags & M_EXT) { |
0a7de745 | 6349 | if (m->m_len <= m_maxsize(MC_CL)) { |
2d21ac55 | 6350 | MCLGET(n, how); |
0a7de745 | 6351 | } else if (m->m_len <= m_maxsize(MC_BIGCL)) { |
2d21ac55 | 6352 | n = m_mbigget(n, how); |
0a7de745 | 6353 | } else if (m->m_len <= m_maxsize(MC_16KCL) && njcl > 0) { |
2d21ac55 | 6354 | n = m_m16kget(n, how); |
0a7de745 | 6355 | } |
2d21ac55 A |
6356 | if (!(n->m_flags & M_EXT)) { |
6357 | (void) m_free(n); | |
1c79356b | 6358 | goto nospace; |
2d21ac55 | 6359 | } |
cb323159 A |
6360 | } else { |
6361 | VERIFY((copyhdr == 1 && m->m_len <= MHLEN) || | |
6362 | (copyhdr == 0 && m->m_len <= MLEN)); | |
1c79356b A |
6363 | } |
6364 | *np = n; | |
2d21ac55 A |
6365 | if (copyhdr) { |
6366 | /* Don't use M_COPY_PKTHDR: preserve m_data */ | |
3a60a9f5 | 6367 | m_dup_pkthdr(n, m, how); |
1c79356b | 6368 | copyhdr = 0; |
0a7de745 | 6369 | if (!(n->m_flags & M_EXT)) { |
1c79356b | 6370 | n->m_data = n->m_pktdat; |
0a7de745 | 6371 | } |
1c79356b A |
6372 | } |
6373 | n->m_len = m->m_len; | |
6374 | /* | |
6375 | * Get the dup on the same bdry as the original | |
6376 | * Assume that the two mbufs have the same offset to data area | |
2d21ac55 | 6377 | * (up to word boundaries) |
1c79356b | 6378 | */ |
2d21ac55 | 6379 | bcopy(MTOD(m, caddr_t), MTOD(n, caddr_t), (unsigned)n->m_len); |
1c79356b A |
6380 | m = m->m_next; |
6381 | np = &n->m_next; | |
6382 | #if BLUE_DEBUG | |
39037602 | 6383 | printf(">%x: %x, %x, %x\n", n, n->m_flags, n->m_len, |
2d21ac55 | 6384 | n->m_data); |
1c79356b A |
6385 | #endif |
6386 | } | |
6387 | ||
0a7de745 | 6388 | if (top == NULL) { |
1c79356b | 6389 | MDFail++; |
0a7de745 A |
6390 | } |
6391 | return top; | |
2d21ac55 A |
6392 | |
6393 | nospace: | |
1c79356b A |
6394 | m_freem(top); |
6395 | MDFail++; | |
0a7de745 | 6396 | return NULL; |
1c79356b A |
6397 | } |
6398 | ||
0a7de745 A |
6399 | #define MBUF_MULTIPAGES(m) \ |
6400 | (((m)->m_flags & M_EXT) && \ | |
6401 | ((IS_P2ALIGNED((m)->m_data, PAGE_SIZE) \ | |
6402 | && (m)->m_len > PAGE_SIZE) || \ | |
6403 | (!IS_P2ALIGNED((m)->m_data, PAGE_SIZE) && \ | |
3e170ce0 | 6404 | P2ROUNDUP((m)->m_data, PAGE_SIZE) < ((uintptr_t)(m)->m_data + (m)->m_len)))) |
2d21ac55 A |
6405 | |
6406 | static struct mbuf * | |
6407 | m_expand(struct mbuf *m, struct mbuf **last) | |
9bccf70c | 6408 | { |
2d21ac55 A |
6409 | struct mbuf *top = NULL; |
6410 | struct mbuf **nm = ⊤ | |
6411 | uintptr_t data0, data; | |
6412 | unsigned int len0, len; | |
6413 | ||
6414 | VERIFY(MBUF_MULTIPAGES(m)); | |
6415 | VERIFY(m->m_next == NULL); | |
6416 | data0 = (uintptr_t)m->m_data; | |
6417 | len0 = m->m_len; | |
6418 | *last = top; | |
6419 | ||
6420 | for (;;) { | |
6421 | struct mbuf *n; | |
6422 | ||
6423 | data = data0; | |
0a7de745 | 6424 | if (IS_P2ALIGNED(data, PAGE_SIZE) && len0 > PAGE_SIZE) { |
3e170ce0 | 6425 | len = PAGE_SIZE; |
0a7de745 A |
6426 | } else if (!IS_P2ALIGNED(data, PAGE_SIZE) && |
6427 | P2ROUNDUP(data, PAGE_SIZE) < (data + len0)) { | |
3e170ce0 | 6428 | len = P2ROUNDUP(data, PAGE_SIZE) - data; |
0a7de745 | 6429 | } else { |
2d21ac55 | 6430 | len = len0; |
0a7de745 | 6431 | } |
2d21ac55 A |
6432 | |
6433 | VERIFY(len > 0); | |
6434 | VERIFY(m->m_flags & M_EXT); | |
6435 | m->m_data = (void *)data; | |
6436 | m->m_len = len; | |
6437 | ||
6438 | *nm = *last = m; | |
6439 | nm = &m->m_next; | |
6440 | m->m_next = NULL; | |
6441 | ||
6442 | data0 += len; | |
6443 | len0 -= len; | |
0a7de745 | 6444 | if (len0 == 0) { |
2d21ac55 | 6445 | break; |
0a7de745 | 6446 | } |
2d21ac55 A |
6447 | |
6448 | n = _M_RETRY(M_DONTWAIT, MT_DATA); | |
6449 | if (n == NULL) { | |
6450 | m_freem(top); | |
6451 | top = *last = NULL; | |
6452 | break; | |
6453 | } | |
6454 | ||
6455 | n->m_ext = m->m_ext; | |
6456 | m_incref(m); | |
6457 | n->m_flags |= M_EXT; | |
6458 | m = n; | |
6459 | } | |
0a7de745 | 6460 | return top; |
9bccf70c A |
6461 | } |
6462 | ||
2d21ac55 A |
6463 | struct mbuf * |
6464 | m_normalize(struct mbuf *m) | |
9bccf70c | 6465 | { |
2d21ac55 A |
6466 | struct mbuf *top = NULL; |
6467 | struct mbuf **nm = ⊤ | |
6468 | boolean_t expanded = FALSE; | |
6469 | ||
6470 | while (m != NULL) { | |
6471 | struct mbuf *n; | |
6472 | ||
6473 | n = m->m_next; | |
6474 | m->m_next = NULL; | |
6475 | ||
6476 | /* Does the data cross one or more page boundaries? */ | |
6477 | if (MBUF_MULTIPAGES(m)) { | |
6478 | struct mbuf *last; | |
6479 | if ((m = m_expand(m, &last)) == NULL) { | |
6480 | m_freem(n); | |
6481 | m_freem(top); | |
6482 | top = NULL; | |
6483 | break; | |
6484 | } | |
6485 | *nm = m; | |
6486 | nm = &last->m_next; | |
6487 | expanded = TRUE; | |
6488 | } else { | |
6489 | *nm = m; | |
6490 | nm = &m->m_next; | |
6491 | } | |
6492 | m = n; | |
6493 | } | |
0a7de745 | 6494 | if (expanded) { |
2d21ac55 | 6495 | atomic_add_32(&mb_normalized, 1); |
0a7de745 A |
6496 | } |
6497 | return top; | |
9bccf70c A |
6498 | } |
6499 | ||
6d2010ae A |
6500 | /* |
6501 | * Append the specified data to the indicated mbuf chain, | |
6502 | * Extend the mbuf chain if the new data does not fit in | |
6503 | * existing space. | |
6504 | * | |
6505 | * Return 1 if able to complete the job; otherwise 0. | |
6506 | */ | |
6507 | int | |
6508 | m_append(struct mbuf *m0, int len, caddr_t cp) | |
6509 | { | |
6510 | struct mbuf *m, *n; | |
6511 | int remainder, space; | |
6512 | ||
0a7de745 | 6513 | for (m = m0; m->m_next != NULL; m = m->m_next) { |
6d2010ae | 6514 | ; |
0a7de745 | 6515 | } |
6d2010ae A |
6516 | remainder = len; |
6517 | space = M_TRAILINGSPACE(m); | |
6518 | if (space > 0) { | |
6519 | /* | |
6520 | * Copy into available space. | |
6521 | */ | |
0a7de745 | 6522 | if (space > remainder) { |
6d2010ae | 6523 | space = remainder; |
0a7de745 | 6524 | } |
6d2010ae A |
6525 | bcopy(cp, mtod(m, caddr_t) + m->m_len, space); |
6526 | m->m_len += space; | |
39037602 A |
6527 | cp += space; |
6528 | remainder -= space; | |
6d2010ae A |
6529 | } |
6530 | while (remainder > 0) { | |
6531 | /* | |
6532 | * Allocate a new mbuf; could check space | |
6533 | * and allocate a cluster instead. | |
6534 | */ | |
6535 | n = m_get(M_WAITOK, m->m_type); | |
0a7de745 | 6536 | if (n == NULL) { |
6d2010ae | 6537 | break; |
0a7de745 | 6538 | } |
6d2010ae A |
6539 | n->m_len = min(MLEN, remainder); |
6540 | bcopy(cp, mtod(n, caddr_t), n->m_len); | |
6541 | cp += n->m_len; | |
6542 | remainder -= n->m_len; | |
6543 | m->m_next = n; | |
6544 | m = n; | |
6545 | } | |
0a7de745 | 6546 | if (m0->m_flags & M_PKTHDR) { |
6d2010ae | 6547 | m0->m_pkthdr.len += len - remainder; |
0a7de745 A |
6548 | } |
6549 | return remainder == 0; | |
6d2010ae A |
6550 | } |
6551 | ||
6552 | struct mbuf * | |
6553 | m_last(struct mbuf *m) | |
6554 | { | |
0a7de745 | 6555 | while (m->m_next != NULL) { |
6d2010ae | 6556 | m = m->m_next; |
0a7de745 A |
6557 | } |
6558 | return m; | |
6d2010ae A |
6559 | } |
6560 | ||
316670eb A |
6561 | unsigned int |
6562 | m_fixhdr(struct mbuf *m0) | |
6563 | { | |
6564 | u_int len; | |
6565 | ||
39236c6e A |
6566 | VERIFY(m0->m_flags & M_PKTHDR); |
6567 | ||
316670eb A |
6568 | len = m_length2(m0, NULL); |
6569 | m0->m_pkthdr.len = len; | |
0a7de745 | 6570 | return len; |
316670eb A |
6571 | } |
6572 | ||
6573 | unsigned int | |
6574 | m_length2(struct mbuf *m0, struct mbuf **last) | |
6575 | { | |
6576 | struct mbuf *m; | |
6577 | u_int len; | |
6578 | ||
6579 | len = 0; | |
6580 | for (m = m0; m != NULL; m = m->m_next) { | |
6581 | len += m->m_len; | |
0a7de745 | 6582 | if (m->m_next == NULL) { |
316670eb | 6583 | break; |
0a7de745 | 6584 | } |
316670eb | 6585 | } |
0a7de745 | 6586 | if (last != NULL) { |
316670eb | 6587 | *last = m; |
0a7de745 A |
6588 | } |
6589 | return len; | |
316670eb A |
6590 | } |
6591 | ||
6592 | /* | |
6593 | * Defragment a mbuf chain, returning the shortest possible chain of mbufs | |
6594 | * and clusters. If allocation fails and this cannot be completed, NULL will | |
6595 | * be returned, but the passed in chain will be unchanged. Upon success, | |
6596 | * the original chain will be freed, and the new chain will be returned. | |
6597 | * | |
6598 | * If a non-packet header is passed in, the original mbuf (chain?) will | |
6599 | * be returned unharmed. | |
6600 | * | |
6601 | * If offset is specfied, the first mbuf in the chain will have a leading | |
6602 | * space of the amount stated by the "off" parameter. | |
6603 | * | |
6604 | * This routine requires that the m_pkthdr.header field of the original | |
6605 | * mbuf chain is cleared by the caller. | |
6606 | */ | |
6607 | struct mbuf * | |
6608 | m_defrag_offset(struct mbuf *m0, u_int32_t off, int how) | |
6609 | { | |
6610 | struct mbuf *m_new = NULL, *m_final = NULL; | |
6611 | int progress = 0, length, pktlen; | |
6612 | ||
0a7de745 A |
6613 | if (!(m0->m_flags & M_PKTHDR)) { |
6614 | return m0; | |
6615 | } | |
316670eb A |
6616 | |
6617 | VERIFY(off < MHLEN); | |
6618 | m_fixhdr(m0); /* Needed sanity check */ | |
6619 | ||
6620 | pktlen = m0->m_pkthdr.len + off; | |
0a7de745 | 6621 | if (pktlen > MHLEN) { |
316670eb | 6622 | m_final = m_getcl(how, MT_DATA, M_PKTHDR); |
0a7de745 | 6623 | } else { |
316670eb | 6624 | m_final = m_gethdr(how, MT_DATA); |
0a7de745 | 6625 | } |
316670eb | 6626 | |
0a7de745 | 6627 | if (m_final == NULL) { |
316670eb | 6628 | goto nospace; |
0a7de745 | 6629 | } |
316670eb A |
6630 | |
6631 | if (off > 0) { | |
6632 | pktlen -= off; | |
316670eb A |
6633 | m_final->m_data += off; |
6634 | } | |
6635 | ||
6636 | /* | |
6637 | * Caller must have handled the contents pointed to by this | |
6638 | * pointer before coming here, as otherwise it will point to | |
6639 | * the original mbuf which will get freed upon success. | |
6640 | */ | |
39236c6e | 6641 | VERIFY(m0->m_pkthdr.pkt_hdr == NULL); |
316670eb | 6642 | |
0a7de745 | 6643 | if (m_dup_pkthdr(m_final, m0, how) == 0) { |
316670eb | 6644 | goto nospace; |
0a7de745 | 6645 | } |
316670eb A |
6646 | |
6647 | m_new = m_final; | |
6648 | ||
6649 | while (progress < pktlen) { | |
6650 | length = pktlen - progress; | |
0a7de745 | 6651 | if (length > MCLBYTES) { |
316670eb | 6652 | length = MCLBYTES; |
0a7de745 | 6653 | } |
39236c6e | 6654 | length -= ((m_new == m_final) ? off : 0); |
0a7de745 | 6655 | if (length < 0) { |
5ba3f43e | 6656 | goto nospace; |
0a7de745 | 6657 | } |
316670eb A |
6658 | |
6659 | if (m_new == NULL) { | |
0a7de745 | 6660 | if (length > MLEN) { |
316670eb | 6661 | m_new = m_getcl(how, MT_DATA, 0); |
0a7de745 | 6662 | } else { |
316670eb | 6663 | m_new = m_get(how, MT_DATA); |
0a7de745 A |
6664 | } |
6665 | if (m_new == NULL) { | |
316670eb | 6666 | goto nospace; |
0a7de745 | 6667 | } |
316670eb A |
6668 | } |
6669 | ||
6670 | m_copydata(m0, progress, length, mtod(m_new, caddr_t)); | |
6671 | progress += length; | |
6672 | m_new->m_len = length; | |
0a7de745 | 6673 | if (m_new != m_final) { |
316670eb | 6674 | m_cat(m_final, m_new); |
0a7de745 | 6675 | } |
316670eb A |
6676 | m_new = NULL; |
6677 | } | |
6678 | m_freem(m0); | |
6679 | m0 = m_final; | |
0a7de745 | 6680 | return m0; |
316670eb | 6681 | nospace: |
0a7de745 | 6682 | if (m_final) { |
316670eb | 6683 | m_freem(m_final); |
0a7de745 A |
6684 | } |
6685 | return NULL; | |
316670eb A |
6686 | } |
6687 | ||
6688 | struct mbuf * | |
6689 | m_defrag(struct mbuf *m0, int how) | |
6690 | { | |
0a7de745 | 6691 | return m_defrag_offset(m0, 0, how); |
316670eb A |
6692 | } |
6693 | ||
9bccf70c A |
6694 | void |
6695 | m_mchtype(struct mbuf *m, int t) | |
6696 | { | |
2d21ac55 A |
6697 | mtype_stat_inc(t); |
6698 | mtype_stat_dec(m->m_type); | |
6699 | (m)->m_type = t; | |
9bccf70c A |
6700 | } |
6701 | ||
2d21ac55 A |
6702 | void * |
6703 | m_mtod(struct mbuf *m) | |
9bccf70c | 6704 | { |
0a7de745 | 6705 | return MTOD(m, void *); |
9bccf70c A |
6706 | } |
6707 | ||
2d21ac55 A |
6708 | struct mbuf * |
6709 | m_dtom(void *x) | |
9bccf70c | 6710 | { |
0a7de745 | 6711 | return (struct mbuf *)((uintptr_t)(x) & ~(MSIZE - 1)); |
9bccf70c A |
6712 | } |
6713 | ||
2d21ac55 A |
6714 | void |
6715 | m_mcheck(struct mbuf *m) | |
9bccf70c | 6716 | { |
2d21ac55 | 6717 | _MCHECK(m); |
9bccf70c A |
6718 | } |
6719 | ||
6d2010ae A |
6720 | /* |
6721 | * Return a pointer to mbuf/offset of location in mbuf chain. | |
6722 | */ | |
6723 | struct mbuf * | |
6724 | m_getptr(struct mbuf *m, int loc, int *off) | |
6725 | { | |
6d2010ae A |
6726 | while (loc >= 0) { |
6727 | /* Normal end of search. */ | |
6728 | if (m->m_len > loc) { | |
6729 | *off = loc; | |
0a7de745 | 6730 | return m; |
6d2010ae A |
6731 | } else { |
6732 | loc -= m->m_len; | |
6733 | if (m->m_next == NULL) { | |
6734 | if (loc == 0) { | |
6735 | /* Point at the end of valid data. */ | |
6736 | *off = m->m_len; | |
0a7de745 | 6737 | return m; |
6d2010ae | 6738 | } |
0a7de745 | 6739 | return NULL; |
6d2010ae A |
6740 | } |
6741 | m = m->m_next; | |
6742 | } | |
6743 | } | |
0a7de745 | 6744 | return NULL; |
6d2010ae A |
6745 | } |
6746 | ||
2d21ac55 A |
6747 | /* |
6748 | * Inform the corresponding mcache(s) that there's a waiter below. | |
6749 | */ | |
6750 | static void | |
6751 | mbuf_waiter_inc(mbuf_class_t class, boolean_t comp) | |
9bccf70c | 6752 | { |
2d21ac55 A |
6753 | mcache_waiter_inc(m_cache(class)); |
6754 | if (comp) { | |
6755 | if (class == MC_CL) { | |
6756 | mcache_waiter_inc(m_cache(MC_MBUF_CL)); | |
6757 | } else if (class == MC_BIGCL) { | |
6758 | mcache_waiter_inc(m_cache(MC_MBUF_BIGCL)); | |
6759 | } else if (class == MC_16KCL) { | |
6760 | mcache_waiter_inc(m_cache(MC_MBUF_16KCL)); | |
6761 | } else { | |
6762 | mcache_waiter_inc(m_cache(MC_MBUF_CL)); | |
6763 | mcache_waiter_inc(m_cache(MC_MBUF_BIGCL)); | |
6764 | } | |
6765 | } | |
9bccf70c A |
6766 | } |
6767 | ||
2d21ac55 A |
6768 | /* |
6769 | * Inform the corresponding mcache(s) that there's no more waiter below. | |
6770 | */ | |
6771 | static void | |
6772 | mbuf_waiter_dec(mbuf_class_t class, boolean_t comp) | |
6773 | { | |
6774 | mcache_waiter_dec(m_cache(class)); | |
6775 | if (comp) { | |
6776 | if (class == MC_CL) { | |
6777 | mcache_waiter_dec(m_cache(MC_MBUF_CL)); | |
6778 | } else if (class == MC_BIGCL) { | |
6779 | mcache_waiter_dec(m_cache(MC_MBUF_BIGCL)); | |
6780 | } else if (class == MC_16KCL) { | |
6781 | mcache_waiter_dec(m_cache(MC_MBUF_16KCL)); | |
6782 | } else { | |
6783 | mcache_waiter_dec(m_cache(MC_MBUF_CL)); | |
6784 | mcache_waiter_dec(m_cache(MC_MBUF_BIGCL)); | |
6785 | } | |
6786 | } | |
6787 | } | |
9bccf70c | 6788 | |
6d2010ae A |
6789 | /* |
6790 | * Called during slab (blocking and non-blocking) allocation. If there | |
6791 | * is at least one waiter, and the time since the first waiter is blocked | |
6792 | * is greater than the watchdog timeout, panic the system. | |
6793 | */ | |
6794 | static void | |
6795 | mbuf_watchdog(void) | |
6796 | { | |
6797 | struct timeval now; | |
6798 | unsigned int since; | |
6799 | ||
0a7de745 | 6800 | if (mb_waiters == 0 || !mb_watchdog) { |
6d2010ae | 6801 | return; |
0a7de745 | 6802 | } |
6d2010ae A |
6803 | |
6804 | microuptime(&now); | |
6805 | since = now.tv_sec - mb_wdtstart.tv_sec; | |
6806 | if (since >= MB_WDT_MAXTIME) { | |
6807 | panic_plain("%s: %d waiters stuck for %u secs\n%s", __func__, | |
6808 | mb_waiters, since, mbuf_dump()); | |
6809 | /* NOTREACHED */ | |
6810 | } | |
6811 | } | |
6812 | ||
2d21ac55 A |
6813 | /* |
6814 | * Called during blocking allocation. Returns TRUE if one or more objects | |
6815 | * are available at the per-CPU caches layer and that allocation should be | |
6816 | * retried at that level. | |
6817 | */ | |
6818 | static boolean_t | |
6819 | mbuf_sleep(mbuf_class_t class, unsigned int num, int wait) | |
9bccf70c | 6820 | { |
2d21ac55 A |
6821 | boolean_t mcache_retry = FALSE; |
6822 | ||
5ba3f43e | 6823 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
6824 | |
6825 | /* Check if there's anything at the cache layer */ | |
6826 | if (mbuf_cached_above(class, wait)) { | |
6827 | mcache_retry = TRUE; | |
6828 | goto done; | |
6829 | } | |
6830 | ||
6831 | /* Nothing? Then try hard to get it from somewhere */ | |
6832 | m_reclaim(class, num, (wait & MCR_COMP)); | |
6833 | ||
6834 | /* We tried hard and got something? */ | |
6835 | if (m_infree(class) > 0) { | |
6836 | mbstat.m_wait++; | |
6837 | goto done; | |
6838 | } else if (mbuf_cached_above(class, wait)) { | |
6839 | mbstat.m_wait++; | |
6840 | mcache_retry = TRUE; | |
6841 | goto done; | |
6842 | } else if (wait & MCR_TRYHARD) { | |
6843 | mcache_retry = TRUE; | |
6844 | goto done; | |
6845 | } | |
6846 | ||
6847 | /* | |
6848 | * There's really nothing for us right now; inform the | |
6849 | * cache(s) that there is a waiter below and go to sleep. | |
6850 | */ | |
6851 | mbuf_waiter_inc(class, (wait & MCR_COMP)); | |
6852 | ||
6853 | VERIFY(!(wait & MCR_NOSLEEP)); | |
6d2010ae A |
6854 | |
6855 | /* | |
6856 | * If this is the first waiter, arm the watchdog timer. Otherwise | |
6857 | * check if we need to panic the system due to watchdog timeout. | |
6858 | */ | |
0a7de745 | 6859 | if (mb_waiters == 0) { |
6d2010ae | 6860 | microuptime(&mb_wdtstart); |
0a7de745 | 6861 | } else { |
6d2010ae | 6862 | mbuf_watchdog(); |
0a7de745 | 6863 | } |
6d2010ae | 6864 | |
2d21ac55 | 6865 | mb_waiters++; |
cc8bc92a | 6866 | m_region_expand(class) += m_total(class) + num; |
5ba3f43e | 6867 | /* wake up the worker thread */ |
a39ff7e2 | 6868 | if (mbuf_worker_ready && |
5ba3f43e A |
6869 | mbuf_worker_needs_wakeup) { |
6870 | wakeup((caddr_t)&mbuf_worker_needs_wakeup); | |
6871 | mbuf_worker_needs_wakeup = FALSE; | |
6872 | } | |
d9a64523 | 6873 | mbwdog_logger("waiting (%d mbufs in class %s)", num, m_cname(class)); |
0a7de745 | 6874 | (void) msleep(mb_waitchan, mbuf_mlock, (PZERO - 1), m_cname(class), NULL); |
d9a64523 | 6875 | mbwdog_logger("woke up (%d mbufs in class %s) ", num, m_cname(class)); |
2d21ac55 A |
6876 | |
6877 | /* We are now up; stop getting notified until next round */ | |
6878 | mbuf_waiter_dec(class, (wait & MCR_COMP)); | |
6879 | ||
6880 | /* We waited and got something */ | |
6881 | if (m_infree(class) > 0) { | |
6882 | mbstat.m_wait++; | |
6883 | goto done; | |
6884 | } else if (mbuf_cached_above(class, wait)) { | |
6885 | mbstat.m_wait++; | |
6886 | mcache_retry = TRUE; | |
6887 | } | |
6888 | done: | |
0a7de745 | 6889 | return mcache_retry; |
9bccf70c A |
6890 | } |
6891 | ||
39037602 | 6892 | __attribute__((noreturn)) |
91447636 | 6893 | static void |
2d21ac55 | 6894 | mbuf_worker_thread(void) |
1c79356b | 6895 | { |
2d21ac55 A |
6896 | int mbuf_expand; |
6897 | ||
91447636 | 6898 | while (1) { |
2d21ac55 | 6899 | lck_mtx_lock(mbuf_mlock); |
d9a64523 | 6900 | mbwdog_logger("worker thread running"); |
cc8bc92a | 6901 | mbuf_worker_run_cnt++; |
2d21ac55 | 6902 | mbuf_expand = 0; |
d9a64523 A |
6903 | /* |
6904 | * Allocations are based on page size, so if we have depleted | |
6905 | * the reserved spaces, try to free mbufs from the major classes. | |
6906 | */ | |
6907 | #if PAGE_SIZE == 4096 | |
6908 | uint32_t m_mbclusters = m_total(MC_MBUF) >> NMBPCLSHIFT; | |
6909 | uint32_t m_clusters = m_total(MC_CL); | |
6910 | uint32_t m_bigclusters = m_total(MC_BIGCL) << NCLPBGSHIFT; | |
6911 | uint32_t sumclusters = m_mbclusters + m_clusters + m_bigclusters; | |
6912 | if (sumclusters >= nclusters) { | |
6913 | mbwdog_logger("reclaiming bigcl"); | |
6914 | mbuf_drain_locked(TRUE); | |
6915 | m_reclaim(MC_BIGCL, 4, FALSE); | |
6916 | } | |
6917 | #else | |
6918 | uint32_t m_16kclusters = m_total(MC_16KCL); | |
6919 | if (njcl > 0 && (m_16kclusters << NCLPJCLSHIFT) >= njcl) { | |
6920 | mbwdog_logger("reclaiming 16kcl"); | |
6921 | mbuf_drain_locked(TRUE); | |
6922 | m_reclaim(MC_16KCL, 4, FALSE); | |
6923 | } | |
6924 | #endif | |
5ba3f43e | 6925 | if (m_region_expand(MC_CL) > 0) { |
91447636 | 6926 | int n; |
cc8bc92a | 6927 | mb_expand_cl_cnt++; |
2d21ac55 | 6928 | /* Adjust to current number of cluster in use */ |
5ba3f43e | 6929 | n = m_region_expand(MC_CL) - |
2d21ac55 | 6930 | (m_total(MC_CL) - m_infree(MC_CL)); |
0a7de745 | 6931 | if ((n + m_total(MC_CL)) > m_maxlimit(MC_CL)) { |
2d21ac55 | 6932 | n = m_maxlimit(MC_CL) - m_total(MC_CL); |
0a7de745 | 6933 | } |
cc8bc92a A |
6934 | if (n > 0) { |
6935 | mb_expand_cl_total += n; | |
6936 | } | |
5ba3f43e | 6937 | m_region_expand(MC_CL) = 0; |
2d21ac55 | 6938 | |
d9a64523 A |
6939 | if (n > 0) { |
6940 | mbwdog_logger("expanding MC_CL by %d", n); | |
a39ff7e2 | 6941 | freelist_populate(MC_CL, n, M_WAIT); |
d9a64523 | 6942 | } |
91447636 | 6943 | } |
5ba3f43e | 6944 | if (m_region_expand(MC_BIGCL) > 0) { |
91447636 | 6945 | int n; |
cc8bc92a | 6946 | mb_expand_bigcl_cnt++; |
2d21ac55 | 6947 | /* Adjust to current number of 4 KB cluster in use */ |
5ba3f43e | 6948 | n = m_region_expand(MC_BIGCL) - |
2d21ac55 | 6949 | (m_total(MC_BIGCL) - m_infree(MC_BIGCL)); |
0a7de745 | 6950 | if ((n + m_total(MC_BIGCL)) > m_maxlimit(MC_BIGCL)) { |
2d21ac55 | 6951 | n = m_maxlimit(MC_BIGCL) - m_total(MC_BIGCL); |
0a7de745 | 6952 | } |
cc8bc92a A |
6953 | if (n > 0) { |
6954 | mb_expand_bigcl_total += n; | |
6955 | } | |
5ba3f43e | 6956 | m_region_expand(MC_BIGCL) = 0; |
2d21ac55 | 6957 | |
d9a64523 A |
6958 | if (n > 0) { |
6959 | mbwdog_logger("expanding MC_BIGCL by %d", n); | |
a39ff7e2 | 6960 | freelist_populate(MC_BIGCL, n, M_WAIT); |
d9a64523 | 6961 | } |
2d21ac55 | 6962 | } |
5ba3f43e | 6963 | if (m_region_expand(MC_16KCL) > 0) { |
2d21ac55 | 6964 | int n; |
cc8bc92a | 6965 | mb_expand_16kcl_cnt++; |
2d21ac55 | 6966 | /* Adjust to current number of 16 KB cluster in use */ |
5ba3f43e | 6967 | n = m_region_expand(MC_16KCL) - |
2d21ac55 | 6968 | (m_total(MC_16KCL) - m_infree(MC_16KCL)); |
0a7de745 | 6969 | if ((n + m_total(MC_16KCL)) > m_maxlimit(MC_16KCL)) { |
2d21ac55 | 6970 | n = m_maxlimit(MC_16KCL) - m_total(MC_16KCL); |
0a7de745 | 6971 | } |
cc8bc92a A |
6972 | if (n > 0) { |
6973 | mb_expand_16kcl_total += n; | |
6974 | } | |
5ba3f43e | 6975 | m_region_expand(MC_16KCL) = 0; |
2d21ac55 | 6976 | |
d9a64523 A |
6977 | if (n > 0) { |
6978 | mbwdog_logger("expanding MC_16KCL by %d", n); | |
2d21ac55 | 6979 | (void) freelist_populate(MC_16KCL, n, M_WAIT); |
d9a64523 | 6980 | } |
2d21ac55 A |
6981 | } |
6982 | ||
6983 | /* | |
6984 | * Because we can run out of memory before filling the mbuf | |
6985 | * map, we should not allocate more clusters than they are | |
6986 | * mbufs -- otherwise we could have a large number of useless | |
6987 | * clusters allocated. | |
91447636 | 6988 | */ |
d9a64523 A |
6989 | mbwdog_logger("totals: MC_MBUF %d MC_BIGCL %d MC_CL %d MC_16KCL %d", |
6990 | m_total(MC_MBUF), m_total(MC_BIGCL), m_total(MC_CL), | |
6991 | m_total(MC_16KCL)); | |
6992 | uint32_t total_mbufs = m_total(MC_MBUF); | |
6993 | uint32_t total_clusters = m_total(MC_BIGCL) + m_total(MC_CL) + | |
6994 | m_total(MC_16KCL); | |
6995 | if (total_mbufs < total_clusters) { | |
6996 | mbwdog_logger("expanding MC_MBUF by %d", | |
0a7de745 | 6997 | total_clusters - total_mbufs); |
d9a64523 A |
6998 | } |
6999 | while (total_mbufs < total_clusters) { | |
a39ff7e2 | 7000 | mb_expand_cnt++; |
0a7de745 | 7001 | if (freelist_populate(MC_MBUF, 1, M_WAIT) == 0) { |
a39ff7e2 | 7002 | break; |
0a7de745 | 7003 | } |
d9a64523 A |
7004 | total_mbufs = m_total(MC_MBUF); |
7005 | total_clusters = m_total(MC_BIGCL) + m_total(MC_CL) + | |
7006 | m_total(MC_16KCL); | |
91447636 | 7007 | } |
2d21ac55 | 7008 | |
39037602 | 7009 | mbuf_worker_needs_wakeup = TRUE; |
cc8bc92a A |
7010 | /* |
7011 | * If there's a deadlock and we're not sending / receiving | |
7012 | * packets, net_uptime() won't be updated. Update it here | |
7013 | * so we are sure it's correct. | |
7014 | */ | |
7015 | net_update_uptime(); | |
7016 | mbuf_worker_last_runtime = net_uptime(); | |
39037602 A |
7017 | assert_wait((caddr_t)&mbuf_worker_needs_wakeup, |
7018 | THREAD_UNINT); | |
d9a64523 | 7019 | mbwdog_logger("worker thread sleeping"); |
2d21ac55 | 7020 | lck_mtx_unlock(mbuf_mlock); |
2d21ac55 | 7021 | (void) thread_block((thread_continue_t)mbuf_worker_thread); |
91447636 | 7022 | } |
1c79356b A |
7023 | } |
7024 | ||
39037602 | 7025 | __attribute__((noreturn)) |
91447636 | 7026 | static void |
2d21ac55 | 7027 | mbuf_worker_thread_init(void) |
55e303ae | 7028 | { |
2d21ac55 A |
7029 | mbuf_worker_ready++; |
7030 | mbuf_worker_thread(); | |
55e303ae | 7031 | } |
1c79356b | 7032 | |
2d21ac55 A |
7033 | static mcl_slab_t * |
7034 | slab_get(void *buf) | |
7035 | { | |
7036 | mcl_slabg_t *slg; | |
7037 | unsigned int ix, k; | |
7038 | ||
5ba3f43e | 7039 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
2d21ac55 A |
7040 | |
7041 | VERIFY(MBUF_IN_MAP(buf)); | |
3e170ce0 | 7042 | ix = ((unsigned char *)buf - mbutl) >> MBSHIFT; |
2d21ac55 A |
7043 | VERIFY(ix < maxslabgrp); |
7044 | ||
7045 | if ((slg = slabstbl[ix]) == NULL) { | |
7046 | /* | |
39037602 | 7047 | * In the current implementation, we never shrink the slabs |
fe8ab488 A |
7048 | * table; if we attempt to reallocate a cluster group when |
7049 | * it's already allocated, panic since this is a sign of a | |
7050 | * memory corruption (slabstbl[ix] got nullified). | |
2d21ac55 A |
7051 | */ |
7052 | ++slabgrp; | |
7053 | VERIFY(ix < slabgrp); | |
7054 | /* | |
7055 | * Slabs expansion can only be done single threaded; when | |
7056 | * we get here, it must be as a result of m_clalloc() which | |
7057 | * is serialized and therefore mb_clalloc_busy must be set. | |
7058 | */ | |
7059 | VERIFY(mb_clalloc_busy); | |
7060 | lck_mtx_unlock(mbuf_mlock); | |
7061 | ||
7062 | /* This is a new buffer; create the slabs group for it */ | |
0a7de745 | 7063 | MALLOC(slg, mcl_slabg_t *, sizeof(*slg), M_TEMP, |
2d21ac55 | 7064 | M_WAITOK | M_ZERO); |
3e170ce0 A |
7065 | MALLOC(slg->slg_slab, mcl_slab_t *, sizeof(mcl_slab_t) * NSLABSPMB, |
7066 | M_TEMP, M_WAITOK | M_ZERO); | |
7067 | VERIFY(slg != NULL && slg->slg_slab != NULL); | |
2d21ac55 A |
7068 | |
7069 | lck_mtx_lock(mbuf_mlock); | |
7070 | /* | |
7071 | * No other thread could have gone into m_clalloc() after | |
7072 | * we dropped the lock above, so verify that it's true. | |
7073 | */ | |
7074 | VERIFY(mb_clalloc_busy); | |
7075 | ||
7076 | slabstbl[ix] = slg; | |
7077 | ||
7078 | /* Chain each slab in the group to its forward neighbor */ | |
0a7de745 | 7079 | for (k = 1; k < NSLABSPMB; k++) { |
2d21ac55 | 7080 | slg->slg_slab[k - 1].sl_next = &slg->slg_slab[k]; |
0a7de745 | 7081 | } |
2d21ac55 A |
7082 | VERIFY(slg->slg_slab[NSLABSPMB - 1].sl_next == NULL); |
7083 | ||
7084 | /* And chain the last slab in the previous group to this */ | |
7085 | if (ix > 0) { | |
7086 | VERIFY(slabstbl[ix - 1]-> | |
7087 | slg_slab[NSLABSPMB - 1].sl_next == NULL); | |
7088 | slabstbl[ix - 1]->slg_slab[NSLABSPMB - 1].sl_next = | |
7089 | &slg->slg_slab[0]; | |
7090 | } | |
7091 | } | |
7092 | ||
3e170ce0 | 7093 | ix = MTOPG(buf) % NSLABSPMB; |
2d21ac55 A |
7094 | VERIFY(ix < NSLABSPMB); |
7095 | ||
0a7de745 | 7096 | return &slg->slg_slab[ix]; |
2d21ac55 A |
7097 | } |
7098 | ||
7099 | static void | |
7100 | slab_init(mcl_slab_t *sp, mbuf_class_t class, u_int32_t flags, | |
7101 | void *base, void *head, unsigned int len, int refcnt, int chunks) | |
7102 | { | |
7103 | sp->sl_class = class; | |
7104 | sp->sl_flags = flags; | |
7105 | sp->sl_base = base; | |
7106 | sp->sl_head = head; | |
7107 | sp->sl_len = len; | |
7108 | sp->sl_refcnt = refcnt; | |
7109 | sp->sl_chunks = chunks; | |
7110 | slab_detach(sp); | |
7111 | } | |
7112 | ||
7113 | static void | |
7114 | slab_insert(mcl_slab_t *sp, mbuf_class_t class) | |
7115 | { | |
7116 | VERIFY(slab_is_detached(sp)); | |
7117 | m_slab_cnt(class)++; | |
7118 | TAILQ_INSERT_TAIL(&m_slablist(class), sp, sl_link); | |
7119 | sp->sl_flags &= ~SLF_DETACHED; | |
3e170ce0 A |
7120 | |
7121 | /* | |
7122 | * If a buffer spans multiple contiguous pages then mark them as | |
7123 | * detached too | |
7124 | */ | |
6d2010ae | 7125 | if (class == MC_16KCL) { |
2d21ac55 | 7126 | int k; |
6d2010ae | 7127 | for (k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
7128 | sp = sp->sl_next; |
7129 | /* Next slab must already be present */ | |
3e170ce0 | 7130 | VERIFY(sp != NULL && slab_is_detached(sp)); |
2d21ac55 A |
7131 | sp->sl_flags &= ~SLF_DETACHED; |
7132 | } | |
7133 | } | |
7134 | } | |
7135 | ||
7136 | static void | |
7137 | slab_remove(mcl_slab_t *sp, mbuf_class_t class) | |
7138 | { | |
3e170ce0 | 7139 | int k; |
2d21ac55 A |
7140 | VERIFY(!slab_is_detached(sp)); |
7141 | VERIFY(m_slab_cnt(class) > 0); | |
7142 | m_slab_cnt(class)--; | |
7143 | TAILQ_REMOVE(&m_slablist(class), sp, sl_link); | |
7144 | slab_detach(sp); | |
6d2010ae | 7145 | if (class == MC_16KCL) { |
6d2010ae | 7146 | for (k = 1; k < NSLABSP16KB; k++) { |
2d21ac55 A |
7147 | sp = sp->sl_next; |
7148 | /* Next slab must already be present */ | |
7149 | VERIFY(sp != NULL); | |
7150 | VERIFY(!slab_is_detached(sp)); | |
7151 | slab_detach(sp); | |
7152 | } | |
7153 | } | |
7154 | } | |
7155 | ||
7156 | static boolean_t | |
7157 | slab_inrange(mcl_slab_t *sp, void *buf) | |
7158 | { | |
0a7de745 A |
7159 | return (uintptr_t)buf >= (uintptr_t)sp->sl_base && |
7160 | (uintptr_t)buf < ((uintptr_t)sp->sl_base + sp->sl_len); | |
2d21ac55 A |
7161 | } |
7162 | ||
b0d623f7 | 7163 | #undef panic |
2d21ac55 A |
7164 | |
7165 | static void | |
7166 | slab_nextptr_panic(mcl_slab_t *sp, void *addr) | |
7167 | { | |
7168 | int i; | |
7169 | unsigned int chunk_len = sp->sl_len / sp->sl_chunks; | |
7170 | uintptr_t buf = (uintptr_t)sp->sl_base; | |
7171 | ||
7172 | for (i = 0; i < sp->sl_chunks; i++, buf += chunk_len) { | |
7173 | void *next = ((mcache_obj_t *)buf)->obj_next; | |
0a7de745 | 7174 | if (next != addr) { |
2d21ac55 | 7175 | continue; |
0a7de745 | 7176 | } |
6d2010ae | 7177 | if (!mclverify) { |
2d21ac55 A |
7178 | if (next != NULL && !MBUF_IN_MAP(next)) { |
7179 | mcache_t *cp = m_cache(sp->sl_class); | |
7180 | panic("%s: %s buffer %p in slab %p modified " | |
7181 | "after free at offset 0: %p out of range " | |
7182 | "[%p-%p)\n", __func__, cp->mc_name, | |
7183 | (void *)buf, sp, next, mbutl, embutl); | |
7184 | /* NOTREACHED */ | |
7185 | } | |
7186 | } else { | |
7187 | mcache_audit_t *mca = mcl_audit_buf2mca(sp->sl_class, | |
7188 | (mcache_obj_t *)buf); | |
7189 | mcl_audit_verify_nextptr(next, mca); | |
7190 | } | |
7191 | } | |
7192 | } | |
7193 | ||
7194 | static void | |
7195 | slab_detach(mcl_slab_t *sp) | |
7196 | { | |
7197 | sp->sl_link.tqe_next = (mcl_slab_t *)-1; | |
7198 | sp->sl_link.tqe_prev = (mcl_slab_t **)-1; | |
7199 | sp->sl_flags |= SLF_DETACHED; | |
7200 | } | |
7201 | ||
7202 | static boolean_t | |
7203 | slab_is_detached(mcl_slab_t *sp) | |
7204 | { | |
0a7de745 A |
7205 | return (intptr_t)sp->sl_link.tqe_next == -1 && |
7206 | (intptr_t)sp->sl_link.tqe_prev == -1 && | |
7207 | (sp->sl_flags & SLF_DETACHED); | |
2d21ac55 A |
7208 | } |
7209 | ||
7210 | static void | |
7211 | mcl_audit_init(void *buf, mcache_audit_t **mca_list, | |
7212 | mcache_obj_t **con_list, size_t con_size, unsigned int num) | |
7213 | { | |
7214 | mcache_audit_t *mca, *mca_tail; | |
7215 | mcache_obj_t *con = NULL; | |
7216 | boolean_t save_contents = (con_list != NULL); | |
7217 | unsigned int i, ix; | |
7218 | ||
3e170ce0 | 7219 | ASSERT(num <= NMBPG); |
2d21ac55 A |
7220 | ASSERT(con_list == NULL || con_size != 0); |
7221 | ||
3e170ce0 | 7222 | ix = MTOPG(buf); |
6d2010ae A |
7223 | VERIFY(ix < maxclaudit); |
7224 | ||
2d21ac55 | 7225 | /* Make sure we haven't been here before */ |
0a7de745 | 7226 | for (i = 0; i < num; i++) { |
2d21ac55 | 7227 | VERIFY(mclaudit[ix].cl_audit[i] == NULL); |
0a7de745 | 7228 | } |
2d21ac55 A |
7229 | |
7230 | mca = mca_tail = *mca_list; | |
0a7de745 | 7231 | if (save_contents) { |
2d21ac55 | 7232 | con = *con_list; |
0a7de745 | 7233 | } |
2d21ac55 A |
7234 | |
7235 | for (i = 0; i < num; i++) { | |
7236 | mcache_audit_t *next; | |
7237 | ||
7238 | next = mca->mca_next; | |
0a7de745 | 7239 | bzero(mca, sizeof(*mca)); |
2d21ac55 A |
7240 | mca->mca_next = next; |
7241 | mclaudit[ix].cl_audit[i] = mca; | |
7242 | ||
7243 | /* Attach the contents buffer if requested */ | |
7244 | if (save_contents) { | |
39236c6e A |
7245 | mcl_saved_contents_t *msc = |
7246 | (mcl_saved_contents_t *)(void *)con; | |
7247 | ||
7248 | VERIFY(msc != NULL); | |
0a7de745 A |
7249 | VERIFY(IS_P2ALIGNED(msc, sizeof(u_int64_t))); |
7250 | VERIFY(con_size == sizeof(*msc)); | |
2d21ac55 | 7251 | mca->mca_contents_size = con_size; |
39236c6e | 7252 | mca->mca_contents = msc; |
2d21ac55 A |
7253 | con = con->obj_next; |
7254 | bzero(mca->mca_contents, mca->mca_contents_size); | |
7255 | } | |
7256 | ||
7257 | mca_tail = mca; | |
7258 | mca = mca->mca_next; | |
7259 | } | |
91447636 | 7260 | |
0a7de745 | 7261 | if (save_contents) { |
2d21ac55 | 7262 | *con_list = con; |
0a7de745 | 7263 | } |
2d21ac55 A |
7264 | |
7265 | *mca_list = mca_tail->mca_next; | |
7266 | mca_tail->mca_next = NULL; | |
7267 | } | |
7268 | ||
fe8ab488 A |
7269 | static void |
7270 | mcl_audit_free(void *buf, unsigned int num) | |
7271 | { | |
7272 | unsigned int i, ix; | |
7273 | mcache_audit_t *mca, *mca_list; | |
7274 | ||
3e170ce0 | 7275 | ix = MTOPG(buf); |
fe8ab488 | 7276 | VERIFY(ix < maxclaudit); |
39037602 | 7277 | |
fe8ab488 A |
7278 | if (mclaudit[ix].cl_audit[0] != NULL) { |
7279 | mca_list = mclaudit[ix].cl_audit[0]; | |
7280 | for (i = 0; i < num; i++) { | |
7281 | mca = mclaudit[ix].cl_audit[i]; | |
7282 | mclaudit[ix].cl_audit[i] = NULL; | |
0a7de745 | 7283 | if (mca->mca_contents) { |
fe8ab488 A |
7284 | mcache_free(mcl_audit_con_cache, |
7285 | mca->mca_contents); | |
0a7de745 | 7286 | } |
fe8ab488 A |
7287 | } |
7288 | mcache_free_ext(mcache_audit_cache, | |
7289 | (mcache_obj_t *)mca_list); | |
7290 | } | |
7291 | } | |
7292 | ||
2d21ac55 | 7293 | /* |
6d2010ae | 7294 | * Given an address of a buffer (mbuf/2KB/4KB/16KB), return |
2d21ac55 A |
7295 | * the corresponding audit structure for that buffer. |
7296 | */ | |
7297 | static mcache_audit_t * | |
3e170ce0 | 7298 | mcl_audit_buf2mca(mbuf_class_t class, mcache_obj_t *mobj) |
2d21ac55 A |
7299 | { |
7300 | mcache_audit_t *mca = NULL; | |
3e170ce0 A |
7301 | int ix = MTOPG(mobj), m_idx = 0; |
7302 | unsigned char *page_addr; | |
2d21ac55 | 7303 | |
6d2010ae | 7304 | VERIFY(ix < maxclaudit); |
3e170ce0 A |
7305 | VERIFY(IS_P2ALIGNED(mobj, MIN(m_maxsize(class), PAGE_SIZE))); |
7306 | ||
7307 | page_addr = PGTOM(ix); | |
2d21ac55 A |
7308 | |
7309 | switch (class) { | |
7310 | case MC_MBUF: | |
7311 | /* | |
6d2010ae | 7312 | * For the mbuf case, find the index of the page |
2d21ac55 | 7313 | * used by the mbuf and use that index to locate the |
6d2010ae A |
7314 | * base address of the page. Then find out the |
7315 | * mbuf index relative to the page base and use | |
2d21ac55 A |
7316 | * it to locate the audit structure. |
7317 | */ | |
3e170ce0 A |
7318 | m_idx = MBPAGEIDX(page_addr, mobj); |
7319 | VERIFY(m_idx < (int)NMBPG); | |
7320 | mca = mclaudit[ix].cl_audit[m_idx]; | |
2d21ac55 A |
7321 | break; |
7322 | ||
7323 | case MC_CL: | |
6d2010ae A |
7324 | /* |
7325 | * Same thing as above, but for 2KB clusters in a page. | |
7326 | */ | |
3e170ce0 A |
7327 | m_idx = CLPAGEIDX(page_addr, mobj); |
7328 | VERIFY(m_idx < (int)NCLPG); | |
7329 | mca = mclaudit[ix].cl_audit[m_idx]; | |
6d2010ae A |
7330 | break; |
7331 | ||
2d21ac55 | 7332 | case MC_BIGCL: |
3e170ce0 A |
7333 | m_idx = BCLPAGEIDX(page_addr, mobj); |
7334 | VERIFY(m_idx < (int)NBCLPG); | |
7335 | mca = mclaudit[ix].cl_audit[m_idx]; | |
7336 | break; | |
2d21ac55 A |
7337 | case MC_16KCL: |
7338 | /* | |
7339 | * Same as above, but only return the first element. | |
7340 | */ | |
7341 | mca = mclaudit[ix].cl_audit[0]; | |
7342 | break; | |
7343 | ||
7344 | default: | |
7345 | VERIFY(0); | |
7346 | /* NOTREACHED */ | |
7347 | } | |
7348 | ||
0a7de745 | 7349 | return mca; |
2d21ac55 A |
7350 | } |
7351 | ||
7352 | static void | |
7353 | mcl_audit_mbuf(mcache_audit_t *mca, void *addr, boolean_t composite, | |
7354 | boolean_t alloc) | |
7355 | { | |
7356 | struct mbuf *m = addr; | |
7357 | mcache_obj_t *next = ((mcache_obj_t *)m)->obj_next; | |
7358 | ||
7359 | VERIFY(mca->mca_contents != NULL && | |
7360 | mca->mca_contents_size == AUDIT_CONTENTS_SIZE); | |
7361 | ||
0a7de745 | 7362 | if (mclverify) { |
6d2010ae | 7363 | mcl_audit_verify_nextptr(next, mca); |
0a7de745 | 7364 | } |
2d21ac55 A |
7365 | |
7366 | if (!alloc) { | |
7367 | /* Save constructed mbuf fields */ | |
7368 | mcl_audit_save_mbuf(m, mca); | |
6d2010ae A |
7369 | if (mclverify) { |
7370 | mcache_set_pattern(MCACHE_FREE_PATTERN, m, | |
7371 | m_maxsize(MC_MBUF)); | |
7372 | } | |
2d21ac55 A |
7373 | ((mcache_obj_t *)m)->obj_next = next; |
7374 | return; | |
7375 | } | |
7376 | ||
7377 | /* Check if the buffer has been corrupted while in freelist */ | |
6d2010ae A |
7378 | if (mclverify) { |
7379 | mcache_audit_free_verify_set(mca, addr, 0, m_maxsize(MC_MBUF)); | |
7380 | } | |
2d21ac55 A |
7381 | /* Restore constructed mbuf fields */ |
7382 | mcl_audit_restore_mbuf(m, mca, composite); | |
7383 | } | |
7384 | ||
7385 | static void | |
7386 | mcl_audit_restore_mbuf(struct mbuf *m, mcache_audit_t *mca, boolean_t composite) | |
7387 | { | |
39236c6e | 7388 | struct mbuf *ms = MCA_SAVED_MBUF_PTR(mca); |
2d21ac55 A |
7389 | |
7390 | if (composite) { | |
7391 | struct mbuf *next = m->m_next; | |
813fb2f6 | 7392 | VERIFY(ms->m_flags == M_EXT && m_get_rfa(ms) != NULL && |
2d21ac55 | 7393 | MBUF_IS_COMPOSITE(ms)); |
39236c6e | 7394 | VERIFY(mca->mca_contents_size == AUDIT_CONTENTS_SIZE); |
2d21ac55 A |
7395 | /* |
7396 | * We could have hand-picked the mbuf fields and restore | |
7397 | * them individually, but that will be a maintenance | |
7398 | * headache. Instead, restore everything that was saved; | |
7399 | * the mbuf layer will recheck and reinitialize anyway. | |
7400 | */ | |
39236c6e | 7401 | bcopy(ms, m, MCA_SAVED_MBUF_SIZE); |
2d21ac55 A |
7402 | m->m_next = next; |
7403 | } else { | |
7404 | /* | |
7405 | * For a regular mbuf (no cluster attached) there's nothing | |
7406 | * to restore other than the type field, which is expected | |
7407 | * to be MT_FREE. | |
7408 | */ | |
7409 | m->m_type = ms->m_type; | |
7410 | } | |
7411 | _MCHECK(m); | |
7412 | } | |
7413 | ||
7414 | static void | |
7415 | mcl_audit_save_mbuf(struct mbuf *m, mcache_audit_t *mca) | |
7416 | { | |
39236c6e | 7417 | VERIFY(mca->mca_contents_size == AUDIT_CONTENTS_SIZE); |
2d21ac55 | 7418 | _MCHECK(m); |
39236c6e | 7419 | bcopy(m, MCA_SAVED_MBUF_PTR(mca), MCA_SAVED_MBUF_SIZE); |
2d21ac55 A |
7420 | } |
7421 | ||
7422 | static void | |
7423 | mcl_audit_cluster(mcache_audit_t *mca, void *addr, size_t size, boolean_t alloc, | |
7424 | boolean_t save_next) | |
7425 | { | |
7426 | mcache_obj_t *next = ((mcache_obj_t *)addr)->obj_next; | |
7427 | ||
7428 | if (!alloc) { | |
6d2010ae A |
7429 | if (mclverify) { |
7430 | mcache_set_pattern(MCACHE_FREE_PATTERN, addr, size); | |
7431 | } | |
2d21ac55 A |
7432 | if (save_next) { |
7433 | mcl_audit_verify_nextptr(next, mca); | |
7434 | ((mcache_obj_t *)addr)->obj_next = next; | |
7435 | } | |
6d2010ae | 7436 | } else if (mclverify) { |
2d21ac55 A |
7437 | /* Check if the buffer has been corrupted while in freelist */ |
7438 | mcl_audit_verify_nextptr(next, mca); | |
7439 | mcache_audit_free_verify_set(mca, addr, 0, size); | |
7440 | } | |
7441 | } | |
7442 | ||
39236c6e A |
7443 | static void |
7444 | mcl_audit_scratch(mcache_audit_t *mca) | |
7445 | { | |
7446 | void *stack[MCACHE_STACK_DEPTH + 1]; | |
7447 | mcl_scratch_audit_t *msa; | |
7448 | struct timeval now; | |
7449 | ||
7450 | VERIFY(mca->mca_contents != NULL); | |
7451 | msa = MCA_SAVED_SCRATCH_PTR(mca); | |
7452 | ||
7453 | msa->msa_pthread = msa->msa_thread; | |
7454 | msa->msa_thread = current_thread(); | |
0a7de745 | 7455 | bcopy(msa->msa_stack, msa->msa_pstack, sizeof(msa->msa_pstack)); |
39236c6e | 7456 | msa->msa_pdepth = msa->msa_depth; |
0a7de745 | 7457 | bzero(stack, sizeof(stack)); |
39236c6e | 7458 | msa->msa_depth = OSBacktrace(stack, MCACHE_STACK_DEPTH + 1) - 1; |
0a7de745 | 7459 | bcopy(&stack[1], msa->msa_stack, sizeof(msa->msa_stack)); |
39236c6e A |
7460 | |
7461 | msa->msa_ptstamp = msa->msa_tstamp; | |
7462 | microuptime(&now); | |
7463 | /* tstamp is in ms relative to base_ts */ | |
7464 | msa->msa_tstamp = ((now.tv_usec - mb_start.tv_usec) / 1000); | |
0a7de745 | 7465 | if ((now.tv_sec - mb_start.tv_sec) > 0) { |
39236c6e | 7466 | msa->msa_tstamp += ((now.tv_sec - mb_start.tv_sec) * 1000); |
0a7de745 | 7467 | } |
39236c6e A |
7468 | } |
7469 | ||
cb323159 | 7470 | __abortlike |
2d21ac55 A |
7471 | static void |
7472 | mcl_audit_mcheck_panic(struct mbuf *m) | |
7473 | { | |
7474 | mcache_audit_t *mca; | |
7475 | ||
7476 | MRANGE(m); | |
7477 | mca = mcl_audit_buf2mca(MC_MBUF, (mcache_obj_t *)m); | |
7478 | ||
7479 | panic("mcl_audit: freed mbuf %p with type 0x%x (instead of 0x%x)\n%s\n", | |
7480 | m, (u_int16_t)m->m_type, MT_FREE, mcache_dump_mca(mca)); | |
7481 | /* NOTREACHED */ | |
7482 | } | |
7483 | ||
7484 | static void | |
7485 | mcl_audit_verify_nextptr(void *next, mcache_audit_t *mca) | |
7486 | { | |
6d2010ae A |
7487 | if (next != NULL && !MBUF_IN_MAP(next) && |
7488 | (next != (void *)MCACHE_FREE_PATTERN || !mclverify)) { | |
2d21ac55 A |
7489 | panic("mcl_audit: buffer %p modified after free at offset 0: " |
7490 | "%p out of range [%p-%p)\n%s\n", | |
7491 | mca->mca_addr, next, mbutl, embutl, mcache_dump_mca(mca)); | |
7492 | /* NOTREACHED */ | |
7493 | } | |
7494 | } | |
7495 | ||
6d2010ae A |
7496 | /* This function turns on mbuf leak detection */ |
7497 | static void | |
7498 | mleak_activate(void) | |
7499 | { | |
7500 | mleak_table.mleak_sample_factor = MLEAK_SAMPLE_FACTOR; | |
7501 | PE_parse_boot_argn("mleak_sample_factor", | |
7502 | &mleak_table.mleak_sample_factor, | |
0a7de745 | 7503 | sizeof(mleak_table.mleak_sample_factor)); |
6d2010ae | 7504 | |
0a7de745 | 7505 | if (mleak_table.mleak_sample_factor == 0) { |
6d2010ae | 7506 | mclfindleak = 0; |
0a7de745 | 7507 | } |
6d2010ae | 7508 | |
0a7de745 | 7509 | if (mclfindleak == 0) { |
6d2010ae | 7510 | return; |
0a7de745 | 7511 | } |
6d2010ae A |
7512 | |
7513 | vm_size_t alloc_size = | |
0a7de745 A |
7514 | mleak_alloc_buckets * sizeof(struct mallocation); |
7515 | vm_size_t trace_size = mleak_trace_buckets * sizeof(struct mtrace); | |
6d2010ae A |
7516 | |
7517 | MALLOC(mleak_allocations, struct mallocation *, alloc_size, | |
7518 | M_TEMP, M_WAITOK | M_ZERO); | |
7519 | VERIFY(mleak_allocations != NULL); | |
7520 | ||
7521 | MALLOC(mleak_traces, struct mtrace *, trace_size, | |
7522 | M_TEMP, M_WAITOK | M_ZERO); | |
7523 | VERIFY(mleak_traces != NULL); | |
7524 | ||
7525 | MALLOC(mleak_stat, mleak_stat_t *, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES), | |
7526 | M_TEMP, M_WAITOK | M_ZERO); | |
7527 | VERIFY(mleak_stat != NULL); | |
7528 | mleak_stat->ml_cnt = MLEAK_NUM_TRACES; | |
7529 | #ifdef __LP64__ | |
7530 | mleak_stat->ml_isaddr64 = 1; | |
7531 | #endif /* __LP64__ */ | |
7532 | } | |
7533 | ||
7534 | static void | |
7535 | mleak_logger(u_int32_t num, mcache_obj_t *addr, boolean_t alloc) | |
7536 | { | |
7537 | int temp; | |
7538 | ||
0a7de745 | 7539 | if (mclfindleak == 0) { |
6d2010ae | 7540 | return; |
0a7de745 | 7541 | } |
6d2010ae | 7542 | |
0a7de745 A |
7543 | if (!alloc) { |
7544 | return mleak_free(addr); | |
7545 | } | |
6d2010ae A |
7546 | |
7547 | temp = atomic_add_32_ov(&mleak_table.mleak_capture, 1); | |
7548 | ||
7549 | if ((temp % mleak_table.mleak_sample_factor) == 0 && addr != NULL) { | |
7550 | uintptr_t bt[MLEAK_STACK_DEPTH]; | |
cb323159 | 7551 | int logged = backtrace(bt, MLEAK_STACK_DEPTH, NULL); |
6d2010ae A |
7552 | mleak_log(bt, addr, logged, num); |
7553 | } | |
7554 | } | |
7555 | ||
7556 | /* | |
7557 | * This function records the allocation in the mleak_allocations table | |
7558 | * and the backtrace in the mleak_traces table; if allocation slot is in use, | |
7559 | * replace old allocation with new one if the trace slot is in use, return | |
7560 | * (or increment refcount if same trace). | |
7561 | */ | |
7562 | static boolean_t | |
7563 | mleak_log(uintptr_t *bt, mcache_obj_t *addr, uint32_t depth, int num) | |
7564 | { | |
7565 | struct mallocation *allocation; | |
7566 | struct mtrace *trace; | |
7567 | uint32_t trace_index; | |
6d2010ae A |
7568 | |
7569 | /* Quit if someone else modifying the tables */ | |
7570 | if (!lck_mtx_try_lock_spin(mleak_lock)) { | |
7571 | mleak_table.total_conflicts++; | |
0a7de745 | 7572 | return FALSE; |
6d2010ae A |
7573 | } |
7574 | ||
7575 | allocation = &mleak_allocations[hashaddr((uintptr_t)addr, | |
7576 | mleak_alloc_buckets)]; | |
7577 | trace_index = hashbacktrace(bt, depth, mleak_trace_buckets); | |
7578 | trace = &mleak_traces[trace_index]; | |
7579 | ||
7580 | VERIFY(allocation <= &mleak_allocations[mleak_alloc_buckets - 1]); | |
7581 | VERIFY(trace <= &mleak_traces[mleak_trace_buckets - 1]); | |
7582 | ||
7583 | allocation->hitcount++; | |
7584 | trace->hitcount++; | |
7585 | ||
7586 | /* | |
7587 | * If the allocation bucket we want is occupied | |
7588 | * and the occupier has the same trace, just bail. | |
7589 | */ | |
7590 | if (allocation->element != NULL && | |
7591 | trace_index == allocation->trace_index) { | |
7592 | mleak_table.alloc_collisions++; | |
7593 | lck_mtx_unlock(mleak_lock); | |
0a7de745 | 7594 | return TRUE; |
6d2010ae A |
7595 | } |
7596 | ||
7597 | /* | |
7598 | * Store the backtrace in the traces array; | |
7599 | * Size of zero = trace bucket is free. | |
7600 | */ | |
7601 | if (trace->allocs > 0 && | |
0a7de745 | 7602 | bcmp(trace->addr, bt, (depth * sizeof(uintptr_t))) != 0) { |
6d2010ae A |
7603 | /* Different, unique trace, but the same hash! Bail out. */ |
7604 | trace->collisions++; | |
7605 | mleak_table.trace_collisions++; | |
7606 | lck_mtx_unlock(mleak_lock); | |
0a7de745 | 7607 | return TRUE; |
6d2010ae A |
7608 | } else if (trace->allocs > 0) { |
7609 | /* Same trace, already added, so increment refcount */ | |
7610 | trace->allocs++; | |
7611 | } else { | |
7612 | /* Found an unused trace bucket, so record the trace here */ | |
7613 | if (trace->depth != 0) { | |
7614 | /* this slot previously used but not currently in use */ | |
7615 | mleak_table.trace_overwrites++; | |
7616 | } | |
7617 | mleak_table.trace_recorded++; | |
7618 | trace->allocs = 1; | |
0a7de745 | 7619 | memcpy(trace->addr, bt, (depth * sizeof(uintptr_t))); |
6d2010ae A |
7620 | trace->depth = depth; |
7621 | trace->collisions = 0; | |
7622 | } | |
7623 | ||
7624 | /* Step 2: Store the allocation record in the allocations array */ | |
7625 | if (allocation->element != NULL) { | |
7626 | /* | |
7627 | * Replace an existing allocation. No need to preserve | |
7628 | * because only a subset of the allocations are being | |
7629 | * recorded anyway. | |
7630 | */ | |
7631 | mleak_table.alloc_collisions++; | |
7632 | } else if (allocation->trace_index != 0) { | |
7633 | mleak_table.alloc_overwrites++; | |
7634 | } | |
7635 | allocation->element = addr; | |
7636 | allocation->trace_index = trace_index; | |
7637 | allocation->count = num; | |
7638 | mleak_table.alloc_recorded++; | |
7639 | mleak_table.outstanding_allocs++; | |
7640 | ||
6d2010ae | 7641 | lck_mtx_unlock(mleak_lock); |
0a7de745 | 7642 | return TRUE; |
6d2010ae A |
7643 | } |
7644 | ||
7645 | static void | |
7646 | mleak_free(mcache_obj_t *addr) | |
7647 | { | |
7648 | while (addr != NULL) { | |
7649 | struct mallocation *allocation = &mleak_allocations | |
7650 | [hashaddr((uintptr_t)addr, mleak_alloc_buckets)]; | |
7651 | ||
7652 | if (allocation->element == addr && | |
7653 | allocation->trace_index < mleak_trace_buckets) { | |
7654 | lck_mtx_lock_spin(mleak_lock); | |
7655 | if (allocation->element == addr && | |
7656 | allocation->trace_index < mleak_trace_buckets) { | |
7657 | struct mtrace *trace; | |
7658 | trace = &mleak_traces[allocation->trace_index]; | |
7659 | /* allocs = 0 means trace bucket is unused */ | |
0a7de745 | 7660 | if (trace->allocs > 0) { |
6d2010ae | 7661 | trace->allocs--; |
0a7de745 A |
7662 | } |
7663 | if (trace->allocs == 0) { | |
6d2010ae | 7664 | trace->depth = 0; |
0a7de745 | 7665 | } |
6d2010ae A |
7666 | /* NULL element means alloc bucket is unused */ |
7667 | allocation->element = NULL; | |
7668 | mleak_table.outstanding_allocs--; | |
7669 | } | |
7670 | lck_mtx_unlock(mleak_lock); | |
7671 | } | |
7672 | addr = addr->obj_next; | |
7673 | } | |
7674 | } | |
7675 | ||
316670eb A |
7676 | static void |
7677 | mleak_sort_traces() | |
7678 | { | |
7679 | int i, j, k; | |
7680 | struct mtrace *swap; | |
7681 | ||
0a7de745 | 7682 | for (i = 0; i < MLEAK_NUM_TRACES; i++) { |
316670eb | 7683 | mleak_top_trace[i] = NULL; |
0a7de745 | 7684 | } |
316670eb | 7685 | |
0a7de745 A |
7686 | for (i = 0, j = 0; j < MLEAK_NUM_TRACES && i < mleak_trace_buckets; i++) { |
7687 | if (mleak_traces[i].allocs <= 0) { | |
316670eb | 7688 | continue; |
0a7de745 | 7689 | } |
316670eb A |
7690 | |
7691 | mleak_top_trace[j] = &mleak_traces[i]; | |
7692 | for (k = j; k > 0; k--) { | |
7693 | if (mleak_top_trace[k]->allocs <= | |
0a7de745 | 7694 | mleak_top_trace[k - 1]->allocs) { |
316670eb | 7695 | break; |
0a7de745 | 7696 | } |
316670eb | 7697 | |
0a7de745 A |
7698 | swap = mleak_top_trace[k - 1]; |
7699 | mleak_top_trace[k - 1] = mleak_top_trace[k]; | |
316670eb A |
7700 | mleak_top_trace[k] = swap; |
7701 | } | |
7702 | j++; | |
7703 | } | |
7704 | ||
7705 | j--; | |
0a7de745 A |
7706 | for (; i < mleak_trace_buckets; i++) { |
7707 | if (mleak_traces[i].allocs <= mleak_top_trace[j]->allocs) { | |
316670eb | 7708 | continue; |
0a7de745 | 7709 | } |
316670eb A |
7710 | |
7711 | mleak_top_trace[j] = &mleak_traces[i]; | |
7712 | ||
7713 | for (k = j; k > 0; k--) { | |
7714 | if (mleak_top_trace[k]->allocs <= | |
0a7de745 | 7715 | mleak_top_trace[k - 1]->allocs) { |
316670eb | 7716 | break; |
0a7de745 | 7717 | } |
316670eb | 7718 | |
0a7de745 A |
7719 | swap = mleak_top_trace[k - 1]; |
7720 | mleak_top_trace[k - 1] = mleak_top_trace[k]; | |
316670eb A |
7721 | mleak_top_trace[k] = swap; |
7722 | } | |
7723 | } | |
7724 | } | |
7725 | ||
7726 | static void | |
7727 | mleak_update_stats() | |
7728 | { | |
7729 | mleak_trace_stat_t *mltr; | |
7730 | int i; | |
7731 | ||
7732 | VERIFY(mleak_stat != NULL); | |
7733 | #ifdef __LP64__ | |
7734 | VERIFY(mleak_stat->ml_isaddr64); | |
7735 | #else | |
7736 | VERIFY(!mleak_stat->ml_isaddr64); | |
7737 | #endif /* !__LP64__ */ | |
7738 | VERIFY(mleak_stat->ml_cnt == MLEAK_NUM_TRACES); | |
7739 | ||
7740 | mleak_sort_traces(); | |
7741 | ||
7742 | mltr = &mleak_stat->ml_trace[0]; | |
0a7de745 | 7743 | bzero(mltr, sizeof(*mltr) * MLEAK_NUM_TRACES); |
316670eb | 7744 | for (i = 0; i < MLEAK_NUM_TRACES; i++) { |
5c9f4661 | 7745 | int j; |
316670eb A |
7746 | |
7747 | if (mleak_top_trace[i] == NULL || | |
0a7de745 | 7748 | mleak_top_trace[i]->allocs == 0) { |
316670eb | 7749 | continue; |
0a7de745 | 7750 | } |
316670eb | 7751 | |
0a7de745 A |
7752 | mltr->mltr_collisions = mleak_top_trace[i]->collisions; |
7753 | mltr->mltr_hitcount = mleak_top_trace[i]->hitcount; | |
7754 | mltr->mltr_allocs = mleak_top_trace[i]->allocs; | |
7755 | mltr->mltr_depth = mleak_top_trace[i]->depth; | |
316670eb A |
7756 | |
7757 | VERIFY(mltr->mltr_depth <= MLEAK_STACK_DEPTH); | |
0a7de745 | 7758 | for (j = 0; j < mltr->mltr_depth; j++) { |
316670eb | 7759 | mltr->mltr_addr[j] = mleak_top_trace[i]->addr[j]; |
0a7de745 | 7760 | } |
316670eb A |
7761 | |
7762 | mltr++; | |
7763 | } | |
7764 | } | |
7765 | ||
6d2010ae | 7766 | static struct mbtypes { |
0a7de745 A |
7767 | int mt_type; |
7768 | const char *mt_name; | |
6d2010ae | 7769 | } mbtypes[] = { |
0a7de745 A |
7770 | { MT_DATA, "data" }, |
7771 | { MT_OOBDATA, "oob data" }, | |
7772 | { MT_CONTROL, "ancillary data" }, | |
7773 | { MT_HEADER, "packet headers" }, | |
7774 | { MT_SOCKET, "socket structures" }, | |
7775 | { MT_PCB, "protocol control blocks" }, | |
7776 | { MT_RTABLE, "routing table entries" }, | |
7777 | { MT_HTABLE, "IMP host table entries" }, | |
7778 | { MT_ATABLE, "address resolution tables" }, | |
7779 | { MT_FTABLE, "fragment reassembly queue headers" }, | |
7780 | { MT_SONAME, "socket names and addresses" }, | |
7781 | { MT_SOOPTS, "socket options" }, | |
7782 | { MT_RIGHTS, "access rights" }, | |
7783 | { MT_IFADDR, "interface addresses" }, | |
7784 | { MT_TAG, "packet tags" }, | |
7785 | { 0, NULL } | |
6d2010ae A |
7786 | }; |
7787 | ||
0a7de745 A |
7788 | #define MBUF_DUMP_BUF_CHK() { \ |
7789 | clen -= k; \ | |
7790 | if (clen < 1) \ | |
7791 | goto done; \ | |
7792 | c += k; \ | |
6d2010ae A |
7793 | } |
7794 | ||
7795 | static char * | |
7796 | mbuf_dump(void) | |
7797 | { | |
cc8bc92a A |
7798 | unsigned long totmem = 0, totfree = 0, totmbufs, totused, totpct, |
7799 | totreturned = 0; | |
6d2010ae A |
7800 | u_int32_t m_mbufs = 0, m_clfree = 0, m_bigclfree = 0; |
7801 | u_int32_t m_mbufclfree = 0, m_mbufbigclfree = 0; | |
7802 | u_int32_t m_16kclusters = 0, m_16kclfree = 0, m_mbuf16kclfree = 0; | |
0a7de745 | 7803 | int nmbtypes = sizeof(mbstat.m_mtypes) / sizeof(short); |
6d2010ae A |
7804 | uint8_t seen[256]; |
7805 | struct mbtypes *mp; | |
7806 | mb_class_stat_t *sp; | |
316670eb | 7807 | mleak_trace_stat_t *mltr; |
6d2010ae | 7808 | char *c = mbuf_dump_buf; |
5c9f4661 | 7809 | int i, j, k, clen = MBUF_DUMP_BUF_SIZE; |
d9a64523 | 7810 | bool printed_banner = false; |
6d2010ae A |
7811 | |
7812 | mbuf_dump_buf[0] = '\0'; | |
7813 | ||
7814 | /* synchronize all statistics in the mbuf table */ | |
7815 | mbuf_stat_sync(); | |
7816 | mbuf_mtypes_sync(TRUE); | |
7817 | ||
7818 | sp = &mb_stat->mbs_class[0]; | |
7819 | for (i = 0; i < mb_stat->mbs_cnt; i++, sp++) { | |
7820 | u_int32_t mem; | |
7821 | ||
7822 | if (m_class(i) == MC_MBUF) { | |
7823 | m_mbufs = sp->mbcl_active; | |
7824 | } else if (m_class(i) == MC_CL) { | |
7825 | m_clfree = sp->mbcl_total - sp->mbcl_active; | |
7826 | } else if (m_class(i) == MC_BIGCL) { | |
7827 | m_bigclfree = sp->mbcl_total - sp->mbcl_active; | |
7828 | } else if (njcl > 0 && m_class(i) == MC_16KCL) { | |
7829 | m_16kclfree = sp->mbcl_total - sp->mbcl_active; | |
7830 | m_16kclusters = sp->mbcl_total; | |
7831 | } else if (m_class(i) == MC_MBUF_CL) { | |
7832 | m_mbufclfree = sp->mbcl_total - sp->mbcl_active; | |
7833 | } else if (m_class(i) == MC_MBUF_BIGCL) { | |
7834 | m_mbufbigclfree = sp->mbcl_total - sp->mbcl_active; | |
7835 | } else if (njcl > 0 && m_class(i) == MC_MBUF_16KCL) { | |
7836 | m_mbuf16kclfree = sp->mbcl_total - sp->mbcl_active; | |
7837 | } | |
7838 | ||
7839 | mem = sp->mbcl_ctotal * sp->mbcl_size; | |
7840 | totmem += mem; | |
7841 | totfree += (sp->mbcl_mc_cached + sp->mbcl_infree) * | |
7842 | sp->mbcl_size; | |
cc8bc92a | 7843 | totreturned += sp->mbcl_release_cnt; |
6d2010ae A |
7844 | } |
7845 | ||
7846 | /* adjust free counts to include composite caches */ | |
7847 | m_clfree += m_mbufclfree; | |
7848 | m_bigclfree += m_mbufbigclfree; | |
7849 | m_16kclfree += m_mbuf16kclfree; | |
7850 | ||
7851 | totmbufs = 0; | |
0a7de745 | 7852 | for (mp = mbtypes; mp->mt_name != NULL; mp++) { |
6d2010ae | 7853 | totmbufs += mbstat.m_mtypes[mp->mt_type]; |
0a7de745 A |
7854 | } |
7855 | if (totmbufs > m_mbufs) { | |
6d2010ae | 7856 | totmbufs = m_mbufs; |
0a7de745 | 7857 | } |
4ba76501 | 7858 | k = scnprintf(c, clen, "%lu/%u mbufs in use:\n", totmbufs, m_mbufs); |
6d2010ae A |
7859 | MBUF_DUMP_BUF_CHK(); |
7860 | ||
0a7de745 | 7861 | bzero(&seen, sizeof(seen)); |
6d2010ae A |
7862 | for (mp = mbtypes; mp->mt_name != NULL; mp++) { |
7863 | if (mbstat.m_mtypes[mp->mt_type] != 0) { | |
7864 | seen[mp->mt_type] = 1; | |
4ba76501 | 7865 | k = scnprintf(c, clen, "\t%u mbufs allocated to %s\n", |
6d2010ae A |
7866 | mbstat.m_mtypes[mp->mt_type], mp->mt_name); |
7867 | MBUF_DUMP_BUF_CHK(); | |
7868 | } | |
7869 | } | |
7870 | seen[MT_FREE] = 1; | |
0a7de745 | 7871 | for (i = 0; i < nmbtypes; i++) { |
6d2010ae | 7872 | if (!seen[i] && mbstat.m_mtypes[i] != 0) { |
4ba76501 | 7873 | k = scnprintf(c, clen, "\t%u mbufs allocated to " |
6d2010ae A |
7874 | "<mbuf type %d>\n", mbstat.m_mtypes[i], i); |
7875 | MBUF_DUMP_BUF_CHK(); | |
7876 | } | |
0a7de745 | 7877 | } |
6d2010ae | 7878 | if ((m_mbufs - totmbufs) > 0) { |
4ba76501 | 7879 | k = scnprintf(c, clen, "\t%lu mbufs allocated to caches\n", |
6d2010ae A |
7880 | m_mbufs - totmbufs); |
7881 | MBUF_DUMP_BUF_CHK(); | |
7882 | } | |
4ba76501 | 7883 | k = scnprintf(c, clen, "%u/%u mbuf 2KB clusters in use\n" |
6d2010ae A |
7884 | "%u/%u mbuf 4KB clusters in use\n", |
7885 | (unsigned int)(mbstat.m_clusters - m_clfree), | |
7886 | (unsigned int)mbstat.m_clusters, | |
7887 | (unsigned int)(mbstat.m_bigclusters - m_bigclfree), | |
7888 | (unsigned int)mbstat.m_bigclusters); | |
7889 | MBUF_DUMP_BUF_CHK(); | |
7890 | ||
7891 | if (njcl > 0) { | |
4ba76501 | 7892 | k = scnprintf(c, clen, "%u/%u mbuf %uKB clusters in use\n", |
6d2010ae A |
7893 | m_16kclusters - m_16kclfree, m_16kclusters, |
7894 | njclbytes / 1024); | |
7895 | MBUF_DUMP_BUF_CHK(); | |
7896 | } | |
7897 | totused = totmem - totfree; | |
7898 | if (totmem == 0) { | |
7899 | totpct = 0; | |
7900 | } else if (totused < (ULONG_MAX / 100)) { | |
7901 | totpct = (totused * 100) / totmem; | |
7902 | } else { | |
7903 | u_long totmem1 = totmem / 100; | |
7904 | u_long totused1 = totused / 100; | |
7905 | totpct = (totused1 * 100) / totmem1; | |
7906 | } | |
4ba76501 | 7907 | k = scnprintf(c, clen, "%lu KB allocated to network (approx. %lu%% " |
6d2010ae A |
7908 | "in use)\n", totmem / 1024, totpct); |
7909 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 7910 | k = scnprintf(c, clen, "%lu KB returned to the system\n", |
cc8bc92a A |
7911 | totreturned / 1024); |
7912 | MBUF_DUMP_BUF_CHK(); | |
7913 | ||
7914 | net_update_uptime(); | |
4ba76501 | 7915 | k = scnprintf(c, clen, |
cc8bc92a A |
7916 | "VM allocation failures: contiguous %u, normal %u, one page %u\n", |
7917 | mb_kmem_contig_failed, mb_kmem_failed, mb_kmem_one_failed); | |
7918 | MBUF_DUMP_BUF_CHK(); | |
7919 | if (mb_kmem_contig_failed_ts || mb_kmem_failed_ts || | |
7920 | mb_kmem_one_failed_ts) { | |
4ba76501 | 7921 | k = scnprintf(c, clen, |
cc8bc92a A |
7922 | "VM allocation failure timestamps: contiguous %llu " |
7923 | "(size %llu), normal %llu (size %llu), one page %llu " | |
7924 | "(now %llu)\n", | |
7925 | mb_kmem_contig_failed_ts, mb_kmem_contig_failed_size, | |
7926 | mb_kmem_failed_ts, mb_kmem_failed_size, | |
7927 | mb_kmem_one_failed_ts, net_uptime()); | |
7928 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 7929 | k = scnprintf(c, clen, |
cc8bc92a A |
7930 | "VM return codes: "); |
7931 | MBUF_DUMP_BUF_CHK(); | |
7932 | for (i = 0; | |
0a7de745 A |
7933 | i < sizeof(mb_kmem_stats) / sizeof(mb_kmem_stats[0]); |
7934 | i++) { | |
4ba76501 | 7935 | k = scnprintf(c, clen, "%s: %u ", mb_kmem_stats_labels[i], |
cc8bc92a A |
7936 | mb_kmem_stats[i]); |
7937 | MBUF_DUMP_BUF_CHK(); | |
7938 | } | |
4ba76501 | 7939 | k = scnprintf(c, clen, "\n"); |
cc8bc92a A |
7940 | MBUF_DUMP_BUF_CHK(); |
7941 | } | |
4ba76501 | 7942 | k = scnprintf(c, clen, |
cc8bc92a A |
7943 | "worker thread runs: %u, expansions: %llu, cl %llu/%llu, " |
7944 | "bigcl %llu/%llu, 16k %llu/%llu\n", mbuf_worker_run_cnt, | |
7945 | mb_expand_cnt, mb_expand_cl_cnt, mb_expand_cl_total, | |
7946 | mb_expand_bigcl_cnt, mb_expand_bigcl_total, mb_expand_16kcl_cnt, | |
7947 | mb_expand_16kcl_total); | |
7948 | MBUF_DUMP_BUF_CHK(); | |
7949 | if (mbuf_worker_last_runtime != 0) { | |
4ba76501 | 7950 | k = scnprintf(c, clen, "worker thread last run time: " |
cc8bc92a A |
7951 | "%llu (%llu seconds ago)\n", |
7952 | mbuf_worker_last_runtime, | |
7953 | net_uptime() - mbuf_worker_last_runtime); | |
7954 | MBUF_DUMP_BUF_CHK(); | |
7955 | } | |
d9a64523 | 7956 | if (mbuf_drain_last_runtime != 0) { |
4ba76501 | 7957 | k = scnprintf(c, clen, "drain routine last run time: " |
d9a64523 A |
7958 | "%llu (%llu seconds ago)\n", |
7959 | mbuf_drain_last_runtime, | |
7960 | net_uptime() - mbuf_drain_last_runtime); | |
7961 | MBUF_DUMP_BUF_CHK(); | |
7962 | } | |
6d2010ae | 7963 | |
d9a64523 | 7964 | #if DEBUG || DEVELOPMENT |
4ba76501 | 7965 | k = scnprintf(c, clen, "\nworker thread log:\n%s\n", mbwdog_logging); |
5c9f4661 | 7966 | MBUF_DUMP_BUF_CHK(); |
d9a64523 | 7967 | #endif |
5c9f4661 A |
7968 | |
7969 | for (j = 0; j < MTRACELARGE_NUM_TRACES; j++) { | |
7970 | struct mtracelarge *trace = &mtracelarge_table[j]; | |
0a7de745 | 7971 | if (trace->size == 0 || trace->depth == 0) { |
5c9f4661 | 7972 | continue; |
0a7de745 | 7973 | } |
d9a64523 | 7974 | if (printed_banner == false) { |
4ba76501 | 7975 | k = scnprintf(c, clen, |
d9a64523 A |
7976 | "\nlargest allocation failure backtraces:\n"); |
7977 | MBUF_DUMP_BUF_CHK(); | |
7978 | printed_banner = true; | |
7979 | } | |
4ba76501 | 7980 | k = scnprintf(c, clen, "size %llu: < ", trace->size); |
5c9f4661 A |
7981 | MBUF_DUMP_BUF_CHK(); |
7982 | for (i = 0; i < trace->depth; i++) { | |
7983 | if (mleak_stat->ml_isaddr64) { | |
4ba76501 | 7984 | k = scnprintf(c, clen, "0x%0llx ", |
5c9f4661 A |
7985 | (uint64_t)VM_KERNEL_UNSLIDE( |
7986 | trace->addr[i])); | |
7987 | } else { | |
4ba76501 | 7988 | k = scnprintf(c, clen, |
5c9f4661 A |
7989 | "0x%08x ", |
7990 | (uint32_t)VM_KERNEL_UNSLIDE( | |
7991 | trace->addr[i])); | |
7992 | } | |
7993 | MBUF_DUMP_BUF_CHK(); | |
7994 | } | |
4ba76501 | 7995 | k = scnprintf(c, clen, ">\n"); |
5c9f4661 A |
7996 | MBUF_DUMP_BUF_CHK(); |
7997 | } | |
7998 | ||
316670eb A |
7999 | /* mbuf leak detection statistics */ |
8000 | mleak_update_stats(); | |
8001 | ||
4ba76501 | 8002 | k = scnprintf(c, clen, "\nmbuf leak detection table:\n"); |
316670eb | 8003 | MBUF_DUMP_BUF_CHK(); |
4ba76501 | 8004 | k = scnprintf(c, clen, "\ttotal captured: %u (one per %u)\n", |
316670eb A |
8005 | mleak_table.mleak_capture / mleak_table.mleak_sample_factor, |
8006 | mleak_table.mleak_sample_factor); | |
8007 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 8008 | k = scnprintf(c, clen, "\ttotal allocs outstanding: %llu\n", |
316670eb A |
8009 | mleak_table.outstanding_allocs); |
8010 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 8011 | k = scnprintf(c, clen, "\tnew hash recorded: %llu allocs, %llu traces\n", |
316670eb A |
8012 | mleak_table.alloc_recorded, mleak_table.trace_recorded); |
8013 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 8014 | k = scnprintf(c, clen, "\thash collisions: %llu allocs, %llu traces\n", |
316670eb A |
8015 | mleak_table.alloc_collisions, mleak_table.trace_collisions); |
8016 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 8017 | k = scnprintf(c, clen, "\toverwrites: %llu allocs, %llu traces\n", |
316670eb A |
8018 | mleak_table.alloc_overwrites, mleak_table.trace_overwrites); |
8019 | MBUF_DUMP_BUF_CHK(); | |
4ba76501 | 8020 | k = scnprintf(c, clen, "\tlock conflicts: %llu\n\n", |
316670eb A |
8021 | mleak_table.total_conflicts); |
8022 | MBUF_DUMP_BUF_CHK(); | |
8023 | ||
4ba76501 | 8024 | k = scnprintf(c, clen, "top %d outstanding traces:\n", |
316670eb A |
8025 | mleak_stat->ml_cnt); |
8026 | MBUF_DUMP_BUF_CHK(); | |
8027 | for (i = 0; i < mleak_stat->ml_cnt; i++) { | |
8028 | mltr = &mleak_stat->ml_trace[i]; | |
4ba76501 | 8029 | k = scnprintf(c, clen, "[%d] %llu outstanding alloc(s), " |
316670eb A |
8030 | "%llu hit(s), %llu collision(s)\n", (i + 1), |
8031 | mltr->mltr_allocs, mltr->mltr_hitcount, | |
8032 | mltr->mltr_collisions); | |
8033 | MBUF_DUMP_BUF_CHK(); | |
8034 | } | |
8035 | ||
0a7de745 | 8036 | if (mleak_stat->ml_isaddr64) { |
4ba76501 | 8037 | k = scnprintf(c, clen, MB_LEAK_HDR_64); |
0a7de745 | 8038 | } else { |
4ba76501 | 8039 | k = scnprintf(c, clen, MB_LEAK_HDR_32); |
0a7de745 | 8040 | } |
316670eb A |
8041 | MBUF_DUMP_BUF_CHK(); |
8042 | ||
8043 | for (i = 0; i < MLEAK_STACK_DEPTH; i++) { | |
4ba76501 | 8044 | k = scnprintf(c, clen, "%2d: ", (i + 1)); |
316670eb A |
8045 | MBUF_DUMP_BUF_CHK(); |
8046 | for (j = 0; j < mleak_stat->ml_cnt; j++) { | |
8047 | mltr = &mleak_stat->ml_trace[j]; | |
8048 | if (i < mltr->mltr_depth) { | |
8049 | if (mleak_stat->ml_isaddr64) { | |
4ba76501 | 8050 | k = scnprintf(c, clen, "0x%0llx ", |
fe8ab488 | 8051 | (uint64_t)VM_KERNEL_UNSLIDE( |
0a7de745 | 8052 | mltr->mltr_addr[i])); |
316670eb | 8053 | } else { |
4ba76501 | 8054 | k = scnprintf(c, clen, |
316670eb | 8055 | "0x%08x ", |
fe8ab488 | 8056 | (uint32_t)VM_KERNEL_UNSLIDE( |
0a7de745 | 8057 | mltr->mltr_addr[i])); |
316670eb A |
8058 | } |
8059 | } else { | |
0a7de745 | 8060 | if (mleak_stat->ml_isaddr64) { |
4ba76501 | 8061 | k = scnprintf(c, clen, |
316670eb | 8062 | MB_LEAK_SPACING_64); |
0a7de745 | 8063 | } else { |
4ba76501 | 8064 | k = scnprintf(c, clen, |
316670eb | 8065 | MB_LEAK_SPACING_32); |
0a7de745 | 8066 | } |
316670eb A |
8067 | } |
8068 | MBUF_DUMP_BUF_CHK(); | |
8069 | } | |
4ba76501 | 8070 | k = scnprintf(c, clen, "\n"); |
316670eb A |
8071 | MBUF_DUMP_BUF_CHK(); |
8072 | } | |
6d2010ae | 8073 | done: |
0a7de745 | 8074 | return mbuf_dump_buf; |
6d2010ae A |
8075 | } |
8076 | ||
8077 | #undef MBUF_DUMP_BUF_CHK | |
8078 | ||
39236c6e A |
8079 | /* |
8080 | * Convert between a regular and a packet header mbuf. Caller is responsible | |
8081 | * for setting or clearing M_PKTHDR; this routine does the rest of the work. | |
8082 | */ | |
8083 | int | |
8084 | m_reinit(struct mbuf *m, int hdr) | |
8085 | { | |
8086 | int ret = 0; | |
8087 | ||
8088 | if (hdr) { | |
8089 | VERIFY(!(m->m_flags & M_PKTHDR)); | |
8090 | if (!(m->m_flags & M_EXT) && | |
8091 | (m->m_data != m->m_dat || m->m_len > 0)) { | |
8092 | /* | |
8093 | * If there's no external cluster attached and the | |
8094 | * mbuf appears to contain user data, we cannot | |
8095 | * safely convert this to a packet header mbuf, | |
8096 | * as the packet header structure might overlap | |
8097 | * with the data. | |
8098 | */ | |
fe8ab488 A |
8099 | printf("%s: cannot set M_PKTHDR on altered mbuf %llx, " |
8100 | "m_data %llx (expected %llx), " | |
8101 | "m_len %d (expected 0)\n", | |
8102 | __func__, | |
8103 | (uint64_t)VM_KERNEL_ADDRPERM(m), | |
8104 | (uint64_t)VM_KERNEL_ADDRPERM(m->m_data), | |
8105 | (uint64_t)VM_KERNEL_ADDRPERM(m->m_dat), m->m_len); | |
39236c6e A |
8106 | ret = EBUSY; |
8107 | } else { | |
8108 | VERIFY((m->m_flags & M_EXT) || m->m_data == m->m_dat); | |
8109 | m->m_flags |= M_PKTHDR; | |
8110 | MBUF_INIT_PKTHDR(m); | |
8111 | } | |
8112 | } else { | |
8113 | /* Check for scratch area overflow */ | |
8114 | m_redzone_verify(m); | |
8115 | /* Free the aux data and tags if there is any */ | |
8116 | m_tag_delete_chain(m, NULL); | |
8117 | m->m_flags &= ~M_PKTHDR; | |
8118 | } | |
8119 | ||
0a7de745 | 8120 | return ret; |
39236c6e A |
8121 | } |
8122 | ||
39037602 A |
8123 | int |
8124 | m_ext_set_prop(struct mbuf *m, uint32_t o, uint32_t n) | |
8125 | { | |
8126 | ASSERT(m->m_flags & M_EXT); | |
0a7de745 | 8127 | return atomic_test_set_32(&MEXT_PRIV(m), o, n); |
39037602 A |
8128 | } |
8129 | ||
8130 | uint32_t | |
8131 | m_ext_get_prop(struct mbuf *m) | |
8132 | { | |
8133 | ASSERT(m->m_flags & M_EXT); | |
0a7de745 | 8134 | return MEXT_PRIV(m); |
39037602 A |
8135 | } |
8136 | ||
8137 | int | |
8138 | m_ext_paired_is_active(struct mbuf *m) | |
8139 | { | |
0a7de745 | 8140 | return MBUF_IS_PAIRED(m) ? (MEXT_PREF(m) > MEXT_MINREF(m)) : 1; |
39037602 A |
8141 | } |
8142 | ||
8143 | void | |
8144 | m_ext_paired_activate(struct mbuf *m) | |
8145 | { | |
8146 | struct ext_ref *rfa; | |
8147 | int hdr, type; | |
8148 | caddr_t extbuf; | |
813fb2f6 | 8149 | m_ext_free_func_t extfree; |
39037602 A |
8150 | u_int extsize; |
8151 | ||
8152 | VERIFY(MBUF_IS_PAIRED(m)); | |
8153 | VERIFY(MEXT_REF(m) == MEXT_MINREF(m)); | |
8154 | VERIFY(MEXT_PREF(m) == MEXT_MINREF(m)); | |
8155 | ||
8156 | hdr = (m->m_flags & M_PKTHDR); | |
8157 | type = m->m_type; | |
8158 | extbuf = m->m_ext.ext_buf; | |
813fb2f6 | 8159 | extfree = m_get_ext_free(m); |
39037602 | 8160 | extsize = m->m_ext.ext_size; |
813fb2f6 | 8161 | rfa = m_get_rfa(m); |
39037602 A |
8162 | |
8163 | VERIFY(extbuf != NULL && rfa != NULL); | |
8164 | ||
8165 | /* | |
8166 | * Safe to reinitialize packet header tags, since it's | |
8167 | * already taken care of at m_free() time. Similar to | |
8168 | * what's done in m_clattach() for the cluster. Bump | |
8169 | * up MEXT_PREF to indicate activation. | |
8170 | */ | |
8171 | MBUF_INIT(m, hdr, type); | |
8172 | MEXT_INIT(m, extbuf, extsize, extfree, (caddr_t)m, rfa, | |
8173 | 1, 1, 2, EXTF_PAIRED, MEXT_PRIV(m), m); | |
8174 | } | |
8175 | ||
39236c6e A |
8176 | void |
8177 | m_scratch_init(struct mbuf *m) | |
8178 | { | |
fe8ab488 A |
8179 | struct pkthdr *pkt = &m->m_pkthdr; |
8180 | ||
39236c6e A |
8181 | VERIFY(m->m_flags & M_PKTHDR); |
8182 | ||
fe8ab488 A |
8183 | /* See comments in <rdar://problem/14040693> */ |
8184 | if (pkt->pkt_flags & PKTF_PRIV_GUARDED) { | |
8185 | panic_plain("Invalid attempt to modify guarded module-private " | |
8186 | "area: mbuf %p, pkt_flags 0x%x\n", m, pkt->pkt_flags); | |
8187 | /* NOTREACHED */ | |
8188 | } | |
8189 | ||
0a7de745 | 8190 | bzero(&pkt->pkt_mpriv, sizeof(pkt->pkt_mpriv)); |
39236c6e A |
8191 | } |
8192 | ||
fe8ab488 A |
8193 | /* |
8194 | * This routine is reserved for mbuf_get_driver_scratch(); clients inside | |
8195 | * xnu that intend on utilizing the module-private area should directly | |
8196 | * refer to the pkt_mpriv structure in the pkthdr. They are also expected | |
8197 | * to set and clear PKTF_PRIV_GUARDED, while owning the packet and prior | |
8198 | * to handing it off to another module, respectively. | |
8199 | */ | |
39236c6e A |
8200 | u_int32_t |
8201 | m_scratch_get(struct mbuf *m, u_int8_t **p) | |
8202 | { | |
fe8ab488 A |
8203 | struct pkthdr *pkt = &m->m_pkthdr; |
8204 | ||
39236c6e A |
8205 | VERIFY(m->m_flags & M_PKTHDR); |
8206 | ||
fe8ab488 A |
8207 | /* See comments in <rdar://problem/14040693> */ |
8208 | if (pkt->pkt_flags & PKTF_PRIV_GUARDED) { | |
8209 | panic_plain("Invalid attempt to access guarded module-private " | |
8210 | "area: mbuf %p, pkt_flags 0x%x\n", m, pkt->pkt_flags); | |
8211 | /* NOTREACHED */ | |
8212 | } | |
8213 | ||
39236c6e A |
8214 | if (mcltrace) { |
8215 | mcache_audit_t *mca; | |
8216 | ||
8217 | lck_mtx_lock(mbuf_mlock); | |
8218 | mca = mcl_audit_buf2mca(MC_MBUF, (mcache_obj_t *)m); | |
0a7de745 | 8219 | if (mca->mca_uflags & MB_SCVALID) { |
39236c6e | 8220 | mcl_audit_scratch(mca); |
0a7de745 | 8221 | } |
39236c6e A |
8222 | lck_mtx_unlock(mbuf_mlock); |
8223 | } | |
8224 | ||
fe8ab488 | 8225 | *p = (u_int8_t *)&pkt->pkt_mpriv; |
0a7de745 | 8226 | return sizeof(pkt->pkt_mpriv); |
39236c6e A |
8227 | } |
8228 | ||
8229 | static void | |
8230 | m_redzone_init(struct mbuf *m) | |
8231 | { | |
8232 | VERIFY(m->m_flags & M_PKTHDR); | |
8233 | /* | |
8234 | * Each mbuf has a unique red zone pattern, which is a XOR | |
8235 | * of the red zone cookie and the address of the mbuf. | |
8236 | */ | |
8237 | m->m_pkthdr.redzone = ((u_int32_t)(uintptr_t)m) ^ mb_redzone_cookie; | |
8238 | } | |
8239 | ||
8240 | static void | |
8241 | m_redzone_verify(struct mbuf *m) | |
8242 | { | |
8243 | u_int32_t mb_redzone; | |
8244 | ||
8245 | VERIFY(m->m_flags & M_PKTHDR); | |
8246 | ||
8247 | mb_redzone = ((u_int32_t)(uintptr_t)m) ^ mb_redzone_cookie; | |
8248 | if (m->m_pkthdr.redzone != mb_redzone) { | |
8249 | panic("mbuf %p redzone violation with value 0x%x " | |
8250 | "(instead of 0x%x, using cookie 0x%x)\n", | |
8251 | m, m->m_pkthdr.redzone, mb_redzone, mb_redzone_cookie); | |
8252 | /* NOTREACHED */ | |
8253 | } | |
8254 | } | |
8255 | ||
813fb2f6 A |
8256 | __private_extern__ inline void |
8257 | m_set_ext(struct mbuf *m, struct ext_ref *rfa, m_ext_free_func_t ext_free, | |
8258 | caddr_t ext_arg) | |
8259 | { | |
8260 | VERIFY(m->m_flags & M_EXT); | |
8261 | if (rfa != NULL) { | |
8262 | m->m_ext.ext_refflags = | |
8263 | (struct ext_ref *)(((uintptr_t)rfa) ^ mb_obscure_extref); | |
8264 | if (ext_free != NULL) { | |
8265 | rfa->ext_token = ((uintptr_t)&rfa->ext_token) ^ | |
8266 | mb_obscure_extfree; | |
f427ee49 A |
8267 | uintptr_t ext_free_val = ptrauth_nop_cast(uintptr_t, ext_free) ^ rfa->ext_token; |
8268 | m->m_ext.ext_free = ptrauth_nop_cast(m_ext_free_func_t, ext_free_val); | |
813fb2f6 | 8269 | if (ext_arg != NULL) { |
5ba3f43e A |
8270 | m->m_ext.ext_arg = |
8271 | (caddr_t)(((uintptr_t)ext_arg) ^ rfa->ext_token); | |
813fb2f6 A |
8272 | } else { |
8273 | m->m_ext.ext_arg = NULL; | |
8274 | } | |
8275 | } else { | |
8276 | rfa->ext_token = 0; | |
8277 | m->m_ext.ext_free = NULL; | |
8278 | m->m_ext.ext_arg = NULL; | |
8279 | } | |
8280 | } else { | |
8281 | /* | |
8282 | * If we are going to loose the cookie in ext_token by | |
8283 | * resetting the rfa, we should use the global cookie | |
8284 | * to obscure the ext_free and ext_arg pointers. | |
8285 | */ | |
8286 | if (ext_free != NULL) { | |
f427ee49 A |
8287 | uintptr_t ext_free_val = ptrauth_nop_cast(uintptr_t, ext_free) ^ mb_obscure_extfree; |
8288 | m->m_ext.ext_free = ptrauth_nop_cast(m_ext_free_func_t, ext_free_val); | |
813fb2f6 | 8289 | if (ext_arg != NULL) { |
5ba3f43e A |
8290 | m->m_ext.ext_arg = |
8291 | (caddr_t)((uintptr_t)ext_arg ^ | |
813fb2f6 A |
8292 | mb_obscure_extfree); |
8293 | } else { | |
8294 | m->m_ext.ext_arg = NULL; | |
8295 | } | |
8296 | } else { | |
8297 | m->m_ext.ext_free = NULL; | |
8298 | m->m_ext.ext_arg = NULL; | |
8299 | } | |
8300 | m->m_ext.ext_refflags = NULL; | |
8301 | } | |
8302 | } | |
8303 | ||
8304 | __private_extern__ inline struct ext_ref * | |
8305 | m_get_rfa(struct mbuf *m) | |
8306 | { | |
0a7de745 A |
8307 | if (m->m_ext.ext_refflags == NULL) { |
8308 | return NULL; | |
8309 | } else { | |
8310 | return (struct ext_ref *)(((uintptr_t)m->m_ext.ext_refflags) ^ mb_obscure_extref); | |
8311 | } | |
813fb2f6 A |
8312 | } |
8313 | ||
8314 | __private_extern__ inline m_ext_free_func_t | |
8315 | m_get_ext_free(struct mbuf *m) | |
8316 | { | |
8317 | struct ext_ref *rfa; | |
0a7de745 A |
8318 | if (m->m_ext.ext_free == NULL) { |
8319 | return NULL; | |
8320 | } | |
813fb2f6 A |
8321 | |
8322 | rfa = m_get_rfa(m); | |
0a7de745 | 8323 | if (rfa == NULL) { |
f427ee49 A |
8324 | uintptr_t ext_free_val = ptrauth_nop_cast(uintptr_t, m->m_ext.ext_free) ^ mb_obscure_extfree; |
8325 | return ptrauth_nop_cast(m_ext_free_func_t, ext_free_val); | |
0a7de745 | 8326 | } else { |
f427ee49 A |
8327 | uintptr_t ext_free_val = ptrauth_nop_cast(uintptr_t, m->m_ext.ext_free) ^ rfa->ext_token; |
8328 | return ptrauth_nop_cast(m_ext_free_func_t, ext_free_val); | |
0a7de745 | 8329 | } |
813fb2f6 A |
8330 | } |
8331 | ||
8332 | __private_extern__ inline caddr_t | |
8333 | m_get_ext_arg(struct mbuf *m) | |
8334 | { | |
8335 | struct ext_ref *rfa; | |
0a7de745 A |
8336 | if (m->m_ext.ext_arg == NULL) { |
8337 | return NULL; | |
8338 | } | |
813fb2f6 A |
8339 | |
8340 | rfa = m_get_rfa(m); | |
8341 | if (rfa == NULL) { | |
0a7de745 | 8342 | return (caddr_t)((uintptr_t)m->m_ext.ext_arg ^ mb_obscure_extfree); |
813fb2f6 | 8343 | } else { |
0a7de745 A |
8344 | return (caddr_t)(((uintptr_t)m->m_ext.ext_arg) ^ |
8345 | rfa->ext_token); | |
813fb2f6 A |
8346 | } |
8347 | } | |
8348 | ||
fe8ab488 A |
8349 | /* |
8350 | * Send a report of mbuf usage if the usage is at least 6% of max limit | |
8351 | * or if there has been at least 3% increase since the last report. | |
8352 | * | |
8353 | * The values 6% and 3% are chosen so that we can do simple arithmetic | |
8354 | * with shift operations. | |
39037602 | 8355 | */ |
fe8ab488 A |
8356 | static boolean_t |
8357 | mbuf_report_usage(mbuf_class_t cl) | |
8358 | { | |
8359 | /* if a report is already in progress, nothing to do */ | |
0a7de745 A |
8360 | if (mb_peak_newreport) { |
8361 | return TRUE; | |
8362 | } | |
fe8ab488 A |
8363 | |
8364 | if (m_total(cl) > m_peak(cl) && | |
8365 | m_total(cl) >= (m_maxlimit(cl) >> 4) && | |
0a7de745 A |
8366 | (m_total(cl) - m_peak(cl)) >= (m_peak(cl) >> 5)) { |
8367 | return TRUE; | |
8368 | } | |
8369 | return FALSE; | |
fe8ab488 A |
8370 | } |
8371 | ||
8372 | __private_extern__ void | |
8373 | mbuf_report_peak_usage(void) | |
8374 | { | |
39037602 | 8375 | int i = 0; |
fe8ab488 A |
8376 | u_int64_t uptime; |
8377 | struct nstat_sysinfo_data ns_data; | |
8378 | uint32_t memreleased = 0; | |
cc8bc92a | 8379 | static uint32_t prevmemreleased; |
fe8ab488 A |
8380 | |
8381 | uptime = net_uptime(); | |
8382 | lck_mtx_lock(mbuf_mlock); | |
8383 | ||
8384 | /* Generate an initial report after 1 week of uptime */ | |
39037602 | 8385 | if (!mb_peak_firstreport && |
fe8ab488 A |
8386 | uptime > MBUF_PEAK_FIRST_REPORT_THRESHOLD) { |
8387 | mb_peak_newreport = TRUE; | |
8388 | mb_peak_firstreport = TRUE; | |
8389 | } | |
8390 | ||
8391 | if (!mb_peak_newreport) { | |
8392 | lck_mtx_unlock(mbuf_mlock); | |
8393 | return; | |
8394 | } | |
8395 | ||
8396 | /* | |
39037602 | 8397 | * Since a report is being generated before 1 week, |
fe8ab488 A |
8398 | * we do not need to force another one later |
8399 | */ | |
0a7de745 | 8400 | if (uptime < MBUF_PEAK_FIRST_REPORT_THRESHOLD) { |
fe8ab488 | 8401 | mb_peak_firstreport = TRUE; |
0a7de745 | 8402 | } |
fe8ab488 A |
8403 | |
8404 | for (i = 0; i < NELEM(mbuf_table); i++) { | |
8405 | m_peak(m_class(i)) = m_total(m_class(i)); | |
8406 | memreleased += m_release_cnt(i); | |
8407 | } | |
cc8bc92a A |
8408 | memreleased = memreleased - prevmemreleased; |
8409 | prevmemreleased = memreleased; | |
fe8ab488 A |
8410 | mb_peak_newreport = FALSE; |
8411 | lck_mtx_unlock(mbuf_mlock); | |
8412 | ||
8413 | bzero(&ns_data, sizeof(ns_data)); | |
8414 | ns_data.flags = NSTAT_SYSINFO_MBUF_STATS; | |
8415 | ns_data.u.mb_stats.total_256b = m_peak(MC_MBUF); | |
8416 | ns_data.u.mb_stats.total_2kb = m_peak(MC_CL); | |
8417 | ns_data.u.mb_stats.total_4kb = m_peak(MC_BIGCL); | |
3e170ce0 | 8418 | ns_data.u.mb_stats.total_16kb = m_peak(MC_16KCL); |
fe8ab488 A |
8419 | ns_data.u.mb_stats.sbmb_total = total_sbmb_cnt_peak; |
8420 | ns_data.u.mb_stats.sb_atmbuflimit = sbmb_limreached; | |
8421 | ns_data.u.mb_stats.draincnt = mbstat.m_drain; | |
8422 | ns_data.u.mb_stats.memreleased = memreleased; | |
39037602 | 8423 | ns_data.u.mb_stats.sbmb_floor = total_sbmb_cnt_floor; |
fe8ab488 A |
8424 | |
8425 | nstat_sysinfo_send_data(&ns_data); | |
39037602 A |
8426 | |
8427 | /* | |
8428 | * Reset the floor whenever we report a new | |
8429 | * peak to track the trend (increase peek usage | |
8430 | * is not a leak if mbufs get released | |
8431 | * between reports and the floor stays low) | |
8432 | */ | |
8433 | total_sbmb_cnt_floor = total_sbmb_cnt_peak; | |
fe8ab488 A |
8434 | } |
8435 | ||
8436 | /* | |
d9a64523 A |
8437 | * Simple routine to avoid taking the lock when we can't run the |
8438 | * mbuf drain. | |
fe8ab488 | 8439 | */ |
d9a64523 A |
8440 | static int |
8441 | mbuf_drain_checks(boolean_t ignore_waiters) | |
8442 | { | |
0a7de745 | 8443 | if (mb_drain_maxint == 0) { |
d9a64523 | 8444 | return 0; |
0a7de745 A |
8445 | } |
8446 | if (!ignore_waiters && mb_waiters != 0) { | |
d9a64523 | 8447 | return 0; |
0a7de745 | 8448 | } |
d9a64523 A |
8449 | |
8450 | return 1; | |
8451 | } | |
8452 | ||
8453 | /* | |
8454 | * Called by the VM when there's memory pressure or when we exhausted | |
8455 | * the 4k/16k reserved space. | |
8456 | */ | |
8457 | static void | |
8458 | mbuf_drain_locked(boolean_t ignore_waiters) | |
fe8ab488 A |
8459 | { |
8460 | mbuf_class_t mc; | |
8461 | mcl_slab_t *sp, *sp_tmp, *nsp; | |
8462 | unsigned int num, k, interval, released = 0; | |
39037602 | 8463 | unsigned long total_mem = 0, use_mem = 0; |
fe8ab488 A |
8464 | boolean_t ret, purge_caches = FALSE; |
8465 | ppnum_t offset; | |
8466 | mcache_obj_t *obj; | |
39037602 | 8467 | unsigned long per; |
fe8ab488 A |
8468 | static unsigned char scratch[32]; |
8469 | static ppnum_t scratch_pa = 0; | |
8470 | ||
d9a64523 | 8471 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); |
0a7de745 | 8472 | if (!mbuf_drain_checks(ignore_waiters)) { |
fe8ab488 | 8473 | return; |
0a7de745 | 8474 | } |
fe8ab488 A |
8475 | if (scratch_pa == 0) { |
8476 | bzero(scratch, sizeof(scratch)); | |
8477 | scratch_pa = pmap_find_phys(kernel_pmap, (addr64_t)scratch); | |
8478 | VERIFY(scratch_pa); | |
8479 | } else if (mclverify) { | |
8480 | /* | |
8481 | * Panic if a driver wrote to our scratch memory. | |
8482 | */ | |
0a7de745 A |
8483 | for (k = 0; k < sizeof(scratch); k++) { |
8484 | if (scratch[k]) { | |
fe8ab488 | 8485 | panic("suspect DMA to freed address"); |
0a7de745 A |
8486 | } |
8487 | } | |
fe8ab488 A |
8488 | } |
8489 | /* | |
8490 | * Don't free memory too often as that could cause excessive | |
8491 | * waiting times for mbufs. Purge caches if we were asked to drain | |
8492 | * in the last 5 minutes. | |
8493 | */ | |
d9a64523 A |
8494 | if (mbuf_drain_last_runtime != 0) { |
8495 | interval = net_uptime() - mbuf_drain_last_runtime; | |
8496 | if (interval <= mb_drain_maxint) { | |
8497 | return; | |
8498 | } | |
0a7de745 | 8499 | if (interval <= mb_drain_maxint * 5) { |
d9a64523 | 8500 | purge_caches = TRUE; |
0a7de745 | 8501 | } |
39037602 | 8502 | } |
d9a64523 | 8503 | mbuf_drain_last_runtime = net_uptime(); |
fe8ab488 A |
8504 | /* |
8505 | * Don't free any memory if we're using 60% or more. | |
8506 | */ | |
8507 | for (mc = 0; mc < NELEM(mbuf_table); mc++) { | |
8508 | total_mem += m_total(mc) * m_maxsize(mc); | |
8509 | use_mem += m_active(mc) * m_maxsize(mc); | |
8510 | } | |
39037602 A |
8511 | per = (use_mem * 100) / total_mem; |
8512 | if (per >= 60) { | |
fe8ab488 A |
8513 | return; |
8514 | } | |
8515 | /* | |
8516 | * Purge all the caches. This effectively disables | |
8517 | * caching for a few seconds, but the mbuf worker thread will | |
8518 | * re-enable them again. | |
8519 | */ | |
0a7de745 | 8520 | if (purge_caches == TRUE) { |
fe8ab488 | 8521 | for (mc = 0; mc < NELEM(mbuf_table); mc++) { |
0a7de745 | 8522 | if (m_total(mc) < m_avgtotal(mc)) { |
fe8ab488 | 8523 | continue; |
0a7de745 | 8524 | } |
fe8ab488 A |
8525 | lck_mtx_unlock(mbuf_mlock); |
8526 | ret = mcache_purge_cache(m_cache(mc), FALSE); | |
8527 | lck_mtx_lock(mbuf_mlock); | |
0a7de745 | 8528 | if (ret == TRUE) { |
fe8ab488 | 8529 | m_purge_cnt(mc)++; |
0a7de745 | 8530 | } |
fe8ab488 | 8531 | } |
0a7de745 | 8532 | } |
fe8ab488 A |
8533 | /* |
8534 | * Move the objects from the composite class freelist to | |
8535 | * the rudimentary slabs list, but keep at least 10% of the average | |
8536 | * total in the freelist. | |
8537 | */ | |
8538 | for (mc = 0; mc < NELEM(mbuf_table); mc++) { | |
39037602 | 8539 | while (m_cobjlist(mc) && |
fe8ab488 A |
8540 | m_total(mc) < m_avgtotal(mc) && |
8541 | m_infree(mc) > 0.1 * m_avgtotal(mc) + m_minlimit(mc)) { | |
8542 | obj = m_cobjlist(mc); | |
8543 | m_cobjlist(mc) = obj->obj_next; | |
8544 | obj->obj_next = NULL; | |
8545 | num = cslab_free(mc, obj, 1); | |
8546 | VERIFY(num == 1); | |
8547 | m_free_cnt(mc)++; | |
8548 | m_infree(mc)--; | |
8549 | /* cslab_free() handles m_total */ | |
8550 | } | |
8551 | } | |
8552 | /* | |
8553 | * Free the buffers present in the slab list up to 10% of the total | |
8554 | * average per class. | |
8555 | * | |
8556 | * We walk the list backwards in an attempt to reduce fragmentation. | |
8557 | */ | |
8558 | for (mc = NELEM(mbuf_table) - 1; (int)mc >= 0; mc--) { | |
8559 | TAILQ_FOREACH_SAFE(sp, &m_slablist(mc), sl_link, sp_tmp) { | |
8560 | /* | |
8561 | * Process only unused slabs occupying memory. | |
8562 | */ | |
8563 | if (sp->sl_refcnt != 0 || sp->sl_len == 0 || | |
0a7de745 | 8564 | sp->sl_base == NULL) { |
fe8ab488 | 8565 | continue; |
0a7de745 | 8566 | } |
fe8ab488 | 8567 | if (m_total(mc) < m_avgtotal(mc) || |
0a7de745 | 8568 | m_infree(mc) < 0.1 * m_avgtotal(mc) + m_minlimit(mc)) { |
fe8ab488 | 8569 | break; |
0a7de745 | 8570 | } |
fe8ab488 A |
8571 | slab_remove(sp, mc); |
8572 | switch (mc) { | |
8573 | case MC_MBUF: | |
3e170ce0 A |
8574 | m_infree(mc) -= NMBPG; |
8575 | m_total(mc) -= NMBPG; | |
0a7de745 | 8576 | if (mclaudit != NULL) { |
3e170ce0 | 8577 | mcl_audit_free(sp->sl_base, NMBPG); |
0a7de745 | 8578 | } |
fe8ab488 A |
8579 | break; |
8580 | case MC_CL: | |
3e170ce0 A |
8581 | m_infree(mc) -= NCLPG; |
8582 | m_total(mc) -= NCLPG; | |
0a7de745 | 8583 | if (mclaudit != NULL) { |
3e170ce0 | 8584 | mcl_audit_free(sp->sl_base, NMBPG); |
0a7de745 | 8585 | } |
fe8ab488 A |
8586 | break; |
8587 | case MC_BIGCL: | |
3e170ce0 A |
8588 | { |
8589 | m_infree(mc) -= NBCLPG; | |
8590 | m_total(mc) -= NBCLPG; | |
0a7de745 | 8591 | if (mclaudit != NULL) { |
3e170ce0 | 8592 | mcl_audit_free(sp->sl_base, NMBPG); |
0a7de745 | 8593 | } |
fe8ab488 | 8594 | break; |
3e170ce0 | 8595 | } |
fe8ab488 A |
8596 | case MC_16KCL: |
8597 | m_infree(mc)--; | |
8598 | m_total(mc)--; | |
8599 | for (nsp = sp, k = 1; k < NSLABSP16KB; k++) { | |
8600 | nsp = nsp->sl_next; | |
39037602 | 8601 | VERIFY(nsp->sl_refcnt == 0 && |
fe8ab488 A |
8602 | nsp->sl_base != NULL && |
8603 | nsp->sl_len == 0); | |
8604 | slab_init(nsp, 0, 0, NULL, NULL, 0, 0, | |
8605 | 0); | |
8606 | nsp->sl_flags = 0; | |
8607 | } | |
5ba3f43e A |
8608 | if (mclaudit != NULL) { |
8609 | if (sp->sl_len == PAGE_SIZE) { | |
8610 | mcl_audit_free(sp->sl_base, | |
8611 | NMBPG); | |
8612 | } else { | |
8613 | mcl_audit_free(sp->sl_base, 1); | |
8614 | } | |
8615 | } | |
fe8ab488 A |
8616 | break; |
8617 | default: | |
8618 | /* | |
8619 | * The composite classes have their own | |
8620 | * freelist (m_cobjlist), so we only | |
8621 | * process rudimentary classes here. | |
8622 | */ | |
8623 | VERIFY(0); | |
8624 | } | |
8625 | m_release_cnt(mc) += m_size(mc); | |
8626 | released += m_size(mc); | |
3e170ce0 A |
8627 | VERIFY(sp->sl_base != NULL && |
8628 | sp->sl_len >= PAGE_SIZE); | |
8629 | offset = MTOPG(sp->sl_base); | |
fe8ab488 A |
8630 | /* |
8631 | * Make sure the IOMapper points to a valid, but | |
8632 | * bogus, address. This should prevent further DMA | |
8633 | * accesses to freed memory. | |
8634 | */ | |
8635 | IOMapperInsertPage(mcl_paddr_base, offset, scratch_pa); | |
8636 | mcl_paddr[offset] = 0; | |
39037602 | 8637 | kmem_free(mb_map, (vm_offset_t)sp->sl_base, |
fe8ab488 A |
8638 | sp->sl_len); |
8639 | slab_init(sp, 0, 0, NULL, NULL, 0, 0, 0); | |
8640 | sp->sl_flags = 0; | |
8641 | } | |
8642 | } | |
8643 | mbstat.m_drain++; | |
8644 | mbstat.m_bigclusters = m_total(MC_BIGCL); | |
8645 | mbstat.m_clusters = m_total(MC_CL); | |
8646 | mbstat.m_mbufs = m_total(MC_MBUF); | |
8647 | mbuf_stat_sync(); | |
8648 | mbuf_mtypes_sync(TRUE); | |
d9a64523 A |
8649 | } |
8650 | ||
8651 | __private_extern__ void | |
8652 | mbuf_drain(boolean_t ignore_waiters) | |
8653 | { | |
8654 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_NOTOWNED); | |
0a7de745 | 8655 | if (!mbuf_drain_checks(ignore_waiters)) { |
d9a64523 | 8656 | return; |
0a7de745 | 8657 | } |
d9a64523 A |
8658 | lck_mtx_lock(mbuf_mlock); |
8659 | mbuf_drain_locked(ignore_waiters); | |
fe8ab488 A |
8660 | lck_mtx_unlock(mbuf_mlock); |
8661 | } | |
8662 | ||
d9a64523 | 8663 | |
fe8ab488 A |
8664 | static int |
8665 | m_drain_force_sysctl SYSCTL_HANDLER_ARGS | |
8666 | { | |
8667 | #pragma unused(arg1, arg2) | |
8668 | int val = 0, err; | |
39037602 | 8669 | |
fe8ab488 | 8670 | err = sysctl_handle_int(oidp, &val, 0, req); |
0a7de745 A |
8671 | if (err != 0 || req->newptr == USER_ADDR_NULL) { |
8672 | return err; | |
8673 | } | |
5c9f4661 | 8674 | if (val) { |
d9a64523 | 8675 | mbuf_drain(TRUE); |
5c9f4661 | 8676 | } |
fe8ab488 | 8677 | |
0a7de745 | 8678 | return err; |
fe8ab488 A |
8679 | } |
8680 | ||
cc8bc92a | 8681 | #if DEBUG || DEVELOPMENT |
d9a64523 A |
8682 | static void |
8683 | _mbwdog_logger(const char *func, const int line, const char *fmt, ...) | |
8684 | { | |
8685 | va_list ap; | |
8686 | struct timeval now; | |
8687 | char str[384], p[256]; | |
8688 | int len; | |
8689 | ||
8690 | LCK_MTX_ASSERT(mbuf_mlock, LCK_MTX_ASSERT_OWNED); | |
8691 | if (mbwdog_logging == NULL) { | |
8692 | mbwdog_logging = _MALLOC(mbwdog_logging_size, | |
0a7de745 A |
8693 | M_TEMP, M_ZERO | M_NOWAIT); |
8694 | if (mbwdog_logging == NULL) { | |
d9a64523 | 8695 | return; |
0a7de745 | 8696 | } |
d9a64523 A |
8697 | } |
8698 | va_start(ap, fmt); | |
8699 | vsnprintf(p, sizeof(p), fmt, ap); | |
8700 | va_end(ap); | |
8701 | microuptime(&now); | |
4ba76501 | 8702 | len = scnprintf(str, sizeof(str), |
d9a64523 A |
8703 | "\n%ld.%d (%d/%llx) %s:%d %s", |
8704 | now.tv_sec, now.tv_usec, | |
8705 | current_proc()->p_pid, | |
8706 | (uint64_t)VM_KERNEL_ADDRPERM(current_thread()), | |
8707 | func, line, p); | |
0a7de745 | 8708 | if (len < 0) { |
d9a64523 | 8709 | return; |
0a7de745 | 8710 | } |
d9a64523 A |
8711 | if (mbwdog_logging_used + len > mbwdog_logging_size) { |
8712 | mbwdog_logging_used = mbwdog_logging_used / 2; | |
8713 | memmove(mbwdog_logging, mbwdog_logging + mbwdog_logging_used, | |
8714 | mbwdog_logging_size - mbwdog_logging_used); | |
8715 | mbwdog_logging[mbwdog_logging_used] = 0; | |
8716 | } | |
8717 | strlcat(mbwdog_logging, str, mbwdog_logging_size); | |
8718 | mbwdog_logging_used += len; | |
8719 | } | |
8720 | ||
8721 | static int | |
8722 | sysctl_mbwdog_log SYSCTL_HANDLER_ARGS | |
8723 | { | |
8724 | #pragma unused(oidp, arg1, arg2) | |
0a7de745 | 8725 | return SYSCTL_OUT(req, mbwdog_logging, mbwdog_logging_used); |
d9a64523 A |
8726 | } |
8727 | SYSCTL_DECL(_kern_ipc); | |
8728 | SYSCTL_PROC(_kern_ipc, OID_AUTO, mbwdog_log, | |
8729 | CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_LOCKED, | |
8730 | 0, 0, sysctl_mbwdog_log, "A", ""); | |
cc8bc92a A |
8731 | |
8732 | static int mbtest_val; | |
8733 | static int mbtest_running; | |
8734 | ||
0a7de745 A |
8735 | static void |
8736 | mbtest_thread(__unused void *arg) | |
cc8bc92a A |
8737 | { |
8738 | int i; | |
a39ff7e2 A |
8739 | int scale_down = 1; |
8740 | int iterations = 250; | |
8741 | int allocations = nmbclusters; | |
8742 | iterations = iterations / scale_down; | |
8743 | allocations = allocations / scale_down; | |
cc8bc92a | 8744 | printf("%s thread starting\n", __func__); |
a39ff7e2 A |
8745 | for (i = 0; i < iterations; i++) { |
8746 | unsigned int needed = allocations; | |
cc8bc92a A |
8747 | struct mbuf *m1, *m2, *m3; |
8748 | ||
8749 | if (njcl > 0) { | |
a39ff7e2 | 8750 | needed = allocations; |
cc8bc92a A |
8751 | m3 = m_getpackets_internal(&needed, 0, M_DONTWAIT, 0, M16KCLBYTES); |
8752 | m_freem_list(m3); | |
8753 | } | |
8754 | ||
a39ff7e2 | 8755 | needed = allocations; |
cc8bc92a A |
8756 | m2 = m_getpackets_internal(&needed, 0, M_DONTWAIT, 0, MBIGCLBYTES); |
8757 | m_freem_list(m2); | |
8758 | ||
8759 | m1 = m_getpackets_internal(&needed, 0, M_DONTWAIT, 0, MCLBYTES); | |
8760 | m_freem_list(m1); | |
8761 | } | |
8762 | ||
8763 | printf("%s thread ending\n", __func__); | |
8764 | ||
8765 | OSDecrementAtomic(&mbtest_running); | |
8766 | wakeup_one((caddr_t)&mbtest_running); | |
8767 | } | |
8768 | ||
0a7de745 A |
8769 | static void |
8770 | sysctl_mbtest(void) | |
cc8bc92a A |
8771 | { |
8772 | /* We launch three threads - wait for all of them */ | |
8773 | OSIncrementAtomic(&mbtest_running); | |
8774 | OSIncrementAtomic(&mbtest_running); | |
8775 | OSIncrementAtomic(&mbtest_running); | |
8776 | ||
8777 | thread_call_func_delayed((thread_call_func_t)mbtest_thread, NULL, 10); | |
8778 | thread_call_func_delayed((thread_call_func_t)mbtest_thread, NULL, 10); | |
8779 | thread_call_func_delayed((thread_call_func_t)mbtest_thread, NULL, 10); | |
8780 | ||
8781 | while (mbtest_running) { | |
8782 | msleep((caddr_t)&mbtest_running, NULL, PUSER, "mbtest_running", NULL); | |
8783 | } | |
8784 | } | |
8785 | ||
8786 | static int | |
8787 | mbtest SYSCTL_HANDLER_ARGS | |
8788 | { | |
8789 | #pragma unused(arg1, arg2) | |
8790 | int error = 0, val, oldval = mbtest_val; | |
8791 | ||
8792 | val = oldval; | |
8793 | error = sysctl_handle_int(oidp, &val, 0, req); | |
0a7de745 A |
8794 | if (error || !req->newptr) { |
8795 | return error; | |
8796 | } | |
cc8bc92a | 8797 | |
0a7de745 | 8798 | if (val != oldval) { |
cc8bc92a | 8799 | sysctl_mbtest(); |
0a7de745 | 8800 | } |
cc8bc92a A |
8801 | |
8802 | mbtest_val = val; | |
8803 | ||
0a7de745 | 8804 | return error; |
cc8bc92a | 8805 | } |
d9a64523 | 8806 | #endif // DEBUG || DEVELOPMENT |
5c9f4661 A |
8807 | |
8808 | static void | |
8809 | mtracelarge_register(size_t size) | |
8810 | { | |
8811 | int i; | |
8812 | struct mtracelarge *trace; | |
8813 | uintptr_t bt[MLEAK_STACK_DEPTH]; | |
8814 | unsigned int depth; | |
8815 | ||
cb323159 | 8816 | depth = backtrace(bt, MLEAK_STACK_DEPTH, NULL); |
5c9f4661 A |
8817 | /* Check if this entry is already on the list. */ |
8818 | for (i = 0; i < MTRACELARGE_NUM_TRACES; i++) { | |
8819 | trace = &mtracelarge_table[i]; | |
8820 | if (trace->size == size && trace->depth == depth && | |
8821 | memcmp(bt, trace->addr, depth * sizeof(uintptr_t)) == 0) { | |
8822 | return; | |
8823 | } | |
5c9f4661 A |
8824 | } |
8825 | for (i = 0; i < MTRACELARGE_NUM_TRACES; i++) { | |
8826 | trace = &mtracelarge_table[i]; | |
8827 | if (size > trace->size) { | |
8828 | trace->depth = depth; | |
8829 | memcpy(trace->addr, bt, depth * sizeof(uintptr_t)); | |
8830 | trace->size = size; | |
8831 | break; | |
8832 | } | |
8833 | } | |
8834 | } | |
8835 | ||
2d21ac55 | 8836 | SYSCTL_DECL(_kern_ipc); |
cc8bc92a A |
8837 | #if DEBUG || DEVELOPMENT |
8838 | SYSCTL_PROC(_kern_ipc, OID_AUTO, mbtest, | |
8839 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &mbtest_val, 0, &mbtest, "I", | |
8840 | "Toggle to test mbufs"); | |
8841 | #endif | |
6d2010ae | 8842 | SYSCTL_PROC(_kern_ipc, KIPC_MBSTAT, mbstat, |
fe8ab488 | 8843 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
2d21ac55 | 8844 | 0, 0, mbstat_sysctl, "S,mbstat", ""); |
6d2010ae | 8845 | SYSCTL_PROC(_kern_ipc, OID_AUTO, mb_stat, |
fe8ab488 | 8846 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
2d21ac55 | 8847 | 0, 0, mb_stat_sysctl, "S,mb_stat", ""); |
6d2010ae | 8848 | SYSCTL_PROC(_kern_ipc, OID_AUTO, mleak_top_trace, |
fe8ab488 | 8849 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
6d2010ae A |
8850 | 0, 0, mleak_top_trace_sysctl, "S,mb_top_trace", ""); |
8851 | SYSCTL_PROC(_kern_ipc, OID_AUTO, mleak_table, | |
fe8ab488 | 8852 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
6d2010ae A |
8853 | 0, 0, mleak_table_sysctl, "S,mleak_table", ""); |
8854 | SYSCTL_INT(_kern_ipc, OID_AUTO, mleak_sample_factor, | |
8855 | CTLFLAG_RW | CTLFLAG_LOCKED, &mleak_table.mleak_sample_factor, 0, ""); | |
8856 | SYSCTL_INT(_kern_ipc, OID_AUTO, mb_normalized, | |
8857 | CTLFLAG_RD | CTLFLAG_LOCKED, &mb_normalized, 0, ""); | |
8858 | SYSCTL_INT(_kern_ipc, OID_AUTO, mb_watchdog, | |
8859 | CTLFLAG_RW | CTLFLAG_LOCKED, &mb_watchdog, 0, ""); | |
fe8ab488 | 8860 | SYSCTL_PROC(_kern_ipc, OID_AUTO, mb_drain_force, |
39037602 | 8861 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, NULL, 0, |
fe8ab488 A |
8862 | m_drain_force_sysctl, "I", |
8863 | "Forces the mbuf garbage collection to run"); | |
8864 | SYSCTL_INT(_kern_ipc, OID_AUTO, mb_drain_maxint, | |
8865 | CTLFLAG_RW | CTLFLAG_LOCKED, &mb_drain_maxint, 0, | |
8866 | "Minimum time interval between garbage collection"); | |
2a1bd2d3 A |
8867 | SYSCTL_INT(_kern_ipc, OID_AUTO, mb_memory_pressure_percentage, |
8868 | CTLFLAG_RW | CTLFLAG_LOCKED, &mb_memory_pressure_percentage, 0, | |
8869 | "Percentage of when we trigger memory-pressure for an mbuf-class"); |