From: Frederic Weisbecker <fweisbec@gmail.com>
To: Oleg Nesterov <oleg@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>,
linux-kernel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Lai Jiangshan <laijs@cn.fujitsu.com>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [RFC][PATCH] create workqueue threads only when needed
Date: Tue, 27 Jan 2009 09:40:49 +0100 [thread overview]
Message-ID: <20090127084048.GA5498@nowhere> (raw)
In-Reply-To: <20090127014651.GA13861@redhat.com>
On Tue, Jan 27, 2009 at 02:46:51AM +0100, Oleg Nesterov wrote:
> Frederic Weisbecker <fweisbec@gmail.com> wrote:
> >
> > static void insert_work(struct cpu_workqueue_struct *cwq,
> > struct work_struct *work, struct list_head *head)
> > {
> > - trace_workqueue_insertion(cwq->thread, work);
> > + trace_workqueue_insertion(cwq->thread, work, cwq->wq->singlethread);
> >
> > set_wq_data(work, cwq);
> > /*
> > @@ -148,6 +176,9 @@ static void __queue_work(struct cpu_workqueue_struct *cwq,
> > {
> > unsigned long flags;
> >
> > + if (!cwq->thread)
> > + create_wq_thread_late(cwq);
> > +
>
> [...snip...]
>
> > +static void create_wq_thread_late_work(struct work_struct *work)
> > +{
> > + struct late_workqueue_creation_data *l;
> > + struct cpu_workqueue_struct *cwq;
> > + int cpu = smp_processor_id();
> > + int err = 0;
> > +
> > + l = container_of(work, struct late_workqueue_creation_data, work);
> > + cwq = l->cwq;
> > +
> > + if (is_wq_single_threaded(cwq->wq)) {
> > + err = create_workqueue_thread(cwq, singlethread_cpu);
> > + start_workqueue_thread(cwq, -1);
> > + } else {
> > + err = create_workqueue_thread(cwq, cpu);
> > + start_workqueue_thread(cwq, cpu);
> > + }
> > + WARN_ON_ONCE(err);
> > + kfree(l);
> > +}
>
> Let's suppose the workqueue was just created, and cwq->thared == NULL
> on (say) CPU 0.
>
> Then CPU 0 does
>
> queue_work(wq, work1);
> queue_work(wq, work2);
>
> Both these calls will notice cwq->thread == NULL, both will schedule
> the work wilth ->func = create_wq_thread_late_work.
>
> The first work correctly creates cwq->thread, the second one creates
> the new thread too and replaces cwq->thread? Now we have two threads
> which run in parallel doing the same work, but the first thread is
> "stealth", no?
You're right. I will put a mutex + a recheck of the cwq->thread inside
create_wq_thread_late_work to be sure there is no race during creation.
> > @@ -904,9 +967,12 @@ static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq)
> > * checks list_empty(), and a "normal" queue_work() can't use
> > * a dead CPU.
> > */
> > - trace_workqueue_destruction(cwq->thread);
> > - kthread_stop(cwq->thread);
> > - cwq->thread = NULL;
> > +
> > + if (cwq->thread) {
> > + trace_workqueue_destruction(cwq->thread, cwq->wq->singlethread);
> > + kthread_stop(cwq->thread);
> > + cwq->thread = NULL;
> > + }
>
> cleanup_workqueue_thread() has already checked cwq->thread != NULL,
> how can it become NULL ?
Right.
> And let's suppose a user does:
>
> wq = create_workqueue(...., when_needed => 1);
> queue_work(wq, some_work);
> destroy_workqueue(wq);
>
> This can return before create_wq_thread_late() populates the necessary
> cwq->thread. We can destroy/free workqueue with the pending work_structs,
> no?
>
> Oleg.
>
Totally right. I 'll fix these bugs.
Thanks!
next prev parent reply other threads:[~2009-01-27 8:41 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-01-27 0:17 [RFC][PATCH] create workqueue threads only when needed Frederic Weisbecker
2009-01-27 0:30 ` Arjan van de Ven
2009-01-31 18:03 ` Frederic Weisbecker
2009-01-31 18:15 ` Arjan van de Ven
2009-01-31 18:28 ` Frederic Weisbecker
2009-02-01 16:22 ` Stefan Richter
2009-02-01 17:04 ` Arjan van de Ven
2009-02-01 17:40 ` Stefan Richter
2009-02-01 17:47 ` Arjan van de Ven
2009-02-01 18:06 ` Stefan Richter
2009-02-01 18:11 ` Arjan van de Ven
2009-02-01 21:37 ` Benjamin Herrenschmidt
2009-02-02 2:24 ` Frederic Weisbecker
2009-02-02 6:00 ` Benjamin Herrenschmidt
2009-02-02 8:42 ` Stefan Richter
2009-02-02 9:05 ` Benjamin Herrenschmidt
2009-02-02 9:14 ` Oliver Neukum
2009-02-02 9:46 ` Benjamin Herrenschmidt
2009-02-02 9:58 ` Peter Zijlstra
2009-02-02 10:03 ` Oliver Neukum
2009-02-02 20:44 ` Benjamin Herrenschmidt
2009-02-04 9:54 ` Oliver Neukum
2009-02-02 11:39 ` Stefan Richter
2009-02-02 11:32 ` Frederic Weisbecker
2009-02-02 11:26 ` Frederic Weisbecker
2009-02-02 5:19 ` Arjan van de Ven
2009-02-02 6:01 ` Benjamin Herrenschmidt
2009-02-02 9:01 ` Stefan Richter
2009-02-02 14:45 ` Arjan van de Ven
[not found] ` <20090126162807.1131c777.akpm@linux-foundation.org>
2009-01-27 1:46 ` Oleg Nesterov
2009-01-27 8:40 ` Frederic Weisbecker [this message]
2009-01-27 3:07 ` Alasdair G Kergon
2009-01-27 8:57 ` Frederic Weisbecker
2009-01-27 12:43 ` Ingo Molnar
2009-02-02 14:49 ` Daniel Walker
2009-02-02 14:51 ` Frédéric Weisbecker
2009-02-02 15:40 ` Daniel Walker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090127084048.GA5498@nowhere \
--to=fweisbec@gmail.com \
--cc=a.p.zijlstra@chello.nl \
--cc=akpm@linux-foundation.org \
--cc=laijs@cn.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@elte.hu \
--cc=oleg@redhat.com \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox