Rust for Linux List
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Alice Ryhl <aliceryhl@google.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nicolas Schier" <nsc@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org,
	"Joshua Liebow-Feeser" <joshlf@google.com>,
	"Jack Wrenn" <jswrenn@google.com>
Subject: Re: [PATCH v2 00/19] `zerocopy` support
Date: Fri, 12 Jun 2026 13:57:47 +0200	[thread overview]
Message-ID: <2026061224-outhouse-widget-8a3a@gregkh> (raw)
In-Reply-To: <aigKVysaNhloK9bS@google.com>

On Tue, Jun 09, 2026 at 12:43:03PM +0000, Alice Ryhl wrote:
> On Tue, Jun 09, 2026 at 12:21:21PM +0000, Alice Ryhl wrote:
> > On Mon, Jun 08, 2026 at 04:14:19PM +0200, Miguel Ojeda wrote:
> > > This patch series introduces support for `zerocopy`:
> > >
> > >     Fast, safe, compile error. Pick two.
> > >
> > >     Zerocopy makes zero-cost memory manipulation effortless. We write
> > >     `unsafe` so you don't have to.
> > 
> > I tried applying this and using it with Binder. I ran into one
> > challenge, which is this uapi struct:
> > 
> > struct binder_transaction_data {
> > 	/* The first two are only used for bcTRANSACTION and brTRANSACTION,
> > 	 * identifying the target and contents of the transaction.
> > 	 */
> > 	union {
> > 		/* target descriptor of command transaction */
> > 		__u32	handle;
> > 		/* target descriptor of return transaction */
> > 		binder_uintptr_t ptr;
> > 	} target;
> > 	binder_uintptr_t	cookie;	/* target object cookie */
> > 	...
> > }
> > 
> > The problem is that when the union contains a handle, there are 4 bytes
> > of padding in the union. Currently Rust Binder handles this by wrapping
> > the uapi struct in MaybeUninit and using MaybeUninit::zeroed() to
> > construct it, ensuring that even if padding is present, it is zeroed.
> > 
> > However, this trick relies on unsafely implementing AsBytes for
> > BinderTransactionData with the safety comment being that the MaybeUninit
> > actually always contains initialized data.
> > 
> > To translate this to zerocopy, I'd have to do this:
> > 
> > unsafe impl zerocopy::IntoBytes for $newname {
> >     fn only_derive_is_allowed_to_implement_this_trait() {}
> > }
> > 
> > One fix could be to update the uapi header by explicitly adding the
> > padding, but that's kind of awkward for a union like this, since I'd
> > have to do it like this with an extra struct:
> > 
> > 	union {
> > 		/* target descriptor of command transaction */
> > 		struct {
> > 			__u32	handle;
> > 			__u32	_pad;
> > 		};
> > 		/* target descriptor of return transaction */
> > 		binder_uintptr_t ptr;
> > 	} target;
> > 
> > It's not clear to me if changing the uapi headers like this is even
> > allowed to begin with. It's a somewhat non-trivial change.
> 
> Hey Greg,
> Do you have any input on this from the C side?
> 
> For context, zerocopy is a tool that helps convert raw bytes into
> structs and vice-versa. When I tried using zerocopy with Rust Binder to
> see if it helps there, I found that zerocopy is flagging that Rust
> Binder copied a uapi struct containing padding into userspace, which is
> of course dangerous since padding on the kernel stack may contain data
> we don't want to leak to userspace.

A UAPI structure should not have padding, and if it does, it must be
zeroed out, as you say.  So in C we normally manually fix this up.

> Now, there's not actually a problem today because I'm using another
> strategy to ensure that the padding is zeroed when copying the output of
> the ioctl to userspace, but ideally I'd like to switch over to using
> zerocopy.
> 
> How is this kind of thing usually handled on the C side? As far as I can
> tell, C Binder handles it by just being extra careful like this:
> 
> 	fp->binder = 0;
> 	fp->handle = rdata.desc;

Yes, we have to almost always manually do this.  Sometimes we use memset
on the structure first before copying, or rely on a foo = {}; to do it
for us (but I never remember if the compiler will zero out the holes...)

> where fp->binder and fp->handle are fields of the same union, with
> fp->binder being 8 bytes and fp->handle being 4 bytes. The first
> assignment ensures that the extra 4 bytes in the union are zeroed.

Yes, that's why we manually do this :(

thanks,

greg k-h

      parent reply	other threads:[~2026-06-12 11:58 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-08 14:14 [PATCH v2 00/19] `zerocopy` support Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 01/19] scripts: generate_rust_analyzer: support passing env vars Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 02/19] rust: kbuild: show the right `quiet_cmd_rustc_procmacrolibrary` Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 03/19] rust: kbuild: remove unused variable Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 04/19] rust: kbuild: define `procmacro-name` function Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 05/19] rust: kbuild: define `procmacro-extension` variable Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 06/19] rust: kbuild: support per-target environment variables Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 07/19] rust: kbuild: support `skip_clippy` for `rustc_procmacro` Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 08/19] rust: zerocopy: import crate Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 09/19] rust: zerocopy: add SPDX License Identifiers Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 10/19] rust: zerocopy: remove float `Display` support Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 11/19] rust: zerocopy: add `README.md` Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 12/19] rust: zerocopy: enable support in kbuild Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 13/19] rust: zerocopy-derive: import crate Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 14/19] rust: zerocopy-derive: add SPDX License Identifiers Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 15/19] rust: zerocopy-derive: avoid generating non-ASCII identifiers Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 16/19] rust: zerocopy-derive: add `README.md` Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 17/19] rust: zerocopy-derive: enable support in kbuild Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 18/19] rust: prelude: add `zerocopy{,_derive}::FromBytes` Miguel Ojeda
2026-06-08 14:14 ` [PATCH v2 19/19] gpu: nova-core: firmware: parse `FalconUCodeDescV2` via `zerocopy` Miguel Ojeda
2026-06-08 14:32   ` Gary Guo
2026-06-08 14:36     ` Danilo Krummrich
2026-06-08 15:30       ` Miguel Ojeda
2026-06-09 10:45 ` [PATCH v2 00/19] `zerocopy` support Miguel Ojeda
2026-06-09 12:21 ` Alice Ryhl
2026-06-09 12:32   ` Gary Guo
2026-06-09 12:43   ` Alice Ryhl
2026-06-09 13:08     ` Miguel Ojeda
2026-06-12 11:58       ` Greg Kroah-Hartman
2026-06-12 11:57     ` Greg Kroah-Hartman [this message]

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=2026061224-outhouse-widget-8a3a@gregkh \
    --to=gregkh@linuxfoundation.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=joshlf@google.com \
    --cc=jswrenn@google.com \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=lossin@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