[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230201232244.212908-3-boqun.feng@gmail.com>
Date: Wed, 1 Feb 2023 15:22:41 -0800
From: Boqun Feng <boqun.feng@...il.com>
To: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Cc: Will Deacon <will@...nel.org>,
Peter Zijlstra <peterz@...radead.org>,
Mark Rutland <mark.rutland@....com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Vincenzo Palazzo <vincenzopalazzodev@...il.com>
Subject: [RFC 2/5] rust: sync: Arc: Introduces ArcInner::count()
This allows reading the current count of a refcount in an `ArcInner`.
Signed-off-by: Boqun Feng <boqun.feng@...il.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 to allow [`Arc`] (and variants) to be used as the type of `self`.
impl<T: ?Sized> core::ops::Receiver for Arc<T> {}
--
2.39.1
Powered by blists - more mailing lists