Skip to contentSkip to conversation

RFC Support hot device unplug in amdgpu

PATCH Superseded Andrey Grodzovsky 8 participants 98 messages Jan 18, 2021 – Feb 25, 2021 1 message not in the archive Page 1 of 2

Subsystems (11)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover RFC Support hot device unplug in amdgpu

Until now extracting a card either by physical extraction (e.g. eGPU with thunderbolt connection or by emulation through syfs -> /sys/bus/pci/devices/device_id/remove) would cause random crashes in user apps. The random crashes in apps were
mostly due to the app having mapped a device backed BO into its address
space was still trying to access the BO while the backing device was gone.
To answer this first problem Christian suggested to fix the handling of mapped memory in the clients when the device goes away by forcibly unmap all buffers the user processes has by clearing their respective VMAs mapping the device BOs. Then when the VMAs try to fill in the page tables again we check in the fault handlerif the device is removed and if so, return an error. This will generate a SIGBUS to the application which can then cleanly terminate.This indeed was done but this in turn created a problem of kernel OOPs were the OOPSes were due to the fact that while the app was terminating because of the SIGBUSit would trigger use after free in the driver by calling to accesses device structures that were already released from the pci remove sequence.This was handled by introducing a 'flush' sequence during device removal were we wait for drm file reference to drop to 0 meaning all user clients directly using this device terminated.

v2:
Based on discussions in the mailing list with Daniel and Pekka [1] and based on the document
produced by Pekka from those discussions [2] the whole approach with returning SIGBUS and
waiting for all user clients having CPU mapping of device BOs to die was dropped.
Instead as per the document suggestion the device structures are kept alive until
the last reference to the device is dropped by user client and in the meanwhile all existing and new CPU mappings of the BOs belonging to the device directly or by dma-buf import are rerouted to per user
process dummy rw page.Also, I skipped the 'Requirements for KMS UAPI' section of [2]
since i am trying to get the minimal set of requirements that still give useful solution
to work and this is the'Requirements for Render and Cross-Device UAPI' section and so my
test case is removing a secondary device, which is render only and is not involved
in KMS.

v3:
More updates following comments from v2 such as removing loop to find DRM file when rerouting page faults to dummy page,getting rid of unnecessary sysfs handling refactoring and moving prevention of GPU recovery post device unplug from amdgpu to scheduler layer. On top of that added unplug support for the IOMMU enabled system.

v4:
Drop last sysfs hack and use sysfs default attribute.
Guard against write accesses after device removal to avoid modifying released memory. Update dummy pages handling to on demand allocation and release through drm managed framework. Add return value to scheduler job TO handler (by Luben Tuikov) and use this in amdgpu for prevention of GPU recovery post device unplug
Also rebase on top of drm-misc-mext instead of amd-staging-drm-next

With these patches I am able to gracefully remove the secondary card using sysfs remove hook while glxgears is running off of secondary card (DRI_PRIME=1) without kernel oopses or hangs and keep working with the primary card or soft reset the device without hangs or oopses

TODOs for followup work:
Convert AMDGPU code to use devm (for hw stuff) and drmm (for sw stuff and allocations) (Daniel) Support plugging the secondary device back after unplug - currently still experiencing HW error on plugging back. Add support for 'Requirements for KMS UAPI' section of [2] - unplugging primary, display connected card.

[1] - Discussions during v3 of the patchset https://www.spinics.net/lists/amd-gfx/msg55576.html
[2] - drm/doc: device hot-unplug for userspace https://www.spinics.net/lists/dri-devel/msg259755.html
[3] - Related gitlab ticket https://gitlab.freedesktop.org/drm/amd/-/issues/1081

Andrey Grodzovsky (13):
drm/ttm: Remap all page faults to per process dummy page.
drm: Unamp the entire device address space on device unplug drm/ttm: Expose ttm_tt_unpopulate for driver use drm/sched: Cancel and flush all oustatdning jobs before finish. drm/amdgpu: Split amdgpu_device_fini into early and late drm/amdgpu: Add early fini callback
drm/amdgpu: Register IOMMU topology notifier per device. drm/amdgpu: Fix a bunch of sdma code crash post device unplug drm/amdgpu: Remap all page faults to per process dummy page. dmr/amdgpu: Move some sysfs attrs creation to default_attr drm/amdgpu: Guard against write accesses after device removal drm/sched: Make timeout timer rearm conditional. drm/amdgpu: Prevent any job recoveries after device is unplugged.

Luben Tuikov (1):
drm/scheduler: Job timeout handler returns status

 drivers/gpu/drm/amd/amdgpu/amdgpu.h               |  11 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c      |  17 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c        | 149 ++++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c           |  20 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c         |  15 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c          |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h          |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c           |   9 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c       |  25 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c           |  26 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.h           |   3 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_job.c           |  19 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c           |  12 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c        |  10 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h        |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c           |  53 +++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h           |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c           |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c          |  70 ++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h          |  52 +-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c           |  21 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c            |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c      |  14 +-
 drivers/gpu/drm/amd/amdgpu/cik_ih.c               |   2 +-
 drivers/gpu/drm/amd/amdgpu/cz_ih.c                |   2 +-
 drivers/gpu/drm/amd/amdgpu/iceland_ih.c           |   2 +-
 drivers/gpu/drm/amd/amdgpu/navi10_ih.c            |   2 +-
 drivers/gpu/drm/amd/amdgpu/psp_v11_0.c            |  16 +--
 drivers/gpu/drm/amd/amdgpu/psp_v12_0.c            |   8 +-
 drivers/gpu/drm/amd/amdgpu/psp_v3_1.c             |   8 +-
 drivers/gpu/drm/amd/amdgpu/si_ih.c                |   2 +-
 drivers/gpu/drm/amd/amdgpu/tonga_ih.c             |   2 +-
 drivers/gpu/drm/amd/amdgpu/vega10_ih.c            |   2 +-
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c |  12 +-
 drivers/gpu/drm/amd/include/amd_shared.h          |   2 +
 drivers/gpu/drm/drm_drv.c                         |   3 +
 drivers/gpu/drm/etnaviv/etnaviv_sched.c           |  10 +-
 drivers/gpu/drm/lima/lima_sched.c                 |   4 +-
 drivers/gpu/drm/panfrost/panfrost_job.c           |   9 +-
 drivers/gpu/drm/scheduler/sched_main.c            |  18 ++-
 drivers/gpu/drm/ttm/ttm_bo_vm.c                   |  82 +++++++++++-
 drivers/gpu/drm/ttm/ttm_tt.c                      |   1 +
 drivers/gpu/drm/v3d/v3d_sched.c                   |  32 ++---
 include/drm/gpu_scheduler.h                       |  17 ++-
 include/drm/ttm/ttm_bo_api.h                      |   2 +
 45 files changed, 583 insertions(+), 198 deletions(-)
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#1 lore raw

PATCH v4 1/14 drm/ttm: Remap all page faults to per process dummy page.

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
 drivers/gpu/drm/ttm/ttm_bo_vm.c | 82 ++++++++++++++++++++++++++++++++++++++++-
 include/drm/ttm/ttm_bo_api.h    |  2 +
 2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +81 -1
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
 #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
 #include <linux/mm.h>
 #include <linux/pfn_t.h>
 #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
 }
 EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
 
+static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+	struct page *dummy_page = (struct page *)res;
+
+	__free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct ttm_bo_device *bdev = bo->bdev;
+	struct drm_device *ddev = bo->base.dev;
+	vm_fault_t ret = VM_FAULT_NOPAGE;
+	unsigned long address = vma->vm_start;
+	unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+	unsigned long pfn;
+	struct page *page;
+	int i;
+
+	/*
+	 * Wait for buffer data in transit, due to a pipelined
+	 * move.
+	 */
+	ret = ttm_bo_vm_fault_idle(bo, vmf);
+	if (unlikely(ret != 0))
+		return ret;
+
+	/* Allocate new dummy page to map all the VA range in this VMA to it*/
+	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!page)
+		return VM_FAULT_OOM;
+
+	pfn = page_to_pfn(page);
+
+	/*
+	 * Prefault the entire VMA range right away to avoid further faults
+	 */
+	for (i = 0; i < num_prefault; ++i) {
+
+		if (unlikely(address >= vma->vm_end))
+			break;
+
+		if (vma->vm_flags & VM_MIXEDMAP)
+			ret = vmf_insert_mixed_prot(vma, address,
+						    __pfn_to_pfn_t(pfn, PFN_DEV),
+						    prot);
+		else
+			ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+		/* Never error on prefaulted PTEs */
+		if (unlikely((ret & VM_FAULT_ERROR))) {
+			if (i == 0)
+				return VM_FAULT_NOPAGE;
+			else
+				break;
+		}
+
+		address += PAGE_SIZE;
+	}
+
+	/* Set the page to be freed using drmm release action */
+	if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+		return VM_FAULT_OOM;
+
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);
+
 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	pgprot_t prot;
 	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct drm_device *ddev = bo->base.dev;
 	vm_fault_t ret;
+	int idx;
 
 	ret = ttm_bo_vm_reserve(bo, vmf);
 	if (ret)
 		return ret;
 
 	prot = vma->vm_page_prot;
-	ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+	if (drm_dev_enter(ddev, &idx)) {
+		ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+		drm_dev_exit(idx);
+	} else {
+		ret = ttm_bo_vm_dummy_page(vmf, prot);
+	}
 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
 		return ret;
 
 	dma_resv_unlock(bo->base.resv);
 
 	return ret;
+
+	return ret;
 }
 EXPORT_SYMBOL(ttm_bo_vm_fault);
 
include/drm/ttm/ttm_bo_api.h +2 -0
index e17be32..12fb240 100644
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
 		     void *buf, int len, int write);
 
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
 #endif
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [PATCH v4 01/14] drm/ttm: Remap all page faults to per process dummy page.

Quoted 123 lines · Andrey Grodzovsky

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
 drivers/gpu/drm/ttm/ttm_bo_vm.c | 82 ++++++++++++++++++++++++++++++++++++++++-
 include/drm/ttm/ttm_bo_api.h    |  2 +
 2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +77 -1
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
 #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
 #include <linux/mm.h>
 #include <linux/pfn_t.h>
 #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
 }
 EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
 
+static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+       struct page *dummy_page = (struct page *)res;
+
+       __free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+       struct vm_area_struct *vma = vmf->vma;
+       struct ttm_buffer_object *bo = vma->vm_private_data;
+       struct ttm_bo_device *bdev = bo->bdev;
+       struct drm_device *ddev = bo->base.dev;
+       vm_fault_t ret = VM_FAULT_NOPAGE;
+       unsigned long address = vma->vm_start;
+       unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+       unsigned long pfn;
+       struct page *page;
+       int i;
+
+       /*
+        * Wait for buffer data in transit, due to a pipelined
+        * move.
+        */
+       ret = ttm_bo_vm_fault_idle(bo, vmf);
+       if (unlikely(ret != 0))
+               return ret;
+
+       /* Allocate new dummy page to map all the VA range in this VMA to it*/
+       page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+       if (!page)
+               return VM_FAULT_OOM;
+
+       pfn = page_to_pfn(page);
+
+       /*
+        * Prefault the entire VMA range right away to avoid further faults
+        */
+       for (i = 0; i < num_prefault; ++i) {
+
+               if (unlikely(address >= vma->vm_end))
+                       break;
+
+               if (vma->vm_flags & VM_MIXEDMAP)
+                       ret = vmf_insert_mixed_prot(vma, address,
+                                                   __pfn_to_pfn_t(pfn, PFN_DEV),
+                                                   prot);
+               else
+                       ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+               /* Never error on prefaulted PTEs */
+               if (unlikely((ret & VM_FAULT_ERROR))) {
+                       if (i == 0)
+                               return VM_FAULT_NOPAGE;
+                       else
+                               break;
+               }
+
+               address += PAGE_SIZE;
+       }
+
+       /* Set the page to be freed using drmm release action */
+       if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+               return VM_FAULT_OOM;
+
+       return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);
+
 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
 {
        struct vm_area_struct *vma = vmf->vma;
        pgprot_t prot;
        struct ttm_buffer_object *bo = vma->vm_private_data;
+       struct drm_device *ddev = bo->base.dev;
        vm_fault_t ret;
+       int idx;
 
        ret = ttm_bo_vm_reserve(bo, vmf);
        if (ret)
                return ret;
 
        prot = vma->vm_page_prot;
-       ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+       if (drm_dev_enter(ddev, &idx)) {
+               ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+               drm_dev_exit(idx);
+       } else {

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 481

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +4 -0
+               ret = ttm_bo_vm_dummy_page(vmf, prot);
+       }
        if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
                return ret;
 
        dma_resv_unlock(bo->base.resv);
 
        return ret;
+
+       return ret;

Duplicate return here.

Alex

Quoted 17 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +0 -0
 }
 EXPORT_SYMBOL(ttm_bo_vm_fault);
 
include/drm/ttm/ttm_bo_api.h +2 -1
index e17be32..12fb240 100644
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
                     void *buf, int len, int write);
 
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
 #endif
--

2.7.4

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#3 lore raw

Re: [PATCH v4 01/14] drm/ttm: Remap all page faults to per process dummy page.

In reply to Andrey Grodzovsky

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 51 lines

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 82 ++++++++++++++++++++++++++++++++++++++++-
  include/drm/ttm/ttm_bo_api.h    |  2 +
  2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +19 -0
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
  #include <drm/ttm/ttm_bo_driver.h>
  #include <drm/ttm/ttm_placement.h>
  #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
  #include <linux/mm.h>
  #include <linux/pfn_t.h>
  #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
  }
  EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
 
+static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+	struct page *dummy_page = (struct page *)res;
+
+	__free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct ttm_bo_device *bdev = bo->bdev;
+	struct drm_device *ddev = bo->base.dev;
+	vm_fault_t ret = VM_FAULT_NOPAGE;
+	unsigned long address = vma->vm_start;
+	unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+	unsigned long pfn;

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 411

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +10 -0
+	struct page *page;
+	int i;
+
+	/*
+	 * Wait for buffer data in transit, due to a pipelined
+	 * move.
+	 */
+	ret = ttm_bo_vm_fault_idle(bo, vmf);
+	if (unlikely(ret != 0))
+		return ret;

This is superfluous and probably quite harmful here because we wait for the hardware to do something.

We map a dummy page instead of the real BO content to the whole range anyway, so no need to wait for the real BO content to show up.

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 423

Quoted 12 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +12 -0
+
+	/* Allocate new dummy page to map all the VA range in this VMA to it*/
+	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!page)
+		return VM_FAULT_OOM;
+
+	pfn = page_to_pfn(page);
+
+	/*
+	 * Prefault the entire VMA range right away to avoid further faults
+	 */
+	for (i = 0; i < num_prefault; ++i) {

Maybe rename the variable to num_pages. I was confused for a moment why we still prefault.

Alternative you can just drop i and do "for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)".

Quoted 7 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +7 -0
+
+		if (unlikely(address >= vma->vm_end))
+			break;
+
+		if (vma->vm_flags & VM_MIXEDMAP)
+			ret = vmf_insert_mixed_prot(vma, address,
+						    __pfn_to_pfn_t(pfn, PFN_DEV),

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 440

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +10 -0
+						    prot);
+		else
+			ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+		/* Never error on prefaulted PTEs */
+		if (unlikely((ret & VM_FAULT_ERROR))) {
+			if (i == 0)
+				return VM_FAULT_NOPAGE;
+			else
+				break;

This should probably be modified to either always return the error or always ignore it.

Apart from that looks good to me.

Christian.

Quoted 50 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +23 -1
+		}
+
+		address += PAGE_SIZE;
+	}
+
+	/* Set the page to be freed using drmm release action */
+	if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+		return VM_FAULT_OOM;
+
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);
+
  vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
  {
  	struct vm_area_struct *vma = vmf->vma;
  	pgprot_t prot;
  	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct drm_device *ddev = bo->base.dev;
  	vm_fault_t ret;
+	int idx;
 
  	ret = ttm_bo_vm_reserve(bo, vmf);
  	if (ret)
  		return ret;
 
  	prot = vma->vm_page_prot;
-	ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+	if (drm_dev_enter(ddev, &idx)) {
+		ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+		drm_dev_exit(idx);
+	} else {
+		ret = ttm_bo_vm_dummy_page(vmf, prot);
+	}
  	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
  		return ret;
 
  	dma_resv_unlock(bo->base.resv);
 
  	return ret;
+
+	return ret;
  }
  EXPORT_SYMBOL(ttm_bo_vm_fault);
 
include/drm/ttm/ttm_bo_api.h +0 -0
index e17be32..12fb240 100644

include/drm/ttm/ttm_bo_api.h void ttm_bo_vm_close(struct vm_area_struct *vma); · line 648

Quoted 6 lines
include/drm/ttm/ttm_bo_api.h +2 -0
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
  int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  		     void *buf, int len, int write);
 
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
  #endif

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw

Re: [PATCH v4 01/14] drm/ttm: Remap all page faults to per process dummy page.

In reply to Andrey Grodzovsky
Quoted 92 lines · Andrey Grodzovsky

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
 drivers/gpu/drm/ttm/ttm_bo_vm.c | 82 ++++++++++++++++++++++++++++++++++++++++-
 include/drm/ttm/ttm_bo_api.h    |  2 +
 2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +60 -0
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
 #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
 #include <linux/mm.h>
 #include <linux/pfn_t.h>
 #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
 }
 EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
 
+static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+	struct page *dummy_page = (struct page *)res;
+
+	__free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct ttm_bo_device *bdev = bo->bdev;
+	struct drm_device *ddev = bo->base.dev;
+	vm_fault_t ret = VM_FAULT_NOPAGE;
+	unsigned long address = vma->vm_start;
+	unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+	unsigned long pfn;
+	struct page *page;
+	int i;
+
+	/*
+	 * Wait for buffer data in transit, due to a pipelined
+	 * move.
+	 */
+	ret = ttm_bo_vm_fault_idle(bo, vmf);
+	if (unlikely(ret != 0))
+		return ret;
+
+	/* Allocate new dummy page to map all the VA range in this VMA to it*/
+	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!page)
+		return VM_FAULT_OOM;
+
+	pfn = page_to_pfn(page);
+
+	/*
+	 * Prefault the entire VMA range right away to avoid further faults
+	 */
+	for (i = 0; i < num_prefault; ++i) {
+
+		if (unlikely(address >= vma->vm_end))
+			break;
+
+		if (vma->vm_flags & VM_MIXEDMAP)
+			ret = vmf_insert_mixed_prot(vma, address,
+						    __pfn_to_pfn_t(pfn, PFN_DEV),
+						    prot);
+		else
+			ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+		/* Never error on prefaulted PTEs */
+		if (unlikely((ret & VM_FAULT_ERROR))) {
+			if (i == 0)
+				return VM_FAULT_NOPAGE;
+			else
+				break;
+		}
+

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 452

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +10 -0
+		address += PAGE_SIZE;
+	}
+
+	/* Set the page to be freed using drmm release action */
+	if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+		return VM_FAULT_OOM;
+
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);

I think we can lift this entire thing (once the ttm_bo_vm_fault_idle is gone) to the drm level, since nothing ttm specific in here. Probably stuff it into drm_gem.c (but really it's not even gem specific, it's fully generic "replace this vma with dummy pages pls" function.

Aside from this nit I think the overall approach you have here is starting to look good. Lots of work&polish, but imo we're getting there and can start landing stuff soon.
-Daniel

Quoted 47 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +11 -1
+
 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
 {
 	struct vm_area_struct *vma = vmf->vma;
 	pgprot_t prot;
 	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct drm_device *ddev = bo->base.dev;
 	vm_fault_t ret;
+	int idx;
 
 	ret = ttm_bo_vm_reserve(bo, vmf);
 	if (ret)
 		return ret;
 
 	prot = vma->vm_page_prot;
-	ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+	if (drm_dev_enter(ddev, &idx)) {
+		ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+		drm_dev_exit(idx);
+	} else {
+		ret = ttm_bo_vm_dummy_page(vmf, prot);
+	}
 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
 		return ret;
 
 	dma_resv_unlock(bo->base.resv);
 
 	return ret;
+
+	return ret;
 }
 EXPORT_SYMBOL(ttm_bo_vm_fault);
 
include/drm/ttm/ttm_bo_api.h +2 -0
index e17be32..12fb240 100644
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
 		     void *buf, int len, int write);
 
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
 #endif
Signature · 1 line
2.7.4
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

Re: [PATCH v4 01/14] drm/ttm: Remap all page faults to per process dummy page.

Quoted 99 lines · Daniel Vetter
Quoted 98 lines · Andrey Grodzovsky

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 82 ++++++++++++++++++++++++++++++++++++++++-
  include/drm/ttm/ttm_bo_api.h    |  2 +
  2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +66 -0
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
  #include <drm/ttm/ttm_bo_driver.h>
  #include <drm/ttm/ttm_placement.h>
  #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
  #include <linux/mm.h>
  #include <linux/pfn_t.h>
  #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
  }
  EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
 
+static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+	struct page *dummy_page = (struct page *)res;
+
+	__free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+	struct vm_area_struct *vma = vmf->vma;
+	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct ttm_bo_device *bdev = bo->bdev;
+	struct drm_device *ddev = bo->base.dev;
+	vm_fault_t ret = VM_FAULT_NOPAGE;
+	unsigned long address = vma->vm_start;
+	unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+	unsigned long pfn;
+	struct page *page;
+	int i;
+
+	/*
+	 * Wait for buffer data in transit, due to a pipelined
+	 * move.
+	 */
+	ret = ttm_bo_vm_fault_idle(bo, vmf);
+	if (unlikely(ret != 0))
+		return ret;
+
+	/* Allocate new dummy page to map all the VA range in this VMA to it*/
+	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	if (!page)
+		return VM_FAULT_OOM;
+
+	pfn = page_to_pfn(page);
+
+	/*
+	 * Prefault the entire VMA range right away to avoid further faults
+	 */
+	for (i = 0; i < num_prefault; ++i) {
+
+		if (unlikely(address >= vma->vm_end))
+			break;
+
+		if (vma->vm_flags & VM_MIXEDMAP)
+			ret = vmf_insert_mixed_prot(vma, address,
+						    __pfn_to_pfn_t(pfn, PFN_DEV),
+						    prot);
+		else
+			ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+		/* Never error on prefaulted PTEs */
+		if (unlikely((ret & VM_FAULT_ERROR))) {
+			if (i == 0)
+				return VM_FAULT_NOPAGE;
+			else
+				break;
+		}
+
+		address += PAGE_SIZE;
+	}
+
+	/* Set the page to be freed using drmm release action */
+	if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+		return VM_FAULT_OOM;
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 452

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +4 -0
+
+	return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);

I think we can lift this entire thing (once the ttm_bo_vm_fault_idle is gone) to the drm level, since nothing ttm specific in here. Probably stuff it into drm_gem.c (but really it's not even gem specific, it's fully generic "replace this vma with dummy pages pls" function.

Once I started with this I noticed that drmm_add_action_or_reset depends on struct drm_device *ddev = bo->base.dev  and bo is the private data we embed at the TTM level when setting up the mapping and so this forces to move drmm_add_action_or_reset out of this function to every client who uses this function, and then you separate the logic of page allocation from it's release. So I suggest we keep it as is.

Andrey

Quoted 47 lines

Aside from this nit I think the overall approach you have here is starting to look good. Lots of work&polish, but imo we're getting there and can start landing stuff soon.
-Daniel

Quoted 41 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +11 -1
+
  vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
  {
  	struct vm_area_struct *vma = vmf->vma;
  	pgprot_t prot;
  	struct ttm_buffer_object *bo = vma->vm_private_data;
+	struct drm_device *ddev = bo->base.dev;
  	vm_fault_t ret;
+	int idx;
 
  	ret = ttm_bo_vm_reserve(bo, vmf);
  	if (ret)
  		return ret;
 
  	prot = vma->vm_page_prot;
-	ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+	if (drm_dev_enter(ddev, &idx)) {
+		ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+		drm_dev_exit(idx);
+	} else {
+		ret = ttm_bo_vm_dummy_page(vmf, prot);
+	}
  	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
  		return ret;
 
  	dma_resv_unlock(bo->base.resv);
 
  	return ret;
+
+	return ret;
  }
  EXPORT_SYMBOL(ttm_bo_vm_fault);
 
include/drm/ttm/ttm_bo_api.h +0 -0
index e17be32..12fb240 100644
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
  int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  		     void *buf, int len, int write);
 
Quoted 6 lines
Quoted 6 lines
include/drm/ttm/ttm_bo_api.h +2 -0
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
  #endif
Signature · 1 line
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#6 lore raw

Re: [PATCH v4 01/14] drm/ttm: Remap all page faults to per process dummy page.

Hey Daniel, just a ping.

Andrey

Quoted 161 lines · Andrey Grodzovsky
Quoted 100 lines · Daniel Vetter
Quoted 99 lines · Andrey Grodzovsky

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 82
++++++++++++++++++++++++++++++++++++++++-
  include/drm/ttm/ttm_bo_api.h    |  2 +

  2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +65 -0
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
   #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
   #include <linux/mm.h>
   #include <linux/pfn_t.h>
   #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault 
 *vmf,
   }
   EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
   +static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+    struct page *dummy_page = (struct page *)res;
+
+    __free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+    struct vm_area_struct *vma = vmf->vma;
+    struct ttm_buffer_object *bo = vma->vm_private_data;
+    struct ttm_bo_device *bdev = bo->bdev;
+    struct drm_device *ddev = bo->base.dev;
+    vm_fault_t ret = VM_FAULT_NOPAGE;
+    unsigned long address = vma->vm_start;
+    unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+    unsigned long pfn;
+    struct page *page;
+    int i;
+
+    /*
+     * Wait for buffer data in transit, due to a pipelined
+     * move.
+     */
+    ret = ttm_bo_vm_fault_idle(bo, vmf);
+    if (unlikely(ret != 0))
+        return ret;
+
+    /* Allocate new dummy page to map all the VA range in this VMA to it*/
+    page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+    if (!page)
+        return VM_FAULT_OOM;
+
+    pfn = page_to_pfn(page);
+
+    /*
+     * Prefault the entire VMA range right away to avoid further faults
+     */
+    for (i = 0; i < num_prefault; ++i) {
+
+        if (unlikely(address >= vma->vm_end))
+            break;
+
+        if (vma->vm_flags & VM_MIXEDMAP)
+            ret = vmf_insert_mixed_prot(vma, address,
+                            __pfn_to_pfn_t(pfn, PFN_DEV),
+                            prot);
+        else
+            ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+        /* Never error on prefaulted PTEs */
+        if (unlikely((ret & VM_FAULT_ERROR))) {
+            if (i == 0)
+                return VM_FAULT_NOPAGE;
+            else
+                break;
+        }
+
+        address += PAGE_SIZE;
+    }
+
+    /* Set the page to be freed using drmm release action */
+    if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+        return VM_FAULT_OOM;
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 452

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +4 -0
+
+    return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);

I think we can lift this entire thing (once the ttm_bo_vm_fault_idle is gone) to the drm level, since nothing ttm specific in here. Probably stuff it into drm_gem.c (but really it's not even gem specific, it's fully generic "replace this vma with dummy pages pls" function.

Once I started with this I noticed that drmm_add_action_or_reset depends on struct drm_device *ddev = bo->base.dev  and bo is the private data we embed at the TTM level when setting up the mapping and so this forces to move drmm_add_action_or_reset out of this function to every client who uses this function, and then you separate the logic of page allocation from it's release. So I suggest we keep it as is.

Andrey

Quoted 39 lines

Aside from this nit I think the overall approach you have here is starting to look good. Lots of work&polish, but imo we're getting there and can start landing stuff soon.
-Daniel

Quoted 33 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +11 -1
+
   vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
   {
       struct vm_area_struct *vma = vmf->vma;
       pgprot_t prot;
       struct ttm_buffer_object *bo = vma->vm_private_data;
+    struct drm_device *ddev = bo->base.dev;
       vm_fault_t ret;
+    int idx;
         ret = ttm_bo_vm_reserve(bo, vmf);
       if (ret)
           return ret;
         prot = vma->vm_page_prot;
-    ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+    if (drm_dev_enter(ddev, &idx)) {
+        ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+        drm_dev_exit(idx);
+    } else {
+        ret = ttm_bo_vm_dummy_page(vmf, prot);
+    }
       if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
           return ret;
         dma_resv_unlock(bo->base.resv);
         return ret;
+
+    return ret;
   }
   EXPORT_SYMBOL(ttm_bo_vm_fault);

  diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h index e17be32..12fb240 100644

include/drm/ttm/ttm_bo_api.h +0 -0
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
include/drm/ttm/ttm_bo_api.h +1 -0
   int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
                void *buf, int len, int write);
   +vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
   #endif
Signature · 1 line
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

Re: [PATCH v4 01/14] drm/ttm: Remap all page faults to per process dummy page.

Quoted 1 line · Andrey Grodzovsky

Hey Daniel, just a ping.

Was on vacations last week.

Quoted 122 lines

Andrey

Quoted 119 lines · Andrey Grodzovsky
Quoted 100 lines · Daniel Vetter
Quoted 99 lines · Andrey Grodzovsky

On device removal reroute all CPU mappings to dummy page.

v3:
Remove loop to find DRM file and instead access it by vma->vm_file->private_data. Move dummy page installation into a separate function.

v4:
Map the entire BOs VA space into on demand allocated dummy page on the first fault for that BO.

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c | 82
++++++++++++++++++++++++++++++++++++++++-
  include/drm/ttm/ttm_bo_api.h    |  2 +

  2 files changed, 83 insertions(+), 1 deletion(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +65 -0
index 6dc96cf..ed89da3 100644
@@ -34,6 +34,8 @@
   #include <drm/ttm/ttm_bo_driver.h>
   #include <drm/ttm/ttm_placement.h>
   #include <drm/drm_vma_manager.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_managed.h>
   #include <linux/mm.h>
   #include <linux/pfn_t.h>
   #include <linux/rbtree.h>
@@ -380,25 +382,103 @@ vm_fault_t
 ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
   }
   EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
   +static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
+{
+    struct page *dummy_page = (struct page *)res;
+
+    __free_page(dummy_page);
+}
+
+vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
+{
+    struct vm_area_struct *vma = vmf->vma;
+    struct ttm_buffer_object *bo = vma->vm_private_data;
+    struct ttm_bo_device *bdev = bo->bdev;
+    struct drm_device *ddev = bo->base.dev;
+    vm_fault_t ret = VM_FAULT_NOPAGE;
+    unsigned long address = vma->vm_start;
+    unsigned long num_prefault = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+    unsigned long pfn;
+    struct page *page;
+    int i;
+
+    /*
+     * Wait for buffer data in transit, due to a pipelined
+     * move.
+     */
+    ret = ttm_bo_vm_fault_idle(bo, vmf);
+    if (unlikely(ret != 0))
+        return ret;
+
+    /* Allocate new dummy page to map all the VA range in this VMA to it*/
+    page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+    if (!page)
+        return VM_FAULT_OOM;
+
+    pfn = page_to_pfn(page);
+
+    /*
+     * Prefault the entire VMA range right away to avoid further faults
+     */
+    for (i = 0; i < num_prefault; ++i) {
+
+        if (unlikely(address >= vma->vm_end))
+            break;
+
+        if (vma->vm_flags & VM_MIXEDMAP)
+            ret = vmf_insert_mixed_prot(vma, address,
+                            __pfn_to_pfn_t(pfn, PFN_DEV),
+                            prot);
+        else
+            ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
+
+        /* Never error on prefaulted PTEs */
+        if (unlikely((ret & VM_FAULT_ERROR))) {
+            if (i == 0)
+                return VM_FAULT_NOPAGE;
+            else
+                break;
+        }
+
+        address += PAGE_SIZE;
+    }
+
+    /* Set the page to be freed using drmm release action */
+    if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
+        return VM_FAULT_OOM;
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_bo_vm.c vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf, · line 452

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +4 -0
+
+    return ret;
+}
+EXPORT_SYMBOL(ttm_bo_vm_dummy_page);

I think we can lift this entire thing (once the ttm_bo_vm_fault_idle is gone) to the drm level, since nothing ttm specific in here. Probably stuff it into drm_gem.c (but really it's not even gem specific, it's fully generic "replace this vma with dummy pages pls" function.

Once I started with this I noticed that drmm_add_action_or_reset depends on struct drm_device *ddev = bo->base.dev  and bo is the private data we embed at the TTM level when setting up the mapping and so this forces to move drmm_add_action_or_reset out of this function to every client who uses this function, and then you separate the logic of page allocation from it's release.
So I suggest we keep it as is.

Uh disappointing. Thing is, ttm essentially means drm devices with gem, except for vmwgfx, which is a drm_device without gem. And I think one of the remaining ttm refactors in this area is to move ttm_device over into drm_device someone, and then we'd have bo->base.dev always set to something that drmm_add_action_or_reset can use.

I guess hand-rolling for now and jotting this down as a TODO item is fine too, but would be good to get this addressed since that's another reason here to do this. Maybe sync with Christian how to best do this. -Daniel

Quoted 51 lines
Quoted 51 lines

Andrey

Quoted 47 lines

Aside from this nit I think the overall approach you have here is starting to look good. Lots of work&polish, but imo we're getting there and can start landing stuff soon.
-Daniel

Quoted 41 lines
drivers/gpu/drm/ttm/ttm_bo_vm.c +11 -1
+
   vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
   {
       struct vm_area_struct *vma = vmf->vma;
       pgprot_t prot;
       struct ttm_buffer_object *bo = vma->vm_private_data;
+    struct drm_device *ddev = bo->base.dev;
       vm_fault_t ret;
+    int idx;
         ret = ttm_bo_vm_reserve(bo, vmf);
       if (ret)
           return ret;
         prot = vma->vm_page_prot;
-    ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+    if (drm_dev_enter(ddev, &idx)) {
+        ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT, 1);
+        drm_dev_exit(idx);
+    } else {
+        ret = ttm_bo_vm_dummy_page(vmf, prot);
+    }
       if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
           return ret;
         dma_resv_unlock(bo->base.resv);
         return ret;
+
+    return ret;
   }
   EXPORT_SYMBOL(ttm_bo_vm_fault);

  diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h index e17be32..12fb240 100644

include/drm/ttm/ttm_bo_api.h +1 -0
@@ -643,4 +643,6 @@ void ttm_bo_vm_close(struct vm_area_struct *vma);
   int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
                void *buf, int len, int write);
   +vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
+
   #endif
Signature · 1 line
2.7.4
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#8 lore raw

