Skip to contentSkip to conversation

drm/sched: Entity handling cleanups

PATCH In review Tvrtko Ursulin 3 participants 14 messages Nov 17 – Nov 18

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

cover drm/sched: Entity handling cleanups

Stricter rules around entity initialization, removing the single scheduler special case which fixes the priority override for amdgpu single IP instance entities and addressing of fence_seq FIXME by pulling the entity->lock up from drm_sched_entity_select_rq().

In the last round sashiko complained about the first patch breaking legacy v3d but Maíra has confirmed that is an unused path so no problem to make it stricter. Lets see what sashiko finds in this round.

Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Maíra Canal <[email protected]>

Tvrtko Ursulin (4):
drm/sched: Disallow initializing entities with un-initialized
schedulers
drm/sched: Ignore stopped entity attempt to change its run-queue drm/sched: Remove the single scheduler special case drm/sched: Address the fence_seq fixme

 drivers/gpu/drm/scheduler/sched_entity.c | 72 +++++++++++++++---------
 drivers/gpu/drm/scheduler/sched_fence.c  |  6 +-
 drivers/gpu/drm/scheduler/sched_main.c   |  7 ++-
 include/drm/gpu_scheduler.h              | 49 ++++++++--------
 4 files changed, 81 insertions(+), 53 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/4 drm/sched: Disallow initializing entities with un-initialized schedulers

Simplify the code and theoretical runtime combinations by disallowing initializing entities with un-initialized schedulers.

Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
I do not *think* any of the drivers relies on this inverted initialization order but it needs more eyes for sure.

---
 drivers/gpu/drm/scheduler/sched_entity.c | 30 +++++++++++++++---------
 1 file changed, 19 insertions(+), 11 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +19 -11
index 03e68b4102d5..2844049d4c66 100644
@@ -119,9 +119,16 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 			  unsigned int num_sched_list,
 			  atomic_t *guilty)
 {
-	if (!entity || !sched_list || !num_sched_list || !sched_list[0])
+	unsigned int i;
+
+	if (!entity || !sched_list || !num_sched_list)
 		return -EINVAL;
 
+	for (i = 0; i < num_sched_list; i++) {
+		if (!sched_list[i] || !sched_list[i]->sched_rq)
+			return -EINVAL;
+	}
+
 	memset(entity, 0, sizeof(struct drm_sched_entity));
 
 	entity->stats = drm_sched_entity_stats_new();
@@ -135,13 +142,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	entity->sched_list = num_sched_list > 1 ? sched_list : NULL;
 	RB_CLEAR_NODE(&entity->rb_tree_node);
 
-	if (!sched_list[0]->sched_rq) {
-		/* Since every entry covered by num_sched_list
-		 * should be non-NULL and therefore we warn drivers
-		 * not to do this and to fix their DRM calling order.
-		 */
-		pr_warn("%s: called with uninitialized scheduler\n", __func__);
-	} else if (priority >= sched_list[0]->num_user_rqs) {
+	if (priority >= sched_list[0]->num_user_rqs) {
 		/*
 		 * The "priority" of an entity cannot exceed the number of
 		 * run-queues of a scheduler. Protect against num_rqs being 0,
@@ -158,8 +159,7 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	entity->priority = priority;
 	entity->rq_priority = drm_sched_policy == DRM_SCHED_POLICY_FAIR ?
 			      DRM_SCHED_PRIORITY_KERNEL : priority;
-	if (sched_list[0]->sched_rq)
-		entity->rq = sched_list[0]->sched_rq[entity->rq_priority];
+	entity->rq = sched_list[0]->sched_rq[entity->rq_priority];
 
 	init_completion(&entity->entity_idle);
 
@@ -192,7 +192,15 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
 				    struct drm_gpu_scheduler **sched_list,
 				    unsigned int num_sched_list)
 {
-	WARN_ON(!num_sched_list || !sched_list);
+	unsigned int i;
+
+	if (WARN_ON(!num_sched_list || !sched_list))
+		return;
+
+	for (i = 0; i < num_sched_list; i++) {
+		if (WARN_ON(!sched_list[i] || !sched_list[i]->sched_rq))
+			return;
+	}
 
 	spin_lock(&entity->lock);
 	entity->sched_list = sched_list;
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/4] drm/sched: Disallow initializing entities with un-initialized schedulers

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/sched: drm_sched_entity_init() breaks v3d_open() on V3D 3.3 - [Medium] drm/sched: uninitialized…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/sched: drm_sched_entity_init() breaks v3d_open() on V3D 3.3
- [Medium] drm/sched: uninitialized SR-IOV VCN0 scheduler in vcn_v3_0_limit_sched()
--

commit 5a80b965f793708bcd7eab260f9a3099009c1c8c
Author: Tvrtko Ursulin <[email protected]>

drm/sched: Disallow initializing entities with un-initialized schedulers

This commit rejects uninitialized schedulers (where sched_rq is NULL) in drm_sched_entity_init() with -EINVAL and in drm_sched_entity_modify_sched() with a WARN_ON() and early return.

Quoted 8 lines
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
index 03e68b4102d50..2844049d4c665 100644
@@ -119,9 +119,16 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 			  unsigned int num_sched_list,
 			  atomic_t *guilty)
 {

drivers/gpu/drm/scheduler/sched_entity.c int drm_sched_entity_init(struct drm_sched_entity *entity, · line 130

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_entity.c +7 -1
-	if (!entity || !sched_list || !num_sched_list || !sched_list[0])
+	unsigned int i;
+
+	if (!entity || !sched_list || !num_sched_list)
 		return -EINVAL;
 
+	for (i = 0; i < num_sched_list; i++) {
+		if (!sched_list[i] || !sched_list[i]->sched_rq)
+			return -EINVAL;
+	}

[Severity: High]
Does returning -EINVAL here break v3d_open() on V3D 3.3 hardware?

On V3D 3.3, v3d_has_csd(v3d) is false, so v3d_sched_init() skips v3d_queue_sched_init() for V3D_CSD and V3D_CACHE_CLEAN, leaving their sched_rq pointers NULL:

drivers/gpu/drm/v3d/v3d_sched.c:v3d_sched_init() {
    ...
	if (v3d_has_csd(v3d)) {
		ret = v3d_queue_sched_init(v3d, &v3d_csd_sched_ops, V3D_CSD,
					   "v3d_csd");
		if (ret)
			goto fail;

		ret = v3d_queue_sched_init(v3d, &v3d_cache_clean_sched_ops,
					   V3D_CACHE_CLEAN, "v3d_cache_clean");
    ...
}

v3d_open() unconditionally calls drm_sched_entity_init() for all V3D_MAX_QUEUES and aborts if it returns an error:

drivers/gpu/drm/v3d/v3d_drv.c:v3d_open() {
    ...
	for (i = 0; i < V3D_MAX_QUEUES; i++) {
		...
		sched = &v3d->queue[i].sched;
		ret = drm_sched_entity_init(&v3d_priv->sched_entity[i],
					    DRM_SCHED_PRIORITY_NORMAL, &sched,
					    1, NULL);
		if (ret)
			goto err_sched;
	}
    ...
}

Previously drm_sched_entity_init() logged a warning and returned 0; with this regression, every open() of the DRM device node on V3D 3.3 fails with -EINVAL.

[ ... ]

Quoted 4 lines
drivers/gpu/drm/scheduler/sched_entity.c +0 -0
@@ -192,7 +192,15 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
 				    struct drm_gpu_scheduler **sched_list,
 				    unsigned int num_sched_list)
 {

drivers/gpu/drm/scheduler/sched_entity.c void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, · line 203

Quoted 10 lines
drivers/gpu/drm/scheduler/sched_entity.c +9 -1
-	WARN_ON(!num_sched_list || !sched_list);
+	unsigned int i;
+
+	if (WARN_ON(!num_sched_list || !sched_list))
+		return;
+
+	for (i = 0; i < num_sched_list; i++) {
+		if (WARN_ON(!sched_list[i] || !sched_list[i]->sched_rq))
+			return;
+	}

[Severity: Medium]
Can vcn_v3_0_limit_sched() hit this WARN_ON() when VCN0 decode is disabled by the hypervisor under SR-IOV?

During vcn_v3_0_sw_init(), amdgpu_ring_init() places &inst[0].ring_dec.sched into adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC][AMDGPU_RING_PRIO_DEFAULT].sched[0]. Later, vcn_v3_0_hw_init() sets ring->no_scheduler = true when the hypervisor has disabled VCN0 decode, which causes amdgpu_device_init_schedulers() to skip drm_sched_init() and leave sched[0]->sched_rq NULL:

drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c:vcn_v3_0_hw_init() {
    ...
			if (amdgpu_vcn_is_disabled_vcn(adev, VCN_DECODE_RING, i)) {
				ring->sched.ready = false;
				ring->no_scheduler = true;
				dev_info(adev->dev, "ring %s is disabled by hypervisor\n", ring->name);
			}
    ...
}

Because vcn_v3_0_early_init() sets adev->vcn.harvest_config = 0 on SR-IOV, an AV1 decode submission on VCN1 reaches vcn_v3_0_limit_sched(), passes the AMDGPU_VCN_HARVEST_VCN0 check, and passes that uninitialized sched[0] to drm_sched_entity_modify_sched():

drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c:vcn_v3_0_limit_sched() {
	struct drm_gpu_scheduler **scheds;

	/* The create msg must be in the first IB submitted */
	if (atomic_read(&job->base.entity->fence_seq))
		return -EINVAL;

	/* if VCN0 is harvested, we can't support AV1 */
	if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
		return -EINVAL;

	scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC]
		[AMDGPU_RING_PRIO_DEFAULT].sched;
	drm_sched_entity_modify_sched(job->base.entity, scheds, 1);
	return 0;
}

An unprivileged guest user submitting an AV1 decode job can trigger the WARN_ON(), and the early return leaves the unsupported AV1 job scheduled on VCN1.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/4] drm/sched: Disallow initializing entities with un-initialized schedulers

+ Christian

Please see below:

Quoted 57 lines · On 17/11/2026 10:46, [email protected] wrote:
Quoted 4 lines
(quoted diff) +0 -0
@@ -192,7 +192,15 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
  				    struct drm_gpu_scheduler **sched_list,
  				    unsigned int num_sched_list)
  {

Review void drm_sched_entity_modify_sched(struct drm_sched_entity *entity, · line 203

Quoted 10 lines
(quoted diff) +9 -1
-	WARN_ON(!num_sched_list || !sched_list);
+	unsigned int i;
+
+	if (WARN_ON(!num_sched_list || !sched_list))
+		return;
+
+	for (i = 0; i < num_sched_list; i++) {
+		if (WARN_ON(!sched_list[i] || !sched_list[i]->sched_rq))
+			return;
+	}

[Severity: Medium]
Can vcn_v3_0_limit_sched() hit this WARN_ON() when VCN0 decode is disabled by the hypervisor under SR-IOV?

During vcn_v3_0_sw_init(), amdgpu_ring_init() places &inst[0].ring_dec.sched into adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC][AMDGPU_RING_PRIO_DEFAULT].sched[0]. Later, vcn_v3_0_hw_init() sets ring->no_scheduler = true when the hypervisor has disabled VCN0 decode, which causes amdgpu_device_init_schedulers() to skip drm_sched_init() and leave sched[0]->sched_rq NULL:

drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c:vcn_v3_0_hw_init() {
     ...
			if (amdgpu_vcn_is_disabled_vcn(adev, VCN_DECODE_RING, i)) {
				ring->sched.ready = false;
				ring->no_scheduler = true;
				dev_info(adev->dev, "ring %s is disabled by hypervisor\n", ring->name);
			}
     ...
}

Because vcn_v3_0_early_init() sets adev->vcn.harvest_config = 0 on SR-IOV, an AV1 decode submission on VCN1 reaches vcn_v3_0_limit_sched(), passes the AMDGPU_VCN_HARVEST_VCN0 check, and passes that uninitialized sched[0] to drm_sched_entity_modify_sched():

drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c:vcn_v3_0_limit_sched() {
	struct drm_gpu_scheduler **scheds;

	/* The create msg must be in the first IB submitted */
	if (atomic_read(&job->base.entity->fence_seq))
		return -EINVAL;

	/* if VCN0 is harvested, we can't support AV1 */
	if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
		return -EINVAL;

	scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC]
		[AMDGPU_RING_PRIO_DEFAULT].sched;
	drm_sched_entity_modify_sched(job->base.entity, scheds, 1);
	return 0;
}
Quoted 3 lines

An unprivileged guest user submitting an AV1 decode job can trigger the WARN_ON(), and the early return leaves the unsupported AV1 job scheduled on VCN1.

If this is true then without this patch on SR-IOV, calling drm_sched_entity_modify_sched() with an un-initialized scheduler leaves entity->rq as NULL which surely explodes badly. At least I cannot see that it is gracefully handled anywhere.

In any case perhaps we should add this to vcn 3 and 4:

drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +5 -0
index 2f7d5d562cb4..adcb94041274 100644
@@ -1895,6 +1895,7 @@ static const struct amdgpu_ring_funcs vcn_v3_0_dec_sw_ring_vm_funcs = {
 static int vcn_v3_0_limit_sched(struct amdgpu_cs_parser *p,
                                struct amdgpu_job *job)
 {
+       struct amdgpu_ring *ring = amdgpu_job_ring(job);
        struct drm_gpu_scheduler **scheds;
        struct dma_fence *fence;
 
@@ -1902,6 +1903,10 @@ static int vcn_v3_0_limit_sched(struct amdgpu_cs_parser *p,
        if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
                return -EINVAL;
 
+       /* If VCN0 is disabled we also cannot support AV1. */
+       if (ring->no_scheduler)
+               return -EINVAL;
+
        /* wait for all jobs to finish before switching to instance 0 */
        fence = amdgpu_ctx_get_fence(p->ctx, job->base.entity, ~0ull);
        if (fence) {

This way submission fails explicitly and -EINVAL trickles out all the way to cs ioctl. Thoughts?

Regards,

Tvrtko

#4 lore raw

Re: [PATCH 1/4] drm/sched: Disallow initializing entities with un-initialized schedulers

In reply to Tvrtko Ursulin
Quoted 11 lines · Tvrtko Ursulin

Simplify the code and theoretical runtime combinations by disallowing initializing entities with un-initialized schedulers.

Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
I do not *think* any of the drivers relies on this inverted initialization order but it needs more eyes for sure.

I increasingly think that the way to go forward is to demand from API users that they use the API correctly and appreciate the distinct lifetimes and especially initialization and teardown orders.

Sure the API can warn when it's misused, but we have certainly not put enough emphasis on really motivating people to set up things right. Most APIs I see in the kernel do not warn in foo_init() whether foo_init() had been called already.

What we actually need is more documentation

P.

#5 lore raw

Re: [PATCH 1/4] drm/sched: Disallow initializing entities with un-initialized schedulers

Quoted 14 lines · Philipp Stanner
Quoted 11 lines · Tvrtko Ursulin

Simplify the code and theoretical runtime combinations by disallowing initializing entities with un-initialized schedulers.

Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>

---
I do not *think* any of the drivers relies on this inverted initialization order but it needs more eyes for sure.

Quoted 3 lines

I increasingly think that the way to go forward is to demand from API users that they use the API correctly and appreciate the distinct lifetimes and especially initialization and teardown orders.

Enforcing initialisation order is what the patch is doing.

Quoted 4 lines

Sure the API can warn when it's misused, but we have certainly not put enough emphasis on really motivating people to set up things right. Most APIs I see in the kernel do not warn in foo_init() whether foo_init() had been called already.

Exactly, so making entity initialisation fail when the provided list of schedulers contains uninitialised elements is this strong motivation you are looking for. New callers simply cannot pass driver bring-up with the wrong init order.

Quoted 1 line

What we actually need is more documentation

As opposed to adding restrictions where we can, or in tandem to adding restrictions?

Tvrtko

#6 lore raw

PATCH 2/4 drm/sched: Ignore stopped entity attempt to change its run-queue

In reply to Tvrtko Ursulin

Jobs submitted to a stopped entity do not go anywhere due how they are handled in drm_sched_rq_add_entity() so there is no point to allow them to change their run-queue. We can simply bail out early which makes entity->stopped a clearer boundary than it currently is.

This is completely safe because it is always guaranteed entity->rq is valid if drm_sched_entity_init() succeeded.

Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 drivers/gpu/drm/scheduler/sched_entity.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +8 -4
index 2844049d4c66..a18f637f9e99 100644
@@ -594,13 +594,16 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 		return;
 
 	spin_lock(&entity->lock);
+
+	/* Cannot submit to stopped entity anyway so bail out early. */
+	if (entity->stopped)
+		goto out;
+
 	fence = entity->last_scheduled;
 
 	/* stay on the same engine if the previous job hasn't finished */
-	if (fence && !dma_fence_is_signaled(fence)) {
-		spin_unlock(&entity->lock);
-		return;
-	}
+	if (fence && !dma_fence_is_signaled(fence))
+		goto out;
 
 	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
 	rq = sched ? sched->sched_rq[entity->rq_priority] : NULL;
@@ -612,6 +615,7 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 	if (entity->num_sched_list == 1)
 		entity->sched_list = NULL;
 
+out:
 	spin_unlock(&entity->lock);
 }
 
Signature · 1 line
2.56.0
#7 lore raw

Re: [PATCH 2/4] drm/sched: Ignore stopped entity attempt to change its run-queue

Quoted 3 lines · Tvrtko Ursulin

Jobs submitted to a stopped entity do not go anywhere due how they are handled in drm_sched_rq_add_entity() so there is no point to allow them to change their run-queue.

Who stops an entity?
Who submits to a stopped entity?
Isn't entity_select_rq only called for drm_sched_job_arm()? Who's arming jobs for an entity that he has stopped himself?

P.

#8 lore raw

Re: [PATCH 2/4] drm/sched: Ignore stopped entity attempt to change its run-queue

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

Jobs submitted to a stopped entity do not go anywhere due how they are handled in drm_sched_rq_add_entity() so there is no point to allow them to change their run-queue.

Who stops an entity?
Who submits to a stopped entity?
Isn't entity_select_rq only called for drm_sched_job_arm()? Who's arming jobs for an entity that he has stopped himself?

I extracted this change as separate patch on your request during a conversation back in June.

But it is not really required, even less so since I abandoned the removal of entity_idle. The only way race could happen if it drivers did crazy things.

I can drop it no problem.

Tvrtko

#9 lore raw

PATCH 3/4 drm/sched: Remove the single scheduler special case

In reply to Tvrtko Ursulin

Long time ago in commit
b3ac17667f11 ("drm/scheduler: rework entity creation")
a change was made which prevented priority changes for entities with only one assigned scheduler.

That commit reduced drm_sched_entity_set_priority() to simply update the entities priority, but the run queue selection logic in drm_sched_entity_select_rq() was never able to actually change the originally assigned run queue.

In practice that only affected amdgpu, being the only driver which can do dynamic priority changes, and it appears it was attempted to rectify this in commit
2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override") .

A few unresolved problems however remained:

The above only fixed drm_sched_entity_set_priority()
if drm_sched_entity_modify_sched() was called first which was not documented anywhere.

Secondly, that only worked if drm_sched_entity_modify_sched() is actually called, which in amdgpu's case is true only for gfx and compute. Priority changes for other engines, which have only one scheduler assigned such as jpeg and video decode still did not work.

(Note that this was also noticed in 981b04d96856 ("drm/sched: improve docs around drm_sched_entity").)

Completely different set of non-obvious confusion was that, whereas drm_sched_entity_init() was not keeping the passed in list of schedulers (courtesy of commit
8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list") ), drm_sched_entity_modify_sched() was disagreeing with that and would simply assign the single item list. That incosistency appears to have been semi-silently fixed in commit
ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3") .

What was also not documented is why it was important to not keep the list of schedulers when there is only one scheduler given. This was in fact due many of the callers passing in a stack container for this case. For the multiple schedulers on the other hand amdgpu and rocket are the only such callers and there the container is not on the stack but correctly follows the lifetime requirement. Keeping a stack backed list in the entity would obviously be undefined behaviour if the list was kept.

Having said that, amdgpu however did only stop passing in stack backed container for the more than one scheduler case in commit 977f7e1068be ("drm/amdgpu: allocate entities on demand")
. Until then dereferencing freed stack from drm_sched_entity_select_rq() was a possibility.

In order to untangle all this lets make this area behave consistently and store the single scheduler case directly in the entity. We can do that easily by creating an union between the current pointer to a list of schedulers, and a new pointer to the single scheduler.

This allows removal of the special cases which artificially cleared entity->sched_list for a single scheduler case and fixes the amdgpu dynamic priority changes for the single IP instances.

Fixes b3ac17667f11 ("drm/scheduler: rework entity creation")

References: 8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list")
References: 977f7e1068be ("drm/amdgpu: allocate entities on demand")
References: 2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override")
References: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
References: 981b04d96856 ("drm/sched: improve docs around drm_sched_entity")
References: 77a6809f1dc3 ("drm/sched: Remove FIFO and RR and simplify to a single run queue")
Cc: Christian König <[email protected]>
Cc: Alex Deucher <[email protected]>
Cc: Luben Tuikov <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: Philipp Stanner <[email protected]>
Cc: Cc: Tomeu Vizoso <[email protected]>
Cc: Sidong Yang <[email protected]>
Cc: [email protected]

---
 drivers/gpu/drm/scheduler/sched_entity.c | 30 ++++++++++-------
 include/drm/gpu_scheduler.h              | 42 ++++++++++++++----------
 2 files changed, 44 insertions(+), 28 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +19 -11
index a18f637f9e99..6ff455373067 100644
@@ -102,7 +102,9 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
  * @guilty: atomic_t set to 1 when a job on this queue
  *          is found to be guilty causing a timeout
  *
- * Note that the &sched_list must have at least one element to schedule the entity.
+ * Note that the &sched_list must have exactly one element to schedule the
+ * entity. Callers which pass more than one scheduler on the @sched_list must
+ * ensure the list remains valid for the lifetime of the entity.
  *
  * For changing @priority later on at runtime see
  * drm_sched_entity_set_priority(). For changing the set of schedulers
@@ -139,7 +141,10 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	entity->guilty = guilty;
 	entity->last_user = current->group_leader;
 	entity->num_sched_list = num_sched_list;
-	entity->sched_list = num_sched_list > 1 ? sched_list : NULL;
+	if (num_sched_list == 1)
+		entity->sched = sched_list[0];
+	else
+		entity->sched_list = sched_list;
 	RB_CLEAR_NODE(&entity->rb_tree_node);
 
 	if (priority >= sched_list[0]->num_user_rqs) {
@@ -183,6 +188,9 @@ EXPORT_SYMBOL(drm_sched_entity_init);
  *		 existing entity->sched_list
  * @num_sched_list: number of drm sched in sched_list
  *
+ * Callers which pass more than one scheduler on the @sched_list must
+ * ensure the list remains valid for the lifetime of the entity.
+ *
  * Note that this must be called under the same common lock for @entity as
  * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to
  * guarantee through some other means that this is never called while new jobs
@@ -203,7 +211,10 @@ void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
 	}
 
 	spin_lock(&entity->lock);
-	entity->sched_list = sched_list;
+	if (num_sched_list == 1)
+		entity->sched = sched_list[0];
+	else
+		entity->sched_list = sched_list;
 	entity->num_sched_list = num_sched_list;
 	spin_unlock(&entity->lock);
 }
@@ -585,10 +596,6 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 	struct drm_gpu_scheduler *sched;
 	struct drm_sched_rq *rq;
 
-	/* single possible engine and already selected */
-	if (!entity->sched_list)
-		return;
-
 	/* queue non-empty, stay on the same engine */
 	if (spsc_queue_count(&entity->job_queue))
 		return;
@@ -605,16 +612,17 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 	if (fence && !dma_fence_is_signaled(fence))
 		goto out;
 
-	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
+	if (entity->num_sched_list == 1)
+		sched = entity->sched;
+	else
+		sched = drm_sched_pick_best(entity->sched_list,
+					    entity->num_sched_list);
 	rq = sched ? sched->sched_rq[entity->rq_priority] : NULL;
 	if (rq != entity->rq) {
 		drm_sched_rq_remove_entity(entity->rq, entity);
 		entity->rq = rq;
 	}
 
-	if (entity->num_sched_list == 1)
-		entity->sched_list = NULL;
-
 out:
 	spin_unlock(&entity->lock);
 }
include/drm/gpu_scheduler.h +25 -17

48 lines not on this page · raw message

Signature · 1 line
2.56.0
#10 lore raw

Re: [PATCH 3/4] drm/sched: Remove the single scheduler special case

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] drm/sched: contradictory sched_list kernel-doc in drm_sched_entity_init() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] drm/sched: contradictory sched_list kernel-doc in drm_sched_entity_init()
--

