|
|
Log in / Subscribe / Register

Cleaning up after BPF exceptions

Cleaning up after BPF exceptions

Posted Apr 16, 2024 19:44 UTC (Tue) by kkdwivedi (guest, #130744)
In reply to: Cleaning up after BPF exceptions by roc
Parent article: Cleaning up after BPF exceptions

Your criticism for this approach is fair. It's not an ideal situation. The above is one of the reasons why I took a step back and tried to rethink the merging logic for paths with different contents in the same stack slot.

The reason this is unlikely (I don't want to use impossible, that's too strong a word and it is certainly possible to concoct such a scenario) to occur in practice is because a valid program in the path of execution which does not throw, would need to release the different kernel resources at the same stack slot. In such a case, there would be a path condition known to the verifier such that it can see the right release function being called for that kernel resource. While such a program can be constructed, it is unlikely the compiler emits such code. I tried making different kernel helpers cancellation points (which amount to throwing an exception and generating frame descriptors) in the selftest suite (which is a reasonable representative set of BPF programs used in production, and tested in CI), and didn't encounter this.

However, if you look at the final reply to Eduard, we eventually decided to resolve the merge conflicts on the same stack slot, instead of rejecting the program, by spilling every unique acquisition of a conflicting resource (tied to a single program counter) to a unique stack slot in a stack frame. All such stack slots would need to be zeroed on entry, but it would mean that when such a situation does occur in the future, the resource would be saved to a unique location and merge conflicts in the same stack slot would be resolved by instead depending on the unique stack slot's value, ignoring other copies of the same resource elsewhere in the stack.

This is a good middle path between the two extremes (actively keeping track of acquired resources on acquisition and release, which has a non-zero cost linear in the number of resource acquisitions and releases on a given path, and keeping track of resources at any program point statically, placing some constraints on program behaviour), and would work for cases where we cannot ensure resource cleanup by just depending on the verifier's knowledge of the state of the stack and registers at a given program point. It mitigates conflicts in the same stack slot for the uncommon cases, while not requiring every resource to be actively accounted for at runtime in the common case.

I am planning to pursue this option in the next revision of this patch set, alleviating these concerns.


to post comments


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds