Re: proper way to terminate a kthread when the parent process dies ?

From: Konstantin Belousov <kostikbel_at_gmail.com>
Date: Tue, 4 Aug 2015 19:02:11 +0300
On Tue, Aug 04, 2015 at 05:32:28PM +0200, Luigi Rizzo wrote:
> On Tue, Aug 4, 2015 at 4:53 PM, Konstantin Belousov <kostikbel_at_gmail.com> wrote:
> > If this is a thread of the normal user process, then it is not a kernel
> > thread, even if it never leaves the kernel mode.
> 
> thanks for the answer.
> 
> i do not really know what is the difference between a "kernel thread"
> and a "thread".
> Could you clarify what is the distinctive feature between the two ?
> Perhaps being owned by pid 0 ?
No.  I probably having troubles answering this exact question.  TDP_KTHREAD
is used as a shortcut in several places, mostly to avoid some specific
actions that are useless for the real kernel process.  E.g. the amd64
context switch code avoids setting some CPU registers when switching to
the kernel thread.  Hacks to not switch address spaces when switching 
to the kthread were already removed.

Overall, claiming that a thread of the normal user process (i.e. process
which executes in userspace) is kernel thread is fragile and, if not
breaking in your tests, could break later.
> 
> This specific thread is created within a system call by invoking kthread_add()
> and associated with the user process.
Did you tried the other approach, by creating the thread from userspace,
and then executing the syscall which would loop ?  This is how things are
done by e.g. nfsd.

Still, if you looping in the kernel, you must call thread_suspend_check()
periodically.
> 
> >
> > You must call thread_suspend_check() in any in-kernel loop to allow the
> > stops and process exit to work.
> 
> so does it mean that the kthread_suspend_check() is incorrect and we should
> use thread_suspend_check() instead ?
Most likely yes.
Received on Tue Aug 04 2015 - 14:02:21 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:59 UTC