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