[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DG71N0HV7DQ8.35GWSCA3G4O8Y@kernel.org>
Date: Thu, 05 Feb 2026 13:57:09 +0100
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Link Mauve" <linkmauve@...kmauve.fr>
Cc: <rust-for-linux@...r.kernel.org>, "Madhavan Srinivasan"
<maddy@...ux.ibm.com>, "Michael Ellerman" <mpe@...erman.id.au>, "Nicholas
Piggin" <npiggin@...il.com>, "Christophe Leroy (CS GROUP)"
<chleroy@...nel.org>, "Srinivas Kandagatla" <srini@...nel.org>, "Miguel
Ojeda" <ojeda@...nel.org>, "Boqun Feng" <boqun@...nel.org>, "Gary Guo"
<gary@...yguo.net>, Björn Roy Baron
<bjorn3_gh@...tonmail.com>, "Benno Lossin" <lossin@...nel.org>, "Andreas
Hindborg" <a.hindborg@...nel.org>, "Alice Ryhl" <aliceryhl@...gle.com>,
"Trevor Gross" <tmgross@...ch.edu>, "Daniel Almeida"
<daniel.almeida@...labora.com>, "Ard Biesheuvel" <ardb@...nel.org>, "Martin
K. Petersen" <martin.petersen@...cle.com>, "Eric Biggers"
<ebiggers@...gle.com>, "Greg Kroah-Hartman" <gregkh@...uxfoundation.org>,
"Lyude Paul" <lyude@...hat.com>, "Asahi Lina" <lina+kernel@...hilina.net>,
"Viresh Kumar" <viresh.kumar@...aro.org>, "Lorenzo Stoakes"
<lorenzo.stoakes@...cle.com>, "Tamir Duberstein" <tamird@...nel.org>,
"FUJITA Tomonori" <fujita.tomonori@...il.com>,
<linuxppc-dev@...ts.ozlabs.org>, <linux-kernel@...r.kernel.org>,
<officialTechflashYT@...il.com>, "Ash Logan" <ash@...quark.com>, "Roberto
Van Eeden" <rw-r-r-0644@...tonmail.com>,
Jonathan Neuschäfer <j.neuschaefer@....net>
Subject: Re: [PATCH v2 2/4] rust: nvmem: Add an abstraction for nvmem
providers
On Thu Feb 5, 2026 at 1:48 PM CET, Link Mauve wrote:
> On Wed, Feb 04, 2026 at 04:22:16PM +0100, Danilo Krummrich wrote:
>> On Wed Feb 4, 2026 at 5:04 AM CET, Link Mauve wrote:
>> > +impl Device {
>> > + /// Register a managed nvmem provider on the given device.
>> > + pub fn nvmem_register<T>(&self, mut config: NvmemConfig<T>, priv_: &T::Priv)
>> > + where
>> > + T: NvmemProvider + Default,
>> > + {
>> > + // FIXME: The last cast to mut indicates some unsoundness here.
>> > + config.inner.priv_ = core::ptr::from_ref(priv_).cast::<c_void>().cast_mut();
>> > + config.inner.dev = self.as_raw();
>> > + config.inner.reg_read = Some(NvmemConfig::<T>::reg_read);
>> > + config.inner.reg_write = Some(NvmemConfig::<T>::reg_write);
>> > + // SAFETY: Both self and config can’t be null here, and should have the correct type.
>> > + unsafe { bindings::devm_nvmem_register(self.as_raw(), &config.inner) };
>> > + }
>> > +}
>>
>> This should not be a method on the generic device type. Typically we use a
>> Registration struct for this, i.e. this would become
>> nvmem::Registration::register().
>
> Should I also switch to the nvmem_register()/nvmem_unregister() API
> instead of the devm_nvmem_register() API, so that the unregister can
> happen in the Drop impl instead of being managed by the kernel?
No, ensuring unregistration when the bus device is unbound is the correct thing
to do.
We typically support two patterns:
impl Registration {
fn new(dev: &Device<Bound>) -> Result<Devres<Registration>>;
fn register(dev: &Device<Bound>) -> Result;
}
Registration::new() still ensures unregistration when the bus device is unbound,
but also allows you to unregister before that happens.
Registration::register() is eqivalent to devm_nvmem_register().
You don't have to implement both, you can just pick the one you need for your
driver. I think in the case of nvmem you probably only every need register().
Powered by blists - more mailing lists