On Wed, Jul 27, 2022 at 12:10:00AM +0000, Klemens Nanni wrote:
> On Fri, Jul 22, 2022 at 01:38:16PM -0500, Scott Cheloha wrote:
> > Hi,
> > 
> > As promised, here is the timeout.9 manpage rewrite I've been sitting
> > on.  I am pretty sure jmc@ (and maybe schwarze@) read an earlier
> > version of this.  It has drifted a bit since then, but not much.
> 
> No general mandoc issues or nits I can see, good.
> 
> > My main goal here is to make all the "gotchas" in the timeout API more
> > explicit.  The API is large, so the manpage is necessarily longer than
> > the average manpage.
> 
> In this regard your diff reads good to me.
> 
> > We're also stuck in the midst of an API transition, so there is some
> > overlap in the API coverage.  Hopefully most of that redundancy can be
> > consolidated in the future after I finish the clock interrupt work.
> 
> If by overlap you mean discrepancy between code and documentation,
> please avoid documenting nonexistent code;  new manual bits should come
> with or after new code.  See inline.
> 
> Some functions exist in code but are not yet covered by this diff.
> This seems fine and can be done as next steps.
> 
> Otherwise existing <kern/timeout.h> signatures match timeout.9 after
> you diff.

Sorry for the delay, let's try it again.  Couple updates:

- Remove timeout_in_nsec().  The kernel is not ready for it.  We then
  also no longer need kclock_nanotime() (yet).  Update timeout.9
  accordingly.

- Remove the TIMEOUT_KCLOCK flag.  It is redundant and complicates the
  code.  We can replace all existing TIMEOUT_KCLOCK checks by comparing
  to_kclock with KCLOCK_NONE or KCLOCK_UPTIME.

- In the CONTEXT section of timeout.9, mention that timeouts need to be
  safe to run on any CPU.

Included here are all the needed source changes.  There is only one
caller each for timeout_set_kclock(), timeout_at_ts(), and
TIMEOUT_INITIALIZER_FLAGS(), so changing them is trivial.

Index: share/man/man9/timeout.9
===================================================================
RCS file: /cvs/src/share/man/man9/timeout.9,v
retrieving revision 1.55
diff -u -p -r1.55 timeout.9
--- share/man/man9/timeout.9    22 Jun 2022 14:10:49 -0000      1.55
+++ share/man/man9/timeout.9    9 Oct 2022 22:51:57 -0000
@@ -1,6 +1,7 @@
 .\"    $OpenBSD: timeout.9,v 1.55 2022/06/22 14:10:49 visa Exp $
 .\"
 .\" Copyright (c) 2000 Artur Grabowski <[email protected]>
+.\" Copyright (c) 2021, 2022 Scott Cheloha <[email protected]>
 .\" All rights reserved.
 .\"
 .\" Redistribution and use in source and binary forms, with or without
@@ -36,6 +37,7 @@
 .Nm timeout_add_nsec ,
 .Nm timeout_add_usec ,
 .Nm timeout_add_tv ,
+.Nm timeout_abs_ts ,
 .Nm timeout_del ,
 .Nm timeout_del_barrier ,
 .Nm timeout_barrier ,
@@ -44,281 +46,364 @@
 .Nm timeout_triggered ,
 .Nm TIMEOUT_INITIALIZER ,
 .Nm TIMEOUT_INITIALIZER_FLAGS
-.Nd execute a function after a specified period of time
+.Nd execute a function in the future
 .Sh SYNOPSIS
 .In sys/types.h
 .In sys/timeout.h
 .Ft void
-.Fn timeout_set "struct timeout *to" "void (*fn)(void *)" "void *arg"
+.Fo timeout_set
+.Fa "struct timeout *to"
+.Fa "void (*fn)(void *)"
+.Fa "void *arg"
+.Fc
 .Ft void
 .Fo timeout_set_flags
 .Fa "struct timeout *to"
 .Fa "void (*fn)(void *)"
 .Fa "void *arg"
+.Fa "int kclock"
 .Fa "int flags"
 .Fc
 .Ft void
-.Fn timeout_set_proc "struct timeout *to" "void (*fn)(void *)" "void *arg"
+.Fo timeout_set_proc
+.Fa "struct timeout *to"
+.Fa "void (*fn)(void *)"
+.Fa "void *arg"
+.Fc
 .Ft int
