1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-01-17 15:27:36 +00:00

Drain grouptaskqueue of the gtask before detaching it.

taskqgroup_detach() would remove the task even if it was running or
enqueued, which could lead to panics (see D17404). With this change,
taskqgroup_detach() drains the task and sets a new flag which prevents the
task from being scheduled again.

I've added grouptask_block() and grouptask_unblock() to allow control
over the flag from other locations as well.

Reviewed by:	Jeffrey Pieper <jeffrey.e.pieper@intel.com>
MFC after:	1 week
Sponsored by:	Limelight Networks
Differential Revision:	https://reviews.freebsd.org/D17674
This commit is contained in:
Stephen Hurd 2018-10-29 14:36:03 +00:00
parent 5beb550712
commit 5201e0f110
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=339861
2 changed files with 58 additions and 3 deletions

View File

@ -51,6 +51,8 @@ __FBSDID("$FreeBSD$");
static MALLOC_DEFINE(M_GTASKQUEUE, "gtaskqueue", "Group Task Queues");
static void gtaskqueue_thread_enqueue(void *);
static void gtaskqueue_thread_loop(void *arg);
static int task_is_running(struct gtaskqueue *queue, struct gtask *gtask);
static void gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask);
TASKQGROUP_DEFINE(softirq, mp_ncpus, 1);
TASKQGROUP_DEFINE(config, 1, 1);
@ -183,6 +185,44 @@ gtaskqueue_free(struct gtaskqueue *queue)
free(queue, M_GTASKQUEUE);
}
/*
* Wait for all to complete, then prevent it from being enqueued
*/
void
grouptask_block(struct grouptask *grouptask)
{
struct gtaskqueue *queue = grouptask->gt_taskqueue;
struct gtask *gtask = &grouptask->gt_task;
#ifdef INVARIANTS
if (queue == NULL) {
gtask_dump(gtask);
panic("queue == NULL");
}
#endif
TQ_LOCK(queue);
gtask->ta_flags |= TASK_NOENQUEUE;
gtaskqueue_drain_locked(queue, gtask);
TQ_UNLOCK(queue);
}
void
grouptask_unblock(struct grouptask *grouptask)
{
struct gtaskqueue *queue = grouptask->gt_taskqueue;
struct gtask *gtask = &grouptask->gt_task;
#ifdef INVARIANTS
if (queue == NULL) {
gtask_dump(gtask);
panic("queue == NULL");
}
#endif
TQ_LOCK(queue);
gtask->ta_flags &= ~TASK_NOENQUEUE;
TQ_UNLOCK(queue);
}
int
grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
{
@ -197,6 +237,10 @@ grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *gtask)
TQ_UNLOCK(queue);
return (0);
}
if (gtask->ta_flags & TASK_NOENQUEUE) {
TQ_UNLOCK(queue);
return (EAGAIN);
}
STAILQ_INSERT_TAIL(&queue->tq_queue, gtask, ta_link);
gtask->ta_flags |= TASK_ENQUEUED;
TQ_UNLOCK(queue);
@ -378,6 +422,13 @@ gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask)
return (error);
}
static void
gtaskqueue_drain_locked(struct gtaskqueue *queue, struct gtask *gtask)
{
while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
TQ_SLEEP(queue, gtask, &queue->tq_mutex, PWAIT, "-", 0);
}
void
gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
{
@ -386,8 +437,7 @@ gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *gtask)
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
TQ_LOCK(queue);
while ((gtask->ta_flags & TASK_ENQUEUED) || task_is_running(queue, gtask))
TQ_SLEEP(queue, gtask, &queue->tq_mutex, PWAIT, "-", 0);
gtaskqueue_drain_locked(queue, gtask);
TQ_UNLOCK(queue);
}
@ -803,6 +853,7 @@ taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
{
int i;
grouptask_block(gtask);
mtx_lock(&qgroup->tqg_lock);
for (i = 0; i < qgroup->tqg_cnt; i++)
if (qgroup->tqg_queue[i].tgc_taskq == gtask->gt_taskqueue)
@ -813,6 +864,7 @@ taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask)
LIST_REMOVE(gtask, gt_list);
mtx_unlock(&qgroup->tqg_lock);
gtask->gt_taskqueue = NULL;
gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE;
}
static void

View File

@ -52,7 +52,9 @@ int gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask);
void gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *task);
void gtaskqueue_drain_all(struct gtaskqueue *queue);
int grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *task);
void grouptask_block(struct grouptask *grouptask);
void grouptask_unblock(struct grouptask *grouptask);
int grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *task);
void taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *grptask,
void *uniq, int irq, const char *name);
int taskqgroup_attach_cpu(struct taskqgroup *qgroup, struct grouptask *grptask,
@ -67,6 +69,7 @@ void taskqgroup_config_gtask_deinit(struct grouptask *gtask);
#define TASK_ENQUEUED 0x1
#define TASK_SKIP_WAKEUP 0x2
#define TASK_NOENQUEUE 0x4
#define GTASK_INIT(task, flags, priority, func, context) do { \