From: "Benno Lossin" <lossin@kernel.org>
To: "Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nicolas Schier" <nsc@kernel.org>
Cc: <rust-for-linux@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-kbuild@vger.kernel.org>
Subject: Re: [PATCH v4 2/3] rust: ptr: add projection infrastructure
Date: Mon, 02 Mar 2026 20:02:07 +0100 [thread overview]
Message-ID: <DGSJ22HTG3LE.3GD00J7KJHBPV@kernel.org> (raw)
In-Reply-To: <20260302164239.284084-3-gary@kernel.org>
On Mon Mar 2, 2026 at 5:42 PM CET, Gary Guo wrote:
> From: Gary Guo <gary@garyguo.net>
>
> 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>
I have a naming concern with `ProjectIndex::get`, but that's only used
from the module & macro and unlikely to be used from the outside. So
renaming later should be easy.
Reviewed-by: Benno Lossin <lossin@kernel.org>
Great work :)
Also found a typo below.
> ---
> rust/kernel/lib.rs | 3 +
> rust/kernel/ptr.rs | 3 +
> rust/kernel/ptr/projection.rs | 294 ++++++++++++++++++++++++++++++++++
> scripts/Makefile.build | 4 +-
> 4 files changed, 303 insertions(+), 1 deletion(-)
> create mode 100644 rust/kernel/ptr/projection.rs
> +/// A helper trait to perform field projection.
> +///
> +/// This trait has a `DEREF` generic parameter so it can be implemented twice for types that
> +/// implement `Deref`. This will cause an ambiguity error and thus block `Deref` types being used
> +/// as base of projection, as they can inject unsoundness. Users therefore must not specify `DEREF`
> +/// and should always leave it to be inferred.
> +///
> +/// # Safety
> +///
> +/// `proj` may only invoke `f` with a valid allocation, as documentation described.
s/described/describes/
Cheers,
Benno
> +#[doc(hidden)]
> +pub unsafe trait ProjectField<const DEREF: bool> {
> + /// Project a pointer to a type to a pointer of a field.
> + ///
> + /// `f` may only be invoked with a valid allocation so it can safely obtain raw pointers to
> + /// fields using `&raw mut`.
> + ///
> + /// This is needed because `base` might not point to a valid allocation, while `&raw mut`
> + /// requires pointers to be in bounds of a valid allocation.
> + ///
> + /// # Safety
> + ///
> + /// `f` must return a pointer in bounds of the provided pointer.
> + unsafe fn proj<F>(base: *mut Self, f: impl FnOnce(*mut Self) -> *mut F) -> *mut F;
> +}
next prev parent reply other threads:[~2026-03-02 19:02 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20260302164239.284084-1-gary@kernel.org>
2026-03-02 16:42 ` [PATCH v4 1/3] rust: ptr: add `KnownSize` trait to support DST size info extraction Gary Guo
2026-03-02 18:56 ` Benno Lossin
2026-03-02 18:57 ` Benno Lossin
2026-03-02 19:42 ` Gary Guo
2026-03-02 21:52 ` Benno Lossin
2026-03-02 16:42 ` [PATCH v4 2/3] rust: ptr: add projection infrastructure Gary Guo
2026-03-02 19:02 ` Benno Lossin [this message]
2026-03-03 21:11 ` Miguel Ojeda
2026-03-02 16:42 ` [PATCH v4 3/3] rust: dma: use pointer projection infra for `dma_{read,write}` macro Gary Guo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DGSJ22HTG3LE.3GD00J7KJHBPV@kernel.org \
--to=lossin@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox