From owner-dev-commits-src-main@freebsd.org Wed Jan 13 15:33:26 2021 Return-Path: Delivered-To: dev-commits-src-main@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 DA13B4E0B52; Wed, 13 Jan 2021 15:33:25 +0000 (UTC) (envelope-from git@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 4DGBKw3GnFz4qKy; Wed, 13 Jan 2021 15:33:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1753119E90; Wed, 13 Jan 2021 15:33:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10DFXOoU064583; Wed, 13 Jan 2021 15:33:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10DFXN1x064582; Wed, 13 Jan 2021 15:33:23 GMT (envelope-from git) Date: Wed, 13 Jan 2021 15:33:23 GMT Message-Id: <202101131533.10DFXN1x064582@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: 5753be8e432f - main - fd: add refcount argument to falloc_noinstall MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 5753be8e432f26b02c0bd06d7dd84316ddc50ed5 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Jan 2021 15:33:27 -0000 The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=5753be8e432f26b02c0bd06d7dd84316ddc50ed5 commit 5753be8e432f26b02c0bd06d7dd84316ddc50ed5 Author: Mateusz Guzik AuthorDate: 2021-01-13 14:16:38 +0000 Commit: Mateusz Guzik CommitDate: 2021-01-13 15:29:34 +0000 fd: add refcount argument to falloc_noinstall This lets callers avoid atomic ops by initializing the count to required value from the get go. While here add falloc_abort to backpedal from this without having to fdrop. --- sys/kern/kern_descrip.c | 29 +++++++++++++++++++++-------- sys/kern/vfs_syscalls.c | 4 ++-- sys/sys/filedesc.h | 4 +++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index 407280d79850..257b1de2a6ac 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -1979,13 +1979,14 @@ falloc_caps(struct thread *td, struct file **resultfp, int *resultfd, int flags, MPASS(resultfp != NULL); MPASS(resultfd != NULL); - error = falloc_noinstall(td, &fp); - if (error) - return (error); /* no reference held on error */ + error = _falloc_noinstall(td, &fp, 2); + if (__predict_false(error != 0)) { + return (error); + } - error = finstall(td, fp, &fd, flags, fcaps); - if (error) { - fdrop(fp, td); /* one reference (fp only) */ + error = finstall_refed(td, fp, &fd, flags, fcaps); + if (__predict_false(error != 0)) { + falloc_abort(td, fp); return (error); } @@ -1999,7 +2000,7 @@ falloc_caps(struct thread *td, struct file **resultfp, int *resultfd, int flags, * Create a new open file structure without allocating a file descriptor. */ int -falloc_noinstall(struct thread *td, struct file **resultfp) +_falloc_noinstall(struct thread *td, struct file **resultfp, u_int n) { struct file *fp; int maxuserfiles = maxfiles - (maxfiles / 20); @@ -2008,6 +2009,7 @@ falloc_noinstall(struct thread *td, struct file **resultfp) static int curfail; KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__)); + MPASS(n > 0); openfiles_new = atomic_fetchadd_int(&openfiles, 1) + 1; if ((openfiles_new >= maxuserfiles && @@ -2022,13 +2024,24 @@ falloc_noinstall(struct thread *td, struct file **resultfp) } fp = uma_zalloc(file_zone, M_WAITOK); bzero(fp, sizeof(*fp)); - refcount_init(&fp->f_count, 1); + refcount_init(&fp->f_count, n); fp->f_cred = crhold(td->td_ucred); fp->f_ops = &badfileops; *resultfp = fp; return (0); } +void +falloc_abort(struct thread *td, struct file *fp) +{ + + /* + * For assertion purposes. + */ + refcount_init(&fp->f_count, 0); + _fdrop(fp, td); +} + /* * Install a file in a file descriptor table. */ diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c index c1b6c70ab0ac..35a56510e9ef 100644 --- a/sys/kern/vfs_syscalls.c +++ b/sys/kern/vfs_syscalls.c @@ -1214,14 +1214,14 @@ success: } } else { filecaps_free(&nd.ni_filecaps); - fdrop_close(fp, td); + falloc_abort(td, fp); } td->td_retval[0] = indx; return (0); bad: KASSERT(indx == -1, ("indx=%d, should be -1", indx)); - fdrop_close(fp, td); + falloc_abort(td, fp); return (error); } diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h index f0cae3ed6911..9b65c7a66054 100644 --- a/sys/sys/filedesc.h +++ b/sys/sys/filedesc.h @@ -229,7 +229,9 @@ int dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode, int openerror, int *indxp); int falloc_caps(struct thread *td, struct file **resultfp, int *resultfd, int flags, struct filecaps *fcaps); -int falloc_noinstall(struct thread *td, struct file **resultfp); +void falloc_abort(struct thread *td, struct file *fp); +int _falloc_noinstall(struct thread *td, struct file **resultfp, u_int n); +#define falloc_noinstall(td, resultfp) _falloc_noinstall(td, resultfp, 1) void _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags, struct filecaps *fcaps); int finstall(struct thread *td, struct file *fp, int *resultfd, int flags,