From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.46.28 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796200541; b=5tw05TkJF2ioa8Zf85hMc5GrAOU7YpQFxZRaM5mO/kG+6Jc9bLe6GN3PMOUwe6oDtWzJ cbZPF5soYJa343Yrq3E/mLttJXM4AHY7d5KJoxqWNJCdaN9OqQf43W8l1RogQv3ZGpdRl 3EBM0HaAhWFjaP6s5A9MzlMoKa1XWnlvzdt3sCQpO7uMa7TQOkCvs5aWSEcjHHTfoPXJK agHCx82TF8SMjpwtpIMgmVap3l2lpssHklGORW3/5SZ+mFcZPNrZui/dbtOzjyg/0belS rIVrGMw4Mu3J9uxnr1WqJ4R47pZ8MrMV9bVlYrXQfEP9wTZAZOdHFfRAd5S3IM0JU5Q== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796200541; h=DMARC-Filter:DKIM-Signature:Received:Received: X-MS-Exchange-Authentication-Results:Received-SPF:Received:Received: Received:From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding:Content-Type: X-Originating-IP:X-ClientProxiedBy:X-EOPAttributedMessage: X-MS-PublicTrafficType:X-MS-TrafficTypeDiagnostic: X-MS-Office365-Filtering-Correlation-Id:X-MS-Exchange-SenderADCheck: X-MS-Exchange-AntiSpam-Relay:X-Microsoft-Antispam: X-Microsoft-Antispam-Message-Info:X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-OriginatorOrg: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-Id: X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-Transport-CrossTenantHeadersStamped; bh=gPO/x0o8/418C1at/BrUWxl9nSPsgVcktjnVReRvVt0=; b=att3I7uI7vRsJelL6ql0O22XtGOgTkPDxLxApYXsezBJP8LCWkAPwA059jPI3svrdoP8 dFB1LBuoi5A7pqewyEzd40hoiGe8mKxbH07ARJhMyV5CKGbdJiA0gfhzI8htywtfJ4YqL Keh6m3DfwMsvwL0SylS4TS/GWQwS5jId5/xHuMk13worh0jpWg3y4tg2ggdle+EyInz2F dt5kha3sFfVvKNV95blznvrl9/QzSn19499gKpkWvatme/VnPnwSspgCMWR7K/qRsiqdJ pxKoh7vCgcAfzkX+WmvbLn7m/FkqvHnKlrHJUsKYfaCdfGPwy6bWadKqwmcslD6MvPg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=RAolElSZ; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.46.28 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010028.outbound.protection.outlook.com [52.101.46.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (secp384r1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11655606A332; Wed, 2 Dec 2026 08:35:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 11655606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.a=rsa-sha256 header.s=selector2 header.b=RAolElSZ ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=X8sMTPI9ila2VOUZGsm6/Z6vxkoz+E5Cf2Fk2tgN8/mmoMxR5VDwAPZK1cHjfQ58vtNTz7ydEgZ++5CGcFwH+9audFckchaRCOarMSO6RrWFcj8/8TUbKDHC0KnDKNCZnGuK0gpaoPYSURVYLZtPrH1VArFH/mvNjV578D426G+clOUy/HZIhs0f7QVlnQXnbzJ21DzfAxnm+PC0zFe4KiO3V3a8QS3580NMb4kwPAquhd7uJuXfuL3Kp/s04m8q0sJioJsOmi7xeyk4FVzrxwRak4svvgCIrwD0hoRflVOKh3PdWKWKqiJ6YeHWFNzfdm3yhNd8ncf/ydJCT3Mdfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gPO/x0o8/418C1at/BrUWxl9nSPsgVcktjnVReRvVt0=; b=mh+YMAhWyCJ4BlK3cvH/0owpPzVAOd92PRHSkCYMIwiDnnD2lzt8iCVLkpqzr3To2F5kF+1IMbZCXfjERzaE2PFNM71S9T8CCYsxZ1aHtA96OshsAhAkEE25RSywWjnpx7N0dQArUDhVWm1u80OVjXz1pJfnDTBl22nW1irOmaCLCb4smuli1aJ9qAU2uBQ9c1YB7hK1Xmb1QXfC9s2iW+YyFc6CT+cJ7CqhE1bhHwjbdAFaF00lTv/leg1YfuTth/NQvf4a1hTlZEfiK3UlGPdJ2YIQHA0WchYwqMygzoYAe38r/QsBXe7jVbwaz9YV281N+/PjI8hpBfm+qeDiuw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gPO/x0o8/418C1at/BrUWxl9nSPsgVcktjnVReRvVt0=; b=RAolElSZcrxjBuUQYxK/ftQfuCylcgkvmZdJiKO6dsd2oHvPFm9Z+reAQATxi5CMczZJsy+adBJ/+UGYhn4Wt6EZJzg/Pf7dyAH7uh1U63RNsoOWQDFhSXIjqv5fWEAOH69tQFLmJz52fsBVniLo3uZAIuBdc04XBFzBUvK1JcOjWqobRekYpgZNe2068YyltrWZtsdHABsQseOoo1yY62Qy13cHJpSW/D6hD/PG7hv8d7bLL5m3zT3UISSoSFGi1jExQ+Oc+uRliRrj9cv9rKGtMbtbFOmCwqsdLwjln9TGZ4BXzZ6LDCZt2TFqcjXBuIXpcLy5posTd4ylbkFBaw== Received: from BN9P220CA0007.NAMP220.PROD.OUTLOOK.COM (2603:10b6:408:13e::12) by DS2PR12MB9687.namprd12.prod.outlook.com (2603:10b6:8:27b::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Wed, 2 Dec 2026 08:35:33 +0000 Received: from LV8PEPF0000005E.namprd02.prod.outlook.com (2603:10b6:408:13e:cafe::99) by BN9P220CA0007.outlook.office365.com (2603:10b6:408:13e::12) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.11 via Frontend Transport; Wed, 2 Dec 2026 08:35:33 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by LV8PEPF0000005E.mail.protection.outlook.com (10.167.245.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.5 via Frontend Transport; Wed, 2 Dec 2026 08:35:32 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 00:35:18 -0800 Received: from kalok-SPC741D16QM3.nvidia.com (10.126.231.37) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Wed, 2 Dec 2026 00:35:04 -0800 From: Alok Kumar To: , , , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v6 13/34] gpu: nova-core: mm: add BarMapping Date: Wed, 2 Dec 2026 13:58:37 +0530 Message-ID: X-Mailer: git-send-email 2.53.0 In-Reply-To: References: X-Mailing-List: rust-for-linux@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 Content-Type: text/plain X-ClientProxiedBy: rnnvmail202.nvidia.com (10.129.68.7) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV8PEPF0000005E:EE_|DS2PR12MB9687:EE_ X-MS-Office365-Filtering-Correlation-Id: 379af8f2-f882-4ec8-8489-08df504f3aef X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|36860700016|23010399003|82310400026|10067099003|56012099006|11063799006|261028144727099003|261021135927099003|261021121427099003|261026121527099003|261022105027099003|261022075727099003|261026140227099003|261102123227099003|261009223027099003|22082099003|18002099003|6133799003; X-Microsoft-Antispam-Message-Info: 6WEpO08sofsVPSgvXFzP2PVYPJutrVLvkDRkGVEvA9eN1zSToQ/Ij5HCCIkl/n4pKpX1VMrLr8QBSKifxUzSI1hTxrkREI8KIQUcBzx+fRBH9K/RxDwuX7IcNW7Mqnc7L3nuM5rqd4BsfwJ8hvR40+2wB8nzgMQhavXMItIL01Uz/RSQueGcybbfObh8JmyTjyJn0So5Uc1CWb/CQOustt+6+ycpW4X4h0uAoqmIvbA8idRqcZce0TXY6DJNYlNiMC/kGd6ZOI+UIiQRFsox0+hHQYQ7PCk3CTNOdmNnG+M4DsxViwkpfaZLBnsOfs+mbdpImXPYkfUyZtpQHQwJr5abeWOUaeTeNnh7yiZE7NK94ZqyvHyZnCIDzg5asiMSTqoiEkWTzAA+O1QGBhzA474Dch/69oxoklXXkZnpawGc1F7Dfh6OH0V0uU0emg4qqdzFG9P+3xhDvv5XdjK44vhxToR0W5CNzne2lfGkb0AfT22v8scBOc/FWznCyHjW5/F2/eCps1ViHT98AxTA1tXIprw8IVD0Yk0gGYxF62Ea/ohpuyVuZX0j5bPyzOVklJnwl8j3BCidOBc68/nzSdgtiY4LA4ka+gsIejQ2pxj+P4RcZ/6TkvYoCHHIGBi5tTo2hYrGgZbEdQCTF3UzPOynjAVaoCpvBkYeCHbUqzj9DYEgkL0ZmKUS85V0WsAiKSGODiQzBmb18k3z7YAN4A== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(36860700016)(23010399003)(82310400026)(10067099003)(56012099006)(11063799006)(261028144727099003)(261021135927099003)(261021121427099003)(261026121527099003)(261022105027099003)(261022075727099003)(261026140227099003)(261102123227099003)(261009223027099003)(22082099003)(18002099003)(6133799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7WM6A0fQ/R2ATWlIlb5eYkSCljHvMOW/7DuoeXZraRgeg6GyVm2ckTmIKqq7dOQ4gFCoLL2aB9o1NION3uAZcNHnv7PR5dT3SeXANDymwRs4Qd669Bj3D0D8pf16K4+9sUORtgJNESr46SWNWW7R6s+iBV8hcOtI9h5lHg5nvKaZQyVjWYdxz8YqMj8yO6yA21/dUK4Qa/gZVijjHwTeqM1XY6+7z2nx1O2+a7WLILbqZs449nk5aWchWJEgsz4a07oLGCutLa4AmHEjmPU61QuaH6eX9t6qPfnJ0yULDGQb2KjzJauS4O9bUDkJ/E7rwo+h/k+6BB41QpR+Ntb/QM8EMKFMyx0n1l4bsbGh9J47KWEnIqauNvdR9OWyJVvBJ1lxJEH5BVRSrWcbW0b2vibkprY+BC477XbkLIriJlz7q6GA8jgbzdt8IvJVDK9a X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2026 08:35:32.9912 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 379af8f2-f882-4ec8-8489-08df504f3aef X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: LV8PEPF0000005E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS2PR12MB9687 From: Zhi Wang vGPU communication buffers need CPU access to regions within their VramBlocks. Add BarMapping to map these regions through BAR1. Store a VramRegion view that borrows its backing allocation, and carry that borrow through the mapping's lifetime. Borrow BarUser and the MM mutex for mapping operations, and unmap on drop using the retained MM dependency. The mapping does not acquire shared ownership of VramBlock. Provide an explicit unmap operation so owners can observe errors before releasing resources. Keep the mapping and borrowed region on failure, remember the first error and reject subsequent accesses. Owners must retain failed mappings and their backing allocation until device teardown; drop does not retry an unmap that already failed. Cc: Alistair Popple Signed-off-by: Zhi Wang Co-developed-by: Alexandre Courbot Signed-off-by: Alexandre Courbot Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/mm/bar_user.rs | 100 +++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/drivers/gpu/nova-core/mm/bar_user.rs b/drivers/gpu/nova-core/mm/bar_user.rs index ff1d0e443281..64c63cf2e977 100644 --- a/drivers/gpu/nova-core/mm/bar_user.rs +++ b/drivers/gpu/nova-core/mm/bar_user.rs @@ -10,6 +10,7 @@ Mmio, // }, new_mutex, + num::casts::FromSafeCast, prelude::*, sync::Mutex, // }; @@ -22,6 +23,7 @@ MappedRange, Vmm, // }, + vram::VramRegion, GpuMm, Pfn, Vfn, @@ -171,6 +173,104 @@ fn drop(&mut self) { } } +/// A BAR mapping that borrows its backing VRAM allocation. +#[must_use] +pub(crate) struct BarMapping<'pool, 'map, 'gpu> { + /// Mapping of `region`. + access: BarUserAccess<'map, 'gpu>, + mm: &'map Mutex>, + /// The VRAM region that is mapped. + region: VramRegion<'pool>, + unmap_error: Option, +} + +#[expect(dead_code)] +impl<'pool, 'map, 'gpu> BarMapping<'pool, 'map, 'gpu> { + /// Maps `region`, which must start and end on a page boundary. + pub(crate) fn new( + bar_user: &'map BarUser<'gpu>, + mm: &'map Mutex>, + region: VramRegion<'pool>, + writable: bool, + ) -> Result { + const U64_PAGE_SIZE: u64 = cv!(PAGE_SIZE); + + let range = region.range(); + if !range.start.is_multiple_of(U64_PAGE_SIZE) || !range.end.is_multiple_of(U64_PAGE_SIZE) { + return Err(EINVAL); + } + + let num_pages = usize::try_from(region.len() / U64_PAGE_SIZE).map_err(|_| EOVERFLOW)?; + let mut pfns = KVec::with_capacity(num_pages, GFP_KERNEL)?; + for page in 0..num_pages { + let address = u64::from_safe_cast(page) + .checked_mul(U64_PAGE_SIZE) + .and_then(|byte_offset| byte_offset.checked_add(range.start)) + .ok_or(EOVERFLOW)?; + pfns.push(Pfn::from(VramAddress::from_raw(address)), GFP_KERNEL)?; + } + + let access = bar_user.map(&mut mm.lock(), &pfns, writable)?; + + Ok(Self { + access, + mm, + region, + unmap_error: None, + }) + } + + pub(crate) fn region(&self) -> &VramRegion<'pool> { + &self.region + } + + fn access(&self) -> Result<&BarUserAccess<'map, 'gpu>> { + if self.access.mapped.is_none() { + return Err(ENODEV); + } + if let Some(error) = self.unmap_error { + return Err(error); + } + Ok(&self.access) + } + + /// Returns a typed MMIO view of a `T` located at the start of the mapped region. + /// + /// Returns an error if the mapping is not live anymore, if a `T` does not fit within the + /// region, or if the region is not properly aligned for `T`. + pub(crate) fn view(&self) -> Result> { + // The mapping covers exactly the region, so this also checks that the `T` fits within it. + self.access()?.view(0) + } + + /// Unmap while retaining the mapping and its backing storage on failure. + /// + /// The first error is retained; later calls do not retry the operation. + pub(crate) fn unmap(&mut self) -> Result { + if let Some(error) = self.unmap_error { + return Err(error); + } + let result = self.access.unmap(&mut self.mm.lock()); + if let Err(error) = result { + self.unmap_error = Some(error); + } + result + } +} + +impl Drop for BarMapping<'_, '_, '_> { + /// Drop unmaps the region and may sleep. Call [`Self::unmap`] to observe errors; + /// on failure the owner must retain this object until device teardown. + fn drop(&mut self) { + if self.unmap_error.is_some() { + return; + } + if let Err(error) = self.unmap() { + kernel::pr_err!("failed to unmap BAR1 region: {:?}\n", error); + } + } +} + /// Run MM subsystem self-tests during probe. /// /// Tests page table infrastructure and `BAR1` MMIO access using the `BAR1`