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 B15BB280CE8; Tue, 20 May 2025 19:23:19 +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=1747769000; cv=none; b=V/W5/uFX+ZaISp1txmDu7wKtyB2fHqVzVHfzUz07RHOnJnT/4C9oAxqCNa3tLpOdclneJlywE1B+SMEm7AQpnqHvrKWHRbksNnmp8Hj0fgn5n4XBgOZI7DzPNMcs/KsteWF5uhe1KDlzNhQo8nEm1s99m2gCT9NXCGjtFfFqtCE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747769000; c=relaxed/simple; bh=S7zYohKmvNL1bavFloZL+R5P0zEBWKzf+Hr9QtfRYZ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J5+SNipsi8ZdrN/YC5XgW3C2qFizAt3aMNEobry4JilYrMRg5go2/qBRV2wbmJLxp+u4foOS4KPfViSWLP2roqbAWGLRA+rY1/yPp3gKwstfj1E3ZTMchovxg3/qOg+jOsMTRGzNLzSrJT1OBzZhwXm7AqDoTYPDeFP69FAP4WY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XrQMEMWG; 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="XrQMEMWG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A90CC4CEE9; Tue, 20 May 2025 19:23:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1747768999; bh=S7zYohKmvNL1bavFloZL+R5P0zEBWKzf+Hr9QtfRYZ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XrQMEMWG7UF/h+uvqT8FyGMLrpNHBY012NxmAMWntUj6lhNagAJqAoBvjovQSXsbw BeYOXom0HBqTSWQk2Gc6mkq2uBs+0vgGF6re2Hli4jDy4t+kYwiLv45FWb82Orw4zr wLIrs21sDwcdug/hPe4p98mqrwkkK8Y+2qAIuXQ9KHsd1LSD/iYAP5/nYswFSZbtQK LvV2ZGpI8glqglLEEpIAcp0ZsOjFXYvGK3U2Aaukjb2nBdYjSRvdP+zZEOJK5NlGoC 1ssv0kx6qHrp09yQwwQGsvqgj1EPUx9LhQX2oAK/ISLs4NIBdxwlcxgQ0mG1874x4D chHzZwpaKTQrg== 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 , Benno Lossin , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] rust: derive `Zeroable` for all structs & unions generated by bindgen where possible Date: Tue, 20 May 2025 21:23:06 +0200 Message-ID: <20250520192307.259142-2-lossin@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250520192307.259142-1-lossin@kernel.org> References: <20250520192307.259142-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 --- This came from a discussion at [1]. The relevant parts for pin-init already got merged into rust-next for v6.16, so we only need to enable them for bindgen. I'm not sure on the impact of build times and rust-analyzer. We're adding a derive macro to every struct and union emitted by bindgen. Building using my test-config took 28.6s before and 29.1s after this change, but those are only two runs. Maybe we have to reevaluate this when more C code is scanned by bindgen. [1]: https://rust-for-linux.zulipchat.com/#narrow/channel/291565-Help/topic/Zeroable.20trait.20for.20C.20structs/with/509711564 --- rust/bindgen_parameters | 4 ++++ rust/bindings/lib.rs | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/rust/bindgen_parameters b/rust/bindgen_parameters index 0f96af8b9a7f..fa4c61ba028f 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 .*=pin_init::MaybeZeroable +--with-derive-custom-union .*=pin_init::MaybeZeroable diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index a08eb5518cac..38615c5b090d 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -33,6 +33,15 @@ mod bindings_raw { 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 pin_init::Zeroable for __BindgenBitfieldUnit where + Storage: pin_init::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::*; -- 2.49.0