Skip to contentSkip to conversation

drm/amdgpu: Track entity initialization status in the driver

PATCH In review Tvrtko Ursulin 2 participants 4 messages Nov 4 – Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Track entity initialization status in the driver

Instead of accessing the internals of the DRM scheduler data structures directly and relying on the own entity backing store to be zeroed on allocation, lets explicitly track whether we have called drm_sched_entity_init() and base on that the decision on whether or not to call drm_sched_entity_destroy().

Alternative could (in theory) be to re-order the driver init/fini flows, but that is really complicated due inter-dependencies between different stages.

Apart from being conceptually cleaner this also re-allows future DRM scheduler work which will embed the run-queue in the scheduler.

Cc Olivier Kaloudoff <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>

---
Considering how many hundreds of kilobytes I have saved so far I hope I can get away with adding a couple of booleans. ;)

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 4 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.h | 1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 4 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.h | 1 +
 4 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +3 -1
index cd35ff7ca864..8f69aa4597e1 100644
@@ -377,7 +377,7 @@ int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
 	void *addr = amdgpu_bo_kptr(adev->uvd.ib_bo);
 	int i, j;
 
-	if (adev->uvd.entity.rq)
+	if (adev->uvd.entity_initialized)
 		drm_sched_entity_destroy(&adev->uvd.entity);
 
 	for (j = 0; j < adev->uvd.num_uvd_inst; ++j) {
@@ -420,6 +420,8 @@ int amdgpu_uvd_entity_init(struct amdgpu_device *adev, struct amdgpu_ring *ring)
 			DRM_ERROR("Failed setting up UVD kernel entity.\n");
 			return r;
 		}
+
+		adev->uvd.entity_initialized = true;
 	}
 
 	return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.h +1 -0
index 9dfad2f48ef4..fcda8a6af839 100644
@@ -62,6 +62,7 @@ struct amdgpu_uvd {
 	struct amdgpu_uvd_inst	inst[AMDGPU_MAX_UVD_INSTANCES];
 	struct drm_file		*filp[AMDGPU_MAX_UVD_HANDLES];
 	atomic_t		handles[AMDGPU_MAX_UVD_HANDLES];
+	bool			entity_initialized;
 	struct drm_sched_entity entity;
 	struct delayed_work	idle_work;
 	unsigned		harvest_config;
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +3 -1
index 3952af9ae115..eede5bb5e24b 100644
@@ -253,7 +253,7 @@ int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
 	if (adev->vce.vcpu_bo == NULL)
 		return 0;
 
-	if (adev->vce.entity.rq)
+	if (adev->vce.entity_initialized)
 		drm_sched_entity_destroy(&adev->vce.entity);
 
 	for (i = 0; i < adev->vce.num_rings; i++)
@@ -288,6 +288,8 @@ int amdgpu_vce_entity_init(struct amdgpu_device *adev, struct amdgpu_ring *ring)
 			DRM_ERROR("Failed setting up VCE run queue.\n");
 			return r;
 		}
+
+		adev->vce.entity_initialized = true;
 	}
 
 	return 0;
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.h +1 -0
index 778c714c8385..f4eb9566a317 100644
@@ -48,6 +48,7 @@ struct amdgpu_vce {
 	struct amdgpu_ring	ring[AMDGPU_MAX_VCE_RINGS];
 	struct amdgpu_irq_src	irq;
 	unsigned		harvest_config;
+	bool			entity_initialized;
 	struct drm_sched_entity	entity;
 	uint32_t                srbm_soft_reset;
 	unsigned		num_rings;
Signature · 1 line
2.56.0
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Tvrtko Ursulin

Instead of accessing the internals of the DRM scheduler data structures directly and relying on the own entity backing store to be zeroed on allocation, lets explicitly track whether we have called drm_sched_entity_init() and base on that the decision on whether or not to call drm_sched_entity_destroy().

Alternative could (in theory) be to re-order the driver init/fini flows, but that is really complicated due inter-dependencies between different stages.

+1, nice.

Quoted 2 lines

Apart from being conceptually cleaner this also re-allows future DRM scheduler work which will embed the run-queue in the scheduler.

Is that related to the spsc_queue work you're doing, or only to Olivier's recent patches?

https://lore.kernel.org/dri-devel/[email protected]/

Quoted 1 line
Quoted 3 lines

---
Considering how many hundreds of kilobytes I have saved so far I hope I can get away with adding a couple of booleans. ;)

You *could* look into reordering the structs to reduce padding? IMO the byte count is irrelevant; so just an idea.

Quoted 15 lines

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 4 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.h | 1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 4 +++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.h | 1 +
 4 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -0
index cd35ff7ca864..8f69aa4597e1 100644
@@ -377,7 +377,7 @@ int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
  	void *addr = amdgpu_bo_kptr(adev->uvd.ib_bo);
  	int i, j;

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c int amdgpu_uvd_sw_fini(struct amdgpu_device *adev) · line 380

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +1 -1
-	if (adev->uvd.entity.rq)
+	if (adev->uvd.entity_initialized)

Just asking, what's the ordering semantics of this like in amdgpu? All guarded by the same outer lock that serializes drm_sched_job_arm() and drm_sched_entity_submit()?

P.

#2 lore raw
Quoted 13 lines · Philipp Stanner
Quoted 9 lines · Tvrtko Ursulin

Instead of accessing the internals of the DRM scheduler data structures directly and relying on the own entity backing store to be zeroed on allocation, lets explicitly track whether we have called drm_sched_entity_init() and base on that the decision on whether or not to call drm_sched_entity_destroy().

