2 * Copyright (c) 2004, 2005,
3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_param.h"
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
38 #include <sys/domain.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
45 #include <security/mac/mac_framework.h>
48 #include <vm/vm_page.h>
50 #include <vm/uma_int.h>
51 #include <vm/uma_dbg.h>
54 * In FreeBSD, Mbufs and Mbuf Clusters are allocated from UMA
57 * Mbuf Clusters (2K, contiguous) are allocated from the Cluster
58 * Zone. The Zone can be capped at kern.ipc.nmbclusters, if the
59 * administrator so desires.
61 * Mbufs are allocated from a UMA Master Zone called the Mbuf
64 * Additionally, FreeBSD provides a Packet Zone, which it
65 * configures as a Secondary Zone to the Mbuf Master Zone,
66 * thus sharing backend Slab kegs with the Mbuf Master Zone.
68 * Thus common-case allocations and locking are simplified:
72 * | .------------>[(Packet Cache)] m_get(), m_gethdr()
74 * [(Cluster Cache)] [ Secondary ] [ (Mbuf Cache) ]
75 * [ Cluster Zone ] [ Zone ] [ Mbuf Master Zone ]
81 * \____________(VM)_________________/
84 * Whenever an object is allocated with uma_zalloc() out of
85 * one of the Zones its _ctor_ function is executed. The same
86 * for any deallocation through uma_zfree() the _dtor_ function
89 * Caches are per-CPU and are filled from the Master Zone.
91 * Whenever an object is allocated from the underlying global
92 * memory pool it gets pre-initialized with the _zinit_ functions.
93 * When the Keg's are overfull objects get decomissioned with
94 * _zfini_ functions and free'd back to the global memory pool.
98 int nmbclusters
; /* limits number of mbuf clusters */
99 int nmbjumbop
; /* limits number of page size jumbo clusters */
100 int nmbjumbo9
; /* limits number of 9k jumbo clusters */
101 int nmbjumbo16
; /* limits number of 16k jumbo clusters */
102 struct mbstat mbstat
;
105 tunable_mbinit(void *dummy
)
108 /* This has to be done before VM init. */
109 nmbclusters
= 1024 + maxusers
* 64;
110 nmbjumbop
= nmbclusters
/ 2;
111 nmbjumbo9
= nmbjumbop
/ 2;
112 nmbjumbo16
= nmbjumbo9
/ 2;
113 TUNABLE_INT_FETCH("kern.ipc.nmbclusters", &nmbclusters
);
115 SYSINIT(tunable_mbinit
, SI_SUB_TUNABLES
, SI_ORDER_ANY
, tunable_mbinit
, NULL
);
117 /* XXX: These should be tuneables. Can't change UMA limits on the fly. */
119 sysctl_nmbclusters(SYSCTL_HANDLER_ARGS
)
121 int error
, newnmbclusters
;
123 newnmbclusters
= nmbclusters
;
124 error
= sysctl_handle_int(oidp
, &newnmbclusters
, 0, req
);
125 if (error
== 0 && req
->newptr
) {
126 if (newnmbclusters
> nmbclusters
) {
127 nmbclusters
= newnmbclusters
;
128 uma_zone_set_max(zone_clust
, nmbclusters
);
129 EVENTHANDLER_INVOKE(nmbclusters_change
);
135 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, nmbclusters
, CTLTYPE_INT
|CTLFLAG_RW
,
136 &nmbclusters
, 0, sysctl_nmbclusters
, "IU",
137 "Maximum number of mbuf clusters allowed");
140 sysctl_nmbjumbop(SYSCTL_HANDLER_ARGS
)
142 int error
, newnmbjumbop
;
144 newnmbjumbop
= nmbjumbop
;
145 error
= sysctl_handle_int(oidp
, &newnmbjumbop
, 0, req
);
146 if (error
== 0 && req
->newptr
) {
147 if (newnmbjumbop
> nmbjumbop
) {
148 nmbjumbop
= newnmbjumbop
;
149 uma_zone_set_max(zone_jumbop
, nmbjumbop
);
155 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, nmbjumbop
, CTLTYPE_INT
|CTLFLAG_RW
,
156 &nmbjumbop
, 0, sysctl_nmbjumbop
, "IU",
157 "Maximum number of mbuf page size jumbo clusters allowed");
161 sysctl_nmbjumbo9(SYSCTL_HANDLER_ARGS
)
163 int error
, newnmbjumbo9
;
165 newnmbjumbo9
= nmbjumbo9
;
166 error
= sysctl_handle_int(oidp
, &newnmbjumbo9
, 0, req
);
167 if (error
== 0 && req
->newptr
) {
168 if (newnmbjumbo9
> nmbjumbo9
) {
169 nmbjumbo9
= newnmbjumbo9
;
170 uma_zone_set_max(zone_jumbo9
, nmbjumbo9
);
176 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, nmbjumbo9
, CTLTYPE_INT
|CTLFLAG_RW
,
177 &nmbjumbo9
, 0, sysctl_nmbjumbo9
, "IU",
178 "Maximum number of mbuf 9k jumbo clusters allowed");
181 sysctl_nmbjumbo16(SYSCTL_HANDLER_ARGS
)
183 int error
, newnmbjumbo16
;
185 newnmbjumbo16
= nmbjumbo16
;
186 error
= sysctl_handle_int(oidp
, &newnmbjumbo16
, 0, req
);
187 if (error
== 0 && req
->newptr
) {
188 if (newnmbjumbo16
> nmbjumbo16
) {
189 nmbjumbo16
= newnmbjumbo16
;
190 uma_zone_set_max(zone_jumbo16
, nmbjumbo16
);
196 SYSCTL_PROC(_kern_ipc
, OID_AUTO
, nmbjumbo16
, CTLTYPE_INT
|CTLFLAG_RW
,
197 &nmbjumbo16
, 0, sysctl_nmbjumbo16
, "IU",
198 "Maximum number of mbuf 16k jumbo clusters allowed");
202 SYSCTL_STRUCT(_kern_ipc
, OID_AUTO
, mbstat
, CTLFLAG_RD
, &mbstat
, mbstat
,
203 "Mbuf general information and statistics");
206 * Zones from which we allocate.
208 uma_zone_t zone_mbuf
;
209 uma_zone_t zone_clust
;
210 uma_zone_t zone_pack
;
211 uma_zone_t zone_jumbop
;
212 uma_zone_t zone_jumbo9
;
213 uma_zone_t zone_jumbo16
;
214 uma_zone_t zone_ext_refcnt
;
219 static int mb_ctor_mbuf(void *, int, void *, int);
220 static int mb_ctor_clust(void *, int, void *, int);
221 static int mb_ctor_pack(void *, int, void *, int);
222 static void mb_dtor_mbuf(void *, int, void *);
223 static void mb_dtor_clust(void *, int, void *);
224 static void mb_dtor_pack(void *, int, void *);
225 static int mb_zinit_pack(void *, int, int);
226 static void mb_zfini_pack(void *, int);
228 static void mb_reclaim(void *);
229 static void mbuf_init(void *);
230 static void *mbuf_jumbo_alloc(uma_zone_t
, int, u_int8_t
*, int);
231 static void mbuf_jumbo_free(void *, int, u_int8_t
);
233 static MALLOC_DEFINE(M_JUMBOFRAME
, "jumboframes", "mbuf jumbo frame buffers");
235 /* Ensure that MSIZE doesn't break dtom() - it must be a power of 2 */
236 CTASSERT((((MSIZE
- 1) ^ MSIZE
) + 1) >> 1 == MSIZE
);
239 * Initialize FreeBSD Network buffer allocation.
241 SYSINIT(mbuf
, SI_SUB_MBUF
, SI_ORDER_FIRST
, mbuf_init
, NULL
);
243 mbuf_init(void *dummy
)
247 * Configure UMA zones for Mbufs, Clusters, and Packets.
249 zone_mbuf
= uma_zcreate(MBUF_MEM_NAME
, MSIZE
,
250 mb_ctor_mbuf
, mb_dtor_mbuf
,
252 trash_init
, trash_fini
,
256 MSIZE
- 1, UMA_ZONE_MAXBUCKET
);
258 zone_clust
= uma_zcreate(MBUF_CLUSTER_MEM_NAME
, MCLBYTES
,
259 mb_ctor_clust
, mb_dtor_clust
,
261 trash_init
, trash_fini
,
265 UMA_ALIGN_PTR
, UMA_ZONE_REFCNT
);
267 uma_zone_set_max(zone_clust
, nmbclusters
);
269 zone_pack
= uma_zsecond_create(MBUF_PACKET_MEM_NAME
, mb_ctor_pack
,
270 mb_dtor_pack
, mb_zinit_pack
, mb_zfini_pack
, zone_mbuf
);
272 /* Make jumbo frame zone too. Page size, 9k and 16k. */
273 zone_jumbop
= uma_zcreate(MBUF_JUMBOP_MEM_NAME
, MJUMPAGESIZE
,
274 mb_ctor_clust
, mb_dtor_clust
,
276 trash_init
, trash_fini
,
280 UMA_ALIGN_PTR
, UMA_ZONE_REFCNT
);
282 uma_zone_set_max(zone_jumbop
, nmbjumbop
);
284 zone_jumbo9
= uma_zcreate(MBUF_JUMBO9_MEM_NAME
, MJUM9BYTES
,
285 mb_ctor_clust
, mb_dtor_clust
,
287 trash_init
, trash_fini
,
291 UMA_ALIGN_PTR
, UMA_ZONE_REFCNT
);
293 uma_zone_set_max(zone_jumbo9
, nmbjumbo9
);
294 uma_zone_set_allocf(zone_jumbo9
, mbuf_jumbo_alloc
);
295 uma_zone_set_freef(zone_jumbo9
, mbuf_jumbo_free
);
297 zone_jumbo16
= uma_zcreate(MBUF_JUMBO16_MEM_NAME
, MJUM16BYTES
,
298 mb_ctor_clust
, mb_dtor_clust
,
300 trash_init
, trash_fini
,
304 UMA_ALIGN_PTR
, UMA_ZONE_REFCNT
);
306 uma_zone_set_max(zone_jumbo16
, nmbjumbo16
);
307 uma_zone_set_allocf(zone_jumbo16
, mbuf_jumbo_alloc
);
308 uma_zone_set_freef(zone_jumbo16
, mbuf_jumbo_free
);
310 zone_ext_refcnt
= uma_zcreate(MBUF_EXTREFCNT_MEM_NAME
, sizeof(u_int
),
313 UMA_ALIGN_PTR
, UMA_ZONE_ZINIT
);
315 /* uma_prealloc() goes here... */
318 * Hook event handler for low-memory situation, used to
319 * drain protocols and push data back to the caches (UMA
320 * later pushes it back to VM).
322 EVENTHANDLER_REGISTER(vm_lowmem
, mb_reclaim
, NULL
,
323 EVENTHANDLER_PRI_FIRST
);
326 * [Re]set counters and local statistics knobs.
327 * XXX Some of these should go and be replaced, but UMA stat
328 * gathering needs to be revised.
331 mbstat
.m_mclusts
= 0;
333 mbstat
.m_msize
= MSIZE
;
334 mbstat
.m_mclbytes
= MCLBYTES
;
335 mbstat
.m_minclsize
= MINCLSIZE
;
336 mbstat
.m_mlen
= MLEN
;
337 mbstat
.m_mhlen
= MHLEN
;
338 mbstat
.m_numtypes
= MT_NTYPES
;
340 mbstat
.m_mcfail
= mbstat
.m_mpfail
= 0;
342 mbstat
.sf_allocwait
= mbstat
.sf_allocfail
= 0;
346 * UMA backend page allocator for the jumbo frame zones.
348 * Allocates kernel virtual memory that is backed by contiguous physical
352 mbuf_jumbo_alloc(uma_zone_t zone
, int bytes
, u_int8_t
*flags
, int wait
)
355 /* Inform UMA that this allocator uses kernel_map/object. */
356 *flags
= UMA_SLAB_KERNEL
;
357 return (contigmalloc(bytes
, M_JUMBOFRAME
, wait
, (vm_paddr_t
)0,
358 ~(vm_paddr_t
)0, 1, 0));
362 * UMA backend page deallocator for the jumbo frame zones.
365 mbuf_jumbo_free(void *mem
, int size
, u_int8_t flags
)
368 contigfree(mem
, size
, M_JUMBOFRAME
);
372 * Constructor for Mbuf master zone.
374 * The 'arg' pointer points to a mb_args structure which
375 * contains call-specific information required to support the
376 * mbuf allocation API. See mbuf.h.
379 mb_ctor_mbuf(void *mem
, int size
, void *arg
, int how
)
382 struct mb_args
*args
;
390 trash_ctor(mem
, size
, arg
, how
);
392 m
= (struct mbuf
*)mem
;
393 args
= (struct mb_args
*)arg
;
398 * The mbuf is initialized later. The caller has the
399 * responsibility to set up any MAC labels too.
401 if (type
== MT_NOINIT
)
409 if (flags
& M_PKTHDR
) {
410 m
->m_data
= m
->m_pktdat
;
411 m
->m_pkthdr
.rcvif
= NULL
;
413 m
->m_pkthdr
.header
= NULL
;
414 m
->m_pkthdr
.csum_flags
= 0;
415 m
->m_pkthdr
.csum_data
= 0;
416 m
->m_pkthdr
.tso_segsz
= 0;
417 m
->m_pkthdr
.ether_vtag
= 0;
418 SLIST_INIT(&m
->m_pkthdr
.tags
);
420 /* If the label init fails, fail the alloc */
421 error
= mac_mbuf_init(m
, how
);
426 m
->m_data
= m
->m_dat
;
431 * The Mbuf master zone destructor.
434 mb_dtor_mbuf(void *mem
, int size
, void *arg
)
439 m
= (struct mbuf
*)mem
;
440 flags
= (unsigned long)arg
;
442 if ((flags
& MB_NOTAGS
) == 0 && (m
->m_flags
& M_PKTHDR
) != 0)
443 m_tag_delete_chain(m
, NULL
);
444 KASSERT((m
->m_flags
& M_EXT
) == 0, ("%s: M_EXT set", __func__
));
445 KASSERT((m
->m_flags
& M_NOFREE
) == 0, ("%s: M_NOFREE set", __func__
));
447 trash_dtor(mem
, size
, arg
);
452 * The Mbuf Packet zone destructor.
455 mb_dtor_pack(void *mem
, int size
, void *arg
)
459 m
= (struct mbuf
*)mem
;
460 if ((m
->m_flags
& M_PKTHDR
) != 0)
461 m_tag_delete_chain(m
, NULL
);
463 /* Make sure we've got a clean cluster back. */
464 KASSERT((m
->m_flags
& M_EXT
) == M_EXT
, ("%s: M_EXT not set", __func__
));
465 KASSERT(m
->m_ext
.ext_buf
!= NULL
, ("%s: ext_buf == NULL", __func__
));
466 KASSERT(m
->m_ext
.ext_free
== NULL
, ("%s: ext_free != NULL", __func__
));
467 KASSERT(m
->m_ext
.ext_arg1
== NULL
, ("%s: ext_arg1 != NULL", __func__
));
468 KASSERT(m
->m_ext
.ext_arg2
== NULL
, ("%s: ext_arg2 != NULL", __func__
));
469 KASSERT(m
->m_ext
.ext_size
== MCLBYTES
, ("%s: ext_size != MCLBYTES", __func__
));
470 KASSERT(m
->m_ext
.ext_type
== EXT_PACKET
, ("%s: ext_type != EXT_PACKET", __func__
));
471 KASSERT(*m
->m_ext
.ref_cnt
== 1, ("%s: ref_cnt != 1", __func__
));
473 trash_dtor(m
->m_ext
.ext_buf
, MCLBYTES
, arg
);
476 * If there are processes blocked on zone_clust, waiting for pages
477 * to be freed up, * cause them to be woken up by draining the
478 * packet zone. We are exposed to a race here * (in the check for
479 * the UMA_ZFLAG_FULL) where we might miss the flag set, but that
480 * is deliberate. We don't want to acquire the zone lock for every
483 if (uma_zone_exhausted_nolock(zone_clust
))
484 zone_drain(zone_pack
);
488 * The Cluster and Jumbo[PAGESIZE|9|16] zone constructor.
490 * Here the 'arg' pointer points to the Mbuf which we
491 * are configuring cluster storage for. If 'arg' is
492 * empty we allocate just the cluster without setting
493 * the mbuf to it. See mbuf.h.
496 mb_ctor_clust(void *mem
, int size
, void *arg
, int how
)
504 trash_ctor(mem
, size
, arg
, how
);
511 #if MJUMPAGESIZE != MCLBYTES
526 panic("unknown cluster size");
530 m
= (struct mbuf
*)arg
;
531 refcnt
= uma_find_refcnt(zone
, mem
);
534 m
->m_ext
.ext_buf
= (caddr_t
)mem
;
535 m
->m_data
= m
->m_ext
.ext_buf
;
537 m
->m_ext
.ext_free
= NULL
;
538 m
->m_ext
.ext_arg1
= NULL
;
539 m
->m_ext
.ext_arg2
= NULL
;
540 m
->m_ext
.ext_size
= size
;
541 m
->m_ext
.ext_type
= type
;
542 m
->m_ext
.ref_cnt
= refcnt
;
549 * The Mbuf Cluster zone destructor.
552 mb_dtor_clust(void *mem
, int size
, void *arg
)
557 zone
= m_getzone(size
);
558 KASSERT(*(uma_find_refcnt(zone
, mem
)) <= 1,
559 ("%s: refcnt incorrect %u", __func__
,
560 *(uma_find_refcnt(zone
, mem
))) );
562 trash_dtor(mem
, size
, arg
);
567 * The Packet secondary zone's init routine, executed on the
568 * object's transition from mbuf keg slab to zone cache.
571 mb_zinit_pack(void *mem
, int size
, int how
)
575 m
= (struct mbuf
*)mem
; /* m is virgin. */
576 if (uma_zalloc_arg(zone_clust
, m
, how
) == NULL
||
577 m
->m_ext
.ext_buf
== NULL
)
579 m
->m_ext
.ext_type
= EXT_PACKET
; /* Override. */
581 trash_init(m
->m_ext
.ext_buf
, MCLBYTES
, how
);
587 * The Packet secondary zone's fini routine, executed on the
588 * object's transition from zone cache to keg slab.
591 mb_zfini_pack(void *mem
, int size
)
595 m
= (struct mbuf
*)mem
;
597 trash_fini(m
->m_ext
.ext_buf
, MCLBYTES
);
599 uma_zfree_arg(zone_clust
, m
->m_ext
.ext_buf
, NULL
);
601 trash_dtor(mem
, size
, NULL
);
606 * The "packet" keg constructor.
609 mb_ctor_pack(void *mem
, int size
, void *arg
, int how
)
612 struct mb_args
*args
;
619 m
= (struct mbuf
*)mem
;
620 args
= (struct mb_args
*)arg
;
625 trash_ctor(m
->m_ext
.ext_buf
, MCLBYTES
, arg
, how
);
629 m
->m_data
= m
->m_ext
.ext_buf
;
631 m
->m_flags
= (flags
| M_EXT
);
634 if (flags
& M_PKTHDR
) {
635 m
->m_pkthdr
.rcvif
= NULL
;
637 m
->m_pkthdr
.header
= NULL
;
638 m
->m_pkthdr
.csum_flags
= 0;
639 m
->m_pkthdr
.csum_data
= 0;
640 m
->m_pkthdr
.tso_segsz
= 0;
641 m
->m_pkthdr
.ether_vtag
= 0;
642 SLIST_INIT(&m
->m_pkthdr
.tags
);
644 /* If the label init fails, fail the alloc */
645 error
= mac_mbuf_init(m
, how
);
650 /* m_ext is already initialized. */
656 * This is the protocol drain routine.
658 * No locks should be held when this is called. The drain routines have to
659 * presently acquire some locks which raises the possibility of lock order
663 mb_reclaim(void *junk
)
668 WITNESS_WARN(WARN_GIANTOK
| WARN_SLEEPOK
| WARN_PANIC
, NULL
,
671 for (dp
= domains
; dp
!= NULL
; dp
= dp
->dom_next
)
672 for (pr
= dp
->dom_protosw
; pr
< dp
->dom_protoswNPROTOSW
; pr
++)
673 if (pr
->pr_drain
!= NULL
)