From: Danilo Krummrich <dakr@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>
Cc: gregkh@linuxfoundation.org, rafael@kernel.org,
bhelgaas@google.com, ojeda@kernel.org, alex.gaynor@gmail.com,
boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com,
benno.lossin@proton.me, tmgross@umich.edu,
a.hindborg@samsung.com, airlied@gmail.com,
fujita.tomonori@gmail.com, lina@asahilina.net,
pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com,
robh@kernel.org, daniel.almeida@collabora.com,
saravanak@google.com, dirk.behme@de.bosch.com, j@jannau.net,
fabien.parent@linaro.org, chrisi.schrefl@gmail.com,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-pci@vger.kernel.org, devicetree@vger.kernel.org,
Wedson Almeida Filho <wedsonaf@gmail.com>
Subject: Re: [PATCH v4 05/13] rust: add `Revocable` type
Date: Mon, 9 Dec 2024 11:40:12 +0100 [thread overview]
Message-ID: <Z1bJDDEmI8Ew0exS@pollux.localdomain> (raw)
In-Reply-To: <CAH5fLggw8Z74updxX6HtXRnZ7Zk16_ZLCoi-wkj=t2khhoV6mQ@mail.gmail.com>
On Fri, Dec 06, 2024 at 04:11:39PM +0100, Alice Ryhl wrote:
> On Thu, Dec 5, 2024 at 3:16 PM Danilo Krummrich <dakr@kernel.org> wrote:
> >
> > From: Wedson Almeida Filho <wedsonaf@gmail.com>
> >
> > Revocable allows access to objects to be safely revoked at run time.
> >
> > This is useful, for example, for resources allocated during device probe;
> > when the device is removed, the driver should stop accessing the device
> > resources even if another state is kept in memory due to existing
> > references (i.e., device context data is ref-counted and has a non-zero
> > refcount after removal of the device).
> >
> > Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com>
> > Co-developed-by: Danilo Krummrich <dakr@kernel.org>
> > Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>
> Overall looks reasonable, but some comments below.
>
> > +impl<T> Revocable<T> {
> > + /// Creates a new revocable instance of the given data.
> > + pub fn new(data: impl PinInit<T>) -> impl PinInit<Self> {
> > + pin_init!(Self {
> > + is_available: AtomicBool::new(true),
> > + // SAFETY: The closure only returns `Ok(())` if `ptr` is fully initialized; on error
> > + // `ptr` is not partially initialized and does not need to be dropped.
> > + data <- unsafe {
> > + Opaque::try_ffi_init(|ptr: *mut T| {
> > + init::PinInit::<T, core::convert::Infallible>::__pinned_init(data, ptr)
> > + })
>
> This is pretty awkward ... could we have an Opaque::pin_init that
> takes an `impl PinInit instead of using fii_init?
Using ffi_init was your suggestion. :) But I agree, having Opaque::pin_init
would be more convenient. I can add a patch for that.
>
> > + },
> > + })
> > + }
> > +
> > + /// Tries to access the revocable wrapped object.
> > + ///
> > + /// Returns `None` if the object has been revoked and is therefore no longer accessible.
> > + ///
> > + /// Returns a guard that gives access to the object otherwise; the object is guaranteed to
> > + /// remain accessible while the guard is alive. In such cases, callers are not allowed to sleep
> > + /// because another CPU may be waiting to complete the revocation of this object.
> > + pub fn try_access(&self) -> Option<RevocableGuard<'_, T>> {
> > + let guard = rcu::read_lock();
> > + if self.is_available.load(Ordering::Relaxed) {
> > + // Since `self.is_available` is true, data is initialised and has to remain valid
> > + // because the RCU read side lock prevents it from being dropped.
> > + Some(RevocableGuard::new(self.data.get(), guard))
> > + } else {
> > + None
> > + }
> > + }
> > +
> > + /// Tries to access the revocable wrapped object.
> > + ///
> > + /// Returns `None` if the object has been revoked and is therefore no longer accessible.
> > + ///
> > + /// Returns a shared reference to the object otherwise; the object is guaranteed to
> > + /// remain accessible while the rcu read side guard is alive. In such cases, callers are not
> > + /// allowed to sleep because another CPU may be waiting to complete the revocation of this
> > + /// object.
> > + pub fn try_access_with_guard<'a>(&'a self, _guard: &'a rcu::Guard) -> Option<&'a T> {
> > + if self.is_available.load(Ordering::Relaxed) {
> > + // SAFETY: Since `self.is_available` is true, data is initialised and has to remain
> > + // valid because the RCU read side lock prevents it from being dropped.
> > + Some(unsafe { &*self.data.get() })
> > + } else {
> > + None
> > + }
> > + }
> > +
> > + /// # Safety
> > + ///
> > + /// Callers must ensure that there are no more concurrent users of the revocable object.
> > + unsafe fn revoke_internal(&self, sync: bool) {
>
> This boolean could be a const generic to enforce that it must be a
> compile-time value.
Agreed.
>
> Alice
next prev parent reply other threads:[~2024-12-09 10:40 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-05 14:14 [PATCH v4 00/13] Device / Driver PCI / Platform Rust abstractions Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 01/13] rust: pass module name to `Module::init` Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 02/13] rust: implement generic driver registration Danilo Krummrich
2024-12-06 13:57 ` Alice Ryhl
2024-12-06 18:13 ` Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 03/13] rust: implement `IdArray`, `IdTable` and `RawDeviceId` Danilo Krummrich
2024-12-07 1:14 ` Fabien Parent
2024-12-09 10:45 ` Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 04/13] rust: add rcu abstraction Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 05/13] rust: add `Revocable` type Danilo Krummrich
2024-12-06 15:11 ` Alice Ryhl
2024-12-09 10:40 ` Danilo Krummrich [this message]
2024-12-05 14:14 ` [PATCH v4 06/13] rust: add `io::{Io, IoRaw}` base types Danilo Krummrich
2024-12-06 14:13 ` Alice Ryhl
2024-12-11 14:52 ` Daniel Almeida
2024-12-05 14:14 ` [PATCH v4 07/13] rust: add devres abstraction Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 08/13] rust: pci: add basic PCI device / driver abstractions Danilo Krummrich
2024-12-06 14:01 ` Alice Ryhl
2024-12-09 10:44 ` Danilo Krummrich
2024-12-10 10:55 ` Alice Ryhl
2024-12-10 22:38 ` Danilo Krummrich
2024-12-11 13:06 ` Alice Ryhl
2024-12-11 14:32 ` Danilo Krummrich
2024-12-11 14:41 ` Greg KH
2024-12-11 14:42 ` Greg KH
2024-12-11 14:44 ` Alice Ryhl
2024-12-06 15:25 ` Alice Ryhl
2024-12-05 14:14 ` [PATCH v4 09/13] rust: pci: implement I/O mappable `pci::Bar` Danilo Krummrich
2024-12-06 10:44 ` Philipp Stanner
2024-12-05 14:14 ` [PATCH v4 10/13] samples: rust: add Rust PCI sample driver Danilo Krummrich
2024-12-05 14:14 ` [PATCH v4 11/13] rust: of: add `of::DeviceId` abstraction Danilo Krummrich
2024-12-09 21:22 ` Rob Herring
2024-12-05 14:14 ` [PATCH v4 12/13] rust: platform: add basic platform device / driver abstractions Danilo Krummrich
2024-12-09 22:37 ` Rob Herring
2024-12-09 23:13 ` Danilo Krummrich
2024-12-10 7:46 ` Greg KH
2024-12-10 9:34 ` Danilo Krummrich
2024-12-10 9:40 ` Greg KH
2024-12-05 14:14 ` [PATCH v4 13/13] samples: rust: add Rust platform sample driver Danilo Krummrich
2024-12-05 17:09 ` Dirk Behme
2024-12-05 18:03 ` Rob Herring
2024-12-06 6:39 ` Dirk Behme
2024-12-06 8:33 ` Danilo Krummrich
2024-12-06 9:29 ` Dirk Behme
2024-12-10 22:59 ` Rob Herring (Arm)
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=Z1bJDDEmI8Ew0exS@pollux.localdomain \
--to=dakr@kernel.org \
--cc=a.hindborg@samsung.com \
--cc=airlied@gmail.com \
--cc=ajanulgu@redhat.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=chrisi.schrefl@gmail.com \
--cc=daniel.almeida@collabora.com \
--cc=devicetree@vger.kernel.org \
--cc=dirk.behme@de.bosch.com \
--cc=fabien.parent@linaro.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=j@jannau.net \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=pstanner@redhat.com \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=saravanak@google.com \
--cc=tmgross@umich.edu \
--cc=wedsonaf@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