Conceptual
Login

Protecting Driver Data with the Kernel Mutex

Two processes can call into your driver at the same moment on two processors, so shared state needs a lock. The kernel's Rust mutex does not sit beside the data it protects, it contains it, and the only way to reach the data is to lock and receive a guard whose existence is the proof the lock is held; when the guard goes out of scope the lock is released. That makes the two classic C bugs, touching shared data without the lock and returning from a function while still holding it, impossible to write. A mutex may put the caller to sleep, so it is only legal in normal context.

Questions this Concept answers

  • Why does the kernel's Rust mutex contain the data it protects instead of sitting beside it?