linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Question about the usage of kernel_thread
@ 2005-11-05  8:40 Yan Zheng
  2005-11-05 11:31 ` Fawad Lateef
  0 siblings, 1 reply; 4+ messages in thread
From: Yan Zheng @ 2005-11-05  8:40 UTC (permalink / raw)
  To: linux-kernel

Hi.

In LKD2, Robert say:
Linux delegates several tasks to kernel threads, most notably the pdflush task and the ksoftirqd task. These threads are created on system boot by other kernel threads. Indeed, a kernel thread can be created only by another kernel thread. 


But I found that kernel_thread(...) are used wildly like:

#include <linux/kernel.h>
#include <linux/module.h>

static int noop(void *dummy)
{
        printk("current->mm = %p\n", current->mm);
        return 0;
}

static int test_init(void)
{
        kernel_thread(noop, NULL, CLONE_KERNEL | SIGCHLD);
        return 0;
}

static void test_exit(void) {}
module_init(test_init);
module_exit(test_exit); 


In this circumstances, The thread created by kernel_thread has "current->mm != NULL".

My question is:
The new thread is truely kernel thread ? The usage of kernel_thread(...) like this is correct?

Thanks advance.
Best Regards



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Question about the usage of kernel_thread
  2005-11-05  8:40 Question about the usage of kernel_thread Yan Zheng
@ 2005-11-05 11:31 ` Fawad Lateef
  2005-11-05 12:20   ` Yan Zheng
  0 siblings, 1 reply; 4+ messages in thread
From: Fawad Lateef @ 2005-11-05 11:31 UTC (permalink / raw)
  To: Yan Zheng; +Cc: linux-kernel

On 11/5/05, Yan Zheng <yanzheng@21cn.com> wrote:
> Hi.
>
> In LKD2, Robert say:
> Linux delegates several tasks to kernel threads, most notably the pdflush task and the ksoftirqd task. These threads are created on system boot by other kernel threads. Indeed, a kernel thread can be created only by another kernel thread.
>
>
> But I found that kernel_thread(...) are used wildly like:
>
> #include <linux/kernel.h>
> #include <linux/module.h>
>
> static int noop(void *dummy)
> {
>         printk("current->mm = %p\n", current->mm);
>         return 0;
> }
>
> static int test_init(void)
> {
>         kernel_thread(noop, NULL, CLONE_KERNEL | SIGCHLD);
>         return 0;
> }
>
> static void test_exit(void) {}
> module_init(test_init);
> module_exit(test_exit);
>
>
> In this circumstances, The thread created by kernel_thread has "current->mm != NULL".
>
> My question is:
> The new thread is truely kernel thread ? The usage of kernel_thread(...) like this is correct?
>

AFAIK the thread created like above is a true kernel thread but in
general practice what I saw and used that by creating thread from
init_module, the thread first call daemonize which actually drops the
mm related to thread and then through reparent_to_init it makes init
as a parent of the thread/process newly created. So after daemonize
call current->mm becomes NULL and when the scheduling is going to be
done the previous_process->mm will be used as the current->mm and
creating thread like above is correct.

--
Fawad Lateef

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Question about the usage of kernel_thread
  2005-11-05 11:31 ` Fawad Lateef
@ 2005-11-05 12:20   ` Yan Zheng
  2005-11-05 12:54     ` Fawad Lateef
  0 siblings, 1 reply; 4+ messages in thread
From: Yan Zheng @ 2005-11-05 12:20 UTC (permalink / raw)
  To: Fawad Lateef; +Cc: linux-kernel

>
> AFAIK the thread created like above is a true kernel thread but in
> general practice what I saw and used that by creating thread from
> init_module, the thread first call daemonize which actually drops the
> mm related to thread and then through reparent_to_init it makes init
> as a parent of the thread/process newly created. So after daemonize
> call current->mm becomes NULL and when the scheduling is going to be
> done the previous_process->mm will be used as the current->mm and
> creating thread like above is correct.
>
> --
> Fawad Lateef
> -

Thank you very much, Fawad.

I do additional test by follow codes, the result is strange.

========================================
#include <linux/kernel.h>
#include <linux/module.h>

static int noop(void *dummy)
{
	int i = 0;
	while(i++ < 10) {
		printk("current->mm = %p\n", current->mm);
		printk("current->active_mm = %p\n", current->active_mm);
		set_current_state(TASK_INTERRUPTIBLE);
		schedule_timeout(HZ);
	}
	return 0;
}

static void create_thread(void *dummy)
{
	kernel_thread(noop, NULL, CLONE_KERNEL | SIGCHLD);
}

static struct work_struct work;

static int test_init(void)
{
	INIT_WORK(&work, create_thread, NULL);
	schedule_work(&work);
	return 0;
}
/*
static int test_init(void)
{
	kernel_thread(noop, NULL, CLONE_KERNEL | SIGCHLD);
	return 0;
}
*/

