git: 2de742797b5f - stable/14 - Abstract UIO allocation and deallocation.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sat, 09 Mar 2024 04:40:45 UTC
The branch stable/14 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=2de742797b5fbcb1b7bf8c3e8dc36dddd051abb0 commit 2de742797b5fbcb1b7bf8c3e8dc36dddd051abb0 Author: Alfredo Mazzinghi <am2419@cl.cam.ac.uk> AuthorDate: 2024-01-18 13:04:41 +0000 Commit: Mark Johnston <markj@FreeBSD.org> CommitDate: 2024-03-09 04:27:20 +0000 Abstract UIO allocation and deallocation. Introduce the allocuio() and freeuio() functions to allocate and deallocate struct uio. This hides the actual allocator interface, so it is easier to modify the sub-allocation layout of struct uio and the corresponding iovec array. Obtained from: CheriBSD Reviewed by: kib, markj MFC after: 2 weeks Sponsored by: CHaOS, EPSRC grant EP/V000292/1 Differential Revision: https://reviews.freebsd.org/D43711 (cherry picked from commit 61cc4830a7b16400efade3d884a59fda6d80d651) --- sys/amd64/linux32/linux32_machdep.c | 2 +- sys/cddl/compat/opensolaris/kern/opensolaris_uio.c | 2 +- sys/compat/freebsd32/freebsd32_misc.c | 29 ++++++++--------- sys/compat/linux/linux_file.c | 6 ++-- sys/kern/kern_jail.c | 6 ++-- sys/kern/kern_ktrace.c | 4 +-- sys/kern/kern_sendfile.c | 4 +-- sys/kern/subr_prf.c | 2 +- sys/kern/subr_uio.c | 38 +++++++++++++++++----- sys/kern/sys_generic.c | 8 ++--- sys/kern/vfs_aio.c | 4 +-- sys/kern/vfs_mount.c | 2 +- sys/kern/vfs_vnops.c | 2 +- sys/sys/uio.h | 2 ++ 14 files changed, 66 insertions(+), 45 deletions(-) diff --git a/sys/amd64/linux32/linux32_machdep.c b/sys/amd64/linux32/linux32_machdep.c index ba4dcc61af76..7f1c85cee201 100644 --- a/sys/amd64/linux32/linux32_machdep.c +++ b/sys/amd64/linux32/linux32_machdep.c @@ -106,7 +106,7 @@ linux_readv(struct thread *td, struct linux_readv_args *uap) if (error) return (error); error = kern_readv(td, uap->fd, auio); - free(auio, M_IOV); + freeuio(auio); return (error); } diff --git a/sys/cddl/compat/opensolaris/kern/opensolaris_uio.c b/sys/cddl/compat/opensolaris/kern/opensolaris_uio.c index e1cc8f802762..b2a08a55bba4 100644 --- a/sys/cddl/compat/opensolaris/kern/opensolaris_uio.c +++ b/sys/cddl/compat/opensolaris/kern/opensolaris_uio.c @@ -68,7 +68,7 @@ uiocopy(void *p, size_t n, enum uio_rw rw, struct uio *uio, size_t *cbytes) error = vn_io_fault_uiomove(p, n, uio_clone); *cbytes = uio->uio_resid - uio_clone->uio_resid; if (uio_clone != &small_uio_clone) - free(uio_clone, M_IOV); + freeuio(uio_clone); return (error); } diff --git a/sys/compat/freebsd32/freebsd32_misc.c b/sys/compat/freebsd32/freebsd32_misc.c index f2d66cf74b2b..facbf00a4c51 100644 --- a/sys/compat/freebsd32/freebsd32_misc.c +++ b/sys/compat/freebsd32/freebsd32_misc.c @@ -1196,32 +1196,29 @@ freebsd32_copyinuio(struct iovec32 *iovp, u_int iovcnt, struct uio **uiop) struct iovec32 iov32; struct iovec *iov; struct uio *uio; - u_int iovlen; int error, i; *uiop = NULL; if (iovcnt > UIO_MAXIOV) return (EINVAL); - iovlen = iovcnt * sizeof(struct iovec); - uio = malloc(iovlen + sizeof *uio, M_IOV, M_WAITOK); - iov = (struct iovec *)(uio + 1); + uio = allocuio(iovcnt); + iov = uio->uio_iov; for (i = 0; i < iovcnt; i++) { error = copyin(&iovp[i], &iov32, sizeof(struct iovec32)); if (error) { - free(uio, M_IOV); + freeuio(uio); return (error); } iov[i].iov_base = PTRIN(iov32.iov_base); iov[i].iov_len = iov32.iov_len; } - uio->uio_iov = iov; uio->uio_iovcnt = iovcnt; uio->uio_segflg = UIO_USERSPACE; uio->uio_offset = -1; uio->uio_resid = 0; for (i = 0; i < iovcnt; i++) { if (iov->iov_len > INT_MAX - uio->uio_resid) { - free(uio, M_IOV); + freeuio(uio); return (EINVAL); } uio->uio_resid += iov->iov_len; @@ -1241,7 +1238,7 @@ freebsd32_readv(struct thread *td, struct freebsd32_readv_args *uap) if (error) return (error); error = kern_readv(td, uap->fd, auio); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -1255,7 +1252,7 @@ freebsd32_writev(struct thread *td, struct freebsd32_writev_args *uap) if (error) return (error); error = kern_writev(td, uap->fd, auio); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -1269,7 +1266,7 @@ freebsd32_preadv(struct thread *td, struct freebsd32_preadv_args *uap) if (error) return (error); error = kern_preadv(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset)); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -1283,7 +1280,7 @@ freebsd32_pwritev(struct thread *td, struct freebsd32_pwritev_args *uap) if (error) return (error); error = kern_pwritev(td, uap->fd, auio, PAIR32TO64(off_t,uap->offset)); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -2199,9 +2196,9 @@ freebsd32_do_sendfile(struct thread *td, out: if (hdr_uio) - free(hdr_uio, M_IOV); + freeuio(hdr_uio); if (trl_uio) - free(trl_uio, M_IOV); + freeuio(trl_uio); return (error); } @@ -2780,7 +2777,7 @@ freebsd32_jail_set(struct thread *td, struct freebsd32_jail_set_args *uap) if (error) return (error); error = kern_jail_set(td, auio, uap->flags); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -2807,7 +2804,7 @@ freebsd32_jail_get(struct thread *td, struct freebsd32_jail_get_args *uap) if (error != 0) break; } - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -3540,7 +3537,7 @@ freebsd32_nmount(struct thread *td, return (error); error = vfs_donmount(td, flags, auio); - free(auio, M_IOV); + freeuio(auio); return error; } diff --git a/sys/compat/linux/linux_file.c b/sys/compat/linux/linux_file.c index 6eab8980a5c5..1279490ae8be 100644 --- a/sys/compat/linux/linux_file.c +++ b/sys/compat/linux/linux_file.c @@ -1038,7 +1038,7 @@ linux_preadv(struct thread *td, struct linux_preadv_args *uap) if (error != 0) return (error); error = kern_preadv(td, uap->fd, auio, offset); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -1065,7 +1065,7 @@ linux_pwritev(struct thread *td, struct linux_pwritev_args *uap) if (error != 0) return (error); error = kern_pwritev(td, uap->fd, auio, offset); - free(auio, M_IOV); + freeuio(auio); return (linux_enobufs2eagain(td, uap->fd, error)); } @@ -1872,6 +1872,6 @@ linux_writev(struct thread *td, struct linux_writev_args *args) if (error != 0) return (error); error = kern_writev(td, args->fd, auio); - free(auio, M_IOV); + freeuio(auio); return (linux_enobufs2eagain(td, args->fd, error)); } diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index 91466c46bc62..1f775f78e581 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -534,7 +534,7 @@ sys_jail_set(struct thread *td, struct jail_set_args *uap) if (error) return (error); error = kern_jail_set(td, auio, uap->flags); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -2264,8 +2264,8 @@ sys_jail_get(struct thread *td, struct jail_get_args *uap) error = kern_jail_get(td, auio, uap->flags); if (error == 0) error = copyout(auio->uio_iov, uap->iovp, - uap->iovcnt * sizeof (struct iovec)); - free(auio, M_IOV); + uap->iovcnt * sizeof(struct iovec)); + freeuio(auio); return (error); } diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c index 01158c2c238f..75b81457402b 100644 --- a/sys/kern/kern_ktrace.c +++ b/sys/kern/kern_ktrace.c @@ -770,7 +770,7 @@ ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error) char *buf; if (error) { - free(uio, M_IOV); + freeuio(uio); return; } uio->uio_offset = 0; @@ -778,7 +778,7 @@ ktrgenio(int fd, enum uio_rw rw, struct uio *uio, int error) datalen = MIN(uio->uio_resid, ktr_geniosize); buf = malloc(datalen, M_KTRACE, M_WAITOK); error = uiomove(buf, datalen, uio); - free(uio, M_IOV); + freeuio(uio); if (error) { free(buf, M_KTRACE); return; diff --git a/sys/kern/kern_sendfile.c b/sys/kern/kern_sendfile.c index f6f6b57adfc7..323e7fcde07b 100644 --- a/sys/kern/kern_sendfile.c +++ b/sys/kern/kern_sendfile.c @@ -1331,8 +1331,8 @@ sendfile(struct thread *td, struct sendfile_args *uap, int compat) (void)copyout(&sbytes, uap->sbytes, sizeof(off_t)); out: - free(hdr_uio, M_IOV); - free(trl_uio, M_IOV); + freeuio(hdr_uio); + freeuio(trl_uio); return (error); } diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index f2be2f0dc664..60040a085162 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -388,7 +388,7 @@ log_console(struct uio *uio) msglogstr(consbuffer, pri, /*filter_cr*/ 1); } msgbuftrigger = 1; - free(uio, M_IOV); + freeuio(uio); free(consbuffer, M_TEMP); } diff --git a/sys/kern/subr_uio.c b/sys/kern/subr_uio.c index 7a7fe9b0f4f9..d836c0ed295e 100644 --- a/sys/kern/subr_uio.c +++ b/sys/kern/subr_uio.c @@ -376,21 +376,20 @@ copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop) if (iovcnt > UIO_MAXIOV) return (EINVAL); iovlen = iovcnt * sizeof(struct iovec); - uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK); - iov = (struct iovec *)(uio + 1); + uio = allocuio(iovcnt); + iov = uio->uio_iov; error = copyin(iovp, iov, iovlen); - if (error) { - free(uio, M_IOV); + if (error != 0) { + freeuio(uio); return (error); } - uio->uio_iov = iov; uio->uio_iovcnt = iovcnt; uio->uio_segflg = UIO_USERSPACE; uio->uio_offset = -1; uio->uio_resid = 0; for (i = 0; i < iovcnt; i++) { if (iov->iov_len > IOSIZE_MAX - uio->uio_resid) { - free(uio, M_IOV); + freeuio(uio); return (EINVAL); } uio->uio_resid += iov->iov_len; @@ -400,16 +399,39 @@ copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop) return (0); } +struct uio * +allocuio(u_int iovcnt) +{ + struct uio *uio; + int iovlen; + + KASSERT(iovcnt <= UIO_MAXIOV, + ("Requested %u iovecs exceed UIO_MAXIOV", iovcnt)); + iovlen = iovcnt * sizeof(struct iovec); + uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK); + uio->uio_iov = (struct iovec *)(uio + 1); + + return (uio); +} + +void +freeuio(struct uio *uio) +{ + free(uio, M_IOV); +} + struct uio * cloneuio(struct uio *uiop) { + struct iovec *iov; struct uio *uio; int iovlen; iovlen = uiop->uio_iovcnt * sizeof(struct iovec); - uio = malloc(iovlen + sizeof(*uio), M_IOV, M_WAITOK); + uio = allocuio(uiop->uio_iovcnt); + iov = uio->uio_iov; *uio = *uiop; - uio->uio_iov = (struct iovec *)(uio + 1); + uio->uio_iov = iov; bcopy(uiop->uio_iov, uio->uio_iov, iovlen); return (uio); } diff --git a/sys/kern/sys_generic.c b/sys/kern/sys_generic.c index c999bd60f95d..b569a9f808d5 100644 --- a/sys/kern/sys_generic.c +++ b/sys/kern/sys_generic.c @@ -273,7 +273,7 @@ sys_readv(struct thread *td, struct readv_args *uap) if (error) return (error); error = kern_readv(td, uap->fd, auio); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -312,7 +312,7 @@ sys_preadv(struct thread *td, struct preadv_args *uap) if (error) return (error); error = kern_preadv(td, uap->fd, auio, uap->offset); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -475,7 +475,7 @@ sys_writev(struct thread *td, struct writev_args *uap) if (error) return (error); error = kern_writev(td, uap->fd, auio); - free(auio, M_IOV); + freeuio(auio); return (error); } @@ -514,7 +514,7 @@ sys_pwritev(struct thread *td, struct pwritev_args *uap) if (error) return (error); error = kern_pwritev(td, uap->fd, auio, uap->offset); - free(auio, M_IOV); + freeuio(auio); return (error); } diff --git a/sys/kern/vfs_aio.c b/sys/kern/vfs_aio.c index 45f912a7cc2d..fd6682ef03b0 100644 --- a/sys/kern/vfs_aio.c +++ b/sys/kern/vfs_aio.c @@ -559,7 +559,7 @@ aio_free_entry(struct kaiocb *job) fdrop(job->fd_file, curthread); crfree(job->cred); if (job->uiop != &job->uio) - free(job->uiop, M_IOV); + freeuio(job->uiop); uma_zfree(aiocb_zone, job); AIO_LOCK(ki); @@ -1736,7 +1736,7 @@ err3: knlist_delete(&job->klist, curthread, 0); err2: if (job->uiop != &job->uio) - free(job->uiop, M_IOV); + freeuio(job->uiop); uma_zfree(aiocb_zone, job); err1: ops->store_error(ujob, error); diff --git a/sys/kern/vfs_mount.c b/sys/kern/vfs_mount.c index c36fda4e8a9e..49d1d21d2d6d 100644 --- a/sys/kern/vfs_mount.c +++ b/sys/kern/vfs_mount.c @@ -474,7 +474,7 @@ sys_nmount(struct thread *td, struct nmount_args *uap) } error = vfs_donmount(td, flags, auio); - free(auio, M_IOV); + freeuio(auio); return (error); } diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c index b650b32ccc55..62339387d7d0 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -1444,7 +1444,7 @@ vn_io_fault1(struct vnode *vp, struct uio *uio, struct vn_io_fault_args *args, td->td_ma_cnt = prev_td_ma_cnt; curthread_pflags_restore(saveheld); out: - free(uio_clone, M_IOV); + freeuio(uio_clone); return (error); } diff --git a/sys/sys/uio.h b/sys/sys/uio.h index 6250b3cfbf03..e09a41ab1045 100644 --- a/sys/sys/uio.h +++ b/sys/sys/uio.h @@ -78,6 +78,8 @@ struct vm_object; struct vm_page; struct bus_dma_segment; +struct uio *allocuio(u_int iovcnt); +void freeuio(struct uio *uio); struct uio *cloneuio(struct uio *uiop); int copyiniov(const struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error);