Chapter 2. Locking Notes
Table of Contents
This chapter is maintained by the FreeBSD SMP Next Generation Project.
This document outlines the locking used in the FreeBSD kernel to permit effective multi-processing within the kernel. Locking can be achieved via several means. Data structures can be protected by mutexes or lockmgr(9) locks. A few variables are protected simply by always using atomic operations to access them.
2.1. Mutexes
A mutex is simply a lock used to guarantee mutual exclusion. Specifically, a mutex may only be owned by one entity at a time. If another entity wishes to obtain a mutex that is already owned, it must wait until the mutex is released. In the FreeBSD kernel, mutexes are owned by processes.
Mutexes may be recursively acquired, but they are intended to be held for a short period of time. Specifically, one may not sleep while holding a mutex. If you need to hold a lock across a sleep, use a lockmgr(9) lock.
Each mutex has several properties of interest:
- Variable Name
The name of the struct mtx variable in the kernel source.
- Logical Name
The name of the mutex assigned to it by
mtx_init
. This name is displayed in KTR trace messages and witness errors and warnings and is used to distinguish mutexes in the witness code.- Type
The type of the mutex in terms of the
MTX_*
flags. The meaning for each flag is related to its meaning as documented in mutex(9).MTX_DEF
A sleep mutex
MTX_SPIN
A spin mutex
MTX_RECURSE
This mutex is allowed to recurse.
- Protectees
A list of data structures or data structure members that this entry protects. For data structure members, the name will be in the form of
structure name
.member name
.- Dependent Functions
Functions that can only be called if this mutex is held.
Variable Name | Logical Name | Type | Protectees | Dependent Functions |
---|---|---|---|---|
sched_lock | "sched lock" |
|
|
|
vm86pcb_lock | "vm86pcb lock" |
|
|
|
Giant | "Giant" |
| nearly everything | lots |
callout_lock | "callout lock" |
|
|
2.2. Shared Exclusive Locks
These locks provide basic reader-writer type functionality and may be held by a sleeping process. Currently they are backed by lockmgr(9).
Variable Name | Protectees |
---|---|
|
|
|
|
2.3. Atomically Protected Variables
An atomically protected variable is a special variable that is not protected by an explicit lock. Instead, all data accesses to the variables use special atomic operations as described in atomic(9). Very few variables are treated this way, although other synchronization primitives such as mutexes are implemented with atomically protected variables.
mtx
.mtx_lock
Last modified on: March 9, 2024 by Danilo G. Baio