kernel - Fix lwbuf leak for i386
[dragonfly.git] / sys / cpu / i386 / misc / lwbuf.c
1 /*
2  * Copyright (c) 2010 by The DragonFly Project and Samuel J. Greear.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Samuel J. Greear <sjg@thesjg.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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
17  *    distribution.
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.
21  *
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
33  * SUCH DAMAGE.
34  */
35
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>
43 #include <sys/proc.h>
44 #include <sys/queue.h>
45 #include <vm/vm.h>
46 #include <vm/pmap.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>
54
55 #include <sys/mplock2.h>
56
57 static void lwbuf_init(void *);
58 SYSINIT(sock_lwb, SI_BOOT2_MACHDEP, SI_ORDER_ANY, lwbuf_init, NULL);
59
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
64
65 MALLOC_DEFINE(M_LWBUF, "lwbuf", "Lightweight buffers");
66
67 static boolean_t
68 lwbuf_initpages(struct mdglobaldata *gd, int pages)
69 {
70     struct lwbuf *lwb;
71     vm_offset_t k;
72     int i;
73
74     get_mplock();
75     k = kmem_alloc_nofault(&kernel_map, PAGE_SIZE * pages, PAGE_SIZE);
76     rel_mplock();
77     if (k == 0)
78         return (FALSE);
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);
82         lwb->gd = gd;
83         SLIST_INSERT_HEAD(&gd->gd_lwbuf_fpages, lwb, next);
84         ++gd->gd_lwbuf_count;
85     }
86
87     return (TRUE);
88 }
89
90 static void
91 lwbuf_init(void *arg)
92 {
93     struct mdglobaldata *gd = mdcpu;
94     int i;
95
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);
100     }
101 }
102
103 struct lwbuf *
104 lwbuf_alloc(vm_page_t m)
105 {
106     struct mdglobaldata *gd = mdcpu;
107     struct lwbuf *lwb;
108
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);
113     }
114     --gd->gd_lwbuf_count;
115     SLIST_REMOVE_HEAD(&gd->gd_lwbuf_fpages, next);
116     lwb->m = m;
117     crit_exit_gd(&gd->mi);
118
119     pmap_kenter_quick(lwb->kva, m->phys_addr);
120     lwb->cpumask |= gd->mi.gd_cpumask;
121
122     return (lwb);
123 }
124
125 static
126 void
127 lwbuf_free_remote(void *arg)
128 {
129     lwbuf_free(arg);
130 }
131
132 void
133 lwbuf_free(struct lwbuf *lwb)
134 {
135     struct mdglobaldata *gd = mdcpu;
136
137     lwb->m = NULL;
138     lwb->cpumask = 0;
139
140     if (gd == lwb->gd) {
141         if (gd->gd_lwbuf_count > LWBUF_BOOT_PAGES) {
142             get_mplock();
143             kmem_free(&kernel_map, lwb->kva, PAGE_SIZE);
144             rel_mplock();
145             bzero(lwb, sizeof(*lwb));
146             kfree(lwb, M_LWBUF);
147         } else {
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);
153         }
154     }
155 #ifdef SMP
156     else {
157         lwkt_send_ipiq_passive(&lwb->gd->mi, lwbuf_free_remote, lwb);
158     }
159 #endif
160 }
161
162 void
163 lwbuf_set_global(struct lwbuf *lwb)
164 {
165     pmap_kenter_sync(lwb->kva);
166     lwb->cpumask = (cpumask_t)-1;
167 }
168
169 static vm_offset_t
170 _lwbuf_kva(struct lwbuf *lwb, struct mdglobaldata *gd)
171 {
172     cpumask_t old, new;
173
174     pmap_kenter_sync_quick(lwb->kva);
175
176     do {
177         old = lwb->cpumask;
178         new = old | gd->mi.gd_cpumask;
179     } while (atomic_cmpset_int(&lwb->cpumask, old, new) == 0);
180
181     return (lwb->kva);
182 }
183
184 __inline vm_offset_t
185 lwbuf_kva(struct lwbuf *lwb)
186 {
187     struct mdglobaldata *gd = mdcpu;
188
189     if (lwb->cpumask & gd->mi.gd_cpumask)
190         return (lwb->kva);
191
192     return (_lwbuf_kva(lwb, gd));
193 }