public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Alice Ryhl" <aliceryhl@google.com>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>, "Tejun Heo" <tj@kernel.org>,
	"Lai Jiangshan" <jiangshanlai@gmail.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v2 2/4] rust: drm: dispatch work items to the private data
Date: Sun, 01 Mar 2026 16:18:15 +0100	[thread overview]
Message-ID: <DGRJO4NKZ4R5.2BZ2D9BSPXDMT@kernel.org> (raw)
In-Reply-To: <aaQr0ntSw9Q5Qzws@google.com>

On Sun Mar 1, 2026 at 1:06 PM CET, Alice Ryhl wrote:
> On Sat, Feb 28, 2026 at 07:40:26PM +0100, Danilo Krummrich wrote:
>> On Wed Feb 4, 2026 at 9:40 PM CET, Daniel Almeida wrote:
>> > This implementation dispatches any work enqueued on ARef<drm::Device<T>> to
>> > its driver-provided handler. It does so by building upon the newly-added
>> > ARef<T> support in workqueue.rs in order to call into the driver
>> > implementations for work_container_of and raw_get_work.
>> >
>> > This is notably important for work items that need access to the drm
>> > device, as it was not possible to enqueue work on a ARef<drm::Device<T>>
>> > previously without failing the orphan rule.
>> >
>> > The current implementation needs T::Data to live inline with drm::Device in
>> > order for work_container_of to function. This restriction is already
>> > captured by the trait bounds. Drivers that need to share their ownership of
>> > T::Data may trivially get around this:
>> >
>> > // Lives inline in drm::Device
>> > struct DataWrapper {
>> >   work: ...,
>> >   // Heap-allocated, shared ownership.
>> >   data: Arc<DriverData>,
>> > }
>> 
>> IIUC, this is how it's supposed to be used:
>> 
>> 	#[pin_data]
>> 	struct MyData {
>> 	    #[pin]
>> 	    work: Work<drm::Device<MyDriver>>,
>> 	    value: u32,
>> 	}
>> 	
>> 	impl_has_work! {
>> 	    impl HasWork<drm::Device<MyDriver>> for MyData { self.work }
>> 	}
>> 	
>> 	impl WorkItem for MyData {
>> 	    type Pointer = ARef<drm::Device<MyDriver>>;
>> 	
>> 	    fn run(dev: ARef<drm::Device<MyDriver>>) {
>> 	        dev_info!(dev, "value = {}\n", dev.value);
>> 	    }
>> 	}
>> 
>> The reason the WorkItem is implemented for MyData, rather than
>> drm::Device<MyDriver> (which would be a bit more straight forward) is the orphan
>> rule, I assume.
>
> This characterizes it as a workaround for the orphan rule. I don't think
> that's fair. Implementing WorkItem for MyDriver directly is the
> idiomatic way to do it, in my opinion.

The trait bound is T::Data: WorkItem, not T: drm::Driver + WorkItem.
Implementing WorkItem for MyDriver seems more straight forward to me.

>> Now, the whole purpose of this is that a driver can implement WorkItem for
>> MyData without needing an additional struct (and allocation), such as:
>> 
>> 	#[pin_data]
>> 	struct MyWork {
>> 	    #[pin]
>> 	    work: Work<Self>,
>> 	    dev: drm::Device<MyDriver>,
>> 	}
>> 
>> How is this supposed to be done when you want multiple different implementations
>> of WorkItem that have a drm::Device<MyDriver> as payload?
>> 
>> Fall back to various struct MyWork? Add in an "artificial" type state for MyData
>> with some phantom data, so you can implement HasWork for MyData<Work0>,
>> MyData<Work1>, etc.?
>
> You cannot configure the code that is executed on a per-call basis
> because the code called by a work item is a function pointer stored
> inside the `struct work_struct`. And it can't be changed after
> initialization of the field.
>
> So either you must store that info in a separate field. This is what
> Binder does, see drivers/android/binder/process.rs for an example.
>
>     impl workqueue::WorkItem for Process {
>         type Pointer = Arc<Process>;
>     
>         fn run(me: Arc<Self>) {
>             let defer;
>             {
>                 let mut inner = me.inner.lock();
>                 defer = inner.defer_work;
>                 inner.defer_work = 0;
>             }
>     
>             if defer & PROC_DEFER_FLUSH != 0 {
>                 me.deferred_flush();
>             }
>             if defer & PROC_DEFER_RELEASE != 0 {
>                 me.deferred_release();
>             }
>         }
>     }

Ok, so this would be a switch to decide what to do when a single work is run,
i.e. it is not for running multiple work.

> Or you must have multiple different fields of type Work, each with a
> different function pointer stored inside it.

This sounds it works for running multiple work, but I wonder how enqueue() knows
which work should be run in this case? I.e. what do we do with:

	impl_has_work! {
	    impl HasWork<drm::Device<MyDriver>> for MyData { self.work }
	}

  reply	other threads:[~2026-03-01 15:18 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-04 20:40 [PATCH v2 0/4] rust: Add ARef support for work items Daniel Almeida
2026-02-04 20:40 ` [PATCH v2 1/4] rust: workqueue: add support for ARef<T> Daniel Almeida
2026-02-04 20:40 ` [PATCH v2 2/4] rust: drm: dispatch work items to the private data Daniel Almeida
2026-02-28 18:40   ` Danilo Krummrich
2026-03-01 12:06     ` Alice Ryhl
2026-03-01 15:18       ` Danilo Krummrich [this message]
2026-03-01 16:53         ` Alice Ryhl
2026-02-04 20:40 ` [PATCH v2 3/4] rust: workqueue: add delayed work support for ARef<T> Daniel Almeida
2026-02-04 20:40 ` [PATCH v2 4/4] rust: drm: dispatch delayed work items to the private data Daniel Almeida
2026-02-16 16:20 ` [PATCH v2 0/4] rust: Add ARef support for work items Daniel Almeida
2026-02-28 17:35   ` Daniel Almeida
2026-03-02 16:37     ` Tejun Heo
2026-03-02 16:46       ` Alice Ryhl
2026-03-01 20:07 ` Danilo Krummrich
2026-03-02 16:47 ` Alice Ryhl

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=DGRJO4NKZ4R5.2BZ2D9BSPXDMT@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tj@kernel.org \
    --cc=tmgross@umich.edu \
    /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