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