-.Fn timeout_add "struct timeout *to" "int ticks"
+.Fo timeout_add
+.Fa "struct timeout *to"
+.Fa "int nticks"
+.Fc
 .Ft int
-.Fn timeout_del "struct timeout *to"
+.Fo timeout_add_sec
+.Fa "struct timeout *to"
+.Fa "int secs"
+.Fc
 .Ft int
-.Fn timeout_del_barrier "struct timeout *to"
-.Ft void
-.Fn timeout_barrier "struct timeout *to"
+.Fo timeout_add_msec
+.Fa "struct timeout *to"
+.Fa "int msecs"
+.Fc
 .Ft int
-.Fn timeout_pending "struct timeout *to"
+.Fo timeout_add_usec
+.Fa "struct timeout *to"
+.Fa "int usecs"
+.Fc
 .Ft int
-.Fn timeout_initialized "struct timeout *to"
+.Fo timeout_add_nsec
+.Fa "struct timeout *to"
+.Fa "int nsecs"
+.Fc
 .Ft int
-.Fn timeout_triggered "struct timeout *to"
+.Fo timeout_add_tv
+.Fa "struct timeout *to"
+.Fa "struct timeval *tv"
+.Fc
 .Ft int
-.Fn timeout_add_tv "struct timeout *to" "struct timeval *"
+.Fo timeout_abs_ts
+.Fa "struct timeout *to"
+.Fa "const struct timespec *abs"
+.Fc
 .Ft int
-.Fn timeout_add_sec "struct timeout *to" "int sec"
+.Fo timeout_del
+.Fa "struct timeout *to"
+.Fc
+.Ft int
+.Fo timeout_del_barrier
+.Fa "struct timeout *to"
+.Fc
+.Ft void
+.Fo timeout_barrier
+.Fa "struct timeout *to"
+.Fc
 .Ft int
-.Fn timeout_add_msec "struct timeout *to" "int msec"
+.Fo timeout_pending
+.Fa "struct timeout *to"
+.Fc
 .Ft int
-.Fn timeout_add_usec "struct timeout *to" "int usec"
+.Fo timeout_initialized
+.Fa "struct timeout *to"
+.Fc
 .Ft int
-.Fn timeout_add_nsec "struct timeout *to" "int nsec"
-.Fn TIMEOUT_INITIALIZER "void (*fn)(void *)" "void *arg"
-.Fn TIMEOUT_INITIALIZER_FLAGS "void (*fn)(void *)" "void *arg" "int flags"
+.Fo timeout_triggered
+.Fa "struct timeout *to"
+.Fc
+.Fo TIMEOUT_INITIALIZER
+.Fa "void (*fn)(void *)"
+.Fa "void *arg"
+.Fc
+.Fo TIMEOUT_INITIALIZER_FLAGS
+.Fa "void (*fn)(void *)"
+.Fa "void *arg"
+.Fa "int kclock"
+.Fa "int flags"
+.Fc
 .Sh DESCRIPTION
 The
 .Nm timeout
-API provides a mechanism to execute a function at a given time.
-The granularity of the time is limited by the granularity of the
-.Xr hardclock 9
-timer which executes
-.Xr hz 9
-times a second.
+API provides a mechanism to schedule a function for asynchronous
+execution in the future.
 .Pp
-It is the responsibility of the caller to provide these functions with
-pre-allocated timeout structures.
+All state is encapsulated in a caller-allocated timeout structure
+.Pq hereafter, a Qo timeout Qc .
+A timeout must be initialized before it may be used as input to other
+functions in the API.
 .Pp
 The
 .Fn timeout_set
-function prepares the timeout structure
-.Fa to
-to be used in future calls to
-.Fn timeout_add
-and
-.Fn timeout_del .
-The timeout will be prepared to call the function specified by the
+function initializes the timeout
+.Fa to .
+When the timeout is executed,
+the function
 .Fa fn
-argument with a
-.Fa void *
-argument given in the
+will be called with
 .Fa arg
-argument.
-Once initialized, the
-.Fa to
-structure can be used repeatedly in
-.Fn timeout_add
-and
-.Fn timeout_del
-and does not need to be reinitialized unless
-the function called and/or its argument must change.
+as its sole parameter.
+The timeout is implicitly scheduled against the
+.Dv KCLOCK_NONE
+clock and is not configured with any additional flags.
 .Pp
 The
 .Fn timeout_set_flags
 function is similar to
-.Fn timeout_set
-but it additionally accepts the bitwise OR of zero or more of the
-following
+.Fn timeout_set ,
+except that it takes two additional parameters:
+.Bl -tag -width kclock
+.It Fa kclock
+The timeout is scheduled against the given
+.Fa kclock ,
+which must be one of the following:
+.Bl -tag -width KCLOCK_UPTIME
+.It Dv KCLOCK_NONE
+Low resolution tick-based clock.
+The granularity of this clock is limited by the
+.Xr hardclock 9 ,
+which executes roughly
+.Xr hz 9
+times per second.
+.It Dv KCLOCK_UPTIME
+The uptime clock.
+Counts the time elapsed since the system booted.
+.El
+.It Fa flags
+The timeout's behavior may be configured with the bitwise OR of
+zero or more of the following
 .Fa flags :
-.Bl -tag -width TIMEOUT_PROC -offset indent
+.Bl -tag -width TIMEOUT_PROC
 .It Dv TIMEOUT_PROC
-Runs the timeout in a process context instead of the default
+Execute the timeout in a process context instead of the default
 .Dv IPL_SOFTCLOCK
 interrupt context.
 .El
+.El
 .Pp
 The
 .Fn timeout_set_proc
 function is similar to
+.Fn timeout_set ,
+except that the given timeout is configured with the
+.Dv TIMEOUT_PROC
+flag.
+.Pp
+A timeout may also be initialized statically.
+The
+.Fn TIMEOUT_INITIALIZER
+macro is equivalent to the
 .Fn timeout_set
-but it runs the timeout in a process context instead of the default
-.Dv IPL_SOFTCLOCK
-interrupt context.
+function and the
+.Fn TIMEOUT_INITIALIZER_FLAGS
+macro is equivalent to the
+.Fn timeout_set_flags
+function.
 .Pp
-The function
-.Fn timeout_add
-schedules the execution of the
-.Fa to
-timeout in at least
-.Fa ticks Ns /hz
+The interfaces available for scheduling a timeout vary with the
+.Fa kclock
+set during initialization.
+.Pp
+.Dv KCLOCK_NONE
+timeouts may be scheduled with the function
+.Fn timeout_add ,
+which schedules the given timeout to execute after
+.Fa nticks
+.Xr hardclock 9
+ticks have elapsed.
+In practice,
+.Fa nticks
+ticks will usually elapse in slightly less than
+.Pq Fa nticks Cm / Dv hz
 seconds.
 Negative values of
-.Fa ticks
+.Fa nticks
 are illegal.
-If the value is
-.Sq 0
-it will, in the current implementation, be treated as
-.Sq 1 ,
-but in the future it might cause an immediate timeout.
-The timeout in the
-.Fa to
-argument must be already initialized by
-.Fn timeout_set ,
-.Fn timeout_set_flags ,
+If
+.Fa nticks
+is zero it will be silently rounded up to one.
+.Pp
+For convenience,
+.Dv KCLOCK_NONE
+timeouts may also be scheduled with
+.Fn timeout_add_sec ,
+.Fn timeout_add_msec ,
+.Fn timeout_add_usec ,
+.Fn timeout_add_nsec ,
 or
-.Fn timeout_set_proc
-and may not be used in calls to
-.Fn timeout_set ,
-.Fn timeout_set_flags ,
+.Fn timeout_add_tv .
+These wrapper functions convert their input durations to a count of
+.Xr hardclock 9
+ticks before calling
+.Fn timeout_add
+to schedule the given timeout.
+.Pp
+Timeouts for any other
+.Fa kclock
+may be scheduled with
+.Fn timeout_abs_ts ,
+which schedules the given timeout to execute at or after the absolute time
+.Fa abs
+has elapsed on the timeout's
+.Fa kclock .
+.Pp
+Once scheduled,
+a timeout is said to be
+.Qq pending .
+A pending timeout may not be reinitialized with
+.Fn timeout_set
 or
-.Fn timeout_set_proc
-until it has timed out or been removed with
-.Fn timeout_del .
-If the timeout in the
-.Fa to
-argument is already scheduled, the old execution time will be
-replaced by the new one.
+.Fn timeout_set_flags
+until it has been executed or it has been cancelled with
+.Fn timeout_del
+or
+.Fn timeout_del_barrier .
+A pending timeout may be rescheduled without first cancelling it with
+.Fn timeout_del
+or
+.Fn timeout_del_barrier .
+The new expiration time will quietly supersede the original.
 .Pp
 The function
 .Fn timeout_del
-will cancel the timeout in the argument
-.Fa to .
-If the timeout has already executed or has never been added,
+cancels any pending execution of the given timeout.
+If the timeout has already executed or was never scheduled,
 the call will have no effect.
 .Pp
+The
 .Fn timeout_del_barrier
-is like
-.Fn timeout_del
-but it will wait until any current execution of the timeout has completed.
+function is similar to
+.Fn timeout_del ,
+except that it may block until any current execution of the given timeout
+has completed.
 .Pp
+The
 .Fn timeout_barrier
-ensures that any current execution of the timeout in the argument
-.Fa to
-has completed before returning.
+function blocks until any current execution of the given timeout
+has completed.
 .Pp
-The caller of
+Callers of
 .Fn timeout_barrier
-or
+and
 .Fn timeout_del_barrier
 must not hold locks that can block processing in the timeout's context.
-Otherwise, the system will deadlock.
+Otherwise,
+the system will deadlock.
 .Pp
 The
 .Fn timeout_pending
-macro can be used to check if a timeout is scheduled to run.
+macro indicates whether the given timeout is scheduled for execution.
+A timeout's pending status is cleared when it executes or is cancelled.
 .Pp
 The
 .Fn timeout_initialized
-macro can be used to check if a timeout has been initialized.
+macro indicates whether the given timeout has been initialized with
+.Fn timeout_set
+or
+.Fn timeout_set_flags .
+This macro must not be used unless the memory pointed to by
+.Fa to
+has been zeroed,
+or its return value is meaningless.
 .Pp
 The
 .Fn timeout_triggered
-macro can be used to check if a timeout is running or has been run.
-The
-.Fn timeout_add
-and
-.Fn timeout_del
-functions clear the triggered state for that timeout.
-.Pp
-When possible, use the
-.Fn timeout_add_tv ,
-.Fn timeout_add_sec ,
-.Fn timeout_add_msec ,
-.Fn timeout_add_usec ,
-and
-.Fn timeout_add_nsec
-functions instead of
-.Fn timeout_add .
-Those functions add a timeout whilst converting the time specified
-by the respective types.
-They also defer the timeout handler for at least one tick if called
-with a positive value.
-.Pp
-A timeout declaration can be initialised with the
-.Fn TIMEOUT_INITIALIZER
-macro.
-The timeout will be prepared to call the function specified by the
-.Fa fn
-argument with the
-.Fa void *
-argument given in
-.Fa arg .
-.Pp
-The
-.Fn TIMEOUT_INITIALIZER_FLAGS
-macro is similar to
-.Fn TIMEOUT_INITIALIZER ,
-but it accepts additional flags.
-See the
-.Fn timeout_set_flags
-function for details.
+macro indicates whether the given timeout is executing or has finished
+executing.
+Rescheduling or cancelling a timeout clears its triggered status.
 .Sh CONTEXT
 .Fn timeout_set ,
 .Fn timeout_set_flags ,
-and
-.Fn timeout_set_proc
-can be called during autoconf, from process context, or from interrupt
-context.
-.Pp
+.Fn timeout_set_proc ,
 .Fn timeout_add ,
 .Fn timeout_add_sec ,
 .Fn timeout_add_msec ,
-.Fn timeout_add_nsec ,
 .Fn timeout_add_usec ,
+.Fn timeout_add_nsec ,
 .Fn timeout_add_tv ,
+.Fn timeout_abs_ts ,
 .Fn timeout_del ,
 .Fn timeout_pending ,
 .Fn timeout_initialized ,
+and
 .Fn timeout_triggered
