xref: /netbsd-src/share/man/man9/locking.9 (revision 92ee6a04957b10e7114b71c9f67e2ab5414d5d64)
1*92ee6a04Swiz.\"	$NetBSD: locking.9,v 1.8 2017/08/27 20:44:42 wiz Exp $
2bce1cecbSkamil.\"
3bce1cecbSkamil.\" Copyright (c) 2015 The NetBSD Foundation, Inc.
4bce1cecbSkamil.\" All rights reserved.
5bce1cecbSkamil.\"
6bce1cecbSkamil.\" This code is derived from software contributed to The NetBSD Foundation
7bce1cecbSkamil.\" by Kamil Rytarowski.
8bce1cecbSkamil.\"
9bce1cecbSkamil.\" Redistribution and use in source and binary forms, with or without
10bce1cecbSkamil.\" modification, are permitted provided that the following conditions
11bce1cecbSkamil.\" are met:
12bce1cecbSkamil.\" 1. Redistributions of source code must retain the above copyright
13bce1cecbSkamil.\"    notice, this list of conditions and the following disclaimer.
14bce1cecbSkamil.\" 2. Redistributions in binary form must reproduce the above copyright
15bce1cecbSkamil.\"    notice, this list of conditions and the following disclaimer in the
16bce1cecbSkamil.\"    documentation and/or other materials provided with the distribution.
17bce1cecbSkamil.\"
18bce1cecbSkamil.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19bce1cecbSkamil.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20bce1cecbSkamil.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21bce1cecbSkamil.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22bce1cecbSkamil.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23bce1cecbSkamil.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24bce1cecbSkamil.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25bce1cecbSkamil.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26bce1cecbSkamil.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27bce1cecbSkamil.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28bce1cecbSkamil.\" POSSIBILITY OF SUCH DAMAGE.
29bce1cecbSkamil.\"
30*92ee6a04Swiz.Dd August 23, 2017
31bce1cecbSkamil.Dt LOCKING 9
32bce1cecbSkamil.Os
33bce1cecbSkamil.Sh NAME
34bce1cecbSkamil.Nm locking
356c4eff14Swiz.Nd introduction to kernel synchronization and interrupt control
36bce1cecbSkamil.Sh DESCRIPTION
37bce1cecbSkamilThe
38bce1cecbSkamil.Nx
39bce1cecbSkamilkernel provides several synchronization and interrupt control primitives.
40d54d8289SsevanThis man page aims to give an overview of these interfaces and their proper
41bce1cecbSkamilapplication.
42d54d8289SsevanAlso included are basic kernel thread control primitives and a rough
43bce1cecbSkamiloverview of the
44bce1cecbSkamil.Nx
45bce1cecbSkamilkernel design.
46bce1cecbSkamil.Sh KERNEL OVERVIEW
47d54d8289SsevanThe aim of synchronization, threads and interrupt control in the kernel is:
48bce1cecbSkamil.Bl -bullet -offset indent
49bce1cecbSkamil.It
50bce1cecbSkamilTo control concurrent access to shared resources (critical sections).
51bce1cecbSkamil.It
52bce1cecbSkamilSpawn tasks from an interrupt in the thread context.
53bce1cecbSkamil.It
54bce1cecbSkamilMask interrupts from threads.
55bce1cecbSkamil.It
56d54d8289SsevanScale on multiple CPU system.
57bce1cecbSkamil.El
58bce1cecbSkamil.Pp
59bce1cecbSkamilThere are three types of contexts in the
60bce1cecbSkamil.Nx
61bce1cecbSkamilkernel:
62bce1cecbSkamil.Bl -bullet -offset indent
63bce1cecbSkamil.It
64bce1cecbSkamil.Em Thread context
65d54d8289Ssevan- running processes (represented by
66bce1cecbSkamil.Dv struct proc )
67bce1cecbSkamiland light-weight processes (represented by
68d54d8289Ssevan.Dv struct lwp ,
69d54d8289Ssevanalso known as kernel threads).
706c4eff14SwizCode in this context can sleep, block resources and own address-space.
71bce1cecbSkamil.It
72bce1cecbSkamil.Em Software interrupt context
73d54d8289Ssevan- limited by thread context.
74bce1cecbSkamilCode in this context must be processed shortly.
756c4eff14SwizThese interrupts don't own any address space context.
76bce1cecbSkamilSoftware interrupts are a way of deferring hardware interrupts to do more
77bce1cecbSkamilexpensive processing at a lower interrupt priority.
78bce1cecbSkamil.It
79bce1cecbSkamil.Em Hard interrupt context
80d54d8289Ssevan- Code in this context must be processed as quickly as possible.
81d54d8289SsevanIt is forbidden for a piece of code to sleep or access long-awaited resources here.
82bce1cecbSkamil.El
83bce1cecbSkamil.Pp
84bce1cecbSkamilThe main differences between processes and kernel threads are:
85bce1cecbSkamil.Bl -bullet -offset indent
86bce1cecbSkamil.It
87d54d8289SsevanA single process can own multiple kernel threads (LWPs).
88bce1cecbSkamil.It
896c4eff14SwizA process owns address space context to map userland address space.
90bce1cecbSkamil.It
91bce1cecbSkamilProcesses are designed for userland executables and kernel threads for
92bce1cecbSkamilin-kernel tasks.
93bce1cecbSkamilThe only process running in the kernel-space is
94bce1cecbSkamil.Dv proc0
95bce1cecbSkamil(called swapper).
96bce1cecbSkamil.El
97bce1cecbSkamil.Sh INTERFACES
98bce1cecbSkamil.Ss Atomic memory operations
99bce1cecbSkamilThe
100bce1cecbSkamil.Nm atomic_ops
101bce1cecbSkamilfamily of functions provide atomic memory operations.
102bce1cecbSkamilThere are 7 classes of atomic memory operations available:
103bce1cecbSkamiladdition, logical
104bce1cecbSkamil.Dq and ,
105bce1cecbSkamilcompare-and-swap, decrement, increment, logical
106bce1cecbSkamil.Dq or ,
107bce1cecbSkamilswap.
108bce1cecbSkamil.Pp
109bce1cecbSkamilSee
110bce1cecbSkamil.Xr atomic_ops 3 .
111bce1cecbSkamil.Ss Condition variables
112bce1cecbSkamilCondition variables (CVs) are used in the kernel to synchronize access to
113bce1cecbSkamilresources that are limited (for example, memory) and to wait for pending I/O
114bce1cecbSkamiloperations to complete.
115bce1cecbSkamil.Pp
116bce1cecbSkamilSee
117bce1cecbSkamil.Xr condvar 9 .
118bce1cecbSkamil.Ss Memory access barrier operations
119bce1cecbSkamilThe
120bce1cecbSkamil.Nm membar_ops
121bce1cecbSkamilfamily of functions provide memory access barrier operations necessary for
122bce1cecbSkamilsynchronization in multiprocessor execution environments that have relaxed load
123bce1cecbSkamiland store order.
124bce1cecbSkamil.Pp
125bce1cecbSkamilSee
126bce1cecbSkamil.Xr membar_ops 3 .
127bce1cecbSkamil.Ss Memory barriers
128bce1cecbSkamilThe memory barriers can be used to control the order in which memory accesses
129bce1cecbSkamiloccur, and thus the order in which those accesses become visible to other
130bce1cecbSkamilprocessors.
131bce1cecbSkamilThey can be used to implement
132bce1cecbSkamil.Dq lockless
133bce1cecbSkamilaccess to data structures where the necessary barrier conditions are well
134bce1cecbSkamilunderstood.
135bce1cecbSkamil.Ss Mutual exclusion primitives
136bce1cecbSkamilThread-base adaptive mutexes.
137bce1cecbSkamilThese are lightweight,
138bce1cecbSkamilexclusive locks that use threads as the focus of synchronization activity.
139d54d8289SsevanAdaptive mutexes typically behave like spinlocks,
140bce1cecbSkamilbut under specific conditions an attempt to acquire an already held adaptive
141bce1cecbSkamilmutex may cause the acquiring thread to sleep.
142bce1cecbSkamilSleep activity occurs rarely.
143bce1cecbSkamilBusy-waiting is typically more efficient because mutex hold times are most
144bce1cecbSkamiloften short.
145bce1cecbSkamilIn contrast to pure spinlocks,
146d54d8289Ssevana thread holding an adaptive mutex may be pre-empted in the kernel,
147bce1cecbSkamilwhich can allow for reduced latency where soft real-time application are in use
148bce1cecbSkamilon the system.
149bce1cecbSkamil.Pp
150bce1cecbSkamilSee
151bce1cecbSkamil.Xr mutex 9 .
152bce1cecbSkamil.Ss Restartable atomic sequences
153bce1cecbSkamilRestartable atomic sequences are user code only sequences which are guaranteed
154bce1cecbSkamilto execute without preemption.
155bce1cecbSkamilThis property is assured by checking the set of restartable atomic sequences
156bce1cecbSkamilregistered for a process during
157bce1cecbSkamil.Xr cpu_switchto 9 .
158bce1cecbSkamilIf a process is found to have been preempted during a restartable sequence,
159bce1cecbSkamilthen its execution is rolled-back to the start of the sequence by resetting its
160d54d8289Ssevanprogram counter which is saved in its process control block (PCB).
161bce1cecbSkamil.Pp
162bce1cecbSkamilSee
163bce1cecbSkamil.Xr ras 9 .
164bce1cecbSkamil.Ss Reader / writer lock primitives
165bce1cecbSkamilReader / writer locks (RW locks) are used in the kernel to synchronize access
166bce1cecbSkamilto an object among LWPs (lightweight processes) and soft interrupt handlers.
167bce1cecbSkamilIn addition to the capabilities provided by mutexes,
168bce1cecbSkamilRW locks distinguish between read (shared) and write (exclusive) access.
169bce1cecbSkamil.Pp
170bce1cecbSkamilSee
171bce1cecbSkamil.Xr rwlock 9 .
172bce1cecbSkamil.Ss Functions to modify system interrupt priority level
173bce1cecbSkamilThese functions raise and lower the interrupt priority level.
174bce1cecbSkamilThey are used by kernel code to block interrupts in critical sections,
175bce1cecbSkamilin order to protect data structures.
176bce1cecbSkamil.Pp
177bce1cecbSkamilSee
178bce1cecbSkamil.Xr spl 9 .
179bce1cecbSkamil.Ss Machine-independent software interrupt framework
180bce1cecbSkamilThe software interrupt framework is designed to provide a generic software
181bce1cecbSkamilinterrupt mechanism which can be used any time a low-priority callback is
182d54d8289Ssevanrequired.
183d54d8289SsevanIt allows dynamic registration of software interrupts for loadable drivers,
184d54d8289Ssevanprotocol stacks, software interrupt prioritization, software interrupt fair
185d54d8289Ssevanqueuing and allows machine-dependent optimizations to reduce cost.
186bce1cecbSkamil.Pp
187bce1cecbSkamilSee
188bce1cecbSkamil.Xr softint 9 .
189bce1cecbSkamil.Ss Functions to raise the system priority level
190bce1cecbSkamilThe
191bce1cecbSkamil.Nm splraiseipl
192bce1cecbSkamilfunction raises the system priority level to the level specified by
193bce1cecbSkamil.Dv icookie ,
194bce1cecbSkamilwhich should be a value returned by
195bce1cecbSkamil.Xr makeiplcookie 9 .
196bce1cecbSkamilIn general, device drivers should not make use of this interface.
197bce1cecbSkamilTo ensure correct synchronization,
198bce1cecbSkamildevice drivers should use the
199bce1cecbSkamil.Xr condvar 9 ,
200bce1cecbSkamil.Xr mutex 9 ,
201bce1cecbSkamiland
202bce1cecbSkamil.Xr rwlock 9
203bce1cecbSkamilinterfaces.
204bce1cecbSkamil.Pp
205bce1cecbSkamilSee
206bce1cecbSkamil.Xr splraiseipl 9 .
207bce1cecbSkamil.Ss Passive serialization mechanism
208bce1cecbSkamilPassive serialization is a reader / writer synchronization mechanism designed
209bce1cecbSkamilfor lock-less read operations.
210bce1cecbSkamilThe read operations may happen from software interrupt at
211bce1cecbSkamil.Dv IPL_SOFTCLOCK .
212bce1cecbSkamil.Pp
213bce1cecbSkamilSee
214bce1cecbSkamil.Xr pserialize 9 .
215dd839657Spgoyette.Ss Passive reference mechanism
216dd839657SpgoyettePassive references allow CPUs to cheaply acquire and release passive
217dd839657Spgoyettereferences to a resource, which guarantee the resource will not be
21862592e2aSwizdestroyed until the reference is released.
21962592e2aSwizAcquiring and releasing passive references requires no interprocessor
22062592e2aSwizsynchronization, except when the resource is pending destruction.
221dd839657Spgoyette.Pp
222dd839657SpgoyetteSee
223dd839657Spgoyette.Xr psref 9 .
224dd839657Spgoyette.Ss Localcount mechanism
225dd839657SpgoyetteLocalcounts are used in the kernel to implement a medium-weight reference
22662592e2aSwizcounting mechanism.
22762592e2aSwizDuring normal operations, localcounts do not need
228dd839657Spgoyettethe interprocessor synchronization associated with
229dd839657Spgoyette.Xr atomic_ops 3
230dd839657Spgoyetteatomic memory operations, and (unlike
231dd839657Spgoyette.Xr psref 9 )
232dd839657Spgoyettelocalcount references can be held across sleeps and can migrate between
23362592e2aSwizCPUs.
23462592e2aSwizDraining a localcount requires more expensive interprocessor
235dd839657Spgoyettesynchronization than
236dd839657Spgoyette.Xr atomic_ops 3
237dd839657Spgoyette(similar to
238dd839657Spgoyette.Xr psref 9 ) .
239dd839657SpgoyetteAnd localcount references require eight bytes of memory per object per-CPU,
240dd839657Spgoyettesignificantly more than
241dd839657Spgoyette.Xr atomic_ops 3
242dd839657Spgoyetteand almost always more than
243dd839657Spgoyette.Xr psref 9 .
244dd839657Spgoyette.Pp
245dd839657SpgoyetteSee
246dd839657Spgoyette.Xr localcount 9 .
247bce1cecbSkamil.Ss Simple do-it-in-thread-context framework
248bce1cecbSkamilThe workqueue utility routines are provided to defer work which is needed to be
249bce1cecbSkamilprocessed in a thread context.
250bce1cecbSkamil.Pp
251bce1cecbSkamilSee
252bce1cecbSkamil.Xr workqueue 9 .
253bce1cecbSkamil.Sh USAGE
254d54d8289SsevanThe following table describes in which contexts the use of the
255bce1cecbSkamil.Nx
256d54d8289Ssevankernel interfaces are valid.
257d54d8289SsevanSynchronization primitives which are available in more than one context
258d54d8289Ssevancan be used to protect shared resources between the contexts they overlap.
259bce1cecbSkamil.Bl -column -offset indent \
260bce1cecbSkamil"xxxxxxxxxxxx " "xxxxxxx " "xxxxxxx " "xxxxxxx "
261bce1cecbSkamil.It Sy interface Ta Sy thread Ta Sy softirq Ta Sy hardirq
262bce1cecbSkamil.It Xr atomic_ops 3 Ta yes Ta yes Ta yes
263bce1cecbSkamil.It Xr condvar 9 Ta yes Ta partly Ta no
264bce1cecbSkamil.It Xr membar_ops 3 Ta yes Ta yes Ta yes
265bce1cecbSkamil.It Xr mutex 9 Ta yes Ta depends Ta depends
266bce1cecbSkamil.It Xr rwlock 9 Ta yes Ta yes Ta no
267bce1cecbSkamil.It Xr softint 9 Ta yes Ta yes Ta yes
268bce1cecbSkamil.It Xr spl 9 Ta yes Ta no Ta no
269bce1cecbSkamil.It Xr splraiseipl 9 Ta yes Ta no Ta no
270bce1cecbSkamil.It Xr pserialize 9 Ta yes Ta yes Ta no
271dd839657Spgoyette.It Xr psref 9 Ta yes Ta yes Ta no
272dd839657Spgoyette.It Xr localcount 9 Ta yes Ta yes Ta no
273bce1cecbSkamil.It Xr workqueue 9 Ta yes Ta yes Ta yes
274bce1cecbSkamil.El
275bce1cecbSkamil.Sh SEE ALSO
276bce1cecbSkamil.Xr atomic_ops 3 ,
277bce1cecbSkamil.Xr membar_ops 3 ,
278bce1cecbSkamil.Xr condvar 9 ,
279bce1cecbSkamil.Xr mutex 9 ,
280bce1cecbSkamil.Xr ras 9 ,
281bce1cecbSkamil.Xr rwlock 9 ,
282bce1cecbSkamil.Xr softint 9 ,
283bce1cecbSkamil.Xr spl 9 ,
284bce1cecbSkamil.Xr splraiseipl 9 ,
285bce1cecbSkamil.Xr workqueue 9
286bce1cecbSkamil.Sh HISTORY
287d54d8289SsevanInitial SMP support was introduced in
288bce1cecbSkamil.Nx 2.0
289d54d8289Ssevanand was designed with a giant kernel lock.
290bce1cecbSkamilThrough
291bce1cecbSkamil.Nx 4.0 ,
292bce1cecbSkamilthe kernel used spinlocks and a per-CPU interrupt priority level (the
293bce1cecbSkamil.Xr spl 9
294bce1cecbSkamilsystem).
295bce1cecbSkamilThese mechanisms did not lend themselves well to a multiprocessor environment
296bce1cecbSkamilsupporting kernel preemption.
297bce1cecbSkamilThe use of thread based (lock) synchronization was limited and the available
298bce1cecbSkamilsynchronization primitive (lockmgr) was inefficient and slow to execute.
299bce1cecbSkamil.Nx 5.0
300d54d8289Ssevanintroduced massive performance improvements on multicore hardware
301d54d8289Ssevanby Andrew Doran.
302d54d8289SsevanThis work was sponsored by The
303bce1cecbSkamil.Nx
304bce1cecbSkamilFoundation.
305bce1cecbSkamil.Pp
306d54d8289SsevanA
307d54d8289Ssevan.Nm
308d54d8289Ssevanmanual first appeared in
309bce1cecbSkamil.Nx 8.0
310bce1cecbSkamiland was inspired by the corresponding
311d54d8289Ssevan.Nm
312d54d8289Ssevanmanuals in
313bce1cecbSkamil.Fx
314bce1cecbSkamiland
315bce1cecbSkamil.Dx .
316bce1cecbSkamil.Sh AUTHORS
317d54d8289Ssevan.An Kamil Rytarowski Aq Mt kamil@NetBSD.org .
318