2 * Copyright (c) 1998-2018 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/backtrace.h>
89 #include <kern/cpu_number.h>
90 #include <kern/zalloc.h>
92 #include <libkern/OSAtomic.h>
93 #include <libkern/OSDebug.h>
94 #include <libkern/libkern.h>
98 #include <IOKit/IOMapper.h>
100 #include <machine/limits.h>
101 #include <machine/machine_routines.h>
104 #include <security/mac_framework.h>
107 #include <sys/mcache.h>
108 #include <net/ntstat.h>
111 * MBUF IMPLEMENTATION NOTES.
113 * There is a total of 5 per-CPU caches:
116 * This is a cache of rudimentary objects of MSIZE in size; each
117 * object represents an mbuf structure. This cache preserves only
118 * the m_type field of the mbuf during its transactions.
121 * This is a cache of rudimentary objects of MCLBYTES in size; each
122 * object represents a mcluster structure. This cache does not
123 * preserve the contents of the objects during its transactions.
126 * This is a cache of rudimentary objects of MBIGCLBYTES in size; each
127 * object represents a mbigcluster structure. This cache does not
128 * preserve the contents of the objects during its transaction.
131 * This is a cache of mbufs each having a cluster attached to it.
132 * It is backed by MC_MBUF and MC_CL rudimentary caches. Several
133 * fields of the mbuf related to the external cluster are preserved
134 * during transactions.
137 * This is a cache of mbufs each having a big cluster attached to it.
138 * It is backed by MC_MBUF and MC_BIGCL rudimentary caches. Several
139 * fields of the mbuf related to the external cluster are preserved
140 * during transactions.
144 * Allocation requests are handled first at the per-CPU (mcache) layer
145 * before falling back to the slab layer. Performance is optimal when
146 * the request is satisfied at the CPU layer because global data/lock
147 * never gets accessed. When the slab layer is entered for allocation,
148 * the slab freelist will be checked first for available objects before
149 * the VM backing store is invoked. Slab layer operations are serialized
150 * for all of the caches as the mbuf global lock is held most of the time.
151 * Allocation paths are different depending on the class of objects:
153 * a. Rudimentary object:
155 * { m_get_common(), m_clattach(), m_mclget(),
156 * m_mclalloc(), m_bigalloc(), m_copym_with_hdrs(),
157 * composite object allocation }
160 * | +-----------------------+
162 * mcache_alloc/mcache_alloc_ext() mbuf_slab_audit()
165 * [CPU cache] -------> (found?) -------+
168 * mbuf_slab_alloc() |
171 * +---------> [freelist] -------> (found?) -------+
177 * +---<<---- kmem_mb_alloc()
179 * b. Composite object:
181 * { m_getpackets_internal(), m_allocpacket_internal() }
184 * | +------ (done) ---------+
186 * mcache_alloc/mcache_alloc_ext() mbuf_cslab_audit()
189 * [CPU cache] -------> (found?) -------+
192 * mbuf_cslab_alloc() |
195 * [freelist] -------> (found?) -------+
198 * (rudimentary object) |
199 * mcache_alloc/mcache_alloc_ext() ------>>-----+
201 * Auditing notes: If auditing is enabled, buffers will be subjected to
202 * integrity checks by the audit routine. This is done by verifying their
203 * contents against DEADBEEF (free) pattern before returning them to caller.
204 * As part of this step, the routine will also record the transaction and
205 * pattern-fill the buffers with BADDCAFE (uninitialized) pattern. It will
206 * also restore any constructed data structure fields if necessary.
208 * OBJECT DEALLOCATION:
210 * Freeing an object simply involves placing it into the CPU cache; this
211 * pollutes the cache to benefit subsequent allocations. The slab layer
212 * will only be entered if the object is to be purged out of the cache.
213 * During normal operations, this happens only when the CPU layer resizes
214 * its bucket while it's adjusting to the allocation load. Deallocation
215 * paths are different depending on the class of objects:
217 * a. Rudimentary object:
219 * { m_free(), m_freem_list(), composite object deallocation }
222 * | +------ (done) ---------+
224 * mcache_free/mcache_free_ext() |
227 * mbuf_slab_audit() |
230 * [CPU cache] ---> (not purging?) -----+
236 * [freelist] ----------->>------------+
237 * (objects get purged to VM only on demand)
239 * b. Composite object:
241 * { m_free(), m_freem_list() }
244 * | +------ (done) ---------+
246 * mcache_free/mcache_free_ext() |
249 * mbuf_cslab_audit() |
252 * [CPU cache] ---> (not purging?) -----+
255 * mbuf_cslab_free() |
258 * [freelist] ---> (not purging?) -----+
261 * (rudimentary object) |
262 * mcache_free/mcache_free_ext() ------->>------+
264 * Auditing notes: If auditing is enabled, the audit routine will save
265 * any constructed data structure fields (if necessary) before filling the
266 * contents of the buffers with DEADBEEF (free) pattern and recording the
267 * transaction. Buffers that are freed (whether at CPU or slab layer) are
268 * expected to contain the free pattern.
272 * Debugging can be enabled by adding "mbuf_debug=0x3" to boot-args; this
273 * translates to the mcache flags (MCF_VERIFY | MCF_AUDIT). Additionally,
274 * the CPU layer cache can be disabled by setting the MCF_NOCPUCACHE flag,
275 * i.e. modify the boot argument parameter to "mbuf_debug=0x13". Leak
276 * detection may also be disabled by setting the MCF_NOLEAKLOG flag, e.g.
277 * "mbuf_debug=0x113". Note that debugging consumes more CPU and memory.
279 * Each object is associated with exactly one mcache_audit_t structure that
280 * contains the information related to its last buffer transaction. Given
281 * an address of an object, the audit structure can be retrieved by finding
282 * the position of the object relevant to the base address of the cluster:
284 * +------------+ +=============+
285 * | mbuf addr | | mclaudit[i] |
286 * +------------+ +=============+
288 * i = MTOBG(addr) +-------------+
289 * | +-----> | cl_audit[1] | -----> mcache_audit_t
290 * b = BGTOM(i) | +-------------+
292 * x = MCLIDX(b, addr) | +-------------+
293 * | | | cl_audit[7] |
294 * +-----------------+ +-------------+
297 * The mclaudit[] array is allocated at initialization time, but its contents
298 * get populated when the corresponding cluster is created. Because a page
299 * can be turned into NMBPG number of mbufs, we preserve enough space for the
300 * mbufs so that there is a 1-to-1 mapping between them. A page that never
301 * gets (or has not yet) turned into mbufs will use only cl_audit[0] with the
302 * remaining entries unused. For 16KB cluster, only one entry from the first
303 * page is allocated and used for the entire object.
306 /* TODO: should be in header file */
307 /* kernel translater */
308 extern vm_offset_t
kmem_mb_alloc(vm_map_t
, int, int, kern_return_t
*);
309 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
310 extern vm_map_t mb_map
; /* special map */
312 static uint32_t mb_kmem_contig_failed
;
313 static uint32_t mb_kmem_failed
;
314 static uint32_t mb_kmem_one_failed
;
315 /* Timestamp of allocation failures. */
316 static uint64_t mb_kmem_contig_failed_ts
;
317 static uint64_t mb_kmem_failed_ts
;
318 static uint64_t mb_kmem_one_failed_ts
;
319 static uint64_t mb_kmem_contig_failed_size
;
320 static uint64_t mb_kmem_failed_size
;
321 static uint32_t mb_kmem_stats
[6];
322 static const char *mb_kmem_stats_labels
[] = { "INVALID_ARGUMENT",
330 decl_lck_mtx_data(static, mbuf_mlock_data
);
331 static lck_mtx_t
*mbuf_mlock
= &mbuf_mlock_data
;
332 static lck_attr_t
*mbuf_mlock_attr
;
333 static lck_grp_t
*mbuf_mlock_grp
;
334 static lck_grp_attr_t
*mbuf_mlock_grp_attr
;
336 /* Back-end (common) layer */
337 static uint64_t mb_expand_cnt
;
338 static uint64_t mb_expand_cl_cnt
;
339 static uint64_t mb_expand_cl_total
;
340 static uint64_t mb_expand_bigcl_cnt
;
341 static uint64_t mb_expand_bigcl_total
;
342 static uint64_t mb_expand_16kcl_cnt
;
343 static uint64_t mb_expand_16kcl_total
;
344 static boolean_t mbuf_worker_needs_wakeup
; /* wait channel for mbuf worker */
345 static uint32_t mbuf_worker_run_cnt
;
346 static uint64_t mbuf_worker_last_runtime
;
347 static uint64_t mbuf_drain_last_runtime
;
348 static int mbuf_worker_ready
; /* worker thread is runnable */
349 static int ncpu
; /* number of CPUs */
350 static ppnum_t
*mcl_paddr
; /* Array of cluster physical addresses */
351 static ppnum_t mcl_pages
; /* Size of array (# physical pages) */
352 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
353 static mcache_t
*ref_cache
; /* Cache of cluster reference & flags */
354 static mcache_t
*mcl_audit_con_cache
; /* Audit contents cache */
355 static unsigned int mbuf_debug
; /* patchable mbuf mcache flags */
356 static unsigned int mb_normalized
; /* number of packets "normalized" */
358 #define MB_GROWTH_AGGRESSIVE 1 /* Threshold: 1/2 of total */
359 #define MB_GROWTH_NORMAL 2 /* Threshold: 3/4 of total */
362 MC_MBUF
= 0, /* Regular mbuf */
364 MC_BIGCL
, /* Large (4KB) cluster */
365 MC_16KCL
, /* Jumbo (16KB) cluster */
366 MC_MBUF_CL
, /* mbuf + cluster */
367 MC_MBUF_BIGCL
, /* mbuf + large (4KB) cluster */
368 MC_MBUF_16KCL
/* mbuf + jumbo (16KB) cluster */
371 #define MBUF_CLASS_MIN MC_MBUF
372 #define MBUF_CLASS_MAX MC_MBUF_16KCL
373 #define MBUF_CLASS_LAST MC_16KCL
374 #define MBUF_CLASS_VALID(c) \
375 ((int)(c) >= MBUF_CLASS_MIN && (int)(c) <= MBUF_CLASS_MAX)
376 #define MBUF_CLASS_COMPOSITE(c) \
377 ((int)(c) > MBUF_CLASS_LAST)
381 * mbuf specific mcache allocation request flags.
383 #define MCR_COMP MCR_USR1 /* for MC_MBUF_{CL,BIGCL,16KCL} caches */
386 * Per-cluster slab structure.
388 * A slab is a cluster control structure that contains one or more object
389 * chunks; the available chunks are chained in the slab's freelist (sl_head).
390 * Each time a chunk is taken out of the slab, the slab's reference count
391 * gets incremented. When all chunks have been taken out, the empty slab
392 * gets removed (SLF_DETACHED) from the class's slab list. A chunk that is
393 * returned to a slab causes the slab's reference count to be decremented;
394 * it also causes the slab to be reinserted back to class's slab list, if
395 * it's not already done.
397 * Compartmentalizing of the object chunks into slabs allows us to easily
398 * merge one or more slabs together when the adjacent slabs are idle, as
399 * well as to convert or move a slab from one class to another; e.g. the
400 * mbuf cluster slab can be converted to a regular cluster slab when all
401 * mbufs in the slab have been freed.
403 * A slab may also span across multiple clusters for chunks larger than
404 * a cluster's size. In this case, only the slab of the first cluster is
405 * used. The rest of the slabs are marked with SLF_PARTIAL to indicate
406 * that they are part of the larger slab.
408 * Each slab controls a page of memory.
410 typedef struct mcl_slab
{
411 struct mcl_slab
*sl_next
; /* neighboring slab */
412 u_int8_t sl_class
; /* controlling mbuf class */
413 int8_t sl_refcnt
; /* outstanding allocations */
414 int8_t sl_chunks
; /* chunks (bufs) in this slab */
415 u_int16_t sl_flags
; /* slab flags (see below) */
416 u_int16_t sl_len
; /* slab length */
417 void *sl_base
; /* base of allocated memory */
418 void *sl_head
; /* first free buffer */
419 TAILQ_ENTRY(mcl_slab
) sl_link
; /* next/prev slab on freelist */
422 #define SLF_MAPPED 0x0001 /* backed by a mapped page */
423 #define SLF_PARTIAL 0x0002 /* part of another slab */
424 #define SLF_DETACHED 0x0004 /* not in slab freelist */
427 * The array of slabs are broken into groups of arrays per 1MB of kernel
428 * memory to reduce the footprint. Each group is allocated on demand
429 * whenever a new piece of memory mapped in from the VM crosses the 1MB
432 #define NSLABSPMB ((1 << MBSHIFT) >> PAGE_SHIFT)
434 typedef struct mcl_slabg
{
435 mcl_slab_t
*slg_slab
; /* group of slabs */
439 * Number of slabs needed to control a 16KB cluster object.
441 #define NSLABSP16KB (M16KCLBYTES >> PAGE_SHIFT)
444 * Per-cluster audit structure.
447 mcache_audit_t
**cl_audit
; /* array of audits */
451 struct thread
*msa_thread
; /* thread doing transaction */
452 struct thread
*msa_pthread
; /* previous transaction thread */
453 uint32_t msa_tstamp
; /* transaction timestamp (ms) */
454 uint32_t msa_ptstamp
; /* prev transaction timestamp (ms) */
455 uint16_t msa_depth
; /* pc stack depth */
456 uint16_t msa_pdepth
; /* previous transaction pc stack */
457 void *msa_stack
[MCACHE_STACK_DEPTH
];
458 void *msa_pstack
[MCACHE_STACK_DEPTH
];
459 } mcl_scratch_audit_t
;
463 * Size of data from the beginning of an mbuf that covers m_hdr,
464 * pkthdr and m_ext structures. If auditing is enabled, we allocate
465 * a shadow mbuf structure of this size inside each audit structure,
466 * and the contents of the real mbuf gets copied into it when the mbuf
467 * is freed. This allows us to pattern-fill the mbuf for integrity
468 * check, and to preserve any constructed mbuf fields (e.g. mbuf +
469 * cluster cache case). Note that we don't save the contents of
470 * clusters when they are freed; we simply pattern-fill them.
472 u_int8_t sc_mbuf
[(MSIZE
- _MHLEN
) + sizeof (_m_ext_t
)];
473 mcl_scratch_audit_t sc_scratch
__attribute__((aligned(8)));
474 } mcl_saved_contents_t
;
476 #define AUDIT_CONTENTS_SIZE (sizeof (mcl_saved_contents_t))
478 #define MCA_SAVED_MBUF_PTR(_mca) \
479 ((struct mbuf *)(void *)((mcl_saved_contents_t *) \
480 (_mca)->mca_contents)->sc_mbuf)
481 #define MCA_SAVED_MBUF_SIZE \
482 (sizeof (((mcl_saved_contents_t *)0)->sc_mbuf))
483 #define MCA_SAVED_SCRATCH_PTR(_mca) \
484 (&((mcl_saved_contents_t *)(_mca)->mca_contents)->sc_scratch)
487 * mbuf specific mcache audit flags
489 #define MB_INUSE 0x01 /* object has not been returned to slab */
490 #define MB_COMP_INUSE 0x02 /* object has not been returned to cslab */
491 #define MB_SCVALID 0x04 /* object has valid saved contents */
494 * Each of the following two arrays hold up to nmbclusters elements.
496 static mcl_audit_t
*mclaudit
; /* array of cluster audit information */
497 static unsigned int maxclaudit
; /* max # of entries in audit table */
498 static mcl_slabg_t
**slabstbl
; /* cluster slabs table */
499 static unsigned int maxslabgrp
; /* max # of entries in slabs table */
500 static unsigned int slabgrp
; /* # of entries in slabs table */
503 int nclusters
; /* # of clusters for non-jumbo (legacy) sizes */
504 int njcl
; /* # of clusters for jumbo sizes */
505 int njclbytes
; /* size of a jumbo cluster */
506 unsigned char *mbutl
; /* first mapped cluster address */
507 unsigned char *embutl
; /* ending virtual address of mclusters */
508 int _max_linkhdr
; /* largest link-level header */
509 int _max_protohdr
; /* largest protocol header */
510 int max_hdr
; /* largest link+protocol header */
511 int max_datalen
; /* MHLEN - max_hdr */
513 static boolean_t mclverify
; /* debug: pattern-checking */
514 static boolean_t mcltrace
; /* debug: stack tracing */
515 static boolean_t mclfindleak
; /* debug: leak detection */
516 static boolean_t mclexpleak
; /* debug: expose leak info to user space */
518 static struct timeval mb_start
; /* beginning of time */
520 /* mbuf leak detection variables */
521 static struct mleak_table mleak_table
;
522 static mleak_stat_t
*mleak_stat
;
524 #define MLEAK_STAT_SIZE(n) \
525 __builtin_offsetof(mleak_stat_t, ml_trace[n])
528 mcache_obj_t
*element
; /* the alloc'ed element, NULL if unused */
529 u_int32_t trace_index
; /* mtrace index for corresponding backtrace */
530 u_int32_t count
; /* How many objects were requested */
531 u_int64_t hitcount
; /* for determining hash effectiveness */
535 u_int64_t collisions
;
539 uintptr_t addr
[MLEAK_STACK_DEPTH
];
542 /* Size must be a power of two for the zhash to be able to just mask off bits */
543 #define MLEAK_ALLOCATION_MAP_NUM 512
544 #define MLEAK_TRACE_MAP_NUM 256
547 * Sample factor for how often to record a trace. This is overwritable
548 * by the boot-arg mleak_sample_factor.
550 #define MLEAK_SAMPLE_FACTOR 500
553 * Number of top leakers recorded.
555 #define MLEAK_NUM_TRACES 5
557 #define MB_LEAK_SPACING_64 " "
558 #define MB_LEAK_SPACING_32 " "
561 #define MB_LEAK_HDR_32 "\n\
562 trace [1] trace [2] trace [3] trace [4] trace [5] \n\
563 ---------- ---------- ---------- ---------- ---------- \n\
566 #define MB_LEAK_HDR_64 "\n\
567 trace [1] trace [2] trace [3] \
568 trace [4] trace [5] \n\
569 ------------------ ------------------ ------------------ \
570 ------------------ ------------------ \n\
573 static uint32_t mleak_alloc_buckets
= MLEAK_ALLOCATION_MAP_NUM
;
574 static uint32_t mleak_trace_buckets
= MLEAK_TRACE_MAP_NUM
;
576 /* Hashmaps of allocations and their corresponding traces */
577 static struct mallocation
*mleak_allocations
;
578 static struct mtrace
*mleak_traces
;
579 static struct mtrace
*mleak_top_trace
[MLEAK_NUM_TRACES
];
581 /* Lock to protect mleak tables from concurrent modification */
582 decl_lck_mtx_data(static, mleak_lock_data
);
583 static lck_mtx_t
*mleak_lock
= &mleak_lock_data
;
584 static lck_attr_t
*mleak_lock_attr
;
585 static lck_grp_t
*mleak_lock_grp
;
586 static lck_grp_attr_t
*mleak_lock_grp_attr
;
588 /* *Failed* large allocations. */
592 uintptr_t addr
[MLEAK_STACK_DEPTH
];
595 #define MTRACELARGE_NUM_TRACES 5
596 static struct mtracelarge mtracelarge_table
[MTRACELARGE_NUM_TRACES
];
598 static void mtracelarge_register(size_t size
);
600 /* Lock to protect the completion callback table */
601 static lck_grp_attr_t
*mbuf_tx_compl_tbl_lck_grp_attr
= NULL
;
602 static lck_attr_t
*mbuf_tx_compl_tbl_lck_attr
= NULL
;
603 static lck_grp_t
*mbuf_tx_compl_tbl_lck_grp
= NULL
;
604 decl_lck_rw_data(, mbuf_tx_compl_tbl_lck_rw_data
);
605 lck_rw_t
*mbuf_tx_compl_tbl_lock
= &mbuf_tx_compl_tbl_lck_rw_data
;
607 extern u_int32_t high_sb_max
;
609 /* The minimum number of objects that are allocated, to start. */
611 #define MINBIGCL (MINCL >> 1)
612 #define MIN16KCL (MINCL >> 2)
614 /* Low watermarks (only map in pages once free counts go below) */
615 #define MBIGCL_LOWAT MINBIGCL
616 #define M16KCL_LOWAT MIN16KCL
619 mbuf_class_t mtbl_class
; /* class type */
620 mcache_t
*mtbl_cache
; /* mcache for this buffer class */
621 TAILQ_HEAD(mcl_slhead
, mcl_slab
) mtbl_slablist
; /* slab list */
622 mcache_obj_t
*mtbl_cobjlist
; /* composite objects freelist */
623 mb_class_stat_t
*mtbl_stats
; /* statistics fetchable via sysctl */
624 u_int32_t mtbl_maxsize
; /* maximum buffer size */
625 int mtbl_minlimit
; /* minimum allowed */
626 int mtbl_maxlimit
; /* maximum allowed */
627 u_int32_t mtbl_wantpurge
; /* purge during next reclaim */
628 uint32_t mtbl_avgtotal
; /* average total on iOS */
629 u_int32_t mtbl_expand
; /* worker should expand the class */
632 #define m_class(c) mbuf_table[c].mtbl_class
633 #define m_cache(c) mbuf_table[c].mtbl_cache
634 #define m_slablist(c) mbuf_table[c].mtbl_slablist
635 #define m_cobjlist(c) mbuf_table[c].mtbl_cobjlist
636 #define m_maxsize(c) mbuf_table[c].mtbl_maxsize
637 #define m_minlimit(c) mbuf_table[c].mtbl_minlimit
638 #define m_maxlimit(c) mbuf_table[c].mtbl_maxlimit
639 #define m_wantpurge(c) mbuf_table[c].mtbl_wantpurge
640 #define m_cname(c) mbuf_table[c].mtbl_stats->mbcl_cname
641 #define m_size(c) mbuf_table[c].mtbl_stats->mbcl_size
642 #define m_total(c) mbuf_table[c].mtbl_stats->mbcl_total
643 #define m_active(c) mbuf_table[c].mtbl_stats->mbcl_active
644 #define m_infree(c) mbuf_table[c].mtbl_stats->mbcl_infree
645 #define m_slab_cnt(c) mbuf_table[c].mtbl_stats->mbcl_slab_cnt
646 #define m_alloc_cnt(c) mbuf_table[c].mtbl_stats->mbcl_alloc_cnt
647 #define m_free_cnt(c) mbuf_table[c].mtbl_stats->mbcl_free_cnt
648 #define m_notified(c) mbuf_table[c].mtbl_stats->mbcl_notified
649 #define m_purge_cnt(c) mbuf_table[c].mtbl_stats->mbcl_purge_cnt
650 #define m_fail_cnt(c) mbuf_table[c].mtbl_stats->mbcl_fail_cnt
651 #define m_ctotal(c) mbuf_table[c].mtbl_stats->mbcl_ctotal
652 #define m_peak(c) mbuf_table[c].mtbl_stats->mbcl_peak_reported
653 #define m_release_cnt(c) mbuf_table[c].mtbl_stats->mbcl_release_cnt
654 #define m_region_expand(c) mbuf_table[c].mtbl_expand
656 static mbuf_table_t mbuf_table
[] = {
658 * The caches for mbufs, regular clusters and big clusters.
659 * The average total values were based on data gathered by actual
660 * usage patterns on iOS.
662 { MC_MBUF
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_MBUF
)),
663 NULL
, NULL
, 0, 0, 0, 0, 3000, 0 },
664 { MC_CL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_CL
)),
665 NULL
, NULL
, 0, 0, 0, 0, 2000, 0 },
666 { MC_BIGCL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_BIGCL
)),
667 NULL
, NULL
, 0, 0, 0, 0, 1000, 0 },
668 { MC_16KCL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_16KCL
)),
669 NULL
, NULL
, 0, 0, 0, 0, 200, 0 },
671 * The following are special caches; they serve as intermediate
672 * caches backed by the above rudimentary caches. Each object
673 * in the cache is an mbuf with a cluster attached to it. Unlike
674 * the above caches, these intermediate caches do not directly
675 * deal with the slab structures; instead, the constructed
676 * cached elements are simply stored in the freelists.
678 { MC_MBUF_CL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0, 2000, 0 },
679 { MC_MBUF_BIGCL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0, 1000, 0 },
680 { MC_MBUF_16KCL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0, 200, 0 },
683 #define NELEM(a) (sizeof (a) / sizeof ((a)[0]))
687 m_avgtotal(mbuf_class_t c
)
689 return (mbuf_table
[c
].mtbl_avgtotal
);
692 static void *mb_waitchan
= &mbuf_table
; /* wait channel for all caches */
693 static int mb_waiters
; /* number of waiters */
695 boolean_t mb_peak_newreport
= FALSE
;
696 boolean_t mb_peak_firstreport
= FALSE
;
698 /* generate a report by default after 1 week of uptime */
699 #define MBUF_PEAK_FIRST_REPORT_THRESHOLD 604800
701 #define MB_WDT_MAXTIME 10 /* # of secs before watchdog panic */
702 static struct timeval mb_wdtstart
; /* watchdog start timestamp */
703 static char *mbuf_dump_buf
;
705 #define MBUF_DUMP_BUF_SIZE 4096
708 * mbuf watchdog is enabled by default on embedded platforms. It is
709 * also toggeable via the kern.ipc.mb_watchdog sysctl.
710 * Garbage collection is also enabled by default on embedded platforms.
711 * mb_drain_maxint controls the amount of time to wait (in seconds) before
712 * consecutive calls to mbuf_drain().
715 static unsigned int mb_watchdog
= 1;
716 static unsigned int mb_drain_maxint
= 60;
718 static unsigned int mb_watchdog
= 0;
719 static unsigned int mb_drain_maxint
= 0;
720 #endif /* CONFIG_EMBEDDED */
722 uintptr_t mb_obscure_extfree
__attribute__((visibility("hidden")));
723 uintptr_t mb_obscure_extref
__attribute__((visibility("hidden")));
726 static u_int32_t mb_redzone_cookie
;
727 static void m_redzone_init(struct mbuf
*);
728 static void m_redzone_verify(struct mbuf
*m
);
730 /* The following are used to serialize m_clalloc() */
731 static boolean_t mb_clalloc_busy
;
732 static void *mb_clalloc_waitchan
= &mb_clalloc_busy
;
733 static int mb_clalloc_waiters
;
735 static void mbuf_mtypes_sync(boolean_t
);
736 static int mbstat_sysctl SYSCTL_HANDLER_ARGS
;
737 static void mbuf_stat_sync(void);
738 static int mb_stat_sysctl SYSCTL_HANDLER_ARGS
;
739 static int mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS
;
740 static int mleak_table_sysctl SYSCTL_HANDLER_ARGS
;
741 static char *mbuf_dump(void);
742 static void mbuf_table_init(void);
743 static inline void m_incref(struct mbuf
*);
744 static inline u_int16_t
m_decref(struct mbuf
*);
745 static int m_clalloc(const u_int32_t
, const int, const u_int32_t
);
746 static void mbuf_worker_thread_init(void);
747 static mcache_obj_t
*slab_alloc(mbuf_class_t
, int);
748 static void slab_free(mbuf_class_t
, mcache_obj_t
*);
749 static unsigned int mbuf_slab_alloc(void *, mcache_obj_t
***,
751 static void mbuf_slab_free(void *, mcache_obj_t
*, int);
752 static void mbuf_slab_audit(void *, mcache_obj_t
*, boolean_t
);
753 static void mbuf_slab_notify(void *, u_int32_t
);
754 static unsigned int cslab_alloc(mbuf_class_t
, mcache_obj_t
***,
756 static unsigned int cslab_free(mbuf_class_t
, mcache_obj_t
*, int);
757 static unsigned int mbuf_cslab_alloc(void *, mcache_obj_t
***,
759 static void mbuf_cslab_free(void *, mcache_obj_t
*, int);
760 static void mbuf_cslab_audit(void *, mcache_obj_t
*, boolean_t
);
761 static int freelist_populate(mbuf_class_t
, unsigned int, int);
762 static void freelist_init(mbuf_class_t
);
763 static boolean_t
mbuf_cached_above(mbuf_class_t
, int);
764 static boolean_t
mbuf_steal(mbuf_class_t
, unsigned int);
765 static void m_reclaim(mbuf_class_t
, unsigned int, boolean_t
);
766 static int m_howmany(int, size_t);
767 static void mbuf_worker_thread(void);
768 static void mbuf_watchdog(void);
769 static boolean_t
mbuf_sleep(mbuf_class_t
, unsigned int, int);
771 static void mcl_audit_init(void *, mcache_audit_t
**, mcache_obj_t
**,
772 size_t, unsigned int);
773 static void mcl_audit_free(void *, unsigned int);
774 static mcache_audit_t
*mcl_audit_buf2mca(mbuf_class_t
, mcache_obj_t
*);
775 static void mcl_audit_mbuf(mcache_audit_t
*, void *, boolean_t
, boolean_t
);
776 static void mcl_audit_cluster(mcache_audit_t
*, void *, size_t, boolean_t
,
778 static void mcl_audit_restore_mbuf(struct mbuf
*, mcache_audit_t
*, boolean_t
);
779 static void mcl_audit_save_mbuf(struct mbuf
*, mcache_audit_t
*);
780 static void mcl_audit_scratch(mcache_audit_t
*);
781 static void mcl_audit_mcheck_panic(struct mbuf
*);
782 static void mcl_audit_verify_nextptr(void *, mcache_audit_t
*);
784 static void mleak_activate(void);
785 static void mleak_logger(u_int32_t
, mcache_obj_t
*, boolean_t
);
786 static boolean_t
mleak_log(uintptr_t *, mcache_obj_t
*, uint32_t, int);
787 static void mleak_free(mcache_obj_t
*);
788 static void mleak_sort_traces(void);
789 static void mleak_update_stats(void);
791 static mcl_slab_t
*slab_get(void *);
792 static void slab_init(mcl_slab_t
*, mbuf_class_t
, u_int32_t
,
793 void *, void *, unsigned int, int, int);
794 static void slab_insert(mcl_slab_t
*, mbuf_class_t
);
795 static void slab_remove(mcl_slab_t
*, mbuf_class_t
);
796 static boolean_t
slab_inrange(mcl_slab_t
*, void *);
797 static void slab_nextptr_panic(mcl_slab_t
*, void *);
798 static void slab_detach(mcl_slab_t
*);
799 static boolean_t
slab_is_detached(mcl_slab_t
*);
801 static int m_copyback0(struct mbuf
**, int, int, const void *, int, int);
802 static struct mbuf
*m_split0(struct mbuf
*, int, int, int);
803 __private_extern__
void mbuf_report_peak_usage(void);
804 static boolean_t
mbuf_report_usage(mbuf_class_t
);
805 #if DEBUG || DEVELOPMENT
806 #define mbwdog_logger(fmt, ...) _mbwdog_logger(__func__, __LINE__, fmt, ## __VA_ARGS__)
807 static void _mbwdog_logger(const char *func
, const int line
, const char *fmt
, ...);
808 static char *mbwdog_logging
;
809 const unsigned mbwdog_logging_size
= 4096;
810 static size_t mbwdog_logging_used
;
812 #define mbwdog_logger(fmt, ...) do { } while (0)
814 static void mbuf_drain_locked(boolean_t
);
816 /* flags for m_copyback0 */
817 #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */
818 #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */
819 #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */
820 #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */
823 * This flag is set for all mbufs that come out of and into the composite
824 * mbuf + cluster caches, i.e. MC_MBUF_CL and MC_MBUF_BIGCL. mbufs that
825 * are marked with such a flag have clusters attached to them, and will be
826 * treated differently when they are freed; instead of being placed back
827 * into the mbuf and cluster freelists, the composite mbuf + cluster objects
828 * are placed back into the appropriate composite cache's freelist, and the
829 * actual freeing is deferred until the composite objects are purged. At
830 * such a time, this flag will be cleared from the mbufs and the objects
831 * will be freed into their own separate freelists.
833 #define EXTF_COMPOSITE 0x1
836 * This flag indicates that the external cluster is read-only, i.e. it is
837 * or was referred to by more than one mbufs. Once set, this flag is never
840 #define EXTF_READONLY 0x2
842 * This flag indicates that the external cluster is paired with the mbuf.
843 * Pairing implies an external free routine defined which will be invoked
844 * when the reference count drops to the minimum at m_free time. This
845 * flag is never cleared.
847 #define EXTF_PAIRED 0x4
850 (EXTF_COMPOSITE | EXTF_READONLY | EXTF_PAIRED)
852 #define MEXT_MINREF(m) ((m_get_rfa(m))->minref)
853 #define MEXT_REF(m) ((m_get_rfa(m))->refcnt)
854 #define MEXT_PREF(m) ((m_get_rfa(m))->prefcnt)
855 #define MEXT_FLAGS(m) ((m_get_rfa(m))->flags)
856 #define MEXT_PRIV(m) ((m_get_rfa(m))->priv)
857 #define MEXT_PMBUF(m) ((m_get_rfa(m))->paired)
858 #define MEXT_TOKEN(m) ((m_get_rfa(m))->ext_token)
859 #define MBUF_IS_COMPOSITE(m) \
860 (MEXT_REF(m) == MEXT_MINREF(m) && \
861 (MEXT_FLAGS(m) & EXTF_MASK) == EXTF_COMPOSITE)
863 * This macro can be used to test if the mbuf is paired to an external
864 * cluster. The test for MEXT_PMBUF being equal to the mbuf in subject
865 * is important, as EXTF_PAIRED alone is insufficient since it is immutable,
866 * and thus survives calls to m_free_paired.
868 #define MBUF_IS_PAIRED(m) \
869 (((m)->m_flags & M_EXT) && \
870 (MEXT_FLAGS(m) & EXTF_MASK) == EXTF_PAIRED && \
871 MEXT_PMBUF(m) == (m))
874 * Macros used to verify the integrity of the mbuf.
876 #define _MCHECK(m) { \
877 if ((m)->m_type != MT_FREE && !MBUF_IS_PAIRED(m)) { \
878 if (mclaudit == NULL) \
879 panic("MCHECK: m_type=%d m=%p", \
880 (u_int16_t)(m)->m_type, m); \
882 mcl_audit_mcheck_panic(m); \
886 #define MBUF_IN_MAP(addr) \
887 ((unsigned char *)(addr) >= mbutl && \
888 (unsigned char *)(addr) < embutl)
890 #define MRANGE(addr) { \
891 if (!MBUF_IN_MAP(addr)) \
892 panic("MRANGE: address out of range 0x%p", addr); \
896 * Macro version of mtod.
898 #define MTOD(m, t) ((t)((m)->m_data))
901 * Macros to obtain page index given a base cluster address
903 #define MTOPG(x) (((unsigned char *)x - mbutl) >> PAGE_SHIFT)
904 #define PGTOM(x) (mbutl + (x << PAGE_SHIFT))
907 * Macro to find the mbuf index relative to a base.
909 #define MBPAGEIDX(c, m) \
910 (((unsigned char *)(m) - (unsigned char *)(c)) >> MSIZESHIFT)
913 * Same thing for 2KB cluster index.
915 #define CLPAGEIDX(c, m) \
916 (((unsigned char *)(m) - (unsigned char *)(c)) >> MCLSHIFT)
919 * Macro to find 4KB cluster index relative to a base
921 #define BCLPAGEIDX(c, m) \
922 (((unsigned char *)(m) - (unsigned char *)(c)) >> MBIGCLSHIFT)
925 * Macros used during mbuf and cluster initialization.
927 #define MBUF_INIT_PKTHDR(m) { \
928 (m)->m_pkthdr.rcvif = NULL; \
929 (m)->m_pkthdr.pkt_hdr = NULL; \
930 (m)->m_pkthdr.len = 0; \
931 (m)->m_pkthdr.csum_flags = 0; \
932 (m)->m_pkthdr.csum_data = 0; \
933 (m)->m_pkthdr.vlan_tag = 0; \
934 m_classifier_init(m, 0); \
940 #define MBUF_INIT(m, pkthdr, type) { \
942 (m)->m_next = (m)->m_nextpkt = NULL; \
944 (m)->m_type = type; \
945 if ((pkthdr) == 0) { \
946 (m)->m_data = (m)->m_dat; \
949 (m)->m_data = (m)->m_pktdat; \
950 (m)->m_flags = M_PKTHDR; \
951 MBUF_INIT_PKTHDR(m); \
955 #define MEXT_INIT(m, buf, size, free, arg, rfa, min, ref, pref, flag, \
957 (m)->m_data = (m)->m_ext.ext_buf = (buf); \
958 (m)->m_flags |= M_EXT; \
959 m_set_ext((m), (rfa), (free), (arg)); \
960 (m)->m_ext.ext_size = (size); \
961 MEXT_MINREF(m) = (min); \
962 MEXT_REF(m) = (ref); \
963 MEXT_PREF(m) = (pref); \
964 MEXT_FLAGS(m) = (flag); \
965 MEXT_PRIV(m) = (priv); \
966 MEXT_PMBUF(m) = (pm); \
969 #define MBUF_CL_INIT(m, buf, rfa, ref, flag) \
970 MEXT_INIT(m, buf, m_maxsize(MC_CL), NULL, NULL, rfa, 0, \
971 ref, 0, flag, 0, NULL)
973 #define MBUF_BIGCL_INIT(m, buf, rfa, ref, flag) \
974 MEXT_INIT(m, buf, m_maxsize(MC_BIGCL), m_bigfree, NULL, rfa, 0, \
975 ref, 0, flag, 0, NULL)
977 #define MBUF_16KCL_INIT(m, buf, rfa, ref, flag) \
978 MEXT_INIT(m, buf, m_maxsize(MC_16KCL), m_16kfree, NULL, rfa, 0, \
979 ref, 0, flag, 0, NULL)
982 * Macro to convert BSD malloc sleep flag to mcache's
984 #define MSLEEPF(f) ((!((f) & M_DONTWAIT)) ? MCR_SLEEP : MCR_NOSLEEP)
987 * The structure that holds all mbuf class statistics exportable via sysctl.
988 * Similar to mbstat structure, the mb_stat structure is protected by the
989 * global mbuf lock. It contains additional information about the classes
990 * that allows for a more accurate view of the state of the allocator.
992 struct mb_stat
*mb_stat
;
993 struct omb_stat
*omb_stat
; /* For backwards compatibility */
995 #define MB_STAT_SIZE(n) \
996 __builtin_offsetof(mb_stat_t, mbs_class[n])
997 #define OMB_STAT_SIZE(n) \
998 ((size_t)(&((struct omb_stat *)0)->mbs_class[n]))
1001 * The legacy structure holding all of the mbuf allocation statistics.
1002 * The actual statistics used by the kernel are stored in the mbuf_table
1003 * instead, and are updated atomically while the global mbuf lock is held.
1004 * They are mirrored in mbstat to support legacy applications (e.g. netstat).
1005 * Unlike before, the kernel no longer relies on the contents of mbstat for
1006 * its operations (e.g. cluster expansion) because the structure is exposed
1007 * to outside and could possibly be modified, therefore making it unsafe.
1008 * With the exception of the mbstat.m_mtypes array (see below), all of the
1009 * statistics are updated as they change.
1011 struct mbstat mbstat
;
1013 #define MBSTAT_MTYPES_MAX \
1014 (sizeof (mbstat.m_mtypes) / sizeof (mbstat.m_mtypes[0]))
1017 * Allocation statistics related to mbuf types (up to MT_MAX-1) are updated
1018 * atomically and stored in a per-CPU structure which is lock-free; this is
1019 * done in order to avoid writing to the global mbstat data structure which
1020 * would cause false sharing. During sysctl request for kern.ipc.mbstat,
1021 * the statistics across all CPUs will be converged into the mbstat.m_mtypes
1022 * array and returned to the application. Any updates for types greater or
1023 * equal than MT_MAX would be done atomically to the mbstat; this slows down
1024 * performance but is okay since the kernel uses only up to MT_MAX-1 while
1025 * anything beyond that (up to type 255) is considered a corner case.
1028 unsigned int cpu_mtypes
[MT_MAX
];
1029 } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE
), packed
)) mtypes_cpu_t
;
1032 mtypes_cpu_t mbs_cpu
[1];
1035 static mbuf_mtypes_t
*mbuf_mtypes
; /* per-CPU statistics */
1037 #define MBUF_MTYPES_SIZE(n) \
1038 ((size_t)(&((mbuf_mtypes_t *)0)->mbs_cpu[n]))
1040 #define MTYPES_CPU(p) \
1041 ((mtypes_cpu_t *)(void *)((char *)(p) + MBUF_MTYPES_SIZE(cpu_number())))
1043 #define mtype_stat_add(type, n) { \
1044 if ((unsigned)(type) < MT_MAX) { \
1045 mtypes_cpu_t *mbs = MTYPES_CPU(mbuf_mtypes); \
1046 atomic_add_32(&mbs->cpu_mtypes[type], n); \
1047 } else if ((unsigned)(type) < (unsigned)MBSTAT_MTYPES_MAX) { \
1048 atomic_add_16((int16_t *)&mbstat.m_mtypes[type], n); \
1052 #define mtype_stat_sub(t, n) mtype_stat_add(t, -(n))
1053 #define mtype_stat_inc(t) mtype_stat_add(t, 1)
1054 #define mtype_stat_dec(t) mtype_stat_sub(t, 1)
1057 mbuf_mtypes_sync(boolean_t locked
)
1063 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1065 bzero(&mtc
, sizeof (mtc
));
1066 for (m
= 0; m
< ncpu
; m
++) {
1067 mtypes_cpu_t
*scp
= &mbuf_mtypes
->mbs_cpu
[m
];
1070 bcopy(&scp
->cpu_mtypes
, &temp
.cpu_mtypes
,
1071 sizeof (temp
.cpu_mtypes
));
1073 for (n
= 0; n
< MT_MAX
; n
++)
1074 mtc
.cpu_mtypes
[n
] += temp
.cpu_mtypes
[n
];
1077 lck_mtx_lock(mbuf_mlock
);
1078 for (n
= 0; n
< MT_MAX
; n
++)
1079 mbstat
.m_mtypes
[n
] = mtc
.cpu_mtypes
[n
];
1081 lck_mtx_unlock(mbuf_mlock
);
1085 mbstat_sysctl SYSCTL_HANDLER_ARGS
1087 #pragma unused(oidp, arg1, arg2)
1088 mbuf_mtypes_sync(FALSE
);
1090 return (SYSCTL_OUT(req
, &mbstat
, sizeof (mbstat
)));
1094 mbuf_stat_sync(void)
1096 mb_class_stat_t
*sp
;
1101 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1103 for (k
= 0; k
< NELEM(mbuf_table
); k
++) {
1105 ccp
= &cp
->mc_cpu
[0];
1106 bktsize
= ccp
->cc_bktsize
;
1107 sp
= mbuf_table
[k
].mtbl_stats
;
1109 if (cp
->mc_flags
& MCF_NOCPUCACHE
)
1110 sp
->mbcl_mc_state
= MCS_DISABLED
;
1111 else if (cp
->mc_purge_cnt
> 0)
1112 sp
->mbcl_mc_state
= MCS_PURGING
;
1113 else if (bktsize
== 0)
1114 sp
->mbcl_mc_state
= MCS_OFFLINE
;
1116 sp
->mbcl_mc_state
= MCS_ONLINE
;
1118 sp
->mbcl_mc_cached
= 0;
1119 for (m
= 0; m
< ncpu
; m
++) {
1120 ccp
= &cp
->mc_cpu
[m
];
1121 if (ccp
->cc_objs
> 0)
1122 sp
->mbcl_mc_cached
+= ccp
->cc_objs
;
1123 if (ccp
->cc_pobjs
> 0)
1124 sp
->mbcl_mc_cached
+= ccp
->cc_pobjs
;
1126 sp
->mbcl_mc_cached
+= (cp
->mc_full
.bl_total
* bktsize
);
1127 sp
->mbcl_active
= sp
->mbcl_total
- sp
->mbcl_mc_cached
-
1130 sp
->mbcl_mc_waiter_cnt
= cp
->mc_waiter_cnt
;
1131 sp
->mbcl_mc_wretry_cnt
= cp
->mc_wretry_cnt
;
1132 sp
->mbcl_mc_nwretry_cnt
= cp
->mc_nwretry_cnt
;
1134 /* Calculate total count specific to each class */
1135 sp
->mbcl_ctotal
= sp
->mbcl_total
;
1136 switch (m_class(k
)) {
1138 /* Deduct mbufs used in composite caches */
1139 sp
->mbcl_ctotal
-= (m_total(MC_MBUF_CL
) +
1140 m_total(MC_MBUF_BIGCL
));
1144 /* Deduct clusters used in composite cache */
1145 sp
->mbcl_ctotal
-= m_total(MC_MBUF_CL
);
1149 /* Deduct clusters used in composite cache */
1150 sp
->mbcl_ctotal
-= m_total(MC_MBUF_BIGCL
);
1154 /* Deduct clusters used in composite cache */
1155 sp
->mbcl_ctotal
-= m_total(MC_MBUF_16KCL
);
1165 mb_stat_sysctl SYSCTL_HANDLER_ARGS
1167 #pragma unused(oidp, arg1, arg2)
1169 int k
, statsz
, proc64
= proc_is64bit(req
->p
);
1171 lck_mtx_lock(mbuf_mlock
);
1175 struct omb_class_stat
*oc
;
1176 struct mb_class_stat
*c
;
1178 omb_stat
->mbs_cnt
= mb_stat
->mbs_cnt
;
1179 oc
= &omb_stat
->mbs_class
[0];
1180 c
= &mb_stat
->mbs_class
[0];
1181 for (k
= 0; k
< omb_stat
->mbs_cnt
; k
++, oc
++, c
++) {
1182 (void) snprintf(oc
->mbcl_cname
, sizeof (oc
->mbcl_cname
),
1183 "%s", c
->mbcl_cname
);
1184 oc
->mbcl_size
= c
->mbcl_size
;
1185 oc
->mbcl_total
= c
->mbcl_total
;
1186 oc
->mbcl_active
= c
->mbcl_active
;
1187 oc
->mbcl_infree
= c
->mbcl_infree
;
1188 oc
->mbcl_slab_cnt
= c
->mbcl_slab_cnt
;
1189 oc
->mbcl_alloc_cnt
= c
->mbcl_alloc_cnt
;
1190 oc
->mbcl_free_cnt
= c
->mbcl_free_cnt
;
1191 oc
->mbcl_notified
= c
->mbcl_notified
;
1192 oc
->mbcl_purge_cnt
= c
->mbcl_purge_cnt
;
1193 oc
->mbcl_fail_cnt
= c
->mbcl_fail_cnt
;
1194 oc
->mbcl_ctotal
= c
->mbcl_ctotal
;
1195 oc
->mbcl_release_cnt
= c
->mbcl_release_cnt
;
1196 oc
->mbcl_mc_state
= c
->mbcl_mc_state
;
1197 oc
->mbcl_mc_cached
= c
->mbcl_mc_cached
;
1198 oc
->mbcl_mc_waiter_cnt
= c
->mbcl_mc_waiter_cnt
;
1199 oc
->mbcl_mc_wretry_cnt
= c
->mbcl_mc_wretry_cnt
;
1200 oc
->mbcl_mc_nwretry_cnt
= c
->mbcl_mc_nwretry_cnt
;
1203 statsz
= OMB_STAT_SIZE(NELEM(mbuf_table
));
1206 statsz
= MB_STAT_SIZE(NELEM(mbuf_table
));
1209 lck_mtx_unlock(mbuf_mlock
);
1211 return (SYSCTL_OUT(req
, statp
, statsz
));
1215 mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS
1217 #pragma unused(oidp, arg1, arg2)
1220 /* Ensure leak tracing turned on */
1221 if (!mclfindleak
|| !mclexpleak
)
1224 lck_mtx_lock(mleak_lock
);
1225 mleak_update_stats();
1226 i
= SYSCTL_OUT(req
, mleak_stat
, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES
));
1227 lck_mtx_unlock(mleak_lock
);
1233 mleak_table_sysctl SYSCTL_HANDLER_ARGS
1235 #pragma unused(oidp, arg1, arg2)
1238 /* Ensure leak tracing turned on */
1239 if (!mclfindleak
|| !mclexpleak
)
1242 lck_mtx_lock(mleak_lock
);
1243 i
= SYSCTL_OUT(req
, &mleak_table
, sizeof (mleak_table
));
1244 lck_mtx_unlock(mleak_lock
);
1250 m_incref(struct mbuf
*m
)
1253 volatile UInt16
*addr
= (volatile UInt16
*)&MEXT_REF(m
);
1259 } while (!OSCompareAndSwap16(old
, new, addr
));
1262 * If cluster is shared, mark it with (sticky) EXTF_READONLY;
1263 * we don't clear the flag when the refcount goes back to the
1264 * minimum, to simplify code calling m_mclhasreference().
1266 if (new > (MEXT_MINREF(m
) + 1) && !(MEXT_FLAGS(m
) & EXTF_READONLY
))
1267 (void) OSBitOrAtomic16(EXTF_READONLY
, &MEXT_FLAGS(m
));
1270 static inline u_int16_t
1271 m_decref(struct mbuf
*m
)
1274 volatile UInt16
*addr
= (volatile UInt16
*)&MEXT_REF(m
);
1280 } while (!OSCompareAndSwap16(old
, new, addr
));
1286 mbuf_table_init(void)
1288 unsigned int b
, c
, s
;
1289 int m
, config_mbuf_jumbo
= 0;
1291 MALLOC(omb_stat
, struct omb_stat
*, OMB_STAT_SIZE(NELEM(mbuf_table
)),
1292 M_TEMP
, M_WAITOK
| M_ZERO
);
1293 VERIFY(omb_stat
!= NULL
);
1295 MALLOC(mb_stat
, mb_stat_t
*, MB_STAT_SIZE(NELEM(mbuf_table
)),
1296 M_TEMP
, M_WAITOK
| M_ZERO
);
1297 VERIFY(mb_stat
!= NULL
);
1299 mb_stat
->mbs_cnt
= NELEM(mbuf_table
);
1300 for (m
= 0; m
< NELEM(mbuf_table
); m
++)
1301 mbuf_table
[m
].mtbl_stats
= &mb_stat
->mbs_class
[m
];
1303 #if CONFIG_MBUF_JUMBO
1304 config_mbuf_jumbo
= 1;
1305 #endif /* CONFIG_MBUF_JUMBO */
1307 if (config_mbuf_jumbo
== 1 || PAGE_SIZE
== M16KCLBYTES
) {
1309 * Set aside 1/3 of the mbuf cluster map for jumbo
1310 * clusters; we do this only on platforms where jumbo
1311 * cluster pool is enabled.
1313 njcl
= nmbclusters
/ 3;
1314 njclbytes
= M16KCLBYTES
;
1318 * nclusters holds both the 2KB and 4KB pools, so ensure it's
1319 * a multiple of 4KB clusters.
1321 nclusters
= P2ROUNDDOWN(nmbclusters
- njcl
, NCLPG
);
1324 * Each jumbo cluster takes 8 2KB clusters, so make
1325 * sure that the pool size is evenly divisible by 8;
1326 * njcl is in 2KB unit, hence treated as such.
1328 njcl
= P2ROUNDDOWN(nmbclusters
- nclusters
, NCLPJCL
);
1330 /* Update nclusters with rounded down value of njcl */
1331 nclusters
= P2ROUNDDOWN(nmbclusters
- njcl
, NCLPG
);
1335 * njcl is valid only on platforms with 16KB jumbo clusters or
1336 * with 16KB pages, where it is configured to 1/3 of the pool
1337 * size. On these platforms, the remaining is used for 2KB
1338 * and 4KB clusters. On platforms without 16KB jumbo clusters,
1339 * the entire pool is used for both 2KB and 4KB clusters. A 4KB
1340 * cluster can either be splitted into 16 mbufs, or into 2 2KB
1343 * +---+---+------------ ... -----------+------- ... -------+
1344 * | c | b | s | njcl |
1345 * +---+---+------------ ... -----------+------- ... -------+
1347 * 1/32th of the shared region is reserved for pure 2KB and 4KB
1348 * clusters (1/64th each.)
1350 c
= P2ROUNDDOWN((nclusters
>> 6), NCLPG
); /* in 2KB unit */
1351 b
= P2ROUNDDOWN((nclusters
>> (6 + NCLPBGSHIFT
)), NBCLPG
); /* in 4KB unit */
1352 s
= nclusters
- (c
+ (b
<< NCLPBGSHIFT
)); /* in 2KB unit */
1355 * 1/64th (c) is reserved for 2KB clusters.
1357 m_minlimit(MC_CL
) = c
;
1358 m_maxlimit(MC_CL
) = s
+ c
; /* in 2KB unit */
1359 m_maxsize(MC_CL
) = m_size(MC_CL
) = MCLBYTES
;
1360 (void) snprintf(m_cname(MC_CL
), MAX_MBUF_CNAME
, "cl");
1363 * Another 1/64th (b) of the map is reserved for 4KB clusters.
1364 * It cannot be turned into 2KB clusters or mbufs.
1366 m_minlimit(MC_BIGCL
) = b
;
1367 m_maxlimit(MC_BIGCL
) = (s
>> NCLPBGSHIFT
) + b
; /* in 4KB unit */
1368 m_maxsize(MC_BIGCL
) = m_size(MC_BIGCL
) = MBIGCLBYTES
;
1369 (void) snprintf(m_cname(MC_BIGCL
), MAX_MBUF_CNAME
, "bigcl");
1372 * The remaining 31/32ths (s) are all-purpose (mbufs, 2KB, or 4KB)
1374 m_minlimit(MC_MBUF
) = 0;
1375 m_maxlimit(MC_MBUF
) = (s
<< NMBPCLSHIFT
); /* in mbuf unit */
1376 m_maxsize(MC_MBUF
) = m_size(MC_MBUF
) = MSIZE
;
1377 (void) snprintf(m_cname(MC_MBUF
), MAX_MBUF_CNAME
, "mbuf");
1380 * Set limits for the composite classes.
1382 m_minlimit(MC_MBUF_CL
) = 0;
1383 m_maxlimit(MC_MBUF_CL
) = m_maxlimit(MC_CL
);
1384 m_maxsize(MC_MBUF_CL
) = MCLBYTES
;
1385 m_size(MC_MBUF_CL
) = m_size(MC_MBUF
) + m_size(MC_CL
);
1386 (void) snprintf(m_cname(MC_MBUF_CL
), MAX_MBUF_CNAME
, "mbuf_cl");
1388 m_minlimit(MC_MBUF_BIGCL
) = 0;
1389 m_maxlimit(MC_MBUF_BIGCL
) = m_maxlimit(MC_BIGCL
);
1390 m_maxsize(MC_MBUF_BIGCL
) = MBIGCLBYTES
;
1391 m_size(MC_MBUF_BIGCL
) = m_size(MC_MBUF
) + m_size(MC_BIGCL
);
1392 (void) snprintf(m_cname(MC_MBUF_BIGCL
), MAX_MBUF_CNAME
, "mbuf_bigcl");
1395 * And for jumbo classes.
1397 m_minlimit(MC_16KCL
) = 0;
1398 m_maxlimit(MC_16KCL
) = (njcl
>> NCLPJCLSHIFT
); /* in 16KB unit */
1399 m_maxsize(MC_16KCL
) = m_size(MC_16KCL
) = M16KCLBYTES
;
1400 (void) snprintf(m_cname(MC_16KCL
), MAX_MBUF_CNAME
, "16kcl");
1402 m_minlimit(MC_MBUF_16KCL
) = 0;
1403 m_maxlimit(MC_MBUF_16KCL
) = m_maxlimit(MC_16KCL
);
1404 m_maxsize(MC_MBUF_16KCL
) = M16KCLBYTES
;
1405 m_size(MC_MBUF_16KCL
) = m_size(MC_MBUF
) + m_size(MC_16KCL
);
1406 (void) snprintf(m_cname(MC_MBUF_16KCL
), MAX_MBUF_CNAME
, "mbuf_16kcl");
1409 * Initialize the legacy mbstat structure.
1411 bzero(&mbstat
, sizeof (mbstat
));
1412 mbstat
.m_msize
= m_maxsize(MC_MBUF
);
1413 mbstat
.m_mclbytes
= m_maxsize(MC_CL
);
1414 mbstat
.m_minclsize
= MINCLSIZE
;
1415 mbstat
.m_mlen
= MLEN
;
1416 mbstat
.m_mhlen
= MHLEN
;
1417 mbstat
.m_bigmclbytes
= m_maxsize(MC_BIGCL
);
1420 #if defined(__LP64__)
1421 typedef struct ncl_tbl
{
1422 uint64_t nt_maxmem
; /* memory (sane) size */
1423 uint32_t nt_mbpool
; /* mbuf pool size */
1427 static ncl_tbl_t ncl_table
[] = {
1428 { (1ULL << GBSHIFT
) /* 1 GB */, (64 << MBSHIFT
) /* 64 MB */ },
1429 { (1ULL << (GBSHIFT
+ 3)) /* 8 GB */, (96 << MBSHIFT
) /* 96 MB */ },
1430 { (1ULL << (GBSHIFT
+ 4)) /* 16 GB */, (128 << MBSHIFT
) /* 128 MB */ },
1435 static ncl_tbl_t ncl_table_srv
[] = {
1436 { (1ULL << GBSHIFT
) /* 1 GB */, (96 << MBSHIFT
) /* 96 MB */ },
1437 { (1ULL << (GBSHIFT
+ 2)) /* 4 GB */, (128 << MBSHIFT
) /* 128 MB */ },
1438 { (1ULL << (GBSHIFT
+ 3)) /* 8 GB */, (160 << MBSHIFT
) /* 160 MB */ },
1439 { (1ULL << (GBSHIFT
+ 4)) /* 16 GB */, (192 << MBSHIFT
) /* 192 MB */ },
1440 { (1ULL << (GBSHIFT
+ 5)) /* 32 GB */, (256 << MBSHIFT
) /* 256 MB */ },
1441 { (1ULL << (GBSHIFT
+ 6)) /* 64 GB */, (384 << MBSHIFT
) /* 384 MB */ },
1444 #endif /* __LP64__ */
1446 __private_extern__
unsigned int
1447 mbuf_default_ncl(int server
, uint64_t mem
)
1449 #if !defined(__LP64__)
1450 #pragma unused(server)
1453 * 32-bit kernel (default to 64MB of mbuf pool for >= 1GB RAM).
1455 if ((n
= ((mem
/ 16) / MCLBYTES
)) > 32768)
1459 ncl_tbl_t
*tbl
= (server
? ncl_table_srv
: ncl_table
);
1461 * 64-bit kernel (mbuf pool size based on table).
1463 n
= tbl
[0].nt_mbpool
;
1464 for (i
= 0; tbl
[i
].nt_mbpool
!= 0; i
++) {
1465 if (mem
< tbl
[i
].nt_maxmem
)
1467 n
= tbl
[i
].nt_mbpool
;
1470 #endif /* !__LP64__ */
1474 __private_extern__
void
1478 unsigned int initmcl
= 0;
1480 thread_t thread
= THREAD_NULL
;
1482 microuptime(&mb_start
);
1485 * These MBUF_ values must be equal to their private counterparts.
1487 _CASSERT(MBUF_EXT
== M_EXT
);
1488 _CASSERT(MBUF_PKTHDR
== M_PKTHDR
);
1489 _CASSERT(MBUF_EOR
== M_EOR
);
1490 _CASSERT(MBUF_LOOP
== M_LOOP
);
1491 _CASSERT(MBUF_BCAST
== M_BCAST
);
1492 _CASSERT(MBUF_MCAST
== M_MCAST
);
1493 _CASSERT(MBUF_FRAG
== M_FRAG
);
1494 _CASSERT(MBUF_FIRSTFRAG
== M_FIRSTFRAG
);
1495 _CASSERT(MBUF_LASTFRAG
== M_LASTFRAG
);
1496 _CASSERT(MBUF_PROMISC
== M_PROMISC
);
1497 _CASSERT(MBUF_HASFCS
== M_HASFCS
);
1499 _CASSERT(MBUF_TYPE_FREE
== MT_FREE
);
1500 _CASSERT(MBUF_TYPE_DATA
== MT_DATA
);
1501 _CASSERT(MBUF_TYPE_HEADER
== MT_HEADER
);
1502 _CASSERT(MBUF_TYPE_SOCKET
== MT_SOCKET
);
1503 _CASSERT(MBUF_TYPE_PCB
== MT_PCB
);
1504 _CASSERT(MBUF_TYPE_RTABLE
== MT_RTABLE
);
1505 _CASSERT(MBUF_TYPE_HTABLE
== MT_HTABLE
);
1506 _CASSERT(MBUF_TYPE_ATABLE
== MT_ATABLE
);
1507 _CASSERT(MBUF_TYPE_SONAME
== MT_SONAME
);
1508 _CASSERT(MBUF_TYPE_SOOPTS
== MT_SOOPTS
);
1509 _CASSERT(MBUF_TYPE_FTABLE
== MT_FTABLE
);
1510 _CASSERT(MBUF_TYPE_RIGHTS
== MT_RIGHTS
);
1511 _CASSERT(MBUF_TYPE_IFADDR
== MT_IFADDR
);
1512 _CASSERT(MBUF_TYPE_CONTROL
== MT_CONTROL
);
1513 _CASSERT(MBUF_TYPE_OOBDATA
== MT_OOBDATA
);
1515 _CASSERT(MBUF_TSO_IPV4
== CSUM_TSO_IPV4
);
1516 _CASSERT(MBUF_TSO_IPV6
== CSUM_TSO_IPV6
);
1517 _CASSERT(MBUF_CSUM_REQ_SUM16
== CSUM_PARTIAL
);
1518 _CASSERT(MBUF_CSUM_TCP_SUM16
== MBUF_CSUM_REQ_SUM16
);
1519 _CASSERT(MBUF_CSUM_REQ_ZERO_INVERT
== CSUM_ZERO_INVERT
);
1520 _CASSERT(MBUF_CSUM_REQ_IP
== CSUM_IP
);
1521 _CASSERT(MBUF_CSUM_REQ_TCP
== CSUM_TCP
);
1522 _CASSERT(MBUF_CSUM_REQ_UDP
== CSUM_UDP
);
1523 _CASSERT(MBUF_CSUM_REQ_TCPIPV6
== CSUM_TCPIPV6
);
1524 _CASSERT(MBUF_CSUM_REQ_UDPIPV6
== CSUM_UDPIPV6
);
1525 _CASSERT(MBUF_CSUM_DID_IP
== CSUM_IP_CHECKED
);
1526 _CASSERT(MBUF_CSUM_IP_GOOD
== CSUM_IP_VALID
);
1527 _CASSERT(MBUF_CSUM_DID_DATA
== CSUM_DATA_VALID
);
1528 _CASSERT(MBUF_CSUM_PSEUDO_HDR
== CSUM_PSEUDO_HDR
);
1530 _CASSERT(MBUF_WAITOK
== M_WAIT
);
1531 _CASSERT(MBUF_DONTWAIT
== M_DONTWAIT
);
1532 _CASSERT(MBUF_COPYALL
== M_COPYALL
);
1534 _CASSERT(MBUF_SC2TC(MBUF_SC_BK_SYS
) == MBUF_TC_BK
);
1535 _CASSERT(MBUF_SC2TC(MBUF_SC_BK
) == MBUF_TC_BK
);
1536 _CASSERT(MBUF_SC2TC(MBUF_SC_BE
) == MBUF_TC_BE
);
1537 _CASSERT(MBUF_SC2TC(MBUF_SC_RD
) == MBUF_TC_BE
);
1538 _CASSERT(MBUF_SC2TC(MBUF_SC_OAM
) == MBUF_TC_BE
);
1539 _CASSERT(MBUF_SC2TC(MBUF_SC_AV
) == MBUF_TC_VI
);
1540 _CASSERT(MBUF_SC2TC(MBUF_SC_RV
) == MBUF_TC_VI
);
1541 _CASSERT(MBUF_SC2TC(MBUF_SC_VI
) == MBUF_TC_VI
);
1542 _CASSERT(MBUF_SC2TC(MBUF_SC_SIG
) == MBUF_TC_VI
);
1543 _CASSERT(MBUF_SC2TC(MBUF_SC_VO
) == MBUF_TC_VO
);
1544 _CASSERT(MBUF_SC2TC(MBUF_SC_CTL
) == MBUF_TC_VO
);
1546 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BK
) == SCVAL_BK
);
1547 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BE
) == SCVAL_BE
);
1548 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VI
) == SCVAL_VI
);
1549 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VO
) == SCVAL_VO
);
1551 /* Module specific scratch space (32-bit alignment requirement) */
1552 _CASSERT(!(offsetof(struct mbuf
, m_pkthdr
.pkt_mpriv
) %
1553 sizeof (uint32_t)));
1555 /* Initialize random red zone cookie value */
1556 _CASSERT(sizeof (mb_redzone_cookie
) ==
1557 sizeof (((struct pkthdr
*)0)->redzone
));
1558 read_random(&mb_redzone_cookie
, sizeof (mb_redzone_cookie
));
1559 read_random(&mb_obscure_extref
, sizeof (mb_obscure_extref
));
1560 read_random(&mb_obscure_extfree
, sizeof (mb_obscure_extfree
));
1561 mb_obscure_extref
|= 0x3;
1562 mb_obscure_extfree
|= 0x3;
1564 /* Make sure we don't save more than we should */
1565 _CASSERT(MCA_SAVED_MBUF_SIZE
<= sizeof (struct mbuf
));
1567 if (nmbclusters
== 0)
1568 nmbclusters
= NMBCLUSTERS
;
1570 /* This should be a sane (at least even) value by now */
1571 VERIFY(nmbclusters
!= 0 && !(nmbclusters
& 0x1));
1573 /* Setup the mbuf table */
1576 /* Global lock for common layer */
1577 mbuf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1578 mbuf_mlock_grp
= lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr
);
1579 mbuf_mlock_attr
= lck_attr_alloc_init();
1580 lck_mtx_init(mbuf_mlock
, mbuf_mlock_grp
, mbuf_mlock_attr
);
1583 * Allocate cluster slabs table:
1585 * maxslabgrp = (N * 2048) / (1024 * 1024)
1587 * Where N is nmbclusters rounded up to the nearest 512. This yields
1588 * mcl_slab_g_t units, each one representing a MB of memory.
1591 (P2ROUNDUP(nmbclusters
, (MBSIZE
>> MCLSHIFT
)) << MCLSHIFT
) >> MBSHIFT
;
1592 MALLOC(slabstbl
, mcl_slabg_t
**, maxslabgrp
* sizeof (mcl_slabg_t
*),
1593 M_TEMP
, M_WAITOK
| M_ZERO
);
1594 VERIFY(slabstbl
!= NULL
);
1597 * Allocate audit structures, if needed:
1599 * maxclaudit = (maxslabgrp * 1024 * 1024) / PAGE_SIZE
1601 * This yields mcl_audit_t units, each one representing a page.
1603 PE_parse_boot_argn("mbuf_debug", &mbuf_debug
, sizeof (mbuf_debug
));
1604 mbuf_debug
|= mcache_getflags();
1605 if (mbuf_debug
& MCF_DEBUG
) {
1608 maxclaudit
= ((maxslabgrp
<< MBSHIFT
) >> PAGE_SHIFT
);
1609 MALLOC(mclaudit
, mcl_audit_t
*, maxclaudit
* sizeof (*mclaudit
),
1610 M_TEMP
, M_WAITOK
| M_ZERO
);
1611 VERIFY(mclaudit
!= NULL
);
1612 for (l
= 0, mclad
= mclaudit
; l
< maxclaudit
; l
++) {
1613 MALLOC(mclad
[l
].cl_audit
, mcache_audit_t
**,
1614 NMBPG
* sizeof(mcache_audit_t
*),
1615 M_TEMP
, M_WAITOK
| M_ZERO
);
1616 VERIFY(mclad
[l
].cl_audit
!= NULL
);
1619 mcl_audit_con_cache
= mcache_create("mcl_audit_contents",
1620 AUDIT_CONTENTS_SIZE
, sizeof (u_int64_t
), 0, MCR_SLEEP
);
1621 VERIFY(mcl_audit_con_cache
!= NULL
);
1623 mclverify
= (mbuf_debug
& MCF_VERIFY
);
1624 mcltrace
= (mbuf_debug
& MCF_TRACE
);
1625 mclfindleak
= !(mbuf_debug
& MCF_NOLEAKLOG
);
1626 mclexpleak
= mclfindleak
&& (mbuf_debug
& MCF_EXPLEAKLOG
);
1628 /* Enable mbuf leak logging, with a lock to protect the tables */
1630 mleak_lock_grp_attr
= lck_grp_attr_alloc_init();
1631 mleak_lock_grp
= lck_grp_alloc_init("mleak_lock", mleak_lock_grp_attr
);
1632 mleak_lock_attr
= lck_attr_alloc_init();
1633 lck_mtx_init(mleak_lock
, mleak_lock_grp
, mleak_lock_attr
);
1638 * Allocate structure for per-CPU statistics that's aligned
1639 * on the CPU cache boundary; this code assumes that we never
1640 * uninitialize this framework, since the original address
1641 * before alignment is not saved.
1643 ncpu
= ml_get_max_cpus();
1644 MALLOC(buf
, void *, MBUF_MTYPES_SIZE(ncpu
) + CPU_CACHE_LINE_SIZE
,
1646 VERIFY(buf
!= NULL
);
1648 mbuf_mtypes
= (mbuf_mtypes_t
*)P2ROUNDUP((intptr_t)buf
,
1649 CPU_CACHE_LINE_SIZE
);
1650 bzero(mbuf_mtypes
, MBUF_MTYPES_SIZE(ncpu
));
1652 /* Calculate the number of pages assigned to the cluster pool */
1653 mcl_pages
= (nmbclusters
<< MCLSHIFT
) / PAGE_SIZE
;
1654 MALLOC(mcl_paddr
, ppnum_t
*, mcl_pages
* sizeof (ppnum_t
),
1656 VERIFY(mcl_paddr
!= NULL
);
1658 /* Register with the I/O Bus mapper */
1659 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
1660 bzero((char *)mcl_paddr
, mcl_pages
* sizeof (ppnum_t
));
1662 embutl
= (mbutl
+ (nmbclusters
* MCLBYTES
));
1663 VERIFY(((embutl
- mbutl
) % MBIGCLBYTES
) == 0);
1665 /* Prime up the freelist */
1666 PE_parse_boot_argn("initmcl", &initmcl
, sizeof (initmcl
));
1668 initmcl
>>= NCLPBGSHIFT
; /* become a 4K unit */
1669 if (initmcl
> m_maxlimit(MC_BIGCL
))
1670 initmcl
= m_maxlimit(MC_BIGCL
);
1672 if (initmcl
< m_minlimit(MC_BIGCL
))
1673 initmcl
= m_minlimit(MC_BIGCL
);
1675 lck_mtx_lock(mbuf_mlock
);
1678 * For classes with non-zero minimum limits, populate their freelists
1679 * so that m_total(class) is at least m_minlimit(class).
1681 VERIFY(m_total(MC_BIGCL
) == 0 && m_minlimit(MC_BIGCL
) != 0);
1682 freelist_populate(m_class(MC_BIGCL
), initmcl
, M_WAIT
);
1683 VERIFY(m_total(MC_BIGCL
) >= m_minlimit(MC_BIGCL
));
1684 freelist_init(m_class(MC_CL
));
1686 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
1687 /* Make sure we didn't miss any */
1688 VERIFY(m_minlimit(m_class(m
)) == 0 ||
1689 m_total(m_class(m
)) >= m_minlimit(m_class(m
)));
1691 /* populate the initial sizes and report from there on */
1692 m_peak(m_class(m
)) = m_total(m_class(m
));
1694 mb_peak_newreport
= FALSE
;
1696 lck_mtx_unlock(mbuf_mlock
);
1698 (void) kernel_thread_start((thread_continue_t
)mbuf_worker_thread_init
,
1700 thread_deallocate(thread
);
1702 ref_cache
= mcache_create("mext_ref", sizeof (struct ext_ref
),
1705 /* Create the cache for each class */
1706 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
1707 void *allocfunc
, *freefunc
, *auditfunc
, *logfunc
;
1711 if (m_class(m
) == MC_MBUF_CL
|| m_class(m
) == MC_MBUF_BIGCL
||
1712 m_class(m
) == MC_MBUF_16KCL
) {
1713 allocfunc
= mbuf_cslab_alloc
;
1714 freefunc
= mbuf_cslab_free
;
1715 auditfunc
= mbuf_cslab_audit
;
1716 logfunc
= mleak_logger
;
1718 allocfunc
= mbuf_slab_alloc
;
1719 freefunc
= mbuf_slab_free
;
1720 auditfunc
= mbuf_slab_audit
;
1721 logfunc
= mleak_logger
;
1725 * Disable per-CPU caches for jumbo classes if there
1726 * is no jumbo cluster pool available in the system.
1727 * The cache itself is still created (but will never
1728 * be populated) since it simplifies the code.
1730 if ((m_class(m
) == MC_MBUF_16KCL
|| m_class(m
) == MC_16KCL
) &&
1732 flags
|= MCF_NOCPUCACHE
;
1735 flags
|= MCF_NOLEAKLOG
;
1737 m_cache(m
) = mcache_create_ext(m_cname(m
), m_maxsize(m
),
1738 allocfunc
, freefunc
, auditfunc
, logfunc
, mbuf_slab_notify
,
1739 (void *)(uintptr_t)m
, flags
, MCR_SLEEP
);
1743 * Set the max limit on sb_max to be 1/16 th of the size of
1744 * memory allocated for mbuf clusters.
1746 high_sb_max
= (nmbclusters
<< (MCLSHIFT
- 4));
1747 if (high_sb_max
< sb_max
) {
1748 /* sb_max is too large for this configuration, scale it down */
1749 if (high_sb_max
> (1 << MBSHIFT
)) {
1750 /* We have atleast 16 M of mbuf pool */
1751 sb_max
= high_sb_max
;
1752 } else if ((nmbclusters
<< MCLSHIFT
) > (1 << MBSHIFT
)) {
1754 * If we have more than 1M of mbufpool, cap the size of
1755 * max sock buf at 1M
1757 sb_max
= high_sb_max
= (1 << MBSHIFT
);
1759 sb_max
= high_sb_max
;
1763 /* allocate space for mbuf_dump_buf */
1764 MALLOC(mbuf_dump_buf
, char *, MBUF_DUMP_BUF_SIZE
, M_TEMP
, M_WAITOK
);
1765 VERIFY(mbuf_dump_buf
!= NULL
);
1767 if (mbuf_debug
& MCF_DEBUG
) {
1768 printf("%s: MLEN %d, MHLEN %d\n", __func__
,
1769 (int)_MLEN
, (int)_MHLEN
);
1772 printf("%s: done [%d MB total pool size, (%d/%d) split]\n", __func__
,
1773 (nmbclusters
<< MCLSHIFT
) >> MBSHIFT
,
1774 (nclusters
<< MCLSHIFT
) >> MBSHIFT
,
1775 (njcl
<< MCLSHIFT
) >> MBSHIFT
);
1777 /* initialize lock form tx completion callback table */
1778 mbuf_tx_compl_tbl_lck_grp_attr
= lck_grp_attr_alloc_init();
1779 if (mbuf_tx_compl_tbl_lck_grp_attr
== NULL
) {
1780 panic("%s: lck_grp_attr_alloc_init failed", __func__
);
1783 mbuf_tx_compl_tbl_lck_grp
= lck_grp_alloc_init("mbuf_tx_compl_tbl",
1784 mbuf_tx_compl_tbl_lck_grp_attr
);
1785 if (mbuf_tx_compl_tbl_lck_grp
== NULL
) {
1786 panic("%s: lck_grp_alloc_init failed", __func__
);
1789 mbuf_tx_compl_tbl_lck_attr
= lck_attr_alloc_init();
1790 if (mbuf_tx_compl_tbl_lck_attr
== NULL
) {
1791 panic("%s: lck_attr_alloc_init failed", __func__
);
1794 lck_rw_init(mbuf_tx_compl_tbl_lock
, mbuf_tx_compl_tbl_lck_grp
,
1795 mbuf_tx_compl_tbl_lck_attr
);
1800 * Obtain a slab of object(s) from the class's freelist.
1802 static mcache_obj_t
*
1803 slab_alloc(mbuf_class_t
class, int wait
)
1808 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1810 /* This should always be NULL for us */
1811 VERIFY(m_cobjlist(class) == NULL
);
1814 * Treat composite objects as having longer lifespan by using
1815 * a slab from the reverse direction, in hoping that this could
1816 * reduce the probability of fragmentation for slabs that hold
1817 * more than one buffer chunks (e.g. mbuf slabs). For other
1818 * slabs, this probably doesn't make much of a difference.
1820 if ((class == MC_MBUF
|| class == MC_CL
|| class == MC_BIGCL
)
1821 && (wait
& MCR_COMP
))
1822 sp
= (mcl_slab_t
*)TAILQ_LAST(&m_slablist(class), mcl_slhead
);
1824 sp
= (mcl_slab_t
*)TAILQ_FIRST(&m_slablist(class));
1827 VERIFY(m_infree(class) == 0 && m_slab_cnt(class) == 0);
1828 /* The slab list for this class is empty */
1832 VERIFY(m_infree(class) > 0);
1833 VERIFY(!slab_is_detached(sp
));
1834 VERIFY(sp
->sl_class
== class &&
1835 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
1837 VERIFY(slab_inrange(sp
, buf
) && sp
== slab_get(buf
));
1838 sp
->sl_head
= buf
->obj_next
;
1839 /* Increment slab reference */
1842 VERIFY(sp
->sl_head
!= NULL
|| sp
->sl_refcnt
== sp
->sl_chunks
);
1844 if (sp
->sl_head
!= NULL
&& !slab_inrange(sp
, sp
->sl_head
)) {
1845 slab_nextptr_panic(sp
, sp
->sl_head
);
1846 /* In case sl_head is in the map but not in the slab */
1847 VERIFY(slab_inrange(sp
, sp
->sl_head
));
1851 if (mclaudit
!= NULL
) {
1852 mcache_audit_t
*mca
= mcl_audit_buf2mca(class, buf
);
1853 mca
->mca_uflags
= 0;
1854 /* Save contents on mbuf objects only */
1855 if (class == MC_MBUF
)
1856 mca
->mca_uflags
|= MB_SCVALID
;
1859 if (class == MC_CL
) {
1860 mbstat
.m_clfree
= (--m_infree(MC_CL
)) + m_infree(MC_MBUF_CL
);
1862 * A 2K cluster slab can have at most NCLPG references.
1864 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_refcnt
<= NCLPG
&&
1865 sp
->sl_chunks
== NCLPG
&& sp
->sl_len
== PAGE_SIZE
);
1866 VERIFY(sp
->sl_refcnt
< NCLPG
|| sp
->sl_head
== NULL
);
1867 } else if (class == MC_BIGCL
) {
1868 mbstat
.m_bigclfree
= (--m_infree(MC_BIGCL
)) +
1869 m_infree(MC_MBUF_BIGCL
);
1871 * A 4K cluster slab can have NBCLPG references.
1873 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_chunks
== NBCLPG
&&
1874 sp
->sl_len
== PAGE_SIZE
&&
1875 (sp
->sl_refcnt
< NBCLPG
|| sp
->sl_head
== NULL
));
1876 } else if (class == MC_16KCL
) {
1880 --m_infree(MC_16KCL
);
1881 VERIFY(sp
->sl_refcnt
== 1 && sp
->sl_chunks
== 1 &&
1882 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1884 * Increment 2nd-Nth slab reference, where N is NSLABSP16KB.
1885 * A 16KB big cluster takes NSLABSP16KB slabs, each having at
1888 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
1890 /* Next slab must already be present */
1891 VERIFY(nsp
!= NULL
);
1893 VERIFY(!slab_is_detached(nsp
));
1894 VERIFY(nsp
->sl_class
== MC_16KCL
&&
1895 nsp
->sl_flags
== (SLF_MAPPED
| SLF_PARTIAL
) &&
1896 nsp
->sl_refcnt
== 1 && nsp
->sl_chunks
== 0 &&
1897 nsp
->sl_len
== 0 && nsp
->sl_base
== sp
->sl_base
&&
1898 nsp
->sl_head
== NULL
);
1901 VERIFY(class == MC_MBUF
);
1902 --m_infree(MC_MBUF
);
1904 * If auditing is turned on, this check is
1905 * deferred until later in mbuf_slab_audit().
1907 if (mclaudit
== NULL
)
1908 _MCHECK((struct mbuf
*)buf
);
1910 * Since we have incremented the reference count above,
1911 * an mbuf slab (formerly a 4KB cluster slab that was cut
1912 * up into mbufs) must have a reference count between 1
1913 * and NMBPG at this point.
1915 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_refcnt
<= NMBPG
&&
1916 sp
->sl_chunks
== NMBPG
&&
1917 sp
->sl_len
== PAGE_SIZE
);
1918 VERIFY(sp
->sl_refcnt
< NMBPG
|| sp
->sl_head
== NULL
);
1921 /* If empty, remove this slab from the class's freelist */
1922 if (sp
->sl_head
== NULL
) {
1923 VERIFY(class != MC_MBUF
|| sp
->sl_refcnt
== NMBPG
);
1924 VERIFY(class != MC_CL
|| sp
->sl_refcnt
== NCLPG
);
1925 VERIFY(class != MC_BIGCL
|| sp
->sl_refcnt
== NBCLPG
);
1926 slab_remove(sp
, class);
1933 * Place a slab of object(s) back into a class's slab list.
1936 slab_free(mbuf_class_t
class, mcache_obj_t
*buf
)
1939 boolean_t reinit_supercl
= false;
1940 mbuf_class_t super_class
;
1942 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1944 VERIFY(class != MC_16KCL
|| njcl
> 0);
1945 VERIFY(buf
->obj_next
== NULL
);
1948 * Synchronizing with m_clalloc, as it reads m_total, while we here
1949 * are modifying m_total.
1951 while (mb_clalloc_busy
) {
1952 mb_clalloc_waiters
++;
1953 (void) msleep(mb_clalloc_waitchan
, mbuf_mlock
,
1954 (PZERO
-1), "m_clalloc", NULL
);
1955 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1958 /* We are busy now; tell everyone else to go away */
1959 mb_clalloc_busy
= TRUE
;
1962 VERIFY(sp
->sl_class
== class && slab_inrange(sp
, buf
) &&
1963 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
1965 /* Decrement slab reference */
1968 if (class == MC_CL
) {
1969 VERIFY(IS_P2ALIGNED(buf
, MCLBYTES
));
1971 * A slab that has been splitted for 2KB clusters can have
1972 * at most 1 outstanding reference at this point.
1974 VERIFY(sp
->sl_refcnt
>= 0 && sp
->sl_refcnt
<= (NCLPG
- 1) &&
1975 sp
->sl_chunks
== NCLPG
&& sp
->sl_len
== PAGE_SIZE
);
1976 VERIFY(sp
->sl_refcnt
< (NCLPG
- 1) ||
1977 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1978 } else if (class == MC_BIGCL
) {
1979 VERIFY(IS_P2ALIGNED(buf
, MBIGCLBYTES
));
1981 /* A 4KB cluster slab can have NBCLPG references at most */
1982 VERIFY(sp
->sl_refcnt
>= 0 && sp
->sl_chunks
== NBCLPG
);
1983 VERIFY(sp
->sl_refcnt
< (NBCLPG
- 1) ||
1984 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1985 } else if (class == MC_16KCL
) {
1989 * A 16KB cluster takes NSLABSP16KB slabs, all must
1990 * now have 0 reference.
1992 VERIFY(IS_P2ALIGNED(buf
, PAGE_SIZE
));
1993 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_chunks
== 1 &&
1994 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1995 VERIFY(slab_is_detached(sp
));
1996 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
1998 /* Next slab must already be present */
1999 VERIFY(nsp
!= NULL
);
2001 VERIFY(slab_is_detached(nsp
));
2002 VERIFY(nsp
->sl_class
== MC_16KCL
&&
2003 (nsp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) &&
2004 nsp
->sl_refcnt
== 0 && nsp
->sl_chunks
== 0 &&
2005 nsp
->sl_len
== 0 && nsp
->sl_base
== sp
->sl_base
&&
2006 nsp
->sl_head
== NULL
);
2010 * A slab that has been splitted for mbufs has at most
2011 * NMBPG reference counts. Since we have decremented
2012 * one reference above, it must now be between 0 and
2015 VERIFY(class == MC_MBUF
);
2016 VERIFY(sp
->sl_refcnt
>= 0 &&
2017 sp
->sl_refcnt
<= (NMBPG
- 1) &&
2018 sp
->sl_chunks
== NMBPG
&&
2019 sp
->sl_len
== PAGE_SIZE
);
2020 VERIFY(sp
->sl_refcnt
< (NMBPG
- 1) ||
2021 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
2025 * When auditing is enabled, ensure that the buffer still
2026 * contains the free pattern. Otherwise it got corrupted
2027 * while at the CPU cache layer.
2029 if (mclaudit
!= NULL
) {
2030 mcache_audit_t
*mca
= mcl_audit_buf2mca(class, buf
);
2032 mcache_audit_free_verify(mca
, buf
, 0,
2035 mca
->mca_uflags
&= ~MB_SCVALID
;
2038 if (class == MC_CL
) {
2039 mbstat
.m_clfree
= (++m_infree(MC_CL
)) + m_infree(MC_MBUF_CL
);
2040 buf
->obj_next
= sp
->sl_head
;
2041 } else if (class == MC_BIGCL
) {
2042 mbstat
.m_bigclfree
= (++m_infree(MC_BIGCL
)) +
2043 m_infree(MC_MBUF_BIGCL
);
2044 buf
->obj_next
= sp
->sl_head
;
2045 } else if (class == MC_16KCL
) {
2046 ++m_infree(MC_16KCL
);
2048 ++m_infree(MC_MBUF
);
2049 buf
->obj_next
= sp
->sl_head
;
2054 * If a slab has been split to either one which holds 2KB clusters,
2055 * or one which holds mbufs, turn it back to one which holds a
2056 * 4 or 16 KB cluster depending on the page size.
2058 if (m_maxsize(MC_BIGCL
) == PAGE_SIZE
) {
2059 super_class
= MC_BIGCL
;
2061 VERIFY(PAGE_SIZE
== m_maxsize(MC_16KCL
));
2062 super_class
= MC_16KCL
;
2064 if (class == MC_MBUF
&& sp
->sl_refcnt
== 0 &&
2065 m_total(class) >= (m_minlimit(class) + NMBPG
) &&
2066 m_total(super_class
) < m_maxlimit(super_class
)) {
2069 m_total(MC_MBUF
) -= NMBPG
;
2070 mbstat
.m_mbufs
= m_total(MC_MBUF
);
2071 m_infree(MC_MBUF
) -= NMBPG
;
2072 mtype_stat_add(MT_FREE
, -((unsigned)NMBPG
));
2075 struct mbuf
*m
= sp
->sl_head
;
2077 sp
->sl_head
= m
->m_next
;
2080 reinit_supercl
= true;
2081 } else if (class == MC_CL
&& sp
->sl_refcnt
== 0 &&
2082 m_total(class) >= (m_minlimit(class) + NCLPG
) &&
2083 m_total(super_class
) < m_maxlimit(super_class
)) {
2086 m_total(MC_CL
) -= NCLPG
;
2087 mbstat
.m_clusters
= m_total(MC_CL
);
2088 m_infree(MC_CL
) -= NCLPG
;
2091 union mcluster
*c
= sp
->sl_head
;
2093 sp
->sl_head
= c
->mcl_next
;
2096 reinit_supercl
= true;
2097 } else if (class == MC_BIGCL
&& super_class
!= MC_BIGCL
&&
2098 sp
->sl_refcnt
== 0 &&
2099 m_total(class) >= (m_minlimit(class) + NBCLPG
) &&
2100 m_total(super_class
) < m_maxlimit(super_class
)) {
2103 VERIFY(super_class
== MC_16KCL
);
2104 m_total(MC_BIGCL
) -= NBCLPG
;
2105 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
2106 m_infree(MC_BIGCL
) -= NBCLPG
;
2109 union mbigcluster
*bc
= sp
->sl_head
;
2111 sp
->sl_head
= bc
->mbc_next
;
2112 bc
->mbc_next
= NULL
;
2114 reinit_supercl
= true;
2117 if (reinit_supercl
) {
2118 VERIFY(sp
->sl_head
== NULL
);
2119 VERIFY(m_total(class) >= m_minlimit(class));
2120 slab_remove(sp
, class);
2122 /* Reinitialize it as a cluster for the super class */
2123 m_total(super_class
)++;
2124 m_infree(super_class
)++;
2125 VERIFY(sp
->sl_flags
== (SLF_MAPPED
| SLF_DETACHED
) &&
2126 sp
->sl_len
== PAGE_SIZE
&& sp
->sl_refcnt
== 0);
2128 slab_init(sp
, super_class
, SLF_MAPPED
, sp
->sl_base
,
2129 sp
->sl_base
, PAGE_SIZE
, 0, 1);
2131 mcache_set_pattern(MCACHE_FREE_PATTERN
,
2132 (caddr_t
)sp
->sl_base
, sp
->sl_len
);
2133 ((mcache_obj_t
*)(sp
->sl_base
))->obj_next
= NULL
;
2135 if (super_class
== MC_BIGCL
) {
2136 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
2137 mbstat
.m_bigclfree
= m_infree(MC_BIGCL
) +
2138 m_infree(MC_MBUF_BIGCL
);
2141 VERIFY(slab_is_detached(sp
));
2142 VERIFY(m_total(super_class
) <= m_maxlimit(super_class
));
2144 /* And finally switch class */
2145 class = super_class
;
2148 /* Reinsert the slab to the class's slab list */
2149 if (slab_is_detached(sp
))
2150 slab_insert(sp
, class);
2152 /* We're done; let others enter */
2153 mb_clalloc_busy
= FALSE
;
2154 if (mb_clalloc_waiters
> 0) {
2155 mb_clalloc_waiters
= 0;
2156 wakeup(mb_clalloc_waitchan
);
2161 * Common allocator for rudimentary objects called by the CPU cache layer
2162 * during an allocation request whenever there is no available element in the
2163 * bucket layer. It returns one or more elements from the appropriate global
2164 * freelist. If the freelist is empty, it will attempt to populate it and
2165 * retry the allocation.
2168 mbuf_slab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int num
, int wait
)
2170 mbuf_class_t
class = (mbuf_class_t
)arg
;
2171 unsigned int need
= num
;
2172 mcache_obj_t
**list
= *plist
;
2174 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2177 lck_mtx_lock(mbuf_mlock
);
2180 if ((*list
= slab_alloc(class, wait
)) != NULL
) {
2181 (*list
)->obj_next
= NULL
;
2182 list
= *plist
= &(*list
)->obj_next
;
2186 * If the number of elements in freelist has
2187 * dropped below low watermark, asynchronously
2188 * populate the freelist now rather than doing
2189 * it later when we run out of elements.
2191 if (!mbuf_cached_above(class, wait
) &&
2192 m_infree(class) < (m_total(class) >> 5)) {
2193 (void) freelist_populate(class, 1,
2199 VERIFY(m_infree(class) == 0 || class == MC_CL
);
2201 (void) freelist_populate(class, 1,
2202 (wait
& MCR_NOSLEEP
) ? M_DONTWAIT
: M_WAIT
);
2204 if (m_infree(class) > 0)
2207 /* Check if there's anything at the cache layer */
2208 if (mbuf_cached_above(class, wait
))
2211 /* watchdog checkpoint */
2214 /* We have nothing and cannot block; give up */
2215 if (wait
& MCR_NOSLEEP
) {
2216 if (!(wait
& MCR_TRYHARD
)) {
2217 m_fail_cnt(class)++;
2224 * If the freelist is still empty and the caller is
2225 * willing to be blocked, sleep on the wait channel
2226 * until an element is available. Otherwise, if
2227 * MCR_TRYHARD is set, do our best to satisfy the
2228 * request without having to go to sleep.
2230 if (mbuf_worker_ready
&&
2231 mbuf_sleep(class, need
, wait
))
2234 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2238 m_alloc_cnt(class) += num
- need
;
2239 lck_mtx_unlock(mbuf_mlock
);
2241 return (num
- need
);
2245 * Common de-allocator for rudimentary objects called by the CPU cache
2246 * layer when one or more elements need to be returned to the appropriate
2250 mbuf_slab_free(void *arg
, mcache_obj_t
*list
, __unused
int purged
)
2252 mbuf_class_t
class = (mbuf_class_t
)arg
;
2253 mcache_obj_t
*nlist
;
2254 unsigned int num
= 0;
2257 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2259 lck_mtx_lock(mbuf_mlock
);
2262 nlist
= list
->obj_next
;
2263 list
->obj_next
= NULL
;
2264 slab_free(class, list
);
2266 if ((list
= nlist
) == NULL
)
2269 m_free_cnt(class) += num
;
2271 if ((w
= mb_waiters
) > 0)
2274 mbwdog_logger("waking up all threads");
2276 lck_mtx_unlock(mbuf_mlock
);
2279 wakeup(mb_waitchan
);
2283 * Common auditor for rudimentary objects called by the CPU cache layer
2284 * during an allocation or free request. For the former, this is called
2285 * after the objects are obtained from either the bucket or slab layer
2286 * and before they are returned to the caller. For the latter, this is
2287 * called immediately during free and before placing the objects into
2288 * the bucket or slab layer.
2291 mbuf_slab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
2293 mbuf_class_t
class = (mbuf_class_t
)arg
;
2294 mcache_audit_t
*mca
;
2296 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2298 while (list
!= NULL
) {
2299 lck_mtx_lock(mbuf_mlock
);
2300 mca
= mcl_audit_buf2mca(class, list
);
2302 /* Do the sanity checks */
2303 if (class == MC_MBUF
) {
2304 mcl_audit_mbuf(mca
, list
, FALSE
, alloc
);
2305 ASSERT(mca
->mca_uflags
& MB_SCVALID
);
2307 mcl_audit_cluster(mca
, list
, m_maxsize(class),
2309 ASSERT(!(mca
->mca_uflags
& MB_SCVALID
));
2311 /* Record this transaction */
2313 mcache_buffer_log(mca
, list
, m_cache(class), &mb_start
);
2316 mca
->mca_uflags
|= MB_INUSE
;
2318 mca
->mca_uflags
&= ~MB_INUSE
;
2319 /* Unpair the object (unconditionally) */
2320 mca
->mca_uptr
= NULL
;
2321 lck_mtx_unlock(mbuf_mlock
);
2323 list
= list
->obj_next
;
2328 * Common notify routine for all caches. It is called by mcache when
2329 * one or more objects get freed. We use this indication to trigger
2330 * the wakeup of any sleeping threads so that they can retry their
2331 * allocation requests.
2334 mbuf_slab_notify(void *arg
, u_int32_t reason
)
2336 mbuf_class_t
class = (mbuf_class_t
)arg
;
2339 ASSERT(MBUF_CLASS_VALID(class));
2341 if (reason
!= MCN_RETRYALLOC
)
2344 lck_mtx_lock(mbuf_mlock
);
2345 if ((w
= mb_waiters
) > 0) {
2346 m_notified(class)++;
2350 mbwdog_logger("waking up all threads");
2352 lck_mtx_unlock(mbuf_mlock
);
2355 wakeup(mb_waitchan
);
2359 * Obtain object(s) from the composite class's freelist.
2362 cslab_alloc(mbuf_class_t
class, mcache_obj_t
***plist
, unsigned int num
)
2364 unsigned int need
= num
;
2365 mcl_slab_t
*sp
, *clsp
, *nsp
;
2367 mcache_obj_t
**list
= *plist
;
2371 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2372 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2374 /* Get what we can from the freelist */
2375 while ((*list
= m_cobjlist(class)) != NULL
) {
2378 m
= (struct mbuf
*)*list
;
2380 cl
= m
->m_ext
.ext_buf
;
2381 clsp
= slab_get(cl
);
2382 VERIFY(m
->m_flags
== M_EXT
&& cl
!= NULL
);
2383 VERIFY(m_get_rfa(m
) != NULL
&& MBUF_IS_COMPOSITE(m
));
2385 if (class == MC_MBUF_CL
) {
2386 VERIFY(clsp
->sl_refcnt
>= 1 &&
2387 clsp
->sl_refcnt
<= NCLPG
);
2389 VERIFY(clsp
->sl_refcnt
>= 1 &&
2390 clsp
->sl_refcnt
<= NBCLPG
);
2393 if (class == MC_MBUF_16KCL
) {
2395 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2397 /* Next slab must already be present */
2398 VERIFY(nsp
!= NULL
);
2399 VERIFY(nsp
->sl_refcnt
== 1);
2403 if ((m_cobjlist(class) = (*list
)->obj_next
) != NULL
&&
2404 !MBUF_IN_MAP(m_cobjlist(class))) {
2405 slab_nextptr_panic(sp
, m_cobjlist(class));
2408 (*list
)->obj_next
= NULL
;
2409 list
= *plist
= &(*list
)->obj_next
;
2414 m_infree(class) -= (num
- need
);
2416 return (num
- need
);
2420 * Place object(s) back into a composite class's freelist.
2423 cslab_free(mbuf_class_t
class, mcache_obj_t
*list
, int purged
)
2425 mcache_obj_t
*o
, *tail
;
2426 unsigned int num
= 0;
2427 struct mbuf
*m
, *ms
;
2428 mcache_audit_t
*mca
= NULL
;
2429 mcache_obj_t
*ref_list
= NULL
;
2430 mcl_slab_t
*clsp
, *nsp
;
2432 mbuf_class_t cl_class
;
2434 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2435 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2436 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2438 if (class == MC_MBUF_CL
) {
2440 } else if (class == MC_MBUF_BIGCL
) {
2441 cl_class
= MC_BIGCL
;
2443 VERIFY(class == MC_MBUF_16KCL
);
2444 cl_class
= MC_16KCL
;
2449 while ((m
= ms
= (struct mbuf
*)o
) != NULL
) {
2450 mcache_obj_t
*rfa
, *nexto
= o
->obj_next
;
2452 /* Do the mbuf sanity checks */
2453 if (mclaudit
!= NULL
) {
2454 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2456 mcache_audit_free_verify(mca
, m
, 0,
2457 m_maxsize(MC_MBUF
));
2459 ms
= MCA_SAVED_MBUF_PTR(mca
);
2462 /* Do the cluster sanity checks */
2463 cl
= ms
->m_ext
.ext_buf
;
2464 clsp
= slab_get(cl
);
2466 size_t size
= m_maxsize(cl_class
);
2467 mcache_audit_free_verify(mcl_audit_buf2mca(cl_class
,
2468 (mcache_obj_t
*)cl
), cl
, 0, size
);
2470 VERIFY(ms
->m_type
== MT_FREE
);
2471 VERIFY(ms
->m_flags
== M_EXT
);
2472 VERIFY(m_get_rfa(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2473 if (cl_class
== MC_CL
) {
2474 VERIFY(clsp
->sl_refcnt
>= 1 &&
2475 clsp
->sl_refcnt
<= NCLPG
);
2477 VERIFY(clsp
->sl_refcnt
>= 1 &&
2478 clsp
->sl_refcnt
<= NBCLPG
);
2480 if (cl_class
== MC_16KCL
) {
2482 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2484 /* Next slab must already be present */
2485 VERIFY(nsp
!= NULL
);
2486 VERIFY(nsp
->sl_refcnt
== 1);
2491 * If we're asked to purge, restore the actual mbuf using
2492 * contents of the shadow structure (if auditing is enabled)
2493 * and clear EXTF_COMPOSITE flag from the mbuf, as we are
2494 * about to free it and the attached cluster into their caches.
2497 /* Restore constructed mbuf fields */
2498 if (mclaudit
!= NULL
)
2499 mcl_audit_restore_mbuf(m
, mca
, TRUE
);
2506 MEXT_PMBUF(m
) = NULL
;
2509 rfa
= (mcache_obj_t
*)(void *)m_get_rfa(m
);
2510 m_set_ext(m
, NULL
, NULL
, NULL
);
2511 rfa
->obj_next
= ref_list
;
2514 m
->m_type
= MT_FREE
;
2515 m
->m_flags
= m
->m_len
= 0;
2516 m
->m_next
= m
->m_nextpkt
= NULL
;
2518 /* Save mbuf fields and make auditing happy */
2519 if (mclaudit
!= NULL
)
2520 mcl_audit_mbuf(mca
, o
, FALSE
, FALSE
);
2522 VERIFY(m_total(class) > 0);
2527 slab_free(MC_MBUF
, o
);
2529 /* And free the cluster */
2530 ((mcache_obj_t
*)cl
)->obj_next
= NULL
;
2531 if (class == MC_MBUF_CL
)
2532 slab_free(MC_CL
, cl
);
2533 else if (class == MC_MBUF_BIGCL
)
2534 slab_free(MC_BIGCL
, cl
);
2536 slab_free(MC_16KCL
, cl
);
2545 tail
->obj_next
= m_cobjlist(class);
2546 m_cobjlist(class) = list
;
2547 m_infree(class) += num
;
2548 } else if (ref_list
!= NULL
) {
2549 mcache_free_ext(ref_cache
, ref_list
);
2556 * Common allocator for composite objects called by the CPU cache layer
2557 * during an allocation request whenever there is no available element in
2558 * the bucket layer. It returns one or more composite elements from the
2559 * appropriate global freelist. If the freelist is empty, it will attempt
2560 * to obtain the rudimentary objects from their caches and construct them
2561 * into composite mbuf + cluster objects.
2564 mbuf_cslab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int needed
,
2567 mbuf_class_t
class = (mbuf_class_t
)arg
;
2568 mbuf_class_t cl_class
= 0;
2569 unsigned int num
= 0, cnum
= 0, want
= needed
;
2570 mcache_obj_t
*ref_list
= NULL
;
2571 mcache_obj_t
*mp_list
= NULL
;
2572 mcache_obj_t
*clp_list
= NULL
;
2573 mcache_obj_t
**list
;
2574 struct ext_ref
*rfa
;
2578 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2581 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2583 /* There should not be any slab for this class */
2584 VERIFY(m_slab_cnt(class) == 0 &&
2585 m_slablist(class).tqh_first
== NULL
&&
2586 m_slablist(class).tqh_last
== NULL
);
2588 lck_mtx_lock(mbuf_mlock
);
2590 /* Try using the freelist first */
2591 num
= cslab_alloc(class, plist
, needed
);
2593 if (num
== needed
) {
2594 m_alloc_cnt(class) += num
;
2595 lck_mtx_unlock(mbuf_mlock
);
2599 lck_mtx_unlock(mbuf_mlock
);
2602 * We could not satisfy the request using the freelist alone;
2603 * allocate from the appropriate rudimentary caches and use
2604 * whatever we can get to construct the composite objects.
2609 * Mark these allocation requests as coming from a composite cache.
2610 * Also, if the caller is willing to be blocked, mark the request
2611 * with MCR_FAILOK such that we don't end up sleeping at the mbuf
2612 * slab layer waiting for the individual object when one or more
2613 * of the already-constructed composite objects are available.
2616 if (!(wait
& MCR_NOSLEEP
))
2619 /* allocate mbufs */
2620 needed
= mcache_alloc_ext(m_cache(MC_MBUF
), &mp_list
, needed
, wait
);
2622 ASSERT(mp_list
== NULL
);
2626 /* allocate clusters */
2627 if (class == MC_MBUF_CL
) {
2629 } else if (class == MC_MBUF_BIGCL
) {
2630 cl_class
= MC_BIGCL
;
2632 VERIFY(class == MC_MBUF_16KCL
);
2633 cl_class
= MC_16KCL
;
2635 needed
= mcache_alloc_ext(m_cache(cl_class
), &clp_list
, needed
, wait
);
2637 ASSERT(clp_list
== NULL
);
2641 needed
= mcache_alloc_ext(ref_cache
, &ref_list
, needed
, wait
);
2643 ASSERT(ref_list
== NULL
);
2648 * By this time "needed" is MIN(mbuf, cluster, ref). Any left
2649 * overs will get freed accordingly before we return to caller.
2651 for (cnum
= 0; cnum
< needed
; cnum
++) {
2654 m
= ms
= (struct mbuf
*)mp_list
;
2655 mp_list
= mp_list
->obj_next
;
2658 clp_list
= clp_list
->obj_next
;
2659 ((mcache_obj_t
*)cl
)->obj_next
= NULL
;
2661 rfa
= (struct ext_ref
*)ref_list
;
2662 ref_list
= ref_list
->obj_next
;
2663 ((mcache_obj_t
*)(void *)rfa
)->obj_next
= NULL
;
2666 * If auditing is enabled, construct the shadow mbuf
2667 * in the audit structure instead of in the actual one.
2668 * mbuf_cslab_audit() will take care of restoring the
2669 * contents after the integrity check.
2671 if (mclaudit
!= NULL
) {
2672 mcache_audit_t
*mca
, *cl_mca
;
2674 lck_mtx_lock(mbuf_mlock
);
2675 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2676 ms
= MCA_SAVED_MBUF_PTR(mca
);
2677 cl_mca
= mcl_audit_buf2mca(cl_class
,
2678 (mcache_obj_t
*)cl
);
2681 * Pair them up. Note that this is done at the time
2682 * the mbuf+cluster objects are constructed. This
2683 * information should be treated as "best effort"
2684 * debugging hint since more than one mbufs can refer
2685 * to a cluster. In that case, the cluster might not
2686 * be freed along with the mbuf it was paired with.
2688 mca
->mca_uptr
= cl_mca
;
2689 cl_mca
->mca_uptr
= mca
;
2691 ASSERT(mca
->mca_uflags
& MB_SCVALID
);
2692 ASSERT(!(cl_mca
->mca_uflags
& MB_SCVALID
));
2693 lck_mtx_unlock(mbuf_mlock
);
2695 /* Technically, they are in the freelist */
2699 mcache_set_pattern(MCACHE_FREE_PATTERN
, m
,
2700 m_maxsize(MC_MBUF
));
2702 if (class == MC_MBUF_CL
)
2703 size
= m_maxsize(MC_CL
);
2704 else if (class == MC_MBUF_BIGCL
)
2705 size
= m_maxsize(MC_BIGCL
);
2707 size
= m_maxsize(MC_16KCL
);
2709 mcache_set_pattern(MCACHE_FREE_PATTERN
, cl
,
2714 MBUF_INIT(ms
, 0, MT_FREE
);
2715 if (class == MC_MBUF_16KCL
) {
2716 MBUF_16KCL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2717 } else if (class == MC_MBUF_BIGCL
) {
2718 MBUF_BIGCL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2720 MBUF_CL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2722 VERIFY(ms
->m_flags
== M_EXT
);
2723 VERIFY(m_get_rfa(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2725 *list
= (mcache_obj_t
*)m
;
2726 (*list
)->obj_next
= NULL
;
2727 list
= *plist
= &(*list
)->obj_next
;
2732 * Free up what's left of the above.
2734 if (mp_list
!= NULL
)
2735 mcache_free_ext(m_cache(MC_MBUF
), mp_list
);
2736 if (clp_list
!= NULL
)
2737 mcache_free_ext(m_cache(cl_class
), clp_list
);
2738 if (ref_list
!= NULL
)
2739 mcache_free_ext(ref_cache
, ref_list
);
2741 lck_mtx_lock(mbuf_mlock
);
2742 if (num
> 0 || cnum
> 0) {
2743 m_total(class) += cnum
;
2744 VERIFY(m_total(class) <= m_maxlimit(class));
2745 m_alloc_cnt(class) += num
+ cnum
;
2747 if ((num
+ cnum
) < want
)
2748 m_fail_cnt(class) += (want
- (num
+ cnum
));
2749 lck_mtx_unlock(mbuf_mlock
);
2751 return (num
+ cnum
);
2755 * Common de-allocator for composite objects called by the CPU cache
2756 * layer when one or more elements need to be returned to the appropriate
2760 mbuf_cslab_free(void *arg
, mcache_obj_t
*list
, int purged
)
2762 mbuf_class_t
class = (mbuf_class_t
)arg
;
2766 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2768 lck_mtx_lock(mbuf_mlock
);
2770 num
= cslab_free(class, list
, purged
);
2771 m_free_cnt(class) += num
;
2773 if ((w
= mb_waiters
) > 0)
2776 mbwdog_logger("waking up all threads");
2779 lck_mtx_unlock(mbuf_mlock
);
2782 wakeup(mb_waitchan
);
2786 * Common auditor for composite objects called by the CPU cache layer
2787 * during an allocation or free request. For the former, this is called
2788 * after the objects are obtained from either the bucket or slab layer
2789 * and before they are returned to the caller. For the latter, this is
2790 * called immediately during free and before placing the objects into
2791 * the bucket or slab layer.
2794 mbuf_cslab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
2796 mbuf_class_t
class = (mbuf_class_t
)arg
, cl_class
;
2797 mcache_audit_t
*mca
;
2798 struct mbuf
*m
, *ms
;
2799 mcl_slab_t
*clsp
, *nsp
;
2803 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2804 if (class == MC_MBUF_CL
)
2806 else if (class == MC_MBUF_BIGCL
)
2807 cl_class
= MC_BIGCL
;
2809 cl_class
= MC_16KCL
;
2810 cl_size
= m_maxsize(cl_class
);
2812 while ((m
= ms
= (struct mbuf
*)list
) != NULL
) {
2813 lck_mtx_lock(mbuf_mlock
);
2814 /* Do the mbuf sanity checks and record its transaction */
2815 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2816 mcl_audit_mbuf(mca
, m
, TRUE
, alloc
);
2818 mcache_buffer_log(mca
, m
, m_cache(class), &mb_start
);
2821 mca
->mca_uflags
|= MB_COMP_INUSE
;
2823 mca
->mca_uflags
&= ~MB_COMP_INUSE
;
2826 * Use the shadow mbuf in the audit structure if we are
2827 * freeing, since the contents of the actual mbuf has been
2828 * pattern-filled by the above call to mcl_audit_mbuf().
2830 if (!alloc
&& mclverify
)
2831 ms
= MCA_SAVED_MBUF_PTR(mca
);
2833 /* Do the cluster sanity checks and record its transaction */
2834 cl
= ms
->m_ext
.ext_buf
;
2835 clsp
= slab_get(cl
);
2836 VERIFY(ms
->m_flags
== M_EXT
&& cl
!= NULL
);
2837 VERIFY(m_get_rfa(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2838 if (class == MC_MBUF_CL
)
2839 VERIFY(clsp
->sl_refcnt
>= 1 &&
2840 clsp
->sl_refcnt
<= NCLPG
);
2842 VERIFY(clsp
->sl_refcnt
>= 1 &&
2843 clsp
->sl_refcnt
<= NBCLPG
);
2845 if (class == MC_MBUF_16KCL
) {
2847 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2849 /* Next slab must already be present */
2850 VERIFY(nsp
!= NULL
);
2851 VERIFY(nsp
->sl_refcnt
== 1);
2856 mca
= mcl_audit_buf2mca(cl_class
, cl
);
2857 mcl_audit_cluster(mca
, cl
, cl_size
, alloc
, FALSE
);
2859 mcache_buffer_log(mca
, cl
, m_cache(class), &mb_start
);
2862 mca
->mca_uflags
|= MB_COMP_INUSE
;
2864 mca
->mca_uflags
&= ~MB_COMP_INUSE
;
2865 lck_mtx_unlock(mbuf_mlock
);
2867 list
= list
->obj_next
;
2872 m_vm_error_stats(uint32_t *cnt
, uint64_t *ts
, uint64_t *size
,
2873 uint64_t alloc_size
, kern_return_t error
)
2881 _CASSERT(sizeof(mb_kmem_stats
) / sizeof(mb_kmem_stats
[0]) ==
2882 sizeof(mb_kmem_stats_labels
) / sizeof(mb_kmem_stats_labels
[0]));
2886 case KERN_INVALID_ARGUMENT
:
2889 case KERN_INVALID_ADDRESS
:
2892 case KERN_RESOURCE_SHORTAGE
:
2908 * Allocate some number of mbuf clusters and place on cluster freelist.
2911 m_clalloc(const u_int32_t num
, const int wait
, const u_int32_t bufsize
)
2915 int numpages
= 0, large_buffer
;
2916 vm_offset_t page
= 0;
2917 mcache_audit_t
*mca_list
= NULL
;
2918 mcache_obj_t
*con_list
= NULL
;
2921 kern_return_t error
;
2923 /* Set if a buffer allocation needs allocation of multiple pages */
2924 large_buffer
= ((bufsize
== m_maxsize(MC_16KCL
)) &&
2925 PAGE_SIZE
< M16KCLBYTES
);
2926 VERIFY(bufsize
== m_maxsize(MC_BIGCL
) ||
2927 bufsize
== m_maxsize(MC_16KCL
));
2929 VERIFY((bufsize
== PAGE_SIZE
) ||
2930 (bufsize
> PAGE_SIZE
&& bufsize
== m_maxsize(MC_16KCL
)));
2932 if (bufsize
== m_size(MC_BIGCL
))
2937 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2940 * Multiple threads may attempt to populate the cluster map one
2941 * after another. Since we drop the lock below prior to acquiring
2942 * the physical page(s), our view of the cluster map may no longer
2943 * be accurate, and we could end up over-committing the pages beyond
2944 * the maximum allowed for each class. To prevent it, this entire
2945 * operation (including the page mapping) is serialized.
2947 while (mb_clalloc_busy
) {
2948 mb_clalloc_waiters
++;
2949 (void) msleep(mb_clalloc_waitchan
, mbuf_mlock
,
2950 (PZERO
-1), "m_clalloc", NULL
);
2951 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2954 /* We are busy now; tell everyone else to go away */
2955 mb_clalloc_busy
= TRUE
;
2958 * Honor the caller's wish to block or not block. We have a way
2959 * to grow the pool asynchronously using the mbuf worker thread.
2961 i
= m_howmany(num
, bufsize
);
2962 if (i
<= 0 || (wait
& M_DONTWAIT
))
2965 lck_mtx_unlock(mbuf_mlock
);
2967 size
= round_page(i
* bufsize
);
2968 page
= kmem_mb_alloc(mb_map
, size
, large_buffer
, &error
);
2971 * If we did ask for "n" 16KB physically contiguous chunks
2972 * and didn't get them, then please try again without this
2975 net_update_uptime();
2976 if (large_buffer
&& page
== 0) {
2977 m_vm_error_stats(&mb_kmem_contig_failed
,
2978 &mb_kmem_contig_failed_ts
,
2979 &mb_kmem_contig_failed_size
,
2981 page
= kmem_mb_alloc(mb_map
, size
, 0, &error
);
2985 m_vm_error_stats(&mb_kmem_failed
,
2987 &mb_kmem_failed_size
,
2989 #if PAGE_SIZE == 4096
2990 if (bufsize
== m_maxsize(MC_BIGCL
)) {
2992 if (bufsize
>= m_maxsize(MC_BIGCL
)) {
2994 /* Try for 1 page if failed */
2996 page
= kmem_mb_alloc(mb_map
, size
, 0, &error
);
2998 m_vm_error_stats(&mb_kmem_one_failed
,
2999 &mb_kmem_one_failed_ts
,
3005 lck_mtx_lock(mbuf_mlock
);
3010 VERIFY(IS_P2ALIGNED(page
, PAGE_SIZE
));
3011 numpages
= size
/ PAGE_SIZE
;
3013 /* If auditing is enabled, allocate the audit structures now */
3014 if (mclaudit
!= NULL
) {
3018 * Yes, I realize this is a waste of memory for clusters
3019 * that never get transformed into mbufs, as we may end
3020 * up with NMBPG-1 unused audit structures per cluster.
3021 * But doing so tremendously simplifies the allocation
3022 * strategy, since at this point we are not holding the
3023 * mbuf lock and the caller is okay to be blocked.
3025 if (bufsize
== PAGE_SIZE
) {
3026 needed
= numpages
* NMBPG
;
3028 i
= mcache_alloc_ext(mcl_audit_con_cache
,
3029 &con_list
, needed
, MCR_SLEEP
);
3031 VERIFY(con_list
!= NULL
&& i
== needed
);
3034 * if multiple 4K pages are being used for a
3037 needed
= numpages
/ NSLABSP16KB
;
3040 i
= mcache_alloc_ext(mcache_audit_cache
,
3041 (mcache_obj_t
**)&mca_list
, needed
, MCR_SLEEP
);
3043 VERIFY(mca_list
!= NULL
&& i
== needed
);
3046 lck_mtx_lock(mbuf_mlock
);
3048 for (i
= 0; i
< numpages
; i
++, page
+= PAGE_SIZE
) {
3050 ((unsigned char *)page
- mbutl
) >> PAGE_SHIFT
;
3051 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, page
);
3054 * If there is a mapper the appropriate I/O page is
3055 * returned; zero out the page to discard its past
3056 * contents to prevent exposing leftover kernel memory.
3058 VERIFY(offset
< mcl_pages
);
3059 if (mcl_paddr_base
!= 0) {
3060 bzero((void *)(uintptr_t) page
, PAGE_SIZE
);
3061 new_page
= IOMapperInsertPage(mcl_paddr_base
,
3064 mcl_paddr
[offset
] = new_page
;
3066 /* Pattern-fill this fresh page */
3068 mcache_set_pattern(MCACHE_FREE_PATTERN
,
3069 (caddr_t
)page
, PAGE_SIZE
);
3071 if (bufsize
== PAGE_SIZE
) {
3073 /* One for the entire page */
3074 sp
= slab_get((void *)page
);
3075 if (mclaudit
!= NULL
) {
3076 mcl_audit_init((void *)page
,
3077 &mca_list
, &con_list
,
3078 AUDIT_CONTENTS_SIZE
, NMBPG
);
3080 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_flags
== 0);
3081 slab_init(sp
, class, SLF_MAPPED
, (void *)page
,
3082 (void *)page
, PAGE_SIZE
, 0, 1);
3083 buf
= (mcache_obj_t
*)page
;
3084 buf
->obj_next
= NULL
;
3086 /* Insert this slab */
3087 slab_insert(sp
, class);
3089 /* Update stats now since slab_get drops the lock */
3092 VERIFY(m_total(class) <= m_maxlimit(class));
3093 if (class == MC_BIGCL
) {
3094 mbstat
.m_bigclfree
= m_infree(MC_BIGCL
) +
3095 m_infree(MC_MBUF_BIGCL
);
3096 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
3099 } else if ((bufsize
> PAGE_SIZE
) &&
3100 (i
% NSLABSP16KB
) == 0) {
3101 union m16kcluster
*m16kcl
= (union m16kcluster
*)page
;
3105 /* One for the entire 16KB */
3106 sp
= slab_get(m16kcl
);
3107 if (mclaudit
!= NULL
)
3108 mcl_audit_init(m16kcl
, &mca_list
, NULL
, 0, 1);
3110 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_flags
== 0);
3111 slab_init(sp
, MC_16KCL
, SLF_MAPPED
,
3112 m16kcl
, m16kcl
, bufsize
, 0, 1);
3113 m16kcl
->m16kcl_next
= NULL
;
3116 * 2nd-Nth page's slab is part of the first one,
3117 * where N is NSLABSP16KB.
3119 for (k
= 1; k
< NSLABSP16KB
; k
++) {
3120 nsp
= slab_get(((union mbigcluster
*)page
) + k
);
3121 VERIFY(nsp
->sl_refcnt
== 0 &&
3122 nsp
->sl_flags
== 0);
3123 slab_init(nsp
, MC_16KCL
,
3124 SLF_MAPPED
| SLF_PARTIAL
,
3125 m16kcl
, NULL
, 0, 0, 0);
3127 /* Insert this slab */
3128 slab_insert(sp
, MC_16KCL
);
3130 /* Update stats now since slab_get drops the lock */
3131 ++m_infree(MC_16KCL
);
3132 ++m_total(MC_16KCL
);
3133 VERIFY(m_total(MC_16KCL
) <= m_maxlimit(MC_16KCL
));
3137 VERIFY(mca_list
== NULL
&& con_list
== NULL
);
3139 if (!mb_peak_newreport
&& mbuf_report_usage(class))
3140 mb_peak_newreport
= TRUE
;
3142 /* We're done; let others enter */
3143 mb_clalloc_busy
= FALSE
;
3144 if (mb_clalloc_waiters
> 0) {
3145 mb_clalloc_waiters
= 0;
3146 wakeup(mb_clalloc_waitchan
);
3151 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3153 mtracelarge_register(size
);
3155 /* We're done; let others enter */
3156 mb_clalloc_busy
= FALSE
;
3157 if (mb_clalloc_waiters
> 0) {
3158 mb_clalloc_waiters
= 0;
3159 wakeup(mb_clalloc_waitchan
);
3163 * When non-blocking we kick a thread if we have to grow the
3164 * pool or if the number of free clusters is less than requested.
3166 if (i
> 0 && mbuf_worker_ready
&& mbuf_worker_needs_wakeup
) {
3167 mbwdog_logger("waking up the worker thread to to grow %s by %d",
3169 wakeup((caddr_t
)&mbuf_worker_needs_wakeup
);
3170 mbuf_worker_needs_wakeup
= FALSE
;
3172 if (class == MC_BIGCL
) {
3175 * Remember total number of 4KB clusters needed
3178 i
+= m_total(MC_BIGCL
);
3179 if (i
> m_region_expand(MC_BIGCL
)) {
3180 m_region_expand(MC_BIGCL
) = i
;
3183 if (m_infree(MC_BIGCL
) >= num
)
3188 * Remember total number of 16KB clusters needed
3191 i
+= m_total(MC_16KCL
);
3192 if (i
> m_region_expand(MC_16KCL
)) {
3193 m_region_expand(MC_16KCL
) = i
;
3196 if (m_infree(MC_16KCL
) >= num
)
3203 * Populate the global freelist of the corresponding buffer class.
3206 freelist_populate(mbuf_class_t
class, unsigned int num
, int wait
)
3208 mcache_obj_t
*o
= NULL
;
3209 int i
, numpages
= 0, count
;
3210 mbuf_class_t super_class
;
3212 VERIFY(class == MC_MBUF
|| class == MC_CL
|| class == MC_BIGCL
||
3215 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3217 VERIFY(PAGE_SIZE
== m_maxsize(MC_BIGCL
) ||
3218 PAGE_SIZE
== m_maxsize(MC_16KCL
));
3220 if (m_maxsize(class) >= PAGE_SIZE
)
3221 return(m_clalloc(num
, wait
, m_maxsize(class)) != 0);
3224 * The rest of the function will allocate pages and will slice
3225 * them up into the right size
3228 numpages
= (num
* m_size(class) + PAGE_SIZE
- 1) / PAGE_SIZE
;
3230 /* Currently assume that pages are 4K or 16K */
3231 if (PAGE_SIZE
== m_maxsize(MC_BIGCL
))
3232 super_class
= MC_BIGCL
;
3234 super_class
= MC_16KCL
;
3236 i
= m_clalloc(numpages
, wait
, m_maxsize(super_class
));
3238 /* how many objects will we cut the page into? */
3239 int numobj
= PAGE_SIZE
/ m_maxsize(class);
3241 for (count
= 0; count
< numpages
; count
++) {
3242 /* respect totals, minlimit, maxlimit */
3243 if (m_total(super_class
) <= m_minlimit(super_class
) ||
3244 m_total(class) >= m_maxlimit(class))
3247 if ((o
= slab_alloc(super_class
, wait
)) == NULL
)
3250 struct mbuf
*m
= (struct mbuf
*)o
;
3251 union mcluster
*c
= (union mcluster
*)o
;
3252 union mbigcluster
*mbc
= (union mbigcluster
*)o
;
3253 mcl_slab_t
*sp
= slab_get(o
);
3254 mcache_audit_t
*mca
= NULL
;
3257 * since one full page will be converted to MC_MBUF or
3258 * MC_CL, verify that the reference count will match that
3261 VERIFY(sp
->sl_refcnt
== 1 && slab_is_detached(sp
));
3262 VERIFY((sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
3264 * Make sure that the cluster is unmolested
3268 mca
= mcl_audit_buf2mca(super_class
,
3270 mcache_audit_free_verify(mca
,
3271 (mcache_obj_t
*)o
, 0, m_maxsize(super_class
));
3274 /* Reinitialize it as an mbuf or 2K or 4K slab */
3275 slab_init(sp
, class, sp
->sl_flags
,
3276 sp
->sl_base
, NULL
, PAGE_SIZE
, 0, numobj
);
3278 VERIFY(sp
->sl_head
== NULL
);
3280 VERIFY(m_total(super_class
) >= 1);
3281 m_total(super_class
)--;
3283 if (super_class
== MC_BIGCL
)
3284 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
3286 m_total(class) += numobj
;
3287 VERIFY(m_total(class) <= m_maxlimit(class));
3288 m_infree(class) += numobj
;
3290 if (!mb_peak_newreport
&& mbuf_report_usage(class))
3291 mb_peak_newreport
= TRUE
;
3294 if (class == MC_MBUF
) {
3295 mbstat
.m_mbufs
= m_total(MC_MBUF
);
3296 mtype_stat_add(MT_FREE
, NMBPG
);
3299 * If auditing is enabled, construct the
3300 * shadow mbuf in the audit structure
3301 * instead of the actual one.
3302 * mbuf_slab_audit() will take care of
3303 * restoring the contents after the
3306 if (mclaudit
!= NULL
) {
3308 mca
= mcl_audit_buf2mca(MC_MBUF
,
3310 ms
= MCA_SAVED_MBUF_PTR(mca
);
3311 ms
->m_type
= MT_FREE
;
3313 m
->m_type
= MT_FREE
;
3315 m
->m_next
= sp
->sl_head
;
3316 sp
->sl_head
= (void *)m
++;
3318 } else if (class == MC_CL
) { /* MC_CL */
3320 m_infree(MC_CL
) + m_infree(MC_MBUF_CL
);
3321 mbstat
.m_clusters
= m_total(MC_CL
);
3323 c
->mcl_next
= sp
->sl_head
;
3324 sp
->sl_head
= (void *)c
++;
3327 VERIFY(class == MC_BIGCL
);
3328 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
3329 mbstat
.m_bigclfree
= m_infree(MC_BIGCL
) +
3330 m_infree(MC_MBUF_BIGCL
);
3332 mbc
->mbc_next
= sp
->sl_head
;
3333 sp
->sl_head
= (void *)mbc
++;
3337 /* Insert into the mbuf or 2k or 4k slab list */
3338 slab_insert(sp
, class);
3340 if ((i
= mb_waiters
) > 0)
3343 mbwdog_logger("waking up all threads");
3344 wakeup(mb_waitchan
);
3347 return (count
!= 0);
3351 * For each class, initialize the freelist to hold m_minlimit() objects.
3354 freelist_init(mbuf_class_t
class)
3356 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3358 VERIFY(class == MC_CL
|| class == MC_BIGCL
);
3359 VERIFY(m_total(class) == 0);
3360 VERIFY(m_minlimit(class) > 0);
3362 while (m_total(class) < m_minlimit(class))
3363 (void) freelist_populate(class, m_minlimit(class), M_WAIT
);
3365 VERIFY(m_total(class) >= m_minlimit(class));
3369 * (Inaccurately) check if it might be worth a trip back to the
3370 * mcache layer due the availability of objects there. We'll
3371 * end up back here if there's nothing up there.
3374 mbuf_cached_above(mbuf_class_t
class, int wait
)
3378 if (wait
& MCR_COMP
)
3379 return (!mcache_bkt_isempty(m_cache(MC_MBUF_CL
)) ||
3380 !mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL
)));
3384 if (wait
& MCR_COMP
)
3385 return (!mcache_bkt_isempty(m_cache(MC_MBUF_CL
)));
3389 if (wait
& MCR_COMP
)
3390 return (!mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL
)));
3394 if (wait
& MCR_COMP
)
3395 return (!mcache_bkt_isempty(m_cache(MC_MBUF_16KCL
)));
3408 return (!mcache_bkt_isempty(m_cache(class)));
3412 * If possible, convert constructed objects to raw ones.
3415 mbuf_steal(mbuf_class_t
class, unsigned int num
)
3417 mcache_obj_t
*top
= NULL
;
3418 mcache_obj_t
**list
= &top
;
3419 unsigned int tot
= 0;
3421 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3433 /* Get the required number of constructed objects if possible */
3434 if (m_infree(class) > m_minlimit(class)) {
3435 tot
= cslab_alloc(class, &list
,
3436 MIN(num
, m_infree(class)));
3439 /* And destroy them to get back the raw objects */
3441 (void) cslab_free(class, top
, 1);
3449 return (tot
== num
);
3453 m_reclaim(mbuf_class_t
class, unsigned int num
, boolean_t comp
)
3457 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3459 VERIFY(m_total(MC_CL
) <= m_maxlimit(MC_CL
));
3460 VERIFY(m_total(MC_BIGCL
) <= m_maxlimit(MC_BIGCL
));
3461 VERIFY(m_total(MC_16KCL
) <= m_maxlimit(MC_16KCL
));
3464 * This logic can be made smarter; for now, simply mark
3465 * all other related classes as potential victims.
3469 m_wantpurge(MC_CL
)++;
3470 m_wantpurge(MC_BIGCL
)++;
3471 m_wantpurge(MC_MBUF_CL
)++;
3472 m_wantpurge(MC_MBUF_BIGCL
)++;
3476 m_wantpurge(MC_MBUF
)++;
3477 m_wantpurge(MC_BIGCL
)++;
3478 m_wantpurge(MC_MBUF_BIGCL
)++;
3480 m_wantpurge(MC_MBUF_CL
)++;
3484 m_wantpurge(MC_MBUF
)++;
3485 m_wantpurge(MC_CL
)++;
3486 m_wantpurge(MC_MBUF_CL
)++;
3488 m_wantpurge(MC_MBUF_BIGCL
)++;
3493 m_wantpurge(MC_MBUF_16KCL
)++;
3502 * Run through each marked class and check if we really need to
3503 * purge (and therefore temporarily disable) the per-CPU caches
3504 * layer used by the class. If so, remember the classes since
3505 * we are going to drop the lock below prior to purging.
3507 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
3508 if (m_wantpurge(m
) > 0) {
3511 * Try hard to steal the required number of objects
3512 * from the freelist of other mbuf classes. Only
3513 * purge and disable the per-CPU caches layer when
3514 * we don't have enough; it's the last resort.
3516 if (!mbuf_steal(m
, num
))
3521 lck_mtx_unlock(mbuf_mlock
);
3524 /* signal the domains to drain */
3525 net_drain_domains();
3527 /* Sigh; we have no other choices but to ask mcache to purge */
3528 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
3529 if ((bmap
& (1 << m
)) &&
3530 mcache_purge_cache(m_cache(m
), TRUE
)) {
3531 lck_mtx_lock(mbuf_mlock
);
3534 lck_mtx_unlock(mbuf_mlock
);
3539 * Request mcache to reap extra elements from all of its caches;
3540 * note that all reaps are serialized and happen only at a fixed
3545 lck_mtx_lock(mbuf_mlock
);
3548 static inline struct mbuf
*
3549 m_get_common(int wait
, short type
, int hdr
)
3552 int mcflags
= MSLEEPF(wait
);
3554 /* Is this due to a non-blocking retry? If so, then try harder */
3555 if (mcflags
& MCR_NOSLEEP
)
3556 mcflags
|= MCR_TRYHARD
;
3558 m
= mcache_alloc(m_cache(MC_MBUF
), mcflags
);
3560 MBUF_INIT(m
, hdr
, type
);
3561 mtype_stat_inc(type
);
3562 mtype_stat_dec(MT_FREE
);
3564 if (hdr
&& mac_init_mbuf(m
, wait
) != 0) {
3568 #endif /* MAC_NET */
3574 * Space allocation routines; these are also available as macros
3575 * for critical paths.
3577 #define _M_GET(wait, type) m_get_common(wait, type, 0)
3578 #define _M_GETHDR(wait, type) m_get_common(wait, type, 1)
3579 #define _M_RETRY(wait, type) _M_GET(wait, type)
3580 #define _M_RETRYHDR(wait, type) _M_GETHDR(wait, type)
3581 #define _MGET(m, how, type) ((m) = _M_GET(how, type))
3582 #define _MGETHDR(m, how, type) ((m) = _M_GETHDR(how, type))
3585 m_get(int wait
, int type
)
3587 return (_M_GET(wait
, type
));
3591 m_gethdr(int wait
, int type
)
3593 return (_M_GETHDR(wait
, type
));
3597 m_retry(int wait
, int type
)
3599 return (_M_RETRY(wait
, type
));
3603 m_retryhdr(int wait
, int type
)
3605 return (_M_RETRYHDR(wait
, type
));
3609 m_getclr(int wait
, int type
)
3613 _MGET(m
, wait
, type
);
3615 bzero(MTOD(m
, caddr_t
), MLEN
);
3620 m_free_paired(struct mbuf
*m
)
3622 VERIFY((m
->m_flags
& M_EXT
) && (MEXT_FLAGS(m
) & EXTF_PAIRED
));
3625 if (MEXT_PMBUF(m
) == m
) {
3626 volatile UInt16
*addr
= (volatile UInt16
*)&MEXT_PREF(m
);
3627 int16_t oprefcnt
, prefcnt
;
3630 * Paired ref count might be negative in case we lose
3631 * against another thread clearing MEXT_PMBUF, in the
3632 * event it occurs after the above memory barrier sync.
3633 * In that case just ignore as things have been unpaired.
3637 prefcnt
= oprefcnt
- 1;
3638 } while (!OSCompareAndSwap16(oprefcnt
, prefcnt
, addr
));
3642 } else if (prefcnt
== 1) {
3643 (*(m_get_ext_free(m
)))(m
->m_ext
.ext_buf
,
3644 m
->m_ext
.ext_size
, m_get_ext_arg(m
));
3646 } else if (prefcnt
== 0) {
3647 VERIFY(MBUF_IS_PAIRED(m
));
3650 * Restore minref to its natural value, so that
3651 * the caller will be able to free the cluster
3657 * Clear MEXT_PMBUF, but leave EXTF_PAIRED intact
3658 * as it is immutable. atomic_set_ptr also causes
3659 * memory barrier sync.
3661 atomic_set_ptr(&MEXT_PMBUF(m
), NULL
);
3663 switch (m
->m_ext
.ext_size
) {
3665 m_set_ext(m
, m_get_rfa(m
), NULL
, NULL
);
3669 m_set_ext(m
, m_get_rfa(m
), m_bigfree
, NULL
);
3673 m_set_ext(m
, m_get_rfa(m
), m_16kfree
, NULL
);
3684 * Tell caller the unpair has occurred, and that the reference
3685 * count on the external cluster held for the paired mbuf should
3692 m_free(struct mbuf
*m
)
3694 struct mbuf
*n
= m
->m_next
;
3696 if (m
->m_type
== MT_FREE
)
3697 panic("m_free: freeing an already freed mbuf");
3699 if (m
->m_flags
& M_PKTHDR
) {
3700 /* Check for scratch area overflow */
3701 m_redzone_verify(m
);
3702 /* Free the aux data and tags if there is any */
3703 m_tag_delete_chain(m
, NULL
);
3705 m_do_tx_compl_callback(m
, NULL
);
3708 if (m
->m_flags
& M_EXT
) {
3710 u_int32_t composite
;
3711 m_ext_free_func_t m_free_func
;
3713 if (MBUF_IS_PAIRED(m
) && m_free_paired(m
))
3716 refcnt
= m_decref(m
);
3717 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
3718 m_free_func
= m_get_ext_free(m
);
3720 if (refcnt
== MEXT_MINREF(m
) && !composite
) {
3721 if (m_free_func
== NULL
) {
3722 mcache_free(m_cache(MC_CL
), m
->m_ext
.ext_buf
);
3723 } else if (m_free_func
== m_bigfree
) {
3724 mcache_free(m_cache(MC_BIGCL
),
3726 } else if (m_free_func
== m_16kfree
) {
3727 mcache_free(m_cache(MC_16KCL
),
3730 (*m_free_func
)(m
->m_ext
.ext_buf
,
3731 m
->m_ext
.ext_size
, m_get_ext_arg(m
));
3733 mcache_free(ref_cache
, m_get_rfa(m
));
3734 m_set_ext(m
, NULL
, NULL
, NULL
);
3735 } else if (refcnt
== MEXT_MINREF(m
) && composite
) {
3736 VERIFY(!(MEXT_FLAGS(m
) & EXTF_PAIRED
));
3737 VERIFY(m
->m_type
!= MT_FREE
);
3739 mtype_stat_dec(m
->m_type
);
3740 mtype_stat_inc(MT_FREE
);
3742 m
->m_type
= MT_FREE
;
3745 m
->m_next
= m
->m_nextpkt
= NULL
;
3747 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
3749 /* "Free" into the intermediate cache */
3750 if (m_free_func
== NULL
) {
3751 mcache_free(m_cache(MC_MBUF_CL
), m
);
3752 } else if (m_free_func
== m_bigfree
) {
3753 mcache_free(m_cache(MC_MBUF_BIGCL
), m
);
3755 VERIFY(m_free_func
== m_16kfree
);
3756 mcache_free(m_cache(MC_MBUF_16KCL
), m
);
3762 if (m
->m_type
!= MT_FREE
) {
3763 mtype_stat_dec(m
->m_type
);
3764 mtype_stat_inc(MT_FREE
);
3767 m
->m_type
= MT_FREE
;
3768 m
->m_flags
= m
->m_len
= 0;
3769 m
->m_next
= m
->m_nextpkt
= NULL
;
3771 mcache_free(m_cache(MC_MBUF
), m
);
3776 __private_extern__
struct mbuf
*
3777 m_clattach(struct mbuf
*m
, int type
, caddr_t extbuf
,
3778 void (*extfree
)(caddr_t
, u_int
, caddr_t
), u_int extsize
, caddr_t extarg
,
3781 struct ext_ref
*rfa
= NULL
;
3784 * If pairing is requested and an existing mbuf is provided, reject
3785 * it if it's already been paired to another cluster. Otherwise,
3786 * allocate a new one or free any existing below.
3788 if ((m
!= NULL
&& MBUF_IS_PAIRED(m
)) ||
3789 (m
== NULL
&& (m
= _M_GETHDR(wait
, type
)) == NULL
))
3792 if (m
->m_flags
& M_EXT
) {
3794 u_int32_t composite
;
3795 m_ext_free_func_t m_free_func
;
3797 refcnt
= m_decref(m
);
3798 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
3799 VERIFY(!(MEXT_FLAGS(m
) & EXTF_PAIRED
) && MEXT_PMBUF(m
) == NULL
);
3800 m_free_func
= m_get_ext_free(m
);
3801 if (refcnt
== MEXT_MINREF(m
) && !composite
) {
3802 if (m_free_func
== NULL
) {
3803 mcache_free(m_cache(MC_CL
), m
->m_ext
.ext_buf
);
3804 } else if (m_free_func
== m_bigfree
) {
3805 mcache_free(m_cache(MC_BIGCL
),
3807 } else if (m_free_func
== m_16kfree
) {
3808 mcache_free(m_cache(MC_16KCL
),
3811 (*m_free_func
)(m
->m_ext
.ext_buf
,
3812 m
->m_ext
.ext_size
, m_get_ext_arg(m
));
3814 /* Re-use the reference structure */
3816 } else if (refcnt
== MEXT_MINREF(m
) && composite
) {
3817 VERIFY(m
->m_type
!= MT_FREE
);
3819 mtype_stat_dec(m
->m_type
);
3820 mtype_stat_inc(MT_FREE
);
3822 m
->m_type
= MT_FREE
;
3825 m
->m_next
= m
->m_nextpkt
= NULL
;
3827 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
3829 /* "Free" into the intermediate cache */
3830 if (m_free_func
== NULL
) {
3831 mcache_free(m_cache(MC_MBUF_CL
), m
);
3832 } else if (m_free_func
== m_bigfree
) {
3833 mcache_free(m_cache(MC_MBUF_BIGCL
), m
);
3835 VERIFY(m_free_func
== m_16kfree
);
3836 mcache_free(m_cache(MC_MBUF_16KCL
), m
);
3839 * Allocate a new mbuf, since we didn't divorce
3840 * the composite mbuf + cluster pair above.
3842 if ((m
= _M_GETHDR(wait
, type
)) == NULL
)
3848 (rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
) {
3854 MEXT_INIT(m
, extbuf
, extsize
, extfree
, extarg
, rfa
,
3855 0, 1, 0, 0, 0, NULL
);
3857 MEXT_INIT(m
, extbuf
, extsize
, extfree
, (caddr_t
)m
, rfa
,
3858 1, 1, 1, EXTF_PAIRED
, 0, m
);
3865 * Perform `fast' allocation mbuf clusters from a cache of recently-freed
3866 * clusters. (If the cache is empty, new clusters are allocated en-masse.)
3869 m_getcl(int wait
, int type
, int flags
)
3872 int mcflags
= MSLEEPF(wait
);
3873 int hdr
= (flags
& M_PKTHDR
);
3875 /* Is this due to a non-blocking retry? If so, then try harder */
3876 if (mcflags
& MCR_NOSLEEP
)
3877 mcflags
|= MCR_TRYHARD
;
3879 m
= mcache_alloc(m_cache(MC_MBUF_CL
), mcflags
);
3882 struct ext_ref
*rfa
;
3885 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
3886 cl
= m
->m_ext
.ext_buf
;
3889 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
3890 VERIFY(MBUF_IS_COMPOSITE(m
) && m_get_ext_free(m
) == NULL
);
3892 flag
= MEXT_FLAGS(m
);
3894 MBUF_INIT(m
, hdr
, type
);
3895 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
3897 mtype_stat_inc(type
);
3898 mtype_stat_dec(MT_FREE
);
3900 if (hdr
&& mac_init_mbuf(m
, wait
) != 0) {
3904 #endif /* MAC_NET */
3909 /* m_mclget() add an mbuf cluster to a normal mbuf */
3911 m_mclget(struct mbuf
*m
, int wait
)
3913 struct ext_ref
*rfa
;
3915 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3918 m
->m_ext
.ext_buf
= m_mclalloc(wait
);
3919 if (m
->m_ext
.ext_buf
!= NULL
) {
3920 MBUF_CL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3922 mcache_free(ref_cache
, rfa
);
3927 /* Allocate an mbuf cluster */
3929 m_mclalloc(int wait
)
3931 int mcflags
= MSLEEPF(wait
);
3933 /* Is this due to a non-blocking retry? If so, then try harder */
3934 if (mcflags
& MCR_NOSLEEP
)
3935 mcflags
|= MCR_TRYHARD
;
3937 return (mcache_alloc(m_cache(MC_CL
), mcflags
));
3940 /* Free an mbuf cluster */
3942 m_mclfree(caddr_t p
)
3944 mcache_free(m_cache(MC_CL
), p
);
3948 * mcl_hasreference() checks if a cluster of an mbuf is referenced by
3949 * another mbuf; see comments in m_incref() regarding EXTF_READONLY.
3952 m_mclhasreference(struct mbuf
*m
)
3954 if (!(m
->m_flags
& M_EXT
))
3957 ASSERT(m_get_rfa(m
) != NULL
);
3959 return ((MEXT_FLAGS(m
) & EXTF_READONLY
) ? 1 : 0);
3962 __private_extern__ caddr_t
3963 m_bigalloc(int wait
)
3965 int mcflags
= MSLEEPF(wait
);
3967 /* Is this due to a non-blocking retry? If so, then try harder */
3968 if (mcflags
& MCR_NOSLEEP
)
3969 mcflags
|= MCR_TRYHARD
;
3971 return (mcache_alloc(m_cache(MC_BIGCL
), mcflags
));
3974 __private_extern__
void
3975 m_bigfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
3977 mcache_free(m_cache(MC_BIGCL
), p
);
3980 /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */
3981 __private_extern__
struct mbuf
*
3982 m_mbigget(struct mbuf
*m
, int wait
)
3984 struct ext_ref
*rfa
;
3986 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3989 m
->m_ext
.ext_buf
= m_bigalloc(wait
);
3990 if (m
->m_ext
.ext_buf
!= NULL
) {
3991 MBUF_BIGCL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3993 mcache_free(ref_cache
, rfa
);
3998 __private_extern__ caddr_t
3999 m_16kalloc(int wait
)
4001 int mcflags
= MSLEEPF(wait
);
4003 /* Is this due to a non-blocking retry? If so, then try harder */
4004 if (mcflags
& MCR_NOSLEEP
)
4005 mcflags
|= MCR_TRYHARD
;
4007 return (mcache_alloc(m_cache(MC_16KCL
), mcflags
));
4010 __private_extern__
void
4011 m_16kfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
4013 mcache_free(m_cache(MC_16KCL
), p
);
4016 /* m_m16kget() add a 16KB mbuf cluster to a normal mbuf */
4017 __private_extern__
struct mbuf
*
4018 m_m16kget(struct mbuf
*m
, int wait
)
4020 struct ext_ref
*rfa
;
4022 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
4025 m
->m_ext
.ext_buf
= m_16kalloc(wait
);
4026 if (m
->m_ext
.ext_buf
!= NULL
) {
4027 MBUF_16KCL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
4029 mcache_free(ref_cache
, rfa
);
4035 * "Move" mbuf pkthdr from "from" to "to".
4036 * "from" must have M_PKTHDR set, and "to" must be empty.
4039 m_copy_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
4041 VERIFY(from
->m_flags
& M_PKTHDR
);
4043 /* Check for scratch area overflow */
4044 m_redzone_verify(from
);
4046 if (to
->m_flags
& M_PKTHDR
) {
4047 /* Check for scratch area overflow */
4048 m_redzone_verify(to
);
4049 /* We will be taking over the tags of 'to' */
4050 m_tag_delete_chain(to
, NULL
);
4052 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
4053 m_classifier_init(from
, 0); /* purge classifier info */
4054 m_tag_init(from
, 1); /* purge all tags from src */
4055 m_scratch_init(from
); /* clear src scratch area */
4056 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
4057 if ((to
->m_flags
& M_EXT
) == 0)
4058 to
->m_data
= to
->m_pktdat
;
4059 m_redzone_init(to
); /* setup red zone on dst */
4063 * Duplicate "from"'s mbuf pkthdr in "to".
4064 * "from" must have M_PKTHDR set, and "to" must be empty.
4065 * In particular, this does a deep copy of the packet tags.
4068 m_dup_pkthdr(struct mbuf
*to
, struct mbuf
*from
, int how
)
4070 VERIFY(from
->m_flags
& M_PKTHDR
);
4072 /* Check for scratch area overflow */
4073 m_redzone_verify(from
);
4075 if (to
->m_flags
& M_PKTHDR
) {
4076 /* Check for scratch area overflow */
4077 m_redzone_verify(to
);
4078 /* We will be taking over the tags of 'to' */
4079 m_tag_delete_chain(to
, NULL
);
4081 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
4082 if ((to
->m_flags
& M_EXT
) == 0)
4083 to
->m_data
= to
->m_pktdat
;
4084 to
->m_pkthdr
= from
->m_pkthdr
;
4085 m_redzone_init(to
); /* setup red zone on dst */
4086 m_tag_init(to
, 0); /* preserve dst static tags */
4087 return (m_tag_copy_chain(to
, from
, how
));
4091 m_copy_pftag(struct mbuf
*to
, struct mbuf
*from
)
4093 memcpy(m_pftag(to
), m_pftag(from
), sizeof(struct pf_mtag
));
4095 m_pftag(to
)->pftag_hdr
= NULL
;
4096 m_pftag(to
)->pftag_flags
&= ~(PF_TAG_HDR_INET
|PF_TAG_HDR_INET6
);
4101 m_classifier_init(struct mbuf
*m
, uint32_t pktf_mask
)
4103 VERIFY(m
->m_flags
& M_PKTHDR
);
4105 m
->m_pkthdr
.pkt_proto
= 0;
4106 m
->m_pkthdr
.pkt_flowsrc
= 0;
4107 m
->m_pkthdr
.pkt_flowid
= 0;
4108 m
->m_pkthdr
.pkt_flags
&= pktf_mask
; /* caller-defined mask */
4109 /* preserve service class and interface info for loopback packets */
4110 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
))
4111 (void) m_set_service_class(m
, MBUF_SC_BE
);
4112 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
4113 m
->m_pkthdr
.pkt_ifainfo
= 0;
4115 * Preserve timestamp if requested
4117 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_TS_VALID
))
4118 m
->m_pkthdr
.pkt_timestamp
= 0;
4122 m_copy_classifier(struct mbuf
*to
, struct mbuf
*from
)
4124 VERIFY(to
->m_flags
& M_PKTHDR
);
4125 VERIFY(from
->m_flags
& M_PKTHDR
);
4127 to
->m_pkthdr
.pkt_proto
= from
->m_pkthdr
.pkt_proto
;
4128 to
->m_pkthdr
.pkt_flowsrc
= from
->m_pkthdr
.pkt_flowsrc
;
4129 to
->m_pkthdr
.pkt_flowid
= from
->m_pkthdr
.pkt_flowid
;
4130 to
->m_pkthdr
.pkt_flags
= from
->m_pkthdr
.pkt_flags
;
4131 (void) m_set_service_class(to
, from
->m_pkthdr
.pkt_svc
);
4132 to
->m_pkthdr
.pkt_ifainfo
= from
->m_pkthdr
.pkt_ifainfo
;
4136 * Return a list of mbuf hdrs that point to clusters. Try for num_needed;
4137 * if wantall is not set, return whatever number were available. Set up the
4138 * first num_with_pkthdrs with mbuf hdrs configured as packet headers; these
4139 * are chained on the m_nextpkt field. Any packets requested beyond this
4140 * are chained onto the last packet header's m_next field. The size of
4141 * the cluster is controlled by the parameter bufsize.
4143 __private_extern__
struct mbuf
*
4144 m_getpackets_internal(unsigned int *num_needed
, int num_with_pkthdrs
,
4145 int wait
, int wantall
, size_t bufsize
)
4148 struct mbuf
**np
, *top
;
4149 unsigned int pnum
, needed
= *num_needed
;
4150 mcache_obj_t
*mp_list
= NULL
;
4151 int mcflags
= MSLEEPF(wait
);
4153 struct ext_ref
*rfa
;
4157 ASSERT(bufsize
== m_maxsize(MC_CL
) ||
4158 bufsize
== m_maxsize(MC_BIGCL
) ||
4159 bufsize
== m_maxsize(MC_16KCL
));
4162 * Caller must first check for njcl because this
4163 * routine is internal and not exposed/used via KPI.
4165 VERIFY(bufsize
!= m_maxsize(MC_16KCL
) || njcl
> 0);
4172 * The caller doesn't want all the requested buffers; only some.
4173 * Try hard to get what we can, but don't block. This effectively
4174 * overrides MCR_SLEEP, since this thread will not go to sleep
4175 * if we can't get all the buffers.
4177 if (!wantall
|| (mcflags
& MCR_NOSLEEP
))
4178 mcflags
|= MCR_TRYHARD
;
4180 /* Allocate the composite mbuf + cluster elements from the cache */
4181 if (bufsize
== m_maxsize(MC_CL
))
4182 cp
= m_cache(MC_MBUF_CL
);
4183 else if (bufsize
== m_maxsize(MC_BIGCL
))
4184 cp
= m_cache(MC_MBUF_BIGCL
);
4186 cp
= m_cache(MC_MBUF_16KCL
);
4187 needed
= mcache_alloc_ext(cp
, &mp_list
, needed
, mcflags
);
4189 for (pnum
= 0; pnum
< needed
; pnum
++) {
4190 m
= (struct mbuf
*)mp_list
;
4191 mp_list
= mp_list
->obj_next
;
4193 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
4194 cl
= m
->m_ext
.ext_buf
;
4197 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
4198 VERIFY(MBUF_IS_COMPOSITE(m
));
4200 flag
= MEXT_FLAGS(m
);
4202 MBUF_INIT(m
, num_with_pkthdrs
, MT_DATA
);
4203 if (bufsize
== m_maxsize(MC_16KCL
)) {
4204 MBUF_16KCL_INIT(m
, cl
, rfa
, 1, flag
);
4205 } else if (bufsize
== m_maxsize(MC_BIGCL
)) {
4206 MBUF_BIGCL_INIT(m
, cl
, rfa
, 1, flag
);
4208 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
4211 if (num_with_pkthdrs
> 0) {
4214 if (mac_mbuf_label_init(m
, wait
) != 0) {
4218 #endif /* MAC_NET */
4222 if (num_with_pkthdrs
> 0)
4227 ASSERT(pnum
!= *num_needed
|| mp_list
== NULL
);
4228 if (mp_list
!= NULL
)
4229 mcache_free_ext(cp
, mp_list
);
4232 mtype_stat_add(MT_DATA
, pnum
);
4233 mtype_stat_sub(MT_FREE
, pnum
);
4236 if (wantall
&& (pnum
!= *num_needed
)) {
4242 if (pnum
> *num_needed
) {
4243 printf("%s: File a radar related to <rdar://10146739>. \
4244 needed = %u, pnum = %u, num_needed = %u \n",
4245 __func__
, needed
, pnum
, *num_needed
);
4253 * Return list of mbuf linked by m_nextpkt. Try for numlist, and if
4254 * wantall is not set, return whatever number were available. The size of
4255 * each mbuf in the list is controlled by the parameter packetlen. Each
4256 * mbuf of the list may have a chain of mbufs linked by m_next. Each mbuf
4257 * in the chain is called a segment. If maxsegments is not null and the
4258 * value pointed to is not null, this specify the maximum number of segments
4259 * for a chain of mbufs. If maxsegments is zero or the value pointed to
4260 * is zero the caller does not have any restriction on the number of segments.
4261 * The actual number of segments of a mbuf chain is return in the value
4262 * pointed to by maxsegments.
4264 __private_extern__
struct mbuf
*
4265 m_allocpacket_internal(unsigned int *numlist
, size_t packetlen
,
4266 unsigned int *maxsegments
, int wait
, int wantall
, size_t wantsize
)
4268 struct mbuf
**np
, *top
, *first
= NULL
;
4269 size_t bufsize
, r_bufsize
;
4270 unsigned int num
= 0;
4271 unsigned int nsegs
= 0;
4272 unsigned int needed
, resid
;
4273 int mcflags
= MSLEEPF(wait
);
4274 mcache_obj_t
*mp_list
= NULL
, *rmp_list
= NULL
;
4275 mcache_t
*cp
= NULL
, *rcp
= NULL
;
4283 if (wantsize
== 0) {
4284 if (packetlen
<= MINCLSIZE
) {
4285 bufsize
= packetlen
;
4286 } else if (packetlen
> m_maxsize(MC_CL
)) {
4287 /* Use 4KB if jumbo cluster pool isn't available */
4288 if (packetlen
<= m_maxsize(MC_BIGCL
) || njcl
== 0)
4289 bufsize
= m_maxsize(MC_BIGCL
);
4291 bufsize
= m_maxsize(MC_16KCL
);
4293 bufsize
= m_maxsize(MC_CL
);
4295 } else if (wantsize
== m_maxsize(MC_CL
) ||
4296 wantsize
== m_maxsize(MC_BIGCL
) ||
4297 (wantsize
== m_maxsize(MC_16KCL
) && njcl
> 0)) {
4303 if (bufsize
<= MHLEN
) {
4305 } else if (bufsize
<= MINCLSIZE
) {
4306 if (maxsegments
!= NULL
&& *maxsegments
== 1) {
4307 bufsize
= m_maxsize(MC_CL
);
4312 } else if (bufsize
== m_maxsize(MC_16KCL
)) {
4314 nsegs
= ((packetlen
- 1) >> M16KCLSHIFT
) + 1;
4315 } else if (bufsize
== m_maxsize(MC_BIGCL
)) {
4316 nsegs
= ((packetlen
- 1) >> MBIGCLSHIFT
) + 1;
4318 nsegs
= ((packetlen
- 1) >> MCLSHIFT
) + 1;
4320 if (maxsegments
!= NULL
) {
4321 if (*maxsegments
&& nsegs
> *maxsegments
) {
4322 *maxsegments
= nsegs
;
4325 *maxsegments
= nsegs
;
4329 * The caller doesn't want all the requested buffers; only some.
4330 * Try hard to get what we can, but don't block. This effectively
4331 * overrides MCR_SLEEP, since this thread will not go to sleep
4332 * if we can't get all the buffers.
4334 if (!wantall
|| (mcflags
& MCR_NOSLEEP
))
4335 mcflags
|= MCR_TRYHARD
;
4338 * Simple case where all elements in the lists/chains are mbufs.
4339 * Unless bufsize is greater than MHLEN, each segment chain is made
4340 * up of exactly 1 mbuf. Otherwise, each segment chain is made up
4341 * of 2 mbufs; the second one is used for the residual data, i.e.
4342 * the remaining data that cannot fit into the first mbuf.
4344 if (bufsize
<= MINCLSIZE
) {
4345 /* Allocate the elements in one shot from the mbuf cache */
4346 ASSERT(bufsize
<= MHLEN
|| nsegs
== 2);
4347 cp
= m_cache(MC_MBUF
);
4348 needed
= mcache_alloc_ext(cp
, &mp_list
,
4349 (*numlist
) * nsegs
, mcflags
);
4352 * The number of elements must be even if we are to use an
4353 * mbuf (instead of a cluster) to store the residual data.
4354 * If we couldn't allocate the requested number of mbufs,
4355 * trim the number down (if it's odd) in order to avoid
4356 * creating a partial segment chain.
4358 if (bufsize
> MHLEN
&& (needed
& 0x1))
4361 while (num
< needed
) {
4364 m
= (struct mbuf
*)mp_list
;
4365 mp_list
= mp_list
->obj_next
;
4368 MBUF_INIT(m
, 1, MT_DATA
);
4370 if (mac_init_mbuf(m
, wait
) != 0) {
4374 #endif /* MAC_NET */
4376 if (bufsize
> MHLEN
) {
4377 /* A second mbuf for this segment chain */
4378 m
->m_next
= (struct mbuf
*)mp_list
;
4379 mp_list
= mp_list
->obj_next
;
4380 ASSERT(m
->m_next
!= NULL
);
4382 MBUF_INIT(m
->m_next
, 0, MT_DATA
);
4388 ASSERT(num
!= *numlist
|| mp_list
== NULL
);
4391 mtype_stat_add(MT_DATA
, num
);
4392 mtype_stat_sub(MT_FREE
, num
);
4396 /* We've got them all; return to caller */
4397 if (num
== *numlist
)
4404 * Complex cases where elements are made up of one or more composite
4405 * mbufs + cluster, depending on packetlen. Each N-segment chain can
4406 * be illustrated as follows:
4408 * [mbuf + cluster 1] [mbuf + cluster 2] ... [mbuf + cluster N]
4410 * Every composite mbuf + cluster element comes from the intermediate
4411 * cache (either MC_MBUF_CL or MC_MBUF_BIGCL). For space efficiency,
4412 * the last composite element will come from the MC_MBUF_CL cache,
4413 * unless the residual data is larger than 2KB where we use the
4414 * big cluster composite cache (MC_MBUF_BIGCL) instead. Residual
4415 * data is defined as extra data beyond the first element that cannot
4416 * fit into the previous element, i.e. there is no residual data if
4417 * the chain only has 1 segment.
4419 r_bufsize
= bufsize
;
4420 resid
= packetlen
> bufsize
? packetlen
% bufsize
: 0;
4422 /* There is residual data; figure out the cluster size */
4423 if (wantsize
== 0 && packetlen
> MINCLSIZE
) {
4425 * Caller didn't request that all of the segments
4426 * in the chain use the same cluster size; use the
4427 * smaller of the cluster sizes.
4429 if (njcl
> 0 && resid
> m_maxsize(MC_BIGCL
))
4430 r_bufsize
= m_maxsize(MC_16KCL
);
4431 else if (resid
> m_maxsize(MC_CL
))
4432 r_bufsize
= m_maxsize(MC_BIGCL
);
4434 r_bufsize
= m_maxsize(MC_CL
);
4436 /* Use the same cluster size as the other segments */
4444 * Attempt to allocate composite mbuf + cluster elements for
4445 * the residual data in each chain; record the number of such
4446 * elements that can be allocated so that we know how many
4447 * segment chains we can afford to create.
4449 if (r_bufsize
<= m_maxsize(MC_CL
))
4450 rcp
= m_cache(MC_MBUF_CL
);
4451 else if (r_bufsize
<= m_maxsize(MC_BIGCL
))
4452 rcp
= m_cache(MC_MBUF_BIGCL
);
4454 rcp
= m_cache(MC_MBUF_16KCL
);
4455 needed
= mcache_alloc_ext(rcp
, &rmp_list
, *numlist
, mcflags
);
4460 /* This is temporarily reduced for calculation */
4466 * Attempt to allocate the rest of the composite mbuf + cluster
4467 * elements for the number of segment chains that we need.
4469 if (bufsize
<= m_maxsize(MC_CL
))
4470 cp
= m_cache(MC_MBUF_CL
);
4471 else if (bufsize
<= m_maxsize(MC_BIGCL
))
4472 cp
= m_cache(MC_MBUF_BIGCL
);
4474 cp
= m_cache(MC_MBUF_16KCL
);
4475 needed
= mcache_alloc_ext(cp
, &mp_list
, needed
* nsegs
, mcflags
);
4477 /* Round it down to avoid creating a partial segment chain */
4478 needed
= (needed
/ nsegs
) * nsegs
;
4484 * We're about to construct the chain(s); take into account
4485 * the number of segments we have created above to hold the
4486 * residual data for each chain, as well as restore the
4487 * original count of segments per chain.
4490 needed
+= needed
/ nsegs
;
4497 struct ext_ref
*rfa
;
4500 m_ext_free_func_t m_free_func
;
4503 if (nsegs
== 1 || (num
% nsegs
) != 0 || resid
== 0) {
4504 m
= (struct mbuf
*)mp_list
;
4505 mp_list
= mp_list
->obj_next
;
4507 m
= (struct mbuf
*)rmp_list
;
4508 rmp_list
= rmp_list
->obj_next
;
4510 m_free_func
= m_get_ext_free(m
);
4512 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
4513 VERIFY(m_free_func
== NULL
|| m_free_func
== m_bigfree
||
4514 m_free_func
== m_16kfree
);
4516 cl
= m
->m_ext
.ext_buf
;
4519 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
4520 VERIFY(MBUF_IS_COMPOSITE(m
));
4522 flag
= MEXT_FLAGS(m
);
4524 pkthdr
= (nsegs
== 1 || (num
% nsegs
) == 1);
4527 MBUF_INIT(m
, pkthdr
, MT_DATA
);
4528 if (m_free_func
== m_16kfree
) {
4529 MBUF_16KCL_INIT(m
, cl
, rfa
, 1, flag
);
4530 } else if (m_free_func
== m_bigfree
) {
4531 MBUF_BIGCL_INIT(m
, cl
, rfa
, 1, flag
);
4533 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
4536 if (pkthdr
&& mac_init_mbuf(m
, wait
) != 0) {
4541 #endif /* MAC_NET */
4544 if ((num
% nsegs
) == 0)
4545 np
= &first
->m_nextpkt
;
4554 mtype_stat_add(MT_DATA
, num
);
4555 mtype_stat_sub(MT_FREE
, num
);
4560 /* We've got them all; return to caller */
4561 if (num
== *numlist
) {
4562 ASSERT(mp_list
== NULL
&& rmp_list
== NULL
);
4567 /* Free up what's left of the above */
4568 if (mp_list
!= NULL
)
4569 mcache_free_ext(cp
, mp_list
);
4570 if (rmp_list
!= NULL
)
4571 mcache_free_ext(rcp
, rmp_list
);
4572 if (wantall
&& top
!= NULL
) {
4581 * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated
4582 * packets on receive ring.
4584 __private_extern__
struct mbuf
*
4585 m_getpacket_how(int wait
)
4587 unsigned int num_needed
= 1;
4589 return (m_getpackets_internal(&num_needed
, 1, wait
, 1,
4594 * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated
4595 * packets on receive ring.
4600 unsigned int num_needed
= 1;
4602 return (m_getpackets_internal(&num_needed
, 1, M_WAIT
, 1,
4607 * Return a list of mbuf hdrs that point to clusters. Try for num_needed;
4608 * if this can't be met, return whatever number were available. Set up the
4609 * first num_with_pkthdrs with mbuf hdrs configured as packet headers. These
4610 * are chained on the m_nextpkt field. Any packets requested beyond this are
4611 * chained onto the last packet header's m_next field.
4614 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
4616 unsigned int n
= num_needed
;
4618 return (m_getpackets_internal(&n
, num_with_pkthdrs
, how
, 0,
4623 * Return a list of mbuf hdrs set up as packet hdrs chained together
4624 * on the m_nextpkt field
4627 m_getpackethdrs(int num_needed
, int how
)
4630 struct mbuf
**np
, *top
;
4635 while (num_needed
--) {
4636 m
= _M_RETRYHDR(how
, MT_DATA
);
4648 * Free an mbuf list (m_nextpkt) while following m_next. Returns the count
4649 * for mbufs packets freed. Used by the drivers.
4652 m_freem_list(struct mbuf
*m
)
4654 struct mbuf
*nextpkt
;
4655 mcache_obj_t
*mp_list
= NULL
;
4656 mcache_obj_t
*mcl_list
= NULL
;
4657 mcache_obj_t
*mbc_list
= NULL
;
4658 mcache_obj_t
*m16k_list
= NULL
;
4659 mcache_obj_t
*m_mcl_list
= NULL
;
4660 mcache_obj_t
*m_mbc_list
= NULL
;
4661 mcache_obj_t
*m_m16k_list
= NULL
;
4662 mcache_obj_t
*ref_list
= NULL
;
4664 int mt_free
= 0, mt_data
= 0, mt_header
= 0, mt_soname
= 0, mt_tag
= 0;
4669 nextpkt
= m
->m_nextpkt
;
4670 m
->m_nextpkt
= NULL
;
4673 struct mbuf
*next
= m
->m_next
;
4674 mcache_obj_t
*o
, *rfa
;
4675 u_int32_t composite
;
4677 m_ext_free_func_t m_free_func
;
4679 if (m
->m_type
== MT_FREE
)
4680 panic("m_free: freeing an already freed mbuf");
4682 if (m
->m_flags
& M_PKTHDR
) {
4683 /* Check for scratch area overflow */
4684 m_redzone_verify(m
);
4685 /* Free the aux data and tags if there is any */
4686 m_tag_delete_chain(m
, NULL
);
4689 if (!(m
->m_flags
& M_EXT
)) {
4694 if (MBUF_IS_PAIRED(m
) && m_free_paired(m
)) {
4701 o
= (mcache_obj_t
*)(void *)m
->m_ext
.ext_buf
;
4702 refcnt
= m_decref(m
);
4703 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
4704 m_free_func
= m_get_ext_free(m
);
4705 if (refcnt
== MEXT_MINREF(m
) && !composite
) {
4706 if (m_free_func
== NULL
) {
4707 o
->obj_next
= mcl_list
;
4709 } else if (m_free_func
== m_bigfree
) {
4710 o
->obj_next
= mbc_list
;
4712 } else if (m_free_func
== m_16kfree
) {
4713 o
->obj_next
= m16k_list
;
4716 (*(m_free_func
))((caddr_t
)o
,
4720 rfa
= (mcache_obj_t
*)(void *)m_get_rfa(m
);
4721 rfa
->obj_next
= ref_list
;
4723 m_set_ext(m
, NULL
, NULL
, NULL
);
4724 } else if (refcnt
== MEXT_MINREF(m
) && composite
) {
4725 VERIFY(!(MEXT_FLAGS(m
) & EXTF_PAIRED
));
4726 VERIFY(m
->m_type
!= MT_FREE
);
4728 * Amortize the costs of atomic operations
4729 * by doing them at the end, if possible.
4731 if (m
->m_type
== MT_DATA
)
4733 else if (m
->m_type
== MT_HEADER
)
4735 else if (m
->m_type
== MT_SONAME
)
4737 else if (m
->m_type
== MT_TAG
)
4740 mtype_stat_dec(m
->m_type
);
4742 m
->m_type
= MT_FREE
;
4745 m
->m_next
= m
->m_nextpkt
= NULL
;
4747 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
4749 /* "Free" into the intermediate cache */
4750 o
= (mcache_obj_t
*)m
;
4751 if (m_free_func
== NULL
) {
4752 o
->obj_next
= m_mcl_list
;
4754 } else if (m_free_func
== m_bigfree
) {
4755 o
->obj_next
= m_mbc_list
;
4758 VERIFY(m_free_func
== m_16kfree
);
4759 o
->obj_next
= m_m16k_list
;
4767 * Amortize the costs of atomic operations
4768 * by doing them at the end, if possible.
4770 if (m
->m_type
== MT_DATA
)
4772 else if (m
->m_type
== MT_HEADER
)
4774 else if (m
->m_type
== MT_SONAME
)
4776 else if (m
->m_type
== MT_TAG
)
4778 else if (m
->m_type
!= MT_FREE
)
4779 mtype_stat_dec(m
->m_type
);
4781 m
->m_type
= MT_FREE
;
4782 m
->m_flags
= m
->m_len
= 0;
4783 m
->m_next
= m
->m_nextpkt
= NULL
;
4785 ((mcache_obj_t
*)m
)->obj_next
= mp_list
;
4786 mp_list
= (mcache_obj_t
*)m
;
4795 mtype_stat_add(MT_FREE
, mt_free
);
4797 mtype_stat_sub(MT_DATA
, mt_data
);
4799 mtype_stat_sub(MT_HEADER
, mt_header
);
4801 mtype_stat_sub(MT_SONAME
, mt_soname
);
4803 mtype_stat_sub(MT_TAG
, mt_tag
);
4805 if (mp_list
!= NULL
)
4806 mcache_free_ext(m_cache(MC_MBUF
), mp_list
);
4807 if (mcl_list
!= NULL
)
4808 mcache_free_ext(m_cache(MC_CL
), mcl_list
);
4809 if (mbc_list
!= NULL
)
4810 mcache_free_ext(m_cache(MC_BIGCL
), mbc_list
);
4811 if (m16k_list
!= NULL
)
4812 mcache_free_ext(m_cache(MC_16KCL
), m16k_list
);
4813 if (m_mcl_list
!= NULL
)
4814 mcache_free_ext(m_cache(MC_MBUF_CL
), m_mcl_list
);
4815 if (m_mbc_list
!= NULL
)
4816 mcache_free_ext(m_cache(MC_MBUF_BIGCL
), m_mbc_list
);
4817 if (m_m16k_list
!= NULL
)
4818 mcache_free_ext(m_cache(MC_MBUF_16KCL
), m_m16k_list
);
4819 if (ref_list
!= NULL
)
4820 mcache_free_ext(ref_cache
, ref_list
);
4826 m_freem(struct mbuf
*m
)
4833 * Mbuffer utility routines.
4836 * Set the m_data pointer of a newly allocated mbuf to place an object of the
4837 * specified size at the end of the mbuf, longword aligned.
4839 * NB: Historically, we had M_ALIGN(), MH_ALIGN(), and MEXT_ALIGN() as
4840 * separate macros, each asserting that it was called at the proper moment.
4841 * This required callers to themselves test the storage type and call the
4842 * right one. Rather than require callers to be aware of those layout
4843 * decisions, we centralize here.
4846 m_align(struct mbuf
*m
, int len
)
4850 /* At this point data must point to start */
4851 VERIFY(m
->m_data
== M_START(m
));
4853 VERIFY(len
<= M_SIZE(m
));
4854 adjust
= M_SIZE(m
) - len
;
4855 m
->m_data
+= adjust
&~ (sizeof(long) - 1);
4859 * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain,
4860 * copy junk along. Does not adjust packet header length.
4863 m_prepend(struct mbuf
*m
, int len
, int how
)
4867 _MGET(mn
, how
, m
->m_type
);
4872 if (m
->m_flags
& M_PKTHDR
) {
4873 M_COPY_PKTHDR(mn
, m
);
4874 m
->m_flags
&= ~M_PKTHDR
;
4878 if (m
->m_flags
& M_PKTHDR
) {
4879 VERIFY(len
<= MHLEN
);
4882 VERIFY(len
<= MLEN
);
4890 * Replacement for old M_PREPEND macro: allocate new mbuf to prepend to
4891 * chain, copy junk along, and adjust length.
4894 m_prepend_2(struct mbuf
*m
, int len
, int how
, int align
)
4896 if (M_LEADINGSPACE(m
) >= len
&&
4897 (!align
|| IS_P2ALIGNED((m
->m_data
- len
), sizeof(u_int32_t
)))) {
4901 m
= m_prepend(m
, len
, how
);
4903 if ((m
) && (m
->m_flags
& M_PKTHDR
))
4904 m
->m_pkthdr
.len
+= len
;
4909 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
4910 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
4911 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
4916 m_copym_mode(struct mbuf
*m
, int off0
, int len
, int wait
, uint32_t mode
)
4918 struct mbuf
*n
, *mhdr
= NULL
, **np
;
4923 if (off
< 0 || len
< 0)
4924 panic("m_copym: invalid offset %d or len %d", off
, len
);
4926 VERIFY((mode
!= M_COPYM_MUST_COPY_HDR
&&
4927 mode
!= M_COPYM_MUST_MOVE_HDR
) || (m
->m_flags
& M_PKTHDR
));
4929 if ((off
== 0 && (m
->m_flags
& M_PKTHDR
)) ||
4930 mode
== M_COPYM_MUST_COPY_HDR
|| mode
== M_COPYM_MUST_MOVE_HDR
) {
4935 while (off
>= m
->m_len
) {
4936 if (m
->m_next
== NULL
)
4937 panic("m_copym: invalid mbuf chain");
4946 if (len
!= M_COPYALL
)
4947 panic("m_copym: len != M_COPYALL");
4952 n
= _M_RETRYHDR(wait
, m
->m_type
);
4954 n
= _M_RETRY(wait
, m
->m_type
);
4961 if ((mode
== M_COPYM_MOVE_HDR
) ||
4962 (mode
== M_COPYM_MUST_MOVE_HDR
)) {
4963 M_COPY_PKTHDR(n
, mhdr
);
4964 } else if ((mode
== M_COPYM_COPY_HDR
) ||
4965 (mode
== M_COPYM_MUST_COPY_HDR
)) {
4966 if (m_dup_pkthdr(n
, mhdr
, wait
) == 0)
4969 if (len
== M_COPYALL
)
4970 n
->m_pkthdr
.len
-= off0
;
4972 n
->m_pkthdr
.len
= len
;
4975 * There is data to copy from the packet header mbuf
4976 * if it is empty or it is before the starting offset
4983 n
->m_len
= MIN(len
, (m
->m_len
- off
));
4984 if (m
->m_flags
& M_EXT
) {
4985 n
->m_ext
= m
->m_ext
;
4987 n
->m_data
= m
->m_data
+ off
;
4988 n
->m_flags
|= M_EXT
;
4991 * Limit to the capacity of the destination
4993 if (n
->m_flags
& M_PKTHDR
)
4994 n
->m_len
= MIN(n
->m_len
, MHLEN
);
4996 n
->m_len
= MIN(n
->m_len
, MLEN
);
4998 if (MTOD(n
, char *) + n
->m_len
> ((char *)n
) + MSIZE
)
4999 panic("%s n %p copy overflow",
5002 bcopy(MTOD(m
, caddr_t
)+off
, MTOD(n
, caddr_t
),
5003 (unsigned)n
->m_len
);
5005 if (len
!= M_COPYALL
)
5025 m_copym(struct mbuf
*m
, int off0
, int len
, int wait
)
5027 return (m_copym_mode(m
, off0
, len
, wait
, M_COPYM_MOVE_HDR
));
5031 * Equivalent to m_copym except that all necessary mbuf hdrs are allocated
5032 * within this routine also, the last mbuf and offset accessed are passed
5033 * out and can be passed back in to avoid having to rescan the entire mbuf
5034 * list (normally hung off of the socket)
5037 m_copym_with_hdrs(struct mbuf
*m0
, int off0
, int len0
, int wait
,
5038 struct mbuf
**m_lastm
, int *m_off
, uint32_t mode
)
5040 struct mbuf
*m
= m0
, *n
, **np
= NULL
;
5041 int off
= off0
, len
= len0
;
5042 struct mbuf
*top
= NULL
;
5043 int mcflags
= MSLEEPF(wait
);
5046 mcache_obj_t
*list
= NULL
;
5049 if (off
== 0 && (m
->m_flags
& M_PKTHDR
))
5052 if (m_lastm
!= NULL
&& *m_lastm
!= NULL
) {
5056 while (off
>= m
->m_len
) {
5066 len
-= MIN(len
, (n
->m_len
- ((needed
== 1) ? off
: 0)));
5073 * If the caller doesn't want to be put to sleep, mark it with
5074 * MCR_TRYHARD so that we may reclaim buffers from other places
5077 if (mcflags
& MCR_NOSLEEP
)
5078 mcflags
|= MCR_TRYHARD
;
5080 if (mcache_alloc_ext(m_cache(MC_MBUF
), &list
, needed
,
5086 n
= (struct mbuf
*)list
;
5087 list
= list
->obj_next
;
5088 ASSERT(n
!= NULL
&& m
!= NULL
);
5090 type
= (top
== NULL
) ? MT_HEADER
: m
->m_type
;
5091 MBUF_INIT(n
, (top
== NULL
), type
);
5093 if (top
== NULL
&& mac_mbuf_label_init(n
, wait
) != 0) {
5094 mtype_stat_inc(MT_HEADER
);
5095 mtype_stat_dec(MT_FREE
);
5099 #endif /* MAC_NET */
5111 if ((mode
== M_COPYM_MOVE_HDR
) ||
5112 (mode
== M_COPYM_MUST_MOVE_HDR
)) {
5113 M_COPY_PKTHDR(n
, m
);
5114 } else if ((mode
== M_COPYM_COPY_HDR
) ||
5115 (mode
== M_COPYM_MUST_COPY_HDR
)) {
5116 if (m_dup_pkthdr(n
, m
, wait
) == 0)
5119 n
->m_pkthdr
.len
= len
;
5122 n
->m_len
= MIN(len
, (m
->m_len
- off
));
5124 if (m
->m_flags
& M_EXT
) {
5125 n
->m_ext
= m
->m_ext
;
5127 n
->m_data
= m
->m_data
+ off
;
5128 n
->m_flags
|= M_EXT
;
5130 if (MTOD(n
, char *) + n
->m_len
> ((char *)n
) + MSIZE
)
5131 panic("%s n %p copy overflow",
5134 bcopy(MTOD(m
, caddr_t
)+off
, MTOD(n
, caddr_t
),
5135 (unsigned)n
->m_len
);
5140 if (m_lastm
!= NULL
&& m_off
!= NULL
) {
5141 if ((off
+ n
->m_len
) == m
->m_len
) {
5142 *m_lastm
= m
->m_next
;
5146 *m_off
= off
+ n
->m_len
;
5156 mtype_stat_inc(MT_HEADER
);
5157 mtype_stat_add(type
, needed
);
5158 mtype_stat_sub(MT_FREE
, needed
+ 1);
5160 ASSERT(list
== NULL
);
5165 mcache_free_ext(m_cache(MC_MBUF
), list
);
5173 * Copy data from an mbuf chain starting "off" bytes from the beginning,
5174 * continuing for "len" bytes, into the indicated buffer.
5177 m_copydata(struct mbuf
*m
, int off
, int len
, void *vp
)
5179 int off0
= off
, len0
= len
;
5180 struct mbuf
*m0
= m
;
5184 if (__improbable(off
< 0 || len
< 0)) {
5185 panic("%s: invalid offset %d or len %d", __func__
, off
, len
);
5190 if (__improbable(m
== NULL
)) {
5191 panic("%s: invalid mbuf chain %p [off %d, len %d]",
5192 __func__
, m0
, off0
, len0
);
5201 if (__improbable(m
== NULL
)) {
5202 panic("%s: invalid mbuf chain %p [off %d, len %d]",
5203 __func__
, m0
, off0
, len0
);
5206 count
= MIN(m
->m_len
- off
, len
);
5207 bcopy(MTOD(m
, caddr_t
) + off
, cp
, count
);
5216 * Concatenate mbuf chain n to m. Both chains must be of the same type
5217 * (e.g. MT_DATA). Any m_pkthdr is not updated.
5220 m_cat(struct mbuf
*m
, struct mbuf
*n
)
5225 if ((m
->m_flags
& M_EXT
) ||
5226 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
5227 /* just join the two chains */
5231 /* splat the data from one into the other */
5232 bcopy(MTOD(n
, caddr_t
), MTOD(m
, caddr_t
) + m
->m_len
,
5234 m
->m_len
+= n
->m_len
;
5240 m_adj(struct mbuf
*mp
, int req_len
)
5246 if ((m
= mp
) == NULL
)
5252 while (m
!= NULL
&& len
> 0) {
5253 if (m
->m_len
<= len
) {
5264 if (m
->m_flags
& M_PKTHDR
)
5265 m
->m_pkthdr
.len
-= (req_len
- len
);
5268 * Trim from tail. Scan the mbuf chain,
5269 * calculating its length and finding the last mbuf.
5270 * If the adjustment only affects this mbuf, then just
5271 * adjust and return. Otherwise, rescan and truncate
5272 * after the remaining size.
5278 if (m
->m_next
== (struct mbuf
*)0)
5282 if (m
->m_len
>= len
) {
5285 if (m
->m_flags
& M_PKTHDR
)
5286 m
->m_pkthdr
.len
-= len
;
5293 * Correct length for chain is "count".
5294 * Find the mbuf with last data, adjust its length,
5295 * and toss data from remaining mbufs on chain.
5298 if (m
->m_flags
& M_PKTHDR
)
5299 m
->m_pkthdr
.len
= count
;
5300 for (; m
; m
= m
->m_next
) {
5301 if (m
->m_len
>= count
) {
5307 while ((m
= m
->m_next
))
5313 * Rearange an mbuf chain so that len bytes are contiguous
5314 * and in the data area of an mbuf (so that mtod and dtom
5315 * will work for a structure of size len). Returns the resulting
5316 * mbuf chain on success, frees it and returns null on failure.
5317 * If there is room, it will add up to max_protohdr-len extra bytes to the
5318 * contiguous region in an attempt to avoid being called next time.
5323 m_pullup(struct mbuf
*n
, int len
)
5329 /* check invalid arguments */
5331 panic("%s: n == NULL", __func__
);
5334 os_log_info(OS_LOG_DEFAULT
, "%s: failed negative len %d",
5339 os_log_info(OS_LOG_DEFAULT
, "%s: failed len %d too big",
5343 if ((n
->m_flags
& M_EXT
) == 0 &&
5344 n
->m_data
>= &n
->m_dat
[MLEN
]) {
5345 os_log_info(OS_LOG_DEFAULT
, "%s: m_data out of bounds",
5351 * If first mbuf has no cluster, and has room for len bytes
5352 * without shifting current data, pullup into it,
5353 * otherwise allocate a new mbuf to prepend to the chain.
5355 if ((n
->m_flags
& M_EXT
) == 0 &&
5356 len
< &n
->m_dat
[MLEN
] - n
->m_data
&& n
->m_next
!= NULL
) {
5357 if (n
->m_len
>= len
)
5365 _MGET(m
, M_DONTWAIT
, n
->m_type
);
5369 if (n
->m_flags
& M_PKTHDR
) {
5370 M_COPY_PKTHDR(m
, n
);
5371 n
->m_flags
&= ~M_PKTHDR
;
5374 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
5376 count
= MIN(MIN(MAX(len
, max_protohdr
), space
), n
->m_len
);
5377 bcopy(MTOD(n
, caddr_t
), MTOD(m
, caddr_t
) + m
->m_len
,
5387 } while (len
> 0 && n
!= NULL
);
5401 * Like m_pullup(), except a new mbuf is always allocated, and we allow
5402 * the amount of empty space before the data in the new mbuf to be specified
5403 * (in the event that the caller expects to prepend later).
5405 __private_extern__
int MSFail
= 0;
5407 __private_extern__
struct mbuf
*
5408 m_copyup(struct mbuf
*n
, int len
, int dstoff
)
5413 if (len
> (MHLEN
- dstoff
))
5415 MGET(m
, M_DONTWAIT
, n
->m_type
);
5419 if (n
->m_flags
& M_PKTHDR
) {
5420 m_copy_pkthdr(m
, n
);
5421 n
->m_flags
&= ~M_PKTHDR
;
5423 m
->m_data
+= dstoff
;
5424 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
5426 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
5427 memcpy(mtod(m
, caddr_t
) + m
->m_len
, mtod(n
, caddr_t
),
5437 } while (len
> 0 && n
);
5451 * Partition an mbuf chain in two pieces, returning the tail --
5452 * all but the first len0 bytes. In case of failure, it returns NULL and
5453 * attempts to restore the chain to its original state.
5456 m_split(struct mbuf
*m0
, int len0
, int wait
)
5458 return (m_split0(m0
, len0
, wait
, 1));
5461 static struct mbuf
*
5462 m_split0(struct mbuf
*m0
, int len0
, int wait
, int copyhdr
)
5465 unsigned len
= len0
, remain
;
5468 * First iterate to the mbuf which contains the first byte of
5469 * data at offset len0
5471 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
5476 * len effectively is now the offset in the current
5477 * mbuf where we have to perform split.
5479 * remain becomes the tail length.
5480 * Note that len can also be == m->m_len
5482 remain
= m
->m_len
- len
;
5485 * If current mbuf len contains the entire remaining offset len,
5486 * just make the second mbuf chain pointing to next mbuf onwards
5487 * and return after making necessary adjustments
5489 if (copyhdr
&& (m0
->m_flags
& M_PKTHDR
) && remain
== 0) {
5490 _MGETHDR(n
, wait
, m0
->m_type
);
5493 n
->m_next
= m
->m_next
;
5495 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
5496 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
5497 m0
->m_pkthdr
.len
= len0
;
5499 } if (copyhdr
&& (m0
->m_flags
& M_PKTHDR
)) {
5500 _MGETHDR(n
, wait
, m0
->m_type
);
5503 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
5504 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
5505 m0
->m_pkthdr
.len
= len0
;
5508 * If current points to external storage
5509 * then it can be shared by making last mbuf
5510 * of head chain and first mbuf of current chain
5511 * pointing to different data offsets
5513 if (m
->m_flags
& M_EXT
)
5515 if (remain
> MHLEN
) {
5516 /* m can't be the lead packet */
5518 n
->m_next
= m_split(m
, len
, wait
);
5519 if (n
->m_next
== NULL
) {
5525 MH_ALIGN(n
, remain
);
5526 } else if (remain
== 0) {
5531 _MGET(n
, wait
, m
->m_type
);
5535 if ((m
->m_flags
& M_EXT
) == 0) {
5536 VERIFY(remain
<= MLEN
);
5541 if (m
->m_flags
& M_EXT
) {
5542 n
->m_flags
|= M_EXT
;
5543 n
->m_ext
= m
->m_ext
;
5545 n
->m_data
= m
->m_data
+ len
;
5547 bcopy(MTOD(m
, caddr_t
) + len
, MTOD(n
, caddr_t
), remain
);
5551 n
->m_next
= m
->m_next
;
5557 * Routine to copy from device local memory into mbufs.
5560 m_devget(char *buf
, int totlen
, int off0
, struct ifnet
*ifp
,
5561 void (*copy
)(const void *, void *, size_t))
5564 struct mbuf
*top
= NULL
, **mp
= &top
;
5565 int off
= off0
, len
;
5573 * If 'off' is non-zero, packet is trailer-encapsulated,
5574 * so we have to skip the type and length fields.
5576 cp
+= off
+ 2 * sizeof (u_int16_t
);
5577 totlen
-= 2 * sizeof (u_int16_t
);
5579 _MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
5582 m
->m_pkthdr
.rcvif
= ifp
;
5583 m
->m_pkthdr
.len
= totlen
;
5586 while (totlen
> 0) {
5588 _MGET(m
, M_DONTWAIT
, MT_DATA
);
5595 len
= MIN(totlen
, epkt
- cp
);
5596 if (len
>= MINCLSIZE
) {
5597 MCLGET(m
, M_DONTWAIT
);
5598 if (m
->m_flags
& M_EXT
) {
5599 m
->m_len
= len
= MIN(len
, m_maxsize(MC_CL
));
5601 /* give up when it's out of cluster mbufs */
5609 * Place initial small packet/header at end of mbuf.
5611 if (len
< m
->m_len
) {
5613 len
+ max_linkhdr
<= m
->m_len
)
5614 m
->m_data
+= max_linkhdr
;
5621 copy(cp
, MTOD(m
, caddr_t
), (unsigned)len
);
5623 bcopy(cp
, MTOD(m
, caddr_t
), (unsigned)len
);
5634 #ifndef MBUF_GROWTH_NORMAL_THRESH
5635 #define MBUF_GROWTH_NORMAL_THRESH 25
5639 * Cluster freelist allocation check.
5642 m_howmany(int num
, size_t bufsize
)
5645 u_int32_t m_mbclusters
, m_clusters
, m_bigclusters
, m_16kclusters
;
5646 u_int32_t m_mbfree
, m_clfree
, m_bigclfree
, m_16kclfree
;
5647 u_int32_t sumclusters
, freeclusters
;
5648 u_int32_t percent_pool
, percent_kmem
;
5649 u_int32_t mb_growth
, mb_growth_thresh
;
5651 VERIFY(bufsize
== m_maxsize(MC_BIGCL
) ||
5652 bufsize
== m_maxsize(MC_16KCL
));
5654 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
5656 /* Numbers in 2K cluster units */
5657 m_mbclusters
= m_total(MC_MBUF
) >> NMBPCLSHIFT
;
5658 m_clusters
= m_total(MC_CL
);
5659 m_bigclusters
= m_total(MC_BIGCL
) << NCLPBGSHIFT
;
5660 m_16kclusters
= m_total(MC_16KCL
);
5661 sumclusters
= m_mbclusters
+ m_clusters
+ m_bigclusters
;
5663 m_mbfree
= m_infree(MC_MBUF
) >> NMBPCLSHIFT
;
5664 m_clfree
= m_infree(MC_CL
);
5665 m_bigclfree
= m_infree(MC_BIGCL
) << NCLPBGSHIFT
;
5666 m_16kclfree
= m_infree(MC_16KCL
);
5667 freeclusters
= m_mbfree
+ m_clfree
+ m_bigclfree
;
5669 /* Bail if we've maxed out the mbuf memory map */
5670 if ((bufsize
== m_maxsize(MC_BIGCL
) && sumclusters
>= nclusters
) ||
5671 (njcl
> 0 && bufsize
== m_maxsize(MC_16KCL
) &&
5672 (m_16kclusters
<< NCLPJCLSHIFT
) >= njcl
)) {
5673 mbwdog_logger("maxed out nclusters (%u >= %u) or njcl (%u >= %u)",
5674 sumclusters
, nclusters
,
5675 (m_16kclusters
<< NCLPJCLSHIFT
), njcl
);
5679 if (bufsize
== m_maxsize(MC_BIGCL
)) {
5681 if (m_bigclusters
< m_minlimit(MC_BIGCL
))
5682 return (m_minlimit(MC_BIGCL
) - m_bigclusters
);
5685 ((sumclusters
- freeclusters
) * 100) / sumclusters
;
5686 percent_kmem
= (sumclusters
* 100) / nclusters
;
5689 * If a light/normal user, grow conservatively (75%)
5690 * If a heavy user, grow aggressively (50%)
5692 if (percent_kmem
< MBUF_GROWTH_NORMAL_THRESH
)
5693 mb_growth
= MB_GROWTH_NORMAL
;
5695 mb_growth
= MB_GROWTH_AGGRESSIVE
;
5697 if (percent_kmem
< 5) {
5698 /* For initial allocations */
5701 /* Return if >= MBIGCL_LOWAT clusters available */
5702 if (m_infree(MC_BIGCL
) >= MBIGCL_LOWAT
&&
5703 m_total(MC_BIGCL
) >=
5704 MBIGCL_LOWAT
+ m_minlimit(MC_BIGCL
))
5707 /* Ensure at least num clusters are accessible */
5708 if (num
>= m_infree(MC_BIGCL
))
5709 i
= num
- m_infree(MC_BIGCL
);
5710 if (num
> m_total(MC_BIGCL
) - m_minlimit(MC_BIGCL
))
5711 j
= num
- (m_total(MC_BIGCL
) -
5712 m_minlimit(MC_BIGCL
));
5717 * Grow pool if percent_pool > 75 (normal growth)
5718 * or percent_pool > 50 (aggressive growth).
5720 mb_growth_thresh
= 100 - (100 / (1 << mb_growth
));
5721 if (percent_pool
> mb_growth_thresh
)
5722 j
= ((sumclusters
+ num
) >> mb_growth
) -
5727 /* Check to ensure we didn't go over limits */
5728 if (i
+ m_bigclusters
>= m_maxlimit(MC_BIGCL
))
5729 i
= m_maxlimit(MC_BIGCL
) - m_bigclusters
;
5730 if ((i
<< 1) + sumclusters
>= nclusters
)
5731 i
= (nclusters
- sumclusters
) >> 1;
5732 VERIFY((m_total(MC_BIGCL
) + i
) <= m_maxlimit(MC_BIGCL
));
5733 VERIFY(sumclusters
+ (i
<< 1) <= nclusters
);
5735 } else { /* 16K CL */
5737 /* Ensure at least num clusters are available */
5738 if (num
>= m_16kclfree
)
5739 i
= num
- m_16kclfree
;
5741 /* Always grow 16KCL pool aggressively */
5742 if (((m_16kclusters
+ num
) >> 1) > m_16kclfree
)
5743 j
= ((m_16kclusters
+ num
) >> 1) - m_16kclfree
;
5746 /* Check to ensure we don't go over limit */
5747 if ((i
+ m_total(MC_16KCL
)) >= m_maxlimit(MC_16KCL
))
5748 i
= m_maxlimit(MC_16KCL
) - m_total(MC_16KCL
);
5753 * Return the number of bytes in the mbuf chain, m.
5756 m_length(struct mbuf
*m
)
5759 unsigned int pktlen
;
5761 if (m
->m_flags
& M_PKTHDR
)
5762 return (m
->m_pkthdr
.len
);
5765 for (m0
= m
; m0
!= NULL
; m0
= m0
->m_next
)
5766 pktlen
+= m0
->m_len
;
5771 * Copy data from a buffer back into the indicated mbuf chain,
5772 * starting "off" bytes from the beginning, extending the mbuf
5773 * chain if necessary.
5776 m_copyback(struct mbuf
*m0
, int off
, int len
, const void *cp
)
5779 struct mbuf
*origm
= m0
;
5789 m_copyback0(&m0
, off
, len
, cp
,
5790 M_COPYBACK0_COPYBACK
| M_COPYBACK0_EXTEND
, M_DONTWAIT
);
5793 if (error
!= 0 || (m0
!= NULL
&& origm
!= m0
))
5794 panic("m_copyback");
5799 m_copyback_cow(struct mbuf
*m0
, int off
, int len
, const void *cp
, int how
)
5803 /* don't support chain expansion */
5804 VERIFY(off
+ len
<= m_length(m0
));
5806 error
= m_copyback0(&m0
, off
, len
, cp
,
5807 M_COPYBACK0_COPYBACK
| M_COPYBACK0_COW
, how
);
5810 * no way to recover from partial success.
5811 * just free the chain.
5820 * m_makewritable: ensure the specified range writable.
5823 m_makewritable(struct mbuf
**mp
, int off
, int len
, int how
)
5828 int origlen
, reslen
;
5830 origlen
= m_length(*mp
);
5833 #if 0 /* M_COPYALL is large enough */
5834 if (len
== M_COPYALL
)
5835 len
= m_length(*mp
) - off
; /* XXX */
5838 error
= m_copyback0(mp
, off
, len
, NULL
,
5839 M_COPYBACK0_PRESERVE
| M_COPYBACK0_COW
, how
);
5843 for (n
= *mp
; n
; n
= n
->m_next
)
5845 if (origlen
!= reslen
)
5846 panic("m_makewritable: length changed");
5847 if (((*mp
)->m_flags
& M_PKTHDR
) && reslen
!= (*mp
)->m_pkthdr
.len
)
5848 panic("m_makewritable: inconsist");
5855 m_copyback0(struct mbuf
**mp0
, int off
, int len
, const void *vp
, int flags
,
5862 const char *cp
= vp
;
5864 VERIFY(mp0
!= NULL
);
5865 VERIFY(*mp0
!= NULL
);
5866 VERIFY((flags
& M_COPYBACK0_PRESERVE
) == 0 || cp
== NULL
);
5867 VERIFY((flags
& M_COPYBACK0_COPYBACK
) == 0 || cp
!= NULL
);
5870 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
5871 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
5874 VERIFY((~flags
& (M_COPYBACK0_EXTEND
|M_COPYBACK0_COW
)) != 0);
5878 while (off
> (mlen
= m
->m_len
)) {
5881 if (m
->m_next
== NULL
) {
5884 if (!(flags
& M_COPYBACK0_EXTEND
))
5888 * try to make some space at the end of "m".
5892 if (off
+ len
>= MINCLSIZE
&&
5893 !(m
->m_flags
& M_EXT
) && m
->m_len
== 0) {
5896 tspace
= M_TRAILINGSPACE(m
);
5898 tspace
= MIN(tspace
, off
+ len
);
5900 bzero(mtod(m
, char *) + m
->m_len
,
5909 * need to allocate an mbuf.
5912 if (off
+ len
>= MINCLSIZE
) {
5913 n
= m_getcl(how
, m
->m_type
, 0);
5915 n
= _M_GET(how
, m
->m_type
);
5921 n
->m_len
= MIN(M_TRAILINGSPACE(n
), off
+ len
);
5922 bzero(mtod(n
, char *), MIN(n
->m_len
, off
));
5929 mlen
= m
->m_len
- off
;
5930 if (mlen
!= 0 && m_mclhasreference(m
)) {
5935 * this mbuf is read-only.
5936 * allocate a new writable mbuf and try again.
5940 if (!(flags
& M_COPYBACK0_COW
))
5941 panic("m_copyback0: read-only");
5942 #endif /* DIAGNOSTIC */
5945 * if we're going to write into the middle of
5946 * a mbuf, split it first.
5948 if (off
> 0 && len
< mlen
) {
5949 n
= m_split0(m
, off
, how
, 0);
5960 * XXX TODO coalesce into the trailingspace of
5961 * the previous mbuf when possible.
5965 * allocate a new mbuf. copy packet header if needed.
5967 n
= _M_GET(how
, m
->m_type
);
5970 if (off
== 0 && (m
->m_flags
& M_PKTHDR
)) {
5971 M_COPY_PKTHDR(n
, m
);
5974 if (len
>= MINCLSIZE
)
5975 MCLGET(n
, M_DONTWAIT
);
5977 (n
->m_flags
& M_EXT
) ? MCLBYTES
: MLEN
;
5983 * free the region which has been overwritten.
5984 * copying data from old mbufs if requested.
5986 if (flags
& M_COPYBACK0_PRESERVE
)
5987 datap
= mtod(n
, char *);
5991 VERIFY(off
== 0 || eatlen
>= mlen
);
5993 VERIFY(len
>= mlen
);
5997 m_copydata(m
, off
, mlen
, datap
);
6004 while (m
!= NULL
&& m_mclhasreference(m
) &&
6005 n
->m_type
== m
->m_type
&& eatlen
> 0) {
6006 mlen
= MIN(eatlen
, m
->m_len
);
6008 m_copydata(m
, 0, mlen
, datap
);
6015 *mp
= m
= m_free(m
);
6023 mlen
= MIN(mlen
, len
);
6024 if (flags
& M_COPYBACK0_COPYBACK
) {
6025 bcopy(cp
, mtod(m
, caddr_t
) + off
, (unsigned)mlen
);
6034 if (m
->m_next
== NULL
) {
6041 if (((m
= *mp0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
)) {
6042 VERIFY(flags
& M_COPYBACK0_EXTEND
);
6043 m
->m_pkthdr
.len
= totlen
;
6053 mcl_to_paddr(char *addr
)
6055 vm_offset_t base_phys
;
6057 if (!MBUF_IN_MAP(addr
))
6059 base_phys
= mcl_paddr
[atop_64(addr
- (char *)mbutl
)];
6063 return ((uint64_t)(ptoa_64(base_phys
) | ((uint64_t)addr
& PAGE_MASK
)));
6067 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
6068 * And really copy the thing. That way, we don't "precompute" checksums
6069 * for unsuspecting consumers. Assumption: m->m_nextpkt == 0. Trick: for
6070 * small packets, don't dup into a cluster. That way received packets
6071 * don't take up too much room in the sockbuf (cf. sbspace()).
6076 m_dup(struct mbuf
*m
, int how
)
6078 struct mbuf
*n
, **np
;
6084 if (m
->m_flags
& M_PKTHDR
)
6088 * Quick check: if we have one mbuf and its data fits in an
6089 * mbuf with packet header, just copy and go.
6091 if (m
->m_next
== NULL
) {
6092 /* Then just move the data into an mbuf and be done... */
6094 if (m
->m_pkthdr
.len
<= MHLEN
&& m
->m_len
<= MHLEN
) {
6095 if ((n
= _M_GETHDR(how
, m
->m_type
)) == NULL
)
6097 n
->m_len
= m
->m_len
;
6098 m_dup_pkthdr(n
, m
, how
);
6099 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
6102 } else if (m
->m_len
<= MLEN
) {
6103 if ((n
= _M_GET(how
, m
->m_type
)) == NULL
)
6105 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
6106 n
->m_len
= m
->m_len
;
6112 printf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
6116 n
= _M_GETHDR(how
, m
->m_type
);
6118 n
= _M_GET(how
, m
->m_type
);
6121 if (m
->m_flags
& M_EXT
) {
6122 if (m
->m_len
<= m_maxsize(MC_CL
))
6124 else if (m
->m_len
<= m_maxsize(MC_BIGCL
))
6125 n
= m_mbigget(n
, how
);
6126 else if (m
->m_len
<= m_maxsize(MC_16KCL
) && njcl
> 0)
6127 n
= m_m16kget(n
, how
);
6128 if (!(n
->m_flags
& M_EXT
)) {
6135 /* Don't use M_COPY_PKTHDR: preserve m_data */
6136 m_dup_pkthdr(n
, m
, how
);
6138 if (!(n
->m_flags
& M_EXT
))
6139 n
->m_data
= n
->m_pktdat
;
6141 n
->m_len
= m
->m_len
;
6143 * Get the dup on the same bdry as the original
6144 * Assume that the two mbufs have the same offset to data area
6145 * (up to word boundaries)
6147 bcopy(MTOD(m
, caddr_t
), MTOD(n
, caddr_t
), (unsigned)n
->m_len
);
6151 printf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
6166 #define MBUF_MULTIPAGES(m) \
6167 (((m)->m_flags & M_EXT) && \
6168 ((IS_P2ALIGNED((m)->m_data, PAGE_SIZE) \
6169 && (m)->m_len > PAGE_SIZE) || \
6170 (!IS_P2ALIGNED((m)->m_data, PAGE_SIZE) && \
6171 P2ROUNDUP((m)->m_data, PAGE_SIZE) < ((uintptr_t)(m)->m_data + (m)->m_len))))
6173 static struct mbuf
*
6174 m_expand(struct mbuf
*m
, struct mbuf
**last
)
6176 struct mbuf
*top
= NULL
;
6177 struct mbuf
**nm
= &top
;
6178 uintptr_t data0
, data
;
6179 unsigned int len0
, len
;
6181 VERIFY(MBUF_MULTIPAGES(m
));
6182 VERIFY(m
->m_next
== NULL
);
6183 data0
= (uintptr_t)m
->m_data
;
6191 if (IS_P2ALIGNED(data
, PAGE_SIZE
) && len0
> PAGE_SIZE
)
6193 else if (!IS_P2ALIGNED(data
, PAGE_SIZE
) &&
6194 P2ROUNDUP(data
, PAGE_SIZE
) < (data
+ len0
))
6195 len
= P2ROUNDUP(data
, PAGE_SIZE
) - data
;
6200 VERIFY(m
->m_flags
& M_EXT
);
6201 m
->m_data
= (void *)data
;
6213 n
= _M_RETRY(M_DONTWAIT
, MT_DATA
);
6220 n
->m_ext
= m
->m_ext
;
6222 n
->m_flags
|= M_EXT
;
6229 m_normalize(struct mbuf
*m
)
6231 struct mbuf
*top
= NULL
;
6232 struct mbuf
**nm
= &top
;
6233 boolean_t expanded
= FALSE
;
6241 /* Does the data cross one or more page boundaries? */
6242 if (MBUF_MULTIPAGES(m
)) {
6244 if ((m
= m_expand(m
, &last
)) == NULL
) {
6260 atomic_add_32(&mb_normalized
, 1);
6265 * Append the specified data to the indicated mbuf chain,
6266 * Extend the mbuf chain if the new data does not fit in
6269 * Return 1 if able to complete the job; otherwise 0.
6272 m_append(struct mbuf
*m0
, int len
, caddr_t cp
)
6275 int remainder
, space
;
6277 for (m
= m0
; m
->m_next
!= NULL
; m
= m
->m_next
)
6280 space
= M_TRAILINGSPACE(m
);
6283 * Copy into available space.
6285 if (space
> remainder
)
6287 bcopy(cp
, mtod(m
, caddr_t
) + m
->m_len
, space
);
6292 while (remainder
> 0) {
6294 * Allocate a new mbuf; could check space
6295 * and allocate a cluster instead.
6297 n
= m_get(M_WAITOK
, m
->m_type
);
6300 n
->m_len
= min(MLEN
, remainder
);
6301 bcopy(cp
, mtod(n
, caddr_t
), n
->m_len
);
6303 remainder
-= n
->m_len
;
6307 if (m0
->m_flags
& M_PKTHDR
)
6308 m0
->m_pkthdr
.len
+= len
- remainder
;
6309 return (remainder
== 0);
6313 m_last(struct mbuf
*m
)
6315 while (m
->m_next
!= NULL
)
6321 m_fixhdr(struct mbuf
*m0
)
6325 VERIFY(m0
->m_flags
& M_PKTHDR
);
6327 len
= m_length2(m0
, NULL
);
6328 m0
->m_pkthdr
.len
= len
;
6333 m_length2(struct mbuf
*m0
, struct mbuf
**last
)
6339 for (m
= m0
; m
!= NULL
; m
= m
->m_next
) {
6341 if (m
->m_next
== NULL
)
6350 * Defragment a mbuf chain, returning the shortest possible chain of mbufs
6351 * and clusters. If allocation fails and this cannot be completed, NULL will
6352 * be returned, but the passed in chain will be unchanged. Upon success,
6353 * the original chain will be freed, and the new chain will be returned.
6355 * If a non-packet header is passed in, the original mbuf (chain?) will
6356 * be returned unharmed.
6358 * If offset is specfied, the first mbuf in the chain will have a leading
6359 * space of the amount stated by the "off" parameter.
6361 * This routine requires that the m_pkthdr.header field of the original
6362 * mbuf chain is cleared by the caller.
6365 m_defrag_offset(struct mbuf
*m0
, u_int32_t off
, int how
)
6367 struct mbuf
*m_new
= NULL
, *m_final
= NULL
;
6368 int progress
= 0, length
, pktlen
;
6370 if (!(m0
->m_flags
& M_PKTHDR
))
6373 VERIFY(off
< MHLEN
);
6374 m_fixhdr(m0
); /* Needed sanity check */
6376 pktlen
= m0
->m_pkthdr
.len
+ off
;
6378 m_final
= m_getcl(how
, MT_DATA
, M_PKTHDR
);
6380 m_final
= m_gethdr(how
, MT_DATA
);
6382 if (m_final
== NULL
)
6387 m_final
->m_data
+= off
;
6391 * Caller must have handled the contents pointed to by this
6392 * pointer before coming here, as otherwise it will point to
6393 * the original mbuf which will get freed upon success.
6395 VERIFY(m0
->m_pkthdr
.pkt_hdr
== NULL
);
6397 if (m_dup_pkthdr(m_final
, m0
, how
) == 0)
6402 while (progress
< pktlen
) {
6403 length
= pktlen
- progress
;
6404 if (length
> MCLBYTES
)
6406 length
-= ((m_new
== m_final
) ? off
: 0);
6410 if (m_new
== NULL
) {
6412 m_new
= m_getcl(how
, MT_DATA
, 0);
6414 m_new
= m_get(how
, MT_DATA
);
6419 m_copydata(m0
, progress
, length
, mtod(m_new
, caddr_t
));
6421 m_new
->m_len
= length
;
6422 if (m_new
!= m_final
)
6423 m_cat(m_final
, m_new
);
6436 m_defrag(struct mbuf
*m0
, int how
)
6438 return (m_defrag_offset(m0
, 0, how
));
6442 m_mchtype(struct mbuf
*m
, int t
)
6445 mtype_stat_dec(m
->m_type
);
6450 m_mtod(struct mbuf
*m
)
6452 return (MTOD(m
, void *));
6458 return ((struct mbuf
*)((uintptr_t)(x
) & ~(MSIZE
-1)));
6462 m_mcheck(struct mbuf
*m
)
6468 * Return a pointer to mbuf/offset of location in mbuf chain.
6471 m_getptr(struct mbuf
*m
, int loc
, int *off
)
6475 /* Normal end of search. */
6476 if (m
->m_len
> loc
) {
6481 if (m
->m_next
== NULL
) {
6483 /* Point at the end of valid data. */
6496 * Inform the corresponding mcache(s) that there's a waiter below.
6499 mbuf_waiter_inc(mbuf_class_t
class, boolean_t comp
)
6501 mcache_waiter_inc(m_cache(class));
6503 if (class == MC_CL
) {
6504 mcache_waiter_inc(m_cache(MC_MBUF_CL
));
6505 } else if (class == MC_BIGCL
) {
6506 mcache_waiter_inc(m_cache(MC_MBUF_BIGCL
));
6507 } else if (class == MC_16KCL
) {
6508 mcache_waiter_inc(m_cache(MC_MBUF_16KCL
));
6510 mcache_waiter_inc(m_cache(MC_MBUF_CL
));
6511 mcache_waiter_inc(m_cache(MC_MBUF_BIGCL
));
6517 * Inform the corresponding mcache(s) that there's no more waiter below.
6520 mbuf_waiter_dec(mbuf_class_t
class, boolean_t comp
)
6522 mcache_waiter_dec(m_cache(class));
6524 if (class == MC_CL
) {
6525 mcache_waiter_dec(m_cache(MC_MBUF_CL
));
6526 } else if (class == MC_BIGCL
) {
6527 mcache_waiter_dec(m_cache(MC_MBUF_BIGCL
));
6528 } else if (class == MC_16KCL
) {
6529 mcache_waiter_dec(m_cache(MC_MBUF_16KCL
));
6531 mcache_waiter_dec(m_cache(MC_MBUF_CL
));
6532 mcache_waiter_dec(m_cache(MC_MBUF_BIGCL
));
6538 * Called during slab (blocking and non-blocking) allocation. If there
6539 * is at least one waiter, and the time since the first waiter is blocked
6540 * is greater than the watchdog timeout, panic the system.
6548 if (mb_waiters
== 0 || !mb_watchdog
)
6552 since
= now
.tv_sec
- mb_wdtstart
.tv_sec
;
6553 if (since
>= MB_WDT_MAXTIME
) {
6554 panic_plain("%s: %d waiters stuck for %u secs\n%s", __func__
,
6555 mb_waiters
, since
, mbuf_dump());
6561 * Called during blocking allocation. Returns TRUE if one or more objects
6562 * are available at the per-CPU caches layer and that allocation should be
6563 * retried at that level.
6566 mbuf_sleep(mbuf_class_t
class, unsigned int num
, int wait
)
6568 boolean_t mcache_retry
= FALSE
;
6570 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
6572 /* Check if there's anything at the cache layer */
6573 if (mbuf_cached_above(class, wait
)) {
6574 mcache_retry
= TRUE
;
6578 /* Nothing? Then try hard to get it from somewhere */
6579 m_reclaim(class, num
, (wait
& MCR_COMP
));
6581 /* We tried hard and got something? */
6582 if (m_infree(class) > 0) {
6585 } else if (mbuf_cached_above(class, wait
)) {
6587 mcache_retry
= TRUE
;
6589 } else if (wait
& MCR_TRYHARD
) {
6590 mcache_retry
= TRUE
;
6595 * There's really nothing for us right now; inform the
6596 * cache(s) that there is a waiter below and go to sleep.
6598 mbuf_waiter_inc(class, (wait
& MCR_COMP
));
6600 VERIFY(!(wait
& MCR_NOSLEEP
));
6603 * If this is the first waiter, arm the watchdog timer. Otherwise
6604 * check if we need to panic the system due to watchdog timeout.
6606 if (mb_waiters
== 0)
6607 microuptime(&mb_wdtstart
);
6612 m_region_expand(class) += m_total(class) + num
;
6613 /* wake up the worker thread */
6614 if (mbuf_worker_ready
&&
6615 mbuf_worker_needs_wakeup
) {
6616 wakeup((caddr_t
)&mbuf_worker_needs_wakeup
);
6617 mbuf_worker_needs_wakeup
= FALSE
;
6619 mbwdog_logger("waiting (%d mbufs in class %s)", num
, m_cname(class));
6620 (void) msleep(mb_waitchan
, mbuf_mlock
, (PZERO
-1), m_cname(class), NULL
);
6621 mbwdog_logger("woke up (%d mbufs in class %s) ", num
, m_cname(class));
6623 /* We are now up; stop getting notified until next round */
6624 mbuf_waiter_dec(class, (wait
& MCR_COMP
));
6626 /* We waited and got something */
6627 if (m_infree(class) > 0) {
6630 } else if (mbuf_cached_above(class, wait
)) {
6632 mcache_retry
= TRUE
;
6635 return (mcache_retry
);
6638 __attribute__((noreturn
))
6640 mbuf_worker_thread(void)
6645 lck_mtx_lock(mbuf_mlock
);
6646 mbwdog_logger("worker thread running");
6647 mbuf_worker_run_cnt
++;
6650 * Allocations are based on page size, so if we have depleted
6651 * the reserved spaces, try to free mbufs from the major classes.
6653 #if PAGE_SIZE == 4096
6654 uint32_t m_mbclusters
= m_total(MC_MBUF
) >> NMBPCLSHIFT
;
6655 uint32_t m_clusters
= m_total(MC_CL
);
6656 uint32_t m_bigclusters
= m_total(MC_BIGCL
) << NCLPBGSHIFT
;
6657 uint32_t sumclusters
= m_mbclusters
+ m_clusters
+ m_bigclusters
;
6658 if (sumclusters
>= nclusters
) {
6659 mbwdog_logger("reclaiming bigcl");
6660 mbuf_drain_locked(TRUE
);
6661 m_reclaim(MC_BIGCL
, 4, FALSE
);
6664 uint32_t m_16kclusters
= m_total(MC_16KCL
);
6665 if (njcl
> 0 && (m_16kclusters
<< NCLPJCLSHIFT
) >= njcl
) {
6666 mbwdog_logger("reclaiming 16kcl");
6667 mbuf_drain_locked(TRUE
);
6668 m_reclaim(MC_16KCL
, 4, FALSE
);
6671 if (m_region_expand(MC_CL
) > 0) {
6674 /* Adjust to current number of cluster in use */
6675 n
= m_region_expand(MC_CL
) -
6676 (m_total(MC_CL
) - m_infree(MC_CL
));
6677 if ((n
+ m_total(MC_CL
)) > m_maxlimit(MC_CL
))
6678 n
= m_maxlimit(MC_CL
) - m_total(MC_CL
);
6680 mb_expand_cl_total
+= n
;
6682 m_region_expand(MC_CL
) = 0;
6685 mbwdog_logger("expanding MC_CL by %d", n
);
6686 freelist_populate(MC_CL
, n
, M_WAIT
);
6689 if (m_region_expand(MC_BIGCL
) > 0) {
6691 mb_expand_bigcl_cnt
++;
6692 /* Adjust to current number of 4 KB cluster in use */
6693 n
= m_region_expand(MC_BIGCL
) -
6694 (m_total(MC_BIGCL
) - m_infree(MC_BIGCL
));
6695 if ((n
+ m_total(MC_BIGCL
)) > m_maxlimit(MC_BIGCL
))
6696 n
= m_maxlimit(MC_BIGCL
) - m_total(MC_BIGCL
);
6698 mb_expand_bigcl_total
+= n
;
6700 m_region_expand(MC_BIGCL
) = 0;
6703 mbwdog_logger("expanding MC_BIGCL by %d", n
);
6704 freelist_populate(MC_BIGCL
, n
, M_WAIT
);
6707 if (m_region_expand(MC_16KCL
) > 0) {
6709 mb_expand_16kcl_cnt
++;
6710 /* Adjust to current number of 16 KB cluster in use */
6711 n
= m_region_expand(MC_16KCL
) -
6712 (m_total(MC_16KCL
) - m_infree(MC_16KCL
));
6713 if ((n
+ m_total(MC_16KCL
)) > m_maxlimit(MC_16KCL
))
6714 n
= m_maxlimit(MC_16KCL
) - m_total(MC_16KCL
);
6716 mb_expand_16kcl_total
+= n
;
6718 m_region_expand(MC_16KCL
) = 0;
6721 mbwdog_logger("expanding MC_16KCL by %d", n
);
6722 (void) freelist_populate(MC_16KCL
, n
, M_WAIT
);
6727 * Because we can run out of memory before filling the mbuf
6728 * map, we should not allocate more clusters than they are
6729 * mbufs -- otherwise we could have a large number of useless
6730 * clusters allocated.
6732 mbwdog_logger("totals: MC_MBUF %d MC_BIGCL %d MC_CL %d MC_16KCL %d",
6733 m_total(MC_MBUF
), m_total(MC_BIGCL
), m_total(MC_CL
),
6735 uint32_t total_mbufs
= m_total(MC_MBUF
);
6736 uint32_t total_clusters
= m_total(MC_BIGCL
) + m_total(MC_CL
) +
6738 if (total_mbufs
< total_clusters
) {
6739 mbwdog_logger("expanding MC_MBUF by %d",
6740 total_clusters
- total_mbufs
);
6742 while (total_mbufs
< total_clusters
) {
6744 if (freelist_populate(MC_MBUF
, 1, M_WAIT
) == 0)
6746 total_mbufs
= m_total(MC_MBUF
);
6747 total_clusters
= m_total(MC_BIGCL
) + m_total(MC_CL
) +
6751 mbuf_worker_needs_wakeup
= TRUE
;
6753 * If there's a deadlock and we're not sending / receiving
6754 * packets, net_uptime() won't be updated. Update it here
6755 * so we are sure it's correct.
6757 net_update_uptime();
6758 mbuf_worker_last_runtime
= net_uptime();
6759 assert_wait((caddr_t
)&mbuf_worker_needs_wakeup
,
6761 mbwdog_logger("worker thread sleeping");
6762 lck_mtx_unlock(mbuf_mlock
);
6763 (void) thread_block((thread_continue_t
)mbuf_worker_thread
);
6767 __attribute__((noreturn
))
6769 mbuf_worker_thread_init(void)
6771 mbuf_worker_ready
++;
6772 mbuf_worker_thread();
6781 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
6783 VERIFY(MBUF_IN_MAP(buf
));
6784 ix
= ((unsigned char *)buf
- mbutl
) >> MBSHIFT
;
6785 VERIFY(ix
< maxslabgrp
);
6787 if ((slg
= slabstbl
[ix
]) == NULL
) {
6789 * In the current implementation, we never shrink the slabs
6790 * table; if we attempt to reallocate a cluster group when
6791 * it's already allocated, panic since this is a sign of a
6792 * memory corruption (slabstbl[ix] got nullified).
6795 VERIFY(ix
< slabgrp
);
6797 * Slabs expansion can only be done single threaded; when
6798 * we get here, it must be as a result of m_clalloc() which
6799 * is serialized and therefore mb_clalloc_busy must be set.
6801 VERIFY(mb_clalloc_busy
);
6802 lck_mtx_unlock(mbuf_mlock
);
6804 /* This is a new buffer; create the slabs group for it */
6805 MALLOC(slg
, mcl_slabg_t
*, sizeof (*slg
), M_TEMP
,
6807 MALLOC(slg
->slg_slab
, mcl_slab_t
*, sizeof(mcl_slab_t
) * NSLABSPMB
,
6808 M_TEMP
, M_WAITOK
| M_ZERO
);
6809 VERIFY(slg
!= NULL
&& slg
->slg_slab
!= NULL
);
6811 lck_mtx_lock(mbuf_mlock
);
6813 * No other thread could have gone into m_clalloc() after
6814 * we dropped the lock above, so verify that it's true.
6816 VERIFY(mb_clalloc_busy
);
6820 /* Chain each slab in the group to its forward neighbor */
6821 for (k
= 1; k
< NSLABSPMB
; k
++)
6822 slg
->slg_slab
[k
- 1].sl_next
= &slg
->slg_slab
[k
];
6823 VERIFY(slg
->slg_slab
[NSLABSPMB
- 1].sl_next
== NULL
);
6825 /* And chain the last slab in the previous group to this */
6827 VERIFY(slabstbl
[ix
- 1]->
6828 slg_slab
[NSLABSPMB
- 1].sl_next
== NULL
);
6829 slabstbl
[ix
- 1]->slg_slab
[NSLABSPMB
- 1].sl_next
=
6834 ix
= MTOPG(buf
) % NSLABSPMB
;
6835 VERIFY(ix
< NSLABSPMB
);
6837 return (&slg
->slg_slab
[ix
]);
6841 slab_init(mcl_slab_t
*sp
, mbuf_class_t
class, u_int32_t flags
,
6842 void *base
, void *head
, unsigned int len
, int refcnt
, int chunks
)
6844 sp
->sl_class
= class;
6845 sp
->sl_flags
= flags
;
6849 sp
->sl_refcnt
= refcnt
;
6850 sp
->sl_chunks
= chunks
;
6855 slab_insert(mcl_slab_t
*sp
, mbuf_class_t
class)
6857 VERIFY(slab_is_detached(sp
));
6858 m_slab_cnt(class)++;
6859 TAILQ_INSERT_TAIL(&m_slablist(class), sp
, sl_link
);
6860 sp
->sl_flags
&= ~SLF_DETACHED
;
6863 * If a buffer spans multiple contiguous pages then mark them as
6866 if (class == MC_16KCL
) {
6868 for (k
= 1; k
< NSLABSP16KB
; k
++) {
6870 /* Next slab must already be present */
6871 VERIFY(sp
!= NULL
&& slab_is_detached(sp
));
6872 sp
->sl_flags
&= ~SLF_DETACHED
;
6878 slab_remove(mcl_slab_t
*sp
, mbuf_class_t
class)
6881 VERIFY(!slab_is_detached(sp
));
6882 VERIFY(m_slab_cnt(class) > 0);
6883 m_slab_cnt(class)--;
6884 TAILQ_REMOVE(&m_slablist(class), sp
, sl_link
);
6886 if (class == MC_16KCL
) {
6887 for (k
= 1; k
< NSLABSP16KB
; k
++) {
6889 /* Next slab must already be present */
6891 VERIFY(!slab_is_detached(sp
));
6898 slab_inrange(mcl_slab_t
*sp
, void *buf
)
6900 return ((uintptr_t)buf
>= (uintptr_t)sp
->sl_base
&&
6901 (uintptr_t)buf
< ((uintptr_t)sp
->sl_base
+ sp
->sl_len
));
6907 slab_nextptr_panic(mcl_slab_t
*sp
, void *addr
)
6910 unsigned int chunk_len
= sp
->sl_len
/ sp
->sl_chunks
;
6911 uintptr_t buf
= (uintptr_t)sp
->sl_base
;
6913 for (i
= 0; i
< sp
->sl_chunks
; i
++, buf
+= chunk_len
) {
6914 void *next
= ((mcache_obj_t
*)buf
)->obj_next
;
6918 if (next
!= NULL
&& !MBUF_IN_MAP(next
)) {
6919 mcache_t
*cp
= m_cache(sp
->sl_class
);
6920 panic("%s: %s buffer %p in slab %p modified "
6921 "after free at offset 0: %p out of range "
6922 "[%p-%p)\n", __func__
, cp
->mc_name
,
6923 (void *)buf
, sp
, next
, mbutl
, embutl
);
6927 mcache_audit_t
*mca
= mcl_audit_buf2mca(sp
->sl_class
,
6928 (mcache_obj_t
*)buf
);
6929 mcl_audit_verify_nextptr(next
, mca
);
6935 slab_detach(mcl_slab_t
*sp
)
6937 sp
->sl_link
.tqe_next
= (mcl_slab_t
*)-1;
6938 sp
->sl_link
.tqe_prev
= (mcl_slab_t
**)-1;
6939 sp
->sl_flags
|= SLF_DETACHED
;
6943 slab_is_detached(mcl_slab_t
*sp
)
6945 return ((intptr_t)sp
->sl_link
.tqe_next
== -1 &&
6946 (intptr_t)sp
->sl_link
.tqe_prev
== -1 &&
6947 (sp
->sl_flags
& SLF_DETACHED
));
6951 mcl_audit_init(void *buf
, mcache_audit_t
**mca_list
,
6952 mcache_obj_t
**con_list
, size_t con_size
, unsigned int num
)
6954 mcache_audit_t
*mca
, *mca_tail
;
6955 mcache_obj_t
*con
= NULL
;
6956 boolean_t save_contents
= (con_list
!= NULL
);
6959 ASSERT(num
<= NMBPG
);
6960 ASSERT(con_list
== NULL
|| con_size
!= 0);
6963 VERIFY(ix
< maxclaudit
);
6965 /* Make sure we haven't been here before */
6966 for (i
= 0; i
< num
; i
++)
6967 VERIFY(mclaudit
[ix
].cl_audit
[i
] == NULL
);
6969 mca
= mca_tail
= *mca_list
;
6973 for (i
= 0; i
< num
; i
++) {
6974 mcache_audit_t
*next
;
6976 next
= mca
->mca_next
;
6977 bzero(mca
, sizeof (*mca
));
6978 mca
->mca_next
= next
;
6979 mclaudit
[ix
].cl_audit
[i
] = mca
;
6981 /* Attach the contents buffer if requested */
6982 if (save_contents
) {
6983 mcl_saved_contents_t
*msc
=
6984 (mcl_saved_contents_t
*)(void *)con
;
6986 VERIFY(msc
!= NULL
);
6987 VERIFY(IS_P2ALIGNED(msc
, sizeof (u_int64_t
)));
6988 VERIFY(con_size
== sizeof (*msc
));
6989 mca
->mca_contents_size
= con_size
;
6990 mca
->mca_contents
= msc
;
6991 con
= con
->obj_next
;
6992 bzero(mca
->mca_contents
, mca
->mca_contents_size
);
6996 mca
= mca
->mca_next
;
7002 *mca_list
= mca_tail
->mca_next
;
7003 mca_tail
->mca_next
= NULL
;
7007 mcl_audit_free(void *buf
, unsigned int num
)
7010 mcache_audit_t
*mca
, *mca_list
;
7013 VERIFY(ix
< maxclaudit
);
7015 if (mclaudit
[ix
].cl_audit
[0] != NULL
) {
7016 mca_list
= mclaudit
[ix
].cl_audit
[0];
7017 for (i
= 0; i
< num
; i
++) {
7018 mca
= mclaudit
[ix
].cl_audit
[i
];
7019 mclaudit
[ix
].cl_audit
[i
] = NULL
;
7020 if (mca
->mca_contents
)
7021 mcache_free(mcl_audit_con_cache
,
7024 mcache_free_ext(mcache_audit_cache
,
7025 (mcache_obj_t
*)mca_list
);
7030 * Given an address of a buffer (mbuf/2KB/4KB/16KB), return
7031 * the corresponding audit structure for that buffer.
7033 static mcache_audit_t
*
7034 mcl_audit_buf2mca(mbuf_class_t
class, mcache_obj_t
*mobj
)
7036 mcache_audit_t
*mca
= NULL
;
7037 int ix
= MTOPG(mobj
), m_idx
= 0;
7038 unsigned char *page_addr
;
7040 VERIFY(ix
< maxclaudit
);
7041 VERIFY(IS_P2ALIGNED(mobj
, MIN(m_maxsize(class), PAGE_SIZE
)));
7043 page_addr
= PGTOM(ix
);
7048 * For the mbuf case, find the index of the page
7049 * used by the mbuf and use that index to locate the
7050 * base address of the page. Then find out the
7051 * mbuf index relative to the page base and use
7052 * it to locate the audit structure.
7054 m_idx
= MBPAGEIDX(page_addr
, mobj
);
7055 VERIFY(m_idx
< (int)NMBPG
);
7056 mca
= mclaudit
[ix
].cl_audit
[m_idx
];
7061 * Same thing as above, but for 2KB clusters in a page.
7063 m_idx
= CLPAGEIDX(page_addr
, mobj
);
7064 VERIFY(m_idx
< (int)NCLPG
);
7065 mca
= mclaudit
[ix
].cl_audit
[m_idx
];
7069 m_idx
= BCLPAGEIDX(page_addr
, mobj
);
7070 VERIFY(m_idx
< (int)NBCLPG
);
7071 mca
= mclaudit
[ix
].cl_audit
[m_idx
];
7075 * Same as above, but only return the first element.
7077 mca
= mclaudit
[ix
].cl_audit
[0];
7089 mcl_audit_mbuf(mcache_audit_t
*mca
, void *addr
, boolean_t composite
,
7092 struct mbuf
*m
= addr
;
7093 mcache_obj_t
*next
= ((mcache_obj_t
*)m
)->obj_next
;
7095 VERIFY(mca
->mca_contents
!= NULL
&&
7096 mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
7099 mcl_audit_verify_nextptr(next
, mca
);
7102 /* Save constructed mbuf fields */
7103 mcl_audit_save_mbuf(m
, mca
);
7105 mcache_set_pattern(MCACHE_FREE_PATTERN
, m
,
7106 m_maxsize(MC_MBUF
));
7108 ((mcache_obj_t
*)m
)->obj_next
= next
;
7112 /* Check if the buffer has been corrupted while in freelist */
7114 mcache_audit_free_verify_set(mca
, addr
, 0, m_maxsize(MC_MBUF
));
7116 /* Restore constructed mbuf fields */
7117 mcl_audit_restore_mbuf(m
, mca
, composite
);
7121 mcl_audit_restore_mbuf(struct mbuf
*m
, mcache_audit_t
*mca
, boolean_t composite
)
7123 struct mbuf
*ms
= MCA_SAVED_MBUF_PTR(mca
);
7126 struct mbuf
*next
= m
->m_next
;
7127 VERIFY(ms
->m_flags
== M_EXT
&& m_get_rfa(ms
) != NULL
&&
7128 MBUF_IS_COMPOSITE(ms
));
7129 VERIFY(mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
7131 * We could have hand-picked the mbuf fields and restore
7132 * them individually, but that will be a maintenance
7133 * headache. Instead, restore everything that was saved;
7134 * the mbuf layer will recheck and reinitialize anyway.
7136 bcopy(ms
, m
, MCA_SAVED_MBUF_SIZE
);
7140 * For a regular mbuf (no cluster attached) there's nothing
7141 * to restore other than the type field, which is expected
7144 m
->m_type
= ms
->m_type
;
7150 mcl_audit_save_mbuf(struct mbuf
*m
, mcache_audit_t
*mca
)
7152 VERIFY(mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
7154 bcopy(m
, MCA_SAVED_MBUF_PTR(mca
), MCA_SAVED_MBUF_SIZE
);
7158 mcl_audit_cluster(mcache_audit_t
*mca
, void *addr
, size_t size
, boolean_t alloc
,
7159 boolean_t save_next
)
7161 mcache_obj_t
*next
= ((mcache_obj_t
*)addr
)->obj_next
;
7165 mcache_set_pattern(MCACHE_FREE_PATTERN
, addr
, size
);
7168 mcl_audit_verify_nextptr(next
, mca
);
7169 ((mcache_obj_t
*)addr
)->obj_next
= next
;
7171 } else if (mclverify
) {
7172 /* Check if the buffer has been corrupted while in freelist */
7173 mcl_audit_verify_nextptr(next
, mca
);
7174 mcache_audit_free_verify_set(mca
, addr
, 0, size
);
7179 mcl_audit_scratch(mcache_audit_t
*mca
)
7181 void *stack
[MCACHE_STACK_DEPTH
+ 1];
7182 mcl_scratch_audit_t
*msa
;
7185 VERIFY(mca
->mca_contents
!= NULL
);
7186 msa
= MCA_SAVED_SCRATCH_PTR(mca
);
7188 msa
->msa_pthread
= msa
->msa_thread
;
7189 msa
->msa_thread
= current_thread();
7190 bcopy(msa
->msa_stack
, msa
->msa_pstack
, sizeof (msa
->msa_pstack
));
7191 msa
->msa_pdepth
= msa
->msa_depth
;
7192 bzero(stack
, sizeof (stack
));
7193 msa
->msa_depth
= OSBacktrace(stack
, MCACHE_STACK_DEPTH
+ 1) - 1;
7194 bcopy(&stack
[1], msa
->msa_stack
, sizeof (msa
->msa_stack
));
7196 msa
->msa_ptstamp
= msa
->msa_tstamp
;
7198 /* tstamp is in ms relative to base_ts */
7199 msa
->msa_tstamp
= ((now
.tv_usec
- mb_start
.tv_usec
) / 1000);
7200 if ((now
.tv_sec
- mb_start
.tv_sec
) > 0)
7201 msa
->msa_tstamp
+= ((now
.tv_sec
- mb_start
.tv_sec
) * 1000);
7205 mcl_audit_mcheck_panic(struct mbuf
*m
)
7207 mcache_audit_t
*mca
;
7210 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
7212 panic("mcl_audit: freed mbuf %p with type 0x%x (instead of 0x%x)\n%s\n",
7213 m
, (u_int16_t
)m
->m_type
, MT_FREE
, mcache_dump_mca(mca
));
7218 mcl_audit_verify_nextptr(void *next
, mcache_audit_t
*mca
)
7220 if (next
!= NULL
&& !MBUF_IN_MAP(next
) &&
7221 (next
!= (void *)MCACHE_FREE_PATTERN
|| !mclverify
)) {
7222 panic("mcl_audit: buffer %p modified after free at offset 0: "
7223 "%p out of range [%p-%p)\n%s\n",
7224 mca
->mca_addr
, next
, mbutl
, embutl
, mcache_dump_mca(mca
));
7229 /* This function turns on mbuf leak detection */
7231 mleak_activate(void)
7233 mleak_table
.mleak_sample_factor
= MLEAK_SAMPLE_FACTOR
;
7234 PE_parse_boot_argn("mleak_sample_factor",
7235 &mleak_table
.mleak_sample_factor
,
7236 sizeof (mleak_table
.mleak_sample_factor
));
7238 if (mleak_table
.mleak_sample_factor
== 0)
7241 if (mclfindleak
== 0)
7244 vm_size_t alloc_size
=
7245 mleak_alloc_buckets
* sizeof (struct mallocation
);
7246 vm_size_t trace_size
= mleak_trace_buckets
* sizeof (struct mtrace
);
7248 MALLOC(mleak_allocations
, struct mallocation
*, alloc_size
,
7249 M_TEMP
, M_WAITOK
| M_ZERO
);
7250 VERIFY(mleak_allocations
!= NULL
);
7252 MALLOC(mleak_traces
, struct mtrace
*, trace_size
,
7253 M_TEMP
, M_WAITOK
| M_ZERO
);
7254 VERIFY(mleak_traces
!= NULL
);
7256 MALLOC(mleak_stat
, mleak_stat_t
*, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES
),
7257 M_TEMP
, M_WAITOK
| M_ZERO
);
7258 VERIFY(mleak_stat
!= NULL
);
7259 mleak_stat
->ml_cnt
= MLEAK_NUM_TRACES
;
7261 mleak_stat
->ml_isaddr64
= 1;
7262 #endif /* __LP64__ */
7266 mleak_logger(u_int32_t num
, mcache_obj_t
*addr
, boolean_t alloc
)
7270 if (mclfindleak
== 0)
7274 return (mleak_free(addr
));
7276 temp
= atomic_add_32_ov(&mleak_table
.mleak_capture
, 1);
7278 if ((temp
% mleak_table
.mleak_sample_factor
) == 0 && addr
!= NULL
) {
7279 uintptr_t bt
[MLEAK_STACK_DEPTH
];
7280 int logged
= backtrace(bt
, MLEAK_STACK_DEPTH
);
7281 mleak_log(bt
, addr
, logged
, num
);
7286 * This function records the allocation in the mleak_allocations table
7287 * and the backtrace in the mleak_traces table; if allocation slot is in use,
7288 * replace old allocation with new one if the trace slot is in use, return
7289 * (or increment refcount if same trace).
7292 mleak_log(uintptr_t *bt
, mcache_obj_t
*addr
, uint32_t depth
, int num
)
7294 struct mallocation
*allocation
;
7295 struct mtrace
*trace
;
7296 uint32_t trace_index
;
7298 /* Quit if someone else modifying the tables */
7299 if (!lck_mtx_try_lock_spin(mleak_lock
)) {
7300 mleak_table
.total_conflicts
++;
7304 allocation
= &mleak_allocations
[hashaddr((uintptr_t)addr
,
7305 mleak_alloc_buckets
)];
7306 trace_index
= hashbacktrace(bt
, depth
, mleak_trace_buckets
);
7307 trace
= &mleak_traces
[trace_index
];
7309 VERIFY(allocation
<= &mleak_allocations
[mleak_alloc_buckets
- 1]);
7310 VERIFY(trace
<= &mleak_traces
[mleak_trace_buckets
- 1]);
7312 allocation
->hitcount
++;
7316 * If the allocation bucket we want is occupied
7317 * and the occupier has the same trace, just bail.
7319 if (allocation
->element
!= NULL
&&
7320 trace_index
== allocation
->trace_index
) {
7321 mleak_table
.alloc_collisions
++;
7322 lck_mtx_unlock(mleak_lock
);
7327 * Store the backtrace in the traces array;
7328 * Size of zero = trace bucket is free.
7330 if (trace
->allocs
> 0 &&
7331 bcmp(trace
->addr
, bt
, (depth
* sizeof (uintptr_t))) != 0) {
7332 /* Different, unique trace, but the same hash! Bail out. */
7333 trace
->collisions
++;
7334 mleak_table
.trace_collisions
++;
7335 lck_mtx_unlock(mleak_lock
);
7337 } else if (trace
->allocs
> 0) {
7338 /* Same trace, already added, so increment refcount */
7341 /* Found an unused trace bucket, so record the trace here */
7342 if (trace
->depth
!= 0) {
7343 /* this slot previously used but not currently in use */
7344 mleak_table
.trace_overwrites
++;
7346 mleak_table
.trace_recorded
++;
7348 memcpy(trace
->addr
, bt
, (depth
* sizeof (uintptr_t)));
7349 trace
->depth
= depth
;
7350 trace
->collisions
= 0;
7353 /* Step 2: Store the allocation record in the allocations array */
7354 if (allocation
->element
!= NULL
) {
7356 * Replace an existing allocation. No need to preserve
7357 * because only a subset of the allocations are being
7360 mleak_table
.alloc_collisions
++;
7361 } else if (allocation
->trace_index
!= 0) {
7362 mleak_table
.alloc_overwrites
++;
7364 allocation
->element
= addr
;
7365 allocation
->trace_index
= trace_index
;
7366 allocation
->count
= num
;
7367 mleak_table
.alloc_recorded
++;
7368 mleak_table
.outstanding_allocs
++;
7370 lck_mtx_unlock(mleak_lock
);
7375 mleak_free(mcache_obj_t
*addr
)
7377 while (addr
!= NULL
) {
7378 struct mallocation
*allocation
= &mleak_allocations
7379 [hashaddr((uintptr_t)addr
, mleak_alloc_buckets
)];
7381 if (allocation
->element
== addr
&&
7382 allocation
->trace_index
< mleak_trace_buckets
) {
7383 lck_mtx_lock_spin(mleak_lock
);
7384 if (allocation
->element
== addr
&&
7385 allocation
->trace_index
< mleak_trace_buckets
) {
7386 struct mtrace
*trace
;
7387 trace
= &mleak_traces
[allocation
->trace_index
];
7388 /* allocs = 0 means trace bucket is unused */
7389 if (trace
->allocs
> 0)
7391 if (trace
->allocs
== 0)
7393 /* NULL element means alloc bucket is unused */
7394 allocation
->element
= NULL
;
7395 mleak_table
.outstanding_allocs
--;
7397 lck_mtx_unlock(mleak_lock
);
7399 addr
= addr
->obj_next
;
7407 struct mtrace
*swap
;
7409 for(i
= 0; i
< MLEAK_NUM_TRACES
; i
++)
7410 mleak_top_trace
[i
] = NULL
;
7412 for(i
= 0, j
= 0; j
< MLEAK_NUM_TRACES
&& i
< mleak_trace_buckets
; i
++)
7414 if (mleak_traces
[i
].allocs
<= 0)
7417 mleak_top_trace
[j
] = &mleak_traces
[i
];
7418 for (k
= j
; k
> 0; k
--) {
7419 if (mleak_top_trace
[k
]->allocs
<=
7420 mleak_top_trace
[k
-1]->allocs
)
7423 swap
= mleak_top_trace
[k
-1];
7424 mleak_top_trace
[k
-1] = mleak_top_trace
[k
];
7425 mleak_top_trace
[k
] = swap
;
7431 for(; i
< mleak_trace_buckets
; i
++) {
7432 if (mleak_traces
[i
].allocs
<= mleak_top_trace
[j
]->allocs
)
7435 mleak_top_trace
[j
] = &mleak_traces
[i
];
7437 for (k
= j
; k
> 0; k
--) {
7438 if (mleak_top_trace
[k
]->allocs
<=
7439 mleak_top_trace
[k
-1]->allocs
)
7442 swap
= mleak_top_trace
[k
-1];
7443 mleak_top_trace
[k
-1] = mleak_top_trace
[k
];
7444 mleak_top_trace
[k
] = swap
;
7450 mleak_update_stats()
7452 mleak_trace_stat_t
*mltr
;
7455 VERIFY(mleak_stat
!= NULL
);
7457 VERIFY(mleak_stat
->ml_isaddr64
);
7459 VERIFY(!mleak_stat
->ml_isaddr64
);
7460 #endif /* !__LP64__ */
7461 VERIFY(mleak_stat
->ml_cnt
== MLEAK_NUM_TRACES
);
7463 mleak_sort_traces();
7465 mltr
= &mleak_stat
->ml_trace
[0];
7466 bzero(mltr
, sizeof (*mltr
) * MLEAK_NUM_TRACES
);
7467 for (i
= 0; i
< MLEAK_NUM_TRACES
; i
++) {
7470 if (mleak_top_trace
[i
] == NULL
||
7471 mleak_top_trace
[i
]->allocs
== 0)
7474 mltr
->mltr_collisions
= mleak_top_trace
[i
]->collisions
;
7475 mltr
->mltr_hitcount
= mleak_top_trace
[i
]->hitcount
;
7476 mltr
->mltr_allocs
= mleak_top_trace
[i
]->allocs
;
7477 mltr
->mltr_depth
= mleak_top_trace
[i
]->depth
;
7479 VERIFY(mltr
->mltr_depth
<= MLEAK_STACK_DEPTH
);
7480 for (j
= 0; j
< mltr
->mltr_depth
; j
++)
7481 mltr
->mltr_addr
[j
] = mleak_top_trace
[i
]->addr
[j
];
7487 static struct mbtypes
{
7489 const char *mt_name
;
7491 { MT_DATA
, "data" },
7492 { MT_OOBDATA
, "oob data" },
7493 { MT_CONTROL
, "ancillary data" },
7494 { MT_HEADER
, "packet headers" },
7495 { MT_SOCKET
, "socket structures" },
7496 { MT_PCB
, "protocol control blocks" },
7497 { MT_RTABLE
, "routing table entries" },
7498 { MT_HTABLE
, "IMP host table entries" },
7499 { MT_ATABLE
, "address resolution tables" },
7500 { MT_FTABLE
, "fragment reassembly queue headers" },
7501 { MT_SONAME
, "socket names and addresses" },
7502 { MT_SOOPTS
, "socket options" },
7503 { MT_RIGHTS
, "access rights" },
7504 { MT_IFADDR
, "interface addresses" },
7505 { MT_TAG
, "packet tags" },
7509 #define MBUF_DUMP_BUF_CHK() { \
7519 unsigned long totmem
= 0, totfree
= 0, totmbufs
, totused
, totpct
,
7521 u_int32_t m_mbufs
= 0, m_clfree
= 0, m_bigclfree
= 0;
7522 u_int32_t m_mbufclfree
= 0, m_mbufbigclfree
= 0;
7523 u_int32_t m_16kclusters
= 0, m_16kclfree
= 0, m_mbuf16kclfree
= 0;
7524 int nmbtypes
= sizeof (mbstat
.m_mtypes
) / sizeof (short);
7527 mb_class_stat_t
*sp
;
7528 mleak_trace_stat_t
*mltr
;
7529 char *c
= mbuf_dump_buf
;
7530 int i
, j
, k
, clen
= MBUF_DUMP_BUF_SIZE
;
7531 bool printed_banner
= false;
7533 mbuf_dump_buf
[0] = '\0';
7535 /* synchronize all statistics in the mbuf table */
7537 mbuf_mtypes_sync(TRUE
);
7539 sp
= &mb_stat
->mbs_class
[0];
7540 for (i
= 0; i
< mb_stat
->mbs_cnt
; i
++, sp
++) {
7543 if (m_class(i
) == MC_MBUF
) {
7544 m_mbufs
= sp
->mbcl_active
;
7545 } else if (m_class(i
) == MC_CL
) {
7546 m_clfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7547 } else if (m_class(i
) == MC_BIGCL
) {
7548 m_bigclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7549 } else if (njcl
> 0 && m_class(i
) == MC_16KCL
) {
7550 m_16kclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7551 m_16kclusters
= sp
->mbcl_total
;
7552 } else if (m_class(i
) == MC_MBUF_CL
) {
7553 m_mbufclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7554 } else if (m_class(i
) == MC_MBUF_BIGCL
) {
7555 m_mbufbigclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7556 } else if (njcl
> 0 && m_class(i
) == MC_MBUF_16KCL
) {
7557 m_mbuf16kclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7560 mem
= sp
->mbcl_ctotal
* sp
->mbcl_size
;
7562 totfree
+= (sp
->mbcl_mc_cached
+ sp
->mbcl_infree
) *
7564 totreturned
+= sp
->mbcl_release_cnt
;
7568 /* adjust free counts to include composite caches */
7569 m_clfree
+= m_mbufclfree
;
7570 m_bigclfree
+= m_mbufbigclfree
;
7571 m_16kclfree
+= m_mbuf16kclfree
;
7574 for (mp
= mbtypes
; mp
->mt_name
!= NULL
; mp
++)
7575 totmbufs
+= mbstat
.m_mtypes
[mp
->mt_type
];
7576 if (totmbufs
> m_mbufs
)
7578 k
= snprintf(c
, clen
, "%lu/%u mbufs in use:\n", totmbufs
, m_mbufs
);
7579 MBUF_DUMP_BUF_CHK();
7581 bzero(&seen
, sizeof (seen
));
7582 for (mp
= mbtypes
; mp
->mt_name
!= NULL
; mp
++) {
7583 if (mbstat
.m_mtypes
[mp
->mt_type
] != 0) {
7584 seen
[mp
->mt_type
] = 1;
7585 k
= snprintf(c
, clen
, "\t%u mbufs allocated to %s\n",
7586 mbstat
.m_mtypes
[mp
->mt_type
], mp
->mt_name
);
7587 MBUF_DUMP_BUF_CHK();
7591 for (i
= 0; i
< nmbtypes
; i
++)
7592 if (!seen
[i
] && mbstat
.m_mtypes
[i
] != 0) {
7593 k
= snprintf(c
, clen
, "\t%u mbufs allocated to "
7594 "<mbuf type %d>\n", mbstat
.m_mtypes
[i
], i
);
7595 MBUF_DUMP_BUF_CHK();
7597 if ((m_mbufs
- totmbufs
) > 0) {
7598 k
= snprintf(c
, clen
, "\t%lu mbufs allocated to caches\n",
7599 m_mbufs
- totmbufs
);
7600 MBUF_DUMP_BUF_CHK();
7602 k
= snprintf(c
, clen
, "%u/%u mbuf 2KB clusters in use\n"
7603 "%u/%u mbuf 4KB clusters in use\n",
7604 (unsigned int)(mbstat
.m_clusters
- m_clfree
),
7605 (unsigned int)mbstat
.m_clusters
,
7606 (unsigned int)(mbstat
.m_bigclusters
- m_bigclfree
),
7607 (unsigned int)mbstat
.m_bigclusters
);
7608 MBUF_DUMP_BUF_CHK();
7611 k
= snprintf(c
, clen
, "%u/%u mbuf %uKB clusters in use\n",
7612 m_16kclusters
- m_16kclfree
, m_16kclusters
,
7614 MBUF_DUMP_BUF_CHK();
7616 totused
= totmem
- totfree
;
7619 } else if (totused
< (ULONG_MAX
/ 100)) {
7620 totpct
= (totused
* 100) / totmem
;
7622 u_long totmem1
= totmem
/ 100;
7623 u_long totused1
= totused
/ 100;
7624 totpct
= (totused1
* 100) / totmem1
;
7626 k
= snprintf(c
, clen
, "%lu KB allocated to network (approx. %lu%% "
7627 "in use)\n", totmem
/ 1024, totpct
);
7628 MBUF_DUMP_BUF_CHK();
7629 k
= snprintf(c
, clen
, "%lu KB returned to the system\n",
7630 totreturned
/ 1024);
7631 MBUF_DUMP_BUF_CHK();
7633 net_update_uptime();
7634 k
= snprintf(c
, clen
,
7635 "VM allocation failures: contiguous %u, normal %u, one page %u\n",
7636 mb_kmem_contig_failed
, mb_kmem_failed
, mb_kmem_one_failed
);
7637 MBUF_DUMP_BUF_CHK();
7638 if (mb_kmem_contig_failed_ts
|| mb_kmem_failed_ts
||
7639 mb_kmem_one_failed_ts
) {
7640 k
= snprintf(c
, clen
,
7641 "VM allocation failure timestamps: contiguous %llu "
7642 "(size %llu), normal %llu (size %llu), one page %llu "
7644 mb_kmem_contig_failed_ts
, mb_kmem_contig_failed_size
,
7645 mb_kmem_failed_ts
, mb_kmem_failed_size
,
7646 mb_kmem_one_failed_ts
, net_uptime());
7647 MBUF_DUMP_BUF_CHK();
7648 k
= snprintf(c
, clen
,
7649 "VM return codes: ");
7650 MBUF_DUMP_BUF_CHK();
7652 i
< sizeof(mb_kmem_stats
) / sizeof(mb_kmem_stats
[0]);
7654 k
= snprintf(c
, clen
, "%s: %u ", mb_kmem_stats_labels
[i
],
7656 MBUF_DUMP_BUF_CHK();
7658 k
= snprintf(c
, clen
, "\n");
7659 MBUF_DUMP_BUF_CHK();
7661 k
= snprintf(c
, clen
,
7662 "worker thread runs: %u, expansions: %llu, cl %llu/%llu, "
7663 "bigcl %llu/%llu, 16k %llu/%llu\n", mbuf_worker_run_cnt
,
7664 mb_expand_cnt
, mb_expand_cl_cnt
, mb_expand_cl_total
,
7665 mb_expand_bigcl_cnt
, mb_expand_bigcl_total
, mb_expand_16kcl_cnt
,
7666 mb_expand_16kcl_total
);
7667 MBUF_DUMP_BUF_CHK();
7668 if (mbuf_worker_last_runtime
!= 0) {
7669 k
= snprintf(c
, clen
, "worker thread last run time: "
7670 "%llu (%llu seconds ago)\n",
7671 mbuf_worker_last_runtime
,
7672 net_uptime() - mbuf_worker_last_runtime
);
7673 MBUF_DUMP_BUF_CHK();
7675 if (mbuf_drain_last_runtime
!= 0) {
7676 k
= snprintf(c
, clen
, "drain routine last run time: "
7677 "%llu (%llu seconds ago)\n",
7678 mbuf_drain_last_runtime
,
7679 net_uptime() - mbuf_drain_last_runtime
);
7680 MBUF_DUMP_BUF_CHK();
7683 #if DEBUG || DEVELOPMENT
7684 k
= snprintf(c
, clen
, "\nworker thread log:\n%s\n", mbwdog_logging
);
7685 MBUF_DUMP_BUF_CHK();
7688 for (j
= 0; j
< MTRACELARGE_NUM_TRACES
; j
++) {
7689 struct mtracelarge
*trace
= &mtracelarge_table
[j
];
7690 if (trace
->size
== 0 || trace
->depth
== 0)
7692 if (printed_banner
== false) {
7693 k
= snprintf(c
, clen
,
7694 "\nlargest allocation failure backtraces:\n");
7695 MBUF_DUMP_BUF_CHK();
7696 printed_banner
= true;
7698 k
= snprintf(c
, clen
, "size %llu: < ", trace
->size
);
7699 MBUF_DUMP_BUF_CHK();
7700 for (i
= 0; i
< trace
->depth
; i
++) {
7701 if (mleak_stat
->ml_isaddr64
) {
7702 k
= snprintf(c
, clen
, "0x%0llx ",
7703 (uint64_t)VM_KERNEL_UNSLIDE(
7706 k
= snprintf(c
, clen
,
7708 (uint32_t)VM_KERNEL_UNSLIDE(
7711 MBUF_DUMP_BUF_CHK();
7713 k
= snprintf(c
, clen
, ">\n");
7714 MBUF_DUMP_BUF_CHK();
7717 /* mbuf leak detection statistics */
7718 mleak_update_stats();
7720 k
= snprintf(c
, clen
, "\nmbuf leak detection table:\n");
7721 MBUF_DUMP_BUF_CHK();
7722 k
= snprintf(c
, clen
, "\ttotal captured: %u (one per %u)\n",
7723 mleak_table
.mleak_capture
/ mleak_table
.mleak_sample_factor
,
7724 mleak_table
.mleak_sample_factor
);
7725 MBUF_DUMP_BUF_CHK();
7726 k
= snprintf(c
, clen
, "\ttotal allocs outstanding: %llu\n",
7727 mleak_table
.outstanding_allocs
);
7728 MBUF_DUMP_BUF_CHK();
7729 k
= snprintf(c
, clen
, "\tnew hash recorded: %llu allocs, %llu traces\n",
7730 mleak_table
.alloc_recorded
, mleak_table
.trace_recorded
);
7731 MBUF_DUMP_BUF_CHK();
7732 k
= snprintf(c
, clen
, "\thash collisions: %llu allocs, %llu traces\n",
7733 mleak_table
.alloc_collisions
, mleak_table
.trace_collisions
);
7734 MBUF_DUMP_BUF_CHK();
7735 k
= snprintf(c
, clen
, "\toverwrites: %llu allocs, %llu traces\n",
7736 mleak_table
.alloc_overwrites
, mleak_table
.trace_overwrites
);
7737 MBUF_DUMP_BUF_CHK();
7738 k
= snprintf(c
, clen
, "\tlock conflicts: %llu\n\n",
7739 mleak_table
.total_conflicts
);
7740 MBUF_DUMP_BUF_CHK();
7742 k
= snprintf(c
, clen
, "top %d outstanding traces:\n",
7743 mleak_stat
->ml_cnt
);
7744 MBUF_DUMP_BUF_CHK();
7745 for (i
= 0; i
< mleak_stat
->ml_cnt
; i
++) {
7746 mltr
= &mleak_stat
->ml_trace
[i
];
7747 k
= snprintf(c
, clen
, "[%d] %llu outstanding alloc(s), "
7748 "%llu hit(s), %llu collision(s)\n", (i
+ 1),
7749 mltr
->mltr_allocs
, mltr
->mltr_hitcount
,
7750 mltr
->mltr_collisions
);
7751 MBUF_DUMP_BUF_CHK();
7754 if (mleak_stat
->ml_isaddr64
)
7755 k
= snprintf(c
, clen
, MB_LEAK_HDR_64
);
7757 k
= snprintf(c
, clen
, MB_LEAK_HDR_32
);
7758 MBUF_DUMP_BUF_CHK();
7760 for (i
= 0; i
< MLEAK_STACK_DEPTH
; i
++) {
7761 k
= snprintf(c
, clen
, "%2d: ", (i
+ 1));
7762 MBUF_DUMP_BUF_CHK();
7763 for (j
= 0; j
< mleak_stat
->ml_cnt
; j
++) {
7764 mltr
= &mleak_stat
->ml_trace
[j
];
7765 if (i
< mltr
->mltr_depth
) {
7766 if (mleak_stat
->ml_isaddr64
) {
7767 k
= snprintf(c
, clen
, "0x%0llx ",
7768 (uint64_t)VM_KERNEL_UNSLIDE(
7769 mltr
->mltr_addr
[i
]));
7771 k
= snprintf(c
, clen
,
7773 (uint32_t)VM_KERNEL_UNSLIDE(
7774 mltr
->mltr_addr
[i
]));
7777 if (mleak_stat
->ml_isaddr64
)
7778 k
= snprintf(c
, clen
,
7779 MB_LEAK_SPACING_64
);
7781 k
= snprintf(c
, clen
,
7782 MB_LEAK_SPACING_32
);
7784 MBUF_DUMP_BUF_CHK();
7786 k
= snprintf(c
, clen
, "\n");
7787 MBUF_DUMP_BUF_CHK();
7790 return (mbuf_dump_buf
);
7793 #undef MBUF_DUMP_BUF_CHK
7796 * Convert between a regular and a packet header mbuf. Caller is responsible
7797 * for setting or clearing M_PKTHDR; this routine does the rest of the work.
7800 m_reinit(struct mbuf
*m
, int hdr
)
7805 VERIFY(!(m
->m_flags
& M_PKTHDR
));
7806 if (!(m
->m_flags
& M_EXT
) &&
7807 (m
->m_data
!= m
->m_dat
|| m
->m_len
> 0)) {
7809 * If there's no external cluster attached and the
7810 * mbuf appears to contain user data, we cannot
7811 * safely convert this to a packet header mbuf,
7812 * as the packet header structure might overlap
7815 printf("%s: cannot set M_PKTHDR on altered mbuf %llx, "
7816 "m_data %llx (expected %llx), "
7817 "m_len %d (expected 0)\n",
7819 (uint64_t)VM_KERNEL_ADDRPERM(m
),
7820 (uint64_t)VM_KERNEL_ADDRPERM(m
->m_data
),
7821 (uint64_t)VM_KERNEL_ADDRPERM(m
->m_dat
), m
->m_len
);
7824 VERIFY((m
->m_flags
& M_EXT
) || m
->m_data
== m
->m_dat
);
7825 m
->m_flags
|= M_PKTHDR
;
7826 MBUF_INIT_PKTHDR(m
);
7829 /* Check for scratch area overflow */
7830 m_redzone_verify(m
);
7831 /* Free the aux data and tags if there is any */
7832 m_tag_delete_chain(m
, NULL
);
7833 m
->m_flags
&= ~M_PKTHDR
;
7840 m_ext_set_prop(struct mbuf
*m
, uint32_t o
, uint32_t n
)
7842 ASSERT(m
->m_flags
& M_EXT
);
7843 return (atomic_test_set_32(&MEXT_PRIV(m
), o
, n
));
7847 m_ext_get_prop(struct mbuf
*m
)
7849 ASSERT(m
->m_flags
& M_EXT
);
7850 return (MEXT_PRIV(m
));
7854 m_ext_paired_is_active(struct mbuf
*m
)
7856 return (MBUF_IS_PAIRED(m
) ? (MEXT_PREF(m
) > MEXT_MINREF(m
)) : 1);
7860 m_ext_paired_activate(struct mbuf
*m
)
7862 struct ext_ref
*rfa
;
7865 m_ext_free_func_t extfree
;
7868 VERIFY(MBUF_IS_PAIRED(m
));
7869 VERIFY(MEXT_REF(m
) == MEXT_MINREF(m
));
7870 VERIFY(MEXT_PREF(m
) == MEXT_MINREF(m
));
7872 hdr
= (m
->m_flags
& M_PKTHDR
);
7874 extbuf
= m
->m_ext
.ext_buf
;
7875 extfree
= m_get_ext_free(m
);
7876 extsize
= m
->m_ext
.ext_size
;
7879 VERIFY(extbuf
!= NULL
&& rfa
!= NULL
);
7882 * Safe to reinitialize packet header tags, since it's
7883 * already taken care of at m_free() time. Similar to
7884 * what's done in m_clattach() for the cluster. Bump
7885 * up MEXT_PREF to indicate activation.
7887 MBUF_INIT(m
, hdr
, type
);
7888 MEXT_INIT(m
, extbuf
, extsize
, extfree
, (caddr_t
)m
, rfa
,
7889 1, 1, 2, EXTF_PAIRED
, MEXT_PRIV(m
), m
);
7893 m_scratch_init(struct mbuf
*m
)
7895 struct pkthdr
*pkt
= &m
->m_pkthdr
;
7897 VERIFY(m
->m_flags
& M_PKTHDR
);
7899 /* See comments in <rdar://problem/14040693> */
7900 if (pkt
->pkt_flags
& PKTF_PRIV_GUARDED
) {
7901 panic_plain("Invalid attempt to modify guarded module-private "
7902 "area: mbuf %p, pkt_flags 0x%x\n", m
, pkt
->pkt_flags
);
7906 bzero(&pkt
->pkt_mpriv
, sizeof (pkt
->pkt_mpriv
));
7910 * This routine is reserved for mbuf_get_driver_scratch(); clients inside
7911 * xnu that intend on utilizing the module-private area should directly
7912 * refer to the pkt_mpriv structure in the pkthdr. They are also expected
7913 * to set and clear PKTF_PRIV_GUARDED, while owning the packet and prior
7914 * to handing it off to another module, respectively.
7917 m_scratch_get(struct mbuf
*m
, u_int8_t
**p
)
7919 struct pkthdr
*pkt
= &m
->m_pkthdr
;
7921 VERIFY(m
->m_flags
& M_PKTHDR
);
7923 /* See comments in <rdar://problem/14040693> */
7924 if (pkt
->pkt_flags
& PKTF_PRIV_GUARDED
) {
7925 panic_plain("Invalid attempt to access guarded module-private "
7926 "area: mbuf %p, pkt_flags 0x%x\n", m
, pkt
->pkt_flags
);
7931 mcache_audit_t
*mca
;
7933 lck_mtx_lock(mbuf_mlock
);
7934 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
7935 if (mca
->mca_uflags
& MB_SCVALID
)
7936 mcl_audit_scratch(mca
);
7937 lck_mtx_unlock(mbuf_mlock
);
7940 *p
= (u_int8_t
*)&pkt
->pkt_mpriv
;
7941 return (sizeof (pkt
->pkt_mpriv
));
7945 m_redzone_init(struct mbuf
*m
)
7947 VERIFY(m
->m_flags
& M_PKTHDR
);
7949 * Each mbuf has a unique red zone pattern, which is a XOR
7950 * of the red zone cookie and the address of the mbuf.
7952 m
->m_pkthdr
.redzone
= ((u_int32_t
)(uintptr_t)m
) ^ mb_redzone_cookie
;
7956 m_redzone_verify(struct mbuf
*m
)
7958 u_int32_t mb_redzone
;
7960 VERIFY(m
->m_flags
& M_PKTHDR
);
7962 mb_redzone
= ((u_int32_t
)(uintptr_t)m
) ^ mb_redzone_cookie
;
7963 if (m
->m_pkthdr
.redzone
!= mb_redzone
) {
7964 panic("mbuf %p redzone violation with value 0x%x "
7965 "(instead of 0x%x, using cookie 0x%x)\n",
7966 m
, m
->m_pkthdr
.redzone
, mb_redzone
, mb_redzone_cookie
);
7971 __private_extern__
inline void
7972 m_set_ext(struct mbuf
*m
, struct ext_ref
*rfa
, m_ext_free_func_t ext_free
,
7975 VERIFY(m
->m_flags
& M_EXT
);
7977 m
->m_ext
.ext_refflags
=
7978 (struct ext_ref
*)(((uintptr_t)rfa
) ^ mb_obscure_extref
);
7979 if (ext_free
!= NULL
) {
7980 rfa
->ext_token
= ((uintptr_t)&rfa
->ext_token
) ^
7982 m
->m_ext
.ext_free
= (m_ext_free_func_t
)
7983 (((uintptr_t)ext_free
) ^ rfa
->ext_token
);
7984 if (ext_arg
!= NULL
) {
7986 (caddr_t
)(((uintptr_t)ext_arg
) ^ rfa
->ext_token
);
7988 m
->m_ext
.ext_arg
= NULL
;
7992 m
->m_ext
.ext_free
= NULL
;
7993 m
->m_ext
.ext_arg
= NULL
;
7997 * If we are going to loose the cookie in ext_token by
7998 * resetting the rfa, we should use the global cookie
7999 * to obscure the ext_free and ext_arg pointers.
8001 if (ext_free
!= NULL
) {
8003 (m_ext_free_func_t
)((uintptr_t)ext_free
^
8004 mb_obscure_extfree
);
8005 if (ext_arg
!= NULL
) {
8007 (caddr_t
)((uintptr_t)ext_arg
^
8008 mb_obscure_extfree
);
8010 m
->m_ext
.ext_arg
= NULL
;
8013 m
->m_ext
.ext_free
= NULL
;
8014 m
->m_ext
.ext_arg
= NULL
;
8016 m
->m_ext
.ext_refflags
= NULL
;
8020 __private_extern__
inline struct ext_ref
*
8021 m_get_rfa(struct mbuf
*m
)
8023 if (m
->m_ext
.ext_refflags
== NULL
)
8026 return ((struct ext_ref
*)(((uintptr_t)m
->m_ext
.ext_refflags
) ^ mb_obscure_extref
));
8029 __private_extern__
inline m_ext_free_func_t
8030 m_get_ext_free(struct mbuf
*m
)
8032 struct ext_ref
*rfa
;
8033 if (m
->m_ext
.ext_free
== NULL
)
8038 return ((m_ext_free_func_t
)((uintptr_t)m
->m_ext
.ext_free
^ mb_obscure_extfree
));
8040 return ((m_ext_free_func_t
)(((uintptr_t)m
->m_ext
.ext_free
)
8044 __private_extern__
inline caddr_t
8045 m_get_ext_arg(struct mbuf
*m
)
8047 struct ext_ref
*rfa
;
8048 if (m
->m_ext
.ext_arg
== NULL
)
8053 return ((caddr_t
)((uintptr_t)m
->m_ext
.ext_arg
^ mb_obscure_extfree
));
8055 return ((caddr_t
)(((uintptr_t)m
->m_ext
.ext_arg
) ^
8061 * Send a report of mbuf usage if the usage is at least 6% of max limit
8062 * or if there has been at least 3% increase since the last report.
8064 * The values 6% and 3% are chosen so that we can do simple arithmetic
8065 * with shift operations.
8068 mbuf_report_usage(mbuf_class_t cl
)
8070 /* if a report is already in progress, nothing to do */
8071 if (mb_peak_newreport
)
8074 if (m_total(cl
) > m_peak(cl
) &&
8075 m_total(cl
) >= (m_maxlimit(cl
) >> 4) &&
8076 (m_total(cl
) - m_peak(cl
)) >= (m_peak(cl
) >> 5))
8081 __private_extern__
void
8082 mbuf_report_peak_usage(void)
8086 struct nstat_sysinfo_data ns_data
;
8087 uint32_t memreleased
= 0;
8088 static uint32_t prevmemreleased
;
8090 uptime
= net_uptime();
8091 lck_mtx_lock(mbuf_mlock
);
8093 /* Generate an initial report after 1 week of uptime */
8094 if (!mb_peak_firstreport
&&
8095 uptime
> MBUF_PEAK_FIRST_REPORT_THRESHOLD
) {
8096 mb_peak_newreport
= TRUE
;
8097 mb_peak_firstreport
= TRUE
;
8100 if (!mb_peak_newreport
) {
8101 lck_mtx_unlock(mbuf_mlock
);
8106 * Since a report is being generated before 1 week,
8107 * we do not need to force another one later
8109 if (uptime
< MBUF_PEAK_FIRST_REPORT_THRESHOLD
)
8110 mb_peak_firstreport
= TRUE
;
8112 for (i
= 0; i
< NELEM(mbuf_table
); i
++) {
8113 m_peak(m_class(i
)) = m_total(m_class(i
));
8114 memreleased
+= m_release_cnt(i
);
8116 memreleased
= memreleased
- prevmemreleased
;
8117 prevmemreleased
= memreleased
;
8118 mb_peak_newreport
= FALSE
;
8119 lck_mtx_unlock(mbuf_mlock
);
8121 bzero(&ns_data
, sizeof(ns_data
));
8122 ns_data
.flags
= NSTAT_SYSINFO_MBUF_STATS
;
8123 ns_data
.u
.mb_stats
.total_256b
= m_peak(MC_MBUF
);
8124 ns_data
.u
.mb_stats
.total_2kb
= m_peak(MC_CL
);
8125 ns_data
.u
.mb_stats
.total_4kb
= m_peak(MC_BIGCL
);
8126 ns_data
.u
.mb_stats
.total_16kb
= m_peak(MC_16KCL
);
8127 ns_data
.u
.mb_stats
.sbmb_total
= total_sbmb_cnt_peak
;
8128 ns_data
.u
.mb_stats
.sb_atmbuflimit
= sbmb_limreached
;
8129 ns_data
.u
.mb_stats
.draincnt
= mbstat
.m_drain
;
8130 ns_data
.u
.mb_stats
.memreleased
= memreleased
;
8131 ns_data
.u
.mb_stats
.sbmb_floor
= total_sbmb_cnt_floor
;
8133 nstat_sysinfo_send_data(&ns_data
);
8136 * Reset the floor whenever we report a new
8137 * peak to track the trend (increase peek usage
8138 * is not a leak if mbufs get released
8139 * between reports and the floor stays low)
8141 total_sbmb_cnt_floor
= total_sbmb_cnt_peak
;
8145 * Simple routine to avoid taking the lock when we can't run the
8149 mbuf_drain_checks(boolean_t ignore_waiters
)
8152 if (mb_drain_maxint
== 0)
8154 if (!ignore_waiters
&& mb_waiters
!= 0)
8161 * Called by the VM when there's memory pressure or when we exhausted
8162 * the 4k/16k reserved space.
8165 mbuf_drain_locked(boolean_t ignore_waiters
)
8168 mcl_slab_t
*sp
, *sp_tmp
, *nsp
;
8169 unsigned int num
, k
, interval
, released
= 0;
8170 unsigned long total_mem
= 0, use_mem
= 0;
8171 boolean_t ret
, purge_caches
= FALSE
;
8175 static unsigned char scratch
[32];
8176 static ppnum_t scratch_pa
= 0;
8178 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
8179 if (!mbuf_drain_checks(ignore_waiters
))
8181 if (scratch_pa
== 0) {
8182 bzero(scratch
, sizeof(scratch
));
8183 scratch_pa
= pmap_find_phys(kernel_pmap
, (addr64_t
)scratch
);
8185 } else if (mclverify
) {
8187 * Panic if a driver wrote to our scratch memory.
8189 for (k
= 0; k
< sizeof(scratch
); k
++)
8191 panic("suspect DMA to freed address");
8194 * Don't free memory too often as that could cause excessive
8195 * waiting times for mbufs. Purge caches if we were asked to drain
8196 * in the last 5 minutes.
8198 if (mbuf_drain_last_runtime
!= 0) {
8199 interval
= net_uptime() - mbuf_drain_last_runtime
;
8200 if (interval
<= mb_drain_maxint
) {
8203 if (interval
<= mb_drain_maxint
* 5)
8204 purge_caches
= TRUE
;
8206 mbuf_drain_last_runtime
= net_uptime();
8208 * Don't free any memory if we're using 60% or more.
8210 for (mc
= 0; mc
< NELEM(mbuf_table
); mc
++) {
8211 total_mem
+= m_total(mc
) * m_maxsize(mc
);
8212 use_mem
+= m_active(mc
) * m_maxsize(mc
);
8214 per
= (use_mem
* 100) / total_mem
;
8219 * Purge all the caches. This effectively disables
8220 * caching for a few seconds, but the mbuf worker thread will
8221 * re-enable them again.
8223 if (purge_caches
== TRUE
)
8224 for (mc
= 0; mc
< NELEM(mbuf_table
); mc
++) {
8225 if (m_total(mc
) < m_avgtotal(mc
))
8227 lck_mtx_unlock(mbuf_mlock
);
8228 ret
= mcache_purge_cache(m_cache(mc
), FALSE
);
8229 lck_mtx_lock(mbuf_mlock
);
8234 * Move the objects from the composite class freelist to
8235 * the rudimentary slabs list, but keep at least 10% of the average
8236 * total in the freelist.
8238 for (mc
= 0; mc
< NELEM(mbuf_table
); mc
++) {
8239 while (m_cobjlist(mc
) &&
8240 m_total(mc
) < m_avgtotal(mc
) &&
8241 m_infree(mc
) > 0.1 * m_avgtotal(mc
) + m_minlimit(mc
)) {
8242 obj
= m_cobjlist(mc
);
8243 m_cobjlist(mc
) = obj
->obj_next
;
8244 obj
->obj_next
= NULL
;
8245 num
= cslab_free(mc
, obj
, 1);
8249 /* cslab_free() handles m_total */
8253 * Free the buffers present in the slab list up to 10% of the total
8254 * average per class.
8256 * We walk the list backwards in an attempt to reduce fragmentation.
8258 for (mc
= NELEM(mbuf_table
) - 1; (int)mc
>= 0; mc
--) {
8259 TAILQ_FOREACH_SAFE(sp
, &m_slablist(mc
), sl_link
, sp_tmp
) {
8261 * Process only unused slabs occupying memory.
8263 if (sp
->sl_refcnt
!= 0 || sp
->sl_len
== 0 ||
8264 sp
->sl_base
== NULL
)
8266 if (m_total(mc
) < m_avgtotal(mc
) ||
8267 m_infree(mc
) < 0.1 * m_avgtotal(mc
) + m_minlimit(mc
))
8269 slab_remove(sp
, mc
);
8272 m_infree(mc
) -= NMBPG
;
8273 m_total(mc
) -= NMBPG
;
8274 if (mclaudit
!= NULL
)
8275 mcl_audit_free(sp
->sl_base
, NMBPG
);
8278 m_infree(mc
) -= NCLPG
;
8279 m_total(mc
) -= NCLPG
;
8280 if (mclaudit
!= NULL
)
8281 mcl_audit_free(sp
->sl_base
, NMBPG
);
8285 m_infree(mc
) -= NBCLPG
;
8286 m_total(mc
) -= NBCLPG
;
8287 if (mclaudit
!= NULL
)
8288 mcl_audit_free(sp
->sl_base
, NMBPG
);
8294 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
8296 VERIFY(nsp
->sl_refcnt
== 0 &&
8297 nsp
->sl_base
!= NULL
&&
8299 slab_init(nsp
, 0, 0, NULL
, NULL
, 0, 0,
8303 if (mclaudit
!= NULL
) {
8304 if (sp
->sl_len
== PAGE_SIZE
) {
8305 mcl_audit_free(sp
->sl_base
,
8308 mcl_audit_free(sp
->sl_base
, 1);
8314 * The composite classes have their own
8315 * freelist (m_cobjlist), so we only
8316 * process rudimentary classes here.
8320 m_release_cnt(mc
) += m_size(mc
);
8321 released
+= m_size(mc
);
8322 VERIFY(sp
->sl_base
!= NULL
&&
8323 sp
->sl_len
>= PAGE_SIZE
);
8324 offset
= MTOPG(sp
->sl_base
);
8326 * Make sure the IOMapper points to a valid, but
8327 * bogus, address. This should prevent further DMA
8328 * accesses to freed memory.
8330 IOMapperInsertPage(mcl_paddr_base
, offset
, scratch_pa
);
8331 mcl_paddr
[offset
] = 0;
8332 kmem_free(mb_map
, (vm_offset_t
)sp
->sl_base
,
8334 slab_init(sp
, 0, 0, NULL
, NULL
, 0, 0, 0);
8339 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
8340 mbstat
.m_clusters
= m_total(MC_CL
);
8341 mbstat
.m_mbufs
= m_total(MC_MBUF
);
8343 mbuf_mtypes_sync(TRUE
);
8346 __private_extern__
void
8347 mbuf_drain(boolean_t ignore_waiters
)
8349 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_NOTOWNED
);
8350 if (!mbuf_drain_checks(ignore_waiters
))
8352 lck_mtx_lock(mbuf_mlock
);
8353 mbuf_drain_locked(ignore_waiters
);
8354 lck_mtx_unlock(mbuf_mlock
);
8359 m_drain_force_sysctl SYSCTL_HANDLER_ARGS
8361 #pragma unused(arg1, arg2)
8364 err
= sysctl_handle_int(oidp
, &val
, 0, req
);
8365 if (err
!= 0 || req
->newptr
== USER_ADDR_NULL
)
8374 #if DEBUG || DEVELOPMENT
8376 _mbwdog_logger(const char *func
, const int line
, const char *fmt
, ...)
8380 char str
[384], p
[256];
8383 LCK_MTX_ASSERT(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
8384 if (mbwdog_logging
== NULL
) {
8385 mbwdog_logging
= _MALLOC(mbwdog_logging_size
,
8386 M_TEMP
, M_ZERO
|M_NOWAIT
);
8387 if (mbwdog_logging
== NULL
)
8391 vsnprintf(p
, sizeof(p
), fmt
, ap
);
8394 len
= snprintf(str
, sizeof(str
),
8395 "\n%ld.%d (%d/%llx) %s:%d %s",
8396 now
.tv_sec
, now
.tv_usec
,
8397 current_proc()->p_pid
,
8398 (uint64_t)VM_KERNEL_ADDRPERM(current_thread()),
8402 if (mbwdog_logging_used
+ len
> mbwdog_logging_size
) {
8403 mbwdog_logging_used
= mbwdog_logging_used
/ 2;
8404 memmove(mbwdog_logging
, mbwdog_logging
+ mbwdog_logging_used
,
8405 mbwdog_logging_size
- mbwdog_logging_used
);
8406 mbwdog_logging
[mbwdog_logging_used
] = 0;
8408 strlcat(mbwdog_logging
, str
, mbwdog_logging_size
);
8409 mbwdog_logging_used
+= len
;
8413 sysctl_mbwdog_log SYSCTL_HANDLER_ARGS
8415 #pragma unused(oidp, arg1, arg2)
8416 return SYSCTL_OUT(req
, mbwdog_logging
, mbwdog_logging_used
);
8418 SYSCTL_DECL(_kern_ipc
);
8419 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mbwdog_log
,
8420 CTLTYPE_STRING
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
8421 0, 0, sysctl_mbwdog_log
, "A", "");
8423 static int mbtest_val
;
8424 static int mbtest_running
;
8426 static void mbtest_thread(__unused
void *arg
)
8430 int iterations
= 250;
8431 int allocations
= nmbclusters
;
8432 iterations
= iterations
/ scale_down
;
8433 allocations
= allocations
/ scale_down
;
8434 printf("%s thread starting\n", __func__
);
8435 for (i
= 0; i
< iterations
; i
++) {
8436 unsigned int needed
= allocations
;
8437 struct mbuf
*m1
, *m2
, *m3
;
8440 needed
= allocations
;
8441 m3
= m_getpackets_internal(&needed
, 0, M_DONTWAIT
, 0, M16KCLBYTES
);
8445 needed
= allocations
;
8446 m2
= m_getpackets_internal(&needed
, 0, M_DONTWAIT
, 0, MBIGCLBYTES
);
8449 m1
= m_getpackets_internal(&needed
, 0, M_DONTWAIT
, 0, MCLBYTES
);
8453 printf("%s thread ending\n", __func__
);
8455 OSDecrementAtomic(&mbtest_running
);
8456 wakeup_one((caddr_t
)&mbtest_running
);
8459 static void sysctl_mbtest(void)
8461 /* We launch three threads - wait for all of them */
8462 OSIncrementAtomic(&mbtest_running
);
8463 OSIncrementAtomic(&mbtest_running
);
8464 OSIncrementAtomic(&mbtest_running
);
8466 thread_call_func_delayed((thread_call_func_t
)mbtest_thread
, NULL
, 10);
8467 thread_call_func_delayed((thread_call_func_t
)mbtest_thread
, NULL
, 10);
8468 thread_call_func_delayed((thread_call_func_t
)mbtest_thread
, NULL
, 10);
8470 while (mbtest_running
) {
8471 msleep((caddr_t
)&mbtest_running
, NULL
, PUSER
, "mbtest_running", NULL
);
8476 mbtest SYSCTL_HANDLER_ARGS
8478 #pragma unused(arg1, arg2)
8479 int error
= 0, val
, oldval
= mbtest_val
;
8482 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
8483 if (error
|| !req
->newptr
)
8493 #endif // DEBUG || DEVELOPMENT
8496 mtracelarge_register(size_t size
)
8499 struct mtracelarge
*trace
;
8500 uintptr_t bt
[MLEAK_STACK_DEPTH
];
8503 depth
= backtrace(bt
, MLEAK_STACK_DEPTH
);
8504 /* Check if this entry is already on the list. */
8505 for (i
= 0; i
< MTRACELARGE_NUM_TRACES
; i
++) {
8506 trace
= &mtracelarge_table
[i
];
8507 if (trace
->size
== size
&& trace
->depth
== depth
&&
8508 memcmp(bt
, trace
->addr
, depth
* sizeof(uintptr_t)) == 0) {
8513 for (i
= 0; i
< MTRACELARGE_NUM_TRACES
; i
++) {
8514 trace
= &mtracelarge_table
[i
];
8515 if (size
> trace
->size
) {
8516 trace
->depth
= depth
;
8517 memcpy(trace
->addr
, bt
, depth
* sizeof(uintptr_t));
8524 SYSCTL_DECL(_kern_ipc
);
8525 #if DEBUG || DEVELOPMENT
8526 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mbtest
,
8527 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &mbtest_val
, 0, &mbtest
, "I",
8528 "Toggle to test mbufs");
8530 SYSCTL_PROC(_kern_ipc
, KIPC_MBSTAT
, mbstat
,
8531 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
8532 0, 0, mbstat_sysctl
, "S,mbstat", "");
8533 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mb_stat
,
8534 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
8535 0, 0, mb_stat_sysctl
, "S,mb_stat", "");
8536 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mleak_top_trace
,
8537 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
8538 0, 0, mleak_top_trace_sysctl
, "S,mb_top_trace", "");
8539 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mleak_table
,
8540 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
8541 0, 0, mleak_table_sysctl
, "S,mleak_table", "");
8542 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mleak_sample_factor
,
8543 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mleak_table
.mleak_sample_factor
, 0, "");
8544 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_normalized
,
8545 CTLFLAG_RD
| CTLFLAG_LOCKED
, &mb_normalized
, 0, "");
8546 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_watchdog
,
8547 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mb_watchdog
, 0, "");
8548 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mb_drain_force
,
8549 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, NULL
, 0,
8550 m_drain_force_sysctl
, "I",
8551 "Forces the mbuf garbage collection to run");
8552 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_drain_maxint
,
8553 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mb_drain_maxint
, 0,
8554 "Minimum time interval between garbage collection");