rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Gary Guo <gary@garyguo.net>
Cc: "Greg KH" <gregkh@linuxfoundation.org>,
	"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>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Martin Rodriguez Reboredo" <yakoyoku@gmail.com>,
	"Will Deacon" <will@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Dirk Behme" <dirk.behme@de.bosch.com>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 1/3] rust: implement `kernel::sync::Refcount`
Date: Mon, 7 Oct 2024 14:30:34 +0200	[thread overview]
Message-ID: <CAH5fLgj6SPAM0KM2pF1v0gUCJA875VvWxddTCQXu7h+t1tDjfw@mail.gmail.com> (raw)
In-Reply-To: <20241005152605.6d7d20e1.gary@garyguo.net>

On Sat, Oct 5, 2024 at 4:26 PM Gary Guo <gary@garyguo.net> wrote:
>
> On Sat, 5 Oct 2024 14:31:06 +0100
> Gary Guo <gary@garyguo.net> wrote:
>
> > On Sat, 5 Oct 2024 09:40:53 +0200
> > Greg KH <gregkh@linuxfoundation.org> wrote:
> >
> > > On Fri, Oct 04, 2024 at 04:52:22PM +0100, Gary Guo wrote:
> > > > This is a wrapping layer of `include/linux/refcount.h`. Currently only
> > > > the most basic operations (read/set/inc/dec/dec_and_test) are implemented,
> > > > additional methods can be implemented when they are needed.
> > > >
> > > > Currently the kernel refcount has already been used in `Arc`, however it
> > > > calls into FFI directly.
> > > >
> > > > Cc: Will Deacon <will@kernel.org>
> > > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > > Cc: Boqun Feng <boqun.feng@gmail.com>
> > > > Cc: Mark Rutland <mark.rutland@arm.com>
> > > > Signed-off-by: Gary Guo <gary@garyguo.net>
> > > > ---
> > > >  rust/helpers/refcount.c      | 15 ++++++
> > > >  rust/kernel/sync.rs          |  2 +
> > > >  rust/kernel/sync/refcount.rs | 94 ++++++++++++++++++++++++++++++++++++
> > > >  3 files changed, 111 insertions(+)
> > > >  create mode 100644 rust/kernel/sync/refcount.rs
> > > >
> > > > diff --git a/rust/helpers/refcount.c b/rust/helpers/refcount.c
> > > > index f47afc148ec3..39649443426b 100644
> > > > --- a/rust/helpers/refcount.c
> > > > +++ b/rust/helpers/refcount.c
> > > > @@ -8,11 +8,26 @@ refcount_t rust_helper_REFCOUNT_INIT(int n)
> > > >   return (refcount_t)REFCOUNT_INIT(n);
> > > >  }
> > > >
> > > > +unsigned int rust_helper_refcount_read(refcount_t *r)
> > > > +{
> > > > + return refcount_read(r);
> > > > +}
> > >
> > > Reading a refcount is almost always a wrong thing to do (it can change
> > > right after you read it), and I don't see any of the later patches in
> > > this series use this call, so can you just drop this?
> > >
> > > thanks,
> > >
> > > greg k-h
> >
> > I originally introduced this thinking I can replace Andreas's atomic
> > 2->0 operation with a read + set, but ended up couldn't do it.
> >
> > The refcount read is still useful to determine if the current value is
> > 1 -- in fact, `Arc::into_unique_or_drop` could use this rather than
> > decrementing the refcount and then incrementing it again (just doing a
> > refcount read would be much better codegen-wise than the current
> > behaviour). I'll probably make this change in the next version of the
> > series.
>
> Actually `into_unique_or_drop` can't use this because it needs to avoid
> running destructor when it races with other threads. The semantics for
> that function is better reflected with `refcount_dec_not_one`, which
> I'll introduce in v2, and I'll drop `read` in v2.

Ah, I did not know that C had a refcount_dec_not_one. Yeah, that's
exactly what into_unique_or_drop does.

Though I don't know if the cmpxchg loop is really more efficient than
just doing an infallible decrement like I do right now?

Alice

  reply	other threads:[~2024-10-07 12:30 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-04 15:52 [PATCH 0/3] implement `kernel::sync::Refcount` and convert users Gary Guo
2024-10-04 15:52 ` [PATCH 1/3] rust: implement `kernel::sync::Refcount` Gary Guo
2024-10-04 16:34   ` Dirk Behme
2024-10-04 18:51   ` Andreas Hindborg
2024-10-04 19:08     ` Gary Guo
2024-10-04 19:22       ` Andreas Hindborg
2024-10-05  6:04       ` Dirk Behme
2024-10-05  7:40   ` Greg KH
2024-10-05 13:31     ` Gary Guo
2024-10-05 14:26       ` Gary Guo
2024-10-07 12:30         ` Alice Ryhl [this message]
2024-10-04 15:52 ` [PATCH 2/3] rust: convert `Arc` to use `Refcount` Gary Guo
2024-10-05 12:06   ` Alice Ryhl
2024-10-05 13:12     ` Gary Guo
2024-10-04 15:52 ` [PATCH 3/3] rust: block: convert `block::mq` " Gary Guo
2024-10-04 16:43   ` Dirk Behme
2024-10-04 18:05   ` Andreas Hindborg
2024-10-05 15:08     ` Gary Guo
2024-10-05 18:47       ` Andreas Hindborg
2024-10-04 18:34   ` Andreas Hindborg
2024-10-04 18:43     ` Gary Guo
2024-10-04 19:18       ` Andreas Hindborg
2024-10-05  7:47   ` Greg KH
2024-10-05  9:48     ` Andreas Hindborg
2024-10-05 10:09       ` Greg KH
2024-10-05 10:10       ` Peter Zijlstra
2024-10-05 10:57         ` Andreas Hindborg
2024-10-05 11:05         ` Miguel Ojeda
2024-10-05 11:59       ` Alice Ryhl
2024-10-05 13:23         ` Gary Guo
2024-10-05 14:56           ` Andreas Hindborg
2024-10-10  8:39             ` Andreas Hindborg
2024-10-10  9:06               ` Andreas Hindborg
2024-10-10  9:48                 ` Benno Lossin
2024-10-10 11:13                   ` Andreas Hindborg
2024-10-05 14:51         ` Andreas Hindborg
2024-10-10  8:41   ` Andreas Hindborg

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=CAH5fLgj6SPAM0KM2pF1v0gUCJA875VvWxddTCQXu7h+t1tDjfw@mail.gmail.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dirk.behme@de.bosch.com \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.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=tmgross@umich.edu \
    --cc=will@kernel.org \
    --cc=yakoyoku@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;
as well as URLs for NNTP newsgroup(s).