Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2312131rwd; Fri, 2 Jun 2023 07:48:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7OGOmJKSaGjehnwtmHZZHstT99LwI2KAiABpOeqvEbEaC96Plgi1IucnyalCmcW81AqnXQ X-Received: by 2002:a17:902:dad0:b0:1b1:94a8:ab2d with SMTP id q16-20020a170902dad000b001b194a8ab2dmr2446062plx.29.1685717307435; Fri, 02 Jun 2023 07:48:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685717307; cv=none; d=google.com; s=arc-20160816; b=K6J9+bTRgHPpfKYhrCEyeJ7I4zju5/xH9kNch6smIwqCzK1S7HGh3AzeNEjLlea1V+ N6e36wBepdkxSH9jr8FVQ6CtWoe+pCJ0OTjYZvweFR9RsuVTh8Ewwk+epJkl9RbA8fio npcu38OcrJn8KGL+hU3ruI+2jBPhKRCzSI1p8HHVVd0OPhKtNu2Wwrm2s6RGBo8l6yKI wJH2lE/8M60NjcE/S47/g5sj4Mc2s9H09rXEQE8Qi1La2n/Tz5wPwEsO3kBG4Gwo3z7s n+V4w7jCXkVzXCrR+0O2oosw2jKFTUQxXbqHyfzGsvg440z3GjhuIrKOhjiMA85NDCYZ eetg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:dkim-signature; bh=dd7s3Z3wE1FRuEKyebQLFIwrLmyQsduGsJm+S7BiDBA=; b=eLnzUKVMcNioZ+f3PGMmnB6Cci66pLHD7rFULO5LPyC9OuAprTnMf7amzZHPxAPm77 JGhKImUkxSL0XR28kfHe34xzeq3DNs7EetFk2E/WLNHDgYjg8GldyOpd9IHU2fTarSn1 WU1QZggYmSGXfAh+mMmw+tEOtl0dDNhjhWhWs3f4yHRtMJXiwDOt6pf0YtD7mCwPSXrn +9dX5fC2vTtELs6p2Rf68vzXpMuA/AsWilViuMH4GvSl/veTZYXCbfhH2lGkpdip//im 7cyWkXbPC4RXbptE5kSlH5Kzu22vXJgwn8wH33HUQ0unBIsFCnl9LLUbOHCzkxfIRXYq aD5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=WbTcJBu7; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a170903010c00b001b024ab08fbsi1016891plc.59.2023.06.02.07.48.12; Fri, 02 Jun 2023 07:48:27 -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=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=WbTcJBu7; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235502AbjFBOkl (ORCPT + 99 others); Fri, 2 Jun 2023 10:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235707AbjFBOkk (ORCPT ); Fri, 2 Jun 2023 10:40:40 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39211BC for ; Fri, 2 Jun 2023 07:40:34 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3f60e536250so25691215e9.1 for ; Fri, 02 Jun 2023 07:40:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1685716832; x=1688308832; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=dd7s3Z3wE1FRuEKyebQLFIwrLmyQsduGsJm+S7BiDBA=; b=WbTcJBu7dkxQd6Xx/FdhcI+/W6kFAf+fOjnkbtuj7zwZ2Zaiz7RATDiV0A9TyiPZDX /9UbiI5tERB8SJ0XsVpCPyn757q28Wt9HzY5kMGLyi2rr0xEPHFYkQ7FMwoqqlBKa/rG T0NtUZJu2NarhxXvNXFTUmo8djZ/dWT6NvIZf4tF7hwLFlX038eumQC/oAE7YrY1wxo6 Sx6Ps3Dcnkc6rz096/AvPzCwBqGaN7l0xOGfTCZq8daGKBb6nwVxKC2p/Qbpjb12Lgjs 4x1b5psWDoiDnT41ltq0ZgoO/nNBh3+wZbhOJdeaHv335cmrVv/qUV41Yyvbuh2j9T3c /axw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685716832; x=1688308832; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=dd7s3Z3wE1FRuEKyebQLFIwrLmyQsduGsJm+S7BiDBA=; b=Mp2qKZqq1yvaJ0IALC2SvKCGEu1U++7dyBGhFgsRAYc5wGCjwu/CtjyJ/7kBRtW++W Z1u41+jq+HfyCfvUSO1CWPt6GnRriKLypxbC93i3Za1NaocjFJeetFUUjHW6rwWBPm7+ iMuxmDoE+wz0XVrWYGUpl3FLJ/UvENzk8svMh31k0nmAyk4viYf6ryyyr7Nve9lAVyVO +8ZPs/tAfsH5azLd9RarLRMT8DdIdHi8bHJXNByqzuKgXpa/HZdzeUkEsoixKOlM2o8q 3J3BLD8Tz5mZNxEeS1t+oAZE4QX8PJdJpfr25L4T+tnoCDIL0R2yvHsXlPwEbczgDEOs Cv5A== X-Gm-Message-State: AC+VfDykBtjthclqx6czmKbENYq0HT3xkvwHyUo8nsTANgmK4c/zPfFO nEjOW/IOHLDYWwVZ96T3VQU7ng== X-Received: by 2002:adf:f606:0:b0:30a:d944:b765 with SMTP id t6-20020adff606000000b0030ad944b765mr57873wrp.15.1685716832593; Fri, 02 Jun 2023 07:40:32 -0700 (PDT) Received: from localhost ([194.62.217.2]) by smtp.gmail.com with ESMTPSA id k16-20020a056000005000b003079986fd71sm1898268wrx.88.2023.06.02.07.40.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 07:40:32 -0700 (PDT) References: <20230601134946.3887870-1-aliceryhl@google.com> <20230601134946.3887870-2-aliceryhl@google.com> User-agent: mu4e 1.10.3; emacs 28.2.50 From: "Andreas Hindborg (Samsung)" To: Alice Ryhl Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Wedson Almeida Filho , Tejun Heo , Lai Jiangshan , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn?= Roy Baron , Benno Lossin , linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH v2 1/8] rust: workqueue: add low-level workqueue bindings Date: Fri, 02 Jun 2023 16:39:00 +0200 In-reply-to: <20230601134946.3887870-2-aliceryhl@google.com> Message-ID: <87o7ly0vxc.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 Alice Ryhl writes: > Define basic low-level bindings to a kernel workqueue. The API defined > here can only be used unsafely. Later commits will provide safe > wrappers. > > Co-developed-by: Gary Guo > Signed-off-by: Gary Guo > Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg (Samsung) > --- > rust/bindings/bindings_helper.h | 1 + > rust/kernel/lib.rs | 1 + > rust/kernel/workqueue.rs | 107 ++++++++++++++++++++++++++++++++ > 3 files changed, 109 insertions(+) > create mode 100644 rust/kernel/workqueue.rs > > diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h > index 50e7a76d5455..ae2e8f018268 100644 > --- a/rust/bindings/bindings_helper.h > +++ b/rust/bindings/bindings_helper.h > @@ -10,6 +10,7 @@ > #include > #include > #include > +#include > > /* `bindgen` gets confused at certain things. */ > const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL; > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs > index 85b261209977..eaded02ffb01 100644 > --- a/rust/kernel/lib.rs > +++ b/rust/kernel/lib.rs > @@ -43,6 +43,7 @@ > pub mod sync; > pub mod task; > pub mod types; > +pub mod workqueue; > > #[doc(hidden)] > pub use bindings; > diff --git a/rust/kernel/workqueue.rs b/rust/kernel/workqueue.rs > new file mode 100644 > index 000000000000..9c630840039b > --- /dev/null > +++ b/rust/kernel/workqueue.rs > @@ -0,0 +1,107 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Work queues. > +//! > +//! C header: [`include/linux/workqueue.h`](../../../../include/linux/workqueue.h) > + > +use crate::{bindings, types::Opaque}; > + > +/// A kernel work queue. > +/// > +/// Wraps the kernel's C `struct workqueue_struct`. > +/// > +/// It allows work items to be queued to run on thread pools managed by the kernel. Several are > +/// always available, for example, `system`, `system_highpri`, `system_long`, etc. > +#[repr(transparent)] > +pub struct Queue(Opaque); > + > +// SAFETY: Kernel workqueues are usable from any thread. > +unsafe impl Send for Queue {} > +unsafe impl Sync for Queue {} > + > +impl Queue { > + /// Use the provided `struct workqueue_struct` with Rust. > + /// > + /// # Safety > + /// > + /// The caller must ensure that the provided raw pointer is not dangling, that it points at a > + /// valid workqueue, and that it remains valid until the end of 'a. > + pub unsafe fn from_raw<'a>(ptr: *const bindings::workqueue_struct) -> &'a Queue { > + // SAFETY: The `Queue` type is `#[repr(transparent)]`, so the pointer cast is valid. The > + // caller promises that the pointer is not dangling. > + unsafe { &*(ptr as *const Queue) } > + } > + > + /// Enqueues a work item. > + /// > + /// This may fail if the work item is already enqueued in a workqueue. > + /// > + /// The work item will be submitted using `WORK_CPU_UNBOUND`. > + pub fn enqueue(&self, w: W) -> W::EnqueueOutput > + where > + W: RawWorkItem + Send + 'static, > + { > + let queue_ptr = self.0.get(); > + > + // SAFETY: We only return `false` if the `work_struct` is already in a workqueue. The other > + // `__enqueue` requirements are not relevant since `W` is `Send` and static. > + // > + // The call to `bindings::queue_work_on` will dereference the provided raw pointer, which > + // is ok because `__enqueue` guarantees that the pointer is valid for the duration of this > + // closure. > + // > + // Furthermore, if the C workqueue code accesses the pointer after this call to > + // `__enqueue`, then the work item was successfully enqueued, and `bindings::queue_work_on` > + // will have returned true. In this case, `__enqueue` promises that the raw pointer will > + // stay valid until we call the function pointer in the `work_struct`, so the access is ok. > + unsafe { > + w.__enqueue(move |work_ptr| { > + bindings::queue_work_on(bindings::WORK_CPU_UNBOUND as _, queue_ptr, work_ptr) > + }) > + } > + } > +} > + > +/// A raw work item. > +/// > +/// This is the low-level trait that is designed for being as general as possible. > +/// > +/// The `ID` parameter to this trait exists so that a single type can provide multiple > +/// implementations of this trait. For example, if a struct has multiple `work_struct` fields, then > +/// you will implement this trait once for each field, using a different id for each field. The > +/// actual value of the id is not important as long as you use different ids for different fields > +/// of the same struct. (Fields of different structs need not use different ids.) > +/// > +/// Note that the id is used only to select the right method to call during compilation. It wont be > +/// part of the final executable. > +/// > +/// # Safety > +/// > +/// Implementers must ensure that any pointers passed to a `queue_work_on` closure by `__enqueue` > +/// remain valid for the duration specified in the documentation for `__enqueue`. > +pub unsafe trait RawWorkItem { > + /// The return type of [`Queue::enqueue`]. > + type EnqueueOutput; > + > + /// Enqueues this work item on a queue using the provided `queue_work_on` method. > + /// > + /// # Guarantees > + /// > + /// If this method calls the provided closure, then the raw pointer is guaranteed to point at a > + /// valid `work_struct` for the duration of the call to the closure. If the closure returns > + /// true, then it is further guaranteed that the pointer remains valid until someone calls the > + /// function pointer stored in the `work_struct`. > + /// > + /// # Safety > + /// > + /// The provided closure may only return `false` if the `work_struct` is already in a workqueue. > + /// > + /// If the work item type is annotated with any lifetimes, then you must not call the function > + /// pointer after any such lifetime expires. (Never calling the function pointer is okay.) > + /// > + /// If the work item type is not [`Send`], then the function pointer must be called on the same > + /// thread as the call to `__enqueue`. > + unsafe fn __enqueue(self, queue_work_on: F) -> Self::EnqueueOutput > + where > + F: FnOnce(*mut bindings::work_struct) -> bool; > +}