Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* rwsem.h: R/W semaphores, public interface |
| 2 | * |
| 3 | * Written by David Howells (dhowells@redhat.com). |
| 4 | * Derived from asm-i386/semaphore.h |
| 5 | */ |
| 6 | |
| 7 | #ifndef _LINUX_RWSEM_H |
| 8 | #define _LINUX_RWSEM_H |
| 9 | |
| 10 | #include <linux/linkage.h> |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
Thomas Gleixner | c16a87c | 2011-01-26 20:05:50 +0000 | [diff] [blame] | 14 | #include <linux/list.h> |
| 15 | #include <linux/spinlock.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 16 | #include <linux/atomic.h> |
Michal Hocko | d479960 | 2016-04-07 17:12:26 +0200 | [diff] [blame] | 17 | #include <linux/err.h> |
Davidlohr Bueso | 5db6c6f | 2014-07-11 14:00:06 -0700 | [diff] [blame] | 18 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
Jason Low | 9063182 | 2014-07-14 10:27:49 -0700 | [diff] [blame] | 19 | #include <linux/osq_lock.h> |
Davidlohr Bueso | 5db6c6f | 2014-07-11 14:00:06 -0700 | [diff] [blame] | 20 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | struct rw_semaphore; |
| 23 | |
| 24 | #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK |
| 25 | #include <linux/rwsem-spinlock.h> /* use a generic implementation */ |
Jason Low | 8ee62b1 | 2016-06-03 22:26:02 -0700 | [diff] [blame] | 26 | #define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #else |
Thomas Gleixner | 1c8ed64 | 2011-01-26 20:05:56 +0000 | [diff] [blame] | 28 | /* All arch specific implementations share the same struct */ |
| 29 | struct rw_semaphore { |
Jason Low | 8ee62b1 | 2016-06-03 22:26:02 -0700 | [diff] [blame] | 30 | atomic_long_t count; |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 31 | struct list_head wait_list; |
Jason Low | ce069fc | 2014-07-14 10:27:52 -0700 | [diff] [blame] | 32 | raw_spinlock_t wait_lock; |
Davidlohr Bueso | 5db6c6f | 2014-07-11 14:00:06 -0700 | [diff] [blame] | 33 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
Jason Low | ce069fc | 2014-07-14 10:27:52 -0700 | [diff] [blame] | 34 | struct optimistic_spin_queue osq; /* spinner MCS lock */ |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 35 | /* |
| 36 | * Write owner. Used as a speculative check to see |
| 37 | * if the owner is running on the cpu. |
| 38 | */ |
| 39 | struct task_struct *owner; |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 40 | #endif |
Thomas Gleixner | 1c8ed64 | 2011-01-26 20:05:56 +0000 | [diff] [blame] | 41 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 42 | struct lockdep_map dep_map; |
| 43 | #endif |
| 44 | }; |
| 45 | |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 46 | extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem); |
| 47 | extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem); |
Michal Hocko | d479960 | 2016-04-07 17:12:26 +0200 | [diff] [blame] | 48 | extern struct rw_semaphore *rwsem_down_write_failed_killable(struct rw_semaphore *sem); |
Thomas Gleixner | d123375 | 2011-01-26 21:32:01 +0100 | [diff] [blame] | 49 | extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *); |
| 50 | extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem); |
Thomas Gleixner | aac7227 | 2011-01-26 20:06:06 +0000 | [diff] [blame] | 51 | |
Thomas Gleixner | 1c8ed64 | 2011-01-26 20:05:56 +0000 | [diff] [blame] | 52 | /* Include the arch specific part */ |
| 53 | #include <asm/rwsem.h> |
Thomas Gleixner | 41e5887 | 2011-01-26 20:06:03 +0000 | [diff] [blame] | 54 | |
| 55 | /* In all implementations count != 0 means locked */ |
| 56 | static inline int rwsem_is_locked(struct rw_semaphore *sem) |
| 57 | { |
Jason Low | 8ee62b1 | 2016-06-03 22:26:02 -0700 | [diff] [blame] | 58 | return atomic_long_read(&sem->count) != 0; |
Thomas Gleixner | 41e5887 | 2011-01-26 20:06:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Jason Low | 8ee62b1 | 2016-06-03 22:26:02 -0700 | [diff] [blame] | 61 | #define __RWSEM_INIT_COUNT(name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
Thomas Gleixner | 12249b3 | 2011-01-26 20:06:00 +0000 | [diff] [blame] | 64 | /* Common initializer macros and functions */ |
| 65 | |
| 66 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 67 | # define __RWSEM_DEP_MAP_INIT(lockname) , .dep_map = { .name = #lockname } |
| 68 | #else |
| 69 | # define __RWSEM_DEP_MAP_INIT(lockname) |
| 70 | #endif |
| 71 | |
Davidlohr Bueso | 5db6c6f | 2014-07-11 14:00:06 -0700 | [diff] [blame] | 72 | #ifdef CONFIG_RWSEM_SPIN_ON_OWNER |
Jason Low | ce069fc | 2014-07-14 10:27:52 -0700 | [diff] [blame] | 73 | #define __RWSEM_OPT_INIT(lockname) , .osq = OSQ_LOCK_UNLOCKED, .owner = NULL |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 74 | #else |
Jason Low | ce069fc | 2014-07-14 10:27:52 -0700 | [diff] [blame] | 75 | #define __RWSEM_OPT_INIT(lockname) |
Davidlohr Bueso | 4fc828e | 2014-05-02 11:24:15 -0700 | [diff] [blame] | 76 | #endif |
Thomas Gleixner | 12249b3 | 2011-01-26 20:06:00 +0000 | [diff] [blame] | 77 | |
Jason Low | ce069fc | 2014-07-14 10:27:52 -0700 | [diff] [blame] | 78 | #define __RWSEM_INITIALIZER(name) \ |
Jason Low | 8ee62b1 | 2016-06-03 22:26:02 -0700 | [diff] [blame] | 79 | { __RWSEM_INIT_COUNT(name), \ |
Jason Low | ce069fc | 2014-07-14 10:27:52 -0700 | [diff] [blame] | 80 | .wait_list = LIST_HEAD_INIT((name).wait_list), \ |
| 81 | .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock) \ |
| 82 | __RWSEM_OPT_INIT(name) \ |
| 83 | __RWSEM_DEP_MAP_INIT(name) } |
| 84 | |
Thomas Gleixner | 12249b3 | 2011-01-26 20:06:00 +0000 | [diff] [blame] | 85 | #define DECLARE_RWSEM(name) \ |
| 86 | struct rw_semaphore name = __RWSEM_INITIALIZER(name) |
| 87 | |
| 88 | extern void __init_rwsem(struct rw_semaphore *sem, const char *name, |
| 89 | struct lock_class_key *key); |
| 90 | |
| 91 | #define init_rwsem(sem) \ |
| 92 | do { \ |
| 93 | static struct lock_class_key __key; \ |
| 94 | \ |
| 95 | __init_rwsem((sem), #sem, &__key); \ |
| 96 | } while (0) |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
Josef Bacik | 4a444b1 | 2013-08-30 10:05:22 -0400 | [diff] [blame] | 99 | * This is the same regardless of which rwsem implementation that is being used. |
| 100 | * It is just a heuristic meant to be called by somebody alreadying holding the |
| 101 | * rwsem to see if somebody from an incompatible type is wanting access to the |
| 102 | * lock. |
| 103 | */ |
| 104 | static inline int rwsem_is_contended(struct rw_semaphore *sem) |
| 105 | { |
| 106 | return !list_empty(&sem->wait_list); |
| 107 | } |
| 108 | |
| 109 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | * lock for reading |
| 111 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 112 | extern void down_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * trylock for reading -- returns 1 if successful, 0 if contention |
| 116 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 117 | extern int down_read_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * lock for writing |
| 121 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 122 | extern void down_write(struct rw_semaphore *sem); |
Michal Hocko | 916633a | 2016-04-07 17:12:31 +0200 | [diff] [blame] | 123 | extern int __must_check down_write_killable(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * trylock for writing -- returns 1 if successful, 0 if contention |
| 127 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 128 | extern int down_write_trylock(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * release a read lock |
| 132 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 133 | extern void up_read(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * release a write lock |
| 137 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 138 | extern void up_write(struct rw_semaphore *sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * downgrade write lock to read lock |
| 142 | */ |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 143 | extern void downgrade_write(struct rw_semaphore *sem); |
| 144 | |
| 145 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 146 | /* |
Ingo Molnar | 5fca80e | 2006-07-10 04:44:02 -0700 | [diff] [blame] | 147 | * nested locking. NOTE: rwsems are not allowed to recurse |
| 148 | * (which occurs if the same task tries to acquire the same |
| 149 | * lock instance multiple times), but multiple locks of the |
| 150 | * same lock class might be taken, if the order of the locks |
| 151 | * is always the same. This ordering rule can be expressed |
| 152 | * to lockdep via the _nested() APIs, but enumerating the |
| 153 | * subclasses that are used. (If the nesting relationship is |
| 154 | * static then another method for expressing nested locking is |
| 155 | * the explicit definition of lock class keys and the use of |
| 156 | * lockdep_set_class() at lock initialization time. |
Davidlohr Bueso | 214e0ae | 2014-07-30 13:41:55 -0700 | [diff] [blame] | 157 | * See Documentation/locking/lockdep-design.txt for more details.) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 158 | */ |
| 159 | extern void down_read_nested(struct rw_semaphore *sem, int subclass); |
| 160 | extern void down_write_nested(struct rw_semaphore *sem, int subclass); |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 161 | extern int down_write_killable_nested(struct rw_semaphore *sem, int subclass); |
Jiri Kosina | 1b963c8 | 2013-01-11 14:31:56 -0800 | [diff] [blame] | 162 | extern void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest_lock); |
| 163 | |
| 164 | # define down_write_nest_lock(sem, nest_lock) \ |
| 165 | do { \ |
| 166 | typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \ |
| 167 | _down_write_nest_lock(sem, &(nest_lock)->dep_map); \ |
| 168 | } while (0); |
| 169 | |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 170 | /* |
| 171 | * Take/release a lock when not the owner will release it. |
| 172 | * |
| 173 | * [ This API should be avoided as much as possible - the |
| 174 | * proper abstraction for this case is completions. ] |
| 175 | */ |
| 176 | extern void down_read_non_owner(struct rw_semaphore *sem); |
| 177 | extern void up_read_non_owner(struct rw_semaphore *sem); |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 178 | #else |
| 179 | # define down_read_nested(sem, subclass) down_read(sem) |
Jiri Kosina | e65b9ad | 2013-01-15 20:12:37 +0100 | [diff] [blame] | 180 | # define down_write_nest_lock(sem, nest_lock) down_write(sem) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 181 | # define down_write_nested(sem, subclass) down_write(sem) |
Al Viro | 887bddf | 2016-05-26 00:04:58 -0400 | [diff] [blame] | 182 | # define down_write_killable_nested(sem, subclass) down_write_killable(sem) |
Kent Overstreet | 84759c6 | 2011-09-21 21:43:05 -0700 | [diff] [blame] | 183 | # define down_read_non_owner(sem) down_read(sem) |
| 184 | # define up_read_non_owner(sem) up_read(sem) |
Ingo Molnar | 4ea2176 | 2006-07-03 00:24:53 -0700 | [diff] [blame] | 185 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | #endif /* _LINUX_RWSEM_H */ |