From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 878961F1932 for ; Sun, 1 Mar 2026 16:53:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772384011; cv=none; b=TCxahTtxSYsjdItd8qSd+ncl+2sN9bLmRsiE1bbrqv7gSwZd7Tiz0SW/KTBXnLRPDrnZTzNoCWFISBK+ovDrL6fm+29F9a9+Ps1XvRRw+SA3GN7AEPMTZ0lR1JzpbHrwum530EJvVq9erlUzkn67MYYnI+n5VVRM9jy+DECQ2ow= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772384011; c=relaxed/simple; bh=y/n4171M5AXZrmPSe1LHW4DkH4/xTU37QaESKgeepEo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Eo77ebAcV4NN8WBzkjnsLuJtWddc/tyavoR4XJ8GAVTbXGDJ9psFyymIGl9t/OG3+dsdLHcCGEd8ShG/sJTzIQ8T5Buv2o5KD2VLvzNhP7Gmkz7MBYmcL6L/Kaf1guHwKbEJ7Tom7+NS5r+VvxtlxXYeUS89FuoRL80xuENML08= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=1I8DhpNl; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="1I8DhpNl" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-439b484ee04so197116f8f.1 for ; Sun, 01 Mar 2026 08:53:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772384008; x=1772988808; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PfdUfCNI/OQhhLtw/VeFZbribXWs9YmY3yISi0+RLUA=; b=1I8DhpNlFpzU+38LaUorn4sGdc8U6B25Sp5mTaH9YFGe5PcCEZgFCWoyioGCii3dgX Z5DWsIGD8cxQ/LaO92CUQDqzmMkYhQmpP78ngIdE9pmorPHz5NGFulekegSb+OBBmh3U Q4sx4WnBS2NUt0qWORVznzbkrkJrEWAmJ3xth73G/Q391peOK9q0uvT9CPiTj60ZRMOK bT0k/0E8158ArNzvUYLJAK3NfQuU5X8c4Id1JQPGnUFQWCxksxV6ENtMCLgr39D2+IXi KuQcB5Loa/jJU0pMQ4q82O6VL8GFOOW0+1Xlr1VM7hDRVDG5sgr3Pu6jyqQAXthAAX8A jkFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772384008; x=1772988808; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PfdUfCNI/OQhhLtw/VeFZbribXWs9YmY3yISi0+RLUA=; b=HMVhKH4b1hb6M9eyJe72rv3q3yVkX+3A9NOBNojcfumgg4LM8i8WO55fR2xvFDIDy3 7z53rEw+VK5GNx34HT4nuPEtfCWHgeLlNok/cmgFulqxTdevY/ljc8muFFPiFIKr/JCT buXVyd6aPx/CBDH16zEe60iBgXz4/OIZOJ5aMKR6/lwpuhofIo7whhoOLE7FVYtYXo8x e6C/5oytOt1YrHcni7PiclBV/PsVEb7WhB6AiFzYijftXYJXLcxqak+vgUmJqYjW/sPB rQWlj/f0z8bca5ULdULBfQHZiqWC5ng64cs6oGM/sZ8gHlTD9obVABD3BWp5i+HpenQF k+jQ== X-Forwarded-Encrypted: i=1; AJvYcCXdELOv9rlaPvypyuAtB8/yDCc2RssCvhrb3zhT98BnjK5jA8Rc8qssA13hGwopZh7c5zj/UkpSerMRRw4=@vger.kernel.org X-Gm-Message-State: AOJu0YzYQTksvlhc8UOh0ilUkVPFQ6WR1pRqpwxIca48dyzovmSsZyLy FV6SdNMC6T0/ThhIqPAoH7PP91gNHzkfIVMffrRkre4t8NBHALQUy35bmOA1f8NziHWUP00hhFg 1zS/8NtchbgAqfDmAIA== X-Received: from wrbbs10.prod.google.com ([2002:a05:6000:70a:b0:439:901b:97cb]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2013:b0:439:afe3:6185 with SMTP id ffacd0b85a97d-439afe364e1mr6850853f8f.15.1772384007596; Sun, 01 Mar 2026 08:53:27 -0800 (PST) Date: Sun, 1 Mar 2026 16:53:26 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260204-aref-workitem-v2-0-bec25b012d2a@collabora.com> <20260204-aref-workitem-v2-2-bec25b012d2a@collabora.com> Message-ID: Subject: Re: [PATCH v2 2/4] rust: drm: dispatch work items to the private data From: Alice Ryhl To: Danilo Krummrich Cc: Daniel Almeida , Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , David Airlie , Simona Vetter , Tejun Heo , Lai Jiangshan , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Content-Type: text/plain; charset="utf-8" On Sun, Mar 01, 2026 at 04:18:15PM +0100, Danilo Krummrich wrote: > 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> to > >> > its driver-provided handler. It does so by building upon the newly-added > >> > ARef 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> > >> > 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, > >> > } > >> > >> IIUC, this is how it's supposed to be used: > >> > >> #[pin_data] > >> struct MyData { > >> #[pin] > >> work: Work>, > >> value: u32, > >> } > >> > >> impl_has_work! { > >> impl HasWork> for MyData { self.work } > >> } > >> > >> impl WorkItem for MyData { > >> type Pointer = ARef>; > >> > >> fn run(dev: ARef>) { > >> dev_info!(dev, "value = {}\n", dev.value); > >> } > >> } > >> > >> The reason the WorkItem is implemented for MyData, rather than > >> drm::Device (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. I missed the part about `for MyData` vs `for MyDriver`. Since you talked about the orphan rule I assumed you wanted the driver to implement it for `drm::Device` directly, which is what the orphan rule would prohibit, rather than for `MyDriver`. In any case, I do think it makes sense that you would implement it on the struct that actually contains the `struct work_struct`. > >> 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, > >> dev: drm::Device, > >> } > >> > >> How is this supposed to be done when you want multiple different implementations > >> of WorkItem that have a drm::Device 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, > >> MyData, 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; > > > > fn run(me: Arc) { > > 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. Yeah. But in any case, a single `struct work_struct` can't be used to schedule multiple work items. It only has one prev/next pointer pair. > > 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> for MyData { self.work } > } Both WorkItem and HasWork are generic over an ID integer. You can specify it to disambiguiate. Alice