Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Jan 2021 03:31:39 GMT
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 530b699a62ad - main - fd: add finstall_refed
Message-ID:  <202101130331.10D3VdiS029053@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=530b699a62ad0f1e5718825d60ddb1ec9b214489

commit 530b699a62ad0f1e5718825d60ddb1ec9b214489
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2021-01-12 16:05:27 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2021-01-13 02:27:03 +0000

    fd: add finstall_refed
    
    Can be used to consume an already existing reference and consequently
    avoid atomic ops.
---
 sys/kern/kern_descrip.c | 28 ++++++++++++++++++++--------
 sys/sys/filedesc.h      |  2 ++
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index 237d15fb5387..407280d79850 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -2059,7 +2059,7 @@ _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags,
 }
 
 int
-finstall(struct thread *td, struct file *fp, int *fd, int flags,
+finstall_refed(struct thread *td, struct file *fp, int *fd, int flags,
     struct filecaps *fcaps)
 {
 	struct filedesc *fdp = td->td_proc->p_fd;
@@ -2067,18 +2067,30 @@ finstall(struct thread *td, struct file *fp, int *fd, int flags,
 
 	MPASS(fd != NULL);
 
-	if (!fhold(fp))
-		return (EBADF);
 	FILEDESC_XLOCK(fdp);
 	error = fdalloc(td, 0, fd);
+	if (__predict_true(error == 0)) {
+		_finstall(fdp, fp, *fd, flags, fcaps);
+	}
+	FILEDESC_XUNLOCK(fdp);
+	return (error);
+}
+
+int
+finstall(struct thread *td, struct file *fp, int *fd, int flags,
+    struct filecaps *fcaps)
+{
+	int error;
+
+	MPASS(fd != NULL);
+
+	if (!fhold(fp))
+		return (EBADF);
+	error = finstall_refed(td, fp, fd, flags, fcaps);
 	if (__predict_false(error != 0)) {
-		FILEDESC_XUNLOCK(fdp);
 		fdrop(fp, td);
-		return (error);
 	}
-	_finstall(fdp, fp, *fd, flags, fcaps);
-	FILEDESC_XUNLOCK(fdp);
-	return (0);
+	return (error);
 }
 
 /*
diff --git a/sys/sys/filedesc.h b/sys/sys/filedesc.h
index 132aa6c1de03..f0cae3ed6911 100644
--- a/sys/sys/filedesc.h
+++ b/sys/sys/filedesc.h
@@ -234,6 +234,8 @@ 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,
 	    struct filecaps *fcaps);
+int	finstall_refed(struct thread *td, struct file *fp, int *resultfd, int flags,
+	    struct filecaps *fcaps);
 int	fdalloc(struct thread *td, int minfd, int *result);
 int	fdallocn(struct thread *td, int minfd, int *fds, int n);
 int	fdcheckstd(struct thread *td);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202101130331.10D3VdiS029053>