2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
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>.
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
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
39 * $DragonFly: src/sys/kern/kern_msfbuf.c,v 1.6 2004/07/21 12:44:25 hmp Exp $
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.
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.
51 * MSFBUFs are primarily designed to be used in subsystems that manipulate
52 * XIOs. The DEV and BUF subsystems are a good example.
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).
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>
73 #include <sys/msfbuf.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
78 #include <vm/vm_page.h>
81 MALLOC_DECLARE(M_MSFBUF);
82 MALLOC_DEFINE(M_MSFBUF, "MSFBUF", "direct-copy buffers");
84 /* lists and queues associated with msf_bufs */
85 LIST_HEAD(msf_buf_list, msf_buf);
87 TAILQ_HEAD(, msf_buf) msf_buf_freelist;
89 /* hash table for tracking msf_bufs */
90 static struct msf_buf_list *msf_buf_hashtable;
91 static u_long msf_buf_hashmask;
93 /* indicate shortage of available msf_bufs */
94 static u_int msf_buf_alloc_want;
96 /* base of the msf_buf map */
97 static vm_offset_t msf_base;
98 static struct msf_buf *msf_bufs;
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");
105 msf_buf_init(void *__dummy)
109 msf_buf_alloc_want = 0;
110 TUNABLE_INT_FETCH("kern.ipc.msfbufs", &num_msf_bufs);
112 msf_buf_hashtable = hashinit(num_msf_bufs, M_TEMP, &msf_buf_hashmask);
113 TAILQ_INIT(&msf_buf_freelist);
115 msf_base = kmem_alloc_nofault(kernel_map,
116 num_msf_bufs * XIO_INTERNAL_SIZE);
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).
123 msf_bufs = malloc(num_msf_bufs * sizeof(struct msf_buf), M_MSFBUF,
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);
134 SYSINIT(msf_buf, SI_SUB_MBUF, SI_ORDER_ANY, msf_buf_init, NULL);
137 * Hash the base page of an MSF's array of pages.
141 msf_buf_hash(vm_page_t base_m)
145 hv = ((int)base_m / sizeof(vm_page_t)) + ((int)base_m >> 12);
146 return(hv & msf_buf_hashmask);
150 * Get an msf_buf from the freelist; if none are available
151 * than it will block.
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.
159 msf_buf_alloc(vm_page_t *pg_ary, int npages, int flags)
161 struct msf_buf_list *hash_chain;
167 KKASSERT(npages != 0 && npages <= XIO_INTERNAL_SIZE);
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])
181 * found existing mapping
183 if (msf->m_flags & SFBA_ONFREEQ) {
184 TAILQ_REMOVE(&msf_buf_freelist, msf, free_list);
185 msf->m_flags &= ~SFBA_ONFREEQ;
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.
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.
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;
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
218 KKASSERT(msf->m_flags & SFBA_ONFREEQ);
219 TAILQ_REMOVE(&msf_buf_freelist, msf, free_list);
220 msf->m_flags &= ~SFBA_ONFREEQ;
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);
226 LIST_INSERT_HEAD(hash_chain, msf, active_list);
228 for (i = 0; i < npages; ++i)
229 msf->m_xio.xio_pages[i] = pg_ary[i];
231 msf->m_xio.xio_npages = npages;
232 msf->m_xio.xio_bytes = npages * PAGE_SIZE;
235 * Successful MSF setup, bump the ref count and enter the pages.
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);
243 pmap_qenter(msf->m_kva, msf->m_xio.xio_pages,
244 msf->m_xio.xio_npages);
245 msf->m_cpumask = (cpumask_t)-1;
254 * Add a reference to a buffer (currently unused)
257 msf_buf_ref(struct msf_buf *msf)
259 if (msf->m_refcnt == 0)
260 panic("msf_buf_ref: referencing a free msf_buf");
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.
273 * Must be called at splimp.
276 msf_buf_free(struct msf_buf *msf)
279 KKASSERT(msf->m_refcnt > 0);
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);