From c13facb9c4a036034ff8d898857a81c64c83dc09 Mon Sep 17 00:00:00 2001 From: Ryan Moeller Date: Tue, 16 Jun 2020 12:59:31 -0400 Subject: [PATCH] Fix FreeBSD condvar semantics We should return -1 instead of negative deltas, and 0 if signaled. Reviewed-by: Alexander Motin Reviewed-by: Brian Behlendorf Reviewed-by: Jorgen Lundman Signed-off-by: Ryan Moeller Closes #10460 --- include/os/freebsd/spl/sys/condvar.h | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/include/os/freebsd/spl/sys/condvar.h b/include/os/freebsd/spl/sys/condvar.h index b21940166c..ff5f308e2e 100644 --- a/include/os/freebsd/spl/sys/condvar.h +++ b/include/os/freebsd/spl/sys/condvar.h @@ -134,12 +134,17 @@ cv_timedwait_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, hrtime_t res, tim += hrtime; if (hrtime >= tim) - return (tim - hrtime); + return (-1); + rc = cv_timedwait_sbt(cvp, mp, zfs_nstosbt(tim), zfs_nstosbt(res), C_ABSOLUTE); - KASSERT(rc == EWOULDBLOCK || rc == 0, ("unexpected rc value %d", rc)); - return (tim - gethrtime()); + if (rc == EWOULDBLOCK) + return (-1); + + KASSERT(rc == 0, ("unexpected rc value %d", rc)); + hrtime = tim - gethrtime(); + return ((hrtime > 0) ? hrtime : -1); } static inline clock_t @@ -157,14 +162,22 @@ cv_timedwait_sig_hires(kcondvar_t *cvp, kmutex_t *mp, hrtime_t tim, tim += hrtime; if (hrtime >= tim) - return (tim - hrtime); + return (-1); sbt = zfs_nstosbt(tim); rc = cv_timedwait_sig_sbt(cvp, mp, sbt, zfs_nstosbt(res), C_ABSOLUTE); - KASSERT(rc == EWOULDBLOCK || rc == EINTR || rc == ERESTART || - rc == 0, ("unexpected rc value %d", rc)); - return (tim - gethrtime()); + switch (rc) { + case EWOULDBLOCK: + return (-1); + case EINTR: + case ERESTART: + return (0); + default: + KASSERT(rc == 0, ("unexpected rc value %d", rc)); + hrtime = tim - gethrtime(); + return ((hrtime > 0) ? hrtime : -1); + } } #endif /* _OPENSOLARIS_SYS_CONDVAR_H_ */