commit 51236e2b589e5bb2f95788276a6d8bea43b26e11
Author: Tvrtko Ursulin <[email protected]>

drm/sched: Remove the single scheduler special case

This commit stores a single scheduler pointer directly in a union with sched_list in struct drm_sched_entity so that entities with a single scheduler retain their scheduler reference for runtime priority changes.

drivers/gpu/drm/scheduler/sched_entity.c ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job) · line 107

Quoted 12 lines
drivers/gpu/drm/scheduler/sched_entity.c +3 -1
index a18f637f9e998..6ff455373067f 100644
@@ -102,7 +102,9 @@ ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
  * @guilty: atomic_t set to 1 when a job on this queue
  *          is found to be guilty causing a timeout
  *
- * Note that the &sched_list must have at least one element to schedule the entity.
+ * Note that the &sched_list must have exactly one element to schedule the
+ * entity. Callers which pass more than one scheduler on the @sched_list must
+ * ensure the list remains valid for the lifetime of the entity.

[Severity: Low]
Should this still say "at least one element" rather than "exactly one element"? drm_sched_entity_init() accepts one or more schedulers in sched_list, as described in the following sentence.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#11 lore raw

Re: [PATCH 3/4] drm/sched: Remove the single scheduler special case

In reply to Tvrtko Ursulin

