rcu-tasks: Make rcu_barrier_tasks*() handle multiple callback queues
[linux-2.6-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);
e4fe5dd6
PM
17typedef void (*pregp_func_t)(void);
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.
d363f833 27 * @rtp_work: Work queue for invoking callbacks.
ce9b1c66
PM
28 * @barrier_q_head: RCU callback for barrier operation.
29 * @cpu: CPU number corresponding to this entry.
30 * @rtpp: Pointer to the rcu_tasks structure.
cafafd67
PM
31 */
32struct rcu_tasks_percpu {
9b073de1 33 struct rcu_segcblist cblist;
381a4f3b 34 raw_spinlock_t __private lock;
d363f833 35 struct work_struct rtp_work;
ce9b1c66 36 struct rcu_head barrier_q_head;
d363f833
PM
37 int cpu;
38 struct rcu_tasks *rtpp;
cafafd67
PM
39};
40
41/**
42 * struct rcu_tasks - Definition for a Tasks-RCU-like mechanism.
a616aec9 43 * @cbs_wq: Wait queue allowing new callback to get kthread's attention.
cafafd67 44 * @cbs_gbl_lock: Lock protecting callback list.
07e10515 45 * @kthread_ptr: This flavor's grace-period/callback-invocation kthread.
5873b8a9 46 * @gp_func: This flavor's grace-period-wait function.
af051ca4 47 * @gp_state: Grace period's most recent state transition (debugging).
4fe192df 48 * @gp_sleep: Per-grace-period sleep to prevent CPU-bound looping.
2393a613 49 * @init_fract: Initial backoff sleep interval.
af051ca4
PM
50 * @gp_jiffies: Time of last @gp_state transition.
51 * @gp_start: Most recent grace-period start in jiffies.
b14fb4fb 52 * @tasks_gp_seq: Number of grace periods completed since boot.
238dbce3 53 * @n_ipis: Number of IPIs sent to encourage grace periods to end.
7e0669c3 54 * @n_ipis_fails: Number of IPI-send failures.
e4fe5dd6
PM
55 * @pregp_func: This flavor's pre-grace-period function (optional).
56 * @pertask_func: This flavor's per-task scan function (optional).
57 * @postscan_func: This flavor's post-task scan function (optional).
85b86994 58 * @holdouts_func: This flavor's holdout-list scan function (optional).
e4fe5dd6 59 * @postgp_func: This flavor's post-grace-period function (optional).
5873b8a9 60 * @call_func: This flavor's call_rcu()-equivalent function.
cafafd67 61 * @rtpcpu: This flavor's rcu_tasks_percpu structure.
7a30871b 62 * @percpu_enqueue_shift: Shift down CPU ID this much when enqueuing callbacks.
8dd593fd 63 * @percpu_enqueue_lim: Number of per-CPU callback queues in use.
ce9b1c66
PM
64 * @barrier_q_mutex: Serialize barrier operations.
65 * @barrier_q_count: Number of queues being waited on.
66 * @barrier_q_completion: Barrier wait/wakeup mechanism.
67 * @barrier_q_seq: Sequence number for barrier operations.
c97d12a6
PM
68 * @name: This flavor's textual name.
69 * @kname: This flavor's kthread name.
07e10515
PM
70 */
71struct rcu_tasks {
07e10515 72 struct wait_queue_head cbs_wq;
cafafd67 73 raw_spinlock_t cbs_gbl_lock;
af051ca4 74 int gp_state;
4fe192df 75 int gp_sleep;
2393a613 76 int init_fract;
af051ca4 77 unsigned long gp_jiffies;
88092d0c 78 unsigned long gp_start;
b14fb4fb 79 unsigned long tasks_gp_seq;
238dbce3 80 unsigned long n_ipis;
7e0669c3 81 unsigned long n_ipis_fails;
07e10515 82 struct task_struct *kthread_ptr;
5873b8a9 83 rcu_tasks_gp_func_t gp_func;
e4fe5dd6
PM
84 pregp_func_t pregp_func;
85 pertask_func_t pertask_func;
86 postscan_func_t postscan_func;
87 holdouts_func_t holdouts_func;
88 postgp_func_t postgp_func;
5873b8a9 89 call_rcu_func_t call_func;
cafafd67 90 struct rcu_tasks_percpu __percpu *rtpcpu;
7a30871b 91 int percpu_enqueue_shift;
8dd593fd 92 int percpu_enqueue_lim;
ce9b1c66
PM
93 struct mutex barrier_q_mutex;
94 atomic_t barrier_q_count;
95 struct completion barrier_q_completion;
96 unsigned long barrier_q_seq;
c97d12a6
PM
97 char *name;
98 char *kname;
07e10515
PM
99};
100
cafafd67
PM
101#define DEFINE_RCU_TASKS(rt_name, gp, call, n) \
102static DEFINE_PER_CPU(struct rcu_tasks_percpu, rt_name ## __percpu) = { \
381a4f3b 103 .lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name ## __percpu.cbs_pcpu_lock), \
cafafd67
PM
104}; \
105static struct rcu_tasks rt_name = \
106{ \
107 .cbs_wq = __WAIT_QUEUE_HEAD_INITIALIZER(rt_name.cbs_wq), \
108 .cbs_gbl_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_gbl_lock), \
109 .gp_func = gp, \
110 .call_func = call, \
111 .rtpcpu = &rt_name ## __percpu, \
112 .name = n, \
7a30871b 113 .percpu_enqueue_shift = ilog2(CONFIG_NR_CPUS), \
8dd593fd 114 .percpu_enqueue_lim = 1, \
ce9b1c66
PM
115 .barrier_q_mutex = __MUTEX_INITIALIZER(rt_name.barrier_q_mutex), \
116 .barrier_q_seq = (0UL - 50UL) << RCU_SEQ_CTR_SHIFT, \
cafafd67 117 .kname = #rt_name, \
07e10515
PM
118}
119
eacd6f04
PM
120/* Track exiting tasks in order to allow them to be waited for. */
121DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
122
b0afa0f0 123/* Avoid IPIing CPUs early in the grace period. */
574de876 124#define RCU_TASK_IPI_DELAY (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) ? HZ / 2 : 0)
b0afa0f0
PM
125static int rcu_task_ipi_delay __read_mostly = RCU_TASK_IPI_DELAY;
126module_param(rcu_task_ipi_delay, int, 0644);
127
eacd6f04
PM
128/* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
129#define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
130static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
131module_param(rcu_task_stall_timeout, int, 0644);
132
af051ca4
PM
133/* RCU tasks grace-period state for debugging. */
134#define RTGS_INIT 0
135#define RTGS_WAIT_WAIT_CBS 1
136#define RTGS_WAIT_GP 2
137#define RTGS_PRE_WAIT_GP 3
138#define RTGS_SCAN_TASKLIST 4
139#define RTGS_POST_SCAN_TASKLIST 5
140#define RTGS_WAIT_SCAN_HOLDOUTS 6
141#define RTGS_SCAN_HOLDOUTS 7
142#define RTGS_POST_GP 8
143#define RTGS_WAIT_READERS 9
144#define RTGS_INVOKE_CBS 10
145#define RTGS_WAIT_CBS 11
8344496e 146#ifndef CONFIG_TINY_RCU
af051ca4
PM
147static const char * const rcu_tasks_gp_state_names[] = {
148 "RTGS_INIT",
149 "RTGS_WAIT_WAIT_CBS",
150 "RTGS_WAIT_GP",
151 "RTGS_PRE_WAIT_GP",
152 "RTGS_SCAN_TASKLIST",
153 "RTGS_POST_SCAN_TASKLIST",
154 "RTGS_WAIT_SCAN_HOLDOUTS",
155 "RTGS_SCAN_HOLDOUTS",
156 "RTGS_POST_GP",
157 "RTGS_WAIT_READERS",
158 "RTGS_INVOKE_CBS",
159 "RTGS_WAIT_CBS",
160};
8344496e 161#endif /* #ifndef CONFIG_TINY_RCU */
af051ca4 162
5873b8a9
PM
163////////////////////////////////////////////////////////////////////////
164//
165// Generic code.
166
d363f833
PM
167static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp);
168
af051ca4
PM
169/* Record grace-period phase and time. */
170static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
171{
172 rtp->gp_state = newstate;
173 rtp->gp_jiffies = jiffies;
174}
175
8344496e 176#ifndef CONFIG_TINY_RCU
af051ca4
PM
177/* Return state name. */
178static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
179{
180 int i = data_race(rtp->gp_state); // Let KCSAN detect update races
181 int j = READ_ONCE(i); // Prevent the compiler from reading twice
182
183 if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
184 return "???";
185 return rcu_tasks_gp_state_names[j];
186}
8344496e 187#endif /* #ifndef CONFIG_TINY_RCU */
af051ca4 188
cafafd67
PM
189// Initialize per-CPU callback lists for the specified flavor of
190// Tasks RCU.
191static void cblist_init_generic(struct rcu_tasks *rtp)
192{
193 int cpu;
194 unsigned long flags;
195
196 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
7a30871b 197 rtp->percpu_enqueue_shift = ilog2(nr_cpu_ids);
8dd593fd 198 rtp->percpu_enqueue_lim = 1;
cafafd67
PM
199 for_each_possible_cpu(cpu) {
200 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
201
202 WARN_ON_ONCE(!rtpcp);
203 if (cpu)
381a4f3b
PM
204 raw_spin_lock_init(&ACCESS_PRIVATE(rtpcp, lock));
205 raw_spin_lock_rcu_node(rtpcp); // irqs already disabled.
9b073de1
PM
206 if (rcu_segcblist_empty(&rtpcp->cblist))
207 rcu_segcblist_init(&rtpcp->cblist);
d363f833
PM
208 INIT_WORK(&rtpcp->rtp_work, rcu_tasks_invoke_cbs_wq);
209 rtpcp->cpu = cpu;
210 rtpcp->rtpp = rtp;
381a4f3b 211 raw_spin_unlock_rcu_node(rtpcp); // irqs remain disabled.
cafafd67
PM
212 }
213 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
214
215}
216
5873b8a9
PM
217// Enqueue a callback for the specified flavor of Tasks RCU.
218static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
219 struct rcu_tasks *rtp)
eacd6f04
PM
220{
221 unsigned long flags;
222 bool needwake;
cafafd67 223 struct rcu_tasks_percpu *rtpcp;
eacd6f04
PM
224
225 rhp->next = NULL;
226 rhp->func = func;
cafafd67 227 local_irq_save(flags);
7a30871b
PM
228 rtpcp = per_cpu_ptr(rtp->rtpcpu,
229 smp_processor_id() >> READ_ONCE(rtp->percpu_enqueue_shift));
381a4f3b 230 raw_spin_lock_rcu_node(rtpcp); // irqs already disabled.
9b073de1 231 if (!rcu_segcblist_is_enabled(&rtpcp->cblist)) {
381a4f3b 232 raw_spin_unlock_rcu_node(rtpcp); // irqs remain disabled.
cafafd67 233 cblist_init_generic(rtp);
381a4f3b 234 raw_spin_lock_rcu_node(rtpcp); // irqs already disabled.
cafafd67 235 }
9b073de1
PM
236 needwake = rcu_segcblist_empty(&rtpcp->cblist);
237 rcu_segcblist_enqueue(&rtpcp->cblist, rhp);
381a4f3b 238 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
eacd6f04 239 /* We can't create the thread unless interrupts are enabled. */
07e10515
PM
240 if (needwake && READ_ONCE(rtp->kthread_ptr))
241 wake_up(&rtp->cbs_wq);
eacd6f04 242}
eacd6f04 243
5873b8a9
PM
244// Wait for a grace period for the specified flavor of Tasks RCU.
245static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
eacd6f04
PM
246{
247 /* Complain if the scheduler has not started. */
248 RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
249 "synchronize_rcu_tasks called too soon");
250
251 /* Wait for the grace period. */
5873b8a9 252 wait_rcu_gp(rtp->call_func);
eacd6f04
PM
253}
254
ce9b1c66
PM
255// RCU callback function for rcu_barrier_tasks_generic().
256static void rcu_barrier_tasks_generic_cb(struct rcu_head *rhp)
257{
258 struct rcu_tasks *rtp;
259 struct rcu_tasks_percpu *rtpcp;
260
261 rtpcp = container_of(rhp, struct rcu_tasks_percpu, barrier_q_head);
262 rtp = rtpcp->rtpp;
263 if (atomic_dec_and_test(&rtp->barrier_q_count))
264 complete(&rtp->barrier_q_completion);
265}
266
267// Wait for all in-flight callbacks for the specified RCU Tasks flavor.
268// Operates in a manner similar to rcu_barrier().
269static void rcu_barrier_tasks_generic(struct rcu_tasks *rtp)
270{
271 int cpu;
272 unsigned long flags;
273 struct rcu_tasks_percpu *rtpcp;
274 unsigned long s = rcu_seq_snap(&rtp->barrier_q_seq);
275
276 mutex_lock(&rtp->barrier_q_mutex);
277 if (rcu_seq_done(&rtp->barrier_q_seq, s)) {
278 smp_mb();
279 mutex_unlock(&rtp->barrier_q_mutex);
280 return;
281 }
282 rcu_seq_start(&rtp->barrier_q_seq);
283 init_completion(&rtp->barrier_q_completion);
284 atomic_set(&rtp->barrier_q_count, 2);
285 for_each_possible_cpu(cpu) {
286 if (cpu >= smp_load_acquire(&rtp->percpu_enqueue_lim))
287 break;
288 rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
289 rtpcp->barrier_q_head.func = rcu_barrier_tasks_generic_cb;
290 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
291 if (rcu_segcblist_entrain(&rtpcp->cblist, &rtpcp->barrier_q_head))
292 atomic_inc(&rtp->barrier_q_count);
293 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
294 }
295 if (atomic_sub_and_test(2, &rtp->barrier_q_count))
296 complete(&rtp->barrier_q_completion);
297 wait_for_completion(&rtp->barrier_q_completion);
298 rcu_seq_end(&rtp->barrier_q_seq);
299 mutex_unlock(&rtp->barrier_q_mutex);
300}
301
4d1114c0
PM
302// Advance callbacks and indicate whether either a grace period or
303// callback invocation is needed.
304static int rcu_tasks_need_gpcb(struct rcu_tasks *rtp)
305{
306 int cpu;
307 unsigned long flags;
308 int needgpcb = 0;
309
310 for (cpu = 0; cpu < rtp->percpu_enqueue_lim; cpu++) {
311 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
312
313 /* Advance and accelerate any new callbacks. */
314 if (rcu_segcblist_empty(&rtpcp->cblist))
315 continue;
316 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
317 rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
318 (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
319 if (rcu_segcblist_pend_cbs(&rtpcp->cblist))
320 needgpcb |= 0x3;
321 if (!rcu_segcblist_empty(&rtpcp->cblist))
322 needgpcb |= 0x1;
323 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
324 }
325 return needgpcb;
326}
327
57881863 328// Advance callbacks and invoke any that are ready.
d363f833 329static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu *rtpcp)
eacd6f04 330{
57881863 331 int cpu;
d363f833 332 int cpunext;
eacd6f04 333 unsigned long flags;
9b073de1 334 int len;
9b073de1 335 struct rcu_head *rhp;
d363f833
PM
336 struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
337 struct rcu_tasks_percpu *rtpcp_next;
338
339 cpu = rtpcp->cpu;
340 cpunext = cpu * 2 + 1;
341 if (cpunext < rtp->percpu_enqueue_lim) {
342 rtpcp_next = per_cpu_ptr(rtp->rtpcpu, cpunext);
343 queue_work_on(cpunext, system_wq, &rtpcp_next->rtp_work);
344 cpunext++;
345 if (cpunext < rtp->percpu_enqueue_lim) {
346 rtpcp_next = per_cpu_ptr(rtp->rtpcpu, cpunext);
347 queue_work_on(cpunext, system_wq, &rtpcp_next->rtp_work);
57881863 348 }
57881863 349 }
d363f833
PM
350
351 if (rcu_segcblist_empty(&rtpcp->cblist))
352 return;
353 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
354 rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
355 rcu_segcblist_extract_done_cbs(&rtpcp->cblist, &rcl);
356 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
357 len = rcl.len;
358 for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
359 local_bh_disable();
360 rhp->func(rhp);
361 local_bh_enable();
362 cond_resched();
363 }
364 raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
365 rcu_segcblist_add_len(&rtpcp->cblist, -len);
366 (void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
367 raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
368}
369
370// Workqueue flood to advance callbacks and invoke any that are ready.
371static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp)
372{
373 struct rcu_tasks *rtp;
374 struct rcu_tasks_percpu *rtpcp = container_of(wp, struct rcu_tasks_percpu, rtp_work);
375
376 rtp = rtpcp->rtpp;
377 rcu_tasks_invoke_cbs(rtp, rtpcp);
57881863
PM
378}
379
380/* RCU-tasks kthread that detects grace periods and invokes callbacks. */
381static int __noreturn rcu_tasks_kthread(void *arg)
382{
383 int needgpcb;
07e10515 384 struct rcu_tasks *rtp = arg;
eacd6f04
PM
385
386 /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
387 housekeeping_affine(current, HK_FLAG_RCU);
07e10515 388 WRITE_ONCE(rtp->kthread_ptr, current); // Let GPs start!
eacd6f04
PM
389
390 /*
391 * Each pass through the following loop makes one check for
392 * newly arrived callbacks, and, if there are some, waits for
393 * one RCU-tasks grace period and then invokes the callbacks.
394 * This loop is terminated by the system going down. ;-)
395 */
396 for (;;) {
0db7c32a 397 set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
eacd6f04 398
eacd6f04 399 /* If there were none, wait a bit and start over. */
4d1114c0 400 wait_event_idle(rtp->cbs_wq, (needgpcb = rcu_tasks_need_gpcb(rtp)));
eacd6f04 401
4d1114c0
PM
402 if (needgpcb & 0x2) {
403 // Wait for one grace period.
404 set_tasks_gp_state(rtp, RTGS_WAIT_GP);
405 rtp->gp_start = jiffies;
406 rcu_seq_start(&rtp->tasks_gp_seq);
407 rtp->gp_func(rtp);
408 rcu_seq_end(&rtp->tasks_gp_seq);
409 }
eacd6f04 410
57881863 411 /* Invoke callbacks. */
af051ca4 412 set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
d363f833 413 rcu_tasks_invoke_cbs(rtp, per_cpu_ptr(rtp->rtpcpu, 0));
57881863 414
eacd6f04 415 /* Paranoid sleep to keep this from entering a tight loop */
4fe192df 416 schedule_timeout_idle(rtp->gp_sleep);
eacd6f04
PM
417 }
418}
419
1b04fa99 420/* Spawn RCU-tasks grace-period kthread. */
5873b8a9 421static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
eacd6f04
PM
422{
423 struct task_struct *t;
424
c97d12a6
PM
425 t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
426 if (WARN_ONCE(IS_ERR(t), "%s: Could not start %s grace-period kthread, OOM is now expected behavior\n", __func__, rtp->name))
5873b8a9 427 return;
eacd6f04 428 smp_mb(); /* Ensure others see full kthread. */
eacd6f04 429}
eacd6f04 430
eacd6f04
PM
431#ifndef CONFIG_TINY_RCU
432
433/*
434 * Print any non-default Tasks RCU settings.
435 */
436static void __init rcu_tasks_bootup_oddness(void)
437{
d5f177d3 438#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
eacd6f04
PM
439 if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
440 pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
d5f177d3
PM
441#endif /* #ifdef CONFIG_TASKS_RCU */
442#ifdef CONFIG_TASKS_RCU
443 pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
eacd6f04 444#endif /* #ifdef CONFIG_TASKS_RCU */
c84aad76
PM
445#ifdef CONFIG_TASKS_RUDE_RCU
446 pr_info("\tRude variant of Tasks RCU enabled.\n");
447#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
d5f177d3
PM
448#ifdef CONFIG_TASKS_TRACE_RCU
449 pr_info("\tTracing variant of Tasks RCU enabled.\n");
450#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
eacd6f04
PM
451}
452
453#endif /* #ifndef CONFIG_TINY_RCU */
5873b8a9 454
8344496e 455#ifndef CONFIG_TINY_RCU
e21408ce
PM
456/* Dump out rcutorture-relevant state common to all RCU-tasks flavors. */
457static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
458{
cafafd67 459 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, 0); // for_each...
7e0669c3 460 pr_info("%s: %s(%d) since %lu g:%lu i:%lu/%lu %c%c %s\n",
e21408ce 461 rtp->kname,
7e0669c3 462 tasks_gp_state_getname(rtp), data_race(rtp->gp_state),
af051ca4 463 jiffies - data_race(rtp->gp_jiffies),
b14fb4fb 464 data_race(rcu_seq_current(&rtp->tasks_gp_seq)),
7e0669c3 465 data_race(rtp->n_ipis_fails), data_race(rtp->n_ipis),
e21408ce 466 ".k"[!!data_race(rtp->kthread_ptr)],
9b073de1 467 ".C"[!data_race(rcu_segcblist_empty(&rtpcp->cblist))],
e21408ce
PM
468 s);
469}
27c0f144 470#endif // #ifndef CONFIG_TINY_RCU
e21408ce 471
25246fc8
PM
472static void exit_tasks_rcu_finish_trace(struct task_struct *t);
473
474#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
5873b8a9 475
d01aa263
PM
476////////////////////////////////////////////////////////////////////////
477//
478// Shared code between task-list-scanning variants of Tasks RCU.
479
480/* Wait for one RCU-tasks grace period. */
481static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
482{
483 struct task_struct *g, *t;
484 unsigned long lastreport;
485 LIST_HEAD(holdouts);
486 int fract;
487
af051ca4 488 set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
d01aa263
PM
489 rtp->pregp_func();
490
491 /*
492 * There were callbacks, so we need to wait for an RCU-tasks
493 * grace period. Start off by scanning the task list for tasks
494 * that are not already voluntarily blocked. Mark these tasks
495 * and make a list of them in holdouts.
496 */
af051ca4 497 set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
d01aa263
PM
498 rcu_read_lock();
499 for_each_process_thread(g, t)
500 rtp->pertask_func(t, &holdouts);
501 rcu_read_unlock();
502
af051ca4 503 set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
9796e1ae 504 rtp->postscan_func(&holdouts);
d01aa263
PM
505
506 /*
507 * Each pass through the following loop scans the list of holdout
508 * tasks, removing any that are no longer holdouts. When the list
509 * is empty, we are done.
510 */
511 lastreport = jiffies;
512
2393a613
PM
513 // Start off with initial wait and slowly back off to 1 HZ wait.
514 fract = rtp->init_fract;
d01aa263 515
77dc1741 516 while (!list_empty(&holdouts)) {
d01aa263
PM
517 bool firstreport;
518 bool needreport;
519 int rtst;
520
d01aa263 521 /* Slowly back off waiting for holdouts */
af051ca4 522 set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
75dc2da5 523 schedule_timeout_idle(fract);
d01aa263 524
75dc2da5
PM
525 if (fract < HZ)
526 fract++;
d01aa263
PM
527
528 rtst = READ_ONCE(rcu_task_stall_timeout);
529 needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
530 if (needreport)
531 lastreport = jiffies;
532 firstreport = true;
533 WARN_ON(signal_pending(current));
af051ca4 534 set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
d01aa263
PM
535 rtp->holdouts_func(&holdouts, needreport, &firstreport);
536 }
537
af051ca4
PM
538 set_tasks_gp_state(rtp, RTGS_POST_GP);
539 rtp->postgp_func(rtp);
d01aa263
PM
540}
541
25246fc8
PM
542#endif /* #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU) */
543
544#ifdef CONFIG_TASKS_RCU
545
5873b8a9
PM
546////////////////////////////////////////////////////////////////////////
547//
548// Simple variant of RCU whose quiescent states are voluntary context
8af9e2c7 549// switch, cond_resched_tasks_rcu_qs(), user-space execution, and idle.
5873b8a9
PM
550// As such, grace periods can take one good long time. There are no
551// read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
552// because this implementation is intended to get the system into a safe
553// state for some of the manipulations involved in tracing and the like.
554// Finally, this implementation does not support high call_rcu_tasks()
555// rates from multiple CPUs. If this is required, per-CPU callback lists
556// will be needed.
06a3ec92
PM
557//
558// The implementation uses rcu_tasks_wait_gp(), which relies on function
559// pointers in the rcu_tasks structure. The rcu_spawn_tasks_kthread()
560// function sets these function pointers up so that rcu_tasks_wait_gp()
561// invokes these functions in this order:
562//
563// rcu_tasks_pregp_step():
564// Invokes synchronize_rcu() in order to wait for all in-flight
565// t->on_rq and t->nvcsw transitions to complete. This works because
566// all such transitions are carried out with interrupts disabled.
567// rcu_tasks_pertask(), invoked on every non-idle task:
568// For every runnable non-idle task other than the current one, use
569// get_task_struct() to pin down that task, snapshot that task's
570// number of voluntary context switches, and add that task to the
571// holdout list.
572// rcu_tasks_postscan():
573// Invoke synchronize_srcu() to ensure that all tasks that were
574// in the process of exiting (and which thus might not know to
575// synchronize with this RCU Tasks grace period) have completed
576// exiting.
577// check_all_holdout_tasks(), repeatedly until holdout list is empty:
578// Scans the holdout list, attempting to identify a quiescent state
579// for each task on the list. If there is a quiescent state, the
580// corresponding task is removed from the holdout list.
581// rcu_tasks_postgp():
582// Invokes synchronize_rcu() in order to ensure that all prior
583// t->on_rq and t->nvcsw transitions are seen by all CPUs and tasks
584// to have happened before the end of this RCU Tasks grace period.
585// Again, this works because all such transitions are carried out
586// with interrupts disabled.
587//
588// For each exiting task, the exit_tasks_rcu_start() and
589// exit_tasks_rcu_finish() functions begin and end, respectively, the SRCU
590// read-side critical sections waited for by rcu_tasks_postscan().
591//
381a4f3b
PM
592// Pre-grace-period update-side code is ordered before the grace
593// via the raw_spin_lock.*rcu_node(). Pre-grace-period read-side code
594// is ordered before the grace period via synchronize_rcu() call in
595// rcu_tasks_pregp_step() and by the scheduler's locks and interrupt
06a3ec92 596// disabling.
5873b8a9 597
e4fe5dd6
PM
598/* Pre-grace-period preparation. */
599static void rcu_tasks_pregp_step(void)
600{
601 /*
602 * Wait for all pre-existing t->on_rq and t->nvcsw transitions
603 * to complete. Invoking synchronize_rcu() suffices because all
604 * these transitions occur with interrupts disabled. Without this
605 * synchronize_rcu(), a read-side critical section that started
606 * before the grace period might be incorrectly seen as having
607 * started after the grace period.
608 *
609 * This synchronize_rcu() also dispenses with the need for a
610 * memory barrier on the first store to t->rcu_tasks_holdout,
611 * as it forces the store to happen after the beginning of the
612 * grace period.
613 */
614 synchronize_rcu();
615}
616
617/* Per-task initial processing. */
618static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
619{
620 if (t != current && READ_ONCE(t->on_rq) && !is_idle_task(t)) {
621 get_task_struct(t);
622 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
623 WRITE_ONCE(t->rcu_tasks_holdout, true);
624 list_add(&t->rcu_tasks_holdout_list, hop);
625 }
626}
627
628/* Processing between scanning taskslist and draining the holdout list. */
04a3c5aa 629static void rcu_tasks_postscan(struct list_head *hop)
e4fe5dd6
PM
630{
631 /*
632 * Wait for tasks that are in the process of exiting. This
633 * does only part of the job, ensuring that all tasks that were
634 * previously exiting reach the point where they have disabled
635 * preemption, allowing the later synchronize_rcu() to finish
636 * the job.
637 */
638 synchronize_srcu(&tasks_rcu_exit_srcu);
639}
640
5873b8a9
PM
641/* See if tasks are still holding out, complain if so. */
642static void check_holdout_task(struct task_struct *t,
643 bool needreport, bool *firstreport)
644{
645 int cpu;
646
647 if (!READ_ONCE(t->rcu_tasks_holdout) ||
648 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
649 !READ_ONCE(t->on_rq) ||
650 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
651 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
652 WRITE_ONCE(t->rcu_tasks_holdout, false);
653 list_del_init(&t->rcu_tasks_holdout_list);
654 put_task_struct(t);
655 return;
656 }
657 rcu_request_urgent_qs_task(t);
658 if (!needreport)
659 return;
660 if (*firstreport) {
661 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
662 *firstreport = false;
663 }
664 cpu = task_cpu(t);
665 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
666 t, ".I"[is_idle_task(t)],
667 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
668 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
669 t->rcu_tasks_idle_cpu, cpu);
670 sched_show_task(t);
671}
672
e4fe5dd6
PM
673/* Scan the holdout lists for tasks no longer holding out. */
674static void check_all_holdout_tasks(struct list_head *hop,
675 bool needreport, bool *firstreport)
676{
677 struct task_struct *t, *t1;
678
679 list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
680 check_holdout_task(t, needreport, firstreport);
681 cond_resched();
682 }
683}
684
685/* Finish off the Tasks-RCU grace period. */
af051ca4 686static void rcu_tasks_postgp(struct rcu_tasks *rtp)
e4fe5dd6
PM
687{
688 /*
689 * Because ->on_rq and ->nvcsw are not guaranteed to have a full
690 * memory barriers prior to them in the schedule() path, memory
691 * reordering on other CPUs could cause their RCU-tasks read-side
692 * critical sections to extend past the end of the grace period.
693 * However, because these ->nvcsw updates are carried out with
694 * interrupts disabled, we can use synchronize_rcu() to force the
695 * needed ordering on all such CPUs.
696 *
697 * This synchronize_rcu() also confines all ->rcu_tasks_holdout
698 * accesses to be within the grace period, avoiding the need for
699 * memory barriers for ->rcu_tasks_holdout accesses.
700 *
701 * In addition, this synchronize_rcu() waits for exiting tasks
702 * to complete their final preempt_disable() region of execution,
703 * cleaning up after the synchronize_srcu() above.
704 */
705 synchronize_rcu();
706}
707
5873b8a9 708void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
c97d12a6 709DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
5873b8a9
PM
710
711/**
712 * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
713 * @rhp: structure to be used for queueing the RCU updates.
714 * @func: actual callback function to be invoked after the grace period
715 *
716 * The callback function will be invoked some time after a full grace
717 * period elapses, in other words after all currently executing RCU
718 * read-side critical sections have completed. call_rcu_tasks() assumes
719 * that the read-side critical sections end at a voluntary context
8af9e2c7 720 * switch (not a preemption!), cond_resched_tasks_rcu_qs(), entry into idle,
5873b8a9
PM
721 * or transition to usermode execution. As such, there are no read-side
722 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
723 * this primitive is intended to determine that all tasks have passed
a616aec9 724 * through a safe state, not so much for data-structure synchronization.
5873b8a9
PM
725 *
726 * See the description of call_rcu() for more detailed information on
727 * memory ordering guarantees.
728 */
729void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
730{
731 call_rcu_tasks_generic(rhp, func, &rcu_tasks);
732}
733EXPORT_SYMBOL_GPL(call_rcu_tasks);
734
735/**
736 * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
737 *
738 * Control will return to the caller some time after a full rcu-tasks
739 * grace period has elapsed, in other words after all currently
740 * executing rcu-tasks read-side critical sections have elapsed. These
741 * read-side critical sections are delimited by calls to schedule(),
742 * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
743 * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
744 *
745 * This is a very specialized primitive, intended only for a few uses in
746 * tracing and other situations requiring manipulation of function
747 * preambles and profiling hooks. The synchronize_rcu_tasks() function
748 * is not (yet) intended for heavy use from multiple CPUs.
749 *
750 * See the description of synchronize_rcu() for more detailed information
751 * on memory ordering guarantees.
752 */
753void synchronize_rcu_tasks(void)
754{
755 synchronize_rcu_tasks_generic(&rcu_tasks);
756}
757EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
758
759/**
760 * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
761 *
762 * Although the current implementation is guaranteed to wait, it is not
763 * obligated to, for example, if there are no pending callbacks.
764 */
765void rcu_barrier_tasks(void)
766{
ce9b1c66 767 rcu_barrier_tasks_generic(&rcu_tasks);
5873b8a9
PM
768}
769EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
770
771static int __init rcu_spawn_tasks_kthread(void)
772{
cafafd67 773 cblist_init_generic(&rcu_tasks);
4fe192df 774 rcu_tasks.gp_sleep = HZ / 10;
75dc2da5 775 rcu_tasks.init_fract = HZ / 10;
e4fe5dd6
PM
776 rcu_tasks.pregp_func = rcu_tasks_pregp_step;
777 rcu_tasks.pertask_func = rcu_tasks_pertask;
778 rcu_tasks.postscan_func = rcu_tasks_postscan;
779 rcu_tasks.holdouts_func = check_all_holdout_tasks;
780 rcu_tasks.postgp_func = rcu_tasks_postgp;
5873b8a9
PM
781 rcu_spawn_tasks_kthread_generic(&rcu_tasks);
782 return 0;
783}
5873b8a9 784
27c0f144
PM
785#if !defined(CONFIG_TINY_RCU)
786void show_rcu_tasks_classic_gp_kthread(void)
e21408ce
PM
787{
788 show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
789}
27c0f144
PM
790EXPORT_SYMBOL_GPL(show_rcu_tasks_classic_gp_kthread);
791#endif // !defined(CONFIG_TINY_RCU)
e21408ce 792
25246fc8
PM
793/* Do the srcu_read_lock() for the above synchronize_srcu(). */
794void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
795{
796 preempt_disable();
797 current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
798 preempt_enable();
799}
800
801/* Do the srcu_read_unlock() for the above synchronize_srcu(). */
802void exit_tasks_rcu_finish(void) __releases(&tasks_rcu_exit_srcu)
803{
804 struct task_struct *t = current;
805
806 preempt_disable();
807 __srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
808 preempt_enable();
809 exit_tasks_rcu_finish_trace(t);
810}
811
e21408ce 812#else /* #ifdef CONFIG_TASKS_RCU */
25246fc8
PM
813void exit_tasks_rcu_start(void) { }
814void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
e21408ce 815#endif /* #else #ifdef CONFIG_TASKS_RCU */
c84aad76
PM
816
817#ifdef CONFIG_TASKS_RUDE_RCU
818
819////////////////////////////////////////////////////////////////////////
820//
821// "Rude" variant of Tasks RCU, inspired by Steve Rostedt's trick of
822// passing an empty function to schedule_on_each_cpu(). This approach
e4be1f44
PM
823// provides an asynchronous call_rcu_tasks_rude() API and batching of
824// concurrent calls to the synchronous synchronize_rcu_tasks_rude() API.
9fc98e31
PM
825// This invokes schedule_on_each_cpu() in order to send IPIs far and wide
826// and induces otherwise unnecessary context switches on all online CPUs,
827// whether idle or not.
828//
829// Callback handling is provided by the rcu_tasks_kthread() function.
830//
831// Ordering is provided by the scheduler's context-switch code.
c84aad76
PM
832
833// Empty function to allow workqueues to force a context switch.
834static void rcu_tasks_be_rude(struct work_struct *work)
835{
836}
837
838// Wait for one rude RCU-tasks grace period.
839static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
840{
238dbce3 841 rtp->n_ipis += cpumask_weight(cpu_online_mask);
c84aad76
PM
842 schedule_on_each_cpu(rcu_tasks_be_rude);
843}
844
845void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
c97d12a6
PM
846DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
847 "RCU Tasks Rude");
c84aad76
PM
848
849/**
850 * call_rcu_tasks_rude() - Queue a callback rude task-based grace period
851 * @rhp: structure to be used for queueing the RCU updates.
852 * @func: actual callback function to be invoked after the grace period
853 *
854 * The callback function will be invoked some time after a full grace
855 * period elapses, in other words after all currently executing RCU
856 * read-side critical sections have completed. call_rcu_tasks_rude()
857 * assumes that the read-side critical sections end at context switch,
8af9e2c7 858 * cond_resched_tasks_rcu_qs(), or transition to usermode execution (as
a6517e9c
NU
859 * usermode execution is schedulable). As such, there are no read-side
860 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
861 * this primitive is intended to determine that all tasks have passed
862 * through a safe state, not so much for data-structure synchronization.
c84aad76
PM
863 *
864 * See the description of call_rcu() for more detailed information on
865 * memory ordering guarantees.
866 */
867void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
868{
869 call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
870}
871EXPORT_SYMBOL_GPL(call_rcu_tasks_rude);
872
873/**
874 * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
875 *
876 * Control will return to the caller some time after a rude rcu-tasks
877 * grace period has elapsed, in other words after all currently
878 * executing rcu-tasks read-side critical sections have elapsed. These
879 * read-side critical sections are delimited by calls to schedule(),
a6517e9c
NU
880 * cond_resched_tasks_rcu_qs(), userspace execution (which is a schedulable
881 * context), and (in theory, anyway) cond_resched().
c84aad76
PM
882 *
883 * This is a very specialized primitive, intended only for a few uses in
884 * tracing and other situations requiring manipulation of function preambles
885 * and profiling hooks. The synchronize_rcu_tasks_rude() function is not
886 * (yet) intended for heavy use from multiple CPUs.
887 *
888 * See the description of synchronize_rcu() for more detailed information
889 * on memory ordering guarantees.
890 */
891void synchronize_rcu_tasks_rude(void)
892{
893 synchronize_rcu_tasks_generic(&rcu_tasks_rude);
894}
895EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
896
897/**
898 * rcu_barrier_tasks_rude - Wait for in-flight call_rcu_tasks_rude() callbacks.
899 *
900 * Although the current implementation is guaranteed to wait, it is not
901 * obligated to, for example, if there are no pending callbacks.
902 */
903void rcu_barrier_tasks_rude(void)
904{
ce9b1c66 905 rcu_barrier_tasks_generic(&rcu_tasks_rude);
c84aad76
PM
906}
907EXPORT_SYMBOL_GPL(rcu_barrier_tasks_rude);
908
909static int __init rcu_spawn_tasks_rude_kthread(void)
910{
cafafd67 911 cblist_init_generic(&rcu_tasks_rude);
4fe192df 912 rcu_tasks_rude.gp_sleep = HZ / 10;
c84aad76
PM
913 rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
914 return 0;
915}
c84aad76 916
27c0f144
PM
917#if !defined(CONFIG_TINY_RCU)
918void show_rcu_tasks_rude_gp_kthread(void)
e21408ce
PM
919{
920 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
921}
27c0f144
PM
922EXPORT_SYMBOL_GPL(show_rcu_tasks_rude_gp_kthread);
923#endif // !defined(CONFIG_TINY_RCU)
924#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
d5f177d3
PM
925
926////////////////////////////////////////////////////////////////////////
927//
928// Tracing variant of Tasks RCU. This variant is designed to be used
929// to protect tracing hooks, including those of BPF. This variant
930// therefore:
931//
932// 1. Has explicit read-side markers to allow finite grace periods
933// in the face of in-kernel loops for PREEMPT=n builds.
934//
935// 2. Protects code in the idle loop, exception entry/exit, and
936// CPU-hotplug code paths, similar to the capabilities of SRCU.
937//
c4f113ac 938// 3. Avoids expensive read-side instructions, having overhead similar
d5f177d3
PM
939// to that of Preemptible RCU.
940//
941// There are of course downsides. The grace-period code can send IPIs to
942// CPUs, even when those CPUs are in the idle loop or in nohz_full userspace.
943// It is necessary to scan the full tasklist, much as for Tasks RCU. There
944// is a single callback queue guarded by a single lock, again, much as for
945// Tasks RCU. If needed, these downsides can be at least partially remedied.
946//
947// Perhaps most important, this variant of RCU does not affect the vanilla
948// flavors, rcu_preempt and rcu_sched. The fact that RCU Tasks Trace
949// readers can operate from idle, offline, and exception entry/exit in no
950// way allows rcu_preempt and rcu_sched readers to also do so.
a434dd10
PM
951//
952// The implementation uses rcu_tasks_wait_gp(), which relies on function
953// pointers in the rcu_tasks structure. The rcu_spawn_tasks_trace_kthread()
954// function sets these function pointers up so that rcu_tasks_wait_gp()
955// invokes these functions in this order:
956//
957// rcu_tasks_trace_pregp_step():
958// Initialize the count of readers and block CPU-hotplug operations.
959// rcu_tasks_trace_pertask(), invoked on every non-idle task:
960// Initialize per-task state and attempt to identify an immediate
961// quiescent state for that task, or, failing that, attempt to
962// set that task's .need_qs flag so that task's next outermost
963// rcu_read_unlock_trace() will report the quiescent state (in which
964// case the count of readers is incremented). If both attempts fail,
45f4b4a2
PM
965// the task is added to a "holdout" list. Note that IPIs are used
966// to invoke trc_read_check_handler() in the context of running tasks
967// in order to avoid ordering overhead on common-case shared-variable
968// accessses.
a434dd10
PM
969// rcu_tasks_trace_postscan():
970// Initialize state and attempt to identify an immediate quiescent
971// state as above (but only for idle tasks), unblock CPU-hotplug
972// operations, and wait for an RCU grace period to avoid races with
973// tasks that are in the process of exiting.
974// check_all_holdout_tasks_trace(), repeatedly until holdout list is empty:
975// Scans the holdout list, attempting to identify a quiescent state
976// for each task on the list. If there is a quiescent state, the
977// corresponding task is removed from the holdout list.
978// rcu_tasks_trace_postgp():
979// Wait for the count of readers do drop to zero, reporting any stalls.
980// Also execute full memory barriers to maintain ordering with code
981// executing after the grace period.
982//
983// The exit_tasks_rcu_finish_trace() synchronizes with exiting tasks.
984//
985// Pre-grace-period update-side code is ordered before the grace
986// period via the ->cbs_lock and barriers in rcu_tasks_kthread().
987// Pre-grace-period read-side code is ordered before the grace period by
988// atomic_dec_and_test() of the count of readers (for IPIed readers) and by
989// scheduler context-switch ordering (for locked-down non-running readers).
d5f177d3
PM
990
991// The lockdep state must be outside of #ifdef to be useful.
992#ifdef CONFIG_DEBUG_LOCK_ALLOC
993static struct lock_class_key rcu_lock_trace_key;
994struct lockdep_map rcu_trace_lock_map =
995 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_trace", &rcu_lock_trace_key);
996EXPORT_SYMBOL_GPL(rcu_trace_lock_map);
997#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
998
999#ifdef CONFIG_TASKS_TRACE_RCU
1000
30d8aa51
PM
1001static atomic_t trc_n_readers_need_end; // Number of waited-for readers.
1002static DECLARE_WAIT_QUEUE_HEAD(trc_wait); // List of holdout tasks.
d5f177d3
PM
1003
1004// Record outstanding IPIs to each CPU. No point in sending two...
1005static DEFINE_PER_CPU(bool, trc_ipi_to_cpu);
1006
40471509
PM
1007// The number of detections of task quiescent state relying on
1008// heavyweight readers executing explicit memory barriers.
6731da9e
PM
1009static unsigned long n_heavy_reader_attempts;
1010static unsigned long n_heavy_reader_updates;
1011static unsigned long n_heavy_reader_ofl_updates;
40471509 1012
b0afa0f0
PM
1013void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func);
1014DEFINE_RCU_TASKS(rcu_tasks_trace, rcu_tasks_wait_gp, call_rcu_tasks_trace,
1015 "RCU Tasks Trace");
1016
b38f57c1
PM
1017/*
1018 * This irq_work handler allows rcu_read_unlock_trace() to be invoked
1019 * while the scheduler locks are held.
1020 */
1021static void rcu_read_unlock_iw(struct irq_work *iwp)
1022{
1023 wake_up(&trc_wait);
1024}
1025static DEFINE_IRQ_WORK(rcu_tasks_trace_iw, rcu_read_unlock_iw);
1026
d5f177d3 1027/* If we are the last reader, wake up the grace-period kthread. */
a5c071cc 1028void rcu_read_unlock_trace_special(struct task_struct *t)
d5f177d3 1029{
f8ab3fad 1030 int nq = READ_ONCE(t->trc_reader_special.b.need_qs);
276c4104 1031
9ae58d7b
PM
1032 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) &&
1033 t->trc_reader_special.b.need_mb)
276c4104
PM
1034 smp_mb(); // Pairs with update-side barriers.
1035 // Update .need_qs before ->trc_reader_nesting for irq/NMI handlers.
1036 if (nq)
1037 WRITE_ONCE(t->trc_reader_special.b.need_qs, false);
a5c071cc 1038 WRITE_ONCE(t->trc_reader_nesting, 0);
276c4104 1039 if (nq && atomic_dec_and_test(&trc_n_readers_need_end))
b38f57c1 1040 irq_work_queue(&rcu_tasks_trace_iw);
d5f177d3
PM
1041}
1042EXPORT_SYMBOL_GPL(rcu_read_unlock_trace_special);
1043
1044/* Add a task to the holdout list, if it is not already on the list. */
1045static void trc_add_holdout(struct task_struct *t, struct list_head *bhp)
1046{
1047 if (list_empty(&t->trc_holdout_list)) {
1048 get_task_struct(t);
1049 list_add(&t->trc_holdout_list, bhp);
1050 }
1051}
1052
1053/* Remove a task from the holdout list, if it is in fact present. */
1054static void trc_del_holdout(struct task_struct *t)
1055{
1056 if (!list_empty(&t->trc_holdout_list)) {
1057 list_del_init(&t->trc_holdout_list);
1058 put_task_struct(t);
1059 }
1060}
1061
1062/* IPI handler to check task state. */
1063static void trc_read_check_handler(void *t_in)
1064{
1065 struct task_struct *t = current;
1066 struct task_struct *texp = t_in;
1067
1068 // If the task is no longer running on this CPU, leave.
1069 if (unlikely(texp != t)) {
d5f177d3
PM
1070 goto reset_ipi; // Already on holdout list, so will check later.
1071 }
1072
1073 // If the task is not in a read-side critical section, and
1074 // if this is the last reader, awaken the grace-period kthread.
bdb0cca0 1075 if (likely(!READ_ONCE(t->trc_reader_nesting))) {
d5f177d3
PM
1076 WRITE_ONCE(t->trc_reader_checked, true);
1077 goto reset_ipi;
1078 }
ba3a86e4 1079 // If we are racing with an rcu_read_unlock_trace(), try again later.
96017bf9 1080 if (unlikely(READ_ONCE(t->trc_reader_nesting) < 0))
ba3a86e4 1081 goto reset_ipi;
d5f177d3
PM
1082 WRITE_ONCE(t->trc_reader_checked, true);
1083
1084 // Get here if the task is in a read-side critical section. Set
1085 // its state so that it will awaken the grace-period kthread upon
1086 // exit from that critical section.
96017bf9 1087 atomic_inc(&trc_n_readers_need_end); // One more to wait on.
f8ab3fad 1088 WARN_ON_ONCE(READ_ONCE(t->trc_reader_special.b.need_qs));
276c4104 1089 WRITE_ONCE(t->trc_reader_special.b.need_qs, true);
d5f177d3
PM
1090
1091reset_ipi:
1092 // Allow future IPIs to be sent on CPU and for task.
1093 // Also order this IPI handler against any later manipulations of
1094 // the intended task.
8211e922 1095 smp_store_release(per_cpu_ptr(&trc_ipi_to_cpu, smp_processor_id()), false); // ^^^
d5f177d3
PM
1096 smp_store_release(&texp->trc_ipi_to_cpu, -1); // ^^^
1097}
1098
1099/* Callback function for scheduler to check locked-down task. */
9b3c4ab3 1100static int trc_inspect_reader(struct task_struct *t, void *arg)
d5f177d3 1101{
7d0c9c50 1102 int cpu = task_cpu(t);
18f08e75 1103 int nesting;
7e3b70e0 1104 bool ofl = cpu_is_offline(cpu);
7d0c9c50
PM
1105
1106 if (task_curr(t)) {
30d8aa51 1107 WARN_ON_ONCE(ofl && !is_idle_task(t));
7e3b70e0 1108
7d0c9c50 1109 // If no chance of heavyweight readers, do it the hard way.
7e3b70e0 1110 if (!ofl && !IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB))
9b3c4ab3 1111 return -EINVAL;
7d0c9c50
PM
1112
1113 // If heavyweight readers are enabled on the remote task,
1114 // we can inspect its state despite its currently running.
1115 // However, we cannot safely change its state.
40471509 1116 n_heavy_reader_attempts++;
7e3b70e0
PM
1117 if (!ofl && // Check for "running" idle tasks on offline CPUs.
1118 !rcu_dynticks_zero_in_eqs(cpu, &t->trc_reader_nesting))
9b3c4ab3 1119 return -EINVAL; // No quiescent state, do it the hard way.
40471509 1120 n_heavy_reader_updates++;
edf3775f
PM
1121 if (ofl)
1122 n_heavy_reader_ofl_updates++;
18f08e75 1123 nesting = 0;
7d0c9c50 1124 } else {
bdb0cca0 1125 // The task is not running, so C-language access is safe.
18f08e75 1126 nesting = t->trc_reader_nesting;
7d0c9c50 1127 }
d5f177d3 1128
18f08e75
PM
1129 // If not exiting a read-side critical section, mark as checked
1130 // so that the grace-period kthread will remove it from the
1131 // holdout list.
1132 t->trc_reader_checked = nesting >= 0;
1133 if (nesting <= 0)
6fedc280 1134 return nesting ? -EINVAL : 0; // If in QS, done, otherwise try again later.
7d0c9c50
PM
1135
1136 // The task is in a read-side critical section, so set up its
1137 // state so that it will awaken the grace-period kthread upon exit
1138 // from that critical section.
1139 atomic_inc(&trc_n_readers_need_end); // One more to wait on.
f8ab3fad 1140 WARN_ON_ONCE(READ_ONCE(t->trc_reader_special.b.need_qs));
7d0c9c50 1141 WRITE_ONCE(t->trc_reader_special.b.need_qs, true);
9b3c4ab3 1142 return 0;
d5f177d3
PM
1143}
1144
1145/* Attempt to extract the state for the specified task. */
1146static void trc_wait_for_one_reader(struct task_struct *t,
1147 struct list_head *bhp)
1148{
1149 int cpu;
1150
1151 // If a previous IPI is still in flight, let it complete.
1152 if (smp_load_acquire(&t->trc_ipi_to_cpu) != -1) // Order IPI
1153 return;
1154
1155 // The current task had better be in a quiescent state.
1156 if (t == current) {
1157 t->trc_reader_checked = true;
bdb0cca0 1158 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
d5f177d3
PM
1159 return;
1160 }
1161
1162 // Attempt to nail down the task for inspection.
1163 get_task_struct(t);
9b3c4ab3 1164 if (!task_call_func(t, trc_inspect_reader, NULL)) {
d5f177d3
PM
1165 put_task_struct(t);
1166 return;
1167 }
1168 put_task_struct(t);
1169
45f4b4a2
PM
1170 // If this task is not yet on the holdout list, then we are in
1171 // an RCU read-side critical section. Otherwise, the invocation of
d0a85858 1172 // trc_add_holdout() that added it to the list did the necessary
45f4b4a2
PM
1173 // get_task_struct(). Either way, the task cannot be freed out
1174 // from under this code.
1175
d5f177d3
PM
1176 // If currently running, send an IPI, either way, add to list.
1177 trc_add_holdout(t, bhp);
574de876
PM
1178 if (task_curr(t) &&
1179 time_after(jiffies + 1, rcu_tasks_trace.gp_start + rcu_task_ipi_delay)) {
d5f177d3
PM
1180 // The task is currently running, so try IPIing it.
1181 cpu = task_cpu(t);
1182
1183 // If there is already an IPI outstanding, let it happen.
1184 if (per_cpu(trc_ipi_to_cpu, cpu) || t->trc_ipi_to_cpu >= 0)
1185 return;
1186
d5f177d3
PM
1187 per_cpu(trc_ipi_to_cpu, cpu) = true;
1188 t->trc_ipi_to_cpu = cpu;
238dbce3 1189 rcu_tasks_trace.n_ipis++;
96017bf9 1190 if (smp_call_function_single(cpu, trc_read_check_handler, t, 0)) {
d5f177d3
PM
1191 // Just in case there is some other reason for
1192 // failure than the target CPU being offline.
46aa886c
NU
1193 WARN_ONCE(1, "%s(): smp_call_function_single() failed for CPU: %d\n",
1194 __func__, cpu);
7e0669c3 1195 rcu_tasks_trace.n_ipis_fails++;
d5f177d3 1196 per_cpu(trc_ipi_to_cpu, cpu) = false;
46aa886c 1197 t->trc_ipi_to_cpu = -1;
d5f177d3
PM
1198 }
1199 }
1200}
1201
1202/* Initialize for a new RCU-tasks-trace grace period. */
1203static void rcu_tasks_trace_pregp_step(void)
1204{
1205 int cpu;
1206
d5f177d3
PM
1207 // Allow for fast-acting IPIs.
1208 atomic_set(&trc_n_readers_need_end, 1);
1209
1210 // There shouldn't be any old IPIs, but...
1211 for_each_possible_cpu(cpu)
1212 WARN_ON_ONCE(per_cpu(trc_ipi_to_cpu, cpu));
81b4a7bc
PM
1213
1214 // Disable CPU hotplug across the tasklist scan.
1215 // This also waits for all readers in CPU-hotplug code paths.
1216 cpus_read_lock();
d5f177d3
PM
1217}
1218
1219/* Do first-round processing for the specified task. */
1220static void rcu_tasks_trace_pertask(struct task_struct *t,
1221 struct list_head *hop)
1222{
1b04fa99
URS
1223 // During early boot when there is only the one boot CPU, there
1224 // is no idle task for the other CPUs. Just return.
1225 if (unlikely(t == NULL))
1226 return;
1227
276c4104 1228 WRITE_ONCE(t->trc_reader_special.b.need_qs, false);
43766c3e 1229 WRITE_ONCE(t->trc_reader_checked, false);
d5f177d3
PM
1230 t->trc_ipi_to_cpu = -1;
1231 trc_wait_for_one_reader(t, hop);
1232}
1233
9796e1ae
PM
1234/*
1235 * Do intermediate processing between task and holdout scans and
1236 * pick up the idle tasks.
1237 */
1238static void rcu_tasks_trace_postscan(struct list_head *hop)
d5f177d3 1239{
9796e1ae
PM
1240 int cpu;
1241
1242 for_each_possible_cpu(cpu)
1243 rcu_tasks_trace_pertask(idle_task(cpu), hop);
1244
81b4a7bc
PM
1245 // Re-enable CPU hotplug now that the tasklist scan has completed.
1246 cpus_read_unlock();
1247
d5f177d3
PM
1248 // Wait for late-stage exiting tasks to finish exiting.
1249 // These might have passed the call to exit_tasks_rcu_finish().
1250 synchronize_rcu();
1251 // Any tasks that exit after this point will set ->trc_reader_checked.
1252}
1253
65b629e7
NU
1254/* Communicate task state back to the RCU tasks trace stall warning request. */
1255struct trc_stall_chk_rdr {
1256 int nesting;
1257 int ipi_to_cpu;
1258 u8 needqs;
1259};
1260
1261static int trc_check_slow_task(struct task_struct *t, void *arg)
1262{
1263 struct trc_stall_chk_rdr *trc_rdrp = arg;
1264
1265 if (task_curr(t))
1266 return false; // It is running, so decline to inspect it.
1267 trc_rdrp->nesting = READ_ONCE(t->trc_reader_nesting);
1268 trc_rdrp->ipi_to_cpu = READ_ONCE(t->trc_ipi_to_cpu);
1269 trc_rdrp->needqs = READ_ONCE(t->trc_reader_special.b.need_qs);
1270 return true;
1271}
1272
4593e772
PM
1273/* Show the state of a task stalling the current RCU tasks trace GP. */
1274static void show_stalled_task_trace(struct task_struct *t, bool *firstreport)
1275{
1276 int cpu;
65b629e7
NU
1277 struct trc_stall_chk_rdr trc_rdr;
1278 bool is_idle_tsk = is_idle_task(t);
4593e772
PM
1279
1280 if (*firstreport) {
1281 pr_err("INFO: rcu_tasks_trace detected stalls on tasks:\n");
1282 *firstreport = false;
1283 }
4593e772 1284 cpu = task_cpu(t);
65b629e7
NU
1285 if (!task_call_func(t, trc_check_slow_task, &trc_rdr))
1286 pr_alert("P%d: %c\n",
1287 t->pid,
1288 ".i"[is_idle_tsk]);
1289 else
1290 pr_alert("P%d: %c%c%c nesting: %d%c cpu: %d\n",
1291 t->pid,
1292 ".I"[trc_rdr.ipi_to_cpu >= 0],
1293 ".i"[is_idle_tsk],
1294 ".N"[cpu >= 0 && tick_nohz_full_cpu(cpu)],
1295 trc_rdr.nesting,
1296 " N"[!!trc_rdr.needqs],
1297 cpu);
4593e772
PM
1298 sched_show_task(t);
1299}
1300
1301/* List stalled IPIs for RCU tasks trace. */
1302static void show_stalled_ipi_trace(void)
1303{
1304 int cpu;
1305
1306 for_each_possible_cpu(cpu)
1307 if (per_cpu(trc_ipi_to_cpu, cpu))
1308 pr_alert("\tIPI outstanding to CPU %d\n", cpu);
1309}
1310
d5f177d3
PM
1311/* Do one scan of the holdout list. */
1312static void check_all_holdout_tasks_trace(struct list_head *hop,
4593e772 1313 bool needreport, bool *firstreport)
d5f177d3
PM
1314{
1315 struct task_struct *g, *t;
1316
81b4a7bc
PM
1317 // Disable CPU hotplug across the holdout list scan.
1318 cpus_read_lock();
1319
d5f177d3
PM
1320 list_for_each_entry_safe(t, g, hop, trc_holdout_list) {
1321 // If safe and needed, try to check the current task.
1322 if (READ_ONCE(t->trc_ipi_to_cpu) == -1 &&
1323 !READ_ONCE(t->trc_reader_checked))
1324 trc_wait_for_one_reader(t, hop);
1325
1326 // If check succeeded, remove this task from the list.
f5dbc594
PM
1327 if (smp_load_acquire(&t->trc_ipi_to_cpu) == -1 &&
1328 READ_ONCE(t->trc_reader_checked))
d5f177d3 1329 trc_del_holdout(t);
4593e772
PM
1330 else if (needreport)
1331 show_stalled_task_trace(t, firstreport);
1332 }
81b4a7bc
PM
1333
1334 // Re-enable CPU hotplug now that the holdout list scan has completed.
1335 cpus_read_unlock();
1336
4593e772 1337 if (needreport) {
89401176 1338 if (*firstreport)
4593e772
PM
1339 pr_err("INFO: rcu_tasks_trace detected stalls? (Late IPI?)\n");
1340 show_stalled_ipi_trace();
d5f177d3
PM
1341 }
1342}
1343
cbe0d8d9
PM
1344static void rcu_tasks_trace_empty_fn(void *unused)
1345{
1346}
1347
d5f177d3 1348/* Wait for grace period to complete and provide ordering. */
af051ca4 1349static void rcu_tasks_trace_postgp(struct rcu_tasks *rtp)
d5f177d3 1350{
cbe0d8d9 1351 int cpu;
4593e772
PM
1352 bool firstreport;
1353 struct task_struct *g, *t;
1354 LIST_HEAD(holdouts);
1355 long ret;
1356
cbe0d8d9
PM
1357 // Wait for any lingering IPI handlers to complete. Note that
1358 // if a CPU has gone offline or transitioned to userspace in the
1359 // meantime, all IPI handlers should have been drained beforehand.
1360 // Yes, this assumes that CPUs process IPIs in order. If that ever
1361 // changes, there will need to be a recheck and/or timed wait.
1362 for_each_online_cpu(cpu)
f5dbc594 1363 if (WARN_ON_ONCE(smp_load_acquire(per_cpu_ptr(&trc_ipi_to_cpu, cpu))))
cbe0d8d9
PM
1364 smp_call_function_single(cpu, rcu_tasks_trace_empty_fn, NULL, 1);
1365
d5f177d3
PM
1366 // Remove the safety count.
1367 smp_mb__before_atomic(); // Order vs. earlier atomics
1368 atomic_dec(&trc_n_readers_need_end);
1369 smp_mb__after_atomic(); // Order vs. later atomics
1370
1371 // Wait for readers.
af051ca4 1372 set_tasks_gp_state(rtp, RTGS_WAIT_READERS);
4593e772
PM
1373 for (;;) {
1374 ret = wait_event_idle_exclusive_timeout(
1375 trc_wait,
1376 atomic_read(&trc_n_readers_need_end) == 0,
1377 READ_ONCE(rcu_task_stall_timeout));
1378 if (ret)
1379 break; // Count reached zero.
af051ca4 1380 // Stall warning time, so make a list of the offenders.
f747c7e1 1381 rcu_read_lock();
4593e772 1382 for_each_process_thread(g, t)
276c4104 1383 if (READ_ONCE(t->trc_reader_special.b.need_qs))
4593e772 1384 trc_add_holdout(t, &holdouts);
f747c7e1 1385 rcu_read_unlock();
4593e772 1386 firstreport = true;
592031cc
PM
1387 list_for_each_entry_safe(t, g, &holdouts, trc_holdout_list) {
1388 if (READ_ONCE(t->trc_reader_special.b.need_qs))
4593e772 1389 show_stalled_task_trace(t, &firstreport);
592031cc
PM
1390 trc_del_holdout(t); // Release task_struct reference.
1391 }
4593e772
PM
1392 if (firstreport)
1393 pr_err("INFO: rcu_tasks_trace detected stalls? (Counter/taskslist mismatch?)\n");
1394 show_stalled_ipi_trace();
1395 pr_err("\t%d holdouts\n", atomic_read(&trc_n_readers_need_end));
1396 }
d5f177d3 1397 smp_mb(); // Caller's code must be ordered after wakeup.
43766c3e 1398 // Pairs with pretty much every ordering primitive.
d5f177d3
PM
1399}
1400
1401/* Report any needed quiescent state for this exiting task. */
25246fc8 1402static void exit_tasks_rcu_finish_trace(struct task_struct *t)
d5f177d3
PM
1403{
1404 WRITE_ONCE(t->trc_reader_checked, true);
bdb0cca0 1405 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
d5f177d3 1406 WRITE_ONCE(t->trc_reader_nesting, 0);
276c4104 1407 if (WARN_ON_ONCE(READ_ONCE(t->trc_reader_special.b.need_qs)))
a5c071cc 1408 rcu_read_unlock_trace_special(t);
d5f177d3
PM
1409}
1410
d5f177d3
PM
1411/**
1412 * call_rcu_tasks_trace() - Queue a callback trace task-based grace period
1413 * @rhp: structure to be used for queueing the RCU updates.
1414 * @func: actual callback function to be invoked after the grace period
1415 *
ed42c380
NU
1416 * The callback function will be invoked some time after a trace rcu-tasks
1417 * grace period elapses, in other words after all currently executing
1418 * trace rcu-tasks read-side critical sections have completed. These
1419 * read-side critical sections are delimited by calls to rcu_read_lock_trace()
1420 * and rcu_read_unlock_trace().
d5f177d3
PM
1421 *
1422 * See the description of call_rcu() for more detailed information on
1423 * memory ordering guarantees.
1424 */
1425void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func)
1426{
1427 call_rcu_tasks_generic(rhp, func, &rcu_tasks_trace);
1428}
1429EXPORT_SYMBOL_GPL(call_rcu_tasks_trace);
1430
1431/**
1432 * synchronize_rcu_tasks_trace - wait for a trace rcu-tasks grace period
1433 *
1434 * Control will return to the caller some time after a trace rcu-tasks
c7dcf810 1435 * grace period has elapsed, in other words after all currently executing
ed42c380 1436 * trace rcu-tasks read-side critical sections have elapsed. These read-side
c7dcf810
PM
1437 * critical sections are delimited by calls to rcu_read_lock_trace()
1438 * and rcu_read_unlock_trace().
d5f177d3
PM
1439 *
1440 * This is a very specialized primitive, intended only for a few uses in
1441 * tracing and other situations requiring manipulation of function preambles
1442 * and profiling hooks. The synchronize_rcu_tasks_trace() function is not
1443 * (yet) intended for heavy use from multiple CPUs.
1444 *
1445 * See the description of synchronize_rcu() for more detailed information
1446 * on memory ordering guarantees.
1447 */
1448void synchronize_rcu_tasks_trace(void)
1449{
1450 RCU_LOCKDEP_WARN(lock_is_held(&rcu_trace_lock_map), "Illegal synchronize_rcu_tasks_trace() in RCU Tasks Trace read-side critical section");
1451 synchronize_rcu_tasks_generic(&rcu_tasks_trace);
1452}
1453EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_trace);
1454
1455/**
1456 * rcu_barrier_tasks_trace - Wait for in-flight call_rcu_tasks_trace() callbacks.
1457 *
1458 * Although the current implementation is guaranteed to wait, it is not
1459 * obligated to, for example, if there are no pending callbacks.
1460 */
1461void rcu_barrier_tasks_trace(void)
1462{
ce9b1c66 1463 rcu_barrier_tasks_generic(&rcu_tasks_trace);
d5f177d3
PM
1464}
1465EXPORT_SYMBOL_GPL(rcu_barrier_tasks_trace);
1466
1467static int __init rcu_spawn_tasks_trace_kthread(void)
1468{
cafafd67 1469 cblist_init_generic(&rcu_tasks_trace);
2393a613 1470 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB)) {
4fe192df 1471 rcu_tasks_trace.gp_sleep = HZ / 10;
75dc2da5 1472 rcu_tasks_trace.init_fract = HZ / 10;
2393a613 1473 } else {
4fe192df
PM
1474 rcu_tasks_trace.gp_sleep = HZ / 200;
1475 if (rcu_tasks_trace.gp_sleep <= 0)
1476 rcu_tasks_trace.gp_sleep = 1;
75dc2da5 1477 rcu_tasks_trace.init_fract = HZ / 200;
2393a613
PM
1478 if (rcu_tasks_trace.init_fract <= 0)
1479 rcu_tasks_trace.init_fract = 1;
1480 }
d5f177d3
PM
1481 rcu_tasks_trace.pregp_func = rcu_tasks_trace_pregp_step;
1482 rcu_tasks_trace.pertask_func = rcu_tasks_trace_pertask;
1483 rcu_tasks_trace.postscan_func = rcu_tasks_trace_postscan;
1484 rcu_tasks_trace.holdouts_func = check_all_holdout_tasks_trace;
1485 rcu_tasks_trace.postgp_func = rcu_tasks_trace_postgp;
1486 rcu_spawn_tasks_kthread_generic(&rcu_tasks_trace);
1487 return 0;
1488}
d5f177d3 1489
27c0f144
PM
1490#if !defined(CONFIG_TINY_RCU)
1491void show_rcu_tasks_trace_gp_kthread(void)
e21408ce 1492{
40471509 1493 char buf[64];
e21408ce 1494
edf3775f
PM
1495 sprintf(buf, "N%d h:%lu/%lu/%lu", atomic_read(&trc_n_readers_need_end),
1496 data_race(n_heavy_reader_ofl_updates),
40471509
PM
1497 data_race(n_heavy_reader_updates),
1498 data_race(n_heavy_reader_attempts));
e21408ce
PM
1499 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_trace, buf);
1500}
27c0f144
PM
1501EXPORT_SYMBOL_GPL(show_rcu_tasks_trace_gp_kthread);
1502#endif // !defined(CONFIG_TINY_RCU)
e21408ce 1503
d5f177d3 1504#else /* #ifdef CONFIG_TASKS_TRACE_RCU */
25246fc8 1505static void exit_tasks_rcu_finish_trace(struct task_struct *t) { }
d5f177d3 1506#endif /* #else #ifdef CONFIG_TASKS_TRACE_RCU */
8fd8ca38 1507
8344496e 1508#ifndef CONFIG_TINY_RCU
e21408ce
PM
1509void show_rcu_tasks_gp_kthreads(void)
1510{
1511 show_rcu_tasks_classic_gp_kthread();
1512 show_rcu_tasks_rude_gp_kthread();
1513 show_rcu_tasks_trace_gp_kthread();
1514}
8344496e 1515#endif /* #ifndef CONFIG_TINY_RCU */
e21408ce 1516
bfba7ed0
URS
1517#ifdef CONFIG_PROVE_RCU
1518struct rcu_tasks_test_desc {
1519 struct rcu_head rh;
1520 const char *name;
1521 bool notrun;
1522};
1523
1524static struct rcu_tasks_test_desc tests[] = {
1525 {
1526 .name = "call_rcu_tasks()",
1527 /* If not defined, the test is skipped. */
1528 .notrun = !IS_ENABLED(CONFIG_TASKS_RCU),
1529 },
1530 {
1531 .name = "call_rcu_tasks_rude()",
1532 /* If not defined, the test is skipped. */
1533 .notrun = !IS_ENABLED(CONFIG_TASKS_RUDE_RCU),
1534 },
1535 {
1536 .name = "call_rcu_tasks_trace()",
1537 /* If not defined, the test is skipped. */
1538 .notrun = !IS_ENABLED(CONFIG_TASKS_TRACE_RCU)
1539 }
1540};
1541
1542static void test_rcu_tasks_callback(struct rcu_head *rhp)
1543{
1544 struct rcu_tasks_test_desc *rttd =
1545 container_of(rhp, struct rcu_tasks_test_desc, rh);
1546
1547 pr_info("Callback from %s invoked.\n", rttd->name);
1548
1549 rttd->notrun = true;
1550}
1551
1552static void rcu_tasks_initiate_self_tests(void)
1553{
1554 pr_info("Running RCU-tasks wait API self tests\n");
1555#ifdef CONFIG_TASKS_RCU
1556 synchronize_rcu_tasks();
1557 call_rcu_tasks(&tests[0].rh, test_rcu_tasks_callback);
1558#endif
1559
1560#ifdef CONFIG_TASKS_RUDE_RCU
1561 synchronize_rcu_tasks_rude();
1562 call_rcu_tasks_rude(&tests[1].rh, test_rcu_tasks_callback);
1563#endif
1564
1565#ifdef CONFIG_TASKS_TRACE_RCU
1566 synchronize_rcu_tasks_trace();
1567 call_rcu_tasks_trace(&tests[2].rh, test_rcu_tasks_callback);
1568#endif
1569}
1570
1571static int rcu_tasks_verify_self_tests(void)
1572{
1573 int ret = 0;
1574 int i;
1575
1576 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1577 if (!tests[i].notrun) { // still hanging.
1578 pr_err("%s has been failed.\n", tests[i].name);
1579 ret = -1;
1580 }
1581 }
1582
1583 if (ret)
1584 WARN_ON(1);
1585
1586 return ret;
1587}
1588late_initcall(rcu_tasks_verify_self_tests);
1589#else /* #ifdef CONFIG_PROVE_RCU */
1590static void rcu_tasks_initiate_self_tests(void) { }
1591#endif /* #else #ifdef CONFIG_PROVE_RCU */
1592
1b04fa99
URS
1593void __init rcu_init_tasks_generic(void)
1594{
1595#ifdef CONFIG_TASKS_RCU
1596 rcu_spawn_tasks_kthread();
1597#endif
1598
1599#ifdef CONFIG_TASKS_RUDE_RCU
1600 rcu_spawn_tasks_rude_kthread();
1601#endif
1602
1603#ifdef CONFIG_TASKS_TRACE_RCU
1604 rcu_spawn_tasks_trace_kthread();
1605#endif
bfba7ed0
URS
1606
1607 // Run the self-tests.
1608 rcu_tasks_initiate_self_tests();
1b04fa99
URS
1609}
1610
8fd8ca38
PM
1611#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
1612static inline void rcu_tasks_bootup_oddness(void) {}
1613#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */