freenode
Kernel & Low-Level

BPF proposal would stop a failing program and free what it holds

Kumar Kartikeya Dwivedi's cancellation work unifies timeouts, faults, and throws, but early review says some programs may no longer load.

Kumar Kartikeya Dwivedi has proposed a single way for the Linux BPF runtime to stop a program that has entered an error state, release everything it acquired, and return a failure value chosen for its type.

Those cases are handled separately today, and often leave the program running in a half-finished state. A loop that overruns its time slice only leaves the loop, so the code after it must be safe without the loop's result. A faulting load from BPF arena memory reads zero and execution continues. A call to bpf_throw() is rejected if the program still holds anything. Recursion checks do not halt the program at all. Dwivedi wrote that the runtime needs a primitive "to halt the execution of the program at a given point, reclaim all resources it has acquired during its execution, and optionally, eject it from the system when supported."

Cancellation sites would cover timed loops, recursive stack checks, arena accesses, bpf_throw(), and calls into functions that can themselves be cancelled. The verifier would emit the cleanup. Each stack frame would release what it holds, then the program would return a value such as a packet drop for traffic control or an error for an LSM hook. An exception callback could override that value. Authors of struct_ops programs could choose the return per member: a cancelled queueing-discipline enqueue would report the packet dropped rather than successfully queued, and a cancelled init would fail so the kernel destroys the half-built qdisc instead of attaching it.

Automated review on the BPF list the same day found the rules are not yet safe to land as written. Leaving hidden cleanup words out of the private-stack decision, and counting held references earlier at call sites, can reject programs that load today. The claim that a cancelled HID BPF program returning zero behaves as if no program were attached looks wrong for descriptor fixup, where the core may already hold a partly rewritten report descriptor. Verifier logs can blame a missing cleanup path when the real cause is a node type's destructors. On x86, a new unwind stub can let an optimized kprobe on the previous function's tail corrupt the return target. A later change in the series fixes that, and the review asked that the fix be folded in so a bisect does not hit the broken stub.