-can be called during autoconf, from process context, or from any
-interrupt context at or below
-.Dv IPL_CLOCK .
+may be called during autoconf,
+from process context,
+or from interrupt context.
 .Pp
 .Fn timeout_barrier
 and
 .Fn timeout_del_barrier
-can be called from process context.
+may only be called from process context.
 .Pp
-When the timeout runs, the
+When a timeout is executed,
+the function
 .Fa fn
-argument to
-.Fn timeout_set
-or
-.Fn timeout_set_flags
-will be called in an interrupt context at
+set during initialization is called from the
 .Dv IPL_SOFTCLOCK
-or a process context if the
+interrupt context,
+or a process context if the timeout was configured with the
 .Dv TIMEOUT_PROC
-flag was given at initialization.
-The
+flag.
+The function
 .Fa fn
-argument to
-.Fn timeout_set_proc
-will be called in a process context.
+must not block and must be safe to execute on any CPU in the system.
+.Pp
+Currently,
+all timeouts are executed under the kernel lock.
 .Sh RETURN VALUES
 .Fn timeout_add ,
 .Fn timeout_add_sec ,
 .Fn timeout_add_msec ,
-.Fn timeout_add_nsec ,
 .Fn timeout_add_usec ,
+.Fn timeout_add_nsec ,
+.Fn timeout_add_tv ,
 and
-.Fn timeout_add_tv
-will return 1 if the timeout
+.Fn timeout_abs_ts
+return 1 if the timeout
 .Fa to
-was added to the timeout schedule or 0 if it was already queued.
+is newly scheduled,
+or zero if the timeout was already pending.
 .Pp
 .Fn timeout_del
 and
 .Fn timeout_del_barrier
-will return 1 if the timeout
+return 1 if the timeout
 .Fa to
-was removed from the pending timeout schedule or 0 if it was not
-currently queued.
+was pending,
+or zero otherwise.
+.Pp
+.Fn timeout_pending ,
+.Fn timeout_initialized ,
+and
+.Fn timeout_triggered
+return non-zero if the corresponding condition is true,
+or zero otherwise.
 .Sh CODE REFERENCES
-These functions are implemented in the file
-.Pa sys/kern/kern_timeout.c .
+.Pa sys/kern/kern_timeout.c
 .Sh SEE ALSO
+.Xr hardclock 9 ,
 .Xr hz 9 ,
+.Xr microtime 9 ,
 .Xr splclock 9 ,
+.Xr task_add 9 ,
 .Xr tsleep 9 ,
 .Xr tvtohz 9
 .Rs
