From nobody Mon Feb 9 01:16:39 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2B09F30F555 for ; Fri, 26 Dec 2025 21:08:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766783325; cv=none; b=LI3JARhTuaGyExhz8mq0ghjvVUL+71iohgrDOkQmoooBOJgG+A4W5MxmNld5c+OTv7+dj7OuTjFbtPIkWoyU+ijqvPOJCMOt9LciLlJ0C7qqYj2nWKHWKmLfIdZrq0H5dECZctIfBePuR+rhNz+INMZ1jWikcOX3PrjJ4bf4vBI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766783325; c=relaxed/simple; bh=qHhaP61mOajwYz1JwneBoVLmywYgSBLmUzjXBnzFvV8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QWigpmvTPMMQe3riA1/fz3CKF6Aqu8Ik7ZOrbmjvVlAa2tILywM8tAlnuQfu9XJjRQazSdUFwTfge8Fvm2Rl3jzb1fXkSj3pcIVdiRGTDVgqyLOG6pDLQG1bOT0JdSriUUwfIB5+sqriC79sn1CNZB5oqbJjJg0B1tqT+A6YfuQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QZMCrx9h; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mmaurer.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QZMCrx9h" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7bf5cdef41dso13191728b3a.0 for ; Fri, 26 Dec 2025 13:08:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1766783322; x=1767388122; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=d7mTBTyMaS+wDpWH5NUbweun10KVW7XKOhqDwPXW//Q=; b=QZMCrx9h+KxCWk/tCdd/zJzoi2kIqgmS0lXse65pTaLMdDAeuBgXYD7qhn9/hE9C9m RdYKESAY6BXV3hb5iYkqgPH0LkKRtzN2SSD1aCvrEKFFoCqn12d0Sg/xcR7fxy6Uc7xC USgh2OGcuSrkypZ6Tw6ftZI+u3teo6FQF72CUkZcYhAI/BKd2OVKIa4SonY8xW0eCV7z MfAky2miWF5V/3aWLnBDrM9gfv0985LdCgEgnrAEh83gjU0yfcaikrg/+34BAJeKX7Ek 0N3NAIASSTskfo/5c/KZINy6QQg9+kNTJ1wJ2FJk+Io8D37oXrTYSTsJ5+39UE194son DeRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766783322; x=1767388122; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=d7mTBTyMaS+wDpWH5NUbweun10KVW7XKOhqDwPXW//Q=; b=ffizOOMBaj4xzbsctX8enULCvY4a66T5n3Ezke5RVYRP5EIVvsgnVCjB7f8y0fPeP1 uTc5mf1qm1nxS7beIqlSrBGfKW3PR4I+XGbnmyLTmKxoU7m7FkbkkR5glkEupOG2vVHC hNjBlY19xkX3xoWhCUiSdUGnIRpZUI4EBUEy2fN3OLqgIDGDKq0nUJ2n5Ole9xkym/v8 bIVA0MQoRkzWxCt1tZkH8GYdadAEZPCQkSUbS/h7UylsVmTfogCvYluZaHOs9ENb0+jT Z5V9n7LWiVTDYhq4DH8UiU4MArjHyMIHORf0xhdToz0+hYzBakw5qI9bGXYq0eh7ujHd mGlw== X-Forwarded-Encrypted: i=1; AJvYcCUftYJiML3MIbfBV7mDWpgJ+f4pDNxl1YmNoX3EVIzyrJYGc9uwTejv6ddXieZatN2rXfmIBaMG28B1aWU=@vger.kernel.org X-Gm-Message-State: AOJu0YyJ0T9QNaMvUVziYsaWgx+y1c3vmPx0nZpwcGmAfOifJsQRg2rx kRQyQsuS2hZ0mn3myWQ+if23yaC3jj08l5K60FvXEgn+K1/Rg00cx23Pw0ZqnBsUxTMxe4NNfgY /EWJD3yT7nw== X-Google-Smtp-Source: AGHT+IGvGXpfjBwZ/r6mO5fSFGZHXr9hj2BRGllitYl1BOk6ELJoP+vvvf04ARqEjIkV1Vb4FuiYpGjACIe0 X-Received: from dlbdx18.prod.google.com ([2002:a05:7022:e12:b0:11a:44f1:daa3]) (user=mmaurer job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:220f:b0:11a:3a1c:6c5c with SMTP id a92af1059eb24-121721ad442mr22259846c88.3.1766783322103; Fri, 26 Dec 2025 13:08:42 -0800 (PST) Date: Fri, 26 Dec 2025 21:08:23 +0000 In-Reply-To: <20251226-transmute-v3-0-c69a81bf8621@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251226-transmute-v3-0-c69a81bf8621@google.com> X-Developer-Key: i=mmaurer@google.com; a=ed25519; pk=2Ezhl7+fEjTOMVFpplDeak2AdQ8cjJieLRVJdNzrW+E= X-Developer-Signature: v=1; a=ed25519-sha256; t=1766783314; l=8180; i=mmaurer@google.com; s=20250429; h=from:subject:message-id; bh=qHhaP61mOajwYz1JwneBoVLmywYgSBLmUzjXBnzFvV8=; b=NRmsJpRDFsVW4ZJALmYPzoGckv/9UAZVtWT9VyjGNFdHigQnABZfSYQjOAKrKPv664fJYhJ6e nauyPoDA387CAY2pWPbD8AOw6RYDi5TsJwbsvGqo2I5GLMd5blGsA6x X-Mailer: b4 0.14.2 Message-ID: <20251226-transmute-v3-4-c69a81bf8621@google.com> Subject: [PATCH v3 4/4] rust: transmute: Support deriving AsBytes/FromBytes on bindgen types From: Matthew Maurer To: Miguel Ojeda , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matthew Maurer Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Provide an internal macro pair, hidden from documentation, `FromBytesFfi` and `AsBytesFfi` which target the `ffi` crate as the definition site of the trait rather than the `kernel` crate. Use `FromBytesFfi` automatically on everything that might support it in bindings/uapi output. Use `AsBytesFfi` on one sample struct that supports it in bindings/uapi output. While it is safe to add this to any type, adding it to a type with padding will result in a compilation failure, so for now only add it to a single sample type. Signed-off-by: Matthew Maurer --- rust/Makefile | 8 ++++++-- rust/bindgen_parameters | 8 ++++++++ rust/bindings/lib.rs | 4 ++++ rust/macros/lib.rs | 24 ++++++++++++++++++++++-- rust/macros/transmute.rs | 12 +++++++----- rust/uapi/lib.rs | 4 ++++ 6 files changed, 51 insertions(+), 9 deletions(-) diff --git a/rust/Makefile b/rust/Makefile index 5c2cb78f7c258aa87d8128593159be1f5945252f..178aa3036c4acba1c4c26619691= 2da2d60fe0d5f 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -660,19 +660,23 @@ $(obj)/ffi.o: private skip_gendwarfksyms =3D 1 $(obj)/ffi.o: $(src)/ffi/lib.rs $(obj)/compiler_builtins.o FORCE +$(call if_changed_rule,rustc_library) =20 -$(obj)/bindings.o: private rustc_target_flags =3D --extern ffi --extern pi= n_init +$(obj)/bindings.o: private rustc_target_flags =3D --extern ffi --extern pi= n_init \ + --extern macros $(obj)/bindings.o: $(src)/bindings/lib.rs \ $(obj)/ffi.o \ $(obj)/pin_init.o \ + $(obj)/$(libmacros_name) \ $(obj)/bindings/bindings_generated.rs \ $(obj)/bindings/bindings_helpers_generated.rs FORCE +$(call if_changed_rule,rustc_library) =20 -$(obj)/uapi.o: private rustc_target_flags =3D --extern ffi --extern pin_in= it +$(obj)/uapi.o: private rustc_target_flags =3D --extern ffi --extern pin_in= it \ + --extern macros $(obj)/uapi.o: private skip_gendwarfksyms =3D 1 $(obj)/uapi.o: $(src)/uapi/lib.rs \ $(obj)/ffi.o \ $(obj)/pin_init.o \ + $(obj)/$(libmacros_name) \ $(obj)/uapi/uapi_generated.rs FORCE +$(call if_changed_rule,rustc_library) =20 diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters index fd2fd1c3cb9a51ea46fcd721907783b457aa1378..d56343ca03979e345f8adb7eb8f= d7f2b9d4be6ee 100644 --- a/rust/bindgen_parameters +++ b/rust/bindgen_parameters @@ -64,3 +64,11 @@ # Structs should implement `Zeroable` when all of their fields do. --with-derive-custom-struct .*=3DMaybeZeroable --with-derive-custom-union .*=3DMaybeZeroable + +# Every C struct can try to derive FromBytes. If they have unmet requireme= nts, +# the impl will just be a no-op due to the where clause. +--with-derive-custom-struct .*=3DFromBytesFfi + +# We can't auto-derive AsBytes, as we need a const-time check to see if th= ere +# is padding involved. Add it explicitly when you expect no padding. +--with-derive-custom-struct cpumask=3DAsBytesFfi diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index 0c57cf9b4004f176997c59ecc58a9a9ac76163d9..c29312fca1b01b707bb11b05d44= 6d44480a4b81f 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -31,6 +31,10 @@ #[allow(clippy::undocumented_unsafe_blocks)] #[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_tran= smutes))] mod bindings_raw { + use macros::{ + AsBytesFfi, + FromBytesFfi, // + }; use pin_init::{MaybeZeroable, Zeroable}; =20 // Manual definition for blocklisted types. diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs index d66397942529f67697f74a908e257cacc4201d84..bde94c2a8ddf708872bcd56a471= 3784b5ccdf04e 100644 --- a/rust/macros/lib.rs +++ b/rust/macros/lib.rs @@ -506,7 +506,7 @@ pub fn kunit_tests(attr: TokenStream, ts: TokenStream) = -> TokenStream { #[proc_macro_derive(FromBytes)] pub fn derive_from_bytes(tokens: TokenStream) -> TokenStream { let input =3D parse_macro_input!(tokens as DeriveInput); - transmute::from_bytes(input).into() + transmute::from_bytes("kernel", input).into() } =20 /// Implements `AsBytes` for a struct. @@ -536,5 +536,25 @@ pub fn derive_from_bytes(tokens: TokenStream) -> Token= Stream { #[proc_macro_derive(AsBytes)] pub fn derive_as_bytes(tokens: TokenStream) -> TokenStream { let input =3D parse_macro_input!(tokens as DeriveInput); - transmute::as_bytes(input).into() + transmute::as_bytes("kernel", input).into() +} + +#[doc(hidden)] +#[proc_macro_derive(FromBytesFfi)] +/// This is equivalent to `FromBytes`, but uses the `ffi` crate as the tra= it definition site +/// instead of `kernel`. This is intended for use inside `bindings`. Every= one else can refer to the +/// trait through `kernel`. +pub fn derive_from_bytes_trait(tokens: TokenStream) -> TokenStream { + let input =3D parse_macro_input!(tokens as DeriveInput); + transmute::from_bytes("ffi", input).into() +} + +#[doc(hidden)] +#[proc_macro_derive(AsBytesFfi)] +/// This is equivalent to `AsBytes`, but uses the `ffi` crate as the trait= definition site +/// instead of `kernel`. This is intended for use inside `bindings`. Every= one else can refer to the +/// trait through `kernel`. +pub fn derive_as_bytes_trait(tokens: TokenStream) -> TokenStream { + let input =3D parse_macro_input!(tokens as DeriveInput); + transmute::as_bytes("ffi", input).into() } diff --git a/rust/macros/transmute.rs b/rust/macros/transmute.rs index 56818880f8b5e3c1933a4e3361790d3c8be3238c..a62c9cb581e2fc1b4b141fc4bc7= 5550bda0618ba 100644 --- a/rust/macros/transmute.rs +++ b/rust/macros/transmute.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use proc_macro2::TokenStream; +use proc_macro2::{Span, TokenStream}; use syn::{parse_quote, DeriveInput, Fields, Ident, ItemConst, Path, WhereC= lause}; =20 fn all_fields_impl(fields: &Fields, trait_: &Path) -> WhereClause { @@ -26,7 +26,8 @@ fn struct_padding_check(fields: &Fields, name: &Ident) ->= ItemConst { } } =20 -pub(crate) fn as_bytes(input: DeriveInput) -> TokenStream { +pub(crate) fn as_bytes(crate_: &str, input: DeriveInput) -> TokenStream { + let crate_ =3D Ident::new(crate_, Span::call_site()); if !input.generics.params.is_empty() { return quote::quote! { compile_error!("#[derive(AsBytes)] does not= support generics") }; } @@ -34,7 +35,7 @@ pub(crate) fn as_bytes(input: DeriveInput) -> TokenStream= { return quote::quote! { compile_error!("#[derive(AsBytes)] only sup= ports structs") }; }; let name =3D input.ident; - let trait_ =3D parse_quote! { ::kernel::transmute::AsBytes }; + let trait_ =3D parse_quote! { ::#crate_::transmute::AsBytes }; let where_clause =3D all_fields_impl(&ds.fields, &trait_); let padding_check =3D struct_padding_check(&ds.fields, &name); quote::quote! { @@ -44,13 +45,14 @@ unsafe impl #trait_ for #name #where_clause {} } } =20 -pub(crate) fn from_bytes(input: DeriveInput) -> TokenStream { +pub(crate) fn from_bytes(crate_: &str, input: DeriveInput) -> TokenStream { + let crate_ =3D Ident::new(crate_, Span::call_site()); let syn::Data::Struct(ref ds) =3D &input.data else { return quote::quote! { compile_error!("#[derive(FromBytes)] only s= upports structs") }; }; let (impl_generics, ty_generics, base_where_clause) =3D input.generics= .split_for_impl(); let name =3D input.ident; - let trait_ =3D parse_quote! { ::kernel::transmute::FromBytes }; + let trait_ =3D parse_quote! { ::#crate_::transmute::FromBytes }; let mut where_clause =3D all_fields_impl(&ds.fields, &trait_); if let Some(base_clause) =3D base_where_clause { where_clause diff --git a/rust/uapi/lib.rs b/rust/uapi/lib.rs index 1d5fd9efb93e9db97fec84fca2bae37b500c20c5..2033b7125558d7ccfae67b94777= f5ce59593a528 100644 --- a/rust/uapi/lib.rs +++ b/rust/uapi/lib.rs @@ -34,6 +34,10 @@ type __kernel_ssize_t =3D isize; type __kernel_ptrdiff_t =3D isize; =20 +use macros::{ + AsBytesFfi, + FromBytesFfi, // +}; use pin_init::MaybeZeroable; =20 include!(concat!(env!("OBJTREE"), "/rust/uapi/uapi_generated.rs")); --=20 2.52.0.351.gbe84eed79e-goog