PATCH v4 2/14 drm: Unamp the entire device address space on device unplug

In reply to Andrey Grodzovsky

Invalidate all BOs CPU mappings once device is removed.

v3: Move the code from TTM into drm_dev_unplug

---
 drivers/gpu/drm/drm_drv.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/drm_drv.c +3 -0
index d384a5b..20d22e4 100644
@@ -469,6 +469,9 @@ void drm_dev_unplug(struct drm_device *dev)
 	synchronize_srcu(&drm_unplug_srcu);
 
 	drm_dev_unregister(dev);
+
+	/* Clear all CPU mappings pointing to this device */
+	unmap_mapping_range(dev->anon_inode->i_mapping, 0, 0, 1);
 }
 EXPORT_SYMBOL(drm_dev_unplug);
 
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

PATCH v4 3/14 drm/ttm: Expose ttm_tt_unpopulate for driver use

In reply to Andrey Grodzovsky

It's needed to drop iommu backed pages on device unplug before device's IOMMU group is released.

---
 drivers/gpu/drm/ttm/ttm_tt.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/ttm/ttm_tt.c +1 -0
index 7f75a13..f9e0b0d 100644
@@ -341,3 +341,4 @@ void ttm_tt_unpopulate(struct ttm_bo_device *bdev,
 		ttm_pool_free(&bdev->pool, ttm);
 	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
 }
+EXPORT_SYMBOL(ttm_tt_unpopulate);
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#10 lore raw

PATCH v4 4/14 drm/sched: Cancel and flush all oustatdning jobs before finish.

In reply to Andrey Grodzovsky

To avoid any possible use after free.

---
 drivers/gpu/drm/scheduler/sched_main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +3 -0
index 997aa15..92637b7 100644
@@ -899,6 +899,9 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
 	if (sched->thread)
 		kthread_stop(sched->thread);
 
+	/* Confirm no work left behind accessing device structures */
+	cancel_delayed_work_sync(&sched->work_tdr);
+
 	sched->ready = false;
 }
 EXPORT_SYMBOL(drm_sched_fini);
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#11 lore raw

Re: [PATCH v4 04/14] drm/sched: Cancel and flush all oustatdning jobs before finish.

Quoted 5 lines · Andrey Grodzovsky

To avoid any possible use after free.

In the subject:
oustatdning -> outstanding

Alex

Quoted 21 lines
---
 drivers/gpu/drm/scheduler/sched_main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +3 -1
index 997aa15..92637b7 100644
@@ -899,6 +899,9 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
        if (sched->thread)
                kthread_stop(sched->thread);
 
+       /* Confirm no work left behind accessing device structures */
+       cancel_delayed_work_sync(&sched->work_tdr);
+
        sched->ready = false;
 }
 EXPORT_SYMBOL(drm_sched_fini);
--

2.7.4

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#12 lore raw

Re: [PATCH v4 04/14] drm/sched: Cancel and flush all oustatdning jobs before finish.

In reply to Andrey Grodzovsky

This is a bug fix and should probably be pushed separately to drm-misc-next.

Christian.

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 13 lines

To avoid any possible use after free.

---
  drivers/gpu/drm/scheduler/sched_main.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 997aa15..92637b7 100644

drivers/gpu/drm/scheduler/sched_main.c void drm_sched_fini(struct drm_gpu_scheduler *sched) · line 907

Quoted 9 lines
drivers/gpu/drm/scheduler/sched_main.c +3 -0
@@ -899,6 +899,9 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
  	if (sched->thread)
  		kthread_stop(sched->thread);
 
+	/* Confirm no work left behind accessing device structures */
+	cancel_delayed_work_sync(&sched->work_tdr);
+
  	sched->ready = false;
  }
  EXPORT_SYMBOL(drm_sched_fini);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#13 lore raw

Re: [PATCH v4 04/14] drm/sched: Cancel and flush all oustatdning jobs before finish.

Added a CC: stable tag and pushed it.

Thanks,
Christian.

Am 19.01.21 um 09:42 schrieb Christian König:

Quoted 13 lines

This is a bug fix and should probably be pushed separately to drm-misc-next.

Christian.

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 8 lines

To avoid any possible use after free.

---
  drivers/gpu/drm/scheduler/sched_main.c | 3 +++

  1 file changed, 3 insertions(+)
Quoted 14 lines
Quoted 7 lines
drivers/gpu/drm/scheduler/sched_main.c +0 -0
index 997aa15..92637b7 100644
@@ -899,6 +899,9 @@ void drm_sched_fini(struct drm_gpu_scheduler *sched)
       if (sched->thread)
           kthread_stop(sched->thread);
Quoted 6 lines

  +    /* Confirm no work left behind accessing device structures */ +    cancel_delayed_work_sync(&sched->work_tdr);
+
      sched->ready = false;
  }
  EXPORT_SYMBOL(drm_sched_fini);

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#14 lore raw

PATCH v4 5/14 drm/amdgpu: Split amdgpu_device_fini into early and late

In reply to Andrey Grodzovsky

Some of the stuff in amdgpu_device_fini such as HW interrupts disable and pending fences finilization must be done right away on pci_remove while most of the stuff which relates to finilizing and releasing driver data structures can be kept until drm_driver.release hook is called, i.e. when the last device reference is dropped.

v4: Change functions prefix early->hw and late->sw

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  6 +++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 26 ++++++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |  7 ++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  | 15 ++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c    | 26 ++++++++++++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_irq.h    |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c    | 12 +++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c    |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  3 ++-
 drivers/gpu/drm/amd/amdgpu/cik_ih.c        |  2 +-
 drivers/gpu/drm/amd/amdgpu/cz_ih.c         |  2 +-
 drivers/gpu/drm/amd/amdgpu/iceland_ih.c    |  2 +-
 drivers/gpu/drm/amd/amdgpu/navi10_ih.c     |  2 +-
 drivers/gpu/drm/amd/amdgpu/si_ih.c         |  2 +-
 drivers/gpu/drm/amd/amdgpu/tonga_ih.c      |  2 +-
 drivers/gpu/drm/amd/amdgpu/vega10_ih.c     |  2 +-
 16 files changed, 78 insertions(+), 35 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -1
index f77443c..478a7d8 100644
@@ -1060,7 +1060,9 @@ static inline struct amdgpu_device *amdgpu_ttm_adev(struct ttm_bo_device *bdev)
 
 int amdgpu_device_init(struct amdgpu_device *adev,
 		       uint32_t flags);
-void amdgpu_device_fini(struct amdgpu_device *adev);
+void amdgpu_device_fini_hw(struct amdgpu_device *adev);
+void amdgpu_device_fini_sw(struct amdgpu_device *adev);
+
 int amdgpu_gpu_wait_for_idle(struct amdgpu_device *adev);
 
 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
@@ -1273,6 +1275,8 @@ void amdgpu_driver_lastclose_kms(struct drm_device *dev);
 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
 void amdgpu_driver_postclose_kms(struct drm_device *dev,
 				 struct drm_file *file_priv);
+void amdgpu_driver_release_kms(struct drm_device *dev);
+
 int amdgpu_device_ip_suspend(struct amdgpu_device *adev);
 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon);
 int amdgpu_device_resume(struct drm_device *dev, bool fbcon);
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +18 -8
index 348ac67..90c8353 100644
@@ -3579,14 +3579,12 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  * Tear down the driver info (all asics).
  * Called at driver shutdown.
  */
-void amdgpu_device_fini(struct amdgpu_device *adev)
+void amdgpu_device_fini_hw(struct amdgpu_device *adev)
 {
 	dev_info(adev->dev, "amdgpu: finishing device.\n");
 	flush_delayed_work(&adev->delayed_init_work);
 	adev->shutdown = true;
 
-	kfree(adev->pci_state);
-
 	/* make sure IB test finished before entering exclusive mode
 	 * to avoid preemption on IB test
 	 * */
@@ -3603,11 +3601,24 @@ void amdgpu_device_fini(struct amdgpu_device *adev)
 		else
 			drm_atomic_helper_shutdown(adev_to_drm(adev));
 	}
-	amdgpu_fence_driver_fini(adev);
+	amdgpu_fence_driver_fini_hw(adev);
+
 	if (adev->pm_sysfs_en)
 		amdgpu_pm_sysfs_fini(adev);
+	if (adev->ucode_sysfs_en)
+		amdgpu_ucode_sysfs_fini(adev);
+	sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
+
+
 	amdgpu_fbdev_fini(adev);
+
+	amdgpu_irq_fini_hw(adev);
+}
+
+void amdgpu_device_fini_sw(struct amdgpu_device *adev)
+{
 	amdgpu_device_ip_fini(adev);
+	amdgpu_fence_driver_fini_sw(adev);
 	release_firmware(adev->firmware.gpu_info_fw);
 	adev->firmware.gpu_info_fw = NULL;
 	adev->accel_working = false;
@@ -3636,14 +3647,13 @@ void amdgpu_device_fini(struct amdgpu_device *adev)
 	adev->rmmio = NULL;
 	amdgpu_device_doorbell_fini(adev);
 
-	if (adev->ucode_sysfs_en)
-		amdgpu_ucode_sysfs_fini(adev);
-
-	sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
 	if (IS_ENABLED(CONFIG_PERF_EVENTS))
 		amdgpu_pmu_fini(adev);
 	if (adev->mman.discovery_bin)
 		amdgpu_discovery_fini(adev);
+
+	kfree(adev->pci_state);
+
 }
 
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -5
index 72efd57..9c0cd00 100644
@@ -1238,14 +1238,10 @@ amdgpu_pci_remove(struct pci_dev *pdev)
 {
 	struct drm_device *dev = pci_get_drvdata(pdev);
 
-#ifdef MODULE
-	if (THIS_MODULE->state != MODULE_STATE_GOING)
-#endif
-		DRM_ERROR("Hotplug removal is not supported\n");
 	drm_dev_unplug(dev);
 	amdgpu_driver_unload_kms(dev);
+
 	pci_disable_device(pdev);
-	pci_set_drvdata(pdev, NULL);
 }
 
 static void
@@ -1569,6 +1565,7 @@ static const struct drm_driver amdgpu_kms_driver = {
 	.dumb_create = amdgpu_mode_dumb_create,
 	.dumb_map_offset = amdgpu_mode_dumb_mmap,
 	.fops = &amdgpu_driver_kms_fops,
+	.release = &amdgpu_driver_release_kms,
 
 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -1
index d56f402..e19b74c 100644
@@ -523,7 +523,7 @@ int amdgpu_fence_driver_init(struct amdgpu_device *adev)
  *
  * Tear down the fence driver for all possible rings (all asics).
  */
-void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
+void amdgpu_fence_driver_fini_hw(struct amdgpu_device *adev)
 {
 	unsigned i, j;
 	int r;
@@ -544,6 +544,19 @@ void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
 		if (!ring->no_scheduler)
 			drm_sched_fini(&ring->sched);
 		del_timer_sync(&ring->fence_drv.fallback_timer);
+	}
+}
+
+void amdgpu_fence_driver_fini_sw(struct amdgpu_device *adev)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
+		struct amdgpu_ring *ring = adev->rings[i];
+
+		if (!ring || !ring->fence_drv.initialized)
+			continue;
+
 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
 			dma_fence_put(ring->fence_drv.fences[j]);
 		kfree(ring->fence_drv.fences);
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +16 -10
index bea57e8..2f1cfc5 100644
@@ -49,6 +49,7 @@
 #include <drm/drm_irq.h>
 #include <drm/drm_vblank.h>
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 #include "amdgpu.h"
 #include "amdgpu_ih.h"
 #include "atom.h"
@@ -313,6 +314,20 @@ int amdgpu_irq_init(struct amdgpu_device *adev)
 	return 0;
 }
 
+
+void amdgpu_irq_fini_hw(struct amdgpu_device *adev)
+{
+	if (adev->irq.installed) {
+		drm_irq_uninstall(&adev->ddev);
+		adev->irq.installed = false;
+		if (adev->irq.msi_enabled)
+			pci_free_irq_vectors(adev->pdev);
+
+		if (!amdgpu_device_has_dc_support(adev))
+			flush_work(&adev->hotplug_work);
+	}
+}
+
 /**
  * amdgpu_irq_fini - shut down interrupt handling
  *
@@ -322,19 +337,10 @@ int amdgpu_irq_init(struct amdgpu_device *adev)
  * functionality, shuts down vblank, hotplug and reset interrupt handling,
  * turns off interrupts from all sources (all ASICs).
  */
-void amdgpu_irq_fini(struct amdgpu_device *adev)
+void amdgpu_irq_fini_sw(struct amdgpu_device *adev)
 {
 	unsigned i, j;
 
-	if (adev->irq.installed) {
-		drm_irq_uninstall(adev_to_drm(adev));
-		adev->irq.installed = false;
-		if (adev->irq.msi_enabled)
-			pci_free_irq_vectors(adev->pdev);
-		if (!amdgpu_device_has_dc_support(adev))
-			flush_work(&adev->hotplug_work);
-	}
-
 	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
 		if (!adev->irq.client[i].sources)
 			continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.h +2 -1
index ac527e5..392a732 100644
@@ -104,7 +104,8 @@ void amdgpu_irq_disable_all(struct amdgpu_device *adev);
 irqreturn_t amdgpu_irq_handler(int irq, void *arg);
 
 int amdgpu_irq_init(struct amdgpu_device *adev);
-void amdgpu_irq_fini(struct amdgpu_device *adev);
+void amdgpu_irq_fini_sw(struct amdgpu_device *adev);
+void amdgpu_irq_fini_hw(struct amdgpu_device *adev);
 int amdgpu_irq_add_id(struct amdgpu_device *adev,
 		      unsigned client_id, unsigned src_id,
 		      struct amdgpu_irq_src *source);
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +11 -1
index b16b327..fee95d3 100644
@@ -29,6 +29,7 @@
 #include "amdgpu.h"
 #include <drm/drm_debugfs.h>
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 #include "amdgpu_uvd.h"
 #include "amdgpu_vce.h"
 #include "atom.h"
@@ -93,7 +94,7 @@ void amdgpu_driver_unload_kms(struct drm_device *dev)
 	}
 
 	amdgpu_acpi_fini(adev);
-	amdgpu_device_fini(adev);
+	amdgpu_device_fini_hw(adev);
 }
 
 void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
@@ -1153,6 +1154,15 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
 	pm_runtime_put_autosuspend(dev->dev);
 }
 
+
+void amdgpu_driver_release_kms(struct drm_device *dev)
+{
+	struct amdgpu_device *adev = drm_to_adev(dev);
+
+	amdgpu_device_fini_sw(adev);
+	pci_set_drvdata(adev->pdev, NULL);
+}
+
 /*
  * VBlank related functions.
  */
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -0
index c136bd4..87eaf13 100644
@@ -2142,6 +2142,7 @@ int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
 	if (!con)
 		return 0;
 
+
 	/* Need disable ras on all IPs here before ip [hw/sw]fini */
 	amdgpu_ras_disable_all_features(adev, 0);
 	amdgpu_ras_recovery_fini(adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +2 -1
index 7112137..accb243 100644
@@ -107,7 +107,8 @@ struct amdgpu_fence_driver {
 };
 
 int amdgpu_fence_driver_init(struct amdgpu_device *adev);
-void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
+void amdgpu_fence_driver_fini_hw(struct amdgpu_device *adev);
+void amdgpu_fence_driver_fini_sw(struct amdgpu_device *adev);
 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
 
 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
drivers/gpu/drm/amd/amdgpu/cik_ih.c +1 -1
index d374571..183d44a 100644
@@ -309,7 +309,7 @@ static int cik_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
 	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/cz_ih.c +1 -1
index da37f8a..ee824d7 100644
@@ -290,7 +290,7 @@ static int cz_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
 	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/iceland_ih.c +1 -1
index 37d8b6c..b24f6fb 100644
@@ -290,7 +290,7 @@ static int iceland_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
 	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/navi10_ih.c +1 -1
index 7ba229e..c191410 100644
@@ -716,7 +716,7 @@ static int navi10_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
drivers/gpu/drm/amd/amdgpu/si_ih.c +1 -1
index 51880f6..751307f 100644
@@ -175,7 +175,7 @@ static int si_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
 
 	return 0;
drivers/gpu/drm/amd/amdgpu/tonga_ih.c +1 -1
index ce33199..729aaaa 100644
@@ -301,7 +301,7 @@ static int tonga_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
 	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/vega10_ih.c +1 -1
index e5ae31e..a342406 100644
@@ -627,7 +627,7 @@ static int vega10_ih_sw_fini(void *handle)
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
 	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
reviewer Acked-by on v4 5/14
#15 lore raw

Re: [PATCH v4 05/14] drm/amdgpu: Split amdgpu_device_fini into early and late

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 10 lines

Some of the stuff in amdgpu_device_fini such as HW interrupts disable and pending fences finilization must be done right away on pci_remove while most of the stuff which relates to finilizing and releasing driver data structures can be kept until drm_driver.release hook is called, i.e. when the last device reference is dropped.

v4: Change functions prefix early->hw and late->sw

The fence and irq changes look sane to me, no idea for the rest.

Quoted 382 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  6 +++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 26 ++++++++++++++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |  7 ++-----
  drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c  | 15 ++++++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c    | 26 ++++++++++++++++----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_irq.h    |  3 ++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c    | 12 +++++++++++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c    |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   |  3 ++-
  drivers/gpu/drm/amd/amdgpu/cik_ih.c        |  2 +-
  drivers/gpu/drm/amd/amdgpu/cz_ih.c         |  2 +-
  drivers/gpu/drm/amd/amdgpu/iceland_ih.c    |  2 +-
  drivers/gpu/drm/amd/amdgpu/navi10_ih.c     |  2 +-
  drivers/gpu/drm/amd/amdgpu/si_ih.c         |  2 +-
  drivers/gpu/drm/amd/amdgpu/tonga_ih.c      |  2 +-
  drivers/gpu/drm/amd/amdgpu/vega10_ih.c     |  2 +-
  16 files changed, 78 insertions(+), 35 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -1
index f77443c..478a7d8 100644
@@ -1060,7 +1060,9 @@ static inline struct amdgpu_device *amdgpu_ttm_adev(struct ttm_bo_device *bdev)
 
  int amdgpu_device_init(struct amdgpu_device *adev,
  		       uint32_t flags);
-void amdgpu_device_fini(struct amdgpu_device *adev);
+void amdgpu_device_fini_hw(struct amdgpu_device *adev);
+void amdgpu_device_fini_sw(struct amdgpu_device *adev);
+
  int amdgpu_gpu_wait_for_idle(struct amdgpu_device *adev);
 
  void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
@@ -1273,6 +1275,8 @@ void amdgpu_driver_lastclose_kms(struct drm_device *dev);
  int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
  void amdgpu_driver_postclose_kms(struct drm_device *dev,
  				 struct drm_file *file_priv);
+void amdgpu_driver_release_kms(struct drm_device *dev);
+
  int amdgpu_device_ip_suspend(struct amdgpu_device *adev);
  int amdgpu_device_suspend(struct drm_device *dev, bool fbcon);
  int amdgpu_device_resume(struct drm_device *dev, bool fbcon);
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +18 -8
index 348ac67..90c8353 100644
@@ -3579,14 +3579,12 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   * Tear down the driver info (all asics).
   * Called at driver shutdown.
   */
-void amdgpu_device_fini(struct amdgpu_device *adev)
+void amdgpu_device_fini_hw(struct amdgpu_device *adev)
  {
  	dev_info(adev->dev, "amdgpu: finishing device.\n");
  	flush_delayed_work(&adev->delayed_init_work);
  	adev->shutdown = true;
 
-	kfree(adev->pci_state);
-
  	/* make sure IB test finished before entering exclusive mode
  	 * to avoid preemption on IB test
  	 * */
@@ -3603,11 +3601,24 @@ void amdgpu_device_fini(struct amdgpu_device *adev)
  		else
  			drm_atomic_helper_shutdown(adev_to_drm(adev));
  	}
-	amdgpu_fence_driver_fini(adev);
+	amdgpu_fence_driver_fini_hw(adev);
+
  	if (adev->pm_sysfs_en)
  		amdgpu_pm_sysfs_fini(adev);
+	if (adev->ucode_sysfs_en)
+		amdgpu_ucode_sysfs_fini(adev);
+	sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
+
+
  	amdgpu_fbdev_fini(adev);
+
+	amdgpu_irq_fini_hw(adev);
+}
+
+void amdgpu_device_fini_sw(struct amdgpu_device *adev)
+{
  	amdgpu_device_ip_fini(adev);
+	amdgpu_fence_driver_fini_sw(adev);
  	release_firmware(adev->firmware.gpu_info_fw);
  	adev->firmware.gpu_info_fw = NULL;
  	adev->accel_working = false;
@@ -3636,14 +3647,13 @@ void amdgpu_device_fini(struct amdgpu_device *adev)
  	adev->rmmio = NULL;
  	amdgpu_device_doorbell_fini(adev);
 
-	if (adev->ucode_sysfs_en)
-		amdgpu_ucode_sysfs_fini(adev);
-
-	sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
  	if (IS_ENABLED(CONFIG_PERF_EVENTS))
  		amdgpu_pmu_fini(adev);
  	if (adev->mman.discovery_bin)
  		amdgpu_discovery_fini(adev);
+
+	kfree(adev->pci_state);
+
  }
 
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -5
index 72efd57..9c0cd00 100644
@@ -1238,14 +1238,10 @@ amdgpu_pci_remove(struct pci_dev *pdev)
  {
  	struct drm_device *dev = pci_get_drvdata(pdev);
 
-#ifdef MODULE
-	if (THIS_MODULE->state != MODULE_STATE_GOING)
-#endif
-		DRM_ERROR("Hotplug removal is not supported\n");
  	drm_dev_unplug(dev);
  	amdgpu_driver_unload_kms(dev);
+
  	pci_disable_device(pdev);
-	pci_set_drvdata(pdev, NULL);
  }
 
  static void
@@ -1569,6 +1565,7 @@ static const struct drm_driver amdgpu_kms_driver = {
  	.dumb_create = amdgpu_mode_dumb_create,
  	.dumb_map_offset = amdgpu_mode_dumb_mmap,
  	.fops = &amdgpu_driver_kms_fops,
+	.release = &amdgpu_driver_release_kms,
 
  	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -1
index d56f402..e19b74c 100644
@@ -523,7 +523,7 @@ int amdgpu_fence_driver_init(struct amdgpu_device *adev)
   *
   * Tear down the fence driver for all possible rings (all asics).
   */
-void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
+void amdgpu_fence_driver_fini_hw(struct amdgpu_device *adev)
  {
  	unsigned i, j;
  	int r;
@@ -544,6 +544,19 @@ void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
  		if (!ring->no_scheduler)
  			drm_sched_fini(&ring->sched);
  		del_timer_sync(&ring->fence_drv.fallback_timer);
+	}
+}
+
+void amdgpu_fence_driver_fini_sw(struct amdgpu_device *adev)
+{
+	unsigned int i, j;
+
+	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
+		struct amdgpu_ring *ring = adev->rings[i];
+
+		if (!ring || !ring->fence_drv.initialized)
+			continue;
+
  		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
  			dma_fence_put(ring->fence_drv.fences[j]);
  		kfree(ring->fence_drv.fences);
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c +16 -10
index bea57e8..2f1cfc5 100644
@@ -49,6 +49,7 @@
  #include <drm/drm_irq.h>
  #include <drm/drm_vblank.h>
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
  #include "amdgpu.h"
  #include "amdgpu_ih.h"
  #include "atom.h"
@@ -313,6 +314,20 @@ int amdgpu_irq_init(struct amdgpu_device *adev)
  	return 0;
  }
 
+
+void amdgpu_irq_fini_hw(struct amdgpu_device *adev)
+{
+	if (adev->irq.installed) {
+		drm_irq_uninstall(&adev->ddev);
+		adev->irq.installed = false;
+		if (adev->irq.msi_enabled)
+			pci_free_irq_vectors(adev->pdev);
+
+		if (!amdgpu_device_has_dc_support(adev))
+			flush_work(&adev->hotplug_work);
+	}
+}
+
  /**
   * amdgpu_irq_fini - shut down interrupt handling
   *
@@ -322,19 +337,10 @@ int amdgpu_irq_init(struct amdgpu_device *adev)
   * functionality, shuts down vblank, hotplug and reset interrupt handling,
   * turns off interrupts from all sources (all ASICs).
   */
-void amdgpu_irq_fini(struct amdgpu_device *adev)
+void amdgpu_irq_fini_sw(struct amdgpu_device *adev)
  {
  	unsigned i, j;
 
-	if (adev->irq.installed) {
-		drm_irq_uninstall(adev_to_drm(adev));
-		adev->irq.installed = false;
-		if (adev->irq.msi_enabled)
-			pci_free_irq_vectors(adev->pdev);
-		if (!amdgpu_device_has_dc_support(adev))
-			flush_work(&adev->hotplug_work);
-	}
-
  	for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
  		if (!adev->irq.client[i].sources)
  			continue;
drivers/gpu/drm/amd/amdgpu/amdgpu_irq.h +2 -1
index ac527e5..392a732 100644
@@ -104,7 +104,8 @@ void amdgpu_irq_disable_all(struct amdgpu_device *adev);
  irqreturn_t amdgpu_irq_handler(int irq, void *arg);
 
  int amdgpu_irq_init(struct amdgpu_device *adev);
-void amdgpu_irq_fini(struct amdgpu_device *adev);
+void amdgpu_irq_fini_sw(struct amdgpu_device *adev);
+void amdgpu_irq_fini_hw(struct amdgpu_device *adev);
  int amdgpu_irq_add_id(struct amdgpu_device *adev,
  		      unsigned client_id, unsigned src_id,
  		      struct amdgpu_irq_src *source);
drivers/gpu/drm/amd/amdgpu/amdgpu_kms.c +11 -1
index b16b327..fee95d3 100644
@@ -29,6 +29,7 @@
  #include "amdgpu.h"
  #include <drm/drm_debugfs.h>
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
  #include "amdgpu_uvd.h"
  #include "amdgpu_vce.h"
  #include "atom.h"
@@ -93,7 +94,7 @@ void amdgpu_driver_unload_kms(struct drm_device *dev)
  	}
 
  	amdgpu_acpi_fini(adev);
-	amdgpu_device_fini(adev);
+	amdgpu_device_fini_hw(adev);
  }
 
  void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
@@ -1153,6 +1154,15 @@ void amdgpu_driver_postclose_kms(struct drm_device *dev,
  	pm_runtime_put_autosuspend(dev->dev);
  }
 
+
+void amdgpu_driver_release_kms(struct drm_device *dev)
+{
+	struct amdgpu_device *adev = drm_to_adev(dev);
+
+	amdgpu_device_fini_sw(adev);
+	pci_set_drvdata(adev->pdev, NULL);
+}
+
  /*
   * VBlank related functions.
   */
drivers/gpu/drm/amd/amdgpu/amdgpu_ras.c +1 -0
index c136bd4..87eaf13 100644
@@ -2142,6 +2142,7 @@ int amdgpu_ras_pre_fini(struct amdgpu_device *adev)
  	if (!con)
  		return 0;
 
+
  	/* Need disable ras on all IPs here before ip [hw/sw]fini */
  	amdgpu_ras_disable_all_features(adev, 0);
  	amdgpu_ras_recovery_fini(adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +2 -1
index 7112137..accb243 100644
@@ -107,7 +107,8 @@ struct amdgpu_fence_driver {
  };
 
  int amdgpu_fence_driver_init(struct amdgpu_device *adev);
-void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
+void amdgpu_fence_driver_fini_hw(struct amdgpu_device *adev);
+void amdgpu_fence_driver_fini_sw(struct amdgpu_device *adev);
  void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
 
  int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
drivers/gpu/drm/amd/amdgpu/cik_ih.c +1 -1
index d374571..183d44a 100644
@@ -309,7 +309,7 @@ static int cik_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
  	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/cz_ih.c +1 -1
index da37f8a..ee824d7 100644
@@ -290,7 +290,7 @@ static int cz_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
  	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/iceland_ih.c +1 -1
index 37d8b6c..b24f6fb 100644
@@ -290,7 +290,7 @@ static int iceland_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
  	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/navi10_ih.c +1 -1
index 7ba229e..c191410 100644
@@ -716,7 +716,7 @@ static int navi10_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
drivers/gpu/drm/amd/amdgpu/si_ih.c +1 -1
index 51880f6..751307f 100644
@@ -175,7 +175,7 @@ static int si_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
 
  	return 0;
drivers/gpu/drm/amd/amdgpu/tonga_ih.c +1 -1
index ce33199..729aaaa 100644
@@ -301,7 +301,7 @@ static int tonga_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);
  	amdgpu_irq_remove_domain(adev);
 
drivers/gpu/drm/amd/amdgpu/vega10_ih.c +0 -0
index e5ae31e..a342406 100644

drivers/gpu/drm/amd/amdgpu/vega10_ih.c static int vega10_ih_sw_fini(void *handle) · line 633

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/vega10_ih.c +1 -1
@@ -627,7 +627,7 @@ static int vega10_ih_sw_fini(void *handle)
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
-	amdgpu_irq_fini(adev);
+	amdgpu_irq_fini_sw(adev);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
  	amdgpu_ih_ring_fini(adev, &adev->irq.ih);

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#16 lore raw

PATCH v4 6/14 drm/amdgpu: Add early fini callback

In reply to Andrey Grodzovsky

Use it to call disply code dependent on device->drv_data before it's set to NULL on device unplug

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c        | 20 ++++++++++++++++++++
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 12 ++++++++++--
 drivers/gpu/drm/amd/include/amd_shared.h          |  2 ++
 3 files changed, 32 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -0
index 90c8353..45e23e3 100644
@@ -2529,6 +2529,24 @@ static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
 	return 0;
 }
 
+static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
+{
+	int i, r;
+
+	for (i = 0; i < adev->num_ip_blocks; i++) {
+		if (!adev->ip_blocks[i].version->funcs->early_fini)
+			continue;
+
+		r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
+		if (r) {
+			DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
+				  adev->ip_blocks[i].version->funcs->name, r);
+		}
+	}
+
+	return 0;
+}
+
 /**
  * amdgpu_device_ip_fini - run fini for hardware IPs
  *
@@ -3613,6 +3631,8 @@ void amdgpu_device_fini_hw(struct amdgpu_device *adev)
 	amdgpu_fbdev_fini(adev);
 
 	amdgpu_irq_fini_hw(adev);
+
+	amdgpu_device_ip_fini_early(adev);
 }
 
 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +10 -2
index 86c2b2c..9b24f3e 100644
@@ -1156,6 +1156,15 @@ static int amdgpu_dm_init(struct amdgpu_device *adev)
 	return -EINVAL;
 }
 
+static int amdgpu_dm_early_fini(void *handle)
+{
+	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
+
+	amdgpu_dm_audio_fini(adev);
+
+	return 0;
+}
+
 static void amdgpu_dm_fini(struct amdgpu_device *adev)
 {
 	int i;
@@ -1164,8 +1173,6 @@ static void amdgpu_dm_fini(struct amdgpu_device *adev)
 		drm_encoder_cleanup(&adev->dm.mst_encoders[i].base);
 	}
 
-	amdgpu_dm_audio_fini(adev);
-
 	amdgpu_dm_destroy_drm_device(&adev->dm);
 
 #ifdef CONFIG_DRM_AMD_DC_HDCP
@@ -2175,6 +2182,7 @@ static const struct amd_ip_funcs amdgpu_dm_funcs = {
 	.late_init = dm_late_init,
 	.sw_init = dm_sw_init,
 	.sw_fini = dm_sw_fini,
+	.early_fini = amdgpu_dm_early_fini,
 	.hw_init = dm_hw_init,
 	.hw_fini = dm_hw_fini,
 	.suspend = dm_suspend,
drivers/gpu/drm/amd/include/amd_shared.h +2 -0
index 9676016..63bb846 100644
@@ -239,6 +239,7 @@ enum amd_dpm_forced_level;
  * @late_init: sets up late driver/hw state (post hw_init) - Optional
  * @sw_init: sets up driver state, does not configure hw
  * @sw_fini: tears down driver state, does not configure hw
+ * @early_fini: tears down stuff before dev detached from driver
  * @hw_init: sets up the hw state
  * @hw_fini: tears down the hw state
  * @late_fini: final cleanup
@@ -267,6 +268,7 @@ struct amd_ip_funcs {
 	int (*late_init)(void *handle);
 	int (*sw_init)(void *handle);
 	int (*sw_fini)(void *handle);
+	int (*early_fini)(void *handle);
 	int (*hw_init)(void *handle);
 	int (*hw_fini)(void *handle);
 	void (*late_fini)(void *handle);
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#17 lore raw

PATCH v4 7/14 drm/amdgpu: Register IOMMU topology notifier per device.

In reply to Andrey Grodzovsky

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
 6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
 #include <linux/dma-fence.h>
 #include <linux/pci.h>
 #include <linux/aer.h>
+#include <linux/notifier.h>
 
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
 
 	bool                            in_pci_err_recovery;
 	struct pci_saved_state          *pci_state;
+
+	struct notifier_block		nb;
+	struct blocking_notifier_head	notifier;
+	struct list_head		device_bo_list;
 };
 
 static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +46 -0

70 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
  *
  * Frees the dummy page used by the driver (all asics).
  */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
 {
 	if (!adev->dummy_page_addr)
 		return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
 int amdgpu_gart_init(struct amdgpu_device *adev);
 void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
 		       int pages);
 int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
 	}
 	amdgpu_bo_unref(&bo->parent);
 
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_del(&bo->bo);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
 	kfree(bo->metadata);
 	kfree(bo);
 }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	if (bp->type == ttm_bo_type_device)
 		bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
 
+	INIT_LIST_HEAD(&bo->bo);
+
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_add_tail(&bo->bo, &adev->device_bo_list);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
 	return 0;
 
 fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
 	struct list_head		shadow_list;
 
 	struct kgd_mem                  *kfd_bo;
+
+	struct list_head		bo;
 };
 
 static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#18 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 2 lines · Andrey Grodzovsky

Handle all DMA IOMMU gropup related dependencies before the

gropup -> group

Alex

Quoted 182 lines

group is removed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
 6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
 #include <linux/dma-fence.h>
 #include <linux/pci.h>
 #include <linux/aer.h>
+#include <linux/notifier.h>
 
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
 
        bool                            in_pci_err_recovery;
        struct pci_saved_state          *pci_state;
+
+       struct notifier_block           nb;
+       struct blocking_notifier_head   notifier;
+       struct list_head                device_bo_list;
 };
 
 static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +46 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
 #include <drm/task_barrier.h>
 #include <linux/pm_runtime.h>
 