Index: sys/sys/timeout.h
===================================================================
RCS file: /cvs/src/sys/sys/timeout.h,v
retrieving revision 1.43
diff -u -p -r1.43 timeout.h
--- sys/sys/timeout.h   13 Jul 2021 17:50:19 -0000      1.43
+++ sys/sys/timeout.h   9 Oct 2022 22:51:57 -0000
@@ -54,7 +54,6 @@ struct timeout {
 #define TIMEOUT_ONQUEUE                0x02    /* on any timeout queue */
 #define TIMEOUT_INITIALIZED    0x04    /* initialized */
 #define TIMEOUT_TRIGGERED      0x08    /* running or ran */
-#define TIMEOUT_KCLOCK         0x10    /* clock-based timeout */
 
 struct timeoutstat {
        uint64_t tos_added;             /* timeout_add*(9) calls */
@@ -88,7 +87,7 @@ int timeout_sysctl(void *, size_t *, voi
 #define KCLOCK_UPTIME  0               /* uptime clock; time since boot */
 #define KCLOCK_MAX     1
 
-#define __TIMEOUT_INITIALIZER(_fn, _arg, _kclock, _flags) {            \
+#define TIMEOUT_INITIALIZER_FLAGS(_fn, _arg, _kclock, _flags) {                
\
        .to_list = { NULL, NULL },                                      \
        .to_abstime = { .tv_sec = 0, .tv_nsec = 0 },                    \
        .to_func = (_fn),                                               \
@@ -98,18 +97,11 @@ int timeout_sysctl(void *, size_t *, voi
        .to_kclock = (_kclock)                                          \
 }
 
-#define TIMEOUT_INITIALIZER_KCLOCK(_fn, _arg, _kclock, _flags)         \
-    __TIMEOUT_INITIALIZER((_fn), (_arg), (_kclock), (_flags) | TIMEOUT_KCLOCK)
-
-#define TIMEOUT_INITIALIZER_FLAGS(_fn, _arg, _flags)                   \
-    __TIMEOUT_INITIALIZER((_fn), (_arg), KCLOCK_NONE, (_flags))
-
 #define TIMEOUT_INITIALIZER(_f, _a)                                    \
-    __TIMEOUT_INITIALIZER((_f), (_a), KCLOCK_NONE, 0)
+    TIMEOUT_INITIALIZER_FLAGS((_f), (_a), KCLOCK_NONE, 0)
 
 void timeout_set(struct timeout *, void (*)(void *), void *);
-void timeout_set_flags(struct timeout *, void (*)(void *), void *, int);
-void timeout_set_kclock(struct timeout *, void (*)(void *), void *, int, int);
+void timeout_set_flags(struct timeout *, void (*)(void *), void *, int, int);
 void timeout_set_proc(struct timeout *, void (*)(void *), void *);
 
 int timeout_add(struct timeout *, int);
@@ -119,8 +111,7 @@ int timeout_add_msec(struct timeout *, i
 int timeout_add_usec(struct timeout *, int);
 int timeout_add_nsec(struct timeout *, int);
 
-int timeout_at_ts(struct timeout *, const struct timespec *);
-int timeout_in_nsec(struct timeout *, uint64_t);
+int timeout_abs_ts(struct timeout *, const struct timespec *);
 
 int timeout_del(struct timeout *);
 int timeout_del_barrier(struct timeout *);
Index: sys/kern/kern_fork.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_fork.c,v
retrieving revision 1.242
diff -u -p -r1.242 kern_fork.c
--- sys/kern/kern_fork.c        14 Aug 2022 01:58:27 -0000      1.242
+++ sys/kern/kern_fork.c        9 Oct 2022 22:51:57 -0000
@@ -198,7 +198,7 @@ process_initialize(struct process *pr, s
        rw_init(&pr->ps_lock, "pslock");
        mtx_init(&pr->ps_mtx, IPL_HIGH);
 
-       timeout_set_kclock(&pr->ps_realit_to, realitexpire, pr,
+       timeout_set_flags(&pr->ps_realit_to, realitexpire, pr,
            KCLOCK_UPTIME, 0);
        timeout_set(&pr->ps_rucheck_to, rucheck, pr);
 }
Index: sys/kern/kern_time.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_time.c,v
retrieving revision 1.157
diff -u -p -r1.157 kern_time.c
--- sys/kern/kern_time.c        14 Aug 2022 01:58:27 -0000      1.157
+++ sys/kern/kern_time.c        9 Oct 2022 22:51:58 -0000
@@ -548,7 +548,8 @@ setitimer(int which, const struct itimer
                if (which == ITIMER_REAL) {
                        if (timespecisset(&its.it_value)) {
                                timespecadd(&its.it_value, &now, &its.it_value);
-                               timeout_at_ts(&pr->ps_realit_to, &its.it_value);
+                               timeout_abs_ts(&pr->ps_realit_to,
+                                   &its.it_value);
                        } else
                                timeout_del(&pr->ps_realit_to);
                }
@@ -691,7 +692,7 @@ realitexpire(void *arg)
        while (timespeccmp(&tp->it_value, &cts, <=))
                timespecadd(&tp->it_value, &tp->it_interval, &tp->it_value);
        if ((pr->ps_flags & PS_EXITING) == 0)
-               timeout_at_ts(&pr->ps_realit_to, &tp->it_value);
+               timeout_abs_ts(&pr->ps_realit_to, &tp->it_value);
 
 out:
        mtx_leave(&pr->ps_mtx);
Index: sys/kern/kern_timeout.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_timeout.c,v
retrieving revision 1.85
diff -u -p -r1.85 kern_timeout.c
--- sys/kern/kern_timeout.c     19 Jun 2021 02:05:33 -0000      1.85
+++ sys/kern/kern_timeout.c     9 Oct 2022 22:51:59 -0000
@@ -166,7 +166,6 @@ struct lock_type timeout_spinlock_type =
        ((needsproc) ? &timeout_sleeplock_obj : &timeout_spinlock_obj)
 #endif
 
-void kclock_nanotime(int, struct timespec *);
 void softclock(void *);
 void softclock_create_thread(void *);
 void softclock_process_kclock_timeout(struct timeout *, int);
@@ -269,9 +268,10 @@ timeout_set(struct timeout *new, void (*
 }
 
 void
-timeout_set_flags(struct timeout *to, void (*fn)(void *), void *arg, int flags)
+timeout_set_flags(struct timeout *to, void (*fn)(void *), void *arg, int 
kclock,
+    int flags)
 {
-       _timeout_set(to, fn, arg, KCLOCK_NONE, flags);
+       _timeout_set(to, fn, arg, kclock, flags);
 }
 
 void
@@ -280,13 +280,6 @@ timeout_set_proc(struct timeout *new, vo
        _timeout_set(new, fn, arg, KCLOCK_NONE, TIMEOUT_PROC);
 }
 
-void
-timeout_set_kclock(struct timeout *to, void (*fn)(void *), void *arg,
-    int kclock, int flags)
-{
-       _timeout_set(to, fn, arg, kclock, flags | TIMEOUT_KCLOCK);
-}
-
 int
 timeout_add(struct timeout *new, int to_ticks)
 {
@@ -294,7 +287,6 @@ timeout_add(struct timeout *new, int to_
        int ret = 1;
 
        KASSERT(ISSET(new->to_flags, TIMEOUT_INITIALIZED));
-       KASSERT(!ISSET(new->to_flags, TIMEOUT_KCLOCK));
        KASSERT(new->to_kclock == KCLOCK_NONE);
        KASSERT(to_ticks >= 0);
 
@@ -395,15 +387,15 @@ timeout_add_nsec(struct timeout *to, int
 }
 
 int
-timeout_at_ts(struct timeout *to, const struct timespec *abstime)
+timeout_abs_ts(struct timeout *to, const struct timespec *abstime)
 {
        struct timespec old_abstime;
        int ret = 1;
 
        mtx_enter(&timeout_mutex);
 
-       KASSERT(ISSET(to->to_flags, TIMEOUT_INITIALIZED | TIMEOUT_KCLOCK));
-       KASSERT(to->to_kclock != KCLOCK_NONE);
+       KASSERT(ISSET(to->to_flags, TIMEOUT_INITIALIZED));
+       KASSERT(to->to_kclock == KCLOCK_UPTIME);
 
        old_abstime = to->to_abstime;
        to->to_abstime = *abstime;
@@ -431,30 +423,6 @@ timeout_at_ts(struct timeout *to, const 
 }
 
 int
-timeout_in_nsec(struct timeout *to, uint64_t nsecs)
-{
-       struct timespec abstime, interval, now;
-
-       kclock_nanotime(to->to_kclock, &now);
-       NSEC_TO_TIMESPEC(nsecs, &interval);
-       timespecadd(&now, &interval, &abstime);
-
-       return timeout_at_ts(to, &abstime);
-}
-
-void
-kclock_nanotime(int kclock, struct timespec *now)
-{
-       switch (kclock) {
-       case KCLOCK_UPTIME:
-               nanouptime(now);
-               break;
-       default:
-               panic("invalid kclock: 0x%x", kclock);
-       }
-}
-
-int
 timeout_del(struct timeout *to)
 {
        int ret = 0;
@@ -497,7 +465,8 @@ timeout_barrier(struct timeout *to)
        procflag = (to->to_flags & TIMEOUT_PROC);
        timeout_sync_order(procflag);
 
-       timeout_set_flags(&barrier, timeout_barrier_timeout, &c, procflag);
+       timeout_set_flags(&barrier, timeout_barrier_timeout, &c, KCLOCK_NONE,
+           procflag);
        barrier.to_process = curproc->p_p;
        cond_init(&c);
 
@@ -531,13 +500,14 @@ timeout_barrier_timeout(void *arg)
 uint32_t
 timeout_bucket(const struct timeout *to)
 {
-       struct kclock *kc = &timeout_kclock[to->to_kclock];
+       struct kclock *kc;
        struct timespec diff, shifted_abstime;
        uint32_t level;
 
-       KASSERT(ISSET(to->to_flags, TIMEOUT_KCLOCK));
-       KASSERT(timespeccmp(&kc->kc_lastscan, &to->to_abstime, <));
+       KASSERT(to->to_kclock == KCLOCK_UPTIME);
+       kc = &timeout_kclock[to->to_kclock];
 
+       KASSERT(timespeccmp(&kc->kc_lastscan, &to->to_abstime, <));
        timespecsub(&to->to_abstime, &kc->kc_lastscan, &diff);
        for (level = 0; level < nitems(timeout_level_width) - 1; level++) {
                if (diff.tv_sec < timeout_level_width[level])
@@ -750,10 +720,10 @@ softclock(void *arg)
                CIRCQ_REMOVE(&to->to_list);
                if (to == first_new)
                        new = 1;
-               if (ISSET(to->to_flags, TIMEOUT_KCLOCK))
-                       softclock_process_kclock_timeout(to, new);
-               else
+               if (to->to_kclock == KCLOCK_NONE)
                        softclock_process_tick_timeout(to, new);
+               else
+                       softclock_process_kclock_timeout(to, new);
        }
        tostat.tos_softclocks++;
        needsproc = !CIRCQ_EMPTY(&timeout_proc);
@@ -807,7 +777,7 @@ softclock_thread(void *arg)
        splx(s);
 }
 
-#ifndef SMALL_KERNEL
+#if !defined(SMALL_KERNEL)
 void
 timeout_adjust_ticks(int adj)
 {
@@ -915,24 +885,24 @@ db_show_timeout(struct timeout *to, stru
        else if (bucket == &timeout_proc)
                where = "thread";
        else {
-               if (ISSET(to->to_flags, TIMEOUT_KCLOCK))
-                       wheel = timeout_wheel_kc;
-               else
+               if (to->to_kclock == KCLOCK_NONE)
                        wheel = timeout_wheel;
+               else
+                       wheel = timeout_wheel_kc;
                snprintf(buf, sizeof(buf), "%3ld/%1ld",
                    (bucket - wheel) % WHEELSIZE,
                    (bucket - wheel) / WHEELSIZE);
                where = buf;
        }
-       if (ISSET(to->to_flags, TIMEOUT_KCLOCK)) {
+       if (to->to_kclock == KCLOCK_NONE) {
+               db_printf("%20d  %8s  %7s  0x%0*lx  %s\n",
+                   to->to_time - ticks, "ticks", where,
+                   width, (ulong)to->to_arg, name);
+       } else {
                kc = &timeout_kclock[to->to_kclock];
                timespecsub(&to->to_abstime, &kc->kc_lastscan, &remaining);
                db_printf("%20s  %8s  %7s  0x%0*lx  %s\n",
                    db_timespec(&remaining), db_kclock(to->to_kclock), where,
-                   width, (ulong)to->to_arg, name);
-       } else {
-               db_printf("%20d  %8s  %7s  0x%0*lx  %s\n",
-                   to->to_time - ticks, "ticks", where,
                    width, (ulong)to->to_arg, name);
        }
 }
Index: sys/netinet/ip_ipsp.c
===================================================================
RCS file: /cvs/src/sys/netinet/ip_ipsp.c,v
retrieving revision 1.273
diff -u -p -r1.273 ip_ipsp.c
--- sys/netinet/ip_ipsp.c       6 Aug 2022 15:57:59 -0000       1.273
+++ sys/netinet/ip_ipsp.c       9 Oct 2022 22:51:59 -0000
@@ -119,7 +119,7 @@ void ipsp_ids_gc(void *);
 LIST_HEAD(, ipsec_ids) ipsp_ids_gc_list =
     LIST_HEAD_INITIALIZER(ipsp_ids_gc_list);   /* [F] */
 struct timeout ipsp_ids_gc_timeout =
-    TIMEOUT_INITIALIZER_FLAGS(ipsp_ids_gc, NULL, TIMEOUT_PROC);
+    TIMEOUT_INITIALIZER_FLAGS(ipsp_ids_gc, NULL, KCLOCK_NONE, TIMEOUT_PROC);
 
 static inline int ipsp_ids_cmp(const struct ipsec_ids *,
     const struct ipsec_ids *);

Reply via email to