2 * Read-Copy Update mechanism for mutual exclusion (tree-based version)
3 * Internal non-public definitions that provide either classic
4 * or preemptable semantics.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * Copyright Red Hat, 2009
21 * Copyright IBM Corporation, 2009
23 * Author: Ingo Molnar <mingo@elte.hu>
24 * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
27 #include <linux/delay.h>
28 #include <linux/stop_machine.h>
31 * Check the RCU kernel configuration parameters and print informative
32 * messages about anything out of the ordinary. If you like #ifdef, you
33 * will love this function.
35 static void __init
rcu_bootup_announce_oddness(void)
37 #ifdef CONFIG_RCU_TRACE
38 printk(KERN_INFO
"\tRCU debugfs-based tracing is enabled.\n");
40 #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
41 printk(KERN_INFO
"\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
44 #ifdef CONFIG_RCU_FANOUT_EXACT
45 printk(KERN_INFO
"\tHierarchical RCU autobalancing is disabled.\n");
47 #ifdef CONFIG_RCU_FAST_NO_HZ
49 "\tRCU dyntick-idle grace-period acceleration is enabled.\n");
51 #ifdef CONFIG_PROVE_RCU
52 printk(KERN_INFO
"\tRCU lockdep checking is enabled.\n");
54 #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
55 printk(KERN_INFO
"\tRCU torture testing starts during boot.\n");
57 #if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
58 printk(KERN_INFO
"\tVerbose stalled-CPUs detection is disabled.\n");
60 #if NUM_RCU_LVL_4 != 0
61 printk(KERN_INFO
"\tExperimental four-level hierarchy is enabled.\n");
65 #ifdef CONFIG_TREE_PREEMPT_RCU
67 struct rcu_state rcu_preempt_state
= RCU_STATE_INITIALIZER(rcu_preempt_state
);
68 DEFINE_PER_CPU(struct rcu_data
, rcu_preempt_data
);
70 static int rcu_preempted_readers_exp(struct rcu_node
*rnp
);
73 * Tell them what RCU they are running.
75 static void __init
rcu_bootup_announce(void)
77 printk(KERN_INFO
"Preemptable hierarchical RCU implementation.\n");
78 rcu_bootup_announce_oddness();
82 * Return the number of RCU-preempt batches processed thus far
83 * for debug and statistics.
85 long rcu_batches_completed_preempt(void)
87 return rcu_preempt_state
.completed
;
89 EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt
);
92 * Return the number of RCU batches processed thus far for debug & stats.
94 long rcu_batches_completed(void)
96 return rcu_batches_completed_preempt();
98 EXPORT_SYMBOL_GPL(rcu_batches_completed
);
101 * Force a quiescent state for preemptible RCU.
103 void rcu_force_quiescent_state(void)
105 force_quiescent_state(&rcu_preempt_state
, 0);
107 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state
);
110 * Record a preemptable-RCU quiescent state for the specified CPU. Note
111 * that this just means that the task currently running on the CPU is
112 * not in a quiescent state. There might be any number of tasks blocked
113 * while in an RCU read-side critical section.
115 * Unlike the other rcu_*_qs() functions, callers to this function
116 * must disable irqs in order to protect the assignment to
117 * ->rcu_read_unlock_special.
119 static void rcu_preempt_qs(int cpu
)
121 struct rcu_data
*rdp
= &per_cpu(rcu_preempt_data
, cpu
);
123 rdp
->passed_quiesc_completed
= rdp
->gpnum
- 1;
125 rdp
->passed_quiesc
= 1;
126 current
->rcu_read_unlock_special
&= ~RCU_READ_UNLOCK_NEED_QS
;
130 * We have entered the scheduler, and the current task might soon be
131 * context-switched away from. If this task is in an RCU read-side
132 * critical section, we will no longer be able to rely on the CPU to
133 * record that fact, so we enqueue the task on the blkd_tasks list.
134 * The task will dequeue itself when it exits the outermost enclosing
135 * RCU read-side critical section. Therefore, the current grace period
136 * cannot be permitted to complete until the blkd_tasks list entries
137 * predating the current grace period drain, in other words, until
138 * rnp->gp_tasks becomes NULL.
140 * Caller must disable preemption.
142 static void rcu_preempt_note_context_switch(int cpu
)
144 struct task_struct
*t
= current
;
146 struct rcu_data
*rdp
;
147 struct rcu_node
*rnp
;
149 if (t
->rcu_read_lock_nesting
&&
150 (t
->rcu_read_unlock_special
& RCU_READ_UNLOCK_BLOCKED
) == 0) {
152 /* Possibly blocking in an RCU read-side critical section. */
153 rdp
= per_cpu_ptr(rcu_preempt_state
.rda
, cpu
);
155 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
156 t
->rcu_read_unlock_special
|= RCU_READ_UNLOCK_BLOCKED
;
157 t
->rcu_blocked_node
= rnp
;
160 * If this CPU has already checked in, then this task
161 * will hold up the next grace period rather than the
162 * current grace period. Queue the task accordingly.
163 * If the task is queued for the current grace period
164 * (i.e., this CPU has not yet passed through a quiescent
165 * state for the current grace period), then as long
166 * as that task remains queued, the current grace period
167 * cannot end. Note that there is some uncertainty as
168 * to exactly when the current grace period started.
169 * We take a conservative approach, which can result
170 * in unnecessarily waiting on tasks that started very
171 * slightly after the current grace period began. C'est
174 * But first, note that the current CPU must still be
177 WARN_ON_ONCE((rdp
->grpmask
& rnp
->qsmaskinit
) == 0);
178 WARN_ON_ONCE(!list_empty(&t
->rcu_node_entry
));
179 if ((rnp
->qsmask
& rdp
->grpmask
) && rnp
->gp_tasks
!= NULL
) {
180 list_add(&t
->rcu_node_entry
, rnp
->gp_tasks
->prev
);
181 rnp
->gp_tasks
= &t
->rcu_node_entry
;
183 list_add(&t
->rcu_node_entry
, &rnp
->blkd_tasks
);
184 if (rnp
->qsmask
& rdp
->grpmask
)
185 rnp
->gp_tasks
= &t
->rcu_node_entry
;
187 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
191 * Either we were not in an RCU read-side critical section to
192 * begin with, or we have now recorded that critical section
193 * globally. Either way, we can now note a quiescent state
194 * for this CPU. Again, if we were in an RCU read-side critical
195 * section, and if that critical section was blocking the current
196 * grace period, then the fact that the task has been enqueued
197 * means that we continue to block the current grace period.
199 local_irq_save(flags
);
201 local_irq_restore(flags
);
205 * Tree-preemptable RCU implementation for rcu_read_lock().
206 * Just increment ->rcu_read_lock_nesting, shared state will be updated
209 void __rcu_read_lock(void)
211 current
->rcu_read_lock_nesting
++;
212 barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
214 EXPORT_SYMBOL_GPL(__rcu_read_lock
);
217 * Check for preempted RCU readers blocking the current grace period
218 * for the specified rcu_node structure. If the caller needs a reliable
219 * answer, it must hold the rcu_node's ->lock.
221 static int rcu_preempted_readers(struct rcu_node
*rnp
)
223 return rnp
->gp_tasks
!= NULL
;
227 * Record a quiescent state for all tasks that were previously queued
228 * on the specified rcu_node structure and that were blocking the current
229 * RCU grace period. The caller must hold the specified rnp->lock with
230 * irqs disabled, and this lock is released upon return, but irqs remain
233 static void rcu_report_unblock_qs_rnp(struct rcu_node
*rnp
, unsigned long flags
)
234 __releases(rnp
->lock
)
237 struct rcu_node
*rnp_p
;
239 if (rnp
->qsmask
!= 0 || rcu_preempted_readers(rnp
)) {
240 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
241 return; /* Still need more quiescent states! */
247 * Either there is only one rcu_node in the tree,
248 * or tasks were kicked up to root rcu_node due to
249 * CPUs going offline.
251 rcu_report_qs_rsp(&rcu_preempt_state
, flags
);
255 /* Report up the rest of the hierarchy. */
257 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
258 raw_spin_lock(&rnp_p
->lock
); /* irqs already disabled. */
259 rcu_report_qs_rnp(mask
, &rcu_preempt_state
, rnp_p
, flags
);
263 * Advance a ->blkd_tasks-list pointer to the next entry, instead
264 * returning NULL if at the end of the list.
266 static struct list_head
*rcu_next_node_entry(struct task_struct
*t
,
267 struct rcu_node
*rnp
)
269 struct list_head
*np
;
271 np
= t
->rcu_node_entry
.next
;
272 if (np
== &rnp
->blkd_tasks
)
278 * Handle special cases during rcu_read_unlock(), such as needing to
279 * notify RCU core processing or task having blocked during the RCU
280 * read-side critical section.
282 static void rcu_read_unlock_special(struct task_struct
*t
)
287 struct list_head
*np
;
288 struct rcu_node
*rnp
;
291 /* NMI handlers cannot block and cannot safely manipulate state. */
295 local_irq_save(flags
);
298 * If RCU core is waiting for this CPU to exit critical section,
299 * let it know that we have done so.
301 special
= t
->rcu_read_unlock_special
;
302 if (special
& RCU_READ_UNLOCK_NEED_QS
) {
303 rcu_preempt_qs(smp_processor_id());
306 /* Hardware IRQ handlers cannot block. */
308 local_irq_restore(flags
);
312 /* Clean up if blocked during RCU read-side critical section. */
313 if (special
& RCU_READ_UNLOCK_BLOCKED
) {
314 t
->rcu_read_unlock_special
&= ~RCU_READ_UNLOCK_BLOCKED
;
317 * Remove this task from the list it blocked on. The
318 * task can migrate while we acquire the lock, but at
319 * most one time. So at most two passes through loop.
322 rnp
= t
->rcu_blocked_node
;
323 raw_spin_lock(&rnp
->lock
); /* irqs already disabled. */
324 if (rnp
== t
->rcu_blocked_node
)
326 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
328 empty
= !rcu_preempted_readers(rnp
);
329 empty_exp
= !rcu_preempted_readers_exp(rnp
);
330 smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
331 np
= rcu_next_node_entry(t
, rnp
);
332 list_del_init(&t
->rcu_node_entry
);
333 if (&t
->rcu_node_entry
== rnp
->gp_tasks
)
335 if (&t
->rcu_node_entry
== rnp
->exp_tasks
)
337 t
->rcu_blocked_node
= NULL
;
340 * If this was the last task on the current list, and if
341 * we aren't waiting on any CPUs, report the quiescent state.
342 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
345 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
347 rcu_report_unblock_qs_rnp(rnp
, flags
);
350 * If this was the last task on the expedited lists,
351 * then we need to report up the rcu_node hierarchy.
353 if (!empty_exp
&& !rcu_preempted_readers_exp(rnp
))
354 rcu_report_exp_rnp(&rcu_preempt_state
, rnp
);
356 local_irq_restore(flags
);
361 * Tree-preemptable RCU implementation for rcu_read_unlock().
362 * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
363 * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
364 * invoke rcu_read_unlock_special() to clean up after a context switch
365 * in an RCU read-side critical section and other special cases.
367 void __rcu_read_unlock(void)
369 struct task_struct
*t
= current
;
371 barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
372 --t
->rcu_read_lock_nesting
;
373 barrier(); /* decrement before load of ->rcu_read_unlock_special */
374 if (t
->rcu_read_lock_nesting
== 0 &&
375 unlikely(ACCESS_ONCE(t
->rcu_read_unlock_special
)))
376 rcu_read_unlock_special(t
);
377 #ifdef CONFIG_PROVE_LOCKING
378 WARN_ON_ONCE(ACCESS_ONCE(t
->rcu_read_lock_nesting
) < 0);
379 #endif /* #ifdef CONFIG_PROVE_LOCKING */
381 EXPORT_SYMBOL_GPL(__rcu_read_unlock
);
383 #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
386 * Dump detailed information for all tasks blocking the current RCU
387 * grace period on the specified rcu_node structure.
389 static void rcu_print_detail_task_stall_rnp(struct rcu_node
*rnp
)
392 struct task_struct
*t
;
394 if (!rcu_preempted_readers(rnp
))
396 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
397 t
= list_entry(rnp
->gp_tasks
,
398 struct task_struct
, rcu_node_entry
);
399 list_for_each_entry_continue(t
, &rnp
->blkd_tasks
, rcu_node_entry
)
401 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
405 * Dump detailed information for all tasks blocking the current RCU
408 static void rcu_print_detail_task_stall(struct rcu_state
*rsp
)
410 struct rcu_node
*rnp
= rcu_get_root(rsp
);
412 rcu_print_detail_task_stall_rnp(rnp
);
413 rcu_for_each_leaf_node(rsp
, rnp
)
414 rcu_print_detail_task_stall_rnp(rnp
);
417 #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
419 static void rcu_print_detail_task_stall(struct rcu_state
*rsp
)
423 #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
426 * Scan the current list of tasks blocked within RCU read-side critical
427 * sections, printing out the tid of each.
429 static void rcu_print_task_stall(struct rcu_node
*rnp
)
431 struct task_struct
*t
;
433 if (!rcu_preempted_readers(rnp
))
435 t
= list_entry(rnp
->gp_tasks
,
436 struct task_struct
, rcu_node_entry
);
437 list_for_each_entry_continue(t
, &rnp
->blkd_tasks
, rcu_node_entry
)
438 printk(" P%d", t
->pid
);
442 * Suppress preemptible RCU's CPU stall warnings by pushing the
443 * time of the next stall-warning message comfortably far into the
446 static void rcu_preempt_stall_reset(void)
448 rcu_preempt_state
.jiffies_stall
= jiffies
+ ULONG_MAX
/ 2;
452 * Check that the list of blocked tasks for the newly completed grace
453 * period is in fact empty. It is a serious bug to complete a grace
454 * period that still has RCU readers blocked! This function must be
455 * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
456 * must be held by the caller.
458 * Also, if there are blocked tasks on the list, they automatically
459 * block the newly created grace period, so set up ->gp_tasks accordingly.
461 static void rcu_preempt_check_blocked_tasks(struct rcu_node
*rnp
)
463 WARN_ON_ONCE(rcu_preempted_readers(rnp
));
464 if (!list_empty(&rnp
->blkd_tasks
))
465 rnp
->gp_tasks
= rnp
->blkd_tasks
.next
;
466 WARN_ON_ONCE(rnp
->qsmask
);
469 #ifdef CONFIG_HOTPLUG_CPU
472 * Handle tasklist migration for case in which all CPUs covered by the
473 * specified rcu_node have gone offline. Move them up to the root
474 * rcu_node. The reason for not just moving them to the immediate
475 * parent is to remove the need for rcu_read_unlock_special() to
476 * make more than two attempts to acquire the target rcu_node's lock.
477 * Returns true if there were tasks blocking the current RCU grace
480 * Returns 1 if there was previously a task blocking the current grace
481 * period on the specified rcu_node structure.
483 * The caller must hold rnp->lock with irqs disabled.
485 static int rcu_preempt_offline_tasks(struct rcu_state
*rsp
,
486 struct rcu_node
*rnp
,
487 struct rcu_data
*rdp
)
489 struct list_head
*lp
;
490 struct list_head
*lp_root
;
492 struct rcu_node
*rnp_root
= rcu_get_root(rsp
);
493 struct task_struct
*t
;
495 if (rnp
== rnp_root
) {
496 WARN_ONCE(1, "Last CPU thought to be offlined?");
497 return 0; /* Shouldn't happen: at least one CPU online. */
500 /* If we are on an internal node, complain bitterly. */
501 WARN_ON_ONCE(rnp
!= rdp
->mynode
);
504 * Move tasks up to root rcu_node. Don't try to get fancy for
505 * this corner-case operation -- just put this node's tasks
506 * at the head of the root node's list, and update the root node's
507 * ->gp_tasks and ->exp_tasks pointers to those of this node's,
508 * if non-NULL. This might result in waiting for more tasks than
509 * absolutely necessary, but this is a good performance/complexity
512 if (rcu_preempted_readers(rnp
))
513 retval
|= RCU_OFL_TASKS_NORM_GP
;
514 if (rcu_preempted_readers_exp(rnp
))
515 retval
|= RCU_OFL_TASKS_EXP_GP
;
516 lp
= &rnp
->blkd_tasks
;
517 lp_root
= &rnp_root
->blkd_tasks
;
518 while (!list_empty(lp
)) {
519 t
= list_entry(lp
->next
, typeof(*t
), rcu_node_entry
);
520 raw_spin_lock(&rnp_root
->lock
); /* irqs already disabled */
521 list_del(&t
->rcu_node_entry
);
522 t
->rcu_blocked_node
= rnp_root
;
523 list_add(&t
->rcu_node_entry
, lp_root
);
524 if (&t
->rcu_node_entry
== rnp
->gp_tasks
)
525 rnp_root
->gp_tasks
= rnp
->gp_tasks
;
526 if (&t
->rcu_node_entry
== rnp
->exp_tasks
)
527 rnp_root
->exp_tasks
= rnp
->exp_tasks
;
528 raw_spin_unlock(&rnp_root
->lock
); /* irqs still disabled */
530 rnp
->gp_tasks
= NULL
;
531 rnp
->exp_tasks
= NULL
;
536 * Do CPU-offline processing for preemptable RCU.
538 static void rcu_preempt_offline_cpu(int cpu
)
540 __rcu_offline_cpu(cpu
, &rcu_preempt_state
);
543 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
546 * Check for a quiescent state from the current CPU. When a task blocks,
547 * the task is recorded in the corresponding CPU's rcu_node structure,
548 * which is checked elsewhere.
550 * Caller must disable hard irqs.
552 static void rcu_preempt_check_callbacks(int cpu
)
554 struct task_struct
*t
= current
;
556 if (t
->rcu_read_lock_nesting
== 0) {
560 if (per_cpu(rcu_preempt_data
, cpu
).qs_pending
)
561 t
->rcu_read_unlock_special
|= RCU_READ_UNLOCK_NEED_QS
;
565 * Process callbacks for preemptable RCU.
567 static void rcu_preempt_process_callbacks(void)
569 __rcu_process_callbacks(&rcu_preempt_state
,
570 &__get_cpu_var(rcu_preempt_data
));
574 * Queue a preemptable-RCU callback for invocation after a grace period.
576 void call_rcu(struct rcu_head
*head
, void (*func
)(struct rcu_head
*rcu
))
578 __call_rcu(head
, func
, &rcu_preempt_state
);
580 EXPORT_SYMBOL_GPL(call_rcu
);
583 * synchronize_rcu - wait until a grace period has elapsed.
585 * Control will return to the caller some time after a full grace
586 * period has elapsed, in other words after all currently executing RCU
587 * read-side critical sections have completed. Note, however, that
588 * upon return from synchronize_rcu(), the caller might well be executing
589 * concurrently with new RCU read-side critical sections that began while
590 * synchronize_rcu() was waiting. RCU read-side critical sections are
591 * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
593 void synchronize_rcu(void)
595 struct rcu_synchronize rcu
;
597 if (!rcu_scheduler_active
)
600 init_rcu_head_on_stack(&rcu
.head
);
601 init_completion(&rcu
.completion
);
602 /* Will wake me after RCU finished. */
603 call_rcu(&rcu
.head
, wakeme_after_rcu
);
605 wait_for_completion(&rcu
.completion
);
606 destroy_rcu_head_on_stack(&rcu
.head
);
608 EXPORT_SYMBOL_GPL(synchronize_rcu
);
610 static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq
);
611 static long sync_rcu_preempt_exp_count
;
612 static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex
);
615 * Return non-zero if there are any tasks in RCU read-side critical
616 * sections blocking the current preemptible-RCU expedited grace period.
617 * If there is no preemptible-RCU expedited grace period currently in
618 * progress, returns zero unconditionally.
620 static int rcu_preempted_readers_exp(struct rcu_node
*rnp
)
622 return rnp
->exp_tasks
!= NULL
;
626 * return non-zero if there is no RCU expedited grace period in progress
627 * for the specified rcu_node structure, in other words, if all CPUs and
628 * tasks covered by the specified rcu_node structure have done their bit
629 * for the current expedited grace period. Works only for preemptible
630 * RCU -- other RCU implementation use other means.
632 * Caller must hold sync_rcu_preempt_exp_mutex.
634 static int sync_rcu_preempt_exp_done(struct rcu_node
*rnp
)
636 return !rcu_preempted_readers_exp(rnp
) &&
637 ACCESS_ONCE(rnp
->expmask
) == 0;
641 * Report the exit from RCU read-side critical section for the last task
642 * that queued itself during or before the current expedited preemptible-RCU
643 * grace period. This event is reported either to the rcu_node structure on
644 * which the task was queued or to one of that rcu_node structure's ancestors,
645 * recursively up the tree. (Calm down, calm down, we do the recursion
648 * Caller must hold sync_rcu_preempt_exp_mutex.
650 static void rcu_report_exp_rnp(struct rcu_state
*rsp
, struct rcu_node
*rnp
)
655 raw_spin_lock_irqsave(&rnp
->lock
, flags
);
657 if (!sync_rcu_preempt_exp_done(rnp
))
659 if (rnp
->parent
== NULL
) {
660 wake_up(&sync_rcu_preempt_exp_wq
);
664 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled */
666 raw_spin_lock(&rnp
->lock
); /* irqs already disabled */
667 rnp
->expmask
&= ~mask
;
669 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
673 * Snapshot the tasks blocking the newly started preemptible-RCU expedited
674 * grace period for the specified rcu_node structure. If there are no such
675 * tasks, report it up the rcu_node hierarchy.
677 * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
680 sync_rcu_preempt_exp_init(struct rcu_state
*rsp
, struct rcu_node
*rnp
)
684 raw_spin_lock(&rnp
->lock
); /* irqs already disabled */
685 if (!list_empty(&rnp
->blkd_tasks
)) {
686 rnp
->exp_tasks
= rnp
->blkd_tasks
.next
;
689 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled */
691 rcu_report_exp_rnp(rsp
, rnp
);
695 * Wait for an rcu-preempt grace period, but expedite it. The basic idea
696 * is to invoke synchronize_sched_expedited() to push all the tasks to
697 * the ->blkd_tasks lists and wait for this list to drain.
699 void synchronize_rcu_expedited(void)
702 struct rcu_node
*rnp
;
703 struct rcu_state
*rsp
= &rcu_preempt_state
;
707 smp_mb(); /* Caller's modifications seen first by other CPUs. */
708 snap
= ACCESS_ONCE(sync_rcu_preempt_exp_count
) + 1;
709 smp_mb(); /* Above access cannot bleed into critical section. */
712 * Acquire lock, falling back to synchronize_rcu() if too many
713 * lock-acquisition failures. Of course, if someone does the
714 * expedited grace period for us, just leave.
716 while (!mutex_trylock(&sync_rcu_preempt_exp_mutex
)) {
718 udelay(trycount
* num_online_cpus());
723 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count
) - snap
) > 0)
724 goto mb_ret
; /* Others did our work for us. */
726 if ((ACCESS_ONCE(sync_rcu_preempt_exp_count
) - snap
) > 0)
727 goto unlock_mb_ret
; /* Others did our work for us. */
729 /* force all RCU readers onto ->blkd_tasks lists. */
730 synchronize_sched_expedited();
732 raw_spin_lock_irqsave(&rsp
->onofflock
, flags
);
734 /* Initialize ->expmask for all non-leaf rcu_node structures. */
735 rcu_for_each_nonleaf_node_breadth_first(rsp
, rnp
) {
736 raw_spin_lock(&rnp
->lock
); /* irqs already disabled. */
737 rnp
->expmask
= rnp
->qsmaskinit
;
738 raw_spin_unlock(&rnp
->lock
); /* irqs remain disabled. */
741 /* Snapshot current state of ->blkd_tasks lists. */
742 rcu_for_each_leaf_node(rsp
, rnp
)
743 sync_rcu_preempt_exp_init(rsp
, rnp
);
744 if (NUM_RCU_NODES
> 1)
745 sync_rcu_preempt_exp_init(rsp
, rcu_get_root(rsp
));
747 raw_spin_unlock_irqrestore(&rsp
->onofflock
, flags
);
749 /* Wait for snapshotted ->blkd_tasks lists to drain. */
750 rnp
= rcu_get_root(rsp
);
751 wait_event(sync_rcu_preempt_exp_wq
,
752 sync_rcu_preempt_exp_done(rnp
));
754 /* Clean up and exit. */
755 smp_mb(); /* ensure expedited GP seen before counter increment. */
756 ACCESS_ONCE(sync_rcu_preempt_exp_count
)++;
758 mutex_unlock(&sync_rcu_preempt_exp_mutex
);
760 smp_mb(); /* ensure subsequent action seen after grace period. */
762 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited
);
765 * Check to see if there is any immediate preemptable-RCU-related work
768 static int rcu_preempt_pending(int cpu
)
770 return __rcu_pending(&rcu_preempt_state
,
771 &per_cpu(rcu_preempt_data
, cpu
));
775 * Does preemptable RCU need the CPU to stay out of dynticks mode?
777 static int rcu_preempt_needs_cpu(int cpu
)
779 return !!per_cpu(rcu_preempt_data
, cpu
).nxtlist
;
783 * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
785 void rcu_barrier(void)
787 _rcu_barrier(&rcu_preempt_state
, call_rcu
);
789 EXPORT_SYMBOL_GPL(rcu_barrier
);
792 * Initialize preemptable RCU's per-CPU data.
794 static void __cpuinit
rcu_preempt_init_percpu_data(int cpu
)
796 rcu_init_percpu_data(cpu
, &rcu_preempt_state
, 1);
800 * Move preemptable RCU's callbacks from dying CPU to other online CPU.
802 static void rcu_preempt_send_cbs_to_online(void)
804 rcu_send_cbs_to_online(&rcu_preempt_state
);
808 * Initialize preemptable RCU's state structures.
810 static void __init
__rcu_init_preempt(void)
812 rcu_init_one(&rcu_preempt_state
, &rcu_preempt_data
);
816 * Check for a task exiting while in a preemptable-RCU read-side
817 * critical section, clean up if so. No need to issue warnings,
818 * as debug_check_no_locks_held() already does this if lockdep
823 struct task_struct
*t
= current
;
825 if (t
->rcu_read_lock_nesting
== 0)
827 t
->rcu_read_lock_nesting
= 1;
831 #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
834 * Tell them what RCU they are running.
836 static void __init
rcu_bootup_announce(void)
838 printk(KERN_INFO
"Hierarchical RCU implementation.\n");
839 rcu_bootup_announce_oddness();
843 * Return the number of RCU batches processed thus far for debug & stats.
845 long rcu_batches_completed(void)
847 return rcu_batches_completed_sched();
849 EXPORT_SYMBOL_GPL(rcu_batches_completed
);
852 * Force a quiescent state for RCU, which, because there is no preemptible
853 * RCU, becomes the same as rcu-sched.
855 void rcu_force_quiescent_state(void)
857 rcu_sched_force_quiescent_state();
859 EXPORT_SYMBOL_GPL(rcu_force_quiescent_state
);
862 * Because preemptable RCU does not exist, we never have to check for
863 * CPUs being in quiescent states.
865 static void rcu_preempt_note_context_switch(int cpu
)
870 * Because preemptable RCU does not exist, there are never any preempted
873 static int rcu_preempted_readers(struct rcu_node
*rnp
)
878 #ifdef CONFIG_HOTPLUG_CPU
880 /* Because preemptible RCU does not exist, no quieting of tasks. */
881 static void rcu_report_unblock_qs_rnp(struct rcu_node
*rnp
, unsigned long flags
)
883 raw_spin_unlock_irqrestore(&rnp
->lock
, flags
);
886 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
889 * Because preemptable RCU does not exist, we never have to check for
890 * tasks blocked within RCU read-side critical sections.
892 static void rcu_print_detail_task_stall(struct rcu_state
*rsp
)
897 * Because preemptable RCU does not exist, we never have to check for
898 * tasks blocked within RCU read-side critical sections.
900 static void rcu_print_task_stall(struct rcu_node
*rnp
)
905 * Because preemptible RCU does not exist, there is no need to suppress
906 * its CPU stall warnings.
908 static void rcu_preempt_stall_reset(void)
913 * Because there is no preemptable RCU, there can be no readers blocked,
914 * so there is no need to check for blocked tasks. So check only for
915 * bogus qsmask values.
917 static void rcu_preempt_check_blocked_tasks(struct rcu_node
*rnp
)
919 WARN_ON_ONCE(rnp
->qsmask
);
922 #ifdef CONFIG_HOTPLUG_CPU
925 * Because preemptable RCU does not exist, it never needs to migrate
926 * tasks that were blocked within RCU read-side critical sections, and
927 * such non-existent tasks cannot possibly have been blocking the current
930 static int rcu_preempt_offline_tasks(struct rcu_state
*rsp
,
931 struct rcu_node
*rnp
,
932 struct rcu_data
*rdp
)
938 * Because preemptable RCU does not exist, it never needs CPU-offline
941 static void rcu_preempt_offline_cpu(int cpu
)
945 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
948 * Because preemptable RCU does not exist, it never has any callbacks
951 static void rcu_preempt_check_callbacks(int cpu
)
956 * Because preemptable RCU does not exist, it never has any callbacks
959 static void rcu_preempt_process_callbacks(void)
964 * Wait for an rcu-preempt grace period, but make it happen quickly.
965 * But because preemptable RCU does not exist, map to rcu-sched.
967 void synchronize_rcu_expedited(void)
969 synchronize_sched_expedited();
971 EXPORT_SYMBOL_GPL(synchronize_rcu_expedited
);
973 #ifdef CONFIG_HOTPLUG_CPU
976 * Because preemptable RCU does not exist, there is never any need to
977 * report on tasks preempted in RCU read-side critical sections during
978 * expedited RCU grace periods.
980 static void rcu_report_exp_rnp(struct rcu_state
*rsp
, struct rcu_node
*rnp
)
985 #endif /* #ifdef CONFIG_HOTPLUG_CPU */
988 * Because preemptable RCU does not exist, it never has any work to do.
990 static int rcu_preempt_pending(int cpu
)
996 * Because preemptable RCU does not exist, it never needs any CPU.
998 static int rcu_preempt_needs_cpu(int cpu
)
1004 * Because preemptable RCU does not exist, rcu_barrier() is just
1005 * another name for rcu_barrier_sched().
1007 void rcu_barrier(void)
1009 rcu_barrier_sched();
1011 EXPORT_SYMBOL_GPL(rcu_barrier
);
1014 * Because preemptable RCU does not exist, there is no per-CPU
1015 * data to initialize.
1017 static void __cpuinit
rcu_preempt_init_percpu_data(int cpu
)
1022 * Because there is no preemptable RCU, there are no callbacks to move.
1024 static void rcu_preempt_send_cbs_to_online(void)
1029 * Because preemptable RCU does not exist, it need not be initialized.
1031 static void __init
__rcu_init_preempt(void)
1035 #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
1039 void synchronize_sched_expedited(void)
1043 EXPORT_SYMBOL_GPL(synchronize_sched_expedited
);
1045 #else /* #ifndef CONFIG_SMP */
1047 static atomic_t sync_sched_expedited_started
= ATOMIC_INIT(0);
1048 static atomic_t sync_sched_expedited_done
= ATOMIC_INIT(0);
1050 static int synchronize_sched_expedited_cpu_stop(void *data
)
1053 * There must be a full memory barrier on each affected CPU
1054 * between the time that try_stop_cpus() is called and the
1055 * time that it returns.
1057 * In the current initial implementation of cpu_stop, the
1058 * above condition is already met when the control reaches
1059 * this point and the following smp_mb() is not strictly
1060 * necessary. Do smp_mb() anyway for documentation and
1061 * robustness against future implementation changes.
1063 smp_mb(); /* See above comment block. */
1068 * Wait for an rcu-sched grace period to elapse, but use "big hammer"
1069 * approach to force grace period to end quickly. This consumes
1070 * significant time on all CPUs, and is thus not recommended for
1071 * any sort of common-case code.
1073 * Note that it is illegal to call this function while holding any
1074 * lock that is acquired by a CPU-hotplug notifier. Failing to
1075 * observe this restriction will result in deadlock.
1077 * This implementation can be thought of as an application of ticket
1078 * locking to RCU, with sync_sched_expedited_started and
1079 * sync_sched_expedited_done taking on the roles of the halves
1080 * of the ticket-lock word. Each task atomically increments
1081 * sync_sched_expedited_started upon entry, snapshotting the old value,
1082 * then attempts to stop all the CPUs. If this succeeds, then each
1083 * CPU will have executed a context switch, resulting in an RCU-sched
1084 * grace period. We are then done, so we use atomic_cmpxchg() to
1085 * update sync_sched_expedited_done to match our snapshot -- but
1086 * only if someone else has not already advanced past our snapshot.
1088 * On the other hand, if try_stop_cpus() fails, we check the value
1089 * of sync_sched_expedited_done. If it has advanced past our
1090 * initial snapshot, then someone else must have forced a grace period
1091 * some time after we took our snapshot. In this case, our work is
1092 * done for us, and we can simply return. Otherwise, we try again,
1093 * but keep our initial snapshot for purposes of checking for someone
1094 * doing our work for us.
1096 * If we fail too many times in a row, we fall back to synchronize_sched().
1098 void synchronize_sched_expedited(void)
1100 int firstsnap
, s
, snap
, trycount
= 0;
1102 /* Note that atomic_inc_return() implies full memory barrier. */
1103 firstsnap
= snap
= atomic_inc_return(&sync_sched_expedited_started
);
1107 * Each pass through the following loop attempts to force a
1108 * context switch on each CPU.
1110 while (try_stop_cpus(cpu_online_mask
,
1111 synchronize_sched_expedited_cpu_stop
,
1115 /* No joy, try again later. Or just synchronize_sched(). */
1116 if (trycount
++ < 10)
1117 udelay(trycount
* num_online_cpus());
1119 synchronize_sched();
1123 /* Check to see if someone else did our work for us. */
1124 s
= atomic_read(&sync_sched_expedited_done
);
1125 if (UINT_CMP_GE((unsigned)s
, (unsigned)firstsnap
)) {
1126 smp_mb(); /* ensure test happens before caller kfree */
1131 * Refetching sync_sched_expedited_started allows later
1132 * callers to piggyback on our grace period. We subtract
1133 * 1 to get the same token that the last incrementer got.
1134 * We retry after they started, so our grace period works
1135 * for them, and they started after our first try, so their
1136 * grace period works for us.
1139 snap
= atomic_read(&sync_sched_expedited_started
) - 1;
1140 smp_mb(); /* ensure read is before try_stop_cpus(). */
1144 * Everyone up to our most recent fetch is covered by our grace
1145 * period. Update the counter, but only if our work is still
1146 * relevant -- which it won't be if someone who started later
1147 * than we did beat us to the punch.
1150 s
= atomic_read(&sync_sched_expedited_done
);
1151 if (UINT_CMP_GE((unsigned)s
, (unsigned)snap
)) {
1152 smp_mb(); /* ensure test happens before caller kfree */
1155 } while (atomic_cmpxchg(&sync_sched_expedited_done
, s
, snap
) != s
);
1159 EXPORT_SYMBOL_GPL(synchronize_sched_expedited
);
1161 #endif /* #else #ifndef CONFIG_SMP */
1163 #if !defined(CONFIG_RCU_FAST_NO_HZ)
1166 * Check to see if any future RCU-related work will need to be done
1167 * by the current CPU, even if none need be done immediately, returning
1168 * 1 if so. This function is part of the RCU implementation; it is -not-
1169 * an exported member of the RCU API.
1171 * Because we have preemptible RCU, just check whether this CPU needs
1172 * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
1173 * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
1175 int rcu_needs_cpu(int cpu
)
1177 return rcu_needs_cpu_quick_check(cpu
);
1181 * Check to see if we need to continue a callback-flush operations to
1182 * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
1183 * entry is not configured, so we never do need to.
1185 static void rcu_needs_cpu_flush(void)
1189 #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
1191 #define RCU_NEEDS_CPU_FLUSHES 5
1192 static DEFINE_PER_CPU(int, rcu_dyntick_drain
);
1193 static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff
);
1196 * Check to see if any future RCU-related work will need to be done
1197 * by the current CPU, even if none need be done immediately, returning
1198 * 1 if so. This function is part of the RCU implementation; it is -not-
1199 * an exported member of the RCU API.
1201 * Because we are not supporting preemptible RCU, attempt to accelerate
1202 * any current grace periods so that RCU no longer needs this CPU, but
1203 * only if all other CPUs are already in dynticks-idle mode. This will
1204 * allow the CPU cores to be powered down immediately, as opposed to after
1205 * waiting many milliseconds for grace periods to elapse.
1207 * Because it is not legal to invoke rcu_process_callbacks() with irqs
1208 * disabled, we do one pass of force_quiescent_state(), then do a
1209 * raise_softirq() to cause rcu_process_callbacks() to be invoked later.
1210 * The per-cpu rcu_dyntick_drain variable controls the sequencing.
1212 int rcu_needs_cpu(int cpu
)
1218 /* Check for being in the holdoff period. */
1219 if (per_cpu(rcu_dyntick_holdoff
, cpu
) == jiffies
)
1220 return rcu_needs_cpu_quick_check(cpu
);
1222 /* Don't bother unless we are the last non-dyntick-idle CPU. */
1223 for_each_online_cpu(thatcpu
) {
1226 snap
= atomic_add_return(0, &per_cpu(rcu_dynticks
,
1228 smp_mb(); /* Order sampling of snap with end of grace period. */
1229 if ((snap
& 0x1) != 0) {
1230 per_cpu(rcu_dyntick_drain
, cpu
) = 0;
1231 per_cpu(rcu_dyntick_holdoff
, cpu
) = jiffies
- 1;
1232 return rcu_needs_cpu_quick_check(cpu
);
1236 /* Check and update the rcu_dyntick_drain sequencing. */
1237 if (per_cpu(rcu_dyntick_drain
, cpu
) <= 0) {
1238 /* First time through, initialize the counter. */
1239 per_cpu(rcu_dyntick_drain
, cpu
) = RCU_NEEDS_CPU_FLUSHES
;
1240 } else if (--per_cpu(rcu_dyntick_drain
, cpu
) <= 0) {
1241 /* We have hit the limit, so time to give up. */
1242 per_cpu(rcu_dyntick_holdoff
, cpu
) = jiffies
;
1243 return rcu_needs_cpu_quick_check(cpu
);
1246 /* Do one step pushing remaining RCU callbacks through. */
1247 if (per_cpu(rcu_sched_data
, cpu
).nxtlist
) {
1249 force_quiescent_state(&rcu_sched_state
, 0);
1250 c
= c
|| per_cpu(rcu_sched_data
, cpu
).nxtlist
;
1252 if (per_cpu(rcu_bh_data
, cpu
).nxtlist
) {
1254 force_quiescent_state(&rcu_bh_state
, 0);
1255 c
= c
|| per_cpu(rcu_bh_data
, cpu
).nxtlist
;
1258 /* If RCU callbacks are still pending, RCU still needs this CPU. */
1260 raise_softirq(RCU_SOFTIRQ
);
1265 * Check to see if we need to continue a callback-flush operations to
1266 * allow the last CPU to enter dyntick-idle mode.
1268 static void rcu_needs_cpu_flush(void)
1270 int cpu
= smp_processor_id();
1271 unsigned long flags
;
1273 if (per_cpu(rcu_dyntick_drain
, cpu
) <= 0)
1275 local_irq_save(flags
);
1276 (void)rcu_needs_cpu(cpu
);
1277 local_irq_restore(flags
);
1280 #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */