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