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 * Author: Dipankar Sarma <dipankar@in.ibm.com> 21 * 22 * Based on the original work by Paul McKenney <paulmck@us.ibm.com> 23 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen. 24 * Papers: 25 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf 26 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001) 27 * 28 * For detailed explanation of Read-Copy Update mechanism see - 29 * http://lse.sourceforge.net/locking/rcupdate.html 30 * 31 */ 32 33#ifndef __LINUX_RCUPDATE_H 34#define __LINUX_RCUPDATE_H 35 36#include <linux/types.h> 37#include <linux/compiler.h> 38#include <linux/atomic.h> 39#include <linux/irqflags.h> 40#include <linux/preempt.h> 41#include <linux/bottom_half.h> 42#include <linux/lockdep.h> 43#include <asm/processor.h> 44#include <linux/cpumask.h> 45 46#define ULONG_CMP_GE(a, b) (ULONG_MAX / 2 >= (a) - (b)) 47#define ULONG_CMP_LT(a, b) (ULONG_MAX / 2 < (a) - (b)) 48#define ulong2long(a) (*(long *)(&(a))) 49#define USHORT_CMP_GE(a, b) (USHRT_MAX / 2 >= (unsigned short)((a) - (b))) 50#define USHORT_CMP_LT(a, b) (USHRT_MAX / 2 < (unsigned short)((a) - (b))) 51 52/* Exported common interfaces */ 53void call_rcu(struct rcu_head *head, rcu_callback_t func); 54void rcu_barrier_tasks(void); 55void rcu_barrier_tasks_rude(void); 56void synchronize_rcu(void); 57 58#ifdef CONFIG_PREEMPT_RCU 59 60void __rcu_read_lock(void); 61void __rcu_read_unlock(void); 62 63/* 64 * Defined as a macro as it is a very low level header included from 65 * areas that don't even know about current. This gives the rcu_read_lock() 66 * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other 67 * types of kernel builds, the rcu_read_lock() nesting depth is unknowable. 68 */ 69#define rcu_preempt_depth() (current->rcu_read_lock_nesting) 70 71#else /* #ifdef CONFIG_PREEMPT_RCU */ 72 73#ifdef CONFIG_TINY_RCU 74#define rcu_read_unlock_strict() do { } while (0) 75#else 76void rcu_read_unlock_strict(void); 77#endif 78 79static inline void __rcu_read_lock(void) 80{ 81 preempt_disable(); 82} 83 84static inline void __rcu_read_unlock(void) 85{ 86 preempt_enable(); 87 if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) 88 rcu_read_unlock_strict(); 89} 90 91static inline int rcu_preempt_depth(void) 92{ 93 return 0; 94} 95 96#endif /* #else #ifdef CONFIG_PREEMPT_RCU */ 97 98/* Internal to kernel */ 99void rcu_init(void); 100extern int rcu_scheduler_active __read_mostly; 101void rcu_sched_clock_irq(int user); 102void rcu_report_dead(unsigned int cpu); 103void rcutree_migrate_callbacks(int cpu); 104 105#ifdef CONFIG_TASKS_RCU_GENERIC 106void rcu_init_tasks_generic(void); 107#else 108static inline void rcu_init_tasks_generic(void) { } 109#endif 110 111#ifdef CONFIG_RCU_STALL_COMMON 112void rcu_sysrq_start(void); 113void rcu_sysrq_end(void); 114#else /* #ifdef CONFIG_RCU_STALL_COMMON */ 115static inline void rcu_sysrq_start(void) { } 116static inline void rcu_sysrq_end(void) { } 117#endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */ 118 119#ifdef CONFIG_NO_HZ_FULL 120void rcu_user_enter(void); 121void rcu_user_exit(void); 122#else 123static inline void rcu_user_enter(void) { } 124static inline void rcu_user_exit(void) { } 125#endif /* CONFIG_NO_HZ_FULL */ 126 127#ifdef CONFIG_RCU_NOCB_CPU 128void rcu_init_nohz(void); 129int rcu_nocb_cpu_offload(int cpu); 130int rcu_nocb_cpu_deoffload(int cpu); 131void rcu_nocb_flush_deferred_wakeup(void); 132#else /* #ifdef CONFIG_RCU_NOCB_CPU */ 133static inline void rcu_init_nohz(void) { } 134static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; } 135static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; } 136static inline void rcu_nocb_flush_deferred_wakeup(void) { } 137#endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */ 138 139/** 140 * RCU_NONIDLE - Indicate idle-loop code that needs RCU readers 141 * @a: Code that RCU needs to pay attention to. 142 * 143 * RCU read-side critical sections are forbidden in the inner idle loop, 144 * that is, between the rcu_idle_enter() and the rcu_idle_exit() -- RCU 145 * will happily ignore any such read-side critical sections. However, 146 * things like powertop need tracepoints in the inner idle loop. 147 * 148 * This macro provides the way out: RCU_NONIDLE(do_something_with_RCU()) 149 * will tell RCU that it needs to pay attention, invoke its argument 150 * (in this example, calling the do_something_with_RCU() function), 151 * and then tell RCU to go back to ignoring this CPU. It is permissible 152 * to nest RCU_NONIDLE() wrappers, but not indefinitely (but the limit is 153 * on the order of a million or so, even on 32-bit systems). It is 154 * not legal to block within RCU_NONIDLE(), nor is it permissible to 155 * transfer control either into or out of RCU_NONIDLE()'s statement. 156 */ 157#define RCU_NONIDLE(a) \ 158 do { \ 159 rcu_irq_enter_irqson(); \ 160 do { a; } while (0); \ 161 rcu_irq_exit_irqson(); \ 162 } while (0) 163 164/* 165 * Note a quasi-voluntary context switch for RCU-tasks's benefit. 166 * This is a macro rather than an inline function to avoid #include hell. 167 */ 168#ifdef CONFIG_TASKS_RCU_GENERIC 169 170# ifdef CONFIG_TASKS_RCU 171# define rcu_tasks_classic_qs(t, preempt) \ 172 do { \ 173 if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout)) \ 174 WRITE_ONCE((t)->rcu_tasks_holdout, false); \ 175 } while (0) 176void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func); 177void synchronize_rcu_tasks(void); 178# else 179# define rcu_tasks_classic_qs(t, preempt) do { } while (0) 180# define call_rcu_tasks call_rcu 181# define synchronize_rcu_tasks synchronize_rcu 182# endif 183 184# ifdef CONFIG_TASKS_RCU_TRACE 185# define rcu_tasks_trace_qs(t) \ 186 do { \ 187 struct task_struct_rh *t_rh = (t)->task_struct_rh; \ 188 if (!likely(READ_ONCE(t_rh->trc_reader_checked)) && \ 189 !unlikely(READ_ONCE(t_rh->trc_reader_nesting))) { \ 190 smp_store_release(&t_rh->trc_reader_checked, true); \ 191 smp_mb(); /* Readers partitioned by store. */ \ 192 } \ 193 } while (0) 194# else 195# define rcu_tasks_trace_qs(t) do { } while (0) 196# endif 197 198#define rcu_tasks_qs(t, preempt) \ 199do { \ 200 rcu_tasks_classic_qs((t), (preempt)); \ 201 rcu_tasks_trace_qs((t)); \ 202} while (0) 203 204# ifdef CONFIG_TASKS_RUDE_RCU 205void call_rcu_tasks_rude(struct rcu_head *head, rcu_callback_t func); 206void synchronize_rcu_tasks_rude(void); 207# endif 208 209#define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false) 210void exit_tasks_rcu_start(void); 211void exit_tasks_rcu_finish(void); 212#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */ 213#define rcu_tasks_qs(t, preempt) do { } while (0) 214#define rcu_note_voluntary_context_switch(t) do { } while (0) 215#define call_rcu_tasks call_rcu 216#define synchronize_rcu_tasks synchronize_rcu 217static inline void exit_tasks_rcu_start(void) { } 218static inline void exit_tasks_rcu_finish(void) { } 219#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */ 220 221/** 222 * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU 223 * 224 * This macro resembles cond_resched(), except that it is defined to 225 * report potential quiescent states to RCU-tasks even if the cond_resched() 226 * machinery were to be shut off, as some advocate for PREEMPTION kernels. 227 */ 228#define cond_resched_tasks_rcu_qs() \ 229do { \ 230 rcu_tasks_qs(current, false); \ 231 cond_resched(); \ 232} while (0) 233 234/* 235 * Infrastructure to implement the synchronize_() primitives in 236 * TREE_RCU and rcu_barrier_() primitives in TINY_RCU. 237 */ 238 239#if defined(CONFIG_TREE_RCU) 240#include <linux/rcutree.h> 241#elif defined(CONFIG_TINY_RCU) 242#include <linux/rcutiny.h> 243#else 244#error "Unknown RCU implementation specified to kernel configuration" 245#endif 246 247/* 248 * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls 249 * are needed for dynamic initialization and destruction of rcu_head 250 * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for 251 * dynamic initialization and destruction of statically allocated rcu_head 252 * structures. However, rcu_head structures allocated dynamically in the 253 * heap don't need any initialization. 254 */ 255#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 256void init_rcu_head(struct rcu_head *head); 257void destroy_rcu_head(struct rcu_head *head); 258void init_rcu_head_on_stack(struct rcu_head *head); 259void destroy_rcu_head_on_stack(struct rcu_head *head); 260#else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 261static inline void init_rcu_head(struct rcu_head *head) { } 262static inline void destroy_rcu_head(struct rcu_head *head) { } 263static inline void init_rcu_head_on_stack(struct rcu_head *head) { } 264static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { } 265#endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 266 267#if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) 268bool rcu_lockdep_current_cpu_online(void); 269#else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */ 270static inline bool rcu_lockdep_current_cpu_online(void) { return true; } 271#endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */ 272 273#ifdef CONFIG_DEBUG_LOCK_ALLOC 274 275static inline void rcu_lock_acquire(struct lockdep_map *map) 276{ 277 lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_); 278} 279 280static inline void rcu_lock_release(struct lockdep_map *map) 281{ 282 lock_release(map, _THIS_IP_); 283} 284 285extern struct lockdep_map rcu_lock_map; 286extern struct lockdep_map rcu_bh_lock_map; 287extern struct lockdep_map rcu_sched_lock_map; 288extern struct lockdep_map rcu_callback_map; 289int debug_lockdep_rcu_enabled(void); 290int rcu_read_lock_held(void); 291int rcu_read_lock_bh_held(void); 292int rcu_read_lock_sched_held(void); 293int rcu_read_lock_any_held(void); 294 295#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 296 297# define rcu_lock_acquire(a) do { } while (0) 298# define rcu_lock_release(a) do { } while (0) 299 300static inline int rcu_read_lock_held(void) 301{ 302 return 1; 303} 304 305static inline int rcu_read_lock_bh_held(void) 306{ 307 return 1; 308} 309 310static inline int rcu_read_lock_sched_held(void) 311{ 312 return !preemptible(); 313} 314 315static inline int rcu_read_lock_any_held(void) 316{ 317 return !preemptible(); 318} 319 320#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 321 322#ifdef CONFIG_PROVE_RCU 323 324/** 325 * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met 326 * @c: condition to check 327 * @s: informative message 328 */ 329#define RCU_LOCKDEP_WARN(c, s) \ 330 do { \ 331 static bool __section(.data.unlikely) __warned; \ 332 if (debug_lockdep_rcu_enabled() && !__warned && (c)) { \ 333 __warned = true; \ 334 lockdep_rcu_suspicious(__FILE__, __LINE__, s); \ 335 } \ 336 } while (0) 337 338#if defined(CONFIG_PROVE_RCU) && !defined(CONFIG_PREEMPT_RCU) 339static inline void rcu_preempt_sleep_check(void) 340{ 341 RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map), 342 "Illegal context switch in RCU read-side critical section"); 343} 344#else /* #ifdef CONFIG_PROVE_RCU */ 345static inline void rcu_preempt_sleep_check(void) { } 346#endif /* #else #ifdef CONFIG_PROVE_RCU */ 347 348#define rcu_sleep_check() \ 349 do { \ 350 rcu_preempt_sleep_check(); \ 351 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \ 352 "Illegal context switch in RCU-bh read-side critical section"); \ 353 RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map), \ 354 "Illegal context switch in RCU-sched read-side critical section"); \ 355 } while (0) 356 357#else /* #ifdef CONFIG_PROVE_RCU */ 358 359#define RCU_LOCKDEP_WARN(c, s) do { } while (0) 360#define rcu_sleep_check() do { } while (0) 361 362#endif /* #else #ifdef CONFIG_PROVE_RCU */ 363 364/* 365 * Helper functions for rcu_dereference_check(), rcu_dereference_protected() 366 * and rcu_assign_pointer(). Some of these could be folded into their 367 * callers, but they are left separate in order to ease introduction of 368 * multiple pointers markings to match different RCU implementations 369 * (e.g., __srcu), should this make sense in the future. 370 */ 371 372#ifdef __CHECKER__ 373#define rcu_check_sparse(p, space) \ 374 ((void)(((typeof(*p) space *)p) == p)) 375#else /* #ifdef __CHECKER__ */ 376#define rcu_check_sparse(p, space) 377#endif /* #else #ifdef __CHECKER__ */ 378 379#define __rcu_access_pointer(p, space) \ 380({ \ 381 typeof(*p) *_________p1 = (typeof(*p) *__force)READ_ONCE(p); \ 382 rcu_check_sparse(p, space); \ 383 ((typeof(*p) __force __kernel *)(_________p1)); \ 384}) 385#define __rcu_dereference_check(p, c, space) \ 386({ \ 387 /* Dependency order vs. p above. */ \ 388 typeof(*p) *________p1 = (typeof(*p) *__force)READ_ONCE(p); \ 389 RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \ 390 rcu_check_sparse(p, space); \ 391 ((typeof(*p) __force __kernel *)(________p1)); \ 392}) 393#define __rcu_dereference_protected(p, c, space) \ 394({ \ 395 RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \ 396 rcu_check_sparse(p, space); \ 397 ((typeof(*p) __force __kernel *)(p)); \ 398}) 399#define rcu_dereference_raw(p) \ 400({ \ 401 /* Dependency order vs. p above. */ \ 402 typeof(p) ________p1 = READ_ONCE(p); \ 403 ((typeof(*p) __force __kernel *)(________p1)); \ 404}) 405 406/** 407 * RCU_INITIALIZER() - statically initialize an RCU-protected global variable 408 * @v: The value to statically initialize with. 409 */ 410#define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v) 411 412/** 413 * rcu_assign_pointer() - assign to RCU-protected pointer 414 * @p: pointer to assign to 415 * @v: value to assign (publish) 416 * 417 * Assigns the specified value to the specified RCU-protected 418 * pointer, ensuring that any concurrent RCU readers will see 419 * any prior initialization. 420 * 421 * Inserts memory barriers on architectures that require them 422 * (which is most of them), and also prevents the compiler from 423 * reordering the code that initializes the structure after the pointer 424 * assignment. More importantly, this call documents which pointers 425 * will be dereferenced by RCU read-side code. 426 * 427 * In some special cases, you may use RCU_INIT_POINTER() instead 428 * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due 429 * to the fact that it does not constrain either the CPU or the compiler. 430 * That said, using RCU_INIT_POINTER() when you should have used 431 * rcu_assign_pointer() is a very bad thing that results in 432 * impossible-to-diagnose memory corruption. So please be careful. 433 * See the RCU_INIT_POINTER() comment header for details. 434 * 435 * Note that rcu_assign_pointer() evaluates each of its arguments only 436 * once, appearances notwithstanding. One of the "extra" evaluations 437 * is in typeof() and the other visible only to sparse (__CHECKER__), 438 * neither of which actually execute the argument. As with most cpp 439 * macros, this execute-arguments-only-once property is important, so 440 * please be careful when making changes to rcu_assign_pointer() and the 441 * other macros that it invokes. 442 */ 443#define rcu_assign_pointer(p, v) \ 444do { \ 445 uintptr_t _r_a_p__v = (uintptr_t)(v); \ 446 rcu_check_sparse(p, __rcu); \ 447 \ 448 if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \ 449 WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \ 450 else \ 451 smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \ 452} while (0) 453 454/** 455 * rcu_replace_pointer() - replace an RCU pointer, returning its old value 456 * @rcu_ptr: RCU pointer, whose old value is returned 457 * @ptr: regular pointer 458 * @c: the lockdep conditions under which the dereference will take place 459 * 460 * Perform a replacement, where @rcu_ptr is an RCU-annotated 461 * pointer and @c is the lockdep argument that is passed to the 462 * rcu_dereference_protected() call used to read that pointer. The old 463 * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr. 464 */ 465#define rcu_replace_pointer(rcu_ptr, ptr, c) \ 466({ \ 467 typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c)); \ 468 rcu_assign_pointer((rcu_ptr), (ptr)); \ 469 __tmp; \ 470}) 471 472/** 473 * rcu_swap_protected() - swap an RCU and a regular pointer 474 * @rcu_ptr: RCU pointer 475 * @ptr: regular pointer 476 * @c: the conditions under which the dereference will take place 477 * 478 * Perform swap(@rcu_ptr, @ptr) where @rcu_ptr is an RCU-annotated pointer and 479 * @c is the argument that is passed to the rcu_dereference_protected() call 480 * used to read that pointer. 481 */ 482#define rcu_swap_protected(rcu_ptr, ptr, c) do { \ 483 typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c)); \ 484 rcu_assign_pointer((rcu_ptr), (ptr)); \ 485 (ptr) = __tmp; \ 486} while (0) 487 488/** 489 * rcu_access_pointer() - fetch RCU pointer with no dereferencing 490 * @p: The pointer to read 491 * 492 * Return the value of the specified RCU-protected pointer, but omit the 493 * lockdep checks for being in an RCU read-side critical section. This is 494 * useful when the value of this pointer is accessed, but the pointer is 495 * not dereferenced, for example, when testing an RCU-protected pointer 496 * against NULL. Although rcu_access_pointer() may also be used in cases 497 * where update-side locks prevent the value of the pointer from changing, 498 * you should instead use rcu_dereference_protected() for this use case. 499 * 500 * It is also permissible to use rcu_access_pointer() when read-side 501 * access to the pointer was removed at least one grace period ago, as 502 * is the case in the context of the RCU callback that is freeing up 503 * the data, or after a synchronize_rcu() returns. This can be useful 504 * when tearing down multi-linked structures after a grace period 505 * has elapsed. 506 */ 507#define rcu_access_pointer(p) __rcu_access_pointer((p), __rcu) 508 509/** 510 * rcu_dereference_check() - rcu_dereference with debug checking 511 * @p: The pointer to read, prior to dereferencing 512 * @c: The conditions under which the dereference will take place 513 * 514 * Do an rcu_dereference(), but check that the conditions under which the 515 * dereference will take place are correct. Typically the conditions 516 * indicate the various locking conditions that should be held at that 517 * point. The check should return true if the conditions are satisfied. 518 * An implicit check for being in an RCU read-side critical section 519 * (rcu_read_lock()) is included. 520 * 521 * For example: 522 * 523 * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock)); 524 * 525 * could be used to indicate to lockdep that foo->bar may only be dereferenced 526 * if either rcu_read_lock() is held, or that the lock required to replace 527 * the bar struct at foo->bar is held. 528 * 529 * Note that the list of conditions may also include indications of when a lock 530 * need not be held, for example during initialisation or destruction of the 531 * target struct: 532 * 533 * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) || 534 * atomic_read(&foo->usage) == 0); 535 * 536 * Inserts memory barriers on architectures that require them 537 * (currently only the Alpha), prevents the compiler from refetching 538 * (and from merging fetches), and, more importantly, documents exactly 539 * which pointers are protected by RCU and checks that the pointer is 540 * annotated as __rcu. 541 */ 542#define rcu_dereference_check(p, c) \ 543 __rcu_dereference_check((p), (c) || rcu_read_lock_held(), __rcu) 544 545/** 546 * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking 547 * @p: The pointer to read, prior to dereferencing 548 * @c: The conditions under which the dereference will take place 549 * 550 * This is the RCU-bh counterpart to rcu_dereference_check(). 551 */ 552#define rcu_dereference_bh_check(p, c) \ 553 __rcu_dereference_check((p), (c) || rcu_read_lock_bh_held(), __rcu) 554 555/** 556 * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking 557 * @p: The pointer to read, prior to dereferencing 558 * @c: The conditions under which the dereference will take place 559 * 560 * This is the RCU-sched counterpart to rcu_dereference_check(). 561 */ 562#define rcu_dereference_sched_check(p, c) \ 563 __rcu_dereference_check((p), (c) || rcu_read_lock_sched_held(), \ 564 __rcu) 565 566/* 567 * The tracing infrastructure traces RCU (we want that), but unfortunately 568 * some of the RCU checks causes tracing to lock up the system. 569 * 570 * The no-tracing version of rcu_dereference_raw() must not call 571 * rcu_read_lock_held(). 572 */ 573#define rcu_dereference_raw_notrace(p) __rcu_dereference_check((p), 1, __rcu) 574 575/** 576 * rcu_dereference_protected() - fetch RCU pointer when updates prevented 577 * @p: The pointer to read, prior to dereferencing 578 * @c: The conditions under which the dereference will take place 579 * 580 * Return the value of the specified RCU-protected pointer, but omit 581 * the READ_ONCE(). This is useful in cases where update-side locks 582 * prevent the value of the pointer from changing. Please note that this 583 * primitive does *not* prevent the compiler from repeating this reference 584 * or combining it with other references, so it should not be used without 585 * protection of appropriate locks. 586 * 587 * This function is only for update-side use. Using this function 588 * when protected only by rcu_read_lock() will result in infrequent 589 * but very ugly failures. 590 */ 591#define rcu_dereference_protected(p, c) \ 592 __rcu_dereference_protected((p), (c), __rcu) 593 594 595/** 596 * rcu_dereference() - fetch RCU-protected pointer for dereferencing 597 * @p: The pointer to read, prior to dereferencing 598 * 599 * This is a simple wrapper around rcu_dereference_check(). 600 */ 601#define rcu_dereference(p) rcu_dereference_check(p, 0) 602 603/** 604 * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing 605 * @p: The pointer to read, prior to dereferencing 606 * 607 * Makes rcu_dereference_check() do the dirty work. 608 */ 609#define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0) 610 611/** 612 * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing 613 * @p: The pointer to read, prior to dereferencing 614 * 615 * Makes rcu_dereference_check() do the dirty work. 616 */ 617#define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0) 618 619/** 620 * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism 621 * @p: The pointer to hand off 622 * 623 * This is simply an identity function, but it documents where a pointer 624 * is handed off from RCU to some other synchronization mechanism, for 625 * example, reference counting or locking. In C11, it would map to 626 * kill_dependency(). It could be used as follows:: 627 * 628 * rcu_read_lock(); 629 * p = rcu_dereference(gp); 630 * long_lived = is_long_lived(p); 631 * if (long_lived) { 632 * if (!atomic_inc_not_zero(p->refcnt)) 633 * long_lived = false; 634 * else 635 * p = rcu_pointer_handoff(p); 636 * } 637 * rcu_read_unlock(); 638 */ 639#define rcu_pointer_handoff(p) (p) 640 641/** 642 * rcu_read_lock() - mark the beginning of an RCU read-side critical section 643 * 644 * When synchronize_rcu() is invoked on one CPU while other CPUs 645 * are within RCU read-side critical sections, then the 646 * synchronize_rcu() is guaranteed to block until after all the other 647 * CPUs exit their critical sections. Similarly, if call_rcu() is invoked 648 * on one CPU while other CPUs are within RCU read-side critical 649 * sections, invocation of the corresponding RCU callback is deferred 650 * until after the all the other CPUs exit their critical sections. 651 * 652 * Note, however, that RCU callbacks are permitted to run concurrently 653 * with new RCU read-side critical sections. One way that this can happen 654 * is via the following sequence of events: (1) CPU 0 enters an RCU 655 * read-side critical section, (2) CPU 1 invokes call_rcu() to register 656 * an RCU callback, (3) CPU 0 exits the RCU read-side critical section, 657 * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU 658 * callback is invoked. This is legal, because the RCU read-side critical 659 * section that was running concurrently with the call_rcu() (and which 660 * therefore might be referencing something that the corresponding RCU 661 * callback would free up) has completed before the corresponding 662 * RCU callback is invoked. 663 * 664 * RCU read-side critical sections may be nested. Any deferred actions 665 * will be deferred until the outermost RCU read-side critical section 666 * completes. 667 * 668 * You can avoid reading and understanding the next paragraph by 669 * following this rule: don't put anything in an rcu_read_lock() RCU 670 * read-side critical section that would block in a !PREEMPTION kernel. 671 * But if you want the full story, read on! 672 * 673 * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU), 674 * it is illegal to block while in an RCU read-side critical section. 675 * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION 676 * kernel builds, RCU read-side critical sections may be preempted, 677 * but explicit blocking is illegal. Finally, in preemptible RCU 678 * implementations in real-time (with -rt patchset) kernel builds, RCU 679 * read-side critical sections may be preempted and they may also block, but 680 * only when acquiring spinlocks that are subject to priority inheritance. 681 */ 682static __always_inline void rcu_read_lock(void) 683{ 684 __rcu_read_lock(); 685 __acquire(RCU); 686 rcu_lock_acquire(&rcu_lock_map); 687 RCU_LOCKDEP_WARN(!rcu_is_watching(), 688 "rcu_read_lock() used illegally while idle"); 689} 690 691/* 692 * So where is rcu_write_lock()? It does not exist, as there is no 693 * way for writers to lock out RCU readers. This is a feature, not 694 * a bug -- this property is what provides RCU's performance benefits. 695 * Of course, writers must coordinate with each other. The normal 696 * spinlock primitives work well for this, but any other technique may be 697 * used as well. RCU does not care how the writers keep out of each 698 * others' way, as long as they do so. 699 */ 700 701/** 702 * rcu_read_unlock() - marks the end of an RCU read-side critical section. 703 * 704 * In most situations, rcu_read_unlock() is immune from deadlock. 705 * However, in kernels built with CONFIG_RCU_BOOST, rcu_read_unlock() 706 * is responsible for deboosting, which it does via rt_mutex_unlock(). 707 * Unfortunately, this function acquires the scheduler's runqueue and 708 * priority-inheritance spinlocks. This means that deadlock could result 709 * if the caller of rcu_read_unlock() already holds one of these locks or 710 * any lock that is ever acquired while holding them. 711 * 712 * That said, RCU readers are never priority boosted unless they were 713 * preempted. Therefore, one way to avoid deadlock is to make sure 714 * that preemption never happens within any RCU read-side critical 715 * section whose outermost rcu_read_unlock() is called with one of 716 * rt_mutex_unlock()'s locks held. Such preemption can be avoided in 717 * a number of ways, for example, by invoking preempt_disable() before 718 * critical section's outermost rcu_read_lock(). 719 * 720 * Given that the set of locks acquired by rt_mutex_unlock() might change 721 * at any time, a somewhat more future-proofed approach is to make sure 722 * that that preemption never happens within any RCU read-side critical 723 * section whose outermost rcu_read_unlock() is called with irqs disabled. 724 * This approach relies on the fact that rt_mutex_unlock() currently only 725 * acquires irq-disabled locks. 726 * 727 * The second of these two approaches is best in most situations, 728 * however, the first approach can also be useful, at least to those 729 * developers willing to keep abreast of the set of locks acquired by 730 * rt_mutex_unlock(). 731 * 732 * See rcu_read_lock() for more information. 733 */ 734static inline void rcu_read_unlock(void) 735{ 736 RCU_LOCKDEP_WARN(!rcu_is_watching(), 737 "rcu_read_unlock() used illegally while idle"); 738 __release(RCU); 739 __rcu_read_unlock(); 740 rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */ 741} 742 743/** 744 * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section 745 * 746 * This is equivalent of rcu_read_lock(), but also disables softirqs. 747 * Note that anything else that disables softirqs can also serve as 748 * an RCU read-side critical section. 749 * 750 * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh() 751 * must occur in the same context, for example, it is illegal to invoke 752 * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh() 753 * was invoked from some other task. 754 */ 755static inline void rcu_read_lock_bh(void) 756{ 757 local_bh_disable(); 758 __acquire(RCU_BH); 759 rcu_lock_acquire(&rcu_bh_lock_map); 760 RCU_LOCKDEP_WARN(!rcu_is_watching(), 761 "rcu_read_lock_bh() used illegally while idle"); 762} 763 764/** 765 * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section 766 * 767 * See rcu_read_lock_bh() for more information. 768 */ 769static inline void rcu_read_unlock_bh(void) 770{ 771 RCU_LOCKDEP_WARN(!rcu_is_watching(), 772 "rcu_read_unlock_bh() used illegally while idle"); 773 rcu_lock_release(&rcu_bh_lock_map); 774 __release(RCU_BH); 775 local_bh_enable(); 776} 777 778/** 779 * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section 780 * 781 * This is equivalent of rcu_read_lock(), but disables preemption. 782 * Read-side critical sections can also be introduced by anything else 783 * that disables preemption, including local_irq_disable() and friends. 784 * 785 * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched() 786 * must occur in the same context, for example, it is illegal to invoke 787 * rcu_read_unlock_sched() from process context if the matching 788 * rcu_read_lock_sched() was invoked from an NMI handler. 789 */ 790static inline void rcu_read_lock_sched(void) 791{ 792 preempt_disable(); 793 __acquire(RCU_SCHED); 794 rcu_lock_acquire(&rcu_sched_lock_map); 795 RCU_LOCKDEP_WARN(!rcu_is_watching(), 796 "rcu_read_lock_sched() used illegally while idle"); 797} 798 799/* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ 800static inline notrace void rcu_read_lock_sched_notrace(void) 801{ 802 preempt_disable_notrace(); 803 __acquire(RCU_SCHED); 804} 805 806/** 807 * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section 808 * 809 * See rcu_read_lock_sched() for more information. 810 */ 811static inline void rcu_read_unlock_sched(void) 812{ 813 RCU_LOCKDEP_WARN(!rcu_is_watching(), 814 "rcu_read_unlock_sched() used illegally while idle"); 815 rcu_lock_release(&rcu_sched_lock_map); 816 __release(RCU_SCHED); 817 preempt_enable(); 818} 819 820/* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ 821static inline notrace void rcu_read_unlock_sched_notrace(void) 822{ 823 __release(RCU_SCHED); 824 preempt_enable_notrace(); 825} 826 827/** 828 * RCU_INIT_POINTER() - initialize an RCU protected pointer 829 * @p: The pointer to be initialized. 830 * @v: The value to initialized the pointer to. 831 * 832 * Initialize an RCU-protected pointer in special cases where readers 833 * do not need ordering constraints on the CPU or the compiler. These 834 * special cases are: 835 * 836 * 1. This use of RCU_INIT_POINTER() is NULLing out the pointer *or* 837 * 2. The caller has taken whatever steps are required to prevent 838 * RCU readers from concurrently accessing this pointer *or* 839 * 3. The referenced data structure has already been exposed to 840 * readers either at compile time or via rcu_assign_pointer() *and* 841 * 842 * a. You have not made *any* reader-visible changes to 843 * this structure since then *or* 844 * b. It is OK for readers accessing this structure from its 845 * new location to see the old state of the structure. (For 846 * example, the changes were to statistical counters or to 847 * other state where exact synchronization is not required.) 848 * 849 * Failure to follow these rules governing use of RCU_INIT_POINTER() will 850 * result in impossible-to-diagnose memory corruption. As in the structures 851 * will look OK in crash dumps, but any concurrent RCU readers might 852 * see pre-initialized values of the referenced data structure. So 853 * please be very careful how you use RCU_INIT_POINTER()!!! 854 * 855 * If you are creating an RCU-protected linked structure that is accessed 856 * by a single external-to-structure RCU-protected pointer, then you may 857 * use RCU_INIT_POINTER() to initialize the internal RCU-protected 858 * pointers, but you must use rcu_assign_pointer() to initialize the 859 * external-to-structure pointer *after* you have completely initialized 860 * the reader-accessible portions of the linked structure. 861 * 862 * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no 863 * ordering guarantees for either the CPU or the compiler. 864 */ 865#define RCU_INIT_POINTER(p, v) \ 866 do { \ 867 rcu_check_sparse(p, __rcu); \ 868 WRITE_ONCE(p, RCU_INITIALIZER(v)); \ 869 } while (0) 870 871/** 872 * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer 873 * @p: The pointer to be initialized. 874 * @v: The value to initialized the pointer to. 875 * 876 * GCC-style initialization for an RCU-protected pointer in a structure field. 877 */ 878#define RCU_POINTER_INITIALIZER(p, v) \ 879 .p = RCU_INITIALIZER(v) 880 881/* 882 * Does the specified offset indicate that the corresponding rcu_head 883 * structure can be handled by kvfree_rcu()? 884 */ 885#define __is_kvfree_rcu_offset(offset) ((offset) < 4096) 886 887/** 888 * kfree_rcu() - kfree an object after a grace period. 889 * @ptr: pointer to kfree for both single- and double-argument invocations. 890 * @rhf: the name of the struct rcu_head within the type of @ptr, 891 * but only for double-argument invocations. 892 * 893 * Many rcu callbacks functions just call kfree() on the base structure. 894 * These functions are trivial, but their size adds up, and furthermore 895 * when they are used in a kernel module, that module must invoke the 896 * high-latency rcu_barrier() function at module-unload time. 897 * 898 * The kfree_rcu() function handles this issue. Rather than encoding a 899 * function address in the embedded rcu_head structure, kfree_rcu() instead 900 * encodes the offset of the rcu_head structure within the base structure. 901 * Because the functions are not allowed in the low-order 4096 bytes of 902 * kernel virtual memory, offsets up to 4095 bytes can be accommodated. 903 * If the offset is larger than 4095 bytes, a compile-time error will 904 * be generated in kvfree_rcu_arg_2(). If this error is triggered, you can 905 * either fall back to use of call_rcu() or rearrange the structure to 906 * position the rcu_head structure into the first 4096 bytes. 907 * 908 * Note that the allowable offset might decrease in the future, for example, 909 * to allow something like kmem_cache_free_rcu(). 910 * 911 * The BUILD_BUG_ON check must not involve any function calls, hence the 912 * checks are done in macros here. 913 */ 914#define kfree_rcu(ptr, rhf...) kvfree_rcu(ptr, ## rhf) 915 916/** 917 * kvfree_rcu() - kvfree an object after a grace period. 918 * 919 * This macro consists of one or two arguments and it is 920 * based on whether an object is head-less or not. If it 921 * has a head then a semantic stays the same as it used 922 * to be before: 923 * 924 * kvfree_rcu(ptr, rhf); 925 * 926 * where @ptr is a pointer to kvfree(), @rhf is the name 927 * of the rcu_head structure within the type of @ptr. 928 * 929 * When it comes to head-less variant, only one argument 930 * is passed and that is just a pointer which has to be 931 * freed after a grace period. Therefore the semantic is 932 * 933 * kvfree_rcu(ptr); 934 * 935 * where @ptr is a pointer to kvfree(). 936 * 937 * Please note, head-less way of freeing is permitted to 938 * use from a context that has to follow might_sleep() 939 * annotation. Otherwise, please switch and embed the 940 * rcu_head structure within the type of @ptr. 941 */ 942#define kvfree_rcu(...) KVFREE_GET_MACRO(__VA_ARGS__, \ 943 kvfree_rcu_arg_2, kvfree_rcu_arg_1)(__VA_ARGS__) 944 945#define KVFREE_GET_MACRO(_1, _2, NAME, ...) NAME 946#define kvfree_rcu_arg_2(ptr, rhf) \ 947do { \ 948 typeof (ptr) ___p = (ptr); \ 949 \ 950 if (___p) { \ 951 BUILD_BUG_ON(!__is_kvfree_rcu_offset(offsetof(typeof(*(ptr)), rhf))); \ 952 kvfree_call_rcu(&((___p)->rhf), (rcu_callback_t)(unsigned long) \ 953 (offsetof(typeof(*(ptr)), rhf))); \ 954 } \ 955} while (0) 956 957#define kvfree_rcu_arg_1(ptr) \ 958do { \ 959 typeof(ptr) ___p = (ptr); \ 960 \ 961 if (___p) \ 962 kvfree_call_rcu(NULL, (rcu_callback_t) (___p)); \ 963} while (0) 964 965/* 966 * Place this after a lock-acquisition primitive to guarantee that 967 * an UNLOCK+LOCK pair acts as a full barrier. This guarantee applies 968 * if the UNLOCK and LOCK are executed by the same CPU or if the 969 * UNLOCK and LOCK operate on the same lock variable. 970 */ 971#ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE 972#define smp_mb__after_unlock_lock() smp_mb() /* Full ordering for lock. */ 973#else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */ 974#define smp_mb__after_unlock_lock() do { } while (0) 975#endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */ 976 977 978/* Has the specified rcu_head structure been handed to call_rcu()? */ 979 980/** 981 * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu() 982 * @rhp: The rcu_head structure to initialize. 983 * 984 * If you intend to invoke rcu_head_after_call_rcu() to test whether a 985 * given rcu_head structure has already been passed to call_rcu(), then 986 * you must also invoke this rcu_head_init() function on it just after 987 * allocating that structure. Calls to this function must not race with 988 * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation. 989 */ 990static inline void rcu_head_init(struct rcu_head *rhp) 991{ 992 rhp->func = (rcu_callback_t)~0L; 993} 994 995/** 996 * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()? 997 * @rhp: The rcu_head structure to test. 998 * @f: The function passed to call_rcu() along with @rhp. 999 * 1000 * Returns @true if the @rhp has been passed to call_rcu() with @func,
1001 * and @false otherwise. Emits a warning in any other case, including 1002 * the case where @rhp has already been invoked after a grace period. 1003 * Calls to this function must not race with callback invocation. One way 1004 * to avoid such races is to enclose the call to rcu_head_after_call_rcu() 1005 * in an RCU read-side critical section that includes a read-side fetch 1006 * of the pointer to the structure containing @rhp. 1007 */ 1008static inline bool 1009rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f) 1010{ 1011 rcu_callback_t func = READ_ONCE(rhp->func); 1012 1013 if (func == f) 1014 return true; 1015 WARN_ON_ONCE(func != (rcu_callback_t)~0L); 1016 return false; 1017} 1018 1019/* kernel/ksysfs.c definitions */ 1020extern int rcu_expedited; 1021extern int rcu_normal; 1022 1023#endif /* __LINUX_RCUPDATE_H */ 1024