1667ff97eSMarkus Pfeiffer.\" 2667ff97eSMarkus Pfeiffer.\" Copyright (c) 2014 Markus Pfeiffer 3667ff97eSMarkus Pfeiffer.\" All rights reserved. 4667ff97eSMarkus Pfeiffer.\" 5667ff97eSMarkus Pfeiffer.\" Redistribution and use in source and binary forms, with or without 6667ff97eSMarkus Pfeiffer.\" modification, are permitted provided that the following conditions 7667ff97eSMarkus Pfeiffer.\" are met: 8667ff97eSMarkus Pfeiffer.\" 1. Redistributions of source code must retain the above copyright 9667ff97eSMarkus Pfeiffer.\" notice, this list of conditions and the following disclaimer. 10667ff97eSMarkus Pfeiffer.\" 2. Redistributions in binary form must reproduce the above copyright 11667ff97eSMarkus Pfeiffer.\" notice, this list of conditions and the following disclaimer in the 12667ff97eSMarkus Pfeiffer.\" documentation and/or other materials provided with the distribution. 13667ff97eSMarkus Pfeiffer.\" 14667ff97eSMarkus Pfeiffer.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15667ff97eSMarkus Pfeiffer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16667ff97eSMarkus Pfeiffer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17667ff97eSMarkus Pfeiffer.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18667ff97eSMarkus Pfeiffer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19667ff97eSMarkus Pfeiffer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20667ff97eSMarkus Pfeiffer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21667ff97eSMarkus Pfeiffer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22667ff97eSMarkus Pfeiffer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23667ff97eSMarkus Pfeiffer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24667ff97eSMarkus Pfeiffer.\" SUCH DAMAGE. 25667ff97eSMarkus Pfeiffer.\" 26667ff97eSMarkus Pfeiffer.\" 27667ff97eSMarkus Pfeiffer.Dd June 5, 2014 28667ff97eSMarkus Pfeiffer.Dt LOCKING 9 29667ff97eSMarkus Pfeiffer.Os 30667ff97eSMarkus Pfeiffer.Sh NAME 31667ff97eSMarkus Pfeiffer.Nm locking 32667ff97eSMarkus Pfeiffer.Nd introduction to kernel locking primitives 33667ff97eSMarkus Pfeiffer.Sh DESCRIPTION 34667ff97eSMarkus PfeifferThe 35667ff97eSMarkus Pfeiffer.Dx 36667ff97eSMarkus Pfeifferkernel provides several locking and synchronisation primitives, each with 37c04308e8SMarkus Pfeifferdifferent characteristics and purposes. 38c04308e8SMarkus PfeifferThis manpage aims at giving an 39f0f0bdbdSMarkus Pfeifferoverview of the available locking primitives and their use cases, as well 40f0f0bdbdSMarkus Pfeifferas pointers towards further information. 41f0f0bdbdSMarkus Pfeiffer.Ss Condition Variables 42c04308e8SMarkus PfeifferCondition variables are used to wait for conditions to occur. 43c04308e8SMarkus PfeifferIn 44667ff97eSMarkus Pfeiffer.Dx 45f0f0bdbdSMarkus Pfeiffercondition variables use a 46f0f0bdbdSMarkus Pfeiffer.Xr spinlock 9 47f0f0bdbdSMarkus Pfeifferinternally. 48c04308e8SMarkus PfeifferThreads that wait on a condition variable are called waiters. 49f0f0bdbdSMarkus PfeifferEither just one or all waiters can be notified of changes to a 50f0f0bdbdSMarkus Pfeiffercondition variable. 51c04308e8SMarkus PfeifferA condition variable can 52c04308e8SMarkus Pfeiffer.Xr tsleep_interlock 9 53c04308e8SMarkus Pfeifferwhen given a 54c04308e8SMarkus Pfeiffer.Xr lockmgr 9 55f0f0bdbdSMarkus Pfeifferlock to avoid missing changes to it, or use regular 56c04308e8SMarkus Pfeiffer.Xr tsleep 9 . 57f0f0bdbdSMarkus Pfeiffer.Pp 58667ff97eSMarkus PfeifferSee 59*94bb8ac5SMarkus Pfeiffer.Xr condvar 9 . 60f0f0bdbdSMarkus Pfeiffer.Ss Critical Sections 61667ff97eSMarkus PfeifferA critical section changes the priority of the current thread to 62c04308e8SMarkus Pfeiffer.Dv TDPRIT_CRIT , 63c04308e8SMarkus Pfeiffereffectively avoiding preemption of the thread. 64667ff97eSMarkus PfeifferCritical sections are a per-cpu primitive, and there is no synchronisation 65667ff97eSMarkus Pfeifferor locking between CPUs. 66f0f0bdbdSMarkus Pfeiffer.Pp 67667ff97eSMarkus PfeifferSee 68c04308e8SMarkus Pfeiffer.Xr crit_enter 9 . 69f0f0bdbdSMarkus Pfeiffer.Ss Lockmgr Locks 70c04308e8SMarkus Pfeiffer.Xr Lockmgr 9 71c04308e8SMarkus Pfeifferlocks are the kitchen sink locking primitive for the 72c04308e8SMarkus Pfeiffer.Dx 73f0f0bdbdSMarkus Pfeifferkernel, and the most heavyweight locking mechanism. 74f0f0bdbdSMarkus Pfeiffer.Xr lockmgr 9 75f0f0bdbdSMarkus Pfeifferlocks can be shared/exclusive and recursive. 76c04308e8SMarkus PfeifferLockmgr locks should be used for 77c04308e8SMarkus Pfeiffer.Fx 78c04308e8SMarkus Pfeiffercompatibility when porting drivers that use 79c04308e8SMarkus Pfeiffer.Fx Ap s 80c04308e8SMarkus Pfeiffermutexes. 81f0f0bdbdSMarkus Pfeiffer.Pp 82c04308e8SMarkus PfeifferSee 83*94bb8ac5SMarkus Pfeiffer.Xr lockmgr 9 . 84f0f0bdbdSMarkus Pfeiffer.Ss LWKT Messages 85f0f0bdbdSMarkus PfeifferLWKT messages can be used to pass messages between light weight kernel 86f0f0bdbdSMarkus Pfeifferthreads in the 87f0f0bdbdSMarkus Pfeiffer.Dx 88f0f0bdbdSMarkus Pfeifferkernel. 89f0f0bdbdSMarkus PfeifferLWKT mesages are sent to message ports. Every light weight kernel thread 90f0f0bdbdSMarkus Pfeifferpossesses a message port, but more can be created if necessary. 91f0f0bdbdSMarkus Pfeiffer.Pp 92f0f0bdbdSMarkus PfeifferSee 93*94bb8ac5SMarkus Pfeiffer.Xr msgport 9 . 94f0f0bdbdSMarkus Pfeiffer.Ss LWKT Serializers 95f0f0bdbdSMarkus PfeifferLWKT serializers provide a fast locked-bus-cycle-based serialization 96f0f0bdbdSMarkus Pfeifferfacility. 97f0f0bdbdSMarkus PfeifferThey are used to serialize access to hardware and other subsystems. 98f0f0bdbdSMarkus PfeifferSerializers were designed to provide low level exclusive locks. 99f0f0bdbdSMarkus Pfeiffer.Pp 100f0f0bdbdSMarkus PfeifferSee 101f0f0bdbdSMarkus Pfeiffer.Xr serializer 9 . 102f0f0bdbdSMarkus Pfeiffer.Ss LWKT Tokens 103f0f0bdbdSMarkus PfeifferLWKT tokens use 104c04308e8SMarkus Pfeiffer.Xr atomic_cmpset 9 105f0f0bdbdSMarkus Pfeifferinternally and are integrated with the LWKT scheduler. 106c04308e8SMarkus PfeifferThe scheduler takes care of acquiring a token before 107667ff97eSMarkus Pfeifferrescheduling, so a thread will not be run unless all tokens for it can be 108667ff97eSMarkus Pfeifferacquired. 109667ff97eSMarkus PfeifferTokens are not owned by a thread, but by the CPU, and threads are only given 110667ff97eSMarkus Pfeifferreferences to tokens. 111*94bb8ac5SMarkus Pfeiffer.Pp 112c04308e8SMarkus PfeifferSee 113*94bb8ac5SMarkus Pfeiffer.Xr token 9 . 114f0f0bdbdSMarkus Pfeiffer.Ss MPLOCK 115667ff97eSMarkus PfeifferThe mplock is an API wrapper for the MP token. The use of this should be 116667ff97eSMarkus Pfeifferavoided at all cost, because there is only one MP token for the whole system. 117f0f0bdbdSMarkus Pfeiffer.Ss MTX Mutexes 118c04308e8SMarkus PfeifferMtx mutexes are a locking primitive that is based around 119c04308e8SMarkus Pfeiffer.Xr atomic_cmpset_int 9 120c04308e8SMarkus Pfeifferinstead of spinlocks. 121c04308e8SMarkus PfeifferThey are much faster and use less memory than 122c04308e8SMarkus Pfeiffer.Xr lockmgr 9 123c04308e8SMarkus Pfeifferlocks. 124c04308e8SMarkus PfeifferMtx mutexes can always be recursive, shared/exclusive and can be held 125c04308e8SMarkus Pfeifferacross blocking calls and sleeps. 126c04308e8SMarkus PfeifferThey are also capable of passing ownership directly to a new owner 127c04308e8SMarkus Pfeifferwithout wakeup. 128*94bb8ac5SMarkus Pfeiffer.Pp 129667ff97eSMarkus PfeifferSee 130c04308e8SMarkus Pfeiffer.Xr mutex 9 . 131f0f0bdbdSMarkus Pfeiffer.Ss Spinlocks 132c04308e8SMarkus PfeifferSpinlocks employ a busy wait loop to acquire a lock. 133c04308e8SMarkus PfeifferThis means that this type of lock is very lightweight, 134c04308e8SMarkus Pfeifferbut should only be held for a very short time, since all contenders 135c04308e8SMarkus Pfeifferwill be spinning and not sleeping. 136c04308e8SMarkus PfeifferNo wakeup is necessary, because a waiter will be spinning already. 137667ff97eSMarkus PfeifferIf a thread tries to sleep while holding a spinlock, the kernel will panic. 138667ff97eSMarkus PfeifferSpinlocks cannot recurse. 139c04308e8SMarkus Pfeiffer.Pp 140c04308e8SMarkus PfeifferThey are mainly used to protect kernel structures, and to 141c04308e8SMarkus Pfeifferimplement higher level locking primitives. 142*94bb8ac5SMarkus Pfeiffer.Pp 143c04308e8SMarkus PfeifferSee 144c04308e8SMarkus Pfeiffer.Xr spinlock 9 . 145c04308e8SMarkus Pfeiffer.Sh SEE ALSO 146c04308e8SMarkus Pfeiffer.Xr atomic 9 , 147c04308e8SMarkus Pfeiffer.Xr condvar 9 , 148c04308e8SMarkus Pfeiffer.Xr crit_enter 9 , 149c04308e8SMarkus Pfeiffer.Xr lockmgr 9 , 150c04308e8SMarkus Pfeiffer.Xr mutex 9 , 151c04308e8SMarkus Pfeiffer.Xr serializer 9 , 152c04308e8SMarkus Pfeiffer.Xr spinlock 9 , 153c04308e8SMarkus Pfeiffer.Xr tsleep 9 154667ff97eSMarkus Pfeiffer.Sh AUTHORS 155667ff97eSMarkus Pfeiffer.An -nosplit 156667ff97eSMarkus PfeifferThis manual page was written by 157667ff97eSMarkus Pfeiffer.An Markus Pfeiffer Aq Mt markus.pfeiffer@morphism.de , 158667ff97eSMarkus Pfeifferbased on comments by various 159667ff97eSMarkus Pfeiffer.Dx 160667ff97eSMarkus Pfeifferauthors. 161