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