Skip to content

rust: pin-init: create self references safely

Gary Guo lkml rust-for-linux 2 versions Applied

Status from this message: Applied to pin-init-next. Thanks all!

Get series with b4
b4 am [email protected] change-id 20261006-dev-selfref-27c37abfa849

Versions newest first

  1. v2 Latest20 patches 2 repliesApplied
  2. v120 patches 2 repliesSuperseded

Changes in v2 from the cover letter

- Add where bounds to covariance check (Sashiko)
- Use NotVisible mechanism for with_project_ref instead of filtering (Sashiko)
- Drop pub from SelfRefSlot (Sashiko)
- Fixed some stale comments (Sashiko)
- Picked up Benno's Ack.
- Link to v1: https://patch.msgid.link/[email protected]

Patches in v2 Applied

#Subject and filesSizeRepliesApplied
1/20kbuild: rust: allow `clippy::comparison_chain` globallyMakefile+2 -01 reply
2/20rust: pin-init: internal: pin_data: infer self-referential structrust/pin-init/internal/src/pin_data.rs rust/pin-init/internal/src/util.rs+253 -30 replies
3/20rust: pin-init: internal: pin_data: rewrite fields that borrow othersrust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+93 -00 replies
4/20rust: pin-init: internal: pin_data: pin borrowed fields with wrapperrust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+38 -00 replies
5/20rust: pin-init: internal: pin_data: teach drop check about generics that cannot danglerust/pin-init/src/__internal.rs+29 -00 replies
6/20rust: pin-init: internal: pin_data: self-referential drop order checksrust/pin-init/internal/src/pin_data.rs rust/pin-init/internal/src/util.rs+268 -90 replies
7/20rust: pin-init: internal: pin_data: check covariance of self-referential fieldsrust/pin-init/internal/src/pin_data.rs rust/pin-init/internal/src/util.rs+96 -30 replies
8/20rust: pin-init: internal: pin_data: implement initialization of borrowed structsrust/pin-init/internal/src/init.rs rust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs +1 more+305 -210 replies
9/20rust: pin-init: internal: pin_data: project self-referential fieldsrust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+107 -140 replies
10/20rust: pin-init: internal: pin_data: add `with_project` methodrust/pin-init/internal/src/pin_data.rs+136 -50 replies
11/20rust: pin-init: internal: pin_data: enable self-referential supportrust/pin-init/examples/selfref.rs rust/pin-init/internal/src/pin_data.rs+35 -70 replies
12/20rust: pin-init: internal: pin_data: allow lifetime to be shortened per field drop orderrust/pin-init/internal/src/pin_data.rs+40 -420 replies
13/20rust: pin-init: internal: pin_data: parse explicit `#[borrowed]` annotationrust/pin-init/internal/src/pin_data.rs+32 -40 replies
14/20rust: pin-init: internal: pin_data: support mutable borrowsrust/pin-init/examples/selfref.rs rust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+149 -240 replies
15/20rust: pin-init: internal: pin_data: parse explicit `#[uses]` annotationrust/pin-init/internal/src/pin_data.rs+95 -40 replies
16/20rust: pin-init: internal: pin_data: make field lifetime invariance imply type invariancerust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+174 -90 replies
17/20rust: pin-init: internal: pin_data: complete invariant borrow supportrust/pin-init/examples/selfref.rs rust/pin-init/internal/src/pin_data.rs+30 -10 replies
18/20rust: pin-init: internal: pin_data: perform AST lifetime replacement if possiblerust/pin-init/internal/src/util.rs+102 -100 replies
19/20rust: pin-init: internal: pin_data: support shared projectionrust/pin-init/internal/src/pin_data.rs+134 -00 replies
20/20rust: pin-init: internal: pin_data: support existential lifetimesrust/pin-init/internal/src/pin_data.rs rust/pin-init/src/__internal.rs+252 -160 replies

Reviews trailers per patch

11 of 20 patches have a Reviewed-by or Acked-by.

PatchBenno LossinMiguel Ojeda
1/20A-b A-b
2/20A-b
3/20
4/20A-b
5/20A-b
6/20
7/20
8/20A-b
9/20
10/20
11/20A-b
12/20A-b
13/20A-b
14/20
15/20A-b
16/20A-b
17/20
18/20A-b
19/20
20/20

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 00/20] rust: pin-init: create self references safely

This is a big series that add support for one field to reference a sibling field in a safe and ergnonomic way. Unlike many other crates in the userspace Rust ecosystem, no additional allocation is required, thus it requires the struct to be pinned, which is exactly what pin-init provides.

This is a very powerful feature, and thus can raise concerns about whether it is sound. I spent a lot of time studying the rules to make this sound, and presented durirng Kangrejos [1].

The simple use case looks like this:

    #[pin_data]
    struct MyDriver<'bound> {
        dev: &'bound Device,
        #[pin]
        irq: irq::Registration<'bound, MyIrqHandler<'bound, 'bar>>,
        bar: Bar<'bound, BAR_SIZE>,
    }

You simply need to mention a lifetime that shares the name with the field. There are more advanced use cases which require annotations like

    #[uses('bar: invariant)]

to explicitly declare that the lifetime is used in invariant manner, and also

    where
        exists<'dev>: 'a

to create new existential lifetimes as a way to erase invariant lifetimes that would otherwise bubble up to users. More info can be seen in my Plumbers slide [2]. Note that the syntax for explicit variance annotation has changed following discussions during Plumbers.

The initial plan was to upstream the simple use case only and iron out the advanced features subsequently; however it turns out that DRM jobqueue would need the variance annotation feature, and Nova Cmdq would need to use the existential lifetime feature.

During Plumbers the upstream schedule is discussed, and instead it was agreed that all the features should be upstreamed at once, but the usage of the advanced feature usage limited to the pre-agreed users only to limit the blast radius in case the feature needs to be reworked.

Detailed documentation about the pin-init self-reference feature would be added the following cycle, when the usage of them become more clear.

The pull request on GitHub [3] has a bunch of test suites, which are not synchronized to kernel tree.

---
Changes in v2:
- Add where bounds to covariance check (Sashiko)
- Use NotVisible mechanism for with_project_ref instead of filtering (Sashiko)
- Drop pub from SelfRefSlot (Sashiko)
- Fixed some stale comments (Sashiko)
- Picked up Benno's Ack.
- Link to v1: https://patch.msgid.link/[email protected]

---
Gary Guo (20):
kbuild: rust: allow clippy::comparison_chain globally
rust: pin-init: internal: pin_data: infer self-referential struct
rust: pin-init: internal: pin_data: rewrite fields that borrow others
rust: pin-init: internal: pin_data: pin borrowed fields with wrapper
rust: pin-init: internal: pin_data: teach drop check about generics that cannot dangle
rust: pin-init: internal: pin_data: self-referential drop order checks
rust: pin-init: internal: pin_data: check covariance of self-referential fields
rust: pin-init: internal: pin_data: implement initialization of borrowed structs
rust: pin-init: internal: pin_data: project self-referential fields
rust: pin-init: internal: pin_data: add with_project method
rust: pin-init: internal: pin_data: enable self-referential support
rust: pin-init: internal: pin_data: allow lifetime to be shortened per field drop order
rust: pin-init: internal: pin_data: parse explicit #[borrowed] annotation
rust: pin-init: internal: pin_data: support mutable borrows
rust: pin-init: internal: pin_data: parse explicit #[uses] annotation
rust: pin-init: internal: pin_data: make field lifetime invariance imply type invariance
rust: pin-init: internal: pin_data: complete invariant borrow support
rust: pin-init: internal: pin_data: perform AST lifetime replacement if possible
rust: pin-init: internal: pin_data: support shared projection
rust: pin-init: internal: pin_data: support existential lifetimes

 Makefile                               |    2 +
 rust/pin-init/examples/selfref.rs      |   60 ++
 rust/pin-init/internal/src/init.rs     |   42 +-
 rust/pin-init/internal/src/pin_data.rs | 1563 +++++++++++++++++++++++++++++++-
 rust/pin-init/internal/src/util.rs     |  269 +++++-
 rust/pin-init/src/__internal.rs        |  357 +++++++-
 rust/pin-init/src/lib.rs               |    1 +
 7 files changed, 2246 insertions(+), 48 deletions(-)

---
base-commit: 0d9aa4b994379c6e0099737221ed421deb7e1a31
change-id: 20261006-dev-selfref-27c37abfa849

Best regards,

Signature · 1 line
Gary Guo <[email protected]>