From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F2AF2ED14F; Thu, 14 Aug 2025 09:31:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755163872; cv=none; b=PLku0nwExxJirOXXHQqXLGUw29keVbSk55AII+D1MIBPuZdL01RCHb6FUSmD6qinD3GJ7ZRRipogjZqCecXspmldxjD9VQn3hQOd7/vkQVZbY8MeJA2Gt6VLKmEK13679Pmc671bmSvVKgRyhjT2nI8GQZTeDkibv5oZlTS5whk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755163872; c=relaxed/simple; bh=ecMGQePExtmxO9h6Y8lQhIkD+aNJkUvwVrFIjOD2lF4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JdyvrD5ZpzVlHyGX+CtNMwWaozAlz4fpHFxhOwo1F3uq7QQhZ5/ZBuMI5FHWUVAMlpLx4aqFJIVhIX0VG+XWZhLpK1Ro3RKosDLsb8EhWYYphqfub13xwXhC522HeOObgmk9tGgjEogJ6ElMQZAuso6KEDK+uNqt5HlOG7ZtiGw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=M9uxn+8q; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="M9uxn+8q" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1502CC4CEEF; Thu, 14 Aug 2025 09:31:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755163872; bh=ecMGQePExtmxO9h6Y8lQhIkD+aNJkUvwVrFIjOD2lF4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M9uxn+8qIYOpyQbVSqDN8C015mQDGgjgcd9g1RRuIsIU6kvQrA9zkbDUinqfSYU3a Otl3EmLruaN2y5hR5a6NNh8XAa6AwDZGFA8bldPBSCC3MB49g0JNB6k6SYLLUDqCIy Qqqxo8NfLzApW+ay9JCE5zwnQ3mA9VHcYpcyc6yeaDziaPdF08kPdRfrousISFrPp1 fVd1ycNDI7poKa/Q9yhoU1nw8ExOjmsi6JlH2E+0JV0ULHHbDlR4sjN9u/XHFyP4Hb VE06IY7uCsdh3hbS/qpvhSqSzlpkW+p+kwMiKtYz3WMZgyQL6exUXDKEXvyarv8Hp4 aWiOXzQLfO85A== From: Benno Lossin To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Tamir Duberstein Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 02/11] rust: derive `Zeroable` for all structs & unions generated by bindgen where possible Date: Thu, 14 Aug 2025 11:30:29 +0200 Message-ID: <20250814093046.2071971-3-lossin@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250814093046.2071971-1-lossin@kernel.org> References: <20250814093046.2071971-1-lossin@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Using the `--with-derive-custom-{struct,union}` option of bindgen, add `#[derive(MaybeZeroable)]` to every struct & union. This makes those types implement `Zeroable` if all their fields implement it. Sadly bindgen doesn't add custom derives to the `__BindgenBitfieldUnit` struct. So manually implement `Zeroable` for that. Signed-off-by: Benno Lossin --- rust/bindgen_parameters | 4 ++++ rust/bindings/lib.rs | 8 ++++++++ rust/uapi/lib.rs | 2 ++ 3 files changed, 14 insertions(+) diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters index 0f96af8b9a7f..307545cf7363 100644 --- a/rust/bindgen_parameters +++ b/rust/bindgen_parameters @@ -34,3 +34,7 @@ # We use const helpers to aid bindgen, to avoid conflicts when constants are # recognized, block generation of the non-helper constants. --blocklist-item ARCH_SLAB_MINALIGN + +# Structs should implement Zeroable when all of their fields do. +--with-derive-custom-struct .*=MaybeZeroable +--with-derive-custom-union .*=MaybeZeroable diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index 474cc98c48a3..0c57cf9b4004 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -31,11 +31,19 @@ #[allow(clippy::undocumented_unsafe_blocks)] #[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))] mod bindings_raw { + use pin_init::{MaybeZeroable, Zeroable}; + // Manual definition for blocklisted types. type __kernel_size_t = usize; type __kernel_ssize_t = isize; type __kernel_ptrdiff_t = isize; + // `bindgen` doesn't automatically do this, see + // + // + // SAFETY: `__BindgenBitfieldUnit` is a newtype around `Storage`. + unsafe impl Zeroable for __BindgenBitfieldUnit where Storage: Zeroable {} + // Use glob import here to expose all helpers. // Symbols defined within the module will take precedence to the glob import. pub use super::bindings_helper::*; diff --git a/rust/uapi/lib.rs b/rust/uapi/lib.rs index 31c2f713313f..1d5fd9efb93e 100644 --- a/rust/uapi/lib.rs +++ b/rust/uapi/lib.rs @@ -34,4 +34,6 @@ type __kernel_ssize_t = isize; type __kernel_ptrdiff_t = isize; +use pin_init::MaybeZeroable; + include!(concat!(env!("OBJTREE"), "/rust/uapi/uapi_generated.rs")); -- 2.50.1