mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhi Wang <zhiw@nvidia.com>
To: <dakr@kernel.org>, <acourbot@nvidia.com>
Cc: <alex@shazbot.org>, <jgg@nvidia.com>, <yishaih@nvidia.com>,
	<skolothumtho@nvidia.com>, <kevin.tian@intel.com>,
	<airlied@gmail.com>, <simona@ffwll.ch>, <ojeda@kernel.org>,
	<alex.gaynor@gmail.com>, <boqun.feng@gmail.com>,
	<gary@garyguo.net>, <bjorn3_gh@protonmail.com>,
	<lossin@kernel.org>, <a.hindborg@kernel.org>,
	<aliceryhl@google.com>, <tmgross@umich.edu>,
	<jhubbard@nvidia.com>, <ecourtney@nvidia.com>, <cjia@nvidia.com>,
	<smitra@nvidia.com>, <kjaju@nvidia.com>, <alkumar@nvidia.com>,
	<ankita@nvidia.com>, <aniketa@nvidia.com>, <kwankhede@nvidia.com>,
	<targupta@nvidia.com>, <nova-gpu@lists.linux.dev>,
	<linux-kernel@vger.kernel.org>, <zhiwang@kernel.org>,
	Zhi Wang <zhiw@nvidia.com>
Subject: [PATCH v3 22/31] gpu: nova-core: vgpu: add instance shutdown
Date: Mon, 28 Sep 2026 13:28:25 +0300	[thread overview]
Message-ID: <45e62548b38d08aa0411bc5264eaa628fd326e91.1790580105.git.zhiw@nvidia.com> (raw)
In-Reply-To: <cover.1790580105.git.zhiw@nvidia.com>

Stop the GSP plugin, wait for SHUTDOWN completion, and release its
firmware resources with CLEANUP before unmapping the communication
buffers.

Add PendingInstance rollback and manager teardown. Register host resources
before firmware work, and retain them until device removal if firmware
state is uncertain or teardown fails. Keep the manager's dependencies
alive until cleanup finishes.

Co-developed-by: Alok Kumar <alkumar@nvidia.com>
Signed-off-by: Alok Kumar <alkumar@nvidia.com>
Signed-off-by: Zhi Wang <zhiw@nvidia.com>
---
 drivers/gpu/nova-core/gsp/cmdq.rs             |   1 -
 drivers/gpu/nova-core/vgpu.rs                 |   9 +-
 drivers/gpu/nova-core/vgpu/commands.rs        |  55 +++++++-
 drivers/gpu/nova-core/vgpu/fw.rs              |   9 ++
 drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs |   1 -
 drivers/gpu/nova-core/vgpu/instance.rs        | 118 +++++++++++++++++-
 6 files changed, 182 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/nova-core/gsp/cmdq.rs b/drivers/gpu/nova-core/gsp/cmdq.rs
index abd9edadff64..8730fd0079c4 100644
--- a/drivers/gpu/nova-core/gsp/cmdq.rs
+++ b/drivers/gpu/nova-core/gsp/cmdq.rs
@@ -812,7 +812,6 @@ pub(crate) fn send_gmc_and_receive_timeout(
     ///
     /// Errors from initializing the request headers and from either callback are propagated
     /// as-is. The current valid element is consumed before a callback error is returned.
-    #[expect(dead_code)]
     pub(crate) fn send_gmc_and_wait_event(
         &self,
         command_id: u32,
diff --git a/drivers/gpu/nova-core/vgpu.rs b/drivers/gpu/nova-core/vgpu.rs
index 84c4d0b3839d..f7184e7009a5 100644
--- a/drivers/gpu/nova-core/vgpu.rs
+++ b/drivers/gpu/nova-core/vgpu.rs
@@ -105,7 +105,7 @@ fn query_state(
 use self::instance::VgpuInstances;
 
 /// Runtime resources for an enabled vGPU boot.
-#[pin_data]
+#[pin_data(PinnedDrop)]
 pub(crate) struct VgpuManager<'gpu> {
     #[pin]
     instances: Mutex<VgpuInstances<'gpu>>,
@@ -146,3 +146,10 @@ pub(crate) fn new(
         })
     }
 }
+
+#[pinned_drop]
+impl PinnedDrop for VgpuManager<'_> {
+    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 2e9c250b65ab..52c6fc0ee335 100644
--- a/drivers/gpu/nova-core/vgpu/commands.rs
+++ b/drivers/gpu/nova-core/vgpu/commands.rs
@@ -28,9 +28,13 @@
             VgpuPropertiesSchema, //
         },
         GMCAPI_CMD_BOOTLOAD_GSP_VGPU_PLUGIN_TASK,
+        GMCAPI_CMD_CLEANUP_GSP_VGPU_PLUGIN_RESOURCES,
         GMCAPI_CMD_QUERY_ASSIGNED_VF_VGPU_TYPE,
-        GMCAPI_CMD_QUERY_VGPU_PROPERTIES, //
-    }, //
+        GMCAPI_CMD_QUERY_VGPU_PROPERTIES,
+        GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK,
+        GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK_COMPLETE, //
+    },
+    instance::Gfid, //
 };
 
 pub(super) use super::fw::commands::{
@@ -108,3 +112,50 @@ pub(super) fn send_bootload(
     )?;
     check_status(dev, command_id, response.status)
 }
+
+/// Shut down a vGPU plugin task and wait for its completion event.
+pub(super) fn send_shutdown(
+    dev: &device::Device<device::Bound>,
+    cmdq: &Cmdq<'_>,
+    gfid: Gfid,
+) -> Result {
+    let payload = u32::from(gfid.get()).to_le_bytes();
+    cmdq.send_gmc_and_wait_event(
+        GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK,
+        &payload,
+        Delta::from_secs(10),
+        |command_id, _max_response_size, _sequence, payload_0, payload_1| {
+            Ok(
+                command_id == GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK_COMPLETE
+                    && payload
+                        .iter()
+                        .copied()
+                        .eq(Iterator::chain(payload_0.iter(), payload_1)
+                            .take(payload.len())
+                            .copied()),
+            )
+        },
+        |command_id, _max_response_size, _sequence, _payload_0, _payload_1| {
+            dev_dbg!(
+                dev,
+                "shutdown: ignoring unrelated event command={:#x}\n",
+                command_id,
+            );
+            Ok(())
+        },
+    )
+}
+
+/// Release firmware resources after a plugin task has stopped.
+pub(super) fn send_cleanup(
+    dev: &device::Device<device::Bound>,
+    cmdq: &Cmdq<'_>,
+    gfid: Gfid,
+) -> Result {
+    let command_id = GMCAPI_CMD_CLEANUP_GSP_VGPU_PLUGIN_RESOURCES;
+    let response =
+        cmdq.send_gmc_and_receive(command_id, &u32::from(gfid.get()).to_le_bytes(), 0)?;
+    check_status(dev, command_id, response.status)?;
+    dev_dbg!(dev, "cleanup: gfid={} done\n", gfid.get());
+    Ok(())
+}
diff --git a/drivers/gpu/nova-core/vgpu/fw.rs b/drivers/gpu/nova-core/vgpu/fw.rs
index 82c29dfb467a..1528cc56ce74 100644
--- a/drivers/gpu/nova-core/vgpu/fw.rs
+++ b/drivers/gpu/nova-core/vgpu/fw.rs
@@ -33,3 +33,12 @@
 
 pub(super) const GMCAPI_CMD_BOOTLOAD_GSP_VGPU_PLUGIN_TASK: u32 =
     bindings::GMCAPI_COMMANDS_GMCAPI_CMD_BOOTLOAD_GSP_VGPU_PLUGIN_TASK;
+
+pub(super) const GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK: u32 =
+    bindings::GMCAPI_COMMANDS_GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK;
+
+pub(super) const GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK_COMPLETE: u32 =
+    bindings::GMCAPI_COMMANDS_GMCAPI_CMD_SHUTDOWN_GSP_VGPU_PLUGIN_TASK_COMPLETE;
+
+pub(super) const GMCAPI_CMD_CLEANUP_GSP_VGPU_PLUGIN_RESOURCES: u32 =
+    bindings::GMCAPI_COMMANDS_GMCAPI_CMD_CLEANUP_GSP_VGPU_PLUGIN_RESOURCES;
diff --git a/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs b/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs
index 0b4d03c5f0fc..a00f4d2affc7 100644
--- a/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs
+++ b/drivers/gpu/nova-core/vgpu/gsp_plugin_comm.rs
@@ -210,7 +210,6 @@ pub(super) fn is_plugin_ready(&self) -> Result<bool> {
     }
 
     /// 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 b2a0b25c0e15..bf02595590e1 100644
--- a/drivers/gpu/nova-core/vgpu/instance.rs
+++ b/drivers/gpu/nova-core/vgpu/instance.rs
@@ -31,6 +31,8 @@
 use super::{
     commands::{
         send_bootload,
+        send_cleanup,
+        send_shutdown,
         Dbdf, //
     },
     fw::commands::{
@@ -143,10 +145,12 @@ struct VgpuInstance<'gpu> {
     // Unmap the communication region before returning its slot and channel IDs.
     vram_slot: VgpuVramSlot,
     chids: ChannelIdReservation<'gpu>,
+    needs_teardown: bool,
+    /// An uncertain or failed operation retains resources until device removal.
+    failure: Option<Error>,
 }
 
 impl<'gpu> VgpuInstance<'gpu> {
-    #[expect(dead_code)]
     fn activate(&mut self, vgpu: &VgpuManager<'gpu>) -> Result {
         let dev = vgpu.dev;
         self.bootload(dev, vgpu.cmdq, &vgpu.fifo_engine_list)?;
@@ -154,6 +158,26 @@ fn activate(&mut self, vgpu: &VgpuManager<'gpu>) -> Result {
         Ok(())
     }
 
+    /// Tear down once, retaining all remaining resources if an operation fails.
+    fn teardown(&mut self, vgpu: &VgpuManager<'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,
@@ -192,6 +216,7 @@ fn bootload(
         );
 
         self.comm.clear_plugin_ready()?;
+        self.needs_teardown = true;
         send_bootload(dev, cmdq, &payload)?;
 
         wait_plugin_ready(dev, &self.comm)?;
@@ -199,6 +224,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<device::Bound>, 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.
@@ -255,8 +289,12 @@ fn alloc_vram_slot(&mut self, mm: &GpuMm<'_>, layout: VgpuVramLayout) -> Result<
         Ok(slot)
     }
 
-    /// Allocate resources and register a new inactive vGPU instance.
-    fn allocate_instance(&mut self, vgpu: &VgpuManager<'gpu>, info: InstanceInfo) -> Result<Gfid> {
+    /// Register host resources before submitting any firmware work.
+    fn allocate_instance<'a>(
+        &'a mut self,
+        vgpu: &'a VgpuManager<'gpu>,
+        info: InstanceInfo,
+    ) -> Result<PendingInstance<'a, 'gpu>> {
         let InstanceInfo {
             gfid,
             dbdf,
@@ -316,23 +354,91 @@ fn allocate_instance(&mut self, vgpu: &VgpuManager<'gpu>, info: InstanceInfo) ->
             comm,
             vram_slot,
             chids,
+            needs_teardown: false,
+            failure: None,
         };
         self.instances
             .push_within_capacity(instance)
             .map_err(|_| EIO)?;
 
-        Ok(gfid)
+        Ok(PendingInstance {
+            instances: self,
+            vgpu,
+            gfid,
+            committed: false,
+        })
     }
 
-    /// 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: &VgpuManager<'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: &VgpuManager<'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();
+    }
+}
+
+/// Rolls back this creation attempt unless ownership has passed to the caller.
+struct PendingInstance<'a, 'gpu> {
+    instances: &'a mut VgpuInstances<'gpu>,
+    vgpu: &'a VgpuManager<'gpu>,
+    gfid: Gfid,
+    committed: bool,
+}
+
+#[expect(dead_code)]
+impl PendingInstance<'_, '_> {
+    fn activate(mut self) -> Result {
+        let instance = self
+            .instances
+            .instances
+            .iter_mut()
+            .find(|instance| instance.gfid == self.gfid)
+            .ok_or(EIO)?;
+        instance.activate(self.vgpu)?;
+        self.committed = true;
+        Ok(())
+    }
+}
+
+impl Drop for PendingInstance<'_, '_> {
+    fn drop(&mut self) {
+        if self.committed {
+            return;
+        }
+        if let Err(error) = self.instances.destroy_instance(self.vgpu, self.gfid) {
+            dev_err!(
+                self.vgpu.dev,
+                "vGPU creation could not release gfid={}: {:?}; resources retained until removal\n",
+                self.gfid.get(),
+                error,
+            );
+        }
+    }
 }

  parent reply	other threads:[~2026-09-28 10:32 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-28 10:28 [PATCH v3 00/31] Introduce NVIDIA vGPU manager Zhi Wang
2026-09-28 10:28 ` [PATCH v3 01/31] gpu: nova-core: gsp: pass boot context through setup helpers Zhi Wang
2026-09-28 10:28 ` [PATCH v3 02/31] gpu: nova-core: gsp: decouple boot context from VgpuManager Zhi Wang
2026-09-28 10:28 ` [PATCH v3 03/31] gpu: nova-core: vgpu: detect boot state independently Zhi Wang
2026-09-28 10:28 ` [PATCH v3 04/31] gpu: nova-core: gpu: add a channel ID pool for vGPU Zhi Wang
2026-09-28 10:28 ` [PATCH v3 05/31] gpu: nova-core: gsp: decode the FIFO engine table Zhi Wang
2026-09-28 10:28 ` [PATCH v3 06/31] gpu: nova-core: vgpu: initialize runtime parameters after GSP boot Zhi Wang
2026-09-28 10:28 ` [PATCH v3 07/31] gpu: nova-core: vgpu: reserve the 48-VM WPR2 heap Zhi Wang
2026-09-28 10:28 ` [PATCH v3 08/31] gpu: nova-core: mm: borrow BarUser for temporary BAR1 access Zhi Wang
2026-09-28 10:28 ` [PATCH v3 09/31] gpu: nova-core: mm: add VramBlock Zhi Wang
2026-09-28 10:28 ` [PATCH v3 10/31] gpu: nova-core: mm: add VramRegion Zhi Wang
2026-09-28 10:28 ` [PATCH v3 11/31] gpu: nova-core: mm: add BarMapping Zhi Wang
2026-09-28 10:28 ` [PATCH v3 12/31] gpu: nova-core: gsp: add synchronous GMC transactions Zhi Wang
2026-09-28 10:28 ` [PATCH v3 13/31] gpu: nova-core: gsp: wait for GMC completion events Zhi Wang
2026-09-28 10:28 ` [PATCH v3 14/31] gpu: nova-core: vgpu: add r000 plugin bindings Zhi Wang
2026-09-28 10:28 ` [PATCH v3 15/31] gpu: nova-core: vgpu: add VRAM slot allocator Zhi Wang
2026-09-28 10:28 ` [PATCH v3 16/31] gpu: nova-core: gsp: factor out NVKV payload conversion Zhi Wang
2026-09-28 10:28 ` [PATCH v3 17/31] gpu: nova-core: vgpu: query VF assignments and properties Zhi Wang
2026-09-28 10:28 ` [PATCH v3 18/31] gpu: nova-core: vgpu: add instance create/destroy Zhi Wang
2026-09-28 10:28 ` [PATCH v3 19/31] gpu: nova-core: vgpu: encode vGPU boot requests Zhi Wang
2026-09-28 10:28 ` [PATCH v3 20/31] gpu: nova-core: vgpu: add GSP plugin communication buffers Zhi Wang
2026-09-28 10:28 ` [PATCH v3 21/31] gpu: nova-core: vgpu: add instance boot Zhi Wang
2026-09-28 10:28 ` Zhi Wang [this message]
2026-09-28 10:28 ` [PATCH v3 23/31] gpu: nova-core: vgpu: initialize GSP plugin RPC buffers Zhi Wang
2026-09-28 10:28 ` [PATCH v3 24/31] gpu: nova-core: vgpu: add GSP plugin RPC transactions Zhi Wang
2026-09-28 10:28 ` [PATCH v3 25/31] gpu: nova-core: vgpu: negotiate the GSP plugin RPC version Zhi Wang
2026-09-28 10:28 ` [PATCH v3 26/31] gpu: nova-core: vgpu: send GSP plugin configuration parameters Zhi Wang
2026-09-28 10:28 ` [PATCH v3 27/31] gpu: nova-core: vgpu: update the GSP plugin BME state Zhi Wang
2026-09-28 10:28 ` [PATCH v3 28/31] gpu: nova-core: vgpu: add CeUtils commands Zhi Wang
2026-09-28 10:28 ` [PATCH v3 29/31] gpu: nova-core: vgpu: scrub guest VRAM with CeUtils Zhi Wang
2026-09-28 10:28 ` [PATCH v3 30/31] gpu: nova-core: vgpu: export plugin log buffers via debugfs Zhi Wang
2026-09-28 10:28 ` [PATCH v3 31/31] gpu: nova-core: vgpu: introduce SR-IOV PF APIs Zhi Wang

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=45e62548b38d08aa0411bc5264eaa628fd326e91.1790580105.git.zhiw@nvidia.com \
    --to=zhiw@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=alex@shazbot.org \
    --cc=aliceryhl@google.com \
    --cc=alkumar@nvidia.com \
    --cc=aniketa@nvidia.com \
    --cc=ankita@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=cjia@nvidia.com \
    --cc=dakr@kernel.org \
    --cc=ecourtney@nvidia.com \
    --cc=gary@garyguo.net \
    --cc=jgg@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=kevin.tian@intel.com \
    --cc=kjaju@nvidia.com \
    --cc=kwankhede@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nova-gpu@lists.linux.dev \
    --cc=ojeda@kernel.org \
    --cc=simona@ffwll.ch \
    --cc=skolothumtho@nvidia.com \
    --cc=smitra@nvidia.com \
    --cc=targupta@nvidia.com \
    --cc=tmgross@umich.edu \
    --cc=yishaih@nvidia.com \
    --cc=zhiwang@kernel.org \
    /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®