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 19621934eeSPaul E. McKenney * 20621934eeSPaul E. McKenney * Author: Paul McKenney <[email protected]> 21621934eeSPaul E. McKenney * 22621934eeSPaul E. McKenney * For detailed explanation of Read-Copy Update mechanism see - 23621934eeSPaul E. McKenney * Documentation/RCU/ *.txt 24621934eeSPaul E. McKenney * 25621934eeSPaul E. McKenney */ 26621934eeSPaul E. McKenney 27eabc0694SAlan Stern #ifndef _LINUX_SRCU_H 28eabc0694SAlan Stern #define _LINUX_SRCU_H 29eabc0694SAlan Stern 30d14aada8SPaul E. McKenney #include <linux/mutex.h> 31ff195cb6SPaul E. McKenney #include <linux/rcupdate.h> 32d14aada8SPaul E. McKenney 33621934eeSPaul E. McKenney struct srcu_struct_array { 34621934eeSPaul E. McKenney int c[2]; 35621934eeSPaul E. McKenney }; 36621934eeSPaul E. McKenney 37621934eeSPaul E. McKenney struct srcu_struct { 38621934eeSPaul E. McKenney int completed; 3943cf38ebSTejun Heo struct srcu_struct_array __percpu *per_cpu_ref; 40621934eeSPaul E. McKenney struct mutex mutex; 41632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 42632ee200SPaul E. McKenney struct lockdep_map dep_map; 43632ee200SPaul E. McKenney #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 44621934eeSPaul E. McKenney }; 45621934eeSPaul E. McKenney 46621934eeSPaul E. McKenney #ifndef CONFIG_PREEMPT 47621934eeSPaul E. McKenney #define srcu_barrier() barrier() 48621934eeSPaul E. McKenney #else /* #ifndef CONFIG_PREEMPT */ 49621934eeSPaul E. McKenney #define srcu_barrier() 50621934eeSPaul E. McKenney #endif /* #else #ifndef CONFIG_PREEMPT */ 51621934eeSPaul E. McKenney 52632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 53632ee200SPaul E. McKenney 54632ee200SPaul E. McKenney int __init_srcu_struct(struct srcu_struct *sp, const char *name, 55632ee200SPaul E. McKenney struct lock_class_key *key); 56632ee200SPaul E. McKenney 57632ee200SPaul E. McKenney #define init_srcu_struct(sp) \ 58632ee200SPaul E. McKenney ({ \ 59632ee200SPaul E. McKenney static struct lock_class_key __srcu_key; \ 60632ee200SPaul E. McKenney \ 61632ee200SPaul E. McKenney __init_srcu_struct((sp), #sp, &__srcu_key); \ 62632ee200SPaul E. McKenney }) 63632ee200SPaul E. McKenney 64632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 65632ee200SPaul E. McKenney 66e6a92013SAlan Stern int init_srcu_struct(struct srcu_struct *sp); 67632ee200SPaul E. McKenney 68632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 69632ee200SPaul E. McKenney 70621934eeSPaul E. McKenney void cleanup_srcu_struct(struct srcu_struct *sp); 71632ee200SPaul E. McKenney int __srcu_read_lock(struct srcu_struct *sp) __acquires(sp); 72632ee200SPaul E. McKenney void __srcu_read_unlock(struct srcu_struct *sp, int idx) __releases(sp); 73621934eeSPaul E. McKenney void synchronize_srcu(struct srcu_struct *sp); 740cd397d3SPaul E. McKenney void synchronize_srcu_expedited(struct srcu_struct *sp); 75621934eeSPaul E. McKenney long srcu_batches_completed(struct srcu_struct *sp); 76eabc0694SAlan Stern 77632ee200SPaul E. McKenney #ifdef CONFIG_DEBUG_LOCK_ALLOC 78632ee200SPaul E. McKenney 79632ee200SPaul E. McKenney /** 80632ee200SPaul E. McKenney * srcu_read_lock_held - might we be in SRCU read-side critical section? 81632ee200SPaul E. McKenney * 82d20200b5SPaul E. McKenney * If CONFIG_DEBUG_LOCK_ALLOC is selected, returns nonzero iff in an SRCU 83d20200b5SPaul E. McKenney * read-side critical section. In absence of CONFIG_DEBUG_LOCK_ALLOC, 84632ee200SPaul E. McKenney * this assumes we are in an SRCU read-side critical section unless it can 85632ee200SPaul E. McKenney * prove otherwise. 86ff195cb6SPaul E. McKenney * 87867f236bSPaul E. McKenney * Checks debug_lockdep_rcu_enabled() to prevent false positives during boot 88867f236bSPaul E. McKenney * and while lockdep is disabled. 89867f236bSPaul E. McKenney * 90ff195cb6SPaul E. McKenney * Note that if the CPU is in the idle loop from an RCU point of view 91ff195cb6SPaul E. McKenney * (ie: that we are in the section between rcu_idle_enter() and 92ff195cb6SPaul E. McKenney * rcu_idle_exit()) then srcu_read_lock_held() returns false even if 93ff195cb6SPaul E. McKenney * the CPU did an srcu_read_lock(). The reason for this is that RCU 94ff195cb6SPaul E. McKenney * ignores CPUs that are in such a section, considering these as in 95ff195cb6SPaul E. McKenney * extended quiescent state, so such a CPU is effectively never in an 96ff195cb6SPaul E. McKenney * RCU read-side critical section regardless of what RCU primitives it 97ff195cb6SPaul E. McKenney * invokes. This state of affairs is required --- we need to keep an 98ff195cb6SPaul E. McKenney * RCU-free window in idle where the CPU may possibly enter into low 99ff195cb6SPaul E. McKenney * power mode. This way we can notice an extended quiescent state to 100ff195cb6SPaul E. McKenney * other CPUs that started a grace period. Otherwise we would delay any 101ff195cb6SPaul E. McKenney * grace period as long as we run in the idle task. 102c0d6d01bSPaul E. McKenney * 103c0d6d01bSPaul E. McKenney * Similarly, we avoid claiming an SRCU read lock held if the current 104c0d6d01bSPaul E. McKenney * CPU is offline. 105632ee200SPaul E. McKenney */ 106632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp) 107632ee200SPaul E. McKenney { 108867f236bSPaul E. McKenney if (!debug_lockdep_rcu_enabled()) 109632ee200SPaul E. McKenney return 1; 110c0d6d01bSPaul E. McKenney if (rcu_is_cpu_idle()) 111c0d6d01bSPaul E. McKenney return 0; 112c0d6d01bSPaul E. McKenney if (!rcu_lockdep_current_cpu_online()) 113c0d6d01bSPaul E. McKenney return 0; 114ff195cb6SPaul E. McKenney return lock_is_held(&sp->dep_map); 115632ee200SPaul E. McKenney } 116632ee200SPaul E. McKenney 117632ee200SPaul E. McKenney #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 118632ee200SPaul E. McKenney 119632ee200SPaul E. McKenney static inline int srcu_read_lock_held(struct srcu_struct *sp) 120632ee200SPaul E. McKenney { 121632ee200SPaul E. McKenney return 1; 122632ee200SPaul E. McKenney } 123632ee200SPaul E. McKenney 124632ee200SPaul E. McKenney #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */ 125632ee200SPaul E. McKenney 126632ee200SPaul E. McKenney /** 127ca5ecddfSPaul E. McKenney * srcu_dereference_check - fetch SRCU-protected pointer for later dereferencing 128ca5ecddfSPaul E. McKenney * @p: the pointer to fetch and protect for later dereferencing 129ca5ecddfSPaul E. McKenney * @sp: pointer to the srcu_struct, which is used to check that we 130ca5ecddfSPaul E. McKenney * really are in an SRCU read-side critical section. 131ca5ecddfSPaul E. McKenney * @c: condition to check for update-side use 132c26d34a5SPaul E. McKenney * 133ca5ecddfSPaul E. McKenney * If PROVE_RCU is enabled, invoking this outside of an RCU read-side 134ca5ecddfSPaul E. McKenney * critical section will result in an RCU-lockdep splat, unless @c evaluates 135ca5ecddfSPaul E. McKenney * to 1. The @c argument will normally be a logical expression containing 136ca5ecddfSPaul E. McKenney * lockdep_is_held() calls. 137c26d34a5SPaul E. McKenney */ 138ca5ecddfSPaul E. McKenney #define srcu_dereference_check(p, sp, c) \ 139ca5ecddfSPaul E. McKenney __rcu_dereference_check((p), srcu_read_lock_held(sp) || (c), __rcu) 140ca5ecddfSPaul E. McKenney 141ca5ecddfSPaul E. McKenney /** 142ca5ecddfSPaul E. McKenney * srcu_dereference - fetch SRCU-protected pointer for later dereferencing 143ca5ecddfSPaul E. McKenney * @p: the pointer to fetch and protect for later dereferencing 144ca5ecddfSPaul E. McKenney * @sp: pointer to the srcu_struct, which is used to check that we 145ca5ecddfSPaul E. McKenney * really are in an SRCU read-side critical section. 146ca5ecddfSPaul E. McKenney * 147ca5ecddfSPaul E. McKenney * Makes rcu_dereference_check() do the dirty work. If PROVE_RCU 148ca5ecddfSPaul E. McKenney * is enabled, invoking this outside of an RCU read-side critical 149ca5ecddfSPaul E. McKenney * section will result in an RCU-lockdep splat. 150ca5ecddfSPaul E. McKenney */ 151ca5ecddfSPaul E. McKenney #define srcu_dereference(p, sp) srcu_dereference_check((p), (sp), 0) 152c26d34a5SPaul E. McKenney 153c26d34a5SPaul E. McKenney /** 154632ee200SPaul E. McKenney * srcu_read_lock - register a new reader for an SRCU-protected structure. 155632ee200SPaul E. McKenney * @sp: srcu_struct in which to register the new reader. 156632ee200SPaul E. McKenney * 157632ee200SPaul E. McKenney * Enter an SRCU read-side critical section. Note that SRCU read-side 15873d4da4dSPaul E. McKenney * critical sections may be nested. However, it is illegal to 15973d4da4dSPaul E. McKenney * call anything that waits on an SRCU grace period for the same 16073d4da4dSPaul E. McKenney * srcu_struct, whether directly or indirectly. Please note that 16173d4da4dSPaul E. McKenney * one way to indirectly wait on an SRCU grace period is to acquire 16273d4da4dSPaul E. McKenney * a mutex that is held elsewhere while calling synchronize_srcu() or 16373d4da4dSPaul E. McKenney * synchronize_srcu_expedited(). 1643842a083SPaul E. McKenney * 1653842a083SPaul E. McKenney * Note that srcu_read_lock() and the matching srcu_read_unlock() must 1663842a083SPaul E. McKenney * occur in the same context, for example, it is illegal to invoke 1673842a083SPaul E. McKenney * srcu_read_unlock() in an irq handler if the matching srcu_read_lock() 1683842a083SPaul E. McKenney * was invoked in process context. 169632ee200SPaul E. McKenney */ 170632ee200SPaul E. McKenney static inline int srcu_read_lock(struct srcu_struct *sp) __acquires(sp) 171632ee200SPaul E. McKenney { 172632ee200SPaul E. McKenney int retval = __srcu_read_lock(sp); 173632ee200SPaul E. McKenney 174ff195cb6SPaul E. McKenney rcu_lock_acquire(&(sp)->dep_map); 175*bde23c68SHeiko Carstens rcu_lockdep_assert(!rcu_is_cpu_idle(), 176*bde23c68SHeiko Carstens "srcu_read_lock() used illegally while idle"); 177632ee200SPaul E. McKenney return retval; 178632ee200SPaul E. McKenney } 179632ee200SPaul E. McKenney 180632ee200SPaul E. McKenney /** 181632ee200SPaul E. McKenney * srcu_read_unlock - unregister a old reader from an SRCU-protected structure. 182632ee200SPaul E. McKenney * @sp: srcu_struct in which to unregister the old reader. 183632ee200SPaul E. McKenney * @idx: return value from corresponding srcu_read_lock(). 184632ee200SPaul E. McKenney * 185632ee200SPaul E. McKenney * Exit an SRCU read-side critical section. 186632ee200SPaul E. McKenney */ 187632ee200SPaul E. McKenney static inline void srcu_read_unlock(struct srcu_struct *sp, int idx) 188632ee200SPaul E. McKenney __releases(sp) 189632ee200SPaul E. McKenney { 190*bde23c68SHeiko Carstens rcu_lockdep_assert(!rcu_is_cpu_idle(), 191*bde23c68SHeiko Carstens "srcu_read_unlock() used illegally while idle"); 192ff195cb6SPaul E. McKenney rcu_lock_release(&(sp)->dep_map); 193632ee200SPaul E. McKenney __srcu_read_unlock(sp, idx); 194632ee200SPaul E. McKenney } 195632ee200SPaul E. McKenney 1960c53dd8bSPaul E. McKenney /** 1970c53dd8bSPaul E. McKenney * srcu_read_lock_raw - register a new reader for an SRCU-protected structure. 1980c53dd8bSPaul E. McKenney * @sp: srcu_struct in which to register the new reader. 1990c53dd8bSPaul E. McKenney * 2000c53dd8bSPaul E. McKenney * Enter an SRCU read-side critical section. Similar to srcu_read_lock(), 2010c53dd8bSPaul E. McKenney * but avoids the RCU-lockdep checking. This means that it is legal to 2020c53dd8bSPaul E. McKenney * use srcu_read_lock_raw() in one context, for example, in an exception 2030c53dd8bSPaul E. McKenney * handler, and then have the matching srcu_read_unlock_raw() in another 2040c53dd8bSPaul E. McKenney * context, for example in the task that took the exception. 2050c53dd8bSPaul E. McKenney * 2060c53dd8bSPaul E. McKenney * However, the entire SRCU read-side critical section must reside within a 2070c53dd8bSPaul E. McKenney * single task. For example, beware of using srcu_read_lock_raw() in 2080c53dd8bSPaul E. McKenney * a device interrupt handler and srcu_read_unlock() in the interrupted 2090c53dd8bSPaul E. McKenney * task: This will not work if interrupts are threaded. 2100c53dd8bSPaul E. McKenney */ 2110c53dd8bSPaul E. McKenney static inline int srcu_read_lock_raw(struct srcu_struct *sp) 2120c53dd8bSPaul E. McKenney { 2130c53dd8bSPaul E. McKenney unsigned long flags; 2140c53dd8bSPaul E. McKenney int ret; 2150c53dd8bSPaul E. McKenney 2160c53dd8bSPaul E. McKenney local_irq_save(flags); 2170c53dd8bSPaul E. McKenney ret = __srcu_read_lock(sp); 2180c53dd8bSPaul E. McKenney local_irq_restore(flags); 2190c53dd8bSPaul E. McKenney return ret; 2200c53dd8bSPaul E. McKenney } 2210c53dd8bSPaul E. McKenney 2220c53dd8bSPaul E. McKenney /** 2230c53dd8bSPaul E. McKenney * srcu_read_unlock_raw - unregister reader from an SRCU-protected structure. 2240c53dd8bSPaul E. McKenney * @sp: srcu_struct in which to unregister the old reader. 2250c53dd8bSPaul E. McKenney * @idx: return value from corresponding srcu_read_lock_raw(). 2260c53dd8bSPaul E. McKenney * 2270c53dd8bSPaul E. McKenney * Exit an SRCU read-side critical section without lockdep-RCU checking. 2280c53dd8bSPaul E. McKenney * See srcu_read_lock_raw() for more details. 2290c53dd8bSPaul E. McKenney */ 2300c53dd8bSPaul E. McKenney static inline void srcu_read_unlock_raw(struct srcu_struct *sp, int idx) 2310c53dd8bSPaul E. McKenney { 2320c53dd8bSPaul E. McKenney unsigned long flags; 2330c53dd8bSPaul E. McKenney 2340c53dd8bSPaul E. McKenney local_irq_save(flags); 2350c53dd8bSPaul E. McKenney __srcu_read_unlock(sp, idx); 2360c53dd8bSPaul E. McKenney local_irq_restore(flags); 2370c53dd8bSPaul E. McKenney } 2380c53dd8bSPaul E. McKenney 239eabc0694SAlan Stern #endif 240