From: "Danilo Krummrich" <dakr@kernel.org>
To: "Rahul Rameshbabu" <sergeantsagara@protonmail.com>
Cc: <rust-for-linux@vger.kernel.org>, <linux-input@vger.kernel.org>,
"Jiri Kosina" <jikos@kernel.org>, <a.hindborg@kernel.org>,
<alex.gaynor@gmail.com>, <aliceryhl@google.com>,
<benno.lossin@proton.me>,
"Benjamin Tissoires" <benjamin.tissoires@redhat.com>,
<bjorn3_gh@protonmail.com>, <boqun.feng@gmail.com>,
<db48x@db48x.net>, <gary@garyguo.net>, <ojeda@kernel.org>,
<tmgross@umich.edu>, <peter.hutterer@who-t.net>
Subject: Re: [PATCH v2 3/4] rust: core abstractions for HID drivers
Date: Tue, 15 Jul 2025 17:04:07 +0200 [thread overview]
Message-ID: <DBCPYJW9852M.2KDHMO6QS6YPY@kernel.org> (raw)
In-Reply-To: <20250713211012.101476-10-sergeantsagara@protonmail.com>
On Sun Jul 13, 2025 at 11:12 PM CEST, Rahul Rameshbabu wrote:
> +// SAFETY: Instances of `Device` are always reference-counted.
> +unsafe impl crate::types::AlwaysRefCounted for Device {
> + fn inc_ref(&self) {
> + // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
> + unsafe { bindings::kref_get(&mut ((*self.as_raw()).ref_)) }
I'm confused, what's the lifecycle of a struct hid_device? It embedds a struct
device, so it also inherits its reference count. Additionally it also has a
struct kref. Can you elaborate please?
I don't know what the struct kref is for, but I'm pretty sure you want to manage
the reference count of the embedded struct device here.
> + }
> +
> + unsafe fn dec_ref(obj: NonNull<Self>) {
> + // SAFETY: The safety requirements guarantee that the refcount is non-zero.
> + unsafe {
> + bindings::kref_put(
> + &mut ((*obj.cast::<bindings::hid_device>().as_ptr()).ref_),
I think you want &raw mut instead.
> + Some(bindings::hiddev_free),
> + )
> + }
> + }
> +}
> +
> +impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
> + fn as_ref(&self) -> &device::Device<Ctx> {
> + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
> + // `struct hid_device`.
> + let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
You also use &raw mut meanwhile.
> +
> + // SAFETY: `dev` points to a valid `struct device`.
> + unsafe { device::Device::as_ref(dev) }
> + }
> +}
next prev parent reply other threads:[~2025-07-15 15:04 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-13 21:11 [PATCH v2 0/4] Initial work for Rust abstraction for HID device driver development Rahul Rameshbabu
2025-07-13 21:11 ` [PATCH v2 1/4] HID: core: Change hid_driver to use a const char* for name Rahul Rameshbabu
2025-07-13 21:12 ` [PATCH v2 2/4] rust: add kref bindings Rahul Rameshbabu
2025-07-20 16:02 ` Rahul Rameshbabu
2025-07-20 16:07 ` Boqun Feng
2025-07-13 21:12 ` [PATCH v2 3/4] rust: core abstractions for HID drivers Rahul Rameshbabu
2025-07-13 21:57 ` Miguel Ojeda
2025-07-20 16:44 ` Rahul Rameshbabu
2025-07-15 15:04 ` Danilo Krummrich [this message]
2025-07-19 22:46 ` Rahul Rameshbabu
2025-07-13 21:12 ` [PATCH v2 4/4] rust: hid: Glorious Gaming PC Race Model O and O- mice reference driver Rahul Rameshbabu
2025-07-13 21:46 ` Miguel Ojeda
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=DBCPYJW9852M.2KDHMO6QS6YPY@kernel.org \
--to=dakr@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benjamin.tissoires@redhat.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=db48x@db48x.net \
--cc=gary@garyguo.net \
--cc=jikos@kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=peter.hutterer@who-t.net \
--cc=rust-for-linux@vger.kernel.org \
--cc=sergeantsagara@protonmail.com \
--cc=tmgross@umich.edu \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.