blob: 30048db7aa49d050c97153ffee23999526f6bcb9 [file] [log] [blame]
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -08001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Task-based RCU implementations.
4 *
5 * Copyright (C) 2020 Paul E. McKenney
6 */
7
Paul E. McKenney8fd8ca32020-03-15 14:51:20 -07008#ifdef CONFIG_TASKS_RCU_GENERIC
Paul E. McKenney5873b8a2020-03-03 11:49:21 -08009
10////////////////////////////////////////////////////////////////////////
11//
12// Generic data structures.
13
14struct rcu_tasks;
15typedef void (*rcu_tasks_gp_func_t)(struct rcu_tasks *rtp);
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080016typedef void (*pregp_func_t)(void);
17typedef void (*pertask_func_t)(struct task_struct *t, struct list_head *hop);
Paul E. McKenney9796e1a2020-03-22 13:18:54 -070018typedef void (*postscan_func_t)(struct list_head *hop);
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080019typedef void (*holdouts_func_t)(struct list_head *hop, bool ndrpt, bool *frptp);
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070020typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -080021
Paul E. McKenney07e10512020-03-02 15:16:57 -080022/**
Paul E. McKenneycafafd62021-11-05 21:52:00 -070023 * struct rcu_tasks_percpu - Per-CPU component of definition for a Tasks-RCU-like mechanism.
Paul E. McKenney07e10512020-03-02 15:16:57 -080024 * @cbs_head: Head of callback list.
25 * @cbs_tail: Tail pointer for callback list.
Paul E. McKenneycafafd62021-11-05 21:52:00 -070026 * @cbs_pcpu_lock: Lock protecting per-CPU callback list.
27 */
28struct rcu_tasks_percpu {
29 struct rcu_head *cbs_head;
30 struct rcu_head **cbs_tail;
31 raw_spinlock_t cbs_pcpu_lock;
32};
33
34/**
35 * struct rcu_tasks - Definition for a Tasks-RCU-like mechanism.
Ingo Molnara616aec2021-03-22 22:29:10 -070036 * @cbs_wq: Wait queue allowing new callback to get kthread's attention.
Paul E. McKenneycafafd62021-11-05 21:52:00 -070037 * @cbs_gbl_lock: Lock protecting callback list.
Paul E. McKenney07e10512020-03-02 15:16:57 -080038 * @kthread_ptr: This flavor's grace-period/callback-invocation kthread.
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080039 * @gp_func: This flavor's grace-period-wait function.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070040 * @gp_state: Grace period's most recent state transition (debugging).
Paul E. McKenney4fe192d2020-09-09 22:05:41 -070041 * @gp_sleep: Per-grace-period sleep to prevent CPU-bound looping.
Paul E. McKenney2393a612020-09-09 21:36:34 -070042 * @init_fract: Initial backoff sleep interval.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070043 * @gp_jiffies: Time of last @gp_state transition.
44 * @gp_start: Most recent grace-period start in jiffies.
Paul E. McKenney238dbce2020-03-18 10:54:05 -070045 * @n_gps: Number of grace periods completed since boot.
46 * @n_ipis: Number of IPIs sent to encourage grace periods to end.
Paul E. McKenney7e0669c2020-03-25 14:36:05 -070047 * @n_ipis_fails: Number of IPI-send failures.
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080048 * @pregp_func: This flavor's pre-grace-period function (optional).
49 * @pertask_func: This flavor's per-task scan function (optional).
50 * @postscan_func: This flavor's post-task scan function (optional).
Lukas Bulwahn85b86992021-01-25 08:41:05 +010051 * @holdouts_func: This flavor's holdout-list scan function (optional).
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080052 * @postgp_func: This flavor's post-grace-period function (optional).
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080053 * @call_func: This flavor's call_rcu()-equivalent function.
Paul E. McKenneycafafd62021-11-05 21:52:00 -070054 * @rtpcpu: This flavor's rcu_tasks_percpu structure.
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080055 * @name: This flavor's textual name.
56 * @kname: This flavor's kthread name.
Paul E. McKenney07e10512020-03-02 15:16:57 -080057 */
58struct rcu_tasks {
Paul E. McKenney07e10512020-03-02 15:16:57 -080059 struct wait_queue_head cbs_wq;
Paul E. McKenneycafafd62021-11-05 21:52:00 -070060 raw_spinlock_t cbs_gbl_lock;
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070061 int gp_state;
Paul E. McKenney4fe192d2020-09-09 22:05:41 -070062 int gp_sleep;
Paul E. McKenney2393a612020-09-09 21:36:34 -070063 int init_fract;
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070064 unsigned long gp_jiffies;
Paul E. McKenney88092d02020-03-17 08:57:02 -070065 unsigned long gp_start;
Paul E. McKenney238dbce2020-03-18 10:54:05 -070066 unsigned long n_gps;
67 unsigned long n_ipis;
Paul E. McKenney7e0669c2020-03-25 14:36:05 -070068 unsigned long n_ipis_fails;
Paul E. McKenney07e10512020-03-02 15:16:57 -080069 struct task_struct *kthread_ptr;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080070 rcu_tasks_gp_func_t gp_func;
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080071 pregp_func_t pregp_func;
72 pertask_func_t pertask_func;
73 postscan_func_t postscan_func;
74 holdouts_func_t holdouts_func;
75 postgp_func_t postgp_func;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080076 call_rcu_func_t call_func;
Paul E. McKenneycafafd62021-11-05 21:52:00 -070077 struct rcu_tasks_percpu __percpu *rtpcpu;
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080078 char *name;
79 char *kname;
Paul E. McKenney07e10512020-03-02 15:16:57 -080080};
81
Paul E. McKenneycafafd62021-11-05 21:52:00 -070082#define DEFINE_RCU_TASKS(rt_name, gp, call, n) \
83static DEFINE_PER_CPU(struct rcu_tasks_percpu, rt_name ## __percpu) = { \
84 .cbs_pcpu_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name ## __percpu.cbs_pcpu_lock), \
85}; \
86static struct rcu_tasks rt_name = \
87{ \
88 .cbs_wq = __WAIT_QUEUE_HEAD_INITIALIZER(rt_name.cbs_wq), \
89 .cbs_gbl_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_gbl_lock), \
90 .gp_func = gp, \
91 .call_func = call, \
92 .rtpcpu = &rt_name ## __percpu, \
93 .name = n, \
94 .kname = #rt_name, \
Paul E. McKenney07e10512020-03-02 15:16:57 -080095}
96
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -080097/* Track exiting tasks in order to allow them to be waited for. */
98DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
99
Paul E. McKenneyb0afa0f2020-03-17 11:39:26 -0700100/* Avoid IPIing CPUs early in the grace period. */
Paul E. McKenney574de872020-09-09 21:51:09 -0700101#define RCU_TASK_IPI_DELAY (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) ? HZ / 2 : 0)
Paul E. McKenneyb0afa0f2020-03-17 11:39:26 -0700102static int rcu_task_ipi_delay __read_mostly = RCU_TASK_IPI_DELAY;
103module_param(rcu_task_ipi_delay, int, 0644);
104
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800105/* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
106#define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
107static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
108module_param(rcu_task_stall_timeout, int, 0644);
109
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700110/* RCU tasks grace-period state for debugging. */
111#define RTGS_INIT 0
112#define RTGS_WAIT_WAIT_CBS 1
113#define RTGS_WAIT_GP 2
114#define RTGS_PRE_WAIT_GP 3
115#define RTGS_SCAN_TASKLIST 4
116#define RTGS_POST_SCAN_TASKLIST 5
117#define RTGS_WAIT_SCAN_HOLDOUTS 6
118#define RTGS_SCAN_HOLDOUTS 7
119#define RTGS_POST_GP 8
120#define RTGS_WAIT_READERS 9
121#define RTGS_INVOKE_CBS 10
122#define RTGS_WAIT_CBS 11
Paul E. McKenney83444962020-05-28 20:03:48 -0700123#ifndef CONFIG_TINY_RCU
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700124static const char * const rcu_tasks_gp_state_names[] = {
125 "RTGS_INIT",
126 "RTGS_WAIT_WAIT_CBS",
127 "RTGS_WAIT_GP",
128 "RTGS_PRE_WAIT_GP",
129 "RTGS_SCAN_TASKLIST",
130 "RTGS_POST_SCAN_TASKLIST",
131 "RTGS_WAIT_SCAN_HOLDOUTS",
132 "RTGS_SCAN_HOLDOUTS",
133 "RTGS_POST_GP",
134 "RTGS_WAIT_READERS",
135 "RTGS_INVOKE_CBS",
136 "RTGS_WAIT_CBS",
137};
Paul E. McKenney83444962020-05-28 20:03:48 -0700138#endif /* #ifndef CONFIG_TINY_RCU */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700139
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800140////////////////////////////////////////////////////////////////////////
141//
142// Generic code.
143
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700144/* Record grace-period phase and time. */
145static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
146{
147 rtp->gp_state = newstate;
148 rtp->gp_jiffies = jiffies;
149}
150
Paul E. McKenney83444962020-05-28 20:03:48 -0700151#ifndef CONFIG_TINY_RCU
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700152/* Return state name. */
153static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
154{
155 int i = data_race(rtp->gp_state); // Let KCSAN detect update races
156 int j = READ_ONCE(i); // Prevent the compiler from reading twice
157
158 if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
159 return "???";
160 return rcu_tasks_gp_state_names[j];
161}
Paul E. McKenney83444962020-05-28 20:03:48 -0700162#endif /* #ifndef CONFIG_TINY_RCU */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700163
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700164// Initialize per-CPU callback lists for the specified flavor of
165// Tasks RCU.
166static void cblist_init_generic(struct rcu_tasks *rtp)
167{
168 int cpu;
169 unsigned long flags;
170
171 raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
172 for_each_possible_cpu(cpu) {
173 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
174
175 WARN_ON_ONCE(!rtpcp);
176 if (cpu)
177 raw_spin_lock_init(&rtpcp->cbs_pcpu_lock);
178 raw_spin_lock(&rtpcp->cbs_pcpu_lock); // irqs already disabled.
179 if (!WARN_ON_ONCE(rtpcp->cbs_tail))
180 rtpcp->cbs_tail = &rtpcp->cbs_head;
181 raw_spin_unlock(&rtpcp->cbs_pcpu_lock); // irqs remain disabled.
182 }
183 raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
184
185}
186
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800187// Enqueue a callback for the specified flavor of Tasks RCU.
188static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
189 struct rcu_tasks *rtp)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800190{
191 unsigned long flags;
192 bool needwake;
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700193 struct rcu_tasks_percpu *rtpcp;
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800194
195 rhp->next = NULL;
196 rhp->func = func;
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700197 local_irq_save(flags);
198 rtpcp = per_cpu_ptr(rtp->rtpcpu, 0 /* smp_processor_id() */);
199 raw_spin_lock(&rtpcp->cbs_pcpu_lock);
200 if (!rtpcp->cbs_tail) {
201 raw_spin_unlock(&rtpcp->cbs_pcpu_lock); // irqs remain disabled.
202 cblist_init_generic(rtp);
203 raw_spin_lock(&rtpcp->cbs_pcpu_lock); // irqs already disabled.
204 }
205 needwake = !rtpcp->cbs_head;
206 WRITE_ONCE(*rtpcp->cbs_tail, rhp);
207 rtpcp->cbs_tail = &rhp->next;
208 raw_spin_unlock_irqrestore(&rtpcp->cbs_pcpu_lock, flags);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800209 /* We can't create the thread unless interrupts are enabled. */
Paul E. McKenney07e10512020-03-02 15:16:57 -0800210 if (needwake && READ_ONCE(rtp->kthread_ptr))
211 wake_up(&rtp->cbs_wq);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800212}
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800213
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800214// Wait for a grace period for the specified flavor of Tasks RCU.
215static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800216{
217 /* Complain if the scheduler has not started. */
218 RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
219 "synchronize_rcu_tasks called too soon");
220
221 /* Wait for the grace period. */
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800222 wait_rcu_gp(rtp->call_func);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800223}
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800224
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800225/* RCU-tasks kthread that detects grace periods and invokes callbacks. */
226static int __noreturn rcu_tasks_kthread(void *arg)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800227{
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800228 unsigned long flags;
229 struct rcu_head *list;
230 struct rcu_head *next;
231 struct rcu_tasks *rtp = arg;
232
233 /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
234 housekeeping_affine(current, HK_FLAG_RCU);
235 WRITE_ONCE(rtp->kthread_ptr, current); // Let GPs start!
236
237 /*
238 * Each pass through the following loop makes one check for
239 * newly arrived callbacks, and, if there are some, waits for
240 * one RCU-tasks grace period and then invokes the callbacks.
241 * This loop is terminated by the system going down. ;-)
242 */
243 for (;;) {
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700244 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, 0); // for_each...
245
Paul E. McKenney0db7c322021-08-11 09:07:44 -0700246 set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800247
248 /* Pick up any new callbacks. */
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700249 raw_spin_lock_irqsave(&rtpcp->cbs_pcpu_lock, flags);
Paul E. McKenney43766c32020-03-16 20:38:29 -0700250 smp_mb__after_spinlock(); // Order updates vs. GP.
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700251 list = rtpcp->cbs_head;
252 rtpcp->cbs_head = NULL;
253 rtpcp->cbs_tail = &rtpcp->cbs_head;
254 raw_spin_unlock_irqrestore(&rtpcp->cbs_pcpu_lock, flags);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800255
256 /* If there were none, wait a bit and start over. */
257 if (!list) {
258 wait_event_interruptible(rtp->cbs_wq,
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700259 READ_ONCE(rtpcp->cbs_head));
260 if (!rtpcp->cbs_head) {
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800261 WARN_ON(signal_pending(current));
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700262 set_tasks_gp_state(rtp, RTGS_WAIT_WAIT_CBS);
Paul E. McKenneyea6eed92020-05-07 16:47:13 -0700263 schedule_timeout_idle(HZ/10);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800264 }
265 continue;
266 }
267
268 // Wait for one grace period.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700269 set_tasks_gp_state(rtp, RTGS_WAIT_GP);
Paul E. McKenney88092d02020-03-17 08:57:02 -0700270 rtp->gp_start = jiffies;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800271 rtp->gp_func(rtp);
Paul E. McKenney238dbce2020-03-18 10:54:05 -0700272 rtp->n_gps++;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800273
274 /* Invoke the callbacks. */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700275 set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800276 while (list) {
277 next = list->next;
278 local_bh_disable();
279 list->func(list);
280 local_bh_enable();
281 list = next;
282 cond_resched();
283 }
284 /* Paranoid sleep to keep this from entering a tight loop */
Paul E. McKenney4fe192d2020-09-09 22:05:41 -0700285 schedule_timeout_idle(rtp->gp_sleep);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800286 }
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800287}
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800288
Uladzislau Rezki (Sony)1b04fa92020-12-09 21:27:31 +0100289/* Spawn RCU-tasks grace-period kthread. */
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800290static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
291{
292 struct task_struct *t;
293
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -0800294 t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
295 if (WARN_ONCE(IS_ERR(t), "%s: Could not start %s grace-period kthread, OOM is now expected behavior\n", __func__, rtp->name))
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800296 return;
297 smp_mb(); /* Ensure others see full kthread. */
298}
299
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800300#ifndef CONFIG_TINY_RCU
301
302/*
303 * Print any non-default Tasks RCU settings.
304 */
305static void __init rcu_tasks_bootup_oddness(void)
306{
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700307#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800308 if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
309 pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700310#endif /* #ifdef CONFIG_TASKS_RCU */
311#ifdef CONFIG_TASKS_RCU
312 pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800313#endif /* #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800314#ifdef CONFIG_TASKS_RUDE_RCU
315 pr_info("\tRude variant of Tasks RCU enabled.\n");
316#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700317#ifdef CONFIG_TASKS_TRACE_RCU
318 pr_info("\tTracing variant of Tasks RCU enabled.\n");
319#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800320}
321
322#endif /* #ifndef CONFIG_TINY_RCU */
323
Paul E. McKenney83444962020-05-28 20:03:48 -0700324#ifndef CONFIG_TINY_RCU
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700325/* Dump out rcutorture-relevant state common to all RCU-tasks flavors. */
326static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
327{
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700328 struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, 0); // for_each...
Paul E. McKenney7e0669c2020-03-25 14:36:05 -0700329 pr_info("%s: %s(%d) since %lu g:%lu i:%lu/%lu %c%c %s\n",
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700330 rtp->kname,
Paul E. McKenney7e0669c2020-03-25 14:36:05 -0700331 tasks_gp_state_getname(rtp), data_race(rtp->gp_state),
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700332 jiffies - data_race(rtp->gp_jiffies),
Paul E. McKenney7e0669c2020-03-25 14:36:05 -0700333 data_race(rtp->n_gps),
334 data_race(rtp->n_ipis_fails), data_race(rtp->n_ipis),
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700335 ".k"[!!data_race(rtp->kthread_ptr)],
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700336 ".C"[!!data_race(rtpcp->cbs_head)],
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700337 s);
338}
Paul E. McKenney27c0f142020-09-15 17:08:03 -0700339#endif // #ifndef CONFIG_TINY_RCU
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700340
Paul E. McKenney25246fc2020-04-05 20:49:13 -0700341static void exit_tasks_rcu_finish_trace(struct task_struct *t);
342
343#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800344
345////////////////////////////////////////////////////////////////////////
346//
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800347// Shared code between task-list-scanning variants of Tasks RCU.
348
349/* Wait for one RCU-tasks grace period. */
350static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
351{
352 struct task_struct *g, *t;
353 unsigned long lastreport;
354 LIST_HEAD(holdouts);
355 int fract;
356
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700357 set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800358 rtp->pregp_func();
359
360 /*
361 * There were callbacks, so we need to wait for an RCU-tasks
362 * grace period. Start off by scanning the task list for tasks
363 * that are not already voluntarily blocked. Mark these tasks
364 * and make a list of them in holdouts.
365 */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700366 set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800367 rcu_read_lock();
368 for_each_process_thread(g, t)
369 rtp->pertask_func(t, &holdouts);
370 rcu_read_unlock();
371
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700372 set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
Paul E. McKenney9796e1a2020-03-22 13:18:54 -0700373 rtp->postscan_func(&holdouts);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800374
375 /*
376 * Each pass through the following loop scans the list of holdout
377 * tasks, removing any that are no longer holdouts. When the list
378 * is empty, we are done.
379 */
380 lastreport = jiffies;
381
Paul E. McKenney2393a612020-09-09 21:36:34 -0700382 // Start off with initial wait and slowly back off to 1 HZ wait.
383 fract = rtp->init_fract;
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800384
Paul E. McKenney77dc1742020-09-15 15:41:50 -0700385 while (!list_empty(&holdouts)) {
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800386 bool firstreport;
387 bool needreport;
388 int rtst;
389
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800390 /* Slowly back off waiting for holdouts */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700391 set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
Paul E. McKenney75dc2da2020-09-17 16:17:17 -0700392 schedule_timeout_idle(fract);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800393
Paul E. McKenney75dc2da2020-09-17 16:17:17 -0700394 if (fract < HZ)
395 fract++;
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800396
397 rtst = READ_ONCE(rcu_task_stall_timeout);
398 needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
399 if (needreport)
400 lastreport = jiffies;
401 firstreport = true;
402 WARN_ON(signal_pending(current));
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700403 set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800404 rtp->holdouts_func(&holdouts, needreport, &firstreport);
405 }
406
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700407 set_tasks_gp_state(rtp, RTGS_POST_GP);
408 rtp->postgp_func(rtp);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800409}
410
Paul E. McKenney25246fc2020-04-05 20:49:13 -0700411#endif /* #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU) */
412
413#ifdef CONFIG_TASKS_RCU
414
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800415////////////////////////////////////////////////////////////////////////
416//
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800417// Simple variant of RCU whose quiescent states are voluntary context
Paul E. McKenney8af9e2c2021-09-15 09:24:18 -0700418// switch, cond_resched_tasks_rcu_qs(), user-space execution, and idle.
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800419// As such, grace periods can take one good long time. There are no
420// read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
421// because this implementation is intended to get the system into a safe
422// state for some of the manipulations involved in tracing and the like.
423// Finally, this implementation does not support high call_rcu_tasks()
424// rates from multiple CPUs. If this is required, per-CPU callback lists
425// will be needed.
Paul E. McKenney06a3ec92021-03-04 14:41:47 -0800426//
427// The implementation uses rcu_tasks_wait_gp(), which relies on function
428// pointers in the rcu_tasks structure. The rcu_spawn_tasks_kthread()
429// function sets these function pointers up so that rcu_tasks_wait_gp()
430// invokes these functions in this order:
431//
432// rcu_tasks_pregp_step():
433// Invokes synchronize_rcu() in order to wait for all in-flight
434// t->on_rq and t->nvcsw transitions to complete. This works because
435// all such transitions are carried out with interrupts disabled.
436// rcu_tasks_pertask(), invoked on every non-idle task:
437// For every runnable non-idle task other than the current one, use
438// get_task_struct() to pin down that task, snapshot that task's
439// number of voluntary context switches, and add that task to the
440// holdout list.
441// rcu_tasks_postscan():
442// Invoke synchronize_srcu() to ensure that all tasks that were
443// in the process of exiting (and which thus might not know to
444// synchronize with this RCU Tasks grace period) have completed
445// exiting.
446// check_all_holdout_tasks(), repeatedly until holdout list is empty:
447// Scans the holdout list, attempting to identify a quiescent state
448// for each task on the list. If there is a quiescent state, the
449// corresponding task is removed from the holdout list.
450// rcu_tasks_postgp():
451// Invokes synchronize_rcu() in order to ensure that all prior
452// t->on_rq and t->nvcsw transitions are seen by all CPUs and tasks
453// to have happened before the end of this RCU Tasks grace period.
454// Again, this works because all such transitions are carried out
455// with interrupts disabled.
456//
457// For each exiting task, the exit_tasks_rcu_start() and
458// exit_tasks_rcu_finish() functions begin and end, respectively, the SRCU
459// read-side critical sections waited for by rcu_tasks_postscan().
460//
461// Pre-grace-period update-side code is ordered before the grace via the
462// ->cbs_lock and the smp_mb__after_spinlock(). Pre-grace-period read-side
463// code is ordered before the grace period via synchronize_rcu() call
464// in rcu_tasks_pregp_step() and by the scheduler's locks and interrupt
465// disabling.
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800466
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800467/* Pre-grace-period preparation. */
468static void rcu_tasks_pregp_step(void)
469{
470 /*
471 * Wait for all pre-existing t->on_rq and t->nvcsw transitions
472 * to complete. Invoking synchronize_rcu() suffices because all
473 * these transitions occur with interrupts disabled. Without this
474 * synchronize_rcu(), a read-side critical section that started
475 * before the grace period might be incorrectly seen as having
476 * started after the grace period.
477 *
478 * This synchronize_rcu() also dispenses with the need for a
479 * memory barrier on the first store to t->rcu_tasks_holdout,
480 * as it forces the store to happen after the beginning of the
481 * grace period.
482 */
483 synchronize_rcu();
484}
485
486/* Per-task initial processing. */
487static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
488{
489 if (t != current && READ_ONCE(t->on_rq) && !is_idle_task(t)) {
490 get_task_struct(t);
491 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
492 WRITE_ONCE(t->rcu_tasks_holdout, true);
493 list_add(&t->rcu_tasks_holdout_list, hop);
494 }
495}
496
497/* Processing between scanning taskslist and draining the holdout list. */
Paul E. McKenney04a3c5a2020-05-28 19:27:06 -0700498static void rcu_tasks_postscan(struct list_head *hop)
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800499{
500 /*
501 * Wait for tasks that are in the process of exiting. This
502 * does only part of the job, ensuring that all tasks that were
503 * previously exiting reach the point where they have disabled
504 * preemption, allowing the later synchronize_rcu() to finish
505 * the job.
506 */
507 synchronize_srcu(&tasks_rcu_exit_srcu);
508}
509
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800510/* See if tasks are still holding out, complain if so. */
511static void check_holdout_task(struct task_struct *t,
512 bool needreport, bool *firstreport)
513{
514 int cpu;
515
516 if (!READ_ONCE(t->rcu_tasks_holdout) ||
517 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
518 !READ_ONCE(t->on_rq) ||
519 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
520 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
521 WRITE_ONCE(t->rcu_tasks_holdout, false);
522 list_del_init(&t->rcu_tasks_holdout_list);
523 put_task_struct(t);
524 return;
525 }
526 rcu_request_urgent_qs_task(t);
527 if (!needreport)
528 return;
529 if (*firstreport) {
530 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
531 *firstreport = false;
532 }
533 cpu = task_cpu(t);
534 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
535 t, ".I"[is_idle_task(t)],
536 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
537 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
538 t->rcu_tasks_idle_cpu, cpu);
539 sched_show_task(t);
540}
541
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800542/* Scan the holdout lists for tasks no longer holding out. */
543static void check_all_holdout_tasks(struct list_head *hop,
544 bool needreport, bool *firstreport)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800545{
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800546 struct task_struct *t, *t1;
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800547
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800548 list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
549 check_holdout_task(t, needreport, firstreport);
550 cond_resched();
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800551 }
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800552}
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800553
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800554/* Finish off the Tasks-RCU grace period. */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700555static void rcu_tasks_postgp(struct rcu_tasks *rtp)
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800556{
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800557 /*
558 * Because ->on_rq and ->nvcsw are not guaranteed to have a full
559 * memory barriers prior to them in the schedule() path, memory
560 * reordering on other CPUs could cause their RCU-tasks read-side
561 * critical sections to extend past the end of the grace period.
562 * However, because these ->nvcsw updates are carried out with
563 * interrupts disabled, we can use synchronize_rcu() to force the
564 * needed ordering on all such CPUs.
565 *
566 * This synchronize_rcu() also confines all ->rcu_tasks_holdout
567 * accesses to be within the grace period, avoiding the need for
568 * memory barriers for ->rcu_tasks_holdout accesses.
569 *
570 * In addition, this synchronize_rcu() waits for exiting tasks
571 * to complete their final preempt_disable() region of execution,
572 * cleaning up after the synchronize_srcu() above.
573 */
574 synchronize_rcu();
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800575}
576
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800577void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -0800578DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800579
580/**
581 * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
582 * @rhp: structure to be used for queueing the RCU updates.
583 * @func: actual callback function to be invoked after the grace period
584 *
585 * The callback function will be invoked some time after a full grace
586 * period elapses, in other words after all currently executing RCU
587 * read-side critical sections have completed. call_rcu_tasks() assumes
588 * that the read-side critical sections end at a voluntary context
Paul E. McKenney8af9e2c2021-09-15 09:24:18 -0700589 * switch (not a preemption!), cond_resched_tasks_rcu_qs(), entry into idle,
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800590 * or transition to usermode execution. As such, there are no read-side
591 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
592 * this primitive is intended to determine that all tasks have passed
Ingo Molnara616aec2021-03-22 22:29:10 -0700593 * through a safe state, not so much for data-structure synchronization.
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800594 *
595 * See the description of call_rcu() for more detailed information on
596 * memory ordering guarantees.
597 */
598void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
599{
600 call_rcu_tasks_generic(rhp, func, &rcu_tasks);
601}
602EXPORT_SYMBOL_GPL(call_rcu_tasks);
603
604/**
605 * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
606 *
607 * Control will return to the caller some time after a full rcu-tasks
608 * grace period has elapsed, in other words after all currently
609 * executing rcu-tasks read-side critical sections have elapsed. These
610 * read-side critical sections are delimited by calls to schedule(),
611 * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
612 * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
613 *
614 * This is a very specialized primitive, intended only for a few uses in
615 * tracing and other situations requiring manipulation of function
616 * preambles and profiling hooks. The synchronize_rcu_tasks() function
617 * is not (yet) intended for heavy use from multiple CPUs.
618 *
619 * See the description of synchronize_rcu() for more detailed information
620 * on memory ordering guarantees.
621 */
622void synchronize_rcu_tasks(void)
623{
624 synchronize_rcu_tasks_generic(&rcu_tasks);
625}
626EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
627
628/**
629 * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
630 *
631 * Although the current implementation is guaranteed to wait, it is not
632 * obligated to, for example, if there are no pending callbacks.
633 */
634void rcu_barrier_tasks(void)
635{
636 /* There is only one callback queue, so this is easy. ;-) */
637 synchronize_rcu_tasks();
638}
639EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
640
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800641static int __init rcu_spawn_tasks_kthread(void)
642{
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700643 cblist_init_generic(&rcu_tasks);
Paul E. McKenney4fe192d2020-09-09 22:05:41 -0700644 rcu_tasks.gp_sleep = HZ / 10;
Paul E. McKenney75dc2da2020-09-17 16:17:17 -0700645 rcu_tasks.init_fract = HZ / 10;
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800646 rcu_tasks.pregp_func = rcu_tasks_pregp_step;
647 rcu_tasks.pertask_func = rcu_tasks_pertask;
648 rcu_tasks.postscan_func = rcu_tasks_postscan;
649 rcu_tasks.holdouts_func = check_all_holdout_tasks;
650 rcu_tasks.postgp_func = rcu_tasks_postgp;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800651 rcu_spawn_tasks_kthread_generic(&rcu_tasks);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800652 return 0;
653}
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800654
Paul E. McKenney27c0f142020-09-15 17:08:03 -0700655#if !defined(CONFIG_TINY_RCU)
656void show_rcu_tasks_classic_gp_kthread(void)
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700657{
658 show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
659}
Paul E. McKenney27c0f142020-09-15 17:08:03 -0700660EXPORT_SYMBOL_GPL(show_rcu_tasks_classic_gp_kthread);
661#endif // !defined(CONFIG_TINY_RCU)
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700662
Paul E. McKenney25246fc2020-04-05 20:49:13 -0700663/* Do the srcu_read_lock() for the above synchronize_srcu(). */
664void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
665{
666 preempt_disable();
667 current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
668 preempt_enable();
669}
670
671/* Do the srcu_read_unlock() for the above synchronize_srcu(). */
672void exit_tasks_rcu_finish(void) __releases(&tasks_rcu_exit_srcu)
673{
674 struct task_struct *t = current;
675
676 preempt_disable();
677 __srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
678 preempt_enable();
679 exit_tasks_rcu_finish_trace(t);
680}
681
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700682#else /* #ifdef CONFIG_TASKS_RCU */
Paul E. McKenney25246fc2020-04-05 20:49:13 -0700683void exit_tasks_rcu_start(void) { }
684void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700685#endif /* #else #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800686
687#ifdef CONFIG_TASKS_RUDE_RCU
688
689////////////////////////////////////////////////////////////////////////
690//
691// "Rude" variant of Tasks RCU, inspired by Steve Rostedt's trick of
692// passing an empty function to schedule_on_each_cpu(). This approach
Paul E. McKenneye4be1f42021-06-22 11:57:15 -0700693// provides an asynchronous call_rcu_tasks_rude() API and batching of
694// concurrent calls to the synchronous synchronize_rcu_tasks_rude() API.
Paul E. McKenney9fc98e32021-03-04 14:46:59 -0800695// This invokes schedule_on_each_cpu() in order to send IPIs far and wide
696// and induces otherwise unnecessary context switches on all online CPUs,
697// whether idle or not.
698//
699// Callback handling is provided by the rcu_tasks_kthread() function.
700//
701// Ordering is provided by the scheduler's context-switch code.
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800702
703// Empty function to allow workqueues to force a context switch.
704static void rcu_tasks_be_rude(struct work_struct *work)
705{
706}
707
708// Wait for one rude RCU-tasks grace period.
709static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
710{
Paul E. McKenney238dbce2020-03-18 10:54:05 -0700711 rtp->n_ipis += cpumask_weight(cpu_online_mask);
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800712 schedule_on_each_cpu(rcu_tasks_be_rude);
713}
714
715void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -0800716DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
717 "RCU Tasks Rude");
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800718
719/**
720 * call_rcu_tasks_rude() - Queue a callback rude task-based grace period
721 * @rhp: structure to be used for queueing the RCU updates.
722 * @func: actual callback function to be invoked after the grace period
723 *
724 * The callback function will be invoked some time after a full grace
725 * period elapses, in other words after all currently executing RCU
726 * read-side critical sections have completed. call_rcu_tasks_rude()
727 * assumes that the read-side critical sections end at context switch,
Paul E. McKenney8af9e2c2021-09-15 09:24:18 -0700728 * cond_resched_tasks_rcu_qs(), or transition to usermode execution (as
Neeraj Upadhyaya6517e92021-08-18 12:58:43 +0530729 * usermode execution is schedulable). As such, there are no read-side
730 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
731 * this primitive is intended to determine that all tasks have passed
732 * through a safe state, not so much for data-structure synchronization.
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800733 *
734 * See the description of call_rcu() for more detailed information on
735 * memory ordering guarantees.
736 */
737void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
738{
739 call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
740}
741EXPORT_SYMBOL_GPL(call_rcu_tasks_rude);
742
743/**
744 * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
745 *
746 * Control will return to the caller some time after a rude rcu-tasks
747 * grace period has elapsed, in other words after all currently
748 * executing rcu-tasks read-side critical sections have elapsed. These
749 * read-side critical sections are delimited by calls to schedule(),
Neeraj Upadhyaya6517e92021-08-18 12:58:43 +0530750 * cond_resched_tasks_rcu_qs(), userspace execution (which is a schedulable
751 * context), and (in theory, anyway) cond_resched().
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800752 *
753 * This is a very specialized primitive, intended only for a few uses in
754 * tracing and other situations requiring manipulation of function preambles
755 * and profiling hooks. The synchronize_rcu_tasks_rude() function is not
756 * (yet) intended for heavy use from multiple CPUs.
757 *
758 * See the description of synchronize_rcu() for more detailed information
759 * on memory ordering guarantees.
760 */
761void synchronize_rcu_tasks_rude(void)
762{
763 synchronize_rcu_tasks_generic(&rcu_tasks_rude);
764}
765EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
766
767/**
768 * rcu_barrier_tasks_rude - Wait for in-flight call_rcu_tasks_rude() callbacks.
769 *
770 * Although the current implementation is guaranteed to wait, it is not
771 * obligated to, for example, if there are no pending callbacks.
772 */
773void rcu_barrier_tasks_rude(void)
774{
775 /* There is only one callback queue, so this is easy. ;-) */
776 synchronize_rcu_tasks_rude();
777}
778EXPORT_SYMBOL_GPL(rcu_barrier_tasks_rude);
779
780static int __init rcu_spawn_tasks_rude_kthread(void)
781{
Paul E. McKenneycafafd62021-11-05 21:52:00 -0700782 cblist_init_generic(&rcu_tasks_rude);
Paul E. McKenney4fe192d2020-09-09 22:05:41 -0700783 rcu_tasks_rude.gp_sleep = HZ / 10;
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800784 rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
785 return 0;
786}
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800787
Paul E. McKenney27c0f142020-09-15 17:08:03 -0700788#if !defined(CONFIG_TINY_RCU)
789void show_rcu_tasks_rude_gp_kthread(void)
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700790{
791 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
792}
Paul E. McKenney27c0f142020-09-15 17:08:03 -0700793EXPORT_SYMBOL_GPL(show_rcu_tasks_rude_gp_kthread);
794#endif // !defined(CONFIG_TINY_RCU)
795#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700796
797////////////////////////////////////////////////////////////////////////
798//
799// Tracing variant of Tasks RCU. This variant is designed to be used
800// to protect tracing hooks, including those of BPF. This variant
801// therefore:
802//
803// 1. Has explicit read-side markers to allow finite grace periods
804// in the face of in-kernel loops for PREEMPT=n builds.
805//
806// 2. Protects code in the idle loop, exception entry/exit, and
807// CPU-hotplug code paths, similar to the capabilities of SRCU.
808//
Paul E. McKenneyc4f113a2021-08-05 09:54:45 -0700809// 3. Avoids expensive read-side instructions, having overhead similar
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700810// to that of Preemptible RCU.
811//
812// There are of course downsides. The grace-period code can send IPIs to
813// CPUs, even when those CPUs are in the idle loop or in nohz_full userspace.
814// It is necessary to scan the full tasklist, much as for Tasks RCU. There
815// is a single callback queue guarded by a single lock, again, much as for
816// Tasks RCU. If needed, these downsides can be at least partially remedied.
817//
818// Perhaps most important, this variant of RCU does not affect the vanilla
819// flavors, rcu_preempt and rcu_sched. The fact that RCU Tasks Trace
820// readers can operate from idle, offline, and exception entry/exit in no
821// way allows rcu_preempt and rcu_sched readers to also do so.
Paul E. McKenneya434dd12021-02-25 10:26:00 -0800822//
823// The implementation uses rcu_tasks_wait_gp(), which relies on function
824// pointers in the rcu_tasks structure. The rcu_spawn_tasks_trace_kthread()
825// function sets these function pointers up so that rcu_tasks_wait_gp()
826// invokes these functions in this order:
827//
828// rcu_tasks_trace_pregp_step():
829// Initialize the count of readers and block CPU-hotplug operations.
830// rcu_tasks_trace_pertask(), invoked on every non-idle task:
831// Initialize per-task state and attempt to identify an immediate
832// quiescent state for that task, or, failing that, attempt to
833// set that task's .need_qs flag so that task's next outermost
834// rcu_read_unlock_trace() will report the quiescent state (in which
835// case the count of readers is incremented). If both attempts fail,
Paul E. McKenney45f4b4a2021-05-24 11:26:53 -0700836// the task is added to a "holdout" list. Note that IPIs are used
837// to invoke trc_read_check_handler() in the context of running tasks
838// in order to avoid ordering overhead on common-case shared-variable
839// accessses.
Paul E. McKenneya434dd12021-02-25 10:26:00 -0800840// rcu_tasks_trace_postscan():
841// Initialize state and attempt to identify an immediate quiescent
842// state as above (but only for idle tasks), unblock CPU-hotplug
843// operations, and wait for an RCU grace period to avoid races with
844// tasks that are in the process of exiting.
845// check_all_holdout_tasks_trace(), repeatedly until holdout list is empty:
846// Scans the holdout list, attempting to identify a quiescent state
847// for each task on the list. If there is a quiescent state, the
848// corresponding task is removed from the holdout list.
849// rcu_tasks_trace_postgp():
850// Wait for the count of readers do drop to zero, reporting any stalls.
851// Also execute full memory barriers to maintain ordering with code
852// executing after the grace period.
853//
854// The exit_tasks_rcu_finish_trace() synchronizes with exiting tasks.
855//
856// Pre-grace-period update-side code is ordered before the grace
857// period via the ->cbs_lock and barriers in rcu_tasks_kthread().
858// Pre-grace-period read-side code is ordered before the grace period by
859// atomic_dec_and_test() of the count of readers (for IPIed readers) and by
860// scheduler context-switch ordering (for locked-down non-running readers).
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700861
862// The lockdep state must be outside of #ifdef to be useful.
863#ifdef CONFIG_DEBUG_LOCK_ALLOC
864static struct lock_class_key rcu_lock_trace_key;
865struct lockdep_map rcu_trace_lock_map =
866 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_trace", &rcu_lock_trace_key);
867EXPORT_SYMBOL_GPL(rcu_trace_lock_map);
868#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
869
870#ifdef CONFIG_TASKS_TRACE_RCU
871
Paul E. McKenney30d8aa52020-06-09 09:24:51 -0700872static atomic_t trc_n_readers_need_end; // Number of waited-for readers.
873static DECLARE_WAIT_QUEUE_HEAD(trc_wait); // List of holdout tasks.
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700874
875// Record outstanding IPIs to each CPU. No point in sending two...
876static DEFINE_PER_CPU(bool, trc_ipi_to_cpu);
877
Paul E. McKenney40471502020-03-22 13:34:34 -0700878// The number of detections of task quiescent state relying on
879// heavyweight readers executing explicit memory barriers.
Paul E. McKenney6731da92020-09-09 14:14:34 -0700880static unsigned long n_heavy_reader_attempts;
881static unsigned long n_heavy_reader_updates;
882static unsigned long n_heavy_reader_ofl_updates;
Paul E. McKenney40471502020-03-22 13:34:34 -0700883
Paul E. McKenneyb0afa0f2020-03-17 11:39:26 -0700884void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func);
885DEFINE_RCU_TASKS(rcu_tasks_trace, rcu_tasks_wait_gp, call_rcu_tasks_trace,
886 "RCU Tasks Trace");
887
Paul E. McKenneyb38f57c2020-03-20 14:29:08 -0700888/*
889 * This irq_work handler allows rcu_read_unlock_trace() to be invoked
890 * while the scheduler locks are held.
891 */
892static void rcu_read_unlock_iw(struct irq_work *iwp)
893{
894 wake_up(&trc_wait);
895}
896static DEFINE_IRQ_WORK(rcu_tasks_trace_iw, rcu_read_unlock_iw);
897
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700898/* If we are the last reader, wake up the grace-period kthread. */
Paul E. McKenneya5c071c2021-07-28 12:28:27 -0700899void rcu_read_unlock_trace_special(struct task_struct *t)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700900{
Paul E. McKenneyf8ab3fa2021-05-24 15:36:37 -0700901 int nq = READ_ONCE(t->trc_reader_special.b.need_qs);
Paul E. McKenney276c4102020-03-17 16:02:06 -0700902
Paul E. McKenney9ae58d72020-03-18 17:16:37 -0700903 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB) &&
904 t->trc_reader_special.b.need_mb)
Paul E. McKenney276c4102020-03-17 16:02:06 -0700905 smp_mb(); // Pairs with update-side barriers.
906 // Update .need_qs before ->trc_reader_nesting for irq/NMI handlers.
907 if (nq)
908 WRITE_ONCE(t->trc_reader_special.b.need_qs, false);
Paul E. McKenneya5c071c2021-07-28 12:28:27 -0700909 WRITE_ONCE(t->trc_reader_nesting, 0);
Paul E. McKenney276c4102020-03-17 16:02:06 -0700910 if (nq && atomic_dec_and_test(&trc_n_readers_need_end))
Paul E. McKenneyb38f57c2020-03-20 14:29:08 -0700911 irq_work_queue(&rcu_tasks_trace_iw);
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700912}
913EXPORT_SYMBOL_GPL(rcu_read_unlock_trace_special);
914
915/* Add a task to the holdout list, if it is not already on the list. */
916static void trc_add_holdout(struct task_struct *t, struct list_head *bhp)
917{
918 if (list_empty(&t->trc_holdout_list)) {
919 get_task_struct(t);
920 list_add(&t->trc_holdout_list, bhp);
921 }
922}
923
924/* Remove a task from the holdout list, if it is in fact present. */
925static void trc_del_holdout(struct task_struct *t)
926{
927 if (!list_empty(&t->trc_holdout_list)) {
928 list_del_init(&t->trc_holdout_list);
929 put_task_struct(t);
930 }
931}
932
933/* IPI handler to check task state. */
934static void trc_read_check_handler(void *t_in)
935{
936 struct task_struct *t = current;
937 struct task_struct *texp = t_in;
938
939 // If the task is no longer running on this CPU, leave.
940 if (unlikely(texp != t)) {
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700941 goto reset_ipi; // Already on holdout list, so will check later.
942 }
943
944 // If the task is not in a read-side critical section, and
945 // if this is the last reader, awaken the grace-period kthread.
Paul E. McKenneybdb0cca2021-05-24 12:48:18 -0700946 if (likely(!READ_ONCE(t->trc_reader_nesting))) {
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700947 WRITE_ONCE(t->trc_reader_checked, true);
948 goto reset_ipi;
949 }
Paul E. McKenneyba3a86e2020-09-14 15:44:37 -0700950 // If we are racing with an rcu_read_unlock_trace(), try again later.
Paul E. McKenney96017bf2021-07-28 10:53:41 -0700951 if (unlikely(READ_ONCE(t->trc_reader_nesting) < 0))
Paul E. McKenneyba3a86e2020-09-14 15:44:37 -0700952 goto reset_ipi;
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700953 WRITE_ONCE(t->trc_reader_checked, true);
954
955 // Get here if the task is in a read-side critical section. Set
956 // its state so that it will awaken the grace-period kthread upon
957 // exit from that critical section.
Paul E. McKenney96017bf2021-07-28 10:53:41 -0700958 atomic_inc(&trc_n_readers_need_end); // One more to wait on.
Paul E. McKenneyf8ab3fa2021-05-24 15:36:37 -0700959 WARN_ON_ONCE(READ_ONCE(t->trc_reader_special.b.need_qs));
Paul E. McKenney276c4102020-03-17 16:02:06 -0700960 WRITE_ONCE(t->trc_reader_special.b.need_qs, true);
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700961
962reset_ipi:
963 // Allow future IPIs to be sent on CPU and for task.
964 // Also order this IPI handler against any later manipulations of
965 // the intended task.
Liu Song8211e922021-06-30 22:08:02 +0800966 smp_store_release(per_cpu_ptr(&trc_ipi_to_cpu, smp_processor_id()), false); // ^^^
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700967 smp_store_release(&texp->trc_ipi_to_cpu, -1); // ^^^
968}
969
970/* Callback function for scheduler to check locked-down task. */
Peter Zijlstra9b3c4ab2021-09-21 21:54:32 +0200971static int trc_inspect_reader(struct task_struct *t, void *arg)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700972{
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700973 int cpu = task_cpu(t);
Paul E. McKenney18f08e72021-07-28 11:32:28 -0700974 int nesting;
Paul E. McKenney7e3b70e2020-03-22 11:24:58 -0700975 bool ofl = cpu_is_offline(cpu);
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700976
977 if (task_curr(t)) {
Paul E. McKenney30d8aa52020-06-09 09:24:51 -0700978 WARN_ON_ONCE(ofl && !is_idle_task(t));
Paul E. McKenney7e3b70e2020-03-22 11:24:58 -0700979
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700980 // If no chance of heavyweight readers, do it the hard way.
Paul E. McKenney7e3b70e2020-03-22 11:24:58 -0700981 if (!ofl && !IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB))
Peter Zijlstra9b3c4ab2021-09-21 21:54:32 +0200982 return -EINVAL;
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700983
984 // If heavyweight readers are enabled on the remote task,
985 // we can inspect its state despite its currently running.
986 // However, we cannot safely change its state.
Paul E. McKenney40471502020-03-22 13:34:34 -0700987 n_heavy_reader_attempts++;
Paul E. McKenney7e3b70e2020-03-22 11:24:58 -0700988 if (!ofl && // Check for "running" idle tasks on offline CPUs.
989 !rcu_dynticks_zero_in_eqs(cpu, &t->trc_reader_nesting))
Peter Zijlstra9b3c4ab2021-09-21 21:54:32 +0200990 return -EINVAL; // No quiescent state, do it the hard way.
Paul E. McKenney40471502020-03-22 13:34:34 -0700991 n_heavy_reader_updates++;
Paul E. McKenneyedf37752020-03-22 14:09:45 -0700992 if (ofl)
993 n_heavy_reader_ofl_updates++;
Paul E. McKenney18f08e72021-07-28 11:32:28 -0700994 nesting = 0;
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700995 } else {
Paul E. McKenneybdb0cca2021-05-24 12:48:18 -0700996 // The task is not running, so C-language access is safe.
Paul E. McKenney18f08e72021-07-28 11:32:28 -0700997 nesting = t->trc_reader_nesting;
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -0700998 }
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700999
Paul E. McKenney18f08e72021-07-28 11:32:28 -07001000 // If not exiting a read-side critical section, mark as checked
1001 // so that the grace-period kthread will remove it from the
1002 // holdout list.
1003 t->trc_reader_checked = nesting >= 0;
1004 if (nesting <= 0)
Linus Torvalds6fedc282021-11-01 20:25:38 -07001005 return nesting ? -EINVAL : 0; // If in QS, done, otherwise try again later.
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -07001006
1007 // The task is in a read-side critical section, so set up its
1008 // state so that it will awaken the grace-period kthread upon exit
1009 // from that critical section.
1010 atomic_inc(&trc_n_readers_need_end); // One more to wait on.
Paul E. McKenneyf8ab3fa2021-05-24 15:36:37 -07001011 WARN_ON_ONCE(READ_ONCE(t->trc_reader_special.b.need_qs));
Paul E. McKenney7d0c9c52020-03-19 15:33:12 -07001012 WRITE_ONCE(t->trc_reader_special.b.need_qs, true);
Peter Zijlstra9b3c4ab2021-09-21 21:54:32 +02001013 return 0;
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001014}
1015
1016/* Attempt to extract the state for the specified task. */
1017static void trc_wait_for_one_reader(struct task_struct *t,
1018 struct list_head *bhp)
1019{
1020 int cpu;
1021
1022 // If a previous IPI is still in flight, let it complete.
1023 if (smp_load_acquire(&t->trc_ipi_to_cpu) != -1) // Order IPI
1024 return;
1025
1026 // The current task had better be in a quiescent state.
1027 if (t == current) {
1028 t->trc_reader_checked = true;
Paul E. McKenneybdb0cca2021-05-24 12:48:18 -07001029 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001030 return;
1031 }
1032
1033 // Attempt to nail down the task for inspection.
1034 get_task_struct(t);
Peter Zijlstra9b3c4ab2021-09-21 21:54:32 +02001035 if (!task_call_func(t, trc_inspect_reader, NULL)) {
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001036 put_task_struct(t);
1037 return;
1038 }
1039 put_task_struct(t);
1040
Paul E. McKenney45f4b4a2021-05-24 11:26:53 -07001041 // If this task is not yet on the holdout list, then we are in
1042 // an RCU read-side critical section. Otherwise, the invocation of
Neeraj Upadhyayd0a85852021-08-18 12:58:39 +05301043 // trc_add_holdout() that added it to the list did the necessary
Paul E. McKenney45f4b4a2021-05-24 11:26:53 -07001044 // get_task_struct(). Either way, the task cannot be freed out
1045 // from under this code.
1046
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001047 // If currently running, send an IPI, either way, add to list.
1048 trc_add_holdout(t, bhp);
Paul E. McKenney574de872020-09-09 21:51:09 -07001049 if (task_curr(t) &&
1050 time_after(jiffies + 1, rcu_tasks_trace.gp_start + rcu_task_ipi_delay)) {
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001051 // The task is currently running, so try IPIing it.
1052 cpu = task_cpu(t);
1053
1054 // If there is already an IPI outstanding, let it happen.
1055 if (per_cpu(trc_ipi_to_cpu, cpu) || t->trc_ipi_to_cpu >= 0)
1056 return;
1057
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001058 per_cpu(trc_ipi_to_cpu, cpu) = true;
1059 t->trc_ipi_to_cpu = cpu;
Paul E. McKenney238dbce2020-03-18 10:54:05 -07001060 rcu_tasks_trace.n_ipis++;
Paul E. McKenney96017bf2021-07-28 10:53:41 -07001061 if (smp_call_function_single(cpu, trc_read_check_handler, t, 0)) {
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001062 // Just in case there is some other reason for
1063 // failure than the target CPU being offline.
Neeraj Upadhyay46aa8862021-08-27 13:43:35 +05301064 WARN_ONCE(1, "%s(): smp_call_function_single() failed for CPU: %d\n",
1065 __func__, cpu);
Paul E. McKenney7e0669c2020-03-25 14:36:05 -07001066 rcu_tasks_trace.n_ipis_fails++;
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001067 per_cpu(trc_ipi_to_cpu, cpu) = false;
Neeraj Upadhyay46aa8862021-08-27 13:43:35 +05301068 t->trc_ipi_to_cpu = -1;
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001069 }
1070 }
1071}
1072
1073/* Initialize for a new RCU-tasks-trace grace period. */
1074static void rcu_tasks_trace_pregp_step(void)
1075{
1076 int cpu;
1077
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001078 // Allow for fast-acting IPIs.
1079 atomic_set(&trc_n_readers_need_end, 1);
1080
1081 // There shouldn't be any old IPIs, but...
1082 for_each_possible_cpu(cpu)
1083 WARN_ON_ONCE(per_cpu(trc_ipi_to_cpu, cpu));
Paul E. McKenney81b4a7b2020-03-22 10:10:07 -07001084
1085 // Disable CPU hotplug across the tasklist scan.
1086 // This also waits for all readers in CPU-hotplug code paths.
1087 cpus_read_lock();
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001088}
1089
1090/* Do first-round processing for the specified task. */
1091static void rcu_tasks_trace_pertask(struct task_struct *t,
1092 struct list_head *hop)
1093{
Uladzislau Rezki (Sony)1b04fa92020-12-09 21:27:31 +01001094 // During early boot when there is only the one boot CPU, there
1095 // is no idle task for the other CPUs. Just return.
1096 if (unlikely(t == NULL))
1097 return;
1098
Paul E. McKenney276c4102020-03-17 16:02:06 -07001099 WRITE_ONCE(t->trc_reader_special.b.need_qs, false);
Paul E. McKenney43766c32020-03-16 20:38:29 -07001100 WRITE_ONCE(t->trc_reader_checked, false);
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001101 t->trc_ipi_to_cpu = -1;
1102 trc_wait_for_one_reader(t, hop);
1103}
1104
Paul E. McKenney9796e1a2020-03-22 13:18:54 -07001105/*
1106 * Do intermediate processing between task and holdout scans and
1107 * pick up the idle tasks.
1108 */
1109static void rcu_tasks_trace_postscan(struct list_head *hop)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001110{
Paul E. McKenney9796e1a2020-03-22 13:18:54 -07001111 int cpu;
1112
1113 for_each_possible_cpu(cpu)
1114 rcu_tasks_trace_pertask(idle_task(cpu), hop);
1115
Paul E. McKenney81b4a7b2020-03-22 10:10:07 -07001116 // Re-enable CPU hotplug now that the tasklist scan has completed.
1117 cpus_read_unlock();
1118
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001119 // Wait for late-stage exiting tasks to finish exiting.
1120 // These might have passed the call to exit_tasks_rcu_finish().
1121 synchronize_rcu();
1122 // Any tasks that exit after this point will set ->trc_reader_checked.
1123}
1124
Paul E. McKenney4593e772020-03-10 12:13:53 -07001125/* Show the state of a task stalling the current RCU tasks trace GP. */
1126static void show_stalled_task_trace(struct task_struct *t, bool *firstreport)
1127{
1128 int cpu;
1129
1130 if (*firstreport) {
1131 pr_err("INFO: rcu_tasks_trace detected stalls on tasks:\n");
1132 *firstreport = false;
1133 }
1134 // FIXME: This should attempt to use try_invoke_on_nonrunning_task().
1135 cpu = task_cpu(t);
1136 pr_alert("P%d: %c%c%c nesting: %d%c cpu: %d\n",
1137 t->pid,
Neeraj Upadhyayd39ec8f2021-08-18 12:58:41 +05301138 ".I"[READ_ONCE(t->trc_ipi_to_cpu) >= 0],
Paul E. McKenney4593e772020-03-10 12:13:53 -07001139 ".i"[is_idle_task(t)],
Neeraj Upadhyayd39ec8f2021-08-18 12:58:41 +05301140 ".N"[cpu >= 0 && tick_nohz_full_cpu(cpu)],
Paul E. McKenneybdb0cca2021-05-24 12:48:18 -07001141 READ_ONCE(t->trc_reader_nesting),
Paul E. McKenneyf8ab3fa2021-05-24 15:36:37 -07001142 " N"[!!READ_ONCE(t->trc_reader_special.b.need_qs)],
Paul E. McKenney4593e772020-03-10 12:13:53 -07001143 cpu);
1144 sched_show_task(t);
1145}
1146
1147/* List stalled IPIs for RCU tasks trace. */
1148static void show_stalled_ipi_trace(void)
1149{
1150 int cpu;
1151
1152 for_each_possible_cpu(cpu)
1153 if (per_cpu(trc_ipi_to_cpu, cpu))
1154 pr_alert("\tIPI outstanding to CPU %d\n", cpu);
1155}
1156
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001157/* Do one scan of the holdout list. */
1158static void check_all_holdout_tasks_trace(struct list_head *hop,
Paul E. McKenney4593e772020-03-10 12:13:53 -07001159 bool needreport, bool *firstreport)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001160{
1161 struct task_struct *g, *t;
1162
Paul E. McKenney81b4a7b2020-03-22 10:10:07 -07001163 // Disable CPU hotplug across the holdout list scan.
1164 cpus_read_lock();
1165
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001166 list_for_each_entry_safe(t, g, hop, trc_holdout_list) {
1167 // If safe and needed, try to check the current task.
1168 if (READ_ONCE(t->trc_ipi_to_cpu) == -1 &&
1169 !READ_ONCE(t->trc_reader_checked))
1170 trc_wait_for_one_reader(t, hop);
1171
1172 // If check succeeded, remove this task from the list.
Paul E. McKenneyf5dbc592021-09-18 20:40:48 -07001173 if (smp_load_acquire(&t->trc_ipi_to_cpu) == -1 &&
1174 READ_ONCE(t->trc_reader_checked))
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001175 trc_del_holdout(t);
Paul E. McKenney4593e772020-03-10 12:13:53 -07001176 else if (needreport)
1177 show_stalled_task_trace(t, firstreport);
1178 }
Paul E. McKenney81b4a7b2020-03-22 10:10:07 -07001179
1180 // Re-enable CPU hotplug now that the holdout list scan has completed.
1181 cpus_read_unlock();
1182
Paul E. McKenney4593e772020-03-10 12:13:53 -07001183 if (needreport) {
Neeraj Upadhyay89401172021-08-18 12:58:40 +05301184 if (*firstreport)
Paul E. McKenney4593e772020-03-10 12:13:53 -07001185 pr_err("INFO: rcu_tasks_trace detected stalls? (Late IPI?)\n");
1186 show_stalled_ipi_trace();
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001187 }
1188}
1189
Paul E. McKenneycbe0d8d2021-07-30 12:17:59 -07001190static void rcu_tasks_trace_empty_fn(void *unused)
1191{
1192}
1193
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001194/* Wait for grace period to complete and provide ordering. */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -07001195static void rcu_tasks_trace_postgp(struct rcu_tasks *rtp)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001196{
Paul E. McKenneycbe0d8d2021-07-30 12:17:59 -07001197 int cpu;
Paul E. McKenney4593e772020-03-10 12:13:53 -07001198 bool firstreport;
1199 struct task_struct *g, *t;
1200 LIST_HEAD(holdouts);
1201 long ret;
1202
Paul E. McKenneycbe0d8d2021-07-30 12:17:59 -07001203 // Wait for any lingering IPI handlers to complete. Note that
1204 // if a CPU has gone offline or transitioned to userspace in the
1205 // meantime, all IPI handlers should have been drained beforehand.
1206 // Yes, this assumes that CPUs process IPIs in order. If that ever
1207 // changes, there will need to be a recheck and/or timed wait.
1208 for_each_online_cpu(cpu)
Paul E. McKenneyf5dbc592021-09-18 20:40:48 -07001209 if (WARN_ON_ONCE(smp_load_acquire(per_cpu_ptr(&trc_ipi_to_cpu, cpu))))
Paul E. McKenneycbe0d8d2021-07-30 12:17:59 -07001210 smp_call_function_single(cpu, rcu_tasks_trace_empty_fn, NULL, 1);
1211
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001212 // Remove the safety count.
1213 smp_mb__before_atomic(); // Order vs. earlier atomics
1214 atomic_dec(&trc_n_readers_need_end);
1215 smp_mb__after_atomic(); // Order vs. later atomics
1216
1217 // Wait for readers.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -07001218 set_tasks_gp_state(rtp, RTGS_WAIT_READERS);
Paul E. McKenney4593e772020-03-10 12:13:53 -07001219 for (;;) {
1220 ret = wait_event_idle_exclusive_timeout(
1221 trc_wait,
1222 atomic_read(&trc_n_readers_need_end) == 0,
1223 READ_ONCE(rcu_task_stall_timeout));
1224 if (ret)
1225 break; // Count reached zero.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -07001226 // Stall warning time, so make a list of the offenders.
Paul E. McKenneyf747c7e2020-09-15 14:27:38 -07001227 rcu_read_lock();
Paul E. McKenney4593e772020-03-10 12:13:53 -07001228 for_each_process_thread(g, t)
Paul E. McKenney276c4102020-03-17 16:02:06 -07001229 if (READ_ONCE(t->trc_reader_special.b.need_qs))
Paul E. McKenney4593e772020-03-10 12:13:53 -07001230 trc_add_holdout(t, &holdouts);
Paul E. McKenneyf747c7e2020-09-15 14:27:38 -07001231 rcu_read_unlock();
Paul E. McKenney4593e772020-03-10 12:13:53 -07001232 firstreport = true;
Paul E. McKenney592031c2020-09-15 14:03:34 -07001233 list_for_each_entry_safe(t, g, &holdouts, trc_holdout_list) {
1234 if (READ_ONCE(t->trc_reader_special.b.need_qs))
Paul E. McKenney4593e772020-03-10 12:13:53 -07001235 show_stalled_task_trace(t, &firstreport);
Paul E. McKenney592031c2020-09-15 14:03:34 -07001236 trc_del_holdout(t); // Release task_struct reference.
1237 }
Paul E. McKenney4593e772020-03-10 12:13:53 -07001238 if (firstreport)
1239 pr_err("INFO: rcu_tasks_trace detected stalls? (Counter/taskslist mismatch?)\n");
1240 show_stalled_ipi_trace();
1241 pr_err("\t%d holdouts\n", atomic_read(&trc_n_readers_need_end));
1242 }
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001243 smp_mb(); // Caller's code must be ordered after wakeup.
Paul E. McKenney43766c32020-03-16 20:38:29 -07001244 // Pairs with pretty much every ordering primitive.
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001245}
1246
1247/* Report any needed quiescent state for this exiting task. */
Paul E. McKenney25246fc2020-04-05 20:49:13 -07001248static void exit_tasks_rcu_finish_trace(struct task_struct *t)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001249{
1250 WRITE_ONCE(t->trc_reader_checked, true);
Paul E. McKenneybdb0cca2021-05-24 12:48:18 -07001251 WARN_ON_ONCE(READ_ONCE(t->trc_reader_nesting));
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001252 WRITE_ONCE(t->trc_reader_nesting, 0);
Paul E. McKenney276c4102020-03-17 16:02:06 -07001253 if (WARN_ON_ONCE(READ_ONCE(t->trc_reader_special.b.need_qs)))
Paul E. McKenneya5c071c2021-07-28 12:28:27 -07001254 rcu_read_unlock_trace_special(t);
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001255}
1256
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001257/**
1258 * call_rcu_tasks_trace() - Queue a callback trace task-based grace period
1259 * @rhp: structure to be used for queueing the RCU updates.
1260 * @func: actual callback function to be invoked after the grace period
1261 *
Neeraj Upadhyayed42c382021-08-25 12:40:50 +05301262 * The callback function will be invoked some time after a trace rcu-tasks
1263 * grace period elapses, in other words after all currently executing
1264 * trace rcu-tasks read-side critical sections have completed. These
1265 * read-side critical sections are delimited by calls to rcu_read_lock_trace()
1266 * and rcu_read_unlock_trace().
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001267 *
1268 * See the description of call_rcu() for more detailed information on
1269 * memory ordering guarantees.
1270 */
1271void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func)
1272{
1273 call_rcu_tasks_generic(rhp, func, &rcu_tasks_trace);
1274}
1275EXPORT_SYMBOL_GPL(call_rcu_tasks_trace);
1276
1277/**
1278 * synchronize_rcu_tasks_trace - wait for a trace rcu-tasks grace period
1279 *
1280 * Control will return to the caller some time after a trace rcu-tasks
Paul E. McKenneyc7dcf812020-06-12 13:11:29 -07001281 * grace period has elapsed, in other words after all currently executing
Neeraj Upadhyayed42c382021-08-25 12:40:50 +05301282 * trace rcu-tasks read-side critical sections have elapsed. These read-side
Paul E. McKenneyc7dcf812020-06-12 13:11:29 -07001283 * critical sections are delimited by calls to rcu_read_lock_trace()
1284 * and rcu_read_unlock_trace().
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001285 *
1286 * This is a very specialized primitive, intended only for a few uses in
1287 * tracing and other situations requiring manipulation of function preambles
1288 * and profiling hooks. The synchronize_rcu_tasks_trace() function is not
1289 * (yet) intended for heavy use from multiple CPUs.
1290 *
1291 * See the description of synchronize_rcu() for more detailed information
1292 * on memory ordering guarantees.
1293 */
1294void synchronize_rcu_tasks_trace(void)
1295{
1296 RCU_LOCKDEP_WARN(lock_is_held(&rcu_trace_lock_map), "Illegal synchronize_rcu_tasks_trace() in RCU Tasks Trace read-side critical section");
1297 synchronize_rcu_tasks_generic(&rcu_tasks_trace);
1298}
1299EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_trace);
1300
1301/**
1302 * rcu_barrier_tasks_trace - Wait for in-flight call_rcu_tasks_trace() callbacks.
1303 *
1304 * Although the current implementation is guaranteed to wait, it is not
1305 * obligated to, for example, if there are no pending callbacks.
1306 */
1307void rcu_barrier_tasks_trace(void)
1308{
1309 /* There is only one callback queue, so this is easy. ;-) */
1310 synchronize_rcu_tasks_trace();
1311}
1312EXPORT_SYMBOL_GPL(rcu_barrier_tasks_trace);
1313
1314static int __init rcu_spawn_tasks_trace_kthread(void)
1315{
Paul E. McKenneycafafd62021-11-05 21:52:00 -07001316 cblist_init_generic(&rcu_tasks_trace);
Paul E. McKenney2393a612020-09-09 21:36:34 -07001317 if (IS_ENABLED(CONFIG_TASKS_TRACE_RCU_READ_MB)) {
Paul E. McKenney4fe192d2020-09-09 22:05:41 -07001318 rcu_tasks_trace.gp_sleep = HZ / 10;
Paul E. McKenney75dc2da2020-09-17 16:17:17 -07001319 rcu_tasks_trace.init_fract = HZ / 10;
Paul E. McKenney2393a612020-09-09 21:36:34 -07001320 } else {
Paul E. McKenney4fe192d2020-09-09 22:05:41 -07001321 rcu_tasks_trace.gp_sleep = HZ / 200;
1322 if (rcu_tasks_trace.gp_sleep <= 0)
1323 rcu_tasks_trace.gp_sleep = 1;
Paul E. McKenney75dc2da2020-09-17 16:17:17 -07001324 rcu_tasks_trace.init_fract = HZ / 200;
Paul E. McKenney2393a612020-09-09 21:36:34 -07001325 if (rcu_tasks_trace.init_fract <= 0)
1326 rcu_tasks_trace.init_fract = 1;
1327 }
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001328 rcu_tasks_trace.pregp_func = rcu_tasks_trace_pregp_step;
1329 rcu_tasks_trace.pertask_func = rcu_tasks_trace_pertask;
1330 rcu_tasks_trace.postscan_func = rcu_tasks_trace_postscan;
1331 rcu_tasks_trace.holdouts_func = check_all_holdout_tasks_trace;
1332 rcu_tasks_trace.postgp_func = rcu_tasks_trace_postgp;
1333 rcu_spawn_tasks_kthread_generic(&rcu_tasks_trace);
1334 return 0;
1335}
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001336
Paul E. McKenney27c0f142020-09-15 17:08:03 -07001337#if !defined(CONFIG_TINY_RCU)
1338void show_rcu_tasks_trace_gp_kthread(void)
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001339{
Paul E. McKenney40471502020-03-22 13:34:34 -07001340 char buf[64];
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001341
Paul E. McKenneyedf37752020-03-22 14:09:45 -07001342 sprintf(buf, "N%d h:%lu/%lu/%lu", atomic_read(&trc_n_readers_need_end),
1343 data_race(n_heavy_reader_ofl_updates),
Paul E. McKenney40471502020-03-22 13:34:34 -07001344 data_race(n_heavy_reader_updates),
1345 data_race(n_heavy_reader_attempts));
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001346 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_trace, buf);
1347}
Paul E. McKenney27c0f142020-09-15 17:08:03 -07001348EXPORT_SYMBOL_GPL(show_rcu_tasks_trace_gp_kthread);
1349#endif // !defined(CONFIG_TINY_RCU)
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001350
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001351#else /* #ifdef CONFIG_TASKS_TRACE_RCU */
Paul E. McKenney25246fc2020-04-05 20:49:13 -07001352static void exit_tasks_rcu_finish_trace(struct task_struct *t) { }
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001353#endif /* #else #ifdef CONFIG_TASKS_TRACE_RCU */
Paul E. McKenney8fd8ca32020-03-15 14:51:20 -07001354
Paul E. McKenney83444962020-05-28 20:03:48 -07001355#ifndef CONFIG_TINY_RCU
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001356void show_rcu_tasks_gp_kthreads(void)
1357{
1358 show_rcu_tasks_classic_gp_kthread();
1359 show_rcu_tasks_rude_gp_kthread();
1360 show_rcu_tasks_trace_gp_kthread();
1361}
Paul E. McKenney83444962020-05-28 20:03:48 -07001362#endif /* #ifndef CONFIG_TINY_RCU */
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001363
Uladzislau Rezki (Sony)bfba7ed2020-12-09 21:27:32 +01001364#ifdef CONFIG_PROVE_RCU
1365struct rcu_tasks_test_desc {
1366 struct rcu_head rh;
1367 const char *name;
1368 bool notrun;
1369};
1370
1371static struct rcu_tasks_test_desc tests[] = {
1372 {
1373 .name = "call_rcu_tasks()",
1374 /* If not defined, the test is skipped. */
1375 .notrun = !IS_ENABLED(CONFIG_TASKS_RCU),
1376 },
1377 {
1378 .name = "call_rcu_tasks_rude()",
1379 /* If not defined, the test is skipped. */
1380 .notrun = !IS_ENABLED(CONFIG_TASKS_RUDE_RCU),
1381 },
1382 {
1383 .name = "call_rcu_tasks_trace()",
1384 /* If not defined, the test is skipped. */
1385 .notrun = !IS_ENABLED(CONFIG_TASKS_TRACE_RCU)
1386 }
1387};
1388
1389static void test_rcu_tasks_callback(struct rcu_head *rhp)
1390{
1391 struct rcu_tasks_test_desc *rttd =
1392 container_of(rhp, struct rcu_tasks_test_desc, rh);
1393
1394 pr_info("Callback from %s invoked.\n", rttd->name);
1395
1396 rttd->notrun = true;
1397}
1398
1399static void rcu_tasks_initiate_self_tests(void)
1400{
1401 pr_info("Running RCU-tasks wait API self tests\n");
1402#ifdef CONFIG_TASKS_RCU
1403 synchronize_rcu_tasks();
1404 call_rcu_tasks(&tests[0].rh, test_rcu_tasks_callback);
1405#endif
1406
1407#ifdef CONFIG_TASKS_RUDE_RCU
1408 synchronize_rcu_tasks_rude();
1409 call_rcu_tasks_rude(&tests[1].rh, test_rcu_tasks_callback);
1410#endif
1411
1412#ifdef CONFIG_TASKS_TRACE_RCU
1413 synchronize_rcu_tasks_trace();
1414 call_rcu_tasks_trace(&tests[2].rh, test_rcu_tasks_callback);
1415#endif
1416}
1417
1418static int rcu_tasks_verify_self_tests(void)
1419{
1420 int ret = 0;
1421 int i;
1422
1423 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1424 if (!tests[i].notrun) { // still hanging.
1425 pr_err("%s has been failed.\n", tests[i].name);
1426 ret = -1;
1427 }
1428 }
1429
1430 if (ret)
1431 WARN_ON(1);
1432
1433 return ret;
1434}
1435late_initcall(rcu_tasks_verify_self_tests);
1436#else /* #ifdef CONFIG_PROVE_RCU */
1437static void rcu_tasks_initiate_self_tests(void) { }
1438#endif /* #else #ifdef CONFIG_PROVE_RCU */
1439
Uladzislau Rezki (Sony)1b04fa92020-12-09 21:27:31 +01001440void __init rcu_init_tasks_generic(void)
1441{
1442#ifdef CONFIG_TASKS_RCU
1443 rcu_spawn_tasks_kthread();
1444#endif
1445
1446#ifdef CONFIG_TASKS_RUDE_RCU
1447 rcu_spawn_tasks_rude_kthread();
1448#endif
1449
1450#ifdef CONFIG_TASKS_TRACE_RCU
1451 rcu_spawn_tasks_trace_kthread();
1452#endif
Uladzislau Rezki (Sony)bfba7ed2020-12-09 21:27:32 +01001453
1454 // Run the self-tests.
1455 rcu_tasks_initiate_self_tests();
Uladzislau Rezki (Sony)1b04fa92020-12-09 21:27:31 +01001456}
1457
Paul E. McKenney8fd8ca32020-03-15 14:51:20 -07001458#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
1459static inline void rcu_tasks_bootup_oddness(void) {}
1460#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */