From owner-freebsd-current@FreeBSD.ORG Thu Apr 29 12:46:24 2010 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9E0B9106564A for ; Thu, 29 Apr 2010 12:46:24 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from cyrus.watson.org (cyrus.watson.org [65.122.17.42]) by mx1.freebsd.org (Postfix) with ESMTP id 5CE9A8FC1B for ; Thu, 29 Apr 2010 12:46:24 +0000 (UTC) Received: from bigwig.baldwin.cx (66.111.2.69.static.nyinternet.net [66.111.2.69]) by cyrus.watson.org (Postfix) with ESMTPSA id ECF3D46B09; Thu, 29 Apr 2010 08:46:23 -0400 (EDT) Received: from jhbbsd.localnet (smtp.hudson-trading.com [209.249.190.9]) by bigwig.baldwin.cx (Postfix) with ESMTPA id 2FFB08A026; Thu, 29 Apr 2010 08:46:18 -0400 (EDT) From: John Baldwin To: freebsd-current@freebsd.org Date: Thu, 29 Apr 2010 07:44:35 -0400 User-Agent: KMail/1.12.1 (FreeBSD/7.3-CBSD-20100217; KDE/4.3.1; amd64; ; ) References: <06D5F9F6F655AD4C92E28B662F7F853E039E389A@seaxch09.desktop.isilon.com> In-Reply-To: <06D5F9F6F655AD4C92E28B662F7F853E039E389A@seaxch09.desktop.isilon.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201004290744.35090.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.0.1 (bigwig.baldwin.cx); Thu, 29 Apr 2010 08:46:18 -0400 (EDT) X-Virus-Scanned: clamav-milter 0.95.1 at bigwig.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=4.2 tests=AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on bigwig.baldwin.cx Cc: Matthew Fleming Subject: Re: sleep bug in taskqueue(9) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 29 Apr 2010 12:46:24 -0000 On Wednesday 28 April 2010 7:59:58 pm Matthew Fleming wrote: > It looks to me like taskqueue_drain(taskqueue_thread, foo) will not > correctly detect whether or not a task is currently running. The check > is against a field in the taskqueue struct, but for the taskqueue_thread > queue with more than one thread, multiple threads can simultaneously be > running a task, thus stomping over the tq_running field. That sounds right. > I have not seen any problem with the code as-is in actual use, so this > is purely an inspection bug. > > The following patch should fix the problem. Because it changes the size > of struct task I'm not sure if it would be suitable for MFC. For HEAD I would maybe pad the flags field out to an int? The compiler is going to make it an int anyway. For the purposes of MFC'ing there are a couple of options. The simplest might be to reuse the high bit of the 'pending' count as a running flag. Breaking the ABI of struct task would prohibit an MFC otherwise. > Thanks, > matthew > > diff --git a/sys/kern/subr_taskqueue.c b/sys/kern/subr_taskqueue.c > index 8405b3d..3b18269 100644 > --- a/sys/kern/subr_taskqueue.c > +++ b/sys/kern/subr_taskqueue.c > @@ -51,7 +51,6 @@ __FBSDID("$FreeBSD$"); > const char *tq_name; > taskqueue_enqueue_fn tq_enqueue; > void *tq_context; > - struct task *tq_running; > struct mtx tq_mutex; > struct thread **tq_threads; > int tq_tcount; > @@ -233,13 +232,13 @@ taskqueue_run(struct taskqueue *queue) > STAILQ_REMOVE_HEAD(&queue->tq_queue, ta_link); > pending = task->ta_pending; > task->ta_pending = 0; > - queue->tq_running = task; > + task->ta_flags |= TA_FLAGS_RUNNING; > TQ_UNLOCK(queue); > > task->ta_func(task->ta_context, pending); > > TQ_LOCK(queue); > - queue->tq_running = NULL; > + task->ta_flags &= ~TA_FLAGS_RUNNING; > wakeup(task); > } > > > @@ -256,14 +255,16 @@ taskqueue_drain(struct taskqueue *queue, struct > task *task) > { > if (queue->tq_spin) { /* XXX */ > mtx_lock_spin(&queue->tq_mutex); > - while (task->ta_pending != 0 || task == > queue->tq_running) > + while (task->ta_pending != 0 || > + (task->ta_flags & TA_FLAGS_RUNNING) != 0) > msleep_spin(task, &queue->tq_mutex, "-", 0); > mtx_unlock_spin(&queue->tq_mutex); > } else { > WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, > __func__); > > mtx_lock(&queue->tq_mutex); > - while (task->ta_pending != 0 || task == > queue->tq_running) > + while (task->ta_pending != 0 || > + (task->ta_flags & TA_FLAGS_RUNNING) != 0) > msleep(task, &queue->tq_mutex, PWAIT, "-", 0); > mtx_unlock(&queue->tq_mutex); > } > diff --git a/sys/sys/_task.h b/sys/sys/_task.h > index 2a51e1b..c57bdd5 100644 > --- a/sys/sys/_task.h > +++ b/sys/sys/_task.h > @@ -36,15 +36,21 @@ > * taskqueue_run(). The first argument is taken from the 'ta_context' > * field of struct task and the second argument is a count of how many > * times the task was enqueued before the call to taskqueue_run(). > + * > + * List of locks > + * (c) const after init > + * (q) taskqueue lock > */ > typedef void task_fn_t(void *context, int pending); > > struct task { > - STAILQ_ENTRY(task) ta_link; /* link for queue */ > - u_short ta_pending; /* count times queued */ > - u_short ta_priority; /* Priority */ > - task_fn_t *ta_func; /* task handler */ > - void *ta_context; /* argument for handler */ > + STAILQ_ENTRY(task) ta_link; /* (q) link for queue */ > + u_char ta_flags; /* (q) state of this task */ > +#define TA_FLAGS_RUNNING 0x01 > + u_short ta_pending; /* (q) count times queued */ > + u_short ta_priority; /* (c) Priority */ > + task_fn_t *ta_func; /* (c) task handler */ > + void *ta_context; /* (c) argument for handler */ > _______________________________________________ > freebsd-current@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-current > To unsubscribe, send any mail to "freebsd-current-unsubscribe@freebsd.org" > -- John Baldwin