Implement a bounce buffer for physio if the buffer passed from userland
authorMatthew Dillon <dillon@dragonflybsd.org>
Sun, 10 Aug 2008 20:03:15 +0000 (20:03 +0000)
committerMatthew Dillon <dillon@dragonflybsd.org>
Sun, 10 Aug 2008 20:03:15 +0000 (20:03 +0000)
is not at least 16-byte aligned.

Reported-by: "Steve O'Hara-Smith" <steve@sohara.org>, and others
sys/kern/kern_physio.c
sys/kern/vfs_bio.c
sys/sys/buf.h

index d8aed75..19744bb 100644 (file)
@@ -17,7 +17,7 @@
  *    are met.
  *
  * $FreeBSD: src/sys/kern/kern_physio.c,v 1.46.2.4 2003/11/14 09:51:47 simokawa Exp $
- * $DragonFly: src/sys/kern/kern_physio.c,v 1.25 2007/08/21 17:26:45 dillon Exp $
+ * $DragonFly: src/sys/kern/kern_physio.c,v 1.26 2008/08/10 20:03:14 dillon Exp $
  */
 
 #include <sys/param.h>
@@ -48,6 +48,8 @@ physio(cdev_t dev, struct uio *uio, int ioflag)
        int saflags;
        int iolen;
        int bcount;
+       int bounceit;
+       caddr_t ubase;
        struct buf *bp;
 
        bp = getpbuf(NULL);
@@ -55,7 +57,7 @@ physio(cdev_t dev, struct uio *uio, int ioflag)
        error = 0;
 
        /* XXX: sanity check */
