mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: Danilo Krummrich <dakr@redhat.com>,
	Wedson Almeida Filho <wedsonaf@gmail.com>,
	ojeda@kernel.org, lyude@redhat.com, robh@kernel.org,
	lina@asahilina.net, mcanal@igalia.com, airlied@gmail.com,
	"daniel@ffwll.ch" <daniel@ffwll.ch>,
	rust-for-linux@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH] drm: panthor: add dev_coredumpv support
Date: Mon, 15 Jul 2024 14:05:49 -0300	[thread overview]
Message-ID: <89FFB898-120A-431E-9B18-4D08AFAEFBFD@collabora.com> (raw)
In-Reply-To: <ZpTl-uUbzmvHGMEH@phenom.ffwll.local>

Hi Sima!


> 
> Yeah I'm not sure a partially converted driver where the main driver is
> still C really works, that pretty much has to throw out all the type
> safety in the interfaces.
> 
> What I think might work is if such partial drivers register as full rust
> drivers, and then largely delegate the implementation to their existing C
> code with a big "safety: trust me, the C side is bug free" comment since
> it's all going to be unsafe :-)
> 
> It would still be a big change, since all the driver's callbacks need to
> switch from container_of to upcast to their driver structure to some small
> rust shim (most likely, I didn't try this out) to get at the driver parts
> on the C side. And I think you also need a small function to downcast to
> the drm base class. But that should be all largely mechanical.
> 
> More freely allowing to mix&match is imo going to be endless pains. We
> kinda tried that with the atomic conversion helpers for legacy kms
> drivers, and the impendance mismatch was just endless amounts of very
> subtle pain. Rust will exacerbate this, because it encodes semantics into
> the types and interfaces. And that was with just one set of helpers, for
> rust we'll likely need a custom one for each driver that's partially
> written in rust.
> -Sima
> 

I humbly disagree here.

I know this is a bit tangential, but earlier this year I converted a bunch of codec libraries to Rust in v4l2. That worked just fine with the C codec drivers. There were no regressions as per our test tools.

The main idea is that you isolate all unsafety to a single point: so long as the C code upholds the safety guarantees when calling into Rust, the Rust layer will be safe. This is just the same logic used in unsafe blocks in Rust itself, nothing new really.

This is not unlike what is going on here, for example:


```
+unsafe extern "C" fn open_callback<T: BaseDriverObject<U>, U: BaseObject>(
+ raw_obj: *mut bindings::drm_gem_object,
+ raw_file: *mut bindings::drm_file,
+) -> core::ffi::c_int {
+ // SAFETY: The pointer we got has to be valid.
+ let file = unsafe {
+ file::File::<<<U as IntoGEMObject>::Driver as drv::Driver>::File>::from_raw(raw_file)
+ };
+ let obj =
+ <<<U as IntoGEMObject>::Driver as drv::Driver>::Object as IntoGEMObject>::from_gem_obj(
+ raw_obj,
+ );
+
+ // SAFETY: from_gem_obj() returns a valid pointer as long as the type is
+ // correct and the raw_obj we got is valid.
+ match T::open(unsafe { &*obj }, &file) {
+ Err(e) => e.to_errno(),
+ Ok(()) => 0,
+ }
+}
```

We have to trust that the kernel is passing in a valid pointer. By the same token, we can choose to trust drivers if we so desire.

> that pretty much has to throw out all the type
> safety in the interfaces.

Can you expand on that?

In particular, I believe that we should ideally be able to convert from a C "struct Foo * " to a Rust “FooRef" for types whose lifetimes are managed either by the kernel itself or by a C driver. In practical terms, this has run into the issues we’ve been discussing in this thread, but there may be solutions e.g.:

> One thing that comes to my mindis , you could probably create some driver specific
> "dummy" types to satisfy the type generics of the types you want to use. Not sure
> how well this works out though.

I haven’t thought of anything yet - which is why I haven’t replied. OTOH, IIRC, Faith seems to have something in mind that can work with the current abstractions, so I am waiting on her reply.


> What I think might work is if such partial drivers register as full rust
> drivers, and then largely delegate the implementation to their existing C
> code with a big "safety: trust me, the C side is bug free" comment since
> it's all going to be unsafe :-)

> with a big "safety: trust me, the C side is bug free" comment since it's all going to be unsafe :-)

This is what I want too :) but I can’t see how your proposed approach is better, at least at a cursory glance. It is a much bigger change, though, which is a clear drawback.

> And that was with just one set of helpers, for
> rust we'll likely need a custom one for each driver that's partially
> written in rust.

