summaryrefslogtreecommitdiffstats
path: root/sys/kern
diff options
context:
space:
mode:
authorbmilekic <bmilekic@FreeBSD.org>2004-05-31 21:46:06 +0000
committerbmilekic <bmilekic@FreeBSD.org>2004-05-31 21:46:06 +0000
commitf7574a2276b935509aba6b131a39c685a68e61d2 (patch)
treedacbb577a5d3ed365d11df0435010eee4c5380da /sys/kern
parentd5d90e314729317ee9cce434f3c548b3f4aaaf04 (diff)
downloadFreeBSD-src-f7574a2276b935509aba6b131a39c685a68e61d2.zip
FreeBSD-src-f7574a2276b935509aba6b131a39c685a68e61d2.tar.gz
Bring in mbuma to replace mballoc.
mbuma is an Mbuf & Cluster allocator built on top of a number of extensions to the UMA framework, all included herein. Extensions to UMA worth noting: - Better layering between slab <-> zone caches; introduce Keg structure which splits off slab cache away from the zone structure and allows multiple zones to be stacked on top of a single Keg (single type of slab cache); perhaps we should look into defining a subset API on top of the Keg for special use by malloc(9), for example. - UMA_ZONE_REFCNT zones can now be added, and reference counters automagically allocated for them within the end of the associated slab structures. uma_find_refcnt() does a kextract to fetch the slab struct reference from the underlying page, and lookup the corresponding refcnt. mbuma things worth noting: - integrates mbuf & cluster allocations with extended UMA and provides caches for commonly-allocated items; defines several zones (two primary, one secondary) and two kegs. - change up certain code paths that always used to do: m_get() + m_clget() to instead just use m_getcl() and try to take advantage of the newly defined secondary Packet zone. - netstat(1) and systat(1) quickly hacked up to do basic stat reporting but additional stats work needs to be done once some other details within UMA have been taken care of and it becomes clearer to how stats will work within the modified framework. From the user perspective, one implication is that the NMBCLUSTERS compile-time option is no longer used. The maximum number of clusters is still capped off according to maxusers, but it can be made unlimited by setting the kern.ipc.nmbclusters boot-time tunable to zero. Work should be done to write an appropriate sysctl handler allowing dynamic tuning of kern.ipc.nmbclusters at runtime. Additional things worth noting/known issues (READ): - One report of 'ips' (ServeRAID) driver acting really slow in conjunction with mbuma. Need more data. Latest report is that ips is equally sucking with and without mbuma. - Giant leak in NFS code sometimes occurs, can't reproduce but currently analyzing; brueffer is able to reproduce but THIS IS NOT an mbuma-specific problem and currently occurs even WITHOUT mbuma. - Issues in network locking: there is at least one code path in the rip code where one or more locks are acquired and we end up in m_prepend() with M_WAITOK, which causes WITNESS to whine from within UMA. Current temporary solution: force all UMA allocations to be M_NOWAIT from within UMA for now to avoid deadlocks unless WITNESS is defined and we can determine with certainty that we're not holding any locks when we're M_WAITOK. - I've seen at least one weird socketbuffer empty-but- mbuf-still-attached panic. I don't believe this to be related to mbuma but please keep your eyes open, turn on debugging, and capture crash dumps. This change removes more code than it adds. A paper is available detailing the change and considering various performance issues, it was presented at BSDCan2004: http://www.unixdaemons.com/~bmilekic/netbuf_bmilekic.pdf Please read the paper for Future Work and implementation details, as well as credits. Testing and Debugging: rwatson, brueffer, Ketrien I. Saihr-Kesenchedra, ... Reviewed by: Lots of people (for different parts)
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/kern_malloc.c27
-rw-r--r--sys/kern/kern_mbuf.c385
-rw-r--r--sys/kern/subr_mbuf.c1548
-rw-r--r--sys/kern/uipc_mbuf.c235
-rw-r--r--sys/kern/uipc_mbuf2.c40
-rw-r--r--sys/kern/uipc_sockbuf.c13
-rw-r--r--sys/kern/uipc_socket.c93
-rw-r--r--sys/kern/uipc_socket2.c13
-rw-r--r--sys/kern/uipc_syscalls.c16
9 files changed, 690 insertions, 1680 deletions
diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c
index c92e70f..4bc3348 100644
--- a/sys/kern/kern_malloc.c
+++ b/sys/kern/kern_malloc.c
@@ -191,6 +191,7 @@ malloc(size, type, flags)
int indx;
caddr_t va;
uma_zone_t zone;
+ uma_keg_t keg;
#ifdef DIAGNOSTIC
unsigned long osize = size;
#endif
@@ -235,6 +236,7 @@ malloc(size, type, flags)
size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
indx = kmemsize[size >> KMEM_ZSHIFT];
zone = kmemzones[indx].kz_zone;
+ keg = zone->uz_keg;
#ifdef MALLOC_PROFILE
krequests[size >> KMEM_ZSHIFT]++;
#endif
@@ -244,10 +246,11 @@ malloc(size, type, flags)
goto out;
ksp->ks_size |= 1 << indx;
- size = zone->uz_size;
+ size = keg->uk_size;
} else {
size = roundup(size, PAGE_SIZE);
zone = NULL;
+ keg = NULL;
va = uma_large_malloc(size, flags);
mtx_lock(&ksp->ks_mtx);
if (va == NULL)
@@ -309,7 +312,7 @@ free(addr, type)
#ifdef INVARIANTS
struct malloc_type **mtp = addr;
#endif
- size = slab->us_zone->uz_size;
+ size = slab->us_keg->uk_size;
#ifdef INVARIANTS
/*
* Cache a pointer to the malloc_type that most recently freed
@@ -325,7 +328,7 @@ free(addr, type)
sizeof(struct malloc_type *);
*mtp = type;
#endif
- uma_zfree_arg(slab->us_zone, addr, slab);
+ uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
} else {
size = slab->us_size;
uma_large_free(slab);
@@ -364,8 +367,8 @@ realloc(addr, size, type, flags)
("realloc: address %p out of range", (void *)addr));
/* Get the size of the original block */
- if (slab->us_zone)
- alloc = slab->us_zone->uz_size;
+ if (slab->us_keg)
+ alloc = slab->us_keg->uk_size;
else
alloc = slab->us_size;
@@ -410,7 +413,6 @@ kmeminit(dummy)
void *dummy;
{
u_int8_t indx;
- u_long npg;
u_long mem_size;
int i;
@@ -428,7 +430,7 @@ kmeminit(dummy)
* Note that the kmem_map is also used by the zone allocator,
* so make sure that there is enough space.
*/
- vm_kmem_size = VM_KMEM_SIZE;
+ vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
mem_size = cnt.v_page_count;
#if defined(VM_KMEM_SIZE_SCALE)
@@ -462,17 +464,8 @@ kmeminit(dummy)
*/
init_param3(vm_kmem_size / PAGE_SIZE);
- /*
- * In mbuf_init(), we set up submaps for mbufs and clusters, in which
- * case we rounddown() (nmbufs * MSIZE) and (nmbclusters * MCLBYTES),
- * respectively. Mathematically, this means that what we do here may
- * amount to slightly more address space than we need for the submaps,
- * but it never hurts to have an extra page in kmem_map.
- */
- npg = (nmbufs*MSIZE + nmbclusters*MCLBYTES + vm_kmem_size) / PAGE_SIZE;
-
kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
- (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
+ (vm_offset_t *)&kmemlimit, vm_kmem_size);
kmem_map->system_map = 1;
uma_startup2();
diff --git a/sys/kern/kern_mbuf.c b/sys/kern/kern_mbuf.c
new file mode 100644
index 0000000..2bec5ad
--- /dev/null
+++ b/sys/kern/kern_mbuf.c
@@ -0,0 +1,385 @@
+/*-
+ * Copyright (c) 2004
+ * Bosko Milekic <bmilekic@FreeBSD.org>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice unmodified, this list of conditions and the following
+ * disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the names of contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "opt_mac.h"
+#include "opt_param.h"
+
+#include <sys/param.h>
+#include <sys/mac.h>
+#include <sys/malloc.h>
+#include <sys/systm.h>
+#include <sys/mbuf.h>
+#include <sys/domain.h>
+#include <sys/eventhandler.h>
+#include <sys/kernel.h>
+#include <sys/protosw.h>
+#include <sys/smp.h>
+#include <sys/sysctl.h>
+
+#include <vm/vm.h>
+#include <vm/vm_page.h>
+#include <vm/uma.h>
+
+/*
+ * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
+ * Zones.
+ *
+ * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
+ * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
+ * administrator so desires.
+ *
+ * Mbufs are allocated from a UMA Master Zone called the Mbuf
+ * Zone.
+ *
+ * Additionally, FreeBSD provides a Packet Zone, which it
+ * configures as a Secondary Zone to the Mbuf Master Zone,
+ * thus sharing backend Slab kegs with the Mbuf Master Zone.
+ *
+ * Thus common-case allocations and locking are simplified:
+ *
+ * m_clget() m_getcl()
+ * | |
+ * | .------------>[(Packet Cache)] m_get(), m_gethdr()
+ * | | [ Packet ] |
+ * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
+ * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
+ * | \________ |
+ * [ Cluster Keg ] \ /
+ * | [ Mbuf Keg ]
+ * [ Cluster Slabs ] |
+ * | [ Mbuf Slabs ]
+ * \____________(VM)_________________/
+ */
+
+int nmbclusters;
+struct mbstat mbstat;
+
+static void
+tunable_mbinit(void *dummy)
+{
+
+ /* This has to be done before VM init. */
+ nmbclusters = 1024 + maxusers * 64;
+ TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
+}
+SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
+
+SYSCTL_DECL(_kern_ipc);
+SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RW, &nmbclusters, 0,
+ "Maximum number of mbuf clusters allowed");
+SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
+ "Mbuf general information and statistics");
+
+/*
+ * Zones from which we allocate.
+ */
+uma_zone_t zone_mbuf;
+uma_zone_t zone_clust;
+uma_zone_t zone_pack;
+
+/*
+ * Local prototypes.
+ */
+static void mb_ctor_mbuf(void *, int, void *);
+static void mb_ctor_clust(void *, int, void *);
+static void mb_ctor_pack(void *, int, void *);
+static void mb_dtor_mbuf(void *, int, void *);
+static void mb_dtor_clust(void *, int, void *); /* XXX */
+static void mb_dtor_pack(void *, int, void *); /* XXX */
+static void mb_init_pack(void *, int);
+static void mb_fini_pack(void *, int);
+
+static void mb_reclaim(void *);
+static void mbuf_init(void *);
+
+/*
+ * Initialize FreeBSD Network buffer allocation.
+ */
+SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
+static void
+mbuf_init(void *dummy)
+{
+
+ /*
+ * Configure UMA zones for Mbufs, Clusters, and Packets.
+ */
+ zone_mbuf = uma_zcreate("Mbuf", MSIZE, mb_ctor_mbuf, mb_dtor_mbuf,
+ NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_MAXBUCKET);
+ zone_clust = uma_zcreate("MbufClust", MCLBYTES, mb_ctor_clust,
+ mb_dtor_clust, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_REFCNT);
+ if (nmbclusters > 0)
+ uma_zone_set_max(zone_clust, nmbclusters);
+ zone_pack = uma_zsecond_create("Packet", mb_ctor_pack, mb_dtor_pack,
+ mb_init_pack, mb_fini_pack, zone_mbuf);
+
+ /* uma_prealloc() goes here */
+
+ /*
+ * Hook event handler for low-memory situation, used to
+ * drain protocols and push data back to the caches (UMA
+ * later pushes it back to VM).
+ */
+ EVENTHANDLER_REGISTER(vm_lowmem, mb_reclaim, NULL,
+ EVENTHANDLER_PRI_FIRST);
+
+ /*
+ * [Re]set counters and local statistics knobs.
+ * XXX Some of these should go and be replaced, but UMA stat
+ * gathering needs to be revised.
+ */
+ mbstat.m_mbufs = 0;
+ mbstat.m_mclusts = 0;
+ mbstat.m_drain = 0;
+ mbstat.m_msize = MSIZE;
+ mbstat.m_mclbytes = MCLBYTES;
+ mbstat.m_minclsize = MINCLSIZE;
+ mbstat.m_mlen = MLEN;
+ mbstat.m_mhlen = MHLEN;
+ mbstat.m_numtypes = MT_NTYPES;
+
+ mbstat.m_mcfail = mbstat.m_mpfail = 0;
+ mbstat.sf_iocnt = 0;
+ mbstat.sf_allocwait = mbstat.sf_allocfail = 0;
+}
+
+/*
+ * Constructor for Mbuf master zone.
+ *
+ * The 'arg' pointer points to a mb_args structure which
+ * contains call-specific information required to support the
+ * mbuf allocation API.
+ */
+static void
+mb_ctor_mbuf(void *mem, int size, void *arg)
+{
+ struct mbuf *m;
+ struct mb_args *args;
+ int flags;
+ int how;
+ short type;
+
+ m = (struct mbuf *)mem;
+ args = (struct mb_args *)arg;
+ flags = args->flags;
+ how = args->how;
+ type = args->type;
+
+ m->m_type = type;
+ m->m_next = NULL;
+ m->m_nextpkt = NULL;
+ if (flags & M_PKTHDR) {
+ m->m_data = m->m_pktdat;
+ m->m_flags = M_PKTHDR;
+ m->m_pkthdr.rcvif = NULL;
+ m->m_pkthdr.csum_flags = 0;
+ SLIST_INIT(&m->m_pkthdr.tags);
+#ifdef MAC
+ /* If the label init fails, fail the alloc */
+ if (mac_init_mbuf(m, how) != 0) {
+ m_free(m);
+/* XXX*/ panic("mb_ctor_mbuf(): can't deal with failure!");
+/* return 0; */
+ }
+#endif
+ } else {
+ m->m_data = m->m_dat;
+ m->m_flags = 0;
+ }
+ mbstat.m_mbufs += 1; /* XXX */
+/* return 1;
+*/
+}
+
+/*
+ * The Mbuf master zone and Packet secondary zone destructor.
+ */
+static void
+mb_dtor_mbuf(void *mem, int size, void *arg)
+{
+ struct mbuf *m;
+
+ m = (struct mbuf *)mem;
+ if ((m->m_flags & M_PKTHDR) != 0)
+ m_tag_delete_chain(m, NULL);
+ mbstat.m_mbufs -= 1; /* XXX */
+}
+
+/* XXX Only because of stats */
+static void
+mb_dtor_pack(void *mem, int size, void *arg)
+{
+ struct mbuf *m;
+
+ m = (struct mbuf *)mem;
+ if ((m->m_flags & M_PKTHDR) != 0)
+ m_tag_delete_chain(m, NULL);
+ mbstat.m_mbufs -= 1; /* XXX */
+ mbstat.m_mclusts -= 1; /* XXX */
+}
+
+/*
+ * The Cluster zone constructor.
+ *
+ * Here the 'arg' pointer points to the Mbuf which we
+ * are configuring cluster storage for.
+ */
+static void
+mb_ctor_clust(void *mem, int size, void *arg)
+{
+ struct mbuf *m;
+
+ m = (struct mbuf *)arg;
+ m->m_ext.ext_buf = (caddr_t)mem;
+ m->m_data = m->m_ext.ext_buf;
+ m->m_flags |= M_EXT;
+ m->m_ext.ext_free = NULL;
+ m->m_ext.ext_args = NULL;
+ m->m_ext.ext_size = MCLBYTES;
+ m->m_ext.ext_type = EXT_CLUSTER;
+ m->m_ext.ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
+ m->m_ext.ext_buf);
+ *(m->m_ext.ref_cnt) = 1;
+ mbstat.m_mclusts += 1; /* XXX */
+/* return 1;
+*/
+}
+
+/* XXX */
+static void
+mb_dtor_clust(void *mem, int size, void *arg)
+{
+ mbstat.m_mclusts -= 1; /* XXX */
+}
+
+/*
+ * The Packet secondary zone's init routine, executed on the
+ * object's transition from keg slab to zone cache.
+ */
+static void
+mb_init_pack(void *mem, int size)
+{
+ struct mbuf *m;
+
+ m = (struct mbuf *)mem;
+ m->m_ext.ext_buf = NULL;
+ uma_zalloc_arg(zone_clust, m, M_NOWAIT);
+ if (m->m_ext.ext_buf == NULL) /* XXX */
+ panic("mb_init_pack(): Can't deal with failure yet.");
+ mbstat.m_mclusts -= 1; /* XXX */
+}
+
+/*
+ * The Packet secondary zone's fini routine, executed on the
+ * object's transition from zone cache to keg slab.
+ */
+static void
+mb_fini_pack(void *mem, int size)
+{
+ struct mbuf *m;
+
+ m = (struct mbuf *)mem;
+ uma_zfree_arg(zone_clust, m->m_ext.ext_buf, NULL);
+ m->m_ext.ext_buf = NULL;
+ mbstat.m_mclusts += 1; /* XXX */
+}
+
+/*
+ * The "packet" keg constructor.
+ */
+static void
+mb_ctor_pack(void *mem, int size, void *arg)
+{
+ struct mbuf *m;
+ struct mb_args *args;
+ int flags, how;
+ short type;
+
+ m = (struct mbuf *)mem;
+ args = (struct mb_args *)arg;
+ flags = args->flags;
+ type = args->type;
+ how = args->how;
+
+ m->m_type = type;
+ m->m_next = NULL;
+ m->m_data = m->m_ext.ext_buf;
+ m->m_flags = flags|M_EXT;
+ m->m_ext.ext_free = NULL;
+ m->m_ext.ext_args = NULL;
+ m->m_ext.ext_size = MCLBYTES;
+ m->m_ext.ext_type = EXT_PACKET;
+ *(m->m_ext.ref_cnt) = 1;
+
+ if (flags & M_PKTHDR) {
+ m->m_nextpkt = NULL;
+ m->m_pkthdr.rcvif = NULL;
+ m->m_pkthdr.csum_flags = 0;
+ SLIST_INIT(&m->m_pkthdr.tags);
+#ifdef MAC
+ /* If the label init fails, fail the alloc */
+ if (mac_init_mbuf(m, how) != 0) {
+ m_free(m);
+/* XXX*/ panic("mb_ctor_pack(): can't deal with failure!");
+/* return 0; */
+ }
+#endif
+ }
+ mbstat.m_mbufs += 1; /* XXX */
+ mbstat.m_mclusts += 1; /* XXX */
+/* return 1;
+*/
+}
+
+/*
+ * This is the protocol drain routine.
+ *
+ * No locks should be held when this is called. The drain routines have to
+ * presently acquire some locks which raises the possibility of lock order
+ * reversal.
+ */
+static void
+mb_reclaim(void *junk)
+{
+ struct domain *dp;
+ struct protosw *pr;
+
+ WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
+ "mb_reclaim()");
+
+ mbstat.m_drain++;
+ for (dp = domains; dp != NULL; dp = dp->dom_next)
+ for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
+ if (pr->pr_drain != NULL)
+ (*pr->pr_drain)();
+}
diff --git a/sys/kern/subr_mbuf.c b/sys/kern/subr_mbuf.c
deleted file mode 100644
index d84ef31..0000000
--- a/sys/kern/subr_mbuf.c
+++ /dev/null
@@ -1,1548 +0,0 @@
-/*-
- * Copyright (c) 2001, 2002, 2003
- * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include "opt_mac.h"
-#include "opt_param.h"
-
-#include <sys/param.h>
-#include <sys/systm.h>
-#include <sys/malloc.h>
-#include <sys/mac.h>
-#include <sys/mbuf.h>
-#include <sys/lock.h>
-#include <sys/mutex.h>
-#include <sys/condvar.h>
-#include <sys/smp.h>
-#include <sys/kernel.h>
-#include <sys/sysctl.h>
-#include <sys/domain.h>
-#include <sys/protosw.h>
-
-#include <vm/vm.h>
-#include <vm/vm_kern.h>
-#include <vm/vm_extern.h>
-#include <vm/pmap.h>
-#include <vm/vm_map.h>
-
-/*
- * mb_alloc: network buffer allocator
- *
- * XXX: currently, the "low watermark" sysctl is marked read-only as its
- * effects are not completely implemented. To be fixed soon.
- */
-
-/*
- * Maximum number of PCPU containers. If you know what you're doing you could
- * explicitly define MBALLOC_NCPU to be exactly the number of CPUs on your
- * system during compilation, and thus prevent kernel structure bloat.
- *
- * SMP and non-SMP kernels clearly have a different number of possible CPUs,
- * but because we cannot assume a dense array of CPUs, we always allocate
- * and traverse PCPU containers up to NCPU amount and merely check for
- * CPU availability.
- */
-#ifdef MBALLOC_NCPU
-#define NCPU MBALLOC_NCPU
-#else
-#define NCPU MAXCPU
-#endif
-
-/*-
- * The mbuf allocator is based on Alfred Perlstein's <alfred@FreeBSD.org>
- * "memcache" proof-of-concept allocator which was itself based on
- * several well-known SMP-friendly allocators.
- *
- * The mb_alloc mbuf allocator is a special when compared to other
- * general-purpose allocators. Some things to take note of:
- *
- * Mbufs and mbuf clusters are two different objects. Sometimes we
- * will allocate a single mbuf, other times a single cluster,
- * other times both. Further, we may sometimes wish to allocate a
- * whole chain of mbufs with clusters. This allocator will perform
- * the common case of each scenario in one function call (this
- * includes constructing or destructing the object) while only
- * locking/unlocking the cache once, if it can get away with it.
- * The caches consist of pure mbufs and pure clusters; that is
- * there are no 'zones' containing mbufs with already pre-hooked
- * clusters. Since we can allocate both objects atomically anyway,
- * we don't bother fragmenting our caches for any particular 'scenarios.'
- *
- * We allocate from seperate sub-maps of kmem_map, thus imposing
- * an ultimate upper-limit on the number of allocatable clusters
- * and mbufs and also, since the clusters all come from a
- * virtually contiguous region, we can keep reference counters
- * for them and "allocate" them purely by indexing into a
- * dense refcount vector.
- *
- * We call out to protocol drain routines (which can be hooked
- * into us) when we're low on space.
- *
- * The mbuf allocator keeps all objects that it allocates in mb_buckets.
- * The buckets keep a number of objects (an object can be an mbuf or an
- * mbuf cluster) and facilitate moving larger sets of contiguous objects
- * from the per-CPU caches to the global cache. The buckets also have
- * the added advantage that objects, when migrated from cache to cache,
- * are migrated in chunks that keep contiguous objects together,
- * minimizing TLB pollution.
- *
- * The buckets are kept on singly-linked lists called "containers." A container
- * is protected by a mutex in order to ensure consistency. The mutex
- * itself is allocated separately and attached to the container at boot time,
- * thus allowing for certain containers to share the same lock. Per-CPU
- * containers for mbufs and mbuf clusters all share the same per-CPU
- * lock whereas the global cache containers for these objects share one
- * global lock.
- */
-struct mb_bucket {
- SLIST_ENTRY(mb_bucket) mb_blist;
- int mb_owner;
- int mb_numfree;
- void *mb_free[0];
-};
-
-struct mb_container {
- SLIST_HEAD(mc_buckethd, mb_bucket) mc_bhead;
- struct mtx *mc_lock;
- int mc_numowner;
- u_int mc_starved;
- long *mc_types;
- u_long *mc_objcount;
- u_long *mc_numbucks;
-};
-
-struct mb_gen_list {
- struct mb_container mb_cont;
- struct cv mgl_mstarved;
-};
-
-struct mb_pcpu_list {
- struct mb_container mb_cont;
-};
-
-/*
- * Boot-time configurable object counts that will determine the maximum
- * number of permitted objects in the mbuf and mcluster cases. In the
- * ext counter (nmbcnt) case, it's just an indicator serving to scale
- * kmem_map size properly - in other words, we may be allowed to allocate
- * more than nmbcnt counters, whereas we will never be allowed to allocate
- * more than nmbufs mbufs or nmbclusters mclusters.
- * As for nsfbufs, it is used to indicate how many sendfile(2) buffers will be
- * allocatable by the sfbuf allocator (found in uipc_syscalls.c)
- */
-#ifndef NMBCLUSTERS
-#define NMBCLUSTERS (1024 + maxusers * 64)
-#endif
-#ifndef NMBUFS
-#define NMBUFS (nmbclusters * 2)
-#endif
-#ifndef NSFBUFS
-#define NSFBUFS (512 + maxusers * 16)
-#endif
-#ifndef NMBCNTS
-#define NMBCNTS (nmbclusters + nsfbufs)
-#endif
-int nmbufs;
-int nmbclusters;
-int nmbcnt;
-int nsfbufs;
-int nsfbufspeak;
-int nsfbufsused;
-
-/*
- * Sizes of objects per bucket. There are this size's worth of mbufs
- * or clusters in each bucket. Please keep these a power-of-2.
- */
-#define MBUF_BUCK_SZ (PAGE_SIZE * 2)
-#define CLUST_BUCK_SZ (PAGE_SIZE * 4)
-
-/*
- * Perform sanity checks of tunables declared above.
- */
-static void
-tunable_mbinit(void *dummy)
-{
-
- /*
- * This has to be done before VM init.
- */
- nmbclusters = NMBCLUSTERS;
- TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters);
- nmbufs = NMBUFS;
- TUNABLE_INT_FETCH("kern.ipc.nmbufs", &nmbufs);
- nsfbufs = NSFBUFS;
- TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
- nmbcnt = NMBCNTS;
- TUNABLE_INT_FETCH("kern.ipc.nmbcnt", &nmbcnt);
- /* Sanity checks */
- if (nmbufs < nmbclusters * 2)
- nmbufs = nmbclusters * 2;
- if (nmbcnt < nmbclusters + nsfbufs)
- nmbcnt = nmbclusters + nsfbufs;
-}
-SYSINIT(tunable_mbinit, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_mbinit, NULL);
-
-/*
- * The freelist structures and mutex locks. The number statically declared
- * here depends on the number of CPUs.
- *
- * We set up in such a way that all the objects (mbufs, clusters)
- * share the same mutex lock. It has been established that we do not benefit
- * from different locks for different objects, so we use the same lock,
- * regardless of object type. This also allows us to do optimised
- * multi-object allocations without dropping the lock in between.
- */
-struct mb_lstmngr {
- struct mb_gen_list *ml_genlist;
- struct mb_pcpu_list *ml_cntlst[NCPU];
- struct mb_bucket **ml_btable;
- vm_map_t ml_map;
- vm_offset_t ml_mapbase;
- vm_offset_t ml_maptop;
- int ml_mapfull;
- u_int ml_objsize;
- u_int ml_objbucks;
- u_int *ml_wmhigh;
- u_int *ml_wmlow;
-};
-static struct mb_lstmngr mb_list_mbuf, mb_list_clust;
-static struct mtx mbuf_gen, mbuf_pcpu[NCPU];
-static u_int *cl_refcntmap;
-
-/*
- * Local macros for internal allocator structure manipulations.
- */
-#ifdef SMP
-#define MB_GET_PCPU_LIST(mb_lst) (mb_lst)->ml_cntlst[PCPU_GET(cpuid)]
-#else
-#define MB_GET_PCPU_LIST(mb_lst) (mb_lst)->ml_cntlst[0]
-#endif
-
-#define MB_GET_GEN_LIST(mb_lst) (mb_lst)->ml_genlist
-
-#define MB_LOCK_CONT(mb_cnt) mtx_lock((mb_cnt)->mb_cont.mc_lock)
-
-#define MB_UNLOCK_CONT(mb_cnt) mtx_unlock((mb_cnt)->mb_cont.mc_lock)
-
-#define MB_GET_PCPU_LIST_NUM(mb_lst, num) \
- (mb_lst)->ml_cntlst[(num)]
-
-#define MB_BUCKET_INDX(mb_obj, mb_lst) \
- (int)(((caddr_t)(mb_obj) - (caddr_t)(mb_lst)->ml_mapbase) / \
- ((mb_lst)->ml_objbucks * (mb_lst)->ml_objsize))
-
-#define MB_GET_OBJECT(mb_objp, mb_bckt, mb_lst) \
-{ \
- struct mc_buckethd *_mchd = &((mb_lst)->mb_cont.mc_bhead); \
- \
- (mb_bckt)->mb_numfree--; \
- (mb_objp) = (mb_bckt)->mb_free[((mb_bckt)->mb_numfree)]; \
- (*((mb_lst)->mb_cont.mc_objcount))--; \
- if ((mb_bckt)->mb_numfree == 0) { \
- SLIST_REMOVE_HEAD(_mchd, mb_blist); \
- SLIST_NEXT((mb_bckt), mb_blist) = NULL; \
- (mb_bckt)->mb_owner |= MB_BUCKET_FREE; \
- } \
-}
-
-#define MB_PUT_OBJECT(mb_objp, mb_bckt, mb_lst) \
- (mb_bckt)->mb_free[((mb_bckt)->mb_numfree)] = (mb_objp); \
- (mb_bckt)->mb_numfree++; \
- (*((mb_lst)->mb_cont.mc_objcount))++;
-
-#define MB_MBTYPES_INC(mb_cnt, mb_type, mb_num) \
- if ((mb_type) != MT_NOTMBUF) \
- (*((mb_cnt)->mb_cont.mc_types + (mb_type))) += (mb_num)
-
-#define MB_MBTYPES_DEC(mb_cnt, mb_type, mb_num) \
- if ((mb_type) != MT_NOTMBUF) \
- (*((mb_cnt)->mb_cont.mc_types + (mb_type))) -= (mb_num)
-
-/*
- * Ownership of buckets/containers is represented by integers. The PCPU
- * lists range from 0 to NCPU-1. We need a free numerical id for the general
- * list (we use NCPU). We also need a non-conflicting free bit to indicate
- * that the bucket is free and removed from a container, while not losing
- * the bucket's originating container id. We use the highest bit
- * for the free marker.
- */
-#define MB_GENLIST_OWNER (NCPU)
-#define MB_BUCKET_FREE (1 << (sizeof(int) * 8 - 1))
-
-/* Statistics structures for allocator (per-CPU and general). */
-static struct mbpstat mb_statpcpu[NCPU + 1];
-struct mbstat mbstat;
-
-/* Sleep time for wait code (in ticks). */
-static int mbuf_wait = 64;
-
-static u_int mbuf_hiwm = 512; /* High wm on # of mbufs per cache */
-static u_int mbuf_lowm = 128; /* Low wm on # of mbufs per cache */
-static u_int clust_hiwm = 128; /* High wm on # of clusters per cache */
-static u_int clust_lowm = 16; /* Low wm on # of clusters per cache */
-
-/*
- * Objects exported by sysctl(8).
- */
-SYSCTL_DECL(_kern_ipc);
-SYSCTL_INT(_kern_ipc, OID_AUTO, nmbclusters, CTLFLAG_RDTUN, &nmbclusters, 0,
- "Maximum number of mbuf clusters available");
-SYSCTL_INT(_kern_ipc, OID_AUTO, nmbufs, CTLFLAG_RDTUN, &nmbufs, 0,
- "Maximum number of mbufs available");
-SYSCTL_INT(_kern_ipc, OID_AUTO, nmbcnt, CTLFLAG_RDTUN, &nmbcnt, 0,
- "Number used to scale kmem_map to ensure sufficient space for counters");
-SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
- "Maximum number of sendfile(2) sf_bufs available");
-SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
- "Number of sendfile(2) sf_bufs at peak usage");
-SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
- "Number of sendfile(2) sf_bufs in use");
-SYSCTL_INT(_kern_ipc, OID_AUTO, mbuf_wait, CTLFLAG_RW, &mbuf_wait, 0,
- "Sleep time of mbuf subsystem wait allocations during exhaustion");
-SYSCTL_UINT(_kern_ipc, OID_AUTO, mbuf_hiwm, CTLFLAG_RW, &mbuf_hiwm, 0,
- "Upper limit of number of mbufs allowed in each cache");
-SYSCTL_UINT(_kern_ipc, OID_AUTO, mbuf_lowm, CTLFLAG_RD, &mbuf_lowm, 0,
- "Lower limit of number of mbufs allowed in each cache");
-SYSCTL_UINT(_kern_ipc, OID_AUTO, clust_hiwm, CTLFLAG_RW, &clust_hiwm, 0,
- "Upper limit of number of mbuf clusters allowed in each cache");
-SYSCTL_UINT(_kern_ipc, OID_AUTO, clust_lowm, CTLFLAG_RD, &clust_lowm, 0,
- "Lower limit of number of mbuf clusters allowed in each cache");
-SYSCTL_STRUCT(_kern_ipc, OID_AUTO, mbstat, CTLFLAG_RD, &mbstat, mbstat,
- "Mbuf general information and statistics");
-SYSCTL_OPAQUE(_kern_ipc, OID_AUTO, mb_statpcpu, CTLFLAG_RD, mb_statpcpu,
- sizeof(mb_statpcpu), "S,", "Mbuf allocator per CPU statistics");
-
-/*
- * Prototypes of local allocator routines.
- */
-static void *mb_alloc_wait(struct mb_lstmngr *, short);
-static struct mb_bucket *mb_pop_cont(struct mb_lstmngr *, int,
- struct mb_pcpu_list *);
-static void mb_reclaim(void);
-static void mbuf_init(void *);
-
-/*
- * Initial allocation numbers. Each parameter represents the number of buckets
- * of each object that will be placed initially in each PCPU container for
- * said object.
- */
-#define NMB_MBUF_INIT 2
-#define NMB_CLUST_INIT 8
-
-/*
- * Internal flags that allow for cache locks to remain "persistent" across
- * allocation and free calls. They may be used in combination.
- */
-#define MBP_PERSIST 0x1 /* Return with lock still held. */
-#define MBP_PERSISTENT 0x2 /* Cache lock is already held coming in. */
-
-/*
- * Initialize the mbuf subsystem.
- *
- * We sub-divide the kmem_map into several submaps; this way, we don't have
- * to worry about artificially limiting the number of mbuf or mbuf cluster
- * allocations, due to fear of one type of allocation "stealing" address
- * space initially reserved for another.
- *
- * Set up both the general containers and all the PCPU containers. Populate
- * the PCPU containers with initial numbers.
- */
-MALLOC_DEFINE(M_MBUF, "mbufmgr", "mbuf subsystem management structures");
-SYSINIT(mbuf, SI_SUB_MBUF, SI_ORDER_FIRST, mbuf_init, NULL)
-static void
-mbuf_init(void *dummy)
-{
- struct mb_pcpu_list *pcpu_cnt;
- vm_size_t mb_map_size;
- int i, j;
-
- /*
- * Set up all the submaps, for each type of object that we deal
- * with in this allocator.
- */
- mb_map_size = (vm_size_t)(nmbufs * MSIZE);
- mb_map_size = rounddown(mb_map_size, MBUF_BUCK_SZ);
- mb_list_mbuf.ml_btable = malloc((unsigned long)mb_map_size /
- MBUF_BUCK_SZ * sizeof(struct mb_bucket *), M_MBUF, M_NOWAIT);
- if (mb_list_mbuf.ml_btable == NULL)
- goto bad;
- mb_list_mbuf.ml_map = kmem_suballoc(kmem_map,&(mb_list_mbuf.ml_mapbase),
- &(mb_list_mbuf.ml_maptop), mb_map_size);
- mb_list_mbuf.ml_map->system_map = 1;
- mb_list_mbuf.ml_mapfull = 0;
- mb_list_mbuf.ml_objsize = MSIZE;
- mb_list_mbuf.ml_objbucks = MBUF_BUCK_SZ / mb_list_mbuf.ml_objsize;
- mb_list_mbuf.ml_wmhigh = &mbuf_hiwm;
- mb_list_mbuf.ml_wmlow = &mbuf_lowm;
-
- mb_map_size = (vm_size_t)(nmbclusters * MCLBYTES);
- mb_map_size = rounddown(mb_map_size, CLUST_BUCK_SZ);
- mb_list_clust.ml_btable = malloc((unsigned long)mb_map_size /
- CLUST_BUCK_SZ * sizeof(struct mb_bucket *), M_MBUF, M_NOWAIT);
- if (mb_list_clust.ml_btable == NULL)
- goto bad;
- mb_list_clust.ml_map = kmem_suballoc(kmem_map,
- &(mb_list_clust.ml_mapbase), &(mb_list_clust.ml_maptop),
- mb_map_size);
- mb_list_clust.ml_map->system_map = 1;
- mb_list_clust.ml_mapfull = 0;
- mb_list_clust.ml_objsize = MCLBYTES;
- mb_list_clust.ml_objbucks = CLUST_BUCK_SZ / mb_list_clust.ml_objsize;
- mb_list_clust.ml_wmhigh = &clust_hiwm;
- mb_list_clust.ml_wmlow = &clust_lowm;
-
- /*
- * Allocate required general (global) containers for each object type.
- */
- mb_list_mbuf.ml_genlist = malloc(sizeof(struct mb_gen_list), M_MBUF,
- M_NOWAIT);
- mb_list_clust.ml_genlist = malloc(sizeof(struct mb_gen_list), M_MBUF,
- M_NOWAIT);
- if ((mb_list_mbuf.ml_genlist == NULL) ||
- (mb_list_clust.ml_genlist == NULL))
- goto bad;
-
- /*
- * Initialize condition variables and general container mutex locks.
- */
- mtx_init(&mbuf_gen, "mbuf subsystem general lists lock", NULL, MTX_DEF);
- cv_init(&(mb_list_mbuf.ml_genlist->mgl_mstarved), "mbuf pool starved");
- cv_init(&(mb_list_clust.ml_genlist->mgl_mstarved),
- "mcluster pool starved");
- mb_list_mbuf.ml_genlist->mb_cont.mc_lock =
- mb_list_clust.ml_genlist->mb_cont.mc_lock = &mbuf_gen;
-
- /*
- * Set up the general containers for each object.
- */
- mb_list_mbuf.ml_genlist->mb_cont.mc_numowner =
- mb_list_clust.ml_genlist->mb_cont.mc_numowner = MB_GENLIST_OWNER;
- mb_list_mbuf.ml_genlist->mb_cont.mc_starved =
- mb_list_clust.ml_genlist->mb_cont.mc_starved = 0;
- mb_list_mbuf.ml_genlist->mb_cont.mc_objcount =
- &(mb_statpcpu[MB_GENLIST_OWNER].mb_mbfree);
- mb_list_clust.ml_genlist->mb_cont.mc_objcount =
- &(mb_statpcpu[MB_GENLIST_OWNER].mb_clfree);
- mb_list_mbuf.ml_genlist->mb_cont.mc_numbucks =
- &(mb_statpcpu[MB_GENLIST_OWNER].mb_mbbucks);
- mb_list_clust.ml_genlist->mb_cont.mc_numbucks =
- &(mb_statpcpu[MB_GENLIST_OWNER].mb_clbucks);
- mb_list_mbuf.ml_genlist->mb_cont.mc_types =
- &(mb_statpcpu[MB_GENLIST_OWNER].mb_mbtypes[0]);
- mb_list_clust.ml_genlist->mb_cont.mc_types = NULL;
- SLIST_INIT(&(mb_list_mbuf.ml_genlist->mb_cont.mc_bhead));
- SLIST_INIT(&(mb_list_clust.ml_genlist->mb_cont.mc_bhead));
-
- /*
- * Allocate all the required counters for clusters. This makes
- * cluster allocations/deallocations much faster.
- */
- cl_refcntmap = malloc(nmbclusters * sizeof(u_int), M_MBUF, M_NOWAIT);
- if (cl_refcntmap == NULL)
- goto bad;
-
- /*
- * Initialize general mbuf statistics.
- */
- mbstat.m_msize = mb_list_mbuf.ml_objsize;
- mbstat.m_mclbytes = mb_list_clust.ml_objsize;
- mbstat.m_minclsize = MINCLSIZE;
- mbstat.m_mlen = MLEN;
- mbstat.m_mhlen = MHLEN;
- mbstat.m_numtypes = MT_NTYPES;
- mbstat.m_mbperbuck = mb_list_mbuf.ml_objbucks;
- mbstat.m_clperbuck = mb_list_clust.ml_objbucks;
-
- /*
- * Allocate and initialize PCPU containers.
- */
- for (i = 0; i < NCPU; i++) {
- if (CPU_ABSENT(i)) {
- mb_statpcpu[i].mb_active = 0;
- continue;
- }
-
- mb_list_mbuf.ml_cntlst[i] = malloc(sizeof(struct mb_pcpu_list),
- M_MBUF, M_NOWAIT);
- mb_list_clust.ml_cntlst[i] = malloc(sizeof(struct mb_pcpu_list),
- M_MBUF, M_NOWAIT);
- if ((mb_list_mbuf.ml_cntlst[i] == NULL) ||
- (mb_list_clust.ml_cntlst[i] == NULL))
- goto bad;
-
- mtx_init(&mbuf_pcpu[i], "mbuf PCPU list lock", NULL, MTX_DEF);
- mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_lock =
- mb_list_clust.ml_cntlst[i]->mb_cont.mc_lock = &mbuf_pcpu[i];
-
- mb_statpcpu[i].mb_active = 1;
- mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_numowner =
- mb_list_clust.ml_cntlst[i]->mb_cont.mc_numowner = i;
- mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_starved =
- mb_list_clust.ml_cntlst[i]->mb_cont.mc_starved = 0;
- mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_objcount =
- &(mb_statpcpu[i].mb_mbfree);
- mb_list_clust.ml_cntlst[i]->mb_cont.mc_objcount =
- &(mb_statpcpu[i].mb_clfree);
- mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_numbucks =
- &(mb_statpcpu[i].mb_mbbucks);
- mb_list_clust.ml_cntlst[i]->mb_cont.mc_numbucks =
- &(mb_statpcpu[i].mb_clbucks);
- mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_types =
- &(mb_statpcpu[i].mb_mbtypes[0]);
- mb_list_clust.ml_cntlst[i]->mb_cont.mc_types = NULL;
-
- SLIST_INIT(&(mb_list_mbuf.ml_cntlst[i]->mb_cont.mc_bhead));
- SLIST_INIT(&(mb_list_clust.ml_cntlst[i]->mb_cont.mc_bhead));
-
- /*
- * Perform initial allocations.
- */
- pcpu_cnt = MB_GET_PCPU_LIST_NUM(&mb_list_mbuf, i);
- MB_LOCK_CONT(pcpu_cnt);
- for (j = 0; j < NMB_MBUF_INIT; j++) {
- if (mb_pop_cont(&mb_list_mbuf, M_DONTWAIT, pcpu_cnt)
- == NULL)
- goto bad;
- }
- MB_UNLOCK_CONT(pcpu_cnt);
-
- pcpu_cnt = MB_GET_PCPU_LIST_NUM(&mb_list_clust, i);
- MB_LOCK_CONT(pcpu_cnt);
- for (j = 0; j < NMB_CLUST_INIT; j++) {
- if (mb_pop_cont(&mb_list_clust, M_DONTWAIT, pcpu_cnt)
- == NULL)
- goto bad;
- }
- MB_UNLOCK_CONT(pcpu_cnt);
- }
-
- return;
-bad:
- panic("mbuf_init(): failed to initialize mbuf subsystem!");
-}
-
-/*
- * Populate a given mbuf PCPU container with a bucket full of fresh new
- * buffers. Return a pointer to the new bucket (already in the container if
- * successful), or return NULL on failure.
- *
- * LOCKING NOTES:
- * PCPU container lock must be held when this is called.
- * The lock is dropped here so that we can cleanly call the underlying VM
- * code. If we fail, we return with no locks held. If we succeed (i.e., return
- * non-NULL), we return with the PCPU lock held, ready for allocation from
- * the returned bucket.
- */
-static struct mb_bucket *
-mb_pop_cont(struct mb_lstmngr *mb_list, int how, struct mb_pcpu_list *cnt_lst)
-{
- struct mb_bucket *bucket;
- caddr_t p;
- int i;
-
- MB_UNLOCK_CONT(cnt_lst);
- /*
- * If our object's (finite) map is starved now (i.e., no more address
- * space), bail out now.
- */
- if (mb_list->ml_mapfull)
- return (NULL);
-
- bucket = malloc(sizeof(struct mb_bucket) +
- mb_list->ml_objbucks * sizeof(void *), M_MBUF, MBTOM(how));
- if (bucket == NULL)
- return (NULL);
-
- p = (caddr_t)kmem_malloc(mb_list->ml_map, mb_list->ml_objsize *
- mb_list->ml_objbucks, MBTOM(how));
- if (p == NULL) {
- free(bucket, M_MBUF);
- if (how == M_TRYWAIT)
- mb_list->ml_mapfull = 1;
- return (NULL);
- }
-
- bucket->mb_numfree = 0;
- mb_list->ml_btable[MB_BUCKET_INDX(p, mb_list)] = bucket;
- for (i = 0; i < mb_list->ml_objbucks; i++) {
- bucket->mb_free[i] = p;
- bucket->mb_numfree++;
- p += mb_list->ml_objsize;
- }
-
- MB_LOCK_CONT(cnt_lst);
- bucket->mb_owner = cnt_lst->mb_cont.mc_numowner;
- SLIST_INSERT_HEAD(&(cnt_lst->mb_cont.mc_bhead), bucket, mb_blist);
- (*(cnt_lst->mb_cont.mc_numbucks))++;
- *(cnt_lst->mb_cont.mc_objcount) += bucket->mb_numfree;
-
- return (bucket);
-}
-
-/*
- * Allocate a network buffer.
- * The general case is very easy. Complications only arise if our PCPU
- * container is empty. Things get worse if the PCPU container is empty,
- * the general container is empty, and we've run out of address space
- * in our map; then we try to block if we're willing to (M_TRYWAIT).
- */
-static
-void *
-mb_alloc(struct mb_lstmngr *mb_list, int how, short type, short persist,
- int *pers_list)
-{
- static int last_report;
- struct mb_pcpu_list *cnt_lst;
- struct mb_bucket *bucket;
- void *m;
-
-#ifdef INVARIANTS
- int flags;
-
- flags = how & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT);
- if (flags != M_DONTWAIT && flags != M_TRYWAIT) {
- static struct timeval lasterr;
- static int curerr;
- if (ppsratecheck(&lasterr, &curerr, 1)) {
- printf("Bad mbuf alloc flags: %x\n", flags);
- backtrace();
- how = M_TRYWAIT;
- }
- }
-#endif
-
- m = NULL;
- if ((persist & MBP_PERSISTENT) != 0) {
- /*
- * If we're a "persistent" call, then the per-CPU #(pers_list)
- * cache lock is already held, and we just need to refer to
- * the correct cache descriptor.
- */
- cnt_lst = MB_GET_PCPU_LIST_NUM(mb_list, *pers_list);
- } else {
- cnt_lst = MB_GET_PCPU_LIST(mb_list);
- MB_LOCK_CONT(cnt_lst);
- }
-
- if ((bucket = SLIST_FIRST(&(cnt_lst->mb_cont.mc_bhead))) != NULL) {
- /*
- * This is the easy allocation case. We just grab an object
- * from a bucket in the PCPU container. At worst, we
- * have just emptied the bucket and so we remove it
- * from the container.
- */
- MB_GET_OBJECT(m, bucket, cnt_lst);
- MB_MBTYPES_INC(cnt_lst, type, 1);
-
- /* If asked to persist, do not drop the lock. */
- if ((persist & MBP_PERSIST) == 0)
- MB_UNLOCK_CONT(cnt_lst);
- else
- *pers_list = cnt_lst->mb_cont.mc_numowner;
- } else {
- struct mb_gen_list *gen_list;
-
- /*
- * This is the less-common more difficult case. We must
- * first verify if the general list has anything for us
- * and if that also fails, we must allocate a page from
- * the map and create a new bucket to place in our PCPU
- * container (already locked). If the map is starved then
- * we're really in for trouble, as we have to wait on
- * the general container's condition variable.
- */
- gen_list = MB_GET_GEN_LIST(mb_list);
- MB_LOCK_CONT(gen_list);
-
- if ((bucket = SLIST_FIRST(&(gen_list->mb_cont.mc_bhead)))
- != NULL) {
- /*
- * Give ownership of the bucket to our CPU's
- * container, but only actually put the bucket
- * in the container if it doesn't become free
- * upon removing an mbuf from it.
- */
- SLIST_REMOVE_HEAD(&(gen_list->mb_cont.mc_bhead),
- mb_blist);
- bucket->mb_owner = cnt_lst->mb_cont.mc_numowner;
- (*(gen_list->mb_cont.mc_numbucks))--;
- (*(cnt_lst->mb_cont.mc_numbucks))++;
- *(gen_list->mb_cont.mc_objcount) -= bucket->mb_numfree;
- bucket->mb_numfree--;
- m = bucket->mb_free[(bucket->mb_numfree)];
- if (bucket->mb_numfree == 0) {
- SLIST_NEXT(bucket, mb_blist) = NULL;
- bucket->mb_owner |= MB_BUCKET_FREE;
- } else {
- SLIST_INSERT_HEAD(&(cnt_lst->mb_cont.mc_bhead),
- bucket, mb_blist);
- *(cnt_lst->mb_cont.mc_objcount) +=
- bucket->mb_numfree;
- }
- MB_UNLOCK_CONT(gen_list);
- MB_MBTYPES_INC(cnt_lst, type, 1);
-
- /* If asked to persist, do not drop the lock. */
- if ((persist & MBP_PERSIST) == 0)
- MB_UNLOCK_CONT(cnt_lst);
- else
- *pers_list = cnt_lst->mb_cont.mc_numowner;
- } else {
- /*
- * We'll have to allocate a new page.
- */
- MB_UNLOCK_CONT(gen_list);
- bucket = mb_pop_cont(mb_list, how, cnt_lst);
- if (bucket != NULL) {
- MB_GET_OBJECT(m, bucket, cnt_lst);
- MB_MBTYPES_INC(cnt_lst, type, 1);
-
- /* If asked to persist, do not drop the lock. */
- if ((persist & MBP_PERSIST) == 0)
- MB_UNLOCK_CONT(cnt_lst);
- else
- *pers_list=cnt_lst->mb_cont.mc_numowner;
- } else {
- if (how == M_TRYWAIT) {
- /*
- * Absolute worst-case scenario.
- * We block if we're willing to, but
- * only after trying to steal from
- * other lists.
- */
- m = mb_alloc_wait(mb_list, type);
- } else {
- /* XXX: No consistency. */
- mbstat.m_drops++;
-
- if (ticks < last_report ||
- (ticks - last_report) >= hz) {
- last_report = ticks;
- printf(
-"All mbufs or mbuf clusters exhausted, please see tuning(7).\n");
- }
-
- }
- if (m != NULL && (persist & MBP_PERSIST) != 0) {
- cnt_lst = MB_GET_PCPU_LIST(mb_list);
- MB_LOCK_CONT(cnt_lst);
- *pers_list=cnt_lst->mb_cont.mc_numowner;
- }
- }
- }
- }
-
- return (m);
-}
-
-/*
- * This is the worst-case scenario called only if we're allocating with
- * M_TRYWAIT. We first drain all the protocols, then try to find an mbuf
- * by looking in every PCPU container. If we're still unsuccesful, we
- * try the general container one last time and possibly block on our
- * starved cv.
- */
-static void *
-mb_alloc_wait(struct mb_lstmngr *mb_list, short type)
-{
- struct mb_pcpu_list *cnt_lst;
- struct mb_gen_list *gen_list;
- struct mb_bucket *bucket;
- void *m;
- int i, cv_ret;
-
- /*
- * Try to reclaim mbuf-related objects (mbufs, clusters).
- */
- mb_reclaim();
-
- /*
- * Cycle all the PCPU containers. Increment starved counts if found
- * empty.
- */
- for (i = 0; i < NCPU; i++) {
- if (CPU_ABSENT(i))
- continue;
- cnt_lst = MB_GET_PCPU_LIST_NUM(mb_list, i);
- MB_LOCK_CONT(cnt_lst);
-
- /*
- * If container is non-empty, get a single object from it.
- * If empty, increment starved count.
- */
- if ((bucket = SLIST_FIRST(&(cnt_lst->mb_cont.mc_bhead))) !=
- NULL) {
- MB_GET_OBJECT(m, bucket, cnt_lst);
- MB_MBTYPES_INC(cnt_lst, type, 1);
- MB_UNLOCK_CONT(cnt_lst);
- mbstat.m_wait++; /* XXX: No consistency. */
- return (m);
- } else
- cnt_lst->mb_cont.mc_starved++;
-
- MB_UNLOCK_CONT(cnt_lst);
- }
-
- /*
- * We're still here, so that means it's time to get the general
- * container lock, check it one more time (now that mb_reclaim()
- * has been called) and if we still get nothing, block on the cv.
- */
- gen_list = MB_GET_GEN_LIST(mb_list);
- MB_LOCK_CONT(gen_list);
- if ((bucket = SLIST_FIRST(&(gen_list->mb_cont.mc_bhead))) != NULL) {
- MB_GET_OBJECT(m, bucket, gen_list);
- MB_MBTYPES_INC(gen_list, type, 1);
- MB_UNLOCK_CONT(gen_list);
- mbstat.m_wait++; /* XXX: No consistency. */
- return (m);
- }
-
- gen_list->mb_cont.mc_starved++;
- cv_ret = cv_timedwait(&(gen_list->mgl_mstarved),
- gen_list->mb_cont.mc_lock, mbuf_wait);
- gen_list->mb_cont.mc_starved--;
-
- if ((cv_ret == 0) &&
- ((bucket = SLIST_FIRST(&(gen_list->mb_cont.mc_bhead))) != NULL)) {
- MB_GET_OBJECT(m, bucket, gen_list);
- MB_MBTYPES_INC(gen_list, type, 1);
- mbstat.m_wait++; /* XXX: No consistency. */
- } else {
- mbstat.m_drops++; /* XXX: No consistency. */
- m = NULL;
- }
-
- MB_UNLOCK_CONT(gen_list);
-
- return (m);
-}
-
-/*-
- * Free an object to its rightful container.
- * In the very general case, this operation is really very easy.
- * Complications arise primarily if:
- * (a) We've hit the high limit on number of free objects allowed in
- * our PCPU container.
- * (b) We're in a critical situation where our container has been
- * marked 'starved' and we need to issue wakeups on the starved
- * condition variable.
- * (c) Minor (odd) cases: our bucket has migrated while we were
- * waiting for the lock; our bucket is in the general container;
- * our bucket is empty.
- */
-static
-void
-mb_free(struct mb_lstmngr *mb_list, void *m, short type, short persist,
- int *pers_list)
-{
- struct mb_pcpu_list *cnt_lst;
- struct mb_gen_list *gen_list;
- struct mb_bucket *bucket;
- u_int owner;
-
- bucket = mb_list->ml_btable[MB_BUCKET_INDX(m, mb_list)];
-
- /*
- * Make sure that if after we lock the bucket's present container the
- * bucket has migrated, that we drop the lock and get the new one.
- */
-retry_lock:
- owner = bucket->mb_owner & ~MB_BUCKET_FREE;
- switch (owner) {
- case MB_GENLIST_OWNER:
- gen_list = MB_GET_GEN_LIST(mb_list);
- if (((persist & MBP_PERSISTENT) != 0) && (*pers_list >= 0)) {
- if (*pers_list != MB_GENLIST_OWNER) {
- cnt_lst = MB_GET_PCPU_LIST_NUM(mb_list,
- *pers_list);
- MB_UNLOCK_CONT(cnt_lst);
- MB_LOCK_CONT(gen_list);
- }
- } else {
- MB_LOCK_CONT(gen_list);
- }
- if (owner != (bucket->mb_owner & ~MB_BUCKET_FREE)) {
- MB_UNLOCK_CONT(gen_list);
- *pers_list = -1;
- goto retry_lock;
- }
-
- /*
- * If we're intended for the general container, this is
- * real easy: no migrating required. The only `bogon'
- * is that we're now contending with all the threads
- * dealing with the general list, but this is expected.
- */
- MB_PUT_OBJECT(m, bucket, gen_list);
- MB_MBTYPES_DEC(gen_list, type, 1);
- if (bucket->mb_owner & MB_BUCKET_FREE) {
- SLIST_INSERT_HEAD(&(gen_list->mb_cont.mc_bhead),
- bucket, mb_blist);
- bucket->mb_owner = MB_GENLIST_OWNER;
- }
- if (gen_list->mb_cont.mc_starved > 0)
- cv_signal(&(gen_list->mgl_mstarved));
- if ((persist & MBP_PERSIST) == 0)
- MB_UNLOCK_CONT(gen_list);
- else
- *pers_list = MB_GENLIST_OWNER;
- break;
-
- default:
- cnt_lst = MB_GET_PCPU_LIST_NUM(mb_list, owner);
- if (((persist & MBP_PERSISTENT) != 0) && (*pers_list >= 0)) {
- if (*pers_list == MB_GENLIST_OWNER) {
- gen_list = MB_GET_GEN_LIST(mb_list);
- MB_UNLOCK_CONT(gen_list);
- MB_LOCK_CONT(cnt_lst);
- } else {
- cnt_lst = MB_GET_PCPU_LIST_NUM(mb_list,
- *pers_list);
- owner = *pers_list;
- }
- } else {
- MB_LOCK_CONT(cnt_lst);
- }
- if (owner != (bucket->mb_owner & ~MB_BUCKET_FREE)) {
- MB_UNLOCK_CONT(cnt_lst);
- *pers_list = -1;
- goto retry_lock;
- }
-
- MB_PUT_OBJECT(m, bucket, cnt_lst);
- MB_MBTYPES_DEC(cnt_lst, type, 1);
- if ((*(cnt_lst->mb_cont.mc_objcount) > *(mb_list->ml_wmhigh)) ||
- (cnt_lst->mb_cont.mc_starved > 0)) {
- /*
- * We've hit the high limit of allowed numbers of mbufs
- * on this PCPU list or we've been flagged that we need
- * to transfer a bucket over to the general cache.
- * We must now migrate a bucket over to the general
- * container.
- */
- gen_list = MB_GET_GEN_LIST(mb_list);
- MB_LOCK_CONT(gen_list);
- if ((bucket->mb_owner & MB_BUCKET_FREE) == 0) {
- bucket =
- SLIST_FIRST(&(cnt_lst->mb_cont.mc_bhead));
- SLIST_REMOVE_HEAD(&(cnt_lst->mb_cont.mc_bhead),
- mb_blist);
- }
- SLIST_INSERT_HEAD(&(gen_list->mb_cont.mc_bhead),
- bucket, mb_blist);
- bucket->mb_owner = MB_GENLIST_OWNER;
- *(cnt_lst->mb_cont.mc_objcount) -= bucket->mb_numfree;
- *(gen_list->mb_cont.mc_objcount) += bucket->mb_numfree;
- (*(cnt_lst->mb_cont.mc_numbucks))--;
- (*(gen_list->mb_cont.mc_numbucks))++;
-
- /*
- * While we're at it, transfer some of the mbtypes
- * "count load" onto the general list's mbtypes
- * array, seeing as how we're moving the bucket
- * there now, meaning that the freeing of objects
- * there will now decrement the _general list's_
- * mbtypes counters, and no longer our PCPU list's
- * mbtypes counters. We do this for the type presently
- * being freed in an effort to keep the mbtypes
- * counters approximately balanced across all lists.
- */
- MB_MBTYPES_DEC(cnt_lst, type,
- mb_list->ml_objbucks - bucket->mb_numfree);
- MB_MBTYPES_INC(gen_list, type,
- mb_list->ml_objbucks - bucket->mb_numfree);
-
- if (cnt_lst->mb_cont.mc_starved > 0) {
- /*
- * Determine whether or not to keep
- * transferring buckets to the general list
- * or whether we've transferred enough already.
- * The thread that is blocked may end up waking
- * up in the meantime, but transferring an
- * extra bucket in a constrained situation
- * is not so bad, as we're likely to need
- * it soon anyway.
- */
- if (gen_list->mb_cont.mc_starved > 0) {
- cnt_lst->mb_cont.mc_starved--;
- cv_signal(&(gen_list->mgl_mstarved));
- } else
- cnt_lst->mb_cont.mc_starved = 0;
- }
- MB_UNLOCK_CONT(gen_list);
- if ((persist & MBP_PERSIST) == 0)
- MB_UNLOCK_CONT(cnt_lst);
- else
- *pers_list = owner;
- break;
- }
-
- if (bucket->mb_owner & MB_BUCKET_FREE) {
- SLIST_INSERT_HEAD(&(cnt_lst->mb_cont.mc_bhead),
- bucket, mb_blist);
- bucket->mb_owner = cnt_lst->mb_cont.mc_numowner;
- }
-
- if ((persist & MBP_PERSIST) == 0)
- MB_UNLOCK_CONT(cnt_lst);
- else
- *pers_list = owner;
- break;
- }
-}
-
-/*
- * Drain protocols in hopes to free up some resources.
- *
- * LOCKING NOTES:
- * No locks should be held when this is called. The drain routines have to
- * presently acquire some locks which raises the possibility of lock order
- * violation if we're holding any mutex if that mutex is acquired in reverse
- * order relative to one of the locks in the drain routines.
- */
-static void
-mb_reclaim(void)
-{
- struct domain *dp;
- struct protosw *pr;
-
- WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK | WARN_PANIC, NULL,
- "mb_reclaim()");
-
- mbstat.m_drain++; /* XXX: No consistency. */
-
- for (dp = domains; dp != NULL; dp = dp->dom_next)
- for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
- if (pr->pr_drain != NULL)
- (*pr->pr_drain)();
-}
-
-/******************************************************************************
- * Internal setup macros.
- */
-
-#define _mb_setup(m, type) do { \
- (m)->m_type = (type); \
- (m)->m_next = NULL; \
- (m)->m_nextpkt = NULL; \
- (m)->m_data = (m)->m_dat; \
- (m)->m_flags = 0; \
-} while (0)
-
-#define _mbhdr_setup(m, type) do { \
- (m)->m_type = (type); \
- (m)->m_next = NULL; \
- (m)->m_nextpkt = NULL; \
- (m)->m_data = (m)->m_pktdat; \
- (m)->m_flags = M_PKTHDR; \
- (m)->m_pkthdr.rcvif = NULL; \
- (m)->m_pkthdr.csum_flags = 0; \
- SLIST_INIT(&(m)->m_pkthdr.tags); \
-} while (0)
-
-#define _mcl_setup(m) do { \
- (m)->m_data = (m)->m_ext.ext_buf; \
- (m)->m_flags |= M_EXT; \
- (m)->m_ext.ext_free = NULL; \
- (m)->m_ext.ext_args = NULL; \
- (m)->m_ext.ext_size = MCLBYTES; \
- (m)->m_ext.ext_type = EXT_CLUSTER; \
-} while (0)
-
-#define _mext_init_ref(m, ref) do { \
- (m)->m_ext.ref_cnt = ((ref) == NULL) ? \
- malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)(ref); \
- if ((m)->m_ext.ref_cnt != NULL) { \
- *((m)->m_ext.ref_cnt) = 0; \
- MEXT_ADD_REF((m)); \
- } \
-} while (0)
-
-#define cl2ref(cl) \
- (((uintptr_t)(cl) - (uintptr_t)mb_list_clust.ml_mapbase) >> MCLSHIFT)
-
-#define _mext_dealloc_ref(m) \
- if ((m)->m_ext.ext_type != EXT_EXTREF) \
- free((m)->m_ext.ref_cnt, M_MBUF)
-
-/******************************************************************************
- * Internal routines.
- *
- * Because mb_alloc() and mb_free() are inlines (to keep the common
- * cases down to a maximum of one function call), below are a few
- * routines used only internally for the sole purpose of making certain
- * functions smaller.
- *
- * - _mext_free(): frees associated storage when the ref. count is
- * exactly one and we're freeing.
- *
- * - _mgetm_internal(): common "persistent-lock" routine that allocates
- * an mbuf and a cluster in one shot, but where the lock is already
- * held coming in (which is what makes it different from the exported
- * m_getcl()). The lock is dropped when done. This is used by m_getm()
- * and, therefore, is very m_getm()-specific.
- */
-static struct mbuf *_mgetm_internal(int, short, short, int);
-
-void
-_mext_free(struct mbuf *mb)
-{
-
- if (mb->m_ext.ext_type == EXT_CLUSTER) {
- mb_free(&mb_list_clust, (caddr_t)mb->m_ext.ext_buf, MT_NOTMBUF,
- 0, NULL);
- } else {
- (*(mb->m_ext.ext_free))(mb->m_ext.ext_buf, mb->m_ext.ext_args);
- _mext_dealloc_ref(mb);
- }
-}
-
-static struct mbuf *
-_mgetm_internal(int how, short type, short persist, int cchnum)
-{
- struct mbuf *mb;
-
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type, persist,&cchnum);
- if (mb == NULL)
- return NULL;
- _mb_setup(mb, type);
-
- if ((persist & MBP_PERSIST) != 0) {
- mb->m_ext.ext_buf = (caddr_t)mb_alloc(&mb_list_clust,
- how, MT_NOTMBUF, MBP_PERSISTENT, &cchnum);
- if (mb->m_ext.ext_buf == NULL) {
- (void)m_free(mb);
- mb = NULL;
- }
- _mcl_setup(mb);
- _mext_init_ref(mb, &cl_refcntmap[cl2ref(mb->m_ext.ext_buf)]);
- }
- return (mb);
-}
-
-/******************************************************************************
- * Exported buffer allocation and de-allocation routines.
- */
-
-/*
- * Allocate and return a single (normal) mbuf. NULL is returned on failure.
- *
- * Arguments:
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- * - type: the type of the mbuf being allocated.
- */
-struct mbuf *
-m_get(int how, short type)
-{
- struct mbuf *mb;
-
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type, 0, NULL);
- if (mb != NULL)
- _mb_setup(mb, type);
- return (mb);
-}
-
-/*
- * Allocate a given length worth of mbufs and/or clusters (whatever fits
- * best) and return a pointer to the top of the allocated chain. If an
- * existing mbuf chain is provided, then we will append the new chain
- * to the existing one but still return the top of the newly allocated
- * chain. NULL is returned on failure, in which case the [optional]
- * provided chain is left untouched, and any memory already allocated
- * is freed.
- *
- * Arguments:
- * - m: existing chain to which to append new chain (optional).
- * - len: total length of data to append, either in mbufs or clusters
- * (we allocate whatever combination yields the best fit).
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- * - type: the type of the mbuf being allocated.
- */
-struct mbuf *
-m_getm(struct mbuf *m, int len, int how, short type)
-{
- struct mbuf *mb, *top, *cur, *mtail;
- int num, rem, cchnum;
- short persist;
- int i;
-
- KASSERT(len >= 0, ("m_getm(): len is < 0"));
-
- /* If m != NULL, we will append to the end of that chain. */
- if (m != NULL)
- for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
- else
- mtail = NULL;
-
- /*
- * In the best-case scenario (which should be the common case
- * unless we're in a starvation situation), we will be able to
- * go through the allocation of all the desired mbufs and clusters
- * here without dropping our per-CPU cache lock in between.
- */
- num = len / MCLBYTES;
- rem = len % MCLBYTES;
- persist = 0;
- cchnum = -1;
- top = cur = NULL;
- for (i = 0; i < num; i++) {
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type,
- MBP_PERSIST | persist, &cchnum);
- if (mb == NULL)
- goto failed;
- _mb_setup(mb, type);
- mb->m_len = 0;
-
- persist = (i != (num - 1) || rem > 0) ? MBP_PERSIST : 0;
- mb->m_ext.ext_buf = (caddr_t)mb_alloc(&mb_list_clust,
- how, MT_NOTMBUF, persist | MBP_PERSISTENT, &cchnum);
- if (mb->m_ext.ext_buf == NULL) {
- (void)m_free(mb);
- goto failed;
- }
- _mcl_setup(mb);
- _mext_init_ref(mb, &cl_refcntmap[cl2ref(mb->m_ext.ext_buf)]);
- persist = MBP_PERSISTENT;
-
- if (cur == NULL)
- top = cur = mb;
- else
- cur = (cur->m_next = mb);
- }
- if (rem > 0) {
- if (cchnum >= 0) {
- persist = MBP_PERSISTENT;
- persist |= (rem > MINCLSIZE) ? MBP_PERSIST : 0;
- mb = _mgetm_internal(how, type, persist, cchnum);
- if (mb == NULL)
- goto failed;
- } else if (rem > MINCLSIZE) {
- mb = m_getcl(how, type, 0);
- } else {
- mb = m_get(how, type);
- }
- if (mb != NULL) {
- mb->m_len = 0;
- if (cur == NULL)
- top = mb;
- else
- cur->m_next = mb;
- } else
- goto failed;
- }
-
- if (mtail != NULL)
- mtail->m_next = top;
- return top;
-failed:
- if (top != NULL)
- m_freem(top);
- return NULL;
-}
-
-/*
- * Allocate and return a single M_PKTHDR mbuf. NULL is returned on failure.
- *
- * Arguments:
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- * - type: the type of the mbuf being allocated.
- */
-struct mbuf *
-m_gethdr(int how, short type)
-{
- struct mbuf *mb;
-
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type, 0, NULL);
- if (mb != NULL) {
- _mbhdr_setup(mb, type);
-#ifdef MAC
- if (mac_init_mbuf(mb, MBTOM(how)) != 0) {
- m_free(mb);
- return (NULL);
- }
-#endif
- }
- return (mb);
-}
-
-/*
- * Allocate and return a single (normal) pre-zero'd mbuf. NULL is
- * returned on failure.
- *
- * Arguments:
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- * - type: the type of the mbuf being allocated.
- */
-struct mbuf *
-m_get_clrd(int how, short type)
-{
- struct mbuf *mb;
-
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type, 0, NULL);
- if (mb != NULL) {
- _mb_setup(mb, type);
- bzero(mtod(mb, caddr_t), MLEN);
- }
- return (mb);
-}
-
-/*
- * Allocate and return a single M_PKTHDR pre-zero'd mbuf. NULL is
- * returned on failure.
- *
- * Arguments:
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- * - type: the type of the mbuf being allocated.
- */
-struct mbuf *
-m_gethdr_clrd(int how, short type)
-{
- struct mbuf *mb;
-
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type, 0, NULL);
- if (mb != NULL) {
- _mbhdr_setup(mb, type);
-#ifdef MAC
- if (mac_init_mbuf(mb, MBTOM(how)) != 0) {
- m_free(mb);
- return (NULL);
- }
-#endif
- bzero(mtod(mb, caddr_t), MHLEN);
- }
- return (mb);
-}
-
-/*
- * Free a single mbuf and any associated storage that it may have attached
- * to it. The associated storage may not be immediately freed if its
- * reference count is above 1. Returns the next mbuf in the chain following
- * the mbuf being freed.
- *
- * Arguments:
- * - mb: the mbuf to free.
- */
-struct mbuf *
-m_free(struct mbuf *mb)
-{
- struct mbuf *nb;
- int cchnum;
- short persist = 0;
-
-#ifdef INVARIANTS
- if (mb->m_flags & M_FREELIST)
- panic("m_free detected a mbuf double-free");
- mb->m_flags |= M_FREELIST;
-#endif
- if ((mb->m_flags & M_PKTHDR) != 0)
- m_tag_delete_chain(mb, NULL);
- nb = mb->m_next;
- if ((mb->m_flags & M_EXT) != 0) {
- MEXT_REM_REF(mb);
- if (atomic_cmpset_int(mb->m_ext.ref_cnt, 0, 1)) {
- if (mb->m_ext.ext_type == EXT_CLUSTER) {
- mb_free(&mb_list_clust,
- (caddr_t)mb->m_ext.ext_buf, MT_NOTMBUF,
- MBP_PERSIST, &cchnum);
- persist = MBP_PERSISTENT;
- } else {
- (*(mb->m_ext.ext_free))(mb->m_ext.ext_buf,
- mb->m_ext.ext_args);
- _mext_dealloc_ref(mb);
- persist = 0;
- }
- }
- }
- mb_free(&mb_list_mbuf, mb, mb->m_type, persist, &cchnum);
- return (nb);
-}
-
-/*
- * Free an entire chain of mbufs and associated external buffers, if
- * applicable. Right now, we only optimize a little so that the cache
- * lock may be held across a single mbuf+cluster free. Hopefully,
- * we'll eventually be holding the lock across more than merely two
- * consecutive frees but right now this is hard to implement because of
- * things like _mext_dealloc_ref (may do a free()) and atomic ops in the
- * loop.
- *
- * - mb: the mbuf chain to free.
- */
-void
-m_freem(struct mbuf *mb)
-{
-
- while (mb != NULL)
- mb = m_free(mb);
-}
-
-/*
- * Fetch an mbuf with a cluster attached to it. If one of the
- * allocations fails, the entire allocation fails. This routine is
- * the preferred way of fetching both the mbuf and cluster together,
- * as it avoids having to unlock/relock between allocations. Returns
- * NULL on failure.
- *
- * Arguments:
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- * - type: the type of the mbuf being allocated.
- * - flags: any flags to pass to the mbuf being allocated; if this includes
- * the M_PKTHDR bit, then the mbuf is configured as a M_PKTHDR mbuf.
- */
-struct mbuf *
-m_getcl(int how, short type, int flags)
-{
- struct mbuf *mb;
- int cchnum;
-
- mb = (struct mbuf *)mb_alloc(&mb_list_mbuf, how, type,
- MBP_PERSIST, &cchnum);
- if (mb == NULL)
- return NULL;
- mb->m_type = type;
- mb->m_next = NULL;
- mb->m_flags = flags;
- if ((flags & M_PKTHDR) != 0) {
- mb->m_nextpkt = NULL;
- mb->m_pkthdr.rcvif = NULL;
- mb->m_pkthdr.csum_flags = 0;
- SLIST_INIT(&mb->m_pkthdr.tags);
- }
-
- mb->m_ext.ext_buf = (caddr_t)mb_alloc(&mb_list_clust, how,
- MT_NOTMBUF, MBP_PERSISTENT, &cchnum);
- if (mb->m_ext.ext_buf == NULL) {
- (void)m_free(mb);
- mb = NULL;
- } else {
- _mcl_setup(mb);
- _mext_init_ref(mb, &cl_refcntmap[cl2ref(mb->m_ext.ext_buf)]);
-#ifdef MAC
- if (flags & M_PKTHDR) {
- if (mac_init_mbuf(mb, MBTOM(how)) != 0) {
- m_free(mb);
- return (NULL);
- }
- }
-#endif
- }
- return (mb);
-}
-
-/*
- * Fetch a single mbuf cluster and attach it to an existing mbuf. If
- * successfull, configures the provided mbuf to have mbuf->m_ext.ext_buf
- * pointing to the cluster, and sets the M_EXT bit in the mbuf's flags.
- * The M_EXT bit is not set on failure.
- *
- * Arguments:
- * - mb: the existing mbuf to which to attach the allocated cluster.
- * - how: M_TRYWAIT to try to block for kern.ipc.mbuf_wait number of ticks
- * if really starved for memory. M_DONTWAIT to never block.
- */
-void
-m_clget(struct mbuf *mb, int how)
-{
-
- mb->m_ext.ext_buf= (caddr_t)mb_alloc(&mb_list_clust,how,MT_NOTMBUF,
- 0, NULL);
- if (mb->m_ext.ext_buf != NULL) {
- _mcl_setup(mb);
- _mext_init_ref(mb, &cl_refcntmap[cl2ref(mb->m_ext.ext_buf)]);
- }
-}
-
-/*
- * Configure a provided mbuf to refer to the provided external storage
- * buffer and setup a reference count for said buffer. If the setting
- * up of the reference count fails, the M_EXT bit will not be set. If
- * successfull, the M_EXT bit is set in the mbuf's flags.
- *
- * Arguments:
- * - mb: the existing mbuf to which to attach the provided buffer.
- * - buf: the address of the provided external storage buffer.
- * - size: the size of the provided buffer.
- * - freef: a pointer to a routine that is responsible for freeing the
- * provided external storage buffer.
- * - args: a pointer to an argument structure (of any type) to be passed
- * to the provided freef routine (may be NULL).
- * - flags: any other flags to be passed to the provided mbuf.
- * - type: the type that the external storage buffer should be labeled with.
- */
-void
-m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
- void (*freef)(void *, void *), void *args, int flags, int type)
-{
- u_int *ref_cnt = NULL;
-
- if (type == EXT_CLUSTER)
- ref_cnt = &cl_refcntmap[cl2ref(mb->m_ext.ext_buf)];
- else if (type == EXT_EXTREF)
- ref_cnt = mb->m_ext.ref_cnt;
- _mext_init_ref(mb, ref_cnt);
- if (mb->m_ext.ref_cnt != NULL) {
- mb->m_flags |= (M_EXT | flags);
- mb->m_ext.ext_buf = buf;
- mb->m_data = mb->m_ext.ext_buf;
- mb->m_ext.ext_size = size;
- mb->m_ext.ext_free = freef;
- mb->m_ext.ext_args = args;
- mb->m_ext.ext_type = type;
- }
-}
-
-/*
- * Change type of provided mbuf. This is a relatively expensive operation
- * (due to the cost of statistics manipulations) and should be avoided, where
- * possible.
- *
- * Arguments:
- * - mb: the provided mbuf for which the type needs to be changed.
- * - new_type: the new type to change the mbuf to.
- */
-void
-m_chtype(struct mbuf *mb, short new_type)
-{
- struct mb_gen_list *gen_list;
-
- gen_list = MB_GET_GEN_LIST(&mb_list_mbuf);
- MB_LOCK_CONT(gen_list);
- MB_MBTYPES_DEC(gen_list, mb->m_type, 1);
- MB_MBTYPES_INC(gen_list, new_type, 1);
- MB_UNLOCK_CONT(gen_list);
- mb->m_type = new_type;
-}
diff --git a/sys/kern/uipc_mbuf.c b/sys/kern/uipc_mbuf.c
index 5815fae..e14aba1 100644
--- a/sys/kern/uipc_mbuf.c
+++ b/sys/kern/uipc_mbuf.c
@@ -86,6 +86,161 @@ SYSCTL_INT(_kern_ipc, OID_AUTO, m_defragrandomfailures, CTLFLAG_RW,
#endif
/*
+ * Malloc-type for external ext_buf ref counts.
+ */
+MALLOC_DEFINE(M_MBUF, "mbextcnt", "mbuf external ref counts");
+
+/*
+ * Allocate a given length worth of mbufs and/or clusters (whatever fits
+ * best) and return a pointer to the top of the allocated chain. If an
+ * existing mbuf chain is provided, then we will append the new chain
+ * to the existing one but still return the top of the newly allocated
+ * chain.
+ */
+struct mbuf *
+m_getm(struct mbuf *m, int len, int how, short type)
+{
+ struct mbuf *mb, *top, *cur, *mtail;
+ int num, rem;
+ int i;
+
+ KASSERT(len >= 0, ("m_getm(): len is < 0"));
+
+ /* If m != NULL, we will append to the end of that chain. */
+ if (m != NULL)
+ for (mtail = m; mtail->m_next != NULL; mtail = mtail->m_next);
+ else
+ mtail = NULL;
+
+ /*
+ * Calculate how many mbufs+clusters ("packets") we need and how much
+ * leftover there is after that and allocate the first mbuf+cluster
+ * if required.
+ */
+ num = len / MCLBYTES;
+ rem = len % MCLBYTES;
+ top = cur = NULL;
+ if (num > 0) {
+ if ((top = cur = m_getcl(how, type, 0)) == NULL)
+ goto failed;
+ }
+ num--;
+ top->m_len = 0;
+
+ for (i = 0; i < num; i++) {
+ mb = m_getcl(how, type, 0);
+ if (mb == NULL)
+ goto failed;
+ mb->m_len = 0;
+ cur = (cur->m_next = mb);
+ }
+ if (rem > 0) {
+ mb = (rem > MINCLSIZE) ?
+ m_getcl(how, type, 0) : m_get(how, type);
+ if (mb == NULL)
+ goto failed;
+ mb->m_len = 0;
+ if (cur == NULL)
+ top = mb;
+ else
+ cur->m_next = mb;
+ }
+
+ if (mtail != NULL)
+ mtail->m_next = top;
+ return top;
+failed:
+ if (top != NULL)
+ m_freem(top);
+ return NULL;
+}
+
+/*
+ * Free an entire chain of mbufs and associated external buffers, if
+ * applicable.
+ */
+void
+m_freem(struct mbuf *mb)
+{
+
+ while (mb != NULL)
+ mb = m_free(mb);
+}
+
+/*-
+ * Configure a provided mbuf to refer to the provided external storage
+ * buffer and setup a reference count for said buffer. If the setting
+ * up of the reference count fails, the M_EXT bit will not be set. If
+ * successfull, the M_EXT bit is set in the mbuf's flags.
+ *
+ * Arguments:
+ * mb The existing mbuf to which to attach the provided buffer.
+ * buf The address of the provided external storage buffer.
+ * size The size of the provided buffer.
+ * freef A pointer to a routine that is responsible for freeing the
+ * provided external storage buffer.
+ * args A pointer to an argument structure (of any type) to be passed
+ * to the provided freef routine (may be NULL).
+ * flags Any other flags to be passed to the provided mbuf.
+ * type The type that the external storage buffer should be
+ * labeled with.
+ *
+ * Returns:
+ * Nothing.
+ */
+void
+m_extadd(struct mbuf *mb, caddr_t buf, u_int size,
+ void (*freef)(void *, void *), void *args, int flags, int type)
+{
+ u_int *ref_cnt = NULL;
+
+ /* XXX Shouldn't be adding EXT_CLUSTER with this API */
+ if (type == EXT_CLUSTER)
+ ref_cnt = (u_int *)uma_find_refcnt(zone_clust,
+ mb->m_ext.ext_buf);
+ else if (type == EXT_EXTREF)
+ ref_cnt = mb->m_ext.ref_cnt;
+ mb->m_ext.ref_cnt = (ref_cnt == NULL) ?
+ malloc(sizeof(u_int), M_MBUF, M_NOWAIT) : (u_int *)ref_cnt;
+ if (mb->m_ext.ref_cnt != NULL) {
+ *(mb->m_ext.ref_cnt) = 1;
+ mb->m_flags |= (M_EXT | flags);
+ mb->m_ext.ext_buf = buf;
+ mb->m_data = mb->m_ext.ext_buf;
+ mb->m_ext.ext_size = size;
+ mb->m_ext.ext_free = freef;
+ mb->m_ext.ext_args = args;
+ mb->m_ext.ext_type = type;
+ }
+}
+
+/*
+ * Non-directly-exported function to clean up after mbufs with M_EXT
+ * storage attached to them if the reference count hits 0.
+ */
+void
+mb_free_ext(struct mbuf *m)
+{
+
+ MEXT_REM_REF(m);
+ if (atomic_cmpset_int(m->m_ext.ref_cnt, 0, 1)) {
+ if (m->m_ext.ext_type == EXT_PACKET) {
+ uma_zfree(zone_pack, m);
+ return;
+ } else if (m->m_ext.ext_type == EXT_CLUSTER) {
+ uma_zfree(zone_clust, m->m_ext.ext_buf);
+ m->m_ext.ext_buf = NULL;
+ } else {
+ (*(m->m_ext.ext_free))(m->m_ext.ext_buf,
+ m->m_ext.ext_args);
+ if (m->m_ext.ext_type != EXT_EXTREF)
+ free(m->m_ext.ref_cnt, M_MBUF);
+ }
+ }
+ uma_zfree(zone_mbuf, m);
+}
+
+/*
* "Move" mbuf pkthdr from "from" to "to".
* "from" must have M_PKTHDR set, and "to" must be empty.
*/
@@ -364,22 +519,22 @@ m_dup(struct mbuf *m, int how)
struct mbuf *n;
/* Get the next new mbuf */
- MGET(n, how, m->m_type);
+ if (remain >= MINCLSIZE) {
+ n = m_getcl(how, m->m_type, 0);
+ nsize = MCLBYTES;
+ } else {
+ n = m_get(how, m->m_type);
+ nsize = MLEN;
+ }
if (n == NULL)
goto nospace;
- if (top == NULL) { /* first one, must be PKTHDR */
- if (!m_dup_pkthdr(n, m, how))
- goto nospace;
- nsize = MHLEN;
- } else /* not the first one */
- nsize = MLEN;
- if (remain >= MINCLSIZE) {
- MCLGET(n, how);
- if ((n->m_flags & M_EXT) == 0) {
- (void)m_free(n);
+
+ if (top == NULL) { /* First one, must be PKTHDR */
+ if (!m_dup_pkthdr(n, m, how)) {
+ m_free(n);
goto nospace;
}
- nsize = MCLBYTES;
+ nsize = MHLEN;
}
n->m_len = 0;
@@ -651,39 +806,42 @@ m_devget(char *buf, int totlen, int off, struct ifnet *ifp,
void (*copy)(char *from, caddr_t to, u_int len))
{
struct mbuf *m;
- struct mbuf *top = 0, **mp = &top;
+ struct mbuf *top = NULL, **mp = &top;
int len;
if (off < 0 || off > MHLEN)
return (NULL);
- MGETHDR(m, M_DONTWAIT, MT_DATA);
- if (m == NULL)
- return (NULL);
- m->m_pkthdr.rcvif = ifp;
- m->m_pkthdr.len = totlen;
- len = MHLEN;
-
while (totlen > 0) {
- if (top) {
- MGET(m, M_DONTWAIT, MT_DATA);
- if (m == NULL) {
- m_freem(top);
- return (NULL);
- }
- len = MLEN;
- }
- if (totlen + off >= MINCLSIZE) {
- MCLGET(m, M_DONTWAIT);
- if (m->m_flags & M_EXT)
+ if (top == NULL) { /* First one, must be PKTHDR */
+ if (totlen + off >= MINCLSIZE) {
+ m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
len = MCLBYTES;
+ } else {
+ m = m_gethdr(M_DONTWAIT, MT_DATA);
+ len = MHLEN;
+
+ /* Place initial small packet/header at end of mbuf */
+ if (m && totlen + off + max_linkhdr <= MLEN) {
+ m->m_data += max_linkhdr;
+ len -= max_linkhdr;
+ }
+ }
+ if (m == NULL)
+ return NULL;
+ m->m_pkthdr.rcvif = ifp;
+ m->m_pkthdr.len = totlen;
} else {
- /*
- * Place initial small packet/header at end of mbuf.
- */
- if (top == NULL && totlen + off + max_linkhdr <= len) {
- m->m_data += max_linkhdr;
- len -= max_linkhdr;
+ if (totlen + off >= MINCLSIZE) {
+ m = m_getcl(M_DONTWAIT, MT_DATA, 0);
+ len = MCLBYTES;
+ } else {
+ m = m_get(M_DONTWAIT, MT_DATA);
+ len = MLEN;
+ }
+ if (m == NULL) {
+ m_freem(top);
+ return NULL;
}
}
if (off) {
@@ -722,9 +880,10 @@ m_copyback(struct mbuf *m0, int off, int len, c_caddr_t cp)
off -= mlen;
totlen += mlen;
if (m->m_next == NULL) {
- n = m_get_clrd(M_DONTWAIT, m->m_type);
+ n = m_get(M_DONTWAIT, m->m_type);
if (n == NULL)
goto out;
+ bzero(mtod(n, caddr_t), MLEN);
n->m_len = min(MLEN, len + off);
m->m_next = n;
}
diff --git a/sys/kern/uipc_mbuf2.c b/sys/kern/uipc_mbuf2.c
index 0d11aac..ff7944d 100644
--- a/sys/kern/uipc_mbuf2.c
+++ b/sys/kern/uipc_mbuf2.c
@@ -230,14 +230,10 @@ m_pulldown(struct mbuf *m, int off, int len, int *offp)
* now, we need to do the hard way. don't m_copy as there's no room
* on both end.
*/
- MGET(o, M_DONTWAIT, m->m_type);
- if (o && len > MLEN) {
- MCLGET(o, M_DONTWAIT);
- if ((o->m_flags & M_EXT) == 0) {
- m_free(o);
- o = NULL;
- }
- }
+ if (len > MLEN)
+ o = m_getcl(M_DONTWAIT, m->m_type, 0);
+ else
+ o = m_get(M_DONTWAIT, m->m_type);
if (!o) {
m_freem(m);
return NULL; /* ENOBUFS */
@@ -274,29 +270,27 @@ static struct mbuf *
m_dup1(struct mbuf *m, int off, int len, int wait)
{
struct mbuf *n;
- int l;
int copyhdr;
if (len > MCLBYTES)
return NULL;
- if (off == 0 && (m->m_flags & M_PKTHDR) != 0) {
+ if (off == 0 && (m->m_flags & M_PKTHDR) != 0)
copyhdr = 1;
- MGETHDR(n, wait, m->m_type);
- l = MHLEN;
- } else {
+ else
copyhdr = 0;
- MGET(n, wait, m->m_type);
- l = MLEN;
- }
- if (n && len > l) {
- MCLGET(n, wait);
- if ((n->m_flags & M_EXT) == 0) {
- m_free(n);
- n = NULL;
- }
+ if (len >= MINCLSIZE) {
+ if (copyhdr == 1)
+ n = m_getcl(wait, m->m_type, M_PKTHDR);
+ else
+ n = m_getcl(wait, m->m_type, 0);
+ } else {
+ if (copyhdr == 1)
+ n = m_gethdr(wait, m->m_type);
+ else
+ n = m_get(wait, m->m_type);
}
if (!n)
- return NULL;
+ return NULL; /* ENOBUFS */
if (copyhdr && !m_dup_pkthdr(n, m, wait)) {
m_free(n);
diff --git a/sys/kern/uipc_sockbuf.c b/sys/kern/uipc_sockbuf.c
index 3ab8f3a..a404d69 100644
--- a/sys/kern/uipc_sockbuf.c
+++ b/sys/kern/uipc_sockbuf.c
@@ -959,15 +959,12 @@ sbcreatecontrol(p, size, type, level)
if (CMSG_SPACE((u_int)size) > MCLBYTES)
return ((struct mbuf *) NULL);
- if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
+ if (CMSG_SPACE((u_int)size > MLEN))
+ m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
+ else
+ m = m_get(M_DONTWAIT, MT_CONTROL);
+ if (m == NULL)
return ((struct mbuf *) NULL);
- if (CMSG_SPACE((u_int)size) > MLEN) {
- MCLGET(m, M_DONTWAIT);
- if ((m->m_flags & M_EXT) == 0) {
- m_free(m);
- return ((struct mbuf *) NULL);
- }
- }
cp = mtod(m, struct cmsghdr *);
m->m_len = 0;
KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index e07f4ef..6735e49 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -527,8 +527,8 @@ sosend(so, addr, uio, top, control, flags, td)
{
struct mbuf **mp;
struct mbuf *m;
- long space, len, resid;
- int clen = 0, error, s, dontroute, mlen;
+ long space, len = 0, resid;
+ int clen = 0, error, s, dontroute;
int atomic = sosendallatonce(so) || top;
#ifdef ZERO_COPY_SOCKETS
int cow_send;
@@ -624,25 +624,23 @@ restart:
#ifdef ZERO_COPY_SOCKETS
cow_send = 0;
#endif /* ZERO_COPY_SOCKETS */
- if (top == 0) {
- MGETHDR(m, M_TRYWAIT, MT_DATA);
- if (m == NULL) {
- error = ENOBUFS;
- goto release;
- }
- mlen = MHLEN;
- m->m_pkthdr.len = 0;
- m->m_pkthdr.rcvif = (struct ifnet *)0;
- } else {
- MGET(m, M_TRYWAIT, MT_DATA);
- if (m == NULL) {
- error = ENOBUFS;
- goto release;
- }
- mlen = MLEN;
- }
if (resid >= MINCLSIZE) {
#ifdef ZERO_COPY_SOCKETS
+ if (top == NULL) {
+ MGETHDR(m, M_TRYWAIT, MT_DATA);
+ if (m == NULL) {
+ error = ENOBUFS;
+ goto release;
+ }
+ m->m_pkthdr.len = 0;
+ m->m_pkthdr.rcvif = (struct ifnet *)0;
+ } else {
+ MGET(m, M_TRYWAIT, MT_DATA);
+ if (m == NULL) {
+ error = ENOBUFS;
+ goto release;
+ }
+ }
if (so_zero_copy_send &&
resid>=PAGE_SIZE &&
space>=PAGE_SIZE &&
@@ -654,29 +652,48 @@ restart:
cow_send = socow_setup(m, uio);
}
}
- if (!cow_send){
+ if (!cow_send) {
+ MCLGET(m, M_TRYWAIT);
+ if ((m->m_flags & M_EXT) == 0) {
+ m_free(m);
+ m = NULL;
+ } else {
+ len = min(min(MCLBYTES, resid), space);
+ }
+ } else
+ len = PAGE_SIZE;
+#else /* ZERO_COPY_SOCKETS */
+ if (top == NULL) {
+ m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
+ m->m_pkthdr.len = 0;
+ m->m_pkthdr.rcvif = (struct ifnet *)0;
+ } else
+ m = m_getcl(M_TRYWAIT, MT_DATA, 0);
+ len = min(min(MCLBYTES, resid), space);
#endif /* ZERO_COPY_SOCKETS */
- MCLGET(m, M_TRYWAIT);
- if ((m->m_flags & M_EXT) == 0)
- goto nopages;
- mlen = MCLBYTES;
- len = min(min(mlen, resid), space);
} else {
-#ifdef ZERO_COPY_SOCKETS
- len = PAGE_SIZE;
+ if (top == NULL) {
+ m = m_gethdr(M_TRYWAIT, MT_DATA);
+ m->m_pkthdr.len = 0;
+ m->m_pkthdr.rcvif = (struct ifnet *)0;
+
+ len = min(min(MHLEN, resid), space);
+ /*
+ * For datagram protocols, leave room
+ * for protocol headers in first mbuf.
+ */
+ if (atomic && m && len < MHLEN)
+ MH_ALIGN(m, len);
+ } else {
+ m = m_get(M_TRYWAIT, MT_DATA);
+ len = min(min(MLEN, resid), space);
}
-
- } else {
-#endif /* ZERO_COPY_SOCKETS */
-nopages:
- len = min(min(mlen, resid), space);
- /*
- * For datagram protocols, leave room
- * for protocol headers in first mbuf.
- */
- if (atomic && top == 0 && len < mlen)
- MH_ALIGN(m, len);
}
+ if (m == NULL) {
+ error = ENOBUFS;
+ goto release;
+ }
+
space -= len;
#ifdef ZERO_COPY_SOCKETS
if (cow_send)
diff --git a/sys/kern/uipc_socket2.c b/sys/kern/uipc_socket2.c
index 3ab8f3a..a404d69 100644
--- a/sys/kern/uipc_socket2.c
+++ b/sys/kern/uipc_socket2.c
@@ -959,15 +959,12 @@ sbcreatecontrol(p, size, type, level)
if (CMSG_SPACE((u_int)size) > MCLBYTES)
return ((struct mbuf *) NULL);
- if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
+ if (CMSG_SPACE((u_int)size > MLEN))
+ m = m_getcl(M_DONTWAIT, MT_CONTROL, 0);
+ else
+ m = m_get(M_DONTWAIT, MT_CONTROL);
+ if (m == NULL)
return ((struct mbuf *) NULL);
- if (CMSG_SPACE((u_int)size) > MLEN) {
- MCLGET(m, M_DONTWAIT);
- if ((m->m_flags & M_EXT) == 0) {
- m_free(m);
- return ((struct mbuf *) NULL);
- }
- }
cp = mtod(m, struct cmsghdr *);
m->m_len = 0;
KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c
index 1b886f5..978c30e 100644
--- a/sys/kern/uipc_syscalls.c
+++ b/sys/kern/uipc_syscalls.c
@@ -61,6 +61,7 @@ __FBSDID("$FreeBSD$");
#include <sys/socketvar.h>
#include <sys/signalvar.h>
#include <sys/syscallsubr.h>
+#include <sys/sysctl.h>
#include <sys/uio.h>
#include <sys/vnode.h>
#ifdef KTRACE
@@ -85,6 +86,21 @@ static int getpeername1(struct thread *td, struct getpeername_args *uap,
int compat);
/*
+ * NSFBUFS-related variables and associated sysctls
+ */
+int nsfbufs;
+int nsfbufspeak;
+int nsfbufsused;
+
+SYSCTL_DECL(_kern_ipc);
+SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
+ "Maximum number of sendfile(2) sf_bufs available");
+SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
+ "Number of sendfile(2) sf_bufs at peak usage");
+SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
+ "Number of sendfile(2) sf_bufs in use");
+
+/*
* System call interface to the socket abstraction.
*/
#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
OpenPOWER on IntegriCloud