Commit 962aff03 authored by Paul E. McKenney's avatar Paul E. McKenney

rcu: Clean up handling of tasks blocked across full-rcu_node offline

Commit 0aa04b05 ("rcu: Process offlining and onlining only at
grace-period start") deferred handling of CPU-hotplug events until the
start of the next grace period, but consider the following sequence
of events:

1.	A task is preempted within an RCU-preempt read-side critical
	section.

2.	The CPU that this task was running on goes offline, along with all
	other CPUs sharing the corresponding leaf rcu_node structure.

3.	The task resumes execution.

4.	One of those CPUs comes back online before a new grace period starts.

In step 2, the code in the next rcu_gp_init() invocation will (correctly)
defer removing the leaf rcu_node structure from the upper-level bitmasks,
and will (correctly) set that structure's ->wait_blkd_tasks field.  During
the ensuing interval, RCU will (correctly) track the tasks preempted on
that structure because they must block any subsequent grace period.

In step 3, the code in rcu_read_unlock_special() will (correctly) remove
the task from the leaf rcu_node structure.  From this point forward, RCU
need not pay attention to this structure, at least not until one of the
corresponding CPUs comes back online.

In step 4, the code in the next rcu_gp_init() invocation will
(incorrectly) invoke rcu_init_new_rnp().  This is incorrect because
the corresponding rcu_cleanup_dead_rnp() was never invoked.  This is
nevertheless harmless because the upper-level bits are still set.
So, no harm, no foul, right?

At least, all is well until a little further into rcu_gp_init()
invocation, which will notice that there are no longer any tasks blocked
on the leaf rcu_node structure, conclude that there is no longer anything
left over from step 2's offline operation, and will therefore invoke
rcu_cleanup_dead_rnp().  But this invocation of rcu_cleanup_dead_rnp()
is for the beginning of the earlier offline interval, and the previous
invocation of rcu_init_new_rnp() is for the end of that same interval.
That is right, they are invoked out of order.

That cannot be good, can it?

It turns out that this is not a (correctness!) problem because
rcu_cleanup_dead_rnp() checks to see if any of the corresponding CPUs
are online, and refuses to do anything if so.  In other words, in the
case where rcu_init_new_rnp() and rcu_cleanup_dead_rnp() execute out of
order, they both have no effect.

But this is at best an accident waiting to happen.

This commit therefore adds logic to rcu_gp_init() so that
rcu_init_new_rnp() and rcu_cleanup_dead_rnp() are always invoked in
order, and so that neither are invoked at all in cases where RCU had to
pay attention to the leaf rcu_node structure during the entire time that
all corresponding CPUs were offline.

And, while in the area, this commit reduces confusion by using formal
parameters rather than local variables that just happen to have the same
value at that particular point in the code.
Signed-off-by: default avatarPaul E. McKenney <paulmck@linux.vnet.ibm.com>
parent 226ca5e7
...@@ -1909,13 +1909,15 @@ static bool rcu_gp_init(struct rcu_state *rsp) ...@@ -1909,13 +1909,15 @@ static bool rcu_gp_init(struct rcu_state *rsp)
/* If zero-ness of ->qsmaskinit changed, propagate up tree. */ /* If zero-ness of ->qsmaskinit changed, propagate up tree. */
if (!oldmask != !rnp->qsmaskinit) { if (!oldmask != !rnp->qsmaskinit) {
if (!oldmask) /* First online CPU for this rcu_node. */ if (!oldmask) { /* First online CPU for rcu_node. */
if (!rnp->wait_blkd_tasks) /* Ever offline? */
rcu_init_new_rnp(rnp); rcu_init_new_rnp(rnp);
else if (rcu_preempt_has_tasks(rnp)) /* blocked tasks */ } else if (rcu_preempt_has_tasks(rnp)) {
rnp->wait_blkd_tasks = true; rnp->wait_blkd_tasks = true; /* blocked tasks */
else /* Last offline CPU and can propagate. */ } else { /* Last offline CPU and can propagate. */
rcu_cleanup_dead_rnp(rnp); rcu_cleanup_dead_rnp(rnp);
} }
}
/* /*
* If all waited-on tasks from prior grace period are * If all waited-on tasks from prior grace period are
...@@ -1923,13 +1925,12 @@ static bool rcu_gp_init(struct rcu_state *rsp) ...@@ -1923,13 +1925,12 @@ static bool rcu_gp_init(struct rcu_state *rsp)
* still offline, propagate up the rcu_node tree and * still offline, propagate up the rcu_node tree and
* clear ->wait_blkd_tasks. Otherwise, if one of this * clear ->wait_blkd_tasks. Otherwise, if one of this
* rcu_node structure's CPUs has since come back online, * rcu_node structure's CPUs has since come back online,
* simply clear ->wait_blkd_tasks (but rcu_cleanup_dead_rnp() * simply clear ->wait_blkd_tasks.
* checks for this, so just call it unconditionally).
*/ */
if (rnp->wait_blkd_tasks && if (rnp->wait_blkd_tasks &&
(!rcu_preempt_has_tasks(rnp) || (!rcu_preempt_has_tasks(rnp) || rnp->qsmaskinit)) {
rnp->qsmaskinit)) {
rnp->wait_blkd_tasks = false; rnp->wait_blkd_tasks = false;
if (!rnp->qsmaskinit)
rcu_cleanup_dead_rnp(rnp); rcu_cleanup_dead_rnp(rnp);
} }
...@@ -2450,9 +2451,10 @@ static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf) ...@@ -2450,9 +2451,10 @@ static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
long mask; long mask;
struct rcu_node *rnp = rnp_leaf; struct rcu_node *rnp = rnp_leaf;
raw_lockdep_assert_held_rcu_node(rnp); raw_lockdep_assert_held_rcu_node(rnp_leaf);
if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) || if (!IS_ENABLED(CONFIG_HOTPLUG_CPU) ||
rnp->qsmaskinit || rcu_preempt_has_tasks(rnp)) WARN_ON_ONCE(rnp_leaf->qsmaskinit) ||
WARN_ON_ONCE(rcu_preempt_has_tasks(rnp_leaf)))
return; return;
for (;;) { for (;;) {
mask = rnp->grpmask; mask = rnp->grpmask;
...@@ -2461,7 +2463,8 @@ static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf) ...@@ -2461,7 +2463,8 @@ static void rcu_cleanup_dead_rnp(struct rcu_node *rnp_leaf)
break; break;
raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */ raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
rnp->qsmaskinit &= ~mask; rnp->qsmaskinit &= ~mask;
rnp->qsmask &= ~mask; /* Between grace periods, so better already be zero! */
WARN_ON_ONCE(rnp->qsmask);
if (rnp->qsmaskinit) { if (rnp->qsmaskinit) {
raw_spin_unlock_rcu_node(rnp); raw_spin_unlock_rcu_node(rnp);
/* irqs remain disabled. */ /* irqs remain disabled. */
...@@ -3479,6 +3482,7 @@ static void rcu_init_new_rnp(struct rcu_node *rnp_leaf) ...@@ -3479,6 +3482,7 @@ static void rcu_init_new_rnp(struct rcu_node *rnp_leaf)
struct rcu_node *rnp = rnp_leaf; struct rcu_node *rnp = rnp_leaf;
raw_lockdep_assert_held_rcu_node(rnp); raw_lockdep_assert_held_rcu_node(rnp);
WARN_ON_ONCE(rnp->wait_blkd_tasks);
for (;;) { for (;;) {
mask = rnp->grpmask; mask = rnp->grpmask;
rnp = rnp->parent; rnp = rnp->parent;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment