Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Aug 2015 17:32:28 +0200
From:      Luigi Rizzo <rizzo@iet.unipi.it>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        freebsd-current <freebsd-current@freebsd.org>,  Stefano Garzarella <stefanogarzarella@gmail.com>, Giuseppe Lettieri <g.lettieri@iet.unipi.it>,  Luigi Rizzo <rizzo@iet.unipi.it>
Subject:   Re: proper way to terminate a kthread when the parent process dies ?
Message-ID:  <CA%2BhQ2%2BiUpK9Z_bV9TYpWQEd0mi=PKtcwNThqg_WYVrka1reBHg@mail.gmail.com>
In-Reply-To: <20150804145311.GN2072@kib.kiev.ua>
References:  <CA%2BhQ2%2Bg8zSSHrLFiuD3-oZ1D0F9BsnJKVwc0hSDowr4gaX6eYw@mail.gmail.com> <20150804145311.GN2072@kib.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Aug 4, 2015 at 4:53 PM, Konstantin Belousov <kostikbel@gmail.com> wrote:
> On Tue, Aug 04, 2015 at 04:38:14PM +0200, Luigi Rizzo wrote:
>> Hi,
>> we have a doubt on the proper way to terminate a kernel thread that
>> has been associated to a user process U within a system call with
>>
>>         kthread_add( .. , .., p, ... )
>>
>> (p is the struct proc * of the calling process, U)
>>
>> When U terminates and goes into kern_exit.c :: exit1()
>> the kernel thread sees the following conditions:
>>
>>         P_SHOULDSTOP(td->td_proc) is TRUE
>>
>> td->td_flags has TDF_ASTPENDING | TDF_NEEDSUSPCHK set
>>
>> We are not sure what is the proper way to terminate
>> our kernel thread, whose body is the following:
>>
>>         while (must_run) { // someone will set must_run = 0
>>                 <check_for_forced_termination>
>>                 kthread_suspend_check(); // void
>>                 work_or_short_tsleep(); // potentially se
>>         }
>>         kthread_exit();
>>
>> We have seen different ways for the <check_for_forced_termination>
>>
>> 1.      if (P_SHOULDSTOP(td->td_proc)
>>                 break; // kthread_exit() is called outside the loop
>>
>> 2.      if (P_SHOULDSTOP(td->td_proc)
>>                 thread_suspend_check(0); // which then terminates the thread
>>         // this is done in sys/rpc/svc.c
>>
>> We are a bit unsure whether calling the thread_*() function in a kthread
>> is correct -- but there is an example in the kernel.
>>
>> Variants involve locking td->td_proc (but is it necessary ? The process
>> won't go away until all child threads die), or checking the td_tdflags
>> instead of the parent process' flags.
>>
>> So what is the correct way ?
>
> 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 ?

This specific thread is created within a system call by invoking kthread_add()
and associated with the user process.

>
> 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 ?

cheers
luigi


-- 
-----------------------------------------+-------------------------------
 Prof. Luigi RIZZO, rizzo@iet.unipi.it  . Dip. di Ing. dell'Informazione
 http://www.iet.unipi.it/~luigi/        . Universita` di Pisa
 TEL      +39-050-2217533               . via Diotisalvi 2
 Mobile   +39-338-6809875               . 56122 PISA (Italy)
-----------------------------------------+-------------------------------



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CA%2BhQ2%2BiUpK9Z_bV9TYpWQEd0mi=PKtcwNThqg_WYVrka1reBHg>