From: Markus Probst <markus.probst@posteo.de>
To: Danilo Krummrich <dakr@kernel.org>,
Markus Probst via B4 Relay
<devnull+markus.probst.posteo.de@kernel.org>
Cc: Rob Herring <robh@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>,
Miguel Ojeda <ojeda@kernel.org>, Gary Guo <gary@garyguo.net>,
Björn Roy Baron <bjorn3_gh@protonmail.com>,
Benno Lossin <lossin@kernel.org>,
Andreas Hindborg <a.hindborg@kernel.org>,
Alice Ryhl <aliceryhl@google.com>,
Trevor Gross <tmgross@umich.edu>,
Kari Argillander <kari.argillander@gmail.com>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Viresh Kumar <viresh.kumar@linaro.org>,
Boqun Feng <boqun@kernel.org>, David Airlie <airlied@gmail.com>,
Simona Vetter <simona@ffwll.ch>,
linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org,
rust-for-linux@vger.kernel.org, linux-pm@vger.kernel.org,
driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v11 1/3] rust: add basic serial device bus abstractions
Date: Sun, 31 May 2026 19:42:21 +0000 [thread overview]
Message-ID: <12fa6237168ecf6dc65ebf46118beeb5c51f3d1c.camel@posteo.de> (raw)
In-Reply-To: <DIWEXUCNLURG.136XXDBHSBNVR@kernel.org>
[-- Attachment #1: Type: text/plain, Size: 3963 bytes --]
On Sun, 2026-05-31 at 01:51 +0200, Danilo Krummrich wrote:
> On Sun May 31, 2026 at 12:51 AM CEST, Markus Probst via B4 Relay wrote:
> > +#[pinned_drop]
> > +impl<T: Driver> PinnedDrop for PrivateData<'_, T> {
> > + fn drop(self: Pin<&mut Self>) {
> > + let mut active = self.active.lock();
> > + if *active {
> > + // SAFETY:
> > + // - We have exclusive access to `self.driver`.
> > + // - `self.driver` is guaranteed to be initialized.
> > + unsafe { (*self.driver.get()).assume_init_drop() };
> > + *active = false;
> > + }
> > +
> > + // SAFETY: We have exclusive access to `self.open`.
> > + if unsafe { *self.open.get() } {
> > + // SAFETY: `self.sdev.as_raw()` is guaranteed to be a pointer to a valid
> > + // `struct serdev_device`.
> > + unsafe { bindings::serdev_device_close(self.sdev.as_raw()) };
> > + }
> > + }
> > +}
>
> Just in case it came across otherwise, I did not mean to push for you to go for
> this approach. We just kept discussing it because it let to the (to me more
> interesting) discussion around the LED class device abstraction.
>
> While this approach gets us rid of an extra allocation and the rust_private_data
> pointer in struct serdev_device it also turns out a bit more convoluted.
>
> That said, both are correct, and I won't object either one; up to you and the
> serdev / tty maintainers.
>
> Please wait a bit before resending, so other people can comment on this as well.
>
> > + extern "C" fn receive_buf_callback(
> > + sdev: *mut bindings::serdev_device,
> > + buf: *const u8,
> > + length: usize,
> > + ) -> usize {
> > + // SAFETY: The serial device bus only ever calls the receive buf callback with a valid
> > + // pointer to a `struct serdev_device`.
> > + //
> > + // INVARIANT: `sdev` is valid for the duration of `receive_buf_callback()`.
> > + let sdev = unsafe { &*sdev.cast::<Device<device::CoreInternal<'_>>>() };
>
> CoreInternal snuck back in, should be BoundInternal.
>
> > +
> > + // SAFETY: `receive_buf_callback` is only ever called after a successful call to
> > + // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
> > + // and stored a `Pin<KBox<PrivateData<'_, T>>>`.
> > + let private_data = unsafe { sdev.as_ref().drvdata_borrow::<PrivateData<'_, T>>() };
> > + let active = private_data.active.lock();
>
> I think SRCU would be a much better fit, but the code didn't land yet, so the
> mutex seems fine for now. But I'd probably add a TODO.
I just noticed, is it even possible to use SRCU here? Currently the
mutex not only ensures that no drvdata access happens after drvdata
drop, but also that the receive_buf waits for the probe to complete, as
the drvdata hasn't been initialized yet.
(drvdata refers to PrivateData.driver in this context, not the whole
PrivateData)
Thanks
- Markus Probst
>
> > +
> > + if !*active {
> > + return length;
> > + }
> > +
> > + // SAFETY: No one has exclusive access to `private_data.driver`.
> > + let data = unsafe { &*private_data.driver.get() };
> > + // SAFETY:
> > + // - `private_data.driver` is pinned.
> > + // - `receive_buf_callback` is only ever called after a successful call to `probe_callback`,
> > + // hence it's guaranteed that `private_data.driver` was initialized.
> > + let data_pinned = unsafe { Pin::new_unchecked(data.assume_init_ref()) };
> > +
> > + // SAFETY: `buf` is guaranteed to be non-null and has the size of `length`.
> > + let buf = unsafe { core::slice::from_raw_parts(buf, length) };
> > +
> > + T::receive(sdev, data_pinned, buf)
> > + }
> > +}
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 870 bytes --]
next prev parent reply other threads:[~2026-05-31 19:42 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-30 22:51 [PATCH v11 0/3] rust: add basic serial device bus abstractions Markus Probst via B4 Relay
2026-05-30 22:51 ` [PATCH v11 1/3] " Markus Probst via B4 Relay
2026-05-30 23:51 ` Danilo Krummrich
2026-05-31 6:58 ` Onur Özkan
2026-05-31 7:01 ` Onur Özkan
2026-05-31 16:37 ` Markus Probst
2026-05-31 17:57 ` Danilo Krummrich
2026-05-31 19:42 ` Markus Probst [this message]
2026-05-31 21:49 ` Danilo Krummrich
2026-05-31 22:00 ` Markus Probst
2026-05-31 22:32 ` Danilo Krummrich
2026-06-01 0:13 ` Markus Probst
2026-06-04 5:07 ` Claude review: " Claude Code Review Bot
2026-05-30 22:51 ` [PATCH v11 2/3] samples: rust: add Rust serial device bus sample device driver Markus Probst via B4 Relay
2026-06-04 5:07 ` Claude review: " Claude Code Review Bot
2026-05-30 22:51 ` [PATCH v11 3/3] MAINTAINERS: serdev: Add self for serdev Markus Probst via B4 Relay
2026-06-04 5:07 ` Claude review: " Claude Code Review Bot
2026-06-04 5:07 ` Claude review: rust: add basic serial device bus abstractions Claude Code Review Bot
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=12fa6237168ecf6dc65ebf46118beeb5c51f3d1c.camel@posteo.de \
--to=markus.probst@posteo.de \
--cc=a.hindborg@kernel.org \
--cc=airlied@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=devnull+markus.probst.posteo.de@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=driver-core@lists.linux.dev \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=kari.argillander@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=viresh.kumar@linaro.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