Author: shurd
Date: Mon Oct 29 14:36:03 2018
New Revision: 339861
URL: https://svnweb.freebsd.org/changeset/base/339861

Log:
  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.pie...@intel.com>
  MFC after:    1 week
  Sponsored by: Limelight Networks
  Differential Revision:        https://reviews.freebsd.org/D17674

Modified:
  head/sys/kern/subr_gtaskqueue.c
  head/sys/sys/gtaskqueue.h

Modified: head/sys/kern/subr_gtaskqueue.c
==============================================================================
--- head/sys/kern/subr_gtaskqueue.c     Mon Oct 29 14:06:20 2018        
(r339860)
+++ head/sys/kern/subr_gtaskqueue.c     Mon Oct 29 14:36:03 2018        
(r339861)
@@ -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, struc
                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 gta
        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 gtas
                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 gr
 {
        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 gr
        LIST_REMOVE(gtask, gt_list);
        mtx_unlock(&qgroup->tqg_lock);
        gtask->gt_taskqueue = NULL;
+       gtask->gt_task.ta_flags &= ~TASK_NOENQUEUE;
 }
 
 static void

Modified: head/sys/sys/gtaskqueue.h
==============================================================================
--- head/sys/sys/gtaskqueue.h   Mon Oct 29 14:06:20 2018        (r339860)
+++ head/sys/sys/gtaskqueue.h   Mon Oct 29 14:36:03 2018        (r339861)
@@ -52,7 +52,9 @@ int   gtaskqueue_cancel(struct gtaskqueue *queue, struct
 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 *
 
 #define TASK_ENQUEUED                  0x1
 #define TASK_SKIP_WAKEUP               0x2
+#define TASK_NOENQUEUE                 0x4
 
 
 #define GTASK_INIT(task, flags, priority, func, context) do {  \
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to