Skip to contentSkip to conversation

distinguish kernel thread / user task

DISCUSSION Sylvain 2 participants 5 messages Dec 3, 2004

lore
Share & tools
lore

Sylvain

author
#0 lore raw

Hi all,

I have little question while doing some kernel implementation. How can I distinguish whether a task_struct is actually kernel thread or mere user task?

My idea was to look at task_struct "mm" field to discriminate them, but that was wrong...

Thanks,

Sylvain

#1 lore raw
Quoted 6 lines · Sylvain

Hi all,

I have little question while doing some kernel implementation. How can I distinguish whether a task_struct is actually kernel thread or mere user task?

Quoted 6 lines

My idea was to look at task_struct "mm" field to discriminate them, but that was wrong...

Thanks,

Sylvain

To the scheduler, a thread is a thread. It doesn't care if it's a kernel thread or not. The difference is execution context, which is cpu-dependant. For example, on x86 the difference is in the code segment the task runs in. Kernel threads run in KERNEL_CS (ring 0), and user threads run USER_CS (or any other ring 3 code segment, or vm86 mode set in eflags). Other cpus might have a flag in the status register.

What are you trying to do that you need to know whether a thread is kernel or user? I suppose if there were a compelling enough reason, a kernel/user flag could be added to the task struct, set in do_fork() for kernel threads, and cleared by execve().

--
Brian Gerst

author
#2 lore raw

I am trying to do a tool to record task switching...separating also kernel/user tasks, but I got some trouble with that last case.

I confused since "ps" is actually able to distinguish kernel thread from user task.
I wouldn't had a flag if It 's not necessary

Sylvain

Quoted 31 lines · Brian Gerst
Quoted 6 lines · Sylvain

Hi all,

I have little question while doing some kernel implementation. How can I distinguish whether a task_struct is actually kernel thread or mere user task?

Quoted 6 lines

My idea was to look at task_struct "mm" field to discriminate them, but that was wrong...

Thanks,

Sylvain

To the scheduler, a thread is a thread. It doesn't care if it's a kernel thread or not. The difference is execution context, which is cpu-dependant. For example, on x86 the difference is in the code segment the task runs in. Kernel threads run in KERNEL_CS (ring 0), and user threads run USER_CS (or any other ring 3 code segment, or vm86 mode set in eflags). Other cpus might have a flag in the status register.

What are you trying to do that you need to know whether a thread is kernel or user? I suppose if there were a compelling enough reason, a kernel/user flag could be added to the task struct, set in do_fork() for kernel threads, and cleared by execve().

--
Brian Gerst

#3 lore raw
Quoted 9 lines · Sylvain

I am trying to do a tool to record task switching...separating also kernel/user tasks, but I got some trouble with that last case.

I confused since "ps" is actually able to distinguish kernel thread from user task.
I wouldn't had a flag if It 's not necessary

Sylvain

Pstools doesn't really know the difference between user and kernel threads. It only shows kernel threads as swapped out (in brackets) because they have an RSS of zero (since kernel threads have no mm struct).

--
Brian Gerst

author
#4 lore raw

kernel threads have no mm struct, right.
but it appears some programs (user tasks) haven't either ?! actually, that what I notice..

Sylvain

Quoted 18 lines · Brian Gerst
Quoted 9 lines · Sylvain

I am trying to do a tool to record task switching...separating also kernel/user tasks, but I got some trouble with that last case.

I confused since "ps" is actually able to distinguish kernel thread from user task.
I wouldn't had a flag if It 's not necessary

Sylvain

Pstools doesn't really know the difference between user and kernel threads. It only shows kernel threads as swapped out (in brackets) because they have an RSS of zero (since kernel threads have no mm struct).

--
Brian Gerst