diff options
author | hselasky <hselasky@FreeBSD.org> | 2016-10-10 11:25:11 +0000 |
---|---|---|
committer | hselasky <hselasky@FreeBSD.org> | 2016-10-10 11:25:11 +0000 |
commit | f0923b024b3150f8f0eda99e5a5665e7f5960b58 (patch) | |
tree | c1f4fcc78b930dbe5a9b97fff0e241633b86eff6 | |
parent | f3eddd4e73c64e4b2b7b2ca5b968e6d742f3669a (diff) | |
download | FreeBSD-src-f0923b024b3150f8f0eda99e5a5665e7f5960b58.zip FreeBSD-src-f0923b024b3150f8f0eda99e5a5665e7f5960b58.tar.gz |
MFC r306441 and r306634:
While draining a timeout task prevent the taskqueue_enqueue_timeout()
function from restarting the timer.
Commonly taskqueue_enqueue_timeout() is called from within the task
function itself without any checks for teardown. Then it can happen
the timer stays active after the return of taskqueue_drain_timeout(),
because the timeout and task is drained separately.
This patch factors out the teardown flag into the timeout task itself,
allowing existing code to stay as-is instead of applying a teardown
flag to each and every of the timeout task consumers.
Add assert to taskqueue_drain_timeout() which prevents parallel
execution on the same timeout task.
Update manual page documenting the return value of
taskqueue_enqueue_timeout().
Differential Revision: https://reviews.freebsd.org/D8012
Reviewed by: kib, trasz
-rw-r--r-- | share/man/man9/taskqueue.9 | 2 | ||||
-rw-r--r-- | sys/kern/subr_taskqueue.c | 23 |
2 files changed, 24 insertions, 1 deletions
diff --git a/share/man/man9/taskqueue.9 b/share/man/man9/taskqueue.9 index 910b33e..4391a18 100644 --- a/share/man/man9/taskqueue.9 +++ b/share/man/man9/taskqueue.9 @@ -221,6 +221,8 @@ Otherwise, the task is scheduled for enqueueing in the future, after the absolute value of .Va ticks is passed. +This function returns -1 if the task is being drained. +Otherwise, the number of pending calls is returned. .Pp The .Fn taskqueue_cancel diff --git a/sys/kern/subr_taskqueue.c b/sys/kern/subr_taskqueue.c index c9addb1..ef090bb 100644 --- a/sys/kern/subr_taskqueue.c +++ b/sys/kern/subr_taskqueue.c @@ -75,6 +75,7 @@ struct taskqueue { #define TQ_FLAGS_UNLOCKED_ENQUEUE (1 << 2) #define DT_CALLOUT_ARMED (1 << 0) +#define DT_DRAIN_IN_PROGRESS (1 << 1) #define TQ_LOCK(tq) \ do { \ @@ -279,7 +280,11 @@ taskqueue_enqueue_timeout(struct taskqueue *queue, KASSERT(!queue->tq_spin, ("Timeout for spin-queue")); timeout_task->q = queue; res = timeout_task->t.ta_pending; - if (ticks == 0) { + if (timeout_task->f & DT_DRAIN_IN_PROGRESS) { + /* Do nothing */ + TQ_UNLOCK(queue); + res = -1; + } else if (ticks == 0) { taskqueue_enqueue_locked(queue, &timeout_task->t); /* The lock is released inside. */ } else { @@ -485,8 +490,24 @@ taskqueue_drain_timeout(struct taskqueue *queue, struct timeout_task *timeout_task) { + /* + * Set flag to prevent timer from re-starting during drain: + */ + TQ_LOCK(queue); + KASSERT((timeout_task->f & DT_DRAIN_IN_PROGRESS) == 0, + ("Drain already in progress")); + timeout_task->f |= DT_DRAIN_IN_PROGRESS; + TQ_UNLOCK(queue); + callout_drain(&timeout_task->c); taskqueue_drain(queue, &timeout_task->t); + + /* + * Clear flag to allow timer to re-start: + */ + TQ_LOCK(queue); + timeout_task->f &= ~DT_DRAIN_IN_PROGRESS; + TQ_UNLOCK(queue); } static void |