From: "Gary Guo" <gary@garyguo.net>
To: "Danilo Krummrich" <dakr@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>
Cc: "Lorenzo Stoakes" <ljs@kernel.org>,
"Vlastimil Babka" <vbabka@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
"Uladzislau Rezki" <urezki@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Lyude Paul" <lyude@redhat.com>,
"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Arve Hjønnevåg" <arve@android.com>,
"Todd Kjos" <tkjos@android.com>,
"Christian Brauner" <brauner@kernel.org>,
"Carlos Llamas" <cmllamas@google.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Dave Ertman" <david.m.ertman@intel.com>,
"Leon Romanovsky" <leon@kernel.org>,
"Paul Moore" <paul@paul-moore.com>,
"Serge Hallyn" <sergeh@kernel.org>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Jan Kara" <jack@suse.cz>,
"Igor Korotin" <igor.korotin@linux.dev>,
"Viresh Kumar" <vireshk@kernel.org>, "Nishanth Menon" <nm@ti.com>,
"Stephen Boyd" <sboyd@kernel.org>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
"Pavel Tikhomirov" <ptikhomirov@virtuozzo.com>,
"Michal Wilczynski" <m.wilczynski@samsung.com>,
"Ira Weiny" <iweiny@kernel.org>,
"Philipp Stanner" <phasta@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, driver-core@lists.linux.dev,
linux-block@vger.kernel.org,
linux-security-module@vger.kernel.org,
dri-devel@lists.freedesktop.org, linux-fsdevel@vger.kernel.org,
linux-pm@vger.kernel.org, linux-pci@vger.kernel.org,
linux-pwm@vger.kernel.org, linux-usb@vger.kernel.org,
"Oliver Mangold" <oliver.mangold@pm.me>
Subject: Re: [PATCH v20 7/8] rust: Add `OwnableRefCounted`
Date: Tue, 25 Aug 2026 14:37:05 +0100 [thread overview]
Message-ID: <DKY2D3AP6PZG.9RIX8KB3FF7P@garyguo.net> (raw)
In-Reply-To: <DKY1WYBNR288.36W5V60TR81MG@kernel.org>
On Tue Aug 25, 2026 at 2:16 PM BST, Danilo Krummrich wrote:
> On Mon Aug 24, 2026 at 1:17 PM CEST, Andreas Hindborg wrote:
>> +/// struct Foo {
>> +/// refcount: Cell<usize>,
>> +/// }
>> +///
>> +/// impl Foo {
>> +/// fn new() -> Result<Owned<Self>> {
>> +/// // We are just using a `KBox` here to handle the actual allocation, as our `Foo` is
>> +/// // not actually a C-allocated object.
>> +/// // INVARIANT: We initialize `refcount` to 1, satisfying the invariants.
>> +/// let result = KBox::new(
>> +/// Foo {
>> +/// refcount: Cell::new(1),
>> +/// },
>> +/// flags::GFP_KERNEL,
>> +/// )?;
>> +/// let result = KBox::into_non_null(result);
>> +/// // SAFETY:
>> +/// // - We just allocated the `Self`, thus it is valid and we own it.
>> +/// // - We can transfer this ownership to the `from_raw` method.
>> +/// Ok(unsafe { Owned::from_raw(result) })
>> +/// }
>> +/// }
>> +///
>> +/// // SAFETY: We increment and decrement each time the respective function is called and only free
>> +/// // the `Foo` when the refcount reaches zero.
>> +/// unsafe impl RefCounted for Foo {
>> +/// fn inc_ref(&self) {
>> +/// self.refcount.replace(self.refcount.get() + 1);
>> +/// }
>> +///
>> +/// unsafe fn dec_ref(this: NonNull<Self>) {
>> +/// // SAFETY: By requirement on calling this function, the refcount is non-zero,
>> +/// // implying the underlying object is valid.
>> +/// let refcount = unsafe { &this.as_ref().refcount };
>> +/// let new_refcount = refcount.get() - 1;
>> +/// if new_refcount == 0 {
>> +/// // The `Foo` will be dropped when `KBox` goes out of scope.
>> +/// // SAFETY: The [`KBox<Foo>`] is still alive as the old refcount is 1. We can pass
>> +/// // ownership to the [`KBox`] as by requirement on calling this function,
>> +/// // the `Self` will no longer be used by the caller.
>> +/// unsafe { KBox::from_raw(this.as_ptr()) };
>> +/// } else {
>> +/// refcount.replace(new_refcount);
>> +/// }
>> +/// }
>> +/// }
>
> This is valid as Foo is !Sync, but I think it does look racy on casual reading
> and possibly even encourages people to do the wrong thing, i.e. to peek a
> reference count and subsequently act on the read value.
>
> Besides that, if the value can't be shared across tasks it's not overly useful
> in the kernel to reference count it in the first place. Do you have a better
> example for this? Maybe a broken down version of the one that motivates the
> patch?
I think the series is very old and might even predate the `sync::Refcount`. But
given that we have that type now, the example should do the correct thing and
use that instead.
Best,
Gary
prev parent reply other threads:[~2026-08-25 13:37 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-24 11:17 [PATCH v20 0/8] rust: add `Ownable` trait and `Owned` type Andreas Hindborg
2026-08-24 11:17 ` [PATCH v20 1/8] rust: alloc: add `KBox::into_non_null` Andreas Hindborg
2026-08-25 13:01 ` Danilo Krummrich
2026-08-24 11:17 ` [PATCH v20 2/8] rust: types: Add Ownable/Owned types Andreas Hindborg
2026-08-24 11:17 ` [PATCH v20 3/8] rust: implement `ForeignOwnable` for `Owned` Andreas Hindborg
2026-08-24 11:17 ` [PATCH v20 4/8] rust: page: convert to `Ownable` Andreas Hindborg
2026-08-25 13:20 ` [PATCH v20 4/8] rust: page: convert to `Ownable`' Alice Ryhl
2026-08-25 13:34 ` [PATCH v20 4/8] rust: page: convert to `Ownable` Danilo Krummrich
2026-08-24 11:17 ` [PATCH v20 6/8] rust: Add missing SAFETY documentation for `ARef` example Andreas Hindborg
2026-08-24 11:36 ` Miguel Ojeda
2026-08-24 11:18 ` [PATCH v20 8/8] rust: page: add `from_raw()` Andreas Hindborg
[not found] ` <20260824-unique-ref-v20-5-490735672187@kernel.org>
2026-08-24 12:26 ` [PATCH v20 5/8] rust: rename `AlwaysRefCounted` to `RefCounted` Uwe Kleine-König
[not found] ` <20260824-unique-ref-v20-7-490735672187@kernel.org>
2026-08-25 13:16 ` [PATCH v20 7/8] rust: Add `OwnableRefCounted` Danilo Krummrich
2026-08-25 13:37 ` Gary Guo [this message]
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=DKY2D3AP6PZG.9RIX8KB3FF7P@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=arve@android.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=brauner@kernel.org \
--cc=cmllamas@google.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=david.m.ertman@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=igor.korotin@linux.dev \
--cc=iweiny@kernel.org \
--cc=jack@suse.cz \
--cc=kwilczynski@kernel.org \
--cc=leon@kernel.org \
--cc=liam@infradead.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-pwm@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=ljs@kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=m.wilczynski@samsung.com \
--cc=nm@ti.com \
--cc=ojeda@kernel.org \
--cc=oliver.mangold@pm.me \
--cc=paul@paul-moore.com \
--cc=phasta@kernel.org \
--cc=ptikhomirov@virtuozzo.com \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=sergeh@kernel.org \
--cc=simona@ffwll.ch \
--cc=tamird@kernel.org \
--cc=tkjos@android.com \
--cc=tmgross@umich.edu \
--cc=urezki@gmail.com \
--cc=vbabka@kernel.org \
--cc=vireshk@kernel.org \
--cc=viro@zeniv.linux.org.uk \
--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