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=1795991109; b=ygtnOmrbMDXs/aJHTSE1D+RJaTFr+pDSZnZN+376/DdFSgrHKt1aIJMv567bj4GjNd35 CZ6uZfzykZegLPR+LUglCfGwnY8uRRBzlKpxsYTFzUg9F0ZlG9wN8pUk4Ox6dkqbYyuGn WsK0GgYD4s8VDuOlZxeaS5x2E+f+IC0AT+WObh9I9N8d6PHNCe9kMvaDB/zfwFwZTFEKr n9/vpl9w0zscL8WP4J7YkArvlVDT2NiOXj5AvHdFPLmQzSOMHXx4dhHN53M4Y6tmhO7YB j/2+TBjMYg/pajh//4mL2kiw7hmbRtDfM5R6qBdgzxKoG1pAxEy55o15byUqqehwyzg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795991109; h=DMARC-Filter:From:DKIM-Signature:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=Orv3E5aI4Ugbh4eRkUbcFSlFWG6BmDY1JWAPLsOtAhI=; b=LbYeTa3wCPVx8lh+rIkcwbJjrvklbyeH0qnj5I//hJmfkDjqBkoTczP1pYjH6F/BVSgG 8ybn0yCnxbNePwrWfcYItVQQqLCMsR/F6ssIhmzR2NSwS7/3qRYrp20vs7Bsi4aQfhRQZ JhhS25n2GRteOqfTo4JIU22gT52aPpDW9VdCgKGJf761BcfW7NlNqxDIF3xRn1BfjCm+5 CMBOe6nmNTSuzFbURBNFRTJe8U7nXvfwIj7fa5zcC8yDpLoDNEZ7eoXqla42zLE9QT8XZ o47y/QoH0i/EVeL6s7j0V0XrITwCmhBxV6z9k+pJFIAAtrjA9fJKMJ6pzaDYcfPyoVg== 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=Ofw31vYS; 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 DEFB9606A68B; Sun, 29 Nov 2026 22:25:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DEFB9606A68B 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=Ofw31vYS From: Matteo Kloiber DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=matt3o12.de; s=securedbylima-20230709; t=1795991105; bh=YSghYnUyQKu/jIyA6D3GaBh+ZOFpV+YWIvEzQ0Tetys=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ofw31vYSnXzBwNSqVY9geYebs6kUVOw4T8Oz0bRAbaglBlo6WZzHth2qh9Mv/lR/H 6ycMMwbb6DZLiFTwANyKInvhNpcog8x2rOqa/VdDYplBQ/Oksj3HQDXDDsmySd+q5F 6x9wnIVdHdt4fLGJN1vu3JdGvXBFvhMT7/UDpgKLimoSJLZK2z2vKvDUy4oAIRUCyb 4g/s0mzQMVAecOezXZ6cAmgzfwRp9AdLt7czgBDL2VplREU/7rmOcRNb/kslBYxEQp svRlViBAIuJAwHpCzyvwPIndwswQTE94RTUOfSbdo/h/YVu+ojfTlEUhJ0moPcN+BP 6B4WWiqrHzc8g== 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 09/10] rust: drm: require a dma::Handle for shmem sg_table() Date: Sun, 29 Nov 2026 23:24:28 +0100 Message-ID: <20261129222429.2380652-10-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 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 --- 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 { 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) -> 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(&self) -> Result> { /// 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, - ) -> 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 Sync for SGTableMap {} 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::::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