Re: [PATCH 2/8] rust: device: add drvdata accessors
From: Danilo Krummrich
Date: Mon Jul 07 2025 - 05:40:19 EST
On Mon, Jul 07, 2025 at 04:46:09PM +0900, Alexandre Courbot wrote:
> let pdev = unsafe { &*pdev.cast::<Device<InternalSet<Pin<KBox<T>>>>>() };
> ...
> // The type of the driver data is already known from `pdev`'s type,
> // so this can be safe.
> let foo = pdev.as_ref().drvdata_borrow();
I think this doesn't remove the safety requirement. drvdata_borrow() or
drvdata_obtain() would still require CoreInternal<Pin<KBox<T>>> to have the
correct type generic.
Maybe we could have some invariant on CoreInternal that the generic type is
*always* the bound driver's private data type. But then we have an
`// INVARIANT` comment on the `Device<CoreInternal<...>>` cast, which would need
the same justification as the current safety requirement.
So, I don't think this safety requirement goes away. You can only move it
around.
> I don't see any use of `drvdata_borrow` in this patchset, so I cannot
> really assess the benefit of making it safe, but for your consideration.
> ^_^;
It will be used from other bus callbacks, such as shutdown(). (There are bus
abstractions on the list (e.g. I2C) that will start using it in the next
cycle.)