static void test_exit(void) {}
module_init(test_init);
module_exit(test_exit);
========================================

If use kernel_thread like above. the output is:
current->mm = 00000000
current->active_mm = dffd2640
current->mm = 00000000
current->active_mm = df4d50e0
current->mm = 00000000
current->active_mm = df4463c0
current->mm = 00000000
current->active_mm = df4d50e0
current->mm = 00000000
current->active_mm = c16ee3e0
current->mm = 00000000
current->active_mm = df4463c0
current->mm = 00000000
current->active_mm = c16ee3e0
current->mm = 00000000
current->active_mm = c16ee3e0
current->mm = 00000000
current->active_mm = df796380
current->mm = 00000000
current->active_mm = c16ee3e0

if use kernel_thread directly in module_init(...). the output is:
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060
current->mm = df988060
current->active_mm = df988060

Would you please do some explanation.

Best Regards

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Question about the usage of kernel_thread
  2005-11-05 12:20   ` Yan Zheng
@ 2005-11-05 12:54     ` Fawad Lateef
  0 siblings, 0 replies; 4+ messages in thread
From: Fawad Lateef @ 2005-11-05 12:54 UTC (permalink / raw)
  To: Yan Zheng; +Cc: linux-kernel

On 11/5/05, Yan Zheng <yzcorp@gmail.com> wrote:
>
> I do additional test by follow codes, the result is strange.
>
> ========================================
> #include <linux/kernel.h>
> #include <linux/module.h>
>
> static int noop(void *dummy)
> {
>         int i = 0;
>         while(i++ < 10) {
>                 printk("current->mm = %p\n", current->mm);
>                 printk("current->active_mm = %p\n", current->active_mm);
>                 set_current_state(TASK_INTERRUPTIBLE);
>                 schedule_timeout(HZ);
>         }
>         return 0;
> }
>
> static void create_thread(void *dummy)
> {
>         kernel_thread(noop, NULL, CLONE_KERNEL | SIGCHLD);
> }
>
> static struct work_struct work;
>
> static int test_init(void)
> {
>         INIT_WORK(&work, create_thread, NULL);
>         schedule_work(&work);
>         return 0;
> }
> /*
> static int test_init(void)
> {
>         kernel_thread(noop, NULL, CLONE_KERNEL | SIGCHLD);
>         return 0;
> }
> */
>
> static void test_exit(void) {}
> module_init(test_init);
> module_exit(test_exit);
> ========================================
>
> If use kernel_thread like above. the output is:
> current->mm = 00000000
> current->active_mm = dffd2640
> current->mm = 00000000
> current->active_mm = df4d50e0
> current->mm = 00000000
> current->active_mm = df4463c0
> current->mm = 00000000
> current->active_mm = df4d50e0
> current->mm = 00000000
> current->active_mm = c16ee3e0
> current->mm = 00000000
> current->active_mm = df4463c0
> current->mm = 00000000
> current->active_mm = c16ee3e0
> current->mm = 00000000
> current->active_mm = c16ee3e0
> current->mm = 00000000
> current->active_mm = df796380
> current->mm = 00000000
> current->active_mm = c16ee3e0
>
> if use kernel_thread directly in module_init(...). the output is:
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
> current->mm = df988060
> current->active_mm = df988060
>
> Would you please do some explanation.
>

The thread created from the code above (means from workqueue) are
by-default have init task as a parent process as init_workqueues
function is called during the booting process init
(http://sosdg.org/~coywolf/lxr/source/init/main.c#L657) from the
function do_basic_setup
(http://sosdg.org/~coywolf/lxr/source/init/main.c#L691) so the
workqueues have current->mm = NULL and when you creates a thread from
the workqueue it also get current->mm = NULL as of parent (workqueue
interface) and current->active_mm contains the mm of the previously
running process (running/scheduled before the current process which is
scheduled)

Whereas, when you create a kernel_thread from init_module it gets the
current->mm of the parent process (insmod is process in init_module
case) and during schedule if current->mm != NULL then the
current->active_mm remains same as that of current->mm, so for
creating a pure kernel thread from init_module daemonize must be
called from thread (I think I was wrong in my previous reply as i
wronggly said "the thread created like above is a true kernel thread")
else without calling daemonize (as I saw from your test) I guess you
can't get the full features of the kernel_thread (like not able to
access __complete__ kernel address space) (CMIIW)



--
Fawad Lateef

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-11-05 12:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-05  8:40 Question about the usage of kernel_thread Yan Zheng
2005-11-05 11:31 ` Fawad Lateef
2005-11-05 12:20   ` Yan Zheng
2005-11-05 12:54     ` Fawad Lateef

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).