1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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>
48#include <linux/rcupdate.h>
49#include <linux/cpu.h>
50#include <linux/mutex.h>
51
52
53static struct rcu_ctrlblk rcu_ctrlblk = {
54 .cur = -300,
55 .completed = -300,
56 .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock),
57 .cpumask = CPU_MASK_NONE,
58};
59static struct rcu_ctrlblk rcu_bh_ctrlblk = {
60 .cur = -300,
61 .completed = -300,
62 .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock),
63 .cpumask = CPU_MASK_NONE,
64};
65
66DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L };
67DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L };
68
69
70static DEFINE_PER_CPU(struct tasklet_struct, rcu_tasklet) = {NULL};
71static int blimit = 10;
72static int qhimark = 10000;
73static int qlowmark = 100;
74
75static atomic_t rcu_barrier_cpu_count;
76static DEFINE_MUTEX(rcu_barrier_mutex);
77static struct completion rcu_barrier_completion;
78
79#ifdef CONFIG_SMP
80static void force_quiescent_state(struct rcu_data *rdp,
81 struct rcu_ctrlblk *rcp)
82{
83 int cpu;
84 cpumask_t cpumask;
85 set_need_resched();
86 if (unlikely(!rcp->signaled)) {
87 rcp->signaled = 1;
88
89
90
91
92 cpumask = rcp->cpumask;
93 cpu_clear(rdp->cpu, cpumask);
94 for_each_cpu_mask(cpu, cpumask)
95 smp_send_reschedule(cpu);
96 }
97}
98#else
99static inline void force_quiescent_state(struct rcu_data *rdp,
100 struct rcu_ctrlblk *rcp)
101{
102 set_need_resched();
103}
104#endif
105
106
107
108
109
110
111
112
113
114
115
116
117void fastcall call_rcu(struct rcu_head *head,
118 void (*func)(struct rcu_head *rcu))
119{
120 unsigned long flags;
121 struct rcu_data *rdp;
122
123 head->func = func;
124 head->next = NULL;
125 local_irq_save(flags);
126 rdp = &__get_cpu_var(rcu_data);
127 *rdp->nxttail = head;
128 rdp->nxttail = &head->next;
129 if (unlikely(++rdp->qlen > qhimark)) {
130 rdp->blimit = INT_MAX;
131 force_quiescent_state(rdp, &rcu_ctrlblk);
132 }
133 local_irq_restore(flags);
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152void fastcall call_rcu_bh(struct rcu_head *head,
153 void (*func)(struct rcu_head *rcu))
154{
155 unsigned long flags;
156 struct rcu_data *rdp;
157
158 head->func = func;
159 head->next = NULL;
160 local_irq_save(flags);
161 rdp = &__get_cpu_var(rcu_bh_data);
162 *rdp->nxttail = head;
163 rdp->nxttail = &head->next;
164
165 if (unlikely(++rdp->qlen > qhimark)) {
166 rdp->blimit = INT_MAX;
167 force_quiescent_state(rdp, &rcu_bh_ctrlblk);
168 }
169
170 local_irq_restore(flags);
171}
172
173
174
175
176
177long rcu_batches_completed(void)
178{
179 return rcu_ctrlblk.completed;
180}
181
182
183
184
185
186long rcu_batches_completed_bh(void)
187{
188 return rcu_bh_ctrlblk.completed;
189}
190
191static void rcu_barrier_callback(struct rcu_head *notused)
192{
193 if (atomic_dec_and_test(&rcu_barrier_cpu_count))
194 complete(&rcu_barrier_completion);
195}
196
197
198
199
200static void rcu_barrier_func(void *notused)
201{
202 int cpu = smp_processor_id();
203 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
204 struct rcu_head *head;
205
206 head = &rdp->barrier;
207 atomic_inc(&rcu_barrier_cpu_count);
208 call_rcu(head, rcu_barrier_callback);
209}
210
211
212
213
214void rcu_barrier(void)
215{
216 BUG_ON(in_interrupt());
217
218 mutex_lock(&rcu_barrier_mutex);
219 init_completion(&rcu_barrier_completion);
220 atomic_set(&rcu_barrier_cpu_count, 0);
221 on_each_cpu(rcu_barrier_func, NULL, 0, 1);
222 wait_for_completion(&rcu_barrier_completion);
223 mutex_unlock(&rcu_barrier_mutex);
224}
225EXPORT_SYMBOL_GPL(rcu_barrier);
226
227
228
229
230
231static void rcu_do_batch(struct rcu_data *rdp)
232{
233 struct rcu_head *next, *list;
234 int count = 0;
235
236 list = rdp->donelist;
237 while (list) {
238 next = rdp->donelist = list->next;
239 list->func(list);
240 list = next;
241 if (++count >= rdp->blimit)
242 break;
243 }
244
245 local_irq_disable();
246 rdp->qlen -= count;
247 local_irq_enable();
248 if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark)
249 rdp->blimit = blimit;
250
251 if (!rdp->donelist)
252 rdp->donetail = &rdp->donelist;
253 else
254 tasklet_schedule(&per_cpu(rcu_tasklet, rdp->cpu));
255}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280static void rcu_start_batch(struct rcu_ctrlblk *rcp)
281{
282 if (rcp->next_pending &&
283 rcp->completed == rcp->cur) {
284 rcp->next_pending = 0;
285
286
287
288
289 smp_wmb();
290 rcp->cur++;
291
292
293
294
295
296
297
298 smp_mb();
299 cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask);
300
301 rcp->signaled = 0;
302 }
303}
304
305
306
307
308
309
310static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp)
311{
312 cpu_clear(cpu, rcp->cpumask);
313 if (cpus_empty(rcp->cpumask)) {
314
315 rcp->completed = rcp->cur;
316 rcu_start_batch(rcp);
317 }
318}
319
320
321
322
323
324
325static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp,
326 struct rcu_data *rdp)
327{
328 if (rdp->quiescbatch != rcp->cur) {
329
330 rdp->qs_pending = 1;
331 rdp->passed_quiesc = 0;
332 rdp->quiescbatch = rcp->cur;
333 return;
334 }
335
336
337
338
339
340 if (!rdp->qs_pending)
341 return;
342
343
344
345
346
347 if (!rdp->passed_quiesc)
348 return;
349 rdp->qs_pending = 0;
350
351 spin_lock(&rcp->lock);
352
353
354
355
356 if (likely(rdp->quiescbatch == rcp->cur))
357 cpu_quiet(rdp->cpu, rcp);
358
359 spin_unlock(&rcp->lock);
360}
361
362
363#ifdef CONFIG_HOTPLUG_CPU
364
365
366
367
368
369static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list,
370 struct rcu_head **tail)
371{
372 local_irq_disable();
373 *this_rdp->nxttail = list;
374 if (list)
375 this_rdp->nxttail = tail;
376 local_irq_enable();
377}
378
379static void __rcu_offline_cpu(struct rcu_data *this_rdp,
380 struct rcu_ctrlblk *rcp, struct rcu_data *rdp)
381{
382
383
384
385
386 spin_lock_bh(&rcp->lock);
387 if (rcp->cur != rcp->completed)
388 cpu_quiet(rdp->cpu, rcp);
389 spin_unlock_bh(&rcp->lock);
390 rcu_move_batch(this_rdp, rdp->curlist, rdp->curtail);
391 rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail);
392 rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail);
393}
394
395static void rcu_offline_cpu(int cpu)
396{
397 struct rcu_data *this_rdp = &get_cpu_var(rcu_data);
398 struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data);
399
400 __rcu_offline_cpu(this_rdp, &rcu_ctrlblk,
401 &per_cpu(rcu_data, cpu));
402 __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk,
403 &per_cpu(rcu_bh_data, cpu));
404 put_cpu_var(rcu_data);
405 put_cpu_var(rcu_bh_data);
406 tasklet_kill_immediate(&per_cpu(rcu_tasklet, cpu), cpu);
407}
408
409#else
410
411static void rcu_offline_cpu(int cpu)
412{
413}
414
415#endif
416
417
418
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
440
441
442
443 rdp->batch = rcp->cur + 1;
444
445
446
447 smp_rmb();
448
449 if (!rcp->next_pending) {
450
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(unsigned long 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
472
473
474 if (rdp->curlist && !rcu_batch_before(rcp->completed, rdp->batch))
475 return 1;
476
477
478 if (!rdp->curlist && rdp->nxtlist)
479 return 1;
480
481
482 if (rdp->donelist)
483 return 1;
484
485
486 if (rdp->quiescbatch != rcp->cur || rdp->qs_pending)
487 return 1;
488
489
490 return 0;
491}
492
493
494
495
496
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
506
507
508
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))) {
523 rcu_qsctr_inc(cpu);
524 rcu_bh_qsctr_inc(cpu);
525 } else if (!in_softirq())
526 rcu_bh_qsctr_inc(cpu);
527 tasklet_schedule(&per_cpu(rcu_tasklet, cpu));
528}
529
530static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp,
531 struct rcu_data *rdp)
532{
533 memset(rdp, 0, sizeof(*rdp));
534 rdp->curtail = &rdp->curlist;
535 rdp->nxttail = &rdp->nxtlist;
536 rdp->donetail = &rdp->donelist;
537 rdp->quiescbatch = rcp->completed;
538 rdp->qs_pending = 0;
539 rdp->cpu = cpu;
540 rdp->blimit = blimit;
541}
542
543static void __devinit rcu_online_cpu(int cpu)
544{
545 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);
546 struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu);
547
548 rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp);
549 rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp);
550 tasklet_init(&per_cpu(rcu_tasklet, cpu), rcu_process_callbacks, 0UL);
551}
552
553static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
554 unsigned long action, void *hcpu)
555{
556 long cpu = (long)hcpu;
557 switch (action) {
558 case CPU_UP_PREPARE:
559 rcu_online_cpu(cpu);
560 break;
561 case CPU_DEAD:
562 rcu_offline_cpu(cpu);
563 break;
564 default:
565 break;
566 }
567 return NOTIFY_OK;
568}
569
570static struct notifier_block __cpuinitdata rcu_nb = {
571 .notifier_call = rcu_cpu_notify,
572};
573
574
575
576
577
578
579
580void __init rcu_init(void)
581{
582 rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE,
583 (void *)(long)smp_processor_id());
584
585 register_cpu_notifier(&rcu_nb);
586}
587
588struct rcu_synchronize {
589 struct rcu_head head;
590 struct completion completion;
591};
592
593
594static void wakeme_after_rcu(struct rcu_head *head)
595{
596 struct rcu_synchronize *rcu;
597
598 rcu = container_of(head, struct rcu_synchronize, head);
599 complete(&rcu->completion);
600}
601
602
603
604
605
606
607
608
609
610
611
612
613
614void synchronize_rcu(void)
615{
616 struct rcu_synchronize rcu;
617
618 init_completion(&rcu.completion);
619
620 call_rcu(&rcu.head, wakeme_after_rcu);
621
622
623 wait_for_completion(&rcu.completion);
624}
625
626module_param(blimit, int, 0);
627module_param(qhimark, int, 0);
628module_param(qlowmark, int, 0);
629EXPORT_SYMBOL_GPL(rcu_batches_completed);
630EXPORT_SYMBOL_GPL(rcu_batches_completed_bh);
631EXPORT_SYMBOL_GPL(call_rcu);
632EXPORT_SYMBOL_GPL(call_rcu_bh);
633EXPORT_SYMBOL_GPL(synchronize_rcu);
634