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