rcu-tasks: Permit use of debug-objects with RCU Tasks flavors
[linux-block.git] / kernel / rcu / tasks.h
CommitLineData
eacd6f04
PM
1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Task-based RCU implementations.
4 *
5 * Copyright (C) 2020 Paul E. McKenney
6 */
7
8fd8ca38 8#ifdef CONFIG_TASKS_RCU_GENERIC
9b073de1 9#include "rcu_segcblist.h"
5873b8a9
PM
10
11////////////////////////////////////////////////////////////////////////
12//
13// Generic data structures.
14
15struct rcu_tasks;
16typedef void (*rcu_tasks_gp_func_t)(struct rcu_tasks *rtp);
7460ade1 17typedef void (*pregp_func_t)(struct list_head *hop);
e4fe5dd6 18typedef void (*pertask_func_t)(struct task_struct *t, struct list_head *hop);
9796e1ae 19typedef void (*postscan_func_t)(struct list_head *hop);
e4fe5dd6 20typedef void (*holdouts_func_t)(struct list_head *hop, bool ndrpt, bool *frptp);
af051ca4 21typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
eacd6f04 22
07e10515 23/**
cafafd67 24 * struct rcu_tasks_percpu - Per-CPU component of definition for a Tasks-RCU-like mechanism.
9b073de1 25 * @cblist: Callback list.
381a4f3b 26 * @lock: Lock protecting per-CPU callback list.
7d13d30b 27 * @rtp_jiffies: Jiffies counter value for statistics.
d119357d
PM
28 * @lazy_timer: Timer to unlazify callbacks.
29 * @urgent_gp: Number of additional non-lazy grace periods.
7d13d30b 30 * @rtp_n_lock_retries: Rough lock-contention statistic.
d363f833 31 * @rtp_work: Work queue for invoking callbacks.
3063b33a 32 * @rtp_irq_work: IRQ work queue for deferred wakeups.
ce9b1c66 33 * @barrier_q_head: RCU callback for barrier operation.
434c9eef 34 * @rtp_blkd_tasks: List of tasks blocked as readers.
ce9b1c66
PM
35 * @cpu: CPU number corresponding to this entry.
36 * @rtpp: Pointer to the rcu_tasks structure.
cafafd67
PM
37 */
38struct rcu_tasks_percpu {
9b073de1 39 struct rcu_segcblist cblist;
381a4f3b 40 raw_spinlock_t __private lock;
7d13d30b
PM
41 unsigned long rtp_jiffies;
42 unsigned long rtp_n_lock_retries;
d119357d
PM
43 struct timer_list lazy_timer;
44 unsigned int urgent_gp;
d363f833 45 struct work_struct rtp_work;
3063b33a 46 struct irq_work rtp_irq_work;
ce9b1c66 47 struct rcu_head barrier_q_head;
434c9eef 48 struct list_head rtp_blkd_tasks;
d363f833
PM
49 int cpu;
50 struct rcu_tasks *rtpp;
cafafd67
PM
51};
52
53/**
54 * struct rcu_tasks - Definition for a Tasks-RCU-like mechanism.
88db792b 55 * @cbs_wait: RCU wait allowing a new callback to get kthread's attention.
cafafd67 56 * @cbs_gbl_lock: Lock protecting callback list.
d96225fd 57 * @tasks_gp_mutex: Mutex protecting grace period, needed during mid-boot dead zone.
5873b8a9 58 * @gp_func: This flavor's grace-period-wait function.
af051ca4 59 * @gp_state: Grace period's most recent state transition (debugging).
4fe192df 60 * @gp_sleep: Per-grace-period sleep to prevent CPU-bound looping.
2393a613 61 * @init_fract: Initial backoff sleep interval.
af051ca4
PM
62 * @gp_jiffies: Time of last @gp_state transition.
63 * @gp_start: Most recent grace-period start in jiffies.
b14fb4fb 64 * @tasks_gp_seq: Number of grace periods completed since boot.
238dbce3 65 * @n_ipis: Number of IPIs sent to encourage grace periods to end.
7e0669c3 66 * @n_ipis_fails: Number of IPI-send failures.
d119357d
PM
67 * @kthread_ptr: This flavor's grace-period/callback-invocation kthread.
68 * @lazy_jiffies: Number of jiffies to allow callbacks to be lazy.
e4fe5dd6
PM
69 * @pregp_func: This flavor's pre-grace-period function (optional).
70 * @pertask_func: This flavor's per-task scan function (optional).
71 * @postscan_func: This flavor's post-task scan function (optional).
85b86994 72 * @holdouts_func: This flavor's holdout-list scan function (optional).
e4fe5dd6 73 * @postgp_func: This flavor's post-grace-period function (optional).
5873b8a9 74 * @call_func: This flavor's call_rcu()-equivalent function.
cafafd67 75 * @rtpcpu: This flavor's rcu_tasks_percpu structure.
7a30871b 76 * @percpu_enqueue_shift: Shift down CPU ID this much when enqueuing callbacks.
2cee0789
PM
77 * @percpu_enqueue_lim: Number of per-CPU callback queues in use for enqueuing.
78 * @percpu_dequeue_lim: Number of per-CPU callback queues in use for dequeuing.
fd796e41 79 * @percpu_dequeue_gpseq: RCU grace-period number to propagate enqueue limit to dequeuers.
ce9b1c66
PM
80 * @barrier_q_mutex: Serialize barrier operations.
81 * @barrier_q_count: Number of queues being waited on.
82 * @barrier_q_completion: Barrier wait/wakeup mechanism.
83 * @barrier_q_seq: Sequence number for barrier operations.
c97d12a6
PM
84 * @name: This flavor's textual name.
85 * @kname: This flavor's kthread name.
07e10515
PM
86 */
87struct rcu_tasks {
88db792b 88 struct rcuwait cbs_wait;
cafafd67 89 raw_spinlock_t cbs_gbl_lock;
d96225fd 90 struct mutex tasks_gp_mutex;
af051ca4 91 int gp_state;
4fe192df 92 int gp_sleep;
2393a613 93 int init_fract;
af051ca4 94 unsigned long gp_jiffies;
88092d0c 95 unsigned long gp_start;
b14fb4fb 96 unsigned long tasks_gp_seq;
238dbce3 97 unsigned long n_ipis;
7e0669c3 98 unsigned long n_ipis_fails;
07e10515 99 struct task_struct *kthread_ptr;
d119357d 100 unsigned long lazy_jiffies;
5873b8a9 101 rcu_tasks_gp_func_t gp_func;
e4fe5dd6
PM
102 pregp_func_t pregp_func;
103 pertask_func_t pertask_func;
104 postscan_func_t postscan_func;
105 holdouts_func_t holdouts_func;
106 postgp_func_t postgp_func;
5873b8a9 107 call_rcu_func_t call_func;
cafafd67 108 struct rcu_tasks_percpu __percpu *rtpcpu;
7a30871b 109 int percpu_enqueue_shift;
8dd593fd 110 int percpu_enqueue_lim;
2cee0789 111 int percpu_dequeue_lim;
fd796e41 112 unsigned long percpu_dequeue_gpseq;
ce9b1c66
PM
113 struct mutex barrier_q_mutex;
114 atomic_t barrier_q_count;
115 struct completion barrier_q_completion;
116 unsigned long barrier_q_seq;
c97d12a6
PM
117 char *name;
118 char *kname;
07e10515
PM
119};
120
3063b33a
PM
121static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp);
122
cafafd67
PM
123#define DEFINE_RCU_TASKS(rt_name, gp, call, n) \
124static DEFINE_PER_CPU(struct rcu_tasks_percpu, rt_name ## __percpu) = { \
381a4f3b 125 .lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name ## __percpu.cbs_pcpu_lock), \
88db792b 126 .rtp_irq_work = IRQ_WORK_INIT_HARD(call_rcu_tasks_iw_wakeup), \
cafafd67
PM
127}; \
128static struct rcu_tasks rt_name = \
129{ \
88db792b 130 .cbs_wait = __RCUWAIT_INITIALIZER(rt_name.wait), \
cafafd67 131 .cbs_gbl_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_gbl_lock), \
d96225fd 132 .tasks_gp_mutex = __MUTEX_INITIALIZER(rt_name.tasks_gp_mutex), \
cafafd67
PM
133 .gp_func = gp, \
134 .call_func = call, \
135 .rtpcpu = &rt_name ## __percpu, \
d119357d 136 .lazy_jiffies = DIV_ROUND_UP(HZ, 4), \
cafafd67 137 .name = n, \
2bcd18e0 138 .percpu_enqueue_shift = order_base_2(CONFIG_NR_CPUS), \
8dd593fd 139 .percpu_enqueue_lim = 1, \
2cee0789 140 .percpu_dequeue_lim = 1, \
ce9b1c66
PM
141 .barrier_q_mutex = __MUTEX_INITIALIZER(rt_name.barrier_q_mutex), \
142 .barrier_q_seq = (0UL - 50UL) << RCU_SEQ_CTR_SHIFT, \
cafafd67 143 .kname = #rt_name, \
07e10515
PM
144}
145
2b4be548 146#ifdef CONFIG_TASKS_RCU
eacd6f04
PM
147/* Track exiting tasks in order to allow them to be waited for. */
148DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
149
a4533cc0
NU
150/* Report delay in synchronize_srcu() completion in rcu_tasks_postscan(). */
151static void tasks_rcu_exit_srcu_stall(struct timer_list *unused);
152static DEFINE_TIMER(tasks_rcu_exit_srcu_stall_timer, tasks_rcu_exit_srcu_stall);
153#endif
154
b0afa0f0 155/* Avoid IPIing CPUs early in the grace period. */
574de876 156#define RCU_TASK_IPI_DELAY (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) ? HZ / 2 : 0)
b0afa0f0
PM
157static int rcu_task_ipi_delay __read_mostly = RCU_TASK_IPI_DELAY;
158module_param(rcu_task_ipi_delay, int, 0644);
159
eacd6f04 160/* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
1cf1144e 161#define RCU_TASK_BOOT_STALL_TIMEOUT (HZ * 30)
eacd6f04
PM
162#define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
163static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
164module_param(rcu_task_stall_timeout, int, 0644);
f2539003
PM
165#define RCU_TASK_STALL_INFO (HZ * 10)
166static int rcu_task_stall_info __read_mostly = RCU_TASK_STALL_INFO;
167module_param(rcu_task_stall_info, int, 0644);
168static int rcu_task_stall_info_mult __read_mostly = 3;
169module_param(rcu_task_stall_info_mult, int, 0444);
eacd6f04 170
8610b656
PM
171static int rcu_task_enqueue_lim __read_mostly = -1;
172module_param(rcu_task_enqueue_lim, int, 0444);
173
ab97152f
PM
174static bool rcu_task_cb_adjust;
175static int rcu_task_contend_lim __read_mostly = 100;
176module_param(rcu_task_contend_lim, int, 0444);
fd796e41
PM
177static int rcu_task_collapse_lim __read_mostly = 10;
178module_param(rcu_task_collapse_lim, int, 0444);
db13710a
PM
179static int rcu_task_lazy_lim __read_mostly = 32;
180module_param(rcu_task_lazy_lim, int, 0444);
ab97152f 181
af051ca4
PM
182/* RCU tasks grace-period state for debugging. */
183#define RTGS_INIT 0
184#define RTGS_WAIT_WAIT_CBS 1
185#define RTGS_WAIT_GP 2
186#define RTGS_PRE_WAIT_GP 3
187#define RTGS_SCAN_TASKLIST 4
188#define RTGS_POST_SCAN_TASKLIST 5
189#define RTGS_WAIT_SCAN_HOLDOUTS 6
190#define RTGS_SCAN_HOLDOUTS 7
191#define RTGS_POST_GP 8
192#define RTGS_WAIT_READERS 9
193#define RTGS_INVOKE_CBS 10
194#define RTGS_WAIT_CBS 11
8344496e 195#ifndef CONFIG_TINY_RCU
af051ca4
PM
196static const char * const rcu_tasks_gp_state_names[] = {
197 "RTGS_INIT",
198 "RTGS_WAIT_WAIT_CBS",
199 "RTGS_WAIT_GP",
200 "RTGS_PRE_WAIT_GP",
201 "RTGS_SCAN_TASKLIST",
202 "RTGS_POST_SCAN_TASKLIST",
203 "RTGS_WAIT_SCAN_HOLDOUTS",
204 "RTGS_SCAN_HOLDOUTS",
205 "RTGS_POST_GP",
206 "RTGS_WAIT_READERS",
207 "RTGS_INVOKE_CBS",
208 "RTGS_WAIT_CBS",
209};
8344496e 210#endif /* #ifndef CONFIG_TINY_RCU */
af051ca4 211
5873b8a9
PM
212////////////////////////////////////////////////////////////////////////
213//
214// Generic code.
215
d363f833
PM
216static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp);
217
af051ca4
PM
218/* Record grace-period phase and time. */
219static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
220{
221 rtp->gp_state = newstate;
222 rtp->gp_jiffies = jiffies;
223}
224
8344496e 225#ifndef CONFIG_TINY_RCU
af051ca4
PM
226/* Return state name. */
227static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
228{
229 int i = data_race(rtp->gp_state); // Let KCSAN detect update races
230 int j = READ_ONCE(i); // Prevent the compiler from reading twice
231
232 if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
233 return "???";
234 return rcu_tasks_gp_state_names[j];
235}
8344496e 236#endif /* #ifndef CONFIG_TINY_RCU */
af051ca4 237
cafafd67 238// Initialize per-CPU callback lists for the specified flavor of
cb88f7f5 239// Tasks RCU. Do not enqueue callbacks before this function is invoked.
cafafd67
PM
240static void cblist_init_generic(struct rcu_tasks *rtp)
241{
242 int cpu;
243 unsigned long flags;
8610b656 244 int lim;
da123016 245 int shift;
cafafd67 246
ab97152f
PM
247 if (rcu_task_enqueue_lim < 0) {
248 rcu_task_enqueue_lim = 1;
249 rcu_task_cb_adjust = true;
ab97152f 250 } else if (rcu_task_enqueue_lim == 0) {
8610b656 251 rcu_task_enqueue_lim = 1;
ab97152f 252 }
8610b656
PM
253 lim = rcu_task_enqueue_lim;
254
255 if (lim > nr_cpu_ids)
256 lim = nr_cpu_ids;
da123016
PM
257 shift = ilog2(nr_cpu_ids / lim);
258 if (((nr_cpu_ids - 1) >> shift) >= lim)
259 shift++;
260 WRITE_ONCE(rtp->percpu_enqueue_shift, shift);
2cee0789 261 WRITE_ONCE(rtp->percpu_dequeue_lim, lim);
8610b656 262 smp_store_release(&rtp->percpu_enqueue_lim, lim);
cafafd67
PM
263 for_each_possible_cpu(cpu) {
264 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
265
266 WARN_ON_ONCE(!rtpcp);
267 if (cpu)
381a4f3b 268 raw_spin_lock_init(&ACCESS_PRIVATE(rtpcp, lock));
cb88f7f5 269 local_irq_save(flags); // serialize initialization
9b073de1
PM
270 if (rcu_segcblist_empty(&rtpcp->cblist))
271 rcu_segcblist_init(&rtpcp->cblist);
cb88f7f5 272 local_irq_restore(flags);
d363f833
PM
273 INIT_WORK(&rtpcp->rtp_work, rcu_tasks_invoke_cbs_wq);
274 rtpcp->cpu = cpu;
275 rtpcp->rtpp = rtp;
434c9eef
PM
276 if (!rtpcp->rtp_blkd_tasks.next)
277 INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
cafafd67 278 }
5fc8cbe4 279
edff5e9a
Z
280 pr_info("%s: Setting shift to %d and lim to %d rcu_task_cb_adjust=%d.\n", rtp->name,
281 data_race(rtp->percpu_enqueue_shift), data_race(rtp->percpu_enqueue_lim), rcu_task_cb_adjust);
cafafd67
PM
282}
283
d119357d
PM
284// Compute wakeup time for lazy callback timer.
285static unsigned long rcu_tasks_lazy_time(struct rcu_tasks *rtp)
286{
287 return jiffies + rtp->lazy_jiffies;
288}
289
290// Timer handler that unlazifies lazy callbacks.
291static void call_rcu_tasks_generic_timer(struct timer_list *tlp)
292{
293 unsigned long flags;
294 bool needwake = false;
295 struct rcu_tasks *rtp;
296 struct rcu_tasks_percpu *rtpcp = from_timer(rtpcp, tlp, lazy_timer);
297
298 rtp = rtpcp->rtpp;
299 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
300 if (!rcu_segcblist_empty(&rtpcp->cblist) && rtp->lazy_jiffies) {
301 if (!rtpcp->urgent_gp)
302 rtpcp->urgent_gp = 1;
303 needwake = true;
304 mod_timer(&rtpcp->lazy_timer, rcu_tasks_lazy_time(rtp));
305 }
306 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
307 if (needwake)
308 rcuwait_wake_up(&rtp->cbs_wait);
309}
310
3063b33a
PM
311// IRQ-work handler that does deferred wakeup for call_rcu_tasks_generic().
312static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp)
313{
314 struct rcu_tasks *rtp;
315 struct rcu_tasks_percpu *rtpcp = container_of(iwp, struct rcu_tasks_percpu, rtp_irq_work);
316
317 rtp = rtpcp->rtpp;
88db792b 318 rcuwait_wake_up(&rtp->cbs_wait);
3063b33a
PM
319}
320
5873b8a9
PM
321// Enqueue a callback for the specified flavor of Tasks RCU.
322static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
323 struct rcu_tasks *rtp)
eacd6f04 324{
07d95c34 325 int chosen_cpu;
eacd6f04 326 unsigned long flags;
d119357d 327 bool havekthread = smp_load_acquire(&rtp->kthread_ptr);
07d95c34 328 int ideal_cpu;
7d13d30b 329 unsigned long j;
ab97152f 330 bool needadjust = false;
eacd6f04 331 bool needwake;
cafafd67 332 struct rcu_tasks_percpu *rtpcp;
eacd6f04
PM
333
334 rhp->next = NULL;
335 rhp->func = func;
cafafd67 336 local_irq_save(flags);
fd796e41 337 rcu_read_lock();
07d95c34
ED
338 ideal_cpu = smp_processor_id() >> READ_ONCE(rtp->percpu_enqueue_shift);
339 chosen_cpu = cpumask_next(ideal_cpu - 1, cpu_possible_mask);
340 rtpcp = per_cpu_ptr(rtp->rtpcpu, chosen_cpu);
7d13d30b
PM
341 if (!raw_spin_trylock_rcu_node(rtpcp)) { // irqs already disabled.
342 raw_spin_lock_rcu_node(rtpcp); // irqs already disabled.
343 j = jiffies;
344 if (rtpcp->rtp_jiffies != j) {
345 rtpcp->rtp_jiffies = j;
346 rtpcp->rtp_n_lock_retries = 0;
347 }
ab97152f
PM
348 if (rcu_task_cb_adjust && ++rtpcp->rtp_n_lock_retries > rcu_task_contend_lim &&
349 READ_ONCE(rtp->percpu_enqueue_lim) != nr_cpu_ids)
350 needadjust = true; // Defer adjustment to avoid deadlock.
7d13d30b 351 }
cb88f7f5
PM
352 // Queuing callbacks before initialization not yet supported.
353 if (WARN_ON_ONCE(!rcu_segcblist_is_enabled(&rtpcp->cblist)))
354 rcu_segcblist_init(&rtpcp->cblist);
db13710a
PM
355 needwake = (func == wakeme_after_rcu) ||
356 (rcu_segcblist_n_cbs(&rtpcp->cblist) == rcu_task_lazy_lim);
357 if (havekthread && !needwake && !timer_pending(&rtpcp->lazy_timer)) {
d119357d
PM
358 if (rtp->lazy_jiffies)
359 mod_timer(&rtpcp->lazy_timer, rcu_tasks_lazy_time(rtp));
360 else
361 needwake = rcu_segcblist_empty(&rtpcp->cblist);
362 }
363 if (needwake)
364 rtpcp->urgent_gp = 3;
9b073de1 365 rcu_segcblist_enqueue(&rtpcp->cblist, rhp);
381a4f3b 366 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
ab97152f
PM
367 if (unlikely(needadjust)) {
368 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
369 if (rtp->percpu_enqueue_lim != nr_cpu_ids) {
00a8b4b5 370 WRITE_ONCE(rtp->percpu_enqueue_shift, 0);
fd796e41 371 WRITE_ONCE(rtp->percpu_dequeue_lim, nr_cpu_ids);
ab97152f
PM
372 smp_store_release(&rtp->percpu_enqueue_lim, nr_cpu_ids);
373 pr_info("Switching %s to per-CPU callback queuing.\n", rtp->name);
374 }
375 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
376 }
fd796e41 377 rcu_read_unlock();
eacd6f04 378 /* We can't create the thread unless interrupts are enabled. */
07e10515 379 if (needwake && READ_ONCE(rtp->kthread_ptr))
3063b33a 380 irq_work_queue(&rtpcp->rtp_irq_work);
eacd6f04 381}
eacd6f04 382
ce9b1c66
PM
383// RCU callback function for rcu_barrier_tasks_generic().
384static void rcu_barrier_tasks_generic_cb(struct rcu_head *rhp)
385{
386 struct rcu_tasks *rtp;
387 struct rcu_tasks_percpu *rtpcp;
388
389 rtpcp = container_of(rhp, struct rcu_tasks_percpu, barrier_q_head);
390 rtp = rtpcp->rtpp;
391 if (atomic_dec_and_test(&rtp->barrier_q_count))
392 complete(&rtp->barrier_q_completion);
393}
394
395// Wait for all in-flight callbacks for the specified RCU Tasks flavor.
396// Operates in a manner similar to rcu_barrier().
397static void rcu_barrier_tasks_generic(struct rcu_tasks *rtp)
398{
399 int cpu;
400 unsigned long flags;
401 struct rcu_tasks_percpu *rtpcp;
402 unsigned long s = rcu_seq_snap(&rtp->barrier_q_seq);
403
404 mutex_lock(&rtp->barrier_q_mutex);
405 if (rcu_seq_done(&rtp->barrier_q_seq, s)) {
406 smp_mb();
407 mutex_unlock(&rtp->barrier_q_mutex);
408 return;
409 }
410 rcu_seq_start(&rtp->barrier_q_seq);
411 init_completion(&rtp->barrier_q_completion);
412 atomic_set(&rtp->barrier_q_count, 2);
413 for_each_possible_cpu(cpu) {
2cee0789 414 if (cpu >= smp_load_acquire(&rtp->percpu_dequeue_lim))
ce9b1c66
PM
415 break;
416 rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
417 rtpcp->barrier_q_head.func = rcu_barrier_tasks_generic_cb;
418 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
419 if (rcu_segcblist_entrain(&rtpcp->cblist, &rtpcp->barrier_q_head))
420 atomic_inc(&rtp->barrier_q_count);
421 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
422 }
423 if (atomic_sub_and_test(2, &rtp->barrier_q_count))
424 complete(&rtp->barrier_q_completion);
425 wait_for_completion(&rtp->barrier_q_completion);
426 rcu_seq_end(&rtp->barrier_q_seq);
427 mutex_unlock(&rtp->barrier_q_mutex);
428}
429
4d1114c0
PM
430// Advance callbacks and indicate whether either a grace period or
431// callback invocation is needed.
432static int rcu_tasks_need_gpcb(struct rcu_tasks *rtp)
433{
434 int cpu;
435 unsigned long flags;
a4fcfbee 436 bool gpdone = poll_state_synchronize_rcu(rtp->percpu_dequeue_gpseq);
fd796e41
PM
437 long n;
438 long ncbs = 0;
439 long ncbsnz = 0;
4d1114c0
PM
440 int needgpcb = 0;
441
2cee0789 442 for (cpu = 0; cpu < smp_load_acquire(&rtp->percpu_dequeue_lim); cpu++) {
4d1114c0
PM
443 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
444
445 /* Advance and accelerate any new callbacks. */
fd796e41 446 if (!rcu_segcblist_n_cbs(&rtpcp->cblist))
4d1114c0
PM
447 continue;
448 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
fd796e41
PM
449 // Should we shrink down to a single callback queue?
450 n = rcu_segcblist_n_cbs(&rtpcp->cblist);
451 if (n) {
452 ncbs += n;
453 if (cpu > 0)
454 ncbsnz += n;
455 }
4d1114c0
PM
456 rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
457 (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
d119357d
PM
458 if (rtpcp->urgent_gp > 0 && rcu_segcblist_pend_cbs(&rtpcp->cblist)) {
459 if (rtp->lazy_jiffies)
460 rtpcp->urgent_gp--;
4d1114c0 461 needgpcb |= 0x3;
d119357d
PM
462 } else if (rcu_segcblist_empty(&rtpcp->cblist)) {
463 rtpcp->urgent_gp = 0;
464 }
465 if (rcu_segcblist_ready_cbs(&rtpcp->cblist))
4d1114c0
PM
466 needgpcb |= 0x1;
467 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
468 }
fd796e41
PM
469
470 // Shrink down to a single callback queue if appropriate.
471 // This is done in two stages: (1) If there are no more than
472 // rcu_task_collapse_lim callbacks on CPU 0 and none on any other
473 // CPU, limit enqueueing to CPU 0. (2) After an RCU grace period,
474 // if there has not been an increase in callbacks, limit dequeuing
475 // to CPU 0. Note the matching RCU read-side critical section in
476 // call_rcu_tasks_generic().
477 if (rcu_task_cb_adjust && ncbs <= rcu_task_collapse_lim) {
478 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
479 if (rtp->percpu_enqueue_lim > 1) {
2bcd18e0 480 WRITE_ONCE(rtp->percpu_enqueue_shift, order_base_2(nr_cpu_ids));
fd796e41
PM
481 smp_store_release(&rtp->percpu_enqueue_lim, 1);
482 rtp->percpu_dequeue_gpseq = get_state_synchronize_rcu();
a4fcfbee 483 gpdone = false;
fd796e41
PM
484 pr_info("Starting switch %s to CPU-0 callback queuing.\n", rtp->name);
485 }
486 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
487 }
a4fcfbee 488 if (rcu_task_cb_adjust && !ncbsnz && gpdone) {
fd796e41
PM
489 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
490 if (rtp->percpu_enqueue_lim < rtp->percpu_dequeue_lim) {
491 WRITE_ONCE(rtp->percpu_dequeue_lim, 1);
492 pr_info("Completing switch %s to CPU-0 callback queuing.\n", rtp->name);
493 }
a4fcfbee
Z
494 if (rtp->percpu_dequeue_lim == 1) {
495 for (cpu = rtp->percpu_dequeue_lim; cpu < nr_cpu_ids; cpu++) {
496 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
4cf0585c 497
a4fcfbee
Z
498 WARN_ON_ONCE(rcu_segcblist_n_cbs(&rtpcp->cblist));
499 }
4cf0585c 500 }
fd796e41
PM
501 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
502 }
503
4d1114c0
PM
504 return needgpcb;
505}
506
57881863 507// Advance callbacks and invoke any that are ready.
d363f833 508static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu *rtpcp)
eacd6f04 509{
57881863 510 int cpu;
d363f833 511 int cpunext;
401b0de3 512 int cpuwq;
eacd6f04 513 unsigned long flags;
9b073de1 514 int len;
9b073de1 515 struct rcu_head *rhp;
d363f833
PM
516 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
517 struct rcu_tasks_percpu *rtpcp_next;
518
519 cpu = rtpcp->cpu;
520 cpunext = cpu * 2 + 1;
2cee0789 521 if (cpunext < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
d363f833 522 rtpcp_next = per_cpu_ptr(rtp->rtpcpu, cpunext);
401b0de3
PM
523 cpuwq = rcu_cpu_beenfullyonline(cpunext) ? cpunext : WORK_CPU_UNBOUND;
524 queue_work_on(cpuwq, system_wq, &rtpcp_next->rtp_work);
d363f833 525 cpunext++;
2cee0789 526 if (cpunext < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
d363f833 527 rtpcp_next = per_cpu_ptr(rtp->rtpcpu, cpunext);
401b0de3
PM
528 cpuwq = rcu_cpu_beenfullyonline(cpunext) ? cpunext : WORK_CPU_UNBOUND;
529 queue_work_on(cpuwq, system_wq, &rtpcp_next->rtp_work);
57881863 530 }
57881863 531 }
d363f833 532
ab2756ea 533 if (rcu_segcblist_empty(&rtpcp->cblist) || !cpu_possible(cpu))
d363f833
PM
534 return;
535 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
536 rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
537 rcu_segcblist_extract_done_cbs(&rtpcp->cblist, &rcl);
538 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
539 len = rcl.len;
540 for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
541 local_bh_disable();
542 rhp->func(rhp);
543 local_bh_enable();
544 cond_resched();
545 }
546 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
547 rcu_segcblist_add_len(&rtpcp->cblist, -len);
548 (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
549 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
550}
551
552// Workqueue flood to advance callbacks and invoke any that are ready.
553static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp)
554{
555 struct rcu_tasks *rtp;
556 struct rcu_tasks_percpu *rtpcp = container_of(wp, struct rcu_tasks_percpu, rtp_work);
557
558 rtp = rtpcp->rtpp;
559 rcu_tasks_invoke_cbs(rtp, rtpcp);
57881863
PM
560}
561
d96225fd 562// Wait for one grace period.
4a8cc433 563static void rcu_tasks_one_gp(struct rcu_tasks *rtp, bool midboot)
57881863
PM
564{
565 int needgpcb;
d96225fd
PM
566
567 mutex_lock(&rtp->tasks_gp_mutex);
d96225fd
PM
568
569 // If there were none, wait a bit and start over.
4a8cc433
PM
570 if (unlikely(midboot)) {
571 needgpcb = 0x2;
572 } else {
573 set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
574 rcuwait_wait_event(&rtp->cbs_wait,
575 (needgpcb = rcu_tasks_need_gpcb(rtp)),
576 TASK_IDLE);
577 }
d96225fd
PM
578
579 if (needgpcb & 0x2) {
580 // Wait for one grace period.
581 set_tasks_gp_state(rtp, RTGS_WAIT_GP);
582 rtp->gp_start = jiffies;
583 rcu_seq_start(&rtp->tasks_gp_seq);
584 rtp->gp_func(rtp);
585 rcu_seq_end(&rtp->tasks_gp_seq);
586 }
587
588 // Invoke callbacks.
589 set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
590 rcu_tasks_invoke_cbs(rtp, per_cpu_ptr(rtp->rtpcpu, 0));
591 mutex_unlock(&rtp->tasks_gp_mutex);
592}
593
594// RCU-tasks kthread that detects grace periods and invokes callbacks.
595static int __noreturn rcu_tasks_kthread(void *arg)
596{
d119357d 597 int cpu;
07e10515 598 struct rcu_tasks *rtp = arg;
eacd6f04 599
d119357d
PM
600 for_each_possible_cpu(cpu) {
601 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
602
603 timer_setup(&rtpcp->lazy_timer, call_rcu_tasks_generic_timer, 0);
604 rtpcp->urgent_gp = 1;
605 }
606
eacd6f04 607 /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
04d4e665 608 housekeeping_affine(current, HK_TYPE_RCU);
d119357d 609 smp_store_release(&rtp->kthread_ptr, current); // Let GPs start!
eacd6f04
PM
610
611 /*
612 * Each pass through the following loop makes one check for
613 * newly arrived callbacks, and, if there are some, waits for
614 * one RCU-tasks grace period and then invokes the callbacks.
615 * This loop is terminated by the system going down. ;-)
616 */
617 for (;;) {
d96225fd
PM
618 // Wait for one grace period and invoke any callbacks
619 // that are ready.
4a8cc433 620 rcu_tasks_one_gp(rtp, false);
57881863 621
d96225fd 622 // Paranoid sleep to keep this from entering a tight loop.
4fe192df 623 schedule_timeout_idle(rtp->gp_sleep);
eacd6f04
PM
624 }
625}
626
68cb4720
PM
627// Wait for a grace period for the specified flavor of Tasks RCU.
628static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
629{
630 /* Complain if the scheduler has not started. */
ea5c8987
Z
631 if (WARN_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
632 "synchronize_%s() called too soon", rtp->name))
633 return;
68cb4720 634
4a8cc433
PM
635 // If the grace-period kthread is running, use it.
636 if (READ_ONCE(rtp->kthread_ptr)) {
637 wait_rcu_gp(rtp->call_func);
638 return;
639 }
640 rcu_tasks_one_gp(rtp, true);
68cb4720
PM
641}
642
1b04fa99 643/* Spawn RCU-tasks grace-period kthread. */
5873b8a9 644static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
eacd6f04
PM
645{
646 struct task_struct *t;
647
c97d12a6
PM
648 t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
649 if (WARN_ONCE(IS_ERR(t), "%s: Could not start %s grace-period kthread, OOM is now expected behavior\n", __func__, rtp->name))
5873b8a9 650 return;
eacd6f04 651 smp_mb(); /* Ensure others see full kthread. */
eacd6f04 652}
eacd6f04 653
eacd6f04
PM
654#ifndef CONFIG_TINY_RCU
655
656/*
657 * Print any non-default Tasks RCU settings.
658 */
659static void __init rcu_tasks_bootup_oddness(void)
660{
d5f177d3 661#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
f2539003
PM
662 int rtsimc;
663
eacd6f04
PM
664 if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
665 pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
f2539003
PM
666 rtsimc = clamp(rcu_task_stall_info_mult, 1, 10);
667 if (rtsimc != rcu_task_stall_info_mult) {
668 pr_info("\tTasks-RCU CPU stall info multiplier clamped to %d (rcu_task_stall_info_mult).\n", rtsimc);
669 rcu_task_stall_info_mult = rtsimc;
670 }
d5f177d3
PM
671#endif /* #ifdef CONFIG_TASKS_RCU */
672#ifdef CONFIG_TASKS_RCU
673 pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
eacd6f04 674#endif /* #ifdef CONFIG_TASKS_RCU */
c84aad76
PM
675#ifdef CONFIG_TASKS_RUDE_RCU
676 pr_info("\tRude variant of Tasks RCU enabled.\n");
677#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
d5f177d3
PM
678#ifdef CONFIG_TASKS_TRACE_RCU
679 pr_info("\tTracing variant of Tasks RCU enabled.\n");
680#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
eacd6f04
PM
681}
682
683#endif /* #ifndef CONFIG_TINY_RCU */
5873b8a9 684
8344496e 685#ifndef CONFIG_TINY_RCU
e21408ce
PM
686/* Dump out rcutorture-relevant state common to all RCU-tasks flavors. */
687static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
688{
10b3742f
PM
689 int cpu;
690 bool havecbs = false;
d119357d
PM
691 bool haveurgent = false;
692 bool haveurgentcbs = false;
10b3742f
PM
693
694 for_each_possible_cpu(cpu) {
695 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
696
d119357d 697 if (!data_race(rcu_segcblist_empty(&rtpcp->cblist)))
10b3742f 698 havecbs = true;
d119357d
PM
699 if (data_race(rtpcp->urgent_gp))
700 haveurgent = true;
701 if (!data_race(rcu_segcblist_empty(&rtpcp->cblist)) && data_race(rtpcp->urgent_gp))
702 haveurgentcbs = true;
703 if (havecbs && haveurgent && haveurgentcbs)
10b3742f 704 break;
10b3742f 705 }
d119357d 706 pr_info("%s: %s(%d) since %lu g:%lu i:%lu/%lu %c%c%c%c l:%lu %s\n",
e21408ce 707 rtp->kname,
7e0669c3 708 tasks_gp_state_getname(rtp), data_race(rtp->gp_state),
af051ca4 709 jiffies - data_race(rtp->gp_jiffies),
b14fb4fb 710 data_race(rcu_seq_current(&rtp->tasks_gp_seq)),
7e0669c3 711 data_race(rtp->n_ipis_fails), data_race(rtp->n_ipis),
e21408ce 712 ".k"[!!data_race(rtp->kthread_ptr)],
10b3742f 713 ".C"[havecbs],
d119357d
PM
714 ".u"[haveurgent],
715 ".U"[haveurgentcbs],
716 rtp->lazy_jiffies,
e21408ce
PM
717 s);
718}
27c0f144 719#endif // #ifndef CONFIG_TINY_RCU
e21408ce 720
25246fc8
PM
721static void exit_tasks_rcu_finish_trace(struct task_struct *t);
722
723#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
5873b8a9 724
d01aa263
PM
725////////////////////////////////////////////////////////////////////////
726//
727// Shared code between task-list-scanning variants of Tasks RCU.
728
729/* Wait for one RCU-tasks grace period. */
730static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
731{
f2539003 732 struct task_struct *g;
d01aa263 733 int fract;
f2539003
PM
734 LIST_HEAD(holdouts);
735 unsigned long j;
736 unsigned long lastinfo;
737 unsigned long lastreport;
738 bool reported = false;
739 int rtsi;
740 struct task_struct *t;
d01aa263 741
af051ca4 742 set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
7460ade1 743 rtp->pregp_func(&holdouts);
d01aa263
PM
744
745 /*
746 * There were callbacks, so we need to wait for an RCU-tasks
747 * grace period. Start off by scanning the task list for tasks
748 * that are not already voluntarily blocked. Mark these tasks
749 * and make a list of them in holdouts.
750 */
af051ca4 751 set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
1a4a8153
PM
752 if (rtp->pertask_func) {
753 rcu_read_lock();
754 for_each_process_thread(g, t)
755 rtp->pertask_func(t, &holdouts);
756 rcu_read_unlock();
757 }
d01aa263 758
af051ca4 759 set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
9796e1ae 760 rtp->postscan_func(&holdouts);
d01aa263
PM
761
762 /*
763 * Each pass through the following loop scans the list of holdout
764 * tasks, removing any that are no longer holdouts. When the list
765 * is empty, we are done.
766 */
767 lastreport = jiffies;
f2539003
PM
768 lastinfo = lastreport;
769 rtsi = READ_ONCE(rcu_task_stall_info);
d01aa263 770
2393a613
PM
771 // Start off with initial wait and slowly back off to 1 HZ wait.
772 fract = rtp->init_fract;
d01aa263 773
77dc1741 774 while (!list_empty(&holdouts)) {
777570d9 775 ktime_t exp;
d01aa263
PM
776 bool firstreport;
777 bool needreport;
778 int rtst;
779
f2539003 780 // Slowly back off waiting for holdouts
af051ca4 781 set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
bddf7122
PM
782 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
783 schedule_timeout_idle(fract);
784 } else {
785 exp = jiffies_to_nsecs(fract);
786 __set_current_state(TASK_IDLE);
787 schedule_hrtimeout_range(&exp, jiffies_to_nsecs(HZ / 2), HRTIMER_MODE_REL_HARD);
788 }
d01aa263 789
75dc2da5
PM
790 if (fract < HZ)
791 fract++;
d01aa263
PM
792
793 rtst = READ_ONCE(rcu_task_stall_timeout);
794 needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
f2539003 795 if (needreport) {
d01aa263 796 lastreport = jiffies;
f2539003
PM
797 reported = true;
798 }
d01aa263
PM
799 firstreport = true;
800 WARN_ON(signal_pending(current));
af051ca4 801 set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
d01aa263 802 rtp->holdouts_func(&holdouts, needreport, &firstreport);
f2539003
PM
803
804 // Print pre-stall informational messages if needed.
805 j = jiffies;
806 if (rtsi > 0 && !reported && time_after(j, lastinfo + rtsi)) {
807 lastinfo = j;
808 rtsi = rtsi * rcu_task_stall_info_mult;
df83fff7 809 pr_info("%s: %s grace period number %lu (since boot) is %lu jiffies old.\n",
f2539003
PM
810 __func__, rtp->kname, rtp->tasks_gp_seq, j - rtp->gp_start);
811 }
d01aa263
PM
812 }
813
af051ca4
PM
814 set_tasks_gp_state(rtp, RTGS_POST_GP);
815 rtp->postgp_func(rtp);
d01aa263
PM
816}
817
25246fc8
PM
818#endif /* #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU) */
819
820#ifdef CONFIG_TASKS_RCU
821
5873b8a9
PM
822////////////////////////////////////////////////////////////////////////
823//
824// Simple variant of RCU whose quiescent states are voluntary context
8af9e2c7 825// switch, cond_resched_tasks_rcu_qs(), user-space execution, and idle.
5873b8a9
PM
826// As such, grace periods can take one good long time. There are no
827// read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
828// because this implementation is intended to get the system into a safe
829// state for some of the manipulations involved in tracing and the like.
830// Finally, this implementation does not support high call_rcu_tasks()
831// rates from multiple CPUs. If this is required, per-CPU callback lists
832// will be needed.
06a3ec92
PM
833//
834// The implementation uses rcu_tasks_wait_gp(), which relies on function
835// pointers in the rcu_tasks structure. The rcu_spawn_tasks_kthread()
836// function sets these function pointers up so that rcu_tasks_wait_gp()
837// invokes these functions in this order:
838//
839// rcu_tasks_pregp_step():
840// Invokes synchronize_rcu() in order to wait for all in-flight
841// t->on_rq and t->nvcsw transitions to complete. This works because
842// all such transitions are carried out with interrupts disabled.
843// rcu_tasks_pertask(), invoked on every non-idle task:
844// For every runnable non-idle task other than the current one, use
845// get_task_struct() to pin down that task, snapshot that task's
846// number of voluntary context switches, and add that task to the
847// holdout list.
848// rcu_tasks_postscan():
849// Invoke synchronize_srcu() to ensure that all tasks that were
850// in the process of exiting (and which thus might not know to
851// synchronize with this RCU Tasks grace period) have completed
852// exiting.
853// check_all_holdout_tasks(), repeatedly until holdout list is empty:
854// Scans the holdout list, attempting to identify a quiescent state
855// for each task on the list. If there is a quiescent state, the
856// corresponding task is removed from the holdout list.
857// rcu_tasks_postgp():
858// Invokes synchronize_rcu() in order to ensure that all prior
859// t->on_rq and t->nvcsw transitions are seen by all CPUs and tasks
860// to have happened before the end of this RCU Tasks grace period.
861// Again, this works because all such transitions are carried out
862// with interrupts disabled.
863//
864// For each exiting task, the exit_tasks_rcu_start() and
865// exit_tasks_rcu_finish() functions begin and end, respectively, the SRCU
866// read-side critical sections waited for by rcu_tasks_postscan().
867//
381a4f3b
PM
868// Pre-grace-period update-side code is ordered before the grace
869// via the raw_spin_lock.*rcu_node(). Pre-grace-period read-side code
870// is ordered before the grace period via synchronize_rcu() call in
871// rcu_tasks_pregp_step() and by the scheduler's locks and interrupt
06a3ec92 872// disabling.
5873b8a9 873
e4fe5dd6 874/* Pre-grace-period preparation. */
7460ade1 875static void rcu_tasks_pregp_step(struct list_head *hop)
e4fe5dd6
PM
876{
877 /*
878 * Wait for all pre-existing t->on_rq and t->nvcsw transitions
879 * to complete. Invoking synchronize_rcu() suffices because all
880 * these transitions occur with interrupts disabled. Without this
881 * synchronize_rcu(), a read-side critical section that started
882 * before the grace period might be incorrectly seen as having
883 * started after the grace period.
884 *
885 * This synchronize_rcu() also dispenses with the need for a
886 * memory barrier on the first store to t->rcu_tasks_holdout,
887 * as it forces the store to happen after the beginning of the
888 * grace period.
889 */
890 synchronize_rcu();
891}
892
893/* Per-task initial processing. */
894static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
895{
896 if (t != current && READ_ONCE(t->on_rq) && !is_idle_task(t)) {
897 get_task_struct(t);
898 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
899 WRITE_ONCE(t->rcu_tasks_holdout, true);
900 list_add(&t->rcu_tasks_holdout_list, hop);
901 }
902}
903
904/* Processing between scanning taskslist and draining the holdout list. */
04a3c5aa 905static void rcu_tasks_postscan(struct list_head *hop)
e4fe5dd6 906{
a4533cc0
NU
907 int rtsi = READ_ONCE(rcu_task_stall_info);
908
909 if (!IS_ENABLED(CONFIG_TINY_RCU)) {
910 tasks_rcu_exit_srcu_stall_timer.expires = jiffies + rtsi;
911 add_timer(&tasks_rcu_exit_srcu_stall_timer);
912 }
913
e4fe5dd6 914 /*
e4e1e808
FW
915 * Exiting tasks may escape the tasklist scan. Those are vulnerable
916 * until their final schedule() with TASK_DEAD state. To cope with
917 * this, divide the fragile exit path part in two intersecting
918 * read side critical sections:
919 *
920 * 1) An _SRCU_ read side starting before calling exit_notify(),
921 * which may remove the task from the tasklist, and ending after
922 * the final preempt_disable() call in do_exit().
923 *
924 * 2) An _RCU_ read side starting with the final preempt_disable()
925 * call in do_exit() and ending with the final call to schedule()
926 * with TASK_DEAD state.
927 *
928 * This handles the part 1). And postgp will handle part 2) with a
929 * call to synchronize_rcu().
e4fe5dd6
PM
930 */
931 synchronize_srcu(&tasks_rcu_exit_srcu);
a4533cc0
NU
932
933 if (!IS_ENABLED(CONFIG_TINY_RCU))
934 del_timer_sync(&tasks_rcu_exit_srcu_stall_timer);
e4fe5dd6
PM
935}
936
5873b8a9
PM
937/* See if tasks are still holding out, complain if so. */
938static void check_holdout_task(struct task_struct *t,
939 bool needreport, bool *firstreport)
940{
941 int cpu;
942
943 if (!READ_ONCE(t->rcu_tasks_holdout) ||
944 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
945 !READ_ONCE(t->on_rq) ||
946 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
947 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
948 WRITE_ONCE(t->rcu_tasks_holdout, false);
949 list_del_init(&t->rcu_tasks_holdout_list);
950 put_task_struct(t);
951 return;
952 }
953 rcu_request_urgent_qs_task(t);
954 if (!needreport)
955 return;
956 if (*firstreport) {
957 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
958 *firstreport = false;
959 }
960 cpu = task_cpu(t);
961 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
962 t, ".I"[is_idle_task(t)],
963 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
964 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
965 t->rcu_tasks_idle_cpu, cpu);
966 sched_show_task(t);
967}
968
e4fe5dd6
PM
969/* Scan the holdout lists for tasks no longer holding out. */
970static void check_all_holdout_tasks(struct list_head *hop,
971 bool needreport, bool *firstreport)
972{
973 struct task_struct *t, *t1;
974
975 list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
976 check_holdout_task(t, needreport, firstreport);
977 cond_resched();
978 }
979}
980
981/* Finish off the Tasks-RCU grace period. */
af051ca4 982static void rcu_tasks_postgp(struct rcu_tasks *rtp)
e4fe5dd6
PM
983{
984 /*
985 * Because ->on_rq and ->nvcsw are not guaranteed to have a full
986 * memory barriers prior to them in the schedule() path, memory
987 * reordering on other CPUs could cause their RCU-tasks read-side
988 * critical sections to extend past the end of the grace period.
989 * However, because these ->nvcsw updates are carried out with
990 * interrupts disabled, we can use synchronize_rcu() to force the
991 * needed ordering on all such CPUs.
992 *
993 * This synchronize_rcu() also confines all ->rcu_tasks_holdout
994 * accesses to be within the grace period, avoiding the need for
995 * memory barriers for ->rcu_tasks_holdout accesses.
996 *
997 * In addition, this synchronize_rcu() waits for exiting tasks
998 * to complete their final preempt_disable() region of execution,
e4e1e808
FW
999 * cleaning up after synchronize_srcu(&tasks_rcu_exit_srcu),
1000 * enforcing the whole region before tasklist removal until
1001 * the final schedule() with TASK_DEAD state to be an RCU TASKS
1002 * read side critical section.
e4fe5dd6
PM
1003 */
1004 synchronize_rcu();
1005}
1006
5873b8a9 1007void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
c97d12a6 1008DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
5873b8a9 1009
a4533cc0
NU
1010static void tasks_rcu_exit_srcu_stall(struct timer_list *unused)
1011{
1012#ifndef CONFIG_TINY_RCU
1013 int rtsi;
1014
1015 rtsi = READ_ONCE(rcu_task_stall_info);
1016 pr_info("%s: %s grace period number %lu (since boot) gp_state: %s is %lu jiffies old.\n",
1017 __func__, rcu_tasks.kname, rcu_tasks.tasks_gp_seq,
1018 tasks_gp_state_getname(&rcu_tasks), jiffies - rcu_tasks.gp_jiffies);
1019 pr_info("Please check any exiting tasks stuck between calls to exit_tasks_rcu_start() and exit_tasks_rcu_finish()\n");
1020 tasks_rcu_exit_srcu_stall_timer.expires = jiffies + rtsi;
1021 add_timer(&tasks_rcu_exit_srcu_stall_timer);
1022#endif // #ifndef CONFIG_TINY_RCU
1023}
1024
5873b8a9
PM
1025/**
1026 * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
1027 * @rhp: structure to be used for queueing the RCU updates.
1028 * @func: actual callback function to be invoked after the grace period
1029 *
1030 * The callback function will be invoked some time after a full grace
1031 * period elapses, in other words after all currently executing RCU
1032 * read-side critical sections have completed. call_rcu_tasks() assumes
1033 * that the read-side critical sections end at a voluntary context
8af9e2c7 1034 * switch (not a preemption!), cond_resched_tasks_rcu_qs(), entry into idle,
5873b8a9
PM
1035 * or transition to usermode execution. As such, there are no read-side
1036 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
1037 * this primitive is intended to determine that all tasks have passed
a616aec9 1038 * through a safe state, not so much for data-structure synchronization.
5873b8a9
PM
1039 *
1040 * See the description of call_rcu() for more detailed information on
1041 * memory ordering guarantees.
1042 */
1043void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
1044{
1045 call_rcu_tasks_generic(rhp, func, &rcu_tasks);
1046}
1047EXPORT_SYMBOL_GPL(call_rcu_tasks);
1048
1049/**
1050 * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
1051 *
1052 * Control will return to the caller some time after a full rcu-tasks
1053 * grace period has elapsed, in other words after all currently
1054 * executing rcu-tasks read-side critical sections have elapsed. These
1055 * read-side critical sections are delimited by calls to schedule(),
1056 * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
1057 * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
1058 *
1059 * This is a very specialized primitive, intended only for a few uses in
1060 * tracing and other situations requiring manipulation of function
1061 * preambles and profiling hooks. The synchronize_rcu_tasks() function
1062 * is not (yet) intended for heavy use from multiple CPUs.
1063 *
1064 * See the description of synchronize_rcu() for more detailed information
1065 * on memory ordering guarantees.
1066 */
1067void synchronize_rcu_tasks(void)
1068{
1069 synchronize_rcu_tasks_generic(&rcu_tasks);
1070}
1071EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
1072
1073/**
1074 * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
1075 *
1076 * Although the current implementation is guaranteed to wait, it is not
1077 * obligated to, for example, if there are no pending callbacks.
1078 */
1079void rcu_barrier_tasks(void)
1080{
ce9b1c66 1081 rcu_barrier_tasks_generic(&rcu_tasks);
5873b8a9
PM
1082}
1083EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
1084
450d461a
PM
1085int rcu_tasks_lazy_ms = -1;
1086module_param(rcu_tasks_lazy_ms, int, 0444);
1087
5873b8a9
PM
1088static int __init rcu_spawn_tasks_kthread(void)
1089{
cafafd67 1090 cblist_init_generic(&rcu_tasks);
4fe192df 1091 rcu_tasks.gp_sleep = HZ / 10;
75dc2da5 1092 rcu_tasks.init_fract = HZ / 10;
450d461a
PM
1093 if (rcu_tasks_lazy_ms >= 0)
1094 rcu_tasks.lazy_jiffies = msecs_to_jiffies(rcu_tasks_lazy_ms);
e4fe5dd6
PM
1095 rcu_tasks.pregp_func = rcu_tasks_pregp_step;
1096 rcu_tasks.pertask_func = rcu_tasks_pertask;
1097 rcu_tasks.postscan_func = rcu_tasks_postscan;
1098 rcu_tasks.holdouts_func = check_all_holdout_tasks;
1099 rcu_tasks.postgp_func = rcu_tasks_postgp;
5873b8a9
PM
1100 rcu_spawn_tasks_kthread_generic(&rcu_tasks);
1101 return 0;
1102}
5873b8a9 1103
27c0f144
PM
1104#if !defined(CONFIG_TINY_RCU)
1105void show_rcu_tasks_classic_gp_kthread(void)
e21408ce
PM
1106{
1107 show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
1108}
27c0f144
PM
1109EXPORT_SYMBOL_GPL(show_rcu_tasks_classic_gp_kthread);
1110#endif // !defined(CONFIG_TINY_RCU)
e21408ce 1111
e4e1e808
FW
1112/*
1113 * Contribute to protect against tasklist scan blind spot while the
1114 * task is exiting and may be removed from the tasklist. See
1115 * corresponding synchronize_srcu() for further details.
1116 */
25246fc8
PM
1117void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
1118{
25246fc8 1119 current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
25246fc8
PM
1120}
1121
e4e1e808
FW
1122/*
1123 * Contribute to protect against tasklist scan blind spot while the
1124 * task is exiting and may be removed from the tasklist. See
1125 * corresponding synchronize_srcu() for further details.
1126 */
28319d6d 1127void exit_tasks_rcu_stop(void) __releases(&tasks_rcu_exit_srcu)
25246fc8
PM
1128{
1129 struct task_struct *t = current;
1130
25246fc8 1131 __srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
28319d6d
FW
1132}
1133
1134/*
1135 * Contribute to protect against tasklist scan blind spot while the
1136 * task is exiting and may be removed from the tasklist. See
1137 * corresponding synchronize_srcu() for further details.
1138 */
1139void exit_tasks_rcu_finish(void)
1140{
1141 exit_tasks_rcu_stop();
1142 exit_tasks_rcu_finish_trace(current);
25246fc8
PM
1143}
1144
e21408ce 1145#else /* #ifdef CONFIG_TASKS_RCU */
25246fc8 1146void exit_tasks_rcu_start(void) { }
28319d6d 1147void exit_tasks_rcu_stop(void) { }
25246fc8 1148void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
e21408ce 1149#endif /* #else #ifdef CONFIG_TASKS_RCU */
c84aad76
PM
1150
1151#ifdef CONFIG_TASKS_RUDE_RCU
1152
1153////////////////////////////////////////////////////////////////////////
1154//
1155// "Rude" variant of Tasks RCU, inspired by Steve Rostedt's trick of
1156// passing an empty function to schedule_on_each_cpu(). This approach
e4be1f44
PM
1157// provides an asynchronous call_rcu_tasks_rude() API and batching of
1158// concurrent calls to the synchronous synchronize_rcu_tasks_rude() API.
9fc98e31
PM
1159// This invokes schedule_on_each_cpu() in order to send IPIs far and wide
1160// and induces otherwise unnecessary context switches on all online CPUs,
1161// whether idle or not.
1162//
1163// Callback handling is provided by the rcu_tasks_kthread() function.
1164//
1165// Ordering is provided by the scheduler's context-switch code.
c84aad76
PM
1166
1167// Empty function to allow workqueues to force a context switch.
1168static void rcu_tasks_be_rude(struct work_struct *work)
1169{
1170}
1171
1172// Wait for one rude RCU-tasks grace period.
1173static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
1174{
238dbce3 1175 rtp->n_ipis += cpumask_weight(cpu_online_mask);
c84aad76
PM
1176 schedule_on_each_cpu(rcu_tasks_be_rude);
1177}
1178
1179void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
c97d12a6
PM
1180DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
1181 "RCU Tasks Rude");
c84aad76
PM
1182
1183/**
1184 * call_rcu_tasks_rude() - Queue a callback rude task-based grace period
1185 * @rhp: structure to be used for queueing the RCU updates.
1186 * @func: actual callback function to be invoked after the grace period
1187 *
1188 * The callback function will be invoked some time after a full grace
1189 * period elapses, in other words after all currently executing RCU
1190 * read-side critical sections have completed. call_rcu_tasks_rude()
1191 * assumes that the read-side critical sections end at context switch,
8af9e2c7 1192 * cond_resched_tasks_rcu_qs(), or transition to usermode execution (as
a6517e9c
NU
1193 * usermode execution is schedulable). As such, there are no read-side
1194 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
1195 * this primitive is intended to determine that all tasks have passed
1196 * through a safe state, not so much for data-structure synchronization.
c84aad76
PM
1197 *
1198 * See the description of call_rcu() for more detailed information on
1199 * memory ordering guarantees.
1200 */
1201void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
1202{
1203 call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
1204}
1205EXPORT_SYMBOL_GPL(call_rcu_tasks_rude);
1206
1207/**
1208 * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
1209 *
1210 * Control will return to the caller some time after a rude rcu-tasks
1211 * grace period has elapsed, in other words after all currently
1212 * executing rcu-tasks read-side critical sections have elapsed. These
1213 * read-side critical sections are delimited by calls to schedule(),
a6517e9c
NU
1214 * cond_resched_tasks_rcu_qs(), userspace execution (which is a schedulable
1215 * context), and (in theory, anyway) cond_resched().
c84aad76
PM
1216 *
1217 * This is a very specialized primitive, intended only for a few uses in
1218 * tracing and other situations requiring manipulation of function preambles
1219 * and profiling hooks. The synchronize_rcu_tasks_rude() function is not
1220 * (yet) intended for heavy use from multiple CPUs.
1221 *
1222 * See the description of synchronize_rcu() for more detailed information
1223 * on memory ordering guarantees.
1224 */
1225void synchronize_rcu_tasks_rude(void)
1226{
1227 synchronize_rcu_tasks_generic(&rcu_tasks_rude);
1228}
1229EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
1230
1231/**
1232 * rcu_barrier_tasks_rude - Wait for in-flight call_rcu_tasks_rude() callbacks.
1233 *
1234 * Although the current implementation is guaranteed to wait, it is not
1235 * obligated to, for example, if there are no pending callbacks.
1236 */
1237void rcu_barrier_tasks_rude(void)
1238{
ce9b1c66 1239 rcu_barrier_tasks_generic(&rcu_tasks_rude);
c84aad76
PM
1240}
1241EXPORT_SYMBOL_GPL(rcu_barrier_tasks_rude);
1242
450d461a
PM
1243int rcu_tasks_rude_lazy_ms = -1;
1244module_param(rcu_tasks_rude_lazy_ms, int, 0444);
1245
c84aad76
PM
1246static int __init rcu_spawn_tasks_rude_kthread(void)
1247{
cafafd67 1248 cblist_init_generic(&rcu_tasks_rude);
4fe192df 1249 rcu_tasks_rude.gp_sleep = HZ / 10;
450d461a
PM
1250 if (rcu_tasks_rude_lazy_ms >= 0)
1251 rcu_tasks_rude.lazy_jiffies = msecs_to_jiffies(rcu_tasks_rude_lazy_ms);
c84aad76
PM
1252 rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
1253 return 0;
1254}
c84aad76 1255
27c0f144
PM
1256#if !defined(CONFIG_TINY_RCU)
1257void show_rcu_tasks_rude_gp_kthread(void)
e21408ce
PM
1258{
1259 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
1260}
27c0f144
PM
1261EXPORT_SYMBOL_GPL(show_rcu_tasks_rude_gp_kthread);
1262#endif // !defined(CONFIG_TINY_RCU)
1263#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
d5f177d3
PM
1264
1265////////////////////////////////////////////////////////////////////////
1266//
1267// Tracing variant of Tasks RCU. This variant is designed to be used
1268// to protect tracing hooks, including those of BPF. This variant
1269// therefore:
1270//
1271// 1. Has explicit read-side markers to allow finite grace periods
1272// in the face of in-kernel loops for PREEMPT=n builds.
1273//
1274// 2. Protects code in the idle loop, exception entry/exit, and
1275// CPU-hotplug code paths, similar to the capabilities of SRCU.
1276//
c4f113ac 1277// 3. Avoids expensive read-side instructions, having overhead similar
d5f177d3
PM
1278// to that of Preemptible RCU.
1279//
eea3423b
PM
1280// There are of course downsides. For example, the grace-period code
1281// can send IPIs to CPUs, even when those CPUs are in the idle loop or
1282// in nohz_full userspace. If needed, these downsides can be at least
1283// partially remedied.
d5f177d3
PM
1284//
1285// Perhaps most important, this variant of RCU does not affect the vanilla
1286// flavors, rcu_preempt and rcu_sched. The fact that RCU Tasks Trace
1287// readers can operate from idle, offline, and exception entry/exit in no
1288// way allows rcu_preempt and rcu_sched readers to also do so.
a434dd10
PM
1289//
1290// The implementation uses rcu_tasks_wait_gp(), which relies on function
1291// pointers in the rcu_tasks structure. The rcu_spawn_tasks_trace_kthread()
1292// function sets these function pointers up so that rcu_tasks_wait_gp()
1293// invokes these functions in this order:
1294//
1295// rcu_tasks_trace_pregp_step():
eea3423b
PM
1296// Disables CPU hotplug, adds all currently executing tasks to the
1297// holdout list, then checks the state of all tasks that blocked
1298// or were preempted within their current RCU Tasks Trace read-side
1299// critical section, adding them to the holdout list if appropriate.
1300// Finally, this function re-enables CPU hotplug.
1301// The ->pertask_func() pointer is NULL, so there is no per-task processing.
a434dd10 1302// rcu_tasks_trace_postscan():
eea3423b
PM
1303// Invokes synchronize_rcu() to wait for late-stage exiting tasks
1304// to finish exiting.
a434dd10
PM
1305// check_all_holdout_tasks_trace(), repeatedly until holdout list is empty:
1306// Scans the holdout list, attempting to identify a quiescent state
1307// for each task on the list. If there is a quiescent state, the
eea3423b
PM
1308// corresponding task is removed from the holdout list. Once this
1309// list is empty, the grace period has completed.
a434dd10 1310// rcu_tasks_trace_postgp():
eea3423b 1311// Provides the needed full memory barrier and does debug checks.
a434dd10
PM
1312//
1313// The exit_tasks_rcu_finish_trace() synchronizes with exiting tasks.
1314//
eea3423b
PM
1315// Pre-grace-period update-side code is ordered before the grace period
1316// via the ->cbs_lock and barriers in rcu_tasks_kthread(). Pre-grace-period
1317// read-side code is ordered before the grace period by atomic operations
1318// on .b.need_qs flag of each task involved in this process, or by scheduler
1319// context-switch ordering (for locked-down non-running readers).
d5f177d3
PM
1320
1321// The lockdep state must be outside of #ifdef to be useful.
1322#ifdef CONFIG_DEBUG_LOCK_ALLOC
1323static struct lock_class_key rcu_lock_trace_key;
1324struct lockdep_map rcu_trace_lock_map =
1325 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_trace", &rcu_lock_trace_key);
1326EXPORT_SYMBOL_GPL(rcu_trace_lock_map);
1327#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
1328
1329#ifdef CONFIG_TASKS_TRACE_RCU
1330
d5f177d3
PM
1331// Record outstanding IPIs to each CPU. No point in sending two...
1332static DEFINE_PER_CPU(bool, trc_ipi_to_cpu);
1333
40471509
PM
1334// The number of detections of task quiescent state relying on
1335// heavyweight readers executing explicit memory barriers.
6731da9e
PM
1336static unsigned long n_heavy_reader_attempts;
1337static unsigned long n_heavy_reader_updates;
1338static unsigned long n_heavy_reader_ofl_updates;
ffcc21a3 1339static unsigned long n_trc_holdouts;
40471509 1340
b0afa0f0
PM
1341void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func);
1342DEFINE_RCU_TASKS(rcu_tasks_trace, rcu_tasks_wait_gp, call_rcu_tasks_trace,
1343 "RCU Tasks Trace");
1344
3847b645
PM
1345/* Load from ->trc_reader_special.b.need_qs with proper ordering. */
1346static u8 rcu_ld_need_qs(struct task_struct *t)
1347{
1348 smp_mb(); // Enforce full grace-period ordering.
1349 return smp_load_acquire(&t->trc_reader_special.b.need_qs);
1350}
1351
1352/* Store to ->trc_reader_special.b.need_qs with proper ordering. */
1353static void rcu_st_need_qs(struct task_struct *t, u8 v)
1354{
1355 smp_store_release(&t->trc_reader_special.b.need_qs, v);
1356 smp_mb(); // Enforce full grace-period ordering.
1357}
1358
1359/*
1360 * Do a cmpxchg() on ->trc_reader_special.b.need_qs, allowing for
1361 * the four-byte operand-size restriction of some platforms.
1362 * Returns the old value, which is often ignored.
1363 */
1364u8 rcu_trc_cmpxchg_need_qs(struct task_struct *t, u8 old, u8 new)
1365{
1366 union rcu_special ret;
1367 union rcu_special trs_old = READ_ONCE(t->trc_reader_special);
1368 union rcu_special trs_new = trs_old;
1369
1370 if (trs_old.b.need_qs != old)
1371 return trs_old.b.need_qs;
1372 trs_new.b.need_qs = new;
1373 ret.s = cmpxchg(&t->trc_reader_special.s, trs_old.s, trs_new.s);
1374 return ret.b.need_qs;
1375}
1376EXPORT_SYMBOL_GPL(rcu_trc_cmpxchg_need_qs);
1377
eea3423b
PM
1378/*
1379 * If we are the last reader, signal the grace-period kthread.
1380 * Also remove from the per-CPU list of blocked tasks.
1381 */
a5c071cc 1382void rcu_read_unlock_trace_special(struct task_struct *t)
d5f177d3 1383{
0bcb3868
PM
1384 unsigned long flags;
1385 struct rcu_tasks_percpu *rtpcp;
1386 union rcu_special trs;
1387
1388 // Open-coded full-word version of rcu_ld_need_qs().
1389 smp_mb(); // Enforce full grace-period ordering.
1390 trs = smp_load_acquire(&t->trc_reader_special);
276c4104 1391
3847b645 1392 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && t->trc_reader_special.b.need_mb)
276c4104
PM
1393 smp_mb(); // Pairs with update-side barriers.
1394 // Update .need_qs before ->trc_reader_nesting for irq/NMI handlers.
0bcb3868 1395 if (trs.b.need_qs == (TRC_NEED_QS_CHECKED | TRC_NEED_QS)) {
3847b645
PM
1396 u8 result = rcu_trc_cmpxchg_need_qs(t, TRC_NEED_QS_CHECKED | TRC_NEED_QS,
1397 TRC_NEED_QS_CHECKED);
1398
0bcb3868
PM
1399 WARN_ONCE(result != trs.b.need_qs, "%s: result = %d", __func__, result);
1400 }
1401 if (trs.b.blocked) {
1402 rtpcp = per_cpu_ptr(rcu_tasks_trace.rtpcpu, t->trc_blkd_cpu);
1403 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1404 list_del_init(&t->trc_blkd_node);
1405 WRITE_ONCE(t->trc_reader_special.b.blocked, false);
1406 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
3847b645 1407 }
a5c071cc 1408 WRITE_ONCE(t->trc_reader_nesting, 0);
d5f177d3
PM
1409}
1410EXPORT_SYMBOL_GPL(rcu_read_unlock_trace_special);
1411
0356d4e6
PM
1412/* Add a newly blocked reader task to its CPU's list. */
1413void rcu_tasks_trace_qs_blkd(struct task_struct *t)
1414{
1415 unsigned long flags;
1416 struct rcu_tasks_percpu *rtpcp;
1417
1418 local_irq_save(flags);
1419 rtpcp = this_cpu_ptr(rcu_tasks_trace.rtpcpu);
1420 raw_spin_lock_rcu_node(rtpcp); // irqs already disabled
1421 t->trc_blkd_cpu = smp_processor_id();
1422 if (!rtpcp->rtp_blkd_tasks.next)
1423 INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
1424 list_add(&t->trc_blkd_node, &rtpcp->rtp_blkd_tasks);
0bcb3868 1425 WRITE_ONCE(t->trc_reader_special.b.blocked, true);
0356d4e6
PM
1426 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1427}
1428EXPORT_SYMBOL_GPL(rcu_tasks_trace_qs_blkd);
1429
d5f177d3
PM
1430/* Add a task to the holdout list, if it is not already on the list. */
1431static void trc_add_holdout(struct task_struct *t, struct list_head *bhp)
1432{
1433 if (list_empty(&t->trc_holdout_list)) {
1434 get_task_struct(t);
1435 list_add(&t->trc_holdout_list, bhp);
ffcc21a3 1436 n_trc_holdouts++;
d5f177d3
PM
1437 }
1438}
1439
1440/* Remove a task from the holdout list, if it is in fact present. */
1441static void trc_del_holdout(struct task_struct *t)
1442{
1443 if (!list_empty(&t->trc_holdout_list)) {
1444 list_del_init(&t->trc_holdout_list);
1445 put_task_struct(t);
ffcc21a3 1446 n_trc_holdouts--;
d5f177d3
PM
1447 }
1448}
1449
1450/* IPI handler to check task state. */
1451static void trc_read_check_handler(void *t_in)
1452{
9ff86b4c 1453 int nesting;
d5f177d3
PM
1454 struct task_struct *t = current;
1455 struct task_struct *texp = t_in;
1456
1457 // If the task is no longer running on this CPU, leave.
3847b645 1458 if (unlikely(texp != t))
d5f177d3 1459 goto reset_ipi; // Already on holdout list, so will check later.
d5f177d3
PM
1460
1461 // If the task is not in a read-side critical section, and
1462 // if this is the last reader, awaken the grace-period kthread.
9ff86b4c
PM
1463 nesting = READ_ONCE(t->trc_reader_nesting);
1464 if (likely(!nesting)) {
3847b645 1465 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
d5f177d3
PM
1466 goto reset_ipi;
1467 }
ba3a86e4 1468 // If we are racing with an rcu_read_unlock_trace(), try again later.
9ff86b4c 1469 if (unlikely(nesting < 0))
ba3a86e4 1470 goto reset_ipi;
d5f177d3 1471
eea3423b
PM
1472 // Get here if the task is in a read-side critical section.
1473 // Set its state so that it will update state for the grace-period
1474 // kthread upon exit from that critical section.
55061126 1475 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS | TRC_NEED_QS_CHECKED);
d5f177d3
PM
1476
1477reset_ipi:
1478 // Allow future IPIs to be sent on CPU and for task.
1479 // Also order this IPI handler against any later manipulations of
1480 // the intended task.
8211e922 1481 smp_store_release(per_cpu_ptr(&trc_ipi_to_cpu, smp_processor_id()), false); // ^^^
d5f177d3
PM
1482 smp_store_release(&texp->trc_ipi_to_cpu, -1); // ^^^
1483}
1484
1485/* Callback function for scheduler to check locked-down task. */
3847b645 1486static int trc_inspect_reader(struct task_struct *t, void *bhp_in)
d5f177d3 1487{
3847b645 1488 struct list_head *bhp = bhp_in;
7d0c9c50 1489 int cpu = task_cpu(t);
18f08e75 1490 int nesting;
7e3b70e0 1491 bool ofl = cpu_is_offline(cpu);
7d0c9c50 1492
897ba84d 1493 if (task_curr(t) && !ofl) {
7d0c9c50 1494 // If no chance of heavyweight readers, do it the hard way.
897ba84d 1495 if (!IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB))
9b3c4ab3 1496 return -EINVAL;
7d0c9c50
PM
1497
1498 // If heavyweight readers are enabled on the remote task,
1499 // we can inspect its state despite its currently running.
1500 // However, we cannot safely change its state.
40471509 1501 n_heavy_reader_attempts++;
897ba84d
PM
1502 // Check for "running" idle tasks on offline CPUs.
1503 if (!rcu_dynticks_zero_in_eqs(cpu, &t->trc_reader_nesting))
9b3c4ab3 1504 return -EINVAL; // No quiescent state, do it the hard way.
40471509 1505 n_heavy_reader_updates++;
18f08e75 1506 nesting = 0;
7d0c9c50 1507 } else {
bdb0cca0 1508 // The task is not running, so C-language access is safe.
18f08e75 1509 nesting = t->trc_reader_nesting;
897ba84d
PM
1510 WARN_ON_ONCE(ofl && task_curr(t) && !is_idle_task(t));
1511 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) && ofl)
1512 n_heavy_reader_ofl_updates++;
7d0c9c50 1513 }
d5f177d3 1514
18f08e75
PM
1515 // If not exiting a read-side critical section, mark as checked
1516 // so that the grace-period kthread will remove it from the
1517 // holdout list.
0968e892
PM
1518 if (!nesting) {
1519 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
1520 return 0; // In QS, so done.
3847b645 1521 }
0968e892 1522 if (nesting < 0)
eea3423b 1523 return -EINVAL; // Reader transitioning, try again later.
7d0c9c50
PM
1524
1525 // The task is in a read-side critical section, so set up its
0968e892
PM
1526 // state so that it will update state upon exit from that critical
1527 // section.
55061126 1528 if (!rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS | TRC_NEED_QS_CHECKED))
3847b645 1529 trc_add_holdout(t, bhp);
9b3c4ab3 1530 return 0;
d5f177d3
PM
1531}
1532
1533/* Attempt to extract the state for the specified task. */
1534static void trc_wait_for_one_reader(struct task_struct *t,
1535 struct list_head *bhp)
1536{
1537 int cpu;
1538
1539 // If a previous IPI is still in flight, let it complete.
1540 if (smp_load_acquire(&t->trc_ipi_to_cpu) != -1) // Order IPI
1541 return;
1542
1543 // The current task had better be in a quiescent state.
1544 if (t == current) {
3847b645 1545 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
bdb0cca0 1546 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
d5f177d3
PM
1547 return;
1548 }
1549
1550 // Attempt to nail down the task for inspection.
1551 get_task_struct(t);
3847b645 1552 if (!task_call_func(t, trc_inspect_reader, bhp)) {
d5f177d3
PM
1553 put_task_struct(t);
1554 return;
1555 }
1556 put_task_struct(t);
1557
45f4b4a2
PM
1558 // If this task is not yet on the holdout list, then we are in
1559 // an RCU read-side critical section. Otherwise, the invocation of
d0a85858 1560 // trc_add_holdout() that added it to the list did the necessary
45f4b4a2
PM
1561 // get_task_struct(). Either way, the task cannot be freed out
1562 // from under this code.
1563
d5f177d3
PM
1564 // If currently running, send an IPI, either way, add to list.
1565 trc_add_holdout(t, bhp);
574de876
PM
1566 if (task_curr(t) &&
1567 time_after(jiffies + 1, rcu_tasks_trace.gp_start + rcu_task_ipi_delay)) {
d5f177d3
PM
1568 // The task is currently running, so try IPIing it.
1569 cpu = task_cpu(t);
1570
1571 // If there is already an IPI outstanding, let it happen.
1572 if (per_cpu(trc_ipi_to_cpu, cpu) || t->trc_ipi_to_cpu >= 0)
1573 return;
1574
d5f177d3
PM
1575 per_cpu(trc_ipi_to_cpu, cpu) = true;
1576 t->trc_ipi_to_cpu = cpu;
238dbce3 1577 rcu_tasks_trace.n_ipis++;
96017bf9 1578 if (smp_call_function_single(cpu, trc_read_check_handler, t, 0)) {
d5f177d3
PM
1579 // Just in case there is some other reason for
1580 // failure than the target CPU being offline.
46aa886c
NU
1581 WARN_ONCE(1, "%s(): smp_call_function_single() failed for CPU: %d\n",
1582 __func__, cpu);
7e0669c3 1583 rcu_tasks_trace.n_ipis_fails++;
d5f177d3 1584 per_cpu(trc_ipi_to_cpu, cpu) = false;
46aa886c 1585 t->trc_ipi_to_cpu = -1;
d5f177d3
PM
1586 }
1587 }
1588}
1589
7460ade1
PM
1590/*
1591 * Initialize for first-round processing for the specified task.
1592 * Return false if task is NULL or already taken care of, true otherwise.
1593 */
1594static bool rcu_tasks_trace_pertask_prep(struct task_struct *t, bool notself)
d5f177d3 1595{
1b04fa99 1596 // During early boot when there is only the one boot CPU, there
19415004
PM
1597 // is no idle task for the other CPUs. Also, the grace-period
1598 // kthread is always in a quiescent state. In addition, just return
1599 // if this task is already on the list.
7460ade1
PM
1600 if (unlikely(t == NULL) || (t == current && notself) || !list_empty(&t->trc_holdout_list))
1601 return false;
1b04fa99 1602
3847b645 1603 rcu_st_need_qs(t, 0);
d5f177d3 1604 t->trc_ipi_to_cpu = -1;
7460ade1
PM
1605 return true;
1606}
1607
1608/* Do first-round processing for the specified task. */
1609static void rcu_tasks_trace_pertask(struct task_struct *t, struct list_head *hop)
1610{
1611 if (rcu_tasks_trace_pertask_prep(t, true))
1612 trc_wait_for_one_reader(t, hop);
1613}
1614
1fa98e2e 1615/* Initialize for a new RCU-tasks-trace grace period. */
7460ade1 1616static void rcu_tasks_trace_pregp_step(struct list_head *hop)
1fa98e2e 1617{
dc7d54b4 1618 LIST_HEAD(blkd_tasks);
1fa98e2e 1619 int cpu;
dc7d54b4
PM
1620 unsigned long flags;
1621 struct rcu_tasks_percpu *rtpcp;
1622 struct task_struct *t;
1fa98e2e
PM
1623
1624 // There shouldn't be any old IPIs, but...
1625 for_each_possible_cpu(cpu)
1626 WARN_ON_ONCE(per_cpu(trc_ipi_to_cpu, cpu));
1627
eea3423b
PM
1628 // Disable CPU hotplug across the CPU scan for the benefit of
1629 // any IPIs that might be needed. This also waits for all readers
1630 // in CPU-hotplug code paths.
1fa98e2e 1631 cpus_read_lock();
7460ade1 1632
eea3423b 1633 // These rcu_tasks_trace_pertask_prep() calls are serialized to
7460ade1 1634 // allow safe access to the hop list.
e386b672
PM
1635 for_each_online_cpu(cpu) {
1636 rcu_read_lock();
1637 t = cpu_curr_snapshot(cpu);
1638 if (rcu_tasks_trace_pertask_prep(t, true))
1639 trc_add_holdout(t, hop);
1640 rcu_read_unlock();
d6ad6063 1641 cond_resched_tasks_rcu_qs();
e386b672 1642 }
dc7d54b4
PM
1643
1644 // Only after all running tasks have been accounted for is it
1645 // safe to take care of the tasks that have blocked within their
1646 // current RCU tasks trace read-side critical section.
1647 for_each_possible_cpu(cpu) {
1648 rtpcp = per_cpu_ptr(rcu_tasks_trace.rtpcpu, cpu);
1649 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1650 list_splice_init(&rtpcp->rtp_blkd_tasks, &blkd_tasks);
1651 while (!list_empty(&blkd_tasks)) {
1652 rcu_read_lock();
1653 t = list_first_entry(&blkd_tasks, struct task_struct, trc_blkd_node);
1654 list_del_init(&t->trc_blkd_node);
1655 list_add(&t->trc_blkd_node, &rtpcp->rtp_blkd_tasks);
1656 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1657 rcu_tasks_trace_pertask(t, hop);
1658 rcu_read_unlock();
1659 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1660 }
1661 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
d6ad6063 1662 cond_resched_tasks_rcu_qs();
dc7d54b4 1663 }
56096ecd
PM
1664
1665 // Re-enable CPU hotplug now that the holdout list is populated.
1666 cpus_read_unlock();
1fa98e2e
PM
1667}
1668
9796e1ae 1669/*
955a0192 1670 * Do intermediate processing between task and holdout scans.
9796e1ae
PM
1671 */
1672static void rcu_tasks_trace_postscan(struct list_head *hop)
d5f177d3
PM
1673{
1674 // Wait for late-stage exiting tasks to finish exiting.
1675 // These might have passed the call to exit_tasks_rcu_finish().
e6c86c51
PM
1676
1677 // If you remove the following line, update rcu_trace_implies_rcu_gp()!!!
d5f177d3 1678 synchronize_rcu();
3847b645
PM
1679 // Any tasks that exit after this point will set
1680 // TRC_NEED_QS_CHECKED in ->trc_reader_special.b.need_qs.
d5f177d3
PM
1681}
1682
65b629e7
NU
1683/* Communicate task state back to the RCU tasks trace stall warning request. */
1684struct trc_stall_chk_rdr {
1685 int nesting;
1686 int ipi_to_cpu;
1687 u8 needqs;
1688};
1689
1690static int trc_check_slow_task(struct task_struct *t, void *arg)
1691{
1692 struct trc_stall_chk_rdr *trc_rdrp = arg;
1693
f90f19da 1694 if (task_curr(t) && cpu_online(task_cpu(t)))
65b629e7
NU
1695 return false; // It is running, so decline to inspect it.
1696 trc_rdrp->nesting = READ_ONCE(t->trc_reader_nesting);
1697 trc_rdrp->ipi_to_cpu = READ_ONCE(t->trc_ipi_to_cpu);
3847b645 1698 trc_rdrp->needqs = rcu_ld_need_qs(t);
65b629e7
NU
1699 return true;
1700}
1701
4593e772
PM
1702/* Show the state of a task stalling the current RCU tasks trace GP. */
1703static void show_stalled_task_trace(struct task_struct *t, bool *firstreport)
1704{
1705 int cpu;
65b629e7
NU
1706 struct trc_stall_chk_rdr trc_rdr;
1707 bool is_idle_tsk = is_idle_task(t);
4593e772
PM
1708
1709 if (*firstreport) {
1710 pr_err("INFO: rcu_tasks_trace detected stalls on tasks:\n");
1711 *firstreport = false;
1712 }
4593e772 1713 cpu = task_cpu(t);
65b629e7 1714 if (!task_call_func(t, trc_check_slow_task, &trc_rdr))
9f3eb5fb 1715 pr_alert("P%d: %c%c\n",
65b629e7 1716 t->pid,
9f3eb5fb 1717 ".I"[t->trc_ipi_to_cpu >= 0],
65b629e7
NU
1718 ".i"[is_idle_tsk]);
1719 else
387c0ad7 1720 pr_alert("P%d: %c%c%c%c nesting: %d%c%c cpu: %d%s\n",
65b629e7
NU
1721 t->pid,
1722 ".I"[trc_rdr.ipi_to_cpu >= 0],
1723 ".i"[is_idle_tsk],
1724 ".N"[cpu >= 0 && tick_nohz_full_cpu(cpu)],
387c0ad7 1725 ".B"[!!data_race(t->trc_reader_special.b.blocked)],
65b629e7 1726 trc_rdr.nesting,
be15a164
PM
1727 " !CN"[trc_rdr.needqs & 0x3],
1728 " ?"[trc_rdr.needqs > 0x3],
c8c03ad9 1729 cpu, cpu_online(cpu) ? "" : "(offline)");
4593e772
PM
1730 sched_show_task(t);
1731}
1732
1733/* List stalled IPIs for RCU tasks trace. */
1734static void show_stalled_ipi_trace(void)
1735{
1736 int cpu;
1737
1738 for_each_possible_cpu(cpu)
1739 if (per_cpu(trc_ipi_to_cpu, cpu))
1740 pr_alert("\tIPI outstanding to CPU %d\n", cpu);
1741}
1742
d5f177d3
PM
1743/* Do one scan of the holdout list. */
1744static void check_all_holdout_tasks_trace(struct list_head *hop,
4593e772 1745 bool needreport, bool *firstreport)
d5f177d3
PM
1746{
1747 struct task_struct *g, *t;
1748
eea3423b 1749 // Disable CPU hotplug across the holdout list scan for IPIs.
81b4a7bc
PM
1750 cpus_read_lock();
1751
d5f177d3
PM
1752 list_for_each_entry_safe(t, g, hop, trc_holdout_list) {
1753 // If safe and needed, try to check the current task.
1754 if (READ_ONCE(t->trc_ipi_to_cpu) == -1 &&
3847b645 1755 !(rcu_ld_need_qs(t) & TRC_NEED_QS_CHECKED))
d5f177d3
PM
1756 trc_wait_for_one_reader(t, hop);
1757
1758 // If check succeeded, remove this task from the list.
f5dbc594 1759 if (smp_load_acquire(&t->trc_ipi_to_cpu) == -1 &&
3847b645 1760 rcu_ld_need_qs(t) == TRC_NEED_QS_CHECKED)
d5f177d3 1761 trc_del_holdout(t);
4593e772
PM
1762 else if (needreport)
1763 show_stalled_task_trace(t, firstreport);
d6ad6063 1764 cond_resched_tasks_rcu_qs();
4593e772 1765 }
81b4a7bc
PM
1766
1767 // Re-enable CPU hotplug now that the holdout list scan has completed.
1768 cpus_read_unlock();
1769
4593e772 1770 if (needreport) {
89401176 1771 if (*firstreport)
4593e772
PM
1772 pr_err("INFO: rcu_tasks_trace detected stalls? (Late IPI?)\n");
1773 show_stalled_ipi_trace();
d5f177d3
PM
1774 }
1775}
1776
cbe0d8d9
PM
1777static void rcu_tasks_trace_empty_fn(void *unused)
1778{
1779}
1780
d5f177d3 1781/* Wait for grace period to complete and provide ordering. */
af051ca4 1782static void rcu_tasks_trace_postgp(struct rcu_tasks *rtp)
d5f177d3 1783{
cbe0d8d9 1784 int cpu;
4593e772 1785
cbe0d8d9
PM
1786 // Wait for any lingering IPI handlers to complete. Note that
1787 // if a CPU has gone offline or transitioned to userspace in the
1788 // meantime, all IPI handlers should have been drained beforehand.
1789 // Yes, this assumes that CPUs process IPIs in order. If that ever
1790 // changes, there will need to be a recheck and/or timed wait.
1791 for_each_online_cpu(cpu)
f5dbc594 1792 if (WARN_ON_ONCE(smp_load_acquire(per_cpu_ptr(&trc_ipi_to_cpu, cpu))))
cbe0d8d9
PM
1793 smp_call_function_single(cpu, rcu_tasks_trace_empty_fn, NULL, 1);
1794
d5f177d3 1795 smp_mb(); // Caller's code must be ordered after wakeup.
43766c3e 1796 // Pairs with pretty much every ordering primitive.
d5f177d3
PM
1797}
1798
1799/* Report any needed quiescent state for this exiting task. */
25246fc8 1800static void exit_tasks_rcu_finish_trace(struct task_struct *t)
d5f177d3 1801{
0356d4e6
PM
1802 union rcu_special trs = READ_ONCE(t->trc_reader_special);
1803
3847b645 1804 rcu_trc_cmpxchg_need_qs(t, 0, TRC_NEED_QS_CHECKED);
bdb0cca0 1805 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
0bcb3868 1806 if (WARN_ON_ONCE(rcu_ld_need_qs(t) & TRC_NEED_QS || trs.b.blocked))
a5c071cc 1807 rcu_read_unlock_trace_special(t);
3847b645
PM
1808 else
1809 WRITE_ONCE(t->trc_reader_nesting, 0);
d5f177d3
PM
1810}
1811
d5f177d3
PM
1812/**
1813 * call_rcu_tasks_trace() - Queue a callback trace task-based grace period
1814 * @rhp: structure to be used for queueing the RCU updates.
1815 * @func: actual callback function to be invoked after the grace period
1816 *
ed42c380
NU
1817 * The callback function will be invoked some time after a trace rcu-tasks
1818 * grace period elapses, in other words after all currently executing
1819 * trace rcu-tasks read-side critical sections have completed. These
1820 * read-side critical sections are delimited by calls to rcu_read_lock_trace()
1821 * and rcu_read_unlock_trace().
d5f177d3
PM
1822 *
1823 * See the description of call_rcu() for more detailed information on
1824 * memory ordering guarantees.
1825 */
1826void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func)
1827{
1828 call_rcu_tasks_generic(rhp, func, &rcu_tasks_trace);
1829}
1830EXPORT_SYMBOL_GPL(call_rcu_tasks_trace);
1831
1832/**
1833 * synchronize_rcu_tasks_trace - wait for a trace rcu-tasks grace period
1834 *
1835 * Control will return to the caller some time after a trace rcu-tasks
c7dcf810 1836 * grace period has elapsed, in other words after all currently executing
ed42c380 1837 * trace rcu-tasks read-side critical sections have elapsed. These read-side
c7dcf810
PM
1838 * critical sections are delimited by calls to rcu_read_lock_trace()
1839 * and rcu_read_unlock_trace().
d5f177d3
PM
1840 *
1841 * This is a very specialized primitive, intended only for a few uses in
1842 * tracing and other situations requiring manipulation of function preambles
1843 * and profiling hooks. The synchronize_rcu_tasks_trace() function is not
1844 * (yet) intended for heavy use from multiple CPUs.
1845 *
1846 * See the description of synchronize_rcu() for more detailed information
1847 * on memory ordering guarantees.
1848 */
1849void synchronize_rcu_tasks_trace(void)
1850{
1851 RCU_LOCKDEP_WARN(lock_is_held(&rcu_trace_lock_map), "Illegal synchronize_rcu_tasks_trace() in RCU Tasks Trace read-side critical section");
1852 synchronize_rcu_tasks_generic(&rcu_tasks_trace);
1853}
1854EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_trace);
1855
1856/**
1857 * rcu_barrier_tasks_trace - Wait for in-flight call_rcu_tasks_trace() callbacks.
1858 *
1859 * Although the current implementation is guaranteed to wait, it is not
1860 * obligated to, for example, if there are no pending callbacks.
1861 */
1862void rcu_barrier_tasks_trace(void)
1863{
ce9b1c66 1864 rcu_barrier_tasks_generic(&rcu_tasks_trace);
d5f177d3
PM
1865}
1866EXPORT_SYMBOL_GPL(rcu_barrier_tasks_trace);
1867
450d461a
PM
1868int rcu_tasks_trace_lazy_ms = -1;
1869module_param(rcu_tasks_trace_lazy_ms, int, 0444);
1870
d5f177d3
PM
1871static int __init rcu_spawn_tasks_trace_kthread(void)
1872{
cafafd67 1873 cblist_init_generic(&rcu_tasks_trace);
2393a613 1874 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB)) {
4fe192df 1875 rcu_tasks_trace.gp_sleep = HZ / 10;
75dc2da5 1876 rcu_tasks_trace.init_fract = HZ / 10;
2393a613 1877 } else {
4fe192df
PM
1878 rcu_tasks_trace.gp_sleep = HZ / 200;
1879 if (rcu_tasks_trace.gp_sleep <= 0)
1880 rcu_tasks_trace.gp_sleep = 1;
75dc2da5 1881 rcu_tasks_trace.init_fract = HZ / 200;
2393a613
PM
1882 if (rcu_tasks_trace.init_fract <= 0)
1883 rcu_tasks_trace.init_fract = 1;
1884 }
450d461a
PM
1885 if (rcu_tasks_trace_lazy_ms >= 0)
1886 rcu_tasks_trace.lazy_jiffies = msecs_to_jiffies(rcu_tasks_trace_lazy_ms);
d5f177d3 1887 rcu_tasks_trace.pregp_func = rcu_tasks_trace_pregp_step;
d5f177d3
PM
1888 rcu_tasks_trace.postscan_func = rcu_tasks_trace_postscan;
1889 rcu_tasks_trace.holdouts_func = check_all_holdout_tasks_trace;
1890 rcu_tasks_trace.postgp_func = rcu_tasks_trace_postgp;
1891 rcu_spawn_tasks_kthread_generic(&rcu_tasks_trace);
1892 return 0;
1893}
d5f177d3 1894
27c0f144
PM
1895#if !defined(CONFIG_TINY_RCU)
1896void show_rcu_tasks_trace_gp_kthread(void)
e21408ce 1897{
40471509 1898 char buf[64];
e21408ce 1899
ffcc21a3
PM
1900 sprintf(buf, "N%lu h:%lu/%lu/%lu",
1901 data_race(n_trc_holdouts),
edf3775f 1902 data_race(n_heavy_reader_ofl_updates),
40471509
PM
1903 data_race(n_heavy_reader_updates),
1904 data_race(n_heavy_reader_attempts));
e21408ce
PM
1905 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_trace, buf);
1906}
27c0f144
PM
1907EXPORT_SYMBOL_GPL(show_rcu_tasks_trace_gp_kthread);
1908#endif // !defined(CONFIG_TINY_RCU)
e21408ce 1909
d5f177d3 1910#else /* #ifdef CONFIG_TASKS_TRACE_RCU */
25246fc8 1911static void exit_tasks_rcu_finish_trace(struct task_struct *t) { }
d5f177d3 1912#endif /* #else #ifdef CONFIG_TASKS_TRACE_RCU */
8fd8ca38 1913
8344496e 1914#ifndef CONFIG_TINY_RCU
e21408ce
PM
1915void show_rcu_tasks_gp_kthreads(void)
1916{
1917 show_rcu_tasks_classic_gp_kthread();
1918 show_rcu_tasks_rude_gp_kthread();
1919 show_rcu_tasks_trace_gp_kthread();
1920}
8344496e 1921#endif /* #ifndef CONFIG_TINY_RCU */
e21408ce 1922
bfba7ed0
URS
1923#ifdef CONFIG_PROVE_RCU
1924struct rcu_tasks_test_desc {
1925 struct rcu_head rh;
1926 const char *name;
1927 bool notrun;
1cf1144e 1928 unsigned long runstart;
bfba7ed0
URS
1929};
1930
1931static struct rcu_tasks_test_desc tests[] = {
1932 {
1933 .name = "call_rcu_tasks()",
1934 /* If not defined, the test is skipped. */
1cf1144e 1935 .notrun = IS_ENABLED(CONFIG_TASKS_RCU),
bfba7ed0
URS
1936 },
1937 {
1938 .name = "call_rcu_tasks_rude()",
1939 /* If not defined, the test is skipped. */
1cf1144e 1940 .notrun = IS_ENABLED(CONFIG_TASKS_RUDE_RCU),
bfba7ed0
URS
1941 },
1942 {
1943 .name = "call_rcu_tasks_trace()",
1944 /* If not defined, the test is skipped. */
1cf1144e 1945 .notrun = IS_ENABLED(CONFIG_TASKS_TRACE_RCU)
bfba7ed0
URS
1946 }
1947};
1948
1949static void test_rcu_tasks_callback(struct rcu_head *rhp)
1950{
1951 struct rcu_tasks_test_desc *rttd =
1952 container_of(rhp, struct rcu_tasks_test_desc, rh);
1953
1954 pr_info("Callback from %s invoked.\n", rttd->name);
1955
1cf1144e 1956 rttd->notrun = false;
bfba7ed0
URS
1957}
1958
1959static void rcu_tasks_initiate_self_tests(void)
1960{
1961 pr_info("Running RCU-tasks wait API self tests\n");
1962#ifdef CONFIG_TASKS_RCU
9420fb93 1963 tests[0].runstart = jiffies;
bfba7ed0
URS
1964 synchronize_rcu_tasks();
1965 call_rcu_tasks(&tests[0].rh, test_rcu_tasks_callback);
1966#endif
1967
1968#ifdef CONFIG_TASKS_RUDE_RCU
9420fb93 1969 tests[1].runstart = jiffies;
bfba7ed0
URS
1970 synchronize_rcu_tasks_rude();
1971 call_rcu_tasks_rude(&tests[1].rh, test_rcu_tasks_callback);
1972#endif
1973
1974#ifdef CONFIG_TASKS_TRACE_RCU
9420fb93 1975 tests[2].runstart = jiffies;
bfba7ed0
URS
1976 synchronize_rcu_tasks_trace();
1977 call_rcu_tasks_trace(&tests[2].rh, test_rcu_tasks_callback);
1978#endif
1979}
1980
e72ee5e1
WL
1981/*
1982 * Return: 0 - test passed
1983 * 1 - test failed, but have not timed out yet
1984 * -1 - test failed and timed out
1985 */
bfba7ed0
URS
1986static int rcu_tasks_verify_self_tests(void)
1987{
1988 int ret = 0;
1989 int i;
1cf1144e 1990 unsigned long bst = rcu_task_stall_timeout;
bfba7ed0 1991
1cf1144e
PM
1992 if (bst <= 0 || bst > RCU_TASK_BOOT_STALL_TIMEOUT)
1993 bst = RCU_TASK_BOOT_STALL_TIMEOUT;
bfba7ed0 1994 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1cf1144e
PM
1995 while (tests[i].notrun) { // still hanging.
1996 if (time_after(jiffies, tests[i].runstart + bst)) {
1997 pr_err("%s has failed boot-time tests.\n", tests[i].name);
1998 ret = -1;
1999 break;
2000 }
e72ee5e1
WL
2001 ret = 1;
2002 break;
bfba7ed0
URS
2003 }
2004 }
e72ee5e1 2005 WARN_ON(ret < 0);
bfba7ed0
URS
2006
2007 return ret;
2008}
e72ee5e1
WL
2009
2010/*
2011 * Repeat the rcu_tasks_verify_self_tests() call once every second until the
2012 * test passes or has timed out.
2013 */
2014static struct delayed_work rcu_tasks_verify_work;
2015static void rcu_tasks_verify_work_fn(struct work_struct *work __maybe_unused)
2016{
2017 int ret = rcu_tasks_verify_self_tests();
2018
2019 if (ret <= 0)
2020 return;
2021
2022 /* Test fails but not timed out yet, reschedule another check */
2023 schedule_delayed_work(&rcu_tasks_verify_work, HZ);
2024}
2025
2026static int rcu_tasks_verify_schedule_work(void)
2027{
2028 INIT_DELAYED_WORK(&rcu_tasks_verify_work, rcu_tasks_verify_work_fn);
2029 rcu_tasks_verify_work_fn(NULL);
2030 return 0;
2031}
2032late_initcall(rcu_tasks_verify_schedule_work);
bfba7ed0
URS
2033#else /* #ifdef CONFIG_PROVE_RCU */
2034static void rcu_tasks_initiate_self_tests(void) { }
2035#endif /* #else #ifdef CONFIG_PROVE_RCU */
2036
1b04fa99
URS
2037void __init rcu_init_tasks_generic(void)
2038{
2039#ifdef CONFIG_TASKS_RCU
2040 rcu_spawn_tasks_kthread();
2041#endif
2042
2043#ifdef CONFIG_TASKS_RUDE_RCU
2044 rcu_spawn_tasks_rude_kthread();
2045#endif
2046
2047#ifdef CONFIG_TASKS_TRACE_RCU
2048 rcu_spawn_tasks_trace_kthread();
2049#endif
bfba7ed0
URS
2050
2051 // Run the self-tests.
2052 rcu_tasks_initiate_self_tests();
1b04fa99
URS
2053}
2054
8fd8ca38
PM
2055#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
2056static inline void rcu_tasks_bootup_oddness(void) {}
2057#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */