blog.rust.gay

Rust for Linux drops driver unbind callbacks in favor of destructors

On November 12, 2026, Danilo Krummrich posted a seven-patch series to the Linux kernel mailing list titled "rust: treewide: remove driver unbind callbacks." The series removes unbind() from the PCI, platform, auxiliary, serdev, and I2C driver traits, and disconnect() from the USB driver trait. It moves the affected sample teardown into destructors and updates the driver documentation.

The diffstat is small in lines and large in implication: 11 files changed, 59 insertions, and 217 deletions. Most of the deleted code is boilerplate that existed to call a teardown function at the right time. The series argues that the driver model's native Rust lifetimes already provide the guarantee that the callback was there to enforce, so the callback can go away.

It is important to say what this is not. It is a patch series under review, not a merged kernel change. The series has to survive review, and any change to driver lifecycle semantics deserves scrutiny. But the design argument is worth understanding, because it shows how Rust for Linux is moving from "write drivers in Rust" to "let Rust's ownership model simplify the driver model itself."

What the patch series does

The series is a treewide cleanup with one idea behind it. In the C driver model, a driver provides a probe or bind function that claims a device and a remove or unbind function that releases it. The kernel calls the teardown function when the device is unbound, the module is removed, or the system shuts down. The pairing is a convention enforced by driver authors, not by the type system.

Rust for Linux's driver abstractions wrap that model. A Rust driver implements a trait, and the framework calls into it when a device is bound or unbound. Until this series, the trait included an explicit unbind() method (or disconnect() for USB) that the driver author implemented to release resources.

The series removes those methods. The argument, as stated in the cover letter, is that the driver model uses native Rust lifetimes, allowing driver data to retain device resources and a bound device reference for use in its destructor. If the driver's data owns the resources, then dropping the data releases them. A separate teardown callback is redundant.

The patches follow the same pattern across subsystems: remove the trait method, move the sample driver's teardown into a Drop implementation, and update the documentation to explain the new expectation. The affected files include rust/kernel/auxiliary.rs, driver.rs, i2c.rs, pci.rs, platform.rs, serdev.rs, and usb.rs, plus the sample drivers for I2C, PCI, and USB.

The cover letter is explicit that this is a treewide change, not a single-subsystem experiment. That matters: it means the maintainers are trying to establish one lifecycle model across heterogeneous device types rather than letting each subsystem invent its own.

Why teardown callbacks are a problem

The case against explicit teardown callbacks is not that they are impossible to use. It is that they create a class of bugs that the compiler cannot catch.

A teardown callback has to run exactly once, at the right time, after the driver has stopped using the device but before the device is gone. If it runs too early, the driver may touch freed resources. If it runs too late, the device may be gone while the driver still holds a reference. If it does not run at all, resources leak. If it runs twice, the driver may free the same memory twice.

In C, those failure modes are managed by discipline, documentation, and review. The kernel has a long history of bugs in this area, and the fixes are often subtle: reorder a cleanup step, add a flag, hold a lock, or move a call. The callback model is workable, but it puts the burden on every driver author to get the ordering right in every error path.

Rust's ownership model changes the shape of the problem. If a resource is owned by a value, the resource is released when the value is dropped. The compiler enforces that the value is not used after it is dropped, and the borrow checker prevents aliases that could outlive it. The destructor runs on normal return, on early return, and on panic unwinding. The author does not have to remember to call it.

That is the theory. The patch series is an attempt to make the driver model match the theory. If the driver's data owns the device resources and the bound device reference, then dropping the data is the teardown. There is no separate callback to forget, and no ordering to get wrong in the common case.

The sample changes show what this looks like in practice. A driver that previously implemented unbind() to release a resource now implements Drop for its driver data. The framework drops the data when the device is unbound, and the destructor runs. The code is shorter because the callback's body moves into a destructor that the compiler knows to call.

Lifetimes, Drop, and the driver model

The key phrase in the cover letter is "native Rust lifetimes." The driver model can retain a bound device reference for use in the destructor. That means the destructor can safely call into the device or the bus because the reference is guaranteed to be valid for the duration of the drop.

That guarantee is not trivial. A device can be unbound while a driver still holds a reference to it. The kernel's reference counting and the driver core's unbind path have to ensure that the device stays alive until the driver's destructor finishes. The series is arguing that the Rust abstractions already encode that guarantee, so the explicit callback is unnecessary.

There is a subtlety here that the cover letter does not spell out but the review will have to settle: the difference between "the driver's data is dropped" and "the device is unbound" is not always a single event. A device can be unbound, rebound, or removed while the system is running. A driver can have multiple devices. A driver can have auxiliary devices that depend on a parent. The destructor model has to handle all of those cases without introducing a new ordering bug.

The series' answer is to let ownership do the work. If the driver data owns the resources, the resources are released when the data is dropped, regardless of which event triggered the drop. The framework is responsible for dropping the data at the right point in the unbind sequence. The driver author is responsible for making the destructor correct.

That division of responsibility is the real change. In the callback model, the driver author is responsible for both the timing and the content of teardown. In the destructor model, the framework owns the timing and the driver author owns the content. That is a better split because the framework can enforce the timing once, instead of every driver reimplementing it.

The hard parts: ordering and device references

The series is not risk-free, and the review discussion will likely focus on two areas.

The first is ordering. In a kernel, teardown order matters. A driver may depend on a parent device, a bus, a clock, a regulator, or another driver. If the destructor runs before a dependency is released, the driver may touch something that is already gone. If it runs after, the driver may hold a resource longer than necessary. The callback model made the order explicit; the destructor model makes it implicit in the ownership graph. The review has to confirm that the implicit order matches the required order in every case.

The second is the device reference. The cover letter says driver data can retain a bound device reference for use in the destructor. That is a strong statement, and it depends on the reference being valid until the destructor completes. If the reference is dropped too early, the destructor can use a freed device. If it is held too long, the device cannot be released. The series has to show that the lifetime is correct for hot-unplug, module removal, and error paths, not just the happy path.

There is also the question of existing drivers. The series changes the trait, so every in-tree Rust driver that implements unbind() or disconnect() has to be updated. The sample drivers are updated in the series, but out-of-tree drivers will need a migration path. The documentation patch is meant to explain the new model, but documentation is not a compatibility shim. Drivers that rely on the old callback will not compile against the new trait, which is a deliberate breaking change.

Those risks do not invalidate the idea. They are the normal costs of a lifecycle refactor. The reason to pay them is that the resulting model is simpler and harder to get wrong. The reason to review carefully is that "harder to get wrong" is not the same as "impossible to get wrong."

What this says about Rust for Linux

Rust support has been upstream in the Linux kernel since the 6.1 cycle, but the project has spent much of its time building abstractions: safe wrappers for memory allocation, locking, device access, and driver registration. This series is a sign that the abstractions are mature enough to change the driver model, not just wrap it.

That is a meaningful milestone. Wrapping C APIs in Rust is valuable, but it does not automatically make the kernel's design safer. The real benefit comes when Rust's ownership model lets the kernel remove a class of manual bookkeeping. Removing unbind() is a small example, but it points in that direction: the driver trait becomes simpler because the language can express the lifecycle guarantee directly.

The series also shows the shape of Rust for Linux's influence. The change is not "rewrite the driver core in Rust." It is "make the Rust driver abstraction use Rust's strengths, and let the C side keep its model." That is a pragmatic approach, and it is probably the only one that can work in a kernel with decades of C code.

For Rust developers outside the kernel, the series is a useful case study in API design. The question it asks is: if the language can enforce a lifecycle invariant, should the API still expose a manual callback for it? The answer here is no, and the reasoning is applicable to user-space libraries as well. A callback that exists only to be called at the right time is a candidate for replacement by ownership and Drop.

What to watch

The first thing to watch is the review. The series touches several subsystems, and the maintainers of each will have opinions about ordering, device references, and migration. A revised series is likely before a merge.

The second is the migration of existing drivers. The in-tree samples are updated, but the kernel has other Rust drivers and out-of-tree drivers that use the old traits. Watch for guidance on how those drivers should move to destructors, and for any compatibility helpers.

The third is the documentation. The series includes a documentation patch, and the quality of that patch will determine whether new driver authors understand the model. A destructor-based lifecycle is simpler, but it is not obvious to someone coming from C. The docs need to explain when the destructor runs, what it can safely access, and what it must not assume.

The fourth is the effect on other subsystems. If the series lands, it establishes a pattern that other Rust abstractions may follow: remove explicit teardown callbacks where ownership can do the job. Watch for similar changes in other parts of the Rust for Linux tree.

The fifth is the broader question of kernel lifecycle safety. The series removes one class of bug from Rust drivers, but it does not remove the need for careful review of ordering and device references. The interesting long-term question is whether the kernel can adopt more of these ownership-based patterns without losing the explicit control that low-level code sometimes needs.

The patch series is a small diff with a clear argument: if the language can guarantee teardown, the API does not need a callback for it. That is the kind of change that makes Rust for Linux more than a syntax choice. It is worth watching whether the kernel community agrees.

Sources


← back to posts