From owner-freebsd-current@freebsd.org Tue Aug 4 16:02:21 2015 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B27D89B255C for ; Tue, 4 Aug 2015 16:02:21 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 297AFEE4 for ; Tue, 4 Aug 2015 16:02:20 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id t74G2CxT041641 (version=TLSv1 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Tue, 4 Aug 2015 19:02:12 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.9.2 kib.kiev.ua t74G2CxT041641 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id t74G2BBg041640; Tue, 4 Aug 2015 19:02:11 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 4 Aug 2015 19:02:11 +0300 From: Konstantin Belousov To: Luigi Rizzo Cc: freebsd-current , Stefano Garzarella , Giuseppe Lettieri Subject: Re: proper way to terminate a kthread when the parent process dies ? Message-ID: <20150804160211.GO2072@kib.kiev.ua> References: <20150804145311.GN2072@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 04 Aug 2015 16:02:21 -0000 On Tue, Aug 04, 2015 at 05:32:28PM +0200, Luigi Rizzo wrote: > On Tue, Aug 4, 2015 at 4:53 PM, Konstantin Belousov 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.