git: 13e770f1f0eb - stable/14 - uio: Use switch statements when handling UIO_READ vs UIO_WRITE

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Sat, 30 Nov 2024 16:51:06 UTC
The branch stable/14 has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=13e770f1f0eb35f5abd0a2df2a0644b5ee6e220c

commit 13e770f1f0eb35f5abd0a2df2a0644b5ee6e220c
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2024-05-10 20:43:36 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-11-30 13:55:57 +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
    
    (cherry picked from commit 473c90ac04cec0abbb414978c53e9c259c9129e8)
---
 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 378cf2f011f6..63fbd1081318 100644
--- a/sys/amd64/amd64/mem.c
+++ b/sys/amd64/amd64/mem.c
@@ -80,6 +80,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;
@@ -111,8 +112,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 67e14d8e0d12..de0d8c775988 100644
--- a/sys/amd64/amd64/uio_machdep.c
+++ b/sys/amd64/amd64/uio_machdep.c
@@ -100,18 +100,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 18661ebd1652..f86d19fc2da5 100644
--- a/sys/arm/arm/uio_machdep.c
+++ b/sys/arm/arm/uio_machdep.c
@@ -97,20 +97,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 44555dcacc22..b4bca481fe37 100644
--- a/sys/arm64/arm64/mem.c
+++ b/sys/arm64/arm64/mem.c
@@ -50,6 +50,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;
 
@@ -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/arm64/arm64/uio_machdep.c b/sys/arm64/arm64/uio_machdep.c
index 4fdcaf74890c..e67d60d75aab 100644
--- a/sys/arm64/arm64/uio_machdep.c
+++ b/sys/arm64/arm64/uio_machdep.c
@@ -98,18 +98,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 eab89d9d13a5..6ff9cea6c4e8 100644
--- a/sys/compat/lindebugfs/lindebugfs.c
+++ b/sys/compat/lindebugfs/lindebugfs.c
@@ -137,19 +137,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 baaa7a096a14..8349de241128 100644
--- a/sys/dev/iicbus/iic.c
+++ b/sys/dev/iicbus/iic.c
@@ -213,7 +213,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)) {
@@ -222,13 +223,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;
 		}
 	}
 
@@ -264,10 +266,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 3854d7cd370f..e0928fab26cc 100644
--- a/sys/fs/procfs/procfs_osrel.c
+++ b/sys/fs/procfs/procfs_osrel.c
@@ -46,9 +46,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);
@@ -63,6 +65,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 92e067b35bed..4a41ea1520e5 100644
--- a/sys/i386/i386/uio_machdep.c
+++ b/sys/i386/i386/uio_machdep.c
@@ -97,10 +97,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();
@@ -108,10 +112,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 e89a0cb28abc..9b35779538b6 100644
--- a/sys/kern/kern_physio.c
+++ b/sys/kern/kern_physio.c
@@ -117,14 +117,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);
 		}
@@ -132,12 +135,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 d836c0ed295e..77cc3a200ee0 100644
--- a/sys/kern/subr_uio.c
+++ b/sys/kern/subr_uio.c
@@ -252,19 +252,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 1171b72a3a96..53a2ddf94862 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -1261,12 +1261,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 5de2dd8b416b..e2ea2810efa9 100644
--- a/sys/powerpc/powerpc/uio_machdep.c
+++ b/sys/powerpc/powerpc/uio_machdep.c
@@ -103,20 +103,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 d8e2706f708e..c33ee6fc31f1 100644
--- a/sys/riscv/riscv/mem.c
+++ b/sys/riscv/riscv/mem.c
@@ -51,6 +51,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;
 
@@ -80,8 +81,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 e2f82974b2e9..c0ca8070ed56 100644
--- a/sys/riscv/riscv/uio_machdep.c
+++ b/sys/riscv/riscv/uio_machdep.c
@@ -98,18 +98,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;