Let the allocation of registers be done by compilers nowadays. The average
[dragonfly.git] / sys / kern / kern_msfbuf.c
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Hiten Pandya <hmp@backplane.com> and Matthew Dillon
6  * <dillon@backplane.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  * The MSF_BUF API was augmented from the SFBUF API:
36  *      Copyright (c) 1998 David Greenman.  All rights reserved.
37  *      src/sys/kern/kern_sfbuf.c,v 1.7 2004/05/13 19:46:18 dillon
38  *
39  * $DragonFly: src/sys/kern/kern_msfbuf.c,v 1.6 2004/07/21 12:44:25 hmp Exp $
40  */
41 /*
42  * MSFBUFs cache linear multi-page ephermal mappings and operate similar
43  * to SFBUFs.  MSFBUFs use XIO's internally to hold the page list and can
44  * be considered to be a KVA wrapper around an XIO.
45  *
46  * Like the SFBUF subsystem, the locking and validation of the page array
47  * is the responsibility of the caller.  Also like the SFBUF subsystem,
48  * MSFBUFs are SMP-friendly, cache the mappings, and will avoid unnecessary
49  * page invalidations when possible.
50  *
51  * MSFBUFs are primarily designed to be used in subsystems that manipulate
52  * XIOs.  The DEV and BUF subsystems are a good example.
53  *
54  * TODO LIST:
55  *      - Implement the FREEQ optimization that exists in the SFBUF code.
56  *      - Allow allocation (aka mapping) based on an XIO instead of a pglist.
57  *      - Overload XIOs representitive of smaller chunks of memory onto the
58  *        same KVA space to efficiently cache smaller mappings (filesystem
59  *        blocks / buffer cache related).
60  */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/globaldata.h>
65 #include <sys/kernel.h>
66 #include <sys/malloc.h>
67 #include <sys/queue.h>
68 #include <sys/sfbuf.h>
69 #include <sys/sysctl.h>
70 #include <sys/thread.h>
71 #include <sys/thread2.h>
72 #include <sys/xio.h>
73 #include <sys/msfbuf.h>
74
75 #include <vm/vm.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_page.h>
79 #include <vm/pmap.h>
80
81 MALLOC_DECLARE(M_MSFBUF);
82 MALLOC_DEFINE(M_MSFBUF, "MSFBUF", "direct-copy buffers");
83
84 /* lists and queues associated with msf_bufs */
85 LIST_HEAD(msf_buf_list, msf_buf);
86
87 TAILQ_HEAD(, msf_buf) msf_buf_freelist;
88
89 /* hash table for tracking msf_bufs */
90 static struct msf_buf_list *msf_buf_hashtable;
91 static u_long msf_buf_hashmask;
92
93 /* indicate shortage of available msf_bufs */
94 static u_int msf_buf_alloc_want;
95
96 /* base of the msf_buf map */
97 static vm_offset_t msf_base;
98 static struct msf_buf *msf_bufs;
99
100 static int num_msf_bufs = 256; /* magic value */
101 SYSCTL_INT(_kern_ipc, OID_AUTO, msfbufs, CTLFLAG_RD, &num_msf_bufs,
102         0, "number of direct-copy buffers available");
103
104 static void
105 msf_buf_init(void *__dummy)
106 {
107         int i;
108         
109         msf_buf_alloc_want = 0;
110         TUNABLE_INT_FETCH("kern.ipc.msfbufs", &num_msf_bufs);
111
112         msf_buf_hashtable = hashinit(num_msf_bufs, M_TEMP, &msf_buf_hashmask);
113         TAILQ_INIT(&msf_buf_freelist);
114
115         msf_base = kmem_alloc_nofault(kernel_map,
116                                         num_msf_bufs * XIO_INTERNAL_SIZE);
117
118         /* 
119          * Use contig. memory for the maps, so it is quicker to access
120          * linear maps, and they can also be passed to device
121          * buffers (in the future).
122          */
123         msf_bufs = malloc(num_msf_bufs * sizeof(struct msf_buf), M_MSFBUF,
124                         M_WAITOK|M_ZERO);
125
126         /* Initialize the free list with necessary information. */
127         for (i = 0; i < num_msf_bufs; i++) {
128                 msf_bufs[i].m_kva = msf_base + i * XIO_INTERNAL_SIZE;
129                 msf_bufs[i].m_flags |= SFBA_ONFREEQ;
130                 xio_init(&msf_bufs[i].m_xio);
131                 TAILQ_INSERT_TAIL(&msf_buf_freelist, &msf_bufs[i], free_list);
132         }
133 }
134 SYSINIT(msf_buf, SI_SUB_MBUF, SI_ORDER_ANY, msf_buf_init, NULL);
135
136 /*
137  * Hash the base page of an MSF's array of pages.
138  */
139 static __inline
140 int
141 msf_buf_hash(vm_page_t base_m)
142 {
143     int hv;
144
145     hv = ((int)base_m / sizeof(vm_page_t)) + ((int)base_m >> 12);
146     return(hv & msf_buf_hashmask);
147 }
148
149 /*
150  * Get an msf_buf from the freelist; if none are available
151  * than it will block.
152  *
153  * If SFBA_PCATCH was specified in 'flags' than the sleep is
154  * block is interruptable by signals etc; this flag is normally
155  * use for system calls.
156  *
157  */
158 struct msf_buf *
159 msf_buf_alloc(vm_page_t *pg_ary, int npages, int flags)
160 {
161         struct msf_buf_list *hash_chain;
162         struct msf_buf *msf;
163         globaldata_t gd;
164         int error, pflags;
165         int i;
166
167         KKASSERT(npages != 0 && npages <= XIO_INTERNAL_SIZE);
168
169         gd = mycpu;
170         crit_enter();
171         hash_chain = &msf_buf_hashtable[msf_buf_hash(*pg_ary)];
172         LIST_FOREACH(msf, hash_chain, active_list) {
173                 if (msf->m_xio.xio_npages == npages) {
174                         for (i = npages - 1; i >= 0; --i) {
175                                 if (msf->m_xio.xio_pages[i] != pg_ary[i])
176                                         break;
177                         }
178                         if (i >= 0)
179                                 continue;
180                         /*
181                          * found existing mapping
182                          */
183                         if (msf->m_flags & SFBA_ONFREEQ) {
184                             TAILQ_REMOVE(&msf_buf_freelist, msf, free_list);
185                             msf->m_flags &= ~SFBA_ONFREEQ;
186                         }
187
188                         goto done;
189                 }
190         }
191
192         /*
193          * Didn't find old mapping.  Get a buffer off the freelist.  We
194          * may have to remove and skip buffers with non-zero ref counts 
195          * that were lazily allocated.
196          *
197          * If the freelist is empty, we block until something becomes
198          * available.  This usually happens pretty quickly because sf_bufs
199          * and msf_bufs are supposed to be temporary mappings.
200          */
201         while ((msf = TAILQ_FIRST(&msf_buf_freelist)) == NULL) {
202                 pflags = (flags & SFBA_PCATCH) ? PCATCH : 0;
203                 ++msf_buf_alloc_want;
204                 error = tsleep(&msf_buf_freelist, pflags, "msfbuf", 0);
205                 --msf_buf_alloc_want;
206                 if (error)
207                         goto done2;
208         }
209         
210         /*
211          * We are finished when we find an msf_buf with ref. count of
212          * 0.  Theoretically, we do not have to remove it from the
213          * freelist but it's a good idea to do so to preserve LRU
214          * operation for the (1) never seen before case and
215          * (2) accidently recycled due to prior cached uses not removing
216          * the buffer case.
217          */
218         KKASSERT(msf->m_flags & SFBA_ONFREEQ);
219         TAILQ_REMOVE(&msf_buf_freelist, msf, free_list);
220         msf->m_flags &= ~SFBA_ONFREEQ;
221
222         /* Remove previous mapping from hash table and overwrite new one */
223         if (msf->m_xio.xio_pages[0] != NULL)
224                 LIST_REMOVE(msf, active_list);
225          
226         LIST_INSERT_HEAD(hash_chain, msf, active_list);
227
228         for (i = 0; i < npages; ++i)
229                 msf->m_xio.xio_pages[i] = pg_ary[i];
230
231         msf->m_xio.xio_npages = npages;
232         msf->m_xio.xio_bytes = npages * PAGE_SIZE;
233
234         /*
235          * Successful MSF setup, bump the ref count and enter the pages.
236          */
237 done:
238         ++msf->m_refcnt;
239         if ((flags & SFBA_QUICK)) {
240                 pmap_qenter2(msf->m_kva, msf->m_xio.xio_pages, 
241                             msf->m_xio.xio_npages, &msf->m_cpumask);
242         } else {
243                 pmap_qenter(msf->m_kva, msf->m_xio.xio_pages,
244                             msf->m_xio.xio_npages);
245                 msf->m_cpumask = (cpumask_t)-1;
246         }
247 done2:
248         crit_exit();
249         return (msf);
250 }
251
252 #if 0
253 /*
254  * Add a reference to a buffer (currently unused)
255  */
256 void
257 msf_buf_ref(struct msf_buf *msf)
258 {
259         if (msf->m_refcnt == 0)
260                 panic("msf_buf_ref: referencing a free msf_buf");
261         crit_enter();
262         ++msf->m_refcnt;
263         crit_exit();
264 }
265 #endif
266
267 /*
268  * Lose a reference to an msf_buf. When none left, detach mapped page
269  * and release resources back to the system.  Note that the sfbuf's
270  * removal from the freelist is delayed, so it may in fact already be
271  * on the free list.  This is the optimal (and most likely) scenario.
272  *
273  * Must be called at splimp.
274  */
275 void
276 msf_buf_free(struct msf_buf *msf)
277 {
278         crit_enter();
279         KKASSERT(msf->m_refcnt > 0);
280
281         msf->m_refcnt--;
282         if (msf->m_refcnt == 0) {
283                 KKASSERT((msf->m_flags & SFBA_ONFREEQ) == 0);
284                 TAILQ_INSERT_TAIL(&msf_buf_freelist, msf, free_list);
285                 msf->m_flags |= SFBA_ONFREEQ;
286                 if (msf_buf_alloc_want > 0)
287                         wakeup_one(&msf_buf_freelist);
288         }
289         crit_exit();
290 }