Why do you call "single scheduler" a special case? That seems to be quite a common case.

Quoted 11 lines · Tvrtko Ursulin

In order to untangle all this lets make this area behave consistently and store the single scheduler case directly in the entity. We can do that easily by creating an union between the current pointer to a list of schedulers, and a new pointer to the single scheduler.

This allows removal of the special cases which artificially cleared entity->sched_list for a single scheduler case and fixes the amdgpu dynamic priority changes for the single IP instances.

Fixes b3ac17667f11 ("drm/scheduler: rework entity creation")

Can you describe what the actual bug is?

Quoted 6 lines

References: 8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list")
References: 977f7e1068be ("drm/amdgpu: allocate entities on demand")
References: 2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override")
References: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
References: 981b04d96856 ("drm/sched: improve docs around drm_sched_entity")
References: 77a6809f1dc3 ("drm/sched: Remove FIFO and RR and simplify to a single run queue")

This commit has been reverted.

Recently, I often end up a confused by your series'. Is this here another RFC?

Is it a preparational patch for your CFS work?

Quoted 5 lines

  *
- * Note that the &sched_list must have at least one element to schedule the entity.
+ * Note that the &sched_list must have exactly one element to schedule the
+ * entity. Callers which pass more than one scheduler on the @sched_list must
+ * ensure the list remains valid for the lifetime of the entity.

What does that even mean, "valid"? Like, not deallocated?

How does drm_sched_entity_modify_sched() play into that?

drivers/gpu/drm/scheduler/sched_entity.c void drm_sched_entity_select_rq(struct drm_sched_entity *entity) · line 619

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_entity.c +5 -1
-	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
+	if (entity->num_sched_list == 1)
+		sched = entity->sched;
+	else
+		sched = drm_sched_pick_best(entity->sched_list,
+					    entity->num_sched_list);

So what you want to achieve is that sched_list can never be NULL? How's that a bug?

Quoted 16 lines
include/drm/gpu_scheduler.h +15 -1
-	struct drm_gpu_scheduler        **sched_list;
+	union {
+		/**
+		 * @sched_list:
+		 *
+		 * A list of schedulers (struct drm_gpu_scheduler).  Jobs from
+		 * this entity can be scheduled on any scheduler on this list.
+		 *
+		 * This can be modified by calling
+		 * drm_sched_entity_modify_sched(). Locking is entirely up to
+		 * the driver, see the above function for more details.
+		 */
+		struct drm_gpu_scheduler        **sched_list;
+
+		/**
+		 * @sched:

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 148

Quoted 10 lines
include/drm/gpu_scheduler.h +10 -0
+		 *
+		 * A single struct drm_gpu_scheduler jobs from this entity will
+		 * be scheduled on.
+		 *
+		 * This can be modified by calling
+		 * drm_sched_entity_modify_sched(). Locking is entirely up to
+		 * the driver, see the above function for more details.
+		 */
+		struct drm_gpu_scheduler        *sched;
+	};

So you now would have some mode where there is sometimes a list and sometimes a pointer, and what it is can change dynamically through drm_sched_entity_modify_sched()??

How would that untangle anything?

P.

#12 lore raw

Re: [PATCH 3/4] drm/sched: Remove the single scheduler special case

Quoted 2 lines · Philipp Stanner

Why do you call "single scheduler" a special case? That seems to be quite a common case.

Special case a) in the code:

drm_sched_entity_init
{
...

	entity->sched_list = num_sched_list > 1 ? sched_list : NULL;

drm_sched_entity_select_rq
{
...

	if (entity->num_sched_list == 1)
		entity->sched_list = NULL;

And b) in the API contract, where when:

1)
drm_sched_entity_init num_sched_list == 1

Caller can pass in a stack sched_list container (as many if not all current callers do) ie. does not have to keep it valid for the lifetime of the entity;

and 2)
drm_sched_entity_init num_sched_list > 1

Caller MUST ensure the sched_list containers remains valid for the lifetime of the entity.

Quoted 6 lines · Tvrtko Ursulin
Quoted 5 lines

In order to untangle all this lets make this area behave consistently and store the single scheduler case directly in the entity. We can do that easily by creating an union between the current pointer to a list of schedulers, and a new pointer to the single scheduler.

Quoted 8 lines
Quoted 6 lines

This allows removal of the special cases which artificially cleared entity->sched_list for a single scheduler case and fixes the amdgpu dynamic priority changes for the single IP instances.

Fixes b3ac17667f11 ("drm/scheduler: rework entity creation")

Can you describe what the actual bug is?

It's explained in the commit message, but in short:

1)
drm_sched_entity_init calling convention (sched_list ownership rules) is dangerous and undocumented.

2)
drm_sched_entity_set_priority is broken when num_sched_list == 1 due above listed special casing.

Furthermore, the special casing, from the analysis presented in the commit message, appears to be a kludge to work around the UAF due unclear sched_list ownership/lifetime rules.

Quoted 8 lines
Quoted 6 lines

References: 8c23056bdc7a ("drm/scheduler: do not keep a copy of sched list")
References: 977f7e1068be ("drm/amdgpu: allocate entities on demand")
References: 2316a86bde49 ("drm/amdgpu: change hw sched list on ctx priority override")
References: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
References: 981b04d96856 ("drm/sched: improve docs around drm_sched_entity")
References: 77a6809f1dc3 ("drm/sched: Remove FIFO and RR and simplify to a single run queue")

This commit has been reverted.

It is stil a valid reference since with that commit the broken drm_sched_entity_set_priority() was fixed in a different way.

Quoted 2 lines

Recently, I often end up a confused by your series'. Is this here another RFC?

No, it's a fix.

I appreciate one needs to devote sufficient time to stay on top of them. Recently you kindly reviewed a series where all patches were about drm_sched_init. This series is all about drm_sched_entity_init.

Quoted 1 line

Is it a preparational patch for your CFS work?

Nope, it is a fix and clarification for drm_sched_entity_init().

Quoted 7 lines
Quoted 5 lines

  *
- * Note that the &sched_list must have at least one element to schedule the entity.
+ * Note that the &sched_list must have exactly one element to schedule the
+ * entity. Callers which pass more than one scheduler on the @sched_list must
+ * ensure the list remains valid for the lifetime of the entity.

What does that even mean, "valid"? Like, not deallocated?

Yes.

Quoted 1 line

How does drm_sched_entity_modify_sched() play into that?

Same issue as drm_sched_entity_init because first job arm will clear the sched_list when num_sched_list == 1, which means run-queue cannot be change ever after.

Quoted 9 lines

drivers/gpu/drm/scheduler/sched_entity.c void drm_sched_entity_select_rq(struct drm_sched_entity *entity) · line 619

Quoted 6 lines
drivers/gpu/drm/scheduler/sched_entity.c +5 -1
-	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
+	if (entity->num_sched_list == 1)
+		sched = entity->sched;
+	else
+		sched = drm_sched_pick_best(entity->sched_list,
+					    entity->num_sched_list);

So what you want to achieve is that sched_list can never be NULL? How's that a bug?

Null sched_list prevent run-queue changes with FIFO and RR.

Quoted 31 lines
Quoted 16 lines
include/drm/gpu_scheduler.h +15 -1
-	struct drm_gpu_scheduler        **sched_list;
+	union {
+		/**
+		 * @sched_list:
+		 *
+		 * A list of schedulers (struct drm_gpu_scheduler).  Jobs from
+		 * this entity can be scheduled on any scheduler on this list.
+		 *
+		 * This can be modified by calling
+		 * drm_sched_entity_modify_sched(). Locking is entirely up to
+		 * the driver, see the above function for more details.
+		 */
+		struct drm_gpu_scheduler        **sched_list;
+
+		/**
+		 * @sched:

include/drm/gpu_scheduler.h struct drm_sched_entity { · line 148

Quoted 10 lines
include/drm/gpu_scheduler.h +10 -0
+		 *
+		 * A single struct drm_gpu_scheduler jobs from this entity will
+		 * be scheduled on.
+		 *
+		 * This can be modified by calling
+		 * drm_sched_entity_modify_sched(). Locking is entirely up to
+		 * the driver, see the above function for more details.
+		 */
+		struct drm_gpu_scheduler        *sched;
+	};

So you now would have some mode where there is sometimes a list and sometimes a pointer, and what it is can change dynamically through drm_sched_entity_modify_sched()??

Quoted 1 line

How would that untangle anything?

I hope it is clearer after this reply? Commit message is complicated for a reason - because the history of the num_sched_list == 1 special case is equally complicated and confused.

Tvrtko

