public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: "Gary Guo" <gary@garyguo.net>,
	"Peter Zijlstra" <peterz@infradead.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Will Deacon" <will@kernel.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Vincenzo Palazzo" <vincenzopalazzodev@gmail.com>
Subject: Re: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
Date: Thu, 2 Feb 2023 17:17:55 +0100	[thread overview]
Message-ID: <Y9viM2POUsSnbKUh@kroah.com> (raw)
In-Reply-To: <Y9vga90K0aVfGUwW@boqun-archlinux>

On Thu, Feb 02, 2023 at 08:10:19AM -0800, Boqun Feng wrote:
> On Thu, Feb 02, 2023 at 04:41:47PM +0100, Greg KH wrote:
> > On Thu, Feb 02, 2023 at 02:21:53PM +0000, Gary Guo wrote:
> > > On Thu, 2 Feb 2023 10:14:06 +0100
> > > Peter Zijlstra <peterz@infradead.org> wrote:
> > > 
> > > > On Wed, Feb 01, 2023 at 03:22:41PM -0800, Boqun Feng wrote:
> > > > > This allows reading the current count of a refcount in an `ArcInner`.
> > > > > 
> > > > > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> > > > > ---
> > > > >  rust/helpers.c          | 6 ++++++
> > > > >  rust/kernel/sync/arc.rs | 9 +++++++++
> > > > >  2 files changed, 15 insertions(+)
> > > > > 
> > > > > diff --git a/rust/helpers.c b/rust/helpers.c
> > > > > index 09a4d93f9d62..afc5f1a39fef 100644
> > > > > --- a/rust/helpers.c
> > > > > +++ b/rust/helpers.c
> > > > > @@ -46,6 +46,12 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r)
> > > > >  }
> > > > >  EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test);
> > > > >  
> > > > > +unsigned int rust_helper_refcount_read(refcount_t *r)
> > > > > +{
> > > > > +	return refcount_read(r);
> > > > > +}
> > > > > +EXPORT_SYMBOL_GPL(rust_helper_refcount_read);
> > > > > +
> > > > >  /*
> > > > >   * We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
> > > > >   * as the Rust `usize` type, so we can use it in contexts where Rust
> > > > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> > > > > index fc680a4a795c..fbfceaa3096e 100644
> > > > > --- a/rust/kernel/sync/arc.rs
> > > > > +++ b/rust/kernel/sync/arc.rs
> > > > > @@ -127,6 +127,15 @@ struct ArcInner<T: ?Sized> {
> > > > >      data: T,
> > > > >  }
> > > > >  
> > > > > +impl<T: ?Sized> ArcInner<T> {
> > > > > +    /// Returns the current reference count of [`ArcInner`].
> > > > > +    fn count(&self) -> u32 {
> > > > > +        // SAFETY: `self.refcount.get()` is always a valid pointer, and `refcount_read()` is a
> > > > > +        // normal atomic read (i.e. no data race) only requiring on the address is valid.
> > > > > +        unsafe { bindings::refcount_read(self.refcount.get()) }
> > > > > +    }
> > > > > +}  
> > > > 
> > > > This is completely unsafe vs concurrency. In order to enable correct
> > > > tracing of refcount manipulations we have the __refcount_*(.oldp) API.
> > > 
> > > Retrieving the reference count is safe. It's just that in many
> > > scenarios it's very hard to use the retrieved reference count
> > > correctly, because it might be concurrently changed.
> > 
> > Yes, so you really should never ever ever care about the value, and that
> > includes printing it out as it will be wrong the instant you read it.
> > 
> 
> Agreed.
> 
> > > But there are correct ways to use a refcount, e.g. if you own
> > > `Arc` and `.count()` returns 1, then you know that you are the
> > > exclusive owner of the `Arc` and nobody else is going to touch it.
> > 
> > But you should never know this, as it is not relevant.
> > 
> > So no, please don't allow printing out of a reference count, that will
> > only cause problems and allow people to think it is safe to do so.
> > 
> 
> People already do it, even in *security* code,
> 
> security/keys/keyring.c:
> 
> 	int key_link(struct key *keyring, struct key *key)
> 	{
> 		...
> 		kenter("{%d,%d}", keyring->serial, refcount_read(&keyring->usage));
> 		...
> 	}
> 
> Should we fix that?

Yes.  But really, that's debugging code, it probably should all be
removed now.

thanks,

greg k-h

  reply	other threads:[~2023-02-02 16:18 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-01 23:22 [RFC 0/5] rust: sync: Arc: Implement Debug and Display Boqun Feng
2023-02-01 23:22 ` [RFC 1/5] rust: sync: impl Display for {Unique,}Arc Boqun Feng
2023-02-02 14:15   ` Gary Guo
2023-02-02 16:50   ` Björn Roy Baron
2023-02-04 10:20   ` Finn Behrens
2023-02-04 18:47   ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count() Boqun Feng
2023-02-02  9:14   ` Peter Zijlstra
2023-02-02 13:46     ` Boqun Feng
2023-02-02 14:21     ` Gary Guo
2023-02-02 15:41       ` Greg KH
2023-02-02 16:10         ` Boqun Feng
2023-02-02 16:17           ` Greg KH [this message]
2023-02-02 16:51             ` Boqun Feng
2023-02-02 21:47               ` Miguel Ojeda
2023-02-03  5:22                 ` Greg KH
2023-02-03  7:25                   ` Boqun Feng
2023-02-03  7:38                     ` Greg KH
2023-02-03  7:43                       ` Boqun Feng
2023-02-03  8:01                       ` Boqun Feng
2023-02-03 19:17                       ` Josh Stone
2023-02-03 19:22                         ` Wedson Almeida Filho
2023-02-02 14:22   ` Gary Guo
2023-02-04 18:48   ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 3/5] rust: sync: Arc: Introduces Arc::get_inner() helper Boqun Feng
2023-02-02 14:24   ` Gary Guo
2023-02-02 16:53   ` Björn Roy Baron
2023-02-04 18:51   ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 4/5] rust: sync: impl Debug for {Unique,}Arc Boqun Feng
2023-02-02 14:28   ` Gary Guo
2023-02-03 19:46     ` Boqun Feng
2023-02-04 18:56   ` Vincenzo Palazzo
2023-02-01 23:22 ` [RFC 5/5] sample: rust: print: Add sampe code for Arc printing Boqun Feng
2023-02-02 16:56   ` Björn Roy Baron
2023-02-04 10:22   ` Finn Behrens
2023-02-04 19:05   ` Vincenzo Palazzo

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=Y9viM2POUsSnbKUh@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=alex.gaynor@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --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=vincenzopalazzodev@gmail.com \
    --cc=wedsonaf@gmail.com \
    --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