Re: [PATCH v2 1/2] rust: add abstraction for struct device

From: Danilo Krummrich
Date: Wed Jun 12 2024 - 10:02:21 EST


On 6/11/24 18:13, Boqun Feng wrote:
On Tue, Jun 11, 2024 at 03:29:22PM +0200, Greg KH wrote:
On Tue, Jun 11, 2024 at 03:21:31PM +0200, Danilo Krummrich wrote:
...hence, I agree we should indeed add to the #Invariants and #Safety section
that `->release` must be callable from any thread.

However, this is just theory, do we actually have cases where `device::release`

@Danilo, right, it's only theorical, but it's good to call it out since
it's the requirement for a safe Rust abstraction.

is not allowed to be called from any thread? If so, this would be very confusing
for a reference counted type from a design point of view...

What do you mean exactly "by any thread"? Maybe not from interrupt

The `Send` trait here doesn't really differ between interrupt contexts
and process contexts, so "by any thread", it includes all the contexts.
However, we rely on klint[1] to detect context mismatch in compile time
(it's still a WIP though). For this case, we would need to mark the
`Device::dec_ref` function as might sleep.

I think once we can detect it on compile time we should do that.

As for the might_sleep(), if we want to have this check, we should probably
do it in device_release() [1], rather than in the Rust code only.

[1] https://elixir.bootlin.com/linux/latest/source/drivers/base/core.c#L2557


Regards,
Boqun

[1]: https://rust-for-linux.com/klint

context, but any other normal thread (i.e. that you can sleep in), it
should be fine to call release() in.

thanks,

greg k-h