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.57.53 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796200812; b=sfDDzEJgmnvZTwl4cAOX+b+myE19ljjgCme+Hkz0mpwLPWDFxtGtFjJ1rZ1jo+V+0C/j juAbUqsrlsKccA2IoziIqpUuri0QcjLlJFmq0z6IfsZRkWACbFGRan9g63lFM33GFMcbS uUHqUuZS37ZSZ9DwGob3ReZkrVjkTI3Ot+0EPzdJXdvOjWZRucbPPRQ7BwyyrYcNUHtqo Auja8se0GZaeuKPlYP0cz9MyiL08M8BqP5Yks0Ew/G3tP/97cqAOYjIVtQlF7Tp9sCjj1 6fnVIGJ0diKE5DVyae5vDWojgXD/RMA4G5txiodgB0x+hdZKzfnnNT4mDp08MN4mU1A== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796200812; 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=ln6Oa9EIUE2KnS/uP+swgAy8PNKrPryZCGM1U4YMoAI=; b=ggam+SshNA+or2ygWmS44s3NWc4OQK5i3VTi6J/r5fc1xY8Wg/dAgfZ7Qr+yGQ25QSx3 mRo3OMBR/CFPpAs+sq/muyUHqh9UKUvrKmlNE5yTvsZu/kPDvTIlJ5zAehvQgBQqlkMTg hCn9ePJV/5v3QMEGjdOpCJnbDx6sOMywyuSdAXCl2DoHXFprGP9Qt9pgATu5FJYXJm0np kowuZD3wa9hCC4+tGfxdgbmAThiL+/ODb1GKX35dEGJ6Iqk2KtwC3pZ277ko9CpNiP/L8 t7Oloy6s2yz1rIlRDqW8Vkb+PtbiCtiCQCCMgOozjoE9aEEppqbS/BxVnaFr75aOU+A== 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=NSzVJ2GZ; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.57.53 Received: from BN8PR05CU002.outbound.protection.outlook.com (mail-eastus2azon11011053.outbound.protection.outlook.com [52.101.57.53]) (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 5C3D7606A327; Wed, 2 Dec 2026 08:40:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5C3D7606A327 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=NSzVJ2GZ ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QW/QJQ/oQuBznvXFnkRqK7FkYvT10u3YPnDBBa0WmUSN4jc00HFX2Gb7u/+OJkk0PqK/SXK/VAyhHdZZLdoGPfnTiOG1kL71yrOgcLqZ0WGuCXwzP3yZYHGEtt1y+zgqDk/BDheCdTHmawOcgOBydQiTyXiVOk3NHZrmRHpSYI0Jm/HvLboLCG/kXrGVIlzV5qXB33A3UEwQllJbPzCIcX2LYHLJhpyMFRF9T7vIRfbJPjbaEwFZqukgzZQ/F3M0lpH2uPxCrY2JkRDe4HkXXPupdH5Ym0SyxP9HdJw1kEtr3HnQn0IhzK/gU7t9npWc+OkGzgeNdx95I7n4m3KKug== 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=ln6Oa9EIUE2KnS/uP+swgAy8PNKrPryZCGM1U4YMoAI=; b=Ei9JsSaKcI4d9x/2RQbQeAr352/qRL2gic0TnJHh0bsoSM2z5ae6JwAUF4IGqEVlLPHH84ib8DVQtX4hp56GKQdNRLcSrfgxwy68TTdlOnc4QTh2P7Io9JXUqPiHtnXflowE7QHIGWdsq+6EMzYYCtwyASUXqNQCKKYuaX2++AWNAckDinU30zkXevHatkAe99nHYxASpVHPDDCLDR2KnveFZLrMGOvgY0TnU2nmhYL2S1pMrCbprIVMELvkdyCTqNvTwqdM0PHxosY/1stuNaAKHMutxhozU76LgvhBYvbxKuYtI9VBN9+/6iYDjKWPogzmYU4cC3KlfN/N6PPwNA== 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=ln6Oa9EIUE2KnS/uP+swgAy8PNKrPryZCGM1U4YMoAI=; b=NSzVJ2GZUDC2R3+heyRPkkEgTys5rBUn61YmYACI/l4VZMLyQQ1S0iWCRLXEx+rczwNrvLRk02DNeTBiR5Ayi3H+Z7Udpwo/HoqxcbFxhxm16DklKIXR+FIvYFXMReI2BdjoI4I9WqAheSJM0LEsW7oz/JuIzfefh+ZPJ3UC7kb0t+ot1u9Y/9C9c28YsSh93QN24YeecYbu8mu34lGcicdR6DFhUKzi6vlBIpS/l9KDxe0WtDNIfCdQRjxVeEqkfiKGi3BL/GdoDGuGuYaBmJWKi6kS/x0aMpNHH3jQxGo0zhlxli/fmAAhoq0XlWBeTKnPOk91y9u50Qy+063KFw== Received: from BN9PR03CA0496.namprd03.prod.outlook.com (2603:10b6:408:130::21) by PH7PR12MB8180.namprd12.prod.outlook.com (2603:10b6:510:2b6::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:40:03 +0000 Received: from LV8PEPF0000005A.namprd02.prod.outlook.com (2603:10b6:408:130:cafe::3a) by BN9PR03CA0496.outlook.office365.com (2603:10b6:408:130::21) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.662.13 via Frontend Transport; Wed, 2 Dec 2026 08:40:03 +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 LV8PEPF0000005A.mail.protection.outlook.com (10.167.245.135) 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:40:03 +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:39:44 -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:39:30 -0800 From: Alok Kumar To: , , , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v6 32/34] gpu: nova-core: vgpu: scrub guest VRAM with CeUtils Date: Wed, 2 Dec 2026 13:58:56 +0530 Message-ID: <1d0b026993de6ff7ac605827215d672a85a35f1b.1796191008.git.alkumar@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: 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 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: LV8PEPF0000005A:EE_|PH7PR12MB8180:EE_ X-MS-Office365-Filtering-Correlation-Id: e65f7266-dfeb-4a26-e75d-08df504fdbe5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|1800799024|23010399003|376014|7416014|82310400026|261028144727099003|261022105027099003|10067099003|5023799004|11063799006|56012099006|261102123227099003|261021121427099003|261009223027099003|261021123027099003|261021135927099003|261022115727099003|261022113327099003|261026121527099003|261026140227099003|22082099003|18002099003|3023799007; X-Microsoft-Antispam-Message-Info: Hm12/s/eedbMaGrOf8az7iFaiBtUTjz7QiUEomsKcc1yQUgflf81AbxZWacHNM0Z4SodQD5QH3Tqc4WkVzuEwKGzW4XVR5H6L+NPynKl7dFIEo+fWHsdvvMDHN1+/N2xrOfaJ9Yo79DJFKMy659mYXI2xEP4gHv0kDM6kEk7xjpB5GE1F+Fo7yG8nkh99hX3M4ih2MFmChw+sEWJaaSOMyYEf3Ug6+3IG/WU/XTo12pAkUvTymXOUTBjdMvjdh7TB6oU0PhKZZ3uj05iUUN9a9J90DKRfeUufnasRwwAY2tTCEhhCfX82nSLTbN8xjFif6ekAFt5NCdAm6g6Y0oDYGDOHaqzgYcgDrtOOdfQzBI7Jt/inoA0r7DiFl1qX9TK0nev4fqJgd4FoHqfcdHMvAEl9Tm7OpeDO4iz+X8Sq54IZoDRVaHtQH3a+TrgoVchmL7iAXedvde1AjTUp9GryaxvK/yI4c8Utlpgtgp8miKSHsMWBx5lcVE/fxzn2ViNEJzLqSNcu/UfbLr0pRg3gYSRagbK+J6R3zFOrNeqiKBLgmeHORFhSVuQIge4DDhACKpMOcH8zxxX9WVKJxMEabrGtKKCXdkQonX1z6W/St6TpUvJDSq6iWiep6xBdecDGNZJ+LA1LciYBu+nkdEIAM6G7yVe013C8IvNTnp4XkXtMYvz0kPpHFoynD2/fNLPA7TnAgM4di0dB2Hv1rt6wg== 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)(36860700016)(1800799024)(23010399003)(376014)(7416014)(82310400026)(261028144727099003)(261022105027099003)(10067099003)(5023799004)(11063799006)(56012099006)(261102123227099003)(261021121427099003)(261009223027099003)(261021123027099003)(261021135927099003)(261022115727099003)(261022113327099003)(261026121527099003)(261026140227099003)(22082099003)(18002099003)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9DB4rGAz7AzxqtuL8WJOh3s3RR+n/Yy4xgCMoMpUYZXPwSwnBwCcD2bkMF/drk85Uwp44+M3cpb64Xh/yfbsREDoZawLjMGAWhvq9CyRyBtbuNl/Gmp6YNBN37gak2aGd4E456JD+Kyan4xSn6BbYoyQd1ABaLcQUP5zI0hrwAY867AxG0NtjNamqOXAUmzWjqpBsQRtV7nln/QZhtgw1HMHfCJseOydiy/hCSJS5a1RytSuW+pcvk+9TiZy0frZiRPTW1JxTNgVVxqEMk77zsyMbSy5h2HXVoLFzcS0hF0bSnZQLR4xo2Jwp3IuFbowD9TnarUmkpfRCX81yS8w5nIR5UT+1MkTftExYXAM6iOM5WDgGMAKisuaSwwSb/sgwEAWbOPA4wvxa88NeMk6OwIRbzvZMuoA0zuLG44NFOT/F/tv6mggKDBIXnwzi2Zn X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2026 08:40:03.0435 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e65f7266-dfeb-4a26-e75d-08df504fdbe5 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: LV8PEPF0000005A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8180 From: Zhi Wang Scrub guest VRAM before plugin boot and after plugin shutdown, before its slot can be reused. Reserve the last channel ID in each instance range for CeUtils and give the remaining channels to the plugin. Submit scrubs in chunks and wait for completion through a temporary BAR1 mapping of the CeUtils semaphore. Release the mapping on both success and failure, preserving a polling error if unmapping also fails. Keep allocation, scrubbing and release within the instance lifecycle. Retain host resources until device removal when firmware ownership is uncertain or a scrub fails, and do not retry a recorded teardown failure. Signed-off-by: Zhi Wang Signed-off-by: Alok Kumar --- drivers/gpu/nova-core/vgpu.rs | 1 + drivers/gpu/nova-core/vgpu/commands.rs | 4 - drivers/gpu/nova-core/vgpu/instance.rs | 56 +++++++- drivers/gpu/nova-core/vgpu/scrubber.rs | 172 +++++++++++++++++++++++++ 4 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 drivers/gpu/nova-core/vgpu/scrubber.rs diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs index 2d60bb5951e5..fa4ff2505815 100644 --- a/drivers/gpu/nova-core/vgpu.rs +++ b/drivers/gpu/nova-core/vgpu.rs @@ -41,6 +41,7 @@ mod gsp_plugin_rpc; mod hal; mod instance; +mod scrubber; mod vram; bitfield! { diff --git a/drivers/gpu/nova-core/vgpu/commands.rs b/drivers/gpu/nova-core/vgpu/commands.rs index de700ff923f8..ad2b6e4d0a67 100644 --- a/drivers/gpu/nova-core/vgpu/commands.rs +++ b/drivers/gpu/nova-core/vgpu/commands.rs @@ -319,7 +319,6 @@ fn read(payload: &mut MessagePayload<'_>) -> Result { } /// Whether a failed allocation may still have transferred CHID ownership to firmware. -#[expect(dead_code)] pub(super) enum CeUtilsAllocError { /// A matching firmware response explicitly rejected the allocation. NotOwned(Error), @@ -328,7 +327,6 @@ pub(super) enum CeUtilsAllocError { } /// Allocate a CeUtils channel and validate its semaphore description. -#[expect(dead_code)] pub(super) fn alloc_ceutils( dev: &device::Device, cmdq: &Cmdq<'_>, @@ -370,7 +368,6 @@ pub(super) fn alloc_ceutils( } /// Release a CeUtils allocation, including one whose allocation reply was lost. -#[expect(dead_code)] pub(super) fn free_ceutils(dev: &device::Device, cmdq: &Cmdq<'_>, gfid: Gfid) -> Result { dev_dbg!(dev, "free CeUtils: gfid={}\n", gfid.get()); @@ -379,7 +376,6 @@ pub(super) fn free_ceutils(dev: &device::Device, cmdq: &Cmdq<'_>, gfid: Gfid) -> } /// Submit an asynchronous guest FB scrub and return its work identifier. -#[expect(dead_code)] pub(super) fn submit_ceutils_scrub( dev: &device::Device, cmdq: &Cmdq<'_>, diff --git a/drivers/gpu/nova-core/vgpu/instance.rs b/drivers/gpu/nova-core/vgpu/instance.rs index 91648ddf2d7c..39bf7f4d5fb8 100644 --- a/drivers/gpu/nova-core/vgpu/instance.rs +++ b/drivers/gpu/nova-core/vgpu/instance.rs @@ -31,12 +31,14 @@ use super::{ commands::{ + free_ceutils, negotiate_plugin_version, send_bootload, send_cleanup, send_plugin_config, send_shutdown, - set_plugin_bme, // + set_plugin_bme, + CeUtilsAllocError, // }, fw::commands::{ encode_plugin_config_params, @@ -47,6 +49,7 @@ }, gsp_plugin_comm::CommBufferRegion, gsp_plugin_rpc::PluginRpc, + scrubber::CeUtils, vram::{ VgpuVramLayout, VgpuVramSlot, @@ -156,12 +159,41 @@ struct VgpuInstance<'pool, 'gpu> { // Unmap the communication region before returning its slot and channel IDs. vram_slot: VgpuVramSlot<'pool>, chids: ChannelIdReservation<'gpu>, + ceutils: Option, needs_teardown: bool, /// An uncertain or failed operation retains resources until device removal. failure: Option, } impl<'gpu> VgpuInstance<'_, 'gpu> { + fn initialize(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result { + let ceutils_chid = + u32::try_from(self.chids.end.checked_sub(1).ok_or(EINVAL)?).map_err(|_| EOVERFLOW)?; + let ceutils = match CeUtils::allocate(vgpu.dev, vgpu.cmdq, self.gfid, ceutils_chid) { + Ok(ceutils) => ceutils, + Err(CeUtilsAllocError::NotOwned(error)) => return Err(error), + Err(CeUtilsAllocError::MayOwn(error)) => { + self.failure = Some(error); + dev_err!(vgpu.dev, "CeUtils allocation failed: {:?}\n", error); + return Err(error); + } + }; + + let result = ceutils.scrub_guest_fb( + vgpu.dev, + vgpu.cmdq, + vgpu.bar_user, + vgpu.mm, + &self.vram_slot.fbmem, + ); + self.ceutils = Some(ceutils); + if let Err(error) = result { + // A failed wait does not establish that the submitted scrub has stopped. + self.failure = Some(error); + } + result + } + fn activate(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result { let dev = vgpu.dev; self.bootload(dev, vgpu.cmdq, &vgpu.fifo_engine_list)?; @@ -180,7 +212,17 @@ fn teardown(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result { } let result = (|| { self.shutdown(vgpu.dev, vgpu.cmdq)?; - + if let Some(ceutils) = self.ceutils.as_ref() { + ceutils.scrub_guest_fb( + vgpu.dev, + vgpu.cmdq, + vgpu.bar_user, + vgpu.mm, + &self.vram_slot.fbmem, + )?; + free_ceutils(vgpu.dev, vgpu.cmdq, self.gfid)?; + self.ceutils = None; + } if self.needs_teardown { send_cleanup(vgpu.dev, vgpu.cmdq, self.gfid)?; self.needs_teardown = false; @@ -246,7 +288,7 @@ fn configure_plugin(&mut self, dev: &device::Device) -> Result { self.dbdf, self.vgpu_type.vgpu_type_id, self.vm_pid, - u32::try_from(self.chids.len()).map_err(|_| EOVERFLOW)?, + u32::try_from(self.chids.len().checked_sub(1).ok_or(EINVAL)?).map_err(|_| EOVERFLOW)?, self.num_plugin_channels, )?; @@ -418,6 +460,9 @@ fn allocate_instance( .map(usize::from_safe_cast) .and_then(NonZeroUsize::new) .ok_or(EINVAL)?; + if channels_per_instance <= cv!(1) { + return Err(EINVAL); + } let chids = vgpu .chid_pool .reserve_ids(channels_per_instance, Alignment::SZ_1)?; @@ -434,6 +479,7 @@ fn allocate_instance( plugin_rpc: PluginRpc::new(comm, vgpu.bar, gfid), vram_slot, chids, + ceutils: None, needs_teardown: false, failure: None, }) @@ -483,7 +529,9 @@ fn create_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, info: InstanceInfo) self.instances.reserve(1, GFP_KERNEL)?; let mut instance = self.allocate_instance(vgpu, info)?; - let result = instance.activate(vgpu); + let result = instance + .initialize(vgpu) + .and_then(|()| instance.activate(vgpu)); if result.is_err() { match instance.teardown(vgpu) { diff --git a/drivers/gpu/nova-core/vgpu/scrubber.rs b/drivers/gpu/nova-core/vgpu/scrubber.rs new file mode 100644 index 000000000000..ade630b5a490 --- /dev/null +++ b/drivers/gpu/nova-core/vgpu/scrubber.rs @@ -0,0 +1,172 @@ +// SPDX-License-Identifier: GPL-2.0 +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + +//! Per-VM CeUtils guest VRAM scrubbing. + +use kernel::{ + device, + io::io_read, + prelude::*, + time::{ + delay::fsleep, + Delta, + Instant, + Monotonic, // + }, + types::ScopeGuard, // +}; + +use crate::{ + gsp::cmdq::Cmdq, + mm::{ + bar_user::BarUser, + vram::VramRegion, + Pfn, + SharedGpuMm, + VramAddress, // + }, + vgpu::instance::Gfid, // +}; + +use super::commands::{ + self, + CeUtilsAllocError, // +}; + +// Semaphore layout from OpenRM `channel_utils.h`. +const NV_CEUTILS_SEMA_PAGE_MAGIC: u32 = 0xce5e_5ea0; + +#[repr(C)] +struct CeUtilsSemaphoreHeader { + magic: u32, + payload: u32, +} + +static_assert!(size_of::() == 8); + +const SCRUB_REQUEST_SIZE: u64 = 4 * 1024 * 1024 * 1024; + +// Host timeout policy; not a firmware ABI value. +const SCRUB_TIMEOUT: Delta = Delta::from_secs(5); + +/// A firmware-owned per-VM CeUtils allocation. +/// +/// Submitted scrubs must complete before releasing this allocation or its VRAM. +pub(super) struct CeUtils { + gfid: Gfid, + semaphore_address: u64, +} + +impl CeUtils { + pub(super) fn allocate( + dev: &device::Device, + cmdq: &Cmdq<'_>, + gfid: Gfid, + chid: u32, + ) -> core::result::Result { + let semaphore_address = commands::alloc_ceutils(dev, cmdq, gfid, chid)?; + Ok(Self { + gfid, + semaphore_address, + }) + } + + /// Scrub the complete guest VRAM and wait for semaphore completion. + pub(super) fn scrub_guest_fb( + &self, + dev: &device::Device, + cmdq: &Cmdq<'_>, + bar_user: &BarUser<'_>, + mm: &SharedGpuMm<'_>, + fb: &VramRegion<'_>, + ) -> Result { + let mut offset = fb.range().start; + let end = fb.range().end; + while offset < end { + let size = core::cmp::min(SCRUB_REQUEST_SIZE, end - offset); + let work_id = commands::submit_ceutils_scrub(dev, cmdq, self.gfid, offset, size)?; + wait_scrub_complete(bar_user, mm, dev, self.semaphore_address, work_id)?; + offset = offset.checked_add(size).ok_or(EOVERFLOW)?; + } + + Ok(()) + } +} + +/// Poll the GSP-owned CeUtils semaphore page through a temporary BAR1 map. +fn wait_scrub_complete( + bar_user: &BarUser<'_>, + mm: &SharedGpuMm<'_>, + dev: &device::Device, + semaphore_address: u64, + work_id: u32, +) -> Result { + let pfn = Pfn::from(VramAddress::from_raw(semaphore_address)); + let semaphore_map = mm.with_locked(|mm| bar_user.map(mm, &[pfn], false))?; + let semaphore_map = ScopeGuard::new_with_data(semaphore_map, |mapping| { + if let Err(error) = mm.with_locked(|mm| mapping.release(mm)) { + dev_err!( + dev, + "failed to release semaphore BAR1 mapping: {:?}\n", + error + ); + } + }); + + let result = (|| { + let semaphore = semaphore_map.view::(0)?; + + let magic = io_read!(semaphore, .magic); + if magic != NV_CEUTILS_SEMA_PAGE_MAGIC { + dev_warn!( + dev, + "bad CeUtils semaphore magic {:#x}, expected {:#x}\n", + magic, + NV_CEUTILS_SEMA_PAGE_MAGIC, + ); + return Err(EIO); + } + + let start = Instant::::now(); + loop { + let value = io_read!(semaphore, .payload); + if value.wrapping_sub(work_id) < 0x8000_0000 { + dev_dbg!( + dev, + "scrub completed after {:?}: semaphore={:#x}, target={:#x}\n", + start.elapsed(), + value, + work_id, + ); + return Ok(()); + } + + if start.elapsed() >= SCRUB_TIMEOUT { + dev_warn!( + dev, + "scrub timed out: semaphore={:#x}, target={:#x}\n", + value, + work_id, + ); + return Err(ETIMEDOUT); + } + fsleep(Delta::from_millis(1)); + } + })(); + + let cleanup = mm.with_locked(|mm| semaphore_map.dismiss().release(mm)); + match result { + Ok(()) => cleanup, + Err(error) => { + if let Err(cleanup_error) = cleanup { + dev_err!( + dev, + "failed to release semaphore BAR1 mapping after error {:?}: {:?}\n", + error, + cleanup_error, + ); + } + Err(error) + } + } +}