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>
50 #include <vm/vm_map.h>
52 #include <vm/uma_int.h>
53 #include <vm/uma_dbg.h>
56 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
59 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
60 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
61 * administrator so desires.
63 * Mbufs are allocated from a UMA Master Zone called the Mbuf
66 * Additionally, FreeBSD provides a Packet Zone, which it
67 * configures as a Secondary Zone to the Mbuf Master Zone,
68 * thus sharing backend Slab kegs with the Mbuf Master Zone.
70 * Thus common-case allocations and locking are simplified:
74 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
76 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
77 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
83 * \____________(VM)_________________/
86 * Whenever an object is allocated with uma_zalloc() out of
87 * one of the Zones its _ctor_ function is executed. The same
88 * for any deallocation through uma_zfree() the _dtor_ function
91 * Caches are per-CPU and are filled from the Master Zone.
93 * Whenever an object is allocated from the underlying global
94 * memory pool it gets pre-initialized with the _zinit_ functions.
95 * When the Keg's are overfull objects get decomissioned with
96 * _zfini_ functions and free'd back to the global memory pool.
100 int nmbufs; /* limits number of mbufs */
101 int nmbclusters; /* limits number of mbuf clusters */
102 int nmbjumbop; /* limits number of page size jumbo clusters */
103 int nmbjumbo9; /* limits number of 9k jumbo clusters */
104 int nmbjumbo16; /* limits number of 16k jumbo clusters */
105 struct mbstat mbstat;
107 static quad_t maxmbufmem; /* overall real memory limit for all mbufs */
109 SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN, &maxmbufmem, 0,
110 "Maximum real memory allocateable to various mbuf types");
113 * tunable_mbinit() has to be run before any mbuf allocations are done.
116 tunable_mbinit(void *dummy)
121 * The default limit for all mbuf related memory is 1/2 of all
122 * available kernel memory (physical or kmem).
123 * At most it can be 3/4 of available kernel memory.
125 realmem = qmin((quad_t)physmem * PAGE_SIZE,
126 vm_map_max(kmem_map) - vm_map_min(kmem_map));
127 maxmbufmem = realmem / 2;
128 TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem);
129 if (maxmbufmem > realmem / 4 * 3)
130 maxmbufmem = realmem / 4 * 3;
132 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
133 if (nmbclusters == 0)
134 nmbclusters = maxmbufmem / MCLBYTES / 4;
136 TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
138 nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
140 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
142 nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
144 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
146 nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
149 * We need at least as many mbufs as we have clusters of
150 * the various types added together.
152 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
153 if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
154 nmbufs = lmax(maxmbufmem / MSIZE / 5,
155 nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
157 SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
160 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
162 int error, newnmbclusters;
164 newnmbclusters = nmbclusters;
165 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
166 if (error == 0 && req->newptr && newnmbclusters != nmbclusters) {
167 if (newnmbclusters > nmbclusters &&
168 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
169 nmbclusters = newnmbclusters;
170 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
171 EVENTHANDLER_INVOKE(nmbclusters_change);
177 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
178 &nmbclusters, 0, sysctl_nmbclusters, "IU",
179 "Maximum number of mbuf clusters allowed");
182 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
184 int error, newnmbjumbop;
186 newnmbjumbop = nmbjumbop;
187 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
188 if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) {
189 if (newnmbjumbop > nmbjumbop &&
190 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
191 nmbjumbop = newnmbjumbop;
192 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
198 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
199 &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
200 "Maximum number of mbuf page size jumbo clusters allowed");
203 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
205 int error, newnmbjumbo9;
207 newnmbjumbo9 = nmbjumbo9;
208 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
209 if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) {
210 if (newnmbjumbo9 > nmbjumbo9 &&
211 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
212 nmbjumbo9 = newnmbjumbo9;
213 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
219 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
220 &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
221 "Maximum number of mbuf 9k jumbo clusters allowed");
224 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
226 int error, newnmbjumbo16;
228 newnmbjumbo16 = nmbjumbo16;
229 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
230 if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) {
231 if (newnmbjumbo16 > nmbjumbo16 &&
232 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
233 nmbjumbo16 = newnmbjumbo16;
234 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
240 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
241 &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
242 "Maximum number of mbuf 16k jumbo clusters allowed");
245 sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
247 int error, newnmbufs;
250 error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
251 if (error == 0 && req->newptr && newnmbufs != nmbufs) {
252 if (newnmbufs > nmbufs) {
254 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
255 EVENTHANDLER_INVOKE(nmbufs_change);
261 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT|CTLFLAG_RW,
262 &nmbufs, 0, sysctl_nmbufs, "IU",
263 "Maximum number of mbufs allowed");
265 SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
266 "Mbuf general information and statistics");
269 * Zones from which we allocate.
271 uma_zone_t zone_mbuf;
272 uma_zone_t zone_clust;
273 uma_zone_t zone_pack;
274 uma_zone_t zone_jumbop;
275 uma_zone_t zone_jumbo9;
276 uma_zone_t zone_jumbo16;
277 uma_zone_t zone_ext_refcnt;
282 static int mb_ctor_mbuf(void *, int, void *, int);
283 static int mb_ctor_clust(void *, int, void *, int);
284 static int mb_ctor_pack(void *, int, void *, int);
285 static void mb_dtor_mbuf(void *, int, void *);
286 static void mb_dtor_clust(void *, int, void *);
287 static void mb_dtor_pack(void *, int, void *);
288 static int mb_zinit_pack(void *, int, int);
289 static void mb_zfini_pack(void *, int);
291 static void mb_reclaim(void *);
292 static void *mbuf_jumbo_alloc(uma_zone_t, int, uint8_t *, int);
294 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
295 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
298 * Initialize FreeBSD Network buffer allocation.
301 mbuf_init(void *dummy)
305 * Configure UMA zones for Mbufs, Clusters, and Packets.
307 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
308 mb_ctor_mbuf, mb_dtor_mbuf,
310 trash_init, trash_fini,
314 MSIZE - 1, UMA_ZONE_MAXBUCKET);
316 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
318 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
319 mb_ctor_clust, mb_dtor_clust,
321 trash_init, trash_fini,
325 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
327 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
329 zone_pack = uma_zsecond_create(MBUF_PACKET_MEM_NAME, mb_ctor_pack,
330 mb_dtor_pack, mb_zinit_pack, mb_zfini_pack, zone_mbuf);
332 /* Make jumbo frame zone too. Page size, 9k and 16k. */
333 zone_jumbop = uma_zcreate(MBUF_JUMBOP_MEM_NAME, MJUMPAGESIZE,
334 mb_ctor_clust, mb_dtor_clust,
336 trash_init, trash_fini,
340 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
342 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
344 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
345 mb_ctor_clust, mb_dtor_clust,
347 trash_init, trash_fini,
351 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
352 uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
354 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
356 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
357 mb_ctor_clust, mb_dtor_clust,
359 trash_init, trash_fini,
363 UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
364 uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
366 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
368 zone_ext_refcnt = uma_zcreate(MBUF_EXTREFCNT_MEM_NAME, sizeof(u_int),
371 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
373 /* uma_prealloc() goes here... */
376 * Hook event handler for low-memory situation, used to
377 * drain protocols and push data back to the caches (UMA
378 * later pushes it back to VM).
380 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
381 EVENTHANDLER_PRI_FIRST);
384 * [Re]set counters and local statistics knobs.
385 * XXX Some of these should go and be replaced, but UMA stat
386 * gathering needs to be revised.
389 mbstat.m_mclusts = 0;
391 mbstat.m_msize = MSIZE;
392 mbstat.m_mclbytes = MCLBYTES;
393 mbstat.m_minclsize = MINCLSIZE;
394 mbstat.m_mlen = MLEN;
395 mbstat.m_mhlen = MHLEN;
396 mbstat.m_numtypes = MT_NTYPES;
398 mbstat.m_mcfail = mbstat.m_mpfail = 0;
400 mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
402 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
405 * UMA backend page allocator for the jumbo frame zones.
407 * Allocates kernel virtual memory that is backed by contiguous physical
411 mbuf_jumbo_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
414 /* Inform UMA that this allocator uses kernel_map/object. */
415 *flags = UMA_SLAB_KERNEL;
416 return ((void *)kmem_alloc_contig(kernel_map, bytes, wait,
417 (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0, VM_MEMATTR_DEFAULT));
421 * Constructor for Mbuf master zone.
423 * The 'arg' pointer points to a mb_args structure which
424 * contains call-specific information required to support the
425 * mbuf allocation API. See mbuf.h.
428 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
431 struct mb_args *args;
439 trash_ctor(mem, size, arg, how);
441 m = (struct mbuf *)mem;
442 args = (struct mb_args *)arg;
447 * The mbuf is initialized later. The caller has the
448 * responsibility to set up any MAC labels too.
450 if (type == MT_NOINIT)
458 if (flags & M_PKTHDR) {
459 m->m_data = m->m_pktdat;
460 m->m_pkthdr.rcvif = NULL;
461 m->m_pkthdr.header = NULL;
463 m->m_pkthdr.csum_flags = 0;
464 m->m_pkthdr.csum_data = 0;
465 m->m_pkthdr.tso_segsz = 0;
466 m->m_pkthdr.ether_vtag = 0;
467 m->m_pkthdr.flowid = 0;
468 SLIST_INIT(&m->m_pkthdr.tags);
470 /* If the label init fails, fail the alloc */
471 error = mac_mbuf_init(m, how);
476 m->m_data = m->m_dat;
481 * The Mbuf master zone destructor.
484 mb_dtor_mbuf(void *mem, int size, void *arg)
489 m = (struct mbuf *)mem;
490 flags = (unsigned long)arg;
492 if ((flags & MB_NOTAGS) == 0 && (m->m_flags & M_PKTHDR) != 0)
493 m_tag_delete_chain(m, NULL);
494 KASSERT((m->m_flags & M_EXT) == 0, ("%s: M_EXT set", __func__));
495 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
497 trash_dtor(mem, size, arg);
502 * The Mbuf Packet zone destructor.
505 mb_dtor_pack(void *mem, int size, void *arg)
509 m = (struct mbuf *)mem;
510 if ((m->m_flags & M_PKTHDR) != 0)
511 m_tag_delete_chain(m, NULL);
513 /* Make sure we've got a clean cluster back. */
514 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
515 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
516 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
517 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
518 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
519 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
520 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
521 KASSERT(*m->m_ext.ref_cnt == 1, ("%s: ref_cnt != 1", __func__));
523 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
526 * If there are processes blocked on zone_clust, waiting for pages
527 * to be freed up, * cause them to be woken up by draining the
528 * packet zone. We are exposed to a race here * (in the check for
529 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
530 * is deliberate. We don't want to acquire the zone lock for every
533 if (uma_zone_exhausted_nolock(zone_clust))
534 zone_drain(zone_pack);
538 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
540 * Here the 'arg' pointer points to the Mbuf which we
541 * are configuring cluster storage for. If 'arg' is
542 * empty we allocate just the cluster without setting
543 * the mbuf to it. See mbuf.h.
546 mb_ctor_clust(void *mem, int size, void *arg, int how)
554 trash_ctor(mem, size, arg, how);
561 #if MJUMPAGESIZE != MCLBYTES
576 panic("unknown cluster size");
580 m = (struct mbuf *)arg;
581 refcnt = uma_find_refcnt(zone, mem);
584 m->m_ext.ext_buf = (caddr_t)mem;
585 m->m_data = m->m_ext.ext_buf;
587 m->m_ext.ext_free = NULL;
588 m->m_ext.ext_arg1 = NULL;
589 m->m_ext.ext_arg2 = NULL;
590 m->m_ext.ext_size = size;
591 m->m_ext.ext_type = type;
592 m->m_ext.ref_cnt = refcnt;
599 * The Mbuf Cluster zone destructor.
602 mb_dtor_clust(void *mem, int size, void *arg)
607 zone = m_getzone(size);
608 KASSERT(*(uma_find_refcnt(zone, mem)) <= 1,
609 ("%s: refcnt incorrect %u", __func__,
610 *(uma_find_refcnt(zone, mem))) );
612 trash_dtor(mem, size, arg);
617 * The Packet secondary zone's init routine, executed on the
618 * object's transition from mbuf keg slab to zone cache.
621 mb_zinit_pack(void *mem, int size, int how)
625 m = (struct mbuf *)mem; /* m is virgin. */
626 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
627 m->m_ext.ext_buf == NULL)
629 m->m_ext.ext_type = EXT_PACKET; /* Override. */
631 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
637 * The Packet secondary zone's fini routine, executed on the
638 * object's transition from zone cache to keg slab.
641 mb_zfini_pack(void *mem, int size)
645 m = (struct mbuf *)mem;
647 trash_fini(m->m_ext.ext_buf, MCLBYTES);
649 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
651 trash_dtor(mem, size, NULL);
656 * The "packet" keg constructor.
659 mb_ctor_pack(void *mem, int size, void *arg, int how)
662 struct mb_args *args;
669 m = (struct mbuf *)mem;
670 args = (struct mb_args *)arg;
675 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
679 m->m_data = m->m_ext.ext_buf;
681 m->m_flags = (flags | M_EXT);
684 if (flags & M_PKTHDR) {
685 m->m_pkthdr.rcvif = NULL;
687 m->m_pkthdr.header = NULL;
688 m->m_pkthdr.csum_flags = 0;
689 m->m_pkthdr.csum_data = 0;
690 m->m_pkthdr.tso_segsz = 0;
691 m->m_pkthdr.ether_vtag = 0;
692 m->m_pkthdr.flowid = 0;
693 SLIST_INIT(&m->m_pkthdr.tags);
695 /* If the label init fails, fail the alloc */
696 error = mac_mbuf_init(m, how);
701 /* m_ext is already initialized. */
707 m_pkthdr_init(struct mbuf *m, int how)
712 m->m_data = m->m_pktdat;
713 SLIST_INIT(&m->m_pkthdr.tags);
714 m->m_pkthdr.rcvif = NULL;
715 m->m_pkthdr.header = NULL;
717 m->m_pkthdr.flowid = 0;
718 m->m_pkthdr.csum_flags = 0;
719 m->m_pkthdr.csum_data = 0;
720 m->m_pkthdr.tso_segsz = 0;
721 m->m_pkthdr.ether_vtag = 0;
723 /* If the label init fails, fail the alloc */
724 error = mac_mbuf_init(m, how);
733 * This is the protocol drain routine.
735 * No locks should be held when this is called. The drain routines have to
736 * presently acquire some locks which raises the possibility of lock order
740 mb_reclaim(void *junk)
745 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
748 for (dp = domains; dp != NULL; dp = dp->dom_next)
749 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
750 if (pr->pr_drain != NULL)