1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Read-Copy Update mechanism for mutual exclusion 4 * 5 * Copyright IBM Corporation, 2001 6 * 7 * Author: Dipankar Sarma <dipankar@in.ibm.com> 8 * 9 * Based on the original work by Paul McKenney <paulmck@vnet.ibm.com> 10 * and inputs from Rusty Russell, Andrea Arcangeli and Andi Kleen. 11 * Papers: 12 * http://www.rdrop.com/users/paulmck/paper/rclockpdcsproof.pdf 13 * http://lse.sourceforge.net/locking/rclock_OLS.2001.05.01c.sc.pdf (OLS2001) 14 * 15 * For detailed explanation of Read-Copy Update mechanism see - 16 * http://lse.sourceforge.net/locking/rcupdate.html 17 * 18 */ 19 20 #ifndef __LINUX_RCUPDATE_H 21 #define __LINUX_RCUPDATE_H 22 23 #include <linux/types.h> 24 #include <linux/compiler.h> 25 #include <linux/atomic.h> 26 #include <linux/irqflags.h> 27 #include <linux/sched.h> 28 #include <linux/bottom_half.h> 29 #include <linux/lockdep.h> 30 #include <linux/cleanup.h> 31 #include <asm/processor.h> 32 #include <linux/context_tracking_irq.h> 33 34 token_context_lock(RCU, __reentrant_ctx_lock); 35 token_context_lock_instance(RCU, RCU_SCHED); 36 token_context_lock_instance(RCU, RCU_BH); 37 38 /* 39 * A convenience macro that can be used for RCU-protected globals or struct 40 * members; adds type qualifier __rcu, and also enforces __guarded_by(RCU). 41 */ 42 #define __rcu_guarded __rcu __guarded_by(RCU) 43 44 #define ULONG_CMP_GE(a, b) (ULONG_MAX / 2 >= (a) - (b)) 45 #define ULONG_CMP_LT(a, b) (ULONG_MAX / 2 < (a) - (b)) 46 47 #define RCU_SEQ_CTR_SHIFT 2 48 #define RCU_SEQ_STATE_MASK ((1 << RCU_SEQ_CTR_SHIFT) - 1) 49 50 /* Exported common interfaces */ 51 void call_rcu(struct rcu_head *head, rcu_callback_t func); 52 void rcu_barrier_tasks(void); 53 void synchronize_rcu(void); 54 55 /* 56 * Grace-period sequence snapshot for the polled RCU APIs: ->norm for the 57 * normal grace period and ->exp for the expedited one. ->exp is unused by 58 * Tiny RCU, but is present unconditionally so that a single definition 59 * serves both Tiny RCU and Tree RCU. 60 */ 61 struct rcu_gp_seq { 62 unsigned long norm; 63 unsigned long exp; 64 }; 65 unsigned long get_completed_synchronize_rcu(void); 66 void get_completed_synchronize_rcu_full(struct rcu_gp_seq *gsp); 67 68 // Maximum number of unsigned long values corresponding to 69 // not-yet-completed RCU grace periods. 70 #define NUM_ACTIVE_RCU_POLL_OLDSTATE 2 71 72 /** 73 * same_state_synchronize_rcu - Are two old-state values identical? 74 * @oldstate1: First old-state value. 75 * @oldstate2: Second old-state value. 76 * 77 * The two old-state values must have been obtained from either 78 * get_state_synchronize_rcu(), start_poll_synchronize_rcu(), or 79 * get_completed_synchronize_rcu(). Returns @true if the two values are 80 * identical and @false otherwise. This allows structures whose lifetimes 81 * are tracked by old-state values to push these values to a list header, 82 * allowing those structures to be slightly smaller. 83 */ 84 static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned long oldstate2) 85 { 86 return oldstate1 == oldstate2; 87 } 88 89 #ifdef CONFIG_PREEMPT_RCU 90 91 void __rcu_read_lock(void); 92 void __rcu_read_unlock(void); 93 94 /* 95 * Defined as a macro as it is a very low level header included from 96 * areas that don't even know about current. This gives the rcu_read_lock() 97 * nesting depth, but makes sense only if CONFIG_PREEMPT_RCU -- in other 98 * types of kernel builds, the rcu_read_lock() nesting depth is unknowable. 99 */ 100 #define rcu_preempt_depth() READ_ONCE(current->rcu_read_lock_nesting) 101 102 #else /* #ifdef CONFIG_PREEMPT_RCU */ 103 104 #ifdef CONFIG_TINY_RCU 105 #define rcu_read_unlock_strict() do { } while (0) 106 #else 107 void rcu_read_unlock_strict(void); 108 #endif 109 110 static inline void __rcu_read_lock(void) 111 { 112 preempt_disable(); 113 } 114 115 static inline void __rcu_read_unlock(void) 116 { 117 if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) 118 rcu_read_unlock_strict(); 119 preempt_enable(); 120 } 121 122 static inline int rcu_preempt_depth(void) 123 { 124 return 0; 125 } 126 127 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */ 128 129 #ifdef CONFIG_RCU_LAZY 130 void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func); 131 #else 132 static inline void call_rcu_hurry(struct rcu_head *head, rcu_callback_t func) 133 { 134 call_rcu(head, func); 135 } 136 #endif 137 138 /* Internal to kernel */ 139 void rcu_init(void); 140 extern int rcu_scheduler_active; 141 void rcu_sched_clock_irq(int user); 142 143 #ifdef CONFIG_RCU_STALL_COMMON 144 void rcu_sysrq_start(void); 145 void rcu_sysrq_end(void); 146 #else /* #ifdef CONFIG_RCU_STALL_COMMON */ 147 static inline void rcu_sysrq_start(void) { } 148 static inline void rcu_sysrq_end(void) { } 149 #endif /* #else #ifdef CONFIG_RCU_STALL_COMMON */ 150 151 #if defined(CONFIG_NO_HZ_FULL) && (!defined(CONFIG_GENERIC_ENTRY) || !defined(CONFIG_VIRT_XFER_TO_GUEST_WORK)) 152 void rcu_irq_work_resched(void); 153 #else 154 static __always_inline void rcu_irq_work_resched(void) { } 155 #endif 156 157 #ifdef CONFIG_RCU_NOCB_CPU 158 void rcu_init_nohz(void); 159 int rcu_nocb_cpu_offload(int cpu); 160 int rcu_nocb_cpu_deoffload(int cpu); 161 void rcu_nocb_flush_deferred_wakeup(void); 162 163 #define RCU_NOCB_LOCKDEP_WARN(c, s) RCU_LOCKDEP_WARN(c, s) 164 165 #else /* #ifdef CONFIG_RCU_NOCB_CPU */ 166 167 static inline void rcu_init_nohz(void) { } 168 static inline int rcu_nocb_cpu_offload(int cpu) { return -EINVAL; } 169 static inline int rcu_nocb_cpu_deoffload(int cpu) { return 0; } 170 static inline void rcu_nocb_flush_deferred_wakeup(void) { } 171 172 #define RCU_NOCB_LOCKDEP_WARN(c, s) 173 174 #endif /* #else #ifdef CONFIG_RCU_NOCB_CPU */ 175 176 /* 177 * Note a quasi-voluntary context switch for RCU-tasks's benefit. 178 * This is a macro rather than an inline function to avoid #include hell. 179 */ 180 #ifdef CONFIG_TASKS_RCU_GENERIC 181 182 # ifdef CONFIG_TASKS_RCU 183 # define rcu_tasks_classic_qs(t, preempt) \ 184 do { \ 185 if (!(preempt) && READ_ONCE((t)->rcu_tasks_holdout)) \ 186 WRITE_ONCE((t)->rcu_tasks_holdout, false); \ 187 } while (0) 188 void call_rcu_tasks(struct rcu_head *head, rcu_callback_t func); 189 void synchronize_rcu_tasks(void); 190 void rcu_tasks_torture_stats_print(char *tt, char *tf); 191 # else 192 # define rcu_tasks_classic_qs(t, preempt) do { } while (0) 193 # define call_rcu_tasks call_rcu 194 # define synchronize_rcu_tasks synchronize_rcu 195 # endif 196 197 #define rcu_tasks_qs(t, preempt) rcu_tasks_classic_qs((t), (preempt)) 198 199 # ifdef CONFIG_TASKS_RUDE_RCU 200 void synchronize_rcu_tasks_rude(void); 201 void rcu_tasks_rude_torture_stats_print(char *tt, char *tf); 202 # endif 203 204 #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false) 205 void exit_tasks_rcu_start(void); 206 void exit_tasks_rcu_finish(void); 207 #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */ 208 #define rcu_tasks_classic_qs(t, preempt) do { } while (0) 209 #define rcu_tasks_qs(t, preempt) do { } while (0) 210 #define rcu_note_voluntary_context_switch(t) do { } while (0) 211 #define call_rcu_tasks call_rcu 212 #define synchronize_rcu_tasks synchronize_rcu 213 static inline void exit_tasks_rcu_start(void) { } 214 static inline void exit_tasks_rcu_finish(void) { } 215 #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */ 216 217 /** 218 * cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU 219 * 220 * This function resembles cond_resched(), except that it is defined to 221 * report potential quiescent states to RCU-tasks even if the cond_resched() 222 * machinery were to be shut off, as some advocate for PREEMPTION kernels. 223 */ 224 static inline void cond_resched_tasks_rcu_qs(void) 225 { 226 rcu_tasks_qs(current, false); 227 cond_resched(); 228 } 229 230 /** 231 * rcu_softirq_qs_periodic - Report RCU and RCU-Tasks quiescent states 232 * @old_ts: jiffies at start of processing. 233 * 234 * This helper is for long-running softirq handlers, such as NAPI threads in 235 * networking. The caller should initialize the variable passed in as @old_ts 236 * at the beginning of the softirq handler. When invoked frequently, this macro 237 * will invoke rcu_softirq_qs() every 100 milliseconds thereafter, which will 238 * provide both RCU and RCU-Tasks quiescent states. Note that this macro 239 * modifies its old_ts argument. 240 * 241 * Because regions of code that have disabled softirq act as RCU read-side 242 * critical sections, this macro should be invoked with softirq (and 243 * preemption) enabled. 244 * 245 * The macro is not needed when CONFIG_PREEMPT_RT is defined. RT kernels would 246 * have more chance to invoke schedule() calls and provide necessary quiescent 247 * states. As a contrast, calling cond_resched() only won't achieve the same 248 * effect because cond_resched() does not provide RCU-Tasks quiescent states. 249 */ 250 #define rcu_softirq_qs_periodic(old_ts) \ 251 do { \ 252 if (!IS_ENABLED(CONFIG_PREEMPT_RT) && \ 253 time_after(jiffies, (old_ts) + HZ / 10)) { \ 254 preempt_disable(); \ 255 rcu_softirq_qs(); \ 256 preempt_enable(); \ 257 (old_ts) = jiffies; \ 258 } \ 259 } while (0) 260 261 /* 262 * Infrastructure to implement the synchronize_() primitives in 263 * TREE_RCU and rcu_barrier_() primitives in TINY_RCU. 264 */ 265 266 #if defined(CONFIG_TREE_RCU) 267 #include <linux/rcutree.h> 268 #elif defined(CONFIG_TINY_RCU) 269 #include <linux/rcutiny.h> 270 #else 271 #error "Unknown RCU implementation specified to kernel configuration" 272 #endif 273 274 /* 275 * The init_rcu_head_on_stack() and destroy_rcu_head_on_stack() calls 276 * are needed for dynamic initialization and destruction of rcu_head 277 * on the stack, and init_rcu_head()/destroy_rcu_head() are needed for 278 * dynamic initialization and destruction of statically allocated rcu_head 279 * structures. However, rcu_head structures allocated dynamically in the 280 * heap don't need any initialization. 281 */ 282 #ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 283 void init_rcu_head(struct rcu_head *head); 284 void destroy_rcu_head(struct rcu_head *head); 285 void init_rcu_head_on_stack(struct rcu_head *head); 286 void destroy_rcu_head_on_stack(struct rcu_head *head); 287 #else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 288 static inline void init_rcu_head(struct rcu_head *head) { } 289 static inline void destroy_rcu_head(struct rcu_head *head) { } 290 static inline void init_rcu_head_on_stack(struct rcu_head *head) { } 291 static inline void destroy_rcu_head_on_stack(struct rcu_head *head) { } 292 #endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 293 294 #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) 295 bool rcu_lockdep_current_cpu_online(void); 296 #else /* #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */ 297 static inline bool rcu_lockdep_current_cpu_online(void) { return true; } 298 #endif /* #else #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PROVE_RCU) */ 299 300 extern struct lockdep_map rcu_lock_map; 301 extern struct lockdep_map rcu_bh_lock_map; 302 extern struct lockdep_map rcu_sched_lock_map; 303 extern struct lockdep_map rcu_callback_map; 304 305 #ifdef CONFIG_DEBUG_LOCK_ALLOC 306 307 static inline void rcu_lock_acquire(struct lockdep_map *map) 308 { 309 lock_acquire(map, 0, 0, 2, 0, NULL, _THIS_IP_); 310 } 311 312 static inline void rcu_try_lock_acquire(struct lockdep_map *map) 313 { 314 lock_acquire(map, 0, 1, 2, 0, NULL, _THIS_IP_); 315 } 316 317 static inline void rcu_lock_release(struct lockdep_map *map) 318 { 319 lock_release(map, _THIS_IP_); 320 } 321 322 int debug_lockdep_rcu_enabled(void); 323 int rcu_read_lock_held(void); 324 int rcu_read_lock_bh_held(void); 325 int rcu_read_lock_sched_held(void); 326 int rcu_read_lock_any_held(void); 327 328 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 329 330 # define rcu_lock_acquire(a) do { } while (0) 331 # define rcu_try_lock_acquire(a) do { } while (0) 332 # define rcu_lock_release(a) do { } while (0) 333 334 static inline int rcu_read_lock_held(void) 335 { 336 return 1; 337 } 338 339 static inline int rcu_read_lock_bh_held(void) 340 { 341 return 1; 342 } 343 344 static inline int rcu_read_lock_sched_held(void) 345 { 346 return !preemptible(); 347 } 348 349 static inline int rcu_read_lock_any_held(void) 350 { 351 return !preemptible(); 352 } 353 354 static inline int debug_lockdep_rcu_enabled(void) 355 { 356 return 0; 357 } 358 359 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 360 361 #ifdef CONFIG_PROVE_RCU 362 363 /** 364 * RCU_LOCKDEP_WARN - emit lockdep splat if specified condition is met 365 * @c: condition to check 366 * @s: informative message 367 * 368 * This checks debug_lockdep_rcu_enabled() before checking (c) to 369 * prevent early boot splats due to lockdep not yet being initialized, 370 * and rechecks it after checking (c) to prevent false-positive splats 371 * due to races with lockdep being disabled. See commit 3066820034b5dd 372 * ("rcu: Reject RCU_LOCKDEP_WARN() false positives") for more detail. 373 */ 374 #define RCU_LOCKDEP_WARN(c, s) \ 375 do { \ 376 static bool __section(".data..unlikely") __warned; \ 377 if (debug_lockdep_rcu_enabled() && (c) && \ 378 debug_lockdep_rcu_enabled() && !__warned) { \ 379 __warned = true; \ 380 lockdep_rcu_suspicious(__FILE__, __LINE__, s); \ 381 } \ 382 } while (0) 383 384 #ifndef CONFIG_PREEMPT_RCU 385 static inline void rcu_preempt_sleep_check(void) 386 { 387 RCU_LOCKDEP_WARN(lock_is_held(&rcu_lock_map), 388 "Illegal context switch in RCU read-side critical section"); 389 } 390 #else // #ifndef CONFIG_PREEMPT_RCU 391 static inline void rcu_preempt_sleep_check(void) { } 392 #endif // #else // #ifndef CONFIG_PREEMPT_RCU 393 394 #define rcu_sleep_check() \ 395 do { \ 396 rcu_preempt_sleep_check(); \ 397 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \ 398 RCU_LOCKDEP_WARN(lock_is_held(&rcu_bh_lock_map), \ 399 "Illegal context switch in RCU-bh read-side critical section"); \ 400 RCU_LOCKDEP_WARN(lock_is_held(&rcu_sched_lock_map), \ 401 "Illegal context switch in RCU-sched read-side critical section"); \ 402 } while (0) 403 404 // See RCU_LOCKDEP_WARN() for an explanation of the double call to 405 // debug_lockdep_rcu_enabled(). 406 static __always_inline bool lockdep_assert_rcu_helper(bool c, const struct __ctx_lock_RCU *ctx) 407 __assumes_shared_ctx_lock(RCU) __assumes_shared_ctx_lock(ctx) 408 { 409 return debug_lockdep_rcu_enabled() && 410 (c || !rcu_is_watching() || !rcu_lockdep_current_cpu_online()) && 411 debug_lockdep_rcu_enabled(); 412 } 413 414 /** 415 * lockdep_assert_in_rcu_read_lock - WARN if not protected by rcu_read_lock() 416 * 417 * Splats if lockdep is enabled and there is no rcu_read_lock() in effect. 418 */ 419 #define lockdep_assert_in_rcu_read_lock() \ 420 WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_lock_map), RCU)) 421 422 /** 423 * lockdep_assert_in_rcu_read_lock_bh - WARN if not protected by rcu_read_lock_bh() 424 * 425 * Splats if lockdep is enabled and there is no rcu_read_lock_bh() in effect. 426 * Note that local_bh_disable() and friends do not suffice here, instead an 427 * actual rcu_read_lock_bh() is required. 428 */ 429 #define lockdep_assert_in_rcu_read_lock_bh() \ 430 WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_bh_lock_map), RCU_BH)) 431 432 /** 433 * lockdep_assert_in_rcu_read_lock_sched - WARN if not protected by rcu_read_lock_sched() 434 * 435 * Splats if lockdep is enabled and there is no rcu_read_lock_sched() 436 * in effect. Note that preempt_disable() and friends do not suffice here, 437 * instead an actual rcu_read_lock_sched() is required. 438 */ 439 #define lockdep_assert_in_rcu_read_lock_sched() \ 440 WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_sched_lock_map), RCU_SCHED)) 441 442 /** 443 * lockdep_assert_in_rcu_reader - WARN if not within some type of RCU reader 444 * 445 * Splats if lockdep is enabled and there is no RCU reader of any 446 * type in effect. Note that regions of code protected by things like 447 * preempt_disable, local_bh_disable(), and local_irq_disable() all qualify 448 * as RCU readers. 449 * 450 * Note that this will never trigger in PREEMPT_NONE or PREEMPT_VOLUNTARY 451 * kernels that are not also built with PREEMPT_COUNT. But if you have 452 * lockdep enabled, you might as well also enable PREEMPT_COUNT. 453 */ 454 #define lockdep_assert_in_rcu_reader() \ 455 WARN_ON_ONCE(lockdep_assert_rcu_helper(!lock_is_held(&rcu_lock_map) && \ 456 !lock_is_held(&rcu_bh_lock_map) && \ 457 !lock_is_held(&rcu_sched_lock_map) && \ 458 preemptible(), RCU)) 459 460 #else /* #ifdef CONFIG_PROVE_RCU */ 461 462 #define RCU_LOCKDEP_WARN(c, s) do { } while (0 && (c)) 463 #define rcu_sleep_check() do { } while (0) 464 465 #define lockdep_assert_in_rcu_read_lock() __assume_shared_ctx_lock(RCU) 466 #define lockdep_assert_in_rcu_read_lock_bh() __assume_shared_ctx_lock(RCU_BH) 467 #define lockdep_assert_in_rcu_read_lock_sched() __assume_shared_ctx_lock(RCU_SCHED) 468 #define lockdep_assert_in_rcu_reader() __assume_shared_ctx_lock(RCU) 469 470 #endif /* #else #ifdef CONFIG_PROVE_RCU */ 471 472 /* 473 * Helper functions for rcu_dereference_check(), rcu_dereference_protected() 474 * and rcu_assign_pointer(). Some of these could be folded into their 475 * callers, but they are left separate in order to ease introduction of 476 * multiple pointers markings to match different RCU implementations 477 * (e.g., __srcu), should this make sense in the future. 478 */ 479 480 #ifdef __CHECKER__ 481 #define rcu_check_sparse(p, space) \ 482 ((void)(((typeof(*p) space *)p) == p)) 483 #else /* #ifdef __CHECKER__ */ 484 #define rcu_check_sparse(p, space) 485 #endif /* #else #ifdef __CHECKER__ */ 486 487 #define __unrcu_pointer(p, local) \ 488 context_unsafe( \ 489 typeof(*p) *local = (typeof(*p) *__force)(p); \ 490 rcu_check_sparse(p, __rcu); \ 491 ((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)) \ 492 ) 493 /** 494 * unrcu_pointer - mark a pointer as not being RCU protected 495 * @p: pointer needing to lose its __rcu property 496 * 497 * Converts @p from an __rcu pointer to a __kernel pointer. 498 * This allows an __rcu pointer to be used with xchg() and friends. 499 */ 500 #define unrcu_pointer(p) __unrcu_pointer(p, __UNIQUE_ID(rcu)) 501 502 #define __rcu_access_pointer(p, local, space) context_unsafe( \ 503 ({ \ 504 typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \ 505 rcu_check_sparse(p, space); \ 506 ((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \ 507 }) ) 508 #define __rcu_dereference_check(p, local, c, space) \ 509 ({ \ 510 /* Dependency order vs. p above. */ \ 511 typeof(*p) *local = (typeof(*p) *__force)READ_ONCE(p); \ 512 RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_check() usage"); \ 513 rcu_check_sparse(p, space); \ 514 ((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \ 515 }) 516 #define __rcu_dereference_protected(p, local, c, space) \ 517 ({ \ 518 RCU_LOCKDEP_WARN(!(c), "suspicious rcu_dereference_protected() usage"); \ 519 rcu_check_sparse(p, space); \ 520 ((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(p)); \ 521 }) 522 #define __rcu_dereference_raw(p, local) \ 523 ({ \ 524 /* Dependency order vs. p above. */ \ 525 typeof(p) local = READ_ONCE(p); \ 526 ((TYPEOF_NO_ADDRESS_SPACE(*p) __force __kernel *)(local)); \ 527 }) 528 #define rcu_dereference_raw(p) __rcu_dereference_raw(p, __UNIQUE_ID(rcu)) 529 530 /** 531 * RCU_INITIALIZER() - statically initialize an RCU-protected global variable 532 * @v: The value to statically initialize with. 533 */ 534 #define RCU_INITIALIZER(v) (typeof(*(v)) __force __rcu *)(v) 535 536 /** 537 * rcu_assign_pointer() - assign to RCU-protected pointer 538 * @p: pointer to assign to 539 * @v: value to assign (publish) 540 * 541 * Assigns the specified value to the specified RCU-protected 542 * pointer, ensuring that any concurrent RCU readers will see 543 * any prior initialization. 544 * 545 * Inserts memory barriers on architectures that require them 546 * (which is most of them), and also prevents the compiler from 547 * reordering the code that initializes the structure after the pointer 548 * assignment. More importantly, this call documents which pointers 549 * will be dereferenced by RCU read-side code. 550 * 551 * In some special cases, you may use RCU_INIT_POINTER() instead 552 * of rcu_assign_pointer(). RCU_INIT_POINTER() is a bit faster due 553 * to the fact that it does not constrain either the CPU or the compiler. 554 * That said, using RCU_INIT_POINTER() when you should have used 555 * rcu_assign_pointer() is a very bad thing that results in 556 * impossible-to-diagnose memory corruption. So please be careful. 557 * See the RCU_INIT_POINTER() comment header for details. 558 * 559 * Note that rcu_assign_pointer() evaluates each of its arguments only 560 * once, appearances notwithstanding. One of the "extra" evaluations 561 * is in typeof() and the other visible only to sparse (__CHECKER__), 562 * neither of which actually execute the argument. As with most cpp 563 * macros, this execute-arguments-only-once property is important, so 564 * please be careful when making changes to rcu_assign_pointer() and the 565 * other macros that it invokes. 566 */ 567 #define rcu_assign_pointer(p, v) \ 568 context_unsafe( \ 569 uintptr_t _r_a_p__v = (uintptr_t)(v); \ 570 rcu_check_sparse(p, __rcu); \ 571 \ 572 if (__builtin_constant_p(v) && (_r_a_p__v) == (uintptr_t)NULL) \ 573 WRITE_ONCE((p), (typeof(p))(_r_a_p__v)); \ 574 else \ 575 smp_store_release(&p, RCU_INITIALIZER((typeof(p))_r_a_p__v)); \ 576 ) 577 578 /** 579 * rcu_replace_pointer() - replace an RCU pointer, returning its old value 580 * @rcu_ptr: RCU pointer, whose old value is returned 581 * @ptr: regular pointer 582 * @c: the lockdep conditions under which the dereference will take place 583 * 584 * Perform a replacement, where @rcu_ptr is an RCU-annotated 585 * pointer and @c is the lockdep argument that is passed to the 586 * rcu_dereference_protected() call used to read that pointer. The old 587 * value of @rcu_ptr is returned, and @rcu_ptr is set to @ptr. 588 */ 589 #define rcu_replace_pointer(rcu_ptr, ptr, c) \ 590 ({ \ 591 typeof(ptr) __tmp = rcu_dereference_protected((rcu_ptr), (c)); \ 592 rcu_assign_pointer((rcu_ptr), (ptr)); \ 593 __tmp; \ 594 }) 595 596 /** 597 * rcu_access_pointer() - fetch RCU pointer with no dereferencing 598 * @p: The pointer to read 599 * 600 * Return the value of the specified RCU-protected pointer, but omit the 601 * lockdep checks for being in an RCU read-side critical section. This is 602 * useful when the value of this pointer is accessed, but the pointer is 603 * not dereferenced, for example, when testing an RCU-protected pointer 604 * against NULL. Within an RCU read-side critical section, there is little 605 * reason to use rcu_access_pointer(). Although rcu_access_pointer() may 606 * also be used in cases where update-side locks prevent the value of the 607 * pointer from changing, you should instead use rcu_dereference_protected() 608 * for this use case. It is also permissible to use rcu_access_pointer() 609 * within lockless updaters to obtain the old value for an atomic operation, 610 * for example, for cmpxchg(). 611 * 612 * It is usually best to test the rcu_access_pointer() return value 613 * directly in order to avoid accidental dereferences being introduced 614 * by later inattentive changes. In other words, assigning the 615 * rcu_access_pointer() return value to a local variable results in an 616 * accident waiting to happen. 617 * 618 * It is also permissible to use rcu_access_pointer() when read-side 619 * access to the pointer was removed at least one grace period ago, as is 620 * the case in the context of the RCU callback that is freeing up the data, 621 * or after a synchronize_rcu() returns. This can be useful when tearing 622 * down multi-linked structures after a grace period has elapsed. However, 623 * rcu_dereference_protected() is normally preferred for this use case. 624 */ 625 #define rcu_access_pointer(p) __rcu_access_pointer((p), __UNIQUE_ID(rcu), __rcu) 626 627 /** 628 * rcu_dereference_check() - rcu_dereference with debug checking 629 * @p: The pointer to read, prior to dereferencing 630 * @c: The conditions under which the dereference will take place 631 * 632 * Do an rcu_dereference(), but check that the conditions under which the 633 * dereference will take place are correct. Typically the conditions 634 * indicate the various locking conditions that should be held at that 635 * point. The check should return true if the conditions are satisfied. 636 * An implicit check for being in an RCU read-side critical section 637 * (rcu_read_lock()) is included. 638 * 639 * For example: 640 * 641 * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock)); 642 * 643 * could be used to indicate to lockdep that foo->bar may only be dereferenced 644 * if either rcu_read_lock() is held, or that the lock required to replace 645 * the bar struct at foo->bar is held. 646 * 647 * Note that the list of conditions may also include indications of when a lock 648 * need not be held, for example during initialisation or destruction of the 649 * target struct: 650 * 651 * bar = rcu_dereference_check(foo->bar, lockdep_is_held(&foo->lock) || 652 * atomic_read(&foo->usage) == 0); 653 * 654 * Inserts memory barriers on architectures that require them 655 * (currently only the Alpha), prevents the compiler from refetching 656 * (and from merging fetches), and, more importantly, documents exactly 657 * which pointers are protected by RCU and checks that the pointer is 658 * annotated as __rcu. 659 */ 660 #define rcu_dereference_check(p, c) \ 661 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \ 662 (c) || rcu_read_lock_held(), __rcu) 663 664 /** 665 * rcu_dereference_bh_check() - rcu_dereference_bh with debug checking 666 * @p: The pointer to read, prior to dereferencing 667 * @c: The conditions under which the dereference will take place 668 * 669 * This is the RCU-bh counterpart to rcu_dereference_check(). However, 670 * please note that starting in v5.0 kernels, vanilla RCU grace periods 671 * wait for local_bh_disable() regions of code in addition to regions of 672 * code demarked by rcu_read_lock() and rcu_read_unlock(). This means 673 * that synchronize_rcu(), call_rcu, and friends all take not only 674 * rcu_read_lock() but also rcu_read_lock_bh() into account. 675 */ 676 #define rcu_dereference_bh_check(p, c) \ 677 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \ 678 (c) || rcu_read_lock_bh_held(), __rcu) 679 680 /** 681 * rcu_dereference_sched_check() - rcu_dereference_sched with debug checking 682 * @p: The pointer to read, prior to dereferencing 683 * @c: The conditions under which the dereference will take place 684 * 685 * This is the RCU-sched counterpart to rcu_dereference_check(). 686 * However, please note that starting in v5.0 kernels, vanilla RCU grace 687 * periods wait for preempt_disable() regions of code in addition to 688 * regions of code demarked by rcu_read_lock() and rcu_read_unlock(). 689 * This means that synchronize_rcu(), call_rcu, and friends all take not 690 * only rcu_read_lock() but also rcu_read_lock_sched() into account. 691 */ 692 #define rcu_dereference_sched_check(p, c) \ 693 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \ 694 (c) || rcu_read_lock_sched_held(), \ 695 __rcu) 696 697 /** 698 * rcu_dereference_all_check() - rcu_dereference_all with debug checking 699 * @p: The pointer to read, prior to dereferencing 700 * @c: The conditions under which the dereference will take place 701 * 702 * This is similar to rcu_dereference_check(), but allows protection 703 * by all forms of vanilla RCU readers, including preemption disabled, 704 * bh-disabled, and interrupt-disabled regions of code. Note that "vanilla 705 * RCU" excludes SRCU and the various Tasks RCU flavors. Please note 706 * that this macro should not be backported to any Linux-kernel version 707 * preceding v5.0 due to changes in synchronize_rcu() semantics prior 708 * to that version. 709 */ 710 #define rcu_dereference_all_check(p, c) \ 711 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \ 712 (c) || rcu_read_lock_any_held(), \ 713 __rcu) 714 715 /* 716 * The tracing infrastructure traces RCU (we want that), but unfortunately 717 * some of the RCU checks causes tracing to lock up the system. 718 * 719 * The no-tracing version of rcu_dereference_raw() must not call 720 * rcu_read_lock_held(). 721 */ 722 #define rcu_dereference_raw_check(p) \ 723 __rcu_dereference_check((p), __UNIQUE_ID(rcu), 1, __rcu) 724 725 /** 726 * rcu_dereference_protected() - fetch RCU pointer when updates prevented 727 * @p: The pointer to read, prior to dereferencing 728 * @c: The conditions under which the dereference will take place 729 * 730 * Return the value of the specified RCU-protected pointer, but omit 731 * the READ_ONCE(). This is useful in cases where update-side locks 732 * prevent the value of the pointer from changing. Please note that this 733 * primitive does *not* prevent the compiler from repeating this reference 734 * or combining it with other references, so it should not be used without 735 * protection of appropriate locks. 736 * 737 * This function is only for update-side use. Using this function 738 * when protected only by rcu_read_lock() will result in infrequent 739 * but very ugly failures. 740 */ 741 #define rcu_dereference_protected(p, c) \ 742 __rcu_dereference_protected((p), __UNIQUE_ID(rcu), (c), __rcu) 743 744 745 /** 746 * rcu_dereference() - fetch RCU-protected pointer for dereferencing 747 * @p: The pointer to read, prior to dereferencing 748 * 749 * This is a simple wrapper around rcu_dereference_check(). 750 */ 751 #define rcu_dereference(p) rcu_dereference_check(p, 0) 752 753 /** 754 * rcu_dereference_bh() - fetch an RCU-bh-protected pointer for dereferencing 755 * @p: The pointer to read, prior to dereferencing 756 * 757 * Makes rcu_dereference_check() do the dirty work. 758 */ 759 #define rcu_dereference_bh(p) rcu_dereference_bh_check(p, 0) 760 761 /** 762 * rcu_dereference_sched() - fetch RCU-sched-protected pointer for dereferencing 763 * @p: The pointer to read, prior to dereferencing 764 * 765 * Makes rcu_dereference_check() do the dirty work. 766 */ 767 #define rcu_dereference_sched(p) rcu_dereference_sched_check(p, 0) 768 769 /** 770 * rcu_dereference_all() - fetch RCU-all-protected pointer for dereferencing 771 * @p: The pointer to read, prior to dereferencing 772 * 773 * Makes rcu_dereference_check() do the dirty work. 774 */ 775 #define rcu_dereference_all(p) rcu_dereference_all_check(p, 0) 776 777 /** 778 * rcu_pointer_handoff() - Hand off a pointer from RCU to other mechanism 779 * @p: The pointer to hand off 780 * 781 * This is simply an identity function, but it documents where a pointer 782 * is handed off from RCU to some other synchronization mechanism, for 783 * example, reference counting or locking. In C11, it would map to 784 * kill_dependency(). It could be used as follows:: 785 * 786 * rcu_read_lock(); 787 * p = rcu_dereference(gp); 788 * long_lived = is_long_lived(p); 789 * if (long_lived) { 790 * if (!atomic_inc_not_zero(p->refcnt)) 791 * long_lived = false; 792 * else 793 * p = rcu_pointer_handoff(p); 794 * } 795 * rcu_read_unlock(); 796 */ 797 #define rcu_pointer_handoff(p) (p) 798 799 /** 800 * rcu_read_lock() - mark the beginning of an RCU read-side critical section 801 * 802 * When synchronize_rcu() is invoked on one CPU while other CPUs 803 * are within RCU read-side critical sections, then the 804 * synchronize_rcu() is guaranteed to block until after all the other 805 * CPUs exit their critical sections. Similarly, if call_rcu() is invoked 806 * on one CPU while other CPUs are within RCU read-side critical 807 * sections, invocation of the corresponding RCU callback is deferred 808 * until after the all the other CPUs exit their critical sections. 809 * 810 * Both synchronize_rcu() and call_rcu() also wait for regions of code 811 * with preemption disabled, including regions of code with interrupts or 812 * softirqs disabled. 813 * 814 * Note, however, that RCU callbacks are permitted to run concurrently 815 * with new RCU read-side critical sections. One way that this can happen 816 * is via the following sequence of events: (1) CPU 0 enters an RCU 817 * read-side critical section, (2) CPU 1 invokes call_rcu() to register 818 * an RCU callback, (3) CPU 0 exits the RCU read-side critical section, 819 * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU 820 * callback is invoked. This is legal, because the RCU read-side critical 821 * section that was running concurrently with the call_rcu() (and which 822 * therefore might be referencing something that the corresponding RCU 823 * callback would free up) has completed before the corresponding 824 * RCU callback is invoked. 825 * 826 * RCU read-side critical sections may be nested. Any deferred actions 827 * will be deferred until the outermost RCU read-side critical section 828 * completes. 829 * 830 * You can avoid reading and understanding the next paragraph by 831 * following this rule: don't put anything in an rcu_read_lock() RCU 832 * read-side critical section that would block in a !PREEMPTION kernel. 833 * But if you want the full story, read on! 834 * 835 * In non-preemptible RCU implementations (pure TREE_RCU and TINY_RCU), 836 * it is illegal to block while in an RCU read-side critical section. 837 * In preemptible RCU implementations (PREEMPT_RCU) in CONFIG_PREEMPTION 838 * kernel builds, RCU read-side critical sections may be preempted, 839 * but explicit blocking is illegal. Finally, in preemptible RCU 840 * implementations in real-time (with -rt patchset) kernel builds, RCU 841 * read-side critical sections may be preempted and they may also block, but 842 * only when acquiring spinlocks that are subject to priority inheritance. 843 */ 844 static __always_inline void rcu_read_lock(void) 845 __acquires_shared(RCU) 846 { 847 __rcu_read_lock(); 848 __acquire_shared(RCU); 849 rcu_lock_acquire(&rcu_lock_map); 850 RCU_LOCKDEP_WARN(!rcu_is_watching(), 851 "rcu_read_lock() used illegally while idle"); 852 } 853 854 /* 855 * So where is rcu_write_lock()? It does not exist, as there is no 856 * way for writers to lock out RCU readers. This is a feature, not 857 * a bug -- this property is what provides RCU's performance benefits. 858 * Of course, writers must coordinate with each other. The normal 859 * spinlock primitives work well for this, but any other technique may be 860 * used as well. RCU does not care how the writers keep out of each 861 * others' way, as long as they do so. 862 */ 863 864 /** 865 * rcu_read_unlock() - marks the end of an RCU read-side critical section. 866 * 867 * In almost all situations, rcu_read_unlock() is immune from deadlock. 868 * This deadlock immunity also extends to the scheduler's runqueue 869 * and priority-inheritance spinlocks, courtesy of the quiescent-state 870 * deferral that is carried out when rcu_read_unlock() is invoked with 871 * interrupts disabled. 872 * 873 * See rcu_read_lock() for more information. 874 */ 875 static inline void rcu_read_unlock(void) 876 __releases_shared(RCU) 877 { 878 RCU_LOCKDEP_WARN(!rcu_is_watching(), 879 "rcu_read_unlock() used illegally while idle"); 880 rcu_lock_release(&rcu_lock_map); /* Keep acq info for rls diags. */ 881 __release_shared(RCU); 882 __rcu_read_unlock(); 883 } 884 885 /** 886 * rcu_read_lock_bh() - mark the beginning of an RCU-bh critical section 887 * 888 * This is equivalent to rcu_read_lock(), but also disables softirqs. 889 * Note that anything else that disables softirqs can also serve as an RCU 890 * read-side critical section. However, please note that this equivalence 891 * applies only to v5.0 and later. Before v5.0, rcu_read_lock() and 892 * rcu_read_lock_bh() were unrelated. 893 * 894 * Note that rcu_read_lock_bh() and the matching rcu_read_unlock_bh() 895 * must occur in the same context, for example, it is illegal to invoke 896 * rcu_read_unlock_bh() from one task if the matching rcu_read_lock_bh() 897 * was invoked from some other task. 898 */ 899 static inline void rcu_read_lock_bh(void) 900 __acquires_shared(RCU) __acquires_shared(RCU_BH) 901 { 902 local_bh_disable(); 903 __acquire_shared(RCU); 904 __acquire_shared(RCU_BH); 905 rcu_lock_acquire(&rcu_bh_lock_map); 906 RCU_LOCKDEP_WARN(!rcu_is_watching(), 907 "rcu_read_lock_bh() used illegally while idle"); 908 } 909 910 /** 911 * rcu_read_unlock_bh() - marks the end of a softirq-only RCU critical section 912 * 913 * See rcu_read_lock_bh() for more information. 914 */ 915 static inline void rcu_read_unlock_bh(void) 916 __releases_shared(RCU) __releases_shared(RCU_BH) 917 { 918 RCU_LOCKDEP_WARN(!rcu_is_watching(), 919 "rcu_read_unlock_bh() used illegally while idle"); 920 rcu_lock_release(&rcu_bh_lock_map); 921 __release_shared(RCU_BH); 922 __release_shared(RCU); 923 local_bh_enable(); 924 } 925 926 /** 927 * rcu_read_lock_sched() - mark the beginning of a RCU-sched critical section 928 * 929 * This is equivalent to rcu_read_lock(), but also disables preemption. 930 * Read-side critical sections can also be introduced by anything else that 931 * disables preemption, including local_irq_disable() and friends. However, 932 * please note that the equivalence to rcu_read_lock() applies only to 933 * v5.0 and later. Before v5.0, rcu_read_lock() and rcu_read_lock_sched() 934 * were unrelated. 935 * 936 * Note that rcu_read_lock_sched() and the matching rcu_read_unlock_sched() 937 * must occur in the same context, for example, it is illegal to invoke 938 * rcu_read_unlock_sched() from process context if the matching 939 * rcu_read_lock_sched() was invoked from an NMI handler. 940 */ 941 static inline void rcu_read_lock_sched(void) 942 __acquires_shared(RCU) __acquires_shared(RCU_SCHED) 943 { 944 preempt_disable(); 945 __acquire_shared(RCU); 946 __acquire_shared(RCU_SCHED); 947 rcu_lock_acquire(&rcu_sched_lock_map); 948 RCU_LOCKDEP_WARN(!rcu_is_watching(), 949 "rcu_read_lock_sched() used illegally while idle"); 950 } 951 952 /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ 953 static inline notrace void rcu_read_lock_sched_notrace(void) 954 __acquires_shared(RCU) __acquires_shared(RCU_SCHED) 955 { 956 preempt_disable_notrace(); 957 __acquire_shared(RCU); 958 __acquire_shared(RCU_SCHED); 959 } 960 961 /** 962 * rcu_read_unlock_sched() - marks the end of a RCU-classic critical section 963 * 964 * See rcu_read_lock_sched() for more information. 965 */ 966 static inline void rcu_read_unlock_sched(void) 967 __releases_shared(RCU) __releases_shared(RCU_SCHED) 968 { 969 RCU_LOCKDEP_WARN(!rcu_is_watching(), 970 "rcu_read_unlock_sched() used illegally while idle"); 971 rcu_lock_release(&rcu_sched_lock_map); 972 __release_shared(RCU_SCHED); 973 __release_shared(RCU); 974 preempt_enable(); 975 } 976 977 /* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ 978 static inline notrace void rcu_read_unlock_sched_notrace(void) 979 __releases_shared(RCU) __releases_shared(RCU_SCHED) 980 { 981 __release_shared(RCU_SCHED); 982 __release_shared(RCU); 983 preempt_enable_notrace(); 984 } 985 986 static __always_inline void rcu_read_lock_dont_migrate(void) 987 __acquires_shared(RCU) 988 { 989 if (IS_ENABLED(CONFIG_PREEMPT_RCU)) 990 migrate_disable(); 991 rcu_read_lock(); 992 } 993 994 static inline void rcu_read_unlock_migrate(void) 995 __releases_shared(RCU) 996 { 997 rcu_read_unlock(); 998 if (IS_ENABLED(CONFIG_PREEMPT_RCU)) 999 migrate_enable(); 1000 } 1001 1002 /** 1003 * RCU_INIT_POINTER() - initialize an RCU protected pointer 1004 * @p: The pointer to be initialized. 1005 * @v: The value to initialized the pointer to. 1006 * 1007 * Initialize an RCU-protected pointer in special cases where readers 1008 * do not need ordering constraints on the CPU or the compiler. These 1009 * special cases are: 1010 * 1011 * 1. This use of RCU_INIT_POINTER() is NULLing out the pointer *or* 1012 * 2. The caller has taken whatever steps are required to prevent 1013 * RCU readers from concurrently accessing this pointer *or* 1014 * 3. The referenced data structure has already been exposed to 1015 * readers either at compile time or via rcu_assign_pointer() *and* 1016 * 1017 * a. You have not made *any* reader-visible changes to 1018 * this structure since then *or* 1019 * b. It is OK for readers accessing this structure from its 1020 * new location to see the old state of the structure. (For 1021 * example, the changes were to statistical counters or to 1022 * other state where exact synchronization is not required.) 1023 * 1024 * Failure to follow these rules governing use of RCU_INIT_POINTER() will 1025 * result in impossible-to-diagnose memory corruption. As in the structures 1026 * will look OK in crash dumps, but any concurrent RCU readers might 1027 * see pre-initialized values of the referenced data structure. So 1028 * please be very careful how you use RCU_INIT_POINTER()!!! 1029 * 1030 * If you are creating an RCU-protected linked structure that is accessed 1031 * by a single external-to-structure RCU-protected pointer, then you may 1032 * use RCU_INIT_POINTER() to initialize the internal RCU-protected 1033 * pointers, but you must use rcu_assign_pointer() to initialize the 1034 * external-to-structure pointer *after* you have completely initialized 1035 * the reader-accessible portions of the linked structure. 1036 * 1037 * Note that unlike rcu_assign_pointer(), RCU_INIT_POINTER() provides no 1038 * ordering guarantees for either the CPU or the compiler. 1039 */ 1040 #define RCU_INIT_POINTER(p, v) \ 1041 context_unsafe( \ 1042 rcu_check_sparse(p, __rcu); \ 1043 WRITE_ONCE(p, RCU_INITIALIZER(v)); \ 1044 ) 1045 1046 /** 1047 * RCU_POINTER_INITIALIZER() - statically initialize an RCU protected pointer 1048 * @p: The pointer to be initialized. 1049 * @v: The value to initialized the pointer to. 1050 * 1051 * GCC-style initialization for an RCU-protected pointer in a structure field. 1052 */ 1053 #define RCU_POINTER_INITIALIZER(p, v) \ 1054 .p = RCU_INITIALIZER(v) 1055 1056 /** 1057 * kfree_rcu() - kfree an object after a grace period. 1058 * @ptr: pointer to kfree for double-argument invocations. 1059 * @rhf: the name of the struct rcu_head within the type of @ptr. 1060 * 1061 * Many rcu callbacks functions just call kfree() on the base structure. 1062 * These functions are trivial, but their size adds up, and furthermore 1063 * when they are used in a kernel module, that module must invoke the 1064 * high-latency rcu_barrier() function at module-unload time. 1065 * 1066 * The kfree_rcu() function handles this issue. In order to have a universal 1067 * callback function handling different offsets of rcu_head, the callback needs 1068 * to determine the starting address of the freed object, which can be a large 1069 * kmalloc or vmalloc allocation. To allow simply aligning the pointer down to 1070 * page boundary for those, only offsets up to 4095 bytes can be accommodated. 1071 * If the offset is larger than 4095 bytes, a compile-time error will 1072 * be generated in kvfree_rcu_arg_2(). If this error is triggered, you can 1073 * either fall back to use of call_rcu() or rearrange the structure to 1074 * position the rcu_head structure into the first 4096 bytes. 1075 * 1076 * The object to be freed can be allocated either by kmalloc(), 1077 * kmalloc_nolock(), or kmem_cache_alloc(). 1078 * 1079 * Note that the allowable offset might decrease in the future. 1080 * 1081 * The BUILD_BUG_ON check must not involve any function calls, hence the 1082 * checks are done in macros here. 1083 */ 1084 #define kfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf) 1085 #define kvfree_rcu(ptr, rhf) kvfree_rcu_arg_2(ptr, rhf) 1086 1087 /** 1088 * kfree_rcu_mightsleep() - kfree an object after a grace period. 1089 * @ptr: pointer to kfree for single-argument invocations. 1090 * 1091 * When it comes to head-less variant, only one argument 1092 * is passed and that is just a pointer which has to be 1093 * freed after a grace period. Therefore the semantic is 1094 * 1095 * kfree_rcu_mightsleep(ptr); 1096 * 1097 * where @ptr is the pointer to be freed by kvfree(). 1098 * 1099 * Please note, head-less way of freeing is permitted to 1100 * use from a context that has to follow might_sleep() 1101 * annotation. Otherwise, please switch and embed the 1102 * rcu_head structure within the type of @ptr. 1103 */ 1104 #define kfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr) 1105 #define kvfree_rcu_mightsleep(ptr) kvfree_rcu_arg_1(ptr) 1106 1107 /* 1108 * In mm/slab_common.c, no suitable header to include here. 1109 */ 1110 void kvfree_call_rcu(struct kvfree_rcu_head *head, void *ptr); 1111 void kfree_call_rcu_nolock(struct kvfree_rcu_head *head, void *ptr); 1112 1113 /* 1114 * The BUILD_BUG_ON() makes sure the rcu_head offset can be handled. See the 1115 * comment of kfree_rcu() for details. 1116 */ 1117 #define kvfree_rcu_arg_2(ptr, kvrhf) \ 1118 do { \ 1119 typeof (ptr) ___p = (ptr); \ 1120 struct kvfree_rcu_head *___head; \ 1121 \ 1122 if (___p) { \ 1123 BUILD_BUG_ON(offsetof(typeof(*(ptr)), kvrhf) >= 4096); \ 1124 ___head = (struct kvfree_rcu_head *) &(___p)->kvrhf; \ 1125 kvfree_call_rcu(___head, (void *) (___p)); \ 1126 } \ 1127 } while (0) 1128 1129 #define kvfree_rcu_arg_1(ptr) \ 1130 do { \ 1131 typeof(ptr) ___p = (ptr); \ 1132 \ 1133 if (___p) \ 1134 kvfree_call_rcu(NULL, (void *) (___p)); \ 1135 } while (0) 1136 1137 /** 1138 * kfree_rcu_nolock() - a version of kfree_rcu() that can be called in any context. 1139 * @ptr: pointer to kfree for double-argument invocations. 1140 * @kvrhf: the name of the struct kvfree_rcu_head within the type of @ptr. 1141 * 1142 * With KVFREE_RCU_BATCHED, kfree_rcu_nolock() tries hard to free objects 1143 * without any deferred processing, but may still defer freeing. 1144 * Large kmalloc and vmalloc objects are always deferred. 1145 * 1146 * kfree_rcu_nolock() supports 2-arg variant only. 1147 */ 1148 #define kfree_rcu_nolock(ptr, kvrhf) \ 1149 do { \ 1150 typeof (ptr) ___p = (ptr); \ 1151 \ 1152 if (___p) { \ 1153 BUILD_BUG_ON(offsetof(typeof(*(ptr)), kvrhf) >= 4096); \ 1154 kfree_call_rcu_nolock(&((___p)->kvrhf), (void *) (___p)); \ 1155 } \ 1156 } while (0) 1157 1158 /* 1159 * Place this after a lock-acquisition primitive to guarantee that 1160 * an UNLOCK+LOCK pair acts as a full barrier. This guarantee applies 1161 * if the UNLOCK and LOCK are executed by the same CPU or if the 1162 * UNLOCK and LOCK operate on the same lock variable. 1163 */ 1164 #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE 1165 #define smp_mb__after_unlock_lock() smp_mb() /* Full ordering for lock. */ 1166 #else /* #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */ 1167 #define smp_mb__after_unlock_lock() do { } while (0) 1168 #endif /* #else #ifdef CONFIG_ARCH_WEAK_RELEASE_ACQUIRE */ 1169 1170 1171 /* Has the specified rcu_head structure been handed to call_rcu()? */ 1172 1173 /** 1174 * rcu_head_init - Initialize rcu_head for rcu_head_after_call_rcu() 1175 * @rhp: The rcu_head structure to initialize. 1176 * 1177 * If you intend to invoke rcu_head_after_call_rcu() to test whether a 1178 * given rcu_head structure has already been passed to call_rcu(), then 1179 * you must also invoke this rcu_head_init() function on it just after 1180 * allocating that structure. Calls to this function must not race with 1181 * calls to call_rcu(), rcu_head_after_call_rcu(), or callback invocation. 1182 */ 1183 static inline void rcu_head_init(struct rcu_head *rhp) 1184 { 1185 rhp->func = (rcu_callback_t)~0L; 1186 } 1187 1188 /** 1189 * rcu_head_after_call_rcu() - Has this rcu_head been passed to call_rcu()? 1190 * @rhp: The rcu_head structure to test. 1191 * @f: The function passed to call_rcu() along with @rhp. 1192 * 1193 * Returns @true if the @rhp has been passed to call_rcu() with @func, 1194 * and @false otherwise. Emits a warning in any other case, including 1195 * the case where @rhp has already been invoked after a grace period. 1196 * Calls to this function must not race with callback invocation. One way 1197 * to avoid such races is to enclose the call to rcu_head_after_call_rcu() 1198 * in an RCU read-side critical section that includes a read-side fetch 1199 * of the pointer to the structure containing @rhp. 1200 */ 1201 static inline bool 1202 rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f) 1203 { 1204 rcu_callback_t func = READ_ONCE(rhp->func); 1205 1206 if (func == f) 1207 return true; 1208 WARN_ON_ONCE(func != (rcu_callback_t)~0L); 1209 return false; 1210 } 1211 1212 /* kernel/ksysfs.c definitions */ 1213 extern int rcu_expedited; 1214 extern int rcu_normal; 1215 1216 DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock()) 1217 DECLARE_LOCK_GUARD_0_ATTRS(rcu, __acquires_shared(RCU), __releases_shared(RCU)) 1218 1219 #endif /* __LINUX_RCUPDATE_H */ 1220