Skip to content

Commit 55f3560

Browse files
a-darwishPeter Zijlstra
authored andcommitted
seqlock: Extend seqcount API with associated locks
A sequence counter write side critical section must be protected by some form of locking to serialize writers. If the serialization primitive is not disabling preemption implicitly, preemption has to be explicitly disabled before entering the write side critical section. There is no built-in debugging mechanism to verify that the lock used for writer serialization is held and preemption is disabled. Some usage sites like dma-buf have explicit lockdep checks for the writer-side lock, but this covers only a small portion of the sequence counter usage in the kernel. Add new sequence counter types which allows to associate a lock to the sequence counter at initialization time. The seqcount API functions are extended to provide appropriate lockdep assertions depending on the seqcount/lock type. For sequence counters with associated locks that do not implicitly disable preemption, preemption protection is enforced in the sequence counter write side functions. This removes the need to explicitly add preempt_disable/enable() around the write side critical sections: the write_begin/end() functions for these new sequence counter types automatically do this. Introduce the following seqcount types with associated locks: seqcount_spinlock_t seqcount_raw_spinlock_t seqcount_rwlock_t seqcount_mutex_t seqcount_ww_mutex_t Extend the seqcount read and write functions to branch out to the specific seqcount_LOCKTYPE_t implementation at compile-time. This avoids kernel API explosion per each new seqcount_LOCKTYPE_t added. Add such compile-time type detection logic into a new, internal, seqlock header. Document the proper seqcount_LOCKTYPE_t usage, and rationale, at Documentation/locking/seqlock.rst. If lockdep is disabled, this lock association is compiled out and has neither storage size nor runtime overhead. Signed-off-by: Ahmed S. Darwish <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 859247d commit 55f3560

File tree

2 files changed

+447
-69
lines changed

2 files changed

+447
-69
lines changed

Documentation/locking/seqlock.rst

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,58 @@ Read path::
8787
} while (read_seqcount_retry(&foo_seqcount, seq));
8888

8989

90+
.. _seqcount_locktype_t:
91+
92+
Sequence counters with associated locks (``seqcount_LOCKTYPE_t``)
93+
-----------------------------------------------------------------
94+
95+
As discussed at :ref:`seqcount_t`, sequence count write side critical
96+
sections must be serialized and non-preemptible. This variant of
97+
sequence counters associate the lock used for writer serialization at
98+
initialization time, which enables lockdep to validate that the write
99+
side critical sections are properly serialized.
100+
101+
This lock association is a NOOP if lockdep is disabled and has neither
102+
storage nor runtime overhead. If lockdep is enabled, the lock pointer is
103+
stored in struct seqcount and lockdep's "lock is held" assertions are
104+
injected at the beginning of the write side critical section to validate
105+
that it is properly protected.
106+
107+
For lock types which do not implicitly disable preemption, preemption
108+
protection is enforced in the write side function.
109+
110+
The following sequence counters with associated locks are defined:
111+
112+
- ``seqcount_spinlock_t``
113+
- ``seqcount_raw_spinlock_t``
114+
- ``seqcount_rwlock_t``
115+
- ``seqcount_mutex_t``
116+
- ``seqcount_ww_mutex_t``
117+
118+
The plain seqcount read and write APIs branch out to the specific
119+
seqcount_LOCKTYPE_t implementation at compile-time. This avoids kernel
120+
API explosion per each new seqcount LOCKTYPE.
121+
122+
Initialization (replace "LOCKTYPE" with one of the supported locks)::
123+
124+
/* dynamic */
125+
seqcount_LOCKTYPE_t foo_seqcount;
126+
seqcount_LOCKTYPE_init(&foo_seqcount, &lock);
127+
128+
/* static */
129+
static seqcount_LOCKTYPE_t foo_seqcount =
130+
SEQCNT_LOCKTYPE_ZERO(foo_seqcount, &lock);
131+
132+
/* C99 struct init */
133+
struct {
134+
.seq = SEQCNT_LOCKTYPE_ZERO(foo.seq, &lock),
135+
} foo;
136+
137+
Write path: same as in :ref:`seqcount_t`, while running from a context
138+
with the associated LOCKTYPE lock acquired.
139+
140+
Read path: same as in :ref:`seqcount_t`.
141+
90142
.. _seqlock_t:
91143

92144
Sequential locks (``seqlock_t``)

0 commit comments

Comments
 (0)