+#include <linux/iommu.h>
+
 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
 };
 
 
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+                                    unsigned long action, void *data)
+{
+       struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+       struct amdgpu_bo *bo = NULL;
+
+       /*
+        * Following is a set of IOMMU group dependencies taken care of before
+        * device's IOMMU group is removed
+        */
+       if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+               spin_lock(&ttm_bo_glob.lru_lock);
+               list_for_each_entry(bo, &adev->device_bo_list, bo) {
+                       if (bo->tbo.ttm)
+                               ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+               }
+               spin_unlock(&ttm_bo_glob.lru_lock);
+
+               if (adev->irq.ih.use_bus_addr)
+                       amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+               if (adev->irq.ih1.use_bus_addr)
+                       amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+               if (adev->irq.ih2.use_bus_addr)
+                       amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+               amdgpu_gart_dummy_page_fini(adev);
+       }
+
+       return NOTIFY_OK;
+}
+
+
 /**
  * amdgpu_device_init - initialize the driver
  *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 
        INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
 
+       INIT_LIST_HEAD(&adev->device_bo_list);
+
        adev->gfx.gfx_off_req_count = 1;
        adev->pm.ac_power = power_supply_is_system_supplied() > 0;
 
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
        if (amdgpu_device_cache_pci_state(adev->pdev))
                pci_restore_state(pdev);
 
+       BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+       adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+       if (adev->dev->iommu_group) {
+               r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+               if (r)
+                       goto failed;
+       }
+
        return 0;
 
 failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
  *
  * Frees the dummy page used by the driver (all asics).
  */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
 {
        if (!adev->dummy_page_addr)
                return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
 int amdgpu_gart_init(struct amdgpu_device *adev);
 void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
                       int pages);
 int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
        }
        amdgpu_bo_unref(&bo->parent);
 
+       spin_lock(&ttm_bo_glob.lru_lock);
+       list_del(&bo->bo);
+       spin_unlock(&ttm_bo_glob.lru_lock);
+
        kfree(bo->metadata);
        kfree(bo);
 }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
        if (bp->type == ttm_bo_type_device)
                bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
 
+       INIT_LIST_HEAD(&bo->bo);
+
+       spin_lock(&ttm_bo_glob.lru_lock);
+       list_add_tail(&bo->bo, &adev->device_bo_list);
+       spin_unlock(&ttm_bo_glob.lru_lock);
+
        return 0;
 
 fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -1
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
        struct list_head                shadow_list;
 
        struct kgd_mem                  *kfd_bo;
+
+       struct list_head                bo;
 };
 
 static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
--

2.7.4

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#19 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

In reply to Andrey Grodzovsky

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 61 lines

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
  6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
  #include <linux/dma-fence.h>
  #include <linux/pci.h>
  #include <linux/aer.h>
+#include <linux/notifier.h>
 
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
 
  	bool                            in_pci_err_recovery;
  	struct pci_saved_state          *pci_state;
+
+	struct notifier_block		nb;
+	struct blocking_notifier_head	notifier;
+	struct list_head		device_bo_list;
  };
 
  static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
  #include <drm/task_barrier.h>
  #include <linux/pm_runtime.h>
 
+#include <linux/iommu.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
  };
 
 
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+				     unsigned long action, void *data)
+{
+	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+	struct amdgpu_bo *bo = NULL;
+
+	/*
+	 * Following is a set of IOMMU group dependencies taken care of before

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static const struct attribute *amdgpu_dev_attributes[] = { · line 3222

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+	 * device's IOMMU group is removed
+	 */
+	if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+		spin_lock(&ttm_bo_glob.lru_lock);
+		list_for_each_entry(bo, &adev->device_bo_list, bo) {
+			if (bo->tbo.ttm)
+				ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+		}
+		spin_unlock(&ttm_bo_glob.lru_lock);

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

Christian.

Quoted 101 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+		if (adev->irq.ih.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+		if (adev->irq.ih1.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+		if (adev->irq.ih2.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+		amdgpu_gart_dummy_page_fini(adev);
+	}
+
+	return NOTIFY_OK;
+}
+
+
  /**
   * amdgpu_device_init - initialize the driver
   *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
 
  	INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
 
+	INIT_LIST_HEAD(&adev->device_bo_list);
+
  	adev->gfx.gfx_off_req_count = 1;
  	adev->pm.ac_power = power_supply_is_system_supplied() > 0;
 
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  	if (amdgpu_device_cache_pci_state(adev->pdev))
  		pci_restore_state(pdev);
 
+	BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+	adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+	if (adev->dev->iommu_group) {
+		r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+		if (r)
+			goto failed;
+	}
+
  	return 0;
 
  failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
   *
   * Frees the dummy page used by the driver (all asics).
   */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
  {
  	if (!adev->dummy_page_addr)
  		return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
  void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
  int amdgpu_gart_init(struct amdgpu_device *adev);
  void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
  int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  		       int pages);
  int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
  	}
  	amdgpu_bo_unref(&bo->parent);
 
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_del(&bo->bo);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
  	kfree(bo->metadata);
  	kfree(bo);
  }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  	if (bp->type == ttm_bo_type_device)
  		bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
 
+	INIT_LIST_HEAD(&bo->bo);
+
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_add_tail(&bo->bo, &adev->device_bo_list);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
  	return 0;
 
  fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index 9ac3756..5ae8555 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo { · line 117

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
@@ -110,6 +110,8 @@ struct amdgpu_bo {
  	struct list_head		shadow_list;
 
  	struct kgd_mem                  *kfd_bo;
+
+	struct list_head		bo;
  };
 
  static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#20 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 69 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 55 lines

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
  6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
  #include <linux/dma-fence.h>
  #include <linux/pci.h>
  #include <linux/aer.h>
+#include <linux/notifier.h>
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
  	bool                            in_pci_err_recovery;
  	struct pci_saved_state          *pci_state;
+
+	struct notifier_block		nb;
+	struct blocking_notifier_head	notifier;
+	struct list_head		device_bo_list;
  };
  static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
  #include <drm/task_barrier.h>
  #include <linux/pm_runtime.h>
+#include <linux/iommu.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
  };
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+				     unsigned long action, void *data)
+{
+	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+	struct amdgpu_bo *bo = NULL;
+
+	/*
+	 * Following is a set of IOMMU group dependencies taken care of before

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static const struct attribute *amdgpu_dev_attributes[] = { · line 3220

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+	 * device's IOMMU group is removed
+	 */
+	if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+		spin_lock(&ttm_bo_glob.lru_lock);
+		list_for_each_entry(bo, &adev->device_bo_list, bo) {
+			if (bo->tbo.ttm)
+				ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+		}
+		spin_unlock(&ttm_bo_glob.lru_lock);

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

Quoted 2 lines

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

I'd also be worried about the notifier mutex getting really badly in the way.

Plus I'm worried why we even need this, it sounds a bit like papering over the iommu subsystem. Assuming we clean up all our iommu mappings in our device hotunplug/unload code, why do we still need to have an additional iommu notifier on top, with all kinds of additional headaches? The iommu shouldn't clean up before the devices in its group have cleaned up.

I think we need more info here on what the exact problem is first. -Daniel

Quoted 103 lines

Christian.

Quoted 99 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+		if (adev->irq.ih.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+		if (adev->irq.ih1.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+		if (adev->irq.ih2.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+		amdgpu_gart_dummy_page_fini(adev);
+	}
+
+	return NOTIFY_OK;
+}
+
+
  /**
   * amdgpu_device_init - initialize the driver
   *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  	INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+	INIT_LIST_HEAD(&adev->device_bo_list);
+
  	adev->gfx.gfx_off_req_count = 1;
  	adev->pm.ac_power = power_supply_is_system_supplied() > 0;
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  	if (amdgpu_device_cache_pci_state(adev->pdev))
  		pci_restore_state(pdev);
+	BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+	adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+	if (adev->dev->iommu_group) {
+		r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+		if (r)
+			goto failed;
+	}
+
  	return 0;
  failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
   *
   * Frees the dummy page used by the driver (all asics).
   */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
  {
  	if (!adev->dummy_page_addr)
  		return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
  void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
  int amdgpu_gart_init(struct amdgpu_device *adev);
  void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
  int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
  		       int pages);
  int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
  	}
  	amdgpu_bo_unref(&bo->parent);
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_del(&bo->bo);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
  	kfree(bo->metadata);
  	kfree(bo);
  }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  	if (bp->type == ttm_bo_type_device)
  		bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+	INIT_LIST_HEAD(&bo->bo);
+
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_add_tail(&bo->bo, &adev->device_bo_list);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
  	return 0;
  fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
  	struct list_head		shadow_list;
  	struct kgd_mem                  *kfd_bo;
+
+	struct list_head		bo;
  };
  static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#21 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 74 lines · Daniel Vetter
Quoted 68 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 55 lines

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
   6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
   #include <linux/aer.h>
+#include <linux/notifier.h>
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
   	bool                            in_pci_err_recovery;
   	struct pci_saved_state          *pci_state;
+
+	struct notifier_block		nb;
+	struct blocking_notifier_head	notifier;
+	struct list_head		device_bo_list;
   };
   static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
   #include <drm/task_barrier.h>
   #include <linux/pm_runtime.h>
+#include <linux/iommu.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
   };
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+				     unsigned long action, void *data)
+{
+	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+	struct amdgpu_bo *bo = NULL;
+
+	/*
+	 * Following is a set of IOMMU group dependencies taken care of before

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static const struct attribute *amdgpu_dev_attributes[] = { · line 3220

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+	 * device's IOMMU group is removed
+	 */
+	if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+		spin_lock(&ttm_bo_glob.lru_lock);
+		list_for_each_entry(bo, &adev->device_bo_list, bo) {
+			if (bo->tbo.ttm)
+				ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+		}
+		spin_unlock(&ttm_bo_glob.lru_lock);

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

Quoted 2 lines

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

I'd also be worried about the notifier mutex getting really badly in the way.

Quoted 8 lines

Plus I'm worried why we even need this, it sounds a bit like papering over the iommu subsystem. Assuming we clean up all our iommu mappings in our device hotunplug/unload code, why do we still need to have an additional iommu notifier on top, with all kinds of additional headaches? The iommu shouldn't clean up before the devices in its group have cleaned up.

I think we need more info here on what the exact problem is first. -Daniel

Originally I experienced the  crash bellow on IOMMU enabled device, it happens post device removal from PCI topology - during shutting down of user client holding last reference to drm device file (X in my case).
The crash is because by the time I get to this point struct device->iommu_group pointer is NULL already since the IOMMU group for the device is unset during PCI removal. So this contradicts what you said above that the iommu shouldn't clean up before the devices in its group have cleaned up.
So instead of guessing when is the right place to place all IOMMU related cleanups it makes sense to get notification from IOMMU subsystem in the form of event IOMMU_GROUP_NOTIFY_DEL_DEVICE
and use that place to do all the relevant cleanups.

Andrey

[  123.810074 <   28.126960>] BUG: kernel NULL pointer dereference, address: 00000000000000c8
[  123.810080 <    0.000006>] #PF: supervisor read access in kernel mode
[  123.810082 <    0.000002>] #PF: error_code(0x0000) - not-present page
[  123.810085 <    0.000003>] PGD 0 P4D 0
[  123.810089 <    0.000004>] Oops: 0000 [#1] SMP NOPTI
[  123.810094 <    0.000005>] CPU: 5 PID: 1418 Comm: Xorg:shlo4 Tainted: G           O      5.9.0-rc2-dev+ #59
[  123.810096 <    0.000002>] Hardware name: System manufacturer System Product Name/PRIME X470-PRO, BIOS 4406 02/28/2019
[  123.810105 <    0.000009>] *RIP: 0010:iommu_get_dma_domain*+0x10/0x20
[  123.810108 <    0.000003>] Code: b0 48 c7 87 98 00 00 00 00 00 00 00 31 c0 c3 b8 f4 ff ff ff eb a6 0f 1f 40 00 0f 1f 44 00 00 48 8b 87 d0 02 00 00 55 48 89 e5 <48> 8b 80 c8 00 00 00 5d c3 0f 1f 80 00 00 00 00 0f 1f 44 00 00 48 [  123.810111 <    0.000003>] RSP: 0018:ffffa2e201f7f980 EFLAGS: 00010246
[  123.810114 <    0.000003>] RAX: 0000000000000000 RBX: 0000000000001000 RCX: 0000000000000000
[  123.810116 <    0.000002>] RDX: 0000000000001000 RSI: 00000000bf5cb000 RDI: ffff93c259dc60b0
[  123.810117 <    0.000001>] RBP: ffffa2e201f7f980 R08: 0000000000000000 R09: 0000000000000000
[  123.810119 <    0.000002>] R10: ffffa2e201f7faf0 R11: 0000000000000001 R12: 00000000bf5cb000
[  123.810121 <    0.000002>] R13: 0000000000001000 R14: ffff93c24cef9c50 R15: ffff93c256c05688
[  123.810124 <    0.000003>] FS:  00007f5e5e8d3700(0000) GS:ffff93c25e940000(0000) knlGS:0000000000000000
[  123.810126 <    0.000002>] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  123.810128 <    0.000002>] CR2: 00000000000000c8 CR3: 000000027fe0a000 CR4: 00000000003506e0
[  123.810130 <    0.000002>] Call Trace:
[  123.810136 <    0.000006>]  __iommu_dma_unmap+0x2e/0x100
[  123.810141 <    0.000005>]  ? kfree+0x389/0x3a0
[  123.810144 <    0.000003>]  iommu_dma_unmap_page+0xe/0x10
[  123.810149 <    0.000005>] dma_unmap_page_attrs+0x4d/0xf0
[  123.810159 <    0.000010>]  ? ttm_bo_del_from_lru+0x8e/0xb0 [ttm]
[  123.810165 <    0.000006>] ttm_unmap_and_unpopulate_pages+0x8e/0xc0 [ttm]
[  123.810252 <    0.000087>] amdgpu_ttm_tt_unpopulate+0xaa/0xd0 [amdgpu]
[  123.810258 <    0.000006>]  ttm_tt_unpopulate+0x59/0x70 [ttm]
[  123.810264 <    0.000006>]  ttm_tt_destroy+0x6a/0x70 [ttm]
[  123.810270 <    0.000006>] ttm_bo_cleanup_memtype_use+0x36/0xa0 [ttm]
[  123.810276 <    0.000006>]  ttm_bo_put+0x1e7/0x400 [ttm]
[  123.810358 <    0.000082>]  amdgpu_bo_unref+0x1e/0x30 [amdgpu]
[  123.810440 <    0.000082>] amdgpu_gem_object_free+0x37/0x50 [amdgpu]
[  123.810459 <    0.000019>]  drm_gem_object_free+0x35/0x40 [drm]
[  123.810476 <    0.000017>] drm_gem_object_handle_put_unlocked+0x9d/0xd0 [drm]
[  123.810494 <    0.000018>] drm_gem_object_release_handle+0x74/0x90 [drm]
[  123.810511 <    0.000017>]  ? drm_gem_object_handle_put_unlocked+0xd0/0xd0 [drm]
[  123.810516 <    0.000005>]  idr_for_each+0x4d/0xd0
[  123.810534 <    0.000018>]  drm_gem_release+0x20/0x30 [drm]
[  123.810550 <    0.000016>]  drm_file_free+0x251/0x2a0 [drm]
[  123.810567 <    0.000017>] drm_close_helper.isra.14+0x61/0x70 [drm]
[  123.810583 <    0.000016>]  drm_release+0x6a/0xe0 [drm]
[  123.810588 <    0.000005>]  __fput+0xa2/0x250
[  123.810592 <    0.000004>]  ____fput+0xe/0x10
[  123.810595 <    0.000003>]  task_work_run+0x6c/0xa0
[  123.810600 <    0.000005>]  do_exit+0x376/0xb60
[  123.810604 <    0.000004>]  do_group_exit+0x43/0xa0
[  123.810608 <    0.000004>]  get_signal+0x18b/0x8e0
[  123.810612 <    0.000004>]  ? do_futex+0x595/0xc20
[  123.810617 <    0.000005>]  arch_do_signal+0x34/0x880
[  123.810620 <    0.000003>]  ? check_preempt_curr+0x50/0x60
[  123.810623 <    0.000003>]  ? ttwu_do_wakeup+0x1e/0x160
[  123.810626 <    0.000003>]  ? ttwu_do_activate+0x61/0x70
[  123.810630 <    0.000004>] exit_to_user_mode_prepare+0x124/0x1b0
[  123.810635 <    0.000005>] syscall_exit_to_user_mode+0x31/0x170
[  123.810639 <    0.000004>]  do_syscall_64+0x43/0x80

Andrey

Quoted 94 lines
Quoted 94 lines

Christian.

Quoted 91 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+		if (adev->irq.ih.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+		if (adev->irq.ih1.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+		if (adev->irq.ih2.use_bus_addr)
+			amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+		amdgpu_gart_dummy_page_fini(adev);
+	}
+
+	return NOTIFY_OK;
+}
+
+
   /**
    * amdgpu_device_init - initialize the driver
    *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   	INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+	INIT_LIST_HEAD(&adev->device_bo_list);
+
   	adev->gfx.gfx_off_req_count = 1;
   	adev->pm.ac_power = power_supply_is_system_supplied() > 0;
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   	if (amdgpu_device_cache_pci_state(adev->pdev))
   		pci_restore_state(pdev);
+	BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+	adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+	if (adev->dev->iommu_group) {
+		r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+		if (r)
+			goto failed;
+	}
+
   	return 0;
   failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
    *
    * Frees the dummy page used by the driver (all asics).
    */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
   {
   	if (!adev->dummy_page_addr)
   		return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
   void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
   int amdgpu_gart_init(struct amdgpu_device *adev);
   void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
   int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
   		       int pages);
   int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
   	}
   	amdgpu_bo_unref(&bo->parent);
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_del(&bo->bo);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
   	kfree(bo->metadata);
   	kfree(bo);
   }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
   	if (bp->type == ttm_bo_type_device)
   		bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+	INIT_LIST_HEAD(&bo->bo);
+
+	spin_lock(&ttm_bo_glob.lru_lock);
+	list_add_tail(&bo->bo, &adev->device_bo_list);
+	spin_unlock(&ttm_bo_glob.lru_lock);
+
   	return 0;
   fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -1
index 9ac3756..5ae8555 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo { · line 115

Quoted 8 lines

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
@@ -110,6 +110,8 @@ struct amdgpu_bo {
   	struct list_head		shadow_list;
   	struct kgd_mem                  *kfd_bo;
+
+	struct list_head		bo;
   };
   static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#22 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 92 lines · Andrey Grodzovsky

On 1/19/21 8:45 AM, Daniel Vetter wrote:

On Tue, Jan 19, 2021 at 09:48:03AM +0100, Christian König wrote:

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
  6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
  #include <linux/dma-fence.h>
  #include <linux/pci.h>
  #include <linux/aer.h>
+#include <linux/notifier.h>
  #include <drm/ttm/ttm_bo_api.h>
  #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
  bool                            in_pci_err_recovery;
  struct pci_saved_state          *pci_state;
+
+ struct notifier_block nb;
+ struct blocking_notifier_head notifier;
+ struct list_head device_bo_list;
  };
  static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
  #include <drm/task_barrier.h>
  #include <linux/pm_runtime.h>
+#include <linux/iommu.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
  };
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+     unsigned long action, void *data)
+{
+ struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+ struct amdgpu_bo *bo = NULL;
+
+ /*
+ * Following is a set of IOMMU group dependencies taken care of before
+ * device's IOMMU group is removed
+ */
+ if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_for_each_entry(bo, &adev->device_bo_list, bo) {
+ if (bo->tbo.ttm)
+ ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+ }
+ spin_unlock(&ttm_bo_glob.lru_lock);
 

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

I'd also be worried about the notifier mutex getting really badly in the way.

Plus I'm worried why we even need this, it sounds a bit like papering over the iommu subsystem. Assuming we clean up all our iommu mappings in our device hotunplug/unload code, why do we still need to have an additional iommu notifier on top, with all kinds of additional headaches? The iommu shouldn't clean up before the devices in its group have cleaned up.

I think we need more info here on what the exact problem is first. -Daniel

Quoted 8 lines

Originally I experienced the crash bellow on IOMMU enabled device, it happens post device removal from PCI topology - during shutting down of user client holding last reference to drm device file (X in my case).
The crash is because by the time I get to this point struct device->iommu_group pointer is NULL already since the IOMMU group for the device is unset during PCI removal. So this contradicts what you said above that the iommu shouldn't clean up before the devices in its group have cleaned up.
So instead of guessing when is the right place to place all IOMMU related cleanups it makes sense to get notification from IOMMU subsystem in the form of event IOMMU_GROUP_NOTIFY_DEL_DEVICE
and use that place to do all the relevant cleanups.

Yeah that goes boom, but you shouldn't need this special iommu cleanup handler. Making sure that all the dma-api mappings are gone needs to be done as part of the device hotunplug, you can't delay that to the last drm_device cleanup.

So I most of the patch here with pulling that out (should be outright removed from the final release code even) is good, just not yet how you call that new code. Probably these bits (aside from walking all buffers and unpopulating the tt) should be done from the early_free callback you're adding.

Also what I just realized: For normal unload you need to make sure the hw is actually stopped first, before we unmap buffers. Otherwise driver unload will likely result in wedged hw, probably not what you want for debugging.
-Daniel

Quoted 165 lines

Andrey

[  123.810074 <   28.126960>] BUG: kernel NULL pointer dereference, address: 00000000000000c8
[  123.810080 <    0.000006>] #PF: supervisor read access in kernel mode
[  123.810082 <    0.000002>] #PF: error_code(0x0000) - not-present page
[  123.810085 <    0.000003>] PGD 0 P4D 0
[  123.810089 <    0.000004>] Oops: 0000 [#1] SMP NOPTI
[  123.810094 <    0.000005>] CPU: 5 PID: 1418 Comm: Xorg:shlo4 Tainted: G           O      5.9.0-rc2-dev+ #59
[  123.810096 <    0.000002>] Hardware name: System manufacturer System Product Name/PRIME X470-PRO, BIOS 4406 02/28/2019
[  123.810105 <    0.000009>] RIP: 0010:iommu_get_dma_domain+0x10/0x20
[  123.810108 <    0.000003>] Code: b0 48 c7 87 98 00 00 00 00 00 00 00 31 c0 c3 b8 f4 ff ff ff eb a6 0f 1f 40 00 0f 1f 44 00 00 48 8b 87 d0 02 00 00 55 48 89 e5 <48> 8b 80 c8 00 00 00 5d c3 0f 1f 80 00 00 00 00 0f 1f 44 00 00 48
[  123.810111 <    0.000003>] RSP: 0018:ffffa2e201f7f980 EFLAGS: 00010246
[  123.810114 <    0.000003>] RAX: 0000000000000000 RBX: 0000000000001000 RCX: 0000000000000000
[  123.810116 <    0.000002>] RDX: 0000000000001000 RSI: 00000000bf5cb000 RDI: ffff93c259dc60b0
[  123.810117 <    0.000001>] RBP: ffffa2e201f7f980 R08: 0000000000000000 R09: 0000000000000000
[  123.810119 <    0.000002>] R10: ffffa2e201f7faf0 R11: 0000000000000001 R12: 00000000bf5cb000
[  123.810121 <    0.000002>] R13: 0000000000001000 R14: ffff93c24cef9c50 R15: ffff93c256c05688
[  123.810124 <    0.000003>] FS:  00007f5e5e8d3700(0000) GS:ffff93c25e940000(0000) knlGS:0000000000000000
[  123.810126 <    0.000002>] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  123.810128 <    0.000002>] CR2: 00000000000000c8 CR3: 000000027fe0a000 CR4: 00000000003506e0
[  123.810130 <    0.000002>] Call Trace:
[  123.810136 <    0.000006>]  __iommu_dma_unmap+0x2e/0x100
[  123.810141 <    0.000005>]  ? kfree+0x389/0x3a0
[  123.810144 <    0.000003>]  iommu_dma_unmap_page+0xe/0x10
[  123.810149 <    0.000005>] dma_unmap_page_attrs+0x4d/0xf0
[  123.810159 <    0.000010>]  ? ttm_bo_del_from_lru+0x8e/0xb0 [ttm]
[  123.810165 <    0.000006>] ttm_unmap_and_unpopulate_pages+0x8e/0xc0 [ttm]
[  123.810252 <    0.000087>] amdgpu_ttm_tt_unpopulate+0xaa/0xd0 [amdgpu]
[  123.810258 <    0.000006>]  ttm_tt_unpopulate+0x59/0x70 [ttm]
[  123.810264 <    0.000006>]  ttm_tt_destroy+0x6a/0x70 [ttm]
[  123.810270 <    0.000006>] ttm_bo_cleanup_memtype_use+0x36/0xa0 [ttm]
[  123.810276 <    0.000006>]  ttm_bo_put+0x1e7/0x400 [ttm]
[  123.810358 <    0.000082>]  amdgpu_bo_unref+0x1e/0x30 [amdgpu]
[  123.810440 <    0.000082>] amdgpu_gem_object_free+0x37/0x50 [amdgpu]
[  123.810459 <    0.000019>]  drm_gem_object_free+0x35/0x40 [drm]
[  123.810476 <    0.000017>] drm_gem_object_handle_put_unlocked+0x9d/0xd0 [drm]
[  123.810494 <    0.000018>] drm_gem_object_release_handle+0x74/0x90 [drm]
[  123.810511 <    0.000017>]  ? drm_gem_object_handle_put_unlocked+0xd0/0xd0 [drm]
[  123.810516 <    0.000005>]  idr_for_each+0x4d/0xd0
[  123.810534 <    0.000018>]  drm_gem_release+0x20/0x30 [drm]
[  123.810550 <    0.000016>]  drm_file_free+0x251/0x2a0 [drm]
[  123.810567 <    0.000017>] drm_close_helper.isra.14+0x61/0x70 [drm]
[  123.810583 <    0.000016>]  drm_release+0x6a/0xe0 [drm]
[  123.810588 <    0.000005>]  __fput+0xa2/0x250
[  123.810592 <    0.000004>]  ____fput+0xe/0x10
[  123.810595 <    0.000003>]  task_work_run+0x6c/0xa0
[  123.810600 <    0.000005>]  do_exit+0x376/0xb60
[  123.810604 <    0.000004>]  do_group_exit+0x43/0xa0
[  123.810608 <    0.000004>]  get_signal+0x18b/0x8e0
[  123.810612 <    0.000004>]  ? do_futex+0x595/0xc20
[  123.810617 <    0.000005>]  arch_do_signal+0x34/0x880
[  123.810620 <    0.000003>]  ? check_preempt_curr+0x50/0x60
[  123.810623 <    0.000003>]  ? ttwu_do_wakeup+0x1e/0x160
[  123.810626 <    0.000003>]  ? ttwu_do_activate+0x61/0x70
[  123.810630 <    0.000004>] exit_to_user_mode_prepare+0x124/0x1b0
[  123.810635 <    0.000005>] syscall_exit_to_user_mode+0x31/0x170
[  123.810639 <    0.000004>]  do_syscall_64+0x43/0x80

Andrey

Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+ if (adev->irq.ih.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+ if (adev->irq.ih1.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+ if (adev->irq.ih2.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+ amdgpu_gart_dummy_page_fini(adev);
+ }
+
+ return NOTIFY_OK;
+}
+
+
  /**
   * amdgpu_device_init - initialize the driver
   *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+ INIT_LIST_HEAD(&adev->device_bo_list);
+
  adev->gfx.gfx_off_req_count = 1;
  adev->pm.ac_power = power_supply_is_system_supplied() > 0;
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
  if (amdgpu_device_cache_pci_state(adev->pdev))
  pci_restore_state(pdev);
+ BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+ adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+ if (adev->dev->iommu_group) {
+ r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+ if (r)
+ goto failed;
+ }
+
  return 0;
  failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
   *
   * Frees the dummy page used by the driver (all asics).
   */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
  {
  if (!adev->dummy_page_addr)
  return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
  void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
  int amdgpu_gart_init(struct amdgpu_device *adev);
  void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
  int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
        int pages);
  int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
  }
  amdgpu_bo_unref(&bo->parent);
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_del(&bo->bo);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
  kfree(bo->metadata);
  kfree(bo);
  }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
  if (bp->type == ttm_bo_type_device)
  bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+ INIT_LIST_HEAD(&bo->bo);
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_add_tail(&bo->bo, &adev->device_bo_list);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
  return 0;
  fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
  struct list_head shadow_list;
  struct kgd_mem                  *kfd_bo;
+
+ struct list_head bo;
  };
  static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#23 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 112 lines · Daniel Vetter
Quoted 91 lines · Andrey Grodzovsky

On 1/19/21 8:45 AM, Daniel Vetter wrote:

On Tue, Jan 19, 2021 at 09:48:03AM +0100, Christian König wrote:

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
   6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
   #include <linux/aer.h>
+#include <linux/notifier.h>
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
   bool                            in_pci_err_recovery;
   struct pci_saved_state          *pci_state;
+
+ struct notifier_block nb;
+ struct blocking_notifier_head notifier;
+ struct list_head device_bo_list;
   };
   static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
   #include <drm/task_barrier.h>
   #include <linux/pm_runtime.h>
+#include <linux/iommu.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
   };
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+     unsigned long action, void *data)
+{
+ struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+ struct amdgpu_bo *bo = NULL;
+
+ /*
+ * Following is a set of IOMMU group dependencies taken care of before
+ * device's IOMMU group is removed
+ */
+ if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_for_each_entry(bo, &adev->device_bo_list, bo) {
+ if (bo->tbo.ttm)
+ ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+ }
+ spin_unlock(&ttm_bo_glob.lru_lock);
 

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

I'd also be worried about the notifier mutex getting really badly in the way.

Plus I'm worried why we even need this, it sounds a bit like papering over the iommu subsystem. Assuming we clean up all our iommu mappings in our device hotunplug/unload code, why do we still need to have an additional iommu notifier on top, with all kinds of additional headaches? The iommu shouldn't clean up before the devices in its group have cleaned up.

I think we need more info here on what the exact problem is first. -Daniel

Quoted 8 lines

Originally I experienced the crash bellow on IOMMU enabled device, it happens post device removal from PCI topology - during shutting down of user client holding last reference to drm device file (X in my case).
The crash is because by the time I get to this point struct device->iommu_group pointer is NULL already since the IOMMU group for the device is unset during PCI removal. So this contradicts what you said above that the iommu shouldn't clean up before the devices in its group have cleaned up.
So instead of guessing when is the right place to place all IOMMU related cleanups it makes sense to get notification from IOMMU subsystem in the form of event IOMMU_GROUP_NOTIFY_DEL_DEVICE
and use that place to do all the relevant cleanups.

Yeah that goes boom, but you shouldn't need this special iommu cleanup handler. Making sure that all the dma-api mappings are gone needs to be done as part of the device hotunplug, you can't delay that to the last drm_device cleanup.

So I most of the patch here with pulling that out (should be outright removed from the final release code even) is good, just not yet how you call that new code. Probably these bits (aside from walking all buffers and unpopulating the tt) should be done from the early_free callback you're adding.

Quoted 5 lines

Also what I just realized: For normal unload you need to make sure the hw is actually stopped first, before we unmap buffers. Otherwise driver unload will likely result in wedged hw, probably not what you want for debugging.
-Daniel

Since device removal from IOMMU group and this hook in particular takes place before call to amdgpu_pci_remove essentially it means that for IOMMU use case the entire amdgpu_device_fini_hw function shouold be called here to stop the HW instead from amdgpu_pci_remove.

Looking at this from another perspective, AFAIK on each new device probing either due to PCI bus rescan or driver reload we are resetting the ASIC before doing any init operations (assuming we successfully gained MMIO access) and so maybe your concern is not an issue ?

Andrey

Quoted 158 lines
Quoted 158 lines

Andrey

[  123.810074 <   28.126960>] BUG: kernel NULL pointer dereference, address: 00000000000000c8
[  123.810080 <    0.000006>] #PF: supervisor read access in kernel mode
[  123.810082 <    0.000002>] #PF: error_code(0x0000) - not-present page
[  123.810085 <    0.000003>] PGD 0 P4D 0
[  123.810089 <    0.000004>] Oops: 0000 [#1] SMP NOPTI
[  123.810094 <    0.000005>] CPU: 5 PID: 1418 Comm: Xorg:shlo4 Tainted: G           O      5.9.0-rc2-dev+ #59
[  123.810096 <    0.000002>] Hardware name: System manufacturer System Product Name/PRIME X470-PRO, BIOS 4406 02/28/2019
[  123.810105 <    0.000009>] RIP: 0010:iommu_get_dma_domain+0x10/0x20
[  123.810108 <    0.000003>] Code: b0 48 c7 87 98 00 00 00 00 00 00 00 31 c0 c3 b8 f4 ff ff ff eb a6 0f 1f 40 00 0f 1f 44 00 00 48 8b 87 d0 02 00 00 55 48 89 e5 <48> 8b 80 c8 00 00 00 5d c3 0f 1f 80 00 00 00 00 0f 1f 44 00 00 48
[  123.810111 <    0.000003>] RSP: 0018:ffffa2e201f7f980 EFLAGS: 00010246
[  123.810114 <    0.000003>] RAX: 0000000000000000 RBX: 0000000000001000 RCX: 0000000000000000
[  123.810116 <    0.000002>] RDX: 0000000000001000 RSI: 00000000bf5cb000 RDI: ffff93c259dc60b0
[  123.810117 <    0.000001>] RBP: ffffa2e201f7f980 R08: 0000000000000000 R09: 0000000000000000
[  123.810119 <    0.000002>] R10: ffffa2e201f7faf0 R11: 0000000000000001 R12: 00000000bf5cb000
[  123.810121 <    0.000002>] R13: 0000000000001000 R14: ffff93c24cef9c50 R15: ffff93c256c05688
[  123.810124 <    0.000003>] FS:  00007f5e5e8d3700(0000) GS:ffff93c25e940000(0000) knlGS:0000000000000000
[  123.810126 <    0.000002>] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  123.810128 <    0.000002>] CR2: 00000000000000c8 CR3: 000000027fe0a000 CR4: 00000000003506e0
[  123.810130 <    0.000002>] Call Trace:
[  123.810136 <    0.000006>]  __iommu_dma_unmap+0x2e/0x100
[  123.810141 <    0.000005>]  ? kfree+0x389/0x3a0
[  123.810144 <    0.000003>]  iommu_dma_unmap_page+0xe/0x10
[  123.810149 <    0.000005>] dma_unmap_page_attrs+0x4d/0xf0
[  123.810159 <    0.000010>]  ? ttm_bo_del_from_lru+0x8e/0xb0 [ttm]
[  123.810165 <    0.000006>] ttm_unmap_and_unpopulate_pages+0x8e/0xc0 [ttm]
[  123.810252 <    0.000087>] amdgpu_ttm_tt_unpopulate+0xaa/0xd0 [amdgpu]
[  123.810258 <    0.000006>]  ttm_tt_unpopulate+0x59/0x70 [ttm]
[  123.810264 <    0.000006>]  ttm_tt_destroy+0x6a/0x70 [ttm]
[  123.810270 <    0.000006>] ttm_bo_cleanup_memtype_use+0x36/0xa0 [ttm]
[  123.810276 <    0.000006>]  ttm_bo_put+0x1e7/0x400 [ttm]
[  123.810358 <    0.000082>]  amdgpu_bo_unref+0x1e/0x30 [amdgpu]
[  123.810440 <    0.000082>] amdgpu_gem_object_free+0x37/0x50 [amdgpu]
[  123.810459 <    0.000019>]  drm_gem_object_free+0x35/0x40 [drm]
[  123.810476 <    0.000017>] drm_gem_object_handle_put_unlocked+0x9d/0xd0 [drm]
[  123.810494 <    0.000018>] drm_gem_object_release_handle+0x74/0x90 [drm]
[  123.810511 <    0.000017>]  ? drm_gem_object_handle_put_unlocked+0xd0/0xd0 [drm]
[  123.810516 <    0.000005>]  idr_for_each+0x4d/0xd0
[  123.810534 <    0.000018>]  drm_gem_release+0x20/0x30 [drm]
[  123.810550 <    0.000016>]  drm_file_free+0x251/0x2a0 [drm]
[  123.810567 <    0.000017>] drm_close_helper.isra.14+0x61/0x70 [drm]
[  123.810583 <    0.000016>]  drm_release+0x6a/0xe0 [drm]
[  123.810588 <    0.000005>]  __fput+0xa2/0x250
[  123.810592 <    0.000004>]  ____fput+0xe/0x10
[  123.810595 <    0.000003>]  task_work_run+0x6c/0xa0
[  123.810600 <    0.000005>]  do_exit+0x376/0xb60
[  123.810604 <    0.000004>]  do_group_exit+0x43/0xa0
[  123.810608 <    0.000004>]  get_signal+0x18b/0x8e0
[  123.810612 <    0.000004>]  ? do_futex+0x595/0xc20
[  123.810617 <    0.000005>]  arch_do_signal+0x34/0x880
[  123.810620 <    0.000003>]  ? check_preempt_curr+0x50/0x60
[  123.810623 <    0.000003>]  ? ttwu_do_wakeup+0x1e/0x160
[  123.810626 <    0.000003>]  ? ttwu_do_activate+0x61/0x70
[  123.810630 <    0.000004>] exit_to_user_mode_prepare+0x124/0x1b0
[  123.810635 <    0.000005>] syscall_exit_to_user_mode+0x31/0x170
[  123.810639 <    0.000004>]  do_syscall_64+0x43/0x80

Andrey

Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+ if (adev->irq.ih.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+ if (adev->irq.ih1.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+ if (adev->irq.ih2.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+ amdgpu_gart_dummy_page_fini(adev);
+ }
+
+ return NOTIFY_OK;
+}
+
+
   /**
    * amdgpu_device_init - initialize the driver
    *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+ INIT_LIST_HEAD(&adev->device_bo_list);
+
   adev->gfx.gfx_off_req_count = 1;
   adev->pm.ac_power = power_supply_is_system_supplied() > 0;
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   if (amdgpu_device_cache_pci_state(adev->pdev))
   pci_restore_state(pdev);
+ BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+ adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+ if (adev->dev->iommu_group) {
+ r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+ if (r)
+ goto failed;
+ }
+
   return 0;
   failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
    *
    * Frees the dummy page used by the driver (all asics).
    */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
   {
   if (!adev->dummy_page_addr)
   return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
   void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
   int amdgpu_gart_init(struct amdgpu_device *adev);
   void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
   int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
         int pages);
   int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
   }
   amdgpu_bo_unref(&bo->parent);
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_del(&bo->bo);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
   kfree(bo->metadata);
   kfree(bo);
   }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
   if (bp->type == ttm_bo_type_device)
   bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+ INIT_LIST_HEAD(&bo->bo);
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_add_tail(&bo->bo, &adev->device_bo_list);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
   return 0;
   fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -1
index 9ac3756..5ae8555 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo { · line 115

Quoted 8 lines

+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.h

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
@@ -110,6 +110,8 @@ struct amdgpu_bo {
   struct list_head shadow_list;
   struct kgd_mem                  *kfd_bo;
+
+ struct list_head bo;
   };
   static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#24 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 121 lines · Andrey Grodzovsky
Quoted 112 lines · Daniel Vetter
Quoted 91 lines · Andrey Grodzovsky

On 1/19/21 8:45 AM, Daniel Vetter wrote:

On Tue, Jan 19, 2021 at 09:48:03AM +0100, Christian König wrote:

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
   drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
   6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
   #include <linux/aer.h>
+#include <linux/notifier.h>
   #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
   bool                            in_pci_err_recovery;
   struct pci_saved_state          *pci_state;
+
+ struct notifier_block nb;
+ struct blocking_notifier_head notifier;
+ struct list_head device_bo_list;
   };
   static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
   #include <drm/task_barrier.h>
   #include <linux/pm_runtime.h>
+#include <linux/iommu.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
   };
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+     unsigned long action, void *data)
+{
+ struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+ struct amdgpu_bo *bo = NULL;
+
+ /*
+ * Following is a set of IOMMU group dependencies taken care of before
+ * device's IOMMU group is removed
+ */
+ if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_for_each_entry(bo, &adev->device_bo_list, bo) {
+ if (bo->tbo.ttm)
+ ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+ }
+ spin_unlock(&ttm_bo_glob.lru_lock);
 

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

I'd also be worried about the notifier mutex getting really badly in the way.

Plus I'm worried why we even need this, it sounds a bit like papering over the iommu subsystem. Assuming we clean up all our iommu mappings in our device hotunplug/unload code, why do we still need to have an additional iommu notifier on top, with all kinds of additional headaches? The iommu shouldn't clean up before the devices in its group have cleaned up.

I think we need more info here on what the exact problem is first. -Daniel

Quoted 8 lines

Originally I experienced the crash bellow on IOMMU enabled device, it happens post device removal from PCI topology - during shutting down of user client holding last reference to drm device file (X in my case).
The crash is because by the time I get to this point struct device->iommu_group pointer is NULL already since the IOMMU group for the device is unset during PCI removal. So this contradicts what you said above that the iommu shouldn't clean up before the devices in its group have cleaned up.
So instead of guessing when is the right place to place all IOMMU related cleanups it makes sense to get notification from IOMMU subsystem in the form of event IOMMU_GROUP_NOTIFY_DEL_DEVICE
and use that place to do all the relevant cleanups.

Yeah that goes boom, but you shouldn't need this special iommu cleanup handler. Making sure that all the dma-api mappings are gone needs to be done as part of the device hotunplug, you can't delay that to the last drm_device cleanup.

So I most of the patch here with pulling that out (should be outright removed from the final release code even) is good, just not yet how you call that new code. Probably these bits (aside from walking all buffers and unpopulating the tt) should be done from the early_free callback you're adding.

Quoted 5 lines

Also what I just realized: For normal unload you need to make sure the hw is actually stopped first, before we unmap buffers. Otherwise driver unload will likely result in wedged hw, probably not what you want for debugging.
-Daniel

Quoted 4 lines

Since device removal from IOMMU group and this hook in particular takes place before call to amdgpu_pci_remove essentially it means that for IOMMU use case the entire amdgpu_device_fini_hw function shouold be called here to stop the HW instead from amdgpu_pci_remove.

The crash you showed was on final drm_close, which should happen after device removal, so that's clearly buggy. If the iommu subsystem removes stuff before the driver could clean up already, then I think that's an iommu bug or dma-api bug. Just plain using dma_map/unmap and friends really shouldn't need notifier hacks like you're implementing here. Can you pls show me a backtrace where dma_unmap_sg blows up when it's put into the pci_driver remove callback?

Quoted 4 lines

Looking at this from another perspective, AFAIK on each new device probing either due to PCI bus rescan or driver reload we are resetting the ASIC before doing any init operations (assuming we successfully gained MMIO access) and so maybe your concern is not an issue ?

Reset on probe is too late. The problem is that if you just remove the driver, your device is doing dma at that moment. And you kinda have to stop that before you free the mappings/memory. Of course when the device is actually hotunplugged, then dma is guaranteed to have stopped already. I'm not sure whether disabling the pci device is enough to make sure no more dma happens, could be that's enough. -Daniel

Quoted 171 lines

Andrey

Quoted 168 lines
Quoted 166 lines

Andrey

[  123.810074 <   28.126960>] BUG: kernel NULL pointer dereference, address: 00000000000000c8
[  123.810080 <    0.000006>] #PF: supervisor read access in kernel mode
[  123.810082 <    0.000002>] #PF: error_code(0x0000) - not-present page
[  123.810085 <    0.000003>] PGD 0 P4D 0
[  123.810089 <    0.000004>] Oops: 0000 [#1] SMP NOPTI
[  123.810094 <    0.000005>] CPU: 5 PID: 1418 Comm: Xorg:shlo4 Tainted: G           O      5.9.0-rc2-dev+ #59
[  123.810096 <    0.000002>] Hardware name: System manufacturer System Product Name/PRIME X470-PRO, BIOS 4406 02/28/2019
[  123.810105 <    0.000009>] RIP: 0010:iommu_get_dma_domain+0x10/0x20
[  123.810108 <    0.000003>] Code: b0 48 c7 87 98 00 00 00 00 00 00 00 31 c0 c3 b8 f4 ff ff ff eb a6 0f 1f 40 00 0f 1f 44 00 00 48 8b 87 d0 02 00 00 55 48 89 e5 <48> 8b 80 c8 00 00 00 5d c3 0f 1f 80 00 00 00 00 0f 1f 44 00 00 48
[  123.810111 <    0.000003>] RSP: 0018:ffffa2e201f7f980 EFLAGS: 00010246
[  123.810114 <    0.000003>] RAX: 0000000000000000 RBX: 0000000000001000 RCX: 0000000000000000
[  123.810116 <    0.000002>] RDX: 0000000000001000 RSI: 00000000bf5cb000 RDI: ffff93c259dc60b0
[  123.810117 <    0.000001>] RBP: ffffa2e201f7f980 R08: 0000000000000000 R09: 0000000000000000
[  123.810119 <    0.000002>] R10: ffffa2e201f7faf0 R11: 0000000000000001 R12: 00000000bf5cb000
[  123.810121 <    0.000002>] R13: 0000000000001000 R14: ffff93c24cef9c50 R15: ffff93c256c05688
[  123.810124 <    0.000003>] FS:  00007f5e5e8d3700(0000) GS:ffff93c25e940000(0000) knlGS:0000000000000000
[  123.810126 <    0.000002>] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  123.810128 <    0.000002>] CR2: 00000000000000c8 CR3: 000000027fe0a000 CR4: 00000000003506e0
[  123.810130 <    0.000002>] Call Trace:
[  123.810136 <    0.000006>]  __iommu_dma_unmap+0x2e/0x100
[  123.810141 <    0.000005>]  ? kfree+0x389/0x3a0
[  123.810144 <    0.000003>]  iommu_dma_unmap_page+0xe/0x10
[  123.810149 <    0.000005>] dma_unmap_page_attrs+0x4d/0xf0
[  123.810159 <    0.000010>]  ? ttm_bo_del_from_lru+0x8e/0xb0 [ttm]
[  123.810165 <    0.000006>] ttm_unmap_and_unpopulate_pages+0x8e/0xc0 [ttm]
[  123.810252 <    0.000087>] amdgpu_ttm_tt_unpopulate+0xaa/0xd0 [amdgpu]
[  123.810258 <    0.000006>]  ttm_tt_unpopulate+0x59/0x70 [ttm]
[  123.810264 <    0.000006>]  ttm_tt_destroy+0x6a/0x70 [ttm]
[  123.810270 <    0.000006>] ttm_bo_cleanup_memtype_use+0x36/0xa0 [ttm]
[  123.810276 <    0.000006>]  ttm_bo_put+0x1e7/0x400 [ttm]
[  123.810358 <    0.000082>]  amdgpu_bo_unref+0x1e/0x30 [amdgpu]
[  123.810440 <    0.000082>] amdgpu_gem_object_free+0x37/0x50 [amdgpu]
[  123.810459 <    0.000019>]  drm_gem_object_free+0x35/0x40 [drm]
[  123.810476 <    0.000017>] drm_gem_object_handle_put_unlocked+0x9d/0xd0 [drm]
[  123.810494 <    0.000018>] drm_gem_object_release_handle+0x74/0x90 [drm]
[  123.810511 <    0.000017>]  ? drm_gem_object_handle_put_unlocked+0xd0/0xd0 [drm]
[  123.810516 <    0.000005>]  idr_for_each+0x4d/0xd0
[  123.810534 <    0.000018>]  drm_gem_release+0x20/0x30 [drm]
[  123.810550 <    0.000016>]  drm_file_free+0x251/0x2a0 [drm]
[  123.810567 <    0.000017>] drm_close_helper.isra.14+0x61/0x70 [drm]
[  123.810583 <    0.000016>]  drm_release+0x6a/0xe0 [drm]
[  123.810588 <    0.000005>]  __fput+0xa2/0x250
[  123.810592 <    0.000004>]  ____fput+0xe/0x10
[  123.810595 <    0.000003>]  task_work_run+0x6c/0xa0
[  123.810600 <    0.000005>]  do_exit+0x376/0xb60
[  123.810604 <    0.000004>]  do_group_exit+0x43/0xa0
[  123.810608 <    0.000004>]  get_signal+0x18b/0x8e0
[  123.810612 <    0.000004>]  ? do_futex+0x595/0xc20
[  123.810617 <    0.000005>]  arch_do_signal+0x34/0x880
[  123.810620 <    0.000003>]  ? check_preempt_curr+0x50/0x60
[  123.810623 <    0.000003>]  ? ttwu_do_wakeup+0x1e/0x160
[  123.810626 <    0.000003>]  ? ttwu_do_activate+0x61/0x70
[  123.810630 <    0.000004>] exit_to_user_mode_prepare+0x124/0x1b0
[  123.810635 <    0.000005>] syscall_exit_to_user_mode+0x31/0x170
[  123.810639 <    0.000004>]  do_syscall_64+0x43/0x80

Andrey

Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+ if (adev->irq.ih.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+ if (adev->irq.ih1.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+ if (adev->irq.ih2.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+ amdgpu_gart_dummy_page_fini(adev);
+ }
+
+ return NOTIFY_OK;
+}
+
+
   /**
    * amdgpu_device_init - initialize the driver
    *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+ INIT_LIST_HEAD(&adev->device_bo_list);
+
   adev->gfx.gfx_off_req_count = 1;
   adev->pm.ac_power = power_supply_is_system_supplied() > 0;
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
   if (amdgpu_device_cache_pci_state(adev->pdev))
   pci_restore_state(pdev);
+ BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+ adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+ if (adev->dev->iommu_group) {
+ r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+ if (r)
+ goto failed;
+ }
+
   return 0;
   failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
    *
    * Frees the dummy page used by the driver (all asics).
    */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
   {
   if (!adev->dummy_page_addr)
   return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
   void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
   int amdgpu_gart_init(struct amdgpu_device *adev);
   void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
   int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
         int pages);
   int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
   }
   amdgpu_bo_unref(&bo->parent);
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_del(&bo->bo);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
   kfree(bo->metadata);
   kfree(bo);
   }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
   if (bp->type == ttm_bo_type_device)
   bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+ INIT_LIST_HEAD(&bo->bo);
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_add_tail(&bo->bo, &adev->device_bo_list);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
   return 0;
   fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
   struct list_head shadow_list;
   struct kgd_mem                  *kfd_bo;
+
+ struct list_head bo;
   };
   static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): [email protected]

#26 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Quoted 136 lines · Andrey Grodzovsky
Quoted 124 lines · Daniel Vetter
Quoted 118 lines · Andrey Grodzovsky
Quoted 111 lines · Daniel Vetter
Quoted 90 lines · Andrey Grodzovsky

On 1/19/21 8:45 AM, Daniel Vetter wrote:

On Tue, Jan 19, 2021 at 09:48:03AM +0100, Christian König wrote:

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
    drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +-
    drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 +
    drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++
    drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++
    6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
    #include <linux/dma-fence.h>
    #include <linux/pci.h>
    #include <linux/aer.h>
+#include <linux/notifier.h>
    #include <drm/ttm/ttm_bo_api.h>
    #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
    bool                            in_pci_err_recovery;
    struct pci_saved_state          *pci_state;
+
+ struct notifier_block nb;
+ struct blocking_notifier_head notifier;
+ struct list_head device_bo_list;
    };
    static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +20 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
    #include <drm/task_barrier.h>
    #include <linux/pm_runtime.h>
+#include <linux/iommu.h>
+
    MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
    MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
    MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] = {
    };
+static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+     unsigned long action, void *data)
+{
+ struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+ struct amdgpu_bo *bo = NULL;
+
+ /*
+ * Following is a set of IOMMU group dependencies taken care of before
+ * device's IOMMU group is removed
+ */
+ if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_for_each_entry(bo, &adev->device_bo_list, bo) {
+ if (bo->tbo.ttm)
+ ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+ }
+ spin_unlock(&ttm_bo_glob.lru_lock);
 

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

I'd also be worried about the notifier mutex getting really badly in the way.

Plus I'm worried why we even need this, it sounds a bit like papering over the iommu subsystem. Assuming we clean up all our iommu mappings in our device hotunplug/unload code, why do we still need to have an additional iommu notifier on top, with all kinds of additional headaches? The iommu shouldn't clean up before the devices in its group have cleaned up.

I think we need more info here on what the exact problem is first. -Daniel

Quoted 8 lines

Originally I experienced the crash bellow on IOMMU enabled device, it happens post device removal from PCI topology - during shutting down of user client holding last reference to drm device file (X in my case).
The crash is because by the time I get to this point struct device->iommu_group pointer is NULL already since the IOMMU group for the device is unset during PCI removal. So this contradicts what you said above that the iommu shouldn't clean up before the devices in its group have cleaned up.
So instead of guessing when is the right place to place all IOMMU related cleanups it makes sense to get notification from IOMMU subsystem in the form of event IOMMU_GROUP_NOTIFY_DEL_DEVICE
and use that place to do all the relevant cleanups.

Yeah that goes boom, but you shouldn't need this special iommu cleanup handler. Making sure that all the dma-api mappings are gone needs to be done as part of the device hotunplug, you can't delay that to the last drm_device cleanup.

So I most of the patch here with pulling that out (should be outright removed from the final release code even) is good, just not yet how you call that new code. Probably these bits (aside from walking all buffers and unpopulating the tt) should be done from the early_free callback you're adding.

Quoted 5 lines

Also what I just realized: For normal unload you need to make sure the hw is actually stopped first, before we unmap buffers. Otherwise driver unload will likely result in wedged hw, probably not what you want for debugging.
-Daniel

Quoted 4 lines

Since device removal from IOMMU group and this hook in particular takes place before call to amdgpu_pci_remove essentially it means that for IOMMU use case the entire amdgpu_device_fini_hw function shouold be called here to stop the HW instead from amdgpu_pci_remove.

Quoted 7 lines

The crash you showed was on final drm_close, which should happen after device removal, so that's clearly buggy. If the iommu subsystem removes stuff before the driver could clean up already, then I think that's an iommu bug or dma-api bug. Just plain using dma_map/unmap and friends really shouldn't need notifier hacks like you're implementing here. Can you pls show me a backtrace where dma_unmap_sg blows up when it's put into the pci_driver remove callback?

Quoted 7 lines

It's not blowing up and has the same effect as using this notifier because setting of device->iommu_group pointer to NULL takes place at the same call stack but after amdgpu_pci_remove is called (see pci_stop_and_remove_bus_device). But i think that using notifier callback is better then just sticking the cleanup code in amdgpu_pci_remove because this is IOMMU specific cleanup and also coupled
in the code to the place where device->iommu_group is unset.

Notifiers are a locking pain, plus dma_unmap_* is really just plain normal driver cleanup work. If you want neat&automatic cleanup, look at devm_ family of functions. There's imo really no reason to have this notifier, and only reasons against it.

I think intermediately the cleanest solution is to put each cleanup into a corresponding hw_fini callback (early_free is maybe a bit ambiguous in what exactly it means).
-Daniel

Quoted 187 lines

Andrey

Quoted 184 lines
Quoted 5 lines

Looking at this from another perspective, AFAIK on each new device probing either due to PCI bus rescan or driver reload we are resetting the ASIC before doing any init operations (assuming we successfully gained MMIO access) and so maybe your concern is not an issue ?

Reset on probe is too late. The problem is that if you just remove the driver, your device is doing dma at that moment. And you kinda have to stop that before you free the mappings/memory. Of course when the device is actually hotunplugged, then dma is guaranteed to have stopped already. I'm not sure whether disabling the pci device is enough to make sure no more dma happens, could be that's enough. -Daniel

Quoted 169 lines

Andrey

Quoted 166 lines
Quoted 165 lines

Andrey

[  123.810074 <   28.126960>] BUG: kernel NULL pointer dereference, address: 00000000000000c8
[  123.810080 <    0.000006>] #PF: supervisor read access in kernel mode
[  123.810082 <    0.000002>] #PF: error_code(0x0000) - not-present page
[  123.810085 <    0.000003>] PGD 0 P4D 0
[  123.810089 <    0.000004>] Oops: 0000 [#1] SMP NOPTI
[  123.810094 <    0.000005>] CPU: 5 PID: 1418 Comm: Xorg:shlo4 Tainted: G           O      5.9.0-rc2-dev+ #59
[  123.810096 <    0.000002>] Hardware name: System manufacturer System Product Name/PRIME X470-PRO, BIOS 4406 02/28/2019
[  123.810105 <    0.000009>] RIP: 0010:iommu_get_dma_domain+0x10/0x20
[  123.810108 <    0.000003>] Code: b0 48 c7 87 98 00 00 00 00 00 00 00 31 c0 c3 b8 f4 ff ff ff eb a6 0f 1f 40 00 0f 1f 44 00 00 48 8b 87 d0 02 00 00 55 48 89 e5 <48> 8b 80 c8 00 00 00 5d c3 0f 1f 80 00 00 00 00 0f 1f 44 00 00 48
[  123.810111 <    0.000003>] RSP: 0018:ffffa2e201f7f980 EFLAGS: 00010246
[  123.810114 <    0.000003>] RAX: 0000000000000000 RBX: 0000000000001000 RCX: 0000000000000000
[  123.810116 <    0.000002>] RDX: 0000000000001000 RSI: 00000000bf5cb000 RDI: ffff93c259dc60b0
[  123.810117 <    0.000001>] RBP: ffffa2e201f7f980 R08: 0000000000000000 R09: 0000000000000000
[  123.810119 <    0.000002>] R10: ffffa2e201f7faf0 R11: 0000000000000001 R12: 00000000bf5cb000
[  123.810121 <    0.000002>] R13: 0000000000001000 R14: ffff93c24cef9c50 R15: ffff93c256c05688
[  123.810124 <    0.000003>] FS:  00007f5e5e8d3700(0000) GS:ffff93c25e940000(0000) knlGS:0000000000000000
[  123.810126 <    0.000002>] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  123.810128 <    0.000002>] CR2: 00000000000000c8 CR3: 000000027fe0a000 CR4: 00000000003506e0
[  123.810130 <    0.000002>] Call Trace:
[  123.810136 <    0.000006>]  __iommu_dma_unmap+0x2e/0x100
[  123.810141 <    0.000005>]  ? kfree+0x389/0x3a0
[  123.810144 <    0.000003>]  iommu_dma_unmap_page+0xe/0x10
[  123.810149 <    0.000005>] dma_unmap_page_attrs+0x4d/0xf0
[  123.810159 <    0.000010>]  ? ttm_bo_del_from_lru+0x8e/0xb0 [ttm]
[  123.810165 <    0.000006>] ttm_unmap_and_unpopulate_pages+0x8e/0xc0 [ttm]
[  123.810252 <    0.000087>] amdgpu_ttm_tt_unpopulate+0xaa/0xd0 [amdgpu]
[  123.810258 <    0.000006>]  ttm_tt_unpopulate+0x59/0x70 [ttm]
[  123.810264 <    0.000006>]  ttm_tt_destroy+0x6a/0x70 [ttm]
[  123.810270 <    0.000006>] ttm_bo_cleanup_memtype_use+0x36/0xa0 [ttm]
[  123.810276 <    0.000006>]  ttm_bo_put+0x1e7/0x400 [ttm]
[  123.810358 <    0.000082>]  amdgpu_bo_unref+0x1e/0x30 [amdgpu]
[  123.810440 <    0.000082>] amdgpu_gem_object_free+0x37/0x50 [amdgpu]
[  123.810459 <    0.000019>]  drm_gem_object_free+0x35/0x40 [drm]
[  123.810476 <    0.000017>] drm_gem_object_handle_put_unlocked+0x9d/0xd0 [drm]
[  123.810494 <    0.000018>] drm_gem_object_release_handle+0x74/0x90 [drm]
[  123.810511 <    0.000017>]  ? drm_gem_object_handle_put_unlocked+0xd0/0xd0 [drm]
[  123.810516 <    0.000005>]  idr_for_each+0x4d/0xd0
[  123.810534 <    0.000018>]  drm_gem_release+0x20/0x30 [drm]
[  123.810550 <    0.000016>]  drm_file_free+0x251/0x2a0 [drm]
[  123.810567 <    0.000017>] drm_close_helper.isra.14+0x61/0x70 [drm]
[  123.810583 <    0.000016>]  drm_release+0x6a/0xe0 [drm]
[  123.810588 <    0.000005>]  __fput+0xa2/0x250
[  123.810592 <    0.000004>]  ____fput+0xe/0x10
[  123.810595 <    0.000003>]  task_work_run+0x6c/0xa0
[  123.810600 <    0.000005>]  do_exit+0x376/0xb60
[  123.810604 <    0.000004>]  do_group_exit+0x43/0xa0
[  123.810608 <    0.000004>]  get_signal+0x18b/0x8e0
[  123.810612 <    0.000004>]  ? do_futex+0x595/0xc20
[  123.810617 <    0.000005>]  arch_do_signal+0x34/0x880
[  123.810620 <    0.000003>]  ? check_preempt_curr+0x50/0x60
[  123.810623 <    0.000003>]  ? ttwu_do_wakeup+0x1e/0x160
[  123.810626 <    0.000003>]  ? ttwu_do_activate+0x61/0x70
[  123.810630 <    0.000004>] exit_to_user_mode_prepare+0x124/0x1b0
[  123.810635 <    0.000005>] syscall_exit_to_user_mode+0x31/0x170
[  123.810639 <    0.000004>]  do_syscall_64+0x43/0x80

Andrey

Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +26 -0
+
+ if (adev->irq.ih.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+ if (adev->irq.ih1.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+ if (adev->irq.ih2.use_bus_addr)
+ amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+ amdgpu_gart_dummy_page_fini(adev);
+ }
+
+ return NOTIFY_OK;
+}
+
+
    /**
     * amdgpu_device_init - initialize the driver
     *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
    INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
+ INIT_LIST_HEAD(&adev->device_bo_list);
+
    adev->gfx.gfx_off_req_count = 1;
    adev->pm.ac_power = power_supply_is_system_supplied() > 0;
@@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
    if (amdgpu_device_cache_pci_state(adev->pdev))
    pci_restore_state(pdev);
+ BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+ adev->nb.notifier_call = amdgpu_iommu_group_notifier;
+
+ if (adev->dev->iommu_group) {
+ r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+ if (r)
+ goto failed;
+ }
+
    return 0;
    failed:
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
     *
     * Frees the dummy page used by the driver (all asics).
     */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
    {
    if (!adev->dummy_page_addr)
    return;
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
    void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
    int amdgpu_gart_init(struct amdgpu_device *adev);
    void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
    int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
          int pages);
    int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +10 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
    }
    amdgpu_bo_unref(&bo->parent);
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_del(&bo->bo);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
    kfree(bo->metadata);
    kfree(bo);
    }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
    if (bp->type == ttm_bo_type_device)
    bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
+ INIT_LIST_HEAD(&bo->bo);
+
+ spin_lock(&ttm_bo_glob.lru_lock);
+ list_add_tail(&bo->bo, &adev->device_bo_list);
+ spin_unlock(&ttm_bo_glob.lru_lock);
+
    return 0;
    fail_unreserve:
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
    struct list_head shadow_list;
    struct kgd_mem                  *kfd_bo;
+
+ struct list_head bo;
    };
    static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#27 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

In reply to Christian König
Quoted 70 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 56 lines

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46 ++++++++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 + drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++

  6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
   #include <linux/aer.h>
+#include <linux/notifier.h>
     #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
         bool                            in_pci_err_recovery;
       struct pci_saved_state          *pci_state;
+
+    struct notifier_block        nb;
+    struct blocking_notifier_head    notifier;
+    struct list_head        device_bo_list;
   };
     static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
   #include <drm/task_barrier.h>
   #include <linux/pm_runtime.h>

  +#include <linux/iommu.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -0
@@ -3200,6 +3202,39 @@ static const struct attribute *amdgpu_dev_attributes[] 
 = {
   };
     +static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+                     unsigned long action, void *data)
+{
+    struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+    struct amdgpu_bo *bo = NULL;
+
+    /*
+     * Following is a set of IOMMU group dependencies taken care of before

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static const struct attribute *amdgpu_dev_attributes[] · line 3221

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+     * device's IOMMU group is removed
+     */
+    if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+        spin_lock(&ttm_bo_glob.lru_lock);
+        list_for_each_entry(bo, &adev->device_bo_list, bo) {
+            if (bo->tbo.ttm)
+                ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+        }
+        spin_unlock(&ttm_bo_glob.lru_lock);

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

Quoted 3 lines

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

Christian.

I can think of switching to RCU list ? Otherwise, elements are added
on BO create and deleted on BO destroy, how can i prevent any of those from happening while in this section besides mutex ? Make a copy list and run over it instead ?

Andrey

Quoted 98 lines
Quoted 98 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +16 -0
+
+        if (adev->irq.ih.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+        if (adev->irq.ih1.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+        if (adev->irq.ih2.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+        amdgpu_gart_dummy_page_fini(adev);
+    }
+
+    return NOTIFY_OK;
+}
+
+
   /**
    * amdgpu_device_init - initialize the driver
    *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
         INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
   +    INIT_LIST_HEAD(&adev->device_bo_list);
+
       adev->gfx.gfx_off_req_count = 1;
       adev->pm.ac_power = power_supply_is_system_supplied() > 0;
   @@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
       if (amdgpu_device_cache_pci_state(adev->pdev))

          pci_restore_state(pdev);
  +    BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier); +    adev->nb.notifier_call = amdgpu_iommu_group_notifier;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -0
+
+    if (adev->dev->iommu_group) {
+        r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+        if (r)
+            goto failed;
+    }
+

      return 0;
    failed:

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct amdgpu_device 
 *adev)
    *
    * Frees the dummy page used by the driver (all asics).
    */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
   {
       if (!adev->dummy_page_addr)

          return;

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
   void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
   int amdgpu_gart_init(struct amdgpu_device *adev);
   void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
   int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
                  int pages);
   int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
       }
       amdgpu_bo_unref(&bo->parent);
   +    spin_lock(&ttm_bo_glob.lru_lock);
+    list_del(&bo->bo);
+    spin_unlock(&ttm_bo_glob.lru_lock);
+
       kfree(bo->metadata);
       kfree(bo);
   }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
       if (bp->type == ttm_bo_type_device)
           bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
   +    INIT_LIST_HEAD(&bo->bo);
+
+    spin_lock(&ttm_bo_glob.lru_lock);
+    list_add_tail(&bo->bo, &adev->device_bo_list);
+    spin_unlock(&ttm_bo_glob.lru_lock);
+
       return 0;

    fail_unreserve:

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
       struct list_head        shadow_list;
         struct kgd_mem                  *kfd_bo;
+
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo { · line 117

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
+    struct list_head        bo;
   };
     static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7Candrey.grodzovsky%40amd.com%7C0c703eb6e73744962d3b08d8bc56f303%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466428923905672%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=2Tkz4EMOEwFLQJUOk1ixd28c2ad1HqjBVIDO%2FX0OgqM%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#28 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Ping

Andrey

Quoted 75 lines · Andrey Grodzovsky
Quoted 73 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 57 lines

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46
++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 + drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++

  6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
   #include <linux/aer.h>
+#include <linux/notifier.h>
     #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
         bool                            in_pci_err_recovery;
       struct pci_saved_state          *pci_state;
+
+    struct notifier_block        nb;
+    struct blocking_notifier_head    notifier;
+    struct list_head        device_bo_list;
   };
     static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
   #include <drm/task_barrier.h>
   #include <linux/pm_runtime.h>

  +#include <linux/iommu.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -0
@@ -3200,6 +3202,39 @@ static const struct attribute 
 *amdgpu_dev_attributes[] = {
   };
     +static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+                     unsigned long action, void *data)
+{
+    struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, nb);
+    struct amdgpu_bo *bo = NULL;
+
+    /*
+     * Following is a set of IOMMU group dependencies taken care of before

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static const struct attribute · line 3221

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +10 -0
+     * device's IOMMU group is removed
+     */
+    if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) {
+
+        spin_lock(&ttm_bo_glob.lru_lock);
+        list_for_each_entry(bo, &adev->device_bo_list, bo) {
+            if (bo->tbo.ttm)
+                ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm);
+        }
+        spin_unlock(&ttm_bo_glob.lru_lock);

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

Quoted 117 lines
Quoted 1 line

Christian.

I can think of switching to RCU list ? Otherwise, elements are added
on BO create and deleted on BO destroy, how can i prevent any of those from happening while in this section besides mutex ? Make a copy list and run over it instead ?

Andrey

