From: "Christian König" <ckoenig.leichtzumerken@gmail.com>
To: Philipp Stanner <phasta@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 3/3] drm/sched: Protect entity->last_scheduled with spinlock
Date: Wed, 16 Sep 2026 14:33:05 +0200 [thread overview]
Message-ID: <14ad8ea7-c65c-48b0-94c5-bc881d7aaddd@gmail.com> (raw)
In-Reply-To: <20260910075942.2000338-5-phasta@kernel.org>
On 9/10/26 09:59, Philipp Stanner wrote:
> The entity->last_scheduled field has always been set and read with
> special RCU functions in addition to memory barriers.
>
> This was added in
>
> commit 70102d77ff22 ("drm/scheduler: add drm_sched_entity_error and use rcu for last_scheduled")
>
> however, no proper justification for that mechanism was provided. There
> seems to be no obvious reason, since the entity lock is available and
> taken at all places that evaluate the last_scheduled field. The only
> exception is drm_sched_entity_error(), which is not performance critical
> in any way.
>
> Improve robustness, readability and maintainability by replacing RCU and
> barriers with the lock.
>
> Signed-off-by: Philipp Stanner <phasta@kernel.org>
Acked-by: Christian König <christian.koenig@amd.com>
> ---
> drivers/gpu/drm/scheduler/sched_entity.c | 50 ++++++++++--------------
> include/drm/gpu_scheduler.h | 10 ++---
> 2 files changed, 24 insertions(+), 36 deletions(-)
>
> diff --git a/drivers/gpu/drm/scheduler/sched_entity.c b/drivers/gpu/drm/scheduler/sched_entity.c
> index e168f445f2ab..fa4a9388e5c9 100644
> --- a/drivers/gpu/drm/scheduler/sched_entity.c
> +++ b/drivers/gpu/drm/scheduler/sched_entity.c
> @@ -136,7 +136,6 @@ int drm_sched_entity_init(struct drm_sched_entity *entity,
> DRM_SCHED_PRIORITY_KERNEL : priority;
> entity->num_sched_list = num_sched_list;
> entity->sched_list = num_sched_list > 1 ? sched_list : NULL;
> - RCU_INIT_POINTER(entity->last_scheduled, NULL);
> RB_CLEAR_NODE(&entity->rb_tree_node);
>
> if (!sched_list[0]->sched_rq) {
> @@ -233,10 +232,10 @@ int drm_sched_entity_error(struct drm_sched_entity *entity)
> struct dma_fence *fence;
> int r;
>
> - rcu_read_lock();
> - fence = rcu_dereference(entity->last_scheduled);
> + spin_lock(&entity->lock);
> + fence = entity->last_scheduled;
> r = fence ? fence->error : 0;
> - rcu_read_unlock();
> + spin_unlock(&entity->lock);
>
> return r;
> }
> @@ -319,9 +318,10 @@ void drm_sched_entity_kill(struct drm_sched_entity *entity)
> /* Make sure this entity is not used by the scheduler at the moment */
> wait_for_completion(&entity->entity_idle);
>
> - /* The entity is guaranteed to not be used by the scheduler */
> - prev = rcu_dereference_check(entity->last_scheduled, true);
> + spin_lock(&entity->lock);
> + prev = entity->last_scheduled;
> dma_fence_get(prev);
> + spin_unlock(&entity->lock);
> while ((job = drm_sched_entity_queue_pop(entity))) {
> struct drm_sched_fence *s_fence = job->s_fence;
>
> @@ -413,8 +413,7 @@ void drm_sched_entity_fini(struct drm_sched_entity *entity)
> entity->dependency = NULL;
> }
>
> - dma_fence_put(rcu_dereference_check(entity->last_scheduled, true));
> - RCU_INIT_POINTER(entity->last_scheduled, NULL);
> + dma_fence_put(entity->last_scheduled);
> drm_sched_entity_stats_put(entity->stats);
> }
> EXPORT_SYMBOL(drm_sched_entity_fini);
> @@ -536,6 +535,10 @@ drm_sched_job_dependency(struct drm_sched_job *job,
>
> struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
> {
> + /* Helper to avoid dropping the reference while the entity lock is held,
> + * just to have some more robustness.
> + */
> + struct dma_fence *prev_last_scheduled;
> struct drm_sched_job *sched_job;
>
> sched_job = drm_sched_entity_queue_peek(entity);
> @@ -552,22 +555,15 @@ struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
> if (entity->guilty && atomic_read(entity->guilty))
> dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);
>
> - dma_fence_put(rcu_dereference_check(entity->last_scheduled, true));
> - rcu_assign_pointer(entity->last_scheduled,
> - dma_fence_get(&sched_job->s_fence->finished));
> -
> - /*
> - * If the queue is empty we allow drm_sched_entity_select_rq() to
> - * locklessly access ->last_scheduled. This only works if we set the
> - * pointer before we dequeue and if we a write barrier here.
> - */
> - smp_wmb();
> -
> spin_lock(&entity->lock);
> + prev_last_scheduled = entity->last_scheduled;
> + entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished);
> spsc_queue_pop(&entity->job_queue);
> drm_sched_rq_pop_entity(entity);
> spin_unlock(&entity->lock);
>
> + dma_fence_put(prev_last_scheduled);
> +
> /* Jobs and entities might have different lifecycles. Since we're
> * removing the job from the entities queue, set the jobs entity pointer
> * to NULL to prevent any future access of the entity through this job.
> @@ -591,21 +587,15 @@ void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
> if (spsc_queue_count(&entity->job_queue))
> return;
>
> - /*
> - * Only when the queue is empty are we guaranteed that
> - * drm_sched_run_job_work() cannot change entity->last_scheduled. To
> - * enforce ordering we need a read barrier here. See
> - * drm_sched_entity_pop_job() for the other side.
> - */
> - smp_rmb();
> -
> - fence = rcu_dereference_check(entity->last_scheduled, true);
> + spin_lock(&entity->lock);
> + fence = entity->last_scheduled;
>
> /* stay on the same engine if the previous job hasn't finished */
> - if (fence && !dma_fence_is_signaled(fence))
> + if (fence && !dma_fence_is_signaled(fence)) {
> + spin_unlock(&entity->lock);
> return;
> + }
>
> - spin_lock(&entity->lock);
> 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) {
> diff --git a/include/drm/gpu_scheduler.h b/include/drm/gpu_scheduler.h
> index 7a64cc11de08..aeeea6efc623 100644
> --- a/include/drm/gpu_scheduler.h
> +++ b/include/drm/gpu_scheduler.h
> @@ -100,8 +100,8 @@ struct drm_sched_entity {
> * @lock:
> *
> * Lock protecting the run-queue (@rq) to which this entity belongs,
> - * @priority, the list of schedulers (@sched_list, @num_sched_list) and
> - * the @rr_ts field.
> + * @priority, @last_scheduled and the list of schedulers (@sched_list,
> + * @num_sched_list).
> */
> spinlock_t lock;
>
> @@ -215,11 +215,9 @@ struct drm_sched_entity {
> /**
> * @last_scheduled:
> *
> - * Points to the finished fence of the last scheduled job. Only written
> - * by drm_sched_entity_pop_job(). Can be accessed locklessly from
> - * drm_sched_job_arm() if the queue is empty.
> + * Points to the finished fence of the last scheduled job.
> */
> - struct dma_fence __rcu *last_scheduled;
> + struct dma_fence *last_scheduled;
>
> /**
> * @last_user: last group leader pushing a job into the entity.
next prev parent reply other threads:[~2026-09-16 12:33 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-10 7:59 [PATCH v3 0/3] drm/sched: Introduce more locking to entity Philipp Stanner
2026-09-10 7:59 ` [PATCH v3 1/3] drm/sched: Lock drm_sched_rq_pop_entity() externally Philipp Stanner
2026-09-10 7:59 ` [PATCH v3 2/3] drm/sched: Lock spsc_queue_pop() in drm_sched_entity_pop_job() Philipp Stanner
2026-09-10 7:59 ` [PATCH v3 3/3] drm/sched: Protect entity->last_scheduled with spinlock Philipp Stanner
2026-09-11 8:17 ` Tvrtko Ursulin
2026-09-16 12:54 ` Philipp Stanner
2026-09-17 7:22 ` Tvrtko Ursulin
2026-09-16 12:33 ` Christian König [this message]
2026-09-11 8:19 ` [PATCH v3 0/3] drm/sched: Introduce more locking to entity Tvrtko Ursulin
2026-09-11 10:34 ` Philipp Stanner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=14ad8ea7-c65c-48b0-94c5-bc881d7aaddd@gmail.com \
--to=ckoenig.leichtzumerken@gmail.com \
--cc=airlied@gmail.com \
--cc=dakr@kernel.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=mripard@kernel.org \
--cc=phasta@kernel.org \
--cc=simona@ffwll.ch \
--cc=tvrtko.ursulin@igalia.com \
--cc=tzimmermann@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®