The branch main has been updated by jhb:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=473c90ac04cec0abbb414978c53e9c259c9129e8

commit 473c90ac04cec0abbb414978c53e9c259c9129e8
Author:     John Baldwin <j...@freebsd.org>
AuthorDate: 2024-05-10 20:43:36 +0000
Commit:     John Baldwin <j...@freebsd.org>
CommitDate: 2024-05-10 20:43:36 +0000

    uio: Use switch statements when handling UIO_READ vs UIO_WRITE
    
    This is mostly to reduce the diff with CheriBSD which adds additional
    constants to enum uio_rw, but also matches the normal style used for
    uio_segflg.
    
    Reviewed by:    kib, emaste
    Obtained from:  CheriBSD
    Differential Revision:  https://reviews.freebsd.org/D45142
---
 sys/amd64/amd64/mem.c              | 13 +++++++++++--
 sys/amd64/amd64/uio_machdep.c      | 16 ++++++++++++----
 sys/arm/arm/uio_machdep.c          | 16 ++++++++++++----
 sys/arm64/arm64/mem.c              | 13 +++++++++++--
 sys/arm64/arm64/uio_machdep.c      | 16 ++++++++++++----
 sys/compat/lindebugfs/lindebugfs.c | 33 +++++++++++++++++++++------------
 sys/dev/iicbus/iic.c               | 16 +++++++++++-----
 sys/fs/procfs/procfs_osrel.c       |  7 +++++--
 sys/i386/i386/uio_machdep.c        | 16 ++++++++++++----
 sys/kern/kern_physio.c             | 14 ++++++++++----
 sys/kern/subr_uio.c                | 16 ++++++++++++----
 sys/kern/vfs_vnops.c               |  7 +++++--
 sys/powerpc/powerpc/uio_machdep.c  | 16 ++++++++++++----
 sys/riscv/riscv/mem.c              | 13 +++++++++++--
 sys/riscv/riscv/uio_machdep.c      | 16 ++++++++++++----
 sys/ufs/ffs/ffs_suspend.c          |  7 +++++--
 16 files changed, 174 insertions(+), 61 deletions(-)

diff --git a/sys/amd64/amd64/mem.c b/sys/amd64/amd64/mem.c
index e5285586b928..25d22805ed3e 100644
--- a/sys/amd64/amd64/mem.c
+++ b/sys/amd64/amd64/mem.c
@@ -79,6 +79,7 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
        struct iovec *iov;
        void *p;
        ssize_t orig_resid;
+       vm_prot_t prot;
        u_long v, vd;
        u_int c;
        int error;
@@ -110,8 +111,16 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
                                break;
                        }
 
-                       if (!kernacc((void *)v, c, uio->uio_rw == UIO_READ ?
-                           VM_PROT_READ : VM_PROT_WRITE)) {
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
+                               prot = VM_PROT_READ;
+                               break;
+                       case UIO_WRITE:
+                               prot = VM_PROT_WRITE;
+                               break;
+                       }
+
+                       if (!kernacc((void *)v, c, prot)) {
                                error = EFAULT;
                                break;
                        }
