[02/13] rust: error: move unsafe block into function call

Message ID 20240116160141.165951-3-kernel@valentinobst.de
State New
Headers
Series rust: kernel: documentation improvements |

Commit Message

Valentin Obst Jan. 16, 2024, 4:01 p.m. UTC
  The `from_err_ptr` function is safe. There is no need for the call to it
to be inside the unsafe block.

Signed-off-by: Valentin Obst <kernel@valentinobst.de>
---
 rust/kernel/error.rs | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
  

Comments

Trevor Gross Jan. 18, 2024, 12:31 a.m. UTC | #1
On Tue, Jan 16, 2024 at 11:05 AM Valentin Obst <kernel@valentinobstde> wrote:
>
> The `from_err_ptr` function is safe. There is no need for the call to it
> to be inside the unsafe block.
>
> Signed-off-by: Valentin Obst <kernel@valentinobst.de>
> ---
>  rust/kernel/error.rs | 7 +------
>  1 file changed, 1 insertion(+), 6 deletions(-)
>
> diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> index 4f0c1edd63b7..6f6676bc0eb9 100644
> --- a/rust/kernel/error.rs
> +++ b/rust/kernel/error.rs
> @@ -265,12 +265,7 @@ pub fn to_result(err: core::ffi::c_int) -> Result {
>  ///     index: u32,
>  /// ) -> Result<*mut core::ffi::c_void> {
>  ///     // SAFETY: FFI call.
> -///     unsafe {
> -///         from_err_ptr(bindings::devm_platform_ioremap_resource(
> -///             pdev.to_ptr(),
> -///             index,
> -///         ))
> -///     }
> +///     from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) })
>  /// }
>  /// ```
>  // TODO: Remove `dead_code` marker once an in-kernel client is available.
> --
> 2.43.0
>
>

If you're up for it, that safety comment could also be improved. Something like

    // SAFETY: `pdev` points to a valid platform device

But that is noncritical.

Reviewed-by: Trevor Gross <tmgross@umich.edu>
  
Valentin Obst Jan. 18, 2024, 8:10 a.m. UTC | #2
> If you're up for it, that safety comment could also be improved.
> Something like
>
>    // SAFETY: `pdev` points to a valid platform device

Thanks, will include something like that in a v2.

Just to make sure I got it correctly: Index is bounds checked [1] and
thus there is no need to include it in the comment. Please object if
that is wrong.

[1]: https://elixir.bootlin.com/linux/v6.7/source/drivers/base/platform.c#L63
  

Patch

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 4f0c1edd63b7..6f6676bc0eb9 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -265,12 +265,7 @@  pub fn to_result(err: core::ffi::c_int) -> Result {
 ///     index: u32,
 /// ) -> Result<*mut core::ffi::c_void> {
 ///     // SAFETY: FFI call.
-///     unsafe {
-///         from_err_ptr(bindings::devm_platform_ioremap_resource(
-///             pdev.to_ptr(),
-///             index,
-///         ))
-///     }
+///     from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) })
 /// }
 /// ```
 // TODO: Remove `dead_code` marker once an in-kernel client is available.