Message ID | 20230408122429.1103522-1-y86-dev@protonmail.com |
---|---|
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp850504vqo; Sat, 8 Apr 2023 05:41:58 -0700 (PDT) X-Google-Smtp-Source: AKy350bas/nzyYNAoHJAPiTOFTxmIdoINzwWEutskOP93zMvjon42s7RDS0I7R+FkSCwEa99P4zn X-Received: by 2002:a17:906:eb8b:b0:886:7e24:82eb with SMTP id mh11-20020a170906eb8b00b008867e2482ebmr2354730ejb.21.1680957717804; Sat, 08 Apr 2023 05:41:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680957717; cv=none; d=google.com; s=arc-20160816; b=D85inT42fQ5db+/3lwfiuDxum7T9ddvb4wHPjjmFg2oGQZGlozqpCCtwmNIfsMTAO8 dnQG1pR+gwM497PL5HgT2vDC+xYkImFdPgq++2vDOZtJuLJ+fOv0F3PZ5Dvuq0va3EPQ CjEEHvzAoiqMA4lRXgcakCUthLFANjlx/XsqNEzcQFNFN8ZT8b3mpmrksIM/tp8KNkwv 0KhgovxAlST8+EE/6bV+7MXlnOtkuEM0h9ui99aNfOP2JwV9ZOp9M3cg1dXiPK6+xqu2 DzqNcs67dY/2wQoz8GHaQ7+Bq5Rvqgwq2my0shc+QLWwXAc5ODPZO3Wi3ODbBMhNflCZ Jkww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:message-id:subject:cc:from:to:dkim-signature:date; bh=tzdDTbN3tpX2XmJibizQwZqpEThygt4DIudc3sder8U=; b=M1En5veo1LPfqovIdI8S6mYScAG/J3/Ej+MNr1eM56c6sn8DYu297J3IYJ6N6XAcZ8 zscmpAfij0czQ8qEkrddKHnU6c/h6AY8K4PgOchRT2j9ybOdk4gk6z06BBkqQUPG4M0/ UGES/EgYBZXVPfC7e9OVT1ZJJhWBIoXjrvDm5mke78ywM/nAMp8kwA1WiJ/A8nJm+vQK 3JzWpbev4P18GbNgPp6fOt3JR0S/ijaAuAapNXX7xoceM5tUseDhYsMp5vnT/WLop+Gf fDmMP4hnlAiM8OcZXD7+GuP3SltHt+bX+3sN/WDYad+4lkh8MFH2EAxPEUC2wm0nJwpE Tk+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=UNleLHVD; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f15-20020a1709062c4f00b00930a6858cd8si5722820ejh.454.2023.04.08.05.41.33; Sat, 08 Apr 2023 05:41:57 -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=@protonmail.com header.s=protonmail3 header.b=UNleLHVD; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229559AbjDHMZa (ORCPT <rfc822;a1648639935@gmail.com> + 99 others); Sat, 8 Apr 2023 08:25:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbjDHMZ2 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 8 Apr 2023 08:25:28 -0400 Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 378A786AE for <linux-kernel@vger.kernel.org>; Sat, 8 Apr 2023 05:25:27 -0700 (PDT) Date: Sat, 08 Apr 2023 12:25:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680956724; x=1681215924; bh=tzdDTbN3tpX2XmJibizQwZqpEThygt4DIudc3sder8U=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=UNleLHVDe+5KrPJOOLbmSQlS8FwVq0ol2nFWA24RtBVuD3idVU5FyBpWmLA1g4pUj bUoQeRfK2M6tmzPlFx2hJyWm8U86ZkIDQlrx7LuSvl/ToW9Iw1S83phvN9T/LD4zXF JW3bkOj0GKf3elnPcI7iufdxCDDDyR8S8ZkBCocXIAk6lKC4GyA7r8roMJ+WIOBZW+ yHqqWu1XZfczhjvH96RbQSbubRQLsB4LpfRY7KwYPvC91WMqCZwU/9r+fL3RlhJb2U WJc8yT5XLx9dHZQmK3ii3rgCB8oNzuVJVVHV+YhH1P4Je2kzPi9O7eArCwTFejvPf6 54pr1NhcLixrw== To: Miguel Ojeda <ojeda@kernel.org>, Alex Gaynor <alex.gaynor@gmail.com>, Wedson Almeida Filho <wedsonaf@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com>, Alice Ryhl <alice@ryhl.io>, Andreas Hindborg <nmi@metaspace.dk> From: Benno Lossin <y86-dev@protonmail.com> Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin <y86-dev@protonmail.com> Subject: [PATCH v7 00/15] Rust pin-init API for pinned initialization of structs Message-ID: <20230408122429.1103522-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762611920024240893?= X-GMAIL-MSGID: =?utf-8?q?1762611920024240893?= |
Series |
Rust pin-init API for pinned initialization of structs
|
|
Message
y86-dev
April 8, 2023, 12:25 p.m. UTC
This is the seventh version of the pin-init API. See [1] for v6. The tree at [2] contains these patches applied on top of 6.3-rc1. The Rust-doc documentation of the pin-init API can be found at [3]. These patches are a long way coming, since I held a presentation on safe pinned initialization at Kangrejos [4]. And my discovery of this problem was almost a year ago [5]. - #1 enables the `pin_macro` feature which is already stable in Rust version 1.68. - #2 adds a utility macro `quote!` for proc-macros. This macro converts the typed characters directly into Rust tokens that are the output of proc-macros. It is used by the pin-init API proc-macros. - #3 changes the `Err` types of the constructor functions of `Arc` and `UniqueArc` to `AllocError`. - #4 adds the `assume_init` function to `UniqueArc<MaybeUninit<T>>` that unsafely assumes the pointee to be initialized and returns a `UniqueArc<T>`. `UniqueArc::write` is modified to use this new function. Later patches use it as well. - #5 adds `Opaque::raw_get` to access the value inside of an `Opaque` from a raw pointer only. - #6-11 introduce the pin-init API. The commit message of #4 details the problem it solves and lays out the overall architecture. The implementation details are fairly complex; however, this is required to provide a safe API for users -- reducing the amount of `unsafe` code is a key goal of the Rust support in the kernel. An example of the before/after difference from the point of view of users is provided in the commit message. It is a goal to at some point have pin-init as a language feature of Rust. A first step in this direction is the Field Projection RFC [6]. - #12 adds important traits and macros from pin-init to the prelude. - #13 adds functions for easier initialization of `Opaque<T>` via FFI and raw pointer initializer functions. This is necessary when writing Rust wrappers and will be used by Wedson's `sync` module patches. - #14 improves the `UniqueArc::try_new_uninit` function by using the pin-init API. The old version first allocated uninitialized memory on the stack and then moved it into the location in the heap. The new version directly allocates this on the heap. - #15 adds functions for initializing already allocated `UniqueArc`s, this will be used by the Android Binder driver. -- Changelog: v6 -> v7: - Changed `DropGuard` and `StackInit` from a tuple struct to a named struct. - Improve `SAFETY` and add `INVARIANT` comments to `StackInit`. - Change `Opaque::ffi_init` to take a closure instead of a function pointer to allow better tracking via klint. v5 -> v6: - Change `pinned_drop` macro to allow `mut self` in the signature. - Change statement fragment to tt fragemnt in `pinned_drop` to prevent parsing errors. - Move evaluation of the value in `stack_pin_init!`/`stack_try_pin_init!` to the beginning. - Move setting uninitialized flag in front of dropping the value in `StackInit::init`. - Remove `Unpin` requirement on `zeroed()`. - Add note about `Pointee` to the `Zeroable` impl on raw pointers. v4 -> v5: - Add `pin_macro` to `rust_allowed_features`. - Improve wording of commit message #6. - Remove `PinInit` as a supertrait from `Init`, instead add a blanket impl: `impl PinInit<T, E> for Init<T, E>`. - Fix `BigBuf` example on `try_init!`. - Fix imports in `arc.rs`. v3 -> v4: - Improve documentation. - Fixing doc-tests imports, comments and full paths in macro examples. - Implement `Zeroable` for many more types. - Fix unsoundness: `Zeroable` allowed to create fat pointers with a null VTABLE pointer. - Split fallible components from `stack_pin_init!` into `stack_try_pin_init!`. - Move `Invariant` type alias and `InitClosure` into `__internal`. - Change the error type of the constructor functions of `Arc` and `UniqueArc` to `AllocError`. - Add `try` variants to `InPlaceInit` to allow custom error types. - Make `StackInit::init` a safe function. - Rename `OnlyCallFromDrop::create` to `OnlyCallFromDrop::new`. - Enable the `pin_macro` feature for use inside of `stack_pin_init!`. - Make `quote!` and `quote_spanned!` use absolute paths. v2 -> v3: - Split the big original commit into six smaller commits. - Use `PinnedDrop` in the `RawFoo` code example. - Move the `init::common::ffi_init` functions to `Opaque::ffi_init`. - Move all `#[doc(hidden)]`, internal types into the new `__internal` module. - Specify safety guarantees and requirements of the initializer macros. - Add a detailed example of what the expanded code of the various macros looks like. - Require generics in the initializer macros to use turbofish syntax (`::<>`). - Refactor the internals of the initializer macros, this way they have better type inference and generic arguments can be omitted more often. - Replace `init::from_value` with a blanket impl of the initializer traits for ordinary objects. - Add initializing functions for already allocated `UniqueArc`s. - Add `Opaque::manual_init` functions akin to `ffi_init`, but they take an `extern "Rust" fn` instead. - Documentation and inline comment improvements. v1 -> v2: - Split the common module and `UniqueArc::assume_init` into their own commits. - Change the generics syntax of `pin_init!` to reflect normal struct generic syntax. - Replace `PinnedDrop::__ensure_no_unsafe_op_in_drop` with an only unsafely creatable token. - Hide `StackInit<T>` in the docs, because it is internal API. - Improve macro internals of `pin_init!` according to Gary's review. - Add check for `PhantomPinned` fields without a `#[pin]` attribute in `#[pin_data]`, as those fields will not have the intended effect. - Add docs to `quote.rs`. [1] https://lore.kernel.org/rust-for-linux/20230405193445.745024-1-y86-dev@protonmail.com/ [2] https://github.com/y86-dev/linux.git patch/pinned-init-v7 [3] https://rust-for-linux.github.io/docs/pinned-init/kernel/init [4] https://kangrejos.com [5] https://github.com/Rust-for-Linux/linux/issues/772 [6] https://github.com/rust-lang/rfcs/pull/3318 Benno Lossin (14): rust: enable the `pin_macro` feature rust: sync: change error type of constructor functions rust: sync: add `assume_init` to `UniqueArc` rust: types: add `Opaque::raw_get` rust: add pin-init API core rust: init: add initialization macros rust: init/sync: add `InPlaceInit` trait to pin-initialize smart pointers rust: init: add `PinnedDrop` trait and macros rust: init: add `stack_pin_init!` macro rust: init: add `Zeroable` trait and `init::zeroed` function rust: prelude: add `pin-init` API items to prelude rust: types: add `Opaque::ffi_init` rust: sync: reduce stack usage of `UniqueArc::try_new_uninit` rust: sync: add functions for initializing `UniqueArc<MaybeUninit<T>>` Gary Guo (1): rust: macros: add `quote!` macro rust/kernel/init.rs | 1427 ++++++++++++++++++++++++++++++++ rust/kernel/init/__internal.rs | 235 ++++++ rust/kernel/init/macros.rs | 971 ++++++++++++++++++++++ rust/kernel/lib.rs | 7 + rust/kernel/prelude.rs | 6 +- rust/kernel/sync/arc.rs | 81 +- rust/kernel/types.rs | 28 + rust/macros/lib.rs | 80 ++ rust/macros/pin_data.rs | 79 ++ rust/macros/pinned_drop.rs | 49 ++ rust/macros/quote.rs | 143 ++++ scripts/Makefile.build | 2 +- 12 files changed, 3100 insertions(+), 8 deletions(-) create mode 100644 rust/kernel/init.rs create mode 100644 rust/kernel/init/__internal.rs create mode 100644 rust/kernel/init/macros.rs create mode 100644 rust/macros/pin_data.rs create mode 100644 rust/macros/pinned_drop.rs create mode 100644 rust/macros/quote.rs base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6 -- 2.39.2
Comments
On Sat, Apr 8, 2023 at 2:25 PM Benno Lossin <y86-dev@protonmail.com> wrote: > > This is the seventh version of the pin-init API. See [1] for v6. Applied to `rust-next`. Thanks! The `assume_init` patch was dropped from this one as discussed elsewhere with Lina who sent a similar patch, which required adjusting that patch's `SAFETY` comment. I also applied the `Reviewed-by`s from here to there since it was essentially the same patch. Cheers, Miguel
Hi Miguel, could you please change my current email on the patches from <y86-dev@protonmail.com> to my new email <benno.lossin@proton.me> Please do this for the Signed-off-by tags as well as the actual git-author. Lina's patch to introduce `UniqueArc::assume_init` has my Co-developed-by tag, also change the email there. Much appreciated! -- Cheers, Benno
On Wed, Apr 12, 2023 at 10:13 PM y86-dev <y86-dev@protonmail.com> wrote: > > could you please change my current email on the patches from > <y86-dev@protonmail.com> > to my new email > <benno.lossin@proton.me> > Please do this for the Signed-off-by tags as well as the actual > git-author. Lina's patch to introduce `UniqueArc::assume_init` has > my Co-developed-by tag, also change the email there. > > Much appreciated! Done in `rust-next` -- my pleasure! Cheers, Miguel