Skip to content

Commit 33321db

Browse files
committed
Replace namespaces+cgroups with Landlock+BPF LSM+prlimit
Signed-off-by: Cong Wang <cwang@multikernel.io>
1 parent 6bd6ab9 commit 33321db

28 files changed

Lines changed: 1577 additions & 1388 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ build/
88
*.so
99
.pytest_cache/
1010
.mypy_cache/
11+
12+
# Generated BPF headers (regenerate via: make -C src/branching/process/bpf vmlinux.h)
13+
src/branching/process/bpf/vmlinux.h

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ with ws.branch("strategy_a") as a:
359359
### Process isolation
360360

361361
For untrusted or crash-prone agent code, `BranchContext` runs each task in
362-
a sandboxed child process with its own filesystem view. No root needed.
362+
a sandboxed child process confined to its own branch via Landlock. No root
363+
needed.
363364

364365
```python
365366
from branching import BranchContext
@@ -392,14 +393,14 @@ outcome = Speculate(candidates, isolate_processes=True, timeout=60)(ws)
392393

393394
### Resource limits
394395

395-
Constrain per-branch memory and CPU via cgroup v2. Passing
396+
Constrain per-branch memory and CPU via setrlimit(2). Passing
396397
`resource_limits` to any pattern automatically enables process isolation -
397-
each branch runs in a forked child with cgroup enforcement.
398+
each branch runs in a forked child with limits enforced.
398399

399400
```python
400401
from branching import ResourceLimits, BestOfN
401402

402-
limits = ResourceLimits(memory=512 * 1024 * 1024, cpu=0.5) # 512 MB, 50% CPU
403+
limits = ResourceLimits(memory=512 * 1024 * 1024, cpu_time=30) # 512 MB, 30s CPU
403404

404405
outcome = BestOfN(candidates, resource_limits=limits)(ws)
405406
```
@@ -495,13 +496,20 @@ first-winner-commit semantics.
495496

496497
You just create a `Workspace` pointed at a mounted BranchFS path.
497498

498-
Process isolation (`BranchContext`) uses unprivileged Linux user namespaces
499-
to give each child its own filesystem view. No root required - works on any
500-
Linux distribution with `unprivileged_userns_clone=1` (the default).
501-
502-
Resource limits (`ResourceLimits`) use cgroup v2 to enforce per-branch
503-
memory and CPU constraints. Each branch gets its own cgroup scope with
504-
limits applied before the child process starts. Requires cgroup v2 with
505-
the memory and cpu controllers enabled (the default on modern systemd
506-
distributions).
499+
Process isolation (`BranchContext`) uses fork + Landlock LSM + BPF LSM to
500+
sandbox each child process. No namespaces, no cgroups, no root required:
501+
502+
- **Landlock LSM** (Linux 5.13+) confines each child to its own branch.
503+
The child can read the filesystem outside the workspace but can only write
504+
under its branch path. Sibling branches and the mount root are denied.
505+
`LANDLOCK_ACCESS_FS_REFER` is included in the handled set so that
506+
rename/link across the branch boundary is blocked.
507+
- **BPF LSM** provides inescapable process tracking. All descendants of a
508+
branched process inherit the branch ID, enabling atomic teardown of an
509+
entire branch's process tree. Requires `CONFIG_BPF_LSM=y` and
510+
`lsm=...,bpf,...` in the kernel command line.
511+
- **setrlimit(2)** enforces per-branch resource limits (memory via
512+
`RLIMIT_AS`, CPU time via `RLIMIT_CPU`, process count via `RLIMIT_NPROC`).
513+
Lightweight alternative to cgroups -- no cgroupfs infrastructure needed,
514+
limits are inherited by children on fork.
507515

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ branching = "cli:main"
2626
[tool.setuptools.packages.find]
2727
where = ["src"]
2828

29+
[tool.setuptools.package-data]
30+
"branching.process.bpf" = ["*.bpf.o"]
31+
2932
[tool.pytest.ini_options]
3033
testpaths = ["tests"]

src/branching/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
BranchContext - Unified branching for speculative execution.
44
55
Supports filesystem branching (BranchFS FUSE), process branching
6-
(fork + namespaces), and AI agent integration patterns (speculation,
7-
best-of-N, reflexion, tree-of-thoughts).
6+
(fork + Landlock + BPF LSM), and AI agent integration patterns
7+
(speculation, best-of-N, reflexion, tree-of-thoughts).
88
99
Layers are loaded lazily — importing only what you need avoids pulling
1010
in unrelated dependencies:
@@ -35,7 +35,6 @@
3535
MountError,
3636
ProcessBranchError,
3737
ForkError,
38-
NamespaceError,
3938
MemoryBranchError,
4039
)
4140

@@ -68,7 +67,6 @@
6867
"MountError",
6968
"ProcessBranchError",
7069
"ForkError",
71-
"NamespaceError",
7270
"MemoryBranchError",
7371
]
7472

0 commit comments

Comments
 (0)