From mboxrd@z Thu Jan 1 00:00:00 1970 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 A5763C7EE23 for ; Thu, 1 Jun 2023 13:56:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233147AbjFAN4w (ORCPT ); Thu, 1 Jun 2023 09:56:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232130AbjFAN4u (ORCPT ); Thu, 1 Jun 2023 09:56:50 -0400 Received: from mail-ej1-x64a.google.com (mail-ej1-x64a.google.com [IPv6:2a00:1450:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6823C129 for ; Thu, 1 Jun 2023 06:56:49 -0700 (PDT) Received: by mail-ej1-x64a.google.com with SMTP id a640c23a62f3a-96faa650a3fso64714966b.0 for ; Thu, 01 Jun 2023 06:56:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685627808; x=1688219808; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vB1KTXT+BDPTSPss1nKfZFCllt9cnJRUrJkIndsk9bQ=; b=ONUw1Bi/LF6RsZlr45Sb+TKYR/o9wrpaSOn2lrXAM6QYiczLPUguB/OtTWl2nFQb/z QHCJFovGIHfcYE7ZKxME4rHu0QrQ4t0lkAsZaKzOuoP35MgMHVzXbqZ9Js6mYq3jSxzW iQhJQID+sbpuY6rX1Kr1tInXmFVQ8bpfhl7KPdzG/3nj30erauVwt9zGGqKaupzxvtkk zJUHWqC/9bRRmif13gTcB23wiU9s0WW2SS+C2rW0b1KB96pBbzxeXvLAmsDK+Q6B9hpm WC4RIxiwCa89hBCrOaMs11hNEoZ8Mf+uzlLbeKiVPw4rmcJHNzBDHeol1J09Swzd8q5c 6l1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685627808; x=1688219808; 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=vB1KTXT+BDPTSPss1nKfZFCllt9cnJRUrJkIndsk9bQ=; b=IOvGNvcmNgvlvueLw7u3vMzCY/CsqCIj60JW8YW0DmqZ/qf5OqpO1ulomVEpvmZbQc JtTfxCx8JgpeAH5qGoK1u8ESun8EsR1SU2ULL5xpXE1diScQMvuWk42x4MhbOmzK6FNl lvb89tjJQ3yfWltCyWKhH82k3EhZz+7dyDgJQXEjm338OAP5y0zo/+vxRIHouyzhr2vR eFbbw3gnz7c02zQphOXnVMnaYlwoMROxFsif+9cO1QfZALUvswe+B7mPLLmeMUHNTzXr PpzZWBztY5CsHVCgQfvj+tDcjAgaJaxBJvZeFpA/J1rovXPOKeSDAEbhZIBsEcz3x0ol wP9g== X-Gm-Message-State: AC+VfDwnZV6F9zw/3f3NSdkpFzG7AFZWqrqS80rwp00sCaPOsUMhG6kP F+95Tsi1YfgXl1NjQv7s1bE4LjbR15QeHXFZReX/AQpoHb/E6O/8FhPrIhcHyXQSA76EXPdyJzK 9/ojyzPAxKpI6sfd1fMZuA71w3YzdQkf3DefTI4aqjjArCTqm5dvdwEAaONNR2Ea7fdNRspaylE FJw7jD X-Google-Smtp-Source: ACHHUZ6wvWf7QDEiZpOYRmHr1ZokpvS+ySsWXbKzAtbGtHwYA1ojE0HCynF9ToBu5yxSIkNVCdyZ/ezhgmRDm2s= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a17:907:2cd9:b0:973:867b:399e with SMTP id hg25-20020a1709072cd900b00973867b399emr2256552ejc.4.1685627807883; Thu, 01 Jun 2023 06:56:47 -0700 (PDT) Date: Thu, 1 Jun 2023 13:49:45 +0000 In-Reply-To: <20230601134946.3887870-1-aliceryhl@google.com> Mime-Version: 1.0 References: <20230601134946.3887870-1-aliceryhl@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230601134946.3887870-8-aliceryhl@google.com> Subject: [PATCH v2 7/8] rust: workqueue: add `try_spawn` helper method From: Alice Ryhl To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Wedson Almeida Filho , Tejun Heo , Lai Jiangshan , 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: rust-for-linux@vger.kernel.org This adds a convenience method that lets you spawn a closure for execution on a workqueue. This will be the most convenient way to use workqueues, but it is fallible because it needs to allocate memory. Co-developed-by: Gary Guo Signed-off-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/kernel/workqueue.rs | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs index f06a2f036d8b..c302e8b8624b 100644 --- a/rust/kernel/workqueue.rs +++ b/rust/kernel/workqueue.rs @@ -29,6 +29,7 @@ //! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h) use crate::{bindings, prelude::*, sync::Arc, types::Opaque}; +use alloc::alloc::AllocError; use alloc::boxed::Box; use core::marker::{PhantomData, PhantomPinned}; use core::pin::Pin; @@ -87,6 +88,44 @@ pub fn enqueue(&self, w: W) -> W::EnqueueOutput }) } } + + /// Tries to spawn the given function or closure as a work item. + /// + /// This method can fail because it allocates memory to store the work item. + pub fn try_spawn(&self, func: T) -> Result<(), AllocError> { + let init = pin_init!(ClosureWork { + work <- Work::new(), + func: Some(func), + }); + + self.enqueue(Box::pin_init(init).map_err(|_| AllocError)?); + Ok(()) + } +} + +/// A helper type used in `try_spawn`. +#[pin_data] +struct ClosureWork { + #[pin] + work: Work>, + func: Option, +} + +impl ClosureWork { + fn project(self: Pin<&mut Self>) -> &mut Option { + // SAFETY: The `func` field is not structurally pinned. + unsafe { &mut self.get_unchecked_mut().func } + } +} + +impl WorkItem for ClosureWork { + type Pointer = Pin>; + + fn run(mut this: Pin>) { + if let Some(func) = this.as_mut().project().take() { + (func)() + } + } } /// A raw work item. @@ -325,6 +364,10 @@ unsafe fn raw_get_work(ptr: *mut Self) -> *mut $crate::workqueue::Work<$work_typ )*}; } +impl_has_work! { + impl HasWork for ClosureWork { self.work } +} + unsafe impl WorkItemPointer for Arc where T: WorkItem, -- 2.41.0.rc0.172.g3f132b7071-goog