kern_physio.c revision 1.80.2.8 1 /* $NetBSD: kern_physio.c,v 1.80.2.8 2007/10/09 15:22:19 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.80.2.8 2007/10/09 15:22:19 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 /*
90 * The routines implemented in this file are described in:
91 * Leffler, et al.: The Design and Implementation of the 4.3BSD
92 * UNIX Operating System (Addison Welley, 1989)
93 * on pages 231-233.
94 *
95 * The routines "getphysbuf" and "putphysbuf" steal and return a swap
96 * buffer. Leffler, et al., says that swap buffers are used to do the
97 * I/O, so raw I/O requests don't have to be single-threaded. Of course,
98 * NetBSD doesn't use "swap buffers" -- we have our own memory pool for
99 * buffer descriptors.
100 */
101
102 /* #define PHYSIO_DEBUG */
103 #if defined(PHYSIO_DEBUG)
104 #define DPRINTF(a) printf a
105 #else /* defined(PHYSIO_DEBUG) */
106 #define DPRINTF(a) /* nothing */
107 #endif /* defined(PHYSIO_DEBUG) */
108
109 struct physio_stat {
110 int ps_running;
111 int ps_error;
112 int ps_failed;
113 off_t ps_endoffset;
114 kmutex_t ps_lock;
115 kcondvar_t ps_cv;
116 };
117
118 /* abuse these flags of struct buf */
119 #define BC_DONTFREE BC_AGE
120
121 /*
122 * allocate a buffer structure for use in physical I/O.
123 */
124 static struct buf *
125 getphysbuf(void)
126 {
127 struct buf *bp;
128
129 bp = getiobuf(NULL, true);
130 bp->b_error = 0;
131 bp->b_cflags = BC_BUSY;
132 return(bp);
133 }
134
135 /*
136 * get rid of a swap buffer structure which has been used in physical I/O.
137 */
138 static void
139 putphysbuf(struct buf *bp)
140 {
141
142 if ((bp->b_cflags & BC_DONTFREE) != 0) {
143 return;
144 }
145
146 if (__predict_false(bp->b_cflags & BC_WANTED))
147 panic("putphysbuf: private buf BC_WANTED");
148 putiobuf(bp);
149 }
150
151 static void
152 physio_done(struct work *wk, void *dummy)
153 {
154 struct buf *bp = (void *)wk;
155 size_t todo = bp->b_bufsize;
156 size_t done = bp->b_bcount - bp->b_resid;
157 struct physio_stat *ps = bp->b_private;
158
159 KASSERT(&bp->b_work == wk);
160 KASSERT(bp->b_bcount <= todo);
161 KASSERT(bp->b_resid <= bp->b_bcount);
162 KASSERT((bp->b_flags & B_PHYS) != 0);
163 KASSERT(dummy == NULL);
164
165 vunmapbuf(bp, todo);
166 uvm_vsunlock(bp->b_proc->p_vmspace, bp->b_data, todo);
167
168 mutex_enter(&ps->ps_lock);
169 if (__predict_false(done != todo)) {
170 off_t endoffset = dbtob(bp->b_blkno) + done;
171
172 /*
173 * we got an error or hit EOM.
174 *
175 * we only care about the first one.
176 * ie. the one at the lowest offset.
177 */
178
179 KASSERT(ps->ps_endoffset != endoffset);
180 DPRINTF(("%s: error=%d at %" PRIu64 " - %" PRIu64
181 ", blkno=%" PRIu64 ", bcount=%d, flags=0x%x\n",
182 __func__, bp->b_error, dbtob(bp->b_blkno), endoffset,
183 bp->b_blkno, bp->b_bcount, bp->b_flags));
184
185 if (ps->ps_endoffset == -1 || endoffset < ps->ps_endoffset) {
186 DPRINTF(("%s: ps=%p, error %d -> %d, endoff %" PRIu64
187 " -> %" PRIu64 "\n",
188 __func__, ps,
189 ps->ps_error, bp->b_error,
190 ps->ps_endoffset, endoffset));
191
192 ps->ps_endoffset = endoffset;
193 ps->ps_error = bp->b_error;
194 }
195 ps->ps_failed++;
196 } else {
197 KASSERT(bp->b_error == 0);
198 }
199
200 ps->ps_running--;
201 cv_signal(&ps->ps_cv);
202 mutex_exit(&ps->ps_lock);
203
204 putphysbuf(bp);
205 }
206
207 static void
208 physio_biodone(struct buf *bp)
209 {
210 #if defined(DIAGNOSTIC)
211 struct physio_stat *ps = bp->b_private;
212 size_t todo = bp->b_bufsize;
213
214 KASSERT(ps->ps_running > 0);
215 KASSERT(bp->b_bcount <= todo);
216 KASSERT(bp->b_resid <= bp->b_bcount);
217 #endif /* defined(DIAGNOSTIC) */
218
219 workqueue_enqueue(physio_workqueue, &bp->b_work, NULL);
220 }
221
222 static void
223 physio_wait(struct physio_stat *ps, int n)
224 {
225
226 KASSERT(mutex_owned(&ps->ps_lock));
227
228 while (ps->ps_running > n)
229 cv_wait(&ps->ps_cv, &ps->ps_lock);
230 }
231
232 static int
233 physio_init(void)
234 {
235 int error;
236
237 KASSERT(physio_workqueue == NULL);
238
239 error = workqueue_create(&physio_workqueue, "physiod",
240 physio_done, NULL, PRIBIO, IPL_BIO, 0);
241
242 return error;
243 }
244
245 #define PHYSIO_CONCURRENCY 16 /* XXX tune */
246
247 /*
248 * Do "physical I/O" on behalf of a user. "Physical I/O" is I/O directly
249 * from the raw device to user buffers, and bypasses the buffer cache.
250 *
251 * Comments in brackets are from Leffler, et al.'s pseudo-code implementation.
252 */
253 int
254 physio(void (*strategy)(struct buf *), struct buf *obp, dev_t dev, int flags,
255 void (*min_phys)(struct buf *), struct uio *uio)
256 {
257 struct iovec *iovp;
258 struct lwp *l = curlwp;
259 struct proc *p = l->l_proc;
260 int i, error;
261 struct buf *bp = NULL;
262 struct physio_stat *ps;
263 int concurrency = PHYSIO_CONCURRENCY - 1;
264
265 error = RUN_ONCE(&physio_initialized, physio_init);
266 if (__predict_false(error != 0)) {
267 return error;
268 }
269
270 DPRINTF(("%s: called: off=%" PRIu64 ", resid=%zu\n",
271 __func__, uio->uio_offset, uio->uio_resid));
272
273 flags &= B_READ | B_WRITE;
274
275 if ((ps = kmem_zalloc(sizeof(*ps), KM_SLEEP)) == NULL)
276 return ENOMEM;
277 /* ps->ps_running = 0; */
278 /* ps->ps_error = 0; */
279 /* ps->ps_failed = 0; */
280 ps->ps_endoffset = -1;
281 mutex_init(&ps->ps_lock, MUTEX_DEFAULT, IPL_NONE);
282 cv_init(&ps->ps_cv, "physio");
283
284 /* Make sure we have a buffer, creating one if necessary. */
285 if (obp != NULL) {
286 /* [raise the processor priority level to splbio;] */
287 mutex_enter(&bufcache_lock);
288 while (bbusy(obp, false, 0) == EPASSTHROUGH)
289 ;
290 /* Mark it busy, so nobody else will use it. */
291 obp->b_cflags |= BC_DONTFREE;
292 mutex_exit(&bufcache_lock);
293 concurrency = 0; /* see "XXXkludge" comment below */
294 }
295
296 uvm_lwp_hold(l);
297
298 for (i = 0; i < uio->uio_iovcnt; i++) {
299 bool sync = true;
300
301 iovp = &uio->uio_iov[i];
302 while (iovp->iov_len > 0) {
303 size_t todo;
304 vaddr_t endp;
305
306 mutex_enter(&ps->ps_lock);
307 if (ps->ps_failed != 0) {
308 goto done_locked;
309 }
310 physio_wait(ps, sync ? 0 : concurrency);
311 mutex_exit(&ps->ps_lock);
312 if (obp != NULL) {
313 /*
314 * XXXkludge
315 * some drivers use "obp" as an identifier.
316 */
317 bp = obp;
318 } else {
319 bp = getphysbuf();
320 }
321 bp->b_dev = dev;
322 bp->b_proc = p;
323 bp->b_private = ps;
324 bp->b_vp = NULL;
325
326 /*
327 * [mark the buffer busy for physical I/O]
328 * (i.e. set B_PHYS (because it's an I/O to user
329 * memory, and B_RAW, because B_RAW is to be
330 * "Set by physio for raw transfers.", in addition
331 * to the "busy" and read/write flag.)
332 */
333 bp->b_oflags = 0;
334 bp->b_cflags = (bp->b_cflags & BC_DONTFREE) | BC_BUSY;
335 bp->b_flags = flags | B_PHYS | B_RAW;
336 bp->b_iodone = physio_biodone;
337
338 /* [set up the buffer for a maximum-sized transfer] */
339 bp->b_blkno = btodb(uio->uio_offset);
340 if (dbtob(bp->b_blkno) != uio->uio_offset) {
341 error = EINVAL;
342 goto done;
343 }
344 bp->b_bcount = MIN(MAXPHYS, iovp->iov_len);
345 bp->b_data = iovp->iov_base;
346
347 /*
348 * [call minphys to bound the transfer size]
349 * and remember the amount of data to transfer,
350 * for later comparison.
351 */
352 (*min_phys)(bp);
353 todo = bp->b_bufsize = bp->b_bcount;
354 #if defined(DIAGNOSTIC)
355 if (todo > MAXPHYS)
356 panic("todo(%zu) > MAXPHYS; minphys broken",
357 todo);
358 #endif /* defined(DIAGNOSTIC) */
359
360 sync = false;
361 endp = (vaddr_t)bp->b_data + todo;
362 if (trunc_page(endp) != endp) {
363 /*
364 * following requests can overlap.
365 * note that uvm_vslock does round_page.
366 */
367 sync = true;
368 }
369
370 /*
371 * [lock the part of the user address space involved
372 * in the transfer]
373 * Beware vmapbuf(); it clobbers b_data and
374 * saves it in b_saveaddr. However, vunmapbuf()
375 * restores it.
376 */
377 error = uvm_vslock(p->p_vmspace, bp->b_data, todo,
378 (flags & B_READ) ? VM_PROT_WRITE : VM_PROT_READ);
379 if (error) {
380 goto done;
381 }
382 vmapbuf(bp, todo);
383
384 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
385
386 mutex_enter(&ps->ps_lock);
387 ps->ps_running++;
388 mutex_exit(&ps->ps_lock);
389
390 /* [call strategy to start the transfer] */
391 (*strategy)(bp);
392 bp = NULL;
393
394 iovp->iov_len -= todo;
395 iovp->iov_base = (char *)iovp->iov_base + todo;
396 uio->uio_offset += todo;
397 uio->uio_resid -= todo;
398 }
399 }
400
401 done:
402 mutex_enter(&ps->ps_lock);
403 done_locked:
404 physio_wait(ps, 0);
405 mutex_exit(&ps->ps_lock);
406
407 if (ps->ps_failed != 0) {
408 off_t delta;
409
410 delta = uio->uio_offset - ps->ps_endoffset;
411 KASSERT(delta > 0);
412 uio->uio_resid += delta;
413 /* uio->uio_offset = ps->ps_endoffset; */
414 } else {
415 KASSERT(ps->ps_endoffset == -1);
416 }
417 if (bp != NULL) {
418 putphysbuf(bp);
419 }
420 if (error == 0) {
421 error = ps->ps_error;
422 }
423 mutex_destroy(&ps->ps_lock);
424 cv_destroy(&ps->ps_cv);
425 kmem_free(ps, sizeof(*ps));
426
427 /*
428 * [clean up the state of the buffer]
429 * Remember if somebody wants it, so we can wake them up below.
430 * Also, if we had to steal it, give it back.
431 */
432 if (obp != NULL) {
433 KASSERT((obp->b_cflags & BC_BUSY) != 0);
434 KASSERT((obp->b_cflags & BC_DONTFREE) != 0);
435
436 /*
437 * [if another process is waiting for the raw I/O buffer,
438 * wake up processes waiting to do physical I/O;
439 */
440 mutex_enter(&bufcache_lock);
441 obp->b_cflags &= ~(BC_DONTFREE | BC_BUSY | BC_WANTED);
442 obp->b_flags &= ~(B_PHYS | B_RAW);
443 obp->b_iodone = NULL;
444 cv_broadcast(&obp->b_busy);
445 mutex_exit(&bufcache_lock);
446 }
447 uvm_lwp_rele(l);
448
449 DPRINTF(("%s: done: off=%" PRIu64 ", resid=%zu\n",
450 __func__, uio->uio_offset, uio->uio_resid));
451
452 return error;
453 }
454
455 /*
456 * Leffler, et al., says on p. 231:
457 * "The minphys() routine is called by physio() to adjust the
458 * size of each I/O transfer before the latter is passed to
459 * the strategy routine..."
460 *
461 * so, just adjust the buffer's count accounting to MAXPHYS here,
462 * and return the new count;
463 */
464 void
465 minphys(struct buf *bp)
466 {
467
468 if (bp->b_bcount > MAXPHYS)
469 bp->b_bcount = MAXPHYS;
470 }
471