Skip to content

Commit f038cc1

Browse files
author
Kent Overstreet
committed
locking/seqlock: Split out seqlock_types.h
Trimming down sched.h dependencies: we don't want to include more than the base types. Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Will Deacon <will@kernel.org> Cc: Waiman Long <longman@redhat.com> Cc: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 53d31ba commit f038cc1

File tree

3 files changed

+96
-78
lines changed

3 files changed

+96
-78
lines changed

include/linux/sched.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <linux/task_io_accounting.h>
3434
#include <linux/posix-timers_types.h>
3535
#include <linux/rseq.h>
36-
#include <linux/seqlock.h>
36+
#include <linux/seqlock_types.h>
3737
#include <linux/kcsan.h>
3838
#include <linux/rv.h>
3939
#include <linux/livepatch_sched.h>

include/linux/seqlock.h

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/lockdep.h>
1919
#include <linux/mutex.h>
2020
#include <linux/preempt.h>
21+
#include <linux/seqlock_types.h>
2122
#include <linux/spinlock.h>
2223

2324
#include <asm/processor.h>
@@ -37,37 +38,6 @@
3738
*/
3839
#define KCSAN_SEQLOCK_REGION_MAX 1000
3940

40-
/*
41-
* Sequence counters (seqcount_t)
42-
*
43-
* This is the raw counting mechanism, without any writer protection.
44-
*
45-
* Write side critical sections must be serialized and non-preemptible.
46-
*
47-
* If readers can be invoked from hardirq or softirq contexts,
48-
* interrupts or bottom halves must also be respectively disabled before
49-
* entering the write section.
50-
*
51-
* This mechanism can't be used if the protected data contains pointers,
52-
* as the writer can invalidate a pointer that a reader is following.
53-
*
54-
* If the write serialization mechanism is one of the common kernel
55-
* locking primitives, use a sequence counter with associated lock
56-
* (seqcount_LOCKNAME_t) instead.
57-
*
58-
* If it's desired to automatically handle the sequence counter writer
59-
* serialization and non-preemptibility requirements, use a sequential
60-
* lock (seqlock_t) instead.
61-
*
62-
* See Documentation/locking/seqlock.rst
63-
*/
64-
typedef struct seqcount {
65-
unsigned sequence;
66-
#ifdef CONFIG_DEBUG_LOCK_ALLOC
67-
struct lockdep_map dep_map;
68-
#endif
69-
} seqcount_t;
70-
7141
static inline void __seqcount_init(seqcount_t *s, const char *name,
7242
struct lock_class_key *key)
7343
{
@@ -131,28 +101,6 @@ static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
131101
* See Documentation/locking/seqlock.rst
132102
*/
133103

134-
/*
135-
* For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
136-
* disable preemption. It can lead to higher latencies, and the write side
137-
* sections will not be able to acquire locks which become sleeping locks
138-
* (e.g. spinlock_t).
139-
*
140-
* To remain preemptible while avoiding a possible livelock caused by the
141-
* reader preempting the writer, use a different technique: let the reader
142-
* detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
143-
* case, acquire then release the associated LOCKNAME writer serialization
144-
* lock. This will allow any possibly-preempted writer to make progress
145-
* until the end of its writer serialization lock critical section.
146-
*
147-
* This lock-unlock technique must be implemented for all of PREEMPT_RT
148-
* sleeping locks. See Documentation/locking/locktypes.rst
149-
*/
150-
#if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
151-
#define __SEQ_LOCK(expr) expr
152-
#else
153-
#define __SEQ_LOCK(expr)
154-
#endif
155-
156104
/*
157105
* typedef seqcount_LOCKNAME_t - sequence counter with LOCKNAME associated
158106
* @seqcount: The real sequence counter
@@ -194,11 +142,6 @@ static inline void seqcount_lockdep_reader_access(const seqcount_t *s)
194142
* @lockbase: prefix for associated lock/unlock
195143
*/
196144
#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase) \
197-
typedef struct seqcount_##lockname { \
198-
seqcount_t seqcount; \
199-
__SEQ_LOCK(locktype *lock); \
200-
} seqcount_##lockname##_t; \
201-
\
202145
static __always_inline seqcount_t * \
203146
__seqprop_##lockname##_ptr(seqcount_##lockname##_t *s) \
204147
{ \
@@ -284,6 +227,7 @@ SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, raw_spin)
284227
SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, spin)
285228
SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, read)
286229
SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
230+
#undef SEQCOUNT_LOCKNAME
287231

288232
/*
289233
* SEQCNT_LOCKNAME_ZERO - static initializer for seqcount_LOCKNAME_t
@@ -794,25 +738,6 @@ static inline void raw_write_seqcount_latch(seqcount_latch_t *s)
794738
smp_wmb(); /* increment "sequence" before following stores */
795739
}
796740

797-
/*
798-
* Sequential locks (seqlock_t)
799-
*
800-
* Sequence counters with an embedded spinlock for writer serialization
801-
* and non-preemptibility.
802-
*
803-
* For more info, see:
804-
* - Comments on top of seqcount_t
805-
* - Documentation/locking/seqlock.rst
806-
*/
807-
typedef struct {
808-
/*
809-
* Make sure that readers don't starve writers on PREEMPT_RT: use
810-
* seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK().
811-
*/
812-
seqcount_spinlock_t seqcount;
813-
spinlock_t lock;
814-
} seqlock_t;
815-
816741
#define __SEQLOCK_UNLOCKED(lockname) \
817742
{ \
818743
.seqcount = SEQCNT_SPINLOCK_ZERO(lockname, &(lockname).lock), \

include/linux/seqlock_types.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __LINUX_SEQLOCK_TYPES_H
3+
#define __LINUX_SEQLOCK_TYPES_H
4+
5+
#include <linux/lockdep_types.h>
6+
#include <linux/mutex_types.h>
7+
#include <linux/spinlock_types.h>
8+
9+
/*
10+
* Sequence counters (seqcount_t)
11+
*
12+
* This is the raw counting mechanism, without any writer protection.
13+
*
14+
* Write side critical sections must be serialized and non-preemptible.
15+
*
16+
* If readers can be invoked from hardirq or softirq contexts,
17+
* interrupts or bottom halves must also be respectively disabled before
18+
* entering the write section.
19+
*
20+
* This mechanism can't be used if the protected data contains pointers,
21+
* as the writer can invalidate a pointer that a reader is following.
22+
*
23+
* If the write serialization mechanism is one of the common kernel
24+
* locking primitives, use a sequence counter with associated lock
25+
* (seqcount_LOCKNAME_t) instead.
26+
*
27+
* If it's desired to automatically handle the sequence counter writer
28+
* serialization and non-preemptibility requirements, use a sequential
29+
* lock (seqlock_t) instead.
30+
*
31+
* See Documentation/locking/seqlock.rst
32+
*/
33+
typedef struct seqcount {
34+
unsigned sequence;
35+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
36+
struct lockdep_map dep_map;
37+
#endif
38+
} seqcount_t;
39+
40+
/*
41+
* For PREEMPT_RT, seqcount_LOCKNAME_t write side critical sections cannot
42+
* disable preemption. It can lead to higher latencies, and the write side
43+
* sections will not be able to acquire locks which become sleeping locks
44+
* (e.g. spinlock_t).
45+
*
46+
* To remain preemptible while avoiding a possible livelock caused by the
47+
* reader preempting the writer, use a different technique: let the reader
48+
* detect if a seqcount_LOCKNAME_t writer is in progress. If that is the
49+
* case, acquire then release the associated LOCKNAME writer serialization
50+
* lock. This will allow any possibly-preempted writer to make progress
51+
* until the end of its writer serialization lock critical section.
52+
*
53+
* This lock-unlock technique must be implemented for all of PREEMPT_RT
54+
* sleeping locks. See Documentation/locking/locktypes.rst
55+
*/
56+
#if defined(CONFIG_LOCKDEP) || defined(CONFIG_PREEMPT_RT)
57+
#define __SEQ_LOCK(expr) expr
58+
#else
59+
#define __SEQ_LOCK(expr)
60+
#endif
61+
62+
#define SEQCOUNT_LOCKNAME(lockname, locktype, preemptible, lockbase) \
63+
typedef struct seqcount_##lockname { \
64+
seqcount_t seqcount; \
65+
__SEQ_LOCK(locktype *lock); \
66+
} seqcount_##lockname##_t;
67+
68+
SEQCOUNT_LOCKNAME(raw_spinlock, raw_spinlock_t, false, raw_spin)
69+
SEQCOUNT_LOCKNAME(spinlock, spinlock_t, __SEQ_RT, spin)
70+
SEQCOUNT_LOCKNAME(rwlock, rwlock_t, __SEQ_RT, read)
71+
SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex)
72+
#undef SEQCOUNT_LOCKNAME
73+
74+
/*
75+
* Sequential locks (seqlock_t)
76+
*
77+
* Sequence counters with an embedded spinlock for writer serialization
78+
* and non-preemptibility.
79+
*
80+
* For more info, see:
81+
* - Comments on top of seqcount_t
82+
* - Documentation/locking/seqlock.rst
83+
*/
84+
typedef struct {
85+
/*
86+
* Make sure that readers don't starve writers on PREEMPT_RT: use
87+
* seqcount_spinlock_t instead of seqcount_t. Check __SEQ_LOCK().
88+
*/
89+
seqcount_spinlock_t seqcount;
90+
spinlock_t lock;
91+
} seqlock_t;
92+
93+
#endif /* __LINUX_SEQLOCK_TYPES_H */

0 commit comments

Comments
 (0)