2 * Copyright (c) 2004, 2005,
3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_param.h"
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/systm.h>
37 #include <sys/domain.h>
38 #include <sys/eventhandler.h>
39 #include <sys/kernel.h>
40 #include <sys/protosw.h>
42 #include <sys/sysctl.h>
44 #include <security/mac/mac_framework.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_page.h>
51 #include <vm/uma_int.h>
52 #include <vm/uma_dbg.h>
55 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
58 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
59 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
60 * administrator so desires.
62 * Mbufs are allocated from a UMA Master Zone called the Mbuf
65 * Additionally, FreeBSD provides a Packet Zone, which it
66 * configures as a Secondary Zone to the Mbuf Master Zone,
67 * thus sharing backend Slab kegs with the Mbuf Master Zone.
69 * Thus common-case allocations and locking are simplified:
73 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
75 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
76 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
82 * \____________(VM)_________________/
85 * Whenever an object is allocated with uma_zalloc() out of
86 * one of the Zones its _ctor_ function is executed. The same
87 * for any deallocation through uma_zfree() the _dtor_ function
90 * Caches are per-CPU and are filled from the Master Zone.
92 * Whenever an object is allocated from the underlying global
93 * memory pool it gets pre-initialized with the _zinit_ functions.
94 * When the Keg's are overfull objects get decomissioned with
95 * _zfini_ functions and free'd back to the global memory pool.
99 int nmbclusters; /* limits number of mbuf clusters */
100 int nmbjumbop; /* limits number of page size jumbo clusters */
101 int nmbjumbo9; /* limits number of 9k jumbo clusters */
102 int nmbjumbo16; /* limits number of 16k jumbo clusters */
103 struct mbstat mbstat;
106 * tunable_mbinit() has to be run before init_maxsockets() thus
107 * the SYSINIT order below is SI_ORDER_MIDDLE while init_maxsockets()
108 * runs at SI_ORDER_ANY.
111 tunable_mbinit(void *dummy)
113 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
115 /* This has to be done before VM init. */
116 if (nmbclusters == 0)
117 nmbclusters = 1024 + maxusers * 64;
118 nmbjumbop = nmbclusters / 2;
119 nmbjumbo9 = nmbjumbop / 2;
120 nmbjumbo16 = nmbjumbo9 / 2;
122 SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
125 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
127 int error, newnmbclusters;
129 newnmbclusters = nmbclusters;
130 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
131 if (error == 0 && req->newptr) {
132 if (newnmbclusters > nmbclusters) {
133 nmbclusters = newnmbclusters;
134 uma_zone_set_max(zone_clust, nmbclusters);
135 EVENTHANDLER_INVOKE(nmbclusters_change);
141 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
142 &nmbclusters, 0, sysctl_nmbclusters, "IU",
143 "Maximum number of mbuf clusters allowed");
146 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
148 int error, newnmbjumbop;
150 newnmbjumbop = nmbjumbop;
151 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
152 if (error == 0 && req->newptr) {
153 if (newnmbjumbop> nmbjumbop) {
154 nmbjumbop = newnmbjumbop;
155 uma_zone_set_max(zone_jumbop, nmbjumbop);
161 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
162 &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
163 "Maximum number of mbuf page size jumbo clusters allowed");
167 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
169 int error, newnmbjumbo9;
171 newnmbjumbo9 = nmbjumbo9;
172 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
173 if (error == 0 && req->newptr) {
174 if (newnmbjumbo9> nmbjumbo9) {
175 nmbjumbo9 = newnmbjumbo9;
176 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
182 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
183 &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
184 "Maximum number of mbuf 9k jumbo clusters allowed");
187 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
189 int error, newnmbjumbo16;
191 newnmbjumbo16 = nmbjumbo16;
192 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
193 if (error == 0 && req->newptr) {
194 if (newnmbjumbo16> nmbjumbo16) {
195 nmbjumbo16 = newnmbjumbo16;
196 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
202 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
203 &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
204 "Maximum number of mbuf 16k jumbo clusters allowed");
208 SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
209 "Mbuf general information and statistics");
212 * Zones from which we allocate.
214 uma_zone_t zone_mbuf;
215 uma_zone_t zone_clust;
216 uma_zone_t zone_pack;
217 uma_zone_t zone_jumbop;
218 uma_zone_t zone_jumbo9;
219 uma_zone_t zone_jumbo16;
220 uma_zone_t zone_ext_refcnt;
225 static int mb_ctor_mbuf(void *, int, void *, int);
226 static int mb_ctor_clust(void *, int, void *, int);
227 static int mb_ctor_pack(void *, int, void *, int);
228 static void mb_dtor_mbuf(void *, int, void *);
229 static void mb_dtor_clust(void *, int, void *);
230 static void mb_dtor_pack(void *, int, void *);
231 static int mb_zinit_pack(void *, int, int);
232 static void mb_zfini_pack(void *, int);
234 static void mb_reclaim(void *);
235 static void mbuf_init(void *);
236 static void *mbuf_jumbo_alloc(uma_zone_t, int, uint8_t *, int);
238 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
239 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
242 * Initialize FreeBSD Network buffer allocation.
244 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
246 mbuf_init(void *dummy)
250 * Configure UMA zones for Mbufs, Clusters, and Packets.
252 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
253 mb_ctor_mbuf, mb_dtor_mbuf,
255 trash_init, trash_fini,
259 MSIZE - 1, UMA_ZONE_MAXBUCKET);
261 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
262 mb_ctor_clust, mb_dtor_clust,
264 trash_init, trash_fini,
268 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
270 uma_zone_set_max(zone_clust, nmbclusters);
272 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
273 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
275 /* Make jumbo frame zone too. Page size, 9k and 16k. */
276 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
277 mb_ctor_clust, mb_dtor_clust,
279 trash_init, trash_fini,
283 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
285 uma_zone_set_max(zone_jumbop, nmbjumbop);
287 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
288 mb_ctor_clust, mb_dtor_clust,
290 trash_init, trash_fini,
294 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
296 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
297 uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
299 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
300 mb_ctor_clust, mb_dtor_clust,
302 trash_init, trash_fini,
306 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
308 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
309 uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
311 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
314 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
316 /* uma_prealloc() goes here... */
319 * Hook event handler for low-memory situation, used to
320 * drain protocols and push data back to the caches (UMA
321 * later pushes it back to VM).
323 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
324 EVENTHANDLER_PRI_FIRST);
327 * [Re]set counters and local statistics knobs.
328 * XXX Some of these should go and be replaced, but UMA stat
329 * gathering needs to be revised.
332 mbstat.m_mclusts = 0;
334 mbstat.m_msize = MSIZE;
335 mbstat.m_mclbytes = MCLBYTES;
336 mbstat.m_minclsize = MINCLSIZE;
337 mbstat.m_mlen = MLEN;
338 mbstat.m_mhlen = MHLEN;
339 mbstat.m_numtypes = MT_NTYPES;
341 mbstat.m_mcfail = mbstat.m_mpfail = 0;
343 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
347 * UMA backend page allocator for the jumbo frame zones.
349 * Allocates kernel virtual memory that is backed by contiguous physical
353 mbuf_jumbo_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
356 /* Inform UMA that this allocator uses kernel_map/object. */
357 *flags = UMA_SLAB_KERNEL;
358 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait,
359 (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
363 * Constructor for Mbuf master zone.
365 * The 'arg' pointer points to a mb_args structure which
366 * contains call-specific information required to support the
367 * mbuf allocation API. See mbuf.h.
370 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
373 struct mb_args *args;
381 trash_ctor(mem, size, arg, how);
383 m = (struct mbuf *)mem;
384 args = (struct mb_args *)arg;
389 * The mbuf is initialized later. The caller has the
390 * responsibility to set up any MAC labels too.
392 if (type == MT_NOINIT)
400 if (flags & M_PKTHDR) {
401 m->m_data = m->m_pktdat;
402 m->m_pkthdr.rcvif = NULL;
403 m->m_pkthdr.header = NULL;
405 m->m_pkthdr.csum_flags = 0;
406 m->m_pkthdr.csum_data = 0;
407 m->m_pkthdr.tso_segsz = 0;
408 m->m_pkthdr.ether_vtag = 0;
409 m->m_pkthdr.flowid = 0;
410 SLIST_INIT(&m->m_pkthdr.tags);
412 /* If the label init fails, fail the alloc */
413 error = mac_mbuf_init(m, how);
418 m->m_data = m->m_dat;
423 * The Mbuf master zone destructor.
426 mb_dtor_mbuf(void *mem, int size, void *arg)
431 m = (struct mbuf *)mem;
432 flags = (unsigned long)arg;
434 if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0)
435 m_tag_delete_chain(m, NULL);
436 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
437 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
439 trash_dtor(mem, size, arg);
444 * The Mbuf Packet zone destructor.
447 mb_dtor_pack(void *mem, int size, void *arg)
451 m = (struct mbuf *)mem;
452 if ((m->m_flags & M_PKTHDR) != 0)
453 m_tag_delete_chain(m, NULL);
455 /* Make sure we've got a clean cluster back. */
456 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
457 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
458 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
459 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
460 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
461 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
462 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
463 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
465 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
468 * If there are processes blocked on zone_clust, waiting for pages
469 * to be freed up, * cause them to be woken up by draining the
470 * packet zone. We are exposed to a race here * (in the check for
471 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
472 * is deliberate. We don't want to acquire the zone lock for every
475 if (uma_zone_exhausted_nolock(zone_clust))
476 zone_drain(zone_pack);
480 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
482 * Here the 'arg' pointer points to the Mbuf which we
483 * are configuring cluster storage for. If 'arg' is
484 * empty we allocate just the cluster without setting
485 * the mbuf to it. See mbuf.h.
488 mb_ctor_clust(void *mem, int size, void *arg, int how)
496 trash_ctor(mem, size, arg, how);
503 #if MJUMPAGESIZE != MCLBYTES
518 panic("unknown cluster size");
522 m = (struct mbuf *)arg;
523 refcnt = uma_find_refcnt(zone, mem);
526 m->m_ext.ext_buf = (caddr_t)mem;
527 m->m_data = m->m_ext.ext_buf;
529 m->m_ext.ext_free = NULL;
530 m->m_ext.ext_arg1 = NULL;
531 m->m_ext.ext_arg2 = NULL;
532 m->m_ext.ext_size = size;
533 m->m_ext.ext_type = type;
534 m->m_ext.ref_cnt = refcnt;
541 * The Mbuf Cluster zone destructor.
544 mb_dtor_clust(void *mem, int size, void *arg)
549 zone = m_getzone(size);
550 KASSERT(*(uma_find_refcnt(zone, mem)) <= 1,
551 ("%s: refcnt incorrect %u", __func__,
552 *(uma_find_refcnt(zone, mem))) );
554 trash_dtor(mem, size, arg);
559 * The Packet secondary zone's init routine, executed on the
560 * object's transition from mbuf keg slab to zone cache.
563 mb_zinit_pack(void *mem, int size, int how)
567 m = (struct mbuf *)mem; /* m is virgin. */
568 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
569 m->m_ext.ext_buf == NULL)
571 m->m_ext.ext_type = EXT_PACKET; /* Override. */
573 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
579 * The Packet secondary zone's fini routine, executed on the
580 * object's transition from zone cache to keg slab.
583 mb_zfini_pack(void *mem, int size)
587 m = (struct mbuf *)mem;
589 trash_fini(m->m_ext.ext_buf, MCLBYTES);
591 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
593 trash_dtor(mem, size, NULL);
598 * The "packet" keg constructor.
601 mb_ctor_pack(void *mem, int size, void *arg, int how)
604 struct mb_args *args;
611 m = (struct mbuf *)mem;
612 args = (struct mb_args *)arg;
617 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
621 m->m_data = m->m_ext.ext_buf;
623 m->m_flags = (flags | M_EXT);
626 if (flags & M_PKTHDR) {
627 m->m_pkthdr.rcvif = NULL;
629 m->m_pkthdr.header = NULL;
630 m->m_pkthdr.csum_flags = 0;
631 m->m_pkthdr.csum_data = 0;
632 m->m_pkthdr.tso_segsz = 0;
633 m->m_pkthdr.ether_vtag = 0;
634 m->m_pkthdr.flowid = 0;
635 SLIST_INIT(&m->m_pkthdr.tags);
637 /* If the label init fails, fail the alloc */
638 error = mac_mbuf_init(m, how);
643 /* m_ext is already initialized. */
649 m_pkthdr_init(struct mbuf *m, int how)
654 m->m_data = m->m_pktdat;
655 SLIST_INIT(&m->m_pkthdr.tags);
656 m->m_pkthdr.rcvif = NULL;
657 m->m_pkthdr.header = NULL;
659 m->m_pkthdr.flowid = 0;
660 m->m_pkthdr.csum_flags = 0;
661 m->m_pkthdr.csum_data = 0;
662 m->m_pkthdr.tso_segsz = 0;
663 m->m_pkthdr.ether_vtag = 0;
665 /* If the label init fails, fail the alloc */
666 error = mac_mbuf_init(m, how);
675 * This is the protocol drain routine.
677 * No locks should be held when this is called. The drain routines have to
678 * presently acquire some locks which raises the possibility of lock order
682 mb_reclaim(void *junk)
687 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
690 for (dp = domains; dp != NULL; dp = dp->dom_next)
691 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
692 if (pr->pr_drain != NULL)