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