Skip to content

Commit

Permalink
firmware: rust: improve safety comments
Browse files Browse the repository at this point in the history
Improve the wording of safety comments to be more explicit about what
exactly is guaranteed to be valid.

Suggested-by: Benno Lossin <benno.lossin@proton.me>
Signed-off-by: Danilo Krummrich <dakr@redhat.com>
Link: https://lore.kernel.org/r/20240619132029.59296-1-dakr@redhat.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Danilo Krummrich authored and Greg Kroah-Hartman committed Jun 20, 2024
1 parent 269e974 commit bbe98f4
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions rust/kernel/firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type FwFunc =
///
/// The pointer is valid, and has ownership over the instance of `struct firmware`.
///
/// Once requested, the `Firmware` backing buffer is not modified until it is freed when `Firmware`
/// is dropped.
/// The `Firmware`'s backing buffer is not modified.
///
/// # Examples
///
Expand Down Expand Up @@ -72,22 +71,22 @@ impl Firmware {

/// Returns the size of the requested firmware in bytes.
pub fn size(&self) -> usize {
// SAFETY: Safe by the type invariant.
// SAFETY: `self.as_raw()` is valid by the type invariant.
unsafe { (*self.as_raw()).size }
}

/// Returns the requested firmware as `&[u8]`.
pub fn data(&self) -> &[u8] {
// SAFETY: Safe by the type invariant. Additionally, `bindings::firmware` guarantees, if
// successfully requested, that `bindings::firmware::data` has a size of
// `bindings::firmware::size` bytes.
// SAFETY: `self.as_raw()` is valid by the type invariant. Additionally,
// `bindings::firmware` guarantees, if successfully requested, that
// `bindings::firmware::data` has a size of `bindings::firmware::size` bytes.
unsafe { core::slice::from_raw_parts((*self.as_raw()).data, self.size()) }
}
}

impl Drop for Firmware {
fn drop(&mut self) {
// SAFETY: Safe by the type invariant.
// SAFETY: `self.as_raw()` is valid by the type invariant.
unsafe { bindings::release_firmware(self.as_raw()) };
}
}
Expand Down

0 comments on commit bbe98f4

Please sign in to comment.