linux/kernel/rcu/update.c
<<
>>
Prefs
   1/*
   2 * Read-Copy Update mechanism for mutual exclusion
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * You should have received a copy of the GNU General Public License
  15 * along with this program; if not, you can access it online at
  16 * http://www.gnu.org/licenses/gpl-2.0.html.
  17 *
  18 * Copyright IBM Corporation, 2001
  19 *
  20 * Authors: Dipankar Sarma <dipankar@in.ibm.com>
  21 *          Manfred Spraul <manfred@colorfullife.com>
  22 *
  23 * Based on the original work by Paul McKenney <paulmck@us.ibm.com>
  24 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen.
  25 * Papers:
  26 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf
  27 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001)
  28 *
  29 * For detailed explanation of Read-Copy Update mechanism see -
  30 *              http://lse.sourceforge.net/locking/rcupdate.html
  31 *
  32 */
  33#include <linux/types.h>
  34#include <linux/kernel.h>
  35#include <linux/init.h>
  36#include <linux/spinlock.h>
  37#include <linux/smp.h>
  38#include <linux/interrupt.h>
  39#include <linux/sched/signal.h>
  40#include <linux/sched/debug.h>
  41#include <linux/atomic.h>
  42#include <linux/bitops.h>
  43#include <linux/percpu.h>
  44#include <linux/notifier.h>
  45#include <linux/cpu.h>
  46#include <linux/mutex.h>
  47#include <linux/export.h>
  48#include <linux/hardirq.h>
  49#include <linux/delay.h>
  50#include <linux/moduleparam.h>
  51#include <linux/kthread.h>
  52#include <linux/tick.h>
  53#include <linux/rcupdate_wait.h>
  54#include <linux/sched/isolation.h>
  55
  56#define CREATE_TRACE_POINTS
  57
  58#include "rcu.h"
  59
  60#ifdef MODULE_PARAM_PREFIX
  61#undef MODULE_PARAM_PREFIX
  62#endif
  63#define MODULE_PARAM_PREFIX "rcupdate."
  64
  65#ifndef CONFIG_TINY_RCU
  66extern int rcu_expedited; /* from sysctl */
  67module_param(rcu_expedited, int, 0);
  68extern int rcu_normal; /* from sysctl */
  69module_param(rcu_normal, int, 0);
  70static int rcu_normal_after_boot;
  71module_param(rcu_normal_after_boot, int, 0);
  72#endif /* #ifndef CONFIG_TINY_RCU */
  73
  74#ifdef CONFIG_DEBUG_LOCK_ALLOC
  75/**
  76 * rcu_read_lock_held_common() - might we be in RCU-sched read-side critical section?
  77 * @ret:        Best guess answer if lockdep cannot be relied on
  78 *
  79 * Returns true if lockdep must be ignored, in which case *ret contains
  80 * the best guess described below.  Otherwise returns false, in which
  81 * case *ret tells the caller nothing and the caller should instead
  82 * consult lockdep.
  83 *
  84 * If CONFIG_DEBUG_LOCK_ALLOC is selected, set *ret to nonzero iff in an
  85 * RCU-sched read-side critical section.  In absence of
  86 * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side
  87 * critical section unless it can prove otherwise.  Note that disabling
  88 * of preemption (including disabling irqs) counts as an RCU-sched
  89 * read-side critical section.  This is useful for debug checks in functions
  90 * that required that they be called within an RCU-sched read-side
  91 * critical section.
  92 *
  93 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot
  94 * and while lockdep is disabled.
  95 *
  96 * Note that if the CPU is in the idle loop from an RCU point of view (ie:
  97 * that we are in the section between rcu_idle_enter() and rcu_idle_exit())
  98 * then rcu_read_lock_held() sets *ret to false even if the CPU did an
  99 * rcu_read_lock().  The reason for this is that RCU ignores CPUs that are
 100 * in such a section, considering these as in extended quiescent state,
 101 * so such a CPU is effectively never in an RCU read-side critical section
 102 * regardless of what RCU primitives it invokes.  This state of affairs is
 103 * required --- we need to keep an RCU-free window in idle where the CPU may
 104 * possibly enter into low power mode. This way we can notice an extended
 105 * quiescent state to other CPUs that started a grace period. Otherwise
 106 * we would delay any grace period as long as we run in the idle task.
 107 *
 108 * Similarly, we avoid claiming an RCU read lock held if the current
 109 * CPU is offline.
 110 */
 111static bool rcu_read_lock_held_common(bool *ret)
 112{
 113        if (!debug_lockdep_rcu_enabled()) {
 114                *ret = 1;
 115                return true;
 116        }
 117        if (!rcu_is_watching()) {
 118                *ret = 0;
 119                return true;
 120        }
 121        if (!rcu_lockdep_current_cpu_online()) {
 122                *ret = 0;
 123                return true;
 124        }
 125        return false;
 126}
 127
 128int rcu_read_lock_sched_held(void)
 129{
 130        bool ret;
 131
 132        if (rcu_read_lock_held_common(&ret))
 133                return ret;
 134        return lock_is_held(&rcu_sched_lock_map) || !preemptible();
 135}
 136EXPORT_SYMBOL(rcu_read_lock_sched_held);
 137#endif
 138
 139#ifndef CONFIG_TINY_RCU
 140
 141/*
 142 * Should expedited grace-period primitives always fall back to their
 143 * non-expedited counterparts?  Intended for use within RCU.  Note
 144 * that if the user specifies both rcu_expedited and rcu_normal, then
 145 * rcu_normal wins.  (Except during the time period during boot from
 146 * when the first task is spawned until the rcu_set_runtime_mode()
 147 * core_initcall() is invoked, at which point everything is expedited.)
 148 */
 149bool rcu_gp_is_normal(void)
 150{
 151        return READ_ONCE(rcu_normal) &&
 152               rcu_scheduler_active != RCU_SCHEDULER_INIT;
 153}
 154EXPORT_SYMBOL_GPL(rcu_gp_is_normal);
 155
 156static atomic_t rcu_expedited_nesting = ATOMIC_INIT(1);
 157
 158/*
 159 * Should normal grace-period primitives be expedited?  Intended for
 160 * use within RCU.  Note that this function takes the rcu_expedited
 161 * sysfs/boot variable and rcu_scheduler_active into account as well
 162 * as the rcu_expedite_gp() nesting.  So looping on rcu_unexpedite_gp()
 163 * until rcu_gp_is_expedited() returns false is a -really- bad idea.
 164 */
 165bool rcu_gp_is_expedited(void)
 166{
 167        return rcu_expedited || atomic_read(&rcu_expedited_nesting) ||
 168               rcu_scheduler_active == RCU_SCHEDULER_INIT;
 169}
 170EXPORT_SYMBOL_GPL(rcu_gp_is_expedited);
 171
 172/**
 173 * rcu_expedite_gp - Expedite future RCU grace periods
 174 *
 175 * After a call to this function, future calls to synchronize_rcu() and
 176 * friends act as the corresponding synchronize_rcu_expedited() function
 177 * had instead been called.
 178 */
 179void rcu_expedite_gp(void)
 180{
 181        atomic_inc(&rcu_expedited_nesting);
 182}
 183EXPORT_SYMBOL_GPL(rcu_expedite_gp);
 184
 185/**
 186 * rcu_unexpedite_gp - Cancel prior rcu_expedite_gp() invocation
 187 *
 188 * Undo a prior call to rcu_expedite_gp().  If all prior calls to
 189 * rcu_expedite_gp() are undone by a subsequent call to rcu_unexpedite_gp(),
 190 * and if the rcu_expedited sysfs/boot parameter is not set, then all
 191 * subsequent calls to synchronize_rcu() and friends will return to
 192 * their normal non-expedited behavior.
 193 */
 194void rcu_unexpedite_gp(void)
 195{
 196        atomic_dec(&rcu_expedited_nesting);
 197}
 198EXPORT_SYMBOL_GPL(rcu_unexpedite_gp);
 199
 200/*
 201 * Inform RCU of the end of the in-kernel boot sequence.
 202 */
 203void rcu_end_inkernel_boot(void)
 204{
 205        rcu_unexpedite_gp();
 206        if (rcu_normal_after_boot)
 207                WRITE_ONCE(rcu_normal, 1);
 208}
 209
 210#endif /* #ifndef CONFIG_TINY_RCU */
 211
 212/*
 213 * Test each non-SRCU synchronous grace-period wait API.  This is
 214 * useful just after a change in mode for these primitives, and
 215 * during early boot.
 216 */
 217void rcu_test_sync_prims(void)
 218{
 219        if (!IS_ENABLED(CONFIG_PROVE_RCU))
 220                return;
 221        synchronize_rcu();
 222        synchronize_rcu_expedited();
 223}
 224
 225#if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU)
 226
 227/*
 228 * Switch to run-time mode once RCU has fully initialized.
 229 */
 230static int __init rcu_set_runtime_mode(void)
 231{
 232        rcu_test_sync_prims();
 233        rcu_scheduler_active = RCU_SCHEDULER_RUNNING;
 234        rcu_test_sync_prims();
 235        return 0;
 236}
 237core_initcall(rcu_set_runtime_mode);
 238
 239#endif /* #if !defined(CONFIG_TINY_RCU) || defined(CONFIG_SRCU) */
 240
 241#ifdef CONFIG_DEBUG_LOCK_ALLOC
 242static struct lock_class_key rcu_lock_key;
 243struct lockdep_map rcu_lock_map =
 244        STATIC_LOCKDEP_MAP_INIT("rcu_read_lock", &rcu_lock_key);
 245EXPORT_SYMBOL_GPL(rcu_lock_map);
 246
 247static struct lock_class_key rcu_bh_lock_key;
 248struct lockdep_map rcu_bh_lock_map =
 249        STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_bh", &rcu_bh_lock_key);
 250EXPORT_SYMBOL_GPL(rcu_bh_lock_map);
 251
 252static struct lock_class_key rcu_sched_lock_key;
 253struct lockdep_map rcu_sched_lock_map =
 254        STATIC_LOCKDEP_MAP_INIT("rcu_read_lock_sched", &rcu_sched_lock_key);
 255EXPORT_SYMBOL_GPL(rcu_sched_lock_map);
 256
 257static struct lock_class_key rcu_callback_key;
 258struct lockdep_map rcu_callback_map =
 259        STATIC_LOCKDEP_MAP_INIT("rcu_callback", &rcu_callback_key);
 260EXPORT_SYMBOL_GPL(rcu_callback_map);
 261
 262int notrace debug_lockdep_rcu_enabled(void)
 263{
 264        return rcu_scheduler_active != RCU_SCHEDULER_INACTIVE && debug_locks &&
 265               current->lockdep_recursion == 0;
 266}
 267EXPORT_SYMBOL_GPL(debug_lockdep_rcu_enabled);
 268
 269/**
 270 * rcu_read_lock_held() - might we be in RCU read-side critical section?
 271 *
 272 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an RCU
 273 * read-side critical section.  In absence of CONFIG_DEBUG_LOCK_ALLOC,
 274 * this assumes we are in an RCU read-side critical section unless it can
 275 * prove otherwise.  This is useful for debug checks in functions that
 276 * require that they be called within an RCU read-side critical section.
 277 *
 278 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot
 279 * and while lockdep is disabled.
 280 *
 281 * Note that rcu_read_lock() and the matching rcu_read_unlock() must
 282 * occur in the same context, for example, it is illegal to invoke
 283 * rcu_read_unlock() in process context if the matching rcu_read_lock()
 284 * was invoked from within an irq handler.
 285 *
 286 * Note that rcu_read_lock() is disallowed if the CPU is either idle or
 287 * offline from an RCU perspective, so check for those as well.
 288 */
 289int rcu_read_lock_held(void)
 290{
 291        bool ret;
 292
 293        if (rcu_read_lock_held_common(&ret))
 294                return ret;
 295        return lock_is_held(&rcu_lock_map);
 296}
 297EXPORT_SYMBOL_GPL(rcu_read_lock_held);
 298
 299/**
 300 * rcu_read_lock_bh_held() - might we be in RCU-bh read-side critical section?
 301 *
 302 * Check for bottom half being disabled, which covers both the
 303 * CONFIG_PROVE_RCU and not cases.  Note that if someone uses
 304 * rcu_read_lock_bh(), but then later enables BH, lockdep (if enabled)
 305 * will show the situation.  This is useful for debug checks in functions
 306 * that require that they be called within an RCU read-side critical
 307 * section.
 308 *
 309 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot.
 310 *
 311 * Note that rcu_read_lock_bh() is disallowed if the CPU is either idle or
 312 * offline from an RCU perspective, so check for those as well.
 313 */
 314int rcu_read_lock_bh_held(void)
 315{
 316        bool ret;
 317
 318        if (rcu_read_lock_held_common(&ret))
 319                return ret;
 320        return in_softirq() || irqs_disabled();
 321}
 322EXPORT_SYMBOL_GPL(rcu_read_lock_bh_held);
 323
 324int rcu_read_lock_any_held(void)
 325{
 326        bool ret;
 327
 328        if (rcu_read_lock_held_common(&ret))
 329                return ret;
 330        if (lock_is_held(&rcu_lock_map) ||
 331            lock_is_held(&rcu_bh_lock_map) ||
 332            lock_is_held(&rcu_sched_lock_map))
 333                return 1;
 334        return !preemptible();
 335}
 336EXPORT_SYMBOL_GPL(rcu_read_lock_any_held);
 337
 338#endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
 339
 340/**
 341 * wakeme_after_rcu() - Callback function to awaken a task after grace period
 342 * @head: Pointer to rcu_head member within rcu_synchronize structure
 343 *
 344 * Awaken the corresponding task now that a grace period has elapsed.
 345 */
 346void wakeme_after_rcu(struct rcu_head *head)
 347{
 348        struct rcu_synchronize *rcu;
 349
 350        rcu = container_of(head, struct rcu_synchronize, head);
 351        complete(&rcu->completion);
 352}
 353EXPORT_SYMBOL_GPL(wakeme_after_rcu);
 354
 355void __wait_rcu_gp(bool checktiny, int n, call_rcu_func_t *crcu_array,
 356                   struct rcu_synchronize *rs_array)
 357{
 358        int i;
 359        int j;
 360
 361        /* Initialize and register callbacks for each crcu_array element. */
 362        for (i = 0; i < n; i++) {
 363                if (checktiny &&
 364                    (crcu_array[i] == call_rcu)) {
 365                        might_sleep();
 366                        continue;
 367                }
 368                init_rcu_head_on_stack(&rs_array[i].head);
 369                init_completion(&rs_array[i].completion);
 370                for (j = 0; j < i; j++)
 371                        if (crcu_array[j] == crcu_array[i])
 372                                break;
 373                if (j == i)
 374                        (crcu_array[i])(&rs_array[i].head, wakeme_after_rcu);
 375        }
 376
 377        /* Wait for all callbacks to be invoked. */
 378        for (i = 0; i < n; i++) {
 379                if (checktiny &&
 380                    (crcu_array[i] == call_rcu))
 381                        continue;
 382                for (j = 0; j < i; j++)
 383                        if (crcu_array[j] == crcu_array[i])
 384                                break;
 385                if (j == i)
 386                        wait_for_completion(&rs_array[i].completion);
 387                destroy_rcu_head_on_stack(&rs_array[i].head);
 388        }
 389}
 390EXPORT_SYMBOL_GPL(__wait_rcu_gp);
 391
 392#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD
 393void init_rcu_head(struct rcu_head *head)
 394{
 395        debug_object_init(head, &rcuhead_debug_descr);
 396}
 397EXPORT_SYMBOL_GPL(init_rcu_head);
 398
 399void destroy_rcu_head(struct rcu_head *head)
 400{
 401        debug_object_free(head, &rcuhead_debug_descr);
 402}
 403EXPORT_SYMBOL_GPL(destroy_rcu_head);
 404
 405static bool rcuhead_is_static_object(void *addr)
 406{
 407        return true;
 408}
 409
 410/**
 411 * init_rcu_head_on_stack() - initialize on-stack rcu_head for debugobjects
 412 * @head: pointer to rcu_head structure to be initialized
 413 *
 414 * This function informs debugobjects of a new rcu_head structure that
 415 * has been allocated as an auto variable on the stack.  This function
 416 * is not required for rcu_head structures that are statically defined or
 417 * that are dynamically allocated on the heap.  This function has no
 418 * effect for !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
 419 */
 420void init_rcu_head_on_stack(struct rcu_head *head)
 421{
 422        debug_object_init_on_stack(head, &rcuhead_debug_descr);
 423}
 424EXPORT_SYMBOL_GPL(init_rcu_head_on_stack);
 425
 426/**
 427 * destroy_rcu_head_on_stack() - destroy on-stack rcu_head for debugobjects
 428 * @head: pointer to rcu_head structure to be initialized
 429 *
 430 * This function informs debugobjects that an on-stack rcu_head structure
 431 * is about to go out of scope.  As with init_rcu_head_on_stack(), this
 432 * function is not required for rcu_head structures that are statically
 433 * defined or that are dynamically allocated on the heap.  Also as with
 434 * init_rcu_head_on_stack(), this function has no effect for
 435 * !CONFIG_DEBUG_OBJECTS_RCU_HEAD kernel builds.
 436 */
 437void destroy_rcu_head_on_stack(struct rcu_head *head)
 438{
 439        debug_object_free(head, &rcuhead_debug_descr);
 440}
 441EXPORT_SYMBOL_GPL(destroy_rcu_head_on_stack);
 442
 443struct debug_obj_descr rcuhead_debug_descr = {
 444        .name = "rcu_head",
 445        .is_static_object = rcuhead_is_static_object,
 446};
 447EXPORT_SYMBOL_GPL(rcuhead_debug_descr);
 448#endif /* #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD */
 449
 450#if defined(CONFIG_TREE_RCU) || defined(CONFIG_PREEMPT_RCU) || defined(CONFIG_RCU_TRACE)
 451void do_trace_rcu_torture_read(const char *rcutorturename, struct rcu_head *rhp,
 452                               unsigned long secs,
 453                               unsigned long c_old, unsigned long c)
 454{
 455        trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c);
 456}
 457EXPORT_SYMBOL_GPL(do_trace_rcu_torture_read);
 458#else
 459#define do_trace_rcu_torture_read(rcutorturename, rhp, secs, c_old, c) \
 460        do { } while (0)
 461#endif
 462
 463#if IS_ENABLED(CONFIG_RCU_TORTURE_TEST) || IS_MODULE(CONFIG_RCU_TORTURE_TEST)
 464/* Get rcutorture access to sched_setaffinity(). */
 465long rcutorture_sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
 466{
 467        int ret;
 468
 469        ret = sched_setaffinity(pid, in_mask);
 470        WARN_ONCE(ret, "%s: sched_setaffinity() returned %d\n", __func__, ret);
 471        return ret;
 472}
 473EXPORT_SYMBOL_GPL(rcutorture_sched_setaffinity);
 474#endif
 475
 476#ifdef CONFIG_RCU_STALL_COMMON
 477int rcu_cpu_stall_suppress __read_mostly; /* 1 = suppress stall warnings. */
 478EXPORT_SYMBOL_GPL(rcu_cpu_stall_suppress);
 479module_param(rcu_cpu_stall_suppress, int, 0644);
 480int rcu_cpu_stall_timeout __read_mostly = CONFIG_RCU_CPU_STALL_TIMEOUT;
 481module_param(rcu_cpu_stall_timeout, int, 0644);
 482#endif /* #ifdef CONFIG_RCU_STALL_COMMON */
 483
 484#ifdef CONFIG_TASKS_RCU
 485
 486/*
 487 * Simple variant of RCU whose quiescent states are voluntary context
 488 * switch, cond_resched_rcu_qs(), user-space execution, and idle.
 489 * As such, grace periods can take one good long time.  There are no
 490 * read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
 491 * because this implementation is intended to get the system into a safe
 492 * state for some of the manipulations involved in tracing and the like.
 493 * Finally, this implementation does not support high call_rcu_tasks()
 494 * rates from multiple CPUs.  If this is required, per-CPU callback lists
 495 * will be needed.
 496 */
 497
 498/* Global list of callbacks and associated lock. */
 499static struct rcu_head *rcu_tasks_cbs_head;
 500static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
 501static DECLARE_WAIT_QUEUE_HEAD(rcu_tasks_cbs_wq);
 502static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
 503
 504/* Track exiting tasks in order to allow them to be waited for. */
 505DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
 506
 507/* Control stall timeouts.  Disable with <= 0, otherwise jiffies till stall. */
 508#define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
 509static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
 510module_param(rcu_task_stall_timeout, int, 0644);
 511
 512static struct task_struct *rcu_tasks_kthread_ptr;
 513
 514/**
 515 * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
 516 * @rhp: structure to be used for queueing the RCU updates.
 517 * @func: actual callback function to be invoked after the grace period
 518 *
 519 * The callback function will be invoked some time after a full grace
 520 * period elapses, in other words after all currently executing RCU
 521 * read-side critical sections have completed. call_rcu_tasks() assumes
 522 * that the read-side critical sections end at a voluntary context
 523 * switch (not a preemption!), cond_resched_rcu_qs(), entry into idle,
 524 * or transition to usermode execution.  As such, there are no read-side
 525 * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
 526 * this primitive is intended to determine that all tasks have passed
 527 * through a safe state, not so much for data-strcuture synchronization.
 528 *
 529 * See the description of call_rcu() for more detailed information on
 530 * memory ordering guarantees.
 531 */
 532void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
 533{
 534        unsigned long flags;
 535        bool needwake;
 536
 537        rhp->next = NULL;
 538        rhp->func = func;
 539        raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
 540        needwake = !rcu_tasks_cbs_head;
 541        *rcu_tasks_cbs_tail = rhp;
 542        rcu_tasks_cbs_tail = &rhp->next;
 543        raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
 544        /* We can't create the thread unless interrupts are enabled. */
 545        if (needwake && READ_ONCE(rcu_tasks_kthread_ptr))
 546                wake_up(&rcu_tasks_cbs_wq);
 547}
 548EXPORT_SYMBOL_GPL(call_rcu_tasks);
 549
 550/**
 551 * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
 552 *
 553 * Control will return to the caller some time after a full rcu-tasks
 554 * grace period has elapsed, in other words after all currently
 555 * executing rcu-tasks read-side critical sections have elapsed.  These
 556 * read-side critical sections are delimited by calls to schedule(),
 557 * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
 558 * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
 559 *
 560 * This is a very specialized primitive, intended only for a few uses in
 561 * tracing and other situations requiring manipulation of function
 562 * preambles and profiling hooks.  The synchronize_rcu_tasks() function
 563 * is not (yet) intended for heavy use from multiple CPUs.
 564 *
 565 * Note that this guarantee implies further memory-ordering guarantees.
 566 * On systems with more than one CPU, when synchronize_rcu_tasks() returns,
 567 * each CPU is guaranteed to have executed a full memory barrier since the
 568 * end of its last RCU-tasks read-side critical section whose beginning
 569 * preceded the call to synchronize_rcu_tasks().  In addition, each CPU
 570 * having an RCU-tasks read-side critical section that extends beyond
 571 * the return from synchronize_rcu_tasks() is guaranteed to have executed
 572 * a full memory barrier after the beginning of synchronize_rcu_tasks()
 573 * and before the beginning of that RCU-tasks read-side critical section.
 574 * Note that these guarantees include CPUs that are offline, idle, or
 575 * executing in user mode, as well as CPUs that are executing in the kernel.
 576 *
 577 * Furthermore, if CPU A invoked synchronize_rcu_tasks(), which returned
 578 * to its caller on CPU B, then both CPU A and CPU B are guaranteed
 579 * to have executed a full memory barrier during the execution of
 580 * synchronize_rcu_tasks() -- even if CPU A and CPU B are the same CPU
 581 * (but again only if the system has more than one CPU).
 582 */
 583void synchronize_rcu_tasks(void)
 584{
 585        /* Complain if the scheduler has not started.  */
 586        RCU_LOCKDEP_WARN(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
 587                         "synchronize_rcu_tasks called too soon");
 588
 589        /* Wait for the grace period. */
 590        wait_rcu_gp(call_rcu_tasks);
 591}
 592EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
 593
 594/**
 595 * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
 596 *
 597 * Although the current implementation is guaranteed to wait, it is not
 598 * obligated to, for example, if there are no pending callbacks.
 599 */
 600void rcu_barrier_tasks(void)
 601{
 602        /* There is only one callback queue, so this is easy.  ;-) */
 603        synchronize_rcu_tasks();
 604}
 605EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
 606
 607/* See if tasks are still holding out, complain if so. */
 608static void check_holdout_task(struct task_struct *t,
 609                               bool needreport, bool *firstreport)
 610{
 611        int cpu;
 612
 613        if (!READ_ONCE(t->rcu_tasks_holdout) ||
 614            t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
 615            !READ_ONCE(t->on_rq) ||
 616            (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
 617             !is_idle_task(t) && t->rcu_tasks_idle_cpu >= 0)) {
 618                WRITE_ONCE(t->rcu_tasks_holdout, false);
 619                list_del_init(&t->rcu_tasks_holdout_list);
 620                put_task_struct(t);
 621                return;
 622        }
 623        rcu_request_urgent_qs_task(t);
 624        if (!needreport)
 625                return;
 626        if (*firstreport) {
 627                pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
 628                *firstreport = false;
 629        }
 630        cpu = task_cpu(t);
 631        pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
 632                 t, ".I"[is_idle_task(t)],
 633                 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
 634                 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
 635                 t->rcu_tasks_idle_cpu, cpu);
 636        sched_show_task(t);
 637}
 638
 639/* RCU-tasks kthread that detects grace periods and invokes callbacks. */
 640static int __noreturn rcu_tasks_kthread(void *arg)
 641{
 642        unsigned long flags;
 643        struct task_struct *g, *t;
 644        unsigned long lastreport;
 645        struct rcu_head *list;
 646        struct rcu_head *next;
 647        LIST_HEAD(rcu_tasks_holdouts);
 648        int fract;
 649
 650        /* Run on housekeeping CPUs by default.  Sysadm can move if desired. */
 651        housekeeping_affine(current, HK_FLAG_RCU);
 652
 653        /*
 654         * Each pass through the following loop makes one check for
 655         * newly arrived callbacks, and, if there are some, waits for
 656         * one RCU-tasks grace period and then invokes the callbacks.
 657         * This loop is terminated by the system going down.  ;-)
 658         */
 659        for (;;) {
 660
 661                /* Pick up any new callbacks. */
 662                raw_spin_lock_irqsave(&rcu_tasks_cbs_lock, flags);
 663                list = rcu_tasks_cbs_head;
 664                rcu_tasks_cbs_head = NULL;
 665                rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
 666                raw_spin_unlock_irqrestore(&rcu_tasks_cbs_lock, flags);
 667
 668                /* If there were none, wait a bit and start over. */
 669                if (!list) {
 670                        wait_event_interruptible(rcu_tasks_cbs_wq,
 671                                                 rcu_tasks_cbs_head);
 672                        if (!rcu_tasks_cbs_head) {
 673                                WARN_ON(signal_pending(current));
 674                                schedule_timeout_interruptible(HZ/10);
 675                        }
 676                        continue;
 677                }
 678
 679                /*
 680                 * Wait for all pre-existing t->on_rq and t->nvcsw
 681                 * transitions to complete.  Invoking synchronize_rcu()
 682                 * suffices because all these transitions occur with
 683                 * interrupts disabled.  Without this synchronize_rcu(),
 684                 * a read-side critical section that started before the
 685                 * grace period might be incorrectly seen as having started
 686                 * after the grace period.
 687                 *
 688                 * This synchronize_rcu() also dispenses with the
 689                 * need for a memory barrier on the first store to
 690                 * ->rcu_tasks_holdout, as it forces the store to happen
 691                 * after the beginning of the grace period.
 692                 */
 693                synchronize_rcu();
 694
 695                /*
 696                 * There were callbacks, so we need to wait for an
 697                 * RCU-tasks grace period.  Start off by scanning
 698                 * the task list for tasks that are not already
 699                 * voluntarily blocked.  Mark these tasks and make
 700                 * a list of them in rcu_tasks_holdouts.
 701                 */
 702                rcu_read_lock();
 703                for_each_process_thread(g, t) {
 704                        if (t != current && READ_ONCE(t->on_rq) &&
 705                            !is_idle_task(t)) {
 706                                get_task_struct(t);
 707                                t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
 708                                WRITE_ONCE(t->rcu_tasks_holdout, true);
 709                                list_add(&t->rcu_tasks_holdout_list,
 710                                         &rcu_tasks_holdouts);
 711                        }
 712                }
 713                rcu_read_unlock();
 714
 715                /*
 716                 * Wait for tasks that are in the process of exiting.
 717                 * This does only part of the job, ensuring that all
 718                 * tasks that were previously exiting reach the point
 719                 * where they have disabled preemption, allowing the
 720                 * later synchronize_rcu() to finish the job.
 721                 */
 722                synchronize_srcu(&tasks_rcu_exit_srcu);
 723
 724                /*
 725                 * Each pass through the following loop scans the list
 726                 * of holdout tasks, removing any that are no longer
 727                 * holdouts.  When the list is empty, we are done.
 728                 */
 729                lastreport = jiffies;
 730
 731                /* Start off with HZ/10 wait and slowly back off to 1 HZ wait*/
 732                fract = 10;
 733
 734                for (;;) {
 735                        bool firstreport;
 736                        bool needreport;
 737                        int rtst;
 738                        struct task_struct *t1;
 739
 740                        if (list_empty(&rcu_tasks_holdouts))
 741                                break;
 742
 743                        /* Slowly back off waiting for holdouts */
 744                        schedule_timeout_interruptible(HZ/fract);
 745
 746                        if (fract > 1)
 747                                fract--;
 748
 749                        rtst = READ_ONCE(rcu_task_stall_timeout);
 750                        needreport = rtst > 0 &&
 751                                     time_after(jiffies, lastreport + rtst);
 752                        if (needreport)
 753                                lastreport = jiffies;
 754                        firstreport = true;
 755                        WARN_ON(signal_pending(current));
 756                        list_for_each_entry_safe(t, t1, &rcu_tasks_holdouts,
 757                                                rcu_tasks_holdout_list) {
 758                                check_holdout_task(t, needreport, &firstreport);
 759                                cond_resched();
 760                        }
 761                }
 762
 763                /*
 764                 * Because ->on_rq and ->nvcsw are not guaranteed
 765                 * to have a full memory barriers prior to them in the
 766                 * schedule() path, memory reordering on other CPUs could
 767                 * cause their RCU-tasks read-side critical sections to
 768                 * extend past the end of the grace period.  However,
 769                 * because these ->nvcsw updates are carried out with
 770                 * interrupts disabled, we can use synchronize_rcu()
 771                 * to force the needed ordering on all such CPUs.
 772                 *
 773                 * This synchronize_rcu() also confines all
 774                 * ->rcu_tasks_holdout accesses to be within the grace
 775                 * period, avoiding the need for memory barriers for
 776                 * ->rcu_tasks_holdout accesses.
 777                 *
 778                 * In addition, this synchronize_rcu() waits for exiting
 779                 * tasks to complete their final preempt_disable() region
 780                 * of execution, cleaning up after the synchronize_srcu()
 781                 * above.
 782                 */
 783                synchronize_rcu();
 784
 785                /* Invoke the callbacks. */
 786                while (list) {
 787                        next = list->next;
 788                        local_bh_disable();
 789                        list->func(list);
 790                        local_bh_enable();
 791                        list = next;
 792                        cond_resched();
 793                }
 794                /* Paranoid sleep to keep this from entering a tight loop */
 795                schedule_timeout_uninterruptible(HZ/10);
 796        }
 797}
 798
 799/* Spawn rcu_tasks_kthread() at core_initcall() time. */
 800static int __init rcu_spawn_tasks_kthread(void)
 801{
 802        struct task_struct *t;
 803
 804        t = kthread_run(rcu_tasks_kthread, NULL, "rcu_tasks_kthread");
 805        if (WARN_ONCE(IS_ERR(t), "%s: Could not start Tasks-RCU grace-period kthread, OOM is now expected behavior\n", __func__))
 806                return 0;
 807        smp_mb(); /* Ensure others see full kthread. */
 808        WRITE_ONCE(rcu_tasks_kthread_ptr, t);
 809        return 0;
 810}
 811core_initcall(rcu_spawn_tasks_kthread);
 812
 813/* Do the srcu_read_lock() for the above synchronize_srcu().  */
 814void exit_tasks_rcu_start(void)
 815{
 816        preempt_disable();
 817        current->rcu_tasks_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
 818        preempt_enable();
 819}
 820
 821/* Do the srcu_read_unlock() for the above synchronize_srcu().  */
 822void exit_tasks_rcu_finish(void)
 823{
 824        preempt_disable();
 825        __srcu_read_unlock(&tasks_rcu_exit_srcu, current->rcu_tasks_idx);
 826        preempt_enable();
 827}
 828
 829#endif /* #ifdef CONFIG_TASKS_RCU */
 830
 831#ifndef CONFIG_TINY_RCU
 832
 833/*
 834 * Print any non-default Tasks RCU settings.
 835 */
 836static void __init rcu_tasks_bootup_oddness(void)
 837{
 838#ifdef CONFIG_TASKS_RCU
 839        if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
 840                pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
 841        else
 842                pr_info("\tTasks RCU enabled.\n");
 843#endif /* #ifdef CONFIG_TASKS_RCU */
 844}
 845
 846#endif /* #ifndef CONFIG_TINY_RCU */
 847
 848#ifdef CONFIG_PROVE_RCU
 849
 850/*
 851 * Early boot self test parameters.
 852 */
 853static bool rcu_self_test;
 854module_param(rcu_self_test, bool, 0444);
 855
 856static int rcu_self_test_counter;
 857
 858static void test_callback(struct rcu_head *r)
 859{
 860        rcu_self_test_counter++;
 861        pr_info("RCU test callback executed %d\n", rcu_self_test_counter);
 862}
 863
 864DEFINE_STATIC_SRCU(early_srcu);
 865
 866static void early_boot_test_call_rcu(void)
 867{
 868        static struct rcu_head head;
 869        static struct rcu_head shead;
 870
 871        call_rcu(&head, test_callback);
 872        if (IS_ENABLED(CONFIG_SRCU))
 873                call_srcu(&early_srcu, &shead, test_callback);
 874}
 875
 876void rcu_early_boot_tests(void)
 877{
 878        pr_info("Running RCU self tests\n");
 879
 880        if (rcu_self_test)
 881                early_boot_test_call_rcu();
 882        rcu_test_sync_prims();
 883}
 884
 885static int rcu_verify_early_boot_tests(void)
 886{
 887        int ret = 0;
 888        int early_boot_test_counter = 0;
 889
 890        if (rcu_self_test) {
 891                early_boot_test_counter++;
 892                rcu_barrier();
 893                if (IS_ENABLED(CONFIG_SRCU)) {
 894                        early_boot_test_counter++;
 895                        srcu_barrier(&early_srcu);
 896                }
 897        }
 898        if (rcu_self_test_counter != early_boot_test_counter) {
 899                WARN_ON(1);
 900                ret = -1;
 901        }
 902
 903        return ret;
 904}
 905late_initcall(rcu_verify_early_boot_tests);
 906#else
 907void rcu_early_boot_tests(void) {}
 908#endif /* CONFIG_PROVE_RCU */
 909
 910#ifndef CONFIG_TINY_RCU
 911
 912/*
 913 * Print any significant non-default boot-time settings.
 914 */
 915void __init rcupdate_announce_bootup_oddness(void)
 916{
 917        if (rcu_normal)
 918                pr_info("\tNo expedited grace period (rcu_normal).\n");
 919        else if (rcu_normal_after_boot)
 920                pr_info("\tNo expedited grace period (rcu_normal_after_boot).\n");
 921        else if (rcu_expedited)
 922                pr_info("\tAll grace periods are expedited (rcu_expedited).\n");
 923        if (rcu_cpu_stall_suppress)
 924                pr_info("\tRCU CPU stall warnings suppressed (rcu_cpu_stall_suppress).\n");
 925        if (rcu_cpu_stall_timeout != CONFIG_RCU_CPU_STALL_TIMEOUT)
 926                pr_info("\tRCU CPU stall warnings timeout set to %d (rcu_cpu_stall_timeout).\n", rcu_cpu_stall_timeout);
 927        rcu_tasks_bootup_oddness();
 928}
 929
 930#endif /* #ifndef CONFIG_TINY_RCU */
 931