Rust for Linux List
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alan Stern" <stern@rowland.harvard.edu>
Cc: "Oliver Neukum" <oneukum@suse.com>,
	"Colin Braun" <colinbrauncl@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Mauro Carvalho Chehab" <mchehab@kernel.org>,
	"Mathias Nyman" <mathias.nyman@intel.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	linux-usb@vger.kernel.org, linux-media@vger.kernel.org,
	"Colin Braun" <colin.braun.cl@gmail.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	driver-core@lists.linux.dev
Subject: Re: [RFC PATCH 2/4] rust: usb: add usb host interface and endpoint abstractions
Date: Tue, 14 Jul 2026 19:53:03 +0200	[thread overview]
Message-ID: <DJYHI6W0VJ73.NP4QFGU57J50@kernel.org> (raw)
In-Reply-To: <fe12ec76-0f2a-48bd-8fd5-9b94b2600718@rowland.harvard.edu>

(Cc: driver-core)

On Tue Jul 14, 2026 at 6:26 PM CEST, Alan Stern wrote:
> I don't see why Rust needs to distinguish between a USB device that is 
> bound and one that is unbound.  There isn't much you can do with one 
> that can't be done with the other.

It has nothing to do with Rust, those driver lifecycle rules exist regardless
and they are present universally, including in C.

The only difference is that in C all the responsibility to enforce them is
usually on the driver -- e.g. by creating, destroying and calling things in the
correct order in probe() and remove() -- and a lot of drivers have bugs in this
regard as a consequence.

With Rust we can enforce those rules with the help of the type system at compile
time; device context states are a part of that.

> Similarly, I don't see why Rust needs to distinguish between an 
> interface that is bound and one that isn't.
>
> Even from the point of view of the device core, a device that is bound 
> to a driver is the same kind of data structure as one that isn't bound; 
> the only difference is whether the ->driver pointer is set.

This is a huge understatement.

The state of a device being bound to a driver defines which entity (i.e. which
driver) is in charge of operating the underlying device, and thus defines who
owns the device (associated) resources.

Many APIs rely on this, as in they only guarantee valid behavior when called
from a scope where the device is guaranteed to be bound to a driver, or IOW
where a driver can prove that it actually operates the device.

Drivers must only acquire device resources when they are actually bound to the
corresponding device, and must hand them back before the device is unbound. The
devres API, for instance, exists for this fundamental reason.

For instance, we can't have drivers manage IRQs, mess with I/O memory, program
IOMMU page tables (e.g. through DMA APIs), etc. for devices they are not bound
to and hence are not allowed to operate (anymore).

Those device resources all have a lifetime that is tied to the lifetime of the
device being bound to a driver.

Consequently, any asynchronous scopes such as IOCTLs from class device
registrations, IRQs, work queued on workqueues, etc. must all be synchronized in
some way such that those asynchronous scopes do not access device resources that
have already been destroyed on driver unbind.

Or in other words, they must be synchronized against the "bound" scope, which is
exactly what the Device<Bound> type state in Rust represents.

So, again, all those lifetime rules around the driver lifecycle exist
universally, it's just that in Rust we enforce them through the type system.

For instance, tying it back to USB, we don't want that a usb_driver still messes
with a usb_interface, e.g. initiating transfers after it has been unbound from
the interface and hence must not operate it anymore. This can easily happen if
e.g. a class device registration is not properly synchronized and the driver
still receives IOCTLs after driver unbind. In Rust we know through the "Bound"
type state which scope provides the guarantee that the device is still bound,
such that mistakes like this become impossible.

  reply	other threads:[~2026-07-14 17:53 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-12 21:07 [RFC PATCH 0/4] rust: usb: add usb request block abstractions and a user Colin Braun
2026-07-12 21:07 ` [RFC PATCH 1/4] rust: usb: add USB ch9 standard descriptors and constants Colin Braun
2026-07-12 21:07 ` [RFC PATCH 2/4] rust: usb: add usb host interface and endpoint abstractions Colin Braun
2026-07-13 13:22   ` Danilo Krummrich
2026-07-13 20:03     ` Colin Braun
2026-07-13 20:09       ` Danilo Krummrich
2026-07-14  9:26         ` Oliver Neukum
2026-07-14 13:05           ` Danilo Krummrich
2026-07-14 16:26             ` Alan Stern
2026-07-14 17:53               ` Danilo Krummrich [this message]
2026-07-14 18:48                 ` Oliver Neukum
2026-07-14 18:57                   ` Danilo Krummrich
2026-07-14 19:25                 ` Alan Stern
2026-07-14 17:57               ` Oliver Neukum
2026-07-14 19:03                 ` Alan Stern
2026-07-14 18:26               ` Miguel Ojeda
2026-07-12 21:08 ` [RFC PATCH 3/4] rust: usb: add urb abstraction with control and isochronous support Colin Braun
2026-07-12 21:08 ` [RFC PATCH 4/4] media: add gv-usb2 audio capture driver Colin Braun
2026-07-13 14:44   ` Danilo Krummrich
2026-07-13 21:08     ` Colin Braun
2026-07-13 13:22 ` [RFC PATCH 0/4] rust: usb: add usb request block abstractions and a user Danilo Krummrich
2026-07-13 20:10   ` Colin Braun
2026-07-13 13:53 ` Daniel Almeida
2026-07-13 20:32   ` Colin Braun

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DJYHI6W0VJ73.NP4QFGU57J50@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=colin.braun.cl@gmail.com \
    --cc=colinbrauncl@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=mathias.nyman@intel.com \
    --cc=mchehab@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=oneukum@suse.com \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox