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