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.52.35 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796200732; b=xZsuk9Dn/oUfmZskVERJJ5CvHr1WT7jYDbg1QLQy18EQ111h4d7Evq1zoc9LBz5NZNow PVPGobA9gFSYEKjk2UJOEGncy4twlJwQmwSIp+wBUIXADVEPohVes2RFSeUGUuIZibzcv m1D5Ki6W+BC1E/CX4k6cKvQyBGx6AIIttHR9nPXLZFmn/GgGMtihyRWHb2QR17ukElnkS V2SzPTmMRLtbOSRCnH/O5VoRPIEGyqHGI/NIWtetdImzYwT4/K5YLGv1B1btzsNI6S8UO oHvFI/j6BLUjF0UzdGmK692RY6nQRJzLFn0pQfA/Ogkmn6VX6B52tgGtNiowUJJ8A+g== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796200732; 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=geNldJpA2RUeGxWxaB4Fbqe6+xzaWHNFsQBhOG3/E1o=; b=vnZIJ95H46tOq23rssh07Q4o32ErCV0CMia1u6W2j+QTzdJFg2k4rkcmKuRE6hdr/SXf a4rg23OSMutp7Oerl7yh3yhxIQdf3bslDo72P1ioS8u4ymLVyNjCZlUr/SIAENtn2DaXc O3nVlUYHMaMoS9xAeU7wG8OQB2KgJ0Q0dnzlTcijHCKfCghiXLbYcsKeTh/2Iq6UF70sN cxcyzkZ6snAWxU6wWrmEI5ruXSdjAcXtQey91fq4FdRmTezUZkdKShCsae6rwsFEmUzBJ WB0EQol1tJ2Pk79W8dMX/1bOAao3pgKRZaNJFsw+w7jGpIUNFyOwcV5HX2ARpHkmJPQ== 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=QjccmgbU; arc=pass header.oldest-pass=0 smtp.remote-ip=52.101.52.35 Received: from BL2PR02CU003.outbound.protection.outlook.com (mail-eastusazon11011035.outbound.protection.outlook.com [52.101.52.35]) (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 4C91D606A323; Wed, 2 Dec 2026 08:38:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4C91D606A323 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=QjccmgbU ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wK5OaVwugXnbTuJlqBpBn7pFBxemSaUPwyaZvnvNvdwnQrwEqdFv2Ins0dxEUaUWxJJmraUEWj1AAriqlzujje2r4c4+nknzPR+MP+xZ6EcBWNZWhIbBl7Qpht/a8K0rP3Xf3agMmAnOp73VJRtuJovRsBIhLYk/Uvi3HBCqcF3I3vJWA8jKCAGh2zGJ5Pec8ByuASMSGGPy9t3TZUXb59a26GnE1tyqSOqLk5Da7neSQHgXUywlyBqKSR07nhBnpcHsfTuI81frkVuUhFAbz/5zfJjIFi3pr2ZwoxcnUhBBtTvBVdXmxu39uQqVNCzaRrF6FkyVMF7oY0VKAPiJJQ== 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=geNldJpA2RUeGxWxaB4Fbqe6+xzaWHNFsQBhOG3/E1o=; b=mDM2MfIkAgRhdOD0RF9mAKwFPvNyjOKNGzzB/IdjzzfvI8KianC9pZKRsL9wYe96YBd7Xk5wGOryOlM98pxOeHazB29jRSgwPotoVPP+K2GynXK9iNpelliMVlhSCCDrfxC/4d4cGlPwYaVMBV7Zmxq+njClnVomZ5+tWGZuis3mtMAxcSppywFc/CEwAS9aDAzYCt71r0nFBj0NZFzHbZxeZR+H+Bgp1ZV18WzAKf96Q3O9ndNn+cqc2gQyGm6OvajbYYygRpjWTZ+uOuE5gc+yEwBi3SOw5kzqx51wUf0jQ20D8Tg/8i2KO4pxt7rRGu22wAtLPu1v0M4FDMVaeQ== 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=geNldJpA2RUeGxWxaB4Fbqe6+xzaWHNFsQBhOG3/E1o=; b=QjccmgbUkGFu0J95IM1h/M+VPv/ezQ1cmeULDXUQdYh+xvFr3cVBEVroO+pvrYde9ZIcD5aOPTus9C5ziq2QFx2wC1s6sdfm2WeonkIoHl7f1TBFT9tE3ZJNih0hXhCOhfxW0zx1pPxYHAjSf3gu6DjeK7VjbQlPoCSrE7jfYhWVYeEScw4dIpn66qLm676ztRdh9G4p88snJrAkx+bd/ou70KgC80fQbt8QqNTMY7TtB4k5jyJYcyKFVBf3G7L+mgqA+Z/yh8Lj11PEWJ6urvkStsLsanF3V9aObkgLvGX2Ze2WjGXGhHwkoHl1nrnivbdL0X4RFziqr59eFhb5Hg== Received: from BN1PR13CA0019.namprd13.prod.outlook.com (2603:10b6:408:e2::24) by PH7PR12MB6858.namprd12.prod.outlook.com (2603:10b6:510:1b4::20) 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:38:38 +0000 Received: from LV8PEPF0000005E.namprd02.prod.outlook.com (2603:10b6:408:e2:cafe::72) by BN1PR13CA0019.outlook.office365.com (2603:10b6:408:e2::24) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.709.4 via Frontend Transport; Wed, 2 Dec 2026 08:38:37 +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:38:37 +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:38:20 -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:38:07 -0800 From: Alok Kumar To: , , , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [PATCH v6 26/34] gpu: nova-core: vgpu: add instance shutdown Date: Wed, 2 Dec 2026 13:58:50 +0530 Message-ID: <1ee16d4314bd675abf5d9fdcb494260bc7c8afe8.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: LV8PEPF0000005E:EE_|PH7PR12MB6858:EE_ X-MS-Office365-Filtering-Correlation-Id: d76d1dfd-3e44-49ee-9819-08df504fa916 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|376014|7416014|1800799024|23010399003|82310400026|10067099003|261102123227099003|56012099006|5023799004|261026140227099003|261022075727099003|261021123027099003|261021121427099003|261022105027099003|11063799006|261026121527099003|261028144727099003|261021135927099003|261009223027099003|22082099003|18002099003|3023799007; X-Microsoft-Antispam-Message-Info: 3Zi/eyFq8sjprqVU9dxZ13KSoelT3W+54Gppp1EY6sZDDM6Z6rZyjMCNqa0JYpYRokP86Zc/NCV41FTv6d7NJWxtsH9PB8XqmLEJNy59+j2khJlwoPeqlYtMf8GHSulwdcpQX1WYdCJVAGKuQhm2e7hWYq9f7jyU+tqTFB12VPDutaNARpG6OVDEYHjwBj1M3Qa1xwm0+Mchbzo8bHqtB3Azenm/HZ21p9qeEymffm2+lHuaRvRikb4O7xHh+GmMqSG0RWUju0wO0ndZSJ6e6X2R0ia6DVwoVx7YNda1uGXEKkJvVYsjma0ek1venVMl7KL3kxSPsB5b6RpRQMAL7oXKqN1UW5vUQe2hFNIiqFng9KVQ/Xu4hs9Jh9EObVWfB08BGQLSeia7PUzZePbqRq5aJUjID9BG0eQxGDUSp/Of8SroDaqjVF8+S6HeWb6di38HJG/HYaXddy4X2kGHzHpkewd7ur8IBu0yiMykkbDi55SbfEtLxnaRHIMrx8/eagOlTsNycm3lAXPtvxLdZNGjzHjJkq8BbvJECIRDyhZVHBSec6ZWeBXqMYfvP8b1scLZO1uWyvg7NMfwvFFYfqdqCGE7PsqgNWEsusGcY2PbjYIecQc2t2yDiNKwT6EcJm8mOezCme/2SsLD+m584neM0YjHu+Bvj1ZKfHocqYs/3CtZoDPq79UkPD+mpf9iH6V8ZtymVWrE/0v0SsRU9w== 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)(376014)(7416014)(1800799024)(23010399003)(82310400026)(10067099003)(261102123227099003)(56012099006)(5023799004)(261026140227099003)(261022075727099003)(261021123027099003)(261021121427099003)(261022105027099003)(11063799006)(261026121527099003)(261028144727099003)(261021135927099003)(261009223027099003)(22082099003)(18002099003)(3023799007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: s9EYE6MbuJxaaoekv1/Nsc+RtFiTpO7N+cPlozMofukNogkG6yLUCuy1ZQHkdeYI9dnZR3UiBWuzY3ayqfTM89wT4bE/KYar40s8uYVlvD12WjrPCmiuzXzQjhnDCOMykhgzYkkPPKlr8CKXicK5uv1/Vl3a97Nemqtxys6tdmMug07fenL3hlpkk5hTv/A9x6wsgmz9vlFCbf+Q3GOD3dImxEQReMqzjkfg4w/uHJmEMo5DAAD6YvB0VdCSDFNSD+C+8XBqez+PhhhkGm03veFIQDvMs1wMDMriRrN3seWARH/hpk8GlDorAMMyLs97PEmOuzeM0eqAu8ZyKMDWVTGDs+JVCufiflvhRxqilEyXuZIqpWmOZVTVqwkNm4IFEb+eL97QtTREurJTLdJo4TtrTL7EX/PmWkdQPVc/Lmjpxsc0IS1XmOInjTsPS4aJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2026 08:38:37.7998 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d76d1dfd-3e44-49ee-9819-08df504fa916 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: PH7PR12MB6858 From: Zhi Wang Stop the GSP plugin, wait for SHUTDOWN completion, and release its firmware resources with CLEANUP before unmapping the communication buffers. Signed-off-by: Zhi Wang Co-developed-by: Alok Kumar Signed-off-by: Alok Kumar Co-developed-by: Alexandre Courbot Signed-off-by: Alexandre Courbot --- drivers/gpu/nova-core/gsp/cmdq/gmc.rs | 1 - drivers/gpu/nova-core/vgpu.rs | 9 +- drivers/gpu/nova-core/vgpu/commands.rs | 66 +++++++++++++ drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs | 1 - drivers/gpu/nova-core/vgpu/instance.rs | 92 +++++++++++++++++-- 5 files changed, 160 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/nova-core/gsp/cmdq/gmc.rs b/drivers/gpu/nova-core/gsp/cmdq/gmc.rs index c9465b20df78..5c0d078204c0 100644 --- a/drivers/gpu/nova-core/gsp/cmdq/gmc.rs +++ b/drivers/gpu/nova-core/gsp/cmdq/gmc.rs @@ -367,7 +367,6 @@ pub(crate) fn send_gmc_command( /// framing validation. /// /// Error codes returned by the command initializer and by `on_event` are propagated as-is. - #[expect(dead_code)] pub(crate) fn send_gmc_command_await_event( &self, command: C, diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs index af257d0f8bc5..1e4c510ea4dd 100644 --- a/drivers/gpu/nova-core/vgpu.rs +++ b/drivers/gpu/nova-core/vgpu.rs @@ -132,7 +132,7 @@ pub(crate) struct VgpuManager<'gpu> /// /// The instances kept in the registry borrow the GPU resources below, which makes this type /// invariant over `'gpu`. -#[pin_data] +#[pin_data(PinnedDrop)] struct VgpuManagerInner<'gpu> { #[pin] instances: Mutex>, @@ -175,3 +175,10 @@ pub(crate) fn new( }) } } + +#[pinned_drop] +impl PinnedDrop for VgpuManagerInner<'_> { + fn drop(self: Pin<&mut Self>) { + self.instances.lock().release_all(&self); + } +} diff --git a/drivers/gpu/nova-core/vgpu/commands.rs b/drivers/gpu/nova-core/vgpu/commands.rs index b384c190a66a..d0fd7ab2593b 100644 --- a/drivers/gpu/nova-core/vgpu/commands.rs +++ b/drivers/gpu/nova-core/vgpu/commands.rs @@ -25,6 +25,7 @@ MessagePayload, // }, Cmdq, + NoReply, // }, nvkv::{ nvkv_words, @@ -35,12 +36,14 @@ GmcCommandId, GSP_PAGE_SIZE, // }, + sbuffer::SBufferIter, vgpu::{ fw::commands::{ AssignedVfVgpuType, VgpuProperties, VgpuPropertiesSchema, // }, + instance::Gfid, Dbdf, }, }; @@ -157,3 +160,66 @@ pub(super) fn send_bootload(_dev: &device::Device, cmdq: &Cmdq<'_>, payload: &[u cmdq.send_gmc_command(BootloadPluginTask(payload), |_| Ok(()))? .map_err(|_| EIO) } + +/// The `SHUTDOWN_GSP_VGPU_PLUGIN_TASK` command. +/// +/// The GSP does not answer it, and sends a `SHUTDOWN_GSP_VGPU_PLUGIN_TASK_COMPLETE` event carrying +/// the same GFID once the task has stopped. +struct ShutdownPluginTask(Gfid); + +impl CommandToGsp for ShutdownPluginTask { + const COMMAND: GmcCommandId = GmcCommandId::ShutdownGspVgpuPluginTask; + type Command = u32; + type Reply = NoReply; + const MAX_REPLY_SIZE: u32 = 0; + const COMPLETION_TIMEOUT: Delta = Delta::from_secs(10); + type InitError = Infallible; + + fn init(&self) -> impl Init { + u32::from(self.0.get()).to_le() + } +} + +/// The `CLEANUP_GSP_VGPU_PLUGIN_RESOURCES` command. +struct CleanupPluginResources(Gfid); + +impl CommandToGsp for CleanupPluginResources { + const COMMAND: GmcCommandId = GmcCommandId::CleanupGspVgpuPluginResources; + type Command = u32; + type Reply = (); + const MAX_REPLY_SIZE: u32 = 0; + type InitError = Infallible; + + fn init(&self) -> impl Init { + u32::from(self.0.get()).to_le() + } +} + +/// Shut down a vGPU plugin task and wait for its completion event. +pub(super) fn send_shutdown(dev: &device::Device, cmdq: &Cmdq<'_>, gfid: Gfid) -> Result { + cmdq.send_gmc_command_await_event(ShutdownPluginTask(gfid), |event| { + let command = event.header().command(); + if !matches!( + command.id(), + Ok(GmcCommandId::ShutdownGspVgpuPluginTaskComplete) + ) { + dev_dbg!(dev, "shutdown: ignoring GMC event {:?}\n", command); + return Ok(None); + } + + let (payload_0, payload_1) = event.payload(); + let mut event_gfid = [0u8; size_of::()]; + SBufferIter::new_reader([payload_0, payload_1]).read_exact(&mut event_gfid)?; + + Ok((u32::from_le_bytes(event_gfid) == u32::from(gfid.get())).then_some(())) + }) +} + +/// Release firmware resources after a plugin task has stopped. +pub(super) fn send_cleanup(dev: &device::Device, cmdq: &Cmdq<'_>, gfid: Gfid) -> Result { + cmdq.send_gmc_command(CleanupPluginResources(gfid), |_| Ok(()))? + .map_err(|_| EIO)?; + + dev_dbg!(dev, "cleanup: gfid={} done\n", gfid.get()); + Ok(()) +} diff --git a/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs b/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs index ec435c026b64..126ba9ad99cf 100644 --- a/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs +++ b/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs @@ -206,7 +206,6 @@ pub(super) fn initialize(&self) -> Result { } /// Invalidate the PTEs and release the communication mapping. - #[expect(dead_code)] pub(super) fn unmap(&mut self) -> Result { self.map.unmap() } diff --git a/drivers/gpu/nova-core/vgpu/instance.rs b/drivers/gpu/nova-core/vgpu/instance.rs index 7a3f29251c4e..892347f073ff 100644 --- a/drivers/gpu/nova-core/vgpu/instance.rs +++ b/drivers/gpu/nova-core/vgpu/instance.rs @@ -30,7 +30,11 @@ }; use super::{ - commands::send_bootload, + commands::{ + send_bootload, + send_cleanup, + send_shutdown, // + }, fw::commands::{ encode_vgpu_bootload, BootloadInfo, @@ -147,10 +151,12 @@ struct VgpuInstance<'pool, 'gpu> { // Unmap the communication region before returning its slot and channel IDs. vram_slot: VgpuVramSlot<'pool>, chids: ChannelIdReservation<'gpu>, + needs_teardown: bool, + /// An uncertain or failed operation retains resources until device removal. + failure: Option, } impl<'gpu> VgpuInstance<'_, 'gpu> { - #[expect(dead_code)] fn activate(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result { let dev = vgpu.dev; self.bootload(dev, vgpu.cmdq, &vgpu.fifo_engine_list)?; @@ -158,6 +164,26 @@ fn activate(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result { Ok(()) } + /// Tear down once, retaining all remaining resources if an operation fails. + fn teardown(&mut self, vgpu: &VgpuManagerInner<'gpu>) -> Result { + if let Some(error) = self.failure { + return Err(error); + } + let result = (|| { + self.shutdown(vgpu.dev, vgpu.cmdq)?; + + if self.needs_teardown { + send_cleanup(vgpu.dev, vgpu.cmdq, self.gfid)?; + self.needs_teardown = false; + } + self.comm.unmap() + })(); + if let Err(error) = result { + self.failure = Some(error); + } + result + } + /// Bootload the GSP vGPU plugin and wait for its BAR1 ready indication. fn bootload( &mut self, @@ -196,6 +222,7 @@ fn bootload( ); self.comm.clear_plugin_ready()?; + self.needs_teardown = true; send_bootload(dev, cmdq, &payload)?; wait_plugin_ready(dev, &self.comm)?; @@ -203,6 +230,15 @@ fn bootload( dev_dbg!(dev, "bootload: gfid={} plugin ready\n", self.gfid.get()); Ok(()) } + + /// Stop the plugin when firmware may own instance resources. + fn shutdown(&mut self, dev: &device::Device, cmdq: &Cmdq<'_>) -> Result { + if self.needs_teardown { + send_shutdown(dev, cmdq, self.gfid)?; + dev_dbg!(dev, "shutdown: gfid={} stopped\n", self.gfid.get()); + } + Ok(()) + } } /// Identity and firmware profile used to allocate an instance. @@ -293,12 +329,19 @@ fn create_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, info: InstanceInfo) .with_project(|this| this.registry.create_instance(vgpu, info)) } - fn destroy_instance(&mut self, gfid: Gfid) -> Result { + fn destroy_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, gfid: Gfid) -> Result { self.pool .as_mut() .ok_or(ENOENT)? .as_mut() - .with_project(|this| this.registry.destroy_instance(gfid)) + .with_project(|this| this.registry.destroy_instance(vgpu, gfid)) + } + + pub(super) fn release_all(&mut self, vgpu: &VgpuManagerInner<'gpu>) { + if let Some(pool) = self.pool.as_mut() { + pool.as_mut() + .with_project(|this| this.registry.release_all(vgpu)); + } } } @@ -369,21 +412,45 @@ fn allocate_instance( comm, vram_slot, chids, + needs_teardown: false, + failure: None, }) } - /// Remove an instance and release its channel and VRAM reservations. - fn destroy_instance(&mut self, gfid: Gfid) -> Result { + /// Remove an instance only after firmware and mapping teardown have succeeded. + fn destroy_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, gfid: Gfid) -> Result { let instance_index = self .instances .iter() .position(|instance| instance.gfid == gfid) .ok_or(ENOENT)?; + let instance = self.instances.get_mut(instance_index).ok_or(EIO)?; + instance.teardown(vgpu)?; + let instance = self.instances.remove(instance_index).map_err(|_| EIO)?; drop(instance); Ok(()) } + /// Release the registry while the manager's MM and firmware dependencies remain available. + pub(super) fn release_all(&mut self, vgpu: &VgpuManagerInner<'gpu>) { + for instance in &mut self.instances { + // Failed operations retain their resources until this final device removal. + // Do not resubmit commands whose firmware outcome was uncertain. + if instance.failure.is_none() { + if let Err(error) = instance.teardown(vgpu) { + dev_err!( + vgpu.dev, + "vGPU teardown failed for gfid={}: {:?}\n", + instance.gfid.get(), + error, + ); + } + } + } + self.instances.clear(); + } + /// Allocate, register, and activate a vGPU instance. /// /// Keep the registry locked through creation or rollback so duplicate checks and @@ -396,6 +463,19 @@ fn create_instance(&mut self, vgpu: &VgpuManagerInner<'gpu>, info: InstanceInfo) let mut instance = self.allocate_instance(vgpu, info)?; let result = instance.activate(vgpu); + if result.is_err() { + match instance.teardown(vgpu) { + // Firmware owns nothing anymore; drop the instance to release its resources. + Ok(()) => return result, + Err(error) => dev_err!( + vgpu.dev, + "vGPU creation could not release gfid={}: {:?}; resources retained\n", + instance.gfid.get(), + error, + ), + } + } + // Register live instances, and failed ones whose resources firmware may still own. self.instances .push_within_capacity(instance)