From owner-freebsd-threads@freebsd.org Mon Sep 23 12:48:27 2019 Return-Path: Delivered-To: freebsd-threads@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id EBB9DF3C42 for ; Mon, 23 Sep 2019 12:48:27 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 46cPJ84Z8xz3yqY; Mon, 23 Sep 2019 12:48:24 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id x8NCmApL006285 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Mon, 23 Sep 2019 15:48:13 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua x8NCmApL006285 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id x8NCmA3N006284; Mon, 23 Sep 2019 15:48:10 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Mon, 23 Sep 2019 15:48:10 +0300 From: Konstantin Belousov To: Andriy Gapon Cc: freebsd-threads@FreeBSD.org Subject: Re: assertion when destroying a process shared mutex Message-ID: <20190923124810.GP2559@kib.kiev.ua> References: <6f6a16a3-8eca-ceb0-4ca3-aadf2d926f81@FreeBSD.org> <20190920173854.GJ2559@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.12.1 (2019-06-15) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-Rspamd-Queue-Id: 46cPJ84Z8xz3yqY X-Spamd-Bar: ----- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-5.98 / 15.00]; NEURAL_HAM_MEDIUM(-0.98)[-0.984,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; REPLY(-4.00)[] X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Sep 2019 12:48:28 -0000 On Mon, Sep 23, 2019 at 03:09:16PM +0300, Andriy Gapon wrote: > On 20/09/2019 20:38, Konstantin Belousov wrote: > > On Fri, Sep 20, 2019 at 07:52:20PM +0300, Andriy Gapon wrote: > >> > >> Fatal error 'mutex 0x800661000 own 0x80000010 is on list 0x8006591a0 0x0' at > >> line 153 in file /usr/src/lib/libthr/thread/thr_mutex.c (errno = 0) > >> > >> This happens with a mutex initialized with PTHREAD_PROCESS_SHARED, > >> PTHREAD_MUTEX_ROBUST and PTHREAD_MUTEX_ERRORCHECK. > >> The situation that leads to the abort seems to be this: > >> - one process takes the lock and then crashes without releasing the lock > >> - some time later another process does a cleanup and attempts to destroy the mutex > >> That's where the assertion happens. > >> > >> Specifically, it seems that the assert is tripped if there are no other > >> operations on the lock between the crash of one process and the destroy in the > >> the other process. > >> > >> I wrote a small test program to demo the issue: > >> https://people.freebsd.org/~avg/shared_mtx.c > >> > >> The state of the mutex in a crash dump is this: > >> (gdb) p/x *(struct pthread_mutex *)0x800661000 > >> $6 = {m_lock = {m_owner = 0x80000010, m_flags = 0x11, m_ceilings = {0x0, 0x0}, > >> m_rb_lnk = 0x0, m_spare = {0x0, 0x0}}, m_flags = 0x1, m_count = 0x0, m_spinloops > >> = 0x0, m_yieldloops = 0x0, m_ps = 0x2, m_qe = {tqe_next = 0x0, > >> tqe_prev = 0x8006591a0}, m_pqe = {tqe_next = 0x0, tqe_prev = 0x0}, m_rb_prev > >> = 0x0} > >> > >> So, it's m_qe.tqe_prev != NULL that leads to the assert. > > > > This is only relevant for robust mutexes, otherwise the behavior is > > undefined if the owner terminates without unlocking it. I believe that > > in case of the kernel-assisted UMUTEX_RB_OWNERDEAD state, we should skip > > mutex_assert_not_owned(), same as in enqueue_mutex(). > > Thank you very much! > The patch does help. > I think that there's probably no good way to clean up m_qe. The state of robust mutexes is mostly recovered by kernel, but kernel only knows about the umutex part of the struct pthread_mutex. In fact, other parts of libthr do the same as the patch: they ignore mutex linkage if it is robust mutex recovered after the owner death.