[PATCH v1 0/3] Rust pin-init API for pinned initialization of structs

From: y86-dev
Date: Sun Mar 12 2023 - 21:23:04 EST


This series adds the pin-init API for initializing pinned structs in-place.
It reduces the need for `unsafe` and streamlines initialization of structs.

The first patch 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.

The second patch introduces the pin-init API. The commit message 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. Ultimately, it is a goal is to at some point have this as a
language feature of Rust. A first step in this direction is the Field
Projection RFC [1].

The third patch improves the function `UniqueArc::try_new_uninit` 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.

These patches provide an important foundation for ergonomic and safe
synchronization primitives and their users. It also enables in-place
initialization of structs, so structs with a bigger size than the stack
size can be created.

These patches are also a long way coming, since I held a presentation on
safe pinned initialization at Kangrejos [2]. And my discovery of this
problem was almost a year ago [3].

The repository at [4] contains these patches applied. The Rust-doc
documentation of the pin-init API can be found at [5].

Link: https://github.com/rust-lang/rfcs/pull/3318 [1]
Link: https://kangrejos.com [2]
Link: https://github.com/Rust-for-Linux/linux/issues/772 [3]
Link: https://github.com/y86-dev/linux.git patch/pinned-init-v1 [4]
Link: https://rust-for-linux.github.io/docs/pinned-init/kernel/init [5]

Benno Lossin (2):
rust: add pin-init API
rust: sync: reduce stack usage of `UniqueArc::try_new_uninit`

Gary Guo (1):
rust: macros: add `quote!` macro

rust/kernel/init.rs | 1396 ++++++++++++++++++++++++++++++++++++
rust/kernel/init/common.rs | 42 ++
rust/kernel/init/macros.rs | 449 ++++++++++++
rust/kernel/lib.rs | 6 +
rust/kernel/prelude.rs | 6 +-
rust/kernel/sync/arc.rs | 48 +-
rust/kernel/types.rs | 8 +
rust/macros/lib.rs | 80 +++
rust/macros/pin_data.rs | 79 ++
rust/macros/pinned_drop.rs | 49 ++
rust/macros/quote.rs | 125 ++++
scripts/Makefile.build | 2 +-
12 files changed, 2286 insertions(+), 4 deletions(-)
create mode 100644 rust/kernel/init.rs
create mode 100644 rust/kernel/init/common.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