mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>,
	Danilo Krummrich <dakr@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sasha Levin <sashal@kernel.org>,
	ojeda@kernel.org, alex.gaynor@gmail.com, mcgrof@kernel.org,
	russ.weight@linux.dev, dakr@redhat.com,
	rust-for-linux@vger.kernel.org
Subject: [PATCH AUTOSEL 6.11 10/20] rust: device: change the from_raw() function
Date: Sun, 13 Oct 2024 23:57:12 -0400	[thread overview]
Message-ID: <20241014035731.2246632-10-sashal@kernel.org> (raw)
In-Reply-To: <20241014035731.2246632-1-sashal@kernel.org>

From: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>

[ Upstream commit cc4332afb5631b0e9d2ce5699b7f4b7caf743526 ]

The function Device::from_raw() increments a refcount by a call to
bindings::get_device(ptr). This can be confused because usually
from_raw() functions don't increment a refcount.
Hence, rename Device::from_raw() to avoid confuion with other "from_raw"
semantics.

The new name of function should be "get_device" to be consistent with
the function get_device() already exist in .c files.

This function body also changed, because the `into()` will convert the
`&'a Device` into `ARef<Device>` and also call `inc_ref` from the
`AlwaysRefCounted` trait implemented for Device.

Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@gmail.com>
Acked-by: Danilo Krummrich <dakr@kernel.org>
Closes: https://github.com/Rust-for-Linux/linux/issues/1088
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://lore.kernel.org/r/20241001205603.106278-1-trintaeoitogc@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 rust/kernel/device.rs   | 15 +++------------
 rust/kernel/firmware.rs |  2 +-
 2 files changed, 4 insertions(+), 13 deletions(-)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index 851018eef885e..c8199ee079eff 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -51,18 +51,9 @@ impl Device {
     ///
     /// It must also be ensured that `bindings::device::release` can be called from any thread.
     /// While not officially documented, this should be the case for any `struct device`.
-    pub unsafe fn from_raw(ptr: *mut bindings::device) -> ARef<Self> {
-        // SAFETY: By the safety requirements, ptr is valid.
-        // Initially increase the reference count by one to compensate for the final decrement once
-        // this newly created `ARef<Device>` instance is dropped.
-        unsafe { bindings::get_device(ptr) };
-
-        // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::device`.
-        let ptr = ptr.cast::<Self>();
-
-        // 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)) }
+    pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {
+        // SAFETY: By the safety requirements ptr is valid
+        unsafe { Self::as_ref(ptr) }.into()
     }
 
     /// Obtain the raw `struct device *`.
diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
index dee5b4b18aec4..13a374a5cdb74 100644
--- a/rust/kernel/firmware.rs
+++ b/rust/kernel/firmware.rs
@@ -44,7 +44,7 @@ fn request_nowarn() -> Self {
 ///
 /// # fn no_run() -> Result<(), Error> {
 /// # // SAFETY: *NOT* safe, just for the example to get an `ARef<Device>` instance
-/// # let dev = unsafe { Device::from_raw(core::ptr::null_mut()) };
+/// # let dev = unsafe { Device::get_device(core::ptr::null_mut()) };
 ///
 /// let fw = Firmware::request(c_str!("path/to/firmware.bin"), &dev)?;
 /// let blob = fw.data();
-- 
2.43.0


  parent reply	other threads:[~2024-10-14  3:57 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-14  3:57 [PATCH AUTOSEL 6.11 01/20] ntfs3: Add bounds checking to mi_enum_attr() Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 02/20] fs/ntfs3: Check if more than chunk-size bytes are written Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 03/20] fs/ntfs3: Fix warning possible deadlock in ntfs_set_state Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 04/20] fs/ntfs3: Stale inode instead of bad Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 05/20] fs/ntfs3: Add rough attr alloc_size check Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 06/20] fs/ntfs3: Fix possible deadlock in mi_read Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 07/20] fs/ntfs3: Additional check in ni_clear() Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 08/20] fs/ntfs3: Fix general protection fault in run_is_mapped_full Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 09/20] fs/ntfs3: Additional check in ntfs_file_release Sasha Levin
2024-10-14  3:57 ` Sasha Levin [this message]
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 11/20] scsi: scsi_transport_fc: Allow setting rport state to current state Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 12/20] cifs: Improve creating native symlinks pointing to directory Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 13/20] cifs: Fix creating native symlinks pointing to current or parent directory Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 14/20] ACPI: resource: Fold Asus Vivobook Pro N6506M* DMI quirks together Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 15/20] powercap: intel_rapl_msr: Add PL4 support for Arrowlake-U Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 16/20] thermal: intel: int340x: processor: Remove MMIO RAPL CPU hotplug support Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 17/20] thermal: intel: int340x: processor: Add MMIO RAPL PL4 support Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 18/20] net: amd: mvme147: Fix probe banner message Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 19/20] NFS: remove revoked delegation from server's delegation list Sasha Levin
2024-10-14  3:57 ` [PATCH AUTOSEL 6.11 20/20] misc: sgi-gru: Don't disable preemption in GRU driver Sasha Levin

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=20241014035731.2246632-10-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=dakr@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=russ.weight@linux.dev \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=trintaeoitogc@gmail.com \
    /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

all inboxes | Powered by JetHome®