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_page.h>
49 #include <vm/uma_int.h>
50 #include <vm/uma_dbg.h>
53 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
56 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
57 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
58 * administrator so desires.
60 * Mbufs are allocated from a UMA Master Zone called the Mbuf
63 * Additionally, FreeBSD provides a Packet Zone, which it
64 * configures as a Secondary Zone to the Mbuf Master Zone,
65 * thus sharing backend Slab kegs with the Mbuf Master Zone.
67 * Thus common-case allocations and locking are simplified:
71 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
73 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
74 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
80 * \____________(VM)_________________/
83 * Whenever an object is allocated with uma_zalloc() out of
84 * one of the Zones its _ctor_ function is executed. The same
85 * for any deallocation through uma_zfree() the _dtor_ function
88 * Caches are per-CPU and are filled from the Master Zone.
90 * Whenever an object is allocated from the underlying global
91 * memory pool it gets pre-initialized with the _zinit_ functions.
92 * When the Keg's are overfull objects get decomissioned with
93 * _zfini_ functions and free'd back to the global memory pool.
97 int nmbclusters; /* limits number of mbuf clusters */
98 int nmbjumbop; /* limits number of page size jumbo clusters */
99 int nmbjumbo9; /* limits number of 9k jumbo clusters */
100 int nmbjumbo16; /* limits number of 16k jumbo clusters */
101 struct mbstat mbstat;
104 * tunable_mbinit() has to be run before init_maxsockets() thus
105 * the SYSINIT order below is SI_ORDER_MIDDLE while init_maxsockets()
106 * runs at SI_ORDER_ANY.
109 tunable_mbinit(void *dummy)
111 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
113 /* This has to be done before VM init. */
114 if (nmbclusters == 0)
115 nmbclusters = 1024 + maxusers * 64;
116 nmbjumbop = nmbclusters / 2;
117 nmbjumbo9 = nmbjumbop / 2;
118 nmbjumbo16 = nmbjumbo9 / 2;
120 SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
123 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
125 int error, newnmbclusters;
127 newnmbclusters = nmbclusters;
128 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
129 if (error == 0 && req->newptr) {
130 if (newnmbclusters > nmbclusters) {
131 nmbclusters = newnmbclusters;
132 uma_zone_set_max(zone_clust, nmbclusters);
133 EVENTHANDLER_INVOKE(nmbclusters_change);
139 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
140 &nmbclusters, 0, sysctl_nmbclusters, "IU",
141 "Maximum number of mbuf clusters allowed");
144 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
146 int error, newnmbjumbop;
148 newnmbjumbop = nmbjumbop;
149 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
150 if (error == 0 && req->newptr) {
151 if (newnmbjumbop> nmbjumbop) {
152 nmbjumbop = newnmbjumbop;
153 uma_zone_set_max(zone_jumbop, nmbjumbop);
159 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
160 &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
161 "Maximum number of mbuf page size jumbo clusters allowed");
165 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
167 int error, newnmbjumbo9;
169 newnmbjumbo9 = nmbjumbo9;
170 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
171 if (error == 0 && req->newptr) {
172 if (newnmbjumbo9> nmbjumbo9) {
173 nmbjumbo9 = newnmbjumbo9;
174 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
180 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
181 &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
182 "Maximum number of mbuf 9k jumbo clusters allowed");
185 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
187 int error, newnmbjumbo16;
189 newnmbjumbo16 = nmbjumbo16;
190 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
191 if (error == 0 && req->newptr) {
192 if (newnmbjumbo16> nmbjumbo16) {
193 nmbjumbo16 = newnmbjumbo16;
194 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
200 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
201 &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
202 "Maximum number of mbuf 16k jumbo clusters allowed");
206 SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
207 "Mbuf general information and statistics");
210 * Zones from which we allocate.
212 uma_zone_t zone_mbuf;
213 uma_zone_t zone_clust;
214 uma_zone_t zone_pack;
215 uma_zone_t zone_jumbop;
216 uma_zone_t zone_jumbo9;
217 uma_zone_t zone_jumbo16;
218 uma_zone_t zone_ext_refcnt;
223 static int mb_ctor_mbuf(void *, int, void *, int);
224 static int mb_ctor_clust(void *, int, void *, int);
225 static int mb_ctor_pack(void *, int, void *, int);
226 static void mb_dtor_mbuf(void *, int, void *);
227 static void mb_dtor_clust(void *, int, void *);
228 static void mb_dtor_pack(void *, int, void *);
229 static int mb_zinit_pack(void *, int, int);
230 static void mb_zfini_pack(void *, int);
232 static void mb_reclaim(void *);
233 static void mbuf_init(void *);
234 static void *mbuf_jumbo_alloc(uma_zone_t, int, u_int8_t *, int);
235 static void mbuf_jumbo_free(void *, int, u_int8_t);
237 static MALLOC_DEFINE(M_JUMBOFRAME, "jumboframes", "mbuf jumbo frame buffers");
239 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
240 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
243 * Initialize FreeBSD Network buffer allocation.
245 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
247 mbuf_init(void *dummy)
251 * Configure UMA zones for Mbufs, Clusters, and Packets.
253 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
254 mb_ctor_mbuf, mb_dtor_mbuf,
256 trash_init, trash_fini,
260 MSIZE - 1, UMA_ZONE_MAXBUCKET);
262 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
263 mb_ctor_clust, mb_dtor_clust,
265 trash_init, trash_fini,
269 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
271 uma_zone_set_max(zone_clust, nmbclusters);
273 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
274 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
276 /* Make jumbo frame zone too. Page size, 9k and 16k. */
277 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
278 mb_ctor_clust, mb_dtor_clust,
280 trash_init, trash_fini,
284 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
286 uma_zone_set_max(zone_jumbop, nmbjumbop);
288 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
289 mb_ctor_clust, mb_dtor_clust,
291 trash_init, trash_fini,
295 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
297 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
298 uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
299 uma_zone_set_freef(zone_jumbo9, mbuf_jumbo_free);
301 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
302 mb_ctor_clust, mb_dtor_clust,
304 trash_init, trash_fini,
308 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
310 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
311 uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
312 uma_zone_set_freef(zone_jumbo16, mbuf_jumbo_free);
314 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
317 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
319 /* uma_prealloc() goes here... */
322 * Hook event handler for low-memory situation, used to
323 * drain protocols and push data back to the caches (UMA
324 * later pushes it back to VM).
326 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
327 EVENTHANDLER_PRI_FIRST);
330 * [Re]set counters and local statistics knobs.
331 * XXX Some of these should go and be replaced, but UMA stat
332 * gathering needs to be revised.
335 mbstat.m_mclusts = 0;
337 mbstat.m_msize = MSIZE;
338 mbstat.m_mclbytes = MCLBYTES;
339 mbstat.m_minclsize = MINCLSIZE;
340 mbstat.m_mlen = MLEN;
341 mbstat.m_mhlen = MHLEN;
342 mbstat.m_numtypes = MT_NTYPES;
344 mbstat.m_mcfail = mbstat.m_mpfail = 0;
346 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
350 * UMA backend page allocator for the jumbo frame zones.
352 * Allocates kernel virtual memory that is backed by contiguous physical
356 mbuf_jumbo_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
359 /* Inform UMA that this allocator uses kernel_map/object. */
360 *flags = UMA_SLAB_KERNEL;
361 return (contigmalloc(bytes, M_JUMBOFRAME, wait, (vm_paddr_t)0,
362 ~(vm_paddr_t)0, 1, 0));
366 * UMA backend page deallocator for the jumbo frame zones.
369 mbuf_jumbo_free(void *mem, int size, u_int8_t flags)
372 contigfree(mem, size, M_JUMBOFRAME);
376 * Constructor for Mbuf master zone.
378 * The 'arg' pointer points to a mb_args structure which
379 * contains call-specific information required to support the
380 * mbuf allocation API. See mbuf.h.
383 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
386 struct mb_args *args;
394 trash_ctor(mem, size, arg, how);
396 m = (struct mbuf *)mem;
397 args = (struct mb_args *)arg;
402 * The mbuf is initialized later. The caller has the
403 * responsibility to set up any MAC labels too.
405 if (type == MT_NOINIT)
413 if (flags & M_PKTHDR) {
414 m->m_data = m->m_pktdat;
415 m->m_pkthdr.rcvif = NULL;
416 m->m_pkthdr.header = NULL;
418 m->m_pkthdr.csum_flags = 0;
419 m->m_pkthdr.csum_data = 0;
420 m->m_pkthdr.tso_segsz = 0;
421 m->m_pkthdr.ether_vtag = 0;
422 m->m_pkthdr.flowid = 0;
423 SLIST_INIT(&m->m_pkthdr.tags);
425 /* If the label init fails, fail the alloc */
426 error = mac_mbuf_init(m, how);
431 m->m_data = m->m_dat;
436 * The Mbuf master zone destructor.
439 mb_dtor_mbuf(void *mem, int size, void *arg)
444 m = (struct mbuf *)mem;
445 flags = (unsigned long)arg;
447 if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0)
448 m_tag_delete_chain(m, NULL);
449 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
450 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
452 trash_dtor(mem, size, arg);
457 * The Mbuf Packet zone destructor.
460 mb_dtor_pack(void *mem, int size, void *arg)
464 m = (struct mbuf *)mem;
465 if ((m->m_flags & M_PKTHDR) != 0)
466 m_tag_delete_chain(m, NULL);
468 /* Make sure we've got a clean cluster back. */
469 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
470 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
471 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
472 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
473 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
474 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
475 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
476 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
478 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
481 * If there are processes blocked on zone_clust, waiting for pages
482 * to be freed up, * cause them to be woken up by draining the
483 * packet zone. We are exposed to a race here * (in the check for
484 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
485 * is deliberate. We don't want to acquire the zone lock for every
488 if (uma_zone_exhausted_nolock(zone_clust))
489 zone_drain(zone_pack);
493 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
495 * Here the 'arg' pointer points to the Mbuf which we
496 * are configuring cluster storage for. If 'arg' is
497 * empty we allocate just the cluster without setting
498 * the mbuf to it. See mbuf.h.
501 mb_ctor_clust(void *mem, int size, void *arg, int how)
509 trash_ctor(mem, size, arg, how);
516 #if MJUMPAGESIZE != MCLBYTES
531 panic("unknown cluster size");
535 m = (struct mbuf *)arg;
536 refcnt = uma_find_refcnt(zone, mem);
539 m->m_ext.ext_buf = (caddr_t)mem;
540 m->m_data = m->m_ext.ext_buf;
542 m->m_ext.ext_free = NULL;
543 m->m_ext.ext_arg1 = NULL;
544 m->m_ext.ext_arg2 = NULL;
545 m->m_ext.ext_size = size;
546 m->m_ext.ext_type = type;
547 m->m_ext.ref_cnt = refcnt;
554 * The Mbuf Cluster zone destructor.
557 mb_dtor_clust(void *mem, int size, void *arg)
562 zone = m_getzone(size);
563 KASSERT(*(uma_find_refcnt(zone, mem)) <= 1,
564 ("%s: refcnt incorrect %u", __func__,
565 *(uma_find_refcnt(zone, mem))) );
567 trash_dtor(mem, size, arg);
572 * The Packet secondary zone's init routine, executed on the
573 * object's transition from mbuf keg slab to zone cache.
576 mb_zinit_pack(void *mem, int size, int how)
580 m = (struct mbuf *)mem; /* m is virgin. */
581 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
582 m->m_ext.ext_buf == NULL)
584 m->m_ext.ext_type = EXT_PACKET; /* Override. */
586 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
592 * The Packet secondary zone's fini routine, executed on the
593 * object's transition from zone cache to keg slab.
596 mb_zfini_pack(void *mem, int size)
600 m = (struct mbuf *)mem;
602 trash_fini(m->m_ext.ext_buf, MCLBYTES);
604 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
606 trash_dtor(mem, size, NULL);
611 * The "packet" keg constructor.
614 mb_ctor_pack(void *mem, int size, void *arg, int how)
617 struct mb_args *args;
624 m = (struct mbuf *)mem;
625 args = (struct mb_args *)arg;
630 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
634 m->m_data = m->m_ext.ext_buf;
636 m->m_flags = (flags | M_EXT);
639 if (flags & M_PKTHDR) {
640 m->m_pkthdr.rcvif = NULL;
642 m->m_pkthdr.header = NULL;
643 m->m_pkthdr.csum_flags = 0;
644 m->m_pkthdr.csum_data = 0;
645 m->m_pkthdr.tso_segsz = 0;
646 m->m_pkthdr.ether_vtag = 0;
647 m->m_pkthdr.flowid = 0;
648 SLIST_INIT(&m->m_pkthdr.tags);
650 /* If the label init fails, fail the alloc */
651 error = mac_mbuf_init(m, how);
656 /* m_ext is already initialized. */
662 * This is the protocol drain routine.
664 * No locks should be held when this is called. The drain routines have to
665 * presently acquire some locks which raises the possibility of lock order
669 mb_reclaim(void *junk)
674 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
677 for (dp = domains; dp != NULL; dp = dp->dom_next)
678 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
679 if (pr->pr_drain != NULL)