rcu: Remove rcu_is_nocb_cpu()
[platform/kernel/linux-starfive.git] / kernel / rcu / tree_nocb.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Read-Copy Update mechanism for mutual exclusion (tree-based version)
4  * Internal non-public definitions that provide either classic
5  * or preemptible semantics.
6  *
7  * Copyright Red Hat, 2009
8  * Copyright IBM Corporation, 2009
9  * Copyright SUSE, 2021
10  *
11  * Author: Ingo Molnar <mingo@elte.hu>
12  *         Paul E. McKenney <paulmck@linux.ibm.com>
13  *         Frederic Weisbecker <frederic@kernel.org>
14  */
15
16 #ifdef CONFIG_RCU_NOCB_CPU
17 static cpumask_var_t rcu_nocb_mask; /* CPUs to have callbacks offloaded. */
18 static bool __read_mostly rcu_nocb_poll;    /* Offload kthread are to poll. */
19 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
20 {
21         return lockdep_is_held(&rdp->nocb_lock);
22 }
23
24 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
25 {
26         /* Race on early boot between thread creation and assignment */
27         if (!rdp->nocb_cb_kthread || !rdp->nocb_gp_kthread)
28                 return true;
29
30         if (current == rdp->nocb_cb_kthread || current == rdp->nocb_gp_kthread)
31                 if (in_task())
32                         return true;
33         return false;
34 }
35
36 /*
37  * Offload callback processing from the boot-time-specified set of CPUs
38  * specified by rcu_nocb_mask.  For the CPUs in the set, there are kthreads
39  * created that pull the callbacks from the corresponding CPU, wait for
40  * a grace period to elapse, and invoke the callbacks.  These kthreads
41  * are organized into GP kthreads, which manage incoming callbacks, wait for
42  * grace periods, and awaken CB kthreads, and the CB kthreads, which only
43  * invoke callbacks.  Each GP kthread invokes its own CBs.  The no-CBs CPUs
44  * do a wake_up() on their GP kthread when they insert a callback into any
45  * empty list, unless the rcu_nocb_poll boot parameter has been specified,
46  * in which case each kthread actively polls its CPU.  (Which isn't so great
47  * for energy efficiency, but which does reduce RCU's overhead on that CPU.)
48  *
49  * This is intended to be used in conjunction with Frederic Weisbecker's
50  * adaptive-idle work, which would seriously reduce OS jitter on CPUs
51  * running CPU-bound user-mode computations.
52  *
53  * Offloading of callbacks can also be used as an energy-efficiency
54  * measure because CPUs with no RCU callbacks queued are more aggressive
55  * about entering dyntick-idle mode.
56  */
57
58
59 /*
60  * Parse the boot-time rcu_nocb_mask CPU list from the kernel parameters.
61  * If the list is invalid, a warning is emitted and all CPUs are offloaded.
62  */
63
64 static bool rcu_nocb_is_setup;
65
66 static int __init rcu_nocb_setup(char *str)
67 {
68         alloc_bootmem_cpumask_var(&rcu_nocb_mask);
69         if (*str == '=') {
70                 if (cpulist_parse(++str, rcu_nocb_mask)) {
71                         pr_warn("rcu_nocbs= bad CPU range, all CPUs set\n");
72                         cpumask_setall(rcu_nocb_mask);
73                 }
74         }
75         rcu_nocb_is_setup = true;
76         return 1;
77 }
78 __setup("rcu_nocbs", rcu_nocb_setup);
79
80 static int __init parse_rcu_nocb_poll(char *arg)
81 {
82         rcu_nocb_poll = true;
83         return 0;
84 }
85 early_param("rcu_nocb_poll", parse_rcu_nocb_poll);
86
87 /*
88  * Don't bother bypassing ->cblist if the call_rcu() rate is low.
89  * After all, the main point of bypassing is to avoid lock contention
90  * on ->nocb_lock, which only can happen at high call_rcu() rates.
91  */
92 static int nocb_nobypass_lim_per_jiffy = 16 * 1000 / HZ;
93 module_param(nocb_nobypass_lim_per_jiffy, int, 0);
94
95 /*
96  * Acquire the specified rcu_data structure's ->nocb_bypass_lock.  If the
97  * lock isn't immediately available, increment ->nocb_lock_contended to
98  * flag the contention.
99  */
100 static void rcu_nocb_bypass_lock(struct rcu_data *rdp)
101         __acquires(&rdp->nocb_bypass_lock)
102 {
103         lockdep_assert_irqs_disabled();
104         if (raw_spin_trylock(&rdp->nocb_bypass_lock))
105                 return;
106         atomic_inc(&rdp->nocb_lock_contended);
107         WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
108         smp_mb__after_atomic(); /* atomic_inc() before lock. */
109         raw_spin_lock(&rdp->nocb_bypass_lock);
110         smp_mb__before_atomic(); /* atomic_dec() after lock. */
111         atomic_dec(&rdp->nocb_lock_contended);
112 }
113
114 /*
115  * Spinwait until the specified rcu_data structure's ->nocb_lock is
116  * not contended.  Please note that this is extremely special-purpose,
117  * relying on the fact that at most two kthreads and one CPU contend for
118  * this lock, and also that the two kthreads are guaranteed to have frequent
119  * grace-period-duration time intervals between successive acquisitions
120  * of the lock.  This allows us to use an extremely simple throttling
121  * mechanism, and further to apply it only to the CPU doing floods of
122  * call_rcu() invocations.  Don't try this at home!
123  */
124 static void rcu_nocb_wait_contended(struct rcu_data *rdp)
125 {
126         WARN_ON_ONCE(smp_processor_id() != rdp->cpu);
127         while (WARN_ON_ONCE(atomic_read(&rdp->nocb_lock_contended)))
128                 cpu_relax();
129 }
130
131 /*
132  * Conditionally acquire the specified rcu_data structure's
133  * ->nocb_bypass_lock.
134  */
135 static bool rcu_nocb_bypass_trylock(struct rcu_data *rdp)
136 {
137         lockdep_assert_irqs_disabled();
138         return raw_spin_trylock(&rdp->nocb_bypass_lock);
139 }
140
141 /*
142  * Release the specified rcu_data structure's ->nocb_bypass_lock.
143  */
144 static void rcu_nocb_bypass_unlock(struct rcu_data *rdp)
145         __releases(&rdp->nocb_bypass_lock)
146 {
147         lockdep_assert_irqs_disabled();
148         raw_spin_unlock(&rdp->nocb_bypass_lock);
149 }
150
151 /*
152  * Acquire the specified rcu_data structure's ->nocb_lock, but only
153  * if it corresponds to a no-CBs CPU.
154  */
155 static void rcu_nocb_lock(struct rcu_data *rdp)
156 {
157         lockdep_assert_irqs_disabled();
158         if (!rcu_rdp_is_offloaded(rdp))
159                 return;
160         raw_spin_lock(&rdp->nocb_lock);
161 }
162
163 /*
164  * Release the specified rcu_data structure's ->nocb_lock, but only
165  * if it corresponds to a no-CBs CPU.
166  */
167 static void rcu_nocb_unlock(struct rcu_data *rdp)
168 {
169         if (rcu_rdp_is_offloaded(rdp)) {
170                 lockdep_assert_irqs_disabled();
171                 raw_spin_unlock(&rdp->nocb_lock);
172         }
173 }
174
175 /*
176  * Release the specified rcu_data structure's ->nocb_lock and restore
177  * interrupts, but only if it corresponds to a no-CBs CPU.
178  */
179 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
180                                        unsigned long flags)
181 {
182         if (rcu_rdp_is_offloaded(rdp)) {
183                 lockdep_assert_irqs_disabled();
184                 raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
185         } else {
186                 local_irq_restore(flags);
187         }
188 }
189
190 /* Lockdep check that ->cblist may be safely accessed. */
191 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
192 {
193         lockdep_assert_irqs_disabled();
194         if (rcu_rdp_is_offloaded(rdp))
195                 lockdep_assert_held(&rdp->nocb_lock);
196 }
197
198 /*
199  * Wake up any no-CBs CPUs' kthreads that were waiting on the just-ended
200  * grace period.
201  */
202 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
203 {
204         swake_up_all(sq);
205 }
206
207 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
208 {
209         return &rnp->nocb_gp_wq[rcu_seq_ctr(rnp->gp_seq) & 0x1];
210 }
211
212 static void rcu_init_one_nocb(struct rcu_node *rnp)
213 {
214         init_swait_queue_head(&rnp->nocb_gp_wq[0]);
215         init_swait_queue_head(&rnp->nocb_gp_wq[1]);
216 }
217
218 static bool __wake_nocb_gp(struct rcu_data *rdp_gp,
219                            struct rcu_data *rdp,
220                            bool force, unsigned long flags)
221         __releases(rdp_gp->nocb_gp_lock)
222 {
223         bool needwake = false;
224
225         if (!READ_ONCE(rdp_gp->nocb_gp_kthread)) {
226                 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
227                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
228                                     TPS("AlreadyAwake"));
229                 return false;
230         }
231
232         if (rdp_gp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
233                 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
234                 del_timer(&rdp_gp->nocb_timer);
235         }
236
237         if (force || READ_ONCE(rdp_gp->nocb_gp_sleep)) {
238                 WRITE_ONCE(rdp_gp->nocb_gp_sleep, false);
239                 needwake = true;
240         }
241         raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
242         if (needwake) {
243                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DoWake"));
244                 wake_up_process(rdp_gp->nocb_gp_kthread);
245         }
246
247         return needwake;
248 }
249
250 /*
251  * Kick the GP kthread for this NOCB group.
252  */
253 static bool wake_nocb_gp(struct rcu_data *rdp, bool force)
254 {
255         unsigned long flags;
256         struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
257
258         raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
259         return __wake_nocb_gp(rdp_gp, rdp, force, flags);
260 }
261
262 /*
263  * Arrange to wake the GP kthread for this NOCB group at some future
264  * time when it is safe to do so.
265  */
266 static void wake_nocb_gp_defer(struct rcu_data *rdp, int waketype,
267                                const char *reason)
268 {
269         unsigned long flags;
270         struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
271
272         raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
273
274         /*
275          * Bypass wakeup overrides previous deferments. In case
276          * of callback storm, no need to wake up too early.
277          */
278         if (waketype == RCU_NOCB_WAKE_BYPASS) {
279                 mod_timer(&rdp_gp->nocb_timer, jiffies + 2);
280                 WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
281         } else {
282                 if (rdp_gp->nocb_defer_wakeup < RCU_NOCB_WAKE)
283                         mod_timer(&rdp_gp->nocb_timer, jiffies + 1);
284                 if (rdp_gp->nocb_defer_wakeup < waketype)
285                         WRITE_ONCE(rdp_gp->nocb_defer_wakeup, waketype);
286         }
287
288         raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
289
290         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, reason);
291 }
292
293 /*
294  * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
295  * However, if there is a callback to be enqueued and if ->nocb_bypass
296  * proves to be initially empty, just return false because the no-CB GP
297  * kthread may need to be awakened in this case.
298  *
299  * Note that this function always returns true if rhp is NULL.
300  */
301 static bool rcu_nocb_do_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
302                                      unsigned long j)
303 {
304         struct rcu_cblist rcl;
305
306         WARN_ON_ONCE(!rcu_rdp_is_offloaded(rdp));
307         rcu_lockdep_assert_cblist_protected(rdp);
308         lockdep_assert_held(&rdp->nocb_bypass_lock);
309         if (rhp && !rcu_cblist_n_cbs(&rdp->nocb_bypass)) {
310                 raw_spin_unlock(&rdp->nocb_bypass_lock);
311                 return false;
312         }
313         /* Note: ->cblist.len already accounts for ->nocb_bypass contents. */
314         if (rhp)
315                 rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
316         rcu_cblist_flush_enqueue(&rcl, &rdp->nocb_bypass, rhp);
317         rcu_segcblist_insert_pend_cbs(&rdp->cblist, &rcl);
318         WRITE_ONCE(rdp->nocb_bypass_first, j);
319         rcu_nocb_bypass_unlock(rdp);
320         return true;
321 }
322
323 /*
324  * Flush the ->nocb_bypass queue into ->cblist, enqueuing rhp if non-NULL.
325  * However, if there is a callback to be enqueued and if ->nocb_bypass
326  * proves to be initially empty, just return false because the no-CB GP
327  * kthread may need to be awakened in this case.
328  *
329  * Note that this function always returns true if rhp is NULL.
330  */
331 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
332                                   unsigned long j)
333 {
334         if (!rcu_rdp_is_offloaded(rdp))
335                 return true;
336         rcu_lockdep_assert_cblist_protected(rdp);
337         rcu_nocb_bypass_lock(rdp);
338         return rcu_nocb_do_flush_bypass(rdp, rhp, j);
339 }
340
341 /*
342  * If the ->nocb_bypass_lock is immediately available, flush the
343  * ->nocb_bypass queue into ->cblist.
344  */
345 static void rcu_nocb_try_flush_bypass(struct rcu_data *rdp, unsigned long j)
346 {
347         rcu_lockdep_assert_cblist_protected(rdp);
348         if (!rcu_rdp_is_offloaded(rdp) ||
349             !rcu_nocb_bypass_trylock(rdp))
350                 return;
351         WARN_ON_ONCE(!rcu_nocb_do_flush_bypass(rdp, NULL, j));
352 }
353
354 /*
355  * See whether it is appropriate to use the ->nocb_bypass list in order
356  * to control contention on ->nocb_lock.  A limited number of direct
357  * enqueues are permitted into ->cblist per jiffy.  If ->nocb_bypass
358  * is non-empty, further callbacks must be placed into ->nocb_bypass,
359  * otherwise rcu_barrier() breaks.  Use rcu_nocb_flush_bypass() to switch
360  * back to direct use of ->cblist.  However, ->nocb_bypass should not be
361  * used if ->cblist is empty, because otherwise callbacks can be stranded
362  * on ->nocb_bypass because we cannot count on the current CPU ever again
363  * invoking call_rcu().  The general rule is that if ->nocb_bypass is
364  * non-empty, the corresponding no-CBs grace-period kthread must not be
365  * in an indefinite sleep state.
366  *
367  * Finally, it is not permitted to use the bypass during early boot,
368  * as doing so would confuse the auto-initialization code.  Besides
369  * which, there is no point in worrying about lock contention while
370  * there is only one CPU in operation.
371  */
372 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
373                                 bool *was_alldone, unsigned long flags)
374 {
375         unsigned long c;
376         unsigned long cur_gp_seq;
377         unsigned long j = jiffies;
378         long ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
379
380         lockdep_assert_irqs_disabled();
381
382         // Pure softirq/rcuc based processing: no bypassing, no
383         // locking.
384         if (!rcu_rdp_is_offloaded(rdp)) {
385                 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
386                 return false;
387         }
388
389         // In the process of (de-)offloading: no bypassing, but
390         // locking.
391         if (!rcu_segcblist_completely_offloaded(&rdp->cblist)) {
392                 rcu_nocb_lock(rdp);
393                 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
394                 return false; /* Not offloaded, no bypassing. */
395         }
396
397         // Don't use ->nocb_bypass during early boot.
398         if (rcu_scheduler_active != RCU_SCHEDULER_RUNNING) {
399                 rcu_nocb_lock(rdp);
400                 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
401                 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
402                 return false;
403         }
404
405         // If we have advanced to a new jiffy, reset counts to allow
406         // moving back from ->nocb_bypass to ->cblist.
407         if (j == rdp->nocb_nobypass_last) {
408                 c = rdp->nocb_nobypass_count + 1;
409         } else {
410                 WRITE_ONCE(rdp->nocb_nobypass_last, j);
411                 c = rdp->nocb_nobypass_count - nocb_nobypass_lim_per_jiffy;
412                 if (ULONG_CMP_LT(rdp->nocb_nobypass_count,
413                                  nocb_nobypass_lim_per_jiffy))
414                         c = 0;
415                 else if (c > nocb_nobypass_lim_per_jiffy)
416                         c = nocb_nobypass_lim_per_jiffy;
417         }
418         WRITE_ONCE(rdp->nocb_nobypass_count, c);
419
420         // If there hasn't yet been all that many ->cblist enqueues
421         // this jiffy, tell the caller to enqueue onto ->cblist.  But flush
422         // ->nocb_bypass first.
423         if (rdp->nocb_nobypass_count < nocb_nobypass_lim_per_jiffy) {
424                 rcu_nocb_lock(rdp);
425                 *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
426                 if (*was_alldone)
427                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
428                                             TPS("FirstQ"));
429                 WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, j));
430                 WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
431                 return false; // Caller must enqueue the callback.
432         }
433
434         // If ->nocb_bypass has been used too long or is too full,
435         // flush ->nocb_bypass to ->cblist.
436         if ((ncbs && j != READ_ONCE(rdp->nocb_bypass_first)) ||
437             ncbs >= qhimark) {
438                 rcu_nocb_lock(rdp);
439                 if (!rcu_nocb_flush_bypass(rdp, rhp, j)) {
440                         *was_alldone = !rcu_segcblist_pend_cbs(&rdp->cblist);
441                         if (*was_alldone)
442                                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
443                                                     TPS("FirstQ"));
444                         WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
445                         return false; // Caller must enqueue the callback.
446                 }
447                 if (j != rdp->nocb_gp_adv_time &&
448                     rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
449                     rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
450                         rcu_advance_cbs_nowake(rdp->mynode, rdp);
451                         rdp->nocb_gp_adv_time = j;
452                 }
453                 rcu_nocb_unlock_irqrestore(rdp, flags);
454                 return true; // Callback already enqueued.
455         }
456
457         // We need to use the bypass.
458         rcu_nocb_wait_contended(rdp);
459         rcu_nocb_bypass_lock(rdp);
460         ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
461         rcu_segcblist_inc_len(&rdp->cblist); /* Must precede enqueue. */
462         rcu_cblist_enqueue(&rdp->nocb_bypass, rhp);
463         if (!ncbs) {
464                 WRITE_ONCE(rdp->nocb_bypass_first, j);
465                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("FirstBQ"));
466         }
467         rcu_nocb_bypass_unlock(rdp);
468         smp_mb(); /* Order enqueue before wake. */
469         if (ncbs) {
470                 local_irq_restore(flags);
471         } else {
472                 // No-CBs GP kthread might be indefinitely asleep, if so, wake.
473                 rcu_nocb_lock(rdp); // Rare during call_rcu() flood.
474                 if (!rcu_segcblist_pend_cbs(&rdp->cblist)) {
475                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
476                                             TPS("FirstBQwake"));
477                         __call_rcu_nocb_wake(rdp, true, flags);
478                 } else {
479                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
480                                             TPS("FirstBQnoWake"));
481                         rcu_nocb_unlock_irqrestore(rdp, flags);
482                 }
483         }
484         return true; // Callback already enqueued.
485 }
486
487 /*
488  * Awaken the no-CBs grace-period kthread if needed, either due to it
489  * legitimately being asleep or due to overload conditions.
490  *
491  * If warranted, also wake up the kthread servicing this CPUs queues.
492  */
493 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_alldone,
494                                  unsigned long flags)
495                                  __releases(rdp->nocb_lock)
496 {
497         unsigned long cur_gp_seq;
498         unsigned long j;
499         long len;
500         struct task_struct *t;
501
502         // If we are being polled or there is no kthread, just leave.
503         t = READ_ONCE(rdp->nocb_gp_kthread);
504         if (rcu_nocb_poll || !t) {
505                 rcu_nocb_unlock_irqrestore(rdp, flags);
506                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
507                                     TPS("WakeNotPoll"));
508                 return;
509         }
510         // Need to actually to a wakeup.
511         len = rcu_segcblist_n_cbs(&rdp->cblist);
512         if (was_alldone) {
513                 rdp->qlen_last_fqs_check = len;
514                 if (!irqs_disabled_flags(flags)) {
515                         /* ... if queue was empty ... */
516                         rcu_nocb_unlock_irqrestore(rdp, flags);
517                         wake_nocb_gp(rdp, false);
518                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
519                                             TPS("WakeEmpty"));
520                 } else {
521                         rcu_nocb_unlock_irqrestore(rdp, flags);
522                         wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE,
523                                            TPS("WakeEmptyIsDeferred"));
524                 }
525         } else if (len > rdp->qlen_last_fqs_check + qhimark) {
526                 /* ... or if many callbacks queued. */
527                 rdp->qlen_last_fqs_check = len;
528                 j = jiffies;
529                 if (j != rdp->nocb_gp_adv_time &&
530                     rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
531                     rcu_seq_done(&rdp->mynode->gp_seq, cur_gp_seq)) {
532                         rcu_advance_cbs_nowake(rdp->mynode, rdp);
533                         rdp->nocb_gp_adv_time = j;
534                 }
535                 smp_mb(); /* Enqueue before timer_pending(). */
536                 if ((rdp->nocb_cb_sleep ||
537                      !rcu_segcblist_ready_cbs(&rdp->cblist)) &&
538                     !timer_pending(&rdp->nocb_timer)) {
539                         rcu_nocb_unlock_irqrestore(rdp, flags);
540                         wake_nocb_gp_defer(rdp, RCU_NOCB_WAKE_FORCE,
541                                            TPS("WakeOvfIsDeferred"));
542                 } else {
543                         rcu_nocb_unlock_irqrestore(rdp, flags);
544                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
545                 }
546         } else {
547                 rcu_nocb_unlock_irqrestore(rdp, flags);
548                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WakeNot"));
549         }
550 }
551
552 /*
553  * Check if we ignore this rdp.
554  *
555  * We check that without holding the nocb lock but
556  * we make sure not to miss a freshly offloaded rdp
557  * with the current ordering:
558  *
559  *  rdp_offload_toggle()        nocb_gp_enabled_cb()
560  * -------------------------   ----------------------------
561  *    WRITE flags                 LOCK nocb_gp_lock
562  *    LOCK nocb_gp_lock           READ/WRITE nocb_gp_sleep
563  *    READ/WRITE nocb_gp_sleep    UNLOCK nocb_gp_lock
564  *    UNLOCK nocb_gp_lock         READ flags
565  */
566 static inline bool nocb_gp_enabled_cb(struct rcu_data *rdp)
567 {
568         u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_GP;
569
570         return rcu_segcblist_test_flags(&rdp->cblist, flags);
571 }
572
573 static inline bool nocb_gp_update_state_deoffloading(struct rcu_data *rdp,
574                                                      bool *needwake_state)
575 {
576         struct rcu_segcblist *cblist = &rdp->cblist;
577
578         if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
579                 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP)) {
580                         rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_GP);
581                         if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
582                                 *needwake_state = true;
583                 }
584                 return false;
585         }
586
587         /*
588          * De-offloading. Clear our flag and notify the de-offload worker.
589          * We will ignore this rdp until it ever gets re-offloaded.
590          */
591         WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
592         rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_GP);
593         if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB))
594                 *needwake_state = true;
595         return true;
596 }
597
598
599 /*
600  * No-CBs GP kthreads come here to wait for additional callbacks to show up
601  * or for grace periods to end.
602  */
603 static void nocb_gp_wait(struct rcu_data *my_rdp)
604 {
605         bool bypass = false;
606         long bypass_ncbs;
607         int __maybe_unused cpu = my_rdp->cpu;
608         unsigned long cur_gp_seq;
609         unsigned long flags;
610         bool gotcbs = false;
611         unsigned long j = jiffies;
612         bool needwait_gp = false; // This prevents actual uninitialized use.
613         bool needwake;
614         bool needwake_gp;
615         struct rcu_data *rdp;
616         struct rcu_node *rnp;
617         unsigned long wait_gp_seq = 0; // Suppress "use uninitialized" warning.
618         bool wasempty = false;
619
620         /*
621          * Each pass through the following loop checks for CBs and for the
622          * nearest grace period (if any) to wait for next.  The CB kthreads
623          * and the global grace-period kthread are awakened if needed.
624          */
625         WARN_ON_ONCE(my_rdp->nocb_gp_rdp != my_rdp);
626         /*
627          * An rcu_data structure is removed from the list after its
628          * CPU is de-offloaded and added to the list before that CPU is
629          * (re-)offloaded.  If the following loop happens to be referencing
630          * that rcu_data structure during the time that the corresponding
631          * CPU is de-offloaded and then immediately re-offloaded, this
632          * loop's rdp pointer will be carried to the end of the list by
633          * the resulting pair of list operations.  This can cause the loop
634          * to skip over some of the rcu_data structures that were supposed
635          * to have been scanned.  Fortunately a new iteration through the
636          * entire loop is forced after a given CPU's rcu_data structure
637          * is added to the list, so the skipped-over rcu_data structures
638          * won't be ignored for long.
639          */
640         list_for_each_entry_rcu(rdp, &my_rdp->nocb_head_rdp, nocb_entry_rdp, 1) {
641                 bool needwake_state = false;
642
643                 if (!nocb_gp_enabled_cb(rdp))
644                         continue;
645                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Check"));
646                 rcu_nocb_lock_irqsave(rdp, flags);
647                 if (nocb_gp_update_state_deoffloading(rdp, &needwake_state)) {
648                         rcu_nocb_unlock_irqrestore(rdp, flags);
649                         if (needwake_state)
650                                 swake_up_one(&rdp->nocb_state_wq);
651                         continue;
652                 }
653                 bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
654                 if (bypass_ncbs &&
655                     (time_after(j, READ_ONCE(rdp->nocb_bypass_first) + 1) ||
656                      bypass_ncbs > 2 * qhimark)) {
657                         // Bypass full or old, so flush it.
658                         (void)rcu_nocb_try_flush_bypass(rdp, j);
659                         bypass_ncbs = rcu_cblist_n_cbs(&rdp->nocb_bypass);
660                 } else if (!bypass_ncbs && rcu_segcblist_empty(&rdp->cblist)) {
661                         rcu_nocb_unlock_irqrestore(rdp, flags);
662                         if (needwake_state)
663                                 swake_up_one(&rdp->nocb_state_wq);
664                         continue; /* No callbacks here, try next. */
665                 }
666                 if (bypass_ncbs) {
667                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
668                                             TPS("Bypass"));
669                         bypass = true;
670                 }
671                 rnp = rdp->mynode;
672
673                 // Advance callbacks if helpful and low contention.
674                 needwake_gp = false;
675                 if (!rcu_segcblist_restempty(&rdp->cblist,
676                                              RCU_NEXT_READY_TAIL) ||
677                     (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq) &&
678                      rcu_seq_done(&rnp->gp_seq, cur_gp_seq))) {
679                         raw_spin_lock_rcu_node(rnp); /* irqs disabled. */
680                         needwake_gp = rcu_advance_cbs(rnp, rdp);
681                         wasempty = rcu_segcblist_restempty(&rdp->cblist,
682                                                            RCU_NEXT_READY_TAIL);
683                         raw_spin_unlock_rcu_node(rnp); /* irqs disabled. */
684                 }
685                 // Need to wait on some grace period?
686                 WARN_ON_ONCE(wasempty &&
687                              !rcu_segcblist_restempty(&rdp->cblist,
688                                                       RCU_NEXT_READY_TAIL));
689                 if (rcu_segcblist_nextgp(&rdp->cblist, &cur_gp_seq)) {
690                         if (!needwait_gp ||
691                             ULONG_CMP_LT(cur_gp_seq, wait_gp_seq))
692                                 wait_gp_seq = cur_gp_seq;
693                         needwait_gp = true;
694                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu,
695                                             TPS("NeedWaitGP"));
696                 }
697                 if (rcu_segcblist_ready_cbs(&rdp->cblist)) {
698                         needwake = rdp->nocb_cb_sleep;
699                         WRITE_ONCE(rdp->nocb_cb_sleep, false);
700                         smp_mb(); /* CB invocation -after- GP end. */
701                 } else {
702                         needwake = false;
703                 }
704                 rcu_nocb_unlock_irqrestore(rdp, flags);
705                 if (needwake) {
706                         swake_up_one(&rdp->nocb_cb_wq);
707                         gotcbs = true;
708                 }
709                 if (needwake_gp)
710                         rcu_gp_kthread_wake();
711                 if (needwake_state)
712                         swake_up_one(&rdp->nocb_state_wq);
713         }
714
715         my_rdp->nocb_gp_bypass = bypass;
716         my_rdp->nocb_gp_gp = needwait_gp;
717         my_rdp->nocb_gp_seq = needwait_gp ? wait_gp_seq : 0;
718
719         if (bypass && !rcu_nocb_poll) {
720                 // At least one child with non-empty ->nocb_bypass, so set
721                 // timer in order to avoid stranding its callbacks.
722                 wake_nocb_gp_defer(my_rdp, RCU_NOCB_WAKE_BYPASS,
723                                    TPS("WakeBypassIsDeferred"));
724         }
725         if (rcu_nocb_poll) {
726                 /* Polling, so trace if first poll in the series. */
727                 if (gotcbs)
728                         trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Poll"));
729                 schedule_timeout_idle(1);
730         } else if (!needwait_gp) {
731                 /* Wait for callbacks to appear. */
732                 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("Sleep"));
733                 swait_event_interruptible_exclusive(my_rdp->nocb_gp_wq,
734                                 !READ_ONCE(my_rdp->nocb_gp_sleep));
735                 trace_rcu_nocb_wake(rcu_state.name, cpu, TPS("EndSleep"));
736         } else {
737                 rnp = my_rdp->mynode;
738                 trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("StartWait"));
739                 swait_event_interruptible_exclusive(
740                         rnp->nocb_gp_wq[rcu_seq_ctr(wait_gp_seq) & 0x1],
741                         rcu_seq_done(&rnp->gp_seq, wait_gp_seq) ||
742                         !READ_ONCE(my_rdp->nocb_gp_sleep));
743                 trace_rcu_this_gp(rnp, my_rdp, wait_gp_seq, TPS("EndWait"));
744         }
745         if (!rcu_nocb_poll) {
746                 raw_spin_lock_irqsave(&my_rdp->nocb_gp_lock, flags);
747                 if (my_rdp->nocb_defer_wakeup > RCU_NOCB_WAKE_NOT) {
748                         WRITE_ONCE(my_rdp->nocb_defer_wakeup, RCU_NOCB_WAKE_NOT);
749                         del_timer(&my_rdp->nocb_timer);
750                 }
751                 WRITE_ONCE(my_rdp->nocb_gp_sleep, true);
752                 raw_spin_unlock_irqrestore(&my_rdp->nocb_gp_lock, flags);
753         }
754         my_rdp->nocb_gp_seq = -1;
755         WARN_ON(signal_pending(current));
756 }
757
758 /*
759  * No-CBs grace-period-wait kthread.  There is one of these per group
760  * of CPUs, but only once at least one CPU in that group has come online
761  * at least once since boot.  This kthread checks for newly posted
762  * callbacks from any of the CPUs it is responsible for, waits for a
763  * grace period, then awakens all of the rcu_nocb_cb_kthread() instances
764  * that then have callback-invocation work to do.
765  */
766 static int rcu_nocb_gp_kthread(void *arg)
767 {
768         struct rcu_data *rdp = arg;
769
770         for (;;) {
771                 WRITE_ONCE(rdp->nocb_gp_loops, rdp->nocb_gp_loops + 1);
772                 nocb_gp_wait(rdp);
773                 cond_resched_tasks_rcu_qs();
774         }
775         return 0;
776 }
777
778 static inline bool nocb_cb_can_run(struct rcu_data *rdp)
779 {
780         u8 flags = SEGCBLIST_OFFLOADED | SEGCBLIST_KTHREAD_CB;
781
782         return rcu_segcblist_test_flags(&rdp->cblist, flags);
783 }
784
785 static inline bool nocb_cb_wait_cond(struct rcu_data *rdp)
786 {
787         return nocb_cb_can_run(rdp) && !READ_ONCE(rdp->nocb_cb_sleep);
788 }
789
790 /*
791  * Invoke any ready callbacks from the corresponding no-CBs CPU,
792  * then, if there are no more, wait for more to appear.
793  */
794 static void nocb_cb_wait(struct rcu_data *rdp)
795 {
796         struct rcu_segcblist *cblist = &rdp->cblist;
797         unsigned long cur_gp_seq;
798         unsigned long flags;
799         bool needwake_state = false;
800         bool needwake_gp = false;
801         bool can_sleep = true;
802         struct rcu_node *rnp = rdp->mynode;
803
804         do {
805                 swait_event_interruptible_exclusive(rdp->nocb_cb_wq,
806                                                     nocb_cb_wait_cond(rdp));
807
808                 // VVV Ensure CB invocation follows _sleep test.
809                 if (smp_load_acquire(&rdp->nocb_cb_sleep)) { // ^^^
810                         WARN_ON(signal_pending(current));
811                         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("WokeEmpty"));
812                 }
813         } while (!nocb_cb_can_run(rdp));
814
815
816         local_irq_save(flags);
817         rcu_momentary_dyntick_idle();
818         local_irq_restore(flags);
819         /*
820          * Disable BH to provide the expected environment.  Also, when
821          * transitioning to/from NOCB mode, a self-requeuing callback might
822          * be invoked from softirq.  A short grace period could cause both
823          * instances of this callback would execute concurrently.
824          */
825         local_bh_disable();
826         rcu_do_batch(rdp);
827         local_bh_enable();
828         lockdep_assert_irqs_enabled();
829         rcu_nocb_lock_irqsave(rdp, flags);
830         if (rcu_segcblist_nextgp(cblist, &cur_gp_seq) &&
831             rcu_seq_done(&rnp->gp_seq, cur_gp_seq) &&
832             raw_spin_trylock_rcu_node(rnp)) { /* irqs already disabled. */
833                 needwake_gp = rcu_advance_cbs(rdp->mynode, rdp);
834                 raw_spin_unlock_rcu_node(rnp); /* irqs remain disabled. */
835         }
836
837         if (rcu_segcblist_test_flags(cblist, SEGCBLIST_OFFLOADED)) {
838                 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB)) {
839                         rcu_segcblist_set_flags(cblist, SEGCBLIST_KTHREAD_CB);
840                         if (rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
841                                 needwake_state = true;
842                 }
843                 if (rcu_segcblist_ready_cbs(cblist))
844                         can_sleep = false;
845         } else {
846                 /*
847                  * De-offloading. Clear our flag and notify the de-offload worker.
848                  * We won't touch the callbacks and keep sleeping until we ever
849                  * get re-offloaded.
850                  */
851                 WARN_ON_ONCE(!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB));
852                 rcu_segcblist_clear_flags(cblist, SEGCBLIST_KTHREAD_CB);
853                 if (!rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP))
854                         needwake_state = true;
855         }
856
857         WRITE_ONCE(rdp->nocb_cb_sleep, can_sleep);
858
859         if (rdp->nocb_cb_sleep)
860                 trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("CBSleep"));
861
862         rcu_nocb_unlock_irqrestore(rdp, flags);
863         if (needwake_gp)
864                 rcu_gp_kthread_wake();
865
866         if (needwake_state)
867                 swake_up_one(&rdp->nocb_state_wq);
868 }
869
870 /*
871  * Per-rcu_data kthread, but only for no-CBs CPUs.  Repeatedly invoke
872  * nocb_cb_wait() to do the dirty work.
873  */
874 static int rcu_nocb_cb_kthread(void *arg)
875 {
876         struct rcu_data *rdp = arg;
877
878         // Each pass through this loop does one callback batch, and,
879         // if there are no more ready callbacks, waits for them.
880         for (;;) {
881                 nocb_cb_wait(rdp);
882                 cond_resched_tasks_rcu_qs();
883         }
884         return 0;
885 }
886
887 /* Is a deferred wakeup of rcu_nocb_kthread() required? */
888 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
889 {
890         return READ_ONCE(rdp->nocb_defer_wakeup) >= level;
891 }
892
893 /* Do a deferred wakeup of rcu_nocb_kthread(). */
894 static bool do_nocb_deferred_wakeup_common(struct rcu_data *rdp_gp,
895                                            struct rcu_data *rdp, int level,
896                                            unsigned long flags)
897         __releases(rdp_gp->nocb_gp_lock)
898 {
899         int ndw;
900         int ret;
901
902         if (!rcu_nocb_need_deferred_wakeup(rdp_gp, level)) {
903                 raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
904                 return false;
905         }
906
907         ndw = rdp_gp->nocb_defer_wakeup;
908         ret = __wake_nocb_gp(rdp_gp, rdp, ndw == RCU_NOCB_WAKE_FORCE, flags);
909         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("DeferredWake"));
910
911         return ret;
912 }
913
914 /* Do a deferred wakeup of rcu_nocb_kthread() from a timer handler. */
915 static void do_nocb_deferred_wakeup_timer(struct timer_list *t)
916 {
917         unsigned long flags;
918         struct rcu_data *rdp = from_timer(rdp, t, nocb_timer);
919
920         WARN_ON_ONCE(rdp->nocb_gp_rdp != rdp);
921         trace_rcu_nocb_wake(rcu_state.name, rdp->cpu, TPS("Timer"));
922
923         raw_spin_lock_irqsave(&rdp->nocb_gp_lock, flags);
924         smp_mb__after_spinlock(); /* Timer expire before wakeup. */
925         do_nocb_deferred_wakeup_common(rdp, rdp, RCU_NOCB_WAKE_BYPASS, flags);
926 }
927
928 /*
929  * Do a deferred wakeup of rcu_nocb_kthread() from fastpath.
930  * This means we do an inexact common-case check.  Note that if
931  * we miss, ->nocb_timer will eventually clean things up.
932  */
933 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
934 {
935         unsigned long flags;
936         struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
937
938         if (!rdp_gp || !rcu_nocb_need_deferred_wakeup(rdp_gp, RCU_NOCB_WAKE))
939                 return false;
940
941         raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
942         return do_nocb_deferred_wakeup_common(rdp_gp, rdp, RCU_NOCB_WAKE, flags);
943 }
944
945 void rcu_nocb_flush_deferred_wakeup(void)
946 {
947         do_nocb_deferred_wakeup(this_cpu_ptr(&rcu_data));
948 }
949 EXPORT_SYMBOL_GPL(rcu_nocb_flush_deferred_wakeup);
950
951 static int rdp_offload_toggle(struct rcu_data *rdp,
952                                bool offload, unsigned long flags)
953         __releases(rdp->nocb_lock)
954 {
955         struct rcu_segcblist *cblist = &rdp->cblist;
956         struct rcu_data *rdp_gp = rdp->nocb_gp_rdp;
957         bool wake_gp = false;
958
959         rcu_segcblist_offload(cblist, offload);
960
961         if (rdp->nocb_cb_sleep)
962                 rdp->nocb_cb_sleep = false;
963         rcu_nocb_unlock_irqrestore(rdp, flags);
964
965         /*
966          * Ignore former value of nocb_cb_sleep and force wake up as it could
967          * have been spuriously set to false already.
968          */
969         swake_up_one(&rdp->nocb_cb_wq);
970
971         raw_spin_lock_irqsave(&rdp_gp->nocb_gp_lock, flags);
972         if (rdp_gp->nocb_gp_sleep) {
973                 rdp_gp->nocb_gp_sleep = false;
974                 wake_gp = true;
975         }
976         raw_spin_unlock_irqrestore(&rdp_gp->nocb_gp_lock, flags);
977
978         if (wake_gp)
979                 wake_up_process(rdp_gp->nocb_gp_kthread);
980
981         return 0;
982 }
983
984 static long rcu_nocb_rdp_deoffload(void *arg)
985 {
986         struct rcu_data *rdp = arg;
987         struct rcu_segcblist *cblist = &rdp->cblist;
988         unsigned long flags;
989         int ret;
990
991         WARN_ON_ONCE(rdp->cpu != raw_smp_processor_id());
992
993         pr_info("De-offloading %d\n", rdp->cpu);
994
995         rcu_nocb_lock_irqsave(rdp, flags);
996         /*
997          * Flush once and for all now. This suffices because we are
998          * running on the target CPU holding ->nocb_lock (thus having
999          * interrupts disabled), and because rdp_offload_toggle()
1000          * invokes rcu_segcblist_offload(), which clears SEGCBLIST_OFFLOADED.
1001          * Thus future calls to rcu_segcblist_completely_offloaded() will
1002          * return false, which means that future calls to rcu_nocb_try_bypass()
1003          * will refuse to put anything into the bypass.
1004          */
1005         WARN_ON_ONCE(!rcu_nocb_flush_bypass(rdp, NULL, jiffies));
1006         /*
1007          * Start with invoking rcu_core() early. This way if the current thread
1008          * happens to preempt an ongoing call to rcu_core() in the middle,
1009          * leaving some work dismissed because rcu_core() still thinks the rdp is
1010          * completely offloaded, we are guaranteed a nearby future instance of
1011          * rcu_core() to catch up.
1012          */
1013         rcu_segcblist_set_flags(cblist, SEGCBLIST_RCU_CORE);
1014         invoke_rcu_core();
1015         ret = rdp_offload_toggle(rdp, false, flags);
1016         swait_event_exclusive(rdp->nocb_state_wq,
1017                               !rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB |
1018                                                         SEGCBLIST_KTHREAD_GP));
1019         /* Stop nocb_gp_wait() from iterating over this structure. */
1020         list_del_rcu(&rdp->nocb_entry_rdp);
1021         /*
1022          * Lock one last time to acquire latest callback updates from kthreads
1023          * so we can later handle callbacks locally without locking.
1024          */
1025         rcu_nocb_lock_irqsave(rdp, flags);
1026         /*
1027          * Theoretically we could clear SEGCBLIST_LOCKING after the nocb
1028          * lock is released but how about being paranoid for once?
1029          */
1030         rcu_segcblist_clear_flags(cblist, SEGCBLIST_LOCKING);
1031         /*
1032          * Without SEGCBLIST_LOCKING, we can't use
1033          * rcu_nocb_unlock_irqrestore() anymore.
1034          */
1035         raw_spin_unlock_irqrestore(&rdp->nocb_lock, flags);
1036
1037         /* Sanity check */
1038         WARN_ON_ONCE(rcu_cblist_n_cbs(&rdp->nocb_bypass));
1039
1040
1041         return ret;
1042 }
1043
1044 int rcu_nocb_cpu_deoffload(int cpu)
1045 {
1046         struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1047         int ret = 0;
1048
1049         mutex_lock(&rcu_state.barrier_mutex);
1050         cpus_read_lock();
1051         if (rcu_rdp_is_offloaded(rdp)) {
1052                 if (cpu_online(cpu)) {
1053                         ret = work_on_cpu(cpu, rcu_nocb_rdp_deoffload, rdp);
1054                         if (!ret)
1055                                 cpumask_clear_cpu(cpu, rcu_nocb_mask);
1056                 } else {
1057                         pr_info("NOCB: Can't CB-deoffload an offline CPU\n");
1058                         ret = -EINVAL;
1059                 }
1060         }
1061         cpus_read_unlock();
1062         mutex_unlock(&rcu_state.barrier_mutex);
1063
1064         return ret;
1065 }
1066 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_deoffload);
1067
1068 static long rcu_nocb_rdp_offload(void *arg)
1069 {
1070         struct rcu_data *rdp = arg;
1071         struct rcu_segcblist *cblist = &rdp->cblist;
1072         unsigned long flags;
1073         int ret;
1074
1075         WARN_ON_ONCE(rdp->cpu != raw_smp_processor_id());
1076         /*
1077          * For now we only support re-offload, ie: the rdp must have been
1078          * offloaded on boot first.
1079          */
1080         if (!rdp->nocb_gp_rdp)
1081                 return -EINVAL;
1082
1083         pr_info("Offloading %d\n", rdp->cpu);
1084
1085         /*
1086          * Cause future nocb_gp_wait() invocations to iterate over
1087          * structure, resetting ->nocb_gp_sleep and waking up the related
1088          * "rcuog".  Since nocb_gp_wait() in turn locks ->nocb_gp_lock
1089          * before setting ->nocb_gp_sleep again, we are guaranteed to
1090          * iterate this newly added structure before "rcuog" goes to
1091          * sleep again.
1092          */
1093         list_add_tail_rcu(&rdp->nocb_entry_rdp, &rdp->nocb_gp_rdp->nocb_head_rdp);
1094
1095         /*
1096          * Can't use rcu_nocb_lock_irqsave() before SEGCBLIST_LOCKING
1097          * is set.
1098          */
1099         raw_spin_lock_irqsave(&rdp->nocb_lock, flags);
1100
1101         /*
1102          * We didn't take the nocb lock while working on the
1103          * rdp->cblist with SEGCBLIST_LOCKING cleared (pure softirq/rcuc mode).
1104          * Every modifications that have been done previously on
1105          * rdp->cblist must be visible remotely by the nocb kthreads
1106          * upon wake up after reading the cblist flags.
1107          *
1108          * The layout against nocb_lock enforces that ordering:
1109          *
1110          *  __rcu_nocb_rdp_offload()   nocb_cb_wait()/nocb_gp_wait()
1111          * -------------------------   ----------------------------
1112          *      WRITE callbacks           rcu_nocb_lock()
1113          *      rcu_nocb_lock()           READ flags
1114          *      WRITE flags               READ callbacks
1115          *      rcu_nocb_unlock()         rcu_nocb_unlock()
1116          */
1117         ret = rdp_offload_toggle(rdp, true, flags);
1118         swait_event_exclusive(rdp->nocb_state_wq,
1119                               rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_CB) &&
1120                               rcu_segcblist_test_flags(cblist, SEGCBLIST_KTHREAD_GP));
1121
1122         /*
1123          * All kthreads are ready to work, we can finally relieve rcu_core() and
1124          * enable nocb bypass.
1125          */
1126         rcu_nocb_lock_irqsave(rdp, flags);
1127         rcu_segcblist_clear_flags(cblist, SEGCBLIST_RCU_CORE);
1128         rcu_nocb_unlock_irqrestore(rdp, flags);
1129
1130         return ret;
1131 }
1132
1133 int rcu_nocb_cpu_offload(int cpu)
1134 {
1135         struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1136         int ret = 0;
1137
1138         mutex_lock(&rcu_state.barrier_mutex);
1139         cpus_read_lock();
1140         if (!rcu_rdp_is_offloaded(rdp)) {
1141                 if (cpu_online(cpu)) {
1142                         ret = work_on_cpu(cpu, rcu_nocb_rdp_offload, rdp);
1143                         if (!ret)
1144                                 cpumask_set_cpu(cpu, rcu_nocb_mask);
1145                 } else {
1146                         pr_info("NOCB: Can't CB-offload an offline CPU\n");
1147                         ret = -EINVAL;
1148                 }
1149         }
1150         cpus_read_unlock();
1151         mutex_unlock(&rcu_state.barrier_mutex);
1152
1153         return ret;
1154 }
1155 EXPORT_SYMBOL_GPL(rcu_nocb_cpu_offload);
1156
1157 void __init rcu_init_nohz(void)
1158 {
1159         int cpu;
1160         bool need_rcu_nocb_mask = false;
1161         struct rcu_data *rdp;
1162
1163 #if defined(CONFIG_NO_HZ_FULL)
1164         if (tick_nohz_full_running && !cpumask_empty(tick_nohz_full_mask))
1165                 need_rcu_nocb_mask = true;
1166 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
1167
1168         if (need_rcu_nocb_mask) {
1169                 if (!cpumask_available(rcu_nocb_mask)) {
1170                         if (!zalloc_cpumask_var(&rcu_nocb_mask, GFP_KERNEL)) {
1171                                 pr_info("rcu_nocb_mask allocation failed, callback offloading disabled.\n");
1172                                 return;
1173                         }
1174                 }
1175                 rcu_nocb_is_setup = true;
1176         }
1177
1178         if (!rcu_nocb_is_setup)
1179                 return;
1180
1181 #if defined(CONFIG_NO_HZ_FULL)
1182         if (tick_nohz_full_running)
1183                 cpumask_or(rcu_nocb_mask, rcu_nocb_mask, tick_nohz_full_mask);
1184 #endif /* #if defined(CONFIG_NO_HZ_FULL) */
1185
1186         if (!cpumask_subset(rcu_nocb_mask, cpu_possible_mask)) {
1187                 pr_info("\tNote: kernel parameter 'rcu_nocbs=', 'nohz_full', or 'isolcpus=' contains nonexistent CPUs.\n");
1188                 cpumask_and(rcu_nocb_mask, cpu_possible_mask,
1189                             rcu_nocb_mask);
1190         }
1191         if (cpumask_empty(rcu_nocb_mask))
1192                 pr_info("\tOffload RCU callbacks from CPUs: (none).\n");
1193         else
1194                 pr_info("\tOffload RCU callbacks from CPUs: %*pbl.\n",
1195                         cpumask_pr_args(rcu_nocb_mask));
1196         if (rcu_nocb_poll)
1197                 pr_info("\tPoll for callbacks from no-CBs CPUs.\n");
1198
1199         for_each_cpu(cpu, rcu_nocb_mask) {
1200                 rdp = per_cpu_ptr(&rcu_data, cpu);
1201                 if (rcu_segcblist_empty(&rdp->cblist))
1202                         rcu_segcblist_init(&rdp->cblist);
1203                 rcu_segcblist_offload(&rdp->cblist, true);
1204                 rcu_segcblist_set_flags(&rdp->cblist, SEGCBLIST_KTHREAD_CB | SEGCBLIST_KTHREAD_GP);
1205                 rcu_segcblist_clear_flags(&rdp->cblist, SEGCBLIST_RCU_CORE);
1206         }
1207         rcu_organize_nocb_kthreads();
1208 }
1209
1210 /* Initialize per-rcu_data variables for no-CBs CPUs. */
1211 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1212 {
1213         init_swait_queue_head(&rdp->nocb_cb_wq);
1214         init_swait_queue_head(&rdp->nocb_gp_wq);
1215         init_swait_queue_head(&rdp->nocb_state_wq);
1216         raw_spin_lock_init(&rdp->nocb_lock);
1217         raw_spin_lock_init(&rdp->nocb_bypass_lock);
1218         raw_spin_lock_init(&rdp->nocb_gp_lock);
1219         timer_setup(&rdp->nocb_timer, do_nocb_deferred_wakeup_timer, 0);
1220         rcu_cblist_init(&rdp->nocb_bypass);
1221         mutex_init(&rdp->nocb_gp_kthread_mutex);
1222 }
1223
1224 /*
1225  * If the specified CPU is a no-CBs CPU that does not already have its
1226  * rcuo CB kthread, spawn it.  Additionally, if the rcuo GP kthread
1227  * for this CPU's group has not yet been created, spawn it as well.
1228  */
1229 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1230 {
1231         struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1232         struct rcu_data *rdp_gp;
1233         struct task_struct *t;
1234         struct sched_param sp;
1235
1236         if (!rcu_scheduler_fully_active || !rcu_nocb_is_setup)
1237                 return;
1238
1239         /* If there already is an rcuo kthread, then nothing to do. */
1240         if (rdp->nocb_cb_kthread)
1241                 return;
1242
1243         /* If we didn't spawn the GP kthread first, reorganize! */
1244         sp.sched_priority = kthread_prio;
1245         rdp_gp = rdp->nocb_gp_rdp;
1246         mutex_lock(&rdp_gp->nocb_gp_kthread_mutex);
1247         if (!rdp_gp->nocb_gp_kthread) {
1248                 t = kthread_run(rcu_nocb_gp_kthread, rdp_gp,
1249                                 "rcuog/%d", rdp_gp->cpu);
1250                 if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo GP kthread, OOM is now expected behavior\n", __func__)) {
1251                         mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1252                         return;
1253                 }
1254                 WRITE_ONCE(rdp_gp->nocb_gp_kthread, t);
1255                 if (kthread_prio)
1256                         sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1257         }
1258         mutex_unlock(&rdp_gp->nocb_gp_kthread_mutex);
1259
1260         /* Spawn the kthread for this CPU. */
1261         t = kthread_run(rcu_nocb_cb_kthread, rdp,
1262                         "rcuo%c/%d", rcu_state.abbr, cpu);
1263         if (WARN_ONCE(IS_ERR(t), "%s: Could not start rcuo CB kthread, OOM is now expected behavior\n", __func__))
1264                 return;
1265
1266         if (kthread_prio)
1267                 sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1268         WRITE_ONCE(rdp->nocb_cb_kthread, t);
1269         WRITE_ONCE(rdp->nocb_gp_kthread, rdp_gp->nocb_gp_kthread);
1270 }
1271
1272 /*
1273  * Once the scheduler is running, spawn rcuo kthreads for all online
1274  * no-CBs CPUs.  This assumes that the early_initcall()s happen before
1275  * non-boot CPUs come online -- if this changes, we will need to add
1276  * some mutual exclusion.
1277  */
1278 static void __init rcu_spawn_nocb_kthreads(void)
1279 {
1280         int cpu;
1281
1282         if (rcu_nocb_is_setup) {
1283                 for_each_online_cpu(cpu)
1284                         rcu_spawn_cpu_nocb_kthread(cpu);
1285         }
1286 }
1287
1288 /* How many CB CPU IDs per GP kthread?  Default of -1 for sqrt(nr_cpu_ids). */
1289 static int rcu_nocb_gp_stride = -1;
1290 module_param(rcu_nocb_gp_stride, int, 0444);
1291
1292 /*
1293  * Initialize GP-CB relationships for all no-CBs CPU.
1294  */
1295 static void __init rcu_organize_nocb_kthreads(void)
1296 {
1297         int cpu;
1298         bool firsttime = true;
1299         bool gotnocbs = false;
1300         bool gotnocbscbs = true;
1301         int ls = rcu_nocb_gp_stride;
1302         int nl = 0;  /* Next GP kthread. */
1303         struct rcu_data *rdp;
1304         struct rcu_data *rdp_gp = NULL;  /* Suppress misguided gcc warn. */
1305
1306         if (!cpumask_available(rcu_nocb_mask))
1307                 return;
1308         if (ls == -1) {
1309                 ls = nr_cpu_ids / int_sqrt(nr_cpu_ids);
1310                 rcu_nocb_gp_stride = ls;
1311         }
1312
1313         /*
1314          * Each pass through this loop sets up one rcu_data structure.
1315          * Should the corresponding CPU come online in the future, then
1316          * we will spawn the needed set of rcu_nocb_kthread() kthreads.
1317          */
1318         for_each_possible_cpu(cpu) {
1319                 rdp = per_cpu_ptr(&rcu_data, cpu);
1320                 if (rdp->cpu >= nl) {
1321                         /* New GP kthread, set up for CBs & next GP. */
1322                         gotnocbs = true;
1323                         nl = DIV_ROUND_UP(rdp->cpu + 1, ls) * ls;
1324                         rdp_gp = rdp;
1325                         INIT_LIST_HEAD(&rdp->nocb_head_rdp);
1326                         if (dump_tree) {
1327                                 if (!firsttime)
1328                                         pr_cont("%s\n", gotnocbscbs
1329                                                         ? "" : " (self only)");
1330                                 gotnocbscbs = false;
1331                                 firsttime = false;
1332                                 pr_alert("%s: No-CB GP kthread CPU %d:",
1333                                          __func__, cpu);
1334                         }
1335                 } else {
1336                         /* Another CB kthread, link to previous GP kthread. */
1337                         gotnocbscbs = true;
1338                         if (dump_tree)
1339                                 pr_cont(" %d", cpu);
1340                 }
1341                 rdp->nocb_gp_rdp = rdp_gp;
1342                 if (cpumask_test_cpu(cpu, rcu_nocb_mask))
1343                         list_add_tail(&rdp->nocb_entry_rdp, &rdp_gp->nocb_head_rdp);
1344         }
1345         if (gotnocbs && dump_tree)
1346                 pr_cont("%s\n", gotnocbscbs ? "" : " (self only)");
1347 }
1348
1349 /*
1350  * Bind the current task to the offloaded CPUs.  If there are no offloaded
1351  * CPUs, leave the task unbound.  Splat if the bind attempt fails.
1352  */
1353 void rcu_bind_current_to_nocb(void)
1354 {
1355         if (cpumask_available(rcu_nocb_mask) && !cpumask_empty(rcu_nocb_mask))
1356                 WARN_ON(sched_setaffinity(current->pid, rcu_nocb_mask));
1357 }
1358 EXPORT_SYMBOL_GPL(rcu_bind_current_to_nocb);
1359
1360 // The ->on_cpu field is available only in CONFIG_SMP=y, so...
1361 #ifdef CONFIG_SMP
1362 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1363 {
1364         return tsp && task_is_running(tsp) && !tsp->on_cpu ? "!" : "";
1365 }
1366 #else // #ifdef CONFIG_SMP
1367 static char *show_rcu_should_be_on_cpu(struct task_struct *tsp)
1368 {
1369         return "";
1370 }
1371 #endif // #else #ifdef CONFIG_SMP
1372
1373 /*
1374  * Dump out nocb grace-period kthread state for the specified rcu_data
1375  * structure.
1376  */
1377 static void show_rcu_nocb_gp_state(struct rcu_data *rdp)
1378 {
1379         struct rcu_node *rnp = rdp->mynode;
1380
1381         pr_info("nocb GP %d %c%c%c%c%c %c[%c%c] %c%c:%ld rnp %d:%d %lu %c CPU %d%s\n",
1382                 rdp->cpu,
1383                 "kK"[!!rdp->nocb_gp_kthread],
1384                 "lL"[raw_spin_is_locked(&rdp->nocb_gp_lock)],
1385                 "dD"[!!rdp->nocb_defer_wakeup],
1386                 "tT"[timer_pending(&rdp->nocb_timer)],
1387                 "sS"[!!rdp->nocb_gp_sleep],
1388                 ".W"[swait_active(&rdp->nocb_gp_wq)],
1389                 ".W"[swait_active(&rnp->nocb_gp_wq[0])],
1390                 ".W"[swait_active(&rnp->nocb_gp_wq[1])],
1391                 ".B"[!!rdp->nocb_gp_bypass],
1392                 ".G"[!!rdp->nocb_gp_gp],
1393                 (long)rdp->nocb_gp_seq,
1394                 rnp->grplo, rnp->grphi, READ_ONCE(rdp->nocb_gp_loops),
1395                 rdp->nocb_gp_kthread ? task_state_to_char(rdp->nocb_gp_kthread) : '.',
1396                 rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1397                 show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1398 }
1399
1400 /* Dump out nocb kthread state for the specified rcu_data structure. */
1401 static void show_rcu_nocb_state(struct rcu_data *rdp)
1402 {
1403         char bufw[20];
1404         char bufr[20];
1405         struct rcu_data *nocb_next_rdp;
1406         struct rcu_segcblist *rsclp = &rdp->cblist;
1407         bool waslocked;
1408         bool wassleep;
1409
1410         if (rdp->nocb_gp_rdp == rdp)
1411                 show_rcu_nocb_gp_state(rdp);
1412
1413         nocb_next_rdp = list_next_or_null_rcu(&rdp->nocb_gp_rdp->nocb_head_rdp,
1414                                               &rdp->nocb_entry_rdp,
1415                                               typeof(*rdp),
1416                                               nocb_entry_rdp);
1417
1418         sprintf(bufw, "%ld", rsclp->gp_seq[RCU_WAIT_TAIL]);
1419         sprintf(bufr, "%ld", rsclp->gp_seq[RCU_NEXT_READY_TAIL]);
1420         pr_info("   CB %d^%d->%d %c%c%c%c%c%c F%ld L%ld C%d %c%c%s%c%s%c%c q%ld %c CPU %d%s\n",
1421                 rdp->cpu, rdp->nocb_gp_rdp->cpu,
1422                 nocb_next_rdp ? nocb_next_rdp->cpu : -1,
1423                 "kK"[!!rdp->nocb_cb_kthread],
1424                 "bB"[raw_spin_is_locked(&rdp->nocb_bypass_lock)],
1425                 "cC"[!!atomic_read(&rdp->nocb_lock_contended)],
1426                 "lL"[raw_spin_is_locked(&rdp->nocb_lock)],
1427                 "sS"[!!rdp->nocb_cb_sleep],
1428                 ".W"[swait_active(&rdp->nocb_cb_wq)],
1429                 jiffies - rdp->nocb_bypass_first,
1430                 jiffies - rdp->nocb_nobypass_last,
1431                 rdp->nocb_nobypass_count,
1432                 ".D"[rcu_segcblist_ready_cbs(rsclp)],
1433                 ".W"[!rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL)],
1434                 rcu_segcblist_segempty(rsclp, RCU_WAIT_TAIL) ? "" : bufw,
1435                 ".R"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL)],
1436                 rcu_segcblist_segempty(rsclp, RCU_NEXT_READY_TAIL) ? "" : bufr,
1437                 ".N"[!rcu_segcblist_segempty(rsclp, RCU_NEXT_TAIL)],
1438                 ".B"[!!rcu_cblist_n_cbs(&rdp->nocb_bypass)],
1439                 rcu_segcblist_n_cbs(&rdp->cblist),
1440                 rdp->nocb_cb_kthread ? task_state_to_char(rdp->nocb_cb_kthread) : '.',
1441                 rdp->nocb_cb_kthread ? (int)task_cpu(rdp->nocb_gp_kthread) : -1,
1442                 show_rcu_should_be_on_cpu(rdp->nocb_cb_kthread));
1443
1444         /* It is OK for GP kthreads to have GP state. */
1445         if (rdp->nocb_gp_rdp == rdp)
1446                 return;
1447
1448         waslocked = raw_spin_is_locked(&rdp->nocb_gp_lock);
1449         wassleep = swait_active(&rdp->nocb_gp_wq);
1450         if (!rdp->nocb_gp_sleep && !waslocked && !wassleep)
1451                 return;  /* Nothing untoward. */
1452
1453         pr_info("   nocb GP activity on CB-only CPU!!! %c%c%c %c\n",
1454                 "lL"[waslocked],
1455                 "dD"[!!rdp->nocb_defer_wakeup],
1456                 "sS"[!!rdp->nocb_gp_sleep],
1457                 ".W"[wassleep]);
1458 }
1459
1460 #else /* #ifdef CONFIG_RCU_NOCB_CPU */
1461
1462 static inline int rcu_lockdep_is_held_nocb(struct rcu_data *rdp)
1463 {
1464         return 0;
1465 }
1466
1467 static inline bool rcu_current_is_nocb_kthread(struct rcu_data *rdp)
1468 {
1469         return false;
1470 }
1471
1472 /* No ->nocb_lock to acquire.  */
1473 static void rcu_nocb_lock(struct rcu_data *rdp)
1474 {
1475 }
1476
1477 /* No ->nocb_lock to release.  */
1478 static void rcu_nocb_unlock(struct rcu_data *rdp)
1479 {
1480 }
1481
1482 /* No ->nocb_lock to release.  */
1483 static void rcu_nocb_unlock_irqrestore(struct rcu_data *rdp,
1484                                        unsigned long flags)
1485 {
1486         local_irq_restore(flags);
1487 }
1488
1489 /* Lockdep check that ->cblist may be safely accessed. */
1490 static void rcu_lockdep_assert_cblist_protected(struct rcu_data *rdp)
1491 {
1492         lockdep_assert_irqs_disabled();
1493 }
1494
1495 static void rcu_nocb_gp_cleanup(struct swait_queue_head *sq)
1496 {
1497 }
1498
1499 static struct swait_queue_head *rcu_nocb_gp_get(struct rcu_node *rnp)
1500 {
1501         return NULL;
1502 }
1503
1504 static void rcu_init_one_nocb(struct rcu_node *rnp)
1505 {
1506 }
1507
1508 static bool rcu_nocb_flush_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1509                                   unsigned long j)
1510 {
1511         return true;
1512 }
1513
1514 static bool rcu_nocb_try_bypass(struct rcu_data *rdp, struct rcu_head *rhp,
1515                                 bool *was_alldone, unsigned long flags)
1516 {
1517         return false;
1518 }
1519
1520 static void __call_rcu_nocb_wake(struct rcu_data *rdp, bool was_empty,
1521                                  unsigned long flags)
1522 {
1523         WARN_ON_ONCE(1);  /* Should be dead code! */
1524 }
1525
1526 static void __init rcu_boot_init_nocb_percpu_data(struct rcu_data *rdp)
1527 {
1528 }
1529
1530 static int rcu_nocb_need_deferred_wakeup(struct rcu_data *rdp, int level)
1531 {
1532         return false;
1533 }
1534
1535 static bool do_nocb_deferred_wakeup(struct rcu_data *rdp)
1536 {
1537         return false;
1538 }
1539
1540 static void rcu_spawn_cpu_nocb_kthread(int cpu)
1541 {
1542 }
1543
1544 static void __init rcu_spawn_nocb_kthreads(void)
1545 {
1546 }
1547
1548 static void show_rcu_nocb_state(struct rcu_data *rdp)
1549 {
1550 }
1551
1552 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */