From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.216.248.212 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795991110; b=ubMrid5w2s3PGCKOG7TzGL2zqHae3468RzWwUEdur6qPgI+MwhYyQBijrYGeYuU/3K/7 yAeD2meCNJ1ifH9PZo4VXA9xdCHGqQunzIhmai/kirW/tEOI71jR2Tgy3KNfJceZE8wJv 81L8C7V8yUcO6/y10Wy6o9l5Hc6VE2c6pNBWKD/ybOIaE1g2q6KWtCa4IF/2gTUbQP3ZB n28XdwrCcN0WU14YfEMGEF+XrwzMn56sTYCOucI1vt6O4h+c8l5g79WdIkkapDYvtMQaN S81i4LavOWWdjvpwShvAvUShYk+HHMw3DK8JXqjhjWjJBTquLcEcvr42KBTsE1wqjVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991110; h=DMARC-Filter:From:DKIM-Signature:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=x2jhfl6xtpnlju+0zvfkef9eJegLcTHUkCV58+Hl1xo=; b=KDHr4htR4FhRmchwpHuW3jVVSOPKE72Jug2QDj/bq2cy1Upcwro9/q6IO/VDSDlLrnGC PPaBAYRbNCZtNL1SHjxOYfaDIa6DMYiYr1iPaYAck9It46R6fmz6Ym8cfc9fIGKGFwLr4 ty/7HKmkxunRaQ2O/jc1B8VvdO9IHO2JkrQsUdBMmjHLgcfMvtCb6uwn29iWu4HlRasc/ NxWafFcq1rTWhO8V2NCYAOB5VETyCn+qRSZagFHyGf1t1zvUMc1+ZwwxhTh6A7Sj1dY/A PJf5p10TU0McXWvSlwhr0RMvIw2KNWHHxNdRLBR0UkwjZZ7VFLdbGrFsu7ZS/mVTiYA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=matt3o12.de; spf=pass smtp.mailfrom=matt3o12.de; dkim=pass header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=hDqSiDpY; arc=none smtp.remote-ip=91.216.248.212 Received: from bulk0.mail-out.lima-city.de (bulk0.mail-out.lima-city.de [91.216.248.212]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E27DB606A699; Sun, 29 Nov 2026 22:25:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E27DB606A699 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=matt3o12.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=matt3o12.de header.i=@matt3o12.de header.a=rsa-sha256 header.s=securedbylima-20230709 header.b=hDqSiDpY From: Matteo Kloiber DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=matt3o12.de; s=securedbylima-20230709; t=1795991106; bh=lNdCZ2CaeEORZ5rouOPWYPw6xYFC0DJVw6BU06VW1RY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hDqSiDpYFX0NjcpXY1rNEeEJZAw40120Vje+h6N7rF2WcrFn7lbDRIG8BLTKUsmzx P2cpoFqaRcY4uz2MlxHE5/3chr/3jiw3rEw1JL+2DR5eem8R2CxYbabitpBq+XEhWz ocd09Sdi9Iy49jjPNDu5xEFVgSStFF9fEjzcqlvTRjf3E8lNMs1JQhfe6s+K2FXxhD uzj7AO1JpxyJPOuZ4194S73jarwlZpJPZD+cPNJBnLfMj0S0rai/txPnwIYR3QGolV TLs3UlQt+8/qkrNc4ihnfPkdhjOhVRzqFfFyUszY0hr1l02GxnCxjPcmMUz1AohmII nb8v42S3IG6kQ== To: Danilo Krummrich , Alexandre Courbot , Alice Ryhl , Abdiel Janulgue , Robin Murphy , Andreas Hindborg , Gary Guo Cc: Daniel Almeida , Miguel Ojeda , David Airlie , Simona Vetter , Bjorn Helgaas , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Greg Kroah-Hartman , rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev, dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev, linux-kernel@vger.kernel.org, Matteo Kloiber Subject: [RFC PATCH v2 10/10] rust: dma: remove the Device trait and its setters Date: Sun, 29 Nov 2026 23:24:29 +0100 Message-ID: <20261129222429.2380652-11-kernel@matt3o12.de> X-Mailer: git-send-email 2.51.2 In-Reply-To: <20261129222429.2380652-1-kernel@matt3o12.de> References: <20261129222429.2380652-1-kernel@matt3o12.de> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit All drivers configure DMA through dma::Setup now, and nothing calls the unsafe dma::Device setters anymore. Keeping them would allow changing the DMA parameters behind an existing dma::Handle. Remove the trait, its PCI and platform implementations, and the dma_set_mask_and_coherent() helper, which only the trait used. With the setters gone, a driver can only change the DMA parameters through its dma::Setup, and all Rust DMA APIs require the dma::Handle that configure() returns. Update the safety comments of the PCI and platform probe callbacks accordingly. Signed-off-by: Matteo Kloiber --- rust/helpers/dma.c | 6 --- rust/kernel/dma.rs | 83 +---------------------------------------- rust/kernel/pci.rs | 5 +-- rust/kernel/platform.rs | 5 +-- 4 files changed, 5 insertions(+), 94 deletions(-) diff --git a/rust/helpers/dma.c b/rust/helpers/dma.c index ff8f24dae9df..443360237606 100644 --- a/rust/helpers/dma.c +++ b/rust/helpers/dma.c @@ -17,12 +17,6 @@ __rust_helper void rust_helper_dma_free_attrs(struct device *dev, size_t size, dma_free_attrs(dev, size, cpu_addr, dma_handle, attrs); } -__rust_helper int rust_helper_dma_set_mask_and_coherent(struct device *dev, - u64 mask) -{ - return dma_set_mask_and_coherent(dev, mask); -} - __rust_helper int rust_helper_dma_set_mask(struct device *dev, u64 mask) { return dma_set_mask(dev, mask); diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs index 226d00397f56..6b06f57bbdd2 100644 --- a/rust/kernel/dma.rs +++ b/rust/kernel/dma.rs @@ -9,8 +9,7 @@ debugfs, device::{ self, - Bound, - Core, // + Bound, // }, error::to_result, fs::file, @@ -178,86 +177,6 @@ pub(crate) fn new_for_test(dev: &'bound device::Device) -> Self { } } -/// Trait to be implemented by DMA capable bus devices. -/// -/// The [`dma::Device`](Device) trait should be implemented by bus specific device representations, -/// where the underlying bus is DMA capable, such as: -#[cfg_attr(CONFIG_PCI, doc = "* [`pci::Device`](kernel::pci::Device)")] -/// * [`platform::Device`](::kernel::platform::Device) -pub trait Device<'a>: AsRef>> { - /// Set up the device's DMA streaming addressing capabilities. - /// - /// This method is usually called once from `probe()` as soon as the device capabilities are - /// known. - /// - /// # Safety - /// - /// This method must not be called concurrently with any DMA allocation or mapping primitives, - /// such as [`Coherent::zeroed`]. - unsafe fn dma_set_mask(&self, mask: DmaMask) -> Result { - // SAFETY: - // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. - // - The safety requirement of this function guarantees that there are no concurrent calls - // to DMA allocation and mapping primitives using this mask. - to_result(unsafe { bindings::dma_set_mask(self.as_ref().as_raw(), mask.value()) }) - } - - /// Set up the device's DMA coherent addressing capabilities. - /// - /// This method is usually called once from `probe()` as soon as the device capabilities are - /// known. - /// - /// # Safety - /// - /// This method must not be called concurrently with any DMA allocation or mapping primitives, - /// such as [`Coherent::zeroed`]. - unsafe fn dma_set_coherent_mask(&self, mask: DmaMask) -> Result { - // SAFETY: - // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. - // - The safety requirement of this function guarantees that there are no concurrent calls - // to DMA allocation and mapping primitives using this mask. - to_result(unsafe { bindings::dma_set_coherent_mask(self.as_ref().as_raw(), mask.value()) }) - } - - /// Set up the device's DMA addressing capabilities. - /// - /// This is a combination of [`Device::dma_set_mask`] and [`Device::dma_set_coherent_mask`]. - /// - /// This method is usually called once from `probe()` as soon as the device capabilities are - /// known. - /// - /// # Safety - /// - /// This method must not be called concurrently with any DMA allocation or mapping primitives, - /// such as [`Coherent::zeroed`]. - unsafe fn dma_set_mask_and_coherent(&self, mask: DmaMask) -> Result { - // SAFETY: - // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. - // - The safety requirement of this function guarantees that there are no concurrent calls - // to DMA allocation and mapping primitives using this mask. - to_result(unsafe { - bindings::dma_set_mask_and_coherent(self.as_ref().as_raw(), mask.value()) - }) - } - - /// Set the maximum size of a single DMA segment the device may request. - /// - /// This method is usually called once from `probe()` as soon as the device capabilities are - /// known. - /// - /// # Safety - /// - /// This method must not be called concurrently with any DMA allocation or mapping primitives, - /// such as [`Coherent::zeroed`]. - unsafe fn dma_set_max_seg_size(&self, size: u32) { - // SAFETY: - // - By the type invariant of `device::Device`, `self.as_ref().as_raw()` is valid. - // - The safety requirement of this function guarantees that there are no concurrent calls - // to DMA allocation and mapping primitives using this parameter. - unsafe { bindings::dma_set_max_seg_size(self.as_ref().as_raw(), size) } - } -} - /// A DMA mask that holds a bitmask with the lowest `n` bits set. /// /// Use [`DmaMask::new`] or [`DmaMask::try_new`] to construct a value. Values diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 10b00b641dea..a98fbc252cd2 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -150,7 +150,8 @@ extern "C" fn probe_callback( from_result(|| { // SAFETY: The bus initializes the DMA parameters before probing. This is the only // setup token issued for this binding, and no driver code has run for the binding - // yet. + // yet. The driver cannot use the Rust DMA allocation or mapping APIs until it + // consumes the token, and has no other way to change the DMA parameters. let dma = unsafe { dma::Setup::new(pdev.as_ref()) }; let data = T::probe(pdev, InitTokens { dma }, info); @@ -839,8 +840,6 @@ unsafe impl device::AsBusDevice for Device kernel::impl_device_context_deref!(unsafe { Device }); kernel::impl_device_context_into_aref!(Device); -impl<'a> crate::dma::Device<'a> for Device> {} - // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::sync::aref::AlwaysRefCounted for Device { #[inline] diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index bdb2c5c09b21..a24114c212bf 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -107,7 +107,8 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff from_result(|| { // SAFETY: The bus initializes the DMA parameters before probing. This is the only // setup token issued for this binding, and no driver code has run for the binding - // yet. + // yet. The driver cannot use the Rust DMA allocation or mapping APIs until it + // consumes the token, and has no other way to change the DMA parameters. let dma = unsafe { dma::Setup::new(pdev.as_ref()) }; let data = T::probe(pdev, InitTokens { dma }, info); @@ -547,8 +548,6 @@ pub fn optional_irq_by_name(&self, name: &CStr) -> Result> { kernel::impl_device_context_deref!(unsafe { Device }); kernel::impl_device_context_into_aref!(Device); -impl<'a> crate::dma::Device<'a> for Device> {} - // SAFETY: Instances of `Device` are always reference-counted. unsafe impl crate::sync::aref::AlwaysRefCounted for Device { fn inc_ref(&self) { -- 2.51.2