All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Danilo Krummrich <dakr@kernel.org>
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, 1 Mar 2026 12:06:42 +0000	[thread overview]
Message-ID: <aaQr0ntSw9Q5Qzws@google.com> (raw)
In-Reply-To: <DGQTCDOIVTHW.3OGUVXRS496FP@kernel.org>

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.

> 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();
            }
        }
    }

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

Note that this is a general workqueue API thing. It's not specific to
ARef<_> or drm::Device<_> based work items, but applies to all users of
the workqueue API.

Alice

  reply	other threads:[~2026-03-01 12:06 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 [this message]
2026-03-01 15:18       ` Danilo Krummrich
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=aaQr0ntSw9Q5Qzws@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=airlied@gmail.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.