#13 lore raw

PATCH 4/4 drm/sched: Address the fence_seq fixme

In reply to Tvrtko Ursulin

Address a FIXME against struct drm_sched_entity->fence_seq which documented this field does not need to be atomic_t.

Indeed now that most of the flow is guarded with entity->lock we can pull out the lock a step up, to drm_sched_job_arm(), which also simplifies drm_sched_entity_select_rq() and convert the field to a standard unsigned long.

Cc Christian König <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Matthew Brost <[email protected]>
Cc Philipp Stanner <[email protected]>
---
 drivers/gpu/drm/scheduler/sched_entity.c | 12 ++++--------
 drivers/gpu/drm/scheduler/sched_fence.c  |  6 ++++--
 drivers/gpu/drm/scheduler/sched_main.c   |  7 ++++++-
 include/drm/gpu_scheduler.h              |  7 ++-----
 4 files changed, 16 insertions(+), 16 deletions(-)
drivers/gpu/drm/scheduler/sched_entity.c +4 -8
index 6ff455373067..af6cb79c8a53 100644
@@ -174,7 +174,6 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
 	spin_lock_init(&entity->lock);
 	spsc_queue_init(&entity->job_queue);
 
-	atomic_set(&entity->fence_seq, 0);
 	entity->fence_context = dma_fence_context_alloc(2);
 
 	return 0;
@@ -596,21 +595,21 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 	struct drm_gpu_scheduler *sched;
 	struct drm_sched_rq *rq;
 
+	lockdep_assert_held(&entity->lock);
+
 	/* queue non-empty, stay on the same engine */
 	if (spsc_queue_count(&entity->job_queue))
 		return;
 
-	spin_lock(&entity->lock);
-
 	/* Cannot submit to stopped entity anyway so bail out early. */
 	if (entity->stopped)
-		goto out;
+		return;
 
 	fence = entity->last_scheduled;
 
 	/* stay on the same engine if the previous job hasn't finished */
 	if (fence && !dma_fence_is_signaled(fence))
-		goto out;
+		return;
 
 	if (entity->num_sched_list == 1)
 		sched = entity->sched;
@@ -622,9 +621,6 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 		drm_sched_rq_remove_entity(entity->rq, entity);
 		entity->rq = rq;
 	}
-
-out:
-	spin_unlock(&entity->lock);
 }
 
 /**
drivers/gpu/drm/scheduler/sched_fence.c +4 -2
index b7a4cb921234..b4c336ff0179 100644
@@ -224,10 +224,12 @@ struct drm_sched_fence *drm_sched_fence_alloc(void *owner,
 void drm_sched_fence_init(struct drm_sched_fence *fence,
 			  struct drm_sched_entity *entity)
 {
-	unsigned seq;
+	unsigned long seq;
+
+	lockdep_assert_held(&entity->lock);
 
 	fence->sched = entity->rq->sched;
-	seq = atomic_inc_return(&entity->fence_seq);
+	seq = ++entity->fence_seq;
 	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
 		       &fence->lock, entity->fence_context, seq);
 	dma_fence_init(&fence->finished, &drm_sched_fence_ops_finished,
drivers/gpu/drm/scheduler/sched_main.c +6 -1
index d0d34c523615..164a48637d56 100644
@@ -646,6 +646,9 @@ void drm_sched_job_arm(struct drm_sched_job *job)
 	struct drm_sched_entity *entity = job->entity;
 
 	BUG_ON(!entity);
+
+	spin_lock(&entity->lock);
+
 	drm_sched_entity_select_rq(entity);
 	sched = entity->rq->sched;
 
@@ -653,7 +656,9 @@ void drm_sched_job_arm(struct drm_sched_job *job)
 	job->s_priority = entity->priority;
 	job->entity_stats = drm_sched_entity_stats_get(entity->stats);
 
-	drm_sched_fence_init(job->s_fence, job->entity);
+	drm_sched_fence_init(job->s_fence, entity);
+
+	spin_unlock(&entity->lock);
 }
 EXPORT_SYMBOL(drm_sched_job_arm);
 
include/drm/gpu_scheduler.h +2 -5
index 2069b3584591..81604b082259 100644
@@ -101,7 +101,7 @@ struct drm_sched_entity {
 	 *
 	 * Lock protecting the run-queue (@rq) to which this entity belongs,
 	 * @priority, @last_scheduled and the list of schedulers (@sched_list,
-	 * @num_sched_list), @rr_ts and @stopped.
+	 * @num_sched_list), @rr_ts, @stopped and @fence_seq.
 	 */
 	spinlock_t			lock;
 
@@ -184,11 +184,8 @@ struct drm_sched_entity {
 	 *
 	 * A linearly increasing seqno incremented with each new
 	 * &drm_sched_fence which is part of the entity.
-	 *
-	 * FIXME: Callers of drm_sched_job_arm() need to ensure correct locking,
-	 * this doesn't need to be atomic.
 	 */
-	atomic_t			fence_seq;
+	unsigned long			fence_seq;
 
 	/**
 	 * @fence_context:
Signature · 1 line
2.56.0