blob: f9d5bbd8adce8bce6a9451adc58d82835bff3b5c [file] [log] [blame]
Paul E. McKenney3549c2b2016-04-15 16:35:29 -07001/*
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
Boqun Feng55ebfce2018-03-09 09:14:51 +080023#include <linux/lockdep.h>
24
Paul E. McKenney09e2db32016-12-18 13:31:02 -080025/*
26 * Record the start of an expedited grace period.
27 */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -070028static void rcu_exp_gp_seq_start(struct rcu_state *rsp)
29{
30 rcu_seq_start(&rsp->expedited_sequence);
31}
Paul E. McKenney09e2db32016-12-18 13:31:02 -080032
33/*
Paul E. McKenney9a414202018-01-31 19:23:24 -080034 * Return then value that expedited-grace-period counter will have
35 * at the end of the current grace period.
36 */
Paul E. McKenney7f5d42d2018-01-31 19:40:22 -080037static __maybe_unused unsigned long rcu_exp_gp_seq_endval(struct rcu_state *rsp)
Paul E. McKenney9a414202018-01-31 19:23:24 -080038{
39 return rcu_seq_endval(&rsp->expedited_sequence);
40}
41
42/*
Paul E. McKenney09e2db32016-12-18 13:31:02 -080043 * Record the end of an expedited grace period.
44 */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -070045static void rcu_exp_gp_seq_end(struct rcu_state *rsp)
46{
47 rcu_seq_end(&rsp->expedited_sequence);
48 smp_mb(); /* Ensure that consecutive grace periods serialize. */
49}
Paul E. McKenney09e2db32016-12-18 13:31:02 -080050
51/*
52 * Take a snapshot of the expedited-grace-period counter.
53 */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -070054static unsigned long rcu_exp_gp_seq_snap(struct rcu_state *rsp)
55{
56 unsigned long s;
57
58 smp_mb(); /* Caller's modifications seen first by other CPUs. */
59 s = rcu_seq_snap(&rsp->expedited_sequence);
60 trace_rcu_exp_grace_period(rsp->name, s, TPS("snap"));
61 return s;
62}
Paul E. McKenney09e2db32016-12-18 13:31:02 -080063
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 */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -070069static bool rcu_exp_gp_seq_done(struct rcu_state *rsp, unsigned long s)
70{
71 return rcu_seq_done(&rsp->expedited_sequence, s);
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 */
81static void sync_exp_reset_tree_hotplug(struct rcu_state *rsp)
82{
83 bool done;
84 unsigned long flags;
85 unsigned long mask;
86 unsigned long oldmask;
Paul E. McKenney313517fc2017-06-08 16:55:40 -070087 int ncpus = smp_load_acquire(&rsp->ncpus); /* Order against locking. */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -070088 struct rcu_node *rnp;
89 struct rcu_node *rnp_up;
90
91 /* If no new CPUs onlined since last time, nothing to do. */
92 if (likely(ncpus == rsp->ncpus_snap))
93 return;
94 rsp->ncpus_snap = ncpus;
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 */
100 rcu_for_each_leaf_node(rsp, rnp) {
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 */
138static void __maybe_unused sync_exp_reset_tree(struct rcu_state *rsp)
139{
140 unsigned long flags;
141 struct rcu_node *rnp;
142
143 sync_exp_reset_tree_hotplug(rsp);
144 rcu_for_each_node_breadth_first(rsp, rnp) {
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 *
Boqun Feng7be8c562018-03-07 16:49:39 +0800159 * Caller must hold the specificed rcu_node structure's ->lock
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700160 */
Paul E. McKenneydcfc3152017-04-18 09:53:07 -0700161static bool sync_rcu_preempt_exp_done(struct rcu_node *rnp)
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700162{
Boqun Feng55ebfce2018-03-09 09:14:51 +0800163 raw_lockdep_assert_held_rcu_node(rnp);
164
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700165 return rnp->exp_tasks == NULL &&
166 READ_ONCE(rnp->expmask) == 0;
167}
168
169/*
Boqun Feng55ebfce2018-03-09 09:14:51 +0800170 * 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
187/*
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700188 * 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 *
Boqun Feng7be8c562018-03-07 16:49:39 +0800195 * Caller must hold the specified rcu_node structure's ->lock.
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700196 */
197static void __rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp,
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(). */
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200215 swake_up_one(&rsp->expedited_wq);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700216 }
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().
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700231 */
232static void __maybe_unused rcu_report_exp_rnp(struct rcu_state *rsp,
233 struct rcu_node *rnp, bool wake)
234{
235 unsigned long flags;
236
237 raw_spin_lock_irqsave_rcu_node(rnp, flags);
238 __rcu_report_exp_rnp(rsp, rnp, wake, flags);
239}
240
241/*
242 * Report expedited quiescent state for multiple CPUs, all covered by the
Boqun Feng7be8c562018-03-07 16:49:39 +0800243 * specified leaf rcu_node structure.
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700244 */
245static void rcu_report_exp_cpu_mult(struct rcu_state *rsp, struct rcu_node *rnp,
246 unsigned long mask, bool wake)
247{
248 unsigned long flags;
249
250 raw_spin_lock_irqsave_rcu_node(rnp, flags);
251 if (!(rnp->expmask & mask)) {
252 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
253 return;
254 }
255 rnp->expmask &= ~mask;
256 __rcu_report_exp_rnp(rsp, rnp, wake, flags); /* Releases rnp->lock. */
257}
258
259/*
260 * Report expedited quiescent state for specified rcu_data (CPU).
261 */
262static void rcu_report_exp_rdp(struct rcu_state *rsp, struct rcu_data *rdp,
263 bool wake)
264{
Paul E. McKenney3e310092018-06-21 12:50:01 -0700265 WRITE_ONCE(rdp->deferred_qs, false);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700266 rcu_report_exp_cpu_mult(rsp, rdp->mynode, rdp->grpmask, wake);
267}
268
269/* Common code for synchronize_{rcu,sched}_expedited() work-done checking. */
Byungchul Park6fba2b32018-03-02 16:39:12 +0900270static bool sync_exp_work_done(struct rcu_state *rsp, unsigned long s)
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700271{
272 if (rcu_exp_gp_seq_done(rsp, s)) {
273 trace_rcu_exp_grace_period(rsp->name, s, TPS("done"));
274 /* Ensure test happens before caller kfree(). */
275 smp_mb__before_atomic(); /* ^^^ */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700276 return true;
277 }
278 return false;
279}
280
281/*
282 * Funnel-lock acquisition for expedited grace periods. Returns true
283 * if some other task completed an expedited grace period that this task
284 * can piggy-back on, and with no mutex held. Otherwise, returns false
285 * with the mutex held, indicating that the caller must actually do the
286 * expedited grace period.
287 */
288static bool exp_funnel_lock(struct rcu_state *rsp, unsigned long s)
289{
290 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, raw_smp_processor_id());
291 struct rcu_node *rnp = rdp->mynode;
292 struct rcu_node *rnp_root = rcu_get_root(rsp);
293
294 /* Low-contention fastpath. */
295 if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s) &&
296 (rnp == rnp_root ||
297 ULONG_CMP_LT(READ_ONCE(rnp_root->exp_seq_rq), s)) &&
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700298 mutex_trylock(&rsp->exp_mutex))
299 goto fastpath;
300
301 /*
302 * Each pass through the following loop works its way up
303 * the rcu_node tree, returning if others have done the work or
304 * otherwise falls through to acquire rsp->exp_mutex. The mapping
305 * from CPU to rcu_node structure can be inexact, as it is just
306 * promoting locality and is not strictly needed for correctness.
307 */
308 for (; rnp != NULL; rnp = rnp->parent) {
Byungchul Park6fba2b32018-03-02 16:39:12 +0900309 if (sync_exp_work_done(rsp, s))
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700310 return true;
311
312 /* Work not done, either wait here or go up. */
313 spin_lock(&rnp->exp_lock);
314 if (ULONG_CMP_GE(rnp->exp_seq_rq, s)) {
315
316 /* Someone else doing GP, so wait for them. */
317 spin_unlock(&rnp->exp_lock);
318 trace_rcu_exp_funnel_lock(rsp->name, rnp->level,
319 rnp->grplo, rnp->grphi,
320 TPS("wait"));
Paul E. McKenney031aeee2017-03-21 07:28:14 -0700321 wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],
Byungchul Park6fba2b32018-03-02 16:39:12 +0900322 sync_exp_work_done(rsp, s));
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700323 return true;
324 }
325 rnp->exp_seq_rq = s; /* Followers can wait on us. */
326 spin_unlock(&rnp->exp_lock);
327 trace_rcu_exp_funnel_lock(rsp->name, rnp->level, rnp->grplo,
328 rnp->grphi, TPS("nxtlvl"));
329 }
330 mutex_lock(&rsp->exp_mutex);
331fastpath:
Byungchul Park6fba2b32018-03-02 16:39:12 +0900332 if (sync_exp_work_done(rsp, s)) {
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700333 mutex_unlock(&rsp->exp_mutex);
334 return true;
335 }
336 rcu_exp_gp_seq_start(rsp);
337 trace_rcu_exp_grace_period(rsp->name, s, TPS("start"));
338 return false;
339}
340
341/* Invoked on each online non-idle CPU for expedited quiescent state. */
342static void sync_sched_exp_handler(void *data)
343{
344 struct rcu_data *rdp;
345 struct rcu_node *rnp;
346 struct rcu_state *rsp = data;
347
348 rdp = this_cpu_ptr(rsp->rda);
349 rnp = rdp->mynode;
350 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask) ||
351 __this_cpu_read(rcu_sched_data.cpu_no_qs.b.exp))
352 return;
353 if (rcu_is_cpu_rrupt_from_idle()) {
354 rcu_report_exp_rdp(&rcu_sched_state,
355 this_cpu_ptr(&rcu_sched_data), true);
356 return;
357 }
358 __this_cpu_write(rcu_sched_data.cpu_no_qs.b.exp, true);
Paul E. McKenney9226b102017-01-27 14:17:50 -0800359 /* Store .exp before .rcu_urgent_qs. */
360 smp_store_release(this_cpu_ptr(&rcu_dynticks.rcu_urgent_qs), true);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700361 resched_cpu(smp_processor_id());
362}
363
364/* Send IPI for expedited cleanup if needed at end of CPU-hotplug operation. */
365static void sync_sched_exp_online_cleanup(int cpu)
366{
367 struct rcu_data *rdp;
368 int ret;
369 struct rcu_node *rnp;
370 struct rcu_state *rsp = &rcu_sched_state;
371
372 rdp = per_cpu_ptr(rsp->rda, cpu);
373 rnp = rdp->mynode;
374 if (!(READ_ONCE(rnp->expmask) & rdp->grpmask))
375 return;
376 ret = smp_call_function_single(cpu, sync_sched_exp_handler, rsp, 0);
377 WARN_ON_ONCE(ret);
378}
379
380/*
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800381 * Select the CPUs within the specified rcu_node that the upcoming
382 * expedited grace period needs to wait for.
383 */
384static void sync_rcu_exp_select_node_cpus(struct work_struct *wp)
385{
386 int cpu;
387 unsigned long flags;
388 smp_call_func_t func;
389 unsigned long mask_ofl_test;
390 unsigned long mask_ofl_ipi;
391 int ret;
392 struct rcu_exp_work *rewp =
393 container_of(wp, struct rcu_exp_work, rew_work);
394 struct rcu_node *rnp = container_of(rewp, struct rcu_node, rew);
395 struct rcu_state *rsp = rewp->rew_rsp;
396
397 func = rewp->rew_func;
398 raw_spin_lock_irqsave_rcu_node(rnp, flags);
399
400 /* Each pass checks a CPU for identity, offline, and idle. */
401 mask_ofl_test = 0;
402 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {
403 unsigned long mask = leaf_node_cpu_bit(rnp, cpu);
404 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
405 struct rcu_dynticks *rdtp = per_cpu_ptr(&rcu_dynticks, cpu);
406 int snap;
407
408 if (raw_smp_processor_id() == cpu ||
409 !(rnp->qsmaskinitnext & mask)) {
410 mask_ofl_test |= mask;
411 } else {
412 snap = rcu_dynticks_snap(rdtp);
413 if (rcu_dynticks_in_eqs(snap))
414 mask_ofl_test |= mask;
415 else
416 rdp->exp_dynticks_snap = snap;
417 }
418 }
419 mask_ofl_ipi = rnp->expmask & ~mask_ofl_test;
420
421 /*
422 * Need to wait for any blocked tasks as well. Note that
423 * additional blocking tasks will also block the expedited GP
424 * until such time as the ->expmask bits are cleared.
425 */
426 if (rcu_preempt_has_tasks(rnp))
427 rnp->exp_tasks = rnp->blkd_tasks.next;
428 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
429
430 /* IPI the remaining CPUs for expedited quiescent state. */
431 for_each_leaf_node_cpu_mask(rnp, cpu, rnp->expmask) {
432 unsigned long mask = leaf_node_cpu_bit(rnp, cpu);
433 struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
434
435 if (!(mask_ofl_ipi & mask))
436 continue;
437retry_ipi:
438 if (rcu_dynticks_in_eqs_since(rdp->dynticks,
439 rdp->exp_dynticks_snap)) {
440 mask_ofl_test |= mask;
441 continue;
442 }
443 ret = smp_call_function_single(cpu, func, rsp, 0);
444 if (!ret) {
445 mask_ofl_ipi &= ~mask;
446 continue;
447 }
448 /* Failed, raced with CPU hotplug operation. */
449 raw_spin_lock_irqsave_rcu_node(rnp, flags);
450 if ((rnp->qsmaskinitnext & mask) &&
451 (rnp->expmask & mask)) {
452 /* Online, so delay for a bit and try again. */
453 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
454 trace_rcu_exp_grace_period(rsp->name, rcu_exp_gp_seq_endval(rsp), TPS("selectofl"));
455 schedule_timeout_uninterruptible(1);
456 goto retry_ipi;
457 }
458 /* CPU really is offline, so we can ignore it. */
459 if (!(rnp->expmask & mask))
460 mask_ofl_ipi &= ~mask;
461 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
462 }
463 /* Report quiescent states for those that went offline. */
464 mask_ofl_test |= mask_ofl_ipi;
465 if (mask_ofl_test)
466 rcu_report_exp_cpu_mult(rsp, rnp, mask_ofl_test, false);
467}
468
469/*
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700470 * Select the nodes that the upcoming expedited grace period needs
471 * to wait for.
472 */
473static void sync_rcu_exp_select_cpus(struct rcu_state *rsp,
474 smp_call_func_t func)
475{
Boqun Fengfcc63542018-06-15 12:06:31 -0700476 int cpu;
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700477 struct rcu_node *rnp;
478
Paul E. McKenney9a414202018-01-31 19:23:24 -0800479 trace_rcu_exp_grace_period(rsp->name, rcu_exp_gp_seq_endval(rsp), TPS("reset"));
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700480 sync_exp_reset_tree(rsp);
Paul E. McKenney9a414202018-01-31 19:23:24 -0800481 trace_rcu_exp_grace_period(rsp->name, rcu_exp_gp_seq_endval(rsp), TPS("select"));
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800482
483 /* Schedule work for each leaf rcu_node structure. */
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700484 rcu_for_each_leaf_node(rsp, rnp) {
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800485 rnp->exp_need_flush = false;
486 if (!READ_ONCE(rnp->expmask))
487 continue; /* Avoid early boot non-existent wq. */
488 rnp->rew.rew_func = func;
489 rnp->rew.rew_rsp = rsp;
490 if (!READ_ONCE(rcu_par_gp_wq) ||
Paul E. McKenney52575142018-04-24 11:03:39 -0700491 rcu_scheduler_active != RCU_SCHEDULER_RUNNING ||
492 rcu_is_last_leaf_node(rsp, rnp)) {
493 /* No workqueues yet or last leaf, do direct call. */
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800494 sync_rcu_exp_select_node_cpus(&rnp->rew.rew_work);
495 continue;
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700496 }
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800497 INIT_WORK(&rnp->rew.rew_work, sync_rcu_exp_select_node_cpus);
Boqun Fengfcc63542018-06-15 12:06:31 -0700498 preempt_disable();
499 cpu = cpumask_next(rnp->grplo - 1, cpu_online_mask);
500 /* If all offline, queue the work on an unbound CPU. */
501 if (unlikely(cpu > rnp->grphi))
502 cpu = WORK_CPU_UNBOUND;
503 queue_work_on(cpu, rcu_par_gp_wq, &rnp->rew.rew_work);
504 preempt_enable();
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800505 rnp->exp_need_flush = true;
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700506 }
Paul E. McKenney25f3d7e2018-02-01 22:05:38 -0800507
508 /* Wait for workqueue jobs (if any) to complete. */
509 rcu_for_each_leaf_node(rsp, rnp)
510 if (rnp->exp_need_flush)
511 flush_work(&rnp->rew.rew_work);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700512}
513
514static void synchronize_sched_expedited_wait(struct rcu_state *rsp)
515{
516 int cpu;
517 unsigned long jiffies_stall;
518 unsigned long jiffies_start;
519 unsigned long mask;
520 int ndetected;
521 struct rcu_node *rnp;
522 struct rcu_node *rnp_root = rcu_get_root(rsp);
523 int ret;
524
Paul E. McKenney9a414202018-01-31 19:23:24 -0800525 trace_rcu_exp_grace_period(rsp->name, rcu_exp_gp_seq_endval(rsp), TPS("startwait"));
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700526 jiffies_stall = rcu_jiffies_till_stall_check();
527 jiffies_start = jiffies;
528
529 for (;;) {
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200530 ret = swait_event_timeout_exclusive(
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700531 rsp->expedited_wq,
Boqun Feng55ebfce2018-03-09 09:14:51 +0800532 sync_rcu_preempt_exp_done_unlocked(rnp_root),
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700533 jiffies_stall);
Boqun Feng55ebfce2018-03-09 09:14:51 +0800534 if (ret > 0 || sync_rcu_preempt_exp_done_unlocked(rnp_root))
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700535 return;
Paul E. McKenney908d2c12016-06-29 14:34:59 -0700536 WARN_ON(ret < 0); /* workqueues should not be signaled. */
Paul E. McKenney24a6cff2016-06-29 14:49:29 -0700537 if (rcu_cpu_stall_suppress)
538 continue;
539 panic_on_rcu_stall();
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700540 pr_err("INFO: %s detected expedited stalls on CPUs/tasks: {",
541 rsp->name);
542 ndetected = 0;
543 rcu_for_each_leaf_node(rsp, rnp) {
544 ndetected += rcu_print_task_exp_stall(rnp);
Mark Rutlandbc75e992016-06-03 15:20:04 +0100545 for_each_leaf_node_possible_cpu(rnp, cpu) {
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700546 struct rcu_data *rdp;
547
Mark Rutlandbc75e992016-06-03 15:20:04 +0100548 mask = leaf_node_cpu_bit(rnp, cpu);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700549 if (!(rnp->expmask & mask))
550 continue;
551 ndetected++;
552 rdp = per_cpu_ptr(rsp->rda, cpu);
553 pr_cont(" %d-%c%c%c", cpu,
554 "O."[!!cpu_online(cpu)],
555 "o."[!!(rdp->grpmask & rnp->expmaskinit)],
556 "N."[!!(rdp->grpmask & rnp->expmaskinitnext)]);
557 }
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700558 }
559 pr_cont(" } %lu jiffies s: %lu root: %#lx/%c\n",
560 jiffies - jiffies_start, rsp->expedited_sequence,
561 rnp_root->expmask, ".T"[!!rnp_root->exp_tasks]);
562 if (ndetected) {
563 pr_err("blocking rcu_node structures:");
564 rcu_for_each_node_breadth_first(rsp, rnp) {
565 if (rnp == rnp_root)
566 continue; /* printed unconditionally */
Boqun Feng55ebfce2018-03-09 09:14:51 +0800567 if (sync_rcu_preempt_exp_done_unlocked(rnp))
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700568 continue;
569 pr_cont(" l=%u:%d-%d:%#lx/%c",
570 rnp->level, rnp->grplo, rnp->grphi,
571 rnp->expmask,
572 ".T"[!!rnp->exp_tasks]);
573 }
574 pr_cont("\n");
575 }
576 rcu_for_each_leaf_node(rsp, rnp) {
Mark Rutlandbc75e992016-06-03 15:20:04 +0100577 for_each_leaf_node_possible_cpu(rnp, cpu) {
578 mask = leaf_node_cpu_bit(rnp, cpu);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700579 if (!(rnp->expmask & mask))
580 continue;
581 dump_cpu_task(cpu);
582 }
583 }
584 jiffies_stall = 3 * rcu_jiffies_till_stall_check() + 3;
585 }
586}
587
588/*
589 * Wait for the current expedited grace period to complete, and then
590 * wake up everyone who piggybacked on the just-completed expedited
591 * grace period. Also update all the ->exp_seq_rq counters as needed
592 * in order to avoid counter-wrap problems.
593 */
594static void rcu_exp_wait_wake(struct rcu_state *rsp, unsigned long s)
595{
596 struct rcu_node *rnp;
597
598 synchronize_sched_expedited_wait(rsp);
599 rcu_exp_gp_seq_end(rsp);
600 trace_rcu_exp_grace_period(rsp->name, s, TPS("end"));
601
602 /*
603 * Switch over to wakeup mode, allowing the next GP, but -only- the
604 * next GP, to proceed.
605 */
606 mutex_lock(&rsp->exp_wake_mutex);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700607
608 rcu_for_each_node_breadth_first(rsp, rnp) {
609 if (ULONG_CMP_LT(READ_ONCE(rnp->exp_seq_rq), s)) {
610 spin_lock(&rnp->exp_lock);
611 /* Recheck, avoid hang in case someone just arrived. */
612 if (ULONG_CMP_LT(rnp->exp_seq_rq, s))
613 rnp->exp_seq_rq = s;
614 spin_unlock(&rnp->exp_lock);
615 }
Paul E. McKenney3c345825c82017-03-04 12:33:53 -0800616 smp_mb(); /* All above changes before wakeup. */
Paul E. McKenney031aeee2017-03-21 07:28:14 -0700617 wake_up_all(&rnp->exp_wq[rcu_seq_ctr(rsp->expedited_sequence) & 0x3]);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700618 }
619 trace_rcu_exp_grace_period(rsp->name, s, TPS("endwake"));
620 mutex_unlock(&rsp->exp_wake_mutex);
621}
622
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700623/*
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800624 * Common code to drive an expedited grace period forward, used by
625 * workqueues and mid-boot-time tasks.
626 */
627static void rcu_exp_sel_wait_wake(struct rcu_state *rsp,
628 smp_call_func_t func, unsigned long s)
629{
630 /* Initialize the rcu_node tree in preparation for the wait. */
631 sync_rcu_exp_select_cpus(rsp, func);
632
633 /* Wait and clean up, including waking everyone. */
634 rcu_exp_wait_wake(rsp, s);
635}
636
637/*
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700638 * Work-queue handler to drive an expedited grace period forward.
639 */
640static void wait_rcu_exp_gp(struct work_struct *wp)
641{
642 struct rcu_exp_work *rewp;
643
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700644 rewp = container_of(wp, struct rcu_exp_work, rew_work);
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800645 rcu_exp_sel_wait_wake(rewp->rew_rsp, rewp->rew_func, rewp->rew_s);
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700646}
647
Paul E. McKenneyf7b8eb82016-06-24 11:30:32 -0700648/*
649 * Given an rcu_state pointer and a smp_call_function() handler, kick
650 * off the specified flavor of expedited grace period.
651 */
652static void _synchronize_rcu_expedited(struct rcu_state *rsp,
653 smp_call_func_t func)
654{
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700655 struct rcu_data *rdp;
656 struct rcu_exp_work rew;
657 struct rcu_node *rnp;
Paul E. McKenneyf7b8eb82016-06-24 11:30:32 -0700658 unsigned long s;
659
660 /* If expedited grace periods are prohibited, fall back to normal. */
661 if (rcu_gp_is_normal()) {
662 wait_rcu_gp(rsp->call);
663 return;
664 }
665
666 /* Take a snapshot of the sequence number. */
667 s = rcu_exp_gp_seq_snap(rsp);
668 if (exp_funnel_lock(rsp, s))
669 return; /* Someone else did our work for us. */
670
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800671 /* Ensure that load happens before action based on it. */
672 if (unlikely(rcu_scheduler_active == RCU_SCHEDULER_INIT)) {
673 /* Direct call during scheduler init and early_initcalls(). */
674 rcu_exp_sel_wait_wake(rsp, func, s);
675 } else {
676 /* Marshall arguments & schedule the expedited grace period. */
677 rew.rew_func = func;
678 rew.rew_rsp = rsp;
679 rew.rew_s = s;
680 INIT_WORK_ONSTACK(&rew.rew_work, wait_rcu_exp_gp);
Paul E. McKenneyad7c9462018-01-08 14:35:52 -0800681 queue_work(rcu_gp_wq, &rew.rew_work);
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800682 }
Paul E. McKenneyf7b8eb82016-06-24 11:30:32 -0700683
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700684 /* Wait for expedited grace period to complete. */
685 rdp = per_cpu_ptr(rsp->rda, raw_smp_processor_id());
686 rnp = rcu_get_root(rsp);
Paul E. McKenney031aeee2017-03-21 07:28:14 -0700687 wait_event(rnp->exp_wq[rcu_seq_ctr(s) & 0x3],
Byungchul Park6fba2b32018-03-02 16:39:12 +0900688 sync_exp_work_done(rsp, s));
Paul E. McKenney3c345825c82017-03-04 12:33:53 -0800689 smp_mb(); /* Workqueue actions happen before return. */
Paul E. McKenney8b355e32016-06-29 13:46:25 -0700690
691 /* Let the next expedited grace period start. */
692 mutex_unlock(&rsp->exp_mutex);
Paul E. McKenneyf7b8eb82016-06-24 11:30:32 -0700693}
694
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700695/**
696 * synchronize_sched_expedited - Brute-force RCU-sched grace period
697 *
698 * Wait for an RCU-sched grace period to elapse, but use a "big hammer"
699 * approach to force the grace period to end quickly. This consumes
700 * significant time on all CPUs and is unfriendly to real-time workloads,
701 * so is thus not recommended for any sort of common-case code. In fact,
702 * if you are using synchronize_sched_expedited() in a loop, please
703 * restructure your code to batch your updates, and then use a single
704 * synchronize_sched() instead.
705 *
706 * This implementation can be thought of as an application of sequence
707 * locking to expedited grace periods, but using the sequence counter to
708 * determine when someone else has already done the work instead of for
709 * retrying readers.
710 */
711void synchronize_sched_expedited(void)
712{
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700713 struct rcu_state *rsp = &rcu_sched_state;
714
Paul E. McKenney8dc79882017-01-09 15:25:55 -0800715 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
716 lock_is_held(&rcu_lock_map) ||
717 lock_is_held(&rcu_sched_lock_map),
718 "Illegal synchronize_sched_expedited() in RCU read-side critical section");
719
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700720 /* If only one CPU, this is automatically a grace period. */
721 if (rcu_blocking_is_gp())
722 return;
723
Paul E. McKenneyf7b8eb82016-06-24 11:30:32 -0700724 _synchronize_rcu_expedited(rsp, sync_sched_exp_handler);
Paul E. McKenney3549c2b2016-04-15 16:35:29 -0700725}
726EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700727
728#ifdef CONFIG_PREEMPT_RCU
729
730/*
731 * Remote handler for smp_call_function_single(). If there is an
732 * RCU read-side critical section in effect, request that the
733 * next rcu_read_unlock() record the quiescent state up the
734 * ->expmask fields in the rcu_node tree. Otherwise, immediately
735 * report the quiescent state.
736 */
737static void sync_rcu_exp_handler(void *info)
738{
Paul E. McKenney3e310092018-06-21 12:50:01 -0700739 unsigned long flags;
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700740 struct rcu_state *rsp = info;
Paul E. McKenney3e310092018-06-21 12:50:01 -0700741 struct rcu_data *rdp = this_cpu_ptr(rsp->rda);
742 struct rcu_node *rnp = rdp->mynode;
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700743 struct task_struct *t = current;
744
745 /*
Paul E. McKenney3e310092018-06-21 12:50:01 -0700746 * First, the common case of not being in an RCU read-side
747 * critical section. If also enabled or idle, immediately
748 * report the quiescent state, otherwise defer.
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700749 */
Paul E. McKenney3e310092018-06-21 12:50:01 -0700750 if (!t->rcu_read_lock_nesting) {
751 if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) ||
752 rcu_dynticks_curr_cpu_in_eqs()) {
753 rcu_report_exp_rdp(rsp, rdp, true);
754 } else {
755 rdp->deferred_qs = true;
756 resched_cpu(rdp->cpu);
757 }
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700758 return;
759 }
760
761 /*
Paul E. McKenney3e310092018-06-21 12:50:01 -0700762 * Second, the less-common case of being in an RCU read-side
763 * critical section. In this case we can count on a future
764 * rcu_read_unlock(). However, this rcu_read_unlock() might
765 * execute on some other CPU, but in that case there will be
766 * a future context switch. Either way, if the expedited
767 * grace period is still waiting on this CPU, set ->deferred_qs
768 * so that the eventual quiescent state will be reported.
769 * Note that there is a large group of race conditions that
770 * can have caused this quiescent state to already have been
771 * reported, so we really do need to check ->expmask.
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700772 */
Paul E. McKenney3e310092018-06-21 12:50:01 -0700773 if (t->rcu_read_lock_nesting > 0) {
774 raw_spin_lock_irqsave_rcu_node(rnp, flags);
775 if (rnp->expmask & rdp->grpmask)
776 rdp->deferred_qs = true;
777 raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
778 }
779
780 /*
781 * The final and least likely case is where the interrupted
782 * code was just about to or just finished exiting the RCU-preempt
783 * read-side critical section, and no, we can't tell which.
784 * So either way, set ->deferred_qs to flag later code that
785 * a quiescent state is required.
786 *
787 * If the CPU is fully enabled (or if some buggy RCU-preempt
788 * read-side critical section is being used from idle), just
789 * invoke rcu_preempt_defer_qs() to immediately report the
790 * quiescent state. We cannot use rcu_read_unlock_special()
791 * because we are in an interrupt handler, which will cause that
792 * function to take an early exit without doing anything.
793 *
794 * Otherwise, use resched_cpu() to force a context switch after
795 * the CPU enables everything.
796 */
797 rdp->deferred_qs = true;
798 if (!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK)) ||
799 WARN_ON_ONCE(rcu_dynticks_curr_cpu_in_eqs()))
800 rcu_preempt_deferred_qs(t);
801 else
802 resched_cpu(rdp->cpu);
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700803}
804
805/**
806 * synchronize_rcu_expedited - Brute-force RCU grace period
807 *
808 * Wait for an RCU-preempt grace period, but expedite it. The basic
809 * idea is to IPI all non-idle non-nohz online CPUs. The IPI handler
810 * checks whether the CPU is in an RCU-preempt critical section, and
811 * if so, it sets a flag that causes the outermost rcu_read_unlock()
812 * to report the quiescent state. On the other hand, if the CPU is
813 * not in an RCU read-side critical section, the IPI handler reports
814 * the quiescent state immediately.
815 *
816 * Although this is a greate improvement over previous expedited
817 * implementations, it is still unfriendly to real-time workloads, so is
818 * thus not recommended for any sort of common-case code. In fact, if
819 * you are using synchronize_rcu_expedited() in a loop, please restructure
820 * your code to batch your updates, and then Use a single synchronize_rcu()
821 * instead.
822 */
823void synchronize_rcu_expedited(void)
824{
825 struct rcu_state *rsp = rcu_state_p;
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700826
Paul E. McKenney8dc79882017-01-09 15:25:55 -0800827 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map) ||
828 lock_is_held(&rcu_lock_map) ||
829 lock_is_held(&rcu_sched_lock_map),
830 "Illegal synchronize_rcu_expedited() in RCU read-side critical section");
831
Paul E. McKenney52d7e482017-01-10 02:28:26 -0800832 if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
833 return;
Paul E. McKenneyf7b8eb82016-06-24 11:30:32 -0700834 _synchronize_rcu_expedited(rsp, sync_rcu_exp_handler);
Paul E. McKenney40e0a6c2016-04-15 16:44:07 -0700835}
836EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
837
838#else /* #ifdef CONFIG_PREEMPT_RCU */
839
840/*
841 * Wait for an rcu-preempt grace period, but make it happen quickly.
842 * But because preemptible RCU does not exist, map to rcu-sched.
843 */
844void synchronize_rcu_expedited(void)
845{
846 synchronize_sched_expedited();
847}
848EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
849
850#endif /* #else #ifdef CONFIG_PREEMPT_RCU */