Skip to content

Commit

Permalink
uio: Use switch statements when handling UIO_READ vs UIO_WRITE
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bsdjhb committed May 10, 2024
1 parent f75764f commit 473c90a
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 61 deletions.
13 changes: 11 additions & 2 deletions sys/amd64/amd64/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 12 additions & 4 deletions sys/amd64/amd64/uio_machdep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions sys/arm/arm/uio_machdep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions sys/arm64/arm64/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 12 additions & 4 deletions sys/arm64/arm64/uio_machdep.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 21 additions & 12 deletions sys/compat/lindebugfs/lindebugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 11 additions & 5 deletions sys/dev/iicbus/iic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions sys/fs/procfs/procfs_osrel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -62,6 +64,7 @@ procfs_doosrel(PFS_FILL_ARGS)
osrel = ov;
}
p->p_osrel = osrel;
break;
}
return (0);
}
16 changes: 12 additions & 4 deletions sys/i386/i386/uio_machdep.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,29 @@ 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();
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;
Expand Down
14 changes: 10 additions & 4 deletions sys/kern/kern_physio.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,33 @@ 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);
}
#endif /* RACCT */

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;
Expand Down
16 changes: 12 additions & 4 deletions sys/kern/subr_uio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions sys/kern/vfs_vnops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading

0 comments on commit 473c90a

Please sign in to comment.