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$");
32 #include "opt_param.h"
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <sys/domain.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
45 #include <security/mac/mac_framework.h>
48 #include <vm/vm_page.h>
50 #include <vm/uma_int.h>
51 #include <vm/uma_dbg.h>
54 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
57 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
58 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
59 * administrator so desires.
61 * Mbufs are allocated from a UMA Master Zone called the Mbuf
64 * Additionally, FreeBSD provides a Packet Zone, which it
65 * configures as a Secondary Zone to the Mbuf Master Zone,
66 * thus sharing backend Slab kegs with the Mbuf Master Zone.
68 * Thus common-case allocations and locking are simplified:
72 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
74 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
75 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
81 * \____________(VM)_________________/
84 * Whenever an object is allocated with uma_zalloc() out of
85 * one of the Zones its _ctor_ function is executed. The same
86 * for any deallocation through uma_zfree() the _dtor_ function
89 * Caches are per-CPU and are filled from the Master Zone.
91 * Whenever an object is allocated from the underlying global
92 * memory pool it gets pre-initialized with the _zinit_ functions.
93 * When the Keg's are overfull objects get decomissioned with
94 * _zfini_ functions and free'd back to the global memory pool.
98 int nmbclusters; /* limits number of mbuf clusters */
99 int nmbjumbop; /* limits number of page size jumbo clusters */
100 int nmbjumbo9; /* limits number of 9k jumbo clusters */
101 int nmbjumbo16; /* limits number of 16k jumbo clusters */
102 struct mbstat mbstat;
105 tunable_mbinit(void *dummy)
108 /* This has to be done before VM init. */
109 nmbclusters = 1024 + maxusers * 64;
110 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
112 SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
114 /* XXX: These should be tuneables. Can't change UMA limits on the fly. */
116 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
118 int error, newnmbclusters;
120 newnmbclusters = nmbclusters;
121 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
122 if (error == 0 && req->newptr) {
123 if (newnmbclusters > nmbclusters) {
124 nmbclusters = newnmbclusters;
125 uma_zone_set_max(zone_clust, nmbclusters);
126 EVENTHANDLER_INVOKE(nmbclusters_change);
132 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
133 &nmbclusters, 0, sysctl_nmbclusters, "IU",
134 "Maximum number of mbuf clusters allowed");
135 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbop, CTLFLAG_RW, &nmbjumbop, 0,
136 "Maximum number of mbuf page size jumbo clusters allowed");
137 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo9, CTLFLAG_RW, &nmbjumbo9, 0,
138 "Maximum number of mbuf 9k jumbo clusters allowed");
139 SYSCTL_INT(_kern_ipc, OID_AUTO, nmbjumbo16, CTLFLAG_RW, &nmbjumbo16, 0,
140 "Maximum number of mbuf 16k jumbo clusters allowed");
141 SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
142 "Mbuf general information and statistics");
145 * Zones from which we allocate.
147 uma_zone_t zone_mbuf;
148 uma_zone_t zone_clust;
149 uma_zone_t zone_pack;
150 uma_zone_t zone_jumbop;
151 uma_zone_t zone_jumbo9;
152 uma_zone_t zone_jumbo16;
153 uma_zone_t zone_ext_refcnt;
158 static int mb_ctor_mbuf(void *, int, void *, int);
159 static int mb_ctor_clust(void *, int, void *, int);
160 static int mb_ctor_pack(void *, int, void *, int);
161 static void mb_dtor_mbuf(void *, int, void *);
162 static void mb_dtor_clust(void *, int, void *);
163 static void mb_dtor_pack(void *, int, void *);
164 static int mb_zinit_pack(void *, int, int);
165 static void mb_zfini_pack(void *, int);
167 static void mb_reclaim(void *);
168 static void mbuf_init(void *);
170 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
171 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
174 * Initialize FreeBSD Network buffer allocation.
176 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
178 mbuf_init(void *dummy)
182 * Configure UMA zones for Mbufs, Clusters, and Packets.
184 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
185 mb_ctor_mbuf, mb_dtor_mbuf,
187 trash_init, trash_fini,
191 MSIZE - 1, UMA_ZONE_MAXBUCKET);
193 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
194 mb_ctor_clust, mb_dtor_clust,
196 trash_init, trash_fini,
200 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
202 uma_zone_set_max(zone_clust, nmbclusters);
204 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
205 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
207 /* Make jumbo frame zone too. Page size, 9k and 16k. */
208 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
209 mb_ctor_clust, mb_dtor_clust,
211 trash_init, trash_fini,
215 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
217 uma_zone_set_max(zone_jumbop, nmbjumbop);
219 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
220 mb_ctor_clust, mb_dtor_clust,
222 trash_init, trash_fini,
226 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
228 uma_zone_set_max(zone_jumbo9, nmbjumbo9);
230 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
231 mb_ctor_clust, mb_dtor_clust,
233 trash_init, trash_fini,
237 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
239 uma_zone_set_max(zone_jumbo16, nmbjumbo16);
241 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
244 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
246 /* uma_prealloc() goes here... */
249 * Hook event handler for low-memory situation, used to
250 * drain protocols and push data back to the caches (UMA
251 * later pushes it back to VM).
253 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
254 EVENTHANDLER_PRI_FIRST);
257 * [Re]set counters and local statistics knobs.
258 * XXX Some of these should go and be replaced, but UMA stat
259 * gathering needs to be revised.
262 mbstat.m_mclusts = 0;
264 mbstat.m_msize = MSIZE;
265 mbstat.m_mclbytes = MCLBYTES;
266 mbstat.m_minclsize = MINCLSIZE;
267 mbstat.m_mlen = MLEN;
268 mbstat.m_mhlen = MHLEN;
269 mbstat.m_numtypes = MT_NTYPES;
271 mbstat.m_mcfail = mbstat.m_mpfail = 0;
273 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
277 * Constructor for Mbuf master zone.
279 * The 'arg' pointer points to a mb_args structure which
280 * contains call-specific information required to support the
281 * mbuf allocation API. See mbuf.h.
284 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
287 struct mb_args *args;
295 trash_ctor(mem, size, arg, how);
297 m = (struct mbuf *)mem;
298 args = (struct mb_args *)arg;
303 * The mbuf is initialized later. The caller has the
304 * responsibility to set up any MAC labels too.
306 if (type == MT_NOINIT)
314 if (flags & M_PKTHDR) {
315 m->m_data = m->m_pktdat;
316 m->m_pkthdr.rcvif = NULL;
318 m->m_pkthdr.header = NULL;
319 m->m_pkthdr.csum_flags = 0;
320 m->m_pkthdr.csum_data = 0;
321 m->m_pkthdr.tso_segsz = 0;
322 m->m_pkthdr.ether_vtag = 0;
323 SLIST_INIT(&m->m_pkthdr.tags);
325 /* If the label init fails, fail the alloc */
326 error = mac_init_mbuf(m, how);
331 m->m_data = m->m_dat;
336 * The Mbuf master zone destructor.
339 mb_dtor_mbuf(void *mem, int size, void *arg)
344 m = (struct mbuf *)mem;
345 flags = (unsigned long)arg;
347 if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0)
348 m_tag_delete_chain(m, NULL);
349 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
351 trash_dtor(mem, size, arg);
356 * The Mbuf Packet zone destructor.
359 mb_dtor_pack(void *mem, int size, void *arg)
363 m = (struct mbuf *)mem;
364 if ((m->m_flags & M_PKTHDR) != 0)
365 m_tag_delete_chain(m, NULL);
367 /* Make sure we've got a clean cluster back. */
368 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
369 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
370 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
371 KASSERT(m->m_ext.ext_args == NULL, ("%s: ext_args != NULL", __func__));
372 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
373 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
374 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
376 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
379 * If there are processes blocked on zone_clust, waiting for pages to be freed up,
380 * cause them to be woken up by draining the packet zone. We are exposed to a race here
381 * (in the check for the UMA_ZFLAG_FULL) where we might miss the flag set, but that is
382 * deliberate. We don't want to acquire the zone lock for every mbuf free.
384 if (uma_zone_exhausted_nolock(zone_clust))
385 zone_drain(zone_pack);
389 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
391 * Here the 'arg' pointer points to the Mbuf which we
392 * are configuring cluster storage for. If 'arg' is
393 * empty we allocate just the cluster without setting
394 * the mbuf to it. See mbuf.h.
397 mb_ctor_clust(void *mem, int size, void *arg, int how)
405 trash_ctor(mem, size, arg, how);
412 #if MJUMPAGESIZE != MCLBYTES
427 panic("unknown cluster size");
431 m = (struct mbuf *)arg;
432 refcnt = uma_find_refcnt(zone, mem);
435 m->m_ext.ext_buf = (caddr_t)mem;
436 m->m_data = m->m_ext.ext_buf;
438 m->m_ext.ext_free = NULL;
439 m->m_ext.ext_args = NULL;
440 m->m_ext.ext_size = size;
441 m->m_ext.ext_type = type;
442 m->m_ext.ref_cnt = refcnt;
449 * The Mbuf Cluster zone destructor.
452 mb_dtor_clust(void *mem, int size, void *arg)
457 zone = m_getzone(size);
458 KASSERT(*(uma_find_refcnt(zone, mem)) <= 1,
459 ("%s: refcnt incorrect %u", __func__,
460 *(uma_find_refcnt(zone, mem))) );
462 trash_dtor(mem, size, arg);
467 * The Packet secondary zone's init routine, executed on the
468 * object's transition from mbuf keg slab to zone cache.
471 mb_zinit_pack(void *mem, int size, int how)
475 m = (struct mbuf *)mem; /* m is virgin. */
476 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
477 m->m_ext.ext_buf == NULL)
479 m->m_ext.ext_type = EXT_PACKET; /* Override. */
481 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
487 * The Packet secondary zone's fini routine, executed on the
488 * object's transition from zone cache to keg slab.
491 mb_zfini_pack(void *mem, int size)
495 m = (struct mbuf *)mem;
497 trash_fini(m->m_ext.ext_buf, MCLBYTES);
499 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
501 trash_dtor(mem, size, NULL);
506 * The "packet" keg constructor.
509 mb_ctor_pack(void *mem, int size, void *arg, int how)
512 struct mb_args *args;
519 m = (struct mbuf *)mem;
520 args = (struct mb_args *)arg;
525 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
529 m->m_data = m->m_ext.ext_buf;
531 m->m_flags = (flags | M_EXT);
534 if (flags & M_PKTHDR) {
535 m->m_pkthdr.rcvif = NULL;
537 m->m_pkthdr.header = NULL;
538 m->m_pkthdr.csum_flags = 0;
539 m->m_pkthdr.csum_data = 0;
540 m->m_pkthdr.tso_segsz = 0;
541 m->m_pkthdr.ether_vtag = 0;
542 SLIST_INIT(&m->m_pkthdr.tags);
544 /* If the label init fails, fail the alloc */
545 error = mac_init_mbuf(m, how);
550 /* m_ext is already initialized. */
556 * This is the protocol drain routine.
558 * No locks should be held when this is called. The drain routines have to
559 * presently acquire some locks which raises the possibility of lock order
563 mb_reclaim(void *junk)
568 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
571 for (dp = domains; dp != NULL; dp = dp->dom_next)
572 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
573 if (pr->pr_drain != NULL)