Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp2426322rdb; Mon, 28 Aug 2023 04:15:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGeayf5mrn78GOU8KZ01skiMvhZUDJF0Gbvv0qx+wVzgajhDQaIAndC+y3ahfVsgL+Cg2g4 X-Received: by 2002:a17:906:dc91:b0:9a1:e0b1:e919 with SMTP id cs17-20020a170906dc9100b009a1e0b1e919mr15984065ejc.4.1693221328337; Mon, 28 Aug 2023 04:15:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693221328; cv=none; d=google.com; s=arc-20160816; b=gmQMLkXgIKszQO4p2HcBAyIjQg8JrFJ5WXU9748MdP0QU5Mt8J+mmvsvKSXu55nqXV 7+xfop3yVoRqv0PJol8KXQxHfqgsCN5h7ubwQ5CpHZRVgY0/TsXv3DnqaoAhM6RJH6Vp tfkdNwtlRKPfLuH/+Pb+08ONUzsF4LUP7zV5viNvZacKJEDj7ve+6LD+KCmNqvFMUbfT pk/qz1AY+Qt3eh9kj46dxr2RVqJT2JCd4x+LjtNnjaTFs+YXWUpFW+NgzkY3paLKJghm BB4Sfwv+KCKSaHKKVIovWBBsbiwYTwCBA4NefzzCtElroKQBE1jfW4KZdQagJBSLhYxY OqWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=EpAYUAyxWxnw3jUtIWPiJWYZLbp2zAaEuj7yHabOWqc=; fh=nippiNbzLPQ7S1eBnGZiMfLgwKfBaRCGKr0cchuFF3c=; b=aviknDdSZzzV8yjz/6D3HmWxtNIZE9XThLZ/zfsMGi3ZETpGg+5XKu/SpfnFe2245h v3C7od9eZi8kEuxl7YU28NOgS2KpFAqPXyrl9xKz0l99aQfLISyq+C0y1JaCBvqX12nN Q/2HaZ+93UvXYqLQ+2uGk2rnr2ip8AG6JTCF5bZNa92rTrbVYZZhGISHULcZKXcTEqAn KRpTOf1l7l4tA738cdpDInmtB0nPm2TSMtoxB/XVfRqmVmIu9ASD04W7/VAUC/dvOmJs ESCmPohyq/0U7RdComu+MQyPafD3VE/593dJD8ra7nUYxLP7coVRPphPkqg7i0eR+7cf BqyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="XzTJ/q1s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rn15-20020a170906d92f00b00992e520a593si4130417ejb.1004.2023.08.28.04.14.52; Mon, 28 Aug 2023 04:15:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="XzTJ/q1s"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232159AbjH1KuP (ORCPT + 99 others); Mon, 28 Aug 2023 06:50:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232132AbjH1Kto (ORCPT ); Mon, 28 Aug 2023 06:49:44 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0DBE1AE for ; Mon, 28 Aug 2023 03:49:19 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-50bf847b267so2372074a12.3 for ; Mon, 28 Aug 2023 03:49:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1693219754; x=1693824554; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EpAYUAyxWxnw3jUtIWPiJWYZLbp2zAaEuj7yHabOWqc=; b=XzTJ/q1sd8jr6ASWsMnGU20/8iQtEMTzUJFSZU++Q4JKU51YXFSxOeNCJEVl/khJm6 AtD492RaXxEUPcNkxt4gJxVCji38QYzs+hnYmZEZdVYlluWZN+RSttUG1W0ZLBwu7Pwg R8X9uAen9oQqaO7cnNtgqh2kVCgvNPhvAxTzvmjB7zRya6BssAHCx1P8gc78gVETHMbs i1SqFq6Iea/ZQYTnF3XT2IsCjCmAg2IcFnuygH3AK73NAXjFLv+PrIIa/z+xEeQJOioF wdZbT+RNvUbtmL5tXRzrJFwxji5i1BGa+xVkTXRjXoxHihu++OWQ8oE8BuJQP3Fdfs/z PcPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693219754; x=1693824554; 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=EpAYUAyxWxnw3jUtIWPiJWYZLbp2zAaEuj7yHabOWqc=; b=kpHFz8XVlwVVrPIogrHjrIqVSXIKEzCUrD1pvOaxQTuaCku9azYumVI4KjQ1Av64t5 xbJgMZzTYU+hCLtXl8ROU2zAh2nqetxpcrbEZhiWaVAZGpU727ZVzPZenUN8lLF0MnlE 0dzIHLNoOOztvoBVLbAKMgnSa6Uzpfjp7l0t3WxT3GGEEMXvXxXC+L1HErO4nlHfqwsZ zoMonQYZx41I+T69/b4+z+598AYpZ+Q5RykNVRugezqbMBSgORfAP4CRePUDW4aXrz9x 1cMysf7rG1wSX1o3ghd8DSXK74QWnNhwvWQgYWDi/exJRo4eSgS8YWuspO47t5zLKJCX MzNA== X-Gm-Message-State: AOJu0YxuJTunz8Dff+vuXbPVzLHlHBIWRub7wkRCcMdENp7lXu1s2Zn9 E7bZOAhIApCinCs+LjD71M3PRndINV7JG6s= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a50:d696:0:b0:522:1d2e:e553 with SMTP id r22-20020a50d696000000b005221d2ee553mr432512edi.8.1693219754366; Mon, 28 Aug 2023 03:49:14 -0700 (PDT) Date: Mon, 28 Aug 2023 10:48:05 +0000 In-Reply-To: <20230828104807.1581592-1-aliceryhl@google.com> Mime-Version: 1.0 References: <20230828104807.1581592-1-aliceryhl@google.com> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog Message-ID: <20230828104807.1581592-6-aliceryhl@google.com> Subject: [PATCH v4 5/7] 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- v3 -> v4: * No changes. 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 251541f1cd68..c1334c48b96b 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -28,8 +28,10 @@ //! //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h) -use crate::{bindings, prelude::*, sync::LockClassKey, types::Opaque}; +use crate::{bindings, prelude::*, sync::Arc, sync::LockClassKey, types::Opaque}; +use alloc::boxed::Box; use core::marker::PhantomData; +use core::pin::Pin; /// Creates a [`Work`] initialiser with the given name and a newly-created lock class. #[macro_export] @@ -363,6 +365,99 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_typ )*}; } +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` stored in a `Work`. + let ptr = ptr as *mut Work; + // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`. + let ptr = unsafe { T::work_container_of(ptr) }; + // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership. + let arc = unsafe { Arc::from_raw(ptr) }; + + T::run(arc) + } +} + +unsafe impl RawWorkItem for Arc +where + T: WorkItem, + T: HasWork, +{ + type EnqueueOutput = 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 pointer is a raw pointer. + let ptr = Arc::into_raw(self).cast_mut(); + + // SAFETY: Pointers into an `Arc` point at a valid value. + let work_ptr = unsafe { T::raw_get_work(ptr) }; + // SAFETY: `raw_get_work` returns a pointer to a valid value. + let work_ptr = unsafe { Work::raw_get(work_ptr) }; + + if queue_work_on(work_ptr) { + Ok(()) + } else { + // SAFETY: The work queue has not taken ownership of the pointer. + 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` stored in a `Work`. + let ptr = ptr as *mut Work; + // SAFETY: This computes the pointer that `__enqueue` got from `Arc::into_raw`. + let ptr = unsafe { T::work_container_of(ptr) }; + // SAFETY: This pointer comes from `Arc::into_raw` and we've been given back ownership. + let boxed = unsafe { Box::from_raw(ptr) }; + // SAFETY: The box was already pinned when it was enqueued. + let pinned = unsafe { Pin::new_unchecked(boxed) }; + + T::run(pinned) + } +} + +unsafe impl RawWorkItem for Pin> +where + T: WorkItem, + T: HasWork, +{ + type EnqueueOutput = (); + + 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 = unsafe { Pin::into_inner_unchecked(self) }; + let ptr = Box::into_raw(boxed); + + // SAFETY: Pointers into a `Box` point at a valid value. + let work_ptr = unsafe { T::raw_get_work(ptr) }; + // SAFETY: `raw_get_work` returns a pointer to a valid value. + let work_ptr = 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 multi-threaded. There are -- 2.42.0.rc1.204.g551eb34607-goog