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 <[email protected]> 9 * Lai Jiangshan <[email protected]> 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/workqueue.h> 22 #include <linux/rcu_segcblist.h> 23 24 struct srcu_struct; 25 26 #ifdef CONFIG_DEBUG_LOCK_ALLOC 27 28 int __init_srcu_struct(struct srcu_struct *ssp, const char *name, 29 struct lock_class_key *key); 30 31 #define init_srcu_struct(ssp) \ 32 ({ \ 33 static struct lock_class_key __srcu_key; \ 34 \ 35 __init_srcu_struct((ssp), #ssp, &__srcu_key); \ 36 }) 37 38 #define __SRCU_DEP_MAP_INIT(srcu_name) .dep_map = { .name = #srcu_name }, 39 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 40 41 int init_srcu_struct(struct srcu_struct *ssp); 42 43 #define __SRCU_DEP_MAP_INIT(srcu_name) 44 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 45 46 /* Values for SRCU Tree srcu_data ->srcu_reader_flavor, but also used by rcutorture. */ 47 #define SRCU_READ_FLAVOR_NORMAL 0x1 // srcu_read_lock(). 48 #define SRCU_READ_FLAVOR_NMI 0x2 // srcu_read_lock_nmisafe(). 49 #define SRCU_READ_FLAVOR_LITE 0x4 // srcu_read_lock_lite(). 50 #define SRCU_READ_FLAVOR_ALL (SRCU_READ_FLAVOR_NORMAL | SRCU_READ_FLAVOR_NMI | \ 51 SRCU_READ_FLAVOR_LITE) // All of the above. 52 #define SRCU_READ_FLAVOR_SLOWGP SRCU_READ_FLAVOR_LITE 53 // Flavors requiring synchronize_rcu() 54 // instead of smp_mb(). 55 56 #ifdef CONFIG_TINY_SRCU 57 #include <linux/srcutiny.h> 58 #elif defined(CONFIG_TREE_SRCU) 59 #include <linux/srcutree.h> 60 #else 61 #error "Unknown SRCU implementation specified to kernel configuration" 62 #endif 63 64 void call_srcu(struct srcu_struct *ssp, struct rcu_head *head, 65 void (*func)(struct rcu_head *head)); 66 void cleanup_srcu_struct(struct srcu_struct *ssp); 67 int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp); 68 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp); 69 #ifdef CONFIG_TINY_SRCU 70 #define __srcu_read_lock_lite __srcu_read_lock 71 #define __srcu_read_unlock_lite __srcu_read_unlock 72 #else // #ifdef CONFIG_TINY_SRCU 73 int __srcu_read_lock_lite(struct srcu_struct *ssp) __acquires(ssp); 74 void __srcu_read_unlock_lite(struct srcu_struct *ssp, int idx) __releases(ssp); 75 #endif // #else // #ifdef CONFIG_TINY_SRCU 76 void synchronize_srcu(struct srcu_struct *ssp); 77 78 #define SRCU_GET_STATE_COMPLETED 0x1 79 80 /** 81 * get_completed_synchronize_srcu - Return a pre-completed polled state cookie 82 * 83 * Returns a value that poll_state_synchronize_srcu() will always treat 84 * as a cookie whose grace period has already completed. 85 */ 86 static inline unsigned long get_completed_synchronize_srcu(void) 87 { 88 return SRCU_GET_STATE_COMPLETED; 89 } 90 91 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp); 92 unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp); 93 bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie); 94 95 // Maximum number of unsigned long values corresponding to 96 // not-yet-completed SRCU grace periods. 97 #define NUM_ACTIVE_SRCU_POLL_OLDSTATE 2 98 99 /** 100 * same_state_synchronize_srcu - Are two old-state values identical? 101 * @oldstate1: First old-state value. 102 * @oldstate2: Second old-state value. 103 * 104 * The two old-state values must have been obtained from either 105 * get_state_synchronize_srcu(), start_poll_synchronize_srcu(), or 106 * get_completed_synchronize_srcu(). Returns @true if the two values are 107 * identical and @false otherwise. This allows structures whose lifetimes 108 * are tracked by old-state values to push these values to a list header, 109 * allowing those structures to be slightly smaller. 110 */ 111 static inline bool same_state_synchronize_srcu(unsigned long oldstate1, unsigned long oldstate2) 112 { 113 return oldstate1 == oldstate2; 114 } 115 116 #ifdef CONFIG_NEED_SRCU_NMI_SAFE 117 int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp); 118 void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) __releases(ssp); 119 #else 120 static inline int __srcu_read_lock_nmisafe(struct srcu_struct *ssp) 121 { 122 return __srcu_read_lock(ssp); 123 } 124 static inline void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) 125 { 126 __srcu_read_unlock(ssp, idx); 127 } 128 #endif /* CONFIG_NEED_SRCU_NMI_SAFE */ 129 130 void srcu_init(void); 131 132 #ifdef CONFIG_DEBUG_LOCK_ALLOC 133 134 /** 135 * srcu_read_lock_held - might we be in SRCU read-side critical section? 136 * @ssp: The srcu_struct structure to check 137 * 138 * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU 139 * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC, 140 * this assumes we are in an SRCU read-side critical section unless it can 141 * prove otherwise. 142 * 143 * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot 144 * and while lockdep is disabled. 145 * 146 * Note that SRCU is based on its own statemachine and it doesn't 147 * relies on normal RCU, it can be called from the CPU which 148 * is in the idle loop from an RCU point of view or offline. 149 */ 150 static inline int srcu_read_lock_held(const struct srcu_struct *ssp) 151 { 152 if (!debug_lockdep_rcu_enabled()) 153 return 1; 154 return lock_is_held(&ssp->dep_map); 155 } 156 157 /* 158 * Annotations provide deadlock detection for SRCU. 159 * 160 * Similar to other lockdep annotations, except there is an additional 161 * srcu_lock_sync(), which is basically an empty *write*-side critical section, 162 * see lock_sync() for more information. 163 */ 164 165 /* Annotates a srcu_read_lock() */ 166 static inline void srcu_lock_acquire(struct lockdep_map *map) 167 { 168 lock_map_acquire_read(map); 169 } 170 171 /* Annotates a srcu_read_lock() */ 172 static inline void srcu_lock_release(struct lockdep_map *map) 173 { 174 lock_map_release(map); 175 } 176 177 /* Annotates a synchronize_srcu() */ 178 static inline void srcu_lock_sync(struct lockdep_map *map) 179 { 180 lock_map_sync(map); 181 } 182 183 #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 184 185 static inline int srcu_read_lock_held(const struct srcu_struct *ssp) 186 { 187 return 1; 188 } 189 190 #define srcu_lock_acquire(m) do { } while (0) 191 #define srcu_lock_release(m) do { } while (0) 192 #define srcu_lock_sync(m) do { } while (0) 193 194 #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 195 196 197 /** 198 * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing 199 * @p: the pointer to fetch and protect for later dereferencing 200 * @ssp: pointer to the srcu_struct, which is used to check that we 201 * really are in an SRCU read-side critical section. 202 * @c: condition to check for update-side use 203 * 204 * If PROVE_RCU is enabled, invoking this outside of an RCU read-side 205 * critical section will result in an RCU-lockdep splat, unless @c evaluates 206 * to 1. The @c argument will normally be a logical expression containing 207 * lockdep_is_held() calls. 208 */ 209 #define srcu_dereference_check(p, ssp, c) \ 210 __rcu_dereference_check((p), __UNIQUE_ID(rcu), \ 211 (c) || srcu_read_lock_held(ssp), __rcu) 212 213 /** 214 * srcu_dereference - fetch SRCU-protected pointer for later dereferencing 215 * @p: the pointer to fetch and protect for later dereferencing 216 * @ssp: pointer to the srcu_struct, which is used to check that we 217 * really are in an SRCU read-side critical section. 218 * 219 * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU 220 * is enabled, invoking this outside of an RCU read-side critical 221 * section will result in an RCU-lockdep splat. 222 */ 223 #define srcu_dereference(p, ssp) srcu_dereference_check((p), (ssp), 0) 224 225 /** 226 * srcu_dereference_notrace - no tracing and no lockdep calls from here 227 * @p: the pointer to fetch and protect for later dereferencing 228 * @ssp: pointer to the srcu_struct, which is used to check that we 229 * really are in an SRCU read-side critical section. 230 */ 231 #define srcu_dereference_notrace(p, ssp) srcu_dereference_check((p), (ssp), 1) 232 233 /** 234 * srcu_read_lock - register a new reader for an SRCU-protected structure. 235 * @ssp: srcu_struct in which to register the new reader. 236 * 237 * Enter an SRCU read-side critical section. Note that SRCU read-side 238 * critical sections may be nested. However, it is illegal to 239 * call anything that waits on an SRCU grace period for the same 240 * srcu_struct, whether directly or indirectly. Please note that 241 * one way to indirectly wait on an SRCU grace period is to acquire 242 * a mutex that is held elsewhere while calling synchronize_srcu() or 243 * synchronize_srcu_expedited(). 244 * 245 * The return value from srcu_read_lock() is guaranteed to be 246 * non-negative. This value must be passed unaltered to the matching 247 * srcu_read_unlock(). Note that srcu_read_lock() and the matching 248 * srcu_read_unlock() must occur in the same context, for example, it is 249 * illegal to invoke srcu_read_unlock() in an irq handler if the matching 250 * srcu_read_lock() was invoked in process context. Or, for that matter to 251 * invoke srcu_read_unlock() from one task and the matching srcu_read_lock() 252 * from another. 253 */ 254 static inline int srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp) 255 { 256 int retval; 257 258 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 259 retval = __srcu_read_lock(ssp); 260 srcu_lock_acquire(&ssp->dep_map); 261 return retval; 262 } 263 264 /** 265 * srcu_read_lock_lite - register a new reader for an SRCU-protected structure. 266 * @ssp: srcu_struct in which to register the new reader. 267 * 268 * Enter an SRCU read-side critical section, but for a light-weight 269 * smp_mb()-free reader. See srcu_read_lock() for more information. 270 * 271 * If srcu_read_lock_lite() is ever used on an srcu_struct structure, 272 * then none of the other flavors may be used, whether before, during, 273 * or after. Note that grace-period auto-expediting is disabled for _lite 274 * srcu_struct structures because auto-expedited grace periods invoke 275 * synchronize_rcu_expedited(), IPIs and all. 276 * 277 * Note that srcu_read_lock_lite() can be invoked only from those contexts 278 * where RCU is watching, that is, from contexts where it would be legal 279 * to invoke rcu_read_lock(). Otherwise, lockdep will complain. 280 */ 281 static inline int srcu_read_lock_lite(struct srcu_struct *ssp) __acquires(ssp) 282 { 283 int retval; 284 285 srcu_check_read_flavor_force(ssp, SRCU_READ_FLAVOR_LITE); 286 retval = __srcu_read_lock_lite(ssp); 287 rcu_try_lock_acquire(&ssp->dep_map); 288 return retval; 289 } 290 291 /** 292 * srcu_read_lock_nmisafe - register a new reader for an SRCU-protected structure. 293 * @ssp: srcu_struct in which to register the new reader. 294 * 295 * Enter an SRCU read-side critical section, but in an NMI-safe manner. 296 * See srcu_read_lock() for more information. 297 * 298 * If srcu_read_lock_nmisafe() is ever used on an srcu_struct structure, 299 * then none of the other flavors may be used, whether before, during, 300 * or after. 301 */ 302 static inline int srcu_read_lock_nmisafe(struct srcu_struct *ssp) __acquires(ssp) 303 { 304 int retval; 305 306 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NMI); 307 retval = __srcu_read_lock_nmisafe(ssp); 308 rcu_try_lock_acquire(&ssp->dep_map); 309 return retval; 310 } 311 312 /* Used by tracing, cannot be traced and cannot invoke lockdep. */ 313 static inline notrace int 314 srcu_read_lock_notrace(struct srcu_struct *ssp) __acquires(ssp) 315 { 316 int retval; 317 318 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 319 retval = __srcu_read_lock(ssp); 320 return retval; 321 } 322 323 /** 324 * srcu_down_read - register a new reader for an SRCU-protected structure. 325 * @ssp: srcu_struct in which to register the new reader. 326 * 327 * Enter a semaphore-like SRCU read-side critical section. Note that 328 * SRCU read-side critical sections may be nested. However, it is 329 * illegal to call anything that waits on an SRCU grace period for the 330 * same srcu_struct, whether directly or indirectly. Please note that 331 * one way to indirectly wait on an SRCU grace period is to acquire 332 * a mutex that is held elsewhere while calling synchronize_srcu() or 333 * synchronize_srcu_expedited(). But if you want lockdep to help you 334 * keep this stuff straight, you should instead use srcu_read_lock(). 335 * 336 * The semaphore-like nature of srcu_down_read() means that the matching 337 * srcu_up_read() can be invoked from some other context, for example, 338 * from some other task or from an irq handler. However, neither 339 * srcu_down_read() nor srcu_up_read() may be invoked from an NMI handler. 340 * 341 * Calls to srcu_down_read() may be nested, similar to the manner in 342 * which calls to down_read() may be nested. 343 */ 344 static inline int srcu_down_read(struct srcu_struct *ssp) __acquires(ssp) 345 { 346 WARN_ON_ONCE(in_nmi()); 347 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 348 return __srcu_read_lock(ssp); 349 } 350 351 /** 352 * srcu_read_unlock - unregister a old reader from an SRCU-protected structure. 353 * @ssp: srcu_struct in which to unregister the old reader. 354 * @idx: return value from corresponding srcu_read_lock(). 355 * 356 * Exit an SRCU read-side critical section. 357 */ 358 static inline void srcu_read_unlock(struct srcu_struct *ssp, int idx) 359 __releases(ssp) 360 { 361 WARN_ON_ONCE(idx & ~0x1); 362 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 363 srcu_lock_release(&ssp->dep_map); 364 __srcu_read_unlock(ssp, idx); 365 } 366 367 /** 368 * srcu_read_unlock_lite - unregister a old reader from an SRCU-protected structure. 369 * @ssp: srcu_struct in which to unregister the old reader. 370 * @idx: return value from corresponding srcu_read_lock(). 371 * 372 * Exit a light-weight SRCU read-side critical section. 373 */ 374 static inline void srcu_read_unlock_lite(struct srcu_struct *ssp, int idx) 375 __releases(ssp) 376 { 377 WARN_ON_ONCE(idx & ~0x1); 378 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_LITE); 379 srcu_lock_release(&ssp->dep_map); 380 __srcu_read_unlock_lite(ssp, idx); 381 } 382 383 /** 384 * srcu_read_unlock_nmisafe - unregister a old reader from an SRCU-protected structure. 385 * @ssp: srcu_struct in which to unregister the old reader. 386 * @idx: return value from corresponding srcu_read_lock(). 387 * 388 * Exit an SRCU read-side critical section, but in an NMI-safe manner. 389 */ 390 static inline void srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx) 391 __releases(ssp) 392 { 393 WARN_ON_ONCE(idx & ~0x1); 394 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NMI); 395 rcu_lock_release(&ssp->dep_map); 396 __srcu_read_unlock_nmisafe(ssp, idx); 397 } 398 399 /* Used by tracing, cannot be traced and cannot call lockdep. */ 400 static inline notrace void 401 srcu_read_unlock_notrace(struct srcu_struct *ssp, int idx) __releases(ssp) 402 { 403 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 404 __srcu_read_unlock(ssp, idx); 405 } 406 407 /** 408 * srcu_up_read - unregister a old reader from an SRCU-protected structure. 409 * @ssp: srcu_struct in which to unregister the old reader. 410 * @idx: return value from corresponding srcu_read_lock(). 411 * 412 * Exit an SRCU read-side critical section, but not necessarily from 413 * the same context as the maching srcu_down_read(). 414 */ 415 static inline void srcu_up_read(struct srcu_struct *ssp, int idx) 416 __releases(ssp) 417 { 418 WARN_ON_ONCE(idx & ~0x1); 419 WARN_ON_ONCE(in_nmi()); 420 srcu_check_read_flavor(ssp, SRCU_READ_FLAVOR_NORMAL); 421 __srcu_read_unlock(ssp, idx); 422 } 423 424 /** 425 * smp_mb__after_srcu_read_unlock - ensure full ordering after srcu_read_unlock 426 * 427 * Converts the preceding srcu_read_unlock into a two-way memory barrier. 428 * 429 * Call this after srcu_read_unlock, to guarantee that all memory operations 430 * that occur after smp_mb__after_srcu_read_unlock will appear to happen after 431 * the preceding srcu_read_unlock. 432 */ 433 static inline void smp_mb__after_srcu_read_unlock(void) 434 { 435 /* __srcu_read_unlock has smp_mb() internally so nothing to do here. */ 436 } 437 438 /** 439 * smp_mb__after_srcu_read_lock - ensure full ordering after srcu_read_lock 440 * 441 * Converts the preceding srcu_read_lock into a two-way memory barrier. 442 * 443 * Call this after srcu_read_lock, to guarantee that all memory operations 444 * that occur after smp_mb__after_srcu_read_lock will appear to happen after 445 * the preceding srcu_read_lock. 446 */ 447 static inline void smp_mb__after_srcu_read_lock(void) 448 { 449 /* __srcu_read_lock has smp_mb() internally so nothing to do here. */ 450 } 451 452 DEFINE_LOCK_GUARD_1(srcu, struct srcu_struct, 453 _T->idx = srcu_read_lock(_T->lock), 454 srcu_read_unlock(_T->lock, _T->idx), 455 int idx) 456 457 #endif 458