From edcdcefb88a833d5170461993ee5c91a83a00ece Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sun, 13 Dec 2020 18:06:24 +0000 Subject: fd: fix fdrop prediction when closing a fd Most of the time this is the last reference, contrary to typical fdrop use. --- sys/kern/kern_descrip.c | 2 +- sys/sys/file.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c index bd3b9508809e..dbcf6d316a2f 100644 --- a/sys/kern/kern_descrip.c +++ b/sys/kern/kern_descrip.c @@ -2766,7 +2766,7 @@ closef(struct file *fp, struct thread *td) FILEDESC_XUNLOCK(fdp); } } - return (fdrop(fp, td)); + return (fdrop_close(fp, td)); } /* diff --git a/sys/sys/file.h b/sys/sys/file.h index 4efed9807bc8..165ca10c9f76 100644 --- a/sys/sys/file.h +++ b/sys/sys/file.h @@ -299,6 +299,17 @@ fhold(struct file *fp) _error; \ }) +#define fdrop_close(fp, td) ({ \ + struct file *_fp; \ + int _error; \ + \ + _error = 0; \ + _fp = (fp); \ + if (__predict_true(refcount_release(&_fp->f_count))) \ + _error = _fdrop(_fp, td); \ + _error; \ +}) + static __inline fo_rdwr_t fo_read; static __inline fo_rdwr_t fo_write; static __inline fo_truncate_t fo_truncate; -- cgit v1.2.3