mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Aritz Bastida <aritzbastida@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: Stopping Kernel Threads at module unload time
Date: Wed, 9 Nov 2005 16:49:49 +0100	[thread overview]
Message-ID: <7d40d7190511090749j3de0e473x@mail.gmail.com> (raw)

Hello

I've got some questions about kernel threads.I am writing a module
which spawns some kernel threads, which would be removed when the
module unloads. For that purpose i call kthread_stop() at module
unload time. When issuing rmmod on the module, it deadlocks at that
point (in the call to kthread_stop), and never returns.

In the thread main function the code was something like this (it's of
course simplified).

thread_main()
{
     while( ! kthread_should_stop())
     {
           .............
           wait_event_interruptible(stop_wq, kthread_should_stop() );
     }

     return 0;
}

So if kthread_stop() first sets the thread "closing flag", and then
calls wake_up_process(), the thread should wake up, see he should
stop, and
end the loop. That doesnt actually never happen.

I have also tried what it is done in kernel/sched.c to finish:

	/* wait for kthread_stop */
	set_current_state(TASK_INTERRUPTIBLE);
	while (!kthread_should_stop()) {	
		schedule();
		set_current_state(TASK_INTERRUPTIBLE);
	}
	__set_current_state(TASK_RUNNING);
                return 0;

I have ensured it actually arrives to that point by using printks, but
when the thread goes to sleep, it does never wake up again, so the
call to kthread_stop() lasts forever.

I dont know why this happens. Is the module cleanup code in the
context of a user process just like system calls? Can that code sleep?
If it can't sleep then the answer would be quite easy: kthread_stop()
wakes up the processes and then waits for the threads to finish (on
call wait_for_completion), but doesnt actually let them execute,
because it cannot sleep, so it deadlocks.

So I would be grateful if anyone can help me in this matter.
Regards

Aritz

             reply	other threads:[~2005-11-09 15:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-09 15:49 Aritz Bastida [this message]
2005-11-09 16:16 ` linux-os (Dick Johnson)
2005-11-09 16:56   ` Aritz Bastida
2005-11-09 17:05     ` linux-os (Dick Johnson)
2005-11-09 17:24       ` Aritz Bastida
2005-11-09 18:09         ` linux-os (Dick Johnson)
2005-11-09 17:27     ` Arnd Bergmann
2005-11-09 17:49       ` Aritz Bastida
2005-11-09 17:54         ` Aritz Bastida
2005-11-09 18:10         ` linux-os (Dick Johnson)

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=7d40d7190511090749j3de0e473x@mail.gmail.com \
    --to=aritzbastida@gmail.com \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®