Re: [RFC] Make need_resched() return true when rcu_urgent_qs requested

From: Paul E. McKenney
Date: Mon Jul 09 2018 - 18:06:14 EST


On Mon, Jul 09, 2018 at 02:05:32PM -0700, Paul E. McKenney wrote:
> On Mon, Jul 09, 2018 at 09:45:45PM +0100, David Woodhouse wrote:
> > On Mon, 2018-07-09 at 13:42 -0700, Paul E. McKenney wrote:
> > > On Mon, Jul 09, 2018 at 09:35:38PM +0100, David Woodhouse wrote:
> > > > 
> > > > 
> > > > On Mon, 2018-07-09 at 13:34 -0700, Paul E. McKenney wrote:
> > > > > 
> > > > > So here are the possible code paths when .rcu_urgent_qs is set to true:
> > > > > 
> > > > > 1.      A context switch will record the quiescent state and clear
> > > > >         .rcu_urgent_qs.  (The failure to do the clearing in current -rcu
> > > > >         for PREEMPT builds is a performance bug that I need to fix.)
> > > > 
> > > > What if there's nothing else runnable and there is no actual context
> > > > switch?
> > >
> > > The scheduler invokes rcu_note_context_switch() before looking to see
> > > if there really will or won't be a context switch.
> > >
> > > I am sure that Peter will correct me if I am confused on this point.  ;-)
> >
> > Ah, OK. Yes, that looks correct. Thanks.
>
> Here is hoping!
>
> > I'll give your patch a spin tomorrow, unless Marius beats me to it.
>
> Please see below for the version that I eventually queued. Should Marius
> have a Reported-by? If so, please tell me his full name so I can add that.

And the earlier patch was against my -rcu tree, which won't be all that
helpful for v4.15. Please see below for a lightly tested backport to v4.15.

It should apply to all the releases of interest. If other backports
are needed, please remind me of my woodhouse.v4.15.2018.07.09a tag.

Thanx, Paul

------------------------------------------------------------------------

commit 6361b81827a8f93f582124da385258fc04a38a7f
Author: Paul E. McKenney <paulmck@xxxxxxxxxxxxxxxxxx>
Date: Mon Jul 9 13:47:30 2018 -0700

rcu: Make need_resched() respond to urgent RCU-QS needs

The per-CPU rcu_dynticks.rcu_urgent_qs variable communicates an urgent
need for an RCU quiescent state from the force-quiescent-state processing
within the grace-period kthread to context switches and to cond_resched().
Unfortunately, such urgent needs are not communicated to need_resched(),
which is sometimes used to decide when to invoke cond_resched(), for
but one example, within the KVM vcpu_run() function. As of v4.15, this
can result in synchronize_sched() being delayed by up to ten seconds,
which can be problematic, to say nothing of annoying.

This commit therefore checks rcu_dynticks.rcu_urgent_qs from within
rcu_check_callbacks(), which is invoked from the scheduling-clock
interrupt handler. If the current task is not an idle task and is
not executing in usermode, a context switch is forced, and either way,
the rcu_dynticks.rcu_urgent_qs variable is set to false. If the current
task is an idle task, then RCU's dyntick-idle code will detect the
quiescent state, so no further action is required. Similarly, if the
task is executing in usermode, other code in rcu_check_callbacks() and
its called functions will report the corresponding quiescent state.

Reported-by: David Woodhouse <dwmw2@xxxxxxxxxxxxx>
Suggested-by: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Signed-off-by: Paul E. McKenney <paulmck@xxxxxxxxxxxxxxxxxx>
[ paulmck: Backported to v4.15. Probably applies elsewhere. ]

diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c
index f9c0ca2ccf0c..3350ece366ab 100644
--- a/kernel/rcu/tree.c
+++ b/kernel/rcu/tree.c
@@ -2839,6 +2839,15 @@ void rcu_check_callbacks(int user)
rcu_bh_qs();
}
rcu_preempt_check_callbacks();
+ /* The load-acquire pairs with the store-release setting to true. */
+ if (smp_load_acquire(this_cpu_ptr(&rcu_dynticks.rcu_urgent_qs))) {
+ /* Idle and userspace execution already are quiescent states. */
+ if (rcu_is_cpu_rrupt_from_idle() && !user) {
+ set_tsk_need_resched(current);
+ set_preempt_need_resched();
+ }
+ __this_cpu_write(rcu_dynticks.rcu_urgent_qs, false);
+ }
if (rcu_pending())
invoke_rcu_core();
if (user)