History

rcu: protect __rcu_read_unlock() against scheduler-using irq handlers

The addition of RCU read-side critical sections within runqueue and priority-inheritance lock critical sections introduced some deadlock cycles, for example, involving interrupts from __rcu_read_unlock() where the interrupt handlers call wake_up(). This situation can cause the instance of __rcu_read_unlock() invoked from interrupt to do some of the processing that would otherwise have been carried out by the task-level instance of __rcu_read_unlock(). When the interrupt-level instance of __rcu_read_unlock() is called with a scheduler lock held from interrupt-entry/exit situations where in_irq() returns false, deadlock can result. This commit resolves these deadlocks by using negative values of the per-task ->rcu_read_lock_nesting counter to indicate that an instance of __rcu_read_unlock() is in flight, which in turn prevents instances from interrupt handlers from doing any special processing. This patch is inspired by Steven Rostedt's earlier patch that similarly made __rcu_read_unlock() guard against interrupt-mediated recursion (see https://lkml.org/lkml/2011/7/15/326), but this commit refines Steven's approach to avoid the need for preemption disabling on the __rcu_read_unlock() fastpath and to also avoid the need for manipulating a separate per-CPU variable. This patch avoids need for preempt_disable() by instead using negative values of the per-task ->rcu_read_lock_nesting counter. Note that nested rcu_read_lock()/rcu_read_unlock() pairs are still permitted, but they will never see ->rcu_read_lock_nesting go to zero, and will therefore never invoke rcu_read_unlock_special(), thus preventing them from seeing the RCU_READ_UNLOCK_BLOCKED bit should it be set in ->rcu_read_unlock_special. This patch also adds a check for ->rcu_read_unlock_special being negative in rcu_check_callbacks(), thus preventing the RCU_READ_UNLOCK_NEED_QS bit from being set should a scheduling-clock interrupt occur while __rcu_read_unlock() is exiting from an outermost RCU read-side critical section. Of course, __rcu_read_unlock() can be preempted during the time that ->rcu_read_lock_nesting is negative. This could result in the setting of the RCU_READ_UNLOCK_BLOCKED bit after __rcu_read_unlock() checks it, and would also result it this task being queued on the corresponding rcu_node structure's blkd_tasks list. Therefore, some later RCU read-side critical section would enter rcu_read_unlock_special() to clean up -- which could result in deadlock if that critical section happened to be in the scheduler where the runqueue or priority-inheritance locks were held. This situation is dealt with by making rcu_preempt_note_context_switch() check for negative ->rcu_read_lock_nesting, thus refraining from queuing the task (and from setting RCU_READ_UNLOCK_BLOCKED) if we are already exiting from the outermost RCU read-side critical section (in other words, we really are no longer actually in that RCU read-side critical section). In addition, rcu_preempt_note_context_switch() invokes rcu_read_unlock_special() to carry out the cleanup in this case, which clears out the ->rcu_read_unlock_special bits and dequeues the task (if necessary), in turn avoiding needless delay of the current RCU grace period and needless RCU priority boosting. It is still illegal to call rcu_read_unlock() while holding a scheduler lock if the prior RCU read-side critical section has ever had either preemption or irqs enabled. However, the common use case is legal, namely where then entire RCU read-side critical section executes with irqs disabled, for example, when the scheduler lock is held across the entire lifetime of the RCU read-side critical section.

Paul E. McKenney committed 15 years ago

Commit 10f39bb1 parent be0e1e21 file · +24 −5Browse files

Unified · Split

kernel/rcutree_plugin.h+24−5
@@ -68,6 +68,7 @@ struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state); · 67 unmodified lines
6868 DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
6969 static struct rcu_state *rcu_state = &rcu_preempt_state;
7070
71+static void rcu_read_unlock_special(struct task_struct *t);
7172 static int rcu_preempted_readers_exp(struct rcu_node *rnp);
7273
7374 /*
@@ -147,7 +148,7 @@ static void rcu_preempt_note_context_switch(int cpu) · 73 unmodified lines
147148 struct rcu_data *rdp;
148149 struct rcu_node *rnp;
149150
150− if (t->rcu_read_lock_nesting &&
151+ if (t->rcu_read_lock_nesting > 0 &&
151152 (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
152153
153154 /* Possibly blocking in an RCU read-side critical section. */
@@ -190,6 +191,14 @@ static void rcu_preempt_note_context_switch(int cpu) · 36 unmodified lines
190191 rnp->gp_tasks = &t->rcu_node_entry;
191192 }
192193 raw_spin_unlock_irqrestore(&rnp->lock, flags);
194+ } else if (t->rcu_read_lock_nesting < 0 &&
195+ t->rcu_read_unlock_special) {
196+
197+ /*
198+ * Complete exit from RCU read-side critical section on
199+ * behalf of preempted instance of __rcu_read_unlock().
200+ */
201+ rcu_read_unlock_special(t);
193202 }
194203
195204 /*
@@ -391,13 +400,22 @@ void __rcu_read_unlock(void) · 195 unmodified lines
391400 struct task_struct *t = current;
392401
393402 barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
394− if (--t->rcu_read_lock_nesting == 0) {
395− barrier(); /* decr before ->rcu_read_unlock_special load */
403+ if (t->rcu_read_lock_nesting != 1)
404+ --t->rcu_read_lock_nesting;
405+ else {
406+ t->rcu_read_lock_nesting = INT_MIN;
407+ barrier(); /* assign before ->rcu_read_unlock_special load */
396408 if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
397409 rcu_read_unlock_special(t);
410+ barrier(); /* ->rcu_read_unlock_special load before assign */
411+ t->rcu_read_lock_nesting = 0;
398412 }
399413 #ifdef CONFIG_PROVE_LOCKING
400− WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
414+ {
415+ int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
416+
417+ WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
418+ }
401419 #endif /* #ifdef CONFIG_PROVE_LOCKING */
402420 }
403421 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
@@ -593,7 +611,8 @@ static void rcu_preempt_check_callbacks(int cpu) · 189 unmodified lines
593611 rcu_preempt_qs(cpu);
594612 return;
595613 }
596− if (per_cpu(rcu_preempt_data, cpu).qs_pending)
614+ if (t->rcu_read_lock_nesting > 0 &&
615+ per_cpu(rcu_preempt_data, cpu).qs_pending)
597616 t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
598617 }
599618