drm/i915: Remove GPU reset dependence on struct_mutex
[linux-2.6-block.git] / kernel / rcu / tree_exp.h
CommitLineData
3549c2bc
PM
1/*
2 * RCU expedited grace periods
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, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
17 *
18 * Copyright IBM Corporation, 2016
19 *
20 * Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
21 */
22
55ebfce0
BF
23#include <linux/lockdep.h>
24
09e2db37
PM
25/*
26 * Record the start of an expedited grace period.
27 */
63d4c8c9 28static void rcu_exp_gp_seq_start(void)
3549c2bc 29{
63d4c8c9 30 rcu_seq_start(&rcu_state.expedited_sequence);
3549c2bc 31}
09e2db37 32
9a414201
PM
33/*
34 * Return then value that expedited-grace-period counter will have
35 * at the end of the current grace period.
36 */
63d4c8c9 37static __maybe_unused unsigned long rcu_exp_gp_seq_endval(void)
9a414201 38{
63d4c8c9 39 return rcu_seq_endval(&rcu_state.expedited_sequence);
9a414201
PM
40}
41
09e2db37
PM
42/*
43 * Record the end of an expedited grace period.
44 */
63d4c8c9 45static void rcu_exp_gp_seq_end(void)
3549c2bc 46{
63d4c8c9 47 rcu_seq_end(&rcu_state.expedited_sequence);
3549c2bc
PM
48 smp_mb(); /* Ensure that consecutive grace periods serialize. */
49}
09e2db37
PM
50
51/*
52 * Take a snapshot of the expedited-grace-period counter.
53 */
63d4c8c9 54static unsigned long rcu_exp_gp_seq_snap(void)
3549c2bc
PM
55{
56 unsigned long s;
57
58 smp_mb(); /* Caller's modifications seen first by other CPUs. */
63d4c8c9
PM
59 s = rcu_seq_snap(&rcu_state.expedited_sequence);
60 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("snap"));
3549c2bc
PM
61 return s;
62}
09e2db37
PM
63
64/*
65 * Given a counter snapshot from rcu_exp_gp_seq_snap(), return true
66 * if a full expedited grace period has elapsed since that snapshot
67 * was taken.
68 */
63d4c8c9 69static bool rcu_exp_gp_seq_done(unsigned long s)
3549c2bc 70{
63d4c8c9 71 return rcu_seq_done(&rcu_state.expedited_sequence, s);
3549c2bc
PM
72}
73
74/*
75 * Reset the ->expmaskinit values in the rcu_node tree to reflect any
76 * recent CPU-online activity. Note that these masks are not cleared
77 * when CPUs go offline, so they reflect the union of all CPUs that have
78 * ever been online. This means that this function normally takes its
79 * no-work-to-do fastpath.
80 */
63d4c8c9 81static void sync_exp_reset_tree_hotplug(void)
3549c2bc
PM
82{
83 bool done;
84 unsigned long flags;
85 unsigned long mask;
86 unsigned long oldmask;
63d4c8c9 87 int ncpus = smp_load_acquire(&rcu_state.ncpus); /* Order vs. locking. */
3549c2bc
PM
88 struct rcu_node *rnp;
89 struct rcu_node *rnp_up;
90
91 /* If no new CPUs onlined since last time, nothing to do. */
63d4c8c9 92 if (likely(ncpus == rcu_state.ncpus_snap))
3549c2bc 93 return;
63d4c8c9 94 rcu_state.ncpus_snap = ncpus;
3549c2bc
PM
95
96 /*
97 * Each pass through the following loop propagates newly onlined
98 * CPUs for the current rcu_node structure up the rcu_node tree.
99 */
aedf4ba9 100 rcu_for_each_leaf_node(rnp) {
3549c2bc
PM
101 raw_spin_lock_irqsave_rcu_node(rnp, flags);
102 if (rnp->expmaskinit == rnp->expmaskinitnext) {
103 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
104 continue; /* No new CPUs, nothing to do. */
105 }
106
107 /* Update this node's mask, track old value for propagation. */
108 oldmask = rnp->expmaskinit;
109 rnp->expmaskinit = rnp->expmaskinitnext;
110 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
111
112 /* If was already nonzero, nothing to propagate. */
113 if (oldmask)
114 continue;
115
116 /* Propagate the new CPU up the tree. */
117 mask = rnp->grpmask;
118 rnp_up = rnp->parent;
119 done = false;
120 while (rnp_up) {
121 raw_spin_lock_irqsave_rcu_node(rnp_up, flags);
122 if (rnp_up->expmaskinit)
123 done = true;
124 rnp_up->expmaskinit |= mask;
125 raw_spin_unlock_irqrestore_rcu_node(rnp_up, flags);
126 if (done)
127 break;
128 mask = rnp_up->grpmask;
129 rnp_up = rnp_up->parent;
130 }
131 }
132}
133
134/*
135 * Reset the ->expmask values in the rcu_node tree in preparation for
136 * a new expedited grace period.
137 */
63d4c8c9 138static void __maybe_unused sync_exp_reset_tree(void)
3549c2bc
PM
139{
140 unsigned long flags;
141 struct rcu_node *rnp;
142
63d4c8c9 143 sync_exp_reset_tree_hotplug();
aedf4ba9 144 rcu_for_each_node_breadth_first(rnp) {
3549c2bc
PM
145 raw_spin_lock_irqsave_rcu_node(rnp, flags);
146 WARN_ON_ONCE(rnp->expmask);
147 rnp->expmask = rnp->expmaskinit;
148 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
149 }
150}
151
152/*
153 * Return non-zero if there is no RCU expedited grace period in progress
154 * for the specified rcu_node structure, in other words, if all CPUs and
155 * tasks covered by the specified rcu_node structure have done their bit
156 * for the current expedited grace period. Works only for preemptible
157 * RCU -- other RCU implementation use other means.
158 *
7be8c56f 159 * Caller must hold the specificed rcu_node structure's ->lock
3549c2bc 160 */
dcfc315b 161static bool sync_rcu_preempt_exp_done(struct rcu_node *rnp)
3549c2bc 162{
55ebfce0
BF
163 raw_lockdep_assert_held_rcu_node(rnp);
164
3549c2bc
PM
165 return rnp->exp_tasks == NULL &&
166 READ_ONCE(rnp->expmask) == 0;
167}
168
55ebfce0
BF
169/*
170 * Like sync_rcu_preempt_exp_done(), but this function assumes the caller
171 * doesn't hold the rcu_node's ->lock, and will acquire and release the lock
172 * itself
173 */
174static bool sync_rcu_preempt_exp_done_unlocked(struct rcu_node *rnp)
175{
176 unsigned long flags;
177 bool ret;
178
179 raw_spin_lock_irqsave_rcu_node(rnp, flags);
180 ret = sync_rcu_preempt_exp_done(rnp);
181 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
182
183 return ret;
184}
185
186
3549c2bc
PM
187/*
188 * Report the exit from RCU read-side critical section for the last task
189 * that queued itself during or before the current expedited preemptible-RCU
190 * grace period. This event is reported either to the rcu_node structure on
191 * which the task was queued or to one of that rcu_node structure's ancestors,
192 * recursively up the tree. (Calm down, calm down, we do the recursion
193 * iteratively!)
194 *
7be8c56f 195 * Caller must hold the specified rcu_node structure's ->lock.
3549c2bc 196 */
63d4c8c9 197static void __rcu_report_exp_rnp(struct rcu_node *rnp,
3549c2bc
PM
198 bool wake, unsigned long flags)
199 __releases(rnp->lock)
200{
201 unsigned long mask;
202
203 for (;;) {
204 if (!sync_rcu_preempt_exp_done(rnp)) {
205 if (!rnp->expmask)
206 rcu_initiate_boost(rnp, flags);
207 else
208 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
209 break;
210 }
211 if (rnp->parent == NULL) {
212 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
213 if (wake) {
214 smp_mb(); /* EGP done before wake_up(). */
63d4c8c9 215 swake_up_one(&rcu_state.expedited_wq);
3549c2bc
PM
216 }
217 break;
218 }
219 mask = rnp->grpmask;
220 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled */
221 rnp = rnp->parent;
222 raw_spin_lock_rcu_node(rnp); /* irqs already disabled */
223 WARN_ON_ONCE(!(rnp->expmask & mask));
224 rnp->expmask &= ~mask;
225 }
226}
227
228/*
229 * Report expedited quiescent state for specified node. This is a
230 * lock-acquisition wrapper function for __rcu_report_exp_rnp().
3549c2bc 231 */
63d4c8c9 232static void __maybe_unused rcu_report_exp_rnp(struct rcu_node *rnp, bool wake)
3549c2bc
PM
233{
234 unsigned long flags;
235
236 raw_spin_lock_irqsave_rcu_node(rnp, flags);
63d4c8c9 237 __rcu_report_exp_rnp(rnp, wake, flags);
3549c2bc
PM
238}
239
240/*
241 * Report expedited quiescent state for multiple CPUs, all covered by the
7be8c56f 242 * specified leaf rcu_node structure.
3549c2bc 243 */
63d4c8c9 244static void rcu_report_exp_cpu_mult(struct rcu_node *rnp,
3549c2bc
PM
245 unsigned long mask, bool wake)
246{
247 unsigned long flags;
248
249 raw_spin_lock_irqsave_rcu_node(rnp, flags);
250 if (!(rnp->expmask & mask)) {
251 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
252 return;
253 }
254 rnp->expmask &= ~mask;
63d4c8c9 255 __rcu_report_exp_rnp(rnp, wake, flags); /* Releases rnp->lock. */
3549c2bc
PM
256}
257
258/*
259 * Report expedited quiescent state for specified rcu_data (CPU).
260 */
63d4c8c9 261static void rcu_report_exp_rdp(struct rcu_data *rdp)
3549c2bc 262{
3e310098 263 WRITE_ONCE(rdp->deferred_qs, false);
63d4c8c9 264 rcu_report_exp_cpu_mult(rdp->mynode, rdp->grpmask, true);
3549c2bc
PM
265}
266
45975c7d 267/* Common code for work-done checking. */
63d4c8c9 268static bool sync_exp_work_done(unsigned long s)
3549c2bc 269{
63d4c8c9
PM
270 if (rcu_exp_gp_seq_done(s)) {
271 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("done"));
3549c2bc
PM
272 /* Ensure test happens before caller kfree(). */
273 smp_mb__before_atomic(); /* ^^^ */
3549c2bc
PM
274 return true;
275 }
276 return false;
277}
278
279/*
280 * Funnel-lock acquisition for expedited grace periods. Returns true
281 * if some other task completed an expedited grace period that this task
282 * can piggy-back on, and with no mutex held. Otherwise, returns false
283 * with the mutex held, indicating that the caller must actually do the
284 * expedited grace period.
285 */
63d4c8c9 286static bool exp_funnel_lock(unsigned long s)
3549c2bc 287{
da1df50d 288 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, raw_smp_processor_id());
3549c2bc 289 struct rcu_node *rnp = rdp->mynode;
336a4f6c 290 struct rcu_node *rnp_root = rcu_get_root();
3549c2bc
PM
291
292 /* Low-contention fastpath. */
293 if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s) &&
294 (rnp == rnp_root ||
295 ULONG_CMP_LT(READ_ONCE(rnp_root->exp_seq_rq), s)) &&
63d4c8c9 296 mutex_trylock(&rcu_state.exp_mutex))
3549c2bc
PM
297 goto fastpath;
298
299 /*
300 * Each pass through the following loop works its way up
301 * the rcu_node tree, returning if others have done the work or
63d4c8c9 302 * otherwise falls through to acquire ->exp_mutex. The mapping
3549c2bc
PM
303 * from CPU to rcu_node structure can be inexact, as it is just
304 * promoting locality and is not strictly needed for correctness.
305 */
306 for (; rnp != NULL; rnp = rnp->parent) {
63d4c8c9 307 if (sync_exp_work_done(s))
3549c2bc
PM
308 return true;
309
310 /* Work not done, either wait here or go up. */
311 spin_lock(&rnp->exp_lock);
312 if (ULONG_CMP_GE(rnp->exp_seq_rq, s)) {
313
314 /* Someone else doing GP, so wait for them. */
315 spin_unlock(&rnp->exp_lock);
63d4c8c9 316 trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level,
3549c2bc
PM
317 rnp->grplo, rnp->grphi,
318 TPS("wait"));
031aeee0 319 wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],
63d4c8c9 320 sync_exp_work_done(s));
3549c2bc
PM
321 return true;
322 }
323 rnp->exp_seq_rq = s; /* Followers can wait on us. */
324 spin_unlock(&rnp->exp_lock);
63d4c8c9
PM
325 trace_rcu_exp_funnel_lock(rcu_state.name, rnp->level,
326 rnp->grplo, rnp->grphi, TPS("nxtlvl"));
3549c2bc 327 }
63d4c8c9 328 mutex_lock(&rcu_state.exp_mutex);
3549c2bc 329fastpath:
63d4c8c9
PM
330 if (sync_exp_work_done(s)) {
331 mutex_unlock(&rcu_state.exp_mutex);
3549c2bc
PM
332 return true;
333 }
63d4c8c9
PM
334 rcu_exp_gp_seq_start();
335 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("start"));
3549c2bc
PM
336 return false;
337}
338
3549c2bc 339/*
25f3d7ef
PM
340 * Select the CPUs within the specified rcu_node that the upcoming
341 * expedited grace period needs to wait for.
3549c2bc 342 */
25f3d7ef 343static void sync_rcu_exp_select_node_cpus(struct work_struct *wp)
3549c2bc
PM
344{
345 int cpu;
346 unsigned long flags;
25f3d7ef 347 smp_call_func_t func;
3549c2bc
PM
348 unsigned long mask_ofl_test;
349 unsigned long mask_ofl_ipi;
350 int ret;
25f3d7ef
PM
351 struct rcu_exp_work *rewp =
352 container_of(wp, struct rcu_exp_work, rew_work);
353 struct rcu_node *rnp = container_of(rewp, struct rcu_node, rew);
3549c2bc 354
25f3d7ef
PM
355 func = rewp->rew_func;
356 raw_spin_lock_irqsave_rcu_node(rnp, flags);
3549c2bc 357
25f3d7ef
PM
358 /* Each pass checks a CPU for identity, offline, and idle. */
359 mask_ofl_test = 0;
360 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {
361 unsigned long mask = leaf_node_cpu_bit(rnp, cpu);
da1df50d 362 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
25f3d7ef
PM
363 int snap;
364
365 if (raw_smp_processor_id() == cpu ||
366 !(rnp->qsmaskinitnext & mask)) {
367 mask_ofl_test |= mask;
368 } else {
dc5a4f29 369 snap = rcu_dynticks_snap(rdp);
25f3d7ef 370 if (rcu_dynticks_in_eqs(snap))
65963d24 371 mask_ofl_test |= mask;
25f3d7ef
PM
372 else
373 rdp->exp_dynticks_snap = snap;
3549c2bc 374 }
25f3d7ef
PM
375 }
376 mask_ofl_ipi = rnp->expmask & ~mask_ofl_test;
3549c2bc 377
25f3d7ef
PM
378 /*
379 * Need to wait for any blocked tasks as well. Note that
380 * additional blocking tasks will also block the expedited GP
381 * until such time as the ->expmask bits are cleared.
382 */
383 if (rcu_preempt_has_tasks(rnp))
384 rnp->exp_tasks = rnp->blkd_tasks.next;
385 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
386
387 /* IPI the remaining CPUs for expedited quiescent state. */
388 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {
389 unsigned long mask = leaf_node_cpu_bit(rnp, cpu);
da1df50d 390 struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
0742ac3e 391
25f3d7ef
PM
392 if (!(mask_ofl_ipi & mask))
393 continue;
3549c2bc 394retry_ipi:
dc5a4f29 395 if (rcu_dynticks_in_eqs_since(rdp, rdp->exp_dynticks_snap)) {
25f3d7ef
PM
396 mask_ofl_test |= mask;
397 continue;
398 }
63d4c8c9 399 ret = smp_call_function_single(cpu, func, NULL, 0);
25f3d7ef
PM
400 if (!ret) {
401 mask_ofl_ipi &= ~mask;
402 continue;
403 }
404 /* Failed, raced with CPU hotplug operation. */
405 raw_spin_lock_irqsave_rcu_node(rnp, flags);
406 if ((rnp->qsmaskinitnext & mask) &&
407 (rnp->expmask & mask)) {
408 /* Online, so delay for a bit and try again. */
3549c2bc 409 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
63d4c8c9 410 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("selectofl"));
25f3d7ef
PM
411 schedule_timeout_uninterruptible(1);
412 goto retry_ipi;
413 }
414 /* CPU really is offline, so we can ignore it. */
415 if (!(rnp->expmask & mask))
416 mask_ofl_ipi &= ~mask;
417 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
418 }
419 /* Report quiescent states for those that went offline. */
420 mask_ofl_test |= mask_ofl_ipi;
421 if (mask_ofl_test)
63d4c8c9 422 rcu_report_exp_cpu_mult(rnp, mask_ofl_test, false);
25f3d7ef
PM
423}
424
425/*
426 * Select the nodes that the upcoming expedited grace period needs
427 * to wait for.
428 */
63d4c8c9 429static void sync_rcu_exp_select_cpus(smp_call_func_t func)
25f3d7ef 430{
fcc63543 431 int cpu;
25f3d7ef
PM
432 struct rcu_node *rnp;
433
63d4c8c9
PM
434 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("reset"));
435 sync_exp_reset_tree();
436 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("select"));
25f3d7ef
PM
437
438 /* Schedule work for each leaf rcu_node structure. */
aedf4ba9 439 rcu_for_each_leaf_node(rnp) {
25f3d7ef
PM
440 rnp->exp_need_flush = false;
441 if (!READ_ONCE(rnp->expmask))
442 continue; /* Avoid early boot non-existent wq. */
443 rnp->rew.rew_func = func;
25f3d7ef 444 if (!READ_ONCE(rcu_par_gp_wq) ||
5257514d 445 rcu_scheduler_active != RCU_SCHEDULER_RUNNING ||
aedf4ba9 446 rcu_is_last_leaf_node(rnp)) {
5257514d 447 /* No workqueues yet or last leaf, do direct call. */
25f3d7ef
PM
448 sync_rcu_exp_select_node_cpus(&rnp->rew.rew_work);
449 continue;
3549c2bc 450 }
25f3d7ef 451 INIT_WORK(&rnp->rew.rew_work, sync_rcu_exp_select_node_cpus);
fcc63543 452 preempt_disable();
9cac83a5 453 cpu = find_next_bit(&rnp->ffmask, BITS_PER_LONG, -1);
fcc63543 454 /* If all offline, queue the work on an unbound CPU. */
9cac83a5 455 if (unlikely(cpu > rnp->grphi - rnp->grplo))
fcc63543 456 cpu = WORK_CPU_UNBOUND;
9cac83a5
PM
457 else
458 cpu += rnp->grplo;
fcc63543
BF
459 queue_work_on(cpu, rcu_par_gp_wq, &rnp->rew.rew_work);
460 preempt_enable();
25f3d7ef 461 rnp->exp_need_flush = true;
3549c2bc 462 }
25f3d7ef
PM
463
464 /* Wait for workqueue jobs (if any) to complete. */
aedf4ba9 465 rcu_for_each_leaf_node(rnp)
25f3d7ef
PM
466 if (rnp->exp_need_flush)
467 flush_work(&rnp->rew.rew_work);
3549c2bc
PM
468}
469
63d4c8c9 470static void synchronize_sched_expedited_wait(void)
3549c2bc
PM
471{
472 int cpu;
473 unsigned long jiffies_stall;
474 unsigned long jiffies_start;
475 unsigned long mask;
476 int ndetected;
477 struct rcu_node *rnp;
336a4f6c 478 struct rcu_node *rnp_root = rcu_get_root();
3549c2bc
PM
479 int ret;
480
63d4c8c9 481 trace_rcu_exp_grace_period(rcu_state.name, rcu_exp_gp_seq_endval(), TPS("startwait"));
3549c2bc
PM
482 jiffies_stall = rcu_jiffies_till_stall_check();
483 jiffies_start = jiffies;
484
485 for (;;) {
b3dae109 486 ret = swait_event_timeout_exclusive(
63d4c8c9 487 rcu_state.expedited_wq,
55ebfce0 488 sync_rcu_preempt_exp_done_unlocked(rnp_root),
3549c2bc 489 jiffies_stall);
55ebfce0 490 if (ret > 0 || sync_rcu_preempt_exp_done_unlocked(rnp_root))
3549c2bc 491 return;
908d2c1f 492 WARN_ON(ret < 0); /* workqueues should not be signaled. */
24a6cff2
PM
493 if (rcu_cpu_stall_suppress)
494 continue;
495 panic_on_rcu_stall();
3549c2bc 496 pr_err("INFO: %s detected expedited stalls on CPUs/tasks: {",
63d4c8c9 497 rcu_state.name);
3549c2bc 498 ndetected = 0;
aedf4ba9 499 rcu_for_each_leaf_node(rnp) {
3549c2bc 500 ndetected += rcu_print_task_exp_stall(rnp);
bc75e999 501 for_each_leaf_node_possible_cpu(rnp, cpu) {
3549c2bc
PM
502 struct rcu_data *rdp;
503
bc75e999 504 mask = leaf_node_cpu_bit(rnp, cpu);
3549c2bc
PM
505 if (!(rnp->expmask & mask))
506 continue;
507 ndetected++;
da1df50d 508 rdp = per_cpu_ptr(&rcu_data, cpu);
3549c2bc
PM
509 pr_cont(" %d-%c%c%c", cpu,
510 "O."[!!cpu_online(cpu)],
511 "o."[!!(rdp->grpmask & rnp->expmaskinit)],
512 "N."[!!(rdp->grpmask & rnp->expmaskinitnext)]);
513 }
3549c2bc
PM
514 }
515 pr_cont(" } %lu jiffies s: %lu root: %#lx/%c\n",
63d4c8c9 516 jiffies - jiffies_start, rcu_state.expedited_sequence,
3549c2bc
PM
517 rnp_root->expmask, ".T"[!!rnp_root->exp_tasks]);
518 if (ndetected) {
519 pr_err("blocking rcu_node structures:");
aedf4ba9 520 rcu_for_each_node_breadth_first(rnp) {
3549c2bc
PM
521 if (rnp == rnp_root)
522 continue; /* printed unconditionally */
55ebfce0 523 if (sync_rcu_preempt_exp_done_unlocked(rnp))
3549c2bc
PM
524 continue;
525 pr_cont(" l=%u:%d-%d:%#lx/%c",
526 rnp->level, rnp->grplo, rnp->grphi,
527 rnp->expmask,
528 ".T"[!!rnp->exp_tasks]);
529 }
530 pr_cont("\n");
531 }
aedf4ba9 532 rcu_for_each_leaf_node(rnp) {
bc75e999
MR
533 for_each_leaf_node_possible_cpu(rnp, cpu) {
534 mask = leaf_node_cpu_bit(rnp, cpu);
3549c2bc
PM
535 if (!(rnp->expmask & mask))
536 continue;
537 dump_cpu_task(cpu);
538 }
539 }
540 jiffies_stall = 3 * rcu_jiffies_till_stall_check() + 3;
541 }
542}
543
544/*
545 * Wait for the current expedited grace period to complete, and then
546 * wake up everyone who piggybacked on the just-completed expedited
547 * grace period. Also update all the ->exp_seq_rq counters as needed
548 * in order to avoid counter-wrap problems.
549 */
63d4c8c9 550static void rcu_exp_wait_wake(unsigned long s)
3549c2bc
PM
551{
552 struct rcu_node *rnp;
553
63d4c8c9
PM
554 synchronize_sched_expedited_wait();
555 rcu_exp_gp_seq_end();
556 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("end"));
3549c2bc
PM
557
558 /*
559 * Switch over to wakeup mode, allowing the next GP, but -only- the
560 * next GP, to proceed.
561 */
63d4c8c9 562 mutex_lock(&rcu_state.exp_wake_mutex);
3549c2bc 563
aedf4ba9 564 rcu_for_each_node_breadth_first(rnp) {
3549c2bc
PM
565 if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s)) {
566 spin_lock(&rnp->exp_lock);
567 /* Recheck, avoid hang in case someone just arrived. */
568 if (ULONG_CMP_LT(rnp->exp_seq_rq, s))
569 rnp->exp_seq_rq = s;
570 spin_unlock(&rnp->exp_lock);
571 }
3c345825 572 smp_mb(); /* All above changes before wakeup. */
63d4c8c9 573 wake_up_all(&rnp->exp_wq[rcu_seq_ctr(rcu_state.expedited_sequence) & 0x3]);
3549c2bc 574 }
63d4c8c9
PM
575 trace_rcu_exp_grace_period(rcu_state.name, s, TPS("endwake"));
576 mutex_unlock(&rcu_state.exp_wake_mutex);
3549c2bc
PM
577}
578
52d7e48b
PM
579/*
580 * Common code to drive an expedited grace period forward, used by
581 * workqueues and mid-boot-time tasks.
582 */
63d4c8c9 583static void rcu_exp_sel_wait_wake(smp_call_func_t func, unsigned long s)
52d7e48b
PM
584{
585 /* Initialize the rcu_node tree in preparation for the wait. */
63d4c8c9 586 sync_rcu_exp_select_cpus(func);
52d7e48b
PM
587
588 /* Wait and clean up, including waking everyone. */
63d4c8c9 589 rcu_exp_wait_wake(s);
52d7e48b
PM
590}
591
8b355e3b
PM
592/*
593 * Work-queue handler to drive an expedited grace period forward.
594 */
595static void wait_rcu_exp_gp(struct work_struct *wp)
596{
597 struct rcu_exp_work *rewp;
598
8b355e3b 599 rewp = container_of(wp, struct rcu_exp_work, rew_work);
63d4c8c9 600 rcu_exp_sel_wait_wake(rewp->rew_func, rewp->rew_s);
8b355e3b
PM
601}
602
f7b8eb84 603/*
8fa946d4
PM
604 * Given a smp_call_function() handler, kick off the specified
605 * implementation of expedited grace period.
f7b8eb84 606 */
63d4c8c9 607static void _synchronize_rcu_expedited(smp_call_func_t func)
f7b8eb84 608{
8b355e3b
PM
609 struct rcu_data *rdp;
610 struct rcu_exp_work rew;
611 struct rcu_node *rnp;
f7b8eb84
PM
612 unsigned long s;
613
614 /* If expedited grace periods are prohibited, fall back to normal. */
615 if (rcu_gp_is_normal()) {
ec5dd444 616 wait_rcu_gp(call_rcu);
f7b8eb84
PM
617 return;
618 }
619
620 /* Take a snapshot of the sequence number. */
63d4c8c9
PM
621 s = rcu_exp_gp_seq_snap();
622 if (exp_funnel_lock(s))
f7b8eb84
PM
623 return; /* Someone else did our work for us. */
624
52d7e48b
PM
625 /* Ensure that load happens before action based on it. */
626 if (unlikely(rcu_scheduler_active == RCU_SCHEDULER_INIT)) {
627 /* Direct call during scheduler init and early_initcalls(). */
63d4c8c9 628 rcu_exp_sel_wait_wake(func, s);
52d7e48b
PM
629 } else {
630 /* Marshall arguments & schedule the expedited grace period. */
631 rew.rew_func = func;
52d7e48b
PM
632 rew.rew_s = s;
633 INIT_WORK_ONSTACK(&rew.rew_work, wait_rcu_exp_gp);
ad7c946b 634 queue_work(rcu_gp_wq, &rew.rew_work);
52d7e48b 635 }
8b355e3b
PM
636
637 /* Wait for expedited grace period to complete. */
da1df50d 638 rdp = per_cpu_ptr(&rcu_data, raw_smp_processor_id());
336a4f6c 639 rnp = rcu_get_root();
031aeee0 640 wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],
63d4c8c9 641 sync_exp_work_done(s));
3c345825 642 smp_mb(); /* Workqueue actions happen before return. */
8b355e3b
PM
643
644 /* Let the next expedited grace period start. */
63d4c8c9 645 mutex_unlock(&rcu_state.exp_mutex);
f7b8eb84
PM
646}
647
40e0a6cf
PM
648#ifdef CONFIG_PREEMPT_RCU
649
650/*
651 * Remote handler for smp_call_function_single(). If there is an
652 * RCU read-side critical section in effect, request that the
653 * next rcu_read_unlock() record the quiescent state up the
654 * ->expmask fields in the rcu_node tree. Otherwise, immediately
655 * report the quiescent state.
656 */
63d4c8c9 657static void sync_rcu_exp_handler(void *unused)
40e0a6cf 658{
3e310098 659 unsigned long flags;
da1df50d 660 struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
3e310098 661 struct rcu_node *rnp = rdp->mynode;
40e0a6cf
PM
662 struct task_struct *t = current;
663
664 /*
3e310098
PM
665 * First, the common case of not being in an RCU read-side
666 * critical section. If also enabled or idle, immediately
667 * report the quiescent state, otherwise defer.
40e0a6cf 668 */
3e310098
PM
669 if (!t->rcu_read_lock_nesting) {
670 if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) ||
671 rcu_dynticks_curr_cpu_in_eqs()) {
63d4c8c9 672 rcu_report_exp_rdp(rdp);
3e310098
PM
673 } else {
674 rdp->deferred_qs = true;
fced9c8c
PM
675 set_tsk_need_resched(t);
676 set_preempt_need_resched();
3e310098 677 }
40e0a6cf
PM
678 return;
679 }
680
681 /*
3e310098
PM
682 * Second, the less-common case of being in an RCU read-side
683 * critical section. In this case we can count on a future
684 * rcu_read_unlock(). However, this rcu_read_unlock() might
685 * execute on some other CPU, but in that case there will be
686 * a future context switch. Either way, if the expedited
687 * grace period is still waiting on this CPU, set ->deferred_qs
688 * so that the eventual quiescent state will be reported.
689 * Note that there is a large group of race conditions that
690 * can have caused this quiescent state to already have been
691 * reported, so we really do need to check ->expmask.
40e0a6cf 692 */
3e310098
PM
693 if (t->rcu_read_lock_nesting > 0) {
694 raw_spin_lock_irqsave_rcu_node(rnp, flags);
05f41571 695 if (rnp->expmask & rdp->grpmask) {
3e310098 696 rdp->deferred_qs = true;
05f41571
PM
697 WRITE_ONCE(t->rcu_read_unlock_special.b.exp_hint, true);
698 }
3e310098
PM
699 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
700 }
701
702 /*
703 * The final and least likely case is where the interrupted
704 * code was just about to or just finished exiting the RCU-preempt
705 * read-side critical section, and no, we can't tell which.
706 * So either way, set ->deferred_qs to flag later code that
707 * a quiescent state is required.
708 *
709 * If the CPU is fully enabled (or if some buggy RCU-preempt
710 * read-side critical section is being used from idle), just
711 * invoke rcu_preempt_defer_qs() to immediately report the
712 * quiescent state. We cannot use rcu_read_unlock_special()
713 * because we are in an interrupt handler, which will cause that
714 * function to take an early exit without doing anything.
715 *
fced9c8c 716 * Otherwise, force a context switch after the CPU enables everything.
3e310098
PM
717 */
718 rdp->deferred_qs = true;
719 if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) ||
fced9c8c 720 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs())) {
3e310098 721 rcu_preempt_deferred_qs(t);
fced9c8c
PM
722 } else {
723 set_tsk_need_resched(t);
724 set_preempt_need_resched();
725 }
40e0a6cf
PM
726}
727
8fa946d4 728/* PREEMPT=y, so no PREEMPT=n expedited grace period to clean up after. */
45975c7d
PM
729static void sync_sched_exp_online_cleanup(int cpu)
730{
731}
732
40e0a6cf
PM
733/**
734 * synchronize_rcu_expedited - Brute-force RCU grace period
735 *
736 * Wait for an RCU-preempt grace period, but expedite it. The basic
737 * idea is to IPI all non-idle non-nohz online CPUs. The IPI handler
738 * checks whether the CPU is in an RCU-preempt critical section, and
739 * if so, it sets a flag that causes the outermost rcu_read_unlock()
740 * to report the quiescent state. On the other hand, if the CPU is
741 * not in an RCU read-side critical section, the IPI handler reports
742 * the quiescent state immediately.
743 *
744 * Although this is a greate improvement over previous expedited
745 * implementations, it is still unfriendly to real-time workloads, so is
746 * thus not recommended for any sort of common-case code. In fact, if
747 * you are using synchronize_rcu_expedited() in a loop, please restructure
748 * your code to batch your updates, and then Use a single synchronize_rcu()
749 * instead.
45975c7d
PM
750 *
751 * This has the same semantics as (but is more brutal than) synchronize_rcu().
40e0a6cf
PM
752 */
753void synchronize_rcu_expedited(void)
754{
8dc79888
PM
755 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
756 lock_is_held(&rcu_lock_map) ||
757 lock_is_held(&rcu_sched_lock_map),
758 "Illegal synchronize_rcu_expedited() in RCU read-side critical section");
759
52d7e48b
PM
760 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
761 return;
63d4c8c9 762 _synchronize_rcu_expedited(sync_rcu_exp_handler);
40e0a6cf
PM
763}
764EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
765
766#else /* #ifdef CONFIG_PREEMPT_RCU */
767
45975c7d 768/* Invoked on each online non-idle CPU for expedited quiescent state. */
da1df50d 769static void sync_sched_exp_handler(void *unused)
45975c7d
PM
770{
771 struct rcu_data *rdp;
772 struct rcu_node *rnp;
45975c7d 773
da1df50d 774 rdp = this_cpu_ptr(&rcu_data);
45975c7d
PM
775 rnp = rdp->mynode;
776 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) ||
777 __this_cpu_read(rcu_data.cpu_no_qs.b.exp))
778 return;
779 if (rcu_is_cpu_rrupt_from_idle()) {
63d4c8c9 780 rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
45975c7d
PM
781 return;
782 }
783 __this_cpu_write(rcu_data.cpu_no_qs.b.exp, true);
784 /* Store .exp before .rcu_urgent_qs. */
2dba13f0 785 smp_store_release(this_cpu_ptr(&rcu_data.rcu_urgent_qs), true);
fced9c8c
PM
786 set_tsk_need_resched(current);
787 set_preempt_need_resched();
45975c7d
PM
788}
789
790/* Send IPI for expedited cleanup if needed at end of CPU-hotplug operation. */
791static void sync_sched_exp_online_cleanup(int cpu)
792{
793 struct rcu_data *rdp;
794 int ret;
795 struct rcu_node *rnp;
45975c7d 796
da1df50d 797 rdp = per_cpu_ptr(&rcu_data, cpu);
45975c7d
PM
798 rnp = rdp->mynode;
799 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask))
800 return;
63d4c8c9 801 ret = smp_call_function_single(cpu, sync_sched_exp_handler, NULL, 0);
45975c7d
PM
802 WARN_ON_ONCE(ret);
803}
804
40e0a6cf 805/*
8fa946d4
PM
806 * Because a context switch is a grace period for !PREEMPT, any
807 * blocking grace-period wait automatically implies a grace period if
808 * there is only one CPU online at any point time during execution of
809 * either synchronize_rcu() or synchronize_rcu_expedited(). It is OK to
810 * occasionally incorrectly indicate that there are multiple CPUs online
811 * when there was in fact only one the whole time, as this just adds some
812 * overhead: RCU still operates correctly.
40e0a6cf 813 */
45975c7d
PM
814static int rcu_blocking_is_gp(void)
815{
816 int ret;
817
818 might_sleep(); /* Check for RCU read-side critical section. */
819 preempt_disable();
820 ret = num_online_cpus() <= 1;
821 preempt_enable();
822 return ret;
823}
824
825/* PREEMPT=n implementation of synchronize_rcu_expedited(). */
40e0a6cf
PM
826void synchronize_rcu_expedited(void)
827{
45975c7d
PM
828 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
829 lock_is_held(&rcu_lock_map) ||
830 lock_is_held(&rcu_sched_lock_map),
8fa946d4 831 "Illegal synchronize_rcu_expedited() in RCU read-side critical section");
45975c7d
PM
832
833 /* If only one CPU, this is automatically a grace period. */
834 if (rcu_blocking_is_gp())
835 return;
836
63d4c8c9 837 _synchronize_rcu_expedited(sync_sched_exp_handler);
40e0a6cf
PM
838}
839EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
840
841#endif /* #else #ifdef CONFIG_PREEMPT_RCU */