rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@redhat.com>
To: gregkh@linuxfoundation.org, rafael@kernel.org
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	Danilo Krummrich <dakr@redhat.com>,
	Benno Lossin <benno.lossin@proton.me>
Subject: [PATCH 1/2] device: rust: improve safety comments
Date: Wed, 19 Jun 2024 15:39:17 +0200	[thread overview]
Message-ID: <20240619133949.64638-1-dakr@redhat.com> (raw)

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>
---
 rust/kernel/device.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index e445e87fb7d7..851018eef885 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -30,8 +30,10 @@
 ///
 /// # Invariants
 ///
-/// The pointer stored in `Self` is non-null and valid for the lifetime of the `ARef` instance. In
-/// particular, the `ARef` instance owns an increment on the underlying object’s reference count.
+/// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.
+///
+/// Instances of this type are always reference-counted, that is, a call to `get_device` ensures
+/// that the allocation remains valid at least until the matching call to `put_device`.
 ///
 /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
 /// dropped from any thread.
@@ -58,7 +60,8 @@ pub unsafe fn from_raw(ptr: *mut bindings::device) -> ARef<Self> {
         // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::device`.
         let ptr = ptr.cast::<Self>();
 
-        // SAFETY: By the safety requirements, ptr is valid.
+        // SAFETY: `ptr` is valid by the safety requirements of this function. By the above call to
+        // `bindings::get_device` we also own a reference to the underlying `struct device`.
         unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(ptr)) }
     }
 

base-commit: de6582833db0e695ba0c548e3cc2ad7dbb6aa260
-- 
2.45.1


             reply	other threads:[~2024-06-19 13:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-19 13:39 Danilo Krummrich [this message]
2024-06-19 13:39 ` [PATCH 2/2] MAINTAINERS: add Rust device abstractions to DRIVER CORE Danilo Krummrich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240619133949.64638-1-dakr@redhat.com \
    --to=dakr@redhat.com \
    --cc=benno.lossin@proton.me \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).