Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1977626pxb; Fri, 5 Mar 2021 04:37:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9m1Y30TJtVSBHOztHMD2VlEZe+JpqXsS0yZsEyzlCwbwp7FdUVazuljNw2EeQnnd9rclq X-Received: by 2002:a17:906:79c7:: with SMTP id m7mr1937005ejo.337.1614947823179; Fri, 05 Mar 2021 04:37:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614947823; cv=none; d=google.com; s=arc-20160816; b=jtY9PlI3emqeySSgDmLUVeRDGIoi4/5LK7mB/L7A1foQ+ESWv5hh++BC8+b9kEeu/Y XL7KzqprgWjKVHCgvSorrFaWUTbAFjGaG/+TeRim3Nz8ft/A+cUYegg/ixBA+oBvfPUg 5VM+yL4zmMOogbpCtxnn0ac46WNw7IrTv+s7Q+53YnSeJcQmCLqcEkQRmHC8mTcOaz73 i5TYa4LSjQxvSnaTt1xMG2pHq+BtyaojF8PSBK3MHr6AAoN7czozIBx0NLHJ54pX2PJi nw+W5n4qc5+0N9CxyNeHDeDCDT1CGFeb2QTPtDK//JoU3lsPfZREMNkL/RvfrnHbx5bQ ZZaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=oGTINlHmQKl9ztLwTRcupX4V+vjHDb/L+9z+tg39Rwg=; b=X1tAt3L/QP5C9oEJaTfBZFnYR2C98irlghM9YGGq8Kv+hZAMb6mrwt+jkJnWH3+rS2 VLARxVAsDjIYkLHjVlD3AQSZ4AHK4w8z2IxmcNBPF43X3PZQb30AMjHrWJqtIlVUX4nc D6RA+GMqYf30bukQKp5gHy5imH8ZKoomT2GZHqlUEwEDRzSJCEEH1hubfVWhzy1mtom9 PSEmcWmZMI5R6qXGqbdwsd5XYzsHBFlA1LMfPHYA/nF7JNeMUnctIX/kAn1iL7q16862 FNsaWSDl/pEJ7ETnAfXkMPtXTN1FO7B6O9yG60RViEV48KlMJm1InNfs8SpsKC7PTKQk d/1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ON9SeZjc; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g20si1513600edy.270.2021.03.05.04.36.40; Fri, 05 Mar 2021 04:37:03 -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=@linuxfoundation.org header.s=korg header.b=ON9SeZjc; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232711AbhCEMfp (ORCPT + 99 others); Fri, 5 Mar 2021 07:35:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:47414 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232707AbhCEMfO (ORCPT ); Fri, 5 Mar 2021 07:35:14 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 384DA6501B; Fri, 5 Mar 2021 12:35:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614947713; bh=htG5O30EYJqYcSTrZwAQjV8N9KjUwSQ1S+e1Fe/+buI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ON9SeZjciD/f4rf5g4B2OeeImFZ19anQKe2613/zwT0rZSkXNEfOzQEXKxnZyL3xs pUzny2KjosRskLuXXKwIKKbLspJKq4mKEdWwxZDXL9MLryPxXHmU3W5qMx7z9iKpmV ar7kyDSId6ymFH0JLmjy98zwzwJ6uvslxAhiVWOM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, "Paul E. McKenney" , Frederic Weisbecker , "Peter Zijlstra (Intel)" , Ingo Molnar , Sasha Levin Subject: [PATCH 5.4 27/72] rcu/nocb: Trigger self-IPI on late deferred wake up before user resume Date: Fri, 5 Mar 2021 13:21:29 +0100 Message-Id: <20210305120858.665649434@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210305120857.341630346@linuxfoundation.org> References: <20210305120857.341630346@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Frederic Weisbecker [ Upstream commit f8bb5cae9616224a39cbb399de382d36ac41df10 ] Entering RCU idle mode may cause a deferred wake up of an RCU NOCB_GP kthread (rcuog) to be serviced. Unfortunately the call to rcu_user_enter() is already past the last rescheduling opportunity before we resume to userspace or to guest mode. We may escape there with the woken task ignored. The ultimate resort to fix every callsites is to trigger a self-IPI (nohz_full depends on arch to implement arch_irq_work_raise()) that will trigger a reschedule on IRQ tail or guest exit. Eventually every site that want a saner treatment will need to carefully place a call to rcu_nocb_flush_deferred_wakeup() before the last explicit need_resched() check upon resume. Fixes: 96d3fd0d315a (rcu: Break call_rcu() deadlock involving scheduler and perf) Reported-by: Paul E. McKenney Signed-off-by: Frederic Weisbecker Signed-off-by: Peter Zijlstra (Intel) Signed-off-by: Ingo Molnar Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20210131230548.32970-4-frederic@kernel.org Signed-off-by: Sasha Levin --- kernel/rcu/tree.c | 21 ++++++++++++++++++++- kernel/rcu/tree.h | 2 +- kernel/rcu/tree_plugin.h | 25 ++++++++++++++++--------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index 4dfa9dd47223..71d3717ee66d 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -604,6 +604,18 @@ void rcu_idle_enter(void) } #ifdef CONFIG_NO_HZ_FULL + +/* + * An empty function that will trigger a reschedule on + * IRQ tail once IRQs get re-enabled on userspace resume. + */ +static void late_wakeup_func(struct irq_work *work) +{ +} + +static DEFINE_PER_CPU(struct irq_work, late_wakeup_work) = + IRQ_WORK_INIT(late_wakeup_func); + /** * rcu_user_enter - inform RCU that we are resuming userspace. * @@ -621,12 +633,19 @@ void rcu_user_enter(void) lockdep_assert_irqs_disabled(); + /* + * We may be past the last rescheduling opportunity in the entry code. + * Trigger a self IPI that will fire and reschedule once we resume to + * user/guest mode. + */ instrumentation_begin(); - do_nocb_deferred_wakeup(rdp); + if (do_nocb_deferred_wakeup(rdp) && need_resched()) + irq_work_queue(this_cpu_ptr(&late_wakeup_work)); instrumentation_end(); rcu_eqs_enter(true); } + #endif /* CONFIG_NO_HZ_FULL */ /* diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h index c612f306fe89..564adf10c86f 100644 --- a/kernel/rcu/tree.h +++ b/kernel/rcu/tree.h @@ -438,7 +438,7 @@ static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp, static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty, unsigned long flags); static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp); -static void do_nocb_deferred_wakeup(struct rcu_data *rdp); +static bool do_nocb_deferred_wakeup(struct rcu_data *rdp); static void rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp); static void rcu_spawn_cpu_nocb_kthread(int cpu); static void __init rcu_spawn_nocb_kthreads(void); diff --git a/kernel/rcu/tree_plugin.h b/kernel/rcu/tree_plugin.h index a71a4a272515..0eca302c59d7 100644 --- a/kernel/rcu/tree_plugin.h +++ b/kernel/rcu/tree_plugin.h @@ -1639,8 +1639,8 @@ bool rcu_is_nocb_cpu(int cpu) * Kick the GP kthread for this NOCB group. Caller holds ->nocb_lock * and this function releases it. */ -static void wake_nocb_gp(struct rcu_data *rdp, bool force, - unsigned long flags) +static bool wake_nocb_gp(struct rcu_data *rdp, bool force, + unsigned long flags) __releases(rdp->nocb_lock) { bool needwake = false; @@ -1651,7 +1651,7 @@ static void wake_nocb_gp(struct rcu_data *rdp, bool force, trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("AlreadyAwake")); rcu_nocb_unlock_irqrestore(rdp, flags); - return; + return false; } del_timer(&rdp->nocb_timer); rcu_nocb_unlock_irqrestore(rdp, flags); @@ -1664,6 +1664,8 @@ static void wake_nocb_gp(struct rcu_data *rdp, bool force, raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags); if (needwake) wake_up_process(rdp_gp->nocb_gp_kthread); + + return needwake; } /* @@ -2155,20 +2157,23 @@ static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp) } /* Do a deferred wakeup of rcu_nocb_kthread(). */ -static void do_nocb_deferred_wakeup_common(struct rcu_data *rdp) +static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp) { unsigned long flags; int ndw; + int ret; rcu_nocb_lock_irqsave(rdp, flags); if (!rcu_nocb_need_deferred_wakeup(rdp)) { rcu_nocb_unlock_irqrestore(rdp, flags); - return; + return false; } ndw = READ_ONCE(rdp->nocb_defer_wakeup); WRITE_ONCE(rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT); - wake_nocb_gp(rdp, ndw == RCU_NOCB_WAKE_FORCE, flags); + ret = wake_nocb_gp(rdp, ndw == RCU_NOCB_WAKE_FORCE, flags); trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake")); + + return ret; } /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */ @@ -2184,10 +2189,11 @@ static void do_nocb_deferred_wakeup_timer(struct timer_list *t) * This means we do an inexact common-case check. Note that if * we miss, ->nocb_timer will eventually clean things up. */ -static void do_nocb_deferred_wakeup(struct rcu_data *rdp) +static bool do_nocb_deferred_wakeup(struct rcu_data *rdp) { if (rcu_nocb_need_deferred_wakeup(rdp)) - do_nocb_deferred_wakeup_common(rdp); + return do_nocb_deferred_wakeup_common(rdp); + return false; } void rcu_nocb_flush_deferred_wakeup(void) @@ -2527,8 +2533,9 @@ static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp) return false; } -static void do_nocb_deferred_wakeup(struct rcu_data *rdp) +static bool do_nocb_deferred_wakeup(struct rcu_data *rdp) { + return false; } static void rcu_spawn_cpu_nocb_kthread(int cpu) -- 2.30.1