From owner-freebsd-current@freebsd.org Sun Feb 19 08:44:08 2017 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 D9156CE5F02 for ; Sun, 19 Feb 2017 08:44:08 +0000 (UTC) (envelope-from hps@selasky.org) Received: from mail.turbocat.net (turbocat.net [IPv6:2a01:4f8:c17:6c4b::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 95CADBF2 for ; Sun, 19 Feb 2017 08:44:08 +0000 (UTC) (envelope-from hps@selasky.org) Received: from hps2016.home.selasky.org (unknown [62.141.129.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.turbocat.net (Postfix) with ESMTPSA id EA0EF1FE086; Sun, 19 Feb 2017 09:44:03 +0100 (CET) Subject: Re: First thread in proc in not passed to thread_dtor eventhandler upon exit To: Konstantin Belousov References: <933e132d-289b-330d-b349-584a25e279d4@selasky.org> <20170219035422.GQ2092@kib.kiev.ua> Cc: FreeBSD Current From: Hans Petter Selasky Message-ID: <060681c7-4c46-dcc7-d797-868fd0ad35a6@selasky.org> Date: Sun, 19 Feb 2017 09:43:24 +0100 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <20170219035422.GQ2092@kib.kiev.ua> Content-Type: multipart/mixed; boundary="------------B8A904A95BD087F287C9271A" X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.23 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: Sun, 19 Feb 2017 08:44:09 -0000 This is a multi-part message in MIME format. --------------B8A904A95BD087F287C9271A Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 02/19/17 04:54, Konstantin Belousov wrote: > On Sat, Feb 18, 2017 at 10:40:00PM +0100, Hans Petter Selasky wrote: >> Hi, >> >> Is the following a bug or feature. I observe that the first thread in a >> procedure is not passed to thread_dtor as declared by the following >> eventhandler, when the procedure exits. >> >> EVENTHANDLER_DECLARE(thread_dtor, thread_dtor_fn); >> >> Is this a bug or feature? > This is a feature. When a zombie process is reaped, the last thread in > the process (the one which exited it) is left in the process thread list. > This is an optimization, because process without at least one thread > is never useful. > > You can see the code in fork1() which allocates struct proc from zone > and then checks if there any thread pre-allocated as well (both struct > proc and struct thread are type-stable). > > Since the last thread is not freed, its destructor is not signalled. Hi Konstantin, I see. To avoid leakage of LinuxKPI, Linux and Dtrace resources to all system threads, I think the attached patch is required. What do you think about the attached patch? If you don't like it can you suggest another approach? --HPS --------------B8A904A95BD087F287C9271A Content-Type: text/x-patch; name="thread_ctor_dtor.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="thread_ctor_dtor.patch" diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index f39326f..a206a0a 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -191,11 +191,17 @@ static int proc_ctor(void *mem, int size, void *arg, int flags) { struct proc *p; + struct thread *td; p = (struct proc *)mem; SDT_PROBE4(proc, , ctor , entry, p, size, arg, flags); EVENTHANDLER_INVOKE(process_ctor, p); SDT_PROBE4(proc, , ctor , return, p, size, arg, flags); + td = FIRST_THREAD_IN_PROC(p); + if (td != NULL) { + /* Make sure all thread constructors are executed */ + EVENTHANDLER_INVOKE(thread_ctor, td); + } return (0); } @@ -220,6 +226,9 @@ proc_dtor(void *mem, int size, void *arg) #endif /* Free all OSD associated to this thread. */ osd_thread_exit(td); + + /* Make sure all thread destructors are executed */ + EVENTHANDLER_INVOKE(thread_dtor, td); } EVENTHANDLER_INVOKE(process_dtor, p); if (p->p_ksi != NULL) --------------B8A904A95BD087F287C9271A--