Quoted 107 lines
Quoted 95 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +16 -0
+
+        if (adev->irq.ih.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+        if (adev->irq.ih1.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+        if (adev->irq.ih2.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+        amdgpu_gart_dummy_page_fini(adev);
+    }
+
+    return NOTIFY_OK;
+}
+
+
   /**
    * amdgpu_device_init - initialize the driver
    *
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device *adev,
         INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
   +    INIT_LIST_HEAD(&adev->device_bo_list);
+
       adev->gfx.gfx_off_req_count = 1;
       adev->pm.ac_power = power_supply_is_system_supplied() > 0;
   @@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,
       if (amdgpu_device_cache_pci_state(adev->pdev))

          pci_restore_state(pdev);
  +    BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier); +    adev->nb.notifier_call = amdgpu_iommu_group_notifier;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -0
+
+    if (adev->dev->iommu_group) {
+        r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+        if (r)
+            goto failed;
+    }
+

      return 0;
    failed:

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct 
 amdgpu_device *adev)
    *
    * Frees the dummy page used by the driver (all asics).
    */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
   {
       if (!adev->dummy_page_addr)

          return;

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev);
   void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
   int amdgpu_gart_init(struct amdgpu_device *adev);
   void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
   int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
                  int pages);
   int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +8 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
       }
       amdgpu_bo_unref(&bo->parent);
   +    spin_lock(&ttm_bo_glob.lru_lock);
+    list_del(&bo->bo);
+    spin_unlock(&ttm_bo_glob.lru_lock);
+
       kfree(bo->metadata);
       kfree(bo);
   }
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
       if (bp->type == ttm_bo_type_device)
           bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
   +    INIT_LIST_HEAD(&bo->bo);
+
+    spin_lock(&ttm_bo_glob.lru_lock);
+    list_add_tail(&bo->bo, &adev->device_bo_list);
+    spin_unlock(&ttm_bo_glob.lru_lock);
+
       return 0;

    fail_unreserve:

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +0 -0
index 9ac3756..5ae8555 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h struct amdgpu_bo { · line 115

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
@@ -110,6 +110,8 @@ struct amdgpu_bo {
       struct list_head        shadow_list;
         struct kgd_mem                  *kfd_bo;
+
+    struct list_head        bo;
   };
     static inline struct amdgpu_bo *ttm_to_amdgpu_bo(struct ttm_buffer_object *tbo)

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7Candrey.grodzovsky%40amd.com%7C0c703eb6e73744962d3b08d8bc56f303%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466428923905672%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=2Tkz4EMOEwFLQJUOk1ixd28c2ad1HqjBVIDO%2FX0OgqM%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

maintainer
#29 lore raw

Re: [PATCH v4 07/14] drm/amdgpu: Register IOMMU topology notifier per device.

Am 20.01.21 um 20:38 schrieb Andrey Grodzovsky:

Quoted 88 lines

Ping

Andrey

Quoted 83 lines · Andrey Grodzovsky
Quoted 73 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 61 lines

Handle all DMA IOMMU gropup related dependencies before the group is removed.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  5 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 46
++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c   |  2 +- drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h   |  1 + drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 10 +++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  2 ++

  6 files changed, 65 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +5 -0
index 478a7d8..2953420 100644
@@ -51,6 +51,7 @@
   #include <linux/dma-fence.h>
   #include <linux/pci.h>
   #include <linux/aer.h>
+#include <linux/notifier.h>
     #include <drm/ttm/ttm_bo_api.h>
   #include <drm/ttm/ttm_bo_driver.h>
@@ -1041,6 +1042,10 @@ struct amdgpu_device {
         bool                            in_pci_err_recovery;
       struct pci_saved_state          *pci_state;
+
+    struct notifier_block        nb;
+    struct blocking_notifier_head    notifier;
+    struct list_head        device_bo_list;
   };
     static inline struct amdgpu_device *drm_to_adev(struct drm_device *ddev)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index 45e23e3..e99f4f1 100644
@@ -70,6 +70,8 @@
   #include <drm/task_barrier.h>
   #include <linux/pm_runtime.h>

  +#include <linux/iommu.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +7 -0
@@ -3200,6 +3202,39 @@ static const struct attribute 
 *amdgpu_dev_attributes[] = {
   };
     +static int amdgpu_iommu_group_notifier(struct notifier_block *nb,
+                     unsigned long action, void *data)
+{
+    struct amdgpu_device *adev = container_of(nb, struct 
 amdgpu_device, nb);
+    struct amdgpu_bo *bo = NULL;
+
+    /*
+     * Following is a set of IOMMU group dependencies taken care 

of before
+     * device's IOMMU group is removed
+     */

Quoted 8 lines

+    if (action == IOMMU_GROUP_NOTIFY_DEL_DEVICE) { +
+        spin_lock(&ttm_bo_glob.lru_lock);
+        list_for_each_entry(bo, &adev->device_bo_list, bo) { +            if (bo->tbo.ttm)
+                ttm_tt_unpopulate(bo->tbo.bdev, bo->tbo.ttm); +        }
+        spin_unlock(&ttm_bo_glob.lru_lock);

That approach won't work. ttm_tt_unpopulate() might sleep on an IOMMU lock.

Quoted 3 lines

You need to use a mutex here or even better make sure you can access the device_bo_list without a lock in this moment.

Christian.

I can think of switching to RCU list ? Otherwise, elements are added
on BO create and deleted on BO destroy, how can i prevent any of those from happening while in this section besides mutex ? Make a copy list and run over it instead ?

RCU won't work since the BO is not RCU protected.

What you can try something like this:

spin_lock(&ttm_bo_glob.lru_lock);
while (list_not_empty(&adev->device_bo_list)) {
    bo = list_first_entry(&adev->device_bo_list);
    list_del(bo->...);
    spin_unlock(&ttm_bo_glob.lru_lock);
    ttm_tt_unpopulate(bo);
    spin_lock(&ttm_bo_glob.lru_lock);
}...

Regards,
Christian.

Quoted 108 lines
Quoted 108 lines

Andrey

Quoted 104 lines
Quoted 104 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +15 -0
+
+        if (adev->irq.ih.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih);
+        if (adev->irq.ih1.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih1);
+        if (adev->irq.ih2.use_bus_addr)
+            amdgpu_ih_ring_fini(adev, &adev->irq.ih2);
+
+        amdgpu_gart_dummy_page_fini(adev);
+    }
+
+    return NOTIFY_OK;
+}
+
+

  /**
   * amdgpu_device_init - initialize the driver
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -0
@@ -3304,6 +3339,8 @@ int amdgpu_device_init(struct amdgpu_device 
 *adev,
         INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
   +    INIT_LIST_HEAD(&adev->device_bo_list);
+
       adev->gfx.gfx_off_req_count = 1;
       adev->pm.ac_power = power_supply_is_system_supplied() > 0;
   @@ -3575,6 +3612,15 @@ int amdgpu_device_init(struct amdgpu_device *adev,

      if (amdgpu_device_cache_pci_state(adev->pdev)) pci_restore_state(pdev);
  +    BLOCKING_INIT_NOTIFIER_HEAD(&adev->notifier);
+    adev->nb.notifier_call = amdgpu_iommu_group_notifier; +
+    if (adev->dev->iommu_group) {
+        r = iommu_group_register_notifier(adev->dev->iommu_group, &adev->nb);
+        if (r)
+            goto failed;
+    }
+
      return 0;
    failed:

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index 0db9330..486ad6d 100644
@@ -92,7 +92,7 @@ static int amdgpu_gart_dummy_page_init(struct 
 amdgpu_device *adev)
    *
    * Frees the dummy page used by the driver (all asics).
    */
-static void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
   {
       if (!adev->dummy_page_addr)

          return;

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.h +1 -0
index afa2e28..5678d9c 100644
@@ -61,6 +61,7 @@ int amdgpu_gart_table_vram_pin(struct 
 amdgpu_device *adev);
   void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev);
   int amdgpu_gart_init(struct amdgpu_device *adev);
   void amdgpu_gart_fini(struct amdgpu_device *adev);
+void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev);
   int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
                  int pages);

  int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -0
index 6cc9919..4a1de69 100644
@@ -94,6 +94,10 @@ static void amdgpu_bo_destroy(struct 
 ttm_buffer_object *tbo)
       }
       amdgpu_bo_unref(&bo->parent);
   +    spin_lock(&ttm_bo_glob.lru_lock);
+    list_del(&bo->bo);
+    spin_unlock(&ttm_bo_glob.lru_lock);
+
       kfree(bo->metadata);
       kfree(bo);

  }

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +5 -0
@@ -613,6 +617,12 @@ static int amdgpu_bo_do_create(struct 
 amdgpu_device *adev,
       if (bp->type == ttm_bo_type_device)
           bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
   +    INIT_LIST_HEAD(&bo->bo);
+
+    spin_lock(&ttm_bo_glob.lru_lock);
+    list_add_tail(&bo->bo, &adev->device_bo_list);
+    spin_unlock(&ttm_bo_glob.lru_lock);
+
       return 0;

    fail_unreserve:

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -0
index 9ac3756..5ae8555 100644
@@ -110,6 +110,8 @@ struct amdgpu_bo {
       struct list_head        shadow_list;
         struct kgd_mem                  *kfd_bo;
+
+    struct list_head        bo;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#30 lore raw

PATCH v4 8/14 drm/amdgpu: Fix a bunch of sdma code crash post device unplug

In reply to Andrey Grodzovsky

We can't allocate and submit IBs post device unplug.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +7 -1
index ad91c0c..5096351 100644
@@ -31,6 +31,7 @@
 #include <linux/dma-buf.h>
 
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 #include "amdgpu.h"
 #include "amdgpu_trace.h"
 #include "amdgpu_amdkfd.h"
@@ -1604,7 +1605,10 @@ static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 	struct amdgpu_vm_update_params params;
 	enum amdgpu_sync_mode sync_mode;
 	uint64_t pfn;
-	int r;
+	int r, idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return -ENOENT;
 
 	memset(&params, 0, sizeof(params));
 	params.adev = adev;
@@ -1647,6 +1651,8 @@ static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
 	if (r)
 		goto error_unlock;
 
+
+	drm_dev_exit(idx);
 	do {
 		uint64_t tmp, num_entries, addr;
 
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#31 lore raw

Re: [PATCH v4 08/14] drm/amdgpu: Fix a bunch of sdma code crash post device unplug

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 20 lines

We can't allocate and submit IBs post device unplug.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +1 -0
index ad91c0c..5096351 100644
@@ -31,6 +31,7 @@
  #include <linux/dma-buf.h>
 
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
  #include "amdgpu.h"
  #include "amdgpu_trace.h"
  #include "amdgpu_amdkfd.h"

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, · line 1611

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -1
@@ -1604,7 +1605,10 @@ static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  	struct amdgpu_vm_update_params params;
  	enum amdgpu_sync_mode sync_mode;
  	uint64_t pfn;
-	int r;
+	int r, idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return -ENOENT;

Why not -ENODEV?

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, · line 1655

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +2 -0
  	memset(&params, 0, sizeof(params));
  	params.adev = adev;
@@ -1647,6 +1651,8 @@ static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
  	if (r)
  		goto error_unlock;
 
+
+	drm_dev_exit(idx);

That's to early. You probably need to do this much further below after the commit.

Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, · line 1658

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
  	do {
  		uint64_t tmp, num_entries, addr;
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#32 lore raw

PATCH v4 9/14 drm/amdgpu: Remap all page faults to per process dummy page.

In reply to Andrey Grodzovsky

On device removal reroute all CPU mappings to dummy page per drm_file instance or imported GEM object.

v4:
Update for modified ttm_bo_vm_dummy_page

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +16 -5
index 9fd2157..550dc5e 100644
@@ -49,6 +49,7 @@
 
 #include <drm/drm_debugfs.h>
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_object.h"
@@ -1982,18 +1983,28 @@ void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
 static vm_fault_t amdgpu_ttm_fault(struct vm_fault *vmf)
 {
 	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
+	struct drm_device *ddev = bo->base.dev;
 	vm_fault_t ret;
+	int idx;
 
 	ret = ttm_bo_vm_reserve(bo, vmf);
 	if (ret)
 		return ret;
 
-	ret = amdgpu_bo_fault_reserve_notify(bo);
-	if (ret)
-		goto unlock;
+	if (drm_dev_enter(ddev, &idx)) {
+		ret = amdgpu_bo_fault_reserve_notify(bo);
+		if (ret) {
+			drm_dev_exit(idx);
+			goto unlock;
+		}
 
-	ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
-				       TTM_BO_VM_NUM_PREFAULT, 1);
+		 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
+						TTM_BO_VM_NUM_PREFAULT, 1);
+
+		 drm_dev_exit(idx);
+	} else {
+		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
+	}
 	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
 		return ret;
 
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
reviewer Reviewed-by on v4 9/14
#33 lore raw

Re: [PATCH v4 09/14] drm/amdgpu: Remap all page faults to per process dummy page.

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 7 lines

On device removal reroute all CPU mappings to dummy page per drm_file instance or imported GEM object.

v4:
Update for modified ttm_bo_vm_dummy_page

Quoted 40 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 21 ++++++++++++++++-----
  1 file changed, 16 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -5
index 9fd2157..550dc5e 100644
@@ -49,6 +49,7 @@
 
  #include <drm/drm_debugfs.h>
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_object.h"
@@ -1982,18 +1983,28 @@ void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
  static vm_fault_t amdgpu_ttm_fault(struct vm_fault *vmf)
  {
  	struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
+	struct drm_device *ddev = bo->base.dev;
  	vm_fault_t ret;
+	int idx;
 
  	ret = ttm_bo_vm_reserve(bo, vmf);
  	if (ret)
  		return ret;
 
-	ret = amdgpu_bo_fault_reserve_notify(bo);
-	if (ret)
-		goto unlock;
+	if (drm_dev_enter(ddev, &idx)) {
+		ret = amdgpu_bo_fault_reserve_notify(bo);
+		if (ret) {
+			drm_dev_exit(idx);
+			goto unlock;
+		}
 
-	ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
-				       TTM_BO_VM_NUM_PREFAULT, 1);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable) · line 2010

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -0
+		 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
+						TTM_BO_VM_NUM_PREFAULT, 1);
+
+		 drm_dev_exit(idx);
+	} else {
+		ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
+	}
  	if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
  		return ret;
 

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#34 lore raw

PATCH v4 10/14 dmr/amdgpu: Move some sysfs attrs creation to default_attr

In reply to Andrey Grodzovsky

This allows to remove explicit creation and destruction of those attrs and by this avoids warnings on device finilizing post physical device extraction.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c | 17 +++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      | 13 +++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c  | 25 ++++++++++---------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 14 +++++---------
 4 files changed, 37 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c +9 -8
index 86add0f..0346e12 100644
@@ -1953,6 +1953,15 @@ static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
 static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
 		   NULL);
 
+static struct attribute *amdgpu_vbios_version_attrs[] = {
+	&dev_attr_vbios_version.attr,
+	NULL
+};
+
+const struct attribute_group amdgpu_vbios_version_attr_group = {
+	.attrs = amdgpu_vbios_version_attrs
+};
+
 /**
  * amdgpu_atombios_fini - free the driver info and callbacks for atombios
  *
@@ -1972,7 +1981,6 @@ void amdgpu_atombios_fini(struct amdgpu_device *adev)
 	adev->mode_info.atom_context = NULL;
 	kfree(adev->mode_info.atom_card_info);
 	adev->mode_info.atom_card_info = NULL;
-	device_remove_file(adev->dev, &dev_attr_vbios_version);
 }
 
 /**
@@ -1989,7 +1997,6 @@ int amdgpu_atombios_init(struct amdgpu_device *adev)
 {
 	struct card_info *atom_card_info =
 	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
-	int ret;
 
 	if (!atom_card_info)
 		return -ENOMEM;
@@ -2027,12 +2034,6 @@ int amdgpu_atombios_init(struct amdgpu_device *adev)
 		amdgpu_atombios_allocate_fb_scratch(adev);
 	}
 
-	ret = device_create_file(adev->dev, &dev_attr_vbios_version);
-	if (ret) {
-		DRM_ERROR("Failed to create device file for VBIOS version\n");
-		return ret;
-	}
-
 	return 0;
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +13 -0
index 9c0cd00..8fddd74 100644
@@ -1587,6 +1587,18 @@ static struct pci_error_handlers amdgpu_pci_err_handler = {
 	.resume		= amdgpu_pci_resume,
 };
 
+extern const struct attribute_group amdgpu_vram_mgr_attr_group;
+extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
+extern const struct attribute_group amdgpu_vbios_version_attr_group;
+
+static const struct attribute_group *amdgpu_sysfs_groups[] = {
+	&amdgpu_vram_mgr_attr_group,
+	&amdgpu_gtt_mgr_attr_group,
+	&amdgpu_vbios_version_attr_group,
+	NULL,
+};
+
+
 static struct pci_driver amdgpu_kms_pci_driver = {
 	.name = DRIVER_NAME,
 	.id_table = pciidlist,
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
 	.shutdown = amdgpu_pci_shutdown,
 	.driver.pm = &amdgpu_pm_ops,
 	.err_handler = &amdgpu_pci_err_handler,
+	.driver.dev_groups = amdgpu_sysfs_groups,
 };
 
 static int __init amdgpu_init(void)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +10 -15
index 8980329..3b7150e 100644
@@ -77,6 +77,16 @@ static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO,
 static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO,
 	           amdgpu_mem_info_gtt_used_show, NULL);
 
+static struct attribute *amdgpu_gtt_mgr_attributes[] = {
+	&dev_attr_mem_info_gtt_total.attr,
+	&dev_attr_mem_info_gtt_used.attr,
+	NULL
+};
+
+const struct attribute_group amdgpu_gtt_mgr_attr_group = {
+	.attrs = amdgpu_gtt_mgr_attributes
+};
+
 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func;
 /**
  * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
@@ -91,7 +101,6 @@ int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
 	struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
 	struct ttm_resource_manager *man = &mgr->manager;
 	uint64_t start, size;
-	int ret;
 
 	man->use_tt = true;
 	man->func = &amdgpu_gtt_mgr_func;
@@ -104,17 +113,6 @@ int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
 	spin_lock_init(&mgr->lock);
 	atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT);
 
-	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
-	if (ret) {
-		DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
-		return ret;
-	}
-	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
-	if (ret) {
-		DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
-		return ret;
-	}
-
 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
 	ttm_resource_manager_set_used(man, true);
 	return 0;
@@ -144,9 +142,6 @@ void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
 	drm_mm_takedown(&mgr->mm);
 	spin_unlock(&mgr->lock);
 
-	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
-	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
-
 	ttm_resource_manager_cleanup(man);
 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +5 -9
index d2de2a7..9158d11 100644
@@ -154,7 +154,7 @@ static DEVICE_ATTR(mem_info_vis_vram_used, S_IRUGO,
 static DEVICE_ATTR(mem_info_vram_vendor, S_IRUGO,
 		   amdgpu_mem_info_vram_vendor, NULL);
 
-static const struct attribute *amdgpu_vram_mgr_attributes[] = {
+static struct attribute *amdgpu_vram_mgr_attributes[] = {
 	&dev_attr_mem_info_vram_total.attr,
 	&dev_attr_mem_info_vis_vram_total.attr,
 	&dev_attr_mem_info_vram_used.attr,
@@ -163,6 +163,10 @@ static const struct attribute *amdgpu_vram_mgr_attributes[] = {
 	NULL
 };
 
+const struct attribute_group amdgpu_vram_mgr_attr_group = {
+	.attrs = amdgpu_vram_mgr_attributes
+};
+
 static const struct ttm_resource_manager_func amdgpu_vram_mgr_func;
 
 /**
@@ -176,7 +180,6 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
 {
 	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
 	struct ttm_resource_manager *man = &mgr->manager;
-	int ret;
 
 	ttm_resource_manager_init(man, adev->gmc.real_vram_size >> PAGE_SHIFT);
 
@@ -187,11 +190,6 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
 	INIT_LIST_HEAD(&mgr->reservations_pending);
 	INIT_LIST_HEAD(&mgr->reserved_pages);
 
-	/* Add the two VRAM-related sysfs files */
-	ret = sysfs_create_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
-	if (ret)
-		DRM_ERROR("Failed to register sysfs\n");
-
 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
 	ttm_resource_manager_set_used(man, true);
 	return 0;
@@ -229,8 +227,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
 	drm_mm_takedown(&mgr->mm);
 	spin_unlock(&mgr->lock);
 
-	sysfs_remove_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
-
 	ttm_resource_manager_cleanup(man);
 	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, NULL);
 }
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#35 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static struct pci_driver amdgpu_kms_pci_driver = { · line 1610

Quoted 8 lines · Andrey Grodzovsky

static struct pci_driver amdgpu_kms_pci_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
 	.name = DRIVER_NAME,
 	.id_table = pciidlist,
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
 	.shutdown = amdgpu_pci_shutdown,
 	.driver.pm = &amdgpu_pm_ops,
 	.err_handler = &amdgpu_pci_err_handler,
+	.driver.dev_groups = amdgpu_sysfs_groups,

Shouldn't this just be:
groups - amdgpu_sysfs_groups,

Why go to the "driver root" here?

Other than that tiny thing, looks good to me, nice cleanup!

greg k-h
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#36 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

Quoted 12 lines · Greg KH

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static struct pci_driver amdgpu_kms_pci_driver = { · line 1610

Quoted 8 lines · Andrey Grodzovsky
  static struct pci_driver amdgpu_kms_pci_driver = {
  	.name = DRIVER_NAME,
  	.id_table = pciidlist,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
  	.shutdown = amdgpu_pci_shutdown,
  	.driver.pm = &amdgpu_pm_ops,
  	.err_handler = &amdgpu_pci_err_handler,
+	.driver.dev_groups = amdgpu_sysfs_groups,

Shouldn't this just be:
groups - amdgpu_sysfs_groups,

Quoted 1 line

Why go to the "driver root" here?

Because I still didn't get to your suggestion to propose a patch to add groups to pci_driver, it's located in 'base' driver struct.

Andrey

Quoted 4 lines

Other than that tiny thing, looks good to me, nice cleanup!

greg k-h

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#37 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

Quoted 14 lines · Andrey Grodzovsky
Quoted 12 lines · Greg KH

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static struct pci_driver amdgpu_kms_pci_driver = { · line 1610

Quoted 8 lines · Andrey Grodzovsky
  static struct pci_driver amdgpu_kms_pci_driver = {
  	.name = DRIVER_NAME,
  	.id_table = pciidlist,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
  	.shutdown = amdgpu_pci_shutdown,
  	.driver.pm = &amdgpu_pm_ops,
  	.err_handler = &amdgpu_pci_err_handler,
+	.driver.dev_groups = amdgpu_sysfs_groups,

Shouldn't this just be:
groups - amdgpu_sysfs_groups,

Quoted 5 lines
Quoted 1 line

Why go to the "driver root" here?

Because I still didn't get to your suggestion to propose a patch to add groups to pci_driver, it's located in 'base' driver struct.

You are a pci driver, you should never have to mess with the "base" driver struct. Look at commit 92d50fc1602e ("PCI/IB: add support for pci driver attribute groups") which got merged in 4.14, way back in 2017 :)

driver.pm also looks odd, but I'm just going to ignore that for now...

thanks,

greg k-h
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#38 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

Quoted 19 lines · Greg KH
Quoted 17 lines · Andrey Grodzovsky
Quoted 13 lines · Greg KH

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static struct pci_driver amdgpu_kms_pci_driver = { · line 1610

Quoted 8 lines · Andrey Grodzovsky
  static struct pci_driver amdgpu_kms_pci_driver = {
          .name = DRIVER_NAME,
          .id_table = pciidlist,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
          .shutdown = amdgpu_pci_shutdown,
          .driver.pm = &amdgpu_pm_ops,
          .err_handler = &amdgpu_pci_err_handler,
+ .driver.dev_groups = amdgpu_sysfs_groups,

Shouldn't this just be:
groups - amdgpu_sysfs_groups,

Why go to the "driver root" here?

Quoted 7 lines
Quoted 2 lines

Because I still didn't get to your suggestion to propose a patch to add groups to pci_driver, it's located in 'base' driver struct.

You are a pci driver, you should never have to mess with the "base" driver struct. Look at commit 92d50fc1602e ("PCI/IB: add support for pci driver attribute groups") which got merged in 4.14, way back in 2017 :)

Per the previous discussion of this patch set:
https://www.mail-archive.com/[email protected]/msg56019.html

Alex

Quoted 10 lines

driver.pm also looks odd, but I'm just going to ignore that for now...

thanks,

greg k-h
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#39 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

Quoted 19 lines · Alex Deucher
Quoted 18 lines · Greg KH
Quoted 17 lines · Andrey Grodzovsky
Quoted 12 lines · Greg KH

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static struct pci_driver amdgpu_kms_pci_driver = { · line 1610

Quoted 8 lines · Andrey Grodzovsky
   static struct pci_driver amdgpu_kms_pci_driver = {
           .name = DRIVER_NAME,
           .id_table = pciidlist,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
           .shutdown = amdgpu_pci_shutdown,
           .driver.pm = &amdgpu_pm_ops,
           .err_handler = &amdgpu_pci_err_handler,
+ .driver.dev_groups = amdgpu_sysfs_groups,

Shouldn't this just be:
groups - amdgpu_sysfs_groups,

Quoted 1 line

Why go to the "driver root" here?

Because I still didn't get to your suggestion to propose a patch to add groups to pci_driver, it's located in 'base' driver struct.

Quoted 8 lines
Quoted 4 lines

You are a pci driver, you should never have to mess with the "base" driver struct. Look at commit 92d50fc1602e ("PCI/IB: add support for pci driver attribute groups") which got merged in 4.14, way back in 2017 :)

Per the previous discussion of this patch set:
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.mail-archive.com%2Famd-gfx%40lists.freedesktop.org%2Fmsg56019.html&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C1b43efdc8a164169eee508d8bcad1ece%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466799090087255%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=T462j96qC%2BXCZzgnJMG%2BbUEOG94GVuqkvTWfUB%2B3%2Fl8%3D&amp;reserved=0

Alex

Got it, Next iteration I will include a patch like the above to pci-devel as part of the series and will update this patch accordingly.

Andrey

Quoted 10 lines
Quoted 10 lines

driver.pm also looks odd, but I'm just going to ignore that for now...

thanks,

greg k-h
_______________________________________________
dri-devel mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Fdri-devel&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C1b43efdc8a164169eee508d8bcad1ece%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466799090087255%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=reQqTGFCsEXvHOmSt8c4B6idrotIS4Q69WKw%2FRtpAEg%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#40 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

In reply to Alex Deucher
Quoted 23 lines · Alex Deucher
Quoted 22 lines · Greg KH
Quoted 19 lines · Andrey Grodzovsky
Quoted 12 lines · Greg KH

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c static struct pci_driver amdgpu_kms_pci_driver = { · line 1610

Quoted 8 lines · Andrey Grodzovsky
  static struct pci_driver amdgpu_kms_pci_driver = {
          .name = DRIVER_NAME,
          .id_table = pciidlist,
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
          .shutdown = amdgpu_pci_shutdown,
          .driver.pm = &amdgpu_pm_ops,
          .err_handler = &amdgpu_pci_err_handler,
+ .driver.dev_groups = amdgpu_sysfs_groups,

Shouldn't this just be:
groups - amdgpu_sysfs_groups,

Quoted 1 line

Why go to the "driver root" here?

Because I still didn't get to your suggestion to propose a patch to add groups to pci_driver, it's located in 'base' driver struct.

Quoted 7 lines
Quoted 4 lines

You are a pci driver, you should never have to mess with the "base" driver struct. Look at commit 92d50fc1602e ("PCI/IB: add support for pci driver attribute groups") which got merged in 4.14, way back in 2017 :)

Per the previous discussion of this patch set:
https://www.mail-archive.com/[email protected]/msg56019.html

Hey, at least I'm consistent :)
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

reviewer Acked-by on v4 10/14
#41 lore raw

Re: [PATCH v4 10/14] dmr/amdgpu: Move some sysfs attrs creation to default_attr

In reply to Andrey Grodzovsky

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 5 lines

This allows to remove explicit creation and destruction of those attrs and by this avoids warnings on device finilizing post physical device extraction.

Quoted 189 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c | 17 +++++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c      | 13 +++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c  | 25 ++++++++++---------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 14 +++++---------
  4 files changed, 37 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c +9 -8
index 86add0f..0346e12 100644
@@ -1953,6 +1953,15 @@ static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
  static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
  		   NULL);
 
+static struct attribute *amdgpu_vbios_version_attrs[] = {
+	&dev_attr_vbios_version.attr,
+	NULL
+};
+
+const struct attribute_group amdgpu_vbios_version_attr_group = {
+	.attrs = amdgpu_vbios_version_attrs
+};
+
  /**
   * amdgpu_atombios_fini - free the driver info and callbacks for atombios
   *
@@ -1972,7 +1981,6 @@ void amdgpu_atombios_fini(struct amdgpu_device *adev)
  	adev->mode_info.atom_context = NULL;
  	kfree(adev->mode_info.atom_card_info);
  	adev->mode_info.atom_card_info = NULL;
-	device_remove_file(adev->dev, &dev_attr_vbios_version);
  }
 
  /**
@@ -1989,7 +1997,6 @@ int amdgpu_atombios_init(struct amdgpu_device *adev)
  {
  	struct card_info *atom_card_info =
  	    kzalloc(sizeof(struct card_info), GFP_KERNEL);
-	int ret;
 
  	if (!atom_card_info)
  		return -ENOMEM;
@@ -2027,12 +2034,6 @@ int amdgpu_atombios_init(struct amdgpu_device *adev)
  		amdgpu_atombios_allocate_fb_scratch(adev);
  	}
 
-	ret = device_create_file(adev->dev, &dev_attr_vbios_version);
-	if (ret) {
-		DRM_ERROR("Failed to create device file for VBIOS version\n");
-		return ret;
-	}
-
  	return 0;
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +13 -0
index 9c0cd00..8fddd74 100644
@@ -1587,6 +1587,18 @@ static struct pci_error_handlers amdgpu_pci_err_handler = {
  	.resume		= amdgpu_pci_resume,
  };
 
+extern const struct attribute_group amdgpu_vram_mgr_attr_group;
+extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
+extern const struct attribute_group amdgpu_vbios_version_attr_group;
+
+static const struct attribute_group *amdgpu_sysfs_groups[] = {
+	&amdgpu_vram_mgr_attr_group,
+	&amdgpu_gtt_mgr_attr_group,
+	&amdgpu_vbios_version_attr_group,
+	NULL,
+};
+
+
  static struct pci_driver amdgpu_kms_pci_driver = {
  	.name = DRIVER_NAME,
  	.id_table = pciidlist,
@@ -1595,6 +1607,7 @@ static struct pci_driver amdgpu_kms_pci_driver = {
  	.shutdown = amdgpu_pci_shutdown,
  	.driver.pm = &amdgpu_pm_ops,
  	.err_handler = &amdgpu_pci_err_handler,
+	.driver.dev_groups = amdgpu_sysfs_groups,
  };
 
  static int __init amdgpu_init(void)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +10 -15
index 8980329..3b7150e 100644
@@ -77,6 +77,16 @@ static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO,
  static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO,
  	           amdgpu_mem_info_gtt_used_show, NULL);
 
+static struct attribute *amdgpu_gtt_mgr_attributes[] = {
+	&dev_attr_mem_info_gtt_total.attr,
+	&dev_attr_mem_info_gtt_used.attr,
+	NULL
+};
+
+const struct attribute_group amdgpu_gtt_mgr_attr_group = {
+	.attrs = amdgpu_gtt_mgr_attributes
+};
+
  static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func;
  /**
   * amdgpu_gtt_mgr_init - init GTT manager and DRM MM
@@ -91,7 +101,6 @@ int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
  	struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr;
  	struct ttm_resource_manager *man = &mgr->manager;
  	uint64_t start, size;
-	int ret;
 
  	man->use_tt = true;
  	man->func = &amdgpu_gtt_mgr_func;
@@ -104,17 +113,6 @@ int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size)
  	spin_lock_init(&mgr->lock);
  	atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT);
 
-	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total);
-	if (ret) {
-		DRM_ERROR("Failed to create device file mem_info_gtt_total\n");
-		return ret;
-	}
-	ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used);
-	if (ret) {
-		DRM_ERROR("Failed to create device file mem_info_gtt_used\n");
-		return ret;
-	}
-
  	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager);
  	ttm_resource_manager_set_used(man, true);
  	return 0;
@@ -144,9 +142,6 @@ void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev)
  	drm_mm_takedown(&mgr->mm);
  	spin_unlock(&mgr->lock);
 
-	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total);
-	device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used);
-
  	ttm_resource_manager_cleanup(man);
  	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL);
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +5 -7
index d2de2a7..9158d11 100644
@@ -154,7 +154,7 @@ static DEVICE_ATTR(mem_info_vis_vram_used, S_IRUGO,
  static DEVICE_ATTR(mem_info_vram_vendor, S_IRUGO,
  		   amdgpu_mem_info_vram_vendor, NULL);
 
-static const struct attribute *amdgpu_vram_mgr_attributes[] = {
+static struct attribute *amdgpu_vram_mgr_attributes[] = {
  	&dev_attr_mem_info_vram_total.attr,
  	&dev_attr_mem_info_vis_vram_total.attr,
  	&dev_attr_mem_info_vram_used.attr,
@@ -163,6 +163,10 @@ static const struct attribute *amdgpu_vram_mgr_attributes[] = {
  	NULL
  };
 
+const struct attribute_group amdgpu_vram_mgr_attr_group = {
+	.attrs = amdgpu_vram_mgr_attributes
+};
+
  static const struct ttm_resource_manager_func amdgpu_vram_mgr_func;
 
  /**
@@ -176,7 +180,6 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
  {
  	struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
  	struct ttm_resource_manager *man = &mgr->manager;
-	int ret;
 
  	ttm_resource_manager_init(man, adev->gmc.real_vram_size >> PAGE_SHIFT);
 
@@ -187,11 +190,6 @@ int amdgpu_vram_mgr_init(struct amdgpu_device *adev)
  	INIT_LIST_HEAD(&mgr->reservations_pending);
  	INIT_LIST_HEAD(&mgr->reserved_pages);
 
-	/* Add the two VRAM-related sysfs files */
-	ret = sysfs_create_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
-	if (ret)
-		DRM_ERROR("Failed to register sysfs\n");
-
  	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, &mgr->manager);
  	ttm_resource_manager_set_used(man, true);
  	return 0;

drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c void amdgpu_vram_mgr_fini(struct amdgpu_device *adev) · line 232

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +0 -2
@@ -229,8 +227,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev)
  	drm_mm_takedown(&mgr->mm);
  	spin_unlock(&mgr->lock);
 
-	sysfs_remove_files(&adev->dev->kobj, amdgpu_vram_mgr_attributes);
-
  	ttm_resource_manager_cleanup(man);
  	ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_VRAM, NULL);
  }

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#42 lore raw

PATCH v4 11/14 drm/amdgpu: Guard against write accesses after device removal

In reply to Andrey Grodzovsky

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
 drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++-------------------
 drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++-----
 drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +---
 drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---
 9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +57 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
 
 #include <linux/iommu.h>
 
+#include <drm/drm_drv.h>
+
 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
  */
 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
 {
+	int idx;
+
 	if (adev->in_pci_err_recovery)
 		return;
 
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
 	if (offset < adev->rmmio_size)
 		writeb(value, adev->rmmio + offset);
 	else
 		BUG();
+
+	drm_dev_exit(idx);
 }
 
 /**
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
 			uint32_t reg, uint32_t v,
 			uint32_t acc_flags)
 {
+	int idx;
+
 	if (adev->in_pci_err_recovery)
 		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
 	if ((reg * 4) < adev->rmmio_size) {
 		if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
 		    amdgpu_sriov_runtime(adev) &&
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
 	}
 
 	trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+	drm_dev_exit(idx);
 }
 
 /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
 			     uint32_t reg, uint32_t v)
 {
+	int idx;
+
 	if (adev->in_pci_err_recovery)
 		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
 	if (amdgpu_sriov_fullaccess(adev) &&
 	    adev->gfx.rlc.funcs &&
 	    adev->gfx.rlc.funcs->is_rlcg_access_range) {
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
 	} else {
 		writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
 	}
+
+	drm_dev_exit(idx);
 }
 
 /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
  */
 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
 {
+	int idx;
+
 	if (adev->in_pci_err_recovery)
 		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
 	if ((reg * 4) < adev->rio_mem_size)
 		iowrite32(v, adev->rio_mem + (reg * 4));
 	else {
 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
 		iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
 	}
+
+	drm_dev_exit(idx);
 }
 
 /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
  */
 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
 {
+	int idx;
+
 	if (adev->in_pci_err_recovery)
 		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
 	if (index < adev->doorbell.num_doorbells) {
 		writel(v, adev->doorbell.ptr + index);
 	} else {
 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
 	}
+
+	drm_dev_exit(idx);
 }
 
 /**
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
  */
 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
 {
+	int idx;
+
 	if (adev->in_pci_err_recovery)
 		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
 	if (index < adev->doorbell.num_doorbells) {
 		atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
 	} else {
 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
 	}
+
+	drm_dev_exit(idx);
 }
 
 /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
 	unsigned long flags;
 	void __iomem *pcie_index_offset;
 	void __iomem *pcie_data_offset;
+	int idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
 
 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
 	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
 	writel(reg_data, pcie_data_offset);
 	readl(pcie_data_offset);
 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+	drm_dev_exit(idx);
 }
 
 /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
 	unsigned long flags;
 	void __iomem *pcie_index_offset;
 	void __iomem *pcie_data_offset;
