xref: /openbmc/linux/kernel/locking/rwbase_rt.c (revision 25b892b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * RT-specific reader/writer semaphores and reader/writer locks
5  *
6  * down_write/write_lock()
7  *  1) Lock rtmutex
8  *  2) Remove the reader BIAS to force readers into the slow path
9  *  3) Wait until all readers have left the critical section
10  *  4) Mark it write locked
11  *
12  * up_write/write_unlock()
13  *  1) Remove the write locked marker
14  *  2) Set the reader BIAS, so readers can use the fast path again
15  *  3) Unlock rtmutex, to release blocked readers
16  *
17  * down_read/read_lock()
18  *  1) Try fast path acquisition (reader BIAS is set)
19  *  2) Take tmutex::wait_lock, which protects the writelocked flag
20  *  3) If !writelocked, acquire it for read
21  *  4) If writelocked, block on tmutex
22  *  5) unlock rtmutex, goto 1)
23  *
24  * up_read/read_unlock()
25  *  1) Try fast path release (reader count != 1)
26  *  2) Wake the writer waiting in down_write()/write_lock() #3
27  *
28  * down_read/read_lock()#3 has the consequence, that rw semaphores and rw
29  * locks on RT are not writer fair, but writers, which should be avoided in
30  * RT tasks (think mmap_sem), are subject to the rtmutex priority/DL
31  * inheritance mechanism.
32  *
33  * It's possible to make the rw primitives writer fair by keeping a list of
34  * active readers. A blocked writer would force all newly incoming readers
35  * to block on the rtmutex, but the rtmutex would have to be proxy locked
36  * for one reader after the other. We can't use multi-reader inheritance
37  * because there is no way to support that with SCHED_DEADLINE.
38  * Implementing the one by one reader boosting/handover mechanism is a
39  * major surgery for a very dubious value.
40  *
41  * The risk of writer starvation is there, but the pathological use cases
42  * which trigger it are not necessarily the typical RT workloads.
43  *
44  * Fast-path orderings:
45  * The lock/unlock of readers can run in fast paths: lock and unlock are only
46  * atomic ops, and there is no inner lock to provide ACQUIRE and RELEASE
47  * semantics of rwbase_rt. Atomic ops should thus provide _acquire()
48  * and _release() (or stronger).
49  *
50  * Common code shared between RT rw_semaphore and rwlock
51  */
52 
53 static __always_inline int rwbase_read_trylock(struct rwbase_rt *rwb)
54 {
55 	int r;
56 
57 	/*
58 	 * Increment reader count, if sem->readers < 0, i.e. READER_BIAS is
59 	 * set.
60 	 */
61 	for (r = atomic_read(&rwb->readers); r < 0;) {
62 		/* Fully-ordered if cmpxchg() succeeds, provides ACQUIRE */
63 		if (likely(atomic_try_cmpxchg(&rwb->readers, &r, r + 1)))
64 			return 1;
65 	}
66 	return 0;
67 }
68 
69 static int __sched __rwbase_read_lock(struct rwbase_rt *rwb,
70 				      unsigned int state)
71 {
72 	struct rt_mutex_base *rtm = &rwb->rtmutex;
73 	int ret;
74 
75 	raw_spin_lock_irq(&rtm->wait_lock);
76 	/*
77 	 * Allow readers, as long as the writer has not completely
78 	 * acquired the semaphore for write.
79 	 */
80 	if (atomic_read(&rwb->readers) != WRITER_BIAS) {
81 		atomic_inc(&rwb->readers);
82 		raw_spin_unlock_irq(&rtm->wait_lock);
83 		return 0;
84 	}
85 
86 	/*
87 	 * Call into the slow lock path with the rtmutex->wait_lock
88 	 * held, so this can't result in the following race:
89 	 *
90 	 * Reader1		Reader2		Writer
91 	 *			down_read()
92 	 *					down_write()
93 	 *					rtmutex_lock(m)
94 	 *					wait()
95 	 * down_read()
96 	 * unlock(m->wait_lock)
97 	 *			up_read()
98 	 *			wake(Writer)
99 	 *					lock(m->wait_lock)
100 	 *					sem->writelocked=true
101 	 *					unlock(m->wait_lock)
102 	 *
103 	 *					up_write()
104 	 *					sem->writelocked=false
105 	 *					rtmutex_unlock(m)
106 	 *			down_read()
107 	 *					down_write()
108 	 *					rtmutex_lock(m)
109 	 *					wait()
110 	 * rtmutex_lock(m)
111 	 *
112 	 * That would put Reader1 behind the writer waiting on
113 	 * Reader2 to call up_read(), which might be unbound.
114 	 */
115 
116 	/*
117 	 * For rwlocks this returns 0 unconditionally, so the below
118 	 * !ret conditionals are optimized out.
119 	 */
120 	ret = rwbase_rtmutex_slowlock_locked(rtm, state);
121 
122 	/*
123 	 * On success the rtmutex is held, so there can't be a writer
124 	 * active. Increment the reader count and immediately drop the
125 	 * rtmutex again.
126 	 *
127 	 * rtmutex->wait_lock has to be unlocked in any case of course.
128 	 */
129 	if (!ret)
130 		atomic_inc(&rwb->readers);
131 	raw_spin_unlock_irq(&rtm->wait_lock);
132 	if (!ret)
133 		rwbase_rtmutex_unlock(rtm);
134 	return ret;
135 }
136 
137 static __always_inline int rwbase_read_lock(struct rwbase_rt *rwb,
138 					    unsigned int state)
139 {
140 	if (rwbase_read_trylock(rwb))
141 		return 0;
142 
143 	return __rwbase_read_lock(rwb, state);
144 }
145 
146 static void __sched __rwbase_read_unlock(struct rwbase_rt *rwb,
147 					 unsigned int state)
148 {
149 	struct rt_mutex_base *rtm = &rwb->rtmutex;
150 	struct task_struct *owner;
151 
152 	raw_spin_lock_irq(&rtm->wait_lock);
153 	/*
154 	 * Wake the writer, i.e. the rtmutex owner. It might release the
155 	 * rtmutex concurrently in the fast path (due to a signal), but to
156 	 * clean up rwb->readers it needs to acquire rtm->wait_lock. The
157 	 * worst case which can happen is a spurious wakeup.
158 	 */
159 	owner = rt_mutex_owner(rtm);
160 	if (owner)
161 		wake_up_state(owner, state);
162 
163 	raw_spin_unlock_irq(&rtm->wait_lock);
164 }
165 
166 static __always_inline void rwbase_read_unlock(struct rwbase_rt *rwb,
167 					       unsigned int state)
168 {
169 	/*
170 	 * rwb->readers can only hit 0 when a writer is waiting for the
171 	 * active readers to leave the critical section.
172 	 *
173 	 * dec_and_test() is fully ordered, provides RELEASE.
174 	 */
175 	if (unlikely(atomic_dec_and_test(&rwb->readers)))
176 		__rwbase_read_unlock(rwb, state);
177 }
178 
179 static inline void __rwbase_write_unlock(struct rwbase_rt *rwb, int bias,
180 					 unsigned long flags)
181 {
182 	struct rt_mutex_base *rtm = &rwb->rtmutex;
183 
184 	/*
185 	 * _release() is needed in case that reader is in fast path, pairing
186 	 * with atomic_try_cmpxchg() in rwbase_read_trylock(), provides RELEASE
187 	 */
188 	(void)atomic_add_return_release(READER_BIAS - bias, &rwb->readers);
189 	raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
190 	rwbase_rtmutex_unlock(rtm);
191 }
192 
193 static inline void rwbase_write_unlock(struct rwbase_rt *rwb)
194 {
195 	struct rt_mutex_base *rtm = &rwb->rtmutex;
196 	unsigned long flags;
197 
198 	raw_spin_lock_irqsave(&rtm->wait_lock, flags);
199 	__rwbase_write_unlock(rwb, WRITER_BIAS, flags);
200 }
201 
202 static inline void rwbase_write_downgrade(struct rwbase_rt *rwb)
203 {
204 	struct rt_mutex_base *rtm = &rwb->rtmutex;
205 	unsigned long flags;
206 
207 	raw_spin_lock_irqsave(&rtm->wait_lock, flags);
208 	/* Release it and account current as reader */
209 	__rwbase_write_unlock(rwb, WRITER_BIAS - 1, flags);
210 }
211 
212 static inline bool __rwbase_write_trylock(struct rwbase_rt *rwb)
213 {
214 	/* Can do without CAS because we're serialized by wait_lock. */
215 	lockdep_assert_held(&rwb->rtmutex.wait_lock);
216 
217 	/*
218 	 * _acquire is needed in case the reader is in the fast path, pairing
219 	 * with rwbase_read_unlock(), provides ACQUIRE.
220 	 */
221 	if (!atomic_read_acquire(&rwb->readers)) {
222 		atomic_set(&rwb->readers, WRITER_BIAS);
223 		return 1;
224 	}
225 
226 	return 0;
227 }
228 
229 static int __sched rwbase_write_lock(struct rwbase_rt *rwb,
230 				     unsigned int state)
231 {
232 	struct rt_mutex_base *rtm = &rwb->rtmutex;
233 	unsigned long flags;
234 
235 	/* Take the rtmutex as a first step */
236 	if (rwbase_rtmutex_lock_state(rtm, state))
237 		return -EINTR;
238 
239 	/* Force readers into slow path */
240 	atomic_sub(READER_BIAS, &rwb->readers);
241 
242 	raw_spin_lock_irqsave(&rtm->wait_lock, flags);
243 	if (__rwbase_write_trylock(rwb))
244 		goto out_unlock;
245 
246 	rwbase_set_and_save_current_state(state);
247 	for (;;) {
248 		/* Optimized out for rwlocks */
249 		if (rwbase_signal_pending_state(state, current)) {
250 			rwbase_restore_current_state();
251 			__rwbase_write_unlock(rwb, 0, flags);
252 			return -EINTR;
253 		}
254 
255 		if (__rwbase_write_trylock(rwb))
256 			break;
257 
258 		raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
259 		rwbase_schedule();
260 		raw_spin_lock_irqsave(&rtm->wait_lock, flags);
261 
262 		set_current_state(state);
263 	}
264 	rwbase_restore_current_state();
265 
266 out_unlock:
267 	raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
268 	return 0;
269 }
270 
271 static inline int rwbase_write_trylock(struct rwbase_rt *rwb)
272 {
273 	struct rt_mutex_base *rtm = &rwb->rtmutex;
274 	unsigned long flags;
275 
276 	if (!rwbase_rtmutex_trylock(rtm))
277 		return 0;
278 
279 	atomic_sub(READER_BIAS, &rwb->readers);
280 
281 	raw_spin_lock_irqsave(&rtm->wait_lock, flags);
282 	if (__rwbase_write_trylock(rwb)) {
283 		raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
284 		return 1;
285 	}
286 	__rwbase_write_unlock(rwb, 0, flags);
287 	return 0;
288 }
289