blob: 6f8a4040fbddf147b5114bdbbb81d3f3c28087ee [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);
18typedef void (*postscan_func_t)(void);
19typedef 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/**
23 * Definition for a Tasks-RCU-like mechanism.
24 * @cbs_head: Head of callback list.
25 * @cbs_tail: Tail pointer for callback list.
26 * @cbs_wq: Wait queue allowning new callback to get kthread's attention.
27 * @cbs_lock: Lock protecting callback list.
28 * @kthread_ptr: This flavor's grace-period/callback-invocation kthread.
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080029 * @gp_func: This flavor's grace-period-wait function.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070030 * @gp_state: Grace period's most recent state transition (debugging).
31 * @gp_jiffies: Time of last @gp_state transition.
32 * @gp_start: Most recent grace-period start in jiffies.
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080033 * @pregp_func: This flavor's pre-grace-period function (optional).
34 * @pertask_func: This flavor's per-task scan function (optional).
35 * @postscan_func: This flavor's post-task scan function (optional).
36 * @holdout_func: This flavor's holdout-list scan function (optional).
37 * @postgp_func: This flavor's post-grace-period function (optional).
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080038 * @call_func: This flavor's call_rcu()-equivalent function.
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080039 * @name: This flavor's textual name.
40 * @kname: This flavor's kthread name.
Paul E. McKenney07e10512020-03-02 15:16:57 -080041 */
42struct rcu_tasks {
43 struct rcu_head *cbs_head;
44 struct rcu_head **cbs_tail;
45 struct wait_queue_head cbs_wq;
46 raw_spinlock_t cbs_lock;
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070047 int gp_state;
48 unsigned long gp_jiffies;
Paul E. McKenney07e10512020-03-02 15:16:57 -080049 struct task_struct *kthread_ptr;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080050 rcu_tasks_gp_func_t gp_func;
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -080051 pregp_func_t pregp_func;
52 pertask_func_t pertask_func;
53 postscan_func_t postscan_func;
54 holdouts_func_t holdouts_func;
55 postgp_func_t postgp_func;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080056 call_rcu_func_t call_func;
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080057 char *name;
58 char *kname;
Paul E. McKenney07e10512020-03-02 15:16:57 -080059};
60
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080061#define DEFINE_RCU_TASKS(rt_name, gp, call, n) \
62static struct rcu_tasks rt_name = \
Paul E. McKenney07e10512020-03-02 15:16:57 -080063{ \
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080064 .cbs_tail = &rt_name.cbs_head, \
65 .cbs_wq = __WAIT_QUEUE_HEAD_INITIALIZER(rt_name.cbs_wq), \
66 .cbs_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_lock), \
Paul E. McKenney5873b8a2020-03-03 11:49:21 -080067 .gp_func = gp, \
68 .call_func = call, \
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -080069 .name = n, \
70 .kname = #rt_name, \
Paul E. McKenney07e10512020-03-02 15:16:57 -080071}
72
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -080073/* Track exiting tasks in order to allow them to be waited for. */
74DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
75
76/* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
77#define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
78static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
79module_param(rcu_task_stall_timeout, int, 0644);
80
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -070081/* RCU tasks grace-period state for debugging. */
82#define RTGS_INIT 0
83#define RTGS_WAIT_WAIT_CBS 1
84#define RTGS_WAIT_GP 2
85#define RTGS_PRE_WAIT_GP 3
86#define RTGS_SCAN_TASKLIST 4
87#define RTGS_POST_SCAN_TASKLIST 5
88#define RTGS_WAIT_SCAN_HOLDOUTS 6
89#define RTGS_SCAN_HOLDOUTS 7
90#define RTGS_POST_GP 8
91#define RTGS_WAIT_READERS 9
92#define RTGS_INVOKE_CBS 10
93#define RTGS_WAIT_CBS 11
94static const char * const rcu_tasks_gp_state_names[] = {
95 "RTGS_INIT",
96 "RTGS_WAIT_WAIT_CBS",
97 "RTGS_WAIT_GP",
98 "RTGS_PRE_WAIT_GP",
99 "RTGS_SCAN_TASKLIST",
100 "RTGS_POST_SCAN_TASKLIST",
101 "RTGS_WAIT_SCAN_HOLDOUTS",
102 "RTGS_SCAN_HOLDOUTS",
103 "RTGS_POST_GP",
104 "RTGS_WAIT_READERS",
105 "RTGS_INVOKE_CBS",
106 "RTGS_WAIT_CBS",
107};
108
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800109////////////////////////////////////////////////////////////////////////
110//
111// Generic code.
112
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700113/* Record grace-period phase and time. */
114static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
115{
116 rtp->gp_state = newstate;
117 rtp->gp_jiffies = jiffies;
118}
119
120/* Return state name. */
121static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
122{
123 int i = data_race(rtp->gp_state); // Let KCSAN detect update races
124 int j = READ_ONCE(i); // Prevent the compiler from reading twice
125
126 if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
127 return "???";
128 return rcu_tasks_gp_state_names[j];
129}
130
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800131// Enqueue a callback for the specified flavor of Tasks RCU.
132static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
133 struct rcu_tasks *rtp)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800134{
135 unsigned long flags;
136 bool needwake;
137
138 rhp->next = NULL;
139 rhp->func = func;
Paul E. McKenney07e10512020-03-02 15:16:57 -0800140 raw_spin_lock_irqsave(&rtp->cbs_lock, flags);
141 needwake = !rtp->cbs_head;
142 WRITE_ONCE(*rtp->cbs_tail, rhp);
143 rtp->cbs_tail = &rhp->next;
144 raw_spin_unlock_irqrestore(&rtp->cbs_lock, flags);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800145 /* We can't create the thread unless interrupts are enabled. */
Paul E. McKenney07e10512020-03-02 15:16:57 -0800146 if (needwake && READ_ONCE(rtp->kthread_ptr))
147 wake_up(&rtp->cbs_wq);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800148}
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800149
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800150// Wait for a grace period for the specified flavor of Tasks RCU.
151static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800152{
153 /* Complain if the scheduler has not started. */
154 RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
155 "synchronize_rcu_tasks called too soon");
156
157 /* Wait for the grace period. */
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800158 wait_rcu_gp(rtp->call_func);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800159}
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800160
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800161/* RCU-tasks kthread that detects grace periods and invokes callbacks. */
162static int __noreturn rcu_tasks_kthread(void *arg)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800163{
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800164 unsigned long flags;
165 struct rcu_head *list;
166 struct rcu_head *next;
167 struct rcu_tasks *rtp = arg;
168
169 /* Run on housekeeping CPUs by default. Sysadm can move if desired. */
170 housekeeping_affine(current, HK_FLAG_RCU);
171 WRITE_ONCE(rtp->kthread_ptr, current); // Let GPs start!
172
173 /*
174 * Each pass through the following loop makes one check for
175 * newly arrived callbacks, and, if there are some, waits for
176 * one RCU-tasks grace period and then invokes the callbacks.
177 * This loop is terminated by the system going down. ;-)
178 */
179 for (;;) {
180
181 /* Pick up any new callbacks. */
182 raw_spin_lock_irqsave(&rtp->cbs_lock, flags);
Paul E. McKenney43766c32020-03-16 20:38:29 -0700183 smp_mb__after_spinlock(); // Order updates vs. GP.
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800184 list = rtp->cbs_head;
185 rtp->cbs_head = NULL;
186 rtp->cbs_tail = &rtp->cbs_head;
187 raw_spin_unlock_irqrestore(&rtp->cbs_lock, flags);
188
189 /* If there were none, wait a bit and start over. */
190 if (!list) {
191 wait_event_interruptible(rtp->cbs_wq,
192 READ_ONCE(rtp->cbs_head));
193 if (!rtp->cbs_head) {
194 WARN_ON(signal_pending(current));
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700195 set_tasks_gp_state(rtp, RTGS_WAIT_WAIT_CBS);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800196 schedule_timeout_interruptible(HZ/10);
197 }
198 continue;
199 }
200
201 // Wait for one grace period.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700202 set_tasks_gp_state(rtp, RTGS_WAIT_GP);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800203 rtp->gp_func(rtp);
204
205 /* Invoke the callbacks. */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700206 set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800207 while (list) {
208 next = list->next;
209 local_bh_disable();
210 list->func(list);
211 local_bh_enable();
212 list = next;
213 cond_resched();
214 }
215 /* Paranoid sleep to keep this from entering a tight loop */
216 schedule_timeout_uninterruptible(HZ/10);
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700217
218 set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800219 }
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800220}
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800221
222/* Spawn RCU-tasks grace-period kthread, e.g., at core_initcall() time. */
223static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
224{
225 struct task_struct *t;
226
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -0800227 t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
228 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 -0800229 return;
230 smp_mb(); /* Ensure others see full kthread. */
231}
232
233/* Do the srcu_read_lock() for the above synchronize_srcu(). */
234void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
235{
236 preempt_disable();
237 current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
238 preempt_enable();
239}
240
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700241static void exit_tasks_rcu_finish_trace(struct task_struct *t);
242
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800243/* Do the srcu_read_unlock() for the above synchronize_srcu(). */
244void exit_tasks_rcu_finish(void) __releases(&tasks_rcu_exit_srcu)
245{
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700246 struct task_struct *t = current;
247
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800248 preempt_disable();
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700249 __srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800250 preempt_enable();
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700251 exit_tasks_rcu_finish_trace(t);
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800252}
253
254#ifndef CONFIG_TINY_RCU
255
256/*
257 * Print any non-default Tasks RCU settings.
258 */
259static void __init rcu_tasks_bootup_oddness(void)
260{
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700261#if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800262 if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
263 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 -0700264#endif /* #ifdef CONFIG_TASKS_RCU */
265#ifdef CONFIG_TASKS_RCU
266 pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800267#endif /* #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800268#ifdef CONFIG_TASKS_RUDE_RCU
269 pr_info("\tRude variant of Tasks RCU enabled.\n");
270#endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700271#ifdef CONFIG_TASKS_TRACE_RCU
272 pr_info("\tTracing variant of Tasks RCU enabled.\n");
273#endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800274}
275
276#endif /* #ifndef CONFIG_TINY_RCU */
277
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700278/* Dump out rcutorture-relevant state common to all RCU-tasks flavors. */
279static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
280{
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700281 pr_info("%s: %s(%d) since %lu %c%c %s\n",
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700282 rtp->kname,
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700283 tasks_gp_state_getname(rtp),
284 data_race(rtp->gp_state),
285 jiffies - data_race(rtp->gp_jiffies),
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700286 ".k"[!!data_race(rtp->kthread_ptr)],
287 ".C"[!!data_race(rtp->cbs_head)],
288 s);
289}
290
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800291#ifdef CONFIG_TASKS_RCU
292
293////////////////////////////////////////////////////////////////////////
294//
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800295// Shared code between task-list-scanning variants of Tasks RCU.
296
297/* Wait for one RCU-tasks grace period. */
298static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
299{
300 struct task_struct *g, *t;
301 unsigned long lastreport;
302 LIST_HEAD(holdouts);
303 int fract;
304
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700305 set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800306 rtp->pregp_func();
307
308 /*
309 * There were callbacks, so we need to wait for an RCU-tasks
310 * grace period. Start off by scanning the task list for tasks
311 * that are not already voluntarily blocked. Mark these tasks
312 * and make a list of them in holdouts.
313 */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700314 set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800315 rcu_read_lock();
316 for_each_process_thread(g, t)
317 rtp->pertask_func(t, &holdouts);
318 rcu_read_unlock();
319
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700320 set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800321 rtp->postscan_func();
322
323 /*
324 * Each pass through the following loop scans the list of holdout
325 * tasks, removing any that are no longer holdouts. When the list
326 * is empty, we are done.
327 */
328 lastreport = jiffies;
329
330 /* Start off with HZ/10 wait and slowly back off to 1 HZ wait. */
331 fract = 10;
332
333 for (;;) {
334 bool firstreport;
335 bool needreport;
336 int rtst;
337
338 if (list_empty(&holdouts))
339 break;
340
341 /* Slowly back off waiting for holdouts */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700342 set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800343 schedule_timeout_interruptible(HZ/fract);
344
345 if (fract > 1)
346 fract--;
347
348 rtst = READ_ONCE(rcu_task_stall_timeout);
349 needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
350 if (needreport)
351 lastreport = jiffies;
352 firstreport = true;
353 WARN_ON(signal_pending(current));
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700354 set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800355 rtp->holdouts_func(&holdouts, needreport, &firstreport);
356 }
357
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700358 set_tasks_gp_state(rtp, RTGS_POST_GP);
359 rtp->postgp_func(rtp);
Paul E. McKenneyd01aa262020-03-05 17:07:07 -0800360}
361
362////////////////////////////////////////////////////////////////////////
363//
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800364// Simple variant of RCU whose quiescent states are voluntary context
365// switch, cond_resched_rcu_qs(), user-space execution, and idle.
366// As such, grace periods can take one good long time. There are no
367// read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
368// because this implementation is intended to get the system into a safe
369// state for some of the manipulations involved in tracing and the like.
370// Finally, this implementation does not support high call_rcu_tasks()
371// rates from multiple CPUs. If this is required, per-CPU callback lists
372// will be needed.
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800373
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800374/* Pre-grace-period preparation. */
375static void rcu_tasks_pregp_step(void)
376{
377 /*
378 * Wait for all pre-existing t->on_rq and t->nvcsw transitions
379 * to complete. Invoking synchronize_rcu() suffices because all
380 * these transitions occur with interrupts disabled. Without this
381 * synchronize_rcu(), a read-side critical section that started
382 * before the grace period might be incorrectly seen as having
383 * started after the grace period.
384 *
385 * This synchronize_rcu() also dispenses with the need for a
386 * memory barrier on the first store to t->rcu_tasks_holdout,
387 * as it forces the store to happen after the beginning of the
388 * grace period.
389 */
390 synchronize_rcu();
391}
392
393/* Per-task initial processing. */
394static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
395{
396 if (t != current && READ_ONCE(t->on_rq) && !is_idle_task(t)) {
397 get_task_struct(t);
398 t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
399 WRITE_ONCE(t->rcu_tasks_holdout, true);
400 list_add(&t->rcu_tasks_holdout_list, hop);
401 }
402}
403
404/* Processing between scanning taskslist and draining the holdout list. */
405void rcu_tasks_postscan(void)
406{
407 /*
408 * Wait for tasks that are in the process of exiting. This
409 * does only part of the job, ensuring that all tasks that were
410 * previously exiting reach the point where they have disabled
411 * preemption, allowing the later synchronize_rcu() to finish
412 * the job.
413 */
414 synchronize_srcu(&tasks_rcu_exit_srcu);
415}
416
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800417/* See if tasks are still holding out, complain if so. */
418static void check_holdout_task(struct task_struct *t,
419 bool needreport, bool *firstreport)
420{
421 int cpu;
422
423 if (!READ_ONCE(t->rcu_tasks_holdout) ||
424 t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
425 !READ_ONCE(t->on_rq) ||
426 (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
427 !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
428 WRITE_ONCE(t->rcu_tasks_holdout, false);
429 list_del_init(&t->rcu_tasks_holdout_list);
430 put_task_struct(t);
431 return;
432 }
433 rcu_request_urgent_qs_task(t);
434 if (!needreport)
435 return;
436 if (*firstreport) {
437 pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
438 *firstreport = false;
439 }
440 cpu = task_cpu(t);
441 pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
442 t, ".I"[is_idle_task(t)],
443 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
444 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
445 t->rcu_tasks_idle_cpu, cpu);
446 sched_show_task(t);
447}
448
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800449/* Scan the holdout lists for tasks no longer holding out. */
450static void check_all_holdout_tasks(struct list_head *hop,
451 bool needreport, bool *firstreport)
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800452{
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800453 struct task_struct *t, *t1;
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800454
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800455 list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
456 check_holdout_task(t, needreport, firstreport);
457 cond_resched();
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800458 }
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800459}
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800460
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800461/* Finish off the Tasks-RCU grace period. */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700462static void rcu_tasks_postgp(struct rcu_tasks *rtp)
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800463{
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800464 /*
465 * Because ->on_rq and ->nvcsw are not guaranteed to have a full
466 * memory barriers prior to them in the schedule() path, memory
467 * reordering on other CPUs could cause their RCU-tasks read-side
468 * critical sections to extend past the end of the grace period.
469 * However, because these ->nvcsw updates are carried out with
470 * interrupts disabled, we can use synchronize_rcu() to force the
471 * needed ordering on all such CPUs.
472 *
473 * This synchronize_rcu() also confines all ->rcu_tasks_holdout
474 * accesses to be within the grace period, avoiding the need for
475 * memory barriers for ->rcu_tasks_holdout accesses.
476 *
477 * In addition, this synchronize_rcu() waits for exiting tasks
478 * to complete their final preempt_disable() region of execution,
479 * cleaning up after the synchronize_srcu() above.
480 */
481 synchronize_rcu();
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800482}
483
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800484void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -0800485DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800486
487/**
488 * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
489 * @rhp: structure to be used for queueing the RCU updates.
490 * @func: actual callback function to be invoked after the grace period
491 *
492 * The callback function will be invoked some time after a full grace
493 * period elapses, in other words after all currently executing RCU
494 * read-side critical sections have completed. call_rcu_tasks() assumes
495 * that the read-side critical sections end at a voluntary context
496 * switch (not a preemption!), cond_resched_rcu_qs(), entry into idle,
497 * or transition to usermode execution. As such, there are no read-side
498 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
499 * this primitive is intended to determine that all tasks have passed
500 * through a safe state, not so much for data-strcuture synchronization.
501 *
502 * See the description of call_rcu() for more detailed information on
503 * memory ordering guarantees.
504 */
505void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
506{
507 call_rcu_tasks_generic(rhp, func, &rcu_tasks);
508}
509EXPORT_SYMBOL_GPL(call_rcu_tasks);
510
511/**
512 * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
513 *
514 * Control will return to the caller some time after a full rcu-tasks
515 * grace period has elapsed, in other words after all currently
516 * executing rcu-tasks read-side critical sections have elapsed. These
517 * read-side critical sections are delimited by calls to schedule(),
518 * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
519 * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
520 *
521 * This is a very specialized primitive, intended only for a few uses in
522 * tracing and other situations requiring manipulation of function
523 * preambles and profiling hooks. The synchronize_rcu_tasks() function
524 * is not (yet) intended for heavy use from multiple CPUs.
525 *
526 * See the description of synchronize_rcu() for more detailed information
527 * on memory ordering guarantees.
528 */
529void synchronize_rcu_tasks(void)
530{
531 synchronize_rcu_tasks_generic(&rcu_tasks);
532}
533EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
534
535/**
536 * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
537 *
538 * Although the current implementation is guaranteed to wait, it is not
539 * obligated to, for example, if there are no pending callbacks.
540 */
541void rcu_barrier_tasks(void)
542{
543 /* There is only one callback queue, so this is easy. ;-) */
544 synchronize_rcu_tasks();
545}
546EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
547
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800548static int __init rcu_spawn_tasks_kthread(void)
549{
Paul E. McKenneye4fe5dd2020-03-04 17:31:43 -0800550 rcu_tasks.pregp_func = rcu_tasks_pregp_step;
551 rcu_tasks.pertask_func = rcu_tasks_pertask;
552 rcu_tasks.postscan_func = rcu_tasks_postscan;
553 rcu_tasks.holdouts_func = check_all_holdout_tasks;
554 rcu_tasks.postgp_func = rcu_tasks_postgp;
Paul E. McKenney5873b8a2020-03-03 11:49:21 -0800555 rcu_spawn_tasks_kthread_generic(&rcu_tasks);
Paul E. McKenneyeacd6f02020-03-02 11:59:20 -0800556 return 0;
557}
558core_initcall(rcu_spawn_tasks_kthread);
559
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700560static void show_rcu_tasks_classic_gp_kthread(void)
561{
562 show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
563}
564
565#else /* #ifdef CONFIG_TASKS_RCU */
566static void show_rcu_tasks_classic_gp_kthread(void) { }
567#endif /* #else #ifdef CONFIG_TASKS_RCU */
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800568
569#ifdef CONFIG_TASKS_RUDE_RCU
570
571////////////////////////////////////////////////////////////////////////
572//
573// "Rude" variant of Tasks RCU, inspired by Steve Rostedt's trick of
574// passing an empty function to schedule_on_each_cpu(). This approach
575// provides an asynchronous call_rcu_tasks_rude() API and batching
576// of concurrent calls to the synchronous synchronize_rcu_rude() API.
577// This sends IPIs far and wide and induces otherwise unnecessary context
578// switches on all online CPUs, whether idle or not.
579
580// Empty function to allow workqueues to force a context switch.
581static void rcu_tasks_be_rude(struct work_struct *work)
582{
583}
584
585// Wait for one rude RCU-tasks grace period.
586static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
587{
588 schedule_on_each_cpu(rcu_tasks_be_rude);
589}
590
591void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
Paul E. McKenneyc97d12a2020-03-03 15:50:31 -0800592DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
593 "RCU Tasks Rude");
Paul E. McKenneyc84aad72020-03-02 21:06:43 -0800594
595/**
596 * call_rcu_tasks_rude() - Queue a callback rude task-based grace period
597 * @rhp: structure to be used for queueing the RCU updates.
598 * @func: actual callback function to be invoked after the grace period
599 *
600 * The callback function will be invoked some time after a full grace
601 * period elapses, in other words after all currently executing RCU
602 * read-side critical sections have completed. call_rcu_tasks_rude()
603 * assumes that the read-side critical sections end at context switch,
604 * cond_resched_rcu_qs(), or transition to usermode execution. As such,
605 * there are no read-side primitives analogous to rcu_read_lock() and
606 * rcu_read_unlock() because this primitive is intended to determine
607 * that all tasks have passed through a safe state, not so much for
608 * data-strcuture synchronization.
609 *
610 * See the description of call_rcu() for more detailed information on
611 * memory ordering guarantees.
612 */
613void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
614{
615 call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
616}
617EXPORT_SYMBOL_GPL(call_rcu_tasks_rude);
618
619/**
620 * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
621 *
622 * Control will return to the caller some time after a rude rcu-tasks
623 * grace period has elapsed, in other words after all currently
624 * executing rcu-tasks read-side critical sections have elapsed. These
625 * read-side critical sections are delimited by calls to schedule(),
626 * cond_resched_tasks_rcu_qs(), userspace execution, and (in theory,
627 * anyway) cond_resched().
628 *
629 * This is a very specialized primitive, intended only for a few uses in
630 * tracing and other situations requiring manipulation of function preambles
631 * and profiling hooks. The synchronize_rcu_tasks_rude() function is not
632 * (yet) intended for heavy use from multiple CPUs.
633 *
634 * See the description of synchronize_rcu() for more detailed information
635 * on memory ordering guarantees.
636 */
637void synchronize_rcu_tasks_rude(void)
638{
639 synchronize_rcu_tasks_generic(&rcu_tasks_rude);
640}
641EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
642
643/**
644 * rcu_barrier_tasks_rude - Wait for in-flight call_rcu_tasks_rude() callbacks.
645 *
646 * Although the current implementation is guaranteed to wait, it is not
647 * obligated to, for example, if there are no pending callbacks.
648 */
649void rcu_barrier_tasks_rude(void)
650{
651 /* There is only one callback queue, so this is easy. ;-) */
652 synchronize_rcu_tasks_rude();
653}
654EXPORT_SYMBOL_GPL(rcu_barrier_tasks_rude);
655
656static int __init rcu_spawn_tasks_rude_kthread(void)
657{
658 rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
659 return 0;
660}
661core_initcall(rcu_spawn_tasks_rude_kthread);
662
Paul E. McKenneye21408c2020-03-16 11:01:55 -0700663static void show_rcu_tasks_rude_gp_kthread(void)
664{
665 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
666}
667
668#else /* #ifdef CONFIG_TASKS_RUDE_RCU */
669static void show_rcu_tasks_rude_gp_kthread(void) {}
670#endif /* #else #ifdef CONFIG_TASKS_RUDE_RCU */
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700671
672////////////////////////////////////////////////////////////////////////
673//
674// Tracing variant of Tasks RCU. This variant is designed to be used
675// to protect tracing hooks, including those of BPF. This variant
676// therefore:
677//
678// 1. Has explicit read-side markers to allow finite grace periods
679// in the face of in-kernel loops for PREEMPT=n builds.
680//
681// 2. Protects code in the idle loop, exception entry/exit, and
682// CPU-hotplug code paths, similar to the capabilities of SRCU.
683//
684// 3. Avoids expensive read-side instruction, having overhead similar
685// to that of Preemptible RCU.
686//
687// There are of course downsides. The grace-period code can send IPIs to
688// CPUs, even when those CPUs are in the idle loop or in nohz_full userspace.
689// It is necessary to scan the full tasklist, much as for Tasks RCU. There
690// is a single callback queue guarded by a single lock, again, much as for
691// Tasks RCU. If needed, these downsides can be at least partially remedied.
692//
693// Perhaps most important, this variant of RCU does not affect the vanilla
694// flavors, rcu_preempt and rcu_sched. The fact that RCU Tasks Trace
695// readers can operate from idle, offline, and exception entry/exit in no
696// way allows rcu_preempt and rcu_sched readers to also do so.
697
698// The lockdep state must be outside of #ifdef to be useful.
699#ifdef CONFIG_DEBUG_LOCK_ALLOC
700static struct lock_class_key rcu_lock_trace_key;
701struct lockdep_map rcu_trace_lock_map =
702 STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_trace", &rcu_lock_trace_key);
703EXPORT_SYMBOL_GPL(rcu_trace_lock_map);
704#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
705
706#ifdef CONFIG_TASKS_TRACE_RCU
707
708atomic_t trc_n_readers_need_end; // Number of waited-for readers.
709DECLARE_WAIT_QUEUE_HEAD(trc_wait); // List of holdout tasks.
710
711// Record outstanding IPIs to each CPU. No point in sending two...
712static DEFINE_PER_CPU(bool, trc_ipi_to_cpu);
713
714/* If we are the last reader, wake up the grace-period kthread. */
715void rcu_read_unlock_trace_special(struct task_struct *t)
716{
717 WRITE_ONCE(t->trc_reader_need_end, false);
718 if (atomic_dec_and_test(&trc_n_readers_need_end))
719 wake_up(&trc_wait);
720}
721EXPORT_SYMBOL_GPL(rcu_read_unlock_trace_special);
722
723/* Add a task to the holdout list, if it is not already on the list. */
724static void trc_add_holdout(struct task_struct *t, struct list_head *bhp)
725{
726 if (list_empty(&t->trc_holdout_list)) {
727 get_task_struct(t);
728 list_add(&t->trc_holdout_list, bhp);
729 }
730}
731
732/* Remove a task from the holdout list, if it is in fact present. */
733static void trc_del_holdout(struct task_struct *t)
734{
735 if (!list_empty(&t->trc_holdout_list)) {
736 list_del_init(&t->trc_holdout_list);
737 put_task_struct(t);
738 }
739}
740
741/* IPI handler to check task state. */
742static void trc_read_check_handler(void *t_in)
743{
744 struct task_struct *t = current;
745 struct task_struct *texp = t_in;
746
747 // If the task is no longer running on this CPU, leave.
748 if (unlikely(texp != t)) {
749 if (WARN_ON_ONCE(atomic_dec_and_test(&trc_n_readers_need_end)))
750 wake_up(&trc_wait);
751 goto reset_ipi; // Already on holdout list, so will check later.
752 }
753
754 // If the task is not in a read-side critical section, and
755 // if this is the last reader, awaken the grace-period kthread.
756 if (likely(!t->trc_reader_nesting)) {
757 if (WARN_ON_ONCE(atomic_dec_and_test(&trc_n_readers_need_end)))
758 wake_up(&trc_wait);
759 // Mark as checked after decrement to avoid false
760 // positives on the above WARN_ON_ONCE().
761 WRITE_ONCE(t->trc_reader_checked, true);
762 goto reset_ipi;
763 }
764 WRITE_ONCE(t->trc_reader_checked, true);
765
766 // Get here if the task is in a read-side critical section. Set
767 // its state so that it will awaken the grace-period kthread upon
768 // exit from that critical section.
769 WARN_ON_ONCE(t->trc_reader_need_end);
770 WRITE_ONCE(t->trc_reader_need_end, true);
771
772reset_ipi:
773 // Allow future IPIs to be sent on CPU and for task.
774 // Also order this IPI handler against any later manipulations of
775 // the intended task.
776 smp_store_release(&per_cpu(trc_ipi_to_cpu, smp_processor_id()), false); // ^^^
777 smp_store_release(&texp->trc_ipi_to_cpu, -1); // ^^^
778}
779
780/* Callback function for scheduler to check locked-down task. */
781static bool trc_inspect_reader(struct task_struct *t, void *arg)
782{
783 if (task_curr(t))
784 return false; // It is running, so decline to inspect it.
785
786 // Mark as checked. Because this is called from the grace-period
787 // kthread, also remove the task from the holdout list.
788 t->trc_reader_checked = true;
789 trc_del_holdout(t);
790
791 // If the task is in a read-side critical section, set up its
792 // its state so that it will awaken the grace-period kthread upon
793 // exit from that critical section.
794 if (unlikely(t->trc_reader_nesting)) {
795 atomic_inc(&trc_n_readers_need_end); // One more to wait on.
796 WARN_ON_ONCE(t->trc_reader_need_end);
797 WRITE_ONCE(t->trc_reader_need_end, true);
798 }
799 return true;
800}
801
802/* Attempt to extract the state for the specified task. */
803static void trc_wait_for_one_reader(struct task_struct *t,
804 struct list_head *bhp)
805{
806 int cpu;
807
808 // If a previous IPI is still in flight, let it complete.
809 if (smp_load_acquire(&t->trc_ipi_to_cpu) != -1) // Order IPI
810 return;
811
812 // The current task had better be in a quiescent state.
813 if (t == current) {
814 t->trc_reader_checked = true;
815 trc_del_holdout(t);
816 WARN_ON_ONCE(t->trc_reader_nesting);
817 return;
818 }
819
820 // Attempt to nail down the task for inspection.
821 get_task_struct(t);
822 if (try_invoke_on_locked_down_task(t, trc_inspect_reader, NULL)) {
823 put_task_struct(t);
824 return;
825 }
826 put_task_struct(t);
827
828 // If currently running, send an IPI, either way, add to list.
829 trc_add_holdout(t, bhp);
830 if (task_curr(t) && time_after(jiffies, rcu_tasks_trace.gp_start + rcu_task_ipi_delay)) {
831 // The task is currently running, so try IPIing it.
832 cpu = task_cpu(t);
833
834 // If there is already an IPI outstanding, let it happen.
835 if (per_cpu(trc_ipi_to_cpu, cpu) || t->trc_ipi_to_cpu >= 0)
836 return;
837
838 atomic_inc(&trc_n_readers_need_end);
839 per_cpu(trc_ipi_to_cpu, cpu) = true;
840 t->trc_ipi_to_cpu = cpu;
841 if (smp_call_function_single(cpu,
842 trc_read_check_handler, t, 0)) {
843 // Just in case there is some other reason for
844 // failure than the target CPU being offline.
845 per_cpu(trc_ipi_to_cpu, cpu) = false;
846 t->trc_ipi_to_cpu = cpu;
847 if (atomic_dec_and_test(&trc_n_readers_need_end)) {
848 WARN_ON_ONCE(1);
849 wake_up(&trc_wait);
850 }
851 }
852 }
853}
854
855/* Initialize for a new RCU-tasks-trace grace period. */
856static void rcu_tasks_trace_pregp_step(void)
857{
858 int cpu;
859
860 // Wait for CPU-hotplug paths to complete.
861 cpus_read_lock();
862 cpus_read_unlock();
863
864 // Allow for fast-acting IPIs.
865 atomic_set(&trc_n_readers_need_end, 1);
866
867 // There shouldn't be any old IPIs, but...
868 for_each_possible_cpu(cpu)
869 WARN_ON_ONCE(per_cpu(trc_ipi_to_cpu, cpu));
870}
871
872/* Do first-round processing for the specified task. */
873static void rcu_tasks_trace_pertask(struct task_struct *t,
874 struct list_head *hop)
875{
876 WRITE_ONCE(t->trc_reader_need_end, false);
Paul E. McKenney43766c32020-03-16 20:38:29 -0700877 WRITE_ONCE(t->trc_reader_checked, false);
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700878 t->trc_ipi_to_cpu = -1;
879 trc_wait_for_one_reader(t, hop);
880}
881
882/* Do intermediate processing between task and holdout scans. */
883static void rcu_tasks_trace_postscan(void)
884{
885 // Wait for late-stage exiting tasks to finish exiting.
886 // These might have passed the call to exit_tasks_rcu_finish().
887 synchronize_rcu();
888 // Any tasks that exit after this point will set ->trc_reader_checked.
889}
890
Paul E. McKenney4593e772020-03-10 12:13:53 -0700891/* Show the state of a task stalling the current RCU tasks trace GP. */
892static void show_stalled_task_trace(struct task_struct *t, bool *firstreport)
893{
894 int cpu;
895
896 if (*firstreport) {
897 pr_err("INFO: rcu_tasks_trace detected stalls on tasks:\n");
898 *firstreport = false;
899 }
900 // FIXME: This should attempt to use try_invoke_on_nonrunning_task().
901 cpu = task_cpu(t);
902 pr_alert("P%d: %c%c%c nesting: %d%c cpu: %d\n",
903 t->pid,
904 ".I"[READ_ONCE(t->trc_ipi_to_cpu) > 0],
905 ".i"[is_idle_task(t)],
906 ".N"[cpu > 0 && tick_nohz_full_cpu(cpu)],
907 t->trc_reader_nesting,
908 " N"[!!t->trc_reader_need_end],
909 cpu);
910 sched_show_task(t);
911}
912
913/* List stalled IPIs for RCU tasks trace. */
914static void show_stalled_ipi_trace(void)
915{
916 int cpu;
917
918 for_each_possible_cpu(cpu)
919 if (per_cpu(trc_ipi_to_cpu, cpu))
920 pr_alert("\tIPI outstanding to CPU %d\n", cpu);
921}
922
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700923/* Do one scan of the holdout list. */
924static void check_all_holdout_tasks_trace(struct list_head *hop,
Paul E. McKenney4593e772020-03-10 12:13:53 -0700925 bool needreport, bool *firstreport)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700926{
927 struct task_struct *g, *t;
928
929 list_for_each_entry_safe(t, g, hop, trc_holdout_list) {
930 // If safe and needed, try to check the current task.
931 if (READ_ONCE(t->trc_ipi_to_cpu) == -1 &&
932 !READ_ONCE(t->trc_reader_checked))
933 trc_wait_for_one_reader(t, hop);
934
935 // If check succeeded, remove this task from the list.
936 if (READ_ONCE(t->trc_reader_checked))
937 trc_del_holdout(t);
Paul E. McKenney4593e772020-03-10 12:13:53 -0700938 else if (needreport)
939 show_stalled_task_trace(t, firstreport);
940 }
941 if (needreport) {
942 if (firstreport)
943 pr_err("INFO: rcu_tasks_trace detected stalls? (Late IPI?)\n");
944 show_stalled_ipi_trace();
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700945 }
946}
947
948/* Wait for grace period to complete and provide ordering. */
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700949static void rcu_tasks_trace_postgp(struct rcu_tasks *rtp)
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700950{
Paul E. McKenney4593e772020-03-10 12:13:53 -0700951 bool firstreport;
952 struct task_struct *g, *t;
953 LIST_HEAD(holdouts);
954 long ret;
955
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700956 // Remove the safety count.
957 smp_mb__before_atomic(); // Order vs. earlier atomics
958 atomic_dec(&trc_n_readers_need_end);
959 smp_mb__after_atomic(); // Order vs. later atomics
960
961 // Wait for readers.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700962 set_tasks_gp_state(rtp, RTGS_WAIT_READERS);
Paul E. McKenney4593e772020-03-10 12:13:53 -0700963 for (;;) {
964 ret = wait_event_idle_exclusive_timeout(
965 trc_wait,
966 atomic_read(&trc_n_readers_need_end) == 0,
967 READ_ONCE(rcu_task_stall_timeout));
968 if (ret)
969 break; // Count reached zero.
Paul E. McKenneyaf051ca2020-03-16 12:13:33 -0700970 // Stall warning time, so make a list of the offenders.
Paul E. McKenney4593e772020-03-10 12:13:53 -0700971 for_each_process_thread(g, t)
972 if (READ_ONCE(t->trc_reader_need_end))
973 trc_add_holdout(t, &holdouts);
974 firstreport = true;
975 list_for_each_entry_safe(t, g, &holdouts, trc_holdout_list)
976 if (READ_ONCE(t->trc_reader_need_end)) {
977 show_stalled_task_trace(t, &firstreport);
978 trc_del_holdout(t);
979 }
980 if (firstreport)
981 pr_err("INFO: rcu_tasks_trace detected stalls? (Counter/taskslist mismatch?)\n");
982 show_stalled_ipi_trace();
983 pr_err("\t%d holdouts\n", atomic_read(&trc_n_readers_need_end));
984 }
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700985 smp_mb(); // Caller's code must be ordered after wakeup.
Paul E. McKenney43766c32020-03-16 20:38:29 -0700986 // Pairs with pretty much every ordering primitive.
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -0700987}
988
989/* Report any needed quiescent state for this exiting task. */
990void exit_tasks_rcu_finish_trace(struct task_struct *t)
991{
992 WRITE_ONCE(t->trc_reader_checked, true);
993 WARN_ON_ONCE(t->trc_reader_nesting);
994 WRITE_ONCE(t->trc_reader_nesting, 0);
995 if (WARN_ON_ONCE(READ_ONCE(t->trc_reader_need_end)))
996 rcu_read_unlock_trace_special(t);
997}
998
999void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func);
1000DEFINE_RCU_TASKS(rcu_tasks_trace, rcu_tasks_wait_gp, call_rcu_tasks_trace,
1001 "RCU Tasks Trace");
1002
1003/**
1004 * call_rcu_tasks_trace() - Queue a callback trace task-based grace period
1005 * @rhp: structure to be used for queueing the RCU updates.
1006 * @func: actual callback function to be invoked after the grace period
1007 *
1008 * The callback function will be invoked some time after a full grace
1009 * period elapses, in other words after all currently executing RCU
1010 * read-side critical sections have completed. call_rcu_tasks_trace()
1011 * assumes that the read-side critical sections end at context switch,
1012 * cond_resched_rcu_qs(), or transition to usermode execution. As such,
1013 * there are no read-side primitives analogous to rcu_read_lock() and
1014 * rcu_read_unlock() because this primitive is intended to determine
1015 * that all tasks have passed through a safe state, not so much for
1016 * data-strcuture synchronization.
1017 *
1018 * See the description of call_rcu() for more detailed information on
1019 * memory ordering guarantees.
1020 */
1021void call_rcu_tasks_trace(struct rcu_head *rhp, rcu_callback_t func)
1022{
1023 call_rcu_tasks_generic(rhp, func, &rcu_tasks_trace);
1024}
1025EXPORT_SYMBOL_GPL(call_rcu_tasks_trace);
1026
1027/**
1028 * synchronize_rcu_tasks_trace - wait for a trace rcu-tasks grace period
1029 *
1030 * Control will return to the caller some time after a trace rcu-tasks
1031 * grace period has elapsed, in other words after all currently
1032 * executing rcu-tasks read-side critical sections have elapsed. These
1033 * read-side critical sections are delimited by calls to schedule(),
1034 * cond_resched_tasks_rcu_qs(), userspace execution, and (in theory,
1035 * anyway) cond_resched().
1036 *
1037 * This is a very specialized primitive, intended only for a few uses in
1038 * tracing and other situations requiring manipulation of function preambles
1039 * and profiling hooks. The synchronize_rcu_tasks_trace() function is not
1040 * (yet) intended for heavy use from multiple CPUs.
1041 *
1042 * See the description of synchronize_rcu() for more detailed information
1043 * on memory ordering guarantees.
1044 */
1045void synchronize_rcu_tasks_trace(void)
1046{
1047 RCU_LOCKDEP_WARN(lock_is_held(&rcu_trace_lock_map), "Illegal synchronize_rcu_tasks_trace() in RCU Tasks Trace read-side critical section");
1048 synchronize_rcu_tasks_generic(&rcu_tasks_trace);
1049}
1050EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_trace);
1051
1052/**
1053 * rcu_barrier_tasks_trace - Wait for in-flight call_rcu_tasks_trace() callbacks.
1054 *
1055 * Although the current implementation is guaranteed to wait, it is not
1056 * obligated to, for example, if there are no pending callbacks.
1057 */
1058void rcu_barrier_tasks_trace(void)
1059{
1060 /* There is only one callback queue, so this is easy. ;-) */
1061 synchronize_rcu_tasks_trace();
1062}
1063EXPORT_SYMBOL_GPL(rcu_barrier_tasks_trace);
1064
1065static int __init rcu_spawn_tasks_trace_kthread(void)
1066{
1067 rcu_tasks_trace.pregp_func = rcu_tasks_trace_pregp_step;
1068 rcu_tasks_trace.pertask_func = rcu_tasks_trace_pertask;
1069 rcu_tasks_trace.postscan_func = rcu_tasks_trace_postscan;
1070 rcu_tasks_trace.holdouts_func = check_all_holdout_tasks_trace;
1071 rcu_tasks_trace.postgp_func = rcu_tasks_trace_postgp;
1072 rcu_spawn_tasks_kthread_generic(&rcu_tasks_trace);
1073 return 0;
1074}
1075core_initcall(rcu_spawn_tasks_trace_kthread);
1076
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001077static void show_rcu_tasks_trace_gp_kthread(void)
1078{
1079 char buf[32];
1080
1081 sprintf(buf, "N%d", atomic_read(&trc_n_readers_need_end));
1082 show_rcu_tasks_generic_gp_kthread(&rcu_tasks_trace, buf);
1083}
1084
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001085#else /* #ifdef CONFIG_TASKS_TRACE_RCU */
1086void exit_tasks_rcu_finish_trace(struct task_struct *t) { }
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001087static inline void show_rcu_tasks_trace_gp_kthread(void) {}
Paul E. McKenneyd5f177d2020-03-09 19:56:53 -07001088#endif /* #else #ifdef CONFIG_TASKS_TRACE_RCU */
Paul E. McKenney8fd8ca32020-03-15 14:51:20 -07001089
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001090void show_rcu_tasks_gp_kthreads(void)
1091{
1092 show_rcu_tasks_classic_gp_kthread();
1093 show_rcu_tasks_rude_gp_kthread();
1094 show_rcu_tasks_trace_gp_kthread();
1095}
1096
Paul E. McKenney8fd8ca32020-03-15 14:51:20 -07001097#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
1098static inline void rcu_tasks_bootup_oddness(void) {}
Paul E. McKenneye21408c2020-03-16 11:01:55 -07001099void show_rcu_tasks_gp_kthreads(void) {}
Paul E. McKenney8fd8ca32020-03-15 14:51:20 -07001100#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */