From nobody Tue Feb 10 04:12:20 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E995EB64DC for ; Tue, 11 Jul 2023 09:49:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231720AbjGKJti (ORCPT ); Tue, 11 Jul 2023 05:49:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231558AbjGKJtc (ORCPT ); Tue, 11 Jul 2023 05:49:32 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C83EA9 for ; Tue, 11 Jul 2023 02:49:31 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c4e77129acfso6680126276.3 for ; Tue, 11 Jul 2023 02:49:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689068970; x=1691660970; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/EqMekonA8uGHFD/lDqj3wOWtQfGKVNTfQouXEykc8E=; b=IhDUOhT0+DvvMgAAceahZcV7aUb3Zv9bWU0qe0g6pEouMuHOgruEFQzK3tcCGz0fY0 dNS22UXL6tq2hBKNniRASmkbPF+d7fQXIRahB/BzW81ZQx4nJ09+2qAuZc7MquRblvcu B16dRYK5dohuej1KyH+5cySONL8YFRLl1Q4FqpPX0X8h3AiNYRFM0VgPDnlAdkr4qpm/ aO71pzpqHLppLt/f+32LBAs2DjfHYu5qKmdBH+7BtfMChJq6jooxLy9I1mphNkfG77LK IxnqMigeSRhV5KTLFgtb17TR6635l4Rc3+dg1lFsZhZiQHhaTjpgXH3XCCbgqJ6YFuCn pqOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689068970; x=1691660970; 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=/EqMekonA8uGHFD/lDqj3wOWtQfGKVNTfQouXEykc8E=; b=UM18nGpiVTkr8x3Hp02mkyUeP39xNULE7vfEbDQZrs7ZD29cvQYGMq22V1kvJ+RZkL oFA9D8CYtFyWHxDhC+jJOIOo7ynXcPkUKUgIDeRxi2jkMFUyuHz3KFiUfu2jff//mUZE MrswG0/Mwf/Wlk8abWLbp3omhx2bxfmtjYprlQlLqvPBu/+8HGxshxWZqAlCOdT0X30W dRdywX7pacnFp1yQR2JVJEgc7ej3/dssrFROHrUErDxxgUQ/Rd+X+knIvqVYdxQMLNEZ Rqw4HLv56dQRG0S7nor6Iw/pCAZT7wVk1BNwzpOFeBZMQTuURSPrQusauJp/VxjwWjSK mz8A== X-Gm-Message-State: ABy/qLagypgRP2Lpt8WMzX+Dju+Wme6inJjNnZVhI6Omj+FbvYYYCc6z swlDKpMrgUDHll2EBJlXN8jXUmH1y+1eFBE= X-Google-Smtp-Source: APBJJlHEHcRVDUW4+Y0GUJSVpFZcA3B0pSPwgs7AJhZbg/V8hUXtvAnY3gg+gxzZTCY2shizts2jw+x1sE8BMX0= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a25:cd09:0:b0:c6c:6122:5b69 with SMTP id d9-20020a25cd09000000b00c6c61225b69mr107979ybf.8.1689068970724; Tue, 11 Jul 2023 02:49:30 -0700 (PDT) Date: Tue, 11 Jul 2023 09:33:01 +0000 In-Reply-To: <20230711093303.1433770-1-aliceryhl@google.com> Mime-Version: 1.0 References: <20230711093303.1433770-1-aliceryhl@google.com> X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230711093303.1433770-8-aliceryhl@google.com> Subject: [PATCH v3 7/9] rust: workqueue: implement `WorkItemPointer` for pointer types From: Alice Ryhl To: rust-for-linux@vger.kernel.org, Tejun Heo , Miguel Ojeda Cc: Lai Jiangshan , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , "=?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?=" , Benno Lossin , Alice Ryhl , linux-kernel@vger.kernel.org, patches@lists.linux.dev, Martin Rodriguez Reboredo , Andreas Hindborg Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This implements the `WorkItemPointer` trait for the pointer types that you are likely to use the workqueue with. The `Arc` type is for reference counted objects, and the `Pin>` type is for objects where the caller has exclusive ownership of the object. Co-developed-by: Gary Guo Signed-off-by: Gary Guo Signed-off-by: Alice Ryhl Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Andreas Hindborg (Samsung) Reviewed-by: Benno Lossin --- v2 -> v3: * Use `cast_mut` to cast pointer in `impl RawWorkItem for Arc` * Add Reviewed-by from Martin, Andreas, Benno. rust/kernel/workqueue.rs | 97 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index a7c972bda0ed..e760040762bd 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -28,8 +28,10 @@ //! //! C header: [`include/linux/workqueue.h`](../../../../include/linux/work= queue.h) =20 -use crate::{bindings, prelude::*, sync::LockClassKey, types::Opaque}; +use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Op= aque}; +use alloc::boxed::Box; use core::marker::PhantomData; +use core::pin::Pin; =20 /// A kernel work queue. /// @@ -345,6 +347,99 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate:= :workqueue::Work<$work_typ )*}; } =20 +unsafe impl WorkItemPointer for Arc +where + T: WorkItem, + T: HasWork, +{ + unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { + // SAFETY: The `__enqueue` method always uses a `work_struct` stor= ed in a `Work`. + let ptr =3D ptr as *mut Work; + // SAFETY: This computes the pointer that `__enqueue` got from `Ar= c::into_raw`. + let ptr =3D unsafe { T::work_container_of(ptr) }; + // SAFETY: This pointer comes from `Arc::into_raw` and we've been = given back ownership. + let arc =3D unsafe { Arc::from_raw(ptr) }; + + T::run(arc) + } +} + +unsafe impl RawWorkItem for Arc +where + T: WorkItem, + T: HasWork, +{ + type EnqueueOutput =3D Result<(), Self>; + + unsafe fn __enqueue(self, queue_work_on: F) -> Self::EnqueueOutput + where + F: FnOnce(*mut bindings::work_struct) -> bool, + { + // Casting between const and mut is not a problem as long as the p= ointer is a raw pointer. + let ptr =3D Arc::into_raw(self).cast_mut(); + + // SAFETY: Pointers into an `Arc` point at a valid value. + let work_ptr =3D unsafe { T::raw_get_work(ptr) }; + // SAFETY: `raw_get_work` returns a pointer to a valid value. + let work_ptr =3D unsafe { Work::raw_get(work_ptr) }; + + if queue_work_on(work_ptr) { + Ok(()) + } else { + // SAFETY: The work queue has not taken ownership of the point= er. + Err(unsafe { Arc::from_raw(ptr) }) + } + } +} + +unsafe impl WorkItemPointer for Pin> +where + T: WorkItem, + T: HasWork, +{ + unsafe extern "C" fn run(ptr: *mut bindings::work_struct) { + // SAFETY: The `__enqueue` method always uses a `work_struct` stor= ed in a `Work`. + let ptr =3D ptr as *mut Work; + // SAFETY: This computes the pointer that `__enqueue` got from `Ar= c::into_raw`. + let ptr =3D unsafe { T::work_container_of(ptr) }; + // SAFETY: This pointer comes from `Arc::into_raw` and we've been = given back ownership. + let boxed =3D unsafe { Box::from_raw(ptr) }; + // SAFETY: The box was already pinned when it was enqueued. + let pinned =3D unsafe { Pin::new_unchecked(boxed) }; + + T::run(pinned) + } +} + +unsafe impl RawWorkItem for Pin> +where + T: WorkItem, + T: HasWork, +{ + type EnqueueOutput =3D (); + + unsafe fn __enqueue(self, queue_work_on: F) -> Self::EnqueueOutput + where + F: FnOnce(*mut bindings::work_struct) -> bool, + { + // SAFETY: We're not going to move `self` or any of its fields, so= its okay to temporarily + // remove the `Pin` wrapper. + let boxed =3D unsafe { Pin::into_inner_unchecked(self) }; + let ptr =3D Box::into_raw(boxed); + + // SAFETY: Pointers into a `Box` point at a valid value. + let work_ptr =3D unsafe { T::raw_get_work(ptr) }; + // SAFETY: `raw_get_work` returns a pointer to a valid value. + let work_ptr =3D unsafe { Work::raw_get(work_ptr) }; + + if !queue_work_on(work_ptr) { + // SAFETY: This method requires exclusive ownership of the box= , so it cannot be in a + // workqueue. + unsafe { ::core::hint::unreachable_unchecked() } + } + } +} + /// Returns the system work queue (`system_wq`). /// /// It is the one used by `schedule[_delayed]_work[_on]()`. Multi-CPU mult= i-threaded. There are --=20 2.41.0.255.g8b1d071c50-goog