mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Matteo Kloiber <kernel@matt3o12.de>
To: Danilo Krummrich <dakr@kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>,
	Alice Ryhl <aliceryhl@google.com>,
	Abdiel Janulgue <abdiel.janulgue@gmail.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Andreas Hindborg <a.hindborg@kernel.org>,
	Gary Guo <gary@garyguo.net>
Cc: "Daniel Almeida" <daniel.almeida@collabora.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Bjorn Helgaas" <bhelgaas@google.com>,
	"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	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" <kernel@matt3o12.de>
Subject: [RFC PATCH v2 09/10] rust: drm: require a dma::Handle for shmem sg_table()
Date: Sun, 29 Nov 2026 23:24:28 +0100	[thread overview]
Message-ID: <20261129222429.2380652-10-kernel@matt3o12.de> (raw)
In-Reply-To: <20261129222429.2380652-1-kernel@matt3o12.de>

shmem::Object::sg_table() DMA-maps the object's pages for the device
passed in. Take a dma::Handle instead, so the mapping cannot be created
before DMA is configured. Convert Tyr, the only user.

Add Handle::new_for_test(), so that the KUnit test can build a handle
for a faux device, which is never probed and so never gets a
dma::Setup.

DRM C callbacks that allocate or map DMA memory on the driver's behalf
do not go through the handle and are not covered by this change.

Signed-off-by: Matteo Kloiber <kernel@matt3o12.de>
---
 drivers/gpu/drm/tyr/vm.rs    |  2 +-
 rust/kernel/dma.rs           | 12 ++++++++++++
 rust/kernel/drm/gem/shmem.rs | 23 +++++++++--------------
 3 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 8465d065dceb..d3614ca602cc 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -638,7 +638,7 @@ fn sm_step_map<'op>(
             return Err(EINVAL);
         }
 
-        let sgt = op.obj().sg_table(context.dma.device()).inspect_err(|e| {
+        let sgt = op.obj().sg_table(context.dma).inspect_err(|e| {
             dev_err!(context.dev, "Failed to get sg_table: {:?}", e);
         })?;
         let prot = match &context.op_type {
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 456ae9f90602..226d00397f56 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -164,6 +164,18 @@ pub fn device(self) -> &'bound device::Device<Bound> {
     pub(crate) fn as_raw(self) -> *mut bindings::device {
         self.dev.as_raw()
     }
+
+    /// Creates a handle for `dev` without calling [`Setup::configure`].
+    ///
+    /// For tests only.
+    #[cfg(CONFIG_KUNIT = "y")]
+    #[allow(dead_code)] // Only used by tests of other, optional abstractions.
+    #[inline]
+    pub(crate) fn new_for_test(dev: &'bound device::Device<Bound>) -> Self {
+        // INVARIANT: No DMA parameters were requested for `dev`, and tests do not change them while
+        // the handle exists.
+        Self { dev }
+    }
 }
 
 /// Trait to be implemented by DMA capable bus devices.
diff --git a/rust/kernel/drm/gem/shmem.rs b/rust/kernel/drm/gem/shmem.rs
index 2a07990ef366..f360e5bce0fc 100644
--- a/rust/kernel/drm/gem/shmem.rs
+++ b/rust/kernel/drm/gem/shmem.rs
@@ -11,11 +11,8 @@
 
 use crate::{
     container_of,
-    device::{
-        self,
-        Bound, //
-    },
     devres::*,
+    dma,
     drm::{
         driver,
         gem,
@@ -253,13 +250,10 @@ pub fn vmap<const SIZE: usize>(&self) -> Result<VMapRef<'_, T, SIZE>> {
     /// Creates (if necessary) and returns an immutable reference to a scatter-gather table of DMA
     /// pages for this object.
     ///
-    /// This will pin the object in memory. It is expected that `dev` should be a pointer to the
-    /// same [`device::Device`] which `self` belongs to, otherwise this function will return
-    /// `Err(EINVAL)`.
-    pub fn sg_table<'a>(
-        &'a self,
-        dev: &'a device::Device<Bound>,
-    ) -> Result<&'a scatterlist::SGTable> {
+    /// This pins the object in memory. The DMA handle must belong to this object's parent
+    /// device, otherwise this function returns `Err(EINVAL)`.
+    pub fn sg_table<'a>(&'a self, dma: dma::Handle<'a>) -> Result<&'a scatterlist::SGTable> {
+        let dev = dma.device();
         let parent = self.dev().as_ref();
         if dev.as_raw() != parent.as_ref().as_raw() {
             return Err(EINVAL);
@@ -576,6 +570,7 @@ unsafe impl<T: DriverObject> Sync for SGTableMap<T> {}
 mod tests {
     use super::*;
     use crate::{
+        device,
         drm::{
             self,
             UnregisteredDevice, //
@@ -702,14 +697,14 @@ fn vmap_io() -> Result {
     fn fail_sg_table_on_wrong_dev() -> Result {
         let (_dev, drm) = create_drm_dev()?;
         let reg = faux::Registration::new(c"EvilKunit", None)?;
-        let wrong_dev = reg.as_ref();
+        let wrong_dma = dma::Handle::new_for_test(reg.as_ref().as_ref());
 
         let obj = Object::<KunitObject>::new(&drm, PAGE_SIZE, ObjectConfig::default(), ())?;
 
-        assert_eq!(obj.sg_table(wrong_dev.as_ref()).err().unwrap(), EINVAL);
+        assert_eq!(obj.sg_table(wrong_dma).err().unwrap(), EINVAL);
 
         // If sgt_res was not initialized mistakenly with the wrong device, this should still fail.
-        assert_eq!(obj.sg_table(wrong_dev.as_ref()).err().unwrap(), EINVAL);
+        assert_eq!(obj.sg_table(wrong_dma).err().unwrap(), EINVAL);
 
         // TODO: Someday, we should test that creating an sg_table here still succeeds.
 
-- 
2.51.2


  parent reply	other threads:[~2026-11-29 22:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 22:24 [RFC PATCH v2 00/10] rust: dma: require device configuration before DMA Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 01/10] rust: pci, platform: pass InitTokens to probe() Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 02/10] rust: dma: add Setup, Params and Handle Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 03/10] gpu: nova-core: configure DMA through dma::Setup Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 04/10] drm/tyr: " Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 05/10] samples: rust: dma: " Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 06/10] rust: dma: require a Handle for coherent allocations Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 07/10] rust: scatterlist: require a dma::Handle for SGTable::new() Matteo Kloiber
2026-11-29 22:24 ` [RFC PATCH v2 08/10] rust: iommu: require a dma::Handle for IoPageTable::new() Matteo Kloiber
2026-11-29 22:24 ` Matteo Kloiber [this message]
2026-11-29 22:24 ` [RFC PATCH v2 10/10] rust: dma: remove the Device trait and its setters Matteo Kloiber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129222429.2380652-10-kernel@matt3o12.de \
    --to=kernel@matt3o12.de \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bhelgaas@google.com \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®