Re: Question about the usage of kernel_thread

From: Yan Zheng
Date: Sat Nov 05 2005 - 07:21:15 EST


>
> 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
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/