From patchwork Fri Jan 3 16:46:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13925709 X-Patchwork-Delegate: bhelgaas@google.com 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 7DE361FBE9E; Fri, 3 Jan 2025 16:47:06 +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=1735922826; cv=none; b=TiJFKsfxQlWJ8dv/yroUyQSnxnV7R0p4+9BiZgAV5h0O7b/c1uHh/XTJPKACG72ErGS1ltW+1tlH7a2nSh8AKgRr0tcw9BVf2ZTqLBlEhT5ZhWyE/ShXLdrq6zsVyzo6wZHQpGSdRv1gdGQPHxD2oiOzttnqjKmRFQeCdBanoBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735922826; c=relaxed/simple; bh=L4CI2sWxV0mXBri8isrl2gtO7Mwaf4FLlktx2xGmygU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GKSpemMz/FgBgD4RcxEXm8vyK8uav6Kx+98DvfLjSn4XyXH7mi6DeLRh/r7Er5kT1apZFIe8nV3sTsfAnp3GJbLsdFIHGDPcIzEHlU/SsfFJk03p23QbslfpsohAODiPUqyCipArY7BqugWJcksvTydpgSLlBrYTp7xqRDxUVpo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FtPhSPt2; 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="FtPhSPt2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74BEEC4CEDC; Fri, 3 Jan 2025 16:47:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735922826; bh=L4CI2sWxV0mXBri8isrl2gtO7Mwaf4FLlktx2xGmygU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FtPhSPt2wU+YAGorHryEmLPVk96EmwsASPwAUYScQcLL15mTNU92DFzjI+Uj6XdNq vdP4E66Vk2Ho2lEy+ieGave0RzMnHzMaqnlwT1R0YyKvyV5vreqQpthb32R5exzp9k tPQtWp3cKsts1hkMwfebUHKkMAXKmhmP3hMueBl+4AOCmh5bd/w6WwMgXj6xwmVSwV aikshqkBJ4BC5F3vfz6WREBYFq48Ck0lAh82K1Vw9JnWP5n6AixVKYqEpmvMhFAFw7 izTqfcgBz7xd/G5vKZFNXApyCHvHoV5HI1YjAAmHVuzo5GTqb79JeybUXKtzkixaoo DkPZq/FRtPaqA== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, fujita.tomonori@gmail.com Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich , kernel test robot Subject: [PATCH 1/3] rust: pci: do not depend on CONFIG_PCI_MSI Date: Fri, 3 Jan 2025 17:46:01 +0100 Message-ID: <20250103164655.96590-2-dakr@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250103164655.96590-1-dakr@kernel.org> References: <20250103164655.96590-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The PCI abstractions do not actually depend on CONFIG_PCI_MSI; it also breaks drivers that only depend on CONFIG_PCI, hence drop it. While at it, move the module entry to its correct location. Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202501030744.4ucqC1cB-lkp@intel.com/ Fixes: 3a9c09193657 ("rust: pci: add basic PCI device / driver abstractions") Signed-off-by: Danilo Krummrich --- rust/kernel/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index e59250dc6c6e..b7351057ed9c 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -58,6 +58,8 @@ pub mod net; pub mod of; pub mod page; +#[cfg(CONFIG_PCI)] +pub mod pci; pub mod pid_namespace; pub mod platform; pub mod prelude; @@ -84,8 +86,6 @@ pub use bindings; pub mod io; pub use macros; -#[cfg(all(CONFIG_PCI, CONFIG_PCI_MSI))] -pub mod pci; pub use uapi; #[doc(hidden)] From patchwork Fri Jan 3 16:46:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13925710 X-Patchwork-Delegate: bhelgaas@google.com 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 503CC1FA8F3; Fri, 3 Jan 2025 16:47:10 +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=1735922830; cv=none; b=t16rAk3eJA8ZRuzAQlvkv9yxhEK21YxyWxOT/oKa8Qah9HWGBBTTmAeDKsk1IWGmlI4HKKQeDSOue5U0H3aL0aA2RTHwsA9h2UJoY9tUQlDXCKGcMHJUVcwHxIEM3SB3mPo6ilsttf/HROy33bSIHxsT0dLjnhylN5esm3sQKV8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735922830; c=relaxed/simple; bh=Ay1tki9Era+nIpnCONid/isRboWTp7NV/6rolAejqd4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JV93N5hOohalufanFTL8L58H0ZZIDIsQEGpGpiZaUVdQAbfGq6Yy8vgjNlbAAcihkIM/k9Zx5+Qvg8buhfl5uJz/rMrJMEyezCV6KwycVAUGAkqK7s8qUjNlP/tMUBdxe43vKmMA62QfhHhfjAlvQKJfr/iUjOdCXftqCLSHr4Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OG8g4wuk; 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="OG8g4wuk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79782C4CEE0; Fri, 3 Jan 2025 16:47:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735922829; bh=Ay1tki9Era+nIpnCONid/isRboWTp7NV/6rolAejqd4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OG8g4wuk88YqdGYAV4wvZ8U+Uk9tlna6VYqUXkNR+4YXz/AlLBDa060vQNGtnmFwp qu6MDQ2kfWhyfhUO5kgw8BmwDdkaYY2Zsz4JmTwrOKdaytinn2aum/F1hHaOAdGRSv xaVEIF/j9HPQ4+nSLKGXooy1C+Tixz6kwQbs8uTMdzuyfwkGuoJu6KcyS6gDNeeOyY EaG1bVGImCgTotxOdghA4oWX/IcdUD7aZ8wM0TjKW1WSvYzGu9rR7TCJy5ghIQ65NL IxfMXB62xc74lNEjk6ppPD3HVROJkenQPYD3JKE6dHt38W4s+GqXvaoH8uAR5g5z5U yuZeigQzghfdQ== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, fujita.tomonori@gmail.com Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 2/3] rust: io: move module entry to its correct location Date: Fri, 3 Jan 2025 17:46:02 +0100 Message-ID: <20250103164655.96590-3-dakr@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250103164655.96590-1-dakr@kernel.org> References: <20250103164655.96590-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The module entry of `io` falsely ended up in the "use" block instead of the "mod" block, hence move it to its correct location. Signed-off-by: Danilo Krummrich --- rust/kernel/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index b7351057ed9c..b11fa08de3c0 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -48,6 +48,7 @@ pub mod firmware; pub mod fs; pub mod init; +pub mod io; pub mod ioctl; pub mod jump_label; #[cfg(CONFIG_KUNIT)] @@ -84,7 +85,6 @@ #[doc(hidden)] pub use bindings; -pub mod io; pub use macros; pub use uapi; From patchwork Fri Jan 3 16:46:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13925711 X-Patchwork-Delegate: bhelgaas@google.com 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 2F1631FC10C; Fri, 3 Jan 2025 16:47:13 +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=1735922834; cv=none; b=hmeCr+aW8MjYmG2vJvQCpFPTkWdZjY/TYX2jZAGDFWw9LAK6v/0JlkNZuf3TkDguIS/mkOm86MouFmGvs7bsl0xKMuNyv+Ii1Dy59O7x0ge/LsO2PU5erDsWjMYNNYlzE1xkiDbZOnfnBDXQ2tx8PNAYMxqzzVW1vLhHC9JCJ9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735922834; c=relaxed/simple; bh=OUnzSPYLSoc//E88dncYu34255MsFamAkyC4TSeJnOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a8kt2LHdzhfSEdiXzkP0lg4JPkzE8wgNTn+2v5JFS7zG10i3k8DcXZq5df1KzTP4PAKDiMtp/DKq62sX2lLLz8Lp5vwszV21EuZfDv+MoV49cmd5lfLAnM2ndd4w/Jgx6kO6B3krz7rkZ7CWJugZsIHleMv+oxFJVcGQMLuFLiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lbHKslwZ; 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="lbHKslwZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D7D1C4CECE; Fri, 3 Jan 2025 16:47:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735922833; bh=OUnzSPYLSoc//E88dncYu34255MsFamAkyC4TSeJnOM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lbHKslwZsO3Yhwrdd9QB6XqQPYXeT7I4jNNqoYmM1YKxVhxS9nx+Er3CKFcnuiTb+ ah5gmWj9MvxhFeFsKFFoRpWHnCNiM0fZDnfwoyB2DUHaVlXlndk9J/AbFoH64ym3p9 g9w6XJ1CU8eYjDBN5qNu4VLIViQbbl4DAJbk1IyUYmLB4O2wHRwVWz0IK1dYXsvAKN sM3pDihgDFNiiGoADiXeHDwwF3mOOh5pAJQ1k5bB0Gsw8Bg2Jp/BPZ4RHybbkhO5om qQ6iW5Cepc6yfsTQ5IDgRJmQIOAOcuOGw2FgWw91Gb+jqoWGnqreVQ1ZfZIbsfT4wU 3IqWUegEatF1w== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, bhelgaas@google.com, fujita.tomonori@gmail.com Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 3/3] rust: driver: address soundness issue in `RegistrationOps` Date: Fri, 3 Jan 2025 17:46:03 +0100 Message-ID: <20250103164655.96590-4-dakr@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250103164655.96590-1-dakr@kernel.org> References: <20250103164655.96590-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The `RegistrationOps` trait holds some obligations to the caller and implementers. While being documented, the trait and the corresponding functions haven't been marked as unsafe. Hence, markt the trait and functions unsafe and add the corresponding safety comments. This patch does not include any fuctional changes. Reported-by: Gary Guo Closes: https://lore.kernel.org/rust-for-linux/20241224195821.3b43302b.gary@garyguo.net/ Signed-off-by: Danilo Krummrich Reviewed-by: Gary Guo --- rust/kernel/driver.rs | 25 ++++++++++++++++++++----- rust/kernel/pci.rs | 8 +++++--- rust/kernel/platform.rs | 8 +++++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/rust/kernel/driver.rs b/rust/kernel/driver.rs index c630e65098ed..2a16d5e64e6c 100644 --- a/rust/kernel/driver.rs +++ b/rust/kernel/driver.rs @@ -17,23 +17,35 @@ /// For instance, the PCI subsystem would set `RegType` to `bindings::pci_driver` and call /// `bindings::__pci_register_driver` from `RegistrationOps::register` and /// `bindings::pci_unregister_driver` from `RegistrationOps::unregister`. -pub trait RegistrationOps { +/// +/// # Safety +/// +/// A call to [`RegistrationOps::unregister`] for a given instance of `RegType` is only valid if a +/// preceding call to [`RegistrationOps::register`] has been successful. +pub unsafe trait RegistrationOps { /// The type that holds information about the registration. This is typically a struct defined /// by the C portion of the kernel. type RegType: Default; /// Registers a driver. /// + /// # Safety + /// /// On success, `reg` must remain pinned and valid until the matching call to /// [`RegistrationOps::unregister`]. - fn register( + unsafe fn register( reg: &Opaque, name: &'static CStr, module: &'static ThisModule, ) -> Result; /// Unregisters a driver previously registered with [`RegistrationOps::register`]. - fn unregister(reg: &Opaque); + /// + /// # Safety + /// + /// Must only be called after a preceding successful call to [`RegistrationOps::register`] for + /// the same `reg`. + unsafe fn unregister(reg: &Opaque); } /// A [`Registration`] is a generic type that represents the registration of some driver type (e.g. @@ -68,7 +80,8 @@ pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit) }; - T::register(drv, name, module) + // SAFETY: `drv` is guaranteed to be pinned until `T::unregister`. + unsafe { T::register(drv, name, module) } }), }) } @@ -77,7 +90,9 @@ pub fn new(name: &'static CStr, module: &'static ThisModule) -> impl PinInit PinnedDrop for Registration { fn drop(self: Pin<&mut Self>) { - T::unregister(&self.reg); + // SAFETY: The existence of `self` guarantees that `self.reg` has previously been + // successfully registered with `T::register` + unsafe { T::unregister(&self.reg) }; } } diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index d5e7f0b15303..4c98b5b9aa1e 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -23,10 +23,12 @@ /// An adapter for the registration of PCI drivers. pub struct Adapter(T); -impl driver::RegistrationOps for Adapter { +// SAFETY: A call to `unregister` for a given instance of `RegType` is guaranteed to be valid if +// a preceding call to `register` has been successful. +unsafe impl driver::RegistrationOps for Adapter { type RegType = bindings::pci_driver; - fn register( + unsafe fn register( pdrv: &Opaque, name: &'static CStr, module: &'static ThisModule, @@ -45,7 +47,7 @@ fn register( }) } - fn unregister(pdrv: &Opaque) { + unsafe fn unregister(pdrv: &Opaque) { // SAFETY: `pdrv` is guaranteed to be a valid `RegType`. unsafe { bindings::pci_unregister_driver(pdrv.get()) } } diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 03287794f9d0..50e6b0421813 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -19,10 +19,12 @@ /// An adapter for the registration of platform drivers. pub struct Adapter(T); -impl driver::RegistrationOps for Adapter { +// SAFETY: A call to `unregister` for a given instance of `RegType` is guaranteed to be valid if +// a preceding call to `register` has been successful. +unsafe impl driver::RegistrationOps for Adapter { type RegType = bindings::platform_driver; - fn register( + unsafe fn register( pdrv: &Opaque, name: &'static CStr, module: &'static ThisModule, @@ -44,7 +46,7 @@ fn register( to_result(unsafe { bindings::__platform_driver_register(pdrv.get(), module.0) }) } - fn unregister(pdrv: &Opaque) { + unsafe fn unregister(pdrv: &Opaque) { // SAFETY: `pdrv` is guaranteed to be a valid `RegType`. unsafe { bindings::platform_driver_unregister(pdrv.get()) }; }