2 * Copyright (c) 1998-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1988, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
64 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
65 * support for mandatory and extensible security protections. This notice
66 * is included in support of clause 2.2 (b) of the Apple Public License,
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
74 #include <sys/kernel.h>
75 #include <sys/sysctl.h>
76 #include <sys/syslog.h>
77 #include <sys/protosw.h>
78 #include <sys/domain.h>
79 #include <sys/queue.h>
82 #include <dev/random/randomdev.h>
84 #include <kern/kern_types.h>
85 #include <kern/simple_lock.h>
86 #include <kern/queue.h>
87 #include <kern/sched_prim.h>
88 #include <kern/cpu_number.h>
89 #include <kern/zalloc.h>
91 #include <libkern/OSAtomic.h>
92 #include <libkern/OSDebug.h>
93 #include <libkern/libkern.h>
95 #include <IOKit/IOMapper.h>
97 #include <machine/limits.h>
98 #include <machine/machine_routines.h>
101 #include <security/mac_framework.h>
104 #include <sys/mcache.h>
105 #include <net/ntstat.h>
108 * MBUF IMPLEMENTATION NOTES.
110 * There is a total of 5 per-CPU caches:
113 * This is a cache of rudimentary objects of MSIZE in size; each
114 * object represents an mbuf structure. This cache preserves only
115 * the m_type field of the mbuf during its transactions.
118 * This is a cache of rudimentary objects of MCLBYTES in size; each
119 * object represents a mcluster structure. This cache does not
120 * preserve the contents of the objects during its transactions.
123 * This is a cache of rudimentary objects of MBIGCLBYTES in size; each
124 * object represents a mbigcluster structure. This cache does not
125 * preserve the contents of the objects during its transaction.
128 * This is a cache of mbufs each having a cluster attached to it.
129 * It is backed by MC_MBUF and MC_CL rudimentary caches. Several
130 * fields of the mbuf related to the external cluster are preserved
131 * during transactions.
134 * This is a cache of mbufs each having a big cluster attached to it.
135 * It is backed by MC_MBUF and MC_BIGCL rudimentary caches. Several
136 * fields of the mbuf related to the external cluster are preserved
137 * during transactions.
141 * Allocation requests are handled first at the per-CPU (mcache) layer
142 * before falling back to the slab layer. Performance is optimal when
143 * the request is satisfied at the CPU layer because global data/lock
144 * never gets accessed. When the slab layer is entered for allocation,
145 * the slab freelist will be checked first for available objects before
146 * the VM backing store is invoked. Slab layer operations are serialized
147 * for all of the caches as the mbuf global lock is held most of the time.
148 * Allocation paths are different depending on the class of objects:
150 * a. Rudimentary object:
152 * { m_get_common(), m_clattach(), m_mclget(),
153 * m_mclalloc(), m_bigalloc(), m_copym_with_hdrs(),
154 * composite object allocation }
157 * | +-----------------------+
159 * mcache_alloc/mcache_alloc_ext() mbuf_slab_audit()
162 * [CPU cache] -------> (found?) -------+
165 * mbuf_slab_alloc() |
168 * +---------> [freelist] -------> (found?) -------+
174 * +---<<---- kmem_mb_alloc()
176 * b. Composite object:
178 * { m_getpackets_internal(), m_allocpacket_internal() }
181 * | +------ (done) ---------+
183 * mcache_alloc/mcache_alloc_ext() mbuf_cslab_audit()
186 * [CPU cache] -------> (found?) -------+
189 * mbuf_cslab_alloc() |
192 * [freelist] -------> (found?) -------+
195 * (rudimentary object) |
196 * mcache_alloc/mcache_alloc_ext() ------>>-----+
198 * Auditing notes: If auditing is enabled, buffers will be subjected to
199 * integrity checks by the audit routine. This is done by verifying their
200 * contents against DEADBEEF (free) pattern before returning them to caller.
201 * As part of this step, the routine will also record the transaction and
202 * pattern-fill the buffers with BADDCAFE (uninitialized) pattern. It will
203 * also restore any constructed data structure fields if necessary.
205 * OBJECT DEALLOCATION:
207 * Freeing an object simply involves placing it into the CPU cache; this
208 * pollutes the cache to benefit subsequent allocations. The slab layer
209 * will only be entered if the object is to be purged out of the cache.
210 * During normal operations, this happens only when the CPU layer resizes
211 * its bucket while it's adjusting to the allocation load. Deallocation
212 * paths are different depending on the class of objects:
214 * a. Rudimentary object:
216 * { m_free(), m_freem_list(), composite object deallocation }
219 * | +------ (done) ---------+
221 * mcache_free/mcache_free_ext() |
224 * mbuf_slab_audit() |
227 * [CPU cache] ---> (not purging?) -----+
233 * [freelist] ----------->>------------+
234 * (objects get purged to VM only on demand)
236 * b. Composite object:
238 * { m_free(), m_freem_list() }
241 * | +------ (done) ---------+
243 * mcache_free/mcache_free_ext() |
246 * mbuf_cslab_audit() |
249 * [CPU cache] ---> (not purging?) -----+
252 * mbuf_cslab_free() |
255 * [freelist] ---> (not purging?) -----+
258 * (rudimentary object) |
259 * mcache_free/mcache_free_ext() ------->>------+
261 * Auditing notes: If auditing is enabled, the audit routine will save
262 * any constructed data structure fields (if necessary) before filling the
263 * contents of the buffers with DEADBEEF (free) pattern and recording the
264 * transaction. Buffers that are freed (whether at CPU or slab layer) are
265 * expected to contain the free pattern.
269 * Debugging can be enabled by adding "mbuf_debug=0x3" to boot-args; this
270 * translates to the mcache flags (MCF_VERIFY | MCF_AUDIT). Additionally,
271 * the CPU layer cache can be disabled by setting the MCF_NOCPUCACHE flag,
272 * i.e. modify the boot argument parameter to "mbuf_debug=0x13". Leak
273 * detection may also be disabled by setting the MCF_NOLEAKLOG flag, e.g.
274 * "mbuf_debug=0x113". Note that debugging consumes more CPU and memory.
276 * Each object is associated with exactly one mcache_audit_t structure that
277 * contains the information related to its last buffer transaction. Given
278 * an address of an object, the audit structure can be retrieved by finding
279 * the position of the object relevant to the base address of the cluster:
281 * +------------+ +=============+
282 * | mbuf addr | | mclaudit[i] |
283 * +------------+ +=============+
285 * i = MTOBG(addr) +-------------+
286 * | +-----> | cl_audit[1] | -----> mcache_audit_t
287 * b = BGTOM(i) | +-------------+
289 * x = MCLIDX(b, addr) | +-------------+
290 * | | | cl_audit[7] |
291 * +-----------------+ +-------------+
294 * The mclaudit[] array is allocated at initialization time, but its contents
295 * get populated when the corresponding cluster is created. Because a page
296 * can be turned into NMBPG number of mbufs, we preserve enough space for the
297 * mbufs so that there is a 1-to-1 mapping between them. A page that never
298 * gets (or has not yet) turned into mbufs will use only cl_audit[0] with the
299 * remaining entries unused. For 16KB cluster, only one entry from the first
300 * page is allocated and used for the entire object.
303 /* TODO: should be in header file */
304 /* kernel translater */
305 extern vm_offset_t
kmem_mb_alloc(vm_map_t
, int, int);
306 extern ppnum_t
pmap_find_phys(pmap_t pmap
, addr64_t va
);
307 extern vm_map_t mb_map
; /* special map */
310 decl_lck_mtx_data(static, mbuf_mlock_data
);
311 static lck_mtx_t
*mbuf_mlock
= &mbuf_mlock_data
;
312 static lck_attr_t
*mbuf_mlock_attr
;
313 static lck_grp_t
*mbuf_mlock_grp
;
314 static lck_grp_attr_t
*mbuf_mlock_grp_attr
;
316 /* Back-end (common) layer */
317 static void *mbuf_worker_run
; /* wait channel for worker thread */
318 static int mbuf_worker_ready
; /* worker thread is runnable */
319 static int mbuf_expand_mcl
; /* number of cluster creation requets */
320 static int mbuf_expand_big
; /* number of big cluster creation requests */
321 static int mbuf_expand_16k
; /* number of 16KB cluster creation requests */
322 static int ncpu
; /* number of CPUs */
323 static ppnum_t
*mcl_paddr
; /* Array of cluster physical addresses */
324 static ppnum_t mcl_pages
; /* Size of array (# physical pages) */
325 static ppnum_t mcl_paddr_base
; /* Handle returned by IOMapper::iovmAlloc() */
326 static mcache_t
*ref_cache
; /* Cache of cluster reference & flags */
327 static mcache_t
*mcl_audit_con_cache
; /* Audit contents cache */
328 static unsigned int mbuf_debug
; /* patchable mbuf mcache flags */
329 static unsigned int mb_normalized
; /* number of packets "normalized" */
331 #define MB_GROWTH_AGGRESSIVE 1 /* Threshold: 1/2 of total */
332 #define MB_GROWTH_NORMAL 2 /* Threshold: 3/4 of total */
335 MC_MBUF
= 0, /* Regular mbuf */
337 MC_BIGCL
, /* Large (4KB) cluster */
338 MC_16KCL
, /* Jumbo (16KB) cluster */
339 MC_MBUF_CL
, /* mbuf + cluster */
340 MC_MBUF_BIGCL
, /* mbuf + large (4KB) cluster */
341 MC_MBUF_16KCL
/* mbuf + jumbo (16KB) cluster */
344 #define MBUF_CLASS_MIN MC_MBUF
345 #define MBUF_CLASS_MAX MC_MBUF_16KCL
346 #define MBUF_CLASS_LAST MC_16KCL
347 #define MBUF_CLASS_VALID(c) \
348 ((int)(c) >= MBUF_CLASS_MIN && (int)(c) <= MBUF_CLASS_MAX)
349 #define MBUF_CLASS_COMPOSITE(c) \
350 ((int)(c) > MBUF_CLASS_LAST)
354 * mbuf specific mcache allocation request flags.
356 #define MCR_COMP MCR_USR1 /* for MC_MBUF_{CL,BIGCL,16KCL} caches */
359 * Per-cluster slab structure.
361 * A slab is a cluster control structure that contains one or more object
362 * chunks; the available chunks are chained in the slab's freelist (sl_head).
363 * Each time a chunk is taken out of the slab, the slab's reference count
364 * gets incremented. When all chunks have been taken out, the empty slab
365 * gets removed (SLF_DETACHED) from the class's slab list. A chunk that is
366 * returned to a slab causes the slab's reference count to be decremented;
367 * it also causes the slab to be reinserted back to class's slab list, if
368 * it's not already done.
370 * Compartmentalizing of the object chunks into slabs allows us to easily
371 * merge one or more slabs together when the adjacent slabs are idle, as
372 * well as to convert or move a slab from one class to another; e.g. the
373 * mbuf cluster slab can be converted to a regular cluster slab when all
374 * mbufs in the slab have been freed.
376 * A slab may also span across multiple clusters for chunks larger than
377 * a cluster's size. In this case, only the slab of the first cluster is
378 * used. The rest of the slabs are marked with SLF_PARTIAL to indicate
379 * that they are part of the larger slab.
381 * Each slab controls a page of memory.
383 typedef struct mcl_slab
{
384 struct mcl_slab
*sl_next
; /* neighboring slab */
385 u_int8_t sl_class
; /* controlling mbuf class */
386 int8_t sl_refcnt
; /* outstanding allocations */
387 int8_t sl_chunks
; /* chunks (bufs) in this slab */
388 u_int16_t sl_flags
; /* slab flags (see below) */
389 u_int16_t sl_len
; /* slab length */
390 void *sl_base
; /* base of allocated memory */
391 void *sl_head
; /* first free buffer */
392 TAILQ_ENTRY(mcl_slab
) sl_link
; /* next/prev slab on freelist */
395 #define SLF_MAPPED 0x0001 /* backed by a mapped page */
396 #define SLF_PARTIAL 0x0002 /* part of another slab */
397 #define SLF_DETACHED 0x0004 /* not in slab freelist */
400 * The array of slabs are broken into groups of arrays per 1MB of kernel
401 * memory to reduce the footprint. Each group is allocated on demand
402 * whenever a new piece of memory mapped in from the VM crosses the 1MB
405 #define NSLABSPMB ((1 << MBSHIFT) >> PAGE_SHIFT)
407 typedef struct mcl_slabg
{
408 mcl_slab_t
*slg_slab
; /* group of slabs */
412 * Number of slabs needed to control a 16KB cluster object.
414 #define NSLABSP16KB (M16KCLBYTES >> PAGE_SHIFT)
417 * Per-cluster audit structure.
420 mcache_audit_t
**cl_audit
; /* array of audits */
424 struct thread
*msa_thread
; /* thread doing transaction */
425 struct thread
*msa_pthread
; /* previous transaction thread */
426 uint32_t msa_tstamp
; /* transaction timestamp (ms) */
427 uint32_t msa_ptstamp
; /* prev transaction timestamp (ms) */
428 uint16_t msa_depth
; /* pc stack depth */
429 uint16_t msa_pdepth
; /* previous transaction pc stack */
430 void *msa_stack
[MCACHE_STACK_DEPTH
];
431 void *msa_pstack
[MCACHE_STACK_DEPTH
];
432 } mcl_scratch_audit_t
;
436 * Size of data from the beginning of an mbuf that covers m_hdr,
437 * pkthdr and m_ext structures. If auditing is enabled, we allocate
438 * a shadow mbuf structure of this size inside each audit structure,
439 * and the contents of the real mbuf gets copied into it when the mbuf
440 * is freed. This allows us to pattern-fill the mbuf for integrity
441 * check, and to preserve any constructed mbuf fields (e.g. mbuf +
442 * cluster cache case). Note that we don't save the contents of
443 * clusters when they are freed; we simply pattern-fill them.
445 u_int8_t sc_mbuf
[(MSIZE
- _MHLEN
) + sizeof (_m_ext_t
)];
446 mcl_scratch_audit_t sc_scratch
__attribute__((aligned(8)));
447 } mcl_saved_contents_t
;
449 #define AUDIT_CONTENTS_SIZE (sizeof (mcl_saved_contents_t))
451 #define MCA_SAVED_MBUF_PTR(_mca) \
452 ((struct mbuf *)(void *)((mcl_saved_contents_t *) \
453 (_mca)->mca_contents)->sc_mbuf)
454 #define MCA_SAVED_MBUF_SIZE \
455 (sizeof (((mcl_saved_contents_t *)0)->sc_mbuf))
456 #define MCA_SAVED_SCRATCH_PTR(_mca) \
457 (&((mcl_saved_contents_t *)(_mca)->mca_contents)->sc_scratch)
460 * mbuf specific mcache audit flags
462 #define MB_INUSE 0x01 /* object has not been returned to slab */
463 #define MB_COMP_INUSE 0x02 /* object has not been returned to cslab */
464 #define MB_SCVALID 0x04 /* object has valid saved contents */
467 * Each of the following two arrays hold up to nmbclusters elements.
469 static mcl_audit_t
*mclaudit
; /* array of cluster audit information */
470 static unsigned int maxclaudit
; /* max # of entries in audit table */
471 static mcl_slabg_t
**slabstbl
; /* cluster slabs table */
472 static unsigned int maxslabgrp
; /* max # of entries in slabs table */
473 static unsigned int slabgrp
; /* # of entries in slabs table */
476 int nclusters
; /* # of clusters for non-jumbo (legacy) sizes */
477 int njcl
; /* # of clusters for jumbo sizes */
478 int njclbytes
; /* size of a jumbo cluster */
479 unsigned char *mbutl
; /* first mapped cluster address */
480 unsigned char *embutl
; /* ending virtual address of mclusters */
481 int _max_linkhdr
; /* largest link-level header */
482 int _max_protohdr
; /* largest protocol header */
483 int max_hdr
; /* largest link+protocol header */
484 int max_datalen
; /* MHLEN - max_hdr */
486 static boolean_t mclverify
; /* debug: pattern-checking */
487 static boolean_t mcltrace
; /* debug: stack tracing */
488 static boolean_t mclfindleak
; /* debug: leak detection */
489 static boolean_t mclexpleak
; /* debug: expose leak info to user space */
491 static struct timeval mb_start
; /* beginning of time */
493 /* mbuf leak detection variables */
494 static struct mleak_table mleak_table
;
495 static mleak_stat_t
*mleak_stat
;
497 #define MLEAK_STAT_SIZE(n) \
498 ((size_t)(&((mleak_stat_t *)0)->ml_trace[n]))
501 mcache_obj_t
*element
; /* the alloc'ed element, NULL if unused */
502 u_int32_t trace_index
; /* mtrace index for corresponding backtrace */
503 u_int32_t count
; /* How many objects were requested */
504 u_int64_t hitcount
; /* for determining hash effectiveness */
508 u_int64_t collisions
;
512 uintptr_t addr
[MLEAK_STACK_DEPTH
];
515 /* Size must be a power of two for the zhash to be able to just mask off bits */
516 #define MLEAK_ALLOCATION_MAP_NUM 512
517 #define MLEAK_TRACE_MAP_NUM 256
520 * Sample factor for how often to record a trace. This is overwritable
521 * by the boot-arg mleak_sample_factor.
523 #define MLEAK_SAMPLE_FACTOR 500
526 * Number of top leakers recorded.
528 #define MLEAK_NUM_TRACES 5
530 #define MB_LEAK_SPACING_64 " "
531 #define MB_LEAK_SPACING_32 " "
534 #define MB_LEAK_HDR_32 "\n\
535 trace [1] trace [2] trace [3] trace [4] trace [5] \n\
536 ---------- ---------- ---------- ---------- ---------- \n\
539 #define MB_LEAK_HDR_64 "\n\
540 trace [1] trace [2] trace [3] \
541 trace [4] trace [5] \n\
542 ------------------ ------------------ ------------------ \
543 ------------------ ------------------ \n\
546 static uint32_t mleak_alloc_buckets
= MLEAK_ALLOCATION_MAP_NUM
;
547 static uint32_t mleak_trace_buckets
= MLEAK_TRACE_MAP_NUM
;
549 /* Hashmaps of allocations and their corresponding traces */
550 static struct mallocation
*mleak_allocations
;
551 static struct mtrace
*mleak_traces
;
552 static struct mtrace
*mleak_top_trace
[MLEAK_NUM_TRACES
];
554 /* Lock to protect mleak tables from concurrent modification */
555 decl_lck_mtx_data(static, mleak_lock_data
);
556 static lck_mtx_t
*mleak_lock
= &mleak_lock_data
;
557 static lck_attr_t
*mleak_lock_attr
;
558 static lck_grp_t
*mleak_lock_grp
;
559 static lck_grp_attr_t
*mleak_lock_grp_attr
;
561 extern u_int32_t high_sb_max
;
563 /* The minimum number of objects that are allocated, to start. */
565 #define MINBIGCL (MINCL >> 1)
566 #define MIN16KCL (MINCL >> 2)
568 /* Low watermarks (only map in pages once free counts go below) */
569 #define MBIGCL_LOWAT MINBIGCL
570 #define M16KCL_LOWAT MIN16KCL
573 mbuf_class_t mtbl_class
; /* class type */
574 mcache_t
*mtbl_cache
; /* mcache for this buffer class */
575 TAILQ_HEAD(mcl_slhead
, mcl_slab
) mtbl_slablist
; /* slab list */
576 mcache_obj_t
*mtbl_cobjlist
; /* composite objects freelist */
577 mb_class_stat_t
*mtbl_stats
; /* statistics fetchable via sysctl */
578 u_int32_t mtbl_maxsize
; /* maximum buffer size */
579 int mtbl_minlimit
; /* minimum allowed */
580 int mtbl_maxlimit
; /* maximum allowed */
581 u_int32_t mtbl_wantpurge
; /* purge during next reclaim */
582 uint32_t mtbl_avgtotal
; /* average total on iOS */
585 #define m_class(c) mbuf_table[c].mtbl_class
586 #define m_cache(c) mbuf_table[c].mtbl_cache
587 #define m_slablist(c) mbuf_table[c].mtbl_slablist
588 #define m_cobjlist(c) mbuf_table[c].mtbl_cobjlist
589 #define m_maxsize(c) mbuf_table[c].mtbl_maxsize
590 #define m_minlimit(c) mbuf_table[c].mtbl_minlimit
591 #define m_maxlimit(c) mbuf_table[c].mtbl_maxlimit
592 #define m_wantpurge(c) mbuf_table[c].mtbl_wantpurge
593 #define m_avgtotal(c) mbuf_table[c].mtbl_avgtotal
594 #define m_cname(c) mbuf_table[c].mtbl_stats->mbcl_cname
595 #define m_size(c) mbuf_table[c].mtbl_stats->mbcl_size
596 #define m_total(c) mbuf_table[c].mtbl_stats->mbcl_total
597 #define m_active(c) mbuf_table[c].mtbl_stats->mbcl_active
598 #define m_infree(c) mbuf_table[c].mtbl_stats->mbcl_infree
599 #define m_slab_cnt(c) mbuf_table[c].mtbl_stats->mbcl_slab_cnt
600 #define m_alloc_cnt(c) mbuf_table[c].mtbl_stats->mbcl_alloc_cnt
601 #define m_free_cnt(c) mbuf_table[c].mtbl_stats->mbcl_free_cnt
602 #define m_notified(c) mbuf_table[c].mtbl_stats->mbcl_notified
603 #define m_purge_cnt(c) mbuf_table[c].mtbl_stats->mbcl_purge_cnt
604 #define m_fail_cnt(c) mbuf_table[c].mtbl_stats->mbcl_fail_cnt
605 #define m_ctotal(c) mbuf_table[c].mtbl_stats->mbcl_ctotal
606 #define m_peak(c) mbuf_table[c].mtbl_stats->mbcl_peak_reported
607 #define m_release_cnt(c) mbuf_table[c].mtbl_stats->mbcl_release_cnt
609 static mbuf_table_t mbuf_table
[] = {
611 * The caches for mbufs, regular clusters and big clusters.
612 * The average total values were based on data gathered by actual
613 * usage patterns on iOS.
615 { MC_MBUF
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_MBUF
)),
616 NULL
, NULL
, 0, 0, 0, 0, 3000 },
617 { MC_CL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_CL
)),
618 NULL
, NULL
, 0, 0, 0, 0, 2000 },
619 { MC_BIGCL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_BIGCL
)),
620 NULL
, NULL
, 0, 0, 0, 0, 1000 },
621 { MC_16KCL
, NULL
, TAILQ_HEAD_INITIALIZER(m_slablist(MC_16KCL
)),
622 NULL
, NULL
, 0, 0, 0, 0, 1000 },
624 * The following are special caches; they serve as intermediate
625 * caches backed by the above rudimentary caches. Each object
626 * in the cache is an mbuf with a cluster attached to it. Unlike
627 * the above caches, these intermediate caches do not directly
628 * deal with the slab structures; instead, the constructed
629 * cached elements are simply stored in the freelists.
631 { MC_MBUF_CL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0, 2000 },
632 { MC_MBUF_BIGCL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0, 1000 },
633 { MC_MBUF_16KCL
, NULL
, { NULL
, NULL
}, NULL
, NULL
, 0, 0, 0, 0, 1000 },
636 #define NELEM(a) (sizeof (a) / sizeof ((a)[0]))
638 static void *mb_waitchan
= &mbuf_table
; /* wait channel for all caches */
639 static int mb_waiters
; /* number of waiters */
641 boolean_t mb_peak_newreport
= FALSE
;
642 boolean_t mb_peak_firstreport
= FALSE
;
644 /* generate a report by default after 1 week of uptime */
645 #define MBUF_PEAK_FIRST_REPORT_THRESHOLD 604800
647 #define MB_WDT_MAXTIME 10 /* # of secs before watchdog panic */
648 static struct timeval mb_wdtstart
; /* watchdog start timestamp */
649 static char *mbuf_dump_buf
;
651 #define MBUF_DUMP_BUF_SIZE 2048
654 * mbuf watchdog is enabled by default on embedded platforms. It is
655 * also toggeable via the kern.ipc.mb_watchdog sysctl.
656 * Garbage collection is also enabled by default on embedded platforms.
657 * mb_drain_maxint controls the amount of time to wait (in seconds) before
658 * consecutive calls to m_drain().
660 static unsigned int mb_watchdog
= 0;
661 static unsigned int mb_drain_maxint
= 0;
664 static u_int32_t mb_redzone_cookie
;
665 static void m_redzone_init(struct mbuf
*);
666 static void m_redzone_verify(struct mbuf
*m
);
668 /* The following are used to serialize m_clalloc() */
669 static boolean_t mb_clalloc_busy
;
670 static void *mb_clalloc_waitchan
= &mb_clalloc_busy
;
671 static int mb_clalloc_waiters
;
673 static void mbuf_mtypes_sync(boolean_t
);
674 static int mbstat_sysctl SYSCTL_HANDLER_ARGS
;
675 static void mbuf_stat_sync(void);
676 static int mb_stat_sysctl SYSCTL_HANDLER_ARGS
;
677 static int mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS
;
678 static int mleak_table_sysctl SYSCTL_HANDLER_ARGS
;
679 static char *mbuf_dump(void);
680 static void mbuf_table_init(void);
681 static inline void m_incref(struct mbuf
*);
682 static inline u_int32_t
m_decref(struct mbuf
*);
683 static int m_clalloc(const u_int32_t
, const int, const u_int32_t
);
684 static void mbuf_worker_thread_init(void);
685 static mcache_obj_t
*slab_alloc(mbuf_class_t
, int);
686 static void slab_free(mbuf_class_t
, mcache_obj_t
*);
687 static unsigned int mbuf_slab_alloc(void *, mcache_obj_t
***,
689 static void mbuf_slab_free(void *, mcache_obj_t
*, int);
690 static void mbuf_slab_audit(void *, mcache_obj_t
*, boolean_t
);
691 static void mbuf_slab_notify(void *, u_int32_t
);
692 static unsigned int cslab_alloc(mbuf_class_t
, mcache_obj_t
***,
694 static unsigned int cslab_free(mbuf_class_t
, mcache_obj_t
*, int);
695 static unsigned int mbuf_cslab_alloc(void *, mcache_obj_t
***,
697 static void mbuf_cslab_free(void *, mcache_obj_t
*, int);
698 static void mbuf_cslab_audit(void *, mcache_obj_t
*, boolean_t
);
699 static int freelist_populate(mbuf_class_t
, unsigned int, int);
700 static void freelist_init(mbuf_class_t
);
701 static boolean_t
mbuf_cached_above(mbuf_class_t
, int);
702 static boolean_t
mbuf_steal(mbuf_class_t
, unsigned int);
703 static void m_reclaim(mbuf_class_t
, unsigned int, boolean_t
);
704 static int m_howmany(int, size_t);
705 static void mbuf_worker_thread(void);
706 static void mbuf_watchdog(void);
707 static boolean_t
mbuf_sleep(mbuf_class_t
, unsigned int, int);
709 static void mcl_audit_init(void *, mcache_audit_t
**, mcache_obj_t
**,
710 size_t, unsigned int);
711 static void mcl_audit_free(void *, unsigned int);
712 static mcache_audit_t
*mcl_audit_buf2mca(mbuf_class_t
, mcache_obj_t
*);
713 static void mcl_audit_mbuf(mcache_audit_t
*, void *, boolean_t
, boolean_t
);
714 static void mcl_audit_cluster(mcache_audit_t
*, void *, size_t, boolean_t
,
716 static void mcl_audit_restore_mbuf(struct mbuf
*, mcache_audit_t
*, boolean_t
);
717 static void mcl_audit_save_mbuf(struct mbuf
*, mcache_audit_t
*);
718 static void mcl_audit_scratch(mcache_audit_t
*);
719 static void mcl_audit_mcheck_panic(struct mbuf
*);
720 static void mcl_audit_verify_nextptr(void *, mcache_audit_t
*);
722 static void mleak_activate(void);
723 static void mleak_logger(u_int32_t
, mcache_obj_t
*, boolean_t
);
724 static boolean_t
mleak_log(uintptr_t *, mcache_obj_t
*, uint32_t, int);
725 static void mleak_free(mcache_obj_t
*);
726 static void mleak_sort_traces(void);
727 static void mleak_update_stats(void);
729 static mcl_slab_t
*slab_get(void *);
730 static void slab_init(mcl_slab_t
*, mbuf_class_t
, u_int32_t
,
731 void *, void *, unsigned int, int, int);
732 static void slab_insert(mcl_slab_t
*, mbuf_class_t
);
733 static void slab_remove(mcl_slab_t
*, mbuf_class_t
);
734 static boolean_t
slab_inrange(mcl_slab_t
*, void *);
735 static void slab_nextptr_panic(mcl_slab_t
*, void *);
736 static void slab_detach(mcl_slab_t
*);
737 static boolean_t
slab_is_detached(mcl_slab_t
*);
739 static int m_copyback0(struct mbuf
**, int, int, const void *, int, int);
740 static struct mbuf
*m_split0(struct mbuf
*, int, int, int);
741 __private_extern__
void mbuf_report_peak_usage(void);
742 static boolean_t
mbuf_report_usage(mbuf_class_t
);
744 /* flags for m_copyback0 */
745 #define M_COPYBACK0_COPYBACK 0x0001 /* copyback from cp */
746 #define M_COPYBACK0_PRESERVE 0x0002 /* preserve original data */
747 #define M_COPYBACK0_COW 0x0004 /* do copy-on-write */
748 #define M_COPYBACK0_EXTEND 0x0008 /* extend chain */
751 * This flag is set for all mbufs that come out of and into the composite
752 * mbuf + cluster caches, i.e. MC_MBUF_CL and MC_MBUF_BIGCL. mbufs that
753 * are marked with such a flag have clusters attached to them, and will be
754 * treated differently when they are freed; instead of being placed back
755 * into the mbuf and cluster freelists, the composite mbuf + cluster objects
756 * are placed back into the appropriate composite cache's freelist, and the
757 * actual freeing is deferred until the composite objects are purged. At
758 * such a time, this flag will be cleared from the mbufs and the objects
759 * will be freed into their own separate freelists.
761 #define EXTF_COMPOSITE 0x1
764 * This flag indicates that the external cluster is read-only, i.e. it is
765 * or was referred to by more than one mbufs. Once set, this flag is never
768 #define EXTF_READONLY 0x2
769 #define EXTF_MASK (EXTF_COMPOSITE | EXTF_READONLY)
771 #define MEXT_RFA(m) ((m)->m_ext.ext_refflags)
772 #define MEXT_REF(m) (MEXT_RFA(m)->refcnt)
773 #define MEXT_FLAGS(m) (MEXT_RFA(m)->flags)
774 #define MBUF_IS_COMPOSITE(m) \
775 (MEXT_REF(m) == 0 && (MEXT_FLAGS(m) & EXTF_MASK) == EXTF_COMPOSITE)
778 * Macros used to verify the integrity of the mbuf.
780 #define _MCHECK(m) { \
781 if ((m)->m_type != MT_FREE) { \
782 if (mclaudit == NULL) \
783 panic("MCHECK: m_type=%d m=%p", \
784 (u_int16_t)(m)->m_type, m); \
786 mcl_audit_mcheck_panic(m); \
790 #define MBUF_IN_MAP(addr) \
791 ((unsigned char *)(addr) >= mbutl && \
792 (unsigned char *)(addr) < embutl)
794 #define MRANGE(addr) { \
795 if (!MBUF_IN_MAP(addr)) \
796 panic("MRANGE: address out of range 0x%p", addr); \
800 * Macro version of mtod.
802 #define MTOD(m, t) ((t)((m)->m_data))
805 * Macros to obtain page index given a base cluster address
807 #define MTOPG(x) (((unsigned char *)x - mbutl) >> PAGE_SHIFT)
808 #define PGTOM(x) (mbutl + (x << PAGE_SHIFT))
811 * Macro to find the mbuf index relative to a base.
813 #define MBPAGEIDX(c, m) \
814 (((unsigned char *)(m) - (unsigned char *)(c)) >> MSIZESHIFT)
817 * Same thing for 2KB cluster index.
819 #define CLPAGEIDX(c, m) \
820 (((unsigned char *)(m) - (unsigned char *)(c)) >> MCLSHIFT)
823 * Macro to find 4KB cluster index relative to a base
825 #define BCLPAGEIDX(c, m) \
826 (((unsigned char *)(m) - (unsigned char *)(c)) >> MBIGCLSHIFT)
829 * Macros used during mbuf and cluster initialization.
831 #define MBUF_INIT_PKTHDR(m) { \
832 (m)->m_pkthdr.rcvif = NULL; \
833 (m)->m_pkthdr.pkt_hdr = NULL; \
834 (m)->m_pkthdr.len = 0; \
835 (m)->m_pkthdr.csum_flags = 0; \
836 (m)->m_pkthdr.csum_data = 0; \
837 (m)->m_pkthdr.vlan_tag = 0; \
838 m_classifier_init(m, 0); \
844 #define MBUF_INIT(m, pkthdr, type) { \
846 (m)->m_next = (m)->m_nextpkt = NULL; \
848 (m)->m_type = type; \
849 if ((pkthdr) == 0) { \
850 (m)->m_data = (m)->m_dat; \
853 (m)->m_data = (m)->m_pktdat; \
854 (m)->m_flags = M_PKTHDR; \
855 MBUF_INIT_PKTHDR(m); \
859 #define MEXT_INIT(m, buf, size, free, arg, rfa, ref, flag) { \
860 (m)->m_data = (m)->m_ext.ext_buf = (buf); \
861 (m)->m_flags |= M_EXT; \
862 (m)->m_ext.ext_size = (size); \
863 (m)->m_ext.ext_free = (free); \
864 (m)->m_ext.ext_arg = (arg); \
865 (m)->m_ext.ext_refs.forward = (m)->m_ext.ext_refs.backward = \
866 &(m)->m_ext.ext_refs; \
867 MEXT_RFA(m) = (rfa); \
868 MEXT_REF(m) = (ref); \
869 MEXT_FLAGS(m) = (flag); \
872 #define MBUF_CL_INIT(m, buf, rfa, ref, flag) \
873 MEXT_INIT(m, buf, m_maxsize(MC_CL), NULL, NULL, rfa, ref, flag)
875 #define MBUF_BIGCL_INIT(m, buf, rfa, ref, flag) \
876 MEXT_INIT(m, buf, m_maxsize(MC_BIGCL), m_bigfree, NULL, rfa, ref, flag)
878 #define MBUF_16KCL_INIT(m, buf, rfa, ref, flag) \
879 MEXT_INIT(m, buf, m_maxsize(MC_16KCL), m_16kfree, NULL, rfa, ref, flag)
882 * Macro to convert BSD malloc sleep flag to mcache's
884 #define MSLEEPF(f) ((!((f) & M_DONTWAIT)) ? MCR_SLEEP : MCR_NOSLEEP)
887 * The structure that holds all mbuf class statistics exportable via sysctl.
888 * Similar to mbstat structure, the mb_stat structure is protected by the
889 * global mbuf lock. It contains additional information about the classes
890 * that allows for a more accurate view of the state of the allocator.
892 struct mb_stat
*mb_stat
;
893 struct omb_stat
*omb_stat
; /* For backwards compatibility */
895 #define MB_STAT_SIZE(n) \
896 ((size_t)(&((mb_stat_t *)0)->mbs_class[n]))
897 #define OMB_STAT_SIZE(n) \
898 ((size_t)(&((struct omb_stat *)0)->mbs_class[n]))
901 * The legacy structure holding all of the mbuf allocation statistics.
902 * The actual statistics used by the kernel are stored in the mbuf_table
903 * instead, and are updated atomically while the global mbuf lock is held.
904 * They are mirrored in mbstat to support legacy applications (e.g. netstat).
905 * Unlike before, the kernel no longer relies on the contents of mbstat for
906 * its operations (e.g. cluster expansion) because the structure is exposed
907 * to outside and could possibly be modified, therefore making it unsafe.
908 * With the exception of the mbstat.m_mtypes array (see below), all of the
909 * statistics are updated as they change.
911 struct mbstat mbstat
;
913 #define MBSTAT_MTYPES_MAX \
914 (sizeof (mbstat.m_mtypes) / sizeof (mbstat.m_mtypes[0]))
917 * Allocation statistics related to mbuf types (up to MT_MAX-1) are updated
918 * atomically and stored in a per-CPU structure which is lock-free; this is
919 * done in order to avoid writing to the global mbstat data structure which
920 * would cause false sharing. During sysctl request for kern.ipc.mbstat,
921 * the statistics across all CPUs will be converged into the mbstat.m_mtypes
922 * array and returned to the application. Any updates for types greater or
923 * equal than MT_MAX would be done atomically to the mbstat; this slows down
924 * performance but is okay since the kernel uses only up to MT_MAX-1 while
925 * anything beyond that (up to type 255) is considered a corner case.
928 unsigned int cpu_mtypes
[MT_MAX
];
929 } __attribute__((aligned(MAX_CPU_CACHE_LINE_SIZE
), packed
)) mtypes_cpu_t
;
932 mtypes_cpu_t mbs_cpu
[1];
935 static mbuf_mtypes_t
*mbuf_mtypes
; /* per-CPU statistics */
937 #define MBUF_MTYPES_SIZE(n) \
938 ((size_t)(&((mbuf_mtypes_t *)0)->mbs_cpu[n]))
940 #define MTYPES_CPU(p) \
941 ((mtypes_cpu_t *)(void *)((char *)(p) + MBUF_MTYPES_SIZE(cpu_number())))
943 #define mtype_stat_add(type, n) { \
944 if ((unsigned)(type) < MT_MAX) { \
945 mtypes_cpu_t *mbs = MTYPES_CPU(mbuf_mtypes); \
946 atomic_add_32(&mbs->cpu_mtypes[type], n); \
947 } else if ((unsigned)(type) < (unsigned)MBSTAT_MTYPES_MAX) { \
948 atomic_add_16((int16_t *)&mbstat.m_mtypes[type], n); \
952 #define mtype_stat_sub(t, n) mtype_stat_add(t, -(n))
953 #define mtype_stat_inc(t) mtype_stat_add(t, 1)
954 #define mtype_stat_dec(t) mtype_stat_sub(t, 1)
957 mbuf_mtypes_sync(boolean_t locked
)
963 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
965 bzero(&mtc
, sizeof (mtc
));
966 for (m
= 0; m
< ncpu
; m
++) {
967 mtypes_cpu_t
*scp
= &mbuf_mtypes
->mbs_cpu
[m
];
970 bcopy(&scp
->cpu_mtypes
, &temp
.cpu_mtypes
,
971 sizeof (temp
.cpu_mtypes
));
973 for (n
= 0; n
< MT_MAX
; n
++)
974 mtc
.cpu_mtypes
[n
] += temp
.cpu_mtypes
[n
];
977 lck_mtx_lock(mbuf_mlock
);
978 for (n
= 0; n
< MT_MAX
; n
++)
979 mbstat
.m_mtypes
[n
] = mtc
.cpu_mtypes
[n
];
981 lck_mtx_unlock(mbuf_mlock
);
985 mbstat_sysctl SYSCTL_HANDLER_ARGS
987 #pragma unused(oidp, arg1, arg2)
988 mbuf_mtypes_sync(FALSE
);
990 return (SYSCTL_OUT(req
, &mbstat
, sizeof (mbstat
)));
1001 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1003 for (k
= 0; k
< NELEM(mbuf_table
); k
++) {
1005 ccp
= &cp
->mc_cpu
[0];
1006 bktsize
= ccp
->cc_bktsize
;
1007 sp
= mbuf_table
[k
].mtbl_stats
;
1009 if (cp
->mc_flags
& MCF_NOCPUCACHE
)
1010 sp
->mbcl_mc_state
= MCS_DISABLED
;
1011 else if (cp
->mc_purge_cnt
> 0)
1012 sp
->mbcl_mc_state
= MCS_PURGING
;
1013 else if (bktsize
== 0)
1014 sp
->mbcl_mc_state
= MCS_OFFLINE
;
1016 sp
->mbcl_mc_state
= MCS_ONLINE
;
1018 sp
->mbcl_mc_cached
= 0;
1019 for (m
= 0; m
< ncpu
; m
++) {
1020 ccp
= &cp
->mc_cpu
[m
];
1021 if (ccp
->cc_objs
> 0)
1022 sp
->mbcl_mc_cached
+= ccp
->cc_objs
;
1023 if (ccp
->cc_pobjs
> 0)
1024 sp
->mbcl_mc_cached
+= ccp
->cc_pobjs
;
1026 sp
->mbcl_mc_cached
+= (cp
->mc_full
.bl_total
* bktsize
);
1027 sp
->mbcl_active
= sp
->mbcl_total
- sp
->mbcl_mc_cached
-
1030 sp
->mbcl_mc_waiter_cnt
= cp
->mc_waiter_cnt
;
1031 sp
->mbcl_mc_wretry_cnt
= cp
->mc_wretry_cnt
;
1032 sp
->mbcl_mc_nwretry_cnt
= cp
->mc_nwretry_cnt
;
1034 /* Calculate total count specific to each class */
1035 sp
->mbcl_ctotal
= sp
->mbcl_total
;
1036 switch (m_class(k
)) {
1038 /* Deduct mbufs used in composite caches */
1039 sp
->mbcl_ctotal
-= (m_total(MC_MBUF_CL
) +
1040 m_total(MC_MBUF_BIGCL
));
1044 /* Deduct clusters used in composite cache */
1045 sp
->mbcl_ctotal
-= m_total(MC_MBUF_CL
);
1049 /* Deduct clusters used in composite cache */
1050 sp
->mbcl_ctotal
-= m_total(MC_MBUF_BIGCL
);
1054 /* Deduct clusters used in composite cache */
1055 sp
->mbcl_ctotal
-= m_total(MC_MBUF_16KCL
);
1065 mb_stat_sysctl SYSCTL_HANDLER_ARGS
1067 #pragma unused(oidp, arg1, arg2)
1069 int k
, statsz
, proc64
= proc_is64bit(req
->p
);
1071 lck_mtx_lock(mbuf_mlock
);
1075 struct omb_class_stat
*oc
;
1076 struct mb_class_stat
*c
;
1078 omb_stat
->mbs_cnt
= mb_stat
->mbs_cnt
;
1079 oc
= &omb_stat
->mbs_class
[0];
1080 c
= &mb_stat
->mbs_class
[0];
1081 for (k
= 0; k
< omb_stat
->mbs_cnt
; k
++, oc
++, c
++) {
1082 (void) snprintf(oc
->mbcl_cname
, sizeof (oc
->mbcl_cname
),
1083 "%s", c
->mbcl_cname
);
1084 oc
->mbcl_size
= c
->mbcl_size
;
1085 oc
->mbcl_total
= c
->mbcl_total
;
1086 oc
->mbcl_active
= c
->mbcl_active
;
1087 oc
->mbcl_infree
= c
->mbcl_infree
;
1088 oc
->mbcl_slab_cnt
= c
->mbcl_slab_cnt
;
1089 oc
->mbcl_alloc_cnt
= c
->mbcl_alloc_cnt
;
1090 oc
->mbcl_free_cnt
= c
->mbcl_free_cnt
;
1091 oc
->mbcl_notified
= c
->mbcl_notified
;
1092 oc
->mbcl_purge_cnt
= c
->mbcl_purge_cnt
;
1093 oc
->mbcl_fail_cnt
= c
->mbcl_fail_cnt
;
1094 oc
->mbcl_ctotal
= c
->mbcl_ctotal
;
1095 oc
->mbcl_release_cnt
= c
->mbcl_release_cnt
;
1096 oc
->mbcl_mc_state
= c
->mbcl_mc_state
;
1097 oc
->mbcl_mc_cached
= c
->mbcl_mc_cached
;
1098 oc
->mbcl_mc_waiter_cnt
= c
->mbcl_mc_waiter_cnt
;
1099 oc
->mbcl_mc_wretry_cnt
= c
->mbcl_mc_wretry_cnt
;
1100 oc
->mbcl_mc_nwretry_cnt
= c
->mbcl_mc_nwretry_cnt
;
1103 statsz
= OMB_STAT_SIZE(NELEM(mbuf_table
));
1106 statsz
= MB_STAT_SIZE(NELEM(mbuf_table
));
1109 lck_mtx_unlock(mbuf_mlock
);
1111 return (SYSCTL_OUT(req
, statp
, statsz
));
1115 mleak_top_trace_sysctl SYSCTL_HANDLER_ARGS
1117 #pragma unused(oidp, arg1, arg2)
1120 /* Ensure leak tracing turned on */
1121 if (!mclfindleak
|| !mclexpleak
)
1124 lck_mtx_lock(mleak_lock
);
1125 mleak_update_stats();
1126 i
= SYSCTL_OUT(req
, mleak_stat
, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES
));
1127 lck_mtx_unlock(mleak_lock
);
1133 mleak_table_sysctl SYSCTL_HANDLER_ARGS
1135 #pragma unused(oidp, arg1, arg2)
1138 /* Ensure leak tracing turned on */
1139 if (!mclfindleak
|| !mclexpleak
)
1142 lck_mtx_lock(mleak_lock
);
1143 i
= SYSCTL_OUT(req
, &mleak_table
, sizeof (mleak_table
));
1144 lck_mtx_unlock(mleak_lock
);
1150 m_incref(struct mbuf
*m
)
1153 volatile UInt32
*addr
= (volatile UInt32
*)&MEXT_REF(m
);
1159 } while (!OSCompareAndSwap(old
, new, addr
));
1162 * If cluster is shared, mark it with (sticky) EXTF_READONLY;
1163 * we don't clear the flag when the refcount goes back to 1
1164 * to simplify code calling m_mclhasreference().
1166 if (new > 1 && !(MEXT_FLAGS(m
) & EXTF_READONLY
))
1167 (void) OSBitOrAtomic(EXTF_READONLY
, &MEXT_FLAGS(m
));
1170 static inline u_int32_t
1171 m_decref(struct mbuf
*m
)
1174 volatile UInt32
*addr
= (volatile UInt32
*)&MEXT_REF(m
);
1180 } while (!OSCompareAndSwap(old
, new, addr
));
1186 mbuf_table_init(void)
1188 unsigned int b
, c
, s
;
1189 int m
, config_mbuf_jumbo
= 0;
1191 MALLOC(omb_stat
, struct omb_stat
*, OMB_STAT_SIZE(NELEM(mbuf_table
)),
1192 M_TEMP
, M_WAITOK
| M_ZERO
);
1193 VERIFY(omb_stat
!= NULL
);
1195 MALLOC(mb_stat
, mb_stat_t
*, MB_STAT_SIZE(NELEM(mbuf_table
)),
1196 M_TEMP
, M_WAITOK
| M_ZERO
);
1197 VERIFY(mb_stat
!= NULL
);
1199 mb_stat
->mbs_cnt
= NELEM(mbuf_table
);
1200 for (m
= 0; m
< NELEM(mbuf_table
); m
++)
1201 mbuf_table
[m
].mtbl_stats
= &mb_stat
->mbs_class
[m
];
1203 #if CONFIG_MBUF_JUMBO
1204 config_mbuf_jumbo
= 1;
1205 #endif /* CONFIG_MBUF_JUMBO */
1207 if (config_mbuf_jumbo
== 1 || PAGE_SIZE
== M16KCLBYTES
) {
1209 * Set aside 1/3 of the mbuf cluster map for jumbo
1210 * clusters; we do this only on platforms where jumbo
1211 * cluster pool is enabled.
1213 njcl
= nmbclusters
/ 3;
1214 njclbytes
= M16KCLBYTES
;
1218 * nclusters holds both the 2KB and 4KB pools, so ensure it's
1219 * a multiple of 4KB clusters.
1221 nclusters
= P2ROUNDDOWN(nmbclusters
- njcl
, NCLPG
);
1224 * Each jumbo cluster takes 8 2KB clusters, so make
1225 * sure that the pool size is evenly divisible by 8;
1226 * njcl is in 2KB unit, hence treated as such.
1228 njcl
= P2ROUNDDOWN(nmbclusters
- nclusters
, NCLPJCL
);
1230 /* Update nclusters with rounded down value of njcl */
1231 nclusters
= P2ROUNDDOWN(nmbclusters
- njcl
, NCLPG
);
1235 * njcl is valid only on platforms with 16KB jumbo clusters or
1236 * with 16KB pages, where it is configured to 1/3 of the pool
1237 * size. On these platforms, the remaining is used for 2KB
1238 * and 4KB clusters. On platforms without 16KB jumbo clusters,
1239 * the entire pool is used for both 2KB and 4KB clusters. A 4KB
1240 * cluster can either be splitted into 16 mbufs, or into 2 2KB
1243 * +---+---+------------ ... -----------+------- ... -------+
1244 * | c | b | s | njcl |
1245 * +---+---+------------ ... -----------+------- ... -------+
1247 * 1/32th of the shared region is reserved for pure 2KB and 4KB
1248 * clusters (1/64th each.)
1250 c
= P2ROUNDDOWN((nclusters
>> 6), NCLPG
); /* in 2KB unit */
1251 b
= P2ROUNDDOWN((nclusters
>> (6 + NCLPBGSHIFT
)), NBCLPG
); /* in 4KB unit */
1252 s
= nclusters
- (c
+ (b
<< NCLPBGSHIFT
)); /* in 2KB unit */
1255 * 1/64th (c) is reserved for 2KB clusters.
1257 m_minlimit(MC_CL
) = c
;
1258 m_maxlimit(MC_CL
) = s
+ c
; /* in 2KB unit */
1259 m_maxsize(MC_CL
) = m_size(MC_CL
) = MCLBYTES
;
1260 (void) snprintf(m_cname(MC_CL
), MAX_MBUF_CNAME
, "cl");
1263 * Another 1/64th (b) of the map is reserved for 4KB clusters.
1264 * It cannot be turned into 2KB clusters or mbufs.
1266 m_minlimit(MC_BIGCL
) = b
;
1267 m_maxlimit(MC_BIGCL
) = (s
>> NCLPBGSHIFT
) + b
; /* in 4KB unit */
1268 m_maxsize(MC_BIGCL
) = m_size(MC_BIGCL
) = MBIGCLBYTES
;
1269 (void) snprintf(m_cname(MC_BIGCL
), MAX_MBUF_CNAME
, "bigcl");
1272 * The remaining 31/32ths (s) are all-purpose (mbufs, 2KB, or 4KB)
1274 m_minlimit(MC_MBUF
) = 0;
1275 m_maxlimit(MC_MBUF
) = (s
<< NMBPCLSHIFT
); /* in mbuf unit */
1276 m_maxsize(MC_MBUF
) = m_size(MC_MBUF
) = MSIZE
;
1277 (void) snprintf(m_cname(MC_MBUF
), MAX_MBUF_CNAME
, "mbuf");
1280 * Set limits for the composite classes.
1282 m_minlimit(MC_MBUF_CL
) = 0;
1283 m_maxlimit(MC_MBUF_CL
) = m_maxlimit(MC_CL
);
1284 m_maxsize(MC_MBUF_CL
) = MCLBYTES
;
1285 m_size(MC_MBUF_CL
) = m_size(MC_MBUF
) + m_size(MC_CL
);
1286 (void) snprintf(m_cname(MC_MBUF_CL
), MAX_MBUF_CNAME
, "mbuf_cl");
1288 m_minlimit(MC_MBUF_BIGCL
) = 0;
1289 m_maxlimit(MC_MBUF_BIGCL
) = m_maxlimit(MC_BIGCL
);
1290 m_maxsize(MC_MBUF_BIGCL
) = MBIGCLBYTES
;
1291 m_size(MC_MBUF_BIGCL
) = m_size(MC_MBUF
) + m_size(MC_BIGCL
);
1292 (void) snprintf(m_cname(MC_MBUF_BIGCL
), MAX_MBUF_CNAME
, "mbuf_bigcl");
1295 * And for jumbo classes.
1297 m_minlimit(MC_16KCL
) = 0;
1298 m_maxlimit(MC_16KCL
) = (njcl
>> NCLPJCLSHIFT
); /* in 16KB unit */
1299 m_maxsize(MC_16KCL
) = m_size(MC_16KCL
) = M16KCLBYTES
;
1300 (void) snprintf(m_cname(MC_16KCL
), MAX_MBUF_CNAME
, "16kcl");
1302 m_minlimit(MC_MBUF_16KCL
) = 0;
1303 m_maxlimit(MC_MBUF_16KCL
) = m_maxlimit(MC_16KCL
);
1304 m_maxsize(MC_MBUF_16KCL
) = M16KCLBYTES
;
1305 m_size(MC_MBUF_16KCL
) = m_size(MC_MBUF
) + m_size(MC_16KCL
);
1306 (void) snprintf(m_cname(MC_MBUF_16KCL
), MAX_MBUF_CNAME
, "mbuf_16kcl");
1309 * Initialize the legacy mbstat structure.
1311 bzero(&mbstat
, sizeof (mbstat
));
1312 mbstat
.m_msize
= m_maxsize(MC_MBUF
);
1313 mbstat
.m_mclbytes
= m_maxsize(MC_CL
);
1314 mbstat
.m_minclsize
= MINCLSIZE
;
1315 mbstat
.m_mlen
= MLEN
;
1316 mbstat
.m_mhlen
= MHLEN
;
1317 mbstat
.m_bigmclbytes
= m_maxsize(MC_BIGCL
);
1320 #if defined(__LP64__)
1321 typedef struct ncl_tbl
{
1322 uint64_t nt_maxmem
; /* memory (sane) size */
1323 uint32_t nt_mbpool
; /* mbuf pool size */
1327 static ncl_tbl_t ncl_table
[] = {
1328 { (1ULL << GBSHIFT
) /* 1 GB */, (64 << MBSHIFT
) /* 64 MB */ },
1329 { (1ULL << (GBSHIFT
+ 3)) /* 8 GB */, (96 << MBSHIFT
) /* 96 MB */ },
1330 { (1ULL << (GBSHIFT
+ 4)) /* 16 GB */, (128 << MBSHIFT
) /* 128 MB */ },
1335 static ncl_tbl_t ncl_table_srv
[] = {
1336 { (1ULL << GBSHIFT
) /* 1 GB */, (96 << MBSHIFT
) /* 96 MB */ },
1337 { (1ULL << (GBSHIFT
+ 2)) /* 4 GB */, (128 << MBSHIFT
) /* 128 MB */ },
1338 { (1ULL << (GBSHIFT
+ 3)) /* 8 GB */, (160 << MBSHIFT
) /* 160 MB */ },
1339 { (1ULL << (GBSHIFT
+ 4)) /* 16 GB */, (192 << MBSHIFT
) /* 192 MB */ },
1340 { (1ULL << (GBSHIFT
+ 5)) /* 32 GB */, (256 << MBSHIFT
) /* 256 MB */ },
1341 { (1ULL << (GBSHIFT
+ 6)) /* 64 GB */, (384 << MBSHIFT
) /* 384 MB */ },
1344 #endif /* __LP64__ */
1346 __private_extern__
unsigned int
1347 mbuf_default_ncl(int server
, uint64_t mem
)
1349 #if !defined(__LP64__)
1350 #pragma unused(server)
1353 * 32-bit kernel (default to 64MB of mbuf pool for >= 1GB RAM).
1355 if ((n
= ((mem
/ 16) / MCLBYTES
)) > 32768)
1359 ncl_tbl_t
*tbl
= (server
? ncl_table_srv
: ncl_table
);
1361 * 64-bit kernel (mbuf pool size based on table).
1363 n
= tbl
[0].nt_mbpool
;
1364 for (i
= 0; tbl
[i
].nt_mbpool
!= 0; i
++) {
1365 if (mem
< tbl
[i
].nt_maxmem
)
1367 n
= tbl
[i
].nt_mbpool
;
1370 #endif /* !__LP64__ */
1374 __private_extern__
void
1378 unsigned int initmcl
= 0;
1380 thread_t thread
= THREAD_NULL
;
1382 microuptime(&mb_start
);
1385 * These MBUF_ values must be equal to their private counterparts.
1387 _CASSERT(MBUF_EXT
== M_EXT
);
1388 _CASSERT(MBUF_PKTHDR
== M_PKTHDR
);
1389 _CASSERT(MBUF_EOR
== M_EOR
);
1390 _CASSERT(MBUF_LOOP
== M_LOOP
);
1391 _CASSERT(MBUF_BCAST
== M_BCAST
);
1392 _CASSERT(MBUF_MCAST
== M_MCAST
);
1393 _CASSERT(MBUF_FRAG
== M_FRAG
);
1394 _CASSERT(MBUF_FIRSTFRAG
== M_FIRSTFRAG
);
1395 _CASSERT(MBUF_LASTFRAG
== M_LASTFRAG
);
1396 _CASSERT(MBUF_PROMISC
== M_PROMISC
);
1397 _CASSERT(MBUF_HASFCS
== M_HASFCS
);
1399 _CASSERT(MBUF_TYPE_FREE
== MT_FREE
);
1400 _CASSERT(MBUF_TYPE_DATA
== MT_DATA
);
1401 _CASSERT(MBUF_TYPE_HEADER
== MT_HEADER
);
1402 _CASSERT(MBUF_TYPE_SOCKET
== MT_SOCKET
);
1403 _CASSERT(MBUF_TYPE_PCB
== MT_PCB
);
1404 _CASSERT(MBUF_TYPE_RTABLE
== MT_RTABLE
);
1405 _CASSERT(MBUF_TYPE_HTABLE
== MT_HTABLE
);
1406 _CASSERT(MBUF_TYPE_ATABLE
== MT_ATABLE
);
1407 _CASSERT(MBUF_TYPE_SONAME
== MT_SONAME
);
1408 _CASSERT(MBUF_TYPE_SOOPTS
== MT_SOOPTS
);
1409 _CASSERT(MBUF_TYPE_FTABLE
== MT_FTABLE
);
1410 _CASSERT(MBUF_TYPE_RIGHTS
== MT_RIGHTS
);
1411 _CASSERT(MBUF_TYPE_IFADDR
== MT_IFADDR
);
1412 _CASSERT(MBUF_TYPE_CONTROL
== MT_CONTROL
);
1413 _CASSERT(MBUF_TYPE_OOBDATA
== MT_OOBDATA
);
1415 _CASSERT(MBUF_TSO_IPV4
== CSUM_TSO_IPV4
);
1416 _CASSERT(MBUF_TSO_IPV6
== CSUM_TSO_IPV6
);
1417 _CASSERT(MBUF_CSUM_REQ_SUM16
== CSUM_PARTIAL
);
1418 _CASSERT(MBUF_CSUM_TCP_SUM16
== MBUF_CSUM_REQ_SUM16
);
1419 _CASSERT(MBUF_CSUM_REQ_IP
== CSUM_IP
);
1420 _CASSERT(MBUF_CSUM_REQ_TCP
== CSUM_TCP
);
1421 _CASSERT(MBUF_CSUM_REQ_UDP
== CSUM_UDP
);
1422 _CASSERT(MBUF_CSUM_REQ_TCPIPV6
== CSUM_TCPIPV6
);
1423 _CASSERT(MBUF_CSUM_REQ_UDPIPV6
== CSUM_UDPIPV6
);
1424 _CASSERT(MBUF_CSUM_DID_IP
== CSUM_IP_CHECKED
);
1425 _CASSERT(MBUF_CSUM_IP_GOOD
== CSUM_IP_VALID
);
1426 _CASSERT(MBUF_CSUM_DID_DATA
== CSUM_DATA_VALID
);
1427 _CASSERT(MBUF_CSUM_PSEUDO_HDR
== CSUM_PSEUDO_HDR
);
1429 _CASSERT(MBUF_WAITOK
== M_WAIT
);
1430 _CASSERT(MBUF_DONTWAIT
== M_DONTWAIT
);
1431 _CASSERT(MBUF_COPYALL
== M_COPYALL
);
1433 _CASSERT(MBUF_SC2TC(MBUF_SC_BK_SYS
) == MBUF_TC_BK
);
1434 _CASSERT(MBUF_SC2TC(MBUF_SC_BK
) == MBUF_TC_BK
);
1435 _CASSERT(MBUF_SC2TC(MBUF_SC_BE
) == MBUF_TC_BE
);
1436 _CASSERT(MBUF_SC2TC(MBUF_SC_RD
) == MBUF_TC_BE
);
1437 _CASSERT(MBUF_SC2TC(MBUF_SC_OAM
) == MBUF_TC_BE
);
1438 _CASSERT(MBUF_SC2TC(MBUF_SC_AV
) == MBUF_TC_VI
);
1439 _CASSERT(MBUF_SC2TC(MBUF_SC_RV
) == MBUF_TC_VI
);
1440 _CASSERT(MBUF_SC2TC(MBUF_SC_VI
) == MBUF_TC_VI
);
1441 _CASSERT(MBUF_SC2TC(MBUF_SC_VO
) == MBUF_TC_VO
);
1442 _CASSERT(MBUF_SC2TC(MBUF_SC_CTL
) == MBUF_TC_VO
);
1444 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BK
) == SCVAL_BK
);
1445 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_BE
) == SCVAL_BE
);
1446 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VI
) == SCVAL_VI
);
1447 _CASSERT(MBUF_TC2SCVAL(MBUF_TC_VO
) == SCVAL_VO
);
1449 /* Module specific scratch space (32-bit alignment requirement) */
1450 _CASSERT(!(offsetof(struct mbuf
, m_pkthdr
.pkt_mpriv
) %
1451 sizeof (uint32_t)));
1453 /* Initialize random red zone cookie value */
1454 _CASSERT(sizeof (mb_redzone_cookie
) ==
1455 sizeof (((struct pkthdr
*)0)->redzone
));
1456 read_random(&mb_redzone_cookie
, sizeof (mb_redzone_cookie
));
1458 /* Make sure we don't save more than we should */
1459 _CASSERT(MCA_SAVED_MBUF_SIZE
<= sizeof (struct mbuf
));
1461 if (nmbclusters
== 0)
1462 nmbclusters
= NMBCLUSTERS
;
1464 /* This should be a sane (at least even) value by now */
1465 VERIFY(nmbclusters
!= 0 && !(nmbclusters
& 0x1));
1467 /* Setup the mbuf table */
1470 /* Global lock for common layer */
1471 mbuf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1472 mbuf_mlock_grp
= lck_grp_alloc_init("mbuf", mbuf_mlock_grp_attr
);
1473 mbuf_mlock_attr
= lck_attr_alloc_init();
1474 lck_mtx_init(mbuf_mlock
, mbuf_mlock_grp
, mbuf_mlock_attr
);
1477 * Allocate cluster slabs table:
1479 * maxslabgrp = (N * 2048) / (1024 * 1024)
1481 * Where N is nmbclusters rounded up to the nearest 512. This yields
1482 * mcl_slab_g_t units, each one representing a MB of memory.
1485 (P2ROUNDUP(nmbclusters
, (MBSIZE
>> MCLSHIFT
)) << MCLSHIFT
) >> MBSHIFT
;
1486 MALLOC(slabstbl
, mcl_slabg_t
**, maxslabgrp
* sizeof (mcl_slabg_t
*),
1487 M_TEMP
, M_WAITOK
| M_ZERO
);
1488 VERIFY(slabstbl
!= NULL
);
1491 * Allocate audit structures, if needed:
1493 * maxclaudit = (maxslabgrp * 1024 * 1024) / PAGE_SIZE
1495 * This yields mcl_audit_t units, each one representing a page.
1497 PE_parse_boot_argn("mbuf_debug", &mbuf_debug
, sizeof (mbuf_debug
));
1498 mbuf_debug
|= mcache_getflags();
1499 if (mbuf_debug
& MCF_DEBUG
) {
1502 maxclaudit
= ((maxslabgrp
<< MBSHIFT
) >> PAGE_SHIFT
);
1503 MALLOC(mclaudit
, mcl_audit_t
*, maxclaudit
* sizeof (*mclaudit
),
1504 M_TEMP
, M_WAITOK
| M_ZERO
);
1505 VERIFY(mclaudit
!= NULL
);
1506 for (l
= 0, mclad
= mclaudit
; l
< maxclaudit
; l
++) {
1507 MALLOC(mclad
[l
].cl_audit
, mcache_audit_t
**,
1508 NMBPG
* sizeof(mcache_audit_t
*),
1509 M_TEMP
, M_WAITOK
| M_ZERO
);
1510 VERIFY(mclad
[l
].cl_audit
!= NULL
);
1513 mcl_audit_con_cache
= mcache_create("mcl_audit_contents",
1514 AUDIT_CONTENTS_SIZE
, sizeof (u_int64_t
), 0, MCR_SLEEP
);
1515 VERIFY(mcl_audit_con_cache
!= NULL
);
1517 mclverify
= (mbuf_debug
& MCF_VERIFY
);
1518 mcltrace
= (mbuf_debug
& MCF_TRACE
);
1519 mclfindleak
= !(mbuf_debug
& MCF_NOLEAKLOG
);
1520 mclexpleak
= mclfindleak
&& (mbuf_debug
& MCF_EXPLEAKLOG
);
1522 /* Enable mbuf leak logging, with a lock to protect the tables */
1524 mleak_lock_grp_attr
= lck_grp_attr_alloc_init();
1525 mleak_lock_grp
= lck_grp_alloc_init("mleak_lock", mleak_lock_grp_attr
);
1526 mleak_lock_attr
= lck_attr_alloc_init();
1527 lck_mtx_init(mleak_lock
, mleak_lock_grp
, mleak_lock_attr
);
1531 /* Calculate the number of pages assigned to the cluster pool */
1532 mcl_pages
= (nmbclusters
<< MCLSHIFT
) / PAGE_SIZE
;
1533 MALLOC(mcl_paddr
, ppnum_t
*, mcl_pages
* sizeof (ppnum_t
),
1535 VERIFY(mcl_paddr
!= NULL
);
1537 /* Register with the I/O Bus mapper */
1538 mcl_paddr_base
= IOMapperIOVMAlloc(mcl_pages
);
1539 bzero((char *)mcl_paddr
, mcl_pages
* sizeof (ppnum_t
));
1541 embutl
= (mbutl
+ (nmbclusters
* MCLBYTES
));
1542 VERIFY(((embutl
- mbutl
) % MBIGCLBYTES
) == 0);
1544 /* Prime up the freelist */
1545 PE_parse_boot_argn("initmcl", &initmcl
, sizeof (initmcl
));
1547 initmcl
>>= NCLPBGSHIFT
; /* become a 4K unit */
1548 if (initmcl
> m_maxlimit(MC_BIGCL
))
1549 initmcl
= m_maxlimit(MC_BIGCL
);
1551 if (initmcl
< m_minlimit(MC_BIGCL
))
1552 initmcl
= m_minlimit(MC_BIGCL
);
1554 lck_mtx_lock(mbuf_mlock
);
1557 * For classes with non-zero minimum limits, populate their freelists
1558 * so that m_total(class) is at least m_minlimit(class).
1560 VERIFY(m_total(MC_BIGCL
) == 0 && m_minlimit(MC_BIGCL
) != 0);
1561 freelist_populate(m_class(MC_BIGCL
), initmcl
, M_WAIT
);
1562 VERIFY(m_total(MC_BIGCL
) >= m_minlimit(MC_BIGCL
));
1563 freelist_init(m_class(MC_CL
));
1565 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
1566 /* Make sure we didn't miss any */
1567 VERIFY(m_minlimit(m_class(m
)) == 0 ||
1568 m_total(m_class(m
)) >= m_minlimit(m_class(m
)));
1570 /* populate the initial sizes and report from there on */
1571 m_peak(m_class(m
)) = m_total(m_class(m
));
1573 mb_peak_newreport
= FALSE
;
1575 lck_mtx_unlock(mbuf_mlock
);
1577 (void) kernel_thread_start((thread_continue_t
)mbuf_worker_thread_init
,
1579 thread_deallocate(thread
);
1581 ref_cache
= mcache_create("mext_ref", sizeof (struct ext_ref
),
1584 /* Create the cache for each class */
1585 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
1586 void *allocfunc
, *freefunc
, *auditfunc
, *logfunc
;
1590 if (m_class(m
) == MC_MBUF_CL
|| m_class(m
) == MC_MBUF_BIGCL
||
1591 m_class(m
) == MC_MBUF_16KCL
) {
1592 allocfunc
= mbuf_cslab_alloc
;
1593 freefunc
= mbuf_cslab_free
;
1594 auditfunc
= mbuf_cslab_audit
;
1595 logfunc
= mleak_logger
;
1597 allocfunc
= mbuf_slab_alloc
;
1598 freefunc
= mbuf_slab_free
;
1599 auditfunc
= mbuf_slab_audit
;
1600 logfunc
= mleak_logger
;
1604 * Disable per-CPU caches for jumbo classes if there
1605 * is no jumbo cluster pool available in the system.
1606 * The cache itself is still created (but will never
1607 * be populated) since it simplifies the code.
1609 if ((m_class(m
) == MC_MBUF_16KCL
|| m_class(m
) == MC_16KCL
) &&
1611 flags
|= MCF_NOCPUCACHE
;
1614 flags
|= MCF_NOLEAKLOG
;
1616 m_cache(m
) = mcache_create_ext(m_cname(m
), m_maxsize(m
),
1617 allocfunc
, freefunc
, auditfunc
, logfunc
, mbuf_slab_notify
,
1618 (void *)(uintptr_t)m
, flags
, MCR_SLEEP
);
1622 * Allocate structure for per-CPU statistics that's aligned
1623 * on the CPU cache boundary; this code assumes that we never
1624 * uninitialize this framework, since the original address
1625 * before alignment is not saved.
1627 ncpu
= ml_get_max_cpus();
1628 MALLOC(buf
, void *, MBUF_MTYPES_SIZE(ncpu
) + CPU_CACHE_LINE_SIZE
,
1630 VERIFY(buf
!= NULL
);
1632 mbuf_mtypes
= (mbuf_mtypes_t
*)P2ROUNDUP((intptr_t)buf
,
1633 CPU_CACHE_LINE_SIZE
);
1634 bzero(mbuf_mtypes
, MBUF_MTYPES_SIZE(ncpu
));
1637 * Set the max limit on sb_max to be 1/16 th of the size of
1638 * memory allocated for mbuf clusters.
1640 high_sb_max
= (nmbclusters
<< (MCLSHIFT
- 4));
1641 if (high_sb_max
< sb_max
) {
1642 /* sb_max is too large for this configuration, scale it down */
1643 if (high_sb_max
> (1 << MBSHIFT
)) {
1644 /* We have atleast 16 M of mbuf pool */
1645 sb_max
= high_sb_max
;
1646 } else if ((nmbclusters
<< MCLSHIFT
) > (1 << MBSHIFT
)) {
1648 * If we have more than 1M of mbufpool, cap the size of
1649 * max sock buf at 1M
1651 sb_max
= high_sb_max
= (1 << MBSHIFT
);
1653 sb_max
= high_sb_max
;
1657 /* allocate space for mbuf_dump_buf */
1658 MALLOC(mbuf_dump_buf
, char *, MBUF_DUMP_BUF_SIZE
, M_TEMP
, M_WAITOK
);
1659 VERIFY(mbuf_dump_buf
!= NULL
);
1661 if (mbuf_debug
& MCF_DEBUG
) {
1662 printf("%s: MLEN %d, MHLEN %d\n", __func__
,
1663 (int)_MLEN
, (int)_MHLEN
);
1666 printf("%s: done [%d MB total pool size, (%d/%d) split]\n", __func__
,
1667 (nmbclusters
<< MCLSHIFT
) >> MBSHIFT
,
1668 (nclusters
<< MCLSHIFT
) >> MBSHIFT
,
1669 (njcl
<< MCLSHIFT
) >> MBSHIFT
);
1673 * Obtain a slab of object(s) from the class's freelist.
1675 static mcache_obj_t
*
1676 slab_alloc(mbuf_class_t
class, int wait
)
1681 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1683 /* This should always be NULL for us */
1684 VERIFY(m_cobjlist(class) == NULL
);
1687 * Treat composite objects as having longer lifespan by using
1688 * a slab from the reverse direction, in hoping that this could
1689 * reduce the probability of fragmentation for slabs that hold
1690 * more than one buffer chunks (e.g. mbuf slabs). For other
1691 * slabs, this probably doesn't make much of a difference.
1693 if ((class == MC_MBUF
|| class == MC_CL
|| class == MC_BIGCL
)
1694 && (wait
& MCR_COMP
))
1695 sp
= (mcl_slab_t
*)TAILQ_LAST(&m_slablist(class), mcl_slhead
);
1697 sp
= (mcl_slab_t
*)TAILQ_FIRST(&m_slablist(class));
1700 VERIFY(m_infree(class) == 0 && m_slab_cnt(class) == 0);
1701 /* The slab list for this class is empty */
1705 VERIFY(m_infree(class) > 0);
1706 VERIFY(!slab_is_detached(sp
));
1707 VERIFY(sp
->sl_class
== class &&
1708 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
1710 VERIFY(slab_inrange(sp
, buf
) && sp
== slab_get(buf
));
1711 sp
->sl_head
= buf
->obj_next
;
1712 /* Increment slab reference */
1715 VERIFY(sp
->sl_head
!= NULL
|| sp
->sl_refcnt
== sp
->sl_chunks
);
1717 if (sp
->sl_head
!= NULL
&& !slab_inrange(sp
, sp
->sl_head
)) {
1718 slab_nextptr_panic(sp
, sp
->sl_head
);
1719 /* In case sl_head is in the map but not in the slab */
1720 VERIFY(slab_inrange(sp
, sp
->sl_head
));
1724 if (mclaudit
!= NULL
) {
1725 mcache_audit_t
*mca
= mcl_audit_buf2mca(class, buf
);
1726 mca
->mca_uflags
= 0;
1727 /* Save contents on mbuf objects only */
1728 if (class == MC_MBUF
)
1729 mca
->mca_uflags
|= MB_SCVALID
;
1732 if (class == MC_CL
) {
1733 mbstat
.m_clfree
= (--m_infree(MC_CL
)) + m_infree(MC_MBUF_CL
);
1735 * A 2K cluster slab can have at most NCLPG references.
1737 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_refcnt
<= NCLPG
&&
1738 sp
->sl_chunks
== NCLPG
&& sp
->sl_len
== PAGE_SIZE
);
1739 VERIFY(sp
->sl_refcnt
< NCLPG
|| sp
->sl_head
== NULL
);
1740 } else if (class == MC_BIGCL
) {
1741 mbstat
.m_bigclfree
= (--m_infree(MC_BIGCL
)) +
1742 m_infree(MC_MBUF_BIGCL
);
1744 * A 4K cluster slab can have NBCLPG references.
1746 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_chunks
== NBCLPG
&&
1747 sp
->sl_len
== PAGE_SIZE
&&
1748 (sp
->sl_refcnt
< NBCLPG
|| sp
->sl_head
== NULL
));
1749 } else if (class == MC_16KCL
) {
1753 --m_infree(MC_16KCL
);
1754 VERIFY(sp
->sl_refcnt
== 1 && sp
->sl_chunks
== 1 &&
1755 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1757 * Increment 2nd-Nth slab reference, where N is NSLABSP16KB.
1758 * A 16KB big cluster takes NSLABSP16KB slabs, each having at
1761 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
1763 /* Next slab must already be present */
1764 VERIFY(nsp
!= NULL
);
1766 VERIFY(!slab_is_detached(nsp
));
1767 VERIFY(nsp
->sl_class
== MC_16KCL
&&
1768 nsp
->sl_flags
== (SLF_MAPPED
| SLF_PARTIAL
) &&
1769 nsp
->sl_refcnt
== 1 && nsp
->sl_chunks
== 0 &&
1770 nsp
->sl_len
== 0 && nsp
->sl_base
== sp
->sl_base
&&
1771 nsp
->sl_head
== NULL
);
1774 VERIFY(class == MC_MBUF
);
1775 --m_infree(MC_MBUF
);
1777 * If auditing is turned on, this check is
1778 * deferred until later in mbuf_slab_audit().
1780 if (mclaudit
== NULL
)
1781 _MCHECK((struct mbuf
*)buf
);
1783 * Since we have incremented the reference count above,
1784 * an mbuf slab (formerly a 4KB cluster slab that was cut
1785 * up into mbufs) must have a reference count between 1
1786 * and NMBPG at this point.
1788 VERIFY(sp
->sl_refcnt
>= 1 && sp
->sl_refcnt
<= NMBPG
&&
1789 sp
->sl_chunks
== NMBPG
&&
1790 sp
->sl_len
== PAGE_SIZE
);
1791 VERIFY(sp
->sl_refcnt
< NMBPG
|| sp
->sl_head
== NULL
);
1794 /* If empty, remove this slab from the class's freelist */
1795 if (sp
->sl_head
== NULL
) {
1796 VERIFY(class != MC_MBUF
|| sp
->sl_refcnt
== NMBPG
);
1797 VERIFY(class != MC_CL
|| sp
->sl_refcnt
== NCLPG
);
1798 VERIFY(class != MC_BIGCL
|| sp
->sl_refcnt
== NBCLPG
);
1799 slab_remove(sp
, class);
1806 * Place a slab of object(s) back into a class's slab list.
1809 slab_free(mbuf_class_t
class, mcache_obj_t
*buf
)
1812 boolean_t reinit_supercl
= false;
1813 mbuf_class_t super_class
;
1815 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
1817 VERIFY(class != MC_16KCL
|| njcl
> 0);
1818 VERIFY(buf
->obj_next
== NULL
);
1821 VERIFY(sp
->sl_class
== class && slab_inrange(sp
, buf
) &&
1822 (sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
1824 /* Decrement slab reference */
1827 if (class == MC_CL
) {
1828 VERIFY(IS_P2ALIGNED(buf
, MCLBYTES
));
1830 * A slab that has been splitted for 2KB clusters can have
1831 * at most 1 outstanding reference at this point.
1833 VERIFY(sp
->sl_refcnt
>= 0 && sp
->sl_refcnt
<= (NCLPG
- 1) &&
1834 sp
->sl_chunks
== NCLPG
&& sp
->sl_len
== PAGE_SIZE
);
1835 VERIFY(sp
->sl_refcnt
< (NCLPG
- 1) ||
1836 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1837 } else if (class == MC_BIGCL
) {
1838 VERIFY(IS_P2ALIGNED(buf
, MBIGCLBYTES
));
1840 /* A 4KB cluster slab can have NBCLPG references at most */
1841 VERIFY(sp
->sl_refcnt
>= 0 && sp
->sl_chunks
== NBCLPG
);
1842 VERIFY(sp
->sl_refcnt
< (NBCLPG
- 1) ||
1843 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1844 } else if (class == MC_16KCL
) {
1848 * A 16KB cluster takes NSLABSP16KB slabs, all must
1849 * now have 0 reference.
1851 VERIFY(IS_P2ALIGNED(buf
, PAGE_SIZE
));
1852 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_chunks
== 1 &&
1853 sp
->sl_len
== m_maxsize(class) && sp
->sl_head
== NULL
);
1854 VERIFY(slab_is_detached(sp
));
1855 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
1857 /* Next slab must already be present */
1858 VERIFY(nsp
!= NULL
);
1860 VERIFY(slab_is_detached(nsp
));
1861 VERIFY(nsp
->sl_class
== MC_16KCL
&&
1862 (nsp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) &&
1863 nsp
->sl_refcnt
== 0 && nsp
->sl_chunks
== 0 &&
1864 nsp
->sl_len
== 0 && nsp
->sl_base
== sp
->sl_base
&&
1865 nsp
->sl_head
== NULL
);
1869 * A slab that has been splitted for mbufs has at most
1870 * NMBPG reference counts. Since we have decremented
1871 * one reference above, it must now be between 0 and
1874 VERIFY(class == MC_MBUF
);
1875 VERIFY(sp
->sl_refcnt
>= 0 &&
1876 sp
->sl_refcnt
<= (NMBPG
- 1) &&
1877 sp
->sl_chunks
== NMBPG
&&
1878 sp
->sl_len
== PAGE_SIZE
);
1879 VERIFY(sp
->sl_refcnt
< (NMBPG
- 1) ||
1880 (slab_is_detached(sp
) && sp
->sl_head
== NULL
));
1884 * When auditing is enabled, ensure that the buffer still
1885 * contains the free pattern. Otherwise it got corrupted
1886 * while at the CPU cache layer.
1888 if (mclaudit
!= NULL
) {
1889 mcache_audit_t
*mca
= mcl_audit_buf2mca(class, buf
);
1891 mcache_audit_free_verify(mca
, buf
, 0,
1894 mca
->mca_uflags
&= ~MB_SCVALID
;
1897 if (class == MC_CL
) {
1898 mbstat
.m_clfree
= (++m_infree(MC_CL
)) + m_infree(MC_MBUF_CL
);
1899 buf
->obj_next
= sp
->sl_head
;
1900 } else if (class == MC_BIGCL
) {
1901 mbstat
.m_bigclfree
= (++m_infree(MC_BIGCL
)) +
1902 m_infree(MC_MBUF_BIGCL
);
1903 buf
->obj_next
= sp
->sl_head
;
1904 } else if (class == MC_16KCL
) {
1905 ++m_infree(MC_16KCL
);
1907 ++m_infree(MC_MBUF
);
1908 buf
->obj_next
= sp
->sl_head
;
1913 * If a slab has been split to either one which holds 2KB clusters,
1914 * or one which holds mbufs, turn it back to one which holds a
1915 * 4 or 16 KB cluster depending on the page size.
1917 if (m_maxsize(MC_BIGCL
) == PAGE_SIZE
) {
1918 super_class
= MC_BIGCL
;
1920 VERIFY(PAGE_SIZE
== m_maxsize(MC_16KCL
));
1921 super_class
= MC_16KCL
;
1923 if (class == MC_MBUF
&& sp
->sl_refcnt
== 0 &&
1924 m_total(class) >= (m_minlimit(class) + NMBPG
) &&
1925 m_total(super_class
) < m_maxlimit(super_class
)) {
1928 m_total(MC_MBUF
) -= NMBPG
;
1929 mbstat
.m_mbufs
= m_total(MC_MBUF
);
1930 m_infree(MC_MBUF
) -= NMBPG
;
1931 mtype_stat_add(MT_FREE
, -((unsigned)NMBPG
));
1934 struct mbuf
*m
= sp
->sl_head
;
1936 sp
->sl_head
= m
->m_next
;
1939 reinit_supercl
= true;
1940 } else if (class == MC_CL
&& sp
->sl_refcnt
== 0 &&
1941 m_total(class) >= (m_minlimit(class) + NCLPG
) &&
1942 m_total(super_class
) < m_maxlimit(super_class
)) {
1945 m_total(MC_CL
) -= NCLPG
;
1946 mbstat
.m_clusters
= m_total(MC_CL
);
1947 m_infree(MC_CL
) -= NCLPG
;
1950 union mcluster
*c
= sp
->sl_head
;
1952 sp
->sl_head
= c
->mcl_next
;
1955 reinit_supercl
= true;
1956 } else if (class == MC_BIGCL
&& super_class
!= MC_BIGCL
&&
1957 sp
->sl_refcnt
== 0 &&
1958 m_total(class) >= (m_minlimit(class) + NBCLPG
) &&
1959 m_total(super_class
) < m_maxlimit(super_class
)) {
1962 VERIFY(super_class
== MC_16KCL
);
1963 m_total(MC_BIGCL
) -= NBCLPG
;
1964 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
1965 m_infree(MC_BIGCL
) -= NBCLPG
;
1968 union mbigcluster
*bc
= sp
->sl_head
;
1970 sp
->sl_head
= bc
->mbc_next
;
1971 bc
->mbc_next
= NULL
;
1973 reinit_supercl
= true;
1976 if (reinit_supercl
) {
1977 VERIFY(sp
->sl_head
== NULL
);
1978 VERIFY(m_total(class) >= m_minlimit(class));
1979 slab_remove(sp
, class);
1981 /* Reinitialize it as a cluster for the super class */
1982 m_total(super_class
)++;
1983 m_infree(super_class
)++;
1984 VERIFY(sp
->sl_flags
== (SLF_MAPPED
| SLF_DETACHED
) &&
1985 sp
->sl_len
== PAGE_SIZE
&& sp
->sl_refcnt
== 0);
1987 slab_init(sp
, super_class
, SLF_MAPPED
, sp
->sl_base
,
1988 sp
->sl_base
, PAGE_SIZE
, 0, 1);
1990 mcache_set_pattern(MCACHE_FREE_PATTERN
,
1991 (caddr_t
)sp
->sl_base
, sp
->sl_len
);
1992 ((mcache_obj_t
*)(sp
->sl_base
))->obj_next
= NULL
;
1994 if (super_class
== MC_BIGCL
) {
1995 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
1996 mbstat
.m_bigclfree
= m_infree(MC_BIGCL
) +
1997 m_infree(MC_MBUF_BIGCL
);
2000 VERIFY(slab_is_detached(sp
));
2001 VERIFY(m_total(super_class
) <= m_maxlimit(super_class
));
2003 /* And finally switch class */
2004 class = super_class
;
2007 /* Reinsert the slab to the class's slab list */
2008 if (slab_is_detached(sp
))
2009 slab_insert(sp
, class);
2013 * Common allocator for rudimentary objects called by the CPU cache layer
2014 * during an allocation request whenever there is no available element in the
2015 * bucket layer. It returns one or more elements from the appropriate global
2016 * freelist. If the freelist is empty, it will attempt to populate it and
2017 * retry the allocation.
2020 mbuf_slab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int num
, int wait
)
2022 mbuf_class_t
class = (mbuf_class_t
)arg
;
2023 unsigned int need
= num
;
2024 mcache_obj_t
**list
= *plist
;
2026 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2029 lck_mtx_lock(mbuf_mlock
);
2032 if ((*list
= slab_alloc(class, wait
)) != NULL
) {
2033 (*list
)->obj_next
= NULL
;
2034 list
= *plist
= &(*list
)->obj_next
;
2038 * If the number of elements in freelist has
2039 * dropped below low watermark, asynchronously
2040 * populate the freelist now rather than doing
2041 * it later when we run out of elements.
2043 if (!mbuf_cached_above(class, wait
) &&
2044 m_infree(class) < (m_total(class) >> 5)) {
2045 (void) freelist_populate(class, 1,
2051 VERIFY(m_infree(class) == 0 || class == MC_CL
);
2053 (void) freelist_populate(class, 1,
2054 (wait
& MCR_NOSLEEP
) ? M_DONTWAIT
: M_WAIT
);
2056 if (m_infree(class) > 0)
2059 /* Check if there's anything at the cache layer */
2060 if (mbuf_cached_above(class, wait
))
2063 /* watchdog checkpoint */
2066 /* We have nothing and cannot block; give up */
2067 if (wait
& MCR_NOSLEEP
) {
2068 if (!(wait
& MCR_TRYHARD
)) {
2069 m_fail_cnt(class)++;
2076 * If the freelist is still empty and the caller is
2077 * willing to be blocked, sleep on the wait channel
2078 * until an element is available. Otherwise, if
2079 * MCR_TRYHARD is set, do our best to satisfy the
2080 * request without having to go to sleep.
2082 if (mbuf_worker_ready
&&
2083 mbuf_sleep(class, need
, wait
))
2086 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2090 m_alloc_cnt(class) += num
- need
;
2091 lck_mtx_unlock(mbuf_mlock
);
2093 return (num
- need
);
2097 * Common de-allocator for rudimentary objects called by the CPU cache
2098 * layer when one or more elements need to be returned to the appropriate
2102 mbuf_slab_free(void *arg
, mcache_obj_t
*list
, __unused
int purged
)
2104 mbuf_class_t
class = (mbuf_class_t
)arg
;
2105 mcache_obj_t
*nlist
;
2106 unsigned int num
= 0;
2109 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2111 lck_mtx_lock(mbuf_mlock
);
2114 nlist
= list
->obj_next
;
2115 list
->obj_next
= NULL
;
2116 slab_free(class, list
);
2118 if ((list
= nlist
) == NULL
)
2121 m_free_cnt(class) += num
;
2123 if ((w
= mb_waiters
) > 0)
2126 lck_mtx_unlock(mbuf_mlock
);
2129 wakeup(mb_waitchan
);
2133 * Common auditor for rudimentary objects called by the CPU cache layer
2134 * during an allocation or free request. For the former, this is called
2135 * after the objects are obtained from either the bucket or slab layer
2136 * and before they are returned to the caller. For the latter, this is
2137 * called immediately during free and before placing the objects into
2138 * the bucket or slab layer.
2141 mbuf_slab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
2143 mbuf_class_t
class = (mbuf_class_t
)arg
;
2144 mcache_audit_t
*mca
;
2146 ASSERT(MBUF_CLASS_VALID(class) && !MBUF_CLASS_COMPOSITE(class));
2148 while (list
!= NULL
) {
2149 lck_mtx_lock(mbuf_mlock
);
2150 mca
= mcl_audit_buf2mca(class, list
);
2152 /* Do the sanity checks */
2153 if (class == MC_MBUF
) {
2154 mcl_audit_mbuf(mca
, list
, FALSE
, alloc
);
2155 ASSERT(mca
->mca_uflags
& MB_SCVALID
);
2157 mcl_audit_cluster(mca
, list
, m_maxsize(class),
2159 ASSERT(!(mca
->mca_uflags
& MB_SCVALID
));
2161 /* Record this transaction */
2163 mcache_buffer_log(mca
, list
, m_cache(class), &mb_start
);
2166 mca
->mca_uflags
|= MB_INUSE
;
2168 mca
->mca_uflags
&= ~MB_INUSE
;
2169 /* Unpair the object (unconditionally) */
2170 mca
->mca_uptr
= NULL
;
2171 lck_mtx_unlock(mbuf_mlock
);
2173 list
= list
->obj_next
;
2178 * Common notify routine for all caches. It is called by mcache when
2179 * one or more objects get freed. We use this indication to trigger
2180 * the wakeup of any sleeping threads so that they can retry their
2181 * allocation requests.
2184 mbuf_slab_notify(void *arg
, u_int32_t reason
)
2186 mbuf_class_t
class = (mbuf_class_t
)arg
;
2189 ASSERT(MBUF_CLASS_VALID(class));
2191 if (reason
!= MCN_RETRYALLOC
)
2194 lck_mtx_lock(mbuf_mlock
);
2195 if ((w
= mb_waiters
) > 0) {
2196 m_notified(class)++;
2199 lck_mtx_unlock(mbuf_mlock
);
2202 wakeup(mb_waitchan
);
2206 * Obtain object(s) from the composite class's freelist.
2209 cslab_alloc(mbuf_class_t
class, mcache_obj_t
***plist
, unsigned int num
)
2211 unsigned int need
= num
;
2212 mcl_slab_t
*sp
, *clsp
, *nsp
;
2214 mcache_obj_t
**list
= *plist
;
2218 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2219 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2221 /* Get what we can from the freelist */
2222 while ((*list
= m_cobjlist(class)) != NULL
) {
2225 m
= (struct mbuf
*)*list
;
2227 cl
= m
->m_ext
.ext_buf
;
2228 clsp
= slab_get(cl
);
2229 VERIFY(m
->m_flags
== M_EXT
&& cl
!= NULL
);
2230 VERIFY(MEXT_RFA(m
) != NULL
&& MBUF_IS_COMPOSITE(m
));
2232 if (class == MC_MBUF_CL
) {
2233 VERIFY(clsp
->sl_refcnt
>= 1 &&
2234 clsp
->sl_refcnt
<= NCLPG
);
2236 VERIFY(clsp
->sl_refcnt
>= 1 &&
2237 clsp
->sl_refcnt
<= NBCLPG
);
2240 if (class == MC_MBUF_16KCL
) {
2242 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2244 /* Next slab must already be present */
2245 VERIFY(nsp
!= NULL
);
2246 VERIFY(nsp
->sl_refcnt
== 1);
2250 if ((m_cobjlist(class) = (*list
)->obj_next
) != NULL
&&
2251 !MBUF_IN_MAP(m_cobjlist(class))) {
2252 slab_nextptr_panic(sp
, m_cobjlist(class));
2255 (*list
)->obj_next
= NULL
;
2256 list
= *plist
= &(*list
)->obj_next
;
2261 m_infree(class) -= (num
- need
);
2263 return (num
- need
);
2267 * Place object(s) back into a composite class's freelist.
2270 cslab_free(mbuf_class_t
class, mcache_obj_t
*list
, int purged
)
2272 mcache_obj_t
*o
, *tail
;
2273 unsigned int num
= 0;
2274 struct mbuf
*m
, *ms
;
2275 mcache_audit_t
*mca
= NULL
;
2276 mcache_obj_t
*ref_list
= NULL
;
2277 mcl_slab_t
*clsp
, *nsp
;
2279 mbuf_class_t cl_class
;
2281 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2282 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2283 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2285 if (class == MC_MBUF_CL
) {
2287 } else if (class == MC_MBUF_BIGCL
) {
2288 cl_class
= MC_BIGCL
;
2290 VERIFY(class == MC_MBUF_16KCL
);
2291 cl_class
= MC_16KCL
;
2296 while ((m
= ms
= (struct mbuf
*)o
) != NULL
) {
2297 mcache_obj_t
*rfa
, *nexto
= o
->obj_next
;
2299 /* Do the mbuf sanity checks */
2300 if (mclaudit
!= NULL
) {
2301 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2303 mcache_audit_free_verify(mca
, m
, 0,
2304 m_maxsize(MC_MBUF
));
2306 ms
= MCA_SAVED_MBUF_PTR(mca
);
2309 /* Do the cluster sanity checks */
2310 cl
= ms
->m_ext
.ext_buf
;
2311 clsp
= slab_get(cl
);
2313 size_t size
= m_maxsize(cl_class
);
2314 mcache_audit_free_verify(mcl_audit_buf2mca(cl_class
,
2315 (mcache_obj_t
*)cl
), cl
, 0, size
);
2317 VERIFY(ms
->m_type
== MT_FREE
);
2318 VERIFY(ms
->m_flags
== M_EXT
);
2319 VERIFY(MEXT_RFA(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2320 if (cl_class
== MC_CL
) {
2321 VERIFY(clsp
->sl_refcnt
>= 1 &&
2322 clsp
->sl_refcnt
<= NCLPG
);
2324 VERIFY(clsp
->sl_refcnt
>= 1 &&
2325 clsp
->sl_refcnt
<= NBCLPG
);
2327 if (cl_class
== MC_16KCL
) {
2329 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2331 /* Next slab must already be present */
2332 VERIFY(nsp
!= NULL
);
2333 VERIFY(nsp
->sl_refcnt
== 1);
2338 * If we're asked to purge, restore the actual mbuf using
2339 * contents of the shadow structure (if auditing is enabled)
2340 * and clear EXTF_COMPOSITE flag from the mbuf, as we are
2341 * about to free it and the attached cluster into their caches.
2344 /* Restore constructed mbuf fields */
2345 if (mclaudit
!= NULL
)
2346 mcl_audit_restore_mbuf(m
, mca
, TRUE
);
2351 rfa
= (mcache_obj_t
*)(void *)MEXT_RFA(m
);
2352 rfa
->obj_next
= ref_list
;
2356 m
->m_type
= MT_FREE
;
2357 m
->m_flags
= m
->m_len
= 0;
2358 m
->m_next
= m
->m_nextpkt
= NULL
;
2360 /* Save mbuf fields and make auditing happy */
2361 if (mclaudit
!= NULL
)
2362 mcl_audit_mbuf(mca
, o
, FALSE
, FALSE
);
2364 VERIFY(m_total(class) > 0);
2369 slab_free(MC_MBUF
, o
);
2371 /* And free the cluster */
2372 ((mcache_obj_t
*)cl
)->obj_next
= NULL
;
2373 if (class == MC_MBUF_CL
)
2374 slab_free(MC_CL
, cl
);
2375 else if (class == MC_MBUF_BIGCL
)
2376 slab_free(MC_BIGCL
, cl
);
2378 slab_free(MC_16KCL
, cl
);
2387 tail
->obj_next
= m_cobjlist(class);
2388 m_cobjlist(class) = list
;
2389 m_infree(class) += num
;
2390 } else if (ref_list
!= NULL
) {
2391 mcache_free_ext(ref_cache
, ref_list
);
2398 * Common allocator for composite objects called by the CPU cache layer
2399 * during an allocation request whenever there is no available element in
2400 * the bucket layer. It returns one or more composite elements from the
2401 * appropriate global freelist. If the freelist is empty, it will attempt
2402 * to obtain the rudimentary objects from their caches and construct them
2403 * into composite mbuf + cluster objects.
2406 mbuf_cslab_alloc(void *arg
, mcache_obj_t
***plist
, unsigned int needed
,
2409 mbuf_class_t
class = (mbuf_class_t
)arg
;
2410 mbuf_class_t cl_class
= 0;
2411 unsigned int num
= 0, cnum
= 0, want
= needed
;
2412 mcache_obj_t
*ref_list
= NULL
;
2413 mcache_obj_t
*mp_list
= NULL
;
2414 mcache_obj_t
*clp_list
= NULL
;
2415 mcache_obj_t
**list
;
2416 struct ext_ref
*rfa
;
2420 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2423 VERIFY(class != MC_MBUF_16KCL
|| njcl
> 0);
2425 /* There should not be any slab for this class */
2426 VERIFY(m_slab_cnt(class) == 0 &&
2427 m_slablist(class).tqh_first
== NULL
&&
2428 m_slablist(class).tqh_last
== NULL
);
2430 lck_mtx_lock(mbuf_mlock
);
2432 /* Try using the freelist first */
2433 num
= cslab_alloc(class, plist
, needed
);
2435 if (num
== needed
) {
2436 m_alloc_cnt(class) += num
;
2437 lck_mtx_unlock(mbuf_mlock
);
2441 lck_mtx_unlock(mbuf_mlock
);
2444 * We could not satisfy the request using the freelist alone;
2445 * allocate from the appropriate rudimentary caches and use
2446 * whatever we can get to construct the composite objects.
2451 * Mark these allocation requests as coming from a composite cache.
2452 * Also, if the caller is willing to be blocked, mark the request
2453 * with MCR_FAILOK such that we don't end up sleeping at the mbuf
2454 * slab layer waiting for the individual object when one or more
2455 * of the already-constructed composite objects are available.
2458 if (!(wait
& MCR_NOSLEEP
))
2461 /* allocate mbufs */
2462 needed
= mcache_alloc_ext(m_cache(MC_MBUF
), &mp_list
, needed
, wait
);
2464 ASSERT(mp_list
== NULL
);
2468 /* allocate clusters */
2469 if (class == MC_MBUF_CL
) {
2471 } else if (class == MC_MBUF_BIGCL
) {
2472 cl_class
= MC_BIGCL
;
2474 VERIFY(class == MC_MBUF_16KCL
);
2475 cl_class
= MC_16KCL
;
2477 needed
= mcache_alloc_ext(m_cache(cl_class
), &clp_list
, needed
, wait
);
2479 ASSERT(clp_list
== NULL
);
2483 needed
= mcache_alloc_ext(ref_cache
, &ref_list
, needed
, wait
);
2485 ASSERT(ref_list
== NULL
);
2490 * By this time "needed" is MIN(mbuf, cluster, ref). Any left
2491 * overs will get freed accordingly before we return to caller.
2493 for (cnum
= 0; cnum
< needed
; cnum
++) {
2496 m
= ms
= (struct mbuf
*)mp_list
;
2497 mp_list
= mp_list
->obj_next
;
2500 clp_list
= clp_list
->obj_next
;
2501 ((mcache_obj_t
*)cl
)->obj_next
= NULL
;
2503 rfa
= (struct ext_ref
*)ref_list
;
2504 ref_list
= ref_list
->obj_next
;
2505 ((mcache_obj_t
*)(void *)rfa
)->obj_next
= NULL
;
2508 * If auditing is enabled, construct the shadow mbuf
2509 * in the audit structure instead of in the actual one.
2510 * mbuf_cslab_audit() will take care of restoring the
2511 * contents after the integrity check.
2513 if (mclaudit
!= NULL
) {
2514 mcache_audit_t
*mca
, *cl_mca
;
2516 lck_mtx_lock(mbuf_mlock
);
2517 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2518 ms
= MCA_SAVED_MBUF_PTR(mca
);
2519 cl_mca
= mcl_audit_buf2mca(cl_class
,
2520 (mcache_obj_t
*)cl
);
2523 * Pair them up. Note that this is done at the time
2524 * the mbuf+cluster objects are constructed. This
2525 * information should be treated as "best effort"
2526 * debugging hint since more than one mbufs can refer
2527 * to a cluster. In that case, the cluster might not
2528 * be freed along with the mbuf it was paired with.
2530 mca
->mca_uptr
= cl_mca
;
2531 cl_mca
->mca_uptr
= mca
;
2533 ASSERT(mca
->mca_uflags
& MB_SCVALID
);
2534 ASSERT(!(cl_mca
->mca_uflags
& MB_SCVALID
));
2535 lck_mtx_unlock(mbuf_mlock
);
2537 /* Technically, they are in the freelist */
2541 mcache_set_pattern(MCACHE_FREE_PATTERN
, m
,
2542 m_maxsize(MC_MBUF
));
2544 if (class == MC_MBUF_CL
)
2545 size
= m_maxsize(MC_CL
);
2546 else if (class == MC_MBUF_BIGCL
)
2547 size
= m_maxsize(MC_BIGCL
);
2549 size
= m_maxsize(MC_16KCL
);
2551 mcache_set_pattern(MCACHE_FREE_PATTERN
, cl
,
2556 MBUF_INIT(ms
, 0, MT_FREE
);
2557 if (class == MC_MBUF_16KCL
) {
2558 MBUF_16KCL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2559 } else if (class == MC_MBUF_BIGCL
) {
2560 MBUF_BIGCL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2562 MBUF_CL_INIT(ms
, cl
, rfa
, 0, EXTF_COMPOSITE
);
2564 VERIFY(ms
->m_flags
== M_EXT
);
2565 VERIFY(MEXT_RFA(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2567 *list
= (mcache_obj_t
*)m
;
2568 (*list
)->obj_next
= NULL
;
2569 list
= *plist
= &(*list
)->obj_next
;
2574 * Free up what's left of the above.
2576 if (mp_list
!= NULL
)
2577 mcache_free_ext(m_cache(MC_MBUF
), mp_list
);
2578 if (clp_list
!= NULL
)
2579 mcache_free_ext(m_cache(cl_class
), clp_list
);
2580 if (ref_list
!= NULL
)
2581 mcache_free_ext(ref_cache
, ref_list
);
2583 lck_mtx_lock(mbuf_mlock
);
2584 if (num
> 0 || cnum
> 0) {
2585 m_total(class) += cnum
;
2586 VERIFY(m_total(class) <= m_maxlimit(class));
2587 m_alloc_cnt(class) += num
+ cnum
;
2589 if ((num
+ cnum
) < want
)
2590 m_fail_cnt(class) += (want
- (num
+ cnum
));
2591 lck_mtx_unlock(mbuf_mlock
);
2593 return (num
+ cnum
);
2597 * Common de-allocator for composite objects called by the CPU cache
2598 * layer when one or more elements need to be returned to the appropriate
2602 mbuf_cslab_free(void *arg
, mcache_obj_t
*list
, int purged
)
2604 mbuf_class_t
class = (mbuf_class_t
)arg
;
2608 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2610 lck_mtx_lock(mbuf_mlock
);
2612 num
= cslab_free(class, list
, purged
);
2613 m_free_cnt(class) += num
;
2615 if ((w
= mb_waiters
) > 0)
2618 lck_mtx_unlock(mbuf_mlock
);
2621 wakeup(mb_waitchan
);
2625 * Common auditor for composite objects called by the CPU cache layer
2626 * during an allocation or free request. For the former, this is called
2627 * after the objects are obtained from either the bucket or slab layer
2628 * and before they are returned to the caller. For the latter, this is
2629 * called immediately during free and before placing the objects into
2630 * the bucket or slab layer.
2633 mbuf_cslab_audit(void *arg
, mcache_obj_t
*list
, boolean_t alloc
)
2635 mbuf_class_t
class = (mbuf_class_t
)arg
, cl_class
;
2636 mcache_audit_t
*mca
;
2637 struct mbuf
*m
, *ms
;
2638 mcl_slab_t
*clsp
, *nsp
;
2642 ASSERT(MBUF_CLASS_VALID(class) && MBUF_CLASS_COMPOSITE(class));
2643 if (class == MC_MBUF_CL
)
2645 else if (class == MC_MBUF_BIGCL
)
2646 cl_class
= MC_BIGCL
;
2648 cl_class
= MC_16KCL
;
2649 cl_size
= m_maxsize(cl_class
);
2651 while ((m
= ms
= (struct mbuf
*)list
) != NULL
) {
2652 lck_mtx_lock(mbuf_mlock
);
2653 /* Do the mbuf sanity checks and record its transaction */
2654 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
2655 mcl_audit_mbuf(mca
, m
, TRUE
, alloc
);
2657 mcache_buffer_log(mca
, m
, m_cache(class), &mb_start
);
2660 mca
->mca_uflags
|= MB_COMP_INUSE
;
2662 mca
->mca_uflags
&= ~MB_COMP_INUSE
;
2665 * Use the shadow mbuf in the audit structure if we are
2666 * freeing, since the contents of the actual mbuf has been
2667 * pattern-filled by the above call to mcl_audit_mbuf().
2669 if (!alloc
&& mclverify
)
2670 ms
= MCA_SAVED_MBUF_PTR(mca
);
2672 /* Do the cluster sanity checks and record its transaction */
2673 cl
= ms
->m_ext
.ext_buf
;
2674 clsp
= slab_get(cl
);
2675 VERIFY(ms
->m_flags
== M_EXT
&& cl
!= NULL
);
2676 VERIFY(MEXT_RFA(ms
) != NULL
&& MBUF_IS_COMPOSITE(ms
));
2677 if (class == MC_MBUF_CL
)
2678 VERIFY(clsp
->sl_refcnt
>= 1 &&
2679 clsp
->sl_refcnt
<= NCLPG
);
2681 VERIFY(clsp
->sl_refcnt
>= 1 &&
2682 clsp
->sl_refcnt
<= NBCLPG
);
2684 if (class == MC_MBUF_16KCL
) {
2686 for (nsp
= clsp
, k
= 1; k
< NSLABSP16KB
; k
++) {
2688 /* Next slab must already be present */
2689 VERIFY(nsp
!= NULL
);
2690 VERIFY(nsp
->sl_refcnt
== 1);
2695 mca
= mcl_audit_buf2mca(cl_class
, cl
);
2696 mcl_audit_cluster(mca
, cl
, cl_size
, alloc
, FALSE
);
2698 mcache_buffer_log(mca
, cl
, m_cache(class), &mb_start
);
2701 mca
->mca_uflags
|= MB_COMP_INUSE
;
2703 mca
->mca_uflags
&= ~MB_COMP_INUSE
;
2704 lck_mtx_unlock(mbuf_mlock
);
2706 list
= list
->obj_next
;
2711 * Allocate some number of mbuf clusters and place on cluster freelist.
2714 m_clalloc(const u_int32_t num
, const int wait
, const u_int32_t bufsize
)
2718 int numpages
= 0, large_buffer
;
2719 vm_offset_t page
= 0;
2720 mcache_audit_t
*mca_list
= NULL
;
2721 mcache_obj_t
*con_list
= NULL
;
2725 /* Set if a buffer allocation needs allocation of multiple pages */
2726 large_buffer
= ((bufsize
== m_maxsize(MC_16KCL
)) &&
2727 PAGE_SIZE
< M16KCLBYTES
);
2728 VERIFY(bufsize
== m_maxsize(MC_BIGCL
) ||
2729 bufsize
== m_maxsize(MC_16KCL
));
2731 VERIFY((bufsize
== PAGE_SIZE
) ||
2732 (bufsize
> PAGE_SIZE
&& bufsize
== m_maxsize(MC_16KCL
)));
2734 if (bufsize
== m_size(MC_BIGCL
))
2739 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2742 * Multiple threads may attempt to populate the cluster map one
2743 * after another. Since we drop the lock below prior to acquiring
2744 * the physical page(s), our view of the cluster map may no longer
2745 * be accurate, and we could end up over-committing the pages beyond
2746 * the maximum allowed for each class. To prevent it, this entire
2747 * operation (including the page mapping) is serialized.
2749 while (mb_clalloc_busy
) {
2750 mb_clalloc_waiters
++;
2751 (void) msleep(mb_clalloc_waitchan
, mbuf_mlock
,
2752 (PZERO
-1), "m_clalloc", NULL
);
2753 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2756 /* We are busy now; tell everyone else to go away */
2757 mb_clalloc_busy
= TRUE
;
2760 * Honor the caller's wish to block or not block. We have a way
2761 * to grow the pool asynchronously using the mbuf worker thread.
2763 i
= m_howmany(num
, bufsize
);
2764 if (i
== 0 || (wait
& M_DONTWAIT
))
2767 lck_mtx_unlock(mbuf_mlock
);
2769 size
= round_page(i
* bufsize
);
2770 page
= kmem_mb_alloc(mb_map
, size
, large_buffer
);
2773 * If we did ask for "n" 16KB physically contiguous chunks
2774 * and didn't get them, then please try again without this
2777 if (large_buffer
&& page
== 0)
2778 page
= kmem_mb_alloc(mb_map
, size
, 0);
2781 if (bufsize
== m_maxsize(MC_BIGCL
)) {
2782 /* Try for 1 page if failed */
2784 page
= kmem_mb_alloc(mb_map
, size
, 0);
2788 lck_mtx_lock(mbuf_mlock
);
2793 VERIFY(IS_P2ALIGNED(page
, PAGE_SIZE
));
2794 numpages
= size
/ PAGE_SIZE
;
2796 /* If auditing is enabled, allocate the audit structures now */
2797 if (mclaudit
!= NULL
) {
2801 * Yes, I realize this is a waste of memory for clusters
2802 * that never get transformed into mbufs, as we may end
2803 * up with NMBPG-1 unused audit structures per cluster.
2804 * But doing so tremendously simplifies the allocation
2805 * strategy, since at this point we are not holding the
2806 * mbuf lock and the caller is okay to be blocked.
2808 if (bufsize
== PAGE_SIZE
) {
2809 needed
= numpages
* NMBPG
;
2811 i
= mcache_alloc_ext(mcl_audit_con_cache
,
2812 &con_list
, needed
, MCR_SLEEP
);
2814 VERIFY(con_list
!= NULL
&& i
== needed
);
2817 * if multiple 4K pages are being used for a
2820 needed
= numpages
/ NSLABSP16KB
;
2823 i
= mcache_alloc_ext(mcache_audit_cache
,
2824 (mcache_obj_t
**)&mca_list
, needed
, MCR_SLEEP
);
2826 VERIFY(mca_list
!= NULL
&& i
== needed
);
2829 lck_mtx_lock(mbuf_mlock
);
2831 for (i
= 0; i
< numpages
; i
++, page
+= PAGE_SIZE
) {
2833 ((unsigned char *)page
- mbutl
) >> PAGE_SHIFT
;
2834 ppnum_t new_page
= pmap_find_phys(kernel_pmap
, page
);
2837 * If there is a mapper the appropriate I/O page is
2838 * returned; zero out the page to discard its past
2839 * contents to prevent exposing leftover kernel memory.
2841 VERIFY(offset
< mcl_pages
);
2842 if (mcl_paddr_base
!= 0) {
2843 bzero((void *)(uintptr_t) page
, PAGE_SIZE
);
2844 new_page
= IOMapperInsertPage(mcl_paddr_base
,
2847 mcl_paddr
[offset
] = new_page
;
2849 /* Pattern-fill this fresh page */
2851 mcache_set_pattern(MCACHE_FREE_PATTERN
,
2852 (caddr_t
)page
, PAGE_SIZE
);
2854 if (bufsize
== PAGE_SIZE
) {
2856 /* One for the entire page */
2857 sp
= slab_get((void *)page
);
2858 if (mclaudit
!= NULL
) {
2859 mcl_audit_init((void *)page
,
2860 &mca_list
, &con_list
,
2861 AUDIT_CONTENTS_SIZE
, NMBPG
);
2863 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_flags
== 0);
2864 slab_init(sp
, class, SLF_MAPPED
, (void *)page
,
2865 (void *)page
, PAGE_SIZE
, 0, 1);
2866 buf
= (mcache_obj_t
*)page
;
2867 buf
->obj_next
= NULL
;
2869 /* Insert this slab */
2870 slab_insert(sp
, class);
2872 /* Update stats now since slab_get drops the lock */
2875 VERIFY(m_total(class) <= m_maxlimit(class));
2876 if (class == MC_BIGCL
) {
2877 mbstat
.m_bigclfree
= m_infree(MC_BIGCL
) +
2878 m_infree(MC_MBUF_BIGCL
);
2879 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
2882 } else if ((bufsize
> PAGE_SIZE
) &&
2883 (i
% NSLABSP16KB
) == 0) {
2884 union m16kcluster
*m16kcl
= (union m16kcluster
*)page
;
2888 /* One for the entire 16KB */
2889 sp
= slab_get(m16kcl
);
2890 if (mclaudit
!= NULL
)
2891 mcl_audit_init(m16kcl
, &mca_list
, NULL
, 0, 1);
2893 VERIFY(sp
->sl_refcnt
== 0 && sp
->sl_flags
== 0);
2894 slab_init(sp
, MC_16KCL
, SLF_MAPPED
,
2895 m16kcl
, m16kcl
, bufsize
, 0, 1);
2896 m16kcl
->m16kcl_next
= NULL
;
2899 * 2nd-Nth page's slab is part of the first one,
2900 * where N is NSLABSP16KB.
2902 for (k
= 1; k
< NSLABSP16KB
; k
++) {
2903 nsp
= slab_get(((union mbigcluster
*)page
) + k
);
2904 VERIFY(nsp
->sl_refcnt
== 0 &&
2905 nsp
->sl_flags
== 0);
2906 slab_init(nsp
, MC_16KCL
,
2907 SLF_MAPPED
| SLF_PARTIAL
,
2908 m16kcl
, NULL
, 0, 0, 0);
2910 /* Insert this slab */
2911 slab_insert(sp
, MC_16KCL
);
2913 /* Update stats now since slab_get drops the lock */
2914 ++m_infree(MC_16KCL
);
2915 ++m_total(MC_16KCL
);
2916 VERIFY(m_total(MC_16KCL
) <= m_maxlimit(MC_16KCL
));
2920 VERIFY(mca_list
== NULL
&& con_list
== NULL
);
2922 if (!mb_peak_newreport
&& mbuf_report_usage(class))
2923 mb_peak_newreport
= TRUE
;
2925 /* We're done; let others enter */
2926 mb_clalloc_busy
= FALSE
;
2927 if (mb_clalloc_waiters
> 0) {
2928 mb_clalloc_waiters
= 0;
2929 wakeup(mb_clalloc_waitchan
);
2934 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2936 /* We're done; let others enter */
2937 mb_clalloc_busy
= FALSE
;
2938 if (mb_clalloc_waiters
> 0) {
2939 mb_clalloc_waiters
= 0;
2940 wakeup(mb_clalloc_waitchan
);
2944 * When non-blocking we kick a thread if we have to grow the
2945 * pool or if the number of free clusters is less than requested.
2947 if (class == MC_BIGCL
) {
2950 * Remember total number of 4KB clusters needed
2953 i
+= m_total(MC_BIGCL
);
2954 if (i
> mbuf_expand_big
) {
2955 mbuf_expand_big
= i
;
2956 if (mbuf_worker_ready
)
2957 wakeup((caddr_t
)&mbuf_worker_run
);
2961 if (m_infree(MC_BIGCL
) >= num
)
2966 * Remember total number of 16KB clusters needed
2969 i
+= m_total(MC_16KCL
);
2970 if (i
> mbuf_expand_16k
) {
2971 mbuf_expand_16k
= i
;
2972 if (mbuf_worker_ready
)
2973 wakeup((caddr_t
)&mbuf_worker_run
);
2977 if (m_infree(MC_16KCL
) >= num
)
2984 * Populate the global freelist of the corresponding buffer class.
2987 freelist_populate(mbuf_class_t
class, unsigned int num
, int wait
)
2989 mcache_obj_t
*o
= NULL
;
2990 int i
, numpages
= 0, count
;
2991 mbuf_class_t super_class
;
2993 VERIFY(class == MC_MBUF
|| class == MC_CL
|| class == MC_BIGCL
||
2996 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
2998 VERIFY(PAGE_SIZE
== m_maxsize(MC_BIGCL
) ||
2999 PAGE_SIZE
== m_maxsize(MC_16KCL
));
3001 if (m_maxsize(class) >= PAGE_SIZE
)
3002 return(m_clalloc(num
, wait
, m_maxsize(class)) != 0);
3005 * The rest of the function will allocate pages and will slice
3006 * them up into the right size
3009 numpages
= (num
* m_size(class) + PAGE_SIZE
- 1) / PAGE_SIZE
;
3011 /* Currently assume that pages are 4K or 16K */
3012 if (PAGE_SIZE
== m_maxsize(MC_BIGCL
))
3013 super_class
= MC_BIGCL
;
3015 super_class
= MC_16KCL
;
3017 i
= m_clalloc(numpages
, wait
, m_maxsize(super_class
));
3019 /* Respect the minimum limit of super class */
3020 if (m_total(super_class
) == m_maxlimit(super_class
) &&
3021 m_infree(super_class
) <= m_minlimit(super_class
))
3022 if (wait
& MCR_COMP
)
3025 /* how many objects will we cut the page into? */
3026 int numobj
= PAGE_SIZE
/ m_maxsize(class);
3028 for (count
= 0; count
< numpages
; count
++) {
3029 /* respect totals, minlimit, maxlimit */
3030 if (m_total(super_class
) <= m_minlimit(super_class
) ||
3031 m_total(class) >= m_maxlimit(class))
3034 if ((o
= slab_alloc(super_class
, wait
)) == NULL
)
3037 struct mbuf
*m
= (struct mbuf
*)o
;
3038 union mcluster
*c
= (union mcluster
*)o
;
3039 union mbigcluster
*mbc
= (union mbigcluster
*)o
;
3040 mcl_slab_t
*sp
= slab_get(o
);
3041 mcache_audit_t
*mca
= NULL
;
3044 * since one full page will be converted to MC_MBUF or
3045 * MC_CL, verify that the reference count will match that
3048 VERIFY(sp
->sl_refcnt
== 1 && slab_is_detached(sp
));
3049 VERIFY((sp
->sl_flags
& (SLF_MAPPED
| SLF_PARTIAL
)) == SLF_MAPPED
);
3051 * Make sure that the cluster is unmolested
3055 mca
= mcl_audit_buf2mca(super_class
,
3057 mcache_audit_free_verify(mca
,
3058 (mcache_obj_t
*)o
, 0, m_maxsize(super_class
));
3061 /* Reinitialize it as an mbuf or 2K or 4K slab */
3062 slab_init(sp
, class, sp
->sl_flags
,
3063 sp
->sl_base
, NULL
, PAGE_SIZE
, 0, numobj
);
3065 VERIFY(sp
->sl_head
== NULL
);
3067 VERIFY(m_total(super_class
) >= 1);
3068 m_total(super_class
)--;
3070 if (super_class
== MC_BIGCL
)
3071 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
3073 m_total(class) += numobj
;
3074 m_infree(class) += numobj
;
3076 if (!mb_peak_newreport
&& mbuf_report_usage(class))
3077 mb_peak_newreport
= TRUE
;
3080 if (class == MC_MBUF
) {
3081 mbstat
.m_mbufs
= m_total(MC_MBUF
);
3082 mtype_stat_add(MT_FREE
, NMBPG
);
3085 * If auditing is enabled, construct the
3086 * shadow mbuf in the audit structure
3087 * instead of the actual one.
3088 * mbuf_slab_audit() will take care of
3089 * restoring the contents after the
3092 if (mclaudit
!= NULL
) {
3094 mca
= mcl_audit_buf2mca(MC_MBUF
,
3096 ms
= MCA_SAVED_MBUF_PTR(mca
);
3097 ms
->m_type
= MT_FREE
;
3099 m
->m_type
= MT_FREE
;
3101 m
->m_next
= sp
->sl_head
;
3102 sp
->sl_head
= (void *)m
++;
3104 } else if (class == MC_CL
) { /* MC_CL */
3106 m_infree(MC_CL
) + m_infree(MC_MBUF_CL
);
3107 mbstat
.m_clusters
= m_total(MC_CL
);
3109 c
->mcl_next
= sp
->sl_head
;
3110 sp
->sl_head
= (void *)c
++;
3113 VERIFY(class == MC_BIGCL
);
3114 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
3115 mbstat
.m_bigclfree
= m_infree(MC_BIGCL
) +
3116 m_infree(MC_MBUF_BIGCL
);
3118 mbc
->mbc_next
= sp
->sl_head
;
3119 sp
->sl_head
= (void *)mbc
++;
3123 /* Insert into the mbuf or 2k or 4k slab list */
3124 slab_insert(sp
, class);
3126 if ((i
= mb_waiters
) > 0)
3129 wakeup(mb_waitchan
);
3131 return (count
!= 0);
3135 * For each class, initialize the freelist to hold m_minlimit() objects.
3138 freelist_init(mbuf_class_t
class)
3140 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3142 VERIFY(class == MC_CL
|| class == MC_BIGCL
);
3143 VERIFY(m_total(class) == 0);
3144 VERIFY(m_minlimit(class) > 0);
3146 while (m_total(class) < m_minlimit(class))
3147 (void) freelist_populate(class, m_minlimit(class), M_WAIT
);
3149 VERIFY(m_total(class) >= m_minlimit(class));
3153 * (Inaccurately) check if it might be worth a trip back to the
3154 * mcache layer due the availability of objects there. We'll
3155 * end up back here if there's nothing up there.
3158 mbuf_cached_above(mbuf_class_t
class, int wait
)
3162 if (wait
& MCR_COMP
)
3163 return (!mcache_bkt_isempty(m_cache(MC_MBUF_CL
)) ||
3164 !mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL
)));
3168 if (wait
& MCR_COMP
)
3169 return (!mcache_bkt_isempty(m_cache(MC_MBUF_CL
)));
3173 if (wait
& MCR_COMP
)
3174 return (!mcache_bkt_isempty(m_cache(MC_MBUF_BIGCL
)));
3178 if (wait
& MCR_COMP
)
3179 return (!mcache_bkt_isempty(m_cache(MC_MBUF_16KCL
)));
3192 return (!mcache_bkt_isempty(m_cache(class)));
3196 * If possible, convert constructed objects to raw ones.
3199 mbuf_steal(mbuf_class_t
class, unsigned int num
)
3201 mcache_obj_t
*top
= NULL
;
3202 mcache_obj_t
**list
= &top
;
3203 unsigned int tot
= 0;
3205 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3217 /* Get the required number of constructed objects if possible */
3218 if (m_infree(class) > m_minlimit(class)) {
3219 tot
= cslab_alloc(class, &list
,
3220 MIN(num
, m_infree(class)));
3223 /* And destroy them to get back the raw objects */
3225 (void) cslab_free(class, top
, 1);
3233 return (tot
== num
);
3237 m_reclaim(mbuf_class_t
class, unsigned int num
, boolean_t comp
)
3241 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
3243 VERIFY(m_total(MC_CL
) <= m_maxlimit(MC_CL
));
3244 VERIFY(m_total(MC_BIGCL
) <= m_maxlimit(MC_BIGCL
));
3245 VERIFY(m_total(MC_16KCL
) <= m_maxlimit(MC_16KCL
));
3248 * This logic can be made smarter; for now, simply mark
3249 * all other related classes as potential victims.
3253 m_wantpurge(MC_CL
)++;
3254 m_wantpurge(MC_BIGCL
)++;
3255 m_wantpurge(MC_MBUF_CL
)++;
3256 m_wantpurge(MC_MBUF_BIGCL
)++;
3260 m_wantpurge(MC_MBUF
)++;
3261 m_wantpurge(MC_BIGCL
)++;
3262 m_wantpurge(MC_MBUF_BIGCL
)++;
3264 m_wantpurge(MC_MBUF_CL
)++;
3268 m_wantpurge(MC_MBUF
)++;
3269 m_wantpurge(MC_CL
)++;
3270 m_wantpurge(MC_MBUF_CL
)++;
3272 m_wantpurge(MC_MBUF_BIGCL
)++;
3277 m_wantpurge(MC_MBUF_16KCL
)++;
3286 * Run through each marked class and check if we really need to
3287 * purge (and therefore temporarily disable) the per-CPU caches
3288 * layer used by the class. If so, remember the classes since
3289 * we are going to drop the lock below prior to purging.
3291 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
3292 if (m_wantpurge(m
) > 0) {
3295 * Try hard to steal the required number of objects
3296 * from the freelist of other mbuf classes. Only
3297 * purge and disable the per-CPU caches layer when
3298 * we don't have enough; it's the last resort.
3300 if (!mbuf_steal(m
, num
))
3305 lck_mtx_unlock(mbuf_mlock
);
3308 /* signal the domains to drain */
3309 net_drain_domains();
3311 /* Sigh; we have no other choices but to ask mcache to purge */
3312 for (m
= 0; m
< NELEM(mbuf_table
); m
++) {
3313 if ((bmap
& (1 << m
)) &&
3314 mcache_purge_cache(m_cache(m
), TRUE
)) {
3315 lck_mtx_lock(mbuf_mlock
);
3318 lck_mtx_unlock(mbuf_mlock
);
3323 * Request mcache to reap extra elements from all of its caches;
3324 * note that all reaps are serialized and happen only at a fixed
3329 lck_mtx_lock(mbuf_mlock
);
3332 static inline struct mbuf
*
3333 m_get_common(int wait
, short type
, int hdr
)
3336 int mcflags
= MSLEEPF(wait
);
3338 /* Is this due to a non-blocking retry? If so, then try harder */
3339 if (mcflags
& MCR_NOSLEEP
)
3340 mcflags
|= MCR_TRYHARD
;
3342 m
= mcache_alloc(m_cache(MC_MBUF
), mcflags
);
3344 MBUF_INIT(m
, hdr
, type
);
3345 mtype_stat_inc(type
);
3346 mtype_stat_dec(MT_FREE
);
3348 if (hdr
&& mac_init_mbuf(m
, wait
) != 0) {
3352 #endif /* MAC_NET */
3358 * Space allocation routines; these are also available as macros
3359 * for critical paths.
3361 #define _M_GET(wait, type) m_get_common(wait, type, 0)
3362 #define _M_GETHDR(wait, type) m_get_common(wait, type, 1)
3363 #define _M_RETRY(wait, type) _M_GET(wait, type)
3364 #define _M_RETRYHDR(wait, type) _M_GETHDR(wait, type)
3365 #define _MGET(m, how, type) ((m) = _M_GET(how, type))
3366 #define _MGETHDR(m, how, type) ((m) = _M_GETHDR(how, type))
3369 m_get(int wait
, int type
)
3371 return (_M_GET(wait
, type
));
3375 m_gethdr(int wait
, int type
)
3377 return (_M_GETHDR(wait
, type
));
3381 m_retry(int wait
, int type
)
3383 return (_M_RETRY(wait
, type
));
3387 m_retryhdr(int wait
, int type
)
3389 return (_M_RETRYHDR(wait
, type
));
3393 m_getclr(int wait
, int type
)
3397 _MGET(m
, wait
, type
);
3399 bzero(MTOD(m
, caddr_t
), MLEN
);
3404 m_free(struct mbuf
*m
)
3406 struct mbuf
*n
= m
->m_next
;
3408 if (m
->m_type
== MT_FREE
)
3409 panic("m_free: freeing an already freed mbuf");
3411 if (m
->m_flags
& M_PKTHDR
) {
3412 /* Check for scratch area overflow */
3413 m_redzone_verify(m
);
3414 /* Free the aux data and tags if there is any */
3415 m_tag_delete_chain(m
, NULL
);
3418 if (m
->m_flags
& M_EXT
) {
3420 u_int32_t composite
;
3422 refcnt
= m_decref(m
);
3423 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
3424 if (refcnt
== 0 && !composite
) {
3425 if (m
->m_ext
.ext_free
== NULL
) {
3426 mcache_free(m_cache(MC_CL
), m
->m_ext
.ext_buf
);
3427 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3428 mcache_free(m_cache(MC_BIGCL
),
3430 } else if (m
->m_ext
.ext_free
== m_16kfree
) {
3431 mcache_free(m_cache(MC_16KCL
),
3434 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
3435 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
3437 mcache_free(ref_cache
, MEXT_RFA(m
));
3439 } else if (refcnt
== 0 && composite
) {
3440 VERIFY(m
->m_type
!= MT_FREE
);
3442 mtype_stat_dec(m
->m_type
);
3443 mtype_stat_inc(MT_FREE
);
3445 m
->m_type
= MT_FREE
;
3448 m
->m_next
= m
->m_nextpkt
= NULL
;
3450 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
3452 /* "Free" into the intermediate cache */
3453 if (m
->m_ext
.ext_free
== NULL
) {
3454 mcache_free(m_cache(MC_MBUF_CL
), m
);
3455 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3456 mcache_free(m_cache(MC_MBUF_BIGCL
), m
);
3458 VERIFY(m
->m_ext
.ext_free
== m_16kfree
);
3459 mcache_free(m_cache(MC_MBUF_16KCL
), m
);
3465 if (m
->m_type
!= MT_FREE
) {
3466 mtype_stat_dec(m
->m_type
);
3467 mtype_stat_inc(MT_FREE
);
3470 m
->m_type
= MT_FREE
;
3471 m
->m_flags
= m
->m_len
= 0;
3472 m
->m_next
= m
->m_nextpkt
= NULL
;
3474 mcache_free(m_cache(MC_MBUF
), m
);
3479 __private_extern__
struct mbuf
*
3480 m_clattach(struct mbuf
*m
, int type
, caddr_t extbuf
,
3481 void (*extfree
)(caddr_t
, u_int
, caddr_t
), u_int extsize
, caddr_t extarg
,
3484 struct ext_ref
*rfa
= NULL
;
3486 if (m
== NULL
&& (m
= _M_GETHDR(wait
, type
)) == NULL
)
3489 if (m
->m_flags
& M_EXT
) {
3491 u_int32_t composite
;
3493 refcnt
= m_decref(m
);
3494 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
3495 if (refcnt
== 0 && !composite
) {
3496 if (m
->m_ext
.ext_free
== NULL
) {
3497 mcache_free(m_cache(MC_CL
), m
->m_ext
.ext_buf
);
3498 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3499 mcache_free(m_cache(MC_BIGCL
),
3501 } else if (m
->m_ext
.ext_free
== m_16kfree
) {
3502 mcache_free(m_cache(MC_16KCL
),
3505 (*(m
->m_ext
.ext_free
))(m
->m_ext
.ext_buf
,
3506 m
->m_ext
.ext_size
, m
->m_ext
.ext_arg
);
3508 /* Re-use the reference structure */
3510 } else if (refcnt
== 0 && composite
) {
3511 VERIFY(m
->m_type
!= MT_FREE
);
3513 mtype_stat_dec(m
->m_type
);
3514 mtype_stat_inc(MT_FREE
);
3516 m
->m_type
= MT_FREE
;
3519 m
->m_next
= m
->m_nextpkt
= NULL
;
3521 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
3523 /* "Free" into the intermediate cache */
3524 if (m
->m_ext
.ext_free
== NULL
) {
3525 mcache_free(m_cache(MC_MBUF_CL
), m
);
3526 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
3527 mcache_free(m_cache(MC_MBUF_BIGCL
), m
);
3529 VERIFY(m
->m_ext
.ext_free
== m_16kfree
);
3530 mcache_free(m_cache(MC_MBUF_16KCL
), m
);
3533 * Allocate a new mbuf, since we didn't divorce
3534 * the composite mbuf + cluster pair above.
3536 if ((m
= _M_GETHDR(wait
, type
)) == NULL
)
3542 (rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
) {
3547 MEXT_INIT(m
, extbuf
, extsize
, extfree
, extarg
, rfa
, 1, 0);
3553 * Perform `fast' allocation mbuf clusters from a cache of recently-freed
3554 * clusters. (If the cache is empty, new clusters are allocated en-masse.)
3557 m_getcl(int wait
, int type
, int flags
)
3560 int mcflags
= MSLEEPF(wait
);
3561 int hdr
= (flags
& M_PKTHDR
);
3563 /* Is this due to a non-blocking retry? If so, then try harder */
3564 if (mcflags
& MCR_NOSLEEP
)
3565 mcflags
|= MCR_TRYHARD
;
3567 m
= mcache_alloc(m_cache(MC_MBUF_CL
), mcflags
);
3570 struct ext_ref
*rfa
;
3573 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
3574 cl
= m
->m_ext
.ext_buf
;
3577 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
3578 VERIFY(MBUF_IS_COMPOSITE(m
) && m
->m_ext
.ext_free
== NULL
);
3580 flag
= MEXT_FLAGS(m
);
3582 MBUF_INIT(m
, hdr
, type
);
3583 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
3585 mtype_stat_inc(type
);
3586 mtype_stat_dec(MT_FREE
);
3588 if (hdr
&& mac_init_mbuf(m
, wait
) != 0) {
3592 #endif /* MAC_NET */
3597 /* m_mclget() add an mbuf cluster to a normal mbuf */
3599 m_mclget(struct mbuf
*m
, int wait
)
3601 struct ext_ref
*rfa
;
3603 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3606 m
->m_ext
.ext_buf
= m_mclalloc(wait
);
3607 if (m
->m_ext
.ext_buf
!= NULL
) {
3608 MBUF_CL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3610 mcache_free(ref_cache
, rfa
);
3615 /* Allocate an mbuf cluster */
3617 m_mclalloc(int wait
)
3619 int mcflags
= MSLEEPF(wait
);
3621 /* Is this due to a non-blocking retry? If so, then try harder */
3622 if (mcflags
& MCR_NOSLEEP
)
3623 mcflags
|= MCR_TRYHARD
;
3625 return (mcache_alloc(m_cache(MC_CL
), mcflags
));
3628 /* Free an mbuf cluster */
3630 m_mclfree(caddr_t p
)
3632 mcache_free(m_cache(MC_CL
), p
);
3636 * mcl_hasreference() checks if a cluster of an mbuf is referenced by
3637 * another mbuf; see comments in m_incref() regarding EXTF_READONLY.
3640 m_mclhasreference(struct mbuf
*m
)
3642 if (!(m
->m_flags
& M_EXT
))
3645 ASSERT(MEXT_RFA(m
) != NULL
);
3647 return ((MEXT_FLAGS(m
) & EXTF_READONLY
) ? 1 : 0);
3650 __private_extern__ caddr_t
3651 m_bigalloc(int wait
)
3653 int mcflags
= MSLEEPF(wait
);
3655 /* Is this due to a non-blocking retry? If so, then try harder */
3656 if (mcflags
& MCR_NOSLEEP
)
3657 mcflags
|= MCR_TRYHARD
;
3659 return (mcache_alloc(m_cache(MC_BIGCL
), mcflags
));
3662 __private_extern__
void
3663 m_bigfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
3665 mcache_free(m_cache(MC_BIGCL
), p
);
3668 /* m_mbigget() add an 4KB mbuf cluster to a normal mbuf */
3669 __private_extern__
struct mbuf
*
3670 m_mbigget(struct mbuf
*m
, int wait
)
3672 struct ext_ref
*rfa
;
3674 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3677 m
->m_ext
.ext_buf
= m_bigalloc(wait
);
3678 if (m
->m_ext
.ext_buf
!= NULL
) {
3679 MBUF_BIGCL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3681 mcache_free(ref_cache
, rfa
);
3686 __private_extern__ caddr_t
3687 m_16kalloc(int wait
)
3689 int mcflags
= MSLEEPF(wait
);
3691 /* Is this due to a non-blocking retry? If so, then try harder */
3692 if (mcflags
& MCR_NOSLEEP
)
3693 mcflags
|= MCR_TRYHARD
;
3695 return (mcache_alloc(m_cache(MC_16KCL
), mcflags
));
3698 __private_extern__
void
3699 m_16kfree(caddr_t p
, __unused u_int size
, __unused caddr_t arg
)
3701 mcache_free(m_cache(MC_16KCL
), p
);
3704 /* m_m16kget() add a 16KB mbuf cluster to a normal mbuf */
3705 __private_extern__
struct mbuf
*
3706 m_m16kget(struct mbuf
*m
, int wait
)
3708 struct ext_ref
*rfa
;
3710 if ((rfa
= mcache_alloc(ref_cache
, MSLEEPF(wait
))) == NULL
)
3713 m
->m_ext
.ext_buf
= m_16kalloc(wait
);
3714 if (m
->m_ext
.ext_buf
!= NULL
) {
3715 MBUF_16KCL_INIT(m
, m
->m_ext
.ext_buf
, rfa
, 1, 0);
3717 mcache_free(ref_cache
, rfa
);
3723 * "Move" mbuf pkthdr from "from" to "to".
3724 * "from" must have M_PKTHDR set, and "to" must be empty.
3727 m_copy_pkthdr(struct mbuf
*to
, struct mbuf
*from
)
3729 VERIFY(from
->m_flags
& M_PKTHDR
);
3731 /* Check for scratch area overflow */
3732 m_redzone_verify(from
);
3734 if (to
->m_flags
& M_PKTHDR
) {
3735 /* Check for scratch area overflow */
3736 m_redzone_verify(to
);
3737 /* We will be taking over the tags of 'to' */
3738 m_tag_delete_chain(to
, NULL
);
3740 to
->m_pkthdr
= from
->m_pkthdr
; /* especially tags */
3741 m_classifier_init(from
, 0); /* purge classifier info */
3742 m_tag_init(from
, 1); /* purge all tags from src */
3743 m_scratch_init(from
); /* clear src scratch area */
3744 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
3745 if ((to
->m_flags
& M_EXT
) == 0)
3746 to
->m_data
= to
->m_pktdat
;
3747 m_redzone_init(to
); /* setup red zone on dst */
3751 * Duplicate "from"'s mbuf pkthdr in "to".
3752 * "from" must have M_PKTHDR set, and "to" must be empty.
3753 * In particular, this does a deep copy of the packet tags.
3756 m_dup_pkthdr(struct mbuf
*to
, struct mbuf
*from
, int how
)
3758 VERIFY(from
->m_flags
& M_PKTHDR
);
3760 /* Check for scratch area overflow */
3761 m_redzone_verify(from
);
3763 if (to
->m_flags
& M_PKTHDR
) {
3764 /* Check for scratch area overflow */
3765 m_redzone_verify(to
);
3766 /* We will be taking over the tags of 'to' */
3767 m_tag_delete_chain(to
, NULL
);
3769 to
->m_flags
= (from
->m_flags
& M_COPYFLAGS
) | (to
->m_flags
& M_EXT
);
3770 if ((to
->m_flags
& M_EXT
) == 0)
3771 to
->m_data
= to
->m_pktdat
;
3772 to
->m_pkthdr
= from
->m_pkthdr
;
3773 m_redzone_init(to
); /* setup red zone on dst */
3774 m_tag_init(to
, 0); /* preserve dst static tags */
3775 return (m_tag_copy_chain(to
, from
, how
));
3779 m_copy_pftag(struct mbuf
*to
, struct mbuf
*from
)
3781 to
->m_pkthdr
.pf_mtag
= from
->m_pkthdr
.pf_mtag
;
3783 to
->m_pkthdr
.pf_mtag
.pftag_hdr
= NULL
;
3784 to
->m_pkthdr
.pf_mtag
.pftag_flags
&= ~(PF_TAG_HDR_INET
|PF_TAG_HDR_INET6
);
3789 m_classifier_init(struct mbuf
*m
, uint32_t pktf_mask
)
3791 VERIFY(m
->m_flags
& M_PKTHDR
);
3793 m
->m_pkthdr
.pkt_proto
= 0;
3794 m
->m_pkthdr
.pkt_flowsrc
= 0;
3795 m
->m_pkthdr
.pkt_flowid
= 0;
3796 m
->m_pkthdr
.pkt_flags
&= pktf_mask
; /* caller-defined mask */
3797 /* preserve service class and interface info for loopback packets */
3798 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
))
3799 (void) m_set_service_class(m
, MBUF_SC_BE
);
3800 if (!(m
->m_pkthdr
.pkt_flags
& PKTF_IFAINFO
))
3801 m
->m_pkthdr
.pkt_ifainfo
= 0;
3803 m
->m_pkthdr
.pkt_bwseq
= 0;
3804 #endif /* MEASURE_BW */
3805 m
->m_pkthdr
.pkt_enqueue_ts
= 0;
3809 m_copy_classifier(struct mbuf
*to
, struct mbuf
*from
)
3811 VERIFY(to
->m_flags
& M_PKTHDR
);
3812 VERIFY(from
->m_flags
& M_PKTHDR
);
3814 to
->m_pkthdr
.pkt_proto
= from
->m_pkthdr
.pkt_proto
;
3815 to
->m_pkthdr
.pkt_flowsrc
= from
->m_pkthdr
.pkt_flowsrc
;
3816 to
->m_pkthdr
.pkt_flowid
= from
->m_pkthdr
.pkt_flowid
;
3817 to
->m_pkthdr
.pkt_flags
= from
->m_pkthdr
.pkt_flags
;
3818 (void) m_set_service_class(to
, from
->m_pkthdr
.pkt_svc
);
3819 to
->m_pkthdr
.pkt_ifainfo
= from
->m_pkthdr
.pkt_ifainfo
;
3821 to
->m_pkthdr
.pkt_bwseq
= from
->m_pkthdr
.pkt_bwseq
;
3822 #endif /* MEASURE_BW */
3826 * Return a list of mbuf hdrs that point to clusters. Try for num_needed;
3827 * if wantall is not set, return whatever number were available. Set up the
3828 * first num_with_pkthdrs with mbuf hdrs configured as packet headers; these
3829 * are chained on the m_nextpkt field. Any packets requested beyond this
3830 * are chained onto the last packet header's m_next field. The size of
3831 * the cluster is controlled by the parameter bufsize.
3833 __private_extern__
struct mbuf
*
3834 m_getpackets_internal(unsigned int *num_needed
, int num_with_pkthdrs
,
3835 int wait
, int wantall
, size_t bufsize
)
3838 struct mbuf
**np
, *top
;
3839 unsigned int pnum
, needed
= *num_needed
;
3840 mcache_obj_t
*mp_list
= NULL
;
3841 int mcflags
= MSLEEPF(wait
);
3843 struct ext_ref
*rfa
;
3847 ASSERT(bufsize
== m_maxsize(MC_CL
) ||
3848 bufsize
== m_maxsize(MC_BIGCL
) ||
3849 bufsize
== m_maxsize(MC_16KCL
));
3852 * Caller must first check for njcl because this
3853 * routine is internal and not exposed/used via KPI.
3855 VERIFY(bufsize
!= m_maxsize(MC_16KCL
) || njcl
> 0);
3862 * The caller doesn't want all the requested buffers; only some.
3863 * Try hard to get what we can, but don't block. This effectively
3864 * overrides MCR_SLEEP, since this thread will not go to sleep
3865 * if we can't get all the buffers.
3867 if (!wantall
|| (mcflags
& MCR_NOSLEEP
))
3868 mcflags
|= MCR_TRYHARD
;
3870 /* Allocate the composite mbuf + cluster elements from the cache */
3871 if (bufsize
== m_maxsize(MC_CL
))
3872 cp
= m_cache(MC_MBUF_CL
);
3873 else if (bufsize
== m_maxsize(MC_BIGCL
))
3874 cp
= m_cache(MC_MBUF_BIGCL
);
3876 cp
= m_cache(MC_MBUF_16KCL
);
3877 needed
= mcache_alloc_ext(cp
, &mp_list
, needed
, mcflags
);
3879 for (pnum
= 0; pnum
< needed
; pnum
++) {
3880 m
= (struct mbuf
*)mp_list
;
3881 mp_list
= mp_list
->obj_next
;
3883 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
3884 cl
= m
->m_ext
.ext_buf
;
3887 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
3888 VERIFY(MBUF_IS_COMPOSITE(m
));
3890 flag
= MEXT_FLAGS(m
);
3892 MBUF_INIT(m
, num_with_pkthdrs
, MT_DATA
);
3893 if (bufsize
== m_maxsize(MC_16KCL
)) {
3894 MBUF_16KCL_INIT(m
, cl
, rfa
, 1, flag
);
3895 } else if (bufsize
== m_maxsize(MC_BIGCL
)) {
3896 MBUF_BIGCL_INIT(m
, cl
, rfa
, 1, flag
);
3898 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
3901 if (num_with_pkthdrs
> 0) {
3904 if (mac_mbuf_label_init(m
, wait
) != 0) {
3908 #endif /* MAC_NET */
3912 if (num_with_pkthdrs
> 0)
3917 ASSERT(pnum
!= *num_needed
|| mp_list
== NULL
);
3918 if (mp_list
!= NULL
)
3919 mcache_free_ext(cp
, mp_list
);
3922 mtype_stat_add(MT_DATA
, pnum
);
3923 mtype_stat_sub(MT_FREE
, pnum
);
3926 if (wantall
&& (pnum
!= *num_needed
)) {
3932 if (pnum
> *num_needed
) {
3933 printf("%s: File a radar related to <rdar://10146739>. \
3934 needed = %u, pnum = %u, num_needed = %u \n",
3935 __func__
, needed
, pnum
, *num_needed
);
3943 * Return list of mbuf linked by m_nextpkt. Try for numlist, and if
3944 * wantall is not set, return whatever number were available. The size of
3945 * each mbuf in the list is controlled by the parameter packetlen. Each
3946 * mbuf of the list may have a chain of mbufs linked by m_next. Each mbuf
3947 * in the chain is called a segment. If maxsegments is not null and the
3948 * value pointed to is not null, this specify the maximum number of segments
3949 * for a chain of mbufs. If maxsegments is zero or the value pointed to
3950 * is zero the caller does not have any restriction on the number of segments.
3951 * The actual number of segments of a mbuf chain is return in the value
3952 * pointed to by maxsegments.
3954 __private_extern__
struct mbuf
*
3955 m_allocpacket_internal(unsigned int *numlist
, size_t packetlen
,
3956 unsigned int *maxsegments
, int wait
, int wantall
, size_t wantsize
)
3958 struct mbuf
**np
, *top
, *first
= NULL
;
3959 size_t bufsize
, r_bufsize
;
3960 unsigned int num
= 0;
3961 unsigned int nsegs
= 0;
3962 unsigned int needed
, resid
;
3963 int mcflags
= MSLEEPF(wait
);
3964 mcache_obj_t
*mp_list
= NULL
, *rmp_list
= NULL
;
3965 mcache_t
*cp
= NULL
, *rcp
= NULL
;
3973 if (wantsize
== 0) {
3974 if (packetlen
<= MINCLSIZE
) {
3975 bufsize
= packetlen
;
3976 } else if (packetlen
> m_maxsize(MC_CL
)) {
3977 /* Use 4KB if jumbo cluster pool isn't available */
3978 if (packetlen
<= m_maxsize(MC_BIGCL
) || njcl
== 0)
3979 bufsize
= m_maxsize(MC_BIGCL
);
3981 bufsize
= m_maxsize(MC_16KCL
);
3983 bufsize
= m_maxsize(MC_CL
);
3985 } else if (wantsize
== m_maxsize(MC_CL
) ||
3986 wantsize
== m_maxsize(MC_BIGCL
) ||
3987 (wantsize
== m_maxsize(MC_16KCL
) && njcl
> 0)) {
3993 if (bufsize
<= MHLEN
) {
3995 } else if (bufsize
<= MINCLSIZE
) {
3996 if (maxsegments
!= NULL
&& *maxsegments
== 1) {
3997 bufsize
= m_maxsize(MC_CL
);
4002 } else if (bufsize
== m_maxsize(MC_16KCL
)) {
4004 nsegs
= ((packetlen
- 1) >> M16KCLSHIFT
) + 1;
4005 } else if (bufsize
== m_maxsize(MC_BIGCL
)) {
4006 nsegs
= ((packetlen
- 1) >> MBIGCLSHIFT
) + 1;
4008 nsegs
= ((packetlen
- 1) >> MCLSHIFT
) + 1;
4010 if (maxsegments
!= NULL
) {
4011 if (*maxsegments
&& nsegs
> *maxsegments
) {
4012 *maxsegments
= nsegs
;
4015 *maxsegments
= nsegs
;
4019 * The caller doesn't want all the requested buffers; only some.
4020 * Try hard to get what we can, but don't block. This effectively
4021 * overrides MCR_SLEEP, since this thread will not go to sleep
4022 * if we can't get all the buffers.
4024 if (!wantall
|| (mcflags
& MCR_NOSLEEP
))
4025 mcflags
|= MCR_TRYHARD
;
4028 * Simple case where all elements in the lists/chains are mbufs.
4029 * Unless bufsize is greater than MHLEN, each segment chain is made
4030 * up of exactly 1 mbuf. Otherwise, each segment chain is made up
4031 * of 2 mbufs; the second one is used for the residual data, i.e.
4032 * the remaining data that cannot fit into the first mbuf.
4034 if (bufsize
<= MINCLSIZE
) {
4035 /* Allocate the elements in one shot from the mbuf cache */
4036 ASSERT(bufsize
<= MHLEN
|| nsegs
== 2);
4037 cp
= m_cache(MC_MBUF
);
4038 needed
= mcache_alloc_ext(cp
, &mp_list
,
4039 (*numlist
) * nsegs
, mcflags
);
4042 * The number of elements must be even if we are to use an
4043 * mbuf (instead of a cluster) to store the residual data.
4044 * If we couldn't allocate the requested number of mbufs,
4045 * trim the number down (if it's odd) in order to avoid
4046 * creating a partial segment chain.
4048 if (bufsize
> MHLEN
&& (needed
& 0x1))
4051 while (num
< needed
) {
4054 m
= (struct mbuf
*)mp_list
;
4055 mp_list
= mp_list
->obj_next
;
4058 MBUF_INIT(m
, 1, MT_DATA
);
4060 if (mac_init_mbuf(m
, wait
) != 0) {
4064 #endif /* MAC_NET */
4066 if (bufsize
> MHLEN
) {
4067 /* A second mbuf for this segment chain */
4068 m
->m_next
= (struct mbuf
*)mp_list
;
4069 mp_list
= mp_list
->obj_next
;
4070 ASSERT(m
->m_next
!= NULL
);
4072 MBUF_INIT(m
->m_next
, 0, MT_DATA
);
4078 ASSERT(num
!= *numlist
|| mp_list
== NULL
);
4081 mtype_stat_add(MT_DATA
, num
);
4082 mtype_stat_sub(MT_FREE
, num
);
4086 /* We've got them all; return to caller */
4087 if (num
== *numlist
)
4094 * Complex cases where elements are made up of one or more composite
4095 * mbufs + cluster, depending on packetlen. Each N-segment chain can
4096 * be illustrated as follows:
4098 * [mbuf + cluster 1] [mbuf + cluster 2] ... [mbuf + cluster N]
4100 * Every composite mbuf + cluster element comes from the intermediate
4101 * cache (either MC_MBUF_CL or MC_MBUF_BIGCL). For space efficiency,
4102 * the last composite element will come from the MC_MBUF_CL cache,
4103 * unless the residual data is larger than 2KB where we use the
4104 * big cluster composite cache (MC_MBUF_BIGCL) instead. Residual
4105 * data is defined as extra data beyond the first element that cannot
4106 * fit into the previous element, i.e. there is no residual data if
4107 * the chain only has 1 segment.
4109 r_bufsize
= bufsize
;
4110 resid
= packetlen
> bufsize
? packetlen
% bufsize
: 0;
4112 /* There is residual data; figure out the cluster size */
4113 if (wantsize
== 0 && packetlen
> MINCLSIZE
) {
4115 * Caller didn't request that all of the segments
4116 * in the chain use the same cluster size; use the
4117 * smaller of the cluster sizes.
4119 if (njcl
> 0 && resid
> m_maxsize(MC_BIGCL
))
4120 r_bufsize
= m_maxsize(MC_16KCL
);
4121 else if (resid
> m_maxsize(MC_CL
))
4122 r_bufsize
= m_maxsize(MC_BIGCL
);
4124 r_bufsize
= m_maxsize(MC_CL
);
4126 /* Use the same cluster size as the other segments */
4134 * Attempt to allocate composite mbuf + cluster elements for
4135 * the residual data in each chain; record the number of such
4136 * elements that can be allocated so that we know how many
4137 * segment chains we can afford to create.
4139 if (r_bufsize
<= m_maxsize(MC_CL
))
4140 rcp
= m_cache(MC_MBUF_CL
);
4141 else if (r_bufsize
<= m_maxsize(MC_BIGCL
))
4142 rcp
= m_cache(MC_MBUF_BIGCL
);
4144 rcp
= m_cache(MC_MBUF_16KCL
);
4145 needed
= mcache_alloc_ext(rcp
, &rmp_list
, *numlist
, mcflags
);
4150 /* This is temporarily reduced for calculation */
4156 * Attempt to allocate the rest of the composite mbuf + cluster
4157 * elements for the number of segment chains that we need.
4159 if (bufsize
<= m_maxsize(MC_CL
))
4160 cp
= m_cache(MC_MBUF_CL
);
4161 else if (bufsize
<= m_maxsize(MC_BIGCL
))
4162 cp
= m_cache(MC_MBUF_BIGCL
);
4164 cp
= m_cache(MC_MBUF_16KCL
);
4165 needed
= mcache_alloc_ext(cp
, &mp_list
, needed
* nsegs
, mcflags
);
4167 /* Round it down to avoid creating a partial segment chain */
4168 needed
= (needed
/ nsegs
) * nsegs
;
4174 * We're about to construct the chain(s); take into account
4175 * the number of segments we have created above to hold the
4176 * residual data for each chain, as well as restore the
4177 * original count of segments per chain.
4180 needed
+= needed
/ nsegs
;
4187 struct ext_ref
*rfa
;
4192 if (nsegs
== 1 || (num
% nsegs
) != 0 || resid
== 0) {
4193 m
= (struct mbuf
*)mp_list
;
4194 mp_list
= mp_list
->obj_next
;
4196 m
= (struct mbuf
*)rmp_list
;
4197 rmp_list
= rmp_list
->obj_next
;
4200 VERIFY(m
->m_type
== MT_FREE
&& m
->m_flags
== M_EXT
);
4201 VERIFY(m
->m_ext
.ext_free
== NULL
||
4202 m
->m_ext
.ext_free
== m_bigfree
||
4203 m
->m_ext
.ext_free
== m_16kfree
);
4205 cl
= m
->m_ext
.ext_buf
;
4208 ASSERT(cl
!= NULL
&& rfa
!= NULL
);
4209 VERIFY(MBUF_IS_COMPOSITE(m
));
4211 flag
= MEXT_FLAGS(m
);
4213 pkthdr
= (nsegs
== 1 || (num
% nsegs
) == 1);
4216 MBUF_INIT(m
, pkthdr
, MT_DATA
);
4217 if (m
->m_ext
.ext_free
== m_16kfree
) {
4218 MBUF_16KCL_INIT(m
, cl
, rfa
, 1, flag
);
4219 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
4220 MBUF_BIGCL_INIT(m
, cl
, rfa
, 1, flag
);
4222 MBUF_CL_INIT(m
, cl
, rfa
, 1, flag
);
4225 if (pkthdr
&& mac_init_mbuf(m
, wait
) != 0) {
4230 #endif /* MAC_NET */
4233 if ((num
% nsegs
) == 0)
4234 np
= &first
->m_nextpkt
;
4243 mtype_stat_add(MT_DATA
, num
);
4244 mtype_stat_sub(MT_FREE
, num
);
4249 /* We've got them all; return to caller */
4250 if (num
== *numlist
) {
4251 ASSERT(mp_list
== NULL
&& rmp_list
== NULL
);
4256 /* Free up what's left of the above */
4257 if (mp_list
!= NULL
)
4258 mcache_free_ext(cp
, mp_list
);
4259 if (rmp_list
!= NULL
)
4260 mcache_free_ext(rcp
, rmp_list
);
4261 if (wantall
&& top
!= NULL
) {
4270 * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated
4271 * packets on receive ring.
4273 __private_extern__
struct mbuf
*
4274 m_getpacket_how(int wait
)
4276 unsigned int num_needed
= 1;
4278 return (m_getpackets_internal(&num_needed
, 1, wait
, 1,
4283 * Best effort to get a mbuf cluster + pkthdr. Used by drivers to allocated
4284 * packets on receive ring.
4289 unsigned int num_needed
= 1;
4291 return (m_getpackets_internal(&num_needed
, 1, M_WAIT
, 1,
4296 * Return a list of mbuf hdrs that point to clusters. Try for num_needed;
4297 * if this can't be met, return whatever number were available. Set up the
4298 * first num_with_pkthdrs with mbuf hdrs configured as packet headers. These
4299 * are chained on the m_nextpkt field. Any packets requested beyond this are
4300 * chained onto the last packet header's m_next field.
4303 m_getpackets(int num_needed
, int num_with_pkthdrs
, int how
)
4305 unsigned int n
= num_needed
;
4307 return (m_getpackets_internal(&n
, num_with_pkthdrs
, how
, 0,
4312 * Return a list of mbuf hdrs set up as packet hdrs chained together
4313 * on the m_nextpkt field
4316 m_getpackethdrs(int num_needed
, int how
)
4319 struct mbuf
**np
, *top
;
4324 while (num_needed
--) {
4325 m
= _M_RETRYHDR(how
, MT_DATA
);
4337 * Free an mbuf list (m_nextpkt) while following m_next. Returns the count
4338 * for mbufs packets freed. Used by the drivers.
4341 m_freem_list(struct mbuf
*m
)
4343 struct mbuf
*nextpkt
;
4344 mcache_obj_t
*mp_list
= NULL
;
4345 mcache_obj_t
*mcl_list
= NULL
;
4346 mcache_obj_t
*mbc_list
= NULL
;
4347 mcache_obj_t
*m16k_list
= NULL
;
4348 mcache_obj_t
*m_mcl_list
= NULL
;
4349 mcache_obj_t
*m_mbc_list
= NULL
;
4350 mcache_obj_t
*m_m16k_list
= NULL
;
4351 mcache_obj_t
*ref_list
= NULL
;
4353 int mt_free
= 0, mt_data
= 0, mt_header
= 0, mt_soname
= 0, mt_tag
= 0;
4358 nextpkt
= m
->m_nextpkt
;
4359 m
->m_nextpkt
= NULL
;
4362 struct mbuf
*next
= m
->m_next
;
4363 mcache_obj_t
*o
, *rfa
;
4364 u_int32_t refcnt
, composite
;
4366 if (m
->m_type
== MT_FREE
)
4367 panic("m_free: freeing an already freed mbuf");
4369 if (m
->m_type
!= MT_FREE
)
4372 if (m
->m_flags
& M_PKTHDR
) {
4373 /* Check for scratch area overflow */
4374 m_redzone_verify(m
);
4375 /* Free the aux data and tags if there is any */
4376 m_tag_delete_chain(m
, NULL
);
4379 if (!(m
->m_flags
& M_EXT
))
4382 o
= (mcache_obj_t
*)(void *)m
->m_ext
.ext_buf
;
4383 refcnt
= m_decref(m
);
4384 composite
= (MEXT_FLAGS(m
) & EXTF_COMPOSITE
);
4385 if (refcnt
== 0 && !composite
) {
4386 if (m
->m_ext
.ext_free
== NULL
) {
4387 o
->obj_next
= mcl_list
;
4389 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
4390 o
->obj_next
= mbc_list
;
4392 } else if (m
->m_ext
.ext_free
== m_16kfree
) {
4393 o
->obj_next
= m16k_list
;
4396 (*(m
->m_ext
.ext_free
))((caddr_t
)o
,
4400 rfa
= (mcache_obj_t
*)(void *)MEXT_RFA(m
);
4401 rfa
->obj_next
= ref_list
;
4404 } else if (refcnt
== 0 && composite
) {
4405 VERIFY(m
->m_type
!= MT_FREE
);
4407 * Amortize the costs of atomic operations
4408 * by doing them at the end, if possible.
4410 if (m
->m_type
== MT_DATA
)
4412 else if (m
->m_type
== MT_HEADER
)
4414 else if (m
->m_type
== MT_SONAME
)
4416 else if (m
->m_type
== MT_TAG
)
4419 mtype_stat_dec(m
->m_type
);
4421 m
->m_type
= MT_FREE
;
4424 m
->m_next
= m
->m_nextpkt
= NULL
;
4426 MEXT_FLAGS(m
) &= ~EXTF_READONLY
;
4428 /* "Free" into the intermediate cache */
4429 o
= (mcache_obj_t
*)m
;
4430 if (m
->m_ext
.ext_free
== NULL
) {
4431 o
->obj_next
= m_mcl_list
;
4433 } else if (m
->m_ext
.ext_free
== m_bigfree
) {
4434 o
->obj_next
= m_mbc_list
;
4437 VERIFY(m
->m_ext
.ext_free
== m_16kfree
);
4438 o
->obj_next
= m_m16k_list
;
4446 * Amortize the costs of atomic operations
4447 * by doing them at the end, if possible.
4449 if (m
->m_type
== MT_DATA
)
4451 else if (m
->m_type
== MT_HEADER
)
4453 else if (m
->m_type
== MT_SONAME
)
4455 else if (m
->m_type
== MT_TAG
)
4457 else if (m
->m_type
!= MT_FREE
)
4458 mtype_stat_dec(m
->m_type
);
4460 m
->m_type
= MT_FREE
;
4461 m
->m_flags
= m
->m_len
= 0;
4462 m
->m_next
= m
->m_nextpkt
= NULL
;
4464 ((mcache_obj_t
*)m
)->obj_next
= mp_list
;
4465 mp_list
= (mcache_obj_t
*)m
;
4474 mtype_stat_add(MT_FREE
, mt_free
);
4476 mtype_stat_sub(MT_DATA
, mt_data
);
4478 mtype_stat_sub(MT_HEADER
, mt_header
);
4480 mtype_stat_sub(MT_SONAME
, mt_soname
);
4482 mtype_stat_sub(MT_TAG
, mt_tag
);
4484 if (mp_list
!= NULL
)
4485 mcache_free_ext(m_cache(MC_MBUF
), mp_list
);
4486 if (mcl_list
!= NULL
)
4487 mcache_free_ext(m_cache(MC_CL
), mcl_list
);
4488 if (mbc_list
!= NULL
)
4489 mcache_free_ext(m_cache(MC_BIGCL
), mbc_list
);
4490 if (m16k_list
!= NULL
)
4491 mcache_free_ext(m_cache(MC_16KCL
), m16k_list
);
4492 if (m_mcl_list
!= NULL
)
4493 mcache_free_ext(m_cache(MC_MBUF_CL
), m_mcl_list
);
4494 if (m_mbc_list
!= NULL
)
4495 mcache_free_ext(m_cache(MC_MBUF_BIGCL
), m_mbc_list
);
4496 if (m_m16k_list
!= NULL
)
4497 mcache_free_ext(m_cache(MC_MBUF_16KCL
), m_m16k_list
);
4498 if (ref_list
!= NULL
)
4499 mcache_free_ext(ref_cache
, ref_list
);
4505 m_freem(struct mbuf
*m
)
4512 * Mbuffer utility routines.
4516 * Compute the amount of space available before the current start
4517 * of data in an mbuf.
4520 m_leadingspace(struct mbuf
*m
)
4522 if (m
->m_flags
& M_EXT
) {
4523 if (MCLHASREFERENCE(m
))
4525 return (m
->m_data
- m
->m_ext
.ext_buf
);
4527 if (m
->m_flags
& M_PKTHDR
)
4528 return (m
->m_data
- m
->m_pktdat
);
4529 return (m
->m_data
- m
->m_dat
);
4533 * Compute the amount of space available after the end of data in an mbuf.
4536 m_trailingspace(struct mbuf
*m
)
4538 if (m
->m_flags
& M_EXT
) {
4539 if (MCLHASREFERENCE(m
))
4541 return (m
->m_ext
.ext_buf
+ m
->m_ext
.ext_size
-
4542 (m
->m_data
+ m
->m_len
));
4544 return (&m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
));
4548 * Lesser-used path for M_PREPEND: allocate new mbuf to prepend to chain,
4549 * copy junk along. Does not adjust packet header length.
4552 m_prepend(struct mbuf
*m
, int len
, int how
)
4556 _MGET(mn
, how
, m
->m_type
);
4561 if (m
->m_flags
& M_PKTHDR
) {
4562 M_COPY_PKTHDR(mn
, m
);
4563 m
->m_flags
&= ~M_PKTHDR
;
4567 if (m
->m_flags
& M_PKTHDR
) {
4568 VERIFY(len
<= MHLEN
);
4571 VERIFY(len
<= MLEN
);
4579 * Replacement for old M_PREPEND macro: allocate new mbuf to prepend to
4580 * chain, copy junk along, and adjust length.
4583 m_prepend_2(struct mbuf
*m
, int len
, int how
, int align
)
4585 if (M_LEADINGSPACE(m
) >= len
&&
4586 (!align
|| IS_P2ALIGNED((m
->m_data
- len
), sizeof(u_int32_t
)))) {
4590 m
= m_prepend(m
, len
, how
);
4592 if ((m
) && (m
->m_flags
& M_PKTHDR
))
4593 m
->m_pkthdr
.len
+= len
;
4598 * Make a copy of an mbuf chain starting "off0" bytes from the beginning,
4599 * continuing for "len" bytes. If len is M_COPYALL, copy to end of mbuf.
4600 * The wait parameter is a choice of M_WAIT/M_DONTWAIT from caller.
4605 m_copym_mode(struct mbuf
*m
, int off0
, int len
, int wait
, uint32_t mode
)
4607 struct mbuf
*n
, *mhdr
= NULL
, **np
;
4612 if (off
< 0 || len
< 0)
4613 panic("m_copym: invalid offset %d or len %d", off
, len
);
4615 VERIFY((mode
!= M_COPYM_MUST_COPY_HDR
&&
4616 mode
!= M_COPYM_MUST_MOVE_HDR
) || (m
->m_flags
& M_PKTHDR
));
4618 if ((off
== 0 && (m
->m_flags
& M_PKTHDR
)) ||
4619 mode
== M_COPYM_MUST_COPY_HDR
|| mode
== M_COPYM_MUST_MOVE_HDR
) {
4624 while (off
>= m
->m_len
) {
4625 if (m
->m_next
== NULL
)
4626 panic("m_copym: invalid mbuf chain");
4635 if (len
!= M_COPYALL
)
4636 panic("m_copym: len != M_COPYALL");
4641 n
= _M_RETRYHDR(wait
, m
->m_type
);
4643 n
= _M_RETRY(wait
, m
->m_type
);
4650 if ((mode
== M_COPYM_MOVE_HDR
) ||
4651 (mode
== M_COPYM_MUST_MOVE_HDR
)) {
4652 M_COPY_PKTHDR(n
, mhdr
);
4653 } else if ((mode
== M_COPYM_COPY_HDR
) ||
4654 (mode
== M_COPYM_MUST_COPY_HDR
)) {
4655 if (m_dup_pkthdr(n
, mhdr
, wait
) == 0)
4658 if (len
== M_COPYALL
)
4659 n
->m_pkthdr
.len
-= off0
;
4661 n
->m_pkthdr
.len
= len
;
4664 * There is data to copy from the packet header mbuf
4665 * if it is empty or it is before the starting offset
4672 n
->m_len
= MIN(len
, (m
->m_len
- off
));
4673 if (m
->m_flags
& M_EXT
) {
4674 n
->m_ext
= m
->m_ext
;
4676 n
->m_data
= m
->m_data
+ off
;
4677 n
->m_flags
|= M_EXT
;
4680 * Limit to the capacity of the destination
4682 if (n
->m_flags
& M_PKTHDR
)
4683 n
->m_len
= MIN(n
->m_len
, MHLEN
);
4685 n
->m_len
= MIN(n
->m_len
, MLEN
);
4687 if (MTOD(n
, char *) + n
->m_len
> ((char *)n
) + MSIZE
)
4688 panic("%s n %p copy overflow",
4691 bcopy(MTOD(m
, caddr_t
)+off
, MTOD(n
, caddr_t
),
4692 (unsigned)n
->m_len
);
4694 if (len
!= M_COPYALL
)
4714 m_copym(struct mbuf
*m
, int off0
, int len
, int wait
)
4716 return (m_copym_mode(m
, off0
, len
, wait
, M_COPYM_MOVE_HDR
));
4720 * Equivalent to m_copym except that all necessary mbuf hdrs are allocated
4721 * within this routine also, the last mbuf and offset accessed are passed
4722 * out and can be passed back in to avoid having to rescan the entire mbuf
4723 * list (normally hung off of the socket)
4726 m_copym_with_hdrs(struct mbuf
*m0
, int off0
, int len0
, int wait
,
4727 struct mbuf
**m_lastm
, int *m_off
, uint32_t mode
)
4729 struct mbuf
*m
= m0
, *n
, **np
= NULL
;
4730 int off
= off0
, len
= len0
;
4731 struct mbuf
*top
= NULL
;
4732 int mcflags
= MSLEEPF(wait
);
4735 mcache_obj_t
*list
= NULL
;
4738 if (off
== 0 && (m
->m_flags
& M_PKTHDR
))
4741 if (m_lastm
!= NULL
&& *m_lastm
!= NULL
) {
4745 while (off
>= m
->m_len
) {
4755 len
-= MIN(len
, (n
->m_len
- ((needed
== 1) ? off
: 0)));
4762 * If the caller doesn't want to be put to sleep, mark it with
4763 * MCR_TRYHARD so that we may reclaim buffers from other places
4766 if (mcflags
& MCR_NOSLEEP
)
4767 mcflags
|= MCR_TRYHARD
;
4769 if (mcache_alloc_ext(m_cache(MC_MBUF
), &list
, needed
,
4775 n
= (struct mbuf
*)list
;
4776 list
= list
->obj_next
;
4777 ASSERT(n
!= NULL
&& m
!= NULL
);
4779 type
= (top
== NULL
) ? MT_HEADER
: m
->m_type
;
4780 MBUF_INIT(n
, (top
== NULL
), type
);
4782 if (top
== NULL
&& mac_mbuf_label_init(n
, wait
) != 0) {
4783 mtype_stat_inc(MT_HEADER
);
4784 mtype_stat_dec(MT_FREE
);
4788 #endif /* MAC_NET */
4800 if ((mode
== M_COPYM_MOVE_HDR
) ||
4801 (mode
== M_COPYM_MUST_MOVE_HDR
)) {
4802 M_COPY_PKTHDR(n
, m
);
4803 } else if ((mode
== M_COPYM_COPY_HDR
) ||
4804 (mode
== M_COPYM_MUST_COPY_HDR
)) {
4805 if (m_dup_pkthdr(n
, m
, wait
) == 0)
4808 n
->m_pkthdr
.len
= len
;
4811 n
->m_len
= MIN(len
, (m
->m_len
- off
));
4813 if (m
->m_flags
& M_EXT
) {
4814 n
->m_ext
= m
->m_ext
;
4816 n
->m_data
= m
->m_data
+ off
;
4817 n
->m_flags
|= M_EXT
;
4819 if (MTOD(n
, char *) + n
->m_len
> ((char *)n
) + MSIZE
)
4820 panic("%s n %p copy overflow",
4823 bcopy(MTOD(m
, caddr_t
)+off
, MTOD(n
, caddr_t
),
4824 (unsigned)n
->m_len
);
4829 if (m_lastm
!= NULL
&& m_off
!= NULL
) {
4830 if ((off
+ n
->m_len
) == m
->m_len
) {
4831 *m_lastm
= m
->m_next
;
4835 *m_off
= off
+ n
->m_len
;
4845 mtype_stat_inc(MT_HEADER
);
4846 mtype_stat_add(type
, needed
);
4847 mtype_stat_sub(MT_FREE
, needed
+ 1);
4849 ASSERT(list
== NULL
);
4854 mcache_free_ext(m_cache(MC_MBUF
), list
);
4862 * Copy data from an mbuf chain starting "off" bytes from the beginning,
4863 * continuing for "len" bytes, into the indicated buffer.
4866 m_copydata(struct mbuf
*m
, int off
, int len
, void *vp
)
4871 if (off
< 0 || len
< 0)
4872 panic("m_copydata: invalid offset %d or len %d", off
, len
);
4876 panic("m_copydata: invalid mbuf chain");
4884 panic("m_copydata: invalid mbuf chain");
4885 count
= MIN(m
->m_len
- off
, len
);
4886 bcopy(MTOD(m
, caddr_t
) + off
, cp
, count
);
4895 * Concatenate mbuf chain n to m. Both chains must be of the same type
4896 * (e.g. MT_DATA). Any m_pkthdr is not updated.
4899 m_cat(struct mbuf
*m
, struct mbuf
*n
)
4904 if ((m
->m_flags
& M_EXT
) ||
4905 m
->m_data
+ m
->m_len
+ n
->m_len
>= &m
->m_dat
[MLEN
]) {
4906 /* just join the two chains */
4910 /* splat the data from one into the other */
4911 bcopy(MTOD(n
, caddr_t
), MTOD(m
, caddr_t
) + m
->m_len
,
4913 m
->m_len
+= n
->m_len
;
4919 m_adj(struct mbuf
*mp
, int req_len
)
4925 if ((m
= mp
) == NULL
)
4931 while (m
!= NULL
&& len
> 0) {
4932 if (m
->m_len
<= len
) {
4943 if (m
->m_flags
& M_PKTHDR
)
4944 m
->m_pkthdr
.len
-= (req_len
- len
);
4947 * Trim from tail. Scan the mbuf chain,
4948 * calculating its length and finding the last mbuf.
4949 * If the adjustment only affects this mbuf, then just
4950 * adjust and return. Otherwise, rescan and truncate
4951 * after the remaining size.
4957 if (m
->m_next
== (struct mbuf
*)0)
4961 if (m
->m_len
>= len
) {
4964 if (m
->m_flags
& M_PKTHDR
)
4965 m
->m_pkthdr
.len
-= len
;
4972 * Correct length for chain is "count".
4973 * Find the mbuf with last data, adjust its length,
4974 * and toss data from remaining mbufs on chain.
4977 if (m
->m_flags
& M_PKTHDR
)
4978 m
->m_pkthdr
.len
= count
;
4979 for (; m
; m
= m
->m_next
) {
4980 if (m
->m_len
>= count
) {
4986 while ((m
= m
->m_next
))
4992 * Rearange an mbuf chain so that len bytes are contiguous
4993 * and in the data area of an mbuf (so that mtod and dtom
4994 * will work for a structure of size len). Returns the resulting
4995 * mbuf chain on success, frees it and returns null on failure.
4996 * If there is room, it will add up to max_protohdr-len extra bytes to the
4997 * contiguous region in an attempt to avoid being called next time.
5002 m_pullup(struct mbuf
*n
, int len
)
5009 * If first mbuf has no cluster, and has room for len bytes
5010 * without shifting current data, pullup into it,
5011 * otherwise allocate a new mbuf to prepend to the chain.
5013 if ((n
->m_flags
& M_EXT
) == 0 &&
5014 n
->m_data
+ len
< &n
->m_dat
[MLEN
] && n
->m_next
) {
5015 if (n
->m_len
>= len
)
5023 _MGET(m
, M_DONTWAIT
, n
->m_type
);
5027 if (n
->m_flags
& M_PKTHDR
) {
5028 M_COPY_PKTHDR(m
, n
);
5029 n
->m_flags
&= ~M_PKTHDR
;
5032 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
5034 count
= MIN(MIN(MAX(len
, max_protohdr
), space
), n
->m_len
);
5035 bcopy(MTOD(n
, caddr_t
), MTOD(m
, caddr_t
) + m
->m_len
,
5045 } while (len
> 0 && n
);
5059 * Like m_pullup(), except a new mbuf is always allocated, and we allow
5060 * the amount of empty space before the data in the new mbuf to be specified
5061 * (in the event that the caller expects to prepend later).
5063 __private_extern__
int MSFail
= 0;
5065 __private_extern__
struct mbuf
*
5066 m_copyup(struct mbuf
*n
, int len
, int dstoff
)
5071 if (len
> (MHLEN
- dstoff
))
5073 MGET(m
, M_DONTWAIT
, n
->m_type
);
5077 if (n
->m_flags
& M_PKTHDR
) {
5078 m_copy_pkthdr(m
, n
);
5079 n
->m_flags
&= ~M_PKTHDR
;
5081 m
->m_data
+= dstoff
;
5082 space
= &m
->m_dat
[MLEN
] - (m
->m_data
+ m
->m_len
);
5084 count
= min(min(max(len
, max_protohdr
), space
), n
->m_len
);
5085 memcpy(mtod(m
, caddr_t
) + m
->m_len
, mtod(n
, caddr_t
),
5095 } while (len
> 0 && n
);
5109 * Partition an mbuf chain in two pieces, returning the tail --
5110 * all but the first len0 bytes. In case of failure, it returns NULL and
5111 * attempts to restore the chain to its original state.
5114 m_split(struct mbuf
*m0
, int len0
, int wait
)
5116 return (m_split0(m0
, len0
, wait
, 1));
5119 static struct mbuf
*
5120 m_split0(struct mbuf
*m0
, int len0
, int wait
, int copyhdr
)
5123 unsigned len
= len0
, remain
;
5125 for (m
= m0
; m
&& len
> m
->m_len
; m
= m
->m_next
)
5129 remain
= m
->m_len
- len
;
5130 if (copyhdr
&& (m0
->m_flags
& M_PKTHDR
)) {
5131 _MGETHDR(n
, wait
, m0
->m_type
);
5134 n
->m_pkthdr
.rcvif
= m0
->m_pkthdr
.rcvif
;
5135 n
->m_pkthdr
.len
= m0
->m_pkthdr
.len
- len0
;
5136 m0
->m_pkthdr
.len
= len0
;
5137 if (m
->m_flags
& M_EXT
)
5139 if (remain
> MHLEN
) {
5140 /* m can't be the lead packet */
5142 n
->m_next
= m_split(m
, len
, wait
);
5143 if (n
->m_next
== NULL
) {
5149 MH_ALIGN(n
, remain
);
5150 } else if (remain
== 0) {
5155 _MGET(n
, wait
, m
->m_type
);
5161 if (m
->m_flags
& M_EXT
) {
5162 n
->m_flags
|= M_EXT
;
5163 n
->m_ext
= m
->m_ext
;
5165 n
->m_data
= m
->m_data
+ len
;
5167 bcopy(MTOD(m
, caddr_t
) + len
, MTOD(n
, caddr_t
), remain
);
5171 n
->m_next
= m
->m_next
;
5177 * Routine to copy from device local memory into mbufs.
5180 m_devget(char *buf
, int totlen
, int off0
, struct ifnet
*ifp
,
5181 void (*copy
)(const void *, void *, size_t))
5184 struct mbuf
*top
= NULL
, **mp
= &top
;
5185 int off
= off0
, len
;
5193 * If 'off' is non-zero, packet is trailer-encapsulated,
5194 * so we have to skip the type and length fields.
5196 cp
+= off
+ 2 * sizeof (u_int16_t
);
5197 totlen
-= 2 * sizeof (u_int16_t
);
5199 _MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
5202 m
->m_pkthdr
.rcvif
= ifp
;
5203 m
->m_pkthdr
.len
= totlen
;
5206 while (totlen
> 0) {
5208 _MGET(m
, M_DONTWAIT
, MT_DATA
);
5215 len
= MIN(totlen
, epkt
- cp
);
5216 if (len
>= MINCLSIZE
) {
5217 MCLGET(m
, M_DONTWAIT
);
5218 if (m
->m_flags
& M_EXT
) {
5219 m
->m_len
= len
= MIN(len
, m_maxsize(MC_CL
));
5221 /* give up when it's out of cluster mbufs */
5229 * Place initial small packet/header at end of mbuf.
5231 if (len
< m
->m_len
) {
5233 len
+ max_linkhdr
<= m
->m_len
)
5234 m
->m_data
+= max_linkhdr
;
5241 copy(cp
, MTOD(m
, caddr_t
), (unsigned)len
);
5243 bcopy(cp
, MTOD(m
, caddr_t
), (unsigned)len
);
5254 #ifndef MBUF_GROWTH_NORMAL_THRESH
5255 #define MBUF_GROWTH_NORMAL_THRESH 25
5259 * Cluster freelist allocation check.
5262 m_howmany(int num
, size_t bufsize
)
5265 u_int32_t m_mbclusters
, m_clusters
, m_bigclusters
, m_16kclusters
;
5266 u_int32_t m_mbfree
, m_clfree
, m_bigclfree
, m_16kclfree
;
5267 u_int32_t sumclusters
, freeclusters
;
5268 u_int32_t percent_pool
, percent_kmem
;
5269 u_int32_t mb_growth
, mb_growth_thresh
;
5271 VERIFY(bufsize
== m_maxsize(MC_BIGCL
) ||
5272 bufsize
== m_maxsize(MC_16KCL
));
5274 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
5276 /* Numbers in 2K cluster units */
5277 m_mbclusters
= m_total(MC_MBUF
) >> NMBPCLSHIFT
;
5278 m_clusters
= m_total(MC_CL
);
5279 m_bigclusters
= m_total(MC_BIGCL
) << NCLPBGSHIFT
;
5280 m_16kclusters
= m_total(MC_16KCL
);
5281 sumclusters
= m_mbclusters
+ m_clusters
+ m_bigclusters
;
5283 m_mbfree
= m_infree(MC_MBUF
) >> NMBPCLSHIFT
;
5284 m_clfree
= m_infree(MC_CL
);
5285 m_bigclfree
= m_infree(MC_BIGCL
) << NCLPBGSHIFT
;
5286 m_16kclfree
= m_infree(MC_16KCL
);
5287 freeclusters
= m_mbfree
+ m_clfree
+ m_bigclfree
;
5289 /* Bail if we've maxed out the mbuf memory map */
5290 if ((bufsize
== m_maxsize(MC_BIGCL
) && sumclusters
>= nclusters
) ||
5291 (njcl
> 0 && bufsize
== m_maxsize(MC_16KCL
) &&
5292 (m_16kclusters
<< NCLPJCLSHIFT
) >= njcl
)) {
5296 if (bufsize
== m_maxsize(MC_BIGCL
)) {
5298 if (m_bigclusters
< m_minlimit(MC_BIGCL
))
5299 return (m_minlimit(MC_BIGCL
) - m_bigclusters
);
5302 ((sumclusters
- freeclusters
) * 100) / sumclusters
;
5303 percent_kmem
= (sumclusters
* 100) / nclusters
;
5306 * If a light/normal user, grow conservatively (75%)
5307 * If a heavy user, grow aggressively (50%)
5309 if (percent_kmem
< MBUF_GROWTH_NORMAL_THRESH
)
5310 mb_growth
= MB_GROWTH_NORMAL
;
5312 mb_growth
= MB_GROWTH_AGGRESSIVE
;
5314 if (percent_kmem
< 5) {
5315 /* For initial allocations */
5318 /* Return if >= MBIGCL_LOWAT clusters available */
5319 if (m_infree(MC_BIGCL
) >= MBIGCL_LOWAT
&&
5320 m_total(MC_BIGCL
) >=
5321 MBIGCL_LOWAT
+ m_minlimit(MC_BIGCL
))
5324 /* Ensure at least num clusters are accessible */
5325 if (num
>= m_infree(MC_BIGCL
))
5326 i
= num
- m_infree(MC_BIGCL
);
5327 if (num
> m_total(MC_BIGCL
) - m_minlimit(MC_BIGCL
))
5328 j
= num
- (m_total(MC_BIGCL
) -
5329 m_minlimit(MC_BIGCL
));
5334 * Grow pool if percent_pool > 75 (normal growth)
5335 * or percent_pool > 50 (aggressive growth).
5337 mb_growth_thresh
= 100 - (100 / (1 << mb_growth
));
5338 if (percent_pool
> mb_growth_thresh
)
5339 j
= ((sumclusters
+ num
) >> mb_growth
) -
5344 /* Check to ensure we didn't go over limits */
5345 if (i
+ m_bigclusters
>= m_maxlimit(MC_BIGCL
))
5346 i
= m_maxlimit(MC_BIGCL
) - m_bigclusters
;
5347 if ((i
<< 1) + sumclusters
>= nclusters
)
5348 i
= (nclusters
- sumclusters
) >> 1;
5349 VERIFY((m_total(MC_BIGCL
) + i
) <= m_maxlimit(MC_BIGCL
));
5350 VERIFY(sumclusters
+ (i
<< 1) <= nclusters
);
5352 } else { /* 16K CL */
5354 /* Ensure at least num clusters are available */
5355 if (num
>= m_16kclfree
)
5356 i
= num
- m_16kclfree
;
5358 /* Always grow 16KCL pool aggressively */
5359 if (((m_16kclusters
+ num
) >> 1) > m_16kclfree
)
5360 j
= ((m_16kclusters
+ num
) >> 1) - m_16kclfree
;
5363 /* Check to ensure we don't go over limit */
5364 if (i
+ m_16kclusters
>= m_maxlimit(MC_16KCL
))
5365 i
= m_maxlimit(MC_16KCL
) - m_16kclusters
;
5366 VERIFY((m_total(MC_16KCL
) + i
) <= m_maxlimit(MC_16KCL
));
5371 * Return the number of bytes in the mbuf chain, m.
5374 m_length(struct mbuf
*m
)
5377 unsigned int pktlen
;
5379 if (m
->m_flags
& M_PKTHDR
)
5380 return (m
->m_pkthdr
.len
);
5383 for (m0
= m
; m0
!= NULL
; m0
= m0
->m_next
)
5384 pktlen
+= m0
->m_len
;
5389 * Copy data from a buffer back into the indicated mbuf chain,
5390 * starting "off" bytes from the beginning, extending the mbuf
5391 * chain if necessary.
5394 m_copyback(struct mbuf
*m0
, int off
, int len
, const void *cp
)
5397 struct mbuf
*origm
= m0
;
5407 m_copyback0(&m0
, off
, len
, cp
,
5408 M_COPYBACK0_COPYBACK
| M_COPYBACK0_EXTEND
, M_DONTWAIT
);
5411 if (error
!= 0 || (m0
!= NULL
&& origm
!= m0
))
5412 panic("m_copyback");
5417 m_copyback_cow(struct mbuf
*m0
, int off
, int len
, const void *cp
, int how
)
5421 /* don't support chain expansion */
5422 VERIFY(off
+ len
<= m_length(m0
));
5424 error
= m_copyback0(&m0
, off
, len
, cp
,
5425 M_COPYBACK0_COPYBACK
| M_COPYBACK0_COW
, how
);
5428 * no way to recover from partial success.
5429 * just free the chain.
5438 * m_makewritable: ensure the specified range writable.
5441 m_makewritable(struct mbuf
**mp
, int off
, int len
, int how
)
5446 int origlen
, reslen
;
5448 origlen
= m_length(*mp
);
5451 #if 0 /* M_COPYALL is large enough */
5452 if (len
== M_COPYALL
)
5453 len
= m_length(*mp
) - off
; /* XXX */
5456 error
= m_copyback0(mp
, off
, len
, NULL
,
5457 M_COPYBACK0_PRESERVE
| M_COPYBACK0_COW
, how
);
5461 for (n
= *mp
; n
; n
= n
->m_next
)
5463 if (origlen
!= reslen
)
5464 panic("m_makewritable: length changed");
5465 if (((*mp
)->m_flags
& M_PKTHDR
) && reslen
!= (*mp
)->m_pkthdr
.len
)
5466 panic("m_makewritable: inconsist");
5473 m_copyback0(struct mbuf
**mp0
, int off
, int len
, const void *vp
, int flags
,
5480 const char *cp
= vp
;
5482 VERIFY(mp0
!= NULL
);
5483 VERIFY(*mp0
!= NULL
);
5484 VERIFY((flags
& M_COPYBACK0_PRESERVE
) == 0 || cp
== NULL
);
5485 VERIFY((flags
& M_COPYBACK0_COPYBACK
) == 0 || cp
!= NULL
);
5488 * we don't bother to update "totlen" in the case of M_COPYBACK0_COW,
5489 * assuming that M_COPYBACK0_EXTEND and M_COPYBACK0_COW are exclusive.
5492 VERIFY((~flags
& (M_COPYBACK0_EXTEND
|M_COPYBACK0_COW
)) != 0);
5496 while (off
> (mlen
= m
->m_len
)) {
5499 if (m
->m_next
== NULL
) {
5502 if (!(flags
& M_COPYBACK0_EXTEND
))
5506 * try to make some space at the end of "m".
5510 if (off
+ len
>= MINCLSIZE
&&
5511 !(m
->m_flags
& M_EXT
) && m
->m_len
== 0) {
5514 tspace
= M_TRAILINGSPACE(m
);
5516 tspace
= MIN(tspace
, off
+ len
);
5518 bzero(mtod(m
, char *) + m
->m_len
,
5527 * need to allocate an mbuf.
5530 if (off
+ len
>= MINCLSIZE
) {
5531 n
= m_getcl(how
, m
->m_type
, 0);
5533 n
= _M_GET(how
, m
->m_type
);
5539 n
->m_len
= MIN(M_TRAILINGSPACE(n
), off
+ len
);
5540 bzero(mtod(n
, char *), MIN(n
->m_len
, off
));
5547 mlen
= m
->m_len
- off
;
5548 if (mlen
!= 0 && m_mclhasreference(m
)) {
5553 * this mbuf is read-only.
5554 * allocate a new writable mbuf and try again.
5558 if (!(flags
& M_COPYBACK0_COW
))
5559 panic("m_copyback0: read-only");
5560 #endif /* DIAGNOSTIC */
5563 * if we're going to write into the middle of
5564 * a mbuf, split it first.
5566 if (off
> 0 && len
< mlen
) {
5567 n
= m_split0(m
, off
, how
, 0);
5578 * XXX TODO coalesce into the trailingspace of
5579 * the previous mbuf when possible.
5583 * allocate a new mbuf. copy packet header if needed.
5585 n
= _M_GET(how
, m
->m_type
);
5588 if (off
== 0 && (m
->m_flags
& M_PKTHDR
)) {
5589 M_COPY_PKTHDR(n
, m
);
5592 if (len
>= MINCLSIZE
)
5593 MCLGET(n
, M_DONTWAIT
);
5595 (n
->m_flags
& M_EXT
) ? MCLBYTES
: MLEN
;
5601 * free the region which has been overwritten.
5602 * copying data from old mbufs if requested.
5604 if (flags
& M_COPYBACK0_PRESERVE
)
5605 datap
= mtod(n
, char *);
5609 VERIFY(off
== 0 || eatlen
>= mlen
);
5611 VERIFY(len
>= mlen
);
5615 m_copydata(m
, off
, mlen
, datap
);
5622 while (m
!= NULL
&& m_mclhasreference(m
) &&
5623 n
->m_type
== m
->m_type
&& eatlen
> 0) {
5624 mlen
= MIN(eatlen
, m
->m_len
);
5626 m_copydata(m
, 0, mlen
, datap
);
5633 *mp
= m
= m_free(m
);
5641 mlen
= MIN(mlen
, len
);
5642 if (flags
& M_COPYBACK0_COPYBACK
) {
5643 bcopy(cp
, mtod(m
, caddr_t
) + off
, (unsigned)mlen
);
5652 if (m
->m_next
== NULL
) {
5659 if (((m
= *mp0
)->m_flags
& M_PKTHDR
) && (m
->m_pkthdr
.len
< totlen
)) {
5660 VERIFY(flags
& M_COPYBACK0_EXTEND
);
5661 m
->m_pkthdr
.len
= totlen
;
5671 mcl_to_paddr(char *addr
)
5673 vm_offset_t base_phys
;
5675 if (!MBUF_IN_MAP(addr
))
5677 base_phys
= mcl_paddr
[atop_64(addr
- (char *)mbutl
)];
5681 return ((uint64_t)(ptoa_64(base_phys
) | ((uint64_t)addr
& PAGE_MASK
)));
5685 * Dup the mbuf chain passed in. The whole thing. No cute additional cruft.
5686 * And really copy the thing. That way, we don't "precompute" checksums
5687 * for unsuspecting consumers. Assumption: m->m_nextpkt == 0. Trick: for
5688 * small packets, don't dup into a cluster. That way received packets
5689 * don't take up too much room in the sockbuf (cf. sbspace()).
5694 m_dup(struct mbuf
*m
, int how
)
5696 struct mbuf
*n
, **np
;
5702 if (m
->m_flags
& M_PKTHDR
)
5706 * Quick check: if we have one mbuf and its data fits in an
5707 * mbuf with packet header, just copy and go.
5709 if (m
->m_next
== NULL
) {
5710 /* Then just move the data into an mbuf and be done... */
5712 if (m
->m_pkthdr
.len
<= MHLEN
&& m
->m_len
<= MHLEN
) {
5713 if ((n
= _M_GETHDR(how
, m
->m_type
)) == NULL
)
5715 n
->m_len
= m
->m_len
;
5716 m_dup_pkthdr(n
, m
, how
);
5717 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
5720 } else if (m
->m_len
<= MLEN
) {
5721 if ((n
= _M_GET(how
, m
->m_type
)) == NULL
)
5723 bcopy(m
->m_data
, n
->m_data
, m
->m_len
);
5724 n
->m_len
= m
->m_len
;
5730 kprintf("<%x: %x, %x, %x\n", m
, m
->m_flags
, m
->m_len
,
5734 n
= _M_GETHDR(how
, m
->m_type
);
5736 n
= _M_GET(how
, m
->m_type
);
5739 if (m
->m_flags
& M_EXT
) {
5740 if (m
->m_len
<= m_maxsize(MC_CL
))
5742 else if (m
->m_len
<= m_maxsize(MC_BIGCL
))
5743 n
= m_mbigget(n
, how
);
5744 else if (m
->m_len
<= m_maxsize(MC_16KCL
) && njcl
> 0)
5745 n
= m_m16kget(n
, how
);
5746 if (!(n
->m_flags
& M_EXT
)) {
5753 /* Don't use M_COPY_PKTHDR: preserve m_data */
5754 m_dup_pkthdr(n
, m
, how
);
5756 if (!(n
->m_flags
& M_EXT
))
5757 n
->m_data
= n
->m_pktdat
;
5759 n
->m_len
= m
->m_len
;
5761 * Get the dup on the same bdry as the original
5762 * Assume that the two mbufs have the same offset to data area
5763 * (up to word boundaries)
5765 bcopy(MTOD(m
, caddr_t
), MTOD(n
, caddr_t
), (unsigned)n
->m_len
);
5769 kprintf(">%x: %x, %x, %x\n", n
, n
->m_flags
, n
->m_len
,
5784 #define MBUF_MULTIPAGES(m) \
5785 (((m)->m_flags & M_EXT) && \
5786 ((IS_P2ALIGNED((m)->m_data, PAGE_SIZE) \
5787 && (m)->m_len > PAGE_SIZE) || \
5788 (!IS_P2ALIGNED((m)->m_data, PAGE_SIZE) && \
5789 P2ROUNDUP((m)->m_data, PAGE_SIZE) < ((uintptr_t)(m)->m_data + (m)->m_len))))
5791 static struct mbuf
*
5792 m_expand(struct mbuf
*m
, struct mbuf
**last
)
5794 struct mbuf
*top
= NULL
;
5795 struct mbuf
**nm
= &top
;
5796 uintptr_t data0
, data
;
5797 unsigned int len0
, len
;
5799 VERIFY(MBUF_MULTIPAGES(m
));
5800 VERIFY(m
->m_next
== NULL
);
5801 data0
= (uintptr_t)m
->m_data
;
5809 if (IS_P2ALIGNED(data
, PAGE_SIZE
) && len0
> PAGE_SIZE
)
5811 else if (!IS_P2ALIGNED(data
, PAGE_SIZE
) &&
5812 P2ROUNDUP(data
, PAGE_SIZE
) < (data
+ len0
))
5813 len
= P2ROUNDUP(data
, PAGE_SIZE
) - data
;
5818 VERIFY(m
->m_flags
& M_EXT
);
5819 m
->m_data
= (void *)data
;
5831 n
= _M_RETRY(M_DONTWAIT
, MT_DATA
);
5838 n
->m_ext
= m
->m_ext
;
5840 n
->m_flags
|= M_EXT
;
5847 m_normalize(struct mbuf
*m
)
5849 struct mbuf
*top
= NULL
;
5850 struct mbuf
**nm
= &top
;
5851 boolean_t expanded
= FALSE
;
5859 /* Does the data cross one or more page boundaries? */
5860 if (MBUF_MULTIPAGES(m
)) {
5862 if ((m
= m_expand(m
, &last
)) == NULL
) {
5878 atomic_add_32(&mb_normalized
, 1);
5883 * Append the specified data to the indicated mbuf chain,
5884 * Extend the mbuf chain if the new data does not fit in
5887 * Return 1 if able to complete the job; otherwise 0.
5890 m_append(struct mbuf
*m0
, int len
, caddr_t cp
)
5893 int remainder
, space
;
5895 for (m
= m0
; m
->m_next
!= NULL
; m
= m
->m_next
)
5898 space
= M_TRAILINGSPACE(m
);
5901 * Copy into available space.
5903 if (space
> remainder
)
5905 bcopy(cp
, mtod(m
, caddr_t
) + m
->m_len
, space
);
5907 cp
+= space
, remainder
-= space
;
5909 while (remainder
> 0) {
5911 * Allocate a new mbuf; could check space
5912 * and allocate a cluster instead.
5914 n
= m_get(M_WAITOK
, m
->m_type
);
5917 n
->m_len
= min(MLEN
, remainder
);
5918 bcopy(cp
, mtod(n
, caddr_t
), n
->m_len
);
5920 remainder
-= n
->m_len
;
5924 if (m0
->m_flags
& M_PKTHDR
)
5925 m0
->m_pkthdr
.len
+= len
- remainder
;
5926 return (remainder
== 0);
5930 m_last(struct mbuf
*m
)
5932 while (m
->m_next
!= NULL
)
5938 m_fixhdr(struct mbuf
*m0
)
5942 VERIFY(m0
->m_flags
& M_PKTHDR
);
5944 len
= m_length2(m0
, NULL
);
5945 m0
->m_pkthdr
.len
= len
;
5950 m_length2(struct mbuf
*m0
, struct mbuf
**last
)
5956 for (m
= m0
; m
!= NULL
; m
= m
->m_next
) {
5958 if (m
->m_next
== NULL
)
5967 * Defragment a mbuf chain, returning the shortest possible chain of mbufs
5968 * and clusters. If allocation fails and this cannot be completed, NULL will
5969 * be returned, but the passed in chain will be unchanged. Upon success,
5970 * the original chain will be freed, and the new chain will be returned.
5972 * If a non-packet header is passed in, the original mbuf (chain?) will
5973 * be returned unharmed.
5975 * If offset is specfied, the first mbuf in the chain will have a leading
5976 * space of the amount stated by the "off" parameter.
5978 * This routine requires that the m_pkthdr.header field of the original
5979 * mbuf chain is cleared by the caller.
5982 m_defrag_offset(struct mbuf
*m0
, u_int32_t off
, int how
)
5984 struct mbuf
*m_new
= NULL
, *m_final
= NULL
;
5985 int progress
= 0, length
, pktlen
;
5987 if (!(m0
->m_flags
& M_PKTHDR
))
5990 VERIFY(off
< MHLEN
);
5991 m_fixhdr(m0
); /* Needed sanity check */
5993 pktlen
= m0
->m_pkthdr
.len
+ off
;
5995 m_final
= m_getcl(how
, MT_DATA
, M_PKTHDR
);
5997 m_final
= m_gethdr(how
, MT_DATA
);
5999 if (m_final
== NULL
)
6004 m_final
->m_data
+= off
;
6008 * Caller must have handled the contents pointed to by this
6009 * pointer before coming here, as otherwise it will point to
6010 * the original mbuf which will get freed upon success.
6012 VERIFY(m0
->m_pkthdr
.pkt_hdr
== NULL
);
6014 if (m_dup_pkthdr(m_final
, m0
, how
) == 0)
6019 while (progress
< pktlen
) {
6020 length
= pktlen
- progress
;
6021 if (length
> MCLBYTES
)
6023 length
-= ((m_new
== m_final
) ? off
: 0);
6025 if (m_new
== NULL
) {
6027 m_new
= m_getcl(how
, MT_DATA
, 0);
6029 m_new
= m_get(how
, MT_DATA
);
6034 m_copydata(m0
, progress
, length
, mtod(m_new
, caddr_t
));
6036 m_new
->m_len
= length
;
6037 if (m_new
!= m_final
)
6038 m_cat(m_final
, m_new
);
6051 m_defrag(struct mbuf
*m0
, int how
)
6053 return (m_defrag_offset(m0
, 0, how
));
6057 m_mchtype(struct mbuf
*m
, int t
)
6060 mtype_stat_dec(m
->m_type
);
6065 m_mtod(struct mbuf
*m
)
6067 return (MTOD(m
, void *));
6073 return ((struct mbuf
*)((uintptr_t)(x
) & ~(MSIZE
-1)));
6077 m_mcheck(struct mbuf
*m
)
6083 * Return a pointer to mbuf/offset of location in mbuf chain.
6086 m_getptr(struct mbuf
*m
, int loc
, int *off
)
6090 /* Normal end of search. */
6091 if (m
->m_len
> loc
) {
6096 if (m
->m_next
== NULL
) {
6098 /* Point at the end of valid data. */
6111 * Inform the corresponding mcache(s) that there's a waiter below.
6114 mbuf_waiter_inc(mbuf_class_t
class, boolean_t comp
)
6116 mcache_waiter_inc(m_cache(class));
6118 if (class == MC_CL
) {
6119 mcache_waiter_inc(m_cache(MC_MBUF_CL
));
6120 } else if (class == MC_BIGCL
) {
6121 mcache_waiter_inc(m_cache(MC_MBUF_BIGCL
));
6122 } else if (class == MC_16KCL
) {
6123 mcache_waiter_inc(m_cache(MC_MBUF_16KCL
));
6125 mcache_waiter_inc(m_cache(MC_MBUF_CL
));
6126 mcache_waiter_inc(m_cache(MC_MBUF_BIGCL
));
6132 * Inform the corresponding mcache(s) that there's no more waiter below.
6135 mbuf_waiter_dec(mbuf_class_t
class, boolean_t comp
)
6137 mcache_waiter_dec(m_cache(class));
6139 if (class == MC_CL
) {
6140 mcache_waiter_dec(m_cache(MC_MBUF_CL
));
6141 } else if (class == MC_BIGCL
) {
6142 mcache_waiter_dec(m_cache(MC_MBUF_BIGCL
));
6143 } else if (class == MC_16KCL
) {
6144 mcache_waiter_dec(m_cache(MC_MBUF_16KCL
));
6146 mcache_waiter_dec(m_cache(MC_MBUF_CL
));
6147 mcache_waiter_dec(m_cache(MC_MBUF_BIGCL
));
6153 * Called during slab (blocking and non-blocking) allocation. If there
6154 * is at least one waiter, and the time since the first waiter is blocked
6155 * is greater than the watchdog timeout, panic the system.
6163 if (mb_waiters
== 0 || !mb_watchdog
)
6167 since
= now
.tv_sec
- mb_wdtstart
.tv_sec
;
6168 if (since
>= MB_WDT_MAXTIME
) {
6169 panic_plain("%s: %d waiters stuck for %u secs\n%s", __func__
,
6170 mb_waiters
, since
, mbuf_dump());
6176 * Called during blocking allocation. Returns TRUE if one or more objects
6177 * are available at the per-CPU caches layer and that allocation should be
6178 * retried at that level.
6181 mbuf_sleep(mbuf_class_t
class, unsigned int num
, int wait
)
6183 boolean_t mcache_retry
= FALSE
;
6185 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
6187 /* Check if there's anything at the cache layer */
6188 if (mbuf_cached_above(class, wait
)) {
6189 mcache_retry
= TRUE
;
6193 /* Nothing? Then try hard to get it from somewhere */
6194 m_reclaim(class, num
, (wait
& MCR_COMP
));
6196 /* We tried hard and got something? */
6197 if (m_infree(class) > 0) {
6200 } else if (mbuf_cached_above(class, wait
)) {
6202 mcache_retry
= TRUE
;
6204 } else if (wait
& MCR_TRYHARD
) {
6205 mcache_retry
= TRUE
;
6210 * There's really nothing for us right now; inform the
6211 * cache(s) that there is a waiter below and go to sleep.
6213 mbuf_waiter_inc(class, (wait
& MCR_COMP
));
6215 VERIFY(!(wait
& MCR_NOSLEEP
));
6218 * If this is the first waiter, arm the watchdog timer. Otherwise
6219 * check if we need to panic the system due to watchdog timeout.
6221 if (mb_waiters
== 0)
6222 microuptime(&mb_wdtstart
);
6227 (void) msleep(mb_waitchan
, mbuf_mlock
, (PZERO
-1), m_cname(class), NULL
);
6229 /* We are now up; stop getting notified until next round */
6230 mbuf_waiter_dec(class, (wait
& MCR_COMP
));
6232 /* We waited and got something */
6233 if (m_infree(class) > 0) {
6236 } else if (mbuf_cached_above(class, wait
)) {
6238 mcache_retry
= TRUE
;
6241 return (mcache_retry
);
6245 mbuf_worker_thread(void)
6250 lck_mtx_lock(mbuf_mlock
);
6253 if (mbuf_expand_mcl
) {
6256 /* Adjust to current number of cluster in use */
6257 n
= mbuf_expand_mcl
-
6258 (m_total(MC_CL
) - m_infree(MC_CL
));
6259 if ((n
+ m_total(MC_CL
)) > m_maxlimit(MC_CL
))
6260 n
= m_maxlimit(MC_CL
) - m_total(MC_CL
);
6261 mbuf_expand_mcl
= 0;
6263 if (n
> 0 && freelist_populate(MC_CL
, n
, M_WAIT
) > 0)
6266 if (mbuf_expand_big
) {
6269 /* Adjust to current number of 4 KB cluster in use */
6270 n
= mbuf_expand_big
-
6271 (m_total(MC_BIGCL
) - m_infree(MC_BIGCL
));
6272 if ((n
+ m_total(MC_BIGCL
)) > m_maxlimit(MC_BIGCL
))
6273 n
= m_maxlimit(MC_BIGCL
) - m_total(MC_BIGCL
);
6274 mbuf_expand_big
= 0;
6276 if (n
> 0 && freelist_populate(MC_BIGCL
, n
, M_WAIT
) > 0)
6279 if (mbuf_expand_16k
) {
6282 /* Adjust to current number of 16 KB cluster in use */
6283 n
= mbuf_expand_16k
-
6284 (m_total(MC_16KCL
) - m_infree(MC_16KCL
));
6285 if ((n
+ m_total(MC_16KCL
)) > m_maxlimit(MC_16KCL
))
6286 n
= m_maxlimit(MC_16KCL
) - m_total(MC_16KCL
);
6287 mbuf_expand_16k
= 0;
6290 (void) freelist_populate(MC_16KCL
, n
, M_WAIT
);
6294 * Because we can run out of memory before filling the mbuf
6295 * map, we should not allocate more clusters than they are
6296 * mbufs -- otherwise we could have a large number of useless
6297 * clusters allocated.
6300 while (m_total(MC_MBUF
) <
6301 (m_total(MC_BIGCL
) + m_total(MC_CL
))) {
6302 if (freelist_populate(MC_MBUF
, 1, M_WAIT
) == 0)
6307 lck_mtx_unlock(mbuf_mlock
);
6309 assert_wait(&mbuf_worker_run
, THREAD_UNINT
);
6310 (void) thread_block((thread_continue_t
)mbuf_worker_thread
);
6315 mbuf_worker_thread_init(void)
6317 mbuf_worker_ready
++;
6318 mbuf_worker_thread();
6327 lck_mtx_assert(mbuf_mlock
, LCK_MTX_ASSERT_OWNED
);
6329 VERIFY(MBUF_IN_MAP(buf
));
6330 ix
= ((unsigned char *)buf
- mbutl
) >> MBSHIFT
;
6331 VERIFY(ix
< maxslabgrp
);
6333 if ((slg
= slabstbl
[ix
]) == NULL
) {
6335 * In the current implementation, we never shrink the slabs
6336 * table; if we attempt to reallocate a cluster group when
6337 * it's already allocated, panic since this is a sign of a
6338 * memory corruption (slabstbl[ix] got nullified).
6341 VERIFY(ix
< slabgrp
);
6343 * Slabs expansion can only be done single threaded; when
6344 * we get here, it must be as a result of m_clalloc() which
6345 * is serialized and therefore mb_clalloc_busy must be set.
6347 VERIFY(mb_clalloc_busy
);
6348 lck_mtx_unlock(mbuf_mlock
);
6350 /* This is a new buffer; create the slabs group for it */
6351 MALLOC(slg
, mcl_slabg_t
*, sizeof (*slg
), M_TEMP
,
6353 MALLOC(slg
->slg_slab
, mcl_slab_t
*, sizeof(mcl_slab_t
) * NSLABSPMB
,
6354 M_TEMP
, M_WAITOK
| M_ZERO
);
6355 VERIFY(slg
!= NULL
&& slg
->slg_slab
!= NULL
);
6357 lck_mtx_lock(mbuf_mlock
);
6359 * No other thread could have gone into m_clalloc() after
6360 * we dropped the lock above, so verify that it's true.
6362 VERIFY(mb_clalloc_busy
);
6366 /* Chain each slab in the group to its forward neighbor */
6367 for (k
= 1; k
< NSLABSPMB
; k
++)
6368 slg
->slg_slab
[k
- 1].sl_next
= &slg
->slg_slab
[k
];
6369 VERIFY(slg
->slg_slab
[NSLABSPMB
- 1].sl_next
== NULL
);
6371 /* And chain the last slab in the previous group to this */
6373 VERIFY(slabstbl
[ix
- 1]->
6374 slg_slab
[NSLABSPMB
- 1].sl_next
== NULL
);
6375 slabstbl
[ix
- 1]->slg_slab
[NSLABSPMB
- 1].sl_next
=
6380 ix
= MTOPG(buf
) % NSLABSPMB
;
6381 VERIFY(ix
< NSLABSPMB
);
6383 return (&slg
->slg_slab
[ix
]);
6387 slab_init(mcl_slab_t
*sp
, mbuf_class_t
class, u_int32_t flags
,
6388 void *base
, void *head
, unsigned int len
, int refcnt
, int chunks
)
6390 sp
->sl_class
= class;
6391 sp
->sl_flags
= flags
;
6395 sp
->sl_refcnt
= refcnt
;
6396 sp
->sl_chunks
= chunks
;
6401 slab_insert(mcl_slab_t
*sp
, mbuf_class_t
class)
6403 VERIFY(slab_is_detached(sp
));
6404 m_slab_cnt(class)++;
6405 TAILQ_INSERT_TAIL(&m_slablist(class), sp
, sl_link
);
6406 sp
->sl_flags
&= ~SLF_DETACHED
;
6409 * If a buffer spans multiple contiguous pages then mark them as
6412 if (class == MC_16KCL
) {
6414 for (k
= 1; k
< NSLABSP16KB
; k
++) {
6416 /* Next slab must already be present */
6417 VERIFY(sp
!= NULL
&& slab_is_detached(sp
));
6418 sp
->sl_flags
&= ~SLF_DETACHED
;
6424 slab_remove(mcl_slab_t
*sp
, mbuf_class_t
class)
6427 VERIFY(!slab_is_detached(sp
));
6428 VERIFY(m_slab_cnt(class) > 0);
6429 m_slab_cnt(class)--;
6430 TAILQ_REMOVE(&m_slablist(class), sp
, sl_link
);
6432 if (class == MC_16KCL
) {
6433 for (k
= 1; k
< NSLABSP16KB
; k
++) {
6435 /* Next slab must already be present */
6437 VERIFY(!slab_is_detached(sp
));
6444 slab_inrange(mcl_slab_t
*sp
, void *buf
)
6446 return ((uintptr_t)buf
>= (uintptr_t)sp
->sl_base
&&
6447 (uintptr_t)buf
< ((uintptr_t)sp
->sl_base
+ sp
->sl_len
));
6453 slab_nextptr_panic(mcl_slab_t
*sp
, void *addr
)
6456 unsigned int chunk_len
= sp
->sl_len
/ sp
->sl_chunks
;
6457 uintptr_t buf
= (uintptr_t)sp
->sl_base
;
6459 for (i
= 0; i
< sp
->sl_chunks
; i
++, buf
+= chunk_len
) {
6460 void *next
= ((mcache_obj_t
*)buf
)->obj_next
;
6464 if (next
!= NULL
&& !MBUF_IN_MAP(next
)) {
6465 mcache_t
*cp
= m_cache(sp
->sl_class
);
6466 panic("%s: %s buffer %p in slab %p modified "
6467 "after free at offset 0: %p out of range "
6468 "[%p-%p)\n", __func__
, cp
->mc_name
,
6469 (void *)buf
, sp
, next
, mbutl
, embutl
);
6473 mcache_audit_t
*mca
= mcl_audit_buf2mca(sp
->sl_class
,
6474 (mcache_obj_t
*)buf
);
6475 mcl_audit_verify_nextptr(next
, mca
);
6481 slab_detach(mcl_slab_t
*sp
)
6483 sp
->sl_link
.tqe_next
= (mcl_slab_t
*)-1;
6484 sp
->sl_link
.tqe_prev
= (mcl_slab_t
**)-1;
6485 sp
->sl_flags
|= SLF_DETACHED
;
6489 slab_is_detached(mcl_slab_t
*sp
)
6491 return ((intptr_t)sp
->sl_link
.tqe_next
== -1 &&
6492 (intptr_t)sp
->sl_link
.tqe_prev
== -1 &&
6493 (sp
->sl_flags
& SLF_DETACHED
));
6497 mcl_audit_init(void *buf
, mcache_audit_t
**mca_list
,
6498 mcache_obj_t
**con_list
, size_t con_size
, unsigned int num
)
6500 mcache_audit_t
*mca
, *mca_tail
;
6501 mcache_obj_t
*con
= NULL
;
6502 boolean_t save_contents
= (con_list
!= NULL
);
6505 ASSERT(num
<= NMBPG
);
6506 ASSERT(con_list
== NULL
|| con_size
!= 0);
6509 VERIFY(ix
< maxclaudit
);
6511 /* Make sure we haven't been here before */
6512 for (i
= 0; i
< NMBPG
; i
++)
6513 VERIFY(mclaudit
[ix
].cl_audit
[i
] == NULL
);
6515 mca
= mca_tail
= *mca_list
;
6519 for (i
= 0; i
< num
; i
++) {
6520 mcache_audit_t
*next
;
6522 next
= mca
->mca_next
;
6523 bzero(mca
, sizeof (*mca
));
6524 mca
->mca_next
= next
;
6525 mclaudit
[ix
].cl_audit
[i
] = mca
;
6527 /* Attach the contents buffer if requested */
6528 if (save_contents
) {
6529 mcl_saved_contents_t
*msc
=
6530 (mcl_saved_contents_t
*)(void *)con
;
6532 VERIFY(msc
!= NULL
);
6533 VERIFY(IS_P2ALIGNED(msc
, sizeof (u_int64_t
)));
6534 VERIFY(con_size
== sizeof (*msc
));
6535 mca
->mca_contents_size
= con_size
;
6536 mca
->mca_contents
= msc
;
6537 con
= con
->obj_next
;
6538 bzero(mca
->mca_contents
, mca
->mca_contents_size
);
6542 mca
= mca
->mca_next
;
6548 *mca_list
= mca_tail
->mca_next
;
6549 mca_tail
->mca_next
= NULL
;
6553 mcl_audit_free(void *buf
, unsigned int num
)
6556 mcache_audit_t
*mca
, *mca_list
;
6559 VERIFY(ix
< maxclaudit
);
6561 if (mclaudit
[ix
].cl_audit
[0] != NULL
) {
6562 mca_list
= mclaudit
[ix
].cl_audit
[0];
6563 for (i
= 0; i
< num
; i
++) {
6564 mca
= mclaudit
[ix
].cl_audit
[i
];
6565 mclaudit
[ix
].cl_audit
[i
] = NULL
;
6566 if (mca
->mca_contents
)
6567 mcache_free(mcl_audit_con_cache
,
6570 mcache_free_ext(mcache_audit_cache
,
6571 (mcache_obj_t
*)mca_list
);
6576 * Given an address of a buffer (mbuf/2KB/4KB/16KB), return
6577 * the corresponding audit structure for that buffer.
6579 static mcache_audit_t
*
6580 mcl_audit_buf2mca(mbuf_class_t
class, mcache_obj_t
*mobj
)
6582 mcache_audit_t
*mca
= NULL
;
6583 int ix
= MTOPG(mobj
), m_idx
= 0;
6584 unsigned char *page_addr
;
6586 VERIFY(ix
< maxclaudit
);
6587 VERIFY(IS_P2ALIGNED(mobj
, MIN(m_maxsize(class), PAGE_SIZE
)));
6589 page_addr
= PGTOM(ix
);
6594 * For the mbuf case, find the index of the page
6595 * used by the mbuf and use that index to locate the
6596 * base address of the page. Then find out the
6597 * mbuf index relative to the page base and use
6598 * it to locate the audit structure.
6600 m_idx
= MBPAGEIDX(page_addr
, mobj
);
6601 VERIFY(m_idx
< (int)NMBPG
);
6602 mca
= mclaudit
[ix
].cl_audit
[m_idx
];
6607 * Same thing as above, but for 2KB clusters in a page.
6609 m_idx
= CLPAGEIDX(page_addr
, mobj
);
6610 VERIFY(m_idx
< (int)NCLPG
);
6611 mca
= mclaudit
[ix
].cl_audit
[m_idx
];
6615 m_idx
= BCLPAGEIDX(page_addr
, mobj
);
6616 VERIFY(m_idx
< (int)NBCLPG
);
6617 mca
= mclaudit
[ix
].cl_audit
[m_idx
];
6621 * Same as above, but only return the first element.
6623 mca
= mclaudit
[ix
].cl_audit
[0];
6635 mcl_audit_mbuf(mcache_audit_t
*mca
, void *addr
, boolean_t composite
,
6638 struct mbuf
*m
= addr
;
6639 mcache_obj_t
*next
= ((mcache_obj_t
*)m
)->obj_next
;
6641 VERIFY(mca
->mca_contents
!= NULL
&&
6642 mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
6645 mcl_audit_verify_nextptr(next
, mca
);
6648 /* Save constructed mbuf fields */
6649 mcl_audit_save_mbuf(m
, mca
);
6651 mcache_set_pattern(MCACHE_FREE_PATTERN
, m
,
6652 m_maxsize(MC_MBUF
));
6654 ((mcache_obj_t
*)m
)->obj_next
= next
;
6658 /* Check if the buffer has been corrupted while in freelist */
6660 mcache_audit_free_verify_set(mca
, addr
, 0, m_maxsize(MC_MBUF
));
6662 /* Restore constructed mbuf fields */
6663 mcl_audit_restore_mbuf(m
, mca
, composite
);
6667 mcl_audit_restore_mbuf(struct mbuf
*m
, mcache_audit_t
*mca
, boolean_t composite
)
6669 struct mbuf
*ms
= MCA_SAVED_MBUF_PTR(mca
);
6672 struct mbuf
*next
= m
->m_next
;
6673 VERIFY(ms
->m_flags
== M_EXT
&& MEXT_RFA(ms
) != NULL
&&
6674 MBUF_IS_COMPOSITE(ms
));
6675 VERIFY(mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
6677 * We could have hand-picked the mbuf fields and restore
6678 * them individually, but that will be a maintenance
6679 * headache. Instead, restore everything that was saved;
6680 * the mbuf layer will recheck and reinitialize anyway.
6682 bcopy(ms
, m
, MCA_SAVED_MBUF_SIZE
);
6686 * For a regular mbuf (no cluster attached) there's nothing
6687 * to restore other than the type field, which is expected
6690 m
->m_type
= ms
->m_type
;
6696 mcl_audit_save_mbuf(struct mbuf
*m
, mcache_audit_t
*mca
)
6698 VERIFY(mca
->mca_contents_size
== AUDIT_CONTENTS_SIZE
);
6700 bcopy(m
, MCA_SAVED_MBUF_PTR(mca
), MCA_SAVED_MBUF_SIZE
);
6704 mcl_audit_cluster(mcache_audit_t
*mca
, void *addr
, size_t size
, boolean_t alloc
,
6705 boolean_t save_next
)
6707 mcache_obj_t
*next
= ((mcache_obj_t
*)addr
)->obj_next
;
6711 mcache_set_pattern(MCACHE_FREE_PATTERN
, addr
, size
);
6714 mcl_audit_verify_nextptr(next
, mca
);
6715 ((mcache_obj_t
*)addr
)->obj_next
= next
;
6717 } else if (mclverify
) {
6718 /* Check if the buffer has been corrupted while in freelist */
6719 mcl_audit_verify_nextptr(next
, mca
);
6720 mcache_audit_free_verify_set(mca
, addr
, 0, size
);
6725 mcl_audit_scratch(mcache_audit_t
*mca
)
6727 void *stack
[MCACHE_STACK_DEPTH
+ 1];
6728 mcl_scratch_audit_t
*msa
;
6731 VERIFY(mca
->mca_contents
!= NULL
);
6732 msa
= MCA_SAVED_SCRATCH_PTR(mca
);
6734 msa
->msa_pthread
= msa
->msa_thread
;
6735 msa
->msa_thread
= current_thread();
6736 bcopy(msa
->msa_stack
, msa
->msa_pstack
, sizeof (msa
->msa_pstack
));
6737 msa
->msa_pdepth
= msa
->msa_depth
;
6738 bzero(stack
, sizeof (stack
));
6739 msa
->msa_depth
= OSBacktrace(stack
, MCACHE_STACK_DEPTH
+ 1) - 1;
6740 bcopy(&stack
[1], msa
->msa_stack
, sizeof (msa
->msa_stack
));
6742 msa
->msa_ptstamp
= msa
->msa_tstamp
;
6744 /* tstamp is in ms relative to base_ts */
6745 msa
->msa_tstamp
= ((now
.tv_usec
- mb_start
.tv_usec
) / 1000);
6746 if ((now
.tv_sec
- mb_start
.tv_sec
) > 0)
6747 msa
->msa_tstamp
+= ((now
.tv_sec
- mb_start
.tv_sec
) * 1000);
6751 mcl_audit_mcheck_panic(struct mbuf
*m
)
6753 mcache_audit_t
*mca
;
6756 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
6758 panic("mcl_audit: freed mbuf %p with type 0x%x (instead of 0x%x)\n%s\n",
6759 m
, (u_int16_t
)m
->m_type
, MT_FREE
, mcache_dump_mca(mca
));
6764 mcl_audit_verify_nextptr(void *next
, mcache_audit_t
*mca
)
6766 if (next
!= NULL
&& !MBUF_IN_MAP(next
) &&
6767 (next
!= (void *)MCACHE_FREE_PATTERN
|| !mclverify
)) {
6768 panic("mcl_audit: buffer %p modified after free at offset 0: "
6769 "%p out of range [%p-%p)\n%s\n",
6770 mca
->mca_addr
, next
, mbutl
, embutl
, mcache_dump_mca(mca
));
6775 /* This function turns on mbuf leak detection */
6777 mleak_activate(void)
6779 mleak_table
.mleak_sample_factor
= MLEAK_SAMPLE_FACTOR
;
6780 PE_parse_boot_argn("mleak_sample_factor",
6781 &mleak_table
.mleak_sample_factor
,
6782 sizeof (mleak_table
.mleak_sample_factor
));
6784 if (mleak_table
.mleak_sample_factor
== 0)
6787 if (mclfindleak
== 0)
6790 vm_size_t alloc_size
=
6791 mleak_alloc_buckets
* sizeof (struct mallocation
);
6792 vm_size_t trace_size
= mleak_trace_buckets
* sizeof (struct mtrace
);
6794 MALLOC(mleak_allocations
, struct mallocation
*, alloc_size
,
6795 M_TEMP
, M_WAITOK
| M_ZERO
);
6796 VERIFY(mleak_allocations
!= NULL
);
6798 MALLOC(mleak_traces
, struct mtrace
*, trace_size
,
6799 M_TEMP
, M_WAITOK
| M_ZERO
);
6800 VERIFY(mleak_traces
!= NULL
);
6802 MALLOC(mleak_stat
, mleak_stat_t
*, MLEAK_STAT_SIZE(MLEAK_NUM_TRACES
),
6803 M_TEMP
, M_WAITOK
| M_ZERO
);
6804 VERIFY(mleak_stat
!= NULL
);
6805 mleak_stat
->ml_cnt
= MLEAK_NUM_TRACES
;
6807 mleak_stat
->ml_isaddr64
= 1;
6808 #endif /* __LP64__ */
6812 mleak_logger(u_int32_t num
, mcache_obj_t
*addr
, boolean_t alloc
)
6816 if (mclfindleak
== 0)
6820 return (mleak_free(addr
));
6822 temp
= atomic_add_32_ov(&mleak_table
.mleak_capture
, 1);
6824 if ((temp
% mleak_table
.mleak_sample_factor
) == 0 && addr
!= NULL
) {
6825 uintptr_t bt
[MLEAK_STACK_DEPTH
];
6826 int logged
= fastbacktrace(bt
, MLEAK_STACK_DEPTH
);
6827 mleak_log(bt
, addr
, logged
, num
);
6832 * This function records the allocation in the mleak_allocations table
6833 * and the backtrace in the mleak_traces table; if allocation slot is in use,
6834 * replace old allocation with new one if the trace slot is in use, return
6835 * (or increment refcount if same trace).
6838 mleak_log(uintptr_t *bt
, mcache_obj_t
*addr
, uint32_t depth
, int num
)
6840 struct mallocation
*allocation
;
6841 struct mtrace
*trace
;
6842 uint32_t trace_index
;
6844 /* Quit if someone else modifying the tables */
6845 if (!lck_mtx_try_lock_spin(mleak_lock
)) {
6846 mleak_table
.total_conflicts
++;
6850 allocation
= &mleak_allocations
[hashaddr((uintptr_t)addr
,
6851 mleak_alloc_buckets
)];
6852 trace_index
= hashbacktrace(bt
, depth
, mleak_trace_buckets
);
6853 trace
= &mleak_traces
[trace_index
];
6855 VERIFY(allocation
<= &mleak_allocations
[mleak_alloc_buckets
- 1]);
6856 VERIFY(trace
<= &mleak_traces
[mleak_trace_buckets
- 1]);
6858 allocation
->hitcount
++;
6862 * If the allocation bucket we want is occupied
6863 * and the occupier has the same trace, just bail.
6865 if (allocation
->element
!= NULL
&&
6866 trace_index
== allocation
->trace_index
) {
6867 mleak_table
.alloc_collisions
++;
6868 lck_mtx_unlock(mleak_lock
);
6873 * Store the backtrace in the traces array;
6874 * Size of zero = trace bucket is free.
6876 if (trace
->allocs
> 0 &&
6877 bcmp(trace
->addr
, bt
, (depth
* sizeof (uintptr_t))) != 0) {
6878 /* Different, unique trace, but the same hash! Bail out. */
6879 trace
->collisions
++;
6880 mleak_table
.trace_collisions
++;
6881 lck_mtx_unlock(mleak_lock
);
6883 } else if (trace
->allocs
> 0) {
6884 /* Same trace, already added, so increment refcount */
6887 /* Found an unused trace bucket, so record the trace here */
6888 if (trace
->depth
!= 0) {
6889 /* this slot previously used but not currently in use */
6890 mleak_table
.trace_overwrites
++;
6892 mleak_table
.trace_recorded
++;
6894 memcpy(trace
->addr
, bt
, (depth
* sizeof (uintptr_t)));
6895 trace
->depth
= depth
;
6896 trace
->collisions
= 0;
6899 /* Step 2: Store the allocation record in the allocations array */
6900 if (allocation
->element
!= NULL
) {
6902 * Replace an existing allocation. No need to preserve
6903 * because only a subset of the allocations are being
6906 mleak_table
.alloc_collisions
++;
6907 } else if (allocation
->trace_index
!= 0) {
6908 mleak_table
.alloc_overwrites
++;
6910 allocation
->element
= addr
;
6911 allocation
->trace_index
= trace_index
;
6912 allocation
->count
= num
;
6913 mleak_table
.alloc_recorded
++;
6914 mleak_table
.outstanding_allocs
++;
6916 lck_mtx_unlock(mleak_lock
);
6921 mleak_free(mcache_obj_t
*addr
)
6923 while (addr
!= NULL
) {
6924 struct mallocation
*allocation
= &mleak_allocations
6925 [hashaddr((uintptr_t)addr
, mleak_alloc_buckets
)];
6927 if (allocation
->element
== addr
&&
6928 allocation
->trace_index
< mleak_trace_buckets
) {
6929 lck_mtx_lock_spin(mleak_lock
);
6930 if (allocation
->element
== addr
&&
6931 allocation
->trace_index
< mleak_trace_buckets
) {
6932 struct mtrace
*trace
;
6933 trace
= &mleak_traces
[allocation
->trace_index
];
6934 /* allocs = 0 means trace bucket is unused */
6935 if (trace
->allocs
> 0)
6937 if (trace
->allocs
== 0)
6939 /* NULL element means alloc bucket is unused */
6940 allocation
->element
= NULL
;
6941 mleak_table
.outstanding_allocs
--;
6943 lck_mtx_unlock(mleak_lock
);
6945 addr
= addr
->obj_next
;
6953 struct mtrace
*swap
;
6955 for(i
= 0; i
< MLEAK_NUM_TRACES
; i
++)
6956 mleak_top_trace
[i
] = NULL
;
6958 for(i
= 0, j
= 0; j
< MLEAK_NUM_TRACES
&& i
< mleak_trace_buckets
; i
++)
6960 if (mleak_traces
[i
].allocs
<= 0)
6963 mleak_top_trace
[j
] = &mleak_traces
[i
];
6964 for (k
= j
; k
> 0; k
--) {
6965 if (mleak_top_trace
[k
]->allocs
<=
6966 mleak_top_trace
[k
-1]->allocs
)
6969 swap
= mleak_top_trace
[k
-1];
6970 mleak_top_trace
[k
-1] = mleak_top_trace
[k
];
6971 mleak_top_trace
[k
] = swap
;
6977 for(; i
< mleak_trace_buckets
; i
++) {
6978 if (mleak_traces
[i
].allocs
<= mleak_top_trace
[j
]->allocs
)
6981 mleak_top_trace
[j
] = &mleak_traces
[i
];
6983 for (k
= j
; k
> 0; k
--) {
6984 if (mleak_top_trace
[k
]->allocs
<=
6985 mleak_top_trace
[k
-1]->allocs
)
6988 swap
= mleak_top_trace
[k
-1];
6989 mleak_top_trace
[k
-1] = mleak_top_trace
[k
];
6990 mleak_top_trace
[k
] = swap
;
6996 mleak_update_stats()
6998 mleak_trace_stat_t
*mltr
;
7001 VERIFY(mleak_stat
!= NULL
);
7003 VERIFY(mleak_stat
->ml_isaddr64
);
7005 VERIFY(!mleak_stat
->ml_isaddr64
);
7006 #endif /* !__LP64__ */
7007 VERIFY(mleak_stat
->ml_cnt
== MLEAK_NUM_TRACES
);
7009 mleak_sort_traces();
7011 mltr
= &mleak_stat
->ml_trace
[0];
7012 bzero(mltr
, sizeof (*mltr
) * MLEAK_NUM_TRACES
);
7013 for (i
= 0; i
< MLEAK_NUM_TRACES
; i
++) {
7016 if (mleak_top_trace
[i
] == NULL
||
7017 mleak_top_trace
[i
]->allocs
== 0)
7020 mltr
->mltr_collisions
= mleak_top_trace
[i
]->collisions
;
7021 mltr
->mltr_hitcount
= mleak_top_trace
[i
]->hitcount
;
7022 mltr
->mltr_allocs
= mleak_top_trace
[i
]->allocs
;
7023 mltr
->mltr_depth
= mleak_top_trace
[i
]->depth
;
7025 VERIFY(mltr
->mltr_depth
<= MLEAK_STACK_DEPTH
);
7026 for (j
= 0; j
< mltr
->mltr_depth
; j
++)
7027 mltr
->mltr_addr
[j
] = mleak_top_trace
[i
]->addr
[j
];
7033 static struct mbtypes
{
7035 const char *mt_name
;
7037 { MT_DATA
, "data" },
7038 { MT_OOBDATA
, "oob data" },
7039 { MT_CONTROL
, "ancillary data" },
7040 { MT_HEADER
, "packet headers" },
7041 { MT_SOCKET
, "socket structures" },
7042 { MT_PCB
, "protocol control blocks" },
7043 { MT_RTABLE
, "routing table entries" },
7044 { MT_HTABLE
, "IMP host table entries" },
7045 { MT_ATABLE
, "address resolution tables" },
7046 { MT_FTABLE
, "fragment reassembly queue headers" },
7047 { MT_SONAME
, "socket names and addresses" },
7048 { MT_SOOPTS
, "socket options" },
7049 { MT_RIGHTS
, "access rights" },
7050 { MT_IFADDR
, "interface addresses" },
7051 { MT_TAG
, "packet tags" },
7055 #define MBUF_DUMP_BUF_CHK() { \
7065 unsigned long totmem
= 0, totfree
= 0, totmbufs
, totused
, totpct
;
7066 u_int32_t m_mbufs
= 0, m_clfree
= 0, m_bigclfree
= 0;
7067 u_int32_t m_mbufclfree
= 0, m_mbufbigclfree
= 0;
7068 u_int32_t m_16kclusters
= 0, m_16kclfree
= 0, m_mbuf16kclfree
= 0;
7069 int nmbtypes
= sizeof (mbstat
.m_mtypes
) / sizeof (short);
7072 mb_class_stat_t
*sp
;
7073 mleak_trace_stat_t
*mltr
;
7074 char *c
= mbuf_dump_buf
;
7075 int i
, k
, clen
= MBUF_DUMP_BUF_SIZE
;
7077 mbuf_dump_buf
[0] = '\0';
7079 /* synchronize all statistics in the mbuf table */
7081 mbuf_mtypes_sync(TRUE
);
7083 sp
= &mb_stat
->mbs_class
[0];
7084 for (i
= 0; i
< mb_stat
->mbs_cnt
; i
++, sp
++) {
7087 if (m_class(i
) == MC_MBUF
) {
7088 m_mbufs
= sp
->mbcl_active
;
7089 } else if (m_class(i
) == MC_CL
) {
7090 m_clfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7091 } else if (m_class(i
) == MC_BIGCL
) {
7092 m_bigclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7093 } else if (njcl
> 0 && m_class(i
) == MC_16KCL
) {
7094 m_16kclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7095 m_16kclusters
= sp
->mbcl_total
;
7096 } else if (m_class(i
) == MC_MBUF_CL
) {
7097 m_mbufclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7098 } else if (m_class(i
) == MC_MBUF_BIGCL
) {
7099 m_mbufbigclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7100 } else if (njcl
> 0 && m_class(i
) == MC_MBUF_16KCL
) {
7101 m_mbuf16kclfree
= sp
->mbcl_total
- sp
->mbcl_active
;
7104 mem
= sp
->mbcl_ctotal
* sp
->mbcl_size
;
7106 totfree
+= (sp
->mbcl_mc_cached
+ sp
->mbcl_infree
) *
7111 /* adjust free counts to include composite caches */
7112 m_clfree
+= m_mbufclfree
;
7113 m_bigclfree
+= m_mbufbigclfree
;
7114 m_16kclfree
+= m_mbuf16kclfree
;
7117 for (mp
= mbtypes
; mp
->mt_name
!= NULL
; mp
++)
7118 totmbufs
+= mbstat
.m_mtypes
[mp
->mt_type
];
7119 if (totmbufs
> m_mbufs
)
7121 k
= snprintf(c
, clen
, "%lu/%u mbufs in use:\n", totmbufs
, m_mbufs
);
7122 MBUF_DUMP_BUF_CHK();
7124 bzero(&seen
, sizeof (seen
));
7125 for (mp
= mbtypes
; mp
->mt_name
!= NULL
; mp
++) {
7126 if (mbstat
.m_mtypes
[mp
->mt_type
] != 0) {
7127 seen
[mp
->mt_type
] = 1;
7128 k
= snprintf(c
, clen
, "\t%u mbufs allocated to %s\n",
7129 mbstat
.m_mtypes
[mp
->mt_type
], mp
->mt_name
);
7130 MBUF_DUMP_BUF_CHK();
7134 for (i
= 0; i
< nmbtypes
; i
++)
7135 if (!seen
[i
] && mbstat
.m_mtypes
[i
] != 0) {
7136 k
= snprintf(c
, clen
, "\t%u mbufs allocated to "
7137 "<mbuf type %d>\n", mbstat
.m_mtypes
[i
], i
);
7138 MBUF_DUMP_BUF_CHK();
7140 if ((m_mbufs
- totmbufs
) > 0) {
7141 k
= snprintf(c
, clen
, "\t%lu mbufs allocated to caches\n",
7142 m_mbufs
- totmbufs
);
7143 MBUF_DUMP_BUF_CHK();
7145 k
= snprintf(c
, clen
, "%u/%u mbuf 2KB clusters in use\n"
7146 "%u/%u mbuf 4KB clusters in use\n",
7147 (unsigned int)(mbstat
.m_clusters
- m_clfree
),
7148 (unsigned int)mbstat
.m_clusters
,
7149 (unsigned int)(mbstat
.m_bigclusters
- m_bigclfree
),
7150 (unsigned int)mbstat
.m_bigclusters
);
7151 MBUF_DUMP_BUF_CHK();
7154 k
= snprintf(c
, clen
, "%u/%u mbuf %uKB clusters in use\n",
7155 m_16kclusters
- m_16kclfree
, m_16kclusters
,
7157 MBUF_DUMP_BUF_CHK();
7159 totused
= totmem
- totfree
;
7162 } else if (totused
< (ULONG_MAX
/ 100)) {
7163 totpct
= (totused
* 100) / totmem
;
7165 u_long totmem1
= totmem
/ 100;
7166 u_long totused1
= totused
/ 100;
7167 totpct
= (totused1
* 100) / totmem1
;
7169 k
= snprintf(c
, clen
, "%lu KB allocated to network (approx. %lu%% "
7170 "in use)\n", totmem
/ 1024, totpct
);
7171 MBUF_DUMP_BUF_CHK();
7173 /* mbuf leak detection statistics */
7174 mleak_update_stats();
7176 k
= snprintf(c
, clen
, "\nmbuf leak detection table:\n");
7177 MBUF_DUMP_BUF_CHK();
7178 k
= snprintf(c
, clen
, "\ttotal captured: %u (one per %u)\n",
7179 mleak_table
.mleak_capture
/ mleak_table
.mleak_sample_factor
,
7180 mleak_table
.mleak_sample_factor
);
7181 MBUF_DUMP_BUF_CHK();
7182 k
= snprintf(c
, clen
, "\ttotal allocs outstanding: %llu\n",
7183 mleak_table
.outstanding_allocs
);
7184 MBUF_DUMP_BUF_CHK();
7185 k
= snprintf(c
, clen
, "\tnew hash recorded: %llu allocs, %llu traces\n",
7186 mleak_table
.alloc_recorded
, mleak_table
.trace_recorded
);
7187 MBUF_DUMP_BUF_CHK();
7188 k
= snprintf(c
, clen
, "\thash collisions: %llu allocs, %llu traces\n",
7189 mleak_table
.alloc_collisions
, mleak_table
.trace_collisions
);
7190 MBUF_DUMP_BUF_CHK();
7191 k
= snprintf(c
, clen
, "\toverwrites: %llu allocs, %llu traces\n",
7192 mleak_table
.alloc_overwrites
, mleak_table
.trace_overwrites
);
7193 MBUF_DUMP_BUF_CHK();
7194 k
= snprintf(c
, clen
, "\tlock conflicts: %llu\n\n",
7195 mleak_table
.total_conflicts
);
7196 MBUF_DUMP_BUF_CHK();
7198 k
= snprintf(c
, clen
, "top %d outstanding traces:\n",
7199 mleak_stat
->ml_cnt
);
7200 MBUF_DUMP_BUF_CHK();
7201 for (i
= 0; i
< mleak_stat
->ml_cnt
; i
++) {
7202 mltr
= &mleak_stat
->ml_trace
[i
];
7203 k
= snprintf(c
, clen
, "[%d] %llu outstanding alloc(s), "
7204 "%llu hit(s), %llu collision(s)\n", (i
+ 1),
7205 mltr
->mltr_allocs
, mltr
->mltr_hitcount
,
7206 mltr
->mltr_collisions
);
7207 MBUF_DUMP_BUF_CHK();
7210 if (mleak_stat
->ml_isaddr64
)
7211 k
= snprintf(c
, clen
, MB_LEAK_HDR_64
);
7213 k
= snprintf(c
, clen
, MB_LEAK_HDR_32
);
7214 MBUF_DUMP_BUF_CHK();
7216 for (i
= 0; i
< MLEAK_STACK_DEPTH
; i
++) {
7218 k
= snprintf(c
, clen
, "%2d: ", (i
+ 1));
7219 MBUF_DUMP_BUF_CHK();
7220 for (j
= 0; j
< mleak_stat
->ml_cnt
; j
++) {
7221 mltr
= &mleak_stat
->ml_trace
[j
];
7222 if (i
< mltr
->mltr_depth
) {
7223 if (mleak_stat
->ml_isaddr64
) {
7224 k
= snprintf(c
, clen
, "0x%0llx ",
7225 (uint64_t)VM_KERNEL_UNSLIDE(
7226 mltr
->mltr_addr
[i
]));
7228 k
= snprintf(c
, clen
,
7230 (uint32_t)VM_KERNEL_UNSLIDE(
7231 mltr
->mltr_addr
[i
]));
7234 if (mleak_stat
->ml_isaddr64
)
7235 k
= snprintf(c
, clen
,
7236 MB_LEAK_SPACING_64
);
7238 k
= snprintf(c
, clen
,
7239 MB_LEAK_SPACING_32
);
7241 MBUF_DUMP_BUF_CHK();
7243 k
= snprintf(c
, clen
, "\n");
7244 MBUF_DUMP_BUF_CHK();
7247 return (mbuf_dump_buf
);
7250 #undef MBUF_DUMP_BUF_CHK
7253 * Convert between a regular and a packet header mbuf. Caller is responsible
7254 * for setting or clearing M_PKTHDR; this routine does the rest of the work.
7257 m_reinit(struct mbuf
*m
, int hdr
)
7262 VERIFY(!(m
->m_flags
& M_PKTHDR
));
7263 if (!(m
->m_flags
& M_EXT
) &&
7264 (m
->m_data
!= m
->m_dat
|| m
->m_len
> 0)) {
7266 * If there's no external cluster attached and the
7267 * mbuf appears to contain user data, we cannot
7268 * safely convert this to a packet header mbuf,
7269 * as the packet header structure might overlap
7272 printf("%s: cannot set M_PKTHDR on altered mbuf %llx, "
7273 "m_data %llx (expected %llx), "
7274 "m_len %d (expected 0)\n",
7276 (uint64_t)VM_KERNEL_ADDRPERM(m
),
7277 (uint64_t)VM_KERNEL_ADDRPERM(m
->m_data
),
7278 (uint64_t)VM_KERNEL_ADDRPERM(m
->m_dat
), m
->m_len
);
7281 VERIFY((m
->m_flags
& M_EXT
) || m
->m_data
== m
->m_dat
);
7282 m
->m_flags
|= M_PKTHDR
;
7283 MBUF_INIT_PKTHDR(m
);
7286 /* Check for scratch area overflow */
7287 m_redzone_verify(m
);
7288 /* Free the aux data and tags if there is any */
7289 m_tag_delete_chain(m
, NULL
);
7290 m
->m_flags
&= ~M_PKTHDR
;
7297 m_scratch_init(struct mbuf
*m
)
7299 struct pkthdr
*pkt
= &m
->m_pkthdr
;
7301 VERIFY(m
->m_flags
& M_PKTHDR
);
7303 /* See comments in <rdar://problem/14040693> */
7304 if (pkt
->pkt_flags
& PKTF_PRIV_GUARDED
) {
7305 panic_plain("Invalid attempt to modify guarded module-private "
7306 "area: mbuf %p, pkt_flags 0x%x\n", m
, pkt
->pkt_flags
);
7310 bzero(&pkt
->pkt_mpriv
, sizeof (pkt
->pkt_mpriv
));
7314 * This routine is reserved for mbuf_get_driver_scratch(); clients inside
7315 * xnu that intend on utilizing the module-private area should directly
7316 * refer to the pkt_mpriv structure in the pkthdr. They are also expected
7317 * to set and clear PKTF_PRIV_GUARDED, while owning the packet and prior
7318 * to handing it off to another module, respectively.
7321 m_scratch_get(struct mbuf
*m
, u_int8_t
**p
)
7323 struct pkthdr
*pkt
= &m
->m_pkthdr
;
7325 VERIFY(m
->m_flags
& M_PKTHDR
);
7327 /* See comments in <rdar://problem/14040693> */
7328 if (pkt
->pkt_flags
& PKTF_PRIV_GUARDED
) {
7329 panic_plain("Invalid attempt to access guarded module-private "
7330 "area: mbuf %p, pkt_flags 0x%x\n", m
, pkt
->pkt_flags
);
7335 mcache_audit_t
*mca
;
7337 lck_mtx_lock(mbuf_mlock
);
7338 mca
= mcl_audit_buf2mca(MC_MBUF
, (mcache_obj_t
*)m
);
7339 if (mca
->mca_uflags
& MB_SCVALID
)
7340 mcl_audit_scratch(mca
);
7341 lck_mtx_unlock(mbuf_mlock
);
7344 *p
= (u_int8_t
*)&pkt
->pkt_mpriv
;
7345 return (sizeof (pkt
->pkt_mpriv
));
7349 m_redzone_init(struct mbuf
*m
)
7351 VERIFY(m
->m_flags
& M_PKTHDR
);
7353 * Each mbuf has a unique red zone pattern, which is a XOR
7354 * of the red zone cookie and the address of the mbuf.
7356 m
->m_pkthdr
.redzone
= ((u_int32_t
)(uintptr_t)m
) ^ mb_redzone_cookie
;
7360 m_redzone_verify(struct mbuf
*m
)
7362 u_int32_t mb_redzone
;
7364 VERIFY(m
->m_flags
& M_PKTHDR
);
7366 mb_redzone
= ((u_int32_t
)(uintptr_t)m
) ^ mb_redzone_cookie
;
7367 if (m
->m_pkthdr
.redzone
!= mb_redzone
) {
7368 panic("mbuf %p redzone violation with value 0x%x "
7369 "(instead of 0x%x, using cookie 0x%x)\n",
7370 m
, m
->m_pkthdr
.redzone
, mb_redzone
, mb_redzone_cookie
);
7376 * Send a report of mbuf usage if the usage is at least 6% of max limit
7377 * or if there has been at least 3% increase since the last report.
7379 * The values 6% and 3% are chosen so that we can do simple arithmetic
7380 * with shift operations.
7383 mbuf_report_usage(mbuf_class_t cl
)
7385 /* if a report is already in progress, nothing to do */
7386 if (mb_peak_newreport
)
7389 if (m_total(cl
) > m_peak(cl
) &&
7390 m_total(cl
) >= (m_maxlimit(cl
) >> 4) &&
7391 (m_total(cl
) - m_peak(cl
)) >= (m_peak(cl
) >> 5))
7396 __private_extern__
void
7397 mbuf_report_peak_usage(void)
7401 struct nstat_sysinfo_data ns_data
;
7402 uint32_t memreleased
= 0;
7404 uptime
= net_uptime();
7405 lck_mtx_lock(mbuf_mlock
);
7407 /* Generate an initial report after 1 week of uptime */
7408 if (!mb_peak_firstreport
&&
7409 uptime
> MBUF_PEAK_FIRST_REPORT_THRESHOLD
) {
7410 mb_peak_newreport
= TRUE
;
7411 mb_peak_firstreport
= TRUE
;
7414 if (!mb_peak_newreport
) {
7415 lck_mtx_unlock(mbuf_mlock
);
7420 * Since a report is being generated before 1 week,
7421 * we do not need to force another one later
7423 if (uptime
< MBUF_PEAK_FIRST_REPORT_THRESHOLD
)
7424 mb_peak_firstreport
= TRUE
;
7426 for (i
= 0; i
< NELEM(mbuf_table
); i
++) {
7427 m_peak(m_class(i
)) = m_total(m_class(i
));
7428 memreleased
+= m_release_cnt(i
);
7429 m_release_cnt(i
) = 0;
7431 mb_peak_newreport
= FALSE
;
7432 lck_mtx_unlock(mbuf_mlock
);
7434 bzero(&ns_data
, sizeof(ns_data
));
7435 ns_data
.flags
= NSTAT_SYSINFO_MBUF_STATS
;
7436 ns_data
.u
.mb_stats
.total_256b
= m_peak(MC_MBUF
);
7437 ns_data
.u
.mb_stats
.total_2kb
= m_peak(MC_CL
);
7438 ns_data
.u
.mb_stats
.total_4kb
= m_peak(MC_BIGCL
);
7439 ns_data
.u
.mb_stats
.total_16kb
= m_peak(MC_16KCL
);
7440 ns_data
.u
.mb_stats
.sbmb_total
= total_sbmb_cnt_peak
;
7441 ns_data
.u
.mb_stats
.sb_atmbuflimit
= sbmb_limreached
;
7442 ns_data
.u
.mb_stats
.draincnt
= mbstat
.m_drain
;
7443 ns_data
.u
.mb_stats
.memreleased
= memreleased
;
7445 nstat_sysinfo_send_data(&ns_data
);
7449 * Called by the VM when there's memory pressure.
7451 __private_extern__
void
7455 mcl_slab_t
*sp
, *sp_tmp
, *nsp
;
7456 unsigned int num
, k
, interval
, released
= 0;
7457 unsigned int total_mem
= 0, use_mem
= 0;
7458 boolean_t ret
, purge_caches
= FALSE
;
7462 static uint64_t last_drain
= 0;
7463 static unsigned char scratch
[32];
7464 static ppnum_t scratch_pa
= 0;
7466 if (mb_drain_maxint
== 0 || mb_waiters
)
7468 if (scratch_pa
== 0) {
7469 bzero(scratch
, sizeof(scratch
));
7470 scratch_pa
= pmap_find_phys(kernel_pmap
, (addr64_t
)scratch
);
7472 } else if (mclverify
) {
7474 * Panic if a driver wrote to our scratch memory.
7476 for (k
= 0; k
< sizeof(scratch
); k
++)
7478 panic("suspect DMA to freed address");
7481 * Don't free memory too often as that could cause excessive
7482 * waiting times for mbufs. Purge caches if we were asked to drain
7483 * in the last 5 minutes.
7485 lck_mtx_lock(mbuf_mlock
);
7486 if (last_drain
== 0) {
7487 last_drain
= net_uptime();
7488 lck_mtx_unlock(mbuf_mlock
);
7491 interval
= net_uptime() - last_drain
;
7492 if (interval
<= mb_drain_maxint
) {
7493 lck_mtx_unlock(mbuf_mlock
);
7496 if (interval
<= mb_drain_maxint
* 5)
7497 purge_caches
= TRUE
;
7498 last_drain
= net_uptime();
7500 * Don't free any memory if we're using 60% or more.
7502 for (mc
= 0; mc
< NELEM(mbuf_table
); mc
++) {
7503 total_mem
+= m_total(mc
) * m_maxsize(mc
);
7504 use_mem
+= m_active(mc
) * m_maxsize(mc
);
7506 per
= (float)use_mem
/ (float)total_mem
;
7508 lck_mtx_unlock(mbuf_mlock
);
7512 * Purge all the caches. This effectively disables
7513 * caching for a few seconds, but the mbuf worker thread will
7514 * re-enable them again.
7516 if (purge_caches
== TRUE
)
7517 for (mc
= 0; mc
< NELEM(mbuf_table
); mc
++) {
7518 if (m_total(mc
) < m_avgtotal(mc
))
7520 lck_mtx_unlock(mbuf_mlock
);
7521 ret
= mcache_purge_cache(m_cache(mc
), FALSE
);
7522 lck_mtx_lock(mbuf_mlock
);
7527 * Move the objects from the composite class freelist to
7528 * the rudimentary slabs list, but keep at least 10% of the average
7529 * total in the freelist.
7531 for (mc
= 0; mc
< NELEM(mbuf_table
); mc
++) {
7532 while (m_cobjlist(mc
) &&
7533 m_total(mc
) < m_avgtotal(mc
) &&
7534 m_infree(mc
) > 0.1 * m_avgtotal(mc
) + m_minlimit(mc
)) {
7535 obj
= m_cobjlist(mc
);
7536 m_cobjlist(mc
) = obj
->obj_next
;
7537 obj
->obj_next
= NULL
;
7538 num
= cslab_free(mc
, obj
, 1);
7542 /* cslab_free() handles m_total */
7546 * Free the buffers present in the slab list up to 10% of the total
7547 * average per class.
7549 * We walk the list backwards in an attempt to reduce fragmentation.
7551 for (mc
= NELEM(mbuf_table
) - 1; (int)mc
>= 0; mc
--) {
7552 TAILQ_FOREACH_SAFE(sp
, &m_slablist(mc
), sl_link
, sp_tmp
) {
7554 * Process only unused slabs occupying memory.
7556 if (sp
->sl_refcnt
!= 0 || sp
->sl_len
== 0 ||
7557 sp
->sl_base
== NULL
)
7559 if (m_total(mc
) < m_avgtotal(mc
) ||
7560 m_infree(mc
) < 0.1 * m_avgtotal(mc
) + m_minlimit(mc
))
7562 slab_remove(sp
, mc
);
7565 m_infree(mc
) -= NMBPG
;
7566 m_total(mc
) -= NMBPG
;
7567 if (mclaudit
!= NULL
)
7568 mcl_audit_free(sp
->sl_base
, NMBPG
);
7571 m_infree(mc
) -= NCLPG
;
7572 m_total(mc
) -= NCLPG
;
7573 if (mclaudit
!= NULL
)
7574 mcl_audit_free(sp
->sl_base
, NMBPG
);
7578 m_infree(mc
) -= NBCLPG
;
7579 m_total(mc
) -= NBCLPG
;
7580 if (mclaudit
!= NULL
)
7581 mcl_audit_free(sp
->sl_base
, NMBPG
);
7587 for (nsp
= sp
, k
= 1; k
< NSLABSP16KB
; k
++) {
7589 VERIFY(nsp
->sl_refcnt
== 0 &&
7590 nsp
->sl_base
!= NULL
&&
7592 slab_init(nsp
, 0, 0, NULL
, NULL
, 0, 0,
7596 if (mclaudit
!= NULL
)
7597 mcl_audit_free(sp
->sl_base
, 1);
7601 * The composite classes have their own
7602 * freelist (m_cobjlist), so we only
7603 * process rudimentary classes here.
7607 m_release_cnt(mc
) += m_size(mc
);
7608 released
+= m_size(mc
);
7609 VERIFY(sp
->sl_base
!= NULL
&&
7610 sp
->sl_len
>= PAGE_SIZE
);
7611 offset
= MTOPG(sp
->sl_base
);
7613 * Make sure the IOMapper points to a valid, but
7614 * bogus, address. This should prevent further DMA
7615 * accesses to freed memory.
7617 IOMapperInsertPage(mcl_paddr_base
, offset
, scratch_pa
);
7618 mcl_paddr
[offset
] = 0;
7619 kmem_free(mb_map
, (vm_offset_t
)sp
->sl_base
,
7621 slab_init(sp
, 0, 0, NULL
, NULL
, 0, 0, 0);
7626 mbstat
.m_bigclusters
= m_total(MC_BIGCL
);
7627 mbstat
.m_clusters
= m_total(MC_CL
);
7628 mbstat
.m_mbufs
= m_total(MC_MBUF
);
7630 mbuf_mtypes_sync(TRUE
);
7631 lck_mtx_unlock(mbuf_mlock
);
7635 m_drain_force_sysctl SYSCTL_HANDLER_ARGS
7637 #pragma unused(arg1, arg2)
7640 err
= sysctl_handle_int(oidp
, &val
, 0, req
);
7641 if (err
!= 0 || req
->newptr
== USER_ADDR_NULL
)
7649 SYSCTL_DECL(_kern_ipc
);
7650 SYSCTL_PROC(_kern_ipc
, KIPC_MBSTAT
, mbstat
,
7651 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
7652 0, 0, mbstat_sysctl
, "S,mbstat", "");
7653 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mb_stat
,
7654 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
7655 0, 0, mb_stat_sysctl
, "S,mb_stat", "");
7656 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mleak_top_trace
,
7657 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
7658 0, 0, mleak_top_trace_sysctl
, "S,mb_top_trace", "");
7659 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mleak_table
,
7660 CTLTYPE_STRUCT
| CTLFLAG_RD
| CTLFLAG_LOCKED
,
7661 0, 0, mleak_table_sysctl
, "S,mleak_table", "");
7662 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mleak_sample_factor
,
7663 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mleak_table
.mleak_sample_factor
, 0, "");
7664 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_normalized
,
7665 CTLFLAG_RD
| CTLFLAG_LOCKED
, &mb_normalized
, 0, "");
7666 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_watchdog
,
7667 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mb_watchdog
, 0, "");
7668 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, mb_drain_force
,
7669 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, NULL
, 0,
7670 m_drain_force_sysctl
, "I",
7671 "Forces the mbuf garbage collection to run");
7672 SYSCTL_INT(_kern_ipc
, OID_AUTO
, mb_drain_maxint
,
7673 CTLFLAG_RW
| CTLFLAG_LOCKED
, &mb_drain_maxint
, 0,
7674 "Minimum time interval between garbage collection");