-       if(dev->si_iosize_max < PAGE_SIZE) {
+       if (dev->si_iosize_max < PAGE_SIZE) {
                kprintf("WARNING: %s si_iosize_max=%d, using DFLTPHYS.\n",
                    devtoname(dev), dev->si_iosize_max);
                dev->si_iosize_max = DFLTPHYS;
@@ -83,25 +85,53 @@ physio(cdev_t dev, struct uio *uio, int ioflag)
                        bp->b_bio1.bio_offset = uio->uio_offset;
                        bp->b_bio1.bio_done = physwakeup;
 
-                       /* Don't exceed drivers iosize limit */
+                       /* 
+                        * Setup for mapping the request into kernel memory.
+                        *
+                        * We can only write as much as fits in a pbuf,
+                        * which is MAXPHYS, and no larger then the device's
+                        * ability.
+                        *
+                        * If not using bounce pages the base address of the
+                        * user mapping into the pbuf may be offset, further
+                        * reducing how much will actually fit in the pbuf.
+                        */
                        if (bcount > dev->si_iosize_max)
                                bcount = dev->si_iosize_max;
 
-                       /* 
-                        * Make sure the pbuf can map the request
-                        * XXX: The pbuf has kvasize = MAXPHYS so a request
-                        * XXX: larger than MAXPHYS - PAGE_SIZE must be
-                        * XXX: page aligned or it will be fragmented.
-                        */
-                       iolen = ((vm_offset_t) uio->uio_iov[i].iov_base) &
-                               PAGE_MASK;
-                       if ((bcount + iolen) > bp->b_kvasize) {
-                               bcount = bp->b_kvasize;
-                               if (iolen != 0)
-                                       bcount -= PAGE_SIZE;
+                       ubase = uio->uio_iov[i].iov_base;
+                       bounceit = (int)(((vm_offset_t)ubase) & 15);
+                       iolen = ((vm_offset_t)ubase) & PAGE_MASK;
+                       if (bounceit) {
+                               if (bcount > bp->b_kvasize)
+                                       bcount = bp->b_kvasize;
+                       } else {
+                               if ((bcount + iolen) > bp->b_kvasize) {
+                                       bcount = bp->b_kvasize;
+                                       if (iolen != 0)
+                                               bcount -= PAGE_SIZE;
+                               }
                        }
+
+                       /*
+                        * If we have to use a bounce buffer allocate kernel
+                        * memory and copyin/copyout.  Otherwise map the
+                        * user buffer directly into kernel memory without
+                        * copying.
+                        */
                        if (uio->uio_segflg == UIO_USERSPACE) {
-                               if (vmapbuf(bp, uio->uio_iov[i].iov_base, bcount) < 0) {
+                               if (bounceit) {
+                                       bp->b_data = bp->b_kvabase;
+                                       bp->b_bcount = bcount;
+                                       vm_hold_load_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bcount);
+                                       if (uio->uio_rw == UIO_WRITE) {
+                                               error = copyin(ubase, bp->b_data, bcount);
+                                               if (error) {
+                                                       vm_hold_free_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bcount);
+                                                       goto doerror;
+                                               }
+                                       }
+                               } else if (vmapbuf(bp, ubase, bcount) < 0) {
                                        error = EFAULT;
                                        goto doerror;
                                }
@@ -115,16 +145,28 @@ physio(cdev_t dev, struct uio *uio, int ioflag)
                                tsleep(&bp->b_bio1, 0, "physstr", 0);
                        crit_exit();
 
-                       if (uio->uio_segflg == UIO_USERSPACE)
-                               vunmapbuf(bp);
                        iolen = bp->b_bcount - bp->b_resid;
+                       if (uio->uio_segflg == UIO_USERSPACE) {
+                               if (bounceit) {
+                                       if (uio->uio_rw == UIO_READ && iolen) {
+                                               error = copyout(bp->b_data, ubase, iolen);
+                                               if (error) {
+                                                       bp->b_flags |= B_ERROR;
+                                                       bp->b_error = error;
+                                               }
+                                       }
+                                       vm_hold_free_pages(bp, (vm_offset_t)bp->b_data, (vm_offset_t)bp->b_data + bcount);
+                               } else {
+                                       vunmapbuf(bp);
+                               }
+                       }
                        if (iolen == 0 && !(bp->b_flags & B_ERROR))
                                goto doerror;   /* EOF */
                        uio->uio_iov[i].iov_len -= iolen;
                        uio->uio_iov[i].iov_base += iolen;
                        uio->uio_resid -= iolen;
                        uio->uio_offset += iolen;
-                       ifbp->b_flags & B_ERROR) {
+                       if (bp->b_flags & B_ERROR) {
                                error = bp->b_error;
                                goto doerror;
                        }
index 92bd1c4..31eef9b 100644 (file)
@@ -12,7 +12,7 @@
  *             John S. Dyson.
  *
  * $FreeBSD: src/sys/kern/vfs_bio.c,v 1.242.2.20 2003/05/28 18:38:10 alc Exp $
- * $DragonFly: src/sys/kern/vfs_bio.c,v 1.113 2008/07/18 00:01:11 dillon Exp $
+ * $DragonFly: src/sys/kern/vfs_bio.c,v 1.114 2008/08/10 20:03:14 dillon Exp $
  */
 
 /*
@@ -90,10 +90,6 @@ static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer");
 
 struct buf *buf;               /* buffer header pool */
 
-static void vm_hold_free_pages(struct buf *bp, vm_offset_t from,
-               vm_offset_t to);
-static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
-               vm_offset_t to);
 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off,
                               int pageno, vm_page_t m);
 static void vfs_clean_pages(struct buf *bp);
index fdfc1ee..882d8e1 100644 (file)
@@ -37,7 +37,7 @@
  *
  *     @(#)buf.h       8.9 (Berkeley) 3/30/95
  * $FreeBSD: src/sys/sys/buf.h,v 1.88.2.10 2003/01/25 19:02:23 dillon Exp $
- * $DragonFly: src/sys/sys/buf.h,v 1.52 2008/07/17 23:55:24 dillon Exp $
+ * $DragonFly: src/sys/sys/buf.h,v 1.53 2008/08/10 20:03:15 dillon Exp $
  */
 
 #ifndef _SYS_BUF_H_
@@ -432,6 +432,8 @@ int scan_all_buffers (int (*)(struct buf *, void *), void *);
 void   reassignbuf (struct buf *);
 struct buf *trypbuf (int *);
 void   bio_ops_sync(struct mount *mp);
+void   vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to);
+void   vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to);
 
 #endif /* _KERNEL */
 #endif /* _KERNEL || _KERNEL_STRUCTURES */