2 * Copyright (c) 2010 by The DragonFly Project and Samuel J. Greear.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Samuel J. Greear <sjg@thesjg.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
37 #include <sys/kernel.h>
38 #include <sys/objcache.h>
39 #include <sys/sysctl.h>
40 #include <sys/param.h>
41 #include <sys/serialize.h>
42 #include <sys/systm.h>
44 #include <sys/queue.h>
47 #include <vm/vm_extern.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_page.h>
50 #include <cpu/lwbuf.h>
51 #include <machine/globaldata.h>
52 #include <machine/atomic.h>
53 #include <machine/param.h>
55 #include <sys/mplock2.h>
57 static void lwbuf_init(void *);
58 SYSINIT(sock_lwb, SI_BOOT2_MACHDEP, SI_ORDER_ANY, lwbuf_init, NULL);
60 /* Number of pages of KVA to allocate at boot per cpu (1MB) */
61 #define LWBUF_BOOT_PAGES 256
62 /* Number to allocate incrementally (128KB) */
63 #define LWBUF_ALLOC_PAGES 32
65 MALLOC_DEFINE(M_LWBUF, "lwbuf", "Lightweight buffers");
68 lwbuf_initpages(struct mdglobaldata *gd, int pages)
75 k = kmem_alloc_nofault(&kernel_map, PAGE_SIZE * pages, PAGE_SIZE);
79 for (i = 0; i < pages; ++i) {
80 lwb = kmalloc(sizeof(*lwb), M_LWBUF, M_WAITOK | M_ZERO);
81 lwb->kva = k + (i * PAGE_SIZE);
83 SLIST_INSERT_HEAD(&gd->gd_lwbuf_fpages, lwb, next);
93 struct mdglobaldata *gd = mdcpu;
96 for (i = 0; i < ncpus; ++i) {
97 gd = &CPU_prvspace[i].mdglobaldata;
98 SLIST_INIT(&gd->gd_lwbuf_fpages);
99 lwbuf_initpages(gd, LWBUF_BOOT_PAGES);
104 lwbuf_alloc(vm_page_t m)
106 struct mdglobaldata *gd = mdcpu;
109 crit_enter_gd(&gd->mi);
110 while ((lwb = SLIST_FIRST(&gd->gd_lwbuf_fpages)) == NULL) {
111 if (lwbuf_initpages(gd, LWBUF_ALLOC_PAGES) == FALSE)
112 tsleep(&gd->gd_lwbuf_fpages, 0, "lwbuf", 0);
114 --gd->gd_lwbuf_count;
115 SLIST_REMOVE_HEAD(&gd->gd_lwbuf_fpages, next);
117 crit_exit_gd(&gd->mi);
119 pmap_kenter_quick(lwb->kva, m->phys_addr);
120 lwb->cpumask |= gd->mi.gd_cpumask;
127 lwbuf_free_remote(void *arg)
133 lwbuf_free(struct lwbuf *lwb)
135 struct mdglobaldata *gd = mdcpu;
141 if (gd->gd_lwbuf_count > LWBUF_BOOT_PAGES) {
143 kmem_free(&kernel_map, lwb->kva, PAGE_SIZE);
145 bzero(lwb, sizeof(*lwb));
148 crit_enter_gd(&gd->mi);
149 SLIST_INSERT_HEAD(&gd->gd_lwbuf_fpages, lwb, next);
150 if (gd->gd_lwbuf_count++ == 0)
151 wakeup_one(&gd->gd_lwbuf_fpages);
152 crit_exit_gd(&gd->mi);
157 lwkt_send_ipiq_passive(&lwb->gd->mi, lwbuf_free_remote, lwb);
163 lwbuf_set_global(struct lwbuf *lwb)
165 pmap_kenter_sync(lwb->kva);
166 lwb->cpumask = (cpumask_t)-1;
170 _lwbuf_kva(struct lwbuf *lwb, struct mdglobaldata *gd)
174 pmap_kenter_sync_quick(lwb->kva);
178 new = old | gd->mi.gd_cpumask;
179 } while (atomic_cmpset_int(&lwb->cpumask, old, new) == 0);
185 lwbuf_kva(struct lwbuf *lwb)
187 struct mdglobaldata *gd = mdcpu;
189 if (lwb->cpumask & gd->mi.gd_cpumask)
192 return (_lwbuf_kva(lwb, gd));