kern_physio.c revision 1.90 1 /* $NetBSD: kern_physio.c,v 1.90 2009/05/18 21:12:33 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
37 */
38
39 /*-
40 * Copyright (c) 1994 Christopher G. Demetriou
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)kern_physio.c 8.1 (Berkeley) 6/10/93
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: kern_physio.c,v 1.90 2009/05/18 21:12:33 ad Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/once.h>
81 #include <sys/workqueue.h>
82 #include <sys/kmem.h>
83
84 #include <uvm/uvm_extern.h>
85
86 ONCE_DECL(physio_initialized);
87 struct workqueue *physio_workqueue;
88
89 int physio_concurrency = 16;
90
91 /* #define PHYSIO_DEBUG */
92 #if defined(PHYSIO_DEBUG)
93 #define DPRINTF(a) printf a
94 #else /* defined(PHYSIO_DEBUG) */
95 #define DPRINTF(a) /* nothing */
96 #endif /* defined(PHYSIO_DEBUG) */
97
98 struct physio_stat {
99 int ps_running;
100 int ps_error;
101 int ps_failed;
102 off_t ps_endoffset;
103 buf_t *ps_orig_bp;
104 kmutex_t ps_lock;
105 kcondvar_t ps_cv;
106 };
107
108 static void
109 physio_done(struct work *wk, void *dummy)
110 {
111 struct buf *bp = (void *)wk;
112 size_t todo = bp->b_bufsize;
113 size_t done = bp->b_bcount - bp->b_resid;
114 struct physio_stat *ps = bp->b_private;
115
116 KASSERT(&bp->b_work == wk);
117 KASSERT(bp->b_bcount <= todo);
118 KASSERT(bp->b_resid <= bp->b_bcount);
119 KASSERT((bp->b_flags & B_PHYS) != 0);
120 KASSERT(dummy == NULL);
121
122 vunmapbuf(bp, todo);
123 uvm_vsunlock(bp->b_proc->p_vmspace, bp->b_data, todo);
124
125 mutex_enter(&ps->ps_lock);
126 if (__predict_false(done != todo)) {
127 off_t endoffset = dbtob(bp->b_blkno) + done;
128
129 /*
130 * we got an error or hit EOM.
131 *
132 * we only care about the first one.
133 * ie. the one at the lowest offset.
134 */
135
136 KASSERT(ps->ps_endoffset != endoffset);
137 DPRINTF(("%s: error=%d at %" PRIu64 " - %" PRIu64
138 ", blkno=%" PRIu64 ", bcount=%d, flags=0x%x\n",
139 __func__, bp->b_error, dbtob(bp->b_blkno), endoffset,
140 bp->b_blkno, bp->b_bcount, bp->b_flags));
141
142 if (ps->ps_endoffset == -1 || endoffset < ps->ps_endoffset) {
143 DPRINTF(("%s: ps=%p, error %d -> %d, endoff %" PRIu64
144 " -> %" PRIu64 "\n",
145 __func__, ps,
146 ps->ps_error, bp->b_error,
147 ps->ps_endoffset, endoffset));
148
149 ps->ps_endoffset = endoffset;
150 ps->ps_error = bp->b_error;
151 }
152 ps->ps_failed++;
153 } else {
154 KASSERT(bp->b_error == 0);
155 }
156
157 ps->ps_running--;
158 cv_signal(&ps->ps_cv);
159 mutex_exit(&ps->ps_lock);
160
161 if (bp != ps->ps_orig_bp)
162 putiobuf(bp);
163 }
164
165 static void
166 physio_biodone(struct buf *bp)
167 {
168 #if defined(DIAGNOSTIC)
169 struct physio_stat *ps = bp->b_private;
170 size_t todo = bp->b_bufsize;
171 size_t done = bp->b_bcount - bp->b_resid;
172
173 KASSERT(ps->ps_running > 0);
174 KASSERT(bp->b_bcount <= todo);
175 KASSERT(bp->b_resid <= bp->b_bcount);
176 if (done == todo)
177 KASSERT(bp->b_error == 0);
178 #endif /* defined(DIAGNOSTIC) */
179
180 workqueue_enqueue(physio_workqueue, &bp->b_work, NULL);
181 }
182
183 static void
184 physio_wait(struct physio_stat *ps, int n)
185 {
186
187 KASSERT(mutex_owned(&ps->ps_lock));
188
189 while (ps->ps_running > n)
190 cv_wait(&ps->ps_cv, &ps->ps_lock);
191 }
192
193 static int
194 physio_init(void)
195 {
196 int error;
197
198 KASSERT(physio_workqueue == NULL);
199
200 error = workqueue_create(&physio_workqueue, "physiod",
201 physio_done, NULL, PRI_BIO, IPL_BIO, WQ_MPSAFE);
202
203 return error;
204 }
205
206 /*
207 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
208 * from the raw device to user buffers, and bypasses the buffer cache.
209 */
210 int
211 physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
212 void (*min_phys)(struct buf *), struct uio *uio)
213 {
214 struct iovec *iovp;
215 struct lwp *l = curlwp;
216 struct proc *p = l->l_proc;
217 int i, error;
218 struct buf *bp = NULL;
219 struct physio_stat *ps;
220 int concurrency = physio_concurrency - 1;
221
222 error = RUN_ONCE(&physio_initialized, physio_init);
223 if (__predict_false(error != 0)) {
224 return error;
225 }
226
227 DPRINTF(("%s: called: off=%" PRIu64 ", resid=%zu\n",
228 __func__, uio->uio_offset, uio->uio_resid));
229
230 flags &= B_READ | B_WRITE;
231
232 if ((ps = kmem_zalloc(sizeof(*ps), KM_SLEEP)) == NULL)
233 return ENOMEM;
234 /* ps->ps_running = 0; */
235 /* ps->ps_error = 0; */
236 /* ps->ps_failed = 0; */
237 ps->ps_orig_bp = obp;
238 ps->ps_endoffset = -1;
239 mutex_init(&ps->ps_lock, MUTEX_DEFAULT, IPL_NONE);
240 cv_init(&ps->ps_cv, "physio");
241
242 /* Make sure we have a buffer, creating one if necessary. */
243 if (obp != NULL) {
244 mutex_enter(&bufcache_lock);
245 /* Mark it busy, so nobody else will use it. */
246 while (bbusy(obp, false, 0, NULL) == EPASSTHROUGH)
247 ;
248 mutex_exit(&bufcache_lock);
249 concurrency = 0; /* see "XXXkludge" comment below */
250 }
251
252 for (i = 0; i < uio->uio_iovcnt; i++) {
253 bool sync = true;
254
255 iovp = &uio->uio_iov[i];
256 while (iovp->iov_len > 0) {
257 size_t todo;
258 vaddr_t endp;
259
260 mutex_enter(&ps->ps_lock);
261 if (ps->ps_failed != 0) {
262 goto done_locked;
263 }
264 physio_wait(ps, sync ? 0 : concurrency);
265 mutex_exit(&ps->ps_lock);
266 if (obp != NULL) {
267 /*
268 * XXXkludge
269 * some drivers use "obp" as an identifier.
270 */
271 bp = obp;
272 } else {
273 bp = getiobuf(NULL, true);
274 bp->b_cflags = BC_BUSY;
275 }
276 bp->b_dev = dev;
277 bp->b_proc = p;
278 bp->b_private = ps;
279
280 /*
281 * Mrk the buffer busy for physical I/O. Also set
282 * B_PHYS because it's an I/O to user memory, and
283 * B_RAW because B_RAW is to be "set by physio for
284 * raw transfers".
285 */
286 bp->b_oflags = 0;
287 bp->b_cflags = BC_BUSY;
288 bp->b_flags = flags | B_PHYS | B_RAW;
289 bp->b_iodone = physio_biodone;
290
291 /* Set up the buffer for a maximum-sized transfer. */
292 bp->b_blkno = btodb(uio->uio_offset);
293 if (dbtob(bp->b_blkno) != uio->uio_offset) {
294 error = EINVAL;
295 goto done;
296 }
297 bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
298 bp->b_data = iovp->iov_base;
299
300 /*
301 * Call minphys to bound the transfer size,
302 * and remember the amount of data to transfer,
303 * for later comparison.
304 */
305 (*min_phys)(bp);
306 todo = bp->b_bufsize = bp->b_bcount;
307 #if defined(DIAGNOSTIC)
308 if (todo > MAXPHYS)
309 panic("todo(%zu) > MAXPHYS; minphys broken",
310 todo);
311 #endif /* defined(DIAGNOSTIC) */
312
313 sync = false;
314 endp = (vaddr_t)bp->b_data + todo;
315 if (trunc_page(endp) != endp) {
316 /*
317 * Following requests can overlap.
318 * note that uvm_vslock does round_page.
319 */
320 sync = true;
321 }
322
323 /*
324 * Lock the part of the user address space involved
325 * in the transfer. Beware vmapbuf(); it clobbers
326 * b_data and saves it in b_saveaddr. However,
327 * vunmapbuf() restores it.
328 */
329 error = uvm_vslock(p->p_vmspace, bp->b_data, todo,
330 (flags & B_READ) ? VM_PROT_WRITE : VM_PROT_READ);
331 if (error) {
332 goto done;
333 }
334 vmapbuf(bp, todo);
335
336 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
337
338 mutex_enter(&ps->ps_lock);
339 ps->ps_running++;
340 mutex_exit(&ps->ps_lock);
341
342 /* Call strategy to start the transfer. */
343 (*strategy)(bp);
344 bp = NULL;
345
346 iovp->iov_len -= todo;
347 iovp->iov_base = (char *)iovp->iov_base + todo;
348 uio->uio_offset += todo;
349 uio->uio_resid -= todo;
350 }
351 }
352
353 done:
354 mutex_enter(&ps->ps_lock);
355 done_locked:
356 physio_wait(ps, 0);
357 mutex_exit(&ps->ps_lock);
358
359 if (ps->ps_failed != 0) {
360 off_t delta;
361
362 delta = uio->uio_offset - ps->ps_endoffset;
363 KASSERT(delta > 0);
364 uio->uio_resid += delta;
365 /* uio->uio_offset = ps->ps_endoffset; */
366 } else {
367 KASSERT(ps->ps_endoffset == -1);
368 }
369 if (bp != NULL && bp != obp) {
370 putiobuf(bp);
371 }
372 if (error == 0) {
373 error = ps->ps_error;
374 }
375 mutex_destroy(&ps->ps_lock);
376 cv_destroy(&ps->ps_cv);
377 kmem_free(ps, sizeof(*ps));
378
379 /*
380 * Clean up the state of the buffer. Remember if somebody wants
381 * it, so we can wake them up below. Also, if we had to steal it,
382 * give it back.
383 */
384 if (obp != NULL) {
385 KASSERT((obp->b_cflags & BC_BUSY) != 0);
386
387 /*
388 * If another process is waiting for the raw I/O buffer,
389 * wake up processes waiting to do physical I/O;
390 */
391 mutex_enter(&bufcache_lock);
392 obp->b_cflags &= ~(BC_BUSY | BC_WANTED);
393 obp->b_flags &= ~(B_PHYS | B_RAW);
394 obp->b_iodone = NULL;
395 cv_broadcast(&obp->b_busy);
396 mutex_exit(&bufcache_lock);
397 }
398
399 DPRINTF(("%s: done: off=%" PRIu64 ", resid=%zu\n",
400 __func__, uio->uio_offset, uio->uio_resid));
401
402 return error;
403 }
404
405 /*
406 * A minphys() routine is called by physio() to adjust the size of each
407 * I/O transfer before the latter is passed to the strategy routine.
408 *
409 * This minphys() is a default that must be called to enforce limits
410 * that are applicable to all devices, because of limitations in the
411 * kernel or the hardware platform.
412 */
413 void
414 minphys(struct buf *bp)
415 {
416
417 if (bp->b_bcount > MAXPHYS)
418 bp->b_bcount = MAXPHYS;
419 }
420