Commit | Line | Data |
---|---|---|
01c1c660 PM |
1 | /* |
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> | |
01c1c660 PM |
48 | #include <linux/cpu.h> |
49 | #include <linux/mutex.h> | |
67182ae1 | 50 | #include <linux/time.h> |
01c1c660 PM |
51 | |
52 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
53 | static struct lock_class_key rcu_lock_key; | |
54 | struct lockdep_map rcu_lock_map = | |
55 | STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key); | |
56 | EXPORT_SYMBOL_GPL(rcu_lock_map); | |
57 | #endif | |
58 | ||
59 | ||
60 | /* Definition for rcupdate control block. */ | |
61 | static struct rcu_ctrlblk rcu_ctrlblk = { | |
62 | .cur = -300, | |
63 | .completed = -300, | |
3cac97cb | 64 | .pending = -300, |
01c1c660 PM |
65 | .lock = __SPIN_LOCK_UNLOCKED(&rcu_ctrlblk.lock), |
66 | .cpumask = CPU_MASK_NONE, | |
67 | }; | |
68 | static struct rcu_ctrlblk rcu_bh_ctrlblk = { | |
69 | .cur = -300, | |
70 | .completed = -300, | |
3cac97cb | 71 | .pending = -300, |
01c1c660 PM |
72 | .lock = __SPIN_LOCK_UNLOCKED(&rcu_bh_ctrlblk.lock), |
73 | .cpumask = CPU_MASK_NONE, | |
74 | }; | |
75 | ||
76 | DEFINE_PER_CPU(struct rcu_data, rcu_data) = { 0L }; | |
77 | DEFINE_PER_CPU(struct rcu_data, rcu_bh_data) = { 0L }; | |
78 | ||
79 | static int blimit = 10; | |
80 | static int qhimark = 10000; | |
81 | static int qlowmark = 100; | |
82 | ||
83 | #ifdef CONFIG_SMP | |
84 | static void force_quiescent_state(struct rcu_data *rdp, | |
85 | struct rcu_ctrlblk *rcp) | |
86 | { | |
87 | int cpu; | |
88 | cpumask_t cpumask; | |
eff9b713 PM |
89 | unsigned long flags; |
90 | ||
01c1c660 | 91 | set_need_resched(); |
eff9b713 | 92 | spin_lock_irqsave(&rcp->lock, flags); |
01c1c660 PM |
93 | if (unlikely(!rcp->signaled)) { |
94 | rcp->signaled = 1; | |
95 | /* | |
96 | * Don't send IPI to itself. With irqs disabled, | |
97 | * rdp->cpu is the current cpu. | |
8558f8f8 GS |
98 | * |
99 | * cpu_online_map is updated by the _cpu_down() | |
9b1a4d38 RR |
100 | * using __stop_machine(). Since we're in irqs disabled |
101 | * section, __stop_machine() is not exectuting, hence | |
8558f8f8 GS |
102 | * the cpu_online_map is stable. |
103 | * | |
104 | * However, a cpu might have been offlined _just_ before | |
105 | * we disabled irqs while entering here. | |
106 | * And rcu subsystem might not yet have handled the CPU_DEAD | |
107 | * notification, leading to the offlined cpu's bit | |
108 | * being set in the rcp->cpumask. | |
109 | * | |
110 | * Hence cpumask = (rcp->cpumask & cpu_online_map) to prevent | |
111 | * sending smp_reschedule() to an offlined CPU. | |
01c1c660 | 112 | */ |
8558f8f8 | 113 | cpus_and(cpumask, rcp->cpumask, cpu_online_map); |
01c1c660 | 114 | cpu_clear(rdp->cpu, cpumask); |
363ab6f1 | 115 | for_each_cpu_mask_nr(cpu, cpumask) |
01c1c660 PM |
116 | smp_send_reschedule(cpu); |
117 | } | |
eff9b713 | 118 | spin_unlock_irqrestore(&rcp->lock, flags); |
01c1c660 PM |
119 | } |
120 | #else | |
121 | static inline void force_quiescent_state(struct rcu_data *rdp, | |
122 | struct rcu_ctrlblk *rcp) | |
123 | { | |
124 | set_need_resched(); | |
125 | } | |
126 | #endif | |
127 | ||
5127bed5 LJ |
128 | static void __call_rcu(struct rcu_head *head, struct rcu_ctrlblk *rcp, |
129 | struct rcu_data *rdp) | |
130 | { | |
131 | long batch; | |
1f7b94cd PM |
132 | |
133 | head->next = NULL; | |
134 | smp_mb(); /* Read of rcu->cur must happen after any change by caller. */ | |
5127bed5 LJ |
135 | |
136 | /* | |
137 | * Determine the batch number of this callback. | |
138 | * | |
139 | * Using ACCESS_ONCE to avoid the following error when gcc eliminates | |
140 | * local variable "batch" and emits codes like this: | |
141 | * 1) rdp->batch = rcp->cur + 1 # gets old value | |
142 | * ...... | |
143 | * 2)rcu_batch_after(rcp->cur + 1, rdp->batch) # gets new value | |
144 | * then [*nxttail[0], *nxttail[1]) may contain callbacks | |
145 | * that batch# = rdp->batch, see the comment of struct rcu_data. | |
146 | */ | |
147 | batch = ACCESS_ONCE(rcp->cur) + 1; | |
148 | ||
149 | if (rdp->nxtlist && rcu_batch_after(batch, rdp->batch)) { | |
150 | /* process callbacks */ | |
151 | rdp->nxttail[0] = rdp->nxttail[1]; | |
152 | rdp->nxttail[1] = rdp->nxttail[2]; | |
153 | if (rcu_batch_after(batch - 1, rdp->batch)) | |
154 | rdp->nxttail[0] = rdp->nxttail[2]; | |
155 | } | |
156 | ||
157 | rdp->batch = batch; | |
158 | *rdp->nxttail[2] = head; | |
159 | rdp->nxttail[2] = &head->next; | |
160 | ||
161 | if (unlikely(++rdp->qlen > qhimark)) { | |
162 | rdp->blimit = INT_MAX; | |
163 | force_quiescent_state(rdp, &rcu_ctrlblk); | |
164 | } | |
165 | } | |
166 | ||
01c1c660 PM |
167 | /** |
168 | * call_rcu - Queue an RCU callback for invocation after a grace period. | |
169 | * @head: structure to be used for queueing the RCU updates. | |
170 | * @func: actual update function to be invoked after the grace period | |
171 | * | |
172 | * The update function will be invoked some time after a full grace | |
173 | * period elapses, in other words after all currently executing RCU | |
174 | * read-side critical sections have completed. RCU read-side critical | |
175 | * sections are delimited by rcu_read_lock() and rcu_read_unlock(), | |
176 | * and may be nested. | |
177 | */ | |
178 | void call_rcu(struct rcu_head *head, | |
179 | void (*func)(struct rcu_head *rcu)) | |
180 | { | |
181 | unsigned long flags; | |
01c1c660 PM |
182 | |
183 | head->func = func; | |
01c1c660 | 184 | local_irq_save(flags); |
5127bed5 | 185 | __call_rcu(head, &rcu_ctrlblk, &__get_cpu_var(rcu_data)); |
01c1c660 PM |
186 | local_irq_restore(flags); |
187 | } | |
188 | EXPORT_SYMBOL_GPL(call_rcu); | |
189 | ||
190 | /** | |
191 | * call_rcu_bh - Queue an RCU for invocation after a quicker grace period. | |
192 | * @head: structure to be used for queueing the RCU updates. | |
193 | * @func: actual update function to be invoked after the grace period | |
194 | * | |
195 | * The update function will be invoked some time after a full grace | |
196 | * period elapses, in other words after all currently executing RCU | |
197 | * read-side critical sections have completed. call_rcu_bh() assumes | |
198 | * that the read-side critical sections end on completion of a softirq | |
199 | * handler. This means that read-side critical sections in process | |
200 | * context must not be interrupted by softirqs. This interface is to be | |
201 | * used when most of the read-side critical sections are in softirq context. | |
202 | * RCU read-side critical sections are delimited by rcu_read_lock() and | |
203 | * rcu_read_unlock(), * if in interrupt context or rcu_read_lock_bh() | |
204 | * and rcu_read_unlock_bh(), if in process context. These may be nested. | |
205 | */ | |
206 | void call_rcu_bh(struct rcu_head *head, | |
207 | void (*func)(struct rcu_head *rcu)) | |
208 | { | |
209 | unsigned long flags; | |
01c1c660 PM |
210 | |
211 | head->func = func; | |
01c1c660 | 212 | local_irq_save(flags); |
5127bed5 | 213 | __call_rcu(head, &rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data)); |
01c1c660 PM |
214 | local_irq_restore(flags); |
215 | } | |
216 | EXPORT_SYMBOL_GPL(call_rcu_bh); | |
217 | ||
218 | /* | |
219 | * Return the number of RCU batches processed thus far. Useful | |
220 | * for debug and statistics. | |
221 | */ | |
222 | long rcu_batches_completed(void) | |
223 | { | |
224 | return rcu_ctrlblk.completed; | |
225 | } | |
226 | EXPORT_SYMBOL_GPL(rcu_batches_completed); | |
227 | ||
228 | /* | |
229 | * Return the number of RCU batches processed thus far. Useful | |
230 | * for debug and statistics. | |
231 | */ | |
232 | long rcu_batches_completed_bh(void) | |
233 | { | |
234 | return rcu_bh_ctrlblk.completed; | |
235 | } | |
236 | EXPORT_SYMBOL_GPL(rcu_batches_completed_bh); | |
237 | ||
238 | /* Raises the softirq for processing rcu_callbacks. */ | |
239 | static inline void raise_rcu_softirq(void) | |
240 | { | |
241 | raise_softirq(RCU_SOFTIRQ); | |
01c1c660 PM |
242 | } |
243 | ||
244 | /* | |
245 | * Invoke the completed RCU callbacks. They are expected to be in | |
246 | * a per-cpu list. | |
247 | */ | |
248 | static void rcu_do_batch(struct rcu_data *rdp) | |
249 | { | |
250 | struct rcu_head *next, *list; | |
251 | int count = 0; | |
252 | ||
253 | list = rdp->donelist; | |
254 | while (list) { | |
255 | next = list->next; | |
256 | prefetch(next); | |
257 | list->func(list); | |
258 | list = next; | |
259 | if (++count >= rdp->blimit) | |
260 | break; | |
261 | } | |
262 | rdp->donelist = list; | |
263 | ||
264 | local_irq_disable(); | |
265 | rdp->qlen -= count; | |
266 | local_irq_enable(); | |
267 | if (rdp->blimit == INT_MAX && rdp->qlen <= qlowmark) | |
268 | rdp->blimit = blimit; | |
269 | ||
270 | if (!rdp->donelist) | |
271 | rdp->donetail = &rdp->donelist; | |
272 | else | |
273 | raise_rcu_softirq(); | |
274 | } | |
275 | ||
276 | /* | |
277 | * Grace period handling: | |
278 | * The grace period handling consists out of two steps: | |
279 | * - A new grace period is started. | |
280 | * This is done by rcu_start_batch. The start is not broadcasted to | |
281 | * all cpus, they must pick this up by comparing rcp->cur with | |
282 | * rdp->quiescbatch. All cpus are recorded in the | |
283 | * rcu_ctrlblk.cpumask bitmap. | |
284 | * - All cpus must go through a quiescent state. | |
285 | * Since the start of the grace period is not broadcasted, at least two | |
286 | * calls to rcu_check_quiescent_state are required: | |
287 | * The first call just notices that a new grace period is running. The | |
288 | * following calls check if there was a quiescent state since the beginning | |
289 | * of the grace period. If so, it updates rcu_ctrlblk.cpumask. If | |
290 | * the bitmap is empty, then the grace period is completed. | |
291 | * rcu_check_quiescent_state calls rcu_start_batch(0) to start the next grace | |
292 | * period (if necessary). | |
293 | */ | |
67182ae1 PM |
294 | |
295 | #ifdef CONFIG_DEBUG_RCU_STALL | |
296 | ||
297 | static inline void record_gp_check_time(struct rcu_ctrlblk *rcp) | |
298 | { | |
299 | rcp->gp_check = get_seconds() + 3; | |
300 | } | |
78635fc7 | 301 | |
67182ae1 PM |
302 | static void print_other_cpu_stall(struct rcu_ctrlblk *rcp) |
303 | { | |
304 | int cpu; | |
305 | long delta; | |
eff9b713 | 306 | unsigned long flags; |
67182ae1 PM |
307 | |
308 | /* Only let one CPU complain about others per time interval. */ | |
309 | ||
eff9b713 | 310 | spin_lock_irqsave(&rcp->lock, flags); |
67182ae1 | 311 | delta = get_seconds() - rcp->gp_check; |
78635fc7 | 312 | if (delta < 2L || cpus_empty(rcp->cpumask)) { |
67182ae1 PM |
313 | spin_unlock(&rcp->lock); |
314 | return; | |
67182ae1 | 315 | } |
293a17eb | 316 | rcp->gp_check = get_seconds() + 30; |
eff9b713 | 317 | spin_unlock_irqrestore(&rcp->lock, flags); |
67182ae1 PM |
318 | |
319 | /* OK, time to rat on our buddy... */ | |
320 | ||
321 | printk(KERN_ERR "RCU detected CPU stalls:"); | |
322 | for_each_cpu_mask(cpu, rcp->cpumask) | |
323 | printk(" %d", cpu); | |
324 | printk(" (detected by %d, t=%lu/%lu)\n", | |
325 | smp_processor_id(), get_seconds(), rcp->gp_check); | |
326 | } | |
78635fc7 | 327 | |
67182ae1 PM |
328 | static void print_cpu_stall(struct rcu_ctrlblk *rcp) |
329 | { | |
eff9b713 PM |
330 | unsigned long flags; |
331 | ||
67182ae1 PM |
332 | printk(KERN_ERR "RCU detected CPU %d stall (t=%lu/%lu)\n", |
333 | smp_processor_id(), get_seconds(), rcp->gp_check); | |
334 | dump_stack(); | |
eff9b713 | 335 | spin_lock_irqsave(&rcp->lock, flags); |
67182ae1 PM |
336 | if ((long)(get_seconds() - rcp->gp_check) >= 0L) |
337 | rcp->gp_check = get_seconds() + 30; | |
eff9b713 | 338 | spin_unlock_irqrestore(&rcp->lock, flags); |
67182ae1 | 339 | } |
78635fc7 IM |
340 | |
341 | static void check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp) | |
67182ae1 PM |
342 | { |
343 | long delta; | |
344 | ||
345 | delta = get_seconds() - rcp->gp_check; | |
346 | if (cpu_isset(smp_processor_id(), rcp->cpumask) && delta >= 0L) { | |
347 | ||
348 | /* We haven't checked in, so go dump stack. */ | |
349 | ||
350 | print_cpu_stall(rcp); | |
351 | ||
78635fc7 IM |
352 | } else { |
353 | if (!cpus_empty(rcp->cpumask) && delta >= 2L) { | |
354 | /* They had two seconds to dump stack, so complain. */ | |
355 | print_other_cpu_stall(rcp); | |
356 | } | |
67182ae1 PM |
357 | } |
358 | } | |
359 | ||
360 | #else /* #ifdef CONFIG_DEBUG_RCU_STALL */ | |
361 | ||
362 | static inline void record_gp_check_time(struct rcu_ctrlblk *rcp) | |
363 | { | |
364 | } | |
78635fc7 IM |
365 | |
366 | static inline void | |
367 | check_cpu_stall(struct rcu_ctrlblk *rcp, struct rcu_data *rdp) | |
67182ae1 PM |
368 | { |
369 | } | |
370 | ||
371 | #endif /* #else #ifdef CONFIG_DEBUG_RCU_STALL */ | |
372 | ||
01c1c660 PM |
373 | /* |
374 | * Register a new batch of callbacks, and start it up if there is currently no | |
375 | * active batch and the batch to be registered has not already occurred. | |
376 | * Caller must hold rcu_ctrlblk.lock. | |
377 | */ | |
378 | static void rcu_start_batch(struct rcu_ctrlblk *rcp) | |
379 | { | |
3cac97cb | 380 | if (rcp->cur != rcp->pending && |
01c1c660 | 381 | rcp->completed == rcp->cur) { |
01c1c660 | 382 | rcp->cur++; |
67182ae1 | 383 | record_gp_check_time(rcp); |
01c1c660 PM |
384 | |
385 | /* | |
386 | * Accessing nohz_cpu_mask before incrementing rcp->cur needs a | |
387 | * Barrier Otherwise it can cause tickless idle CPUs to be | |
388 | * included in rcp->cpumask, which will extend graceperiods | |
389 | * unnecessarily. | |
390 | */ | |
391 | smp_mb(); | |
392 | cpus_andnot(rcp->cpumask, cpu_online_map, nohz_cpu_mask); | |
393 | ||
394 | rcp->signaled = 0; | |
395 | } | |
396 | } | |
397 | ||
398 | /* | |
399 | * cpu went through a quiescent state since the beginning of the grace period. | |
400 | * Clear it from the cpu mask and complete the grace period if it was the last | |
401 | * cpu. Start another grace period if someone has further entries pending | |
402 | */ | |
403 | static void cpu_quiet(int cpu, struct rcu_ctrlblk *rcp) | |
404 | { | |
405 | cpu_clear(cpu, rcp->cpumask); | |
406 | if (cpus_empty(rcp->cpumask)) { | |
407 | /* batch completed ! */ | |
408 | rcp->completed = rcp->cur; | |
409 | rcu_start_batch(rcp); | |
410 | } | |
411 | } | |
412 | ||
413 | /* | |
414 | * Check if the cpu has gone through a quiescent state (say context | |
415 | * switch). If so and if it already hasn't done so in this RCU | |
416 | * quiescent cycle, then indicate that it has done so. | |
417 | */ | |
418 | static void rcu_check_quiescent_state(struct rcu_ctrlblk *rcp, | |
419 | struct rcu_data *rdp) | |
420 | { | |
eff9b713 PM |
421 | unsigned long flags; |
422 | ||
01c1c660 PM |
423 | if (rdp->quiescbatch != rcp->cur) { |
424 | /* start new grace period: */ | |
425 | rdp->qs_pending = 1; | |
426 | rdp->passed_quiesc = 0; | |
427 | rdp->quiescbatch = rcp->cur; | |
428 | return; | |
429 | } | |
430 | ||
431 | /* Grace period already completed for this cpu? | |
432 | * qs_pending is checked instead of the actual bitmap to avoid | |
433 | * cacheline trashing. | |
434 | */ | |
435 | if (!rdp->qs_pending) | |
436 | return; | |
437 | ||
438 | /* | |
439 | * Was there a quiescent state since the beginning of the grace | |
440 | * period? If no, then exit and wait for the next call. | |
441 | */ | |
442 | if (!rdp->passed_quiesc) | |
443 | return; | |
444 | rdp->qs_pending = 0; | |
445 | ||
eff9b713 | 446 | spin_lock_irqsave(&rcp->lock, flags); |
01c1c660 PM |
447 | /* |
448 | * rdp->quiescbatch/rcp->cur and the cpu bitmap can come out of sync | |
449 | * during cpu startup. Ignore the quiescent state. | |
450 | */ | |
451 | if (likely(rdp->quiescbatch == rcp->cur)) | |
452 | cpu_quiet(rdp->cpu, rcp); | |
453 | ||
eff9b713 | 454 | spin_unlock_irqrestore(&rcp->lock, flags); |
01c1c660 PM |
455 | } |
456 | ||
457 | ||
458 | #ifdef CONFIG_HOTPLUG_CPU | |
459 | ||
460 | /* warning! helper for rcu_offline_cpu. do not use elsewhere without reviewing | |
461 | * locking requirements, the list it's pulling from has to belong to a cpu | |
462 | * which is dead and hence not processing interrupts. | |
463 | */ | |
464 | static void rcu_move_batch(struct rcu_data *this_rdp, struct rcu_head *list, | |
5127bed5 | 465 | struct rcu_head **tail, long batch) |
01c1c660 | 466 | { |
5127bed5 LJ |
467 | if (list) { |
468 | local_irq_disable(); | |
469 | this_rdp->batch = batch; | |
470 | *this_rdp->nxttail[2] = list; | |
471 | this_rdp->nxttail[2] = tail; | |
472 | local_irq_enable(); | |
473 | } | |
01c1c660 PM |
474 | } |
475 | ||
476 | static void __rcu_offline_cpu(struct rcu_data *this_rdp, | |
477 | struct rcu_ctrlblk *rcp, struct rcu_data *rdp) | |
478 | { | |
eff9b713 PM |
479 | unsigned long flags; |
480 | ||
1f7b94cd PM |
481 | /* |
482 | * if the cpu going offline owns the grace period | |
01c1c660 PM |
483 | * we can block indefinitely waiting for it, so flush |
484 | * it here | |
485 | */ | |
eff9b713 | 486 | spin_lock_irqsave(&rcp->lock, flags); |
01c1c660 PM |
487 | if (rcp->cur != rcp->completed) |
488 | cpu_quiet(rdp->cpu, rcp); | |
5127bed5 LJ |
489 | rcu_move_batch(this_rdp, rdp->donelist, rdp->donetail, rcp->cur + 1); |
490 | rcu_move_batch(this_rdp, rdp->nxtlist, rdp->nxttail[2], rcp->cur + 1); | |
eff9b713 | 491 | spin_unlock(&rcp->lock); |
199a9528 | 492 | |
199a9528 | 493 | this_rdp->qlen += rdp->qlen; |
eff9b713 | 494 | local_irq_restore(flags); |
01c1c660 PM |
495 | } |
496 | ||
497 | static void rcu_offline_cpu(int cpu) | |
498 | { | |
499 | struct rcu_data *this_rdp = &get_cpu_var(rcu_data); | |
500 | struct rcu_data *this_bh_rdp = &get_cpu_var(rcu_bh_data); | |
501 | ||
502 | __rcu_offline_cpu(this_rdp, &rcu_ctrlblk, | |
503 | &per_cpu(rcu_data, cpu)); | |
504 | __rcu_offline_cpu(this_bh_rdp, &rcu_bh_ctrlblk, | |
505 | &per_cpu(rcu_bh_data, cpu)); | |
506 | put_cpu_var(rcu_data); | |
507 | put_cpu_var(rcu_bh_data); | |
508 | } | |
509 | ||
510 | #else | |
511 | ||
512 | static void rcu_offline_cpu(int cpu) | |
513 | { | |
514 | } | |
515 | ||
516 | #endif | |
517 | ||
518 | /* | |
519 | * This does the RCU processing work from softirq context. | |
520 | */ | |
521 | static void __rcu_process_callbacks(struct rcu_ctrlblk *rcp, | |
522 | struct rcu_data *rdp) | |
523 | { | |
1f7b94cd PM |
524 | long completed_snap; |
525 | ||
5127bed5 | 526 | if (rdp->nxtlist) { |
01c1c660 | 527 | local_irq_disable(); |
1f7b94cd | 528 | completed_snap = ACCESS_ONCE(rcp->completed); |
01c1c660 PM |
529 | |
530 | /* | |
5127bed5 LJ |
531 | * move the other grace-period-completed entries to |
532 | * [rdp->nxtlist, *rdp->nxttail[0]) temporarily | |
533 | */ | |
1f7b94cd | 534 | if (!rcu_batch_before(completed_snap, rdp->batch)) |
5127bed5 | 535 | rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2]; |
1f7b94cd | 536 | else if (!rcu_batch_before(completed_snap, rdp->batch - 1)) |
5127bed5 LJ |
537 | rdp->nxttail[0] = rdp->nxttail[1]; |
538 | ||
539 | /* | |
540 | * the grace period for entries in | |
541 | * [rdp->nxtlist, *rdp->nxttail[0]) has completed and | |
542 | * move these entries to donelist | |
01c1c660 | 543 | */ |
5127bed5 LJ |
544 | if (rdp->nxttail[0] != &rdp->nxtlist) { |
545 | *rdp->donetail = rdp->nxtlist; | |
546 | rdp->donetail = rdp->nxttail[0]; | |
547 | rdp->nxtlist = *rdp->nxttail[0]; | |
548 | *rdp->donetail = NULL; | |
549 | ||
550 | if (rdp->nxttail[1] == rdp->nxttail[0]) | |
551 | rdp->nxttail[1] = &rdp->nxtlist; | |
552 | if (rdp->nxttail[2] == rdp->nxttail[0]) | |
553 | rdp->nxttail[2] = &rdp->nxtlist; | |
554 | rdp->nxttail[0] = &rdp->nxtlist; | |
555 | } | |
01c1c660 | 556 | |
5127bed5 | 557 | local_irq_enable(); |
01c1c660 | 558 | |
3cac97cb | 559 | if (rcu_batch_after(rdp->batch, rcp->pending)) { |
0c925d79 HS |
560 | unsigned long flags; |
561 | ||
01c1c660 | 562 | /* and start it/schedule start if it's a new batch */ |
eff9b713 | 563 | spin_lock_irqsave(&rcp->lock, flags); |
3cac97cb LJ |
564 | if (rcu_batch_after(rdp->batch, rcp->pending)) { |
565 | rcp->pending = rdp->batch; | |
566 | rcu_start_batch(rcp); | |
567 | } | |
eff9b713 | 568 | spin_unlock_irqrestore(&rcp->lock, flags); |
01c1c660 PM |
569 | } |
570 | } | |
571 | ||
572 | rcu_check_quiescent_state(rcp, rdp); | |
573 | if (rdp->donelist) | |
574 | rcu_do_batch(rdp); | |
575 | } | |
576 | ||
577 | static void rcu_process_callbacks(struct softirq_action *unused) | |
578 | { | |
1f7b94cd PM |
579 | /* |
580 | * Memory references from any prior RCU read-side critical sections | |
581 | * executed by the interrupted code must be see before any RCU | |
582 | * grace-period manupulations below. | |
583 | */ | |
584 | ||
585 | smp_mb(); /* See above block comment. */ | |
586 | ||
01c1c660 PM |
587 | __rcu_process_callbacks(&rcu_ctrlblk, &__get_cpu_var(rcu_data)); |
588 | __rcu_process_callbacks(&rcu_bh_ctrlblk, &__get_cpu_var(rcu_bh_data)); | |
1f7b94cd PM |
589 | |
590 | /* | |
591 | * Memory references from any later RCU read-side critical sections | |
592 | * executed by the interrupted code must be see after any RCU | |
593 | * grace-period manupulations above. | |
594 | */ | |
595 | ||
596 | smp_mb(); /* See above block comment. */ | |
01c1c660 PM |
597 | } |
598 | ||
599 | static int __rcu_pending(struct rcu_ctrlblk *rcp, struct rcu_data *rdp) | |
600 | { | |
67182ae1 PM |
601 | /* Check for CPU stalls, if enabled. */ |
602 | check_cpu_stall(rcp, rdp); | |
603 | ||
5127bed5 | 604 | if (rdp->nxtlist) { |
1f7b94cd PM |
605 | long completed_snap = ACCESS_ONCE(rcp->completed); |
606 | ||
5127bed5 LJ |
607 | /* |
608 | * This cpu has pending rcu entries and the grace period | |
609 | * for them has completed. | |
610 | */ | |
1f7b94cd | 611 | if (!rcu_batch_before(completed_snap, rdp->batch)) |
5127bed5 | 612 | return 1; |
1f7b94cd | 613 | if (!rcu_batch_before(completed_snap, rdp->batch - 1) && |
5127bed5 LJ |
614 | rdp->nxttail[0] != rdp->nxttail[1]) |
615 | return 1; | |
616 | if (rdp->nxttail[0] != &rdp->nxtlist) | |
617 | return 1; | |
01c1c660 | 618 | |
5127bed5 LJ |
619 | /* |
620 | * This cpu has pending rcu entries and the new batch | |
621 | * for then hasn't been started nor scheduled start | |
622 | */ | |
623 | if (rcu_batch_after(rdp->batch, rcp->pending)) | |
624 | return 1; | |
625 | } | |
01c1c660 PM |
626 | |
627 | /* This cpu has finished callbacks to invoke */ | |
628 | if (rdp->donelist) | |
629 | return 1; | |
630 | ||
631 | /* The rcu core waits for a quiescent state from the cpu */ | |
632 | if (rdp->quiescbatch != rcp->cur || rdp->qs_pending) | |
633 | return 1; | |
634 | ||
635 | /* nothing to do */ | |
636 | return 0; | |
637 | } | |
638 | ||
639 | /* | |
640 | * Check to see if there is any immediate RCU-related work to be done | |
641 | * by the current CPU, returning 1 if so. This function is part of the | |
642 | * RCU implementation; it is -not- an exported member of the RCU API. | |
643 | */ | |
644 | int rcu_pending(int cpu) | |
645 | { | |
646 | return __rcu_pending(&rcu_ctrlblk, &per_cpu(rcu_data, cpu)) || | |
647 | __rcu_pending(&rcu_bh_ctrlblk, &per_cpu(rcu_bh_data, cpu)); | |
648 | } | |
649 | ||
650 | /* | |
651 | * Check to see if any future RCU-related work will need to be done | |
652 | * by the current CPU, even if none need be done immediately, returning | |
653 | * 1 if so. This function is part of the RCU implementation; it is -not- | |
654 | * an exported member of the RCU API. | |
655 | */ | |
656 | int rcu_needs_cpu(int cpu) | |
657 | { | |
658 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); | |
659 | struct rcu_data *rdp_bh = &per_cpu(rcu_bh_data, cpu); | |
660 | ||
5127bed5 | 661 | return !!rdp->nxtlist || !!rdp_bh->nxtlist || rcu_pending(cpu); |
01c1c660 PM |
662 | } |
663 | ||
1f7b94cd PM |
664 | /* |
665 | * Top-level function driving RCU grace-period detection, normally | |
666 | * invoked from the scheduler-clock interrupt. This function simply | |
667 | * increments counters that are read only from softirq by this same | |
668 | * CPU, so there are no memory barriers required. | |
669 | */ | |
01c1c660 PM |
670 | void rcu_check_callbacks(int cpu, int user) |
671 | { | |
672 | if (user || | |
673 | (idle_cpu(cpu) && !in_softirq() && | |
674 | hardirq_count() <= (1 << HARDIRQ_SHIFT))) { | |
8db559b8 PM |
675 | |
676 | /* | |
677 | * Get here if this CPU took its interrupt from user | |
678 | * mode or from the idle loop, and if this is not a | |
679 | * nested interrupt. In this case, the CPU is in | |
680 | * a quiescent state, so count it. | |
681 | * | |
682 | * Also do a memory barrier. This is needed to handle | |
683 | * the case where writes from a preempt-disable section | |
684 | * of code get reordered into schedule() by this CPU's | |
685 | * write buffer. The memory barrier makes sure that | |
686 | * the rcu_qsctr_inc() and rcu_bh_qsctr_inc() are see | |
687 | * by other CPUs to happen after any such write. | |
688 | */ | |
689 | ||
690 | smp_mb(); /* See above block comment. */ | |
01c1c660 PM |
691 | rcu_qsctr_inc(cpu); |
692 | rcu_bh_qsctr_inc(cpu); | |
8db559b8 PM |
693 | |
694 | } else if (!in_softirq()) { | |
695 | ||
696 | /* | |
697 | * Get here if this CPU did not take its interrupt from | |
698 | * softirq, in other words, if it is not interrupting | |
699 | * a rcu_bh read-side critical section. This is an _bh | |
700 | * critical section, so count it. The memory barrier | |
701 | * is needed for the same reason as is the above one. | |
702 | */ | |
703 | ||
704 | smp_mb(); /* See above block comment. */ | |
01c1c660 | 705 | rcu_bh_qsctr_inc(cpu); |
8db559b8 | 706 | } |
01c1c660 PM |
707 | raise_rcu_softirq(); |
708 | } | |
709 | ||
710 | static void rcu_init_percpu_data(int cpu, struct rcu_ctrlblk *rcp, | |
711 | struct rcu_data *rdp) | |
712 | { | |
cd958517 PM |
713 | long flags; |
714 | ||
715 | spin_lock_irqsave(&rcp->lock, flags); | |
01c1c660 | 716 | memset(rdp, 0, sizeof(*rdp)); |
5127bed5 | 717 | rdp->nxttail[0] = rdp->nxttail[1] = rdp->nxttail[2] = &rdp->nxtlist; |
01c1c660 PM |
718 | rdp->donetail = &rdp->donelist; |
719 | rdp->quiescbatch = rcp->completed; | |
720 | rdp->qs_pending = 0; | |
721 | rdp->cpu = cpu; | |
722 | rdp->blimit = blimit; | |
cd958517 | 723 | spin_unlock_irqrestore(&rcp->lock, flags); |
01c1c660 PM |
724 | } |
725 | ||
726 | static void __cpuinit rcu_online_cpu(int cpu) | |
727 | { | |
728 | struct rcu_data *rdp = &per_cpu(rcu_data, cpu); | |
729 | struct rcu_data *bh_rdp = &per_cpu(rcu_bh_data, cpu); | |
730 | ||
731 | rcu_init_percpu_data(cpu, &rcu_ctrlblk, rdp); | |
732 | rcu_init_percpu_data(cpu, &rcu_bh_ctrlblk, bh_rdp); | |
962cf36c | 733 | open_softirq(RCU_SOFTIRQ, rcu_process_callbacks); |
01c1c660 PM |
734 | } |
735 | ||
736 | static int __cpuinit rcu_cpu_notify(struct notifier_block *self, | |
737 | unsigned long action, void *hcpu) | |
738 | { | |
739 | long cpu = (long)hcpu; | |
740 | ||
741 | switch (action) { | |
742 | case CPU_UP_PREPARE: | |
743 | case CPU_UP_PREPARE_FROZEN: | |
744 | rcu_online_cpu(cpu); | |
745 | break; | |
746 | case CPU_DEAD: | |
747 | case CPU_DEAD_FROZEN: | |
748 | rcu_offline_cpu(cpu); | |
749 | break; | |
750 | default: | |
751 | break; | |
752 | } | |
753 | return NOTIFY_OK; | |
754 | } | |
755 | ||
756 | static struct notifier_block __cpuinitdata rcu_nb = { | |
757 | .notifier_call = rcu_cpu_notify, | |
758 | }; | |
759 | ||
760 | /* | |
761 | * Initializes rcu mechanism. Assumed to be called early. | |
762 | * That is before local timer(SMP) or jiffie timer (uniproc) is setup. | |
763 | * Note that rcu_qsctr and friends are implicitly | |
764 | * initialized due to the choice of ``0'' for RCU_CTR_INVALID. | |
765 | */ | |
766 | void __init __rcu_init(void) | |
767 | { | |
768 | rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, | |
769 | (void *)(long)smp_processor_id()); | |
770 | /* Register notifier for non-boot CPUs */ | |
771 | register_cpu_notifier(&rcu_nb); | |
772 | } | |
773 | ||
774 | module_param(blimit, int, 0); | |
775 | module_param(qhimark, int, 0); | |
776 | module_param(qlowmark, int, 0); |