Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 2 | #ifndef _LINUX_PERCPU_RWSEM_H |
| 3 | #define _LINUX_PERCPU_RWSEM_H |
| 4 | |
Oleg Nesterov | 9390ef0 | 2012-12-17 16:01:36 -0800 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 6 | #include <linux/percpu.h> |
Davidlohr Bueso | 52b9412 | 2017-01-11 07:22:26 -0800 | [diff] [blame] | 7 | #include <linux/rcuwait.h> |
Peter Zijlstra | 7f26482 | 2019-10-30 20:30:41 +0100 | [diff] [blame] | 8 | #include <linux/wait.h> |
Oleg Nesterov | 001dac6 | 2015-08-21 19:42:57 +0200 | [diff] [blame] | 9 | #include <linux/rcu_sync.h> |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 10 | #include <linux/lockdep.h> |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 11 | |
| 12 | struct percpu_rw_semaphore { |
Oleg Nesterov | 001dac6 | 2015-08-21 19:42:57 +0200 | [diff] [blame] | 13 | struct rcu_sync rss; |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 14 | unsigned int __percpu *read_count; |
Peter Zijlstra | 7f26482 | 2019-10-30 20:30:41 +0100 | [diff] [blame] | 15 | struct rcuwait writer; |
| 16 | wait_queue_head_t waiters; |
| 17 | atomic_t block; |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 18 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 19 | struct lockdep_map dep_map; |
| 20 | #endif |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 23 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 24 | #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }, |
| 25 | #else |
| 26 | #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) |
| 27 | #endif |
| 28 | |
Oleg Nesterov | 3f2947b | 2019-04-23 18:32:41 +0200 | [diff] [blame] | 29 | #define __DEFINE_PERCPU_RWSEM(name, is_static) \ |
Peter Zijlstra | 11d9684 | 2015-06-22 14:16:31 +0200 | [diff] [blame] | 30 | static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \ |
Oleg Nesterov | 3f2947b | 2019-04-23 18:32:41 +0200 | [diff] [blame] | 31 | is_static struct percpu_rw_semaphore name = { \ |
Oleg Nesterov | 95bf33b | 2019-04-23 14:07:24 +0200 | [diff] [blame] | 32 | .rss = __RCU_SYNC_INITIALIZER(name.rss), \ |
Peter Zijlstra | 11d9684 | 2015-06-22 14:16:31 +0200 | [diff] [blame] | 33 | .read_count = &__percpu_rwsem_rc_##name, \ |
Davidlohr Bueso | 52b9412 | 2017-01-11 07:22:26 -0800 | [diff] [blame] | 34 | .writer = __RCUWAIT_INITIALIZER(name.writer), \ |
Peter Zijlstra | 7f26482 | 2019-10-30 20:30:41 +0100 | [diff] [blame] | 35 | .waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \ |
| 36 | .block = ATOMIC_INIT(0), \ |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 37 | __PERCPU_RWSEM_DEP_MAP_INIT(name) \ |
Peter Zijlstra | 11d9684 | 2015-06-22 14:16:31 +0200 | [diff] [blame] | 38 | } |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 39 | |
Oleg Nesterov | 3f2947b | 2019-04-23 18:32:41 +0200 | [diff] [blame] | 40 | #define DEFINE_PERCPU_RWSEM(name) \ |
| 41 | __DEFINE_PERCPU_RWSEM(name, /* not static */) |
| 42 | #define DEFINE_STATIC_PERCPU_RWSEM(name) \ |
| 43 | __DEFINE_PERCPU_RWSEM(name, static) |
Peter Zijlstra | 11d9684 | 2015-06-22 14:16:31 +0200 | [diff] [blame] | 44 | |
Peter Zijlstra | 206c98f | 2019-10-30 20:12:37 +0100 | [diff] [blame] | 45 | extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool); |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 46 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 47 | static inline void percpu_down_read(struct percpu_rw_semaphore *sem) |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 48 | { |
| 49 | might_sleep(); |
| 50 | |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 51 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 52 | |
| 53 | preempt_disable(); |
| 54 | /* |
| 55 | * We are in an RCU-sched read-side critical section, so the writer |
| 56 | * cannot both change sem->state from readers_fast and start checking |
| 57 | * counters while we are here. So if we see !sem->state, we know that |
| 58 | * the writer won't be checking until we're past the preempt_enable() |
Paul E. McKenney | e3e7405 | 2018-11-07 13:53:34 -0800 | [diff] [blame] | 59 | * and that once the synchronize_rcu() is done, the writer will see |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 60 | * anything we did within this RCU-sched read-size critical section. |
| 61 | */ |
Peter Zijlstra | 71365d4 | 2019-10-30 20:17:51 +0100 | [diff] [blame] | 62 | if (likely(rcu_sync_is_idle(&sem->rss))) |
Hou Tao | e6b1a44 | 2020-09-15 22:07:50 +0800 | [diff] [blame] | 63 | this_cpu_inc(*sem->read_count); |
Peter Zijlstra | 71365d4 | 2019-10-30 20:17:51 +0100 | [diff] [blame] | 64 | else |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 65 | __percpu_down_read(sem, false); /* Unconditional memory barrier */ |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 66 | /* |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 67 | * The preempt_enable() prevents the compiler from |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 68 | * bleeding the critical section out. |
| 69 | */ |
Peter Zijlstra | 259d69b | 2015-11-23 15:23:55 +0100 | [diff] [blame] | 70 | preempt_enable(); |
| 71 | } |
| 72 | |
Peter Zijlstra | 206c98f | 2019-10-30 20:12:37 +0100 | [diff] [blame] | 73 | static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem) |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 74 | { |
Peter Zijlstra | 206c98f | 2019-10-30 20:12:37 +0100 | [diff] [blame] | 75 | bool ret = true; |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 76 | |
| 77 | preempt_disable(); |
| 78 | /* |
| 79 | * Same as in percpu_down_read(). |
| 80 | */ |
Peter Zijlstra | 71365d4 | 2019-10-30 20:17:51 +0100 | [diff] [blame] | 81 | if (likely(rcu_sync_is_idle(&sem->rss))) |
Hou Tao | e6b1a44 | 2020-09-15 22:07:50 +0800 | [diff] [blame] | 82 | this_cpu_inc(*sem->read_count); |
Peter Zijlstra | 71365d4 | 2019-10-30 20:17:51 +0100 | [diff] [blame] | 83 | else |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 84 | ret = __percpu_down_read(sem, true); /* Unconditional memory barrier */ |
| 85 | preempt_enable(); |
| 86 | /* |
| 87 | * The barrier() from preempt_enable() prevents the compiler from |
| 88 | * bleeding the critical section out. |
| 89 | */ |
| 90 | |
| 91 | if (ret) |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 92 | rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 97 | static inline void percpu_up_read(struct percpu_rw_semaphore *sem) |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 98 | { |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 99 | rwsem_release(&sem->dep_map, _RET_IP_); |
| 100 | |
Peter Zijlstra | 02e525b2 | 2019-02-21 15:38:40 +0100 | [diff] [blame] | 101 | preempt_disable(); |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 102 | /* |
| 103 | * Same as in percpu_down_read(). |
| 104 | */ |
Davidlohr Bueso | ac8dec4 | 2019-11-18 15:19:35 -0800 | [diff] [blame] | 105 | if (likely(rcu_sync_is_idle(&sem->rss))) { |
Hou Tao | e6b1a44 | 2020-09-15 22:07:50 +0800 | [diff] [blame] | 106 | this_cpu_dec(*sem->read_count); |
Davidlohr Bueso | ac8dec4 | 2019-11-18 15:19:35 -0800 | [diff] [blame] | 107 | } else { |
| 108 | /* |
| 109 | * slowpath; reader will only ever wake a single blocked |
| 110 | * writer. |
| 111 | */ |
| 112 | smp_mb(); /* B matches C */ |
| 113 | /* |
| 114 | * In other words, if they see our decrement (presumably to |
| 115 | * aggregate zero, as that is the only time it matters) they |
| 116 | * will also see our critical section. |
| 117 | */ |
Hou Tao | e6b1a44 | 2020-09-15 22:07:50 +0800 | [diff] [blame] | 118 | this_cpu_dec(*sem->read_count); |
Davidlohr Bueso | ac8dec4 | 2019-11-18 15:19:35 -0800 | [diff] [blame] | 119 | rcuwait_wake_up(&sem->writer); |
| 120 | } |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 121 | preempt_enable(); |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 122 | } |
Mikulas Patocka | 5c1eabe | 2012-10-22 19:37:47 -0400 | [diff] [blame] | 123 | |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 124 | extern void percpu_down_write(struct percpu_rw_semaphore *); |
| 125 | extern void percpu_up_write(struct percpu_rw_semaphore *); |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 126 | |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 127 | extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, |
| 128 | const char *, struct lock_class_key *); |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 129 | |
Oleg Nesterov | a1fd3e2 | 2012-12-17 16:01:32 -0800 | [diff] [blame] | 130 | extern void percpu_free_rwsem(struct percpu_rw_semaphore *); |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 131 | |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 132 | #define percpu_init_rwsem(sem) \ |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 133 | ({ \ |
| 134 | static struct lock_class_key rwsem_key; \ |
Peter Zijlstra | 80127a3 | 2016-07-14 20:08:46 +0200 | [diff] [blame] | 135 | __percpu_init_rwsem(sem, #sem, &rwsem_key); \ |
Oleg Nesterov | 8ebe347 | 2012-12-17 16:01:38 -0800 | [diff] [blame] | 136 | }) |
| 137 | |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 138 | #define percpu_rwsem_is_held(sem) lockdep_is_held(sem) |
| 139 | #define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem) |
Peter Zijlstra | 11d9684 | 2015-06-22 14:16:31 +0200 | [diff] [blame] | 140 | |
Oleg Nesterov | 55cc15650 | 2015-07-21 20:26:44 +0200 | [diff] [blame] | 141 | static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, |
| 142 | bool read, unsigned long ip) |
| 143 | { |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 144 | lock_release(&sem->dep_map, ip); |
Oleg Nesterov | 55cc15650 | 2015-07-21 20:26:44 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem, |
| 148 | bool read, unsigned long ip) |
| 149 | { |
Peter Zijlstra | 1751060 | 2019-10-30 20:01:26 +0100 | [diff] [blame] | 150 | lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip); |
Oleg Nesterov | 55cc15650 | 2015-07-21 20:26:44 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Mikulas Patocka | 62ac665 | 2012-09-26 07:46:43 +0200 | [diff] [blame] | 153 | #endif |