blob: 6f8696c502f429ebcbaf65e537fdf47e3127d300 [file] [log] [blame]
Paul E. McKenney01c1c662008-01-25 21:08:24 +01001/*
2 * Read-Copy Update mechanism for mutual exclusion
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Copyright IBM Corporation, 2001
19 *
20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
21 * Manfred Spraul <manfred@colorfullife.com>
22 *
23 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
24 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
25 * Papers:
26 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
27 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
28 *
29 * For detailed explanation of Read-Copy Update mechanism see -
30 * Documentation/RCU
31 *
32 */
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>
Paul E. McKenney01c1c662008-01-25 21:08:24 +010048#include <linux/cpu.h>
49#include <linux/mutex.h>
50
51#ifdef CONFIG_DEBUG_LOCK_ALLOC
52static struct lock_class_key rcu_lock_key;
53struct lockdep_map rcu_lock_map =
54 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
55EXPORT_SYMBOL_GPL(rcu_lock_map);
56#endif
57
58
59/* Definition for rcupdate control block. */
60static struct rcu_ctrlblk rcu_ctrlblk = {
61 .cur = -300,
62 .completed = -300,
63 .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
64 .cpumask = CPU_MASK_NONE,
65};
66static struct rcu_ctrlblk rcu_bh_ctrlblk = {
67 .cur = -300,
68 .completed = -300,
69 .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
70 .cpumask = CPU_MASK_NONE,
71};
72
73DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
74DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
75
76static int blimit = 10;
77static int qhimark = 10000;
78static int qlowmark = 100;
79
80#ifdef CONFIG_SMP
81static void force_quiescent_state(struct rcu_data *rdp,
82 struct rcu_ctrlblk *rcp)
83{
84 int cpu;
85 cpumask_t cpumask;
86 set_need_resched();
87 if (unlikely(!rcp->signaled)) {
88 rcp->signaled = 1;
89 /*
90 * Don't send IPI to itself. With irqs disabled,
91 * rdp->cpu is the current cpu.
Gautham R Shenoy8558f8f2008-06-27 10:17:38 +053092 *
93 * cpu_online_map is updated by the _cpu_down()
94 * using stop_machine_run(). Since we're in irqs disabled
95 * section, stop_machine_run() is not exectuting, hence
96 * the cpu_online_map is stable.
97 *
98 * However, a cpu might have been offlined _just_ before
99 * we disabled irqs while entering here.
100 * And rcu subsystem might not yet have handled the CPU_DEAD
101 * notification, leading to the offlined cpu's bit
102 * being set in the rcp->cpumask.
103 *
104 * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent
105 * sending smp_reschedule() to an offlined CPU.
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100106 */
Gautham R Shenoy8558f8f2008-06-27 10:17:38 +0530107 cpus_and(cpumask, rcp->cpumask, cpu_online_map);
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100108 cpu_clear(rdp->cpu, cpumask);
Mike Travis363ab6f2008-05-12 21:21:13 +0200109 for_each_cpu_mask_nr(cpu, cpumask)
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100110 smp_send_reschedule(cpu);
111 }
112}
113#else
114static inline void force_quiescent_state(struct rcu_data *rdp,
115 struct rcu_ctrlblk *rcp)
116{
117 set_need_resched();
118}
119#endif
120
121/**
122 * call_rcu - Queue an RCU callback for invocation after a grace period.
123 * @head: structure to be used for queueing the RCU updates.
124 * @func: actual update function to be invoked after the grace period
125 *
126 * The update function will be invoked some time after a full grace
127 * period elapses, in other words after all currently executing RCU
128 * read-side critical sections have completed. RCU read-side critical
129 * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
130 * and may be nested.
131 */
132void call_rcu(struct rcu_head *head,
133 void (*func)(struct rcu_head *rcu))
134{
135 unsigned long flags;
136 struct rcu_data *rdp;
137
138 head->func = func;
139 head->next = NULL;
140 local_irq_save(flags);
141 rdp = &__get_cpu_var(rcu_data);
142 *rdp->nxttail = head;
143 rdp->nxttail = &head->next;
144 if (unlikely(++rdp->qlen > qhimark)) {
145 rdp->blimit = INT_MAX;
146 force_quiescent_state(rdp, &rcu_ctrlblk);
147 }
148 local_irq_restore(flags);
149}
150EXPORT_SYMBOL_GPL(call_rcu);
151
152/**
153 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period.
154 * @head: structure to be used for queueing the RCU updates.
155 * @func: actual update function to be invoked after the grace period
156 *
157 * The update function will be invoked some time after a full grace
158 * period elapses, in other words after all currently executing RCU
159 * read-side critical sections have completed. call_rcu_bh() assumes
160 * that the read-side critical sections end on completion of a softirq
161 * handler. This means that read-side critical sections in process
162 * context must not be interrupted by softirqs. This interface is to be
163 * used when most of the read-side critical sections are in softirq context.
164 * RCU read-side critical sections are delimited by rcu_read_lock() and
165 * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh()
166 * and rcu_read_unlock_bh(), if in process context. These may be nested.
167 */
168void call_rcu_bh(struct rcu_head *head,
169 void (*func)(struct rcu_head *rcu))
170{
171 unsigned long flags;
172 struct rcu_data *rdp;
173
174 head->func = func;
175 head->next = NULL;
176 local_irq_save(flags);
177 rdp = &__get_cpu_var(rcu_bh_data);
178 *rdp->nxttail = head;
179 rdp->nxttail = &head->next;
180
181 if (unlikely(++rdp->qlen > qhimark)) {
182 rdp->blimit = INT_MAX;
183 force_quiescent_state(rdp, &rcu_bh_ctrlblk);
184 }
185
186 local_irq_restore(flags);
187}
188EXPORT_SYMBOL_GPL(call_rcu_bh);
189
190/*
191 * Return the number of RCU batches processed thus far. Useful
192 * for debug and statistics.
193 */
194long rcu_batches_completed(void)
195{
196 return rcu_ctrlblk.completed;
197}
198EXPORT_SYMBOL_GPL(rcu_batches_completed);
199
200/*
201 * Return the number of RCU batches processed thus far. Useful
202 * for debug and statistics.
203 */
204long rcu_batches_completed_bh(void)
205{
206 return rcu_bh_ctrlblk.completed;
207}
208EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
209
210/* Raises the softirq for processing rcu_callbacks. */
211static inline void raise_rcu_softirq(void)
212{
213 raise_softirq(RCU_SOFTIRQ);
214 /*
215 * The smp_mb() here is required to ensure that this cpu's
216 * __rcu_process_callbacks() reads the most recently updated
217 * value of rcu->cur.
218 */
219 smp_mb();
220}
221
222/*
223 * Invoke the completed RCU callbacks. They are expected to be in
224 * a per-cpu list.
225 */
226static void rcu_do_batch(struct rcu_data *rdp)
227{
228 struct rcu_head *next, *list;
229 int count = 0;
230
231 list = rdp->donelist;
232 while (list) {
233 next = list->next;
234 prefetch(next);
235 list->func(list);
236 list = next;
237 if (++count >= rdp->blimit)
238 break;
239 }
240 rdp->donelist = list;
241
242 local_irq_disable();
243 rdp->qlen -= count;
244 local_irq_enable();
245 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
246 rdp->blimit = blimit;
247
248 if (!rdp->donelist)
249 rdp->donetail = &rdp->donelist;
250 else
251 raise_rcu_softirq();
252}
253
254/*
255 * Grace period handling:
256 * The grace period handling consists out of two steps:
257 * - A new grace period is started.
258 * This is done by rcu_start_batch. The start is not broadcasted to
259 * all cpus, they must pick this up by comparing rcp->cur with
260 * rdp->quiescbatch. All cpus are recorded in the
261 * rcu_ctrlblk.cpumask bitmap.
262 * - All cpus must go through a quiescent state.
263 * Since the start of the grace period is not broadcasted, at least two
264 * calls to rcu_check_quiescent_state are required:
265 * The first call just notices that a new grace period is running. The
266 * following calls check if there was a quiescent state since the beginning
267 * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If
268 * the bitmap is empty, then the grace period is completed.
269 * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace
270 * period (if necessary).
271 */
272/*
273 * Register a new batch of callbacks, and start it up if there is currently no
274 * active batch and the batch to be registered has not already occurred.
275 * Caller must hold rcu_ctrlblk.lock.
276 */
277static void rcu_start_batch(struct rcu_ctrlblk *rcp)
278{
279 if (rcp->next_pending &&
280 rcp->completed == rcp->cur) {
281 rcp->next_pending = 0;
282 /*
283 * next_pending == 0 must be visible in
284 * __rcu_process_callbacks() before it can see new value of cur.
285 */
286 smp_wmb();
287 rcp->cur++;
288
289 /*
290 * Accessing nohz_cpu_mask before incrementing rcp->cur needs a
291 * Barrier Otherwise it can cause tickless idle CPUs to be
292 * included in rcp->cpumask, which will extend graceperiods
293 * unnecessarily.
294 */
295 smp_mb();
296 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
297
298 rcp->signaled = 0;
299 }
300}
301
302/*
303 * cpu went through a quiescent state since the beginning of the grace period.
304 * Clear it from the cpu mask and complete the grace period if it was the last
305 * cpu. Start another grace period if someone has further entries pending
306 */
307static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
308{
309 cpu_clear(cpu, rcp->cpumask);
310 if (cpus_empty(rcp->cpumask)) {
311 /* batch completed ! */
312 rcp->completed = rcp->cur;
313 rcu_start_batch(rcp);
314 }
315}
316
317/*
318 * Check if the cpu has gone through a quiescent state (say context
319 * switch). If so and if it already hasn't done so in this RCU
320 * quiescent cycle, then indicate that it has done so.
321 */
322static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
323 struct rcu_data *rdp)
324{
325 if (rdp->quiescbatch != rcp->cur) {
326 /* start new grace period: */
327 rdp->qs_pending = 1;
328 rdp->passed_quiesc = 0;
329 rdp->quiescbatch = rcp->cur;
330 return;
331 }
332
333 /* Grace period already completed for this cpu?
334 * qs_pending is checked instead of the actual bitmap to avoid
335 * cacheline trashing.
336 */
337 if (!rdp->qs_pending)
338 return;
339
340 /*
341 * Was there a quiescent state since the beginning of the grace
342 * period? If no, then exit and wait for the next call.
343 */
344 if (!rdp->passed_quiesc)
345 return;
346 rdp->qs_pending = 0;
347
348 spin_lock(&rcp->lock);
349 /*
350 * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync
351 * during cpu startup. Ignore the quiescent state.
352 */
353 if (likely(rdp->quiescbatch == rcp->cur))
354 cpu_quiet(rdp->cpu, rcp);
355
356 spin_unlock(&rcp->lock);
357}
358
359
360#ifdef CONFIG_HOTPLUG_CPU
361
362/* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing
363 * locking requirements, the list it's pulling from has to belong to a cpu
364 * which is dead and hence not processing interrupts.
365 */
366static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
367 struct rcu_head **tail)
368{
369 local_irq_disable();
370 *this_rdp->nxttail = list;
371 if (list)
372 this_rdp->nxttail = tail;
373 local_irq_enable();
374}
375
376static void __rcu_offline_cpu(struct rcu_data *this_rdp,
377 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
378{
379 /* if the cpu going offline owns the grace period
380 * we can block indefinitely waiting for it, so flush
381 * it here
382 */
383 spin_lock_bh(&rcp->lock);
384 if (rcp->cur != rcp->completed)
385 cpu_quiet(rdp->cpu, rcp);
386 spin_unlock_bh(&rcp->lock);
Paul E. McKenneye0ecfa72008-01-25 21:08:24 +0100387 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100388 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
389 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
Lai Jiangshan199a9522008-06-26 10:06:43 +0800390
391 local_irq_disable();
392 this_rdp->qlen += rdp->qlen;
393 local_irq_enable();
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100394}
395
396static void rcu_offline_cpu(int cpu)
397{
398 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
399 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
400
401 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
402 &per_cpu(rcu_data, cpu));
403 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
404 &per_cpu(rcu_bh_data, cpu));
405 put_cpu_var(rcu_data);
406 put_cpu_var(rcu_bh_data);
407}
408
409#else
410
411static void rcu_offline_cpu(int cpu)
412{
413}
414
415#endif
416
417/*
418 * This does the RCU processing work from softirq context.
419 */
420static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp,
421 struct rcu_data *rdp)
422{
423 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch)) {
424 *rdp->donetail = rdp->curlist;
425 rdp->donetail = rdp->curtail;
426 rdp->curlist = NULL;
427 rdp->curtail = &rdp->curlist;
428 }
429
430 if (rdp->nxtlist && !rdp->curlist) {
431 local_irq_disable();
432 rdp->curlist = rdp->nxtlist;
433 rdp->curtail = rdp->nxttail;
434 rdp->nxtlist = NULL;
435 rdp->nxttail = &rdp->nxtlist;
436 local_irq_enable();
437
438 /*
439 * start the next batch of callbacks
440 */
441
442 /* determine batch number */
443 rdp->batch = rcp->cur + 1;
444 /* see the comment and corresponding wmb() in
445 * the rcu_start_batch()
446 */
447 smp_rmb();
448
449 if (!rcp->next_pending) {
450 /* and start it/schedule start if it's a new batch */
451 spin_lock(&rcp->lock);
452 rcp->next_pending = 1;
453 rcu_start_batch(rcp);
454 spin_unlock(&rcp->lock);
455 }
456 }
457
458 rcu_check_quiescent_state(rcp, rdp);
459 if (rdp->donelist)
460 rcu_do_batch(rdp);
461}
462
463static void rcu_process_callbacks(struct softirq_action *unused)
464{
465 __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data));
466 __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data));
467}
468
469static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
470{
471 /* This cpu has pending rcu entries and the grace period
472 * for them has completed.
473 */
474 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
475 return 1;
476
477 /* This cpu has no pending entries, but there are new entries */
478 if (!rdp->curlist && rdp->nxtlist)
479 return 1;
480
481 /* This cpu has finished callbacks to invoke */
482 if (rdp->donelist)
483 return 1;
484
485 /* The rcu core waits for a quiescent state from the cpu */
486 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
487 return 1;
488
489 /* nothing to do */
490 return 0;
491}
492
493/*
494 * Check to see if there is any immediate RCU-related work to be done
495 * by the current CPU, returning 1 if so. This function is part of the
496 * RCU implementation; it is -not- an exported member of the RCU API.
497 */
498int rcu_pending(int cpu)
499{
500 return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) ||
501 __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu));
502}
503
504/*
505 * Check to see if any future RCU-related work will need to be done
506 * by the current CPU, even if none need be done immediately, returning
507 * 1 if so. This function is part of the RCU implementation; it is -not-
508 * an exported member of the RCU API.
509 */
510int rcu_needs_cpu(int cpu)
511{
512 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
513 struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu);
514
515 return (!!rdp->curlist || !!rdp_bh->curlist || rcu_pending(cpu));
516}
517
518void rcu_check_callbacks(int cpu, int user)
519{
520 if (user ||
521 (idle_cpu(cpu) && !in_softirq() &&
522 hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
Paul E. McKenney8db559b2008-05-12 21:21:05 +0200523
524 /*
525 * Get here if this CPU took its interrupt from user
526 * mode or from the idle loop, and if this is not a
527 * nested interrupt. In this case, the CPU is in
528 * a quiescent state, so count it.
529 *
530 * Also do a memory barrier. This is needed to handle
531 * the case where writes from a preempt-disable section
532 * of code get reordered into schedule() by this CPU's
533 * write buffer. The memory barrier makes sure that
534 * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see
535 * by other CPUs to happen after any such write.
536 */
537
538 smp_mb(); /* See above block comment. */
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100539 rcu_qsctr_inc(cpu);
540 rcu_bh_qsctr_inc(cpu);
Paul E. McKenney8db559b2008-05-12 21:21:05 +0200541
542 } else if (!in_softirq()) {
543
544 /*
545 * Get here if this CPU did not take its interrupt from
546 * softirq, in other words, if it is not interrupting
547 * a rcu_bh read-side critical section. This is an _bh
548 * critical section, so count it. The memory barrier
549 * is needed for the same reason as is the above one.
550 */
551
552 smp_mb(); /* See above block comment. */
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100553 rcu_bh_qsctr_inc(cpu);
Paul E. McKenney8db559b2008-05-12 21:21:05 +0200554 }
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100555 raise_rcu_softirq();
556}
557
558static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
559 struct rcu_data *rdp)
560{
561 memset(rdp, 0, sizeof(*rdp));
562 rdp->curtail = &rdp->curlist;
563 rdp->nxttail = &rdp->nxtlist;
564 rdp->donetail = &rdp->donelist;
565 rdp->quiescbatch = rcp->completed;
566 rdp->qs_pending = 0;
567 rdp->cpu = cpu;
568 rdp->blimit = blimit;
569}
570
571static void __cpuinit rcu_online_cpu(int cpu)
572{
573 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
574 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
575
576 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
577 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
Carlos R. Mafra962cf362008-05-15 11:15:37 -0300578 open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
Paul E. McKenney01c1c662008-01-25 21:08:24 +0100579}
580
581static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
582 unsigned long action, void *hcpu)
583{
584 long cpu = (long)hcpu;
585
586 switch (action) {
587 case CPU_UP_PREPARE:
588 case CPU_UP_PREPARE_FROZEN:
589 rcu_online_cpu(cpu);
590 break;
591 case CPU_DEAD:
592 case CPU_DEAD_FROZEN:
593 rcu_offline_cpu(cpu);
594 break;
595 default:
596 break;
597 }
598 return NOTIFY_OK;
599}
600
601static struct notifier_block __cpuinitdata rcu_nb = {
602 .notifier_call = rcu_cpu_notify,
603};
604
605/*
606 * Initializes rcu mechanism. Assumed to be called early.
607 * That is before local timer(SMP) or jiffie timer (uniproc) is setup.
608 * Note that rcu_qsctr and friends are implicitly
609 * initialized due to the choice of ``0'' for RCU_CTR_INVALID.
610 */
611void __init __rcu_init(void)
612{
613 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
614 (void *)(long)smp_processor_id());
615 /* Register notifier for non-boot CPUs */
616 register_cpu_notifier(&rcu_nb);
617}
618
619module_param(blimit, int, 0);
620module_param(qhimark, int, 0);
621module_param(qlowmark, int, 0);