1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * Sleepable Read-Copy Update mechanism for mutual exclusion 4 * 5 * Copyright (C) IBM Corporation, 2006 6 * Copyright (C) Fujitsu, 2012 7 * 8 * Author: Paul McKenney <paulmck@linux.ibm.com> 9 * Lai Jiangshan <laijs@cn.fujitsu.com> 10 * 11 * For detailed explanation of Read-Copy Update mechanism see - 12 * Documentation/RCU/ *.txt 13 * 14 */ 15 16 #ifndef _LINUX_SRCU_H 17 #define _LINUX_SRCU_H 18 19 #include <linux/mutex.h> 20 #include <linux/rcupdate.h> 21 #include <linux/rculist.h> 22 #include <linux/workqueue.h> 23 #include <linux/rcu_segcblist.h> 24 25 context_lock_struct(srcu_struct, __reentrant_ctx_lock); 26 27 #ifdef CONFIG_DEBUG_LOCK_ALLOC 28 29 int init_srcu_struct_lockdep(struct srcu_struct *ssp, const char *name, 30 struct lock_class_key *key); 31 static inline int __init_srcu_struct(struct srcu_struct *ssp, const char *name, 32 struct lock_class_key *key) 33 { 34 return init_srcu_struct_lockdep(ssp, name, key); 35 } 36 #ifndef CONFIG_TINY_SRCU 37 int __init_srcu_struct_fast(struct srcu_struct *ssp, const char *name, struct lock_class_key *key); 38 int __init_srcu_struct_fast_updown(struct srcu_struct *ssp, const char *name, 39 struct lock_class_key *key); 40 int __init_srcu_struct_atomic(struct srcu_struct *ssp, const char *name, 41 struct lock_class_key *key); 42 #endif // #ifndef CONFIG_TINY_SRCU 43 44 #define init_srcu_struct_fast(ssp) \ 45 ({ \ 46 static struct lock_class_key __srcu_key; \ 47 \ 48 __init_srcu_struct_fast((ssp), #ssp, &__srcu_key); \ 49 }) 50 51 #define init_srcu_struct_fast_updown(ssp) \ 52 ({ \ 53 static struct lock_class_key __srcu_key; \ 54 \ 55 __init_srcu_struct_fast_updown((ssp), #ssp, &__srcu_key); \ 56 }) 57 58 #define init_srcu_struct_atomic(ssp) \ 59 ({ \ 60 static struct lock_class_key __srcu_key; \ 61 \ 62 __init_srcu_struct_atomic((ssp), #ssp, &__srcu_key); \ 63 }) 64 65 #define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name }, 66 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 67 68 int init_srcu_struct_generic(struct srcu_struct *ssp); 69 static inline int __init_srcu_struct(struct srcu_struct *ssp, const char *name, 70 struct lock_class_key *key) 71 { 72 return init_srcu_struct_generic(ssp); 73 } 74 #ifndef CONFIG_TINY_SRCU 75 int init_srcu_struct_fast(struct srcu_struct *ssp); 76 int init_srcu_struct_fast_updown(struct srcu_struct *ssp); 77 int init_srcu_struct_atomic(struct srcu_struct *ssp); 78 #endif // #ifndef CONFIG_TINY_SRCU 79 80 #define __SRCU_DEP_MAP_INIT(srcu_name) 81 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 82 83 #define init_srcu_struct(ssp) \ 84 ({ \ 85 static struct lock_class_key __srcu_key; \ 86 \ 87 __init_srcu_struct((ssp), #ssp, &__srcu_key); \ 88 }) 89 90 /* Values for SRCU Tree srcu_data ->srcu_reader_flavor, but also used by rcutorture. */ 91 #define SRCU_READ_FLAVOR_NORMAL 0x01 // srcu_read_lock(). 92 #define SRCU_READ_FLAVOR_NMI 0x02 // srcu_read_lock_nmisafe(). 93 // 0x04 // SRCU-lite is no longer with us. 94 #define SRCU_READ_FLAVOR_FAST 0x04 // srcu_read_lock_fast(), also NMI-safe. 95 #define SRCU_READ_FLAVOR_FAST_UPDOWN 0x08 // srcu_read_lock_fast_updown(). 96 #define SRCU_READ_FLAVOR_ATOMIC 0x10 // srcu_read_lock_atomic(). 97 #define SRCU_READ_FLAVOR_ALL (SRCU_READ_FLAVOR_NORMAL | SRCU_READ_FLAVOR_NMI | \ 98 SRCU_READ_FLAVOR_FAST | SRCU_READ_FLAVOR_FAST_UPDOWN | \ 99 SRCU_READ_FLAVOR_ATOMIC) 100 // All of the above. 101 #define SRCU_READ_FLAVOR_PREDEF (SRCU_READ_FLAVOR_FAST | SRCU_READ_FLAVOR_ATOMIC) 102 // Flavors special DEFINE_SRCU() flavors. 103 #define SRCU_READ_FLAVOR_SLOWGP (SRCU_READ_FLAVOR_FAST | SRCU_READ_FLAVOR_FAST_UPDOWN) 104 // Flavors requiring synchronize_rcu() 105 // instead of smp_mb(). 106 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases_shared(ssp); 107 108 #ifdef CONFIG_TINY_SRCU 109 #include <linux/srcutiny.h> 110 #elif defined(CONFIG_TREE_SRCU) 111 #include <linux/srcutree.h> 112 #else 113 #error "Unknown SRCU implementation specified to kernel configuration" 114 #endif 115 116 void call_srcu(struct srcu_struct *ssp, struct rcu_head *head, 117 void (*func)(struct rcu_head *head)); 118 void cleanup_srcu_struct(struct srcu_struct *ssp); 119 void synchronize_srcu(struct srcu_struct *ssp); 120 void synchronize_srcu_atomic(struct srcu_struct *ssp); 121 122 /** 123 * list_splice_init_srcu - splice an SRCU-protected list into an existing list 124 * @list: the SRCU-protected list to splice 125 * @head: the place in the existing list to splice the first list into 126 * @ssp: the srcu_struct that readers of @list use 127 * 128 * Like list_splice_init_rcu(), but wait for the SRCU readers of @list 129 * instead of RCU ones, so that none of them can follow a next pointer 130 * onto @head. 131 * 132 * Note that this function blocks, and that the caller must prevent any 133 * other updates to @list. 134 */ 135 static inline void list_splice_init_srcu(struct list_head *list, 136 struct list_head *head, 137 struct srcu_struct *ssp) 138 { 139 struct list_head *first = list->next; 140 struct list_head *last = list->prev; 141 struct list_head *next = head->next; 142 143 if (list_empty(list)) 144 return; 145 146 INIT_LIST_HEAD_RCU(list); 147 synchronize_srcu(ssp); 148 ASSERT_EXCLUSIVE_ACCESS(*first); 149 ASSERT_EXCLUSIVE_ACCESS(*last); 150 151 last->next = next; 152 rcu_assign_pointer(list_next_rcu(head), first); 153 first->prev = head; 154 next->prev = last; 155 } 156 157 #define SRCU_GET_STATE_COMPLETED 0x1 158 159 /** 160 * get_completed_synchronize_srcu - Return a pre-completed polled state cookie 161 * 162 * Returns a value that poll_state_synchronize_srcu() will always treat 163 * as a cookie whose grace period has already completed. 164 */ 165 static inline unsigned long get_completed_synchronize_srcu(void) 166 { 167 return SRCU_GET_STATE_COMPLETED; 168 } 169 170 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp); 171 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp); 172 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie); 173 174 // Maximum number of unsigned long values corresponding to 175 // not-yet-completed SRCU grace periods. 176 #define NUM_ACTIVE_SRCU_POLL_OLDSTATE 2 177 178 /** 179 * same_state_synchronize_srcu - Are two old-state values identical? 180 * @oldstate1: First old-state value. 181 * @oldstate2: Second old-state value. 182 * 183 * The two old-state values must have been obtained from either 184 * get_state_synchronize_srcu(), start_poll_synchronize_srcu(), or 185 * get_completed_synchronize_srcu(). Returns @true if the two values are 186 * identical and @false otherwise. This allows structures whose lifetimes 187 * are tracked by old-state values to push these values to a list header, 188 * allowing those structures to be slightly smaller. 189 */ 190 static inline bool same_state_synchronize_srcu(unsigned long oldstate1, unsigned long oldstate2) 191 { 192 return oldstate1 == oldstate2; 193 } 194 195 #ifdef CONFIG_NEED_SRCU_NMI_SAFE 196 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires_shared(ssp); 197 void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) __releases_shared(ssp); 198 #else 199 static inline int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) 200 __acquires_shared(ssp) 201 { 202 return __srcu_read_lock(ssp); 203 } 204 static inline void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) 205 __releases_shared(ssp) 206 { 207 __srcu_read_unlock(ssp, idx); 208 } 209 #endif /* CONFIG_NEED_SRCU_NMI_SAFE */ 210 211 void srcu_init(void); 212 213 #ifdef CONFIG_DEBUG_LOCK_ALLOC 214 215 /** 216 * srcu_read_lock_held - might we be in SRCU read-side critical section? 217 * @ssp: The srcu_struct structure to check 218 * 219 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU 220 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC, 221 * this assumes we are in an SRCU read-side critical section unless it can 222 * prove otherwise. 223 * 224 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot 225 * and while lockdep is disabled. 226 * 227 * Note that SRCU is based on its own statemachine and it doesn't 228 * relies on normal RCU, it can be called from the CPU which 229 * is in the idle loop from an RCU point of view or offline. 230 */ 231 static inline int srcu_read_lock_held(const struct srcu_struct *ssp) 232 { 233 if (!debug_lockdep_rcu_enabled()) 234 return 1; 235 return lock_is_held(&ssp->dep_map); 236 } 237 238 /* 239 * Annotations provide deadlock detection for SRCU. 240 * 241 * Similar to other lockdep annotations, except there is an additional 242 * srcu_lock_sync(), which is basically an empty *write*-side critical section, 243 * see lock_sync() for more information. 244 */ 245 246 /* Annotates a srcu_read_lock() */ 247 static inline void srcu_lock_acquire(struct lockdep_map *map) 248 { 249 lock_map_acquire_read(map); 250 } 251 252 /* Annotates a srcu_read_lock() */ 253 static inline void srcu_lock_release(struct lockdep_map *map) 254 { 255 lock_map_release(map); 256 } 257 258 /* Annotates a synchronize_srcu() */ 259 static inline void srcu_lock_sync(struct lockdep_map *map) 260 { 261 lock_map_sync(map); 262 } 263 264 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 265 266 static inline int srcu_read_lock_held(const struct srcu_struct *ssp) 267 { 268 return 1; 269 } 270 271 #define srcu_lock_acquire(m) do { } while (0) 272 #define srcu_lock_release(m) do { } while (0) 273 #define srcu_lock_sync(m) do { } while (0) 274 275 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 276 277 /* 278 * No-op helper to denote that ssp must be held. Because SRCU-protected pointers 279 * should still be marked with __rcu_guarded, and we do not want to mark them 280 * with __guarded_by(ssp) as it would complicate annotations for writers, we 281 * choose the following strategy: srcu_dereference_check() calls this helper 282 * that checks that the passed ssp is held, and then fake-acquires 'RCU'. 283 * srcu_dereference_check() only calls this helper if @c is the compile-time 284 * constant 0 because otherwise its callers may satisfy the condition @c 285 * instead of holding ssp, e.g. on the update side. srcu_dereference_notrace() 286 * passes 1 as @c and hence calls this helper explicitly. 287 */ 288 static inline void __srcu_read_lock_must_hold(const struct srcu_struct *ssp) __must_hold_shared(ssp) { } 289 290 /** 291 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing 292 * @p: the pointer to fetch and protect for later dereferencing 293 * @ssp: pointer to the srcu_struct, which is used to check that we 294 * really are in an SRCU read-side critical section. 295 * @c: condition to check for update-side use 296 * 297 * If PROVE_RCU is enabled, invoking this outside of an RCU read-side 298 * critical section will result in an RCU-lockdep splat, unless @c evaluates 299 * to 1. The @c argument will normally be a logical expression containing 300 * lockdep_is_held() calls. 301 */ 302 #define srcu_dereference_check(p, ssp, c) \ 303 ({ \ 304 if (const_true(!(c))) \ 305 __srcu_read_lock_must_hold(ssp); \ 306 __acquire_shared_ctx_lock(RCU); \ 307 __auto_type __v = __rcu_dereference_check((p), __UNIQUE_ID(rcu), \ 308 (c) || srcu_read_lock_held(ssp), __rcu); \ 309 __release_shared_ctx_lock(RCU); \ 310 __v; \ 311 }) 312 313 /** 314 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing 315 * @p: the pointer to fetch and protect for later dereferencing 316 * @ssp: pointer to the srcu_struct, which is used to check that we 317 * really are in an SRCU read-side critical section. 318 * 319 * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU 320 * is enabled, invoking this outside of an RCU read-side critical 321 * section will result in an RCU-lockdep splat. 322 */ 323 #define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0) 324 325 /** 326 * srcu_dereference_notrace - no tracing and no lockdep calls from here 327 * @p: the pointer to fetch and protect for later dereferencing 328 * @ssp: pointer to the srcu_struct, which is used to check that we 329 * really are in an SRCU read-side critical section. 330 */ 331 #define srcu_dereference_notrace(p, ssp) \ 332 ({ \ 333 __srcu_read_lock_must_hold(ssp); \ 334 srcu_dereference_check((p), (ssp), 1); \ 335 }) 336 337 /** 338 * srcu_read_lock - register a new reader for an SRCU-protected structure. 339 * @ssp: srcu_struct in which to register the new reader. 340 * 341 * Enter an SRCU read-side critical section. Note that SRCU read-side 342 * critical sections may be nested. However, it is illegal to 343 * call anything that waits on an SRCU grace period for the same 344 * srcu_struct, whether directly or indirectly. Please note that 345 * one way to indirectly wait on an SRCU grace period is to acquire 346 * a mutex that is held elsewhere while calling synchronize_srcu() or 347 * synchronize_srcu_expedited(). 348 * 349 * The return value from srcu_read_lock() is guaranteed to be 350 * non-negative. This value must be passed unaltered to the matching 351 * srcu_read_unlock(). Note that srcu_read_lock() and the matching 352 * srcu_read_unlock() must occur in the same context, for example, it is 353 * illegal to invoke srcu_read_unlock() in an irq handler if the matching 354 * srcu_read_lock() was invoked in process context. Or, for that matter to 355 * invoke srcu_read_unlock() from one task and the matching srcu_read_lock() 356 * from another. 357 */ 358 static inline int srcu_read_lock(struct srcu_struct *ssp) 359 __acquires_shared(ssp) 360 { 361 int retval; 362 363 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 364 retval = __srcu_read_lock(ssp); 365 srcu_lock_acquire(&ssp->dep_map); 366 return retval; 367 } 368 369 /** 370 * srcu_read_lock_atomic - register a new reader promising an atomic section 371 * @ssp: srcu_struct in which to register the new reader. 372 * 373 * As srcu_read_lock(), but the caller promises that the read-side 374 * critical section never sleeps and never blocks on anything which 375 * may itself depend on memory allocation to make progress. Preemption 376 * is disabled for the duration, which both enforces that promise (any 377 * sleepable call in the section will splat on every configuration) 378 * and bounds the section so that the update side may spin rather 379 * than sleep when waiting for readers: see synchronize_srcu_atomic(). 380 * 381 * The lock and matching srcu_read_unlock_atomic() must be invoked on 382 * the same CPU, from the same context; passing the return value to 383 * another task is not permitted for this flavor. 384 */ 385 static inline int srcu_read_lock_atomic(struct srcu_struct *ssp) 386 __acquires_shared(ssp) 387 { 388 int retval; 389 390 preempt_disable(); 391 /* 392 * Arm might_sleep() to catch even a *potentially* sleeping call 393 * in the section, not just an actual schedule: the atomic-domain 394 * promise must hold on every path, contended or not. In hardirq, 395 * softirq, or NMI the annotation would land on the interrupted 396 * task, and can also result in data races against that task's 397 * own non_block_start()/non_block_end() invocations; it is also 398 * redundant there, so skip it. 399 */ 400 if (in_task()) 401 non_block_start(); 402 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_ATOMIC); 403 retval = __srcu_read_lock(ssp); 404 srcu_lock_acquire(&ssp->dep_map); 405 return retval; 406 } 407 408 /** 409 * srcu_read_lock_fast - register a new reader for an SRCU-protected structure. 410 * @ssp: srcu_struct in which to register the new reader. 411 * 412 * Enter an SRCU read-side critical section, but for a light-weight 413 * smp_mb()-free reader. See srcu_read_lock() for more information. This 414 * function is NMI-safe, in a manner similar to srcu_read_lock_nmisafe(). 415 * 416 * For srcu_read_lock_fast() to be used on an srcu_struct structure, 417 * that structure must have been defined using either DEFINE_SRCU_FAST() 418 * or DEFINE_STATIC_SRCU_FAST() on the one hand or initialized with 419 * init_srcu_struct_fast() on the other. Such an srcu_struct structure 420 * cannot be passed to any non-fast variant of srcu_read_{,un}lock() or 421 * srcu_{down,up}_read(). In kernels built with CONFIG_PROVE_RCU=y, 422 * __srcu_check_read_flavor() will complain bitterly if you ignore this 423 * restriction. 424 * 425 * Grace-period auto-expediting is disabled for SRCU-fast srcu_struct 426 * structures because SRCU-fast expedited grace periods invoke 427 * synchronize_rcu_expedited(), IPIs and all. If you need expedited 428 * SRCU-fast grace periods, use synchronize_srcu_expedited(). 429 * 430 * The srcu_read_lock_fast() function can be invoked only from those 431 * contexts where RCU is watching, that is, from contexts where it would 432 * be legal to invoke rcu_read_lock(). Otherwise, lockdep will complain. 433 */ 434 static inline struct srcu_ctr __percpu *srcu_read_lock_fast(struct srcu_struct *ssp) __acquires_shared(ssp) 435 __acquires_shared(ssp) 436 { 437 struct srcu_ctr __percpu *retval; 438 439 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_fast()."); 440 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST); 441 retval = __srcu_read_lock_fast(ssp); 442 rcu_try_lock_acquire(&ssp->dep_map); 443 return retval; 444 } 445 446 /** 447 * srcu_read_lock_fast_updown - register a new reader for an SRCU-fast-updown structure. 448 * @ssp: srcu_struct in which to register the new reader. 449 * 450 * Enter an SRCU read-side critical section, but for a light-weight 451 * smp_mb()-free reader. See srcu_read_lock() for more information. 452 * This function is compatible with srcu_down_read_fast(), but is not 453 * NMI-safe. 454 * 455 * For srcu_read_lock_fast_updown() to be used on an srcu_struct 456 * structure, that structure must have been defined using either 457 * DEFINE_SRCU_FAST_UPDOWN() or DEFINE_STATIC_SRCU_FAST_UPDOWN() on the one 458 * hand or initialized with init_srcu_struct_fast_updown() on the other. 459 * Such an srcu_struct structure cannot be passed to any non-fast-updown 460 * variant of srcu_read_{,un}lock() or srcu_{down,up}_read(). In kernels 461 * built with CONFIG_PROVE_RCU=y, __srcu_check_read_flavor() will complain 462 * bitterly if you ignore this * restriction. 463 * 464 * Grace-period auto-expediting is disabled for SRCU-fast-updown 465 * srcu_struct structures because SRCU-fast-updown expedited grace periods 466 * invoke synchronize_rcu_expedited(), IPIs and all. If you need expedited 467 * SRCU-fast-updown grace periods, use synchronize_srcu_expedited(). 468 * 469 * The srcu_read_lock_fast_updown() function can be invoked only from 470 * those contexts where RCU is watching, that is, from contexts where 471 * it would be legal to invoke rcu_read_lock(). Otherwise, lockdep will 472 * complain. 473 */ 474 static inline struct srcu_ctr __percpu *srcu_read_lock_fast_updown(struct srcu_struct *ssp) 475 __acquires_shared(ssp) 476 { 477 struct srcu_ctr __percpu *retval; 478 479 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_lock_fast_updown()."); 480 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN); 481 retval = __srcu_read_lock_fast_updown(ssp); 482 rcu_try_lock_acquire(&ssp->dep_map); 483 return retval; 484 } 485 486 /* 487 * Used by tracing, cannot be traced and cannot call lockdep. 488 * See srcu_read_lock_fast() for more information. 489 */ 490 static inline struct srcu_ctr __percpu *srcu_read_lock_fast_notrace(struct srcu_struct *ssp) 491 __acquires_shared(ssp) 492 { 493 struct srcu_ctr __percpu *retval; 494 495 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST); 496 retval = __srcu_read_lock_fast(ssp); 497 return retval; 498 } 499 500 /** 501 * srcu_down_read_fast - register a new reader for an SRCU-protected structure. 502 * @ssp: srcu_struct in which to register the new reader. 503 * 504 * Enter a semaphore-like SRCU read-side critical section, but for 505 * a light-weight smp_mb()-free reader. See srcu_read_lock_fast() and 506 * srcu_down_read() for more information. 507 * 508 * The same srcu_struct may be used concurrently by srcu_down_read_fast() 509 * and srcu_read_lock_fast(). However, the same definition/initialization 510 * requirements called out for srcu_read_lock_fast_updown() apply. 511 */ 512 static inline struct srcu_ctr __percpu *srcu_down_read_fast(struct srcu_struct *ssp) __acquires_shared(ssp) 513 { 514 WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi()); 515 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_down_read_fast()."); 516 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN); 517 return __srcu_read_lock_fast_updown(ssp); 518 } 519 520 /** 521 * srcu_read_lock_nmisafe - register a new reader for an SRCU-protected structure. 522 * @ssp: srcu_struct in which to register the new reader. 523 * 524 * Enter an SRCU read-side critical section, but in an NMI-safe manner. 525 * See srcu_read_lock() for more information. 526 * 527 * If srcu_read_lock_nmisafe() is ever used on an srcu_struct structure, 528 * then none of the other flavors may be used, whether before, during, 529 * or after. 530 */ 531 static inline int srcu_read_lock_nmisafe(struct srcu_struct *ssp) 532 __acquires_shared(ssp) 533 { 534 int retval; 535 536 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NMI); 537 retval = __srcu_read_lock_nmisafe(ssp); 538 rcu_try_lock_acquire(&ssp->dep_map); 539 return retval; 540 } 541 542 /* Used by tracing, cannot be traced and cannot invoke lockdep. */ 543 static inline notrace int 544 srcu_read_lock_notrace(struct srcu_struct *ssp) 545 __acquires_shared(ssp) 546 { 547 int retval; 548 549 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 550 retval = __srcu_read_lock(ssp); 551 return retval; 552 } 553 554 /** 555 * srcu_down_read - register a new reader for an SRCU-protected structure. 556 * @ssp: srcu_struct in which to register the new reader. 557 * 558 * Enter a semaphore-like SRCU read-side critical section. Note that 559 * SRCU read-side critical sections may be nested. However, it is 560 * illegal to call anything that waits on an SRCU grace period for the 561 * same srcu_struct, whether directly or indirectly. Please note that 562 * one way to indirectly wait on an SRCU grace period is to acquire 563 * a mutex that is held elsewhere while calling synchronize_srcu() or 564 * synchronize_srcu_expedited(). But if you want lockdep to help you 565 * keep this stuff straight, you should instead use srcu_read_lock(). 566 * 567 * The semaphore-like nature of srcu_down_read() means that the matching 568 * srcu_up_read() can be invoked from some other context, for example, 569 * from some other task or from an irq handler. However, neither 570 * srcu_down_read() nor srcu_up_read() may be invoked from an NMI handler. 571 * 572 * Calls to srcu_down_read() may be nested, similar to the manner in 573 * which calls to down_read() may be nested. The same srcu_struct may be 574 * used concurrently by srcu_down_read() and srcu_read_lock(). 575 */ 576 static inline int srcu_down_read(struct srcu_struct *ssp) 577 __acquires_shared(ssp) 578 { 579 WARN_ON_ONCE(in_nmi()); 580 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 581 return __srcu_read_lock(ssp); 582 } 583 584 /** 585 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure. 586 * @ssp: srcu_struct in which to unregister the old reader. 587 * @idx: return value from corresponding srcu_read_lock(). 588 * 589 * Exit an SRCU read-side critical section. 590 */ 591 static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx) 592 __releases_shared(ssp) 593 { 594 WARN_ON_ONCE(idx & ~0x1); 595 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 596 srcu_lock_release(&ssp->dep_map); 597 __srcu_read_unlock(ssp, idx); 598 } 599 600 /** 601 * srcu_read_unlock_atomic - unregister an atomic-section reader 602 * @ssp: srcu_struct from which to unregister the old reader. 603 * @idx: return value from corresponding srcu_read_lock_atomic(). 604 */ 605 static inline void srcu_read_unlock_atomic(struct srcu_struct *ssp, int idx) 606 __releases_shared(ssp) 607 { 608 WARN_ON_ONCE(idx & ~0x1); 609 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_ATOMIC); 610 srcu_lock_release(&ssp->dep_map); 611 __srcu_read_unlock(ssp, idx); 612 if (in_task()) 613 non_block_end(); 614 preempt_enable(); 615 } 616 617 /** 618 * srcu_read_unlock_fast - unregister a old reader from an SRCU-protected structure. 619 * @ssp: srcu_struct in which to unregister the old reader. 620 * @scp: return value from corresponding srcu_read_lock_fast(). 621 * 622 * Exit a light-weight SRCU read-side critical section. 623 */ 624 static inline void srcu_read_unlock_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp) 625 __releases_shared(ssp) 626 { 627 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST); 628 srcu_lock_release(&ssp->dep_map); 629 __srcu_read_unlock_fast(ssp, scp); 630 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_read_unlock_fast()."); 631 } 632 633 /** 634 * srcu_read_unlock_fast_updown - unregister a old reader from an SRCU-fast-updown structure. 635 * @ssp: srcu_struct in which to unregister the old reader. 636 * @scp: return value from corresponding srcu_read_lock_fast_updown(). 637 * 638 * Exit an SRCU-fast-updown read-side critical section. 639 */ 640 static inline void 641 srcu_read_unlock_fast_updown(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp) __releases_shared(ssp) 642 { 643 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN); 644 srcu_lock_release(&ssp->dep_map); 645 __srcu_read_unlock_fast_updown(ssp, scp); 646 RCU_LOCKDEP_WARN(!rcu_is_watching(), 647 "RCU must be watching srcu_read_unlock_fast_updown()."); 648 } 649 650 /* 651 * Used by tracing, cannot be traced and cannot call lockdep. 652 * See srcu_read_unlock_fast() for more information. 653 */ 654 static inline void srcu_read_unlock_fast_notrace(struct srcu_struct *ssp, 655 struct srcu_ctr __percpu *scp) __releases_shared(ssp) 656 { 657 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST); 658 __srcu_read_unlock_fast(ssp, scp); 659 } 660 661 /** 662 * srcu_up_read_fast - unregister a old reader from an SRCU-protected structure. 663 * @ssp: srcu_struct in which to unregister the old reader. 664 * @scp: return value from corresponding srcu_read_lock_fast(). 665 * 666 * Exit an SRCU read-side critical section, but not necessarily from 667 * the same context as the maching srcu_down_read_fast(). 668 */ 669 static inline void srcu_up_read_fast(struct srcu_struct *ssp, struct srcu_ctr __percpu *scp) 670 __releases_shared(ssp) 671 { 672 WARN_ON_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && in_nmi()); 673 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_FAST_UPDOWN); 674 __srcu_read_unlock_fast_updown(ssp, scp); 675 RCU_LOCKDEP_WARN(!rcu_is_watching(), "RCU must be watching srcu_up_read_fast_updown()."); 676 } 677 678 /** 679 * srcu_read_unlock_nmisafe - unregister a old reader from an SRCU-protected structure. 680 * @ssp: srcu_struct in which to unregister the old reader. 681 * @idx: return value from corresponding srcu_read_lock_nmisafe(). 682 * 683 * Exit an SRCU read-side critical section, but in an NMI-safe manner. 684 */ 685 static inline void srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) 686 __releases_shared(ssp) 687 { 688 WARN_ON_ONCE(idx & ~0x1); 689 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NMI); 690 rcu_lock_release(&ssp->dep_map); 691 __srcu_read_unlock_nmisafe(ssp, idx); 692 } 693 694 /* Used by tracing, cannot be traced and cannot call lockdep. */ 695 static inline notrace void 696 srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases_shared(ssp) 697 { 698 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 699 __srcu_read_unlock(ssp, idx); 700 } 701 702 /** 703 * srcu_up_read - unregister a old reader from an SRCU-protected structure. 704 * @ssp: srcu_struct in which to unregister the old reader. 705 * @idx: return value from corresponding srcu_read_lock(). 706 * 707 * Exit an SRCU read-side critical section, but not necessarily from 708 * the same context as the maching srcu_down_read(). 709 */ 710 static inline void srcu_up_read(struct srcu_struct *ssp, int idx) 711 __releases_shared(ssp) 712 { 713 WARN_ON_ONCE(idx & ~0x1); 714 WARN_ON_ONCE(in_nmi()); 715 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 716 __srcu_read_unlock(ssp, idx); 717 } 718 719 /** 720 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock 721 * 722 * Converts the preceding srcu_read_unlock into a two-way memory barrier. 723 * 724 * Call this after srcu_read_unlock, to guarantee that all memory operations 725 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after 726 * the preceding srcu_read_unlock. 727 */ 728 static inline void smp_mb__after_srcu_read_unlock(void) 729 { 730 /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */ 731 } 732 733 /** 734 * smp_mb__after_srcu_read_lock - ensure full ordering after srcu_read_lock 735 * 736 * Converts the preceding srcu_read_lock into a two-way memory barrier. 737 * 738 * Call this after srcu_read_lock, to guarantee that all memory operations 739 * that occur after smp_mb__after_srcu_read_lock will appear to happen after 740 * the preceding srcu_read_lock. 741 */ 742 static inline void smp_mb__after_srcu_read_lock(void) 743 { 744 /* __srcu_read_lock has smp_mb() internally so nothing to do here. */ 745 } 746 747 DEFINE_LOCK_GUARD_1(srcu, struct srcu_struct, 748 _T->idx = srcu_read_lock(_T->lock), 749 srcu_read_unlock(_T->lock, _T->idx), 750 int idx) 751 DECLARE_LOCK_GUARD_1_ATTRS(srcu, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T)) 752 #define class_srcu_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu, _T) 753 754 DEFINE_LOCK_GUARD_1(srcu_fast, struct srcu_struct, 755 _T->scp = srcu_read_lock_fast(_T->lock), 756 srcu_read_unlock_fast(_T->lock, _T->scp), 757 struct srcu_ctr __percpu *scp) 758 DECLARE_LOCK_GUARD_1_ATTRS(srcu_fast, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T)) 759 #define class_srcu_fast_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu_fast, _T) 760 761 DEFINE_LOCK_GUARD_1(srcu_fast_notrace, struct srcu_struct, 762 _T->scp = srcu_read_lock_fast_notrace(_T->lock), 763 srcu_read_unlock_fast_notrace(_T->lock, _T->scp), 764 struct srcu_ctr __percpu *scp) 765 DECLARE_LOCK_GUARD_1_ATTRS(srcu_fast_notrace, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T)) 766 #define class_srcu_fast_notrace_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu_fast_notrace, _T) 767 768 DEFINE_LOCK_GUARD_1(srcu_fast_updown, struct srcu_struct, 769 _T->scp = srcu_read_lock_fast_updown(_T->lock), 770 srcu_read_unlock_fast_updown(_T->lock, _T->scp), 771 struct srcu_ctr __percpu *scp) 772 DECLARE_LOCK_GUARD_1_ATTRS(srcu_fast_updown, __acquires_shared(_T), __releases_shared(*(struct srcu_struct **)_T)) 773 #define class_srcu_fast_updown_constructor(_T) WITH_LOCK_GUARD_1_ATTRS(srcu_fast_updown, _T) 774 775 #endif 776