2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004, 2005,
5 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_param.h"
35 #include <sys/param.h>
36 #include <sys/domainset.h>
37 #include <sys/malloc.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
41 #include <sys/domain.h>
42 #include <sys/eventhandler.h>
43 #include <sys/kernel.h>
44 #include <sys/limits.h>
46 #include <sys/mutex.h>
47 #include <sys/protosw.h>
49 #include <sys/sysctl.h>
52 #include <vm/vm_extern.h>
53 #include <vm/vm_kern.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_map.h>
57 #include <vm/uma_dbg.h>
60 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
63 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
64 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
65 * administrator so desires.
67 * Mbufs are allocated from a UMA Master Zone called the Mbuf
70 * Additionally, FreeBSD provides a Packet Zone, which it
71 * configures as a Secondary Zone to the Mbuf Master Zone,
72 * thus sharing backend Slab kegs with the Mbuf Master Zone.
74 * Thus common-case allocations and locking are simplified:
78 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
80 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
81 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
87 * \____________(VM)_________________/
90 * Whenever an object is allocated with uma_zalloc() out of
91 * one of the Zones its _ctor_ function is executed. The same
92 * for any deallocation through uma_zfree() the _dtor_ function
95 * Caches are per-CPU and are filled from the Master Zone.
97 * Whenever an object is allocated from the underlying global
98 * memory pool it gets pre-initialized with the _zinit_ functions.
99 * When the Keg's are overfull objects get decommissioned with
100 * _zfini_ functions and free'd back to the global memory pool.
104 int nmbufs; /* limits number of mbufs */
105 int nmbclusters; /* limits number of mbuf clusters */
106 int nmbjumbop; /* limits number of page size jumbo clusters */
107 int nmbjumbo9; /* limits number of 9k jumbo clusters */
108 int nmbjumbo16; /* limits number of 16k jumbo clusters */
110 static quad_t maxmbufmem; /* overall real memory limit for all mbufs */
112 SYSCTL_QUAD(_kern_ipc, OID_AUTO, maxmbufmem, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &maxmbufmem, 0,
113 "Maximum real memory allocatable to various mbuf types");
116 * tunable_mbinit() has to be run before any mbuf allocations are done.
119 tunable_mbinit(void *dummy)
124 * The default limit for all mbuf related memory is 1/2 of all
125 * available kernel memory (physical or kmem).
126 * At most it can be 3/4 of available kernel memory.
128 realmem = qmin((quad_t)physmem * PAGE_SIZE, vm_kmem_size);
129 maxmbufmem = realmem / 2;
130 TUNABLE_QUAD_FETCH("kern.ipc.maxmbufmem", &maxmbufmem);
131 if (maxmbufmem > realmem / 4 * 3)
132 maxmbufmem = realmem / 4 * 3;
134 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
135 if (nmbclusters == 0)
136 nmbclusters = maxmbufmem / MCLBYTES / 4;
138 TUNABLE_INT_FETCH("kern.ipc.nmbjumbop", &nmbjumbop);
140 nmbjumbop = maxmbufmem / MJUMPAGESIZE / 4;
142 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo9", &nmbjumbo9);
144 nmbjumbo9 = maxmbufmem / MJUM9BYTES / 6;
146 TUNABLE_INT_FETCH("kern.ipc.nmbjumbo16", &nmbjumbo16);
148 nmbjumbo16 = maxmbufmem / MJUM16BYTES / 6;
151 * We need at least as many mbufs as we have clusters of
152 * the various types added together.
154 TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
155 if (nmbufs < nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16)
156 nmbufs = lmax(maxmbufmem / MSIZE / 5,
157 nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16);
159 SYSINIT(tunable_mbinit, SI_SUB_KMEM, SI_ORDER_MIDDLE, tunable_mbinit, NULL);
162 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS)
164 int error, newnmbclusters;
166 newnmbclusters = nmbclusters;
167 error = sysctl_handle_int(oidp, &newnmbclusters, 0, req);
168 if (error == 0 && req->newptr && newnmbclusters != nmbclusters) {
169 if (newnmbclusters > nmbclusters &&
170 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
171 nmbclusters = newnmbclusters;
172 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
173 EVENTHANDLER_INVOKE(nmbclusters_change);
179 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbclusters, CTLTYPE_INT|CTLFLAG_RW,
180 &nmbclusters, 0, sysctl_nmbclusters, "IU",
181 "Maximum number of mbuf clusters allowed");
184 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS)
186 int error, newnmbjumbop;
188 newnmbjumbop = nmbjumbop;
189 error = sysctl_handle_int(oidp, &newnmbjumbop, 0, req);
190 if (error == 0 && req->newptr && newnmbjumbop != nmbjumbop) {
191 if (newnmbjumbop > nmbjumbop &&
192 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
193 nmbjumbop = newnmbjumbop;
194 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
200 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbop, CTLTYPE_INT|CTLFLAG_RW,
201 &nmbjumbop, 0, sysctl_nmbjumbop, "IU",
202 "Maximum number of mbuf page size jumbo clusters allowed");
205 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS)
207 int error, newnmbjumbo9;
209 newnmbjumbo9 = nmbjumbo9;
210 error = sysctl_handle_int(oidp, &newnmbjumbo9, 0, req);
211 if (error == 0 && req->newptr && newnmbjumbo9 != nmbjumbo9) {
212 if (newnmbjumbo9 > nmbjumbo9 &&
213 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
214 nmbjumbo9 = newnmbjumbo9;
215 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
221 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo9, CTLTYPE_INT|CTLFLAG_RW,
222 &nmbjumbo9, 0, sysctl_nmbjumbo9, "IU",
223 "Maximum number of mbuf 9k jumbo clusters allowed");
226 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS)
228 int error, newnmbjumbo16;
230 newnmbjumbo16 = nmbjumbo16;
231 error = sysctl_handle_int(oidp, &newnmbjumbo16, 0, req);
232 if (error == 0 && req->newptr && newnmbjumbo16 != nmbjumbo16) {
233 if (newnmbjumbo16 > nmbjumbo16 &&
234 nmbufs >= nmbclusters + nmbjumbop + nmbjumbo9 + nmbjumbo16) {
235 nmbjumbo16 = newnmbjumbo16;
236 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
242 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbjumbo16, CTLTYPE_INT|CTLFLAG_RW,
243 &nmbjumbo16, 0, sysctl_nmbjumbo16, "IU",
244 "Maximum number of mbuf 16k jumbo clusters allowed");
247 sysctl_nmbufs(SYSCTL_HANDLER_ARGS)
249 int error, newnmbufs;
252 error = sysctl_handle_int(oidp, &newnmbufs, 0, req);
253 if (error == 0 && req->newptr && newnmbufs != nmbufs) {
254 if (newnmbufs > nmbufs) {
256 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
257 EVENTHANDLER_INVOKE(nmbufs_change);
263 SYSCTL_PROC(_kern_ipc, OID_AUTO, nmbufs, CTLTYPE_INT|CTLFLAG_RW,
264 &nmbufs, 0, sysctl_nmbufs, "IU",
265 "Maximum number of mbufs allowed");
268 * Zones from which we allocate.
270 uma_zone_t zone_mbuf;
271 uma_zone_t zone_clust;
272 uma_zone_t zone_pack;
273 uma_zone_t zone_jumbop;
274 uma_zone_t zone_jumbo9;
275 uma_zone_t zone_jumbo16;
280 static int mb_ctor_mbuf(void *, int, void *, int);
281 static int mb_ctor_clust(void *, int, void *, int);
282 static int mb_ctor_pack(void *, int, void *, int);
283 static void mb_dtor_mbuf(void *, int, void *);
284 static void mb_dtor_pack(void *, int, void *);
285 static int mb_zinit_pack(void *, int, int);
286 static void mb_zfini_pack(void *, int);
287 static void mb_reclaim(uma_zone_t, int);
288 static void *mbuf_jumbo_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
290 /* Ensure that MSIZE is a power of 2. */
291 CTASSERT((((MSIZE - 1) ^ MSIZE) + 1) >> 1 == MSIZE);
294 * Initialize FreeBSD Network buffer allocation.
297 mbuf_init(void *dummy)
301 * Configure UMA zones for Mbufs, Clusters, and Packets.
303 zone_mbuf = uma_zcreate(MBUF_MEM_NAME, MSIZE,
304 mb_ctor_mbuf, mb_dtor_mbuf,
306 trash_init, trash_fini,
310 MSIZE - 1, UMA_ZONE_MAXBUCKET);
312 nmbufs = uma_zone_set_max(zone_mbuf, nmbufs);
313 uma_zone_set_warning(zone_mbuf, "kern.ipc.nmbufs limit reached");
314 uma_zone_set_maxaction(zone_mbuf, mb_reclaim);
316 zone_clust = uma_zcreate(MBUF_CLUSTER_MEM_NAME, MCLBYTES,
319 trash_dtor, trash_init, trash_fini,
325 nmbclusters = uma_zone_set_max(zone_clust, nmbclusters);
326 uma_zone_set_warning(zone_clust, "kern.ipc.nmbclusters limit reached");
327 uma_zone_set_maxaction(zone_clust, mb_reclaim);
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,
336 trash_dtor, trash_init, trash_fini,
342 nmbjumbop = uma_zone_set_max(zone_jumbop, nmbjumbop);
343 uma_zone_set_warning(zone_jumbop, "kern.ipc.nmbjumbop limit reached");
344 uma_zone_set_maxaction(zone_jumbop, mb_reclaim);
346 zone_jumbo9 = uma_zcreate(MBUF_JUMBO9_MEM_NAME, MJUM9BYTES,
349 trash_dtor, trash_init, trash_fini,
354 uma_zone_set_allocf(zone_jumbo9, mbuf_jumbo_alloc);
356 nmbjumbo9 = uma_zone_set_max(zone_jumbo9, nmbjumbo9);
357 uma_zone_set_warning(zone_jumbo9, "kern.ipc.nmbjumbo9 limit reached");
358 uma_zone_set_maxaction(zone_jumbo9, mb_reclaim);
360 zone_jumbo16 = uma_zcreate(MBUF_JUMBO16_MEM_NAME, MJUM16BYTES,
363 trash_dtor, trash_init, trash_fini,
368 uma_zone_set_allocf(zone_jumbo16, mbuf_jumbo_alloc);
370 nmbjumbo16 = uma_zone_set_max(zone_jumbo16, nmbjumbo16);
371 uma_zone_set_warning(zone_jumbo16, "kern.ipc.nmbjumbo16 limit reached");
372 uma_zone_set_maxaction(zone_jumbo16, mb_reclaim);
375 * Hook event handler for low-memory situation, used to
376 * drain protocols and push data back to the caches (UMA
377 * later pushes it back to VM).
379 EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
380 EVENTHANDLER_PRI_FIRST);
382 SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL);
386 * netdump makes use of a pre-allocated pool of mbufs and clusters. When
387 * netdump is configured, we initialize a set of UMA cache zones which return
388 * items from this pool. At panic-time, the regular UMA zone pointers are
389 * overwritten with those of the cache zones so that drivers may allocate and
390 * free mbufs and clusters without attempting to allocate physical memory.
392 * We keep mbufs and clusters in a pair of mbuf queues. In particular, for
393 * the purpose of caching clusters, we treat them as mbufs.
395 static struct mbufq nd_mbufq =
396 { STAILQ_HEAD_INITIALIZER(nd_mbufq.mq_head), 0, INT_MAX };
397 static struct mbufq nd_clustq =
398 { STAILQ_HEAD_INITIALIZER(nd_clustq.mq_head), 0, INT_MAX };
400 static int nd_clsize;
401 static uma_zone_t nd_zone_mbuf;
402 static uma_zone_t nd_zone_clust;
403 static uma_zone_t nd_zone_pack;
406 nd_buf_import(void *arg, void **store, int count, int domain __unused,
415 for (i = 0; i < count; i++) {
416 m = mbufq_dequeue(q);
419 trash_init(m, q == &nd_mbufq ? MSIZE : nd_clsize, flags);
426 nd_buf_release(void *arg, void **store, int count)
434 for (i = 0; i < count; i++) {
436 (void)mbufq_enqueue(q, m);
441 nd_pack_import(void *arg __unused, void **store, int count, int domain __unused,
448 for (i = 0; i < count; i++) {
449 m = m_get(MT_DATA, M_NOWAIT);
452 clust = uma_zalloc(nd_zone_clust, M_NOWAIT);
457 mb_ctor_clust(clust, nd_clsize, m, 0);
464 nd_pack_release(void *arg __unused, void **store, int count)
470 for (i = 0; i < count; i++) {
472 clust = m->m_ext.ext_buf;
473 uma_zfree(nd_zone_clust, clust);
474 uma_zfree(nd_zone_mbuf, m);
479 * Free the pre-allocated mbufs and clusters reserved for netdump, and destroy
480 * the corresponding UMA cache zones.
483 netdump_mbuf_drain(void)
488 if (nd_zone_mbuf != NULL) {
489 uma_zdestroy(nd_zone_mbuf);
492 if (nd_zone_clust != NULL) {
493 uma_zdestroy(nd_zone_clust);
494 nd_zone_clust = NULL;
496 if (nd_zone_pack != NULL) {
497 uma_zdestroy(nd_zone_pack);
501 while ((m = mbufq_dequeue(&nd_mbufq)) != NULL)
503 while ((item = mbufq_dequeue(&nd_clustq)) != NULL)
504 uma_zfree(m_getzone(nd_clsize), item);
508 * Callback invoked immediately prior to starting a netdump.
511 netdump_mbuf_dump(void)
515 * All cluster zones return buffers of the size requested by the
516 * drivers. It's up to the driver to reinitialize the zones if the
517 * MTU of a netdump-enabled interface changes.
519 printf("netdump: overwriting mbuf zone pointers\n");
520 zone_mbuf = nd_zone_mbuf;
521 zone_clust = nd_zone_clust;
522 zone_pack = nd_zone_pack;
523 zone_jumbop = nd_zone_clust;
524 zone_jumbo9 = nd_zone_clust;
525 zone_jumbo16 = nd_zone_clust;
529 * Reinitialize the netdump mbuf+cluster pool and cache zones.
532 netdump_mbuf_reinit(int nmbuf, int nclust, int clsize)
537 netdump_mbuf_drain();
541 nd_zone_mbuf = uma_zcache_create("netdump_" MBUF_MEM_NAME,
542 MSIZE, mb_ctor_mbuf, mb_dtor_mbuf,
544 trash_init, trash_fini,
548 nd_buf_import, nd_buf_release,
549 &nd_mbufq, UMA_ZONE_NOBUCKET);
551 nd_zone_clust = uma_zcache_create("netdump_" MBUF_CLUSTER_MEM_NAME,
552 clsize, mb_ctor_clust,
554 trash_dtor, trash_init, trash_fini,
558 nd_buf_import, nd_buf_release,
559 &nd_clustq, UMA_ZONE_NOBUCKET);
561 nd_zone_pack = uma_zcache_create("netdump_" MBUF_PACKET_MEM_NAME,
562 MCLBYTES, mb_ctor_pack, mb_dtor_pack, NULL, NULL,
563 nd_pack_import, nd_pack_release,
564 NULL, UMA_ZONE_NOBUCKET);
566 while (nmbuf-- > 0) {
567 m = m_get(MT_DATA, M_WAITOK);
568 uma_zfree(nd_zone_mbuf, m);
570 while (nclust-- > 0) {
571 item = uma_zalloc(m_getzone(nd_clsize), M_WAITOK);
572 uma_zfree(nd_zone_clust, item);
578 * UMA backend page allocator for the jumbo frame zones.
580 * Allocates kernel virtual memory that is backed by contiguous physical
584 mbuf_jumbo_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
588 /* Inform UMA that this allocator uses kernel_map/object. */
589 *flags = UMA_SLAB_KERNEL;
590 return ((void *)kmem_alloc_contig_domainset(DOMAINSET_FIXED(domain),
591 bytes, wait, (vm_paddr_t)0, ~(vm_paddr_t)0, 1, 0,
592 VM_MEMATTR_DEFAULT));
596 * Constructor for Mbuf master zone.
598 * The 'arg' pointer points to a mb_args structure which
599 * contains call-specific information required to support the
600 * mbuf allocation API. See mbuf.h.
603 mb_ctor_mbuf(void *mem, int size, void *arg, int how)
606 struct mb_args *args;
612 trash_ctor(mem, size, arg, how);
614 args = (struct mb_args *)arg;
618 * The mbuf is initialized later. The caller has the
619 * responsibility to set up any MAC labels too.
621 if (type == MT_NOINIT)
624 m = (struct mbuf *)mem;
626 MPASS((flags & M_NOFREE) == 0);
628 error = m_init(m, how, type, flags);
634 * The Mbuf master zone destructor.
637 mb_dtor_mbuf(void *mem, int size, void *arg)
642 m = (struct mbuf *)mem;
643 flags = (unsigned long)arg;
645 KASSERT((m->m_flags & M_NOFREE) == 0, ("%s: M_NOFREE set", __func__));
646 if (!(flags & MB_DTOR_SKIP) && (m->m_flags & M_PKTHDR) && !SLIST_EMPTY(&m->m_pkthdr.tags))
647 m_tag_delete_chain(m, NULL);
649 trash_dtor(mem, size, arg);
654 * The Mbuf Packet zone destructor.
657 mb_dtor_pack(void *mem, int size, void *arg)
661 m = (struct mbuf *)mem;
662 if ((m->m_flags & M_PKTHDR) != 0)
663 m_tag_delete_chain(m, NULL);
665 /* Make sure we've got a clean cluster back. */
666 KASSERT((m->m_flags & M_EXT) == M_EXT, ("%s: M_EXT not set", __func__));
667 KASSERT(m->m_ext.ext_buf != NULL, ("%s: ext_buf == NULL", __func__));
668 KASSERT(m->m_ext.ext_free == NULL, ("%s: ext_free != NULL", __func__));
669 KASSERT(m->m_ext.ext_arg1 == NULL, ("%s: ext_arg1 != NULL", __func__));
670 KASSERT(m->m_ext.ext_arg2 == NULL, ("%s: ext_arg2 != NULL", __func__));
671 KASSERT(m->m_ext.ext_size == MCLBYTES, ("%s: ext_size != MCLBYTES", __func__));
672 KASSERT(m->m_ext.ext_type == EXT_PACKET, ("%s: ext_type != EXT_PACKET", __func__));
674 trash_dtor(m->m_ext.ext_buf, MCLBYTES, arg);
677 * If there are processes blocked on zone_clust, waiting for pages
678 * to be freed up, * cause them to be woken up by draining the
679 * packet zone. We are exposed to a race here * (in the check for
680 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
681 * is deliberate. We don't want to acquire the zone lock for every
684 if (uma_zone_exhausted_nolock(zone_clust))
685 zone_drain(zone_pack);
689 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
691 * Here the 'arg' pointer points to the Mbuf which we
692 * are configuring cluster storage for. If 'arg' is
693 * empty we allocate just the cluster without setting
694 * the mbuf to it. See mbuf.h.
697 mb_ctor_clust(void *mem, int size, void *arg, int how)
702 trash_ctor(mem, size, arg, how);
704 m = (struct mbuf *)arg;
706 m->m_ext.ext_buf = (char *)mem;
707 m->m_data = m->m_ext.ext_buf;
709 m->m_ext.ext_free = NULL;
710 m->m_ext.ext_arg1 = NULL;
711 m->m_ext.ext_arg2 = NULL;
712 m->m_ext.ext_size = size;
713 m->m_ext.ext_type = m_gettype(size);
714 m->m_ext.ext_flags = EXT_FLAG_EMBREF;
715 m->m_ext.ext_count = 1;
722 * The Packet secondary zone's init routine, executed on the
723 * object's transition from mbuf keg slab to zone cache.
726 mb_zinit_pack(void *mem, int size, int how)
730 m = (struct mbuf *)mem; /* m is virgin. */
731 if (uma_zalloc_arg(zone_clust, m, how) == NULL ||
732 m->m_ext.ext_buf == NULL)
734 m->m_ext.ext_type = EXT_PACKET; /* Override. */
736 trash_init(m->m_ext.ext_buf, MCLBYTES, how);
742 * The Packet secondary zone's fini routine, executed on the
743 * object's transition from zone cache to keg slab.
746 mb_zfini_pack(void *mem, int size)
750 m = (struct mbuf *)mem;
752 trash_fini(m->m_ext.ext_buf, MCLBYTES);
754 uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
756 trash_dtor(mem, size, NULL);
761 * The "packet" keg constructor.
764 mb_ctor_pack(void *mem, int size, void *arg, int how)
767 struct mb_args *args;
771 m = (struct mbuf *)mem;
772 args = (struct mb_args *)arg;
775 MPASS((flags & M_NOFREE) == 0);
778 trash_ctor(m->m_ext.ext_buf, MCLBYTES, arg, how);
781 error = m_init(m, how, type, flags);
783 /* m_ext is already initialized. */
784 m->m_data = m->m_ext.ext_buf;
785 m->m_flags = (flags | M_EXT);
791 * This is the protocol drain routine. Called by UMA whenever any of the
792 * mbuf zones is closed to its limit.
794 * No locks should be held when this is called. The drain routines have to
795 * presently acquire some locks which raises the possibility of lock order
799 mb_reclaim(uma_zone_t zone __unused, int pending __unused)
804 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL, __func__);
806 for (dp = domains; dp != NULL; dp = dp->dom_next)
807 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
808 if (pr->pr_drain != NULL)
813 * Clean up after mbufs with M_EXT storage attached to them if the
814 * reference count hits 1.
817 mb_free_ext(struct mbuf *m)
819 volatile u_int *refcnt;
823 KASSERT(m->m_flags & M_EXT, ("%s: M_EXT not set on %p", __func__, m));
825 /* See if this is the mbuf that holds the embedded refcount. */
826 if (m->m_ext.ext_flags & EXT_FLAG_EMBREF) {
827 refcnt = &m->m_ext.ext_count;
830 KASSERT(m->m_ext.ext_cnt != NULL,
831 ("%s: no refcounting pointer on %p", __func__, m));
832 refcnt = m->m_ext.ext_cnt;
833 mref = __containerof(refcnt, struct mbuf, m_ext.ext_count);
837 * Check if the header is embedded in the cluster. It is
838 * important that we can't touch any of the mbuf fields
839 * after we have freed the external storage, since mbuf
840 * could have been embedded in it. For now, the mbufs
841 * embedded into the cluster are always of type EXT_EXTREF,
842 * and for this type we won't free the mref.
844 if (m->m_flags & M_NOFREE) {
846 KASSERT(m->m_ext.ext_type == EXT_EXTREF,
847 ("%s: no-free mbuf %p has wrong type", __func__, m));
851 /* Free attached storage if this mbuf is the only reference to it. */
852 if (*refcnt == 1 || atomic_fetchadd_int(refcnt, -1) == 1) {
853 switch (m->m_ext.ext_type) {
855 /* The packet zone is special. */
858 uma_zfree(zone_pack, mref);
861 uma_zfree(zone_clust, m->m_ext.ext_buf);
862 uma_zfree(zone_mbuf, mref);
865 uma_zfree(zone_jumbop, m->m_ext.ext_buf);
866 uma_zfree(zone_mbuf, mref);
869 uma_zfree(zone_jumbo9, m->m_ext.ext_buf);
870 uma_zfree(zone_mbuf, mref);
873 uma_zfree(zone_jumbo16, m->m_ext.ext_buf);
874 uma_zfree(zone_mbuf, mref);
880 KASSERT(mref->m_ext.ext_free != NULL,
881 ("%s: ext_free not set", __func__));
882 mref->m_ext.ext_free(mref);
883 uma_zfree(zone_mbuf, mref);
886 KASSERT(m->m_ext.ext_free != NULL,
887 ("%s: ext_free not set", __func__));
888 m->m_ext.ext_free(m);
891 KASSERT(m->m_ext.ext_type == 0,
892 ("%s: unknown ext_type", __func__));
896 if (freembuf && m != mref)
897 uma_zfree(zone_mbuf, m);
901 * Official mbuf(9) allocation KPI for stack and drivers:
903 * m_get() - a single mbuf without any attachments, sys/mbuf.h.
904 * m_gethdr() - a single mbuf initialized as M_PKTHDR, sys/mbuf.h.
905 * m_getcl() - an mbuf + 2k cluster, sys/mbuf.h.
906 * m_clget() - attach cluster to already allocated mbuf.
907 * m_cljget() - attach jumbo cluster to already allocated mbuf.
908 * m_get2() - allocate minimum mbuf that would fit size argument.
909 * m_getm2() - allocate a chain of mbufs/clusters.
910 * m_extadd() - attach external cluster to mbuf.
912 * m_free() - free single mbuf with its tags and ext, sys/mbuf.h.
913 * m_freem() - free chain of mbufs.
917 m_clget(struct mbuf *m, int how)
920 KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
922 m->m_ext.ext_buf = (char *)NULL;
923 uma_zalloc_arg(zone_clust, m, how);
925 * On a cluster allocation failure, drain the packet zone and retry,
926 * we might be able to loosen a few clusters up on the drain.
928 if ((how & M_NOWAIT) && (m->m_ext.ext_buf == NULL)) {
929 zone_drain(zone_pack);
930 uma_zalloc_arg(zone_clust, m, how);
932 MBUF_PROBE2(m__clget, m, how);
933 return (m->m_flags & M_EXT);
937 * m_cljget() is different from m_clget() as it can allocate clusters without
938 * attaching them to an mbuf. In that case the return value is the pointer
939 * to the cluster of the requested size. If an mbuf was specified, it gets
940 * the cluster attached to it and the return value can be safely ignored.
941 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
944 m_cljget(struct mbuf *m, int how, int size)
950 KASSERT((m->m_flags & M_EXT) == 0, ("%s: mbuf %p has M_EXT",
952 m->m_ext.ext_buf = NULL;
955 zone = m_getzone(size);
956 retval = uma_zalloc_arg(zone, m, how);
958 MBUF_PROBE4(m__cljget, m, how, size, retval);
964 * m_get2() allocates minimum mbuf that would fit "size" argument.
967 m_get2(int size, int how, short type, int flags)
975 if (size <= MHLEN || (size <= MLEN && (flags & M_PKTHDR) == 0))
976 return (uma_zalloc_arg(zone_mbuf, &args, how));
977 if (size <= MCLBYTES)
978 return (uma_zalloc_arg(zone_pack, &args, how));
980 if (size > MJUMPAGESIZE)
983 m = uma_zalloc_arg(zone_mbuf, &args, how);
987 n = uma_zalloc_arg(zone_jumbop, m, how);
989 uma_zfree(zone_mbuf, m);
997 * m_getjcl() returns an mbuf with a cluster of the specified size attached.
998 * For size it takes MCLBYTES, MJUMPAGESIZE, MJUM9BYTES, MJUM16BYTES.
1001 m_getjcl(int how, short type, int flags, int size)
1003 struct mb_args args;
1007 if (size == MCLBYTES)
1008 return m_getcl(how, type, flags);
1013 m = uma_zalloc_arg(zone_mbuf, &args, how);
1017 zone = m_getzone(size);
1018 n = uma_zalloc_arg(zone, m, how);
1020 uma_zfree(zone_mbuf, m);
1027 * Allocate a given length worth of mbufs and/or clusters (whatever fits
1028 * best) and return a pointer to the top of the allocated chain. If an
1029 * existing mbuf chain is provided, then we will append the new chain
1030 * to the existing one but still return the top of the newly allocated
1034 m_getm2(struct mbuf *m, int len, int how, short type, int flags)
1036 struct mbuf *mb, *nm = NULL, *mtail = NULL;
1038 KASSERT(len >= 0, ("%s: len is < 0", __func__));
1040 /* Validate flags. */
1041 flags &= (M_PKTHDR | M_EOR);
1043 /* Packet header mbuf must be first in chain. */
1044 if ((flags & M_PKTHDR) && m != NULL)
1047 /* Loop and append maximum sized mbufs to the chain tail. */
1050 mb = m_getjcl(how, type, (flags & M_PKTHDR),
1052 else if (len >= MINCLSIZE)
1053 mb = m_getcl(how, type, (flags & M_PKTHDR));
1054 else if (flags & M_PKTHDR)
1055 mb = m_gethdr(how, type);
1057 mb = m_get(how, type);
1059 /* Fail the whole operation if one mbuf can't be allocated. */
1073 flags &= ~M_PKTHDR; /* Only valid on the first mbuf. */
1076 mtail->m_flags |= M_EOR; /* Only valid on the last mbuf. */
1078 /* If mbuf was supplied, append new chain to the end of it. */
1080 for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next)
1083 mtail->m_flags &= ~M_EOR;
1091 * Configure a provided mbuf to refer to the provided external storage
1092 * buffer and setup a reference count for said buffer.
1095 * mb The existing mbuf to which to attach the provided buffer.
1096 * buf The address of the provided external storage buffer.
1097 * size The size of the provided buffer.
1098 * freef A pointer to a routine that is responsible for freeing the
1099 * provided external storage buffer.
1100 * args A pointer to an argument structure (of any type) to be passed
1101 * to the provided freef routine (may be NULL).
1102 * flags Any other flags to be passed to the provided mbuf.
1103 * type The type that the external storage buffer should be
1110 m_extadd(struct mbuf *mb, char *buf, u_int size, m_ext_free_t freef,
1111 void *arg1, void *arg2, int flags, int type)
1114 KASSERT(type != EXT_CLUSTER, ("%s: EXT_CLUSTER not allowed", __func__));
1116 mb->m_flags |= (M_EXT | flags);
1117 mb->m_ext.ext_buf = buf;
1118 mb->m_data = mb->m_ext.ext_buf;
1119 mb->m_ext.ext_size = size;
1120 mb->m_ext.ext_free = freef;
1121 mb->m_ext.ext_arg1 = arg1;
1122 mb->m_ext.ext_arg2 = arg2;
1123 mb->m_ext.ext_type = type;
1125 if (type != EXT_EXTREF) {
1126 mb->m_ext.ext_count = 1;
1127 mb->m_ext.ext_flags = EXT_FLAG_EMBREF;
1129 mb->m_ext.ext_flags = 0;
1133 * Free an entire chain of mbufs and associated external buffers, if
1137 m_freem(struct mbuf *mb)
1140 MBUF_PROBE1(m__freem, mb);