From owner-svn-src-head@freebsd.org Thu Dec 17 18:51:09 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id D48514BFF67; Thu, 17 Dec 2020 18:51:09 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Cxh0Y5kbfz4ZPb; Thu, 17 Dec 2020 18:51:09 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B7AA61D4E2; Thu, 17 Dec 2020 18:51:09 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0BHIp9RK037918; Thu, 17 Dec 2020 18:51:09 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0BHIp9Nb037917; Thu, 17 Dec 2020 18:51:09 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202012171851.0BHIp9Nb037917@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Thu, 17 Dec 2020 18:51:09 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r368730 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 368730 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Dec 2020 18:51:09 -0000 Author: mjg Date: Thu Dec 17 18:51:09 2020 New Revision: 368730 URL: https://svnweb.freebsd.org/changeset/base/368730 Log: fd: refactor closefp in preparation for close_range rework Modified: head/sys/kern/kern_descrip.c Modified: head/sys/kern/kern_descrip.c ============================================================================== --- head/sys/kern/kern_descrip.c Thu Dec 17 18:29:30 2020 (r368729) +++ head/sys/kern/kern_descrip.c Thu Dec 17 18:51:09 2020 (r368730) @@ -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 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,7 +1272,31 @@ closefp(struct filedesc *fdp, int fd, struct file *fp, 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--; if (fdp->fd_holdleaderscount == 0 && @@ -1301,6 +1309,20 @@ closefp(struct filedesc *fdp, int fd, struct file *fp, 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); } }