Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp113342rwd; Wed, 17 May 2023 15:36:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5mKBYk3WcTku0/FZe8fGlITu2R0qTyUTtlxTfZil14QDyimdpu7gaUOrR51u5sFboDhNf0 X-Received: by 2002:a05:6a00:134d:b0:64a:f69e:9091 with SMTP id k13-20020a056a00134d00b0064af69e9091mr1801002pfu.20.1684362982426; Wed, 17 May 2023 15:36:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684362982; cv=none; d=google.com; s=arc-20160816; b=nR/evjWk/vljoqgiXzd/2XuydpvOMDQuALHylM8BhXsSdX4AFaG+OnSawR27+3rYzP kS15Udv3dhL9y0TOeF2KEWPOSyOQSzjNAgwWPZTC3QHFthZUjzVxwm54QmmOWlR4LJCr hHn8wEvArqc5M97JfZ4ejXhm53APWa1Rwyv04gogqkP4aNBKw18gHcdKQo+FeqmL44oL GCXHO/mzr9Hi9WZqhd79iSTxo8xVEG7rIOWeypk6XRgXc8KcmCtwWdFyYSF4vtY/stEF eYK1aCoGYYVftn+Q7yjGMGsITonjYzMumlFOAj7fGoW684KgT351fsBkVawZOJRi1xfP fyXg== 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=qCQ3NjxZoebSIAtGeHR5p1IA6stp8bUJIlG4AwU4skw=; b=UBAIZbdTDXGLMO7t4L6kHGiw6/ZGAaEsb5z73T7dhGR+SygdH0ij/K7oeNhFzJbe21 PE7FlhK2rCJWlrRg5aKyROCOuEf6qc1vW1QVoUkuuCBdULs1q9iFP6N2qgOHNoqIb7VL 7GgDP2mI9uEzVUX57Ytdir/x3QaGEaYKB/l1S7tyCSp1jhRD44MCXFKTgHMnKZL/Ei+V mnRjrvC+FUOMAA/z05ixPFLo45GHqVKoMIXFDFAMdOHXyYdh1ze9CJxOH4ieN4wd86FK ukuBD56fXEuLIoXV0yXKOpWH/2Lvm2f3LcRg+jkt5TfWF70jF/WLjbU8+j4XgSPuDi9Y wltw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=4vgsoY+5; 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 y192-20020a638ac9000000b00527d158ec73si22734569pgd.3.2023.05.17.15.36.07; Wed, 17 May 2023 15:36:22 -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=4vgsoY+5; 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 S229808AbjEQWW0 (ORCPT + 99 others); Wed, 17 May 2023 18:22:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbjEQWWY (ORCPT ); Wed, 17 May 2023 18:22:24 -0400 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 073554EF8 for ; Wed, 17 May 2023 15:22:23 -0700 (PDT) Received: by mail-ed1-x549.google.com with SMTP id 4fb4d7f45d1cf-50bd7555c6eso1324126a12.1 for ; Wed, 17 May 2023 15:22:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684362141; x=1686954141; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qCQ3NjxZoebSIAtGeHR5p1IA6stp8bUJIlG4AwU4skw=; b=4vgsoY+5jg02OmnaPeEWdmac9MQv5QgavqYYTecATI2iI4NboNWkxSh2nzbpk0VLCQ 1P2DkeGOfUT2Qp1xK7LPXfGpmW0RrVlmiRL59/wUEXcyiBT/N/6NWTeQk+lyuGlbeKoc P0Kt5+RNnAgHuUe8PxnnlnmAoSdBMcdFPEywkmBUKz61dRhfUK1efGsDLggM0K2E3sPw 5egtqGedMvFnjHxzdyzRXcKon5hhGc7ajk4WplTzQvh6KqLqmnPgShi/AU9TqabaWiUE 5TSBzXNr6sg8rs0q4jDdHXHYfA+lQ6I/aq25oRpOlqjYLO5UQJrgUEIrFSNBxA8nmTkN tfzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684362141; x=1686954141; 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=qCQ3NjxZoebSIAtGeHR5p1IA6stp8bUJIlG4AwU4skw=; b=Ee1akn+jc63Z+New8+yHa7bkuUyxCHhP147NrYXCDt57s1Vi9Wr5b8Ts7ay/k2qoZg 7BFSbNPR0Y/yigb+s36jhm0GVNpvhHyp7iEeJ4jAGtTEJEQayWINxC+d70C5n6JPiQpT Xpq5RuDTjchXuT93BpB1CF/+ADjzSkiSNBEe/EDe16lG62xsVGj4hUddQnsxojVe8pu/ 3+SuNAZf7ctePc68fzw3TnAMQPGKx8qrU40GaZQTIic7uq9SQ4i3ckU7b/vEnQLfhH91 +88dBhaoxhQhrqZ1ybibbSKg8kdQz0Cx4yZ8nLP99H4GKdxiA0yZIzWrjB3tCikhUr1+ cKTA== X-Gm-Message-State: AC+VfDx7YWTI3UOi5HVgIAHdOWNOTbteODsK8kDLRQ6k41nzx8E7xa1T HecwTB0B8y7c3wRRiX19rXMbOy02VnLCmQA= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:31:98fb:c0a8:6c8]) (user=aliceryhl job=sendgmr) by 2002:a50:d09d:0:b0:504:8731:4ef2 with SMTP id v29-20020a50d09d000000b0050487314ef2mr1612484edd.7.1684362141496; Wed, 17 May 2023 15:22:21 -0700 (PDT) Date: Wed, 17 May 2023 22:22:19 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Message-ID: <20230517222219.3191560-1-aliceryhl@google.com> Subject: Re: [PATCH v1 0/7] Bindings for the workqueue From: Alice Ryhl To: tj@kernel.org Cc: alex.gaynor@gmail.com, aliceryhl@google.com, benno.lossin@proton.me, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, gary@garyguo.net, jiangshanlai@gmail.com, linux-kernel@vger.kernel.org, ojeda@kernel.org, patches@lists.linux.dev, rust-for-linux@vger.kernel.org, wedsonaf@gmail.com 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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 On Wed, 17 May 2023 11:48:19 -1000, Tejun Heo wrote: > I tried to read the patches but am too dumb to understand much. The patch is more complicated than I would have liked, unfortunately. However, as I mentioned in the cover letter, simplifications should be on their way. Luckily, using the workqueue bindings is simpler than the bindings themselves. > Any chance you can provide some examples so that I can at least > imagine how workqueue would be used from rust side? Yes, of course! The simplest way to use the workqueue is to use the `try_spawn` method introduced by the last patch in the series. With this function, you just pass a function pointer to the `try_spawn` method, and it schedules the function for execution. Unfortunately this allocates memory, making it a fallible operation. To avoid allocation memory, we do something else. As an example, we can look at the Rust binder driver that I am currently working on. Here is how it will be used in the binder driver: First, the `Process` struct will be given a `work_struct` field: #[pin_data] pub(crate) struct Process { // Work node for deferred work item. #[pin] defer_work: Work>, // Other fields follow... } Here, we use the type `Work>` for our field. This type is the Rust wrapper for `work_struct`. The generic parameter to `Work` should be the pointer type used to access `Process`, and in this case it is `Arc`. The pointer type `Arc` is used for reference counting, and its a pointer type that owns a ref-count to the inner value. (So e.g., it decrements the ref-cout when the arc goes out of scope.) Arc is an abbreviation of "atomic reference count". This means that while it is enqueued in the workqueue, the workqueue owns a ref-count to the process. Next, binder will use the `impl_has_work!` macro to declare that it wants to use `defer_work` as its `work_struct` field. That looks like this: kernel::impl_has_work! { impl HasWork> for Process { self.defer_work } } To define the code that should run when the work item is executed on the workqueue, binder does the following: impl workqueue::ArcWorkItem for Process { fn run(self: Arc) { // this runs when the work item is executed } } Finally to schedule it to the system workqueue, it does the following: let _ = workqueue::system().enqueue(process); Here, the `enqueue` call is fallible, since it might fail if the process has already been enqueued to a work queue. However, binder just uses `let _ =` to ignore the failure, since it doesn't need to do anything special in that case. I hope that helps, and let me know if you have any further questions. Alice