summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Pieuchot <mpi@cvs.openbsd.org>2020-01-14 08:52:19 +0000
committerMartin Pieuchot <mpi@cvs.openbsd.org>2020-01-14 08:52:19 +0000
commit34cd52d5abc5e201d459c009553ed98e8039b325 (patch)
tree54dfb4932a4ef2b0e444707a171831400e6be392
parent027bed726054577818b1ccd0227a098e14709bd8 (diff)
Introduce TIMESPEC_TO_NSEC() and use it to convert userland facing
tsleep(9) to tsleep_nsec(9). ok bluhm@
-rw-r--r--sys/kern/kern_sig.c20
-rw-r--r--sys/kern/kern_synch.c12
-rw-r--r--sys/kern/sys_futex.c11
-rw-r--r--sys/sys/time.h10
4 files changed, 24 insertions, 29 deletions
diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c
index 5f831c1727b..cabca171b50 100644
--- a/sys/kern/kern_sig.c
+++ b/sys/kern/kern_sig.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_sig.c,v 1.240 2020/01/08 16:27:41 visa Exp $ */
+/* $OpenBSD: kern_sig.c,v 1.241 2020/01/14 08:52:18 mpi Exp $ */
/* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */
/*
@@ -1699,7 +1699,7 @@ sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
sigset_t *m;
sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask;
siginfo_t si;
- uint64_t to_ticks = 0;
+ uint64_t nsecs = INFSLP;
int timeinvalid = 0;
int error = 0;
@@ -1715,14 +1715,8 @@ sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
#endif
if (!timespecisvalid(&ts))
timeinvalid = 1;
- else {
- to_ticks = (uint64_t)hz * ts.tv_sec +
- ts.tv_nsec / (tick * 1000);
- if (to_ticks > INT_MAX)
- to_ticks = INT_MAX;
- if (to_ticks == 0 && ts.tv_nsec)
- to_ticks = 1;
- }
+ else
+ nsecs = TIMESPEC_TO_NSEC(&ts);
}
dosigsuspend(p, p->p_sigmask &~ mask);
@@ -1749,14 +1743,14 @@ sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
if (timeinvalid)
error = EINVAL;
- if (SCARG(uap, timeout) != NULL && to_ticks == 0)
+ if (SCARG(uap, timeout) != NULL && nsecs == INFSLP)
error = EAGAIN;
if (error != 0)
break;
- error = tsleep(&sigwaitsleep, PPAUSE|PCATCH, "sigwait",
- (int)to_ticks);
+ error = tsleep_nsec(&sigwaitsleep, PPAUSE|PCATCH, "sigwait",
+ nsecs);
}
if (error == 0) {
diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c
index 5e33a94106d..7d3c86481f3 100644
--- a/sys/kern/kern_synch.c
+++ b/sys/kern/kern_synch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: kern_synch.c,v 1.156 2020/01/12 00:01:12 cheloha Exp $ */
+/* $OpenBSD: kern_synch.c,v 1.157 2020/01/14 08:52:18 mpi Exp $ */
/* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */
/*
@@ -641,7 +641,7 @@ thrsleep(struct proc *p, struct sys___thrsleep_args *v)
long ident = (long)SCARG(uap, ident);
struct timespec *tsp = (struct timespec *)SCARG(uap, tp);
void *lock = SCARG(uap, lock);
- uint64_t to_ticks = 0;
+ uint64_t nsecs = INFSLP;
int abort, error;
clockid_t clock_id = SCARG(uap, clock_id);
@@ -665,10 +665,7 @@ thrsleep(struct proc *p, struct sys___thrsleep_args *v)
}
timespecsub(tsp, &now, tsp);
- to_ticks = (uint64_t)hz * tsp->tv_sec +
- (tsp->tv_nsec + tick * 1000 - 1) / (tick * 1000) + 1;
- if (to_ticks > INT_MAX)
- to_ticks = INT_MAX;
+ nsecs = TIMESPEC_TO_NSEC(tsp);
}
p->p_thrslpid = ident;
@@ -692,8 +689,7 @@ thrsleep(struct proc *p, struct sys___thrsleep_args *v)
void *sleepaddr = &p->p_thrslpid;
if (ident == -1)
sleepaddr = &globalsleepaddr;
- error = tsleep(sleepaddr, PWAIT|PCATCH, "thrsleep",
- (int)to_ticks);
+ error = tsleep_nsec(sleepaddr, PWAIT|PCATCH, "thrsleep", nsecs);
}
out:
diff --git a/sys/kern/sys_futex.c b/sys/kern/sys_futex.c
index 0fbb31ad9ef..fa932e41bac 100644
--- a/sys/kern/sys_futex.c
+++ b/sys/kern/sys_futex.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sys_futex.c,v 1.13 2019/07/10 15:52:17 mpi Exp $ */
+/* $OpenBSD: sys_futex.c,v 1.14 2020/01/14 08:52:18 mpi Exp $ */
/*
* Copyright (c) 2016-2017 Martin Pieuchot
@@ -213,7 +213,7 @@ futex_wait(uint32_t *uaddr, uint32_t val, const struct timespec *timeout,
{
struct proc *p = curproc;
struct futex *f;
- uint64_t to_ticks = 0;
+ uint64_t nsecs = INFSLP;
uint32_t cval;
int error;
@@ -244,17 +244,14 @@ futex_wait(uint32_t *uaddr, uint32_t val, const struct timespec *timeout,
#endif
if (ts.tv_sec < 0 || !timespecisvalid(&ts))
return EINVAL;
- to_ticks = (uint64_t)hz * ts.tv_sec +
- (ts.tv_nsec + tick * 1000 - 1) / (tick * 1000) + 1;
- if (to_ticks > INT_MAX)
- to_ticks = INT_MAX;
+ nsecs = TIMESPEC_TO_NSEC(&ts);
}
f = futex_get(uaddr, flags | FT_CREATE);
TAILQ_INSERT_TAIL(&f->ft_threads, p, p_fut_link);
p->p_futex = f;
- error = rwsleep(p, &ftlock, PWAIT|PCATCH, "fsleep", (int)to_ticks);
+ error = rwsleep_nsec(p, &ftlock, PWAIT|PCATCH, "fsleep", nsecs);
if (error == ERESTART)
error = ECANCELED;
else if (error == EWOULDBLOCK) {
diff --git a/sys/sys/time.h b/sys/sys/time.h
index a6211a165df..faa25eb7e2a 100644
--- a/sys/sys/time.h
+++ b/sys/sys/time.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: time.h,v 1.48 2020/01/01 14:09:59 bluhm Exp $ */
+/* $OpenBSD: time.h,v 1.49 2020/01/14 08:52:18 mpi Exp $ */
/* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */
/*
@@ -372,6 +372,14 @@ USEC_TO_NSEC(uint64_t microseconds)
return microseconds * 1000ULL;
}
+static inline uint64_t
+TIMESPEC_TO_NSEC(const struct timespec *ts)
+{
+ if (ts->tv_sec > (UINT64_MAX - ts->tv_nsec) / 1000000000ULL)
+ return UINT64_MAX;
+ return ts->tv_sec * 1000000000ULL + ts->tv_nsec;
+}
+
#else /* !_KERNEL */
#include <time.h>