Alternative could (in theory) be to re-order the driver init/fini flows, but that is really complicated due inter-dependencies between different stages.

+1, nice.

Quoted 7 lines
Quoted 2 lines

Apart from being conceptually cleaner this also re-allows future DRM scheduler work which will embed the run-queue in the scheduler.

Is that related to the spsc_queue work you're doing, or only to Olivier's recent patches?

https://lore.kernel.org/dri-devel/[email protected]/

Not that one but it is a follow up on 3ad55d18f468 ("drm/amdgpu: guard VCE/UVD sched entity teardown on failed probe").

Quoted 10 lines
Quoted 1 line
Quoted 3 lines

---
Considering how many hundreds of kilobytes I have saved so far I hope I can get away with adding a couple of booleans. ;)

You *could* look into reordering the structs to reduce padding? IMO the byte count is irrelevant; so just an idea.

I had that idea as well some time ago, from my branch called 250221-amdholes-2, so you figure the naming scheme:

31052b84323b (250221-amdholes-2) drm/amdgpu: Reduce holes in struct amdgpu_job fcee566a59ed drm/amdgpu: Remove hole from struct amdgpu_ib af32c97bc278 struct amdgpu_bo_param
337fa2519367 struct amdgpu_bo_list_entry
bc0187dd2856 struct amdgpu_reset_context
429001ac71ef struct amdgpu_reset_domain
856887ace7a0 struct amdgpu_wb
c1fc08af9241 struct amdgpu_pm
95efb111d2f4 struct amdgpu_vmid_mgr
4e6b00a4da43 struct amdgpu_vm_manager
79cffa09930d struct amdgpu_vm_update_params
4f2d88484f3b struct amdgpu_vm
fb8f72204695 struct amdgpu_rlc
b91d7eca6360 struct amdgpu_irq
a0bfd7075a88 struct amdgpu_iv_entry
838956ae96f6 struct amdgpu_ih_ring
12fb7dde6fca stable_pstate
ac62fdab31b7 struct amdgpu_ctx
e89d67894fe4 struct amdgpu_ring {
3f98c5c7e73b struct amdgpu_fence_driver

:)

But it's not that interesting for most of those have no many instances at runtime. And is easy to get re-broken. So I wasn't pursuing it much, only ctx one I got merged. If you are curious there was bigger fish to fry:

6eadd448d7e7 drm/amdgpu: Choose SOC15 RLC register read write functions at init time

or:

789551e08ebe drm/amdgpu: Extend logical to device instance lookup to all devices

Not listing the smaller ones, those two on their own saved half a megabyte. So I can be excused to add a couple of booleans I think. :)

Quoted 16 lines
Quoted 16 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 4 +++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.h | 1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 4 +++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vce.h | 1 +
  4 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -0
index cd35ff7ca864..8f69aa4597e1 100644
@@ -377,7 +377,7 @@ int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
  	void *addr = amdgpu_bo_kptr(adev->uvd.ib_bo);
  	int i, j;
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c int amdgpu_uvd_sw_fini(struct amdgpu_device *adev) · line 380

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +1 -1
-	if (adev->uvd.entity.rq)
+	if (adev->uvd.entity_initialized)

Just asking, what's the ordering semantics of this like in amdgpu? All guarded by the same outer lock that serializes drm_sched_job_arm() and drm_sched_entity_submit()?

This is the driver probe and unbind paths so shouldn't have any connection to job arming etc.

Regards,

Tvrtko

#3 lore raw
In reply to Philipp Stanner

[Gently promoting Alex and Christian from Cc to To]

Would you be okay merging this to decouple amdgpu from scheduler internals a bit more?

Thanks,

Tvrtko

Quoted 57 lines · Philipp Stanner
Quoted 9 lines · Tvrtko Ursulin

Instead of accessing the internals of the DRM scheduler data structures directly and relying on the own entity backing store to be zeroed on allocation, lets explicitly track whether we have called drm_sched_entity_init() and base on that the decision on whether or not to call drm_sched_entity_destroy().

Alternative could (in theory) be to re-order the driver init/fini flows, but that is really complicated due inter-dependencies between different stages.

+1, nice.

Quoted 2 lines

Apart from being conceptually cleaner this also re-allows future DRM scheduler work which will embed the run-queue in the scheduler.

Is that related to the spsc_queue work you're doing, or only to Olivier's recent patches?

https://lore.kernel.org/dri-devel/[email protected]/

Quoted 1 line
Quoted 3 lines

---
Considering how many hundreds of kilobytes I have saved so far I hope I can get away with adding a couple of booleans. ;)

You *could* look into reordering the structs to reduce padding? IMO the byte count is irrelevant; so just an idea.

Quoted 12 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 4 +++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.h | 1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 4 +++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_vce.h | 1 +
  4 files changed, 8 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -0
index cd35ff7ca864..8f69aa4597e1 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c int amdgpu_uvd_sw_fini(struct amdgpu_device *adev) · line 380

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +1 -1
@@ -377,7 +377,7 @@ int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
  	void *addr = amdgpu_bo_kptr(adev->uvd.ib_bo);
  	int i, j;
 
-	if (adev->uvd.entity.rq)
+	if (adev->uvd.entity_initialized)

Just asking, what's the ordering semantics of this like in amdgpu? All guarded by the same outer lock that serializes drm_sched_job_arm() and drm_sched_entity_submit()?

P.