1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Read-Copy Update mechanism for mutual exclusion
4 *
5 * Copyright IBM Corporation, 2001
6 *
7 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
8 * Manfred Spraul <manfred@colorfullife.com>
9 *
10 * Based on the original work by Paul McKenney <paulmck@linux.ibm.com>
11 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
12 * Papers:
13 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
14 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
15 *
16 * For detailed explanation of Read-Copy Update mechanism see -
17 * http://lse.sourceforge.net/locking/rcupdate.html
18 *
19 */
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/spinlock.h>
24 #include <linux/smp.h>
25 #include <linux/interrupt.h>
26 #include <linux/sched/signal.h>
27 #include <linux/sched/debug.h>
28 #include <linux/atomic.h>
29 #include <linux/bitops.h>
30 #include <linux/percpu.h>
31 #include <linux/notifier.h>
32 #include <linux/cpu.h>
33 #include <linux/mutex.h>
34 #include <linux/export.h>
35 #include <linux/hardirq.h>
36 #include <linux/delay.h>
37 #include <linux/moduleparam.h>
38 #include <linux/kthread.h>
39 #include <linux/tick.h>
40 #include <linux/rcupdate_wait.h>
41 #include <linux/sched/isolation.h>
42 #include <linux/kprobes.h>
43 #include <linux/slab.h>
44 #include <linux/irq_work.h>
45 #include <linux/rcupdate_trace.h>
46 #include <linux/jiffies.h>
47
48 #define CREATE_TRACE_POINTS
49
50 #include "rcu.h"
51
52 #ifdef MODULE_PARAM_PREFIX
53 #undef MODULE_PARAM_PREFIX
54 #endif
55 #define MODULE_PARAM_PREFIX "rcupdate."
56
57 #ifndef CONFIG_TINY_RCU
58 module_param(rcu_expedited, int, 0);
59 module_param(rcu_normal, int, 0);
60 static int rcu_normal_after_boot = IS_ENABLED(CONFIG_PREEMPT_RT);
61 #ifndef CONFIG_PREEMPT_RT
62 module_param(rcu_normal_after_boot, int, 0);
63 #endif
64 #endif /* #ifndef CONFIG_TINY_RCU */
65
66 #ifdef CONFIG_DEBUG_LOCK_ALLOC
67 /**
68 * rcu_read_lock_held_common() - might we be in RCU-sched read-side critical section?
69 * @ret: Best guess answer if lockdep cannot be relied on
70 *
71 * Returns true if lockdep must be ignored, in which case ``*ret`` contains
72 * the best guess described below. Otherwise returns false, in which
73 * case ``*ret`` tells the caller nothing and the caller should instead
74 * consult lockdep.
75 *
76 * If CONFIG_DEBUG_LOCK_ALLOC is selected, set ``*ret`` to nonzero iff in an
77 * RCU-sched read-side critical section. In absence of
78 * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side
79 * critical section unless it can prove otherwise. Note that disabling
80 * of preemption (including disabling irqs) counts as an RCU-sched
81 * read-side critical section. This is useful for debug checks in functions
82 * that required that they be called within an RCU-sched read-side
83 * critical section.
84 *
85 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot
86 * and while lockdep is disabled.
87 *
88 * Note that if the CPU is in the idle loop from an RCU point of view (ie:
89 * that we are in the section between rcu_idle_enter() and rcu_idle_exit())
90 * then rcu_read_lock_held() sets ``*ret`` to false even if the CPU did an
91 * rcu_read_lock(). The reason for this is that RCU ignores CPUs that are
92 * in such a section, considering these as in extended quiescent state,
93 * so such a CPU is effectively never in an RCU read-side critical section
94 * regardless of what RCU primitives it invokes. This state of affairs is
95 * required --- we need to keep an RCU-free window in idle where the CPU may
96 * possibly enter into low power mode. This way we can notice an extended
97 * quiescent state to other CPUs that started a grace period. Otherwise
98 * we would delay any grace period as long as we run in the idle task.
99 *
100 * Similarly, we avoid claiming an RCU read lock held if the current
101 * CPU is offline.
102 */
rcu_read_lock_held_common(bool * ret)103 static bool rcu_read_lock_held_common(bool *ret)
104 {
105 if (!debug_lockdep_rcu_enabled()) {
106 *ret = true;
107 return true;
108 }
109 if (!rcu_is_watching()) {
110 *ret = false;
111 return true;
112 }
113 if (!rcu_lockdep_current_cpu_online()) {
114 *ret = false;
115 return true;
116 }
117 return false;
118 }
119
rcu_read_lock_sched_held(void)120 int rcu_read_lock_sched_held(void)
121 {
122 bool ret;
123
124 if (rcu_read_lock_held_common(&ret))
125 return ret;
126 return lock_is_held(&rcu_sched_lock_map) || !preemptible();
127 }
128 EXPORT_SYMBOL(rcu_read_lock_sched_held);
129 #endif
130
131 #ifndef CONFIG_TINY_RCU
132
133 /*
134 * Should expedited grace-period primitives always fall back to their
135 * non-expedited counterparts? Intended for use within RCU. Note
136 * that if the user specifies both rcu_expedited and rcu_normal, then
137 * rcu_normal wins. (Except during the time period during boot from
138 * when the first task is spawned until the rcu_set_runtime_mode()
139 * core_initcall() is invoked, at which point everything is expedited.)
140 */
rcu_gp_is_normal(void)141 bool rcu_gp_is_normal(void)
142 {
143 return READ_ONCE(rcu_normal) &&
144 rcu_scheduler_active != RCU_SCHEDULER_INIT;
145 }
146 EXPORT_SYMBOL_GPL(rcu_gp_is_normal);
147
148 static atomic_t rcu_async_hurry_nesting = ATOMIC_INIT(1);
149 /*
150 * Should call_rcu() callbacks be processed with urgency or are
151 * they OK being executed with arbitrary delays?
152 */
rcu_async_should_hurry(void)153 bool rcu_async_should_hurry(void)
154 {
155 return !IS_ENABLED(CONFIG_RCU_LAZY) ||
156 atomic_read(&rcu_async_hurry_nesting);
157 }
158 EXPORT_SYMBOL_GPL(rcu_async_should_hurry);
159
160 /**
161 * rcu_async_hurry - Make future async RCU callbacks not lazy.
162 *
163 * After a call to this function, future calls to call_rcu()
164 * will be processed in a timely fashion.
165 */
rcu_async_hurry(void)166 void rcu_async_hurry(void)
167 {
168 if (IS_ENABLED(CONFIG_RCU_LAZY))
169 atomic_inc(&rcu_async_hurry_nesting);
170 }
171 EXPORT_SYMBOL_GPL(rcu_async_hurry);
172
173 /**
174 * rcu_async_relax - Make future async RCU callbacks lazy.
175 *
176 * After a call to this function, future calls to call_rcu()
177 * will be processed in a lazy fashion.
178 */
rcu_async_relax(void)179 void rcu_async_relax(void)
180 {
181 if (IS_ENABLED(CONFIG_RCU_LAZY))
182 atomic_dec(&rcu_async_hurry_nesting);
183 }
184 EXPORT_SYMBOL_GPL(rcu_async_relax);
185
186 static atomic_t rcu_expedited_nesting = ATOMIC_INIT(1);
187 /*
188 * Should normal grace-period primitives be expedited? Intended for
189 * use within RCU. Note that this function takes the rcu_expedited
190 * sysfs/boot variable and rcu_scheduler_active into account as well
191 * as the rcu_expedite_gp() nesting. So looping on rcu_unexpedite_gp()
192 * until rcu_gp_is_expedited() returns false is a -really- bad idea.
193 */
rcu_gp_is_expedited(void)194 bool rcu_gp_is_expedited(void)
195 {
196 return rcu_expedited || atomic_read(&rcu_expedited_nesting);
197 }
198 EXPORT_SYMBOL_GPL(rcu_gp_is_expedited);
199
200 /**
201 * rcu_expedite_gp - Expedite future RCU grace periods
202 *
203 * After a call to this function, future calls to synchronize_rcu() and
204 * friends act as the corresponding synchronize_rcu_expedited() function
205 * had instead been called.
206 */
rcu_expedite_gp(void)207 void rcu_expedite_gp(void)
208 {
209 atomic_inc(&rcu_expedited_nesting);
210 }
211 EXPORT_SYMBOL_GPL(rcu_expedite_gp);
212
213 /**
214 * rcu_unexpedite_gp - Cancel prior rcu_expedite_gp() invocation
215 *
216 * Undo a prior call to rcu_expedite_gp(). If all prior calls to
217 * rcu_expedite_gp() are undone by a subsequent call to rcu_unexpedite_gp(),
218 * and if the rcu_expedited sysfs/boot parameter is not set, then all
219 * subsequent calls to synchronize_rcu() and friends will return to
220 * their normal non-expedited behavior.
221 */
rcu_unexpedite_gp(void)222 void rcu_unexpedite_gp(void)
223 {
224 atomic_dec(&rcu_expedited_nesting);
225 }
226 EXPORT_SYMBOL_GPL(rcu_unexpedite_gp);
227
228 /*
229 * Minimum time in milliseconds from the start boot until RCU can consider
230 * in-kernel boot as completed. This can also be tuned at runtime to end the
231 * boot earlier, by userspace init code writing the time in milliseconds (even
232 * 0) to: /sys/module/rcupdate/parameters/android_rcu_boot_end_delay. The sysfs
233 * node can also be used to extend the delay to be larger than the default,
234 * assuming the marking of boot complete has not yet occurred.
235 */
236 static int android_rcu_boot_end_delay = CONFIG_RCU_BOOT_END_DELAY;
237
238 static bool rcu_boot_ended __read_mostly;
239 static bool rcu_boot_end_called __read_mostly;
240 static DEFINE_MUTEX(rcu_boot_end_lock);
241
242 /*
243 * Inform RCU of the end of the in-kernel boot sequence. The boot sequence will
244 * not be marked ended until at least android_rcu_boot_end_delay milliseconds
245 * have passed.
246 */
247 void rcu_end_inkernel_boot(void);
rcu_boot_end_work_fn(struct work_struct * work)248 static void rcu_boot_end_work_fn(struct work_struct *work)
249 {
250 rcu_end_inkernel_boot();
251 }
252 static DECLARE_DELAYED_WORK(rcu_boot_end_work, rcu_boot_end_work_fn);
253
254 /* Must be called with rcu_boot_end_lock held. */
rcu_end_inkernel_boot_locked(void)255 static void rcu_end_inkernel_boot_locked(void)
256 {
257 rcu_boot_end_called = true;
258
259 if (rcu_boot_ended)
260 return;
261
262 if (android_rcu_boot_end_delay) {
263 u64 boot_ms = div_u64(ktime_get_boot_fast_ns(), 1000000UL);
264
265 if (boot_ms < android_rcu_boot_end_delay) {
266 schedule_delayed_work(&rcu_boot_end_work,
267 msecs_to_jiffies(android_rcu_boot_end_delay - boot_ms));
268 return;
269 }
270 }
271
272 cancel_delayed_work(&rcu_boot_end_work);
273 rcu_unexpedite_gp();
274 rcu_async_relax();
275 if (rcu_normal_after_boot)
276 WRITE_ONCE(rcu_normal, 1);
277 rcu_boot_ended = true;
278 }
279
rcu_end_inkernel_boot(void)280 void rcu_end_inkernel_boot(void)
281 {
282 mutex_lock(&rcu_boot_end_lock);
283 rcu_end_inkernel_boot_locked();
284 mutex_unlock(&rcu_boot_end_lock);
285 }
286
param_set_rcu_boot_end(const char * val,const struct kernel_param * kp)287 static int param_set_rcu_boot_end(const char *val, const struct kernel_param *kp)
288 {
289 uint end_ms;
290 int ret = kstrtouint(val, 0, &end_ms);
291
292 if (ret)
293 return ret;
294 /*
295 * rcu_end_inkernel_boot() should be called at least once during init
296 * before we can allow param changes to end the boot.
297 */
298 mutex_lock(&rcu_boot_end_lock);
299 android_rcu_boot_end_delay = end_ms;
300 if (!rcu_boot_ended && rcu_boot_end_called) {
301 rcu_end_inkernel_boot_locked();
302 }
303 mutex_unlock(&rcu_boot_end_lock);
304 return ret;
305 }
306
307 static const struct kernel_param_ops rcu_boot_end_ops = {
308 .set = param_set_rcu_boot_end,
309 .get = param_get_uint,
310 };
311 module_param_cb(android_rcu_boot_end_delay, &rcu_boot_end_ops, &android_rcu_boot_end_delay, 0644);
312
313 /*
314 * Let rcutorture know when it is OK to turn it up to eleven.
315 */
rcu_inkernel_boot_has_ended(void)316 bool rcu_inkernel_boot_has_ended(void)
317 {
318 return rcu_boot_ended;
319 }
320 EXPORT_SYMBOL_GPL(rcu_inkernel_boot_has_ended);
321
322 #endif /* #ifndef CONFIG_TINY_RCU */
323
324 /*
325 * Test each non-SRCU synchronous grace-period wait API. This is
326 * useful just after a change in mode for these primitives, and
327 * during early boot.
328 */
rcu_test_sync_prims(void)329 void rcu_test_sync_prims(void)
330 {
331 if (!IS_ENABLED(CONFIG_PROVE_RCU))
332 return;
333 synchronize_rcu();
334 synchronize_rcu_expedited();
335 }
336
337 #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU)
338
339 /*
340 * Switch to run-time mode once RCU has fully initialized.
341 */
rcu_set_runtime_mode(void)342 static int __init rcu_set_runtime_mode(void)
343 {
344 rcu_test_sync_prims();
345 rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
346 kfree_rcu_scheduler_running();
347 rcu_test_sync_prims();
348 return 0;
349 }
350 core_initcall(rcu_set_runtime_mode);
351
352 #endif /* #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU) */
353
354 #ifdef CONFIG_DEBUG_LOCK_ALLOC
355 static struct lock_class_key rcu_lock_key;
356 struct lockdep_map rcu_lock_map = {
357 .name = "rcu_read_lock",
358 .key = &rcu_lock_key,
359 .wait_type_outer = LD_WAIT_FREE,
360 .wait_type_inner = LD_WAIT_CONFIG, /* XXX PREEMPT_RCU ? */
361 };
362 EXPORT_SYMBOL_GPL(rcu_lock_map);
363
364 static struct lock_class_key rcu_bh_lock_key;
365 struct lockdep_map rcu_bh_lock_map = {
366 .name = "rcu_read_lock_bh",
367 .key = &rcu_bh_lock_key,
368 .wait_type_outer = LD_WAIT_FREE,
369 .wait_type_inner = LD_WAIT_CONFIG, /* PREEMPT_LOCK also makes BH preemptible */
370 };
371 EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
372
373 static struct lock_class_key rcu_sched_lock_key;
374 struct lockdep_map rcu_sched_lock_map = {
375 .name = "rcu_read_lock_sched",
376 .key = &rcu_sched_lock_key,
377 .wait_type_outer = LD_WAIT_FREE,
378 .wait_type_inner = LD_WAIT_SPIN,
379 };
380 EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
381
382 // Tell lockdep when RCU callbacks are being invoked.
383 static struct lock_class_key rcu_callback_key;
384 struct lockdep_map rcu_callback_map =
385 STATIC_LOCKDEP_MAP_INIT("rcu_callback", &rcu_callback_key);
386 EXPORT_SYMBOL_GPL(rcu_callback_map);
387
debug_lockdep_rcu_enabled(void)388 noinstr int notrace debug_lockdep_rcu_enabled(void)
389 {
390 return rcu_scheduler_active != RCU_SCHEDULER_INACTIVE && READ_ONCE(debug_locks) &&
391 current->lockdep_recursion == 0;
392 }
393 EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
394
395 /**
396 * rcu_read_lock_held() - might we be in RCU read-side critical section?
397 *
398 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an RCU
399 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC,
400 * this assumes we are in an RCU read-side critical section unless it can
401 * prove otherwise. This is useful for debug checks in functions that
402 * require that they be called within an RCU read-side critical section.
403 *
404 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
405 * and while lockdep is disabled.
406 *
407 * Note that rcu_read_lock() and the matching rcu_read_unlock() must
408 * occur in the same context, for example, it is illegal to invoke
409 * rcu_read_unlock() in process context if the matching rcu_read_lock()
410 * was invoked from within an irq handler.
411 *
412 * Note that rcu_read_lock() is disallowed if the CPU is either idle or
413 * offline from an RCU perspective, so check for those as well.
414 */
rcu_read_lock_held(void)415 int rcu_read_lock_held(void)
416 {
417 bool ret;
418
419 if (rcu_read_lock_held_common(&ret))
420 return ret;
421 return lock_is_held(&rcu_lock_map);
422 }
423 EXPORT_SYMBOL_GPL(rcu_read_lock_held);
424
425 /**
426 * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
427 *
428 * Check for bottom half being disabled, which covers both the
429 * CONFIG_PROVE_RCU and not cases. Note that if someone uses
430 * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
431 * will show the situation. This is useful for debug checks in functions
432 * that require that they be called within an RCU read-side critical
433 * section.
434 *
435 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
436 *
437 * Note that rcu_read_lock_bh() is disallowed if the CPU is either idle or
438 * offline from an RCU perspective, so check for those as well.
439 */
rcu_read_lock_bh_held(void)440 int rcu_read_lock_bh_held(void)
441 {
442 bool ret;
443
444 if (rcu_read_lock_held_common(&ret))
445 return ret;
446 return in_softirq() || irqs_disabled();
447 }
448 EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
449
rcu_read_lock_any_held(void)450 int rcu_read_lock_any_held(void)
451 {
452 bool ret;
453
454 if (rcu_read_lock_held_common(&ret))
455 return ret;
456 if (lock_is_held(&rcu_lock_map) ||
457 lock_is_held(&rcu_bh_lock_map) ||
458 lock_is_held(&rcu_sched_lock_map))
459 return 1;
460 return !preemptible();
461 }
462 EXPORT_SYMBOL_GPL(rcu_read_lock_any_held);
463
464 #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
465
466 /**
467 * wakeme_after_rcu() - Callback function to awaken a task after grace period
468 * @head: Pointer to rcu_head member within rcu_synchronize structure
469 *
470 * Awaken the corresponding task now that a grace period has elapsed.
471 */
wakeme_after_rcu(struct rcu_head * head)472 void wakeme_after_rcu(struct rcu_head *head)
473 {
474 struct rcu_synchronize *rcu;
475
476 rcu = container_of(head, struct rcu_synchronize, head);
477 complete(&rcu->completion);
478 }
479 EXPORT_SYMBOL_GPL(wakeme_after_rcu);
480
__wait_rcu_gp(bool checktiny,int n,call_rcu_func_t * crcu_array,struct rcu_synchronize * rs_array)481 void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
482 struct rcu_synchronize *rs_array)
483 {
484 int i;
485 int j;
486
487 /* Initialize and register callbacks for each crcu_array element. */
488 for (i = 0; i < n; i++) {
489 if (checktiny &&
490 (crcu_array[i] == call_rcu)) {
491 might_sleep();
492 continue;
493 }
494 for (j = 0; j < i; j++)
495 if (crcu_array[j] == crcu_array[i])
496 break;
497 if (j == i) {
498 init_rcu_head_on_stack(&rs_array[i].head);
499 init_completion(&rs_array[i].completion);
500 (crcu_array[i])(&rs_array[i].head, wakeme_after_rcu);
501 }
502 }
503
504 /* Wait for all callbacks to be invoked. */
505 for (i = 0; i < n; i++) {
506 if (checktiny &&
507 (crcu_array[i] == call_rcu))
508 continue;
509 for (j = 0; j < i; j++)
510 if (crcu_array[j] == crcu_array[i])
511 break;
512 if (j == i) {
513 wait_for_completion(&rs_array[i].completion);
514 destroy_rcu_head_on_stack(&rs_array[i].head);
515 }
516 }
517 }
518 EXPORT_SYMBOL_GPL(__wait_rcu_gp);
519
520 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
init_rcu_head(struct rcu_head * head)521 void init_rcu_head(struct rcu_head *head)
522 {
523 debug_object_init(head, &rcuhead_debug_descr);
524 }
525 EXPORT_SYMBOL_GPL(init_rcu_head);
526
destroy_rcu_head(struct rcu_head * head)527 void destroy_rcu_head(struct rcu_head *head)
528 {
529 debug_object_free(head, &rcuhead_debug_descr);
530 }
531 EXPORT_SYMBOL_GPL(destroy_rcu_head);
532
rcuhead_is_static_object(void * addr)533 static bool rcuhead_is_static_object(void *addr)
534 {
535 return true;
536 }
537
538 /**
539 * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
540 * @head: pointer to rcu_head structure to be initialized
541 *
542 * This function informs debugobjects of a new rcu_head structure that
543 * has been allocated as an auto variable on the stack. This function
544 * is not required for rcu_head structures that are statically defined or
545 * that are dynamically allocated on the heap. This function has no
546 * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
547 */
init_rcu_head_on_stack(struct rcu_head * head)548 void init_rcu_head_on_stack(struct rcu_head *head)
549 {
550 debug_object_init_on_stack(head, &rcuhead_debug_descr);
551 }
552 EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
553
554 /**
555 * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
556 * @head: pointer to rcu_head structure to be initialized
557 *
558 * This function informs debugobjects that an on-stack rcu_head structure
559 * is about to go out of scope. As with init_rcu_head_on_stack(), this
560 * function is not required for rcu_head structures that are statically
561 * defined or that are dynamically allocated on the heap. Also as with
562 * init_rcu_head_on_stack(), this function has no effect for
563 * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
564 */
destroy_rcu_head_on_stack(struct rcu_head * head)565 void destroy_rcu_head_on_stack(struct rcu_head *head)
566 {
567 debug_object_free(head, &rcuhead_debug_descr);
568 }
569 EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
570
571 const struct debug_obj_descr rcuhead_debug_descr = {
572 .name = "rcu_head",
573 .is_static_object = rcuhead_is_static_object,
574 };
575 EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
576 #endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
577
578 #if defined(CONFIG_TREE_RCU) || defined(CONFIG_RCU_TRACE)
do_trace_rcu_torture_read(const char * rcutorturename,struct rcu_head * rhp,unsigned long secs,unsigned long c_old,unsigned long c)579 void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
580 unsigned long secs,
581 unsigned long c_old, unsigned long c)
582 {
583 trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
584 }
585 EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
586 #else
587 #define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
588 do { } while (0)
589 #endif
590
591 #if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST)
592 /* Get rcutorture access to sched_setaffinity(). */
rcutorture_sched_setaffinity(pid_t pid,const struct cpumask * in_mask)593 long rcutorture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
594 {
595 int ret;
596
597 ret = sched_setaffinity(pid, in_mask);
598 WARN_ONCE(ret, "%s: sched_setaffinity() returned %d\n", __func__, ret);
599 return ret;
600 }
601 EXPORT_SYMBOL_GPL(rcutorture_sched_setaffinity);
602 #endif
603
604 #ifdef CONFIG_RCU_STALL_COMMON
605 int rcu_cpu_stall_ftrace_dump __read_mostly;
606 module_param(rcu_cpu_stall_ftrace_dump, int, 0644);
607 int rcu_cpu_stall_suppress __read_mostly; // !0 = suppress stall warnings.
608 EXPORT_SYMBOL_GPL(rcu_cpu_stall_suppress);
609 module_param(rcu_cpu_stall_suppress, int, 0644);
610 int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
611 module_param(rcu_cpu_stall_timeout, int, 0644);
612 #endif /* #ifdef CONFIG_RCU_STALL_COMMON */
613
614 // Suppress boot-time RCU CPU stall warnings and rcutorture writer stall
615 // warnings. Also used by rcutorture even if stall warnings are excluded.
616 int rcu_cpu_stall_suppress_at_boot __read_mostly; // !0 = suppress boot stalls.
617 EXPORT_SYMBOL_GPL(rcu_cpu_stall_suppress_at_boot);
618 module_param(rcu_cpu_stall_suppress_at_boot, int, 0444);
619
620 #ifdef CONFIG_PROVE_RCU
621
622 /*
623 * Early boot self test parameters.
624 */
625 static bool rcu_self_test;
626 module_param(rcu_self_test, bool, 0444);
627
628 static int rcu_self_test_counter;
629
test_callback(struct rcu_head * r)630 static void test_callback(struct rcu_head *r)
631 {
632 rcu_self_test_counter++;
633 pr_info("RCU test callback executed %d\n", rcu_self_test_counter);
634 }
635
636 DEFINE_STATIC_SRCU(early_srcu);
637 static unsigned long early_srcu_cookie;
638
639 struct early_boot_kfree_rcu {
640 struct rcu_head rh;
641 };
642
early_boot_test_call_rcu(void)643 static void early_boot_test_call_rcu(void)
644 {
645 static struct rcu_head head;
646 static struct rcu_head shead;
647 struct early_boot_kfree_rcu *rhp;
648
649 call_rcu(&head, test_callback);
650 if (IS_ENABLED(CONFIG_SRCU)) {
651 early_srcu_cookie = start_poll_synchronize_srcu(&early_srcu);
652 call_srcu(&early_srcu, &shead, test_callback);
653 }
654 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
655 if (!WARN_ON_ONCE(!rhp))
656 kfree_rcu(rhp, rh);
657 }
658
rcu_early_boot_tests(void)659 void rcu_early_boot_tests(void)
660 {
661 pr_info("Running RCU self tests\n");
662
663 if (rcu_self_test)
664 early_boot_test_call_rcu();
665 rcu_test_sync_prims();
666 }
667
rcu_verify_early_boot_tests(void)668 static int rcu_verify_early_boot_tests(void)
669 {
670 int ret = 0;
671 int early_boot_test_counter = 0;
672
673 if (rcu_self_test) {
674 early_boot_test_counter++;
675 rcu_barrier();
676 if (IS_ENABLED(CONFIG_SRCU)) {
677 early_boot_test_counter++;
678 srcu_barrier(&early_srcu);
679 WARN_ON_ONCE(!poll_state_synchronize_srcu(&early_srcu, early_srcu_cookie));
680 }
681 }
682 if (rcu_self_test_counter != early_boot_test_counter) {
683 WARN_ON(1);
684 ret = -1;
685 }
686
687 return ret;
688 }
689 late_initcall(rcu_verify_early_boot_tests);
690 #else
rcu_early_boot_tests(void)691 void rcu_early_boot_tests(void) {}
692 #endif /* CONFIG_PROVE_RCU */
693
694 #include "tasks.h"
695
696 #ifndef CONFIG_TINY_RCU
697
698 /*
699 * Print any significant non-default boot-time settings.
700 */
rcupdate_announce_bootup_oddness(void)701 void __init rcupdate_announce_bootup_oddness(void)
702 {
703 if (rcu_normal)
704 pr_info("\tNo expedited grace period (rcu_normal).\n");
705 else if (rcu_normal_after_boot)
706 pr_info("\tNo expedited grace period (rcu_normal_after_boot).\n");
707 else if (rcu_expedited)
708 pr_info("\tAll grace periods are expedited (rcu_expedited).\n");
709 if (rcu_cpu_stall_suppress)
710 pr_info("\tRCU CPU stall warnings suppressed (rcu_cpu_stall_suppress).\n");
711 if (rcu_cpu_stall_timeout != CONFIG_RCU_CPU_STALL_TIMEOUT)
712 pr_info("\tRCU CPU stall warnings timeout set to %d (rcu_cpu_stall_timeout).\n", rcu_cpu_stall_timeout);
713 rcu_tasks_bootup_oddness();
714 }
715
716 #endif /* #ifndef CONFIG_TINY_RCU */
717