From owner-freebsd-current@freebsd.org Tue Aug 4 15:32:30 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 CA4669B3C44 for ; Tue, 4 Aug 2015 15:32:30 +0000 (UTC) (envelope-from rizzo.unipi@gmail.com) Received: from mail-la0-x231.google.com (mail-la0-x231.google.com [IPv6:2a00:1450:4010:c03::231]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 52784B73 for ; Tue, 4 Aug 2015 15:32:30 +0000 (UTC) (envelope-from rizzo.unipi@gmail.com) Received: by lady2 with SMTP id y2so6007480lad.0 for ; Tue, 04 Aug 2015 08:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=KkM5bFd0BGe7Lxua42Bj2Zf5orNiqT9hbt5wiPlaxQ8=; b=hIe7pMOfJ3jqtJNiNuFSPNqbqPSTJGYiaw3XfZr2R49wH7W3lHtVFGfAE1y1BM5ZM9 LLEcW7oRVfc0N4NxJGYLjtCcTiYUrO56lOsx3oMuUhkUz+Ke95GBb5MVxWIIpQOw0P3a DFp9h9Q000MT3z0u8Nf+wDgeE3igJrv3DDbrEP/Z0iRnBC0vpgkQuJsEFqNtaDjUpolD pCOuu5k8Kd8t9ACVtIRCahPfgpDtdNVuRHERc4P4o4k25Vg/NlnB56W/4aMIiNWRwnQ1 UowWIP5NXl681ZJbiB6o8Iwli8QgXouAwojaGjEUICg38WZyhSPmSYBfDTQoux119DZC x4mA== MIME-Version: 1.0 X-Received: by 10.112.210.6 with SMTP id mq6mr4169007lbc.83.1438702348245; Tue, 04 Aug 2015 08:32:28 -0700 (PDT) Sender: rizzo.unipi@gmail.com Received: by 10.114.80.133 with HTTP; Tue, 4 Aug 2015 08:32:28 -0700 (PDT) In-Reply-To: <20150804145311.GN2072@kib.kiev.ua> References: <20150804145311.GN2072@kib.kiev.ua> Date: Tue, 4 Aug 2015 17:32:28 +0200 X-Google-Sender-Auth: 2Ic_AFjbT8rtXYazoSwJZt_LCo8 Message-ID: Subject: Re: proper way to terminate a kthread when the parent process dies ? From: Luigi Rizzo To: Konstantin Belousov Cc: freebsd-current , Stefano Garzarella , Giuseppe Lettieri , Luigi Rizzo Content-Type: text/plain; charset=UTF-8 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 15:32:31 -0000 On Tue, Aug 4, 2015 at 4:53 PM, Konstantin Belousov 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 >> >> kthread_suspend_check(); // void >> work_or_short_tsleep(); // potentially se >> } >> kthread_exit(); >> >> We have seen different ways for the >> >> 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) -----------------------------------------+-------------------------------