From 1e71e7c4f63126a76605e2373607c7fdd0730b06 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Thu, 17 Dec 2020 18:51:09 +0000 Subject: [PATCH] fd: refactor closefp in preparation for close_range rework --- sys/kern/kern_descrip.c | 64 +++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index aea37d3dfc7..618271a6e82 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -107,7 +107,7 @@ __read_mostly uma_zone_t pwd_zone; VFS_SMR_DECLARE; static int closefp(struct filedesc *fdp, int fd, struct file *fp, - struct thread *td, int holdleaders); + struct thread *td, bool holdleaders); static int fd_first_free(struct filedesc *fdp, int low, int size); static void fdgrowtable(struct filedesc *fdp, int nfd); static void fdgrowtable_exp(struct filedesc *fdp, int nfd); @@ -998,7 +998,7 @@ kern_dup(struct thread *td, u_int mode, int flags, int old, int new) error = 0; if (delfp != NULL) { - (void) closefp(fdp, new, delfp, td, 1); + (void) closefp(fdp, new, delfp, td, true); FILEDESC_UNLOCK_ASSERT(fdp); } else { unlock: @@ -1239,29 +1239,13 @@ fgetown(struct sigio **sigiop) return (pgid); } -/* - * Function drops the filedesc lock on return. - */ static int -closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, - int holdleaders) +closefp_impl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td) { int error; FILEDESC_XLOCK_ASSERT(fdp); - if (holdleaders) { - if (td->td_proc->p_fdtol != NULL) { - /* - * Ask fdfree() to sleep to ensure that all relevant - * process leaders can be traversed in closef(). - */ - fdp->fd_holdleaderscount++; - } else { - holdleaders = 0; - } - } - /* * We now hold the fp reference that used to be owned by the * descriptor array. We have to unlock the FILEDESC *AFTER* @@ -1288,6 +1272,30 @@ closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, if (error == ERESTART) error = EINTR; + return (error); +} + +static int +closefp_hl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, + bool holdleaders) +{ + int error; + + FILEDESC_XLOCK_ASSERT(fdp); + + if (holdleaders) { + if (td->td_proc->p_fdtol != NULL) { + /* + * Ask fdfree() to sleep to ensure that all relevant + * process leaders can be traversed in closef(). + */ + fdp->fd_holdleaderscount++; + } else { + holdleaders = false; + } + } + + error = closefp_impl(fdp, fd, fp, td); if (holdleaders) { FILEDESC_XLOCK(fdp); fdp->fd_holdleaderscount--; @@ -1301,6 +1309,20 @@ closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, return (error); } +static int +closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, + bool holdleaders) +{ + + FILEDESC_XLOCK_ASSERT(fdp); + + if (__predict_false(td->td_proc->p_fdtol != NULL)) { + return (closefp_hl(fdp, fd, fp, td, holdleaders)); + } else { + return (closefp_impl(fdp, fd, fp, td)); + } +} + /* * Close a file descriptor. */ @@ -1335,7 +1357,7 @@ kern_close(struct thread *td, int fd) fdfree(fdp, fd); /* closefp() drops the FILEDESC lock for us. */ - return (closefp(fdp, fd, fp, td, 1)); + return (closefp(fdp, fd, fp, td, true)); } int @@ -2649,7 +2671,7 @@ fdcloseexec(struct thread *td) (fde->fde_flags & UF_EXCLOSE))) { FILEDESC_XLOCK(fdp); fdfree(fdp, i); - (void) closefp(fdp, i, fp, td, 0); + (void) closefp(fdp, i, fp, td, false); FILEDESC_UNLOCK_ASSERT(fdp); } } -- 2.45.0