Organize companion code and materials by book part: part-01/ Foundations part-02/ Kernel & driver fundamentals part-03/ Character & block devices part-04/ Networking & storage part-05/ Buses & advanced topics part-06/ Debugging & performance part-07/ Real-world drivers appendices/ Supplementary material README.md Overview and build instructions
3.5 KiB
myfirst Locking Strategy
Chapter 11 driver, version 0.5-concurrency.
Overview
The driver uses a single sleep mutex (sc->mtx) to serialize all
accesses to the circular buffer, the sleep channel, the selinfo
structures, and several per-softc fields. Byte counters are migrated
to counter(9) per-CPU counters in Stage 3 and protected there by
the counter(9) API itself.
What sc->mtx Protects
sc->cb(the circular buffer's internal state:cb_head,cb_used,cb_data).sc->open_count,sc->active_fhs.sc->is_attached(writes; reads at handler entry may be unprotected as an optimization, re-checked after every sleep).sc->rsel,sc->wsel(indirectly:selrecordis called inside a critical section;selwakeupis called with the mutex dropped).
Lock-Free Fields (Stage 3 and later)
sc->bytes_read,sc->bytes_written:counter_u64_t. Updates viacounter_u64_add; reads viacounter_u64_fetch.
Locking Discipline
- Acquire with
MYFIRST_LOCK(sc)and release withMYFIRST_UNLOCK(sc). These are macros aroundmtx_lockandmtx_unlockrespectively; they can be changed to a different lock class without touching call sites. - Wait with
mtx_sleep(&sc->cb, &sc->mtx, PCATCH, wmesg, 0). Wake withwakeup(&sc->cb). - NEVER hold
sc->mtxacrossuiomove(9),copyin(9),copyout(9),selwakeup(9), orwakeup(9). Each of these may sleep or take other locks. - All
myfirst_buf_*andmyfirst_wait_*helpers assertMYFIRST_ASSERT(sc)at entry (compiles tomtx_assert(MA_OWNED)).
Known Non-Locked Accesses
sc->is_attached (reads at handler entry)
Unprotected plain read. Safe because:
- A stale "true" is re-checked after every sleep via
if (!sc->is_attached) return (ENXIO). - A stale "false" causes the handler to return ENXIO early, which is also what it would do with a fresh false.
sc->open_count, sc->active_fhs (sysctl reads)
Unprotected plain loads. Safe on amd64 and arm64 (aligned 64-bit loads are atomic). Acceptable on i386 and armv7 because the torn read, if it ever happened, would produce a single bad statistic with no correctness impact.
Lock Order
Currently, the driver holds only sc->mtx. There are no lock-order
rules. When Chapter 12 introduces additional locks (sx(9) is a
likely candidate), the order will be documented here as:
sc->mtx -> sc->other_lock
meaning: a thread holding sc->mtx may acquire sc->other_lock,
but not the reverse.
Wait Channels
&sc->cb: signals that the buffer state has changed (space or data has become available). Used by both read and write paths.
Rules Summary
- Never sleep while holding
sc->mtxexcept viamtx_sleep, which atomically drops and reacquires. - Never call
uiomove,copyin,copyout,selwakeup, orwakeupwhile holdingsc->mtx. - Every
cbuf_*call must happen withsc->mtxheld. - The detach path clears
sc->is_attachedunder the mutex and then wakes any sleepers before destroying the mutex. counter_u64_adddoes not require the mutex.
History
- 0.5-concurrency (Chapter 11, Stage 2): MYFIRST_LOCK/UNLOCK/ASSERT macros; explicit version string.
- 0.5-counter9 (Chapter 11, Stage 3): byte counters migrated to counter(9).
- 0.5-kasserts (Chapter 11, Stage 5): KASSERT calls added for every invariant on the cbuf and helper paths.
- 0.4-poll-refactor (Chapter 10, Stage 4): wait helpers; locking strategy documented inline.
- Earlier versions: see Chapter 10 history.