That’s exactly what I am trying to avoid. In other words, I want to find a way to use the same abstractions and the same APIs so that we do not run precisely into that problem.

— Daniel


  reply	other threads:[~2024-07-15 17:06 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-10 22:50 Daniel Almeida
2024-07-11  0:01 ` Danilo Krummrich
2024-07-15  9:03   ` Daniel Vetter
2024-07-15 17:05     ` Daniel Almeida [this message]
2024-07-16  9:25       ` Daniel Vetter
2024-07-25 19:35         ` Lyude Paul
2024-07-26 13:40           ` Daniel Vetter
2024-07-29 18:34             ` Lyude Paul
2024-07-30  8:29               ` Daniel Vetter
2024-07-11 16:57 ` Liviu Dudau
2024-07-11 18:40   ` Daniel Almeida
2024-07-12  9:46 ` Steven Price
2024-07-12 14:35   ` Daniel Almeida
2024-07-12 14:53     ` Danilo Krummrich
2024-07-12 15:13       ` Daniel Almeida
2024-07-12 15:32         ` Danilo Krummrich
2024-07-13  0:48           ` Dave Airlie
2024-07-13  1:00             ` Daniel Almeida
2024-07-13  8:17             ` Miguel Ojeda
2024-07-15  9:12     ` Steven Price
2024-07-23  9:44       ` Alice Ryhl
2024-07-23 16:06       ` Boris Brezillon
2024-07-23 17:23         ` Daniel Almeida
2024-07-24  8:59         ` Steven Price
2024-07-24 10:44           ` Boris Brezillon
2024-07-24 12:37             ` Steven Price
2024-07-24 13:15           ` Rob Herring
2024-07-24 13:54             ` Steven Price
2024-07-24 14:27               ` Daniel Almeida
2024-07-24 14:35                 ` Steven Price
2024-07-24 14:38               ` Miguel Ojeda
2024-07-25 11:42               ` Carsten Haitzler
2024-07-25 11:45         ` Carsten Haitzler
2024-07-23  9:53 ` Alice Ryhl
2024-07-23 13:41   ` Daniel Almeida
2024-07-23 13:45     ` Alice Ryhl
2024-08-13 21:05 ` [PATCH v2 0/5] Panthor devcoredump support Daniel Almeida
2024-08-13 21:05   ` [PATCH v2 1/5] drm: panthor: expose some fw information through the query ioctl Daniel Almeida
2024-08-14 13:44     ` Boris Brezillon
2024-08-13 21:05   ` [PATCH v2 2/5] drm: panthor: add devcoredump support Daniel Almeida
2024-08-15  6:01     ` kernel test robot
2024-08-13 21:05   ` [PATCH v2 3/5] drm: panthor: add debugfs support in panthor_sched Daniel Almeida
2024-08-13 21:05   ` [PATCH v2 4/5] drm: panthor: add debugfs knob to dump successful jobs Daniel Almeida
2024-08-13 21:05   ` [PATCH v2 5/5] drm: panthor: allow dumping multiple jobs Daniel Almeida
2024-08-21 14:37 ` [PATCH v2 RESEND 0/5] Panthor devcoredump support Daniel Almeida
2024-08-21 14:37   ` [PATCH v2 RESEND 1/5] drm: panthor: expose some fw information through the query ioctl Daniel Almeida
2024-08-22 12:26     ` Adrian Larumbe
2024-08-23 13:15     ` Boris Brezillon
2024-09-20 12:57     ` Mihail Atanassov
2024-08-21 14:37   ` [PATCH v2 RESEND 2/5] drm: panthor: add devcoredump support Daniel Almeida
2024-08-22 12:27     ` Adrian Larumbe
2024-08-23 14:46     ` Boris Brezillon
2024-08-21 14:37   ` [PATCH v2 RESEND 3/5] drm: panthor: add debugfs support in panthor_sched Daniel Almeida
2024-08-21 14:37   ` [PATCH v2 RESEND 4/5] drm: panthor: add debugfs knob to dump successful jobs Daniel Almeida
2024-08-22 12:36     ` Adrian Larumbe
2024-08-21 14:37   ` [PATCH v2 RESEND 5/5] drm: panthor: allow dumping multiple jobs Daniel Almeida

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=89FFB898-120A-431E-9B18-4D08AFAEFBFD@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=airlied@gmail.com \
    --cc=dakr@redhat.com \
    --cc=daniel.vetter@ffwll.ch \
    --cc=daniel@ffwll.ch \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyude@redhat.com \
    --cc=mcanal@igalia.com \
    --cc=ojeda@kernel.org \
    --cc=robh@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --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

all inboxes | Powered by JetHome®