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 D5628297B6D; Fri, 23 May 2025 14:52:17 +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=1748011937; cv=none; b=RvwyH02a3Vy/cuLuo3IORhkjGATN3rBd5w26RTUYjx41NK988l06Av+5GKP4RFnhfJuJSrJ1+7+pV1hRo9yApni+m500LHhUYGX1mpJHV47NbnfluY0sT+zSDVjAe3uiwYkUfzgPb9dMl6yhD3Ef4OF2CwgdWmCEicduJITwWRQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748011937; c=relaxed/simple; bh=ZBjrKXZ/qrgShZJwAWynpWpJDpFSRPRO6cQ3mqt1FoQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TmQqA7dtMURJh9B3rlj9o6KmLzs6nOJ+v5WKS4XE4IKBX7eNxOKxotuncDsCQEJnMRMuASm1QBtc39flBLjyMZ793ArcFB1DLYQcmhHxpcl4jR224cT2m4iZN92N6h6xh7ynkurbAxS7qOGdNZB5gC5Oqd8y/V5Y3NNn0lgWJtQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V8o4gAuV; 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="V8o4gAuV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66EF2C4CEE9; Fri, 23 May 2025 14:52:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1748011937; bh=ZBjrKXZ/qrgShZJwAWynpWpJDpFSRPRO6cQ3mqt1FoQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V8o4gAuVpib9YeaRBYvriQwFSsghqC29E0YkrYw/fdtbPyJf1s7sRGXZfeDaQ8CYu 0Ri5zJbDNXXtkP1nLVMX7Xy+7X0/W+v9vk0GDNT4rDvWhzZj/vPaxLzLSziHVxe5/p 9MYYUQU6GtemeC/3Qm7wK060MEaieN0QOxXwN5V+pixKWgQcCCdiLLXuuy7JuHogNA /rx29tZM2NE7XFxYTgkyMEBfb400dSYQ5885sHlBL30BSi1GLvFOo2nB+/e1+sW72v lO0txyc9wn36AV9ZiaMeD1UsV+ytRlO97lHCTke19AZpaK5RCNZ+9HAv5iLuoj0URo y2RzpPGdxzQeA== 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 , Fiona Behrens Cc: Lyude Paul , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 08/13] rust: derive `Zeroable` for all structs & unions generated by bindgen where possible Date: Fri, 23 May 2025 16:51:04 +0200 Message-ID: <20250523145125.523275-9-lossin@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250523145125.523275-1-lossin@kernel.org> References: <20250523145125.523275-1-lossin@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@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 a08eb5518cac..9cadc10a8109 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -28,11 +28,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 c98d7a8cde77..06b23f8c2663 100644 --- a/rust/uapi/lib.rs +++ b/rust/uapi/lib.rs @@ -31,4 +31,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.49.0