Skip to content

Commit 8117ab5

Browse files
a-darwishPeter Zijlstra
authored andcommitted
seqlock: seqcount_LOCKNAME_t: Introduce PREEMPT_RT support
Preemption must be disabled before entering a sequence counter write side critical section. Otherwise the read side section can preempt the write side section and spin for the entire scheduler tick. If that reader belongs to a real-time scheduling class, it can spin forever and the kernel will livelock. Disabling preemption cannot be done for PREEMPT_RT though: it can lead to higher latencies, and the write side sections will not be able to acquire locks which become sleeping locks (e.g. spinlock_t). To remain preemptible, while avoiding a possible livelock caused by the reader preempting the writer, use a different technique: let the reader detect if a seqcount_LOCKNAME_t writer is in progress. If that's the case, acquire then release the associated LOCKNAME writer serialization lock. This will allow any possibly-preempted writer to make progress until the end of its writer serialization lock critical section. Implement this lock-unlock technique for all seqcount_LOCKNAME_t with an associated (PREEMPT_RT) sleeping lock. References: 55f3560 ("seqlock: Extend seqcount API with associated locks") Signed-off-by: Ahmed S. Darwish <a.darwish@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20200519214547.352050-1-a.darwish@linutronix.de
1 parent 52ac39e commit 8117ab5

1 file changed

Lines changed: 51 additions & 10 deletions

File tree

include/linux/seqlock.h

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/kcsan-checks.h>
1818
#include <linux/lockdep.h>
1919
#include <linux/mutex.h>
20+
#include <linux/ww_mutex.h>
2021
#include <linux/preempt.h>
2122
#include <linux/spinlock.h>
2223

@@ -131,7 +132,23 @@ static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
131132
* See Documentation/locking/seqlock.rst
132133
*/
133134

134-
#ifdef CONFIG_LOCKDEP
135+
/*
136+
* For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
137+
* disable preemption. It can lead to higher latencies, and the write side
138+
* sections will not be able to acquire locks which become sleeping locks
139+
* (e.g. spinlock_t).
140+
*
141+
* To remain preemptible while avoiding a possible livelock caused by the
142+
* reader preempting the writer, use a different technique: let the reader
143+
* detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
144+
* case, acquire then release the associated LOCKNAME writer serialization
145+
* lock. This will allow any possibly-preempted writer to make progress
146+
* until the end of its writer serialization lock critical section.
147+
*
148+
* This lock-unlock technique must be implemented for all of PREEMPT_RT
149+
* sleeping locks. See Documentation/locking/locktypes.rst
150+
*/
151+
#if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
135152
#define __SEQ_LOCK(expr) expr
136153
#else
137154
#define __SEQ_LOCK(expr)
@@ -162,10 +179,12 @@ static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
162179
*
163180
* @lockname: "LOCKNAME" part of seqcount_LOCKNAME_t
164181
* @locktype: LOCKNAME canonical C data type
165-
* @preemptible: preemptibility of above lockname
182+
* @preemptible: preemptibility of above locktype
166183
* @lockmember: argument for lockdep_assert_held()
184+
* @lockbase: associated lock release function (prefix only)
185+
* @lock_acquire: associated lock acquisition function (full call)
167186
*/
168-
#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockmember) \
187+
#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockmember, lockbase, lock_acquire) \
169188
typedef struct seqcount_##lockname { \
170189
seqcount_t seqcount; \
171190
__SEQ_LOCK(locktype *lock); \
@@ -187,13 +206,33 @@ __seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
187206
static __always_inline unsigned \
188207
__seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \
189208
{ \
190-
return READ_ONCE(s->seqcount.sequence); \
209+
unsigned seq = READ_ONCE(s->seqcount.sequence); \
210+
\
211+
if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
212+
return seq; \
213+
\
214+
if (preemptible && unlikely(seq & 1)) { \
215+
__SEQ_LOCK(lock_acquire); \
216+
__SEQ_LOCK(lockbase##_unlock(s->lock)); \
217+
\
218+
/* \
219+
* Re-read the sequence counter since the (possibly \
220+
* preempted) writer made progress. \
221+
*/ \
222+
seq = READ_ONCE(s->seqcount.sequence); \
223+
} \
224+
\
225+
return seq; \
191226
} \
192227
\
193228
static __always_inline bool \
194229
__seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \
195230
{ \
196-
return preemptible; \
231+
if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \
232+
return preemptible; \
233+
\
234+
/* PREEMPT_RT relies on the above LOCK+UNLOCK */ \
235+
return false; \
197236
} \
198237
\
199238
static __always_inline void \
@@ -226,11 +265,13 @@ static inline void __seqprop_assert(const seqcount_t *s)
226265
lockdep_assert_preemption_disabled();
227266
}
228267

229-
SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, s->lock)
230-
SEQCOUNT_LOCKNAME(spinlock, spinlock_t, false, s->lock)
231-
SEQCOUNT_LOCKNAME(rwlock, rwlock_t, false, s->lock)
232-
SEQCOUNT_LOCKNAME(mutex, struct mutex, true, s->lock)
233-
SEQCOUNT_LOCKNAME(ww_mutex, struct ww_mutex, true, &s->lock->base)
268+
#define __SEQ_RT IS_ENABLED(CONFIG_PREEMPT_RT)
269+
270+
SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, s->lock, raw_spin, raw_spin_lock(s->lock))
271+
SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, s->lock, spin, spin_lock(s->lock))
272+
SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, s->lock, read, read_lock(s->lock))
273+
SEQCOUNT_LOCKNAME(mutex, struct mutex, true, s->lock, mutex, mutex_lock(s->lock))
274+
SEQCOUNT_LOCKNAME(ww_mutex, struct ww_mutex, true, &s->lock->base, ww_mutex, ww_mutex_lock(s->lock, NULL))
234275

235276
/*
236277
* SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t

0 commit comments

Comments
 (0)