From: Andreas Hindborg <a.hindborg@kernel.org>
To: Tamir Duberstein <tamird@gmail.com>
Cc: "Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Will Deacon" <will@kernel.org>,
"Peter Zijlstra" <peterz@infradead.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"Jens Axboe" <axboe@kernel.dk>,
"Francesco Zardi" <frazar00@gmail.com>,
rust-for-linux@vger.kernel.org, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 3/4] rust: block: convert `block::mq` to use `Refcount`
Date: Thu, 20 Feb 2025 20:18:37 +0100 [thread overview]
Message-ID: <8734g8ifw2.fsf@kernel.org> (raw)
In-Reply-To: <CAJ-ks9mEqo72Cwq5_BMtdLPjGSb_sQbm_p+TV_u=iNuYSnuPKQ@mail.gmail.com> (Tamir Duberstein's message of "Wed, 19 Feb 2025 17:53:43 -0500")
Tamir Duberstein <tamird@gmail.com> writes:
> On Wed, Feb 19, 2025 at 5:26 PM Tamir Duberstein <tamird@gmail.com> wrote:
>>
>> On Wed, Feb 19, 2025 at 3:17 PM Gary Guo <gary@garyguo.net> wrote:
>> >
>> > Currently there's a custom reference counting in `block::mq`, which uses
>> > `AtomicU64` Rust atomics, and this type doesn't exist on some 32-bit
>> > architectures. We cannot just change it to use 32-bit atomics, because
>> > doing so will make it vulnerable to refcount overflow. So switch it to
>> > use the kernel refcount `kernel::sync::Refcount` instead.
>> >
>> > There is an operation needed by `block::mq`, atomically decreasing
>> > refcount from 2 to 0, which is not available through refcount.h, so
>> > I exposed `Refcount::as_atomic` which allows accessing the refcount
>> > directly.
>> >
>> > Acked-by: Andreas Hindborg <a.hindborg@kernel.org>
>> > Signed-off-by: Gary Guo <gary@garyguo.net>
>> > ---
>> > rust/kernel/block/mq/operations.rs | 7 +--
>> > rust/kernel/block/mq/request.rs | 70 ++++++++++--------------------
>> > rust/kernel/sync/refcount.rs | 14 ++++++
>> > 3 files changed, 40 insertions(+), 51 deletions(-)
>> >
>> > diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/operations.rs
>> > index 864ff379dc91..c399dcaa6740 100644
>> > --- a/rust/kernel/block/mq/operations.rs
>> > +++ b/rust/kernel/block/mq/operations.rs
>> > @@ -10,9 +10,10 @@
>> > block::mq::Request,
>> > error::{from_result, Result},
>> > prelude::*,
>> > + sync::Refcount,
>> > types::ARef,
>> > };
>> > -use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering};
>> > +use core::marker::PhantomData;
>> >
>> > /// Implement this trait to interface blk-mq as block devices.
>> > ///
>> > @@ -78,7 +79,7 @@ impl<T: Operations> OperationsVTable<T> {
>> > let request = unsafe { &*(*bd).rq.cast::<Request<T>>() };
>> >
>> > // One refcount for the ARef, one for being in flight
>> > - request.wrapper_ref().refcount().store(2, Ordering::Relaxed);
>> > + request.wrapper_ref().refcount().set(2);
>> >
>> > // SAFETY:
>> > // - We own a refcount that we took above. We pass that to `ARef`.
>> > @@ -187,7 +188,7 @@ impl<T: Operations> OperationsVTable<T> {
>> >
>> > // SAFETY: The refcount field is allocated but not initialized, so
>> > // it is valid for writes.
>> > - unsafe { RequestDataWrapper::refcount_ptr(pdu.as_ptr()).write(AtomicU64::new(0)) };
>> > + unsafe { RequestDataWrapper::refcount_ptr(pdu.as_ptr()).write(Refcount::new(0)) };
>>
>> Could we just make the field pub and remove refcount_ptr? I believe a
>> few callers of `wrapper_ptr` could be replaced with `wrapper_ref`.
>
> I took a stab at this to check it was possible:
> https://gist.github.com/tamird/c9de7fa6e54529996f433950268f3f87
The access method uses a raw pointer because it is not always safe to
reference the field.
I think line 25 in your patch is UB as the field is not initialized.
At any rate, such a change is orthogonal. You could submit a separate
patch with that refactor.
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2025-02-20 19:18 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-19 20:15 [PATCH v3 0/4] implement `kernel::sync::Refcount` and convert users Gary Guo
2025-02-19 20:15 ` [PATCH v3 1/4] rust: implement `kernel::sync::Refcount` Gary Guo
2025-02-19 22:12 ` Tamir Duberstein
2025-02-21 16:02 ` Gary Guo
2025-02-21 17:23 ` Tamir Duberstein
2025-02-20 12:46 ` Fiona Behrens
2025-02-19 20:15 ` [PATCH v3 2/4] rust: convert `Arc` to use `Refcount` Gary Guo
2025-02-19 22:12 ` Tamir Duberstein
2025-02-21 16:14 ` Gary Guo
2025-02-21 17:27 ` Tamir Duberstein
2025-02-21 18:28 ` Gary Guo
2025-02-21 18:33 ` Tamir Duberstein
2025-02-21 12:05 ` Alice Ryhl
2025-02-19 20:15 ` [PATCH v3 3/4] rust: block: convert `block::mq` " Gary Guo
2025-02-19 22:26 ` Tamir Duberstein
2025-02-19 22:53 ` Tamir Duberstein
2025-02-20 19:18 ` Andreas Hindborg [this message]
2025-02-20 12:27 ` David Gow
2025-02-19 20:15 ` [PATCH v3 4/4] MAINTAINERS: update atomic infrastructure entry to include Rust Gary Guo
2025-02-19 21:13 ` Boqun Feng
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=8734g8ifw2.fsf@kernel.org \
--to=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=axboe@kernel.dk \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=frazar00@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
--cc=will@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).