diff --git a/sys/amd64/amd64/uio_machdep.c b/sys/amd64/amd64/uio_machdep.c
index 7b5c89747bc2..83795653fa28 100644
--- a/sys/amd64/amd64/uio_machdep.c
+++ b/sys/amd64/amd64/uio_machdep.c
@@ -97,18 +97,26 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, 
struct uio *uio)
                switch (uio->uio_segflg) {
                case UIO_USERSPACE:
                        maybe_yield();
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                error = copyout(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                error = copyin(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        if (error)
                                goto out;
                        break;
                case UIO_SYSSPACE:
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bcopy(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                bcopy(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        break;
                case UIO_NOCOPY:
                        break;
diff --git a/sys/arm/arm/uio_machdep.c b/sys/arm/arm/uio_machdep.c
index de9bc94317e8..6b8e4352e06f 100644
--- a/sys/arm/arm/uio_machdep.c
+++ b/sys/arm/arm/uio_machdep.c
@@ -94,20 +94,28 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, 
struct uio *uio)
                switch (uio->uio_segflg) {
                case UIO_USERSPACE:
                        maybe_yield();
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                error = copyout(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                error = copyin(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        if (error) {
                                sf_buf_free(sf);
                                goto out;
                        }
                        break;
                case UIO_SYSSPACE:
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bcopy(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                bcopy(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        break;
                case UIO_NOCOPY:
                        break;
diff --git a/sys/arm64/arm64/mem.c b/sys/arm64/arm64/mem.c
index 1f44d547204e..2a998ca1f845 100644
--- a/sys/arm64/arm64/mem.c
+++ b/sys/arm64/arm64/mem.c
@@ -49,6 +49,7 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
        struct vm_page m;
        vm_page_t marr;
        vm_offset_t off, v;
+       vm_prot_t prot;
        u_int cnt;
        int error;
 
@@ -78,8 +79,16 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
                                break;
                        }
 
-                       if (!kernacc((void *)v, cnt, uio->uio_rw == UIO_READ ?
-                           VM_PROT_READ : VM_PROT_WRITE)) {
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
+                               prot = VM_PROT_READ;
+                               break;
+                       case UIO_WRITE:
+                               prot = VM_PROT_WRITE;
+                               break;
+                       }
+
+                       if (!kernacc((void *)v, cnt, prot)) {
                                error = EFAULT;
                                break;
                        }
diff --git a/sys/arm64/arm64/uio_machdep.c b/sys/arm64/arm64/uio_machdep.c
index c42aee94506e..1c12940419cc 100644
--- a/sys/arm64/arm64/uio_machdep.c
+++ b/sys/arm64/arm64/uio_machdep.c
@@ -95,18 +95,26 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, 
struct uio *uio)
                switch (uio->uio_segflg) {
                case UIO_USERSPACE:
                        maybe_yield();
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                error = copyout(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                error = copyin(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        if (error)
                                goto out;
                        break;
                case UIO_SYSSPACE:
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bcopy(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                bcopy(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        break;
                case UIO_NOCOPY:
                        break;
diff --git a/sys/compat/lindebugfs/lindebugfs.c 
b/sys/compat/lindebugfs/lindebugfs.c
index e63c31cee1d7..2cede0ef213f 100644
--- a/sys/compat/lindebugfs/lindebugfs.c
+++ b/sys/compat/lindebugfs/lindebugfs.c
@@ -136,19 +136,28 @@ debugfs_fill(PFS_FILL_ARGS)
        }
 
        rc = -ENODEV;
-       if (uio->uio_rw == UIO_READ && d->dm_fops->read) {
-               rc = -ENOMEM;
-               buf = (char *) malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT);
-               if (buf != NULL) {
-                       rc = d->dm_fops->read(&lf, buf, sb->s_size, &off);
-                       if (rc > 0)
-                               sbuf_bcpy(sb, buf, strlen(buf));
-
-                       free(buf, M_DFSINT);
+       switch (uio->uio_rw) {
+       case UIO_READ:
+               if (d->dm_fops->read != NULL) {
+                       rc = -ENOMEM;
+                       buf = malloc(sb->s_size, M_DFSINT, M_ZERO | M_NOWAIT);
+                       if (buf != NULL) {
+                               rc = d->dm_fops->read(&lf, buf, sb->s_size,
+                                   &off);
+                               if (rc > 0)
+                                       sbuf_bcpy(sb, buf, strlen(buf));
+
+                               free(buf, M_DFSINT);
+                       }
                }
-       } else if (uio->uio_rw == UIO_WRITE && d->dm_fops->write) {
-               sbuf_finish(sb);
-               rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb), &off);
+               break;
+       case UIO_WRITE:
+               if (d->dm_fops->write != NULL) {
+                       sbuf_finish(sb);
+                       rc = d->dm_fops->write(&lf, sbuf_data(sb), sbuf_len(sb),
+                           &off);
+               }
+               break;
        }
 
        if (d->dm_fops->release)
diff --git a/sys/dev/iicbus/iic.c b/sys/dev/iicbus/iic.c
index 8c9dbb6bc145..0d65bdea5782 100644
--- a/sys/dev/iicbus/iic.c
+++ b/sys/dev/iicbus/iic.c
@@ -239,7 +239,8 @@ iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, int 
last)
                num_bytes = MIN(uio->uio_resid, sizeof(buffer));
                transferred_bytes = 0;
 
-               if (uio->uio_rw == UIO_WRITE) {
+               switch (uio->uio_rw) {
+               case UIO_WRITE:
                        error = uiomove(buffer, num_bytes, uio);
 
                        while ((error == 0) && (transferred_bytes < num_bytes)) 
{
@@ -248,13 +249,14 @@ iicuio_move(struct iic_cdevpriv *priv, struct uio *uio, 
int last)
                                    num_bytes - transferred_bytes, 
&written_bytes, 0);
                                transferred_bytes += written_bytes;
                        }
-                               
-               } else if (uio->uio_rw == UIO_READ) {
+                       break;
+               case UIO_READ:
                        error = iicbus_read(parent, buffer,
                            num_bytes, &transferred_bytes,
                            ((uio->uio_resid <= sizeof(buffer)) ? last : 0), 0);
                        if (error == 0)
                                error = uiomove(buffer, transferred_bytes, uio);
+                       break;
                }
        }
 
@@ -290,10 +292,14 @@ iicuio(struct cdev *dev, struct uio *uio, int ioflag)
                return (error);
        }
 
-       if (uio->uio_rw == UIO_READ)
+       switch (uio->uio_rw) {
+       case UIO_READ:
                addr = priv->addr | LSB;
-       else
+               break;
+       case UIO_WRITE:
                addr = priv->addr & ~LSB;
+               break;
+       }
 
        error = iicbus_start(parent, addr, 0);
        if (error != 0)
diff --git a/sys/fs/procfs/procfs_osrel.c b/sys/fs/procfs/procfs_osrel.c
index fd6a4d7e0eea..0102090de4da 100644
--- a/sys/fs/procfs/procfs_osrel.c
+++ b/sys/fs/procfs/procfs_osrel.c
@@ -45,9 +45,11 @@ procfs_doosrel(PFS_FILL_ARGS)
 
        if (uio == NULL)
                return (EOPNOTSUPP);
-       if (uio->uio_rw == UIO_READ) {
+       switch (uio->uio_rw) {
+       case UIO_READ:
                sbuf_printf(sb, "%d\n", p->p_osrel);
-       } else {
+               break;
+       case UIO_WRITE:
                sbuf_trim(sb);
                sbuf_finish(sb);
                pp = sbuf_data(sb);
@@ -62,6 +64,7 @@ procfs_doosrel(PFS_FILL_ARGS)
                        osrel = ov;
                }
                p->p_osrel = osrel;
+               break;
        }
        return (0);
 }
diff --git a/sys/i386/i386/uio_machdep.c b/sys/i386/i386/uio_machdep.c
index fd0a27eb269c..b41460aef5b4 100644
--- a/sys/i386/i386/uio_machdep.c
+++ b/sys/i386/i386/uio_machdep.c
@@ -94,10 +94,14 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, 
struct uio *uio)
                switch (uio->uio_segflg) {
                case UIO_USERSPACE:
                        maybe_yield();
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                error = copyout(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                error = copyin(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        if (error) {
                                sf_buf_free(sf);
                                sched_unpin();
@@ -105,10 +109,14 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int 
n, struct uio *uio)
                        }
                        break;
                case UIO_SYSSPACE:
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bcopy(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                bcopy(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        break;
                case UIO_NOCOPY:
                        break;
diff --git a/sys/kern/kern_physio.c b/sys/kern/kern_physio.c
index 91026402db05..79b7694192e4 100644
--- a/sys/kern/kern_physio.c
+++ b/sys/kern/kern_physio.c
@@ -116,14 +116,17 @@ physio(struct cdev *dev, struct uio *uio, int ioflag)
 #ifdef RACCT
                if (racct_enable) {
                        PROC_LOCK(curproc);
-                       if (uio->uio_rw == UIO_READ) {
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                racct_add_force(curproc, RACCT_READBPS,
                                    uio->uio_iov[i].iov_len);
                                racct_add_force(curproc, RACCT_READIOPS, 1);
-                       } else {
+                               break;
+                       case UIO_WRITE:
                                racct_add_force(curproc, RACCT_WRITEBPS,
                                    uio->uio_iov[i].iov_len);
                                racct_add_force(curproc, RACCT_WRITEIOPS, 1);
+                               break;
                        }
                        PROC_UNLOCK(curproc);
                }
@@ -131,12 +134,15 @@ physio(struct cdev *dev, struct uio *uio, int ioflag)
 
                while (uio->uio_iov[i].iov_len) {
                        g_reset_bio(bp);
-                       if (uio->uio_rw == UIO_READ) {
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bp->bio_cmd = BIO_READ;
                                curthread->td_ru.ru_inblock++;
-                       } else {
+                               break;
+                       case UIO_WRITE:
                                bp->bio_cmd = BIO_WRITE;
                                curthread->td_ru.ru_oublock++;
+                               break;
                        }
                        bp->bio_offset = uio->uio_offset;
                        base = uio->uio_iov[i].iov_base;
diff --git a/sys/kern/subr_uio.c b/sys/kern/subr_uio.c
index cbc4b0c0ed8e..ff63ac3e2dc0 100644
--- a/sys/kern/subr_uio.c
+++ b/sys/kern/subr_uio.c
@@ -249,19 +249,27 @@ uiomove_faultflag(void *cp, int n, struct uio *uio, int 
nofault)
                switch (uio->uio_segflg) {
                case UIO_USERSPACE:
                        maybe_yield();
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                error = copyout(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                error = copyin(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        if (error)
                                goto out;
                        break;
 
                case UIO_SYSSPACE:
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bcopy(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                bcopy(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        break;
                case UIO_NOCOPY:
                        break;
diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index 0e864f959e36..aff3947f91cd 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -1259,12 +1259,15 @@ vn_io_fault_doio(struct vn_io_fault_args *args, struct 
uio *uio,
                    uio, args->cred, args->flags, td);
                break;
        case VN_IO_FAULT_VOP:
-               if (uio->uio_rw == UIO_READ) {
+               switch (uio->uio_rw) {
+               case UIO_READ:
                        error = VOP_READ(args->args.vop_args.vp, uio,
                            args->flags, args->cred);
-               } else if (uio->uio_rw == UIO_WRITE) {
+                       break;
+               case UIO_WRITE:
                        error = VOP_WRITE(args->args.vop_args.vp, uio,
                            args->flags, args->cred);
+                       break;
                }
                break;
        default:
diff --git a/sys/powerpc/powerpc/uio_machdep.c 
b/sys/powerpc/powerpc/uio_machdep.c
index ee0f9f2a0795..63911963279a 100644
--- a/sys/powerpc/powerpc/uio_machdep.c
+++ b/sys/powerpc/powerpc/uio_machdep.c
@@ -100,20 +100,28 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int 
n, struct uio *uio)
                switch (uio->uio_segflg) {
                        case UIO_USERSPACE:
                                maybe_yield();
-                               if (uio->uio_rw == UIO_READ)
+                               switch (uio->uio_rw) {
+                               case UIO_READ:
                                        error = copyout(cp, iov->iov_base, cnt);
-                               else
+                                       break;
+                               case UIO_WRITE:
                                        error = copyin(iov->iov_base, cp, cnt);
+                                       break;
+                               }
                                if (error) {
                                        sf_buf_free(sf);
                                        goto out;
                                }
                                break;
                        case UIO_SYSSPACE:
-                               if (uio->uio_rw == UIO_READ)
+                               switch (uio->uio_rw) {
+                               case UIO_READ:
                                        bcopy(cp, iov->iov_base, cnt);
-                               else
+                                       break;
+                               case UIO_WRITE:
                                        bcopy(iov->iov_base, cp, cnt);
+                                       break;
+                               }
                                break;
                        case UIO_NOCOPY:
                                break;
diff --git a/sys/riscv/riscv/mem.c b/sys/riscv/riscv/mem.c
index 7bf48a02e16a..9792c01fa673 100644
--- a/sys/riscv/riscv/mem.c
+++ b/sys/riscv/riscv/mem.c
@@ -50,6 +50,7 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
        struct iovec *iov;
        struct vm_page m;
        vm_page_t marr;
+       vm_prot_t prot;
        u_int cnt;
        int error;
 
@@ -79,8 +80,16 @@ memrw(struct cdev *dev, struct uio *uio, int flags)
                                break;
                        }
 
-                       if (!kernacc((void *)v, cnt, uio->uio_rw == UIO_READ ?
-                           VM_PROT_READ : VM_PROT_WRITE)) {
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
+                               prot = VM_PROT_READ;
+                               break;
+                       case UIO_WRITE:
+                               prot = VM_PROT_WRITE;
+                               break;
+                       }
+
+                       if (!kernacc((void *)v, cnt, prot)) {
                                error = EFAULT;
                                break;
                        }
diff --git a/sys/riscv/riscv/uio_machdep.c b/sys/riscv/riscv/uio_machdep.c
index 2e6ed2337fc5..002685c98ccf 100644
--- a/sys/riscv/riscv/uio_machdep.c
+++ b/sys/riscv/riscv/uio_machdep.c
@@ -95,18 +95,26 @@ uiomove_fromphys(vm_page_t ma[], vm_offset_t offset, int n, 
struct uio *uio)
                switch (uio->uio_segflg) {
                case UIO_USERSPACE:
                        maybe_yield();
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                error = copyout(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                error = copyin(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        if (error)
                                goto out;
                        break;
                case UIO_SYSSPACE:
-                       if (uio->uio_rw == UIO_READ)
+                       switch (uio->uio_rw) {
+                       case UIO_READ:
                                bcopy(cp, iov->iov_base, cnt);
-                       else
+                               break;
+                       case UIO_WRITE:
                                bcopy(iov->iov_base, cp, cnt);
+                               break;
+                       }
                        break;
                case UIO_NOCOPY:
                        break;
diff --git a/sys/ufs/ffs/ffs_suspend.c b/sys/ufs/ffs/ffs_suspend.c
index e23e12cc8be4..3afbab82cba5 100644
--- a/sys/ufs/ffs/ffs_suspend.c
+++ b/sys/ufs/ffs/ffs_suspend.c
@@ -138,7 +138,8 @@ ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
                            NOCRED, &bp);
                        if (error != 0)
                                goto out;
-                       if (uio->uio_rw == UIO_WRITE) {
+                       switch (uio->uio_rw) {
+                       case UIO_WRITE:
                                error = copyin(base, bp->b_data, len);
                                if (error != 0) {
                                        bp->b_flags |= B_INVAL | B_NOCACHE;
@@ -148,11 +149,13 @@ ffs_susp_rdwr(struct cdev *dev, struct uio *uio, int 
ioflag)
                                error = bwrite(bp);
                                if (error != 0)
                                        goto out;
-                       } else {
+                               break;
+                       case UIO_READ:
                                error = copyout(bp->b_data, base, len);
                                brelse(bp);
                                if (error != 0)
                                        goto out;
+                               break;
                        }
                        uio->uio_iov[i].iov_base =
                            (char *)uio->uio_iov[i].iov_base + len;

Reply via email to