duende-mlock
Memory locking for swap-critical daemons.
Problem: Swap Deadlock (DT-007)
When a daemon serves as a swap device (e.g., trueno-ublk), a deadlock occurs if:
- Kernel needs memory → initiates swap-out to the daemon
- Daemon needs memory to process I/O
- Kernel tries to swap daemon's pages → to the same daemon
- Deadlock: daemon blocked waiting for itself
INFO: task trueno-ublk:59497 blocked for more than 122 seconds.
task:trueno-ublk state:D (uninterruptible sleep)
__swap_writepage+0x111/0x1a0
Solution
use lock_all;
API
Quick Start
use ;
// Lock all memory
let status = lock_all?;
assert!;
// Verify
assert!;
println!;
Configuration
use ;
let config = builder
.current // Lock existing pages
.future // Lock future allocations
.required // Don't fail if mlock fails
.onfault // Lock immediately (not on fault)
.build;
match lock_with_config
Error Handling
use ;
match lock_all
Platform Support
| Platform | Support | Notes |
|---|---|---|
| Linux | Full | Requires CAP_IPC_LOCK or root |
| macOS | Limited | Requires entitlements |
| Windows | None | Returns Unsupported |
| WASM | None | Returns Unsupported |
Container Requirements
Docker
docker-compose.yml
services:
daemon:
cap_add:
- IPC_LOCK
ulimits:
memlock:
soft: -1
hard: -1
Kubernetes
apiVersion: v1
kind: Pod
spec:
containers:
- name: daemon
securityContext:
capabilities:
add:
Systemd
[Service]
CapabilityBoundingSet=CAP_IPC_LOCK
AmbientCapabilities=CAP_IPC_LOCK
LimitMEMLOCK=infinity
Minimal Dependencies
This crate has minimal dependencies for maximum compatibility:
libc- Formlockall(2)andmunlockall(2)syscalls- No async runtime (no tokio)
- No heavy frameworks
License
MIT OR Apache-2.0