public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] drm/sched: Fix race in drm_sched_entity_select_rq()
@ 2025-10-22  6:34 Philipp Stanner
  2025-10-22  7:16 ` Tvrtko Ursulin
  0 siblings, 1 reply; 3+ messages in thread
From: Philipp Stanner @ 2025-10-22  6:34 UTC (permalink / raw)
  To: Matthew Brost, Danilo Krummrich, Philipp Stanner,
	Christian König, David Airlie, Simona Vetter, Tvrtko Ursulin
  Cc: dri-devel, linux-kernel, linux-media, stable

In a past bug fix it was forgotten that entity access must be protected
by the entity lock. That's a data race and potentially UB.

Move the spin_unlock() to the appropriate position.

Cc: stable@vger.kernel.org # v5.13+
Fixes: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
Signed-off-by: Philipp Stanner <phasta@kernel.org>
---
 drivers/gpu/drm/scheduler/sched_entity.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
index 5a4697f636f2..aa222166de58 100644
--- a/drivers/gpu/drm/scheduler/sched_entity.c
+++ b/drivers/gpu/drm/scheduler/sched_entity.c
@@ -552,10 +552,11 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
 		drm_sched_rq_remove_entity(entity->rq, entity);
 		entity->rq = rq;
 	}
-	spin_unlock(&entity->lock);
 
 	if (entity->num_sched_list == 1)
 		entity->sched_list = NULL;
+
+	spin_unlock(&entity->lock);
 }
 
 /**
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/sched: Fix race in drm_sched_entity_select_rq()
  2025-10-22  6:34 [PATCH] drm/sched: Fix race in drm_sched_entity_select_rq() Philipp Stanner
@ 2025-10-22  7:16 ` Tvrtko Ursulin
  2025-10-27 12:50   ` Philipp Stanner
  0 siblings, 1 reply; 3+ messages in thread
From: Tvrtko Ursulin @ 2025-10-22  7:16 UTC (permalink / raw)
  To: Philipp Stanner, Matthew Brost, Danilo Krummrich,
	Christian König, David Airlie, Simona Vetter
  Cc: dri-devel, linux-kernel, linux-media, stable


On 22/10/2025 07:34, Philipp Stanner wrote:
> In a past bug fix it was forgotten that entity access must be protected
> by the entity lock. That's a data race and potentially UB.
> 
> Move the spin_unlock() to the appropriate position.
> 
> Cc: stable@vger.kernel.org # v5.13+
> Fixes: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
> Signed-off-by: Philipp Stanner <phasta@kernel.org>
> ---
>   drivers/gpu/drm/scheduler/sched_entity.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
> index 5a4697f636f2..aa222166de58 100644
> --- a/drivers/gpu/drm/scheduler/sched_entity.c
> +++ b/drivers/gpu/drm/scheduler/sched_entity.c
> @@ -552,10 +552,11 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
>   		drm_sched_rq_remove_entity(entity->rq, entity);
>   		entity->rq = rq;
>   	}
> -	spin_unlock(&entity->lock);
>   
>   	if (entity->num_sched_list == 1)
>   		entity->sched_list = NULL;
> +
> +	spin_unlock(&entity->lock);
>   }
>   
>   /**

Agreed, unlock at the end is correct.

Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>

Luckily only amdgpu calls drm_sched_entity_modify_sched(), and I am not 
sure if with hypothetical multi-threaded submit it could be possible to 
hit it or not.

One thread would have to clear the sched_list, while another would have 
to have passed the !sched_list at the beginning of the function, and the 
job_queue count and last_scheduled completed guards. Which would mean 
job has to get completed between the unlock and clearing the pointer.

Also, take a look at this when you can please:

https://lore.kernel.org/dri-devel/20240719094730.55301-1-tursulin@igalia.com/

This would also have fixed this issue by removing clearing of 
entity->sched_list. Latter being just a hack to work around the issue of 
confused container ownership rules.

Regards,

Tvrtko


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] drm/sched: Fix race in drm_sched_entity_select_rq()
  2025-10-22  7:16 ` Tvrtko Ursulin
@ 2025-10-27 12:50   ` Philipp Stanner
  0 siblings, 0 replies; 3+ messages in thread
From: Philipp Stanner @ 2025-10-27 12:50 UTC (permalink / raw)
  To: Tvrtko Ursulin, Philipp Stanner, Matthew Brost, Danilo Krummrich,
	Christian König, David Airlie, Simona Vetter
  Cc: dri-devel, linux-kernel, linux-media, stable

On Wed, 2025-10-22 at 08:16 +0100, Tvrtko Ursulin wrote:
> 
> On 22/10/2025 07:34, Philipp Stanner wrote:
> > In a past bug fix it was forgotten that entity access must be protected
> > by the entity lock. That's a data race and potentially UB.
> > 
> > Move the spin_unlock() to the appropriate position.
> > 
> > Cc: stable@vger.kernel.org # v5.13+
> > Fixes: ac4eb83ab255 ("drm/sched: select new rq even if there is only one v3")
> > Signed-off-by: Philipp Stanner <phasta@kernel.org>
> > ---
> >   drivers/gpu/drm/scheduler/sched_entity.c | 3 ++-
> >   1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
> > index 5a4697f636f2..aa222166de58 100644
> > --- a/drivers/gpu/drm/scheduler/sched_entity.c
> > +++ b/drivers/gpu/drm/scheduler/sched_entity.c
> > @@ -552,10 +552,11 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
> >   		drm_sched_rq_remove_entity(entity->rq, entity);
> >   		entity->rq = rq;
> >   	}
> > -	spin_unlock(&entity->lock);
> >   
> >   	if (entity->num_sched_list == 1)
> >   		entity->sched_list = NULL;
> > +
> > +	spin_unlock(&entity->lock);
> >   }
> >   
> >   /**
> 
> Agreed, unlock at the end is correct.
> 
> Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>

Applied to drm-misc-fixes.

Thx
P.


> 
> Luckily only amdgpu calls drm_sched_entity_modify_sched(), and I am not 
> sure if with hypothetical multi-threaded submit it could be possible to 
> hit it or not.
> 
> One thread would have to clear the sched_list, while another would have 
> to have passed the !sched_list at the beginning of the function, and the 
> job_queue count and last_scheduled completed guards. Which would mean
> job has to get completed between the unlock and clearing the pointer.
> 
> Also, take a look at this when you can please:
> 
> https://lore.kernel.org/dri-devel/20240719094730.55301-1-tursulin@igalia.com/
> 
> This would also have fixed this issue by removing clearing of 
> entity->sched_list. Latter being just a hack to work around the issue of 
> confused container ownership rules.
> 
> Regards,
> 
> Tvrtko
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-10-27 12:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-22  6:34 [PATCH] drm/sched: Fix race in drm_sched_entity_select_rq() Philipp Stanner
2025-10-22  7:16 ` Tvrtko Ursulin
2025-10-27 12:50   ` Philipp Stanner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox