The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Benno Lossin" <lossin@kernel.org>
To: "Al Viro" <viro@zeniv.linux.org.uk>, "Alice Ryhl" <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Arnd Bergmann" <arnd@arndb.de>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Al Viro" <viro@ftp.linux.org.uk>
Subject: Re: [PATCH v2] uaccess: rust: use newtype for user pointers
Date: Wed, 28 May 2025 22:35:01 +0200	[thread overview]
Message-ID: <DA82XR7C0NCM.DPN2F8RHAB3Y@kernel.org> (raw)
In-Reply-To: <20250528174546.GC2023217@ZenIV>

On Wed May 28, 2025 at 7:45 PM CEST, Al Viro wrote:
> On Wed, May 28, 2025 at 10:47:12AM +0000, Alice Ryhl wrote:
>
>> We don't currently have any way to perform that kind of pointer-math on
>> user pointers, nor do we have any users of it. I imagine that this type
>> checking is only useful if you can actually perform pointer math in the
>> first place?
>
> What you want is something like
> 	x->field::UserPtr(beta) iff
> 		x::UserPtr(alpha) and
> 		_.field::beta where _::alpha

Not 100% sure I understand your code correctly, do you mean that:
`x->field` is of type `UserPtr(beta)` given that `x` is of type
`UserPtr(alpha)` and `field` is a field of `alpha` of type `beta`?

If that is correct, then I have a proposal called field projection [1]
for the rust language itself to support this kind of operation for any
custom type.

I also have an macro-based implementation [2] and I could cook up some
examples with `UserPtr` [^3].

[1]: https://github.com/rust-lang/rfcs/pull/3735
[2]: https://github.com/Rust-for-Linux/field-projection/tree/new
[^3]: Here is a quick sketch of how the above would look like in Rust
      using [2]:

      #[derive(HasFields)]
      struct Alpha {
          field: Beta,
      }

      impl Alpha {
          fn write_beta<'a>(mut self: UserPtr<'a, Self>, value: Beta) {
              start_proj!(mut self);
              let mut field: UserPtr<'a, Beta> = p!(@mut self->field);
              field.write(value);
          }
      }

      A couple notes for anyone not too familiar with Rust:

      * `'a` is called a lifetime, it says for how long a value is valid
      * the `< ... >` are generic types
      * the `ident: Type` is a type annotation used in parameters (not
        optional) and in local variable bindings (optional)
      * the `self` parameter is special, it allows one to use the
        `val.fun` syntax (it's similar to `this` in java and `self` in
        python, but without the object orientation stuff associated with
        it)

---
Cheers,
Benno

> Generated code would be "add offset and cast to pointer to type of...",
> but doing that manually would really invite headache.

  parent reply	other threads:[~2025-05-28 20:35 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-27 13:53 [PATCH v2] uaccess: rust: use newtype for user pointers Alice Ryhl
2025-05-27 14:42 ` Greg Kroah-Hartman
2025-05-27 15:20 ` Boqun Feng
2025-05-27 15:22   ` Alice Ryhl
2025-05-27 19:03 ` Christian Schrefl
2025-05-27 19:09   ` Alice Ryhl
2025-05-27 22:12 ` Al Viro
2025-05-27 23:13   ` Boqun Feng
2025-05-28 10:48     ` Alice Ryhl
2025-05-28 15:38     ` Benno Lossin
2025-05-28 16:02       ` Boqun Feng
2025-05-28 10:47   ` Alice Ryhl
2025-05-28 17:45     ` Al Viro
2025-05-28 20:01       ` Al Viro
2025-05-28 20:35       ` Benno Lossin [this message]
2025-05-28 10:52 ` Danilo Krummrich
2025-05-28 16:55 ` Benno Lossin

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=DA82XR7C0NCM.DPN2F8RHAB3Y@kernel.org \
    --to=lossin@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=aliceryhl@google.com \
    --cc=arnd@arndb.de \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    --cc=viro@ftp.linux.org.uk \
    --cc=viro@zeniv.linux.org.uk \
    /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