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, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 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/cache.h> 37#include <linux/spinlock.h> 38#include <linux/threads.h> 39#include <linux/cpumask.h> 40#include <linux/seqlock.h> 41#include <linux/lockdep.h> 42#include <linux/completion.h> 43#include <linux/debugobjects.h> 44 45#ifdef CONFIG_RCU_TORTURE_TEST 46extern int rcutorture_runnable; /* for sysctl */ 47#endif /* #ifdef CONFIG_RCU_TORTURE_TEST */ 48 49/** 50 * struct rcu_head - callback structure for use with RCU 51 * @next: next update requests in a list 52 * @func: actual update function to call after the grace period. 53 */ 54struct rcu_head { 55 struct rcu_head *next; 56 void (*func)(struct rcu_head *head); 57}; 58 59/* Exported common interfaces */ 60extern void rcu_barrier(void); 61extern void rcu_barrier_bh(void); 62extern void rcu_barrier_sched(void); 63extern void synchronize_sched_expedited(void); 64extern int sched_expedited_torture_stats(char *page); 65 66/* Internal to kernel */ 67extern void rcu_init(void); 68 69#if defined(CONFIG_TREE_RCU) || defined(CONFIG_TREE_PREEMPT_RCU) 70#include <linux/rcutree.h> 71#elif defined(CONFIG_TINY_RCU) 72#include <linux/rcutiny.h> 73#else 74#error "Unknown RCU implementation specified to kernel configuration" 75#endif 76 77#define RCU_HEAD_INIT { .next = NULL, .func = NULL } 78#define RCU_HEAD(head) struct rcu_head head = RCU_HEAD_INIT 79#define INIT_RCU_HEAD(ptr) do { \ 80 (ptr)->next = NULL; (ptr)->func = NULL; \ 81} while (0) 82 83/* 84 * init_rcu_head_on_stack()/destroy_rcu_head_on_stack() are needed for dynamic 85 * initialization and destruction of rcu_head on the stack. rcu_head structures 86 * allocated dynamically in the heap or defined statically don't need any 87 * initialization. 88 */ 89#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 90extern void init_rcu_head_on_stack(struct rcu_head *head); 91extern void destroy_rcu_head_on_stack(struct rcu_head *head); 92#else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 93static inline void init_rcu_head_on_stack(struct rcu_head *head) 94{ 95} 96 97static inline void destroy_rcu_head_on_stack(struct rcu_head *head) 98{ 99} 100#endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 101 102#ifdef CONFIG_DEBUG_LOCK_ALLOC 103 104extern struct lockdep_map rcu_lock_map; 105# define rcu_read_acquire() \ 106 lock_acquire(&rcu_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_) 107# define rcu_read_release() lock_release(&rcu_lock_map, 1, _THIS_IP_) 108 109extern struct lockdep_map rcu_bh_lock_map; 110# define rcu_read_acquire_bh() \ 111 lock_acquire(&rcu_bh_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_) 112# define rcu_read_release_bh() lock_release(&rcu_bh_lock_map, 1, _THIS_IP_) 113 114extern struct lockdep_map rcu_sched_lock_map; 115# define rcu_read_acquire_sched() \ 116 lock_acquire(&rcu_sched_lock_map, 0, 0, 2, 1, NULL, _THIS_IP_) 117# define rcu_read_release_sched() \ 118 lock_release(&rcu_sched_lock_map, 1, _THIS_IP_) 119 120extern int debug_lockdep_rcu_enabled(void); 121 122/** 123 * rcu_read_lock_held - might we be in RCU read-side critical section? 124 * 125 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an RCU 126 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC, 127 * this assumes we are in an RCU read-side critical section unless it can 128 * prove otherwise. 129 * 130 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot 131 * and while lockdep is disabled. 132 */ 133static inline int rcu_read_lock_held(void) 134{ 135 if (!debug_lockdep_rcu_enabled()) 136 return 1; 137 return lock_is_held(&rcu_lock_map); 138} 139 140/* 141 * rcu_read_lock_bh_held() is defined out of line to avoid #include-file 142 * hell. 143 */ 144extern int rcu_read_lock_bh_held(void); 145 146/** 147 * rcu_read_lock_sched_held - might we be in RCU-sched read-side critical section? 148 * 149 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an 150 * RCU-sched read-side critical section. In absence of 151 * CONFIG_DEBUG_LOCK_ALLOC, this assumes we are in an RCU-sched read-side 152 * critical section unless it can prove otherwise. Note that disabling 153 * of preemption (including disabling irqs) counts as an RCU-sched 154 * read-side critical section. 155 * 156 * Check debug_lockdep_rcu_enabled() to prevent false positives during boot 157 * and while lockdep is disabled. 158 */ 159#ifdef CONFIG_PREEMPT 160static inline int rcu_read_lock_sched_held(void) 161{ 162 int lockdep_opinion = 0; 163 164 if (!debug_lockdep_rcu_enabled()) 165 return 1; 166 if (debug_locks) 167 lockdep_opinion = lock_is_held(&rcu_sched_lock_map); 168 return lockdep_opinion || preempt_count() != 0 || irqs_disabled(); 169} 170#else /* #ifdef CONFIG_PREEMPT */ 171static inline int rcu_read_lock_sched_held(void) 172{ 173 return 1; 174} 175#endif /* #else #ifdef CONFIG_PREEMPT */ 176 177#else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 178 179# define rcu_read_acquire() do { } while (0) 180# define rcu_read_release() do { } while (0) 181# define rcu_read_acquire_bh() do { } while (0) 182# define rcu_read_release_bh() do { } while (0) 183# define rcu_read_acquire_sched() do { } while (0) 184# define rcu_read_release_sched() do { } while (0) 185 186static inline int rcu_read_lock_held(void) 187{ 188 return 1; 189} 190 191static inline int rcu_read_lock_bh_held(void) 192{ 193 return 1; 194} 195 196#ifdef CONFIG_PREEMPT 197static inline int rcu_read_lock_sched_held(void) 198{ 199 return preempt_count() != 0 || irqs_disabled(); 200} 201#else /* #ifdef CONFIG_PREEMPT */ 202static inline int rcu_read_lock_sched_held(void) 203{ 204 return 1; 205} 206#endif /* #else #ifdef CONFIG_PREEMPT */ 207 208#endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 209 210#ifdef CONFIG_PROVE_RCU 211 212extern int rcu_my_thread_group_empty(void); 213 214#define __do_rcu_dereference_check(c) \ 215 do { \ 216 static bool __warned; \ 217 if (debug_lockdep_rcu_enabled() && !__warned && !(c)) { \ 218 __warned = true; \ 219 lockdep_rcu_dereference(__FILE__, __LINE__); \ 220 } \ 221 } while (0) 222 223/** 224 * rcu_dereference_check - rcu_dereference with debug checking 225 * @p: The pointer to read, prior to dereferencing 226 * @c: The conditions under which the dereference will take place 227 * 228 * Do an rcu_dereference(), but check that the conditions under which the 229 * dereference will take place are correct. Typically the conditions indicate 230 * the various locking conditions that should be held at that point. The check 231 * should return true if the conditions are satisfied. 232 * 233 * For example: 234 * 235 * bar = rcu_dereference_check(foo->bar, rcu_read_lock_held() || 236 * lockdep_is_held(&foo->lock)); 237 * 238 * could be used to indicate to lockdep that foo->bar may only be dereferenced 239 * if either the RCU read lock is held, or that the lock required to replace 240 * the bar struct at foo->bar is held. 241 * 242 * Note that the list of conditions may also include indications of when a lock 243 * need not be held, for example during initialisation or destruction of the 244 * target struct: 245 * 246 * bar = rcu_dereference_check(foo->bar, rcu_read_lock_held() || 247 * lockdep_is_held(&foo->lock) || 248 * atomic_read(&foo->usage) == 0); 249 */ 250#define rcu_dereference_check(p, c) \ 251 ({ \ 252 __do_rcu_dereference_check(c); \ 253 rcu_dereference_raw(p); \ 254 }) 255 256/** 257 * rcu_dereference_protected - fetch RCU pointer when updates prevented 258 * 259 * Return the value of the specified RCU-protected pointer, but omit 260 * both the smp_read_barrier_depends() and the ACCESS_ONCE(). This 261 * is useful in cases where update-side locks prevent the value of the 262 * pointer from changing. Please note that this primitive does -not- 263 * prevent the compiler from repeating this reference or combining it 264 * with other references, so it should not be used without protection 265 * of appropriate locks. 266 */ 267#define rcu_dereference_protected(p, c) \ 268 ({ \ 269 __do_rcu_dereference_check(c); \ 270 (p); \ 271 }) 272 273#else /* #ifdef CONFIG_PROVE_RCU */ 274 275#define rcu_dereference_check(p, c) rcu_dereference_raw(p) 276#define rcu_dereference_protected(p, c) (p) 277 278#endif /* #else #ifdef CONFIG_PROVE_RCU */ 279 280/** 281 * rcu_access_pointer - fetch RCU pointer with no dereferencing 282 * 283 * Return the value of the specified RCU-protected pointer, but omit the 284 * smp_read_barrier_depends() and keep the ACCESS_ONCE(). This is useful 285 * when the value of this pointer is accessed, but the pointer is not 286 * dereferenced, for example, when testing an RCU-protected pointer against 287 * NULL. This may also be used in cases where update-side locks prevent 288 * the value of the pointer from changing, but rcu_dereference_protected() 289 * is a lighter-weight primitive for this use case. 290 */ 291#define rcu_access_pointer(p) ACCESS_ONCE(p) 292 293/** 294 * rcu_read_lock - mark the beginning of an RCU read-side critical section. 295 * 296 * When synchronize_rcu() is invoked on one CPU while other CPUs 297 * are within RCU read-side critical sections, then the 298 * synchronize_rcu() is guaranteed to block until after all the other 299 * CPUs exit their critical sections. Similarly, if call_rcu() is invoked 300 * on one CPU while other CPUs are within RCU read-side critical 301 * sections, invocation of the corresponding RCU callback is deferred 302 * until after the all the other CPUs exit their critical sections. 303 * 304 * Note, however, that RCU callbacks are permitted to run concurrently 305 * with RCU read-side critical sections. One way that this can happen 306 * is via the following sequence of events: (1) CPU 0 enters an RCU 307 * read-side critical section, (2) CPU 1 invokes call_rcu() to register 308 * an RCU callback, (3) CPU 0 exits the RCU read-side critical section, 309 * (4) CPU 2 enters a RCU read-side critical section, (5) the RCU 310 * callback is invoked. This is legal, because the RCU read-side critical 311 * section that was running concurrently with the call_rcu() (and which 312 * therefore might be referencing something that the corresponding RCU 313 * callback would free up) has completed before the corresponding 314 * RCU callback is invoked. 315 * 316 * RCU read-side critical sections may be nested. Any deferred actions 317 * will be deferred until the outermost RCU read-side critical section 318 * completes. 319 * 320 * It is illegal to block while in an RCU read-side critical section. 321 */ 322static inline void rcu_read_lock(void) 323{ 324 __rcu_read_lock(); 325 __acquire(RCU); 326 rcu_read_acquire(); 327} 328 329/* 330 * So where is rcu_write_lock()? It does not exist, as there is no 331 * way for writers to lock out RCU readers. This is a feature, not 332 * a bug -- this property is what provides RCU's performance benefits. 333 * Of course, writers must coordinate with each other. The normal 334 * spinlock primitives work well for this, but any other technique may be 335 * used as well. RCU does not care how the writers keep out of each 336 * others' way, as long as they do so. 337 */ 338 339/** 340 * rcu_read_unlock - marks the end of an RCU read-side critical section. 341 * 342 * See rcu_read_lock() for more information. 343 */ 344static inline void rcu_read_unlock(void) 345{ 346 rcu_read_release(); 347 __release(RCU); 348 __rcu_read_unlock(); 349} 350 351/** 352 * rcu_read_lock_bh - mark the beginning of a softirq-only RCU critical section 353 * 354 * This is equivalent of rcu_read_lock(), but to be used when updates 355 * are being done using call_rcu_bh(). Since call_rcu_bh() callbacks 356 * consider completion of a softirq handler to be a quiescent state, 357 * a process in RCU read-side critical section must be protected by 358 * disabling softirqs. Read-side critical sections in interrupt context 359 * can use just rcu_read_lock(). 360 * 361 */ 362static inline void rcu_read_lock_bh(void) 363{ 364 __rcu_read_lock_bh(); 365 __acquire(RCU_BH); 366 rcu_read_acquire_bh(); 367} 368 369/* 370 * rcu_read_unlock_bh - marks the end of a softirq-only RCU critical section 371 * 372 * See rcu_read_lock_bh() for more information. 373 */ 374static inline void rcu_read_unlock_bh(void) 375{ 376 rcu_read_release_bh(); 377 __release(RCU_BH); 378 __rcu_read_unlock_bh(); 379} 380 381/** 382 * rcu_read_lock_sched - mark the beginning of a RCU-classic critical section 383 * 384 * Should be used with either 385 * - synchronize_sched() 386 * or 387 * - call_rcu_sched() and rcu_barrier_sched() 388 * on the write-side to insure proper synchronization. 389 */ 390static inline void rcu_read_lock_sched(void) 391{ 392 preempt_disable(); 393 __acquire(RCU_SCHED); 394 rcu_read_acquire_sched(); 395} 396 397/* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ 398static inline notrace void rcu_read_lock_sched_notrace(void) 399{ 400 preempt_disable_notrace(); 401 __acquire(RCU_SCHED); 402} 403 404/* 405 * rcu_read_unlock_sched - marks the end of a RCU-classic critical section 406 * 407 * See rcu_read_lock_sched for more information. 408 */ 409static inline void rcu_read_unlock_sched(void) 410{ 411 rcu_read_release_sched(); 412 __release(RCU_SCHED); 413 preempt_enable(); 414} 415 416/* Used by lockdep and tracing: cannot be traced, cannot call lockdep. */ 417static inline notrace void rcu_read_unlock_sched_notrace(void) 418{ 419 __release(RCU_SCHED); 420 preempt_enable_notrace(); 421} 422 423 424/** 425 * rcu_dereference_raw - fetch an RCU-protected pointer 426 * 427 * The caller must be within some flavor of RCU read-side critical 428 * section, or must be otherwise preventing the pointer from changing, 429 * for example, by holding an appropriate lock. This pointer may later 430 * be safely dereferenced. It is the caller's responsibility to have 431 * done the right thing, as this primitive does no checking of any kind. 432 * 433 * Inserts memory barriers on architectures that require them 434 * (currently only the Alpha), and, more importantly, documents 435 * exactly which pointers are protected by RCU. 436 */ 437#define rcu_dereference_raw(p) ({ \ 438 typeof(p) _________p1 = ACCESS_ONCE(p); \ 439 smp_read_barrier_depends(); \ 440 (_________p1); \ 441 }) 442 443/** 444 * rcu_dereference - fetch an RCU-protected pointer, checking for RCU 445 * 446 * Makes rcu_dereference_check() do the dirty work. 447 */ 448#define rcu_dereference(p) \ 449 rcu_dereference_check(p, rcu_read_lock_held()) 450 451/** 452 * rcu_dereference_bh - fetch an RCU-protected pointer, checking for RCU-bh 453 * 454 * Makes rcu_dereference_check() do the dirty work. 455 */ 456#define rcu_dereference_bh(p) \ 457 rcu_dereference_check(p, rcu_read_lock_bh_held() || irqs_disabled()) 458 459/** 460 * rcu_dereference_sched - fetch RCU-protected pointer, checking for RCU-sched 461 * 462 * Makes rcu_dereference_check() do the dirty work. 463 */ 464#define rcu_dereference_sched(p) \ 465 rcu_dereference_check(p, rcu_read_lock_sched_held()) 466 467/** 468 * rcu_assign_pointer - assign (publicize) a pointer to a newly 469 * initialized structure that will be dereferenced by RCU read-side 470 * critical sections. Returns the value assigned. 471 * 472 * Inserts memory barriers on architectures that require them 473 * (pretty much all of them other than x86), and also prevents 474 * the compiler from reordering the code that initializes the 475 * structure after the pointer assignment. More importantly, this 476 * call documents which pointers will be dereferenced by RCU read-side 477 * code. 478 */ 479 480#define rcu_assign_pointer(p, v) \ 481 ({ \ 482 if (!__builtin_constant_p(v) || \ 483 ((v) != NULL)) \ 484 smp_wmb(); \ 485 (p) = (v); \ 486 }) 487 488/* Infrastructure to implement the synchronize_() primitives. */ 489 490struct rcu_synchronize { 491 struct rcu_head head; 492 struct completion completion; 493}; 494 495extern void wakeme_after_rcu(struct rcu_head *head); 496 497/** 498 * call_rcu - Queue an RCU callback for invocation after a grace period. 499 * @head: structure to be used for queueing the RCU updates. 500 * @func: actual update function to be invoked after the grace period 501 * 502 * The update function will be invoked some time after a full grace 503 * period elapses, in other words after all currently executing RCU 504 * read-side critical sections have completed. RCU read-side critical 505 * sections are delimited by rcu_read_lock() and rcu_read_unlock(), 506 * and may be nested. 507 */ 508extern void call_rcu(struct rcu_head *head, 509 void (*func)(struct rcu_head *head)); 510 511/** 512 * call_rcu_bh - Queue an RCU for invocation after a quicker grace period. 513 * @head: structure to be used for queueing the RCU updates. 514 * @func: actual update function to be invoked after the grace period 515 * 516 * The update function will be invoked some time after a full grace 517 * period elapses, in other words after all currently executing RCU 518 * read-side critical sections have completed. call_rcu_bh() assumes 519 * that the read-side critical sections end on completion of a softirq 520 * handler. This means that read-side critical sections in process 521 * context must not be interrupted by softirqs. This interface is to be 522 * used when most of the read-side critical sections are in softirq context. 523 * RCU read-side critical sections are delimited by : 524 * - rcu_read_lock() and rcu_read_unlock(), if in interrupt context. 525 * OR 526 * - rcu_read_lock_bh() and rcu_read_unlock_bh(), if in process context. 527 * These may be nested. 528 */ 529extern void call_rcu_bh(struct rcu_head *head, 530 void (*func)(struct rcu_head *head)); 531 532/* 533 * debug_rcu_head_queue()/debug_rcu_head_unqueue() are used internally 534 * by call_rcu() and rcu callback execution, and are therefore not part of the 535 * RCU API. Leaving in rcupdate.h because they are used by all RCU flavors. 536 */ 537 538#ifdef CONFIG_DEBUG_OBJECTS_RCU_HEAD 539# define STATE_RCU_HEAD_READY 0 540# define STATE_RCU_HEAD_QUEUED 1 541 542extern struct debug_obj_descr rcuhead_debug_descr; 543 544static inline void debug_rcu_head_queue(struct rcu_head *head) 545{ 546 debug_object_activate(head, &rcuhead_debug_descr); 547 debug_object_active_state(head, &rcuhead_debug_descr, 548 STATE_RCU_HEAD_READY, 549 STATE_RCU_HEAD_QUEUED); 550} 551 552static inline void debug_rcu_head_unqueue(struct rcu_head *head) 553{ 554 debug_object_active_state(head, &rcuhead_debug_descr, 555 STATE_RCU_HEAD_QUEUED, 556 STATE_RCU_HEAD_READY); 557 debug_object_deactivate(head, &rcuhead_debug_descr); 558} 559#else /* !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 560static inline void debug_rcu_head_queue(struct rcu_head *head) 561{ 562} 563 564static inline void debug_rcu_head_unqueue(struct rcu_head *head) 565{ 566} 567#endif /* #else !CONFIG_DEBUG_OBJECTS_RCU_HEAD */ 568 569#ifndef CONFIG_PROVE_RCU 570#define __do_rcu_dereference_check(c) do { } while (0) 571#endif /* #ifdef CONFIG_PROVE_RCU */ 572 573#define __rcu_dereference_index_check(p, c) \ 574 ({ \ 575 typeof(p) _________p1 = ACCESS_ONCE(p); \ 576 __do_rcu_dereference_check(c); \ 577 smp_read_barrier_depends(); \ 578 (_________p1); \ 579 }) 580 581/** 582 * rcu_dereference_index_check() - rcu_dereference for indices with debug checking 583 * @p: The pointer to read, prior to dereferencing 584 * @c: The conditions under which the dereference will take place 585 * 586 * Similar to rcu_dereference_check(), but omits the sparse checking. 587 * This allows rcu_dereference_index_check() to be used on integers, 588 * which can then be used as array indices. Attempting to use 589 * rcu_dereference_check() on an integer will give compiler warnings 590 * because the sparse address-space mechanism relies on dereferencing 591 * the RCU-protected pointer. Dereferencing integers is not something 592 * that even gcc will put up with. 593 * 594 * Note that this function does not implicitly check for RCU read-side 595 * critical sections. If this function gains lots of uses, it might 596 * make sense to provide versions for each flavor of RCU, but it does 597 * not make sense as of early 2010. 598 */ 599#define rcu_dereference_index_check(p, c) \ 600 __rcu_dereference_index_check((p), (c)) 601 602#endif /* __LINUX_RCUPDATE_H */ 603