mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: Dmitry Antipov <dmitry.antipov@linaro.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Module/kthread/printk question/problem
Date: Thu, 02 Feb 2012 10:15:58 +0100	[thread overview]
Message-ID: <1328174158.2279.3.camel@edumazet-HP-Compaq-6005-Pro-SFF-PC> (raw)
In-Reply-To: <4F2A296E.5080007@linaro.org>

Le jeudi 02 février 2012 à 10:13 +0400, Dmitry Antipov a écrit :
> On 02/01/2012 09:16 PM, Eric Dumazet wrote:
> 
> >> I realize this, but there was a second part of the question: what's the
> >> better way to ensure that all test/X threads are really gone at some point of
> >> testmod_exit()?
> >>
> >
> > You could use kthread_stop()
> >
> > This way you can control all your kernel threads really exited before
> > module cleanup.
> 
> Hm... if I try something like:
> 
> static void __exit testmod_exit(void)
> {
> 	int i;
> 
> 	wait_for_completion(&done);
> 	for (i = 0; i < nrthreads; i++)
> 		kthread_stop(threads[i]);
> 	kfree(threads);
> }

Try following code :



#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/slab.h>

MODULE_LICENSE("GPL");

static int nrthreads = 128;
module_param(nrthreads, int, 0644);

static int loopcount = 1024;
module_param(loopcount, int, 0644);

static int usehrtime = 0;
module_param(usehrtime, int, 0644);

static int slack = 50000;
module_param(slack, int, 0644);

static int msecs = 1;
module_param(msecs, int, 0644);

static struct task_struct **threads;

static int test(void *unused)
{
	 int i;
	 ktime_t expires = ktime_set(0, msecs * NSEC_PER_MSEC);

	 for (i = 0; !kthread_should_stop() && i < loopcount; i++) {
		 if (usehrtime) {
			 set_current_state(TASK_UNINTERRUPTIBLE);
			 schedule_hrtimeout_range(&expires, slack, HRTIMER_MODE_REL);
		 }
		 else
			 schedule_timeout_uninterruptible(msecs_to_jiffies(msecs));
	 }

	 return 0;
}

static int __init testmod_init(void)
{
	 int i;

	 printk("test begin\n");

	 threads = kmalloc(nrthreads * sizeof(struct task_struct *), GFP_KERNEL);
	 if (!threads)
		 return -ENOMEM;

	 for (i = 0; i < nrthreads; i++) {
		 threads[i] = kthread_create(test, NULL, "test/%d", i);
		 if (IS_ERR(threads[i])) {
			 int j, err = PTR_ERR(threads[i]);

			 for (j = 0; j < i; j++)
				 kthread_stop(threads[j]);
			 kfree(threads);
			 return err;
		 }
	 }
	 for (i = 0; i < nrthreads; i++) {
		get_task_struct(threads[i]);
		wake_up_process(threads[i]);
	 }
	 return 0;
}

static void __exit testmod_exit(void)
{
	int i;

	for (i = 0; i < nrthreads; i++) {
		kthread_stop(threads[i]);
		put_task_struct(threads[i]);
	}
	kfree(threads);
}

module_init(testmod_init);
module_exit(testmod_exit);



  parent reply	other threads:[~2012-02-02  9:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-01 16:09 Dmitry Antipov
2012-02-01 16:31 ` Eric Dumazet
2012-02-01 16:35   ` Dmitry Antipov
2012-02-01 17:16     ` Eric Dumazet
2012-02-02  6:13       ` Dmitry Antipov
2012-02-02  7:45         ` Eric Dumazet
2012-02-02  9:20           ` Dmitry Antipov
2012-02-02  9:15         ` Eric Dumazet [this message]
2012-02-02  9:22           ` Dmitry Antipov
2012-02-02  9:58             ` Eric Dumazet
2012-02-06 23:43               ` Rusty Russell

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=1328174158.2279.3.camel@edumazet-HP-Compaq-6005-Pro-SFF-PC \
    --to=eric.dumazet@gmail.com \
    --cc=dmitry.antipov@linaro.org \
    --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®