]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
rust: ptr: add projection infrastructure
authorGary Guo <gary@garyguo.net>
Mon, 2 Mar 2026 16:42:35 +0000 (16:42 +0000)
committerDanilo Krummrich <dakr@kernel.org>
Sat, 7 Mar 2026 22:06:17 +0000 (23:06 +0100)
commitf41941aab3acd33f13d65a2ae496329bc8ae4de0
tree01f538d12fd1a4837f85df81b434a75fef6184d4
parent08da98f18f4f99aa16838397b76086d2d1d364b3
rust: ptr: add projection infrastructure

Add a generic infrastructure for performing field and index projections on
raw pointers. This will form the basis of performing I/O projections.

Pointers manipulations are intentionally using the safe wrapping variants
instead of the unsafe variants, as the latter requires pointers to be
inside an allocation which is not necessarily true for I/O pointers.

This projection macro protects against rogue `Deref` implementation, which
can causes the projected pointer to be outside the bounds of starting
pointer. This is extremely unlikely and Rust has a lint to catch this, but
is unsoundness regardless. The protection works by inducing type inference
ambiguity when `Deref` is implemented.

This projection macro also stops projecting into unaligned fields (i.e.
fields of `#[repr(packed)]` structs), as misaligned pointers require
special handling. This is implemented by attempting to create reference to
projected field inside a `if false` block. Despite being unreachable, Rust
still checks that they're not unaligned fields.

The projection macro supports both fallible and infallible index
projections. These are described in detail inside the documentation.

Signed-off-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Benno Lossin <lossin@kernel.org>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://patch.msgid.link/20260302164239.284084-3-gary@kernel.org
[ * Add intro-doc links where possible,
  * Fix typos and slightly improve wording, e.g. "as documentation
    describes" -> "as the documentation of [`Self::proj`] describes",
  * Add an empty line between regular and safety comments, before
    examples, and between logically independent comments,
  * Capitalize various safety comments.

    - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/lib.rs
rust/kernel/ptr.rs
rust/kernel/ptr/projection.rs [new file with mode: 0644]
scripts/Makefile.build