+	int idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
 
 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
 	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
 	writel((u32)(reg_data >> 32), pcie_data_offset);
 	readl(pcie_data_offset);
 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+	drm_dev_exit(idx);
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +9 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
 #include "amdgpu_ras.h"
 #include "amdgpu_xgmi.h"
 
+#include <drm/drm_drv.h>
+
 /**
  * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO
  *
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
 {
 	void __iomem *ptr = (void *)cpu_pt_addr;
 	uint64_t value;
+	int idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return 0;
 
 	/*
 	 * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
 	value = addr & 0x0000FFFFFFFFF000ULL;
 	value |= flags;
 	writeq(value, ptr + (gpu_page_idx * 8));
+
+	drm_dev_exit(idx);
+
 	return 0;
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +33 -20

138 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
 			  const char *chip_name);
 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
 					uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size);
+
 #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +70 -0

79 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -45

57 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -12
index bd4248c..b3ce5be 100644
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy PSP KDB binary to memory */
-	memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+	psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
 
 	/* Provide the PSP KDB to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy PSP SPL binary to memory */
-	memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+	psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
 
 	/* Provide the PSP SPL to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy PSP System Driver binary to memory */
-	memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+	psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
 
 	/* Provide the sys driver to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy Secure OS binary to PSP memory */
-	memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+	psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
 
 	/* Provide the PSP secure OS to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -6
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy PSP System Driver binary to memory */
-	memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+	psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
 
 	/* Provide the sys driver to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy Secure OS binary to PSP memory */
-	memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+	psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
 
 	/* Provide the PSP secure OS to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +2 -6
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy PSP System Driver binary to memory */
-	memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+	psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
 
 	/* Provide the sys driver to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct psp_context *psp)
 	if (ret)
 		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
 	/* Copy Secure OS binary to PSP memory */
-	memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+	psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
 
 	/* Provide the PSP secure OS to bootloader */
 	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#43 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 651 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70 ++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++-------------------
  drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++-----
  drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +---
  drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---
  9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +57 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
 
  #include <linux/iommu.h>
 
+#include <drm/drm_drv.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
   */
  void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
  {
+	int idx;
+
  	if (adev->in_pci_err_recovery)
  		return;
 
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
  	if (offset < adev->rmmio_size)
  		writeb(value, adev->rmmio + offset);
  	else
  		BUG();
+
+	drm_dev_exit(idx);
  }
 
  /**
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
  			uint32_t reg, uint32_t v,
  			uint32_t acc_flags)
  {
+	int idx;
+
  	if (adev->in_pci_err_recovery)
  		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
  	if ((reg * 4) < adev->rmmio_size) {
  		if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
  		    amdgpu_sriov_runtime(adev) &&
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
  	}
 
  	trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+	drm_dev_exit(idx);
  }
 
  /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
  void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
  			     uint32_t reg, uint32_t v)
  {
+	int idx;
+
  	if (adev->in_pci_err_recovery)
  		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
  	if (amdgpu_sriov_fullaccess(adev) &&
  	    adev->gfx.rlc.funcs &&
  	    adev->gfx.rlc.funcs->is_rlcg_access_range) {
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
  	} else {
  		writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
  	}
+
+	drm_dev_exit(idx);
  }
 
  /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
   */
  void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  {
+	int idx;
+
  	if (adev->in_pci_err_recovery)
  		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
  	if ((reg * 4) < adev->rio_mem_size)
  		iowrite32(v, adev->rio_mem + (reg * 4));
  	else {
  		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
  		iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
  	}
+
+	drm_dev_exit(idx);
  }
 
  /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
   */
  void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  {
+	int idx;
+
  	if (adev->in_pci_err_recovery)
  		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
  	if (index < adev->doorbell.num_doorbells) {
  		writel(v, adev->doorbell.ptr + index);
  	} else {
  		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  	}
+
+	drm_dev_exit(idx);
  }
 
  /**
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
   */
  void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
  {
+	int idx;
+
  	if (adev->in_pci_err_recovery)
  		return;
 
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
+
  	if (index < adev->doorbell.num_doorbells) {
  		atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
  	} else {
  		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
  	}
+
+	drm_dev_exit(idx);
  }
 
  /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
  	unsigned long flags;
  	void __iomem *pcie_index_offset;
  	void __iomem *pcie_data_offset;
+	int idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
 
  	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
  	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
  	writel(reg_data, pcie_data_offset);
  	readl(pcie_data_offset);
  	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+	drm_dev_exit(idx);
  }
 
  /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
  	unsigned long flags;
  	void __iomem *pcie_index_offset;
  	void __iomem *pcie_data_offset;
+	int idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return;
 
  	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
  	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
  	writel((u32)(reg_data >> 32), pcie_data_offset);
  	readl(pcie_data_offset);
  	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+	drm_dev_exit(idx);
  }
 
  /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +9 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
  #include "amdgpu_ras.h"
  #include "amdgpu_xgmi.h"
 
+#include <drm/drm_drv.h>
+
  /**
   * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO
   *
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
  {
  	void __iomem *ptr = (void *)cpu_pt_addr;
  	uint64_t value;
+	int idx;
+
+	if (!drm_dev_enter(&adev->ddev, &idx))
+		return 0;
 
  	/*
  	 * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, void *cpu_pt_addr,
  	value = addr & 0x0000FFFFFFFFF000ULL;
  	value |= flags;
  	writeq(value, ptr + (gpu_page_idx * 8));
+
+	drm_dev_exit(idx);
+
  	return 0;
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +33 -20
index 523d22d..89e2bfe 100644
@@ -37,6 +37,8 @@
 
  #include "amdgpu_ras.h"
 
+#include <drm/drm_drv.h>
+
  static int psp_sysfs_init(struct amdgpu_device *adev);
  static void psp_sysfs_fini(struct amdgpu_device *adev);
 
@@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
  		   struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
  {
  	int ret;
-	int index;
+	int index, idx;
  	int timeout = 2000;
  	bool ras_intr = false;
  	bool skip_unsupport = false;
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
  	if (psp->adev->in_pci_err_recovery)
  		return 0;
 
+	if (!drm_dev_enter(&psp->adev->ddev, &idx))
+		return 0;
+
  	mutex_lock(&psp->mutex);
 
  	memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
  	ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
  	if (ret) {
  		atomic_dec(&psp->fence_value);
-		mutex_unlock(&psp->mutex);
-		return ret;
+		goto exit;
  	}
 
  	amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
  			 psp->cmd_buf_mem->cmd_id,
  			 psp->cmd_buf_mem->resp.status);
  		if (!timeout) {
-			mutex_unlock(&psp->mutex);
-			return -EINVAL;
+			ret = -EINVAL;
+			goto exit;
  		}
  	}
 
@@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,
  		ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
  		ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
  	}
-	mutex_unlock(&psp->mutex);
 
+exit:
+	mutex_unlock(&psp->mutex);
+	drm_dev_exit(idx);
  	return ret;
  }
 
@@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp,
  	if (!cmd)
  		return -ENOMEM;
  	/* Copy toc to psp firmware private buffer */
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size);
+	psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size);
 
  	psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size);
 
@@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp)
  	if (!cmd)
  		return -ENOMEM;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
+	psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size);
 
  	psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr,
  				  psp->asd_ucode_size);
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
  	if (!cmd)
  		return -ENOMEM;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
+	psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
 
  	psp_prep_ta_load_cmd_buf(cmd,
  				 psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
  	if (!cmd)
  		return -ENOMEM;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
+	psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
 
  	psp_prep_ta_load_cmd_buf(cmd,
  				 psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
  	if (!cmd)
  		return -ENOMEM;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+	psp_copy_fw(psp, psp->ta_hdcp_start_addr,
  	       psp->ta_hdcp_ucode_size);
 
  	psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
  	if (!cmd)
  		return -ENOMEM;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
+	psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
 
  	psp_prep_ta_load_cmd_buf(cmd,
  				 psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
  	if (!cmd)
  		return -ENOMEM;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-	memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
+	psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
 
  	psp_prep_ta_load_cmd_buf(cmd,
  				 psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
  	return count;
  }
 
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
+{
+	int idx;
+
+	if (!drm_dev_enter(&psp->adev->ddev, &idx))
+		return;
+
+	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+	memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+	drm_dev_exit(idx);
+}
+
+
  static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
  		   psp_usbc_pd_fw_sysfs_read,
  		   psp_usbc_pd_fw_sysfs_write);
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
  			  const char *chip_name);
  int psp_get_fw_attestation_records_addr(struct psp_context *psp,
  					uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size);
+
  #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +70 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
  #include "amdgpu.h"
  #include "atom.h"
 
+#include <drm/drm_drv.h>
+
  /*
   * Rings
   * Most engines on the GPU are fed via ring buffers.  Ring
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
  	ring->sched.ready = !r;
  	return r;
  }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+	int idx;
+	int i = 0;
+
+	if (!drm_dev_enter(&ring->adev->ddev, &idx))
+		return;
+
+	while (i <= ring->buf_mask)
+		ring->ring[i++] = ring->funcs->nop;
+
+	drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+	int idx;
+
+	if (!drm_dev_enter(&ring->adev->ddev, &idx))
+		return;
+
+	if (ring->count_dw <= 0)
+		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+	ring->ring[ring->wptr++ & ring->buf_mask] = v;
+	ring->wptr &= ring->ptr_mask;
+	ring->count_dw--;
+
+	drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+					      void *src, int count_dw)
+{
+	unsigned occupied, chunk1, chunk2;
+	void *dst;
+	int idx;
+
+	if (!drm_dev_enter(&ring->adev->ddev, &idx))
+		return;
+
+	if (unlikely(ring->count_dw < count_dw))
+		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+
+	occupied = ring->wptr & ring->buf_mask;
+	dst = (void *)&ring->ring[occupied];
+	chunk1 = ring->buf_mask + 1 - occupied;
+	chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+	chunk2 = count_dw - chunk1;
+	chunk1 <<= 2;
+	chunk2 <<= 2;
+
+	if (chunk1)
+		memcpy(dst, src, chunk1);
+
+	if (chunk2) {
+		src += chunk1;
+		dst = (void *)ring->ring;
+		memcpy(dst, src, chunk2);
+	}
+
+	ring->wptr += count_dw;
+	ring->wptr &= ring->ptr_mask;
+	ring->count_dw -= count_dw;
+
+	drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -45
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
  	*ring->cond_exe_cpu_addr = cond_exec;
  }
 
-static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-	int i = 0;
-	while (i <= ring->buf_mask)
-		ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-	if (ring->count_dw <= 0)
-		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-	ring->ring[ring->wptr++ & ring->buf_mask] = v;
-	ring->wptr &= ring->ptr_mask;
-	ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
 
-static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-					      void *src, int count_dw)
-{
-	unsigned occupied, chunk1, chunk2;
-	void *dst;
-
-	if (unlikely(ring->count_dw < count_dw))
-		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-
-	occupied = ring->wptr & ring->buf_mask;
-	dst = (void *)&ring->ring[occupied];
-	chunk1 = ring->buf_mask + 1 - occupied;
-	chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-	chunk2 = count_dw - chunk1;
-	chunk1 <<= 2;
-	chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
 
-	if (chunk1)
-		memcpy(dst, src, chunk1);
-
-	if (chunk2) {
-		src += chunk1;
-		dst = (void *)ring->ring;
-		memcpy(dst, src, chunk2);
-	}
-
-	ring->wptr += count_dw;
-	ring->wptr &= ring->ptr_mask;
-	ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+					      void *src, int count_dw);
 
  int amdgpu_ring_test_helper(struct amdgpu_ring *ring);
 
drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -12
index bd4248c..b3ce5be 100644
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy PSP KDB binary to memory */
-	memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+	psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
 
  	/* Provide the PSP KDB to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy PSP SPL binary to memory */
-	memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+	psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
 
  	/* Provide the PSP SPL to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy PSP System Driver binary to memory */
-	memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+	psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
 
  	/* Provide the sys driver to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy Secure OS binary to PSP memory */
-	memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+	psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
 
  	/* Provide the PSP secure OS to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -6
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy PSP System Driver binary to memory */
-	memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+	psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
 
  	/* Provide the sys driver to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy Secure OS binary to PSP memory */
-	memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+	psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
 
  	/* Provide the PSP secure OS to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -3
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct psp_context *psp)
  	if (ret)
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy PSP System Driver binary to memory */
-	memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+	psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
 
  	/* Provide the sys driver to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct psp_context *psp)
  	if (ret)

drivers/gpu/drm/amd/amdgpu/psp_v3_1.c static int psp_v3_1_bootloader_load_sos(struct psp_context *psp) · line 148

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -3
  		return ret;
 
-	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
  	/* Copy Secure OS binary to PSP memory */
-	memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+	psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
 
  	/* Provide the PSP secure OS to bootloader */
  	WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#44 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

There is really no other way according to this article https://lwn.net/Articles/767885/

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user to prevent them from yanking a device and we cannot check for a presence change after every device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?
The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 612 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 604 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++

  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++

  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70 ++++++++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++------------------- drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++----- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +--- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---

  9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
     #include <linux/iommu.h>

  +#include <drm/drm_drv.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, 
 uint32_t offset)
    */
   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;

  +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if (offset < adev->rmmio_size)
          writeb(value, adev->rmmio + offset);
      else
          BUG();
+
+    drm_dev_exit(idx);
  }
    /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
               uint32_t reg, uint32_t v,
               uint32_t acc_flags)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if ((reg * 4) < adev->rmmio_size) {

          if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev) &&

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
       }
         trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
   }
     /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
   void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                    uint32_t reg, uint32_t v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+

      if (amdgpu_sriov_fullaccess(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.funcs->is_rlcg_access_range) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +32 -0
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
       } else {
           writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
    */
   void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if ((reg * 4) < adev->rio_mem_size)
           iowrite32(v, adev->rio_mem + (reg * 4));
       else {
           iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
           iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 
 index)
    */
   void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           writel(v, adev->doorbell.ptr + index);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, 
 u32 index)
    */
   void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device 
 *adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags);
       pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
       writel(reg_data, pcie_data_offset);
       readl(pcie_data_offset);
       spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
   }
     /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device 
 *adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags);
       pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device 
 *adev,
       writel((u32)(reg_data >> 32), pcie_data_offset);
       readl(pcie_data_offset);
       spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
   }
     /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
   #include "amdgpu_ras.h"
   #include "amdgpu_xgmi.h"

  +#include <drm/drm_drv.h>
+
  /**
   * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO *

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -0
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, 
 void *cpu_pt_addr,
   {
       void __iomem *ptr = (void *)cpu_pt_addr;
       uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
         /*
        * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev, 
 void *cpu_pt_addr,
       value = addr & 0x0000FFFFFFFFF000ULL;
       value |= flags;
       writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
       return 0;
   }

  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -37,6 +37,8 @@
     #include "amdgpu_ras.h"

  +#include <drm/drm_drv.h>
+
  static int psp_sysfs_init(struct amdgpu_device *adev); static void psp_sysfs_fini(struct amdgpu_device *adev); @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp, struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr) {
      int ret;
-    int index;
+    int index, idx;
      int timeout = 2000;
      bool ras_intr = false;
      bool skip_unsupport = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -4
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
       if (psp->adev->in_pci_err_recovery)
           return 0;
   +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
       mutex_lock(&psp->mutex);
         memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
       ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
       if (ret) {
           atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
       }
         amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                psp->cmd_buf_mem->cmd_id,
                psp->cmd_buf_mem->resp.status);
           if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
           }
       }
   @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,

          ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo; ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi; }
-    mutex_unlock(&psp->mutex);
  +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
      return ret;
  }
  @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp, if (!cmd)
          return -ENOMEM;
      /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size); +    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size); psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size); @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp) if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size); +    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size); psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_ucode_size);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +18 -5
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
              psp->ta_hdcp_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct 
 device *dev,
       return count;
   }
   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+
   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
              psp_usbc_pd_fw_sysfs_read,

             psp_usbc_pd_fw_sysfs_write);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                 const char *chip_name);
   int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                       uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t 
 bin_size);
+

  #endif

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
   #include "amdgpu.h"
   #include "atom.h"

  +#include <drm/drm_drv.h>
+
  /*
   * Rings
   * Most engines on the GPU are fed via ring buffers.  Ring

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +68 -0
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
       ring->sched.ready = !r;
       return r;
   }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void 
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
       *ring->cond_exe_cpu_addr = cond_exec;
   }
   -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
   -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
   -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);
     int amdgpu_ring_test_helper(struct amdgpu_ring *ring);

  diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
         /* Provide the PSP KDB to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
         /* Provide the PSP SPL to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct 
 psp_context *psp)
       if (ret)
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/psp_v3_1.c static int psp_v3_1_bootloader_load_sos(struct · line 148

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

maintainer
#45 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

The is also the possibility to have the drm_dev_enter/exit much more high level.

E.g. we should it have anyway on every IOCTL and what remains are work items, scheduler threads and interrupts.

Christian.

Am 19.01.21 um 16:35 schrieb Andrey Grodzovsky:

Quoted 10 lines

There is really no other way according to this article https://lwn.net/Articles/767885/

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user to prevent them from yanking a device and we cannot check for a presence change after every device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?
The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Quoted 646 lines

Andrey

Quoted 640 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 631 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57
++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53
+++++++++++++---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70
++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++------------------- drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++----- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +--- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---

  9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
     #include <linux/iommu.h>

  +#include <drm/drm_drv.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device 
 *adev, uint32_t offset)
    */
   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;

  +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if (offset < adev->rmmio_size)
          writeb(value, adev->rmmio + offset);
      else
          BUG();
+
+    drm_dev_exit(idx);
  }
    /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device 
 *adev,
               uint32_t reg, uint32_t v,
               uint32_t acc_flags)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if ((reg * 4) < adev->rmmio_size) {

          if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev) &&

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
       }
         trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
   }
     /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device 
 *adev,
   void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                    uint32_t reg, uint32_t v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+

      if (amdgpu_sriov_fullaccess(adev) && adev->gfx.rlc.funcs && adev->gfx.rlc.funcs->is_rlcg_access_range) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +32 -0
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct 
 amdgpu_device *adev,
       } else {
           writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, 
 u32 reg)
    */
   void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if ((reg * 4) < adev->rio_mem_size)
           iowrite32(v, adev->rio_mem + (reg * 4));
       else {
           iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
           iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device 
 *adev, u32 index)
    */
   void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           writel(v, adev->doorbell.ptr + index);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device 
 *adev, u32 index)
    */
   void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct 
 amdgpu_device *adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags);
       pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct 
 amdgpu_device *adev,
       writel(reg_data, pcie_data_offset);
       readl(pcie_data_offset);
       spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
   }
     /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct 
 amdgpu_device *adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags);
       pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct 
 amdgpu_device *adev,
       writel((u32)(reg_data >> 32), pcie_data_offset);
       readl(pcie_data_offset);
       spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
   }
     /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
   #include "amdgpu_ras.h"
   #include "amdgpu_xgmi.h"

  +#include <drm/drm_drv.h>
+
  /**
   * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO *

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -0
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device 
 *adev, void *cpu_pt_addr,
   {
       void __iomem *ptr = (void *)cpu_pt_addr;
       uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
         /*
        * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device 
 *adev, void *cpu_pt_addr,
       value = addr & 0x0000FFFFFFFFF000ULL;
       value |= flags;
       writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
       return 0;
   }

  diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -37,6 +37,8 @@
     #include "amdgpu_ras.h"

  +#include <drm/drm_drv.h>
+
  static int psp_sysfs_init(struct amdgpu_device *adev); static void psp_sysfs_fini(struct amdgpu_device *adev); @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp, struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr) {
      int ret;
-    int index;
+    int index, idx;
      int timeout = 2000;
      bool ras_intr = false;
      bool skip_unsupport = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -4
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
       if (psp->adev->in_pci_err_recovery)
           return 0;
   +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
       mutex_lock(&psp->mutex);
         memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
       ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
       if (ret) {
           atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
       }
         amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                psp->cmd_buf_mem->cmd_id,
                psp->cmd_buf_mem->resp.status);
           if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
           }
       }
   @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,

          ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo; ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi; }
-    mutex_unlock(&psp->mutex);
  +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
      return ret;
  }
  @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp, if (!cmd)
          return -ENOMEM;
      /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size); +    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size); psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size); @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp) if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size); +    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size); psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_ucode_size);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +1 -1
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,

                   psp->fw_pri_mc_addr,

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +17 -4
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, 
 psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
              psp->ta_hdcp_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, 
 psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, 
 psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t 
 psp_usbc_pd_fw_sysfs_write(struct device *dev,
       return count;
   }
   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+
   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
              psp_usbc_pd_fw_sysfs_read,

             psp_usbc_pd_fw_sysfs_write);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                 const char *chip_name);
   int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                       uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, 
 uint32_t bin_size);
+

  #endif

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
   #include "amdgpu.h"
   #include "atom.h"

  +#include <drm/drm_drv.h>
+
  /*
   * Rings
   * Most engines on the GPU are fed via ring buffers.  Ring

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring 
 *ring)
       ring->sched.ready = !r;
       return r;

  }

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +25 -0
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v; +    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +16 -0
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +24 -0
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void 
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
       *ring->cond_exe_cpu_addr = cond_exec;
   }
   -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, 
 uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
   -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
   -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);

    int amdgpu_ring_test_helper(struct amdgpu_ring *ring);
  diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
         /* Provide the PSP KDB to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
         /* Provide the PSP SPL to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int 
 psp_v11_0_bootloader_load_sysdrv(struct psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int 
 psp_v12_0_bootloader_load_sysdrv(struct psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +2 -4
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int 
 psp_v3_1_bootloader_load_sysdrv(struct psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct 
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#46 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

In reply to Andrey Grodzovsky
Quoted 11 lines · Andrey Grodzovsky

There is really no other way according to this article
https://lwn.net/Articles/767885/

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user
to prevent them from yanking a device and we cannot check for a presence change after every
device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?

The read side is supposed to be dirt cheap, the write side is were we just stall for all readers to eventually complete on their own. Definitely should be much cheaper than mmio read, on the mmio write side it might actually hurt a bit. Otoh I think those don't stall the cpu by default when they're timing out, so maybe if the overhead is too much for those, we could omit them?

Maybe just do a small microbenchmark for these for testing, with a register that doesn't change hw state. So with and without drm_dev_enter/exit, and also one with the hw plugged out so that we have actual timeouts in the transactions.
-Daniel

Quoted 645 lines

The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 634 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 624 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
  drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70 ++++++++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++-------------------
  drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++-----
  drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +---
  drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---
  9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +50 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
    #include <linux/iommu.h>
  +#include <drm/drm_drv.h>
+
  MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
  MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev,
 uint32_t offset)
   */
  void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t
 value)
  {
+    int idx;
+
      if (adev->in_pci_err_recovery)
          return;
  +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if (offset < adev->rmmio_size)
          writeb(value, adev->rmmio + offset);
      else
          BUG();
+
+    drm_dev_exit(idx);
  }
    /**
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
              uint32_t reg, uint32_t v,
              uint32_t acc_flags)
  {
+    int idx;
+
      if (adev->in_pci_err_recovery)
          return;
  +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if ((reg * 4) < adev->rmmio_size) {
          if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
              amdgpu_sriov_runtime(adev) &&
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
      }
        trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
  }
    /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
  void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                   uint32_t reg, uint32_t v)
  {
+    int idx;
+
      if (adev->in_pci_err_recovery)
          return;
  +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if (amdgpu_sriov_fullaccess(adev) &&
          adev->gfx.rlc.funcs &&
          adev->gfx.rlc.funcs->is_rlcg_access_range) {
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
      } else {
          writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
      }
+
+    drm_dev_exit(idx);
  }
    /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
   */
  void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
  {
+    int idx;
+
      if (adev->in_pci_err_recovery)
          return;
  +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if ((reg * 4) < adev->rio_mem_size)
          iowrite32(v, adev->rio_mem + (reg * 4));
      else {
          iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
          iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
      }
+
+    drm_dev_exit(idx);
  }
    /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32
 index)
   */
  void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
  {
+    int idx;
+
      if (adev->in_pci_err_recovery)
          return;
  +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if (index < adev->doorbell.num_doorbells) {
          writel(v, adev->doorbell.ptr + index);
      } else {
          DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
      }
+
+    drm_dev_exit(idx);
  }
    /**
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev,
 u32 index)
   */
  void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
  {
+    int idx;
+
      if (adev->in_pci_err_recovery)
          return;
  +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
      if (index < adev->doorbell.num_doorbells) {
          atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
      } else {
          DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
      }
+
+    drm_dev_exit(idx);
  }
    /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device
 *adev,
      unsigned long flags;
      void __iomem *pcie_index_offset;
      void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
        spin_lock_irqsave(&adev->pcie_idx_lock, flags);
      pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
      writel(reg_data, pcie_data_offset);
      readl(pcie_data_offset);
      spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
  }
    /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
      unsigned long flags;
      void __iomem *pcie_index_offset;
      void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
        spin_lock_irqsave(&adev->pcie_idx_lock, flags);
      pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
      writel((u32)(reg_data >> 32), pcie_data_offset);
      readl(pcie_data_offset);
      spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
  }
    /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +8 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
  #include "amdgpu_ras.h"
  #include "amdgpu_xgmi.h"
  +#include <drm/drm_drv.h>
+
  /**
   * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO
   *
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
  {
      void __iomem *ptr = (void *)cpu_pt_addr;
      uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
        /*
       * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
      value = addr & 0x0000FFFFFFFFF000ULL;
      value |= flags;
      writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
      return 0;
  }

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +7 -5
@@ -37,6 +37,8 @@
    #include "amdgpu_ras.h"
  +#include <drm/drm_drv.h>
+
  static int psp_sysfs_init(struct amdgpu_device *adev);
  static void psp_sysfs_fini(struct amdgpu_device *adev);
  @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
             struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
  {
      int ret;
-    int index;
+    int index, idx;
      int timeout = 2000;
      bool ras_intr = false;
      bool skip_unsupport = false;
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
      if (psp->adev->in_pci_err_recovery)
          return 0;
  +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
      mutex_lock(&psp->mutex);
        memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
      ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr,
 index);
      if (ret) {
          atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
      }
        amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
               psp->cmd_buf_mem->cmd_id,
               psp->cmd_buf_mem->resp.status);
          if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
          }
      }
  @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,
          ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
          ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
      }
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +22 -9
-    mutex_unlock(&psp->mutex);
  +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
      return ret;
  }
  @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp,
      if (!cmd)
          return -ENOMEM;
      /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size);
+    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size);
        psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size);
  @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp)
      if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
+    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size);
        psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr,
                    psp->asd_ucode_size);
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
      if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
        psp_prep_ta_load_cmd_buf(cmd,
                   psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
      if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
        psp_prep_ta_load_cmd_buf(cmd,
                   psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
      if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
             psp->ta_hdcp_ucode_size);
        psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
      if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
        psp_prep_ta_load_cmd_buf(cmd,
                   psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
      if (!cmd)
          return -ENOMEM;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
        psp_prep_ta_load_cmd_buf(cmd,
                   psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct
 device *dev,
      return count;
  }
  +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size)
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+
  static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
             psp_usbc_pd_fw_sysfs_read,
             psp_usbc_pd_fw_sysfs_write);
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                const char *chip_name);
  int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                      uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size);
+
  #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +69 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
  #include "amdgpu.h"
  #include "atom.h"
  +#include <drm/drm_drv.h>
+
  /*
   * Rings
   * Most engines on the GPU are fed via ring buffers.  Ring
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
      ring->sched.ready = !r;
      return r;
  }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
      *ring->cond_exe_cpu_addr = cond_exec;
  }
  -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
  -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
  -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);
    int amdgpu_ring_test_helper(struct amdgpu_ring *ring);
  diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c

b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c
index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
        /* Provide the PSP KDB to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
        /* Provide the PSP SPL to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
        /* Provide the sys driver to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
        /* Provide the PSP secure OS to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
        /* Provide the sys driver to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
        /* Provide the PSP secure OS to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +2 -4
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
        /* Provide the sys driver to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct
 psp_context *psp)
      if (ret)
          return ret;
  -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
      /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
        /* Provide the PSP secure OS to bootloader */
      WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#47 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 19 lines · Daniel Vetter
Quoted 10 lines · Andrey Grodzovsky

There is really no other way according to this article
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flwn.net%2FArticles%2F767885%2F&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7a1f5ae6a06f4661d47708d8bca4cb32%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466763278674162%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=QupsglO9WRuis8XRLBFIhl6miTXVOdAnk8oP4BfSclQ%3D&amp;reserved=0

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user
to prevent them from yanking a device and we cannot check for a presence change after every
device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?

The read side is supposed to be dirt cheap, the write side is were we just stall for all readers to eventually complete on their own. Definitely should be much cheaper than mmio read, on the mmio write side it might actually hurt a bit. Otoh I think those don't stall the cpu by default when they're timing out, so maybe if the overhead is too much for those, we could omit them?

Quoted 5 lines

Maybe just do a small microbenchmark for these for testing, with a register that doesn't change hw state. So with and without drm_dev_enter/exit, and also one with the hw plugged out so that we have actual timeouts in the transactions.
-Daniel

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

Andrey

Quoted 635 lines
Quoted 635 lines

The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 623 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 615 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70 ++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++-------------------
   drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++-----
   drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +---
   drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---
   9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +50 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
     #include <linux/iommu.h>
   +#include <drm/drm_drv.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev,
 uint32_t offset)
    */
   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t
 value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (offset < adev->rmmio_size)
           writeb(value, adev->rmmio + offset);
       else
           BUG();
+
+    drm_dev_exit(idx);
   }
     /**
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
               uint32_t reg, uint32_t v,
               uint32_t acc_flags)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if ((reg * 4) < adev->rmmio_size) {
           if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
               amdgpu_sriov_runtime(adev) &&
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
       }
         trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
   }
     /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
   void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                    uint32_t reg, uint32_t v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (amdgpu_sriov_fullaccess(adev) &&
           adev->gfx.rlc.funcs &&
           adev->gfx.rlc.funcs->is_rlcg_access_range) {
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
       } else {
           writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
    */
   void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if ((reg * 4) < adev->rio_mem_size)
           iowrite32(v, adev->rio_mem + (reg * 4));
       else {
           iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
           iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32
 index)
    */
   void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           writel(v, adev->doorbell.ptr + index);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev,
 u32 index)
    */
   void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
       }
+
+    drm_dev_exit(idx);
   }
     /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device
 *adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags);
       pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
       writel(reg_data, pcie_data_offset);
       readl(pcie_data_offset);
       spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
   }
     /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags);
       pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
       writel((u32)(reg_data >> 32), pcie_data_offset);
       readl(pcie_data_offset);
       spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
   }
     /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +8 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
   #include "amdgpu_ras.h"
   #include "amdgpu_xgmi.h"
   +#include <drm/drm_drv.h>
