Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
62ac665f MP |
2 | #ifndef _LINUX_PERCPU_RWSEM_H |
3 | #define _LINUX_PERCPU_RWSEM_H | |
4 | ||
9390ef0c | 5 | #include <linux/atomic.h> |
62ac665f | 6 | #include <linux/percpu.h> |
52b94129 | 7 | #include <linux/rcuwait.h> |
7f26482a | 8 | #include <linux/wait.h> |
001dac62 | 9 | #include <linux/rcu_sync.h> |
8ebe3473 | 10 | #include <linux/lockdep.h> |
fdfda868 | 11 | #include <linux/cleanup.h> |
62ac665f MP |
12 | |
13 | struct percpu_rw_semaphore { | |
001dac62 | 14 | struct rcu_sync rss; |
80127a39 | 15 | unsigned int __percpu *read_count; |
7f26482a PZ |
16 | struct rcuwait writer; |
17 | wait_queue_head_t waiters; | |
18 | atomic_t block; | |
1751060e PZ |
19 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
20 | struct lockdep_map dep_map; | |
21 | #endif | |
62ac665f MP |
22 | }; |
23 | ||
1751060e PZ |
24 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
25 | #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) .dep_map = { .name = #lockname }, | |
26 | #else | |
27 | #define __PERCPU_RWSEM_DEP_MAP_INIT(lockname) | |
28 | #endif | |
29 | ||
3f2947b7 | 30 | #define __DEFINE_PERCPU_RWSEM(name, is_static) \ |
11d9684c | 31 | static DEFINE_PER_CPU(unsigned int, __percpu_rwsem_rc_##name); \ |
3f2947b7 | 32 | is_static struct percpu_rw_semaphore name = { \ |
95bf33b5 | 33 | .rss = __RCU_SYNC_INITIALIZER(name.rss), \ |
11d9684c | 34 | .read_count = &__percpu_rwsem_rc_##name, \ |
52b94129 | 35 | .writer = __RCUWAIT_INITIALIZER(name.writer), \ |
7f26482a PZ |
36 | .waiters = __WAIT_QUEUE_HEAD_INITIALIZER(name.waiters), \ |
37 | .block = ATOMIC_INIT(0), \ | |
1751060e | 38 | __PERCPU_RWSEM_DEP_MAP_INIT(name) \ |
11d9684c | 39 | } |
1751060e | 40 | |
3f2947b7 ON |
41 | #define DEFINE_PERCPU_RWSEM(name) \ |
42 | __DEFINE_PERCPU_RWSEM(name, /* not static */) | |
43 | #define DEFINE_STATIC_PERCPU_RWSEM(name) \ | |
44 | __DEFINE_PERCPU_RWSEM(name, static) | |
11d9684c | 45 | |
559b3bbf | 46 | extern bool __percpu_down_read(struct percpu_rw_semaphore *, bool, bool); |
80127a39 | 47 | |
559b3bbf JB |
48 | static inline void percpu_down_read_internal(struct percpu_rw_semaphore *sem, |
49 | bool freezable) | |
80127a39 PZ |
50 | { |
51 | might_sleep(); | |
52 | ||
1751060e | 53 | rwsem_acquire_read(&sem->dep_map, 0, 0, _RET_IP_); |
80127a39 PZ |
54 | |
55 | preempt_disable(); | |
56 | /* | |
57 | * We are in an RCU-sched read-side critical section, so the writer | |
58 | * cannot both change sem->state from readers_fast and start checking | |
59 | * counters while we are here. So if we see !sem->state, we know that | |
60 | * the writer won't be checking until we're past the preempt_enable() | |
e3e74054 | 61 | * and that once the synchronize_rcu() is done, the writer will see |
80127a39 PZ |
62 | * anything we did within this RCU-sched read-size critical section. |
63 | */ | |
71365d40 | 64 | if (likely(rcu_sync_is_idle(&sem->rss))) |
e6b1a44e | 65 | this_cpu_inc(*sem->read_count); |
71365d40 | 66 | else |
559b3bbf | 67 | __percpu_down_read(sem, false, freezable); /* Unconditional memory barrier */ |
80127a39 | 68 | /* |
02e525b2 | 69 | * The preempt_enable() prevents the compiler from |
80127a39 PZ |
70 | * bleeding the critical section out. |
71 | */ | |
259d69b7 PZ |
72 | preempt_enable(); |
73 | } | |
74 | ||
559b3bbf JB |
75 | static inline void percpu_down_read(struct percpu_rw_semaphore *sem) |
76 | { | |
77 | percpu_down_read_internal(sem, false); | |
78 | } | |
79 | ||
80 | static inline void percpu_down_read_freezable(struct percpu_rw_semaphore *sem, | |
81 | bool freeze) | |
82 | { | |
83 | percpu_down_read_internal(sem, freeze); | |
84 | } | |
85 | ||
206c98ff | 86 | static inline bool percpu_down_read_trylock(struct percpu_rw_semaphore *sem) |
80127a39 | 87 | { |
206c98ff | 88 | bool ret = true; |
80127a39 PZ |
89 | |
90 | preempt_disable(); | |
91 | /* | |
92 | * Same as in percpu_down_read(). | |
93 | */ | |
71365d40 | 94 | if (likely(rcu_sync_is_idle(&sem->rss))) |
e6b1a44e | 95 | this_cpu_inc(*sem->read_count); |
71365d40 | 96 | else |
559b3bbf | 97 | ret = __percpu_down_read(sem, true, false); /* Unconditional memory barrier */ |
80127a39 PZ |
98 | preempt_enable(); |
99 | /* | |
100 | * The barrier() from preempt_enable() prevents the compiler from | |
101 | * bleeding the critical section out. | |
102 | */ | |
103 | ||
104 | if (ret) | |
1751060e | 105 | rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_); |
80127a39 PZ |
106 | |
107 | return ret; | |
108 | } | |
109 | ||
02e525b2 | 110 | static inline void percpu_up_read(struct percpu_rw_semaphore *sem) |
80127a39 | 111 | { |
1751060e PZ |
112 | rwsem_release(&sem->dep_map, _RET_IP_); |
113 | ||
02e525b2 | 114 | preempt_disable(); |
80127a39 PZ |
115 | /* |
116 | * Same as in percpu_down_read(). | |
117 | */ | |
ac8dec42 | 118 | if (likely(rcu_sync_is_idle(&sem->rss))) { |
e6b1a44e | 119 | this_cpu_dec(*sem->read_count); |
ac8dec42 DB |
120 | } else { |
121 | /* | |
122 | * slowpath; reader will only ever wake a single blocked | |
123 | * writer. | |
124 | */ | |
125 | smp_mb(); /* B matches C */ | |
126 | /* | |
127 | * In other words, if they see our decrement (presumably to | |
128 | * aggregate zero, as that is the only time it matters) they | |
129 | * will also see our critical section. | |
130 | */ | |
e6b1a44e | 131 | this_cpu_dec(*sem->read_count); |
ac8dec42 DB |
132 | rcuwait_wake_up(&sem->writer); |
133 | } | |
80127a39 | 134 | preempt_enable(); |
80127a39 | 135 | } |
5c1eabe6 | 136 | |
01fe8a3f | 137 | extern bool percpu_is_read_locked(struct percpu_rw_semaphore *); |
a1fd3e24 ON |
138 | extern void percpu_down_write(struct percpu_rw_semaphore *); |
139 | extern void percpu_up_write(struct percpu_rw_semaphore *); | |
62ac665f | 140 | |
fdfda868 PZI |
141 | DEFINE_GUARD(percpu_read, struct percpu_rw_semaphore *, |
142 | percpu_down_read(_T), percpu_up_read(_T)) | |
143 | DEFINE_GUARD_COND(percpu_read, _try, percpu_down_read_trylock(_T)) | |
144 | ||
145 | DEFINE_GUARD(percpu_write, struct percpu_rw_semaphore *, | |
146 | percpu_down_write(_T), percpu_up_write(_T)) | |
147 | ||
01fe8a3f ME |
148 | static inline bool percpu_is_write_locked(struct percpu_rw_semaphore *sem) |
149 | { | |
150 | return atomic_read(&sem->block); | |
151 | } | |
152 | ||
8ebe3473 ON |
153 | extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, |
154 | const char *, struct lock_class_key *); | |
80127a39 | 155 | |
a1fd3e24 | 156 | extern void percpu_free_rwsem(struct percpu_rw_semaphore *); |
62ac665f | 157 | |
80127a39 | 158 | #define percpu_init_rwsem(sem) \ |
8ebe3473 ON |
159 | ({ \ |
160 | static struct lock_class_key rwsem_key; \ | |
80127a39 | 161 | __percpu_init_rwsem(sem, #sem, &rwsem_key); \ |
8ebe3473 ON |
162 | }) |
163 | ||
1751060e PZ |
164 | #define percpu_rwsem_is_held(sem) lockdep_is_held(sem) |
165 | #define percpu_rwsem_assert_held(sem) lockdep_assert_held(sem) | |
11d9684c | 166 | |
55cc1565 | 167 | static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem, |
c01a5d89 | 168 | unsigned long ip) |
55cc1565 | 169 | { |
1751060e | 170 | lock_release(&sem->dep_map, ip); |
55cc1565 ON |
171 | } |
172 | ||
173 | static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem, | |
174 | bool read, unsigned long ip) | |
175 | { | |
1751060e | 176 | lock_acquire(&sem->dep_map, 0, 1, read, 1, NULL, ip); |
55cc1565 ON |
177 | } |
178 | ||
62ac665f | 179 | #endif |