From: Andreas Hindborg <a.hindborg@kernel.org>
To: Tran Van Tien <7enlab@gmail.com>, ojeda@kernel.org
Cc: boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com,
lossin@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
dakr@kernel.org, daniel.almeida@collabora.com, tamird@kernel.org,
acourbot@nvidia.com, work@onurozkan.dev,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust: sync: make `AlwaysRefCounted::inc_ref` an associated function
Date: Thu, 26 Nov 2026 11:36:25 +0100 [thread overview]
Message-ID: <871p87vpzq.fsf@t14s.mail-host-address-is-not-set> (raw)
In-Reply-To: <1795684556.6d661f19@gmail.com>
"Tran Van Tien" <7enlab@gmail.com> writes:
> `AlwaysRefCounted::inc_ref` is currently a method taking `&self`, which
> makes it easy to increment the reference count by accident on any value
> that happens to be in scope. Incrementing the refcount should be a
> deliberate act: every successful call must be paired with exactly one
> `dec_ref`, and getting that wrong leaks the object or frees it early.
>
> `dec_ref` is already an associated function taking `NonNull<Self>` for
> the same reason. Align `inc_ref` with it so that the two halves of the
> refcount protocol have the same shape, and so that a call site has to
> name the type explicitly.
>
> Callers that legitimately need to increment the refcount now obtain a
> `NonNull` first, which also makes the safety requirement explicit: the
> pointer must be valid.
>
> This is a mechanical conversion of the trait and of all 27
> implementations in-tree. `ARef::clone` and `From<&T> for ARef<T>`
> already hold a `NonNull`, so they pass it directly. `serdev::Device`
> forwards to the inner `device::Device` implementation. `Fence`,
> `FwNode`, `GpuVm`, `I2cAdapter`, `I2cClient`, `Request`, `Mm`,
> `MmWithUser`, `MmWithUserAsync`, `OPP`, `Chip`, `Credential`,
> `PidNamespace`, `Task`, `LocalFile`, `File`, and `Device` (platform,
> pci, usb, auxiliary, drm) each take the raw pointer through a shared
> reference.
>
> All 27 implementations are converted; no behaviour changes.
>
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Link: https://github.com/Rust-for-Linux/linux/issues/1177
> Signed-off-by: Tran Van Tien <7enlab@gmail.com>
<cut>
> diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
> index 9983ee085..0198c042e 100644
> --- a/rust/kernel/sync/aref.rs
> +++ b/rust/kernel/sync/aref.rs
> @@ -49,7 +49,11 @@
> /// alive.)
> pub unsafe trait AlwaysRefCounted {
> /// Increments the reference count on the object.
> - fn inc_ref(&self);
> + ///
> + /// # Safety
> + ///
> + /// Callers must ensure that `obj` points to a valid object of this type.
> + unsafe fn inc_ref(obj: NonNull<Self>);
I do not think we should make this unsafe. Leaking is safe. I also I
don't think there is any reason to make this take `NonNull`. You can
make it an associated function that takes `this: &Self`.
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2026-11-26 10:36 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <OToxFKfgF7gRhE7bRLNCy8rCFgDYsz3vWShTvaBIm5_P087CsaSTZlwyx4vhVvAruJaRUGhZMg9GJoY02B5qcQ==@protonmail.internalid>
2026-11-26 9:15 ` Tran Van Tien
2026-11-26 10:22 ` [PATCH v2] rust: sync: make AlwaysRefCounted::inc_ref " Tran Van Tien
2026-11-26 10:43 ` [PATCH v3] " Tran Van Tien
2026-11-26 10:55 ` Andreas Hindborg
2026-11-26 13:48 ` Nicolás Antinori
2026-11-26 13:57 ` Tran Van Tien
2026-11-26 10:36 ` Andreas Hindborg [this message]
2026-11-26 9:04 [PATCH] " Tran Van Tien
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=871p87vpzq.fsf@t14s.mail-host-address-is-not-set \
--to=a.hindborg@kernel.org \
--cc=7enlab@gmail.com \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/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®