Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1494220pxb; Thu, 4 Mar 2021 12:50:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJwUKoQ0rxw6MJ/hFGLBZJ1fXPzPwkrenVmqvZjM5K8xWfA76+yYB/F6ZJpWKxZwpZh5hEUK X-Received: by 2002:a05:6402:4d3:: with SMTP id n19mr6488355edw.2.1614891045841; Thu, 04 Mar 2021 12:50:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614891045; cv=none; d=google.com; s=arc-20160816; b=vYkISC48gFFnit2Qlg/EaT3Ya9p4kKatlG95Qsz7Zb7p7vVsFaV1UvJoT28Q+iDYof 3+j+OxeY+K9AQZ5NTY8kL6AQYP1HjhKYdyqrJEyG1RCw/QSy8vHrYxRXnTfEsTlQKazo Y0WjQX47+qryOTekJH/i/Pa97eMM57TeOpmEvE19DrlRnrT7/raPPAZQDf3nE2kdFuDk 6HAmrlBDU6+KT9glI4nBwZrPitioZBbnAQGaPXjcRSdIs03Do4IsSvFEOxQ1uODepZ9u fyOYm5zT1VlWaig0/XtxHsAGNaAZv5yBktS+HPQw9L0QHWpf/I8tu9B2ztljZUo50IgO 04Vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=ViTcAlhSmNkExG94Wm51K4123S+pWZpoRGPdHFgXJew=; b=Fg3lkP9u9IQXM2mJhoTFo9ee4innBdqOsstZyCImn336rvfVQoEYfVRY4qwfKsuvZn auLjTcnVUr6JKqOOFopzgBtL5S9LNhgnuW8Uqvg6kRkfhCb5wDimXzJf64jnkawimwzL Y+VTT7LjL0AH9kejix/FSTW4U3Cb4D2j2cbAYYhI6H9h670oPmr6rOjq2xBUo/Tlim+g cs36DoqeoL8BhNu/sVpHEN3Q2iMFEeraMTdlUPZFHPlWjmBHEgVWMEIVhcKatB4VGbQz 1wpYS3KUeorHf1VyCR/r5IyULd4fXwYi8RHBQNWz9fX73QApPyAZpbKcx1MW4ACg2C/D WXIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="YLtx/QpG"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t12si204376ejc.21.2021.03.04.12.50.23; Thu, 04 Mar 2021 12:50:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="YLtx/QpG"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357653AbhCCLXg (ORCPT + 99 others); Wed, 3 Mar 2021 06:23:36 -0500 Received: from mail.kernel.org ([198.145.29.99]:34188 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347784AbhCCCH0 (ORCPT ); Tue, 2 Mar 2021 21:07:26 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9342264E6C; Wed, 3 Mar 2021 02:06:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614737203; bh=yg9kPXSlYXmA2Ua4RbSvM8mbNJoUaTmwL4I5y8Snpfk=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=YLtx/QpGA5m0FeOE4fBQzaYSyAGBsxMFyDEQ6+xJQRFiY6Fsob/yXxuhI90e7CSIi V3cwI8HhzB6DdjMLo2l3ZfOYlp+ABXtwx00HUC8FU1KCsQHq83Hzc+XOKIEBGpoaz5 UL2Wk5MZ5sAQiClOcfjJcBxhbh8iHGvvF/ayqW17O0Mi/nIcTQlqb/nd4s42InFtzQ 6o3dFCooZ0gJle5RJG/Q93rEsmVoshH3tIT8RNuEWz2HyFCbtlS012KJIvH9Tskc2K rziu05qWUAfHDmzictahZ/sdryWlplg2q7TiuxROTHbEOgwJy9kpqoxjQGEhK7YAIo TDPEEtDqF6NNg== Received: by paulmck-ThinkPad-P72.home (Postfix, from userid 1000) id 3B57D3522A62; Tue, 2 Mar 2021 18:06:43 -0800 (PST) Date: Tue, 2 Mar 2021 18:06:43 -0800 From: "Paul E. McKenney" To: Frederic Weisbecker Cc: LKML , Thomas Gleixner , Boqun Feng , Lai Jiangshan , Neeraj Upadhyay , Josh Triplett , Stable , Joel Fernandes Subject: Re: [PATCH 01/13] rcu/nocb: Fix potential missed nocb_timer rearm Message-ID: <20210303020643.GV2696@paulmck-ThinkPad-P72> Reply-To: paulmck@kernel.org References: <20210223001011.127063-1-frederic@kernel.org> <20210223001011.127063-2-frederic@kernel.org> <20210224183709.GI2743@paulmck-ThinkPad-P72> <20210224220606.GA3179@lothringen> <20210302014829.GK2696@paulmck-ThinkPad-P72> <20210302123444.GA97498@lothringen> <20210302181729.GN2696@paulmck-ThinkPad-P72> <20210303013533.GA102493@lothringen> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210303013533.GA102493@lothringen> User-Agent: Mutt/1.9.4 (2018-02-28) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 03, 2021 at 02:35:33AM +0100, Frederic Weisbecker wrote: > On Tue, Mar 02, 2021 at 10:17:29AM -0800, Paul E. McKenney wrote: > > On Tue, Mar 02, 2021 at 01:34:44PM +0100, Frederic Weisbecker wrote: > > > > OK, how about if I queue a temporary commit (shown below) that just > > calls out the first scenario so that I can start testing, and you get > > me more detail on the second scenario? I can then update the commit. > > Sure, meanwhile here is an attempt for a nocb_bypass_timer based > scenario, it's overly hairy and perhaps I picture more power > in the hands of callbacks advancing on nocb_cb_wait() than it > really has: Thank you very much! I must defer looking through this in detail until I am more awake, but I do very much like the fine-grained exposition. Thanx, Paul > 0. CPU 0's ->nocb_cb_kthread just called rcu_do_batch() and > executed all the ready callbacks. Its segcblist is now > entirely empty. It's preempted while calling local_bh_enable(). > > 1. A new callback is enqueued on CPU 0 with IRQs enabled. So > the ->nocb_gp_kthread for CPU 0-2's is awaken. Then a storm > of callbacks enqueue follows on CPU 0 and even reaches the > bypass queue. Note that ->nocb_gp_kthread is also associated > with CPU 0. > > 2. CPU 0 queues one last bypass callback. > > 3. The ->nocb_gp_kthread wakes up and associates a grace period > with the whole queue of regular callbacks on CPU 0. It also > tries to flush the bypass queue of CPU 0 but the bypass lock > is contended due to the concurrent enqueuing on the previous > step 2, so the flush fails. > > 4. This ->nocb_gp_kthread arms its ->nocb_bypass_timer and goes > to sleep waiting for the end of this future grace period. > > 5. This grace period elapses before the ->nocb_bypass_timer timer > fires. This is normally improbably given that the timer is set > for only two jiffies, but timers can be delayed. Besides, it > is possible that kernel was built with CONFIG_RCU_STRICT_GRACE_PERIOD=y. > > 6. The grace period ends, so rcu_gp_kthread awakens the > ->nocb_gp_kthread but it doesn't get a chance to run on a CPU > before a while. > > 7. CPU 0's ->nocb_cb_kthread get back to the CPU after its preemption. > As it notices the new completed grace period, it advances the callbacks > and executes them. Then it gets preempted again on local_bh_enabled(). > > 8. A new callback enqueue on CPU 0 flushes itself the bypass queue > because CPU 0's ->nocb_nobypass_count < nocb_nobypass_lim_per_jiffy. > > 9. CPUs from other ->nocb_gp_kthread groups (above CPU 2) initiate and > elapse a few grace periods. CPU 0's ->nocb_gp_kthread still hasn't > got an opportunity to run on a CPU and its ->nocb_bypass_timer still > hasn't fired. > > 10. CPU 0's ->nocb_cb_kthread wakes up from preemption. It notices the > new grace periods that have elapsed, advance all the callbacks and > executes them. Then it goes to sleep waiting for invocable callbacks. > > 11. CPU 0 enqueues a new callback with interrupts disabled, and > defers awakening its ->nocb_gp_kthread even though ->nocb_gp_sleep > is actually false. It therefore queues its rcu_data structure's > ->nocb_timer. At this point, CPU 0's rdp->nocb_defer_wakeup is > RCU_NOCB_WAKE. > > 12. The ->nocb_bypass_timer finally fires! It doesn't wake up > ->nocb_gp_kthread because it's actually awaken already. > But it cancels CPU 0's ->nocb_timer armed at 11. Yet it doesn't > re-initialize CPU 0's ->nocb_defer_wakeup which stays with the > stale RCU_NOCB_WAKE value. So CPU 0's->nocb_defer_wakeup and > its ->nocb_timer are now desynchronized. > > 13. The ->nocb_gp_kthread finally runs. It cancels the ->nocb_bypass_timer > which has already fired. It sees the new callback on CPU 0 and > associate it with a new grace period then sleep on it. > > 14. The grace period elapses, rcu_gp_kthread wakes up ->nocb_gb_kthread > which wakes up CPU 0's->nocb_cb_kthread which runs the callback. > Both ->nocb_gp_kthread and CPU 0's->nocb_cb_kthread now wait for new > callbacks. > > 15. CPU 0 enqueues another callback, again with interrupts > disabled so it must queue a timer for a deferred wakeup. However > the value of its ->nocb_defer_wakeup is RCU_NOCB_WAKE which > incorrectly indicates that a timer is already queued. Instead, > CPU 0's ->nocb_timer was cancelled in 12. CPU 0 therefore fails > to queue the ->nocb_timer. > > 16. CPU 0 has its pending callback and it may go unnoticed until > some other CPU ever wakes up ->nocb_gp_kthread or CPU 0 ever > calls an explicit deferred wakeup, for example, during idle entry. > > > Thanks.