From: ebiederm@xmission.com (Eric W. Biederman)
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Kernel development list <linux-kernel@vger.kernel.org>
Subject: Re: Kthread question
Date: Thu, 26 Apr 2007 13:11:15 -0600 [thread overview]
Message-ID: <m1r6q7j6i4.fsf@ebiederm.dsl.xmission.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.0704261114080.3552-100000@iolanthe.rowland.org> (Alan Stern's message of "Thu, 26 Apr 2007 11:29:52 -0400 (EDT)")
Alan Stern <stern@rowland.harvard.edu> writes:
> Here's a question about how to use a kthread.
>
> The usb-storage driver creates a new thread to do LUN scanning whenever a
> newly-hotplugged device is detected. This scanning involves waiting for
> several seconds and then carrying out a potentially long-lasting series of
> I/O operations. So we don't want to use schedule_work(); instead we
> create a new thread to do it.
>
> Once the scanning thread's work is done, there's no reason for it to hang
> around. The driver's probe() method has long since exited, leaving nobody
> to reap the thread. Of course it could always just exit normally, without
> checking kthread_should_stop(), but...
>
> If the device is unplugged during that initial several-second-long delay
> period, we need to stop the scanning thread immediately. The obvious
> answer is to use kthread_stop(), but that's not consistent with the
> thread's normal behavior of exiting without waiting for
> kthread_should_stop().
>
> Another problem arises as well. If the driver's remove() method doesn't
> call kthread_stop() then it doesn't have any straightforward way to wait
> for the thread to exit. This leads to the possibility that the driver's
> module could be unloaded while the scanning thread is still running.
>
> Would the best approach be to set up a special-purpose struct completion?
> Then the thread could call complete_and_exit() and the remove() method
> could wait for it safely.
Ok. Because of the module unloading issue, and because we don't have
a lot of these threads running around, the current plan is to fix
thread_create and kthread_stop so that they must always be paired,
and so that kthread_stop will work correctly if the task has already
exited.
Basically that just involves calling get_task_struct in kthread_create
and put_task_struct in kthread_stop.
I have some patches, hopefully I will start getting them out in the next day or
two. Sorry I should have acted on this sooner, but I got a little distracted.
Eric
prev parent reply other threads:[~2007-04-26 19:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-04-26 15:29 Alan Stern
2007-04-26 19:11 ` Eric W. Biederman [this message]
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=m1r6q7j6i4.fsf@ebiederm.dsl.xmission.com \
--to=ebiederm@xmission.com \
--cc=linux-kernel@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
/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
all inboxes | Powered by JetHome®