1621934eeSPaul E. McKenney /* 2621934eeSPaul E. McKenney * Sleepable Read-Copy Update mechanism for mutual exclusion 3621934eeSPaul E. McKenney * 4621934eeSPaul E. McKenney * This program is free software; you can redistribute it and/or modify 5621934eeSPaul E. McKenney * it under the terms of the GNU General Public License as published by 6621934eeSPaul E. McKenney * the Free Software Foundation; either version 2 of the License, or 7621934eeSPaul E. McKenney * (at your option) any later version. 8621934eeSPaul E. McKenney * 9621934eeSPaul E. McKenney * This program is distributed in the hope that it will be useful, 10621934eeSPaul E. McKenney * but WITHOUT ANY WARRANTY; without even the implied warranty of 11621934eeSPaul E. McKenney * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12621934eeSPaul E. McKenney * GNU General Public License for more details. 13621934eeSPaul E. McKenney * 14621934eeSPaul E. McKenney * You should have received a copy of the GNU General Public License 15621934eeSPaul E. McKenney * along with this program; if not, write to the Free Software 16621934eeSPaul E. McKenney * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17621934eeSPaul E. McKenney * 18621934eeSPaul E. McKenney * Copyright (C) IBM Corporation, 2006 19*4e87b2d7SLai Jiangshan * Copyright (C) Fujitsu, 2012 20621934eeSPaul E. McKenney * 21621934eeSPaul E. McKenney * Author: Paul McKenney <[email protected]> 22*4e87b2d7SLai Jiangshan * Lai Jiangshan <[email protected]> 23621934eeSPaul E. McKenney * 24621934eeSPaul E. McKenney * For detailed explanation of Read-Copy Update mechanism see - 25621934eeSPaul E. McKenney * Documentation/RCU/ *.txt 26621934eeSPaul E. McKenney * 27621934eeSPaul E. McKenney */ 28621934eeSPaul E. McKenney 29eabc0694SAlan Stern #ifndef _LINUX_SRCU_H 30eabc0694SAlan Stern #define _LINUX_SRCU_H 31eabc0694SAlan Stern 32d14aada8SPaul E. McKenney #include <linux/mutex.h> 33ff195cb6SPaul E. McKenney #include <linux/rcupdate.h> 34931ea9d1SLai Jiangshan #include <linux/workqueue.h> 35d14aada8SPaul E. McKenney 36621934eeSPaul E. McKenney struct srcu_struct_array { 37cef50120SPaul E. McKenney unsigned long c[2]; 38b52ce066SLai Jiangshan unsigned long seq[2]; 39621934eeSPaul E. McKenney }; 40621934eeSPaul E. McKenney 41931ea9d1SLai Jiangshan struct rcu_batch { 42931ea9d1SLai Jiangshan struct rcu_head *head, **tail; 43931ea9d1SLai Jiangshan }; 44931ea9d1SLai Jiangshan 45621934eeSPaul E. McKenney struct srcu_struct { 46cef50120SPaul E. McKenney unsigned completed; 4743cf38ebSTejun Heo struct srcu_struct_array __percpu *per_cpu_ref; 48931ea9d1SLai Jiangshan spinlock_t queue_lock; /* protect ->batch_queue, ->running */ 49931ea9d1SLai Jiangshan bool running; 50931ea9d1SLai Jiangshan /* callbacks just queued */ 51931ea9d1SLai Jiangshan struct rcu_batch batch_queue; 52931ea9d1SLai Jiangshan /* callbacks try to do the first check_zero */ 53931ea9d1SLai Jiangshan struct rcu_batch batch_check0; 54931ea9d1SLai Jiangshan /* callbacks done with the first check_zero and the flip */ 55931ea9d1SLai Jiangshan struct rcu_batch batch_check1; 56931ea9d1SLai Jiangshan struct rcu_batch batch_done; 57931ea9d1SLai Jiangshan struct delayed_work work; 58632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 59632ee200SPaul E. McKenney struct lockdep_map dep_map; 60632ee200SPaul E. McKenney #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 61621934eeSPaul E. McKenney }; 62621934eeSPaul E. McKenney 63632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 64632ee200SPaul E. McKenney 65632ee200SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *sp, const char *name, 66632ee200SPaul E. McKenney struct lock_class_key *key); 67632ee200SPaul E. McKenney 68632ee200SPaul E. McKenney #define init_srcu_struct(sp) \ 69632ee200SPaul E. McKenney ({ \ 70632ee200SPaul E. McKenney static struct lock_class_key __srcu_key; \ 71632ee200SPaul E. McKenney \ 72632ee200SPaul E. McKenney __init_srcu_struct((sp), #sp, &__srcu_key); \ 73632ee200SPaul E. McKenney }) 74632ee200SPaul E. McKenney 75632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 76632ee200SPaul E. McKenney 77e6a92013SAlan Stern int init_srcu_struct(struct srcu_struct *sp); 78632ee200SPaul E. McKenney 79632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 80632ee200SPaul E. McKenney 81931ea9d1SLai Jiangshan /** 82931ea9d1SLai Jiangshan * call_srcu() - Queue a callback for invocation after an SRCU grace period 83931ea9d1SLai Jiangshan * @sp: srcu_struct in queue the callback 84931ea9d1SLai Jiangshan * @head: structure to be used for queueing the SRCU callback. 85931ea9d1SLai Jiangshan * @func: function to be invoked after the SRCU grace period 86931ea9d1SLai Jiangshan * 87931ea9d1SLai Jiangshan * The callback function will be invoked some time after a full SRCU 88931ea9d1SLai Jiangshan * grace period elapses, in other words after all pre-existing SRCU 89931ea9d1SLai Jiangshan * read-side critical sections have completed. However, the callback 90931ea9d1SLai Jiangshan * function might well execute concurrently with other SRCU read-side 91931ea9d1SLai Jiangshan * critical sections that started after call_srcu() was invoked. SRCU 92931ea9d1SLai Jiangshan * read-side critical sections are delimited by srcu_read_lock() and 93931ea9d1SLai Jiangshan * srcu_read_unlock(), and may be nested. 94931ea9d1SLai Jiangshan * 95931ea9d1SLai Jiangshan * The callback will be invoked from process context, but must nevertheless 96931ea9d1SLai Jiangshan * be fast and must not block. 97931ea9d1SLai Jiangshan */ 98931ea9d1SLai Jiangshan void call_srcu(struct srcu_struct *sp, struct rcu_head *head, 99931ea9d1SLai Jiangshan void (*func)(struct rcu_head *head)); 100931ea9d1SLai Jiangshan 101621934eeSPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *sp); 102632ee200SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *sp) __acquires(sp); 103632ee200SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *sp, int idx) __releases(sp); 104621934eeSPaul E. McKenney void synchronize_srcu(struct srcu_struct *sp); 1050cd397d3SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *sp); 106621934eeSPaul E. McKenney long srcu_batches_completed(struct srcu_struct *sp); 107931ea9d1SLai Jiangshan void srcu_barrier(struct srcu_struct *sp); 108eabc0694SAlan Stern 109632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 110632ee200SPaul E. McKenney 111632ee200SPaul E. McKenney /** 112632ee200SPaul E. McKenney * srcu_read_lock_held - might we be in SRCU read-side critical section? 113632ee200SPaul E. McKenney * 114d20200b5SPaul E. McKenney * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU 115d20200b5SPaul E. McKenney * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC, 116632ee200SPaul E. McKenney * this assumes we are in an SRCU read-side critical section unless it can 117632ee200SPaul E. McKenney * prove otherwise. 118ff195cb6SPaul E. McKenney * 119867f236bSPaul E. McKenney * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot 120867f236bSPaul E. McKenney * and while lockdep is disabled. 121867f236bSPaul E. McKenney * 122ff195cb6SPaul E. McKenney * Note that if the CPU is in the idle loop from an RCU point of view 123ff195cb6SPaul E. McKenney * (ie: that we are in the section between rcu_idle_enter() and 124ff195cb6SPaul E. McKenney * rcu_idle_exit()) then srcu_read_lock_held() returns false even if 125ff195cb6SPaul E. McKenney * the CPU did an srcu_read_lock(). The reason for this is that RCU 126ff195cb6SPaul E. McKenney * ignores CPUs that are in such a section, considering these as in 127ff195cb6SPaul E. McKenney * extended quiescent state, so such a CPU is effectively never in an 128ff195cb6SPaul E. McKenney * RCU read-side critical section regardless of what RCU primitives it 129ff195cb6SPaul E. McKenney * invokes. This state of affairs is required --- we need to keep an 130ff195cb6SPaul E. McKenney * RCU-free window in idle where the CPU may possibly enter into low 131ff195cb6SPaul E. McKenney * power mode. This way we can notice an extended quiescent state to 132ff195cb6SPaul E. McKenney * other CPUs that started a grace period. Otherwise we would delay any 133ff195cb6SPaul E. McKenney * grace period as long as we run in the idle task. 134c0d6d01bSPaul E. McKenney * 135c0d6d01bSPaul E. McKenney * Similarly, we avoid claiming an SRCU read lock held if the current 136c0d6d01bSPaul E. McKenney * CPU is offline. 137632ee200SPaul E. McKenney */ 138632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp) 139632ee200SPaul E. McKenney { 140867f236bSPaul E. McKenney if (!debug_lockdep_rcu_enabled()) 141632ee200SPaul E. McKenney return 1; 142c0d6d01bSPaul E. McKenney if (rcu_is_cpu_idle()) 143c0d6d01bSPaul E. McKenney return 0; 144c0d6d01bSPaul E. McKenney if (!rcu_lockdep_current_cpu_online()) 145c0d6d01bSPaul E. McKenney return 0; 146ff195cb6SPaul E. McKenney return lock_is_held(&sp->dep_map); 147632ee200SPaul E. McKenney } 148632ee200SPaul E. McKenney 149632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 150632ee200SPaul E. McKenney 151632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp) 152632ee200SPaul E. McKenney { 153632ee200SPaul E. McKenney return 1; 154632ee200SPaul E. McKenney } 155632ee200SPaul E. McKenney 156632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 157632ee200SPaul E. McKenney 158632ee200SPaul E. McKenney /** 159ca5ecddfSPaul E. McKenney * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing 160ca5ecddfSPaul E. McKenney * @p: the pointer to fetch and protect for later dereferencing 161ca5ecddfSPaul E. McKenney * @sp: pointer to the srcu_struct, which is used to check that we 162ca5ecddfSPaul E. McKenney * really are in an SRCU read-side critical section. 163ca5ecddfSPaul E. McKenney * @c: condition to check for update-side use 164c26d34a5SPaul E. McKenney * 165ca5ecddfSPaul E. McKenney * If PROVE_RCU is enabled, invoking this outside of an RCU read-side 166ca5ecddfSPaul E. McKenney * critical section will result in an RCU-lockdep splat, unless @c evaluates 167ca5ecddfSPaul E. McKenney * to 1. The @c argument will normally be a logical expression containing 168ca5ecddfSPaul E. McKenney * lockdep_is_held() calls. 169c26d34a5SPaul E. McKenney */ 170ca5ecddfSPaul E. McKenney #define srcu_dereference_check(p, sp, c) \ 171ca5ecddfSPaul E. McKenney __rcu_dereference_check((p), srcu_read_lock_held(sp) || (c), __rcu) 172ca5ecddfSPaul E. McKenney 173ca5ecddfSPaul E. McKenney /** 174ca5ecddfSPaul E. McKenney * srcu_dereference - fetch SRCU-protected pointer for later dereferencing 175ca5ecddfSPaul E. McKenney * @p: the pointer to fetch and protect for later dereferencing 176ca5ecddfSPaul E. McKenney * @sp: pointer to the srcu_struct, which is used to check that we 177ca5ecddfSPaul E. McKenney * really are in an SRCU read-side critical section. 178ca5ecddfSPaul E. McKenney * 179ca5ecddfSPaul E. McKenney * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU 180ca5ecddfSPaul E. McKenney * is enabled, invoking this outside of an RCU read-side critical 181ca5ecddfSPaul E. McKenney * section will result in an RCU-lockdep splat. 182ca5ecddfSPaul E. McKenney */ 183ca5ecddfSPaul E. McKenney #define srcu_dereference(p, sp) srcu_dereference_check((p), (sp), 0) 184c26d34a5SPaul E. McKenney 185c26d34a5SPaul E. McKenney /** 186632ee200SPaul E. McKenney * srcu_read_lock - register a new reader for an SRCU-protected structure. 187632ee200SPaul E. McKenney * @sp: srcu_struct in which to register the new reader. 188632ee200SPaul E. McKenney * 189632ee200SPaul E. McKenney * Enter an SRCU read-side critical section. Note that SRCU read-side 19073d4da4dSPaul E. McKenney * critical sections may be nested. However, it is illegal to 19173d4da4dSPaul E. McKenney * call anything that waits on an SRCU grace period for the same 19273d4da4dSPaul E. McKenney * srcu_struct, whether directly or indirectly. Please note that 19373d4da4dSPaul E. McKenney * one way to indirectly wait on an SRCU grace period is to acquire 19473d4da4dSPaul E. McKenney * a mutex that is held elsewhere while calling synchronize_srcu() or 19573d4da4dSPaul E. McKenney * synchronize_srcu_expedited(). 1963842a083SPaul E. McKenney * 1973842a083SPaul E. McKenney * Note that srcu_read_lock() and the matching srcu_read_unlock() must 1983842a083SPaul E. McKenney * occur in the same context, for example, it is illegal to invoke 1993842a083SPaul E. McKenney * srcu_read_unlock() in an irq handler if the matching srcu_read_lock() 2003842a083SPaul E. McKenney * was invoked in process context. 201632ee200SPaul E. McKenney */ 202632ee200SPaul E. McKenney static inline int srcu_read_lock(struct srcu_struct *sp) __acquires(sp) 203632ee200SPaul E. McKenney { 204632ee200SPaul E. McKenney int retval = __srcu_read_lock(sp); 205632ee200SPaul E. McKenney 206ff195cb6SPaul E. McKenney rcu_lock_acquire(&(sp)->dep_map); 207bde23c68SHeiko Carstens rcu_lockdep_assert(!rcu_is_cpu_idle(), 208bde23c68SHeiko Carstens "srcu_read_lock() used illegally while idle"); 209632ee200SPaul E. McKenney return retval; 210632ee200SPaul E. McKenney } 211632ee200SPaul E. McKenney 212632ee200SPaul E. McKenney /** 213632ee200SPaul E. McKenney * srcu_read_unlock - unregister a old reader from an SRCU-protected structure. 214632ee200SPaul E. McKenney * @sp: srcu_struct in which to unregister the old reader. 215632ee200SPaul E. McKenney * @idx: return value from corresponding srcu_read_lock(). 216632ee200SPaul E. McKenney * 217632ee200SPaul E. McKenney * Exit an SRCU read-side critical section. 218632ee200SPaul E. McKenney */ 219632ee200SPaul E. McKenney static inline void srcu_read_unlock(struct srcu_struct *sp, int idx) 220632ee200SPaul E. McKenney __releases(sp) 221632ee200SPaul E. McKenney { 222bde23c68SHeiko Carstens rcu_lockdep_assert(!rcu_is_cpu_idle(), 223bde23c68SHeiko Carstens "srcu_read_unlock() used illegally while idle"); 224ff195cb6SPaul E. McKenney rcu_lock_release(&(sp)->dep_map); 225632ee200SPaul E. McKenney __srcu_read_unlock(sp, idx); 226632ee200SPaul E. McKenney } 227632ee200SPaul E. McKenney 2280c53dd8bSPaul E. McKenney /** 2290c53dd8bSPaul E. McKenney * srcu_read_lock_raw - register a new reader for an SRCU-protected structure. 2300c53dd8bSPaul E. McKenney * @sp: srcu_struct in which to register the new reader. 2310c53dd8bSPaul E. McKenney * 2320c53dd8bSPaul E. McKenney * Enter an SRCU read-side critical section. Similar to srcu_read_lock(), 2330c53dd8bSPaul E. McKenney * but avoids the RCU-lockdep checking. This means that it is legal to 2340c53dd8bSPaul E. McKenney * use srcu_read_lock_raw() in one context, for example, in an exception 2350c53dd8bSPaul E. McKenney * handler, and then have the matching srcu_read_unlock_raw() in another 2360c53dd8bSPaul E. McKenney * context, for example in the task that took the exception. 2370c53dd8bSPaul E. McKenney * 2380c53dd8bSPaul E. McKenney * However, the entire SRCU read-side critical section must reside within a 2390c53dd8bSPaul E. McKenney * single task. For example, beware of using srcu_read_lock_raw() in 2400c53dd8bSPaul E. McKenney * a device interrupt handler and srcu_read_unlock() in the interrupted 2410c53dd8bSPaul E. McKenney * task: This will not work if interrupts are threaded. 2420c53dd8bSPaul E. McKenney */ 2430c53dd8bSPaul E. McKenney static inline int srcu_read_lock_raw(struct srcu_struct *sp) 2440c53dd8bSPaul E. McKenney { 2450c53dd8bSPaul E. McKenney unsigned long flags; 2460c53dd8bSPaul E. McKenney int ret; 2470c53dd8bSPaul E. McKenney 2480c53dd8bSPaul E. McKenney local_irq_save(flags); 2490c53dd8bSPaul E. McKenney ret = __srcu_read_lock(sp); 2500c53dd8bSPaul E. McKenney local_irq_restore(flags); 2510c53dd8bSPaul E. McKenney return ret; 2520c53dd8bSPaul E. McKenney } 2530c53dd8bSPaul E. McKenney 2540c53dd8bSPaul E. McKenney /** 2550c53dd8bSPaul E. McKenney * srcu_read_unlock_raw - unregister reader from an SRCU-protected structure. 2560c53dd8bSPaul E. McKenney * @sp: srcu_struct in which to unregister the old reader. 2570c53dd8bSPaul E. McKenney * @idx: return value from corresponding srcu_read_lock_raw(). 2580c53dd8bSPaul E. McKenney * 2590c53dd8bSPaul E. McKenney * Exit an SRCU read-side critical section without lockdep-RCU checking. 2600c53dd8bSPaul E. McKenney * See srcu_read_lock_raw() for more details. 2610c53dd8bSPaul E. McKenney */ 2620c53dd8bSPaul E. McKenney static inline void srcu_read_unlock_raw(struct srcu_struct *sp, int idx) 2630c53dd8bSPaul E. McKenney { 2640c53dd8bSPaul E. McKenney unsigned long flags; 2650c53dd8bSPaul E. McKenney 2660c53dd8bSPaul E. McKenney local_irq_save(flags); 2670c53dd8bSPaul E. McKenney __srcu_read_unlock(sp, idx); 2680c53dd8bSPaul E. McKenney local_irq_restore(flags); 2690c53dd8bSPaul E. McKenney } 2700c53dd8bSPaul E. McKenney 271eabc0694SAlan Stern #endif 272