sched: Add check for lost wakeups in idle loop

Note: Diagnostic only, not intended for mainline.

The scheduler can lose wakeups, as can be seen by making the rcutorture
TREE01 scenario enable RCU priority boosting without also enabling testing
of RCU priority boosting.  The following pair of commits in my -rcu tree
[1] make these changes:

c7684673c6ca ("EXP rcutorture: Enable RCU_BOOST where supported")
4b08261f7bff ("EXP rcutorture: Disable RCU priority boost testing on non-TREE03 scenarios")

The following command reproduces this problem, which shows up as RCU
CPU stall warnings on rcu_torture_reader() kthreads:

tools/testing/selftests/rcutorture/bin/kvm.sh --allcpus --duration 20m --configs '19*TREE01' --bootargs "nohz=off rcutorture.onoff_interval=0 rcutorture.nocbs_nthreads=0 rcutorture.shuffle_interval=0" --trust-make

The following pair of commits add diagnostics that show that the kthread
causing the stall is consistently preempted on an idle CPU:

49be2ba9f0fa ("rcu: Add running and boosted indications to RCU task stall dump")
e3bbe32c1fd9 ("EXP rcu: Print CPU that preempted stalled reader wants to run on")

It appears that the cause of this issue is the wakeup racing with the
target CPU entering the idle loop.  This patch does a recheck of flags to
detect this condition and exit the idle loop when detected.  Note that
this is *not* a complete fix in the general case because the wakeup
might happen just after this check.  However, it does appear to work
for the above reproducer because of the nohz=off kernel boot parameter.

This patch also splats when the recheck detects a racing wakeup, which
helps demonstrate that this patch is actually doing something.

An alternative work-around for this issue within RCU may be found here:

34bace6b7fab ("EXP rcu: Send resched_cpu() to idle CPUs having preempted RCU readers")

This commit depends on e3bbe32c1fd9 above, and both have the annoying
habit of reaching into scheduler internals to gain access to the
cpu_curr() function.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git

Assisted-by: LLM
Not-signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 file changed