]>
Commit | Line | Data |
---|---|---|
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, 2008 | |
19 | * | |
20 | * Authors: Dipankar Sarma <dipankar@in.ibm.com> | |
21 | * Manfred Spraul <manfred@colorfullife.com> | |
22 | * Paul E. McKenney <paulmck@linux.vnet.ibm.com> Hierarchical version | |
23 | * | |
24 | * Based on the original work by Paul McKenney <paulmck@us.ibm.com> | |
25 | * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen. | |
26 | * | |
27 | * For detailed explanation of Read-Copy Update mechanism see - | |
28 | * Documentation/RCU | |
29 | */ | |
30 | #include <linux/types.h> | |
31 | #include <linux/kernel.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/spinlock.h> | |
34 | #include <linux/smp.h> | |
35 | #include <linux/rcupdate.h> | |
36 | #include <linux/interrupt.h> | |
37 | #include <linux/sched.h> | |
38 | #include <linux/nmi.h> | |
39 | #include <linux/atomic.h> | |
40 | #include <linux/bitops.h> | |
41 | #include <linux/export.h> | |
42 | #include <linux/completion.h> | |
43 | #include <linux/moduleparam.h> | |
44 | #include <linux/percpu.h> | |
45 | #include <linux/notifier.h> | |
46 | #include <linux/cpu.h> | |
47 | #include <linux/mutex.h> | |
48 | #include <linux/time.h> | |
49 | #include <linux/kernel_stat.h> | |
50 | #include <linux/wait.h> | |
51 | #include <linux/kthread.h> | |
52 | #include <linux/prefetch.h> | |
53 | #include <linux/delay.h> | |
54 | #include <linux/stop_machine.h> | |
55 | #include <linux/random.h> | |
56 | ||
57 | #include "rcutree.h" | |
58 | #include <trace/events/rcu.h> | |
59 | ||
60 | #include "rcu.h" | |
61 | ||
62 | /* Data structures. */ | |
63 | ||
64 | static struct lock_class_key rcu_node_class[RCU_NUM_LVLS]; | |
65 | static struct lock_class_key rcu_fqs_class[RCU_NUM_LVLS]; | |
66 | ||
67 | #define RCU_STATE_INITIALIZER(sname, cr) { \ | |
68 | .level = { &sname##_state.node[0] }, \ | |
69 | .call = cr, \ | |
70 | .fqs_state = RCU_GP_IDLE, \ | |
71 | .gpnum = -300, \ | |
72 | .completed = -300, \ | |
73 | .onofflock = __RAW_SPIN_LOCK_UNLOCKED(&sname##_state.onofflock), \ | |
74 | .orphan_nxttail = &sname##_state.orphan_nxtlist, \ | |
75 | .orphan_donetail = &sname##_state.orphan_donelist, \ | |
76 | .barrier_mutex = __MUTEX_INITIALIZER(sname##_state.barrier_mutex), \ | |
77 | .name = #sname, \ | |
78 | } | |
79 | ||
80 | struct rcu_state rcu_sched_state = | |
81 | RCU_STATE_INITIALIZER(rcu_sched, call_rcu_sched); | |
82 | DEFINE_PER_CPU(struct rcu_data, rcu_sched_data); | |
83 | ||
84 | struct rcu_state rcu_bh_state = RCU_STATE_INITIALIZER(rcu_bh, call_rcu_bh); | |
85 | DEFINE_PER_CPU(struct rcu_data, rcu_bh_data); | |
86 | ||
87 | static struct rcu_state *rcu_state; | |
88 | LIST_HEAD(rcu_struct_flavors); | |
89 | ||
90 | /* Increase (but not decrease) the CONFIG_RCU_FANOUT_LEAF at boot time. */ | |
91 | static int rcu_fanout_leaf = CONFIG_RCU_FANOUT_LEAF; | |
92 | module_param(rcu_fanout_leaf, int, 0444); | |
93 | int rcu_num_lvls __read_mostly = RCU_NUM_LVLS; | |
94 | static int num_rcu_lvl[] = { /* Number of rcu_nodes at specified level. */ | |
95 | NUM_RCU_LVL_0, | |
96 | NUM_RCU_LVL_1, | |
97 | NUM_RCU_LVL_2, | |
98 | NUM_RCU_LVL_3, | |
99 | NUM_RCU_LVL_4, | |
100 | }; | |
101 | int rcu_num_nodes __read_mostly = NUM_RCU_NODES; /* Total # rcu_nodes in use. */ | |
102 | ||
103 | /* | |
104 | * The rcu_scheduler_active variable transitions from zero to one just | |
105 | * before the first task is spawned. So when this variable is zero, RCU | |
106 | * can assume that there is but one task, allowing RCU to (for example) | |
107 | * optimized synchronize_sched() to a simple barrier(). When this variable | |
108 | * is one, RCU must actually do all the hard work required to detect real | |
109 | * grace periods. This variable is also used to suppress boot-time false | |
110 | * positives from lockdep-RCU error checking. | |
111 | */ | |
112 | int rcu_scheduler_active __read_mostly; | |
113 | EXPORT_SYMBOL_GPL(rcu_scheduler_active); | |
114 | ||
115 | /* | |
116 | * The rcu_scheduler_fully_active variable transitions from zero to one | |
117 | * during the early_initcall() processing, which is after the scheduler | |
118 | * is capable of creating new tasks. So RCU processing (for example, | |
119 | * creating tasks for RCU priority boosting) must be delayed until after | |
120 | * rcu_scheduler_fully_active transitions from zero to one. We also | |
121 | * currently delay invocation of any RCU callbacks until after this point. | |
122 | * | |
123 | * It might later prove better for people registering RCU callbacks during | |
124 | * early boot to take responsibility for these callbacks, but one step at | |
125 | * a time. | |
126 | */ | |
127 | static int rcu_scheduler_fully_active __read_mostly; | |
128 | ||
129 | #ifdef CONFIG_RCU_BOOST | |
130 | ||
131 | /* | |
132 | * Control variables for per-CPU and per-rcu_node kthreads. These | |
133 | * handle all flavors of RCU. | |
134 | */ | |
135 | static DEFINE_PER_CPU(struct task_struct *, rcu_cpu_kthread_task); | |
136 | DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_status); | |
137 | DEFINE_PER_CPU(unsigned int, rcu_cpu_kthread_loops); | |
138 | DEFINE_PER_CPU(char, rcu_cpu_has_work); | |
139 | ||
140 | #endif /* #ifdef CONFIG_RCU_BOOST */ | |
141 | ||
142 | static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp, int outgoingcpu); | |
143 | static void invoke_rcu_core(void); | |
144 | static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp); | |
145 | ||
146 | /* | |
147 | * Track the rcutorture test sequence number and the update version | |
148 | * number within a given test. The rcutorture_testseq is incremented | |
149 | * on every rcutorture module load and unload, so has an odd value | |
150 | * when a test is running. The rcutorture_vernum is set to zero | |
151 | * when rcutorture starts and is incremented on each rcutorture update. | |
152 | * These variables enable correlating rcutorture output with the | |
153 | * RCU tracing information. | |
154 | */ | |
155 | unsigned long rcutorture_testseq; | |
156 | unsigned long rcutorture_vernum; | |
157 | ||
158 | /* | |
159 | * Return true if an RCU grace period is in progress. The ACCESS_ONCE()s | |
160 | * permit this function to be invoked without holding the root rcu_node | |
161 | * structure's ->lock, but of course results can be subject to change. | |
162 | */ | |
163 | static int rcu_gp_in_progress(struct rcu_state *rsp) | |
164 | { | |
165 | return ACCESS_ONCE(rsp->completed) != ACCESS_ONCE(rsp->gpnum); | |
166 | } | |
167 | ||
168 | /* | |
169 | * Note a quiescent state. Because we do not need to know | |
170 | * how many quiescent states passed, just if there was at least | |
171 | * one since the start of the grace period, this just sets a flag. | |
172 | * The caller must have disabled preemption. | |
173 | */ | |
174 | void rcu_sched_qs(int cpu) | |
175 | { | |
176 | struct rcu_data *rdp = &per_cpu(rcu_sched_data, cpu); | |
177 | ||
178 | if (rdp->passed_quiesce == 0) | |
179 | trace_rcu_grace_period("rcu_sched", rdp->gpnum, "cpuqs"); | |
180 | rdp->passed_quiesce = 1; | |
181 | } | |
182 | ||
183 | void rcu_bh_qs(int cpu) | |
184 | { | |
185 | struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu); | |
186 | ||
187 | if (rdp->passed_quiesce == 0) | |
188 | trace_rcu_grace_period("rcu_bh", rdp->gpnum, "cpuqs"); | |
189 | rdp->passed_quiesce = 1; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Note a context switch. This is a quiescent state for RCU-sched, | |
194 | * and requires special handling for preemptible RCU. | |
195 | * The caller must have disabled preemption. | |
196 | */ | |
197 | void rcu_note_context_switch(int cpu) | |
198 | { | |
199 | trace_rcu_utilization("Start context switch"); | |
200 | rcu_sched_qs(cpu); | |
201 | rcu_preempt_note_context_switch(cpu); | |
202 | trace_rcu_utilization("End context switch"); | |
203 | } | |
204 | EXPORT_SYMBOL_GPL(rcu_note_context_switch); | |
205 | ||
206 | DEFINE_PER_CPU(struct rcu_dynticks, rcu_dynticks) = { | |
207 | .dynticks_nesting = DYNTICK_TASK_EXIT_IDLE, | |
208 | .dynticks = ATOMIC_INIT(1), | |
209 | }; | |
210 | ||
211 | static int blimit = 10; /* Maximum callbacks per rcu_do_batch. */ | |
212 | static int qhimark = 10000; /* If this many pending, ignore blimit. */ | |
213 | static int qlowmark = 100; /* Once only this many pending, use blimit. */ | |
214 | ||
215 | module_param(blimit, int, 0444); | |
216 | module_param(qhimark, int, 0444); | |
217 | module_param(qlowmark, int, 0444); | |
218 | ||
219 | int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */ | |
220 | int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT; | |
221 | ||
222 | module_param(rcu_cpu_stall_suppress, int, 0644); | |
223 | module_param(rcu_cpu_stall_timeout, int, 0644); | |
224 | ||
225 | static ulong jiffies_till_first_fqs = RCU_JIFFIES_TILL_FORCE_QS; | |
226 | static ulong jiffies_till_next_fqs = RCU_JIFFIES_TILL_FORCE_QS; | |
227 | ||
228 | module_param(jiffies_till_first_fqs, ulong, 0644); | |
229 | module_param(jiffies_till_next_fqs, ulong, 0644); | |
230 | ||
231 | static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *)); | |
232 | static void force_quiescent_state(struct rcu_state *rsp); | |
233 | static int rcu_pending(int cpu); | |
234 | ||
235 | /* | |
236 | * Return the number of RCU-sched batches processed thus far for debug & stats. | |
237 | */ | |
238 | long rcu_batches_completed_sched(void) | |
239 | { | |
240 | return rcu_sched_state.completed; | |
241 | } | |
242 | EXPORT_SYMBOL_GPL(rcu_batches_completed_sched); | |
243 | ||
244 | /* | |
245 | * Return the number of RCU BH batches processed thus far for debug & stats. | |
246 | */ | |
247 | long rcu_batches_completed_bh(void) | |
248 | { | |
249 | return rcu_bh_state.completed; | |
250 | } | |
251 | EXPORT_SYMBOL_GPL(rcu_batches_completed_bh); | |
252 | ||
253 | /* | |
254 | * Force a quiescent state for RCU BH. | |
255 | */ | |
256 | void rcu_bh_force_quiescent_state(void) | |
257 | { | |
258 | force_quiescent_state(&rcu_bh_state); | |
259 | } | |
260 | EXPORT_SYMBOL_GPL(rcu_bh_force_quiescent_state); | |
261 | ||
262 | /* | |
263 | * Record the number of times rcutorture tests have been initiated and | |
264 | * terminated. This information allows the debugfs tracing stats to be | |
265 | * correlated to the rcutorture messages, even when the rcutorture module | |
266 | * is being repeatedly loaded and unloaded. In other words, we cannot | |
267 | * store this state in rcutorture itself. | |
268 | */ | |
269 | void rcutorture_record_test_transition(void) | |
270 | { | |
271 | rcutorture_testseq++; | |
272 | rcutorture_vernum = 0; | |
273 | } | |
274 | EXPORT_SYMBOL_GPL(rcutorture_record_test_transition); | |
275 | ||
276 | /* | |
277 | * Record the number of writer passes through the current rcutorture test. | |
278 | * This is also used to correlate debugfs tracing stats with the rcutorture | |
279 | * messages. | |
280 | */ | |
281 | void rcutorture_record_progress(unsigned long vernum) | |
282 | { | |
283 | rcutorture_vernum++; | |
284 | } | |
285 | EXPORT_SYMBOL_GPL(rcutorture_record_progress); | |
286 | ||
287 | /* | |
288 | * Force a quiescent state for RCU-sched. | |
289 | */ | |
290 | void rcu_sched_force_quiescent_state(void) | |
291 | { | |
292 | force_quiescent_state(&rcu_sched_state); | |
293 | } | |
294 | EXPORT_SYMBOL_GPL(rcu_sched_force_quiescent_state); | |
295 | ||
296 | /* | |
297 | * Does the CPU have callbacks ready to be invoked? | |
298 | */ | |
299 | static int | |
300 | cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp) | |
301 | { | |
302 | return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL]; | |
303 | } | |
304 | ||
305 | /* | |
306 | * Does the current CPU require a yet-as-unscheduled grace period? | |
307 | */ | |
308 | static int | |
309 | cpu_needs_another_gp(struct rcu_state *rsp, struct rcu_data *rdp) | |
310 | { | |
311 | return *rdp->nxttail[RCU_DONE_TAIL + | |
312 | ACCESS_ONCE(rsp->completed) != rdp->completed] && | |
313 | !rcu_gp_in_progress(rsp); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Return the root node of the specified rcu_state structure. | |
318 | */ | |
319 | static struct rcu_node *rcu_get_root(struct rcu_state *rsp) | |
320 | { | |
321 | return &rsp->node[0]; | |
322 | } | |
323 | ||
324 | /* | |
325 | * rcu_eqs_enter_common - current CPU is moving towards extended quiescent state | |
326 | * | |
327 | * If the new value of the ->dynticks_nesting counter now is zero, | |
328 | * we really have entered idle, and must do the appropriate accounting. | |
329 | * The caller must have disabled interrupts. | |
330 | */ | |
331 | static void rcu_eqs_enter_common(struct rcu_dynticks *rdtp, long long oldval, | |
332 | bool user) | |
333 | { | |
334 | trace_rcu_dyntick("Start", oldval, 0); | |
335 | if (!is_idle_task(current) && !user) { | |
336 | struct task_struct *idle = idle_task(smp_processor_id()); | |
337 | ||
338 | trace_rcu_dyntick("Error on entry: not idle task", oldval, 0); | |
339 | ftrace_dump(DUMP_ORIG); | |
340 | WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s", | |
341 | current->pid, current->comm, | |
342 | idle->pid, idle->comm); /* must be idle task! */ | |
343 | } | |
344 | rcu_prepare_for_idle(smp_processor_id()); | |
345 | /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */ | |
346 | smp_mb__before_atomic_inc(); /* See above. */ | |
347 | atomic_inc(&rdtp->dynticks); | |
348 | smp_mb__after_atomic_inc(); /* Force ordering with next sojourn. */ | |
349 | WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1); | |
350 | ||
351 | /* | |
352 | * It is illegal to enter an extended quiescent state while | |
353 | * in an RCU read-side critical section. | |
354 | */ | |
355 | rcu_lockdep_assert(!lock_is_held(&rcu_lock_map), | |
356 | "Illegal idle entry in RCU read-side critical section."); | |
357 | rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map), | |
358 | "Illegal idle entry in RCU-bh read-side critical section."); | |
359 | rcu_lockdep_assert(!lock_is_held(&rcu_sched_lock_map), | |
360 | "Illegal idle entry in RCU-sched read-side critical section."); | |
361 | } | |
362 | ||
363 | /* | |
364 | * Enter an RCU extended quiescent state, which can be either the | |
365 | * idle loop or adaptive-tickless usermode execution. | |
366 | */ | |
367 | static void rcu_eqs_enter(bool user) | |
368 | { | |
369 | long long oldval; | |
370 | struct rcu_dynticks *rdtp; | |
371 | ||
372 | rdtp = &__get_cpu_var(rcu_dynticks); | |
373 | oldval = rdtp->dynticks_nesting; | |
374 | WARN_ON_ONCE((oldval & DYNTICK_TASK_NEST_MASK) == 0); | |
375 | if ((oldval & DYNTICK_TASK_NEST_MASK) == DYNTICK_TASK_NEST_VALUE) | |
376 | rdtp->dynticks_nesting = 0; | |
377 | else | |
378 | rdtp->dynticks_nesting -= DYNTICK_TASK_NEST_VALUE; | |
379 | rcu_eqs_enter_common(rdtp, oldval, user); | |
380 | } | |
381 | ||
382 | /** | |
383 | * rcu_idle_enter - inform RCU that current CPU is entering idle | |
384 | * | |
385 | * Enter idle mode, in other words, -leave- the mode in which RCU | |
386 | * read-side critical sections can occur. (Though RCU read-side | |
387 | * critical sections can occur in irq handlers in idle, a possibility | |
388 | * handled by irq_enter() and irq_exit().) | |
389 | * | |
390 | * We crowbar the ->dynticks_nesting field to zero to allow for | |
391 | * the possibility of usermode upcalls having messed up our count | |
392 | * of interrupt nesting level during the prior busy period. | |
393 | */ | |
394 | void rcu_idle_enter(void) | |
395 | { | |
396 | unsigned long flags; | |
397 | ||
398 | local_irq_save(flags); | |
399 | rcu_eqs_enter(0); | |
400 | local_irq_restore(flags); | |
401 | } | |
402 | EXPORT_SYMBOL_GPL(rcu_idle_enter); | |
403 | ||
404 | #ifdef CONFIG_RCU_USER_QS | |
405 | /** | |
406 | * rcu_user_enter - inform RCU that we are resuming userspace. | |
407 | * | |
408 | * Enter RCU idle mode right before resuming userspace. No use of RCU | |
409 | * is permitted between this call and rcu_user_exit(). This way the | |
410 | * CPU doesn't need to maintain the tick for RCU maintenance purposes | |
411 | * when the CPU runs in userspace. | |
412 | */ | |
413 | void rcu_user_enter(void) | |
414 | { | |
415 | unsigned long flags; | |
416 | struct rcu_dynticks *rdtp; | |
417 | ||
418 | /* | |
419 | * Some contexts may involve an exception occuring in an irq, | |
420 | * leading to that nesting: | |
421 | * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit() | |
422 | * This would mess up the dyntick_nesting count though. And rcu_irq_*() | |
423 | * helpers are enough to protect RCU uses inside the exception. So | |
424 | * just return immediately if we detect we are in an IRQ. | |
425 | */ | |
426 | if (in_interrupt()) | |
427 | return; | |
428 | ||
429 | WARN_ON_ONCE(!current->mm); | |
430 | ||
431 | local_irq_save(flags); | |
432 | rdtp = &__get_cpu_var(rcu_dynticks); | |
433 | if (!rdtp->in_user) { | |
434 | rdtp->in_user = true; | |
435 | rcu_eqs_enter(1); | |
436 | } | |
437 | local_irq_restore(flags); | |
438 | } | |
439 | ||
440 | /** | |
441 | * rcu_user_enter_after_irq - inform RCU that we are going to resume userspace | |
442 | * after the current irq returns. | |
443 | * | |
444 | * This is similar to rcu_user_enter() but in the context of a non-nesting | |
445 | * irq. After this call, RCU enters into idle mode when the interrupt | |
446 | * returns. | |
447 | */ | |
448 | void rcu_user_enter_after_irq(void) | |
449 | { | |
450 | unsigned long flags; | |
451 | struct rcu_dynticks *rdtp; | |
452 | ||
453 | local_irq_save(flags); | |
454 | rdtp = &__get_cpu_var(rcu_dynticks); | |
455 | /* Ensure this irq is interrupting a non-idle RCU state. */ | |
456 | WARN_ON_ONCE(!(rdtp->dynticks_nesting & DYNTICK_TASK_MASK)); | |
457 | rdtp->dynticks_nesting = 1; | |
458 | local_irq_restore(flags); | |
459 | } | |
460 | #endif /* CONFIG_RCU_USER_QS */ | |
461 | ||
462 | /** | |
463 | * rcu_irq_exit - inform RCU that current CPU is exiting irq towards idle | |
464 | * | |
465 | * Exit from an interrupt handler, which might possibly result in entering | |
466 | * idle mode, in other words, leaving the mode in which read-side critical | |
467 | * sections can occur. | |
468 | * | |
469 | * This code assumes that the idle loop never does anything that might | |
470 | * result in unbalanced calls to irq_enter() and irq_exit(). If your | |
471 | * architecture violates this assumption, RCU will give you what you | |
472 | * deserve, good and hard. But very infrequently and irreproducibly. | |
473 | * | |
474 | * Use things like work queues to work around this limitation. | |
475 | * | |
476 | * You have been warned. | |
477 | */ | |
478 | void rcu_irq_exit(void) | |
479 | { | |
480 | unsigned long flags; | |
481 | long long oldval; | |
482 | struct rcu_dynticks *rdtp; | |
483 | ||
484 | local_irq_save(flags); | |
485 | rdtp = &__get_cpu_var(rcu_dynticks); | |
486 | oldval = rdtp->dynticks_nesting; | |
487 | rdtp->dynticks_nesting--; | |
488 | WARN_ON_ONCE(rdtp->dynticks_nesting < 0); | |
489 | if (rdtp->dynticks_nesting) | |
490 | trace_rcu_dyntick("--=", oldval, rdtp->dynticks_nesting); | |
491 | else | |
492 | rcu_eqs_enter_common(rdtp, oldval, 1); | |
493 | local_irq_restore(flags); | |
494 | } | |
495 | ||
496 | /* | |
497 | * rcu_eqs_exit_common - current CPU moving away from extended quiescent state | |
498 | * | |
499 | * If the new value of the ->dynticks_nesting counter was previously zero, | |
500 | * we really have exited idle, and must do the appropriate accounting. | |
501 | * The caller must have disabled interrupts. | |
502 | */ | |
503 | static void rcu_eqs_exit_common(struct rcu_dynticks *rdtp, long long oldval, | |
504 | int user) | |
505 | { | |
506 | smp_mb__before_atomic_inc(); /* Force ordering w/previous sojourn. */ | |
507 | atomic_inc(&rdtp->dynticks); | |
508 | /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */ | |
509 | smp_mb__after_atomic_inc(); /* See above. */ | |
510 | WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1)); | |
511 | rcu_cleanup_after_idle(smp_processor_id()); | |
512 | trace_rcu_dyntick("End", oldval, rdtp->dynticks_nesting); | |
513 | if (!is_idle_task(current) && !user) { | |
514 | struct task_struct *idle = idle_task(smp_processor_id()); | |
515 | ||
516 | trace_rcu_dyntick("Error on exit: not idle task", | |
517 | oldval, rdtp->dynticks_nesting); | |
518 | ftrace_dump(DUMP_ORIG); | |
519 | WARN_ONCE(1, "Current pid: %d comm: %s / Idle pid: %d comm: %s", | |
520 | current->pid, current->comm, | |
521 | idle->pid, idle->comm); /* must be idle task! */ | |
522 | } | |
523 | } | |
524 | ||
525 | /* | |
526 | * Exit an RCU extended quiescent state, which can be either the | |
527 | * idle loop or adaptive-tickless usermode execution. | |
528 | */ | |
529 | static void rcu_eqs_exit(bool user) | |
530 | { | |
531 | struct rcu_dynticks *rdtp; | |
532 | long long oldval; | |
533 | ||
534 | rdtp = &__get_cpu_var(rcu_dynticks); | |
535 | oldval = rdtp->dynticks_nesting; | |
536 | WARN_ON_ONCE(oldval < 0); | |
537 | if (oldval & DYNTICK_TASK_NEST_MASK) | |
538 | rdtp->dynticks_nesting += DYNTICK_TASK_NEST_VALUE; | |
539 | else | |
540 | rdtp->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE; | |
541 | rcu_eqs_exit_common(rdtp, oldval, user); | |
542 | } | |
543 | ||
544 | /** | |
545 | * rcu_idle_exit - inform RCU that current CPU is leaving idle | |
546 | * | |
547 | * Exit idle mode, in other words, -enter- the mode in which RCU | |
548 | * read-side critical sections can occur. | |
549 | * | |
550 | * We crowbar the ->dynticks_nesting field to DYNTICK_TASK_NEST to | |
551 | * allow for the possibility of usermode upcalls messing up our count | |
552 | * of interrupt nesting level during the busy period that is just | |
553 | * now starting. | |
554 | */ | |
555 | void rcu_idle_exit(void) | |
556 | { | |
557 | unsigned long flags; | |
558 | ||
559 | local_irq_save(flags); | |
560 | rcu_eqs_exit(0); | |
561 | local_irq_restore(flags); | |
562 | } | |
563 | EXPORT_SYMBOL_GPL(rcu_idle_exit); | |
564 | ||
565 | #ifdef CONFIG_RCU_USER_QS | |
566 | /** | |
567 | * rcu_user_exit - inform RCU that we are exiting userspace. | |
568 | * | |
569 | * Exit RCU idle mode while entering the kernel because it can | |
570 | * run a RCU read side critical section anytime. | |
571 | */ | |
572 | void rcu_user_exit(void) | |
573 | { | |
574 | unsigned long flags; | |
575 | struct rcu_dynticks *rdtp; | |
576 | ||
577 | /* | |
578 | * Some contexts may involve an exception occuring in an irq, | |
579 | * leading to that nesting: | |
580 | * rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit() | |
581 | * This would mess up the dyntick_nesting count though. And rcu_irq_*() | |
582 | * helpers are enough to protect RCU uses inside the exception. So | |
583 | * just return immediately if we detect we are in an IRQ. | |
584 | */ | |
585 | if (in_interrupt()) | |
586 | return; | |
587 | ||
588 | local_irq_save(flags); | |
589 | rdtp = &__get_cpu_var(rcu_dynticks); | |
590 | if (rdtp->in_user) { | |
591 | rdtp->in_user = false; | |
592 | rcu_eqs_exit(1); | |
593 | } | |
594 | local_irq_restore(flags); | |
595 | } | |
596 | ||
597 | /** | |
598 | * rcu_user_exit_after_irq - inform RCU that we won't resume to userspace | |
599 | * idle mode after the current non-nesting irq returns. | |
600 | * | |
601 | * This is similar to rcu_user_exit() but in the context of an irq. | |
602 | * This is called when the irq has interrupted a userspace RCU idle mode | |
603 | * context. When the current non-nesting interrupt returns after this call, | |
604 | * the CPU won't restore the RCU idle mode. | |
605 | */ | |
606 | void rcu_user_exit_after_irq(void) | |
607 | { | |
608 | unsigned long flags; | |
609 | struct rcu_dynticks *rdtp; | |
610 | ||
611 | local_irq_save(flags); | |
612 | rdtp = &__get_cpu_var(rcu_dynticks); | |
613 | /* Ensure we are interrupting an RCU idle mode. */ | |
614 | WARN_ON_ONCE(rdtp->dynticks_nesting & DYNTICK_TASK_NEST_MASK); | |
615 | rdtp->dynticks_nesting += DYNTICK_TASK_EXIT_IDLE; | |
616 | local_irq_restore(flags); | |
617 | } | |
618 | #endif /* CONFIG_RCU_USER_QS */ | |
619 | ||
620 | /** | |
621 | * rcu_irq_enter - inform RCU that current CPU is entering irq away from idle | |
622 | * | |
623 | * Enter an interrupt handler, which might possibly result in exiting | |
624 | * idle mode, in other words, entering the mode in which read-side critical | |
625 | * sections can occur. | |
626 | * | |
627 | * Note that the Linux kernel is fully capable of entering an interrupt | |
628 | * handler that it never exits, for example when doing upcalls to | |
629 | * user mode! This code assumes that the idle loop never does upcalls to | |
630 | * user mode. If your architecture does do upcalls from the idle loop (or | |
631 | * does anything else that results in unbalanced calls to the irq_enter() | |
632 | * and irq_exit() functions), RCU will give you what you deserve, good | |
633 | * and hard. But very infrequently and irreproducibly. | |
634 | * | |
635 | * Use things like work queues to work around this limitation. | |
636 | * | |
637 | * You have been warned. | |
638 | */ | |
639 | void rcu_irq_enter(void) | |
640 | { | |
641 | unsigned long flags; | |
642 | struct rcu_dynticks *rdtp; | |
643 | long long oldval; | |
644 | ||
645 | local_irq_save(flags); | |
646 | rdtp = &__get_cpu_var(rcu_dynticks); | |
647 | oldval = rdtp->dynticks_nesting; | |
648 | rdtp->dynticks_nesting++; | |
649 | WARN_ON_ONCE(rdtp->dynticks_nesting == 0); | |
650 | if (oldval) | |
651 | trace_rcu_dyntick("++=", oldval, rdtp->dynticks_nesting); | |
652 | else | |
653 | rcu_eqs_exit_common(rdtp, oldval, 1); | |
654 | local_irq_restore(flags); | |
655 | } | |
656 | ||
657 | /** | |
658 | * rcu_nmi_enter - inform RCU of entry to NMI context | |
659 | * | |
660 | * If the CPU was idle with dynamic ticks active, and there is no | |
661 | * irq handler running, this updates rdtp->dynticks_nmi to let the | |
662 | * RCU grace-period handling know that the CPU is active. | |
663 | */ | |
664 | void rcu_nmi_enter(void) | |
665 | { | |
666 | struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks); | |
667 | ||
668 | if (rdtp->dynticks_nmi_nesting == 0 && | |
669 | (atomic_read(&rdtp->dynticks) & 0x1)) | |
670 | return; | |
671 | rdtp->dynticks_nmi_nesting++; | |
672 | smp_mb__before_atomic_inc(); /* Force delay from prior write. */ | |
673 | atomic_inc(&rdtp->dynticks); | |
674 | /* CPUs seeing atomic_inc() must see later RCU read-side crit sects */ | |
675 | smp_mb__after_atomic_inc(); /* See above. */ | |
676 | WARN_ON_ONCE(!(atomic_read(&rdtp->dynticks) & 0x1)); | |
677 | } | |
678 | ||
679 | /** | |
680 | * rcu_nmi_exit - inform RCU of exit from NMI context | |
681 | * | |
682 | * If the CPU was idle with dynamic ticks active, and there is no | |
683 | * irq handler running, this updates rdtp->dynticks_nmi to let the | |
684 | * RCU grace-period handling know that the CPU is no longer active. | |
685 | */ | |
686 | void rcu_nmi_exit(void) | |
687 | { | |
688 | struct rcu_dynticks *rdtp = &__get_cpu_var(rcu_dynticks); | |
689 | ||
690 | if (rdtp->dynticks_nmi_nesting == 0 || | |
691 | --rdtp->dynticks_nmi_nesting != 0) | |
692 | return; | |
693 | /* CPUs seeing atomic_inc() must see prior RCU read-side crit sects */ | |
694 | smp_mb__before_atomic_inc(); /* See above. */ | |
695 | atomic_inc(&rdtp->dynticks); | |
696 | smp_mb__after_atomic_inc(); /* Force delay to next write. */ | |
697 | WARN_ON_ONCE(atomic_read(&rdtp->dynticks) & 0x1); | |
698 | } | |
699 | ||
700 | /** | |
701 | * rcu_is_cpu_idle - see if RCU thinks that the current CPU is idle | |
702 | * | |
703 | * If the current CPU is in its idle loop and is neither in an interrupt | |
704 | * or NMI handler, return true. | |
705 | */ | |
706 | int rcu_is_cpu_idle(void) | |
707 | { | |
708 | int ret; | |
709 | ||
710 | preempt_disable(); | |
711 | ret = (atomic_read(&__get_cpu_var(rcu_dynticks).dynticks) & 0x1) == 0; | |
712 | preempt_enable(); | |
713 | return ret; | |
714 | } | |
715 | EXPORT_SYMBOL(rcu_is_cpu_idle); | |
716 | ||
717 | #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) | |
718 | ||
719 | /* | |
720 | * Is the current CPU online? Disable preemption to avoid false positives | |
721 | * that could otherwise happen due to the current CPU number being sampled, | |
722 | * this task being preempted, its old CPU being taken offline, resuming | |
723 | * on some other CPU, then determining that its old CPU is now offline. | |
724 | * It is OK to use RCU on an offline processor during initial boot, hence | |
725 | * the check for rcu_scheduler_fully_active. Note also that it is OK | |
726 | * for a CPU coming online to use RCU for one jiffy prior to marking itself | |
727 | * online in the cpu_online_mask. Similarly, it is OK for a CPU going | |
728 | * offline to continue to use RCU for one jiffy after marking itself | |
729 | * offline in the cpu_online_mask. This leniency is necessary given the | |
730 | * non-atomic nature of the online and offline processing, for example, | |
731 | * the fact that a CPU enters the scheduler after completing the CPU_DYING | |
732 | * notifiers. | |
733 | * | |
734 | * This is also why RCU internally marks CPUs online during the | |
735 | * CPU_UP_PREPARE phase and offline during the CPU_DEAD phase. | |
736 | * | |
737 | * Disable checking if in an NMI handler because we cannot safely report | |
738 | * errors from NMI handlers anyway. | |
739 | */ | |
740 | bool rcu_lockdep_current_cpu_online(void) | |
741 | { | |
742 | struct rcu_data *rdp; | |
743 | struct rcu_node *rnp; | |
744 | bool ret; | |
745 | ||
746 | if (in_nmi()) | |
747 | return 1; | |
748 | preempt_disable(); | |
749 | rdp = &__get_cpu_var(rcu_sched_data); | |
750 | rnp = rdp->mynode; | |
751 | ret = (rdp->grpmask & rnp->qsmaskinit) || | |
752 | !rcu_scheduler_fully_active; | |
753 | preempt_enable(); | |
754 | return ret; | |
755 | } | |
756 | EXPORT_SYMBOL_GPL(rcu_lockdep_current_cpu_online); | |
757 | ||
758 | #endif /* #if defined(CONFIG_PROVE_RCU) && defined(CONFIG_HOTPLUG_CPU) */ | |
759 | ||
760 | /** | |
761 | * rcu_is_cpu_rrupt_from_idle - see if idle or immediately interrupted from idle | |
762 | * | |
763 | * If the current CPU is idle or running at a first-level (not nested) | |
764 | * interrupt from idle, return true. The caller must have at least | |
765 | * disabled preemption. | |
766 | */ | |
767 | int rcu_is_cpu_rrupt_from_idle(void) | |
768 | { | |
769 | return __get_cpu_var(rcu_dynticks).dynticks_nesting <= 1; | |
770 | } | |
771 | ||
772 | /* | |
773 | * Snapshot the specified CPU's dynticks counter so that we can later | |
774 | * credit them with an implicit quiescent state. Return 1 if this CPU | |
775 | * is in dynticks idle mode, which is an extended quiescent state. | |
776 | */ | |
777 | static int dyntick_save_progress_counter(struct rcu_data *rdp) | |
778 | { | |
779 | rdp->dynticks_snap = atomic_add_return(0, &rdp->dynticks->dynticks); | |
780 | return (rdp->dynticks_snap & 0x1) == 0; | |
781 | } | |
782 | ||
783 | /* | |
784 | * Return true if the specified CPU has passed through a quiescent | |
785 | * state by virtue of being in or having passed through an dynticks | |
786 | * idle state since the last call to dyntick_save_progress_counter() | |
787 | * for this same CPU, or by virtue of having been offline. | |
788 | */ | |
789 | static int rcu_implicit_dynticks_qs(struct rcu_data *rdp) | |
790 | { | |
791 | unsigned int curr; | |
792 | unsigned int snap; | |
793 | ||
794 | curr = (unsigned int)atomic_add_return(0, &rdp->dynticks->dynticks); | |
795 | snap = (unsigned int)rdp->dynticks_snap; | |
796 | ||
797 | /* | |
798 | * If the CPU passed through or entered a dynticks idle phase with | |
799 | * no active irq/NMI handlers, then we can safely pretend that the CPU | |
800 | * already acknowledged the request to pass through a quiescent | |
801 | * state. Either way, that CPU cannot possibly be in an RCU | |
802 | * read-side critical section that started before the beginning | |
803 | * of the current RCU grace period. | |
804 | */ | |
805 | if ((curr & 0x1) == 0 || UINT_CMP_GE(curr, snap + 2)) { | |
806 | trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "dti"); | |
807 | rdp->dynticks_fqs++; | |
808 | return 1; | |
809 | } | |
810 | ||
811 | /* | |
812 | * Check for the CPU being offline, but only if the grace period | |
813 | * is old enough. We don't need to worry about the CPU changing | |
814 | * state: If we see it offline even once, it has been through a | |
815 | * quiescent state. | |
816 | * | |
817 | * The reason for insisting that the grace period be at least | |
818 | * one jiffy old is that CPUs that are not quite online and that | |
819 | * have just gone offline can still execute RCU read-side critical | |
820 | * sections. | |
821 | */ | |
822 | if (ULONG_CMP_GE(rdp->rsp->gp_start + 2, jiffies)) | |
823 | return 0; /* Grace period is not old enough. */ | |
824 | barrier(); | |
825 | if (cpu_is_offline(rdp->cpu)) { | |
826 | trace_rcu_fqs(rdp->rsp->name, rdp->gpnum, rdp->cpu, "ofl"); | |
827 | rdp->offline_fqs++; | |
828 | return 1; | |
829 | } | |
830 | return 0; | |
831 | } | |
832 | ||
833 | static int jiffies_till_stall_check(void) | |
834 | { | |
835 | int till_stall_check = ACCESS_ONCE(rcu_cpu_stall_timeout); | |
836 | ||
837 | /* | |
838 | * Limit check must be consistent with the Kconfig limits | |
839 | * for CONFIG_RCU_CPU_STALL_TIMEOUT. | |
840 | */ | |
841 | if (till_stall_check < 3) { | |
842 | ACCESS_ONCE(rcu_cpu_stall_timeout) = 3; | |
843 | till_stall_check = 3; | |
844 | } else if (till_stall_check > 300) { | |
845 | ACCESS_ONCE(rcu_cpu_stall_timeout) = 300; | |
846 | till_stall_check = 300; | |
847 | } | |
848 | return till_stall_check * HZ + RCU_STALL_DELAY_DELTA; | |
849 | } | |
850 | ||
851 | static void record_gp_stall_check_time(struct rcu_state *rsp) | |
852 | { | |
853 | rsp->gp_start = jiffies; | |
854 | rsp->jiffies_stall = jiffies + jiffies_till_stall_check(); | |
855 | } | |
856 | ||
857 | static void print_other_cpu_stall(struct rcu_state *rsp) | |
858 | { | |
859 | int cpu; | |
860 | long delta; | |
861 | unsigned long flags; | |
862 | int ndetected = 0; | |
863 | struct rcu_node *rnp = rcu_get_root(rsp); | |
864 | ||
865 | /* Only let one CPU complain about others per time interval. */ | |
866 | ||
867 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
868 | delta = jiffies - rsp->jiffies_stall; | |
869 | if (delta < RCU_STALL_RAT_DELAY || !rcu_gp_in_progress(rsp)) { | |
870 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
871 | return; | |
872 | } | |
873 | rsp->jiffies_stall = jiffies + 3 * jiffies_till_stall_check() + 3; | |
874 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
875 | ||
876 | /* | |
877 | * OK, time to rat on our buddy... | |
878 | * See Documentation/RCU/stallwarn.txt for info on how to debug | |
879 | * RCU CPU stall warnings. | |
880 | */ | |
881 | printk(KERN_ERR "INFO: %s detected stalls on CPUs/tasks:", | |
882 | rsp->name); | |
883 | print_cpu_stall_info_begin(); | |
884 | rcu_for_each_leaf_node(rsp, rnp) { | |
885 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
886 | ndetected += rcu_print_task_stall(rnp); | |
887 | if (rnp->qsmask != 0) { | |
888 | for (cpu = 0; cpu <= rnp->grphi - rnp->grplo; cpu++) | |
889 | if (rnp->qsmask & (1UL << cpu)) { | |
890 | print_cpu_stall_info(rsp, | |
891 | rnp->grplo + cpu); | |
892 | ndetected++; | |
893 | } | |
894 | } | |
895 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
896 | } | |
897 | ||
898 | /* | |
899 | * Now rat on any tasks that got kicked up to the root rcu_node | |
900 | * due to CPU offlining. | |
901 | */ | |
902 | rnp = rcu_get_root(rsp); | |
903 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
904 | ndetected += rcu_print_task_stall(rnp); | |
905 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
906 | ||
907 | print_cpu_stall_info_end(); | |
908 | printk(KERN_CONT "(detected by %d, t=%ld jiffies)\n", | |
909 | smp_processor_id(), (long)(jiffies - rsp->gp_start)); | |
910 | if (ndetected == 0) | |
911 | printk(KERN_ERR "INFO: Stall ended before state dump start\n"); | |
912 | else if (!trigger_all_cpu_backtrace()) | |
913 | dump_stack(); | |
914 | ||
915 | /* Complain about tasks blocking the grace period. */ | |
916 | ||
917 | rcu_print_detail_task_stall(rsp); | |
918 | ||
919 | force_quiescent_state(rsp); /* Kick them all. */ | |
920 | } | |
921 | ||
922 | static void print_cpu_stall(struct rcu_state *rsp) | |
923 | { | |
924 | unsigned long flags; | |
925 | struct rcu_node *rnp = rcu_get_root(rsp); | |
926 | ||
927 | /* | |
928 | * OK, time to rat on ourselves... | |
929 | * See Documentation/RCU/stallwarn.txt for info on how to debug | |
930 | * RCU CPU stall warnings. | |
931 | */ | |
932 | printk(KERN_ERR "INFO: %s self-detected stall on CPU", rsp->name); | |
933 | print_cpu_stall_info_begin(); | |
934 | print_cpu_stall_info(rsp, smp_processor_id()); | |
935 | print_cpu_stall_info_end(); | |
936 | printk(KERN_CONT " (t=%lu jiffies)\n", jiffies - rsp->gp_start); | |
937 | if (!trigger_all_cpu_backtrace()) | |
938 | dump_stack(); | |
939 | ||
940 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
941 | if (ULONG_CMP_GE(jiffies, rsp->jiffies_stall)) | |
942 | rsp->jiffies_stall = jiffies + | |
943 | 3 * jiffies_till_stall_check() + 3; | |
944 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
945 | ||
946 | set_need_resched(); /* kick ourselves to get things going. */ | |
947 | } | |
948 | ||
949 | static void check_cpu_stall(struct rcu_state *rsp, struct rcu_data *rdp) | |
950 | { | |
951 | unsigned long j; | |
952 | unsigned long js; | |
953 | struct rcu_node *rnp; | |
954 | ||
955 | if (rcu_cpu_stall_suppress) | |
956 | return; | |
957 | j = ACCESS_ONCE(jiffies); | |
958 | js = ACCESS_ONCE(rsp->jiffies_stall); | |
959 | rnp = rdp->mynode; | |
960 | if (rcu_gp_in_progress(rsp) && | |
961 | (ACCESS_ONCE(rnp->qsmask) & rdp->grpmask) && ULONG_CMP_GE(j, js)) { | |
962 | ||
963 | /* We haven't checked in, so go dump stack. */ | |
964 | print_cpu_stall(rsp); | |
965 | ||
966 | } else if (rcu_gp_in_progress(rsp) && | |
967 | ULONG_CMP_GE(j, js + RCU_STALL_RAT_DELAY)) { | |
968 | ||
969 | /* They had a few time units to dump stack, so complain. */ | |
970 | print_other_cpu_stall(rsp); | |
971 | } | |
972 | } | |
973 | ||
974 | static int rcu_panic(struct notifier_block *this, unsigned long ev, void *ptr) | |
975 | { | |
976 | rcu_cpu_stall_suppress = 1; | |
977 | return NOTIFY_DONE; | |
978 | } | |
979 | ||
980 | /** | |
981 | * rcu_cpu_stall_reset - prevent further stall warnings in current grace period | |
982 | * | |
983 | * Set the stall-warning timeout way off into the future, thus preventing | |
984 | * any RCU CPU stall-warning messages from appearing in the current set of | |
985 | * RCU grace periods. | |
986 | * | |
987 | * The caller must disable hard irqs. | |
988 | */ | |
989 | void rcu_cpu_stall_reset(void) | |
990 | { | |
991 | struct rcu_state *rsp; | |
992 | ||
993 | for_each_rcu_flavor(rsp) | |
994 | rsp->jiffies_stall = jiffies + ULONG_MAX / 2; | |
995 | } | |
996 | ||
997 | static struct notifier_block rcu_panic_block = { | |
998 | .notifier_call = rcu_panic, | |
999 | }; | |
1000 | ||
1001 | static void __init check_cpu_stall_init(void) | |
1002 | { | |
1003 | atomic_notifier_chain_register(&panic_notifier_list, &rcu_panic_block); | |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * Update CPU-local rcu_data state to record the newly noticed grace period. | |
1008 | * This is used both when we started the grace period and when we notice | |
1009 | * that someone else started the grace period. The caller must hold the | |
1010 | * ->lock of the leaf rcu_node structure corresponding to the current CPU, | |
1011 | * and must have irqs disabled. | |
1012 | */ | |
1013 | static void __note_new_gpnum(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp) | |
1014 | { | |
1015 | if (rdp->gpnum != rnp->gpnum) { | |
1016 | /* | |
1017 | * If the current grace period is waiting for this CPU, | |
1018 | * set up to detect a quiescent state, otherwise don't | |
1019 | * go looking for one. | |
1020 | */ | |
1021 | rdp->gpnum = rnp->gpnum; | |
1022 | trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpustart"); | |
1023 | rdp->passed_quiesce = 0; | |
1024 | rdp->qs_pending = !!(rnp->qsmask & rdp->grpmask); | |
1025 | zero_cpu_stall_ticks(rdp); | |
1026 | } | |
1027 | } | |
1028 | ||
1029 | static void note_new_gpnum(struct rcu_state *rsp, struct rcu_data *rdp) | |
1030 | { | |
1031 | unsigned long flags; | |
1032 | struct rcu_node *rnp; | |
1033 | ||
1034 | local_irq_save(flags); | |
1035 | rnp = rdp->mynode; | |
1036 | if (rdp->gpnum == ACCESS_ONCE(rnp->gpnum) || /* outside lock. */ | |
1037 | !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */ | |
1038 | local_irq_restore(flags); | |
1039 | return; | |
1040 | } | |
1041 | __note_new_gpnum(rsp, rnp, rdp); | |
1042 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1043 | } | |
1044 | ||
1045 | /* | |
1046 | * Did someone else start a new RCU grace period start since we last | |
1047 | * checked? Update local state appropriately if so. Must be called | |
1048 | * on the CPU corresponding to rdp. | |
1049 | */ | |
1050 | static int | |
1051 | check_for_new_grace_period(struct rcu_state *rsp, struct rcu_data *rdp) | |
1052 | { | |
1053 | unsigned long flags; | |
1054 | int ret = 0; | |
1055 | ||
1056 | local_irq_save(flags); | |
1057 | if (rdp->gpnum != rsp->gpnum) { | |
1058 | note_new_gpnum(rsp, rdp); | |
1059 | ret = 1; | |
1060 | } | |
1061 | local_irq_restore(flags); | |
1062 | return ret; | |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * Initialize the specified rcu_data structure's callback list to empty. | |
1067 | */ | |
1068 | static void init_callback_list(struct rcu_data *rdp) | |
1069 | { | |
1070 | int i; | |
1071 | ||
1072 | rdp->nxtlist = NULL; | |
1073 | for (i = 0; i < RCU_NEXT_SIZE; i++) | |
1074 | rdp->nxttail[i] = &rdp->nxtlist; | |
1075 | } | |
1076 | ||
1077 | /* | |
1078 | * Advance this CPU's callbacks, but only if the current grace period | |
1079 | * has ended. This may be called only from the CPU to whom the rdp | |
1080 | * belongs. In addition, the corresponding leaf rcu_node structure's | |
1081 | * ->lock must be held by the caller, with irqs disabled. | |
1082 | */ | |
1083 | static void | |
1084 | __rcu_process_gp_end(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp) | |
1085 | { | |
1086 | /* Did another grace period end? */ | |
1087 | if (rdp->completed != rnp->completed) { | |
1088 | ||
1089 | /* Advance callbacks. No harm if list empty. */ | |
1090 | rdp->nxttail[RCU_DONE_TAIL] = rdp->nxttail[RCU_WAIT_TAIL]; | |
1091 | rdp->nxttail[RCU_WAIT_TAIL] = rdp->nxttail[RCU_NEXT_READY_TAIL]; | |
1092 | rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL]; | |
1093 | ||
1094 | /* Remember that we saw this grace-period completion. */ | |
1095 | rdp->completed = rnp->completed; | |
1096 | trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuend"); | |
1097 | ||
1098 | /* | |
1099 | * If we were in an extended quiescent state, we may have | |
1100 | * missed some grace periods that others CPUs handled on | |
1101 | * our behalf. Catch up with this state to avoid noting | |
1102 | * spurious new grace periods. If another grace period | |
1103 | * has started, then rnp->gpnum will have advanced, so | |
1104 | * we will detect this later on. Of course, any quiescent | |
1105 | * states we found for the old GP are now invalid. | |
1106 | */ | |
1107 | if (ULONG_CMP_LT(rdp->gpnum, rdp->completed)) { | |
1108 | rdp->gpnum = rdp->completed; | |
1109 | rdp->passed_quiesce = 0; | |
1110 | } | |
1111 | ||
1112 | /* | |
1113 | * If RCU does not need a quiescent state from this CPU, | |
1114 | * then make sure that this CPU doesn't go looking for one. | |
1115 | */ | |
1116 | if ((rnp->qsmask & rdp->grpmask) == 0) | |
1117 | rdp->qs_pending = 0; | |
1118 | } | |
1119 | } | |
1120 | ||
1121 | /* | |
1122 | * Advance this CPU's callbacks, but only if the current grace period | |
1123 | * has ended. This may be called only from the CPU to whom the rdp | |
1124 | * belongs. | |
1125 | */ | |
1126 | static void | |
1127 | rcu_process_gp_end(struct rcu_state *rsp, struct rcu_data *rdp) | |
1128 | { | |
1129 | unsigned long flags; | |
1130 | struct rcu_node *rnp; | |
1131 | ||
1132 | local_irq_save(flags); | |
1133 | rnp = rdp->mynode; | |
1134 | if (rdp->completed == ACCESS_ONCE(rnp->completed) || /* outside lock. */ | |
1135 | !raw_spin_trylock(&rnp->lock)) { /* irqs already off, so later. */ | |
1136 | local_irq_restore(flags); | |
1137 | return; | |
1138 | } | |
1139 | __rcu_process_gp_end(rsp, rnp, rdp); | |
1140 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1141 | } | |
1142 | ||
1143 | /* | |
1144 | * Do per-CPU grace-period initialization for running CPU. The caller | |
1145 | * must hold the lock of the leaf rcu_node structure corresponding to | |
1146 | * this CPU. | |
1147 | */ | |
1148 | static void | |
1149 | rcu_start_gp_per_cpu(struct rcu_state *rsp, struct rcu_node *rnp, struct rcu_data *rdp) | |
1150 | { | |
1151 | /* Prior grace period ended, so advance callbacks for current CPU. */ | |
1152 | __rcu_process_gp_end(rsp, rnp, rdp); | |
1153 | ||
1154 | /* Set state so that this CPU will detect the next quiescent state. */ | |
1155 | __note_new_gpnum(rsp, rnp, rdp); | |
1156 | } | |
1157 | ||
1158 | /* | |
1159 | * Initialize a new grace period. | |
1160 | */ | |
1161 | static int rcu_gp_init(struct rcu_state *rsp) | |
1162 | { | |
1163 | struct rcu_data *rdp; | |
1164 | struct rcu_node *rnp = rcu_get_root(rsp); | |
1165 | ||
1166 | raw_spin_lock_irq(&rnp->lock); | |
1167 | rsp->gp_flags = 0; /* Clear all flags: New grace period. */ | |
1168 | ||
1169 | if (rcu_gp_in_progress(rsp)) { | |
1170 | /* Grace period already in progress, don't start another. */ | |
1171 | raw_spin_unlock_irq(&rnp->lock); | |
1172 | return 0; | |
1173 | } | |
1174 | ||
1175 | /* Advance to a new grace period and initialize state. */ | |
1176 | rsp->gpnum++; | |
1177 | trace_rcu_grace_period(rsp->name, rsp->gpnum, "start"); | |
1178 | record_gp_stall_check_time(rsp); | |
1179 | raw_spin_unlock_irq(&rnp->lock); | |
1180 | ||
1181 | /* Exclude any concurrent CPU-hotplug operations. */ | |
1182 | get_online_cpus(); | |
1183 | ||
1184 | /* | |
1185 | * Set the quiescent-state-needed bits in all the rcu_node | |
1186 | * structures for all currently online CPUs in breadth-first order, | |
1187 | * starting from the root rcu_node structure, relying on the layout | |
1188 | * of the tree within the rsp->node[] array. Note that other CPUs | |
1189 | * will access only the leaves of the hierarchy, thus seeing that no | |
1190 | * grace period is in progress, at least until the corresponding | |
1191 | * leaf node has been initialized. In addition, we have excluded | |
1192 | * CPU-hotplug operations. | |
1193 | * | |
1194 | * The grace period cannot complete until the initialization | |
1195 | * process finishes, because this kthread handles both. | |
1196 | */ | |
1197 | rcu_for_each_node_breadth_first(rsp, rnp) { | |
1198 | raw_spin_lock_irq(&rnp->lock); | |
1199 | rdp = this_cpu_ptr(rsp->rda); | |
1200 | rcu_preempt_check_blocked_tasks(rnp); | |
1201 | rnp->qsmask = rnp->qsmaskinit; | |
1202 | rnp->gpnum = rsp->gpnum; | |
1203 | WARN_ON_ONCE(rnp->completed != rsp->completed); | |
1204 | rnp->completed = rsp->completed; | |
1205 | if (rnp == rdp->mynode) | |
1206 | rcu_start_gp_per_cpu(rsp, rnp, rdp); | |
1207 | rcu_preempt_boost_start_gp(rnp); | |
1208 | trace_rcu_grace_period_init(rsp->name, rnp->gpnum, | |
1209 | rnp->level, rnp->grplo, | |
1210 | rnp->grphi, rnp->qsmask); | |
1211 | raw_spin_unlock_irq(&rnp->lock); | |
1212 | #ifdef CONFIG_PROVE_RCU_DELAY | |
1213 | if ((random32() % (rcu_num_nodes * 8)) == 0) | |
1214 | schedule_timeout_uninterruptible(2); | |
1215 | #endif /* #ifdef CONFIG_PROVE_RCU_DELAY */ | |
1216 | cond_resched(); | |
1217 | } | |
1218 | ||
1219 | put_online_cpus(); | |
1220 | return 1; | |
1221 | } | |
1222 | ||
1223 | /* | |
1224 | * Do one round of quiescent-state forcing. | |
1225 | */ | |
1226 | int rcu_gp_fqs(struct rcu_state *rsp, int fqs_state_in) | |
1227 | { | |
1228 | int fqs_state = fqs_state_in; | |
1229 | struct rcu_node *rnp = rcu_get_root(rsp); | |
1230 | ||
1231 | rsp->n_force_qs++; | |
1232 | if (fqs_state == RCU_SAVE_DYNTICK) { | |
1233 | /* Collect dyntick-idle snapshots. */ | |
1234 | force_qs_rnp(rsp, dyntick_save_progress_counter); | |
1235 | fqs_state = RCU_FORCE_QS; | |
1236 | } else { | |
1237 | /* Handle dyntick-idle and offline CPUs. */ | |
1238 | force_qs_rnp(rsp, rcu_implicit_dynticks_qs); | |
1239 | } | |
1240 | /* Clear flag to prevent immediate re-entry. */ | |
1241 | if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) { | |
1242 | raw_spin_lock_irq(&rnp->lock); | |
1243 | rsp->gp_flags &= ~RCU_GP_FLAG_FQS; | |
1244 | raw_spin_unlock_irq(&rnp->lock); | |
1245 | } | |
1246 | return fqs_state; | |
1247 | } | |
1248 | ||
1249 | /* | |
1250 | * Clean up after the old grace period. | |
1251 | */ | |
1252 | static void rcu_gp_cleanup(struct rcu_state *rsp) | |
1253 | { | |
1254 | unsigned long gp_duration; | |
1255 | struct rcu_data *rdp; | |
1256 | struct rcu_node *rnp = rcu_get_root(rsp); | |
1257 | ||
1258 | raw_spin_lock_irq(&rnp->lock); | |
1259 | gp_duration = jiffies - rsp->gp_start; | |
1260 | if (gp_duration > rsp->gp_max) | |
1261 | rsp->gp_max = gp_duration; | |
1262 | ||
1263 | /* | |
1264 | * We know the grace period is complete, but to everyone else | |
1265 | * it appears to still be ongoing. But it is also the case | |
1266 | * that to everyone else it looks like there is nothing that | |
1267 | * they can do to advance the grace period. It is therefore | |
1268 | * safe for us to drop the lock in order to mark the grace | |
1269 | * period as completed in all of the rcu_node structures. | |
1270 | */ | |
1271 | raw_spin_unlock_irq(&rnp->lock); | |
1272 | ||
1273 | /* | |
1274 | * Propagate new ->completed value to rcu_node structures so | |
1275 | * that other CPUs don't have to wait until the start of the next | |
1276 | * grace period to process their callbacks. This also avoids | |
1277 | * some nasty RCU grace-period initialization races by forcing | |
1278 | * the end of the current grace period to be completely recorded in | |
1279 | * all of the rcu_node structures before the beginning of the next | |
1280 | * grace period is recorded in any of the rcu_node structures. | |
1281 | */ | |
1282 | rcu_for_each_node_breadth_first(rsp, rnp) { | |
1283 | raw_spin_lock_irq(&rnp->lock); | |
1284 | rnp->completed = rsp->gpnum; | |
1285 | raw_spin_unlock_irq(&rnp->lock); | |
1286 | cond_resched(); | |
1287 | } | |
1288 | rnp = rcu_get_root(rsp); | |
1289 | raw_spin_lock_irq(&rnp->lock); | |
1290 | ||
1291 | rsp->completed = rsp->gpnum; /* Declare grace period done. */ | |
1292 | trace_rcu_grace_period(rsp->name, rsp->completed, "end"); | |
1293 | rsp->fqs_state = RCU_GP_IDLE; | |
1294 | rdp = this_cpu_ptr(rsp->rda); | |
1295 | if (cpu_needs_another_gp(rsp, rdp)) | |
1296 | rsp->gp_flags = 1; | |
1297 | raw_spin_unlock_irq(&rnp->lock); | |
1298 | } | |
1299 | ||
1300 | /* | |
1301 | * Body of kthread that handles grace periods. | |
1302 | */ | |
1303 | static int __noreturn rcu_gp_kthread(void *arg) | |
1304 | { | |
1305 | int fqs_state; | |
1306 | unsigned long j; | |
1307 | int ret; | |
1308 | struct rcu_state *rsp = arg; | |
1309 | struct rcu_node *rnp = rcu_get_root(rsp); | |
1310 | ||
1311 | for (;;) { | |
1312 | ||
1313 | /* Handle grace-period start. */ | |
1314 | for (;;) { | |
1315 | wait_event_interruptible(rsp->gp_wq, | |
1316 | rsp->gp_flags & | |
1317 | RCU_GP_FLAG_INIT); | |
1318 | if ((rsp->gp_flags & RCU_GP_FLAG_INIT) && | |
1319 | rcu_gp_init(rsp)) | |
1320 | break; | |
1321 | cond_resched(); | |
1322 | flush_signals(current); | |
1323 | } | |
1324 | ||
1325 | /* Handle quiescent-state forcing. */ | |
1326 | fqs_state = RCU_SAVE_DYNTICK; | |
1327 | j = jiffies_till_first_fqs; | |
1328 | if (j > HZ) { | |
1329 | j = HZ; | |
1330 | jiffies_till_first_fqs = HZ; | |
1331 | } | |
1332 | for (;;) { | |
1333 | rsp->jiffies_force_qs = jiffies + j; | |
1334 | ret = wait_event_interruptible_timeout(rsp->gp_wq, | |
1335 | (rsp->gp_flags & RCU_GP_FLAG_FQS) || | |
1336 | (!ACCESS_ONCE(rnp->qsmask) && | |
1337 | !rcu_preempt_blocked_readers_cgp(rnp)), | |
1338 | j); | |
1339 | /* If grace period done, leave loop. */ | |
1340 | if (!ACCESS_ONCE(rnp->qsmask) && | |
1341 | !rcu_preempt_blocked_readers_cgp(rnp)) | |
1342 | break; | |
1343 | /* If time for quiescent-state forcing, do it. */ | |
1344 | if (ret == 0 || (rsp->gp_flags & RCU_GP_FLAG_FQS)) { | |
1345 | fqs_state = rcu_gp_fqs(rsp, fqs_state); | |
1346 | cond_resched(); | |
1347 | } else { | |
1348 | /* Deal with stray signal. */ | |
1349 | cond_resched(); | |
1350 | flush_signals(current); | |
1351 | } | |
1352 | j = jiffies_till_next_fqs; | |
1353 | if (j > HZ) { | |
1354 | j = HZ; | |
1355 | jiffies_till_next_fqs = HZ; | |
1356 | } else if (j < 1) { | |
1357 | j = 1; | |
1358 | jiffies_till_next_fqs = 1; | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | /* Handle grace-period end. */ | |
1363 | rcu_gp_cleanup(rsp); | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | /* | |
1368 | * Start a new RCU grace period if warranted, re-initializing the hierarchy | |
1369 | * in preparation for detecting the next grace period. The caller must hold | |
1370 | * the root node's ->lock, which is released before return. Hard irqs must | |
1371 | * be disabled. | |
1372 | * | |
1373 | * Note that it is legal for a dying CPU (which is marked as offline) to | |
1374 | * invoke this function. This can happen when the dying CPU reports its | |
1375 | * quiescent state. | |
1376 | */ | |
1377 | static void | |
1378 | rcu_start_gp(struct rcu_state *rsp, unsigned long flags) | |
1379 | __releases(rcu_get_root(rsp)->lock) | |
1380 | { | |
1381 | struct rcu_data *rdp = this_cpu_ptr(rsp->rda); | |
1382 | struct rcu_node *rnp = rcu_get_root(rsp); | |
1383 | ||
1384 | if (!rsp->gp_kthread || | |
1385 | !cpu_needs_another_gp(rsp, rdp)) { | |
1386 | /* | |
1387 | * Either we have not yet spawned the grace-period | |
1388 | * task or this CPU does not need another grace period. | |
1389 | * Either way, don't start a new grace period. | |
1390 | */ | |
1391 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1392 | return; | |
1393 | } | |
1394 | ||
1395 | rsp->gp_flags = RCU_GP_FLAG_INIT; | |
1396 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1397 | wake_up(&rsp->gp_wq); | |
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * Report a full set of quiescent states to the specified rcu_state | |
1402 | * data structure. This involves cleaning up after the prior grace | |
1403 | * period and letting rcu_start_gp() start up the next grace period | |
1404 | * if one is needed. Note that the caller must hold rnp->lock, as | |
1405 | * required by rcu_start_gp(), which will release it. | |
1406 | */ | |
1407 | static void rcu_report_qs_rsp(struct rcu_state *rsp, unsigned long flags) | |
1408 | __releases(rcu_get_root(rsp)->lock) | |
1409 | { | |
1410 | WARN_ON_ONCE(!rcu_gp_in_progress(rsp)); | |
1411 | raw_spin_unlock_irqrestore(&rcu_get_root(rsp)->lock, flags); | |
1412 | wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */ | |
1413 | } | |
1414 | ||
1415 | /* | |
1416 | * Similar to rcu_report_qs_rdp(), for which it is a helper function. | |
1417 | * Allows quiescent states for a group of CPUs to be reported at one go | |
1418 | * to the specified rcu_node structure, though all the CPUs in the group | |
1419 | * must be represented by the same rcu_node structure (which need not be | |
1420 | * a leaf rcu_node structure, though it often will be). That structure's | |
1421 | * lock must be held upon entry, and it is released before return. | |
1422 | */ | |
1423 | static void | |
1424 | rcu_report_qs_rnp(unsigned long mask, struct rcu_state *rsp, | |
1425 | struct rcu_node *rnp, unsigned long flags) | |
1426 | __releases(rnp->lock) | |
1427 | { | |
1428 | struct rcu_node *rnp_c; | |
1429 | ||
1430 | /* Walk up the rcu_node hierarchy. */ | |
1431 | for (;;) { | |
1432 | if (!(rnp->qsmask & mask)) { | |
1433 | ||
1434 | /* Our bit has already been cleared, so done. */ | |
1435 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1436 | return; | |
1437 | } | |
1438 | rnp->qsmask &= ~mask; | |
1439 | trace_rcu_quiescent_state_report(rsp->name, rnp->gpnum, | |
1440 | mask, rnp->qsmask, rnp->level, | |
1441 | rnp->grplo, rnp->grphi, | |
1442 | !!rnp->gp_tasks); | |
1443 | if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) { | |
1444 | ||
1445 | /* Other bits still set at this level, so done. */ | |
1446 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1447 | return; | |
1448 | } | |
1449 | mask = rnp->grpmask; | |
1450 | if (rnp->parent == NULL) { | |
1451 | ||
1452 | /* No more levels. Exit loop holding root lock. */ | |
1453 | ||
1454 | break; | |
1455 | } | |
1456 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1457 | rnp_c = rnp; | |
1458 | rnp = rnp->parent; | |
1459 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
1460 | WARN_ON_ONCE(rnp_c->qsmask); | |
1461 | } | |
1462 | ||
1463 | /* | |
1464 | * Get here if we are the last CPU to pass through a quiescent | |
1465 | * state for this grace period. Invoke rcu_report_qs_rsp() | |
1466 | * to clean up and start the next grace period if one is needed. | |
1467 | */ | |
1468 | rcu_report_qs_rsp(rsp, flags); /* releases rnp->lock. */ | |
1469 | } | |
1470 | ||
1471 | /* | |
1472 | * Record a quiescent state for the specified CPU to that CPU's rcu_data | |
1473 | * structure. This must be either called from the specified CPU, or | |
1474 | * called when the specified CPU is known to be offline (and when it is | |
1475 | * also known that no other CPU is concurrently trying to help the offline | |
1476 | * CPU). The lastcomp argument is used to make sure we are still in the | |
1477 | * grace period of interest. We don't want to end the current grace period | |
1478 | * based on quiescent states detected in an earlier grace period! | |
1479 | */ | |
1480 | static void | |
1481 | rcu_report_qs_rdp(int cpu, struct rcu_state *rsp, struct rcu_data *rdp) | |
1482 | { | |
1483 | unsigned long flags; | |
1484 | unsigned long mask; | |
1485 | struct rcu_node *rnp; | |
1486 | ||
1487 | rnp = rdp->mynode; | |
1488 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
1489 | if (rdp->passed_quiesce == 0 || rdp->gpnum != rnp->gpnum || | |
1490 | rnp->completed == rnp->gpnum) { | |
1491 | ||
1492 | /* | |
1493 | * The grace period in which this quiescent state was | |
1494 | * recorded has ended, so don't report it upwards. | |
1495 | * We will instead need a new quiescent state that lies | |
1496 | * within the current grace period. | |
1497 | */ | |
1498 | rdp->passed_quiesce = 0; /* need qs for new gp. */ | |
1499 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1500 | return; | |
1501 | } | |
1502 | mask = rdp->grpmask; | |
1503 | if ((rnp->qsmask & mask) == 0) { | |
1504 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1505 | } else { | |
1506 | rdp->qs_pending = 0; | |
1507 | ||
1508 | /* | |
1509 | * This GP can't end until cpu checks in, so all of our | |
1510 | * callbacks can be processed during the next GP. | |
1511 | */ | |
1512 | rdp->nxttail[RCU_NEXT_READY_TAIL] = rdp->nxttail[RCU_NEXT_TAIL]; | |
1513 | ||
1514 | rcu_report_qs_rnp(mask, rsp, rnp, flags); /* rlses rnp->lock */ | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | /* | |
1519 | * Check to see if there is a new grace period of which this CPU | |
1520 | * is not yet aware, and if so, set up local rcu_data state for it. | |
1521 | * Otherwise, see if this CPU has just passed through its first | |
1522 | * quiescent state for this grace period, and record that fact if so. | |
1523 | */ | |
1524 | static void | |
1525 | rcu_check_quiescent_state(struct rcu_state *rsp, struct rcu_data *rdp) | |
1526 | { | |
1527 | /* If there is now a new grace period, record and return. */ | |
1528 | if (check_for_new_grace_period(rsp, rdp)) | |
1529 | return; | |
1530 | ||
1531 | /* | |
1532 | * Does this CPU still need to do its part for current grace period? | |
1533 | * If no, return and let the other CPUs do their part as well. | |
1534 | */ | |
1535 | if (!rdp->qs_pending) | |
1536 | return; | |
1537 | ||
1538 | /* | |
1539 | * Was there a quiescent state since the beginning of the grace | |
1540 | * period? If no, then exit and wait for the next call. | |
1541 | */ | |
1542 | if (!rdp->passed_quiesce) | |
1543 | return; | |
1544 | ||
1545 | /* | |
1546 | * Tell RCU we are done (but rcu_report_qs_rdp() will be the | |
1547 | * judge of that). | |
1548 | */ | |
1549 | rcu_report_qs_rdp(rdp->cpu, rsp, rdp); | |
1550 | } | |
1551 | ||
1552 | #ifdef CONFIG_HOTPLUG_CPU | |
1553 | ||
1554 | /* | |
1555 | * Send the specified CPU's RCU callbacks to the orphanage. The | |
1556 | * specified CPU must be offline, and the caller must hold the | |
1557 | * ->onofflock. | |
1558 | */ | |
1559 | static void | |
1560 | rcu_send_cbs_to_orphanage(int cpu, struct rcu_state *rsp, | |
1561 | struct rcu_node *rnp, struct rcu_data *rdp) | |
1562 | { | |
1563 | /* | |
1564 | * Orphan the callbacks. First adjust the counts. This is safe | |
1565 | * because ->onofflock excludes _rcu_barrier()'s adoption of | |
1566 | * the callbacks, thus no memory barrier is required. | |
1567 | */ | |
1568 | if (rdp->nxtlist != NULL) { | |
1569 | rsp->qlen_lazy += rdp->qlen_lazy; | |
1570 | rsp->qlen += rdp->qlen; | |
1571 | rdp->n_cbs_orphaned += rdp->qlen; | |
1572 | rdp->qlen_lazy = 0; | |
1573 | ACCESS_ONCE(rdp->qlen) = 0; | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * Next, move those callbacks still needing a grace period to | |
1578 | * the orphanage, where some other CPU will pick them up. | |
1579 | * Some of the callbacks might have gone partway through a grace | |
1580 | * period, but that is too bad. They get to start over because we | |
1581 | * cannot assume that grace periods are synchronized across CPUs. | |
1582 | * We don't bother updating the ->nxttail[] array yet, instead | |
1583 | * we just reset the whole thing later on. | |
1584 | */ | |
1585 | if (*rdp->nxttail[RCU_DONE_TAIL] != NULL) { | |
1586 | *rsp->orphan_nxttail = *rdp->nxttail[RCU_DONE_TAIL]; | |
1587 | rsp->orphan_nxttail = rdp->nxttail[RCU_NEXT_TAIL]; | |
1588 | *rdp->nxttail[RCU_DONE_TAIL] = NULL; | |
1589 | } | |
1590 | ||
1591 | /* | |
1592 | * Then move the ready-to-invoke callbacks to the orphanage, | |
1593 | * where some other CPU will pick them up. These will not be | |
1594 | * required to pass though another grace period: They are done. | |
1595 | */ | |
1596 | if (rdp->nxtlist != NULL) { | |
1597 | *rsp->orphan_donetail = rdp->nxtlist; | |
1598 | rsp->orphan_donetail = rdp->nxttail[RCU_DONE_TAIL]; | |
1599 | } | |
1600 | ||
1601 | /* Finally, initialize the rcu_data structure's list to empty. */ | |
1602 | init_callback_list(rdp); | |
1603 | } | |
1604 | ||
1605 | /* | |
1606 | * Adopt the RCU callbacks from the specified rcu_state structure's | |
1607 | * orphanage. The caller must hold the ->onofflock. | |
1608 | */ | |
1609 | static void rcu_adopt_orphan_cbs(struct rcu_state *rsp) | |
1610 | { | |
1611 | int i; | |
1612 | struct rcu_data *rdp = __this_cpu_ptr(rsp->rda); | |
1613 | ||
1614 | /* Do the accounting first. */ | |
1615 | rdp->qlen_lazy += rsp->qlen_lazy; | |
1616 | rdp->qlen += rsp->qlen; | |
1617 | rdp->n_cbs_adopted += rsp->qlen; | |
1618 | if (rsp->qlen_lazy != rsp->qlen) | |
1619 | rcu_idle_count_callbacks_posted(); | |
1620 | rsp->qlen_lazy = 0; | |
1621 | rsp->qlen = 0; | |
1622 | ||
1623 | /* | |
1624 | * We do not need a memory barrier here because the only way we | |
1625 | * can get here if there is an rcu_barrier() in flight is if | |
1626 | * we are the task doing the rcu_barrier(). | |
1627 | */ | |
1628 | ||
1629 | /* First adopt the ready-to-invoke callbacks. */ | |
1630 | if (rsp->orphan_donelist != NULL) { | |
1631 | *rsp->orphan_donetail = *rdp->nxttail[RCU_DONE_TAIL]; | |
1632 | *rdp->nxttail[RCU_DONE_TAIL] = rsp->orphan_donelist; | |
1633 | for (i = RCU_NEXT_SIZE - 1; i >= RCU_DONE_TAIL; i--) | |
1634 | if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL]) | |
1635 | rdp->nxttail[i] = rsp->orphan_donetail; | |
1636 | rsp->orphan_donelist = NULL; | |
1637 | rsp->orphan_donetail = &rsp->orphan_donelist; | |
1638 | } | |
1639 | ||
1640 | /* And then adopt the callbacks that still need a grace period. */ | |
1641 | if (rsp->orphan_nxtlist != NULL) { | |
1642 | *rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxtlist; | |
1643 | rdp->nxttail[RCU_NEXT_TAIL] = rsp->orphan_nxttail; | |
1644 | rsp->orphan_nxtlist = NULL; | |
1645 | rsp->orphan_nxttail = &rsp->orphan_nxtlist; | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | /* | |
1650 | * Trace the fact that this CPU is going offline. | |
1651 | */ | |
1652 | static void rcu_cleanup_dying_cpu(struct rcu_state *rsp) | |
1653 | { | |
1654 | RCU_TRACE(unsigned long mask); | |
1655 | RCU_TRACE(struct rcu_data *rdp = this_cpu_ptr(rsp->rda)); | |
1656 | RCU_TRACE(struct rcu_node *rnp = rdp->mynode); | |
1657 | ||
1658 | RCU_TRACE(mask = rdp->grpmask); | |
1659 | trace_rcu_grace_period(rsp->name, | |
1660 | rnp->gpnum + 1 - !!(rnp->qsmask & mask), | |
1661 | "cpuofl"); | |
1662 | } | |
1663 | ||
1664 | /* | |
1665 | * The CPU has been completely removed, and some other CPU is reporting | |
1666 | * this fact from process context. Do the remainder of the cleanup, | |
1667 | * including orphaning the outgoing CPU's RCU callbacks, and also | |
1668 | * adopting them. There can only be one CPU hotplug operation at a time, | |
1669 | * so no other CPU can be attempting to update rcu_cpu_kthread_task. | |
1670 | */ | |
1671 | static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp) | |
1672 | { | |
1673 | unsigned long flags; | |
1674 | unsigned long mask; | |
1675 | int need_report = 0; | |
1676 | struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu); | |
1677 | struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */ | |
1678 | ||
1679 | /* Adjust any no-longer-needed kthreads. */ | |
1680 | rcu_boost_kthread_setaffinity(rnp, -1); | |
1681 | ||
1682 | /* Remove the dead CPU from the bitmasks in the rcu_node hierarchy. */ | |
1683 | ||
1684 | /* Exclude any attempts to start a new grace period. */ | |
1685 | raw_spin_lock_irqsave(&rsp->onofflock, flags); | |
1686 | ||
1687 | /* Orphan the dead CPU's callbacks, and adopt them if appropriate. */ | |
1688 | rcu_send_cbs_to_orphanage(cpu, rsp, rnp, rdp); | |
1689 | rcu_adopt_orphan_cbs(rsp); | |
1690 | ||
1691 | /* Remove the outgoing CPU from the masks in the rcu_node hierarchy. */ | |
1692 | mask = rdp->grpmask; /* rnp->grplo is constant. */ | |
1693 | do { | |
1694 | raw_spin_lock(&rnp->lock); /* irqs already disabled. */ | |
1695 | rnp->qsmaskinit &= ~mask; | |
1696 | if (rnp->qsmaskinit != 0) { | |
1697 | if (rnp != rdp->mynode) | |
1698 | raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */ | |
1699 | break; | |
1700 | } | |
1701 | if (rnp == rdp->mynode) | |
1702 | need_report = rcu_preempt_offline_tasks(rsp, rnp, rdp); | |
1703 | else | |
1704 | raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */ | |
1705 | mask = rnp->grpmask; | |
1706 | rnp = rnp->parent; | |
1707 | } while (rnp != NULL); | |
1708 | ||
1709 | /* | |
1710 | * We still hold the leaf rcu_node structure lock here, and | |
1711 | * irqs are still disabled. The reason for this subterfuge is | |
1712 | * because invoking rcu_report_unblock_qs_rnp() with ->onofflock | |
1713 | * held leads to deadlock. | |
1714 | */ | |
1715 | raw_spin_unlock(&rsp->onofflock); /* irqs remain disabled. */ | |
1716 | rnp = rdp->mynode; | |
1717 | if (need_report & RCU_OFL_TASKS_NORM_GP) | |
1718 | rcu_report_unblock_qs_rnp(rnp, flags); | |
1719 | else | |
1720 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1721 | if (need_report & RCU_OFL_TASKS_EXP_GP) | |
1722 | rcu_report_exp_rnp(rsp, rnp, true); | |
1723 | WARN_ONCE(rdp->qlen != 0 || rdp->nxtlist != NULL, | |
1724 | "rcu_cleanup_dead_cpu: Callbacks on offline CPU %d: qlen=%lu, nxtlist=%p\n", | |
1725 | cpu, rdp->qlen, rdp->nxtlist); | |
1726 | init_callback_list(rdp); | |
1727 | /* Disallow further callbacks on this CPU. */ | |
1728 | rdp->nxttail[RCU_NEXT_TAIL] = NULL; | |
1729 | } | |
1730 | ||
1731 | #else /* #ifdef CONFIG_HOTPLUG_CPU */ | |
1732 | ||
1733 | static void rcu_cleanup_dying_cpu(struct rcu_state *rsp) | |
1734 | { | |
1735 | } | |
1736 | ||
1737 | static void rcu_cleanup_dead_cpu(int cpu, struct rcu_state *rsp) | |
1738 | { | |
1739 | } | |
1740 | ||
1741 | #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */ | |
1742 | ||
1743 | /* | |
1744 | * Invoke any RCU callbacks that have made it to the end of their grace | |
1745 | * period. Thottle as specified by rdp->blimit. | |
1746 | */ | |
1747 | static void rcu_do_batch(struct rcu_state *rsp, struct rcu_data *rdp) | |
1748 | { | |
1749 | unsigned long flags; | |
1750 | struct rcu_head *next, *list, **tail; | |
1751 | int bl, count, count_lazy, i; | |
1752 | ||
1753 | /* If no callbacks are ready, just return.*/ | |
1754 | if (!cpu_has_callbacks_ready_to_invoke(rdp)) { | |
1755 | trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, 0); | |
1756 | trace_rcu_batch_end(rsp->name, 0, !!ACCESS_ONCE(rdp->nxtlist), | |
1757 | need_resched(), is_idle_task(current), | |
1758 | rcu_is_callbacks_kthread()); | |
1759 | return; | |
1760 | } | |
1761 | ||
1762 | /* | |
1763 | * Extract the list of ready callbacks, disabling to prevent | |
1764 | * races with call_rcu() from interrupt handlers. | |
1765 | */ | |
1766 | local_irq_save(flags); | |
1767 | WARN_ON_ONCE(cpu_is_offline(smp_processor_id())); | |
1768 | bl = rdp->blimit; | |
1769 | trace_rcu_batch_start(rsp->name, rdp->qlen_lazy, rdp->qlen, bl); | |
1770 | list = rdp->nxtlist; | |
1771 | rdp->nxtlist = *rdp->nxttail[RCU_DONE_TAIL]; | |
1772 | *rdp->nxttail[RCU_DONE_TAIL] = NULL; | |
1773 | tail = rdp->nxttail[RCU_DONE_TAIL]; | |
1774 | for (i = RCU_NEXT_SIZE - 1; i >= 0; i--) | |
1775 | if (rdp->nxttail[i] == rdp->nxttail[RCU_DONE_TAIL]) | |
1776 | rdp->nxttail[i] = &rdp->nxtlist; | |
1777 | local_irq_restore(flags); | |
1778 | ||
1779 | /* Invoke callbacks. */ | |
1780 | count = count_lazy = 0; | |
1781 | while (list) { | |
1782 | next = list->next; | |
1783 | prefetch(next); | |
1784 | debug_rcu_head_unqueue(list); | |
1785 | if (__rcu_reclaim(rsp->name, list)) | |
1786 | count_lazy++; | |
1787 | list = next; | |
1788 | /* Stop only if limit reached and CPU has something to do. */ | |
1789 | if (++count >= bl && | |
1790 | (need_resched() || | |
1791 | (!is_idle_task(current) && !rcu_is_callbacks_kthread()))) | |
1792 | break; | |
1793 | } | |
1794 | ||
1795 | local_irq_save(flags); | |
1796 | trace_rcu_batch_end(rsp->name, count, !!list, need_resched(), | |
1797 | is_idle_task(current), | |
1798 | rcu_is_callbacks_kthread()); | |
1799 | ||
1800 | /* Update count, and requeue any remaining callbacks. */ | |
1801 | if (list != NULL) { | |
1802 | *tail = rdp->nxtlist; | |
1803 | rdp->nxtlist = list; | |
1804 | for (i = 0; i < RCU_NEXT_SIZE; i++) | |
1805 | if (&rdp->nxtlist == rdp->nxttail[i]) | |
1806 | rdp->nxttail[i] = tail; | |
1807 | else | |
1808 | break; | |
1809 | } | |
1810 | smp_mb(); /* List handling before counting for rcu_barrier(). */ | |
1811 | rdp->qlen_lazy -= count_lazy; | |
1812 | ACCESS_ONCE(rdp->qlen) -= count; | |
1813 | rdp->n_cbs_invoked += count; | |
1814 | ||
1815 | /* Reinstate batch limit if we have worked down the excess. */ | |
1816 | if (rdp->blimit == LONG_MAX && rdp->qlen <= qlowmark) | |
1817 | rdp->blimit = blimit; | |
1818 | ||
1819 | /* Reset ->qlen_last_fqs_check trigger if enough CBs have drained. */ | |
1820 | if (rdp->qlen == 0 && rdp->qlen_last_fqs_check != 0) { | |
1821 | rdp->qlen_last_fqs_check = 0; | |
1822 | rdp->n_force_qs_snap = rsp->n_force_qs; | |
1823 | } else if (rdp->qlen < rdp->qlen_last_fqs_check - qhimark) | |
1824 | rdp->qlen_last_fqs_check = rdp->qlen; | |
1825 | WARN_ON_ONCE((rdp->nxtlist == NULL) != (rdp->qlen == 0)); | |
1826 | ||
1827 | local_irq_restore(flags); | |
1828 | ||
1829 | /* Re-invoke RCU core processing if there are callbacks remaining. */ | |
1830 | if (cpu_has_callbacks_ready_to_invoke(rdp)) | |
1831 | invoke_rcu_core(); | |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * Check to see if this CPU is in a non-context-switch quiescent state | |
1836 | * (user mode or idle loop for rcu, non-softirq execution for rcu_bh). | |
1837 | * Also schedule RCU core processing. | |
1838 | * | |
1839 | * This function must be called from hardirq context. It is normally | |
1840 | * invoked from the scheduling-clock interrupt. If rcu_pending returns | |
1841 | * false, there is no point in invoking rcu_check_callbacks(). | |
1842 | */ | |
1843 | void rcu_check_callbacks(int cpu, int user) | |
1844 | { | |
1845 | trace_rcu_utilization("Start scheduler-tick"); | |
1846 | increment_cpu_stall_ticks(); | |
1847 | if (user || rcu_is_cpu_rrupt_from_idle()) { | |
1848 | ||
1849 | /* | |
1850 | * Get here if this CPU took its interrupt from user | |
1851 | * mode or from the idle loop, and if this is not a | |
1852 | * nested interrupt. In this case, the CPU is in | |
1853 | * a quiescent state, so note it. | |
1854 | * | |
1855 | * No memory barrier is required here because both | |
1856 | * rcu_sched_qs() and rcu_bh_qs() reference only CPU-local | |
1857 | * variables that other CPUs neither access nor modify, | |
1858 | * at least not while the corresponding CPU is online. | |
1859 | */ | |
1860 | ||
1861 | rcu_sched_qs(cpu); | |
1862 | rcu_bh_qs(cpu); | |
1863 | ||
1864 | } else if (!in_softirq()) { | |
1865 | ||
1866 | /* | |
1867 | * Get here if this CPU did not take its interrupt from | |
1868 | * softirq, in other words, if it is not interrupting | |
1869 | * a rcu_bh read-side critical section. This is an _bh | |
1870 | * critical section, so note it. | |
1871 | */ | |
1872 | ||
1873 | rcu_bh_qs(cpu); | |
1874 | } | |
1875 | rcu_preempt_check_callbacks(cpu); | |
1876 | if (rcu_pending(cpu)) | |
1877 | invoke_rcu_core(); | |
1878 | trace_rcu_utilization("End scheduler-tick"); | |
1879 | } | |
1880 | ||
1881 | /* | |
1882 | * Scan the leaf rcu_node structures, processing dyntick state for any that | |
1883 | * have not yet encountered a quiescent state, using the function specified. | |
1884 | * Also initiate boosting for any threads blocked on the root rcu_node. | |
1885 | * | |
1886 | * The caller must have suppressed start of new grace periods. | |
1887 | */ | |
1888 | static void force_qs_rnp(struct rcu_state *rsp, int (*f)(struct rcu_data *)) | |
1889 | { | |
1890 | unsigned long bit; | |
1891 | int cpu; | |
1892 | unsigned long flags; | |
1893 | unsigned long mask; | |
1894 | struct rcu_node *rnp; | |
1895 | ||
1896 | rcu_for_each_leaf_node(rsp, rnp) { | |
1897 | cond_resched(); | |
1898 | mask = 0; | |
1899 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
1900 | if (!rcu_gp_in_progress(rsp)) { | |
1901 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1902 | return; | |
1903 | } | |
1904 | if (rnp->qsmask == 0) { | |
1905 | rcu_initiate_boost(rnp, flags); /* releases rnp->lock */ | |
1906 | continue; | |
1907 | } | |
1908 | cpu = rnp->grplo; | |
1909 | bit = 1; | |
1910 | for (; cpu <= rnp->grphi; cpu++, bit <<= 1) { | |
1911 | if ((rnp->qsmask & bit) != 0 && | |
1912 | f(per_cpu_ptr(rsp->rda, cpu))) | |
1913 | mask |= bit; | |
1914 | } | |
1915 | if (mask != 0) { | |
1916 | ||
1917 | /* rcu_report_qs_rnp() releases rnp->lock. */ | |
1918 | rcu_report_qs_rnp(mask, rsp, rnp, flags); | |
1919 | continue; | |
1920 | } | |
1921 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
1922 | } | |
1923 | rnp = rcu_get_root(rsp); | |
1924 | if (rnp->qsmask == 0) { | |
1925 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
1926 | rcu_initiate_boost(rnp, flags); /* releases rnp->lock. */ | |
1927 | } | |
1928 | } | |
1929 | ||
1930 | /* | |
1931 | * Force quiescent states on reluctant CPUs, and also detect which | |
1932 | * CPUs are in dyntick-idle mode. | |
1933 | */ | |
1934 | static void force_quiescent_state(struct rcu_state *rsp) | |
1935 | { | |
1936 | unsigned long flags; | |
1937 | bool ret; | |
1938 | struct rcu_node *rnp; | |
1939 | struct rcu_node *rnp_old = NULL; | |
1940 | ||
1941 | /* Funnel through hierarchy to reduce memory contention. */ | |
1942 | rnp = per_cpu_ptr(rsp->rda, raw_smp_processor_id())->mynode; | |
1943 | for (; rnp != NULL; rnp = rnp->parent) { | |
1944 | ret = (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) || | |
1945 | !raw_spin_trylock(&rnp->fqslock); | |
1946 | if (rnp_old != NULL) | |
1947 | raw_spin_unlock(&rnp_old->fqslock); | |
1948 | if (ret) { | |
1949 | rsp->n_force_qs_lh++; | |
1950 | return; | |
1951 | } | |
1952 | rnp_old = rnp; | |
1953 | } | |
1954 | /* rnp_old == rcu_get_root(rsp), rnp == NULL. */ | |
1955 | ||
1956 | /* Reached the root of the rcu_node tree, acquire lock. */ | |
1957 | raw_spin_lock_irqsave(&rnp_old->lock, flags); | |
1958 | raw_spin_unlock(&rnp_old->fqslock); | |
1959 | if (ACCESS_ONCE(rsp->gp_flags) & RCU_GP_FLAG_FQS) { | |
1960 | rsp->n_force_qs_lh++; | |
1961 | raw_spin_unlock_irqrestore(&rnp_old->lock, flags); | |
1962 | return; /* Someone beat us to it. */ | |
1963 | } | |
1964 | rsp->gp_flags |= RCU_GP_FLAG_FQS; | |
1965 | raw_spin_unlock_irqrestore(&rnp_old->lock, flags); | |
1966 | wake_up(&rsp->gp_wq); /* Memory barrier implied by wake_up() path. */ | |
1967 | } | |
1968 | ||
1969 | /* | |
1970 | * This does the RCU core processing work for the specified rcu_state | |
1971 | * and rcu_data structures. This may be called only from the CPU to | |
1972 | * whom the rdp belongs. | |
1973 | */ | |
1974 | static void | |
1975 | __rcu_process_callbacks(struct rcu_state *rsp) | |
1976 | { | |
1977 | unsigned long flags; | |
1978 | struct rcu_data *rdp = __this_cpu_ptr(rsp->rda); | |
1979 | ||
1980 | WARN_ON_ONCE(rdp->beenonline == 0); | |
1981 | ||
1982 | /* | |
1983 | * Advance callbacks in response to end of earlier grace | |
1984 | * period that some other CPU ended. | |
1985 | */ | |
1986 | rcu_process_gp_end(rsp, rdp); | |
1987 | ||
1988 | /* Update RCU state based on any recent quiescent states. */ | |
1989 | rcu_check_quiescent_state(rsp, rdp); | |
1990 | ||
1991 | /* Does this CPU require a not-yet-started grace period? */ | |
1992 | if (cpu_needs_another_gp(rsp, rdp)) { | |
1993 | raw_spin_lock_irqsave(&rcu_get_root(rsp)->lock, flags); | |
1994 | rcu_start_gp(rsp, flags); /* releases above lock */ | |
1995 | } | |
1996 | ||
1997 | /* If there are callbacks ready, invoke them. */ | |
1998 | if (cpu_has_callbacks_ready_to_invoke(rdp)) | |
1999 | invoke_rcu_callbacks(rsp, rdp); | |
2000 | } | |
2001 | ||
2002 | /* | |
2003 | * Do RCU core processing for the current CPU. | |
2004 | */ | |
2005 | static void rcu_process_callbacks(struct softirq_action *unused) | |
2006 | { | |
2007 | struct rcu_state *rsp; | |
2008 | ||
2009 | if (cpu_is_offline(smp_processor_id())) | |
2010 | return; | |
2011 | trace_rcu_utilization("Start RCU core"); | |
2012 | for_each_rcu_flavor(rsp) | |
2013 | __rcu_process_callbacks(rsp); | |
2014 | trace_rcu_utilization("End RCU core"); | |
2015 | } | |
2016 | ||
2017 | /* | |
2018 | * Schedule RCU callback invocation. If the specified type of RCU | |
2019 | * does not support RCU priority boosting, just do a direct call, | |
2020 | * otherwise wake up the per-CPU kernel kthread. Note that because we | |
2021 | * are running on the current CPU with interrupts disabled, the | |
2022 | * rcu_cpu_kthread_task cannot disappear out from under us. | |
2023 | */ | |
2024 | static void invoke_rcu_callbacks(struct rcu_state *rsp, struct rcu_data *rdp) | |
2025 | { | |
2026 | if (unlikely(!ACCESS_ONCE(rcu_scheduler_fully_active))) | |
2027 | return; | |
2028 | if (likely(!rsp->boost)) { | |
2029 | rcu_do_batch(rsp, rdp); | |
2030 | return; | |
2031 | } | |
2032 | invoke_rcu_callbacks_kthread(); | |
2033 | } | |
2034 | ||
2035 | static void invoke_rcu_core(void) | |
2036 | { | |
2037 | raise_softirq(RCU_SOFTIRQ); | |
2038 | } | |
2039 | ||
2040 | /* | |
2041 | * Handle any core-RCU processing required by a call_rcu() invocation. | |
2042 | */ | |
2043 | static void __call_rcu_core(struct rcu_state *rsp, struct rcu_data *rdp, | |
2044 | struct rcu_head *head, unsigned long flags) | |
2045 | { | |
2046 | /* | |
2047 | * If called from an extended quiescent state, invoke the RCU | |
2048 | * core in order to force a re-evaluation of RCU's idleness. | |
2049 | */ | |
2050 | if (rcu_is_cpu_idle() && cpu_online(smp_processor_id())) | |
2051 | invoke_rcu_core(); | |
2052 | ||
2053 | /* If interrupts were disabled or CPU offline, don't invoke RCU core. */ | |
2054 | if (irqs_disabled_flags(flags) || cpu_is_offline(smp_processor_id())) | |
2055 | return; | |
2056 | ||
2057 | /* | |
2058 | * Force the grace period if too many callbacks or too long waiting. | |
2059 | * Enforce hysteresis, and don't invoke force_quiescent_state() | |
2060 | * if some other CPU has recently done so. Also, don't bother | |
2061 | * invoking force_quiescent_state() if the newly enqueued callback | |
2062 | * is the only one waiting for a grace period to complete. | |
2063 | */ | |
2064 | if (unlikely(rdp->qlen > rdp->qlen_last_fqs_check + qhimark)) { | |
2065 | ||
2066 | /* Are we ignoring a completed grace period? */ | |
2067 | rcu_process_gp_end(rsp, rdp); | |
2068 | check_for_new_grace_period(rsp, rdp); | |
2069 | ||
2070 | /* Start a new grace period if one not already started. */ | |
2071 | if (!rcu_gp_in_progress(rsp)) { | |
2072 | unsigned long nestflag; | |
2073 | struct rcu_node *rnp_root = rcu_get_root(rsp); | |
2074 | ||
2075 | raw_spin_lock_irqsave(&rnp_root->lock, nestflag); | |
2076 | rcu_start_gp(rsp, nestflag); /* rlses rnp_root->lock */ | |
2077 | } else { | |
2078 | /* Give the grace period a kick. */ | |
2079 | rdp->blimit = LONG_MAX; | |
2080 | if (rsp->n_force_qs == rdp->n_force_qs_snap && | |
2081 | *rdp->nxttail[RCU_DONE_TAIL] != head) | |
2082 | force_quiescent_state(rsp); | |
2083 | rdp->n_force_qs_snap = rsp->n_force_qs; | |
2084 | rdp->qlen_last_fqs_check = rdp->qlen; | |
2085 | } | |
2086 | } | |
2087 | } | |
2088 | ||
2089 | static void | |
2090 | __call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu), | |
2091 | struct rcu_state *rsp, bool lazy) | |
2092 | { | |
2093 | unsigned long flags; | |
2094 | struct rcu_data *rdp; | |
2095 | ||
2096 | WARN_ON_ONCE((unsigned long)head & 0x3); /* Misaligned rcu_head! */ | |
2097 | debug_rcu_head_queue(head); | |
2098 | head->func = func; | |
2099 | head->next = NULL; | |
2100 | ||
2101 | /* | |
2102 | * Opportunistically note grace-period endings and beginnings. | |
2103 | * Note that we might see a beginning right after we see an | |
2104 | * end, but never vice versa, since this CPU has to pass through | |
2105 | * a quiescent state betweentimes. | |
2106 | */ | |
2107 | local_irq_save(flags); | |
2108 | rdp = this_cpu_ptr(rsp->rda); | |
2109 | ||
2110 | /* Add the callback to our list. */ | |
2111 | if (unlikely(rdp->nxttail[RCU_NEXT_TAIL] == NULL)) { | |
2112 | /* _call_rcu() is illegal on offline CPU; leak the callback. */ | |
2113 | WARN_ON_ONCE(1); | |
2114 | local_irq_restore(flags); | |
2115 | return; | |
2116 | } | |
2117 | ACCESS_ONCE(rdp->qlen)++; | |
2118 | if (lazy) | |
2119 | rdp->qlen_lazy++; | |
2120 | else | |
2121 | rcu_idle_count_callbacks_posted(); | |
2122 | smp_mb(); /* Count before adding callback for rcu_barrier(). */ | |
2123 | *rdp->nxttail[RCU_NEXT_TAIL] = head; | |
2124 | rdp->nxttail[RCU_NEXT_TAIL] = &head->next; | |
2125 | ||
2126 | if (__is_kfree_rcu_offset((unsigned long)func)) | |
2127 | trace_rcu_kfree_callback(rsp->name, head, (unsigned long)func, | |
2128 | rdp->qlen_lazy, rdp->qlen); | |
2129 | else | |
2130 | trace_rcu_callback(rsp->name, head, rdp->qlen_lazy, rdp->qlen); | |
2131 | ||
2132 | /* Go handle any RCU core processing required. */ | |
2133 | __call_rcu_core(rsp, rdp, head, flags); | |
2134 | local_irq_restore(flags); | |
2135 | } | |
2136 | ||
2137 | /* | |
2138 | * Queue an RCU-sched callback for invocation after a grace period. | |
2139 | */ | |
2140 | void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu)) | |
2141 | { | |
2142 | __call_rcu(head, func, &rcu_sched_state, 0); | |
2143 | } | |
2144 | EXPORT_SYMBOL_GPL(call_rcu_sched); | |
2145 | ||
2146 | /* | |
2147 | * Queue an RCU callback for invocation after a quicker grace period. | |
2148 | */ | |
2149 | void call_rcu_bh(struct rcu_head *head, void (*func)(struct rcu_head *rcu)) | |
2150 | { | |
2151 | __call_rcu(head, func, &rcu_bh_state, 0); | |
2152 | } | |
2153 | EXPORT_SYMBOL_GPL(call_rcu_bh); | |
2154 | ||
2155 | /* | |
2156 | * Because a context switch is a grace period for RCU-sched and RCU-bh, | |
2157 | * any blocking grace-period wait automatically implies a grace period | |
2158 | * if there is only one CPU online at any point time during execution | |
2159 | * of either synchronize_sched() or synchronize_rcu_bh(). It is OK to | |
2160 | * occasionally incorrectly indicate that there are multiple CPUs online | |
2161 | * when there was in fact only one the whole time, as this just adds | |
2162 | * some overhead: RCU still operates correctly. | |
2163 | */ | |
2164 | static inline int rcu_blocking_is_gp(void) | |
2165 | { | |
2166 | int ret; | |
2167 | ||
2168 | might_sleep(); /* Check for RCU read-side critical section. */ | |
2169 | preempt_disable(); | |
2170 | ret = num_online_cpus() <= 1; | |
2171 | preempt_enable(); | |
2172 | return ret; | |
2173 | } | |
2174 | ||
2175 | /** | |
2176 | * synchronize_sched - wait until an rcu-sched grace period has elapsed. | |
2177 | * | |
2178 | * Control will return to the caller some time after a full rcu-sched | |
2179 | * grace period has elapsed, in other words after all currently executing | |
2180 | * rcu-sched read-side critical sections have completed. These read-side | |
2181 | * critical sections are delimited by rcu_read_lock_sched() and | |
2182 | * rcu_read_unlock_sched(), and may be nested. Note that preempt_disable(), | |
2183 | * local_irq_disable(), and so on may be used in place of | |
2184 | * rcu_read_lock_sched(). | |
2185 | * | |
2186 | * This means that all preempt_disable code sequences, including NMI and | |
2187 | * hardware-interrupt handlers, in progress on entry will have completed | |
2188 | * before this primitive returns. However, this does not guarantee that | |
2189 | * softirq handlers will have completed, since in some kernels, these | |
2190 | * handlers can run in process context, and can block. | |
2191 | * | |
2192 | * This primitive provides the guarantees made by the (now removed) | |
2193 | * synchronize_kernel() API. In contrast, synchronize_rcu() only | |
2194 | * guarantees that rcu_read_lock() sections will have completed. | |
2195 | * In "classic RCU", these two guarantees happen to be one and | |
2196 | * the same, but can differ in realtime RCU implementations. | |
2197 | */ | |
2198 | void synchronize_sched(void) | |
2199 | { | |
2200 | rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) && | |
2201 | !lock_is_held(&rcu_lock_map) && | |
2202 | !lock_is_held(&rcu_sched_lock_map), | |
2203 | "Illegal synchronize_sched() in RCU-sched read-side critical section"); | |
2204 | if (rcu_blocking_is_gp()) | |
2205 | return; | |
2206 | wait_rcu_gp(call_rcu_sched); | |
2207 | } | |
2208 | EXPORT_SYMBOL_GPL(synchronize_sched); | |
2209 | ||
2210 | /** | |
2211 | * synchronize_rcu_bh - wait until an rcu_bh grace period has elapsed. | |
2212 | * | |
2213 | * Control will return to the caller some time after a full rcu_bh grace | |
2214 | * period has elapsed, in other words after all currently executing rcu_bh | |
2215 | * read-side critical sections have completed. RCU read-side critical | |
2216 | * sections are delimited by rcu_read_lock_bh() and rcu_read_unlock_bh(), | |
2217 | * and may be nested. | |
2218 | */ | |
2219 | void synchronize_rcu_bh(void) | |
2220 | { | |
2221 | rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) && | |
2222 | !lock_is_held(&rcu_lock_map) && | |
2223 | !lock_is_held(&rcu_sched_lock_map), | |
2224 | "Illegal synchronize_rcu_bh() in RCU-bh read-side critical section"); | |
2225 | if (rcu_blocking_is_gp()) | |
2226 | return; | |
2227 | wait_rcu_gp(call_rcu_bh); | |
2228 | } | |
2229 | EXPORT_SYMBOL_GPL(synchronize_rcu_bh); | |
2230 | ||
2231 | static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0); | |
2232 | static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0); | |
2233 | ||
2234 | static int synchronize_sched_expedited_cpu_stop(void *data) | |
2235 | { | |
2236 | /* | |
2237 | * There must be a full memory barrier on each affected CPU | |
2238 | * between the time that try_stop_cpus() is called and the | |
2239 | * time that it returns. | |
2240 | * | |
2241 | * In the current initial implementation of cpu_stop, the | |
2242 | * above condition is already met when the control reaches | |
2243 | * this point and the following smp_mb() is not strictly | |
2244 | * necessary. Do smp_mb() anyway for documentation and | |
2245 | * robustness against future implementation changes. | |
2246 | */ | |
2247 | smp_mb(); /* See above comment block. */ | |
2248 | return 0; | |
2249 | } | |
2250 | ||
2251 | /** | |
2252 | * synchronize_sched_expedited - Brute-force RCU-sched grace period | |
2253 | * | |
2254 | * Wait for an RCU-sched grace period to elapse, but use a "big hammer" | |
2255 | * approach to force the grace period to end quickly. This consumes | |
2256 | * significant time on all CPUs and is unfriendly to real-time workloads, | |
2257 | * so is thus not recommended for any sort of common-case code. In fact, | |
2258 | * if you are using synchronize_sched_expedited() in a loop, please | |
2259 | * restructure your code to batch your updates, and then use a single | |
2260 | * synchronize_sched() instead. | |
2261 | * | |
2262 | * Note that it is illegal to call this function while holding any lock | |
2263 | * that is acquired by a CPU-hotplug notifier. And yes, it is also illegal | |
2264 | * to call this function from a CPU-hotplug notifier. Failing to observe | |
2265 | * these restriction will result in deadlock. | |
2266 | * | |
2267 | * This implementation can be thought of as an application of ticket | |
2268 | * locking to RCU, with sync_sched_expedited_started and | |
2269 | * sync_sched_expedited_done taking on the roles of the halves | |
2270 | * of the ticket-lock word. Each task atomically increments | |
2271 | * sync_sched_expedited_started upon entry, snapshotting the old value, | |
2272 | * then attempts to stop all the CPUs. If this succeeds, then each | |
2273 | * CPU will have executed a context switch, resulting in an RCU-sched | |
2274 | * grace period. We are then done, so we use atomic_cmpxchg() to | |
2275 | * update sync_sched_expedited_done to match our snapshot -- but | |
2276 | * only if someone else has not already advanced past our snapshot. | |
2277 | * | |
2278 | * On the other hand, if try_stop_cpus() fails, we check the value | |
2279 | * of sync_sched_expedited_done. If it has advanced past our | |
2280 | * initial snapshot, then someone else must have forced a grace period | |
2281 | * some time after we took our snapshot. In this case, our work is | |
2282 | * done for us, and we can simply return. Otherwise, we try again, | |
2283 | * but keep our initial snapshot for purposes of checking for someone | |
2284 | * doing our work for us. | |
2285 | * | |
2286 | * If we fail too many times in a row, we fall back to synchronize_sched(). | |
2287 | */ | |
2288 | void synchronize_sched_expedited(void) | |
2289 | { | |
2290 | int firstsnap, s, snap, trycount = 0; | |
2291 | ||
2292 | /* Note that atomic_inc_return() implies full memory barrier. */ | |
2293 | firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started); | |
2294 | get_online_cpus(); | |
2295 | WARN_ON_ONCE(cpu_is_offline(raw_smp_processor_id())); | |
2296 | ||
2297 | /* | |
2298 | * Each pass through the following loop attempts to force a | |
2299 | * context switch on each CPU. | |
2300 | */ | |
2301 | while (try_stop_cpus(cpu_online_mask, | |
2302 | synchronize_sched_expedited_cpu_stop, | |
2303 | NULL) == -EAGAIN) { | |
2304 | put_online_cpus(); | |
2305 | ||
2306 | /* No joy, try again later. Or just synchronize_sched(). */ | |
2307 | if (trycount++ < 10) { | |
2308 | udelay(trycount * num_online_cpus()); | |
2309 | } else { | |
2310 | synchronize_sched(); | |
2311 | return; | |
2312 | } | |
2313 | ||
2314 | /* Check to see if someone else did our work for us. */ | |
2315 | s = atomic_read(&sync_sched_expedited_done); | |
2316 | if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) { | |
2317 | smp_mb(); /* ensure test happens before caller kfree */ | |
2318 | return; | |
2319 | } | |
2320 | ||
2321 | /* | |
2322 | * Refetching sync_sched_expedited_started allows later | |
2323 | * callers to piggyback on our grace period. We subtract | |
2324 | * 1 to get the same token that the last incrementer got. | |
2325 | * We retry after they started, so our grace period works | |
2326 | * for them, and they started after our first try, so their | |
2327 | * grace period works for us. | |
2328 | */ | |
2329 | get_online_cpus(); | |
2330 | snap = atomic_read(&sync_sched_expedited_started); | |
2331 | smp_mb(); /* ensure read is before try_stop_cpus(). */ | |
2332 | } | |
2333 | ||
2334 | /* | |
2335 | * Everyone up to our most recent fetch is covered by our grace | |
2336 | * period. Update the counter, but only if our work is still | |
2337 | * relevant -- which it won't be if someone who started later | |
2338 | * than we did beat us to the punch. | |
2339 | */ | |
2340 | do { | |
2341 | s = atomic_read(&sync_sched_expedited_done); | |
2342 | if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) { | |
2343 | smp_mb(); /* ensure test happens before caller kfree */ | |
2344 | break; | |
2345 | } | |
2346 | } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s); | |
2347 | ||
2348 | put_online_cpus(); | |
2349 | } | |
2350 | EXPORT_SYMBOL_GPL(synchronize_sched_expedited); | |
2351 | ||
2352 | /* | |
2353 | * Check to see if there is any immediate RCU-related work to be done | |
2354 | * by the current CPU, for the specified type of RCU, returning 1 if so. | |
2355 | * The checks are in order of increasing expense: checks that can be | |
2356 | * carried out against CPU-local state are performed first. However, | |
2357 | * we must check for CPU stalls first, else we might not get a chance. | |
2358 | */ | |
2359 | static int __rcu_pending(struct rcu_state *rsp, struct rcu_data *rdp) | |
2360 | { | |
2361 | struct rcu_node *rnp = rdp->mynode; | |
2362 | ||
2363 | rdp->n_rcu_pending++; | |
2364 | ||
2365 | /* Check for CPU stalls, if enabled. */ | |
2366 | check_cpu_stall(rsp, rdp); | |
2367 | ||
2368 | /* Is the RCU core waiting for a quiescent state from this CPU? */ | |
2369 | if (rcu_scheduler_fully_active && | |
2370 | rdp->qs_pending && !rdp->passed_quiesce) { | |
2371 | rdp->n_rp_qs_pending++; | |
2372 | } else if (rdp->qs_pending && rdp->passed_quiesce) { | |
2373 | rdp->n_rp_report_qs++; | |
2374 | return 1; | |
2375 | } | |
2376 | ||
2377 | /* Does this CPU have callbacks ready to invoke? */ | |
2378 | if (cpu_has_callbacks_ready_to_invoke(rdp)) { | |
2379 | rdp->n_rp_cb_ready++; | |
2380 | return 1; | |
2381 | } | |
2382 | ||
2383 | /* Has RCU gone idle with this CPU needing another grace period? */ | |
2384 | if (cpu_needs_another_gp(rsp, rdp)) { | |
2385 | rdp->n_rp_cpu_needs_gp++; | |
2386 | return 1; | |
2387 | } | |
2388 | ||
2389 | /* Has another RCU grace period completed? */ | |
2390 | if (ACCESS_ONCE(rnp->completed) != rdp->completed) { /* outside lock */ | |
2391 | rdp->n_rp_gp_completed++; | |
2392 | return 1; | |
2393 | } | |
2394 | ||
2395 | /* Has a new RCU grace period started? */ | |
2396 | if (ACCESS_ONCE(rnp->gpnum) != rdp->gpnum) { /* outside lock */ | |
2397 | rdp->n_rp_gp_started++; | |
2398 | return 1; | |
2399 | } | |
2400 | ||
2401 | /* nothing to do */ | |
2402 | rdp->n_rp_need_nothing++; | |
2403 | return 0; | |
2404 | } | |
2405 | ||
2406 | /* | |
2407 | * Check to see if there is any immediate RCU-related work to be done | |
2408 | * by the current CPU, returning 1 if so. This function is part of the | |
2409 | * RCU implementation; it is -not- an exported member of the RCU API. | |
2410 | */ | |
2411 | static int rcu_pending(int cpu) | |
2412 | { | |
2413 | struct rcu_state *rsp; | |
2414 | ||
2415 | for_each_rcu_flavor(rsp) | |
2416 | if (__rcu_pending(rsp, per_cpu_ptr(rsp->rda, cpu))) | |
2417 | return 1; | |
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | /* | |
2422 | * Check to see if any future RCU-related work will need to be done | |
2423 | * by the current CPU, even if none need be done immediately, returning | |
2424 | * 1 if so. | |
2425 | */ | |
2426 | static int rcu_cpu_has_callbacks(int cpu) | |
2427 | { | |
2428 | struct rcu_state *rsp; | |
2429 | ||
2430 | /* RCU callbacks either ready or pending? */ | |
2431 | for_each_rcu_flavor(rsp) | |
2432 | if (per_cpu_ptr(rsp->rda, cpu)->nxtlist) | |
2433 | return 1; | |
2434 | return 0; | |
2435 | } | |
2436 | ||
2437 | /* | |
2438 | * Helper function for _rcu_barrier() tracing. If tracing is disabled, | |
2439 | * the compiler is expected to optimize this away. | |
2440 | */ | |
2441 | static void _rcu_barrier_trace(struct rcu_state *rsp, char *s, | |
2442 | int cpu, unsigned long done) | |
2443 | { | |
2444 | trace_rcu_barrier(rsp->name, s, cpu, | |
2445 | atomic_read(&rsp->barrier_cpu_count), done); | |
2446 | } | |
2447 | ||
2448 | /* | |
2449 | * RCU callback function for _rcu_barrier(). If we are last, wake | |
2450 | * up the task executing _rcu_barrier(). | |
2451 | */ | |
2452 | static void rcu_barrier_callback(struct rcu_head *rhp) | |
2453 | { | |
2454 | struct rcu_data *rdp = container_of(rhp, struct rcu_data, barrier_head); | |
2455 | struct rcu_state *rsp = rdp->rsp; | |
2456 | ||
2457 | if (atomic_dec_and_test(&rsp->barrier_cpu_count)) { | |
2458 | _rcu_barrier_trace(rsp, "LastCB", -1, rsp->n_barrier_done); | |
2459 | complete(&rsp->barrier_completion); | |
2460 | } else { | |
2461 | _rcu_barrier_trace(rsp, "CB", -1, rsp->n_barrier_done); | |
2462 | } | |
2463 | } | |
2464 | ||
2465 | /* | |
2466 | * Called with preemption disabled, and from cross-cpu IRQ context. | |
2467 | */ | |
2468 | static void rcu_barrier_func(void *type) | |
2469 | { | |
2470 | struct rcu_state *rsp = type; | |
2471 | struct rcu_data *rdp = __this_cpu_ptr(rsp->rda); | |
2472 | ||
2473 | _rcu_barrier_trace(rsp, "IRQ", -1, rsp->n_barrier_done); | |
2474 | atomic_inc(&rsp->barrier_cpu_count); | |
2475 | rsp->call(&rdp->barrier_head, rcu_barrier_callback); | |
2476 | } | |
2477 | ||
2478 | /* | |
2479 | * Orchestrate the specified type of RCU barrier, waiting for all | |
2480 | * RCU callbacks of the specified type to complete. | |
2481 | */ | |
2482 | static void _rcu_barrier(struct rcu_state *rsp) | |
2483 | { | |
2484 | int cpu; | |
2485 | struct rcu_data *rdp; | |
2486 | unsigned long snap = ACCESS_ONCE(rsp->n_barrier_done); | |
2487 | unsigned long snap_done; | |
2488 | ||
2489 | _rcu_barrier_trace(rsp, "Begin", -1, snap); | |
2490 | ||
2491 | /* Take mutex to serialize concurrent rcu_barrier() requests. */ | |
2492 | mutex_lock(&rsp->barrier_mutex); | |
2493 | ||
2494 | /* | |
2495 | * Ensure that all prior references, including to ->n_barrier_done, | |
2496 | * are ordered before the _rcu_barrier() machinery. | |
2497 | */ | |
2498 | smp_mb(); /* See above block comment. */ | |
2499 | ||
2500 | /* | |
2501 | * Recheck ->n_barrier_done to see if others did our work for us. | |
2502 | * This means checking ->n_barrier_done for an even-to-odd-to-even | |
2503 | * transition. The "if" expression below therefore rounds the old | |
2504 | * value up to the next even number and adds two before comparing. | |
2505 | */ | |
2506 | snap_done = ACCESS_ONCE(rsp->n_barrier_done); | |
2507 | _rcu_barrier_trace(rsp, "Check", -1, snap_done); | |
2508 | if (ULONG_CMP_GE(snap_done, ((snap + 1) & ~0x1) + 2)) { | |
2509 | _rcu_barrier_trace(rsp, "EarlyExit", -1, snap_done); | |
2510 | smp_mb(); /* caller's subsequent code after above check. */ | |
2511 | mutex_unlock(&rsp->barrier_mutex); | |
2512 | return; | |
2513 | } | |
2514 | ||
2515 | /* | |
2516 | * Increment ->n_barrier_done to avoid duplicate work. Use | |
2517 | * ACCESS_ONCE() to prevent the compiler from speculating | |
2518 | * the increment to precede the early-exit check. | |
2519 | */ | |
2520 | ACCESS_ONCE(rsp->n_barrier_done)++; | |
2521 | WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 1); | |
2522 | _rcu_barrier_trace(rsp, "Inc1", -1, rsp->n_barrier_done); | |
2523 | smp_mb(); /* Order ->n_barrier_done increment with below mechanism. */ | |
2524 | ||
2525 | /* | |
2526 | * Initialize the count to one rather than to zero in order to | |
2527 | * avoid a too-soon return to zero in case of a short grace period | |
2528 | * (or preemption of this task). Exclude CPU-hotplug operations | |
2529 | * to ensure that no offline CPU has callbacks queued. | |
2530 | */ | |
2531 | init_completion(&rsp->barrier_completion); | |
2532 | atomic_set(&rsp->barrier_cpu_count, 1); | |
2533 | get_online_cpus(); | |
2534 | ||
2535 | /* | |
2536 | * Force each CPU with callbacks to register a new callback. | |
2537 | * When that callback is invoked, we will know that all of the | |
2538 | * corresponding CPU's preceding callbacks have been invoked. | |
2539 | */ | |
2540 | for_each_online_cpu(cpu) { | |
2541 | rdp = per_cpu_ptr(rsp->rda, cpu); | |
2542 | if (ACCESS_ONCE(rdp->qlen)) { | |
2543 | _rcu_barrier_trace(rsp, "OnlineQ", cpu, | |
2544 | rsp->n_barrier_done); | |
2545 | smp_call_function_single(cpu, rcu_barrier_func, rsp, 1); | |
2546 | } else { | |
2547 | _rcu_barrier_trace(rsp, "OnlineNQ", cpu, | |
2548 | rsp->n_barrier_done); | |
2549 | } | |
2550 | } | |
2551 | put_online_cpus(); | |
2552 | ||
2553 | /* | |
2554 | * Now that we have an rcu_barrier_callback() callback on each | |
2555 | * CPU, and thus each counted, remove the initial count. | |
2556 | */ | |
2557 | if (atomic_dec_and_test(&rsp->barrier_cpu_count)) | |
2558 | complete(&rsp->barrier_completion); | |
2559 | ||
2560 | /* Increment ->n_barrier_done to prevent duplicate work. */ | |
2561 | smp_mb(); /* Keep increment after above mechanism. */ | |
2562 | ACCESS_ONCE(rsp->n_barrier_done)++; | |
2563 | WARN_ON_ONCE((rsp->n_barrier_done & 0x1) != 0); | |
2564 | _rcu_barrier_trace(rsp, "Inc2", -1, rsp->n_barrier_done); | |
2565 | smp_mb(); /* Keep increment before caller's subsequent code. */ | |
2566 | ||
2567 | /* Wait for all rcu_barrier_callback() callbacks to be invoked. */ | |
2568 | wait_for_completion(&rsp->barrier_completion); | |
2569 | ||
2570 | /* Other rcu_barrier() invocations can now safely proceed. */ | |
2571 | mutex_unlock(&rsp->barrier_mutex); | |
2572 | } | |
2573 | ||
2574 | /** | |
2575 | * rcu_barrier_bh - Wait until all in-flight call_rcu_bh() callbacks complete. | |
2576 | */ | |
2577 | void rcu_barrier_bh(void) | |
2578 | { | |
2579 | _rcu_barrier(&rcu_bh_state); | |
2580 | } | |
2581 | EXPORT_SYMBOL_GPL(rcu_barrier_bh); | |
2582 | ||
2583 | /** | |
2584 | * rcu_barrier_sched - Wait for in-flight call_rcu_sched() callbacks. | |
2585 | */ | |
2586 | void rcu_barrier_sched(void) | |
2587 | { | |
2588 | _rcu_barrier(&rcu_sched_state); | |
2589 | } | |
2590 | EXPORT_SYMBOL_GPL(rcu_barrier_sched); | |
2591 | ||
2592 | /* | |
2593 | * Do boot-time initialization of a CPU's per-CPU RCU data. | |
2594 | */ | |
2595 | static void __init | |
2596 | rcu_boot_init_percpu_data(int cpu, struct rcu_state *rsp) | |
2597 | { | |
2598 | unsigned long flags; | |
2599 | struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu); | |
2600 | struct rcu_node *rnp = rcu_get_root(rsp); | |
2601 | ||
2602 | /* Set up local state, ensuring consistent view of global state. */ | |
2603 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
2604 | rdp->grpmask = 1UL << (cpu - rdp->mynode->grplo); | |
2605 | init_callback_list(rdp); | |
2606 | rdp->qlen_lazy = 0; | |
2607 | ACCESS_ONCE(rdp->qlen) = 0; | |
2608 | rdp->dynticks = &per_cpu(rcu_dynticks, cpu); | |
2609 | WARN_ON_ONCE(rdp->dynticks->dynticks_nesting != DYNTICK_TASK_EXIT_IDLE); | |
2610 | WARN_ON_ONCE(atomic_read(&rdp->dynticks->dynticks) != 1); | |
2611 | #ifdef CONFIG_RCU_USER_QS | |
2612 | WARN_ON_ONCE(rdp->dynticks->in_user); | |
2613 | #endif | |
2614 | rdp->cpu = cpu; | |
2615 | rdp->rsp = rsp; | |
2616 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
2617 | } | |
2618 | ||
2619 | /* | |
2620 | * Initialize a CPU's per-CPU RCU data. Note that only one online or | |
2621 | * offline event can be happening at a given time. Note also that we | |
2622 | * can accept some slop in the rsp->completed access due to the fact | |
2623 | * that this CPU cannot possibly have any RCU callbacks in flight yet. | |
2624 | */ | |
2625 | static void __cpuinit | |
2626 | rcu_init_percpu_data(int cpu, struct rcu_state *rsp, int preemptible) | |
2627 | { | |
2628 | unsigned long flags; | |
2629 | unsigned long mask; | |
2630 | struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu); | |
2631 | struct rcu_node *rnp = rcu_get_root(rsp); | |
2632 | ||
2633 | /* Set up local state, ensuring consistent view of global state. */ | |
2634 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
2635 | rdp->beenonline = 1; /* We have now been online. */ | |
2636 | rdp->preemptible = preemptible; | |
2637 | rdp->qlen_last_fqs_check = 0; | |
2638 | rdp->n_force_qs_snap = rsp->n_force_qs; | |
2639 | rdp->blimit = blimit; | |
2640 | init_callback_list(rdp); /* Re-enable callbacks on this CPU. */ | |
2641 | rdp->dynticks->dynticks_nesting = DYNTICK_TASK_EXIT_IDLE; | |
2642 | atomic_set(&rdp->dynticks->dynticks, | |
2643 | (atomic_read(&rdp->dynticks->dynticks) & ~0x1) + 1); | |
2644 | rcu_prepare_for_idle_init(cpu); | |
2645 | raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */ | |
2646 | ||
2647 | /* | |
2648 | * A new grace period might start here. If so, we won't be part | |
2649 | * of it, but that is OK, as we are currently in a quiescent state. | |
2650 | */ | |
2651 | ||
2652 | /* Exclude any attempts to start a new GP on large systems. */ | |
2653 | raw_spin_lock(&rsp->onofflock); /* irqs already disabled. */ | |
2654 | ||
2655 | /* Add CPU to rcu_node bitmasks. */ | |
2656 | rnp = rdp->mynode; | |
2657 | mask = rdp->grpmask; | |
2658 | do { | |
2659 | /* Exclude any attempts to start a new GP on small systems. */ | |
2660 | raw_spin_lock(&rnp->lock); /* irqs already disabled. */ | |
2661 | rnp->qsmaskinit |= mask; | |
2662 | mask = rnp->grpmask; | |
2663 | if (rnp == rdp->mynode) { | |
2664 | /* | |
2665 | * If there is a grace period in progress, we will | |
2666 | * set up to wait for it next time we run the | |
2667 | * RCU core code. | |
2668 | */ | |
2669 | rdp->gpnum = rnp->completed; | |
2670 | rdp->completed = rnp->completed; | |
2671 | rdp->passed_quiesce = 0; | |
2672 | rdp->qs_pending = 0; | |
2673 | trace_rcu_grace_period(rsp->name, rdp->gpnum, "cpuonl"); | |
2674 | } | |
2675 | raw_spin_unlock(&rnp->lock); /* irqs already disabled. */ | |
2676 | rnp = rnp->parent; | |
2677 | } while (rnp != NULL && !(rnp->qsmaskinit & mask)); | |
2678 | ||
2679 | raw_spin_unlock_irqrestore(&rsp->onofflock, flags); | |
2680 | } | |
2681 | ||
2682 | static void __cpuinit rcu_prepare_cpu(int cpu) | |
2683 | { | |
2684 | struct rcu_state *rsp; | |
2685 | ||
2686 | for_each_rcu_flavor(rsp) | |
2687 | rcu_init_percpu_data(cpu, rsp, | |
2688 | strcmp(rsp->name, "rcu_preempt") == 0); | |
2689 | } | |
2690 | ||
2691 | /* | |
2692 | * Handle CPU online/offline notification events. | |
2693 | */ | |
2694 | static int __cpuinit rcu_cpu_notify(struct notifier_block *self, | |
2695 | unsigned long action, void *hcpu) | |
2696 | { | |
2697 | long cpu = (long)hcpu; | |
2698 | struct rcu_data *rdp = per_cpu_ptr(rcu_state->rda, cpu); | |
2699 | struct rcu_node *rnp = rdp->mynode; | |
2700 | struct rcu_state *rsp; | |
2701 | ||
2702 | trace_rcu_utilization("Start CPU hotplug"); | |
2703 | switch (action) { | |
2704 | case CPU_UP_PREPARE: | |
2705 | case CPU_UP_PREPARE_FROZEN: | |
2706 | rcu_prepare_cpu(cpu); | |
2707 | rcu_prepare_kthreads(cpu); | |
2708 | break; | |
2709 | case CPU_ONLINE: | |
2710 | case CPU_DOWN_FAILED: | |
2711 | rcu_boost_kthread_setaffinity(rnp, -1); | |
2712 | break; | |
2713 | case CPU_DOWN_PREPARE: | |
2714 | rcu_boost_kthread_setaffinity(rnp, cpu); | |
2715 | break; | |
2716 | case CPU_DYING: | |
2717 | case CPU_DYING_FROZEN: | |
2718 | /* | |
2719 | * The whole machine is "stopped" except this CPU, so we can | |
2720 | * touch any data without introducing corruption. We send the | |
2721 | * dying CPU's callbacks to an arbitrarily chosen online CPU. | |
2722 | */ | |
2723 | for_each_rcu_flavor(rsp) | |
2724 | rcu_cleanup_dying_cpu(rsp); | |
2725 | rcu_cleanup_after_idle(cpu); | |
2726 | break; | |
2727 | case CPU_DEAD: | |
2728 | case CPU_DEAD_FROZEN: | |
2729 | case CPU_UP_CANCELED: | |
2730 | case CPU_UP_CANCELED_FROZEN: | |
2731 | for_each_rcu_flavor(rsp) | |
2732 | rcu_cleanup_dead_cpu(cpu, rsp); | |
2733 | break; | |
2734 | default: | |
2735 | break; | |
2736 | } | |
2737 | trace_rcu_utilization("End CPU hotplug"); | |
2738 | return NOTIFY_OK; | |
2739 | } | |
2740 | ||
2741 | /* | |
2742 | * Spawn the kthread that handles this RCU flavor's grace periods. | |
2743 | */ | |
2744 | static int __init rcu_spawn_gp_kthread(void) | |
2745 | { | |
2746 | unsigned long flags; | |
2747 | struct rcu_node *rnp; | |
2748 | struct rcu_state *rsp; | |
2749 | struct task_struct *t; | |
2750 | ||
2751 | for_each_rcu_flavor(rsp) { | |
2752 | t = kthread_run(rcu_gp_kthread, rsp, rsp->name); | |
2753 | BUG_ON(IS_ERR(t)); | |
2754 | rnp = rcu_get_root(rsp); | |
2755 | raw_spin_lock_irqsave(&rnp->lock, flags); | |
2756 | rsp->gp_kthread = t; | |
2757 | raw_spin_unlock_irqrestore(&rnp->lock, flags); | |
2758 | } | |
2759 | return 0; | |
2760 | } | |
2761 | early_initcall(rcu_spawn_gp_kthread); | |
2762 | ||
2763 | /* | |
2764 | * This function is invoked towards the end of the scheduler's initialization | |
2765 | * process. Before this is called, the idle task might contain | |
2766 | * RCU read-side critical sections (during which time, this idle | |
2767 | * task is booting the system). After this function is called, the | |
2768 | * idle tasks are prohibited from containing RCU read-side critical | |
2769 | * sections. This function also enables RCU lockdep checking. | |
2770 | */ | |
2771 | void rcu_scheduler_starting(void) | |
2772 | { | |
2773 | WARN_ON(num_online_cpus() != 1); | |
2774 | WARN_ON(nr_context_switches() > 0); | |
2775 | rcu_scheduler_active = 1; | |
2776 | } | |
2777 | ||
2778 | /* | |
2779 | * Compute the per-level fanout, either using the exact fanout specified | |
2780 | * or balancing the tree, depending on CONFIG_RCU_FANOUT_EXACT. | |
2781 | */ | |
2782 | #ifdef CONFIG_RCU_FANOUT_EXACT | |
2783 | static void __init rcu_init_levelspread(struct rcu_state *rsp) | |
2784 | { | |
2785 | int i; | |
2786 | ||
2787 | for (i = rcu_num_lvls - 1; i > 0; i--) | |
2788 | rsp->levelspread[i] = CONFIG_RCU_FANOUT; | |
2789 | rsp->levelspread[0] = rcu_fanout_leaf; | |
2790 | } | |
2791 | #else /* #ifdef CONFIG_RCU_FANOUT_EXACT */ | |
2792 | static void __init rcu_init_levelspread(struct rcu_state *rsp) | |
2793 | { | |
2794 | int ccur; | |
2795 | int cprv; | |
2796 | int i; | |
2797 | ||
2798 | cprv = nr_cpu_ids; | |
2799 | for (i = rcu_num_lvls - 1; i >= 0; i--) { | |
2800 | ccur = rsp->levelcnt[i]; | |
2801 | rsp->levelspread[i] = (cprv + ccur - 1) / ccur; | |
2802 | cprv = ccur; | |
2803 | } | |
2804 | } | |
2805 | #endif /* #else #ifdef CONFIG_RCU_FANOUT_EXACT */ | |
2806 | ||
2807 | /* | |
2808 | * Helper function for rcu_init() that initializes one rcu_state structure. | |
2809 | */ | |
2810 | static void __init rcu_init_one(struct rcu_state *rsp, | |
2811 | struct rcu_data __percpu *rda) | |
2812 | { | |
2813 | static char *buf[] = { "rcu_node_0", | |
2814 | "rcu_node_1", | |
2815 | "rcu_node_2", | |
2816 | "rcu_node_3" }; /* Match MAX_RCU_LVLS */ | |
2817 | static char *fqs[] = { "rcu_node_fqs_0", | |
2818 | "rcu_node_fqs_1", | |
2819 | "rcu_node_fqs_2", | |
2820 | "rcu_node_fqs_3" }; /* Match MAX_RCU_LVLS */ | |
2821 | int cpustride = 1; | |
2822 | int i; | |
2823 | int j; | |
2824 | struct rcu_node *rnp; | |
2825 | ||
2826 | BUILD_BUG_ON(MAX_RCU_LVLS > ARRAY_SIZE(buf)); /* Fix buf[] init! */ | |
2827 | ||
2828 | /* Initialize the level-tracking arrays. */ | |
2829 | ||
2830 | for (i = 0; i < rcu_num_lvls; i++) | |
2831 | rsp->levelcnt[i] = num_rcu_lvl[i]; | |
2832 | for (i = 1; i < rcu_num_lvls; i++) | |
2833 | rsp->level[i] = rsp->level[i - 1] + rsp->levelcnt[i - 1]; | |
2834 | rcu_init_levelspread(rsp); | |
2835 | ||
2836 | /* Initialize the elements themselves, starting from the leaves. */ | |
2837 | ||
2838 | for (i = rcu_num_lvls - 1; i >= 0; i--) { | |
2839 | cpustride *= rsp->levelspread[i]; | |
2840 | rnp = rsp->level[i]; | |
2841 | for (j = 0; j < rsp->levelcnt[i]; j++, rnp++) { | |
2842 | raw_spin_lock_init(&rnp->lock); | |
2843 | lockdep_set_class_and_name(&rnp->lock, | |
2844 | &rcu_node_class[i], buf[i]); | |
2845 | raw_spin_lock_init(&rnp->fqslock); | |
2846 | lockdep_set_class_and_name(&rnp->fqslock, | |
2847 | &rcu_fqs_class[i], fqs[i]); | |
2848 | rnp->gpnum = rsp->gpnum; | |
2849 | rnp->completed = rsp->completed; | |
2850 | rnp->qsmask = 0; | |
2851 | rnp->qsmaskinit = 0; | |
2852 | rnp->grplo = j * cpustride; | |
2853 | rnp->grphi = (j + 1) * cpustride - 1; | |
2854 | if (rnp->grphi >= NR_CPUS) | |
2855 | rnp->grphi = NR_CPUS - 1; | |
2856 | if (i == 0) { | |
2857 | rnp->grpnum = 0; | |
2858 | rnp->grpmask = 0; | |
2859 | rnp->parent = NULL; | |
2860 | } else { | |
2861 | rnp->grpnum = j % rsp->levelspread[i - 1]; | |
2862 | rnp->grpmask = 1UL << rnp->grpnum; | |
2863 | rnp->parent = rsp->level[i - 1] + | |
2864 | j / rsp->levelspread[i - 1]; | |
2865 | } | |
2866 | rnp->level = i; | |
2867 | INIT_LIST_HEAD(&rnp->blkd_tasks); | |
2868 | } | |
2869 | } | |
2870 | ||
2871 | rsp->rda = rda; | |
2872 | init_waitqueue_head(&rsp->gp_wq); | |
2873 | rnp = rsp->level[rcu_num_lvls - 1]; | |
2874 | for_each_possible_cpu(i) { | |
2875 | while (i > rnp->grphi) | |
2876 | rnp++; | |
2877 | per_cpu_ptr(rsp->rda, i)->mynode = rnp; | |
2878 | rcu_boot_init_percpu_data(i, rsp); | |
2879 | } | |
2880 | list_add(&rsp->flavors, &rcu_struct_flavors); | |
2881 | } | |
2882 | ||
2883 | /* | |
2884 | * Compute the rcu_node tree geometry from kernel parameters. This cannot | |
2885 | * replace the definitions in rcutree.h because those are needed to size | |
2886 | * the ->node array in the rcu_state structure. | |
2887 | */ | |
2888 | static void __init rcu_init_geometry(void) | |
2889 | { | |
2890 | int i; | |
2891 | int j; | |
2892 | int n = nr_cpu_ids; | |
2893 | int rcu_capacity[MAX_RCU_LVLS + 1]; | |
2894 | ||
2895 | /* If the compile-time values are accurate, just leave. */ | |
2896 | if (rcu_fanout_leaf == CONFIG_RCU_FANOUT_LEAF && | |
2897 | nr_cpu_ids == NR_CPUS) | |
2898 | return; | |
2899 | ||
2900 | /* | |
2901 | * Compute number of nodes that can be handled an rcu_node tree | |
2902 | * with the given number of levels. Setting rcu_capacity[0] makes | |
2903 | * some of the arithmetic easier. | |
2904 | */ | |
2905 | rcu_capacity[0] = 1; | |
2906 | rcu_capacity[1] = rcu_fanout_leaf; | |
2907 | for (i = 2; i <= MAX_RCU_LVLS; i++) | |
2908 | rcu_capacity[i] = rcu_capacity[i - 1] * CONFIG_RCU_FANOUT; | |
2909 | ||
2910 | /* | |
2911 | * The boot-time rcu_fanout_leaf parameter is only permitted | |
2912 | * to increase the leaf-level fanout, not decrease it. Of course, | |
2913 | * the leaf-level fanout cannot exceed the number of bits in | |
2914 | * the rcu_node masks. Finally, the tree must be able to accommodate | |
2915 | * the configured number of CPUs. Complain and fall back to the | |
2916 | * compile-time values if these limits are exceeded. | |
2917 | */ | |
2918 | if (rcu_fanout_leaf < CONFIG_RCU_FANOUT_LEAF || | |
2919 | rcu_fanout_leaf > sizeof(unsigned long) * 8 || | |
2920 | n > rcu_capacity[MAX_RCU_LVLS]) { | |
2921 | WARN_ON(1); | |
2922 | return; | |
2923 | } | |
2924 | ||
2925 | /* Calculate the number of rcu_nodes at each level of the tree. */ | |
2926 | for (i = 1; i <= MAX_RCU_LVLS; i++) | |
2927 | if (n <= rcu_capacity[i]) { | |
2928 | for (j = 0; j <= i; j++) | |
2929 | num_rcu_lvl[j] = | |
2930 | DIV_ROUND_UP(n, rcu_capacity[i - j]); | |
2931 | rcu_num_lvls = i; | |
2932 | for (j = i + 1; j <= MAX_RCU_LVLS; j++) | |
2933 | num_rcu_lvl[j] = 0; | |
2934 | break; | |
2935 | } | |
2936 | ||
2937 | /* Calculate the total number of rcu_node structures. */ | |
2938 | rcu_num_nodes = 0; | |
2939 | for (i = 0; i <= MAX_RCU_LVLS; i++) | |
2940 | rcu_num_nodes += num_rcu_lvl[i]; | |
2941 | rcu_num_nodes -= n; | |
2942 | } | |
2943 | ||
2944 | void __init rcu_init(void) | |
2945 | { | |
2946 | int cpu; | |
2947 | ||
2948 | rcu_bootup_announce(); | |
2949 | rcu_init_geometry(); | |
2950 | rcu_init_one(&rcu_sched_state, &rcu_sched_data); | |
2951 | rcu_init_one(&rcu_bh_state, &rcu_bh_data); | |
2952 | __rcu_init_preempt(); | |
2953 | open_softirq(RCU_SOFTIRQ, rcu_process_callbacks); | |
2954 | ||
2955 | /* | |
2956 | * We don't need protection against CPU-hotplug here because | |
2957 | * this is called early in boot, before either interrupts | |
2958 | * or the scheduler are operational. | |
2959 | */ | |
2960 | cpu_notifier(rcu_cpu_notify, 0); | |
2961 | for_each_online_cpu(cpu) | |
2962 | rcu_cpu_notify(NULL, CPU_UP_PREPARE, (void *)(long)cpu); | |
2963 | check_cpu_stall_init(); | |
2964 | } | |
2965 | ||
2966 | #include "rcutree_plugin.h" |