From owner-freebsd-hackers@freebsd.org Tue Jan 10 21:15:18 2017 Return-Path: Delivered-To: freebsd-hackers@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 22144CAA2D6 for ; Tue, 10 Jan 2017 21:15:18 +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 E3D6910BE; Tue, 10 Jan 2017 21:15:17 +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 CEEFC1FE1B5; Tue, 10 Jan 2017 22:15:15 +0100 (CET) Subject: Re: draining high-frequency callouts To: Mark Johnston , freebsd-hackers@FreeBSD.org References: <20170110205711.GA86449@wkstn-mjohnston.west.isilon.com> From: Hans Petter Selasky Message-ID: <68472d52-f4a0-a4cc-ec66-d39ee2f065ee@selasky.org> Date: Tue, 10 Jan 2017 22:14:44 +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: <20170110205711.GA86449@wkstn-mjohnston.west.isilon.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Jan 2017 21:15:18 -0000 On 01/10/17 21:57, Mark Johnston wrote: > I'm occasionally seeing an assertion failure in softclock_call_cc() when > running DTrace tests on a system with hz=10000. The assertion > (c->c_flags & CALLOUT_ACTIVE) != 0 is failing while a thread is > concurrently draining the callout, which runs at a high frequency. At > the time of the panic, that thread is spinning on the per-CPU callout > lock after having been awoken from "codrain", and CALLOUT_PENDING is > set on the callout. The callout is direct, i.e., it is executed in hard > interrupt context. > > I think this is what's happening: > - callout_drain() is called while the callout is executing but after the > callout has rescheduled itself, and goes to sleep after having cleared > CALLOUT_ACTIVE. > - softclock_call_cc() wakes up the callout_drain() caller, but the > callout fires again before the caller is scheduled. > - the second softclock_call_cc() call sees that CALLOUT_ACTIVE is > cleared and panics. > > Is there anything that prevents this scenario? Is it really correct to > leave CALLOUT_ACTIVE cleared when the per-CPU callout lock must be > dropped in order to acquire a sleepqueue lock? > Hi Mark, First of all, thank you for reporting this bug. I believe this bug is not reproducible with the hps_head project branch, which has the exact same KASSERT() in softclock_call_cc(), but uses a different callout drain logic: https://svnweb.freebsd.org/base/projects/hps_head/sys/kern/kern_timeout.c?revision=309221&view=markup#l1261 From my understanding of kern/kern_timeout.c, you are right that the "goto again" due to a LOR between the sleepqueue spinlock and the callout subsystem spinlocks, leaves a race open. I've long thought that the callout_drain() and callout_reset() functions in -head have become obfuscated after the introduction of per-CPU callouts and deserves a rewrite. https://svnweb.freebsd.org/base/projects/hps_head --HPS