2 * Copyright (c) 1998-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1988, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
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,
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/syslog.h>
77 #include <sys/protosw.h>
78 #include <sys/domain.h>
79 #include <sys/queue.h>
82 #include <dev/random/randomdev.h>
84 #include <kern/kern_types.h>
85 #include <kern/simple_lock.h>
86 #include <kern/queue.h>
87 #include <kern/sched_prim.h>
88 #include <kern/cpu_number.h>
89 #include <kern/zalloc.h>
91 #include <libkern/OSAtomic.h>
92 #include <libkern/OSDebug.h>
93 #include <libkern/libkern.h>
95 #include <IOKit/IOMapper.h>
97 #include <machine/limits.h>
98 #include <machine/machine_routines.h>
101 #include <security/mac_framework.h>
104 #include <sys/mcache.h>
107 * MBUF IMPLEMENTATION NOTES.
109 * There is a total of 5 per-CPU caches:
112 * This is a cache of rudimentary objects of MSIZE in size; each
113 * object represents an mbuf structure. This cache preserves only
114 * the m_type field of the mbuf during its transactions.
117 * This is a cache of rudimentary objects of MCLBYTES in size; each
118 * object represents a mcluster structure. This cache does not
119 * preserve the contents of the objects during its transactions.
122 * This is a cache of rudimentary objects of MBIGCLBYTES in size; each
123 * object represents a mbigcluster structure. This cache does not
124 * preserve the contents of the objects during its transaction.
127 * This is a cache of mbufs each having a cluster attached to it.
128 * It is backed by MC_MBUF and MC_CL rudimentary caches. Several
129 * fields of the mbuf related to the external cluster are preserved
130 * during transactions.
133 * This is a cache of mbufs each having a big cluster attached to it.
134 * It is backed by MC_MBUF and MC_BIGCL rudimentary caches. Several
135 * fields of the mbuf related to the external cluster are preserved
136 * during transactions.
140 * Allocation requests are handled first at the per-CPU (mcache) layer
141 * before falling back to the slab layer. Performance is optimal when
142 * the request is satisfied at the CPU layer because global data/lock
143 * never gets accessed. When the slab layer is entered for allocation,
144 * the slab freelist will be checked first for available objects before
145 * the VM backing store is invoked. Slab layer operations are serialized
146 * for all of the caches as the mbuf global lock is held most of the time.
147 * Allocation paths are different depending on the class of objects:
149 * a. Rudimentary object:
151 * { m_get_common(), m_clattach(), m_mclget(),
152 * m_mclalloc(), m_bigalloc(), m_copym_with_hdrs(),
153 * composite object allocation }
156 * | +-----------------------+
158 * mcache_alloc/mcache_alloc_ext() mbuf_slab_audit()
161 * [CPU cache] -------> (found?) -------+
164 * mbuf_slab_alloc() |
167 * +---------> [freelist] -------> (found?) -------+
173 * +---<<---- kmem_mb_alloc()
175 * b. Composite object:
177 * { m_getpackets_internal(), m_allocpacket_internal() }
180 * | +------ (done) ---------+
182 * mcache_alloc/mcache_alloc_ext() mbuf_cslab_audit()
185 * [CPU cache] -------> (found?) -------+
188 * mbuf_cslab_alloc() |
191 * [freelist] -------> (found?) -------+
194 * (rudimentary object) |
195 * mcache_alloc/mcache_alloc_ext() ------>>-----+
197 * Auditing notes: If auditing is enabled, buffers will be subjected to
198 * integrity checks by the audit routine. This is done by verifying their
199 * contents against DEADBEEF (free) pattern before returning them to caller.
200 * As part of this step, the routine will also record the transaction and
201 * pattern-fill the buffers with BADDCAFE (uninitialized) pattern. It will
202 * also restore any constructed data structure fields if necessary.
204 * OBJECT DEALLOCATION:
206 * Freeing an object simply involves placing it into the CPU cache; this
207 * pollutes the cache to benefit subsequent allocations. The slab layer
208 * will only be entered if the object is to be purged out of the cache.
209 * During normal operations, this happens only when the CPU layer resizes
210 * its bucket while it's adjusting to the allocation load. Deallocation
211 * paths are different depending on the class of objects:
213 * a. Rudimentary object:
215 * { m_free(), m_freem_list(), composite object deallocation }
218 * | +------ (done) ---------+
220 * mcache_free/mcache_free_ext() |
223 * mbuf_slab_audit() |
226 * [CPU cache] ---> (not purging?) -----+
232 * [freelist] ----------->>------------+
233 * (objects never get purged to VM)
235 * b. Composite object:
237 * { m_free(), m_freem_list() }
240 * | +------ (done) ---------+
242 * mcache_free/mcache_free_ext() |
245 * mbuf_cslab_audit() |
248 * [CPU cache] ---> (not purging?) -----+
251 * mbuf_cslab_free() |
254 * [freelist] ---> (not purging?) -----+
257 * (rudimentary object) |
258 * mcache_free/mcache_free_ext() ------->>------+
260 * Auditing notes: If auditing is enabled, the audit routine will save
261 * any constructed data structure fields (if necessary) before filling the
262 * contents of the buffers with DEADBEEF (free) pattern and recording the
263 * transaction. Buffers that are freed (whether at CPU or slab layer) are
264 * expected to contain the free pattern.
268 * Debugging can be enabled by adding "mbuf_debug=0x3" to boot-args; this
269 * translates to the mcache flags (MCF_VERIFY | MCF_AUDIT). Additionally,
270 * the CPU layer cache can be disabled by setting the MCF_NOCPUCACHE flag,
271 * i.e. modify the boot argument parameter to "mbuf_debug=0x13". Leak
272 * detection may also be disabled by setting the MCF_NOLEAKLOG flag, e.g.
273 * "mbuf_debug=0x113". Note that debugging consumes more CPU and memory.
275 * Each object is associated with exactly one mcache_audit_t structure that
276 * contains the information related to its last buffer transaction. Given
277 * an address of an object, the audit structure can be retrieved by finding
278 * the position of the object relevant to the base address of the cluster:
280 * +------------+ +=============+
281 * | mbuf addr | | mclaudit[i] |
282 * +------------+ +=============+
284 * i = MTOBG(addr) +-------------+
285 * | +-----> | cl_audit[1] | -----> mcache_audit_t
286 * b = BGTOM(i) | +-------------+
288 * x = MCLIDX(b, addr) | +-------------+
289 * | | | cl_audit[7] |
290 * +-----------------+ +-------------+
293 * The mclaudit[] array is allocated at initialization time, but its contents
294 * get populated when the corresponding cluster is created. Because a page
295 * can be turned into NMBPBG number of mbufs, we preserve enough space for the
296 * mbufs so that there is a 1-to-1 mapping between them. A page that never
297 * gets (or has not yet) turned into mbufs will use only cl_audit[0] with the
298 * remaining entries unused. For 16KB cluster, only one entry from the first
299 * page is allocated and used for the entire object.
302 /* TODO: should be in header file */
303 /* kernel translater */
304 extern vm_offset_t
kmem_mb_alloc(vm_map_t
, int, int);
305 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
306 extern vm_map_t mb_map
; /* special map */
309 decl_lck_mtx_data(static, mbuf_mlock_data
);
310 static lck_mtx_t
*mbuf_mlock
= &mbuf_mlock_data
;
311 static lck_attr_t
*mbuf_mlock_attr
;
312 static lck_grp_t
*mbuf_mlock_grp
;
313 static lck_grp_attr_t
*mbuf_mlock_grp_attr
;
315 /* Back-end (common) layer */
316 static void *mbuf_worker_run
; /* wait channel for worker thread */
317 static int mbuf_worker_ready
; /* worker thread is runnable */
318 static int mbuf_expand_mcl
; /* number of cluster creation requets */
319 static int mbuf_expand_big
; /* number of big cluster creation requests */
320 static int mbuf_expand_16k
; /* number of 16KB cluster creation requests */
321 static int ncpu
; /* number of CPUs */
322 static ppnum_t
*mcl_paddr
; /* Array of cluster physical addresses */
323 static ppnum_t mcl_pages
; /* Size of array (# physical pages) */
324 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
325 static mcache_t
*ref_cache
; /* Cache of cluster reference & flags */
326 static mcache_t
*mcl_audit_con_cache
; /* Audit contents cache */
327 static unsigned int mbuf_debug
; /* patchable mbuf mcache flags */
328 static unsigned int mb_normalized
; /* number of packets "normalized" */
330 #define MB_GROWTH_AGGRESSIVE 1 /* Threshold: 1/2 of total */
331 #define MB_GROWTH_NORMAL 2 /* Threshold: 3/4 of total */
334 MC_MBUF
= 0, /* Regular mbuf */
336 MC_BIGCL
, /* Large (4KB) cluster */
337 MC_16KCL
, /* Jumbo (16KB) cluster */
338 MC_MBUF_CL
, /* mbuf + cluster */
339 MC_MBUF_BIGCL
, /* mbuf + large (4KB) cluster */
340 MC_MBUF_16KCL
/* mbuf + jumbo (16KB) cluster */
343 #define MBUF_CLASS_MIN MC_MBUF
344 #define MBUF_CLASS_MAX MC_MBUF_16KCL
345 #define MBUF_CLASS_LAST MC_16KCL
346 #define MBUF_CLASS_VALID(c) \
347 ((int)(c) >= MBUF_CLASS_MIN && (int)(c) <= MBUF_CLASS_MAX)
348 #define MBUF_CLASS_COMPOSITE(c) \
349 ((int)(c) > MBUF_CLASS_LAST)
353 * mbuf specific mcache allocation request flags.
355 #define MCR_COMP MCR_USR1 /* for MC_MBUF_{CL,BIGCL,16KCL} caches */
358 * Per-cluster slab structure.
360 * A slab is a cluster control structure that contains one or more object
361 * chunks; the available chunks are chained in the slab's freelist (sl_head).
362 * Each time a chunk is taken out of the slab, the slab's reference count
363 * gets incremented. When all chunks have been taken out, the empty slab
364 * gets removed (SLF_DETACHED) from the class's slab list. A chunk that is
365 * returned to a slab causes the slab's reference count to be decremented;
366 * it also causes the slab to be reinserted back to class's slab list, if
367 * it's not already done.
369 * Compartmentalizing of the object chunks into slabs allows us to easily
370 * merge one or more slabs together when the adjacent slabs are idle, as
371 * well as to convert or move a slab from one class to another; e.g. the
372 * mbuf cluster slab can be converted to a regular cluster slab when all
373 * mbufs in the slab have been freed.
375 * A slab may also span across multiple clusters for chunks larger than
376 * a cluster's size. In this case, only the slab of the first cluster is
377 * used. The rest of the slabs are marked with SLF_PARTIAL to indicate
378 * that they are part of the larger slab.
380 * Each slab controls a page of memory.
382 typedef struct mcl_slab
{
383 struct mcl_slab
*sl_next
; /* neighboring slab */
384 u_int8_t sl_class
; /* controlling mbuf class */
385 int8_t sl_refcnt
; /* outstanding allocations */
386 int8_t sl_chunks
; /* chunks (bufs) in this slab */
387 u_int16_t sl_flags
; /* slab flags (see below) */
388 u_int16_t sl_len
; /* slab length */
389 void *sl_base
; /* base of allocated memory */
390 void *sl_head
; /* first free buffer */
391 TAILQ_ENTRY(mcl_slab
) sl_link
; /* next/prev slab on freelist */
394 #define SLF_MAPPED 0x0001 /* backed by a mapped page */
395 #define SLF_PARTIAL 0x0002 /* part of another slab */
396 #define SLF_DETACHED 0x0004 /* not in slab freelist */
399 * The array of slabs are broken into groups of arrays per 1MB of kernel
400 * memory to reduce the footprint. Each group is allocated on demand
401 * whenever a new piece of memory mapped in from the VM crosses the 1MB
404 #define NSLABSPMB ((1 << MBSHIFT) >> PGSHIFT) /* 256 slabs/grp */
406 typedef struct mcl_slabg
{
407 mcl_slab_t slg_slab
[NSLABSPMB
]; /* group of slabs */
411 * Number of slabs needed to control a 16KB cluster object.
413 #define NSLABSP16KB (M16KCLBYTES >> PGSHIFT)
416 * Per-cluster audit structure.
419 mcache_audit_t
*cl_audit
[NMBPBG
]; /* array of audits */
423 struct thread
*msa_thread
; /* thread doing transaction */
424 struct thread
*msa_pthread
; /* previous transaction thread */
425 uint32_t msa_tstamp
; /* transaction timestamp (ms) */
426 uint32_t msa_ptstamp
; /* prev transaction timestamp (ms) */
427 uint16_t msa_depth
; /* pc stack depth */
428 uint16_t msa_pdepth
; /* previous transaction pc stack */
429 void *msa_stack
[MCACHE_STACK_DEPTH
];
430 void *msa_pstack
[MCACHE_STACK_DEPTH
];
431 } mcl_scratch_audit_t
;
435 * Size of data from the beginning of an mbuf that covers m_hdr,
436 * pkthdr and m_ext structures. If auditing is enabled, we allocate
437 * a shadow mbuf structure of this size inside each audit structure,
438 * and the contents of the real mbuf gets copied into it when the mbuf
439 * is freed. This allows us to pattern-fill the mbuf for integrity
440 * check, and to preserve any constructed mbuf fields (e.g. mbuf +
441 * cluster cache case). Note that we don't save the contents of
442 * clusters when they are freed; we simply pattern-fill them.
444 u_int8_t sc_mbuf
[(MSIZE
- _MHLEN
) + sizeof (_m_ext_t
)];
445 mcl_scratch_audit_t sc_scratch
__attribute__((aligned(8)));
446 } mcl_saved_contents_t
;
448 #define AUDIT_CONTENTS_SIZE (sizeof (mcl_saved_contents_t))
450 #define MCA_SAVED_MBUF_PTR(_mca) \
451 ((struct mbuf *)(void *)((mcl_saved_contents_t *) \
452 (_mca)->mca_contents)->sc_mbuf)
453 #define MCA_SAVED_MBUF_SIZE \
454 (sizeof (((mcl_saved_contents_t *)0)->sc_mbuf))
455 #define MCA_SAVED_SCRATCH_PTR(_mca) \
456 (&((mcl_saved_contents_t *)(_mca)->mca_contents)->sc_scratch)
459 * mbuf specific mcache audit flags
461 #define MB_INUSE 0x01 /* object has not been returned to slab */
462 #define MB_COMP_INUSE 0x02 /* object has not been returned to cslab */
463 #define MB_SCVALID 0x04 /* object has valid saved contents */
466 * Each of the following two arrays hold up to nmbclusters elements.
468 static mcl_audit_t
*mclaudit
; /* array of cluster audit information */
469 static unsigned int maxclaudit
; /* max # of entries in audit table */
470 static mcl_slabg_t
**slabstbl
; /* cluster slabs table */
471 static unsigned int maxslabgrp
; /* max # of entries in slabs table */
472 static unsigned int slabgrp
; /* # of entries in slabs table */
475 int nclusters
; /* # of clusters for non-jumbo (legacy) sizes */
476 int njcl
; /* # of clusters for jumbo sizes */
477 int njclbytes
; /* size of a jumbo cluster */
478 union mbigcluster
*mbutl
; /* first mapped cluster address */
479 union mbigcluster
*embutl
; /* ending virtual address of mclusters */
480 int _max_linkhdr
; /* largest link-level header */
481 int _max_protohdr
; /* largest protocol header */
482 int max_hdr
; /* largest link+protocol header */
483 int max_datalen
; /* MHLEN - max_hdr */
485 static boolean_t mclverify
; /* debug: pattern-checking */
486 static boolean_t mcltrace
; /* debug: stack tracing */
487 static boolean_t mclfindleak
; /* debug: leak detection */
488 static boolean_t mclexpleak
; /* debug: expose leak info to user space */
490 static struct timeval mb_start
; /* beginning of time */
492 /* mbuf leak detection variables */
493 static struct mleak_table mleak_table
;
494 static mleak_stat_t
*mleak_stat
;
496 #define MLEAK_STAT_SIZE(n) \
497 ((size_t)(&((mleak_stat_t *)0)->ml_trace[n]))
500 mcache_obj_t
*element
; /* the alloc'ed element, NULL if unused */
501 u_int32_t trace_index
; /* mtrace index for corresponding backtrace */
502 u_int32_t count
; /* How many objects were requested */
503 u_int64_t hitcount
; /* for determining hash effectiveness */
507 u_int64_t collisions
;
511 uintptr_t addr
[MLEAK_STACK_DEPTH
];
514 /* Size must be a power of two for the zhash to be able to just mask off bits */
515 #define MLEAK_ALLOCATION_MAP_NUM 512
516 #define MLEAK_TRACE_MAP_NUM 256
519 * Sample factor for how often to record a trace. This is overwritable
520 * by the boot-arg mleak_sample_factor.
522 #define MLEAK_SAMPLE_FACTOR 500
525 * Number of top leakers recorded.
527 #define MLEAK_NUM_TRACES 5
529 #define MB_LEAK_SPACING_64 " "
530 #define MB_LEAK_SPACING_32 " "
533 #define MB_LEAK_HDR_32 "\n\
534 trace [1] trace [2] trace [3] trace [4] trace [5] \n\
535 ---------- ---------- ---------- ---------- ---------- \n\
538 #define MB_LEAK_HDR_64 "\n\
539 trace [1] trace [2] trace [3] \
540 trace [4] trace [5] \n\
541 ------------------ ------------------ ------------------ \
542 ------------------ ------------------ \n\
545 static uint32_t mleak_alloc_buckets
= MLEAK_ALLOCATION_MAP_NUM
;
546 static uint32_t mleak_trace_buckets
= MLEAK_TRACE_MAP_NUM
;
548 /* Hashmaps of allocations and their corresponding traces */
549 static struct mallocation
*mleak_allocations
;
550 static struct mtrace
*mleak_traces
;
551 static struct mtrace
*mleak_top_trace
[MLEAK_NUM_TRACES
];
553 /* Lock to protect mleak tables from concurrent modification */
554 decl_lck_mtx_data(static, mleak_lock_data
);
555 static lck_mtx_t
*mleak_lock
= &mleak_lock_data
;
556 static lck_attr_t
*mleak_lock_attr
;
557 static lck_grp_t
*mleak_lock_grp
;
558 static lck_grp_attr_t
*mleak_lock_grp_attr
;
560 extern u_int32_t high_sb_max
;
562 /* The minimum number of objects that are allocated, to start. */
564 #define MINBIGCL (MINCL >> 1)
565 #define MIN16KCL (MINCL >> 2)
567 /* Low watermarks (only map in pages once free counts go below) */
568 #define MBIGCL_LOWAT MINBIGCL
569 #define M16KCL_LOWAT MIN16KCL
572 mbuf_class_t mtbl_class
; /* class type */
573 mcache_t
*mtbl_cache
; /* mcache for this buffer class */
574 TAILQ_HEAD(mcl_slhead
, mcl_slab
) mtbl_slablist
; /* slab list */
575 mcache_obj_t
*mtbl_cobjlist
; /* composite objects freelist */
576 mb_class_stat_t
*mtbl_stats
; /* statistics fetchable via sysctl */
577 u_int32_t mtbl_maxsize
; /* maximum buffer size */
578 int mtbl_minlimit
; /* minimum allowed */
579 int mtbl_maxlimit
; /* maximum allowed */
580 u_int32_t mtbl_wantpurge
; /* purge during next reclaim */
583 #define m_class(c) mbuf_table[c].mtbl_class
584 #define m_cache(c) mbuf_table[c].mtbl_cache
585 #define m_slablist(c) mbuf_table[c].mtbl_slablist
586 #define m_cobjlist(c) mbuf_table[c].mtbl_cobjlist
587 #define m_maxsize(c) mbuf_table[c].mtbl_maxsize
588 #define m_minlimit(c) mbuf_table[c].mtbl_minlimit
589 #define m_maxlimit(c) mbuf_table[c].mtbl_maxlimit
590 #define m_wantpurge(c) mbuf_table[c].mtbl_wantpurge
591 #define m_cname(c) mbuf_table[c].mtbl_stats->mbcl_cname
592 #define m_size(c) mbuf_table[c].mtbl_stats->mbcl_size
593 #define m_total(c) mbuf_table[c].mtbl_stats->mbcl_total
594 #define m_active(c) mbuf_table[c].mtbl_stats->mbcl_active
595 #define m_infree(c) mbuf_table[c].mtbl_stats->mbcl_infree
596 #define m_slab_cnt(c) mbuf_table[c].mtbl_stats->mbcl_slab_cnt
597 #define m_alloc_cnt(c) mbuf_table[c].mtbl_stats->mbcl_alloc_cnt
598 #define m_free_cnt(c) mbuf_table[c].mtbl_stats->mbcl_free_cnt
599 #define m_notified(c) mbuf_table[c].mtbl_stats->mbcl_notified
600 #define m_purge_cnt(c) mbuf_table[c].mtbl_stats->mbcl_purge_cnt
601 #define m_fail_cnt(c) mbuf_table[c].mtbl_stats->mbcl_fail_cnt
602 #define m_ctotal(c) mbuf_table[c].mtbl_stats->mbcl_ctotal
604 static mbuf_table_t mbuf_table
[] = {
606 * The caches for mbufs, regular clusters and big clusters.
608 { MC_MBUF
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_MBUF
)),
609 NULL
, NULL
, 0, 0, 0, 0 },
610 { MC_CL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_CL
)),
611 NULL
, NULL
, 0, 0, 0, 0 },
612 { MC_BIGCL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_BIGCL
)),
613 NULL
, NULL
, 0, 0, 0, 0 },
614 { MC_16KCL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_16KCL
)),
615 NULL
, NULL
, 0, 0, 0, 0 },
617 * The following are special caches; they serve as intermediate
618 * caches backed by the above rudimentary caches. Each object
619 * in the cache is an mbuf with a cluster attached to it. Unlike
620 * the above caches, these intermediate caches do not directly
621 * deal with the slab structures; instead, the constructed
622 * cached elements are simply stored in the freelists.
624 { MC_MBUF_CL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0 },
625 { MC_MBUF_BIGCL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0 },
626 { MC_MBUF_16KCL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0 },
629 #define NELEM(a) (sizeof (a) / sizeof ((a)[0]))
631 static void *mb_waitchan
= &mbuf_table
; /* wait channel for all caches */
632 static int mb_waiters
; /* number of waiters */
634 #define MB_WDT_MAXTIME 10 /* # of secs before watchdog panic */
635 static struct timeval mb_wdtstart
; /* watchdog start timestamp */
636 static char *mbuf_dump_buf
;
638 #define MBUF_DUMP_BUF_SIZE 2048
641 * mbuf watchdog is enabled by default on embedded platforms. It is
642 * also toggeable via the kern.ipc.mb_watchdog sysctl.
644 static unsigned int mb_watchdog
= 0;
647 static u_int32_t mb_redzone_cookie
;
648 static void m_redzone_init(struct mbuf
*);
649 static void m_redzone_verify(struct mbuf
*m
);
651 /* The following are used to serialize m_clalloc() */
652 static boolean_t mb_clalloc_busy
;
653 static void *mb_clalloc_waitchan
= &mb_clalloc_busy
;
654 static int mb_clalloc_waiters
;
656 static void mbuf_mtypes_sync(boolean_t
);
657 static int mbstat_sysctl SYSCTL_HANDLER_ARGS
;
658 static void mbuf_stat_sync(void);
659 static int mb_stat_sysctl SYSCTL_HANDLER_ARGS
;
660 static int mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS
;
661 static int mleak_table_sysctl SYSCTL_HANDLER_ARGS
;
662 static char *mbuf_dump(void);
663 static void mbuf_table_init(void);
664 static inline void m_incref(struct mbuf
*);
665 static inline u_int32_t
m_decref(struct mbuf
*);
666 static int m_clalloc(const u_int32_t
, const int, const u_int32_t
);
667 static void mbuf_worker_thread_init(void);
668 static mcache_obj_t
*slab_alloc(mbuf_class_t
, int);
669 static void slab_free(mbuf_class_t
, mcache_obj_t
*);
670 static unsigned int mbuf_slab_alloc(void *, mcache_obj_t
***,
672 static void mbuf_slab_free(void *, mcache_obj_t
*, int);
673 static void mbuf_slab_audit(void *, mcache_obj_t
*, boolean_t
);
674 static void mbuf_slab_notify(void *, u_int32_t
);
675 static unsigned int cslab_alloc(mbuf_class_t
, mcache_obj_t
***,
677 static unsigned int cslab_free(mbuf_class_t
, mcache_obj_t
*, int);
678 static unsigned int mbuf_cslab_alloc(void *, mcache_obj_t
***,
680 static void mbuf_cslab_free(void *, mcache_obj_t
*, int);
681 static void mbuf_cslab_audit(void *, mcache_obj_t
*, boolean_t
);
682 static int freelist_populate(mbuf_class_t
, unsigned int, int);
683 static void freelist_init(mbuf_class_t
);
684 static boolean_t
mbuf_cached_above(mbuf_class_t
, int);
685 static boolean_t
mbuf_steal(mbuf_class_t
, unsigned int);
686 static void m_reclaim(mbuf_class_t
, unsigned int, boolean_t
);
687 static int m_howmany(int, size_t);
688 static void mbuf_worker_thread(void);
689 static void mbuf_watchdog(void);
690 static boolean_t
mbuf_sleep(mbuf_class_t
, unsigned int, int);
692 static void mcl_audit_init(void *, mcache_audit_t
**, mcache_obj_t
**,
693 size_t, unsigned int);
694 static mcache_audit_t
*mcl_audit_buf2mca(mbuf_class_t
, mcache_obj_t
*);
695 static void mcl_audit_mbuf(mcache_audit_t
*, void *, boolean_t
, boolean_t
);
696 static void mcl_audit_cluster(mcache_audit_t
*, void *, size_t, boolean_t
,
698 static void mcl_audit_restore_mbuf(struct mbuf
*, mcache_audit_t
*, boolean_t
);
699 static void mcl_audit_save_mbuf(struct mbuf
*, mcache_audit_t
*);
700 static void mcl_audit_scratch(mcache_audit_t
*);
701 static void mcl_audit_mcheck_panic(struct mbuf
*);
702 static void mcl_audit_verify_nextptr(void *, mcache_audit_t
*);
704 static void mleak_activate(void);
705 static void mleak_logger(u_int32_t
, mcache_obj_t
*, boolean_t
);
706 static boolean_t
mleak_log(uintptr_t *, mcache_obj_t
*, uint32_t, int);
707 static void mleak_free(mcache_obj_t
*);
708 static void mleak_sort_traces(void);
709 static void mleak_update_stats(void);
711 static mcl_slab_t
*slab_get(void *);
712 static void slab_init(mcl_slab_t
*, mbuf_class_t
, u_int32_t
,
713 void *, void *, unsigned int, int, int);
714 static void slab_insert(mcl_slab_t
*, mbuf_class_t
);
715 static void slab_remove(mcl_slab_t
*, mbuf_class_t
);
716 static boolean_t
slab_inrange(mcl_slab_t
*, void *);
717 static void slab_nextptr_panic(mcl_slab_t
*, void *);
718 static void slab_detach(mcl_slab_t
*);
719 static boolean_t
slab_is_detached(mcl_slab_t
*);
721 static int m_copyback0(struct mbuf
**, int, int, const void *, int, int);
722 static struct mbuf
*m_split0(struct mbuf
*, int, int, int);
724 /* flags for m_copyback0 */
725 #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */
726 #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */
727 #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */
728 #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */
731 * This flag is set for all mbufs that come out of and into the composite
732 * mbuf + cluster caches, i.e. MC_MBUF_CL and MC_MBUF_BIGCL. mbufs that
733 * are marked with such a flag have clusters attached to them, and will be
734 * treated differently when they are freed; instead of being placed back
735 * into the mbuf and cluster freelists, the composite mbuf + cluster objects
736 * are placed back into the appropriate composite cache's freelist, and the
737 * actual freeing is deferred until the composite objects are purged. At
738 * such a time, this flag will be cleared from the mbufs and the objects
739 * will be freed into their own separate freelists.
741 #define EXTF_COMPOSITE 0x1
744 * This flag indicates that the external cluster is read-only, i.e. it is
745 * or was referred to by more than one mbufs. Once set, this flag is never
748 #define EXTF_READONLY 0x2
749 #define EXTF_MASK (EXTF_COMPOSITE | EXTF_READONLY)
751 #define MEXT_RFA(m) ((m)->m_ext.ext_refflags)
752 #define MEXT_REF(m) (MEXT_RFA(m)->refcnt)
753 #define MEXT_FLAGS(m) (MEXT_RFA(m)->flags)
754 #define MBUF_IS_COMPOSITE(m) \
755 (MEXT_REF(m) == 0 && (MEXT_FLAGS(m) & EXTF_MASK) == EXTF_COMPOSITE)
758 * Macros used to verify the integrity of the mbuf.
760 #define _MCHECK(m) { \
761 if ((m)->m_type != MT_FREE) { \
762 if (mclaudit == NULL) \
763 panic("MCHECK: m_type=%d m=%p", \
764 (u_int16_t)(m)->m_type, m); \
766 mcl_audit_mcheck_panic(m); \
770 #define MBUF_IN_MAP(addr) \
771 ((void *)(addr) >= (void *)mbutl && (void *)(addr) < (void *)embutl)
773 #define MRANGE(addr) { \
774 if (!MBUF_IN_MAP(addr)) \
775 panic("MRANGE: address out of range 0x%p", addr); \
779 * Macro version of mtod.
781 #define MTOD(m, t) ((t)((m)->m_data))
784 * Macros to obtain (4KB) cluster index and base cluster address.
787 #define MTOBG(x) (((char *)(x) - (char *)mbutl) >> MBIGCLSHIFT)
788 #define BGTOM(x) ((union mbigcluster *)(mbutl + (x)))
791 * Macro to find the mbuf index relative to a base.
793 #define MCLIDX(c, m) (((char *)(m) - (char *)(c)) >> MSIZESHIFT)
796 * Same thing for 2KB cluster index.
798 #define CLBGIDX(c, m) (((char *)(m) - (char *)(c)) >> MCLSHIFT)
801 * Macros used during mbuf and cluster initialization.
803 #define MBUF_INIT_PKTHDR(m) { \
804 (m)->m_pkthdr.rcvif = NULL; \
805 (m)->m_pkthdr.pkt_hdr = NULL; \
806 (m)->m_pkthdr.len = 0; \
807 (m)->m_pkthdr.csum_flags = 0; \
808 (m)->m_pkthdr.csum_data = 0; \
809 (m)->m_pkthdr.vlan_tag = 0; \
810 m_classifier_init(m, 0); \
816 #define MBUF_INIT(m, pkthdr, type) { \
818 (m)->m_next = (m)->m_nextpkt = NULL; \
820 (m)->m_type = type; \
821 if ((pkthdr) == 0) { \
822 (m)->m_data = (m)->m_dat; \
825 (m)->m_data = (m)->m_pktdat; \
826 (m)->m_flags = M_PKTHDR; \
827 MBUF_INIT_PKTHDR(m); \
831 #define MEXT_INIT(m, buf, size, free, arg, rfa, ref, flag) { \
832 (m)->m_data = (m)->m_ext.ext_buf = (buf); \
833 (m)->m_flags |= M_EXT; \
834 (m)->m_ext.ext_size = (size); \
835 (m)->m_ext.ext_free = (free); \
836 (m)->m_ext.ext_arg = (arg); \
837 (m)->m_ext.ext_refs.forward = (m)->m_ext.ext_refs.backward = \
838 &(m)->m_ext.ext_refs; \
839 MEXT_RFA(m) = (rfa); \
840 MEXT_REF(m) = (ref); \
841 MEXT_FLAGS(m) = (flag); \
844 #define MBUF_CL_INIT(m, buf, rfa, ref, flag) \
845 MEXT_INIT(m, buf, m_maxsize(MC_CL), NULL, NULL, rfa, ref, flag)
847 #define MBUF_BIGCL_INIT(m, buf, rfa, ref, flag) \
848 MEXT_INIT(m, buf, m_maxsize(MC_BIGCL), m_bigfree, NULL, rfa, ref, flag)
850 #define MBUF_16KCL_INIT(m, buf, rfa, ref, flag) \
851 MEXT_INIT(m, buf, m_maxsize(MC_16KCL), m_16kfree, NULL, rfa, ref, flag)
854 * Macro to convert BSD malloc sleep flag to mcache's
856 #define MSLEEPF(f) ((!((f) & M_DONTWAIT)) ? MCR_SLEEP : MCR_NOSLEEP)
859 * The structure that holds all mbuf class statistics exportable via sysctl.
860 * Similar to mbstat structure, the mb_stat structure is protected by the
861 * global mbuf lock. It contains additional information about the classes
862 * that allows for a more accurate view of the state of the allocator.
864 struct mb_stat
*mb_stat
;
865 struct omb_stat
*omb_stat
; /* For backwards compatibility */
867 #define MB_STAT_SIZE(n) \
868 ((size_t)(&((mb_stat_t *)0)->mbs_class[n]))
869 #define OMB_STAT_SIZE(n) \
870 ((size_t)(&((struct omb_stat *)0)->mbs_class[n]))
873 * The legacy structure holding all of the mbuf allocation statistics.
874 * The actual statistics used by the kernel are stored in the mbuf_table
875 * instead, and are updated atomically while the global mbuf lock is held.
876 * They are mirrored in mbstat to support legacy applications (e.g. netstat).
877 * Unlike before, the kernel no longer relies on the contents of mbstat for
878 * its operations (e.g. cluster expansion) because the structure is exposed
879 * to outside and could possibly be modified, therefore making it unsafe.
880 * With the exception of the mbstat.m_mtypes array (see below), all of the
881 * statistics are updated as they change.
883 struct mbstat mbstat
;
885 #define MBSTAT_MTYPES_MAX \
886 (sizeof (mbstat.m_mtypes) / sizeof (mbstat.m_mtypes[0]))
889 * Allocation statistics related to mbuf types (up to MT_MAX-1) are updated
890 * atomically and stored in a per-CPU structure which is lock-free; this is
891 * done in order to avoid writing to the global mbstat data structure which
892 * would cause false sharing. During sysctl request for kern.ipc.mbstat,
893 * the statistics across all CPUs will be converged into the mbstat.m_mtypes
894 * array and returned to the application. Any updates for types greater or
895 * equal than MT_MAX would be done atomically to the mbstat; this slows down
896 * performance but is okay since the kernel uses only up to MT_MAX-1 while
897 * anything beyond that (up to type 255) is considered a corner case.
900 unsigned int cpu_mtypes
[MT_MAX
];
901 } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE
), packed
)) mtypes_cpu_t
;
904 mtypes_cpu_t mbs_cpu
[1];
907 static mbuf_mtypes_t
*mbuf_mtypes
; /* per-CPU statistics */
909 #define MBUF_MTYPES_SIZE(n) \
910 ((size_t)(&((mbuf_mtypes_t *)0)->mbs_cpu[n]))
912 #define MTYPES_CPU(p) \
913 ((mtypes_cpu_t *)(void *)((char *)(p) + MBUF_MTYPES_SIZE(cpu_number())))
915 #define mtype_stat_add(type, n) { \
916 if ((unsigned)(type) < MT_MAX) { \
917 mtypes_cpu_t *mbs = MTYPES_CPU(mbuf_mtypes); \
918 atomic_add_32(&mbs->cpu_mtypes[type], n); \
919 } else if ((unsigned)(type) < (unsigned)MBSTAT_MTYPES_MAX) { \
920 atomic_add_16((int16_t *)&mbstat.m_mtypes[type], n); \
924 #define mtype_stat_sub(t, n) mtype_stat_add(t, -(n))
925 #define mtype_stat_inc(t) mtype_stat_add(t, 1)
926 #define mtype_stat_dec(t) mtype_stat_sub(t, 1)
929 mbuf_mtypes_sync(boolean_t locked
)
935 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
937 bzero(&mtc
, sizeof (mtc
));
938 for (m
= 0; m
< ncpu
; m
++) {
939 mtypes_cpu_t
*scp
= &mbuf_mtypes
->mbs_cpu
[m
];
942 bcopy(&scp
->cpu_mtypes
, &temp
.cpu_mtypes
,
943 sizeof (temp
.cpu_mtypes
));
945 for (n
= 0; n
< MT_MAX
; n
++)
946 mtc
.cpu_mtypes
[n
] += temp
.cpu_mtypes
[n
];
949 lck_mtx_lock(mbuf_mlock
);
950 for (n
= 0; n
< MT_MAX
; n
++)
951 mbstat
.m_mtypes
[n
] = mtc
.cpu_mtypes
[n
];
953 lck_mtx_unlock(mbuf_mlock
);
957 mbstat_sysctl SYSCTL_HANDLER_ARGS
959 #pragma unused(oidp, arg1, arg2)
960 mbuf_mtypes_sync(FALSE
);
962 return (SYSCTL_OUT(req
, &mbstat
, sizeof (mbstat
)));
973 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
975 for (k
= 0; k
< NELEM(mbuf_table
); k
++) {
977 ccp
= &cp
->mc_cpu
[0];
978 bktsize
= ccp
->cc_bktsize
;
979 sp
= mbuf_table
[k
].mtbl_stats
;
981 if (cp
->mc_flags
& MCF_NOCPUCACHE
)
982 sp
->mbcl_mc_state
= MCS_DISABLED
;
983 else if (cp
->mc_purge_cnt
> 0)
984 sp
->mbcl_mc_state
= MCS_PURGING
;
985 else if (bktsize
== 0)
986 sp
->mbcl_mc_state
= MCS_OFFLINE
;
988 sp
->mbcl_mc_state
= MCS_ONLINE
;
990 sp
->mbcl_mc_cached
= 0;
991 for (m
= 0; m
< ncpu
; m
++) {
992 ccp
= &cp
->mc_cpu
[m
];
993 if (ccp
->cc_objs
> 0)
994 sp
->mbcl_mc_cached
+= ccp
->cc_objs
;
995 if (ccp
->cc_pobjs
> 0)
996 sp
->mbcl_mc_cached
+= ccp
->cc_pobjs
;
998 sp
->mbcl_mc_cached
+= (cp
->mc_full
.bl_total
* bktsize
);
999 sp
->mbcl_active
= sp
->mbcl_total
- sp
->mbcl_mc_cached
-
1002 sp
->mbcl_mc_waiter_cnt
= cp
->mc_waiter_cnt
;
1003 sp
->mbcl_mc_wretry_cnt
= cp
->mc_wretry_cnt
;
1004 sp
->mbcl_mc_nwretry_cnt
= cp
->mc_nwretry_cnt
;
1006 /* Calculate total count specific to each class */
1007 sp
->mbcl_ctotal
= sp
->mbcl_total
;
1008 switch (m_class(k
)) {
1010 /* Deduct mbufs used in composite caches */
1011 sp
->mbcl_ctotal
-= (m_total(MC_MBUF_CL
) +
1012 m_total(MC_MBUF_BIGCL
));
1016 /* Deduct clusters used in composite cache */
1017 sp
->mbcl_ctotal
-= m_total(MC_MBUF_CL
);
1021 /* Deduct clusters used in composite cache */
1022 sp
->mbcl_ctotal
-= m_total(MC_MBUF_BIGCL
);
1026 /* Deduct clusters used in composite cache */
1027 sp
->mbcl_ctotal
-= m_total(MC_MBUF_16KCL
);
1037 mb_stat_sysctl SYSCTL_HANDLER_ARGS
1039 #pragma unused(oidp, arg1, arg2)
1041 int k
, statsz
, proc64
= proc_is64bit(req
->p
);
1043 lck_mtx_lock(mbuf_mlock
);
1047 struct omb_class_stat
*oc
;
1048 struct mb_class_stat
*c
;
1050 omb_stat
->mbs_cnt
= mb_stat
->mbs_cnt
;
1051 oc
= &omb_stat
->mbs_class
[0];
1052 c
= &mb_stat
->mbs_class
[0];
1053 for (k
= 0; k
< omb_stat
->mbs_cnt
; k
++, oc
++, c
++) {
1054 (void) snprintf(oc
->mbcl_cname
, sizeof (oc
->mbcl_cname
),
1055 "%s", c
->mbcl_cname
);
1056 oc
->mbcl_size
= c
->mbcl_size
;
1057 oc
->mbcl_total
= c
->mbcl_total
;
1058 oc
->mbcl_active
= c
->mbcl_active
;
1059 oc
->mbcl_infree
= c
->mbcl_infree
;
1060 oc
->mbcl_slab_cnt
= c
->mbcl_slab_cnt
;
1061 oc
->mbcl_alloc_cnt
= c
->mbcl_alloc_cnt
;
1062 oc
->mbcl_free_cnt
= c
->mbcl_free_cnt
;
1063 oc
->mbcl_notified
= c
->mbcl_notified
;
1064 oc
->mbcl_purge_cnt
= c
->mbcl_purge_cnt
;
1065 oc
->mbcl_fail_cnt
= c
->mbcl_fail_cnt
;
1066 oc
->mbcl_ctotal
= c
->mbcl_ctotal
;
1067 oc
->mbcl_mc_state
= c
->mbcl_mc_state
;
1068 oc
->mbcl_mc_cached
= c
->mbcl_mc_cached
;
1069 oc
->mbcl_mc_waiter_cnt
= c
->mbcl_mc_waiter_cnt
;
1070 oc
->mbcl_mc_wretry_cnt
= c
->mbcl_mc_wretry_cnt
;
1071 oc
->mbcl_mc_nwretry_cnt
= c
->mbcl_mc_nwretry_cnt
;
1074 statsz
= OMB_STAT_SIZE(NELEM(mbuf_table
));
1077 statsz
= MB_STAT_SIZE(NELEM(mbuf_table
));
1080 lck_mtx_unlock(mbuf_mlock
);
1082 return (SYSCTL_OUT(req
, statp
, statsz
));
1086 mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS
1088 #pragma unused(oidp, arg1, arg2)
1091 /* Ensure leak tracing turned on */
1092 if (!mclfindleak
|| !mclexpleak
)
1095 lck_mtx_lock(mleak_lock
);
1096 mleak_update_stats();
1097 i
= SYSCTL_OUT(req
, mleak_stat
, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES
));
1098 lck_mtx_unlock(mleak_lock
);
1104 mleak_table_sysctl SYSCTL_HANDLER_ARGS
1106 #pragma unused(oidp, arg1, arg2)
1109 /* Ensure leak tracing turned on */
1110 if (!mclfindleak
|| !mclexpleak
)
1113 lck_mtx_lock(mleak_lock
);
1114 i
= SYSCTL_OUT(req
, &mleak_table
, sizeof (mleak_table
));
1115 lck_mtx_unlock(mleak_lock
);
1121 m_incref(struct mbuf
*m
)
1124 volatile UInt32
*addr
= (volatile UInt32
*)&MEXT_REF(m
);
1130 } while (!OSCompareAndSwap(old
, new, addr
));
1133 * If cluster is shared, mark it with (sticky) EXTF_READONLY;
1134 * we don't clear the flag when the refcount goes back to 1
1135 * to simplify code calling m_mclhasreference().
1137 if (new > 1 && !(MEXT_FLAGS(m
) & EXTF_READONLY
))
1138 (void) OSBitOrAtomic(EXTF_READONLY
, &MEXT_FLAGS(m
));
1141 static inline u_int32_t
1142 m_decref(struct mbuf
*m
)
1145 volatile UInt32
*addr
= (volatile UInt32
*)&MEXT_REF(m
);
1151 } while (!OSCompareAndSwap(old
, new, addr
));
1157 mbuf_table_init(void)
1159 unsigned int b
, c
, s
;
1162 MALLOC(omb_stat
, struct omb_stat
*, OMB_STAT_SIZE(NELEM(mbuf_table
)),
1163 M_TEMP
, M_WAITOK
| M_ZERO
);
1164 VERIFY(omb_stat
!= NULL
);
1166 MALLOC(mb_stat
, mb_stat_t
*, MB_STAT_SIZE(NELEM(mbuf_table
)),
1167 M_TEMP
, M_WAITOK
| M_ZERO
);
1168 VERIFY(mb_stat
!= NULL
);
1170 mb_stat
->mbs_cnt
= NELEM(mbuf_table
);
1171 for (m
= 0; m
< NELEM(mbuf_table
); m
++)
1172 mbuf_table
[m
].mtbl_stats
= &mb_stat
->mbs_class
[m
];
1174 #if CONFIG_MBUF_JUMBO
1176 * Set aside 1/3 of the mbuf cluster map for jumbo clusters; we do
1177 * this only on platforms where jumbo cluster pool is enabled.
1179 njcl
= nmbclusters
/ 3;
1180 njclbytes
= M16KCLBYTES
;
1181 #endif /* CONFIG_MBUF_JUMBO */
1184 * nclusters holds both the 2KB and 4KB pools, so ensure it's
1185 * a multiple of 4KB clusters.
1187 nclusters
= P2ROUNDDOWN(nmbclusters
- njcl
, NCLPBG
);
1190 * Each jumbo cluster takes 8 2KB clusters, so make
1191 * sure that the pool size is evenly divisible by 8;
1192 * njcl is in 2KB unit, hence treated as such.
1194 njcl
= P2ROUNDDOWN(nmbclusters
- nclusters
, 8);
1196 /* Update nclusters with rounded down value of njcl */
1197 nclusters
= P2ROUNDDOWN(nmbclusters
- njcl
, NCLPBG
);
1201 * njcl is valid only on platforms with 16KB jumbo clusters, where
1202 * it is configured to 1/3 of the pool size. On these platforms,
1203 * the remaining is used for 2KB and 4KB clusters. On platforms
1204 * without 16KB jumbo clusters, the entire pool is used for both
1205 * 2KB and 4KB clusters. A 4KB cluster can either be splitted into
1206 * 16 mbufs, or into 2 2KB clusters.
1208 * +---+---+------------ ... -----------+------- ... -------+
1209 * | c | b | s | njcl |
1210 * +---+---+------------ ... -----------+------- ... -------+
1212 * 1/32th of the shared region is reserved for pure 2KB and 4KB
1213 * clusters (1/64th each.)
1215 c
= P2ROUNDDOWN((nclusters
>> 6), 2); /* in 2KB unit */
1216 b
= P2ROUNDDOWN((nclusters
>> (6 + NCLPBGSHIFT
)), 2); /* in 4KB unit */
1217 s
= nclusters
- (c
+ (b
<< NCLPBGSHIFT
)); /* in 2KB unit */
1220 * 1/64th (c) is reserved for 2KB clusters.
1222 m_minlimit(MC_CL
) = c
;
1223 m_maxlimit(MC_CL
) = s
+ c
; /* in 2KB unit */
1224 m_maxsize(MC_CL
) = m_size(MC_CL
) = MCLBYTES
;
1225 (void) snprintf(m_cname(MC_CL
), MAX_MBUF_CNAME
, "cl");
1228 * Another 1/64th (b) of the map is reserved for 4KB clusters.
1229 * It cannot be turned into 2KB clusters or mbufs.
1231 m_minlimit(MC_BIGCL
) = b
;
1232 m_maxlimit(MC_BIGCL
) = (s
>> NCLPBGSHIFT
) + b
; /* in 4KB unit */
1233 m_maxsize(MC_BIGCL
) = m_size(MC_BIGCL
) = MBIGCLBYTES
;
1234 (void) snprintf(m_cname(MC_BIGCL
), MAX_MBUF_CNAME
, "bigcl");
1237 * The remaining 31/32ths (s) are all-purpose (mbufs, 2KB, or 4KB)
1239 m_minlimit(MC_MBUF
) = 0;
1240 m_maxlimit(MC_MBUF
) = (s
<< NMBPCLSHIFT
); /* in mbuf unit */
1241 m_maxsize(MC_MBUF
) = m_size(MC_MBUF
) = MSIZE
;
1242 (void) snprintf(m_cname(MC_MBUF
), MAX_MBUF_CNAME
, "mbuf");
1245 * Set limits for the composite classes.
1247 m_minlimit(MC_MBUF_CL
) = 0;
1248 m_maxlimit(MC_MBUF_CL
) = m_maxlimit(MC_CL
);
1249 m_maxsize(MC_MBUF_CL
) = MCLBYTES
;
1250 m_size(MC_MBUF_CL
) = m_size(MC_MBUF
) + m_size(MC_CL
);
1251 (void) snprintf(m_cname(MC_MBUF_CL
), MAX_MBUF_CNAME
, "mbuf_cl");
1253 m_minlimit(MC_MBUF_BIGCL
) = 0;
1254 m_maxlimit(MC_MBUF_BIGCL
) = m_maxlimit(MC_BIGCL
);
1255 m_maxsize(MC_MBUF_BIGCL
) = MBIGCLBYTES
;
1256 m_size(MC_MBUF_BIGCL
) = m_size(MC_MBUF
) + m_size(MC_BIGCL
);
1257 (void) snprintf(m_cname(MC_MBUF_BIGCL
), MAX_MBUF_CNAME
, "mbuf_bigcl");
1260 * And for jumbo classes.
1262 m_minlimit(MC_16KCL
) = 0;
1263 m_maxlimit(MC_16KCL
) = (njcl
>> NCLPJCLSHIFT
); /* in 16KB unit */
1264 m_maxsize(MC_16KCL
) = m_size(MC_16KCL
) = M16KCLBYTES
;
1265 (void) snprintf(m_cname(MC_16KCL
), MAX_MBUF_CNAME
, "16kcl");
1267 m_minlimit(MC_MBUF_16KCL
) = 0;
1268 m_maxlimit(MC_MBUF_16KCL
) = m_maxlimit(MC_16KCL
);
1269 m_maxsize(MC_MBUF_16KCL
) = M16KCLBYTES
;
1270 m_size(MC_MBUF_16KCL
) = m_size(MC_MBUF
) + m_size(MC_16KCL
);
1271 (void) snprintf(m_cname(MC_MBUF_16KCL
), MAX_MBUF_CNAME
, "mbuf_16kcl");
1274 * Initialize the legacy mbstat structure.
1276 bzero(&mbstat
, sizeof (mbstat
));
1277 mbstat
.m_msize
= m_maxsize(MC_MBUF
);
1278 mbstat
.m_mclbytes
= m_maxsize(MC_CL
);
1279 mbstat
.m_minclsize
= MINCLSIZE
;
1280 mbstat
.m_mlen
= MLEN
;
1281 mbstat
.m_mhlen
= MHLEN
;
1282 mbstat
.m_bigmclbytes
= m_maxsize(MC_BIGCL
);
1285 #if defined(__LP64__)
1286 typedef struct ncl_tbl
{
1287 uint64_t nt_maxmem
; /* memory (sane) size */
1288 uint32_t nt_mbpool
; /* mbuf pool size */
1292 static ncl_tbl_t ncl_table
[] = {
1293 { (1ULL << GBSHIFT
) /* 1 GB */, (64 << MBSHIFT
) /* 64 MB */ },
1294 { (1ULL << (GBSHIFT
+ 3)) /* 8 GB */, (96 << MBSHIFT
) /* 96 MB */ },
1295 { (1ULL << (GBSHIFT
+ 4)) /* 16 GB */, (128 << MBSHIFT
) /* 128 MB */ },
1300 static ncl_tbl_t ncl_table_srv
[] = {
1301 { (1ULL << GBSHIFT
) /* 1 GB */, (96 << MBSHIFT
) /* 96 MB */ },
1302 { (1ULL << (GBSHIFT
+ 2)) /* 4 GB */, (128 << MBSHIFT
) /* 128 MB */ },
1303 { (1ULL << (GBSHIFT
+ 3)) /* 8 GB */, (160 << MBSHIFT
) /* 160 MB */ },
1304 { (1ULL << (GBSHIFT
+ 4)) /* 16 GB */, (192 << MBSHIFT
) /* 192 MB */ },
1305 { (1ULL << (GBSHIFT
+ 5)) /* 32 GB */, (256 << MBSHIFT
) /* 256 MB */ },
1306 { (1ULL << (GBSHIFT
+ 6)) /* 64 GB */, (384 << MBSHIFT
) /* 384 MB */ },
1309 #endif /* __LP64__ */
1311 __private_extern__
unsigned int
1312 mbuf_default_ncl(int server
, uint64_t mem
)
1314 #if !defined(__LP64__)
1315 #pragma unused(server)
1318 * 32-bit kernel (default to 64MB of mbuf pool for >= 1GB RAM).
1320 if ((n
= ((mem
/ 16) / MCLBYTES
)) > 32768)
1324 ncl_tbl_t
*tbl
= (server
? ncl_table_srv
: ncl_table
);
1326 * 64-bit kernel (mbuf pool size based on table).
1328 n
= tbl
[0].nt_mbpool
;
1329 for (i
= 0; tbl
[i
].nt_mbpool
!= 0; i
++) {
1330 if (mem
< tbl
[i
].nt_maxmem
)
1332 n
= tbl
[i
].nt_mbpool
;
1335 #endif /* !__LP64__ */
1339 __private_extern__
void
1343 unsigned int initmcl
= 0;
1345 thread_t thread
= THREAD_NULL
;
1347 microuptime(&mb_start
);
1350 * These MBUF_ values must be equal to their private counterparts.
1352 _CASSERT(MBUF_EXT
== M_EXT
);
1353 _CASSERT(MBUF_PKTHDR
== M_PKTHDR
);
1354 _CASSERT(MBUF_EOR
== M_EOR
);
1355 _CASSERT(MBUF_LOOP
== M_LOOP
);
1356 _CASSERT(MBUF_BCAST
== M_BCAST
);
1357 _CASSERT(MBUF_MCAST
== M_MCAST
);
1358 _CASSERT(MBUF_FRAG
== M_FRAG
);
1359 _CASSERT(MBUF_FIRSTFRAG
== M_FIRSTFRAG
);
1360 _CASSERT(MBUF_LASTFRAG
== M_LASTFRAG
);
1361 _CASSERT(MBUF_PROMISC
== M_PROMISC
);
1362 _CASSERT(MBUF_HASFCS
== M_HASFCS
);
1364 _CASSERT(MBUF_TYPE_FREE
== MT_FREE
);
1365 _CASSERT(MBUF_TYPE_DATA
== MT_DATA
);
1366 _CASSERT(MBUF_TYPE_HEADER
== MT_HEADER
);
1367 _CASSERT(MBUF_TYPE_SOCKET
== MT_SOCKET
);
1368 _CASSERT(MBUF_TYPE_PCB
== MT_PCB
);
1369 _CASSERT(MBUF_TYPE_RTABLE
== MT_RTABLE
);
1370 _CASSERT(MBUF_TYPE_HTABLE
== MT_HTABLE
);
1371 _CASSERT(MBUF_TYPE_ATABLE
== MT_ATABLE
);
1372 _CASSERT(MBUF_TYPE_SONAME
== MT_SONAME
);
1373 _CASSERT(MBUF_TYPE_SOOPTS
== MT_SOOPTS
);
1374 _CASSERT(MBUF_TYPE_FTABLE
== MT_FTABLE
);
1375 _CASSERT(MBUF_TYPE_RIGHTS
== MT_RIGHTS
);
1376 _CASSERT(MBUF_TYPE_IFADDR
== MT_IFADDR
);
1377 _CASSERT(MBUF_TYPE_CONTROL
== MT_CONTROL
);
1378 _CASSERT(MBUF_TYPE_OOBDATA
== MT_OOBDATA
);
1380 _CASSERT(MBUF_TSO_IPV4
== CSUM_TSO_IPV4
);
1381 _CASSERT(MBUF_TSO_IPV6
== CSUM_TSO_IPV6
);
1382 _CASSERT(MBUF_CSUM_REQ_SUM16
== CSUM_PARTIAL
);
1383 _CASSERT(MBUF_CSUM_TCP_SUM16
== MBUF_CSUM_REQ_SUM16
);
1384 _CASSERT(MBUF_CSUM_REQ_IP
== CSUM_IP
);
1385 _CASSERT(MBUF_CSUM_REQ_TCP
== CSUM_TCP
);
1386 _CASSERT(MBUF_CSUM_REQ_UDP
== CSUM_UDP
);
1387 _CASSERT(MBUF_CSUM_REQ_TCPIPV6
== CSUM_TCPIPV6
);
1388 _CASSERT(MBUF_CSUM_REQ_UDPIPV6
== CSUM_UDPIPV6
);
1389 _CASSERT(MBUF_CSUM_DID_IP
== CSUM_IP_CHECKED
);
1390 _CASSERT(MBUF_CSUM_IP_GOOD
== CSUM_IP_VALID
);
1391 _CASSERT(MBUF_CSUM_DID_DATA
== CSUM_DATA_VALID
);
1392 _CASSERT(MBUF_CSUM_PSEUDO_HDR
== CSUM_PSEUDO_HDR
);
1394 _CASSERT(MBUF_WAITOK
== M_WAIT
);
1395 _CASSERT(MBUF_DONTWAIT
== M_DONTWAIT
);
1396 _CASSERT(MBUF_COPYALL
== M_COPYALL
);
1398 _CASSERT(MBUF_SC2TC(MBUF_SC_BK_SYS
) == MBUF_TC_BK
);
1399 _CASSERT(MBUF_SC2TC(MBUF_SC_BK
) == MBUF_TC_BK
);
1400 _CASSERT(MBUF_SC2TC(MBUF_SC_BE
) == MBUF_TC_BE
);
1401 _CASSERT(MBUF_SC2TC(MBUF_SC_RD
) == MBUF_TC_BE
);
1402 _CASSERT(MBUF_SC2TC(MBUF_SC_OAM
) == MBUF_TC_BE
);
1403 _CASSERT(MBUF_SC2TC(MBUF_SC_AV
) == MBUF_TC_VI
);
1404 _CASSERT(MBUF_SC2TC(MBUF_SC_RV
) == MBUF_TC_VI
);
1405 _CASSERT(MBUF_SC2TC(MBUF_SC_VI
) == MBUF_TC_VI
);
1406 _CASSERT(MBUF_SC2TC(MBUF_SC_VO
) == MBUF_TC_VO
);
1407 _CASSERT(MBUF_SC2TC(MBUF_SC_CTL
) == MBUF_TC_VO
);
1409 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BK
) == SCVAL_BK
);
1410 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BE
) == SCVAL_BE
);
1411 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VI
) == SCVAL_VI
);
1412 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VO
) == SCVAL_VO
);
1414 /* Module specific scratch space (32-bit alignment requirement) */
1415 _CASSERT(!(offsetof(struct mbuf
, m_pkthdr
.pkt_mpriv
) %
1416 sizeof (uint32_t)));
1418 /* Initialize random red zone cookie value */
1419 _CASSERT(sizeof (mb_redzone_cookie
) ==
1420 sizeof (((struct pkthdr
*)0)->redzone
));
1421 read_random(&mb_redzone_cookie
, sizeof (mb_redzone_cookie
));
1423 /* Make sure we don't save more than we should */
1424 _CASSERT(MCA_SAVED_MBUF_SIZE
<= sizeof (struct mbuf
));
1426 if (nmbclusters
== 0)
1427 nmbclusters
= NMBCLUSTERS
;
1429 /* This should be a sane (at least even) value by now */
1430 VERIFY(nmbclusters
!= 0 && !(nmbclusters
& 0x1));
1432 /* Setup the mbuf table */
1435 /* Global lock for common layer */
1436 mbuf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1437 mbuf_mlock_grp
= lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr
);
1438 mbuf_mlock_attr
= lck_attr_alloc_init();
1439 lck_mtx_init(mbuf_mlock
, mbuf_mlock_grp
, mbuf_mlock_attr
);
1442 * Allocate cluster slabs table:
1444 * maxslabgrp = (N * 2048) / (1024 * 1024)
1446 * Where N is nmbclusters rounded up to the nearest 512. This yields
1447 * mcl_slab_g_t units, each one representing a MB of memory.
1450 (P2ROUNDUP(nmbclusters
, (MBSIZE
>> 11)) << MCLSHIFT
) >> MBSHIFT
;
1451 MALLOC(slabstbl
, mcl_slabg_t
**, maxslabgrp
* sizeof (mcl_slabg_t
*),
1452 M_TEMP
, M_WAITOK
| M_ZERO
);
1453 VERIFY(slabstbl
!= NULL
);
1456 * Allocate audit structures, if needed:
1458 * maxclaudit = (maxslabgrp * 1024 * 1024) / 4096
1460 * This yields mcl_audit_t units, each one representing a page.
1462 PE_parse_boot_argn("mbuf_debug", &mbuf_debug
, sizeof (mbuf_debug
));
1463 mbuf_debug
|= mcache_getflags();
1464 if (mbuf_debug
& MCF_DEBUG
) {
1465 maxclaudit
= ((maxslabgrp
<< MBSHIFT
) >> PGSHIFT
);
1466 MALLOC(mclaudit
, mcl_audit_t
*, maxclaudit
* sizeof (*mclaudit
),
1467 M_TEMP
, M_WAITOK
| M_ZERO
);
1468 VERIFY(mclaudit
!= NULL
);
1470 mcl_audit_con_cache
= mcache_create("mcl_audit_contents",
1471 AUDIT_CONTENTS_SIZE
, sizeof (u_int64_t
), 0, MCR_SLEEP
);
1472 VERIFY(mcl_audit_con_cache
!= NULL
);
1474 mclverify
= (mbuf_debug
& MCF_VERIFY
);
1475 mcltrace
= (mbuf_debug
& MCF_TRACE
);
1476 mclfindleak
= !(mbuf_debug
& MCF_NOLEAKLOG
);
1477 mclexpleak
= mclfindleak
&& (mbuf_debug
& MCF_EXPLEAKLOG
);
1479 /* Enable mbuf leak logging, with a lock to protect the tables */
1481 mleak_lock_grp_attr
= lck_grp_attr_alloc_init();
1482 mleak_lock_grp
= lck_grp_alloc_init("mleak_lock", mleak_lock_grp_attr
);
1483 mleak_lock_attr
= lck_attr_alloc_init();
1484 lck_mtx_init(mleak_lock
, mleak_lock_grp
, mleak_lock_attr
);
1488 /* Calculate the number of pages assigned to the cluster pool */
1489 mcl_pages
= (nmbclusters
* MCLBYTES
) / CLBYTES
;
1490 MALLOC(mcl_paddr
, ppnum_t
*, mcl_pages
* sizeof (ppnum_t
),
1492 VERIFY(mcl_paddr
!= NULL
);
1494 /* Register with the I/O Bus mapper */
1495 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
1496 bzero((char *)mcl_paddr
, mcl_pages
* sizeof (ppnum_t
));
1498 embutl
= (union mbigcluster
*)
1499 ((void *)((unsigned char *)mbutl
+ (nmbclusters
* MCLBYTES
)));
1500 VERIFY((((char *)embutl
- (char *)mbutl
) % MBIGCLBYTES
) == 0);
1502 /* Prime up the freelist */
1503 PE_parse_boot_argn("initmcl", &initmcl
, sizeof (initmcl
));
1505 initmcl
>>= NCLPBGSHIFT
; /* become a 4K unit */
1506 if (initmcl
> m_maxlimit(MC_BIGCL
))
1507 initmcl
= m_maxlimit(MC_BIGCL
);
1509 if (initmcl
< m_minlimit(MC_BIGCL
))
1510 initmcl
= m_minlimit(MC_BIGCL
);
1512 lck_mtx_lock(mbuf_mlock
);
1515 * For classes with non-zero minimum limits, populate their freelists
1516 * so that m_total(class) is at least m_minlimit(class).
1518 VERIFY(m_total(MC_BIGCL
) == 0 && m_minlimit(MC_BIGCL
) != 0);
1519 freelist_populate(m_class(MC_BIGCL
), initmcl
, M_WAIT
);
1520 VERIFY(m_total(MC_BIGCL
) >= m_minlimit(MC_BIGCL
));
1521 freelist_init(m_class(MC_CL
));
1523 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
1524 /* Make sure we didn't miss any */
1525 VERIFY(m_minlimit(m_class(m
)) == 0 ||
1526 m_total(m_class(m
)) >= m_minlimit(m_class(m
)));
1529 lck_mtx_unlock(mbuf_mlock
);
1531 (void) kernel_thread_start((thread_continue_t
)mbuf_worker_thread_init
,
1533 thread_deallocate(thread
);
1535 ref_cache
= mcache_create("mext_ref", sizeof (struct ext_ref
),
1538 /* Create the cache for each class */
1539 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
1540 void *allocfunc
, *freefunc
, *auditfunc
, *logfunc
;
1544 if (m_class(m
) == MC_MBUF_CL
|| m_class(m
) == MC_MBUF_BIGCL
||
1545 m_class(m
) == MC_MBUF_16KCL
) {
1546 allocfunc
= mbuf_cslab_alloc
;
1547 freefunc
= mbuf_cslab_free
;
1548 auditfunc
= mbuf_cslab_audit
;
1549 logfunc
= mleak_logger
;
1551 allocfunc
= mbuf_slab_alloc
;
1552 freefunc
= mbuf_slab_free
;
1553 auditfunc
= mbuf_slab_audit
;
1554 logfunc
= mleak_logger
;
1558 * Disable per-CPU caches for jumbo classes if there
1559 * is no jumbo cluster pool available in the system.
1560 * The cache itself is still created (but will never
1561 * be populated) since it simplifies the code.
1563 if ((m_class(m
) == MC_MBUF_16KCL
|| m_class(m
) == MC_16KCL
) &&
1565 flags
|= MCF_NOCPUCACHE
;
1568 flags
|= MCF_NOLEAKLOG
;
1570 m_cache(m
) = mcache_create_ext(m_cname(m
), m_maxsize(m
),
1571 allocfunc
, freefunc
, auditfunc
, logfunc
, mbuf_slab_notify
,
1572 (void *)(uintptr_t)m
, flags
, MCR_SLEEP
);
1576 * Allocate structure for per-CPU statistics that's aligned
1577 * on the CPU cache boundary; this code assumes that we never
1578 * uninitialize this framework, since the original address
1579 * before alignment is not saved.
1581 ncpu
= ml_get_max_cpus();
1582 MALLOC(buf
, void *, MBUF_MTYPES_SIZE(ncpu
) + CPU_CACHE_LINE_SIZE
,
1584 VERIFY(buf
!= NULL
);
1586 mbuf_mtypes
= (mbuf_mtypes_t
*)P2ROUNDUP((intptr_t)buf
,
1587 CPU_CACHE_LINE_SIZE
);
1588 bzero(mbuf_mtypes
, MBUF_MTYPES_SIZE(ncpu
));
1591 * Set the max limit on sb_max to be 1/16 th of the size of
1592 * memory allocated for mbuf clusters.
1594 high_sb_max
= (nmbclusters
<< (MCLSHIFT
- 4));
1595 if (high_sb_max
< sb_max
) {
1596 /* sb_max is too large for this configuration, scale it down */
1597 if (high_sb_max
> (1 << MBSHIFT
)) {
1598 /* We have atleast 16 M of mbuf pool */
1599 sb_max
= high_sb_max
;
1600 } else if ((nmbclusters
<< MCLSHIFT
) > (1 << MBSHIFT
)) {
1602 * If we have more than 1M of mbufpool, cap the size of
1603 * max sock buf at 1M
1605 sb_max
= high_sb_max
= (1 << MBSHIFT
);
1607 sb_max
= high_sb_max
;
1611 /* allocate space for mbuf_dump_buf */
1612 MALLOC(mbuf_dump_buf
, char *, MBUF_DUMP_BUF_SIZE
, M_TEMP
, M_WAITOK
);
1613 VERIFY(mbuf_dump_buf
!= NULL
);
1615 if (mbuf_debug
& MCF_DEBUG
) {
1616 printf("%s: MLEN %d, MHLEN %d\n", __func__
,
1617 (int)_MLEN
, (int)_MHLEN
);
1620 printf("%s: done [%d MB total pool size, (%d/%d) split]\n", __func__
,
1621 (nmbclusters
<< MCLSHIFT
) >> MBSHIFT
,
1622 (nclusters
<< MCLSHIFT
) >> MBSHIFT
,
1623 (njcl
<< MCLSHIFT
) >> MBSHIFT
);
1627 * Obtain a slab of object(s) from the class's freelist.
1629 static mcache_obj_t
*
1630 slab_alloc(mbuf_class_t
class, int wait
)
1635 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1637 VERIFY(class != MC_16KCL
|| njcl
> 0);
1639 /* This should always be NULL for us */
1640 VERIFY(m_cobjlist(class) == NULL
);
1643 * Treat composite objects as having longer lifespan by using
1644 * a slab from the reverse direction, in hoping that this could
1645 * reduce the probability of fragmentation for slabs that hold
1646 * more than one buffer chunks (e.g. mbuf slabs). For other
1647 * slabs, this probably doesn't make much of a difference.
1649 if ((class == MC_MBUF
|| class == MC_CL
) && (wait
& MCR_COMP
))
1650 sp
= (mcl_slab_t
*)TAILQ_LAST(&m_slablist(class), mcl_slhead
);
1652 sp
= (mcl_slab_t
*)TAILQ_FIRST(&m_slablist(class));
1655 VERIFY(m_infree(class) == 0 && m_slab_cnt(class) == 0);
1656 /* The slab list for this class is empty */
1660 VERIFY(m_infree(class) > 0);
1661 VERIFY(!slab_is_detached(sp
));
1662 VERIFY(sp
->sl_class
== class &&
1663 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
1665 VERIFY(slab_inrange(sp
, buf
) && sp
== slab_get(buf
));
1667 if (class == MC_MBUF
) {
1668 sp
->sl_head
= buf
->obj_next
;
1669 VERIFY(sp
->sl_head
!= NULL
|| sp
->sl_refcnt
== (NMBPBG
- 1));
1670 } else if (class == MC_CL
) {
1671 sp
->sl_head
= buf
->obj_next
;
1672 VERIFY(sp
->sl_head
!= NULL
|| sp
->sl_refcnt
== (NCLPBG
- 1));
1676 if (sp
->sl_head
!= NULL
&& !slab_inrange(sp
, sp
->sl_head
)) {
1677 slab_nextptr_panic(sp
, sp
->sl_head
);
1678 /* In case sl_head is in the map but not in the slab */
1679 VERIFY(slab_inrange(sp
, sp
->sl_head
));
1683 /* Increment slab reference */
1686 if (mclaudit
!= NULL
) {
1687 mcache_audit_t
*mca
= mcl_audit_buf2mca(class, buf
);
1688 mca
->mca_uflags
= 0;
1689 /* Save contents on mbuf objects only */
1690 if (class == MC_MBUF
)
1691 mca
->mca_uflags
|= MB_SCVALID
;
1694 if (class == MC_CL
) {
1695 mbstat
.m_clfree
= (--m_infree(MC_CL
)) + m_infree(MC_MBUF_CL
);
1697 * A 2K cluster slab can have at most NCLPBG references.
1699 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_refcnt
<= NCLPBG
&&
1700 sp
->sl_chunks
== NCLPBG
&&
1701 sp
->sl_len
== m_maxsize(MC_BIGCL
));
1702 VERIFY(sp
->sl_refcnt
< NCLPBG
|| sp
->sl_head
== NULL
);
1703 } else if (class == MC_BIGCL
) {
1704 mbstat
.m_bigclfree
= (--m_infree(MC_BIGCL
)) +
1705 m_infree(MC_MBUF_BIGCL
);
1707 * A 4K cluster slab can have at most 1 reference.
1709 VERIFY(sp
->sl_refcnt
== 1 && sp
->sl_chunks
== 1 &&
1710 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1711 } else if (class == MC_16KCL
) {
1715 --m_infree(MC_16KCL
);
1716 VERIFY(sp
->sl_refcnt
== 1 && sp
->sl_chunks
== 1 &&
1717 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1719 * Increment 2nd-Nth slab reference, where N is NSLABSP16KB.
1720 * A 16KB big cluster takes NSLABSP16KB slabs, each having at
1723 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
1725 /* Next slab must already be present */
1726 VERIFY(nsp
!= NULL
);
1728 VERIFY(!slab_is_detached(nsp
));
1729 VERIFY(nsp
->sl_class
== MC_16KCL
&&
1730 nsp
->sl_flags
== (SLF_MAPPED
| SLF_PARTIAL
) &&
1731 nsp
->sl_refcnt
== 1 && nsp
->sl_chunks
== 0 &&
1732 nsp
->sl_len
== 0 && nsp
->sl_base
== sp
->sl_base
&&
1733 nsp
->sl_head
== NULL
);
1736 VERIFY(class == MC_MBUF
);
1737 --m_infree(MC_MBUF
);
1739 * If auditing is turned on, this check is
1740 * deferred until later in mbuf_slab_audit().
1742 if (mclaudit
== NULL
)
1743 _MCHECK((struct mbuf
*)buf
);
1745 * Since we have incremented the reference count above,
1746 * an mbuf slab (formerly a 4KB cluster slab that was cut
1747 * up into mbufs) must have a reference count between 1
1748 * and NMBPBG at this point.
1750 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_refcnt
<= NMBPBG
&&
1751 sp
->sl_chunks
== NMBPBG
&&
1752 sp
->sl_len
== m_maxsize(MC_BIGCL
));
1753 VERIFY(sp
->sl_refcnt
< NMBPBG
|| sp
->sl_head
== NULL
);
1756 /* If empty, remove this slab from the class's freelist */
1757 if (sp
->sl_head
== NULL
) {
1758 VERIFY(class != MC_MBUF
|| sp
->sl_refcnt
== NMBPBG
);
1759 VERIFY(class != MC_CL
|| sp
->sl_refcnt
== NCLPBG
);
1760 slab_remove(sp
, class);
1767 * Place a slab of object(s) back into a class's slab list.
1770 slab_free(mbuf_class_t
class, mcache_obj_t
*buf
)
1774 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1776 VERIFY(class != MC_16KCL
|| njcl
> 0);
1777 VERIFY(buf
->obj_next
== NULL
);
1779 VERIFY(sp
->sl_class
== class && slab_inrange(sp
, buf
) &&
1780 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
1782 /* Decrement slab reference */
1785 if (class == MC_CL
) {
1786 VERIFY(IS_P2ALIGNED(buf
, MCLBYTES
));
1788 * A slab that has been splitted for 2KB clusters can have
1789 * at most 1 outstanding reference at this point.
1791 VERIFY(sp
->sl_refcnt
>= 0 && sp
->sl_refcnt
<= (NCLPBG
- 1) &&
1792 sp
->sl_chunks
== NCLPBG
&&
1793 sp
->sl_len
== m_maxsize(MC_BIGCL
));
1794 VERIFY(sp
->sl_refcnt
< (NCLPBG
- 1) ||
1795 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1796 } else if (class == MC_BIGCL
) {
1797 VERIFY(IS_P2ALIGNED(buf
, MCLBYTES
));
1799 * A 4KB cluster slab can have at most 1 reference
1800 * which must be 0 at this point.
1802 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_chunks
== 1 &&
1803 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1804 VERIFY(slab_is_detached(sp
));
1805 } else if (class == MC_16KCL
) {
1809 * A 16KB cluster takes NSLABSP16KB slabs, all must
1810 * now have 0 reference.
1812 VERIFY(IS_P2ALIGNED(buf
, MBIGCLBYTES
));
1813 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_chunks
== 1 &&
1814 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1815 VERIFY(slab_is_detached(sp
));
1816 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
1818 /* Next slab must already be present */
1819 VERIFY(nsp
!= NULL
);
1821 VERIFY(slab_is_detached(nsp
));
1822 VERIFY(nsp
->sl_class
== MC_16KCL
&&
1823 (nsp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) &&
1824 nsp
->sl_refcnt
== 0 && nsp
->sl_chunks
== 0 &&
1825 nsp
->sl_len
== 0 && nsp
->sl_base
== sp
->sl_base
&&
1826 nsp
->sl_head
== NULL
);
1830 * A slab that has been splitted for mbufs has at most NMBPBG
1831 * reference counts. Since we have decremented one reference
1832 * above, it must now be between 0 and NMBPBG-1.
1834 VERIFY(class == MC_MBUF
);
1835 VERIFY(sp
->sl_refcnt
>= 0 && sp
->sl_refcnt
<= (NMBPBG
- 1) &&
1836 sp
->sl_chunks
== NMBPBG
&&
1837 sp
->sl_len
== m_maxsize(MC_BIGCL
));
1838 VERIFY(sp
->sl_refcnt
< (NMBPBG
- 1) ||
1839 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1843 * When auditing is enabled, ensure that the buffer still
1844 * contains the free pattern. Otherwise it got corrupted
1845 * while at the CPU cache layer.
1847 if (mclaudit
!= NULL
) {
1848 mcache_audit_t
*mca
= mcl_audit_buf2mca(class, buf
);
1850 mcache_audit_free_verify(mca
, buf
, 0, m_maxsize(class));
1852 mca
->mca_uflags
&= ~MB_SCVALID
;
1855 if (class == MC_CL
) {
1856 mbstat
.m_clfree
= (++m_infree(MC_CL
)) + m_infree(MC_MBUF_CL
);
1857 buf
->obj_next
= sp
->sl_head
;
1858 } else if (class == MC_BIGCL
) {
1859 mbstat
.m_bigclfree
= (++m_infree(MC_BIGCL
)) +
1860 m_infree(MC_MBUF_BIGCL
);
1861 } else if (class == MC_16KCL
) {
1862 ++m_infree(MC_16KCL
);
1864 ++m_infree(MC_MBUF
);
1865 buf
->obj_next
= sp
->sl_head
;
1870 * If a slab has been splitted to either one which holds 2KB clusters,
1871 * or one which holds mbufs, turn it back to one which holds a 4KB
1874 if (class == MC_MBUF
&& sp
->sl_refcnt
== 0 &&
1875 m_total(class) > m_minlimit(class) &&
1876 m_total(MC_BIGCL
) < m_maxlimit(MC_BIGCL
)) {
1879 m_total(MC_BIGCL
)++;
1880 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
1881 m_total(MC_MBUF
) -= NMBPBG
;
1882 mbstat
.m_mbufs
= m_total(MC_MBUF
);
1883 m_infree(MC_MBUF
) -= NMBPBG
;
1884 mtype_stat_add(MT_FREE
, -((unsigned)NMBPBG
));
1886 VERIFY(m_total(MC_BIGCL
) <= m_maxlimit(MC_BIGCL
));
1887 VERIFY(m_total(MC_MBUF
) >= m_minlimit(MC_MBUF
));
1890 struct mbuf
*m
= sp
->sl_head
;
1892 sp
->sl_head
= m
->m_next
;
1895 VERIFY(sp
->sl_head
== NULL
);
1897 /* Remove the slab from the mbuf class's slab list */
1898 slab_remove(sp
, class);
1900 /* Reinitialize it as a 4KB cluster slab */
1901 slab_init(sp
, MC_BIGCL
, sp
->sl_flags
, sp
->sl_base
, sp
->sl_base
,
1905 mcache_set_pattern(MCACHE_FREE_PATTERN
,
1906 (caddr_t
)sp
->sl_head
, m_maxsize(MC_BIGCL
));
1908 mbstat
.m_bigclfree
= (++m_infree(MC_BIGCL
)) +
1909 m_infree(MC_MBUF_BIGCL
);
1911 VERIFY(slab_is_detached(sp
));
1912 /* And finally switch class */
1914 } else if (class == MC_CL
&& sp
->sl_refcnt
== 0 &&
1915 m_total(class) > m_minlimit(class) &&
1916 m_total(MC_BIGCL
) < m_maxlimit(MC_BIGCL
)) {
1919 m_total(MC_BIGCL
)++;
1920 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
1921 m_total(MC_CL
) -= NCLPBG
;
1922 mbstat
.m_clusters
= m_total(MC_CL
);
1923 m_infree(MC_CL
) -= NCLPBG
;
1924 VERIFY(m_total(MC_BIGCL
) <= m_maxlimit(MC_BIGCL
));
1925 VERIFY(m_total(MC_CL
) >= m_minlimit(MC_CL
));
1928 union mcluster
*c
= sp
->sl_head
;
1930 sp
->sl_head
= c
->mcl_next
;
1933 VERIFY(sp
->sl_head
== NULL
);
1935 /* Remove the slab from the 2KB cluster class's slab list */
1936 slab_remove(sp
, class);
1938 /* Reinitialize it as a 4KB cluster slab */
1939 slab_init(sp
, MC_BIGCL
, sp
->sl_flags
, sp
->sl_base
, sp
->sl_base
,
1943 mcache_set_pattern(MCACHE_FREE_PATTERN
,
1944 (caddr_t
)sp
->sl_head
, m_maxsize(MC_BIGCL
));
1946 mbstat
.m_bigclfree
= (++m_infree(MC_BIGCL
)) +
1947 m_infree(MC_MBUF_BIGCL
);
1949 VERIFY(slab_is_detached(sp
));
1950 /* And finally switch class */
1954 /* Reinsert the slab to the class's slab list */
1955 if (slab_is_detached(sp
))
1956 slab_insert(sp
, class);
1960 * Common allocator for rudimentary objects called by the CPU cache layer
1961 * during an allocation request whenever there is no available element in the
1962 * bucket layer. It returns one or more elements from the appropriate global
1963 * freelist. If the freelist is empty, it will attempt to populate it and
1964 * retry the allocation.
1967 mbuf_slab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int num
, int wait
)
1969 mbuf_class_t
class = (mbuf_class_t
)arg
;
1970 unsigned int need
= num
;
1971 mcache_obj_t
**list
= *plist
;
1973 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
1976 lck_mtx_lock(mbuf_mlock
);
1979 if ((*list
= slab_alloc(class, wait
)) != NULL
) {
1980 (*list
)->obj_next
= NULL
;
1981 list
= *plist
= &(*list
)->obj_next
;
1985 * If the number of elements in freelist has
1986 * dropped below low watermark, asynchronously
1987 * populate the freelist now rather than doing
1988 * it later when we run out of elements.
1990 if (!mbuf_cached_above(class, wait
) &&
1991 m_infree(class) < m_total(class) >> 5) {
1992 (void) freelist_populate(class, 1,
1998 VERIFY(m_infree(class) == 0 || class == MC_CL
);
2000 (void) freelist_populate(class, 1,
2001 (wait
& MCR_NOSLEEP
) ? M_DONTWAIT
: M_WAIT
);
2003 if (m_infree(class) > 0)
2006 /* Check if there's anything at the cache layer */
2007 if (mbuf_cached_above(class, wait
))
2010 /* watchdog checkpoint */
2013 /* We have nothing and cannot block; give up */
2014 if (wait
& MCR_NOSLEEP
) {
2015 if (!(wait
& MCR_TRYHARD
)) {
2016 m_fail_cnt(class)++;
2023 * If the freelist is still empty and the caller is
2024 * willing to be blocked, sleep on the wait channel
2025 * until an element is available. Otherwise, if
2026 * MCR_TRYHARD is set, do our best to satisfy the
2027 * request without having to go to sleep.
2029 if (mbuf_worker_ready
&&
2030 mbuf_sleep(class, need
, wait
))
2033 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2037 m_alloc_cnt(class) += num
- need
;
2038 lck_mtx_unlock(mbuf_mlock
);
2040 return (num
- need
);
2044 * Common de-allocator for rudimentary objects called by the CPU cache
2045 * layer when one or more elements need to be returned to the appropriate
2049 mbuf_slab_free(void *arg
, mcache_obj_t
*list
, __unused
int purged
)
2051 mbuf_class_t
class = (mbuf_class_t
)arg
;
2052 mcache_obj_t
*nlist
;
2053 unsigned int num
= 0;
2056 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2058 lck_mtx_lock(mbuf_mlock
);
2061 nlist
= list
->obj_next
;
2062 list
->obj_next
= NULL
;
2063 slab_free(class, list
);
2065 if ((list
= nlist
) == NULL
)
2068 m_free_cnt(class) += num
;
2070 if ((w
= mb_waiters
) > 0)
2073 lck_mtx_unlock(mbuf_mlock
);
2076 wakeup(mb_waitchan
);
2080 * Common auditor for rudimentary objects called by the CPU cache layer
2081 * during an allocation or free request. For the former, this is called
2082 * after the objects are obtained from either the bucket or slab layer
2083 * and before they are returned to the caller. For the latter, this is
2084 * called immediately during free and before placing the objects into
2085 * the bucket or slab layer.
2088 mbuf_slab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
2090 mbuf_class_t
class = (mbuf_class_t
)arg
;
2091 mcache_audit_t
*mca
;
2093 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2095 while (list
!= NULL
) {
2096 lck_mtx_lock(mbuf_mlock
);
2097 mca
= mcl_audit_buf2mca(class, list
);
2099 /* Do the sanity checks */
2100 if (class == MC_MBUF
) {
2101 mcl_audit_mbuf(mca
, list
, FALSE
, alloc
);
2102 ASSERT(mca
->mca_uflags
& MB_SCVALID
);
2104 mcl_audit_cluster(mca
, list
, m_maxsize(class),
2106 ASSERT(!(mca
->mca_uflags
& MB_SCVALID
));
2108 /* Record this transaction */
2110 mcache_buffer_log(mca
, list
, m_cache(class), &mb_start
);
2113 mca
->mca_uflags
|= MB_INUSE
;
2115 mca
->mca_uflags
&= ~MB_INUSE
;
2116 /* Unpair the object (unconditionally) */
2117 mca
->mca_uptr
= NULL
;
2118 lck_mtx_unlock(mbuf_mlock
);
2120 list
= list
->obj_next
;
2125 * Common notify routine for all caches. It is called by mcache when
2126 * one or more objects get freed. We use this indication to trigger
2127 * the wakeup of any sleeping threads so that they can retry their
2128 * allocation requests.
2131 mbuf_slab_notify(void *arg
, u_int32_t reason
)
2133 mbuf_class_t
class = (mbuf_class_t
)arg
;
2136 ASSERT(MBUF_CLASS_VALID(class));
2138 if (reason
!= MCN_RETRYALLOC
)
2141 lck_mtx_lock(mbuf_mlock
);
2142 if ((w
= mb_waiters
) > 0) {
2143 m_notified(class)++;
2146 lck_mtx_unlock(mbuf_mlock
);
2149 wakeup(mb_waitchan
);
2153 * Obtain object(s) from the composite class's freelist.
2156 cslab_alloc(mbuf_class_t
class, mcache_obj_t
***plist
, unsigned int num
)
2158 unsigned int need
= num
;
2159 mcl_slab_t
*sp
, *clsp
, *nsp
;
2161 mcache_obj_t
**list
= *plist
;
2165 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2166 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2168 /* Get what we can from the freelist */
2169 while ((*list
= m_cobjlist(class)) != NULL
) {
2172 m
= (struct mbuf
*)*list
;
2174 cl
= m
->m_ext
.ext_buf
;
2175 clsp
= slab_get(cl
);
2176 VERIFY(m
->m_flags
== M_EXT
&& cl
!= NULL
);
2177 VERIFY(MEXT_RFA(m
) != NULL
&& MBUF_IS_COMPOSITE(m
));
2179 if (class == MC_MBUF_CL
) {
2180 VERIFY(clsp
->sl_refcnt
>= 1 &&
2181 clsp
->sl_refcnt
<= NCLPBG
);
2183 VERIFY(clsp
->sl_refcnt
== 1);
2186 if (class == MC_MBUF_16KCL
) {
2188 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2190 /* Next slab must already be present */
2191 VERIFY(nsp
!= NULL
);
2192 VERIFY(nsp
->sl_refcnt
== 1);
2196 if ((m_cobjlist(class) = (*list
)->obj_next
) != NULL
&&
2197 !MBUF_IN_MAP(m_cobjlist(class))) {
2198 slab_nextptr_panic(sp
, m_cobjlist(class));
2201 (*list
)->obj_next
= NULL
;
2202 list
= *plist
= &(*list
)->obj_next
;
2207 m_infree(class) -= (num
- need
);
2209 return (num
- need
);
2213 * Place object(s) back into a composite class's freelist.
2216 cslab_free(mbuf_class_t
class, mcache_obj_t
*list
, int purged
)
2218 mcache_obj_t
*o
, *tail
;
2219 unsigned int num
= 0;
2220 struct mbuf
*m
, *ms
;
2221 mcache_audit_t
*mca
= NULL
;
2222 mcache_obj_t
*ref_list
= NULL
;
2223 mcl_slab_t
*clsp
, *nsp
;
2225 mbuf_class_t cl_class
;
2227 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2228 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2229 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2231 if (class == MC_MBUF_CL
) {
2233 } else if (class == MC_MBUF_BIGCL
) {
2234 cl_class
= MC_BIGCL
;
2236 VERIFY(class == MC_MBUF_16KCL
);
2237 cl_class
= MC_16KCL
;
2242 while ((m
= ms
= (struct mbuf
*)o
) != NULL
) {
2243 mcache_obj_t
*rfa
, *nexto
= o
->obj_next
;
2245 /* Do the mbuf sanity checks */
2246 if (mclaudit
!= NULL
) {
2247 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2249 mcache_audit_free_verify(mca
, m
, 0,
2250 m_maxsize(MC_MBUF
));
2252 ms
= MCA_SAVED_MBUF_PTR(mca
);
2255 /* Do the cluster sanity checks */
2256 cl
= ms
->m_ext
.ext_buf
;
2257 clsp
= slab_get(cl
);
2259 size_t size
= m_maxsize(cl_class
);
2260 mcache_audit_free_verify(mcl_audit_buf2mca(cl_class
,
2261 (mcache_obj_t
*)cl
), cl
, 0, size
);
2263 VERIFY(ms
->m_type
== MT_FREE
);
2264 VERIFY(ms
->m_flags
== M_EXT
);
2265 VERIFY(MEXT_RFA(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2266 if (cl_class
== MC_CL
) {
2267 VERIFY(clsp
->sl_refcnt
>= 1 &&
2268 clsp
->sl_refcnt
<= NCLPBG
);
2270 VERIFY(clsp
->sl_refcnt
== 1);
2272 if (cl_class
== MC_16KCL
) {
2274 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2276 /* Next slab must already be present */
2277 VERIFY(nsp
!= NULL
);
2278 VERIFY(nsp
->sl_refcnt
== 1);
2283 * If we're asked to purge, restore the actual mbuf using
2284 * contents of the shadow structure (if auditing is enabled)
2285 * and clear EXTF_COMPOSITE flag from the mbuf, as we are
2286 * about to free it and the attached cluster into their caches.
2289 /* Restore constructed mbuf fields */
2290 if (mclaudit
!= NULL
)
2291 mcl_audit_restore_mbuf(m
, mca
, TRUE
);
2296 rfa
= (mcache_obj_t
*)(void *)MEXT_RFA(m
);
2297 rfa
->obj_next
= ref_list
;
2301 m
->m_type
= MT_FREE
;
2302 m
->m_flags
= m
->m_len
= 0;
2303 m
->m_next
= m
->m_nextpkt
= NULL
;
2305 /* Save mbuf fields and make auditing happy */
2306 if (mclaudit
!= NULL
)
2307 mcl_audit_mbuf(mca
, o
, FALSE
, FALSE
);
2309 VERIFY(m_total(class) > 0);
2314 slab_free(MC_MBUF
, o
);
2316 /* And free the cluster */
2317 ((mcache_obj_t
*)cl
)->obj_next
= NULL
;
2318 if (class == MC_MBUF_CL
)
2319 slab_free(MC_CL
, cl
);
2320 else if (class == MC_MBUF_BIGCL
)
2321 slab_free(MC_BIGCL
, cl
);
2323 slab_free(MC_16KCL
, cl
);
2332 tail
->obj_next
= m_cobjlist(class);
2333 m_cobjlist(class) = list
;
2334 m_infree(class) += num
;
2335 } else if (ref_list
!= NULL
) {
2336 mcache_free_ext(ref_cache
, ref_list
);
2343 * Common allocator for composite objects called by the CPU cache layer
2344 * during an allocation request whenever there is no available element in
2345 * the bucket layer. It returns one or more composite elements from the
2346 * appropriate global freelist. If the freelist is empty, it will attempt
2347 * to obtain the rudimentary objects from their caches and construct them
2348 * into composite mbuf + cluster objects.
2351 mbuf_cslab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int needed
,
2354 mbuf_class_t
class = (mbuf_class_t
)arg
;
2355 mbuf_class_t cl_class
= 0;
2356 unsigned int num
= 0, cnum
= 0, want
= needed
;
2357 mcache_obj_t
*ref_list
= NULL
;
2358 mcache_obj_t
*mp_list
= NULL
;
2359 mcache_obj_t
*clp_list
= NULL
;
2360 mcache_obj_t
**list
;
2361 struct ext_ref
*rfa
;
2365 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2368 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2370 /* There should not be any slab for this class */
2371 VERIFY(m_slab_cnt(class) == 0 &&
2372 m_slablist(class).tqh_first
== NULL
&&
2373 m_slablist(class).tqh_last
== NULL
);
2375 lck_mtx_lock(mbuf_mlock
);
2377 /* Try using the freelist first */
2378 num
= cslab_alloc(class, plist
, needed
);
2380 if (num
== needed
) {
2381 m_alloc_cnt(class) += num
;
2382 lck_mtx_unlock(mbuf_mlock
);
2386 lck_mtx_unlock(mbuf_mlock
);
2389 * We could not satisfy the request using the freelist alone;
2390 * allocate from the appropriate rudimentary caches and use
2391 * whatever we can get to construct the composite objects.
2396 * Mark these allocation requests as coming from a composite cache.
2397 * Also, if the caller is willing to be blocked, mark the request
2398 * with MCR_FAILOK such that we don't end up sleeping at the mbuf
2399 * slab layer waiting for the individual object when one or more
2400 * of the already-constructed composite objects are available.
2403 if (!(wait
& MCR_NOSLEEP
))
2406 /* allocate mbufs */
2407 needed
= mcache_alloc_ext(m_cache(MC_MBUF
), &mp_list
, needed
, wait
);
2409 ASSERT(mp_list
== NULL
);
2413 /* allocate clusters */
2414 if (class == MC_MBUF_CL
) {
2416 } else if (class == MC_MBUF_BIGCL
) {
2417 cl_class
= MC_BIGCL
;
2419 VERIFY(class == MC_MBUF_16KCL
);
2420 cl_class
= MC_16KCL
;
2422 needed
= mcache_alloc_ext(m_cache(cl_class
), &clp_list
, needed
, wait
);
2424 ASSERT(clp_list
== NULL
);
2428 needed
= mcache_alloc_ext(ref_cache
, &ref_list
, needed
, wait
);
2430 ASSERT(ref_list
== NULL
);
2435 * By this time "needed" is MIN(mbuf, cluster, ref). Any left
2436 * overs will get freed accordingly before we return to caller.
2438 for (cnum
= 0; cnum
< needed
; cnum
++) {
2441 m
= ms
= (struct mbuf
*)mp_list
;
2442 mp_list
= mp_list
->obj_next
;
2445 clp_list
= clp_list
->obj_next
;
2446 ((mcache_obj_t
*)cl
)->obj_next
= NULL
;
2448 rfa
= (struct ext_ref
*)ref_list
;
2449 ref_list
= ref_list
->obj_next
;
2450 ((mcache_obj_t
*)(void *)rfa
)->obj_next
= NULL
;
2453 * If auditing is enabled, construct the shadow mbuf
2454 * in the audit structure instead of in the actual one.
2455 * mbuf_cslab_audit() will take care of restoring the
2456 * contents after the integrity check.
2458 if (mclaudit
!= NULL
) {
2459 mcache_audit_t
*mca
, *cl_mca
;
2461 lck_mtx_lock(mbuf_mlock
);
2462 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2463 ms
= MCA_SAVED_MBUF_PTR(mca
);
2464 cl_mca
= mcl_audit_buf2mca(MC_CL
, (mcache_obj_t
*)cl
);
2467 * Pair them up. Note that this is done at the time
2468 * the mbuf+cluster objects are constructed. This
2469 * information should be treated as "best effort"
2470 * debugging hint since more than one mbufs can refer
2471 * to a cluster. In that case, the cluster might not
2472 * be freed along with the mbuf it was paired with.
2474 mca
->mca_uptr
= cl_mca
;
2475 cl_mca
->mca_uptr
= mca
;
2477 ASSERT(mca
->mca_uflags
& MB_SCVALID
);
2478 ASSERT(!(cl_mca
->mca_uflags
& MB_SCVALID
));
2479 lck_mtx_unlock(mbuf_mlock
);
2481 /* Technically, they are in the freelist */
2485 mcache_set_pattern(MCACHE_FREE_PATTERN
, m
,
2486 m_maxsize(MC_MBUF
));
2488 if (class == MC_MBUF_CL
)
2489 size
= m_maxsize(MC_CL
);
2490 else if (class == MC_MBUF_BIGCL
)
2491 size
= m_maxsize(MC_BIGCL
);
2493 size
= m_maxsize(MC_16KCL
);
2495 mcache_set_pattern(MCACHE_FREE_PATTERN
, cl
,
2500 MBUF_INIT(ms
, 0, MT_FREE
);
2501 if (class == MC_MBUF_16KCL
) {
2502 MBUF_16KCL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2503 } else if (class == MC_MBUF_BIGCL
) {
2504 MBUF_BIGCL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2506 MBUF_CL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2508 VERIFY(ms
->m_flags
== M_EXT
);
2509 VERIFY(MEXT_RFA(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2511 *list
= (mcache_obj_t
*)m
;
2512 (*list
)->obj_next
= NULL
;
2513 list
= *plist
= &(*list
)->obj_next
;
2518 * Free up what's left of the above.
2520 if (mp_list
!= NULL
)
2521 mcache_free_ext(m_cache(MC_MBUF
), mp_list
);
2522 if (clp_list
!= NULL
)
2523 mcache_free_ext(m_cache(cl_class
), clp_list
);
2524 if (ref_list
!= NULL
)
2525 mcache_free_ext(ref_cache
, ref_list
);
2527 lck_mtx_lock(mbuf_mlock
);
2528 if (num
> 0 || cnum
> 0) {
2529 m_total(class) += cnum
;
2530 VERIFY(m_total(class) <= m_maxlimit(class));
2531 m_alloc_cnt(class) += num
+ cnum
;
2533 if ((num
+ cnum
) < want
)
2534 m_fail_cnt(class) += (want
- (num
+ cnum
));
2535 lck_mtx_unlock(mbuf_mlock
);
2537 return (num
+ cnum
);
2541 * Common de-allocator for composite objects called by the CPU cache
2542 * layer when one or more elements need to be returned to the appropriate
2546 mbuf_cslab_free(void *arg
, mcache_obj_t
*list
, int purged
)
2548 mbuf_class_t
class = (mbuf_class_t
)arg
;
2552 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2554 lck_mtx_lock(mbuf_mlock
);
2556 num
= cslab_free(class, list
, purged
);
2557 m_free_cnt(class) += num
;
2559 if ((w
= mb_waiters
) > 0)
2562 lck_mtx_unlock(mbuf_mlock
);
2565 wakeup(mb_waitchan
);
2569 * Common auditor for composite objects called by the CPU cache layer
2570 * during an allocation or free request. For the former, this is called
2571 * after the objects are obtained from either the bucket or slab layer
2572 * and before they are returned to the caller. For the latter, this is
2573 * called immediately during free and before placing the objects into
2574 * the bucket or slab layer.
2577 mbuf_cslab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
2579 mbuf_class_t
class = (mbuf_class_t
)arg
;
2580 mcache_audit_t
*mca
;
2581 struct mbuf
*m
, *ms
;
2582 mcl_slab_t
*clsp
, *nsp
;
2586 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2588 while ((m
= ms
= (struct mbuf
*)list
) != NULL
) {
2589 lck_mtx_lock(mbuf_mlock
);
2590 /* Do the mbuf sanity checks and record its transaction */
2591 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2592 mcl_audit_mbuf(mca
, m
, TRUE
, alloc
);
2594 mcache_buffer_log(mca
, m
, m_cache(class), &mb_start
);
2597 mca
->mca_uflags
|= MB_COMP_INUSE
;
2599 mca
->mca_uflags
&= ~MB_COMP_INUSE
;
2602 * Use the shadow mbuf in the audit structure if we are
2603 * freeing, since the contents of the actual mbuf has been
2604 * pattern-filled by the above call to mcl_audit_mbuf().
2606 if (!alloc
&& mclverify
)
2607 ms
= MCA_SAVED_MBUF_PTR(mca
);
2609 /* Do the cluster sanity checks and record its transaction */
2610 cl
= ms
->m_ext
.ext_buf
;
2611 clsp
= slab_get(cl
);
2612 VERIFY(ms
->m_flags
== M_EXT
&& cl
!= NULL
);
2613 VERIFY(MEXT_RFA(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2614 if (class == MC_MBUF_CL
)
2615 VERIFY(clsp
->sl_refcnt
>= 1 &&
2616 clsp
->sl_refcnt
<= NCLPBG
);
2618 VERIFY(clsp
->sl_refcnt
== 1);
2620 if (class == MC_MBUF_16KCL
) {
2622 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2624 /* Next slab must already be present */
2625 VERIFY(nsp
!= NULL
);
2626 VERIFY(nsp
->sl_refcnt
== 1);
2630 mca
= mcl_audit_buf2mca(MC_CL
, cl
);
2631 if (class == MC_MBUF_CL
)
2632 size
= m_maxsize(MC_CL
);
2633 else if (class == MC_MBUF_BIGCL
)
2634 size
= m_maxsize(MC_BIGCL
);
2636 size
= m_maxsize(MC_16KCL
);
2637 mcl_audit_cluster(mca
, cl
, size
, alloc
, FALSE
);
2639 mcache_buffer_log(mca
, cl
, m_cache(class), &mb_start
);
2642 mca
->mca_uflags
|= MB_COMP_INUSE
;
2644 mca
->mca_uflags
&= ~MB_COMP_INUSE
;
2645 lck_mtx_unlock(mbuf_mlock
);
2647 list
= list
->obj_next
;
2652 * Allocate some number of mbuf clusters and place on cluster freelist.
2655 m_clalloc(const u_int32_t num
, const int wait
, const u_int32_t bufsize
)
2659 int numpages
= 0, large_buffer
= (bufsize
== m_maxsize(MC_16KCL
));
2660 vm_offset_t page
= 0;
2661 mcache_audit_t
*mca_list
= NULL
;
2662 mcache_obj_t
*con_list
= NULL
;
2665 VERIFY(bufsize
== m_maxsize(MC_BIGCL
) ||
2666 bufsize
== m_maxsize(MC_16KCL
));
2668 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2671 * Multiple threads may attempt to populate the cluster map one
2672 * after another. Since we drop the lock below prior to acquiring
2673 * the physical page(s), our view of the cluster map may no longer
2674 * be accurate, and we could end up over-committing the pages beyond
2675 * the maximum allowed for each class. To prevent it, this entire
2676 * operation (including the page mapping) is serialized.
2678 while (mb_clalloc_busy
) {
2679 mb_clalloc_waiters
++;
2680 (void) msleep(mb_clalloc_waitchan
, mbuf_mlock
,
2681 (PZERO
-1), "m_clalloc", NULL
);
2682 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2685 /* We are busy now; tell everyone else to go away */
2686 mb_clalloc_busy
= TRUE
;
2689 * Honor the caller's wish to block or not block. We have a way
2690 * to grow the pool asynchronously using the mbuf worker thread.
2692 i
= m_howmany(num
, bufsize
);
2693 if (i
== 0 || (wait
& M_DONTWAIT
))
2696 lck_mtx_unlock(mbuf_mlock
);
2698 size
= round_page(i
* bufsize
);
2699 page
= kmem_mb_alloc(mb_map
, size
, large_buffer
);
2702 * If we did ask for "n" 16KB physically contiguous chunks
2703 * and didn't get them, then please try again without this
2706 if (large_buffer
&& page
== 0)
2707 page
= kmem_mb_alloc(mb_map
, size
, 0);
2710 if (bufsize
== m_maxsize(MC_BIGCL
)) {
2711 /* Try for 1 page if failed, only 4KB request */
2713 page
= kmem_mb_alloc(mb_map
, size
, 0);
2717 lck_mtx_lock(mbuf_mlock
);
2722 VERIFY(IS_P2ALIGNED(page
, NBPG
));
2723 numpages
= size
/ NBPG
;
2725 /* If auditing is enabled, allocate the audit structures now */
2726 if (mclaudit
!= NULL
) {
2730 * Yes, I realize this is a waste of memory for clusters
2731 * that never get transformed into mbufs, as we may end
2732 * up with NMBPBG-1 unused audit structures per cluster.
2733 * But doing so tremendously simplifies the allocation
2734 * strategy, since at this point we are not holding the
2735 * mbuf lock and the caller is okay to be blocked.
2737 if (bufsize
== m_maxsize(MC_BIGCL
)) {
2738 needed
= numpages
* NMBPBG
;
2740 i
= mcache_alloc_ext(mcl_audit_con_cache
,
2741 &con_list
, needed
, MCR_SLEEP
);
2743 VERIFY(con_list
!= NULL
&& i
== needed
);
2745 needed
= numpages
/ NSLABSP16KB
;
2748 i
= mcache_alloc_ext(mcache_audit_cache
,
2749 (mcache_obj_t
**)&mca_list
, needed
, MCR_SLEEP
);
2751 VERIFY(mca_list
!= NULL
&& i
== needed
);
2754 lck_mtx_lock(mbuf_mlock
);
2756 for (i
= 0; i
< numpages
; i
++, page
+= NBPG
) {
2757 ppnum_t offset
= ((char *)page
- (char *)mbutl
) / NBPG
;
2758 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, page
);
2761 * If there is a mapper the appropriate I/O page is returned;
2762 * zero out the page to discard its past contents to prevent
2763 * exposing leftover kernel memory.
2765 VERIFY(offset
< mcl_pages
);
2766 if (mcl_paddr_base
!= 0) {
2767 bzero((void *)(uintptr_t) page
, page_size
);
2768 new_page
= IOMapperInsertPage(mcl_paddr_base
,
2771 mcl_paddr
[offset
] = new_page
;
2773 /* Pattern-fill this fresh page */
2775 mcache_set_pattern(MCACHE_FREE_PATTERN
,
2776 (caddr_t
)page
, NBPG
);
2778 if (bufsize
== m_maxsize(MC_BIGCL
)) {
2779 union mbigcluster
*mbc
= (union mbigcluster
*)page
;
2781 /* One for the entire page */
2783 if (mclaudit
!= NULL
) {
2784 mcl_audit_init(mbc
, &mca_list
, &con_list
,
2785 AUDIT_CONTENTS_SIZE
, NMBPBG
);
2787 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_flags
== 0);
2788 slab_init(sp
, MC_BIGCL
, SLF_MAPPED
,
2789 mbc
, mbc
, bufsize
, 0, 1);
2791 /* Insert this slab */
2792 slab_insert(sp
, MC_BIGCL
);
2794 /* Update stats now since slab_get() drops the lock */
2795 mbstat
.m_bigclfree
= ++m_infree(MC_BIGCL
) +
2796 m_infree(MC_MBUF_BIGCL
);
2797 mbstat
.m_bigclusters
= ++m_total(MC_BIGCL
);
2798 VERIFY(m_total(MC_BIGCL
) <= m_maxlimit(MC_BIGCL
));
2799 } else if ((i
% NSLABSP16KB
) == 0) {
2800 union m16kcluster
*m16kcl
= (union m16kcluster
*)page
;
2805 /* One for the entire 16KB */
2806 sp
= slab_get(m16kcl
);
2807 if (mclaudit
!= NULL
)
2808 mcl_audit_init(m16kcl
, &mca_list
, NULL
, 0, 1);
2810 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_flags
== 0);
2811 slab_init(sp
, MC_16KCL
, SLF_MAPPED
,
2812 m16kcl
, m16kcl
, bufsize
, 0, 1);
2815 * 2nd-Nth page's slab is part of the first one,
2816 * where N is NSLABSP16KB.
2818 for (k
= 1; k
< NSLABSP16KB
; k
++) {
2819 nsp
= slab_get(((union mbigcluster
*)page
) + k
);
2820 VERIFY(nsp
->sl_refcnt
== 0 &&
2821 nsp
->sl_flags
== 0);
2822 slab_init(nsp
, MC_16KCL
,
2823 SLF_MAPPED
| SLF_PARTIAL
,
2824 m16kcl
, NULL
, 0, 0, 0);
2827 /* Insert this slab */
2828 slab_insert(sp
, MC_16KCL
);
2830 /* Update stats now since slab_get() drops the lock */
2831 m_infree(MC_16KCL
)++;
2832 m_total(MC_16KCL
)++;
2833 VERIFY(m_total(MC_16KCL
) <= m_maxlimit(MC_16KCL
));
2836 VERIFY(mca_list
== NULL
&& con_list
== NULL
);
2838 /* We're done; let others enter */
2839 mb_clalloc_busy
= FALSE
;
2840 if (mb_clalloc_waiters
> 0) {
2841 mb_clalloc_waiters
= 0;
2842 wakeup(mb_clalloc_waitchan
);
2845 if (bufsize
== m_maxsize(MC_BIGCL
))
2848 VERIFY(bufsize
== m_maxsize(MC_16KCL
));
2849 return (numpages
/ NSLABSP16KB
);
2852 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2854 /* We're done; let others enter */
2855 mb_clalloc_busy
= FALSE
;
2856 if (mb_clalloc_waiters
> 0) {
2857 mb_clalloc_waiters
= 0;
2858 wakeup(mb_clalloc_waitchan
);
2862 * When non-blocking we kick a thread if we have to grow the
2863 * pool or if the number of free clusters is less than requested.
2865 if (bufsize
== m_maxsize(MC_BIGCL
)) {
2868 * Remember total number of 4KB clusters needed
2871 i
+= m_total(MC_BIGCL
);
2872 if (i
> mbuf_expand_big
) {
2873 mbuf_expand_big
= i
;
2874 if (mbuf_worker_ready
)
2875 wakeup((caddr_t
)&mbuf_worker_run
);
2879 if (m_infree(MC_BIGCL
) >= num
)
2884 * Remember total number of 16KB clusters needed
2887 i
+= m_total(MC_16KCL
);
2888 if (i
> mbuf_expand_16k
) {
2889 mbuf_expand_16k
= i
;
2890 if (mbuf_worker_ready
)
2891 wakeup((caddr_t
)&mbuf_worker_run
);
2895 if (m_infree(MC_16KCL
) >= num
)
2902 * Populate the global freelist of the corresponding buffer class.
2905 freelist_populate(mbuf_class_t
class, unsigned int num
, int wait
)
2907 mcache_obj_t
*o
= NULL
;
2908 int i
, numpages
= 0, count
;
2910 VERIFY(class == MC_MBUF
|| class == MC_CL
|| class == MC_BIGCL
||
2913 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2919 numpages
= (num
* m_size(class) + NBPG
- 1) / NBPG
;
2920 i
= m_clalloc(numpages
, wait
, m_maxsize(MC_BIGCL
));
2922 /* Respect the 4KB clusters minimum limit */
2923 if (m_total(MC_BIGCL
) == m_maxlimit(MC_BIGCL
) &&
2924 m_infree(MC_BIGCL
) <= m_minlimit(MC_BIGCL
)) {
2925 if (class != MC_BIGCL
|| (wait
& MCR_COMP
))
2928 if (class == MC_BIGCL
)
2933 return (m_clalloc(num
, wait
, m_maxsize(class)) != 0);
2941 VERIFY(class == MC_MBUF
|| class == MC_CL
);
2943 /* how many objects will we cut the page into? */
2944 int numobj
= (class == MC_MBUF
? NMBPBG
: NCLPBG
);
2946 for (count
= 0; count
< numpages
; count
++) {
2948 /* respect totals, minlimit, maxlimit */
2949 if (m_total(MC_BIGCL
) <= m_minlimit(MC_BIGCL
) ||
2950 m_total(class) >= m_maxlimit(class))
2953 if ((o
= slab_alloc(MC_BIGCL
, wait
)) == NULL
)
2956 struct mbuf
*m
= (struct mbuf
*)o
;
2957 union mcluster
*c
= (union mcluster
*)o
;
2958 mcl_slab_t
*sp
= slab_get(o
);
2959 mcache_audit_t
*mca
= NULL
;
2961 VERIFY(slab_is_detached(sp
) &&
2962 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
2965 * Make sure that the cluster is unmolested
2969 mca
= mcl_audit_buf2mca(MC_BIGCL
, o
);
2970 mcache_audit_free_verify(mca
, o
, 0,
2971 m_maxsize(MC_BIGCL
));
2974 /* Reinitialize it as an mbuf or 2K slab */
2975 slab_init(sp
, class, sp
->sl_flags
,
2976 sp
->sl_base
, NULL
, sp
->sl_len
, 0, numobj
);
2978 VERIFY(o
== (mcache_obj_t
*)sp
->sl_base
);
2979 VERIFY(sp
->sl_head
== NULL
);
2981 VERIFY(m_total(MC_BIGCL
) > 0);
2982 m_total(MC_BIGCL
)--;
2983 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
2985 m_total(class) += numobj
;
2986 m_infree(class) += numobj
;
2988 VERIFY(m_total(MC_BIGCL
) >= m_minlimit(MC_BIGCL
));
2989 VERIFY(m_total(class) <= m_maxlimit(class));
2992 if (class == MC_MBUF
) {
2993 mbstat
.m_mbufs
= m_total(MC_MBUF
);
2994 mtype_stat_add(MT_FREE
, NMBPBG
);
2997 * If auditing is enabled, construct the
2998 * shadow mbuf in the audit structure
2999 * instead of the actual one.
3000 * mbuf_slab_audit() will take care of
3001 * restoring the contents after the
3004 if (mclaudit
!= NULL
) {
3006 mca
= mcl_audit_buf2mca(MC_MBUF
,
3008 ms
= MCA_SAVED_MBUF_PTR(mca
);
3009 ms
->m_type
= MT_FREE
;
3011 m
->m_type
= MT_FREE
;
3013 m
->m_next
= sp
->sl_head
;
3014 sp
->sl_head
= (void *)m
++;
3016 } else { /* MC_CL */
3018 m_infree(MC_CL
) + m_infree(MC_MBUF_CL
);
3019 mbstat
.m_clusters
= m_total(MC_CL
);
3021 c
->mcl_next
= sp
->sl_head
;
3022 sp
->sl_head
= (void *)c
++;
3026 /* Insert into the mbuf or 2k slab list */
3027 slab_insert(sp
, class);
3029 if ((i
= mb_waiters
) > 0)
3032 wakeup(mb_waitchan
);
3034 return (count
!= 0);
3038 * For each class, initialize the freelist to hold m_minlimit() objects.
3041 freelist_init(mbuf_class_t
class)
3043 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3045 VERIFY(class == MC_CL
|| class == MC_BIGCL
);
3046 VERIFY(m_total(class) == 0);
3047 VERIFY(m_minlimit(class) > 0);
3049 while (m_total(class) < m_minlimit(class))
3050 (void) freelist_populate(class, m_minlimit(class), M_WAIT
);
3052 VERIFY(m_total(class) >= m_minlimit(class));
3056 * (Inaccurately) check if it might be worth a trip back to the
3057 * mcache layer due the availability of objects there. We'll
3058 * end up back here if there's nothing up there.
3061 mbuf_cached_above(mbuf_class_t
class, int wait
)
3065 if (wait
& MCR_COMP
)
3066 return (!mcache_bkt_isempty(m_cache(MC_MBUF_CL
)) ||
3067 !mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL
)));
3071 if (wait
& MCR_COMP
)
3072 return (!mcache_bkt_isempty(m_cache(MC_MBUF_CL
)));
3076 if (wait
& MCR_COMP
)
3077 return (!mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL
)));
3081 if (wait
& MCR_COMP
)
3082 return (!mcache_bkt_isempty(m_cache(MC_MBUF_16KCL
)));
3095 return (!mcache_bkt_isempty(m_cache(class)));
3099 * If possible, convert constructed objects to raw ones.
3102 mbuf_steal(mbuf_class_t
class, unsigned int num
)
3104 mcache_obj_t
*top
= NULL
;
3105 mcache_obj_t
**list
= &top
;
3106 unsigned int tot
= 0;
3108 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3120 /* Get the required number of constructed objects if possible */
3121 if (m_infree(class) > m_minlimit(class)) {
3122 tot
= cslab_alloc(class, &list
,
3123 MIN(num
, m_infree(class)));
3126 /* And destroy them to get back the raw objects */
3128 (void) cslab_free(class, top
, 1);
3136 return (tot
== num
);
3140 m_reclaim(mbuf_class_t
class, unsigned int num
, boolean_t comp
)
3144 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3146 VERIFY(m_total(MC_CL
) <= m_maxlimit(MC_CL
));
3147 VERIFY(m_total(MC_BIGCL
) <= m_maxlimit(MC_BIGCL
));
3148 VERIFY(m_total(MC_16KCL
) <= m_maxlimit(MC_16KCL
));
3151 * This logic can be made smarter; for now, simply mark
3152 * all other related classes as potential victims.
3156 m_wantpurge(MC_CL
)++;
3157 m_wantpurge(MC_BIGCL
)++;
3158 m_wantpurge(MC_MBUF_CL
)++;
3159 m_wantpurge(MC_MBUF_BIGCL
)++;
3163 m_wantpurge(MC_MBUF
)++;
3164 m_wantpurge(MC_BIGCL
)++;
3165 m_wantpurge(MC_MBUF_BIGCL
)++;
3167 m_wantpurge(MC_MBUF_CL
)++;
3171 m_wantpurge(MC_MBUF
)++;
3172 m_wantpurge(MC_CL
)++;
3173 m_wantpurge(MC_MBUF_CL
)++;
3175 m_wantpurge(MC_MBUF_BIGCL
)++;
3180 m_wantpurge(MC_MBUF_16KCL
)++;
3189 * Run through each marked class and check if we really need to
3190 * purge (and therefore temporarily disable) the per-CPU caches
3191 * layer used by the class. If so, remember the classes since
3192 * we are going to drop the lock below prior to purging.
3194 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
3195 if (m_wantpurge(m
) > 0) {
3198 * Try hard to steal the required number of objects
3199 * from the freelist of other mbuf classes. Only
3200 * purge and disable the per-CPU caches layer when
3201 * we don't have enough; it's the last resort.
3203 if (!mbuf_steal(m
, num
))
3208 lck_mtx_unlock(mbuf_mlock
);
3211 /* signal the domains to drain */
3212 net_drain_domains();
3214 /* Sigh; we have no other choices but to ask mcache to purge */
3215 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
3216 if ((bmap
& (1 << m
)) &&
3217 mcache_purge_cache(m_cache(m
))) {
3218 lck_mtx_lock(mbuf_mlock
);
3221 lck_mtx_unlock(mbuf_mlock
);
3226 * Request mcache to reap extra elements from all of its caches;
3227 * note that all reaps are serialized and happen only at a fixed
3232 lck_mtx_lock(mbuf_mlock
);
3235 static inline struct mbuf
*
3236 m_get_common(int wait
, short type
, int hdr
)
3239 int mcflags
= MSLEEPF(wait
);
3241 /* Is this due to a non-blocking retry? If so, then try harder */
3242 if (mcflags
& MCR_NOSLEEP
)
3243 mcflags
|= MCR_TRYHARD
;
3245 m
= mcache_alloc(m_cache(MC_MBUF
), mcflags
);
3247 MBUF_INIT(m
, hdr
, type
);
3248 mtype_stat_inc(type
);
3249 mtype_stat_dec(MT_FREE
);
3251 if (hdr
&& mac_init_mbuf(m
, wait
) != 0) {
3255 #endif /* MAC_NET */
3261 * Space allocation routines; these are also available as macros
3262 * for critical paths.
3264 #define _M_GET(wait, type) m_get_common(wait, type, 0)
3265 #define _M_GETHDR(wait, type) m_get_common(wait, type, 1)
3266 #define _M_RETRY(wait, type) _M_GET(wait, type)
3267 #define _M_RETRYHDR(wait, type) _M_GETHDR(wait, type)
3268 #define _MGET(m, how, type) ((m) = _M_GET(how, type))
3269 #define _MGETHDR(m, how, type) ((m) = _M_GETHDR(how, type))
3272 m_get(int wait
, int type
)
3274 return (_M_GET(wait
, type
));
3278 m_gethdr(int wait
, int type
)
3280 return (_M_GETHDR(wait
, type
));
3284 m_retry(int wait
, int type
)
3286 return (_M_RETRY(wait
, type
));
3290 m_retryhdr(int wait
, int type
)
3292 return (_M_RETRYHDR(wait
, type
));
3296 m_getclr(int wait
, int type
)
3300 _MGET(m
, wait
, type
);
3302 bzero(MTOD(m
, caddr_t
), MLEN
);
3307 m_free(struct mbuf
*m
)
3309 struct mbuf
*n
= m
->m_next
;
3311 if (m
->m_type
== MT_FREE
)
3312 panic("m_free: freeing an already freed mbuf");
3314 if (m
->m_flags
& M_PKTHDR
) {
3315 /* Check for scratch area overflow */
3316 m_redzone_verify(m
);
3317 /* Free the aux data and tags if there is any */
3318 m_tag_delete_chain(m
, NULL
);
3321 if (m
->m_flags
& M_EXT
) {
3323 u_int32_t composite
;
3325 refcnt
= m_decref(m
);
3326 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
3327 if (refcnt
== 0 && !composite
) {
3328 if (m
->m_ext
.ext_free
== NULL
) {
3329 mcache_free(m_cache(MC_CL
), m
->m_ext
.ext_buf
);
3330 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3331 mcache_free(m_cache(MC_BIGCL
),
3333 } else if (m
->m_ext
.ext_free
== m_16kfree
) {
3334 mcache_free(m_cache(MC_16KCL
),
3337 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
3338 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
3340 mcache_free(ref_cache
, MEXT_RFA(m
));
3342 } else if (refcnt
== 0 && composite
) {
3343 VERIFY(m
->m_type
!= MT_FREE
);
3345 mtype_stat_dec(m
->m_type
);
3346 mtype_stat_inc(MT_FREE
);
3348 m
->m_type
= MT_FREE
;
3351 m
->m_next
= m
->m_nextpkt
= NULL
;
3353 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
3355 /* "Free" into the intermediate cache */
3356 if (m
->m_ext
.ext_free
== NULL
) {
3357 mcache_free(m_cache(MC_MBUF_CL
), m
);
3358 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3359 mcache_free(m_cache(MC_MBUF_BIGCL
), m
);
3361 VERIFY(m
->m_ext
.ext_free
== m_16kfree
);
3362 mcache_free(m_cache(MC_MBUF_16KCL
), m
);
3368 if (m
->m_type
!= MT_FREE
) {
3369 mtype_stat_dec(m
->m_type
);
3370 mtype_stat_inc(MT_FREE
);
3373 m
->m_type
= MT_FREE
;
3374 m
->m_flags
= m
->m_len
= 0;
3375 m
->m_next
= m
->m_nextpkt
= NULL
;
3377 mcache_free(m_cache(MC_MBUF
), m
);
3382 __private_extern__
struct mbuf
*
3383 m_clattach(struct mbuf
*m
, int type
, caddr_t extbuf
,
3384 void (*extfree
)(caddr_t
, u_int
, caddr_t
), u_int extsize
, caddr_t extarg
,
3387 struct ext_ref
*rfa
= NULL
;
3389 if (m
== NULL
&& (m
= _M_GETHDR(wait
, type
)) == NULL
)
3392 if (m
->m_flags
& M_EXT
) {
3394 u_int32_t composite
;
3396 refcnt
= m_decref(m
);
3397 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
3398 if (refcnt
== 0 && !composite
) {
3399 if (m
->m_ext
.ext_free
== NULL
) {
3400 mcache_free(m_cache(MC_CL
), m
->m_ext
.ext_buf
);
3401 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3402 mcache_free(m_cache(MC_BIGCL
),
3404 } else if (m
->m_ext
.ext_free
== m_16kfree
) {
3405 mcache_free(m_cache(MC_16KCL
),
3408 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
3409 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
3411 /* Re-use the reference structure */
3413 } else if (refcnt
== 0 && composite
) {
3414 VERIFY(m
->m_type
!= MT_FREE
);
3416 mtype_stat_dec(m
->m_type
);
3417 mtype_stat_inc(MT_FREE
);
3419 m
->m_type
= MT_FREE
;
3422 m
->m_next
= m
->m_nextpkt
= NULL
;
3424 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
3426 /* "Free" into the intermediate cache */
3427 if (m
->m_ext
.ext_free
== NULL
) {
3428 mcache_free(m_cache(MC_MBUF_CL
), m
);
3429 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3430 mcache_free(m_cache(MC_MBUF_BIGCL
), m
);
3432 VERIFY(m
->m_ext
.ext_free
== m_16kfree
);
3433 mcache_free(m_cache(MC_MBUF_16KCL
), m
);
3436 * Allocate a new mbuf, since we didn't divorce
3437 * the composite mbuf + cluster pair above.
3439 if ((m
= _M_GETHDR(wait
, type
)) == NULL
)
3445 (rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
) {
3450 MEXT_INIT(m
, extbuf
, extsize
, extfree
, extarg
, rfa
, 1, 0);
3456 * Perform `fast' allocation mbuf clusters from a cache of recently-freed
3457 * clusters. (If the cache is empty, new clusters are allocated en-masse.)
3460 m_getcl(int wait
, int type
, int flags
)
3463 int mcflags
= MSLEEPF(wait
);
3464 int hdr
= (flags
& M_PKTHDR
);
3466 /* Is this due to a non-blocking retry? If so, then try harder */
3467 if (mcflags
& MCR_NOSLEEP
)
3468 mcflags
|= MCR_TRYHARD
;
3470 m
= mcache_alloc(m_cache(MC_MBUF_CL
), mcflags
);
3473 struct ext_ref
*rfa
;
3476 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
3477 cl
= m
->m_ext
.ext_buf
;
3480 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
3481 VERIFY(MBUF_IS_COMPOSITE(m
) && m
->m_ext
.ext_free
== NULL
);
3483 flag
= MEXT_FLAGS(m
);
3485 MBUF_INIT(m
, hdr
, type
);
3486 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
3488 mtype_stat_inc(type
);
3489 mtype_stat_dec(MT_FREE
);
3491 if (hdr
&& mac_init_mbuf(m
, wait
) != 0) {
3495 #endif /* MAC_NET */
3500 /* m_mclget() add an mbuf cluster to a normal mbuf */
3502 m_mclget(struct mbuf
*m
, int wait
)
3504 struct ext_ref
*rfa
;
3506 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3509 m
->m_ext
.ext_buf
= m_mclalloc(wait
);
3510 if (m
->m_ext
.ext_buf
!= NULL
) {
3511 MBUF_CL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3513 mcache_free(ref_cache
, rfa
);
3518 /* Allocate an mbuf cluster */
3520 m_mclalloc(int wait
)
3522 int mcflags
= MSLEEPF(wait
);
3524 /* Is this due to a non-blocking retry? If so, then try harder */
3525 if (mcflags
& MCR_NOSLEEP
)
3526 mcflags
|= MCR_TRYHARD
;
3528 return (mcache_alloc(m_cache(MC_CL
), mcflags
));
3531 /* Free an mbuf cluster */
3533 m_mclfree(caddr_t p
)
3535 mcache_free(m_cache(MC_CL
), p
);
3539 * mcl_hasreference() checks if a cluster of an mbuf is referenced by
3540 * another mbuf; see comments in m_incref() regarding EXTF_READONLY.
3543 m_mclhasreference(struct mbuf
*m
)
3545 if (!(m
->m_flags
& M_EXT
))
3548 ASSERT(MEXT_RFA(m
) != NULL
);
3550 return ((MEXT_FLAGS(m
) & EXTF_READONLY
) ? 1 : 0);
3553 __private_extern__ caddr_t
3554 m_bigalloc(int wait
)
3556 int mcflags
= MSLEEPF(wait
);
3558 /* Is this due to a non-blocking retry? If so, then try harder */
3559 if (mcflags
& MCR_NOSLEEP
)
3560 mcflags
|= MCR_TRYHARD
;
3562 return (mcache_alloc(m_cache(MC_BIGCL
), mcflags
));
3565 __private_extern__
void
3566 m_bigfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
3568 mcache_free(m_cache(MC_BIGCL
), p
);
3571 /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */
3572 __private_extern__
struct mbuf
*
3573 m_mbigget(struct mbuf
*m
, int wait
)
3575 struct ext_ref
*rfa
;
3577 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3580 m
->m_ext
.ext_buf
= m_bigalloc(wait
);
3581 if (m
->m_ext
.ext_buf
!= NULL
) {
3582 MBUF_BIGCL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3584 mcache_free(ref_cache
, rfa
);
3589 __private_extern__ caddr_t
3590 m_16kalloc(int wait
)
3592 int mcflags
= MSLEEPF(wait
);
3594 /* Is this due to a non-blocking retry? If so, then try harder */
3595 if (mcflags
& MCR_NOSLEEP
)
3596 mcflags
|= MCR_TRYHARD
;
3598 return (mcache_alloc(m_cache(MC_16KCL
), mcflags
));
3601 __private_extern__
void
3602 m_16kfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
3604 mcache_free(m_cache(MC_16KCL
), p
);
3607 /* m_m16kget() add a 16KB mbuf cluster to a normal mbuf */
3608 __private_extern__
struct mbuf
*
3609 m_m16kget(struct mbuf
*m
, int wait
)
3611 struct ext_ref
*rfa
;
3613 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3616 m
->m_ext
.ext_buf
= m_16kalloc(wait
);
3617 if (m
->m_ext
.ext_buf
!= NULL
) {
3618 MBUF_16KCL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3620 mcache_free(ref_cache
, rfa
);
3626 * "Move" mbuf pkthdr from "from" to "to".
3627 * "from" must have M_PKTHDR set, and "to" must be empty.
3630 m_copy_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
3632 VERIFY(from
->m_flags
& M_PKTHDR
);
3634 /* Check for scratch area overflow */
3635 m_redzone_verify(from
);
3637 if (to
->m_flags
& M_PKTHDR
) {
3638 /* Check for scratch area overflow */
3639 m_redzone_verify(to
);
3640 /* We will be taking over the tags of 'to' */
3641 m_tag_delete_chain(to
, NULL
);
3643 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
3644 m_classifier_init(from
, 0); /* purge classifier info */
3645 m_tag_init(from
, 1); /* purge all tags from src */
3646 m_scratch_init(from
); /* clear src scratch area */
3647 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
3648 if ((to
->m_flags
& M_EXT
) == 0)
3649 to
->m_data
= to
->m_pktdat
;
3650 m_redzone_init(to
); /* setup red zone on dst */
3654 * Duplicate "from"'s mbuf pkthdr in "to".
3655 * "from" must have M_PKTHDR set, and "to" must be empty.
3656 * In particular, this does a deep copy of the packet tags.
3659 m_dup_pkthdr(struct mbuf
*to
, struct mbuf
*from
, int how
)
3661 VERIFY(from
->m_flags
& M_PKTHDR
);
3663 /* Check for scratch area overflow */
3664 m_redzone_verify(from
);
3666 if (to
->m_flags
& M_PKTHDR
) {
3667 /* Check for scratch area overflow */
3668 m_redzone_verify(to
);
3669 /* We will be taking over the tags of 'to' */
3670 m_tag_delete_chain(to
, NULL
);
3672 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
3673 if ((to
->m_flags
& M_EXT
) == 0)
3674 to
->m_data
= to
->m_pktdat
;
3675 to
->m_pkthdr
= from
->m_pkthdr
;
3676 m_redzone_init(to
); /* setup red zone on dst */
3677 m_tag_init(to
, 0); /* preserve dst static tags */
3678 return (m_tag_copy_chain(to
, from
, how
));
3682 m_copy_pftag(struct mbuf
*to
, struct mbuf
*from
)
3684 to
->m_pkthdr
.pf_mtag
= from
->m_pkthdr
.pf_mtag
;
3686 to
->m_pkthdr
.pf_mtag
.pftag_hdr
= NULL
;
3687 to
->m_pkthdr
.pf_mtag
.pftag_flags
&= ~(PF_TAG_HDR_INET
|PF_TAG_HDR_INET6
);
3692 m_classifier_init(struct mbuf
*m
, uint32_t pktf_mask
)
3694 VERIFY(m
->m_flags
& M_PKTHDR
);
3696 m
->m_pkthdr
.pkt_proto
= 0;
3697 m
->m_pkthdr
.pkt_flowsrc
= 0;
3698 m
->m_pkthdr
.pkt_flowid
= 0;
3699 m
->m_pkthdr
.pkt_flags
&= pktf_mask
; /* caller-defined mask */
3700 /* preserve service class and interface info for loopback packets */
3701 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
))
3702 (void) m_set_service_class(m
, MBUF_SC_BE
);
3703 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
3704 m
->m_pkthdr
.pkt_ifainfo
= 0;
3706 m
->m_pkthdr
.pkt_bwseq
= 0;
3707 #endif /* MEASURE_BW */
3711 m_copy_classifier(struct mbuf
*to
, struct mbuf
*from
)
3713 VERIFY(to
->m_flags
& M_PKTHDR
);
3714 VERIFY(from
->m_flags
& M_PKTHDR
);
3716 to
->m_pkthdr
.pkt_proto
= from
->m_pkthdr
.pkt_proto
;
3717 to
->m_pkthdr
.pkt_flowsrc
= from
->m_pkthdr
.pkt_flowsrc
;
3718 to
->m_pkthdr
.pkt_flowid
= from
->m_pkthdr
.pkt_flowid
;
3719 to
->m_pkthdr
.pkt_flags
= from
->m_pkthdr
.pkt_flags
;
3720 (void) m_set_service_class(to
, from
->m_pkthdr
.pkt_svc
);
3721 to
->m_pkthdr
.pkt_ifainfo
= from
->m_pkthdr
.pkt_ifainfo
;
3722 to
->m_pkthdr
.ipsec_policy
= from
->m_pkthdr
.ipsec_policy
;
3724 to
->m_pkthdr
.pkt_bwseq
= from
->m_pkthdr
.pkt_bwseq
;
3725 #endif /* MEASURE_BW */
3729 * Return a list of mbuf hdrs that point to clusters. Try for num_needed;
3730 * if wantall is not set, return whatever number were available. Set up the
3731 * first num_with_pkthdrs with mbuf hdrs configured as packet headers; these
3732 * are chained on the m_nextpkt field. Any packets requested beyond this
3733 * are chained onto the last packet header's m_next field. The size of
3734 * the cluster is controlled by the parameter bufsize.
3736 __private_extern__
struct mbuf
*
3737 m_getpackets_internal(unsigned int *num_needed
, int num_with_pkthdrs
,
3738 int wait
, int wantall
, size_t bufsize
)
3741 struct mbuf
**np
, *top
;
3742 unsigned int pnum
, needed
= *num_needed
;
3743 mcache_obj_t
*mp_list
= NULL
;
3744 int mcflags
= MSLEEPF(wait
);
3746 struct ext_ref
*rfa
;
3750 ASSERT(bufsize
== m_maxsize(MC_CL
) ||
3751 bufsize
== m_maxsize(MC_BIGCL
) ||
3752 bufsize
== m_maxsize(MC_16KCL
));
3755 * Caller must first check for njcl because this
3756 * routine is internal and not exposed/used via KPI.
3758 VERIFY(bufsize
!= m_maxsize(MC_16KCL
) || njcl
> 0);
3765 * The caller doesn't want all the requested buffers; only some.
3766 * Try hard to get what we can, but don't block. This effectively
3767 * overrides MCR_SLEEP, since this thread will not go to sleep
3768 * if we can't get all the buffers.
3770 if (!wantall
|| (mcflags
& MCR_NOSLEEP
))
3771 mcflags
|= MCR_TRYHARD
;
3773 /* Allocate the composite mbuf + cluster elements from the cache */
3774 if (bufsize
== m_maxsize(MC_CL
))
3775 cp
= m_cache(MC_MBUF_CL
);
3776 else if (bufsize
== m_maxsize(MC_BIGCL
))
3777 cp
= m_cache(MC_MBUF_BIGCL
);
3779 cp
= m_cache(MC_MBUF_16KCL
);
3780 needed
= mcache_alloc_ext(cp
, &mp_list
, needed
, mcflags
);
3782 for (pnum
= 0; pnum
< needed
; pnum
++) {
3783 m
= (struct mbuf
*)mp_list
;
3784 mp_list
= mp_list
->obj_next
;
3786 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
3787 cl
= m
->m_ext
.ext_buf
;
3790 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
3791 VERIFY(MBUF_IS_COMPOSITE(m
));
3793 flag
= MEXT_FLAGS(m
);
3795 MBUF_INIT(m
, num_with_pkthdrs
, MT_DATA
);
3796 if (bufsize
== m_maxsize(MC_16KCL
)) {
3797 MBUF_16KCL_INIT(m
, cl
, rfa
, 1, flag
);
3798 } else if (bufsize
== m_maxsize(MC_BIGCL
)) {
3799 MBUF_BIGCL_INIT(m
, cl
, rfa
, 1, flag
);
3801 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
3804 if (num_with_pkthdrs
> 0) {
3807 if (mac_mbuf_label_init(m
, wait
) != 0) {
3811 #endif /* MAC_NET */
3815 if (num_with_pkthdrs
> 0)
3820 ASSERT(pnum
!= *num_needed
|| mp_list
== NULL
);
3821 if (mp_list
!= NULL
)
3822 mcache_free_ext(cp
, mp_list
);
3825 mtype_stat_add(MT_DATA
, pnum
);
3826 mtype_stat_sub(MT_FREE
, pnum
);
3829 if (wantall
&& (pnum
!= *num_needed
)) {
3835 if (pnum
> *num_needed
) {
3836 printf("%s: File a radar related to <rdar://10146739>. \
3837 needed = %u, pnum = %u, num_needed = %u \n",
3838 __func__
, needed
, pnum
, *num_needed
);
3846 * Return list of mbuf linked by m_nextpkt. Try for numlist, and if
3847 * wantall is not set, return whatever number were available. The size of
3848 * each mbuf in the list is controlled by the parameter packetlen. Each
3849 * mbuf of the list may have a chain of mbufs linked by m_next. Each mbuf
3850 * in the chain is called a segment. If maxsegments is not null and the
3851 * value pointed to is not null, this specify the maximum number of segments
3852 * for a chain of mbufs. If maxsegments is zero or the value pointed to
3853 * is zero the caller does not have any restriction on the number of segments.
3854 * The actual number of segments of a mbuf chain is return in the value
3855 * pointed to by maxsegments.
3857 __private_extern__
struct mbuf
*
3858 m_allocpacket_internal(unsigned int *numlist
, size_t packetlen
,
3859 unsigned int *maxsegments
, int wait
, int wantall
, size_t wantsize
)
3861 struct mbuf
**np
, *top
, *first
= NULL
;
3862 size_t bufsize
, r_bufsize
;
3863 unsigned int num
= 0;
3864 unsigned int nsegs
= 0;
3865 unsigned int needed
, resid
;
3866 int mcflags
= MSLEEPF(wait
);
3867 mcache_obj_t
*mp_list
= NULL
, *rmp_list
= NULL
;
3868 mcache_t
*cp
= NULL
, *rcp
= NULL
;
3876 if (wantsize
== 0) {
3877 if (packetlen
<= MINCLSIZE
) {
3878 bufsize
= packetlen
;
3879 } else if (packetlen
> m_maxsize(MC_CL
)) {
3880 /* Use 4KB if jumbo cluster pool isn't available */
3881 if (packetlen
<= m_maxsize(MC_BIGCL
) || njcl
== 0)
3882 bufsize
= m_maxsize(MC_BIGCL
);
3884 bufsize
= m_maxsize(MC_16KCL
);
3886 bufsize
= m_maxsize(MC_CL
);
3888 } else if (wantsize
== m_maxsize(MC_CL
) ||
3889 wantsize
== m_maxsize(MC_BIGCL
) ||
3890 (wantsize
== m_maxsize(MC_16KCL
) && njcl
> 0)) {
3896 if (bufsize
<= MHLEN
) {
3898 } else if (bufsize
<= MINCLSIZE
) {
3899 if (maxsegments
!= NULL
&& *maxsegments
== 1) {
3900 bufsize
= m_maxsize(MC_CL
);
3905 } else if (bufsize
== m_maxsize(MC_16KCL
)) {
3907 nsegs
= ((packetlen
- 1) >> (PGSHIFT
+ 2)) + 1;
3908 } else if (bufsize
== m_maxsize(MC_BIGCL
)) {
3909 nsegs
= ((packetlen
- 1) >> PGSHIFT
) + 1;
3911 nsegs
= ((packetlen
- 1) >> MCLSHIFT
) + 1;
3913 if (maxsegments
!= NULL
) {
3914 if (*maxsegments
&& nsegs
> *maxsegments
) {
3915 *maxsegments
= nsegs
;
3918 *maxsegments
= nsegs
;
3922 * The caller doesn't want all the requested buffers; only some.
3923 * Try hard to get what we can, but don't block. This effectively
3924 * overrides MCR_SLEEP, since this thread will not go to sleep
3925 * if we can't get all the buffers.
3927 if (!wantall
|| (mcflags
& MCR_NOSLEEP
))
3928 mcflags
|= MCR_TRYHARD
;
3931 * Simple case where all elements in the lists/chains are mbufs.
3932 * Unless bufsize is greater than MHLEN, each segment chain is made
3933 * up of exactly 1 mbuf. Otherwise, each segment chain is made up
3934 * of 2 mbufs; the second one is used for the residual data, i.e.
3935 * the remaining data that cannot fit into the first mbuf.
3937 if (bufsize
<= MINCLSIZE
) {
3938 /* Allocate the elements in one shot from the mbuf cache */
3939 ASSERT(bufsize
<= MHLEN
|| nsegs
== 2);
3940 cp
= m_cache(MC_MBUF
);
3941 needed
= mcache_alloc_ext(cp
, &mp_list
,
3942 (*numlist
) * nsegs
, mcflags
);
3945 * The number of elements must be even if we are to use an
3946 * mbuf (instead of a cluster) to store the residual data.
3947 * If we couldn't allocate the requested number of mbufs,
3948 * trim the number down (if it's odd) in order to avoid
3949 * creating a partial segment chain.
3951 if (bufsize
> MHLEN
&& (needed
& 0x1))
3954 while (num
< needed
) {
3957 m
= (struct mbuf
*)mp_list
;
3958 mp_list
= mp_list
->obj_next
;
3961 MBUF_INIT(m
, 1, MT_DATA
);
3963 if (mac_init_mbuf(m
, wait
) != 0) {
3967 #endif /* MAC_NET */
3969 if (bufsize
> MHLEN
) {
3970 /* A second mbuf for this segment chain */
3971 m
->m_next
= (struct mbuf
*)mp_list
;
3972 mp_list
= mp_list
->obj_next
;
3973 ASSERT(m
->m_next
!= NULL
);
3975 MBUF_INIT(m
->m_next
, 0, MT_DATA
);
3981 ASSERT(num
!= *numlist
|| mp_list
== NULL
);
3984 mtype_stat_add(MT_DATA
, num
);
3985 mtype_stat_sub(MT_FREE
, num
);
3989 /* We've got them all; return to caller */
3990 if (num
== *numlist
)
3997 * Complex cases where elements are made up of one or more composite
3998 * mbufs + cluster, depending on packetlen. Each N-segment chain can
3999 * be illustrated as follows:
4001 * [mbuf + cluster 1] [mbuf + cluster 2] ... [mbuf + cluster N]
4003 * Every composite mbuf + cluster element comes from the intermediate
4004 * cache (either MC_MBUF_CL or MC_MBUF_BIGCL). For space efficiency,
4005 * the last composite element will come from the MC_MBUF_CL cache,
4006 * unless the residual data is larger than 2KB where we use the
4007 * big cluster composite cache (MC_MBUF_BIGCL) instead. Residual
4008 * data is defined as extra data beyond the first element that cannot
4009 * fit into the previous element, i.e. there is no residual data if
4010 * the chain only has 1 segment.
4012 r_bufsize
= bufsize
;
4013 resid
= packetlen
> bufsize
? packetlen
% bufsize
: 0;
4015 /* There is residual data; figure out the cluster size */
4016 if (wantsize
== 0 && packetlen
> MINCLSIZE
) {
4018 * Caller didn't request that all of the segments
4019 * in the chain use the same cluster size; use the
4020 * smaller of the cluster sizes.
4022 if (njcl
> 0 && resid
> m_maxsize(MC_BIGCL
))
4023 r_bufsize
= m_maxsize(MC_16KCL
);
4024 else if (resid
> m_maxsize(MC_CL
))
4025 r_bufsize
= m_maxsize(MC_BIGCL
);
4027 r_bufsize
= m_maxsize(MC_CL
);
4029 /* Use the same cluster size as the other segments */
4037 * Attempt to allocate composite mbuf + cluster elements for
4038 * the residual data in each chain; record the number of such
4039 * elements that can be allocated so that we know how many
4040 * segment chains we can afford to create.
4042 if (r_bufsize
<= m_maxsize(MC_CL
))
4043 rcp
= m_cache(MC_MBUF_CL
);
4044 else if (r_bufsize
<= m_maxsize(MC_BIGCL
))
4045 rcp
= m_cache(MC_MBUF_BIGCL
);
4047 rcp
= m_cache(MC_MBUF_16KCL
);
4048 needed
= mcache_alloc_ext(rcp
, &rmp_list
, *numlist
, mcflags
);
4053 /* This is temporarily reduced for calculation */
4059 * Attempt to allocate the rest of the composite mbuf + cluster
4060 * elements for the number of segment chains that we need.
4062 if (bufsize
<= m_maxsize(MC_CL
))
4063 cp
= m_cache(MC_MBUF_CL
);
4064 else if (bufsize
<= m_maxsize(MC_BIGCL
))
4065 cp
= m_cache(MC_MBUF_BIGCL
);
4067 cp
= m_cache(MC_MBUF_16KCL
);
4068 needed
= mcache_alloc_ext(cp
, &mp_list
, needed
* nsegs
, mcflags
);
4070 /* Round it down to avoid creating a partial segment chain */
4071 needed
= (needed
/ nsegs
) * nsegs
;
4077 * We're about to construct the chain(s); take into account
4078 * the number of segments we have created above to hold the
4079 * residual data for each chain, as well as restore the
4080 * original count of segments per chain.
4083 needed
+= needed
/ nsegs
;
4090 struct ext_ref
*rfa
;
4095 if (nsegs
== 1 || (num
% nsegs
) != 0 || resid
== 0) {
4096 m
= (struct mbuf
*)mp_list
;
4097 mp_list
= mp_list
->obj_next
;
4099 m
= (struct mbuf
*)rmp_list
;
4100 rmp_list
= rmp_list
->obj_next
;
4103 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
4104 VERIFY(m
->m_ext
.ext_free
== NULL
||
4105 m
->m_ext
.ext_free
== m_bigfree
||
4106 m
->m_ext
.ext_free
== m_16kfree
);
4108 cl
= m
->m_ext
.ext_buf
;
4111 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
4112 VERIFY(MBUF_IS_COMPOSITE(m
));
4114 flag
= MEXT_FLAGS(m
);
4116 pkthdr
= (nsegs
== 1 || (num
% nsegs
) == 1);
4119 MBUF_INIT(m
, pkthdr
, MT_DATA
);
4120 if (m
->m_ext
.ext_free
== m_16kfree
) {
4121 MBUF_16KCL_INIT(m
, cl
, rfa
, 1, flag
);
4122 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
4123 MBUF_BIGCL_INIT(m
, cl
, rfa
, 1, flag
);
4125 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
4128 if (pkthdr
&& mac_init_mbuf(m
, wait
) != 0) {
4133 #endif /* MAC_NET */
4136 if ((num
% nsegs
) == 0)
4137 np
= &first
->m_nextpkt
;
4146 mtype_stat_add(MT_DATA
, num
);
4147 mtype_stat_sub(MT_FREE
, num
);
4152 /* We've got them all; return to caller */
4153 if (num
== *numlist
) {
4154 ASSERT(mp_list
== NULL
&& rmp_list
== NULL
);
4159 /* Free up what's left of the above */
4160 if (mp_list
!= NULL
)
4161 mcache_free_ext(cp
, mp_list
);
4162 if (rmp_list
!= NULL
)
4163 mcache_free_ext(rcp
, rmp_list
);
4164 if (wantall
&& top
!= NULL
) {
4173 * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated
4174 * packets on receive ring.
4176 __private_extern__
struct mbuf
*
4177 m_getpacket_how(int wait
)
4179 unsigned int num_needed
= 1;
4181 return (m_getpackets_internal(&num_needed
, 1, wait
, 1,
4186 * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated
4187 * packets on receive ring.
4192 unsigned int num_needed
= 1;
4194 return (m_getpackets_internal(&num_needed
, 1, M_WAIT
, 1,
4199 * Return a list of mbuf hdrs that point to clusters. Try for num_needed;
4200 * if this can't be met, return whatever number were available. Set up the
4201 * first num_with_pkthdrs with mbuf hdrs configured as packet headers. These
4202 * are chained on the m_nextpkt field. Any packets requested beyond this are
4203 * chained onto the last packet header's m_next field.
4206 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
4208 unsigned int n
= num_needed
;
4210 return (m_getpackets_internal(&n
, num_with_pkthdrs
, how
, 0,
4215 * Return a list of mbuf hdrs set up as packet hdrs chained together
4216 * on the m_nextpkt field
4219 m_getpackethdrs(int num_needed
, int how
)
4222 struct mbuf
**np
, *top
;
4227 while (num_needed
--) {
4228 m
= _M_RETRYHDR(how
, MT_DATA
);
4240 * Free an mbuf list (m_nextpkt) while following m_next. Returns the count
4241 * for mbufs packets freed. Used by the drivers.
4244 m_freem_list(struct mbuf
*m
)
4246 struct mbuf
*nextpkt
;
4247 mcache_obj_t
*mp_list
= NULL
;
4248 mcache_obj_t
*mcl_list
= NULL
;
4249 mcache_obj_t
*mbc_list
= NULL
;
4250 mcache_obj_t
*m16k_list
= NULL
;
4251 mcache_obj_t
*m_mcl_list
= NULL
;
4252 mcache_obj_t
*m_mbc_list
= NULL
;
4253 mcache_obj_t
*m_m16k_list
= NULL
;
4254 mcache_obj_t
*ref_list
= NULL
;
4256 int mt_free
= 0, mt_data
= 0, mt_header
= 0, mt_soname
= 0, mt_tag
= 0;
4261 nextpkt
= m
->m_nextpkt
;
4262 m
->m_nextpkt
= NULL
;
4265 struct mbuf
*next
= m
->m_next
;
4266 mcache_obj_t
*o
, *rfa
;
4267 u_int32_t refcnt
, composite
;
4269 if (m
->m_type
== MT_FREE
)
4270 panic("m_free: freeing an already freed mbuf");
4272 if (m
->m_type
!= MT_FREE
)
4275 if (m
->m_flags
& M_PKTHDR
) {
4276 /* Check for scratch area overflow */
4277 m_redzone_verify(m
);
4278 /* Free the aux data and tags if there is any */
4279 m_tag_delete_chain(m
, NULL
);
4282 if (!(m
->m_flags
& M_EXT
))
4285 o
= (mcache_obj_t
*)(void *)m
->m_ext
.ext_buf
;
4286 refcnt
= m_decref(m
);
4287 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
4288 if (refcnt
== 0 && !composite
) {
4289 if (m
->m_ext
.ext_free
== NULL
) {
4290 o
->obj_next
= mcl_list
;
4292 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
4293 o
->obj_next
= mbc_list
;
4295 } else if (m
->m_ext
.ext_free
== m_16kfree
) {
4296 o
->obj_next
= m16k_list
;
4299 (*(m
->m_ext
.ext_free
))((caddr_t
)o
,
4303 rfa
= (mcache_obj_t
*)(void *)MEXT_RFA(m
);
4304 rfa
->obj_next
= ref_list
;
4307 } else if (refcnt
== 0 && composite
) {
4308 VERIFY(m
->m_type
!= MT_FREE
);
4310 * Amortize the costs of atomic operations
4311 * by doing them at the end, if possible.
4313 if (m
->m_type
== MT_DATA
)
4315 else if (m
->m_type
== MT_HEADER
)
4317 else if (m
->m_type
== MT_SONAME
)
4319 else if (m
->m_type
== MT_TAG
)
4322 mtype_stat_dec(m
->m_type
);
4324 m
->m_type
= MT_FREE
;
4327 m
->m_next
= m
->m_nextpkt
= NULL
;
4329 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
4331 /* "Free" into the intermediate cache */
4332 o
= (mcache_obj_t
*)m
;
4333 if (m
->m_ext
.ext_free
== NULL
) {
4334 o
->obj_next
= m_mcl_list
;
4336 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
4337 o
->obj_next
= m_mbc_list
;
4340 VERIFY(m
->m_ext
.ext_free
== m_16kfree
);
4341 o
->obj_next
= m_m16k_list
;
4349 * Amortize the costs of atomic operations
4350 * by doing them at the end, if possible.
4352 if (m
->m_type
== MT_DATA
)
4354 else if (m
->m_type
== MT_HEADER
)
4356 else if (m
->m_type
== MT_SONAME
)
4358 else if (m
->m_type
== MT_TAG
)
4360 else if (m
->m_type
!= MT_FREE
)
4361 mtype_stat_dec(m
->m_type
);
4363 m
->m_type
= MT_FREE
;
4364 m
->m_flags
= m
->m_len
= 0;
4365 m
->m_next
= m
->m_nextpkt
= NULL
;
4367 ((mcache_obj_t
*)m
)->obj_next
= mp_list
;
4368 mp_list
= (mcache_obj_t
*)m
;
4377 mtype_stat_add(MT_FREE
, mt_free
);
4379 mtype_stat_sub(MT_DATA
, mt_data
);
4381 mtype_stat_sub(MT_HEADER
, mt_header
);
4383 mtype_stat_sub(MT_SONAME
, mt_soname
);
4385 mtype_stat_sub(MT_TAG
, mt_tag
);
4387 if (mp_list
!= NULL
)
4388 mcache_free_ext(m_cache(MC_MBUF
), mp_list
);
4389 if (mcl_list
!= NULL
)
4390 mcache_free_ext(m_cache(MC_CL
), mcl_list
);
4391 if (mbc_list
!= NULL
)
4392 mcache_free_ext(m_cache(MC_BIGCL
), mbc_list
);
4393 if (m16k_list
!= NULL
)
4394 mcache_free_ext(m_cache(MC_16KCL
), m16k_list
);
4395 if (m_mcl_list
!= NULL
)
4396 mcache_free_ext(m_cache(MC_MBUF_CL
), m_mcl_list
);
4397 if (m_mbc_list
!= NULL
)
4398 mcache_free_ext(m_cache(MC_MBUF_BIGCL
), m_mbc_list
);
4399 if (m_m16k_list
!= NULL
)
4400 mcache_free_ext(m_cache(MC_MBUF_16KCL
), m_m16k_list
);
4401 if (ref_list
!= NULL
)
4402 mcache_free_ext(ref_cache
, ref_list
);
4408 m_freem(struct mbuf
*m
)
4415 * Mbuffer utility routines.
4419 * Compute the amount of space available before the current start
4420 * of data in an mbuf.
4423 m_leadingspace(struct mbuf
*m
)
4425 if (m
->m_flags
& M_EXT
) {
4426 if (MCLHASREFERENCE(m
))
4428 return (m
->m_data
- m
->m_ext
.ext_buf
);
4430 if (m
->m_flags
& M_PKTHDR
)
4431 return (m
->m_data
- m
->m_pktdat
);
4432 return (m
->m_data
- m
->m_dat
);
4436 * Compute the amount of space available after the end of data in an mbuf.
4439 m_trailingspace(struct mbuf
*m
)
4441 if (m
->m_flags
& M_EXT
) {
4442 if (MCLHASREFERENCE(m
))
4444 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
4445 (m
->m_data
+ m
->m_len
));
4447 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
4451 * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain,
4452 * copy junk along. Does not adjust packet header length.
4455 m_prepend(struct mbuf
*m
, int len
, int how
)
4459 _MGET(mn
, how
, m
->m_type
);
4464 if (m
->m_flags
& M_PKTHDR
) {
4465 M_COPY_PKTHDR(mn
, m
);
4466 m
->m_flags
&= ~M_PKTHDR
;
4477 * Replacement for old M_PREPEND macro: allocate new mbuf to prepend to
4478 * chain, copy junk along, and adjust length.
4481 m_prepend_2(struct mbuf
*m
, int len
, int how
)
4483 if (M_LEADINGSPACE(m
) >= len
) {
4487 m
= m_prepend(m
, len
, how
);
4489 if ((m
) && (m
->m_flags
& M_PKTHDR
))
4490 m
->m_pkthdr
.len
+= len
;
4495 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
4496 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
4497 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
4502 m_copym_mode(struct mbuf
*m
, int off0
, int len
, int wait
, uint32_t mode
)
4504 struct mbuf
*n
, *mhdr
= NULL
, **np
;
4509 if (off
< 0 || len
< 0)
4510 panic("m_copym: invalid offset %d or len %d", off
, len
);
4512 if (off
== 0 && (m
->m_flags
& M_PKTHDR
)) {
4517 while (off
>= m
->m_len
) {
4518 if (m
->m_next
== NULL
)
4519 panic("m_copym: invalid mbuf chain");
4528 if (len
!= M_COPYALL
)
4529 panic("m_copym: len != M_COPYALL");
4533 n
= _M_RETRY(wait
, m
->m_type
);
4540 if (mode
== M_COPYM_MOVE_HDR
) {
4541 M_COPY_PKTHDR(n
, mhdr
);
4542 } else if (mode
== M_COPYM_COPY_HDR
) {
4543 if (m_dup_pkthdr(n
, mhdr
, wait
) == 0)
4546 if (len
== M_COPYALL
)
4547 n
->m_pkthdr
.len
-= off0
;
4549 n
->m_pkthdr
.len
= len
;
4552 if (len
== M_COPYALL
) {
4553 if (MIN(len
, (m
->m_len
- off
)) == len
) {
4554 printf("m->m_len %d - off %d = %d, %d\n",
4555 m
->m_len
, off
, m
->m_len
- off
,
4556 MIN(len
, (m
->m_len
- off
)));
4559 n
->m_len
= MIN(len
, (m
->m_len
- off
));
4560 if (n
->m_len
== M_COPYALL
) {
4561 printf("n->m_len == M_COPYALL, fixing\n");
4564 if (m
->m_flags
& M_EXT
) {
4565 n
->m_ext
= m
->m_ext
;
4567 n
->m_data
= m
->m_data
+ off
;
4568 n
->m_flags
|= M_EXT
;
4570 bcopy(MTOD(m
, caddr_t
)+off
, MTOD(n
, caddr_t
),
4571 (unsigned)n
->m_len
);
4573 if (len
!= M_COPYALL
)
4593 m_copym(struct mbuf
*m
, int off0
, int len
, int wait
)
4595 return (m_copym_mode(m
, off0
, len
, wait
, M_COPYM_MOVE_HDR
));
4599 * Equivalent to m_copym except that all necessary mbuf hdrs are allocated
4600 * within this routine also, the last mbuf and offset accessed are passed
4601 * out and can be passed back in to avoid having to rescan the entire mbuf
4602 * list (normally hung off of the socket)
4605 m_copym_with_hdrs(struct mbuf
*m
, int off0
, int len0
, int wait
,
4606 struct mbuf
**m_lastm
, int *m_off
, uint32_t mode
)
4608 struct mbuf
*n
, **np
= NULL
;
4609 int off
= off0
, len
= len0
;
4610 struct mbuf
*top
= NULL
;
4611 int mcflags
= MSLEEPF(wait
);
4614 mcache_obj_t
*list
= NULL
;
4617 if (off
== 0 && (m
->m_flags
& M_PKTHDR
))
4620 if (*m_lastm
!= NULL
) {
4624 while (off
>= m
->m_len
) {
4634 len
-= MIN(len
, (n
->m_len
- ((needed
== 1) ? off
: 0)));
4641 * If the caller doesn't want to be put to sleep, mark it with
4642 * MCR_TRYHARD so that we may reclaim buffers from other places
4645 if (mcflags
& MCR_NOSLEEP
)
4646 mcflags
|= MCR_TRYHARD
;
4648 if (mcache_alloc_ext(m_cache(MC_MBUF
), &list
, needed
,
4654 n
= (struct mbuf
*)list
;
4655 list
= list
->obj_next
;
4656 ASSERT(n
!= NULL
&& m
!= NULL
);
4658 type
= (top
== NULL
) ? MT_HEADER
: m
->m_type
;
4659 MBUF_INIT(n
, (top
== NULL
), type
);
4661 if (top
== NULL
&& mac_mbuf_label_init(n
, wait
) != 0) {
4662 mtype_stat_inc(MT_HEADER
);
4663 mtype_stat_dec(MT_FREE
);
4667 #endif /* MAC_NET */
4679 if (mode
== M_COPYM_MOVE_HDR
) {
4680 M_COPY_PKTHDR(n
, m
);
4681 } else if (mode
== M_COPYM_COPY_HDR
) {
4682 if (m_dup_pkthdr(n
, m
, wait
) == 0)
4685 n
->m_pkthdr
.len
= len
;
4688 n
->m_len
= MIN(len
, (m
->m_len
- off
));
4690 if (m
->m_flags
& M_EXT
) {
4691 n
->m_ext
= m
->m_ext
;
4693 n
->m_data
= m
->m_data
+ off
;
4694 n
->m_flags
|= M_EXT
;
4696 bcopy(MTOD(m
, caddr_t
)+off
, MTOD(n
, caddr_t
),
4697 (unsigned)n
->m_len
);
4702 if ((off
+ n
->m_len
) == m
->m_len
) {
4703 *m_lastm
= m
->m_next
;
4707 *m_off
= off
+ n
->m_len
;
4716 mtype_stat_inc(MT_HEADER
);
4717 mtype_stat_add(type
, needed
);
4718 mtype_stat_sub(MT_FREE
, needed
+ 1);
4720 ASSERT(list
== NULL
);
4725 mcache_free_ext(m_cache(MC_MBUF
), list
);
4733 * Copy data from an mbuf chain starting "off" bytes from the beginning,
4734 * continuing for "len" bytes, into the indicated buffer.
4737 m_copydata(struct mbuf
*m
, int off
, int len
, void *vp
)
4742 if (off
< 0 || len
< 0)
4743 panic("m_copydata: invalid offset %d or len %d", off
, len
);
4747 panic("m_copydata: invalid mbuf chain");
4755 panic("m_copydata: invalid mbuf chain");
4756 count
= MIN(m
->m_len
- off
, len
);
4757 bcopy(MTOD(m
, caddr_t
) + off
, cp
, count
);
4766 * Concatenate mbuf chain n to m. Both chains must be of the same type
4767 * (e.g. MT_DATA). Any m_pkthdr is not updated.
4770 m_cat(struct mbuf
*m
, struct mbuf
*n
)
4775 if ((m
->m_flags
& M_EXT
) ||
4776 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
4777 /* just join the two chains */
4781 /* splat the data from one into the other */
4782 bcopy(MTOD(n
, caddr_t
), MTOD(m
, caddr_t
) + m
->m_len
,
4784 m
->m_len
+= n
->m_len
;
4790 m_adj(struct mbuf
*mp
, int req_len
)
4796 if ((m
= mp
) == NULL
)
4802 while (m
!= NULL
&& len
> 0) {
4803 if (m
->m_len
<= len
) {
4814 if (m
->m_flags
& M_PKTHDR
)
4815 m
->m_pkthdr
.len
-= (req_len
- len
);
4818 * Trim from tail. Scan the mbuf chain,
4819 * calculating its length and finding the last mbuf.
4820 * If the adjustment only affects this mbuf, then just
4821 * adjust and return. Otherwise, rescan and truncate
4822 * after the remaining size.
4828 if (m
->m_next
== (struct mbuf
*)0)
4832 if (m
->m_len
>= len
) {
4835 if (m
->m_flags
& M_PKTHDR
)
4836 m
->m_pkthdr
.len
-= len
;
4843 * Correct length for chain is "count".
4844 * Find the mbuf with last data, adjust its length,
4845 * and toss data from remaining mbufs on chain.
4848 if (m
->m_flags
& M_PKTHDR
)
4849 m
->m_pkthdr
.len
= count
;
4850 for (; m
; m
= m
->m_next
) {
4851 if (m
->m_len
>= count
) {
4857 while ((m
= m
->m_next
))
4863 * Rearange an mbuf chain so that len bytes are contiguous
4864 * and in the data area of an mbuf (so that mtod and dtom
4865 * will work for a structure of size len). Returns the resulting
4866 * mbuf chain on success, frees it and returns null on failure.
4867 * If there is room, it will add up to max_protohdr-len extra bytes to the
4868 * contiguous region in an attempt to avoid being called next time.
4873 m_pullup(struct mbuf
*n
, int len
)
4880 * If first mbuf has no cluster, and has room for len bytes
4881 * without shifting current data, pullup into it,
4882 * otherwise allocate a new mbuf to prepend to the chain.
4884 if ((n
->m_flags
& M_EXT
) == 0 &&
4885 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
4886 if (n
->m_len
>= len
)
4894 _MGET(m
, M_DONTWAIT
, n
->m_type
);
4898 if (n
->m_flags
& M_PKTHDR
) {
4899 M_COPY_PKTHDR(m
, n
);
4900 n
->m_flags
&= ~M_PKTHDR
;
4903 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
4905 count
= MIN(MIN(MAX(len
, max_protohdr
), space
), n
->m_len
);
4906 bcopy(MTOD(n
, caddr_t
), MTOD(m
, caddr_t
) + m
->m_len
,
4916 } while (len
> 0 && n
);
4930 * Like m_pullup(), except a new mbuf is always allocated, and we allow
4931 * the amount of empty space before the data in the new mbuf to be specified
4932 * (in the event that the caller expects to prepend later).
4934 __private_extern__
int MSFail
= 0;
4936 __private_extern__
struct mbuf
*
4937 m_copyup(struct mbuf
*n
, int len
, int dstoff
)
4942 if (len
> (MHLEN
- dstoff
))
4944 MGET(m
, M_DONTWAIT
, n
->m_type
);
4948 if (n
->m_flags
& M_PKTHDR
) {
4949 m_copy_pkthdr(m
, n
);
4950 n
->m_flags
&= ~M_PKTHDR
;
4952 m
->m_data
+= dstoff
;
4953 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
4955 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
4956 memcpy(mtod(m
, caddr_t
) + m
->m_len
, mtod(n
, caddr_t
),
4966 } while (len
> 0 && n
);
4980 * Partition an mbuf chain in two pieces, returning the tail --
4981 * all but the first len0 bytes. In case of failure, it returns NULL and
4982 * attempts to restore the chain to its original state.
4985 m_split(struct mbuf
*m0
, int len0
, int wait
)
4987 return (m_split0(m0
, len0
, wait
, 1));
4990 static struct mbuf
*
4991 m_split0(struct mbuf
*m0
, int len0
, int wait
, int copyhdr
)
4994 unsigned len
= len0
, remain
;
4996 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
5000 remain
= m
->m_len
- len
;
5001 if (copyhdr
&& (m0
->m_flags
& M_PKTHDR
)) {
5002 _MGETHDR(n
, wait
, m0
->m_type
);
5005 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
5006 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
5007 m0
->m_pkthdr
.len
= len0
;
5008 if (m
->m_flags
& M_EXT
)
5010 if (remain
> MHLEN
) {
5011 /* m can't be the lead packet */
5013 n
->m_next
= m_split(m
, len
, wait
);
5014 if (n
->m_next
== NULL
) {
5020 MH_ALIGN(n
, remain
);
5021 } else if (remain
== 0) {
5026 _MGET(n
, wait
, m
->m_type
);
5032 if (m
->m_flags
& M_EXT
) {
5033 n
->m_flags
|= M_EXT
;
5034 n
->m_ext
= m
->m_ext
;
5036 n
->m_data
= m
->m_data
+ len
;
5038 bcopy(MTOD(m
, caddr_t
) + len
, MTOD(n
, caddr_t
), remain
);
5042 n
->m_next
= m
->m_next
;
5048 * Routine to copy from device local memory into mbufs.
5051 m_devget(char *buf
, int totlen
, int off0
, struct ifnet
*ifp
,
5052 void (*copy
)(const void *, void *, size_t))
5055 struct mbuf
*top
= NULL
, **mp
= &top
;
5056 int off
= off0
, len
;
5064 * If 'off' is non-zero, packet is trailer-encapsulated,
5065 * so we have to skip the type and length fields.
5067 cp
+= off
+ 2 * sizeof (u_int16_t
);
5068 totlen
-= 2 * sizeof (u_int16_t
);
5070 _MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
5073 m
->m_pkthdr
.rcvif
= ifp
;
5074 m
->m_pkthdr
.len
= totlen
;
5077 while (totlen
> 0) {
5079 _MGET(m
, M_DONTWAIT
, MT_DATA
);
5086 len
= MIN(totlen
, epkt
- cp
);
5087 if (len
>= MINCLSIZE
) {
5088 MCLGET(m
, M_DONTWAIT
);
5089 if (m
->m_flags
& M_EXT
) {
5090 m
->m_len
= len
= MIN(len
, m_maxsize(MC_CL
));
5092 /* give up when it's out of cluster mbufs */
5100 * Place initial small packet/header at end of mbuf.
5102 if (len
< m
->m_len
) {
5104 len
+ max_linkhdr
<= m
->m_len
)
5105 m
->m_data
+= max_linkhdr
;
5112 copy(cp
, MTOD(m
, caddr_t
), (unsigned)len
);
5114 bcopy(cp
, MTOD(m
, caddr_t
), (unsigned)len
);
5125 #ifndef MBUF_GROWTH_NORMAL_THRESH
5126 #define MBUF_GROWTH_NORMAL_THRESH 25
5130 * Cluster freelist allocation check.
5133 m_howmany(int num
, size_t bufsize
)
5136 u_int32_t m_mbclusters
, m_clusters
, m_bigclusters
, m_16kclusters
;
5137 u_int32_t m_mbfree
, m_clfree
, m_bigclfree
, m_16kclfree
;
5138 u_int32_t sumclusters
, freeclusters
;
5139 u_int32_t percent_pool
, percent_kmem
;
5140 u_int32_t mb_growth
, mb_growth_thresh
;
5142 VERIFY(bufsize
== m_maxsize(MC_BIGCL
) ||
5143 bufsize
== m_maxsize(MC_16KCL
));
5145 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
5147 /* Numbers in 2K cluster units */
5148 m_mbclusters
= m_total(MC_MBUF
) >> NMBPCLSHIFT
;
5149 m_clusters
= m_total(MC_CL
);
5150 m_bigclusters
= m_total(MC_BIGCL
) << NCLPBGSHIFT
;
5151 m_16kclusters
= m_total(MC_16KCL
);
5152 sumclusters
= m_mbclusters
+ m_clusters
+ m_bigclusters
;
5154 m_mbfree
= m_infree(MC_MBUF
) >> NMBPCLSHIFT
;
5155 m_clfree
= m_infree(MC_CL
);
5156 m_bigclfree
= m_infree(MC_BIGCL
) << NCLPBGSHIFT
;
5157 m_16kclfree
= m_infree(MC_16KCL
);
5158 freeclusters
= m_mbfree
+ m_clfree
+ m_bigclfree
;
5160 /* Bail if we've maxed out the mbuf memory map */
5161 if ((bufsize
== m_maxsize(MC_BIGCL
) && sumclusters
>= nclusters
) ||
5162 (njcl
> 0 && bufsize
== m_maxsize(MC_16KCL
) &&
5163 (m_16kclusters
<< NCLPJCLSHIFT
) >= njcl
)) {
5167 if (bufsize
== m_maxsize(MC_BIGCL
)) {
5169 if (m_bigclusters
< m_minlimit(MC_BIGCL
))
5170 return (m_minlimit(MC_BIGCL
) - m_bigclusters
);
5173 ((sumclusters
- freeclusters
) * 100) / sumclusters
;
5174 percent_kmem
= (sumclusters
* 100) / nclusters
;
5177 * If a light/normal user, grow conservatively (75%)
5178 * If a heavy user, grow aggressively (50%)
5180 if (percent_kmem
< MBUF_GROWTH_NORMAL_THRESH
)
5181 mb_growth
= MB_GROWTH_NORMAL
;
5183 mb_growth
= MB_GROWTH_AGGRESSIVE
;
5185 if (percent_kmem
< 5) {
5186 /* For initial allocations */
5189 /* Return if >= MBIGCL_LOWAT clusters available */
5190 if (m_infree(MC_BIGCL
) >= MBIGCL_LOWAT
&&
5191 m_total(MC_BIGCL
) >=
5192 MBIGCL_LOWAT
+ m_minlimit(MC_BIGCL
))
5195 /* Ensure at least num clusters are accessible */
5196 if (num
>= m_infree(MC_BIGCL
))
5197 i
= num
- m_infree(MC_BIGCL
);
5198 if (num
> m_total(MC_BIGCL
) - m_minlimit(MC_BIGCL
))
5199 j
= num
- (m_total(MC_BIGCL
) -
5200 m_minlimit(MC_BIGCL
));
5205 * Grow pool if percent_pool > 75 (normal growth)
5206 * or percent_pool > 50 (aggressive growth).
5208 mb_growth_thresh
= 100 - (100 / (1 << mb_growth
));
5209 if (percent_pool
> mb_growth_thresh
)
5210 j
= ((sumclusters
+ num
) >> mb_growth
) -
5215 /* Check to ensure we didn't go over limits */
5216 if (i
+ m_bigclusters
>= m_maxlimit(MC_BIGCL
))
5217 i
= m_maxlimit(MC_BIGCL
) - m_bigclusters
;
5218 if ((i
<< 1) + sumclusters
>= nclusters
)
5219 i
= (nclusters
- sumclusters
) >> 1;
5220 VERIFY((m_total(MC_BIGCL
) + i
) <= m_maxlimit(MC_BIGCL
));
5221 VERIFY(sumclusters
+ (i
<< 1) <= nclusters
);
5223 } else { /* 16K CL */
5226 if (m_16kclusters
< MIN16KCL
)
5227 return (MIN16KCL
- m_16kclusters
);
5228 if (m_16kclfree
>= M16KCL_LOWAT
)
5231 /* Ensure at least num clusters are available */
5232 if (num
>= m_16kclfree
)
5233 i
= num
- m_16kclfree
;
5235 /* Always grow 16KCL pool aggressively */
5236 if (((m_16kclusters
+ num
) >> 1) > m_16kclfree
)
5237 j
= ((m_16kclusters
+ num
) >> 1) - m_16kclfree
;
5240 /* Check to ensure we don't go over limit */
5241 if (i
+ m_16kclusters
>= m_maxlimit(MC_16KCL
))
5242 i
= m_maxlimit(MC_16KCL
) - m_16kclusters
;
5243 VERIFY((m_total(MC_16KCL
) + i
) <= m_maxlimit(MC_16KCL
));
5248 * Return the number of bytes in the mbuf chain, m.
5251 m_length(struct mbuf
*m
)
5254 unsigned int pktlen
;
5256 if (m
->m_flags
& M_PKTHDR
)
5257 return (m
->m_pkthdr
.len
);
5260 for (m0
= m
; m0
!= NULL
; m0
= m0
->m_next
)
5261 pktlen
+= m0
->m_len
;
5266 * Copy data from a buffer back into the indicated mbuf chain,
5267 * starting "off" bytes from the beginning, extending the mbuf
5268 * chain if necessary.
5271 m_copyback(struct mbuf
*m0
, int off
, int len
, const void *cp
)
5274 struct mbuf
*origm
= m0
;
5284 m_copyback0(&m0
, off
, len
, cp
,
5285 M_COPYBACK0_COPYBACK
| M_COPYBACK0_EXTEND
, M_DONTWAIT
);
5288 if (error
!= 0 || (m0
!= NULL
&& origm
!= m0
))
5289 panic("m_copyback");
5294 m_copyback_cow(struct mbuf
*m0
, int off
, int len
, const void *cp
, int how
)
5298 /* don't support chain expansion */
5299 VERIFY(off
+ len
<= m_length(m0
));
5301 error
= m_copyback0(&m0
, off
, len
, cp
,
5302 M_COPYBACK0_COPYBACK
| M_COPYBACK0_COW
, how
);
5305 * no way to recover from partial success.
5306 * just free the chain.
5315 * m_makewritable: ensure the specified range writable.
5318 m_makewritable(struct mbuf
**mp
, int off
, int len
, int how
)
5323 int origlen
, reslen
;
5325 origlen
= m_length(*mp
);
5328 #if 0 /* M_COPYALL is large enough */
5329 if (len
== M_COPYALL
)
5330 len
= m_length(*mp
) - off
; /* XXX */
5333 error
= m_copyback0(mp
, off
, len
, NULL
,
5334 M_COPYBACK0_PRESERVE
| M_COPYBACK0_COW
, how
);
5338 for (n
= *mp
; n
; n
= n
->m_next
)
5340 if (origlen
!= reslen
)
5341 panic("m_makewritable: length changed");
5342 if (((*mp
)->m_flags
& M_PKTHDR
) && reslen
!= (*mp
)->m_pkthdr
.len
)
5343 panic("m_makewritable: inconsist");
5350 m_copyback0(struct mbuf
**mp0
, int off
, int len
, const void *vp
, int flags
,
5357 const char *cp
= vp
;
5359 VERIFY(mp0
!= NULL
);
5360 VERIFY(*mp0
!= NULL
);
5361 VERIFY((flags
& M_COPYBACK0_PRESERVE
) == 0 || cp
== NULL
);
5362 VERIFY((flags
& M_COPYBACK0_COPYBACK
) == 0 || cp
!= NULL
);
5365 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
5366 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
5369 VERIFY((~flags
& (M_COPYBACK0_EXTEND
|M_COPYBACK0_COW
)) != 0);
5373 while (off
> (mlen
= m
->m_len
)) {
5376 if (m
->m_next
== NULL
) {
5379 if (!(flags
& M_COPYBACK0_EXTEND
))
5383 * try to make some space at the end of "m".
5387 if (off
+ len
>= MINCLSIZE
&&
5388 !(m
->m_flags
& M_EXT
) && m
->m_len
== 0) {
5391 tspace
= M_TRAILINGSPACE(m
);
5393 tspace
= MIN(tspace
, off
+ len
);
5395 bzero(mtod(m
, char *) + m
->m_len
,
5404 * need to allocate an mbuf.
5407 if (off
+ len
>= MINCLSIZE
) {
5408 n
= m_getcl(how
, m
->m_type
, 0);
5410 n
= _M_GET(how
, m
->m_type
);
5416 n
->m_len
= MIN(M_TRAILINGSPACE(n
), off
+ len
);
5417 bzero(mtod(n
, char *), MIN(n
->m_len
, off
));
5424 mlen
= m
->m_len
- off
;
5425 if (mlen
!= 0 && m_mclhasreference(m
)) {
5430 * this mbuf is read-only.
5431 * allocate a new writable mbuf and try again.
5435 if (!(flags
& M_COPYBACK0_COW
))
5436 panic("m_copyback0: read-only");
5437 #endif /* DIAGNOSTIC */
5440 * if we're going to write into the middle of
5441 * a mbuf, split it first.
5443 if (off
> 0 && len
< mlen
) {
5444 n
= m_split0(m
, off
, how
, 0);
5455 * XXX TODO coalesce into the trailingspace of
5456 * the previous mbuf when possible.
5460 * allocate a new mbuf. copy packet header if needed.
5462 n
= _M_GET(how
, m
->m_type
);
5465 if (off
== 0 && (m
->m_flags
& M_PKTHDR
)) {
5466 M_COPY_PKTHDR(n
, m
);
5469 if (len
>= MINCLSIZE
)
5470 MCLGET(n
, M_DONTWAIT
);
5472 (n
->m_flags
& M_EXT
) ? MCLBYTES
: MLEN
;
5478 * free the region which has been overwritten.
5479 * copying data from old mbufs if requested.
5481 if (flags
& M_COPYBACK0_PRESERVE
)
5482 datap
= mtod(n
, char *);
5486 VERIFY(off
== 0 || eatlen
>= mlen
);
5488 VERIFY(len
>= mlen
);
5492 m_copydata(m
, off
, mlen
, datap
);
5499 while (m
!= NULL
&& m_mclhasreference(m
) &&
5500 n
->m_type
== m
->m_type
&& eatlen
> 0) {
5501 mlen
= MIN(eatlen
, m
->m_len
);
5503 m_copydata(m
, 0, mlen
, datap
);
5510 *mp
= m
= m_free(m
);
5518 mlen
= MIN(mlen
, len
);
5519 if (flags
& M_COPYBACK0_COPYBACK
) {
5520 bcopy(cp
, mtod(m
, caddr_t
) + off
, (unsigned)mlen
);
5529 if (m
->m_next
== NULL
) {
5536 if (((m
= *mp0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
)) {
5537 VERIFY(flags
& M_COPYBACK0_EXTEND
);
5538 m
->m_pkthdr
.len
= totlen
;
5548 mcl_to_paddr(char *addr
)
5550 vm_offset_t base_phys
;
5552 if (!MBUF_IN_MAP(addr
))
5554 base_phys
= mcl_paddr
[atop_64(addr
- (char *)mbutl
)];
5558 return ((uint64_t)(ptoa_64(base_phys
) | ((uint64_t)addr
& PAGE_MASK
)));
5562 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
5563 * And really copy the thing. That way, we don't "precompute" checksums
5564 * for unsuspecting consumers. Assumption: m->m_nextpkt == 0. Trick: for
5565 * small packets, don't dup into a cluster. That way received packets
5566 * don't take up too much room in the sockbuf (cf. sbspace()).
5571 m_dup(struct mbuf
*m
, int how
)
5573 struct mbuf
*n
, **np
;
5579 if (m
->m_flags
& M_PKTHDR
)
5583 * Quick check: if we have one mbuf and its data fits in an
5584 * mbuf with packet header, just copy and go.
5586 if (m
->m_next
== NULL
) {
5587 /* Then just move the data into an mbuf and be done... */
5589 if (m
->m_pkthdr
.len
<= MHLEN
&& m
->m_len
<= MHLEN
) {
5590 if ((n
= _M_GETHDR(how
, m
->m_type
)) == NULL
)
5592 n
->m_len
= m
->m_len
;
5593 m_dup_pkthdr(n
, m
, how
);
5594 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
5597 } else if (m
->m_len
<= MLEN
) {
5598 if ((n
= _M_GET(how
, m
->m_type
)) == NULL
)
5600 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
5601 n
->m_len
= m
->m_len
;
5607 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
5611 n
= _M_GETHDR(how
, m
->m_type
);
5613 n
= _M_GET(how
, m
->m_type
);
5616 if (m
->m_flags
& M_EXT
) {
5617 if (m
->m_len
<= m_maxsize(MC_CL
))
5619 else if (m
->m_len
<= m_maxsize(MC_BIGCL
))
5620 n
= m_mbigget(n
, how
);
5621 else if (m
->m_len
<= m_maxsize(MC_16KCL
) && njcl
> 0)
5622 n
= m_m16kget(n
, how
);
5623 if (!(n
->m_flags
& M_EXT
)) {
5630 /* Don't use M_COPY_PKTHDR: preserve m_data */
5631 m_dup_pkthdr(n
, m
, how
);
5633 if (!(n
->m_flags
& M_EXT
))
5634 n
->m_data
= n
->m_pktdat
;
5636 n
->m_len
= m
->m_len
;
5638 * Get the dup on the same bdry as the original
5639 * Assume that the two mbufs have the same offset to data area
5640 * (up to word boundaries)
5642 bcopy(MTOD(m
, caddr_t
), MTOD(n
, caddr_t
), (unsigned)n
->m_len
);
5646 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
5661 #define MBUF_MULTIPAGES(m) \
5662 (((m)->m_flags & M_EXT) && \
5663 ((IS_P2ALIGNED((m)->m_data, NBPG) && (m)->m_len > NBPG) || \
5664 (!IS_P2ALIGNED((m)->m_data, NBPG) && \
5665 P2ROUNDUP((m)->m_data, NBPG) < ((uintptr_t)(m)->m_data + (m)->m_len))))
5667 static struct mbuf
*
5668 m_expand(struct mbuf
*m
, struct mbuf
**last
)
5670 struct mbuf
*top
= NULL
;
5671 struct mbuf
**nm
= &top
;
5672 uintptr_t data0
, data
;
5673 unsigned int len0
, len
;
5675 VERIFY(MBUF_MULTIPAGES(m
));
5676 VERIFY(m
->m_next
== NULL
);
5677 data0
= (uintptr_t)m
->m_data
;
5685 if (IS_P2ALIGNED(data
, NBPG
) && len0
> NBPG
)
5687 else if (!IS_P2ALIGNED(data
, NBPG
) &&
5688 P2ROUNDUP(data
, NBPG
) < (data
+ len0
))
5689 len
= P2ROUNDUP(data
, NBPG
) - data
;
5694 VERIFY(m
->m_flags
& M_EXT
);
5695 m
->m_data
= (void *)data
;
5707 n
= _M_RETRY(M_DONTWAIT
, MT_DATA
);
5714 n
->m_ext
= m
->m_ext
;
5716 n
->m_flags
|= M_EXT
;
5723 m_normalize(struct mbuf
*m
)
5725 struct mbuf
*top
= NULL
;
5726 struct mbuf
**nm
= &top
;
5727 boolean_t expanded
= FALSE
;
5735 /* Does the data cross one or more page boundaries? */
5736 if (MBUF_MULTIPAGES(m
)) {
5738 if ((m
= m_expand(m
, &last
)) == NULL
) {
5754 atomic_add_32(&mb_normalized
, 1);
5759 * Append the specified data to the indicated mbuf chain,
5760 * Extend the mbuf chain if the new data does not fit in
5763 * Return 1 if able to complete the job; otherwise 0.
5766 m_append(struct mbuf
*m0
, int len
, caddr_t cp
)
5769 int remainder
, space
;
5771 for (m
= m0
; m
->m_next
!= NULL
; m
= m
->m_next
)
5774 space
= M_TRAILINGSPACE(m
);
5777 * Copy into available space.
5779 if (space
> remainder
)
5781 bcopy(cp
, mtod(m
, caddr_t
) + m
->m_len
, space
);
5783 cp
+= space
, remainder
-= space
;
5785 while (remainder
> 0) {
5787 * Allocate a new mbuf; could check space
5788 * and allocate a cluster instead.
5790 n
= m_get(M_WAITOK
, m
->m_type
);
5793 n
->m_len
= min(MLEN
, remainder
);
5794 bcopy(cp
, mtod(n
, caddr_t
), n
->m_len
);
5796 remainder
-= n
->m_len
;
5800 if (m0
->m_flags
& M_PKTHDR
)
5801 m0
->m_pkthdr
.len
+= len
- remainder
;
5802 return (remainder
== 0);
5806 m_last(struct mbuf
*m
)
5808 while (m
->m_next
!= NULL
)
5814 m_fixhdr(struct mbuf
*m0
)
5818 VERIFY(m0
->m_flags
& M_PKTHDR
);
5820 len
= m_length2(m0
, NULL
);
5821 m0
->m_pkthdr
.len
= len
;
5826 m_length2(struct mbuf
*m0
, struct mbuf
**last
)
5832 for (m
= m0
; m
!= NULL
; m
= m
->m_next
) {
5834 if (m
->m_next
== NULL
)
5843 * Defragment a mbuf chain, returning the shortest possible chain of mbufs
5844 * and clusters. If allocation fails and this cannot be completed, NULL will
5845 * be returned, but the passed in chain will be unchanged. Upon success,
5846 * the original chain will be freed, and the new chain will be returned.
5848 * If a non-packet header is passed in, the original mbuf (chain?) will
5849 * be returned unharmed.
5851 * If offset is specfied, the first mbuf in the chain will have a leading
5852 * space of the amount stated by the "off" parameter.
5854 * This routine requires that the m_pkthdr.header field of the original
5855 * mbuf chain is cleared by the caller.
5858 m_defrag_offset(struct mbuf
*m0
, u_int32_t off
, int how
)
5860 struct mbuf
*m_new
= NULL
, *m_final
= NULL
;
5861 int progress
= 0, length
, pktlen
;
5863 if (!(m0
->m_flags
& M_PKTHDR
))
5866 VERIFY(off
< MHLEN
);
5867 m_fixhdr(m0
); /* Needed sanity check */
5869 pktlen
= m0
->m_pkthdr
.len
+ off
;
5871 m_final
= m_getcl(how
, MT_DATA
, M_PKTHDR
);
5873 m_final
= m_gethdr(how
, MT_DATA
);
5875 if (m_final
== NULL
)
5880 m_final
->m_data
+= off
;
5884 * Caller must have handled the contents pointed to by this
5885 * pointer before coming here, as otherwise it will point to
5886 * the original mbuf which will get freed upon success.
5888 VERIFY(m0
->m_pkthdr
.pkt_hdr
== NULL
);
5890 if (m_dup_pkthdr(m_final
, m0
, how
) == 0)
5895 while (progress
< pktlen
) {
5896 length
= pktlen
- progress
;
5897 if (length
> MCLBYTES
)
5899 length
-= ((m_new
== m_final
) ? off
: 0);
5901 if (m_new
== NULL
) {
5903 m_new
= m_getcl(how
, MT_DATA
, 0);
5905 m_new
= m_get(how
, MT_DATA
);
5910 m_copydata(m0
, progress
, length
, mtod(m_new
, caddr_t
));
5912 m_new
->m_len
= length
;
5913 if (m_new
!= m_final
)
5914 m_cat(m_final
, m_new
);
5927 m_defrag(struct mbuf
*m0
, int how
)
5929 return (m_defrag_offset(m0
, 0, how
));
5933 m_mchtype(struct mbuf
*m
, int t
)
5936 mtype_stat_dec(m
->m_type
);
5941 m_mtod(struct mbuf
*m
)
5943 return (MTOD(m
, void *));
5949 return ((struct mbuf
*)((uintptr_t)(x
) & ~(MSIZE
-1)));
5953 m_mcheck(struct mbuf
*m
)
5959 * Return a pointer to mbuf/offset of location in mbuf chain.
5962 m_getptr(struct mbuf
*m
, int loc
, int *off
)
5966 /* Normal end of search. */
5967 if (m
->m_len
> loc
) {
5972 if (m
->m_next
== NULL
) {
5974 /* Point at the end of valid data. */
5987 * Inform the corresponding mcache(s) that there's a waiter below.
5990 mbuf_waiter_inc(mbuf_class_t
class, boolean_t comp
)
5992 mcache_waiter_inc(m_cache(class));
5994 if (class == MC_CL
) {
5995 mcache_waiter_inc(m_cache(MC_MBUF_CL
));
5996 } else if (class == MC_BIGCL
) {
5997 mcache_waiter_inc(m_cache(MC_MBUF_BIGCL
));
5998 } else if (class == MC_16KCL
) {
5999 mcache_waiter_inc(m_cache(MC_MBUF_16KCL
));
6001 mcache_waiter_inc(m_cache(MC_MBUF_CL
));
6002 mcache_waiter_inc(m_cache(MC_MBUF_BIGCL
));
6008 * Inform the corresponding mcache(s) that there's no more waiter below.
6011 mbuf_waiter_dec(mbuf_class_t
class, boolean_t comp
)
6013 mcache_waiter_dec(m_cache(class));
6015 if (class == MC_CL
) {
6016 mcache_waiter_dec(m_cache(MC_MBUF_CL
));
6017 } else if (class == MC_BIGCL
) {
6018 mcache_waiter_dec(m_cache(MC_MBUF_BIGCL
));
6019 } else if (class == MC_16KCL
) {
6020 mcache_waiter_dec(m_cache(MC_MBUF_16KCL
));
6022 mcache_waiter_dec(m_cache(MC_MBUF_CL
));
6023 mcache_waiter_dec(m_cache(MC_MBUF_BIGCL
));
6029 * Called during slab (blocking and non-blocking) allocation. If there
6030 * is at least one waiter, and the time since the first waiter is blocked
6031 * is greater than the watchdog timeout, panic the system.
6039 if (mb_waiters
== 0 || !mb_watchdog
)
6043 since
= now
.tv_sec
- mb_wdtstart
.tv_sec
;
6044 if (since
>= MB_WDT_MAXTIME
) {
6045 panic_plain("%s: %d waiters stuck for %u secs\n%s", __func__
,
6046 mb_waiters
, since
, mbuf_dump());
6052 * Called during blocking allocation. Returns TRUE if one or more objects
6053 * are available at the per-CPU caches layer and that allocation should be
6054 * retried at that level.
6057 mbuf_sleep(mbuf_class_t
class, unsigned int num
, int wait
)
6059 boolean_t mcache_retry
= FALSE
;
6061 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
6063 /* Check if there's anything at the cache layer */
6064 if (mbuf_cached_above(class, wait
)) {
6065 mcache_retry
= TRUE
;
6069 /* Nothing? Then try hard to get it from somewhere */
6070 m_reclaim(class, num
, (wait
& MCR_COMP
));
6072 /* We tried hard and got something? */
6073 if (m_infree(class) > 0) {
6076 } else if (mbuf_cached_above(class, wait
)) {
6078 mcache_retry
= TRUE
;
6080 } else if (wait
& MCR_TRYHARD
) {
6081 mcache_retry
= TRUE
;
6086 * There's really nothing for us right now; inform the
6087 * cache(s) that there is a waiter below and go to sleep.
6089 mbuf_waiter_inc(class, (wait
& MCR_COMP
));
6091 VERIFY(!(wait
& MCR_NOSLEEP
));
6094 * If this is the first waiter, arm the watchdog timer. Otherwise
6095 * check if we need to panic the system due to watchdog timeout.
6097 if (mb_waiters
== 0)
6098 microuptime(&mb_wdtstart
);
6103 (void) msleep(mb_waitchan
, mbuf_mlock
, (PZERO
-1), m_cname(class), NULL
);
6105 /* We are now up; stop getting notified until next round */
6106 mbuf_waiter_dec(class, (wait
& MCR_COMP
));
6108 /* We waited and got something */
6109 if (m_infree(class) > 0) {
6112 } else if (mbuf_cached_above(class, wait
)) {
6114 mcache_retry
= TRUE
;
6117 return (mcache_retry
);
6121 mbuf_worker_thread(void)
6126 lck_mtx_lock(mbuf_mlock
);
6129 if (mbuf_expand_mcl
) {
6132 /* Adjust to current number of cluster in use */
6133 n
= mbuf_expand_mcl
-
6134 (m_total(MC_CL
) - m_infree(MC_CL
));
6135 if ((n
+ m_total(MC_CL
)) > m_maxlimit(MC_CL
))
6136 n
= m_maxlimit(MC_CL
) - m_total(MC_CL
);
6137 mbuf_expand_mcl
= 0;
6139 if (n
> 0 && freelist_populate(MC_CL
, n
, M_WAIT
) > 0)
6142 if (mbuf_expand_big
) {
6145 /* Adjust to current number of 4 KB cluster in use */
6146 n
= mbuf_expand_big
-
6147 (m_total(MC_BIGCL
) - m_infree(MC_BIGCL
));
6148 if ((n
+ m_total(MC_BIGCL
)) > m_maxlimit(MC_BIGCL
))
6149 n
= m_maxlimit(MC_BIGCL
) - m_total(MC_BIGCL
);
6150 mbuf_expand_big
= 0;
6152 if (n
> 0 && freelist_populate(MC_BIGCL
, n
, M_WAIT
) > 0)
6155 if (mbuf_expand_16k
) {
6158 /* Adjust to current number of 16 KB cluster in use */
6159 n
= mbuf_expand_16k
-
6160 (m_total(MC_16KCL
) - m_infree(MC_16KCL
));
6161 if ((n
+ m_total(MC_16KCL
)) > m_maxlimit(MC_16KCL
))
6162 n
= m_maxlimit(MC_16KCL
) - m_total(MC_16KCL
);
6163 mbuf_expand_16k
= 0;
6166 (void) freelist_populate(MC_16KCL
, n
, M_WAIT
);
6170 * Because we can run out of memory before filling the mbuf
6171 * map, we should not allocate more clusters than they are
6172 * mbufs -- otherwise we could have a large number of useless
6173 * clusters allocated.
6176 while (m_total(MC_MBUF
) <
6177 (m_total(MC_BIGCL
) + m_total(MC_CL
))) {
6178 if (freelist_populate(MC_MBUF
, 1, M_WAIT
) == 0)
6183 lck_mtx_unlock(mbuf_mlock
);
6185 assert_wait(&mbuf_worker_run
, THREAD_UNINT
);
6186 (void) thread_block((thread_continue_t
)mbuf_worker_thread
);
6191 mbuf_worker_thread_init(void)
6193 mbuf_worker_ready
++;
6194 mbuf_worker_thread();
6203 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
6205 VERIFY(MBUF_IN_MAP(buf
));
6206 ix
= ((char *)buf
- (char *)mbutl
) >> MBSHIFT
;
6207 VERIFY(ix
< maxslabgrp
);
6209 if ((slg
= slabstbl
[ix
]) == NULL
) {
6211 * In the current implementation, we never shrink the memory
6212 * pool (hence the cluster map); if we attempt to reallocate
6213 * a cluster group when it's already allocated, panic since
6214 * this is a sign of a memory corruption (slabstbl[ix] got
6215 * nullified). This also means that there shouldn't be any
6216 * hole in the kernel sub-map for the mbuf pool.
6219 VERIFY(ix
< slabgrp
);
6221 * Slabs expansion can only be done single threaded; when
6222 * we get here, it must be as a result of m_clalloc() which
6223 * is serialized and therefore mb_clalloc_busy must be set.
6225 VERIFY(mb_clalloc_busy
);
6226 lck_mtx_unlock(mbuf_mlock
);
6228 /* This is a new buffer; create the slabs group for it */
6229 MALLOC(slg
, mcl_slabg_t
*, sizeof (*slg
), M_TEMP
,
6231 VERIFY(slg
!= NULL
);
6233 lck_mtx_lock(mbuf_mlock
);
6235 * No other thread could have gone into m_clalloc() after
6236 * we dropped the lock above, so verify that it's true.
6238 VERIFY(mb_clalloc_busy
);
6242 /* Chain each slab in the group to its forward neighbor */
6243 for (k
= 1; k
< NSLABSPMB
; k
++)
6244 slg
->slg_slab
[k
- 1].sl_next
= &slg
->slg_slab
[k
];
6245 VERIFY(slg
->slg_slab
[NSLABSPMB
- 1].sl_next
== NULL
);
6247 /* And chain the last slab in the previous group to this */
6249 VERIFY(slabstbl
[ix
- 1]->
6250 slg_slab
[NSLABSPMB
- 1].sl_next
== NULL
);
6251 slabstbl
[ix
- 1]->slg_slab
[NSLABSPMB
- 1].sl_next
=
6256 ix
= MTOBG(buf
) % NSLABSPMB
;
6257 VERIFY(ix
< NSLABSPMB
);
6259 return (&slg
->slg_slab
[ix
]);
6263 slab_init(mcl_slab_t
*sp
, mbuf_class_t
class, u_int32_t flags
,
6264 void *base
, void *head
, unsigned int len
, int refcnt
, int chunks
)
6266 sp
->sl_class
= class;
6267 sp
->sl_flags
= flags
;
6271 sp
->sl_refcnt
= refcnt
;
6272 sp
->sl_chunks
= chunks
;
6277 slab_insert(mcl_slab_t
*sp
, mbuf_class_t
class)
6279 VERIFY(slab_is_detached(sp
));
6280 m_slab_cnt(class)++;
6281 TAILQ_INSERT_TAIL(&m_slablist(class), sp
, sl_link
);
6282 sp
->sl_flags
&= ~SLF_DETACHED
;
6283 if (class == MC_16KCL
) {
6285 for (k
= 1; k
< NSLABSP16KB
; k
++) {
6287 /* Next slab must already be present */
6289 VERIFY(slab_is_detached(sp
));
6290 sp
->sl_flags
&= ~SLF_DETACHED
;
6296 slab_remove(mcl_slab_t
*sp
, mbuf_class_t
class)
6298 VERIFY(!slab_is_detached(sp
));
6299 VERIFY(m_slab_cnt(class) > 0);
6300 m_slab_cnt(class)--;
6301 TAILQ_REMOVE(&m_slablist(class), sp
, sl_link
);
6303 if (class == MC_16KCL
) {
6305 for (k
= 1; k
< NSLABSP16KB
; k
++) {
6307 /* Next slab must already be present */
6309 VERIFY(!slab_is_detached(sp
));
6316 slab_inrange(mcl_slab_t
*sp
, void *buf
)
6318 return ((uintptr_t)buf
>= (uintptr_t)sp
->sl_base
&&
6319 (uintptr_t)buf
< ((uintptr_t)sp
->sl_base
+ sp
->sl_len
));
6325 slab_nextptr_panic(mcl_slab_t
*sp
, void *addr
)
6328 unsigned int chunk_len
= sp
->sl_len
/ sp
->sl_chunks
;
6329 uintptr_t buf
= (uintptr_t)sp
->sl_base
;
6331 for (i
= 0; i
< sp
->sl_chunks
; i
++, buf
+= chunk_len
) {
6332 void *next
= ((mcache_obj_t
*)buf
)->obj_next
;
6336 if (next
!= NULL
&& !MBUF_IN_MAP(next
)) {
6337 mcache_t
*cp
= m_cache(sp
->sl_class
);
6338 panic("%s: %s buffer %p in slab %p modified "
6339 "after free at offset 0: %p out of range "
6340 "[%p-%p)\n", __func__
, cp
->mc_name
,
6341 (void *)buf
, sp
, next
, mbutl
, embutl
);
6345 mcache_audit_t
*mca
= mcl_audit_buf2mca(sp
->sl_class
,
6346 (mcache_obj_t
*)buf
);
6347 mcl_audit_verify_nextptr(next
, mca
);
6353 slab_detach(mcl_slab_t
*sp
)
6355 sp
->sl_link
.tqe_next
= (mcl_slab_t
*)-1;
6356 sp
->sl_link
.tqe_prev
= (mcl_slab_t
**)-1;
6357 sp
->sl_flags
|= SLF_DETACHED
;
6361 slab_is_detached(mcl_slab_t
*sp
)
6363 return ((intptr_t)sp
->sl_link
.tqe_next
== -1 &&
6364 (intptr_t)sp
->sl_link
.tqe_prev
== -1 &&
6365 (sp
->sl_flags
& SLF_DETACHED
));
6369 mcl_audit_init(void *buf
, mcache_audit_t
**mca_list
,
6370 mcache_obj_t
**con_list
, size_t con_size
, unsigned int num
)
6372 mcache_audit_t
*mca
, *mca_tail
;
6373 mcache_obj_t
*con
= NULL
;
6374 boolean_t save_contents
= (con_list
!= NULL
);
6377 ASSERT(num
<= NMBPBG
);
6378 ASSERT(con_list
== NULL
|| con_size
!= 0);
6381 VERIFY(ix
< maxclaudit
);
6383 /* Make sure we haven't been here before */
6384 for (i
= 0; i
< NMBPBG
; i
++)
6385 VERIFY(mclaudit
[ix
].cl_audit
[i
] == NULL
);
6387 mca
= mca_tail
= *mca_list
;
6391 for (i
= 0; i
< num
; i
++) {
6392 mcache_audit_t
*next
;
6394 next
= mca
->mca_next
;
6395 bzero(mca
, sizeof (*mca
));
6396 mca
->mca_next
= next
;
6397 mclaudit
[ix
].cl_audit
[i
] = mca
;
6399 /* Attach the contents buffer if requested */
6400 if (save_contents
) {
6401 mcl_saved_contents_t
*msc
=
6402 (mcl_saved_contents_t
*)(void *)con
;
6404 VERIFY(msc
!= NULL
);
6405 VERIFY(IS_P2ALIGNED(msc
, sizeof (u_int64_t
)));
6406 VERIFY(con_size
== sizeof (*msc
));
6407 mca
->mca_contents_size
= con_size
;
6408 mca
->mca_contents
= msc
;
6409 con
= con
->obj_next
;
6410 bzero(mca
->mca_contents
, mca
->mca_contents_size
);
6414 mca
= mca
->mca_next
;
6420 *mca_list
= mca_tail
->mca_next
;
6421 mca_tail
->mca_next
= NULL
;
6425 * Given an address of a buffer (mbuf/2KB/4KB/16KB), return
6426 * the corresponding audit structure for that buffer.
6428 static mcache_audit_t
*
6429 mcl_audit_buf2mca(mbuf_class_t
class, mcache_obj_t
*o
)
6431 mcache_audit_t
*mca
= NULL
;
6434 VERIFY(ix
< maxclaudit
);
6435 VERIFY(IS_P2ALIGNED(o
, MIN(m_maxsize(class), NBPG
)));
6440 * For the mbuf case, find the index of the page
6441 * used by the mbuf and use that index to locate the
6442 * base address of the page. Then find out the
6443 * mbuf index relative to the page base and use
6444 * it to locate the audit structure.
6446 VERIFY(MCLIDX(BGTOM(ix
), o
) < (int)NMBPBG
);
6447 mca
= mclaudit
[ix
].cl_audit
[MCLIDX(BGTOM(ix
), o
)];
6452 * Same thing as above, but for 2KB clusters in a page.
6454 VERIFY(CLBGIDX(BGTOM(ix
), o
) < (int)NCLPBG
);
6455 mca
= mclaudit
[ix
].cl_audit
[CLBGIDX(BGTOM(ix
), o
)];
6461 * Same as above, but only return the first element.
6463 mca
= mclaudit
[ix
].cl_audit
[0];
6475 mcl_audit_mbuf(mcache_audit_t
*mca
, void *addr
, boolean_t composite
,
6478 struct mbuf
*m
= addr
;
6479 mcache_obj_t
*next
= ((mcache_obj_t
*)m
)->obj_next
;
6481 VERIFY(mca
->mca_contents
!= NULL
&&
6482 mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
6485 mcl_audit_verify_nextptr(next
, mca
);
6488 /* Save constructed mbuf fields */
6489 mcl_audit_save_mbuf(m
, mca
);
6491 mcache_set_pattern(MCACHE_FREE_PATTERN
, m
,
6492 m_maxsize(MC_MBUF
));
6494 ((mcache_obj_t
*)m
)->obj_next
= next
;
6498 /* Check if the buffer has been corrupted while in freelist */
6500 mcache_audit_free_verify_set(mca
, addr
, 0, m_maxsize(MC_MBUF
));
6502 /* Restore constructed mbuf fields */
6503 mcl_audit_restore_mbuf(m
, mca
, composite
);
6507 mcl_audit_restore_mbuf(struct mbuf
*m
, mcache_audit_t
*mca
, boolean_t composite
)
6509 struct mbuf
*ms
= MCA_SAVED_MBUF_PTR(mca
);
6512 struct mbuf
*next
= m
->m_next
;
6513 VERIFY(ms
->m_flags
== M_EXT
&& MEXT_RFA(ms
) != NULL
&&
6514 MBUF_IS_COMPOSITE(ms
));
6515 VERIFY(mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
6517 * We could have hand-picked the mbuf fields and restore
6518 * them individually, but that will be a maintenance
6519 * headache. Instead, restore everything that was saved;
6520 * the mbuf layer will recheck and reinitialize anyway.
6522 bcopy(ms
, m
, MCA_SAVED_MBUF_SIZE
);
6526 * For a regular mbuf (no cluster attached) there's nothing
6527 * to restore other than the type field, which is expected
6530 m
->m_type
= ms
->m_type
;
6536 mcl_audit_save_mbuf(struct mbuf
*m
, mcache_audit_t
*mca
)
6538 VERIFY(mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
6540 bcopy(m
, MCA_SAVED_MBUF_PTR(mca
), MCA_SAVED_MBUF_SIZE
);
6544 mcl_audit_cluster(mcache_audit_t
*mca
, void *addr
, size_t size
, boolean_t alloc
,
6545 boolean_t save_next
)
6547 mcache_obj_t
*next
= ((mcache_obj_t
*)addr
)->obj_next
;
6551 mcache_set_pattern(MCACHE_FREE_PATTERN
, addr
, size
);
6554 mcl_audit_verify_nextptr(next
, mca
);
6555 ((mcache_obj_t
*)addr
)->obj_next
= next
;
6557 } else if (mclverify
) {
6558 /* Check if the buffer has been corrupted while in freelist */
6559 mcl_audit_verify_nextptr(next
, mca
);
6560 mcache_audit_free_verify_set(mca
, addr
, 0, size
);
6565 mcl_audit_scratch(mcache_audit_t
*mca
)
6567 void *stack
[MCACHE_STACK_DEPTH
+ 1];
6568 mcl_scratch_audit_t
*msa
;
6571 VERIFY(mca
->mca_contents
!= NULL
);
6572 msa
= MCA_SAVED_SCRATCH_PTR(mca
);
6574 msa
->msa_pthread
= msa
->msa_thread
;
6575 msa
->msa_thread
= current_thread();
6576 bcopy(msa
->msa_stack
, msa
->msa_pstack
, sizeof (msa
->msa_pstack
));
6577 msa
->msa_pdepth
= msa
->msa_depth
;
6578 bzero(stack
, sizeof (stack
));
6579 msa
->msa_depth
= OSBacktrace(stack
, MCACHE_STACK_DEPTH
+ 1) - 1;
6580 bcopy(&stack
[1], msa
->msa_stack
, sizeof (mca
->mca_pstack
));
6582 msa
->msa_ptstamp
= msa
->msa_tstamp
;
6584 /* tstamp is in ms relative to base_ts */
6585 msa
->msa_tstamp
= ((now
.tv_usec
- mb_start
.tv_usec
) / 1000);
6586 if ((now
.tv_sec
- mb_start
.tv_sec
) > 0)
6587 msa
->msa_tstamp
+= ((now
.tv_sec
- mb_start
.tv_sec
) * 1000);
6591 mcl_audit_mcheck_panic(struct mbuf
*m
)
6593 mcache_audit_t
*mca
;
6596 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
6598 panic("mcl_audit: freed mbuf %p with type 0x%x (instead of 0x%x)\n%s\n",
6599 m
, (u_int16_t
)m
->m_type
, MT_FREE
, mcache_dump_mca(mca
));
6604 mcl_audit_verify_nextptr(void *next
, mcache_audit_t
*mca
)
6606 if (next
!= NULL
&& !MBUF_IN_MAP(next
) &&
6607 (next
!= (void *)MCACHE_FREE_PATTERN
|| !mclverify
)) {
6608 panic("mcl_audit: buffer %p modified after free at offset 0: "
6609 "%p out of range [%p-%p)\n%s\n",
6610 mca
->mca_addr
, next
, mbutl
, embutl
, mcache_dump_mca(mca
));
6615 /* This function turns on mbuf leak detection */
6617 mleak_activate(void)
6619 mleak_table
.mleak_sample_factor
= MLEAK_SAMPLE_FACTOR
;
6620 PE_parse_boot_argn("mleak_sample_factor",
6621 &mleak_table
.mleak_sample_factor
,
6622 sizeof (mleak_table
.mleak_sample_factor
));
6624 if (mleak_table
.mleak_sample_factor
== 0)
6627 if (mclfindleak
== 0)
6630 vm_size_t alloc_size
=
6631 mleak_alloc_buckets
* sizeof (struct mallocation
);
6632 vm_size_t trace_size
= mleak_trace_buckets
* sizeof (struct mtrace
);
6634 MALLOC(mleak_allocations
, struct mallocation
*, alloc_size
,
6635 M_TEMP
, M_WAITOK
| M_ZERO
);
6636 VERIFY(mleak_allocations
!= NULL
);
6638 MALLOC(mleak_traces
, struct mtrace
*, trace_size
,
6639 M_TEMP
, M_WAITOK
| M_ZERO
);
6640 VERIFY(mleak_traces
!= NULL
);
6642 MALLOC(mleak_stat
, mleak_stat_t
*, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES
),
6643 M_TEMP
, M_WAITOK
| M_ZERO
);
6644 VERIFY(mleak_stat
!= NULL
);
6645 mleak_stat
->ml_cnt
= MLEAK_NUM_TRACES
;
6647 mleak_stat
->ml_isaddr64
= 1;
6648 #endif /* __LP64__ */
6652 mleak_logger(u_int32_t num
, mcache_obj_t
*addr
, boolean_t alloc
)
6656 if (mclfindleak
== 0)
6660 return (mleak_free(addr
));
6662 temp
= atomic_add_32_ov(&mleak_table
.mleak_capture
, 1);
6664 if ((temp
% mleak_table
.mleak_sample_factor
) == 0 && addr
!= NULL
) {
6665 uintptr_t bt
[MLEAK_STACK_DEPTH
];
6666 int logged
= fastbacktrace(bt
, MLEAK_STACK_DEPTH
);
6667 mleak_log(bt
, addr
, logged
, num
);
6672 * This function records the allocation in the mleak_allocations table
6673 * and the backtrace in the mleak_traces table; if allocation slot is in use,
6674 * replace old allocation with new one if the trace slot is in use, return
6675 * (or increment refcount if same trace).
6678 mleak_log(uintptr_t *bt
, mcache_obj_t
*addr
, uint32_t depth
, int num
)
6680 struct mallocation
*allocation
;
6681 struct mtrace
*trace
;
6682 uint32_t trace_index
;
6684 /* Quit if someone else modifying the tables */
6685 if (!lck_mtx_try_lock_spin(mleak_lock
)) {
6686 mleak_table
.total_conflicts
++;
6690 allocation
= &mleak_allocations
[hashaddr((uintptr_t)addr
,
6691 mleak_alloc_buckets
)];
6692 trace_index
= hashbacktrace(bt
, depth
, mleak_trace_buckets
);
6693 trace
= &mleak_traces
[trace_index
];
6695 VERIFY(allocation
<= &mleak_allocations
[mleak_alloc_buckets
- 1]);
6696 VERIFY(trace
<= &mleak_traces
[mleak_trace_buckets
- 1]);
6698 allocation
->hitcount
++;
6702 * If the allocation bucket we want is occupied
6703 * and the occupier has the same trace, just bail.
6705 if (allocation
->element
!= NULL
&&
6706 trace_index
== allocation
->trace_index
) {
6707 mleak_table
.alloc_collisions
++;
6708 lck_mtx_unlock(mleak_lock
);
6713 * Store the backtrace in the traces array;
6714 * Size of zero = trace bucket is free.
6716 if (trace
->allocs
> 0 &&
6717 bcmp(trace
->addr
, bt
, (depth
* sizeof (uintptr_t))) != 0) {
6718 /* Different, unique trace, but the same hash! Bail out. */
6719 trace
->collisions
++;
6720 mleak_table
.trace_collisions
++;
6721 lck_mtx_unlock(mleak_lock
);
6723 } else if (trace
->allocs
> 0) {
6724 /* Same trace, already added, so increment refcount */
6727 /* Found an unused trace bucket, so record the trace here */
6728 if (trace
->depth
!= 0) {
6729 /* this slot previously used but not currently in use */
6730 mleak_table
.trace_overwrites
++;
6732 mleak_table
.trace_recorded
++;
6734 memcpy(trace
->addr
, bt
, (depth
* sizeof (uintptr_t)));
6735 trace
->depth
= depth
;
6736 trace
->collisions
= 0;
6739 /* Step 2: Store the allocation record in the allocations array */
6740 if (allocation
->element
!= NULL
) {
6742 * Replace an existing allocation. No need to preserve
6743 * because only a subset of the allocations are being
6746 mleak_table
.alloc_collisions
++;
6747 } else if (allocation
->trace_index
!= 0) {
6748 mleak_table
.alloc_overwrites
++;
6750 allocation
->element
= addr
;
6751 allocation
->trace_index
= trace_index
;
6752 allocation
->count
= num
;
6753 mleak_table
.alloc_recorded
++;
6754 mleak_table
.outstanding_allocs
++;
6756 lck_mtx_unlock(mleak_lock
);
6761 mleak_free(mcache_obj_t
*addr
)
6763 while (addr
!= NULL
) {
6764 struct mallocation
*allocation
= &mleak_allocations
6765 [hashaddr((uintptr_t)addr
, mleak_alloc_buckets
)];
6767 if (allocation
->element
== addr
&&
6768 allocation
->trace_index
< mleak_trace_buckets
) {
6769 lck_mtx_lock_spin(mleak_lock
);
6770 if (allocation
->element
== addr
&&
6771 allocation
->trace_index
< mleak_trace_buckets
) {
6772 struct mtrace
*trace
;
6773 trace
= &mleak_traces
[allocation
->trace_index
];
6774 /* allocs = 0 means trace bucket is unused */
6775 if (trace
->allocs
> 0)
6777 if (trace
->allocs
== 0)
6779 /* NULL element means alloc bucket is unused */
6780 allocation
->element
= NULL
;
6781 mleak_table
.outstanding_allocs
--;
6783 lck_mtx_unlock(mleak_lock
);
6785 addr
= addr
->obj_next
;
6793 struct mtrace
*swap
;
6795 for(i
= 0; i
< MLEAK_NUM_TRACES
; i
++)
6796 mleak_top_trace
[i
] = NULL
;
6798 for(i
= 0, j
= 0; j
< MLEAK_NUM_TRACES
&& i
< mleak_trace_buckets
; i
++)
6800 if (mleak_traces
[i
].allocs
<= 0)
6803 mleak_top_trace
[j
] = &mleak_traces
[i
];
6804 for (k
= j
; k
> 0; k
--) {
6805 if (mleak_top_trace
[k
]->allocs
<=
6806 mleak_top_trace
[k
-1]->allocs
)
6809 swap
= mleak_top_trace
[k
-1];
6810 mleak_top_trace
[k
-1] = mleak_top_trace
[k
];
6811 mleak_top_trace
[k
] = swap
;
6817 for(; i
< mleak_trace_buckets
; i
++) {
6818 if (mleak_traces
[i
].allocs
<= mleak_top_trace
[j
]->allocs
)
6821 mleak_top_trace
[j
] = &mleak_traces
[i
];
6823 for (k
= j
; k
> 0; k
--) {
6824 if (mleak_top_trace
[k
]->allocs
<=
6825 mleak_top_trace
[k
-1]->allocs
)
6828 swap
= mleak_top_trace
[k
-1];
6829 mleak_top_trace
[k
-1] = mleak_top_trace
[k
];
6830 mleak_top_trace
[k
] = swap
;
6836 mleak_update_stats()
6838 mleak_trace_stat_t
*mltr
;
6841 VERIFY(mleak_stat
!= NULL
);
6843 VERIFY(mleak_stat
->ml_isaddr64
);
6845 VERIFY(!mleak_stat
->ml_isaddr64
);
6846 #endif /* !__LP64__ */
6847 VERIFY(mleak_stat
->ml_cnt
== MLEAK_NUM_TRACES
);
6849 mleak_sort_traces();
6851 mltr
= &mleak_stat
->ml_trace
[0];
6852 bzero(mltr
, sizeof (*mltr
) * MLEAK_NUM_TRACES
);
6853 for (i
= 0; i
< MLEAK_NUM_TRACES
; i
++) {
6856 if (mleak_top_trace
[i
] == NULL
||
6857 mleak_top_trace
[i
]->allocs
== 0)
6860 mltr
->mltr_collisions
= mleak_top_trace
[i
]->collisions
;
6861 mltr
->mltr_hitcount
= mleak_top_trace
[i
]->hitcount
;
6862 mltr
->mltr_allocs
= mleak_top_trace
[i
]->allocs
;
6863 mltr
->mltr_depth
= mleak_top_trace
[i
]->depth
;
6865 VERIFY(mltr
->mltr_depth
<= MLEAK_STACK_DEPTH
);
6866 for (j
= 0; j
< mltr
->mltr_depth
; j
++)
6867 mltr
->mltr_addr
[j
] = mleak_top_trace
[i
]->addr
[j
];
6873 static struct mbtypes
{
6875 const char *mt_name
;
6877 { MT_DATA
, "data" },
6878 { MT_OOBDATA
, "oob data" },
6879 { MT_CONTROL
, "ancillary data" },
6880 { MT_HEADER
, "packet headers" },
6881 { MT_SOCKET
, "socket structures" },
6882 { MT_PCB
, "protocol control blocks" },
6883 { MT_RTABLE
, "routing table entries" },
6884 { MT_HTABLE
, "IMP host table entries" },
6885 { MT_ATABLE
, "address resolution tables" },
6886 { MT_FTABLE
, "fragment reassembly queue headers" },
6887 { MT_SONAME
, "socket names and addresses" },
6888 { MT_SOOPTS
, "socket options" },
6889 { MT_RIGHTS
, "access rights" },
6890 { MT_IFADDR
, "interface addresses" },
6891 { MT_TAG
, "packet tags" },
6895 #define MBUF_DUMP_BUF_CHK() { \
6905 unsigned long totmem
= 0, totfree
= 0, totmbufs
, totused
, totpct
;
6906 u_int32_t m_mbufs
= 0, m_clfree
= 0, m_bigclfree
= 0;
6907 u_int32_t m_mbufclfree
= 0, m_mbufbigclfree
= 0;
6908 u_int32_t m_16kclusters
= 0, m_16kclfree
= 0, m_mbuf16kclfree
= 0;
6909 int nmbtypes
= sizeof (mbstat
.m_mtypes
) / sizeof (short);
6912 mb_class_stat_t
*sp
;
6913 mleak_trace_stat_t
*mltr
;
6914 char *c
= mbuf_dump_buf
;
6915 int i
, k
, clen
= MBUF_DUMP_BUF_SIZE
;
6917 mbuf_dump_buf
[0] = '\0';
6919 /* synchronize all statistics in the mbuf table */
6921 mbuf_mtypes_sync(TRUE
);
6923 sp
= &mb_stat
->mbs_class
[0];
6924 for (i
= 0; i
< mb_stat
->mbs_cnt
; i
++, sp
++) {
6927 if (m_class(i
) == MC_MBUF
) {
6928 m_mbufs
= sp
->mbcl_active
;
6929 } else if (m_class(i
) == MC_CL
) {
6930 m_clfree
= sp
->mbcl_total
- sp
->mbcl_active
;
6931 } else if (m_class(i
) == MC_BIGCL
) {
6932 m_bigclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
6933 } else if (njcl
> 0 && m_class(i
) == MC_16KCL
) {
6934 m_16kclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
6935 m_16kclusters
= sp
->mbcl_total
;
6936 } else if (m_class(i
) == MC_MBUF_CL
) {
6937 m_mbufclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
6938 } else if (m_class(i
) == MC_MBUF_BIGCL
) {
6939 m_mbufbigclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
6940 } else if (njcl
> 0 && m_class(i
) == MC_MBUF_16KCL
) {
6941 m_mbuf16kclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
6944 mem
= sp
->mbcl_ctotal
* sp
->mbcl_size
;
6946 totfree
+= (sp
->mbcl_mc_cached
+ sp
->mbcl_infree
) *
6951 /* adjust free counts to include composite caches */
6952 m_clfree
+= m_mbufclfree
;
6953 m_bigclfree
+= m_mbufbigclfree
;
6954 m_16kclfree
+= m_mbuf16kclfree
;
6957 for (mp
= mbtypes
; mp
->mt_name
!= NULL
; mp
++)
6958 totmbufs
+= mbstat
.m_mtypes
[mp
->mt_type
];
6959 if (totmbufs
> m_mbufs
)
6961 k
= snprintf(c
, clen
, "%lu/%u mbufs in use:\n", totmbufs
, m_mbufs
);
6962 MBUF_DUMP_BUF_CHK();
6964 bzero(&seen
, sizeof (seen
));
6965 for (mp
= mbtypes
; mp
->mt_name
!= NULL
; mp
++) {
6966 if (mbstat
.m_mtypes
[mp
->mt_type
] != 0) {
6967 seen
[mp
->mt_type
] = 1;
6968 k
= snprintf(c
, clen
, "\t%u mbufs allocated to %s\n",
6969 mbstat
.m_mtypes
[mp
->mt_type
], mp
->mt_name
);
6970 MBUF_DUMP_BUF_CHK();
6974 for (i
= 0; i
< nmbtypes
; i
++)
6975 if (!seen
[i
] && mbstat
.m_mtypes
[i
] != 0) {
6976 k
= snprintf(c
, clen
, "\t%u mbufs allocated to "
6977 "<mbuf type %d>\n", mbstat
.m_mtypes
[i
], i
);
6978 MBUF_DUMP_BUF_CHK();
6980 if ((m_mbufs
- totmbufs
) > 0) {
6981 k
= snprintf(c
, clen
, "\t%lu mbufs allocated to caches\n",
6982 m_mbufs
- totmbufs
);
6983 MBUF_DUMP_BUF_CHK();
6985 k
= snprintf(c
, clen
, "%u/%u mbuf 2KB clusters in use\n"
6986 "%u/%u mbuf 4KB clusters in use\n",
6987 (unsigned int)(mbstat
.m_clusters
- m_clfree
),
6988 (unsigned int)mbstat
.m_clusters
,
6989 (unsigned int)(mbstat
.m_bigclusters
- m_bigclfree
),
6990 (unsigned int)mbstat
.m_bigclusters
);
6991 MBUF_DUMP_BUF_CHK();
6994 k
= snprintf(c
, clen
, "%u/%u mbuf %uKB clusters in use\n",
6995 m_16kclusters
- m_16kclfree
, m_16kclusters
,
6997 MBUF_DUMP_BUF_CHK();
6999 totused
= totmem
- totfree
;
7002 } else if (totused
< (ULONG_MAX
/ 100)) {
7003 totpct
= (totused
* 100) / totmem
;
7005 u_long totmem1
= totmem
/ 100;
7006 u_long totused1
= totused
/ 100;
7007 totpct
= (totused1
* 100) / totmem1
;
7009 k
= snprintf(c
, clen
, "%lu KB allocated to network (approx. %lu%% "
7010 "in use)\n", totmem
/ 1024, totpct
);
7011 MBUF_DUMP_BUF_CHK();
7013 /* mbuf leak detection statistics */
7014 mleak_update_stats();
7016 k
= snprintf(c
, clen
, "\nmbuf leak detection table:\n");
7017 MBUF_DUMP_BUF_CHK();
7018 k
= snprintf(c
, clen
, "\ttotal captured: %u (one per %u)\n",
7019 mleak_table
.mleak_capture
/ mleak_table
.mleak_sample_factor
,
7020 mleak_table
.mleak_sample_factor
);
7021 MBUF_DUMP_BUF_CHK();
7022 k
= snprintf(c
, clen
, "\ttotal allocs outstanding: %llu\n",
7023 mleak_table
.outstanding_allocs
);
7024 MBUF_DUMP_BUF_CHK();
7025 k
= snprintf(c
, clen
, "\tnew hash recorded: %llu allocs, %llu traces\n",
7026 mleak_table
.alloc_recorded
, mleak_table
.trace_recorded
);
7027 MBUF_DUMP_BUF_CHK();
7028 k
= snprintf(c
, clen
, "\thash collisions: %llu allocs, %llu traces\n",
7029 mleak_table
.alloc_collisions
, mleak_table
.trace_collisions
);
7030 MBUF_DUMP_BUF_CHK();
7031 k
= snprintf(c
, clen
, "\toverwrites: %llu allocs, %llu traces\n",
7032 mleak_table
.alloc_overwrites
, mleak_table
.trace_overwrites
);
7033 MBUF_DUMP_BUF_CHK();
7034 k
= snprintf(c
, clen
, "\tlock conflicts: %llu\n\n",
7035 mleak_table
.total_conflicts
);
7036 MBUF_DUMP_BUF_CHK();
7038 k
= snprintf(c
, clen
, "top %d outstanding traces:\n",
7039 mleak_stat
->ml_cnt
);
7040 MBUF_DUMP_BUF_CHK();
7041 for (i
= 0; i
< mleak_stat
->ml_cnt
; i
++) {
7042 mltr
= &mleak_stat
->ml_trace
[i
];
7043 k
= snprintf(c
, clen
, "[%d] %llu outstanding alloc(s), "
7044 "%llu hit(s), %llu collision(s)\n", (i
+ 1),
7045 mltr
->mltr_allocs
, mltr
->mltr_hitcount
,
7046 mltr
->mltr_collisions
);
7047 MBUF_DUMP_BUF_CHK();
7050 if (mleak_stat
->ml_isaddr64
)
7051 k
= snprintf(c
, clen
, MB_LEAK_HDR_64
);
7053 k
= snprintf(c
, clen
, MB_LEAK_HDR_32
);
7054 MBUF_DUMP_BUF_CHK();
7056 for (i
= 0; i
< MLEAK_STACK_DEPTH
; i
++) {
7058 k
= snprintf(c
, clen
, "%2d: ", (i
+ 1));
7059 MBUF_DUMP_BUF_CHK();
7060 for (j
= 0; j
< mleak_stat
->ml_cnt
; j
++) {
7061 mltr
= &mleak_stat
->ml_trace
[j
];
7062 if (i
< mltr
->mltr_depth
) {
7063 if (mleak_stat
->ml_isaddr64
) {
7064 k
= snprintf(c
, clen
, "0x%0llx ",
7065 mltr
->mltr_addr
[i
]);
7067 k
= snprintf(c
, clen
,
7069 (u_int32_t
)mltr
->mltr_addr
[i
]);
7072 if (mleak_stat
->ml_isaddr64
)
7073 k
= snprintf(c
, clen
,
7074 MB_LEAK_SPACING_64
);
7076 k
= snprintf(c
, clen
,
7077 MB_LEAK_SPACING_32
);
7079 MBUF_DUMP_BUF_CHK();
7081 k
= snprintf(c
, clen
, "\n");
7082 MBUF_DUMP_BUF_CHK();
7085 return (mbuf_dump_buf
);
7088 #undef MBUF_DUMP_BUF_CHK
7091 * Convert between a regular and a packet header mbuf. Caller is responsible
7092 * for setting or clearing M_PKTHDR; this routine does the rest of the work.
7095 m_reinit(struct mbuf
*m
, int hdr
)
7100 VERIFY(!(m
->m_flags
& M_PKTHDR
));
7101 if (!(m
->m_flags
& M_EXT
) &&
7102 (m
->m_data
!= m
->m_dat
|| m
->m_len
> 0)) {
7104 * If there's no external cluster attached and the
7105 * mbuf appears to contain user data, we cannot
7106 * safely convert this to a packet header mbuf,
7107 * as the packet header structure might overlap
7110 printf("%s: cannot set M_PKTHDR on altered mbuf %p, "
7111 "m_data %p (expected %p), m_len %d (expected 0)\n",
7112 __func__
, m
, m
->m_data
, m
->m_dat
, m
->m_len
);
7115 VERIFY((m
->m_flags
& M_EXT
) || m
->m_data
== m
->m_dat
);
7116 m
->m_flags
|= M_PKTHDR
;
7117 MBUF_INIT_PKTHDR(m
);
7120 /* Check for scratch area overflow */
7121 m_redzone_verify(m
);
7122 /* Free the aux data and tags if there is any */
7123 m_tag_delete_chain(m
, NULL
);
7124 m
->m_flags
&= ~M_PKTHDR
;
7131 m_scratch_init(struct mbuf
*m
)
7133 VERIFY(m
->m_flags
& M_PKTHDR
);
7135 bzero(&m
->m_pkthdr
.pkt_mpriv
, sizeof (m
->m_pkthdr
.pkt_mpriv
));
7139 m_scratch_get(struct mbuf
*m
, u_int8_t
**p
)
7141 VERIFY(m
->m_flags
& M_PKTHDR
);
7144 mcache_audit_t
*mca
;
7146 lck_mtx_lock(mbuf_mlock
);
7147 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
7148 if (mca
->mca_uflags
& MB_SCVALID
)
7149 mcl_audit_scratch(mca
);
7150 lck_mtx_unlock(mbuf_mlock
);
7153 *p
= (u_int8_t
*)&m
->m_pkthdr
.pkt_mpriv
;
7154 return (sizeof (m
->m_pkthdr
.pkt_mpriv
));
7158 m_redzone_init(struct mbuf
*m
)
7160 VERIFY(m
->m_flags
& M_PKTHDR
);
7162 * Each mbuf has a unique red zone pattern, which is a XOR
7163 * of the red zone cookie and the address of the mbuf.
7165 m
->m_pkthdr
.redzone
= ((u_int32_t
)(uintptr_t)m
) ^ mb_redzone_cookie
;
7169 m_redzone_verify(struct mbuf
*m
)
7171 u_int32_t mb_redzone
;
7173 VERIFY(m
->m_flags
& M_PKTHDR
);
7175 mb_redzone
= ((u_int32_t
)(uintptr_t)m
) ^ mb_redzone_cookie
;
7176 if (m
->m_pkthdr
.redzone
!= mb_redzone
) {
7177 panic("mbuf %p redzone violation with value 0x%x "
7178 "(instead of 0x%x, using cookie 0x%x)\n",
7179 m
, m
->m_pkthdr
.redzone
, mb_redzone
, mb_redzone_cookie
);
7184 SYSCTL_DECL(_kern_ipc
);
7185 SYSCTL_PROC(_kern_ipc
, KIPC_MBSTAT
, mbstat
,
7186 CTLFLAG_RD
| CTLFLAG_LOCKED
,
7187 0, 0, mbstat_sysctl
, "S,mbstat", "");
7188 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mb_stat
,
7189 CTLFLAG_RD
| CTLFLAG_LOCKED
,
7190 0, 0, mb_stat_sysctl
, "S,mb_stat", "");
7191 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mleak_top_trace
,
7192 CTLFLAG_RD
| CTLFLAG_LOCKED
,
7193 0, 0, mleak_top_trace_sysctl
, "S,mb_top_trace", "");
7194 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mleak_table
,
7195 CTLFLAG_RD
| CTLFLAG_LOCKED
,
7196 0, 0, mleak_table_sysctl
, "S,mleak_table", "");
7197 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mleak_sample_factor
,
7198 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mleak_table
.mleak_sample_factor
, 0, "");
7199 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_normalized
,
7200 CTLFLAG_RD
| CTLFLAG_LOCKED
, &mb_normalized
, 0, "");
7201 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_watchdog
,
7202 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mb_watchdog
, 0, "");