CPU mappings created before a device wedge can keep valid PTEs and
continue accessing VRAM. Mapping new faults to a dummy page does not
replace these existing mappings.

Use the common device I/O SRCU gate to wait for active CPU faults to
finish. Then invalidate all tracked VRAM mappings. Faults starting
after the wedge use the per-BO dummy page.

This ensures userspace is notified only after existing VRAM mappings
have been removed.

Also invalidate existing VRAM CPU mappings during PCI error recovery.
After setting in_reset and draining active faults, remove the mappings
before disabling PCI. Later faults use the dummy page because device
I/O is blocked.

v2:
- Drain active faults before invalidating VRAM mappings

v3:
- Use the common DRM device SRCU gate instead of a separate VRAM fault
  SRCU domain.
- Invalidate existing VRAM CPU mappings before PCI disable during AER.
  (Sashiko)

Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Himal Prasad Ghimiray <[email protected]>
Cc: Rodrigo Vivi <[email protected]>
Assisted-by: LLM
Signed-off-by: Arvind Yadav <[email protected]>
---
 drivers/gpu/drm/xe/xe_bo.c        | 20 ++++++++++++++++++++
 drivers/gpu/drm/xe/xe_bo.h        |  1 +
 drivers/gpu/drm/xe/xe_device.c    |  4 ++++
 drivers/gpu/drm/xe/xe_pci_error.c |  4 ++++
 4 files changed, 29 insertions(+)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index 6efcf3f5dda8..2b3c000b7f94 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -4235,6 +4235,26 @@ void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo 
*bo)
        list_del_init(&bo->vram_userfault_link);
 }
 
+/**
+ * xe_bo_wedged_invalidate_mmaps - Invalidate CPU mappings backed by VRAM
+ * @xe: xe device instance
+ *
+ * The caller must drain the common device I/O gate before calling this
+ * function. Remove all tracked VRAM mappings so later faults map the
+ * per-BO dummy page.
+ */
+void xe_bo_wedged_invalidate_mmaps(struct xe_device *xe)
+{
+       struct xe_bo *bo, *next;
+
+       mutex_lock(&xe->mem_access.vram_userfault.lock);
+       list_for_each_entry_safe(bo, next,
+                                &xe->mem_access.vram_userfault.list,
+                                vram_userfault_link)
+               xe_bo_runtime_pm_release_mmap_offset(bo);
+       mutex_unlock(&xe->mem_access.vram_userfault.lock);
+}
+
 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
 #include "tests/xe_bo.c"
 #endif
diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
index 77b3a02e9b8e..3bd19866819c 100644
--- a/drivers/gpu/drm/xe/xe_bo.h
+++ b/drivers/gpu/drm/xe/xe_bo.h
@@ -453,6 +453,7 @@ int xe_gem_create_ioctl(struct drm_device *dev, void *data,
 int xe_gem_mmap_offset_ioctl(struct drm_device *dev, void *data,
                             struct drm_file *file);
 void xe_bo_runtime_pm_release_mmap_offset(struct xe_bo *bo);
+void xe_bo_wedged_invalidate_mmaps(struct xe_device *xe);
 
 int xe_bo_dumb_create(struct drm_file *file_priv,
                      struct drm_device *dev,
diff --git a/drivers/gpu/drm/xe/xe_device.c b/drivers/gpu/drm/xe/xe_device.c
index 4d4139fa1be4..fa025da253d5 100644
--- a/drivers/gpu/drm/xe/xe_device.c
+++ b/drivers/gpu/drm/xe/xe_device.c
@@ -936,6 +936,10 @@ static void xe_device_wedged_work(struct work_struct *work)
        unsigned long method;
        int err;
 
+       /* Drain active faults before invalidating VRAM mappings. */
+       xe_device_io_drain(xe);
+       xe_bo_wedged_invalidate_mmaps(xe);
+
        /* Report at most one recovery method per worker invocation. */
        method = READ_ONCE(xe->wedged.method);
        if (method != READ_ONCE(xe->wedged.reported_method)) {
diff --git a/drivers/gpu/drm/xe/xe_pci_error.c 
b/drivers/gpu/drm/xe/xe_pci_error.c
index 6606e2457503..ea55ce8114fa 100644
--- a/drivers/gpu/drm/xe/xe_pci_error.c
+++ b/drivers/gpu/drm/xe/xe_pci_error.c
@@ -5,6 +5,7 @@
 
 #include <linux/pci.h>
 
+#include "xe_bo.h"
 #include "xe_device.h"
 #include "xe_ggtt.h"
 #include "xe_gt.h"
@@ -40,6 +41,9 @@ static void prepare_device_for_reset(struct pci_dev *pdev)
         */
        xe_device_io_drain(xe);
 
+       /* Remove existing VRAM CPU mappings before disabling PCI. */
+       xe_bo_wedged_invalidate_mmaps(xe);
+
        pci_disable_device(pdev);
 }
 
-- 
2.43.0

Reply via email to