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