Return the reference to the initialized data in the `devres::register` function.
This is needed in a following commit (rust: add basic serial device bus abstractions). Signed-off-by: Markus Probst <[email protected]> --- rust/kernel/cpufreq.rs | 3 ++- rust/kernel/devres.rs | 15 +++++++++++++-- rust/kernel/drm/driver.rs | 3 ++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/rust/kernel/cpufreq.rs b/rust/kernel/cpufreq.rs index 76faa1ac8501..8cf86bb8e0f4 100644 --- a/rust/kernel/cpufreq.rs +++ b/rust/kernel/cpufreq.rs @@ -1051,7 +1051,8 @@ pub fn new_foreign_owned(dev: &Device<Bound>) -> Result where T: 'static, { - devres::register(dev, Self::new()?, GFP_KERNEL) + devres::register(dev, Self::new()?, GFP_KERNEL)?; + Ok(()) } } diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index 6afe196be42c..f882bace8601 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -326,15 +326,26 @@ fn register_foreign<P>(dev: &Device<Bound>, data: P) -> Result /// } /// /// fn from_bound_context(dev: &Device<Bound>) -> Result { -/// devres::register(dev, Registration::new(), GFP_KERNEL) +/// devres::register(dev, Registration::new(), GFP_KERNEL)?; +/// Ok(()) /// } /// ``` -pub fn register<T, E>(dev: &Device<Bound>, data: impl PinInit<T, E>, flags: Flags) -> Result +pub fn register<'a, T, E>( + dev: &'a Device<Bound>, + data: impl PinInit<T, E>, + flags: Flags, +) -> Result<&'a T> where T: Send + 'static, Error: From<E>, { let data = KBox::pin_init(data, flags)?; + let data_ptr = &raw const *data; + register_foreign(dev, data) + // SAFETY: `dev` is valid for the lifetime of 'a. As long as there is a reference to + // `Device<Bound>`, it is guaranteed that the device is not unbound and data has not been + // dropped. Thus `data_ptr` is also valid for the lifetime of 'a. + .map(|()| unsafe { &*data_ptr }) } diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index e09f977b5b51..51e0c7e30cc2 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -145,7 +145,8 @@ pub fn new_foreign_owned( let reg = Registration::<T>::new(drm, flags)?; - devres::register(dev, reg, GFP_KERNEL) + devres::register(dev, reg, GFP_KERNEL)?; + Ok(()) } /// Returns a reference to the `Device` instance for this registration. -- 2.52.0