+
   /**
    * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO
    *
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
   {
       void __iomem *ptr = (void *)cpu_pt_addr;
       uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
         /*
        * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
       value = addr & 0x0000FFFFFFFFF000ULL;
       value |= flags;
       writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
       return 0;
   }
   diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c

b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c
index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +7 -5
@@ -37,6 +37,8 @@
     #include "amdgpu_ras.h"
   +#include <drm/drm_drv.h>
+
   static int psp_sysfs_init(struct amdgpu_device *adev);
   static void psp_sysfs_fini(struct amdgpu_device *adev);
   @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
              struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
   {
       int ret;
-    int index;
+    int index, idx;
       int timeout = 2000;
       bool ras_intr = false;
       bool skip_unsupport = false;
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
       if (psp->adev->in_pci_err_recovery)
           return 0;
   +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
       mutex_lock(&psp->mutex);
         memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
       ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr,
 index);
       if (ret) {
           atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
       }
         amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                psp->cmd_buf_mem->cmd_id,
                psp->cmd_buf_mem->resp.status);
           if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
           }
       }
   @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,
           ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
           ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
       }
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +22 -9
-    mutex_unlock(&psp->mutex);
   +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
       return ret;
   }
   @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp,
       if (!cmd)
           return -ENOMEM;
       /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size);
+    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size);
         psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size);
   @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
+    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size);
         psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr,
                     psp->asd_ucode_size);
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
              psp->ta_hdcp_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
       if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct
 device *dev,
       return count;
   }
   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size)
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+
   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
              psp_usbc_pd_fw_sysfs_read,
              psp_usbc_pd_fw_sysfs_write);
drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                 const char *chip_name);
   int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                       uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size);
+
   #endif
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +69 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
   #include "amdgpu.h"
   #include "atom.h"
   +#include <drm/drm_drv.h>
+
   /*
    * Rings
    * Most engines on the GPU are fed via ring buffers.  Ring
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
       ring->sched.ready = !r;
       return r;
   }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
       *ring->cond_exe_cpu_addr = cond_exec;
   }
   -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
   -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
   -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);
     int amdgpu_ring_test_helper(struct amdgpu_ring *ring);
   diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c

b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c
index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
         /* Provide the PSP KDB to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
         /* Provide the PSP SPL to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct
 psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
         /* Provide the sys driver to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct
 psp_context *psp)
       if (ret)
Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/psp_v3_1.c static int psp_v3_1_bootloader_load_sos(struct · line 148

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
         /* Provide the PSP secure OS to bootloader */
       WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#48 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 29 lines · Daniel Vetter
Quoted 20 lines
Quoted 11 lines · Andrey Grodzovsky

There is really no other way according to this article
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flwn.net%2FArticles%2F767885%2F&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7a1f5ae6a06f4661d47708d8bca4cb32%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466763278674162%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=QupsglO9WRuis8XRLBFIhl6miTXVOdAnk8oP4BfSclQ%3D&amp;reserved=0

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user
to prevent them from yanking a device and we cannot check for a presence change after every
device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?

The read side is supposed to be dirt cheap, the write side is were we just stall for all readers to eventually complete on their own. Definitely should be much cheaper than mmio read, on the mmio write side it might actually hurt a bit. Otoh I think those don't stall the cpu by default when they're timing out, so maybe if the overhead is too much for those, we could omit them?

Quoted 5 lines

Maybe just do a small microbenchmark for these for testing, with a register that doesn't change hw state. So with and without drm_dev_enter/exit, and also one with the hw plugged out so that we have actual timeouts in the transactions.
-Daniel

Quoted 2 lines

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

Quoted 691 lines

Andrey

Quoted 677 lines
Quoted 677 lines

The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 665 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 657 lines

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57
++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++ drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53
+++++++++++++---------
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70
++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49
++-------------------
   drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++----- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +--- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---

   9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
      #include <linux/iommu.h>

   +#include <drm/drm_drv.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device 
 *adev,
 uint32_t offset)
     */

   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (offset < adev->rmmio_size)
           writeb(value, adev->rmmio + offset);
       else
           BUG();
+
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device 
 *adev,
                uint32_t reg, uint32_t v,
                uint32_t acc_flags)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rmmio_size) {

           if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev) &&

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device 
 *adev,
        }
 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
    }
      /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device 
 *adev,
    void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                     uint32_t reg, uint32_t v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+

       if (amdgpu_sriov_fullaccess(adev) &&
           adev->gfx.rlc.funcs &&
adev->gfx.rlc.funcs->is_rlcg_access_range) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +14 -0
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct 
 amdgpu_device *adev,
        } else {
            writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device 
 *adev, u32 reg)
     */
    void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rio_mem_size)
            iowrite32(v, adev->rio_mem + (reg * 4));
        else {
            iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
            iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device 
 *adev, u32
 index)
     */
    void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if (index < adev->doorbell.num_doorbells) {
            writel(v, adev->doorbell.ptr + index);
        } else {
            DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
        }
+
+    drm_dev_exit(idx);
    }

     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct 
 amdgpu_device *adev,
 u32 index)
     */
    void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if (index < adev->doorbell.num_doorbells) {
            atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
        } else {
            DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
        }
+
+    drm_dev_exit(idx);
    }

     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct 

amdgpu_device
*adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags); pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct 
 amdgpu_device *adev,
        writel(reg_data, pcie_data_offset);
        readl(pcie_data_offset);
        spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct 

amdgpu_device
*adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
         spin_lock_irqsave(&adev->pcie_idx_lock, flags); pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct 

amdgpu_device
*adev,
       writel((u32)(reg_data >> 32), pcie_data_offset); readl(pcie_data_offset); spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); +
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"

   +#include <drm/drm_drv.h>
+
   /**
    * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO *

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +4 -0
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct 
 amdgpu_device *adev,
 void *cpu_pt_addr,
    {
        void __iomem *ptr = (void *)cpu_pt_addr;
        uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
          /*

        * The following is for PTE only. GART does not have PDEs.

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +3 -0
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct 
 amdgpu_device *adev,
 void *cpu_pt_addr,
        value = addr & 0x0000FFFFFFFFF000ULL;
        value |= flags;
        writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
        return 0;

   }
   diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -37,6 +37,8 @@
      #include "amdgpu_ras.h"

   +#include <drm/drm_drv.h>
+
   static int psp_sysfs_init(struct amdgpu_device *adev); static void psp_sysfs_fini(struct amdgpu_device *adev); @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp, struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr) {
       int ret;
-    int index;
+    int index, idx;
       int timeout = 2000;
       bool ras_intr = false;
       bool skip_unsupport = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -4
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
        if (psp->adev->in_pci_err_recovery)
            return 0;
    +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
        mutex_lock(&psp->mutex);
          memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
        ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr,
 index);
        if (ret) {
            atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
        }
          amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                 psp->cmd_buf_mem->cmd_id,
                 psp->cmd_buf_mem->resp.status);
            if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
            }
        }
    @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,

           ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo; ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi; }
-    mutex_unlock(&psp->mutex);
   +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
       return ret;
   }
   @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp, if (!cmd)
           return -ENOMEM;
       /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size); +    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size); psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size); @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp) if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
+    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size); psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_ucode_size);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +1 -1
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,

                    psp->fw_pri_mc_addr,

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +1 -1
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, 
 psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, 
 psp->ta_ras_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,

                    psp->fw_pri_mc_addr,

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +2 -2
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context 
 *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
               psp->ta_hdcp_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context 
 *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, 
 psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, 
 psp->ta_dtm_ucode_size);

         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +1 -1
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context 
 *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, 
 psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, 
 psp->ta_rap_ucode_size);

         psp_prep_ta_load_cmd_buf(cmd,
                    psp->fw_pri_mc_addr,

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -2778,6 +2777,20 @@ static ssize_t 

psp_usbc_pd_fw_sysfs_write(struct
device *dev,
       return count;
   }
   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +13 -0
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+

   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR, psp_usbc_pd_fw_sysfs_read, psp_usbc_pd_fw_sysfs_write);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +2 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context 
 *psp,
                  const char *chip_name);
    int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                        uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, 

uint32_t
bin_size);
+
   #endif

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
    #include "amdgpu.h"
    #include "atom.h"

   +#include <drm/drm_drv.h>
+
   /*
    * Rings
    * Most engines on the GPU are fed via ring buffers. Ring

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct 
 amdgpu_ring *ring)
        ring->sched.ready = !r;
        return r;

   }

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +25 -0
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v; +    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +16 -0
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +24 -0
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
        *ring->cond_exe_cpu_addr = cond_exec;
    }
    -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, 
 uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
    -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
    -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);

     int amdgpu_ring_test_helper(struct amdgpu_ring *ring); diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c
index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +2 -4
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
          /* Provide the PSP KDB to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
          /* Provide the PSP SPL to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int 

psp_v11_0_bootloader_load_sysdrv(struct
psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size); +    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size); /* Provide the sys driver to bootloader */ WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +1 -2
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +0 -0
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int 

psp_v12_0_bootloader_load_sysdrv(struct
psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size); +    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size); /* Provide the sys driver to bootloader */ WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +1 -2
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +0 -0
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int 

psp_v3_1_bootloader_load_sysdrv(struct
psp_context *psp)
       if (ret)
           return ret;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
       /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size); +    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size); /* Provide the sys driver to bootloader */ WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +0 -0
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/psp_v3_1.c static int psp_v3_1_bootloader_load_sos(struct · line 148

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#49 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 39 lines · Christian König

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 29 lines · Daniel Vetter
Quoted 20 lines
Quoted 11 lines · Andrey Grodzovsky

There is really no other way according to this article
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flwn.net%2FArticles%2F767885%2F&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7Cee61fb937d2d4baedf6f08d8bcac5b02%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466795752297305%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=a9Y4ZMEVYaMP7IeMVxQgXGpAkDXSkedMAiWkyqwzEe8%3D&amp;reserved=0

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user
to prevent them from yanking a device and we cannot check for a presence change after every
device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?

The read side is supposed to be dirt cheap, the write side is were we just stall for all readers to eventually complete on their own. Definitely should be much cheaper than mmio read, on the mmio write side it might actually hurt a bit. Otoh I think those don't stall the cpu by default when they're timing out, so maybe if the overhead is too much for those, we could omit them?

Quoted 5 lines

Maybe just do a small microbenchmark for these for testing, with a register that doesn't change hw state. So with and without drm_dev_enter/exit, and also one with the hw plugged out so that we have actual timeouts in the transactions.
-Daniel

Quoted 2 lines

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 5 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

Will do.

Andrey

Quoted 662 lines
Quoted 662 lines

Andrey

Quoted 657 lines
Quoted 655 lines

The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 643 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 635 lines

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++

   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++

   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70
++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++------------------- drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++----- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +--- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---

   9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
      #include <linux/iommu.h>

   +#include <drm/drm_drv.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev,
 uint32_t offset)
     */

   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (offset < adev->rmmio_size)
           writeb(value, adev->rmmio + offset);
       else
           BUG();
+
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
                uint32_t reg, uint32_t v,
                uint32_t acc_flags)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rmmio_size) {

           if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev) &&

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
        }
 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
    }
      /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
    void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                     uint32_t reg, uint32_t v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+

       if (amdgpu_sriov_fullaccess(adev) &&
           adev->gfx.rlc.funcs &&
adev->gfx.rlc.funcs->is_rlcg_access_range) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +8 -0
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
        } else {
            writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 
 reg)
     */
    void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rio_mem_size)
            iowrite32(v, adev->rio_mem + (reg * 4));
        else {
            iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
            iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, 

u32
index)
    */
   void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v) {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           writel(v, adev->doorbell.ptr + index);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); }
+
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +18 -0
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev,
 u32 index)
     */
    void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if (index < adev->doorbell.num_doorbells) {
            atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
        } else {
            DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device
 *adev,
        unsigned long flags;
        void __iomem *pcie_index_offset;
        void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
          spin_lock_irqsave(&adev->pcie_idx_lock, flags);
        pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device 
 *adev,
        writel(reg_data, pcie_data_offset);
        readl(pcie_data_offset);
 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
        unsigned long flags;
        void __iomem *pcie_index_offset;
        void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
          spin_lock_irqsave(&adev->pcie_idx_lock, flags);
        pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
        writel((u32)(reg_data >> 32), pcie_data_offset);
        readl(pcie_data_offset);
 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"

   +#include <drm/drm_drv.h>
+
   /**
    * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO *

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -0
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
    {
        void __iomem *ptr = (void *)cpu_pt_addr;
        uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
          /*
         * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
        value = addr & 0x0000FFFFFFFFF000ULL;
        value |= flags;
        writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
        return 0;
    }

   diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -37,6 +37,8 @@
      #include "amdgpu_ras.h"

   +#include <drm/drm_drv.h>
+
   static int psp_sysfs_init(struct amdgpu_device *adev); static void psp_sysfs_fini(struct amdgpu_device *adev); @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp, struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr) {
       int ret;
-    int index;
+    int index, idx;
       int timeout = 2000;
       bool ras_intr = false;
       bool skip_unsupport = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -4
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
        if (psp->adev->in_pci_err_recovery)
            return 0;
    +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
        mutex_lock(&psp->mutex);
          memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
        ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr,
 index);
        if (ret) {
            atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
        }
          amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                 psp->cmd_buf_mem->cmd_id,
                 psp->cmd_buf_mem->resp.status);
            if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
            }
        }
    @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,

           ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo; ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi; }
-    mutex_unlock(&psp->mutex);
   +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
       return ret;
   }
   @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp, if (!cmd)
           return -ENOMEM;
       /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size); +    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size); psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size); @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp) if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size); +    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size); psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_ucode_size);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -5
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, 
 psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
               psp->ta_hdcp_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, 
 psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, 
 psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct
 device *dev,
        return count;
    }

   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +13 -0
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+

   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR, psp_usbc_pd_fw_sysfs_read, psp_usbc_pd_fw_sysfs_write);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                  const char *chip_name);
    int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                        uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size);
+

   #endif

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
    #include "amdgpu.h"
    #include "atom.h"

   +#include <drm/drm_drv.h>
+
   /*
    * Rings
    * Most engines on the GPU are fed via ring buffers. Ring

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +25 -0
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
        ring->sched.ready = !r;
        return r;
    }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v; +    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +16 -0
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +24 -0
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
        *ring->cond_exe_cpu_addr = cond_exec;
    }
    -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
    -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
    -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);

     int amdgpu_ring_test_helper(struct amdgpu_ring *ring); diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c
index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
          /* Provide the PSP KDB to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
          /* Provide the PSP SPL to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +2 -4
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
Quoted 6 lines
Quoted 5 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7Cee61fb937d2d4baedf6f08d8bcac5b02%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466795752297305%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=a5MkPkwHh7WkR24K9EoCWSKPdCpiXCJH6RwGbGyhHyA%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#50 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 32 lines · Andrey Grodzovsky
Quoted 30 lines · Christian König

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 29 lines · Daniel Vetter
Quoted 25 lines
Quoted 11 lines · Andrey Grodzovsky

There is really no other way according to this article
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flwn.net%2FArticles%2F767885%2F&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7Cee61fb937d2d4baedf6f08d8bcac5b02%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466795752297305%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=a9Y4ZMEVYaMP7IeMVxQgXGpAkDXSkedMAiWkyqwzEe8%3D&amp;reserved=0

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user
to prevent them from yanking a device and we cannot check for a presence change after every
device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?

The read side is supposed to be dirt cheap, the write side is were we just stall for all readers to eventually complete on their own. Definitely should be much cheaper than mmio read, on the mmio write side it might actually hurt a bit. Otoh I think those don't stall the cpu by default when they're timing out, so maybe if the overhead is too much for those, we could omit them?

Maybe just do a small microbenchmark for these for testing, with a register that doesn't change hw state. So with and without drm_dev_enter/exit, and also one with the hw plugged out so that we have actual timeouts in the transactions.
-Daniel

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Just realized, I can't test this part since I don't have eGPU to yank out.

Andrey

Quoted 683 lines
Quoted 6 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

Will do.

Andrey

Quoted 670 lines
Quoted 669 lines

Andrey

Quoted 649 lines
Quoted 649 lines

The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 637 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 629 lines

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++

   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++

   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70
++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++------------------- drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++----- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +--- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---

   9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
      #include <linux/iommu.h>

   +#include <drm/drm_drv.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev,
 uint32_t offset)
     */

   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (offset < adev->rmmio_size)
           writeb(value, adev->rmmio + offset);
       else
           BUG();
+
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
                uint32_t reg, uint32_t v,
                uint32_t acc_flags)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rmmio_size) {

           if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev) &&

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
        }
 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
    }
      /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
    void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                     uint32_t reg, uint32_t v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+

       if (amdgpu_sriov_fullaccess(adev) &&
           adev->gfx.rlc.funcs &&
adev->gfx.rlc.funcs->is_rlcg_access_range) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +14 -0
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device 
 *adev,
        } else {
            writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 
 reg)
     */
    void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rio_mem_size)
            iowrite32(v, adev->rio_mem + (reg * 4));
        else {
            iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
            iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device 
 *adev, u32
 index)
     */
    void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if (index < adev->doorbell.num_doorbells) {
            writel(v, adev->doorbell.ptr + index);
        } else {
            DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
        }
+
+    drm_dev_exit(idx);
    }

     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +12 -0
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev,
 u32 index)
     */
    void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if (index < adev->doorbell.num_doorbells) {
            atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
        } else {
            DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device
 *adev,
        unsigned long flags;
        void __iomem *pcie_index_offset;
        void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
        pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct 
 amdgpu_device *adev,
        writel(reg_data, pcie_data_offset);
        readl(pcie_data_offset);
 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct 

amdgpu_device
*adev,
       unsigned long flags;
       void __iomem *pcie_index_offset;
       void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
spin_lock_irqsave(&adev->pcie_idx_lock, flags); pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -0
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
        writel((u32)(reg_data >> 32), pcie_data_offset);
        readl(pcie_data_offset);
 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"

   +#include <drm/drm_drv.h>
+
   /**
    * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO *

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -0
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
    {
        void __iomem *ptr = (void *)cpu_pt_addr;
        uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
          /*
         * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
        value = addr & 0x0000FFFFFFFFF000ULL;
        value |= flags;
        writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
        return 0;
    }

   diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -37,6 +37,8 @@
      #include "amdgpu_ras.h"

   +#include <drm/drm_drv.h>
+
   static int psp_sysfs_init(struct amdgpu_device *adev); static void psp_sysfs_fini(struct amdgpu_device *adev); @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp, struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr) {
       int ret;
-    int index;
+    int index, idx;
       int timeout = 2000;
       bool ras_intr = false;
       bool skip_unsupport = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -4
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
        if (psp->adev->in_pci_err_recovery)
            return 0;
    +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
        mutex_lock(&psp->mutex);
          memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
        ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr,
 index);
        if (ret) {
            atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
        }
          amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                 psp->cmd_buf_mem->cmd_id,
                 psp->cmd_buf_mem->resp.status);
            if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
            }
        }
    @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,

           ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo; ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi; }
-    mutex_unlock(&psp->mutex);
   +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
       return ret;
   }
   @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp, if (!cmd)
           return -ENOMEM;
       /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size); +    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size); psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size); @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp) if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size); +    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size); psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_ucode_size);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -5
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, 
 psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
               psp->ta_hdcp_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, 
 psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, 
 psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct
 device *dev,
        return count;
    }

   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +13 -0
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+

   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR, psp_usbc_pd_fw_sysfs_read, psp_usbc_pd_fw_sysfs_write);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                  const char *chip_name);
    int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                        uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size);
+

   #endif

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
    #include "amdgpu.h"
    #include "atom.h"

   +#include <drm/drm_drv.h>
+
   /*
    * Rings
    * Most engines on the GPU are fed via ring buffers. Ring

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +25 -0
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
        ring->sched.ready = !r;
        return r;
    }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v; +    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +16 -0
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +24 -0
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
        *ring->cond_exe_cpu_addr = cond_exec;
    }
    -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
    -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
    -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);

     int amdgpu_ring_test_helper(struct amdgpu_ring *ring); diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c
index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
          /* Provide the PSP KDB to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
          /* Provide the PSP SPL to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/psp_v3_1.c static int psp_v3_1_bootloader_load_sos(struct · line 148

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +1 -2
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7Cee61fb937d2d4baedf6f08d8bcac5b02%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466795752297305%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=a5MkPkwHh7WkR24K9EoCWSKPdCpiXCJH6RwGbGyhHyA%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#51 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

In reply to Christian König
Quoted 39 lines · Christian König

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 29 lines · Daniel Vetter
Quoted 20 lines
Quoted 11 lines · Andrey Grodzovsky

There is really no other way according to this article
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flwn.net%2FArticles%2F767885%2F&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7Cee61fb937d2d4baedf6f08d8bcac5b02%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466795752297305%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=a9Y4ZMEVYaMP7IeMVxQgXGpAkDXSkedMAiWkyqwzEe8%3D&amp;reserved=0

"A perfect solution seems nearly impossible though; we cannot acquire a mutex on the user
to prevent them from yanking a device and we cannot check for a presence change after every
device access for performance reasons. "

But I assumed srcu_read_lock should be pretty seamless performance wise, no ?

The read side is supposed to be dirt cheap, the write side is were we just stall for all readers to eventually complete on their own. Definitely should be much cheaper than mmio read, on the mmio write side it might actually hurt a bit. Otoh I think those don't stall the cpu by default when they're timing out, so maybe if the overhead is too much for those, we could omit them?

Quoted 5 lines

Maybe just do a small microbenchmark for these for testing, with a register that doesn't change hw state. So with and without drm_dev_enter/exit, and also one with the hw plugged out so that we have actual timeouts in the transactions.
-Daniel

Quoted 2 lines

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 5 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +0 -0
index 3763921..1650549 100644
@@ -873,6 +873,11 @@ static int gfx_v8_0_ring_test_ring(struct amdgpu_ring *ring)

        if (i >= adev->usec_timeout)
                r = -ETIMEDOUT;

+       DRM_ERROR("Before write 1M times to scratch register"); +       for (i = 0; i < 1000000; i++)
+               WREG32(scratch, 0xDEADBEEF);
+       DRM_ERROR("After write 1M times to scratch register"); +
 error_free_scratch:
        amdgpu_gfx_scratch_free(adev, scratch); return r;

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
index 5f4805e..7ecbfef 100644
@@ -1063,6 +1063,11 @@ static int gfx_v9_0_ring_test_ring(struct amdgpu_ring *ring)

        if (i >= adev->usec_timeout)
                r = -ETIMEDOUT;

+       DRM_ERROR("Before write 1M times to scratch register"); +       for (i = 0; i < 1000000; i++)
+               WREG32(scratch, 0xDEADBEEF);
+       DRM_ERROR("After write 1M times to scratch register"); +
 error_free_scratch:
        amdgpu_gfx_scratch_free(adev, scratch); return r;

Andrey

Andrey

Quoted 662 lines
Quoted 662 lines

Andrey

Quoted 657 lines
Quoted 655 lines

The other solution would be as I suggested to keep all the device IO ranges reserved and system
memory pages unfreed until the device is finalized in the driver but Daniel said this would upset the PCI layer (the MMIO ranges reservation part).

Andrey

Quoted 643 lines · Christian König

Am 18.01.21 um 22:01 schrieb Andrey Grodzovsky:

Quoted 2 lines

This should prevent writing to memory or IO ranges possibly already allocated for other uses after our device is removed.

Wow, that adds quite some overhead to every register access. I'm not sure we can do this.

Christian.

Quoted 635 lines

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 57 ++++++++++++++++++++++++

   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c    |  9 ++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c    | 53 +++++++++++++---------
   drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h    |  3 ++

   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c   | 70
++++++++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h   | 49 ++------------------- drivers/gpu/drm/amd/amdgpu/psp_v11_0.c     | 16 ++----- drivers/gpu/drm/amd/amdgpu/psp_v12_0.c     |  8 +--- drivers/gpu/drm/amd/amdgpu/psp_v3_1.c      |  8 +---

   9 files changed, 184 insertions(+), 89 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
index e99f4f1..0a9d73c 100644
@@ -72,6 +72,8 @@
      #include <linux/iommu.h>

   +#include <drm/drm_drv.h>
+
   MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
   MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -0
@@ -404,13 +406,21 @@ uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev,
 uint32_t offset)
     */

   void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
   {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (offset < adev->rmmio_size)
           writeb(value, adev->rmmio + offset);
       else
           BUG();
+
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -427,9 +437,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
                uint32_t reg, uint32_t v,
                uint32_t acc_flags)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rmmio_size) {

           if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev) &&

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
@@ -444,6 +459,8 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
        }
 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
+
+    drm_dev_exit(idx);
    }
      /*
@@ -454,9 +471,14 @@ void amdgpu_device_wreg(struct amdgpu_device *adev,
    void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
                     uint32_t reg, uint32_t v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+

       if (amdgpu_sriov_fullaccess(adev) &&
           adev->gfx.rlc.funcs &&
adev->gfx.rlc.funcs->is_rlcg_access_range) {

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +8 -0
@@ -465,6 +487,8 @@ void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
        } else {
            writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -499,15 +523,22 @@ u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 
 reg)
     */
    void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if ((reg * 4) < adev->rio_mem_size)
            iowrite32(v, adev->rio_mem + (reg * 4));
        else {
            iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
            iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -544,14 +575,21 @@ u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, 

u32
index)
    */
   void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v) {
+    int idx;
+
       if (adev->in_pci_err_recovery)
           return;
   +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
       if (index < adev->doorbell.num_doorbells) {
           writel(v, adev->doorbell.ptr + index);
       } else {
           DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); }
+
+    drm_dev_exit(idx);
   }
     /**

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +18 -0
@@ -588,14 +626,21 @@ u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev,
 u32 index)
     */
    void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
    {
+    int idx;
+
        if (adev->in_pci_err_recovery)
            return;
    +    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
+
        if (index < adev->doorbell.num_doorbells) {
            atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
        } else {
            DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
        }
+
+    drm_dev_exit(idx);
    }
      /**
@@ -682,6 +727,10 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device
 *adev,
        unsigned long flags;
        void __iomem *pcie_index_offset;
        void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
          spin_lock_irqsave(&adev->pcie_idx_lock, flags);
        pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -692,6 +741,8 @@ void amdgpu_device_indirect_wreg(struct amdgpu_device 
 *adev,
        writel(reg_data, pcie_data_offset);
        readl(pcie_data_offset);
 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
@@ -711,6 +762,10 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
        unsigned long flags;
        void __iomem *pcie_index_offset;
        void __iomem *pcie_data_offset;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return;
          spin_lock_irqsave(&adev->pcie_idx_lock, flags);
        pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
@@ -727,6 +782,8 @@ void amdgpu_device_indirect_wreg64(struct amdgpu_device
 *adev,
        writel((u32)(reg_data >> 32), pcie_data_offset);
        readl(pcie_data_offset);
 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
+
+    drm_dev_exit(idx);
    }
      /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index fe1a39f..1beb4e6 100644
@@ -31,6 +31,8 @@
    #include "amdgpu_ras.h"
    #include "amdgpu_xgmi.h"

   +#include <drm/drm_drv.h>
+
   /**
    * amdgpu_gmc_get_pde_for_bo - get the PDE for a BO *

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +7 -0
@@ -98,6 +100,10 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
    {
        void __iomem *ptr = (void *)cpu_pt_addr;
        uint64_t value;
+    int idx;
+
+    if (!drm_dev_enter(&adev->ddev, &idx))
+        return 0;
          /*
         * The following is for PTE only. GART does not have PDEs.
@@ -105,6 +111,9 @@ int amdgpu_gmc_set_pte_pde(struct amdgpu_device *adev,
 void *cpu_pt_addr,
        value = addr & 0x0000FFFFFFFFF000ULL;
        value |= flags;
        writeq(value, ptr + (gpu_page_idx * 8));
+
+    drm_dev_exit(idx);
+
        return 0;
    }

   diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c index 523d22d..89e2bfe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +0 -0
@@ -37,6 +37,8 @@
      #include "amdgpu_ras.h"

   +#include <drm/drm_drv.h>
+
   static int psp_sysfs_init(struct amdgpu_device *adev); static void psp_sysfs_fini(struct amdgpu_device *adev); @@ -248,7 +250,7 @@ psp_cmd_submit_buf(struct psp_context *psp, struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr) {
       int ret;
-    int index;
+    int index, idx;
       int timeout = 2000;
       bool ras_intr = false;
       bool skip_unsupport = false;

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -4
@@ -256,6 +258,9 @@ psp_cmd_submit_buf(struct psp_context *psp,
        if (psp->adev->in_pci_err_recovery)
            return 0;
    +    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return 0;
+
        mutex_lock(&psp->mutex);
          memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
@@ -266,8 +271,7 @@ psp_cmd_submit_buf(struct psp_context *psp,
        ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr,
 index);
        if (ret) {
            atomic_dec(&psp->fence_value);
-        mutex_unlock(&psp->mutex);
-        return ret;
+        goto exit;
        }
          amdgpu_asic_invalidate_hdp(psp->adev, NULL);
@@ -307,8 +311,8 @@ psp_cmd_submit_buf(struct psp_context *psp,
                 psp->cmd_buf_mem->cmd_id,
                 psp->cmd_buf_mem->resp.status);
            if (!timeout) {
-            mutex_unlock(&psp->mutex);
-            return -EINVAL;
+            ret = -EINVAL;
+            goto exit;
            }
        }
    @@ -316,8 +320,10 @@ psp_cmd_submit_buf(struct psp_context *psp,

           ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo; ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi; }
-    mutex_unlock(&psp->mutex);
   +exit:
+    mutex_unlock(&psp->mutex);
+    drm_dev_exit(idx);
       return ret;
   }
   @@ -354,8 +360,7 @@ static int psp_load_toc(struct psp_context *psp, if (!cmd)
           return -ENOMEM;
       /* Copy toc to psp firmware private buffer */
-    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size); +    psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size); psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size); @@ -570,8 +575,7 @@ static int psp_asd_load(struct psp_context *psp) if (!cmd)
           return -ENOMEM;
   -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size); +    psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size); psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_ucode_size);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +5 -5
@@ -726,8 +730,7 @@ static int psp_xgmi_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, 
 psp->ta_xgmi_ucode_size);
+    psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -982,8 +985,7 @@ static int psp_ras_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, 
 psp->ta_ras_ucode_size);
+    psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -1219,8 +1221,7 @@ static int psp_hdcp_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
+    psp_copy_fw(psp, psp->ta_hdcp_start_addr,
               psp->ta_hdcp_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
@@ -1366,8 +1367,7 @@ static int psp_dtm_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, 
 psp->ta_dtm_ucode_size);
+    psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -1507,8 +1507,7 @@ static int psp_rap_load(struct psp_context *psp)
        if (!cmd)
            return -ENOMEM;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-    memcpy(psp->fw_pri_buf, psp->ta_rap_start_addr, 
 psp->ta_rap_ucode_size);
+    psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
          psp_prep_ta_load_cmd_buf(cmd,
                     psp->fw_pri_mc_addr,
@@ -2778,6 +2777,20 @@ static ssize_t psp_usbc_pd_fw_sysfs_write(struct
 device *dev,
        return count;
    }

   +void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c +13 -0
+{
+    int idx;
+
+    if (!drm_dev_enter(&psp->adev->ddev, &idx))
+        return;
+
+    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
+    memcpy(psp->fw_pri_buf, start_addr, bin_size);
+
+    drm_dev_exit(idx);
+}
+
+

   static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR, psp_usbc_pd_fw_sysfs_read, psp_usbc_pd_fw_sysfs_write);

drivers/gpu/drm/amd/amdgpu/amdgpu_psp.h +3 -0
index da250bc..ac69314 100644
@@ -400,4 +400,7 @@ int psp_init_ta_microcode(struct psp_context *psp,
                  const char *chip_name);
    int psp_get_fw_attestation_records_addr(struct psp_context *psp,
                        uint64_t *output_ptr);
+
+void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t
 bin_size);
+

   #endif

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +0 -0
index 1a612f5..d656494 100644
@@ -35,6 +35,8 @@
    #include "amdgpu.h"
    #include "atom.h"

   +#include <drm/drm_drv.h>
+
   /*
    * Rings
    * Most engines on the GPU are fed via ring buffers. Ring

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +25 -0
@@ -463,3 +465,71 @@ int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
        ring->sched.ready = !r;
        return r;
    }
+
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
+{
+    int idx;
+    int i = 0;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    while (i <= ring->buf_mask)
+        ring->ring[i++] = ring->funcs->nop;
+
+    drm_dev_exit(idx);
+
+}
+
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
+{
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (ring->count_dw <= 0)
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");
+    ring->ring[ring->wptr++ & ring->buf_mask] = v; +    ring->wptr &= ring->ptr_mask;
+    ring->count_dw--;

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +16 -0
+
+    drm_dev_exit(idx);
+}
+
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw)
+{
+    unsigned occupied, chunk1, chunk2;
+    void *dst;
+    int idx;
+
+    if (!drm_dev_enter(&ring->adev->ddev, &idx))
+        return;
+
+    if (unlikely(ring->count_dw < count_dw))
+        DRM_ERROR("amdgpu: writing more dwords to the ring than 

expected!\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +24 -0
+
+    occupied = ring->wptr & ring->buf_mask;
+    dst = (void *)&ring->ring[occupied];
+    chunk1 = ring->buf_mask + 1 - occupied;
+    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
+    chunk2 = count_dw - chunk1;
+    chunk1 <<= 2;
+    chunk2 <<= 2;
+
+    if (chunk1)
+        memcpy(dst, src, chunk1);
+
+    if (chunk2) {
+        src += chunk1;
+        dst = (void *)ring->ring;
+        memcpy(dst, src, chunk2);
+    }
+
+    ring->wptr += count_dw;
+    ring->wptr &= ring->ptr_mask;
+    ring->count_dw -= count_dw;
+
+    drm_dev_exit(idx);
+}
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.h +4 -42
index accb243..f90b81f 100644
@@ -300,53 +300,12 @@ static inline void
 amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
        *ring->cond_exe_cpu_addr = cond_exec;
    }
    -static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
-{
-    int i = 0;
-    while (i <= ring->buf_mask)
-        ring->ring[i++] = ring->funcs->nop;
-
-}
-
-static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
-{
-    if (ring->count_dw <= 0)
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-    ring->ring[ring->wptr++ & ring->buf_mask] = v;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw--;
-}
+void amdgpu_ring_clear_ring(struct amdgpu_ring *ring);
    -static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
-                          void *src, int count_dw)
-{
-    unsigned occupied, chunk1, chunk2;
-    void *dst;
-
-    if (unlikely(ring->count_dw < count_dw))
-        DRM_ERROR("amdgpu: writing more dwords to the ring than 
 expected!\n");
-
-    occupied = ring->wptr & ring->buf_mask;
-    dst = (void *)&ring->ring[occupied];
-    chunk1 = ring->buf_mask + 1 - occupied;
-    chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
-    chunk2 = count_dw - chunk1;
-    chunk1 <<= 2;
-    chunk2 <<= 2;
+void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v);
    -    if (chunk1)
-        memcpy(dst, src, chunk1);
-
-    if (chunk2) {
-        src += chunk1;
-        dst = (void *)ring->ring;
-        memcpy(dst, src, chunk2);
-    }
-
-    ring->wptr += count_dw;
-    ring->wptr &= ring->ptr_mask;
-    ring->count_dw -= count_dw;
-}
+void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
+                          void *src, int count_dw);

     int amdgpu_ring_test_helper(struct amdgpu_ring *ring); diff --git a/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c b/drivers/gpu/drm/amd/amdgpu/psp_v11_0.c
index bd4248c..b3ce5be 100644

drivers/gpu/drm/amd/amdgpu/psp_v11_0.c +4 -8
@@ -269,10 +269,8 @@ static int psp_v11_0_bootloader_load_kdb(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP KDB binary to memory */
-    memcpy(psp->fw_pri_buf, psp->kdb_start_addr, psp->kdb_bin_size);
+    psp_copy_fw(psp, psp->kdb_start_addr, psp->kdb_bin_size);
          /* Provide the PSP KDB to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -302,10 +300,8 @@ static int psp_v11_0_bootloader_load_spl(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP SPL binary to memory */
