2 * Read-Copy Update mechanism for mutual exclusion
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright IBM Corporation, 2001
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
23 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
24 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
26 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
27 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
29 * For detailed explanation of Read-Copy Update mechanism see -
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/spinlock.h>
37 #include <linux/smp.h>
38 #include <linux/rcupdate.h>
39 #include <linux/interrupt.h>
40 #include <linux/sched.h>
41 #include <asm/atomic.h>
42 #include <linux/bitops.h>
43 #include <linux/module.h>
44 #include <linux/completion.h>
45 #include <linux/moduleparam.h>
46 #include <linux/percpu.h>
47 #include <linux/notifier.h>
48 #include <linux/cpu.h>
49 #include <linux/mutex.h>
51 #ifdef CONFIG_DEBUG_LOCK_ALLOC
52 static struct lock_class_key rcu_lock_key;
53 struct lockdep_map rcu_lock_map =
54 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
55 EXPORT_SYMBOL_GPL(rcu_lock_map);
59 /* Definition for rcupdate control block. */
60 static struct rcu_ctrlblk rcu_ctrlblk = {
64 .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
65 .cpumask = CPU_MASK_NONE,
67 static struct rcu_ctrlblk rcu_bh_ctrlblk = {
71 .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
72 .cpumask = CPU_MASK_NONE,
75 DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
76 DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
78 static int blimit = 10;
79 static int qhimark = 10000;
80 static int qlowmark = 100;
83 static void force_quiescent_state(struct rcu_data *rdp,
84 struct rcu_ctrlblk *rcp)
89 if (unlikely(!rcp->signaled)) {
92 * Don't send IPI to itself. With irqs disabled,
93 * rdp->cpu is the current cpu.
95 * cpu_online_map is updated by the _cpu_down()
96 * using stop_machine_run(). Since we're in irqs disabled
97 * section, stop_machine_run() is not exectuting, hence
98 * the cpu_online_map is stable.
100 * However, a cpu might have been offlined _just_ before
101 * we disabled irqs while entering here.
102 * And rcu subsystem might not yet have handled the CPU_DEAD
103 * notification, leading to the offlined cpu's bit
104 * being set in the rcp->cpumask.
106 * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent
107 * sending smp_reschedule() to an offlined CPU.
109 cpus_and(cpumask, rcp->cpumask, cpu_online_map);
110 cpu_clear(rdp->cpu, cpumask);
111 for_each_cpu_mask(cpu, cpumask)
112 smp_send_reschedule(cpu);
116 static inline void force_quiescent_state(struct rcu_data *rdp,
117 struct rcu_ctrlblk *rcp)
124 * call_rcu - Queue an RCU callback for invocation after a grace period.
125 * @head: structure to be used for queueing the RCU updates.
126 * @func: actual update function to be invoked after the grace period
128 * The update function will be invoked some time after a full grace
129 * period elapses, in other words after all currently executing RCU
130 * read-side critical sections have completed. RCU read-side critical
131 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
134 void call_rcu(struct rcu_head *head,
135 void (*func)(struct rcu_head *rcu))
138 struct rcu_data *rdp;
142 local_irq_save(flags);
143 rdp = &__get_cpu_var(rcu_data);
144 *rdp->nxttail = head;
145 rdp->nxttail = &head->next;
146 if (unlikely(++rdp->qlen > qhimark)) {
147 rdp->blimit = INT_MAX;
148 force_quiescent_state(rdp, &rcu_ctrlblk);
150 local_irq_restore(flags);
152 EXPORT_SYMBOL_GPL(call_rcu);
155 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
156 * @head: structure to be used for queueing the RCU updates.
157 * @func: actual update function to be invoked after the grace period
159 * The update function will be invoked some time after a full grace
160 * period elapses, in other words after all currently executing RCU
161 * read-side critical sections have completed. call_rcu_bh() assumes
162 * that the read-side critical sections end on completion of a softirq
163 * handler. This means that read-side critical sections in process
164 * context must not be interrupted by softirqs. This interface is to be
165 * used when most of the read-side critical sections are in softirq context.
166 * RCU read-side critical sections are delimited by rcu_read_lock() and
167 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
168 * and rcu_read_unlock_bh(), if in process context. These may be nested.
170 void call_rcu_bh(struct rcu_head *head,
171 void (*func)(struct rcu_head *rcu))
174 struct rcu_data *rdp;
178 local_irq_save(flags);
179 rdp = &__get_cpu_var(rcu_bh_data);
180 *rdp->nxttail = head;
181 rdp->nxttail = &head->next;
183 if (unlikely(++rdp->qlen > qhimark)) {
184 rdp->blimit = INT_MAX;
185 force_quiescent_state(rdp, &rcu_bh_ctrlblk);
188 local_irq_restore(flags);
190 EXPORT_SYMBOL_GPL(call_rcu_bh);
193 * Return the number of RCU batches processed thus far. Useful
194 * for debug and statistics.
196 long rcu_batches_completed(void)
198 return rcu_ctrlblk.completed;
200 EXPORT_SYMBOL_GPL(rcu_batches_completed);
203 * Return the number of RCU batches processed thus far. Useful
204 * for debug and statistics.
206 long rcu_batches_completed_bh(void)
208 return rcu_bh_ctrlblk.completed;
210 EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
212 /* Raises the softirq for processing rcu_callbacks. */
213 static inline void raise_rcu_softirq(void)
215 raise_softirq(RCU_SOFTIRQ);
217 * The smp_mb() here is required to ensure that this cpu's
218 * __rcu_process_callbacks() reads the most recently updated
225 * Invoke the completed RCU callbacks. They are expected to be in
228 static void rcu_do_batch(struct rcu_data *rdp)
230 struct rcu_head *next, *list;
233 list = rdp->donelist;
239 if (++count >= rdp->blimit)
242 rdp->donelist = list;
247 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
248 rdp->blimit = blimit;
251 rdp->donetail = &rdp->donelist;
257 * Grace period handling:
258 * The grace period handling consists out of two steps:
259 * - A new grace period is started.
260 * This is done by rcu_start_batch. The start is not broadcasted to
261 * all cpus, they must pick this up by comparing rcp->cur with
262 * rdp->quiescbatch. All cpus are recorded in the
263 * rcu_ctrlblk.cpumask bitmap.
264 * - All cpus must go through a quiescent state.
265 * Since the start of the grace period is not broadcasted, at least two
266 * calls to rcu_check_quiescent_state are required:
267 * The first call just notices that a new grace period is running. The
268 * following calls check if there was a quiescent state since the beginning
269 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
270 * the bitmap is empty, then the grace period is completed.
271 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
272 * period (if necessary).
275 * Register a new batch of callbacks, and start it up if there is currently no
276 * active batch and the batch to be registered has not already occurred.
277 * Caller must hold rcu_ctrlblk.lock.
279 static void rcu_start_batch(struct rcu_ctrlblk *rcp)
281 if (rcp->cur != rcp->pending &&
282 rcp->completed == rcp->cur) {
286 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
287 * Barrier Otherwise it can cause tickless idle CPUs to be
288 * included in rcp->cpumask, which will extend graceperiods
292 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
299 * cpu went through a quiescent state since the beginning of the grace period.
300 * Clear it from the cpu mask and complete the grace period if it was the last
301 * cpu. Start another grace period if someone has further entries pending
303 static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
305 cpu_clear(cpu, rcp->cpumask);
306 if (cpus_empty(rcp->cpumask)) {
307 /* batch completed ! */
308 rcp->completed = rcp->cur;
309 rcu_start_batch(rcp);
314 * Check if the cpu has gone through a quiescent state (say context
315 * switch). If so and if it already hasn't done so in this RCU
316 * quiescent cycle, then indicate that it has done so.
318 static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
319 struct rcu_data *rdp)
321 if (rdp->quiescbatch != rcp->cur) {
322 /* start new grace period: */
324 rdp->passed_quiesc = 0;
325 rdp->quiescbatch = rcp->cur;
329 /* Grace period already completed for this cpu?
330 * qs_pending is checked instead of the actual bitmap to avoid
331 * cacheline trashing.
333 if (!rdp->qs_pending)
337 * Was there a quiescent state since the beginning of the grace
338 * period? If no, then exit and wait for the next call.
340 if (!rdp->passed_quiesc)
344 spin_lock(&rcp->lock);
346 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
347 * during cpu startup. Ignore the quiescent state.
349 if (likely(rdp->quiescbatch == rcp->cur))
350 cpu_quiet(rdp->cpu, rcp);
352 spin_unlock(&rcp->lock);
356 #ifdef CONFIG_HOTPLUG_CPU
358 /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
359 * locking requirements, the list it's pulling from has to belong to a cpu
360 * which is dead and hence not processing interrupts.
362 static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
363 struct rcu_head **tail)
366 *this_rdp->nxttail = list;
368 this_rdp->nxttail = tail;
372 static void __rcu_offline_cpu(struct rcu_data *this_rdp,
373 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
375 /* if the cpu going offline owns the grace period
376 * we can block indefinitely waiting for it, so flush
379 spin_lock_bh(&rcp->lock);
380 if (rcp->cur != rcp->completed)
381 cpu_quiet(rdp->cpu, rcp);
382 spin_unlock_bh(&rcp->lock);
383 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
384 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
385 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
388 this_rdp->qlen += rdp->qlen;
392 static void rcu_offline_cpu(int cpu)
394 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
395 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
397 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
398 &per_cpu(rcu_data, cpu));
399 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
400 &per_cpu(rcu_bh_data, cpu));
401 put_cpu_var(rcu_data);
402 put_cpu_var(rcu_bh_data);
407 static void rcu_offline_cpu(int cpu)
414 * This does the RCU processing work from softirq context.
416 static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
417 struct rcu_data *rdp)
419 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
420 *rdp->donetail = rdp->curlist;
421 rdp->donetail = rdp->curtail;
423 rdp->curtail = &rdp->curlist;
426 if (rdp->nxtlist && !rdp->curlist) {
428 rdp->curlist = rdp->nxtlist;
429 rdp->curtail = rdp->nxttail;
431 rdp->nxttail = &rdp->nxtlist;
435 * start the next batch of callbacks
438 /* determine batch number */
439 rdp->batch = rcp->cur + 1;
441 if (rcu_batch_after(rdp->batch, rcp->pending)) {
442 /* and start it/schedule start if it's a new batch */
443 spin_lock(&rcp->lock);
444 if (rcu_batch_after(rdp->batch, rcp->pending)) {
445 rcp->pending = rdp->batch;
446 rcu_start_batch(rcp);
448 spin_unlock(&rcp->lock);
452 rcu_check_quiescent_state(rcp, rdp);
457 static void rcu_process_callbacks(struct softirq_action *unused)
459 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
460 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
463 static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
465 /* This cpu has pending rcu entries and the grace period
466 * for them has completed.
468 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
471 /* This cpu has no pending entries, but there are new entries */
472 if (!rdp->curlist && rdp->nxtlist)
475 /* This cpu has finished callbacks to invoke */
479 /* The rcu core waits for a quiescent state from the cpu */
480 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
488 * Check to see if there is any immediate RCU-related work to be done
489 * by the current CPU, returning 1 if so. This function is part of the
490 * RCU implementation; it is -not- an exported member of the RCU API.
492 int rcu_pending(int cpu)
494 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
495 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
499 * Check to see if any future RCU-related work will need to be done
500 * by the current CPU, even if none need be done immediately, returning
501 * 1 if so. This function is part of the RCU implementation; it is -not-
502 * an exported member of the RCU API.
504 int rcu_needs_cpu(int cpu)
506 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
507 struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
509 return (!!rdp->curlist || !!rdp_bh->curlist || rcu_pending(cpu));
512 void rcu_check_callbacks(int cpu, int user)
515 (idle_cpu(cpu) && !in_softirq() &&
516 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
519 * Get here if this CPU took its interrupt from user
520 * mode or from the idle loop, and if this is not a
521 * nested interrupt. In this case, the CPU is in
522 * a quiescent state, so count it.
524 * Also do a memory barrier. This is needed to handle
525 * the case where writes from a preempt-disable section
526 * of code get reordered into schedule() by this CPU's
527 * write buffer. The memory barrier makes sure that
528 * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see
529 * by other CPUs to happen after any such write.
532 smp_mb(); /* See above block comment. */
534 rcu_bh_qsctr_inc(cpu);
536 } else if (!in_softirq()) {
539 * Get here if this CPU did not take its interrupt from
540 * softirq, in other words, if it is not interrupting
541 * a rcu_bh read-side critical section. This is an _bh
542 * critical section, so count it. The memory barrier
543 * is needed for the same reason as is the above one.
546 smp_mb(); /* See above block comment. */
547 rcu_bh_qsctr_inc(cpu);
552 static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
553 struct rcu_data *rdp)
555 memset(rdp, 0, sizeof(*rdp));
556 rdp->curtail = &rdp->curlist;
557 rdp->nxttail = &rdp->nxtlist;
558 rdp->donetail = &rdp->donelist;
559 rdp->quiescbatch = rcp->completed;
562 rdp->blimit = blimit;
565 static void __cpuinit rcu_online_cpu(int cpu)
567 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
568 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
570 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
571 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
572 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
575 static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
576 unsigned long action, void *hcpu)
578 long cpu = (long)hcpu;
582 case CPU_UP_PREPARE_FROZEN:
586 case CPU_DEAD_FROZEN:
587 rcu_offline_cpu(cpu);
595 static struct notifier_block __cpuinitdata rcu_nb = {
596 .notifier_call = rcu_cpu_notify,
600 * Initializes rcu mechanism. Assumed to be called early.
601 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
602 * Note that rcu_qsctr and friends are implicitly
603 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
605 void __init __rcu_init(void)
607 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
608 (void *)(long)smp_processor_id());
609 /* Register notifier for non-boot CPUs */
610 register_cpu_notifier(&rcu_nb);
613 module_param(blimit, int, 0);
614 module_param(qhimark, int, 0);
615 module_param(qlowmark, int, 0);