-    memcpy(psp->fw_pri_buf, psp->spl_start_addr, psp->spl_bin_size);
+    psp_copy_fw(psp, psp->spl_start_addr, psp->spl_bin_size);
          /* Provide the PSP SPL to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -335,10 +331,8 @@ static int psp_v11_0_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -371,10 +365,8 @@ static int psp_v11_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v12_0.c +2 -4
index c4828bd..618e5b6 100644
@@ -138,10 +138,8 @@ static int psp_v12_0_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -179,10 +177,8 @@ static int psp_v12_0_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
drivers/gpu/drm/amd/amdgpu/psp_v3_1.c +2 -4
index f2e725f..d0a6cccd 100644
@@ -102,10 +102,8 @@ static int psp_v3_1_bootloader_load_sysdrv(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy PSP System Driver binary to memory */
-    memcpy(psp->fw_pri_buf, psp->sys_start_addr, psp->sys_bin_size);
+    psp_copy_fw(psp, psp->sys_start_addr, psp->sys_bin_size);
          /* Provide the sys driver to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
@@ -143,10 +141,8 @@ static int psp_v3_1_bootloader_load_sos(struct
 psp_context *psp)
        if (ret)
            return ret;
    -    memset(psp->fw_pri_buf, 0, PSP_1_MEG);
-
        /* Copy Secure OS binary to PSP memory */
-    memcpy(psp->fw_pri_buf, psp->sos_start_addr, psp->sos_bin_size);
+    psp_copy_fw(psp, psp->sos_start_addr, psp->sos_bin_size);
          /* Provide the PSP secure OS to bootloader */
        WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_36,
Quoted 6 lines
Quoted 5 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7Cee61fb937d2d4baedf6f08d8bcac5b02%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637466795752297305%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=a5MkPkwHh7WkR24K9EoCWSKPdCpiXCJH6RwGbGyhHyA%3D&amp;reserved=0

with drm_dev_enter

[   20.606168 <    0.053014>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   20.914669 <    0.308501>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   20.914857 <    0.000188>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   21.224795 <    0.309938>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   21.224986 <    0.000191>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   21.533422 <    0.308436>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   21.843633 <    0.000164>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   22.152756 <    0.309123>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   22.152934 <    0.000178>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   22.462544 <    0.309610>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   22.462721 <    0.000177>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   22.771921 <    0.309200>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   22.772128 <    0.000207>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   23.081148 <    0.309020>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   23.081331 <    0.000183>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   23.391600 <    0.310269>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   23.391783 <    0.000183>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   23.702026 <    0.310243>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   25.330532 <    0.000112>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   25.642806 <    0.312274>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   25.643123 <    0.000317>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   25.954685 <    0.311562>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   25.954906 <    0.000221>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   26.266457 <    0.311551>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   26.266675 <    0.000218>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   26.579848 <    0.313173>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   26.580066 <    0.000218>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   26.891740 <    0.311674>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   26.891958 <    0.000218>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   27.203947 <    0.311989>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   27.204166 <    0.000219>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   27.516040 <    0.311874>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   27.516265 <    0.000225>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   27.828137 <    0.311872>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   27.828356 <    0.000219>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   28.140182 <    0.311826>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   28.140395 <    0.000213>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   28.452405 <    0.312010>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register

without drm_dev_enter

[   28.519096 <    0.049775>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   28.728785 <    0.209689>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   28.728946 <    0.000161>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   28.938814 <    0.209868>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   28.938979 <    0.000165>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   29.148659 <    0.209680>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   29.148809 <    0.000150>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   29.358514 <    0.209705>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   29.358664 <    0.000150>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   29.568358 <    0.209694>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   29.568508 <    0.000150>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   29.778389 <    0.209881>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   29.778539 <    0.000150>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   29.988240 <    0.209701>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   29.988391 <    0.000151>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   30.198077 <    0.209686>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   30.198228 <    0.000151>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   30.407928 <    0.209700>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   30.408079 <    0.000151>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   30.617796 <    0.209717>] [drm:gfx_v8_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   32.222242 <    0.000086>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   32.431423 <    0.209181>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   32.431834 <    0.000411>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   32.641075 <    0.209241>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   32.641268 <    0.000193>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   32.850482 <    0.209214>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   32.850671 <    0.000189>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   33.059900 <    0.209229>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   33.060092 <    0.000192>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   33.269336 <    0.209244>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   33.269526 <    0.000190>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   33.478741 <    0.209215>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   33.478931 <    0.000190>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   33.688259 <    0.209328>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   33.688449 <    0.000190>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   33.897756 <    0.209307>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   33.897946 <    0.000190>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   34.107256 <    0.209310>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register
[   34.107445 <    0.000189>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* Before write 1M times to scratch register
[   34.316758 <    0.209313>] [drm:gfx_v9_0_ring_test_ring [amdgpu]] *ERROR* After write 1M times to scratch register

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

maintainer
#52 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 19 lines · Christian König
Quoted 17 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 5 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Quoted 8 lines
Quoted 1 line

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Christian.
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#53 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 23 lines · Christian König

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 22 lines · Christian König
Quoted 18 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 5 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

Quoted 7 lines
Quoted 5 lines

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

Quoted 2 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower). Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Andrey

Quoted 2 lines

Christian.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#54 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 27 lines · Christian König
Quoted 25 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 24 lines · Christian König
Quoted 13 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 5 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 5 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 8 lines
Quoted 2 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower). Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 9 lines

Andrey

Quoted 2 lines

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#55 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Daniel, ping. Also, please refer to the other thread with Bjorn from pci-dev on the same topic I added you to.

Andrey

Quoted 48 lines · Christian König

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 27 lines · Christian König
Quoted 25 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 24 lines · Christian König
Quoted 13 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 5 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 5 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 8 lines
Quoted 2 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower). Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Quoted 17 lines

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 10 lines

Andrey

Quoted 2 lines

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7e63c7ba9ac44d80163108d8c48b9507%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637475451078731703%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=SozIYYmHpkk%2B4PRycs8T7x1DYagThy6lQoFXV5Ddamk%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#56 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 3 lines · Andrey Grodzovsky

Daniel, ping. Also, please refer to the other thread with Bjorn from pci-dev on the same topic I added you to.

Summarizing my take over there for here plus maybe some more clarification. There's two problems:

- You must guarantee that after the ->remove callback of your driver is finished, there's no more mmio or any other hw access. A combination of stopping stuff and drm_dev_enter/exit can help with that. This prevents the use-after-free issue.

- For the actual hotunplug time, i.e. anything that can run while your driver is used up to the point where ->remove callback has finished stopp hw access you must guarantee that code doesn't blow up when it gets bogus reads (in the form of 0xff values). drm_dev_enter/exit can't help you with that. Plus you should make sure that we're not spending forever waiting for a big pile of mmio access all to time out because you never bail out - some coarse-grained drm_dev_enter/exit might help here.

Plus finally the userspace access problem: You must guarantee that after ->remove has finished that none of the uapi or cross-driver access points (driver ioctl, dma-buf, dma-fence, anything else that hangs around) can reach the data structures/memory mappings/whatever which have been released as part of your ->remove callback. drm_dev_enter/exit is again the tool of choice here.

So you have to use drm_dev_enter/exit for some of the problems we face on hotunplug, but it's not the tool that can handle the actual hw hotunplug race conditions for you.

Unfortunately the hw hotunplug race condition is an utter pain to test, since essentially you need to validate your driver against spurious 0xff reads at any moment. And I don't even have a clever idea to simulate this, e.g. by forcefully replacing the iobar mapping: What we'd need is a mapping that allows reads (so we can fill a page with 0xff and use that everywhere), but instead of rejecting writes, allows them, but drops them (so that the 0xff stays intact). Maybe we could simulate this with some kernel debug tricks (kinda like mmiotrace) with a read-only mapping and dropping every write every time we fault. But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?
-Daniel

Quoted 82 lines

Andrey

Quoted 78 lines · Christian König

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 35 lines · Christian König
Quoted 33 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 32 lines · Christian König
Quoted 14 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 6 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged
and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 7 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 4 lines
Quoted 3 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Quoted 9 lines

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the
device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower).
Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 10 lines

Andrey

Quoted 2 lines

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7e63c7ba9ac44d80163108d8c48b9507%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637475451078731703%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=SozIYYmHpkk%2B4PRycs8T7x1DYagThy6lQoFXV5Ddamk%3D&amp;reserved=0

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#57 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 45 lines · Daniel Vetter
Quoted 3 lines · Andrey Grodzovsky

Daniel, ping. Also, please refer to the other thread with Bjorn from pci-dev on the same topic I added you to.

Summarizing my take over there for here plus maybe some more clarification. There's two problems:

- You must guarantee that after the ->remove callback of your driver is finished, there's no more mmio or any other hw access. A combination of stopping stuff and drm_dev_enter/exit can help with that. This prevents the use-after-free issue.

- For the actual hotunplug time, i.e. anything that can run while your driver is used up to the point where ->remove callback has finished stopp hw access you must guarantee that code doesn't blow up when it gets bogus reads (in the form of 0xff values). drm_dev_enter/exit can't help you with that. Plus you should make sure that we're not spending forever waiting for a big pile of mmio access all to time out because you never bail out - some coarse-grained drm_dev_enter/exit might help here.

Plus finally the userspace access problem: You must guarantee that after ->remove has finished that none of the uapi or cross-driver access points (driver ioctl, dma-buf, dma-fence, anything else that hangs around) can reach the data structures/memory mappings/whatever which have been released as part of your ->remove callback. drm_dev_enter/exit is again the tool of choice here.

So you have to use drm_dev_enter/exit for some of the problems we face on hotunplug, but it's not the tool that can handle the actual hw hotunplug race conditions for you.

Unfortunately the hw hotunplug race condition is an utter pain to test, since essentially you need to validate your driver against spurious 0xff reads at any moment. And I don't even have a clever idea to simulate this, e.g. by forcefully replacing the iobar mapping: What we'd need is a mapping that allows reads (so we can fill a page with 0xff and use that everywhere), but instead of rejecting writes, allows them, but drops them (so that the 0xff stays intact). Maybe we could simulate this with some kernel debug tricks (kinda like mmiotrace) with a read-only mapping and dropping every write every time we fault. But ugh ...

Quoted 4 lines

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?
-Daniel

Not sure it's not a dump idea but still, worth asking - what if I just simply quietly return early from the .remove callback without doing anything there, the driver will not be aware that the device is removed and will at least try to continue working as usual including IOCTLs, job scheduling e.t.c. On the other hand all MMIO read accesses will start returning ~0, regarding rejecting writes - I don't see anywhere we test for result of writing (e.g. amdgpu_mm_wreg8) so seems they will just seamlessly go through... Or is it the pci_dev that will be freed by PCI core itself and so I will immediately crash ?

Andrey

Quoted 76 lines
Quoted 76 lines

Andrey

Quoted 71 lines · Christian König

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 35 lines · Christian König
Quoted 33 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 32 lines · Christian König
Quoted 14 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 6 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged
and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 7 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 4 lines
Quoted 3 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Quoted 9 lines

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the
device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower).
Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 4 lines

Andrey

Quoted 1 line
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 1 line

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7810d8d6f03443ce2e0408d8ca22ea99%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637481598615581693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=zTV6FTpL3titmMTVEPxxVT8e5lTKVsLViwZudEsNn%2Bw%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#58 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 54 lines · Andrey Grodzovsky
Quoted 45 lines · Daniel Vetter
Quoted 3 lines · Andrey Grodzovsky

Daniel, ping. Also, please refer to the other thread with Bjorn from pci-dev on the same topic I added you to.

Summarizing my take over there for here plus maybe some more clarification. There's two problems:

- You must guarantee that after the ->remove callback of your driver is finished, there's no more mmio or any other hw access. A combination of stopping stuff and drm_dev_enter/exit can help with that. This prevents the use-after-free issue.

- For the actual hotunplug time, i.e. anything that can run while your driver is used up to the point where ->remove callback has finished stopp hw access you must guarantee that code doesn't blow up when it gets bogus reads (in the form of 0xff values). drm_dev_enter/exit can't help you with that. Plus you should make sure that we're not spending forever waiting for a big pile of mmio access all to time out because you never bail out - some coarse-grained drm_dev_enter/exit might help here.

Plus finally the userspace access problem: You must guarantee that after ->remove has finished that none of the uapi or cross-driver access points (driver ioctl, dma-buf, dma-fence, anything else that hangs around) can reach the data structures/memory mappings/whatever which have been released as part of your ->remove callback. drm_dev_enter/exit is again the tool of choice here.

So you have to use drm_dev_enter/exit for some of the problems we face on hotunplug, but it's not the tool that can handle the actual hw hotunplug race conditions for you.

Unfortunately the hw hotunplug race condition is an utter pain to test, since essentially you need to validate your driver against spurious 0xff reads at any moment. And I don't even have a clever idea to simulate this, e.g. by forcefully replacing the iobar mapping: What we'd need is a mapping that allows reads (so we can fill a page with 0xff and use that everywhere), but instead of rejecting writes, allows them, but drops them (so that the 0xff stays intact). Maybe we could simulate this with some kernel debug tricks (kinda like mmiotrace) with a read-only mapping and dropping every write every time we fault. But ugh ...

Quoted 4 lines

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?
-Daniel

Quoted 9 lines

Not sure it's not a dump idea but still, worth asking - what if I just simply quietly return early from the .remove callback without doing anything there, the driver will not be aware that the device is removed and will at least try to continue working as usual including IOCTLs, job scheduling e.t.c. On the other hand all MMIO read accesses will start returning ~0, regarding rejecting writes - I don't see anywhere we test for result of writing (e.g. amdgpu_mm_wreg8) so seems they will just seamlessly go through... Or is it the pci_dev that will be freed by PCI core itself and so I will immediately crash ?

This still requires that you physically unplug the device, so not something you can do in CI. Plus it doesn't allow you to easily fake a hotunplug in the middle of something interesting like an atomic modeset commit. If you instead punch out the mmio mapping with some pte trick, you can intercept the faults and count down until you actually switch over to only returning 0xff. This allows you to sweep through entire complex execution flows so that you have a guarantee you've actually caught everything.

If otoh you just hotunplug and don't clean up (or equivalent, insert a long sleep at the beginning of your ->remove hook) then you just check that at the beginning of each operation there's a check that bails out.

It's better than nothing for prototyping, but I don't think it's useful in a CI setting to assure stuff stays fixed. -Daniel

Quoted 88 lines

Andrey

Quoted 86 lines
Quoted 83 lines

Andrey

Quoted 78 lines · Christian König

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 35 lines · Christian König
Quoted 33 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 32 lines · Christian König
Quoted 14 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 6 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged
and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 7 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 4 lines
Quoted 3 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Quoted 9 lines

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the
device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower).
Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 10 lines

Andrey

Quoted 2 lines

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7810d8d6f03443ce2e0408d8ca22ea99%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637481598615581693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=zTV6FTpL3titmMTVEPxxVT8e5lTKVsLViwZudEsNn%2Bw%3D&amp;reserved=0

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#59 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

In reply to Daniel Vetter
Quoted 34 lines · Daniel Vetter
Quoted 3 lines · Andrey Grodzovsky

Daniel, ping. Also, please refer to the other thread with Bjorn from pci-dev on the same topic I added you to.

Summarizing my take over there for here plus maybe some more clarification. There's two problems:

- You must guarantee that after the ->remove callback of your driver is finished, there's no more mmio or any other hw access. A combination of stopping stuff and drm_dev_enter/exit can help with that. This prevents the use-after-free issue.

- For the actual hotunplug time, i.e. anything that can run while your driver is used up to the point where ->remove callback has finished stopp hw access you must guarantee that code doesn't blow up when it gets bogus reads (in the form of 0xff values). drm_dev_enter/exit can't help you with that. Plus you should make sure that we're not spending forever waiting for a big pile of mmio access all to time out because you never bail out - some coarse-grained drm_dev_enter/exit might help here.

Plus finally the userspace access problem: You must guarantee that after ->remove has finished that none of the uapi or cross-driver access points (driver ioctl, dma-buf, dma-fence, anything else that hangs around) can reach the data structures/memory mappings/whatever which have been released as part of your ->remove callback. drm_dev_enter/exit is again the tool of choice here.

So you have to use drm_dev_enter/exit for some of the problems we face on hotunplug, but it's not the tool that can handle the actual hw hotunplug race conditions for you.

Quoted 9 lines

Unfortunately the hw hotunplug race condition is an utter pain to test, since essentially you need to validate your driver against spurious 0xff reads at any moment. And I don't even have a clever idea to simulate this, e.g. by forcefully replacing the iobar mapping: What we'd need is a mapping that allows reads (so we can fill a page with 0xff and use that everywhere), but instead of rejecting writes, allows them, but drops them (so that the 0xff stays intact). Maybe we could simulate this with some kernel debug tricks (kinda like mmiotrace) with a read-only mapping and dropping every write every time we fault.

Clarification - as far as I know there are no page fault handlers for kernel
mappings. And we are talking about kernel mappings here, right ? If there were I could solve all those issues the same as I do for user mappings, by invalidating all existing mappings in the kernel (both kmaps and ioreamps)and insert dummy zero or ~0 filled page instead. Also, I assume forcefully remapping the IO BAR to ~0 filled page would involve
ioremap API and it's not something that I think can be easily done according to
am answer i got to a related topic a few weeks ago https://www.spinics.net/lists/linux-pci/msg103396.html (that was the only reply i got)

Quoted 5 lines

But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?

Or I could just for validation purposes return ~0 from all reg reads in the code
and ignore writes if drm_dev_unplugged, this could already easily validate a big portion of the code flow under such scenario.

Andrey

Quoted 87 lines

-Daniel

Quoted 82 lines

Andrey

Quoted 78 lines · Christian König

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 35 lines · Christian König
Quoted 33 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 32 lines · Christian König
Quoted 14 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 6 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged
and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 7 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 4 lines
Quoted 3 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Quoted 9 lines

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the
device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower).
Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 10 lines

Andrey

Quoted 2 lines

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7810d8d6f03443ce2e0408d8ca22ea99%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637481598615581693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=zTV6FTpL3titmMTVEPxxVT8e5lTKVsLViwZudEsNn%2Bw%3D&amp;reserved=0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#60 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 48 lines · Andrey Grodzovsky
Quoted 34 lines · Daniel Vetter
Quoted 3 lines · Andrey Grodzovsky

Daniel, ping. Also, please refer to the other thread with Bjorn from pci-dev on the same topic I added you to.

Summarizing my take over there for here plus maybe some more clarification. There's two problems:

- You must guarantee that after the ->remove callback of your driver is finished, there's no more mmio or any other hw access. A combination of stopping stuff and drm_dev_enter/exit can help with that. This prevents the use-after-free issue.

- For the actual hotunplug time, i.e. anything that can run while your driver is used up to the point where ->remove callback has finished stopp hw access you must guarantee that code doesn't blow up when it gets bogus reads (in the form of 0xff values). drm_dev_enter/exit can't help you with that. Plus you should make sure that we're not spending forever waiting for a big pile of mmio access all to time out because you never bail out - some coarse-grained drm_dev_enter/exit might help here.

Plus finally the userspace access problem: You must guarantee that after ->remove has finished that none of the uapi or cross-driver access points (driver ioctl, dma-buf, dma-fence, anything else that hangs around) can reach the data structures/memory mappings/whatever which have been released as part of your ->remove callback. drm_dev_enter/exit is again the tool of choice here.

So you have to use drm_dev_enter/exit for some of the problems we face on hotunplug, but it's not the tool that can handle the actual hw hotunplug race conditions for you.

Quoted 9 lines

Unfortunately the hw hotunplug race condition is an utter pain to test, since essentially you need to validate your driver against spurious 0xff reads at any moment. And I don't even have a clever idea to simulate this, e.g. by forcefully replacing the iobar mapping: What we'd need is a mapping that allows reads (so we can fill a page with 0xff and use that everywhere), but instead of rejecting writes, allows them, but drops them (so that the 0xff stays intact). Maybe we could simulate this with some kernel debug tricks (kinda like mmiotrace) with a read-only mapping and dropping every write every time we fault.

Quoted 10 lines

Clarification - as far as I know there are no page fault handlers for kernel mappings. And we are talking about kernel mappings here, right ? If there were I could solve all those issues the same as I do for user mappings, by invalidating all existing mappings in the kernel (both kmaps and ioreamps)and insert dummy zero or ~0 filled page instead.
Also, I assume forcefully remapping the IO BAR to ~0 filled page would involve ioremap API and it's not something that I think can be easily done according to am answer i got to a related topic a few weeks ago
https://www.spinics.net/lists/linux-pci/msg103396.html (that was the only reply i got)

mmiotrace can, but only for debug, and only on x86 platforms:

https://www.kernel.org/doc/html/latest/trace/mmiotrace.html

Should be feasible (but maybe not worth the effort) to extend this to support fake unplug.

Quoted 10 lines
Quoted 6 lines

But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?

Or I could just for validation purposes return ~0 from all reg reads in the code and ignore writes if drm_dev_unplugged, this could already easily validate a big portion of the code flow under such scenario.

Hm yeah if your really wrap them all, that should work too. Since iommappings have __iomem pointer type, as long as amdgpu is sparse warning free, should be doable to guarantee this. -Daniel

Quoted 89 lines

Andrey

Quoted 87 lines

-Daniel

Quoted 82 lines

Andrey

Quoted 78 lines · Christian König

Am 29.01.21 um 18:35 schrieb Andrey Grodzovsky:

Quoted 35 lines · Christian König
Quoted 33 lines

Am 28.01.21 um 18:23 schrieb Andrey Grodzovsky:

Quoted 32 lines · Christian König
Quoted 14 lines

Am 19.01.21 um 19:22 schrieb Andrey Grodzovsky:

Quoted 6 lines · Daniel Vetter
Quoted 1 line

[SNIP]

So say writing in a loop to some harmless scratch register for many times both for plugged
and unplugged case and measure total time delta ?

I think we should at least measure the following:

1. Writing X times to a scratch reg without your patch.
2. Writing X times to a scratch reg with your patch.
3. Writing X times to a scratch reg with the hardware physically disconnected.

Quoted 7 lines

I suggest to repeat that once for Polaris (or older) and once for Vega or Navi.

The SRBM on Polaris is meant to introduce some delay in each access, so it might react differently then the newer hardware.

Christian.

See attached results and the testing code. Ran on Polaris (gfx8) and Vega10(gfx9)

In summary, over 1 million WWREG32 in loop with and without this patch you get around 10ms of accumulated overhead ( so 0.00001 millisecond penalty for each WWREG32) for using drm_dev_enter check when writing registers.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

P.S Bullet 3 I cannot test as I need eGPU and currently I don't have one.

Well if I'm not completely mistaken that are 100ms of accumulated overhead. So around 100ns per write. And even bigger problem is that this is a ~67% increase.

My bad, and 67% from what ? How u calculate ?

My bad, (308501-209689)/209689=47% increase.

Quoted 4 lines
Quoted 3 lines

I'm not sure how many write we do during normal operation, but that sounds like a bit much. Ideas?

Quoted 9 lines

Well, u suggested to move the drm_dev_enter way up but as i see it the problem with this is that it increase the chance of race where the
device is extracted after we check for drm_dev_enter (there is also such chance even when it's placed inside WWREG but it's lower).
Earlier I propsed that instead of doing all those guards scattered all over the code simply delay release of system memory pages and unreserve of MMIO ranges to until after the device itself is gone after last drm device reference is dropped. But Daniel opposes delaying MMIO ranges unreserve to after PCI remove code because according to him it will upset the PCI subsytem.

Yeah, that's most likely true as well.

Maybe Daniel has another idea when he's back from vacation.

Christian.

Quoted 10 lines

Andrey

Quoted 2 lines

Christian.

_______________________________________________
amd-gfx mailing list
[email protected]
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.freedesktop.org%2Fmailman%2Flistinfo%2Famd-gfx&amp;data=04%7C01%7CAndrey.Grodzovsky%40amd.com%7C7810d8d6f03443ce2e0408d8ca22ea99%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637481598615581693%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=zTV6FTpL3titmMTVEPxxVT8e5lTKVsLViwZudEsNn%2Bw%3D&amp;reserved=0

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#61 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Am 07.02.21 um 22:50 schrieb Daniel Vetter:

Quoted 11 lines

[SNIP]

Quoted 10 lines

Clarification - as far as I know there are no page fault handlers for kernel mappings. And we are talking about kernel mappings here, right ? If there were I could solve all those issues the same as I do for user mappings, by invalidating all existing mappings in the kernel (both kmaps and ioreamps)and insert dummy zero or ~0 filled page instead.
Also, I assume forcefully remapping the IO BAR to ~0 filled page would involve ioremap API and it's not something that I think can be easily done according to am answer i got to a related topic a few weeks ago
https://www.spinics.net/lists/linux-pci/msg103396.html (that was the only reply i got)

Quoted 6 lines

mmiotrace can, but only for debug, and only on x86 platforms:

https://www.kernel.org/doc/html/latest/trace/mmiotrace.html

Should be feasible (but maybe not worth the effort) to extend this to support fake unplug.

Mhm, interesting idea you guys brought up here.

We don't need a page fault for this to work, all we need to do is to insert dummy PTEs into the kernels page table at the place where previously the MMIO mapping has been.

Quoted 11 lines
Quoted 8 lines
Quoted 5 lines

But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?

Or I could just for validation purposes return ~0 from all reg reads in the code and ignore writes if drm_dev_unplugged, this could already easily validate a big portion of the code flow under such scenario.

Hm yeah if your really wrap them all, that should work too. Since iommappings have __iomem pointer type, as long as amdgpu is sparse warning free, should be doable to guarantee this.

Problem is that ~0 is not always a valid register value.

You would need to audit every register read that it doesn't use the returned value blindly as index or similar. That is quite a bit of work.

Regards,
Christian.

Quoted 4 lines

-Daniel

Quoted 2 lines

Andrey

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#62 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 16 lines · Christian König

Am 07.02.21 um 22:50 schrieb Daniel Vetter:

Quoted 15 lines

[SNIP]

Quoted 10 lines

Clarification - as far as I know there are no page fault handlers for kernel mappings. And we are talking about kernel mappings here, right ? If there were I could solve all those issues the same as I do for user mappings, by invalidating all existing mappings in the kernel (both kmaps and ioreamps)and insert dummy zero or ~0 filled page instead.
Also, I assume forcefully remapping the IO BAR to ~0 filled page would involve ioremap API and it's not something that I think can be easily done according to am answer i got to a related topic a few weeks ago
https://www.spinics.net/lists/linux-pci/msg103396.html (that was the only reply i got)

mmiotrace can, but only for debug, and only on x86 platforms:

https://www.kernel.org/doc/html/latest/trace/mmiotrace.html

Quoted 8 lines
Quoted 2 lines

Should be feasible (but maybe not worth the effort) to extend this to support fake unplug.

Mhm, interesting idea you guys brought up here.

We don't need a page fault for this to work, all we need to do is to insert dummy PTEs into the kernels page table at the place where previously the MMIO mapping has been.

Simply pte trick isn't enough, because we need:
- drop all writes silently
- all reads return 0xff

ptes can't do that themselves, we minimally need write protection and then silently proceed on each write fault without restarting the instruction. Better would be to only catch reads, but x86 doesn't do write-only pte permissions afaik.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?

Or I could just for validation purposes return ~0 from all reg reads in the code and ignore writes if drm_dev_unplugged, this could already easily validate a big portion of the code flow under such scenario.

Quoted 8 lines
Quoted 3 lines

Hm yeah if your really wrap them all, that should work too. Since iommappings have __iomem pointer type, as long as amdgpu is sparse warning free, should be doable to guarantee this.

Problem is that ~0 is not always a valid register value.

You would need to audit every register read that it doesn't use the returned value blindly as index or similar. That is quite a bit of work.

Yeah that's the entire crux here :-/
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#63 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Am 08.02.21 um 10:48 schrieb Daniel Vetter:

Quoted 24 lines · Christian König
Quoted 16 lines

Am 07.02.21 um 22:50 schrieb Daniel Vetter:

Quoted 15 lines

[SNIP]

Quoted 10 lines

Clarification - as far as I know there are no page fault handlers for kernel mappings. And we are talking about kernel mappings here, right ? If there were I could solve all those issues the same as I do for user mappings, by invalidating all existing mappings in the kernel (both kmaps and ioreamps)and insert dummy zero or ~0 filled page instead.
Also, I assume forcefully remapping the IO BAR to ~0 filled page would involve ioremap API and it's not something that I think can be easily done according to am answer i got to a related topic a few weeks ago
https://www.spinics.net/lists/linux-pci/msg103396.html (that was the only reply i got)

mmiotrace can, but only for debug, and only on x86 platforms:

https://www.kernel.org/doc/html/latest/trace/mmiotrace.html

Quoted 7 lines
Quoted 2 lines

Should be feasible (but maybe not worth the effort) to extend this to support fake unplug.

Mhm, interesting idea you guys brought up here.

We don't need a page fault for this to work, all we need to do is to insert dummy PTEs into the kernels page table at the place where previously the MMIO mapping has been.

Quoted 8 lines

Simply pte trick isn't enough, because we need:
- drop all writes silently
- all reads return 0xff

ptes can't do that themselves, we minimally need write protection and then silently proceed on each write fault without restarting the instruction. Better would be to only catch reads, but x86 doesn't do write-only pte permissions afaik.

You are not thinking far enough :)

The dummy PTE is point to a dummy MMIO page which is just never used.

That hast the exact same properties than our removed MMIO space just doesn't goes bananas when a new device is MMIO mapped into that and our driver still tries to write there.

Regards,
Christian.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines

But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?

Or I could just for validation purposes return ~0 from all reg reads in the code and ignore writes if drm_dev_unplugged, this could already easily validate a big portion of the code flow under such scenario.

Hm yeah if your really wrap them all, that should work too. Since iommappings have __iomem pointer type, as long as amdgpu is sparse warning free, should be doable to guarantee this.

Quoted 6 lines
Quoted 4 lines

Problem is that ~0 is not always a valid register value.

You would need to audit every register read that it doesn't use the returned value blindly as index or similar. That is quite a bit of work.

Yeah that's the entire crux here :-/
-Daniel

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#64 lore raw

Re: [PATCH v4 11/14] drm/amdgpu: Guard against write accesses after device removal

Quoted 34 lines · Christian König

Am 08.02.21 um 10:48 schrieb Daniel Vetter:

Quoted 24 lines · Christian König
Quoted 16 lines

Am 07.02.21 um 22:50 schrieb Daniel Vetter:

Quoted 15 lines

[SNIP]

Quoted 10 lines

Clarification - as far as I know there are no page fault handlers for kernel mappings. And we are talking about kernel mappings here, right ? If there were I could solve all those issues the same as I do for user mappings, by invalidating all existing mappings in the kernel (both kmaps and ioreamps)and insert dummy zero or ~0 filled page instead.
Also, I assume forcefully remapping the IO BAR to ~0 filled page would involve ioremap API and it's not something that I think can be easily done according to am answer i got to a related topic a few weeks ago
https://www.spinics.net/lists/linux-pci/msg103396.html (that was the only reply i got)

mmiotrace can, but only for debug, and only on x86 platforms:

https://www.kernel.org/doc/html/latest/trace/mmiotrace.html

Quoted 7 lines
Quoted 2 lines

Should be feasible (but maybe not worth the effort) to extend this to support fake unplug.

Mhm, interesting idea you guys brought up here.

We don't need a page fault for this to work, all we need to do is to insert dummy PTEs into the kernels page table at the place where previously the MMIO mapping has been.

Quoted 8 lines

Simply pte trick isn't enough, because we need:
- drop all writes silently
- all reads return 0xff

ptes can't do that themselves, we minimally need write protection and then silently proceed on each write fault without restarting the instruction. Better would be to only catch reads, but x86 doesn't do write-only pte permissions afaik.

Quoted 7 lines

You are not thinking far enough :)

The dummy PTE is point to a dummy MMIO page which is just never used.

That hast the exact same properties than our removed MMIO space just doesn't goes bananas when a new device is MMIO mapped into that and our driver still tries to write there.

Hm, but where do we get such a "guaranteed never used" mmio page from?

It's a nifty idea indeed otherwise ...
-Daniel

Quoted 24 lines

Regards,
Christian.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

But ugh ...

Otoh validating an entire driver like amdgpu without such a trick against 0xff reads is practically impossible. So maybe you need to add this as one of the tasks here?

Or I could just for validation purposes return ~0 from all reg reads in the code and ignore writes if drm_dev_unplugged, this could already easily validate a big portion of the code flow under such scenario.

Quoted 7 lines
Quoted 3 lines

Hm yeah if your really wrap them all, that should work too. Since iommappings have __iomem pointer type, as long as amdgpu is sparse warning free, should be doable to guarantee this.

Problem is that ~0 is not always a valid register value.

You would need to audit every register read that it doesn't use the returned value blindly as index or similar. That is quite a bit of work.

Yeah that's the entire crux here :-/
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel